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

Unit V Intermediate Code Generation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Unit V Intermediate Code Generation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Manisha Mali

[email protected]

Unit V : Semantic Analysis and Intermediate


Code Generation
Course : Language Processor and Compiler Construction

BRACT’S, Vishwakarma Institute of Information Technology, Pune-48


(An Autonomous Institute affiliated to Savitribai Phule Pune University)
(NBA and NAAC accredited, ISO 9001:2015 certified)
Department of Computer Engineering
Contents
 Semantic Analysis
 Need, Syntax Directed Translation, Syntax Directed Definitions,
Translation of assignment Statements, iterative statements, Boolean
expressions, conditional statements, Type Checking and Type
conversion
 Intermediate Code Generation
 Postfix notation, Syntax trees, Three address code, Quadruples
and triples

Manisha Mali Dept. Of


2 27-Oct-20
Computer Engg, VIIT
Contents
 Semantic Analysis
 Need, Syntax Directed Translation, Syntax Directed Definitions,
Translation of assignment Statements, iterative statements, Boolean
expressions, conditional statements, Type Checking and Type conversion

 Intermediate Code Generation


 Postfix notation
 Syntax trees
 Three address code
Quadruples
Triples

Manisha Mali Dept. Of 27-Oct-20


3
Computer Engg, VIIT
Analysis Synthesis
of input program of output program
(front-end) (back-end)
character
stream
Compiler Phases Intermediate
and Passes Lexical Analysis Code Generation

token intermediate
stream form

Syntactic Analysis Optimization

abstract intermediate
syntax tree form

Semantic Analysis Code Generation

annotated target
AST language
Intermediate Code Generation
 Translating source program into an “intermediate language.”
 Simple
 CPU Independent,
 …yet, close in spirit to machine language.

 Or, depending on the application other intermediate languages may be


used, but in general, we opt for simple, well structured intermediate
forms.

 (and this completes the “Front-End” of Compilation).

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.

 The given program in a source language is converted to an equivalent program


in an intermediate language by the intermediate code generator.

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

 postfix notation can be used as an intermediate language.

 three-address code (Quadraples or triples) can be used as an intermediate


language
 we will use quadraples to discuss intermediate code generation
 quadraples are close to machine instructions, but they are not actual machine instructions.

 some programming languages have well defined intermediate languages.


 java – java virtual machine
 prolog – warren abstract machine
 In fact, there are byte-code emulators to execute instructions in these intermediate
languages.
 Types of Intermediate Code Generation
 Postfix notation
 Syntax trees
 Three address code
Quadruples
Triples

Manisha Mali Dept. Of 27-Oct-20


7
Computer Engg, VIIT
Postfix notation

Infix Expression Postfix Expression

(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

 No built-up arithmetic expressions are allowed.

 As a result, x:=y + z * w
should be represented as
t1:=z * w
t2:=y + t1
x:=t2

 Observe that given the syntax-tree or the dag of the graphical


representation we can easily derive a three address code for assignments
as above.

 In fact three-address code is a linearization of the tree.

 Three-address code is useful: related to machine-language/ simple/


optimizable.
Example of 3-address code

Consider the assignment a:=b*-c+b*-c:

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

(1) assign (0) y

op arg1 arg2

(0) []= y i

(1) assign x (0)


Implementations of 3-address statements
 Quadruples
op arg1 arg2 result
t1:=- c
t2:=b * t1 (0) uminus c t1
t3:=- c
t4:=b * t3 (1) * b t1 t2
t5:=t2 + t4
a:=t5 (2) uminus c t3

(3) * b t3 t4

(4) + t2 t4 t5

(5) := t5 a

Temporary names must be entered into the symbol


table as they are created.
Implementations of 3-address statements, II
 Triples op arg1 arg2
t1:=- c
t2:=b * t1 (0) uminus c
t3:=- c
t4:=b * t3 (1) * b (0)
t5:=t2 + t4
a:=t5 (2) uminus c

(3) * b (2)

(4) + (1) (3)

(5) assign a (4)

Temporary names are not entered into the symbol table.


Implementations of 3-address statements, III

 Indirect Triples
op op arg1 arg2

(0) (14) (14) uminus c

(1) (15) (15) * b (14)

(2) (16) (16) uminus c

(3) (17) (17) * b (16)

(4) (18) (18) + (15) (17)

(5) (19) (19) assign a (18)


Ex. : Generate intermediate codes for the given
assignment stmt (8 Mks)
cost = rate *(start-finish) + 2 * (start-finish-100)
Solution :-
 Three address code
 Quadruples
 Triples
 Indirect triplets

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)

i) If a < b goto i+3


Ex. 1 : a < b (Relational Operator)

i) If a < b goto i+3


i+1) t1 = 0
Ex. 1 : a < b (Relational Operator)

i) If a < b goto i+3


i+1) t1 = 0
i+2) goto i+4
Ex. 1 : a < b (Relational Operator)

i) If a < b goto i+3


