Generation of Intermediate Code 66.648 Compiler Design Lecture (03/30//98) Computer Science Rensselaer Polytechnic.

Slides:



Advertisements
Similar presentations
Chapter 6 Intermediate Code Generation
Advertisements

Decision Structures – The Syntax Tree Lecture 22 Fri, Apr 8, 2005.
ANALYSIS OF PROG. LANG. PROGRAM ANALYSIS Instructors: Crista Lopes Copyright © Instructors. 1.
Intermediate Code Generation
Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
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.
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.
Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.
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.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
Compiler design Computer Science Rensselaer Polytechnic Lecture 1.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
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.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow.
Attribute Grammar Examples and Symbol Tables Compiler Design Lecture (02/23/98) Computer Science Rensselaer Polytechnic.
Compiler Chapter# 5 Intermediate code generation.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
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.
Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY Week 7 and 8(10.14 and 10.21): The lexical of TINY + Implement.
Winter Compilers Software Eng. Dept. – Ort Braude Compiling Assignments and Expressions Lecturer: Esti Stein brd4.ort.org.il/~esti2.
Lexical Analysis - Scanner- Contd Computer Science Rensselaer Polytechnic Compiler Design Lecture 3(01/21/98)
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
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.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
An Attribute Grammar for Tiny Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 18.
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.
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..
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Road Map Regular Exprs, Context-Free Grammars Regular Exprs, Context-Free Grammars LR parsing algorithm LR parsing algorithm Building LR parse tables Building.
A Simple Syntax-Directed Translator
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Intermediate code generation Jakub Yaghob
Intermediate Code Generation
Subject Name:COMPILER DESIGN Subject Code:10CS63
Syntax Analysis - LR(1) and LALR(1) Parsing
Intermediate Code Generation Part II
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
Compiler Design 21. Intermediate Code Generation
Three Address Code Generation – Backpatching
Intermediate Code Generation Part II
Backpatching Lecture 23 Wed, Apr 13, 2005.
Boolean Expressions Lecture 25 Fri, Apr 22, 2005.
Intermediate Code Generation
Compiler Construction
Intermediate Code Generation Part II
Compiler Construction
Compiler Design 21. Intermediate Code Generation
Presentation transcript:

Generation of Intermediate Code Compiler Design Lecture (03/30//98) Computer Science Rensselaer Polytechnic

Lecture Outline Intermediate Codes for if Intermediate Codes for if Backpatching Backpatching Examples Examples Administration Administration

Short-circuit code approach Idea: use conditional jump instruction after evaluating partial expression. Example: a and b and c if a were false, then we need not evaluate the rest of the expressions. So, we insert labels E.true and E.false in the appropriate places.

Short Circuit - Contd If a goto E.true goto E.false E.true: if b goto E1.true goto E.false E1.true: if c goto E2.true goto E.false E2.true : exp =1 E.false: exp =0

Syntax Directed Translation The translation uses the following attributes: true, false, and code. True and false are inherited attributes whereas code is a synthesized attribute. The inherited attribute true and false specify the true and false branch destinations for the short circuit code generated by sub- expressions. Caution: The true and false inherited attributes cannot be evaluated on the fly using bottom-up parsing. One needs to build a parse tree and the dependency graph for evaluating the attributes.

E1 --> E2 or T { E2.true =E1.true; T.true=E1.true; E2.false=newlabel(); T.false=E1.false; E1.code || gen(“label”E2.false,”:”) || T.code } E1--> T { T.true=E1.true;T.false=E.false;E.code=T.code} T1--> T2 and F { T2.true=newlabel();F.true=T1.true; T2.false=T1.false;F.false=T1.false;T1.code=T2.c ode||gen(“label”,T2.true,”.”)||F.code} T-->F {F.true=T.true;F.false=T.false;T.code=F.code} T-->F {F.true=T.true;F.false=T.false;T.code=F.code} Syntax-Directed Translation

F--> NOT F1 {F1.false=F.true;F1.true=F.false; F.code=F1.code} F--> (E) { E.true=F.true;E.false=F.false; F.code=E.code} F--> ID1 RELOP ID2 {F.code=gen(“if”ID1.place RELOP ID2.place “goto “ F.true|| gen(“goto” F.false} F--> True { F.code=gen(“goto”F.true)} F--> false {F.code=gen(“goto”F.false)} Syntax-Directed Translation

Switch/Case statement in source language form: switch(expr) { case_value1: Stmt1..Case_valuen-1:Stmt(n-1) default: Stmtn } Generating Code for Switch Stmts

Translation into 3 A C Translation of source switch/case statement into intermediate language 3 A C. Switchstmt.code= code to evaluate expression into temporary t goto test L1: code for Stmt1 goto next goto next L2: code for Stmt2

Switch Stmt Goto next L(n-1): code for Stmt(n-1) go to next L(n): code for Stmt(n) goto next test: if t=V1 goto L1 if (t=V2) goto L2 if (t=V2) goto L2 … if (t=V(n-1)) goto L(n-1) else goto L(n)

Switch Statement It becomes the responsibility of the code generation phase to generate an efficient implementation of a n-way branch. Common ideas: use a hash table, balance tree or a combination of the above.

Back patching Back patching is a technique to solve the problem of replacing symbolic names in goto statements by the actual target addresses. This problem comes up because of some languages do not allow symbolic names in the branches. Idea: Maintain a list of branches that have the same target label and replace them once they are defined.

Example Source: if a or b then if a or b then if c then if c then x= y+1 x= y+1Translation: if a go to L1 if a go to L1 if b go to L1 if b go to L1 go to L3 go to L3

Example-Contd L1: if c goto L2 goto L3 goto L3 L2: x= y+1 L3: After Backpatching: 100: if a goto : if b goto 103 goto 106

Back patching List operations: merge(p1,p2) - merges two lists pointed by p1 and p2 back patch(p,j) inserts the target label j for each list pointed by p. Attributes are: addr (address of the marker symbol), nextlist (list of jumps whose addresses have to be filled by address that follows the current stmt), truelist and falselist

Back patching- Syntax Directed Translation We assume that the function next_address() returns the address to be occupied by the next intermediate language statement that is generated. Stmts--> stmt { stmts.nextlist=stmt.nextlist;} stmts1-->stmts2 ‘;’ marker stmt { backpatch ( stmts2. extlist,marker.addr); stmts1.nextlist = stmt.nextlist} marker --> epsilon {marker.address = nextaddress() }

Back patching- Syntax Directed Translation Stmt1 --> IF expr THEN marker stmt2 { backpatch(expr.truelist,marker.addr); stmt1.nextlist=merge(stmt2.nextlist,expr.falselis t)} Stmt1 --> While marker expr DO marker1 stmt2 { backpatch(stmt2.nextlist,marker.addr);backpatc h(expr.truelist,marker1.addr); stmt1.nextlist=expr.falselist} stmt: ID = expr { stmt.nextlist=nil}

Examples If a or b then if c then x=y+1 100:if a goto ____ 101:if b goto ____ 102: goto ____ 103:if c goto ____ 104goto ____

3 AC Statements

Comments and Feedback Projects 3 and 4 are out. Please start working. PLEASE do not wait for the due date to come. We are in chapter 8. Please read that chapter and read the relevant portion of Java. Please keep studying this material and work exercises.