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

Similar documents
CSE P 501 Compilers. Static Semantics Hal Perkins Spring UW CSE P 501 Spring 2018 I-1

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

CSE Compilers. Reminders/ Announcements. Lecture 15: Seman9c Analysis, Part III Michael Ringenburg Winter 2013

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

Syntax Errors; Static Semantics

CSE 401 Final Exam. December 16, 2010

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

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Autumn /20/ Hal Perkins & UW CSE H-1

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Winter /22/ Hal Perkins & UW CSE H-1

The role of semantic analysis in a compiler

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Introduction to Programming Using Java (98-388)

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

Intermediate Code Generation

Compiler construction

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

Compiler Passes. Semantic Analysis. Semantic Analysis/Checking. Symbol Tables. An Example

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

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

CSE 431S Type Checking. Washington University Spring 2013

CSE 401/M501 Compilers

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

CSE 401/M501 Compilers

Project Compiler. CS031 TA Help Session November 28, 2011

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

CSE P 501 Exam 8/5/04

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

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

Programming Languages Third Edition. Chapter 7 Basic Semantics

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

The SPL Programming Language Reference Manual

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

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Classes, Structs and Records. Internal and External Field Access

Compilers CS S-05 Semantic Analysis

CMSC 330: Organization of Programming Languages

Semantic Processing. Semantic Errors. Semantics - Part 1. Semantics - Part 1

CSCE 314 Programming Languages. Type System

Types and Type Inference

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

CSE 401 Midterm Exam Sample Solution 11/4/11

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

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

Writing Evaluators MIF08. Laure Gonnord

A Short Summary of Javali

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

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

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Type systems. Static typing

Semantic Analysis and Type Checking

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

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

Fundamentals of Programming Languages

The PCAT Programming Language Reference Manual

Semantic actions for declarations and expressions

Semantic Analysis. Lecture 9. February 7, 2018

Semantic actions for declarations and expressions. Monday, September 28, 15

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

CSE 401/M501 Compilers

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

CS5363 Final Review. cs5363 1

CS558 Programming Languages

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Type checking. Jianguo Lu. November 27, slides adapted from Sean Treichler and Alex Aiken s. Jianguo Lu November 27, / 39

The Decaf Language. 1 Lexical considerations

Program Representations

CS S-06 Semantic Analysis 1

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

CSE 307: Principles of Programming Languages

Type Checking. Error Checking

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Types and Type Inference

CA Compiler Construction

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

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Short Notes of CS201

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

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CSE 431S Final Review. Washington University Spring 2013

CS201 - Introduction to Programming Glossary By

Lecture 15 CIS 341: COMPILERS

Semantic Analysis. Compiler Architecture

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

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

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

Compilers. Compiler Construction Tutorial The Front-end

CSE 401/M501 Compilers


Type Checking Binary Operators

Compiler construction in4020 lecture 5

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Context Analysis. Mooly Sagiv. html://

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

(Not Quite) Minijava

Time : 1 Hour Max Marks : 30

Transcription:

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

Agenda Static semantics Types Attribute grammars Representing types Symbol tables Note: this covers a superset of what we need for MiniJava 1/22/2008 2002-08 Hal Perkins & UW CSE I-2

What do we need to know to compile this? class C { int a; C(int initial) { a = initial; } void seta(int val) { a = val; } } class Main { public static void main(){ C c = new C(17); c.seta(42); } } 1/22/2008 2002-08 Hal Perkins & UW CSE I-3

Beyond Syntax There is a level of correctness that is not captured by a context-free grammar Has a variable been declared? Are types consistent in an expression? In the assignment x=y, is y assignable to x? Does a method call have the right number and types of parameters? In a selector p.q, is q a method or field of class instance p? Is variable x guaranteed to be initialized before it is used? Could p be null when p.q is executed? Etc. etc. etc. 1/22/2008 2002-08 Hal Perkins & UW CSE I-4

What else do we need to know to generate code? Where are fields allocated in an object? How big are objects? (i.e., how much storage needs to be allocated by new) Where are local variables stored when a method is called? Which methods are associated with an object/class? In particular, how do we figure out which method to call based on the run-time type of an object? 1/22/2008 2002-08 Hal Perkins & UW CSE I-5

Types Classical roles of types in programming languages Run-time safety Compile-time error detection Improved expressiveness (method or operator overloading, for example) Provide information to optimizer 1/22/2008 2002-08 Hal Perkins & UW CSE I-6

Semantic Analysis Main tasks: Extract types and other information from the program Check language rules that go beyond the contextfree grammar Key data structures: symbol tables For each identifier in the program, record its attributes (kind, type, etc.) Later: assign storage locations (stack frame offsets) for variables; other annotations 1/22/2008 2002-08 Hal Perkins & UW CSE I-7

