From System F to Typed Assembly Language

GREG MORRISETT and DAVID WALKER Cornell University KARL CRARY Carnegie Mellon University and NEAL GLEW Cornell University

We motivate the design of a typed assembly language (TAL) and present a type-preserving translation from System F to TAL. The typed assembly language we present is based on a conventional RISC assembly language, but its static type system provides support for enforcing high-level language abstractions, such as closures, tuples, and user-defined abstract data types. The type system ensures that well-typed programs cannot violate these abstractions. In addition, the typing constructs admit many low-level compiler optimizations. Our translation to TAL is specified as a sequence of type-preserving transformations, including CPS and closure conversion phases; type-correct source programs are mapped to type-correct assembly language. A key contribution is an approach to polymorphic closure conversion that is considerably simpler than previous work. The compiler and typed assembly language provide a fully automatic way to produce certified code, suitable for use in systems where untrusted and potentially malicious code must be checked for safety before execution.

Categories and Subject Descriptors: D.2.11 [Software Engineering]: Software Architectures— Languages; D.3.1 [Programming Languages]: Formal Definitions and Theory—Semantics, Syntax; D.3.2 [Programming Languages]: Language Classifications—Macro and Assembly Languages; D.3.4 [Programming Languages]: Processors—Compilers; F.3.2 [Logics and Meanings of Programs]: Semantics of Programming Languages—Operational Semantics; F.3.3 [Logics and Meanings of Programs]: Studies of Program Constructs—Type Structure

General Terms: Languages, Security, Theory, Verification Additional Key Words and Phrases: Closure conversion, certified code, secure extensible systems, typed assembly language, type-directed compilation, typed intermediate languages

This material is based on work supported in part by the AFOSR grant F49620-97-1-0013, ARPA/RADC grant F30602-1-0317, and NSF grants CCR-9317320 and CCR-9708915. Any opinions, findings, and conclusions or recommendations expressed in this article are those of the authors and do not reflect the views of these agencies. Authors’s addresses: G. Morrisett, D. Walker, and N. Glew: 4130 Upson Hall, Ithaca, NY 14853-7501, USA; K. Crary: School of Computer Science, Carnegie Mellon University, 5000 Forbes Avenue, Pittsburgh, PA 15213, USA. Permission to make digital/hard copy of all or part of this material without fee is granted provided that the copies are not made or distributed for profit or commercial advantage, the ACM copyright/server notice, the title of the publication, and its date appear, and notice is given that copying is by permission of the Association for Computing Machinery, Inc. (ACM). To copy otherwise, to republish, to post on servers, or to redistribute to lists requires prior specific permission and/or a fee.

2 · G. Morrisett et al.

1. INTRODUCTION

Compilers that manipulate statically typed intermediate languages have compelling advantages over traditional compilers that manipulate untyped program representations. An optimizing compiler for a high-level language such as ML may make as many as 20 passes over a single program, performing sophisticated analyses and transformations such as CPS conversion, closure conversion, unboxing, subsumption elimination, or region inference. Many of these optimizations require type information in order to succeed, and even those that do not, often do benefit from the additional structure supplied by a typing discipline. Moreover, the essence of many of these program transformations can be specified by the corresponding type translation. Types provide concise and yet precise documentation of the compilation process that can be automatically verified by a type checker. In practice, this technique has been invaluable for debugging new transformations and optimizations [Tarditi et al. 1996; Morrisett et al. 1996].

Today a small number of compilers work with typed intermediate languages in order to realize some or all of these benefits [Leroy 1992; Peyton Jones et al. 1993; Birkedal et al. 1993; Tarditi et al. 1996; Lindholm and Yellin 1996; Shao 1997; Dimock et al. 1997]. However, in all of these compilers, there is a conceptual line where types are lost. For instance, the TIL/ML compiler preserves type information through approximately 80% of compilation, but the remaining 20% is untyped. We show how to recode the untyped portions of a compiler to maintain type information through all phases of compilation and, in so doing, extend the paradigm of compiling with typed intermediate languages to compiling with typed target languages.

The target language in this article is a strongly typed assembly language (TAL) based on a generic RISC instruction set. The type system for TAL is surprisingly standard; supporting tuples, polymorphism, existential packages, and a restricted form of function pointer, yet it is sufficiently powerful that we can automatically generate well-typed code from high-level ML-like languages.

The TAL framework admits most conventional low-level optimizations such as global register allocation, copy propagation, constant folding, and dead-code elimination. Except for a small number of atomic code patterns, TAL also supports code motion optimizations such as instruction scheduling, common-subexpression elimination, and loop-invariant removal. Some more advanced implementation techniques are not supported by the simple typed assembly language we present here, including run-time code generation, intensional polymorphism, and array bounds check elimination. In Section 8 we discuss how to extend the type system presented here to support such techniques.

From System F to Typed Assembly Language · 3

TAL not only allows us to reap the benefits of types throughout a compiler, but it also enables a practical system for executing untrusted code safely and efficiently. For example, as suggested by the SPIN project [Bershad et al. 1995], operating systems could allow users to download TAL extensions into the kernel. The kernel would type check the TAL extension, thereby ensuring that it never accesses hidden resources within the kernel, always calls kernel routines with the right number and types of arguments, and so forth. After the type checker has verified the extension, the kernel can safely assemble it and dynamically link it in. Such a TAL-based system has a number of advantages. Currently, SPIN requires that extensions be written in a single high-level language (Modula-3) and use a single trusted compiler (along with cryptographic signatures) in order to ensure their safety. In contrast, a kernel based on a typed assembly language could support extensions written in a variety of high-level languages using a variety of untrusted compilers, since the safety of the resulting assembly code can be checked independently of the source code or the compiler. Furthermore, critical inner loops could be hand-written in assembly language in order to achieve better performance. TAL could also be used to support extensible Web browsers, extensible servers, active networks, or any other extensible system where security, performance, and language independence are desired. Of course, while type safety implies many important security properties such as memory safety, neither SPIN nor TAL can enforce other important security properties, such as termination, that do not follow from type safety.

Another framework for verifying safety properties in low-level programs, proposed by Necula and Lee, is called proof-carrying code (PCC) [Necula and Lee 1996; Necula 1997; 1998]. Necula and Lee encode the relevant operational content of simple type systems using extensions to first-order predicate logic, and automatically verify proofs of security properties such as memory safety [Necula 1997]. Because Necula and Lee use a general-purpose logic, they can encode more expressive security properties and permit some optimizations that are impossible in TAL. TAL, on the other hand, provides compiler writers with a higher-level set of abstractions than PCC. These abstractions make compiling languages with features such as higher-order functions and data types simpler. In order to do the same, a PCC programmer must build such abstractions from the low-level logical primitives, and it is not always obvious how to obtain a compact logical encoding of these language constructs that preserves the necessary security properties. Another benefit of the TAL abstractions is that they make it possible to automatically reconstruct the proof of type safety; TAL binaries can be more compact than PCC binaries because they do not need to contain a complete proof. Clearly, however, the ideal system contains both the compiler support and compact annotations of TAL and the flexibility of PCC. We leave this long-term goal to future research; here we focus on the theoretical framework for automatic compilation of high-level languages to type-safe assembly language.

2. OVERVIEW

The goals of this work are twofold: first, to define a type system for a conventional assembly language and to prove its soundness, and, second, to demonstrate the expressiveness of the resulting language by showing how to automatically compile a high-level language to type-correct assembly code. In this section, we give a brief overview of our typed assembly language and the structure of our compiler.

2.1 TAL

The primary goal of the TAL type system is to provide a fully automatic way to verify that programs will not violate the primitive abstractions of the language. In a conventional untyped assembly language, all values are represented as wordsized integers, and the primitive operations of the language apply to any such values. That is, in an untyped assembly language, there is only one abstraction: the machine word. In contrast, TAL provides a set of built-in abstractions, such as (word-sized) integers, pointers to tuples, and code labels, for each of which only some operations are applicable. For example, arithmetic is only permitted on integer values; dereferencing is only permitted for pointer values; and control transfer is only permitted for code labels. We say that a program becomes stuck if it attempts to perform an unpermissible operation. Hence, the primary goal of the TAL type system is to ensure that well-typed programs do not become stuck.

Because TAL treats integers as a separate abstraction from pointers or code labels, and because arithmetic is only permitted on integers, it is possible to show, that, in addition to never becoming stuck, a well-typed TAL program satisfies a number of important safety policies relevant to security. For instance, it is possible to conclude that programs cannot manufacture pointers to arbitrary objects, or that programs cannot jump to code that has not been verified.

In addition to providing a set of built-in abstractions, TAL provides a set of type constructors that may be used by programmers or compilers to build new abstractions. For example, in the functional language compiler that we sketch, closures (a high-level language abstraction) are encoded as TAL-level abstractions using the existential type constructor. In the high-level language, it is impossible for a program to apply any primitive operation to a closure except for function application. For instance, it is impossible for a program to inspect the environment of the closure. At the TAL level, closures are represented as a pair of a label (to some code) and an environment data structure (intended to hold the free variables of the code). We use an existential type to hide the type of the environment data structure and to connect it to the code. The resulting object prevents malicious or faulty code from reading the environment, or passing anything but the closure’s environment to the closure’s code.

In other work, we have extended the type system to support many more abstractions, such as modules [Glew and Morrisett 1999] and the run-time stack [Morrisett et al. 1998]. Here, we have attempted to keep the type system simple enough that the formalism may be understood and proven sound, yet powerful enough that we can demonstrate how a high-level ML-like language may be compiled to type-correct TAL code automatically.

The typed assembly language we present here is based on a conventional RISCstyle assembly language. In particular, all but two of the instructions are standard assembly operations. In an effort to simplify the formalism, we have omitted many typical instructions, such as a jump-and-link, that may be synthesized using our primitives. Figure 1 gives an example TAL program that, when control is transferred to the label l main, computes 6 factorial and then halts with the result in register r1. The code looks and behaves much like standard assembly code, except that each label is annotated with a code precondition that associates types with registers. The precondition specifies, that, before control can be transferred to the corresponding label, the registers must contain values of the specified types. Hence, before allowing a jump to l fact as in l main, the type checker ensures that an integer value resides in register r1.

2.2 A Type-Preserving Compiler

In order to motivate the typing constructs in TAL and to justify our claims about its expressiveness, we spend a large part of this article sketching a compiler from a

l main:

code[ ]{}. % entry point

    mov r1,6
    jmp l fact
l fact:

code[ ]{r1:int}. % compute factorial of r1 mov r2,r1 % set up for loop

    mov r1,1
    jmp l loop
l loop:

code[ ]{r1:int,r2:int}. % r1: the product so far,

% r2: the next number to be multiplied

    bnz r2,l nonzero            % branch if not zero

halt[int] % halt with result in r1

l nonzero:

code[ ]{r1:int,r2:int}.

mul r1,r1,r2 % multiply next number sub r2,r2,1 % decrement the counter

    jmp l loop

Fig. 1. A TAL program that computes 6 factorial.

variant of the polymorphic lambda-calculus to TAL. Our compiler is structured as five translations between six typed calculi:

[figure: λ^{F}; conversion; CPS; λ^{K}; conversion; Closure; λ^{C}; Hoisting; λ^{H}; Allocation; λ^{A}; generation; Code; TAL]

Each calculus is a first-class programming language in the sense that each translation operates correctly on any well-typed program of its input calculus. The translations do not assume that their input is the output from the preceding translation. This fact frees a compiler to optimize code aggressively between any of the translation steps. The inspiration for the phases and their ordering is derived from SML/NJ [Appel and MacQueen 1991; Appel 1992] (which is in turn based on the Rabbit [Steele 1978] and Orbit [Kranz et al. 1986] compilers) except that our compiler uses types throughout compilation.

The rest of this article proceeds by describing each of the languages and translations in our compiler in detail. We give the syntax and static semantics of each language as well as type-preserving translations between them. The middle calculi (λ^{K}, λ^{C}, λ^{H}, and λ^{A}) have many features in common. Therefore, we only describe λ^{K} in full, and each successive calculus is defined in terms of its differences from the preceding one.

We begin by presenting the compiler’s source language, λ^{F}, in Section 3. Section 4 describes the first intermediate language, λ^{K}, and gives a typed CPS translation to it based on Harper and Lillibridge [1993]. The CPS translation fixes the order of evaluation and names intermediate computations. Section 5 presents λ^{C} and gives a typed closure translation to it based on, but considerably simpler than, that of Minamide et al. [1996]. The closure translation makes the construction of functions’ environments and closures explicit, thereby rendering all data structures explicit. This is followed by a simple hoisting translation that lifts the (now closed) code to the top level.

Section 6 presents λ^{A}, in which the allocation and initialization of data structures is made explicit, and gives a translation from λ^{H} to λ^{A}. At this point in compilation, the intermediate code is essentially in a lambda-calculus syntax for assembly language (following the ideas of Wand [1992]). Section 7 presents the formal details of our typed assembly language. We show type safety for TAL, and also define a translation from λ^{A} to TAL. Finally, in Section 8 we discuss extensions to TAL to support additional language constructs and optimizations. We also describe our current implementation of TAL and discuss some directions for future investigation.

3. SYSTEM F

The source language for our compiler, λ^{F}, is a variant of System F [Girard 1971; 1972; Reynolds 1974] (the polymorphic λ-calculus) augmented with integers, products, and recursion on terms. The syntax for λ^{F} appears below:

types  τ, σ ::= α | int | τ_{1} → τ_{2} | ∀α.τ | ⟨τ⃗⟩
annotated terms  e ::= u^{τ}
terms  u ::= x | i | fix x(x_{1}:τ_{1}):τ_{2}.e | e_{1}e_{2} | Λα.e | e[τ] |
⟨e⃗⟩ | π_{i}(e) | e_{1} p e_{2} | if0(e_{1}, e_{2}, e_{3})
primitives  p ::= + | − | ×
type contexts  Δ ::= α_{1}, …, α_{n}
value contexts  Γ ::= x_{1}:τ_{1}, …, x_{n}:τ_{n}

Integers, the only base type, are introduced by integer literals (i), operated on by by arithmetic primitives p, and eliminated by a conditional expression if0. The term if0(e_{1}, e_{2}, e_{3}) evaluates to e_{2} when e_{1} evaluates to zero, and otherwise evaluates to e_{3}. We use the notation E⃗ to refer to a vector of syntactic objects drawn from the syntactic class E. For example, ⟨e⃗ ⟩ is shorthand for a tuple ⟨e_{1}, …, e_{n}⟩. The elimination form for tuples, π_{i}(e), evaluates to the ith field of the tuple e. Recursive functions are written fix x(x_{1}:τ_{1}):τ_{2}.e, where x is the name of the recursive function and may appear free in the body; x_{1} is its argument (with type τ_{1}); and e is its body (with type τ_{2}). Polymorphic functions are written Λα.e, where α is the abstracted type, and e is the body of the polymorphic function. For example, the polymorphic identity function may be written as Λα. fix id(x:α):α.x. Instantiation of a polymorphic expression e is written e [τ]. As usual, we consider types and expressions that differ only in the names of bound variables to be identical. We write the capture-avoiding substitution of E for X in E′ as E′[E/X].

We interpret λ^{F} with a conventional call-by-value operational semantics (which is not presented here). The static semantics (given in Figure 2) is specified as a set of inference rules for concluding judgments of the form Δ; Γ ⊢_{F} e : τ, where Δ is a context containing the free type variables of Γ, e, and τ; Γ is a context that assigns types to the free variables of e; and τ is the type of e. A second judgment Δ ⊢_{F} τ asserts that type τ is well-formed under type context Δ. In later judgments, we will use ∅ to denote an empty type or value context.

To simplify the presentation of our translations, we use type-annotated terms (e), which are unannotated terms (u) marked with their types. This decision allows us to present our translations in a simple, syntax-directed fashion, rather than making them dependent on the structure of typing derivations. The typing rules ensure that

Δ ⊢_{F} τ (FTV (τ) ⊆ Δ)  {Δ; Γ ⊢_{F} u : τ \atop Δ; Γ ⊢_{F} u^{τ} : τ}
Δ; Γ ⊢_{F} x : τ (Γ(x) = τ)  Δ; Γ ⊢_{F} i : int
Δ ⊢_{F} τ_{1} Δ ⊢_{F} τ_{2} Δ; Γ, x:τ_{1} → τ_{2}, x_{1}:τ_{1} ⊢_{F} e : τ_{2}

Δ; Γ ⊢_{F} fix x(x_{1}:τ_{1}):τ_{2}.e : τ_{1} → τ_{2} (x, x_{1} ∉ Γ)

Δ; Γ ⊢_{F} e_{1} : τ_{1} → τ_{2} Δ; Γ ⊢_{F} e_{2} : τ_{1}
Δ; Γ ⊢_{F} e_{1}e_{2} : τ_{2}
{Δ, α; Γ ⊢_{F} e : τ \atop Δ; Γ ⊢_{F} Λα.e : ∀α.τ} (α ∉ Δ)

Δ ⊢_{F} τ Δ; Γ ⊢_{F} e : ∀α.τ′

Δ; Γ ⊢_{F} e[τ] : τ′[τ/α]

{Δ; Γ ⊢_{F} e_{i} : τ_{i} \atop Δ; Γ ⊢_{F} ⟨e_{1}, …, e_{n}⟩ : ⟨τ_{1}, …, τ_{n}⟩}  {Δ; Γ ⊢_{F} e : ⟨τ_{1}, …, τ_{n}⟩ \atop Δ; Γ ⊢_{F} π_{i}(e) : τ_{i}} (1 ≤ i ≤ n)
Δ; Γ ⊢_{F} e_{1} : int Δ; Γ ⊢_{F} e_{2} : int
Δ; Γ ⊢_{F} e_{1} p e_{2} : int

Δ; Γ ⊢_{F} e_{1} : int Δ; Γ ⊢_{F} e_{2} : τ Δ; Γ ⊢_{F} e_{3} : τ

Δ; Γ ⊢_{F} if0(e_{1}, e_{2}, e_{3}) : τ

