runtime environments what is in the memory?. 2301373 runtime environment2 outline memory...
TRANSCRIPT
Runtime Environments
What is in the memory?
2301373 Runtime Environment 2
Outline Memory organization during program
execution Static runtime environments Stack-based runtime environments
Without local procedure With local procedure
Parameter passing
2301373 Runtime Environment 3
Memory organization during program execution
Main memory Store large amount of
data Slower access
Registers Very small amount of
data Faster access
Code area
Global/static area
stack
Free space
Heap
Main memory
registers
Data area
2301373 Runtime Environment 4
Code Area Addresses in code
area are static (i.e. no change during execution) for most programming language.
Addresses are known at compile time.
proc 1
proc 2
proc n
entry point
entry point
entry point
2301373 Runtime Environment 5
Data Area
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.
2301373 Runtime Environment 6
Registers General-purpose registers
Used for calculation Special purpose registers
Program counter (pc) Stack pointer (sp) Frame pointer (fp) Argument pointer (ap)
2301373 Runtime Environment 7
Calling Sequence Sequence of operations that must be done for
procedure calls Call sequence
Sequence of operations performed during procedure calls Find the arguments 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. Return sequence
Sequence of operations performed when return from procedure calls Find the arguments and pass them back to the caller. Free the callee environment. Restore the caller environment, including PC.
2301373 Runtime Environment 8
Issues in Call Sequence Which part of the call sequence is
included in the caller code? Which is in the callee code? Save space in the code segment if the call
sequence is included in the callee code. Normally, the caller finds the arguments and
provides them to the callee. Which operation is supported in
hardware? The more operations supported in hardware,
the lower cost (i.e. execution time and space for code) is.
2301373 Runtime Environment 9
Static runtime environments Static data
Both local and global variables are allocated once at the beginning and deallocated 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:FORTRAN 77
2301373 Runtime Environment 10
Memory Organization for Static Runtime EnvironmentPROGRAM TESTCOMMON MAXINTEGER MAXREAL TAB(10), TEMP…QMEAN(TAB, 3, TEMP)…END
SUBROUTINE QMEAN(A, SIZE, MEAN)COMMON MAXINTEGER MAX, SIZEREAL A(SIZE), MEAN, TEMPINTEGER K…END
MAX
TAB(1)TAB(2)…TAB(10)TEMP3
ASIZEMEANReturn addrTEMP K
Global area
Activation record for main
Activation record for QMEAN
2301373 Runtime Environment 11
Stack-based runtime environments
Handle recursive calls Activation records are allocated in stack,
called rumtime stack or call stack One procedure can have more than one
activation records in the stack at one time Call sequence is more complex than the
sequence in static environment
2301373 Runtime Environment 12
Stack-based Environments Without Local Procedures Maintain pointer to the current
activation record Store frame pointer in an fp register
Record the link from an activation record to the previous record 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.
Sometimes, the area for parameters and local variables need to be identified.
A stack pointer is maintained in an sp register.
local var.sreturn addr
control link
parameters
SP
FP
2301373 Runtime Environment 13
Call Sequence in Stack-based Environments Without Local Procedures
Calling sequence Push arguments Push fp as control link Copy sp to fp Store return address Jump to callee Reserve space for local
variables Return sequence
Copy fp to sp Load control link into fp Jump to return address Change sp to pop
arguments
arguments
activation record of
main
control link
return addr
sp
fp
fp
sp
splocal var.s
sp
sp
argumentscontrol link
return addr
sp
splocal var.s
sp
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
Dire
ctio
n of
sta
ck g
row
th
2301373 Runtime Environment 14
Activation Treemain(){ …;
g(x); return(0); }
void g(int m){ …
f(y); … g(y);…
}
void f(int n){ g(n);
…}
main
g(2)
f(1) g(1)
g(1)
Global area
activation record for main
activation record for g(2)
activation record for f(1)
activation record for g(1)
activation record for g(1)
2301373 Runtime Environment 15
Calculating Address in Stack-Based Environments
Address of local variables and parameters are calculated dynamically during execution time
Offset of variables and parameters from fp is: static known during compile time Stored in symbol table
Address of x =fp+xOffset
Address of a =fp+aOffset
parameters
control link
return address
local variables
fp
sp
x
a
aOffset
xOffset
2301373 Runtime Environment 16
Considerations in Address Calculation
Sizes of variables depend on data types Addressing elements in array
Number of dimensions Size of array Ordering in storage
Row-major order Column-major order
2301373 Runtime Environment 17
Local TemporariesX = (a+b)/((c-d)*f(i))
Values of (a+b) and (c-d) must be stored before f can be called.
Thus, area for temporary variables must be reserved in the stack.
Usually, temporaries are pushed into stacks after local variables.
2301373 Runtime Environment 18
Nested Declarations Local variables of blocks can be defined.
void f{ int x; int i;
…{ char x; int j;
…}…
}
A block could be treated as a procedure, but it is inefficient.
Another simpler method is to: push local variables in stack when the block is entered pop the local variables when the block is exited
control link of f
xj
return addr of f
xi
rest of stack
activationrecord
of f
local var. of block
2301373 Runtime Environment 19
Local Proceduresprogram main;var a[10]:int;proc cal(a[10]:int);var ans:real;
func sum(e[10]:int);var t: int;begin
…return(t);
endfunc
ave(b[10],n:int);var ans:int;begin
ans=sum(b);return(total/n);
end
begin
ans=ave(a,10); return;end
begininput(a);cal(a);return;
end
2301373 Runtime Environment 20
Stack-based Environments with Local Procedures
Access link/static link must be included in each activation records Represents the defining
environment Access link and control
link need not be the same Control link
represents the calling environment
return addr
access link
control link
parameters
local var.
activation record
2301373 Runtime Environment 21
Call Sequence in Stack-based Environments With Local Proceduresprogram main;var a[10],n :int;
proccal(a[10]:int);var ans:real;
func sum(e[10]:int);var t: int;begin
…return(t);
endfunc ave(b[10]);var ans:int;begin
ans=sum(b);return(ans/n);
endbegin
ans=ave(a,10));return;
endbegin
n=10;input(a);cal(a);return;
end
global areaactivation record for
main
activation record for cal
access linkcontrol linkreturn addr
ans
a[10]
activation record for ave
access linkcontrol linkreturn addr
ans
b[10]
e[10]
activation record for sum
access linkcontrol linkreturn addr
t
ans
2301373 Runtime Environment 22
Call Sequence Calling sequence
Push arguments Push access link Push fp as control link Copy sp to fp Store return address Jump to callee Reserve space for local variables
How to find access link
Return sequence Copy fp to sp Load control link
into fp Pop access link Jump to return
address Change sp to
pop arguments
2301373 Runtime Environment 23
Accessing Variables in Local Procedures
Access Chaining Follow access links from one activation record
until the required variable is found Inefficient
Display Access links are stored in an array called
display
2301373 Runtime Environment 24
Accessing Chaining Nesting level
Indicates the depth of the procedure in program definition Level 0: outermost
Need to be stored in the symbol table
To find the activation record for the nth nesting level from the activation record in the ith nesting level Follow the access link (n-i) times
Examples: To access ans in cal
Follow links 2-1 times To access n in main
Follow links 2-0 times
global areaactivation record for
main
activation record for cal
nesting level: 1
access linkcontrol linkreturn addr
ans
a[10]
activation record for ave
nesting level: 2
access linkcontrol linkreturn addr
ans
b[10]
e[10]
activation record for sum
nesting level:2
access linkcontrol linkreturn addr
t
2301373 Runtime Environment 25
Display Access links are
stored in the array display.
The activation record of a procedure in the ith nesting level is stoted in display[i].
global areaactivation record for
main
access linkaccess linkcontrol linkreturn addr
ans
a[10]
access linkcontrol linkreturn addr
ans
b[10]
e[10]access linkcontrol linkreturn addr
t
display[0]
display[1]
display[2] display[2]cal
ave
sum
2301373 Runtime Environment 26
Parameter Passing Pass by value Pass by reference Pass by value-result Pass by name
2301373 Runtime Environment 27
Pass by Value Value parameters are
not changed during the execution
Only the value is sent into the procedure, and are used locally
When the control is returned from the callee, the value of the parameter is not passed back to the caller.
void change(int x){ x++;return;
}
void main(){ int y=0;change(y);printf(“%d\n”,y);return;
}
Output:0
2301373 Runtime Environment 28
Pass by Reference The reference to a variable is
passed to the callee. The callee used the reference
(address) to refer to the variable. Indirect addressing is needed to
refer to parameters The variable in the caller and
the referenced memory in the callee share the same memory location.
The value of the variable in the caller is also changed when the referenced in the callee is changed.
void change (int &x)
{ x++;return;
}
void main(){ int y=0;
change(y);printf(“%d\n”,y);return;
}Output:
1
2301373 Runtime Environment 29
Pass by Value-Result The value of the
parameter is copied into the callee when the callee is entered.
New memory location is provided for the parameter in the callee’s activation record. No indirect address is needed
When the control is returned to the caller, the value is copied back.
void change(int x)
{ x++;return;
}
void main(){ int y=0;change(y);printf(“%d\n”,y);return;
}
Output:1
2301373 Runtime Environment 30
Pass by Name The argument is replaced
by the textual representation of the parameter passed from the caller.
The evaluation of the actual parameters must be delayed until the execution.
A procedure (thunk) is called to find the value of the parameter in the callee.
void change(int x){ i=4;x++;return;
}
void main(){ int i=0;inta[5]={0,0,0,0,0};change(a[i]);return;
}
After the call:a={0,0,0,0,1}