Runtime Environments

Download Report

Transcript Runtime Environments

Runtime
Environments
What is in the memory?
Outline



Memory organization during program
execution
Static runtime environments
Stack-based runtime environments



Without local procedure
With local procedure
Parameter passing
2301373
Runtime Environment
2
Memory organization during program
execution

Data area
Main memory
Code area

Global/static area

stack

Registers

Free space

registers
Store large amount of
data
Slower access
Very small amount of
data
Faster access
Heap
Main memory
2301373
Runtime Environment
3
Code Area


Addresses in code
area are static (i.e. no
change during
execution) for most
programming
language.
Addresses are known
at compile time.
2301373
Runtime Environment
entry point
entry point
proc 1
proc 2
entry point
proc n
4
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.


2301373
Stack (LIFO allocation) for procedure activation
record, etc.
Heap for user allocated memory, etc.
Runtime Environment
5
Registers

General-purpose registers


Used for calculation
Special purpose registers




2301373
Program counter (pc)
Stack pointer (sp)
Frame pointer (fp)
Argument pointer (ap)
Runtime Environment
6
Calling Sequence

Sequence of operations that must be done for
procedure calls

Call sequence
 Sequence of operations performed during procedure
calls




Return sequence
 Sequence of operations performed when return from
procedure calls



2301373
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.
Find the arguments and pass them back to the caller.
Free the callee environment.
Restore the caller environment, including PC.
Runtime Environment
7
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?

2301373
The more operations supported in hardware,
the lower cost (i.e. execution time and space
for code) is.
Runtime Environment
8
Static runtime environments

Static data




No dynamic allocation
No recursive call



Both local and global variables are allocated
once at the beginning and deallocated at
program termination
Fixed address
Procedure calls are allowed, but no recursion.
One activation record for each procedure,
allocated statically
Example:FORTRAN 77
2301373
Runtime Environment
9
Memory Organization for
Static Runtime Environment
PROGRAM TEST
COMMON MAX
INTEGER MAX
REAL TAB(10), TEMP
…
QMEAN(TAB, 3, TEMP)
…
END
Global area
Activation record
for main
SUBROUTINE QMEAN(A, SIZE, MEAN)
COMMON MAX
Activation record
INTEGER MAX, SIZE
for QMEAN
REAL A(SIZE), MEAN, TEMP
INTEGER K
…
END
2301373
Runtime Environment
MAX
TAB(1)
TAB(2)
…
TAB(10)
TEMP
3
A
SIZE
MEAN
Return addr
TEMP
K
10
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
11
Stack-based Environments
Without Local Procedures

Maintain pointer to the current
activation record


Record the link from an activation
record to the previous record



Store frame pointer 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.
local var.s
return addr
control link
SP
FP
parameters
Sometimes, the area for
parameters and local variables
need to be identified.
A stack pointer is maintained in
an sp register.
2301373
Runtime Environment
12
Call Sequence in Stack-based Environments
Without Local Procedures
Global area
Calling sequence







Direction of stack growth

Push arguments
Push fp as control link
Copy sp to fp
Store return address
Jump to callee
Reserve space for local
variables
Return sequence




2301373
Copy fp to sp
Load control link into fp
Jump to return address
Change sp to pop
arguments
activation
record of
main
fp
sp
arguments
control link
return addr
local var.s
sp
sp fp
sp
sp
arguments
control link
return addr
local var.s
sp
sp fp
sp
sp
Calling sequence
Return
Reserve
Compute
Store
Move
Load
Jump
Push
Pop arguments
control
fp
to
return
as
return
area
arguments
control
address
link
for
addr
local
link
and
fpvariables
push
intoand
stack
fp
into
sp
(tointo
pop
local
var.s
return addr)
Runtime Environment
13
Activation Tree
main()
{ …;
g(x);
return(0); }
void g(int m)
{ …
f(y);
…
g(y);
…
}
void f(int n)
{ g(n);
…
}
2301373
Global area
activation
record for
main
main
g(2)
f(1)
g(1)
activation
record for
g(2)
g(1)
activation
record for
f(1)
g(1)
activation
record for
g(1)
Runtime Environment
14
Calculating Address in StackBased 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
parameters
a
aOffset
control link
fp
return address
local variables
xOffset
x
sp
Address of x =fp+xOffset
Address of a =fp+aOffset
2301373
Runtime Environment
15
Considerations in
Address Calculation


