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 either the four basic number types, i.e., integers and floating-point data of 32 or 64 bit width each, or vectors of 128 bit width, or of reference type.
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
References other than null are represented with additional administrative instructions. They either are scalar references, containing a 31-bit integer, structure references, pointing to a specific structure address, array references, pointing to a specific array address, function references, pointing to a specific function address, or host references pointing to an uninterpreted form of host address defined by the embedder. Any of the aformentioned references can furthermore be wrapped up as an external reference.
Note
Future versions of WebAssembly may add additional forms of reference.
Value types can have an associated default value;
it is the respective value
Convention¶
The meta variable
ranges over reference values where clear from context.
Results¶
A result is the outcome of a computation. It is either a sequence of values or a trap.
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, element segments, data segments, and structures or arrays that have been allocated during the life time of the abstract machine. [1]
It is an invariant of the semantics that no element or data instance is addressed from anywhere else but the owning module instances.
Syntactically, the store is defined as a record listing the existing instances of each category:
Convention¶
The meta variable
ranges over stores where clear from context.
Addresses¶
Function instances, table instances, memory instances, and global instances, element instances, data instances and structure or array instances in the store are referenced with abstract addresses. These are simply indices into the respective store component. In addition, an embedder may supply an uninterpreted set of host addresses.
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
There is no specific limit on the number of allocations of store objects, hence logical addresses can be arbitrarily large natural numbers.
Conventions¶
The notation
denotes the set of addresses from address space occurring free in . We sometimes reinterpret this set as the vector of its elements.
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.
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.
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, but within certain constraints that ensure the integrity of the runtime.
Table Instances¶
A table instance is the runtime representation of a table. It records its type and holds a vector of reference values.
Table elements can be mutated through table instructions, the execution of an active element segment, or by external means provided by the embedder.
It is an invariant of the semantics that all table elements have a type matching the element type of
Memory Instances¶
A memory instance is the runtime representation of a linear memory. It records its type and holds a vector of bytes.
The length of the vector always is a multiple of the WebAssembly page size, which is defined to be the constant
The bytes can be mutated through memory instructions, the execution of an active 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 of
Global Instances¶
A global instance is the runtime representation of a global variable. It records its type and holds an individual value.
The value of mutable globals can be mutated through variable instructions or by external means provided by the embedder.
It is an invariant of the semantics that the value has a type matching the value type of
Element Instances¶
An element instance is the runtime representation of an element segment. It holds a vector of references and their common type.
Data Instances¶
An data instance is the runtime representation of a data segment. It holds a vector of bytes.
Export Instances¶
An export instance is the runtime representation of an export. It defines the export’s name and the associated external value.
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.
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:
Aggregate Instances¶
A structure instance is the runtime representation of a heap object allocated from a structure type. Likewise, an array instance is the runtime representation of a heap object allocated from an array type. Both record their respective defined type and hold a vector of the values of their fields.
Conventions¶
Conversion of a regular value to a field value is defined as follows:
The inverse conversion of a field value to a regular value is defined as follows:
Stack¶
Besides the store, most instructions interact with an implicit stack. The stack contains three kinds of entries:
Values: the operands of instructions.
Labels: active structured control instructions that can be targeted by branches.
Activations: the call frames of active function calls.
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 WebAssembly 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
Intuitively,
Note
For example, a loop label has the form
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
When branching, the empty continuation ends the targeted block, such that execution can proceed with consecutive instructions.
Activation Frames¶
Activation frames carry the return arity
Locals may be uninitialized, in which case they are empty. Locals are mutated by respective variable instructions.
Conventions¶
The meta variable
ranges over labels where clear from context.The meta variable
ranges over frame states where clear from context.The following auxiliary definition takes a block type and looks up the instruction type that it denotes in the current frame:
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:
The
The
The
The
Note
For example, the reduction rule for
This replaces the block with a label instruction,
which can be interpreted as “pushing” the label on the stack.
When
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
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:
Here, the hole
Configurations¶
A configuration consists of the current store and an executing thread.
A thread is a computation over instructions that operates relative to the state of a current frame referring to the module instance in which the computation runs, i.e., where the current function originates from.
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:
Reduction terminates when a thread’s instruction sequence has been reduced to a result,
that is, either a sequence of values or to a
Note
The restriction on evaluation contexts rules out contexts like
For an example of reduction under evaluation contexts, consider the following instruction sequence.
This can be decomposed into
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.