CSE 504: Compiler Design. Intermediate Representations Symbol Table

Similar documents
CSE 504: Compiler Design. Runtime Environments

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

The Procedure Abstraction Part I: Basics

Intermediate Representations & Symbol Tables

Symbol Tables. For compile-time efficiency, compilers often use a symbol table: associates lexical names (symbols) with their attributes

The Procedure Abstraction

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

What about Object-Oriented Languages?

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Symbol Table Information. Symbol Tables. Symbol table organization. Hash Tables. What kind of information might the compiler need?

Semantic Analysis. Compiler Architecture

Syntax Errors; Static Semantics

UNIT-V. Symbol Table & Run-Time Environments Symbol Table

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Compiler construction

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

The role of semantic analysis in a compiler

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

About this exam review

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Modelica Change Proposal MCP-0019 Flattening (In Development) Proposed Changes to the Modelica Language Specification Version 3.

Optimizer. Defining and preserving the meaning of the program

The Structure of a Syntax-Directed Compiler

UNIT-4 (COMPILER DESIGN)

CS 406/534 Compiler Construction Intermediate Representation and Procedure Abstraction

LECTURE 3. Compiler Phases

Intermediate Representations Part II

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Naming in OOLs and Storage Layout Comp 412

Lecture08: Scope and Lexical Address

Acknowledgement. CS Compiler Design. Semantic Processing. Alternatives for semantic processing. Intro to Semantic Analysis. V.

Runtime Environment. Part II May 8th Wednesday, May 8, 13

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

Today's Topics. CISC 458 Winter J.R. Cordy

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

Late Binding; OOP as a Racket Pattern

When do We Run a Compiler?

Sri Vidya College of Engineering & Technology

Procedure and Object- Oriented Abstraction

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 3:: Names, Scopes, and Bindings (cont.)

5. Semantic Analysis!

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Appendix A The DL Language

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

PLD Semester Exam Study Guide Dec. 2018

The compilation process is driven by the syntactic structure of the program as discovered by the parser

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Compiler Optimisation

2. Reachability in garbage collection is just an approximation of garbage.

CS 4201 Compilers 2014/2015 Handout: Lab 1

CS 314 Principles of Programming Languages

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Hashing. 1. Introduction. 2. Direct-address tables. CmSc 250 Introduction to Algorithms

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

CSC 467 Lecture 13-14: Semantic Analysis

TABLES AND HASHING. Chapter 13

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Principles of Compiler Design

CS 314 Principles of Programming Languages. Lecture 13

Programming Assignment IV

CPS 506 Comparative Programming Languages. Programming Language

Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee

Names, Scopes, and Bindings II. Hwansoo Han

Chapter 9. Subprograms

CSE 12 Abstract Syntax Trees

10CS36: Object Oriented Programming with C++

Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412

Undergraduate Compilers in a Day

Semantic Analysis and Type Checking

Intermediate Code Generation

Intermediate Code Generation

Iterative Languages. Scoping

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables

Crafting a Compiler with C (II) Compiler V. S. Interpreter

Code Shape Comp 412 COMP 412 FALL Chapters 4, 5, 6 & 7 in EaC2e. source code. IR IR target. code. Front End Optimizer Back End


Concepts of Programming Languages

Semantic actions for declarations and expressions

Intermediate Formats. for object oriented languages

Hashing Techniques. Material based on slides by George Bebis

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Lecture 16: Static Semantics Overview 1

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

The Structure of a Syntax-Directed Compiler

Reading Assignment. Scanner. Read Chapter 3 of Crafting a Compiler.

Lecture 17. Improving open-addressing hashing. Brent s method. Ordered hashing CSE 100, UCSD: LEC 17. Page 1 of 19

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

CS 415 Midterm Exam Spring SOLUTION

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

Transcription:

Intermediate Representations Symbol Table Pradipta De pradipta.de@sunykorea.ac.kr

Current Topic Intermediate Representations Graphical IRs Linear IRs Symbol Table

Information in a Program Compiler manages different forms of information, occurring as names in a program: Variables: data type, storage class, name, lexical scope, base address and offset Array: number of dimensions, upper and lower bounds Structures: list of fields Functions or procedures: number of parameters and their type, types of return values, what variables a procedure can reference (in OOP)

How to manage the information Record the information in the IR Common approach for compiler design Record information as attributes of nodes Variables in an AST are annotated Disadvantage: navigate AST to find the information Derive the information every time it is required If IR is on disk, then recompute information may be faster than disk access

Symbol Table A central repository of facts with efficient access mechanisms to it Localizes information derived from distant parts of the code Simplifies design and implementation of code that accesses the data during compilation Avoids the expense of searching a tree

Implementation: Symbol Table Hash table based implementation Names, along with any information derived by the compiler, are mapped into the hash table Constant time expected case lookup Open Hashing On collision, use a linear list of records Open Addressing Avoid collision by recomputing a new slot if the slot is not empty

Symbol Table Interface Lookup(name) return the record stored in the table at h(name) No record found error Insert(name, record) Stores the information in record in the table at h(name) Suitable for Adhoc Syntax Directed Translation While processing declaration, build up a set of attributes for each variable On a production that declares a variable, insert name and attributes into the symbol table When variable name occurs outside declaration syntax, use lookup for the name Lookup fails on undeclared names For languages with a single name space this is implementation is sufficient Typically languages have nested scopes

Nested Scopes Programming languages allow names to be declared at multiple levels Each level determines a scope Scope is region in program s text where the name is valid When scopes are nested, then a name must be associated to the correct scope Compiler uses a lexically scoped symbol table Map each variable name to its specific declaration (called name resolution)

Example: Nested Scope

Managing nested scopes Create a new symbol table on entering a new lexical scope Creates a sheaf of tables linked together in the lexical nesting order Insert operates on the current symbol table On lookup, start by checking the current symbol table, and move up the nesting order Two additional interfaces for symbol table InitializeScope() increment current level create new symbol table and link to enclosing levels table FinalizeScope() Change current level pointer, and decrement current level

Execution b = a + b + c + w Level 2 is entered twice

Nested Symbol Table: Implementation Choices Lexical Scoping with Open Hashing

Nested Symbol Table: Implementation Choices A two dimensional hash table implementation Uses two separate hash tables One table hashes the symbol name Another is the hash table for field names or symbol attributes (like type, offset, length)

Scoping in Object Oriented Languages Scoping is guided by the structure of the data, in addition to structure of code Record inheritance hierarchy Java: external classes that are referenced in the code Simple implementation Attach a symbol table to each class with two nesting hierarchies One for lexical scoping within individual methods Other for inheritance hierarchy for each class To resolve a name fee in method m in Class C, Check lexically scoped symbol table for m If not found, then search scope for various classes in the inheritance hierarchy, starting with C If this fails, then check global symbol table for a class