[comp.lang.functional] Three address code

E.Ireland@massey.ac.nz (Evan Ireland) (04/05/91)

I've had no luck with mail, so this is for csdw at Rhodes University.

>
>In an attempt to optimize a functional language, I would like to
>turn the stack based intermediate code into three address code. 
>
>Has anyone done similar conversions?  Any references would be
>greatly appreciated.

I do not have any references, but I thought that one aspect of my FAM
implementation might be of interest.

A number of interpreters and compilers that I have seen implement a stack
pointer in a register or global variable.  Then to implement various stack
operations, they use auto-increment or auto-decrement operations on the stack
pointer register.  Since I generate portable C, and thus cannot assume I have

    DATA *f (register DATA *fp)
    {
	....
    }

Thus I pass to each function the current pointer to top of stack, from which it
can index downwards to find its arguments.  Within the function, I use indexing
operations on fp, e.g. fp[3] = fp[1], to manipulate values on the stack, so I
am not continually manipulating the stack pointer.  If "f" calls another
function, it will pass the address of the current top of stack, e.g. g (&f[5]).

The advantage to me is that I have a register for a stack pointer even though I
am generating portable C code.

Now the relationship to three-address code.  If you adopt such a scheme, and
your three address instructions allow some indexing, you can sometimes generate

	ADD	fp[3],f[4],fp[3]

I hope this helps.
_______________________________________________________________________________

E.Ireland@massey.ac.nz          Evan Ireland, School of Information Sciences,
  +64 63 69099 x8541          Massey University, Palmerston North, New Zealand.