Professional Documents
Culture Documents
3 Assignment Statements
E1 + E2
- E1
{ E.place := newtmp;
id := E
E
E
E
id
{ E.place := E1.place; }
{ p := lookup (id.name);
if p nil then E.place := p
else error }
MD
D ; D | id : T | proc id ; D ; S
E1 + E2
evaluate E1 into t1
evaluate E2 into t2
t1 := t1 + t2
:=
:=
:=
:=
:=
:=
a * b
c * d
t0 + t1
e * f
t0 - t1
t0
Count
0
1
2
1
2
1
0
comp-expr
cond-expr
bool-expr
boolean
Need a way of translating booleans and control-flow (if-thenelse) into three-address code
Numerical Representation
Flow-of-control
Represent value of boolean by position in program
E.g., if first part of or evaluates to true, jump over evaluation of
second part
a or b and not c
t1 := not c
t2 := b and t1
t3 := a or t1
Numerical Representation
Numerical Representation
E
E1 or E2
{ E.place := newtmp;
E1 and E2
{ E.place := newtmp;
not E1
{ E.place := newtmp;
100:
101:
102:
103:
104:
E
E
E
true
1' )}
false
0' )}
Numerical Representation
E
{ E.place := newtmp;
{ E.place := E1.place; }
Flow-of-Control Statements
Consider grammar
S if E then S1 | if E then S1 else S2 | while E do S1
Flow-of-Control Statements
E.code
E.true:
E.false:
S1.code
S.begin:
E.code
E.true:
...
if-then
Flow-of-Control Statements
E.true:
S1.code
goto S.next
E.false:
S.next:
E.code
S1.code
goto S.begin
S2.code
E.false:
...
while-do
...
if-then-else
Flow-of-Control Statements
S
if E then S1
E.false := S.next;
S1 .next := S.next;
S.code := E.code || gen (E.true ':' ) S1 .code }
S
Flow-of-Control Statements
{ E.true := newlabel;
S
if a
goto
L1: if c
goto
L2: if e
goto
Control-Flow Translation
E
E1 or E2
{ E1.true := E.true;
E1.false := newlabel
E2.true := E.true;
E2.false := E.false;
E1 and E2
{ E1.true := newlabel;
E1.false := E.false
E2.true := E.true;
E2.false := E.false;
E.code := E1.code || gen (E1.true ':' ) || E2.code }
Control-Flow Translation
E
not E1
{ E1.true := E.false;
E1.false := E.true
E.code := E1.code; }
( E1 )
E
{E1.true := E.true;
E1.false := E.false
E.code := E1.code;}
E
E
true
false
Control-Flow Translation
E