Use of Java byte Code instruction set detailed _java

Source: Internet
Author: User
Tags arithmetic array length arrays bitwise instance method
The Java Virtual Machine directive consists of a byte length, an opcode (Opcode) that represents a particular meaning, and the following 0 to many operands representing this action parameter. Many of the instructions in the virtual machine do not contain operands, only one opcode. If you ignore exceptions, the JVM interpreter can work effectively with the code.
Copy Code code as follows:

do{
Automatically compute the PC register and remove the opcode from the PC register location
If (there is an operand) fetch the operand;
Perform the operation defined by the opcode;
}while (process Next loop)

the number of operands and the length, depending on the opcode, if an operand is longer than one byte, will be stored in Big-endian order (high in the preceding byte code) and the value should be (byte1<<8) |byte2.

Byte code instruction flow is a single byte alignment, only the "Tableswitch" and "Lookupswitch" two instruction exception, their operands are more special, with 4 bytes as the boundary, need to set aside the corresponding vacancy to achieve alignment.

Limiting the length of the Java virtual machine opcode to one byte, and discarding the parameter length alignment of the compiled code, is to obtain a short, lean compilation code, even if it may cost a certain performance cost to the JVM implementation. Because the opcode can only have one byte length, it limits the number of instruction sets, and does not assume that the data is aligned, meaning that data more than one byte, you have to reconstruct from the byte of the specific data structure, loss of some performance.

Data types and Java virtual machines

In the instruction set in the JVM, most directives contain data type information corresponding to their operations. such as the iload instruction loads int data into the operand stack from a local variable table, and fload loads a float type of data.

For most byte-code directives related to data types, their opcode mnemonics have special characters that indicate: I represents the type int, l on behalf of the representative of the Long,s on behalf of the BYTE,C Representative Char,f Representative Float,d representative, double, A represents reference. There are individual directives that can be used when necessary to convert some types that are not unsupported to a type that can be supported.

Loading and storing directives

Load and store directives are used to transmit data back and forth between the local variable table and the operand stack of the stack frame.
1 The instructions to load a local variable into the operand stack include: Iload,iload_<n>,lload, lload_<n>, float, fload_<n>, dload, Dload_<n> Aload, aload_<n>.
2 The instruction of storing a numerical value from the operand stack to the local variable label: istore,istore_<n>,lstore,lstore_<n>,fstore,fstore_<n>,dstore,dstore_ <n>,astore,astore_<n>
3 The instructions to load the constants onto 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 instruction for local variable table: Wide
A part of an instruction that ends with an angle bracket represents a set of instructions, such as Iload_<i&gt, representing the iload_0,iload_1, which are generic instructions with one operand.

Operation Instruction

The arithmetic instruction is used to perform a specific operation on the values on the two operand stacks and to store the results back to the top of the Operation Stack.
1) Addition instruction: Iadd,ladd,fadd,dadd
2) Subtraction Instruction: Isub,lsub,fsub,dsub
3) Multiplication instruction: Imul,lmul,fmul,dmul
4 Division Instruction: Idiv,ldiv,fdiv,ddiv
5) The remainder instruction: Irem,lrem,frem,drem
6) Take counter instruction: Ineg,leng,fneg,dneg
7) Displacement instruction: Ishl,ishr,iushr,lshl,lshr,lushr
8) Bitwise OR instruction: Ior,lor
9) Bitwise AND INSTRUCTION: Iand,land
10) Bitwise XOR OR instruction: Ixor,lxor
11 local variable self-increasing instruction: Iinc
12) comparison instruction: DCMPG,DCMPL,FCMPG,FCMPL,LCMP

The Java Virtual machine does not explicitly specify an integer data overflow, but when processing integer data, only division and remainder directives with a divisor of 0 can cause the virtual machine to throw an exception.

Loading and storing directives

Load and store directives are used to send data back and forth between the local variables table and the operand stacks of the stack frame.

1 The instructions to load a local variable into the operand stack include: Iload,iload_<n>,lload, lload_<n>, float, fload_<n>, dload, Dload_<n> Aload, aload_<n>.
2 The instruction of storing a numerical value from the operand stack to the local variable label: istore,istore_<n>,lstore,lstore_<n>,fstore,fstore_<n>,dstore,dstore_ <n>,astore,astore_<n>
3 The instructions to load the constants onto 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 instruction for local variable table: Wide
A part of an instruction that ends with an angle bracket represents a set of instructions, such as Iload_<i&gt, representing the iload_0,iload_1, which are generic instructions with one operand.

Operation Instruction

