Runtime Environments - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Runtime Environments

Description:

Title: PowerPoint Presentation Last modified by: walkinnet Created Date: 1/1/1601 12:00:00 AM Document presentation format: Other titles – PowerPoint PPT presentation

Number of Views:94
Avg rating:3.0/5.0
Slides: 33
Provided by: educ5460
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
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.

13
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
14
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
15
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
16
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
17
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.

18
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

19
Example 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
20
Memory Organization
code area global/static area stack free
space heap
free space
21
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
22
Example 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
23
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
24
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
25
Memory Organization
code area global/static area stack free
space heap
free space
26
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
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
28
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
During the second call to g
sp
29
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
30
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
31
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)
32
Homework
  • 7.1, 7.2
Write a Comment
User Comments (0)
About PowerShow.com