COS 320. Compiling Techniques

Similar documents
Topic 6: Types COS 320. Compiling Techniques. Princeton University Spring Prof. David August. Adapted from slides by Aarne Ranta

Pierce Ch. 3, 8, 11, 15. Type Systems

Programming Languages

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

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

Simply-Typed Lambda Calculus

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

Type Systems, Type Inference, and Polymorphism

The role of semantic analysis in a compiler

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

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

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

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Functional programming Primer I

CMSC 330: Organization of Programming Languages

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

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

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Part III. Chapter 15: Subtyping

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

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Softwaretechnik. Lecture 03: Types and Type Soundness. Peter Thiemann. University of Freiburg, Germany SS 2008

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

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

Types and Type Inference

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Semantic Analysis and Type Checking

Operational Semantics. One-Slide Summary. Lecture Outline

CSCE 314 Programming Languages. Type System

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

Some instance messages and methods

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

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

CS558 Programming Languages

Type Checking and Type Inference

Chapter 3 (part 3) Describing Syntax and Semantics

Semantic Analysis. Lecture 9. February 7, 2018

Part III Chapter 15: Subtyping

Programming Languages and Compilers (CS 421)

Operational Semantics of Cool

CSCI-GA Scripting Languages

Types and Type Inference

Principles of Programming Languages

Topic 7: Intermediate Representations

Types, Type Inference and Unification

Programming Languages Third Edition

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

Introduction to Programming Using Java (98-388)

1 Lexical Considerations

Programming Languages Fall 2014

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

Subtyping. Lecture 13 CS 565 3/27/06

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

CSE 505: Programming Languages. Lecture 10 Types. Zach Tatlock Winter 2015

A Short Summary of Javali

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

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

Intermediate Code Generation

Writing Evaluators MIF08. Laure Gonnord

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Lecture 15 CIS 341: COMPILERS

Programming Languages Lecture 14: Sum, Product, Recursive Types

Subsumption. Principle of safe substitution

More Lambda Calculus and Intro to Type Systems

At build time, not application time. Types serve as statically checkable invariants.

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

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

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

Foundations: Syntax, Semantics, and Graphs

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

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

CS558 Programming Languages

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

Tradeoffs. CSE 505: Programming Languages. Lecture 15 Subtyping. Where shall we add useful completeness? Where shall we add completeness?

Topic 16: Issues in compiling functional and object-oriented languages

1. Abstract syntax: deep structure and binding. 2. Static semantics: typing rules. 3. Dynamic semantics: execution rules.

Tracing Ambiguity in GADT Type Inference

Compilers and computer architecture: Semantic analysis

5. Semantic Analysis!

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

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

Lecture #23: Conversion and Type Inference

More Lambda Calculus and Intro to Type Systems

In Our Last Exciting Episode

Second-Order Type Systems

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Lecture 10 Notes Linked Lists

Ornaments in ML. Thomas Williams, Didier Rémy. April 18, Inria - Gallium

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

CS4215 Programming Language Implementation

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

Informal Reference Manual for X

Introduction to OCaml

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

CS558 Programming Languages

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Mutable References. Chapter 1

Transcription:

Topic 5: Types COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1

Types: potential benefits (I) 2 For programmers: help to eliminate common programming mistakes, particularly mistakes that might lead to runtime errors (or prevent program from being compiled) provide abstractions and modularization discipline: can substitute code with alternative implementation without breaking surrounding program context Example (ML signatures): sig type sorted_list val sort : int list -> sorted_list val lookup : sorted_list -> int -> bool val insert: sorted_list -> int -> sorted_list end Internal definition of sorted_list not revealed to clients, so can replace one implementation by another one! Similarly for other invariants.

Types: potential benefits (II) For language designers: yields structuring mechanism for programs thus encodes abstraction principles that motivate development of new language basis for studying (interaction between) language features (references, exceptions, IO, other side effects, h-o-functions) formal basis for reasoning about program behavior (verification, security analysis, resource usage) 3

Types: potential benefits (III) For compiler writers: filter out programs that backend should never see (and can t handle) provide information that s useful in later phases: is that + a floating point add or an integer add? does value v fit into a single register? (size of data types) how should the stack frame for function f be organized (number and type/size of function arguments and return value) support generation of efficient code: less code needed for handling errors (and handling casting) enables sharing of implementations (source of confusion eliminated by types) posty2k: typed intermediate languages model intermediate representations as full language, with types that communicate structural code properties and analysis results between compiler phases (example: different types for caller/callee registers) refined type systems: provide alternative formalism for program analysis and optimization 4

