Title: Runtime Environments
1Runtime Environments
- Source language issues
- trace the execution of a program
- activation trees
- the scope of a name
- parameter passing
- Storage organization
- the control stack
- symbol tables
- dynamic storage allocation
2Procedures and Functions
- Structural programming
- combine shared code
- parameterized
- Advantages over a flat language
- Modularity
- blocks for building large systems
- Information hiding
- Separate compilation
3Procedure Definitions and Calls
formal parameters
- Function definition
- Function call
void sort(int a, int l, int u) ...
return type
actual parameters
sort(a,0,100)
4Trace the Execution of Programs
gtqsort(1,9) gtpartition(1,9)
ltpartition(1,9) gtqsort(1,3)
gtpartition(1,3) ltpartition(1,3) ...
ltqsort(1,3) gtqsort(5,9) ...
ltqsort(5,9) ltqsort(1,9)
void qsort(int l, int u) int i if
(lgtu) return i partition(l,u)
qsort(l,i-1) qsort(i1,u)
activation
5Activation Trees
q(1,9)
p(1,9)
q(1,3)
q(5,9)
p(1,3)
q(1,0)
q(2,3)
- each node represents an activation
- the root represents the main program
- node A is a parent of node B if A calls B
- the order of siblings is determined by the
lifetimes of the activations
6The Scope of a Name
main() int a 0 int b 0 int
b 1 int a 2 printf("d
d\n",a,b) int b 3
printf("d d\n",a,b) printf("d
d\n",a,b) printf("d d\n",a,b)
most closely nested rule
7Parameter Passing
- Call by value
- primitive types, pointers, structures in C
- all data types in Java
- Call by reference
- arrays in C
- var arguments in Pascal
- Call by name
- in-line expansion
8Call by Value
actual
a
1. evaluate the actual parameter
2. pass it to the activation record
a
3. use the formal parameter as a local variable
formal
- formal and actual are independent
9Call by Reference
actual
- formal becomes an alias for actual
- when formal is updated, actual is also updated
a
formal
10Call by Name
- The body is substituted for the call
- The local names of the called procedure are
renamed if necessary to keep them different from
those in the calling procedure - The actual parameter are surrounded by
parentheses if necessary
11Questions to Ask
- May procedures be recursive?
- What happens to the values of local names when
control returns from an activation of a
procedure? - May a procedure refer to nonlocal names?
- How are parameters passed?
- May procedures be passed as parameters?
- May procedures be returned as results?
- Must storage be deallocated explicitly?
12Storage Organization
Code
Code program library Static data global
variables Stack activation frames Heap
dynamically allocated memory
Static Data
Stack
Heap
13Control Stack
gtqsort(1,9) gtpartition(1,9)
ltpartition(1,9) gtqsort(1,3)
gtpartition(1,3)
qs(1,9)
qs(1,3)
pa(1,3)
top_sp
- A frame is pushed onto the stack each time a
procedure is called - The frame is popped off after the execution ends
14Activation Records (Frames)
returned value
actual parameters
optional control link parent frame etc. access
link frame of the outer procedure machine
status program counter registers
optional control link
optional access link
saved machine status
local data
temporaries
15Calling and Returning Sequences
- Calling sequence
- evaluate actual parameters
- set callee's frame (arguments,return
address,parent) - reset the top_sp register
- save registers including the program counter
- initialize local data
- begin execution
caller
callee
16Calling and Returning Sequences
- Returning sequence
- place return value
- restore the state including P and top_sp
- continue execution
callee
caller
17Dangling References
main() int p p dangle() int
dangle() int i23 return i