Java Reference
In-Depth Information
subi $sp,$sp,frameSz # Push frame on stack
sw $ra,0($sp) # Save return address in frame
sw $fp,4($sp) # Save old frame pointer in frame
move $fp,$sp # Set $fp to access new frame
# Save callee-save registers (if any) here
# Body of method is here
# Restore callee-save registers (if any) here
lw
$ra,0($fp)
# Reload return address register
lw
$fp,4($fp)
# Reload old frame pointer
addi
$sp,$sp,frameSz
# Pop frame from stack
jr
$ra
# Jump to return address
Figure 13.6: MIPS prologue and epilogue code
To translate an invokevirtual instruction, we must implement a dynamic
dispatching mechanism. When a method M is called using invokevirtual,we
are given, as the first parameter, a pointer to the object in which M is to execute.
By semantic analysis we know this object's class, or a parent class, must contain
a definition of M.ButwhatifM is defined in both a parent class and a subclass
(which is quite legal)? How do we know which version of M to execute?
To support garbage collection and heap management, each heap object
has a type code as part of its header. This type code can be used to index into
a dispatch table that contains the addresses of all methods the object contains.
If we assign to each method a unique o
ff
set, we can use method M's o
ff
set in
the object's dispatch table to choose the correct method to execute.
Fortunately, it is often the case that a class C has no subclasses that redefine
M.(e.g.,ifC or M is private or final). If dynamic resolution of C is not required,
we can select M's implementation at compile-time, and generate code to call it
directly without any table-lookup overhead.
13.1.4 Example of Bytecode Translation
As an example of the overall bytecode translation process, let us consider the
following simple method, stringSum. This method sums integers from 1 to its
parameter, limit, and returns a string representation of the sum:
 
 
Search WWH ::




Custom Search