7 4 Boolean Expression and Control Flow Statements

7. 4 Boolean Expression and Control Flow Statements Two goals of Boolean expression Calculate logic value c = (a > b) && a > 1 Conditional expression in control flow if (a > b && a > 1) then … 1/25

7. 4 Boolean Expression and Control Flow Statements Two ways of calculation: if ( 1 or i++ > 3) then Non short-circuit E 1 or E 2 Short-circuit E 1 or E 2 => if E 1 then true else E 2 E 1 and E 2 => if E 1 then E 2 else false

7. 4 Boolean Expression and Control Flow Statements 7. 4. 1 Translation for Boolean Expressions E E or E | E and E | not E | ( E ) | id relop id | true | false a<b 100: if a < b goto 103 101: t : = 0 102: goto 104 103: t : = 1 104: >, >=, <, <=, <>, == 对于a<b的翻译而言,由于三地址代 码中不包括形如t=a<b的格式,故 此使用if x relop y goto L 来实现。

7. 4 Boolean Expression and Control Flow Statements E E 1 or E 2 {E. place : = newtemp; emit (E. place, ‘: =’, E 1. place, ‘or’ E 2. place) } E id 1 relop id 2 {E. place : = newtemp; emit (‘if’, id 1. place, relop. op, id 2. place, ‘goto’, nextstat+3 ); a < b的翻译 emit (E. place, ‘: =’, ‘ 0’ ); 100: if a < b goto 103 emit (‘goto’, nextstat + 2 ); 101: t : = 0 102: goto 104 emit (E. place, ‘: =’, ‘ 1’ ) } 103: t : = 1 104:

7. 4 Boolean Expression and Control Flow Statements 7. 4. 2 Translation of control flow statements S if E then S 1 | if E then S 1 else S 2 | while E do S 1 | S 1; S 2 5/25

7. 4 Boolean Expression and Control Flow Statements E. true: E. code To E. true To E. false S 1. code. . . E s 1 (a) if--then S. begin: E. true: E. code To E. true To E. false S 1. code goto S. begin. . . E s 1 (c) while-do E. true: E. code To E. true To E. false S 1. code S 1. next: goto S. next (S 1. next) E. false: S 2. . code E s 1 s 2 (b) if-then-else S 1. next: S 1. code S 2. code. . . (d) S 1; S 2

7. 4 Boolean Expression and Control Flow Statements E. true: E. code To E. true To E. false S 1. code S if E then S 1. . . {E. true : = newlabel; E. false : = S. next; (a) if-then S 1. next : = S. next; S. code : = E. code || gen(E. true, ‘: ’) || S 1. code }

7. 4 Boolean Expression and Control Flow Statements E. true: E. code To E. true To E. false S 1. code S if E then S 1 else S 2 goto S. next {E. true : = newlabel; E. false: S 2. code E. false : = newlabel; S 1. next : = S. next; (b) if-then-else S 2. next : = S. next; S. code : = E. code || gen(E. true, ‘: ’) || S 1. code || gen(‘goto’, S. next) || gen(E. false, ‘: ’) || S 2. code}. . .

7. 4 Boolean Expression and Control Flow Statements S. begin: E. code To E. true To E. false S while E do S 1 E. true: S 1. code {S. begin: = newlabel; goto S. begin E. true : = newlabel; . . . E. false : = S. next; (c) while-do S 1. next : = S. begin; S. code : = gen(S. begin, ‘: ’) || E. code || gen(E. true, ‘: ’) || S 1. code || gen(‘goto’, S. begin) }

7. 4 Boolean Expression and Control Flow Statements S S 1; S 2 {S. code : = S 1. code || gen(S 1. next, ‘: ’) || S 2. code } S 1. next: S 1. code S 2. code. . . (d) S 1; S 2 10/25

7. 4 Boolean Expression and Control Flow Statements 7. 4. 3 Translation for Boolean Expression based Control Flow If E of a < b format, The corresponding code is: if a < b goto E. true goto E. false

7. 4 Boolean Expression and Control Flow Statements The code for a < b or c < d and e < f is: if a < b goto Ltrue goto L 1: if c < d goto L 2 goto Lfalse L 2: if e < f goto Ltrue goto Lfalse

