Instructions¶
Instructions are classified by stack types \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\) that describe how instructions manipulate the operand stack.
The types describe the required input stack with operand types \(t_1^\ast\) that an instruction pops off and the provided output stack with result values of types \(t_2^\ast\) that it pushes back. Stack types are akin to function types, except that they allow individual operands to be classified as \(\bot\) (bottom), indicating that the type is unconstrained. As an auxiliary notion, an operand type \(t_1\) matches another operand type \(t_2\), if \(t_1\) is either \(\bot\) or equal to \(t_2\). This is extended to stack types in a point-wise manner.
Note
For example, the instruction \(\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{add}}\) has type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}]\), consuming two \(\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}\) values and producing one.
Typing extends to instruction sequences \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\). Such a sequence has a stack type \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\) if the accumulative effect of executing the instructions is consuming values of types \(t_1^\ast\) off the operand stack and pushing new values of types \(t_2^\ast\).
For some instructions, the typing rules do not fully constrain the type, and therefore allow for multiple types. Such instructions are called polymorphic. Two degrees of polymorphism can be distinguished:
value-polymorphic: the value type \(t\) of one or several individual operands is unconstrained. That is the case for all parametric instructions like \(\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{drop}}\) and \(\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}}\).
stack-polymorphic: the entire (or most of the) stack type \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\) of the instruction is unconstrained. That is the case for all control instructions that perform an unconditional control transfer, such as \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{unreachable}}\), \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br}}\), \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br\_table}}\), and \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{return}}\).
In both cases, the unconstrained types or type sequences can be chosen arbitrarily, as long as they meet the constraints imposed for the surrounding parts of the program.
Note
For example, the \(\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}}\) instruction is valid with type \([t~t~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t]\), for any possible number type \(t\). Consequently, both instruction sequences
and
are valid, with \(t\) in the typing of \(\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}}\) being instantiated to \(\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}\) or \(\href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}\), respectively.
The \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{unreachable}}\) instruction is valid with type \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\) for any possible sequences of operand types \(t_1^\ast\) and \(t_2^\ast\). Consequently,
is valid by assuming type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}]\) for the \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{unreachable}}\) instruction. In contrast,
is invalid, because there is no possible type to pick for the \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{unreachable}}\) instruction that would make the sequence well-typed.
The Appendix describes a type checking algorithm that efficiently implements validation of instruction sequences as prescribed by the rules given here.
Numeric Instructions¶
\(t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c\)¶
The instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t]\).
\(t\mathsf{.}\href{../syntax/instructions.html#syntax-unop}{\mathit{unop}}\)¶
The instruction is valid with type \([t] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t]\).
\(t\mathsf{.}\href{../syntax/instructions.html#syntax-binop}{\mathit{binop}}\)¶
The instruction is valid with type \([t~t] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t]\).
\(t\mathsf{.}\href{../syntax/instructions.html#syntax-testop}{\mathit{testop}}\)¶
The instruction is valid with type \([t] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}]\).
\(t\mathsf{.}\href{../syntax/instructions.html#syntax-relop}{\mathit{relop}}\)¶
The instruction is valid with type \([t~t] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}]\).
\(t_2\mathsf{.}\href{../syntax/instructions.html#syntax-cvtop}{\mathit{cvtop}}\mathsf{\_}t_1\mathsf{\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}^?\)¶
The instruction is valid with type \([t_1] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2]\).
Reference Instructions¶
\(\href{../syntax/instructions.html#syntax-instr-ref}{\mathsf{ref{.}null}}~t\)¶
The instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t]\).
Note
In future versions of WebAssembly, there may be reference types for which no null reference is allowed.
\(\href{../syntax/instructions.html#syntax-instr-ref}{\mathsf{ref{.}is\_null}}\)¶
The instruction is valid with type \([t] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}]\), for any reference type \(t\).
\(\href{../syntax/instructions.html#syntax-instr-ref}{\mathsf{ref{.}func}}~x\)¶
The function \(C.\href{../valid/conventions.html#context}{\mathsf{funcs}}[x]\) must be defined in the context.
The function index \(x\) must be contained in \(C.\href{../valid/conventions.html#context}{\mathsf{refs}}\).
The instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-reftype}{\mathsf{funcref}}]\).
Vector Instructions¶
Vector instructions can have a prefix to describe the shape of the operand. Packed numeric types, \(\href{../syntax/values.html#syntax-int}{\mathit{i8}}\) and \(\href{../syntax/values.html#syntax-int}{\mathit{i16}}\), are not value types. An auxiliary function maps such packed type shapes to value types:
The following auxiliary function denotes the number of lanes in a vector shape, i.e., its dimension:
\(\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-vec}{\mathsf{const}}~c\)¶
The instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}\mathsf{.}\href{../syntax/instructions.html#syntax-vvunop}{\mathit{vvunop}}\)¶
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}\mathsf{.}\href{../syntax/instructions.html#syntax-vvbinop}{\mathit{vvbinop}}\)¶
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}\mathsf{.}\href{../syntax/instructions.html#syntax-vvternop}{\mathit{vvternop}}\)¶
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}\mathsf{.}\href{../syntax/instructions.html#syntax-vvtestop}{\mathit{vvtestop}}\)¶
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}]\).
\(\mathsf{i8x16.}\href{../syntax/instructions.html#syntax-instr-vec}{\mathsf{swizzle}}\)¶
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\mathsf{i8x16.}\href{../syntax/instructions.html#syntax-instr-vec}{\mathsf{shuffle}}~\href{../syntax/instructions.html#syntax-laneidx}{\mathit{laneidx}}^{16}\)¶
For all \(\href{../syntax/instructions.html#syntax-laneidx}{\mathit{laneidx}}_i\), in \(\href{../syntax/instructions.html#syntax-laneidx}{\mathit{laneidx}}^{16}\), \(\href{../syntax/instructions.html#syntax-laneidx}{\mathit{laneidx}}_i\) must be smaller than \(32\).
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\href{../syntax/instructions.html#syntax-shape}{\mathit{shape}}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-vec}{\mathsf{splat}}\)¶
Let \(t\) be \(\href{../valid/instructions.html#aux-unpacked}{\mathrm{unpacked}}(\href{../syntax/instructions.html#syntax-shape}{\mathit{shape}})\).
The instruction is valid with type \([t] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\href{../syntax/instructions.html#syntax-shape}{\mathit{shape}}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-vec}{\mathsf{extract\_lane}}\mathsf{\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}^?~\href{../syntax/instructions.html#syntax-laneidx}{\mathit{laneidx}}\)¶
The lane index \(\href{../syntax/instructions.html#syntax-laneidx}{\mathit{laneidx}}\) must be smaller than \(\href{../valid/instructions.html#aux-dim}{\mathrm{dim}}(\href{../syntax/instructions.html#syntax-shape}{\mathit{shape}})\).
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../valid/instructions.html#aux-unpacked}{\mathrm{unpacked}}(\href{../syntax/instructions.html#syntax-shape}{\mathit{shape}})]\).
\(\href{../syntax/instructions.html#syntax-shape}{\mathit{shape}}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-vec}{\mathsf{replace\_lane}}~\href{../syntax/instructions.html#syntax-laneidx}{\mathit{laneidx}}\)¶
The lane index \(\href{../syntax/instructions.html#syntax-laneidx}{\mathit{laneidx}}\) must be smaller than \(\href{../valid/instructions.html#aux-dim}{\mathrm{dim}}(\href{../syntax/instructions.html#syntax-shape}{\mathit{shape}})\).
Let \(t\) be \(\href{../valid/instructions.html#aux-unpacked}{\mathrm{unpacked}}(\href{../syntax/instructions.html#syntax-shape}{\mathit{shape}})\).
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}~t] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\href{../syntax/instructions.html#syntax-shape}{\mathit{shape}}\mathsf{.}\href{../syntax/instructions.html#syntax-vunop}{\mathit{vunop}}\)¶
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\href{../syntax/instructions.html#syntax-shape}{\mathit{shape}}\mathsf{.}\href{../syntax/instructions.html#syntax-vbinop}{\mathit{vbinop}}\)¶
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\href{../syntax/instructions.html#syntax-shape}{\mathit{shape}}\mathsf{.}\href{../syntax/instructions.html#syntax-vrelop}{\mathit{vrelop}}\)¶
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\href{../syntax/instructions.html#syntax-shape}{\mathit{ishape}}\mathsf{.}\href{../syntax/instructions.html#syntax-vishiftop}{\mathit{vishiftop}}\)¶
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\href{../syntax/instructions.html#syntax-shape}{\mathit{shape}}\mathsf{.}\href{../syntax/instructions.html#syntax-vtestop}{\mathit{vtestop}}\)¶
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}]\).
\(\href{../syntax/instructions.html#syntax-shape}{\mathit{shape}}\mathsf{.}\href{../syntax/instructions.html#syntax-vcvtop}{\mathit{vcvtop}}\mathsf{\_}\href{../syntax/instructions.html#syntax-half}{\mathit{half}}^?\mathsf{\_}\href{../syntax/instructions.html#syntax-shape}{\mathit{shape}}\mathsf{\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}^?\mathsf{\_zero}^?\)¶
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\href{../syntax/instructions.html#syntax-shape}{\mathit{ishape}}_1\mathsf{.}\href{../syntax/instructions.html#syntax-instr-vec}{\mathsf{narrow}}\mathsf{\_}\href{../syntax/instructions.html#syntax-shape}{\mathit{ishape}}_2\mathsf{\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}\)¶
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\href{../syntax/instructions.html#syntax-shape}{\mathit{ishape}}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-vec}{\mathsf{bitmask}}\)¶
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}]\).
\(\href{../syntax/instructions.html#syntax-shape}{\mathit{ishape}}_1\mathsf{.}\href{../syntax/instructions.html#syntax-instr-vec}{\mathsf{dot}}\mathsf{\_}\href{../syntax/instructions.html#syntax-shape}{\mathit{ishape}}_2\mathsf{\_s}\)¶
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\href{../syntax/instructions.html#syntax-shape}{\mathit{ishape}}_1\mathsf{.}\href{../syntax/instructions.html#syntax-instr-vec}{\mathsf{extmul}}\mathsf{\_}\href{../syntax/instructions.html#syntax-half}{\mathit{half}}\mathsf{\_}\href{../syntax/instructions.html#syntax-shape}{\mathit{ishape}}_2\mathsf{\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}\)¶
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\href{../syntax/instructions.html#syntax-shape}{\mathit{ishape}}_1\mathsf{.}\href{../syntax/instructions.html#syntax-instr-vec}{\mathsf{extadd\_pairwise}}\mathsf{\_}\href{../syntax/instructions.html#syntax-shape}{\mathit{ishape}}_2\mathsf{\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}\)¶
The instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
Parametric Instructions¶
\(\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{drop}}\)¶
The instruction is valid with type \([t] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\), for any operand type \(t\).
Note
Both \(\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{drop}}\) and \(\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}}\) without annotation are value-polymorphic instructions.
\(\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}}~(t^\ast)^?\)¶
If \(t^\ast\) is present, then:
The length of \(t^\ast\) must be \(1\).
Then the instruction is valid with type \([t^\ast~t^\ast~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^\ast]\).
Else:
The instruction is valid with type \([t~t~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t]\), for any operand type \(t\) that matches some number type or vector type.
Note
In future versions of WebAssembly, \(\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}}\) may allow more than one value per choice.
Variable Instructions¶
\(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{local.get}}~x\)¶
The local \(C.\href{../valid/conventions.html#context}{\mathsf{locals}}[x]\) must be defined in the context.
Let \(t\) be the value type \(C.\href{../valid/conventions.html#context}{\mathsf{locals}}[x]\).
Then the instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t]\).
\(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{local.set}}~x\)¶
The local \(C.\href{../valid/conventions.html#context}{\mathsf{locals}}[x]\) must be defined in the context.
Let \(t\) be the value type \(C.\href{../valid/conventions.html#context}{\mathsf{locals}}[x]\).
Then the instruction is valid with type \([t] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\).
\(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{local.tee}}~x\)¶
The local \(C.\href{../valid/conventions.html#context}{\mathsf{locals}}[x]\) must be defined in the context.
Let \(t\) be the value type \(C.\href{../valid/conventions.html#context}{\mathsf{locals}}[x]\).
Then the instruction is valid with type \([t] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t]\).
\(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{global.get}}~x\)¶
The global \(C.\href{../valid/conventions.html#context}{\mathsf{globals}}[x]\) must be defined in the context.
Let \(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}~t\) be the global type \(C.\href{../valid/conventions.html#context}{\mathsf{globals}}[x]\).
Then the instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t]\).
\(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{global.set}}~x\)¶
The global \(C.\href{../valid/conventions.html#context}{\mathsf{globals}}[x]\) must be defined in the context.
Let \(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}~t\) be the global type \(C.\href{../valid/conventions.html#context}{\mathsf{globals}}[x]\).
The mutability \(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}\) must be \(\href{../syntax/types.html#syntax-mut}{\mathsf{var}}\).
Then the instruction is valid with type \([t] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\).
Table Instructions¶
\(\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.get}}~x\)¶
The table \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[x]\) must be defined in the context.
Let \(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}~t\) be the table type \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[x]\).
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t]\).
\(\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.set}}~x\)¶
The table \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[x]\) must be defined in the context.
Let \(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}~t\) be the table type \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[x]\).
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~t] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\).
\(\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.size}}~x\)¶
The table \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[x]\) must be defined in the context.
Then the instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}]\).
\(\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.grow}}~x\)¶
The table \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[x]\) must be defined in the context.
Let \(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}~t\) be the table type \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[x]\).
Then the instruction is valid with type \([t~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}]\).
\(\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.fill}}~x\)¶
The table \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[x]\) must be defined in the context.
Let \(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}~t\) be the table type \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[x]\).
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~t~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\).
\(\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.copy}}~x~y\)¶
The table \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[x]\) must be defined in the context.
Let \(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}_1~t_1\) be the table type \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[x]\).
The table \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[y]\) must be defined in the context.
Let \(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}_2~t_2\) be the table type \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[y]\).
The reference type \(t_1\) must be the same as \(t_2\).
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\).
\(\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.init}}~x~y\)¶
The table \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[x]\) must be defined in the context.
Let \(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}~t_1\) be the table type \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[x]\).
The element segment \(C.\href{../valid/conventions.html#context}{\mathsf{elems}}[y]\) must be defined in the context.
Let \(t_2\) be the reference type \(C.\href{../valid/conventions.html#context}{\mathsf{elems}}[y]\).
The reference type \(t_1\) must be the same as \(t_2\).
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\).
\(\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{elem.drop}}~x\)¶
The element segment \(C.\href{../valid/conventions.html#context}{\mathsf{elems}}[x]\) must be defined in the context.
Then the instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\).
Memory Instructions¶
\(t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{load}}~x~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}\)¶
The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[x]\) must be defined in the context.
The alignment \(2^{\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{align}}}\) must not be larger than the bit width of \(t\) divided by \(8\).
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t]\).
\(t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{load}}{N}\mathsf{\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}~x~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}\)¶
The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[x]\) must be defined in the context.
The alignment \(2^{\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{align}}}\) must not be larger than \(N/8\).
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t]\).
\(t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{store}}~x~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}\)¶
The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[x]\) must be defined in the context.
The alignment \(2^{\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{align}}}\) must not be larger than the bit width of \(t\) divided by \(8\).
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~t] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\).
\(t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{store}}{N}~x~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}\)¶
The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[x]\) must be defined in the context.
The alignment \(2^{\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{align}}}\) must not be larger than \(N/8\).
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~t] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\).
\(\mathsf{v128.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{load}}{N}\mathsf{x}M\_\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}~x~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}\)¶
The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[x]\) must be defined in the context.
The alignment \(2^{\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{align}}}\) must not be larger than \(N/8 \cdot M\).
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\mathsf{v128.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{load}}{N}\mathsf{\_splat}~x~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}\)¶
The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[x]\) must be defined in the context.
The alignment \(2^{\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{align}}}\) must not be larger than \(N/8\).
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\mathsf{v128.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{load}}{N}\mathsf{\_zero}~x~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}\)¶
The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[x]\) must be defined in the context.
The alignment \(2^{\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{align}}}\) must not be larger than \(N/8\).
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\mathsf{v128.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{load}}{N}\mathsf{\_lane}~x~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}~\href{../syntax/instructions.html#syntax-laneidx}{\mathit{laneidx}}\)¶
The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[x]\) must be defined in the context.
The alignment \(2^{\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{align}}}\) must not be larger than \(N/8\).
The lane index \(\href{../syntax/instructions.html#syntax-laneidx}{\mathit{laneidx}}\) must be smaller than \(128/N\).
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\mathsf{v128.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{store}}{N}\mathsf{\_lane}~x~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}~\href{../syntax/instructions.html#syntax-laneidx}{\mathit{laneidx}}\)¶
The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[x]\) must be defined in the context.
The alignment \(2^{\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{align}}}\) must not be larger than \(N/8\).
The lane index \(\href{../syntax/instructions.html#syntax-laneidx}{\mathit{laneidx}}\) must be smaller than \(128/N\).
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}]\).
\(\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.size}}~x\)¶
The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[x]\) must be defined in the context.
Then the instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}]\).
\(\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.grow}}~x\)¶
The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[x]\) must be defined in the context.
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}]\).
\(\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.fill}}~x\)¶
The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[x]\) must be defined in the context.
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\).
\(\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.copy}}~x~y\)¶
The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[x]\) must be defined in the context.
The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[y]\) must be defined in the context.
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\).
\(\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.init}}~x~y\)¶
The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[x]\) must be defined in the context.
The data segment \(C.\href{../valid/conventions.html#context}{\mathsf{datas}}[y]\) must be defined in the context.
Then the instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\).
\(\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{data.drop}}~x\)¶
The data segment \(C.\href{../valid/conventions.html#context}{\mathsf{datas}}[x]\) must be defined in the context.
Then the instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\).
Control Instructions¶
\(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{nop}}\)¶
The instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\).
\(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{unreachable}}\)¶
The instruction is valid with type \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\), for any sequences of operand types \(t_1^\ast\) and \(t_2^\ast\).
Note
The \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{unreachable}}\) instruction is stack-polymorphic.
\(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{block}}~\href{../syntax/instructions.html#syntax-blocktype}{\mathit{blocktype}}~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}\)¶
The block type must be valid as some function type \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\).
Let \(C'\) be the same context as \(C\), but with the result type \([t_2^\ast]\) prepended to the \(\href{../valid/conventions.html#context}{\mathsf{labels}}\) vector.
Under context \(C'\), the instruction sequence \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\) must be valid with type \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\).
Then the compound instruction is valid with type \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\).
Note
The notation \(C,\href{../valid/conventions.html#context}{\mathsf{labels}}\,[t^\ast]\) inserts the new label type at index \(0\), shifting all others.
\(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{loop}}~\href{../syntax/instructions.html#syntax-blocktype}{\mathit{blocktype}}~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}\)¶
The block type must be valid as some function type \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\).
Let \(C'\) be the same context as \(C\), but with the result type \([t_1^\ast]\) prepended to the \(\href{../valid/conventions.html#context}{\mathsf{labels}}\) vector.
Under context \(C'\), the instruction sequence \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\) must be valid with type \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\).
Then the compound instruction is valid with type \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\).
Note
The notation \(C,\href{../valid/conventions.html#context}{\mathsf{labels}}\,[t^\ast]\) inserts the new label type at index \(0\), shifting all others.
\(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{if}}~\href{../syntax/instructions.html#syntax-blocktype}{\mathit{blocktype}}~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_1^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{else}}~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_2^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}\)¶
The block type must be valid as some function type \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\).
Let \(C'\) be the same context as \(C\), but with the result type \([t_2^\ast]\) prepended to the \(\href{../valid/conventions.html#context}{\mathsf{labels}}\) vector.
Under context \(C'\), the instruction sequence \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_1^\ast\) must be valid with type \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\).
Under context \(C'\), the instruction sequence \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_2^\ast\) must be valid with type \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\).
Then the compound instruction is valid with type \([t_1^\ast~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\).
Note
The notation \(C,\href{../valid/conventions.html#context}{\mathsf{labels}}\,[t^\ast]\) inserts the new label type at index \(0\), shifting all others.
\(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br}}~l\)¶
The label \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l]\) must be defined in the context.
Let \([t^\ast]\) be the result type \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l]\).
Then the instruction is valid with type \([t_1^\ast~t^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\), for any sequences of operand types \(t_1^\ast\) and \(t_2^\ast\).
Note
The label index space in the context \(C\) contains the most recent label first, so that \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l]\) performs a relative lookup as expected.
The \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br}}\) instruction is stack-polymorphic.
\(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br\_if}}~l\)¶
The label \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l]\) must be defined in the context.
Let \([t^\ast]\) be the result type \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l]\).
Then the instruction is valid with type \([t^\ast~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^\ast]\).
Note
The label index space in the context \(C\) contains the most recent label first, so that \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l]\) performs a relative lookup as expected.
\(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br\_table}}~l^\ast~l_N\)¶
The label \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l_N]\) must be defined in the context.
For each label \(l_i\) in \(l^\ast\), the label \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l_i]\) must be defined in the context.
There must be a sequence \(t^\ast\) of operand types, such that:
The length of the sequence \(t^\ast\) is the same as the length of the sequence \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l_N]\).
For each operand type \(t_j\) in \(t^\ast\) and corresponding type \(t'_{Nj}\) in \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l_N]\), \(t_j\) matches \(t'_{Nj}\).
For each label \(l_i\) in \(l^\ast\):
The length of the sequence \(t^\ast\) is the same as the length of the sequence \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l_i]\).
For each operand type \(t_j\) in \(t^\ast\) and corresponding type \(t'_{ij}\) in \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l_i]\), \(t_j\) matches \(t'_{ij}\).
Then the instruction is valid with type \([t_1^\ast~t^\ast~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\), for any sequences of operand types \(t_1^\ast\) and \(t_2^\ast\).
Note
The label index space in the context \(C\) contains the most recent label first, so that \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l_i]\) performs a relative lookup as expected.
The \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br\_table}}\) instruction is stack-polymorphic.
\(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{return}}\)¶
The return type \(C.\href{../valid/conventions.html#context}{\mathsf{return}}\) must not be absent in the context.
Let \([t^\ast]\) be the result type of \(C.\href{../valid/conventions.html#context}{\mathsf{return}}\).
Then the instruction is valid with type \([t_1^\ast~t^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\), for any sequences of operand types \(t_1^\ast\) and \(t_2^\ast\).
Note
The \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{return}}\) instruction is stack-polymorphic.
\(C.\href{../valid/conventions.html#context}{\mathsf{return}}\) is absent (set to \(\epsilon\)) when validating an expression that is not a function body. This differs from it being set to the empty result type (\([\epsilon]\)), which is the case for functions not returning anything.
\(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{call}}~x\)¶
The function \(C.\href{../valid/conventions.html#context}{\mathsf{funcs}}[x]\) must be defined in the context.
Then the instruction is valid with type \(C.\href{../valid/conventions.html#context}{\mathsf{funcs}}[x]\).
\(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{call\_indirect}}~x~y\)¶
The table \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[x]\) must be defined in the context.
Let \(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}~t\) be the table type \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[x]\).
The reference type \(t\) must be \(\href{../syntax/types.html#syntax-reftype}{\mathsf{funcref}}\).
The type \(C.\href{../valid/conventions.html#context}{\mathsf{types}}[y]\) must be defined in the context.
Let \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\) be the function type \(C.\href{../valid/conventions.html#context}{\mathsf{types}}[y]\).
Then the instruction is valid with type \([t_1^\ast~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\).
Instruction Sequences¶
Typing of instruction sequences is defined recursively.
Empty Instruction Sequence: \(\epsilon\)¶
The empty instruction sequence is valid with type \([t^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^\ast]\), for any sequence of operand types \(t^\ast\).
Non-empty Instruction Sequence: \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_N\)¶
The instruction sequence \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\) must be valid with type \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\), for some sequences of operand types \(t_1^\ast\) and \(t_2^\ast\).
The instruction \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_N\) must be valid with type \([t^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_3^\ast]\), for some sequences of operand types \(t^\ast\) and \(t_3^\ast\).
There must be a sequence of operand types \(t_0^\ast\), such that \(t_2^\ast = t_0^\ast~{t'}^\ast\) where the type sequence \({t'}^\ast\) is as long as \(t^\ast\).
For each operand type \(t'_i\) in \({t'}^\ast\) and corresponding type \(t_i\) in \(t^\ast\), \(t'_i\) matches \(t_i\).
Then the combined instruction sequence is valid with type \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_0^\ast~t_3^\ast]\).
Expressions¶
Expressions \(\href{../syntax/instructions.html#syntax-expr}{\mathit{expr}}\) are classified by result types of the form \([t^\ast]\).
\(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}\)¶
The instruction sequence \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\) must be valid with some stack type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [{t'}^\ast]\).
For each operand type \(t'_i\) in \({t'}^\ast\) and corresponding value type \(t_i\) in \(t^\ast\), \(t'_i\) matches \(t_i\).
Then the expression is valid with result type \([t^\ast]\).
Constant Expressions¶
In a constant expression \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}\) all instructions in \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\) must be constant.
A constant instruction \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}\) must be:
either of the form \(t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c\),
or of the form \(\href{../syntax/instructions.html#syntax-instr-ref}{\mathsf{ref{.}null}}\),
or of the form \(\href{../syntax/instructions.html#syntax-instr-ref}{\mathsf{ref{.}func}}~x\),
or of the form \(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{global.get}}~x\), in which case \(C.\href{../valid/conventions.html#context}{\mathsf{globals}}[x]\) must be a global type of the form \(\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~t\).
Note
Currently, constant expressions occurring in globals, element, or data segments are further constrained in that contained \(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{global.get}}\) instructions are only allowed to refer to imported globals. This is enforced in the validation rule for modules by constraining the context \(C\) accordingly.
The definition of constant expression may be extended in future versions of WebAssembly.