from system f to typedassemblylanguage by greg morrisett ...hendren/621/2012/olivier-tal.pdf ·...

25
From System F to TypedAssemblyLanguage by Greg Morrisett et al. Olivier S. McGill University, School of Computer Science March 26, 2012 Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al. March 26, 2012 1 / 25

Upload: dohuong

Post on 01-Sep-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

From System F to TypedAssemblyLanguageby Greg Morrisett et al.

Olivier S.

McGill University, School of Computer Science

March 26, 2012

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 1 / 25

Where the main author is introduced

Greg Morrisett

Received his PhD at CMU in 1995, “Compiling with Types”,supervised by Harper and Wing.

From 1996 to 2001, assistant professor at Cornell University

Since 2004, Professor at Harvard University.

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 2 / 25

The Paper

Published in proceeding of POPL98

Received “Most Influential POPL Paper Award” in 2008,

541 journal citations.

Main contribution

Show how high-level abstractions can be enforced at a machine-code level.

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 3 / 25

Table of content

1 Typed source

2 Typed compilation

3 Typed target

4 Conclusion

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 4 / 25

Types in High-Level languages: Motivation

Historically, type system seen as a safety net for programmers.

Benjamin Pierce’s definition of a type

The part of a compiler that tells you it doesn’t understand your program.

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 5 / 25

System F

λC with type variables, here extended with product type and recursion.

Factorial of 6 in System F:

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 6 / 25

Types in IR: Motivation

1) Type directed transformations

Authors’ motivation

Many optimizations require type information in order to succeed, and eventhose that do not often benefit from the additional structure supplied by atyping discipline.

In general, helps the compiler determine data representations, alignement,calling conventions and register selection([5])

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 7 / 25

Types in IR: Motivation(bis)

2) Certifying compilers

Authors’ motivation

Ability to type-check intermediate code provides an invaluable tool fordebugging new transformations and optimizations.

Type-preserving transformations

term τ → target τ

Alternative: Leroy’s Compcert([3])Specify, prove safety of a compiler in an interactive theorem prover.

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 8 / 25

Transformations(CPS)

Continuation passing style:

From A → B to ¬(A ∧ ¬B)

Optimize recursion, every call is in tail position.

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 9 / 25

Example

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 10 / 25

Transformations(Closure-conversion,hoisting)

Closes the functions λx .M into < λxenv .λx .M, env >, and pull them at atop-level let construct, separating code and data.(Minamide[4])

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 11 / 25

Example

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 12 / 25

Interlude

That’s where most type-preserving compilers drop types.

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 13 / 25

Types in Low-Level Languages: Motivation

From the idea of Proof-carrying codeFirst presented in Necula’s paper([6]) at POPL97(978 citations, 2007influencial paper award.).

Main idea

Ship code with a safety proof. Proof is verified before executing the code,after which the code can run normaly.

Compared to this paper: More restrictive type language(no dependenttypes), but complete algorithm to generate the TAL from well-formedsource.

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 14 / 25

Types in Low-Level Languages: Motivation(bis)

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 15 / 25

Type-safe allocation

Tuples are explicitelly allocated and filled field by field.e.g.: Creation of a pair.

We can only access fields of initialized type(τ1)

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 16 / 25

TAL

Based on a generic RISC instruction set.

Every basic block has a type which describe the memory state.

If we erase types, gets back assembly language except unpack andmalloc.

unpack is a movmalloc is a sequence of instruction that allocates a tuple of theappropriate size.

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 17 / 25

TAL Syntax

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 18 / 25

Example

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 19 / 25

Extension to TAL, JVML

1998: “Stack-Based Typed Assembly Language”Stack based instead of continuation passing, incorporate IO andexceptions.

1999: “Type-Safe Linking and Modular Assembly Language.”Formalize and explain how to have a type-safe linking phase, andextend the language to handle higher-order type constructors(HaskellType classes, ML modules)

1999:“TALx86: A realistic Typed Assembly Language”([2])From typed C-like language to fragment of Intel IA32 assembly(withtypes)

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 20 / 25

Summary

1 Certified compiler from System F to TAL.

2 Type-preserving transformations

3 Assembly-like language with high-level abstractions

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 21 / 25

Short Questions

1) Give one advantage of the Compcert model of certified compiler, andone of using the type-preserving(TAL) model.

2) Similarly to TALx86,JVM has high-level abstraction at bytecode level.Can you find what problem TALx86 is trying to bridge?

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 22 / 25

Answer to SQ2

JVM has high-level abstraction at bytecode level, but:

Difficult to compile high-level languages other than Java to JVM

JIT happens after verification, so error in compilers can introducesecurity hole.

JVM specifications are a mess, can’t prove e.g. type-soundness.

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 23 / 25

Long Question

Describe how you would add disjoint sums type to the type system.Concentrate on the representation at the TAL level.

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 24 / 25

Bibliography

[1] Greg Morrisett et al. From system f to typed assembly language. InProceedings of Principles of programming languages, POPL ’98, pages85–97, New York, NY, USA, 1998. ACM. ISBN 0-89791-979-3.

[2] Greg Morrisett et al. Talx86: A realistic typed assembly language. InSecond Workshop on Compiler Support for System Software, pages25–35, 1999.

[3] Xavier Leroy. Formal verification of a realistic compiler. Commun.ACM, 52(7):107–115, July 2009. ISSN 0001-0782.

[4] Yasuhiko Minamide, Greg Morrisett, and Robert Harper. Typed closureconversion. In Proceedings of Principles of programming languages,pages 271–283, 1996.

[5] Greg Morrisett. Compiling with types, 1995.

[6] George C. Necula. Proof-carrying code. In Proceedings of Principles ofprogramming languages, POPL ’97, pages 106–119, New York, NY,USA, 1997. ACM.

Olivier S. (McGill University) From System F to TypedAssemblyLanguage by Greg Morrisett et al.March 26, 2012 25 / 25