Professional Documents
Culture Documents
Run Time ST All 2 PDF
Run Time ST All 2 PDF
- Part 2
Y.N. Srikant
Computer Science and Automation
Indian Institute of Science
Bangalore 560 012
Y.N. Srikant 3
Dynamic Data Storage Allocation
Y.N. Srikant 4
Activation Record Structure
Return address
Note:
Static and Dynamic links
(also called Access and Control link resp.) The position of the fields
(Address of) function result of the act. record as
shown are only notional.
Actual parameters
Implementations can
choose different orders;
Local variables e.g., function result
could be at the top of the
Temporaries act. record.
Y.N. Srikant 5
Variable Storage Offset Computation
Y.N. Srikant 7
Allocation of Activation Records
SL chain Static Link DL chain
Dynamic Link
program RTST; Base RTST
procedure P; Next
procedure Q;
begin R; end
procedure R; Activation records are
created at procedure entry
begin Q; end time and destroyed at
begin R; end procedure exit time
begin P; end
Y.N. Srikant 8
Allocation of Activation Records
SL chain Static Link DL chain
Dynamic Link
program RTST; RTST
Y.N. Srikant 9
Allocation of Activation Records
SL chain Static Link DL chain
Dynamic Link
program RTST; RTST
Y.N. Srikant 10
Allocation of Activation Records
SL chain Static Link DL chain
Dynamic Link
program RTST; RTST
Y.N. Srikant 11
Allocation of Activation Records
SL chain Static Link DL chain
Dynamic Link
1 program RTST; RTST
Y.N. Srikant 12
Allocation of Activation Records
Skip L1-L2+1 records SL chain Static Link DL chain
Dynamic Link
starting from the caller’s RTST
AR and establish the
Static Link
static link to the AR Dynamic Link
reached P
Y.N. Srikant 13
Display Stack of Activation Records
1 program RTST;
call
2 procedure P;
3 procedure Q; P
begin R; end RTST RTST
3 procedure R; return
begin Q; end
begin R; end
Q R
begin P; end P
P
Pop L1-L2+1 records off the display RTST RTST
of the caller and push the pointer to
AR of callee (L1 – caller, L2 – Callee)
The popped pointers are stored in R
the AR of the caller and restored to P
the DISPLAY after the callee returns RTST
Y.N. Srikant 14
Static Scope and Dynamic Scope
Static Scope
A global identifier refers to the identifier with that name that
is declared in the closest enclosing scope of the program
text
Uses the static (unchanging) relationship between blocks in
the program text
Dynamic Scope
A global identifier refers to the identifier associated with the
most recent activation record
Uses the actual sequence of calls that are executed in the
dynamic (changing) execution of the program
Both are identical as far as local variables are
concerned
Y.N. Srikant 15
Static Scope and Dynamic Scope :
An Example
int x = 1; x 1 outer block
function g(z) = x+z;
function f(y) = {
int x = y+1; y 3 f(3)
return g(y*x) x 4
};
f(3);
z 12 g(12)
After the call to g,
Static scope: x = 1 Stack of activation records
Dynamic scope: x = 4 after the call to g
Y.N. Srikant 16
Static Scope and Dynamic Scope:
Another Example
float r = 0.25; Under static scoping,
void show() { printf(“%f”,r); } the output is
void small() { 0.25 0.25
float r = 0.125; show(); 0.25 0.25
} Under dynamic
int main (){ scoping, the output is
show(); small(); printf(“\n”); 0.25 0.125
show(); small(); printf(“\n”); 0.25 0.125
}
Y.N. Srikant 17
Implementing Dynamic Scope –
Deep Access Method
Use dynamic link as static link
Search activation records on the stack to find the
first AR containing the non-local name
The depth of search depends on the input to the
program and cannot be determined at compile time
Needs some information on the identifiers to be
maintained at runtime within the ARs
Takes longer time to access globals, but no
overhead when activations begin and end
Y.N. Srikant 18
Implementing Dynamic Scope –
Shallow Access Method
Allocate some static storage for each name
When a new AR is created for a procedure p,
a local name n in p takes over the static
storage allocated to name n
The previous value of n held in static storage
is saved in the AR of p and is restored when
the activation of p ends
Direct and quick access to globals, but some
overhead is incurred when activations begin
and end
Y.N. Srikant 19
Passing Functions as Parameters
A language has first-class functions
An example: if functions can be
main() declared within any scope
Y.N. Srikant 20
Passing Functions as Parameters –
Implementation with Static Scope
SL chain An example:
x=4 main()
main { int x = 4;
int f (int y) {
SL pointer to return x*y;
code for f }
g(f) int g (int → int h){
x=7 closure for int x = 7;
parameter h return h(3) + x;
}
SL g(f);// returns 12
AR for the
h(3) call f(3)
}
y=3
Y.N. Srikant 21
Passing Functions as Parameters –
Implementation with Static Scope
In this example, when executing the call
An example: h(3), h is really f and 3 is the parameter y
main() of f
{ int x = 4; Without passing a closure, the AR of the
int f (int y) { main program cannot be accessed, and
hence, the value of x within f will not be 4
return x*y; When f is passed as a parameter in the
} call g(f), a closure consisting of a pointer
int g (int → int h){ to the code for f and a pointer to the AR
of the main program is passed
int x = 7;
When processing the call h(3), after
return h(3) + x; setting up an AR for h (i.e., f), the SL for
} the AR is set up using the AR pointer in
g(f); the closure for f that has been passed to
the call g(f)
}
Y.N. Srikant 22
Heap Memory Management