Scope, Function Calls and Storage Management

Slides:



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

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #17, March 12, 2007 Procedure Abstraction, Name Spaces, Scoping Rules, Activation Records,
CS 536 Spring Run-time organization Lecture 19.
Dr. Muhammed Al-Mulhem ICS Chapter 7 Scope, Functions, and Storage Management.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Runtime Environments Source language issues Storage organization
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Scope, Function Calls and Storage Management John Mitchell CS
Run-time Environment and Program Organization
Week 8-9b spring 2002CSCI337 Organization of Prog. Lang0 A Brief Recap Subprogram benefits, basic characteristics from names to their values  names can.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Scope, Function Calls and Storage Management John Mitchell CS
Slide 1 Vitaly Shmatikov CS 345 Scope and Activation Records.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
CS 2104 Prog. Lang. Concepts Subprograms
Runtime Environments Compiler Construction Chapter 7.
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.
Basic Semantics Associating meaning with language entities.
Midterm Review John Mitchell CS 242. Midterm uThurs Nov 7, 7-9PM Terman Aud Closed book uClass schedule Thurs Oct 31, Tues Nov 5 – topics not on midterm.
CSC 8505 Compiler Construction Runtime Environments.
Slide 1 WEEK 8 Imperative Programming Original by Vitaly Shmatikov.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Scope, Function Calls and Storage Management John Mitchell CS Reading: Chapter 7, Concepts in Programming Languages.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Implementing Subprograms
Chapter 14 Functions.
Storage Allocation Mechanisms
Chapter 10 : Implementing Subprograms
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Implementing Subprograms
Implementing Subprograms
Subprograms The basic abstraction mechanism.
Run-time organization
Memory Locations For Variables
Run-Time Storage Organization
Run-Time Storage Organization
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Chapter 9 :: Subroutines and Control Abstraction
Implementing Subprograms
Implementing Subprograms
The University of Adelaide, School of Computer Science
Understanding Program Address Space
Implementing Subprograms
PZ09A - Activation records
Implementing Subprograms
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Yan Shi CS/SE 2630 Lecture Notes
Run Time Environments 薛智文
Runtime Environments What is in the memory?.
Where is all the knowledge we lost with information? T. S. Eliot
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

Scope, Function Calls and Storage Management CS 242 Scope, Function Calls and Storage Management John Mitchell

Revised class schedule Friday Oct 17 No lecture; discussion section as usual Friday Oct 24 No section Monday Oct 27 Review section during class meeting time, Gates B01 Wednesday Oct 29 No lecture Evening exam: 7PM, Gates B01 and B03

Topics Block-structured languages and stack storage In-line Blocks activation records storage for local, global variables First-order functions parameter passing tail recursion and iteration Higher-order functions deviations from stack discipline language expressiveness => implementation complexity

