0% found this document useful (0 votes)
108 views

Run Time Environment

The document discusses how procedures access nonlocal data on the stack in languages that allow nested procedures. It introduces the concept of access links, where each activation record has a pointer forming a chain to activations at lower nesting depths. When a procedure is called, the access links are used to find the correct activation record and nonlocal data. The document also discusses using a display array as a more efficient alternative to long chains of access links.

Uploaded by

sahana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Run Time Environment

The document discusses how procedures access nonlocal data on the stack in languages that allow nested procedures. It introduces the concept of access links, where each activation record has a pointer forming a chain to activations at lower nesting depths. When a procedure is called, the access links are used to find the correct activation record and nonlocal data. The document also discusses using a display array as a more efficient alternative to long chains of access links.

Uploaded by

sahana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

ACCESS TO NONLOCAL DATA ON THE

STACK
 Data Access Without Nested Procedures
 Issues With Nested Procedures

 A Language With Nested Procedure Declarations

 Nesting Depth

 Access Links

 Manipulating Access Links

 Access links for procedure parameters

 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

 Let us give nesting depth1 to procedures that are not


nested within any other procedure.
 For example, all c functions are at nesting
depth1.However, if a procedure p is defined
immediately within a procedure at nesting depth i
then give up nesting depth i+1.
fun sort(input file, output file)=let
Val a=array(11,0);
fun read Array(input file)=….a…..;
fun exchange(i,j)=..a…;
fun quicksort(m,n)=let
Val v=…;
fun partition( y,z )=…a….v….exchange…
in …..a…v…partition…. Quicksort
end
in
….a…read Array….quicksort….
end;
ACCESS LINKS
 A direct implementation of the normal static
scope rule for nested functions is obtained by
adding a pointer called access link to each
activation record.
 Access links form a chain from the activation
record at the top of the stack to a sequence of
activations at progressively lower nesting depths.
ACCESS LINKS FOR FINDING NONLOCAL
DATA
MANIPULATING ACCESS LINKS
 To determine access links, simple case occurs
when a procedure call is to a particular procedure
whose name is given explicitly in the procedure
call.
 The harder case is when the call is to a
procedure-parameter, the particular procedure
being called is not known until run time.
 Consider two cases, what happens when a
procedure q calls procedure p explicitly:
 Procedure p is at higher nesting depth than q. Then p
must be defined immediately within q, or the call by q
would not be at a position that is within scope of the
procedure name p.
 1) Procedure p is at a higher nesting depth than q.
then p must be defined immediately within q or the
call by q
 2)The call is recursive, that is p=q then the access link
for the new activation record is as same as that of the
activation record.
 3)The nesting depth np of p is less than the nesting
depth nq of q. in order for the call within q to be in a
scope of name p, procedure
q must be nested within procedure r, while procedure p
is immediately defined within r.
 The top of the activation record can be found by
following chain of access links, starting in the
activation record for q, for nq-np+1 hops.
ACCESS LINKS FOR PROCEDURE
PARAMETER

 When a procedure p is passed to another


procedure q as a parameter, and q then calls its
parameter, it is possible that q doesn’t know the
context in which p appears in the program. If so,
it is impossible for q to know how to set the
access link for p.
 The solution for this problem is , when
procedures are used as parameters, the caller
needs to pass, along with the name of the
procedure-parameter, the proper access link for
the parameter.
ML PROGRAM THAT USES FUNCTIONAL
PARAMETERS

fun a(x)= let


fun b(x)=…f…;
fun c(y)= let
fun d(z)=….in
…..b(d)….
end
in
…c(1)…
end
DISPLAYS
 One problem with the access-link approach to nonlocal
data is if the nesting depth gets large,
have to follow long chain of links to reach the
data.
 A more efficient implementation uses an auxiliary
array d, called the display, which consists of one
pointer for each nesting depth,
d[i] is a pointer to the highest activation
record on the stack for any procedure at
nesting depth i.
MAINTAINING THE DISPLAY

You might also like