wasm_exec (wasm v0.16.0)

View Source

The interpreter.

Nothing here is called directly; you arrive through wasm:call/3. Read it when you want to know why execution behaves as it does on the BEAM, or before you change the dispatch loop.

Continuation lists, not a program counter. WebAssembly control flow is structured: br N can only exit N enclosing blocks or jump back to a loop header. Nothing can jump to an arbitrary offset. So the instruction stream never needs to be flattened and there is no program counter at all. A block's body is a nested cons list, and entering one pushes a control frame holding the list to resume on exit.

This is the opposite of what a C runtime does, and the benchmarks are why. Walking a cons list measured 3.7 ns per instruction; the flattened bytecode-plus-index shape that every C interpreter uses measured 5.6 ns, because element/2 with a runtime index is a bounds-checked operation while matching a list head is a dereference the compiler turns into a jump table. Porting Wasmtime's IR shape would have cost about 35% for nothing.

Saved stack tails instead of heights. A control frame stores the operand stack list as it was on entry, not its depth. Leaving a block normally then costs nothing at all (validation guarantees the stack is already correct), and branching out is take(Arity) ++ SavedTail. Tracking an integer height would mean maintaining a counter on every push and pop.

Explicit call frames. Calls push onto frames rather than recursing through Erlang. Erlang has no first-class continuations, so recursion would make suspension impossible; explicit frames keep the whole execution state a plain term that can be captured, inspected from another process, and bounded.

Fuel. Charged at back-edges and calls only. Every unbounded execution has to pass through one or the other, so bounding those bounds everything while straight-line code pays nothing. Fuel is there for resource limits and metering, not for scheduler safety: every dispatch step here is an Erlang function call and therefore consumes a reduction, so the BEAM preempts this loop whether or not you enabled fuel. A pure-Erlang interpreter cannot monopolise a scheduler, which is precisely what a NIF-based one can.

Where things are

Three functions carry most of the work, and AGENTS.md singles them out because three separate changes to them have each cost about 70% on QuickJS while the synthetic loop measured nothing:

functionwhat it is
run/3the dispatch loop. One clause per IR instruction, in %%% dispatch
branch/3control flow: taking a label, unwinding to it, in %%% branches
do_call/4the call trampoline, in %%% calls

Anything you add on those three goes through bench/paths/realbench.erl on QuickJS, five interleaved pairs, both orderings, before you believe it.

The rest by banner: %%% api and %%% call entry are where an invocation starts and a function's frame is made, and generated code is entered from do_call/4, in %%% calls, when the invocation carries a compiled unit (the outermost entry is wasm_jit:entry/3, called from wasm); %%% bounded operand cache is the operand stack; %%% operations, for generated code holds the helpers wasm_core emits calls to, so the interpreted and compiled paths cannot disagree; %%% exceptions, %%% gc, %%% simd memory and %%% bulk memory are the proposals.

Most exports are the ABI of generated code

Of the 35 exports, 25 are there for the code wasm_core generates, which calls them; no other module in src/ does. 2 more are read by wasm_core while it generates:

groupexports
operations and globalsop1/2, op2/3, load_at/5, store_at/6, global_at/2, set_global_at/4, global_ref_at/2, set_global_ref_at/3
calls out of a compiled unitcall_out/7, shard_call/8, indirect_out/9, indirect_index/5, unreachable_at/7
the depth limitmax_depth/1, depth_exhausted/0
memorymemory_size_at/2, memory_grow_at/5, memory_fill_at/6, memory_copy_at/8, memory_init_at/8, data_drop_at/3
SIMD memorysimd_load_at/7, simd_store_at/6, simd_load_lane_at/9, simd_store_lane_at/8
read at generation timeload_spec/1, store_spec/1

Their names and arities are what generated code calls, so changing one changes generated code's shape: bump ABI in wasm_jit, as "Bump the ABI when generated code changes shape" in docs/compiled-tier-internals.md says. Of the rest, call/5, open_budget/1, close_budget/1, memory_limit/0 and fresh_mems/1 are called from wasm; init_state/4, check_depth/2 and table_func/3 have no caller outside this module.

Why it stays one module

The elvis rule against large modules is turned off for this module, and rebar.config gives the reason: "wasm_exec is one dispatcher on purpose: every extra call on that path has measured as a regression. See ATTEMPTS.md." AGENTS.md gives the evidence: three changes to run/3, branch/3 or do_call/4 have cost about 70% on QuickJS while the synthetic loop measured nothing or measured an improvement. The helpers for generated code stay here too, beside the interpreter code they share, so that the interpreted and compiled paths cannot disagree.

Summary

Functions

Invoke function FuncIdx with Args.

A call out of compiled code into an interpreted function.

Charge one level of call depth, or raise what enter/5 raises.

Take away a budget, restoring whatever an enclosing invocation had.

Drop a passive data segment, answering the new state.

Raise what check_depth/2 raises when the depth limit is reached.

The state with every memory handle brought up to date with the arena chunks published since it was taken.

Read a global that is an inline value rather than a shared cell.

Read a global that is a shared cell, as global_get_ref does.

The function a call_indirect reaches when it is in this instance, as its index, or foreign.

A memory load, for generated code. Bounds-checks and traps as the interpreter does.

The instance's call-depth limit.

Copy a range between two memories, or within one.

Fill a range of a memory with one byte.

Grow a memory, answering the old size and the new state.

Copy from a passive data segment into a memory.

The memory.grow that max_memory_pages refused during the running invocation, as #{max_pages := N, requested := P}, or undefined. P is the size in pages the grow asked its memory to reach; N is the instance's max_memory_pages.

The size of a memory in pages.

A unary operation on one operand value.

A binary operation on two operand values. Traps exactly as the interpreter does.

Install a budget for an outermost invocation, answering what to restore.

Write a global, and record the mutation so a trap does not lose it.

Write a global that is a shared cell, as global_set_ref does.

Call a function this generated module does not hold but a sibling does.

A vector load, for generated code.

A vector load into one lane of an existing vector.

A vector store, answering the state to go on with as store_at/6 does.

A store of one lane of a vector, answering the state to go on with.

A memory store, answering the state to go on with: the same one, or one whose memory handle has seen arena chunks this store published.

The function entry Idx of Table names, with its instance, for thread.new-indirect: the lookup and the traps of call_indirect, checked against Want, a plain signature, since the type a component names has no canonical id in any instance.

Trap unreachable from compiled code.

Types

heaptype()

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

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

call(Inst, Mut, FuncIdx, Args, Opts)

-spec call(#inst{id :: reference(),
                 ckpt :: reference(),
                 entry_key :: undefined | reference(),
                 types :: tuple(),
                 funcs :: tuple(),
                 exports :: #{binary() => {func | table | mem | global, non_neg_integer()}},
                 elems :: tuple(),
                 datas :: tuple(),
                 globaltypes :: tuple(),
                 tags :: tuple(),
                 canon :: tuple(),
                 fields :: tuple(),
                 kinds :: tuple(),
                 supers :: tuple(),
                 heap :: undefined | wasm_heap:heap(),
                 identity :: undefined | {sha256, binary()} | reference(),
                 module_handle :: undefined | wasm_module_cache:handle(),
                 leases :: undefined | atomics:atomics_ref(),
                 store :: term(),
                 version :: term(),
                 limits :: map(),
                 ctx :: term(),
                 shared_ir :: undefined | {wasm_ir_shared, binary(), term()}},
           #mut{globals :: tuple(),
                tables :: tuple(),
                mems :: tuple(),
                dropped_elems :: map(),
                dropped_datas :: map()},
           non_neg_integer(),
           [term()],
           map()) ->
              {ok,
               [term()],
               #mut{globals :: tuple(),
                    tables :: tuple(),
                    mems :: tuple(),
                    dropped_elems :: map(),
                    dropped_datas :: map()}}.

Invoke function FuncIdx with Args.

call_out(Inst, Mut, Idx, Args, Depth, Mod, Gen)

-spec call_out(#inst{id :: reference(),
                     ckpt :: reference(),
                     entry_key :: undefined | reference(),
                     types :: tuple(),
                     funcs :: tuple(),
                     exports :: #{binary() => {func | table | mem | global, non_neg_integer()}},
                     elems :: tuple(),
                     datas :: tuple(),
                     globaltypes :: tuple(),
                     tags :: tuple(),
                     canon :: tuple(),
                     fields :: tuple(),
                     kinds :: tuple(),
                     supers :: tuple(),
                     heap :: undefined | wasm_heap:heap(),
                     identity :: undefined | {sha256, binary()} | reference(),
                     module_handle :: undefined | wasm_module_cache:handle(),
                     leases :: undefined | atomics:atomics_ref(),
                     store :: term(),
                     version :: term(),
                     limits :: map(),
                     ctx :: term(),
                     shared_ir :: undefined | {wasm_ir_shared, binary(), term()}},
               #mut{globals :: tuple(),
                    tables :: tuple(),
                    mems :: tuple(),
                    dropped_elems :: map(),
                    dropped_datas :: map()},
               non_neg_integer(),
               [term()],
               non_neg_integer(),
               module(),
               non_neg_integer()) ->
                  {[term()],
                   #mut{globals :: tuple(),
                        tables :: tuple(),
                        mems :: tuple(),
                        dropped_elems :: map(),
                        dropped_datas :: map()}}.

