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

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

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

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

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

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 Front-End

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

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

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

CSE 401/M501 Compilers

Programming Languages Third Edition. Chapter 7 Basic Semantics

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

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

CMSC 330: Organization of Programming Languages

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

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

Introduction to Programming Using Java (98-388)

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Types and Type Inference

Semantic Analysis. Lecture 9. February 7, 2018

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Semantic analysis. Semantic analysis. What we ll cover. Type checking. Symbol tables. Symbol table entries

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

Syntax Errors; Static Semantics

Semantic Analysis and Type Checking

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

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

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

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

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

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

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

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

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

A Short Summary of Javali

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

Type Bindings. Static Type Binding

Semantic Analysis. Compiler Architecture

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

CSCE 314 Programming Languages. Type System

1. Every program must have at least one class declaration. (*) 2. Every class declared in a program must have a distinct identifier.

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

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

Chapter 5. Names, Bindings, and Scopes

Compiler construction

Types and Type Inference

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

(Not Quite) Minijava

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

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

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

Types. What is a type?

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

Project Compiler. CS031 TA Help Session November 28, 2011

CSE 401 Final Exam. December 16, 2010

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

Lecture 16: Static Semantics Overview 1

Fundamentals of Programming Languages

CSE 341: Programming Languages

CSCI 171 Chapter Outlines

CS558 Programming Languages

CS558 Programming Languages

1 Lexical Considerations

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

CS558 Programming Languages. Winter 2013 Lecture 3

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

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

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

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

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

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

CS558 Programming Languages

Names, Scopes, and Bindings II. Hwansoo Han

Data Types (cont.) Administrative Issues. Academic Dishonesty. How do we detect plagiarism? Strongly Typed Languages. Type System

More On Syntax Directed Translation

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1

The SPL Programming Language Reference Manual

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

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

IC Language Specification

CSE P 501 Exam 8/5/04

CSE 5317 Midterm Examination 4 March Solutions

JavaScript: Sort of a Big Deal,

Compiler construction

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

Ch. 12: Operator Overloading

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

Type Checking and Type Inference

CS558 Programming Languages

G Programming Languages - Fall 2012

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

CSE 401/M501 Compilers

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

Type systems. Static typing

C++ Important Questions with Answers

CSCI-GA Scripting Languages

COS 320. Compiling Techniques

Transcription:

CSE 401 Compilers Static Semantics Hal Perkins Winter 2009 2/3/2009 2002-09 Hal Perkins & UW CSE I-1

Agenda Static semantics Types Symbol tables General ideas for now; details later for MiniJava project 2/3/2009 2002-09 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); } } 2/3/2009 2002-09 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. 2/3/2009 2002-09 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? 2/3/2009 2002-09 Hal Perkins & UW CSE I-5

Semantic Analysis Main tasks Extract types and other information from the program Check language rules that go beyond the contextfree grammar Resolve names Relate assignments to and references of each variable Understand the program well enough for synthesis Final part of the analysis phase / front end of the compiler 2/3/2009 2002-09 Hal Perkins & UW CSE I-6

Symbol Tables Key data structure during semantic analysis For each identifier in the program, record its attributes (kind, type, etc.) Later: assign storage locations (stack frame or object offsets) for variables; other annotations Build during semantics pass Maps identifier names to information Declarations add bindings to table Uses look up information error if not found 2/3/2009 2002-09 Hal Perkins & UW CSE I-7

Nested Scopes Can have same name declared in different scopes Why? References use closest textuallyenclosing declaration static/lexical scoping, block structure closer declaration shadows declaration of enclosing scope CSE401 Wi09 8

Nested Scopes: Approach Simple solution one symbol table per scope each scope s s symbol table refers to its lexically enclosing scope s symbol table root is the global scope s symbol table look up declaration ation of name starting ting with nearest est symbol table, proceed to enclosing symbol tables if not found locally All scopes in program form a tree Industrial-strength compiler: engineer this so table operations are O(1) CSE401 Wi09 9

