Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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)

2 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;

3 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)

4 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?

5 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; }

6 10/16/2015IT 3276 Scope  Activation George Washington America  A.D. 1732-1799

7 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. 1732-1799

8 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

9 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 10/16/2015IT 32710 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

11 10/16/2015IT 32711 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?)

12 10/16/2015IT 32712 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

13 10/16/2015IT 32713 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

14 10/16/2015IT 32714 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

15 10/16/2015IT 32715 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

16 10/16/2015IT 32716 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)

17 10/16/2015IT 32717 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)

18 10/16/2015IT 32718 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)

19 10/16/2015IT 32719 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

20 10/16/2015IT 32720 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.

21 10/16/2015IT 32721 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]

22 10/16/2015IT 32722 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]

23 10/16/2015IT 32723 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 []

24 10/16/2015IT 32724 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.

25 10/16/2015IT 32725 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])

26 10/16/2015IT 32726 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)

27 10/16/2015IT 32727 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;

28 10/16/2015IT 32728 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

29 10/16/2015IT 32729 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

30 10/16/2015IT 32730 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 quicksort(x)@quicksort(y) end; Quick Sort in ML pivot

31 10/16/2015IT 32731 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 below @ [pivot] @ quicksort above end;

32 10/16/2015IT 32732 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, …

33 10/16/2015IT 32733 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, 3 1 2 33

34 10/16/2015IT 32734 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

35 10/16/2015IT 32735 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 }

36 10/16/2015IT 32736 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)

37 10/16/2015IT 32737 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

38 10/16/2015IT 32738 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?

39 10/16/2015IT 32739 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

40 10/16/2015IT 32740 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

41 10/16/2015IT 32741 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

42 10/16/2015IT 32742 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

43 10/16/2015IT 32743 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:?

44 10/16/2015IT 32744 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 ?

45 10/16/2015IT 32745 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: ??


Download ppt "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."

Similar presentations


Ads by Google