Three Address Code Generation Backpatching-I Prepared By: Siddharth Tiwary 04CS3010.

Slides:



Advertisements
Similar presentations
Chapter 2-2 A Simple One-Pass Compiler
Advertisements

Chapter 6 Intermediate Code Generation
Decision Structures – The Syntax Tree Lecture 22 Fri, Apr 8, 2005.
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.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
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.
Compiler Designs and Constructions
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Lecture 09 – IR (Backpatching) Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6
Lecture 15 Control Flow Topics Review Positional Encoding of Booleans Short Circuit Evaluation Control Flow Statements Readings: 8.4, 8.6 March 13, 2006.
Intermediate Code Generation Professor Yihjia Tsai Tamkang University.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
1 Intermediate Code generation. 2 Intermediate Code Generation l Intermediate languages l Declarations l Expressions l Statements l Reference: »Chapter.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
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.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
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.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Chapter 8: Intermediate Code Generation
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
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.
Pascal Programming Pointers and Dynamic Variables.
Overview of Previous Lesson(s) Over View  In syntax-directed translation 1 st we construct a parse tree or a syntax tree then compute the values of.
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.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Code Generation How to produce intermediate or target code.
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.
Code Generation for Control Flow Mooly Sagiv html:// Chapter
Three Address Code Generation of Control Statements continued..
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Intermediate code generation Jakub Yaghob
Compiler Construction
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
Three-address code A more common representation is THREE-ADDRESS CODE . Three address code is close to assembly language, making machine code generation.
Compiler Design 21. Intermediate Code Generation
Three Address Code Generation – Backpatching
SYNTAX DIRECTED DEFINITION
Intermediate Code Generation Part II
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 Design 21. Intermediate Code Generation
Presentation transcript:

Three Address Code Generation Backpatching-I Prepared By: Siddharth Tiwary 04CS3010

For the examples of the previous lectures for implementing syntax-directed definitions, the easiest way is to use two passes. First syntax tree is constructed and is then traversed in depth-first order to compute the translations given in the definition. The main problem in generating three address codes in a single pass for Boolean expressions and flow of control statements is that we may not know the labels that control must go to at the time jump statements are generated.

This problem is solved by generating a series of branch statements with the targets of the jumps temporarily left unspecified. Each such statement will be put on a list of goto statements whose labels will be filled in when the proper label can be determined. This subsequent filling of addresses for the determined labels is called BACKPATCHING.

For implementing Backpatching,we generate quadruples into a quadruple array and Labels are indices to this array. To manipulate list if labels,we use three functions: makelist(i),merge(p 1, p 2 ) and backpatch(p,i).

makelist(i) : creates a new list containing only i, an index into the array of quadruples and returns pointer to the list it has made. merge(i,j) – concatenates the lists pointed to by i and j,and returns a pointer to the concatenated list. backpatch(p,i) – inserts i as the target label for each of the statements on the list pointed to by p.

Let’s now try to construct the translation scheme for Boolean expression. Lets the grammar be: E → E 1 or ME 2 E → E 1 and ME 2 E → not E 1 E → (E 1 ) E → id 1 relop id 2 E → false E → true M → ε

What we have done is inserting a marker non-terminal M into the grammar to cause a semantic action to pick up, at appropriate times the index of the next quadruple to be generated. This is done by the semantic action: { M.Quad = nextquad } for the rule M → ε

Two synthesized attributes truelist and falselist of non-terminal E are used to generate jumping code for Boolean expressions. E.truelist : Contains the list of all the jump statements left incomplete to be filled by the label for the start of the code for E=true. E.falselist : Contains the list of all the jump statements left incomplete to be filled by the label for the start of the code for E=false.

The variable nextquad holds the index of the next quadruple to follow. This value will be backpatched onto E 1.truelist in case of E → E 1 and ME 2 where it contains the address of the first statement of E 2.code. This value will be backpatched onto E 1.falselist in case of E → E 1 or ME 2 where it contains the address of the first statement of E 2.code.

We use the following semantic actions for the above grammar : 1) 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 2) 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)

3) E → not E 1 E.truelist = E 1.falselist E.falselist = E 1.truelist 4) E → (E 1 ) E.truelist = E 1.truelist E.falselist = E 1.falselist 5) E → id 1 relop id 2 E.truelist = makelist(nextquad) E.falselist = makelist(nextquad +1 ) emit(if id 1.place relop id 2.place goto __ ) emit(goto ___)

6) E → true E.truelist = makelist(nextquad) emit(goto ___) 7) E → false E.falselist = makelist(nextquad) emit(goto ___) 8) M → ε M.Quad = nextquad