Chapter 5 Intermediate Code Generation. Chapter 5 -- Intermediate Code Generation2  Let us see where we are now.  We have tokenized the program and.

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

Intermediate Code Generation
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
1 Compiler Construction Intermediate Code Generation.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
CPSC Compiler Tutorial 9 Review of Compiler.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Chapter 2 A Simple Compiler
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
Semantic Analysis Legality checks –Check that program obey all rules of the language that are not described by a context-free grammar Disambiguation –Name.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Introduction to code generation.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Review Joey Paquet,
Compiler Chapter# 5 Intermediate code generation.
Syntax Directed Translation. Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC  assembly code/mc Cmm subexpression,……
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Overview of Previous Lesson(s) Over View  An ambiguous grammar which fails to be LR and thus is not in any of the classes of grammars i.e SLR, LALR.
# 1 CMPS 450 Syntax-Directed Translations CMPS 450 J. Moloney.
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
Joey Paquet, 2000, Lecture 10 Introduction to Code Generation and Intermediate Representations.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Introduction to Code Generation and Intermediate Representations
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
Introduction to Compiling
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
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.
LESSON 04.
Overview of Previous Lesson(s) Over View  Syntax-directed translation is done by attaching rules or program fragments to productions in a grammar. 
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
1 Structure of a Compiler Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
Review 1.Structure of the course Lexical Analysis Syntax Analysis Grammar & Language RG & DFA Top-down LL(1) Parsing Bottom-Up LR Layered Automation Semantic.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
BNF A CFL Metalanguage Some Variations Particular View to SLK Copyright © 2015 – Curt Hill.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Lecture 9 Symbol Table and Attributed Grammars
A Simple Syntax-Directed Translator
Constructing Precedence Table
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 5 Syntax Directed Translation
Ch. 4 – Semantic Analysis Errors can arise in syntax, static semantics, dynamic semantics Some PL features are impossible or infeasible to specify in grammar.
CS 3304 Comparative Languages
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Chapter 6 Intermediate-Code Generation
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler design.
SYNTAX DIRECTED DEFINITION
Chapter 5 Syntax Directed Translation
CMPE 152: Compiler Design September 17 Class Meeting
Compiler design Review COMP 442/6421 – Compiler Design
Presentation transcript:

Chapter 5 Intermediate Code Generation

Chapter 5 -- Intermediate Code Generation2  Let us see where we are now.  We have tokenized the program and parsed it.  We know the structure of the program and of every statement in it,  and we have presumably established that it is free of grammatical errors.  It would appear that we are ready to start translating it.

Chapter 5 -- Intermediate Code Generation3 1. Semantic Actions and Syntax- Directed Translation  We can attach a meaning to every production  Because the sequence of productions guides the generation of intermediate code, we call this process Syntax-Directed Translation.  Def: The computations or other operations attached to the productions impute meaning to each production, and so these operations are called Semantic Actions

Chapter 5 -- Intermediate Code Generation4  Def: The information obtained by the semantic actions is associated with the symbols of the grammar; it is normally put in fields of records associated with the symbols; these fields are called attributes  Note: as far as the parser is concerned, neither the semantic actions nor the attributes are a part of the grammar; they are only used as a device for bridging the gap between parsing and constructing an intermediate representation.

Chapter 5 -- Intermediate Code Generation5  Things we must take care of with the semantic actions:  making sure the variables are declared before use.  type checking  making sure actual and formal parameters are matched  These things are called semantic analysis  So we can now have it both ways, we can Put context dependent information and actions together into a language that is still context free.

Chapter 5 -- Intermediate Code Generation6 2. Intermediate Representations  We will look at several different representations  Syntax Trees  Directed Acyclic Graphs  Postfix notation  Three-Address Code  Other Forms.

Chapter 5 -- Intermediate Code Generation7 2.1 Syntax Trees  typically used when intermediate code is to be generated later (maybe after an optimization pass)

Chapter 5 -- Intermediate Code Generation8  Statement: x = a * b + a * b  Parse Tree

