Three Address Code Generation – Backpatching

Slides:



Advertisements
Similar presentations
Chapter 6 Intermediate Code Generation
Advertisements

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.
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.
Compiler Designs and Constructions
Three Address Code Generation Backpatching-I Prepared By: Siddharth Tiwary 04CS3010.
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.
1 Intermediate Code generation. 2 Intermediate Code Generation l Intermediate languages l Declarations l Expressions l Statements l Reference: »Chapter.
Basic Building Blocks of Programming. Variables and Assignment Think of a variable as an empty container Assignment symbol (=) means putting a value into.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
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.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Backpatching Lecture 24 Fri, Apr 16, Linked Lists in Java The Java Platform includes a LinkedList class. Unfortunately, it was introduced in Java.
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.
Boolean expressions 1 productionsemantic action E  E1 or E2E1.trueLabel = E.trueLabel; E1.falseLabel = freshLabel(); E2.trueLabel = E.trueLabel; E2.falseLabel.
Chap. 4, Intermediate Code Generation
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..
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
Chapter 6: Loops.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Loops in Java.
Welcome to Computer Science Jeopardy
Intermediate code generation Jakub Yaghob
Compiler Construction
Intermediate Code Generation
Subject Name:COMPILER DESIGN Subject Code:10CS63
Compiler Optimization and Code Generation
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Flow of Control.
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
Flow of Control.
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
Computer Science Core Concepts
THREE ADDRESS CODE GENERATION
CS2011 Introduction to Programming I Selections (I)
Intermediate Code Generation Part II
Backpatching Lecture 23 Wed, Apr 13, 2005.
Intermediate Code Generation
Compiler Construction
Introduction to Programming
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
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
CSC1401 Manipulating Pictures 2
Compiler Design 21. Intermediate Code Generation
Review: What is an activation record?
Intermediate Code Generating machine-independent intermediate form.
Presentation transcript:

Three Address Code Generation – Backpatching Date : 9/11/06 By: NEERAJ KUMAR (04CS3014) Three Address Code Generation – Backpatching The problem in generating three address codes in a single pass is that we may not know the labels that control must go to at the time jump statements are generated. So to get around this problem a series of branching statements with the targets of the jumps temporarily left unspecified is generated. BackPatching is putting the address instead of labels when the proper label is determined. Backpatching Algorithms perform three types of operations 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. The Translation scheme is as follows :- 1) E ---> E1 or M E2 backpatch(E1.falselist, M.quad) E.truelist = merge(E1.truelist, E2.truelist) E.falselist = E2.falselist 2) E ---> E1 and M E2 backpatch(E1.truelist, M.quad) E.truelist = E2.truelist E.falselist = merge(E1.falselist, E2.falselist) 3) E ----> not E1 E.truelist = E1.falselist E.falselist = E1.truelist 4) E -----> (E1) E.truelist = E1.truelist E.falselist = E1.falselist 5) E -----> id1 relop id2 E.truelist = makelist(nextquad) E.falselist = makelist(nextquad +1 ) emit(if id1.place relop id2.place goto __ ) emit(goto ___) 6) E -----> true 7) E -----> false E.falselist = makelist(nextquad) 8) M -----> epsilon M.Quad = nextquad   Most programming languages have a common set of statements that define the control flow of a program written in that language.These statements are : 1.Assignment : A single statement assigning some expression to a variable. After executing this statement, the control flows to the immmediate next statement in the sequence. 2.if<condition>then<then-part>else<else-part> : Control flows to either the then-part or the else-part depending on whether the condition is true or false. 3.while<condition>do <loop> : Control remains within the loop until the condition becomes false. 4.<block of statements> : a group of statements put within a begin ... end block marker. The grammar for these statements is given by : S -> if B then S | if B then S else S | while B do S | begin L end | A /*assignment*/ L -> L S | S To aid the generation of the code, two new marker nonterminals M and N are introduced. The modified grammar becomes : S -> if B then M S | if B then M S N else M S | while M B do M S L -> L M S M -> 'epsilon' N -> 'epsilon' B is a Boolean expression. Attributes are defined as follows: S.nextlist : List of quadruples with jumps to the quadruple succeeding S L.nextlist : The same, where L is a group of statements. The nonterminal N iss ued as it permits the generation of a jump after the 'then' in if-then-else. The atribute N.nextlist holds the quadruple number for this statement. Actions : S -> if B then M S1 { backpatch (B.truelist,M.quad) S.nextlist = mergelist(B.falselist,S1.nextlist) } S -> if B then M1S1N else M2S2 backpatch (B.truelist,M1.quad) backpatch (B.falselist,M2.quad) S.nextlist = mergelist(S1 .nextlist,mergelist(N.nextlist,S2.nextlist)) S -> while M1B do M2S1 backpatch(S1.nextlist,M1.quad) backpatch(B.truelist,M2.quad) S.nextlist = B.falselist emit('goto' M1.quad) S -> begin L end S.nextlist = L.nextlist S -> A S.nextlist = <nil> L -> L1 M S backpatch(L1.nextlist,M.quad) L.nextlist = S.nextlist L -> S code translation: begin while a > b do x = y + z a = a - b end x = y - z becomes 1. if a>b goto 3 2. goto 8 3. t1 = y + z 4. x = t1 5. t2 = a - b 6. a = t2 7. goto 1 8. t3 = y - z 9. x = t3