Java Reference
In-Depth Information
iconst_5
invokestatic #5;//Methodcsquare:(I)I
If we were to implement interfaces (this is left as an exercise), we would want to generate
the
invokeinterface
instruction.
5.4.2 Field Selection
When we talk about generating code for a field selection, we are interested in the case where
we want the value
7
for the field selection expression. The case where the field selection occurs
as the target of an assignment statement is considered in the next section.
As for message expressions, much of the work in decoding a field selection occurs during
analysis. By the time the code generation phase begins, the compiler has determined all of
what it needs to know to generate code.
JFieldSelection
's
codegen()
works as follows:
1. It generates code for its target. (If the target is a class, no code is generated.)
2. The compiler must again treat the special case
a.length
where
a
is an array. As was
noted in the discussion of analysis, this is a language hack. The code generated is also
a hack, making use of the special instruction
arraylength
.
3. Otherwise, it is treated as a proper field selection. The field selection instruction is
determined:
getfield
for instance fields and
getstatic
for static fields.
4. The
addMemberAccessInstruction()
method is invoked with the following argu-
ments:
(a) The instruction (
getfield
or
getstatic
)
(b) The JVM name for the target's type
(c) The field name
(d) The JVM descriptor for the type of the field, and so the type of the result
For example, given that
instanceField
is indeed an instance field, the field selection
s.instanceField
would be translated as
aloads'
getfieldinstanceField:I
On the other hand, for a static field selection such as
Square1.staticField
our compiler would generate
getstaticstaticField:I
7
Actually, as we shall see in the next section, the eld expression'sr-value.
Search WWH ::
Custom Search