A call out of compiled code into an interpreted function.

call/5 rebuilds fuel and depth from the ?BUDGET process dictionary (init_state/4) and so does not inherit a compiled caller's depth. This takes it explicitly, which is what keeps max_depth meaning the same thing on both sides of the crossing.

Priced before it was written, in bench/paths/callcost.erl: 37.4 to 43.4 ns against the interpreter's own 43.5 to 44.7 for the same call.

check_depth(Inst, Depth)

-spec check_depth(#inst{id :: reference(),
                        ckpt :: reference(),
                        entry_key :: undefined | reference(),
                        types :: tuple(),
                        funcs :: tuple(),
                        exports :: #{binary() => {func | table | mem | global, non_neg_integer()}},
                        elems :: tuple(),
                        datas :: tuple(),
                        globaltypes :: tuple(),
                        tags :: tuple(),
                        canon :: tuple(),
                        fields :: tuple(),
                        kinds :: tuple(),
                        supers :: tuple(),
                        heap :: undefined | wasm_heap:heap(),
                        identity :: undefined | {sha256, binary()} | reference(),
                        module_handle :: undefined | wasm_module_cache:handle(),
                        leases :: undefined | atomics:atomics_ref(),
                        store :: term(),
                        version :: term(),
                        limits :: map(),
                        ctx :: term(),
                        shared_ir :: undefined | {wasm_ir_shared, binary(), term()}},
                  non_neg_integer()) ->
                     ok.

Charge one level of call depth, or raise what enter/5 raises.

Compiled recursion runs on the Erlang stack, which grows on the process heap rather than overflowing, so without this a runaway one exhausts memory instead of trapping.

close_budget/1

-spec close_budget(term()) -> ok.

Take away a budget, restoring whatever an enclosing invocation had.

data_drop_at/3

-spec data_drop_at(#inst{id :: reference(),
                         ckpt :: reference(),
                         entry_key :: undefined | reference(),
                         types :: tuple(),
                         funcs :: tuple(),
                         exports :: #{binary() => {func | table | mem | global, non_neg_integer()}},
                         elems :: tuple(),
                         datas :: tuple(),
                         globaltypes :: tuple(),
                         tags :: tuple(),
                         canon :: tuple(),
                         fields :: tuple(),
                         kinds :: tuple(),
                         supers :: tuple(),
                         heap :: undefined | wasm_heap:heap(),
                         identity :: undefined | {sha256, binary()} | reference(),
                         module_handle :: undefined | wasm_module_cache:handle(),
                         leases :: undefined | atomics:atomics_ref(),
                         store :: term(),
                         version :: term(),
                         limits :: map(),
                         ctx :: term(),
                         shared_ir :: undefined | {wasm_ir_shared, binary(), term()}},
                   #mut{globals :: tuple(),
                        tables :: tuple(),
                        mems :: tuple(),
                        dropped_elems :: map(),
                        dropped_datas :: map()},
                   non_neg_integer()) ->
                      #mut{globals :: tuple(),
                           tables :: tuple(),
                           mems :: tuple(),
                           dropped_elems :: map(),
                           dropped_datas :: map()}.

Drop a passive data segment, answering the new state.

depth_exhausted()

-spec depth_exhausted() -> no_return().

Raise what check_depth/2 raises when the depth limit is reached.

Generated code calls it only once its inline comparison has found no frames left, so this is the error path and never the call path. The context carries no depth: generated code counts the frames left, not the depth, and a calls-only function does not know the limit.

fresh_mems/1

-spec fresh_mems(#mut{globals :: tuple(),
                      tables :: tuple(),
                      mems :: tuple(),
                      dropped_elems :: map(),
                      dropped_datas :: map()}) ->
                    #mut{globals :: tuple(),
                         tables :: tuple(),
                         mems :: tuple(),
                         dropped_elems :: map(),
                         dropped_datas :: map()}.

The state with every memory handle brought up to date with the arena chunks published since it was taken.

For where a state is picked up after writes it did not make: the return from a host function, and the start of an invocation, which follows whatever wasm:write_memory/3 did in between.

global_at(Mu, I)

