0% found this document useful (0 votes)
77 views30 pages

Unit - 4 Pushdown Automata: Run Time Environments

The document discusses run time environments. The run time environment establishes relationships between names and data objects, manages allocation and de-allocation of objects, and provides services like storage layout, variable access, procedure linkage, and interface to operating systems. It deals with issues in procedure calls, activation trees showing procedure flow, scope of declarations, binding of names to storage locations, and subdivision of runtime memory.

Uploaded by

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

Unit - 4 Pushdown Automata: Run Time Environments

The document discusses run time environments. The run time environment establishes relationships between names and data objects, manages allocation and de-allocation of objects, and provides services like storage layout, variable access, procedure linkage, and interface to operating systems. It deals with issues in procedure calls, activation trees showing procedure flow, scope of declarations, binding of names to storage locations, and subdivision of runtime memory.

Uploaded by

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

Unit – 4 Chapter 7

Pushdown
Run Time Environments

Automata

Chapter – 7 : Run Time Environment 1 Bahir Dar Institute of Technology


Preface
▪ Run Time Environment establishes relationships between names
and data objects.
▪ The allocation and de-allocation of data objects are managed by the
Run Time Environment
▪ Each execution of a procedure is referred to as an activation of the
procedure.
▪ If the procedure is recursive, several of its activations may alive at
the same time.
▪ Each call of a procedure leads to an activation that may manipulate
data objects allocated for its use.
▪ The representation of a data object at run time is determined by its
type.
▪ Often, elementary data types, such as characters, integers, and reals
can be represented by equivalent data objects in the target machine.
▪ However, aggregates, such as arrays, strings, and structures, are
usually represented by collections of primitive objects.
Chapter – 7 : Run Time Environment 2 Bahir Dar Institute of Technology
Introduction
 Compiler must cooperates with OS and other system software
to support implementation of different abstractions (names,
scopes, bindings, data types, operators, procedures,
parameters, flow-of-control) on the target machine
 Compiler does this by Run-Time Environment in which it
assumes its target programs are being executed
 Runtime environment is a state of the target machine,
which may include software libraries, environment
variables, etc., to provide services to the processes
running in the system.
 Run-Time Environment deals with
– Layout and allocation of storage
– Access to variable and data
– Linkage between procedures
– Parameter passing
– Interface to OS, I/O devices etc.
Chapter – 7 : Run Time Environment 3 Bahir Dar Institute of Technology
Source Language Issues: Procedure call
▪ A procedure definition is a declaration that associates an identifier
with a statement.
▪ The identifier is the procedure name and the statement is the
procedure body.
▪ A procedure returns value for the called function.
▪ A complete program will also be treated as a procedure.
▪ When a procedure name appears within an executable statement,
we say that the procedure is called at that point.
▪ The basic idea is that a procedure call executes the procedure body.
▪ Some of the identifiers appearing in a procedure definition are
special, and are called formal parameters of the procedure.
▪ Actual parameters may be passed to a called procedure.
▪ Procedures can contains local and global variables.

Chapter – 7 : Run Time Environment 4 Bahir Dar Institute of Technology


Source Language Issues: Activation tree (call tree)
 The process of executing a procedure is called activation.
 An activation tree is used to show 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 A is a parent of the node B iff the control flows
from A to B.
– The node A is left to the node B iff the lifetime of A occurs
before the lifetime of B.
 Each execution of a procedure body is referred to as an activation of the
procedure.
 The 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, procedures called
by them. and so on.
 In general, the term ‘lifetime’ refers to a consecutive sequence of steps
during the execution of a program.
Chapter – 7 : Run Time Environment 5 Bahir Dar Institute of Technology
Source Language Issues: Activation tree (call tree)
 More over,
 During the execution of a program among procedures
(during flow of control):
 Control flows sequentially; that is, the execution of a
program consists of a sequence of steps, with control
being at some specific point in the program at each
step.
 Each execution of a procedure starts at the beginning of
the procedure body and eventually returns control to the
point immediately following the place where the
procedure was called.
 This means the flow of control between procedures can
be depicted using trees.

Chapter – 7 : Run Time Environment 6 Bahir Dar Institute of Technology


Source Language Issues: Activation tree
▪ Example:
Main(){
. printf(“Enter Your Name:“); Main()
scanf(“%s”, username);
show_data(username);
printf(“Press any key…”);
. . .
}
int show_data(char *user)
{
printf(“Your name is %s”,
username);
return 0;
}

Chapter – 7 : Run Time Environment 7 Bahir Dar Institute of Technology


Source Language Issues: Control stack

 The flow of the control in a program corresponds to a


