Scope, Functions, and Storage Management

Similar documents
Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

Procedure and Object- Oriented Abstraction

Topic 7: Activation Records

CA Compiler Construction

Concepts Introduced in Chapter 7

Weeks 6&7: Procedures and Parameter Passing

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Run-time Environments

Run-time Environments

Lecture08: Scope and Lexical Address

Run-time Environments - 2

Control in Sequential Languages

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

Implementing Subprograms

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

INF3110 Programming Languages Runtime Organization part II

Run-time Environments - 3

G53CMP: Lecture 14. Run-Time Organisation I. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 14 p.1/37

G Programming Languages - Fall 2012

This Lecture. G53CMP: Lecture 14 Run-Time Organisation I. Example: Lifetime (1) Storage Areas

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Run-Time Data Structures

Parsing Scheme (+ (* 2 3) 1) * 1

Runtime Environments I. Basilio B. Fraguela

CS 314 Principles of Programming Languages. Lecture 13

CIT Week13 Lecture

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

CS 345. Exceptions. Vitaly Shmatikov. slide 1

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Types, Type Inference and Unification

Recap: Functions as first-class values

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Run-Time Environments

Code Generation & Parameter Passing

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

The Procedure Abstraction

CSE 504: Compiler Design. Runtime Environments

Variables and Bindings

Implementing Subroutines. Outline [1]

CSE 307: Principles of Programming Languages

Lecture 9: Procedures & Functions. CS 540 George Mason University

Today's Topics. CISC 458 Winter J.R. Cordy

Run-time Environment

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

Chapter 9 :: Subroutines and Control Abstraction

System Software Assignment 1 Runtime Support for Procedures

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

CSE 504: Compilers. Expressions. R. Sekar

Compilers and Code Optimization EDOARDO FUSELLA

Contents. 8-1 Copyright (c) N. Afshartous

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Procedures and Run-Time Storage

Types and Type Inference

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Run Time Environments

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Module 27 Switch-case statements and Run-time storage management

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Procedures and Run-Time Storage

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Principles of Programming Languages

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Administration CS 412/413. Advanced Language Support. First-class vs. Second-class. First-class functions. Function Types

Qualifying Exam in Programming Languages and Compilers

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

CS558 Programming Languages

Special Topics: Programming Languages

CS 320: Concepts of Programming Languages

Run-Time Environements

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Scope and Parameter Passing 1 / 19

DO NOT OPEN UNTIL INSTRUCTED

INF 212 ANALYSIS OF PROG. LANGS ELEMENTS OF IMPERATIVE PROGRAMMING STYLE. Instructors: Crista Lopes Copyright Instructors.

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

Chapter 14 Functions

CSC 2400: Computer Systems. Using the Stack for Function Calls

CS558 Programming Languages

Homework 5. Notes. Turn-In Instructions. Reading. Problems. Handout 19 CSCI 334: Spring (Required) Read Mitchell, Chapters 6 and 7.

CS 4110 Programming Languages & Logics

Names, Bindings, Scopes

INF 212 ANALYSIS OF PROG. LANGS PROCEDURES & FUNCTIONS. Instructors: Kaj Dreef Copyright Instructors.

CS 4110 Programming Languages & Logics. Lecture 35 Typed Assembly Language

CS558 Programming Languages. Winter 2013 Lecture 4

Functions and Procedures

Review of Activation Frames. FP of caller Y X Return value A B C

CS558 Programming Languages. Winter 2013 Lecture 3

Compiler Construction Lecture 05 A Simple Stack Machine. Lent Term, Lecturer: Timothy G. Griffin. Computer Laboratory University of Cambridge

CS 314 Principles of Programming Languages

Transcription:

Scope, Functions, and Storage Management Implementing Functions and Blocks cs3723 1

Simplified Machine Model (Compare To List Abstract Machine) Registers Code Data Program Counter (current instruction) Environment Pointer (current scope) Stack---- map variables to their values Heap---- dynamically allocated data cs3723 2

