Presentation is loading. Please wait.

Presentation is loading. Please wait.

Runtime Environments Compiler Construction Chapter 7.

Similar presentations


Presentation on theme: "Runtime Environments Compiler Construction Chapter 7."— Presentation transcript:

1 Runtime Environments Compiler Construction Chapter 7

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

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

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

5 Static Code and Data The code area of a program is fixed prior to execution The code area of a program is fixed prior to execution Thus the address for code can be computed at compile time 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 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 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 Static data however, does have a fixed memory address and is known at compile time Constants can also be assigned a fixed address Constants can also be assigned a fixed address This is usually reserved for strings and other large constants This is usually reserved for strings and other large constants Other constants like ‘0’ or ‘1’ are inserted directly into the code 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 memory for dynamic data is typically organized into two major areas The stack The stack Used for local variables, parameter variables, return addresses and return values Used for local variables, parameter variables, return addresses and return values The heap The heap Used for dynamically allocated variables Used for dynamically allocated variables

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

8 Procedure Activation Records arguments (parameters) return address etc. local variables 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 called a stack-frame if stored on the stack Depending on the Number of Registers much of this data might be stored in registers

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

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

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

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

13 Stack-based Runtime Environments Supports Recursion Supports Recursion No way of knowing at compile time, how many times a procedure will need to be called 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 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: 10 v: 5 old bp return value return address first call to gcd second call to gcd u: 15 v: 10 main bp return value return address

19 Activation Records x: 15 y: 10 global static area activation record of main u: 10 v: 5 old bp return value return address first call to gcd second call to gcd u: 15 v: 10 main bp return value return address 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: 10 v: 5 old bp rv: 5 return address first call to gcd second call to gcd u: 15 v: 10 main bp return value return address

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 return value activation record of main sp bp

25 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( ) sp bp

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( ) sp bp

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( ) sp bp

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

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

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

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


Download ppt "Runtime Environments Compiler Construction Chapter 7."

Similar presentations


Ads by Google