Runtime Environments - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Runtime Environments

Description:

Chapter 7 Runtime Environments Gang S. Liu College of Computer Science & Technology Harbin Engineering University Introduction In previous chapter we have studied the ... – PowerPoint PPT presentation

Number of Views:74
Avg rating:3.0/5.0
Slides: 44
Provided by: educ5490
Category:

less

Transcript and Presenter's Notes

Title: Runtime Environments


1
Chapter 7
  • Runtime Environments

Gang S. Liu College of Computer Science
Technology Harbin Engineering University
2
Introduction
  • 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
  1. Scanning
  2. Parsing
  3. Static semantic analysis

3
Last 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
4
Runtime 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.

5
Memory 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.
6
Code 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.
7
Data 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

8
Dynamic 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

9
Memory Organization
code area global/static area stack free
space heap
free space
10
Procedure 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
11
Registers
  • 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)

12
Calling Sequence
  • The calling sequence is the sequence of
    operations that must occur when a procedure or
    function is called.
  • Allocation of memory for the activation record
  • The computation and storing the arguments
  • Storing and setting registers

13
Calling Sequence Design
  1. Division of the calling sequence operations
    between the caller and the callee.
  2. Processor support versus generating explicit code
    for each step of calling sequence.
  • It is easier to generate calling sequence code
    at the point of call rather than inside the
    callee.
  • This causes the size of the size to grow.

14
Return Sequence
  • The return sequence is the sequence of operations
    needed when a procedure or function returns.
  • The placing of the return value where it can be
    accessed by the caller
  • Readjustment of registers
  • Releasing of activation record memory

15
Runtime 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.

16
Fully 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
17
Memory 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
18
Example 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
19
Example 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
20
Stack-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.

21
Global 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

22
Example 7.2
  • 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

Suppose the user inputs the values 15 and 10 to
this program
uv
v
u
5
10
15
0
5
10
End
0
5
23
Memory Organization
code area global/static area stack free
space heap
free space
24
Procedure 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
25
Example 7.2
  • 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

Suppose the user inputs the values 15 and 10 to
this program
uv
v
u
5
10
15
0
5
10
End
0
5
26
Example 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
27
Example 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
28
Memory Organization
code area global/static area stack free
space heap
free space
29
Procedure 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
30
Example 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
31
Example 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
sp
During the second call to g
32
Example 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
33
Example 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
34
Activation 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)
35
Access to Variables
  • In static environment, parameters and local
    variables can be accessed by fixed addresses.
  • In a stack-based environment, they must be found
    by offset from the current frame pointer.
  • In most languages, the offset for each local
    variable is still statically computable by
    compiler.
  • The declarations of a procedure are fixed at
    compile time and the memory size to be allocated
    for each declaration is fixed by its data type.

36
Example
  • Each activation record of g has the same form.

m
control link
return address
y
mOffset 4
fp
yOffset-6
We assumed that the stack grows from higher to
lower memory addresses, integers require 2 bytes
of storage and addresses require 4 bytes.
37
Example 7.5
x 1 x (from f) 0

m2 control link return address y 1
free space
Global static area
Activation record of main
Activation record of call to g
fp
sp
Before the third call to g
38
Example 7.5 (cont)
rest of stack
rest of stack
m 2
control link
return address
y 1
m 2
control link
return address
y 1
fp
fp
sp
free space
m 2
sp
free space
The value of parameter m is pushed onto the stack.
39
Example 7.5 (cont)
fp is pushed onto the stack.
rest of stack
rest of stack
m 2
control link
return address
y 1
m 2
control link
return address
y 1
fp
fp
m 2
sp
m 2
control link
free space
sp
free space
40
Example 7.5 (cont)
rest of stack
sp is copied into fp.
rest of stack
m 2
control link
return address
y 1
m 2
control link
return address
y 1
fp
m 2
control link
m 2
control link
fpsp
sp
free space
free space
41
Example 7.5 (cont)
rest of stack
rest of stack
The return address is pushed onto the stack.
m 2
control link
return address
y 1
m 2
control link
return address
y 1
m 2
control link
return address
m 2
control link
fp
fpsp
sp
free space
free space
42
Example 7.5 (cont)
rest of stack
rest of stack
The new local variable y is allocated and
initialized.
m 2
control link
return address
y 1
m 2
control link
return address
y 1
m 2
control link
return address
m 2
control link
return address
y 1
fp
fp
sp
free space
sp
free space
43
Homework
  • 7.1, 7.2
Write a Comment
User Comments (0)
About PowerShow.com