Bytecode Linker v5
Bytecode Linker v5
Link multiple module bytecode blobs into a single Bytecode program.
Function signature
func Link(mods []ModuleBytecode) (Bytecode, error)
Types
ModuleBytecodehasName,Imports,Exports,Bytecode.BytecodehasMain []InstrandFunctions []Function.FunctionhasID,Name,Params,Code []Instr.InstrhasOp,Int,Str.
Rules
- Module names must be unique.
mainmust 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>
- Unknown module:
Linking steps
- Validate module order and imports.
- Merge function tables with ID rebasing.
- Patch every
MAKE_CLOSUREinstruction to the rebased ID. - Rebase
JUMPandJUMP_IF_FALSEtargets in each module’sMainby the number of Main instructions already emitted. Keep function-local jump indexes unchanged. - Concatenate module
Maincode in order.
Inputs use valid local jump targets (including the one-past-end exit) and dense
local function IDs 0..len(Functions)-1; function slice order need not equal
ID order. Do not mutate input artifacts. Repeated linking of the same inputs
must produce equivalent output. Copy instruction slices before patching.
Notes
- Preserve module order from the input.
- Return the first error encountered.
- Keep the linker readable and straightforward.
Before implementation
Use the module’s worked reconstruction and independent debugging lab to predict an intermediate artifact first. Preserve one failing counterexample while implementing the stated API. The lesson’s transfer task uses a fresh case after the guided trace.
Run tests to see results
No issues detected