Closure Compiler Baojian Hua Closure An implementation technique traditionally for functional languages Lisp, Scheme, Haskell, Ocaml,

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

Computer Architecture CSCE 350
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Standard ML- Part I Compiler Baojian Hua
Subprograms The basic abstraction mechanism. Functions correspond to the mathematical notion of computation input output procedures affect the environment,
Informática II Prof. Dr. Gustavo Patiño MJ
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
Subprograms The basic abstraction mechanism. Functions correspond to the mathematical notion of computation input output procedures affect the environment,
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #17, March 12, 2007 Procedure Abstraction, Name Spaces, Scoping Rules, Activation Records,
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
Function Compiler Baojian Hua Function, or Procedure, or method, or … High-level abstraction of code logically-grouped Good for many.
Run time vs. Compile time
Catriel Beeri Pls/Winter 2004/5 environment 68  Some details of implementation As part of / extension of type-checking: Each declaration d(x) associated.
Semantics of Calls and Returns
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:
C and Data Structures Baojian Hua
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
Closure and Environment Compiler Baojian Hua
Memory Layout C and Data Structures Baojian Hua
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
Slide 1 Vitaly Shmatikov CS 345 Scope and Activation Records.
C FAQ’S Collected from the students who attended technical round in TCS recruitment.
Chapter 8 :: Subroutines and Control Abstraction
ESP int f(int x) {.... } int g(int y) { …. f(2); …. } int main() { …. g(1); …. } EIP 100: 200: 250: 300: 350:
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Runtime Environments Compiler Construction Chapter 7.
1 Comp 104: Operating Systems Concepts Java Development and Run-Time Store Organisation.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Chapter 8 - Control II: Procedures and Environments
10/14/20151 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
Exception Compiler Baojian Hua
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Exception Compiler Baojian Hua
Buffer Overflow Attack Proofing of Code Binary Gopal Gupta, Parag Doshi, R. Reghuramalingam, Doug Harris The University of Texas at Dallas.
Memory Layout Compiler Baojian Hua
CMSC 330: Organization of Programming Languages Functional Programming with OCaml.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
CSE 5317/4305 L12: Higher-Order Functions1 Functional Languages and Higher-Order Functions Leonidas Fegaras.
Object Model Baojian Hua What ’ s an Object Model? An object model dictates how to represent an object in memory A good object.
Scope, Function Calls and Storage Management John Mitchell CS Reading: Chapter 7, Concepts in Programming Languages.
Memory Management in Java Mr. Gerb Computer Science 4.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
1 Topic 6: Activation Records COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer.
Advanced Programming in C
Instructions for test_function
Storage Allocation Mechanisms
Names and Attributes Names are a key programming language feature
Implementing Subprograms
Subprograms The basic abstraction mechanism.
Run-Time Storage Organization
Run-Time Storage Organization
Introduction to Compilers Tim Teitelbaum
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Strategies for automatic memory management
Understanding Program Address Space
PZ09A - Activation records
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scope, Function Calls and Storage Management
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

Closure Compiler Baojian Hua

Closure An implementation technique traditionally for functional languages Lisp, Scheme, Haskell, Ocaml, F#, … But more popular in other (even OO) languages C++ 0x11, Lambda C# 3.5 Java 8.0 (this June?)

Call Stack LIFO discipline to support (recursive) function call Function frame also obeys LIFO, so most (C) compilers use call stack to hold frames frame 0 high address %ebp frame 1 frame 2 %esp low address

C-style Stack Frame int f (int arg0, int arg1, …) { int local1; int local2; …; } … arg1 arg0 ret addr old ebp local1 local2 … %ebp %esp

Higher-order functions Functions as values (first-class) Pass as arguments Return as values Stored into data structures Implementation: A code pointer, (i.e., a code heap address)

Nested functions int f(){ int x; int y; g(); return 0; } int g (){ int z; return z+x; } int f(){ int x; int y; g(); return 0; } Escaped variable We say function f is declared at level 0, and g is at level 1. General principal to compile nested functions: flatten! int g (){ int z; return z+x; } Ooops, where to find “ x ” ? But “ x ” is still in “ f ”’ s frame, right?

Static Links frame 0 xyxy Link z low address int f(){ int x; int y; g(%ebp); return 0; } int g (Link){ int z; return z+ link->x; } f g

Higher-order Nested Functions void->int f(){ int x; int y; return g; } h = f(); // h==g h(); // g() int g (){ int z; return z+x; } frame 0 xyxy f z g Function frames don ’ t obey LIFO discipline any more. What one need to do is to we need to keep frames live long enough! Heap-allocation!

Heap-allocated Frames void->int f(){ int x; int y; return g; } h = f(); // h == cg h(); int g (){ int z; return z+x; } frame 0 ret ebp frame next x y envcode f g cg

Heap-allocated Frames void->int f(){ int x; int y; return g; } h = f(); // h == cg h(); int g (){ int z; return z+x; } frame 0 env ebp frame next x y envcode g g cg h->code(h->env);

Closure Conversion By heap allocating function frame, inner function can visit variable in outer function, even if the outer function has returned relying on the GC to reclaim frames We can also make the closure explicit at source-level this is called closure conversion

Closure Conversion void->int f(){ int x; int y; int g(struct f_fr *env){ int z; return z+(env->x); } struct f_fr *env = malloc (link, x, y); return malloc(env, g); } frame 0 link x y envcode f g struct f_fr{ … *link; int x; int y; }; Closures are objects! Objects are closures!

From Closure to Inner Class void->int f(){ int x; int y; int g(){ int z; return z+x; } return g; } h = f(); h(); class f{ int x; int y; class g(){ int z; int foo(){ return z+x; } int foo(){ return new g(); } h = new f().foo(); h.foo();

From Closure to Inner Class class f{ int x; int y; class g(){ int z; int foo(){ return z+x; } int foo(){ return new g(); } h = new f().foo(); h.foo(); vptr x outer z h y f_foo vptr g_foo

Method Local Inner Class class A{ int x; int f(){ int y; class B{ int g(){ return x+y; } return new B(); } To access “ y ” in the method “ g ”, y must be heap-allocated (notice y is a method local variable for method f). But this violates the JVM semantics, so the Java designer invents the ugly “ final ” rule …