Fig. 2. Static Semantics of λ^{F}.

all annotations in a well-formed term are correct. In the interest of clarity, however, we will omit the type annotations in informal discussions and examples.

As a running example, we will consider compiling a term that computes 6 factorial:

(fix f(n:int):int. if0(n, 1, n × f(n − 1))) 6

4. CPS CONVERSION

The first compilation stage is conversion to continuation-passing style (CPS). This stage names all intermediate computations and eliminates the need for a control stack. All unconditional control transfers, including function invocation and return, are achieved via function call. The target calculus for this phase is called λ^{K}:

types  τ, σ ::= α | int | ∀[α⃗].(τ⃗) → void | ⟨τ_{1}, …, τ_{n}⟩
annotated values  v ::= u^{τ}
values  u ::= x | i | fix x[α⃗](x_{1}:τ_{1}, …, x_{n}:τ_{n}).e | ⟨v⃗⟩
primitives  p ::= + | − | ×
declarations  d ::= x = v | x = π_{i} v | x = v_{1} p v_{2}
terms  e ::= let d in e
| v[τ⃗](v⃗)
| if0(v, e_{1}, e_{2})
| halt[τ]v
type contexts  Δ ::= α_{1}, …, α_{n}
value contexts  Γ ::= x_{1}:τ_{1}, …, x_{n}:τ_{n}

Code in λ^{K} is nearly linear: it consists of a series of let bindings followed by a function call. The exception to this is the if0 construct, which forms a tree containing two subexpressions.

In λ^{K} there is only one abstraction mechanism (fix), which abstracts both type and value variables, thereby simplifying the rest of the compiler. The corresponding

Δ ⊢_{K} τ (FTV (τ) ⊆ Δ)  {Δ; Γ ⊢_{K} u : τ \atop Δ; Γ ⊢_{K} u^{τ} : τ}
Δ; Γ ⊢_{K} x : τ (Γ(x) = τ)  Δ; Γ ⊢_{K} i : int
Δ, α⃗ ⊢_{K} τ_{i} (Δ, α⃗); (Γ, x:∀[α⃗].(τ_{1}, …, τ_{n}) → void, x_{1}:τ_{1}, …, x_{n}:τ_{n}) ⊢_{K} e

Δ; Γ ⊢_{K} fix x[α⃗](x_{1}:τ_{1}, …, x_{n}:τ_{n}).e : ∀[α⃗].(τ_{1}, …, τ_{n}) → void (α⃗ ∉ Δ ∧ x, x⃗ ∉ Γ)

{Δ; Γ ⊢_{K} v_{i} : τ_{i} \atop Δ; Γ ⊢_{K} ⟨v_{1}, …, v_{n}⟩ : ⟨τ_{1}, …, τ_{n}⟩}

Δ; Γ ⊢_{K} v : τ Δ; Γ, x:τ ⊢_{K} e

Δ; Γ ⊢_{K} let x = v in e

(x ∉ Γ)

Δ; Γ ⊢_{K} v : ⟨τ_{1}, …, τ_{n}⟩ Δ; Γ, x:τ_{i} ⊢_{K} e
Δ; Γ ⊢_{K} let x = π_{i} v in e

(x ∉ Γ ∧ 1 ≤ i ≤ n)

Δ; Γ ⊢_{K} v_{1} : int Δ; Γ ⊢_{K} v_{2} : int Δ; Γ, x:int ⊢_{K} e

Δ; Γ ⊢_{K} let x = v_{1} p v_{2} in e

(x ∉ Γ)
Δ ⊢_{K} σ_{i} Δ; Γ ⊢_{K} v : ∀[α_{1}, …, α_{m}].(τ_{1}, …, τ_{n}) → void Δ; Γ ⊢_{K} v_{i} : τ_{i}[σ⃗/α⃗]

Δ; Γ ⊢_{K} v[σ_{1}, …, σ_{m}](v_{1}, …, v_{n})

Δ; Γ ⊢_{K} v : int Δ; Γ ⊢_{K} e_{1} Δ; Γ ⊢_{K} e_{2}
Δ; Γ ⊢_{K} if0(v, e_{1}, e_{2})
Δ; Γ ⊢_{K} v : τ
Δ; Γ ⊢_{K} halt[τ]v

Fig. 3. Static Semantics of λ^{K}.

∀ and → types are also combined. We abbreviate ∀[ ].(τ⃗) → void as (τ⃗) → void; we abbreviate fix f[α⃗](x_{1}:τ_{1}, …, x_{n}:τ_{n}).e as λ[α⃗](x_{1}:τ_{1}, …, x_{n}:τ_{n}).e, when f does not appear free in e; and we omit empty type argument brackets in both the fix and λ forms and in applications.

In λ^{K}, unlike λ^{F}, functions do not return values, so function calls are just jumps. The function notation “→ void” is intended to suggest this fact. If control is to be returned to the caller, the caller must pass the callee a continuation function for it to invoke. Execution is completed by the construct halt[τ]v, which accepts a result value v of type τ and terminates the computation. Typically, this construct is used by the top-level continuation.

Since expressions never return values, only typing judgments for values state types. The new judgment Δ; Γ ⊢_{K} e indicates that the term e is well formed under type and value contexts Δ and Γ. Aside from these issues, the static semantics for λ^{K} is standard and appears in Figure 3.

4.1 Translation

The CPS translation that takes λ^{F} to λ^{K} is based on that of Harper and Lillibridge [1993] and appears in Figure 4. The type translation is written K[[ · ]]. The principal translation for terms, K_{exp}[[e]], takes a continuation k, computes the value of e and hands that value to k. A second term translation for full programs, K_{prog}[[e]], calls the principal translation with a special top-level continuation that

K[[α]]  =^{def} α
K[[int]]  =^{def} int
K[[τ_{1} → τ_{2}]]  =^{def} (K[[τ_{1}]], K_{cont}[[τ_{2}]]) → void
K[[∀α.τ]]  =^{def} ∀[α].(K_{cont}[[τ]]) → void
K[[⟨τ_{1}, …, τ_{n}⟩]]  =^{def} ⟨K[[τ_{1}]], …, K[[τ_{n}]]⟩
K_{cont}[[τ]]  =^{def} (K[[τ]]) → void

K_{prog}[[u^{τ}]]  =^{def} K_{exp}[[u^{τ}]](λx:K[[τ]].halt[K[[τ]]]x^{K[[τ]]})^{K_{cont}[[τ]]}
K_{exp}[[y^{τ}]]k  =^{def} k(y^{K[[τ]]})
K_{exp}[[i^{τ}]]k  =^{def} k(i^{K[[τ]]})
K_{exp}[[(fix x(x_{1}:τ_{1}):τ_{2}.e)^{τ}]]k =^{def} k((fix x(x_{1}:K[[τ_{1}]], c:K_{cont}[[τ_{2}]]).K_{exp}[[e]]c^{K_{cont}[[τ_{2}]]})^{K[[τ]]})
K_{exp}[[(u_{1}^{τ_{1}} u_{2}^{τ_{2}})^{τ}]]k =^{def} K_{exp}[[u_{1}^{τ_{1}}]](λx_{1}:K[[τ_{1}]].

K_{exp}[[u_{2}^{τ_{2}}]](λx_{2}:K[[τ_{2}]].

x_{1}^{K[[τ_{1}]]}(x_{2}^{K[[τ_{2}]]}, k))^{K_{cont}[[τ_{2}]]})^{K_{cont}[[τ_{1}]]}
K_{exp}[[(Λα.u^{τ})^{τ′}]]k  =^{def} k((λ[α](c:K_{cont}[[τ]]).K_{exp}[[u^{τ}]]c^{K_{cont}[[τ]]})^{K[[τ′]]})
K_{exp}[[(u^{τ} [σ])^{τ′}]]k  =^{def} K_{exp}[[u^{τ}]](λx:K[[τ]].x^{K[[τ]]}[K[[σ]]](k))^{K_{cont}[[τ]]}
K_{exp}[[⟨u_{1}^{τ_{1}}, …, u_{n}^{τ_{n}}⟩^{τ}]]k  =^{def} K_{exp}[[u_{1}^{τ_{1}}]](λx_{1}:K[[τ_{1}]]. · · ·

K_{exp}[[u_{n}^{τ_{n}}]](λx_{n}:K[[τ_{n}]].

k(⟨x_{1}^{K[[τ_{1}]]}, …, x_{n}^{K[[τ_{n}]]}⟩^{K[[τ]]}))^{K_{cont}[[τ_{n}]]} ⋯)^{K_{cont}[[τ_{1}]]}

K_{exp}[[π_{i}(u^{τ})^{τ′}]]k  =^{def} K_{exp}[[u^{τ}]](λx:K[[τ]]. let y = π_{i}(x) in k(y^{K[[τ′]]}))^{K_{cont}[[τ]]}
K_{exp}[[e_{1} p e_{2}^{τ}]]k  =^{def} K_{exp}[[e_{1}]](λx_{1}:int.

K_{exp}[[e_{2}]](λx_{2}:int.

let y = x_{1} p x_{2} in k(y^{int} ))^{K_{cont}[[int]]})^{K_{cont}[[int]]}
K_{exp}[[if0(e_{1}, e_{2}, e_{3})^{τ}]]k =^{def} K_{exp}[[e_{1}]](λx:int.

if0(x^{int} , K_{exp}[[e_{2}]]k, K_{exp}[[e_{3}]]k))^{K_{cont}[[int]]}

Fig. 4. Translation from λ^{F} to λ^{K}.

accepts a final answer and halts. In the translation, the variables c and x are assumed to be fresh in order to avoid variable capture.

An important property of the translation is that it translates well-formed λ^{F} expressions to well-formed λ^{K} expressions:

Lemma (CPS Conversion Type Correctness). If ∅; ∅ ⊢_{F} e : τ then ∅; ∅ ⊢_{K} K_{prog}[[e]].

In this translation, and in those that follow, no particular effort is made to optimize the resulting code. A realistic compiler based on these type systems, such as the one we discuss in Section 8, would integrate optimizations into these translations. For instance, a realistic CPS-converter would eliminate “administrative” redices and optimize tail recursion [Danvy and Filinski 1992].

The factorial example coded in λ^{K} is given below. This code would be obtained by K_{prog}[[ ·]] in conjunction with two optimizations mentioned above.

(fix f (n:int, k:(int) → void).

if0(\begin{matrix}n, k(1), \\ let x = n − 1 in \\ f(x, λ(y:int). let z = n × y in k(z)))) \end{matrix}

(6, λ(n:int). halt[int]n)

5. SIMPLIFIED POLYMORPHIC CLOSURE CONVERSION

The second compilation stage is closure conversion, which makes closures explicit, thereby separating program code from data. This is done in two steps. The first and main step, closure conversion proper, rewrites all functions so that they contain no free variables. Any variables that appear free in a function must be taken as additional arguments to that function. Those additional arguments are collected in an environment that is paired with the (now closed) code to create a closure. Function calls are performed by extracting the code and the environment from the closure, and then calling that code with the environment as an additional argument.

In the second step, hoisting, the code blocks are lifted to the top of the program, achieving the desired separation between code and data. Since those code blocks are closed, hoisting can be done without difficulty. We begin with closure conversion proper; the hoisting step is considered in Section 5.2.

Although the operational explanation of closure conversion is quite simple, there are a number of subtle issues involved in type-checking the resulting code. In the absence of polymorphic functions, our approach to typing closure conversion is based on Minamide et al. [1996], who observe that if two functions with the same type but different free variables (and therefore different environment types) were naively typed after closure conversion, the types of their closures would not be the same. To prevent this, they use existential types [Mitchell and Plotkin 1988] to abstract the types of environments, thereby hiding the fact that the closures’ environments have different types.

In the presence of polymorphism, functions may have free type variables as well as free value variables, and, just as for free value variables, closure conversion must rewrite functions to take free type variables as additional arguments. Our approach for dealing with this issue diverges from that of Minamide et al., who desire a typepassing interpretation of polymorphism in which types are constructed and passed as data at run time. In such a type-passing interpretation, those additional type arguments must be collected in a type environment, which is the type-level equivalent of the value environment discussed earlier. Type environments necessitate two complex mechanisms: abstract kinds, to hide the differences between type environments, and translucent types, to ensure that code blocks are called with the correct type environments.

We propose a considerably simpler approach to polymorphic closure conversion. To avoid the complexities of type environments, we adopt a type-erasure interpretation of polymorphism as in The Definition of Standard ML [Milner et al. 1997]. In a type-erasure interpretation, we need not save the contents of free type variables in a type environment; instead, we substitute them directly into code blocks. Semantically, this amounts to making copies of code blocks in which the relevant

Additional syntactic constructs:

types  τ, σ ::= … | ∃α.τ
values  u ::= … | v[τ] | pack [τ_{1}, v] as τ_{2}
declarations  d ::= … | [α, x] = unpack v
terms  e ::= … | replace v[τ⃗](v⃗) by v(v⃗)

The typing rule for fix x[α⃗](x_{1}:τ_{1}, …, x_{n}:τ_{n}).e is replaced by:

α⃗ ⊢_{C} τ_{i} α⃗; x:∀[α⃗](τ_{1}, …, τ_{n}) → void, x_{1}:τ_{1}, …, x_{n}:τ_{n} ⊢_{C} e
Δ; Γ ⊢_{C} fix x[α⃗](x_{n}:τ_{1}, …, x_{n}:τ_{n}).e : ∀[α⃗](τ_{1}, …, τ_{n}) → void

The typing rule for v[τ⃗](v⃗) is replaced by:

Δ; Γ ⊢_{C} v : (τ_{1}, …, τ_{n}) → void Δ; Γ ⊢_{C} v_{i} : τ_{i}
Δ; Γ ⊢_{C} v(v_{1}, …, v_{n})

Additional typing rules:

Δ ⊢_{C} σ Δ; Γ ⊢_{C} v : ∀[α, β⃗].(τ⃗) → void
Δ; Γ ⊢_{C} v[σ] : (∀[β⃗].(τ⃗[σ/α]) → void)

Δ ⊢_{C} τ_{1} Δ; Γ ⊢_{C} v : τ_{2}[τ_{1}/α]
Δ; Γ ⊢_{C} pack [τ_{1}, v] as ∃α.τ_{2} : ∃α.τ_{2}
Δ; Γ ⊢_{C} v : ∃α.τ (Δ, α); (Γ, x:τ) ⊢_{C} e
Δ; Γ ⊢_{C} let [α, x] = unpack v in e

(α ∉ Δ ∧ x ∉ Γ)

Shorthand:

v[ ] =^{def} v
u^{∀[α,β⃗].(σ⃗)→void} [τ, τ⃗] =^{def} (u^{∀[α,β⃗].(σ⃗)→void} [τ])^{∀[β⃗].(σ⃗[τ/α])→void} [τ⃗]
Fig. 5. Changes from λ^{K} to λ^{C}.

substitutions have been performed. However, as types will ultimately be erased, these “copies” are represented by the same term at run time, resulting in no runtime cost.

Formally this means, that, in a type-erasure interpretation, we consider the partial application of a function to a type argument to be a value. For example, suppose v has the type ∀[α⃗, β⃗].(τ⃗) → void where the type variables α⃗ stand for the function’s free type variables, and the type variables β⃗ are the function’s ordinary type arguments. If σ⃗ are the contents of those free type variables, then the partial instantiation v[σ⃗] is considered a value and has type ∀[β⃗].(τ⃗[σ⃗/α⃗]) → void. This instantiation takes the place of the construction of a type environment.

The work of Minamide et al. arose from the TIL compiler [Morrisett et al. 1996], which uses run-time type information to optimize data layout [Tarditi et al. 1996]. At first, it seems that a type-erasure semantics precludes these optimizations. However, recent work of Crary et al. [1998; 1999] shows how to encode run-time type information in a type-erasure language. Rather than manipulating types directly, programs manipulate values that represent types. Using this device, the type environment can become part of the value environment, and closure conversion may be performed in a similar fashion as described here. These mechanisms can be added to TAL, and the optimizations above can be used in a compiler targeting it.

Figure 5 presents the differences between λ^{C} and λ^{K}. The principal difference is that the body of a function must type check in a context containing only its formal arguments. In other words, code blocks must be closed, as desired. As discussed above, we also make type instantiation a value form. Finally, we add existential types [Mitchell and Plotkin 1988] to support the typing of closures. Note that in a type-erasure interpretation, the type portion of an existential package (all but v of pack [τ, v] as ∃α.τ′) is erased at run time, and hence the creation of such a package has no run-time cost.

5.1 Translation

The closure conversion algorithm is formalized in Figure 6. The translation for types is denoted by C[[ ·]], the only interesting rule of which is the one for function types:

C[[∀[α⃗].(τ_{1}, …, τ_{n}) → void]] = ∃β.⟨∀[α⃗].(β, C[[τ_{1}]], …, C[[τ_{n}]]) → void, β⟩

The existentially quantified variable β represents the type of the value environment for the closure. The closure itself is a pair consisting of a piece of code instantiated with types for its free type variables, and a value environment. The instantiated code takes as arguments its original type and value arguments, as well as the value environment. Closures are invoked by extracting the code and environment from the closure and then applying the code to the environment and the function’s arguments.

The term translation has three parts: one for terms, C_{exp}[[·]], one for declarations, C_{dec}[[ ·]], and one for values, C_{val}[[ ·]]. For uniformity with other translations, we also provide a whole program translation (C_{prog}[[ ·]]), which in this case simply invokes the term translation. To avoid variable capture, the variables z and γ are assumed to be fresh.

Again, we may show that the translation preserves well-formedness of programs:

Lemma (Closure Conversion Type Correctness). If ∅; ∅ ⊢_{K} e then ∅; ∅ ⊢_{C} C_{prog}[[e]].

5.2 Hoisting

After closure conversion, all functions are closed and may be hoisted out to the top level without difficulty. In a real compiler, these two phases would be combined, but we have separated them here for simplicity. The target of the hoisting translation is the calculus λ^{H}, in which fix is no longer a value form. Instead, code blocks are defined by a letrec prefix, which we call a heap in anticipation of the heaps of λ^{A} and TAL. This change is made precise in Figure 7.

