Upload
gyles-day
View
259
Download
0
Embed Size (px)
DESCRIPTION
Scope and Lifetime of Variables 30/01/ Chapter 6 Code Generation and Data Types
Citation preview
RUNTIME ENVIRONMENT AND VARIABLE BINDINGSHow to manage local variables
Outline Lifetime of variables Memory organization for programs Stack-based runtime environment
Without local procedure With local procedure
030523/ /
2
2301380 Chapter 6 Code Generation and Data Types
Scope and Lifetime of Variables
030523/ /
3
2301380 Chapter 6 Code Generation and Data Types
Scope of Variables
030523/ /2301380 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
Static Scope
030523/ /
5
2301380 Chapter 6 Code Generation and Data Types
IN12
IN22
IN21
C1C2
Local variables declared in a child node in the hierarchy can be seen from its parent node.
Dynamic Scope
030523/ /
6
2301380 Chapter 6 Code Generation and Data Types
Local variables declared in a node can be seen from its predecessors in the path. Call FST
Call SCND
Call SCND
Call FST
Call IN
Lifetime: Categories of Variables
030523/ /2301380 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.
Memory Organization for Programs
030523/ /
8
2301380 Chapter 6 Code Generation and Data Types
Memory Area
030523/ /
9
2301380 Chapter 6 Code Generation and Data Types
Code area
Global/static area
stack
Free space
Heap
Main memory
registersData area
Code Area
030523/ /2301380 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.
Data Area
030523/ /2301380 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.
Memory Areas for Variables
030523/ /2301380 Chapter 6 Code Generation and Data Types
12
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.
030523/ /
13
2301380 Chapter 6 Code Generation and Data Types
Call Sequence Return Sequence
Static Run-time Environment
030523/ /
14
2301380 Chapter 6 Code Generation and Data Types
Static runtime Environments
030523/ /2301380 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
Example
030523/ /
16
2301380 Chapter 6 Code Generation and Data Types
Without local procedure
Stack-based Runtime Environment
030523/ /
17
2301380 Chapter 6 Code Generation and Data Types
Local Procedureprogram main;{ int a[10];
func cal(a[10]:int){ float ans;
func sum(e[10]:int)
{ int t;…return(t);
}
func ave(b[10],n:int){ int ans;ans=sum(b);return(total/n);}ans=ave(a,10);return;}input(a);cal(a);return;
}
030523/ /
18
2301380 Chapter 6 Code Generation and Data Types
Environment Without Local Procedures
030523/ /2301380 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.
Example
030523/ /
20
2301380 Chapter 6 Code Generation and Data Types
mainint x, y;
call fun1
fun1int x; call fun2 call fun3
fun2int y; call fun3
main x yfun3int y;
fun3 yfun2 y
fun1 x
030523/ /
21
2301380 Chapter 6 Code Generation and Data Types
parameters
activation record of
main
control link return addr
sp
fp
fp
sp
splocal var.s
spsp
parameterscontrol link return addr
sp
splocal var.s
spsp
fp
Compute arguments and push into stackStore fp as control linkMove fpPush return addressReserve area for local variablesCalling sequenceReturn sequenceLoad fp into sp (to pop local var.s and return addr)Load control link into fp
fp
Jump to return addrPop arguments
Global area
Dire
ctio
n of
sta
ck g
rowt
h
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
030523/ /
22
2301380 Chapter 6 Code Generation and Data Types
Calling sequence Return sequence
With local procedure
Stack-based Runtime Environment
030523/ /
23
2301380 Chapter 6 Code Generation and Data Types
Environment with Local Procedures
030523/ /
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
Example
030523/ /
25
2301380 Chapter 6 Code Generation and Data Types
mainint x, y;
call fun1
fun1int x;
call fun2 call fun3
fun2int y; call fun3
main x yfun3int y;
fun3 y
fun2 y
fun1 x
Example
030523/ /2301380 Chapter 6 Code Generation and Data Types
26
program main{ int a[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 areaactivation record for mainaccess linkcontrol linkreturn addrans
a[10]
access linkcontrol linkreturn addrans
b[10]
e[10]access linkcontrol linkreturn addrt
n=10;input(a);cal(a);return;
}
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
030523/ /
27
2301380 Chapter 6 Code Generation and Data Types
Calling sequence Return sequence