MIT 6.035 Intermediate Formats Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantic Analysis and Symbol Tables
Symbol Table.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Intermediate Code Generation
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
1 Compiler Construction Intermediate Code Generation.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Compilers and Language Translation
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
ISBN Chapter 10 Implementing Subprograms.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Run time vs. Compile time
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
Context-sensitive Analysis, II Ad-hoc syntax-directed translation, Symbol Tables, andTypes.
Compiler Summary Mooly Sagiv html://
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Semantic Analysis Legality checks –Check that program obey all rules of the language that are not described by a context-free grammar Disambiguation –Name.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
COMPILERS Semantic Analysis hussein suleman uct csc3005h 2006.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Compiler Construction
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Joey Paquet, 2000, Lecture 10 Introduction to Code Generation and Intermediate Representations.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Introduction to Code Generation and Intermediate Representations
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.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Chapter 1 Introduction Major Data Structures in Compiler
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
CS 153: Concepts of Compiler Design October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Lecture 9 Symbol Table and Attributed Grammars
Names and Attributes Names are a key programming language feature
A Simple Syntax-Directed Translator
Constructing Precedence Table
Compiler Construction (CS-636)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Semantic Analysis with Emphasis on Name Analysis
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
Chapter 6 Intermediate-Code Generation
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
UNIT V Run Time Environments.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Procedure Linkages Standard procedure linkage Procedure has
Presentation transcript:

MIT Intermediate Formats Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Program Representation Goals Enable Program Analysis and Transformation –Semantic Checks, Correctness Checks, Optimizations Structure Translation to Machine Code –Sequence of Steps Parse Tree High Level Intermediate Representation Low Level Intermediate Representation Machine Code Semantic Analysis

High Level IR Preserves Object Structure Preserves Structured Flow of Control Primary Goal: Analyze Program Low Level IR Moves Data Model to Flat Address Space Eliminates Structured Control Flow Suitable for Low Level Compilation Tasks –Register Allocation –Instruction Selection

Examples of Object Representation and Program Execution (This happens when program runs)