Programs are translated from λ^{C} to λ^{H} by replacing all fix expressions with fresh variables and binding those variables to the corresponding code expressions in the heap. This translation, denoted by H_{prog}[[ ·]], is straightforward to formalize, so we omit the formalization in the interest of brevity.

Lemma (Hoisting Type Correctness). If ∅; ∅ ⊢_{C} e then ⊢_{H} H_{prog}[[e]].

Some examples of closure conversion and hoisting appear in Figures 8 and 9. Figure 8 gives the factorial example after closure conversion, hoisting, and few simplifying optimizations (beta reduction and copy propagation). To illustrate polymorphic closure conversion we consider another example in Figure 9, the polymorphic, higher-order function twice that takes a function and composes it with itself. The twice function contains two nested functions, twicef and oncef , each

C[[α]]  =^{def} α
C[[int]]  =^{def} int
C[[∀[α⃗].(τ_{1}, …, τ_{n}) → void]]  =^{def} ∃β.⟨∀[α⃗].(β, C[[τ_{1}]], …, C[[τ_{n}]]) → void, β⟩
C[[⟨τ_{1}, …, τ_{n}⟩]]  =^{def} ⟨C[[τ_{1}]], …, C[[τ_{n}]]⟩
C_{prog}[[e]]  =^{def} C_{exp}[[e]]
C_{exp}[[let d in e]]  =^{def} let C_{dec}[[d]] in C_{exp}[[e]]
C_{exp}[[u^{τ} [σ_{1}, …, σ_{m}](v_{1}, …, v_{n})]] =^{def} let [γ, z] = unpack C_{val}[[u^{τ}]] in

let z_{code} = π_{1}(z^{⟨τ_{code} ,γ⟩}) in let z_{env} = π_{2}(z^{⟨τ_{code} ,γ⟩}) in (z_{code}^{τ_{code}} [C[[σ_{1}]], …, C[[σ_{m}]]])

(z_{env}^{γ}, C_{val}[[v_{1}]], …, C_{val}[[v_{n}]]) where C[[τ]] = ∃γ.⟨τ_{code}, γ⟩

C_{exp}[[if0(v, e_{1}, e_{2})]]  =^{def} if0(C_{val}[[v]], C_{exp}[[e_{1}]], C_{exp}[[e_{2}]])
C_{exp}[[halt[τ]v]]  =^{def} halt[C[[τ]]]C_{val}[[v]]
C_{dec}[[x = v]]  =^{def} x = C_{val}[[v]]
C_{dec}[[x = π_{i}(v)]]  =^{def} x = π_{i}(C_{val}[[v]])
C_{dec}[[x = v_{1} p v_{2}]]  =^{def} x = C_{val}[[v_{1}]] p C_{val}[[v_{2}]]
C_{val}[[x^{τ}]]  =^{def} x^{C[[τ]]}
C_{val}[[i^{τ}]]  =^{def} i^{C[[τ]]}
C_{val}[[⟨v_{1}, …, v_{n}⟩^{τ}]]  =^{def} ⟨C_{val}[[v_{1}]], …, C_{val}[[v_{n}]]⟩^{C[[τ]]}
C_{val}[[(fix x[α⃗](x_{1}:τ_{1}, …, x_{n}:τ_{n}).e)^{τ}]] =^{def} (pack [τ_{env} , ⟨v_{code}[β⃗], v_{env} ⟩^{⟨τ_{code} ,τ_{env} ⟩}] as C[[τ]])^{C[[τ]]}

where y_{1}^{σ_{1}}, …, y_{m}^{σ_{m}} = FV (fix x[α⃗](x_{1}:τ_{1}, …, x_{n}:τ_{n}).e)

β⃗  = FTV (fix x[α⃗](x_{1}:τ_{1}, …, x_{n}:τ_{n}).e)
τ_{env}  = C[[⟨σ_{1}, …, σ_{m}⟩]]
τ_{rawcode}  = ∀[β⃗, α⃗].(τ_{env} , C[[τ_{1}]], …, C[[τ_{n}]]) → void
τ_{code}  = ∀[α⃗].(τ_{env} , C[[τ_{1}]], …, C[[τ_{n}]]) → void
v_{code}  = (fix z_{code}[β⃗, α⃗](z_{env} :τ_{env} , x_{1}:C[[τ_{1}]], …, x_{n}:C[[τ_{n}]]).
let x = pack [τ_{env} , ⟨z_{code}^{τ_{rawcode}} [β⃗], z_{env}^{τ_{env}} ⟩^{⟨τ_{code} ,τ_{env} ⟩}] as C[[τ]] in
let y_{1} = π_{1}(z_{env}^{τ_{env}} ) in
⋮
let y_{m} = π_{m}(z_{env}^{τ_{env}} ) in C_{exp}[[e]])^{τ_{rawcode}}
v_{env}  = ⟨y_{1}^{C[[σ_{1}]]}, …, y_{m}^{C[[σ_{m}]]}⟩^{τ_{env}}

Fig. 6. Translation from λ^{K} to λ^{C}.

of which contains the free type variable α, and therefore, after closure conversion, α becomes part of the type environment for these functions. Consequently, the type argument to twice_{code} is an ordinary type argument, but the type arguments to the code blocks twicef_{code} and oncef_{code} stand for free type variables and are instantiated appropriately whenever closures are formed from those code blocks.

Syntax changes:

values  u ::= delete fix x(x_{1}:τ_{1}, …, x_{n}:τ_{n}).e
heap values  h ::= code[α⃗](x_{1}:τ_{1}, …, x_{n}:τ_{n}).e
programs  P ::= letrec x_{1} ↦ h_{1}, …, x_{n} ↦ h_{n} in e

The typing rule for fix is replaced by a heap value rule for code:

α⃗ ⊢_{H} τ_{i} α⃗; (Γ, x_{1}:τ_{1}, …, x_{n}:τ_{n}) ⊢_{H} e

Γ ⊢_{H} code[α⃗](x_{1}:τ_{1}, …, x_{n}:τ_{n}).e : ∀[α⃗](τ_{1}, …, τ_{n}) → void hval (x_{1}, …, x_{n} ∉ Γ)

New typing rule:

∅ ⊢_{H} τ_{i} x_{1}:τ_{1}, …, x_{n} : τ_{n} ⊢_{H} h_{i} : τ_{i} hval ∅; x_{1}:τ_{1}, …, x_{n} : τ_{n} ⊢_{H} e

⊢_{H} letrec x_{1} ↦ h_{1}, …, x_{n} ↦ h_{n} in e (x_{i} ≠ x_{j} for i ≠ j)

Fig. 7. Changes from λ^{C} to λ^{H}.

letrec f_{code} ↦ (* main factorial code block *)

code[ ](env:⟨⟩, n:int, k:τ_{k}).

if0(n, (* true branch: continue with 1 *) let [β, k_{unpack} ] = unpack k in let k_{code} = π_{1}(k_{unpack} ) in let k_{env} = π_{2}(k_{unpack} ) in k_{code}(k_{env} , 1), (* false branch: recurse with n − 1 *) let x = n − 1 in f_{code}(env, x, pack [⟨int, τ_{k}⟩, ⟨cont_{code}, ⟨n, k⟩⟩] as τ_{k}))

cont_{code} ↦ (* code block for continuation after factorial computation *)

code[ ](env:⟨int, τ_{k}⟩, y:int). (* open the environment *) let n = π_{1}(env) in let k = π_{2}(env) in (* compute n! into z *) let z = n × y in (* continue with z *) let [β, k_{unpack} ] = unpack k in let k_{code} = π_{1}(k_{unpack} ) in let k_{env} = π_{2}(k_{unpack} ) in k_{code}(k_{env} , z)

halt_{code} ↦ (* code block for top-level continuation *)

code[ ](env:⟨⟩, n:int). halt[int]n

in f_{code}(⟨⟩, 6, pack [⟨⟩, ⟨halt_{code}, ⟨⟩⟩] as τ_{k})

where τ_{k} is ∃α.⟨(α, int) → void, α⟩

Fig. 8. Factorial in λ^{H}.

6. EXPLICIT ALLOCATION

The λ^{H} intermediate language still has an atomic constructor for forming tuples, but machines must allocate space for a tuple and fill it out field by field; the allocation stage makes this process explicit. To do so, we eliminate the value form for tuples, and introduce new declaration forms for allocating and initializing tuples, as shown in Figure 10. The creation of an n-element tuple becomes a computation that is separated into an allocation step and n initialization steps. For example, if v_{1} and

λ^{F} source:

twice = Λα. λf:α → α. λx:α. f(fx)

λ^{K} source:

twice =

λ[α](f :τ_{f} , c:(τ_{f}) → void).

let twicef =

λ(x:α, c′:(α) → void).

let oncef = λ(z:α). f (z, c′) in f (x, oncef )

in c[ ](twicef )

where τ_{f} = (α, (α) → void) → void

λ^{H} translation:

letrec twice_{code}[α](env:⟨⟩, f :τ_{f} , c:∃ρ_{3}.⟨(ρ_{3}, τ_{f}) → void, ρ_{3}⟩). let twicef = pack [⟨τ_{f} ⟩, ⟨twicef_{code}[α], ⟨f ⟩⟩] as τ_{f} in (* create closure *) let [ρ_{3}, c_{unpack} ] = unpack c in let c_{code} = π_{1}(c_{unpack} ) in let c_{env} = π_{2}(c_{unpack} ) in

c_{code}(c_{env} , twicef ) twicef_{code}[α](env:⟨τ_{f} ⟩, x:α, c′:τ_{αc}).

let f = π_{1}(env) in let oncef = pack [⟨τ_{f} , τ_{αc}⟩, ⟨oncef_{code}[α], ⟨f , c′⟩⟩] as τ_{αc} in (* create closure *) let [ρ_{1}, f_{unpack} ] = unpack f in let f_{code} = π_{1}(f_{unpack} ) in let f_{env} = π_{2}(f_{unpack} ) in

f_{code}(f_{env} , x, oncef ) oncef_{code}[α](env : ⟨τ_{f} , τ_{αc}⟩, z : α).

let f = π_{1}(env) in let c′ = π_{2}(env) in let [ρ_{1}, f_{unpack} ] = unpack f in let f_{code} = π_{1}(f_{unpack} ) in let f_{env} = π_{2}(f_{unpack} ) in

f_{code}(f_{env} , z, c′)

in ⋯

where τ_{f} = ∃ρ_{1}.⟨(ρ_{1}, α, τ_{αc}) → void, ρ_{1}⟩ τ_{αc} = ∃ρ_{2}.⟨(ρ_{2}, α) → void, ρ_{2}⟩

Fig. 9. Polymorphic example.

v_{2} are integers, the pair ⟨v_{1}, v_{2}⟩ is created as follows (where types have been added for clarity):

let x_{1}:⟨int^{0}, int^{0}⟩ = malloc[int, int]
x_{2}:⟨int^{1}, int^{0}⟩ = x_{1}[1] ← v_{1}
x :⟨int^{1}, int^{1}⟩ = x_{2}[2] ← v_{2}
⋮

The “x_{1} = malloc[int, int]” step allocates an uninitialized tuple and binds x_{1} to the address of the tuple. The “0” superscripts on the types of the fields indicate that the fields are uninitialized, and hence no projection may be performed on those

Syntax changes:

types  τ, σ ::= … | replace ⟨τ⃗⟩ by ⟨τ_{1}^{φ_{1}}, …, τ_{n}^{φ_{n}}⟩
initialization flags  φ ::= 1 | 0
values  u ::= delete ⟨v⃗⟩
declarations  d ::= … | x = malloc[τ⃗] | x = v_{1}[i] ← v_{2}
heap values  h ::= … | ⟨v⃗⟩

The typing rule for projection is replaced by:

Δ; Γ ⊢_{A} v : ⟨τ_{1}^{φ_{1}}, …, τ_{n}^{φ_{n}}⟩ Δ; Γ, x:τ_{i} ⊢_{A} e
Δ; Γ ⊢_{A} let x = π_{i}(v) in e

(x ∉ Γ ∧ 1 ≤ i ≤ n ∧ φ_{i} = 1)

The typing rule for tuples is replaced by a heap value rule:

{∅; Γ ⊢ v_{i} : τ_{i} \atop Γ ⊢_{A} ⟨v_{1}, …, v_{n}⟩ : ⟨τ_{1}^{1}, …, τ_{n}^{1}⟩ hval}

New typing rules:

Δ ⊢_{A} τ_{i} Δ; Γ, x:⟨τ_{1}^{0}, …, τ_{n}^{0}⟩ ⊢_{A} e

Δ; Γ ⊢_{A} let x = malloc[τ_{1}, …, τ_{n}] in e

(x ∉ Γ)
Δ; Γ ⊢_{A} v_{1} : ⟨τ_{1}^{φ_{1}}, …, τ_{n}^{φ_{n}}⟩ Δ; Γ ⊢_{A} v_{2} : τ_{i}
Δ; Γ, x:⟨τ_{1}^{φ_{1}}, …, τ_{i−1}^{φ_{i−1}}, τ_{i}^{1}, τ_{i+1}^{φ_{i+1}}, …, τ_{n}^{φ_{n}}⟩ ⊢_{A} e

Δ; Γ ⊢_{A} let x = v_{1}[i] ← v_{2} in e (x ∉ Γ ∧ 1 ≤ i ≤ n)

Shorthand:

let ϵ in e =^{def} e
let d, d⃗ in e =^{def} let d in let d⃗ in e
Fig. 10. Changes from λ^{H} to λ^{A}.

fields. The “x_{2} = x_{1}[1] ← v_{1}” step updates the first field of the tuple with the value v_{1} and binds x_{2} to the address of the tuple. Note that x_{2} is assigned a type where the first field has a “1” superscript, indicating that this field is initialized. Finally, the “x = x_{2}[2] ← v_{2}” step initializes the second field of the tuple with v_{2} and binds x to the address of the tuple, which is assigned the fully initialized type ⟨int^{1}, int^{1}⟩. Hence, both π_{1} and π_{2} are allowed on x.

The code sequence above need not be atomic; it may be rearranged or interleaved with projections in any well-typed manner. The initialization flags on the types ensure that a field cannot be projected unless it has been initialized. Moreover, a syntactic value restriction ensures there is no unsoundness in the presence of polymorphism. Operationally, the declaration x[i] ← v is interpreted as an imperative operation, and thus at the end of the sequence, x_{1}, x_{2}, and x are all aliases for the same location, even though they have different types. Consequently, the initialization flags do not prevent a field from being initialized twice. It is possible to use monads [Wadler 1990a; Launchbury and Peyton Jones 1995] or linear types [Girard 1987; Wadler 1990b; 1993] to ensure that a tuple is initialized exactly once, but we have avoided these approaches in the interest of a simpler type system. The presence of uninitialized values also raises some garbage collection issues; in Section 8 we discuss how our implementation deals with these issues.

6.1 Translation

The translation of types from λ^{H} to λ^{A} is simple; it amounts to adding initialization flags to each field of tuple types:

A[[⟨τ_{1}, …, τ_{n}⟩]] =^{def} ⟨A[[τ_{1}]]^{1}, …, A[[τ_{n}]]^{1}⟩

The term translation is formalized in Figure 11 as five translations: full programs (A_{prog}[[·]]), heap values (A_{hval}[[·]]), expressions (A_{exp}[[·]]), declarations (A_{dec}[[·]]), and values (A_{val}[[ ·]]). The focus of the translation is on the last rule, which generalizes the informal translation of tuples given in the previous section. This rule returns the sequence of declarations to allocate and initialize a tuple. Although the other (nontuple) values are more or less unchanged by the translation, they too must return sequences of declarations needed to construct those values. Such sequences will be empty unless the value in question contains a tuple. Similarly, the declaration translation produces a sequence of declarations. To avoid variable capture, the variable y is assumed to be fresh.

Lemma (Allocation Type Correctness). If ⊢_{H} P then ⊢_{A} A_{prog}[[P]].

The factorial example after application of the explicit allocation translation appears in Figure 12.

7. TYPED ASSEMBLY LANGUAGE

The final compilation stage, code generation, converts λ^{A} to TAL. All of the major typing constructs in TAL are present in λ^{A} and, indeed, code generation is largely syntactic. To summarize the type structure at this point, there is a combined abstraction mechanism that may simultaneously abstract a type environment, a set of type arguments, and a set of value arguments. Values of these types may be partially applied to type environments and remain values. There are existential types to support closures and other data abstractions. Finally, there are n-tuples with flags on the fields indicating whether the field has been initialized.

A key technical distinction between λ^{A} and TAL is that λ^{A} uses alpha-varying variables, whereas TAL uses register names, which, like labels on records, do not alpha-vary.^{1} We assume an infinite supply of registers. Mapping to a language with a finite number of registers may be performed by spilling registers into a tuple and reloading values from this tuple when necessary.

One of the consequences of this aspect of TAL is that a calling convention must be used in code generation, and calling conventions must be made explicit in the types. Hence TAL includes the type ∀[α⃗].{r1:τ_{1}, …, rn:τ_{n}}, which is used to describe entry points of code blocks and is the TAL analog of the λ^{A} function type, ∀[α⃗].(τ_{1}, …, τ_{n}) → void. The key difference is that we assign fixed registers to the arguments of the code. Intuitively, to jump to a block of code of this type, the type variables α⃗ must be suitably instantiated, and registers r1 through rn must contain values of type τ_{1} through τ_{n}, respectively.

Another distinction between λ^{A} and TAL is, that, while λ^{A} has one mechanism (variables) for identifying values, TAL follows real machines and distinguishes between labels (which may be thought of as pointers) and registers. Registers may

1Indeed, the register file may be viewed as a record, and register names as field labels.

