0% found this document useful (0 votes)
19 views18 pages

5.2 Design of a simple Code Generator

Uploaded by

saileone23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views18 pages

5.2 Design of a simple Code Generator

Uploaded by

saileone23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

1

Code Generation
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

j: t := a + b [ live(a) = true, live(b) = true, live(t) = true,


nextuse(a) = none, nextuse(b) = none, nextuse(t) = none ]

Attach current live/next-use information


Because info is empty, assume variables are live
5

Next-Use (Step 2)

i: a := b + c live(a) = true nextuse(a) = j


live(b) = true nextuse(b) = j
live(t) = false nextuse(t) = none
j: t := a + b [ live(a) = true, live(b) = true, live(t) = true,
nextuse(a) = none, nextuse(b) = none, nextuse(t) = none ]

Compute live/next-use information at j


6

Next-Use (Step 3)

i: a := b + c [ live(a) = true, live(b) = true, live(c) = false,


nextuse(a) = j, nextuse(b) = j, nextuse(c) = none ]

j: t := a + b [ live(a) = true, live(b) = true, live(t) = true,


nextuse(a) = none, nextuse(b) = none, nextuse(t) = none ]

Attach current live/next-use information to i


7

Next-Use (Step 4)

live(a) = false nextuse(a) = none


live(b) = true nextuse(b) = i
live(c) = true nextuse(c) = i
live(t) = false nextuse(t) = none
i: a := b + c [ live(a) = true, live(b) = true, live(c) = false,
nextuse(a) = j, nextuse(b) = j, nextuse(c) = none ]

j: t := a + b [ live(a) = false, live(b) = false, live(t) = false,


nextuse(a) = none, nextuse(b) = none, nextuse(t) = none ]

Compute live/next-use information i


8

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

Register and Address Descriptors


• A register descriptor keeps track of what is
currently stored in a register at a particular point in
the code, e.g. a local variable, argument, global
variable, etc.
MOV a,R0 “R0 contains a”
• An address descriptor keeps track of the location
where the current value of the name can be found
at run time, e.g. a register, stack location, memory
address, etc.
MOV a,R0
MOV R0,R1 “a in R0 and R1”
10

The Code Generation Algorithm


• For each statement x := y op z
1. Set location L = getreg(y, z)
2. If y  L then generate
MOV y’,L
where y’ denotes one of the locations where the value
of y is available (choose register if possible)
3. Generate
OP z’,L
where z’ is one of the locations of z;
Update register/address descriptor of x to include L
If L is a register update its descriptor
4. If y and/or z has no next use and is stored in register,
update register descriptors to remove y and/or z
11

Code Generation Algorithm


• For x:= y
– If y is in a register change the register and
address descriptor to record that x is found only
in register for y
– If y is in memory; invoke getreg and load y and
then make it the location of x (or) use MOV y,
x
12

The getreg Algorithm


• To compute getreg(y,z)
1. If y is stored in a register R and R only holds the
value y, and y has no next use, then return R;
Update address descriptor: value y no longer in R
2. Else, return a new empty register if available
3. Else, if x has a next use in the block find an occupied
register R;
Store contents (register spill) by generating
MOV R,M
for every M in address descriptor of y;
Return register R
4. Return a memory location
13

Code Generation Example


Register Address
Statements Code Generated
Descriptor Descriptor
Registers empty
t := a - b MOV a,R0 R0 contains t t in R0
SUB b,R0

u := a - c MOV a,R1 R0 contains t t in R0


SUB c,R1 R1 contains u u in R1

v := t + u ADD R1,R0 R0 contains v u in R1


R1 contains u v in R0
d := v + u ADD R1,R0 R0 contains d d in R0
MOV R0,d d in R0 and
memory
14

Code sequences for indexed assignments

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

Code sequences for indexed assignments

• 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

Code sequences for pointer


assignments
• a = *p
p is in Register Rp MOV *Rp, a (2)
p is in Mp MOV Mp, R
MOV *R, R (3)
p is in Stack MOV Sp(A), R
MOV *R, R (3)
17

Code sequences for pointer


assignments
*p := a
p is in Register Rp MOV a, *Rp (2)
p is in Mp MOV Mp, R
MOV a, *R (4)
p is in Stack MOV a, R
MOV R, *Sp(A)(4)
18

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy