.. index:: instruction, function type, context, value, operand stack, ! polymorphism
:ref:`Instructions <syntax-instr>` are classified by :ref:`function types <syntax-functype>` [t_1^\ast] \to [t_2^\ast] that describe how they manipulate the :ref:`operand stack <stack>`. The types describe the required input stack with argument values of 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.
Note
For example, the instruction \I32.\ADD has type [\I32~\I32] \to [\I32], consuming two |I32| values and producing one.
Typing extends to :ref:`instruction sequences <valid-instr-seq>` \instr^\ast. Such a sequence has a :ref:`function type <syntax-functype>` [t_1^\ast] \to [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 :ref:`value type <syntax-valtype>` t of one or several individual operands is unconstrained. That is the case for all :ref:`parametric instructions <valid-instr-parametric>` like |DROP| and |SELECT|.
- stack-polymorphic: the entire (or most of the) :ref:`function type <syntax-functype>` [t_1^\ast] \to [t_2^\ast] of the instruction is unconstrained. That is the case for all :ref:`control instructions <valid-instr-control>` that perform an unconditional control transfer, such as |UNREACHABLE|, |BR|, |BRTABLE|, and |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 |SELECT| instruction is valid with type [t~t~\I32] \to [t], for any possible :ref:`value type <syntax-valtype>` t. Consequently, both instruction sequences
(\I32.\CONST~1)~~(\I32.\CONST~2)~~(\I32.\CONST~3)~~\SELECT{}
and
(\F64.\CONST~1.0)~~(\F64.\CONST~2.0)~~(\I32.\CONST~3)~~\SELECT{}
are valid, with t in the typing of |SELECT| being instantiated to |I32| or |F64|, respectively.
The |UNREACHABLE| instruction is valid with type [t_1^\ast] \to [t_2^\ast] for any possible sequences of value types t_1^\ast and t_2^\ast. Consequently,
\UNREACHABLE~~\I32.\ADD
is valid by assuming type [] \to [\I32~\I32] for the |UNREACHABLE| instruction. In contrast,
\UNREACHABLE~~(\I64.\CONST~0)~~\I32.\ADD
is invalid, because there is no possible type to pick for the |UNREACHABLE| instruction that would make the sequence well-typed.
.. index:: numeric instruction pair: validation; instruction single: abstract syntax; instruction
- The instruction is valid with type [] \to [t].
\frac{ }{ C \vdashinstr t\K{.}\CONST~c : [] \to [t] }
- The instruction is valid with type [t] \to [t].
\frac{ }{ C \vdashinstr t\K{.}\unop : [t] \to [t] }
- The instruction is valid with type [t~t] \to [t].
\frac{ }{ C \vdashinstr t\K{.}\binop : [t~t] \to [t] }
- The instruction is valid with type [t] \to [\I32].
\frac{ }{ C \vdashinstr t\K{.}\testop : [t] \to [\I32] }
- The instruction is valid with type [t~t] \to [\I32].
\frac{ }{ C \vdashinstr t\K{.}\relop : [t~t] \to [\I32] }
- The instruction is valid with type [t_1] \to [t_2].
\frac{ }{ C \vdashinstr t_2\K{.}\cvtop\K{\_}t_1\K{\_}\sx^? : [t_1] \to [t_2] }
.. index:: parametric instructions, value type, polymorphism pair: validation; instruction single: abstract syntax; instruction
- The instruction is valid with type [t] \to [], for any :ref:`value type <syntax-valtype>` t.
\frac{ }{ C \vdashinstr \DROP : [t] \to [] }
- The instruction is valid with type [t~t~\I32] \to [t], for any :ref:`value type <syntax-valtype>` t.
\frac{ }{ C \vdashinstr \SELECT : [t~t~\I32] \to [t] }
Note
Both |DROP| and |SELECT| are :ref:`value-polymorphic <polymorphism>` instructions.
.. index:: variable instructions, local index, global index, context pair: validation; instruction single: abstract syntax; instruction
- The local C.\CLOCALS[x] must be defined in the context.
- Let t be the :ref:`value type <syntax-valtype>` C.\CLOCALS[x].
- Then the instruction is valid with type [] \to [t].
\frac{ C.\CLOCALS[x] = t }{ C \vdashinstr \LOCALGET~x : [] \to [t] }
- The local C.\CLOCALS[x] must be defined in the context.
- Let t be the :ref:`value type <syntax-valtype>` C.\CLOCALS[x].
- Then the instruction is valid with type [t] \to [].
\frac{ C.\CLOCALS[x] = t }{ C \vdashinstr \LOCALSET~x : [t] \to [] }
- The local C.\CLOCALS[x] must be defined in the context.
- Let t be the :ref:`value type <syntax-valtype>` C.\CLOCALS[x].
- Then the instruction is valid with type [t] \to [t].
\frac{ C.\CLOCALS[x] = t }{ C \vdashinstr \LOCALTEE~x : [t] \to [t] }
- The global C.\CGLOBALS[x] must be defined in the context.
- Let \mut~t be the :ref:`global type <syntax-globaltype>` C.\CGLOBALS[x].
- Then the instruction is valid with type [] \to [t].
\frac{ C.\CGLOBALS[x] = \mut~t }{ C \vdashinstr \GLOBALGET~x : [] \to [t] }
- The global C.\CGLOBALS[x] must be defined in the context.
- Let \mut~t be the :ref:`global type <syntax-globaltype>` C.\CGLOBALS[x].
- The mutability \mut must be |MVAR|.
- Then the instruction is valid with type [t] \to [].
\frac{ C.\CGLOBALS[x] = \MVAR~t }{ C \vdashinstr \GLOBALSET~x : [t] \to [] }
.. index:: table instruction, table index, context pair: validation; instruction single: abstract syntax; instruction
- The table C.\CTABLES[0] must be defined in the context.
- Then the instruction is valid with type [\I32~\I32~\I32] \to [].
\frac{ C.\CTABLES[0] = \tabletype }{ C \vdashinstr \TABLECOPY : [\I32~\I32~\I32] \to [] }
- The table C.\CTABLES[0] must be defined in the context.
- The element segment C.\CELEM[x] must be defined in the context.
- Then the instruction is valid with type [\I32~\I32~\I32] \to [].
\frac{ C.\CTABLES[0] = \tabletype \qquad C.\CELEM[x] = \segtype }{ C \vdashinstr \TABLEINIT~x : [\I32~\I32~\I32] \to [] }
- The element segment C.\CELEM[x] must be defined in the context.
- Then the instruction is valid with type [] \to [].
\frac{ C.\CELEM[x] = \segtype }{ C \vdashinstr \ELEMDROP~x : [] \to [] }
.. index:: memory instruction, memory index, context pair: validation; instruction single: abstract syntax; instruction
- The memory C.\CMEMS[0] must be defined in the context.
- The alignment 2^{\memarg.\ALIGN} must not be larger than the :ref:`bit width <syntax-valtype>` of t divided by 8.
- Then the instruction is valid with type [\I32] \to [t].
\frac{ C.\CMEMS[0] = \memtype \qquad 2^{\memarg.\ALIGN} \leq |t|/8 }{ C \vdashinstr t\K{.load}~\memarg : [\I32] \to [t] }
- The memory C.\CMEMS[0] must be defined in the context.
- The alignment 2^{\memarg.\ALIGN} must not be larger than N/8.
- Then the instruction is valid with type [\I32] \to [t].
\frac{ C.\CMEMS[0] = \memtype \qquad 2^{\memarg.\ALIGN} \leq N/8 }{ C \vdashinstr t\K{.load}N\K{\_}\sx~\memarg : [\I32] \to [t] }
- The memory C.\CMEMS[0] must be defined in the context.
- The alignment 2^{\memarg.\ALIGN} must not be larger than the :ref:`bit width <syntax-valtype>` of t divided by 8.
- Then the instruction is valid with type [\I32~t] \to [].
\frac{ C.\CMEMS[0] = \memtype \qquad 2^{\memarg.\ALIGN} \leq |t|/8 }{ C \vdashinstr t\K{.store}~\memarg : [\I32~t] \to [] }
- The memory C.\CMEMS[0] must be defined in the context.
- The alignment 2^{\memarg.\ALIGN} must not be larger than N/8.
- Then the instruction is valid with type [\I32~t] \to [].
\frac{ C.\CMEMS[0] = \memtype \qquad 2^{\memarg.\ALIGN} \leq N/8 }{ C \vdashinstr t\K{.store}N~\memarg : [\I32~t] \to [] }
- The memory C.\CMEMS[0] must be defined in the context.
- Then the instruction is valid with type [] \to [\I32].
\frac{ C.\CMEMS[0] = \memtype }{ C \vdashinstr \MEMORYSIZE : [] \to [\I32] }
- The memory C.\CMEMS[0] must be defined in the context.
- Then the instruction is valid with type [\I32] \to [\I32].
\frac{ C.\CMEMS[0] = \memtype }{ C \vdashinstr \MEMORYGROW : [\I32] \to [\I32] }
- The memory C.\CMEMS[0] must be defined in the context.
- Then the instruction is valid with type [\I32~\I32~\I32] \to [].
\frac{ C.\CMEMS[0] = \memtype }{ C \vdashinstr \MEMORYFILL : [\I32~\I32~\I32] \to [] }
- The memory C.\CMEMS[0] must be defined in the context.
- Then the instruction is valid with type [\I32~\I32~\I32] \to [].
\frac{ C.\CMEMS[0] = \memtype }{ C \vdashinstr \MEMORYCOPY : [\I32~\I32~\I32] \to [] }
- The memory C.\CMEMS[0] must be defined in the context.
- The data segment C.\CDATA[x] must be defined in the context.
- Then the instruction is valid with type [\I32~\I32~\I32] \to [].
\frac{ C.\CMEMS[0] = \memtype \qquad C.\CDATA[x] = \segtype }{ C \vdashinstr \MEMORYINIT~x : [\I32~\I32~\I32] \to [] }
- The data segment C.\CDATA[x] must be defined in the context.
- Then the instruction is valid with type [] \to [].
\frac{ C.\CDATA[x] = \segtype }{ C \vdashinstr \DATADROP~x : [] \to [] }
.. index:: control instructions, structured control, label, block, branch, result type, label index, function index, type index, vector, polymorphism, context pair: validation; instruction single: abstract syntax; instruction
- The instruction is valid with type [] \to [].
\frac{ }{ C \vdashinstr \NOP : [] \to [] }
- The instruction is valid with type [t_1^\ast] \to [t_2^\ast], for any sequences of :ref:`value types <syntax-valtype>` t_1^\ast and t_2^\ast.
\frac{ }{ C \vdashinstr \UNREACHABLE : [t_1^\ast] \to [t_2^\ast] }
Note
The |UNREACHABLE| instruction is :ref:`stack-polymorphic <polymorphism>`.
- Let C' be the same :ref:`context <context>` as C, but with the :ref:`result type <syntax-resulttype>` [t^?] prepended to the |CLABELS| vector.
- Under context C', the instruction sequence \instr^\ast must be :ref:`valid <valid-instr-seq>` with type [] \to [t^?].
- Then the compound instruction is valid with type [] \to [t^?].
\frac{ C,\CLABELS\,[t^?] \vdashinstrseq \instr^\ast : [] \to [t^?] }{ C \vdashinstr \BLOCK~[t^?]~\instr^\ast~\END : [] \to [t^?] }
Note
The :ref:`notation <notation-extend>` C,\CLABELS\,[t^?] inserts the new label type at index 0, shifting all others.
The fact that the nested instruction sequence \instr^\ast must have type [] \to [t^?] implies that it cannot access operands that have been pushed on the stack before the block was entered. This may be generalized in future versions of WebAssembly.
- Let C' be the same :ref:`context <context>` as C, but with the empty :ref:`result type <syntax-resulttype>` [] prepended to the |CLABELS| vector.
- Under context C', the instruction sequence \instr^\ast must be :ref:`valid <valid-instr-seq>` with type [] \to [t^?].
- Then the compound instruction is valid with type [] \to [t^?].
\frac{ C,\CLABELS\,[] \vdashinstrseq \instr^\ast : [] \to [t^?] }{ C \vdashinstr \LOOP~[t^?]~\instr^\ast~\END : [] \to [t^?] }
Note
The :ref:`notation <notation-extend>` C,\CLABELS\,[] inserts the new label type at index 0, shifting all others.
The fact that the nested instruction sequence \instr^\ast must have type [] \to [t^?] implies that it cannot access operands that have been pushed on the stack before the loop was entered. This may be generalized in future versions of WebAssembly.
- Let C' be the same :ref:`context <context>` as C, but with the :ref:`result type <syntax-resulttype>` [t^?] prepended to the |CLABELS| vector.
- Under context C', the instruction sequence \instr_1^\ast must be :ref:`valid <valid-instr-seq>` with type [] \to [t^?].
- Under context C', the instruction sequence \instr_2^\ast must be :ref:`valid <valid-instr-seq>` with type [] \to [t^?].
- Then the compound instruction is valid with type [\I32] \to [t^?].
\frac{ C,\CLABELS\,[t^?] \vdashinstrseq \instr_1^\ast : [] \to [t^?] \qquad C,\CLABELS\,[t^?] \vdashinstrseq \instr_2^\ast : [] \to [t^?] }{ C \vdashinstr \IF~[t^?]~\instr_1^\ast~\ELSE~\instr_2^\ast~\END : [\I32] \to [t^?] }
Note
The :ref:`notation <notation-extend>` C,\CLABELS\,[t^?] inserts the new label type at index 0, shifting all others.
The fact that the nested instruction sequence \instr^\ast must have type [] \to [t^?] implies that it cannot access operands that have been pushed on the stack before the conditional was entered. This may be generalized in future versions of WebAssembly.
- The label C.\CLABELS[l] must be defined in the context.
- Let [t^?] be the :ref:`result type <syntax-resulttype>` C.\CLABELS[l].
- Then the instruction is valid with type [t_1^\ast~t^?] \to [t_2^\ast], for any sequences of :ref:`value types <syntax-valtype>` t_1^\ast and t_2^\ast.
\frac{ C.\CLABELS[l] = [t^?] }{ C \vdashinstr \BR~l : [t_1^\ast~t^?] \to [t_2^\ast] }
Note
The :ref:`label index <syntax-labelidx>` space in the :ref:`context <context>` C contains the most recent label first, so that C.\CLABELS[l] performs a relative lookup as expected.
The |BR| instruction is :ref:`stack-polymorphic <polymorphism>`.
- The label C.\CLABELS[l] must be defined in the context.
- Let [t^?] be the :ref:`result type <syntax-resulttype>` C.\CLABELS[l].
- Then the instruction is valid with type [t^?~\I32] \to [t^?].
\frac{ C.\CLABELS[l] = [t^?] }{ C \vdashinstr \BRIF~l : [t^?~\I32] \to [t^?] }
Note
The :ref:`label index <syntax-labelidx>` space in the :ref:`context <context>` C contains the most recent label first, so that C.\CLABELS[l] performs a relative lookup as expected.
- The label C.\CLABELS[l_N] must be defined in the context.
- Let [t^?] be the :ref:`result type <syntax-resulttype>` C.\CLABELS[l_N].
- For all l_i in l^\ast, the label C.\CLABELS[l_i] must be defined in the context.
- For all l_i in l^\ast, C.\CLABELS[l_i] must be [t^?].
- Then the instruction is valid with type [t_1^\ast~t^?~\I32] \to [t_2^\ast], for any sequences of :ref:`value types <syntax-valtype>` t_1^\ast and t_2^\ast.
\frac{ (C.\CLABELS[l] = [t^?])^\ast \qquad C.\CLABELS[l_N] = [t^?] }{ C \vdashinstr \BRTABLE~l^\ast~l_N : [t_1^\ast~t^?~\I32] \to [t_2^\ast] }
Note
The :ref:`label index <syntax-labelidx>` space in the :ref:`context <context>` C contains the most recent label first, so that C.\CLABELS[l_i] performs a relative lookup as expected.
The |BRTABLE| instruction is :ref:`stack-polymorphic <polymorphism>`.
- The return type C.\CRETURN must not be absent in the context.
- Let [t^?] be the :ref:`result type <syntax-resulttype>` of C.\CRETURN.
- Then the instruction is valid with type [t_1^\ast~t^?] \to [t_2^\ast], for any sequences of :ref:`value types <syntax-valtype>` t_1^\ast and t_2^\ast.
\frac{ C.\CRETURN = [t^?] }{ C \vdashinstr \RETURN : [t_1^\ast~t^?] \to [t_2^\ast] }
Note
The |RETURN| instruction is :ref:`stack-polymorphic <polymorphism>`.
C.\CRETURN is absent (set to \epsilon) when validating an :ref:`expression <valid-expr>` 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.
- The function C.\CFUNCS[x] must be defined in the context.
- Then the instruction is valid with type C.\CFUNCS[x].
\frac{ C.\CFUNCS[x] = [t_1^\ast] \to [t_2^\ast] }{ C \vdashinstr \CALL~x : [t_1^\ast] \to [t_2^\ast] }
- The table C.\CTABLES[0] must be defined in the context.
- Let \limits~\elemtype be the :ref:`table type <syntax-tabletype>` C.\CTABLES[0].
- The :ref:`element type <syntax-elemtype>` \elemtype must be |FUNCREF|.
- The type C.\CTYPES[x] must be defined in the context.
- Let [t_1^\ast] \to [t_2^\ast] be the :ref:`function type <syntax-functype>` C.\CTYPES[x].
- Then the instruction is valid with type [t_1^\ast~\I32] \to [t_2^\ast].
\frac{ C.\CTABLES[0] = \limits~\FUNCREF \qquad C.\CTYPES[x] = [t_1^\ast] \to [t_2^\ast] }{ C \vdashinstr \CALLINDIRECT~x : [t_1^\ast~\I32] \to [t_2^\ast] }
.. index:: instruction, instruction sequence
Typing of instruction sequences is defined recursively.
- The empty instruction sequence is valid with type [t^\ast] \to [t^\ast], for any sequence of :ref:`value types <syntax-valtype>` t^\ast.
\frac{ }{ C \vdashinstrseq \epsilon : [t^\ast] \to [t^\ast] }
- The instruction sequence \instr^\ast must be valid with type [t_1^\ast] \to [t_2^\ast], for some sequences of :ref:`value types <syntax-valtype>` t_1^\ast and t_2^\ast.
- The instruction \instr_N must be valid with type [t^\ast] \to [t_3^\ast], for some sequences of :ref:`value types <syntax-valtype>` t^\ast and t_3^\ast.
- There must be a sequence of :ref:`value types <syntax-valtype>` t_0^\ast, such that t_2^\ast = t_0^\ast~t^\ast.
- Then the combined instruction sequence is valid with type [t_1^\ast] \to [t_0^\ast~t_3^\ast].
\frac{ C \vdashinstrseq \instr^\ast : [t_1^\ast] \to [t_0^\ast~t^\ast] \qquad C \vdashinstr \instr_N : [t^\ast] \to [t_3^\ast] }{ C \vdashinstrseq \instr^\ast~\instr_N : [t_1^\ast] \to [t_0^\ast~t_3^\ast] }
.. index:: expression pair: validation; expression single: abstract syntax; expression single: expression; constant
Expressions \expr are classified by :ref:`result types <syntax-resulttype>` of the form [t^?].
- The instruction sequence \instr^\ast must be :ref:`valid <valid-instr-seq>` with type [] \to [t^?], for some optional :ref:`value type <syntax-valtype>` t^?.
- Then the expression is valid with :ref:`result type <syntax-resulttype>` [t^?].
\frac{ C \vdashinstrseq \instr^\ast : [] \to [t^?] }{ C \vdashexpr \instr^\ast~\END : [t^?] }
.. index:: ! constant
- In a constant expression \instr^\ast~\END all instructions in \instr^\ast must be constant.
- A constant instruction \instr must be:
- either of the form t.\CONST~c,
- or of the form \GLOBALGET~x, in which case C.\CGLOBALS[x] must be a :ref:`global type <syntax-globaltype>` of the form \CONST~t.
\frac{ (C \vdashinstrconst \instr \const)^\ast }{ C \vdashexprconst \instr^\ast~\END \const }
\frac{ }{ C \vdashinstrconst t.\CONST~c \const } \qquad \frac{ C.\CGLOBALS[x] = \CONST~t }{ C \vdashinstrconst \GLOBALGET~x \const }
Note
Currently, constant expressions occurring as initializers of :ref:`globals <syntax-global>` are further constrained in that contained |GLOBALGET| instructions are only allowed to refer to imported globals. This is enforced in the :ref:`validation rule for modules <valid-module>` by constraining the context C accordingly.
The definition of constant expression may be extended in future versions of WebAssembly.