当前位置:网站首页>JVM (IV) Class file structure (complete parsing of bytecode attached)

JVM (IV) Class file structure (complete parsing of bytecode attached)

2022-06-12 10:02:00 A god given dream never wakes up

JVM( Four ).Class File structure ( Complete analysis of bytecode attached )

1. summary

The program that the computer executes is 01 The instruction structure ; The code needs to be translated into 01 The binary format can be executed ; But when , The development of virtual machine language in the last decade , More and more programming languages have chosen to work with operating system and Machine instructions Irrelevant format as compiled format ; The compiler compiles the source code into bytecode , Then the virtual machine of the specific system translates the bytecode into executable instructions ;

2. Irrelevance

There are many kinds of instruction sets for computers X86,ARM , Operating system platform Windows ,Linux; therefore Write Once,Run Anywhere Is implemented on the operating system platform ; Virtual machines on different platforms load unified bytecode to realize ;

The basis of independence is the virtual machine and bytecode ,Java Virtual machine load only Class file ,Class There are Java Instruction set of virtual machine , The symbol table , And other supporting information ; Ensure that all platforms are consistent , therefore Class Documents must have many mandatory syntax and institutional constraints ; No matter what source code you write , As long as the compiled language conforms to Class Specification of documents , Can be executed by the virtual machine ( Language independence );

 Insert picture description here

3. Class File structure

Java Technology has always maintained good compatibility .Class The stability of the file structure is indispensable ,Java In the release of more than a dozen large versions , After countless small updates ,Class The details of the document have hardly changed ; Although the virtual machine specification is for Class The file format has been updated , It is also a new extension of the original structure , The original definition is not modified ;

// example   class  file   16bit View 
CAFEBABE 00000034 00150A00 04001109  num:1 16B
00030012 07001307 00140100 01610100  num:2 32B
01490100 063C696E 69743E01 00032829  num:3 48B
56010004 436F6465 01000F4C 696E654E  num:4 64B
756D6265 72546162 6C650100 04676574  num:5 80B
41010003 28294901 00047365 74410100  num:6 96B
04284929 5601000A 536F7572 63654669  num:7 112B
6C650100 0748572E 6A617661 0C000700  num:8 128B
080C0005 00060100 0B636F6D 2F696A76  num:9 144B
6D2F4857 0100106A 6176612F 6C616E67  num:10 160B
2F4F626A 65637400 21000300 04000000  num:11 176B
01000200 05000600 00000300 01000700  num:12 192B
08000100 09000000 1D000100 01000000  num:13 208B
052AB700 01B10000 0001000A 00000006  num:14 224B
00010000 00030001 000B000C 00010009  num:15 240B
0000001D 00010001 00000005 2AB40002  num:16 256B
AC000000 01000A00 00000600 01000000  num:17 272B
08000100 0D000E00 01000900 00002200  num:18 288B
02000200 0000062A 1BB50002 B1000000  num:19 304B
01000A00 00000A00 02000000 0C000500  num:20 320B
0D000100 0F000000 020010  num:21 331B

// Related to the source code 
package com.ijvm;
public class HW {
    
    private int a;
    public int getA() {
    
        return a;
    }
    public void setA(int a) {
    
        this.a = a;
    }
}

Class The files are grouped in 8 A binary stream in bytes ; Data items are arranged in order , There is no separator in the middle ;

Class The format of the file is : notes u1,u2,u4,u8 respectively 1,2,4,8 An unsigned number of bytes

type name Number
u4magic1
u2minor_version1
u2major_version1
u2constant_pool_count1
cp_infoconstant_poolconstant_pool_count-1
u2access_flags1
u2this_class1
u2super_class1
u2interfaces_count1
u2interfaces1
u2field_count1
field_infofieldfield_count
u2method_count1
method_infomethodmethod_count
u2attribute_count1
attribute_infoattributeattribute_count

3.1 Magic and Class Version of a file

CAFEBABE 00000034

Class In front of the file 4 Bytes oxCAFEBABE , Make sure the file is acceptable to the virtual machine Class file , Because the file extension can be modified at will ;

The four bytes following the magic number are the version number :5~6 Sub version number 0000 7~8 The major version number 0034 The corresponding decimal system is 52;

JDK Version number Version number
1.145
1.246
1.347
1.448
JDK549
JDK650
JDK751
JDK852
JDK953

You can see 52 The corresponding version is JDK8 That is the JDK8 Or the above version of virtual machine Class file

notes : High version virtual machines support adding parameters to compile code into low version virtual machine class files

javac -target 7 -source 7   HW.java

3.2 Constant pool

The major and minor version numbers are followed by the constant pool ; It is Class The most data associated with other projects in the file structure ;

CAFEBABE 00000034
// Constant pool   u2  constant_pool_count 0015->21
0015
0A00 04001109  num:1 16B
00030012 07001307 00140100 01610100  num:2 32B
01490100 063C696E 69743E01 00032829  num:3 48B

Constant pool capacity ox0015 convert to 10 Base number 21; Express 20(constant_pool_count) A constant string ; The index of the string is 1 ~ 21; common 21 individual ;