Sizes of variables depend on data types
Addressing elements in array



Number of dimensions
Size of array
Ordering in storage


2301373
Row-major order
Column-major order
Runtime Environment
16
Local Temporaries
X = (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
17
Nested Declarations

Local variables of blocks can be defined.
void f
{ int
…
{
}


}
…
rest of stack
x;
int
char x;
…
i;
int
control link of f
j;
return addr of f
x
i
x
j
activation
record
of f
local var.
of block
A block could be treated as a procedure, but it is
inefficient.
Another simpler method is to:


2301373
push local variables in stack when the block is entered
pop the local variables when the block is exited
Runtime Environment
18
Local Procedures
program
main;
var a[10]:int;
proc
cal(a[10]:int);
var
ans:real;
begin
func sum(e[10]:int);
ans=ave(a,10);
var t: int;
return;
begin
end
…
return(t);
begin
end
input(a);
func ave(b[10],n:int);
cal(a);
var
ans:int;
return;
begin
end
ans=sum(b);
return(total/n);
end
2301373
Runtime Environment
19
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

2301373
parameters
access link
activation record
control link
return addr
local var.
represents the calling
environment
Runtime Environment
20
Call Sequence in Stack-based Environments
global area
With Local Procedures
program
main;
var
a[10],n :int;
proc
cal(a[10]:int);
var ans:real;
func sum(e[10]:int);
var
t: int;
begin
… ans
return(t);
end
func ave(b[10]);
var
ans:int;
begin
ans=sum(b);
return(ans/n);
end
begin
ans=ave(a,10));
return;
end
begin
n=10;
input(a);
cal(a);
return;
end
2301373
Runtime Environment
activation
record for
main
a[10]
access link
control link
return addr
activation
record
for cal
ans
b[10]
access link
control link
return addr
activation
record
for ave
ans
e[10]
access link
control link
return addr
activation
record
for sum
t
21
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

Return sequence






Copy fp to sp
Load control link
into fp
Pop access link
Jump to return
address
Change sp to
pop arguments
How to find access link
2301373
Runtime Environment
22
Accessing Variables in
Local Procedures

Access Chaining



Follow access links from one activation record
until the required variable is found
Inefficient
Display

2301373
Access links are stored in an array called
display
Runtime Environment
23
Accessing Chaining global area

Nesting level

Indicates the depth of the
procedure in program definition



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


Level 0: outermost
Follow the access link (n-i) times
Examples:

To access ans in cal


To access n in main

2301373
Follow links 2-1 times
activation
record for
main
a[10]
access link
control link
return addr
ans
b[10]
access link
control link
return addr
ans
e[10]
access link
control link
return addr
t
activation
record
for cal
nesting level: 1
activation
record
for ave
nesting level: 2
activation
record
for sum
nesting level:2
Follow links 2-0 times
Runtime Environment
24
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 area
activation
record for
main
a[10]
access link
control link
return addr
ans
display[0]
display[1]
cal
display[2]
b[10]
access link
control link
return addr
ave
ans
e[10]
access link
control link
return addr
t
2301373
Runtime Environment
sum
25
Parameter Passing




Pass
Pass
Pass
Pass
2301373
by
by
by
by
value
reference
value-result
name
Runtime Environment
26
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.
2301373
void change(int x)
{ x++;
return;
}
void main()
{ int y=0;
change(y);
printf(“%d\n”,y);
return;
}
Output:
0
Runtime Environment
27
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.
2301373
Runtime Environment
void change (int
&x)
{ x++;
return;
}
void main()
{ int y=0;
change(y);
printf(“%d\n”,y);
return;
}
Output:
1
28
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.


void main()
{ int y=0;
change(y);
printf(“%d\n”,y);
return;
No indirect address is needed
}
When the control is
returned to the caller, the
value is copied back.
2301373
void change(int x)
{ x++;
return;
}
Runtime Environment
Output:
1
29
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.
2301373
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}
Runtime Environment
30