Some Kinds of Semantic Information Information Generated From Used to process Symbol tables Declarations Expressions, statements Type information Constant/variable information Register & memory locations Declarations, expressions Declarations, expressions Assigned by compiler Operations Statements, expressions Code generation Values Constants Expressions 1/22/2008 2002-08 Hal Perkins & UW CSE I-8

Semantic Checks For each language construct we want to know: What semantic rules should be checked: specified by language definition (type compatibility, etc.) For an expression, what is its type (used to check whether the expression is legal in the current context) For declarations in particular, what information needs to be captured to be used elsewhere 1/22/2008 2002-08 Hal Perkins & UW CSE I-9

A Sampling of Semantic Checks (0) Name: id id has been declared and is in scope Inferred type of id is its declared type Memory location assigned by compiler Constant: v Inferred type and value are explicit 1/22/2008 2002-08 Hal Perkins & UW CSE I-10

A Sampling of Semantic Checks (1) Binary operator: exp 1 op exp 2 exp 1 and exp 2 have compatible types Identical, or Well-defined conversion to appropriate types Inferred type is a function of the operator and operands 1/22/2008 2002-08 Hal Perkins & UW CSE I-11

A Sampling of Semantic Checks (2) Assignment: exp 1 = exp 2 exp 1 is assignable (not a constant or expression) exp 1 and exp 2 have compatible types Identical, or exp 2 can be converted to exp 1 (e.g., char to int), or Type of exp 2 is a subclass of type of exp 1 (can be decided at compile time) Inferred type is type of exp 1 Location where value is stored is assigned by the compiler 1/22/2008 2002-08 Hal Perkins & UW CSE I-12

A Sampling of Semantic Checks (3) Cast: (exp 1 ) exp 2 exp 1 is a type exp 2 either Has same type as exp 1 Can be converted to type exp 1 (e.g., double to int) Is a superclass of exp 1 (in general requires a runtime check to verify that exp 2 has type exp 1 ) Inferred type is exp 1 1/22/2008 2002-08 Hal Perkins & UW CSE I-13

A Sampling of Semantic Checks (4) Field reference exp.f exp is a reference type (class instance) The class of exp has a field named f Inferred type is declared type of f 1/22/2008 2002-08 Hal Perkins & UW CSE I-14

A Sampling of Semantic Checks (5) Method call exp.m(e 1, e 2,, e n ) exp is a reference type (class instance) The class of exp has a method named m The method has n parameters Each argument has a type that can be assigned to the associated parameter Inferred type is given by method declaration (or is void) 1/22/2008 2002-08 Hal Perkins & UW CSE I-15

A Sampling of Semantic Checks (6) Return statement return exp; return; The expression can be assigned to a variable with the declared type of the method (if the method is not void) There s no expression (if the method is void) 1/22/2008 2002-08 Hal Perkins & UW CSE I-16

Semantic Analysis Parser builds abstract syntax tree Now need to extract semantic information and check constraints Can sometimes be done during the parse, but often easier to organize as separate phases And some things can t be done on the fly during the parse, e.g., information about identifiers that are used before they are declared (fields, classes) Information stored in symbol tables Generated by semantic analysis, used there and later 1/22/2008 2002-08 Hal Perkins & UW CSE I-17

Attribute Grammars A systematic way to think about semantic analysis Sometimes used directly, but, even if ad hoc techniques are used, AGs are a useful way to think about the analysis 1/22/2008 2002-08 Hal Perkins & UW CSE I-18

Attribute Grammars Idea: associate attributes with each node in the (abstract) syntax tree Examples of attributes Type information Storage location Assignable (e.g., expression vs variable lvalue vs rvalue for C/C++ programmers) Value (for constant expressions) etc. Notation: X.a if a is an attribute of node X 1/22/2008 2002-08 Hal Perkins & UW CSE I-19

Attribute Example Assume that each node has an attribute.val AST and attribution for (1+2) * (6 / 2) 1/22/2008 2002-08 Hal Perkins & UW CSE I-20

Inherited and Synthesized Attributes Given a production X ::= Y 1 Y 2 Y n A synthesized attribute is X.a is a function of some combination of attributes of Y i s (bottom up) An inherited attribute Y i.b is a function of some combination of attributes X.a and other Y j.c (top down) 1/22/2008 2002-08 Hal Perkins & UW CSE I-21

Informal Example of Attribute Rules (1) Attributes for simple arithmetic language Grammar program ::= decl stmt decl ::= int id; stmt ::= exp = exp ; exp ::= id exp + exp 1 1/22/2008 2002-08 Hal Perkins & UW CSE I-22

