Java Reference
In-Depth Information
the size required would be 8 bytes plus header information, commonly 2 or 4
words.
For newarray, we determine the allocation size bymultiplying the number
of elements requested (in the register corresponding to the top-of-stack) by
the size required for individual array elements (stored in the symbol table
entry for the requested class). Again, space for a fixed-size object header
must be included. The object header includes size and type information of
the allocated array for runtime reference, as well as the object's monitor for
synchronization. Default initialization of fields within objects must also be
performed by clearing or copying appropriate bit patterns (based on type
declarations).
In languages like C and C
, objects and arrays can be allocated inline
in the current frame if their size is known at compile-time. We can do a
similar allocation within the current frame if we know that no reference to the
allocated object escapes . That is, if no reference to the allocated object or array
is assigned to a field or returned as a function value, then the object or array
is no longer accessible after the current method (and its corresponding frame)
are terminated.
As a further optimization, string objects (which are immutable) are often
defined to be a string literal. Space for such a string may be allocated statically,
andaccessedviaafixedstaticaddress.
In the JVM, array elements are accessed or updatedusing a single bytecode
(e.g., iaload and iastore for integer arrays). In conventional architectures,
like the MIPS, several instructions are needed to access an array, especially if
array bounds checking is included. Details of array indexing are discussed
in Section 12.3 on page 460. Here we will just show the kind of code that is
needed to implement a JVM array load or store instruction.
An iaload instruction expects an array index at the top of the stack, and
an array reference at the top
++
1 position. In our implementation, both of
these values will be loaded or evaluated into MIPS registers. Let us call these
registers $index and $array. We will need to generate code to check that
$index is a legal index, and then to actually fetch the desired integer value
from the array. Since arrays are just objects, the size of the array, and the array
elements, are at fixed o
ff
sets relative to the start of the array object. Assume
the array size is at o
set OFFSET. Then the
MIPS code shown if Figure 13.3 can be used to implement iaload,leavingthe
array value in register $val. For simplicity and e
ff
set SIZE and that elements are at o
ff
ciency, we will assume a
null reference is represented by an invalid address that will force a memory
fault.
The register $temp is a work register, used within the code sequence to
hold intermediate values (see Section 13.3.1). The iastore instruction is very
similar. A value at top
2(in$val) is stored in the array referenced at top
1
 
Search WWH ::




Custom Search