Homework # 8
due 2006/4/11

Temporaries Attribute Grammar

Write an attribute grammar for Cool expressions and branches that

Don't worry about running out of registers (don't spill registers). Use Figure 9.8 as an inspiration, except that you need to use Cool ASTs, not the book's, and you don't need to generate code. Please don't use a macro like handle_op. For example:

loop(cond,body : Expression)
  cond.reg = reg
  body.reg = reg
  maxreg = max(cond.maxreg,body.maxreg)

integer_constant(token : Symbol)
  maxreg = reg // don't need any others beyond this
Assume that let-bound and case-bound variables will be stored in temporary registers just like operands.

Spilling

The code generated by Figure 9.8 is very poor. In particular it may spill the same register over and over even when the saved value is not changing. A better way to do spilling is to use the ``maximum register needed'' attribute you defined in your attribute grammar, to generate code that spills an intermediate result at the point it is defined, if we know we're going to need the register for a subcomputation. That way, the intermediate value will be spilled and restored only once. This improvement can be accomplished through a few changes in the macro handle_op.



John Tang Boyland
2006-04-05