Intermediate Code Generation-17-19
Intermediate Code Generation-17-19
• Postfix notation
• Abstract Syntax Tree
• Three Address Code
C Compiler
C Program
Front end
Intermediate
code
Machine Machine
instructions for instructions for
80X86 systems SPARC systems
Front Target
Language 1 end machine 2
Eack
Intermediate end Target
Language 2 Representation machine 1
Target
Language 3 machine 3
Intermediate code
Front-end Back-end
Target machine code
• Language independent:
• 3-address code
• Postfix notation
+, *, /, -, and, or
a*(b+c/a)
a*(b+c/a)
a*(b+c/a)
a*(b+c/a)
a*(b+c/a)
a*(b+c/a)
a*(b+c/a)
The Result
• We can treat the stack generated by the prior process as the intermediate representation
of the input, in postfix notation form:
• Two solutions:
• 1. Convert to a binary op:
• Map: ‘-a’ to ‘0a-’
• 2. Create a new operator for the unary use of ‘-’:
• map: ‘-a’ to ‘a_’
Assignment:
• An assignment statement:
V=a+b can be represented as follows: Vab+=
• Expressions:
• leaves: identifiers or constants;
• internal nodes are labeled with operators;
• the children of a node are its operands.
• Statements:
• a node’s label indicates what kind of statement
it is;
• the children correspond to the components of
the statement.
if(a<b)
{
if-else
p=q;
r=s;
} < stmt-list stmt-list
else
{
a b= = = =
c=d;
e=f
}
p q r s c de f
Grammar :
E -> E + T | T
T -> T * F | F
F -> ( E ) | id
Input: id + id * id
Compile: a : = b * -c + b * -c
Code for syntax tree
t1 := - c
t2 := b * t1
t3 := - c
t4 : = b * t3
t5 := t2 + t4
a := t5
Code for DAG
t1 := - c
t2 := b * t1
t5 := t2 + t2
a := t5
BITS Pilani, Hyderabad Campus
Three-Address Code
Linearized representation of AST
a+a*(b-c)+(b-c)*d
• Attributes examples:
– code – code generated for a nonterminal
– temp – name of variable that stores result of nonterminal
• newTemp() – helper function that returns the name of a new
variable
assign E.Temp = t5
E.code =‘t1 = -c
+ t2 = b*t1
a t3 = -c
t4 = b*t3
t5 = t2+t4’
E.Temp = t4
E.code =‘t3 = -c
E.Temp = t2 t4 = b*t3’
* *
E.code =‘t1 = -c
E.Temp = t3
t2 = b*t1’
E.code =‘t3 = -c’
b uminus E.Temp = t1 b uminus
E.code =‘t1 = -c’
E.Temp = b E.Temp = b
E.code =‘’ c E.code =‘’ c
E.Temp = c E.Temp = c
E.code =‘’ E.code =‘’
BITS Pilani, Hyderabad Campus
Three address code for
assignment statements
V1=V2+V3-V4
t0=V2+V3
t1=t0-V4
V1=t1
E or E
a < b E and E
c < d e < f
E or E
a < b E and E
E or E
a < b E and E
E or E
a < b E and E
E or E
a < b E and E
E or E
a < b E and E
• One option is to create code for E, create code for S, and then
create a jump to the beginning of S or the end of S according to E’s
value.
The semantic • S
equation will be
.E.false = S.next
if E then S
For E.true, we create •
a new label between
.E’s code and S’s code
E.code
E.true: to E.false
S1.code
E.false:
…
if - then
goto S.next
E.false:
S2.code
S.next:
…
if – then - else
BITS Pilani, Hyderabad Campus
Generating Code for
Control Statements : While loop
Suppose we have the following grammar:-
S while E do S1
E1 or M E2
{ M.instr = nextinstr;}
Use M to obtain the address just before E2 code starts being
gencodeerated
E
E.t = {100}
E.f = {101}
E or M E
and
x < 100
M
E E
100: if x< 100 goto _
101: goto _
x > 200 x != y
E
E.t = {100}
E.f = {101}
M.i = 102
E or M E
and
x < 100
M
E E
100: if x< 100 goto _
101: goto _
x > 200 x != y
E
E.t = {100}
E.f = {101}
M.i = 102
E or M E
and
x < 100
E.t = {102} M
E.f = {103} E E
100: if x< 100 goto _
101: goto _
x != y
102: if x> 200 goto _ x > 200
103: goto _
E id1 relop id2 E.trueList = makeList(nextInstr);
E.falseList = makeList(nextInstr+1);
gencode (‘if’ id1.Temp relop id2.Temp ‘goto _’) ||
gencode(‘goto _’); BITS Pilani, Hyderabad Campus
Example
X < 100 or x > 200 and x != y
E
E.t = {100}
E.f = {101}
M.i = 102
E or M E
and
x < 100 M.i = 104
E.t = {102} M
E.f = {103} E E
100: if x< 100 goto _
101: goto _
x != y
102: if x> 200 goto _ x > 200
103: goto _
E
E.t = {100}
E.f = {101}
M.i = 102
E or M E
and
x < 100 M.i = 104
E.t = {102} M E.t = {104}
E.f = {103} E E E.f = {105}
100: if x< 100 goto _
101: goto _
x != y
102: if x> 200 goto _ x > 200
104: if x!=y goto _
103: goto _ 105: goto _
E
E.t = {100}
E.f = {101} E.t = {104}
M.i = 102
E.f = {103,105}
E or M E
and
x < 100 M.i = 104
E.t = {102} M E.t = {104}
E.f = {103} E E E.f = {105}
100: if x< 100 goto _
101: goto _
x != y
102: if x> 200 goto 104 x > 200
104: if x!=y goto _
103: goto _ 105: goto _
E E1 and M E2 backpatch(E1.trueList,M.instr);
E.trueList = E2.trueList;
E.falseList = merge(E1.falseList,E2.falseList);
BITS Pilani, Hyderabad Campus
Example
X < 100 or x > 200 and x != y
E.t = {100,104}
E
E.f = {103,105}
E.t = {100}
E.f = {101} E.t = {104}
M.i = 102
E.f = {103,105}
E or M E
and
x < 100 M.i = 104
E.t = {102} M E.t = {104}
E.f = {103} E E E.f = {105}
100: if x< 100 goto _
101: goto 102
x != y
102: if x> 200 goto 104 x > 200
104: if x!=y goto _
103: goto _ 105: goto _
E E1 or M E2 backpatch(E1.falseList,M.instr);
E.trueList = merge(E1.trueList,E2.trueList);
E.falseList = E2.falseList;
BITS Pilani, Hyderabad Campus
Example
t1-= i * 20
t1 = t1+j
The TAC for this expression can be written as t2 = c /*base – 84 */
t3 = 4 * t1
t4 = t2[t3]
x= t4
E->id {p=lookup(id.Temp);
if p!=Null E.Temp=p else error}
foo(a+1, b, 7)
t1 := a + 1
t2 := 7
param t1
refparam b
param t2
call foo 4