180 likes | 314 Views
Runtime 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. Procedures and Functions. Structural programming combine shared code
E N D
Runtime 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 by Neng-Fa Zhou
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
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
Trace the Execution of Programs >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) void qsort(int l, int u){ int i; if (l>=u) return; i = partition(l,u); qsort(l,i-1); qsort(i+1,u); } activation by Neng-Fa Zhou
Activation Trees q(1,9) • 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 p(1,9) q(1,3) q(5,9) p(1,3) q(1,0) q(2,3) by Neng-Fa Zhou
The 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 by Neng-Fa Zhou
Parameter 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 by Neng-Fa Zhou
Call by Value actual • formal and actual are independent a 1. evaluate the actual parameter 2. pass it to the activation record a 3. use the formal parameter as a local variable formal by Neng-Fa Zhou
Call by Reference actual • formal becomes an alias for actual • when formal is updated, actual is also updated a formal by Neng-Fa Zhou
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
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
Storage Organization Code Code: program + library Static data: global variables Stack: activation frames Heap: dynamically allocated memory Static Data Stack Heap by Neng-Fa Zhou
Control Stack • A frame is pushed onto the stack each time a procedure is called • The frame is popped off after the execution ends >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) by Neng-Fa Zhou
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
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
Calling and Returning Sequences • Returning sequence • place return value • restore the state including P and top_sp • continue execution callee caller by Neng-Fa Zhou
Dangling References main(){ int *p; p = dangle(); } int *dangle(){ int i=23; return &i; } by Neng-Fa Zhou