A[[α]]  =^{def} α
A[[int]]  =^{def} int
A[[∀[α⃗].(τ_{1}, …, τ_{n}) → void]] =^{def} ∀[α⃗].(A[[τ_{1}]], …, A[[τ_{n}]]) → void
A[[⟨τ_{1}, …, τ_{n}⟩]]  =^{def} ⟨A[[τ_{1}]]^{1}, …, A[[τ_{n}]]^{1}⟩
A[[∃α.τ]]  =^{def} ∃α.A[[τ]]

A_{prog}[[letrec x_{1} ↦ h_{1}, …, x_{n} ↦ h_{n} in e]]

=^{def} letrec x_{1} ↦ A_{hval}[[h_{1}]], …, x_{n} ↦ A_{hval}[[h_{n}]] in A_{exp}[[e]]

A_{hval}[[code[α⃗](x_{1}:τ_{1}, …, x_{n}:τ_{n}).e]]

=^{def} code[α⃗](x_{1}:A[[τ_{1}]], …, x_{n}:A[[τ_{n}]]).A_{exp}[[e]]
A_{exp}[[let d in e]]  =^{def} let A_{dec}[[d]] in A_{exp}[[e]]
A_{exp}[[v(v_{1}, …, v_{n})]]  =^{def} let d⃗, d⃗_{1}, …, d⃗_{n} in v′(v′_{1}, …, v′_{n})
where ⟨d⃗, v′⟩ = A_{val}[[v]] and ⟨d⃗_{i}, v′_{i}⟩ = A_{val}[[v_{i}]]
A_{exp}[[if0(v, e_{1}, e_{2})]] =^{def} let d⃗ in if0(v′, A_{exp}[[e_{1}]], A_{exp}[[e_{2}]])
where ⟨d⃗, v′⟩ = A_{val}[[v]]
A_{exp}[[halt[τ]v]] =^{def} let d⃗ in halt[A[[τ]]]v′
where ⟨d⃗, v′⟩ = A_{val}[[v]]
A_{dec}[[x = v]]  =^{def} d⃗, x = v′  where ⟨d⃗ , v′⟩ = A_{val}[[v]]
A_{dec}[[x = π_{i}(v)]]  =^{def} d⃗, x = π_{i}(v′)  where ⟨d⃗, v′⟩ = A_{val}[[v]]
A_{dec}[[x = v_{1} p v_{2}]]  =^{def} d⃗_{1}, d⃗_{2}, x = v′_{1} p v′_{2}  where ⟨d⃗_{i}, v′_{i}⟩ = A_{val}[[v_{i}]]
A_{dec}[[[α, x] = unpack v]]  =^{def} d⃗, [α, x] = unpack v′ where ⟨d⃗, v′⟩ = A_{val}[[v]]
A_{val}[[x^{τ}]]  =^{def} ⟨ϵ, x^{A[[τ]]}⟩
A_{val}[[i^{τ}]]  =^{def} ⟨ϵ, i^{A[[τ]]}⟩
A_{val}[[(v[σ])^{τ}]]  =^{def} ⟨d⃗, (v′[A[[σ]]])^{A[[τ]]}⟩  where ⟨d⃗, v′⟩ = A_{val}[[v]]
A_{val}[[(pack [τ, v] as τ′)^{τ′′}]] =^{def} ⟨d⃗, (pack [A[[τ]], v′] as A[[τ′]])^{A[[τ′′]]}⟩
where ⟨d⃗, v′⟩ = A_{val}[[v]]
A_{val}[[⟨u_{1}^{τ_{1}}, …, u_{n}^{τ_{n}}⟩^{τ}]]  =^{def} ⟨(d⃗_{1}, …, d⃗_{n}, y_{0} = malloc[A[[τ_{1}]], …, A[[τ_{n}]]], ; y_{1} = y_{0}^{(τ^{(0)})}[1] ← v′_{1}, ; ⋮ ; y_{n} = y_{n−1}^{(τ^{(n−1)})}[n] ← v′_{n}),

y_{n}^{A[[τ]]}⟩
where ⟨d⃗_{i}, v′_{i}⟩ = A_{val}[[u_{i}^{τ_{i}}]]

and τ^{(i)} = ⟨A[[τ_{1}]]^{1}, …, A[[τ_{i}]]^{1}, A[[τ_{i+1}]]^{0}, …, A[[τ_{n}]]^{0}⟩

Fig. 11. Translation from λ^{H} to λ^{A}.

contain only word values, which are integers or pointers. As in λ^{A}, tuples and code blocks are large values and must be heap allocated. Heap objects are identified by labels, which may reside in registers. In this manner, TAL makes the layout of data in memory explicit.

In the remainder of this section, we present the syntax of TAL (Section 7.1), its dynamic semantics (Section 7.2), and its full static semantics (Section 7.3). Finally,

letrec f_{code} ↦ (* main factorial code block *)

code[ ](env:⟨⟩, n:int, k:τ_{k}).

if0(n, (* true branch: continue with 1 *) let [β, k_{unpack} ] = unpack k in let k_{code} = π_{1}(k_{unpack} ) in let k_{env} = π_{2}(k_{unpack} ) in k_{code}(k_{env} , 1), (* false branch: recurse with n − 1 *) let x = n − 1 in let y_{1} = malloc[int, τ_{k}] in let y_{2} = y_{1}[1] ← n in let y_{3} = y_{2}[2] ← k in (* ⟨n, k⟩ *) let y_{4} = malloc[(⟨int, τ_{k}⟩, int) → void, ⟨int, τ_{k}⟩] in let y_{5} = y_{4}[1] ← cont_{code} in let y_{6} = y_{5}[2] ← y_{3} in (* ⟨cont_{code}, ⟨n, k⟩⟩ *) f_{code}(env, x, pack [⟨int, τ_{k}⟩, y_{6}] as τ_{k}))

cont_{code} ↦ (* code block for continuation after factorial computation *)

code[ ](env:⟨int, τ_{k}⟩, y:int). (* open the environment *) let n = π_{1}(env) in let k = π_{2}(env) in (* continue with n × y *) let z = n × y in let [β, k_{unpack} ] = unpack k in let k_{code} = π_{1}(k_{unpack} ) in let k_{env} = π_{2}(k_{unpack} ) in k_{code}(k_{env} , z)

halt_{code} ↦ (* code block for top-level continuation *)

code[ ](env:⟨⟩, n:int). halt[int]n

in let y_{7} = malloc[ ] in (* ⟨⟩ *) let y_{8} = malloc[ ] in (* ⟨⟩ *) let y_{9} = malloc[(⟨⟩, int) → void, ⟨⟩] in let y_{10} = y_{9}[1] ← halt_{code} in let y_{11} = y_{10}[2] ← y_{8} in (* ⟨halt_{code}, ⟨⟩⟩ *) f_{code}(y_{7}, 6, pack [⟨⟩, y_{11}] as τ_{k})

where τ_{k} is ∃α.⟨(α, int) → void, α⟩

Fig. 12. Factorial in λ^{A}.

we present the translation from λ^{A} to TAL (Section 7.4).

7.1 TAL Syntax

We present the syntax of TAL in Figure 13. A TAL abstract machine state, or program, is a triple consisting of a heap (H), a register file (R), and a sequence of instructions (I). The heap is a mapping of labels (ℓ) to heap values (tuples and code blocks). The register file is a mapping of registers (r) to word values. Heaps, register files, and their respective types are not syntactically correct if they repeat labels or registers. When r appears in R, the notation R{r ↦ w} represents the register file R with the r binding replaced with w; if r does not appear in R, the indicated binding is simply added to R. Similar notation is used for heaps, register file types, and heap types.

types  τ, σ ::= α | int | ∀[α⃗].Γ | ⟨τ_{1}^{φ_{1}}, …, τ_{n}^{φ_{n}}⟩ | ∃α.τ
initialization flags  φ ::= 0 | 1
heap types  Ψ ::= {ℓ_{1}:τ_{1}, …, ℓ_{n}:τ_{n}}
register file types  Γ ::= {r_{1}:τ_{1}, …, r_{n}:τ_{n}}
type contexts  Δ ::= α_{1}, …, α_{n}
registers  r ::= r1 | r2 | r3 | ⋯
word values  w ::= ℓ | i | ?τ | w[τ] | pack [τ, w] as τ′
small values  v ::= r | w | v[τ] | pack [τ, v] as τ′
heap values  h ::= ⟨w_{1}, …, w_{n}⟩ | code[α⃗]Γ.I
heaps  H ::= {ℓ_{1} ↦ h_{1}, …, ℓ_{n} ↦ h_{n}}
register files  R ::= {r_{1} ↦ w_{1}, …, r_{n} ↦ w_{n}}
instructions  ι ::= add r_{d}, r_{s}, v | bnz r, v | ld r_{d}, r_{s}[i] | malloc r_{d}[τ⃗] | mov r_{d}, v |

mul r_{d}, r_{s}, v | st r_{d}[i], r_{s} | sub r_{d}, r_{s}, v | unpack[α, r_{d}], v

instruction sequences  I ::= ι; I | jmp v | halt[τ]
programs  P ::= (H, R, I)

Fig. 13. Syntax of TAL.

Although heap values are not word values, the labels that point to them are. The other word values are integers, instantiations of word values, existential packages, and junk values (?τ), which are used by the operational semantics to represent uninitialized data. A small value is either a word value, a register, or an instantiated or packed small value. We draw a distinction between word values and small values because a register must contain a word, not another register. Code blocks are linear sequences of instructions that abstract a set of type variables and state their register assumptions. The sequence of instructions is always terminated by a jmp or halt instruction. Expressions that differ only by alpha-variation of bound type variables are considered identical, as are expressions that differ only in the order of the fields in a heap, a register file, or a heap or register file type.

7.2 TAL Operational Semantics

The operational semantics of TAL is presented in Figure 14 as a deterministic rewriting system P ⟼ P′ that maps programs to programs. Although, as discussed above, we ultimately intend a type-erasure interpretation, we do not erase the types from the operational semantics presented here, so that we may more easily state and prove a subject reduction theorem (Lemma 1). If we erase the types from the instructions, then their meaning is intuitively clear, and there is a one-to-one correspondence with conventional assembly language instructions. The two exceptions to this are the unpack and malloc instructions, which are discussed below.

Intuitively, the ld r_{d}, r_{s}[i] instruction loads the ith component (counting from 0) of the tuple bound to the label in r_{s}, and places this word value in r_{d}. Conversely, st r_{d}[i], r_{s} places the word value in r_{s} into the ith position of the tuple bound to the label in r_{d}. The instruction jmp v, where v is a value of the form ℓ[τ⃗], transfers control to the code bound to the label ℓ, instantiating the abstracted type variables of that code with τ⃗. The bnz r, v instruction tests the value in r to see if it is zero. If so, control continues with the next instruction; otherwise control is transferred

(H, R, I) ⟼ P where

if I =  then P =
add r_{d}, r_{s}, v; I′  (H, R{r_{d} ↦ R(r_{s}) + R̂(v)}, I′)

and similarly for mul and sub

bnz r, v; I′  (H, R, I′)
when R(r) = 0
bnz r, v; I′  (H, R, I′′[τ⃗/α⃗])
when R(r) = i and i ≠ 0 where R̂(v) = ℓ[τ⃗] and H(ℓ) = code[α⃗]Γ.I′′
jmp v  (H, R, I′[τ⃗/α⃗])

where R̂(v) = ℓ[τ⃗] and H(ℓ) = code[α⃗]Γ.I′

ld r_{d}, r_{s}[i]; I′  (H, R{r_{d} ↦ w_{i}}, I′)

where R(r_{s}) = ℓ and H(ℓ) = ⟨w_{0}, …, w_{n−1}⟩ with 0 ≤ i < n

malloc r_{d}[τ_{1}, …, τ_{n}]; I′  (H{ℓ ↦ ⟨?τ_{1}, …,?τ_{n}⟩}, R{r_{d} ↦ ℓ}, I′)

where ℓ ∉ H

mov r_{d}, v; I′  (H, R{r_{d} ↦ R̂(v)}, I′)
st r_{d}[i], r_{s}; I′  (H{ℓ ↦ ⟨w_{0}, …, w_{i−1}, R(r_{s}), w_{i+1}, …, w_{n−1}⟩}, R, I′)

where R(r_{d}) = ℓ and H(ℓ) = ⟨w_{0}, …, w_{n−1}⟩ with 0 ≤ i < n

unpack[α, r_{d}], v; I′  (H, R{r_{d} ↦ w}, I′[τ/α])

where R̂(v) = pack [τ, w] as τ′

