Intermediate Code Generation
Intermediate Code Generation
Source Program
Front Intermediate Back Target Program
Representation
End End
IR
INTERMEDIATE REPRESENTATION
Source Target
IR
program program
Machine
WHY
?
independent
Advantages Source language
Front-end phases
1
IR
x y z op
• Names
• Constants • Any operator
• Compiler generated temporaries
t1 = y * z
x+y*z
t2 = x + t 1
t2 = b * t1 a +
t3 = - c
t4 = b * t3 * *
- b -
t5 = t2 + t4 b
a = t5
c c
t2 = b * t1 a +
t3 = t-2c+ t2
at4 == tb3 * t3
*
t5 = t2 + t4
b -
a = t5
c
Three Address Code DAG
Types of statements
Three
address
code
Unconditional Conditional
Assignment Copy
jump jump
x = op z
“op” unary operator
Types of statements
Three
address
code
Address &
Index Procedure call
Pointer
assignment and return
assignment
param x1
param x2
x[i]=y x = &y .
.
.
x=y[i] x = *y param xn
call P, n
*x = y
return y
Implementation
Three
address
code
Indirect
Quadruples Triples
Triples
Implementation
Three
address
code
Indirect
Quadruples Triples
Triples
op op arg1 arg2
(0) (14) (14) - c
(1) (15) (15) * b (14)
(2) (16) (16) - c
(4) (17) (17) * b (16)
(5) (18) (18) + t2 (17)
(6) (19) (19) = t5 (18)
Reference
Alfred Aho, Ravi Sethi, Jeffrey D Ullman, Compilers Principles, Techniques and Tools,
Pearson Education Asia.