Java bytecode Instruction Set Usage Details

Source: Internet
Author: User

A Java VM instruction is composed of an Opcode with a byte length that represents a specific meaning and a zero number that represents the operands of this operation parameter. In a virtual machine, many commands do not contain operands, but only one operation code. If an exception is ignored, the JVM interpreter can work effectively with the code. Copy codeThe Code is as follows: do {
Automatically calculate the PC register and retrieve the operation code from the position of the PC register
If (existing operand) retrieves the operand;
Execute the operation defined by the operation code;
} While (processing the next loop)

The number and length of operations depend on the operation code. If the length of an operation exceeds one byte, it is stored in the Big-Endian sequence (High Front bytecode ), the value must be (byte1 <8) | byte2.

The bytecode instruction stream is single-byte alignment, except for the "tableswitch" and "lookupswitch" commands. Their operands are special and are divided by 4 bytes, you need to reserve the corresponding space for alignment.

The length of the operation code of the Java Virtual Machine is limited to one byte, and the parameter length alignment of the compiled code is discarded to obtain the short and concise compilation code, even if the JVM implementation may be paid a certain performance cost. Because the operation code can only have one byte length, the number of instruction sets is limited. It is not assumed that the data is aligned, which means that when the data exceeds one byte, you have to re-create a specific data structure from the byte, which will result in loss of performance.
Data Type and Java Virtual Machine

In the JVM instruction set, most commands contain the data type information corresponding to their operations. For example, the iload command loads int-type data from the local variable table to the operand stack, while fload loads float-type data.

For most bytecode commands related to data types, their operation code mnemonic symbols have special characters to indicate: I represents the int type, l represents long, s represents short, and B Represents byte, c Indicates char, f indicates float, d Indicates double, and a indicates reference. Some separate commands can be used to convert unsupported types to supported types as necessary.
Load and store commands

The load and storage commands are used to transmit data back and forth between the local variable table of the stack frame and the operand stack.
1) commands for loading a local variable to the operand stack include: iload, iload _ <n>, lload, lload _ <n>, float, fload _ <n>, dload, dload _ <n>, aload, aload _ <n>.
2) commands for storing a value from the operand stack to a local variable: istore, istore _ <n>, lstore, lstore _ <n>, fstore, fstore _ <n>, dstore, dstore _ <n>, astore, astore _ <n>
3) commands for loading constants to the operand Stack: bipush, sipush, ldc, ldc_w, ldc2_w, aconst_null, iconst_ml, iconst _ <I>, lconst _ <l>, fconst _ <f>, dconst _ <d>
4) Access Index Command for the local variable table: wide
Some commands ending with Angle brackets represent a group of commands, such as iload _ <I>, which represent iload_0, iload_1, and so on. These commands are general commands with an operand.
Operation commands

The 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.
1) Addition command: iadd, ladd, fadd, dadd
2) subtraction commands: isub, lsub, fsub, and dsub
3) multiplication command: imul, lmul, fmul, dmul
4) division command: idiv, ldiv, fdiv, ddiv
5) remainder command: irem, lrem, frem, drem
6) reverse command: ineg, leng, fneg, dneg
7) Displacement command: ishl, ishr, iushr, lshl, lshr, lushr
8) bitwise OR command: ior, lor
9) bitwise AND command: iand, land
10) bitwise XOR or command: ixor, lxor
11) local variable auto-increment command: iinc
12) Comparison commands: dcmpg, dcmpl, fcmpg, fcmpl, and lcmp

The Java virtual machine does not explicitly specify Integer Data overflow. However, it specifies that when integer data is processed, an exception is thrown only when the division and remainder instruction have a division of 0.

Load and store commands

The load and storage commands are used to transmit data back and forth between the local variable table and the operand stack of the Oh you have passed the stack frame.

1) commands for loading a local variable to the operand stack include: iload, iload _ <n>, lload, lload _ <n>, float, fload _ <n>, dload, dload _ <n>, aload, aload _ <n>.
2) commands for storing a value from the operand stack to a local variable: istore, istore _ <n>, lstore, lstore _ <n>, fstore, fstore _ <n>, dstore, dstore _ <n>, astore, astore _ <n>
3) commands for loading constants to the operand Stack: bipush, sipush, ldc, ldc_w, ldc2_w, aconst_null, iconst_ml, iconst _ <I>, lconst _ <l>, fconst _ <f>, dconst _ <d>
4) Access Index Command for the local variable table: wide
Some commands ending with Angle brackets represent a group of commands, such as iload _ <I>, which represent iload_0, iload_1, and so on. These commands are general commands with an operand.
Operation commands

