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

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

Semantic Analysis and Type Checking

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

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

5. Semantic Analysis!

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

CA Compiler Construction

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation

Alternatives for semantic processing

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Introduction to optimizations. CS Compiler Design. Phases inside the compiler. Optimization. Introduction to Optimizations. V.

Register allocation. CS Compiler Design. Liveness analysis. Register allocation. Liveness analysis and Register allocation. V.

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

Intermediate Code Generation

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

Academic Formalities. CS Modern Compilers: Theory and Practise. Images of the day. What, When and Why of Compilers

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

G Programming Languages - Fall 2012

UNIT-4 (COMPILER DESIGN)

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

1 Lexical Considerations

The role of semantic analysis in a compiler

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

Context-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis

Academic Formalities. CS Language Translators. Compilers A Sangam. What, When and Why of Compilers

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

Last class. CS Principles of Programming Languages. Introduction. Outline

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

The Structure of a Syntax-Directed Compiler

Short Notes of CS201

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

Programming Languages Third Edition. Chapter 7 Basic Semantics

CS201 - Introduction to Programming Glossary By

Lecture 16: Static Semantics Overview 1

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

CS 314 Principles of Programming Languages

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

Lexical Considerations

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

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

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

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

Operational Semantics of Cool

Compiler construction

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Lexical Considerations

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

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

Intermediate Code Generation

Operational Semantics. One-Slide Summary. Lecture Outline

(Not Quite) Minijava

CS 314 Principles of Programming Languages. Lecture 13

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

When do We Run a Compiler?

A Short Summary of Javali

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Intermediate Formats. for object oriented languages

CS 132 Compiler Construction

Intermediate Formats. for object oriented languages

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

CS558 Programming Languages

CSE 307: Principles of Programming Languages

The Decaf Language. 1 Lexical considerations

Syntax Errors; Static Semantics

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

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

Challenges in the back end. CS Compiler Design. Basic blocks. Compiler analysis

CSCE 314 Programming Languages. Type System

Time : 1 Hour Max Marks : 30

Translation. From ASTs to IR trees

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

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Syntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans

Semantic Analysis. Lecture 9. February 7, 2018

CS 314 Principles of Programming Languages. Lecture 11

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far.

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

The Procedure Abstraction

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Classes, Structs and Records. Internal and External Field Access

Implementing Subprograms

The Procedure Abstraction Part I: Basics

Intermediate Formats. Martin Rinard Laboratory for Computer Science

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

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Intermediate Code Generation

Programming Languages

CS 360 Programming Languages Interpreters

Intermediate Representations & Symbol Tables

Register allocation. instruction selection. machine code. register allocation. errors

Project Compiler. CS031 TA Help Session November 28, 2011

Chapter 10. Implementing Subprograms ISBN

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

CS558 Programming Languages

Transcription:

Acknowledgement CS3300 - Compiler Design Intro to Semantic Analysis V. Krishna Nandivada IIT Madras Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and full citation on the first page. To copy otherwise, to republish, to post on servers, or to redistribute to lists, requires prior specific permission and/or fee. Request permission to publish from hosking@cs.purdue.edu. V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 2 / 21 Semantic Processing Alternatives for semantic processing The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the program based on its syntactic structure two purposes: finish analysis by deriving context-sensitive information (e.g. type checking) begin synthesis by generating the IR or target code associated with individual productions of a context free grammar or subtrees of a syntax tree one-pass analysis and synthesis one-pass compiler plus peephole one-pass analysis & IR synthesis + code generation pass multipass analysis multipass synthesis language-independent and retargetable (e.g. gcc) compilers (e.g. gcc) (e.g. gcc) Our focus in the assignments: One-pass analysis & IR synthesis + multipass analysis + multipass synthesis. V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 3 / 21 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 4 / 21

Goal - Type checking (MiniJava) Symbol tables We need generate type information. For fields, variables, expressions, functions. Need to enforce types: Assignments, function calls, expressions. We need to remember the type information and recall them as/where required symbol table. For compile-time efficiency, compilers use a symbol table: associates lexical names (symbols) with their attributes What items should be entered? variable names defined constants procedure and function names literal constants and strings source text labels compiler-generated temporaries (we ll get there) A symbol table is a compile-time structure Separate table for structure layouts (types) (includes field offsets and lengths) May need to preserve list of locals for the debugger V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 5 / 21 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 6 / 21 Symbol table information Storage classes of variables What kind of information might the compiler need? textual name data type dimension information declaring procedure lexical level of declaration storage class offset in storage if record, to structure table if parameter, by-reference or by-value? can it be aliased? to what other names? number and type of arguments to functions... (for aggregates) (base address) During code generation, each variable is assigned an address (addressing method), approrpriate to its storage class. A local variable is not assigned a fixed machine address (or relative to the base of a module) rather a stack location that is accessed by an offest from a register whose value does not point to the same location, each time the procedure is invoked. Why is it interesting? Four major storage classes: global, stack, stack static, registers V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 7 / 21 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 8 / 21