depth-first traversal of the activation tree that:
– starts at the root,
– visits a node before its children, and
– recursively visits children at each node from a left-to-right order.
 A stack (called control stack) can be used to keep track of
live procedure activations.
– An activation record is pushed onto the control stack as the
activation starts.
– That activation record is popped when that activation ends.
– Dynamic – grows and shrinks
 When node n is at the top of the control stack, the stack
contains the nodes along the path from n to the root.

Chapter – 7 : Run Time Environment 8 Bahir Dar Institute of Technology


Source Language Issues: Scope of declaration
 A declaration in a language is a syntactic construct that
associates information with a name.
 The same variable name can be used in the different parts of
the program.
 The scope rules of the language determine which declaration of
a name applies when the name appears in the program.
 An occurrence of a variable (a name) is:
– local: If that occurrence is in the same procedure in
which that name is declared.
– non-local: Otherwise (i.e. it is declared outside of that procedure)
var b:real;
procedure p;
procedure q;
var a: integer;
begin a := 1; b := 2; end; a. is local
begin ... end; b. is non-local
Chapter – 7 : Run Time Environment 9 Bahir Dar Institute of Technology
Source Language Issues: Binding of names
When an environment maps name x to storage location s, we
say “x is bound to s”. The association is a binding.
▪ Even if each name is declared once in a program, the same name may denote
different data objects at run time.
▪ The informal term "data object“ corresponds to a storage location that can hold values.
 In programming language semantics, the term environment refers to a
function that maps a name to a storage location, and the term state refers to a
function that maps a storage location to the value held here as in Fig. below

Fig. Two-stage mapping


from names to values

Environments and states are different; an assignment changes


the state, but not the environment.
For example, suppose that storage address 100, associated with
variable pi, holds 0.
After the assignment pi := 3. 14, the same storage address is
associated with pi, but the value held there is 3.14.
Chapter – 7 : Run Time Environment 10 Bahir Dar Institute of Technology
Storage Organization: Subdivision of Runtime Memory
• Compiler deals with logical address space. i.e.
• The compiler demands for a block of memory to operating system; and the OS
maps the logical addresses to physical addresses.
• The compiler utilizes this block of memory executing the compiled program. This
block of memory is called run time storage/memory.
• The run-time storage might be subdivided to hold: the generated target code, data
objects, & a counterpart of the control stack to keep track of procedure activations.

Code Memory locations for code are determined at compile time.


Usually placed in the low end of memory
Static data Size of generated code are known at compile time – can be
Stack placed another statically determined area
Stack
Grows towards lower address
Free Memory Stores activation records
Dynamic space areas – size changes during program execution.
Heap
Heap Grows towards higher address
Stores data allocated under program control

• Typical subdivision of run-time memory


Chapter – 7 : Run Time Environment 11 Bahir Dar Institute of Technology
Storage Organization: Subdivision of Runtime Memory
▪ The size of generated code is fixed at compile time. Hence the
target code occupies the determined area of the memory.
▪ Similarly the size of the data objects is known at the compiled
time and hence data objects also can be placed at the statically
determined area of the memory.
▪ Stack is used to manage the active procedure.
▪ Managing of active procedures means when a call occurs then
execution of activation is interrupted and information about
status of the stack is saved on the stack.
▪ Heap area is the area of run time storage in which the other
information is stored. (i.e. the heap stores data allocated under
program control)

Chapter – 7 : Run Time Environment 12 Bahir Dar Institute of Technology


Storage Organization: Activation Record
▪ An activation record contains all the necessary information
required to call a procedure.
▪ Temporary values: stores the values that arise in the
evaluation of an expression. Temporary value
▪ Local variables: hold the data that is local to the Local variables
execution of the procedure. Machine status
▪ Machine status: holds the information about status of Access link
machine just before the function call.
Control link
▪ Access link (optional): refers to non-local data held in
other activation records. Actual
▪ Control link (optional): points to activation record of parameters
caller. Return value
▪ Actual parameters: This field holds the information
about the actual parameters.
▪ Return value: used by the called procedure to return a
value to calling procedure.

▪ NB: The execution of a procedure is called its activation.

Chapter – 7 : Run Time Environment 13 Bahir Dar Institute of Technology


Storage allocation strategies
The different storage allocation strategies are;
▪ Static allocation
▪ Stack allocation
▪ Heap allocation

▪ Static allocation
▪ lays out storage for all data objects at compile time.
▪ In static allocation, names are bound to storage as the program is
compiled, so there is no need for a run-time support package.
▪ Since the bindings do not change at run-time, every time a
procedure is activated, its names are bounded to the same storage
location.

Chapter – 7 : Run Time Environment 14 Bahir Dar Institute of Technology