The constant pool mainly stores two types of information Literal Literal And symbols Symbolic Reference

  • Module export or open package Package
  • Class and interface fully qualified names Fully Qualified Name
  • Field and name descriptors Descriptor
  • Method and name descriptors
  • Method handle and method type Method Handle Method Type Invoke Dynamic
  • Dynamic call points and dynamic constants Dynamically-Computed Call Site , Dynamically-Computed Constant

The item types of the constant pool are : Every constant in the constant pool is a table : The starting position is u1 type The mark of a tag ;

type tag describe project digit byte meaning
CONSTANT_Utf8_info1Utf8 code String tag
length
bytes
1
2
length
tag
Number of string characters
utf8 code String u1 Array
CONSTANT_Integer_info3 integer Literal tag
bytes
1
4
tag
Store in high order int value
CONSTANT_Float_info4 floating-point Literal tag
bytes
1
4
tag
Store in high order float value
CONSTANT_Long_info5 Long integer Literal tag
bytes
1
8
tag
Store in high order long value
CONSTANT_Double_info6 Double precision Literal tag
bytes
1
8
tag
Store in high order double value
CONSTANT_Class_info7 Symbolic references to classes and interfaces tag
index
1
2
tag
Index to a fully qualified constant item
CONSTANT_String_info8 character string Literal tag
index
1
2
tag
Index to literal string
CONSTANT_Fieldref_info9 Field symbol reference tag
index
index
1
2
2
tag
CONSTANT_Class_info The index of
CONSTANT_NameAndType_info The index of
CONSTANT_Methodref_info10 Symbolic references to methods in class tag
index
index
1
2
2
tag
CONSTANT_Class_info The index of
CONSTANT_NameAndType_info The index of
CONSTANT_InterfaceMethodref_info11 Symbolic references to methods in interfaces tag
index
index
1
2
2
tag
CONSTANT_Class_info The index of
CONSTANT_NameAndType_info The index of
CONSTANT_NameAndType_info12 Symbolic references to fields or methods tag
index
index
1
2
2
tag
Index of field or method name
Field or method descriptor index
CONSTANT_MethodHandle_info15 Method handle tag
reference_kind
reference_kind
1
1
2
tag
1~9 Handle type
Constant pool index
CONSTANT_MethodType_info16 Method type tag
descriptor_kind
1
2
tag
CONSTANT_Utf8_info The reference index of
CONSTANT_Dynamic_info17 Calculate constants dynamically tag
index
index
1
2
2
tag
Method index
CONSTANT_NameAndType_info The index of
CONSTANT_InvokeDynamic_info18 Dynamic method call points tag
index
index
1
2
2
tag
Method index
CONSTANT_NameAndType_info The index of
CONSTANT_Module_info19 Represents a module tag
index
1
2
tag
CONSTANT_Utf8_info The reference index of
CONSTANT_Package_info20 Open or exported packages in modules tag
index
1
2
tag
CONSTANT_Utf8_info The reference index of

It's on it 17 Types of constants ; Each constant type has its own completely independent data structure Example ;

CONSTANT_Utf8_info {
    
    u1 tag;	
    u2 length;
    u1 bytes[length]; 
}

analysis :

#1 // Constant pool   u2  constant_pool_count 0015->21  
0015
//tag =  0A -> 10    CONSTANT_Methodref_info
// 0004-> #4  0011->#17  
0A 0004 0011  

#2 //tag = 09 -> 9   CONSTANT_Fieldref_info 
// 0003-> #3  0012->#18  
09 0003 0012  

#3 //tag = 07 -> 7  CONSTANT_Class_info 
// 0013-> #19  
07 0013

#4  //tag = 07 -> 7  CONSTANT_Class_info 
// 0014-> #20
07 0014

#5  ~  # 16    CONSTANT_Utf8_info
// a 
01 0001 61   
01 0001 49
//  <init>
01 0006 3C696E69743E 
01 0003 282956
01 0004 436F6465 
01 000F 4C696E654E756D6265725461626C65  
01 0004 67657441 
01 0003 282949
01 0004 73657441
01 0004 28492956 
01 000A 536F7572636546696C65
// HW.java
01 0007 48572E6A617661 

#17 //tag = 0c -> 12  CONSTANT_NameAndType_info 
// 0007-> #7  0008->#8
0C 0007 0008 

#18 //tag = 0c -> 12  CONSTANT_NameAndType_info 
// 0007-> #7  0008->#8
0C 0005 0006
#19   // com/ijvm/HW
01 000B 636F6D2F696A766D2F4857 
#20  // java/lang/Object
01 0010 6A6176612F6C616E672F4F626A656374

Verify that the resolution was successful Use Built in command parsing

javap -v  HW.class

Classfile /D:/Resource/study/src/main/java/com/ijvm/HW.class
  Last modified 2022-2-6; size 331 bytes
  MD5 checksum f794f59e60ead2b659a9a4b1f8079a7f
  Compiled from "HW.java"
public class com.ijvm.HW
  minor version: 0
  major version: 51
  flags: ACC_PUBLIC, ACC_SUPER
  // contrast   The above analysis is consistent 
