Compiler Designs and Constructions

Slides:



Advertisements
Similar presentations
Chapter 6 Intermediate Code Generation
Advertisements

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.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Compiler Construction Sohail Aslam Lecture Boolean Expressions E → E 1 and M E 2 {backpatch(E 1.truelist, M.quad); E.truelist = E 2.truelist; E.falselist.
Three Address Code Generation Backpatching-I Prepared By: Siddharth Tiwary 04CS3010.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Lecture 15 Control Flow Topics Review Positional Encoding of Booleans Short Circuit Evaluation Control Flow Statements Readings: 8.4, 8.6 March 13, 2006.
1 CMPSC 160 Translation of Programming Languages Fall 2002 Lecture-Modules 17 and 18 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
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.
Lecture 22 Code Generation Topics Arrays Code Generation Readings: 9 April 10, 2006 CSCE 531 Compiler Construction.
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
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.
CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson.
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.
Chapter 8: Intermediate Code Generation
Review: –What is an activation record? –What are the typical fields in an activation record? –What are the storage allocation strategies? Which program.
Intermediate Code Generation
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.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Boolean expressions 1 productionsemantic action E  E1 or E2E1.trueLabel = E.trueLabel; E1.falseLabel = freshLabel(); E2.trueLabel = E.trueLabel; E2.falseLabel.
Code Generation CPSC 388 Ellen Walker Hiram College.
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.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
1 February 28, February 28, 2016February 28, 2016February 28, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Three Address Code Generation of Control Statements continued..
1 Chapter 6: Semantic Analysis. 2 Semantic Analyzer ==> Semantic Structure - What is the program supposed to do? - Semantics analysis can be done during.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
Road Map Regular Exprs, Context-Free Grammars Regular Exprs, Context-Free Grammars LR parsing algorithm LR parsing algorithm Building LR parse tables Building.
Translation Scheme for Addressing Array Elements
CSCE 531 Compiler Construction
Intermediate code generation Jakub Yaghob
Compiler Construction
Compiler Construction
Intermediate Code Generation
Subject Name:COMPILER DESIGN Subject Code:10CS63
Compiler Optimization and Code Generation
Intermediate Code Generation Part II
Three Address Code Generations for Boolean Functions
Three Address Code Generation Control Statement
Chapter 6 Intermediate-Code Generation
Intermediate Code Generation Part II
Three Address Code Generation - Control Statements
Intermediate Code Generation Part II
Intermediate code generation
Three-address code A more common representation is THREE-ADDRESS CODE . Three address code is close to assembly language, making machine code generation.
7.4 Boolean Expression and Control Flow Statements
Compiler Design 21. Intermediate Code Generation
Three Address Code Generation – Backpatching
THREE ADDRESS CODE GENERATION
Intermediate Code Generation Part II
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 Design 21. Intermediate Code Generation
Review: What is an activation record?
Code generation and data types
Presentation transcript:

Compiler Designs and Constructions Chapter 11: Intermediate Code Generation(part II) Compiler Designs and Constructions Chapter 11: Intermediate Code Generation(part II) Objectives: Dr. Mohsen Chitsaz Chapter 11: Intermediate Code Generation (part II) COSC 470

Method II Flow of Control Translation of Boolean Expression (Position Reached) True (Exit) False (Exit) Chapter 11: Intermediate Code Generation (part II)

Method II Flow of Control Example: IF (a<b OR c>d) THEN N = M+K; IF a<b GOTO 4 IF c>d GOTO 4 GOTO 6 T1 = M + K N = T1 True 4 False 6 Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Address of Jump? Functions: Backpatch (P, I) Quadruple P takes I as a target Makelist (i) Create a new list with element I, (an index into the array of quadruples) Makelist returns a pointer Merge (P1, P2) Concatenate pointer P1 and P2 into one list, returns a pointer to the new list Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Example WHILE (a=b AND a=c) DO { N = a+b; c = N; } IF a=b GOTO __ GOTO __ IF a=c GOTO __ T1 = a+b N = T1 C=N Marker1 (True)=1-> 3 Marker2 (True)=3-> 5 Marker3 (False)=2-> 9 Marker4 (False)=4-> 9 How do I know the address? Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) E. True Target Location (Jump) (MARKER) E.False E  E1 AND E2 To find the jump address, we need to check E2 Modify the Grammar: E  E1 OR M E2 E  E1 AND M E2 E  Not E1 E  (E1) E  id E  id1 Relop id2 M  Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) M.Quad = NextQuad E  id1 Relap id2 E.True = Makelist (NextQuad) E.False = Makelist (NextQuad+1) Gen (IF id1.Place Relap id2.Place GOTO ) Gen (GOTO ) E  id Gen (IF id1.Place GOTO ) Gen (GOTO ) Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) E  (E1) E.True = E1.True E.False = E1.False E  Not E1 E.True = E1.False E.False=E1.True E  E1 AND M E2 Backpatch (E1.True, M.Quad) E.True := E2.True E.False := Merge (E1.False, E2.False) Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) E  E1 OR M E2 Backpatch (E1.False, M.Quad) E.True := Merge (E1.True, E2.True) E.False := E2.False Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Practice: 1- Build the Parse Tree 2- Add attributes a<b OR c<d AND e>f Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) 0. IF a<b GOTO___ True GOTO 2 IF c<d GOTO 4 GOTO___ False IF c>f GOTO ___ True GOTO ___ False E  E1 AND M E Backpatch(2,4) E  E1 OR M E2 Backpatch (1,2) Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Flow of Control Statement  IF (exp) Statement ELSE Statement  WHILE (exp) Statement  {State_list} Statement  Assignment_Statement State_list  State_list; Statement State_list  Statement Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Flow of Control What should we do with nested WHILE loop? WHILE { WHILE . { . . . } } Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Method 1: Using Stack Method 2: Using Markers Statement  WHILE M1(exp) M2 Statement M Example: WHILE (a>b) WHILE (c>d) .. END Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Method 1 0. IF a> b GOTO 2 GOTO ___ 2. IF c<d GOTO 4 GOTO __ GOTO 2 False2: GOTO 0 False1: Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Method 2 Statement  WHILE M1(exp) M2 Statement Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Grammar Statement  IF (exp) M1 Statement1 N ELSE M2 Statement2 Backpatch(E.True, M1.Quad) Backpatch (E.False, M2.Quad) Statement.Next: Merge (S1.Next, N.Next, S2.Next) { Next is the next statement to Jump} N  N.Next = Makelist (NextQuad) Gen (GOTO) Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Grammar M  M.Quad = NextQuad Statement  IF (exp) M Statement Backpatch (E.True, M.Quad); S.Next = Merge (E.False, S.Next) Statement  WHILE M1(exp) M2 Statement Gen (GOTO M1.Quad) Backpatch(S.Next, M1.Quad) Backpatch(E.True, M2.Quad) S.Next = E.False Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Grammar Statement  { State_list } S.Next = L.Next Statement  Assignment_Statement S.Next = Makelist (NextQuad); State_list  State_List1; M Statement Backpatch (L1.Next, M.Quad) L.Next := S.Next State_List  Statement Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Practice: 1- Build parse tree 2- Add attributes WHILE (a< b) IF (c > d) a = b + c; Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) WHILE (a< b) DO IF (c > d) THEN a = b + c; 0. IF a< b GOTO 2 GOTO 7 IF c>d GOTO 4 GOTO 0 T1 = b+c a = T1 GOTO Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Static Array List: Array [1..20, 1..30] of datatype; How much space do we need? How do we store this array? Row Major Column Major Where is List [20,30] started? List [I,j] is stored at = +((I-1) * 30) * bpw + (j-1) * bpw = +30i + j – 31 (bpw)? Base Register –31  Compiled Time (Fixed) Index Register 30i+j  Calculated Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Example B = List [I,j] 0- T1 = i+*30 1- T1 = T1 + j  Index Register 2- T2 = -31 * 4  Assume bpw = 4 3- T3 = 4* T1 4- T4 = T2 [T3] 5- B = T4 Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Array Code for Array []= A i T1 T1 = A[i] = B T1 T1 = B For K dimensional array: (Row Major) A [i1,i2,…ik] =(i1-1) d2,d3…dk+(i2-1)d3,d4…dk + …+(ik-1-1)dk+(ik-1) = i1d2d3…dk+i2d3d4…dk+…+dk+1 =(d2d3…dk+d3d4…dk+…+dk+1) Fixed = D If Address of A is THEN D is computed at Compile Time Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Array A L=E E  E+E E  (E) E  L E  elist ] L  id elist  elist, E elist  id [ E Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Array elist  id [ E elist.Place = E.Place elist.Ndim = 1; elist.Array = id.Place elist  elist1, E T=NewTemp(); M=elist.Ndim + 1; Gen (t ‘=‘ elist1.Place ‘*’ Limit (elist1.Array, M)) Gen (t ‘=‘ t ‘+’ E.Place); elist.Array = elist1.Array elist.Place = t elist.Ndim = M Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Array L  id L.Place = id.Place L.offset = Null L  elist ] L.Place = NewTemp(); L.offset = NewTemp(); Gen (L.offset ‘=‘ ‘bpw’ ‘*’ elist.Place) E  L IF (L.offset = Null) E.Place = L.Place ELSE { T = NewTemp(); Gen (T ‘=‘ L.Place ‘[‘ L.offset ‘]’); E.Place = T } Chapter 11: Intermediate Code Generation (part II)

Chapter 11: Intermediate Code Generation (part II) Array E  (E1) E.Place = E1.Place E  E1 + E2 T = NewTemp() Gen (T ‘=‘ E1.Place + E2.Place); E.Place = T A  L = E IF L.offset = Null Gen(L.Place ‘=‘ E.Place) ELSE Gen (L.Place ‘[‘ L.offset ‘]’ := E.Place) Chapter 11: Intermediate Code Generation (part II)