You are on page 1of 11

Module 4

1. Subprogram Control
Subprogram Control deals with:
1. Interaction among subprograms
2. How subprograms pass data among themselves
Simple subprogram call return
Copy rule view of subprograms:
The effect of a call statement is the same as if the subprogram were copied and inserted into the
main program.
Assumptions
Subprograms cannot be recursive
Explicit call statements are required
Subprograms must execute completely at each call
Immediate transfer of control at point of call
Single execution sequence



Simple flow of execution
Simple call-return subprograms
Execution of subprograms consists of
Subprogram definition.
Subprogram activation.
Call
Return


Subprogram definition
The definition is translated into a template, used to create an activation each time a
subprogram is called.
Subprogram activation
Consists of:
1. a code segment (the invariant part) -
executable code and constants,
2. an activation record (the dynamic part) -
local data, parameters.
Activation Record is created anew each time the subprogram is called, and destroyed when the
subprogram returns.
System-defined pointers
Current-instruction pointer CIP
address of the next statement to be executed
Current-environment pointer CEP
pointer to the activation record.
On call instruction
An activation record is created
Current CIP and CEP are saved in the created activation record as return point
CEP is assigned the address of the activation record.
CIP gets the address of the first instruction in the code segment
The execution continues from the address in CIP
On return
The old values of CIP and CEP are retrieved.
The execution continues from the address in CIP
Restrictions of the model: at most one activation of any subprogram
The simplest implementation
Allocate storage for a single activation record statically as an extension of the code segment.
Used in FORTRAN and COBOL.
The activation record is not destroyed - only reinitialized for each subprogram execution.


Hardware support - CIP is the program counter, CEP is not used, simple jump executed on
return.
Stack-based implementation
The simplest run-time storage management technique
call statements : push CIP and CEP
return statements : pop CIP and CEP off of the stack.
Used in most C implementations
LISP: uses the stack as an environment.
Recursive Subprograms
Specification
Syntactically - no difference
Semantically - multiple activations of the same subprogram exist simultaneously at
some point in the execution.
Eg. the first recursive call creates a second activation within the lifetime of the first activation.
I mplementation
Stack-based -
CIP and CEP are stored in stack, forming a dynamic chain of links.
A new activation record is created for each call and destroyed on return.
The lifetimes of the activation records cannot overlap - they are nested.
2. Attributes of Data Control
Data Control deals with accessibility of data at different points during program execution
Eg. X:=Y+2*Z
Y may be a local or nonlocal variable
We must know scope rules for declarations:
if Y is a formal parameter, parameter transmission
if Y is a parameterless subprogram,mechanisms for returning results
Names and Referencing Environments
Two ways that a data object can be made available as an operand for an operation:
Direct transmission.
Referencing through a named data object.


Direct transmission.
Is used for data control within expressions.
Data object is allocated storage temporarily during its lifetime, without any name.
Like 2*Z in X:=Y+2*Z.
Referencing through a named data object.
Most data controls outside of expressions .
A name
o X:=Y+K where K=2*Z
A name of a larger data object with a selection operation.
o Class_name.class_member_name
Program elements that may be named
Variable names.
Formal parameter names.
Subprogram names.
Names for defined types.
Names for defined constants.
Statement labels (names for statements).
Exception names.
Names for primitive operations, e.g., + , * ,SQRT.
Names for literal constants, e.g., 17, 3.25.
Name can be :
Simple name: designating an entire data structure.
Composite name: a name for a component of a data structure.
Eg, a.room
Associations and Referencing Environments
Association: binding of identifiers(simple names) to particular data objects and subprograms.
During Execution of a program:
1. At the beginning of execution of the main program, associations are made(variables and
subprograms).
2. During execution of main program, it invokes referencing operations, to determine the
particular data object or subprogram associated with an identifier.


A:=B+FN(C)
3. When each subprogram is called, a new set of associations is created.
4. It invokes referencing operations too.
5. When the subprogram return, its associations are destroyed.
6. The main program continues ...
Referencing Environments
Referencing Environment of a Program or Subprogram: Set of identifier associations
available for use in referencing during execution.
Invariant during execution.
Set up when the subprogram activation is created.
Remains unchanged during lifetime of activation
Referencing environment components
Local referencing environment.
o Created on entry to a subprogram.
Nonlocal referencing environment.
o Not Created on entry to a subprogram.
Global referencing environment.
o Created at the start of execution of main program.
Predefined referencing environment.
o Defined directly in the language definition.
Visibility : An Association for an identifier is said to be visible within a subprogram if it is part
of the referencing environment for that subprogram.
Hidden if redefined.
Dynamic scope: Dynamic scope of an association consists of the set of subprogram activations
within which it is visible.
Referencing operation: an operation with the signature
ref-op: id*referencing-environment -> data object or subprogram
Local, nonlocal, and global references.
If Association is Local then Referencing is called Local referencing
Similarly others
Aliases for Data Objects


Data object is visible through more than one name in the same referencing environment
Each name referred to as alias of data object
Troublesome for programmer
o It makes understanding difficult
Eg.
X=A+B
Y=C+D
Aliasing makes Optimization difficult
Dynamic Scope
The Dynamic scope of an association for an identifier, is the set of subprogram
activations in which the association is visible during execution.
A Dynamic scope rule defines the dynamic scope of each association in terms of the
dynamic course of program execution.
o For example you can define it according to the dynamic chain of subprogram
activations.
Static Scope
The static scope of a declaration is that part of the program text where a use of the
identifier is a reference to that particular declaration of the id.
A static scope rule is a rule for determining the static scope of a declaration.
o For example the rule used in Pascal.
The importance of static scope
Static type checking: faster and more reliable.
Reading a program easier
Block structure
In a block-structured language, each program or subprogram is organized as a set of
nested blocks.
Chief characteristics of block: Introducing a new local referencing environment
Static scope rules associated with block structure
Head of each block defines ref environment