-spec global_at(#mut{globals :: tuple(),
                     tables :: tuple(),
                     mems :: tuple(),
                     dropped_elems :: map(),
                     dropped_datas :: map()},
                non_neg_integer()) ->
                   term().

Read a global that is an inline value rather than a shared cell.

global_ref_at(Mu, I)

-spec global_ref_at(#mut{globals :: tuple(),
                         tables :: tuple(),
                         mems :: tuple(),
                         dropped_elems :: map(),
                         dropped_datas :: map()},
                    non_neg_integer()) ->
                       term().

Read a global that is a shared cell, as global_get_ref does.

indirect_index(TypeIdx, TableIdx, I, Inst, Mut)

-spec indirect_index(non_neg_integer(),
                     non_neg_integer(),
                     term(),
                     #inst{id :: reference(),
                           ckpt :: reference(),
                           entry_key :: undefined | reference(),
                           types :: tuple(),
                           funcs :: tuple(),
                           exports :: #{binary() => {func | table | mem | global, non_neg_integer()}},
                           elems :: tuple(),
                           datas :: tuple(),
                           globaltypes :: tuple(),
                           tags :: tuple(),
                           canon :: tuple(),
                           fields :: tuple(),
                           kinds :: tuple(),
                           supers :: tuple(),
                           heap :: undefined | wasm_heap:heap(),
                           identity :: undefined | {sha256, binary()} | reference(),
                           module_handle :: undefined | wasm_module_cache:handle(),
                           leases :: undefined | atomics:atomics_ref(),
                           store :: term(),
                           version :: term(),
                           limits :: map(),
                           ctx :: term(),
                           shared_ir :: undefined | {wasm_ir_shared, binary(), term()}},
                     #mut{globals :: tuple(),
                          tables :: tuple(),
                          mems :: tuple(),
                          dropped_elems :: map(),
                          dropped_datas :: map()}) ->
                        non_neg_integer() | foreign.

The function a call_indirect reaches when it is in this instance, as its index, or foreign.

indirect_target/5 does the resolution, bounds, null and the type check, so the traps are the interpreter's. Generated code calls a function of its own unit directly from the index, and hands anything else to indirect_out/9.

indirect_out(Inst, Mut, TypeIdx, TableIdx, I, Args, Depth, Mod, Gen)

-spec indirect_out(#inst{id :: reference(),
                         ckpt :: reference(),
                         entry_key :: undefined | reference(),
                         types :: tuple(),
                         funcs :: tuple(),
                         exports :: #{binary() => {func | table | mem | global, non_neg_integer()}},
                         elems :: tuple(),
                         datas :: tuple(),
                         globaltypes :: tuple(),
                         tags :: tuple(),
                         canon :: tuple(),
                         fields :: tuple(),
                         kinds :: tuple(),
                         supers :: tuple(),
                         heap :: undefined | wasm_heap:heap(),
                         identity :: undefined | {sha256, binary()} | reference(),
                         module_handle :: undefined | wasm_module_cache:handle(),
                         leases :: undefined | atomics:atomics_ref(),
                         store :: term(),
                         version :: term(),
                         limits :: map(),
                         ctx :: term(),
                         shared_ir :: undefined | {wasm_ir_shared, binary(), term()}},
                   #mut{globals :: tuple(),
                        tables :: tuple(),
                        mems :: tuple(),
                        dropped_elems :: map(),
                        dropped_datas :: map()},
                   non_neg_integer(),
                   non_neg_integer(),
                   term(),
                   [term()],
                   non_neg_integer(),
                   module(),
                   non_neg_integer()) ->
                      {[term()],
                       #mut{globals :: tuple(),
                            tables :: tuple(),
                            mems :: tuple(),
                            dropped_elems :: map(),
                            dropped_datas :: map()}}.

An indirect call out of compiled code.

indirect_target/4 already does the whole resolution -- bounds, null, and the type check against the canonical type -- and already tells a target in this instance from one in another, so it is reused rather than restated and the three traps it raises stay identical. It wants an #st{}, and the two fields it reads are the two given here.

A target compiled in the caller's own unit does not come here: generated code resolves it with indirect_index/5 and calls it through a dispatcher local to the module. This takes what is left, a foreign target, and is the fallback.

init_state(Inst, Mut, Args, Opts)

