0% found this document useful (0 votes)
21 views

Module 4 - Intermediate Code Generation

Module 4 covers intermediate code generation, including various forms such as abstract syntax trees, postfix notation, and three address code. It explains the representation of three address code through quadruples, triples, and indirect triples, along with types of three address statements like assignment and jump statements. Additionally, it discusses the process of backpatching for control flow statements and the semantic actions involved in generating three address code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Module 4 - Intermediate Code Generation

Module 4 covers intermediate code generation, including various forms such as abstract syntax trees, postfix notation, and three address code. It explains the representation of three address code through quadruples, triples, and indirect triples, along with types of three address statements like assignment and jump statements. Additionally, it discusses the process of backpatching for control flow statements and the semantic actions involved in generating three address code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 61

Module 4 – Intermediate Code

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

* * *

b uminus b uminus b uminus

c c Syntax Tree c DAG


Postfix Notation
• Postfix notation is a linearization of a syntax tree.
• In postfix notation the operands occurs first and then operators
are arranged.
• Ex: (A + B) * (C + D)
Postfix notation: A B + C D + *

• 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.

Triple Indirect Triple


No. Operator Arg1 Arg2 Statement No. Operator Arg1 Arg2
(0) uminus a (0) (14) (0) uminus a
(1) * (0) b (1) (15) (1) * (14) b
(2) uminus a (2) (16) (2) uminus a
(3) * (2) b (3) (17) (3) * (16) b
(4) + (1) (3) (4) (18) (4) + (15) (17)
(5) = x (4) (5) (19) (5) = x (18)
Exercise
Write quadruple, triple and indirect triple for following:
1. -(a*b)+(c+d)
2. a*-(b+c)
3. x=(a+b*c)^(d*e)+f*g^h
4. z=g+a*(b-c)+(x-y)*d
Types of three address statements
1. Binary Assignment statements
2. Unary Assignment statements
3. Copy statements
4. Unconditional jump
5. Conditional jump
6. Procedural call
7. Indexed assignments
8. Address and pointer assignments
1. & 2. Assignment Statements
1. Binary Assignment statements
• Syn: X = Y op Z where X,Y,Z are compiler generated statements
• Ex: a = b+c*d
Three address statement
t1 = c * d
t2 = b + t1
a = t2
3. Copy statements
2. Unary Assignment statements Syn: X = Y
• Syn: X = op Y Ex: a = t2
• Ex: a = b*-c Three address statement
Three address statement a = t2
t1 = -c
t2 = b * t1
a = t2
4. Unconditional jump & 5. Conditional
jump
• Unconditional jump
• Syn: goto L [the control will be transferred to the
three address statement labelled with L]

• 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)}

Elist  Elist, E {append E.place to the end of queue}

Elist  E {initialize queue to contain E.place}


Example
add (a, b) QUEU a b
E p p

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

• Makelist(i) – creates a new list with index, i

• Merge(L1, L2) – concatenate List, L1 with list, L2

• Backpatch(L, label) – fills all 3ac in list, L with the


target, Label

• Nextquad – gives index of next quadruple


SDT for Boolean expression using
backpatching
A non-terminal marker M in the grammar
generate a semantic action to pick up, at
suitable times, the index of the next
instruction to be created.

m.quad should be filled with all


statements in E1.falselist
SDT for Boolean expression using
backpatching
M M.quad should be
index filled with all
of E2 statements in
E1.falselist

A non-terminal marker M in the grammar generate


a semantic action to pick up, at suitable times, the
index of the next instruction to be created.
a<b or c>d and e<f

100: if a<b goto --------------


101: goto --------------
102: if c>d goto --------------
103: goto --------------
104: if e<f goto --------------
105: goto --------------
106: (true part - code)
107: (false part – code)
a<b or c>d and e<f

100: if a<b goto 106


101: goto 102
102: if c>d goto 104
103: goto 107
104: if e<f goto 106
105: goto 107
106: (true part - code)
107: (false part – code)
Flow-of-Control Statements -
Backpatching

SDTs for Control Flow statements

You might also like