Java Reference
In-Depth Information
A compiler is free to use a method's registers in any manner, provided that
register usage respects type consistency. For example, suppose register 4 holds
a reference to an object after an astore 4 instruction. The reference cannot be
pushed onto the stack as an int using an iload 4 instruction. Such an error
is detected by the bytecode verification phase of the JVM.
Registers and Types
Although registers are untyped, most Java execution environments are re-
quired to perform static analysis (often called bytecode verification )onthe
JVMcode to ensure that values flow in and out of registers without compromis-
ing Java's type system. Such analysis prevents a JVM program from loading a
reference to an object (using aload) and subsequently performing math on the
reference to trick the JVM into accessing storage inappropriately. In the iload
example on page 402, analysis can show that the stack's top contains an int
value. Whatever instruction consumes that value must type-match success-
fully with the int value. For example, an istore 3 would successfully pop
the 23 from TOS and store it in register 3. However, an fstore 3 would be
detected by the analysis as a faulty instruction when the analysis is performed
(prior to executing the code).
In this regard, the JVM appears to be stricter than the Java language:
an int value can be treated as a float without casting in Java. However,
it is important to understand that the transition from int to float, while
allowed by the language without casting, is nonetheless a type transformation.
Semantic analysis as described in Chapters 2 and 8 can insert the int-to-float
type conversion , which is then realized by generating an i2f instruction.
i2f
The JVM type conversion instructions operate by pop-
ping a value o
the stack and pushing its converted
value. The stack must contain a value of the appropri-
ate type at its top,
In the example, the int 23 is at the TOS. When the in-
struction has finished, the value is e
ff
23
23.0
ff
ectively replaced
by its float representation.
Before
After
The bit patterns for 23 and 23
erent, with the former in two's
complement and the latter in IEEE floating point format. Moreover, bytecode
verification tracks the type of the stack cells, and the i2f instruction leaves a
cell of type float on TOS.
.
0aremarkedlydi
ff
 
Search WWH ::




Custom Search