Intermediate Code Generation
Intermediate Code Generation
Intermediate Code Generation
Part I
Compiler Architecture
Intermediate
Code
Static Intermediate Code Target
Parser
Checker Code generator Generator language
• Graphical IRs:
– Abstract Syntax trees
– Directed Acyclic Graphs (DAGs)
– Control Flow Graphs
• Linear IRs:
– Stack based (postfix)
– Three address code (quadruples)
Graphical IRs
a := b *-c + b*-c
:= :=
a + a +
* * *
b - (uni) b - (uni) b - (uni)
c c c
AST DAG
Implementation of DAG/AST: Value Number Method
Three-Address Code
• Source Code
– a = b * -c + b * -c
• Three address code
• Tree Representation
Three-Address Statements
• Two concepts
– Address
– Instruction
• Address
– Name: source-program names to appear as addresses
– Constant: Different types of constants
– Compiler Generated temporary:
Three-Address Instruction
Assignment Type 1: x := y op z
op is a binary arithmetic or logical operation
x, y and z are addresses
Assignment Type 2: x := op z
op is a unary arithmetic or logical operation
x and z are addresses
Copy Instruction: x := y
x and z are addresses and x is assigned the value of y
Three-Address Instructions
Indexed Assignments:
x := y[i]
sets x to the value in location i memory units beyond location y
y[i] := x
sets contents of the location i memory units beyond location y to the
value of x
Address and Pointer Assignments:
x := &y
sets the r-value of x to l-value of y
x := *y where y is a pointer whose r-value is a location
sets the r-value of x equal to the contents of that location
*x := y
sets the r-value of the object pointed by x to the r-value of y
Three address code example
L: t1=i+1 100: t1 = I +1
i=t1 101: i = t1
t2=i*8 102: t2=i*8
t3=a[t2] 103: t3=a[t2]
if t3 < v goto L 104: if t3 < v goto 100
• x = minus y
– Does not use arg2
• x = y
– Op is =
• param a1
– Uses neither arg2 nor result
• Conditional/Unconditional jumps
– Put the target label in result
Quadruples
• a = b * -c + b * -c
Quadruples
op arg1 arg2
0 [ ]= x i
1 := 0 y
Indirect Triples