BC0 File Parser

A bc0 file is divided into several sections:

  1. Magic Number Starting with C0 C0 FF EE, the magic number tells us that this file is generated by a CC0 compiler. Some other "meta-data" like version number etc. are also included in this section.

  2. Integer Pool Inside the bc0 instructions, we can push small values ranging from 0x00 to 0xFF directly into the operand stack using BIPUSH instruction. However, to use larger integer constants (literals that written in the code like 0xDEADBEEF), we need to store them separately in a Int Pool.

  3. String Pool String literals are stored in a specific memory section and will be loaded when the VM initializes. During the runtime, these strings will be loaded into the operand stack according to the instructions. This specific section is called the String Pool.

  4. Functions The name of function and the bytecode instructions.

  5. Native Pool The native functions that will be needed in this program will be logged here.

The parser will convert the bc0 raw string into a data structure like this:

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)[];
};

Last updated