wasm_decode_code (wasm v0.1.0)

View Source

Instruction and expression decoding.

Read this before you add an opcode. Block bodies nest directly rather than being flattened with end markers. WebAssembly control flow is structured, so nesting is lossless, and it lets wasm_ir build continuation lists without a separate label-resolution pass. A flat stream would have to be re-scanned to pair every block with its end anyway.

The opcode dispatch is a dense integer match, which the BEAM compiler turns into a jump table. Writing one clause per opcode is verbose but it is the shape the compiler optimises best, and it keeps each opcode's immediate operands visible at the point of decoding.

Summary

Functions

Block signature. Value types are encoded as negative s33 values, type indices as non-negative ones, so a single signed read distinguishes them.

Decode an expression: instructions up to and including the end byte.

Types

blocktype()

-type blocktype() :: empty | {valtype, valtype()} | {typeidx, typeidx()}.

heaptype()

-type heaptype() ::
          func | extern | exn | any | eq | i31 | struct | array | nofunc | noextern | noexn | none |
          {type, typeidx()}.

instr()

-type instr() :: atom() | tuple().

numtype()

-type numtype() :: i32 | i64 | f32 | f64.

reftype()

-type reftype() :: {ref, null | nonull, heaptype()}.

typeidx()

-type typeidx() :: non_neg_integer().

valtype()

-type valtype() :: numtype() | vectype() | reftype().

vectype()

-type vectype() :: v128.

Functions

blocktype(Bin)

-spec blocktype(binary()) -> {blocktype(), binary()}.

Block signature. Value types are encoded as negative s33 values, type indices as non-negative ones, so a single signed read distinguishes them.

expr(Bin)

-spec expr(binary()) -> {[instr()], binary()}.

Decode an expression: instructions up to and including the end byte.

instrs(Bin)

-spec instrs(binary()) -> {[instr()], end_ | else_, binary()}.

mem_arg(Bin, Op)