next up previous
Next: Optimizations for the generated code Up: Compiler Theory: Intermediate Code Generation Previous: Three-Address Code

Boolean Expressions and Control Flow

BOOLEAN EXPRESSIONS are constructed using boolean operators. We will consider here the following rules.

E  $ \longmapsto$  E $ \bf or$ E
E  $ \longmapsto$  E $ \bf and$ E
E  $ \longmapsto$  $ \bf not$ E
E  $ \longmapsto$ $ \bf ($E$ \bf )$
E  $ \longmapsto$  $ \bf id$ $ \bf relop$ $ \bf id$
E  $ \longmapsto$  $ \bf true$
E  $ \longmapsto$  $ \bf false$


WHILE STATEMENTS WITH THE NUMERICAL METHOD.

The following syntax-directed translation generates code for a while statement.
Production Semantic Rule
S $ \longmapsto$ $ \bf while$ E $ \bf repeat$ S1 S.begin := newlabel
  S.after := newlabel
  code1 := generate(S.begin ':') | | E.code | |
  code2 := generate('if ' E.place '= 0 ' 'goto' S.after) | | S1.code
  code3 := generate('goto' S.begin) | | generate(S.after)
  S.code := code1 | | code2 | | code3
With respect to the previous syntax-directed translation


FLOW OF CONTROL STATEMENTS WITH THE JUMP METHOD. We will consider here the following rules.

S $ \longmapsto$ $ \bf if$ E $ \bf then$ S1
S $ \longmapsto$ $ \bf if$ E $ \bf then$ S1 $ \bf else$S2
S $ \longmapsto$ $ \bf while$ E $ \bf repeat$ S1
Production Semantic Rule
S  $ \longmapsto$  $ \bf if$ E $ \bf then$ S1 E.true := newlabel
  E.false := S.next
  S1.next := S.next
  S.code := E.code | | generate(E.true ':') | | S1.code
S  $ \longmapsto$  $ \bf if$ E $ \bf then$ S1 $ \bf else$ S2 E.true := newlabel
  E.false := newlabel
  S1.next := S.next
  S2.next := S.next
  code1 := E.code | | generate(E.true ':') | | S1.code
  code2 := generate('goto' S.next) | |
  code3 := generate(E.false ':') | | S2.code
  S.code := code1 | | code2 | | code3
S  $ \longmapsto$  $ \bf while$ E $ \bf repeat$ S1 S.begin := newlabel
  E.true := newlabel
  E.false := S.next
  S1.next := S.begin
  code1 := generate(S.begin ':') | | E.code
  code2 := generate(E.true ':') | | S1.code
  code3 := generate('goto' S.begin)
  S.code := code1 | | code2 | | code3
Warning! The above is a syntax-directed definition: It provides formulas for the computation of the attributes S.code (via the computations of the other attributes).


TRANSLATION OF BOOLEAN EXPRESSIONS WITH THE JUMP METHOD. We will consider here the following rules.

E $ \longmapsto$ E1 $ \bf or$ E2
E $ \longmapsto$ E1 $ \bf and$ E2
E $ \longmapsto$ $ \bf not$ E1
E $ \longmapsto$ (E1)
E $ \longmapsto$ $ \bf id_{1}^{}$ $ \bf relop$ $ \bf id_{2}^{}$
E $ \longmapsto$ $ \bf true$
E $ \longmapsto$ $ \bf false$
Production Semantic Rule
E  $ \longmapsto$  E1 $ \bf or$ E2 E1.true := E.true
  E1.false := newlabel
  E2.true := E.true
  E2.false := E.false
  E.code := E1.code | | generate(E1.false':') | | E2.code
E  $ \longmapsto$  E1 $ \bf and$ E2 E1.true := newlabel
  E1.false := E.false
  E2.true := E.true
  E2.false := E.false
  E.code := E1.code | | generate(E1.true':') | | E2.code
E  $ \longmapsto$  $ \bf not$ E1 E1.true := E.false
  E1.false := E.true
  E.code := E1.code
E  $ \longmapsto$  (E1) E1.true := E.true
  E1.false := E.false
  E.code := E1.code
E  $ \longmapsto$  $ \bf id_{1}^{}$ $ \bf relop$ $ \bf id_{2}^{}$ code1 := generate('if' $ \bf id_{1}^{}$.place $ \bf relop$ $ \bf id_{2}^{}$.place 'goto' E.true)
  code2 := generate('goto' E.false)
  E.code := code1 | | code2
E  $ \longmapsto$  $ \bf true$ E.code := generate('goto' E.true)
E  $ \longmapsto$  $ \bf false$ E.code := generate('goto' E.false)

Example 5   Let us consider the expression
a < b
Assume that Then the translation is

if a < b goto Ltrue
goto Lfalse

Observations.

Example 6   Now consider the expression
a < b or c < d
Again assume that the labels Ltrue and Lfalse have been set for the entire expression. Then the translation is

if a < b goto Ltrue
goto L1
L1: if c < d goto Ltrue
goto Lfalse

Example 7   Now consider the expression
a < b or (c < d and e < f)
Then the translation is

if a < b goto Ltrue
goto L1
L1: if c < d goto L2
goto Lfalse
L2: if e < f goto Ltrue
goto Lfalse

Of course the generated code is not optimal! Indeed the second statement can be eliminated.

Example 8   Finally consider the expression
while a < b do
    if c < d then
       x := y + z
    else
       x := y - z
Then the translation is

L1: if a < b goto L2
goto Lnext
L2: if c < d goto L3
goto L4
L3: t1 := y + z
x := t1
goto L1
L4: t2 := y - z
x := t2
goto L1
Lnext:


next up previous
Next: Optimizations for the generated code Up: Compiler Theory: Intermediate Code Generation Previous: Three-Address Code
Marc Moreno Maza
2004-12-02