Example Vector Class class vector { int v[]; void add(int x) { int i; i = 0; while (i < v.length) { v[i] = v[i]+x; i = i+1; } }

Items Stored Contiguously In Memory Length Stored In First Word Color Code –Red - generated by compiler automatically –Blue, Yellow, Lavender - program data or code –Magenta - executing code or data Representing Arrays 3748

Representing Vector Objects First Word Points to Class Information –Method Table, Garbage Collector Data Next Words Have Object Fields –For vectors, Next Word is Reference to Array Class Info 3748

Invoking Vector Add Method vect.add(1); Create Activation Record 3748 Class Info

Invoking Vector Add Method vect.add(1); Create Activation Record –this onto stack this 3748 Class Info

Invoking Vector Add Method vect.add(1); Create Activation Record –this onto stack –parameters onto stack 1 x this 3748 Class Info

Invoking Vector Add Method vect.add(1); Create Activation Record –this onto stack –parameters onto stack –space for locals on stack 1 x i this 3748 Class Info

Executing Vector Add Method void add(int x) { int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; } 1 x i this 3748 Class Info

Executing Vector Add Method void add(int x) { int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; } 1 0 x i this 3748 Class Info

Executing Vector Add Method void add(int x) { int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; } 1 0 x i this 3748 Class Info

void add(int x) { int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; } 1 0 x i this 3748 Executing Vector Add Method Class Info

void add(int x) { int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; } 1 0 x i this 3748 Executing Vector Add Method Class Info

Executing Vector Add Method void add(int x) { int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; } 1 0 x i this 3748 Class Info

Executing Vector Add Method void add(int x) { int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; } 1 0 x i this 3748 Class Info

Executing Vector Add Method void add(int x) { int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; } 1 0 x i this 3748 Class Info

Executing Vector Add Method void add(int x) { int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; } 1 0 x i this 3748 Class Info

Executing Vector Add Method void add(int x) { int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; } 1 0 x i this 3748 Class Info

Executing Vector Add Method void add(int x) { int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; } 1 0 x i this 3848 Class Info

Executing Vector Add Method void add(int x) { int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; } 1 0 x i this 3848 Class Info

Executing Vector Add Method void add(int x) { int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; } 1 1 x i this 3848 Class Info

Executing Vector Add Method void add(int x) { int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; } 1 3 x i this 3859 Class Info

What does the compiler have to do to make all of this work?

Compilation Tasks Determine Format of Objects and Arrays Determine Format of Call Stack Generate Code to Read Values –this, parameters, locals, array elements, object fields Generate Code to Evaluate Expressions Generate Code to Write Values Generate Code for Control Constructs

Further Complication - Inheritance Object Extension

Inheritance Example - Point Class class point { int c; int getColor() { return(c); } int distance() { return(0); } }

Point Subclasses class cartesianPoint extends point{ int x, y; int distance() { return(x*x + y*y); } } class polarPoint extends point { int r, t; int distance() { return(r*r); } int angle() { return(t); } }

Implementing Object Fields Each object is a contiguous piece of memory Fields from inheritance hierarchy allocated sequentially in piece of memory Example: polarPoint object Class Info polarPoint c r t

Point Objects c c x y c r t Class Info point Class Info cartesianPoint Class Info polarPoint

Compilation Tasks Determine Object Format in Memory –Fields from Parent Classes –Fields from Current Class Generate Code for Methods –Field, Local Variable and Parameter Accesses –Method Invocations

Symbol Tables - Key Concept in Compilation Compiler Uses Symbol Tables to Produce –Object Layout in Memory –Code to Access Object Fields Access Local Variables Access Parameters Invoke Methods

Symbol Tables During Translation From Parse Tree to IR Symbol Tables Map Identifiers (strings) to Descriptors (information about identifiers) Basic Operation: Lookup –Given A String, find Descriptor –Typical Implementation: Hash Table Examples –Given a class name, find class descriptor –Given variable name, find descriptor local descriptor, parameter descriptor, field descriptor

Hierarchy In Symbol Tables Hierarchy Comes From –Nested Scopes - Local Scope Inside Field Scope –Inheritance - Child Class Inside Parent Class Symbol Table Hierarchy Reflects These Hierarchies Lookup Proceeds Up Hierarchy Until Descriptor is Found

Hierarchy in vector add Method i descriptor for local i x descriptor for parameter x v descriptor for field v Symbol Table for Fields of vector Class Symbol Table for Parameters of add Symbol Table for Locals of add this descriptor for this

Lookup In vector Example v[i] = v[i]+x; i descriptor for local i x descriptor for parameter x v descriptor for field v this descriptor for this

Lookup i In vector Example v[i] = v[i]+x; i descriptor for local i x descriptor for parameter x v descriptor for field v this descriptor for this

Lookup i In vector Example v[i] = v[i]+x; i descriptor for local i x descriptor for parameter x v descriptor for field v this descriptor for this

Lookup x In vector Example v[i] = v[i]+x; i descriptor for local i x descriptor for parameter x v descriptor for field v this descriptor for this

Lookup x In vector Example v[i] = v[i]+x; i descriptor for local i x descriptor for parameter x v descriptor for field v this descriptor for this

Lookup x In vector Example v[i] = v[i]+x; i descriptor for local i x descriptor for parameter x v descriptor for field v this descriptor for this

Descriptors What do descriptors contain? Information used for code generation and semantic analysis –local descriptors - name, type, stack offset –field descriptors - name, type, object offset –method descriptors signature (type of return value, receiver, and parameters) reference to local symbol table reference to code for method

Program Symbol Table Maps class names to class descriptors Typical Implementation: Hash Table vector point cartesianPoint polarPoint class descriptor for vector class descriptor for point class descriptor for cartesianPoint class descriptor for polarPoint

Class Descriptor Has Two Symbol Tables –Symbol Table for Methods Parent Symbol Table is Symbol Table for Methods of Parent Class –Symbol Table for Fields Parent Symbol Table is Symbol Table for Fields of Parent Class Reference to Descriptor of Parent Class

Class Descriptors for point and cartesianPoint y x field descriptor for x field descriptor for y distance method descriptor for distance c field descriptor for c getColor method descriptor for getColor class descriptor for point class descriptor for cartesianPoint method descriptor for distance distance

Field, Parameter and Local and Type Descriptors Field, Parameter and Local Descriptors Refer to Type Descriptors –Base type descriptor: int, boolean –Array type descriptor, which contains reference to type descriptor for array elements –Class descriptor Relatively Simple Type Descriptors Base Type Descriptors and Array Descriptors Stored in Type Symbol Table

Example Type Symbol Table boolean boolean descriptor int int descriptor int [] array descriptor boolean [] array descriptor vector [] array descriptor class descriptor for vector vector

Method Descriptors Contain Reference to Code for Method Contain Reference to Local Symbol Table for Local Variables of Method Parent Symbol Table of Local Symbol Table is Parameter Symbol Table for Parameters of Method

Method Descriptor for add Method this i this descriptor local descriptor field symbol table for vector class local variable symbol table x parameter symbol table parameter descriptor code for add method Method descriptor for add

Symbol Table Summary Program Symbol Table (Class Descriptors) Class Descriptors – Field Symbol Table (Field Descriptors) Field Symbol Table for SuperClass – Method Symbol Table (Method Descriptors) Method Symbol Table for Superclass Method Descriptors –Local Variable Symbol Table (Local Variable Descriptors) Parameter Symbol Table (Parameter Descriptors) –Field Symbol Table of Receiver Class Local, Parameter and Field Descriptors –Type Descriptors in Type Symbol Table or Class Descriptors

v field descriptor add this i this descriptor local descriptor x parameter descriptor code for add method boolean boolean descriptor int int descriptor int [] array descriptor boolean [] array descriptor vector [] array descriptor class_decl vectorfield_decl intv[] class descriptor for vector

v field descriptor add this i this descriptor local descriptor x parameter descriptor code for add method boolean boolean descriptor int int descriptor int [] array descriptor boolean [] array descriptor vector [] array descriptor class_decl vectorfield_decl intv[] class descriptor for vector method descriptor for add type symbol table local symbol table parameter symbol table field symbol table method symbol table

Translating from Abstract Syntax Trees to Symbol Tables

Example Abstract Syntax Tree class_decl vectorfield_decl intv method_decl add intx param_declvar_decl inti statements class vector { int v[]; void add(int x) { int i; i = 0; while (i < v.length) { v[i] = v[i]+x; i = i+1; } }

class_decl vectorfield_decl intv method_decl add intx param_declvar_decl inti statements class symbol table

class descriptor for vector class_decl vectorfield_decl intv method_decl add intx param_declvar_decl inti statements class symbol table vector

v field descriptor class descriptor for vector class_decl vectorfield_decl intv method_decl add intx param_declvar_decl inti statements class symbol table vector

v field descriptor add class descriptor for vector this this descriptor Method descriptor for add class_decl vectorfield_decl intv method_decl add intx param_declvar_decl inti statements class symbol table vector

v field descriptor add class descriptor for vector this this descriptor x parameter descriptor Method descriptor for add class_decl vectorfield_decl intv method_decl add intx param_declvar_decl inti statements class symbol table vector

v field descriptor add class descriptor for vector this i this descriptor local descriptor x parameter descriptor Method descriptor for add class_decl vectorfield_decl intv method_decl add intx param_declvar_decl inti statements class symbol table vector

Representing Code in High-Level Intermediate Representation

Basic Idea Move towards assembly language Preserve high-level structure –object format –structured control flow –distinction between parameters, locals and fields High-level abstractions of assembly language –load and store nodes –access abstract locals, parameters and fields, not memory locations directly

Representing Expressions Expression Trees Represent Expressions –Internal Nodes - Operations like +, -, etc. –Leaves - Load Nodes Represent Variable Accesses Load Nodes –ldf node for field accesses - field descriptor (implicitly accesses this - could add a reference to accessed object) –ldl node for local variable accesses - local descriptor –ldp node for parameter accesses - parameter descriptor –lda node for array accesses expression tree for array expression tree for index

Example x*x + y*y + ldf field descriptor for x in field symbol table for cartesianPoint class * ldf * field descriptor for y in field symbol table for cartesianPoint class

Example v[i]+x lda + ldp field descriptor for v in field symbol table for vector class local descriptor for i in local symbol table of vector add parameter descriptor for x in parameter symbol table of vector add ldl ldf

Special Case: Array Length Operator len node represents length of array –expression tree for array Example: v.length len field descriptor for v in field symbol table for vector class ldf

Representing Assignment Statements Store Nodes –stf for stores to fields field descriptor expression tree for stored value –stl for stores to local variables local descriptor expression tree for stored value –sta for stores to array elements expression tree for array expression tree for index expression tree for stored value

Representing Procedure Calls Call statement Refers to method descriptor for invoked method Has list of parameters (this is first parameter) vect.add(1) call 1 method descriptor for add in method symbol table for vector class ldl local descriptor for vect in local symbol table of method containing the call statement vect.add(1) constant

Example v[i]=v[i]+x lda + ldp field descriptor for v in field symbol table for vector class local descriptor for i in local symbol table of vector add parameter descriptor for x in parameter symbol table of vector add ldl sta ldlldf

Representing Flow of Control Statement Nodes –sequence node - first statement, next statement –if node expression tree for condition then statement node and else statement node –while node expression tree for condition statement node for loop body –return node expression tree for return value

Example while (i < v.length) v[i] = v[i]+x; local descriptor for i ldl field descriptor for vparameter descriptor for x while < len lda + ldp ldl sta ldl ldf

From Abstract Syntax Trees to Intermediate Representation

while (i < v.length) v[i] = v[i]+x; local descriptor for ifield descriptor for vparameter descriptor for x

while (i < v.length) v[i] = v[i]+x; local descriptor for i ldl field descriptor for vparameter descriptor for x

while (i < v.length) v[i] = v[i]+x; local descriptor for i ldl field descriptor for vparameter descriptor for x ldf

while (i < v.length) v[i] = v[i]+x; local descriptor for i ldl field descriptor for vparameter descriptor for x len ldf

while (i < v.length) v[i] = v[i]+x; local descriptor for i ldl field descriptor for vparameter descriptor for x < len ldf

while (i < v.length) v[i] = v[i]+x; local descriptor for i ldl field descriptor for vparameter descriptor for x < len ldf

while (i < v.length) v[i] = v[i]+x; local descriptor for i ldl field descriptor for vparameter descriptor for x < len ldlldf

while (i < v.length) v[i] = v[i]+x; local descriptor for i ldl field descriptor for vparameter descriptor for x < len lda ldlldf

while (i < v.length) v[i] = v[i]+x; local descriptor for i ldl field descriptor for vparameter descriptor for x < len ldaldp ldlldf

while (i < v.length) v[i] = v[i]+x; local descriptor for i ldl field descriptor for vparameter descriptor for x < len lda + ldp ldlldf

while (i < v.length) v[i] = v[i]+x; local descriptor for i ldl field descriptor for vparameter descriptor for x < len lda + ldp ldlldf

while (i < v.length) v[i] = v[i]+x; local descriptor for i ldl field descriptor for vparameter descriptor for x < len lda + ldp ldl ldf

while (i < v.length) v[i] = v[i]+x; local descriptor for i ldl field descriptor for vparameter descriptor for x < len lda + ldp ldl sta ldl ldf

while (i < v.length) v[i] = v[i]+x; local descriptor for i ldl field descriptor for vparameter descriptor for x while < len lda + ldp ldl sta ldl ldf

while (i < v.length) v[i] = v[i]+x; local descriptor for i ldl field descriptor for vparameter descriptor for x while < len lda + ldp ldl sta ldl ldf

Abbreviated Notation while (i < v.length) v[i] = v[i]+x; ldl i while < len lda + ldp x ldl i sta ldl i ldf v

From Abstract Syntax Trees to IR Recursively Traverse Abstract Syntax Tree Build Up Representation Bottom-Up Manner –Look Up Variable Identifiers in Symbol Tables –Build Load Nodes to Access Variables –Build Expressions Out of Load Nodes and Operator Nodes –Build Store Nodes for Assignment Statements –Combine Store Nodes with Flow of Control Nodes

Summary High-Level Intermediate Representation Goal: represent program in an intuitive way that supports future compilation tasks Representing program data –Symbol tables –Hierarchical organization Representing computation –Expression trees –Various types of load and store nodes –Structured flow of control Traverse abstract syntax tree to build IR

Dynamic Dispatch if (x == 0) { p = new point(); } else if (x < 0) { p = new cartesianPoint(); } else if (x > 0) { p = new polarPoint(); } y = p.distance(); Which distance method is invoked? if p is a point return(0) if p is a cartesianPoint return(x*x + y*y) if p is a polarPoint return(r*r) Invoked Method Depends on Type of Receiver!

Implementing Dynamic Dispatch Basic Mechanism: Method Table getColor method for point distance method for cartesianPoint getColor method for point distance method for polarPoint getColor method for point distance method for point method table for point objects method table for cartesianPoint objects method table for polarPoint objects angle method for polarPoint

Invoking Methods Compiler Numbers Methods In Each Inheritance Hierarchy –getColor is Method 0, distance is Method 1, angle is Method 2 Method Invocation Sites Access Corresponding Entry in Method Table Works For Single Inheritance Only –not for multiple inheritance, multiple dispatch, or interfaces

Hierarchy in Method Symbol Tables for Points distance method descriptor for distance getColor method descriptor for getColor method descriptor for distance distance method descriptor for distance method descriptor for angle angle

Lookup In Method Symbol Tables Starts with method table of declared class of receiver object Goes up class hierarchy until method found –point p; p = new point(); p.distance(); finds distance in point method symbol table –point p; p = new cartesianPoint(); p.distance(); finds distance in point method symbol table –cartesianPoint p; p = new cartesianPoint(); p.getColor(); finds getColor in point method symbol table

Static Versus Dynamic Lookup Static lookup done at compile time for type checking and code generation Dynamic lookup done when program runs to dispatch method call Static and dynamic lookup results may differ! –point p; p = new cartesianPoint(); p.distance(); Static lookup finds distance in point method table Dynamic lookup invokes distance in cartesianPoint class Dynamic dispatch mechanism used to make this happen

Static and Dynamic Tables Static Method Symbol Table –Used to look up method definitions at compile time –Index is method name –Lookup starts at method symbol table determined by declared type of receiver object –Lookup may traverse multiple symbol tables Dynamic Method Table –Used to look up method to invoke at run time –Index is method number –Lookup simply accesses a single table element

getColor method for point distance method for cartesianPoint getColor method for point distance method for polarPoint getColor method for point distance method for point angle method for polarPoint distance method descriptor for distance getColor method descriptor for getColor method descriptor for distance distance method descriptor for distance method descriptor for angle angle

v field descriptor add class descriptor for vector this i this descriptor local descriptor x parameter descriptor Method descriptor for add class_decl vectorfield_decl intv method_decl add intx param_declvar_decl inti statements code for add method class symbol table vector

Eliminating Parse Tree Construction Parser actions build symbol tables –Reduce actions build tables in bottom-up fashion –Actions correspond to activities that take place in top-down fashion in parse tree traversal Eliminates intermediate construction of parse tree - improves performance Also less code to write (but code may be harder to write than if just traverse parse tree)

class vector { int v[]; void add(int x) { int i;... }} class symbol table

field_decl intv field descriptor class vector { int v[]; void add(int x) { int i;... }} class symbol table

intx param_decl field_decl intv field descriptor class vector { int v[]; void add(int x) { int i;... }} parameter descriptor class symbol table

intx param_decl field_decl intv field descriptor class vector { int v[]; void add(int x) { int i;... }} parameter descriptor var_decl inti local descriptor class symbol table

intx param_decl field_decl intv field descriptor class vector { int v[]; void add(int x) { int i;... }} parameter descriptor var_decl inti local descriptor statements code for add method class symbol table

intx param_decl field_decl intv field descriptor class vector { int v[]; void add(int x) { int i;... }} parameter descriptor var_decl inti local descriptor statements code for add method this this descriptor x i Method descriptor for add method_decl add class symbol table

intx param_decl field_decl intv field descriptor class vector { int v[]; void add(int x) { int i;... }} parameter descriptor var_decl inti local descriptor statements code for add method this this descriptor x i Method descriptor for add method_decl add class symbol table add class descriptor for vector v

Nested Scopes So far, have seen several kinds of nesting –Method symbol tables nested inside class symbol tables –Local symbol tables nesting inside method symbol tables Nesting disambiguates potential name clashes –Same name used for class field and local variable –Name refers to local variable inside method

Nested Code Scopes Symbol tables can be nested arbitrarily deeply with code nesting: class bar { baz x; int foo(int x) { double x = 5.0; { float x = 10.0; { int x = 1;... x...}... x... }... x... } Note: Name clashes with nesting can reflect programming error. Compilers often generate warning messages if it occurs.

What is a Parse Tree? Parse Tree Records Results of Parse External nodes are terminals/tokens Internal nodes are non-terminals class_decl::=‘class’ name ‘{’field_decl method_decl‘}’ field_decl::= ‘int’ name ‘[];’ method_decl::= ‘void’ name ‘( ’ param_decl ‘) ’ ‘{‘ var_decl stats ‘}’

Abstract Versus Concrete Trees Remember grammar hacks –left factoring, ambiguity elimination, precedence of binary operators Hacks lead to a tree that may not reflect cleanest interpretation of program May be more convenient to work with abstract syntax tree (roughly, parse tree from grammar before hacks)

Building IR Alternatives Build concrete parse tree in parser, translate to abstract syntax tree, translate to IR Build abstract syntax tree in parser, translate to IR Roll IR construction into parsing

FromAbstract Syntax Trees to Symbol Tables Recursively Traverse Tree Build Up Symbol Tables As Traversal Visits Nodes

Traversing Class Declarations Extract Class Name and Superclass Name Create Class Descriptor (field and method symbol tables), Put Descriptor Into Class Symbol Table Put Array Descriptor Into Type Symbol Table Lookup Superclass Name in Class Symbol Table, Make Superclass Link in Class Descriptor Point to Retrieved Class Descriptor Traverse Field Declarations to Fill Up Field Symbol Table Traverse Method Declarations to Fill Up Method Symbol Table