Virtual Machine State

A virtual machine's state is defined in ./src/types/types.d.ts

type VM_State = {
    // Bytecode file to be executed
    P: C0ByteCode,
    // Constants the VM will use
    C: VM_Constants,
    // The callstack of the virtual machine
    CallStack: VM_StackFrame[],
    // The function frame that is currently executed
    CurrFrame: VM_StackFrame,
    // The line number of .bc0 file that is currently executing
    CurrLineNumber: number,
    // The type pool (struct type information) hashmap
    TypeRecord: Map<string, Map<number, Struct_Type_Record>>
};

Any running instance of virtual machine can be described by a VM_State value and the corresponding heap memory status.

P - The Byte Code to be Executed

The property P stores the byte code that C0VM is currently executing. The C0ByteCode value is a parsed result of raw string of .bc0 file.

type C0ByteCode = {
    version: number;
    /* Int Constant Pool */
    intCount: number;
    intPool: Int32Array;
    /* String Constant Pool */
    stringCount: number;
    stringPool: Uint8Array;
    /* Function Pool */
    functionCount: number;
    functionPool: C0Function[];
    /* Native Functions */
    nativeCount: number;
    nativePool: (C0Native|undefined)[];
};

All fields except nativePool property are the same with the original C0VM (implemented in C). The property nativePool is an array of C0Native objects which are dynamically loaded during the parsing process.

For more information of C0Native, refer to the page that describes Native Functions

Native Function Loader

C - Important Constants of Virtual Machine

type VM_Constants = {
    // The pointer that points to the start of string pool in heap allocator
    stringPoolPtr: C0Pointer
}

Up to now, the VM_Constants only stores the pointer points to the start of string pool in heap memory.

This is stored explicitly since all the string literals are loaded by adding an offset on this address during runtime.

CurrFrame - The Function Frame currently Executing

When a function is executed by the C0VM.ts, a stack frame will be created to store the "context" of function execution. Specifically, the

  1. PC - Program Counter that indicates the position of next instruction in bc0 program

  2. Local Variables

  3. Operand Stack

  4. The code of function itself

Will be stored inside each stack frame.

type VM_StackFrame = {
    PC: number,
    S: VM_OperandStack,
    V: VM_LocalVariables,
    P: C0Function
};

CallStack - The Call Stack of Virtual Machine

When a user-defined function get called, (specifically, the invokestatic instruction is executed, a new function frame will be created to store the status of callee. The original caller frame will be pushed into a stack called "call stack" so that the execution progress can be resumed after callee returns.

CurrLineNumber - Line Number of bc0 File VM is on

Records the current line number in bc0 file that VM is executing.

TypeRecord - The Type information of Structs

The hash map that records the type information within each struct. The Struct_Type_Record is defined as:

type Struct_Type_Record = {
    name ?: string,
    type ?: C0Type<C0TypeClass>
};

The namerefers to the field name of the struct.

The type refers to the type of that field of the struct.

Last updated