Phase 3: Generating SPARC Code

First, you should convert the printing routine in the previous phase so that it prints each expression as a SPARC comment. (SPARC comments star with the ! character and continue to the end of the line.

Next, you should generate SPARC code for the expression trees built by the code in the previous phase. You do not need to generate code for all of the C operators. In particular, you do not need to generate code for the following operators:
symbols operator names
() [] -> . function call, array index, pointer dereference, member access
! * & (type) sizeof logical negation, pointer dereference, address of, type case, sizeof
&& || ?: logical and, logical or, conditional
, comma

You should assume that every value and every variable refers to an integer (i.e., int).

Remember, make it work then try to generate good code.

Suggested steps:

  1. Write a fully recursive code generator that puts an expression tree into a specified register.
  2. Use the sethi operation and displaced addressing to load and store values. (This will change the routine that you use to generate lvalues.)
  3. Use the 13-bit immediate constants provided by the SPARC data manipulation instructions whenever possible. (You'll need to handle constant expressions and should consider commutativity or operators.)
  4. Keep track of what's in each register and try to re-use register values whenever possible. This is a pretty big step. Two things happen: first, you need to try to keep values in registers; second, you need to recognize that your code generator may not put the value in the register you asked it to use.

Barney Maccabe
Last modified: Thu Oct 2 14:40:09 MDT 1997