Runtime Structure

Store, stack, and other runtime structure forming the WebAssembly abstract machine, such as values or module instances, are made precise in terms of additional auxiliary syntax.

Values

WebAssembly computations manipulate values of the four basic value types: integers and floating-point data of 32 or 64 bit width each, respectively.

In most places of the semantics, values of different types can occur. In order to avoid ambiguities, values are therefore represented with an abstract syntax that makes their type explicit. It is convenient to reuse the same notation as for the \(\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}\) instructions producing them:

\[\begin{split}\begin{array}{llcl} \def\mathdef1555#1{{}}\mathdef1555{(value)} & \href{../exec/runtime.html#syntax-val}{\mathit{val}} &::=& \href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~\href{../syntax/values.html#syntax-int}{\def\mathdef1571#1{{\mathit{i}#1}}\mathdef1571{\mathit{32}}} \\&&|& \href{../syntax/types.html#syntax-valtype}{\mathsf{i64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~\href{../syntax/values.html#syntax-int}{\def\mathdef1572#1{{\mathit{i}#1}}\mathdef1572{\mathit{64}}} \\&&|& \href{../syntax/types.html#syntax-valtype}{\mathsf{f32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~\href{../syntax/values.html#syntax-float}{\def\mathdef1575#1{{\mathit{f}#1}}\mathdef1575{\mathit{32}}} \\&&|& \href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~\href{../syntax/values.html#syntax-float}{\def\mathdef1576#1{{\mathit{f}#1}}\mathdef1576{\mathit{64}}} \end{array}\end{split}\]

Store

The store represents all global state that can be manipulated by WebAssembly programs. It consists of the runtime representation of all instances of functions, tables, memories, and globals that have been allocated during the life time of the abstract machine. [1]

Syntactically, the store is defined as a record listing the existing instances of each category:

\[\begin{split}\begin{array}{llll} \def\mathdef1555#1{{}}\mathdef1555{(store)} & \href{../exec/runtime.html#syntax-store}{\mathit{store}} &::=& \{~ \begin{array}[t]{l@{~}ll} \href{../exec/runtime.html#syntax-store}{\mathsf{funcs}} & \href{../exec/runtime.html#syntax-funcinst}{\mathit{funcinst}}^\ast, \\ \href{../exec/runtime.html#syntax-store}{\mathsf{tables}} & \href{../exec/runtime.html#syntax-tableinst}{\mathit{tableinst}}^\ast, \\ \href{../exec/runtime.html#syntax-store}{\mathsf{mems}} & \href{../exec/runtime.html#syntax-meminst}{\mathit{meminst}}^\ast, \\ \href{../exec/runtime.html#syntax-store}{\mathsf{globals}} & \href{../exec/runtime.html#syntax-globalinst}{\mathit{globalinst}}^\ast ~\} \\ \end{array} \end{array}\end{split}\]
[1]In practice, implementations may apply techniques like garbage collection to remove objects from the store that are no longer referenced. However, such techniques are not semantically observable, and hence outside the scope of this specification.

Convention

  • The meta variable \(S\) ranges over stores where clear from context.

Addresses

Function instances, table instances, memory instances, and global instances in the store are referenced with abstract addresses. These are simply indices into the respective store component.

\[\begin{split}\begin{array}{llll} \def\mathdef1555#1{{}}\mathdef1555{(address)} & \href{../exec/runtime.html#syntax-addr}{\mathit{addr}} &::=& 0 ~|~ 1 ~|~ 2 ~|~ \dots \\ \def\mathdef1555#1{{}}\mathdef1555{(function address)} & \href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}} &::=& \href{../exec/runtime.html#syntax-addr}{\mathit{addr}} \\ \def\mathdef1555#1{{}}\mathdef1555{(table address)} & \href{../exec/runtime.html#syntax-tableaddr}{\mathit{tableaddr}} &::=& \href{../exec/runtime.html#syntax-addr}{\mathit{addr}} \\ \def\mathdef1555#1{{}}\mathdef1555{(memory address)} & \href{../exec/runtime.html#syntax-memaddr}{\mathit{memaddr}} &::=& \href{../exec/runtime.html#syntax-addr}{\mathit{addr}} \\ \def\mathdef1555#1{{}}\mathdef1555{(global address)} & \href{../exec/runtime.html#syntax-globaladdr}{\mathit{globaladdr}} &::=& \href{../exec/runtime.html#syntax-addr}{\mathit{addr}} \\ \end{array}\end{split}\]

An embedder may assign identity to exported store objects corresponding to their addresses, even where this identity is not observable from within WebAssembly code itself (such as for function instances or immutable globals).

Note

Addresses are dynamic, globally unique references to runtime objects, in contrast to indices, which are static, module-local references to their original definitions. A memory address \(\href{../exec/runtime.html#syntax-memaddr}{\mathit{memaddr}}\) denotes the abstract address of a memory instance in the store, not an offset inside a memory instance.

There is no specific limit on the number of allocations of store objects, hence logical addresses can be arbitrarily large natural numbers.

Module Instances

A module instance is the runtime representation of a module. It is created by instantiating a module, and collects runtime representations of all entities that are imported, defined, or exported by the module.

\[\begin{split}\begin{array}{llll} \def\mathdef1555#1{{}}\mathdef1555{(module instance)} & \href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}} &::=& \{ \begin{array}[t]{l@{~}ll} \href{../exec/runtime.html#syntax-moduleinst}{\mathsf{types}} & \href{../syntax/types.html#syntax-functype}{\mathit{functype}}^\ast, \\ \href{../exec/runtime.html#syntax-moduleinst}{\mathsf{funcaddrs}} & \href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}}^\ast, \\ \href{../exec/runtime.html#syntax-moduleinst}{\mathsf{tableaddrs}} & \href{../exec/runtime.html#syntax-tableaddr}{\mathit{tableaddr}}^\ast, \\ \href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}} & \href{../exec/runtime.html#syntax-memaddr}{\mathit{memaddr}}^\ast, \\ \href{../exec/runtime.html#syntax-moduleinst}{\mathsf{globaladdrs}} & \href{../exec/runtime.html#syntax-globaladdr}{\mathit{globaladdr}}^\ast, \\ \href{../exec/runtime.html#syntax-moduleinst}{\mathsf{exports}} & \href{../exec/runtime.html#syntax-exportinst}{\mathit{exportinst}}^\ast ~\} \\ \end{array} \end{array}\end{split}\]

Each component references runtime instances corresponding to respective declarations from the original module – whether imported or defined – in the order of their static indices. Function instances, table instances, memory instances, and global instances are referenced with an indirection through their respective addresses in the store.

It is an invariant of the semantics that all export instances in a given module instance have different names.

Function Instances

A function instance is the runtime representation of a function. It effectively is a closure of the original function over the runtime module instance of its originating module. The module instance is used to resolve references to other definitions during execution of the function.

\[\begin{split}\begin{array}{llll} \def\mathdef1555#1{{}}\mathdef1555{(function instance)} & \href{../exec/runtime.html#syntax-funcinst}{\mathit{funcinst}} &::=& \{ \href{../exec/runtime.html#syntax-funcinst}{\mathsf{type}}~\href{../syntax/types.html#syntax-functype}{\mathit{functype}}, \href{../exec/runtime.html#syntax-funcinst}{\mathsf{module}}~\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}, \href{../exec/runtime.html#syntax-funcinst}{\mathsf{code}}~\href{../syntax/modules.html#syntax-func}{\mathit{func}} \} \\ &&|& \{ \href{../exec/runtime.html#syntax-funcinst}{\mathsf{type}}~\href{../syntax/types.html#syntax-functype}{\mathit{functype}}, \href{../exec/runtime.html#syntax-funcinst}{\mathsf{hostcode}}~\href{../exec/runtime.html#syntax-hostfunc}{\mathit{hostfunc}} \} \\ \def\mathdef1555#1{{}}\mathdef1555{(host function)} & \href{../exec/runtime.html#syntax-hostfunc}{\mathit{hostfunc}} &::=& \dots \\ \end{array}\end{split}\]

A host function is a function expressed outside WebAssembly but passed to a module as an import. The definition and behavior of host functions are outside the scope of this specification. For the purpose of this specification, it is assumed that when invoked, a host function behaves non-deterministically.

Note

Function instances are immutable, and their identity is not observable by WebAssembly code. However, the embedder might provide implicit or explicit means for distinguishing their addresses.

Table Instances

A table instance is the runtime representation of a table. It holds a vector of function elements and an optional maximum size, if one was specified in the table type at the table’s definition site.

Each function element is either empty, representing an uninitialized table entry, or a function address. Function elements can be mutated through the execution of an element segment or by external means provided by the embedder.

\[\begin{split}\begin{array}{llll} \def\mathdef1555#1{{}}\mathdef1555{(table instance)} & \href{../exec/runtime.html#syntax-tableinst}{\mathit{tableinst}} &::=& \{ \href{../exec/runtime.html#syntax-tableinst}{\mathsf{elem}}~\href{../syntax/conventions.html#syntax-vec}{\mathit{vec}}(\href{../exec/runtime.html#syntax-funcelem}{\mathit{funcelem}}), \href{../exec/runtime.html#syntax-tableinst}{\mathsf{max}}~\href{../syntax/values.html#syntax-int}{\def\mathdef1561#1{{\mathit{u}#1}}\mathdef1561{\mathit{32}}}^? \} \\ \def\mathdef1555#1{{}}\mathdef1555{(function element)} & \href{../exec/runtime.html#syntax-funcelem}{\mathit{funcelem}} &::=& \href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}}^? \\ \end{array}\end{split}\]

It is an invariant of the semantics that the length of the element vector never exceeds the maximum size, if present.

Note

Other table elements may be added in future versions of WebAssembly.

Memory Instances

A memory instance is the runtime representation of a linear memory. It holds a vector of bytes and an optional maximum size, if one was specified at the definition site of the memory.

\[\begin{split}\begin{array}{llll} \def\mathdef1555#1{{}}\mathdef1555{(memory instance)} & \href{../exec/runtime.html#syntax-meminst}{\mathit{meminst}} &::=& \{ \href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}~\href{../syntax/conventions.html#syntax-vec}{\mathit{vec}}(\href{../syntax/values.html#syntax-byte}{\mathit{byte}}), \href{../exec/runtime.html#syntax-meminst}{\mathsf{max}}~\href{../syntax/values.html#syntax-int}{\def\mathdef1561#1{{\mathit{u}#1}}\mathdef1561{\mathit{32}}}^? \} \\ \end{array}\end{split}\]

The length of the vector always is a multiple of the WebAssembly page size, which is defined to be the constant \(65536\) – abbreviated \(64\,\mathrm{Ki}\). Like in a memory type, the maximum size in a memory instance is given in units of this page size.

The bytes can be mutated through memory instructions, the execution of a data segment, or by external means provided by the embedder.

It is an invariant of the semantics that the length of the byte vector, divided by page size, never exceeds the maximum size, if present.

Global Instances

A global instance is the runtime representation of a global variable. It holds an individual value and a flag indicating whether it is mutable.

\[\begin{split}\begin{array}{llll} \def\mathdef1555#1{{}}\mathdef1555{(global instance)} & \href{../exec/runtime.html#syntax-globalinst}{\mathit{globalinst}} &::=& \{ \href{../exec/runtime.html#syntax-globalinst}{\mathsf{value}}~\href{../exec/runtime.html#syntax-val}{\mathit{val}}, \href{../exec/runtime.html#syntax-globalinst}{\mathsf{mut}}~\href{../syntax/types.html#syntax-mut}{\mathit{mut}} \} \\ \end{array}\end{split}\]

The value of mutable globals can be mutated through variable instructions or by external means provided by the embedder.

Export Instances

An export instance is the runtime representation of an export. It defines the export’s name and the associated external value.

\[\begin{split}\begin{array}{llll} \def\mathdef1555#1{{}}\mathdef1555{(export instance)} & \href{../exec/runtime.html#syntax-exportinst}{\mathit{exportinst}} &::=& \{ \href{../exec/runtime.html#syntax-exportinst}{\mathsf{name}}~\href{../syntax/values.html#syntax-name}{\mathit{name}}, \href{../exec/runtime.html#syntax-exportinst}{\mathsf{value}}~\href{../exec/runtime.html#syntax-externval}{\mathit{externval}} \} \\ \end{array}\end{split}\]

External Values

An external value is the runtime representation of an entity that can be imported or exported. It is an address denoting either a function instance, table instance, memory instance, or global instances in the shared store.

\[\begin{split}\begin{array}{llcl} \def\mathdef1555#1{{}}\mathdef1555{(external value)} & \href{../exec/runtime.html#syntax-externval}{\mathit{externval}} &::=& \href{../exec/runtime.html#syntax-externval}{\mathsf{func}}~\href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}} \\&&|& \href{../exec/runtime.html#syntax-externval}{\mathsf{table}}~\href{../exec/runtime.html#syntax-tableaddr}{\mathit{tableaddr}} \\&&|& \href{../exec/runtime.html#syntax-externval}{\mathsf{mem}}~\href{../exec/runtime.html#syntax-memaddr}{\mathit{memaddr}} \\&&|& \href{../exec/runtime.html#syntax-externval}{\mathsf{global}}~\href{../exec/runtime.html#syntax-globaladdr}{\mathit{globaladdr}} \\ \end{array}\end{split}\]

Conventions

The following auxiliary notation is defined for sequences of external values. It filters out entries of a specific kind in an order-preserving fashion:

  • \(\href{../exec/runtime.html#syntax-externval}{\mathrm{funcs}}(\href{../exec/runtime.html#syntax-externval}{\mathit{externval}}^\ast) = [\href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}} ~|~ (\href{../exec/runtime.html#syntax-externval}{\mathsf{func}}~\href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}}) \in \href{../exec/runtime.html#syntax-externval}{\mathit{externval}}^\ast]\)
  • \(\href{../exec/runtime.html#syntax-externval}{\mathrm{tables}}(\href{../exec/runtime.html#syntax-externval}{\mathit{externval}}^\ast) = [\href{../exec/runtime.html#syntax-tableaddr}{\mathit{tableaddr}} ~|~ (\href{../exec/runtime.html#syntax-externval}{\mathsf{table}}~\href{../exec/runtime.html#syntax-tableaddr}{\mathit{tableaddr}}) \in \href{../exec/runtime.html#syntax-externval}{\mathit{externval}}^\ast]\)
  • \(\href{../exec/runtime.html#syntax-externval}{\mathrm{mems}}(\href{../exec/runtime.html#syntax-externval}{\mathit{externval}}^\ast) = [\href{../exec/runtime.html#syntax-memaddr}{\mathit{memaddr}} ~|~ (\href{../exec/runtime.html#syntax-externval}{\mathsf{mem}}~\href{../exec/runtime.html#syntax-memaddr}{\mathit{memaddr}}) \in \href{../exec/runtime.html#syntax-externval}{\mathit{externval}}^\ast]\)
  • \(\href{../exec/runtime.html#syntax-externval}{\mathrm{globals}}(\href{../exec/runtime.html#syntax-externval}{\mathit{externval}}^\ast) = [\href{../exec/runtime.html#syntax-globaladdr}{\mathit{globaladdr}} ~|~ (\href{../exec/runtime.html#syntax-externval}{\mathsf{global}}~\href{../exec/runtime.html#syntax-globaladdr}{\mathit{globaladdr}}) \in \href{../exec/runtime.html#syntax-externval}{\mathit{externval}}^\ast]\)

Stack

Besides the store, most instructions interact with an implicit stack. The stack contains three kinds of entries:

These entries can occur on the stack in any order during the execution of a program. Stack entries are described by abstract syntax as follows.

Note

It is possible to model the WebAssebmly semantics using separate stacks for operands, control constructs, and calls. However, because the stacks are interdependent, additional book keeping about associated stack heights would be required. For the purpose of this specification, an interleaved representation is simpler.

Values

Values are represented by themselves.

Labels

Labels carry an argument arity \(n\) and their associated branch target, which is expressed syntactically as an instruction sequence:

\[\begin{split}\begin{array}{llll} \def\mathdef1555#1{{}}\mathdef1555{(label)} & \href{../exec/runtime.html#syntax-label}{\mathit{label}} &::=& \href{../exec/runtime.html#syntax-label}{\mathsf{label}}_n\{\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\} \\ \end{array}\end{split}\]

Intuitively, \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\) is the continuation to execute when the branch is taken, in place of the original control construct.

Note

For example, a loop label has the form

\[\href{../exec/runtime.html#syntax-label}{\mathsf{label}}_n\{\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{loop}}~[t^?]~\dots~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}\}\]

When performing a branch to this label, this executes the loop, effectively restarting it from the beginning. Conversely, a simple block label has the form

\[\href{../exec/runtime.html#syntax-label}{\mathsf{label}}_n\{\epsilon\}\]

When branching, the empty continuation ends the targeted block, such that execution can proceed with consecutive instructions.

Frames

Activation frames carry the return arity of the respective function, hold the values of its locals (including arguments) in the order corresponding to their static local indices, and a reference to the function’s own module instance:

\[\begin{split}\begin{array}{llll} \def\mathdef1555#1{{}}\mathdef1555{(activation)} & \mathit{activation} &::=& \href{../exec/runtime.html#syntax-frame}{\mathsf{frame}}_n\{\href{../exec/runtime.html#syntax-frame}{\mathit{frame}}\} \\ \def\mathdef1555#1{{}}\mathdef1555{(frame)} & \href{../exec/runtime.html#syntax-frame}{\mathit{frame}} &::=& \{ \href{../exec/runtime.html#syntax-frame}{\mathsf{locals}}~\href{../exec/runtime.html#syntax-val}{\mathit{val}}^\ast, \href{../exec/runtime.html#syntax-frame}{\mathsf{module}}~\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}} \} \\ \end{array}\end{split}\]

The values of the locals are mutated by respective variable instructions.

Conventions

  • The meta variable \(L\) ranges over labels where clear from context.
  • The meta variable \(F\) ranges over frames where clear from context.

Note

In the current version of WebAssembly, the arities of labels and frames cannot be larger than \(1\). This may be generalized in future versions.

Administrative Instructions

Note

This section is only relevant for the formal notation.

In order to express the reduction of traps, calls, and control instructions, the syntax of instructions is extended to include the following administrative instructions:

\[\begin{split}\begin{array}{llcl} \def\mathdef1555#1{{}}\mathdef1555{(administrative instruction)} & \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}} &::=& \dots \\ &&|& \href{../exec/runtime.html#syntax-trap}{\mathsf{trap}} \\ &&|& \href{../exec/runtime.html#syntax-invoke}{\mathsf{invoke}}~\href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}} \\ &&|& \href{../exec/runtime.html#syntax-label}{\mathsf{label}}_n\{\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\}~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} \\ &&|& \href{../exec/runtime.html#syntax-frame}{\mathsf{frame}}_n\{\href{../exec/runtime.html#syntax-frame}{\mathit{frame}}\}~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} \\ \end{array}\end{split}\]

The \(\href{../exec/runtime.html#syntax-trap}{\mathsf{trap}}\) instruction represents the occurrence of a trap. Traps are bubbled up through nested instruction sequences, ultimately reducing the entire program to a single \(\href{../exec/runtime.html#syntax-trap}{\mathsf{trap}}\) instruction, signalling abrupt termination.

The \(\href{../exec/runtime.html#syntax-invoke}{\mathsf{invoke}}\) instruction represents the imminent invocation of a function instance, identified by its address. It unifies the handling of different forms of calls.

The \(\href{../exec/runtime.html#syntax-label}{\mathsf{label}}\) and \(\href{../exec/runtime.html#syntax-frame}{\mathsf{frame}}\) instructions model labels and frames “on the stack”. Moreover, the administrative syntax maintains the nesting structure of the original structured control instruction or function body and their instruction sequences with an \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}\) marker. That way, the end of the inner instruction sequence is known when part of an outer sequence.

Note

For example, the reduction rule for \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{block}}\) is:

\[\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{block}}~[t^n]~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} \quad\href{../exec/conventions.html#formal-notation}{\hookrightarrow}\quad \href{../exec/runtime.html#syntax-label}{\mathsf{label}}_n\{\epsilon\}~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}\]

This replaces the block with a label instruction, which can be interpreted as “pushing” the label on the stack. When \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}\) is reached, i.e., the inner instruction sequence has been reduced to the empty sequence – or rather, a sequence of \(n\) \(\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}\) instructions representing the resulting values – then the \(\href{../exec/runtime.html#syntax-label}{\mathsf{label}}\) instruction is eliminated courtesy of its own reduction rule:

\[\href{../exec/runtime.html#syntax-label}{\mathsf{label}}_n\{\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^n\}~\href{../exec/runtime.html#syntax-val}{\mathit{val}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} \quad\href{../exec/conventions.html#formal-notation}{\hookrightarrow}\quad \href{../exec/runtime.html#syntax-val}{\mathit{val}}^n\]

This can be interpreted as removing the label from the stack and only leaving the locally accumulated operand values.

Block Contexts

In order to specify the reduction of branches, the following syntax of block contexts is defined, indexed by the count \(k\) of labels surrounding the hole:

\[\begin{split}\begin{array}{llll} \def\mathdef1555#1{{}}\mathdef1555{(block contexts)} & \href{../exec/runtime.html#syntax-ctxt-block}{B}^0 &::=& \href{../exec/runtime.html#syntax-val}{\mathit{val}}^\ast~[\_]~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast \\ \def\mathdef1555#1{{}}\mathdef1555{(block contexts)} & \href{../exec/runtime.html#syntax-ctxt-block}{B}^{k+1} &::=& \href{../exec/runtime.html#syntax-val}{\mathit{val}}^\ast~\href{../exec/runtime.html#syntax-label}{\mathsf{label}}_n\{\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\}~\href{../exec/runtime.html#syntax-ctxt-block}{B}^k~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast \\ \end{array}\end{split}\]

This definition allows to index active labels surrounding a branch or return instruction.

Note

For example, the reduction of a simple branch can be defined as follows:

\[\href{../exec/runtime.html#syntax-label}{\mathsf{label}}_0\{\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\}~\href{../exec/runtime.html#syntax-ctxt-block}{B}^l[\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br}}~l]~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} \quad\href{../exec/conventions.html#formal-notation}{\hookrightarrow}\quad \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\]

Here, the hole \([\_]\) of the context is instantiated with a branch instruction. When a branch occurs, this rule replaces the targeted label and associated instruction sequence with the label’s continuation. The selected label is identified through the label index \(l\), which corresponds to the number of surrounding \(\href{../exec/runtime.html#syntax-label}{\mathsf{label}}\) instructions that must be hopped over – which is exactly the count encoded in the index of a block context.

Module Instructions

Module instantiation is a complex operation. It is hence expressed in terms of reduction into smaller steps expressed by a sequence of administrative module instructions that are a superset of ordinary instructions and defined as follow.

\[\begin{split}\begin{array}{llcl} \def\mathdef1555#1{{}}\mathdef1555{(module instruction)} & \href{../exec/runtime.html#syntax-moduleinstr}{\mathit{moduleinstr}} &::=& \href{../exec/runtime.html#syntax-instantiate}{\mathsf{instantiate}}~\href{../syntax/modules.html#syntax-module}{\mathit{module}}~\href{../exec/runtime.html#syntax-externval}{\mathit{externval}}^\ast \\ &&|& \href{../exec/runtime.html#syntax-init-elem}{\mathsf{init\_elem}}~\href{../exec/runtime.html#syntax-tableaddr}{\mathit{tableaddr}}~\href{../syntax/values.html#syntax-int}{\def\mathdef1561#1{{\mathit{u}#1}}\mathdef1561{\mathit{32}}}~\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}~\href{../syntax/modules.html#syntax-funcidx}{\mathit{funcidx}}^\ast \\ &&|& \href{../exec/runtime.html#syntax-init-data}{\mathsf{init\_data}}~\href{../exec/runtime.html#syntax-memaddr}{\mathit{memaddr}}~\href{../syntax/values.html#syntax-int}{\def\mathdef1561#1{{\mathit{u}#1}}\mathdef1561{\mathit{32}}}~\href{../syntax/values.html#syntax-byte}{\mathit{byte}}^\ast \\ &&|& \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}} \\ \end{array}\end{split}\]

The \(\href{../exec/runtime.html#syntax-instantiate}{\mathsf{instantiate}}\) instruction expresses instantiation of a module itself, requiring a sequence of external values for the expected imports. It reduces into a sequence of initialization instructions for element and data segments, and a possible invocation of the start function that reduces further to a sequence of regular instructions.

Note

The reason for splitting instantiation into individual reduction steps is to provide a semantics that is compatible with future extensions like threads.

Unlike the administrative instructions above, module instructions embed ordinary instructions \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}\) instead of extending them. Consequently, they can only occur at the top-level.

Configurations

A configuration consists of the current store and an executing thread.

A thread can either be a regular computation over instructions, or a module instantiation, represented as a computation over module instructions. Regular computation operates relative to a current frame referring to their home module instance, whereas module computations are not local to a module, and consequently require no frame.

\[\begin{split}\begin{array}{llcl} \def\mathdef1555#1{{}}\mathdef1555{(configuration)} & \href{../exec/runtime.html#syntax-config}{\mathit{config}} &::=& \href{../exec/runtime.html#syntax-store}{\mathit{store}}; \href{../exec/runtime.html#syntax-thread}{\mathit{thread}} \\ \def\mathdef1555#1{{}}\mathdef1555{(thread)} & \href{../exec/runtime.html#syntax-thread}{\mathit{thread}} &::=& \href{../exec/runtime.html#syntax-frame}{\mathit{frame}}; \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast ~|~ \href{../exec/runtime.html#syntax-moduleinstr}{\mathit{moduleinstr}}^\ast \\ \end{array}\end{split}\]

Note

The current version of WebAssembly is single-threaded, but configurations with multiple threads may be supported in the future.

Evaluation Contexts

Finally, the following definition of evaluation context and associated structural rules enable reduction inside instruction sequences and administrative forms as well as the propagation of traps:

\[\begin{split}\begin{array}{llll} \def\mathdef1555#1{{}}\mathdef1555{(evaluation contexts)} & E &::=& [\_] ~|~ \href{../exec/runtime.html#syntax-val}{\mathit{val}}^\ast~E~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast ~|~ \href{../exec/runtime.html#syntax-label}{\mathsf{label}}_n\{\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\}~E~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} ~|~ \href{../exec/runtime.html#syntax-frame}{\mathsf{frame}}_n\{\href{../exec/runtime.html#syntax-frame}{\mathit{frame}}\}~E~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} \\ \def\mathdef1555#1{{}}\mathdef1555{(module evaluation contexts)} & M &::=& [\_]~~\href{../exec/runtime.html#syntax-moduleinstr}{\mathit{moduleinstr}}^\ast \\ \end{array}\end{split}\]
\[\begin{split}\begin{array}{lcl@{\qquad}l} S; F; E[\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast] &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S'; F'; E[{\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}'}^\ast] & (\mathrel{\mbox{if}} S; F; \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast \href{../exec/conventions.html#formal-notation}{\hookrightarrow} S'; F'; {\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}'}^\ast) \\ S; F; E[\href{../exec/runtime.html#syntax-trap}{\mathsf{trap}}] &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S; F; \href{../exec/runtime.html#syntax-trap}{\mathsf{trap}} & (\mathrel{\mbox{if}} E \neq [\_]) \\ S; M[E[\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}]] &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S'; M[E[{\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}'}^\ast]] & (\mathrel{\mbox{if}} S; F_0; \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}} \href{../exec/conventions.html#formal-notation}{\hookrightarrow} S'; F_0; {\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}'}^\ast) \\ S; M[\href{../exec/runtime.html#syntax-trap}{\mathsf{trap}}] &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S; \href{../exec/runtime.html#syntax-trap}{\mathsf{trap}} & (\mathrel{\mbox{if}} M \neq [\_]) \\ \end{array}\end{split}\]

where \(F_0 = \{\href{../exec/runtime.html#syntax-frame}{\mathsf{locals}}~\epsilon, \href{../exec/runtime.html#syntax-frame}{\mathsf{module}}~\{\}\}\) is an empty dummy frame.

Reduction terminates for a regular thread when its instruction sequence has been reduced to a sequence of values or to a \(\href{../exec/runtime.html#syntax-trap}{\mathsf{trap}}\). Reduction terminates for a module thread when its module instruction sequence has been reduced to an empty sequence or to a \(\href{../exec/runtime.html#syntax-trap}{\mathsf{trap}}\).

Note

For example, the following instruction sequence,

\[(\href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~x_1)~(\href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~x_2)~\href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{neg}}~(\href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~x_3)~\href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{add}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{mul}}\]

can be decomposed into \(E[(\href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~x_2)~\href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{neg}}]\) where

\[E = (\href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~x_1)~[\_]~(\href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~x_3)~\href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{add}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{mul}}\]

Moreover, this is the only possible choice of evaluation context where the contents of the hole matches the left-hand side of a reduction rule.