Presentation is loading. Please wait.

Presentation is loading. Please wait.

7.4 Boolean Expression and Control Flow Statements

Similar presentations


Presentation on theme: "7.4 Boolean Expression and Control Flow Statements"— Presentation transcript:

1 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

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

3 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 来实现。

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

5 7.4 Boolean Expression and Control Flow Statements
7.4.2 Translation of control flow statements S  if E then S1 | if E then S1 else S2 | while E do S1 | S1; S2 5/25

6 7.4 Boolean Expression and Control Flow Statements
To E.true To E.true E.code To E.false E.code To E.false E.true: E.true: S1.code S1.code . . . S1.next: goto S.next (S1.next) E.false: S2.code E s1 (a) if--then . . . E s1 s2 (b) if-then-else E.code S1.code E.true: . . . To E.true To E.false goto S.begin S.begin: (c) while-do S1.code S2.code S1.next: . . . (d) S1; S2 E s1

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

8 7.4 Boolean Expression and Control Flow Statements
E.code S1.code E.true: . . . To E.true To E.false E.false: goto S.next S2.code (b) if-then-else S  if E then S1 else S2 {E.true := newlabel; E.false := newlabel; S1.next := S.next; S2.next := S.next; S.code := E.code || gen(E.true, ‘:’) || S1.code || gen(‘goto’, S.next) || gen(E.false, ‘:’) || S2.code}

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

10 7.4 Boolean Expression and Control Flow Statements
S  S1; S2 {S.code := S1.code || gen(S1.next, ‘:’) || S2.code } S1.code S2.code S1.next: . . . (d) S1; S2 10/25

11 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

12 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 L1 L1: if c < d goto L2 goto Lfalse L2: if e < f goto Ltrue

13 7.4 Boolean Expression and Control Flow Statements
To E1.true E1.code To E1.false E1.true: E  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 } E2.code . . . E1 and E2

14 7.4 Boolean Expression and Control Flow Statements
E  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 } E  (E1 ) {E1.true := E.true; E.code := E1.code }

15 7.4 Boolean Expression and Control Flow Statements
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 } 15/25

16 7.4 Boolean Expression and Control Flow Statements
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 } E  not E1 {E1.true := E.false; E1.false := E.true; E.code := E1.code }

17 7.4 Boolean Expression and Control Flow Statements
E:a < b, The code is: if a < b goto E.true goto E.false E  id1 relop id2 {E.code := gen(‘if’, id1.place, relop.op, id2.place, ‘goto’, E.true) || gen(‘goto’, E.false) } E  true {E.code := gen(‘goto’, E.true)} E  false {E.code := gen(‘goto’, E.false)}

18 7.4 Boolean Expression and Control Flow Statements
7.4.4 Translation for Switch Statement switch E begin case V1: S1 case V2: S2 . . . case Vn - 1: Sn – 1 default: Sn end

19 7.4 Boolean Expression and Control Flow Statements
Small number of cases: t := E’s code if t  V1 goto L1 S1’s code goto next L1: if t  V2 goto L2 S2’s code L2: . . . . . . Ln-2: if t  Vn-1 goto Ln-1 Sn -1’s code | goto next Ln-1: Sn’s code next:

20 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 L1: S1's code |test: if t = V1 goto L1 goto next | if t = V2 goto L2 L2: S2's code | goto next | if t = Vn-1 goto Ln-1 | goto Ln Ln-1: Sn -1's code | next: goto next 20/25

21 7.4 Boolean Expression and Control Flow Statements
In IR, add a special case, to facilitate the special processing for it test: case V1 L1 case V2 L2 . . . case Vn-1 Ln-1 case t Ln next:

22 7.4 Boolean Expression and Control Flow Statements
7.4.5 Translation for procedure calls S  call id (Elist) Elist  Elist, E Elist  E

23 7.4 Boolean Expression and Control Flow Statements
The IR structure of calling id(E1, E2, …, En) E1.place := E1's code E2.place := E2's code . . . En.place := En's code param E1.place param E2.place param En.place call id.place, n

24 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  Elist, E {Push E.place to the end of queue} Elist  E {Initialize the queue, make it contain only E.place} E1.place | E2.place | E3.place | E4.place …..

25 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

26

27

28

29

30

31

32


Download ppt "7.4 Boolean Expression and Control Flow Statements"

Similar presentations


Ads by Google