With manual

In Android programming, if you want to read and modify bytecode, you need to parse and translate the Class file structure. The JVM defines the Class file structure and instruction set, and you can learn the content logic of the Class file by consulting the byte code table and instruction set. Below, all the table contents and instructions involved are sorted out for inquiry.

There are two main contents: Class file structure description table and bytecode instruction table

Table references from “Understanding the Java Virtual Machine in Depth: ADVANCED JVM Specific and Best Practices”

Class file structure description table

A Class file is a compiled Java file that describes the contents of a file definition in a jVM-defined structure. The main forms are:

  • Class file structure table
  • Often scale
  • Access tag table
  • Field in the table
  • Method table
  • Property sheet
  • List of special strings

Class file structure table

type The name of the describe The number of
U4 (4 bytes) magic Determines whether the file is a Class file that can be accepted by the virtual machine, similar to an ID 1
U2 (2 bytes) minot_version Second version number 1
U2 (2 bytes) mahor_version The major version number 1
U2 (2 bytes) constant_pool_count The constant pool capacity is calculated from 1. 0 indicates that no constant pool item is referenced 1
cp_info constant_pool Constant pool constant_pool_count-1
U2 (2 bytes) access_flags Access tokens 1
U2 (2 bytes) this_class Class index 1
U2 (2 bytes) super_class Index of the parent class 1
U2 (2 bytes) interfaces_count The number of interfaces implemented 1
U2 (4 bytes) interfaces The interface index interfaces_count
U2 (4 bytes) fields_count Number of fields 1
field_info fields Content of the field fields_count
U2 (2 bytes) methods_count Number of methods 1
method_info methods Method content methods_count
U2 (2 bytes) attributes_count Number of attributes 1
attribute_info attributes Attribute content attributes_count

Often scale

There are two main types of constant pools:

  • Literals, including text strings, final constant values, and so on
  • Symbol references, fully qualified names of classes and interfaces, field names and descriptors, and method names and descriptors

The Class file only holds the information of each method and field, not the memory information. The true memory entry can only be obtained through run-time transformations. When a VIRTUAL machine is running, it needs to obtain the symbolic reference from the constant pool, and then obtain the specific memory address through class creation or runtime parsing.

type substructure mark describe
CONSTANT_Utf8_info tag u1 = 1 The character string is utF-8 encoded
lenght u2 Number of bytes in a UTF-8 encoded string
bytes u1 The value is a utF-8 encoded string of lenght length
CONSTANT_Integer_info tag u1=3 Integer literals
bytes u4 Int values stored first in order of high order
CONSTANT_Float_info tag u1=4 Floating point literals
bytes u4 Float values stored first in order of magnitude
CONSTANT_Long_info tag u1=5 Long integer literals
bytes u8 The long value stored in front of the high order
CONSTANT_Double_info tag u1=6 A double – precision floating-point literal
bytes u8 A double stored in front of the high order
CONSTANT_Class_info tag u1=7 Symbolic reference to a class or interface
bytes u2 Index to a fully qualified named constant item
CONSTANT_String_info tag u1=8 String type literals
bytes u2 Index to a string literal
CONSTANT_Fieldref_info tag u1=9 Symbolic reference to a field
index u2 Index entry to the class or interface descriptor CONSTANT_Class_info that declares the field
index u2 Index entry pointing to the class or interface descriptor CONSTANT_NameAndType_info for the declared field
CONSTANT_Methodred_info tag u1=10 Symbolic references to methods in a class
index u2 Index entry to the class or interface descriptor CONSTANT_Class_info that declares the field
index u2 Index entry pointing to the class or interface descriptor CONSTANT_NameAndType_info for the declared field
CONSTANT_InterfaceMethodref_info tag u1=11 Symbolic references to methods in the interface
index u2 Index entry to the class or interface descriptor CONSTANT_Class_info that declares the field
index u2 Index entry pointing to the class or interface descriptor CONSTANT_NameAndType_info for the declared field
CONSTANT_NameAndType_info tag u1=12 A partial symbolic reference to a field or method
index u2 Index to the constant entry of the field or method name
index u2 Index to the constant entry of the field or method name
CONSTANT_MethodHandle_info tag u1=15 Represents a method handle
reference_kind u1 The value must be in [1,9], which determines the type of method handle. The value of the method handle type represents the bytecode behavior of the method handle
reference_index u2 The value must be a valid index to the constant pool
CONSTANT_MethodType_info tag u1=16 Identification method type
descriptor_index u2 The value must be a valid index to the constant pool, and the constant pool entry at that index must be a CONSTANT_Utf8_info structure representing the descriptor of the method
CONSTANT_InvokeDynamic_info tag u1=18 Represents a dynamic method call point
bootstrap_method_attar_index u2 The value must be a valid index to the bootSTRAP_methods [] array of the bootstrap method table in the current Class file
name_and_type_index u2 The value must be a valid index to the current constant pool, and the constant pool value at that index must be a CONSTANT_NameAndType_info structure representing the method name and method descriptor