Where R̂(v) = \{ \begin{matrix}R(r) & when v = r \\ w & when v = w \\ R̂(v′)[τ] & when v = v′[τ] \\ pack [τ, R̂(v′)] as τ′ & when v = pack [τ, v′] as τ′ \end{matrix}

Fig. 14. Operational semantics of TAL.

to v as with the jmp instruction.

The instruction unpack[α, r_{d}], v, where v is a value of the form pack [τ′, v′] as τ, is evaluated by substituting τ′ for α in the remainder of the sequence of instructions currently being executed and by binding the register r_{d} to the value v′. If types are erased, the unpack instruction reduces to a simple mov instruction.

As in λ^{A}, malloc r_{d}[τ_{1}, …, τ_{n}] allocates a fresh, uninitialized tuple in the heap and binds r_{d} to the address of this tuple. Of course, real machines do not provide a primitive malloc instruction. Our intention is, that, when types are erased, malloc is expanded into a fixed instruction sequence that allocates a tuple of the appropriate size. Because this instruction sequence is abstract, it prevents optimization from reordering and interleaving these underlying instructions with the surrounding TAL code. However, this is the only instruction sequence that is abstract in TAL.

7.3 TAL Static Semantics

The purpose of the static semantics is to specify when programs are well formed and to ensure that well-formed programs do not get stuck. As programs are closed and self-contained, this is expressed by the judgment ⊢_{TAL} P. The well-formedness of a program is defined by the well-formedness of its three components: the heap, the register file, and the instruction stream. Consequently, formation judgments are required for heaps, register files, and instruction sequences, which in turn require judgments for the various sorts of values and types. The static semantics for TAL appears in Figures 16–18 and consists of 13 judgments, summarized in Figure 15

Judgment  Meaning
Δ ⊢_{TAL} τ  τ is a well-formed type
⊢_{TAL} Ψ  Ψ is a well-formed heap type
Δ ⊢_{TAL} Γ  Γ is a well-formed register file type
Δ ⊢_{TAL} τ_{1} ≤ τ_{2}  τ_{1} is a subtype of τ_{2}
Δ ⊢_{TAL} Γ_{1} ≤ Γ_{2}  Γ_{1} is register file subtype of Γ_{2}
⊢_{TAL} H : Ψ  H is a well-formed heap of heap type Ψ
Ψ ⊢_{TAL} R : Γ  R is a well-formed register file of register file type Γ
Ψ ⊢_{TAL} h : τ hval  h is a well-formed heap value of type τ
Ψ; Δ ⊢_{TAL} w : τ wval w is a well-formed word value of type τ
Ψ; Δ ⊢_{TAL} w : τ^{φ}  w is a well-formed word value of flagged type τ^{φ}

(i.e., w has type τ or w is ?τ and φ is 0)

Ψ; Δ; Γ ⊢_{TAL} v : τ  v is a well-formed small value of type τ
Ψ; Δ; Γ ⊢_{TAL} I  I is a well-formed instruction sequence
⊢_{TAL} P  P is a well-formed program

Fig. 15. TAL static semantic judgments.

and elaborated on below. The large number of judgments is a reflection more of the large number of syntactic classes, than of any inherent semantic complexity. The static semantics is inspired by and follows the conventions of Morrisett and Harper’s [1997] λ_{gc}^{→∀}.

The first five judgments in Figure 15 specify the well-formedness conditions for types and define subtyping relationships. Four of the five judgments include a type context that indicates which type variables are in scope. Heaps and heap types must be closed, and as a result, their judgments do not include type contexts.

The subtyping judgments are not intended to support subtyping in the usual generality, although they could be expanded to do so. Instead, they are used to allow the forgetting of information. The judgment Δ ⊢_{TAL} τ_{1} ≤ τ_{2}, for instance, makes it possible to forget that a field of a tuple has been initialized. This is used in the subject reduction argument (Lemma 1) where it is sometimes necessary that references to an initialized tuple be given the old uninitialized type. The register file subtyping judgment makes it possible to forget about the contents of some registers. This makes it possible to jump to a code block when too many registers are defined.

The rest of the judgments check the well-formedness of the term constructs. Neither heaps nor register files may contain free type variables, so their judgments do not include a type context. Since values in the heap are mutually recursive, the heap’s own type is used while typing the heap; to make this sound we separately require that heap types be well formed. The next four judgments are for assigning types to values. In addition to one judgment for each sort of value, there is a judgment for assigning flagged types to word values: the junk value ?τ may not be assigned any regular type, but it may be assigned the flagged type τ^{0}. Each sort of value may contain references to the heap; all but heap values may contain free type variables, but only small values may contain registers. Consequently, heap value formation requires only a heap type; word value formation adds a type context; and small value formation adds a type context and a register file type.

The central result is the type safety of TAL programs: well-formed programs

Δ ⊢_{TAL} τ ⊢_{TAL} Ψ Δ ⊢_{TAL} Γ

(type) FTV (τ) ⊆ Δ ; Δ ⊢_{TAL} τ  (htype) {∅ ⊢_{TAL} τ_{i} \atop ⊢_{TAL} \{ℓ_{1}:τ_{1}, …, ℓ_{n}:τ_{n}\}}

(rftype)

Δ ⊢_{TAL} τ_{i}
Δ ⊢_{TAL} {r_{1}:τ_{1}, …, r_{n}:τ_{n}}

Δ ⊢_{TAL} τ_{1} ≤ τ_{2} Δ ⊢_{TAL} Γ_{1} ≤ Γ_{2}

(reflex)

Δ ⊢_{TAL} τ Δ ⊢_{TAL} τ ≤ τ

(trans)

Δ ⊢_{TAL} τ_{1} ≤ τ_{2} Δ ⊢_{TAL} τ_{2} ≤ τ_{3}
Δ ⊢_{TAL} τ_{1} ≤ τ_{3}

(0-1) Δ ⊢_{TAL} τ_{i}

Δ ⊢_{TAL} ⟨τ_{1}^{φ_{1}}, …, τ_{i−1}^{φ_{i−1}}, τ_{i}^{1}, τ_{i+1}^{φ_{i+1}}, …, τ_{n}^{φ_{n}}⟩ ≤ ⟨τ_{1}^{φ_{1}}, …, τ_{i−1}^{φ_{i−1}}, τ_{i}^{0}, τ_{i+1}^{φ_{i+1}}, …, τ_{n}^{φ_{n}}⟩

(weaken) Δ ⊢_{TAL} τ_{i} (for 1 ≤ i ≤ m) Δ ⊢_{TAL} {r_{1} : τ_{1}, …, r_{m} : τ_{m}} ≤ {r_{1} : τ_{1}, …, r_{n} : τ_{n}} (m ≥ n)

⊢_{TAL} P ⊢_{TAL} H : Ψ Ψ ⊢_{TAL} R : Γ

(prog)

⊢_{TAL} H : Ψ Ψ ⊢_{TAL} R : Γ Ψ; ∅; Γ ⊢_{TAL} I
⊢_{TAL} (H, R, I)

(heap) ⊢_{TAL} Ψ Ψ ⊢_{TAL} h_{i} : τ_{i} hval ⊢_{TAL} {ℓ_{1} ↦ h_{1}, …, ℓ_{n} ↦ h_{n}} : Ψ (Ψ = {ℓ_{1}:τ_{1}, …, ℓ_{n}:τ_{n}})

(reg) Ψ; ∅ ⊢_{TAL} w_{i} : τ_{i} wval (for 1 ≤ i ≤ m) Ψ ⊢_{TAL} {r_{1} ↦ w_{1}, …, r_{m} ↦ w_{m}} : {r_{1} ↦ τ_{1}, …, r_{n} ↦ τ_{n}} (m ≥ n)

Fig. 16. Static semantics of TAL (miscellaneous).

never get “stuck.” The well-formed terminal configurations of the operational semantics have the form (H, R{r1 ↦ w}, halt[τ]). All other terminal configurations are considered stuck. Type safety follows from the usual Subject Reduction and Progress theorems. Their proofs are given in the Appendix.

Theorem (Subject Reduction). If ⊢_{TAL} P and P ⟼ P′, then ⊢_{TAL} P′.

Theorem (Progress). If ⊢_{TAL} P, then either there exists P′ such that P ⟼ P′ or P is of the form (H, R{r1 ↦ w}, halt[τ]).

Corollary (Type Safety). If ⊢_{TAL} P, then there is no stuck P′ such that P ⟼^{∗} P′.

7.4 Code Generation

The translation from λ^{A} to TAL appears in Figures 19 and 20. The type translation, T[[·]], from λ^{A} to TAL is straightforward. The only point of interest is the translation of function types, which must assign registers to value arguments:

T[[∀[α⃗](τ_{1}, ⋯, τ_{n}) → void]] =^{def} ∀[α⃗]{r1:T[[τ_{1}]], …, rn:T[[τ_{n}]]}

The most interesting part of the term translation is the translation of declara-

Ψ ⊢_{TAL} h : τ hval Ψ; Δ ⊢_{TAL} w : τ wval Ψ; Δ ⊢_{TAL} w : τ^{φ} Ψ; Δ; Γ ⊢_{TAL} v : τ

(tuple)

Ψ; ∅ ⊢_{TAL} w_{i} : τ_{i}^{φ_{i}}

Ψ ⊢_{TAL} ⟨w_{1}, …, w_{n}⟩ : ⟨τ_{1}^{φ_{1}}, …, τ_{n}^{φ_{n}}⟩ hval

(code)

α⃗ ⊢_{TAL} Γ Ψ; α⃗; Γ ⊢_{TAL} I
Ψ ⊢_{TAL} code[α⃗]Γ.I : ∀[α⃗].Γ hval
(label) Δ ⊢_{TAL} τ′ ≤ τ Ψ; Δ ⊢_{TAL} ℓ : τ wval (Ψ(ℓ) = τ′)  (int) Ψ; Δ ⊢_{TAL} i : int wval

(tapp-word)

Δ ⊢_{TAL} τ Ψ; Δ ⊢_{TAL} w : ∀[α, β⃗].Γ wval
Ψ; Δ ⊢_{TAL} w[τ] : ∀[β⃗].Γ[τ/α] wval

(pack-word)

Δ ⊢_{TAL} τ Ψ; Δ ⊢_{TAL} w : τ′[τ/α] wval
Ψ; Δ ⊢_{TAL} pack [τ, w] as ∃α.τ′ : ∃α.τ′ wval

(init)

Ψ; Δ ⊢_{TAL} w : τ wval
Ψ; Δ ⊢_{TAL} w : τ^{φ}

(uninit)

Δ ⊢_{TAL} τ
Ψ; Δ ⊢_{TAL} ?τ : τ^{0}

(reg-val)

Ψ; Δ; Γ ⊢_{TAL} r : τ (Γ(r) = τ)

(word-val)

Ψ; Δ ⊢_{TAL} w : τ wval
Ψ; Δ; Γ ⊢_{TAL} w : τ

(tapp-val)

Δ ⊢_{TAL} τ Ψ; Δ; Γ ⊢_{TAL} v : ∀[α, β⃗].Γ′
Ψ; Δ; Γ ⊢_{TAL} v[τ] : ∀[β⃗].Γ′[τ/α]

(pack-val)

Δ ⊢_{TAL} τ Ψ; Δ; Γ ⊢_{TAL} v : τ′[τ/α]

Ψ; Δ; Γ ⊢_{TAL} pack [τ, v] as ∃α.τ′ : ∃α.τ′

Fig. 17. Static semantics of TAL (values).

tions. Informally, declarations are translated to instruction sequences as follows:

— x = v is mapped to mov r_{x}, v.

— x = π_{i}(v) is mapped to the sequence mov r_{x}, v; ld r_{x}, r_{x}[i − 1].

— x = v_{1} p v_{2} is mapped to the sequence mov r_{x}, v_{1} ; arith r_{x}, r_{x}, v_{2}, where arith is the appropriate arithmetic instruction.

— [α, x] = unpack v is mapped to unpack[α, r_{x}], v.

— x = malloc[τ⃗] is mapped to malloc r_{x}[τ⃗].

— x = v[i] ← v′ is mapped to the sequence mov r_{x}, v; mov r_{temp}, v′; st r_{x}[i − 1], r_{temp}.

— v(v_{1}, …, v_{n}) is mapped to the sequence

mov r_{temp}, v; mov r_{temp_{1}}, v_{1} ; …; mov r_{temp_{n}}, v_{n}; movr1, r_{temp_{1}} ; …; movrn, r_{temp_{n}} ; jmp r_{temp} .

Note that the arguments cannot be moved immediately into the registers r1, …, rn because those registers may be used in later arguments.

— if0(v, e_{1}, e_{2}) is mapped to the sequence mov r_{temp}, v; bnz r_{temp}, ℓ[α⃗] ; I_{1}

Ψ; Δ; Γ ⊢_{TAL} I

(s-arith) Ψ; Δ; Γ ⊢_{TAL} r_{s} : int  Ψ; Δ; Γ ⊢_{TAL} v : int Ψ; Δ; Γ{r_{d}:int} ⊢_{TAL} I Ψ; Δ; Γ ⊢_{TAL} arith r_{d}, r_{s}, v; I (arith ∈ {add, mul, sub})

(s-bnz)

Ψ; Δ; Γ ⊢_{TAL} r : int Ψ; Δ; Γ ⊢_{TAL} v : ∀[ ].Γ′

Δ ⊢_{TAL} Γ ≤ Γ′ Ψ; Δ; Γ ⊢_{TAL} I
Ψ; Δ; Γ ⊢_{TAL} bnz r, v; I
(s-ld) Ψ; Δ; Γ ⊢_{TAL} r_{s} : ⟨τ_{0}^{φ_{0}}, …, τ_{n−1}^{φ_{n−1}}⟩ Ψ; Δ; Γ{r_{d}:τ_{i}} ⊢_{TAL} I Ψ; Δ; Γ ⊢_{TAL} ld r_{d}, r_{s}[i]; I (φ_{i} = 1, 0 ≤ i < n)

(s-malloc)

Δ ⊢_{TAL} τ_{i} Ψ; Δ; Γ{r_{d}:⟨τ_{1}^{0}, …, τ_{n}^{0}⟩} ⊢_{TAL} I
Ψ; Δ; Γ ⊢_{TAL} malloc r_{d}[τ_{1}, …, τ_{n}]; I

(s-mov)

Ψ; Δ; Γ ⊢_{TAL} v : τ Ψ; Δ; Γ{r_{d} : τ} ⊢_{TAL} I
Ψ; Δ; Γ ⊢_{TAL} mov r_{d}, v; I

(s-sto) Ψ; Δ; Γ ⊢_{TAL} r_{d} : ⟨τ_{0}^{φ_{0}}, …, τ_{n−1}^{φ_{n−1}}⟩  Ψ; Δ; Γ ⊢_{TAL} r_{s} : τ_{i} Ψ; Δ; Γ{r_{d}:⟨τ_{0}^{φ_{0}}, …, τ_{i−1}^{φ_{i−1}}, τ_{i}^{1}, τ_{i+1}^{φ_{i+1}}, …, τ_{n−1}^{φ_{n−1}}⟩} ⊢_{TAL} I Ψ; Δ; Γ ⊢_{TAL} st r_{d}[i], r_{s}; I (0 ≤ i < n)
(s-unpack) Ψ; Δ; Γ ⊢_{TAL} v : ∃α.τ Ψ; Δ, α; Γ{r_{d}:τ} ⊢_{TAL} I Ψ; Δ; Γ ⊢_{TAL} unpack[α, r_{d}], v; I (α ∉ Δ)

(s-jmp)

Ψ; Δ; Γ ⊢_{TAL} v : ∀[ ].Γ′ Δ ⊢_{TAL} Γ ≤ Γ′
Ψ; Δ; Γ ⊢_{TAL} jmp v

(s-halt)

Ψ; Δ; Γ ⊢_{TAL} r1 : τ
Ψ; Δ; Γ ⊢_{TAL} halt[τ]

Fig. 18. Static semantics of TAL (instructions).

where ℓ is bound in the heap to code[α⃗]Γ.I_{2}; the translation of e_{i} is I_{i}; the free type variables of e_{2} are contained in α⃗; and Γ is the register file type corresponding to the free variables of e_{2}.

— halt[τ]v is mapped to the sequence movr1, v; halt[τ]

The formal translation uses a mapping γ that tracks what label or register is used to implement each term variable. As discussed above, if0 terms are implemented by a conditional branch to a new code block representing the else-clause. These new code blocks must be heap allocated, so translations of terms (and translations of heap values, which can contain terms) must return an addition to the heap as well as an instruction sequence. Also, the translation of terms must track the current type context Δ and register file type Γ in order to place that information into new code blocks resulting from if0 terms.

Lemma (Code Generation Type Correctness). If ⊢_{A} P then ⊢_{TAL} T_{prog}[[P]].

By composing the five translations (CPS conversion, closure conversion, hoisting, allocation, and code generation), we obtain a translation from λ^{F} to TAL. The type correctness of the composite translation follows from the preceding type correctness

T[[α]]  =^{def} α
T[[int]]  =^{def} int
T[[∀[α⃗].(τ_{1}, …, τ_{n}) → void]]  =^{def} ∀[α⃗].{r1:T[[τ_{1}]], …, rn:T[[τ_{n}]]}
T[[⟨τ_{1}^{φ_{1}}, …, τ_{n}^{φ_{n}⟩}]]  =^{def} ⟨T[[τ_{1}]]^{φ_{1}}, …, T[[τ_{n}]]^{φ_{n}}⟩
T[[∃α.τ]]  =^{def} ∃α.T[[τ]]
T_{prog}[[letrec x_{1} ↦ h_{1}, …, x_{n} ↦ h_{n} in e]] =^{def} (H, ∅, I)

where γ  = {x_{1} ↦ ℓ_{1}, …, x_{n} ↦ ℓ_{n}}

⟨H_{i}, h′_{i}⟩ = T_{hval}^{γ}[[h_{i}]]
⟨H_{exp}, I⟩ = T_{exp}^{γ,∅,∅}[[e]]
H_{root}  = {ℓ_{1} ↦ h′_{1}, …, ℓ_{n} ↦ h′_{n}}
H  = H_{root} H_{1} ⋯ H_{n}H_{exp}

ℓ_{i} are distinct

T_{hval}^{γ}[[code[α⃗](x_{1}:τ_{1}, …, x_{n}:τ_{n}).e]]  =^{def} ⟨H, code[α⃗]Γ.I⟩

where

Γ  = {r1:T[[τ_{1}]], …, rn:T[[τ_{n}]]}
γ′  = γ{x_{1} ↦ r1, …, x_{n} ↦ rn}
⟨H, I⟩ = T_{exp}^{γ′,α⃗,Γ}[[e]]
T_{hval}^{γ}[[⟨v_{1}, …, v_{n}⟩]]  =^{def} ⟨∅, ⟨T_{val}^{γ}[[v_{1}]], …, T_{val}^{γ}[[v_{n}]]⟩⟩
T_{val}^{γ}[[x^{τ}]]  =^{def} γ(x)
T_{val}^{γ}[[i^{τ}]]  =^{def} i
T_{val}^{γ}[[(v[σ])^{τ}]]  =^{def} T_{val}^{γ}[[v]][T[[σ]]]
T_{val}^{γ}[[(pack [τ_{1}, v] as τ_{2})^{τ}]]  =^{def} pack [T[[τ_{1}]], T_{val}^{γ}[[v]]] as T[[τ_{2}]]

Fig. 19. Translation from λ^{A} to TAL (except expressions).

lemmas.

Corollary (Compiler Type Correctness). If ⊢_{F} e : τ then ⊢_{TAL} (T_{prog} ∘ A_{prog} ∘ H_{prog} ∘ C_{prog} ∘ K_{prog})[[e]].

7.5 TAL Factorial

The factorial computation translated into TAL appears in Figure 21. To obtain the code shown, a few standard optimizations were applied; in particular, a clever (but automatable) register allocation and the removal of redundant moves. Were the efficiency of this version unsatisfactory, a more efficient version could be obtained by improving the λ^{F} source program (e.g., by using tail recursion), by optimizing intermediate language programs (e.g., by eliminating unnecessary closure creation), or by hand-coding a highly optimized version directly in TAL, such as the one in Figure 1.

8. EXTENSIONS AND PRACTICE

The previous sections provide a theoretical basis for compiling high-level languages to typed assembly language. In this section we discuss some issues that arise when putting this technology into practice.

T_{exp}^{γ,Δ,Γ}[[let x = u^{τ} in e]]  =^{def} ⟨H, (mov r, T_{val}^{γ}[[u^{τ}]]; I)⟩

where ⟨H, I⟩ = T_{exp}^{γ{x↦r},Δ,Γ{r:T[[τ]]}}[[e]]

r is fresh

T_{exp}^{γ,Δ,Γ}[[let x = π_{i}(u^{⟨τ_{1}^{φ1} ,...,τ_{n}^{φn} ⟩}) in e]] =^{def} ⟨H, (\begin{matrix}mov r, T_{val}^{γ}[[u^{⟨τ_{1}^{φ1} ,...,τ_{n}^{φn} ⟩}]]; \\ ld r, r[i − 1]; I)⟩ \end{matrix}

where ⟨H, I⟩ = T_{exp}^{γ{x↦r},Δ,Γ{r:T[[τ_{i}]]}}[[e]]

r is fresh

T_{exp}^{γ,Δ,Γ}[[let x = v_{1} p v_{2} in e]]  =^{def} ⟨H, (\begin{matrix}mov r, T_{val}^{γ}[[v_{1}]]; \\ arith_{p} r, r, T_{val}^{γ}[[v_{2}]]; I)⟩ \end{matrix}

where ⟨H, I⟩ = T_{exp}^{γ{x↦r},Δ,Γ{r:int}}[[e]]

arith_{+} = add
arith_{−} = sub
arith_{×} = mul

r is fresh

T_{exp}^{γ,Δ,Γ}[[let [α, x] = unpack u^{∃α.τ} in e]]  =^{def} ⟨H, (unpack[α, r], T_{val}^{γ}[[u^{∃α.τ}]]; I)⟩

where ⟨H, I⟩ = T_{exp}^{γ{x↦r},Δ{α},Γ{r:T[[τ]]}}[[e]]

α, r are fresh

T_{exp}^{γ,Δ,Γ}[[let x = malloc[τ_{1}, …, τ_{n}] in e]]  =^{def} ⟨H, (malloc r[T[[τ_{1}]], …, T[[τ_{n}]]]; I)⟩

where

⟨H, I⟩ = T_{exp}^{γ{x↦r},Δ,Γ{r:⟨T[[τ_{1}]]^{0},...,T[[τ_{n}]]^{0}⟩}}[[e]]

r is fresh

T_{exp}^{γ,Δ,Γ}[[let x = u^{⟨τ_{1}^{φ1} ,...,τ_{n}^{φn} ⟩}[i] ← v in e]] =^{def} ⟨H, (\begin{matrix}mov r, T_{val}^{γ}[[u^{⟨τ_{1}^{φ1} ,...,τ_{n}^{φn} ⟩}]]; \\ mov r′, T_{val}^{γ}[[v]]; \\ st r[i − 1], r′; I)⟩ \end{matrix}

where

⟨H, I⟩ = T_{exp}^{γ{x↦r},Δ,Γ′}[[e]]
Γ′  = Γ\{r:T[[⟨\begin{matrix}τ_{1}^{φ_{1}}, …, τ_{i−1}^{φ_{i−1}}, τ_{i}^{1}, \\ τ_{i+1}^{φ_{i+1}}, …, τ_{n}^{φ_{n}}⟩]]\} \end{matrix}

r, r′ are fresh

T_{exp}^{γ,Δ,Γ}[[v(v_{1}, …, v_{n})]]  =^{def} ⟨∅, (\begin{matrix}mov r′_{0}, T_{val}^{γ}[[v]]; \\ mov r′_{1}, T_{val}^{γ}[[v_{1}]]; …; \\ mov r′_{n}, T_{val}^{γ}[[v_{n}]]; \\ movr1, r′_{1}; … \\ movrn, r′_{n}; \\ jmp r′_{0})⟩ \end{matrix}

where r′_{i} are fresh and r′_{i} ∉ {r1, …, rn}

T_{exp}^{γ,Δ,Γ}[[if0(v, e_{1}, e_{2})]]  =^{def} ⟨H_{1}H_{2}\{ℓ ↦ h\}, (\begin{matrix}mov r, T_{val}^{γ}[[v]]; \\ bnz r, ℓ[Δ]; I_{1})⟩ \end{matrix}

where

⟨H_{i}, I_{i}⟩ = T_{exp}^{γ,Δ,Γ}[[e_{i}]]
h  = code[Δ]Γ.I_{2}

ℓ, r are fresh

T_{exp}^{γ,Δ,Γ}[[halt[τ]v]]  =^{def} ⟨∅, (\begin{matrix}movr1, T_{val}^{γ}[[v]]; \\ halt[T[[τ]]])⟩ \end{matrix}

Fig. 20. Translation of expressions from λ^{A} to TAL.

(H, {}, I) where

H =
l fact:
code[ ]{r1:⟨⟩,r2:int,r3:τ_{k}}.
bnz r2,l nonzero

unpack [α,r3],r3 % zero branch: call k (in r3) with 1 ld r4,r3[0] % project k code ld r1,r3[1] % project k environment

mov r2,1

jmp r4 % jump with {r1 = env, r2 = 1}

l nonzero:

code[ ]{r1:⟨⟩,r2:int,r3:τ_{k}}.

sub r4,r2,1                  % n − 1

malloc r5[int, τ_{k}] % create environment for cont in r5 st r5[0],r2 % store n into environment st r5[1],r3 % store k into environment malloc r3[∀[ ].{r1:⟨int^{1}, τ_{k}^{1}⟩,r2:int}, ⟨int^{1}, τ_{k}^{1}⟩] % create cont closure in r3

mov r2,l cont

st r3[0],r2 % store cont code st r3[1],r5 % store environment ⟨n, k⟩

mov r2,r4                    % arg := n − 1

mov r3,pack [⟨int^{1}, τ_{k}^{1}⟩,r3] as τ_{k} % abstract the type of the environment jmp l fact % call fact(env, n − 1, l cont)

l cont:

code[ ]{r1:⟨int^{1}, τ_{k}^{1}⟩,r2:int}. % r2 contains (n − 1)!

ld r3,r1[0]                  % retrieve n
ld r4,r1[1]                  % retrieve k
mul r2,r3,r2                 % n × (n − 1)!
unpack [α,r4],r4             % unpack k

ld r3,r4[0] % project k code ld r1,r4[1] % project k environment jmp r3 % jump to k with {r1 = env, r2 = n!}

l halt:

code[ ]{r1:⟨⟩,r2:int}.

mov r1,r2

halt[int] % halt with result in r1

and I = malloc r1[ ] % create an empty environment (⟨⟩) malloc r2[ ] % create another empty environment malloc r3[∀[ ].{r1:⟨⟩,r2:int}, ⟨⟩] % create halt closure in r3

mov r4,l halt

st r3[0],r4 % store halt code st r3[1],r2 % store halt environment ⟨⟩ mov r2,6 % load argument (6) mov r3,pack [⟨⟩,r3] as τ_{k} % abstract the type of the environment jmp l fact % call fact(⟨⟩, 6, l halt)

and τ_{k} = ∃α.⟨∀[ ].{r1:α,r2:int}^{1}, α^{1}⟩

Fig. 21. Typed assembly code for factorial.

8.1 Implementation

In order to investigate the applicability of our approach to realistic modern programming languages, we have implemented a version of TAL for the Intel 32-bit Architecture (IA32) [Intel 1996], and have compilers for a number of different source languages including a safe C-like language [Morrisett et al. 1999] and a higher-order, dynamically typed language (a subset of Scheme). Compilers for Standard ML and a small object-oriented language are also in development.

TALx86, the target language for our compilers, is a strongly typed version of the IA32 assembly language. Our type checker verifies standard MASM assembly code in which type annotations and complex instructions such as malloc are assembly language macros. The MASM assembler processes this annotated code as it would any other assembly code, expanding the instruction macros as their definitions dictate and erasing types as it translates the assembly code into concrete machine instructions. We have also implemented our own assembler and are extending it to produced typed object files. Such typed object files include code/data segments and a type segment similar to Necula and Lee’s code and proof segments in their PCC binaries [Necula 1997]. We have implemented a tool that reads TALx86 files, type checks them, and assembles them into object files or invokes MASM.

The TALx86 type system is based on the type system described in this article but enriched with a variety of standard constructs including floats, sums, arrays, references, recursive types, and higher-order type constructors. In order to deal with floating-point values correctly in the presence of polymorphism, we use a kind structure that distinguishes types of objects that are 32 bits wide (such as pointers and integers) from types of objects possibly of other sizes. If a polymorphic type variable α has the 32-bit kind, then objects of type α can be passed in generalpurpose registers, and tuple offsets may be computable for fields appearing after a field of type α. If, on the other hand, α has the more general kind “Type,” the type checker cannot tell how large objects of type α are, and these operations are disallowed.

To support separate compilation and type-safe linking, we have also augmented our typed assembly language with a module system [Glew and Morrisett 1999]. A TAL interface file specifies the types and terms that a TAL implementation file defines. The types may either be opaque to support information hiding and modularity, or transparent to allow information sharing and admit some cross-module optimizations. Our system performs a series of checks to ensure that implementations are well formed and that their interfaces are compatible and complete. Once interface compatibility and completeness have been verified, we assemble the code as described above and invoke a standard untyped linker.

To deal with the creation and examination of exception packets TALx86 includes a type-tagging mechanism [Glew 1999]. The basic idea is that freshly created heap pointers may be associated with a type, and that a tag for an unknown type α can be tested against a tag for a known type τ. If the test succeeds, the unknown type is refined to the known type. Using these tags, we implement an exception packet as an existentially packaged pair containing a tag of the hidden type (serving as the exception name) and a value of that type.

TALx86 also contains some support for compiling objects. The type system has a more general notion of subtyping than this article that includes the usual contravariant rule for code, right-extension and depth subtyping for tuples, and a variance mechanism for arrays and references. Furthermore, the type-tagging mechanism can also be used to tag objects with their class. This mechanism provides us with a way to implement down-casting. However, while TALx86 contains the necessary constructs to admit some simple object encodings, we are still developing the theoretical and practical tools we need to admit efficient object encodings.

Although this article describes a CPS-based compiler, all of the compilers we have built use a stack-based compilation model. Both standard continuations and exceptions are allocated on the stack, and the stack is also used to store spilled temporary values. The details of our stack typing discipline are discussed in Morrisett et al. [1998]. The primary mechanisms are as follows. The size of the stack and the types of its contents are specified by stack types, and code blocks indicate stack types describing the state of the stack they expect. Since code is typically expected to work with stacks of varying size, functions may quantify over stack type variables, resulting in stack polymorphism. The combination of stack types and our register typing discipline allows us to model almost any standard calling convention. Arguments, results, and continuations (or return addresses) may be placed in registers, on the stack, or both, and the implementer may specialize conventions for known functions for better register allocation.

Real machines also have a finite amount of heap space. It is straightforward to link TALx86 to a conservative garbage collector [Boehm and Weiser 1988] and reclaim dead heap values. It is worth noting that our use of conservative collection is sound. Conservative collectors make assumptions about the way pointers can be used in programs that untyped assembly language programs can violate. However, the TAL type system guarantees that these assumptions do hold because labels are a strong abstraction; labels cannot be synthesized out of integers, and operations like pointer arithmetic are disallowed. TAL guarantees that other GC constraints hold because values that disobey the constraints cannot be constructed. For example, TAL disallows pointers into the middle of objects and ensures alignment constraints are obeyed.

Support for an accurate collector would require introducing tags so that we may distinguish pointers from integers, or else require a type-passing interpretation [Tolmach 1994; Morrisett and Harper 1997]. In the former case, we would have to ensure that all values are properly tagged and fully initialized at every potential garbage collection site. We believe that it is feasible to devise a type system to ensure these constraints, but we have not seriously investigated this option.

8.2 Future Work

There remain several directions in which TAL could be improved. One of the most important is to make array manipulation efficient. In order to ensure safe access to arrays, TALx86 uses complex instructions (which expand into three real instructions) that perform subscript and update operations after checking that the array offset is in bounds. These bounds checks cannot be eliminated in the current TAL framework. As a result, array-intensive applications will suffer the same performance penalties that they do in Java just-in-time compilers where there is no time to perform analyses to eliminate the checks. However, Xi [1999] and Xi and Pfenning [1998; 1999] have shown how to eliminate array bounds checks effectively using dependent types. TALx86 can be extended with similar constructs. We have implemented a prototype version in which these checks can be eliminated, but we have not yet added compiler support for generating code with unchecked array subscripts.

Another important direction is to augment our compiler with data-layout optimizations such as those used in the TIL compiler [Tarditi et al. 1996]. As discussed in Section 5, such optimizations require programs to have the ability to analyze types at run-time, which is not directly compatible with the type-erasure interpretation adopted here. To make such optimizations permissible, we are augmenting the TALx86 language so that TAL programs can construct values that represent types and analyze those values when necessary, following the work of Crary et al. [1998; 1999].

Although we believe our translations are operationally correct, we are still searching for robust proofs of correctness. Similar CPS [Danvy and Filinski 1992] and closure conversion [Minamide et al. 1996] translations have already been proven correct, but these results do not easily extend to languages that include recursive types or objects. The principal problem is that these arguments are based on inductively defined, type-indexed logical relations between source and target language terms. Extending this framework so that it supports recursive types or objects is difficult because the relations can no longer be constructed in a simple inductive fashion. A syntactic proof of correctness seems possible (we have constructed such arguments for the CPS and closure conversion phases), but the proofs are overly specific to the details of the translation. Moreover, security-conscious applications might require translations that are not only operationally correct but also fully abstract. We hope further research on the proof theory of similar systems will eventually allow us to construct these arguments.

Other avenues of future research include extension of our type system to the same level of generality as PCC through the use of a dependent type theory, an investigation of the support required to compile Java classes and objects into TAL, and an exploration of type-theoretic mechanisms for performing explicit memory management.

9. SUMMARY

We have given a compiler from System F to a statically typed assembly language. The type system for the assembly language ensures that source-level abstractions such as closures and polymorphic functions are enforced at the machine-code level. Furthermore, although the type system may preclude some advanced optimizations, many common compiler-introduced, low-level optimizations, such as register allocation, instruction selection, or instruction scheduling, are largely unaffected. Furthermore, programmers concerned with efficiency can hand-code routines in assembly, as long as the resulting code passes the type checker. Consequently, TAL provides a foundation for high-performance computing in environments where untrusted code must be checked for safety before being executed.

APPENDIX

Lemma (Context Strengthening). If Δ ⊆ Δ′ then (1 ) If Δ ⊢_{TAL} τ then Δ′ ⊢_{TAL} τ (2 ) If Δ ⊢_{TAL} τ_{1} ≤ τ_{2} then Δ′ ⊢_{TAL} τ_{1} ≤ τ_{2}.

Proof. Part 1 is immediate by (type). Part 2 is by induction on derivations. Lemma (Subtyping Regularity). If Δ ⊢_{TAL} τ ≤ τ′ then Δ ⊢_{TAL} τ and

Δ ⊢_{TAL} τ′.

Proof. By induction on derivations.

Lemma (Heap Extension). If ⊢_{TAL} H : Ψ, ∅ ⊢_{TAL} τ, Ψ{ℓ : τ} ⊢_{TAL} h : τ hval, and ℓ ∉ H then (1 ) ⊢_{TAL} Ψ{ℓ : τ} (2 ) ⊢_{TAL} H{ℓ ↦ h} : Ψ{ℓ : τ} (3 ) If Ψ ⊢_{TAL} R : Γ then Ψ{ℓ : τ} ⊢_{TAL} R : Γ (4 ) If Ψ; Δ; Γ ⊢_{TAL} I then Ψ{ℓ : τ}; Δ; Γ ⊢_{TAL} I (5 ) If Ψ ⊢_{TAL} h : σ hval then Ψ{ℓ : τ} ⊢_{TAL} h : σ hval (6 ) If Ψ; Δ ⊢_{TAL} w : σ^{φ} then Ψ{ℓ : τ}; Δ ⊢_{TAL} w : σ^{φ} (7 ) If Ψ; Δ ⊢_{TAL} w : σ wval then Ψ{ℓ : τ}; Δ ⊢_{TAL} w : σ wval (8 ) If Ψ; Δ; Γ ⊢_{TAL} v : σ then Ψ{ℓ : τ}; Δ; Γ ⊢_{TAL} v : σ.

Proof. Part 1 is immediate by (htype). Part 2 follows from parts 1 and 5. Parts 3–8 are by induction on derivations.

Lemma (Heap Update). If ⊢_{TAL} H : Ψ, ∅ ⊢_{TAL} τ ≤ Ψ(ℓ), and Ψ{ℓ : τ} ⊢_{TAL} h : τ then (1 ) ⊢_{TAL} Ψ{ℓ : τ} (2 ) ⊢_{TAL} H{ℓ ↦ h} : Ψ{ℓ : τ} (3 ) If Ψ ⊢_{TAL} R : Γ then Ψ{ℓ : τ} ⊢_{TAL} R : Γ (4 ) If Ψ; Δ; Γ ⊢_{TAL} I then Ψ{ℓ : τ}; Δ; Γ ⊢_{TAL} I (5 ) If Ψ ⊢_{TAL} h : σ hval then Ψ{ℓ : τ} ⊢_{TAL} h : σ hval (6 ) If Ψ; Δ ⊢_{TAL} w : σ^{φ} then Ψ{ℓ : τ}; Δ ⊢_{TAL} w : σ^{φ} (7 ) If Ψ; Δ ⊢_{TAL} w : σ wval then Ψ{ℓ : τ}; Δ ⊢_{TAL} w : σ wval (8 ) If Ψ; Δ; Γ ⊢_{TAL} v : σ then Ψ{ℓ : τ}; Δ; Γ ⊢_{TAL} v : σ.

Proof. Part 1 is immediate by (htype) and Subtyping Regularity. Part 2 follows from parts 1 and 5. Parts 3–8 are by induction on derivations. The only interesting case is the case for the rule (label). The derivation must end with the following:

Δ ⊢_{TAL} σ′ ≤ σ

Ψ; Δ ⊢_{TAL} ℓ′ : σ wval

(Ψ(ℓ′) = σ′)

If ℓ ≠ ℓ′ then clearly the inference also holds for Ψ{ℓ : τ}. Suppose ℓ = ℓ′. By hypothesis and Context Strengthening, we deduce Δ ⊢_{TAL} τ ≤ σ′. Then the conclusion may be proven with the (trans) rule, as follows:

Δ ⊢_{TAL} τ ≤ σ′ Δ ⊢_{TAL} σ′ ≤ σ
Δ ⊢_{TAL} τ ≤ σ
Ψ{ℓ : τ}; Δ ⊢_{TAL} ℓ : σ wval

(Ψ{ℓ : τ}(ℓ) = τ)

Lemma (Register File Update). If Ψ ⊢_{TAL} R : Γ and Ψ; ∅ ⊢_{TAL} w : τ wval then Ψ ⊢_{TAL} R{r ↦ w} : Γ{r : τ}.

Proof. Suppose R is {r_{1} ↦ w_{1}, …, r_{n} ↦ w_{n}} and Γ is {r_{1} ↦ τ_{1}, …, r_{m} ↦ τ_{m}} where r may or may not be in {r_{1}, …, r_{n}}. Since Ψ ⊢_{TAL} R : Γ, by the rule (reg) it must be the case that n ≥ m and Ψ; ∅ ⊢_{TAL} w_{i} : τ_{i} wval (for all 1 ≤ i ≤ n and some τ_{m+1}, …, τ_{n}). So certainly for i such that r_{i} ≠ r, we have Ψ; ∅ ⊢_{TAL} w_{i} : τ_{i} wval, and by hypothesis we have Ψ; ∅ ⊢_{TAL} w : τ wval; so by rule (reg) Ψ ⊢_{TAL} R{r ↦ w} : Γ{r ↦ τ}.

Lemma (Canonical Heap Forms). If Ψ ⊢_{TAL} h : τ hval then

(1 ) If τ = ∀[α⃗].Γ then (a) h = code[α⃗]Γ.I

(b) Ψ; α⃗; Γ ⊢_{TAL} I. (2 ) If τ = ⟨τ_{0}^{φ_{0}}, …, τ_{n−1}^{φ_{n−1}}⟩ then

(a) h = ⟨w_{0}, …, w_{n−1}⟩

(b) Ψ; ∅ ⊢_{TAL} w_{i} : τ_{i}^{φ_{i}}.

Proof. By inspection.

Lemma (Canonical Word Forms). If ⊢_{TAL} H : Ψ and Ψ; ∅ ⊢_{TAL} w : τ wval then

(1 ) If τ = int then w = i. (2 ) If τ = ∀[β_{1}, …, β_{m}].Γ then

(a) w = ℓ[σ_{1}, …, σ_{n}]

(b) H(ℓ) = code[α_{1}, …, α_{n}, β_{1}, …, β_{m}]Γ′.I

(c) Γ = Γ′[σ⃗/α⃗]

(d) Ψ; α_{1}, …, α_{n}, β_{1}, …, β_{m}; Γ′ ⊢_{TAL} I. (3 ) If τ = ⟨τ_{0}^{φ_{0}}, …, τ_{n−1}^{φ_{n−1}}⟩ then

(a) w = ℓ

(b) H(ℓ) = ⟨w_{0}, …, w_{n−1}⟩

(c) Ψ; ∅ ⊢_{TAL} w_{i} : τ_{i}^{φ_{i}}. (4 ) If τ = ∃α.τ then w = pack [τ′, w′] as ∃α.τ and Ψ; ∅ ⊢_{TAL} w′ : τ[τ′/α] wval.

Proof. (1) By inspection.

(2) By induction on the derivation of Ψ; ∅ ⊢_{TAL} w : τ wval: The derivation must end with either the (label) or the (tapp-word) rule. Suppose the former. Then we have w = ℓ, Ψ(ℓ) = τ′, and ∅ ⊢_{TAL} τ′ ≤ ∀[β⃗].Γ. Inspection of the subtyping rules then reveals that τ′ = ∀[β⃗].Γ. Since ⊢_{TAL} H : Ψ, we may deduce that Ψ ⊢_{TAL} H(ℓ) : ∀[β⃗].Γ hval. The conclusion follows by Canonical Heap Forms.

Alternatively, suppose the derivation ends with (tapp-word). Then w = w′[σ] and Ψ; ∅ ⊢_{TAL} w′ : ∀[α, β⃗].Γ′ wval with Γ = Γ′[σ/α]. The conclusion follows by induction.

(3) The derivation Ψ; ∅ ⊢_{TAL} w : τ wval must be shown by use of the (label) rule. Thus, w = ℓ, Ψ(ℓ) = τ′, and ∅ ⊢_{TAL} τ′ ≤ ⟨τ_{0}^{φ_{0}}, …, τ_{n−1}^{φ_{n−1}}⟩. Let us say that φ ≤ φ and 1 ≤ 0. Then inspection of the subtype rules reveals that τ′ must be of the form ⟨τ_{0}^{φ′_{0}}, …, τ_{n−1}^{φ′_{n−1}}⟩ with φ′_{i} ≤ φ_{i} (for each 0 ≤ i ≤ n − 1). Since

⊢_{TAL} H : Ψ, we may deduce that Ψ ⊢_{TAL} H(ℓ) : ⟨τ_{0}^{φ′_{0}}, …, τ_{n−1}^{φ′_{n−1}}⟩ hval. Thus

H(ℓ) = ⟨w_{0}, …, w_{n−1}⟩ and Ψ; ∅ ⊢_{TAL} w_{i} : τ_{i}^{φ′_{i}} by Canonical Heap Forms. It remains to show that Ψ; ∅ ⊢_{TAL} w_{i} : τ_{i}^{φ_{i}} for all 0 ≤ i ≤ n − 1. Suppose φ′_{i} = 1 and φ_{i} = 0 (otherwise the conclusion is immediate). Then Ψ; ∅ ⊢_{TAL} w_{i} : τ_{i}^{1} is shown by the (init) rule, which also permits the deduction of Ψ; ∅ ⊢_{TAL} w_{i} : τ_{i}^{0}.

(4) By inspection.

Lemma (R̂ Typing). If Ψ ⊢_{TAL} R : Γ and Ψ; ∅; Γ ⊢_{TAL} v : τ then Ψ; ∅ ⊢_{TAL} R̂(v) : τ wval.

Proof. The proof is by induction on the syntax of v. Consider the cases for v:

Case v = w. Immediate.

Case v = r. The only rule that can type v is (reg-val), and this rule requires τ = Γ(r). The only rule that can type R is (reg), and this rule requires Ψ; ∅ ⊢_{TAL} R(r) : τ wval. The conclusion follows, since R̂(r) = R(r).

Case v = v′[σ]. The only rule that can type v is (tapp-val), so τ = ∀[β⃗].Γ′[σ/α] and Ψ; ∅; Γ ⊢_{TAL} v′ : ∀[α, β⃗].Γ′. By induction we deduce Ψ; ∅ ⊢_{TAL} R̂(v′) : ∀[α, β⃗].Γ′ wval, and then the rule (tapp-word) proves Ψ; ∅ ⊢_{TAL} R̂(v′)[σ] : ∀[β⃗].Γ′[σ/α] wval. The result follows, since R̂(v′[σ]) = R̂(v′)[σ].

Case v = pack [σ, v′] as ∃α.τ′. The only rule that can type v is (pack-val), so τ = ∃α.τ′ and Ψ; ∅; Γ ⊢_{TAL} v′ : τ′[σ/α]. By induction we deduce Ψ; ∅ ⊢_{TAL} R̂(v′) : τ′[σ/α] wval, and then the rule (pack-word) proves Ψ; ∅ ⊢_{TAL} pack [σ, R̂(v′)] as ∃α.τ′ : ∃α.τ′ wval. The result follows, since R̂(pack [σ, v′] as ∃α.τ′) = pack [σ, R̂(v′)] as ∃α.τ′.

Lemma (Canonical Forms). If ⊢_{TAL} H : Ψ hval, Ψ ⊢_{TAL} R : Γ, and Ψ; ∅; Γ ⊢_{TAL} v : τ then

(1 ) If τ = int then R̂(v) = i. (2 ) If τ = ∀[β_{1}, …, β_{m}].Γ then

(a) R̂(v) = ℓ[σ_{1}, …, σ_{n}]

(b) H(ℓ) = code[α_{1}, …, α_{n}, β_{1}, …, β_{m}]Γ′.I
(c) Γ = Γ′[σ⃗/α⃗]
(d) Ψ; α_{1}, …, α_{n}, β_{1}, …, β_{m}; Γ′ ⊢_{TAL} I.

(3 ) If τ = ⟨τ_{0}^{φ_{0}}, …, τ_{n−1}^{φ_{n−1}}⟩ then (a) R̂(v) = ℓ

(b) H(ℓ) = ⟨w_{0}, …, w_{n−1}⟩
(c) Ψ; ∅ ⊢_{TAL} w_{i} : τ_{i}^{φ_{i}}.

(4 ) If τ = ∃α.τ then R̂(v) = pack [τ′, w] as ∃α.τ and Ψ; ∅ ⊢_{TAL} w : τ[τ′/α] wval.

Proof. Immediate from R̂ Typing and Canonical Word Forms.

Lemma (Type Substitution). If β⃗ ⊢_{TAL} τ_{i} then

(1 ) If Ψ; α⃗, β⃗; Γ ⊢_{TAL} I then Ψ; β⃗; Γ[τ⃗/α⃗] ⊢_{TAL} I[τ⃗/α⃗] (2 ) If Ψ; α⃗, β⃗; Γ ⊢_{TAL} v : τ then Ψ; β⃗; Γ[τ⃗/α⃗] ⊢_{TAL} v[τ⃗/α⃗] : τ[τ⃗/α⃗] (3 ) If Ψ; α⃗, β⃗ ⊢_{TAL} w : τ wval then Ψ; β⃗ ⊢_{TAL} w[τ⃗/α⃗] : τ[τ⃗/α⃗] wval (4 ) If α⃗, β⃗ ⊢_{TAL} Γ_{1} ≤ Γ_{2} then β⃗ ⊢_{TAL} Γ_{1}[τ⃗/α⃗] ≤ Γ_{2}[τ⃗/α⃗] (5 ) If α⃗, β⃗ ⊢_{TAL} τ_{1} ≤ τ_{2} then β⃗ ⊢_{TAL} τ_{1}[τ⃗/α⃗] ≤ τ_{2}[τ⃗/α⃗] (6 ) If α⃗, β⃗ ⊢_{TAL} τ then β⃗ ⊢_{TAL} τ[τ⃗/α⃗].

Proof. By induction on derivations. The only interesting case is the case for the rule (type):

{FTV (τ) ⊆ \{α⃗, β⃗\} \atop α⃗, β⃗ ⊢_{TAL} τ}

The hypothesis must also be proven with the rule (type), so FTV (τ_{i}) ⊆ {β⃗}. Consequently

FTV (τ[τ⃗/α⃗]) ⊆ FTV (τ) \ {α⃗} ∪ (⋃_{i} FTV (τ_{i}))
⊆ {α⃗, β⃗} \ {α⃗} ∪ {β⃗}
= {β⃗}.

Hence we may prove β⃗ ⊢_{TAL} τ[τ⃗/α⃗] using the (type) rule.

Lemma (Register File Weakening). If Δ ⊢_{TAL} Γ_{1} ≤ Γ_{2} and Ψ; Δ ⊢_{TAL} R : Γ_{1} then Ψ; Δ ⊢_{TAL} R : Γ_{2}.

Proof. By inspection of the rules (weaken) and (reg).

Theorem (Subject Reduction). If ⊢_{TAL} P and P ⟼ P′ then ⊢_{TAL} P′.

Proof. P has the form (H, R, ι; I) or (H, R, jmp v). Let T D be the derivation of ⊢_{TAL} P. Consider the following cases for jmp or ι:

Case jmp. T D has the form

⊢_{TAL} H : Ψ Ψ ⊢_{TAL} R : Γ Ψ; ∅; Γ ⊢_{TAL} v : ∀[ ].Γ′ ∅ ⊢_{TAL} Γ ≤ Γ′ Ψ; ∅; Γ ⊢_{TAL} jmp v ⊢_{TAL} P.

By the operational semantics, P′ = (H, R, I[σ⃗/α⃗]) where R̂(v) = ℓ[σ⃗] and H(ℓ) = code[α⃗]Γ′′.I. Then

(1) ⊢_{TAL} H : Ψ is in T D.

(2) From ∅ ⊢_{TAL} Γ ≤ Γ′ and Ψ ⊢_{TAL} R : Γ it follows by Register File Weakening that Ψ ⊢_{TAL} R : Γ′.

(3) By Canonical Forms it follows from Ψ; ∅; Γ ⊢_{TAL} v : ∀[ ].Γ′ that Γ′ = Γ′′[σ⃗/α⃗]

and Ψ; α⃗; Γ′′ ⊢_{TAL} I. By Type Substitution we conclude Ψ; ∅; Γ′ ⊢_{TAL} I[σ⃗/α⃗]. Case add, mul, sub. T D has the form

⊢_{TAL} H : Ψ Ψ ⊢_{TAL} R : Γ

Ψ; ∅; Γ ⊢_{TAL} r_{s} : int Ψ; ∅; Γ ⊢_{TAL} v : int
Ψ; ∅; Γ′ ⊢_{TAL} I
Ψ; ∅; Γ ⊢_{TAL} arith_{p} r_{d}, r_{s}, v; I
⊢_{TAL} P

where Γ′ = Γ{r_{d} : int}. By the operational semantics, P′ = (H, R′, I) where R′ = R{r_{d} ↦ R(r_{s}) p R̂(v)}. Then (1) ⊢_{TAL} H : Ψ is in T D.

(2) By Canonical Forms it follows that R(r_{s}) and R̂(v) are integer literals, and therefore Ψ; ∅ ⊢_{TAL} R(r_{s}) p R̂(v) : int wval. We conclude Ψ ⊢_{TAL} R′ : Γ′ by Register File Update.

(3) Ψ; ∅; Γ′ ⊢_{TAL} I is in T D.

Case bnz. T D has the form

⊢_{TAL} H : Ψ Ψ ⊢_{TAL} R : Γ Ψ; ∅; Γ ⊢_{TAL} r : int  Ψ; ∅; Γ ⊢_{TAL} v : ∀[ ].Γ′ ∅ ⊢_{TAL} Γ ≤ Γ′  Ψ; ∅; Γ ⊢_{TAL} I Ψ; ∅; Γ ⊢_{TAL} bnz r, v; I ⊢_{TAL} P .

If R(r) = 0 then P′ = (H, R, I) and ⊢_{TAL} P′ follows, since Ψ; ∅; Γ ⊢_{TAL} I is in T D. Otherwise the reasoning is exactly as in the case for jmp.

Case ld. T D has the form

⊢_{TAL} H : Ψ Ψ ⊢_{TAL} R : Γ

0 ≤ i ≤ n − 1 φ_{i} = 1
Ψ; ∅; Γ ⊢_{TAL} r_{s} : ⟨τ_{0}^{φ_{0}}, …, τ_{n−1}^{φ_{n−1}}⟩
Ψ; ∅; Γ′ ⊢_{TAL} I
Ψ; ∅; Γ ⊢_{TAL} ld r_{d}, r_{s}[i]; I
⊢_{TAL} P

where Γ′ = Γ{r_{d} : τ_{i}}. By the operational semantics, P′ = (H, R′, I) where R′ = R{r_{d} ↦ w_{i}}, R(r_{s}) = ℓ, H(ℓ) = ⟨w_{0}, …, w_{m−1}}, and 0 ≤ i < m. Then

