Instructions

Instructions are classified by function types \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\) that describe how they manipulate the operand 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 \(\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 function types \([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) function 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 value type \(t\). Consequently, both instruction sequences

\[(\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~1)~~(\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~2)~~(\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~3)~~\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}}{}\]

and

\[(\href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~1.0)~~(\href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~2.0)~~(\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~3)~~\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}}{}\]

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 value types \(t_1^\ast\) and \(t_2^\ast\). Consequently,

\[\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{unreachable}}~~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{add}}\]

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,

\[\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{unreachable}}~~(\href{../syntax/types.html#syntax-valtype}{\mathsf{i64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~0)~~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{add}}\]

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.

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]\).
\[\frac{ }{ C \href{../valid/instructions.html#valid-instr}{\vdash} t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c : [] \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]\).
\[\frac{ }{ C \href{../valid/instructions.html#valid-instr}{\vdash} t\mathsf{.}\href{../syntax/instructions.html#syntax-unop}{\mathit{unop}} : [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]\).
\[\frac{ }{ C \href{../valid/instructions.html#valid-instr}{\vdash} t\mathsf{.}\href{../syntax/instructions.html#syntax-binop}{\mathit{binop}} : [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}}]\).
\[\frac{ }{ C \href{../valid/instructions.html#valid-instr}{\vdash} t\mathsf{.}\href{../syntax/instructions.html#syntax-testop}{\mathit{testop}} : [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}}]\).
\[\frac{ }{ C \href{../valid/instructions.html#valid-instr}{\vdash} t\mathsf{.}\href{../syntax/instructions.html#syntax-relop}{\mathit{relop}} : [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}}/t_1\)

  • The instruction is valid with type \([t_1] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2]\).
\[\frac{ }{ C \href{../valid/instructions.html#valid-instr}{\vdash} t_2\mathsf{.}\href{../syntax/instructions.html#syntax-cvtop}{\mathit{cvtop}}/t_1 : [t_1] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2] }\]

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 value type \(t\).
\[\frac{ }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{drop}} : [t] \href{../syntax/types.html#syntax-functype}{\rightarrow} [] }\]

\(\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}}\)

  • 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 value type \(t\).
\[\frac{ }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}} : [t~t~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t] }\]

Note

Both \(\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{drop}}\) and \(\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}}\) are value-polymorphic instructions.

Variable Instructions

\(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{get\_local}}~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]\).
\[\frac{ C.\href{../valid/conventions.html#context}{\mathsf{locals}}[x] = t }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{get\_local}}~x : [] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t] }\]

\(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{set\_local}}~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} []\).
\[\frac{ C.\href{../valid/conventions.html#context}{\mathsf{locals}}[x] = t }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{set\_local}}~x : [t] \href{../syntax/types.html#syntax-functype}{\rightarrow} [] }\]

\(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{tee\_local}}~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]\).
\[\frac{ C.\href{../valid/conventions.html#context}{\mathsf{locals}}[x] = t }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{tee\_local}}~x : [t] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t] }\]

\(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{get\_global}}~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]\).
\[\frac{ C.\href{../valid/conventions.html#context}{\mathsf{globals}}[x] = \href{../syntax/types.html#syntax-mut}{\mathit{mut}}~t }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{get\_global}}~x : [] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t] }\]

\(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{set\_global}}~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} []\).
\[\frac{ C.\href{../valid/conventions.html#context}{\mathsf{globals}}[x] = \href{../syntax/types.html#syntax-mut}{\mathsf{var}}~t }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{set\_global}}~x : [t] \href{../syntax/types.html#syntax-functype}{\rightarrow} [] }\]

Memory Instructions

\(t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{load}}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}\)

  • The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[0]\) 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 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]\).
\[\frac{ C.\href{../valid/conventions.html#context}{\mathsf{mems}}[0] = \href{../syntax/types.html#syntax-memtype}{\mathit{memtype}} \qquad 2^{\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{align}}} \leq |t|/8 }{ C \href{../valid/instructions.html#valid-instr}{\vdash} t\mathsf{.load}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}} : [\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}}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}\)

  • The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[0]\) 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]\).
\[\frac{ C.\href{../valid/conventions.html#context}{\mathsf{mems}}[0] = \href{../syntax/types.html#syntax-memtype}{\mathit{memtype}} \qquad 2^{\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{align}}} \leq N/8 }{ C \href{../valid/instructions.html#valid-instr}{\vdash} t\mathsf{.load}N\mathsf{\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}} : [\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}}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}\)

  • The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[0]\) 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 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} []\).
\[\frac{ C.\href{../valid/conventions.html#context}{\mathsf{mems}}[0] = \href{../syntax/types.html#syntax-memtype}{\mathit{memtype}} \qquad 2^{\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{align}}} \leq |t|/8 }{ C \href{../valid/instructions.html#valid-instr}{\vdash} t\mathsf{.store}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}} : [\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}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}\)

  • The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[0]\) 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} []\).
\[\frac{ C.\href{../valid/conventions.html#context}{\mathsf{mems}}[0] = \href{../syntax/types.html#syntax-memtype}{\mathit{memtype}} \qquad 2^{\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{align}}} \leq N/8 }{ C \href{../valid/instructions.html#valid-instr}{\vdash} t\mathsf{.store}N~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}} : [\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}~t] \href{../syntax/types.html#syntax-functype}{\rightarrow} [] }\]

\(\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{current\_memory}}\)

  • The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[0]\) 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}}]\).
\[\frac{ C.\href{../valid/conventions.html#context}{\mathsf{mems}}[0] = \href{../syntax/types.html#syntax-memtype}{\mathit{memtype}} }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{current\_memory}} : [] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] }\]

\(\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{grow\_memory}}\)

  • The memory \(C.\href{../valid/conventions.html#context}{\mathsf{mems}}[0]\) 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}}]\).
\[\frac{ C.\href{../valid/conventions.html#context}{\mathsf{mems}}[0] = \href{../syntax/types.html#syntax-memtype}{\mathit{memtype}} }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{grow\_memory}} : [\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] }\]

Control Instructions

\(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{nop}}\)

  • The instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\).
\[\frac{ }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-control}{\mathsf{nop}} : [] \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 value types \(t_1^\ast\) and \(t_2^\ast\).
\[\frac{ }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-control}{\mathsf{unreachable}} : [t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [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}}~[t^?]~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}\)

  • Let \(C'\) be the same context as \(C\), but with the result type \([t^?]\) 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 \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?]\).
  • Then the compound instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?]\).
\[\frac{ C,\href{../valid/conventions.html#context}{\mathsf{labels}}\,[t^?] \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast : [] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?] }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-control}{\mathsf{block}}~[t^?]~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} : [] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?] }\]

Note

The fact that the nested instruction sequence \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\) must have type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [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.

\(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{loop}}~[t^?]~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}\)

  • Let \(C'\) be the same context as \(C\), but with the empty result type \([]\) 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 \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?]\).
  • Then the compound instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?]\).
\[\frac{ C,\href{../valid/conventions.html#context}{\mathsf{labels}}\,[] \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast : [] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?] }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-control}{\mathsf{loop}}~[t^?]~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} : [] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?] }\]

Note

The fact that the nested instruction sequence \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\) must have type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [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.

\(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{if}}~[t^?]~\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}}\)

  • Let \(C'\) be the same context as \(C\), but with the empty result type \([t^?]\) 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 \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?]\).
  • Under context \(C'\), the instruction sequence \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_2^\ast\) must be valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?]\).
  • Then the compound instruction is valid with type \([\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?]\).
\[\frac{ C,\href{../valid/conventions.html#context}{\mathsf{labels}}\,[t^?] \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_1^\ast : [] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?] \qquad C,\href{../valid/conventions.html#context}{\mathsf{labels}}\,[t^?] \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_2^\ast : [] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?] }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-control}{\mathsf{if}}~[t^?]~\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}} : [\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?] }\]

Note

The fact that the nested instruction sequence \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\) must have type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [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.

\(\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^?]\) be the result type \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l]\).
  • Then the instruction is valid with type \([t_1^\ast~t^?] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\), for any sequences of value types \(t_1^\ast\) and \(t_2^\ast\).
\[\frac{ C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l] = [t^?] }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br}}~l : [t_1^\ast~t^?] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast] }\]

Note

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^?]\) be the result type \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l]\).
  • Then the instruction is valid with type \([t^?~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?]\).
\[\frac{ C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l] = [t^?] }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br\_if}}~l : [t^?~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?] }\]

