Академический Документы
Профессиональный Документы
Культура Документы
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.
assign assign
+
a + a
*
* *
b uminus uminus
uminus b
c c
b c
Syntax Dir. Definition for Assignment Statements
E → - E1 {E.nptr = mknode(‘uminus’,E1.nptr) }
E → ( E1 ) {E.nptr = E1.nptr }
As a result, x:=y + z * w
should be represented as
t1:=z * w
t2:=y + t1
x:=t2
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
e.g. a := b * - (c+d)
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)
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 ‘:’)
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
(2) uminus c
a:=t5
(3) * b t3 t4
(4) + t2 t4 t5
(5) := t5 a
op arg1 arg2
(0) []= x i
op arg1 arg2
(0) []= y i
Indirect Triples
op op arg1 arg2
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
P→ D
D → D ; D | id : T | proc id ; D ; S
D → D1 ; D2 ...