(Continued from previous)

    ; fifth item in the current frame
^_5 ; dereference address 5

Any memory address (including aliases) can be dereferenced with the '^' operator, however the '@' operator can only be used with relative addresses and aliases  (however, this will be changed to support @_5 even though we already know the address of _5!)

Generating Addresses

You will need to assign addresses to two types of objects, addresses for the variables declared in the source program and temporary addresses that the compiler uses for intermediate results of computations.  Assigning addresses to variables defined in the source program is simply a matter of maintaining a counter and numbering them as the parser encounters them in the VAR section. 

For virtually every instruction that computes a value you will need to generate a temporary address to store the result. The simplest way to generate valid addresses to use for temporary storage is to continue using the same counter that you used to generate addresses for the variables in the symbol table.  This way you are sure that the temporaries you create won't overwrite any parameters or local variables.

This will result in your compiler generating quite a few redundant statements like:

move   n, _1
move  _1, _2
move  _2, _3

which will tend to use up huge amounts of memory if no optimization is done.   A human can easily see that the instruction move _1, _2 is redundant if we simply move n immediately into address 3.  However, there is no practical or easy way to determine during parsing which memory locations are safe to reuse, so don't even bother trying.  It may seem silly to generate a temporary memory location to store the value of another memory location (i.e. when retrieving the contents of variables for instance) but if that is the easiest solution don't be concerned.  Getting rid of all the temporaries is a task for the optimizer and there is a very simple algorithm to optimize memory (or register) allocations once all the code has been generated.


Back to top

To contact us:

© Copyright 1999 Vassar College
Poughkeepsie, New York

Web pages  by
Keith Suderman Consulting