|
11.3 Code Generation from Abstract Syntax TreesConsider the following abstract syntax tree for the two assignment statements: X1 := A + BB * 12; X2 := A/2 + BB * 12; from Module 1:
Using a tree walk which generates code for the subtrees first, then applies the parent operator, the following code can be easily produced:
Load BB, R1 For two leaf nodes, the method shown loads the left-most into a register. The right-most leaf node could just as well have been the one put into the register. Exercise 3 asks the reader to write the algorithm for this method. Notice that the code continually stores the value in the register into memory in order to reuse it for the next computation. This is called a register spill. The example in this section generates code for assignment statements whose right-hand sides are expressions. The next section discusses code generation strategies for various other language constructs.
|