Block-Structured Languages Nested blocks, local variables Example { int x = 2; { int y = 3; x = y+2; } Storage management Enter block: allocate space for variables Exits block: some or all space may be deallocated new variables declared in nested blocks outer block global variable inner block local variable

Examples Blocks in common languages Two forms of blocks 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, access to local variables, parameters,global vars

Simplified Machine Model Registers Code Data Stack Program Counter Heap Environment Pointer

Interested in Memory Mgmt Only Registers, Code segment, Program counter Ignore registers Details of instruction set will not matter Data Segment Stack contains data related to block entry/exit Heap contains data of varying lifetime Environment pointer points to current stack position Block entry: add new activation record to stack Block exit: remove most recent activation record

Some basic concepts Scope Lifetime Region of program text where declaration is visible Lifetime Period of time when location is allocated to program { int x = … ; { int y = … ; { int x = … ; …. }; Inner declaration of x hides outer one. Called “hole in scope” Lifetime of outer x includes time when inner block is executed Lifetime  scope Lines indicate “contour model” of scope.

In-line Blocks Activation record Example Data structure stored on run-time stack Contains space for local variables Example Push record with space for x, y Set values of x, y Push record for inner block Set value of z Pop record for inner block Pop record for outer block { int x=0; int y=x+1; { int z=(x+y)*(x-y); }; May need space for variables and intermediate results like (x+y), (x-y)

Activation record for in-line block Control link pointer to previous record on stack Push record on stack: Set new control link to point to old env ptr Set env ptr to new record Pop record off stack Follow control link of current record to reset environment pointer Control link Local variables Intermediate results Control link Local variables Intermediate results Environment Pointer

Example Environment Pointer Control link x y 1 x+y x-y -1 z { int x=0; 1 x+y x-y Environment Pointer -1 z { int x=0; int y=x+1; { int z=(x+y)*(x-y); }; Push record with space for x, y Set values of x, y Push record for inner block Set value of z Pop record for inner block Pop record for outer block

Scoping rules Global and local variables Static scope Dynamic scope x, y are local to outer block z is local to inner bock x, y are global to inner block { int x=0; int y=x+1; { int z=(x+y)*(x-y); }; Static scope global refers to declaration in closest enclosing block Dynamic scope global refers to most recent activation record These are same until we consider function calls.

Functions and procedures Syntax of procedures (Algol) and functions (C) procedure P (<pars>) <type> function f(<pars>) begin { <local vars> <local vars> <proc body> <function body> end; }; Activation record must include space for parameters return address return value (an intermediate result) location to put return value on function exit

Activation record for function Return address Location of code to execute on function return Return-result address Address in activation record of calling block to receive return address Parameters Locations to contain data from calling block Control link Return address Return-result addr Parameters Local variables Intermediate results Environment Pointer

Example Function Return result address Parameter Intermediate result fact(n) = if n<= 1 then 1 else n * fact(n-1) Return result address location to put fact(n) Parameter set to value of n by calling sequence Intermediate result locations to contain value of fact(n-1) Control link Return address Return result addr Parameters Local variables Intermediate results Environment Pointer

Function call fact(n) = if n<= 1 then 1 else n * fact(n-1) Control link fact(n-1) n Return-result addr k fact(k) Environment Pointer Control link fact(n-1) n Return-result addr 3 fact(3) Control link fact(n-1) n Return-result addr 2 fact(2) Control link fact(n-1) n Return-result addr 1 fact(1) Function return next slide  fact(n) = if n<= 1 then 1 else n * fact(n-1) Return address omitted; would be ptr into code segment

Function return fact(n) = if n<= 1 then 1 else n * fact(n-1) Control link Control link Return result addr Return result addr n 3 n 3 fact(n-1) fact(n-1) 2 fact(2) fact(2) Control link Control link Return result addr Return result addr n 2 n 2 fact(n-1) 1 fact(n-1) 1 fact(1) Control link Return result addr fact(n) = if n<= 1 then 1 else n * fact(n-1) n 1 fact(n-1)

Topics for first-order functions Parameter passing use ML reference cells to describe pass-by-value, pass-by-reference Access to global variables global variables are contained in an activation record higher “up” the stack Tail recursion an optimization for certain recursive functions See this yourself: write factorial and run under debugger

ML imperative features (review) General terminology: L-values and R-values Assignment y := x+3 Identifier on left refers to location, called its L-value Identifier on right refers to contents, called R-value ML reference cells and assignment Different types for location and contents x : int non-assignable integer value y : int ref location whose contents must be integer !y the contents ref x expression creating new cell initialized to x ML form of assignment y := x+3 place value of x+3 in location (cell) y y := !y + 3 add 3 to contents of y and store in location y

Parameter passing Pass-by-reference Pass-by-value Caller places L-value (address) of actual parameter in activation record Function can assign to variable that is passed Pass-by-value Caller places R-value (contents) Function cannot change value of caller’s variable Reduces aliasing (alias: two names refer to same loc)

Example pseudo-code Standard ML pass-by-ref fun f (x : int ref) = ( x := !x+1; !x ); y = ref 0 : int ref; f(y) + !y; pass-by-ref function f (x) = { x := x+1; return x }; var y : int = 0; print f(y)+y; fun f (z : int) = let x = ref z in x := !x+1; !x end; y = ref 0 : int ref; f(!y) + !y; pass-by-value

Access to global variables Two possible scoping conventions Static scope: refer to closest enclosing block Dynamic scope: most recent activation record on stack Example outer block x 1 int x=1; function g(z) = x+z; function f(y) = { int x = y+1; return g(y*x) }; f(3); f(3) y 3 x 4 g(12) z 12 Which x is used for expression x+z ?

Activation record for static scope Control link Link to activation record of previous (calling) block Access link Link to activation record of closest enclosing block in program text Difference Control link depends on dynamic behavior of prog Access link depends on static form of program text Control link Access link Return address Return result addr Parameters Local variables Intermediate results Environment Pointer

Complex nesting structure function m(…) { int x=1; … function n( … ){ function g(z) = x+z; { … function f(y) { int x = y+1; return g(y*x) }; f(3); … } … n( … ) …} … m(…) int x=1; function g(z) = x+z; function f(y) = { int x = y+1; return g(y*x) }; f(3); Simplify to Simplified code has same block nesting, if we follow convention that each declaration begins a new block.

Static scope with access links outer block x 1 int x=1; function g(z) = x+z; function f(y) = { int x = y+1; return g(y*x) }; f(3); control link access link g … control link access link f … f(3) control link access link Use access link to find global variable: Access link is always set to frame of closest enclosing lexical block For function body, this is block that contains function declaration y 3 x 4 g(12) control link access link z 12

Tail recursion (first-order case) Function g makes a tail call to function f if Return value of function f is return value of g Example fun g(x) = if x>0 then f(x) else f(x)*2 Optimization Can pop activation record on a tail call Especially useful for recursive tail call next activation record has exactly same form tail call not a tail call

Example Calculate least power of 2 greater than y control control Optimization Set return value address to that of caller Question Can we do the same with control link? avoid return to caller return val return val x 1 x 1 y 3 y 3 control return val x 2 fun f(x,y) = if x>y then x else f(2*x, y); f(1,3) + 7; y 3 control return val x 4 y 3

Tail recursion elimination f(1,3) f(2,3) f(4,3) control control control return val return val return val x 1 x 2 x 4 y 3 y 3 y 3 Optimization pop followed by push = reuse activation record in place Conclusion Tail recursive function equiv to iterative loop fun f(x,y) = if x>y then x else f(2*x, y); f(1,3);

Tail recursion and iteration control return val x 1 y 3 f(4,3) 2 f(1,3) 4 f(2,3) test fun f(x,y) = if x>y then x else f(2*x, y); f(1,y); fun g(y) = { x := 1; while not(x>y) do x := 2*x; return x; }; initial value loop body

Higher-Order Functions Language features Functions passed as arguments Functions that return functions from nested blocks Need to maintain environment of function Simpler case Function passed as argument Need pointer to activation record “higher up” in stack More complicated second case Function returned as result of function call Need to keep activation record of returning function

Example Map function Modify repeated elements in list Why this example here at this point in the lecture???? Map function fun map (f, nil) = nil | map(f, x::xs) = f(x) :: map(f,xs) Modify repeated elements in list fun modify(l) = let val c = ref (hd l) fun f(y) = ((if y = !c then c:=y+1 else c:=y); !c) in (hd l) :: map(f, tl l) end; modify [1,2,2,3,4] => [1,2,3,4,5] Exercise: pure functional version of modify

Pass function as argument val x = 4; fun f(y) = x*y; fun g(h) = let val x=7 in h(3) + x; g(f); There are two declarations of x Which one is used for each occurrence of x? { int x = 4; { int f(int y) {return x*y;} { int g(intint h) { int x=7; return h(3) + x; } g(f); } } }

Static Scope for Function Argument val x = 4; fun f(y) = x*y; fun g(h) = let val x=7 in h(3) + x; g(f); x 4 Code for f f g Code for g h g(f) x 7 y 3 h(3) x * y local var follow access link How is access link for h(3) set?

Static Scope for Function Argument { int x = 4; { int f(int y) {return x*y;} { int g(intint h) { int x=7; return h(3) + x; } g(f); } } } x 4 Code for f f g Code for g h g(f) x 7 y 3 h(3) x * y local var follow access link How is access link for h(3) set?

Closures Function value is pair closure = env, code  When a function represented by a closure is called, Allocate activation record for call (as always) Set the access link in the activation record using the environment pointer from the closure

Function Argument and Closures Run-time stack with access links val x = 4; fun f(y) = x*y; fun g(h) = let val x=7 in h(3) + x; g(f); x 4 f access Code for f h(3) y 3 access g access g(f) h access x 7 Code for g access link set from closure

Function Argument and Closures Run-time stack with access links { int x = 4; { int f(int y){return x*y;} { int g(intint h) { int x=7; return h(3)+x; } g(f); }}} x 4 f access Code for f h(3) y 3 access g access g(f) h access x 7 Code for g access link set from closure

Summary: Function Arguments Use closure to maintain a pointer to the static environment of a function body When called, set access link from closure All access links point “up” in stack May jump past activ records to find global vars Still deallocate activ records using stack (lifo) order

Return Function as Result Language feature Functions that return “new” functions Need to maintain environment of function Example fun compose(f,g) = (fn x => g(f x)); Function “created” dynamically expression with free variables values are determined at run time function value is closure = env, code code not compiled dynamically (in most languages)

Example: Return fctn with private state fun mk_counter (init : int) = let val count = ref init fun counter(inc:int) = (count := !count + inc; !count) in counter end; val c = mk_counter(1); c(2) + c(2); Function to “make counter” returns a closure How is correct value of count determined in c(2) ?

Example: Return fctn with private state {intint mk_counter (int init) { int count = init; int counter(int inc) { return count += inc;} return counter} intint c = mk_counter(1); print c(2) + c(2); } Function to “make counter” returns a closure How is correct value of count determined in call c(2) ?

Function Results and Closures fun mk_counter (init : int) = let val count = ref init fun counter(inc:int) = (count := !count + inc; !count) in counter end end; val c = mk_counter(1); c(2) + c(2); 1 mk_counter(1) count init access counter mk_c Code for mk_counter c access Call changes cell value from 1 to 3 3 c(2) access inc 2 Code for counter

Function Results and Closures {intint mk_counter (int init) { int count = init; int counter(int inc) { return count+=inc;} } intint c = mk_counter(1); print c(2) + c(2); 1 mk_counter(1) count init access counter mk_c Code for mk_counter c access Call changes cell value from 1 to 3 3 c(2) access inc 2 Code for counter

Summary: Return Function Results Use closure to maintain static environment May need to keep activation records after return Stack (lifo) order fails! Possible “stack” implementation Forget about explicit deallocation Put activation records on heap Invoke garbage collector as needed Not as totally crazy as is sounds May only need to search reachable data

Summary of scope issues Block-structured lang uses stack of activ records Activation records contain parameters, local vars, … Also pointers to enclosing scope Several different parameter passing mechanisms Tail calls may be optimized Function parameters/results require closures Closure environment pointer used on function call Stack deallocation may fail if function returned from call Closures not needed if functions not in nested blocks