Type Analysis and Typed Compilation Stephanie Weirich Cornell University.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Modern Programming Languages, 2nd ed.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
COS 441 Exam Stuff David Walker. TAL 2 Logistics take-home exam will become available on the course web site Jan write down when you download &
The Semantic Soundness of a Type System for Interprocedural Register Allocation and Constructor Registration Torben Amtoft Kansas State University joint.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Foundational Certified Code in a Metalogical Framework Karl Crary and Susmit Sarkar Carnegie Mellon University.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Current Techniques in Language-based Security David Walker COS 597B With slides stolen from: Steve Zdancewic University of Pennsylvania.
1 How to transform an analyzer into a verifier. 2 OUTLINE OF THE LECTURE a verification technique which combines abstract interpretation and Park’s fixpoint.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
CSE 425: Semantics II Implementing Scopes A symbol table is in essence a dictionary –I.e., every name appears in it, with the info known about it –Usually.
Type Checking.
Ross Tate, Juan Chen, Chris Hawblitzel. Typed Assembly Languages Compilers are great but they make mistakes and can introduce vulnerabilities Typed assembly.
Introduction The Approach ’ s Overview A Language of Pointers The Type System Operational Semantics Type Safety Type Inference The Rest of C Experiments.
Typed Assembly Languages COS 441, Fall 2004 Frances Spalding Based on slides from Dave Walker and Greg Morrisett.
Intensional Polymorphism in Type-Erasure Semantics Karl Crary, Stephanie Weirich, Greg Morrisett Presentation by Nate Waisbrot.
CS 536 Spring Run-time organization Lecture 19.
Survey of Typed Assembly Language (TAL) Introduction and Motivation –Conventional untyped compiler < Typed intermediate languages –Typed intermediate language.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
Typed Memory Management in a Calculus of Capabilities David Walker (with Karl Crary and Greg Morrisett)
Good Advice for Type-directed Programming Aspect-oriented Programming and Extensible Generic Functions Geoffrey Washburn [ ] Joint.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
STAL David Walker (joint work with Karl Crary, Neal Glew and Greg Morrisett)
Run time vs. Compile time
A Type System for Expressive Security Policies David Walker Cornell University.
Run-time Environment and Program Organization
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
1/25 Pointer Logic Changki PSWLAB Pointer Logic Daniel Kroening and Ofer Strichman Decision Procedure.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Introduction to Problem Solving. Steps in Programming A Very Simplified Picture –Problem Definition & Analysis – High Level Strategy for a solution –Arriving.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
Singleton Kinds and Singleton Types Christopher A. Stone August 2, 1999 Thesis Committee Bob Harper, chair Peter Lee John Reynolds Jon Riecke (Bell Laboratories)
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Object-Oriented Programming Chapter Chapter
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
Prologue Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Dr. Hussien Sharaf Dr Emad Nabil. Dr. Hussien M. Sharaf 2 position := initial + rate * Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 *
An Efficient Compilation Framework for Languages Based on a Concurrent Process Calculus Yoshihiro Oyama Kenjiro Taura Akinori Yonezawa Yonezawa Laboratory.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Type Checking and Type Inference
Programming Languages and Compilers (CS 421)
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2017.
Compiler Construction (CS-636)
Run-time organization
Introduction to Compilers Tim Teitelbaum
Chapter 1: Introduction to Compiling (Cont.)
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
TALx86: A Realistic Typed Assembly Language
Programming Languages and Compilers (CS 421)
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2018.
Closure Representations in Higher-Order Programming Languages
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2017.
Drew Wyborski Programming Languages
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2019.
Presentation transcript:

Type Analysis and Typed Compilation Stephanie Weirich Cornell University

ICFP '98 Outline Typed Compilation background Type Analysis background Initial framework - Type Passing Problems with Type Passing Type Erasure framework Closure Conversion comparison Related Work

ICFP '98 Traditional Compilation Untyped Assembly Language Untyped Intermediate Language Typed Abstract Syntax Implicitly Typed Source Language Compilation is a series of translations between several languages

ICFP '98 Typed Compilation Assembly Language Typed Intermediate Language Typed Abstract Syntax Most of those languages are typed, and the types are translated with the terms Implicitly Typed Source Language