Storage Allocation Strategies: Static allocation
• When A calls B:
– in A: evaluate actual parameters and place into B’s data area, place
RA in B’s data area, save any registers or status data needed,
update the program counter (PC) to B’s code
• When the call returns
– in B: move return value to known place in data area, update PC to
value in RA
– in A: get return value, restore any saved registers or status data

Code area Data area for procedure A Data area for procedure B

Code return addr return addr


generated
for A Local data, Local data,
parameters, parameters,
return return
Code value,
generated value,
registers registers
for B for B
for A

Chapter – 7 : Run Time Environment 15 Bahir Dar Institute of Technology


Storage Allocation Strategies: Stack allocation
▪ Manages the run-time storage as a stack.
▪ All compilers for languages that use procedures, functions or methods
as units of user defined actions manage at least part of their run-time
memory as a stack.
▪ Each time a procedure is called, space for its local variables is pushed
onto a stack, and when the procedure terminates, the space is popped
off the stack.
▪ Calling Sequences
▪ Procedures calls are implemented by generating what are known as
calling sequences in the target code.
▪ (i.e. Is sequence of operations performed during procedure calls)
▪ A Return sequence restores the state of machine so the calling
procedure can continue its execution.
▪ The code in calling sequence is often divided between the calling
procedure (caller) and procedure is calls (callee).
Chapter – 7 : Run Time Environment 16 Bahir Dar Institute of Technology
Stack allocation: Calling Sequences
▪ Value communicated between caller and callee are generally placed at the
beginning of the callee’s activation record, so they are as close as possible to
the caller’s activation record.
▪ Fixed length items are generally placed in the middle. Such items typically
include the control link, the access link, and the machine status field.
▪ Items whose size may not be known early enough are placed at the end of the
activation record. We must locate the top of the stack pointer judiciously.

…. Caller’s
Control link activation
Parameter and return value record
Temporaries and local data
Caller’s
Parameter and return value responsibility
Control link
Links and saved status Callee’s
top_sp activation
Temporaries and local data Callee’s record
responsibility
Chapter – 7 : Run Time Environment 17 Bahir Dar Institute of Technology
Stack allocation: Calling Sequences
▪ The calling sequence and its division between caller and callee are as follows:
1. The caller evaluates the actual parameters.
2. The caller stores a return address and the old value of top_sp into the callee’s
activation record. The caller then increments the top_sp to the respective
positions.
3. The callee saves the register values and other status information.
4. The callee initializes its local data and begins execution.
…. Caller’s
Control link activation
Parameter and return value record
Temporaries and local data
Caller’s
Parameter and return value responsibility
Control link
Links and saved status Callee’s
top_sp activation
Temporaries and local data Callee’s record
responsibility
Chapter – 7 : Run Time Environment 18 Bahir Dar Institute of Technology
Stack allocation: Calling Sequences
▪ A suitable, corresponding return sequence is:
1. The callee places the return value next to the parameters.
2. Using the information in the machine status field, the callee restores top_sp
and other registers, and then branches to the return address that the caller
placed in the status field.
3. Although top_sp has been decremented, the caller knows where the return
value is, relative to the current value of top_sp ; the caller therefore may use
that value.
…. Caller’s
Control link activation
Parameter and return value record
Temporaries and local data
Caller’s
Parameter and return value responsibility
Control link
Links and saved status Callee’s
top_sp activation
Temporaries and local data Callee’s record
responsibility
Chapter – 7 : Run Time Environment 19 Bahir Dar Institute of Technology
Stack allocation: Variable length data on stack
▪ The run time memory management system must deal frequently with the
allocation of objects, the sizes of which are not known at the compile time, but
which are local to a procedure and thus may be allocated on the stack.
▪ The same scheme works for objects of any type if they are local to the
procedure called have a size that depends on the parameter of the call.

Control link
Pointer to A
Pointer to B
Pointer to C

Array A
Array B
Array C
top_sp
Control link
sp
Chapter – 7 : Run Time Environment 20 Bahir Dar Institute of Technology
Stack allocation: Dangling Reference

▪ Whenever storage can be allocated, the problem of dangling


reference arises.
▪ The dangling reference occurs when there is a reference of storage
that has been deallocated. (i.e. dangling references occur if
deallocation occurs before the last reference)
▪ It is a logical error to use dangling reference, since, the value of
de-allocated storage is undefined according to the semantics of
most languages.
▪ (The process of accessing locations accessible from the
environment but not present in the domain of the store)

Referencing deleted data is a dangling-pointer-dereference error.


Pointers to storage that has been deallocated are known as dangling pointers.

Chapter – 7 : Run Time Environment 21 Bahir Dar Institute of Technology