Constant pool:
   #1 = Methodref          #4.#17         // java/lang/Object."<init>":()V
   #2 = Fieldref           #3.#18         // com/ijvm/HW.a:I
   #3 = Class              #19            // com/ijvm/HW
   #4 = Class              #20            // java/lang/Object
   #5 = Utf8               a
   #6 = Utf8               I
   #7 = Utf8               <init>
   #8 = Utf8               ()V
   #9 = Utf8               Code
  #10 = Utf8               LineNumberTable
  #11 = Utf8               getA
  #12 = Utf8               ()I
  #13 = Utf8               setA
  #14 = Utf8               (I)V
  #15 = Utf8               SourceFile
  #16 = Utf8               HW.java
  #17 = NameAndType        #7:#8          // "<init>":()V
  #18 = NameAndType        #5:#6          // a:I
  #19 = Utf8               com/ijvm/HW
  #20 = Utf8               java/lang/Object

a I <init> V LineNumberTable And other strings are automatically generated by the compiler , Describe something that is not convenient to express in fixed bytes

3.3 Access signs

After the end of the measuring cell , And then one u2 Represents the access flag access_flags: Used to represent access information at the class or interface level , Identify whether it is a class or an interface , Whether it is public,abstract ,final

Logo name Flag value tag meaning
ACC_PUBLIC0x0001 Whether it is public
ACC_FINAL0x0010 Whether it is final
ACC_SUPER0x00201.0.2 After that, it's all true
ACC_INTERFACE0x0200 Whether it is Interface
ACC_ABSTRACT0x0400 Whether it is abstract class
ACC_SYNTHETIC0x1000 Non user code generation
ACC_ANNOTATION0x2000 Whether it is annotation
ACC_ENUM0x4000 Whether it is enumeration
ACC_MODULE0x8000 Whether it is A module

Instance class ACC_PUBLICACC_SUPER The sign should be true , Other signs should be false ;access_flags The value should be :0x0001|0x0020=0x0021

// Constant pool end position 
2F4F626A 656374    
// Access signs  0x0021 
00 21
000300 04000000  
01000200 05000600 00000300 01000700  
08000100 09000000 1D000100 01000000   
052AB700 01B10000 0001000A 00000006 
00010000 00030001 000B000C 00010009   

3.4 Class index 、 Parent index and interface index set

One Class There are the following three to determine the inheritance relationship of a class

  • Class index this_class u2 type The data of : The fully qualified name of the class ; Point to a type of CONSTANT_Class_info Class descriptor constant for
  • The parent class is called super_class u2 type The data of : The fully qualified name of the parent class ;Java It's single inheritance , except java.lang.Object outside , be-all Java Class has a parent class ; Point to a type of CONSTANT_Class_info Class descriptor constant for
  • Interface index set ( A class may implement multiple interfaces ) It's a u2 type Set ; The data structure of a collection is generally u2 Number , Then there is the data ;
// Access signs  0x0021 
00 21
//this_class   super_class   Look in the constant pool  #3 #4
// #3 = Class              #19           
// #4 = Class              #20           
// #19 = Utf8               com/ijvm/HW    this_class
// #20 = Utf8               java/lang/Object
0003 0004 
// Interface index set   The length is 0  That is to say, the interfaces that are not implemented subsequently 
0000

00  01000200 05000600 00000300 01000700  
08000100 09000000 1D000100 01000000   
052AB700 01B10000 0001000A 00000006 
.......

3.5 Field table aggregate

Field table Field_info Describe variables declared in interfaces or classes ; There are several directions ;

  • Scope public private protected Modifier
  • Class level (static) And instance level variables
  • variability final
  • Concurrent visibility volatile
  • Can be serialized transient
  • Type of field Basic types object Array

Data structure of field table

type name Number meaning
u2access_flags1 Access signs
u2name_index1 Field name
u2descriptor_index1 Field descriptors
u2attributes_count1 Number of attributes
attribute_infoattributesattributes_count
// A field 
0001
//#### 3.5.1 access_flags  0002 -> public 
0002
// 3.5.2   name_index  Application of constant pool   a  
0005 
// 3.5.3 descriptor_index   Application of constant pool   I -> int  type 
0006 
//attributes_count 0000
0000
000300 01000700  
08000100 09000000 1D000100 01000000   
052AB700 01B10000 0001000A 00000006 
.......

3.5.1 access_flags

access_flags And the above class access_flags similar

Logo name Flag value tag meaning
ACC_PUBLIC0x0001 Whether it is public
ACC_PRIVATE0x0002 Whether it is private
ACC_PROTESTED0x0004 Whether it is protected
ACC_STATIC0x0008 Whether it is static
ACC_FINAL0x0010 Whether it is final
ACC_VOLATILE0x0040 Whether it is volatile
ACC_TRANSIENT0x0080 Whether it is transient
ACC_SYNTHETIC0x1000 Whether it is The compiler generates
ACC_ENUM0x4000 Whether it is enumeration

3.5.2 name_index

This is a reference to the constant pool ;

3.5.3 descriptor_index

This is a reference to the constant pool ; The resulting data is of the following types ; For example, the example code arrives at I

Identification characters meaning Identification characters meaning
BbyteJlong
CcharSshort
DdoubleZboolean
FfloatVvoid
IintL object type

3.6 Method table aggregate

Class File store The description of the method in the format is almost exactly the same as that of the field , The structure of a method table is like a field table , In turn, it includes the access flag access_flags、 Index of names name_index、 Descriptor index descriptor_index、 Property sheet aggregate attributes Several items ;

