Java Reference
In-Depth Information
in the enclosing scope within the procedure. Thus, both c and e[] are placed
after b because both block 1 and block 2 are enclosed by the block comprising
p's body. As blocks are compiled, a “high-water mark” is maintained that rep-
resents the maximum o
set used by any local variable. This high-water mark
determines the size of the overall frame. Thus, a[9] occupies the maximum
o
ff
ff
set within the frame, so its location determines the size of p's frame.
The process of assigning local variables procedure-level o
sets is some-
times done using scope flattening . That is, local declarations are mapped to
equivalent procedure-level declarations. This process is particularly e
ff
ective
if procedure-level register allocation is part of the compilation process (see
Section 13.3.2 on page 508).
ff
12.2.6 More About Frames
We now consider briefly a number of language and hardware issues that a
ff
ect
the design and use of frames at runtime.
Closures In C it is possible to create a pointer to a function. Since a function's
frame is created only when it is called, a function pointer is implemented as
the function's entry point address. In C
, pointers to member functions of a
class are allowed. When the pointer is used, a particular instance of the class
must be provided by the user program. That is, two pointers are needed, one
to the function itself and a second to the class instance in which it resides.
This second pointer allows the member function to correctly access local data
belonging to the class.
Other languages, particularly functional languages like Lisp, Scheme, and
ML, are much more general in their treatment of functions. Functions are first-
class objects . They can be stored in variables and data structures, constructed
during execution, and returned as function results.
Runtime creation and manipulation of functions can be extremely useful.
For example, it is sometimes the case that the computation of f(x) takes a
significant amount of time. Once f(x) is known, a common optimization,
called memoizing ,recordsthepair(x,f(x)) so that subsequent calls to f
with argument x canusetheknownvalueoff(x) rather than recompute it.
In ML it is possible to write a function memo that takes a function f and an
argument arg. memo computes f(arg) and also returns a “smarter” version of
f that has the value of f(arg) “built into” it. This smarter version of f can be
used instead of f in all subsequent computations:
++
fun memo(fct,parm)= let val ans = fct(parm) in
(ans, fn x=> if x=parm then ans else fct(x)) end;
 
 
Search WWH ::




Custom Search