440 likes | 593 Views
All about binding. Memory Locations for Variables (ch 12). Variables are bound (dynamically) to values values must be stored somewhere in the memory. Imperative Functional. a := 0; a := 1; val a = 0; val a = 1;. Imperative languages a := 0 Store a zero in a ’s memory location
E N D
All about binding Memory Locations for Variables (ch 12) • Variables are bound (dynamically) to values • values must be stored somewhere in the memory. IT 327
Imperative Functional a := 0; a := 1; val a = 0; val a = 1; • Imperative languages a := 0 • Store a zero in a’s memory location • Functional languages val a = 0 • Binda to the value zero Big deal? Yes! IT 327
Function Activations • Activation of a function : The lifetime of one execution of the function, from call to corresponding return. If each activation has itsown binding for variables, the variables are called activation-specificvariable (dynamic or automatic) most modern languages IT 327
Block Activations • 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? IT 327
Other Lifetimes For Variables • We usually have a way to declare a variable that is bound to an independent memory (independent from any functions) • static allocation, the loader does the job of allocation int count = 0; int nextcount() { count = count + 1; return count;} IT 327
Scope Activation George Washington America A.D. 1732-1799 IT 327
Scope Activation (lifetime) George Washington America A.D. 1732-1799 • In most modern languages, variables with local scope have activation-specific lifetimes, by default • some exception int nextcount() {static int count = 0; count = count + 1; return count;} not activation-specific IT 327
there are other lifetimes for variables In addition to activation-specific variables • In OOP, some variables’ lifetimes are associated with object lifetimes • Some variables may last across multiple executions of the program IT 327
Activation Records Each function being executed has an activation record An activation record contains information about : • Activation-specific variables • Return address (or pointer to the current instructions) • Link to caller’s activation record IT 327
Block Activation Records • When a block is entered, space must be found for the local variables of that block • 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 IT 327
Static Allocation • The simplest approach: allocate one activation record for every function, statically • Older dialects of Fortran and Cobol used this system • Simple and fast, but....(what’s the problem?) IT 327
Static activation record Fortran Example FUNCTION AVG (ARR, N) DIMENSION ARR(N)SUM = 0.0 DO 100 I = 1, NSUM = SUM + ARR(I)100 CONTINUEAVG = SUM / FLOAT(N)RETURN END IT 327
Drawback • 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: • Recursion • Multithreading IT 327
Stacks Of Activation Records • To support recursion, we need to allocate a new activation record for each activation • Dynamic allocation: allocate deallocate • A stack of activation records: stack frames pushpop IT 327
Current Activation Recordthe one for the function that is running • Static: location of activation record was determined before runtime • Dynamic: location of the current activation record is not known until runtime • A function must know how to find the address of its current activation record. A machine register is reserved to hold this IT 327
C Example The evaluation of fact(3)before the 1st recursive call, fact(2) int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result;} .... cout << ... << fact(3) << .... ..... somewhere calling fact(3) IT 327
int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result;} calling fact(2) calling fact(3) After calling fact(2) before calling fact(1) IT 327
int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1);return result;} Beforefact(1)returns calling fact(2) calling fact(3) calling fact(1) IT 327
The second activation is about to return. int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result;} 2 2 1 IT 327
The first activation is about to return with the result fact(3) = 6. int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result;} IT 327
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] IT 327
halve [3,4] This shows the contents of memory just before the third activation. 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; IT 327
halve [] This shows the contents of memory just before the third activation returns. 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; IT 327
The second activation is about to return. 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; IT 327
The first activation is about to return with the result halve [1,2,3,4] =([1,3],[2,4]) 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; IT 327
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) • ML, Ada, Pascal provide such feature; • C, Java don’t. (for good reasons) IT 327
An easy solution: further trace down the previous activation record. Called by a recursive call. f(…) { int i = 3; … g(…) … } f(…) { int i = 3; } Called by another inner function g(…) { … i … } g(…) { … g(…)… … i … } h(…) { g(…) … … } int i = 2; …… h(…) …… What is i in function g? The tricky part is that, the previous activation record may not help IT 327
Static Scoping vs Dynamic Scoping static scoping • What is the vale of i in function g ? (suppose h f g) f(…) { int i = 3; } h(…) { int i = 2; …… f(…) …… } g(…) { … i … } dynamic scoping ……g(…)…… • Static Scoping • Dynamic Scoping i = 3 i = 3 IT 327
Static Scoping andNesting Link static scoping For Static Scoping: • An inner function needs to find the address of the most recent activation of the outerfunction, not the caller (caller is for dynamic) • We can keep a nesting linkin the activation record… f(…) { int i = 3; } h(…) { int i = 2; …… g(…) …… } g(…) { … i … } dynamic scoping ……g(…)…… IT 327
Example 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 below @ [pivot] @ quicksort above end; IT 327
time Current Activation Record Register Split(…) Split(…) Split(…) QuickSort(…) Return Address Return Address Return Address Return Address Previous Activation Record Previous Activation Record Previous Activation Record Previous Activation Record QuickSort local variable: x, xs, … QuickSort local variable: x, xs, … QuickSort local variable: x, xs, … QuickSort local variable: pivote, rest, … Can’t find pivot here IT 327
time Current Activation Record Split(…) Split(…) Split(…) QuickSort(…) Return Address Return Address Return Address Return Address Previous Activation Record Previous Activation Record Previous Activation Record Previous Activation Record QuickSort local variable: x, xs, … QuickSort local variable: x, xs, … QuickSort local variable: x, xs, … QuickSort local variable: pivote, rest, … Nesting link Nesting link Nesting link Nesting link null 3 3 2 1 The rules to setup the nesting links: 1, 2, 3 IT 327
How to Set Nesting Links • 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 as caller’s activation record • 3. Calling from inner to inner: (e.g. split calls split) set nesting link same as caller’s nesting link IT 327
Multiple Levels Of Nesting f(…) { int fi; } g(…) { int gi; } • References n nesting levels away chain back throughn nesting links h(…) { int hi … … hi, gi, fi } IT 327
Common methods for referring to non-local variables in outer functions • Nesting links in activation records • Displays: nesting links not in the activation records, but collected in a single static array • Lambda lifting: passing all needed variables as parameters (i.e., as hidden parameters) IT 327
Functions As Parameters • What really gets passed? • Source code, compiled code, pointer to code, or implementation in some other form macro expanding OOP objects C’s way functional languages IT 327
Example fun addXToAll (x,theList) = let fun addX y = y + x; in map addX theList end; 5, [2,3,4] [7,8,9] Nesting link where to? map’s activation record? In the previous example: quicksort (call) split Nesting link addXToAll (call)map, map (call)addX, addX(refers)x (in addXToAll’s activation record) IT 327
When mapaddX, 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 parameteraddX 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 IT 327
Example Current Activation Record fun addXToAll (x,theList) = let fun addX y = y + x; in map addX theList end; Right before the call tomap. The variable addX is bound to a function-value including code and nesting link. y=>y+x Nesting link IT 327
Not Just For Parameters • Functional languages allow many more kinds of operations on function-values: • passed as parameters, • returned from functions, • constructed by expressions, etc. • Function-values include both code to call, and nesting link to use when calling it IT 327
One More Complication • 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 IT 327
Current Activation Record fun test = let val f = funToAddX 3; in f 5 end; fun funToAddX x = let fun addX y = y + x; in addX end; y=>y+x Nesting link BeforefunToAddXreturnsaddx. IT 327
Current Activation Record fun test = let val f = funToAddX 3; in f 5 end; fun funToAddX x = let fun addX y = y + x; in addX end; y=>y+x Nesting link AfterfunToAddXreturns, f is the bound to the new function-value. Any Problem ? IT 327
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: waste too much memory Solution: Garbage collection New problem: ?? IT 327