reg, next_free_reg, and code for each of the nodes in the syntax tree given below. Note that the second of these attributes is inherited, while the other two are synthesized. Assume that the number k of available registers is 8. You needn't use any particular algorithm to find the attribute values.
reg_names : array [0..k-1] of register_name := ["r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8"]
-- ordered set of temporaries
program → expr
⊲ expr.next_free_reg := 0
⊲ program.code := expr.code
'+' : expr1 → expr2 expr3
⊲ handle_op(expr1, expr2, expr3, "+")
'-' : expr1 → expr2 expr3
⊲ handle_op(expr1, expr2, expr3, "-")
'*' : expr1 → expr2 expr3
⊲ handle_op(expr1, expr2, expr3, "*")
'/' : expr1 → expr2 expr3
⊲ handle_op(expr1, expr2, expr3, "/")
id : expr → ε
⊲ expr.reg := reg_names[expr.next_free_reg mod k]
⊲ expr.code := [expr.reg ":=" expr.stp->name]
macro handle_op(ref result, L_operand, R_operand, op: syntax_tree_node)
result.reg := L_operand.reg
L_operand.next_free_reg := result.next_free_reg
R_operand.next_free_reg := result.next_free_reg + 1
if R_operand.next_free_reg < k
spill_code := restore_code := nil
else
spill_code := ["*sp :=" reg_names[R_operand.next_free_reg mod k]] +
["sp := sp - 4"]
restore_code := ["sp := sp + 4"] +
[reg_names[R_operand.next_free_reg mod k] ":= *sp"]
result.code := L_operand.code + spill_code + R_operand.code +
[result.reg ":=" L_operand.reg op R_operand.reg + restore_code
The grammar uses the notational conventions of Section 9.3 of Scott. In particular, square brackets delimit individual target instructions, juxtaposition indicates concatenation withing instructions, and the + operator indicates concatenation of instruction lists. You may assume that expr.stp->name yields the name of an identifier. Note that handle_op is a macro and not a grammar rule.The syntax tree to be used is
Program
|
*
/ \
+ -
/ \ / \
a b c /
/ \
d e
frame pointer register to point to a known location within a frame of a subroutine call stack.
spill code in the previous problem?
p points to a structure with a field first