ICFP '98 Typed Compilation Safety –“well-typed programs can’t go wrong” –Types describe invariants maintained by the compiler Performance –Types provide information which may be used by the compiler for optimization –Tag-free garbage collection –Data layout control

ICFP '98 Type Analysis Create functions from types to values Code may branch on an unknown type fun toString x:a => (typecase a of int => Int.toString | char => Char.toString | * => fn (fst,snd) => “(” ^ (toString fst) ^ “,” ^ (toString snd) ^ “)” ) x

ICFP '98 06/08/9 9 Data Layout Optimization Parametric polymorphism requires uniformity of representation for all arguments, regardless of their types. kinds k = T4 | T types t = B4 | t array | a | t ->t terms e,f = x | lx.e | e f | La:k.e | e [t] | ld(e,f) | i | e  f | if0 then else... | malloc(e,e...) | st(e,e,e) ld : (t array * B4) -> t st : (t array * B4 * t ) -> t

ICFP '98 06/08/9 9 ICFP '98 Because any array may be passed to a polymorphic function, all arrays must look the same, no matter the type of their elements. A:int Array B:bool Array sub = fn (A:a array,i:int) => wordsub(A,i) Polymorphic Subscript

ICFP '98 06/08/9 9 In languages such as C, the type of an array is always known at its use. A[2] B[2] wordsub(A,2) wordsub(B,0)&(1 0 int A[4] bool B[4] Monomorphic subscript

