Memory Locations For Variables
Chapter Twelve Modern Programming Languages, 2nd ed. 1
Memory Locations For Variables Chapter Twelve Modern Programming - - PowerPoint PPT Presentation
Memory Locations For Variables Chapter Twelve Modern Programming Languages, 2nd ed. 1 A Binding Question Variables are bound (dynamically) to values Those values must be stored somewhere Therefore, variables must somehow be bound to
Chapter Twelve Modern Programming Languages, 2nd ed. 1
Chapter Twelve Modern Programming Languages, 2nd ed. 2
– Store a zero in a’s memory location
– Bind a to the value zero
Chapter Twelve Modern Programming Languages, 2nd ed. 3
Chapter Twelve Modern Programming Languages, 2nd ed. 4
Chapter Twelve Modern Programming Languages, 2nd ed. 5
Chapter Twelve Modern Programming Languages, 2nd ed. 6
Chapter Twelve Modern Programming Languages, 2nd ed. 7
Chapter Twelve Modern Programming Languages, 2nd ed. 8
Chapter Twelve Modern Programming Languages, 2nd ed. 9
Chapter Twelve Modern Programming Languages, 2nd ed. 10
– Return address: where to go in the program
– Link to caller’s activation record: more about
Chapter Twelve Modern Programming Languages, 2nd ed. 11
When a block is entered, space must be found for
Various possibilities:
– Preallocate in the containing function’s activation
– Extend the function’s activation record when the block
– Allocate separate block activation records
Our illustrations will show the first option
Chapter Twelve Modern Programming Languages, 2nd ed. 12
Chapter Twelve Modern Programming Languages, 2nd ed. 13
Chapter Twelve Modern Programming Languages, 2nd ed. 14
Chapter Twelve Modern Programming Languages, 2nd ed. 15
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 address return address ARR address I SUM AVG
– Recursion – Multithreading
Chapter Twelve Modern Programming Languages, 2nd ed. 16
Chapter Twelve Modern Programming Languages, 2nd ed. 17
Chapter Twelve Modern Programming Languages, 2nd ed. 18
Chapter Twelve Modern Programming Languages, 2nd ed. 19
Chapter Twelve Modern Programming Languages, 2nd ed. 20
int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result; } We are evaluating fact(3). This shows the contents of memory just before the recursive call that creates a second activation.
previous activation record result: ? return address n: 3 current activation record
Chapter Twelve Modern Programming Languages, 2nd ed. 21
int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result; } This shows the contents
the third activation.
previous activation record result: ? return address n: 3 previous activation record result: ? return address n: 2 current activation record
Chapter Twelve Modern Programming Languages, 2nd ed. 22
int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result; } This shows the contents
the third activation returns.
previous activation record result: ? return address n: 3 previous activation record result: ? return address n: 2 previous activation record result: 1 return address n: 1 current activation record
Chapter Twelve Modern Programming Languages, 2nd ed. 23
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.
previous activation record result: ? return address n: 3 previous activation record result: 2 return address n: 2 previous activation record result: 1 return address n: 1 current activation record
Chapter Twelve Modern Programming Languages, 2nd ed. 24
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.
previous activation record result: 6 return address n: 3 previous activation record result: 2 return address n: 2 previous activation record result: 1 return address n: 1 current activation record
Chapter Twelve Modern Programming Languages, 2nd ed. 25
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;
We are evaluating halve [1,2,3,4]. This shows the contents of memory just before the recursive call that creates a second activation.
parameter: [1,2,3,4] current activation record return address previous activation record a: 1 b: 2 cs: [3,4] x: ? y: ? value to return: ?
Chapter Twelve Modern Programming Languages, 2nd ed. 26
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
the third activation.
parameter: [1,2,3,4] return address previous activation record a: 1 b: 2 cs: [3,4] x: ? y: ? value to return: ? parameter: [3,4] current activation record return address previous activation record a: 3 b: 4 cs: [] x: ? y: ? value to return: ?
Chapter Twelve Modern Programming Languages, 2nd ed. 27
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
the third activation returns.
parameter: [1,2,3,4] return address previous activation record a: 1 b: 2 cs: [3,4] x: ? y: ? value to return: ? parameter: [3,4] return address previous activation record a: 3 b: 4 cs: [] x: ? y: ? value to return: ? parameter: [] current activation record return address previous activation record value to return: ([], [])
Chapter Twelve Modern Programming Languages, 2nd ed. 28
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.
parameter: [1,2,3,4] return address previous activation record a: 1 b: 2 cs: [3,4] x: ? y: ? value to return: ? parameter: [3,4] return address previous activation record a: 3 b: 4 cs: [] x: [] y: [] value to return: ([3], [4]) parameter: [] current activation record return address previous activation record value to return: ([], [])
Chapter Twelve Modern Programming Languages, 2nd ed. 29
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])
parameter: [1,2,3,4] return address previous activation record a: 1 b: 2 cs: [3,4] x: [3] y: [4] value to return: ([1,3],[2,4]) parameter: [3,4] return address previous activation record a: 3 b: 4 cs: [] x: [] y: [] value to return: ([3], [4]) parameter: [] current activation record return address previous activation record value to return: ([], [])
Chapter Twelve Modern Programming Languages, 2nd ed. 30
– Function definitions can be nested inside other
– Inner functions can refer to local variables of
Chapter Twelve Modern Programming Languages, 2nd ed. 31
Chapter Twelve Modern Programming Languages, 2nd ed. 32
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;
Chapter Twelve Modern Programming Languages, 2nd ed. 33
Chapter Twelve Modern Programming Languages, 2nd ed. 34
parameter return address previous activation record split’s variables: x, xs, etc. current activation record a split activation parameter return address previous activation record split’s variables: x, xs, etc. another split activation parameter return address previous activation record quicksort’s variables: pivot, rest, etc. first caller: a quicksort activation
Chapter Twelve Modern Programming Languages, 2nd ed. 35
Chapter Twelve Modern Programming Languages, 2nd ed. 36
parameter return address previous activation record current activation record a split activation parameter return address previous activation record split’s variables: x, xs, etc. another split activation parameter return address previous activation record quicksort’s variables: pivot, rest, etc. first caller: a quicksort activation
nesting link nesting link nesting link: null split’s variables: x, xs, etc.
– Calling outer function: set to null – Calling from outer to inner: set nesting link
– Calling from inner to inner: set nesting link
Chapter Twelve Modern Programming Languages, 2nd ed. 37
References at the same level (f1 to v1, f2 to v2,
References n nesting levels away chain back
Chapter Twelve Modern Programming Languages, 2nd ed. 38
function f1 function f2 variable v1 function f3 variable v2 variable v3
– Nesting links in activation records: as shown – Displays: nesting links not in the activation
– Lambda lifting: problem references replaced by
Chapter Twelve Modern Programming Languages, 2nd ed. 39
Chapter Twelve Modern Programming Languages, 2nd ed. 40
Chapter Twelve Modern Programming Languages, 2nd ed. 41
Chapter Twelve Modern Programming Languages, 2nd ed. 42
fun addXToAll (x,theList) = let fun addX y = y + x; in map addX theList end;
– Not map’s activation record: addX is not
– Not map’s nesting link: map is not nested
Chapter Twelve Modern Programming Languages, 2nd ed. 43
– passed as parameters, returned from functions,
Chapter Twelve Modern Programming Languages, 2nd ed. 44
fn y => y + x
Chapter Twelve Modern Programming Languages, 2nd ed. 45
fun addXToAll (x,theList) = let fun addX y = y + x; in map addX theList end; This shows the contents of memory just before the call to
bound to a function-value including code and nesting link.
fn y => y + x parameter return address previous activation record x nesting link: null theList addX current activation record
Chapter Twelve Modern Programming Languages, 2nd ed. 46
Chapter Twelve Modern Programming Languages, 2nd ed. 47
fun funToAddX x = let fun addX y = y + x; in addX end; fun test () = let val f = funToAddX 3; in f 5 end;
Note: test’s parameter here is the special value (). That’s the one and
a sort of placeholder for functions that don’t have significant parameters.
Chapter Twelve Modern Programming Languages, 2nd ed. 48
fun funToAddX x = let fun addX y = y + x; in addX end; fun test () = let val f = funToAddX 3; in f 5 end;
This shows the contents of memory just before funToAddX returns.
fn y => y + x parameter x: 3 return address previous activation record nesting link: null addX current activation record return address previous activation record nesting link: null f: ?
Chapter Twelve Modern Programming Languages, 2nd ed. 49
fun funToAddX x = let fun addX y = y + x; in addX end; fun test () = let val f = funToAddX 3; in f 5 end;
After funToAddX returns, f is the bound to the new function-value.
fn y => y + x parameter x: 3 return address previous activation record nesting link: null addX current activation record return address previous activation record nesting link: null f
Chapter Twelve Modern Programming Languages, 2nd ed. 50
Chapter Twelve Modern Programming Languages, 2nd ed. 51
– Static allocation: works for languages that
– Simple stack allocation: works for languages
Chapter Twelve Modern Programming Languages, 2nd ed. 52
– Nesting links (or some such trick): required for
– Some languages (like ML) permit references to
Chapter Twelve Modern Programming Languages, 2nd ed. 53