Module-5: Syntax Directed Translation, Intermediate Code Generation, Code Generation 5.1,5.2,5.3, 6.1,6.2,8.1,8.2
Module-5: Syntax Directed Translation, Intermediate Code Generation, Code Generation 5.1,5.2,5.3, 6.1,6.2,8.1,8.2
Module-5: Syntax Directed Translation, Intermediate Code Generation, Code Generation 5.1,5.2,5.3, 6.1,6.2,8.1,8.2
Front Code
Code optimizer
end Generator
In the code generation phase,
various issues can arises:
Input to the code generator
Target program
Memory management
Instruction selection
Register allocation
Evaluation order
Explain Issues in the Design of Code
Generator
The most important criterion is that it produces correct code
1.Input to the code generator
IR + Symbol table
We assume front end produces low-level IR, i.e. values of names in it can be
directly manipulated by the machine instructions.
Syntactic and semantic errors have been already detected
The code generation phase needs complete error-free intermediate code as an
input requires.
P:=Q+R
Example-inefficient code sequence
S:=P+T
MOV R0,Q
ADD R0, R
MOV P, R0
MOV R0.P
ADD R0,T
MOV S , R0
5.Register allocation issues-
Use of registers make the computations faster in
comparison to that of memory, so efficient utilization
of registers is important.
The use of registers are subdivided into two sub problems:
During Register allocation – we select only those set
of variables that will reside in the registers at each
point in the program.
During a subsequent Register assignment phase, the
specific register is picked to access the variable.
The quality of the generated code can be determined by
its speed and size.
6.Evaluation order
–
The code generator decides the order in which the
instruction will be executed.
1. The order of computations affects the efficiency of
the target code.
2. Among many computational orders, some will
require only fewer registers to hold the
intermediate results
A simple target machine model
Load operations: LD r,x and LD r1, r2
Store operations: ST x,r
Computation operations: OP dst, src1, src2
Unconditional jumps: BR L
Conditional jumps: Bcond r, L like BLTZ r, L
Addressing Modes
variable name: x
indexed address: a(r) like LD R1, a(R2) means
R1=contents(a+contents(R2))
integer indexed by a register : like LD R1, 100(R2)
Indirect addressing mode: *r and *100(r)
immediate constant addressing mode: like LD R1, #100
b = a [i]
LD R1, i //R1 = i
MUL R1, R1, 8 //R1 = Rl * 8
LD R2, a(R1) //R2=contents(a+contents(R1))
ST b, R2 //b = R2
a[j] = c
LD R1, c //R1 = c
LD R2, j // R2 = j
MUL R2, R2, 8 //R2 = R2 * 8
ST a(R2), R1 //contents(a+contents(R2))=R1
x=*p
LD R1, p //R1 = p
LD R2, 0(R1) // R2 = contents(0+contents(R1))
ST x, R2 // x=R2
conditional-jump three-address instruction
If x<y goto L
LD R1, x // R1 = x
LD R2, y // R2 = y
SUB R1, R1, R2 // R1 = R1 - R2
BLTZ R1, M // i f R1 < 0 jump t o M
Generate code for the following three-address statements
assuming all variables are stored in memory locations.
3. LD R1, a
ADD R1, R1, #1
ST x, R1
4. LD R1, a
4.x = a + b LD R2, b
5.The two statements ADD R1, R1, R2
x = b * c
ST x, R1
y = a + x
5. LD R1, b
LD R2, c
MUL R1, R1, R2
LD R3, a
ADD R3, R3, R1
ST y, R3
Generate code for the following three-address
statements assuming a and b are arrays whose element
are 4-byte values.
s=0 LD R2, #0
i=0 LD R1, R2
L1: if i > n goto L2 LD R3, n
s=s+i L1: SUB R4, R1, R3
i=i+1 BGTZ R4, L2
goto L1 ADD R2, R2, R1
L2: ADD R1, R1, #1
BR L1
L2:
Generate code for the following three-address sequence
assuming that p and q are in memory locations:
LD R1, q
y = *q LD R2, 0(R1)
q = q + 4 ADD R1, R1, #4
*p = y ST q, R1
p = p + 4 LD R1, p
ST 0(R1), R2
ADD R1, R1, #4
ST p, R1
costs associated with the addressing modes
2. LD R0, i Answer :
MUL R0, R0, 8 1. 2 + 2 + 1 + 2 = 7
LD R1, a(R0) 2. 2 + 1 + 2 + 2 = 7
ST b, R1 3. 2 + 2 + 1 + 2 = 7
Determine the costs of the following instruction
sequences:
4.LD R0, p 6. LD R0, x
LD R1, 0(R0) LD R1, y
ST x, R1 SUB R0, R0, R1
BLTZ *R3, R0
5. LD R0, p Answer:
LD R1, x 4.2 + 2 + 2 = 6
ST 0(R0), R1 5.2 + 2 + 2 = 6
6.2 + 2 + 1 + 1 = 6
Addresses in the Target Code
A statically determined area Code
B1 B2
(1) prod := 0 (3) t1 := 4* i
(4) t2 := a[t1]
(2) i := 1
(5) t3 := 4* i
(6) t4 := b[t3]
(7) t5 := t2*t4
(8) t6 := prod+t5
(9) prod := t6
(10) t7 := i+1
(11) i := t7
(12) if i<=10 goto (3)
Flow Graph
Flow graph is a directed graph. It contains the
flow of control information for the set of basic
block.
A control flow graph is used to depict that how
the program control is being parsed among
the blocks. It is useful in the loop optimization.
Block B1 is the initial node.
Block B2 immediately
follows B1, so from B1 to B2
there is an edge.
The target of jump from last
statement of B2 is the first
statement B2, so from B2
to B2 there is an edge.
B2 is a successor of B1 and
B1 is the predecessor of
B2.
Intermediate code to set a 10*10 matrix to
an identity matrix
// element [i,j]
// offset for a[i,j] (8 byte reals)
// program array starts at [1,1]
assembler at [0,0]