Java Reference
In-Depth Information
13.1.1 Allocating memory addresses
As we learned in Section 12.2 on page 447, local variables and parameters
are allocated in the frame associated with a procedure or method. Therefore
we must map each JVM local variable into a unique frame o
set, used to
address the variable in load and store instructions. Since a frame contains
some fixed-size control information followed by local data, a simple formula
like o ff set = const + size index su
ff
ces, where index is the JVM index assigned
to a variable, size is the size (in bytes) of each stack value, const is the size (in
bytes) of the fixed-size control area in the frame, and o ff set is the frame o
ff
set
used in generated MIPS code.
Static fields of classes are assigned fixed, static addresses when a class
is compiled. These addresses are used whenever a static field is referenced.
Instance fields are accessed as an o
set relative to the beginning of a particular
object. The compiler must provision for the instance fields of all superclasses
of the compiled class. In Java, the Object class has no instance fields. Thus, if
we had a class Complex defined as
ff
class Complex extends Object { float re; float im;}
then the two fields re and im, each one word in size, can be given o
sets
of 0 and 4, respectively, within instances of the class. The JVM instruction
getfield Complex/im F fetches field im of the Complex object referenced by
the top-of-stack. The F in the instruction denotes the type of the field (as
discussed in Section 10.2.2 on page 399). Translation is straightforward. We
first look up the o
ff
set of field im in class Complex,whichis4.Apointertothe
referenced object is in the register corresponding to top-of-stack, say $t0.We
could add 4 to $t0, but since the MIPS has an indexed addressing mode that
adds a constant to a register automatically (denoted const($reg)), we need
generate no code. We simply generate lw $t1,4($t0), which loads the field
into register $t1, and which now corresponds to the top-of-stack.
ff
13.1.2 Allocating Arrays and Objects
In Java, and hence in the JVM, all instantiated objects are allocated on the heap.
To translate a new or newarray bytecode, we will need to call a heap allocation
subroutine, like malloc in C. We pass the size of the object required and receive
a pointer to a newly allocated block of heap memory. For a new bytecode, the
size required is determined by the number and size of the fields in the object.
In addition, a fixed-size header (to store the size and type of the object) is
required. The overall memory size needed can be computed when the class
definition of the object is compiled. In our earlier example of a Complex object,
 
 
 
Search WWH ::




Custom Search