Unit V Intermediate Code Generation
Unit V Intermediate Code Generation
token intermediate
stream form
abstract intermediate
syntax tree form
annotated target
AST language
Intermediate Code Generation
Translating source program into an “intermediate language.”
Simple
CPU Independent,
…yet, close in spirit to machine language.
Benefits
1. Retargeting is facilitated
2. Machine independent Code Optimization can be
applied.
Intermediate Code Generation (II)
Intermediate codes are machine independent codes, but they are close to
machine instructions.
Intermediate language can be many different languages, and the designer of the
compiler decides this intermediate language.
syntax trees can be used as an intermediate language.
(A + B) * (C + D) AB+CD+*
A*B+C*D AB*CD*+
A+B+C+D AB+C+D+
Types of Intermediate Languages
Syntax trees and DAG
Graphical Representations.
Consider the assignment a:=b*-c+b*-c:
assign assign
+
a + a
*
* *
b uminus uminus
uminus b
c c
b c
Directed Acyclic Graphs for Expressions
a+ a*(b–c)+(b–c)*d
+ *
* d
a -
b c
1 Manisha Mali Dept. Of 27-Oct-20
Three Address Code
Statements of general form x:=y op z
As a result, x:=y + z * w
should be represented as
t1:=z * w
t2:=y + t1
x:=t2
t1:=- c t1:=- c
t2:=b * t1 t2:=b * t1
t3:=- c t5:=t2 + t2
t4:=b * t3 a:=t5
t5:=t2 + t4
a:=t5
Types of Three-Address Statements.
Assignment Statement: x:=y op z
Assignment Statement: x:=op z
Copy Statement: x:=z
Unconditional Jump: goto L
Conditional Jump: if x relop y goto L
Stack Operations: Push/pop
More Advanced:
Procedure:
param x1
param x2
…
param xn
call p,n
Index Assignments:
x:=y[i]
x[i]:=y
Address and Pointer Assignments:
x:=&y
x:=*y
*x:=y
Other types of 3-address statements
e.g. ternary operations like
x[i]:=y x:=y[i]
require two or more entries. e.g.
op arg1 arg2
(0) []= x i
op arg1 arg2
(0) []= y i
(3) * b t3 t4
(4) + t2 t4 t5
(5) := t5 a
(3) * b (2)
Indirect Triples
op op arg1 arg2
18
Generate intermediate codes for the given
assignment stmt (8 Mks)
cost = rate *(start-finish) + 2 * (start-finish-100)
Three address code
Quadruples
Triples
Indirect triplets
T1 = Start – finish
T2 = rate * T1
T3 = start – finish
T4 = T3 – 100
T5 = 2 * T4
T6 = T2 + T5
Cost = T6
19
Ex. 1 : a < b (Relational Operator)
t1 = y+1
param t1
param 0
call f, 2
retrieve t2
t3 = t2-1
z = t3
Ex. 9 : int I = 1, a[10];
while(i<=10) a[i] = 0;
1) i=1
2) if i <= 10 goto 4
3) goto 8
4) t1 = i * width
5) t2 = addr(a) – width
6) t2 [t1] = 0
7) goto 2
e.g. a := b * - (c+d)
Boolean Expressions
E E1 or E2
{E1.true := E.true; E1.false := newlabel;
E2.true := E.true; E2.false := E.false;
E.code := E1.code || gen(E1.false ‘:’) || E2.code; }
E E1 and E2
{E1.true := newlabel; E1.false := E.false;
E2.true := E.true; E2.false := E.false;
E.code := E1.code || gen(E1.true ‘:’) || E2.code; }
E not E1
{E1.true := E.false; E1.false := E.true;
E.code := E1.code; }
Boolean Expressions
E “(” E1 “)”
{ E1.true := E.true; E1.false := E.false;
E.code := E1.code; }
E id1 relop id2
{ E.code :=
gen(‘if’ id1.place relop.op id2.place ‘goto’ E.true)
|| gen(‘goto’ E.false); }
E true
{ E.code := gen(‘goto’ E.true); }
E false
{ E.code := gen(‘goto’ E.false); }
Type Conversion
E E1 + E2
{E.place := newtemp;
if E1.type = int and E2.type = int then begin
emit(E.place ‘:=’ E1.place ‘int+’ E2.place); E.type := int;
end else if E1.type = real and E2.type = real then begin
emit(E.place ‘:=’ E1.place ‘real+’ E2.place);
E.type := real;
end else if E1.type = int and E2.type = real then begin
u := newtemp; emit(u ‘:=’ ‘inttoreal’ E1.place);
emit(E.place ‘:=’ u ‘real+’ E2.place); E.type := real;
end else if …
}
Flow-of-Control Statements
S if E then S1
| if E then S1 else S2
| while E do S1
| switch E begin
case V1: S1
case V2: S2
…
case Vn-1: Sn-1
default: Sn
end
What about things that are not assignments?
E.g. while statements of the form “while E do S”
(intepreted as while the value of E is not 0 do S)
PRODUCTION
S while E do S1
Semantic Rule
S.begin = newlabel;
S.after = newlabel ;
S.code = gen(S.begin ‘:’)
|| E.code
|| gen(‘if’ E.place ‘=’ ‘0’ ‘goto’ S.after)
|| S1.code
|| gen(‘goto’ S.begin)
|| gen(S.after ‘:’)
Dealing with Procedures
P procedure id ‘;’ block ‘;’
Semantic Rule
begin = newlabel;
Enter into symbol-table in the entry of the procedure name the begin
label.
P.code = gen(begin ‘:’) || block.code ||
gen(‘pop’ return_address) || gen(“goto return_address”)
S call id
Semantic Rule
Look up symbol table to find procedure name. Find its begin label called
proc_begin
return = newlabel;
S.code = gen(‘push’return); gen(goto proc_begin) || gen(return “:”)
Declarations
Using a global variable offset
PD
D D ; D | id : T | proc id ; D ; S
D D1 ; D2 ...
References
D. M. Dhamdhere, Systems Programming and Operating Systems, Tata
McGraw-Hill, ISBN 13:978-0-07-463579-7, Second Revised Edition
Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman, Compilers Principles,
Techniques and Tools, Addison Wesley, ISBN:981–235–885 - 4, Low
50
27-Oct-20
Manisha Mali Dept. Of 51
Computer Engg, VIIT