-spec init_state(#inst{id :: reference(),
                       ckpt :: reference(),
                       entry_key :: undefined | reference(),
                       types :: tuple(),
                       funcs :: tuple(),
                       exports :: #{binary() => {func | table | mem | global, non_neg_integer()}},
                       elems :: tuple(),
                       datas :: tuple(),
                       globaltypes :: tuple(),
                       tags :: tuple(),
                       canon :: tuple(),
                       fields :: tuple(),
                       kinds :: tuple(),
                       supers :: tuple(),
                       heap :: undefined | wasm_heap:heap(),
                       identity :: undefined | {sha256, binary()} | reference(),
                       module_handle :: undefined | wasm_module_cache:handle(),
                       leases :: undefined | atomics:atomics_ref(),
                       store :: term(),
                       version :: term(),
                       limits :: map(),
                       ctx :: term(),
                       shared_ir :: undefined | {wasm_ir_shared, binary(), term()}},
                 #mut{globals :: tuple(),
                      tables :: tuple(),
                      mems :: tuple(),
                      dropped_elems :: map(),
                      dropped_datas :: map()},
                 [term()],
                 map()) ->
                    #st{stack :: [term()],
                        locals :: tuple(),
                        frames :: [tuple() | toplevel],
                        inst ::
                            #inst{id :: reference(),
                                  ckpt :: reference(),
                                  entry_key :: undefined | reference(),
                                  types :: tuple(),
                                  funcs :: tuple(),
                                  exports ::
                                      #{binary() => {func | table | mem | global, non_neg_integer()}},
                                  elems :: tuple(),
                                  datas :: tuple(),
                                  globaltypes :: tuple(),
                                  tags :: tuple(),
                                  canon :: tuple(),
                                  fields :: tuple(),
                                  kinds :: tuple(),
                                  supers :: tuple(),
                                  heap :: undefined | wasm_heap:heap(),
                                  identity :: undefined | {sha256, binary()} | reference(),
                                  module_handle :: undefined | wasm_module_cache:handle(),
                                  leases :: undefined | atomics:atomics_ref(),
                                  store :: term(),
                                  version :: term(),
                                  limits :: map(),
                                  ctx :: term(),
                                  shared_ir :: undefined | {wasm_ir_shared, binary(), term()}},
                        mut ::
                            #mut{globals :: tuple(),
                                 tables :: tuple(),
                                 mems :: tuple(),
                                 dropped_elems :: map(),
                                 dropped_datas :: map()},
                        fuel :: non_neg_integer() | infinity,
                        depth :: non_neg_integer(),
                        max_depth :: pos_integer(),
                        code :: undefined | {module(), non_neg_integer()}}.

load_at(Mu, M, N, Kind, Addr)

-spec load_at(#mut{globals :: tuple(),
                   tables :: tuple(),
                   mems :: tuple(),
                   dropped_elems :: map(),
                   dropped_datas :: map()},
              non_neg_integer(),
              pos_integer(),
              atom(),
              non_neg_integer()) ->
                 term().

A memory load, for generated code. Bounds-checks and traps as the interpreter does.

load_spec/1

max_depth/1

-spec max_depth(#inst{id :: reference(),
                      ckpt :: reference(),
                      entry_key :: undefined | reference(),
                      types :: tuple(),
                      funcs :: tuple(),
                      exports :: #{binary() => {func | table | mem | global, non_neg_integer()}},
                      elems :: tuple(),
                      datas :: tuple(),
                      globaltypes :: tuple(),
                      tags :: tuple(),
                      canon :: tuple(),
                      fields :: tuple(),
                      kinds :: tuple(),
                      supers :: tuple(),
                      heap :: undefined | wasm_heap:heap(),
                      identity :: undefined | {sha256, binary()} | reference(),
                      module_handle :: undefined | wasm_module_cache:handle(),
                      leases :: undefined | atomics:atomics_ref(),
                      store :: term(),
                      version :: term(),
                      limits :: map(),
                      ctx :: term(),
                      shared_ir :: undefined | {wasm_ir_shared, binary(), term()}}) ->
                   non_neg_integer().

The instance's call-depth limit.

Generated code reads it once per entry, at invoke/6, and compares against it inline before every call; check_depth/2 reads the same value.

memory_copy_at/8

-spec memory_copy_at(#mut{globals :: tuple(),
                          tables :: tuple(),
                          mems :: tuple(),
                          dropped_elems :: map(),
                          dropped_datas :: map()},
                     non_neg_integer(),
                     non_neg_integer(),
                     term(),
                     term(),
                     term(),
                     32 | 64,
                     32 | 64) ->
                        #mut{globals :: tuple(),
                             tables :: tuple(),
                             mems :: tuple(),
                             dropped_elems :: map(),
                             dropped_datas :: map()}.

Copy a range between two memories, or within one.

memory_fill_at(Mu, M, D, B, N, W)

