1 / 17

Runtime Environments

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

emmett
Download Presentation

Runtime Environments

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  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 >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

  5. 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

  6. 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

  7. 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

  8. 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

  9. Call by Reference actual • 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: global variables Stack: activation frames Heap: dynamically allocated memory Static Data Stack Heap by Neng-Fa Zhou

  13. 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

  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 • Returning sequence • 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

More Related