Symbol table organization How should the table be organized? Linear List O(n) probes per lookup easy to expand no fixed size one allocation per insertion Ordered Linear List O(log 2 n) probes per lookup using binary search insertion is expensive (to reorganize list) Binary Tree O(n) probes per lookup unbalanced O(log 2 n) probes per lookup balanced easy to expand no fixed size one allocation per insertion Hash Table O(1) probes per lookup on average expansion costs vary with specific scheme Nested scopes: block-structured symbol tables What information is needed? when asking about a name, want most recent declaration declaration may be from current scope or outer scope innermost scope overrides outer scope declarations Key point: new declarations occur only in current scope What operations do we need? void put (Symbol key, Object value) bind key to value Object get(symbol key) return value bound to key void beginscope() remember current state of table void endscope() close current scope and restore table to state at most recent open beginscope V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 9 / 21 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 10 / 21 Nested scopes: complications Nested scopes: complications (cont.) Fields and records: give each record type its own symbol table or assign record numbers to qualify field names in table with R do stmt : all IDs in stmt are treated first as R.id separate record tables: chain R s scope ahead of outer scopes record numbers: open new scope, copy entries with R s record number or chain record numbers: search using these first Implicit declarations: labels: declare and define name (in Pascal accessible only within enclosing scope) Ada/Modula-3/Tiger FOR loop: loop index has type of range specifier Overloading: link alternatives (check no clashes), choose based on context Forward references: bind symbol only after all possible definitions multiple passes Other complications: packages, modules, interfaces IMPORT, EXPORT V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 11 / 21 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 12 / 21

Attribute information Type expressions Attributes are internal representation of declarations Symbol table associates names with attributes Names may have different attributes depending on their meaning: variables: type, procedure level, frame offset types: type descriptor, data size/alignment constants: type, value procedures: formals (names/types), result type, block information (local decls.), frame size Type expressions are a textual representation for types: 1 basic types: boolean, char, integer, real, etc. 2 type names 3 constructed types (constructors applied to type expressions): 1 array(i,t) denotes an array of T indexed over I e.g., array(1...10,integer) 2 products: T 1 T 2 denotes Cartesian product of type expressions T 1 and T 2 3 records: fields have names e.g., record((a integer),(b real)) 4 s: (T) denotes the type to an object of type T 5 functions: D R denotes the type of a function mapping domain type D to range type R e.g., integer integer integer V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 13 / 21 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 14 / 21 Type descriptors Type compatibility Type descriptors are compile-time structures representing type expressions e.g., char char (integer)!! char char integer or char integer Type checking needs to determine type equivalence Two approaches: Name equivalence: each type name is a distinct type. Structural equivalence: two types are equivalent iff. they have the same structure (after substituting type expressions for type names) s t iff. s and t are the same basic types array(s 1,s 2 ) array(t 1,t 2 ) iff. s 1 t 1 and s 2 t 2 s 1 s 2 t 1 t 2 iff. s 1 t 1 and s 2 t 2 (s) (t) iff. s t s 1 s 2 t 1 t 2 iff. s 1 t 1 and s 2 t 2 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 15 / 21 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 16 / 21

Type compatibility: example Consider: type link = cell; var next : link; last : link; p : cell; q, r : cell; Under name equivalence: next and last have the same type p, q and r have the same type p and next have different type Under structural equivalence all variables have the same type Ada/Pascal/Modula-2/Tiger are somewhat confusing: they treat distinct type definitions as distinct types, so p has different type from q and r Type compatibility: Pascal name equivalence Build compile-time structure called a type graph: each constructor or basic type creates a node each name creates a leaf (associated with the type s descriptor) next link last = p cell q Type expressions are equivalent if they are represented by the same node in the graph r V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 17 / 21 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 18 / 21 Type compatibility: recursive types Consider: type link = cell; cell = record info : integer; next : link; end; We may want to eliminate the names from the type graph Eliminating name link from type graph for record: cell =record Type compatibility: recursive types Allowing cycles in the type graph eliminates cell: cell =record info integer next info integer next cell Think: If structural equivalence was to be used by Java, how to type check? V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 19 / 21 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 20 / 21

Enforcing type checks in MiniJava Examples Assignment statements, If-expression, Arithmatic expression, Function call, Return statement, V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 21 / 21