Presentation is loading. Please wait.

Presentation is loading. Please wait.

RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.

Similar presentations


Presentation on theme: "RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables."— Presentation transcript:

1 RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables

2 Outline  Lifetime of variables  Memory organization for programs  Stack-based runtime environment  Without local procedure  With local procedure 30/01/59 2 2301380 Chapter 6 Code Generation and Data Types

3 Scope and Lifetime of Variables 30/01/59 3 2301380 Chapter 6 Code Generation and Data Types

4 Scope of Variables 30/01/592301380 Chapter 6 Code Generation and Data Types 4  Static scope: scope of variables are defined by the block location.  Nested static scope: JavaScript, Ada  Non-nested static scope: C, C++  Dynamic scope: scope of variables are defined by the sequence of subprogram calls.  Scope is determined at execution time.  APL, SNOBOL

5 MAIN Static Scope 30/01/59 5 2301380 Chapter 6 Code Generation and Data Types FST SCND IN11 IN12 IN22 IN21 C1 C2 MAINFSTIN11C1C2IN12SCNDIN21IN22 Local variables declared in a child node in the hierarchy can be seen from its parent node.

6 MAIN Dynamic Scope 30/01/59 6 2301380 Chapter 6 Code Generation and Data Types FST SCND IN MAINFSTINSCND Local variables declared in a node can be seen from its predecessors in the path. Call FST Call SCND Call FST Call IN

7 Lifetime: Categories of Variables 30/01/592301380 Chapter 6 Code Generation and Data Types 7  Static variables  Bound to the same memory location during the whole execution time of the program.  Stack-dynamic variables  Bound to a memory location during one activation of the subprogram.  Explicit heap-dynamic variables  Allocation/deallocation of memory is done explicitly by programmers.  Implicit heap-dynamic variables  Allocation/deallocation of memory is done implicitly when it is needed.

8 Memory Organization for Programs 30/01/59 8 2301380 Chapter 6 Code Generation and Data Types

9 Memory Area 30/01/59 9 2301380 Chapter 6 Code Generation and Data Types Code area Global/static area stack Free space Heap Main memory registers Data area

10 Code Area 30/01/592301380 Chapter 6 Code Generation and Data Types 10  Addresses in code area are static (i.e. no change during execution) for most programming language.  Addresses are known at compile time.

11 Data Area 30/01/592301380 Chapter 6 Code Generation and Data Types 11  Addresses in data area are static for some data and dynamic for others.  Static data are located in static area.  Dynamic data are located in stack or heap. Stack (LIFO allocation) for procedure activation record, etc. Heap for user allocated memory, etc.  Activation Record  Collection of data used for each specific subprogram activation.  Contain local variables, parameters, return address, etc.

12 Memory Areas for Variables 30/01/592301380 Chapter 6 Code Generation and Data Types 12 Static variable In global/static area Stack-dynamic variable In stack area For local variables in subprograms Explicit heap-dynamic variable In heap area Programmers explicitly allocate the memory. Implicit heap-dynamic variable In heap area Systems implicitly allocate the memory.

13 Calling Sequence  Find the parameters and pass them to the callee.  Save the caller environment, i.e. local variables in activation records, return address.  Create the callee environment, i.e. local variables in activation records, callee’s entry point.  Find the parameters and pass them back to the caller.  Free the callee environment.  Restore the caller environment, including PC. 30/01/59 13 2301380 Chapter 6 Code Generation and Data Types Call SequenceReturn Sequence

14 Static Run-time Environment 30/01/59 14 2301380 Chapter 6 Code Generation and Data Types

15 Static runtime Environments 30/01/592301380 Chapter 6 Code Generation and Data Types 15  Static data  Both local and global variables are allocated once at the beginning and de-allocated at program termination  Fixed address  No dynamic allocation  No recursive call  Procedure calls are allowed, but no recursion.  One activation record for each procedure, allocated statically

16 Example 30/01/59 16 2301380 Chapter 6 Code Generation and Data Types

17 Without local procedure Stack-based Runtime Environment 30/01/59 17 2301380 Chapter 6 Code Generation and Data Types

18 Local Procedure programmain; {inta[10]; func cal(a[10]:int) {floatans; func sum(e[10]:int) {intt; … return(t); } func ave(b[10],n:int) {intans; ans=sum(b); return(total/n); } ans=ave(a,10); return; } input(a); cal(a); return; } 30/01/59 18 2301380 Chapter 6 Code Generation and Data Types main cal sumave

19 Environment Without Local Procedures 30/01/592301380 Chapter 6 Code Generation and Data Types 19  Run-time environment (i.e. activation record) of each subprogram is pushed into the stack when a subprogram is called.  A stack pointer, pointing to the top of stack, is maintained in an sp register.  A frame pointer, pointing to the current activation record, is maintained in an fp register  In each activation record, a link from an activation record to the activation record of the caller, called a dynamic link or control link is stored.

20 Example 30/01/59 20 2301380 Chapter 6 Code Generation and Data Types main int x, y; call fun1 fun1 int x; call fun2 call fun3 fun2 int y; call fun3 main xy fun3 int y; fun3 y fun2 y fun1 x fun3 y

21 30/01/59 21 2301380 Chapter 6 Code Generation and Data Types parameters activation record of main control link return addr sp fp sp local var.s sp parameters control link return addr sp local var.s sp fp Compute arguments and push into stackStore fp as control linkMove fpPush return addressReserve area for local variables Calling sequenceReturn sequence Load fp into sp (to pop local var.s and return addr) Load control link into fp fp Jump to return addrPop arguments Global area Direction of stack growth

22 Call Sequence  Push parameters  Push fp as control link  Copy sp to fp  Store return address  Jump to callee  Reserve space for local variables  Copy fp to sp  Load control link into fp  Jump to return address  Change sp to pop parameters 30/01/59 22 2301380 Chapter 6 Code Generation and Data Types Calling sequenceReturn sequence

23 With local procedure Stack-based Runtime Environment 30/01/59 23 2301380 Chapter 6 Code Generation and Data Types

24 Environment with Local Procedures 30/01/59 24 2301380 Chapter 6 Code Generation and Data Types  Access link/static link must be included in each activation records  Represents the defining environment  Control link  represents the calling environment  Access link and control link need not be the same

25 Example 30/01/59 25 2301380 Chapter 6 Code Generation and Data Types main int x, y; call fun1 fun1 int x; call fun2 call fun3 fun2 int y; call fun3 main xy fun3 int y; fun3 y fun2 y fun1 x fun3 y

26 Example 30/01/592301380 Chapter 6 Code Generation and Data Types 26 programmain {inta[10],n ; int cal(int x) {float ans; int func sum(e[10]) { int t; … return(t); } void func ave(b[10]) { ans:int; ans=sum(b); return(ans/n); } ans=ave(x,10)); return; } global area activation record for main access link control link return addr ans a[10] access link control link return addr ans b[10] e[10] access link control link return addr t n=10; input(a); cal(a); return; }

27 Call Sequence  Push parameters  Push access link  Push fp as control link  Copy sp to fp  Store return address  Jump to callee  Reserve space for local variables  Copy fp to sp  Load control link into fp  Pop access link  Jump to return address  Change sp to pop parameters 30/01/59 27 2301380 Chapter 6 Code Generation and Data Types Calling sequenceReturn sequence


Download ppt "RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables."

Similar presentations


Ads by Google