Java Reference
In-Depth Information
move
$a0,$t0
# Copy $t0 to parm register 1
li
$a1,2
# Load 2 into parm register 2
sw
$t0,32($fp)
# Store $t0 across call
jal
f
# Call function f
# Function value is in $v0
lw
$t0,32($fp)
# Restore $t0
sw
$v0,a
# Store function value in a
Figure 13.5: MIPS code for the function call a = f(i,2);
Additional details must be handled to complete our translation of an
invokestatic instruction. Since variable and expression values may be held
in registers at the point of call, these registers must be saved prior to execution
of the method. All registers that hold values that may be destroyed during the
call (by the instructions in the called method's body) are saved on the stack
and restored after the method completes execution. Registers may be saved by
the caller (these are caller-save registers ) or by the method to be called (these
are callee-save registers ). It does not matter if the caller or callee does the
saving (often both save selected registers), but any register holding a program
value needed after the call must be protected.
If a non-local or global variable is held in a register, it must be saved prior
to a call in its assigned memory location. This guarantees that the subprogram
will see the correct value during the call. Upon return, registers holding non-
local or global variables must be reloaded since the subprogram may have
updated their values.
As an example, consider the function call a = f(i,2);,wherea is a static
field, f is a static method and i is a local variable held in register $t0, a caller-
save register. Assume that a storage temporary, assigned frame o
set 32,is
created to hold the value of $t0 acrossthecall. TheMIPScodeshownin
Figure 13.5 is produced.
When amethod is called, space for its framemust be pushed, and the frame
and stack pointers must be properly updated. This is normally done in the
prologue of the called method, just before its body is executed. Similarly, after
a method is finished, its frame must be popped, and frame and stack pointers
properly reset. This is done in the method's epilogue , just before branching
back to the caller's return address. The exact code sequences vary according
to hardware and operating system conventions. The MIPS instructions shown
in Figure 13.6 can be used to push, and later pop, a method's frame. (The size
of the frame, frameSz, is determined when the method is compiled and all its
local declarations are processed; on the MIPS architecture, the runtime stack
grows downward.)
ff
 
Search WWH ::




Custom Search