If the identifier is referenced and no local declaration then outer outer blocks are
checkedup too the predefined language env., and then an error.
Declarations within Inner blocks are hidden from outside.
Block name becomes part of local referencing environment of containing block.
Local data and local referencing environment
Local environment of subprogram Q consists of
o ids in the head of Q ,
o variable names,
o formal parameter names,
o subprogram names
Static scope rules: implemented by means of a table of the local declarations
Dynamic scope rules:
o Retention - Associations and the bound values are retained after execution
o Deletion - Associations are deleted
Implementation of dynamic scope rules
By means of a local environment table to associate names, types and values.
Retention: the table is kept as part of the code segment
Deletion: the table is kept as part of the activation record, destroyed after each execution.
3. Shared data in subprograms
Sharing of data objects :
1) direct sharing through parameter transmission
2) through nonlocal environments
explicit common environments and implicit nonlocal environments
dynamic scope
static scope
inheritance
Parameter and Parameter Transmission
When we use parameters and when we use nonlocal reference?
Parameters for
o data


o subprograms
o statement labels
Methods for Transmitting Parameters
Call by name
call by reference
call by value
call by value-result
call by constant value
call by result
Call by name
The actual parameter is to be substituted everywhere for the formal parameter in the body
of the called program before execution of the subprogram begins.
If we have call Sub(X)
o Actual parameter X, as a simple parameterless subprogram.
o Solve the ambiguity if X is already a variable known to Sub.
Call by value
Upon invoking a subprogram, the value is passed (r-value ).
The formal parameter contains the value that is used.
No alias
Call by value-result
The formal parameter is a local variable of the same type as the actual parameter.
Like call by value, but the result is copied in the actual parameter at the end.
Call by constant value
No change in the value of the formal par. Is allowed during prog. Execution.
If transmitted to another subprogram it must be by constant value too.
Just an input
Call by result
The formal parameter is a local variable with no initial value.
The result is copied in the actual parameter at the end.
Implementation of parameter transmission
In activation record.


Each formal parameter P is a local data object P
o a local data object of type T (type of actual parameter).
o a pointer to a data object of type T .
Various actions
o at the point of call : actual parameters evaluated.
o at the point of entry and exit:
prologue: complete the transmission.
epilogue: copy the result values (in transmissions by result or value-
result).
Compiler:
o transmission of parameters.
o static type checking.
4. Abstract Data Types Revisited
Abstract Data Type
Abstract data type is a new data type defined by the programmer
It includes
o New data type
o Operations
o Encapsulation
Implementation of Abstract Data Type
Two models of implementation
Indirect Encapsulation: The actual storage is maintained in the activation record
corresponding to the part where the data type is defined and when declared within
another part the activation record of that contains a pointer to the actual storage
Direct Encapsulation: the actual storage is maintained where it is declared.
Direct Vs I ndirect
In Indirect case implementation of abstract data type truly independent.
Indirect case has a run-time penalty.
In direct case accessing of components will be faster.


In direct case if abstract object changes then all the instances of its use must also be
recompiled.

Generic Abstract Data Type
One basic data type may be defined
Attribute of the type may be specified separately
eg. Template Classes in C++
I nstantiation of Generic Abstract type definition
Generic abstract type definition represents template
From templates particular abstract data types are created
Process of creating particular abstract data type from template is called instantiation
I mplementation
Compiler uses the generic abstract data type definition
Inserts specified values for the parameters
Compiles the definition as if it were an ordinary abstract data type definition

Inheritance
Inheritance is the receiving of properties or characteristics of one component by another
component according to a special relationship that exists between two components. For Example
In block structured language inner block inherited from outer blocks, Classes in C++ etc.
I mplementation
Inherited names from base class are added to the local name space of the derived class
Two approaches
Copy- Based Approach : Every instance of class object has its own data storage
Consisting of data objects making up objects
Pointers to all methods defined for the objects of that class
Delegation-Based Approach
Object of derived class uses the data storage of the base class
Inherited properties are not duplicated




I nheritance Principles
1. Specialization: Usual form of inheritance: Checking inherits properties of Account. Opposite
is generalization: Account is more general than Checking.
2. Decomposition: Breaking an encapsulated object into parts. Opposite concept is aggregation.
3. Instantiation: Creation of instances of an object: rational A, B, C; Represents 3
instantiations of object rational.
4. Individualization: Related to specialization. Separate objects by function, not structure. A
stack and a set can both be an array and and an index pointer, but functionality different.
Opposite is grouping.
Abstract classes
Simply a template and not allow objects to be declared with that definition
Two alternatives to this model:
Abstract Superclass
No objects created from abstract superclass
Mixin Inheritance
We only define the difference between the base class and the new derived
class
We define a delta class
New class = Old class + Delta class
Polymorphism
Single operator or subprogram name refer to many number of function definitions
depending on the data type of arguments and results.
Polymorphism can be Function Polymorphism or Operator Polymorphism

You might also like