Intermediate Code Generation 1
Intermediate Code Generation 1
Shashwat Shriparv
[email protected]
InfinitySoft
Intermediate Code Generation
Benefits is
1. Retargeting is facilitated
2. Machine independent Code Optimization can be applied.
Intermediate Code Generation
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 (Quadruples) can be used as an intermediate
language
we will use quadruples to discuss intermediate code generation
quadruples are close to machine instructions, but they are not actual
machine instructions.
Types of Intermediate Languages
Graphical Representations.
Consider the assignment a:=b*-c+b*-c:
assign assign
a + +
a
*
* *
c c
b c
Syntax Dir. Definition to produce syntax trees for
Assignment Statements.
PRODUCTION Semantic Rule
S id := E { S.nptr = mknode (‘assign’,
mkleaf(id, id.entry), E.nptr) }
E E1 + E2 {E.nptr = mknode(‘+’, E1.nptr,E2.nptr) }
E E1 * E2 {E.nptr = mknode(‘*’, E1.nptr,E2.nptr) }
E - E1 {E.nptr = mknode(‘uminus’, E1.nptr) }
E ( E1 ) {E.nptr = E1.nptr }
E id {E.nptr = mkleaf(id, id.entry) }
Three Address Code
x,y,z- names,constants or
Statements of general form x:=y op z compiler-generated temporaries
assign assign
a + +
a
*
* *
c c
b c
3-address codes are
t1:=- c t1:=- c
t2:=b * t1 t2:=b * t1
t3:=- c
t4:=b * t3 t5:=t2 + t2
t5:=t2 + t4 a:=t5
a:=t5
Types of Three-Address Statements.
More Advanced
Procedure: Index Assignments:
param x1 x:=y[ i ]
param x2 Generated as part of x[ i ]:=y
… call of proc. p(x1,x2,
param xn ……,xn)
call p,n Address and Pointer
Assignments:
x:=&y
x:=*y
*x:=y
Syntax-Directed Translation into 3-address
code.
Syntax-Directed Translation for 3-address code for
assignment statements
Use attributes
E.place to hold the name of the “place” that will hold the value of
E
Identifier will be assumed to already have the place attribute
defined.
For example, the place attribute will be of the form t0, t1, t2, …
for identifiers and v0,v1,v2 etc. for the rest.
E.code to 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
‘||’: string concatenation
PRODUCTIONSemantic 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)
while statements
E.g. while statements of the form “while E do S”
(interpreted as while the value of E is not 0 do S)
PRODUCTION
S while E do S1
To mark the 1st stmt. In
S.begin:
code for E E.code
Semantic Rule
stmt. following code S If E.place = 0 goto S.after
S.begin = newlabel;
S.after = newlabel ; S1.code
S.code = gen(S.begin ‘:’)
Goto S.begin
|| E.code S.after:
……………….
|| gen(‘if’ E.place ‘=’ ‘0’ ‘goto’ S.after)
|| S1.code
|| gen(‘goto’ S.begin)
|| gen(S.after ‘:’)
Implementation of 3 address code
Quadruples
Triples
Indirect triples
Quadruples
Declarations in a procedure
Langs. like C , Pascal allows declarations in single procedure to
be processed as a group
A global variable offset keeps track of the next available relative
addresses
Before the Ist declaration is considered, the value of offset is set
to 0.
When a new name is seen , name is entered in symbol table
with current value as offset , offset incre. by width of data object
denoted by name.
Procedure enter(name,type,offset) creates symbol table entry
for name , gives it type type ,and rel.addr. offset in its data area
Type , width – denotes no. of memory units taken by objects of
that type
SDT to generate ICode for Declarations
Using a global variable offset
PRODUCTION Semantic Rule
PD {}
DD;D
D id : T { enter (id.name, T.type, offset);
offset:=offset + T.width }
T integer {T.type = integer ; T.width = 4; }
T real {T.type = real ; T.width = 8}
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.
e.g. a := b * - (c+d)
Boolean Expressions
to E.true
E.false := S.next;
E.code
to E.false
E.true: S1.next := S.next;
S1.code
E.false:
……….. S.code := E.code ||
gen(E.true ':') ||
S1.code
2.Code for if-then-else
Semantic rules
S if E then S1 else S2
E.true := newlabel;
E.false := newlabel;
to E.true S1.next := S.next;
E.code
to E.false S2.next := S.next;
E.true:
S1.code S.code := E.code ||
goto S.next gen(E.true ':') ||
E.false: S1.code ||
S2.code gen(‘ goto‘ S.next) ||
S.next ………..
gen ( E.false ‘:’ ) ||
S2.code
3. Code for while-do
Semantic rules
S.begin := newlabel;
S while E do S1
E.true := newlabel;
E.false := S.next;
S.begin to E.true S1.next := S.begin;
E.code
to E.false
S.code := gen(S.begin ':') ||
E.true:
S1.code E.code ||
goto S.begin gen(E.true ':') ||
E.false:
………..
S1.code ||
gen('goto' S.begin)
Jumping code/Short Circuit code for boolean
expression
Boolean Expressions are translated in a sequence of
conditional and unconditional jumps to either E.true or
E.false.
a < b. The code is of the form:
if a < b then goto E.true
goto E.false
E1 or E2. If E1 is true then E is true, so E1.true = E.true.
Otherwise, E2 must be evaluated, so E1.false is set to
the label of the first statement in the code for E2.
E1 and E2. Analogous considerations apply.
not E1. We just interchange the true and false with that
for E.
Control flow translation of boolean expression
We will now see the code produced for the boolean expression E
while a < b do
if c < d then
x := y + z
else
x := y - z
Example
while a < b do
if c < d then
Lbegin: if a < b goto L1
x := y + z
goto Lnext
else L1: if c < d goto L2
x := y - z goto L3
L2: t1 := y + z
x := t1
goto Lbegin
L3: t2 := y - z
x := t2
goto Lbegin
Lnext:
Case Statements
Switch <expression>
begin
case value : statement
case value : statement
……..
case value : statement
default : statement
end
Translation of a case stmt
E E1 or M E2
| E1 and M E2
| not E1
| (E1)
| id1 relop id2
| true
| false
M ε
The New Marker , M
If E1 is false:
Then E is also false
So statements on E .falselist become part of
1
E.falselist
If E1 is true:
Still
need to test E2
Target for statements on E .truelist must be the
1
beginning of code generated for E2
Target is obtained using the marker M
New Syntax-Directed Definition (1)
Next, we reduce by E E1 or M E2
Semantic action calls backpatch({101}, 102)
E .falselist contains only 101
1
Line 101 now reads: goto 102
Statements generated so far:
100: if a < b goto _
101: goto 102
102: if c < d goto 104
103: goto _
104: if e < f goto _
105: goto _
Remaining goto instructions will have their addresses filled in
later
Annotated Parse Tree
Procedure Calls
Grammar
S -> call id ( Elist )
Elist -> Elist , E
Elist -> E
Semantic Actions
1. S -> call id (Elist) for each item p in queue do
{ gen(‘param’ p)
gen(‘call’ id.place)
}
2. Elist -> Elist , E {append E.place to the end of queue}
e.g.
P (x1,x2,x3,…………….xn)
param x1
param x2
………….
………….
param xn
call P
Shashwat Shriparv
[email protected]
InfinitySoft