0% found this document useful (0 votes)
43 views38 pages

CH4 1

The document discusses runtime environments and storage organization. It defines a runtime environment as the state of the target machine that provides services to running processes. It covers storage allocation strategies used by compilers to map names to storage locations. Activation records are used to manage procedure calls and returns, storing information like parameters and return values. Control stacks track active procedures by pushing and popping names.

Uploaded by

sam negro
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)
43 views38 pages

CH4 1

The document discusses runtime environments and storage organization. It defines a runtime environment as the state of the target machine that provides services to running processes. It covers storage allocation strategies used by compilers to map names to storage locations. Activation records are used to manage procedure calls and returns, storing information like parameters and return values. Control stacks track active procedures by pushing and popping names.

Uploaded by

sam negro
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/ 38

Run-time Environment

1
Contents To Be Covered

Introduction

Storage Organization

Storage allocation Strategies.

2
Introduction
 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.

 Execution of a program is initially under the control of the operating system (OS)

 Compilers create and manage a runtime environment in which the target programs are
executed.

 To successfully implement the various issues of the source languages, like names, scope,
bindings, data types, procedures, parameters, etc.. And also the compiler must be cooperate
with OS.

 The compiler must also work together with the other system software to support the issues on
the target machine.

3
Introduction
 When a program is invoked: start to execute.
 The OS allocates space for the program

 The code is loaded into part of this space

 The OS jumps to the entry point of the program (i.e., to the beginning of

the “main” function)

4
Introduction
 Issues handled by runtime environments are:
 Allocation of storage for objects.
 Access to variable and data.
 Linkages between procedures
 Parameter passing mechanisms
 Interfaces to the OS, I/O and other programs.

5
Introduction
 At run time, we need a system to map NAMES (in the source program) to
STORAGE on the machine.

 The places of the data objects that can be determined at compile time will be
allocated statically.

 But the places for the some of data objects will be allocated at run-time is dynamic
allocation.

 The Allocation and deallocation of data objects to memory is handled by a RUN-


TIME SUPPORT typically linked and loaded along with the compiled target code.

 One of the primary responsibilities of the run-time system is to manage


ACTIVATIONS of procedures.

6
Procedures
 A procedure definition is a declaration that associates an identifier with a
statement.

 A program is a sequence of instructions combined into a number of


procedures.

 The identifier is the procedure name, and the statement is the procedure
body. For example
 Example, the following is the definition of procedure named readarray :
procedure readarray; var i : integer;
begin
for i : = 1 to 9 do read(a[i]) end;

7
Procedures
 When a procedure name appears within an executable statement,
the procedure is said to be called at that point.

 A procedure has a start and an end delimiter and everything


inside it is called the body of the procedure.

 The procedure identifier and the sequence of finite instructions


inside it make up the body of the procedure.

 The execution of a procedure is called its activation.

8
Activation record
 The state of activation is managed by flow control.

 And the flow control information stored in


Activation record.

 An activation record contains all the


necessary information required to call a
procedure.

9
Activation record
Return Value Stores return values.(result of called procedure)

Actual Stores actual parameters, i.e., parameters which are used


Parameters to send input to the called function or procedure.

Stores the information of data which is outside the local


Access Link
scope.

Stores the address of activation record of the caller


Control Link
procedure.

Stores machine status such as Registers, Program Counter


Machine Status
etc., before the procedure is called.

Local Data Stores local data of the called procedure.

Stores temporary and intermediate values of an


Temporaries
expression.
10
Procedures
 Whenever a procedure is executed, its activation record is stored on
the stack, also known as control stack.

 When a procedure calls another procedure, the execution of the caller


is suspended until the called procedure finishes execution.

 At this time, the activation record of the called procedure is stored on


the stack.

11
Activation
 Every execution of a procedure is called an ACTIVATION.

 The LIFETIME of an activation of procedure P is the sequence


of steps between the first and last steps of P’s body, including
any procedures called while P is running.

 Normally, when control flows from one activation to another, it


must (eventually) return to the same activation.

 When activations are nested, we can represent control flow with


ACTIVATION TREES.

12
Activation tree
 We assume that the program control flows in a sequential manner and