-spec memory_fill_at(#mut{globals :: tuple(),
                          tables :: tuple(),
                          mems :: tuple(),
                          dropped_elems :: map(),
                          dropped_datas :: map()},
                     non_neg_integer(),
                     term(),
                     term(),
                     term(),
                     32 | 64) ->
                        #mut{globals :: tuple(),
                             tables :: tuple(),
                             mems :: tuple(),
                             dropped_elems :: map(),
                             dropped_datas :: map()}.

Fill a range of a memory with one byte.

memory_grow_at/5

-spec memory_grow_at(#inst{id :: reference(),
                           ckpt :: reference(),
                           entry_key :: undefined | reference(),
                           types :: tuple(),
                           funcs :: tuple(),
                           exports :: #{binary() => {func | table | mem | global, non_neg_integer()}},
                           elems :: tuple(),
                           datas :: tuple(),
                           globaltypes :: tuple(),
                           tags :: tuple(),
                           canon :: tuple(),
                           fields :: tuple(),
                           kinds :: tuple(),
                           supers :: tuple(),
                           heap :: undefined | wasm_heap:heap(),
                           identity :: undefined | {sha256, binary()} | reference(),
                           module_handle :: undefined | wasm_module_cache:handle(),
                           leases :: undefined | atomics:atomics_ref(),
                           store :: term(),
                           version :: term(),
                           limits :: map(),
                           ctx :: term(),
                           shared_ir :: undefined | {wasm_ir_shared, binary(), term()}},
                     #mut{globals :: tuple(),
                          tables :: tuple(),
                          mems :: tuple(),
                          dropped_elems :: map(),
                          dropped_datas :: map()},
                     non_neg_integer(),
                     term(),
                     32 | 64) ->
                        {integer(),
                         #mut{globals :: tuple(),
                              tables :: tuple(),
                              mems :: tuple(),
                              dropped_elems :: map(),
                              dropped_datas :: map()}}.

Grow a memory, answering the old size and the new state.

memory_init_at(Inst, Mu, D, M, Da, So, N, W)

-spec memory_init_at(#inst{id :: reference(),
                           ckpt :: reference(),
                           entry_key :: undefined | reference(),
                           types :: tuple(),
                           funcs :: tuple(),
                           exports :: #{binary() => {func | table | mem | global, non_neg_integer()}},
                           elems :: tuple(),
                           datas :: tuple(),
                           globaltypes :: tuple(),
                           tags :: tuple(),
                           canon :: tuple(),
                           fields :: tuple(),
                           kinds :: tuple(),
                           supers :: tuple(),
                           heap :: undefined | wasm_heap:heap(),
                           identity :: undefined | {sha256, binary()} | reference(),
                           module_handle :: undefined | wasm_module_cache:handle(),
                           leases :: undefined | atomics:atomics_ref(),
                           store :: term(),
                           version :: term(),
                           limits :: map(),
                           ctx :: term(),
                           shared_ir :: undefined | {wasm_ir_shared, binary(), term()}},
                     #mut{globals :: tuple(),
                          tables :: tuple(),
                          mems :: tuple(),
                          dropped_elems :: map(),
                          dropped_datas :: map()},
                     non_neg_integer(),
                     non_neg_integer(),
                     term(),
                     term(),
                     term(),
                     32 | 64) ->
                        #mut{globals :: tuple(),
                             tables :: tuple(),
                             mems :: tuple(),
                             dropped_elems :: map(),
                             dropped_datas :: map()}.

Copy from a passive data segment into a memory.

memory_limit()

-spec memory_limit() ->
                      #{max_pages := non_neg_integer() | undefined, requested := non_neg_integer()} |
                      undefined.

The memory.grow that max_memory_pages refused during the running invocation, as #{max_pages := N, requested := P}, or undefined. P is the size in pages the grow asked its memory to reach; N is the instance's max_memory_pages.

memory_size_at(Mu, M)

-spec memory_size_at(#mut{globals :: tuple(),
                          tables :: tuple(),
                          mems :: tuple(),
                          dropped_elems :: map(),
                          dropped_datas :: map()},
                     non_neg_integer()) ->
                        non_neg_integer().

The size of a memory in pages.

op1/2

-spec op1(atom(), term()) -> term().

A unary operation on one operand value.

op2(Op, A, B)

-spec op2(atom(), term(), term()) -> term().

A binary operation on two operand values. Traps exactly as the interpreter does.

open_budget(Opts)

-spec open_budget(map()) -> term().

Install a budget for an outermost invocation, answering what to restore.

wasm:call/4 is the only caller. A nested one inherits what is already there.

set_global_at/4

