Runtime EnvironmentsRuntime Environments
Compiler ConstructionCompiler Construction
Chapter 7Chapter 7
In This LectureIn This Lecture
Runtime EnvironmentsRuntime EnvironmentsLanguage Features for different Language Features for different
EnvironmentsEnvironmentsScoping and AllocationScoping and AllocationProcedure CallsProcedure CallsParameter PassingParameter Passing
Runtime EnvironmentsRuntime Environments
Three typesThree typesFully StaticFully Static
Fortran77Fortran77
Stack-basedStack-basedC, C++, Pascal, AdaC, C++, Pascal, Ada
Fully DynamicFully DynamicLISPLISP
Memory OrganizationMemory 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
Static Code and DataStatic Code and Data The code area of a program is fixed prior to executionThe code area of a program is fixed prior to execution Thus the address for code can be computed at compile Thus the address for code can be computed at compile
timetime Actually, the location of the code is usually relative to some base Actually, the location of the code is usually relative to some base
register, or some other memory addressing schemeregister, or some other memory addressing scheme The address for data (variables etc.) is in general not The address for data (variables etc.) is in general not
assigned fixed locations at compile timeassigned fixed locations at compile time Static data however, does have a fixed memory address Static data however, does have a fixed memory address
and is known at compile timeand is known at compile time Constants can also be assigned a fixed addressConstants can also be assigned a fixed address
This is usually reserved for strings and other large constantsThis is usually reserved for strings and other large constants Other constants like ‘0’ or ‘1’ are inserted directly into the codeOther constants like ‘0’ or ‘1’ are inserted directly into the code
Dynamic Data AllocationDynamic Data Allocation
The memory for dynamic data is typically The memory for dynamic data is typically organized into two major areasorganized into two major areasThe stackThe stack
Used for local variables, parameter variables, Used for local variables, parameter variables, return addresses and return valuesreturn addresses and return values
The heapThe heapUsed for dynamically allocated variablesUsed for dynamically allocated variables
Runtime Storage OrganizationRuntime Storage Organization
Code Area
Global Static Area
Stack
Free space
HeapThe heap generally growstowards lower memory
The stack generally growstowards higher memory
Procedure Activation RecordsProcedure Activation Records
arguments (parameters)
return address etc.
local variables
In C – like languages these records are stored on the stackIn Fortran they are in the static storage areaIn Lisp they are in the heap area
called a stack-frameif stored on the stack
Depending on theNumber of Registersmuch of this data might be stored in registers
Calling Sequence Calling Sequence Operations that must be performed when a Operations that must be performed when a
procedure is called the procedure is called the calling sequencecalling sequence IncludesIncludes
allocation of memory for activation recordallocation of memory for activation record computation and storing of argumentscomputation and storing of arguments storing and setting necessary registersstoring and setting necessary registers
Additional operations are needed when a Additional operations are needed when a procedure returns called the procedure returns called the return sequencereturn sequence placing the return valueplacing the return value readjustment of registersreadjustment of registers de-allocation of memory for activation recordde-allocation of memory for activation record
Important RegistersImportant Registers
Program Counter (pc)Program Counter (pc)set this for procedure callsset this for procedure calls
Stack Pointer (sp)Stack Pointer (sp) for activation frame info in stack-based for activation frame info in stack-based
environmentsenvironmentsBase Pointer (bp)Base Pointer (bp)
similar to the stack pointer, but points to the similar to the stack pointer, but points to the beginning of the activation recordbeginning of the activation record
Fully Static Fully Static Runtime EnvironmentsRuntime Environments
All variables are allocated staticallyAll variables are allocated statically including local variablesincluding local variables
No pointers or dynamic allocationNo pointers or dynamic allocationNo recursionNo recursion
why?why?All procedures have a single activation All procedures have a single activation
recordrecordallocated statically before program executionallocated statically before program execution
Static Runtime EnvironmentStatic 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
Stack-basedStack-basedRuntime EnvironmentsRuntime Environments
Supports RecursionSupports RecursionNo way of knowing at compile time, how many No way of knowing at compile time, how many
times a procedure will need to be called times a procedure will need to be called Use stack-based activation records in order to Use stack-based activation records in order to
support recursionsupport recursion
GCDGCD
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 RecordsActivation Records
x: 15y: 10
global static area
Activation RecordsActivation Records
x: 15y: 10
global static area
activation record of main
Activation RecordsActivation Records
x: 15y: 10
global static area
activation record of main
u: 15v: 10
main bpreturn value
return address
first call to gcd
Activation RecordsActivation Records
x: 15y: 10
global static area
activation record of main
u: 10v: 5old bp
return valuereturn address
first call to gcd
second call to gcd
u: 15v: 10
main bpreturn value
return address
Activation RecordsActivation Records
x: 15y: 10
global static area
activation record of main
u: 10v: 5old bp
return valuereturn address
first call to gcd
second call to gcd
u: 15v: 10
main bpreturn value
return address
u: 5v: 0old bp
return address
third call to gcd
Activation RecordsActivation Records
x: 15y: 10
global static area
activation record of main
u: 10v: 5old bprv: 5
return address
first call to gcd
second call to gcd
u: 15v: 10
main bpreturn value
return address
Activation RecordsActivation Records
x: 15y: 10
global static area
activation record of main
u: 15v: 10
main bprv : 5
return address
first call to gcd
Activation RecordsActivation Records
x: 15y: 10
global static area
activation record of main
u: 15v: 10
main bprv : 5
return address
first call to gcd
this value sent to printf
Sample codeSample 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 RecordsActivation Records
x: 2f::x 1
global static area
return value activation record of mainspbp
Activation RecordsActivation Records
x: 2f::x 1
global static area
return value activation record of main
m: 2main bp
return addressy: 1
call to g( )
sp
bp
Activation RecordsActivation Records
x: 2f::x 1
global static area
return value activation record of main
m: 2main bp
return addressy: 1
call to g( )
n: 10g: bp
return address
call to f( )
sp
bp
Activation RecordsActivation Records
x: 2f::x 1
global static area
return value activation record of main
m: 2main bp
return addressy: 1
call to g( )
n: 10g: bp
return address
call to f( )
m: 1main bp
return addressy: 0
call to g( )
sp
bp
Arrays on the stackArrays on the stack
void f ( int x, char c){
int a[10];double y;…
}
Consider function f:
Activation RecordActivation Record
x: 2c: 1
old bp:return address
a[9] …
a[2]a[1]a[0]
ysp
bp
low memory
high memory
offset of a = -13*2 = -26
a[i] = bp + a + 2*i
calculation of a[i]
Calling SequenceCalling Sequence Create space for return valueCreate space for return value
push 0push 0 Store a return address Store a return address
push [pc + 4]push [pc + 4] Store the current base pointerStore the current base pointer
push bppush bp Set bp to new addressSet bp to new address
mov bp, spmov bp, sp Create space for local variablesCreate space for local variables Compute the arguments and store them in the new Compute the arguments and store them in the new
activation recordactivation record (push arguments in order)(push arguments in order)
Jump to the new addressJump to the new address
Return SequenceReturn Sequence
change the sp to pop the argumentschange the sp to pop the arguments load the control link to the bpload the control link to the bp
pop bppop bp jump to the return addressjump to the return address
pop pcpop pccopy retvalue into right locationcopy retvalue into right location