Topics:
Simple expressions
| S --> id := E |
S.code = E.code
& id.res := E.res
|
|
|
|
| E --> E + E |
E.res = new_temp |
E.code = E1.code
& E2.code
& E.res := E1.res + E2.res
|
|
| E --> id |
E.res = id.res |
E.code = "" |
While loops
| S --> while E to S |
S.start = new_label |
S.end = new_label |
|
S.code = S.start :
& E.code
& if E.res = 0 goto S.end
& S1.code
& goto S.start
& S.end :
|
Source language
Consider types and type conversions
E.res := new_temp if E1.type = integer and E2.type = integer then begin code(E.res := E1.res Int+ E2.res) E.type := integer end else if E1.type = real and E2.type = real then begin code(E.res := E1.res Real+ E2.res) E.type := real end else if E1.type = integer and E2.type = real then begin z := newtemp; code ( z := int_to_real E1.res) code(E.res := z Real+ E2.res) E.type := real end else if E1.type = real and E2.type = integer then begin z := newtemp; code ( z := int_to_real E2.res) code(E.res := E1.res Real+ z) E.type := real end else E.type := type_error
t1 := c Int* d t2 := Int2Real t1 t3 := b Real+ t2 a := t3
Boolean expressions
Numeric translation of Boolean expressions
if a < b r := 1 else r := 0
A |
if a < b goto D |
B |
t1 := 0 |
C |
goto E |
D |
t1 := 1 |
E |
if c < d goto H |
F |
t2 := 0 |
G |
goto I |
H |
t2 := 1 |
I |
if e < f goto L |
J |
t3 := 0 |
K |
goto M |
L |
t3 := 1 |
M |
t4 := t2 and t3 |
N |
t5 := t1 or t4 |