340 likes | 623 Views
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
E N D
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 • Fully Dynamic • LISP
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
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
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
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
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
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
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
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
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
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
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)); }
Activation Records x: 15 y: 10 global static area
Activation Records x: 15 y: 10 global static area activation record of main
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
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
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
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
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
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
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; }
Activation Records x: 2 f::x 1 global static area bp return value activation record of main sp
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
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
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
Arrays on the stack void f ( int x, char c) { int a[10]; double y; … } Consider function f:
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
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
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