Skip to content
This repository was archived by the owner on Nov 3, 2021. It is now read-only.

Latest commit

 

History

History
943 lines (624 loc) · 24.6 KB

instructions.rst

File metadata and controls

943 lines (624 loc) · 24.6 KB
.. index:: instruction, function type, context, value, operand stack, ! polymorphism

Instructions

: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:

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

Numeric Instructions

t\K{.}\CONST~c

  • The instruction is valid with type [] \to [t].
\frac{
}{
  C \vdashinstr t\K{.}\CONST~c : [] \to [t]
}

t\K{.}\unop

  • The instruction is valid with type [t] \to [t].
\frac{
}{
  C \vdashinstr t\K{.}\unop : [t] \to [t]
}

t\K{.}\binop

  • The instruction is valid with type [t~t] \to [t].
\frac{
}{
  C \vdashinstr t\K{.}\binop : [t~t] \to [t]
}

t\K{.}\testop

  • The instruction is valid with type [t] \to [\I32].
\frac{
}{
  C \vdashinstr t\K{.}\testop : [t] \to [\I32]
}

t\K{.}\relop

  • The instruction is valid with type [t~t] \to [\I32].
\frac{
}{
  C \vdashinstr t\K{.}\relop : [t~t] \to [\I32]
}

t_2\K{.}\cvtop\K{\_}t_1\K{\_}\sx^?

  • 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

Parametric Instructions

\DROP

\frac{
}{
  C \vdashinstr \DROP : [t] \to []
}

\SELECT

\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

Variable Instructions

\LOCALGET~x

  • 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]
}

\LOCALSET~x

  • 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 []
}

\LOCALTEE~x

  • 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]
}

\GLOBALGET~x

\frac{
  C.\CGLOBALS[x] = \mut~t
}{
  C \vdashinstr \GLOBALGET~x : [] \to [t]
}

\GLOBALSET~x

  • 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

Table Instructions

\TABLECOPY

  • 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 []
}

\TABLEINIT~x

  • 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 []
}

\ELEMDROP~x

  • 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

Memory Instructions

t\K{.}\LOAD~\memarg

  • 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]
}

t\K{.}\LOAD{N}\K{\_}\sx~\memarg

  • 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]
}

t\K{.}\STORE~\memarg

  • 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 []
}

t\K{.}\STORE{N}~\memarg

  • 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 []
}

\MEMORYSIZE

  • 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]
}

\MEMORYGROW

  • 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]
}

\MEMORYFILL

  • 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 []
}

\MEMORYCOPY

  • 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 []
}

\MEMORYINIT~x

  • 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 []
}

\DATADROP~x

  • 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

Control Instructions

\NOP

  • The instruction is valid with type [] \to [].
\frac{
}{
  C \vdashinstr \NOP : [] \to []
}

\UNREACHABLE

\frac{
}{
  C \vdashinstr \UNREACHABLE : [t_1^\ast] \to [t_2^\ast]
}

\BLOCK~[t^?]~\instr^\ast~\END

\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.

\LOOP~[t^?]~\instr^\ast~\END

\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.

\IF~[t^?]~\instr_1^\ast~\ELSE~\instr_2^\ast~\END

\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.

\BR~l

\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>`.

\BRIF~l

  • 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.

\BRTABLE~l^\ast~l_N

  • 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>`.

\RETURN

\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.

\CALL~x

  • 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]
}

\CALLINDIRECT~x

\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

Instruction Sequences

Typing of instruction sequences is defined recursively.

Empty Instruction Sequence: \epsilon

\frac{
}{
  C \vdashinstrseq \epsilon : [t^\ast] \to [t^\ast]
}

Non-empty Instruction Sequence: \instr^\ast~\instr_N

  • 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

Expressions \expr are classified by :ref:`result types <syntax-resulttype>` of the form [t^?].

\instr^\ast~\END

\frac{
  C \vdashinstrseq \instr^\ast : [] \to [t^?]
}{
  C \vdashexpr \instr^\ast~\END : [t^?]
}
.. index:: ! constant

Constant Expressions

  • In a constant expression \instr^\ast~\END all instructions in \instr^\ast must be constant.
  • A constant instruction \instr must be:
\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.