Storage Allocation Strategies: Heap Allocation
▪ Allocates and de-allocates storage as needed at run time from a data
area known as heap. It means
▪ Variables local to a procedure are allocated and de-allocated only at
runtime.
▪ Heap allocation is used to dynamically allocate memory to the variables
and claim it back when the variables are no more required.
▪ Heap is an alternate for stack.
▪ Because, the deallocation of activation records need not occur in a last-
in first-out fashion, so storage cannot be organized as a stack.
▪ Except statically allocated memory area, both stack and heap memory
can grow and shrink dynamically and unexpectedly.
▪ Therefore, they cannot be provided with a fixed amount of memory in
the system.
▪ Heap allocation parcels out pieces of contiguous storage, as needed for
activation records or other objects. Pieces may be deallocated in any
order. So over time the heap will consist of alternate areas that are free
and in use.
Chapter – 7 : Run Time Environment 22 Bahir Dar Institute of Technology
Symbol Table
▪ A compiler uses a symbol table to keep track of scope and binding
information about names.
▪ Symbol table is a data structure used by compiler to keep track of
semantics of a variable.
▪ A symbol Table is also known as Book Keeping
▪ Symbol table is built in lexical and syntax analysis phases and is used
by all phases of the compiler.
▪ A symbol-table mechanism must allow us to add new entries and
find existing entries efficiently.
▪ The items to be stored into symbol table are:
1. Variable names
2. Constants
3. Procedure names
4. Function names
5. Literal constants and strings
6. Compiler generated temporaries
7. Labels in source language
Chapter – 7 : Run Time Environment 23 Bahir Dar Institute of Technology
Symbol Table entries
▪ Each entry in the symbol table is for the declaration of a name.
▪ The format of entries does not have to be uniform; because the
information saved about a name depends on the usage of the
name.
▪ Information is entered into the symbol table at various times.
▪ Keywords are entered into the table initially before lexical analysis
begins, if at all.
▪ The lexical analyzer looks up sequences of letters and digits in the
symbol table to determine if a reserved keyword or a name has
been collected.
▪ The symbol-table entry itself can be set up when the role of a
name becomes clear, with the attribute values being filled in as
the information becomes available.
▪ In some cases, the entry can be initiated from the lexical analyzer
as soon as a name is seen in the input.

Chapter – 7 : Run Time Environment 24 Bahir Dar Institute of Technology


Data structures for a symbol table
List Data structure
▪ The name can be stored with the help of starting index and length
of each name.
▪ Linear list is a simplest kind of mechanism to implement the
symbol table.
▪ In this method an array is used to store names and associated
information.
▪ New names can be added in the order as they arrive.
▪ The list data structure using array is given below:
Name 1 Info 1
Name 2 Info 2
Name 3 Info 3

Name n Info n

Chapter – 7 : Run Time Environment 25 Bahir Dar Institute of Technology


Data structures for a symbol table
Self organizing list
▪ This symbol table implementation is using linked list.
▪ A link field is added to each record.
▪ We search the records in the order pointed by the link of link field.
▪ The pointer “First” is maintained to point to first record of the
symbol table.
First Name 1 Info 1
Name 2 Info 2
Name 3 Info 3

Name n Info n

Chapter – 7 : Run Time Environment 26 Bahir Dar Institute of Technology


Data structures for a symbol table
Binary tree
▪ When the organization of symbol table is by means of binary
tree, the node structure will as follows:
▪ The left child field stores the address of previous symbol.
▪ Right child field stores the address of next symbol.
▪ The symbol field is used to store the name of the symbols.
▪ Information field is used to give information about the
symbol.
Left child Symbols Information Right child

Chapter – 7 : Run Time Environment 27 Bahir Dar Institute of Technology


Data structures for a symbol table
Hash table
▪ In hashing scheme two tables are maintained- a hash table and
symbol table.
▪ Hashing schemes provide better performance for somewhat
greater programming effort and space overhead.
▪ The hash table consists of k entries from 0,1 to k-1.
▪ These entries are basically pointers to symbol table pointing to the
names of symbol table.
▪ To determine whether the 'Name' is in symbol table, we use a
hash function 'h' such that h(name) will result any integer
between 0 to k-1.
▪ We can search any name by position=h(name).
▪ Using this position we can obtain the exact locations of name in
symbol table.
▪ Advantage of hashing is possibility of quick search and the
disadvantage is that hashing is complicated to implement.
Chapter – 7 : Run Time Environment 28 Bahir Dar Institute of Technology
Data structures for a symbol table
Hash table example
Array of list headers.
indexed by hash value

Fig. A hash table of size 211

Chapter – 7 : Run Time Environment 29 Bahir Dar Institute of Technology


Chapter – 7 : Run Time Environment 30 Bahir Dar Institute of Technology

You might also like