


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Example: let us apply the algorithm to the following segment of 3-address code:
a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t
live = {b,c} a = b + c live = {a} t1 = a * a live = {a,t1} b = t1 + a live = { b,t1} c = t1 * b live = {b,c} t2 = c + b live = {b,c,t2} a = t2 + t live = {a,b,c}
Basic Code Generation
With live/next use information computed, the basic code generation algorithm proceeds as follows. Process the 3-address instructions from beginning to end of a block. For each instruction, use machine registers to hold operands whenever possible. A non-live value in a register can be discarded, freeing that register. The code generator uses two data structures for keeping track of register usage:
Instruction type: x = y op z
OP z’, R
where z’ = best location for z. i.e., lookup address descriptor for z. Prefer register location if z is present in a register.
OP y’, R
(y’ = best location for y)
MOV y’, R OP z’, R
MOV y’,x OP z’,x
After generating machine instructions, update information about the current best location of x. If x is in a register, update that register’s information (descriptor). If y and/or z are not live after this instruction, update register and address descriptors according.
Let us return to the 3-address code example and apply the basic code generation algorithm. Recall the basic block with liveness information:
live = {b,c} a = b + c live = {a} t1 = a * a live = {a,t1} b = t1 + a live = { b,t1} c = t1 * b live = {b,c} t2 = c + b live = {b,c,t2} a = t2 + t live = {a,b,c}
Initially three registers: ( -, -, -) all empty current values: (a,b,c,t1,t2) = (m,m,m, -, -)
End of block move all live variables to memory: MOV R3,a MOV R1,b MOV R2,c all registers available
Thus the machine code (assembly language) generated is
; a := b + c LOAD b,R ADD c,R1 ; R1 := R1 + c ; t1 := a * a MOV R1,R MUL R2,R2 ; R2 = R2* R ; b := t1 + a ADD R2,R1 ; R1 = R1+R ; c := t1 * b MUL R1,R2 ; R2 = R1*R ; t2 := c + b MOV R2,R ADD R1,R3 ; R3 = R1+R ; a := t2 + t ADD R3,R MOV R3,a ; mov live MOV R1,b ; var to memory MOV R2,c