Module 4 - Intermediate Code Generation
Module 4 - Intermediate Code Generation
Generation
Different Intermediate Forms
• Abstract syntax tree
• Postfix notation
• Three address code
Abstract syntax tree & DAG
• A syntax tree depicts the natural hierarchical structure of a
source program.
• A DAG (Directed Acyclic Graph) gives the same information but
in a more compact way because common sub-expressions are
identified.
= =
• Ex: a=b*-c+b*-c
+
a +
a
* * *
• Ex: (A + B) * C
Postfix notation: A B + C *
• Ex: (A * B) + (C * D)
Postfix notation: A B * C D * +
Three address code
• Three address code is a sequence of statements of the general
form,
a:= b op c
• Where a, b or c are the operands that can be names or
constants and op stands for any operator.
• Example: a = b + c + d
t1=b+c
t2=t1+d
a= t2
• Here t1 and t2 are the temporary names generated by the
compiler.
• There are at most three addresses allowed (two for operands
Different Representation of Three Address
Code
• There are three types of representation used for three
address code:
1. Quadruples
2. Triples
3. Indirect triples
• Ex: x= -a*b + -a*b
t1= - a
t2 = t 1 * b
t3= - a
t4 = t 3 * b
t5 = t 2 + t 4
x= t5
Quadruple
• The quadruple is a structure with at the most four fields such
as op, arg1, arg2 and result.
• The op field is used to represent the internal code for operator.
• The arg1 and arg2 represent the two operands.
• And result field is used to store the result of an expression.
Quadruple
No. Operator Arg1 Arg2 Result
x= -a*b + -a*b
(0) uminus a t1
t 1= - a
(1) * t1 b t2
t2 = t 1 * b
t 3= - a (2) uminus a t3
t4 = t 3 * b (3) * t3 b t4
t5 = t 2 + t 4 (4) + t2 t4 t5
x= t5 (5) = t5 x
Triple
• To avoid entering temporary names into the symbol table, we
might refer a temporary value by the position of the statement
that computes it.
• If we do so, three address statements can be represented by
records with only three fields: op, arg1 and arg2.
Quadruple Triple
No. Operator Arg1 Arg2 Result No. Operator Arg1 Arg2
(0) uminus a t1 (0) uminus a
(1) * t1 b t2 (1) * (0) b
(2) uminus a t3 (2) uminus a
(3) * t3 b t4 (3) * (2) b
(4) + t2 t4 t5 (4) + (1) (3)
(5) = t5 x (5) = x (4)
Indirect Triple
• In the indirect triple representation the listing of triples has
been done. And listing pointers are used instead of using
statement.
• This implementation is called indirect triples.
• Conditional jump
• Syn: if x relop y goto L
• Ex: if x > y goto L
• True the control will be transferred to statement labelled
with L
• False next statement after conditional jump statement
6. Procedure call
• Syn: P (x1, x2, …, xn)
• Three address statement
Param x1
Param x2
…
Param xn
Call P, n
• Syn: return y
• Three address statement
return y
7. Indexed Assignments
• Syn: x = y[index] & y[index] = x
• Ex: a[i]=b[i]
Three address statement
t=b[i]
a[i]=t
8. Address and pointer assignments
• Syn: x = & y, x = *y, *x = y
• Ex: *x = *y
Three address statement
t = *y
*x = t
Boolean Expression (True / False)
1. Compute logical values (T / F)
2. Conditional expressions in flow of control statements
• Ex: if BE then S
if BE then S1 else S2
while BE do S
• Boolean operators: and, or, not
Operators Precedence Associativit
y
or 3 [Low] Left
and 2 Left
not 1 [High] Right
1. SDT to construct 3AC for
Booleans
•
•
•
•
•
•
•
𝐸 → 𝐸 1 𝑜𝑟 𝐸 2
𝐸 → 𝐸 1 𝑎𝑛𝑑 𝐸 2
𝐸 → 𝑛𝑜𝑡 𝐸 1
𝐸 → ( 𝐸 ¿¿ 1 )¿
𝐸 → 𝑖𝑑1 𝑟𝑒𝑙𝑜𝑝 𝑖𝑑 2
𝐸 → 𝑡𝑟𝑢𝑒
𝐸 → 𝑓𝑎𝑙𝑠𝑒
Example
1. a or b and not c 2. a < b
Three address if a < b then 1 else 0
representation Three address representation
t1 = not c 100: if a < b then goto 103
t2 = b and t1 [100 + 3]
t3 = a or t2 101: t = 0
102: goto 104 [102 + 2]
103: t = 1
Example
100: if a<b then goto 103
101: t1=0
• a<b or c<d and e<f 102: goto 104
103: t1=1
104: if c<d then goto 107
105: t2=0
106: goto 108
107: t2=1
108: if e<f then goto 111
109: t3=0
110: goto 112
111: t3=1
112: t4 = t2 and t3
113: t5 = t1 or t4
2. Control Flow Translation of Boolean
Expressions [Short circuit code or jump code]
• Generate code without generating code for Boolean
operators
• Generate code without evaluating the entire expression
• Ex:
A<B
If A<B goto E.true
Goto E.false
SDT to construct 3AC for Booleans
SDT to construct 3AC for Booleans –
cont…
Assignment Statement
Production rule Semantic actions
S → id :=E {p = look_up(id.name);
If p ≠ nil then
Emit (p = E.place)
Else
Error;
}
E → E1 + E2 {E.place = newtemp();
Emit (E.place = E1.place '+' E2.place)
}
E → E1 - E2 {E.place = newtemp();
Emit (E.place = E1.place ‘-' E2.place)
}
E → E1 * E2 {E.place = newtemp();
Emit (E.place = E1.place ‘*' E2.place)
}
E → E1 / E2 {E.place = newtemp();
Emit (E.place = E1.place ‘/' E2.place)
}
Assignment Statement
Production rule Semantic actions
E → - E1 {E.place = newtemp();
Emit (E.place = ‘uminus’ E1.place)
}
E → (E1) {E.place = E1.place}
E → id {p = look_up(id.name);
If p ≠ nil then
p = E.place
Else
Error;
}
The p returns the entry for id.name in the symbol table.
The Emit function is used for appending the three address code to the
output file. Otherwise it will report an error.
The newtemp() is a function used to generate new temporary variables.
E.place holds the value of E.
Flow of control statements
1. Code for if-then
2. Code for if-then-else
3. Code for while-do
Example
While a<b do
If c<d then
X=y+z
Else
X=y-z
Case Statements
Translation of a case statement
Procedure calls
main()
Function Calling: P(X1,X2,
{
….,Xn) .
Three address code: .
Param x1 add(x,y)
.
Param x2
.
. }
. void add(int a, int b)
. {
Param xn .
Call P, n .
}
SDT for procedure calls
S call id (Elist) {for each item p on queue do emit
(‘param’ p);
emit(‘call’ id.place)}
Three address
statement
param a
param b
call add
NOTE:
Elist.nd = for storing the number of actual
parameters
Backpatching
• Problem generated by short-circuit code in generation of
three address code
• Process of delaying the address generation
• Leaves the label unspecified and fill it later
• Ex:
a<b or c>d and e<f
Functions in backpatching