-
A JVM instruction consists of:
- a one-byte opcode, specifying the operation to be performed,
- zero or more operands, supplying arguments or data that are used by the operation.
-
the effective inner loop of a JVM interpreter:
do {
atomically calculate pc and fetch opcode at pc;
if (operands) fetch operands;
execute the action for the opcode;
} while (there is more to do);
- The number and size of the operands are determined by the opcode. If an operand is more than one byte in size, then it is stored in big-endian order.
- The bytecode instruction stream is only single-byte aligned. The two exceptions are the
lookupswitch
andtableswitch
instructions, which are 4-byte aligned.
2.11.1 Types and the JVM
Most of the instructions in the JVM instruction set encode type information about the operations they perform.
-
instruction types in the opcode mnemonic:
i→int
, l→long
, f→float
, d→double
b→byte
, s→short
, c→char
, a→reference
-
A compiler encodes loads of literal values of types
byte
andshort
using JVM instructions that sign-extend those values to values of typeint
at compile-time or run-time. -
Likewise, values of types
boolean
andchar
are zero-extended.
- Certain JVM instructions such as
pop
andswap
operate on the operand stack without regard to type; however, such instructions are constrained to use only on values of certain categories of computational types.
2.11.2 Load and Store Instructions
local variables ⇌ operand stack
- Load a local variable onto the operand stack:
Tload
,Tload_<n>
, T∈{i, l, f, d, a}, n∈{0, 1, 2, 3} - Store a value from the operand stack into a local variable:
Tstore
,Tstore_<n>
, T∈{i, l, f, d, a}, n∈{0, 1, 2, 3} - Load a constant on to the operand stack:
bipush
,sipush
ldc
,ldc_w
,ldc2_w
aconst_null
iconst_m1
,iconst_<i>
, i∈{0, 1, 2, 3, 4, 5}
fconst_<f>
, f∈{0, 1, 2}
lconst_<l>
,dconst_<d>
, l,d∈{0, 1} - Gain access to more local variables using a wider index, or to a larger immediate operand:
wide
Instruction mnemonics shown above with trailing letters between angle brackets denote families of instructions. The letter between the angle brackets specifies the type of the implicit operand for that family of instructions.
2.11.3 Arithmetic Instructions
T∈{i, l, f, d}
X∈{i, l}
- Add:
Tadd
, Subtract:Tsub
, Multiply:Tmul
, Divide:Tdiv
- Remainder:
Trem
, Negate:Tneg
- Shift:
Xshl
,Xshr
,Xushr
- Bitwise:
Xor
,Xand
,Xxor
- Local variable increment:
iinc
- Comparison:
dcmpg
,dcmpl
,fcmpg
,fcmpl
,lcmp
2.11.4 Type Conversion Instructions
The type conversion instructions allow conversion between JVM numeric types.
-
The JVM directly supports the following widening numeric conversions:
- int to long, float, or double:
i2l
,i2f
,i2d
- long to float or double:
l2f
,l2d
- float to double:
f2d
- int to long, float, or double:
-
The JVM also directly supports the following narrowing numeric conversions:
- int to byte, short, or char:
i2b
,i2s
,i2c
- long to int:
l2i
- float to int or long:
f2i
,f2l
- double to int, long, or float:
d2i
,d2l
,d2f
- int to byte, short, or char:
2.11.5 Object Creation and Manipulation
The JVM creates and manipulates class instances and arrays using distinct sets of instructions:
- Create:
class instance:new
array:newarray
,anewarray
,multianewarray
- Access fields of classes and fields of class instances:
getstatic
,putstatic
,getfield
,putfield
-
array component ⇌ operand stack
Taload
,Tastore
, T∈{b, c, s, i, l, f, d, a} - Get the length of array:
arraylength
- Check properties of class instances or arrays:
instanceof
,checkcast
2.11.6 Operand Stack Management Instructions
pop
, pop2
dup
, dup2
, dup_x1
, dup2_x1
, dup_x2
, dup2_x2
swap
2.11.7 Control Transfer Instructions
- Conditional branch:
ifeq
,ifne
,iflt
,ifle
,ifgt
,ifge
ifnull
,ifnonnull
if_icmpeq
,if_icmpne
,if_icmplt
,if_icmple
,if_icmpgt
,if_icmpge
if_acmpeq
,if_acmpne
- Compound conditional branch:
tableswitch
,lookupswitch
- Unconditional branch:
goto
,goto_w
jsr
,jsr_w
ret
2.11.8 Method Invocation and Return Instructions
- Five instructions that invoke methods:
invokevirtual
invokeinterface
invokespecial
invokestatic
invokedynamic
- The method return instructions:
Treturn
, T∈{i, l, f, d, a}
return
2.11.9 Throwing Exceptions
athrow
2.11.10 Synchronization
The JVM supports synchronization of both methods and sequences of instructions by a single synchronization construct: the monitor.
- Method-level synchronization is performed implicitly, as part of method invocation and return.
- The JVM supplies the
monitorenter
andmonitorexit
instructions to support synchronized block of the Java language. - Structured locking is the situation when, during a method invocation, every exit on a given monitor matches a preceding entry on that monitor.
网友评论