(1) ⊢_{TAL} H : Ψ is in T D.

(2) By Canonical Forms it follows from Ψ; ∅; Γ ⊢_{TAL} r_{s} : ⟨τ_{0}^{φ_{0}}, …, τ_{n−1}^{φ_{n−1}}⟩ that m = n and Ψ; ∅ ⊢_{TAL} w_{j} : τ_{j}^{φ_{j}} for 0 ≤ j < n. Since φ_{i} = 1 it must be the case (by inspection of the (init) rule) that Ψ; ∅ ⊢_{TAL} w_{i} : τ_{i} wval. By Register File we conclude Ψ ⊢_{TAL} R′ : Γ′.

(3) Ψ; ∅; Γ′ ⊢_{TAL} I is in T D.

Case malloc. T D has the form

⊢_{TAL} H : Ψ Ψ ⊢_{TAL} R : Γ

∅ ⊢_{TAL} τ_{i} Ψ; ∅; Γ′ ⊢_{TAL} I
Ψ; ∅; Γ ⊢_{TAL} malloc r_{d}[τ_{1}, …, τ_{n}]; I
⊢_{TAL} P

where σ = ⟨τ_{1}^{0}, …, τ_{n}^{0}⟩, Ψ′ = Ψ{ℓ : σ}, and Γ′ = Γ{r_{d} : σ}. By the operational semantics, P′ = (H′, R′, I) where H′ = H{ℓ ↦ ⟨?τ_{1}, …,?τ_{n}⟩}, R′ = R{r_{d} ↦ ℓ}, and ℓ ∉ H. Then

