Cdunit 6
Cdunit 6
• Within one basic block, the program point after a statement is the same
– IN[B] = IN[S1]
– OUT[B] = OUT[Sn]
– OUT[B] = fB (IN[B] )
– IN[B] = fB (OUT[B])
• Compiler scans each basic block to derive GEN and KILL sets
The Reaching definitions are defined by the following equations:
A Simple Code Generator
10
A Simple Code Generator
11
Register and Address Descriptors
• Address descriptor
– For each program variable
12
The Code-Generation Algorithm
• Function getReg(I)
– Selecting registers for each memory location associated with the
three-address instruction I.
• Machine Instructions for Operations
– For a three-address instruction such as x = y + z, do the following:
– If y was in Ry , do nothing.
14
The Code-Generation Algorithm
• Managing Register and Address Descriptors
1 . For the instruction LD R, x
– (a) Change the register descriptor for register R so it holds only x.
– (b) Change the address descriptor for x by adding register R as an
additional location.
2. For the instruction ST x, R, change the address descriptor for x to
include its own location.
3. For an operation such as ADD Rx , Ry , Rz for x = y + z
– (a) Change the register descriptor for Rx so that it holds only x.
– (b) Change the address descriptor for x so that its only location is Rx .
– Note that the memory location for x is not now in the address
descriptor for x .
– (c) Remove Rx from the address descriptor of any variable other than
x.
4. When process a copy statement x = y , after generating the load for y
into register Ry, if needed, and after managing descriptors as for all
load statements (per rule 1 ) :
– (a) Add x to the register descriptor for Ry .
– (b) Change the address descriptor for x so that its only location is Ry .
15
16
Design of the Function getReg
2. If y is not used after the instruction, and Ry holds only y after being
loaded, then Ry can be used as Rx; A similar option holds regarding z
and Rz ·
Exercise: Generate the machine code for the following Expressions
(a) x = a/(b+c) – d * (e + f)
(b) I = 0;
while( I < 10)
{
a[I] = 0
I = I + 1;
18
}
Assignment - 6
1. Take a sample flow graph and explain how the functions gen and kill are
computed. Also compute IN and OUT set for each block using Iteration
algorithm to compute reaching definitions.
2. Generate the code for the following block
I = 0; while(I < 10) { a[I] = 0; I = I + 1; }
3. Explain the following terms.
(a) Register Descriptor (b) Address Descriptor (c) Instruction costs
4. Explain in detail about simple code generation algorithm with example.
5. Explain about live variable analysis with example.