when a procedure is called, its control is transferred to the called
procedure.

 When a called procedure is executed, it returns the control back to the


caller.

 This type of control flow makes it easier to represent a series of


activations in the form of a tree, known as the activation tree.

13
Activation tree
 An activation tree is used 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 if and only if control flows
from activation a to b.
 The node for a is to the left of the node for b if and only if the lifetime of a
occurs before the lifetime of b.

14
Activation tree
...

printf(“Enter Your Name: “);

scanf(“%s”, username);

show_data(username);

printf(“Press any key to continue…”);

...

int show_data(char *user)

printf(“Your name is %s”, username);

return 0;

...

15
Control stacks
 Control stack or runtime stack is used to keep track of the
live procedure activations. i.e. the procedures whose execution
have not been completed.

 A procedure name is pushed on to the stack when it is called


(activation begins) and it is popped when it returns (activation
ends).

 At any point in time, the control stack represents a path from the
root of the activation tree to one of the nodes.
16
Control stacks
 This partial activation tree corresponds to control stack
(growing downward). (read array, partitioning and quick
Print f
sort procedure)
printf(“Enter Your Name: “); Print f
scanf(“%s”, username);
showdata
show_data(username);

printf(“Press any key to continue…”); scanf


. . .
printf
int show_data(char *user)

