Module Wasm_backend.Ir

The WebAssembly-GC subset shared by the compiler, binary emitter, and diagnostic printer. Indices are zero-based. There is no linear memory or function table; all reference value and storage types are nullable.

type valtype =
  1. | I32
  2. | Ref of int
    (*

    Ref t denotes (ref null t), where t indexes modul.m_types.

    *)
type functype = {
  1. params : valtype list;
  2. results : valtype list;
}
type storage =
  1. | St_i32
  2. | St_i8
  3. | St_ref of int
    (*

    St_i8 is packed array/struct storage; St_ref t is a nullable reference to type t.

    *)
type field = {
  1. fld : storage;
  2. fmut : bool;
}
type comptype =
  1. | CFunc of functype
  2. | CStruct of field list
  3. | CArray of field
type subtype = {
  1. super : int option;
  2. sfinal : bool;
  3. comp : comptype;
}

super is an optional type index; sfinal prevents further subtyping.

type instr =
  1. | Const of int
  2. | Add
  3. | Sub
  4. | Mul
  5. | Div_s
  6. | Rem_s
  7. | Eq
  8. | Ne
  9. | Lt_s
  10. | Le_s
  11. | Gt_s
  12. | Ge_s
  13. | Eqz
  14. | Local_get of int
  15. | Local_set of int
  16. | Local_tee of int
  17. | Global_get of int
  18. | Call of int
    (*

    Index into m_funcs, excluding imports.

    *)
  19. | CallImport of int
    (*

    Index into m_imports.

    *)
  20. | CallRef of int
    (*

    Function type index.

    *)
  21. | RefFunc of int
    (*

    Local function index, also listed in m_declared.

    *)
  22. | RefCast of int
    (*

    Cast to a non-null reference of this type; traps on null or mismatch.

    *)
  23. | StructNew of int
  24. | StructGet of int * int
    (*

    Type index and field index.

    *)
  25. | ArrayNewDefault of int
  26. | ArrayNewData of int * int
    (*

    Array type index and passive data segment index.

    *)
  27. | ArrayGetU of int
  28. | ArraySet of int
  29. | ArrayLen
  30. | ArrayCopy of int * int
    (*

    Destination and source array type indices.

    *)
  31. | If of valtype list * instr list * instr list
  32. | Block of valtype list * instr list
  33. | Loop of valtype list * instr list
  34. | Br of int
    (*

    Relative label depth; zero addresses the innermost label.

    *)

Local indices count parameters first, then declared locals. Structured instructions carry result types; the binary emitter supports zero or one result per block. Other integer type operands index m_types.

type func = {
  1. fn_name : string;
    (*

    Diagnostic name.

    *)
  2. fn_type : int;
    (*

    Function type index.

    *)
  3. fn_locals : valtype list;
    (*

    Locals following the parameters.

    *)
  4. fn_body : instr list;
}
type global = {
  1. gl_name : string;
  2. gl_type : valtype;
  3. gl_mut : bool;
  4. gl_init : instr list;
}
type export = {
  1. ex_name : string;
  2. ex_func : int;
    (*

    Local function index, excluding imports.

    *)
}
type modul = {
  1. m_types : subtype list;
    (*

    One ordered recursion group.

    *)
  2. m_imports : (string * string * int) list;
    (*

    Module name, import name, and function type index.

    *)
  3. m_funcs : func list;
    (*

    Ordered local functions.

    *)
  4. m_globals : global list;
  5. m_exports : export list;
  6. m_declared : int list;
    (*

    Local functions allowed as RefFunc operands.

    *)
  7. m_datas : string list;
    (*

    Ordered passive byte segments.

    *)
  8. m_customs : (string * string) list;
    (*

    Custom section names and raw payloads.

    *)
}

The binary emitter adds the import count to local function references, declarations, and exports. The IR does not itself validate indices or instruction typing.