JVM-bytecode command, jvm-bytecode
Java Virtual Machine bytecode command
After learning about the class file, I think it is necessary to understand the bytecode commands in the JVM. It is also very helpful to heap the class file and the JVM running mechanism.
The commands of a Java virtual machine are composed of numbers (called the Opcode and Opcode) with a specific operation meaning and zero to multiple parameters (called the operands, Oprands) with a specific operation meaning) and constitute. because Java virtual machine uses the architecture of the operand stack rather than the register, most of the parameter commands do not contain the operand and only have one operation code.
Some features of bytecode commands
Because the length of the Java Virtual Machine Operation Code is limited to one byte (0 ~ 255), which means that the total number of operation codes of the instruction set cannot exceed 256.
Because the class file format does not give up the length alignment of the compiled operands, this means that when the virtual machine processes more than one byte of data, it has to re-create the structure of the specific data from the byte during runtime. if you want to store a 16-bit unsigned integer with two unsigned bytes (name them byte1 and byte2 ), then their values should be like this: (byte <8) | byte2. to some extent, this operation may cause some performance loss when interpreting and executing bytecode. however, the advantage of doing so is also very obvious. If the length alignment of the operands is abandoned, it means that many fill and interval symbols can be omitted: The operation code is expressed in one byte, it is also intended to get as short as possible compiled code.
Bytecode instructions and Data Types
For most of the bytecode commands related to data types, their operation code mnemonic symbols have special characters to indicate the specific data type service: I: stands for int, l stands for long, s stands for short, B stands for byte, c stands for char, f stands for float, d stands for double, and a stands for reference.
The Java virtual machine instruction set only provides limited types of related commands for specific operations. In other words, the instruction set will be intentionally designed as a non-completely independent Java virtual machine specification to call this feature "Not Orthpogoal ". that is, not all operations have corresponding commands. some separate commands can convert unsupported types into supported types when necessary.
Load and store commands
The load and storage commands are used to transmit data back and forth between local variables and operands in the stack frame.
Load a local variable to the operation Stack: ilaod, ioload <n>, llaod, llaod <n>, float, float <n>, double, double <n>, aload, aload <n>.
Store a data from the operand stack to the local variable table: istore, istore <n>, lstore, lstore <n>, fstore. fstore <n>, dstore, dstore <n>, astore, asotre <n>.
Load a constant to the data stack: bipush, sipush, ldc, ldc_w, ldc2_w, aconst_null, iconst_ml, iconst _ <I>, lconst _ <l>, fconst _ <f>, dconst _ <d>
Command for expanding local variable Table Access Index: wide
Operation commands
An operation or arithmetic command is used to perform a specific operation on the values on the two operand stacks, and restore the results to the top of the Operation stack.
Generally, arithmetic commands can be divided into two types: commands for integer data operations for floating point data operations. regardless of the arithmetic commands, the Data Type of the Java Virtual Machine is used. Because the byte, short, char, and boolean arithmetic commands are not directly supported, this type of data operation should be replaced by int-type commands.
Addition command: iadd, ladd, fadd, dadd
Subtraction command: isub, lsub, fsub, dsub
Multiplication command: imul, lmul, fmul, dmul
Remainder instruction: irem, lrem, frem, drem
Reverse command: ineg, lneg, fneg, dneg
Shift command: ishl. ishr, lshl, lshr, lushr
Bitwise OR command: ior, lor
Bitwise AND command: iand, land]
By-bit XOR or command: ixor, lxor
Local variable auto-increment command: iinc
Comparison commands: dcmpy, dcmpl, fcmpg, fsmpyl, lcomp
The Java virtual machine requires that all calculation results be rounded to appropriate precision during floating point operations, and inaccurate results must be rounded to the nearest precise value to be expressed. if there are two forms that can be expressed as similar to this value, the lowest valid bit is given priority to zero, which is called the nearest number rounding mode.
When a floating point number is converted to an integer, the Java virtual machine uses the zero-to-zero round mode in the IEEE754 standard. The Round-to-zero round result in the number being truncated, all valid decimal bytes are discarded. in the toward-zero rounding mode, a number that is closest to but not greater than the original value is selected for the target value type and the value type as the most precise rounding result.
When the Java Virtual Machine is processing floating-point operations, it will not throw any running exception. When an operation overflows, it will be expressed with a signed infinity, if the result of an operation is not clearly defined in mathematics, the NaN value is used for representation. All arithmetic operations that use the NaN value as the operand will return NaN.
When comparing long-type values, the virtual machine uses a signed comparison method. When comparing floating-point values (dcmpy, dcmpl, fcmpg, fcmpl ), the virtual opportunity adopts the Nonsignaling Conparions method defined in the IEEE754 standard.
Type conversion command
The type conversion command can convert two different numeric types.
Java directly supports (that is, conversion commands are not displayed during conversion) the following numeric types of wide type conversion (that is, the range type is converted to a large range of types of safe conversion ):
The conversion commands that must be displayed to complete narrow type conversion include i2b, i2c, i2s, l2i, f2c, d2i, d2l, d2f. narrow type conversion may lead to different positive and negative numbers in the conversion results. different orders of magnitude may cause loss of numerical precision.
When narrowing a floating point value to an integer (int or long), the following conversion rules will be followed.
- If the floating point value is NaN, the conversion result is 0 of the int or long type.
- If the floating point value is not infinite, the floating point value is rounded to the zero rounding mode to obtain the integer v. If the value is within the range indicated by the target type, the conversion result is v.
- Otherwise, it is converted to the maximum positive number that T can represent Based on the symbol v.
Object creation and access commands
Command:
- Command for creating a class instance: new
- Run the following command to create an array: newarray, anewarray, and multianewarray.
- Getfield, putfield, getstatic, putstatic \
- Commands for loading an array element to the operand Stack: baload, caload, saload, iaload, laload, faload, daload, aaload
- Store the value of an operand stack to the command in the array element: bastore, castore, sastore, iastore, fastore, dastore, and aastore
- Command for getting the array length: arraylength
- Check instance type commands: instanceof, checkcast
Operations stack management commands
The Java virtual machine provides instructions for direct operations on the operands:
- One or two elements of the stack at the top of the stack: pop, po2
- Copy one or two elements at the top of the stack and re-Press the copy value or dual value to the top of the stack: dup, dup2, duo_x1, dup_x2, dup2_x2
- Swap two values at the top of the stack: swap
Transfer control instruction
The Control Transfer Instruction allows the Java Virtual Machine to execute the program from the specified location without the control of the next instruction of the transfer instruction. It can be considered that the transfer control command is to modify the value of the PC register with conditions or conditions.
Transfer control command:
- Condition branches: ifeq, iflt, ifne, ifge, ifgt, ifnull, ifnonnull, if_icmpeq, if_icmpne, if_icmplt, limit, if_icmpge, if_cmpge, if_icmpeq, if_acmpne
- Compound condition branch: tableswitch, lookupswitch
- Unconditional branch: goto, goto_w, jsr, jsr_w, ret
Method call and return command
The invokevirtual command is used to call the method of the object Debon instance and distribute Objects Based on the object type (virtual method assignment), which is the most common method Assignment Method in Java.
The invokeinterface command is used to call an interface method. It searches for an object implementing this interface method at runtime to find a suitable method for calling.
The invokespecial command is used to call some instance methods that require special processing, including the instance initialization method, private method, and parent method.
The involvestatic command is used to call the class method (static method)
The invokedynamic command is used to dynamically parse the method referenced by the call point qualifier at run time and execute this method
The dispatch logic of the preceding four invocation commands is fixed in the Java virtual machine, and the dispatch logic of invokedynamic commands is determined by the user-defined guiding method.
Exception Handling Command
Exception catch statements are not implemented by bytecode commands in Java virtual machines, but are implemented by exception tables.
All throw operations (throw statements) explicitly throw exceptions in Java programs are implemented by athrow, except for explicitly throwing exceptions using throw statements, the Java virtual machine specification also specifies that many runtime exceptions are automatically thrown when other Java Virtual Machine commands detect exceptions.
Synchronous commands
Java Virtual Machine supports method-level synchronization and synchronization of a command sequence within a method. Both types of synchronization structures are supported by the management (Monitor.
Method-level synchronization is implicit, which neither requires byte code instructions to control, but also implements method calls and return operations.
The synchronous command sequence is usually represented by the synchronize statement block in Java. the Java VM instruction set includes the monitorenter and monitorexit commands to support synchronize keywords, to correctly implement the synchronized keyword, the Java compiler and Java virtual machine must work together.