Java Reference
In-Depth Information
Top of Stack
Space for n = 1
Control Information
Return Value = 1
Frame Pointer
Space for n = 2
Control Information
Return Value
Space for n = 3
Control Information
Return Value
Figure 12.3: Runtime Stack for a Call of fact(3)
The runtime stack corresponding to the call fact(3) is shown in Figure 12.3
atthepointwherethecalloffact(1) is about to return. In our example we
show a slot for the function's return value at the very beginning of the frame.
This means that upon return, the return value is conveniently placed on the
stack, just beyond the end of the caller's frame. As an optimization, many
compilers try to return scalar values in specially designated registers. This
helps to eliminate unnecessary loads and stores. For function values too large
to fit in a register (e.g., a struct passed by value), the stack is the natural
choice.
When a subroutine returns, its frame must be popped from the stack and
the frame pointer must be reset to point to the caller's frame. In simple cases
this can be done by adjusting the frame pointer by the size of the current
frame. Because the stack may contain more than just frames (e.g., function
return values or registers saved across calls), it is common practice to save the
caller's frame pointer as part of the callee's control information. Thus each
frame points to the preceding frame on the stack. This pointer is often called
a dynamic link because it links a frame to its dynamic (runtime) predecessor.
The runtime stack corresponding to a call of fact(3), with dynamic links
included, is shown in Figure 12.4.
12.2.3 Handling Classes and Objects
C, C
, and Java do not allow procedures or methods to nest. That is,
a procedure may not be declared within another procedure. This simplifies
runtime data access; all variables are either global or local to the currently
executing procedure. Global variables are statically allocated. Local variables
are part of a single frame, accessed through the frame pointer.
Languages often need to support simultaneous access to variables in mul-
++
,C
 
 
Search WWH ::




Custom Search