0% found this document useful (0 votes)
56 views34 pages

cmps445 Lecture10

The document discusses different techniques for implementing subprograms in programming languages. It covers calling and returning from subprograms, parameter passing, static and dynamic allocation of local variables, nested subprograms, blocks, and dynamic scoping. For simple subprograms without nesting or dynamic variables, activation records of fixed size can be statically allocated. For subprograms with dynamic local variables, activation records of dynamic size are allocated on the stack. Nested subprograms and nonlocal variable references require static or dynamic chains to locate variable declarations. Blocks can be implemented by treating them as subprograms or allocating their variables on the parent's activation record. Dynamic scoping requires searching the dynamic chain or using a central table to locate nonlocal variables.

Uploaded by

Daddy Bruce
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)
56 views34 pages

cmps445 Lecture10

The document discusses different techniques for implementing subprograms in programming languages. It covers calling and returning from subprograms, parameter passing, static and dynamic allocation of local variables, nested subprograms, blocks, and dynamic scoping. For simple subprograms without nesting or dynamic variables, activation records of fixed size can be statically allocated. For subprograms with dynamic local variables, activation records of dynamic size are allocated on the stack. Nested subprograms and nonlocal variable references require static or dynamic chains to locate variable declarations. Blocks can be implemented by treating them as subprograms or allocating their variables on the parent's activation record. Dynamic scoping requires searching the dynamic chain or using a central table to locate nonlocal variables.

Uploaded by

Daddy Bruce
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/ 34

IMPLEMENTING

SUBPROGRAMS
CHAPTER 10 TOPICS
 The General Semantics of Calls and Returns
 Implementing “Simple” Subprograms
 Implementing Subprograms with Stack-Dynamic Local Variables
 Nested Subprograms
 Blocks
 Implementing Dynamic Scoping
THE GENERAL SEMANTICS OF CALLS AND
RETURNS
 Def: The subprogram call and return operations of a language are together
called its subprogram linkage

 General semantics of calls to a subprogram


 Parameter passing methods
 Stack-dynamic allocation of local variables
 Save the execution status of calling program
 Transfer of control and arrange for the return
 If subprogram nesting is supported, access to nonlocal variables must be arranged
THE GENERAL SEMANTICS OF CALLS AND
RETURNS
 General semantics of returns from a subprogram
 If out mode or inout mode
 Move local parameters values to actual parameters
 Deallocate storage used for local variables
 Restore execution status of the calling program unit
 Control must be retsurned to the calling program unit
IMPLEMENTING “SIMPLE” SUBPROGRAMS
 We mean by “Simple” that they can not be nested and that all
local variables are static.

 Call Semantics:
 1. Save the execution status of the caller
 2. Carry out the parameter-passing process
 3. Pass the return address to the callee
 4. Transfer control to the callee
IMPLEMENTING “SIMPLE” SUBPROGRAMS
 Return Semantics:
1. If pass-by-value-result parameters are used, move the current values of
those parameters to their corresponding actual parameters
2. If it is a function, move the functional value to a place the caller can get it
3. Restore the execution status of the caller
4. Transfer control back to the caller
IMPLEMENTING “SIMPLE” SUBPROGRAMS
 Required Storage: Status information of the caller, parameters,
return address, and functional value (if it is a function)
 The format, or layout, of the non-code part of an executing
subprogram is called an activation record
 An activation record instance is a concrete example of an
activation record (the collection of data for a particular
subprogram activation)
AN ACTIVATION RECORD FOR “SIMPLE”
SUBPROGRAMS
 Has fixed size ➔ can be sttically allocated
CODE AND ACTIVATION RECORDS OF A
PROGRAM WITH “SIMPLE” SUBPROGRAMS
IMPLEMENTING SUBPROGRAMS WITH STACK-
DYNAMIC LOCAL VARIABLES
 More complicated because:
 The compiler must generate code to cause implicit allocation and
deallocation of local variables
 Recursion must be supported (adds the possibility of multiple
simultaneous activations of a subprogram)
TYPICAL ACTIVATION RECORD FOR A LANGUAGE
WITH STACK-DYNAMIC LOCAL VARIABLES
IMPLEMENTING SUBPROGRAMS WITH STACK-
DYNAMIC LOCAL VARIABLES

 The activation record format is static, but its size may be dynamic
 The dynamic link points to the top of the instance of the
activation record of the caller
 An activation record instance is dynamically created when a
subprogram is called
AN EXAMPLE C FUNCTION
void sub(float total, int part)
[4]

