Chapter 8: Intermediate Code Generation

Control flow translation of boolean expression

 

E E1 or E 2 E1 .true := E.true
  E 1 .false := newlabel
  E2 .true := E.true
  E2 .false := E.false
  E.code := E 1 .code || gen(E 1 .false) || E2 .code
E E 1 and E2 E 1 .true := new label
  E 1 false := E.false
  E2 .true := E.true
  E2 false := E.false
  E.code := E 1 .code || gen(E1 .true) || E2 .code
   
   

E is translated into a sequence of conditional and unconditional jumps to one of the two locations: E.true or E.false depending if E is true or false. If E is of the form E1 or E2, then if E1 is true then we immediately know that E itself is true, so E1.true is the same as E.true. If E1 is false then E2 must be evaluated, so E1.false is the label of the first statement of E2. If E2 is evaluated and E2 is true, it implies that E is true, so E2.true is set to E.true. Similarly, if E2 is evaluated and it is false, the entire expression is false.

E -> E1 or E2 E1.true := E.true

E1.false := newlabel

E2.true := E.true

E2.false := E.false

E.code := E1.code || gen(E1.false) || E2.code

Analogously E1 and E2 can also be translated. Here if E1 is false then there need be no further considerations.

E -> E1 and E2 E1.true := new label

E1 false := E.false

E2.true := E.true

E2 false := E.false

E.code := E1.code || gen(E1.true) || E2.code