Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler Construction Sohail Aslam Lecture 40. 2 Boolean Expressions E → E 1 and M E 2 {backpatch(E 1.truelist, M.quad); E.truelist = E 2.truelist; E.falselist.

Similar presentations


Presentation on theme: "Compiler Construction Sohail Aslam Lecture 40. 2 Boolean Expressions E → E 1 and M E 2 {backpatch(E 1.truelist, M.quad); E.truelist = E 2.truelist; E.falselist."— Presentation transcript:

1 Compiler Construction Sohail Aslam Lecture 40

2 2 Boolean Expressions E → E 1 and M E 2 {backpatch(E 1.truelist, M.quad); E.truelist = E 2.truelist; E.falselist = merge(E 1.falselist, E 2.falselist);} So the target for E 1.truelist must be the beginning of code generated for E 2

3 3 Boolean Expressions E → E 1 and M E 2 {backpatch(E 1.truelist, M.quad); E.truelist = E 2.truelist; E.falselist = merge(E 1.falselist, E 2.falselist);} This target is obtained using the marker nonterminal M.

4 4 Boolean Expressions E → E 1 and M E 2 {backpatch(E 1.truelist, M.quad); E.truelist = E 2.truelist; E.falselist = merge(E 1.falselist, E 2.falselist);} M.quad records the number of the first statement of E 2.code.

5 5 Boolean Expressions: OR E → E 1 or M E 2 { backpatch(E 1.falselist, M.quad); E.truelist = merge(E 1.truelist, E 2.truelist); E.falselist = E 2.falselist; } If E 1 is false, need to test E 2

6 6 Boolean Experssions E → not E 1 { E.truelist = E 1.falselist; E.falselist = E 1.truelist; }

7 7 Boolean Experssions E → ( E 1 ) { E.truelist = E 1.truelist; E.falselist = E 1.falselist; }

8 8 Boolean Experssions E → id 1 relop id 2 { E.truelist = makelist(nextquad()); E.falselist = makelist(nextquad()+1); emit(‘if’ id 1 relop id 2 ‘goto _’); emit(‘goto _’ ); }

9 9 Boolean Experssions E → true { E.truelist = makelist(nextquad()); emit(‘goto _’ ); }

10 10 Boolean Experssions E → false { E.falselist = makelist(nextquad()); emit(‘goto _’ ); }

11 11 Boolean Expressions M →  { M.quad = nextquad(); }

12 12 BackpatchingBackpatching  consider again, the boolean expression a < b or c < d and e < f  We carry out a bottom-up parse

13 13 BackpatchingBackpatching  In response to reduction of a < b to E, the two quadruples 100: if a < b goto _ 101: goto _ are generated

14 14 RecallRecall E → id 1 relop id 2 { E.truelist = makelist(nextquad()); E.falselist = makelist(nextquad()+1); emit(‘if’ id 1 relop id 2 ‘goto _’); emit(‘goto _’ ); } View this in a parse tree

15 15 E.t = {100} E.f = {101} c < da < be < forand

16 16 BackpatchingBackpatching  The marker non-terminal M in the production E → E 1 or M E 2 records the value of nextquad which at this time is 102.

17 17 E.t = {100} E.f = {101} c < d a < b e < f M.q = 102 orand 

18 18 BackpatchingBackpatching  The reduction of c < d to E, the two quadruples 102: if c < d goto _ 103: goto _ are generated

19 19 E.t = {100} E.f = {101} E.t = {102} E.f = {103} c < d a < b e < f M.q = 102 or and 

20 20 BackpatchingBackpatching  The marker non-terminal M in the production E → E 1 and M E 2 records the value of nextquad which at this time is 104.

21 21 E.t = {100} E.f = {101} E.t = {102} E.f = {103} c < d a < b e < f M.q = 104 M.q = 102 or and  

22 22 BackpatchingBackpatching  Reducing e < f to E, generates 104: if e < f goto _ 105: goto _

23 23 E.t = {100} E.f = {101} E.t = {102} E.f = {103} E.t = {104} E.f = {105} c < d a < b e < f M.q = 104 M.q = 102 or and  

24 24 BackpatchingBackpatching  We now reduce by the production E → E 1 and M E 2

25 25 E.t = {100} E.f = {101} E.t = {104} E.f = {103, 105} E.t = {102} E.f = {103} E.t = {104} E.f = {105} c < d a < b e < f M.q = 104 M.q = 102 orand  

26 26 Semantic Actions E → E 1 and M E 2 {backpatch(E 1.truelist, M.quad); E.truelist = E 2.truelist; E.falselist = merge(E 1.falselist, E 2.falselist);}

27 27 BackpatchingBackpatching The semantic action calls backpatch({102},104) where {102} denotes E 1.truelist containing only 102

28 28 BackpatchingBackpatching The six statements generated so far are thus 100: if a < b goto _ 101: goto _ 102: if c < d goto _ 103: goto _ 104: if e < f goto _ 105: goto _

29 29 BackpatchingBackpatching The semantic action calls backpatch({102},104) which fills in 104 in statement 102.

30 30 BackpatchingBackpatching The call fills in 104 in statement 102 100: if a < b goto _ 101: goto _ 102: if c < d goto 103: goto _ 104: if e < f goto _ 105: goto _ 104

31 31 Semantic Actions E → E 1 and M E 2 {backpatch(E 1.truelist, M.quad); E.truelist = E 2.truelist; E.falselist = merge(E 1.falselist, E 2.falselist);}

32 32 E.t = {100} E.f = {101} E.t = {104} E.f = {103, 105} E.t = {102} E.f = {103} E.t = {104} E.f = {105} c < d a < b e < f M.q = 104 M.q = 102 or and   104 103105

33 33 BackpatchingBackpatching  We now reduce by the production E → E 1 or M E 2

34 34 E.t = {100, 104} E.f = {103, 105} E.t = {100} E.f = {101} E.t = {104} E.f = {103, 105} E.t = {102} E.f = {103} E.t = {104} E.f = {105} c < d a < b e < f M.q = 104 M.q = 102 or and  

35 35 Semantic Actions E → E 1 or M E 2 { backpatch(E 1.falselist, M.quad); E.truelist = merge(E 1.truelist, E 2.truelist); E.falselist = E 2.falselist; }

36 36 BackpatchingBackpatching The semantic action calls backpatch({101},102) which fills in 102 in statement 101.

37 37 BackpatchingBackpatching 100: if a < b goto _ 101: goto 102: if c < d goto 104 103: goto _ 104: if e < f goto _ 105: goto _ 102

38 38 BackpatchingBackpatching 100: if a < b goto _ 101: goto 102 102: if c < d goto 104 103: goto _ 104: if e < f goto _ 105: goto _ These instructions will have their targets filled later in the compilation

39 39 Semantic Actions E → E 1 or M E 2 { backpatch(E 1.falselist, M.quad); E.truelist = merge(E 1.truelist, E 2.truelist); E.falselist = E 2.falselist; }

40 40 E.t = {100, 104} E.f = {103, 105} E.t = {100} E.f = {101} E.t = {104} E.f = {103, 105} E.t = {102} E.f = {103} E.t = {104} E.f = {105} c < d a < b e < f M.q = 104 M.q = 102 or and   {100} {104} {103, 105}

41 41 Flow-of-Control Statements  We now use backpatching to translate flow-of-control statements in one pass  We will use the same list- handling procedures as before

42 42 Flow-of-Control Statements  We now use backpatching to translate flow-of-control statements in one pass  We will use the same list- handling procedures as before

43 43 Flow-of-Control Statements S → if E then S | if E then S else S | while E do S | begin L end | A L → L ; S | S


Download ppt "Compiler Construction Sohail Aslam Lecture 40. 2 Boolean Expressions E → E 1 and M E 2 {backpatch(E 1.truelist, M.quad); E.truelist = E 2.truelist; E.falselist."

Similar presentations


Ads by Google