-spec set_global_at(#inst{id :: reference(),
                          ckpt :: reference(),
                          entry_key :: undefined | reference(),
                          types :: tuple(),
                          funcs :: tuple(),
                          exports :: #{binary() => {func | table | mem | global, non_neg_integer()}},
                          elems :: tuple(),
                          datas :: tuple(),
                          globaltypes :: tuple(),
                          tags :: tuple(),
                          canon :: tuple(),
                          fields :: tuple(),
                          kinds :: tuple(),
                          supers :: tuple(),
                          heap :: undefined | wasm_heap:heap(),
                          identity :: undefined | {sha256, binary()} | reference(),
                          module_handle :: undefined | wasm_module_cache:handle(),
                          leases :: undefined | atomics:atomics_ref(),
                          store :: term(),
                          version :: term(),
                          limits :: map(),
                          ctx :: term(),
                          shared_ir :: undefined | {wasm_ir_shared, binary(), term()}},
                    #mut{globals :: tuple(),
                         tables :: tuple(),
                         mems :: tuple(),
                         dropped_elems :: map(),
                         dropped_datas :: map()},
                    non_neg_integer(),
                    term()) ->
                       #mut{globals :: tuple(),
                            tables :: tuple(),
                            mems :: tuple(),
                            dropped_elems :: map(),
                            dropped_datas :: map()}.

Write a global, and record the mutation so a trap does not lose it.

The checkpoint is the reason this is a named helper rather than a setelement/3 open-coded in Core: every store mutation has to be visible to the invocation's catch, or what a trapping call wrote is silently rolled back in compiled code and kept in interpreted code. A mutable i32 or i64 global nothing else sees does not come here: it is a slot, which generated code writes in place.

set_global_ref_at(Mu, I, V)

-spec set_global_ref_at(#mut{globals :: tuple(),
                             tables :: tuple(),
                             mems :: tuple(),
                             dropped_elems :: map(),
                             dropped_datas :: map()},
                        non_neg_integer(),
                        term()) ->
                           ok.

Write a global that is a shared cell, as global_set_ref does.

No checkpoint, as in the interpreter: the write goes to the cell, which every instance sharing it already sees, so a trap after it has nothing to keep.

shard_call(Inst, Mut, Idx, Args, Depth, Target, Head, Gen)

-spec shard_call(#inst{id :: reference(),
                       ckpt :: reference(),
                       entry_key :: undefined | reference(),
                       types :: tuple(),
                       funcs :: tuple(),
                       exports :: #{binary() => {func | table | mem | global, non_neg_integer()}},
                       elems :: tuple(),
                       datas :: tuple(),
                       globaltypes :: tuple(),
                       tags :: tuple(),
                       canon :: tuple(),
                       fields :: tuple(),
                       kinds :: tuple(),
                       supers :: tuple(),
                       heap :: undefined | wasm_heap:heap(),
                       identity :: undefined | {sha256, binary()} | reference(),
                       module_handle :: undefined | wasm_module_cache:handle(),
                       leases :: undefined | atomics:atomics_ref(),
                       store :: term(),
                       version :: term(),
                       limits :: map(),
                       ctx :: term(),
                       shared_ir :: undefined | {wasm_ir_shared, binary(), term()}},
                 #mut{globals :: tuple(),
                      tables :: tuple(),
                      mems :: tuple(),
                      dropped_elems :: map(),
                      dropped_datas :: map()},
                 non_neg_integer(),
                 [term()],
                 non_neg_integer(),
                 module(),
                 module(),
                 non_neg_integer() | binary()) ->
                    {[term()],
                     #mut{globals :: tuple(),
                          tables :: tuple(),
                          mems :: tuple(),
                          dropped_elems :: map(),
                          dropped_datas :: map()}}.

Call a function this generated module does not hold but a sibling does.

One wasm module may be compiled into several generated ones, and a call between them is a call, not a crossing: which unit holds which function was decided before either was generated, so the name is a literal exactly as the caller's own is. Going out through the interpreter and back in through the head of the chain instead measured 268.5 ms against 174.4 on QuickJS.

{error, _} means the sibling's slot was refilled between generation and now, which its stamp check catches. The crossing is the right answer to that, and it is the same one a caller with no sibling at all gets.

simd_load_at(Mu, M, Op, Offset, W, N, Base)

-spec simd_load_at(#mut{globals :: tuple(),
                        tables :: tuple(),
                        mems :: tuple(),
                        dropped_elems :: map(),
                        dropped_datas :: map()},
                   non_neg_integer(),
                   atom(),
                   non_neg_integer(),
                   32 | 64,
                   pos_integer(),
                   term()) ->
                      term().