Informal Example of Attribute Rules (2) Attributes env (environment, e.g., symbol table); synthesized by decl, inherited by stmt type (expression type); synthesized kind (variable [lvalue] vs value [rvalue]); synthesized 1/22/2008 2002-08 Hal Perkins & UW CSE I-23

Attributes for Declarations decl ::= int id; decl.env = {identifier, int, var} 1/22/2008 2002-08 Hal Perkins & UW CSE I-24

Attributes for Program program ::= decl stmt stmt.env = decl.env 1/22/2008 2002-08 Hal Perkins & UW CSE I-25

Attributes for Constants exp ::= 1 exp.kind = val exp.type = int 1/22/2008 2002-08 Hal Perkins & UW CSE I-26

Attributes for Expressions exp ::= id id.type = exp.env.lookup(id) exp.type = id.type exp.kind = id.kind 1/22/2008 2002-08 Hal Perkins & UW CSE I-27

Attributes for Addition exp ::= exp 1 + exp 2 exp 1.env = exp.env exp 2.env = exp.env error if exp 1.type!= exp 2.type (or error if not combatable if rules are move complex) exp.type = exp 1.type exp.kind = val 1/22/2008 2002-08 Hal Perkins & UW CSE I-28

Attribute Rules for Assignment stmt ::= exp 1 = exp 2 ; exp 1.env = stmt.env exp 2.env = stmt.env Error if exp2.type is not assignment compatibile with exp1.type error if exp 1.kind == val (must be var) 1/22/2008 2002-08 Hal Perkins & UW CSE I-29

Example int x; x = x + 1; 1/22/2008 2002-08 Hal Perkins & UW CSE I-30

Extensions This can be extended to handle sequences of declarations and statements Sequence of declarations builds up combined environment with information about all declarations Full environment is passed down to statements and expressions 1/22/2008 2002-08 Hal Perkins & UW CSE I-31

Observations These are equational (functional) computations This can be automated, provided the attribute equations are non-circular Problems Non-local computation Can t afford to literally pass around copies of large, aggregate structures like environments 1/22/2008 2002-08 Hal Perkins & UW CSE I-32

In Practice Attribute grammars give us a good way of thinking about how to structure semantic checks Symbol tables will hold environment information Add fields to AST nodes to refer to appropriate attributes (symbol table entries for identifiers, expression types, etc.) Put in appropriate places in inheritance tree most statements don t need types, for example 1/22/2008 2002-08 Hal Perkins & UW CSE I-33

Symbol Tables Map identifiers to <type, kind, location, other properties> Operations Lookup(id) => information Enter(id, information) Open/close scopes 1/22/2008 2002-08 Hal Perkins & UW CSE I-34

