Professional Documents
Culture Documents
Code Generation
Part II
2
Next-Use
• A three address statement x:=y+z is said to define
x and to use y and z.
• A name in a basic block is said to be live at a
given point if its value is used after that point in
the program.
• Use of a name:
– if statement i assigns a value to x ;
– stmt j has x as an operand and control can flow from i
to j along a path with no intervening assignments to
– we can say that j uses the value of x computed at i.
3
Next-Use
• Next-use information is needed for dead-code
elimination and register assignment
• Next-use is computed by a backward scan of a
basic block and performing the following actions
on statement
i: x := y op z
– Add liveness/next-use info on x, y, and z to statement i
– Set x to “not live” and “no next use”
– Set y and z to “live” and the next uses of y and z to i
4
Next-Use (Step 1)
i: a := b + c
Next-Use (Step 2)
Next-Use (Step 3)
Next-Use (Step 4)
A Code Generator
• Generates target code for a sequence of three-
address statements using next-use information
• Uses new function getreg to assign registers to
variables
• Computed results are kept in registers as long as
possible, which means until:
– Result is needed in another computation
– End of procedure call or end of block
• Checks if operands to three-address code are
available in registers
9
a : = b[i]
i is in Register Ri MOV b(Ri), R (2)
i is in Memory Mi MOV Mi, R
MOV b(R), R (4)
i in Stack MOV Si(A), R
MOV b(R), R (4)
15
• a[i] := b
i is in Register Ri MOV b, a(Ri) (3)
i is in Memory Mi MOV Mi, R
MOV b, a(R) (5)
i in Stack MOV Si(A), R
MOV b, a(R) (5)
16
Conditional Statements
• Branch if value of register meets the required condition
• Condition codes
if x < y goto z
CMP x, y
CJ< z
Example 1
x := y +z if x < 0 goto z
MOV y, R0
ADD z, R0
MOV R0, x
CJ< z
Example 2