The 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.
1) Addition command: iadd, ladd, fadd, dadd
2) subtraction commands: isub, lsub, fsub, and dsub
3) multiplication command: imul, lmul, fmul, dmul
4) division command: idiv, ldiv, fdiv, ddiv
5) remainder command: irem, lrem, frem, drem
6) reverse command: ineg, leng, fneg, dneg
7) Displacement command: ishl, ishr, iushr, lshl, lshr, lushr
8) bitwise OR command: ior, lor
9) bitwise AND command: iand, land
10) bitwise XOR or command: ixor, lxor
11) local variable auto-increment command: iinc
12) Comparison commands: dcmpg, dcmpl, fcmpg, fcmpl, and lcmp

The Java virtual machine does not explicitly specify Integer Data overflow. However, it specifies that when integer data is processed, an exception is thrown only when the division and remainder instruction have a division of 0.
Type conversion command

The type conversion command converts the numeric types of the two Java virtual machines. These operations are generally used for explicit type conversion of user code.
JVM supports wide type conversion (small-range type conversion to large-range type conversion ):
1) int to long, float, and double
2) long to float and double
3) float to double type

Narrow-type conversion commands: i2b, i2c, i2s, l2i, f2i, f2l, d2l, and d2f. Narrow-type conversion may lead to different positive and negative numbers in the conversion results, different orders of magnitude, the conversion process may result in loss of numerical precision. For example, when int or long type is converted to integer type T, the conversion process only discards the unexpected content of N bytes of the second BIT (N is the data type length of type T)

Object creation and operation

Although the class instances and arrays are both objects, Java virtual machine uses different bytecode commands to create and operate class instances and arrays.
1) command for creating an instance: new
2) Instructions for creating Arrays: newarray, anewarray, and multianewarray
3) Access Field Command: getfield, putfield, getstatic, putstatic
4) Load array elements to the operand stack. commands: baload, caload, saload, iaload, laload, faload, daload, and aaload
5) store the value of the operand stack to the array element and execute: bastore, castore, castore, sastore, iastore, fastore, dastore, and aastore.
6) arraylength command
7) Check instance type commands: instanceof, checkcast
Operations stack management commands

Commands for directly operating the operand Stack: pop, pop2, dup, dup2, dup_x1, dup2_x1, dup_x2, dup2_x2, and swap
Transfer control instruction

Enable the JVM to execute programs conditionally or unconditionally from the specified instruction rather than the next instruction that controls the transfer instruction. Transfer control commands include:
1) condition branches: ifeq, iflt, ifle, ifne, ifgt, ifge, ifnull, ifnotnull, if_cmpeq, if_icmpne, if_icmlt, if_icmpgt, etc.

2) compound condition branch: tableswitch, lookupswitch

3) Unconditional branch: goto, goto_w, jsr, jsr_w, ret

The JVM has a special instruction set to process comparison operations between conditional branches of the int and reference types. In order to clearly identify whether an object value is null, a special instruction is provided to detect null values. Comparison between conditional branches of the boolean and byte types, char and short types is performed using the int type comparison command, while comparison between long, float, and double condition branches is performed, the operation command returns an integer value to the operand stack, and then performs the int-type conditional comparison operation to complete the entire branch jump. All types of comparisons are eventually converted to int type comparison operations.
Method call and return command

Invokevirtual command: Call the instance method of an object and assign the object based on the actual type (Vm allocation ).
Invokeinterface command: Call an interface method, search for an object that implements this interface method at runtime, and find a suitable method for calling.
Invokespecial: Call the instance method that requires special processing, including the instance initialization method, private method, and parent method.
Invokestatic: call class method (static)
Method return instructions are differentiated based on the type of returned values, including ireturn (return values are boolean, byte, char, short and int), lreturn, freturn, drturn and areturn, another return is used by the void method, instance initialization method, class and interface class initialization I method.

Synchronization

JVM supports method-level synchronization and a command sequence synchronization within a method. Both are implemented through moniter.

Method-level synchronization is implicit and does not need to be controlled by bytecode commands. It is implemented in method calls and return operations. The ACC_SYNCHRONIZED mark in the method label structure in the method constant pool of the VM identifies whether it is a synchronous method. When the method is called, The Call Command checks whether the flag is set. If it is set, the execution thread holds the moniter, then executes the method, and releases the moniter when the method is completed.

Synchronized is used to synchronize a sequence of instruction sets, which is usually identified by synchronized blocks. The JVM instruction sets include monitorenter and monitorexit to support synchronized semantics.

Structured locking is a situation where every monitor exit during a method call matches the previous monitor entry. JVM uses the following two rules to ensure the establishment of a structured lock (T represents a thread, and M represents a monitor ):

1) The number of times T holds M during method execution must be equal to the number of times T releases M at method completion.

2) at any time, T releases M more often than T holds M.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.