// Number of methods   0x0003 
0003 
// Access signs  public   Method name #7  <init>   Method Descriptor #8    ()V
0001       0007         0008     
// The number of arguments   Parameter name #9 ->Code 
0001 0009 

000000 1D000100 01000000   
052AB700 01B10000 0001000A 00000006 
.......

Data structure of method table

type name Number meaning
u2access_flags1 Access signs
u2name_index1 Method name
u2descriptor_index1 Method Descriptor
u2attributes_count1 Number of attributes
attribute_infoattributesattributes_count

3.6.1 access_flags

access_flags It is a little different from the field , such as volatile Just not on the method

Logo name Flag value tag meaning
ACC_PUBLIC0x0001 Whether it is public
ACC_PRIVATE0x0002 Whether it is private
ACC_PROTESTED0x0004 Whether it is protected
ACC_STATIC0x0008 Whether it is static
ACC_FINAL0x0010 Whether it is final
ACC_SYNCHRONIZED0x0020 Whether it is synchronized
ACC_BRIDEG0x0040 Whether it is Compiler bridging method
ACC_VARARGS0x0080 Whether it is Variable parameters
ACC_NATIVE0x0100 Whether it is native
ACC_ABSTRACT0x0400 Whether it is abstract
ACC_STRICT0x0800 Whether it is strictfp
ACC_SYNTHETIC0x1000 Whether it is The compiler generates

3.6.2 name_index descriptor_index attributes_count

name_index descriptor_index attributes_count Same field

3.6.3 attribute_info

Below

3.7 Property sheet aggregate

Property sheet (attribute_info) It has appeared several times ,Class file 、 Field table 、 Methods and tables are OK Carry your own property sheet collection , To describe some scenario specific information ; The set of property sheets is slightly more restrictive some , It is no longer required that each property sheet has a strict order

The attribute name Use location meaning
Code Method table Java Bytecode instructions for code compilation
ConstantValue Field table from final Constant value defined by keyword
Deprecated Class method table Statement Deprecated
Exceptions Method table Method throws a list of exceptions
LineNumberTableCode attribute Java Source code line number and byte code correspondence
SourceFile Class file Record source file name
LocalVariableTableCode attribute Local variable description
InnerClass Class file Inner class list
StackMapTableCode attribute JDK6 Add a new attribute after , Verify that the types match
synthetic class , Method table , Field table Identity is automatically generated by the compiler

3.7.1 Code

Java The code in the method body of the program goes through Javac After the compiler processes , Finally, the bytecode instructions are stored in Code Within the properties

Code The data structure of the table

type name Number meaning
u2attribute_name_index1 Fix Code
u4attribute_length1 Property sheet length -u2-u4; Back length
u2max_stack1 Method Descriptor
u2max_locals1 Local variable table length , Variable slot 32bit A variable slot ; That's all 2 Variable slot
u4code_length1 Bytecode length ; The theory is 2^32, The actual use 65535 Maximum (u2)
u1codecode_length Code u1 Set u1=0x00~0xFF The decimal system is 0~255 Can express 256 Orders
u2exception_table_length1 Number of exception tables
exception_infoexception_tableexception_table_length Abnormal information
u2attributes_count1 Number of attributes
attribute_infoattributesattributes_count Property sheet

Exception table data structure ( Interview test return value )

type name Number meaning
u2start_pc1try Start
u2end_pc1try end
u2handle_pc1 The exception is to jump to handle_pc
u2catch_type1 Exception types by 0 when
Any exception to handle_pc
// Example 1 inc()  normal  1  abnormal  2 ;
public int inc() {
    
        int x;
        try {
    
            x = 1;
            // int i = 2/0;
            return x;
        } catch (Exception e) {
    
            x = 2;
            // int i = 2/0;
            return x;
        } finally {
    
            x = 3;
        }
    }

** notes : When the return value is a basic type return Instructions will be copied x To Variable slot of local variable table Inside , hinder finally The modification in is x Not at all Variable slot Data in , So the return is 2 No 3, If it's a reference type There is no process of copying **

// Number of methods   0x0003 
0003 
// Access signs  public   Method name #7  <init>   Method Descriptor #8    ()V
0001       0007         0008     
// The number of arguments   Parameter name #9 ->Code 
0001 0009 
//code length = 0x0000001D = 29
0000001D 
//max_stack  max_locals
0001 0001 
//code_length
00000005
2A B7 00 01 B1
// A byte   An order    Five instructions 
0 2A: The first reference type is a local variable  
1 B7: Call superclass constructor , Instance initialization method , Private method 
2 00: Don't do anything? 
3 01: take null Push to top of stack 
4 B1: Return from current method void
//
 Code:
      stack=1, locals=1, args_size=1  
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 3: 0

//exception_table_length  exception_info  because  exception_table_length = 0  No,  
0000

//  3.7.3 LineNumberTable attribute  
//attributes_count    One 
0001
// Code  attribute  000A->LineNumberTable   length  00000006 
000A 00000006
// The number of line numbers corresponds to  0001   Contrast relationship 0000:0003
0001 0000 0003


Be careful : stack=1, locals=1, args_size=1 Why? args_size=1 It's because of putting this It also becomes a parameter , If it is static The method of decoration Will be for 0

3.7.2 Exceptions

Code An attribute of the same rank ; List the checked exceptions that may be thrown in the method (Checked Exceptions); The method is described in throws Exception listed after keyword ;

