Java JVM: Class class file structure (4)

Java maintains good backward compatibility, and the stability of the Class file structure is indispensable

1. The cornerstone of irrelevance

  • The Java virtual machine is not bound to any programming language including the Java language, it is only associated with the specific binary file format of "Class file"
    • The Class file contains the Java virtual machine instruction set, symbol table, and several other auxiliary information
  • The virtual machine does not care what language the source of the Class is

2. Class files

  • Java maintains good backward compatibility, and the stability of the Class file structure is indispensable
  • Any Class file corresponds to the definition information of a unique class or interface
    • Conversely, classes or interfaces do not necessarily have to be defined in files (it can be dynamically generated and sent directly to the class loader)
  • Class files are a set of binary streams based on 8 bytes
    • For data items that occupy more than 8 bytes of space, they are divided into several 8 bytes for storage according to the high order first method
  • Parsing requires two data types as the basis
    • Unsigned numbers are primitive data types
      • Describe numbers, index references, quantity values ​​or form strings according to UTF-8 encoding
    • A table is a composite data type composed of multiple unsigned numbers or other tables as data items (ending with "_info")
  • To describe multiple data of the same type but with a variable number, a pre-capacity counter is used
    • A series of continuous data of a certain type is a "set" of a certain type
  • The version of the magic number and Class file
    • The first 4 bytes of the Class file are called the magic number
      • Used to determine whether this file is a Class file that can be accepted by the virtual machine
    • The 5th and 6th bytes are the minor version number, and the 7th and 8th bytes are the major version number
      • The Java version number starts from 45. Higher versions are backward compatible, but not upward compatible.

3. Constant pool

  • The resource warehouse in the Class file is the first table-type data item that appears in the Class file
  • Only the capacity count of the constant pool in the Class file starts from 1, and the others start from 0
  • main storage
    • Literal
      • literal string, constant value declared final
    • symbol reference
      • Packages exported or exposed by modules
      • fully qualified names of classes and interfaces
      • Field names and descriptors
      • method name and descriptor
      • Method handle and method type
      • Dynamic call sites and dynamic constants
  • When the Java code is compiled with Javac, when the virtual machine loads the Class file, it is dynamically linked
  • When the virtual machine does class loading, it will obtain the corresponding symbol reference from the constant pool, parse and translate it into a specific memory address when the class is created or run
  • Each constant in the constant pool is a table
  • A tool to analyze Class file bytecode: javap

4. Access sign

  • After the constant pool, the next 2 bytes represent access flags (access_flags)
    • Mainly identifies the access information at the class or interface level, whether the class is an interface, public, abstract, final, etc.
  • access_flags has a total of 16 flags that can be used
  • Class index, parent class index and interface index
    • Three items of data are used to determine the inheritance relationship of this type
    • Class Index: Determine the fully qualified name of this class
    • Parent class index: Determine the fully qualified name of the parent class of this class
    • Interface Index: Describes which interfaces this class implements

Five, table collection

  • field table collection
    • Used to describe variables declared in interfaces or classes
    • Can include modifiers with field scope (public, private, protected modifiers), instance variables or class variables, mutability, concurrent visibility, field data types, etc.
    • Fully qualified name: Replace the "." in the class full name with "/"
    • Simple name: inc() method and m field are: "inc" and "m" respectively
    • String[][] => [[Ljava/lang/String
    • int[] => [I
  • method table collection
    • Structure: access flag access_flags, name index name_index, descriptor index descriptor_index, attribute table set attributes
  • attribute table collection
    • Class files, field tables, and method tables can all carry their own attribute table collections
    • It is no longer required that each attribute table has a strict order. As long as it does not overlap with existing attribute names, any compiler implemented by anyone can write their own defined attribute information into the attribute table

6. Introduction to Bytecode Instructions

  • The bytecode instruction set can be regarded as an instruction set architecture with distinctive characteristics, outstanding advantages and disadvantages
  • The length of the Java virtual machine opcode is limited to one byte, and the total number of opcodes in the instruction set cannot exceed 256
  • Bytecode and data types
    • The iload instruction is used to load the int type data from the local variable table to the operand stack, and the fload is the float type
    • The instruction set of the Java virtual machine only provides limited type-related instructions to support specific operations
  • load and store instructions
    • Load and store instructions are used to transfer data to and from the local variable table in the stack frame and the operand stack
    • The operand stack and local variable table for storing data are mainly operated by load and store instructions
  • Operation instruction
    • Addition instructions: iadd, ladd, fadd, dadd
    • Subtraction instructions, multiplication instructions...
  • other
    • type conversion instruction
    • operand stack management instructions
    • control transfer instruction
    • Method calls and return instructions
    • exception handling instructions
    • synchronous command

7. Shared design, private implementation

  • How virtual machines are implemented
    • Translate the input Java virtual machine code into another virtual machine instruction set at load time or execution time
    • Translation of input Java virtual machine code into the native instruction set of the host processor at load time or execution time (i.e. even compiler code generation techniques)

Guess you like

Origin blog.csdn.net/baidu_40468340/article/details/128785629