(1) By the (tuple) and (uninit) rules we may deduce Ψ′ ⊢_{TAL} ⟨?τ_{1}, …,?τ_{n}⟩ : σ hval. By Heap Extension it follows that ⊢_{TAL} H′ : Ψ′.

(2) By the (type), (reflex), and (label) rules we may deduce that Ψ′; ∅ ⊢_{TAL} ℓ : σ wval. By Heap Extension we deduce that Ψ′ ⊢_{TAL} R : Γ, and it follows by Register File Update that Ψ′ ⊢_{TAL} R′ : Γ′.

(3) By Heap Extension, Ψ′; ∅; Γ′ ⊢_{TAL} I.

Case mov. T D has the form

⊢_{TAL} H : Ψ Ψ ⊢_{TAL} R : Γ

Ψ; ∅; Γ ⊢_{TAL} v : τ Ψ; ∅; Γ′ ⊢_{TAL} I
Ψ; ∅; Γ ⊢_{TAL} mov r, v; I
⊢_{TAL} P

where Γ′ = Γ{r : τ}. By the operational semantics, P′ = (H, R′, I) where R′ = R{r ↦ R̂(v)}. Then

(1) ⊢_{TAL} H : Ψ is in T D.

(2) By R̂ Typing it follows from Ψ; ∅; Γ ⊢_{TAL} v : τ that Ψ; ∅ ⊢_{TAL} R̂(v) : τ wval. Using Register File Update we conclude that Ψ ⊢_{TAL} R′ : Γ′.

(3) Ψ; ∅; Γ′ ⊢_{TAL} I is in T D.

Case st. T D has the form

⊢_{TAL} H : Ψ Ψ ⊢_{TAL} R : Γ

0 ≤ i ≤ n − 1 Ψ; ∅; Γ ⊢_{TAL} r_{d} : σ_{0}
Ψ; ∅; Γ ⊢_{TAL} r_{s} : τ_{i} Ψ; ∅; Γ′ ⊢_{TAL} I
Ψ; ∅; Γ ⊢_{TAL} st r_{d}[i], r_{s}; I
⊢_{TAL} P

where

σ_{0} = ⟨τ_{0}^{φ_{0}}, …, τ_{n−1}^{φ_{n−1}}⟩
σ_{1} = ⟨τ_{0}^{φ_{0}}, …, τ_{i−1}^{φ_{i−1}}, τ_{i}^{1}, τ_{i+1}^{φ_{i+1}}, …, τ_{n−1}^{φ_{n−1}}⟩
Γ′ = Γ{r_{d} : σ_{1}}.

By the operational semantics, P′ = (H′, R, I) where

H′ = H{ℓ ↦ ⟨w_{0}, …, w_{i−1}, R(r_{s}), w_{i+1}, …, w_{m−1}⟩}

and R(r_{d}) = ℓ, H(ℓ) = ⟨w_{0}, …, w_{m}⟩, and 0 ≤ i < m. Then

(1) Since Ψ; ∅; Γ ⊢_{TAL} r_{d} : σ_{0}, it must be the case that Γ(r_{d}) = σ_{0}, and thus since Ψ ⊢_{TAL} R : Γ and R(r_{d}) = ℓ we may deduce Ψ; ∅ ⊢_{TAL} ℓ : σ_{0} wval. The latter judgment must be proven with the (label) rule; hence ∅ ⊢_{TAL} σ′_{0} ≤ σ_{0} where Ψ(ℓ) = σ′_{0}. Note that it follows from Subtyping Regularity and the definition of σ_{0} that ∅ ⊢_{TAL} τ_{j} for each 0 ≤ j < n. Let us say that φ ≤ φ and 1 ≤ 0. Inspection of the subtyping rules reveals that σ′_{0} must be of the form ⟨τ_{0}^{φ′_{0}}, …, τ_{n−1}^{φ′_{n−1}}⟩ with φ′_{j} ≤ φ_{j}. Let

