Translation to Trees
Intermediate representations
Tree representation
Translating Tiger expressions into the Tree representation

Slide 2

Intermediate Representations
Will be used
To gain information about the program
To generate efficientcode
Needs to be
Easy to construct
Low-level enough to be useful for many target machine architectures
Easy to manipulate, for optimisations, information gathering

Trees
Tree language is based upon an abstract machine architecture
memory and registers
machine operators (+ some r/t utilities)
Operations (nodes) in the tree categorised into
expressions
compute some value
statements
perform side effects on memory and registers and
control flow within the tree

Expressions – abstract class Exp
CONST( int i )
TEMP( Temp.Temp temp )
BINOP( int binop, Exp left, Exp right )
MEM( Exp exp )
CALL( Exp func, ExpList args )
ESEQ( Stm stm, Exp exp )

Statements – abstract class Stm
MOVE( Exp dst, Exp src )
EXP( Exp exp )
JUMP( Exp exp, Temp.LabelList targets )
CJUMP( int relop, Exp left, Exp right,
Label iftrue, Lable iffalse )
SEQ( Stm left, Stm right
LABEL( Label label )

How to translate to trees?  View…
Program fragment in isolation of the rest of the program
Tree code for the fragment always the same
Program fragment in context, being used by the program around it
Code different according to context

So isolated components stored in Translate.Exp
Ex
Subclass of Translate.Exp for expressions with a result value
Nx
Subclass for void expressions – statements
Cx
Subclass for conditional expressions

Methods for Translate.Exp
Adjust the Ex, Nx, Cx according to the way in which it is used
Tree.Exp unEx()
Presents whatever unEx has been called on as a simple expression
Tree.Stm unNx()
Presents receiver as a statement
Tree.Stm unCx(Temp.Label t, Temp.Label f)
Presents receiver as a conditional statement, passing control either to label t or f

Translating functions
The function body is wrapped in a prologue and an epilogue
These last two perform the necessary housekeeping – saving/restoring registers, return values etc.
Each function is translated into a fragment, consisting of
Frame: m/c specifics on params and locals
Body: the tree code for the function

Chapter seven
Gives a flavour for how each construct in the language should be translated
Structure of compiler
Translate/Translate.java contains a method to translate each construct
These make calls to constructors in Tree
Semant.java contains calls to these translator methods

Finally
I’ll set up the code for download after next Tuesday’s tutorial session
Read through Chapter 7
If you can do this with code in front of you, it will probably make more sense