Data Storage Management Runtime stack: mapping variables to their values When introducing new variables: push new stores to stack When variables are out of scope: pop outdated storages Environment pointer: current stack position Used to keep track of storages of all active variables Heap: dynamically allocated data of varying lifetime Variables that last throughout the program Data pointed to by variables on the runtime stack Target of garbage collection The code space: the whole program to evaluate Program counter: current/next instruction to evaluate keep track of instructions being evaluated Registers: temporary storages for variables cs3723 3

Blocks in C/C++ outer block { } int x = 2; { int y = 3; x = y+2; } inner block Blocks: regions of code that introduces new variables Enter block: allocate space for variables Exits block: some or all space may be deallocated Blocks are nested but not partially overlapped Jumping out of a block Make sure variables are freed before exiting What about jumping into the middle of a block? Variables in the block have not yet been allocated cs3723 4

Blocks in Functional languages ML: let fun g(y) = y + 3 in let fun h(z) = g(g(z)) in h(3) end end; Lisp: ( (lambda (g) ((lambda (h) (h 3)) (lambda (z) (g (g z)))) (lambda (y) (+ y 3))) cs3723 5

Summary of Blocks Blocks in common languages C { } Algol begin end ML let in end Two forms of blocks In-line blocks Blocks associated with functions or procedures Topic: block-based memory management cs3723 6

Managing Data Storage In a Block Local variables Declared inside the current block Enter block: allocate space Exit block: de-allocate space Global variables Declared in a previously entered block Already allocated before entering current Block Remain allocated after exiting current block Function parameters Input parameters Allocated and initialized before entering function body De-allocated after exiting function body Return values Address remembered before entering function body Value set after exiting function body Scoping rules: where to find memory allocated for variables? Need to find the block that introduced the variable cs3723 7

Parameter passing Each function have a number of formal parameters At invocation, they are matched against actual parameters Pass-by name Rename each occurrence of formal parameter with its actual parameter --- delay of evaluation Used in Lambda calculus and side-effect free languages Pass-by-value Replace formal parameter with value of its actual parameter Callee cannot change values of actual parameters Pass-by-reference Replace formal parameter with address of its actual parameter Callee can change values of actual parameters Different formal parameters may have the same location cs3723 8

Example: What is the final result? pseudo-code int f (int x) { x := x+1; return x; }; main() { int y = 0; print f(y)+y; } pass-by-ref pass-by-value Standard ML fun f (x : int ref) = ( x :=!x+1;!x ); val y = ref 0 : int ref; f(y) +!y; fun f (z : int) = let val x = ref z in x :=!x+1;!x end; val y = ref 0 : int ref; f(!y) +!y; cs3723 9

Scoping rules Finding non-local (global) variables Global and local variables Which x? outer block x 0 { int x=0; fun g(z) = x+z; h(3) z 3 fun h(z) = let x = 1 in x 1 g(z) end; g(12) z 3 h(3) Static scope }; Find global declarations in the closest enclosing blocks in program text Dynamic scope Find global variables in the most recent activation record cs3723 10

Managing Blocks Activation record: memory storage for each block Contains values for local variables in the block Managing Activation Records Allocated on a runtime stack: First-In-Last-Out Before evaluating each block, push its activation record onto runtime stack; after exit the block, pop its activation record off stack Compilers generate instructions for pushing & popping of activation records (pre-compute their sizes) Finding locations of local variables Compiler calculate the offset of each variable Dynamically find activation record of introducing block Location = activation record pointer + offset cs3723 11

Activation Record For Inline Blocks Control link Access link Local variables Environment Pointer Control link Point to activation record of previous (calling) block Depend on runtime behavior Support push/pop of ARs Access link Point to activation record of immediate enclosing block Depend on static form of program Push record on stack Set new control link to env ptr Set env ptr to new record Pop record off stack Follow current control link to reset environment pointer cs3723 12

Activation Records For Functions Control link Access link Return address Return-result addr Parameters Local variables Environment Pointer Return address Where to continue execution after return Pointer to the next instruction following the function call Return-result address Where to put return result Pointer to caller s activation record Parameters Values for formal parameters Initialized with the actual parameters cs3723 13

Function Abstraction As Values 1 2 3 4 5 6 7 let val x=1; fun g(z) = x+z; fun h(z) = in h(3) end; let x = 2 in g(z) end What are values for g,h? How to determine their access links? Inlined blocks Access link = control link Function blocks Enclosing block of the function definition Outer h(3) line6 g(z) line5 control link access link x 1 g h tmp? control link access link return address return result adr z 3 x 2 tmp? control link access link return address return result adr z 3 Code for h cs3723 14 Code for g

Closures A function value is a closure: (env, code) code: a pointer to the function body env: activation record of the enclosing block Use closure to maintain a pointer to the static environment of a function body When called, set access link from closure When a function is called, Retrieve the closure of the function Push a new activation record onto runtime stack Set return address, return value addr, parameters and local variables Set access link to equal to the env pointer in closure Start the next instruction from code pointer in closure cs3723 15

Example: Function Calls fact(k) Control link Access link Return address Return-result addr n k fact(n-1) Environment pointer fact(n) = if n<= 1 then 1 else n * fact(n-1) fact(3) fact(2) fact(1) Control link Access link Return addr Return-result addr n 3 Fact(n-1) Control link Access link Return addr Return-result addr n 2 Fact(n-1) Control link Access link Return addr Return-result addr n 1 Fact(n-1) cs3723 16

Return Function as Result Language feature: functions that return new functions E.g. fun compose(f,g) = (fn x => g(f x)); Each function value is a closure = (env, code), where code may contain references to variables in env Code is not created dynamically (static compilation) Use a closure to save the runtime environment of function Environment: pointer to enclosing activation records But the enclosing activation record may have been popped off the runtime stack Returning functions as results is not allowed in C Just like returning pointers to local variables Need to extend the standard stack implementation Put activation records on heap Invoke garbage collector as needed Not as crazy as is sounds cs3723 17

Tail Call And Tail Recursion A function call from g to f is a tail call if g returns the result of calling f with no further computation Example fun g(x) = if x>0 then f(x) else f(x)*2 Optimization tail call not a tail call Can pop activation record on a tail call Especially useful for a tail recursive call (f to f) Callee s activation record has exactly same form Callee can reuse activation record of the caller A sequence of tail recursive calls can be translated into a loop cs3723 18

Example: what is the result? f(1,3) control return val x 1 y 3 fun f(x,y) = if x>y then x else f(2*x, y); f(1,3) + 7; control return val x 1 y 3 control return val x 2 y 3 control return val x 4 y 3 Expressed in loop: fun f(x,y) = let val z = ref x in while not (!z >y) do z := 2 *!z;!z end; f(1,3) + 7; cs3723 19

Tail recursion elimination f(1,3) control return val x 1 y 3 f(2,3) control return val x 2 y 3 f(4,3) control return val x 4 y 3 fun f(x,y) = if x>y then x else f(2*x, y); f(1,3); Optimization: pop followed by push => reuse activation record in place Conclusion: tail recursive function calls are equivalent to iterative loops cs3723 20

Tail recursion and iteration Tail recursive function fun last(x::nil) = x last( x::y) = last(y); Iteration fun last(input) = let val y= ref input in while not(tl(!y)=nil) do y := tl(!y) end; hd(!y) end Step1: what parameters change when making recursive calls? create a reference for each changed parameter. NOTE: no need to create reference for the return result Tail recursion only returns at the base case Step2: what is the base case of recursion? This is the stop condition for the while loop. Step3: what to do before making tail call? loop body: prepare for the next tail call Step4: return base case value. cs3723 21

Summary Block-structured languages use runtime stack to maintain activation records of blocks Activation records contain parameters, local variables, Also pointers to enclosing scope Several different parameter passing mechanisms Tail calls may be optimized Function parameters/results require closures Env pointer of closure used when function is called Runtime stack management may fail if functions are returned as result Closures is not needed if functions are not in nested blocks Example: C cs3723 22