Access tag table

The access tag table is further divided according to the following different tag types:

  1. Class access flag
  2. The inner class accesses the label
  3. Field access flag
  4. Method access flag

Class access flag

Access information that identifies some Class or interface level, including whether the Class file is a Class or interface, is defined as a public type, is defined as an abstract Class type, and, if so, is declared final.

Sign the name Flag values describe
ACC_PUBLIC 0x0001 Whether the type is public
ACC_FINAL 0x0010 Whether or not to be declared final can be set only by the class
ACC_SUPER 0x0020 Whether the invokespecial bytecode instruction is allowed to use the new semantics. The invokespecial instruction has changed in JDK1.0.2. In order to distinguish the semantics used in this instruction, all classes compiled after JDK1.0.2 must be true
ACC_INTERFACE 0x0200 Identify this as an interface
ACC_ABSTRACT 0x0400 The value of this flag is true for interfaces or abstract classes and false for other types
ACC_SYNTHETIC 0x1000 Identifies that this class is not generated by user code
ACC_ANNOTATION 0x2000 Mark this as an annotation
ACC_ENUM 0x4000 Identifies this as an enumeration

Inner classes access the table

Sign the name Flag values describe
ACC_PUBLIC 0x0001 Whether the inner class is public
ACC_PRIVATE 0x0002 Whether the inner class is private
ACC_PROTECTED 0x0004 Whether the inner class is protected
ACC_STATIC 0x0008 Whether the inner class is protected
ACC_FINAL 0x0010 Whether the inner class is protected
ACC_INTERFACE 0x0020 Whether the inner class is an interface
ACC_ABSTRACT 0x0400 Whether the inner class is abstract
ACC_SYNTHETIC 0x1000 Whether an inner class is not generated by user code
ACC_ANNOTATION 0x2000 Whether the inner class is an annotation
ACC_ENUM 0x4000 Whether the inner class is an enumeration

Field access flag

Sign the name Flag values describe
ACC_PUBLIC 0x0001 Whether the field is public
ACC_PRIVATE 0x0002 Whether the field is private
ACC_PROTECTED 0x0004 Whether the field is protected
ACC_STATIC 0x0008 Whether the field is static
ACC_FINAL 0x0010 Whether the field is final
ACC_VOLATILE 0x0040 Whether the field is volatile
ACC_TRANSIENT 0x0080 Whether the field is TRANSIENT
ACC_SYNTHETIC 0x1000 Whether the field is automatically generated by the compiler
ACC_ENUM 0x4000 Whether the field is enum

Method access flag

Sign the name Flag values describe
ACC_PUBLIC 0x0001 Whether the method is public
ACC_PRIVATE 0x0002 Whether the method is private
ACC_PROTECTED 0x0004 Whether the method is protected
ACC_STATIC 0x0008 Whether the method is static
ACC_FINAL 0x0010 Whether the method is final
ACC_SYNCHRONIZED 0x0020 Whether the method is synchronized
ACC_BRIDGE 0x0040 Method is the bridge method generated by the compiler
ACC_VARARGS 0x0080 Whether a method accepts an indefinite parameter
ACC_NATIVE 0x0100 Whether the method is native
ACC_ABSTRACT 0x0400 Whether the method is abstract
ACC_STRICTFP 0x0800 Whether the method is strictFP
ACC_SYNTHETIC 0x1000 Whether a method is generated automatically by the compiler

Field in the table

Used to describe variables declared in interfaces and classes, including class-level and instance-level variables.

type The name of the The number of
u2 access_flags 1
u2 name_index 1
u2 descriptor_index 1
u2 attributes_count 1
u2 attributes attributes_count
Among themaccess_flagsSee field access flags for Access flags table.

Method table

The method table contains access flags, name indexes, descriptor indexes, and property tables

type The name of the The number of
u2 access_flags 1
u2 name_index 1
u2 descriptor_index 1
u2 attributes_count 1
attribute_info attributes attributes_count
Methodologicalaccess_flagsSee method access flags above

Property sheet

Property sheets are tables that explain properties carried in Class files, field tables, and method tables. Properties are used to describe information specific to certain scenarios.

