1 Chapter - 5: Intermediate Code Generation Bahir Dar Institute of Technology
1 Chapter - 5: Intermediate Code Generation Bahir Dar Institute of Technology
1 Chapter - 5: Intermediate Code Generation Bahir Dar Institute of Technology
Phases of compiler
Chapter – 5 : Intermediate Code Generation 2 Bahir Dar Institute of Technology
Introduction to intermediate code generation
• Intermediate code is the interface between front end and back end in a
compiler
• It receives input from its predecessor phase semantic analyzer, in the
form of an annotated syntax tree.
• Ideally the details of source language are confined to the front end and
the details of target machines to the back end
▪ Means that, m * n compliers can be built by writing m front ends and n
back ends – save considerable amount of effort
▪ In a compiler,
• the front end translates source program into an
intermediate representation,
• and the back end generates the target code from this
intermediate representation.
C SPARC
Pascal HP PA
FORTRAN x86
C SPARC
Pascal HP PA
IR
FORTRAN x86
* * a +
b - (unary) b - (unary) *
b - (unary)
c c
AST c
DAG
Chapter – 5 : Intermediate Code Generation 10 Bahir Dar Institute of Technology
Constructing DAG/AST using Value Number Method
• Nodes of a syntax tree or DAG are stored in an array of records.
• Each row of the array represents one record, and therefore one node.
• In each record, the first field is an operation code, indicating the label
of the node.
• Leaves have one additional field, which holds the lexical value (either a
symbol-table pointer or a constant, in this case), and
• interior nodes have two additional fields indicating the left and right
children
• Egg. Representation of statement: i =i+10
x = 5*y + 5*y/3;
else B1 B2
y = 5; x = 5*y + 5*y/3 y = 5
x = x+y;
B3 x = x+y
b - c appears
once b/c this
code is for DAG
• DAG Representation
Three address code representation
y * z
x + t0
* y z t0 + x t0 t1
+ x t0 t1
= t1 a
= t1 a
Less
Easy to
space
Re-order
op arg1 arg2
• Example-2: X[i]:= y 0 [ ]= x i
• But this instruction is difficult 1 := 0 y
• It takes two triples
Chapter – 5 : Intermediate Code Generation 26 Bahir Dar Institute of Technology
Triples
Triple representations of statement: a = b*- c + b*- c
• Declarations
• Declarations in procedures
• Flow of control statements
• Backpatching and Procedure calls