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.

Slides:



Advertisements
Similar presentations
Chapter 6 Intermediate Code Generation
Advertisements

Code Generation for Control Flow Mooly Sagiv Ohad Shacham Chapter 6.4.
Decision Structures – The Syntax Tree Lecture 22 Fri, Apr 8, 2005.
Compiler Construction Sohail Aslam Lecture StackInput ¤0¤0 id – id  id $ s4 ¤0 id 4 – id  id $ r6 F → id ¤0F3¤0F3 – id  id $ r5 T → F ¤0T2¤0T2.
Intermediate Code Generation
Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.
Backpatching: The syntax directed definition we discussed before can be implemented in two or more passes (we have both synthesized attributes and inheritent.
Lecture 08a – Backpatching & Recap Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6.
Short circuit code for boolean expressions: Boolean expressions are typically used in the flow of control statements, such as if, while and for statements,
8 Intermediate code generation
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
1 Compiler Construction Intermediate Code Generation.
Generation of Intermediate Code Compiler Design Lecture (03/30//98) Computer Science Rensselaer Polytechnic.
Compiler Construction Sohail Aslam Lecture Finite Automaton of Items Then for every item A →  X  we must add an  -transition for every production.
Lecture 20 Types / Offsets Revisited Topics Procedural Abstraction Activation records Readings: 8.2 March 29, 2006 CSCE 531 Compiler Construction.
Compiler Designs and Constructions
Three Address Code Generation Backpatching-I Prepared By: Siddharth Tiwary 04CS3010.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Backpatching דוגמא : switch. דוגמא switch : הדקדוק.
Lecture 09 – IR (Backpatching) Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6
Theory of Compilation Erez Petrank Lecture 7: Intermediate Representation Part 2: Backpatching 1.
Theory of Compilation Erez Petrank Lecture 6: Intermediate Representation 1.
Lecture 15 Control Flow Topics Review Positional Encoding of Booleans Short Circuit Evaluation Control Flow Statements Readings: 8.4, 8.6 March 13, 2006.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
1 Intermediate Code generation. 2 Intermediate Code Generation l Intermediate languages l Declarations l Expressions l Statements l Reference: »Chapter.
Test 2 Post Mortem Topics Questions and points Grade Distribution Answers April, 2006 CSCE 531 Compiler Construction.
Compiler Construction CS 606 Sohail Aslam Lecture 2.
Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f.
What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.
1 Structure of a Compiler Front end of a compiler is efficient and can be automated Back end is generally hard to automate and finding the optimum solution.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 6, 10/02/2003 Prof. Roy Levow.
Topic #7: Intermediate Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Intermediate Code Generation Abstraction at the source level identifiers, operators, expressions, statements, conditionals, iteration, functions (user.
Winter Compilers Software Eng. Dept. – Ort Braude Compiling Assignments and Expressions Lecturer: Esti Stein brd4.ort.org.il/~esti2.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Boolean expressions 1 productionsemantic action E  E1 or E2E1.trueLabel = E.trueLabel; E1.falseLabel = freshLabel(); E2.trueLabel = E.trueLabel; E2.falseLabel.
Intermediate code generation Simplifying initial assumptions a 3-address code will be used for instructions  choice of instruction mnemonics and layouts.
Chap. 4, Intermediate Code Generation
1 Structure of a Compiler Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator.
Theory of Compilation Erez Petrank Lecture 6: Intermediate Representation and Attribute Grammars 1.
1 February 28, February 28, 2016February 28, 2016February 28, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Code Generation for Control Flow Mooly Sagiv html:// Chapter
Three Address Code Generation of Control Statements continued..
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
CSCE 531 Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Construction
Subject Name:COMPILER DESIGN Subject Code:10CS63
Compiler Optimization and Code Generation
Intermediate Code Generation Part II
Chapter 6 Intermediate-Code Generation
Intermediate Code Generation Part II
Three Address Code Generation - Control Statements
Intermediate Code Generation Part II
Compiler Construction
Compiler Design 21. Intermediate Code Generation
Three Address Code Generation – Backpatching
Intermediate Code Generation Part II
Compiler Construction
Boolean Expressions Lecture 25 Fri, Apr 22, 2005.
Intermediate Code Generation
Compiler Construction
Intermediate Code Generation Part II
Review: For array a[2,5], how would the memory for this array looks like using row major layout? What about column major layout? How to compute the address.
Compiler Construction
Compiler Construction
Compiler Design 21. Intermediate Code Generation
Intermediate Code Generating machine-independent intermediate form.
Presentation transcript:

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 = 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 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 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 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 Boolean Experssions E → not E 1 { E.truelist = E 1.falselist; E.falselist = E 1.truelist; }

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

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 Boolean Experssions E → true { E.truelist = makelist(nextquad()); emit(‘goto _’ ); }

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

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

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

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

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 E.t = {100} E.f = {101} c < da < be < forand

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 E.t = {100} E.f = {101} c < d a < b e < f M.q = 102 orand 

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

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 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 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 BackpatchingBackpatching  Reducing e < f to E, generates 104: if e < f goto _ 105: goto _

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 BackpatchingBackpatching  We now reduce by the production E → E 1 and M E 2

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 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 BackpatchingBackpatching The semantic action calls backpatch({102},104) where {102} denotes E 1.truelist containing only 102

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 BackpatchingBackpatching The semantic action calls backpatch({102},104) which fills in 104 in statement 102.

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

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 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  

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

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 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 BackpatchingBackpatching The semantic action calls backpatch({101},102) which fills in 102 in statement 101.

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

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

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 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 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 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 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