sub program control
TRANSCRIPT
-
8/6/2019 Sub Program Control
1/32
Chapter 9: Subprogram Control
Subprogram Sequence Control
Attributes of Data Control Parameter passing
Explicit Common Environments
Subprogram Control :interaction among subprogramshow subprograms pass data among themselves
-
8/6/2019 Sub Program Control
2/32
Subprogram Sequence Control
Simple subprogram call returnCopy rule view of subprograms:
the effect of a call statement is the
same as if the subprogram werecopied and inserted into the mainprogram.
-
8/6/2019 Sub Program Control
3/32
-
8/6/2019 Sub Program Control
4/32
Simple flow of execution
CALL
RETURN
-
8/6/2019 Sub Program Control
5/32
Simple call-return subprograms
Execution of subprograms
Subprogram definition.
Subprogram activation.
-
8/6/2019 Sub Program Control
6/32
Subprogram definition
The definition is translated into a template,
used to create an activation each time asubprogram is called.
-
8/6/2019 Sub Program Control
7/32
Subprogram activationa code segment(the invariant part) -
executable code and constants,
anactivationrecord (the dynamicpart) -local data, parameters.
created anew each timethe subprogramis called,
destroyed whenthe subprogramreturns.
-
8/6/2019 Sub Program Control
8/32
System-definedpointers
y Current-instructionpointer
CIPaddressofthe nextstatementtobe
executed
y Current-environmentpointer CEPpointertothe activationrecord.
-
8/6/2019 Sub Program Control
9/32
-
8/6/2019 Sub Program Control
10/32
Oncall
instruction
Anactivationrecord iscreated
CurrentCIP and CEP are saved inthe createdactivationrecord asreturnpoint
CEP isassigned the addressofthe activationrecord.
CIP getsthe addressofthe firstinstructioninthecode segment
The executioncontinuesfrom the addressinCIP
-
8/6/2019 Sub Program Control
11/32
Onretu
rn
The old values ofCIP and CEP are retrieved.
The executioncontinues from the address inCIP
Restrictions of the model:
at most one activationof any subprogram
-
8/6/2019 Sub Program Control
12/32
The simplestimplementation
Allocate storage for asingle activation record
statically asanextensionofthe code segment.Used inFORTRANand COBOL.
The activationrecord isnotdestroyed - only
reinitialized for eachsubprogramexecution.
Hardware support- CIP isthe programcounter,CEP isnotused, simple jumpexecuted onreturn.
-
8/6/2019 Sub Program Control
13/32
Stack-basedimplementationThe simplestrun-time storage managementtechnique
callstatements: push CIP andCEPreturnstatements: popCIP andCEP offofthe stack.
UsedinmostCimplementations
LISP: usesthe stackasanenvironment.
-
8/6/2019 Sub Program Control
14/32
Recursive Subprograms
SpecificationSyntactically - no difference
Semantically - multiple activations of thesame subprogram exist simultaneously atsome point in the execution.
E.G. the first recursive call creates a secondactivation within the lifetime of the firstactivation.
-
8/6/2019 Sub Program Control
15/32
Implementation
Stack-based -
CIP and CEP are stored in stack, forminga dynamic chain of links.
A new activation record is created foreach call and destroyed on return.
The lifetimes of the activation recordscannot overlap - they are nested.
-
8/6/2019 Sub Program Control
16/32
Attributes of Data ControlData control features determine theaccessibility of data at different points during
program execution.
Central problem:
the meaning of variable names, i.e. thecorrespondence between names and memorylocations.
-
8/6/2019 Sub Program Control
17/32
Names and Referencing
Environments
Two ways to make a data object available as an
operand for an operation
Direct transmission
Referencing through a named data object
-
8/6/2019 Sub Program Control
18/32
Directtransmission
A dataobjectcomputed atone pointastheresultofanoperation may be directly
transmitted toanotheroperationasanoperand
Example: x = y + 2*z;
The resultofmultiplicationistransmitteddirectly asanoperand ofthe additionoperation
-
8/6/2019 Sub Program Control
19/32
Referencingthrough
anameddataobject
Adataobjectmay be givenaname
whenitis created,
the name may thenbe usedtodesignate it
as anoperandofanoperation.
-
8/6/2019 Sub Program Control
20/32
Programelementsthatmaybe
namedTobe discussednextVariablesFormalparameters
Subprograms
resolved at translation time:Defined types
DefinedconstantsLabelsException namesPrimitive operations
Literalconstants
-
8/6/2019 Sub Program Control
21/32
Associations and Referencing
Environments
Association: binding identifiers to particular dataobjects and subprograms
Referencing environment: the set of identifierassociations for a given subprogram.
Referencing operations during programexecution: determine the particular data object orsubprogram associated with an identifier
-
8/6/2019 Sub Program Control
22/32
Localreferencingenvironment
The setofassociations created onentrytoa subprogram
formal parameters,
local variables, and
subprograms defined only withinthatsubprogram
-
8/6/2019 Sub Program Control
23/32
Non-localreferencingenvironment
The setofassociations foridentifiers used withina subprogram
not created onentry toit
Global referencingenvironment:associations created atthe startofexecutionofthe
mainprogram, available tobe used ina subprogram
Predefined referencingenvironments:predefined associations inthe language definition
-
8/6/2019 Sub Program Control
24/32
A
ssociationsVisibility of associations
Associationsare visible ifthey arepartofthe referencing environment.Otherwise associationsare hidden
Dynamic Scope of associations
The setofsubprogram activationswithinwhich the associationisvisible
-
8/6/2019 Sub Program Control
25/32
AliasesforDataObjects
Multiple namesofadataobject- separate environments- noproblem- in asingle referencing environment- called
aliases.
Problemswith aliasing Can make code difficulttounderstand
Implementation difficultiesatthe optimizationstep - difficulttospotinterdependentstatements-nottoreorderthem
-
8/6/2019 Sub Program Control
26/32
Example of aliasingProgram main;
var I: integer;
procedure Sub1 ( var J: integer);
begin
(* I and J refer to same dataobject *)
end;
begin
. Sub1(I);
.
end.
-
8/6/2019 Sub Program Control
27/32
Static and Dynamic Scope
The dynamic scope of an association for anidentifier:
the set of subprogram activations in which the
association is visible during execution. tied to the dynamic chain of subprogram
activations.
The static scope of a declaration
the part ofthe program textwhere thedeclared identifier is used.
-
8/6/2019 Sub Program Control
28/32
Dynamicscope rules
Staticscope rules
Dynamic scope rules :Relate references with associations fornamesduring program execution
Static scope rules :
relate references with declarations ofnamesinthe programtext.
-
8/6/2019 Sub Program Control
29/32
B
lock structure
Block-structured languages :
y Each program or subprogram is organized asa set of nested blocks.
y
Each block introduces a new localreferencing environment.
-
8/6/2019 Sub Program Control
30/32
Subprogram A
Declaration of X
Declaration of Y
Hidden to A
SubprogramB
Declaration of Y
Declaration of Z
Use of Y
Use of X
Use of Z
Static scoperules forblock-structured
programs
-
8/6/2019 Sub Program Control
31/32
Local Data and Local Referencing
Environments
Local environment of a subprogram:various identifiers declared in the subprogram :
variables, parameters, subprogram names.
Static scope rules: implemented by means of atable of the local declarations
Dynamic scope rules:Retention - Associations and the bound values
are retained after executionDeletion - Associations are deleted
-
8/6/2019 Sub Program Control
32/32
Implementationofdynamicscope
rules
Bymeansofalocalenvironmenttable toassociate
names, typesandvalues.
Retention: the table iskeptaspartofthe codesegment
Deletion: the table iskeptaspartoftheactivationrecord, destroyedaftereach execution.