This article is participating in “Java Theme Month – Java Development Practice”, for details: juejin.cn/post/696826…
This is the 11th day of my participation in Gwen Challenge
If the profile
Java virtual machine instructions consist of a one-byte number (called an opcode) that represents a particular operation,
Opcode) followed by zero representing at least one of the parameters (called Operands) required for the operation.
Instruction is introduced
By limiting the length of Java virtual machine opcodes to one byte (that is, 0 to 255), this means that the total number of opcodes in the instruction set cannot exceed 256.
Most instructions contain information about the data type of their operations, for example:
-
The ILoAD instruction is used to load int data from a local variable table into the operand stack
-
The FLOAD directive loads data of type float.
-
Most directives do not support the integer types byte, char, and short, or even Boolean.
-
Most operations on Boolean, byte, short, and CHAR data actually use the corresponding int type as the operation type
Load and store instructions
Instructions for transferring data back and forth between a local table of variables in a stack frame and the operand stack include the following.
Load a local variable onto the stack:
Iload, ILoAD_ < N >, Lload, Lload_ < N >, fload, FLOAD_ < N >, Dload, dload_ < N >, ALOad, ALOAD_ < N >.Copy the code
Stores a value from the operand stack to the local variable table
Istore, istore_ < N >, lstore, lstore_ < N >, fstore, fstore_ < N >, dstore, dstore_ < N >, astore, astore_ < N >.Copy the code
Load a constant onto the operand stack
Bipush, sipush, LDC, LDC_w, LDC2_W, ACONST_NULL, ICONST_M1, iconST_ < I >, LCONST_ < L >, FCONST_ < F >, dCONST_ < D >.Copy the code
An instruction that extends the access index of a local variable table
wide
An operation or arithmetic instruction
Used to perform a specific operation on two operand stacks and store the result back to the top of the stack.
Add instructions: iadd, ladd, fadd, dadd. Subtraction instructions: ISub, LSUB, fsub, dsub. Multiplication instructions: IMul, LMUl, FMUl, dMUl, etcCopy the code
Type conversion instruction
Two different numeric types can be converted to each other, and the Java Virtual machine directly supports wide type conversions (that is, safe conversions from small to large range types) of the following numeric types:
- Int to long, float, or double.
- Long to float, double.
- Float to double.
Playing a role of Narrowing Numeric Conversions, it is necessary to explicitly use conversion instructions. These conversion instructions include:
I2b, I2C, I2S, L2I, F2I, F2L, D2I, D2L and D2F.Copy the code
Directives for creating class instances:
New.
Instructions for creating arrays:
Newarray, AneWarray, multianewarray.
Access field instructions:
Getfield, putfield, getStatic, putStatic.
Array access related instructions
An instruction to load an array element onto the operand stack:
Baload, Caload, Saload, iaload, Laload, Faload, Daload, aaload.Copy the code
An instruction to store the values of an operand stack into an array element:
Bastore, Castore, sastore, iastore, Fastore, Dastore, aastore.Copy the code
The instruction to take the length of an array:
Arraylength.Copy the code
Directives to check class instance types:
Instanceof, checkcast.Copy the code
Operand stack management instructions
As with the stack in a normal data structure, the Java virtual machine provides instructions for manipulating the operand stack directly, including:
Removes one or two elements from the top of the operand stack
Pop, pop2.Copy the code
Copy one or two values from the top of the stack and push the copied value or double copy back to the top:
Dup, DUP2, DUp_X1, DUp2_x1, DUP_x2, dup2_x2.Copy the code
Swap the top two values of the stack:
swap
Copy the code
Control transfer instruction
A control transfer instruction allows the Java virtual machine to conditionally or unconditionally proceed from a specified location to the next instruction instead of the control transfer instruction. Understood from a conceptual model, a control transfer instruction can be thought of as modifying the value of a PC register conditionally or unconditionally. The control transfer instructions are as follows.
Conditional branch:
Ifeq, IFLT, IFLE, IFNE, IFGT, IFGE, IFNULL, IFnonNULL, IF_ICMPEQ, IF_ICMPne, IF_ICMPLt, IF_ICMPGT, IF_ICMPLE, IF_ICMPGE, if_ACMPEq and If_acmpne.Copy the code
Compound condition branch:
Tableswitch, lookupswitch.Copy the code
Unconditional branch:
Goto, GOTO_ W, JSR, JSR_ W, ret.Copy the code
Method call instruction
- The Invokevirtual directive is used to invoke instance methods of objects, dispatching them based on the actual type of the object (virtual method partitioning)
Pie), which is also the most common method dispatch in the Java language.
- The InvokeInterface directive is used to invoke an interface method, which searches at run time for a pair that implements the interface method
For example, find the appropriate method to call.
-
The Invokespecial directive is used to call instance methods that require special processing, including instance initialization methods, private methods, and parent methods.
-
The Invokestatic directive is used to invoke class methods (static methods).
-
The InvokeDynamic directive dynamically resolves and executes the method referenced by the call point qualifier at run time. The dispatch logic for the first four invokeDynamic directives is hardwired into the Java VIRTUAL machine, and the dispatch logic for the InvokeDynamic directive is determined by the bootstrapped method specified by the user.
Method call instructions are independent of the data type.
Method return instruction
Are distinguished by the type of return value, including
Ireturn (used when the return value is Boolean, byte, CHAR, short, and int), lReturn, freturn, dreturn, and Areturn, There is also a return directive for methods declared as void, instance initializers, and class initializers for classes and interfaces.Copy the code
Exception handling instruction
The operations (throws) that explicitly throw exceptions in Java programs are implemented by the Athrow instruction
Synchronized instruction
Monitorenter and Monitorexit directives support the semantics of the synchronized keyword