Java Reference
In-Depth Information
D.2.1 pc Register
The JVM can support many threads of execution at once, and each thread has its own pc
(program counter) register. At any point, each JVM thread is executing the code of a single
method, the current method for that thread. If the method is not native , the pc register
contains the address of the JVM instruction currently being executed.
D.2.2 JVM Stacks and Stack Frames
The JVM is not a register machine, but a stack machine. Each JVM thread has a run-time
data stack, created at the same time as the thread. The JVM stack is analogous to the stack
of a conventional language such as C; it holds local variables and partial results, and plays
a role in method invocation and return. There are instructions for loading data values onto
the stack, for performing operations on the value(s) that are on top of the stack, and there
are instructions for storing the results of computations back in variables.
For example, consider the following simple expression.
34+6*11
If the compiler does no constant folding, it might produce the following JVM code for
performing the calculation.
ldc34
ldc6
ldc11
imul
iadd
Executing this sequence of instructions takes a run-time stack through the sequence of
states illustrated in Figure D.1
FIGURE D.1 The stack states for computing 34+6*11 .
In (a), the run-time stack is in some initial state, where top points to its top value. Executing
the rst ldc (load constant) instruction causes the JVM to push the value 34 onto the stack,
leaving it in state (b). The second ldc , pushes 6 onto the stack, leaving it in state (c). The
third ldc pushes 11 onto the stack, leaving it in state (d). Then, executing the imul (integer
multiplication) instruction causes the JVM to pop off the top two values (11 and 6) from
the stack, multiply them together, and push the resultant 66 back onto the stack, leaving it
in state (e). Finally, the iadd (integer addition) instruction pops off the top two values (66
and 34) the stack, adds them together, and pushes the resultant 100 back onto the stack,
leaving it in the state (f).
As we saw in Chapter 1, the stack is organized into stack frames. Each time a method is
invoked, a new stack frame for the method invocation is pushed onto the stack. All actual
 
Search WWH ::




Custom Search