σ′_{1} = ⟨τ_{0}^{φ′_{0}}, …, τ_{i−1}^{φ′_{i−1}}, τ_{i}^{1}, τ_{i+1}^{φ′_{i+1}}, …, τ_{n−1}^{φ′_{n−1}}⟩.

Then ∅ ⊢_{TAL} σ′_{1} ≤ σ′_{0} and ∅ ⊢_{TAL} σ′_{1} ≤ σ_{1}. Since ⊢_{TAL} H : Ψ, we may deduce

that m = n and Ψ; ∅ ⊢_{TAL} w_{j} : τ_{j}^{φ′_{j}} for 0 ≤ j < n. Let Ψ′ = Ψ{ℓ : σ′_{1}}. By

Heap Update it follows that Ψ′; ∅ ⊢_{TAL} w_{j} : τ_{j}^{φ′_{j}}. Using R̂ Typing and Heap Update, we may deduce that Ψ′; ∅ ⊢_{TAL} R(r_{s}) : τ_{i} wval, and by applying the (init) and (tuple) rules we may conclude that

Ψ′ ⊢_{TAL} ⟨w_{0}, …, w_{i−1}, R(r_{s}), w_{i+1}, …, w_{m−1}⟩ : σ′_{1} hval.

Hence ⊢_{TAL} H′ : Ψ′ by Heap Update.

(2) By Heap Update we may deduce that Ψ′ ⊢_{TAL} R : Γ. Recall that ∅ ⊢_{TAL} σ′_{1} ≤ σ_{1}. Thus, Ψ′; ∅ ⊢_{TAL} ℓ : σ_{1} wval, and by Register File Update we may conclude that Ψ′ ⊢_{TAL} R : Γ′ (since R = R{r_{d} ↦ ℓ}).

(3) By Heap Update, Ψ′; ∅; Γ′ ⊢_{TAL} I.

Case unpack. T D has the form

⊢_{TAL} H : Ψ Ψ ⊢_{TAL} R : Γ Ψ; ∅; Γ ⊢_{TAL} v : ∃α.τ′ Ψ; α; Γ{r:τ′} ⊢_{TAL} I Ψ; ∅; Γ ⊢_{TAL} unpack[α, r], v; I ⊢_{TAL} P.

By the operational semantics, P′ = (H, R′, I′) where R′ = R{r ↦ w}, I′ = I[τ/α] and R̂(v) = pack [τ, w] as ∃α.τ′. Then

(1) ⊢_{TAL} H : Ψ is in T D.

(2) By Canonical Forms it follows from Ψ; ∅; Γ ⊢_{TAL} v : ∃α.τ′ that Ψ; ∅ ⊢_{TAL} w : τ′[τ/α] wval. Let Γ′ = Γ{r : τ′[τ/α]}. By Register File Update if follows that Ψ ⊢_{TAL} R′ : Γ′.

(3) By Type Substitution it follows from Ψ; α; Γ{r:τ′} ⊢_{TAL} I that Ψ; ∅; Γ′ ⊢_{TAL} I′.

Theorem (Progress). If ⊢_{TAL} P then either there exists P′ such that P ⟼ P′, or P is of the form (H, R{r1 ↦ w}, halt[τ]) (and, moreover, Ψ; ∅ ⊢_{TAL} w : τ wval for some Ψ such that ⊢_{TAL} H : Ψ).

Proof. Suppose P = (H, R, I_{full}). Let T D be the derivation of ⊢_{TAL} P. The proof is by cases on the first instruction of I_{full}. Case halt T D has the form

⊢_{TAL} H : Ψ Ψ; ∅ ⊢_{TAL} R : Γ Ψ; ∅; Γ ⊢_{TAL} r1 : τ Ψ; ∅; Γ ⊢_{TAL} halt[τ] ⊢_{TAL} (H, R, halt[τ]).

By R̂ Typing we may deduce that R̂(r1) is defined and Ψ; ∅ ⊢_{TAL} R̂(r1) : τ wval. In other words, R = R′{r1 ↦ w} and Ψ; ∅ ⊢_{TAL} w : τ wval. Case add, mul, sub T D has the form

⊢_{TAL} H : Ψ Ψ; ∅ ⊢_{TAL} R : Γ Ψ; ∅; Γ ⊢_{TAL} r_{s} : int Ψ; ∅; Γ ⊢_{TAL} v : int ⋯ Ψ; ∅; Γ ⊢_{TAL} arith_{p} r_{d}, r_{s}, v; I ⊢_{TAL} (H, R, I_{full}).

By Canonical Forms, R(r_{s}) and R(v) each represent integer literals. Hence P ⟼ (H, R{r_{d} ↦ R(r_{s}) p R̂(v)}, I). Case bnz T D has the form

⊢_{TAL} H : Ψ Ψ; ∅ ⊢_{TAL} R : Γ Ψ; ∅; Γ ⊢_{TAL} r : int Ψ; ∅; Γ ⊢_{TAL} v : ∀[ ].Γ′ ⋯ Ψ; ∅; Γ ⊢_{TAL} bnz r, v; I ⊢_{TAL} (H, R, I_{full}).

By Canonical Forms, R(r) is an integer literal and R̂(v) = ℓ[σ_{1}, …, σ_{n}] with H(ℓ) = code[α_{1}, …, α_{n}].Γ′′.I′. If R(r) = 0 then P ⟼ (H, R, I). If R(r) ≠ 0 then P ⟼ (H, R, I′[σ⃗/α⃗]). Case jmp T D has the form

⊢_{TAL} H : Ψ Ψ; ∅ ⊢_{TAL} R : Γ Ψ; ∅; Γ ⊢_{TAL} v : ∀[ ].Γ′ ⋯ Ψ; ∅; Γ ⊢_{TAL} jmp v ⊢_{TAL} (H, R, I_{full}).

By Canonical Forms, R̂(v) = ℓ[σ_{1}, …, σ_{n}] with H(ℓ) = code[α_{1}, …, α_{n}].Γ′′.I′. Hence P ⟼ (H, R, I′[σ⃗/α⃗]). Case ld T D has the form

⊢_{TAL} H : Ψ Ψ; ∅ ⊢_{TAL} R : Γ Ψ; ∅; Γ ⊢_{TAL} r_{s} : ⟨τ_{0}^{φ_{0}}, …, τ_{n−1}^{φ_{n−1}}⟩ ⋯ Ψ; ∅; Γ ⊢_{TAL} ld r_{d}, r_{s}[i]; I (1 ≤ i < n) ⊢_{TAL} (H, R, I_{full}).

By Canonical Forms, R(r_{s}) = ℓ with H(ℓ) = ⟨w_{0}, …, w_{n−1}⟩. Hence P ⟼ (H, R{r_{d} ↦ w_{i}}, I). Case malloc Suppose that I_{full} is of the form malloc r[τ_{1}, …, τ_{n}]; I. Then P ⟼ (H{ℓ ↦ ⟨?τ_{1}, …,?τ_{n}⟩}, R{r ↦ ℓ}, I) for some ℓ ∉ H. Case mov T D has the form

⊢_{TAL} H : Ψ Ψ; ∅ ⊢_{TAL} R : Γ Ψ; ∅; Γ ⊢_{TAL} v : τ ⋯ Ψ; ∅; Γ ⊢_{TAL} mov r, v; I ⊢_{TAL} (H, R, I_{full}).

By R̂ Typing, R̂(v) is defined. Hence P ⟼ (H, R{r ↦ R̂(v)}, I). Case st T D has the form

⊢_{TAL} H : Ψ Ψ; ∅ ⊢_{TAL} R : Γ Ψ; ∅; Γ ⊢_{TAL} r_{d} : ⟨τ_{0}^{φ_{0}}, …, τ_{n−1}^{φ_{n−1}}⟩ Ψ; ∅; Γ ⊢_{TAL} r_{s} : τ_{i} ⋯ Ψ; ∅; Γ ⊢_{TAL} st r_{d}[i], r_{s}; I (1 ≤ i < n) ⊢_{TAL} (H, R, I_{full}).

By Canonical Forms, R(r_{d}) = ℓ with H(ℓ) = ⟨w_{0}, …, w_{n−1}⟩. By R̂ Typing, R(r_{s}) is defined. Hence P ⟼ (H{ℓ ↦ ⟨w_{0}, …, w_{i−1}, R(r_{s}), w_{i+1}, …, w_{n−1}⟩}, R, I). Case unpack T D has the form

⊢_{TAL} H : Ψ Ψ; ∅ ⊢_{TAL} R : Γ Ψ; ∅; Γ ⊢_{TAL} v : ∃α.τ ⋯ Ψ; ∅; Γ ⊢_{TAL} unpack[α, r], v; I ⊢_{TAL} (H, R, I_{full}).

By Canonical Forms, R̂(v) = pack [τ′, w] as ∃α.τ. Hence P ⟼ (H, R{r ↦ w}, I[τ′/α]).

ACKNOWLEDGEMENTS

We are grateful to Dan Grossman, Dexter Kozen, Frederick Smith, Stephanie Weirich, Steve Zdancewic, and the anonymous referees for their many helpful comments and suggestions.

REFERENCES Appel, A. 1992. Compiling with Continuations. Cambridge University Press, New York, NY,

USA. Appel, A. W. and MacQueen, D. B. 1991. Standard ML of New Jersey. In 3rd International

Symposium on Programming Language Implementation and Logic Programming, M. Wirsing, Ed. Springer-Verlag, New York, NY, USA, 1–13. Volume 528 of Lecture Notes in Computer Science.

Bershad, B., Savage, S., Pardyak, P., Sirer, E., Fiuczynski, M., Becker, D., Chambers, C., and Eggers, S. 1995. Extensibility, safety and performance in the SPIN operating system. In 15th ACM Symposium on Operating Systems Principles. ACM Press, New York, NY, USA, 267–284.

Birkedal, L., Rothwell, N., Tofte, M., and Turner, D. 1993. The ML Kit (version 1). Tech.

Rep. 93/14, Department of Computer Science, University of Copenhagen. Boehm, H. and Weiser, M. 1988. Garbage collection in an uncooperative environment. Softw.

Pract. Exper. 18, 9 (Sept.), 807–820. Crary, K. and Weirich, S. 1999. Flexible type analysis. In 1999 ACM SIGPLAN International

Conference on Functional Programming. ACM Press, New York, NY, USA. To appear. Crary, K., Weirich, S., and Morrisett, G. 1998. Intensional polymorphism in type-erasure

semantics. In 1998 ACM SIGPLAN International Conference on Functional Programming. ACM Press, New York, NY, USA, 301–312.

Danvy, O. and Filinski, A. 1992. Representing control: a study of the CPS transformation.

Math. Struct. Comput. Sci. 2, 4 (Dec.), 361–391. Dimock, A., Muller, R., Turbak, F., and Wells, J. B. 1997. Strongly typed flow-directed

reprsentation transformations. In 1997 ACM SIGPLAN International Conference on Functional Programming. ACM Press, New York, NY, USA, 85–98.

Girard, J.-Y. 1971. Une extension de l’interprétation de Gödel à l’analyse, et son application à l’élimination de coupures dans l’analyse et la théorie des types. In Proceedings of the Second Scandinavian Logic Symposium, J. E. Fenstad, Ed. North-Holland, 63–92.

Girard, J.-Y. 1972. Interprétation fonctionelle et élimination des coupures de l’arithmétique

d’ordre supérieur. Ph.D. thesis, Université Paris VII. Girard, J.-Y. 1987. Linear logic. Theor. Comput. Sci. 50, 1–102. Glew, N. 1999. Type dispatch for named hierarchical types. In 1999 ACM SIGPLAN Interna-

tional Conference on Functional Programming. ACM Press, New York, NY, USA. Glew, N. and Morrisett, G. 1999. Type-safe linking and modular assembly language. In 26th

ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages. ACM Press, New York, NY, USA, 250–261.

Harper, R. and Lillibridge, M. 1993. Explicit polymorphism and CPS conversion. In 20th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages. ACM Press, New York, NY, USA, 206–219.

Intel. 1996. Intel Architecture Software Developer’s Manual. Intel Corporation, P.O. Box 7641,

Mt. Prospect IL 60056-7641. Kranz, D., Kelsey, R., Rees, J., Hudak, P. R., Philbin, J., and Adams, N. 1986. ORBIT:

An optimizing compiler for Scheme. In Proceedings of the ACM SIGPLAN ’86 Symposium on Compiler Construction. ACM Press, New York, NY, USA, 219–233.

Launchbury, J. and Peyton Jones, S. 1995. State in Haskell. J. Lisp Symb. Comput. 8, 4

(Dec.), 293–341. Leroy, X. 1992. Unboxed objects and polymorphic typing. In 19th ACM SIGPLAN-SIGACT

Symposium on Principles of Programming Languages. ACM Press, New York, NY, USA, 177– 188.

Lindholm, T. and Yellin, F. 1996. The Java Virtual Machine Specification. Addison-Wesley,

Menlo Park, CA, USA. Milner, R., Tofte, M., Harper, R., and MacQueen, D. 1997. The Definition of Standard ML

(Revised). MIT Press, Cambridge, MA, USA. Minamide, Y., Morrisett, G., and Harper, R. 1996. Typed closure conversion. In 23rd ACM

SIGPLAN-SIGACT Symposium on Principles of Programming Languages. ACM Press, New York, NY, USA, 271–283.

Mitchell, J. and Plotkin, G. 1988. Abstract types have existential type. ACM Trans. Program.

Lang. Syst. 10, 3 (July), 470–502. Morrisett, G., Crary, K., Glew, N., Grossman, D., Samuels, R., Smith, F., Walker, D.,

Weirich, S., and Zdancewic, S. 1999. TALx86: A realistic typed assembly language. In ACM SIGPLAN Workshop on Compiler Support for System Software. INRIA Research Report, vol. 0228. INRIA, Centre de Diffusion, INRIA, BP 105-78153 Le Chesnay Cedex, France.

Morrisett, G., Crary, K., Glew, N., and Walker, D. 1998. Stack-based typed assembly language. In Workshop on Types in Compilation. Lecture Notes in Computer Science, vol. 1473. Springer-Verlag, Berlin, Germany, 28–52.

Morrisett, G. and Harper, R. 1997. Semantics of memory management for polymorphic languages. In 1st Workshop on Higher Order Operational Techniques in Semantics, A. Gordon and A. Pitts, Eds. Publications of the Newton Institute. Cambridge University Press, Cambridge, UK.

Morrisett, G., Tarditi, D., Cheng, P., Stone, C., Harper, R., and Lee, P. 1996. The TIL/ML compiler: Performance and safety through types. In ACM SIGPLAN Workshop on Compiler Support for System Software.

Necula, G. 1997. Proof-carrying code. In 24th ACM SIGPLAN-SIGACT Symposium on Prin-

ciples of Programming Languages. ACM Press, New York, NY, USA, 106–119. Necula, G. 1998. Compiling with proofs. Ph.D. thesis, School of Computer Science, Carnegie

Mellon University, Pittsburgh, PA 15213-3891, USA. Also available as CMU technical report CMU-CS-98-154.

Necula, G. and Lee, P. 1996. Safe kernel extensions without run-time checking. In 2nd USENIX Symposium on Operating System Design and Implementation. USENIX Assocation, Berkeley, CA, USA, 229–243.

Peyton Jones, S., Hall, C., Hammond, K., Partain, W., and Wadler, P. 1993. The Glasgow Haskell compiler: a technical overview. In Proc. UK Joint Framework for Information Technology (JFIT) Technical Conference. 249–257.

Reynolds, J. 1974. Towards a theory of type structure. In Programming Symposium. Lecture

Notes in Computer Science, vol. 19. Springer-Verlag, New York, NY, USA, 408–425. Shao, Z. 1997. An overview of the FLINT/ML compiler. In Workshop on Types in Compila-

tion. Boston College Computer Science Department Technical Report, vol. BCSS-97-03. Boston College, Fulton Hall, Room 460, Chestnull Hill, MA 02467-3808, USA.

Steele, Jr., G. 1978. Rabbit: A compiler for Scheme. M.S. thesis, MIT. Tarditi, D., Morrisett, G., Cheng, P., Stone, C., Harper, R., and Lee, P. 1996. TIL: A

type-directed optimizing compiler for ML. In ACM SIGPLAN Conference on Programming Language Design and Implementation. ACM Press, New York, NY, USA, 181–192.

Tolmach, A. 1994. Tag-free garbage collection using explicit type parameters. In 1994 ACM

Conference on Lisp and Functional Programming. ACM Press, New York, NY, USA, 1–11. Wadler, P. 1990a. Comprehending monads. In 1990 ACM Conference on Lisp and Functional

Programming. ACM Press, New York, NY, USA, 61–78. Wadler, P. 1990b. Linear types can change the world! In Programming Concepts and Meth-

ods, M. Broy and C. Jones, Eds. North-Holland, Sea of Galilee, Israel. IFIP TC 2 Working Conference.

Wadler, P. 1993. A taste of linear logic. In Mathematical Foundations of Computer Science.

Lecture Notes in Computer Science, vol. 711. Springer-Verlag, Gdansk, Poland. Wand, M. 1992. Correctness of procedure representations in higher-order assembly language. In

Proceedings Mathematical Foundations of Programming Semantics ’91, S. Brookes, Ed. Lecture Notes in Computer Science, vol. 598. Springer-Verlag, New York, NY, USA, 294–311.

Xi, H. 1999. Dependent types in practical programming. Ph.D. thesis, School of Computer

Science, Carnegie Mellon University, Pittsburgh, PA 15213-3891, USA. Xi, H. and Pfenning, F. 1998. Eliminating array bound checking through dependent types.

In ACM SIGPLAN Conference on Programming Language Design and Implementation. ACM Press, New York, NY, USA, 249–257.

Xi, H. and Pfenning, F. 1999. Dependent types in practical programming. In 26th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages. ACM Press, New York, NY, USA, 214–227.

Received December 1997; revised December 1998; accepted March 1999