\(\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]\) must be defined in the context.
  • Let \([t^?]\) be the result type \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l_N]\).
  • For all \(l_i\) in \(l^\ast\), the label \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l_i]\) must be defined in the context.
  • For all \(l_i\) in \(l^\ast\), \(C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l_i]\) must be \(t^?\).
  • Then the instruction is valid with type \([t_1^\ast~t^?~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\), for any sequences of value types \(t_1^\ast\) and \(t_2^\ast\).
\[\frac{ (C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l] = [t^?])^\ast \qquad C.\href{../valid/conventions.html#context}{\mathsf{labels}}[l_N] = [t^?] }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br\_table}}~l^\ast~l_N : [t_1^\ast~t^?~\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast] }\]

Note

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 empty in the context.
  • Let \([t^?]\) be the result type of \(C.\href{../valid/conventions.html#context}{\mathsf{return}}\).
  • Then the instruction is valid with type \([t_1^\ast~t^?] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\), for any sequences of value types \(t_1^\ast\) and \(t_2^\ast\).
\[\frac{ C.\href{../valid/conventions.html#context}{\mathsf{return}} = [t^?] }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-control}{\mathsf{return}} : [t_1^\ast~t^?] \href{../syntax/types.html#syntax-functype}{\rightarrow} [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 empty (\(\epsilon\)) when validating an expression that is not a function body. This differs from it being set to the empty result type (\([]\)), 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]\).
\[\frac{ C.\href{../valid/conventions.html#context}{\mathsf{funcs}}[x] = [t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast] }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-control}{\mathsf{call}}~x : [t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast] }\]

\(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{call\_indirect}}~x\)

  • The table \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[0]\) must be defined in the context.
  • Let \(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}~\href{../syntax/types.html#syntax-elemtype}{\mathit{elemtype}}\) be the table type \(C.\href{../valid/conventions.html#context}{\mathsf{tables}}[0]\).
  • The element type \(\href{../syntax/types.html#syntax-elemtype}{\mathit{elemtype}}\) must be \(\href{../syntax/types.html#syntax-elemtype}{\mathsf{anyfunc}}\).
  • The type \(C.\href{../valid/conventions.html#context}{\mathsf{types}}[x]\) must be defined in the context.
  • Then the instruction is valid with type \(C.\href{../valid/conventions.html#context}{\mathsf{types}}[x]\).
\[\frac{ C.\href{../valid/conventions.html#context}{\mathsf{tables}}[0] = \href{../syntax/types.html#syntax-limits}{\mathit{limits}}~\href{../syntax/types.html#syntax-elemtype}{\mathsf{anyfunc}} \qquad C.\href{../valid/conventions.html#context}{\mathsf{types}}[x] = [t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast] }{ C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr-control}{\mathsf{call\_indirect}}~x : [t_1^\ast] \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 value types \(t^\ast\).
\[\frac{ }{ C \href{../valid/instructions.html#valid-instr-seq}{\vdash} \epsilon : [t^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [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 value 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 value types \(t^\ast\) and \(t_3^\ast\).
  • There must be a sequence of value types \(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] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_0^\ast~t_3^\ast]\).
\[\frac{ C \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast : [t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_0^\ast~t^\ast] \qquad C \href{../valid/instructions.html#valid-instr}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_N : [t^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_3^\ast] }{ C \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_N : [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^?]\).

\(\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 type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?]\), for some optional value type \(t^?\).
  • Then the expression is valid with result type \([t^?]\).
\[\frac{ C \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast : [] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^?] }{ C \href{../valid/instructions.html#valid-expr}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} : [t^?] }\]

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-variable}{\mathsf{get\_global}}~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\).
\[\frac{ (C \href{../valid/instructions.html#valid-constant}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}} \href{../valid/instructions.html#valid-constant}{\mathrel{\mbox{const}}})^\ast }{ C \href{../valid/instructions.html#valid-constant}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} \href{../valid/instructions.html#valid-constant}{\mathrel{\mbox{const}}} }\]
\[\frac{ }{ C \href{../valid/instructions.html#valid-constant}{\vdash} t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c \href{../valid/instructions.html#valid-constant}{\mathrel{\mbox{const}}} } \qquad \frac{ C.\href{../valid/conventions.html#context}{\mathsf{globals}}[x] = \href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~t }{ C \href{../valid/instructions.html#valid-constant}{\vdash} \href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{get\_global}}~x \href{../valid/instructions.html#valid-constant}{\mathrel{\mbox{const}}} }\]

Note

The definition of constant expression may be extended in future versions of WebAssembly.