A vector load, for generated code.

simd_load_lane_at(Mu, M, Op, Offset, W, N, Lane, Base, V)

-spec simd_load_lane_at(#mut{globals :: tuple(),
                             tables :: tuple(),
                             mems :: tuple(),
                             dropped_elems :: map(),
                             dropped_datas :: map()},
                        non_neg_integer(),
                        atom(),
                        non_neg_integer(),
                        32 | 64,
                        pos_integer(),
                        non_neg_integer(),
                        term(),
                        term()) ->
                           term().

A vector load into one lane of an existing vector.

simd_store_at(Mu, M, Offset, W, Base, V)

-spec simd_store_at(#mut{globals :: tuple(),
                         tables :: tuple(),
                         mems :: tuple(),
                         dropped_elems :: map(),
                         dropped_datas :: map()},
                    non_neg_integer(),
                    non_neg_integer(),
                    32 | 64,
                    term(),
                    term()) ->
                       #mut{globals :: tuple(),
                            tables :: tuple(),
                            mems :: tuple(),
                            dropped_elems :: map(),
                            dropped_datas :: map()}.

A vector store, answering the state to go on with as store_at/6 does.

simd_store_lane_at(Mu, M, Op, Offset, W, Lane, Base, V)

-spec simd_store_lane_at(#mut{globals :: tuple(),
                              tables :: tuple(),
                              mems :: tuple(),
                              dropped_elems :: map(),
                              dropped_datas :: map()},
                         non_neg_integer(),
                         atom(),
                         non_neg_integer(),
                         32 | 64,
                         non_neg_integer(),
                         term(),
                         term()) ->
                            #mut{globals :: tuple(),
                                 tables :: tuple(),
                                 mems :: tuple(),
                                 dropped_elems :: map(),
                                 dropped_datas :: map()}.

A store of one lane of a vector, answering the state to go on with.

store_at(Mu, M, N, Kind, Addr, Value)

-spec store_at(#mut{globals :: tuple(),
                    tables :: tuple(),
                    mems :: tuple(),
                    dropped_elems :: map(),
                    dropped_datas :: map()},
               non_neg_integer(),
               pos_integer(),
               atom(),
               non_neg_integer(),
               term()) ->
                  #mut{globals :: tuple(),
                       tables :: tuple(),
                       mems :: tuple(),
                       dropped_elems :: map(),
                       dropped_datas :: map()}.

A memory store, answering the state to go on with: the same one, or one whose memory handle has seen arena chunks this store published.

store_spec/1

table_func(Table, Idx, Want)

-spec table_func(wasm_table:table(),
                 non_neg_integer(),
                 #functype{params :: [valtype()], results :: [valtype()]}) ->
                    {ok,
                     #inst{id :: reference(),
                           ckpt :: reference(),
                           entry_key :: undefined | reference(),
                           types :: tuple(),
                           funcs :: tuple(),
                           exports :: #{binary() => {func | table | mem | global, non_neg_integer()}},
                           elems :: tuple(),
                           datas :: tuple(),
                           globaltypes :: tuple(),
                           tags :: tuple(),
                           canon :: tuple(),
                           fields :: tuple(),
                           kinds :: tuple(),
                           supers :: tuple(),
                           heap :: undefined | wasm_heap:heap(),
                           identity :: undefined | {sha256, binary()} | reference(),
                           module_handle :: undefined | wasm_module_cache:handle(),
                           leases :: undefined | atomics:atomics_ref(),
                           store :: term(),
                           version :: term(),
                           limits :: map(),
                           ctx :: term(),
                           shared_ir :: undefined | {wasm_ir_shared, binary(), term()}},
                     non_neg_integer()} |
                    {trap, atom()}.

The function entry Idx of Table names, with its instance, for thread.new-indirect: the lookup and the traps of call_indirect, checked against Want, a plain signature, since the type a component names has no canonical id in any instance.

unreachable_at(Mut, P1, A1, B1, P2, A2, B2)

-spec unreachable_at(#mut{globals :: tuple(),
                          tables :: tuple(),
                          mems :: tuple(),
                          dropped_elems :: map(),
                          dropped_datas :: map()},
                     term(),
                     term(),
                     term(),
                     term(),
                     term(),
                     term()) ->
                        no_return().

Trap unreachable from compiled code.

The arguments are the state and memory 0's translation cache, and are not read. Passing them makes the trap a use of them, which the generator's ordering of an access's results depends on: see wasm_core's private answer/3.