Java Reference
In-Depth Information
Top of Stack
Space for b = 10
Dynamic Link
Static Link
Frame Pointer
Space for b = −10
Dynamic Link
Static Link
Space for a
Dynamic Link = Null
Static Link = Null
Figure 12.6: An Example of Static Links
information area. The static link will point to the frame of the procedure that
statically encloses the current procedure. If a procedure is not nested within
any other procedure, its static link is null. This approach is illustrated in
Figure 12.6.
As usual, dynamic links always point to the next frame down in the stack.
Static links always point downward in the stack, but they may skip past many
frames. Static links always point to the most recent frame of the subprogram
that statically encloses the current routine. Thus, in our example, the static
links of both of q's frames point to p, since it is p that encloses q's definition.
In evaluating the expression a+b that q returns, b,beinglocaltoq, is accessed
directly through the frame pointer. Variable a is local to p, but also visible to
q because q nests within p. a is accessed by extracting q's static link, and then
using that address (plus the appropriate o
set) to access a.
An alternative to using static links to access frames of enclosing routines
is the use of a display . A display generalizes our use of a frame pointer.
Rather than maintaining a single register, we maintain a set of registers which
comprise the display. If procedure definitions nest n deep (this can be easily
determined by examining a program's abstract syntax tree (AST)), we will
need n +
ff
1 display registers. Each procedure definition is tagged with a nesting
level . Procedures that are not nested within any other routine are at nesting
level 0, while procedures that are nested within a procedure at level n are at
level n +
1. Frames for routines at level n are always accessed using display
register Dn . Thus, whenever a procedure r is executing, we have direct access
to r's frame plus the frames of all routines that enclose r.Eachofthese
routines must be at a di
erent
display register. Consider Figure 12.7, which illustrates our earlier example,
now using display registers rather than static links.
Since q is at nesting level 1, its frame is pointed to by D 1. All of q's local
variables, including b,areatafixedo
ff
erent nesting level, and hence will use a di
ff
set relative to D 1. Similarly, since p is at
nesting level 0, its frame and local variables are accessed via D 0. Note that each
ff
 
Search WWH ::




Custom Search