Code
* code goneat addaeg
Bue
Code
stoteeds and
to sto
stattnut
(onald
tatmen La:
tf Can
ApD
na
Cost
(o1)
ADD (out= 2
Mimony lecato
Moy C
mevo
ADD RS to Rs and aodd
Reglaten and oddess dusenipdion
* The cod
dusciptoos
Co ntent
deunp toack
hames
track d docation
* keup name Can be
walue
be
addes those
Raisken daoaiptan n
kuup tatt srod
A
code-generation algorithm
The algorithm takes as input a sequence of three-address statements constituting a basic block. For each
three-address statement of the formx:=y opz, perform the following actions:
1. Invoke a function getreg to determine the location L where the result of the computation y op z
should be stored.
2. Consult the address descriptor for y to determine y', the current kocation of y. Prefer the register for
' if the value of y is currently both in memory and a register. If the value of y is not already in L,
generate the instruction MOV y',Lto place a copy of yin L.
3. Generate the instruction OP 2', L where z' is a current location of z. Prefer a register to a memory
location if z is in both. Update the address descriptor of x to indicate that x is in location L. If x is in L,
update its descriptor and remove x from all other descriptors.
4. If the current values of y or z have no next uses, are not live on exit from the block, and are in
registers, alter the register descriptor to indicate that, after execution of x :=y op z, those registers will
no longer contain y or z
Generating Code for Assignment Statements:
" The assignment d : (a-b) + (a-c) + (a-c) might be translated into the following three-address code
sequence:
Code sequence for the example is:
t:a-b
u:a-c
V:tt u
d:v+u
with d live at the end
Code sequence for the example is:
Statements Code Generated Register descriptor Address descriptor
Register empty
t:a-b MOV a, RO RO contains t t in RO
SUB b, RO
u:a-c MOV a, RI RO contains t Iin RO
SUB c, RI RI contains u u in RI
Vtt u ADD RI, RO RO contains v u in RI
RI contains u V in R0
d:y+u ADD RI, RO RO contains d d in RO
d in RO and memory
MOV RO, d
Generating Code for Indexed Assignments
The table shows the code sequences generated for the indexed assignmen a:= b[ i] and al i ]:=b
Statements Code Generated Cost
a :- b[i] MOV b(Ri), R 2
a[i] :b MOV b, a(Ri) 3
Generating Code for Pointer Assignments
The table shows the code sequences generated for the pointer assignments a : = "p and "p:= a
Statements Code Generated Cost
a:=p MOV Rp, a 2
"p:a MOV a, Rp 2
Generating Code for Conditional Statements
Statement Code
ifx <y goto z CMP ×,y
CJ< z /° jump to z if condition code is negative /
MOV y, RO
if x <0 goto z ADD z, RO
MOV RO.X
CJ<z