type name Number meaning
u2attribute_name_index1Exceptions
u4attribute_length1 The length added up later u2+
u2*number_of_exceptions
u2number_of_exceptions1exception The number of
u2exception_index_tablenumber_of_exceptionsCONSTANT_Class_info The index of the type constant

3.7.3 LineNumberTable

LineNumberTable Property is used to describe Java Source code line number and bytecode line number ( The offset of the bytecode ) The correspondence between , By default, it will be generated to Class In the document . Optional , Javac Use in -g:none or -g:lines Option to cancel or request the generation of this information .

If you choose not to generate LineNumberTable attribute , The most important impact on program operation Ring is when an exception is thrown , The wrong line number will not be displayed on the stack , And when debugging the program , You can't set breakpoints according to the source line .

type name Number meaning
u2attribute_name_index1LineNumberTable
u4attribute_length1 The length added up later u2+
(u2+u2)* line_number_table_length
u2line_number_table_length1line_number_table The number of
line_number_info(u2+u2)line_number_tableline_number_table_length every last line_number_table;
Bytecode line number , The latter is Java Source

3.7.4 LocalVariableTable And LocalVariableTypeTable attribute

LocalVariableTable: Describes the variables and values of the local variable table in the stack frame Java The relationship between variables defined in the source code ; Optional , stay Javac Use in -g:none or -g:vars Option to cancel or request the generation of this information . If this property is not generated , All parameter names will be lost ,IDE Will use things like arg0、arg1 Such placeholders replace the original parameter names , The program operation has no effect , But it will bring great inconvenience to code writing , And during debugging, the parameter value cannot be obtained from the context according to the parameter name ;

type name Number meaning
u2attribute_name_index1LocalVariableTable
u4attribute_length1 The length added up later
u2local_variable_table_length1local_variable_table The number of
local_variable_infolocal_variable_tablelocal_variable_table_length every last local_variable_table

local_variable_info structure

type name Number meaning
u2start_pc1 Variable scope start
u2length1 Offset after the start of variable scope
u2name_index1CONSTANT_Utf8_info The index of the type constant
u2descriptor1CONSTANT_Utf8_info The index of the type constant
u2index1 Position of the variable slot in the local variable table ; yes 64 Bit type ;
The variable slot is index and index+1 Two

3.7.5 SourceFile And SourceDebugExtension attribute

SourceFile Property is used to record the generation of this Class File source file name ; Optional ; Do not generate this property , When an exception is thrown , Stack The file name to which the error code belongs... Will not be displayed

type name Number meaning
u2attribute_name_index1SourceFile
u4attribute_length1 Attribute length
u2source_index1 Indexes

SourceDebugExtension Property is used to store additional code debugging information . A typical scenario is when JSP file debugging

3.7.6 ConstantValue attribute

ConstantValue Property is used to inform the virtual machine to automatically assign values to static variables . Only by static Keyword modifies the variable ( Class variables ) To use this property . Right static Variable of type ( That is, instance variables ) The assignment of is in the instance constructor () Method ; And for class variables , There are two ways to choose : stay class constructor () Method or use ConstantValue attribute

type name Number meaning
u2attribute_name_index1ConstantValue
u4attribute_length1 Attribute length
u2constant_value_index1 Indexes

3.7.6 InnerClasses attribute

InnerClasses Property is used to record the association between the internal class and the host class . If an inner class is defined in a class , Then the compiler will generate... For it and the inner classes it contains InnerClasses attribute

type name Number meaning
u2attribute_name_index1InnerClasses
u4attribute_length1 Attribute length
u2number_of_class1 Indexes
inner_class_infoinner_classnumber_of_class

inner_class_info structure

type name Number meaning
u2inner_class_info_index1CONSTANT_Class_info The index of the type constant
u2outer_class_info_index1CONSTANT_Class_info The index of the type constant
u2inner_class_index1CONSTANT_Utf8_info The index of the type constant
u2inner_class_access_flagsnumber_of_class Class like access_flags

3.7.8 Deprecated and Synthetic

Deprecated and Synthetic: Boolean properties , There is only a difference between with and without , No property values ;

Deprecated Property is used to represent a class 、 Fields or methods , It has been determined by the program author that it is no longer recommended to use , It can be used in code “@deprecated” Comments to set

Synthetic Property represents this field or method, not by Java The source code is generated directly , It's added by the compiler

type name Number meaning
u2attribute_name_index1Deprecated/Synthetic
u4attribute_length1 Value must be 0x00000000, Because there is no attribute value to set

3.7.9 .Signature attribute

Signature Attribute in JDK 5 Add to Class In the document specification ; class 、 Field In the property table of the table and method table structure ; Greatly enhanced Java Grammar of language , After that , Any class 、 Interface 、 Initializes the generic signature of a method or member if it contains a type variable (Type Variable) Or parameterized type Parameterized Type, be Signature Property will be for it Record generic signature information . Bytecode (Code attribute ) Compile all generic information in ( Type variable 、 Parameterized types ) After compilation, it is all Erase ; For example, you need to use this signature information area to verify the type of added data ;

type name Number meaning
u2attribute_name_index1Signature
u4attribute_length1 Value must be 0x00000000, Because there is no attribute value to set
u2signature_index1CONSTANT_Utf8_info The index of