Type-enforced safety guarantees Memory Safety can t dereference something not a pointer Control-Flow Safety can t jump to something not code Type Safety typing predications ( this value will be a string ) come true at run time, so no operator-operand mismatches All these errors are eliminated during development time, so applications much more robust! Prevents programmer from writing code that obviously can t be right. Contrast with C (weakly typed): implicit casting, null pointers, array-out-of-bounds, buffer overruns, security violations 5

Type systems: limitations Can t eliminate all runtime errors division by zero (input dependence) exception behavior often not modeled/enforced static type analyses are typically conservative: will reject some safe programs due to fundamental undecidability of perfectly predicting control flow Types typically involve some programmer annotations - - burden + documentation; + burden occurs at compile time, not runtime cryptic error messages but trade-off against debugging/ tracing effort upon hitting segfault 6

Types: design & implementation tasks Practical tasks (for compiler writer): develop algorithms for type inference: given an expression e, calculate whether there some type T such that e:t holds. If so, return (the best such) type T, or a representation of all such types. May need program annotations. type checking: given a fully type-annotated program, check that the typing rules are indeed applied correctly Theoretical tasks (language designer): study uniqueness of typings, existence of best types decidability and complexity of above tasks / algorithms type soundness: give a precise definition of good behavior (runtime model, error model) and prove that well-typed programs can t go wrong (Milner) Common formalism: derivation systems (cf formal logic) formal judgments, derivation/typing rules, derivation trees 7

Defining a Formal Type System т т RE Lexing CFG Parsers Inductive Definitions Type Systems / logical derivation systems Components of a type system: a notion of types specification of syntactic judgment forms A judgment is an assertion/claim, may or may not be true. implicitly or explicitly underpinned by an interpretation ( validity ) Typical judgement forms for type systems in PL: e:t, Γ e:t inference rules tell us how to obtain new judgment instances from previously derived ones should preserve validity so that only true judgments can be derived 8

Inference Rules An inference rule has a set of premises J 1,..., J n and one conclusion J, separated by a horizontal line: Read: If I can establish the truth of the premises J 1,...,J n, I can conclude: J is true. To check J, check J 1,...,J n. An inference rule with no premises is called an Axiom J always true 9

But what IS a type? Competing views: 1. Types are mostly syntactic entities, with little inherent meaning: the types for this language are A, B, C; here are the typing rules if you can t infer a type for e / check that e:t holds, reject e: untyped programs are not programs intent / design goals of type system (partially) revealed by what you can do with well-typed programs (e.g. compile to efficient code) 10

But what IS a type? Competing views: 1. Types are mostly syntactic entities, with little inherent meaning: the types for this language are A, B, C; here are the typing rules if you can t infer a type for e / check that e:t holds, reject e: untyped programs are not programs intent / design goals of type system (partially) revealed by what you can do with well-typed programs (e.g. compile to efficient code) 2. Types have semantic content, for example by capturing properties an execution may have types as an algorithmic approximation to classify behaviors if you can t derive a judgement e:t using the typing rules, but e still has the behavior captured by T, that s fine => types describe properties of a priori untyped programs 11

But what IS a type? traditional compiler view Competing views: 1. Types are mostly syntactic entities, with little inherent meaning: the types for this language are A, B, C; here are the typing rules if you can t infer a type for e / check that e:t holds, reject e: untyped programs are not programs intent / design goals of type system (partially) revealed by what you can do with well-typed programs (e.g. compile to efficient code) 2. Types have semantic content, for example by capturing properties an execution may have types as an algorithmic approximation to classify behaviors if you can t derive a judgement e:t using the typing rules, but e still has the behavior captured by T, that s fine => types describe properties of a priori untyped programs Many variations possible, depending on goals! often more modular 12

13 Type system for simple expressions

14 Type system for simple expressions

15 Type system for simple expressions

Type Checking Implementation fun check (e: Expr, t: Type): bool := case t of Bool => (case e of ) Int => (case e of ); type in the host language (the language the compiler is implemented in) 16

Type Checking Implementation fun check (e: Expr, t: Type): bool := case t of Bool => (case e of.. ) expressions/types of the object language, ie the language for which we re writing a compiler Int => (case e of ) 17