Name Spaces One name may unambiguously refer to different things class F { int F(F F) {// 3 different F s... new F()...... F =...... this.f(...)... } } MiniJava has three name spaces: classes, methods, and variables We always know which we mean for each name reference, based on its syntactic position So, have the symbol table store a separate map for each name space CSE401 Au08 10

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

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 e e Following slides: A sampler Not specific to the project (we ll do that later) 2/3/2009 2002-09 Hal Perkins & UW CSE I-12

A Sampling of Semantic Checks (0) Name use: 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 it 2/3/2009 2002-09 Hal Perkins & UW CSE I-13

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 p types Inferred type is a function of the operator and operands 2/3/2009 2002-09 Hal Perkins & UW CSE I-14

A Sampling of Semantic Checks (2) Assignment: exp 1 = exp 2 exp 1 is assignable (not a constant or expression) 1 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 2/3/2009 2002-09 Hal Perkins & UW CSE I-15

A Sampling of Semantic Checks (3) Cast: (exp 1 ) exp 2 exp 1 is a type 1 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 2/3/2009 2002-09 Hal Perkins & UW CSE I-16

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 2/3/2009 2002-09 Hal Perkins & UW CSE I-17

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) 2/3/2009 2002-09 Hal Perkins & UW CSE I-18

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) 2/3/2009 2002-09 Hal Perkins & UW CSE I-19

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 t are used before they are declared (fields, classes) Information stored in symbol tables 2/3/2009 2002-09 Hal Perkins & UW CSE I-20

Error Recovery Common example: 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 2/3/2009 2002-09 Hal Perkins & UW CSE I-21

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 2/3/2009 2002-09 Hal Perkins & UW CSE I-22

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 2/3/2009 2002-08 Hal Perkins & UW CSE I-23

Type Checking Terminology Static vs. dynamic typing static: checking done prior to execution (e.g. compile-time) dynamic: checking during execution Strong vs. weak typing strong: guarantees no illegal operations performed weak: can t make guarantees Caveats: Hybrids common Inconsistent usage common untyped, typeless could mean dynamic or weak static strong Java Lisp dynamic weak C PERL (1-5) CSE401 Wi09 24

Type Systems Base Types Fundamental, atomic types Typical examples: int, double, char Compound/Constructed Types Built up from other types (recursively) Constructors t include arrays, records/ structs/classes, pointers, enumerations, functions, modules, 2/3/2009 2002-09 Hal Perkins & UW CSE I-25

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) 2/3/2009 2002-09 Hal Perkins & UW CSE I-26

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 (i.e., graphs match) Name equivalence: two types are the same only if they have the same name, even if their structures match Different language g design philosophies p 2/3/2009 2002-09 Hal Perkins & UW CSE I-27

Structural Equivalence Structural equivalence says two types are equal iff they have same structure atomic types are tautologically t ll the same structure t if type constructors: same constructor recursively, equivalent arguments to constructor Ex: atomic types, array types, ML record types Implement with recursive implementation ti of equals, or by canonicalization of types when types created then use pointer equality CSE401 Wi09 28

Name Equivalence Name equivalence says that two types are equal iff they came from the same textual occurrence of a type constructor Ex: class types, C struct types (struct tag name), datatypes in ML special case: type synonyms (e.g. typedef) don t define new types Implement with pointer equality assuming appropriate representation of type info CSE401 Wi09 29

Type Casts In most languages, one can explicitly cast an object of one type to another sometimes cast means a conversion (e.g., casts between numeric types) sometimes cast means a change of static type without doing any computation (casts between pointer types or pointer and numeric types) CSE401 Wi09 30

Type Conversions and Coercions In Java, can explicitly convert an value of type double to one of type int can represent as unary operator typecheck, codegen normally In Java, can implicitly coerce an value of type int to one of type double compiler must insert unary conversion operators, based on result of type checking CSE401 Wi09 31

C and Java: type casts In C: safety/correctness of casts not checked allows writing low-level code that s type-unsafe more often used to work around limitations in C s static type system In Java: downcasts from superclass to subclass include run-time type check to preserve type safety static ti typechecker allows the cast codegen introduces run-time check Java s main form of dynamic type checking CSE401 Wi09 32

Coming Attractions Semantics checking for MiniJava project Then on to code generation 2/3/2009 2002-09 Hal Perkins & UW CSE I-33