Chapter 5 -- Intermediate Code Generation9  Syntax Tree

Chapter 5 -- Intermediate Code Generation10  In a Parse Tree the emphasis is on the grammatical structure of the statement.  In a Syntax Tree the emphasis is on the actual computation to be performed.

Chapter 5 -- Intermediate Code Generation Directed Acyclic Graphs  The directed acyclic graph (DAG) is a relative of a Syntax Tree.  The difference is that nodes for variables or repeated sub-expressions are merged.

Chapter 5 -- Intermediate Code Generation12  DAG -- Notice that this is not the same computations as in the previous examples  They had a typo -- (swapped the * and +)  But it still explains the concept

Chapter 5 -- Intermediate Code Generation13  The use of DAG's to eliminate redundant code is our first instance of optimization. We will see more optimization in Chapter 6.  Redundant code really comes into play when we do array subscripts.  When you start generating intermediate code, you will be amazed at how much is generated for array subscripts.

Chapter 5 -- Intermediate Code Generation Postfix Notation  Very easy to generate from a Bottom-Up parse.  You can also generate it from a Syntax Tree via a postorder traversal.  The chief virtue of postsfix is that it can be evaluated with the use of a stack.  Nested if statements can cause problems.

Chapter 5 -- Intermediate Code Generation Three-Address Code  This form breaks the program down into elementary statements having no more than 3 variables and no more than one operator.  Sample Statement: x = a + b * b  Translation:  T := b * b  x := a + T  Note: T is a temporary variable.

Chapter 5 -- Intermediate Code Generation16  The notation is a compromise; it has the general form of a high-level language, but the individual statements are simple enough that they map into assembly language in a reasonably straight forward manner.  3AC may be:  Generated from a traversal of a Syntax Tree or a DAG.  or it may be generated as intermediate code directly in the course of the parse.

Chapter 5 -- Intermediate Code Generation Intermediate Languages  Sometimes the Intermediate representation may be a language of its own.  This helps uncouple the front end of the compiler from the back end.  You can then have a front end for each language that generate the same intermediate language, and then one back end for each type of computer.

Chapter 5 -- Intermediate Code Generation18  Examples:  UNCOL (1961) -- UNiversal Compiler- Oriented Language.  P-Code (1981) -- UCSD -- based upon a p-code interpreter (they also built p-code compilers.)  GNU Intermediate Code -- gcc, g++, g77, gada,  -- a Lispish type intermediate language.

Chapter 5 -- Intermediate Code Generation19 3. Bottom-Up Translation  Bottom-Up parsing generally lends itself to intermediate code generation more readily than does top-down parsing.  In either case, we must keep track of the various elements or pieces of the intermediate representation we are using, so we can get at them when we need them.

Chapter 5 -- Intermediate Code Generation20  These elements will be attributes of symbols in the grammar.  For an identifier, the attribute will usually be its address in the symbol table.  For a non-terminal, the attribute will be some appropriate reference to part of the intermediate representation.

Chapter 5 -- Intermediate Code Generation21  The most convenient way to keep track of these attributes is by keeping them in a stack (known as the semantic stack).  In the case of bottom-up parsing, the semantic stack and the parser stack move in synchronism.  When we pop from the parse stack we pop the semantic stack, and when we push something onto the parse stack we will push something onto the semantic stack

Chapter 5 -- Intermediate Code Generation Trees  Syntax Trees  The semantic action associated with each production will include planting a tree and taking care of the grafts.  The attribute will contain a pointer to the root of the tree for this expression.  You will need functions like make_tree( ) and make_leaf( )

Chapter 5 -- Intermediate Code Generation23  DAGS  The main difference between constructing a DAG and constructing a Syntax Tree is that we do not create redundant nodes in a DAG.  That means that the functions to create trees and grafts must be modified to check for duplicates.  This is typically done with a hash function.

Chapter 5 -- Intermediate Code Generation Postfix Notation  Postfix notation is particularly easy to generate from a bottom-up parse.  Grammar:

