In: Computer Science
Activation Records :
1.Modern imperative programming languages typically have local variables.
*Created upon entry to function.
*Destroyed when function returns.
2.Each invocation of a function has its own instantiation of local variables.
*Recursive calls to a function require several instantiations to exist simultaneously.
*Functions return only after all functions it calls have returned last-in-first-out (LIFO) behavior.
* A LIFO structure called a stack is used to hold each instantiation.
3.The portion of the stack used for an invocation of a function is called the function’s stack frame or activation record.
Stack
*Used to hold local variables.
*Large array which typically grows downwards in memory toward lower
addresses,shrinks upwards.
*Push(r1):
stack_pointer--;
M[stack_pointer] = r1;
*r1 = Pop():
r1 = M[stack_pointer];
stack_pointer++;
*Previous activation records need to be accessed, so push/pop not
sufficient.
– Treat stack as array with index off of stack pointer.
– Push and pop entire activation records.
Consider the following example,
let
function g(x:int) =
let
var y := 10
in
x+y
end
function h(y:int):int =
y + g(y)
in
h(4)
end
Step 1: h(4) called
Chunk of memory allocated on the stack in order to hold local variables of h. The activation record (or stack frame) of h is pushed onto the stack.
Step 2: g(4) called
Activation record for g allocated (pushed) on stack.
Step 3: g(4) returns with value 14
Activation record for g deallocated (popped) from stack.
Step 4: h(4) returns with value 18
Activation record for h deallocated (popped) from stack. Stack now empty.