Backpatching Lecture 24 Fri, Apr 16, 2004. Linked Lists in Java The Java Platform includes a LinkedList class. Unfortunately, it was introduced in Java.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Chapter 6 Intermediate Code Generation
Decision Structures – The Syntax Tree Lecture 22 Fri, Apr 8, 2005.
Intermediate Code Generation
Backpatching: The syntax directed definition we discussed before can be implemented in two or more passes (we have both synthesized attributes and inheritent.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
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,
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
Generation of Intermediate Code Compiler Design Lecture (03/30//98) Computer Science Rensselaer Polytechnic.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Exercise 1: Balanced Parentheses Show that the following balanced parentheses grammar is ambiguous (by finding two parse trees for some input sequence)
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.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
The switch statement Week 5. The switch statement Java Method Coding CONCEPTS COVERED THIS WEEK.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
1/20 Symbolic Execution and Program Testing Charngki PSWLAB Symbolic Execution and Program Testing James C.King IBM Thomas J.Watson Research Center.
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. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Decision Structures – Code Generation Lecture 24 Mon, Apr 18, 2005.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
Code Generation CPSC 388 Ellen Walker Hiram College.
Intermediate code generation Simplifying initial assumptions a 3-address code will be used for instructions  choice of instruction mnemonics and layouts.
Code Generation How to produce intermediate or target code.
Sum of Arithmetic Sequences. Definitions Sequence Series.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
1 February 28, February 28, 2016February 28, 2016February 28, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Improvements to the Compiler Lecture 27 Mon, Apr 26, 2004.
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
Objects, If, Routines. Creating Objects an object is a fundamental software element that has a state and a set of behaviors. Ex: bank account or student.
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Unit-1 Introduction to Java
Intermediate code generation Jakub Yaghob
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Procedures: Building the Syntax Tree
Recursion.
Professor Jodi Neely-Ritz CGS3460
Procedures – Building the Syntax Tree
IF Statements.
CS 1111 Introduction to Programming Spring 2019
Three Address Code Generation – Backpatching
When a function is called...
Program Flow.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Backpatching Lecture 23 Wed, Apr 13, 2005.
Boolean Expressions Lecture 25 Fri, Apr 22, 2005.
Compiler Construction
Language semantics Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Code generation and data types
Lecture 20 – Practice Exercises 4
Presentation transcript:

Backpatching Lecture 24 Fri, Apr 16, 2004

Linked Lists in Java The Java Platform includes a LinkedList class. Unfortunately, it was introduced in Java 1.2 and we are using Java To have access to the LinkedList class, you will have to download Java from Sun Microsystems. Go to

The “next” Destination of a Statement Every statement will be represented as a LinkedList object in the grammar. The linked list will be a list of all backpatch labels occurring within the statement that must be resolved to the “next” destination of that statement. In the grammar, we must write nonterminal LinkedList stmts, stmt, n;

Labels and Jumps in the Grammar The productions that will involve jumps are stmts  stmts m stmt stmt  IF ( cexpr ) m stmt stmt  IF ( cexpr ) m stmt n ELSE m stmt func  fbeg stmts m RBRACE Remember, m represents a destination. n represents a jump.

Sequences of Statements Consider the first production. stmts  stmts 1 m stmt Every statement has a “next” destination. Normally, but not always, this is the next statement. This production will insert labels between consecutive statements, even though most of them will not be used.

Sequences of Statments The label at m serves as the “next” destination of stmts 1.

Sequences of Statments The “next” destination from stmt becomes the “next” destination from stmts (a synthesized attribute). stmts  stmts 1 m stmt

Sequences of Statments The “next” destination from stmt becomes the “next” destination from stmts (a synthesized attribute). stmts  stmts 1 m stmt next

Sequences of Statments The “next” destination from stmt becomes the “next” destination from stmts (a synthesized attribute). stmts  stmts 1 m stmt next

Action We must take the following actions. Backpatch stmts 1.nextList to m. Return stmt.nextList (to be the nextList of stmts). This resolves the “next” destination from stmts 1 and leaves the “next” destination from stmt unresolved.

The One-Way if Statement Now consider the one-way if statement. stmt  IF ( cexpr ) m stmt 1 The “true” list of the backpatch node associated with cexpr will be resolved to m. The “false” list of cexpr will later be resolved to the label following stmt 1. stmt  IF ( cexpr ) m stmt 1

The One-Way if Statement Now consider the one-way if statement. stmt  IF ( cexpr ) m stmt 1 The “true” list of the backpatch node associated with cexpr will be resolved to m. The “false” list of cexpr will later be resolved to the label following stmt 1. stmt  IF ( cexpr ) m stmt 1 T

The One-Way if Statement Now consider the one-way if statement. stmt  IF ( cexpr ) m stmt 1 The “true” list of the backpatch node associated with cexpr will be resolved to m. The “false” list of cexpr will later be resolved to the label following stmt 1. stmt  IF ( cexpr ) m stmt 1 T F

The One-Way if Statement Now consider the one-way if statement. stmt  IF ( cexpr ) m stmt 1 The “true” list of the backpatch node associated with cexpr will be resolved to m. The “false” list of cexpr will later be resolved to the label following stmt 1. stmt  IF ( cexpr ) m stmt 1 T F next

Action We must take the following actions. Backpatch cexpr.trueList to m. Merge cexpr.falseList and stmt 1.nextList. Return the merged list (to be the nextList of stmt).

Example Consider the following segment of code. if (a) b = 900; a = 200;

Example JUMPT INT BLABEL blabel=3 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a" JUMP INT BLABEL blabel=4 LABEL label=5 ASSIGN INT NAME PTR|INT value="b" NUM INT value=900 EQU BLABEL blabel=3 LABEL label=5 LABEL label=6 ASSIGN INT NAME PTR|INT value="a" NUM INT value=200 EQU BLABEL blabel=4 LABEL label=6

Example JUMPT INT BLABEL blabel=3 = 5 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a" JUMP INT BLABEL blabel=4 LABEL label=5 ASSIGN INT NAME PTR|INT value="b" NUM INT value=900 EQU BLABEL blabel=3 LABEL label=5 LABEL label=6 ASSIGN INT NAME PTR|INT value="a" NUM INT value=200 EQU BLABEL blabel=4 LABEL label=6

Example JUMPT INT BLABEL blabel=3 = 5 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a" JUMP INT BLABEL blabel=4 LABEL label=5 ASSIGN INT NAME PTR|INT value="b" NUM INT value=900 EQU BLABEL blabel=3 LABEL label=5 LABEL label=6 ASSIGN INT NAME PTR|INT value="a" NUM INT value=200 EQU BLABEL blabel=4 LABEL label=6

Example JUMPT INT BLABEL blabel=3 = 5 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a" JUMP INT BLABEL blabel=4 = 6 LABEL label=5 ASSIGN INT NAME PTR|INT value="b" NUM INT value=900 EQU BLABEL blabel=3 LABEL label=5 LABEL label=6 ASSIGN INT NAME PTR|INT value="a" NUM INT value=200 EQU BLABEL blabel=4 LABEL label=6

Example JUMPT INT BLABEL blabel=3 = 5 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a" JUMP INT BLABEL blabel=4 = 6 LABEL label=5 ASSIGN INT NAME PTR|INT value="b" NUM INT value=900 EQU BLABEL blabel=3 LABEL label=5 LABEL label=6 ASSIGN INT NAME PTR|INT value="a" NUM INT value=200 EQU BLABEL blabel=4 LABEL label=6

Two-Way if Statements The two-way if production is more interesting. stmt  IF ( cexpr ) m 1 stmt 1 n ELSE m 2 stmt 2 The “true” destination from cexpr is m 1 and the “false” destination is m 2. The “next” destination of stmt 1 is the same as the “next destination of n. n represents a jump to the “next” destination of stmt 2. The “next” destination of stmt 2 becomes the “next” destination of stmt.

Two-Way if Statements Consider the two-way if statement. stmt  IF ( cexpr ) m 1 stmt 1 n ELSE m 2 stmt 2

Two-Way if Statements If cexpr is true, then execution jumps to the label m 1. stmt  IF ( cexpr ) m 1 stmt 1 n ELSE m 2 stmt 2 T

Two-Way if Statements If cexpr is false, then execution jumps to the label m 2. stmt  IF ( cexpr ) m 1 stmt 1 n ELSE m 2 stmt 2 T F

Two-Way if Statements When stmt 1 is completed, execution jumps to the statement following the if statement, i.e., it jumps to stmt.nextList. stmt  IF ( cexpr ) m 1 stmt 1 n ELSE m 2 stmt 2 T F next

Two-Way if Statements If execution reaches n, then it jumps to stmt.nextList. stmt  IF ( cexpr ) m 1 stmt 1 n ELSE m 2 stmt 2 T F next

Two-Way if Statements When stmt 2 is completed, execution jumps to stmt.nextList. stmt  IF ( cexpr ) m 1 stmt 1 n ELSE m 2 stmt 2 T F next

Action We must take the following actions. Backpatch cexpr.trueList to m 1. Backpatch cexpr.falseList to m 2. Merge stmt 1.nextList, n.nextList, and stmt 2.nextList.

Example Consider the following segment of code. if (a) b = 900; else b = 500; a = 200;

Example JUMPT INT BLABEL blabel=3 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a" JUMP INT BLABEL blabel=4 LABEL label=5 ASSIGN INT NAME PTR|INT value="b" NUM INT value=900 JUMP INT BLABEL blabel=6 LABEL label=7 ASSIGN INT NAME PTR|INT value="b" NUM INT value=500 EQU BLABEL blabel=3 LABEL label=5 EQU BLABEL blabel=4 LABEL label=7 LABEL label=8 ASSIGN INT NAME PTR|INT value="a" NUM INT value=200 EQU BLABEL blabel=6 LABEL label=8

Example JUMPT INT BLABEL blabel=3 = 5 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a" JUMP INT BLABEL blabel=4 LABEL label=5 ASSIGN INT NAME PTR|INT value="b" NUM INT value=900 JUMP INT BLABEL blabel=6 LABEL label=7 ASSIGN INT NAME PTR|INT value="b" NUM INT value=500 EQU BLABEL blabel=3 LABEL label=5 EQU BLABEL blabel=4 LABEL label=7 LABEL label=8 ASSIGN INT NAME PTR|INT value="a" NUM INT value=200 EQU BLABEL blabel=6 LABEL label=8

Example JUMPT INT BLABEL blabel=3 = 5 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a" JUMP INT BLABEL blabel=4 = 7 LABEL label=5 ASSIGN INT NAME PTR|INT value="b" NUM INT value=900 JUMP INT BLABEL blabel=6 LABEL label=7 ASSIGN INT NAME PTR|INT value="b" NUM INT value=500 EQU BLABEL blabel=3 LABEL label=5 EQU BLABEL blabel=4 LABEL label=7 LABEL label=8 ASSIGN INT NAME PTR|INT value="a" NUM INT value=200 EQU BLABEL blabel=6 LABEL label=8

Example JUMPT INT BLABEL blabel=3 = 5 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a" JUMP INT BLABEL blabel=4 = 7 LABEL label=5 ASSIGN INT NAME PTR|INT value="b" NUM INT value=900 JUMP INT BLABEL blabel=6 = 8 LABEL label=7 ASSIGN INT NAME PTR|INT value="b" NUM INT value=500 EQU BLABEL blabel=3 LABEL label=5 EQU BLABEL blabel=4 LABEL label=7 LABEL label=8 ASSIGN INT NAME PTR|INT value="a" NUM INT value=200 EQU BLABEL blabel=6 LABEL label=8

Backpatching at a Function End Consider the production func  fbeg stmts m RBRACE stmts is a linked list of backpatch labels that must be resolved. If we reach the end of the function without yet resolving them, then they must be resolved to the return that occurs at the end of the function.

Backpatching at a Function End When the code is generated for this production, it is important that the backpatching occur first, before the return.