Bytecode Linker v5

medium · compilers, bytecode, linking

Bytecode Linker v5

Link multiple module bytecode blobs into a single Bytecode program.

Function signature

func Link(mods []ModuleBytecode) (Bytecode, error)

Types

  • ModuleBytecode has Name, Imports, Exports, Bytecode.
  • Bytecode has Main []Instr and Functions []Function.
  • Function has ID, Name, Params, Code []Instr.
  • Instr has Op, Int, Str.

Rules

  • Module names must be unique.
  • main must exist and be last in the slice.
  • Each import must reference an earlier module by name.
    • Unknown module: unknown module: <name>
    • Import of future module: import of future module: <name>

Linking steps

  1. Validate module order and imports.
  2. Merge function tables with ID rebasing.
  3. Patch every MAKE_CLOSURE instruction to the rebased ID.
  4. Concatenate module Main code in order.

Notes

  • Preserve module order from the input.
  • Return the first error encountered.
  • Keep the linker readable and straightforward.
Run tests to see results
No issues detected