Java Reference
In-Depth Information
Run-Time Stack
Our run-time stack conforms to the run-time convention described for SPIM in [Larus,
2009] and our Section 6.2.4. Each time a method is invoked, a new stack frame of the type
illustrated in Figure 6.5 is pushed onto the stack. Upon return from the method, the same
frame is popped off the stack.
When the caller wants to invoke a callee, it does the following:
1. Argument passing. The first four arguments are passed in registers $a0 to $a3. Any
additional arguments are pushed onto the stack and thus will appear at the beginning
of the callee's stack frame.
2. Caller-saved registers. Registers $a0 to $a3 and $t0 to $t9 can be used by the callee
without its having to save their values, so it is up to the caller to save any registers
whose values it expects to use after the call, within its own stack frame before executing
the call.
3. Executing the call. After arguments have passed and caller registers have been saved,
we execute the jal instruction, which jumps to the callee's rst instruction and saves
the return address in register $ra.
Once a routine (the callee) has been invoked, it must first do the following:
1. Push stack frame. Allocate memory for its new stack frame by subtracting the frame's
size from the stack pointer $sp. (Recall, the stack grows downward in memory.)
2. Callee-saved registers. The callee must save any of $s0 to $s7, $fp, and $ra before
altering any of them because the caller expects their values will be preserved. We
must always save $fp. $ra must be saved only if the callee invokes another routine.
3. The frame pointer. The new $fp = $sp + stack frame size 4.
The stack frame for an invoked (callee) routine is illustrated in Figure 6.13.
Once it has done its work, the callee returns control to the caller by doing the following:
1. Return value. If the callee returns a value, it must put that value in register $0.
2. Callee-saved registers. All callee-saved registers that were saved at the start of the
routine are restored.
3. Stack frame. The stack frame is popped from the stack, restoring register $fp from its
saved location in the frame, and adding the frame size to register $sp.
4. Return. Execute the return instruction, which causes a jump to the address in register
$ra.
A Layout for Objects
Although we mean only to support static methods, objects do leak into our implementation.
For example, method main() takes as its sole argument an array of strings.
Although, we do not really support objects, doing so need not be dicult. [Corliss and
Lewis, 2007]) propose layouts, which we use here. For example, an arbitrary object might
be organized as in Figure 6.14.
 
Search WWH ::




Custom Search