0% found this document useful (0 votes)
22 views35 pages

Intermediate Code

The document discusses compiler design, specifically focusing on intermediate code generation and its benefits, such as retargeting and optimization. It details various forms of intermediate representation, including syntax trees, DAGs, and three-address code, along with examples and semantic rules for generating this code. Additionally, it covers the implementation of three-address statements and flow control constructs, providing insights into their structure and translation into intermediate code.
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)
22 views35 pages

Intermediate Code

The document discusses compiler design, specifically focusing on intermediate code generation and its benefits, such as retargeting and optimization. It details various forms of intermediate representation, including syntax trees, DAGs, and three-address code, along with examples and semantic rules for generating this code. Additionally, it covers the implementation of three-address statements and flow control constructs, providing insights into their structure and translation into intermediate code.
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

Compiler Design

Samit Biswas

samit@cs.iiests.ac.in

Department of Computer Science and Technology,


Indian Institute of Engineering Science and Technology, Shibpur

October 4, 2018

Samit Biswas Compiler Design


Intermediate Code Generation

Samit Biswas Compiler Design


Benefits of of using a machine-independent intermediate form
are:
I Retargeting is facilitated;
I A machine independent code optimizer can be applied to
the intermediate representation.

Samit Biswas Compiler Design


Intermediate Representation
I Syntax trees
I DAG
I Three Address Code

Samit Biswas Compiler Design


Example: Syntax Tree and DAG
a = b ∗ −c + b ∗ −c

Samit Biswas Compiler Design


Table: SDD to produce Syntax Trees for assignment statements
Productions Semantic Rules
S → id = E S.nptr = mknode(’assign’, mkleaf(id, id.place), E.nptr )
E → E1 + E2 E.nptr = mknode(0 +0 , E1.nptr , E2.nptr )
E → E1 ∗ E2 E.nptr = mknode(0 ∗0 , E1.nptr , E2.nptr )
E → −E1 E.nptr = mkunode(0 uminus0 , E1.nptr )
E → (E1 ) E.nptr = E1.nptr
E → id E.nptr = mkleaf (id, id.place)

Samit Biswas Compiler Design


Three Address Code
Three address code is a sequence of statements of the general
form

x = y op z

where x, y and z are names, constants or compiler-generated


temporaries; op stands for operator.
The Source language expressions like x + y ∗ z might be
translated into the following sequences:

t1 = y ∗ z
t2 = x + t1

Samit Biswas Compiler Design


Three address code is a linearised representation of Syntax
tree or DAG.
Example: a = b ∗ −c + b ∗ −c

Code for the Syntax Tree Code for the DAG


t1 = −c
t2 = b ∗ t1 t1 = −c
t3 = −c t2 = b ∗ t1
t4 = b ∗ t3 t5 = t2 + t2
t5 = t2 + t4 a = t5
a = t5

Samit Biswas Compiler Design


Types of Three Address Statements
I Assignment statements
x = y op z
Here op is a binary arithmetic or logical operation.

Samit Biswas Compiler Design


Types of Three Address Statements
I Assignment statements
x = y op z
Here op is a binary arithmetic or logical operation.
I Assignment statements
x = op z
here op is a unary operation

Samit Biswas Compiler Design


Types of Three Address Statements
I Assignment statements
x = y op z
Here op is a binary arithmetic or logical operation.
I Assignment statements
x = op z
here op is a unary operation
I Copy statements
x =y
The value of y is assigned to x.

Samit Biswas Compiler Design


I Unconditional Jump
goto L
Three address statement with label L is the next to be
executed.

Samit Biswas Compiler Design


I Unconditional Jump
goto L
Three address statement with label L is the next to be
executed.
I Conditional Jump
if x relop y goto L
Example: if a < b then 1 else 0

Samit Biswas Compiler Design


I Unconditional Jump
goto L
Three address statement with label L is the next to be
executed.
I Conditional Jump
if x relop y goto L
Example: if a < b then 1 else 0 .
.
100: if a < b goto 103
101: t = 0
102: goto 104
103: t =1
104:
.
.

Samit Biswas Compiler Design


w h i l e a < b do
i f c < d then
x = y + z
else
x = y − z

Samit Biswas Compiler Design


w h i l e a < b do
i f c < d then
x = y + z
else
x = y − z
Three address code :
L1 : i f a< b goto L2
goto Lnext
L2 : i f c < d goto L3
goto L4
L3 : t1 = y + z
x = t1
goto L1
L4 : t2 = y − z
x =t2
goto L1
Lnext :

Samit Biswas Compiler Design


I Statement for procedure calls
I Param x, set a parameter for a procedure call
I Call p, n call procedure p with n parameters
I Return y return from a procedure with return value y
(optional)
Example: procedure call: p(x1, x2, x3, . . . , xn)
param x1
param x2
param x3
...
param xn
call p, n

Samit Biswas Compiler Design


I Statement for procedure calls
I Param x, set a parameter for a procedure call
I Call p, n call procedure p with n parameters
I Return y return from a procedure with return value y
(optional)
Example: procedure call: p(x1, x2, x3, . . . , xn)
param x1
param x2
param x3
...
param xn
call p, n
I Indexed Assignments
I x = y[i] and x[i] = y

Samit Biswas Compiler Design


I Statement for procedure calls
I Param x, set a parameter for a procedure call
I Call p, n call procedure p with n parameters
I Return y return from a procedure with return value y
(optional)
Example: procedure call: p(x1, x2, x3, . . . , xn)
param x1
param x2
param x3
...
param xn
call p, n
I Indexed Assignments
I x = y[i] and x[i] = y
I Address and Pointer Assignments
I x =&y, x=*y

Samit Biswas Compiler Design


Syntax Directed Translation into Three Address Code

Production Semantic Rules