The attribute name Use location meaning
Code Method table Bytecode instructions compiled into Java code
ConstantValue Field in the table Constant value defined by the final keyword
Deprecated Class, method table, field table Constant value defined by the final keyword
Exceptions Method table An exception thrown by the final method
EnclosingMethod The class file This property is only available if a class is local or anonymous, and is used to identify the enclosing method of the class
InnerClasses The class file Inner class list
LineNumberTable Code attributes The mapping of Java source line numbers to bytecode instructions
LocalVariableTable Code attributes Method local variable description
StackMapTable Code attributes New property in JDK1.6 for the new Type Checker to check and process whether the local variable of the target method matches the Type required by the operand stack lock
Signature Class, method table, field table In the Java language, the generic signature of any class, interface, initializer, or member that contains Type Variables or Parameterized Types. The Signature attribute records generic Signature information for it. Because Java generics are implemented by erasure, this property is needed to record relevant information in the generics in order for the type information to be erased to cause confusion in the signature
SourceFile The class file Record the source file name
SourceDebugExtension The class file A new property in JDK1.6, the SourceDebugExtension property is used to store additional debugging information. For example, when debugging JSP files, the Java stack cannot be used to locate the line number of a JSP file. The JSR-45 specification provides a standard mechanism for debugging programs that are not written in Java but need to be compiled into bytecode and run in the Java virtual machine. The SourceDebugExtension property can be used to store debugging information added to the standard
Synthetic Class, method table, field table Identifies whether a method or field is automatically generated by the compiler
LocalVariableTypeTable class New properties in JDK1.5, which use signature signatures instead of descriptors, were added to describe generic parameterized types after the introduction of generic syntax
RuntimevisibleAnnotations Class, method table, field table New properties in JDK1.5 to provide support for dynamic annotations. RuntimevisibleAnnotations property is used to specify which annotations are runtime (actually the runtime is reflection calls)
RuntimeInvisibleAnnotations Class, method table, field table JDK1.5 new attributes, in contrary to RuntimevisibleAnnotations attribute function, used to indicate which notes are not visible at runtime
RuntimeVisibleParameterAnnotations Method table New attribute, role in JDK1.5 like RuntimevisibleAnnotations attribute, but function object for method parameters
RuntimeInvisibleParameterAnnotations Method table New attribute, role in JDK1.5 like RuntimeInvisibleAnnotations attribute, but function object for method parameters
AnnotationDetault Method table Attribute added in JDK1.5 to record default values for annotation class elements
BootstrapMethods The class file Property added in JDK1.5 to hold bootstrap method qualifiers for invokeDynamic directive references

Each of the above attributes needs to be identified by referencing a constant of type CONSTANT_Utf8_info from the constant pool. It also contains attribute_length (u4), which identifies the number of digits occupied by the attribute value, followed by the attribute content.

Code attribute structure

type The name of the The number of
u2 attribute_name_index 1
u4 attribute_length 1
u2 max_stack 1
u2 max_locals 1
u4 code_length 1
u1 code code_lenght
u2 exception_table_lenght 1
exception_info exception_table exception_table_length
u2 attributes_count 1
attribute_info attributes attributes_count

Exception attribute structure

type The name of the The number of
u2 start_pc 1
u2 end_pc 1
u2 handler_pc 1
u2 catch_type 1

Exceptions attribute structure

This table is different from the exception table, which is the checked exceptions that may be thrown by methods in the listing, that is, the exceptions listed by the keyword when method description

type The name of the The number of
u2 attribute_name_index 1
u4 attribute_length 1
u2 number_of_exceptions 1
u2 exception_index_table number_of_exceptions

LineNumberTable Property structure

Used to describe the mapping between Java source line numbers and bytecode line numbers, generated by default in a Class file.

type The name of the The number of
u2 attribute_name_index 1
u4 attribute_length 1
u2 line_number_table_length 1
line_number_info line_number_table line_number_table_length
Line_number_info contains two u2-type data items, start_PC and line_number.

LocalVariableTable property structure

Used to describe the relationship between variables in the local variable table in the stack frame and variables defined in the Java source code, generated by default to a Class file.

type The name of the The number of
u2 attribute_name_index 1
u4 attribute_length 1
u2 local_variable_table_lenght 1
local_variable_info local_variable_table local_variable_table_lenght

Where local_variable_info represents the association between stack frames and local variables in source code, as shown in the following table:

type The name of the meaning The number of
u2 start_pc The bytecode offset at the start of the life cycle of a local variable 1
u2 length The life cycle of a local variable begins with scope coverage length 1
u2 name_index Points to the constant pool index CONSTANT_Utf8_info 1
u2 descriptor_index Points to the constant pool index CONSTANT_Utf8_info 1
u2 index The Slot position of a local variable in the stack frame local variable table 1

SourceFile attribute structure

Record the name of the source file that generated the Class file.

type The name of the The number of
u2 attribute_name_index 1
u4 attribute_length 1
u2 sourcefile_index 1

Sourcefile_index is the index pointing to the constant pool CONSTANT_Utf8_info.

ConstantValue attribute structure

Used to tell the virtual machine to automatically assign values to static variables. Only variables decorated with the static keyword can use this property.

type The name of the The number of
u2 attribute_name_index 1
u4 attribute_length 1
u2 constant_index 1

InnerClasses property structure

Used to record associations between inner classes and host classes. If an inner class is defined in a class, the compiler generates the InnerClasses attribute for it.

type The name of the The number of
u2 attribute_name_index 1
u4 attribute_length 1
u2 number_of_classes 1
inner_classes_info inner_classes number_of_classes

Each inner_classes_info represents an inner class, structured as follows:

type The name of the meaning The number of
u2 inner_class_info_index Points to the constant pool CONSTANT_Class_info index 1
u2 outer_class_info_index Points to the constant pool CONSTANT_Class_info index 1
u2 inner_name_index Points to the constant pool CONSTANT_Utf8_info index, which represents the name of this inner class, or 0 if anonymous 1
u2 inner_class_access_flags For access flags for inner classes, see the access flags section above 1

Deprecated/Synthetic attribute structure

The former is used to indicate whether a class, field, or method is no longer recommended.

The latter is used to indicate that a field or method is not directly generated by Java source code. All methods generated by non-user code require the Synthetic property or ACC_SYNTHETIC flag to be set, except for

and

. Their structure is as follows:

type The name of the The number of
u2 attribute_name_index 1
u4 attribute_length 1

StackMapTable attribute structure

Added to the Class file specification after JDK1.6 in the Code property table, this property is used by the Type Checker during the bytecode verification phase of the virtual machine Class load.

type The name of the The number of
u2 attribute_name_index 1
u4 attribute_length 1
u2 number_of_entries 1
stack_map_frame stack_map_frame_entries number_of_entries

Signature attribute structure

Added to the Class file specification after the JDK1.5 release, it is an optional fixed-length property that can appear in the property tables of classes, property tables, and method table structures. This property records the signature information of generics, which in the Java language are pseudo-generics implemented by erasure. In bytecode properties, the generics information is erased after compilation. This property was added to compensate for the inability of runtimes like C# to support retrieving true generic types, and Java reflection can now retrieve generic types.

type The name of the The number of
u2 attribute_name_index 1
u4 attribute_length 1
u2 signature_index 1

Signature_index must be a valid index to the constant pool and be CONSTANT_Utf8_info, indicating the class signature, method type signature, or field type signature. This structure represents the class Signature if the current Signature attribute is an attribute of the class file, the method type Signature if the current Signature attribute is an attribute of the method table, and the field type Signature if the current Signature attribute is an attribute of the field table.

BootstrapMethods attribute structure

Added to the Class file specification after JDK1.7 was released, is a complex variant-length property that resides in the property sheet of the Class file.

type The name of the The number of
u2 attribute_name_index 1
u4 attribute_length 1
u2 num_bootstrap_methods 1
bootstrap_method bootstrap_methods num_bootstrap_methods

The bootSTRap_method structure is as follows

type The name of the The number of
u2 bootstrap_method_ref 1
u2 num_bootstrap_arguments 1
u2 bootstrap_arguments num_bootstrap_arguments

List of special strings

A fully qualified name is a class name separated by a “.”. For example, com/yummylau/TestClass replaces “.” with “/” to become com.yummylau.TestClass. Segmentation. Simple names are methods or field names without type and parameter modifications, such as method inc() and field M, identified as inc and m, respectively. Special string tables contain descriptions of some basic types and method descriptions. As follows:

  • The descriptor
Identification character meaning
B Basic type byte
C Base type char
D Base type double
F Base type float
I Basic int
J Base type long
S Basic type short
Z Basic type Boolean
V Base type void
L Object types, such as Ljava/lang/Object

