0% found this document useful (0 votes)
149 views

Compilers and RTE

Presentation based on the study of compilers and their functioning for various programming languages.

Uploaded by

wooDefy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views

Compilers and RTE

Presentation based on the study of compilers and their functioning for various programming languages.

Uploaded by

wooDefy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Chapter 7

Run-Time Environments

Dewan Tanvir Ahmed


Assistant Professor, CSE
BUET

Introduction
In previous chapters we have studied the phases
of a compiler:

Scanning
Parsing
Static semantic analysis

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

Runtime Environment
1.

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.

2.

Three kinds of run time environment

3.

Fully static environment (FORTRAN77)


Stack-based environment (C, C++, PASCAL)
Fully dynamic environment (LISP)

Hybrids of these are also possible.

Source Language Issues


1.

A program is made up of procedures.

2.

What are the differences between the source text of a procedure


and its activation at run time?

3.

A procedure definition is a declaration that associates an identifier


with a statement.

Identifier is a procedure name

The statement is the procedure body

4.

Procedures that return values are called function in many languages.

5.

Formal and actual parameters


int max(int x, int y) { ..}
------max(m,n)

Activation Trees
1.

Assumptions about flow of control among procedures during execution of a


program:

Control flows sequentially


Each execution of a procedure starts at the beginning of the procedure body and
eventually returns to the point immediately following the place where the procedure was
called.

2.

Each execution of a procedure body is referred as an activation of the


procedure.

3.

Lifetime of an activation of a procedure P is the sequence of steps between the


first and last steps in the execution of the procedure body, including time spent
executing procedures called by P, the procedures called by them and so on.

4.

If a and b are procedure activations, then there life times are either nonoverlapping or are nested.

5.

A procedure is nested if a new activation can begin before an earlier activation of


the same procedure has ended.

Activation Trees (cont.)


1.
2.

We can use activation tree to depict the way control enters and leaves
activations.
In an activation tree

Each node represents an activation of a procedure


The root represents the activation of the main program
The node for a is the parent of the node for b iff control flows from the
activation a to b, and
The node for a is left of the the node for b iff the lifetime of a occurs
before the lifetime of b.

main()

main()

gcd(15,10)

g(2)

gcd(10,5)

f(1)

gcd(5,0)

g(1)

g(1)

Control Stacks
1.

The flow of control in a program corresponds to a depth-firsttraversal of the activation tree that starts at the root, visits a
node before its children, and recursively visits children at each
node in a left-to-right order.

2.

We can use a stack, called control stack to keep track of live


procedure activations.

3.

The idea is to push the node for an activation onto the control
stack as the activation begins and pop the node when the
activation ends.
Thus the contents of the stack are related to paths to the root
of the activation tree.

4.
5.

n is at the top of the control stack, the stack contains


the nodes along the path from n to the root.
When node

The Scope of a Declaration


1.
2.

A declaration is a syntactic construct that associates the information


with a name.
Declaration may be

Explicit

Implicit

3.
4.
5.

var i : integer
Any variable name starting with I is assumed to denote an integer (Fortran)

The scope rules determine which declaration of a name applies when


the name appears in the text of a program.
The portion of the program to which a declaration applies is called
scope of that declaration.
An occurrence of a name in a procedure is said to be local to the
procedure if it is in the scope of a declaration within the procedure,
otherwise, the occurrence is said to be non-local.

Binding of Names
1.
2.
3.
4.

Even if each name is declared once in a program, the same name may
denote different data objects at run time.
Data object corresponds to a storage location that can hold values.
The term environment refers to a function that maps a name to a storage
location.
The term state refers to a function that maps a storage location to the
value held there.

environment

name
5.

state

storage

value

Environments and states are different; an assignment changes the state


but not the environment.

Binding of Names (cont.)


1.

When an environment associates storage location

with a name

x,

x is bound to s; the association itself is referred to


as a binding of x.
we say that

2.

A binding is the dynamic counter part of a declaration.

Static Notation

Dynamic Counterpart

Definition of a procedure

Activations of the procedure

Declaration of a name

Binding of the name

Scope of a declaration

Lifetime of a binding

Storage Organization
1.

The compiler obtains a block of storage from the operating system for the
compiled program to run in.

2.

Run time storage might be subdivided to hold:

The generated target code

Data objects, and

A counterpart of the control stack to keep track of procedure activations.

3.

In most compiled languages, it is not possible to make changes to the code area
during execution.

4.

The code area is fixed prior to the execution, and all code addresses are
computable at compile time.

5.

The size of the some data objects may also be known at compile time, and
these too can be placed in a statically determined area.

Memory Organization (cont.)


code area
global/static area
stack
free
free space
space
heap

Storage Organization Fortran


Memory
Register Area
Code Area
RAM
Data Area

Code Memory
Entry point for procedure 1
Entry point for procedure 2

Code for
Procedure 1
Code for
Procedure 2
.
.

Entry point for procedure n

Code for
Procedure n

Entry point of each procedure and function is known at compile time.

Data Area
1.

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

Dynamic Memory
1. The memory area for the allocation of dynamic data can be
organized in many different ways.
2. A typical organization divides the dynamic memory into
stack area (LIFO protocol)
heap area

Why Stack and Heap?


1.

C/Pascal uses extensions of the control stack to manage activations of


procedures.

2.

When a call occurs, execution of an activation is interrupted and information


about the status of the machine, such as the value of the program counter
and machine registers, is saved on the stack.

3.

When control returns from the call, this activation can be restarted after
restoring the values of relevant registers and setting the program counter to
the point immediately after the call.

4.

Heap holds all other information like the data under program control i.e.
dynamic memory allocation.
By convention, stacks grow down. Heaps grow up.

5.

Procedure Activation Record


1.
2.

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.
1. The picture illustrates the general
organization of an activation record.
2. Details depend on the architecture of
target machine and properties of the
language.
3. When activation records are kept on
arguments
stack, they are called stack frames.

bookkeeping information
(return address)
local data
local temporaries

Procedure Activation Record (cont.)


Returned value

Return a value to the calling


procedure

Actual parameter

To supply parameters to the called


procedure

Optional control link

Points to the activation record of


the caller

Optional access link


Saved machine status
Local data
temporaries

To refer to nonlocal data held in the


other activation records
Info about the state of the machine
just before the proc is called
Local data
temporaries

Article 7.3
Self study

Best of luck
in the Exam

You might also like