Presentation is loading. Please wait.

Presentation is loading. Please wait.

Runtime Environments Source language issues Storage organization

Similar presentations


Presentation on theme: "Runtime Environments Source language issues Storage organization"— Presentation transcript:

1 Runtime Environments Source language issues Storage organization
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 by Neng-Fa Zhou

2 Procedures and Functions
Structural programming combine shared code parameterized Advantages over a flat language Modularity blocks for building large systems Information hiding Separate compilation by Neng-Fa Zhou

3 Procedure 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) by Neng-Fa Zhou

4 Trace the Execution of Programs
void qsort(int l, int u){ int i; if (l>=u) return; i = partition(l,u); qsort(l,i-1); qsort(i+1,u); } >qsort(1,9) >partition(1,9) <partition(1,9) >qsort(1,3) >partition(1,3) <partition(1,3) ... <qsort(1,3) >qsort(5,9) <qsort(5,9) <qsort(1,9) activation by Neng-Fa Zhou

5 Activation 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 by Neng-Fa Zhou

6 The Scope of a Name most closely nested rule main(){ int a = 0;
int b = 0; { int b = 1; int a = 2; printf("%d %d\n",a,b); } int b = 3; most closely nested rule by Neng-Fa Zhou

7 Parameter Passing Call by value Call by reference Call by name
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 by Neng-Fa Zhou

8 Call by Value formal and actual are independent 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 by Neng-Fa Zhou

9 Call by Reference formal becomes an alias for actual
when formal is updated, actual is also updated a formal by Neng-Fa Zhou

10 Call 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 by Neng-Fa Zhou

11 Questions 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? by Neng-Fa Zhou

12 Storage Organization Code Code: program + library Static Data
Static data: global variables Stack: activation frames Heap: dynamically allocated memory Static Data Stack Heap by Neng-Fa Zhou

13 Control Stack >qsort(1,9) >partition(1,9) <partition(1,9) >qsort(1,3) >partition(1,3) qs(1,9) qs(1,3) top_sp pa(1,3) A frame is pushed onto the stack each time a procedure is called The frame is popped off after the execution ends by Neng-Fa Zhou

14 Activation 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 by Neng-Fa Zhou

15 Calling 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 by Neng-Fa Zhou

16 Calling and Returning Sequences
place return value restore the state including P and top_sp continue execution callee caller by Neng-Fa Zhou

17 Dangling References main(){ int *p; p = dangle(); } int *dangle(){
int i=23; return &i; by Neng-Fa Zhou


Download ppt "Runtime Environments Source language issues Storage organization"

Similar presentations


Ads by Google