3.7.10 . Runtime annotation related properties

Runtime**Annotations * There are many kinds of signs Runtime annotations

type name Number meaning
u2attribute_name_index1InnerClasses
u4attribute_length1 Attribute length
u2number_of_annotations1 Indexes
annotationannotationsnumber_of_annotationsannotations Each element in represents
A note visible at runtime

annotation structure

type name Number meaning
u2type_index1 pool CONSTANT_Utf8_info Index value of constant
u2num_element_value_pairs1 The number of key value pairs in the annotation
element_value_pairelement_value_pairs1 Key value pair

4. Bytecode instruction

Java Virtual machine instructions : opcode + Parameters ( Operands )

opcode :Opcode A byte 0x00 ~ 0xFF 0~255 ; Each of these numbers corresponds to an operation / Instruction code ; So in theory, the biggest one is 256 Orders ;

Parameters ( Operands ): Most instructions do not contain operands , There is only one opcode , Instructions are stored in the operand stack ;Class The file has no length adjustment on the storage operand

Example 1

// Source code 
this.a = 1111;

//class  file  2A:aload_0 
2A
//11:sipush  Put a short integer constant value (-32768~32767) Push to top of stack   This is followed by two digit parameters  0457 Stored 
// But when reading, it comes one by one  0457 0x04 << 8 | 0x57 =  Get a short integer  0x0457 =  Decimal system 1111 
11 0457   
    
// Bytecode instruction 
ALOAD 0
SIPUSH 1111

Example 2

// Source code 
this.a = 10;

//class  file  2A:aload_0 
2A
//10:bipush  The constant value of a single byte (-128~127) Push to top of stack   And then 1  Bit parameters  0A Stored  
10 0A   
    
// Bytecode instruction 
ALOAD 0
BIPUSH 10

Pseudo code execution instructions

do{
    
    pc register +1;
    pc The register value removes the opcode from the byte code stream ;
    if( opcode   Yes   Operands )  Fetch operand from byte code stream ;
     Perform the operation ;
}while( Byte stream length  > 0 )

4.1 Bytecode & data type

stay Java Instruction set of virtual machine , Most instructions contain data type information corresponding to their operations ; stay Java Instruction set of virtual machine , Most instructions contain data type information corresponding to their operations ; The sub instruction does not support integer type byte、char and short, There's not even any instruction support boolean type , Most of them are for boolean、byte、short and char Operation of type data , In fact, they all use corresponding pairs int Type as operation type Computational Type To carry out

4.2 Load and store instructions

Load a local variable into the operation stack Tload [ _<n> ]; Example :iload ,iload ,lload ,lload_<n> n : 0 1 2 3

Store a value from the operand stack to the local variable table Tstore [ _<n> ];

Load a constant onto the operand stack Tconst [ _<T> ]bipush ,sipush,ldc;

4.3 Operation instruction

Not directly supported byte、short、char and boolean Type of arithmetic instruction , Use operation int Type of instruction instead of

  • Add instruction :iadd、ladd、fadd、dadd
  • Subtraction instructions :isub、lsub、fsub、dsub
  • Multiplication instructions :imul、lmul、fmul、dmul
  • Division instructions :idiv、ldiv、fdiv、ddiv
  • Order for redundancy :irem、lrem、frem、drem
  • Reverse instruction :ineg、lneg、fneg、dneg
  • Displacement command :ishl、ishr、iushr、lshl、lshr、lushr
  • Press bit or command :ior、lor
  • Press bit and command :iand、land
  • Bitwise XOR command :ixor、lxor
  • Local variable auto increment instruction :iinc
  • Comparison instruction :dcmpg、dcmpl、fcmpg、fcmpl、lcmp

4.4 Type conversion instructions

