Java Reference
In-Depth Information
This approach is valid for structs and classes allocated on the runtime stack
and heap too. Using our earlier example, if var,oftypes, is a local variable
within a method, then s will be assigned an o
ff
set within the current frame.
The o
ff
set in a frame for a field within s is s's frame o
ff
set plus the field's own
o
set within its struct or class. Similarly, for a struct allocated on the heap, the
address of a field is the field's o
ff
set plus the struct's heap address.
Classes generalize structs by allowing members that are methods as well
as data fields. However, the code for methods is not allocated within the data
allocation for a class object. As described in Section 12.2.3, only one translation
of each method is created; it is used with all instances of the class object it is
defined for. Thus when we translate fields within classes, we ignore method
definitions, making classes and structs e
ff
ff
ectively identical in translation.
12.2.2 Accessing Frames at Runtime
During execution therewill be many frames on the stack. This is because when
a procedure A calls a procedure B,aframeforB's local variables is pushed on
the stack, covering A's frame. A's frame cannot be popped o
because A will
resume execution after B returns. In the case of recursive routines, there can
be hundreds or even thousands of frames on the stack. All frames except the
topmost represent suspended subroutine activations that are waiting for a call
to return.
The topmost frame corresponds to the currently active subroutine, and it
is important to be able to access it directly. Since the frame is at the top of the
stack, the stack top register could be used to access it. But the runtime stack
may also be used to hold data other than frames, including temporary values
or return values too large to fit within a register (compound values like arrays,
structures, and strings).
It is therefore unwise to require that the currently active frame always be
at exactly the top of the stack. Instead a distinct register, often called the frame
pointer , is used to access the current frame. This allows local variables to be
accessed directly as o
ff
set plus frame pointer, using the indexed addressing
mode found on all modern machines.
As an example, consider the following recursive function that computes
factorials:
ff
int fact(int n) {
if (n > 1)
return n * fact(n-1);
else
return 1;
}
 
 
Search WWH ::




Custom Search