Академический Документы
Профессиональный Документы
Культура Документы
IA marks : 25
Exam hrs: 03
Exam marks:100
Unit 6: Intermediate code generation
Syllabus:
Variants of syntax trees;three-address
code;types & declarations;Translation
of expressions;type checking;control
flow;back patching;switch statements;
Intermediate code for procedues.
:8 hrs
Introduction
Analysis-synthesis model:
Front end analyses a source code and
Front end
Source Intermediate
Static
Parser Code
Pgm. checker
Generator
Intermediate
Code
Code
Generator
Back end
Directed Acyclic Graphs (DAG)
Leaves correspond to atomic operands
Interior nodes correspond to operators
<op, l, r>
Method:search the array for the node with label
op,left child l & right child r.If found return its value
number.If not found,we create in the array a new
node with label op left child l and right child r &
return its value number
DAG Construction
i = i + 10
= 1 id * To i
2 num 10
+
3 + 1 2
i
10
4 = 1 3
Hash table and buckets
Using hash tables are more efficient.
Hash table is a array of ‘buckets’.
The index of the bucket is computed using
a hash function h for a signature <op,l,r>.
The buckets can be implemented as linked
lists.An array of pointers indexed by the
hash value points to first nodes of the
buckets.Thus the node <op,l,r> can be
found on the list whose header index is
given by h(op,l,r).
Using Hash tables & Buckets
Pointer
To list of
nodes
(sub-
trees)
Three-address code
In three-address code,there is at most one
operator on the right side of an instuction
If more than one operator is to be used
used as addresses
Three address instructions
Assignment instructions: x=y op z;
Assignments of the form: x=op y;
Copy instructions: x=u;
An unconditional jump: goto L
Conditional jumps(1): if x goto L
Conditional jumps(2): if x relop y goto L
For procedure calls and returns
Indexed copy instructions
Address and pointer assignments: x=&y
Quadruples
Quadruples are used to implement the three
address instructions in compilers. They have
four fields: op,arg1,arg2 & result.Exceptions
are:
Instructions with unary operators Eg x=y
[2] C
[3] C
ε
Storage layout
Computing types and widths
RULE:
IF f HAS TYPE st AND x HAS TYPE s THEN
float float
long long
int int
byte
1.Widening 2.Narrowing
conversions conversions
TYPE CONVERSIONCONTD
Consider the production: E -> E1 + E2
Its semantic can be explained with the 2 functions:
max(t1,t2) : takes 2 types t1 and t2 and returns maximum of the two in the
widening hierarchy
widen(a,t,w) : performs type conversion by widening address a of type t into
a value of type w
pseudocode:
widen(addr a, type t, type w)
{ if(t=w) return a;
else if(t=int and w=float)
{ temp=new Temp();
gen(temp ‘=‘ ‘(float)’ a);
return temp;
}
else error;
}
here, a is returned if a and w are of same type
else, conversion is done in a temporary that is returned
Flow of control statements
Consider the following statements:
S->if (b) s1
where s represents statements and b represents boolean
expressions.
The translation of this to b.true
statement consists of b.code
b.code followed by to b.false
b.true:
s1.code as shown. S1.code
Based on the values of ........
b.false:
b, there are jumps
within b.code.
If block
Similar are the other flow control statements
Sdd for some control statements
production Semantic rules
P S S.Next = newlabel()
P.Code = S.code || label(S.next)
S->assign
S.code= assign.code
S->if(b) s1 b.true= vewlabel()
b.false=s1.next=s.next
s.code=b.code||label(b.true) ||s1.code
S->while(b) s1 begin=newlabel()
b.true=newlabel()
b.false=s.next
s1.next=begin
s.code=label(begin) || b.code
|| label(b.true)||s1.code
|| gen(‘goto’ begin)
S->s1 s2 S1.next=newlabel()
S2.next=s.next
s.code=s1.code || label(s1.next) || s2.code
BACKPATCHING