1 / 31

Runtime Environments

Runtime Environments. Compiler Construction Chapter 7. In This Lecture. Runtime Environments Language Features for different Environments Scoping and Allocation Procedure Calls Parameter Passing. Runtime Environments. Three types Fully Static Fortran77 Stack-based C, C++, Pascal, Ada

qamra
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 Compiler Construction Chapter 7

  2. In This Lecture • Runtime Environments • Language Features for different Environments • Scoping and Allocation • Procedure Calls • Parameter Passing

  3. Runtime Environments • Three types • Fully Static • Fortran77 • Stack-based • C, C++, Pascal, Ada • Fully Dynamic • LISP

  4. Memory Organization Entry Point for Proc 1 Code for Proc 1 Code for Proc 2 Entry Point for Proc 2 … Entry Point for Proc n Code for Proc n Code memory

  5. Static Code and Data • The code area of a program is fixed prior to execution • Thus the address for code can be computed at compile time • Actually, the location of the code is usually relative to some base register, or some other memory addressing scheme • The address for data (variables etc.) is in general not assigned fixed locations at compile time • Static data however, does have a fixed memory address and is known at compile time • Constants can also be assigned a fixed address • This is usually reserved for strings and other large constants • Other constants like ‘0’ or ‘1’ are inserted directly into the code

  6. Dynamic Data Allocation • The memory for dynamic data is typically organized into two major areas • The stack • Used for local variables, parameter variables, return addresses and return values • The heap • Used for dynamically allocated variables

  7. Runtime Storage Organization Code Area Global Static Area Stack The stack generally grows towards higher memory Free space The heap generally grows towards lower memory Heap

  8. arguments (parameters) return address etc. local variables Procedure Activation Records Depending on the Number of Registers much of this data might be stored in registers called a stack-frame if stored on the stack In C – like languages these records are stored on the stack In Fortran they are in the static storage area In Lisp they are in the heap area

  9. Calling Sequence • Operations that must be performed when a procedure is called the calling sequence • Includes • allocation of memory for activation record • computation and storing of arguments • storing and setting necessary registers • Additional operations are needed when a procedure returns called the return sequence • placing the return value • readjustment of registers • de-allocation of memory for activation record

  10. Important Registers • Program Counter (pc) • set this for procedure calls • Stack Pointer (sp) • for activation frame info in stack-based environments • Base Pointer (bp) • similar to the stack pointer, but points to the beginning of the activation record

  11. Fully Static Runtime Environments • All variables are allocated statically • including local variables • No pointers or dynamic allocation • No recursion • why? • All procedures have a single activation record • allocated statically before program execution

  12. Static Runtime Environment code for main code for proc 1 Code Area … code for proc n global data area Data Area activation record for main activation record for proc 1 … activation record for proc 2

  13. Stack-basedRuntime Environments • Supports Recursion • No way of knowing at compile time, how many times a procedure will need to be called • Use stack-based activation records in order to support recursion

  14. GCD int x,y; int gcd ( int u, int v) { if (v == 0) return u; else return gcd (v, u%v); } void main() { scanf(“%d%d”,&x,&y); printf(“%d\n”,gcd(x,y)); }

  15. Activation Records x: 15 y: 10 global static area

  16. Activation Records x: 15 y: 10 global static area activation record of main

  17. Activation Records x: 15 y: 10 global static area activation record of main u: 15 v: 10 main bp return value return address first call to gcd

  18. Activation Records x: 15 y: 10 global static area activation record of main u: 15 v: 10 main bp return value return address first call to gcd u: 10 v: 5 old bp return value return address second call to gcd

  19. Activation Records x: 15 y: 10 global static area activation record of main u: 15 v: 10 main bp return value return address first call to gcd u: 10 v: 5 old bp return value return address second call to gcd u: 5 v: 0 old bp return address third call to gcd

  20. Activation Records x: 15 y: 10 global static area activation record of main u: 15 v: 10 main bp return value return address first call to gcd u: 10 v: 5 old bp rv: 5 return address second call to gcd

  21. Activation Records x: 15 y: 10 global static area activation record of main u: 15 v: 10 main bp rv : 5 return address first call to gcd

  22. Activation Records x: 15 y: 10 global static area activation record of main u: 15 v: 10 main bp rv : 5 return address first call to gcd this value sent to printf

  23. Sample code int x = 2; void f(int n) { static int x= 1; g(n); x--; } void g(int m) { int y=m-1; if (y>0) { f(y); x--; g(y); } } int main () { g(x) return 0; }

  24. Activation Records x: 2 f::x 1 global static area bp return value activation record of main sp

  25. Activation Records x: 2 f::x 1 global static area return value activation record of main bp m: 2 main bp return address y: 1 call to g( ) sp

  26. Activation Records x: 2 f::x 1 global static area return value activation record of main m: 2 main bp return address y: 1 call to g( ) n: 10 g: bp return address call to f( ) bp sp

  27. Activation Records x: 2 f::x 1 global static area return value activation record of main m: 2 main bp return address y: 1 call to g( ) n: 10 g: bp return address call to f( ) m: 1 main bp return address y: 0 call to g( ) bp sp

  28. Arrays on the stack void f ( int x, char c) { int a[10]; double y; … } Consider function f:

  29. Activation Record high memory bp x: 2 c: 1 old bp: return address a[9] … a[2] a[1] a[0] y offset of a = -13*2 = -26 sp calculation of a[i] a[i] = bp + a + 2*i low memory

  30. Calling Sequence • Create space for return value • push 0 • Store a return address • push [pc + 4] • Store the current base pointer • push bp • Set bp to new address • mov bp, sp • Create space for local variables • Compute the arguments and store them in the new activation record • (push arguments in order) • Jump to the new address

  31. Return Sequence • change the sp to pop the arguments • load the control link to the bp • pop bp • jump to the return address • pop pc • copy retvalue into right location

More Related