ICFP '98 06/08/9 9 ICFP '98 sub = fn (A:a array,i:int) => typecase a of int => wordsub(A,i) | bool => (wordsub(A,i div 32) & (1 0 A:int Array B:bool Array Type Analysis to the Rescue Type analysis allows us to determine the type at run time.

ICFP '98 Initial Framework -  i ML Make type abstraction/application explicit (as in System F) (  a. e) [ int ] Add typecase operator typecase t of int =>... | b * g =>... Code execution (operational semantics) now relies on the type system

ICFP '98 06/08/9 9 Type Passing Semantics (  a. (A:a array,i:int). typecase a of int => wordsub(A,i) bool => (wordsub(A,i div 32) & (1 0) [int] (A,3) sub[int](A,3) wordsub(A,3) typecase int of int => wordsub(A,3) bool => (wordsub(A,3 div 32) & (1 0

ICFP '98 Typechecking To typecheck a typecase term we annotate the term with its return type: tostring  a. x:a. (typecase [ d.d -> string ] a of int => Int.toString | char => Char.toString | b * g => fn (fst,snd):b*g => “(” ^ (toString[b] fst) ^ “,” ^ (toString[g] snd) ^ “)” ) x Then we check that each branch satisfies that type with the appropriate type substitution. The return type of the entire term is a substituted for d in the annotated type.

ICFP '98 06/08/9 9 ICFP '98 Problems Issues about expressiveness –Complicates low-level constructs –Can’t express some optimizations –Can’t express abstraction boundaries

ICFP '98 06/08/9 9 Complexity Polymorphic closure conversion –Minamide et al. [1996] –Morrisett et al. [1998] Duplication of effort –Optimization of runtime behavior –Explicit modeling of low level computation Allocation Semantics Typed Assembly Languages Complexity

ICFP '98 06/08/9 9 ICFP '98 Inefficiency Must pass all types even if some are never examined TIL -- eliminates unexamined run-time types in ad hoc manner in translation to untyped calculus Inefficiency

ICFP '98 06/08/9 9 ICFP '98 No way to hold types abstract if they can always be examined Clients allowed to break abstraction barriers and infer more information than desired HostClient bool refcapability Loss of Abstraction

ICFP '98 06/08/9 9 ICFP '98 Solution Pass terms that represent types

ICFP '98 06/08/9 9 Type Erasure Semantics sub [int] Rint (A,3)

ICFP '98 06/08/9 9 Type Erasure Semantics (  a. x:R(a). (A:a array,i:int). typecase x of Rint => wordsub(A,i) Rbool => (wordsub(A,i div 32) & (1 0) [int] Rint (A,3) sub [int] Rint (A,3) wordsub(A,3) typecase Rint of Rint => wordsub(A,3) Rbool => (wordsub(A,3 div 32) & (1 0

ICFP '98 06/08/9 9 Type Erasure Semantics ( . x:R(  ). (A:  array,i:int). typecase x of Rint => wordsub(A,i) Rbool => (wordsub(A,i div 32) & (1 0) [int] Rint (A,3) sub [int] Rint (A,3) wordsub(A,3) typecase Rint of Rint => wordsub(A,3) Rbool => (wordsub(A,3 div 32) & (1 0

ICFP '98 06/08/9 9 Formalization Special representation terms: –R int –R  (x,y) A term e which represents a type  has the special type R(  ). –R  (R int, R int ) : R(int  int) Instead of a type, the argument to typecase is a term of type R(  ) The type system tracks the correspondence between a type and its representation

ICFP '98 Typechecking To typecheck a typecase term we still annotate the term with its return type: tostring  a. x:a. y:R(a). (typecase [ d.d -> string ] y of Rint => Int.toString | Rchar => Char.toString | R*(r1,r2) as b*g => fn (fst,snd):b*g => “(” ^ (toString[b] fst r1) ^ “,” ^ (toString[g] snd r2) ^ “)” ) x We require that the argument to the typecase be of type R(t) for the typecase term to typecheck. We can then substitute a for d in the annotated type as before.

ICFP '98 06/08/9 9 SolutionSolution Everything that happens at run-time is described by the terms Can go to a type erasure semantics Optimization Traditional code optimizers can optimize type representations Sophisticated techniques still possible If a representation is not provided a type may not be analyzed

ICFP '98 Typed Closure Conversion At run-time, a first-class function is represented by a code pointer But now x is unbound -- so we change the type of l2 to take another argument A closure is just a code pointer paired with the values of its free variables -- its environment f  x:int. y:int. x + y l1: x:int. l2 l2: y:int. x + y l1: x:int. (l2, x) l2: y,x:int. x + y

ICFP '98 (f 3) 4 Typed Closure Conversion Application just extracts the environment and applies the function to it let clos = (f 3) in (#1 clos) (4,#2 clos) clos : int -> intclos : (int*int -> int) * int f  x:int. y:int. x + y

ICFP '98 Existential Types Unfortunately, the type of the closure now depends on its free variables Existential types hold the types of free variables abstract l1: x:int. pack (l2,x) as  env.((int*env -> int)*env) hiding int unpack (env,clos) = (f 3) in (#1 clos)(4,#2 clos) clos : (int*int -> int) * int

ICFP '98 Polymorphic Closure Conversion In a type passing semantics a function may also have free type variables at run time They also must be part of the closure, via translucent types The type environment is then abstracted using existential kinds  a. x:a.  y:int.... l1:  a. x:a.  (b=a).(l2[b], x) l2:  b. y:int,x:b.... clos :  k.  b:T.  a:k.  g:k = a. (int * b) -> int * b

ICFP '98 Closure Conversion In a type erasure semantics, only the type representation remains at run time. At the time of closure creation, the type arguments may be given to the function.  a. x:a, z:R(a). y:int.... l1:  a.  x:a,z:R(a). pack (l2[a], z) as  b.(int*b -> int)*b hiding (a*R(a)) l2:  b. y:int,(x:b,z:R(b))....

ICFP '98 Multi-stage Type Analysis Typed Assembly Language Typed Intermediate Language Typed Abstract Syntax Final Step of typed compilation: compile to a typed assembly language Implicitly Typed Source Language

ICFP '98 Multi-stage Type Analysis How do we preserve the meaning of typecase when the types themselves change? –in TALx86 both int and float are compiled into B4, the type of 4-byte values – int  int may be compiled into a variety of types depending on the calling convention

ICFP '98 06/08/9 9 Related Work Harper, Morrisett - POPL 95 Minamide, Morrisett, Harper - POPL 96 Minamide - 2nd Fuji Intl Workshop on Functional and Logic Programming 96 Morrisett, Walker, Crary, Glew - POPL 98 Crary, Weirich, Morrisett - ICFP 98 Crary, Weirich - ICFP 99

ICFP '98 Low-level Type Analysis How do we analyze types with quantifiers? –In TALx86 every function (polymorphic or not) is compiled into a polymorphic code pointer

ICFP '98 06/08/ Areas for Future Work

ICFP '98

Outline Introduction –Typed Compilation –Type Analysis in general toString example –Type Analysis in compilation bit array example Initial framework –syntax -- from examples –semantics type passing –problems –complication of theory –can’t express efficient code –loss of abstraction Type erasure semantics syntax dynamic semantics static semantics Closure Conversion example LX Teaser Related work

ICFP '98 A note about typechecking –In this example wordsub has a strange type a array * int -> int –It would better if it were of type int array * int -> int –Then argument to subscript must allways be an int array. But that forgets its actual type of bool array. So we create a special type, called packed array, with a type-level type analysis operator.

ICFP '98 06/08/9 9 12ICFP '98 Type Passing Semantics sub[int](A,3)

ICFP '98 06/08/ Type Passing Semantics ( . (A:  array,i:int). typecase  of int => wordsub(A,3) bool => (wordsub(A,3 div 32) & (1 0) [int] (A,3) sub[int](A,3)

ICFP '98 06/08/ Type Passing Semantics ( . (A:  array,i:int). typecase  of int => wordsub(A,3) bool => (wordsub(A,3 div 32) & (1 0) [int] (A,3) sub[int](A,3) typecase int of int => wordsub(A,3) bool => (wordsub(A,3 div 32) & (1 0

ICFP '98 06/08/ Formalization Special representation terms: –R int –R  (x,y)

ICFP '98 06/08/ Formalization Special representation terms: –R int –R  (x,y) A term e which represents a type  has the special type R(  ). –R  (R int, R int ) : R(int  int)

ICFP '98 06/08/ Type Erasure Semantics ( . x:R(  ). (A:  array,i:int). typecase x of Rint => wordsub(A,3) Rbool => (wordsub(A,3 div 32) & (1 0) [int] Rint (A,3) sub [int] Rint (A,3)

ICFP '98 06/08/ Type Erasure Semantics ( . x:R(  ). (A:  array,i:int). typecase x of Rint => wordsub(A,3) Rbool => (wordsub(A,3 div 32) & (1 0) [int] Rint (A,3) sub [int] Rint (A,3) typecase Rint of Rint => wordsub(A,3) Rbool => (wordsub(A,3 div 32) & (1 0

ICFP '98

06/08/9 9 5ICFP '98 Type based compilation TermsTypes Source Language Intermediate Language Machine Language

ICFP '98 Multi-stage Type Analysis

ICFP '98 Type Level Type Analysis

ICFP '98 Type Safe Language Give us guarentees about the run-time behavior of programs Types abstractly describe the run-time flow of values

ICFP '98 Traditional Compilation ( fn x => x+1 ) 3 ( x. x + 1 ) 3 l1: push 3 call l2 retn l2: mov eax,[esp+4] add eax,eax mov [esp+4], eax retn ( fn x : int => x +1 ) 3 Source File Type Inference Checking Untyped IL Machine Code

ICFP '98 Type Based Compilation ( fn x => x+1 ) 3 ( x : int. x + 1 ) 3 l1: push 3 call l2 retn l2: mov eax,[esp+4] add eax,eax mov [esp+4], eax retn ( fn x : int => x +1 ) 3 Source File Type Inference Checking Typed IL Machine Code

ICFP '98 Why Typed Compilation Safety -- assurances about compiler correctness Type based optimizations For example...

ICFP '98 But we don’t always know the types, what then ? For example -- Parametric polymorphism Introduce an operator into the language that can distinguish types Typecase !

ICFP '98 Need a language with this operator - lmli

ICFP '98 Second example print

ICFP '98 06/08/9 9 16ICFP '98 Performance and Safety TermsTypes Source IL Machine

ICFP '98 06/08/9 9 17ICFP '98 Safety Safety TermsTypes Source IL Machine TAL

ICFP '98 06/08/9 9 11ICFP '98 Type Passing Semantics Used by the language  i ML, the Intermediate language of TIL/ML and FLINT compilers Unlike most calculi where types may be erased prior to run-time, types do have an operational significance -- they are arguments to typecase terms.

ICFP '98 06/08/9 9 10ICFP '98 Intensional Type Analysis Valuable element of type-directed compilers Allows otherwise untypeable optimizations –Specialized data layout –Tag-free Garbage Collection –Polymorphic marshalling –...

ICFP '98 06/08/ Solution More efficient –Only pass type representations when necessary –Traditional code optimizers can help –Sophisticated techniques still possible Recovers abstraction –Can withhold representation from clients