7. 4 Boolean Expression and Control Flow Statements E 1. true: E 1. code To E 1. true To E 1. false E 2. code E E 1 and E 2. . . {E 1. true : = newlabel; E 1. false : = E. false; E 1 and E 2. true : = E. true; E 2. false : = E. false; E. code : = E 1. code || gen(E 1. true, ‘: ’) || E 2. code }

7. 4 Boolean Expression and Control Flow Statements E E 1 and E 2 {E 1. true : = newlabel; E 1. false : = E. false; E 2. true : = E. true; E 2. false : = E. false; E. code : = E 1. code || gen(E 1. true, ‘: ’) || E 2. code } E (E 1 ) {E 1. true : = E. true; E 1. false : = E. false; E. code : = E 1. code }

7. 4 Boolean Expression and Control Flow Statements E E 1 or E 2 {E 1. true : = E. true; E 1. false : = newlabel; E 2. true : = E. true; E 2. false : = E. false; E. code : = E 1. code || gen(E 1. false, ‘: ’) || E 2. code } 15/25

7. 4 Boolean Expression and Control Flow Statements E E 1 or E 2 {E 1. true : = E. true; E 1. false : = newlabel; E 2. true : = E. true; E 2. false : = E. false; E. code : = E 1. code || gen(E 1. false, ‘: ’) || E 2. code } E not E 1 {E 1. true : = E. false; E 1. false : = E. true; E. code : = E 1. code }

7. 4 Boolean Expression and Control Flow Statements E: a < b, The code is: E id 1 relop id 2 if a < b goto E. true E. false {E. code : = gen(‘if’, id 1 goto. place , relop. op, id 2. place, ‘goto’, E. true) || gen(‘goto’, E. false) } E true {E. code : = gen(‘goto’, E. true)} E false {E. code : = gen(‘goto’, E. false)}

7. 4 Boolean Expression and Control Flow Statements 7. 4. 4 Translation for Switch Statement switch E begin case V 1: S 1 case V 2: S 2. . . case Vn - 1: Sn – 1 default: Sn end

7. 4 Boolean Expression and Control Flow Statements Small number of cases: t : = E’s code if t V 1 goto L 1 S 1’s code goto next L 1: if t V 2 goto L 2 S 2’s code goto next L 2: . . . Ln-2: if t Vn-1 goto Ln-1 Sn -1’s code | goto next Ln-1: Sn’s code next:

7. 4 Boolean Expression and Control Flow Statements Large number of cases, put the test code for the cases together t : = E's code | Ln: Sn's code goto test | goto next L 1: S 1's code |test: if t = V 1 goto L 1 goto next | if t = V 2 goto L 2: S 2's code |. . . goto next | if t = Vn-1 goto Ln-1. . . | goto Ln Ln-1: Sn -1's code | next: goto next 20/25

7. 4 Boolean Expression and Control Flow Statements In IR, add a special case, to facilitate the special processing for it test: case V 1 case V 2. . . case Vn-1 case t next: L 1 L 2 Ln-1 Ln

7. 4 Boolean Expression and Control Flow Statements 7. 4. 5 Translation for procedure calls S call id (Elist) Elist, E Elist E

7. 4 Boolean Expression and Control Flow Statements The IR structure of calling id(E 1, E 2, …, En) E 1. place : = E 1's code E 2. place : = E 2's code. . . En. place : = En's code param E 1. place param E 2. place. . . param En. place call id. place, n

7. 4 Boolean Expression and Control Flow Statements S call id (Elist) {For each E. place in the queue, emit(‘param’, E. place); emit(‘call’, id. plase, n) } Elist, E {Push E. place to the end of queue} Elist E {Initialize the queue, make it contain only E. place} E 1. place | E 2. place | E 3. place | E 4. place …. .

Revision Types of IRs, and the transformation between them Structure of the symbol table, and the storage of the scope information Two ways of realization for Boolean expressions IR structures for assignment/control flow statements, and their translation schemes IR structures for procedure call, and the translation schemes 25/25







- Slides: 32