i+1) t1 = 0
i+2) goto i+4
i+3) t1+1
Ex. 2 : a < b and c > d
i) If a < b goto i+3
i+1) t1 = 0
i+2) goto i+4
i+3) t1 = 1
i+4) If c > d goto i+7
i+5) t2 = 0
i+6) goto i+8
i+7) t2 = 1
i+8) t3 = t1 and t2
Ex. 3 : if a<b then x=y+z else p=q+r
i) If a < b goto i+2
i+1) goto i+5
i+2) t1 = y+z
i+3) x = t1
i+4) goto ____
i+5) t2 = 0
i+6) goto i+8
i+7) t2 = q+r
i+8) p = t2
Ex. 4 : do x=y+z while a < b
i) t1 = y+z
i+1) x = t1
i+2) If a < b goto i
i+3) goto ____
Ex. 5 : while a<b do x = y+z
i) If a < b goto i+2
i+1) goto ___
i+2) t1 = y+z
i+3) x = t1
i+4) goto i
Ex. 6 : for(i=1; i<=20; i++) x=y+z
j) i = 1
j+1) If i < = 20 goto j+6
j+2) goto ____
j+3) t1 = i + 1
j+4) i = t1
j+5) goto j+1
j+6) t2 = y+z
j+7) x = t2
j+8) goto j+3
Ex. 7 : switch(i+j)
{
case 1 : x = y + z;
case 2 : u = v + w;
default : p = q + r
}
Ex. 7 : switch(i+j)
{ case 1 : x = y + z;
case 2 : u = v + w;
default : p = q + r}
i) t1 = i + j
i+1) goto i+11
i+2) t2 = y+z
i+3) x = t2
i+4) goto __
i+5) t3 = v + w
i+6) u = t3
i+7) goto ___
i+8) t4 = q + r
i+9) p = t4
i+10) goto __
i+11) if t1 = 1 goto i+2
i+12) if t1 = 2 goto i+5
i+13) goto i+8
Ex. 8 : Function call z = f(0, y+1) - 1

 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

Where width is the number of bytes required for each element


Ex. 10 : a[i,j]
 t1 = i * 20
 t1 = t1 + j
 t1 = t1 * 4
 t2 = addr(a) – 84 …………[20*4+4 = 84]
 t3 = t2[t1]
Ex. 11 : if (a <b+c)
a = a - c;
c = b * c;
1) t1 = b + c
2) t2 = a < t1
3) if t2 goto (5)
4) goto (7)
5) t3 = a – c
6) a = t3
7) t4 = b * c
8) c = t4
Ex. 12 :

While (A < C and B > D) do


If A = 1 then
C=C+1
Else
while A<= D do
A=A+3
Ex: Generate three address code for following code fragment. (8
Mks)
sum=0
for (j=1; j<=10; j++)
sum= sum + a[j] + b[j]

Ex: Generate quadruples and indirect triples for following


statement. (8 Mks)
a= b ^ (c + d) *f/g
Syntax-Directed Translation into 3-address code.

 First deal with assignments.


 Use attributes
 E.place: the name that will hold the value of E
 Identifier will be assumed to already have the place attribute defined.
 E.code:hold the three address code statements that evaluate E (this
is the `translation’ attribute).
 Use function newtemp that returns a new temporary variable that we
can use.
 Use function gen to generate a single three address statement given the
necessary information (variable names and operations).
Syntax-Dir. Definition for 3-address code
PRODUCTION Semantic Rule
S  id := E { S.code = E.code||gen(id.place ‘=’ E.place ‘;’) }
E  E1 + E2 {E.place= newtemp ;
E.code = E1.code || E2.code ||
|| gen(E.place‘:=’E1.place‘+’E2.place) }
E  E1 * E2 {E.place= newtemp ;
E.code = E1.code || E2.code ||
|| gen(E.place‘=’E1.place‘*’E2.place) }
E  - E1 {E.place= newtemp ;
E.code = E1.code ||
|| gen(E.place ‘=’ ‘uminus’ E1.place) }
E  ( E1 ) {E.place= E1.place ; E.code = E1.code}
E  id {E.place = id.entry ; E.code = ‘’ }

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)

Extension to the previous syntax-dir. Def.

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

PRODUCTION Semantic Rule


PMD {}
M {offset:=0 }
D  id : T { addtype(id.entry, T.type, offset)
offset:=offset + T.width }
T  char {T.type = char; T.width = 4; }
T  integer {T.type = integer ; T.width = 4; }
T  array [ num ] of T1
{T.type=array(1..num.val,T1.type)
T.width = num.val * T1.width}
T  ^T1 {T.type = pointer(T1.type);
T1.width = 4}
Nested Procedure Declarations
 For each procedure we should create a symbol table.
mktable(previous) – create a new symbol table where previous is the parent
symbol table of this new symbol table
enter(symtable,name,type,offset) – create a new entry for a variable in the
given symbol table.
enterproc(symtable,name,newsymbtable) – create a new entry for the
procedure in the symbol table of its parent.
addwidth(symtable,width) – puts the total width of all entries in the symbol
table into the header of that table.

 We will have two stacks:


 tblptr – to hold the pointers to the symbol tables
 offset – to hold the current offsets in the symbol tables in tblptr stack.
Keeping Track of Scope Information
Consider the grammar fraction:

PD
D  D ; D | id : T | proc id ; D ; S

Each procedure should be allowed to use independent names.


Nested procedures are allowed.
Keeping Track of Scope Information
(a translation scheme)
PMD { addwidth(top(tblptr), top(offset)); pop(tblptr); pop(offset) }

M { t:=mktable(null); push(t, tblptr); push(0, offset)}

D  D1 ; D2 ...

D  proc id ; N D ; S { t:=top(tblpr); addwidth(t,top(offset));


pop(tblptr); pop(offset);
enterproc(top(tblptr), id.name, t)}

N {t:=mktable(top(tblptr)); push(t,tblptr); push(0,offset);}

D  id : T {enter(top(tblptr), id.name, T.type, top(offset);


top(offset):=top(offset) + T.width

Example: proc func1; D; proc func2 D; S; S


translation of a simple if-statement


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

Manisha Mali Dept. Of Computer Engg, VIIT


Price Edition
 J. J. Donovan, Systems Programming, McGraw-Hill, ISBN 13:978-0-
07-460482-3, Indian Edition

50
27-Oct-20
Manisha Mali Dept. Of 51
Computer Engg, VIIT

You might also like