Aside: Implementing Symbol Tables Topic in classical compiler course: implementing a hashed symbol table These days: use the collection classes that are provided with the standard language libraries (Java, C#, C++, etc.) For Java: Map (HashMap) will solve most of the problems List (ArrayList) for ordered lists (parameters, etc.) 1/22/2008 2002-08 Hal Perkins & UW CSE I-35

Symbol Tables for MiniJava (1) Global Per Program Information Single global table to map class names to per-class symbol tables Created in a pass over class definitions in AST Used in remaining parts of compiler to check field/method names and extract information about them 1/22/2008 2002-08 Hal Perkins & UW CSE I-36

Symbol Tables for MiniJava (2) Global Per Class Information 1 Symbol table for each class 1 entry for each method/field declared in the class Contents: type information, public/private, parameter types (for methods), storage locations (later), etc. In full Java, multiple symbol tables (or more complex symbol table) per class since methods and fields can have the same names in a class 1/22/2008 2002-08 Hal Perkins & UW CSE I-37

Symbol Tables for MiniJava (3) Global (cont) All global tables persist throughout the compilation And beyond in a real Java or C# compiler (e.g., symbolic information in Java.class files) 1/22/2008 2002-08 Hal Perkins & UW CSE I-38

Symbol Tables for MiniJava (4) Local symbol table for each method 1 entry for each local variable or parameter Contents: type information, storage locations (later), etc. Needed only while compiling the method; can discard when done 1/22/2008 2002-08 Hal Perkins & UW CSE I-39

Beyond MiniJava What we aren t dealing with: nested scopes Inner classes Nested scopes in methods reuse of identifiers in parallel or (if allowed) inner scopes Basic idea: new symbol table for inner scopes, linked to surrounding scope s table Look for identifier in inner scope; if not found look in surrounding scope (recursively) Pop back up on scope exit 1/22/2008 2002-08 Hal Perkins & UW CSE I-40

Engineering Issues In practice, want to retain O(1) lookup Use hash tables with additional information to get the scope nesting right In multipass compilers, symbol table information needs to persist after analysis of inner scopes for use on later passes See a compiler textbook for details 1/22/2008 2002-08 Hal Perkins & UW CSE I-41

Error Recovery What to do when an undeclared identifier is encountered? Only complain once (Why?) Can forge a symbol table entry for it once you ve complained so it will be found in the future Assign the forged entry a type of unknown Unknown is the type of all malformed expressions and is compatible with all other types to avoid redundant error messages 1/22/2008 2002-08 Hal Perkins & UW CSE I-42

Predefined Things Many languages have some predefined items Include code in the compiler to manually create symbol table entries for these when the compiler starts up Rest of compiler generally doesn t need to know the difference between predeclared items and ones found in the program 1/22/2008 2002-08 Hal Perkins & UW CSE I-43

Type Systems Base Types Fundamental, atomic types Typical examples: int, double, char Compound/Constructed Types Built up from other types (recursively) Constructors include arrays, records/ structs/classes, pointers, enumerations, functions, modules, 1/22/2008 2002-08 Hal Perkins & UW CSE I-44

Type Representation Create a shallow class hierarchy abstract class Type { } // or interface class ClassType extends Type { } class BaseType extends Type { } Should not need too many of these 1/22/2008 2002-08 Hal Perkins & UW CSE I-45

Base Types For each base type (int, boolean, others in other languages), create a single object to represent it Symbol table entries and AST nodes for expressions refer to these to represent type info Usually create at compiler startup Useful to create a void type object to tag functions that do not return a value (if you implement these) Also useful to create an unknown type object for errors (Having void and unknown type objects reduces the need for special case code for these in various places.) 1/22/2008 2002-08 Hal Perkins & UW CSE I-46

Compound Types Basic idea: represent with an object that refers to component types Limited number of these correspond directly to type constructors in the language (record/struct, array, function, ) A compound type is a graph 1/22/2008 2002-08 Hal Perkins & UW CSE I-47

Class Types class Id { fields and methods } class ClassType extends Type { Type baseclasstype; // ref to base class Map fields; // type info for fields Map methods; // type info for methods } (Note: may not want to do this literally, depending on how symbol tables for classes are represented. i.e., class symbol tables might be useful as the representation of the class type.) 1/22/2008 2002-08 Hal Perkins & UW CSE I-48

Array Types For Java this is simple: only possibility is # of dimensions and element type class ArrayType extends Type { int ndims; Type elementtype; } 1/22/2008 2002-08 Hal Perkins & UW CSE I-49

Array Types for Pascal &c. Pascal allows arrays to be indexed by any discrete type array[indextype] of elementtype Element type can be any other type, including an array class GeneralArrayType extends Type { Type indextype; Type elementtype; } 1/22/2008 2002-08 Hal Perkins & UW CSE I-50

Methods/Functions Type of a method is its result type plus an ordered list of parameter types class MethodType extends Type { } Type resulttype; List parametertypes; // type or void 1/22/2008 2002-08 Hal Perkins & UW CSE I-51

Type Equivalance For base types this is simple Types are the same if they are identical Normally there are well defined rules for coercions between arithmetic types Compiler inserts these automatically or when requested by programmer (casts) 1/22/2008 2002-08 Hal Perkins & UW CSE I-52

Type Equivalence for Compound Types Two basic strategies Structural equivalence: two types are the same if they are the same kind of type and their component types are equivalent, recursively Name equivalence: two types are the same only if they have the same name, even if their structures match Different language design philosophies 1/22/2008 2002-08 Hal Perkins & UW CSE I-53

Type Equivalence and Inheritance Suppose we have class Base { } class Extended extends Base { } A variable declared with type Base has a compile-time type of Base During execution, that variable may refer to an object of class Base or any of its subclasses like Extended (or can be null, which is compatible with all class types) Sometimes called the runtime type 1/22/2008 2002-08 Hal Perkins & UW CSE I-54

Useful Compiler Functions Create a handful of methods to decide different kinds of type compatibility: Types are identical Type t1 is assignment compatibile with t2 Parameter list is compatible with types of expressions in the call Normal modularity reasons: isolates these decisions in one place and hides the actual type representation from the rest of the compiler 1/22/2008 2002-08 Hal Perkins & UW CSE I-55

Implementing Type Checking for MiniJava Create multiple visitors for the AST First passe(s): gather information Collect global type information for classes Could do this in one pass, or might want to do one pass to collect class information, then a second one to collect per-class information about fields, methods Next set of passes: go through method bodies to check types, other semantic constraints 1/22/2008 2002-08 Hal Perkins & UW CSE I-56

Coming Attractions Need to start thinking about translating to object code (actually x86 assembly language, the default for this project) Next: x86 overview (as a target for simple compilers) Runtime representation of classes, objects, data, and method stack frames Assembly language code for higher-level language statements 1/22/2008 2002-08 Hal Perkins & UW CSE I-57