Closure conversion Compiling λ.

Slides:



Advertisements
Similar presentations
Closures & Environments CS153: Compilers Greg Morrisett.
Advertisements

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
The University of Adelaide, School of Computer Science
Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
CS 312 Spring 2004 Lecture 18 Environment Model. Substitution Model Represents computation as doing substitutions for bound variables at reduction of.
CS 312 Spring 2002 Lecture 16 The Environment Model.
Catriel Beeri Pls/Winter 2004/5 environment 68  Some details of implementation As part of / extension of type-checking: Each declaration d(x) associated.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
7/2/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Closure and Environment Compiler Baojian Hua
1 Saves repeated renaming and substitution: explicit substitution is replaced by variable bindings using new data structures (frame, environment). Can.
EOPL3: Section 3.3 PROC and App B: SLLGEN
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
10/14/20151 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Plt /17/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.8 Parameter-Passing Variations.
Chap 9 Run-time Storage Consider the C program : static int x; int abc(int y) { float z; static int w;  abc(z);  malloc(20) } main {  abc(3)
1 Lecture 14: Assignment and the Environment Model (EM)
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
A LISP interepreter for the Lambda Calculus Functional Programming
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Programming with ANSI C ++
The interpreter.
Programming Languages and Compilers (CS 421)
The Environment Model*
CS 153: Concepts of Compiler Design November 28 Class Meeting
Implementing Recursion
Env. Model Implementation
SSA in Scheme Static single assignment (SSA) :
This pointer, Dynamic memory allocation, Constructors and Destructor
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
6.001 SICP Data abstractions
First-class continuations
The Metacircular Evaluator
Programming Languages and Compilers (CS 421)
Closure Representations in Higher-Order Programming Languages
Continuations and Compilation
Dynamic Scoping Lazy Evaluation
And more store-passing interpreters
Procedures App B: SLLGEN 1.
Programming Language Principles
6.001 SICP Environment model
Continuation-passing Style (CPS)
Continuation Marks A john clements talk.
Continuations in Scheme
Extending our interpreted language with Scheme’s set!
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
6.001 SICP Environment model
UNIT V Run Time Environments.
Madhusudan Parthasarathy
Languages and Compilers (SProg og Oversættere)
COP 3330 Object-oriented Programming in C++
Programming Languages and Compilers (CS 421)
6.001 SICP Environment model
Assignments and Procs w/Params
Scope, Function Calls and Storage Management
Recursive Procedures and Scopes
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Principles of Programming Languages
Principles of Programming Languages
Chengyu Sun California State University, Los Angeles
CMPE 152: Compiler Design May 2 Class Meeting
Presentation transcript:

Closure conversion Compiling λ

λ try, finally, dynamic-wind raise, guard let loop, for, while continue, break Call/cc, call/ec, return λ threads, coroutines Function calls closure objects (code ptr, environment)

Closure conversion In strict CPS, lambdas no longer return. Function calls are just first-class goto’s that take arguments and carry values for free variables in an environment. Closure conversion: Hoists all functions to the top-level. Allocates closures that save the current environment. Function calls explicitly pass the closure’s env. Replaces references to free variables with env access.

Closure conversion (λ (a b) …) new Lambda43(x,y,z); FV((λ (a b) …)) class Lambda43 : public Lam { private: const u64 x,y,z; public: Lambda43(u64 x, u64 y, u64 z) : x(x), y(y), z(z) {} u64 apply(u64 a, u64 b) … } }; FV((λ (a b) …)) = {x,y,z}

Closure conversion class Lambda43 : public Lam { private: vtable* x y z Lambda43::apply class Lambda43 : public Lam { private: const u64 x,y,z; public: Lambda43(u64 x, u64 y, u64 z) : x(x), y(y), z(z) {} u64 apply(u64 a, u64 b) … } };

Closure conversion: two principal strategies Top-down closure conversion: Traverses the AST and turns lambdas into closures on the way down (computing environments as it goes). Produces linked environments/closures. Pros: compact, shared environments; Cons: slower. Bottom-up closure conversion: Traverses the AST and turns lambdas into closures on the way back up (computing free vars as it goes). Produces flat environments/closures. Pros: fast, computes free vars; Cons: more copying.

… (λ (a) (λ (b c) … (λ (d) … (f a c) …) …) …) …

Top-down closure conversion As the AST is traversed, a set of locally bound variables are computed. A map from non-locally bound variables to their access expressions is maintained so that variables can be looked up in an environment. At each lambda: the algorithm lifts the lambda to a first-order C-like procedure, allocates a new closure and its environment, then converts it’s body using an updated map for non-locally bound variables (with paths into this newly defined environment). Previously allocated environment is linked-to/shared.

… (λ (a) bound vars: x (λ (b c) … (λ (d) … (f a c) …) …) …) …

(proc (lam0 env0 a) (proc (main) … (vector … lam0 (λ (b c) (prim vector x)) …) … (λ (b c) (λ (d) … (f a c) …) …) …) env mapping: x -> (vector-ref env0 ‘0)

(proc (lam0 env0 a) (proc (main) … (prim vector … lam0 (prim vector x)) …) … (prim vector lam1 env0 a y)) …) (proc (lam1 env1 b c) bound vars: env0,a,y … (λ (d) env mapping: x -> (vector-ref env0 ‘0) env0 -> (vector-ref env1 ‘0) a -> (vector-ref env1 ‘1) y -> (vector-ref env1 ‘2) (f a c) …) …)

Bottom-up closure conversion As the AST is traversed, free variables are computed. At each lambda: 1) the algorithm converts any lambdas under the lambda’s body first (and also computes a set of free variables); then 2) it emits code to allocate the lambda’s closure/environment and replaces free vars with env access. Converting the body of a lambda yields a set of free variables that can be canonically ordered. Closures are flat heap-allocated vectors containing a function pointer and then each free var in order. Accesses of free variables are turned into a vector-ref with the predetermined index.

… (λ (a) (λ (b c) … (λ (d) … free vars: a,c,f (f a c) …) …) …) …

allocates flat closure (proc (lam14 env d) … (clo-app (prim vector-ref env ‘3) ;f env ‘1) ;a env ‘2)) ;c …) … (λ (a) (λ (b c) … adds first-order proc (prim vector lam14 a c f) allocates flat closure …) …) …) …

references at closure allocation … (λ (a) (λ (b c) … (prim vector lam14 a c f) free vars: a f x y { references at closure allocation can remain free …) …) …) …

(let ([f-clo (prim vector-ref env ‘3)]) (clo-app (prim vector-ref env ‘3) ;f env ‘1) ;a env ‘2)) ;c (let ([f-clo (prim vector-ref env ‘3)]) (let ([f-ptr (prim vector-ref f-clo ‘0)]) (let ([a (prim vector-ref env ‘1)]) (let ([c (prim vector-ref env ‘2)]) (C-style-call f-ptr f-clo a c))))) application: 1) function pointer is accessed from closure 2) closure (f-clo) is passed to invoked function ptr

Let’s live code bottom-up closure conversion.