Run Time Environment
Run Time Environment
STACK
Data Access Without Nested Procedures
Issues With Nested Procedures
Nesting Depth
Access Links
Displays
ACCESS TO NONLOCAL DATA ON THE STACK
In this section, we consider how procedures access
their data.
The mechanism for finding data used within a
procedure p but that does not belong to p.
Access becomes more complicated in languages where
procedures can be declared inside other procedures.
We therefore begin with the simple case of c functions,
then introduce a language ML.
ML is a general purpose modular functional
programming language with compile time type
checking.
ML that permits both nested function declarations and
functions as “first-class objects; ”i.e., functions can
take functions as arguments and return functions as
values.
This capabilities can be supported by modifying the
implementation of run-time stack.
DATA ACCESS WITHOUT NESTED
PROCEDURES
In the c family of languages, all variables are defined
either within a single function or outside any
function(“globally”).
It is impossible to declare one procedure whose scope is
entirely within another procedure.
Rather, a global variable v has a scope consisting of all
the functions that follow the declaration of v, except
where there is a local definition of the identifier v.
Variables declared within a function have a scope
consisting of that function only, or part of it.
For languages that do not allow nested procedure
declarations, allocation of storage for variables and
access to those variables is simple:
Global variables are allocated static storage. The
locations of these variables remain fixed are known at
compile time.
Any other name must be local to the activation at the
top of the stack.
We may access these variables through the top_sp
pointer of the stack.
An important benefit of static allocation for global is
that declared procedures may be passed as parameters
or returned as results
with no substantial change in the data-access
strategy.
ISSUES WITH NESTED PROCEDURES
Access becomes far more complicated when a language
allows procedure declarations to be nested and also
uses the normal static scoping rule.
At compile time the declaration of p is immediately
nested within q doesn’t tell the relative positions of
their activation records at run time.
Since, either p or q or both may be recursive, there
may be several activation records of p and/or q on the
stack.
One possible solution to this problem is to use “access
links”.
A LANGUAGE WITH NESTED PROCEDURE
DECLARATIONS
ML is a language with nested procedures, we shall
borrow syntax and semantics of this language.
ML is a functional language, variables once declared
and initialized are not changed.
The few exceptions are arrays whose elements can be
changed by special function calls.
Variables are defined, and have their unchangeable
values initialized, by a statement of the form:
Val(name)=<expression>
Functions are defined using the syntax:
fun<name> (<arguments>)=<body>
For function bodies we shall use let- statements of the
form:
let<list of definitions> in <statements> end
NESTING DEPTH