{
Main function
printf(“Your name is %s”, username);

return 0;

17
Scope of declaration & binding of names
 Syntactic construct that associates information with a name.
eg. Var i:integer; in pascal int I; in c++

 Binding of names:
 Environment: function that maps a name to a storage location
 State: function that maps a storage location to the value held there.

environment State

Name storage value

 When an environment associates storage location s with a name x →x is based on S.

 Eg.  = initial =0  = 3.14 assignment →  = 3.14

18
Storage Organization
 The executing target program runs in its own logical address space in which each
program value has a location.

 The management and organization of this logical address space is shared between
the complier, operating system and target machine.

 The operating system maps the logical address into physical addresses, which are
usually spread throughout memory.

 During the execution of a program, the same name in the source can denote
different data objects in the computer.

 The allocation and deallocation of data objects is managed by the run-time


support package.

19
Storage Organization
Terminologies
 Name storage space: The mapping of a name to a storage space is
Called environment.

 Storage space value:


 State:- The current value of a storage space.
 Binding:- The association of a name to a storage location.
 Activation:- Each execution of a procedure.
 If it is a recursive procedure, then several of its activations may exist at
the same time.

 Life time: The time between the first and last steps in a procedure. A
recursive procedure needs not to call itself directly.

20
General Run Time Storage
layout

21
General run time storage
layout

22
STORAGE ALLOCATION
➢Runtime environment manages runtime memory
requirements for the following entities.
➢ Code : It is known as the text part of a program that does not
change at runtime. Its memory requirements are known at the
compile time.
➢ Procedures : Their text part is static but they are called in a
random manner. That is why, stack storage is used to manage
procedure calls and activations.
➢ Variables : Variables are known at the runtime only, unless they
are global or constant.
23
STORAGE ALLOCATION
STRATEGIES
➢The different storage allocation strategies are :
➢ Static allocation - lays out storage for all data objects at
compile time
➢ Stack allocation - manages the run-time storage as a
stack.
➢ Heap allocation - allocates and deallocates storage as
needed at run time from a data area known as heap.

24
Static Allocation
 In static allocation, names are bound to storage locations.

 If memory is created at compile time then the memory will be created in static area
and only once.

 Static allocation supports the dynamic data structure that means memory is created
only at compile time and deallocated after program completion.

 In this the compiler can determine the amount of storage required by each data
object and therefore it becomes easy for a compiler to find the address of these data
in the activation record.

 Since the bindings do not change at run-time, every time a process is activated, its
names are bound to the same storage locations.

25
Static Allocation
 FORTRAN uses this kind of storage allocation strategies.

 Advantages
 It is easy to implement.
 It allows type checking during compilation.
 It eliminates the feasibility of running out of memory.

 Disadvantages
 It is incompatible with recursive subprograms.
 It is not possible to use variables whose size has to be determined at run time.

26
Stack Allocation
 The storage is organized as stack .This stack is also called
control stack.

 An activation record is pushed into the stack when activation


begins and it is popped when the activation end.

 The locals are stored in the each activation record. Hence locals
are bound to corresponding activation record on each fresh
activation.

 The value of locals is deleted when the activation ends.

27
Stack Allocation
 The data structures can be created dynamically for stack
allocation.

 It works on the basis of last-in-first-out (LIFO) and this


allocation supports the recursion process.

 Each time a procedure is called, space for its local variables is


pushed onto a stack, and when the procedure terminates, that
space is popped off the stack.

 Hence this type of allocation is slower than static allocation.

28
Stack Allocation
 On each execution of a procedure, An Activation Record is generated,
which contains information like Local data, actual parameter, return value,
return address of a procedure. The Activation Record for that procedure is
saved onto the stack.

 If procedure A calls B, and then B calls C, then stack allocation will be

 .

29
Stack Allocation
 Advantages
 It supports recursion.
 It creates a data structure for the data item dynamically.

 Disadvantages
 Memory Addressing can be done using pointers & index
Registers.

 C/C++/Java use either stack or heap allocation.

30
Heap Storage Allocation
 Heap allocation is the most flexible allocation scheme.

 Allocation and deallocation of memory can be done at any time


and at any place depending upon the user's requirement.

 Heap allocation is used to allocate memory to the variables


dynamically and when the variables are no more used then
claim it back.

 This deallocated space can be further reused by heap manager.

31
Heap Storage Allocation
 If the values of non local variables must be retained even after
the activation record then such a retaining is not possible by
stack allocation.

 This limitation of stack allocation is because of its Last in First


Out nature.

 For retaining of such local variables heap allocation strategy is


used.

 Heap storage allocation supports the recursion process.

32
Heap Storage Allocation
 Advantages
 A large block of storage can be partitioned into smaller
blocks at run time.

 Disadvantages
 It creates the problem of fragmentation.

33
Parameter Passing
 The Parameter passing is a way of communication among the
variables.

 The called procedure gets the value of the variable from the calling
procedure through a default mechanism

 Some basic terminologies


 Formal Parameter: The variables declared in the called function’s
definition and take the information passed by the calling process.
 Actual Parameters: The variables that are mentioned in the
function are called arguments, and whose address or values are
passed to the called process are known as actual parameters.
34
Parameter Passing
 r-value:
 The value of an expression is called its r-value. The value contained
in a single variable also becomes an r-value if it appears on the
right-hand side of the assignment operator. r-values can always be
assigned to some other variable.

 l-value:
 The location of memory (address) where an expression is stored is
known as the l-value of that expression. It always appears at the left
hand side of an assignment operator.

35
Parameter Passing
 For example:
day = 1;
week = day * 7;
month = 1;
year = month * 12;
 From this example, we understand that constant values like 1, 7, 12, and
variables like day, week, month and year, all have r-values. Only variables have
l-values as they also represent the memory location assigned to them.

 For example:
7 = x + y;
is an l-value error, as the constant 7 does not represent any memory location.

36
Parameter Passing
 Pass by Value:
 When the calling process passes the r-value of the actual parameter, and then it is transferred to the
activation record of the called process is known as the pass by value mechanism.
 The formal parameter is responsible for holding the values passed by the calling procedure, and if
these values hold by formal parameter changes, then it should not affect the actual parameter.

 Pass by Reference
 When the l-value of the actual parameter is transferred to the activation record of the
called procedure and now the called procedure contains the address of the actual
parameter, and also the formal parameters refer to the same memory is known as pass by
reference.
 Thus, If the value held by the formal parameter changes, there should be no change in the
actual parameter as they point to the same value.

37
Parameter Passing
 Pass by Copy-restore
 It is very much similar to pass by reference; the difference is that when the
procedure call ends, the changes are visible in actual parameters.
 When there is the function call, the value of actual parameters is transferred to the
activation record of the called procedure.

 Pass by Name:
 When the name of the procedure is called and is replaced by its actual body, it is
called pass by name.
 It substitutes the argument during procedure call for corresponding parameters in the
body, and thus, it can work on actual parameters, much similar to pass by reference.

38

You might also like