Title: Runtime Environments
1Chapter 7
Gang S. Liu College of Computer Science
Technology Harbin Engineering University
2Introduction
- In previous chapter we have studied the phases of
a compiler - These stages
- depend only on the properties of the source
language. - are completely independent
- Of the target (machine or assembly) language
- The properties of the target machine
- Operating system
- Scanning
- Parsing
- Static semantic analysis
3Last Stage
- The last stage of compile process is
__________________ . - Much of this task is dependent on the details of
the target machine. - The general characteristics of the code
generation remains the same across a wide
variety of architectures.
the code generation
4Runtime Environment
- Runtime Environment is the structure of the
target computers registers and memory that
serves to manage memory and maintain the
information needed to guide the execution
process. - Three kinds of run time environment
- Fully static environment (FORTRAN77)
- Stack-based environment (C, C, PASCAL)
- Fully dynamic environment (LISP)
- Hybrids of these are also possible.
5Memory Organization
Memory
ROM
Register Area
Code Area RAM
Data Area
In most compiled languages, it is not possible to
make changes to the code area during
execution. The code area is fixed prior to the
execution, and all code addresses are computable
at compile time.
6Code Memory
Entry point for procedure 1
Code for Procedure 1 Code for Procedure
2 . . Code for Procedure n
Entry point for procedure 2
Entry point for procedure n
Entry point of each procedure and function is
known at compile time.
7Data Area
- Only a small part of data can be assigned fixed
locations before execution begins - Global and/or static data
- Compile-time constants
- Large integer values
- Floating-point values
- Literal strings
8Dynamic Memory
- The memory area for the allocation of dynamic
data can be organized in many different ways. - A typical organization divides the dynamic memory
into
- stack area (LIFO protocol)
- heap area
9Memory Organization
code area global/static area stack free
space heap
free space
10Procedure Activation Record
- An important unit of memory
- Procedure activation record contains memory
allocated for the local data of a procedure or
function when it is called, or activated.
- The picture illustrates the general
organization of an activation record. - Details depend on the architecture of target
machine and properties of the language. - When activation records are kept on stack, they
are called stack frames.
arguments bookkeeping information (return
address) local data local temporaries
11Registers
- Registers may be used to store temporaries, local
variables, or even global variables. - When a processor has many registers, the entire
static area and whole activation records may be
kept in the registers. - Special purpose registers
- Program counter (PC)
- Stack pointer (SP)
12Runtime Environment
- Runtime Environment is the structure of the
target computers registers and memory that
serves to manage memory and maintain the
information needed to guide the execution
process. - Three kinds of run time environment
- Fully static environment (FORTRAN77)
- Stack-based environment (C, C, PASCAL)
- Fully dynamic environment (LISP)
- Hybrids of these are also possible.
13Fully Static Runtime Environment
- The simplest kind of a runtime environment.
- All data are static, remaining in memory for the
duration of the program. - All variables can be accessed directly via fixed
addresses - Each procedure has only a single activation
record, which is allocated statically prior the
execution. - Such environment can be used to implement a
language in which - Example
- There are no pointers or dynamic allocation,
- Procedures cannot be called recursively.
FORTRAN77
14Memory Organization (static runtime environment)
code for main procedure code for procedure
1 code for procedure n global data
area activation record of main procedure activatio
n record of procedure 1 activation record of
procedure n
Code area
Data area
15Example 7.1
- PROGRAM TEST
- COMMON MAXSIZE
- INTEGER MAXSIZE
- REAL TABLE(10), TEMP
- MAXSIZE10
- READ , TABLE(1), TABLE(2), TABLE(3)
- CALL QADMEAN(TABLE, 3, TEMP)
- PRINT , TEMP
- END
SUBROUTINE QUADMEAN(A, SIZE, QMEAN)
COMMON MAXSIZE INTEGER MAXSIZE, SIZE
REAL A(SIZE), QMEAN, TEMP INTEGER K
TEMP 0.0 IF ((SIZE.GT.MAXSIZE).OR.)SIZE.LT.
1)) GOTO 99 DO 10 K1,SIZE TEMP
TEMP A(K)A(K) 10 CONTINUE 99 QMEANSQRT(TEMP/SIZ
E) RETURN END
16Example 7.1 Data Area
Global area
MAXSIZE
TABLE (1) (2) (10)
TEMP
3
A
SIZE
QMEAN
return address
TEMP
K
arguments bookkeeping information (return
address) local data local temporaries
Activation record of main
Activation record of QUADMEAN
Arrows indicate the values of the parameters
17Stack-based Runtime Environment
- In a language in which recursive calls are
allowed, activation records cannot be allocated
statically. - Activation records are allocated in a stack-based
fashion. - This stack is called the stack of activation
records (runtime stack, call stack). - Each procedure may have several different
activation records at one time.
18Global Procedures
- In a language where all procedures are global
(the C language), a stack-based environment
requires two things - A pointer the current activation record to allow
access to local variables. - This pointer is called the frame pointer (fp) and
is usually kept in a register. - The position or size of the callers activation
record - This information is commonly kept in the current
activation record as a pointer to the previous
activation record and referred as the control
link or dynamic link. - Sometimes, the pointer is called the old fp
- Additionally, there may be a stack pointer (sp)
- It always points to the top of the stack
19Example 7.2
Suppose the user inputs the values 15 and 10 to
this program
- include ltstdio.hgt
- int x,y
- int gcd(int u,int v)
-
- if (v 0) return u
- else return gcd(v, uv)
-
- main()
-
- scanf(dd, x, y)
- printf(d\n, gcd(x,y))
- return 0
uv
v
u
5
10
15
0
5
10
End
0
5
20Memory Organization
code area global/static area stack free
space heap
free space
21Procedure Activation Record
- An important unit of memory
- Procedure activation record contains memory
allocated for the local data of a procedure or
function when it is called, or activated.
arguments bookkeeping information (return
address) local data local temporaries
22Example 7.2
Suppose the user inputs the values 15 and 10 to
this program
- include ltstdio.hgt
- int x,y
- int gcd(int u,int v)
-
- if (v 0) return u
- else return gcd(v, uv)
-
- main()
-
- scanf(dd, x, y)
- printf(d\n, gcd(x,y))
- return 0
uv
v
u
5
10
15
0
5
10
End
0
5
23Example 7.2 (cont)
x 15 y 10
Global static area
include ltstdio.hgt int x,y int gcd(int u,int
v) if (v 0) return u else return
gcd(v, uv) main() scanf(dd, x,
y) printf(d\n, gcd(x,y)) return 0
Activation record of main
u 15 v 10 control link return address
Activation record of first call to gcd
u 10 v 5 control link return address
Activation record of second call to gcd
u 5 v 0 control link return address
Activation record of third call to gcd
fp
Direction of stack grow
sp
free space
24Example 7.3
- int x2
- void g(int) / prototype /
- void f (int n)
-
- static int x1
- g(n)
- x--
-
- void g(int m)
-
- int y m-1
- if(ygt0)
-
- f(y)
- x--
- g(y)
-
main () g(x) return 0
x1 g(1) x1-10
ym-12-11 y1gt0 f(1) x--2-11 g(1)
ym-11-10 y0
ym-11-10 y0
25Memory Organization
code area global/static area stack free
space heap
free space
26Procedure Activation Record
- An important unit of memory
- Procedure activation record contains memory
allocated for the local data of a procedure or
function when it is called, or activated.
arguments bookkeeping information (return
address) local data local temporaries
27Example 7.3
- int x2
- void g(int) / prototype /
- void f (int n)
-
- static int x1
- g(n)
- x--
-
- void g(int m)
-
- int y m-1
- if(ygt0)
-
- f(y)
- x--
- g(y)
-
main () g(x) return 0
x1 g(1) x1-10
ym-12-11 y1gt0 f(1) x--2-11 g(1)
ym-11-10 y0
28Example 7.3 (cont)
x 2 x (from f) 1
m2 control link return address y 1
n1 control link return address
m1 control link return address y 0
free space
Global static area
Activation record of main
Activation record of call to g
Activation record of call to f
Activation record of call to g
fp
During the second call to g
sp
29Example 7.3
- int x2
- void g(int) / prototype /
- void f (int n)
-
- static int x1
- g(n)
- x--
-
- void g(int m)
-
- int y m-1
- if(ygt0)
-
- f(y)
- x--
- g(y)
-
main () g(x) return 0
x1 g(1) x1-10
ym-12-11 y1gt0 f(1) x--2-11 g(1)
ym-11-10 y0
ym-11-10 y0
30Example 7.3 (cont)
x 1 x (from f) 0
m2 control link return address y 1
m1 control link return address y 0
free space
Global static area
Activation record of main
Activation record of call to g
fp
Activation record of call to g
sp
During the third call to g
31Activation Tree
- A useful tool for the analysis of complex
structures in a program. - Activation tree each activation record becomes a
node on this tree, and the descendants of each
node represent all the calls made during the call
corresponding to that node.
main()
g(2)
f(1)
g(1)
g(1)
32Homework