{ [3]

int list[5];
[2]

[1]

float sum; [0]


}
AN EXAMPLE C PROGRAM WITHOUT RECURSION
void A(int x) {
int y;
...
C(y);
...
}
void B(float r) {
int s, t;
...
A(s);
...
}
void C(int q) {
...
}
void main() {
float p;
...
B(p);
...
}
STACK CONTENTS FOR PROGRAM
Note that:
 main calls B
 B calls A
 A calls C
IMPLEMENTING SUBPROGRAMS WITH STACK-
DYNAMIC LOCAL VARIABLES
 The collection of dynamic links in the stack at a given time is called the
dynamic chain, or call chain
 Local variables can be accessed by their offset from the beginning of
the activation record. This offset is called the local_offset
 The local_offset of a local variable can be determined by the compiler
 Assuming all stack positions are the same size, the first local variable declared
has an offset of three plus the number of parameters, e.g., the local_offset of Y in
A is 4
RECURSION
RECURSION
RECURSION
NESTED SUBPROGRAMS
 Some non-C-based static-scoped languages (e.g., Fortran 95+,
Ada, Python, JavaScript, Ruby, and Lua) use stack-dynamic local
variables and allow subprograms to be nested
 Observation: All variables that can be nonlocally accessed reside
in some activation record instance in the stack
 The process of locating a nonlocal reference:
 1. Find the correct activation record instance
 2. Determine the correct offset within that activation record instance
THE PROCESS OF LOCATING A NONLOCAL
REFERENCE
 Finding the offset is easy
 Finding the correct activation record instance:
 Static semantic rules guarantee that all nonlocal variables that can be
referenced have been allocated in some activation record instance that is
on the stack when the reference is made
STATIC CHAINS
 A static chain is a chain of static links that connects certain
activation record instances
 The static link in an activation record instance for subprogram A
points to one of the activation record instances of A's static
parent
 The static chain from an activation record instance connects it to
all of its static ancestors
STATIC CHAINS (CONTINUED)
 To find the declaration for a reference to a nonlocal variable:
 You could chase the static chain until the activation record instance (ari)
that has the variable is found, searching each ari as it is found, if variable
names were stored in the ari

 Def: static_depth is an integer associated with a static scope


whose value is the depth of nesting of that scope
STATIC CHAINS (CONTINUED)

If B references a variable from main the chain_offset is 2.


(static_depth of B – static_depth of main)
STATIC CHAINS (CONTINUED)
 Def: The chain_offset or nesting_depth of a nonlocal reference is
the difference between the static_depth of the reference and that
of the scope where it is declared
 A reference can be represented by the pair:
 (chain_offset, local_offset)
 where local_offset is the offset in the activation record of the
variable being referenced
EXAMPLE JAVASCRIPT PROGRAM

Call sequence
STACK
CONTENTS At position 1 in SUB1:
a - (0, 3)
b - (1, 4)
c - (1, 5)

At position 2 in SUB3:
e - (0, 4)
b - (1, 4)
a - (2, 3)

At position 3 in SUB2:
a - (1, 3)
d - an error
e - (0, 5)
BLOCKS
 Blocks are user-specified local scopes for variables
 An example in C
{
int temp;
temp = list [upper];
list [upper] = list [lower];
list [lower] = temp
}
 The lifetime of temp in the above example begins when control enters the
block
 An advantage of using a local variable like temp is that it cannot interfere
with any other variable with the same name
BLOCKS
 Two Methods:
1. Treat blocks as parameterless subprograms
 Every block has an activation record; an instance is created every time
the block is executed
 Uses stack chains.

2. Allocate blocks local variables on top of the ari of the subprogram


IMPLEMENTING DYNAMIC SCOPING
1. Deep Access - nonlocal references are found by searching the
activation record instances on the dynamic chain
 Length of the chain cannot be statically determined
 Every activation record instance must have variable names
STACK CONTENTS FOR
DYNAMIC-SCOPED PROGRAM
void sub3() {
int x, z;
x = u + v;

}
void sub2() {
int w, x;

}
void sub1() {
int v, w;

}
void main() {
int v, u;

}
IMPLEMENTING DYNAMIC SCOPING
2. Shallow Access - put locals in a central place
 One stack for each variable name
 Central table with an entry for each variable name
USING SHALLOW ACCESS TO IMPLEMENT
DYNAMIC SCOPING
void sub3() {
int x, z;
x = u + v;

}
void sub2() {
int w, x;

}
void sub1() {
int v, w;

}
void main() {
int v, u;

}
FUNCTIONAL
PROGRAMMING
LANGUAGES

You might also like