Chapter 5 -- Intermediate Code Generation Three-Address Code  We can obtain 3AC from a tree or a DAG, or we can generate it directly in the course of a bottom-up parse.  To generate 3AC during the parse we need the following functions:  MakeQuad( ) -- puts its parameters into the listing file in the proper format.  GetTemp( ) -- Generates a new temporary number.

Chapter 5 -- Intermediate Code Generation26 4. Top-Down Translation  It is, to be blunt, a mess  Two stacks, one for the parser, and one for the attributes. Thus the semantic stack must now be separate and will not move in synchronism with the parser stack

Chapter 5 -- Intermediate Code Generation27  Up to now, we picked up attributes from children, and the bottom-up parser provided a handy way to do this. Now we have to be able to transfer attributes between siblings, and in some cases it may be necessary to transfer them from parent to child.

Chapter 5 -- Intermediate Code Generation Synthesized and Inherited Attributes  synthesized -- from your children (like we have looked at).  inherited -- from your parent, or sibling  Def: A grammar in which all attributes are synthesized is called an S-attribute grammar

Chapter 5 -- Intermediate Code Generation29  Notice that inheritance so far is from left to right.  That may not always be the case, and that can really mess things up.  Def: Grammars in which no non-terminal ever inherits from a younger brother are called L- attributed grammars

Chapter 5 -- Intermediate Code Generation Attributes in a Top-Down Parse  Since so many of our problems arise from the need to eliminate left recursion in the underlying grammar, the place to start is to see how a normal semantic action has to be modified when removing left recursion.

Chapter 5 -- Intermediate Code Generation31  In this example,.s is used to denote a synthesized attribute, and.i an inherited one.

Chapter 5 -- Intermediate Code Generation32  An sample parse tree of a+b

Chapter 5 -- Intermediate Code Generation Removal of Left Recursion  When transforming a grammar to remove left recursions, we must also transform the semantic actions, as the example we just saw implies.  There are some basic rules on how to do this, but we will not cover them at this time.

Chapter 5 -- Intermediate Code Generation34 5. More about Bottom-Up Translation  We are now going to consider the cases where semantic actions must be embedded in the midst of the right-hand side of a production.  And cases where it is expedient to use inherited attributes.

Chapter 5 -- Intermediate Code Generation Embedded Semantic Actions  You will recall that in S-attributed grammars, the semantic actions occur in one piece at the end of the right-hand side of the production.  For some statements that is fine, but for others it is not.  The typical example is: if C then S  because based upon the value of C we need a conditional jump to the end of the statement S.

Chapter 5 -- Intermediate Code Generation36  There are a few ways around this:  Add Semantic actions in the middle of the production (YACC/BISON allow this)  Breaking Productions up  Adding Marker Non-Terminals  Another problem:  The limit a jump non zero can go (jnz limit). On the Intel architecture, this is 127 bytes.

Chapter 5 -- Intermediate Code Generation Inherited Attributes  Example: Fortran Variables  The data type comes first, so you can enter lexemes into the symbol table with the data type (an inherited attribute)

Chapter 5 -- Intermediate Code Generation38 6. Pascal-Type Declarations  Problem:  The data type comes last, so you can’t add the variables into the Symbol table as they come.  Solution:  Save the identifiers (or their symbol table pointer) onto some stack/list  When the data types come around, update the list elements.

Chapter 5 -- Intermediate Code Generation39 7. Type Checking and Coercion  integer operation / float operation  is it integer_add, float_add,...  The problem is that this is an overloaded operator  It is up to the Semantic Analyzer  to determine which operation is desired and to choose the appropriate implementation.  And if the user has specified two incompatible operands, it must take some action.

Chapter 5 -- Intermediate Code Generation40

Chapter 5 -- Intermediate Code Generation41 8. Summary  We have seen some of the principal methods for intermediate code generation and some of the principal problems.  Now we have nearly reached the conclusion of the front end of the compiler.  Some front-ends also include some optimization, and some interpreters also stop here.