The runtime data area of the Java virtual machine-go language implementation

The runtime data area of ​​the Java virtual machine

The Java virtual machine calls the memory area that stores various data the runtime data area. Runtime data areas are divided into two categories:

One is shared by multiple threads, and the other is private to multiple threads. The data shared by multiple threads is created when the Java virtual machine starts and destroyed when the Java virtual machine exits. The thread-private runtime data area is created when the thread is created and destroyed when the thread exits.

runtime data area

The area shared by multiple threads includes classes and instance objects. Instance objects are stored in the heap, and classes are stored in the method area. The class data includes the class's methods, fields, method bytecodes, and constant pool information.

The thread-private area includes the PC (program counter) and the Java virtual machine stack, and the Java virtual machine stack is composed of stack frames, which store local variables and operand stack information. At any time, a certain thread must execute a certain method, and this method is called the current method of the thread; the frame in which the method is executed is called the current frame.

Data types supported by the java virtual machine

Operand types supported by the java virtual machine

As shown in the figure above, the data types supported by the java virtual machine include two types, one is the basic type, and the other is the reference type

Basic types include Boolean types and numeric types, and numeric types include integer types and floating point types.

Reference types include class types, interface types, and array types. Among them, the class type reference points to the class instance, the array type reference points to the array instance, and the interface type reference points to the class or array instance that implements the interface.

The go language provides a very rich data type, and a certain mapping relationship has been established between these basic types and the data types of the Java virtual machine.

Java virtual machine data type and go language data type mapping table

Implementing a runtime data area

According to the previous description, define the Thread structure:

type Thread struct {
    
    
    pc int //程序计数器
    stack *Stack //Java虚拟机栈
}

The Java virtual machine specification has quite loose constraints on the Java virtual machine stack. The virtual machine stack can be a continuous space, or it can be discontinuous, it can be a fixed size, or it can be dynamically expanded at runtime.

If the stack space required by the execution thread exceeds the limit, a StackOverflowError exception will be thrown. If the Java virtual machine can be dynamically expanded, but the memory has been exhausted, an OutOfMemoryError exception will be thrown.

For the Java virtual machine stack, we usually implement it with a linked list data structure, so that the stack can use memory space as needed, and the popped frames can be recycled by Go's garbage collector. Define the Stack structure as follows:

type Stack struct {
    
    
    maxSize uint //栈的最大大小
    size uint //当前大小
    _top *Frame //保存栈顶指针
}

Define the structure of the frame in the virtual machine as follows:

type Frame struct {
    
    
    lower *Frame //用于实现链式栈
    localVars LocalVars //局部变量表
    operandStack *OperandStack  //操作数栈
}

Define the local variable table as follows:

type LocalVars []Slot
type Slot struct {
    
    
    num int32 //用来保存基本数据类型
    ref *Object //用来保存引用类型
}
type Object struct {
    
    
}

Define the operand stack OperandStack as follows:

type OperandStack struct {
    
    
    size uint //记录栈顶的位置
    slots []Slot //
}

See github for specific implementation

Guess you like

Origin blog.csdn.net/sinat_28199083/article/details/129348519