Movatterモバイル変換


[0]ホーム

URL:


Contents |Prev |Next |IndexThe Java Virtual Machine Specification


CHAPTER 7

Compiling for the Java Virtual Machine


The Java virtual machine is designed to support the Java programming language. Sun's JDK releases and Java 2 SDK contain both a compiler from source code writtenin the Java programming language to the instruction set of the Java virtual machine, and a runtime system that implements the Java virtual machine itself. Understanding how one compiler utilizes the Java virtual machine is useful to the prospective compiler writer, as well as to one trying to understand the Java virtual machine itself.

Although this chapter concentrates on compiling source code written in the Java programming language, the Java virtual machine does not assume that the instructions it executes were generated from such code. While there have been a number of efforts aimed at compiling other languages to the Java virtual machine, the current version of the Java virtual machine was not designed to support a wide range of languages. Some languages may be hosted fairly directly by the Java virtual machine. Other languages may be implemented only inefficiently.

Note that the term "compiler" is sometimes used when referring to a translator from the instruction set of a Java virtual machine to the instruction set of a specific CPU. One example of such a translator is a just-in-time (JIT) code generator, which generates platform-specific instructions only after Java virtual machine code has been loaded. This chapter does not address issues associated with code generation, only those associated with compiling source code written in the Java programming language to Java virtual machine instructions.


7.1 Format of Examples

This chapter consists mainly of examples of source code together with annotated listings of the Java virtual machine code that thejavac compiler in Sun's JDK release 1.0.2 generates for the examples. The Java virtual machine code is written in the informal "virtual machine assembly language" output by Sun'sjavap utility, distributed with the JDK software and the Java 2 SDK. You can usejavap to generateadditional examples of compiled methods.

The format of the examples should be familiar to anyone who has read assembly code. Each instruction takes the form

<index> <opcode> [<operand1> [<operand2>...]] [<comment>]
The <index> is the index of the opcode of the instruction in the array that contains the bytes of Java virtual machine code for this method. Alternatively, the <index> may be thought of as a byte offset from the beginning of the method. The <opcode> is the mnemonic for the instruction's opcode, and the zero or more <operandN> are the operands of the instruction. The optional <comment> is given in end-of-line comment syntax:

8 bipush 100// Pushint constant100
Some of the material in the comments is emitted byjavap; the rest is supplied by the authors. The <index> prefacing each instruction may be used as the target of a control transfer instruction. For instance, a goto 8 instruction transfers control to the instruction at index 8. Note that the actual operands of Java virtual machine control transfer instructions are offsets from the addresses of the opcodes of those instructions;these operands are displayed byjavap (and are shown in this chapter) as more easily read offsets into their methods.

We preface an operand representing a runtime constant pool index with a hash sign and follow the instruction by a comment identifying the runtime constant pool item referenced, as in

  10   ldc #1 // Pushfloat constant100.0
or

   9   invokevirtual #4// MethodExample.addTwo(II)I
For the purposes of this chapter, we do not worry about specifying details such as operand sizes.


7.2 Use of Constants, Local Variables, and Control Constructs

Java virtual machine code exhibits a set of general characteristics imposed by the Java virtual machine's design and use of types. In the first example we encounter many of these, and we consider them in some detail.

Thespin method simply spins around an emptyfor loop 100 times:

void spin() {    int i;    for (i = 0; i < 100; i++) {     ;// Loop body is empty    }}
A compiler might compilespin to

Methodvoidspin()   0iconst_0// Pushint constant0   1 istore_1// Store into local variable 1 (i=0)   2goto 8// First time through don't increment   5iinc 1 1// Increment local variable 1 by 1 (i++)   8iload_1// Push local variable 1 (i)   9bipush 100// Pushint constant100  11if_icmplt 5// Compare and loop if less than (i<100)  14return// Returnvoid when done
The Java virtual machine is stack-oriented, with most operations taking one or more operands from the operand stack of the Java virtual machine's current frame or pushing results back onto the operand stack. A new frame is created each time a method is invoked, and with it is created a new operand stack and set of local variables for use by that method (seeSection 3.6, "Frames"). At any one point of the computation, there are thus likely to be many frames and equally many operand stacks per thread of control, corresponding to many nested method invocations. Only the operand stack in the current frame is active.

The instruction set of the Java virtual machine distinguishes operand types by using distinct bytecodes for operations on its various data types. The methodspin operates only on values of typeint. The instructions in its compiled code chosen to operate on typed data (iconst_0, istore_1, iinc, iload_1, if_icmplt) are all specialized for typeint.

The two constants inspin,0 and100, are pushed onto the operand stack using two different instructions. The0 is pushed using an iconst_0 instruction, one of the family of iconst_<i> instructions. The100 is pushed using a bipush instruction, which fetches the value it pushes as an immediate operand.

The Java virtual machine frequently takes advantage of the likelihood of certain operands (int constants -1, 0, 1, 2, 3, 4 and 5 in the case of the iconst_<i> instructions) by making those operands implicit in the opcode. Because the iconst_0 instruction knows it is going to push anint0, iconst_0 does not need to store an operand to tell it what value to push, nor does it need to fetch or decode an operand. Compiling the push of0 as bipush 0 would have been correct, but would have made the compiled code forspin one byte longer. A simple virtual machine would have also spent additional time fetching and decoding the explicit operand each time around the loop. Use of implicit operands makes compiled code more compact and efficient.

Theinti inspin is stored as Java virtual machine local variable 1. Because most Java virtual machine instructions operate on values popped from the operand stack rather than directly on local variables, instructions that transfer values between local variables and the operand stack are common in code compiled for the Java virtual machine. These operations also have special support in the instruction set. Inspin, values are transferred to and from local variables using the istore_1 and iload_1 instructions, each of which implicitly operates on local variable 1. The istore_1 instruction pops anint from the operand stack and stores it in local variable 1. The iload_1 instruction pushes the value in local variable 1 onto the operand stack.

The use (and reuse) of local variables is the responsibility of the compiler writer. The specialized load and store instructions should encourage the compiler writer to reuse local variables as much as is feasible. The resulting code is faster, more compact, and uses less space in the frame.

Certain very frequent operations on local variables are catered to specially by the Java virtual machine. The iinc instruction increments the contents of a local variable by a one-byte signed value. The iinc instruction inspin increments the first local variable (its first operand) by 1 (its second operand). The iinc instruction is very handy when implementing looping constructs.

Thefor loop ofspin is accomplished mainly by these instructions:

   5iinc 1 1// Increment local 1 by 1 (i++)   8iload_1// Push local variable 1 (i)   9bipush 100// Pushint constant100  11if_icmplt 5// Compare and loop if less than (i<100)
The bipush instruction pushes the value 100 onto the operand stack as anint, then the if_icmplt instruction pops that value off the operand stack and compares it against i. If the comparison succeeds (the variablei is less than100), control is transferred to index 5 and the next iteration of thefor loop begins. Otherwise, controlpasses to the instruction following the if_icmplt.

If thespin example had used a data type other thanint for the loop counter, the compiled code would necessarily change to reflect the different data type. For instance, if instead of anint thespin example uses adouble, as shown,

void dspin() {    double i;    for (i = 0.0; i < 100.0; i++) {        ;// Loop body is empty    }}
the compiled code is

Methodvoid dspin()   0 dconst_0// Pushdouble constant0.0   1 dstore_1// Store into local variables 1 and 2   2 goto 9// First time through don't increment   5 dload_1// Push local variables 1 and 2    6 dconst_1// Pushdouble constant1.0    7 dadd// Add; there is no dinc instruction   8 dstore_1// Store result in local variables 1 and 2   9 dload_1// Push local variables 1 and 2   10 ldc2_w #4 // Pushdouble constant100.0   13 dcmpg// There is no if_dcmplt instruction  14 iflt 5// Compare and loop if less than (i<100.0)  17 return// Returnvoid when done
The instructions that operate on typed data are now specialized for typedouble. (The ldc2_w instruction will be discussed later in this chapter.)

Recall thatdouble values occupy two local variables, although they are only accessed using the lesser index of the two local variables. This is also the case for values of typelong. Again for example,

double doubleLocals(double d1, double d2) {    return d1 + d2;}
becomes

MethoddoubledoubleLocals(double,double)   0 dload_1// First argument in local variables 1 and 2   1 dload_3// Second argument in local variables 3 and 4   2 dadd   3 dreturn
Note that local variables of the local variable pairs used to storedouble values indoubleLocals must never be manipulated individually.

The Java virtual machine's opcode size of 1 byte results in its compiled code being very compact. However, 1-byte opcodes also mean that the Java virtual machine instruction set must stay small. As a compromise, the Java virtual machine does not provide equal support for all data types: it is not completely orthogonal (seeTable 3.2, "Type support in the Java virtual machine instruction set").

For example, the comparison of values of typeint in thefor statement of examplespin can be implemented using a single if_icmplt instruction; however, there is no single instruction in the Java virtual machine instruction set that performs a conditional branch on values of typedouble. Thus,dspin must implement its comparison of values of typedouble using a dcmpg instruction followed by an iflt instruction.

The Java virtual machine provides the most direct support for data of typeint. This is partly in anticipation of efficient implementations of the Java virtual machine's operand stacks and local variable arrays. It is also motivated by the frequency ofint data in typical programs. Other integral types have less direct support. There are nobyte,char, orshort versions of the store, load, or add instructions, for instance. Here is thespin example written using ashort:

void sspin() {    short i;    for (i = 0; i < 100; i++) {        ;// Loop body is empty    }}
It must be compiled for the Java virtual machine, as follows, using instructions operating on another type, most likelyint, converting betweenshort andint values as necessary to ensure that the results of operations onshort data stay within the appropriate range:

Methodvoidsspin()   0 iconst_0   1 istore_1   2 goto 10   5 iload_1// Theshort is treated as though anint   6 iconst_1   7iadd   8 i2s // Truncateint toshort   9 istore_1  10 iload_1  11 bipush 100  13 if_icmplt 5  16 return
The lack of direct support forbyte,char, andshort types in the Java virtual machine is not particularly painful, because values of those types are internally promotedtoint (byte andshort are sign-extended toint,char is zero-extended). Operations onbyte,char, andshort data can thus be done usingint instructions. The only additional cost is that of truncating the values ofint operations to valid ranges.

Thelong and floating-point types have an intermediate level of support in the Java virtual machine, lacking only the full complement of conditional control transfer instructions.


7.3 Arithmetic

The Java virtual machine generally does arithmetic on its operand stack. (The exception is the iinc instruction, which directly increments the value of a local variable.) For instance, thealign2grain method aligns anint value to a given power of 2:

int align2grain(int i, int grain) {    return ((i + grain-1) & ~(grain-1));}
Operands for arithmetic operations are popped from the operand stack, and the results of operations are pushed back onto the operand stack. Results of arithmetic subcomputations can thus be made available as operands of their nesting computation. For instance, the calculation of~(grain-1) is handled by these instructions:

   5 iload_2// Pushgrain    6 iconst_1// Pushint constant1    7 isub// Subtract; push result    8 iconst_m1// Pushint constant -1    9 ixor// Do XOR; push result
Firstgrain - 1 is calculated using the contents of local variable 2 and an immediateint value1. These operands are popped from the operand stack and their difference pushed back onto the operand stack. The difference is thus immediatelyavailable for use as one operand of the ixor instruction. (Recall that ~x == -1^x.) Similarly, the result of the ixor instruction becomes an operand for the subsequent iand instruction.

The code for the entire method follows:

Methodintalign2grain(int,int)   0 iload_1   1 iload_2   2 iadd   3 iconst_1   4 isub   5 iload_2   6 iconst_1   7 isub   8 iconst_m1   9 ixor  10iand  11ireturn

7.4 Accessing the Runtime Constant Pool

Many numeric constants, as well as objects, fields, and methods, are accessed via the runtime constant pool of the current class. Object access is considered later(§7.8). Data of typesint,long,float, anddouble, as well as references to instances of classString, are managed using the ldc, ldc_w, and ldc2_w instructions.

The ldc and ldc_w instructions are used to access values in the runtime constant pool (including instances of classString) of types other thandouble andlong. The ldc_w instruction is used in place of ldc only when there is a large number of runtime constant pool items and a larger index is needed to access an item. The ldc2_w instruction is used to access all values of typesdouble andlong; there is no non-wide variant.

Integral constants of typesbyte,char, orshort, as well as smallint values, may be compiled using the bipush, sipush, or iconst_<i> instructions, as seen earlier(§7.2). Certain small floating-point constants may be compiled using the fconst_<f> and dconst_<d> instructions.

In all of these cases, compilation is straightforward. For instance, the constants for

void useManyNumeric() {    int i = 100;    int j = 1000000;    long l1 = 1;    long l2 = 0xffffffff;    double d = 2.2;    ...do some calculations...}
are set up as follows:

MethodvoiduseManyNumeric()   0bipush 100// Push a smallint with bipush   2istore_1   3 ldc #1 // Pushint constant1000000; a largerint// value uses ldc   5 istore_2   6 lconst_1// A tinylong value uses short, fast lconst_1   7 lstore_3   8ldc2_w #6 // Pushlong0xffffffff (that is, anint -1); any//long constant value can be pushed using ldc2_w  11lstore 5  13 ldc2_w #8 // Pushdouble constant2.200000; uncommon//double values are also pushed using ldc2_w  16 dstore 7...do those calculations...

7.5 More Control Examples

Compilation offor statements was shown in an earlier section(§7.2). Most of the Java programming language's other control constructs (if-then-else,do,while,break, andcontinue) are also compiled in the obvious ways. The compilation ofswitch statements is handled in a separate section (Section 7.10, "Compiling Switches"), as are the compilation of exceptions (Section 7.12, "Throwing and HandlingExceptions") and the compilation offinally clauses (Section 7.13, "Compilingfinally").

As a further example, awhile loop is compiled in an obvious way, although the specific control transfer instructions made available by the Java virtual machine vary by data type. As usual, there is more support for data of typeint, for example:

void whileInt() {    int i = 0;    while (i < 100) {        i++;    }}
is compiled to

MethodvoidwhileInt()   0 iconst_0   1 istore_1   2 goto 8   5 iinc 1 1   8 iload_1   9 bipush 100  11 if_icmplt 5  14 return
Note that the test of thewhile statement (implemented using the if_icmplt instruction) is at the bottom of the Java virtual machine code for the loop. (This was also the case in thespin examples earlier.) The test being at the bottom of the loop forces the use of a goto instruction to get to the test prior to the first iteration of the loop. If that test fails, and the loop body is never entered, this extra instruction is wasted. However,while loops are typically used when their body is expected to be run, often for many iterations. For subsequent iterations, putting the test at the bottom of the loop saves a Java virtual machine instruction each time around the loop: if the test were at the top of the loop, the loop body would need a trailing goto instruction to get back to the top.

Control constructs involving other data types are compiled in similar ways, but must use the instructions available for those data types. This leads to somewhat less efficient code because more Java virtual machine instructions are needed, for example:

void whileDouble() {    double i = 0.0;    while (i < 100.1) {        i++;    }}
is compiled to

MethodvoidwhileDouble()   0 dconst_0   1 dstore_1   2 goto 9   5 dload_1   6 dconst_1   7 dadd   8 dstore_1   9 dload_1  10 ldc2_w #4 // Pushdouble constant100.1  13 dcmpg// To do the compare and branch we have to use...  14 iflt 5// ...two instructions  17 return
Each floating-point type has two comparison instructions: fcmpl and fcmpg for typefloat, and dcmpl and dcmpg for typedouble. The variants differ only in their treatment of NaN. NaN is unordered, so all floating-point comparisons fail if either of their operands is NaN. The compiler chooses the variant of the comparison instruction for the appropriate type that produces the same result whether the comparison fails on non-NaN values or encounters a NaN.

For instance:

int lessThan100(double d) {    if (d < 100.0) {        return 1;    } else {        return -1;    }}
compiles to

MethodintlessThan100(double)   0 dload_1   1 ldc2_w #4 // Pushdouble constant100.0   4 dcmpg// Push 1 ifd is NaN ord \>100.0;// push 0 ifd ==100.0   5 ifge 10// Branch on 0 or 1   8 iconst_1   9 ireturn  10 iconst_m1  11 ireturn
Ifd is not NaN and is less than100.0, the dcmpg instruction pushes anint -1 onto the operand stack, and the ifge instruction does not branch. Whetherd is greater than100.0 or is NaN, the dcmpg instruction pushes anint 1 onto the operand stack, and the ifge branches. Ifd is equal to100.0, the dcmpg instruction pushes anint 0 onto the operand stack, and the ifge branches.

The dcmpl instruction achieves the same effect if the comparison is reversed:

int greaterThan100(double d) {    if (d > 100.0) {        return 1;    } else {        return -1;    }}
becomes

MethodintgreaterThan100(double)   0 dload_1   1 ldc2_w #4 // Pushdouble constant100.0   4 dcmpl// Push -1 ifd is Nan ord< 100.0;// push 0 ifd ==100.0   5 ifle 10// Branch on 0 or -1   8 iconst_1   9 ireturn  10 iconst_m1  11 ireturn
Once again, whether the comparison fails on a non-NaN value or because it is passed a NaN, the dcmpl instruction pushes anint value onto the operand stack that causes the ifle to branch. If both of the dcmp instructions did not exist, one of the example methods would have had to do more work to detect NaN.


7.6 Receiving Arguments

Ifn arguments are passed to an instance method, they are received, by convention, in the local variables numbered 1 through n of the frame created for the new method invocation. The arguments are received in the order they were passed. For example:

int addTwo(int i, int j) {    return i + j;}
compiles to

MethodintaddTwo(int,int)   0iload_1// Push value of local variable 1 (i)   1iload_2// Push value of local variable 2 (j)   2iadd// Add; leaveint result on operand stack   3 ireturn// Returnint result
By convention, an instance method is passed areference to its instance in local variable 0. In the Java programming language the instance is accessible via thethis keyword.

Class (static) methods do not have an instance, so for them this use of local variable zero is unnecessary. A class method starts using local variables at index zero. If theaddTwo method were a class method, its arguments would be passed in a similar way to the first version:

static int addTwoStatic(int i, int j) {    return i + j;}
compiles to

MethodintaddTwoStatic(int,int)   0 iload_0   1iload_1   2 iadd   3 ireturn
The only difference is that the method arguments appear starting in local variable 0 rather than 1.


7.7 Invoking Methods

The normal method invocation for a instance method dispatches on the runtime type of the object. (They are virtual, in C++ terms.) Such an invocation is implemented using the invokevirtual instruction, which takes as its argument an index to a runtimeconstant pool entry giving the fully qualified name of the class type of the object, the name of the method to invoke, and that method's descriptor(§4.3.3). To invoke theaddTwo method, defined earlier as an instance method, we might write

int add12and13() {    return addTwo(12, 13);}
This compiles to

Methodintadd12and13()   0 aload_0 // Push local variable 0 (this)   1 bipush 12// Pushint constant12   3 bipush 13// Pushint constant13   5 invokevirtual #4// MethodExample.addtwo(II)I   8ireturn // Returnint on top of operand stack; it is // theint result ofaddTwo()
The invocation is set up by first pushing areference to the current instance,this, onto the operand stack. The method invocation's arguments,int values12 and13, are then pushed. When the frame for theaddTwo method is created, the arguments passed to the method become the initial values of the new frame's local variables. That is, thereference forthis and the two arguments, pushed onto the operand stack by the invoker, will become the initial values of local variables 0, 1, and 2 of the invoked method.

Finally,addTwo is invoked. When it returns, itsint return value is pushed onto the operand stack of the frame of the invoker, theadd12and13 method. The return value is thus put in place to be immediately returned to the invoker ofadd12and13.

The return fromadd12and13 is handled by the ireturn instruction ofadd12and13. The ireturn instruction takes theint value returned byaddTwo, on the operand stack of the current frame, and pushes it onto the operand stack of the frame of the invoker. It then returns control to the invoker, making the invoker's frame current. The Java virtual machine provides distinct return instructions for many of its numeric andreference data types, as well as a return instruction for methods with no return value. The same set of return instructions is used for all varieties of method invocations.

The operand of the invokevirtual instruction (in the example, the runtime constant pool index #4) is not the offset of the method in the class instance. The compiler does not know the internal layout of a class instance. Instead, it generates symbolic references to the methods of an instance, which are stored in the runtime constant pool. Those runtime constant pool items are resolved at run time to determine the actual method location. The same is true for all other Java virtual machine instructions that access class instances.

InvokingaddTwoStatic, a class (static) variant ofaddTwo, is similar, as shown:

int add12and13() {    return addTwoStatic(12, 13);}
although a different Java virtual machine method invocation instruction is used:

Methodint add12and13()   0 bipush 12   2 bipush 13   4 invokestatic #3 // MethodExample.addTwoStatic(II)I   7 ireturn
Compiling an invocation of a class (static) method is very much like compiling an invocation of an instance method, exceptthis is not passed by the invoker. The method arguments will thus be received beginning with local variable 0 (seeSection 7.6, "Receiving Arguments"). The invokestatic instruction is always used to invoke class methods.

The invokespecial instruction must be used to invoke instance initialization methods (seeSection 7.8, "Working with Class Instances"). It is also used when invoking methods in the superclass (super) and when invokingprivate methods. For instance, given classesNear andFar declared as

class Near {    int it;    public int getItNear() {        return getIt();    }    private int getIt() {        return it;    }}class Far extends Near {    int getItFar() {        return super.getItNear();    }}
the methodNear.getItNear (which invokes aprivate method) becomes

MethodintgetItNear()   0 aload_0   1 invokespecial #5 // MethodNear.getIt()I   4 ireturn
The methodFar.getItFar (which invokes a superclass method) becomes

MethodintgetItFar()   0 aload_0   1 invokespecial #4// MethodNear.getItNear()I   4ireturn
Note that methods called using the invokespecial instruction always passthis to the invoked method as its first argument. As usual, it is received in local variable 0.


7.8 Working with Class Instances

Java virtual machine class instances are created using the Java virtual machine's new instruction. Recall that at the level of the Java virtual machine, a constructor appears as a method with the compiler-supplied name<init>. This specially named method is known as the instance initialization method(§3.9). Multiple instance initialization methods, corresponding to multiple constructors, may exist for a given class. Once the class instance has been created and its instance variables, including those of the class and all of its superclasses, have been initialized to their default values, an instance initialization method of the new class instance is invoked. For example:

Object create() {    return new Object();}
compiles to

Methodjava.lang.Objectcreate()   0 new #1 // Classjava.lang.Object   3 dup   4 invokespecial #4 // Methodjava.lang.Object.<init>()V   7 areturn
Class instances are passed and returned (asreference types) very much like numeric values, although typereference has its own complement of instructions, for example:

int i;// An instance variableMyObj example() {    MyObj o = new MyObj();    return silly(o);}MyObj silly(MyObj o) {    if (o != null) {        return o;    } else {        return o;    }}
becomes

MethodMyObjexample()   0 new #2 // ClassMyObj   3 dup   4 invokespecial #5 // MethodMyObj.<init>()V   7 astore_1   8 aload_0   9 aload_1  10 invokevirtual #4 // MethodExample.silly(LMyObj;)LMyObj;  13 areturnMethodMyObjsilly(MyObj)   0 aload_1   1 ifnull 6   4aload_1   5 areturn   6 aload_1   7 areturn
The fields of a class instance (instance variables) are accessed using the getfield and putfield instructions. Ifi is an instance variable of typeint, the methodssetIt andgetIt, defined as

void setIt(int value) {    i = value;}int getIt() {    return i;}
become

MethodvoidsetIt(int)   0 aload_0   1 iload_1   2 putfield #4 // FieldExample.i I   5returnMethodintgetIt()   0 aload_0   1 getfield #4 // FieldExample.i I   4ireturn
As with the operands of method invocation instructions, the operands of the putfield and getfield instructions (the runtime constant pool index #4) are not the offsets of the fields in the class instance. The compiler generates symbolic references to the fields of an instance, which are stored in the runtime constant pool. Those runtime constant pool items are resolved at run time to determine the location of the field within the referenced object.


7.9 Arrays

Java virtual machine arrays are also objects. Arrays are created and manipulated using a distinct set of instructions. The newarray instruction is used to create an array of a numeric type. The code

void createBuffer() {    int buffer[];    int bufsz = 100;    int value = 12;    buffer = new int[bufsz];    buffer[10] = value;    value = buffer[11];}
might be compiled to

MethodvoidcreateBuffer()   0 bipush 100// Pushint constant 100 (bufsz)   2 istore_2// Storebufsz in local variable 2   3 bipush 12// Pushint constant 12 (value)   5 istore_3// Storevalue in local variable 3   6iload_2// Pushbufsz...   7newarrayint// ...and create new array ofint of that length   9 astore_1// Store new array inbuffer  10 aload_1// Pushbuffer  11 bipush 10// Pushint constant10  13 iload_3// Pushvalue  14 iastore// Store value atbuffer[10]  15 aload_1// Pushbuffer  16 bipush 11// Pushint constant11  18 iaload// Push value atbuffer[11]...   19 istore_3// ...and store it invalue  20return
The anewarray instruction is used to create a one-dimensional array of object references, for example:

void createThreadArray() {    Thread threads[];    int count = 10;    threads = new Thread[count];    threads[0] = new Thread();}
becomes

Methodvoid createThreadArray()   0 bipush 10// Pushint constant10   2 istore_2// Initializecount to that   3 iload_2// Pushcount, used by anewarray   4 anewarray class #1 // Create new array of classThread   7 astore_1// Store new array inthreads   8 aload_1// Push value ofthreads   9 iconst_0// Pushint constant0  10 new #1 // Create instance of classThread  13 dup// Make duplicate reference...  14 invokespecial #5 // ...to pass to instance initialization method// Methodjava.lang.Thread.<init>()V  17 aastore// Store newThread in array at0  18 return
The anewarray instruction can also be used to create the first dimension of a multidimensional array. Alternatively, the multianewarray instruction can be used to create several dimensions at once. For example, the three-dimensional array:

int[][][] create3DArray() {    int grid[][][];    grid = new int[10][5][];    return grid;}
is created by

Methodintcreate3DArray()[][][]   0 bipush 10// Pushint10 (dimension one)   2 iconst_5// Pushint5 (dimension two)   3 multianewarray #1 dim #2 // Class[[[I, a three// dimensionalint array;// only create first two // dimensions   7 astore_1// Store new array...   8 aload_1// ...then prepare to return it   9 areturn
The first operand of the multianewarray instruction is the runtime constant pool index to the array class type to be created. The second is the number of dimensions of that array type to actually create. The multianewarray instruction can be used to create all the dimensions of the type, as the code forcreate3DArray shows. Note that the multidimensionalarray is just an object and so is loaded and returned by an aload_1 and areturn instruction, respectively. For information about array class names, seeSection 4.4.1.

All arrays have associated lengths, which are accessed via the arraylength instruction.


7.10 Compiling Switches

Compilation ofswitch statements uses the tableswitch and lookupswitch instructions.The tableswitch instruction is used when the cases of theswitch can be efficientlyrepresented as indices into a table of target offsets. Thedefault target of theswitch is used if the value of the expression of theswitch falls outside the range of valid indices. For instance,

int chooseNear(int i) {    switch (i) {        case 0:  return 0;        case 1:  return 1;        case 2:  return 2;        default: return -1;    }}
compiles to

MethodintchooseNear(int)   0iload_1// Push local variable 1 (argumenti)   1 tableswitch 0 to 2: // Valid indices are 0 through 20: 28// Ifi is0, continue at 28 1: 30// Ifi is1, continue at 30 2: 32// Ifi is2, continue at 32default:34// Otherwise, continue at 34  28 iconst_0//i was0; pushint constant0...  29 ireturn// ...and return it  30 iconst_1//i was1; pushint constant1...  31 ireturn// ...and return it  32 iconst_2//i was2; pushint constant2...  33 ireturn// ...and return it  34 iconst_m1// otherwise pushint constant -1...  35 ireturn// ...and return it
The Java virtual machine's tableswitch and lookupswitch instructions operate only onint data. Because operations onbyte,char, orshort values are internally promoted toint, aswitch whose expression evaluates to one of those types is compiled as though it evaluated to typeint. If thechooseNear method had been written using typeshort, the same Java virtual machine instructions would have been generated as when using typeint. Other numeric types must be narrowed to typeint for use in aswitch.

Where the cases of theswitch are sparse, the table representation of the tableswitch instruction becomes inefficient in terms of space. The lookupswitch instruction may be used instead. The lookupswitch instruction pairsint keys (the values of thecase labels) with target offsets in a table. When a lookupswitch instruction is executed, the value of the expression of theswitch is compared against the keys in the table. If one of the keys matches the value of the expression, execution continues at the associated target offset. If no key matches, execution continues at thedefault target. For instance, the compiled code for

int chooseFar(int i) {    switch (i) {        case -100: return -1;        case 0:   return 0;        case 100:  return 1;        default:   return -1;    }}
looks just like the code forchooseNear, except for the use of the lookupswitch instruction:

MethodintchooseFar(int)   0 iload_1   1 lookupswitch 3:  -100: 36 0: 38 100: 40 default:42  36 iconst_m1  37 ireturn  38 iconst_0  39 ireturn  40 iconst_1  41ireturn  42 iconst_m1  43 ireturn
The Java virtual machine specifies that the table of the lookupswitch instruction must be sorted by key so that implementations may use searches more efficient than a linear scan. Even so, the lookupswitch instruction must search its keys for a match rather than simply perform a bounds check and index into a table like tableswitch. Thus, a tableswitch instruction is probably more efficient than a lookupswitch where space considerations permit a choice.


7.11 Operations on the Operand Stack

The Java virtual machine has a large complement of instructions that manipulate the contents of the operand stack as untyped values. These are useful because of the Java virtual machine's reliance on deft manipulation of its operand stack. For instance,

public long nextIndex() {    return index++;}private long index = 0;
is compiled to

Methodlong nextIndex()   0 aload_0// Pushthis   1 dup// Make a copy of it   2 getfield #4 // One of the copies ofthis is consumed// pushinglong fieldindex,// above the originalthis   5 dup2_x1// Thelong on top of the operand stack is // inserted into the operand stack below the // originalthis   6 lconst_1// Pushlong constant 1    7 ladd// The index value is incremented...   8 putfield #4 // ...and the result stored back in the field  11 lreturn// The original value ofindex is left on// top of the operand stack, ready to be returned
Note that the Java virtual machine never allows its operand stack manipulation instructions to modify or break up individual values on the operand stack.


7.12 Throwing and Handling Exceptions

Exceptions are thrown from programs using thethrow keyword. Its compilation is simple:

void cantBeZero(int i) throws TestExc {    if (i == 0) {        throw new TestExc();    }}
becomes

MethodvoidcantBeZero(int)   0 iload_1// Push argument 1 (i)   1ifne 12// Ifi==0, allocate instance and throw   4new #1 // Create instance ofTestExc   7 dup// One reference goes to the constructor   8 invokespecial #7 // MethodTestExc.<init>()V  11 athrow// Second reference is thrown  12 return// Never get here if we threwTestExc
Compilation oftry-catch constructs is straightforward. For example,

void catchOne() {    try {        tryItOut();    } catch (TestExc e) {        handleExc(e);    }}
is compiled as

MethodvoidcatchOne()   0 aload_0// Beginning oftry block   1 invokevirtual #6 // MethodExample.tryItOut()V   4 return// End oftry block; normal return   5 astore_1// Store thrown value in local variable 1   6 aload_0// Pushthis   7 aload_1// Push thrown value   8 invokevirtual #5 // Invoke handler method: //Example.handleExc(LTestExc;)V  11 return// Return after handlingTestExcException table:   From To Target Type     0     4     5 ClassTestExc
Looking more closely, thetry block is compiled just as it would be if thetry were not present:

MethodvoidcatchOne()   0aload_0// Beginning oftry block   1 invokevirtual #4 // MethodExample.tryItOut()V   4 return// End oftry block; normal return
If no exception is thrown during the execution of thetry block, it behaves as though thetry were not there:tryItOut is invoked andcatchOne returns.

Following thetry block is the Java virtual machine code that implements the singlecatch clause:

   5 astore_1// Store thrown value in local variable 1   6 aload_0// Pushthis    7 aload_1// Push thrown value   8 invokevirtual #5 // Invoke handler method: //Example.handleExc(LTestExc;)V  11 return// Return after handlingTestExcException table:   From To Target Type     0     4     5 ClassTestExc
The invocation ofhandleExc, the contents of thecatch clause, is also compiled like a normal method invocation. However, the presence of acatch clause causes the compiler to generate an exception table entry. The exception table for thecatchOne method has one entry corresponding to the one argument (an instance of classTestExc) that thecatch clause ofcatchOne can handle. If some value that is an instance ofTestExc is thrown during execution of the instructions between indices 0 and 4 incatchOne, control is transferred to the Java virtual machine code at index 5, which implements the block of thecatch clause. If the value that is thrown is not an instance ofTestExc, thecatch clause ofcatchOne cannot handle it. Instead, the value is rethrown to the invoker ofcatchOne.

Atry may have multiplecatch clauses:

void catchTwo() {    try {        tryItOut();    } catch (TestExc1 e) {        handleExc(e);    } catch (TestExc2 e) {        handleExc(e);    }}
Multiplecatch clauses of a giventry statement are compiled by simply appending the Java virtual machine code for eachcatch clause one after the other and adding entries to the exception table, as shown:

Methodvoid catchTwo()   0 aload_0// Begintry block   1 invokevirtual #5 // MethodExample.tryItOut()V   4 return// End oftry block; normal return   5 astore_1// Beginning of handler forTestExc1;// Store thrown value in local variable 1   6 aload_0// Pushthis   7 aload_1// Push thrown value   8 invokevirtual #7 // Invoke handler method://Example.handleExc(LTestExc1;)V  11 return// Return after handlingTestExc1  12 astore_1// Beginning of handler forTestExc2;// Store thrown value in local variable 1  13 aload_0// Pushthis  14 aload_1// Push thrown value  15 invokevirtual #7// Invoke handler method://Example.handleExc(LTestExc2;)V  18 return// Return after handlingTestExc2Exception table:From To Target Type     0     4     5   ClassTestExc1     0     4    12   ClassTestExc2
If during the execution of thetry clause (between indices 0 and 4) a value is thrown that matches the parameter of one or more of thecatch clauses (the value is an instance of one or more of the parameters), the first (innermost) suchcatch clause is selected. Control is transferred to the Java virtual machine code for the block of thatcatch clause. If the value thrown does not match the parameter of any of thecatch clauses ofcatchTwo, the Java virtual machine rethrows the value without invoking code in anycatch clause ofcatchTwo.

Nestedtry-catch statements are compiled very much like atry statement with multiplecatch clauses:

void nestedCatch() {    try {        try {            tryItOut();        } catch (TestExc1 e) {            handleExc1(e);        }    } catch (TestExc2 e) {        handleExc2(e);    }}
becomes

Methodvoid nestedCatch()   0 aload_0// Begintry block   1 invokevirtual #8 // MethodExample.tryItOut()V   4 return// End oftry block; normal return   5 astore_1// Beginning of handler forTestExc1;// Store thrown value in local variable 1   6 aload_0// Pushthis   7 aload_1// Push thrown value   8 invokevirtual #7 // Invoke handler method: //Example.handleExc1(LTestExc1;)V  11 return// Return after handlingTestExc1  12 astore_1// Beginning of handler forTestExc2;// Store thrown value in local variable 1  13 aload_0// Pushthis  14 aload_1// Push thrown value  15 invokevirtual #6 // Invoke handler method://Example.handleExc2(LTestExc2;)V  18 return// Return after handlingTestExc2Exception table:   From To Target Type     0     4     5 ClassTestExc1     0    12    12 ClassTestExc2
The nesting ofcatch clauses is represented only in the exception table. When an exception is thrown, the first (innermost) catch clause that contains the site of the exception and with a matching parameter is selected to handle it. For instance, if the invocation oftryItOut (at index 1) threw an instance ofTestExc1, it would be handled by thecatch clause that invokeshandleExc1. This is so even though the exception occurs within the bounds of the outercatch clause (catchingTestExc2) and even though that outercatch clause might otherwise have been able to handle the thrown value.

As a subtle point, note that the range of acatch clause is inclusive on the "from" end and exclusive on the "to" end (§4.7.3). Thus, the exception table entry for thecatch clause catchingTestExc1 does not cover the return instruction at offset 4. However, the exception table entry for thecatch clause catchingTestExc2 does cover the return instruction at offset 11. Return instructions within nestedcatch clauses are included in the range of instructions covered by nestingcatch clauses.


7.13 Compilingfinally

Compilation of atry-finally statement is similar to that oftry-catch. Prior to transferring control outside thetry statement, whether that transfer is normal or abrupt, because an exception has been thrown, thefinally clause must first be executed.For this simple example

void tryFinally() {    try {        tryItOut();    } finally {        wrapItUp();    }}
the compiled code is

Methodvoid tryFinally()   0 aload_0// Beginning oftry block   1invokevirtual #6 // MethodExample.tryItOut()V   4 jsr 14// Callfinally block   7 return// End oftry block   8 astore_1// Beginning of handler for any throw   9 jsr 14// Callfinally block  12 aload_1// Push thrown value  13 athrow// ...and rethrow the value to the invoker  14 astore_2// Beginning offinally block  15 aload_0// Pushthis  16 invokevirtual #5 // MethodExample.wrapItUp()V  19 ret 2// Return fromfinally blockException table:   From To Target Type0    4    8   any
There are four ways for control to pass outside of thetry statement: by falling through the bottom of that block, by returning, by executing abreak orcontinue statement, or by raising an exception. IftryItOut returns without raising an exception,control is transferred to thefinally block using a jsr instruction. The jsr 14 instruction at index 4 makes a "subroutine call" to the code for thefinally block at index 14 (thefinally block is compiled as an embedded subroutine). When thefinally block completes, the ret 2 instruction returns control to the instruction followingthe jsr instruction at index 4.

In more detail, the subroutine call works as follows: The jsr instruction pushes the address of the following instruction (return at index 7) onto the operand stack before jumping. The astore_2 instruction that is the jump target stores the address on the operand stack into local variable 2. The code for thefinally block (in this case the aload_0 and invokevirtual instructions) is run. Assuming execution of that code completes normally, the ret instruction retrieves the address from local variable 2 and resumes execution at that address. The return instruction is executed, andtryFinally returns normally.

Atry statement with afinally clause is compiled to have a special exception handler, one that can handle any exception thrown within thetry statement. IftryItOut throws an exception, the exception table fortryFinally is searched for an appropriate exception handler. The special handler is found, causing execution to continue at index 8. The astore_1 instruction at index 8 stores the thrown value into local variable 1. The following jsr instruction does a subroutine call to the code for thefinally block. Assuming that code returns normally, the aload_1 instruction at index 12 pushes the thrown value back onto the operand stack, and the following athrow instruction rethrows the value.

Compiling atry statement with both acatch clause and afinally clause is more complex:

void tryCatchFinally() {    try {        tryItOut();    } catch (TestExc e) {        handleExc(e);    } finally {        wrapItUp();    }}
becomes

MethodvoidtryCatchFinally()   0 aload_0// Beginning oftry block   1 invokevirtual #4 // MethodExample.tryItOut()V   4 goto 16// Jump tofinally block   7 astore_3// Beginning of handler forTestExc;// Store thrown value in local variable 3   8 aload_0// Pushthis   9 aload_3// Push thrown value  10 invokevirtual #6 // Invoke handler method://Example.handleExc(LTestExc;)V  13 goto 16// Huh???1  16 jsr 26// Callfinally block  19 return// Return after handlingTestExc  20 astore_1// Beginning of handler for exceptions// other thanTestExc, or exceptions// thrown while handlingTestExc  21 jsr 26// Callfinally block  24 aload_1// Push thrown value...  25 athrow// ...and rethrow the value to the invoker  26 astore_2// Beginning offinally block  27 aload_0// Pushthis  28 invokevirtual #5 // MethodExample.wrapItUp()V  31 ret 2// Return fromfinally blockException table:   From To Target Type     0     4     7 ClassTestExc     0   16    20   any
If thetry statement completes normally, the goto instruction at index 4 jumps to the subroutine call for thefinally block at index 16. Thefinally block at index 26 is executed, control returns to the return instruction at index 19, andtryCatchFinally returns normally.

IftryItOut throws an instance ofTestExc, the first (innermost) applicable exception handler in the exception table is chosen to handle the exception. The code for that exception handler, beginning at index 7, passes the thrown value tohandleExc and on its return makes the same subroutine call to thefinally block at index 26 as in the normal case. If an exception is not thrown byhandleExc,tryCatchFinally returns normally.

IftryItOut throws a value that is not an instance ofTestExc or ifhandleExc itself throws an exception, the condition is handled by the second entry in the exception table, which handles any value thrown between indices 0 and 16. That exception handler transfers control to index 20, where the thrown value is first stored in local variable 1. The code for thefinally block at index 26 is called as a subroutine. If it returns, the thrown value is retrieved from local variable 1 and rethrown using the athrow instruction. If a new value is thrown during execution of thefinally clause, thefinally clause aborts, andtryCatchFinally returns abruptly, throwing the new value to its invoker.


7.14 Synchronization

The Java virtual machine provides explicit support for synchronization through its monitorenter and monitorexit instructions. For code written in the Java programminglanguage, however, perhaps the most common form of synchronization is thesynchronized method.

Asynchronized method is not normally implemented using monitorenter and monitorexit. Rather, it is simply distinguished in the runtime constant pool by theACC_SYNCHRONIZED flag, which is checked by the method invocation instructions. When invoking a method for whichACC_SYNCHRONIZED is set, the current thread acquires a monitor, invokes the method itself, and releases the monitor whether the method invocation completes normally or abruptly. During the time the executing thread owns the monitor, no other thread may acquire it. If an exception is thrown during invocation of thesynchronized method and thesynchronized method does not handle the exception, the monitor for the method is automatically released before the exception is rethrown out of thesynchronized method.

The monitorenter and monitorexit instructions exist to supportsynchronized statements. For example:

void onlyMe(Foo f) {    synchronized(f) {        doSomething();    }}
is compiled to

Methodvoid onlyMe(Foo)   0 aload_1// Pushf   1 astore_2// Store it in local variable 2   2 aload_2// Push local variable 2 (f)   3 monitorenter// Enter the monitor associated withf   4 aload_0// Holding the monitor, passthis and...   5 invokevirtual #5 // ...callExample.doSomething()V   8aload_2// Push local variable 2 (f)   9monitorexit// Exit the monitor associated withf  10return// Return normally  11 aload_2// In case of any throw, end up here  12 monitorexit// Be sure to exit monitor...  13 athrow// ...then rethrow the value to the invokerException table:   FromTo Target Type     4     8    11   any

7.15 Compiling Nested Classes and Interfaces

JDK release 1.1 addednested classes and interfaces to the Java programming language. Nested classes and interfaces are sometimes referred to asinner classes and interfaces, which are one sort of nested classes and interfaces. However, nested classes and interfaces also encompass nested top-level classes and interfaces, which are not inner classes or interfaces.

A full treatment of the compilation of nested classes and interfaces is outside the scope of this chapter. However, interested readers can refer to the Inner Classes Specification athttp://java.sun.com/products/jdk/1.1/docs/guide/innerclasses/spec/innerclasses.doc.html.


1 This goto instruction is strictly unnecessary, but is generated by thejavac compiler of Sun's JDK release 1.0.2.

Contents |Prev |Next |Index

Virtual Machine Specification
Copyright © 1999 Sun Microsystems, Inc.All rights reserved
Please send any comments or corrections through ourfeedback form


[8]ページ先頭

©2009-2025 Movatter.jp