For arrays, each dimension is described with a leading “[” character, such as defining a “java.lang.string [][]” array, which is recorded as “[[java.lang.string; “and an integer array” int[] “, which is recorded as [I for methods

Methods the scene The descriptor
Void inc () (V)
Java. Lang. String toString () () Ljava/lang/String;
Int indexOf (char[]source, int sourceOffest, int sourceCount, char[] target, int targetOffset, int targetCOunt, int formIndex) ([CII [CIII) I

Table of bytecode instructions

According to the type of instruction/target data type/common instruction, it can be further divided into the following contents:

  • Summary table of bytecode instructions
  • Conversion of data types in instructions
  • The data type supported by the instruction set
  • Load/store instructions
  • Operation instruction
  • Type conversion instruction
  • Object creation and access directives
  • Operand stack management instructions
  • Control transfer instruction
  • Method calls and return directives
  • Exception handling instruction
  • Synchronization instructions

Summary table of bytecode instructions

Tip: Refer to the official JVM directive documentation for a more detailed description

The bytecode mnemonics Instruction meaning
0x00 nop Do nothing
0x01 aconst_null Push NULL to the top of the stack
0x02 iconst_m1 Push int -1 to the top of the stack
0x03 iconst_0 Push int 0 to the top of the stack
0x04 iconst_1 Push int 1 to the top of the stack
0x05 iconst_2 Push int 2 to the top of the stack
0x06 iconst_3 Push int 3 to the top of the stack
0x07 iconst_4 Push int 4 to the top of the stack
0x08 iconst_5 Push int 5 to the top of the stack
0x09 lconst_0 Push long 0 to the top of the stack
0x0a lconst_1 Push long 1 to the top of the stack
0x0b fconst_0 Push type float 0 to the top of the stack
0x0c fconst_1 Push type float 1 to the top of the stack
0x0d fconst_2 Push type float 2 to the top of the stack
0x0e dconst_0 Push double 0 to the top of the stack
0x0f dconst_1 Push double 1 to the top of the stack
0x10 bipush Push a one-byte constant (-128-127) to the top of the stack
0x11 sipush Push a short integer constant constant (-32768-32767) to the top of the stack
0x12 ldc Pushes int, float, String constants from the constant pool to the top of the stack
0x13 ldc_w Push int, float, String constant values from the constant pool to the top of the stack (wide index)
0x14 ldc2_w Push long or float constant values from the constant pool to the top of the stack (wide index)
0x15 iload Pushes the specified local variable of type int to the top of the stack
0x16 lload Pushes the specified long local variable to the top of the stack
0x17 fload Pushes the specified local variable of type float to the top of the stack
0x18 dload Pushes the specified dload local variable to the top of the stack
0x19 aload Pushes the specified reference type local variable to the top of the stack
0x1a iload_0 Push the first local variable of int to the top of the stack
0x1b iload_1 Push the second local variable of int to the top of the stack
0x1c iload_2 Push the third local variable of type int to the top of the stack
0x1d iload_3 Push the fourth local variable of int to the top of the stack
0x1e lload_0 Push the first long local variable to the top of the stack
0x1f lload_1 Push the second long local variable to the top of the stack
0x20 lload_2 Push the third long local variable to the top of the stack
0x21 lload_3 Push the fourth long local variable to the top of the stack
0x22 fload_0 Push the first local variable of type float to the top of the stack
0x23 fload_1 Push the second local variable of type float to the top of the stack
0x24 fload_2 Push the third local variable of type float to the top of the stack
0x25 fload_3 Push the fourth local variable of type float to the top of the stack
0x26 dload_0 Push the first double local variable to the top of the stack
0x27 dload_1 Push the second double local variable to the top of the stack
0x28 dload_2 Push the third double local variable to the top of the stack
0x29 dload_3 Push the fourth local variable of type double to the top of the stack
0x2a aload_0 Push the first reference type local variable to the top of the stack
0x2b aload_1 Push the second reference type local variable to the top of the stack
0x2c aload_2 Push the third reference type local variable to the top of the stack
0x2d aload_3 Push the fourth reference type local variable to the top of the stack
0x2e iaload Pushes the value of the specified index of an int to the top of the stack
0x2f laload Pushes the value of the specified index of the long array to the top of the stack
0x30 faload Pushes the value of the specified index of a float array to the top of the stack
0x31 daload Pushes the value of the specified index of an array double to the top of the stack
0x32 aaload Pushes the value of the specified index of a reference array to the top of the stack
0x33 baload Pushes the value of a Boolean or Byte array specified index to the top of the stack
0x34 caload Pushes the value of a char array to the top of the stack
0x35 saload Pushes the value of the specified index of the short array to the top of the stack
0x36 istore Stores the top int value to the specified local variable
0x37 lstore Stores the top of the stack long value to the specified local variable
0x38 fstore Stores the top stack float value to the specified local variable
0x39 dstore Stores the top of the stack double to the specified local variable
0x3a astore Stores the top of the stack reference value to the specified local variable
0x3b istore_0 Store the top int value to the first local variable
0x3c istore_1 Store the top int value to the second local variable
0x3d istore_2 Store the top int value into the third local variable
0x3e istore_3 Store the top int value into the fourth local variable
0x3f lstore_0 Store the top of the stack value into the first local variable
0x40 lstore_1 Store the top of the stack long value into the second local variable
0x41 lstore_2 Store the top of the stack long value into the third local variable
0x42 lstore_3 Store the top of the stack long value into the fourth local variable
0x43 fstore_0 Store the top stack float value to the first local variable
0x44 fstore_1 Store the top stack float value into the second local variable
0x45 fstore_2 Store the top stack float value into the third local variable
0x46 fstore_3 Store the top stack float value into the fourth local variable
0x47 dstore_0 Store the top of the stack double to the first local variable
0x48 dstore_1 Store the top of the stack double to the second local variable
0x49 dstore_2 Store the top of the stack double into the third local variable
0x4a dstore_3 Store the top of the stack double into the fourth local variable
0x4b astore_0 Store the top of the stack reference value into the first local variable
0x4c astore_1 Store the top of the stack reference value into the second local variable
0x4d astore_2 Store the top of the stack reference value into a third local variable
0x4e astore_3 Store the top of the stack reference value into the fourth local variable
0x4f iastore Stores the top int into the specified index position of the specified array
0x50 lastore Stores the top long value to the specified index position in the specified array
0x51 fastore Stores the top float value to the specified index position of the specified array
0x52 dastore Stores the top double to the specified index position in the specified array
0x53 aastore Stores the top of the stack reference value to the specified index position of the specified array
0x54 bastore Stores the top stack Boolean or Byte value to the specified index position of the specified array
0x55 castore Stores the top char value to the specified index position in the specified array
0x56 sastore Stores the top short value to the specified index position in the specified array
0x57 pop Pop top value (value cannot be long or double)
0x58 pop_2 Pops one (for long or double) or two values (for other types that are not long or double) at the top of the stack
0x59 dup Copies the top value of the stack and pushes it to the top
0x5a dup_x1 Copies the top value of the stack and pushes the two replicated values to the top
0x5b dup_x2 Copy the top value and push three (or two) copy values to the top of the stack
0x5c dup_2 Copy one (for long or double) or two (for other types other than long or double) values to the top of the stack and push the copy value to the top of the stack.
0x5d dup_2_x1 Double version of the dup_x1 instruction
0x5e dup_2_x2 Double version of the dup_x2 instruction
0x5f swap Swap the two values at the top of the stack (values cannot be long or double)
0x60 iadd Add two int values at the top of the stack and push the result to the top
0x61 ladd Add the top two longs and push the result to the top of the stack
0x62 fadd Add two float values at the top of the stack and push the result to the top
0x63 dadd Add the top two double values and push the result to the top of the stack
0x64 isub Subtract the top two int values and push the result to the top of the stack
0x65 lsub Subtract the top two longs and push the result to the top of the stack
0x66 fsub Subtract the top two float values and push the result to the top of the stack
0x67 dsub Subtract the top double and push the result to the top of the stack
0x68 imul Multiply the top two int values and push the result to the top of the stack
0x69 lmul Multiply the top two longs and push the result to the top of the stack
0x6a fmul Multiply the top two float values and push the result to the top of the stack
0x6b dmul Multiply the top two double values and push the result to the top of the stack
0x6c idiv Divide the top two int values and push the result to the top of the stack
0x6d ldiv Divide the top two longs and push the result to the top of the stack
0x6e fdiv Divide two float values at the top of the stack and push the result to the top
0x6f ddiv Divide the top double and push the result to the top of the stack
0x70 irem The two int values at the top of the stack are modelled and the results are pushed to the top of the stack
0x71 lrem The two long values at the top of the stack are used for modular operation and the result is pushed to the top of the stack
0x72 frem Modulo the top two float values and push the result to the top of the stack
0x73 drem Modulo operation is performed on the top two double values and the result is pushed to the top of the stack
0x74 ineg Negates the top two ints and pushes the result to the top of the stack
0x75 lneg Negate the top two longs and push the result to the top of the stack
0x76 fneg Negates the top two float values and pushes the result to the top of the stack
0x77 dneg Negates the top double and pushes the result to the top
0x78 ishl Shifts the top two ints to the left by the specified number of digits and pushes the result to the top of the stack
0x79 lshl Shifts the top two longs to the left by the specified number of digits and pushes the result to the top of the stack
0x7a ishr Shifts (signed) the specified number of digits right of the top two int values and pushes the result to the top of the stack
0x7b lshr Shifts the top two longs right (signed) by the specified number of digits and pushes the result to the top of the stack
0x7c iushr Shifts the top two int values to the right (unsigned) by the specified number of digits and pushes the result to the top of the stack
0x7d lushr Shifts the top two longs right (unsigned) by the specified number of digits and pushes the result to the top of the stack
0x7e iand The top two int values are “bitwise and” and pushed to the top of the stack
0x7f land The two longs at the top of the stack are bitwise and pushed to the top of the stack
0x80 ior Place the top two ints as bitwise or and push the result to the top of the stack
0x81 lor The top two longs are “bitwise or” and pushed to the top of the stack
0x82 ixor The top two int values are bitwise xor and pushed to the top of the stack
0x83 lxor The top two longs are bitwise xor and the result is pushed to the top of the stack
0x84 iinc Increments a top int variable to the specified value (I ++, I –, I +=2, etc.)
0x85 i2l Forces the top int value to a long value and pushes the result to the top of the stack
0x86 i2f Forces the top int value to a float value and pushes the result to the top of the stack
0x87 i2d Forces the top int to a double and pushes the result to the top of the stack
0x88 l2i Forces the top long value to an int value and pushes the result to the top of the stack
0x89 l2f Forces the top of the stack long value to float and pushes the result to the top of the stack
0x8a l2d Forces the top long into a double and pushes the result to the top of the stack
0x8b f2i Force a top float value to an int value and push the result to the top of the stack
0x8c f2l Force a top float value to a long value and push the result to the top of the stack
0x8d f2d Forces a top float value to a double value and pushes the result to the top of the stack
0x8e d2i Forces the top double to an int and pushes the result to the top of the stack
0x8f d2l Forces the top double to a long and pushes the result to the top of the stack
0x90 d2f Forces the top double to float and pushes the result to the top of the stack
0x91 i2b Forces the top int value to a byte value and pushes the result to the top of the stack
0x92 i2c Forces the top int value to a char value and pushes the result to the top of the stack
0x93 i2s Forces the top int to a short value and pushes the result to the top of the stack
0x94 lcmp Compare the two longs at the top of the stack and push the result (1, 0, or -1) to the top of the stack
0x95 fcmpl Compare the size of two float values at the top of the stack and push the result (1, 0, or -1) to the top of the stack; When one of the values is “NaN”, -1 is pushed to the top of the stack
0x96 fcmpg Compare the size of two float values at the top of the stack and push the result (1, 0, or -1) to the top of the stack; When one of the values is “NaN”, 1 is pushed to the top of the stack
0x97 dcmpl Compare two double values at the top of the stack and push the result (1, 0, or -1) to the top of the stack; When one of the values is “NaN”, -1 is pushed to the top of the stack
0x98 dcmpg Compare two double values at the top of the stack and push the result (1, 0, or -1) to the top of the stack; When one of the values is “NaN”, 1 is pushed to the top of the stack
0x99 ifeg Jump if the top value of int equals 0
0x9a ifne Jump if the top value of int does not equal 0
0x9b iflt Jump if the top value of int is less than 0
0x9c ifge Jump if the top value of int is greater than or equal to 0
0x9d ifgt Jump if the top value of int is greater than 0
0x9e ifle Jump if the top value of int is less than or equal to 0
0x9f if_icmpeq Compares the size of two ints at the top of the stack, jumps when the result equals 0
0xa0 if_icmpne Compares the size of two ints at the top of the stack, jumps if the result is not equal to 0
0xa1 if_icmplt Compares the size of two ints at the top of the stack, jumps if the result is less than 0
0xa2 if_icmpge Compares the size of two ints at the top of the stack, jumps if the result is greater than or equal to 0
0xa3 if_icmpgt Compares the size of two ints at the top of the stack, jumps if the result is greater than 0
0xa4 if_icmple Compares the size of two ints at the top of the stack, jumps if the result is less than or equal to 0
0xa5 if_icmpeq Compares two referential values at the top of the stack, jumps if the results are equal
0xa6 if_icmpnc Compares two referential values at the top of the stack, jumps if the results are not equal
0xa7 goto Unconditional jump
0xa8 jsr Jumps to the specified 16-bit offset and pushes the address of the next JSR instruction to the top of the stack
0xa9 ret Returns the instruction location to the index specified by the local variable (usually used in conjunction with JSR or jSR_w)
0xaa tableswitch For switch conditional jump, case value continuous (variable length instruction)
0xab lookupswitch For switch conditional jump, case value not continuous (variable length instruction)
0xac ireturn Returns an int from the current method
0xad lreturn Returns long from the current method
0xae freturn Returns float from the current method
0xaf dreturn Returns double from the current method
0xb0 areturn Returns an object reference from the current method
0xb1 return Returns void from the current method
0xb2 getstatic Gets the static field of the specified class and pushes its value to the top of the stack
0xb3 putstatic Assigns a value to the static field of the specified class
0xb4 getfield Gets the instance domain of the specified class and pushes its value to the top of the stack
0xb5 putfield Assigns a value to the instance field of the specified class
0xb6 invokevirtual Calling instance methods
0xb7 invokespecial Call superclass constructor, instance initializer, private method
0xb8 invokestatic Calling static methods
0xb9 invokeinterface Calling interface methods
0xba invokedynamic Calling dynamic methods
0xbb new Creates an object and pushes its reference to the top of the stack
0xbc newarray Creates an array of the specified primitive type (such as int, float, etc.) and pushes its reference value to the top of the stack
0xbd anewarray Creates an array of references (such as classes, interfaces, arrays) and pushes its reference values to the top of the stack
0xbe arraylength Get the length of the array and push it to the top of the stack
0xbf athrow Throws the exception at the top of the stack
0xc0 checkcast Check for type conversions, failing which throws a ClassCastException
0xc1 instanceof Checks if the object is an instance of the specified class, if so, 1 is pushed to the top of the stack, otherwise 0 is pushed to the top
0xc2 monitorenter Gets the lock of an object that is used to synchronize a method or block
0xc3 monitorexit Release the lock of an object for synchronizing methods or blocks
0xc4 wide Expand the width of a local variable
0xc5 multianewarray Creates a multidimensional array of the specified type and the specified dimension (to execute this instruction, the stack must contain the length values for each dimension) and pushes its reference values to the top of the stack
0xc6 ifnull If the value is null, it jumps
0xc7 ifnonnull The value is not null
0xc8 goto_w Unconditional jump (wide index)
0xc9 jsr_w Jumps to the specified 32-bit offset position and pushes the next instruction address of jSR_W to the top of the stack

Conversion of data types in instructions

The data type byte short int long float double char reference
Simplified conversion b s i l f d c a

The data type supported by the instruction set

The T+ instruction in the following table constitutes Opcode, and T is the simplified transformation of each data type in the above table.

opcode byte short int long float double char reference
Tipush bipush sipush
Tconst iconst lconst fconst dconst aconst
Tload iload lload fload dload aload
Tstore istore lstore fstore dstore astore
Tinc iinc
Taload baload saload iaload laload faload daload caload aaload
Tastore bastore sastore iastore lastore fastore dastore castore aastore
Tadd iadd ladd fadd dadd
Tsub isub lsub fsub dsub
Tmul imul lmul fmul dmul
Tdiv idiv ldiv fdiv ddiv
Trem irem lrem frem drem
Tneg ineg lneg fneg dneg
Tshl ishl lshl
Tshr ishr lshr
Tushr iushr lushr
Tand iand land
Tor ior lor
Txor ixor lxor
i2T i2b i2s i2l i2f i2d
l2T l2i l2f l2d
f2T f2i f2l f2d
d2T d2i d2l d2f
Tcmp lcmp
Tcml fcml dcml
Tcmpg fcmpg dcmpg
if_TcmpOP if_icmpOP if_acopOP
Treturn ireturn lreturn freturn dreturn areturn

Most instructions do not support byte, char, short or even Boolean, and the compiler extends these types of data to int at compiler or runtime.

Load/store instructions

Load/store instructions are used to transfer data back and forth between the local variable table and operand stack in a stack frame.

  • Load a local variable onto the stack:TloadTload_nThe latter means a set of instructions.
  • To store a value from the operand stack to a local variable table:TstoreTstore_nThe latter means a set of instructions.
  • Load a constant onto the operand stack:TipushldcT_const
  • Extended local variable table access index instruction:wide

Operation instruction

Perform an operation on the operand stack and store the result back on top of the operand stack.

  • Add instructionTadd
  • Subtraction instructionsTsub
  • Multiplication instructionsTmul
  • Division instructionsTdiv
  • For more instructionTrem
  • Take the instructionTneg
  • Displacement instructionsTshl.Tshr.Tushr
  • By bit or instructionTor
  • By bit and instructionTand
  • Bitwise xOR instructionTxor
  • Local variable increment instructionTinc
  • More instructionsTcmpg ,Tcmpl

Type conversion instruction

Conversion instructions are used to convert two different numeric types to each other. Such conversion operations are typically used to implement explicit conversion operations in user code, or to deal with the problem of data type related instructions in the bytecode instruction set that do not have a one-to-one mapping to the data type.

  • Int type to otheri2T
  • Type long to otherl2T
  • Float type to otherf2T
  • Type double to otherd2T

Object creation and access directives

Although class instances and arrays are both objects, the Java virtual machine uses different bytecode instructions to create and manipulate class instances and arrays.

  • Creating a class instancenew
  • Create an arraynewarray.anewarray.multianewarray
  • Access class variables and instance variablesgetfield.putfield.getstatic.putstatic
  • Load an array element onto the operand stackTaload
  • Stores the values of an operand stack into an array elementTastore
  • The instruction to take the length of an arrayarraylength
  • Check the class instance typeinstanceof.checkcast

Operand stack management instructions

  • Push one or two elements off the top of the operand stackpop.pop2
  • Copies one or two values from the top of the stack and pushes them back to the topdup.dup2.dup_x1.dup2_x1.dup_x2.dup2_x2
  • Swap the top two values on the stackswap

Control transfer instruction

Allow the virtual machine to execute programs conditionally or unconditionally from a specific location rather than at the next instruction that controls the transfer instruction.

  • Conditional branchifeq.ifit.ifle.ifne.ifgt.ifge.ifull.ifnonnull.if_icmpeq.if_icmpne.if_icmplt.if_icmpgt.if_icmple.if_icmpge.if_acmpeq.if_acmpne
  • Compound conditional branchtableswitch.lookupswitch
  • Unconditional branchgoto.goto_w.jsr.jsr_w.ret

Method calls and return directives

  • Invokes the instance method of the objectinvokevirtualAccording to the actual type of the object
  • Calling interface methodsinvokeinterface, will search at runtime for an object that implements a method of this interface and find the appropriate method to call
  • Call some instance methods that require special handlinginvokespecialIncluding instance initializer methods, private methods, and superclass methods
  • Calling class methodsinvokestaticUsed to call static methods
  • The runtime dynamically parses the method referenced by the call point qualifier at call point and executes itinvokedynamicUnlike the previous four instructions, which are hardwired into the JVM, the dispatch logic for this instruction is determined by the boot method set by the user.

Exception handling instruction

The athrow directive is used to perform the operation of explicitly throwing an exception (throw). In addition to throwing an exception, the JVM also specifies that it will be thrown automatically at runtime when other JVM instructions detect an exception. For example, the JVM throws the ArithmeticException in idiV or Ldiv when the divisor is zero.

Synchronization instructions

The JVM uses Monitor to support the following synchronization scenarios:

  • Method level synchronization, which does not require bytecode control, is implemented for method calls and return operation macros. ACC_SYNCHRONIZED from the method table shows whether a method is synchronized. If set, the thread of execution needs to hold the pipe before it can execute and release the pipe when it is finished.
  • The synchronization of a sequence of instructions within a methodmonitorentermonitorexitTo support synchronized functionality.

If you use Java bytecode file structures and instructions, save them

Welcome to the Android Zen account to share valuable and thoughtful technical articles with you. Can add wechat “Ming_Lyan” remarks “into the group” to join the technical exchange group, discussion of technical problems is strictly prohibited all advertising irrigation. If you have technical problems in the Android field or have doubts about your future career plan, discuss with us. Welcome to the party.