Java Virtual machine directly supports ( That is to say, there is no explicit conversion instruction ) The following generalized type conversions for numeric types (Widening Numeric Conversion, That is, the safe conversion from small scope type to large scope type ; such as int -> long 、double

Narrowing type conversion (Narrowing Numeric Conversion) when , You must explicitly use conversion instructions to accomplish , These conversion instructions include i2b、i2c、i2s、l2i、f2i、f2l、d2i、d2l and d2f

4.5 Object creation and access instructions

Instances and arrays are created using different bytecode instructions

  • Instructions for creating class instances :new
  • Instructions for creating arrays :newarray、anewarray、multianewarray Access class fields (static Field , Or called a class variable ) And instance fields ( Not static Field , Or called instance variable ) Of Instructions :getfield、putfield、getstatic、putstatic
  • An instruction to load an array element into the operand stack :baload、caload、saload、iaload、laload、faload、 daload、aaload
  • An instruction that stores the value of an operand stack in an array element :bastore、castore、sastore、iastore、fastore、aastore
  • The instruction to get the length of the array :arraylength
  • Check class instance type instructions :instanceof、checkcast

4.6 Operand stack management instructions

  • One or two elements at the top of the stack are out of the stack :pop、pop2

  • Swap the two values at the top of the stack :swap

  • Copy one or two values at the top of the stack and push the copied values or double copied values back into the top of the stack :dup、dup2、dup_x1、 dup2_x1、dup_x2、dup2_x2

4.7 Control transfer instructions

Give Way Java A virtual machine commands conditionally or unconditionally from a specified location ( Instead of controlling transfer instructions ) Next to An instruction continues to execute the program , Understand... From a conceptual model , It can be considered that the control instruction is modified conditionally or unconditionally PC Register value ;

  • 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
  • Compound conditional branch :tableswitch、lookupswitch
  • An unconditional branch :goto、goto_w、jsr、jsr_w、ret

boolean type 、byte type 、char The type and short Type of conditional branch comparison operation , All use int Type of comparison instruction ;long type 、float The type and double Type of conditional branch comparison operation , The corresponding type of comparison operation instruction will be executed first dcmpg、dcmpl、fcmpg、fcmpl、lcmp,

4.8 Method calls and return instructions

Method calls and Data type independent

  • invokevirtual Instructions : Used to invoke The instance method of the object , Dispatch based on the actual type of object ( Virtual method dispatch ).
  • invokeinterface Instructions : Used to invoke Interface method , It searches at run time for an object that implements this interface method , Find the right method to call .
  • invokespecial Instructions : Used to call some instance methods that need special handling , Include Instance initialization method 、 Private methods and Parent class method .
  • invokestatic Instructions : Used to call class Static methods (static Method ).
  • invokedynamic Instructions : Used to dynamically resolve the method referenced by the call point qualifier at run time . And implement the method .

Method return and Data types are related to

boolean、byte、char、short int The return value of the ireturnlreturn、freturn、dreturn and areturn, return void, Instance initialization method 、 Class initialization of classes and interfaces , Use return Instructions ;

4.9 Exception handling instructions

Operation that throws an exception athrow Instructions

Handling exceptions (catch sentence ) Not by bytecode instructions , The exception table is used to complete ;

4.10 Synchronization instructions

Java Virtual machines can support Method level synchronization and Synchronization of a sequence of instructions within a method ;

The synchronous instruction set sequence consists of Java In language synchronized Statement block to represent ,Java Instruction set of virtual machine Yes monitorenter and monitorexit Two instructions to support synchronized The semantics of keywords , Correct implementation synchronized Keywords need Javac compiler ( The compiler will automatically generate an exception handler to execute monitorexit) And Java The two virtual machines work together to support

// Source code  
void fun (HW3 hw3){
    
       synchronized (hw3){
    
           fun2();
       }
   }
// Bytecode 
  fun(Lcom/ijvm/HW3;)V
    TRYCATCHBLOCK L0 L1 L2 null  //L0 ~ L1  error   Jump  L2
    TRYCATCHBLOCK L2 L3 L2 null  //L2 ~ L3  error   Jump  L2  Guarantee  monitorexit  It will be carried out 
   L4
    LINENUMBER 11 L4
    ALOAD 1
    DUP
    ASTORE 2
    MONITORENTER   //  Lock   Instructions   To  (ASTORE 2 ) On the object 
   L0
    LINENUMBER 12 L0
    ALOAD 0
    INVOKEVIRTUAL com/ijvm/HW3.fun2 ()V
   L5
    LINENUMBER 13 L5
    ALOAD 2
    MONITOREXIT  //  Normal execution : Unlock   Instructions   To  (ASTORE 2 ) On the object 
   L1
    GOTO L6
   //  Method exception flow 
   L2
   FRAME FULL [com/ijvm/HW3 com/ijvm/HW3 java/lang/Object] [java/lang/Throwable]
    ASTORE 3 // Storage exception 
    ALOAD 2
    MONITOREXIT //  Exception execution : Unlock   Instructions   To  (ASTORE 2 ) On the object   Guarantee  monitorexit  It will be carried out 
   L3
    ALOAD 3  // It is very difficult to get out   Throw out 
    ATHROW
   L6
    LINENUMBER 14 L6
   FRAME CHOP 1
    RETURN

notes : Whether the method is abnormal or not monitorenter Instructions are executed accordingly monitorexit Instructions .

5. Public design , Private implementation

《Java Virtual machine specification 》 It depicts Java What virtual machines should be common Program storage format :Class File format and bytecode instruction set ; There are many ways to implement virtual machines , however Class The file and its corresponding instructions are unique ;

6. Appendix bytecode

// Related to the source code 
package com.ijvm;
public class HW {
    
    private int a;
    public int getA() {
    
        return a;
    }
    public void setA(int a) {
    
        this.a = a;
    }
}

Complete data

// example   class  file   16bit View 
CAFEBABE 00000034 00150A00 04001109  num:1 16B
00030012 07001307 00140100 01610100  num:2 32B
01490100 063C696E 69743E01 00032829  num:3 48B
56010004 436F6465 01000F4C 696E654E  num:4 64B
756D6265 72546162 6C650100 04676574  num:5 80B
41010003 28294901 00047365 74410100  num:6 96B
04284929 5601000A 536F7572 63654669  num:7 112B
6C650100 0748572E 6A617661 0C000700  num:8 128B
080C0005 00060100 0B636F6D 2F696A76  num:9 144B
6D2F4857 0100106A 6176612F 6C616E67  num:10 160B
2F4F626A 65637400 21000300 04000000  num:11 176B
01000200 05000600 00000300 01000700  num:12 192B
08000100 09000000 1D000100 01000000  num:13 208B
052AB700 01B10000 0001000A 00000006  num:14 224B
00010000 00030001 000B000C 00010009  num:15 240B
0000001D 00010001 00000005 2AB40002  num:16 256B
AC000000 01000A00 00000600 01000000  num:17 272B
08000100 0D000E00 01000900 00002200  num:18 288B
02000200 0000062A 1BB50002 B1000000  num:19 304B
01000A00 00000A00 02000000 0C000500  num:20 320B
0D000100 0F000000 020010  num:21 331B

Parsing data

 magic number :
CAFEBABE

 Small version  minor version: 
0000
 The big version  JDK  edition  major version :
00034  // ->52 JDK8

 Constant pool    Number :0015  -> 21  On behalf of 20 individual  1~20  
#1  0A->Methodref  (1 2 2 )    
0A 0004 0011  //Methodref #4 #17
#2 09 Fieldref  (1 2 2 )    
09 0003 0012  //Fieldref #3 #18
#3 07 Class (1 2 )
07 0013 //Class #19
#4 07 Class (1 2 )
07 0014 //Class #20

#5 ~ #16  --> 01 CONSTANT_Utf8_info( Back Utf8  Ellipsis )(1 2 1 )
01 0001 61  //Utf8 1 a
01 0001 49  //Utf8 1 I
01 0006 3C696E69743E //Utf8 6 <init>
01 0003 282956  //Utf8 3 ()V
01 0004 436F6465  //Utf8 4 Code
01 000F 4C696E654E756D6265725461626C65 //Utf8 15 LineNumberTable
01 0004 67657441  //Utf8 4 getA
01 0003 282949   //Utf8 3 ()I
01 0004 73657441   //Utf8 4 setA
01 0004 28492956   //Utf8 4 (I)V
01 000A 536F7572636546696C65  //Utf8 10 SourceFile
01 0007 48572E 6A617661   //Utf8 7 HW.java
#17 0C -> NameAndType1 2 20C 0007 0008  // NameAndType #7:#8
#18 0C -> NameAndType1 2 20C 0005 0006  // NameAndType #5:#6
#19  --> 01 CONSTANT_Utf8_info
01 000B 636F6D2F696A766D2F4857  //Utf8 11 com/ijvm/HW
#20  --> 01 CONSTANT_Utf8_info
01 0010 6A6176612F6C616E672F4F626A656374  //Utf8 16 java/lang/Object

// Class description  0x0001|0x0020=0x0021
0021  // The access flag of the class is  ACC_PUBLIC
0003 0004  // Class index  ->(#3)-> (#19)-> com/ijvm/HW 、 Parent index ->(#4)->(#20)->java/lang/Object
0000 // The interface index has no 

// Field description  
0001 // There is always one field 
#  First field 
0002 //private 
0005 // The attribute name  a
0006 // Attribute types  I  Express int 
0000 // There are no other properties   There can be annotation information here   What?   Can exist here 

//  Methods described 
0003  // There are three methods   With the default constructor 
// The first method 
0001  //public
0007  // Method name  <init>
0008  // Return value  ()V
0001  // An attribute 
// Property start  0009->code (2 4 2 2 4 1*n 2 2*m 2 2*j)
0009 
0000001D // Subsequent length  29
0001 0001 // Stack depth   Local variable table 
00000005 2AB70001B1 // Bytecode instruction length   Bytecode instruction 
0000 // The number of exceptions  0  therefore n = 0
0001 // Number of attributes  Code  Nested properties in  
//#10 ->LineNumberTable  Row correspondence  (2 4 2 (2+2)*n) 
000A 
00000006  0001 0000 0003  // Subsequent length 6 0001 Two corresponding relations   The first pair  0:3 
// Second method  
0001  //public
000B  //getA
000C  //()I 
0001  // An attribute 
// Property start  0009->code (2 4 2 2 4 1*n 2 2*m 2 2*j) 
0009   
0000001D  // Subsequent length  29
0001 0001 // Stack depth   Local variable table 
00000005 2AB40002AC  // Bytecode instruction length   Bytecode instruction  
0000 // The number of exceptions  0  therefore n = 0
0001 // Number of attributes  Code  Nested properties in  
//#10 ->LineNumberTable  Row correspondence  (2 4 2 (2+2)*n) 
000A 
00000006 0001 0000 0008 // Subsequent length 6 0001 Two corresponding relations   The first pair  0:3 
// Third way  
0001 //public
000D //setA
000E //(I)V
0001 // An attribute 
// Property start  0009->code (2 4 2 2 4 1*n 2 2*m 2 2*j) 
0009 
00000022 // Subsequent length  29
0002 0002 // Stack depth   Local variable table 
00000006 2A1BB50002B1 // Bytecode instruction length   Bytecode instruction  
0000 // The number of exceptions  0  therefore n = 0
0001 // Number of attributes  Code  Nested properties in 
//#10 ->LineNumberTable  Row correspondence  (2 4 2 (2+2)*n) 
000A
0000000A 0002 0000 000C 0005 000D // Subsequent length 10 0002 Two corresponding relations   The first pair  0:12  Second pairs  5:13

 Additional information 
0001  // Number  0001 
//#15 000F->SourceFile (2 4 2) 
000F  00000002  0010  // The additional information is the source file   length 2 0010->#16 HW.java
原网站

版权声明
本文为[A god given dream never wakes up]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010528446494.html