S → id = E S.code = E.code k gen(id.place,0 =0 ,E.place )
E → E1 + E2 E.place = newtemp
E.code = E1.code ||E2.code ||
gen(E.place ,0 =0 , E1.place ,0 +0 , E2.place )
E → E1 ∗ E2 E.place = newtemp
E.code = E1.code ||E2.code ||
gen(E.place ,0 =0 , E1.place ,0 ∗0 , E2.place )
E → −E1 E.place = newtemp
E.code = E1.code ||gen(E.place ,0 =0 ,0 uminus0 , E1.place )
E → (E1 ) E.place = E1.place
E.code = E1.code
E → id E.place = id.place
E.code = ’ ’

Samit Biswas Compiler Design


Three address Code : Assignment Statement
Example: a = b ∗ −c + b ∗ −c
Three Address Code:
t1 = −c
t2 = b ∗ t1
t3 = −c
t4 = b ∗ t3
t5 = t2 + t4
a = t5

Samit Biswas Compiler Design


Implementations of Three-Address Statements
A Three address code is an abstract form of Intermediate code.
This can be implemented in the form of records with fields for
the operator and the operands. Three such representations
are as follows:
I Quadruples
I Triples
I indirect Triples

Samit Biswas Compiler Design


Quadruples
It is a record structure with four fields (op, arg1, arg2, result)
I x = y op z
representing by placing y in arg1, z in arg2 and
x in result.
I x = −y or x = y
we do not use arg2.
I The fields arg1 or arg2 and result are pointers to the
symbol table.

Samit Biswas Compiler Design


Quadruples for the assignment
a = b ∗ −c + b ∗ −c

op arg1 arg2 result


(0) uminus c t1
(1) * b t1 t2
(2) uminus c t3
(3) * b t3 t4
(4) + t2 t4 t5
(5) = t5 a

Samit Biswas Compiler Design


Triples
It is a record structure with three fields (op, arg1, arg2)
I The fields arg1 or arg2 are either pointers to the symbol
table entry or pointer into Triple structure.

op arg1 arg2
(0) uminus c
(1) * b (0)
(2) uminus c
(3) * b (2)
(4) + (1) (3)
(5) = a (4)

Samit Biswas Compiler Design


Indirect Triples
Listing of Pointers to Triples is maintained by a separate
structure.

Statement
(0) (14)
(1) (15)
(2) (16)
(3) (17)
(4) (18)
(5) (19)

Samit Biswas Compiler Design


op arg1 arg2
(14) uminus c
(15) * b (14)
(16) uminus c
(17) * b (16)
(18) + (15) (17)
(19) = a (18)

Samit Biswas Compiler Design


Semantic rules generating three address code for a flow of
control statements statement:

S → if E then S1
| if E then S1 else S2
| while E do S1

we assume that a three address statement can be symbolically


labelled and the function newlabel returns a new symbolic label
each time called. We associate two labels:
I E.true : The label to which control flows if E is true.
I E.false: The label to which control flows if E is false.

Samit Biswas Compiler Design


SDD for Flow-of-Control : if − then

Production Semantic Rules


S → if E then S1 E.true = newlabel;
E.false = S.next ;
S1.next = S.next;
S.code = E.code k gen(E.true,0 :0 )k S1.code

Samit Biswas Compiler Design


SDD for Flow-of-Control : if − then − else

Production Semantic Rules


S → if E then S1 else S2 E.true = newlabel;
E.false = newlabel;
S1.next = S.next ;
S2.next = S.next
S.code = E.code k
gen(E.true ,0 :0 )k S1.code k
gen(’goto’, S.next) k
gen(E.false , ’:’ ) k S2.code

Samit Biswas Compiler Design


SDD for Flow-of-Control : while − do

Production Semantic Rules


S → while E do S1 S.begin = newlabel;
E.true = newlabel
E.false = S.next ;
S1.next = S.begin ;
S.code = gen(S.begin ,0 :0 ) k E.code k
gen(E.true ,0 :0 )k S1.code k
gen(’goto’, S.begin)

Samit Biswas Compiler Design


Semantic rules generating TAC for a while statement:
w h i l e a < b do
i f c < d then
x = y + z
else
x = y − z
Three address code :
L1 : i f a< b goto L2
goto Lnext
L2 : i f c < d goto L3
goto L4
L3 : t1 = y + z
x = t1
goto L1
L4 : t2 = y − z
x =t2
goto L1
Lnext :

Samit Biswas Compiler Design


SDD for : Boolean expression
Let us Consider the following Expression:

a < b or c < d and e < f

Suppose that true and false exists for the entire expression
have been set to Ltrue and Lfalse

if a < b goto L t r u e
goto L1
L1 : i f c < d goto L2
goto Lfalse
L2 : i f e < f goto L t r u e
goto Lfalse

Samit Biswas Compiler Design


SDD for : Boolean expression

Production Semantic Rules


E → E1 or E2 E1.true = E.true ;
E1.false = newlabel;
E2.true = E.true ;
E2.false = E.false ;
E.code = E1.code k gen(E1.false ,0 :0 )k 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 k gen(E1.true ,0 :0 )k E2.code
E → not E1 E1.true = E.false ;
E1.false = E.true
E.code = E1.code

Samit Biswas Compiler Design


SDD for : Boolean expression

Table: default

Production Semantic Rules


E → (E1 ) E1.true = E.true ;
E1.false = Efalse ;
E.code = E1.code ;
E → id1 relop id2 E.code = gen(0 if 0 , id1.place , relopop , id2.place
0 goto 0 , E 0 0
true )k gen( goto , Efalse )
E → true 0 0
Ecode = gen( goto , Etrue )
E → false Ecode = gen(0 goto0 , Efalse )

Samit Biswas Compiler Design

You might also like