The arithmetic instruction is used to perform a specific operation on the values on the two operand stacks and to store the results back to the top of the Operation Stack.
1) Addition instruction: Iadd,ladd,fadd,dadd
2) Subtraction Instruction: Isub,lsub,fsub,dsub
3) Multiplication instruction: Imul,lmul,fmul,dmul
4 Division Instruction: Idiv,ldiv,fdiv,ddiv
5) The remainder instruction: Irem,lrem,frem,drem
6) Take counter instruction: Ineg,leng,fneg,dneg
7) Displacement instruction: Ishl,ishr,iushr,lshl,lshr,lushr
8) Bitwise OR instruction: Ior,lor
9) Bitwise AND INSTRUCTION: Iand,land
10) Bitwise XOR OR instruction: Ixor,lxor
11 local variable self-increasing instruction: Iinc
12) comparison instruction: DCMPG,DCMPL,FCMPG,FCMPL,LCMP

The Java Virtual machine does not explicitly specify an integer data overflow, but when processing integer data, only division and remainder directives with a divisor of 0 can cause the virtual machine to throw an exception.

Type conversion directives

The type conversion instruction converts two Java virtual machine numeric types, which are typically used to implement explicit type conversion operations for user code.
JVM supports wide type conversion (small range type to large range type conversion):
1 int type to long,float,double type
2 long type to float,double type
3 float to double type

Narrow-Flower type conversion directives: I2B,I2C,I2S,L2I,F2I,F2L,D2L and d2f, narrowing type conversions can result in different positive and negative numbers, varying orders of magnitude, which may result in a loss of precision in the value of the conversion process. When an int or a long type converts an integer type T,The conversion process is simply discarding the least bit N bytes unexpected content (n is the type T data type length)

object creation and manipulation

Although class instances and arrays are objects, Java virtual machines use different byte code directives for the creation and operation of class instances and arrays.
1 The instructions to create the instance: new
2 to create an array of instructions: Newarray,anewarray,multianewarray
3 access field directive: Getfield,putfield,getstatic,putstatic
4 loading the array elements into the operand stack instruction: baload,caload,saload,iaload,laload,faload,daload,aaload
5 Store the operand stack's value in the array element execution: Bastore,castore,castore,sastore,iastore,fastore,dastore,aastore
6) Take the array length instruction: arraylength
7) Check instance type directive: Instanceof,checkcast

Operand Stack Management instruction

Direct operation operand stack instruction: pop,pop2,dup,dup2,dup_x1,dup2_x1,dup_x2,dup2_x2 and swap
Control transfer Instruction

Allow the JVM to continue executing the program conditionally or unconditionally from the specified instruction rather than the next instruction of the control transfer instruction. Control transfer instructions include:
1) Conditional branch: 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

There is a specialized instruction set in the JVM that handles conditional branching comparisons for int and reference types, so that there are specific instructions for detecting null values in order to be able to clearly indicate whether an entity value is null or not. Boolean and byte types, char types, and short type conditional branch comparison operations are done using comparison directives of type int, while long,float,double conditional branching comparisons are performed by the corresponding type of comparison operation instruction, The operation instruction returns an integer value into the operand stack, followed by a conditional comparison operation of type int to complete the branch jump. Each type of comparison is eventually converted to a comparison operation of type int.

Method calls and return directives

invokevirtual directive: Invokes an instance method of an object and assigns it according to the actual type of the object (virtual machine allocation).
Invokeinterface instruction: Invokes an interface method, searches for an object that implements this interface method at run time, and finds the appropriate method to invoke it.
Invokespecial: Invoke instance methods that require special handling, including instance initialization methods, private methods, and parent class methods
Invokestatic: Calling class method (static)
The method return instruction is distinguished by the type of the return value, including Ireturn (the return value is boolean,byte,char,short and int), Lreturn,freturn,drturn and Areturn, and the other returns the Void method , instance initialization methods, class and interface class initialization I methods are used.

Synchronous

The JVM supports method-level synchronization and synchronization of a sequence of instructions within a method, both of which are implemented through Moniter.

Method-level synchronization is implicit and does not need to be controlled by byte-code directives, which are implemented in method calls and return operations. The virtual machine distinguishes between the acc_synchronized flag in the method's standard structure in the method constant pool and whether it is a synchronous method. Method invocation, the calling instruction checks whether the flag is set, and if so, the execution thread holds the Moniter, executes the method, and then releases the Moniter when the method is finalized.

Synchronizing a sequence of instruction sets, usually marked by a synchronized block, where the JVM instructions have monitorenter and monitorexit to support synchronized semantics.

Structured locking is the case where each monitor exit during a method call matches the previous monitor entry. The JVM uses the following two rules to guarantee the formation of a knot-structured lock (t represents a thread, and M represents a monitor):

1 The number of m held by the method must be equal to the number of M that is released when the method completes

2 There will be no time when T-release m is more than T-holding m of the number of times
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.