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 Variables (ch 12)
10/16/2015IT 3272 Imperative Functional n Imperative languages a := 0 – Store a zero in a ’s memory location n Functional languages val a = 0 – Bind a to the value zero a := 0; a := 1; val a = 0; val a = 1;
10/16/2015IT 3273 Function Activations n Activation of a function : The lifetime of one execution of the function, from call to corresponding return. most modern languages If each activation has its own binding for variables, the variables are called activation-specific variable (dynamic or automatic)
10/16/2015IT 3274 Block Activations n A variable might be specific to a particular block (within a function): fun fact n = if (n=0) then 1 else let val b = fact (n-1) in n*b end; Do we have it in C++/JAVA?
10/16/2015IT 3275 Other Lifetimes For Variables n We usually have a way to declare a variable that is bound to an independent memory (independent from any functions) n static allocation, the loader does the job of allocation int count = 0; int nextcount() { int inc = 1; count = count + inc; return count; }
10/16/2015IT 3276 Scope Activation George Washington America A.D
10/16/2015IT 3277 n In most modern languages, variables with local scope have activation-specific lifetimes, by default n some exception int nextcount() { static int count = 0; int inc = 1; count = count + inc; return count; } not activation-specific (lifetime) Scope Activation George Washington America A.D
10/16/2015IT 3278 there are other lifetimes for variables n In OOP, some variables’ lifetimes are associated with object lifetimes n Some variables may last across multiple executions of the program In addition to activation-specific variables
10/16/2015IT 3279 Activation Records Each function being executed has an activation record An activation record contains information about : 1.Activation-specific variables 2.Return address (or pointer to the current instructions) 3.Link to caller’s activation record
10/16/2015IT Block Activation Records n When a block is entered, space (memory) must be found for the local variables of that block n Possibile implementations: – Preallocate in the containing function’s activation record – Extend the function’s activation record when the block is entered (and revert when exited) – Allocate separate block activation records
10/16/2015IT Static Allocation n The simplest approach: allocate one activation record for every function, statically n Older dialects of Fortran and Cobol used this system n Simple and fast, but....(what’s the problem?)
10/16/2015IT Fortran Example FUNCTION AVG (ARR, N) DIMENSION ARR(N) SUM = 0.0 DO 100 I = 1, N SUM = SUM + ARR(I) 100 CONTINUE AVG = SUM / FLOAT(N) RETURN END n Static activation record
10/16/2015IT Drawback n Each function has one activation record, thus There can be only one activation. Modern languages (including modern dialects Cobol and Fortran) do not obey this restriction for: 1.Recursion 2.Multithreading
10/16/2015IT Stacks Of Activation Records n To support recursion, we need to allocate a new activation record for each activation n Dynamic allocation: allocate deallocate n A stack of activation records: stack frames push pop
10/16/2015IT Current Activation Record the one for the function that is running n Static: location of activation record was determined before runtime n Dynamic: location of the current activation record is not known until runtime n A function must know how to find the address of its current activation record. A machine register is reserved to hold this
10/16/2015IT C Example int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result; } The evaluation of fact(3) before the 1 st recursive call, fact(2).... cout <<... << fact(3) << somewhere calling fact(3)
10/16/2015IT int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result; } before calling fact(1) After calling fact(2) calling fact(3) calling fact(2)
10/16/2015IT int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result; } Before fact(1) returns calling fact(3) calling fact(2) calling fact(1)
10/16/2015IT int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result; } The second activation is about to return. 12 2
10/16/2015IT int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result; } The first activation is about to return with the result fact(3) = 6.
10/16/2015IT ML Example fun halve nil = (nil, nil) | halve [a] = ([a], nil) | halve (a::b::cs) = let val (x, y) = halve cs in (a::x, b::y) end; halve [1,2,3,4]
10/16/2015IT fun halve nil = (nil, nil) | halve [a] = ([a], nil) | halve (a::b::cs) = let val (x, y) = halve cs in (a::x, b::y) end; This shows the contents of memory just before the third activation. halve [3,4]
10/16/2015IT fun halve nil = (nil, nil) | halve [a] = ([a], nil) | halve (a::b::cs) = let val (x, y) = halve cs in (a::x, b::y) end; This shows the contents of memory just before the third activation returns. halve []
10/16/2015IT fun halve nil = (nil, nil) | halve [a] = ([a], nil) | halve (a::b::cs) = let val (x, y) = halve cs in (a::x, b::y) end; The second activation is about to return.
10/16/2015IT fun halve nil = (nil, nil) | halve [a] = ([a], nil) | halve (a::b::cs) = let val (x, y) = halve cs in (a::x, b::y) end; The first activation is about to return with the result halve [1,2,3,4] = ([1,3],[2,4])
10/16/2015IT Nesting Functions – Function definitions can be nested inside other function definitions – Inner functions can refer to local variables of the outer functions (under the usual block scoping rule) n ML, Ada, Pascal provide such feature; n C, Java don’t. (for good reasons)
10/16/2015IT Called by another inner function An easy solution: further trace down the previous activation record. Called by a recursive call. f(…) { int i = 3; } …… h(…) …… g(…) { … i … } h(…) { g(…) … } f(…) { int i = 3; … g(…) … } g(…) { … g(…)… … i … } What is i in function g ? The tricky part is that, the previous activation record may not help int i = 2;
10/16/2015IT Static Scoping vs Dynamic Scoping What is the vale of i in function g ? (suppose h f g) f(…) { int i = 3; } h(…) { int i = 2; …… f(…) …… } ……g(…)…… g(…) { … i … } dynamic scoping static scoping n Static Scoping n Dynamic Scoping i = 3
10/16/2015IT Static Scoping and Nesting Link For Static Scoping: n An inner function needs to find the address of the most recent activation of the outer function, not the caller (caller is for dynamic) n We can keep a nesting link in the activation record… f(…) { int i = 3; } h(…) { int i = 2; …… g(…) …… } ……g(…)…… g(…) { … i … } dynamic scoping static scoping
10/16/2015IT fun split nil = (nil,nil) | split [a] = ([a],nil) | split [a,b] = if (a <= b)then ([a],[b]) else ([b],[a]) | split (a::b::c) = let val (x,y) = split (a::c) in if (a < b) then (x, b::y) else (b::x, y) end; fun quicksort nil = nil | quicksort [a] = [a] | quicksort a = let val (x,y) = split a in end; Quick Sort in ML pivot
10/16/2015IT Better ML fun quicksort nil = nil | quicksort (pivot::rest) = let fun split(nil) = (nil,nil) | split(x::xs) = let val (below, above) = split(xs) in if x < pivot then (x::below, above) else (below, x::above) end; val (below, above) = split(rest) in quicksort quicksort above end;
10/16/2015IT time Current Activation Record Register Can’t find pivot here QuickSort(…) Return Address Previous Activation Record QuickSort local variable: pivote, rest, … Split(…) Return Address Previous Activation Record QuickSort local variable: x, xs, … Split(…) Return Address Previous Activation Record QuickSort local variable: x, xs, … Split(…) Return Address Previous Activation Record QuickSort local variable: x, xs, …
10/16/2015IT time Current Activation Record QuickSort(…) Return Address Previous Activation Record QuickSort local variable: pivote, rest, … Split(…) Return Address Previous Activation Record QuickSort local variable: x, xs, … Split(…) Return Address Previous Activation Record QuickSort local variable: x, xs, … Split(…) Return Address Previous Activation Record QuickSort local variable: x, xs, … Nesting link null The rules to setup the nesting links: 1, 2,
10/16/2015IT How to Set Nesting Links n For one level of nesting: – 1. Calling from outer to outer: (non-nesting function, e.g. main calls quicksort) set to null – 2. Calling from outer to inner: (e.g. quicksort calls split, where split is defined inside quicksort) set nesting link same to caller’s activation record – 3. Calling from inner to inner: (e.g. split calls split) set nesting link same to caller’s nesting link
10/16/2015IT Multiple Levels Of Nesting n References n nesting levels away chain back through n nesting links f(…) { int fi; } g(…) { int gi; } h(…) { int hi … hi, gi, fi }
10/16/2015IT n Common methods for referring to non-local variables in outer functions – Nesting links in activation records – Displays: no nesting links in the activation records, but collected in a single static array – Lambda lifting: passing all needed variables as parameters (i.e., as hidden parameters)
10/16/2015IT Functions As Parameters n What really gets passed? n Source code, compiled code, pointer to code, or implementation in some other form macro expandingOOP objectsC’s way functional languages
10/16/2015IT Example fun addXToAll (x,theList) = let fun addX y = y + x; in map addX theList end; addXToAll (call) map, map (call) addX, addX (refers) x In the previous example: quicksort (call) split 5, [2,3,4] [7,8,9] where to? Nesting link (in addXToAll ’s activation record) map’s activation record?
10/16/2015IT When map addX, what nesting link will addX be given? – Not map ’s activation record: because addX is not nested inside map – Not map ’s nesting link: because map is not nested inside any other funcion Therefore, the parameter addX passed to map must include the nesting link to use when addX is called, i.e., the nesting link of addx should be prepared when addToAll tries to pass it as an argument to map
10/16/2015IT Example fun addXToAll (x,theList) = let fun addX y = y + x; in map addX theList end; Right before the call to map. The variable addX is bound to a function-value including code and nesting link. Current Activation Record y=>y+x Nesting link Parameter Return address Nesting link Previous activation record x theList addX
10/16/2015IT Not Just For Parameters n Functional languages allow many more kinds of operations on function-values: – passed as parameters, – returned from functions, – constructed by expressions, etc. n Function-values include both code to call, and nesting link to use when calling it
10/16/2015IT One More Complication n What happens if a function created is to be returned as a value? fun funToAddX x = let fun addX y = y + x; in addX end; fun test = let val f = funToAddX 3; in f 5 end; return a function that add x
10/16/2015IT fun funToAddX x = let fun addX y = y + x; in addX end; fun test = let val f = funToAddX 3; in f 5 end; Before funToAddX returns addx. y=>y+x Nesting link Parameter :3 Return address Nesting link: null Previous activation record x=3 addX Current Activation Record Parameter Return address Nesting link … f:?
10/16/2015IT fun funToAddX x = let fun addX y = y + x; in addX end; fun test = let val f = funToAddX 3; in f 5 end; y=>y+x Nesting link Parameter :3 Return address Nesting link: null Previous activation record x=3 addX Current Activation Record Parameter Return address Nesting link … f: After funToAddX returns, f is the bound to the new function-value. Any Problem ?
10/16/2015IT Problem -- This will fail if the language system deallocated that activation record when the function returned Solution:keep all activation records (ML’s way) New problem: Solution: waste too much memory Garbage collection New problem: ??