Type Checking Implementation fun check (e: Expr, t: Type): bool := case t of Bool => (case e of tt => true ff => true f e1 e2 => (case f of AND => check (e1,bool) andalso check (e2, Bool) (*similar case for OR *) LESS => check (e1, Int) andalso check(e2, Int) (*similar cases for EQ etc*) _ => false) IF e1 THEN e2 ELSE e3 => check (e1, Bool) andalso check (e2, Bool) andalso (e3, Bool)) Int => (case e of ) 18 Alternative: swap nesting of case distinctions for expressions and types.

Type Inference Implementation fun infer (e:expr): Type option = case e of tt => Some Bool ff => Some Bool BINOP f e1 e2 =>??? 19

Type Inference Implementation fun infer (e:expr): Type option = case e of tt => Some Bool ff => Some Bool BINOP f e1 e2 => (case f of AND => if check(e1, Bool) andalso check(e2, Bool) then Some Bool else None 20

Type Inference Implementation fun infer (e:expr): Type option = case e of tt => Some Bool ff => Some Bool BINOP f e1 e2 => (case f of AND => if check(e1, Bool) andalso check(e2, Bool) then Some Bool else None alternative that does not use check: case (infer e1, infer e2) of (Some bool, Some bool) => Some bool (_, _) => None 21

Type Inference Implementation fun infer (e:expr): Type option = case e of tt => Some Bool ff => Some Bool BINOP f e1 e2 => (case f of AND => if check(e1, Bool) andalso check(e2, Bool) then Some Bool else None PLUS => if check (e1, Int) andalso check(e2, Int) then Some Int else None LESS => if check (e1, Int) andalso check (e2, Int) then Some Bool else None ) IF e1 THEN e2 ELSE e3 =>??? alternative that does not use check: case (infer e1, infer e2) of (Some bool, Some bool) => Some bool (_, _) => None 22

Type Inference Implementation fun infer (e:expr): Type option = case e of tt => Some Bool ff => Some Bool BINOP f e1 e2 => (case f of AND => if check(e1, Bool) andalso check(e2,bool) then Some Bool else None (*similar cases for other binops*) ) IF e1 THEN e2 ELSE e3 => if check(e1, Bool) (*other expressions*) then case (infer e2, infer e3) of (Some t1, Some t2) => if t1=t2 then Some t1 else None (_, _) => None else None equality between types (often defined by induction) 23 Improvement: replace return type Type option by type that allows informative error messages in case where inference fails.

24 Type system for simple expressions

25 Type system for simple expressions

26 Type system for simple expressions

Adding variables (I) aka symbol table 27

Adding variables (II) side condition 28

Adding variables (II) side condition 29

Adding variables (II) side condition 30

31 Adding variables (III)

32 Adding variables (III)

33 Adding variables (III)

34 Adding functions (I)

35 Adding functions (I)

36 Adding functions (I)

37 Adding functions (II)

Adding functions (II)??? 38

39 Adding functions (II)

40 Adding functions (II)

Adding functions (II)??? 41

42 Adding functions (II)

43 Adding functions (II)

44 References (cf. ML-primer)

45 Products/tuples

46 Products/tuples

47 Products/tuples

48 Subtyping (I)

49 Subtyping (I)

50 Subtyping (I)

51 Subtyping (II)

52 Subtyping (II)

53 Subtyping (III): propagating through products

54 Subtyping (III): propagating through products

55 Subtyping (IV): propagating through functions

Subtyping (V): propagating through references 56

Subtyping (V): propagating through references 57

HW4: type analysis No higher-order functions 58

Additional aspects 59 separate name spaces for type definitions vs variables/ functions/procedures separate environments (cf Tiger) when syntax-directedness fails, requiring user-supplied type annotations helps inference Example: functions declarations, in particularly (mutually) recursive ones not covered: overloading (multiple typings for operators) example: arithmetic operations over int, float, double additional syntactic category (eg statements): new judgements Casting/coercion explicit (ie visible in program syntax): similar to other operators implicit: destroys syntax-directness similar to subtyping often symbolizes/triggers change of representation (int -> double) that s significant for compiler backend polymorphism (finite representations for infinitely many typings) arrays, class/object systems incl inheritance, signatures/modules/interfaces

Next steps IR code generation But first: need to learn a bit how data will be laid out in memory: activation records / frame stack Useful homework: read MCIL s sections on TIGER s semantic analysis (Chapter 5) and, if possible, TIGER s activation record layout (Chapters 6)! 60

Quiz 3: LR(0) How many states does the LR(0) parse table for the following grammar have? (You may guess or draw the parse table ;-) ) S B $ P ε E B B id P P ( E ) E B, E B id ( E ] (cf MCIL, page 85, exercise 3.11) 61