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

Similar documents
COS 320. Compiling Techniques

Lecture 15 CIS 341: COMPILERS

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

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

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

Writing Evaluators MIF08. Laure Gonnord

Chapter 3 (part 3) Describing Syntax and Semantics

Syntax Errors; Static Semantics

Operational Semantics. One-Slide Summary. Lecture Outline

CSCE 314 Programming Languages. Type System

Harvard School of Engineering and Applied Sciences Computer Science 152

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

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

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Operational Semantics of Cool

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Separate Compilation Model

Chapter 2: Introduction to C++

Programming Languages Third Edition

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

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

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

(Not Quite) Minijava

LECTURE 02 INTRODUCTION TO C++

Pointers, Dynamic Data, and Reference Types

CS2141 Software Development using C/C++ C++ Basics

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

Lecture 16: Static Semantics Overview 1

Semantics of programming languages

Semantics of programming languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

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

Dynamic Memory Allocation

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

ARG! Language Reference Manual

Visual C# Instructor s Manual Table of Contents

Names, Scope, and Bindings

GO MOCK TEST GO MOCK TEST I

Introduction to Programming, Aug-Dec 2006

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CMSC 330: Organization of Programming Languages

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

Launchpad Lecture -10

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

A brief introduction to C programming for Java programmers

The SPL Programming Language Reference Manual

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Midterm II CS164, Spring 2006

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

Operational Semantics of Cool

Semantics of programming languages

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

CHAPTER 3 BASIC INSTRUCTION OF C++

CS558 Programming Languages

Array. Prepared By - Rifat Shahriyar

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

CSC 467 Lecture 13-14: Semantic Analysis

Tour of common optimizations

Type Checking. Prof. James L. Frankel Harvard University

1 Lexical Considerations

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

Programming for Engineers Iteration

Continued from previous lecture

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

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

The Structure of a Syntax-Directed Compiler

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Important Java terminology

Type Checking and Type Inference

Chapter 3. Describing Syntax and Semantics

Names, Scope, and Bindings

Formal Semantics of Programming Languages

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

Introduction to Programming Using Java (98-388)

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

Static Checking and Type Systems

Lexical Considerations

Your first C++ program

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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

(Refer Slide Time: 4:00)

Chapter 2, Part III Arithmetic Operators and Decision Making

Lecture 3. Lecture

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

CAAM 420 FALL 2012 Lecture 9. Mishael Owoyemi

Lexical Considerations

A Short Summary of Javali

Object Oriented Software Design

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Decaf Language Reference

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

Intermediate Code Generation

1.3b Type Conversion

Scoping rules match identifier uses with identifier definitions. A type is a set of values coupled with a set of operations on those values.

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

3. Java - Language Constructs I

CA Compiler Construction

4. Assignment statements Give an assignment statement that sets the value of a variable called total to 20: Answer: total = 20;

Modules, Structs, Hashes, and Operational Semantics

Transcription:

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

Types What is a type? Type Checking: Helps find language-level errors: Memory Safety can t dereference something not a pointer Control-Flow Safety can t jump to something not code Type Safety redundant specification checked Helps find application-level errors: Ensures isolation properties Helps generate code: Is that + a floating point add or an integer add? 2

Defining a Type System RE à Lexing CFG à Parsers Inductive Definitions à Type Systems An inductive definition really has two parts: 1. Specification of the form of judgments A judgment is an assertion/claim, may or may not be true. A valid judgment is a true/provable judgment. 2. A collection of inference rules what allow you to conclude whether a judgment is true or false 3

Inference Rules An implementation-language-independent way: type system with inference rules. Read: if a has type bool and b has type bool, then a && b has type bool. 4

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 5

Judgments The premises and conclusions are called judgments. The most common judgments in type systems have the form: Read: expression e has type T. Means: Based on no outside evidence, e is an expression with type T 6

Axioms and Rules Examples: BT, BF, B&&, B 7

Type Checking and Type Inference Two activities: Type checking: Given an expression e and a type T, decide if e : T Type inference: Given an expression e, find a type T such that e : T Both activities necessary. Both originate from typing rules. 8

Type Checking Implementation Example: type checking for &&: check(a && b, bool): check(a, bool) check(b, bool) No patterns matching types other than bool. 9

Type Inference Implementation Example: type checking for &&: infer(a && b): check(a, bool) check(b, bool) return bool Inference involves checking. 10

Recall Symbol Table, Scope Topic Generally, a variable can be any type available in the language. In C and Java, type determined by the declaration of the variable. In inference rules, variables are collected to a context. Context is a symbol table of (variable, type) pairs. In inference rules, the context is denoted by the Greek letter Γ, Gamma. The judgment form for typing is generalized to: Read: expression e has type T in context Γ 11

Context Consider: This means: x + y > y is bool in context where x and y are ints Context notation: Adding variable to existing context: 12

Context Most judgments share the same Γ, because the context doesn t change. For declarations: The condition if x : T in Γ is not a judgment but a sentence in the metalanguage (English). (Condition is a symbol table lookup of x in Γ.) 13

Functions Function Application: Notation: 14

Proofs Proof Tree: a trace of the steps that the type checker performs, built up rule by rule. Each judgment is a conclusion from the ones above with some of the rules, indicated beside the line. This tree uses the variable rule and the rules for + and >: 15

Overloading The binary arithmetic operations (+ - * /) and comparisons (==!= < > <= >=) are overloaded in many languages. If the possible types are int, double, and string, the typing rules become: 16

Overloading Implementation First infer the type of the first operand, then check the second operand with respect to this type: infer (a + b) : t := infer(a) // check that t {int, double, string} check (b, t) return t 17

Type Conversion Example: an integer can be converted into a double Generally, integers and doubles have different binary representations operated upon by different instructions. Compiler generates a conversion instruction (or instructions) for type conversions. 18

Type Conversion 2 + hello produces 2hello Evaluate: 1 + 2 + "hello" + 1 + 2 19

Statement Validity When type-checking a statement, simply check whether the statement is valid. A new judgment form: Read: Statement s is valid in environment Γ. Example: while 20

Expression Statements Some expressions simply need a type inference. For example: assignments and function calls. Parameters of the function define the context. The body statements s 1... s n are checked in this context. Context may change within the body from declarations. Check all variables in the parameter list are distinct. 21

Return Statements Return statement should be of expected type. Control flow makes this interesting: 22

Declarations and Block Structure Each declaration has a scope, in a certain block. Blocks in C and Java correspond (roughly) to parts of code between curly brackets: { } Two principles regulate the use of variables: 1. A variable declared in a block has its scope till the end of that block. 2. A variable can be declared again in an inner block, but not otherwise. 23

24 Declarations and Block Structure

Stack of Contexts Context to deal with blocks: Instead of a simple lookup table, Γ must be a stack of lookup tables. Notation: where Γ 1 is an old (i.e. outer) context and Γ 2 an inner context. The innermost context is the top of the stack. Recall Symbol Table Discussion 25

Declarations A declaration introduces a new variable in the current scope, checked to be fresh with respect to the context. Rules for sequences of statements, not just individual statements: A declaration extends the context used for checking the statements that follow: 26

27 Example: If Statement Derivation/Proof

28 Example: sizeof

29 Example: Function