Instructions
WebAssembly code consists of sequences of instructions.
Its computational model is based on a stack machine in that instructions manipulate values on an implicit operand stack,
consuming (popping) argument values and producing or returning (pushing) result values.
In addition to dynamic operands from the stack, some instructions also have static immediate arguments,
typically indices or type annotations,
which are part of the instruction itself.
Some instructions are structured in that they bracket nested sequences of instructions.
The following sections group instructions into a number of different categories.
Numeric Instructions
Numeric instructions provide basic operations over numeric values of specific type.
These operations closely match respective operations available in hardware.
\[\begin{split}\begin{array}{llcl}
\def\mathdef1619#1{{}}\mathdef1619{width} & \mathit{nn}, \mathit{mm} &::=&
\mathsf{32} ~|~ \mathsf{64} \\
\def\mathdef1619#1{{}}\mathdef1619{signedness} & \href{../syntax/instructions.html#syntax-sx}{\mathit{sx}} &::=&
\mathsf{u} ~|~ \mathsf{s} \\
\def\mathdef1619#1{{}}\mathdef1619{instruction} & \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}} &::=&
\mathsf{i}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~\href{../syntax/values.html#syntax-int}{\def\mathdef1656#1{{\mathit{i#1}}}\mathdef1656{\mathit{nn}}} ~|~
\mathsf{f}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~\href{../syntax/values.html#syntax-float}{\def\mathdef1657#1{{\mathit{f#1}}}\mathdef1657{\mathit{nn}}} \\&&|&
\mathsf{i}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-iunop}{\mathit{iunop}} ~|~
\mathsf{f}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-funop}{\mathit{funop}} \\&&|&
\mathsf{i}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-ibinop}{\mathit{ibinop}} ~|~
\mathsf{f}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-fbinop}{\mathit{fbinop}} \\&&|&
\mathsf{i}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-itestop}{\mathit{itestop}} \\&&|&
\mathsf{i}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-irelop}{\mathit{irelop}} ~|~
\mathsf{f}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-frelop}{\mathit{frelop}} \\&&|&
\mathsf{i}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{extend}}\mathsf{8\_s} ~|~
\mathsf{i}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{extend}}\mathsf{16\_s} ~|~
\mathsf{i64.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{extend}}\mathsf{32\_s} \\&&|&
\mathsf{i32.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{wrap}}\mathsf{\_i64} ~|~
\mathsf{i64.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{extend}}\mathsf{\_i32}\mathsf{\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}} ~|~
\mathsf{i}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{trunc}}\mathsf{\_f}\mathit{mm}\mathsf{\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}} \\&&|&
\mathsf{i}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{trunc}}\mathsf{\_sat\_f}\mathit{mm}\mathsf{\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}} \\&&|&
\mathsf{f32.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{demote}}\mathsf{\_f64} ~|~
\mathsf{f64.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{promote}}\mathsf{\_f32} ~|~
\mathsf{f}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{convert}}\mathsf{\_i}\mathit{mm}\mathsf{\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}} \\&&|&
\mathsf{i}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{reinterpret}}\mathsf{\_f}\mathit{nn} ~|~
\mathsf{f}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{reinterpret}}\mathsf{\_i}\mathit{nn} \\&&|&
\dots \\
\def\mathdef1619#1{{}}\mathdef1619{integer unary operator} & \href{../syntax/instructions.html#syntax-iunop}{\mathit{iunop}} &::=&
\mathsf{clz} ~|~
\mathsf{ctz} ~|~
\mathsf{popcnt} \\
\def\mathdef1619#1{{}}\mathdef1619{integer binary operator} & \href{../syntax/instructions.html#syntax-ibinop}{\mathit{ibinop}} &::=&
\mathsf{add} ~|~
\mathsf{sub} ~|~
\mathsf{mul} ~|~
\mathsf{div\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}} ~|~
\mathsf{rem\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}} \\&&|&
\mathsf{and} ~|~
\mathsf{or} ~|~
\mathsf{xor} ~|~
\mathsf{shl} ~|~
\mathsf{shr\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}} ~|~
\mathsf{rotl} ~|~
\mathsf{rotr} \\
\def\mathdef1619#1{{}}\mathdef1619{floating-point unary operator} & \href{../syntax/instructions.html#syntax-funop}{\mathit{funop}} &::=&
\mathsf{abs} ~|~
\mathsf{neg} ~|~
\mathsf{sqrt} ~|~
\mathsf{ceil} ~|~
\mathsf{floor} ~|~
\mathsf{trunc} ~|~
\mathsf{nearest} \\
\def\mathdef1619#1{{}}\mathdef1619{floating-point binary operator} & \href{../syntax/instructions.html#syntax-fbinop}{\mathit{fbinop}} &::=&
\mathsf{add} ~|~
\mathsf{sub} ~|~
\mathsf{mul} ~|~
\mathsf{div} ~|~
\mathsf{min} ~|~
\mathsf{max} ~|~
\mathsf{copysign} \\
\def\mathdef1619#1{{}}\mathdef1619{integer test operator} & \href{../syntax/instructions.html#syntax-itestop}{\mathit{itestop}} &::=&
\mathsf{eqz} \\
\def\mathdef1619#1{{}}\mathdef1619{integer relational operator} & \href{../syntax/instructions.html#syntax-irelop}{\mathit{irelop}} &::=&
\mathsf{eq} ~|~
\mathsf{ne} ~|~
\mathsf{lt\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}} ~|~
\mathsf{gt\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}} ~|~
\mathsf{le\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}} ~|~
\mathsf{ge\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}} \\
\def\mathdef1619#1{{}}\mathdef1619{floating-point relational operator} & \href{../syntax/instructions.html#syntax-frelop}{\mathit{frelop}} &::=&
\mathsf{eq} ~|~
\mathsf{ne} ~|~
\mathsf{lt} ~|~
\mathsf{gt} ~|~
\mathsf{le} ~|~
\mathsf{ge} \\
\end{array}\end{split}\]
Numeric instructions are divided by value type.
For each type, several subcategories can be distinguished:
Constants: return a static constant.
Unary Operations: consume one operand and produce one result of the respective type.
Binary Operations: consume two operands and produce one result of the respective type.
Tests: consume one operand of the respective type and produce a Boolean integer result.
Comparisons: consume two operands of the respective type and produce a Boolean integer result.
Conversions: consume a value of one type and produce a result of another
(the source type of the conversion is the one after the “\(\mathsf{\_}\)”).
Some integer instructions come in two flavors,
where a signedness annotation \(\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}\) distinguishes whether the operands are to be interpreted as unsigned or signed integers.
For the other integer instructions, the use of two’s complement for the signed interpretation means that they behave the same regardless of signedness.
Conventions
Occasionally, it is convenient to group operators together according to the following grammar shorthands:
\[\begin{split}\begin{array}{llll}
\def\mathdef1619#1{{}}\mathdef1619{unary operator} & \href{../syntax/instructions.html#syntax-unop}{\mathit{unop}} &::=&
\href{../syntax/instructions.html#syntax-iunop}{\mathit{iunop}} ~|~
\href{../syntax/instructions.html#syntax-funop}{\mathit{funop}} ~|~
\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{extend}}{N}\mathsf{\_s} \\
\def\mathdef1619#1{{}}\mathdef1619{binary operator} & \href{../syntax/instructions.html#syntax-binop}{\mathit{binop}} &::=& \href{../syntax/instructions.html#syntax-ibinop}{\mathit{ibinop}} ~|~ \href{../syntax/instructions.html#syntax-fbinop}{\mathit{fbinop}} \\
\def\mathdef1619#1{{}}\mathdef1619{test operator} & \href{../syntax/instructions.html#syntax-testop}{\mathit{testop}} &::=& \href{../syntax/instructions.html#syntax-itestop}{\mathit{itestop}} \\
\def\mathdef1619#1{{}}\mathdef1619{relational operator} & \href{../syntax/instructions.html#syntax-relop}{\mathit{relop}} &::=& \href{../syntax/instructions.html#syntax-irelop}{\mathit{irelop}} ~|~ \href{../syntax/instructions.html#syntax-frelop}{\mathit{frelop}} \\
\def\mathdef1619#1{{}}\mathdef1619{conversion operator} & \href{../syntax/instructions.html#syntax-cvtop}{\mathit{cvtop}} &::=&
\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{wrap}} ~|~
\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{extend}} ~|~
\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{trunc}} ~|~
\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{trunc}}\mathsf{\_sat} ~|~
\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{convert}} ~|~
\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{demote}} ~|~
\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{promote}} ~|~
\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{reinterpret}} \\
\end{array}\end{split}\]
Reference Instructions
Instructions in this group are concerned with accessing references.
\[\begin{split}\begin{array}{llcl}
\def\mathdef1619#1{{}}\mathdef1619{instruction} & \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}} &::=&
\dots \\&&|&
\href{../syntax/instructions.html#syntax-instr-ref}{\mathsf{ref{.}null}}~\href{../syntax/types.html#syntax-reftype}{\mathit{reftype}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-ref}{\mathsf{ref{.}is\_null}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-ref}{\mathsf{ref{.}func}}~\href{../syntax/modules.html#syntax-funcidx}{\mathit{funcidx}} \\
\end{array}\end{split}\]
These instruction produce a null value, check for a null value, or produce a reference to a given function, respectively.
Parametric Instructions
Instructions in this group can operate on operands of any value type.
\[\begin{split}\begin{array}{llcl}
\def\mathdef1619#1{{}}\mathdef1619{instruction} & \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}} &::=&
\dots \\&&|&
\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{drop}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}}~(\href{../syntax/types.html#syntax-valtype}{\mathit{valtype}}^\ast)^? \\
\end{array}\end{split}\]
The \(\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{drop}}\) instruction simply throws away a single operand.
The \(\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}}\) instruction selects one of its first two operands based on whether its third operand is zero or not.
It may include a value type determining the type of these operands. If missing, the operands must be of numeric type.
Note
In future versions of WebAssembly, the type annotation on \(\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}}\) may allow for more than a single value being selected at the same time.
Variable Instructions
Variable instructions are concerned with access to local or global variables.
\[\begin{split}\begin{array}{llcl}
\def\mathdef1619#1{{}}\mathdef1619{instruction} & \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}} &::=&
\dots \\&&|&
\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{local.get}}~\href{../syntax/modules.html#syntax-localidx}{\mathit{localidx}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{local.set}}~\href{../syntax/modules.html#syntax-localidx}{\mathit{localidx}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{local.tee}}~\href{../syntax/modules.html#syntax-localidx}{\mathit{localidx}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{global.get}}~\href{../syntax/modules.html#syntax-globalidx}{\mathit{globalidx}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{global.set}}~\href{../syntax/modules.html#syntax-globalidx}{\mathit{globalidx}} \\
\end{array}\end{split}\]
These instructions get or set the values of variables, respectively.
The \(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{local.tee}}\) instruction is like \(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{local.set}}\) but also returns its argument.
Table Instructions
Instructions in this group are concerned with tables table.
\[\begin{split}\begin{array}{llcl}
\def\mathdef1619#1{{}}\mathdef1619{instruction} & \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}} &::=&
\dots \\&&|&
\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.get}}~\href{../syntax/modules.html#syntax-tableidx}{\mathit{tableidx}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.set}}~\href{../syntax/modules.html#syntax-tableidx}{\mathit{tableidx}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.size}}~\href{../syntax/modules.html#syntax-tableidx}{\mathit{tableidx}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.grow}}~\href{../syntax/modules.html#syntax-tableidx}{\mathit{tableidx}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.fill}}~\href{../syntax/modules.html#syntax-tableidx}{\mathit{tableidx}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.copy}}~\href{../syntax/modules.html#syntax-tableidx}{\mathit{tableidx}}~\href{../syntax/modules.html#syntax-tableidx}{\mathit{tableidx}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.init}}~\href{../syntax/modules.html#syntax-tableidx}{\mathit{tableidx}}~\href{../syntax/modules.html#syntax-elemidx}{\mathit{elemidx}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{elem.drop}}~\href{../syntax/modules.html#syntax-elemidx}{\mathit{elemidx}} \\
\end{array}\end{split}\]
The \(\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.get}}\) and \(\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.set}}\) instructions load or store an element in a table, respectively.
The \(\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.size}}\) instruction returns the current size of a table.
The \(\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.grow}}\) instruction grows table by a given delta and returns the previous size, or \(-1\) if enough space cannot be allocated.
It also takes an initialization value for the newly allocated entries.
The \(\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.fill}}\) instruction sets all entries in a range to a given value.
The \(\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.copy}}\) instruction copies elements from a source table region to a possibly overlapping destination region; the first index denotes the destination.
The \(\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{table.init}}\) instruction copies elements from a passive element segment into a table.
The \(\href{../syntax/instructions.html#syntax-instr-table}{\mathsf{elem.drop}}\) instruction prevents further use of a passive element segment. This instruction is intended to be used as an optimization hint. After an element segment is dropped its elements can no longer be retrieved, so the memory used by this segment may be freed.
An additional instruction that accesses a table is the control instruction \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{call\_indirect}}\).
Memory Instructions
Instructions in this group are concerned with linear memory.
\[\begin{split}\begin{array}{llcl}
\def\mathdef1619#1{{}}\mathdef1619{memory immediate} & \href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}} &::=&
\{ \href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{offset}}~\href{../syntax/values.html#syntax-int}{\mathit{u32}}, \href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{align}}~\href{../syntax/values.html#syntax-int}{\mathit{u32}} \} \\
\def\mathdef1619#1{{}}\mathdef1619{instruction} & \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}} &::=&
\dots \\&&|&
\mathsf{i}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{load}}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}} ~|~
\mathsf{f}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{load}}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}} \\&&|&
\mathsf{i}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{store}}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}} ~|~
\mathsf{f}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{store}}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}} \\&&|&
\mathsf{i}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{load}}\mathsf{8\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}} ~|~
\mathsf{i}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{load}}\mathsf{16\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}} ~|~
\mathsf{i64.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{load}}\mathsf{32\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}} \\&&|&
\mathsf{i}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{store}}\mathsf{8}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}} ~|~
\mathsf{i}\mathit{nn}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{store}}\mathsf{16}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}} ~|~
\mathsf{i64.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{store}}\mathsf{32}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.size}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.grow}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.fill}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.copy}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.init}}~\href{../syntax/modules.html#syntax-dataidx}{\mathit{dataidx}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{data.drop}}~\href{../syntax/modules.html#syntax-dataidx}{\mathit{dataidx}} \\
\end{array}\end{split}\]
Memory is accessed with \(\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{load}}\) and \(\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{store}}\) instructions for the different value types.
They all take a memory immediate \(\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}\) that contains an address offset and the expected alignment (expressed as the exponent of a power of 2).
Integer loads and stores can optionally specify a storage size that is smaller than the bit width of the respective value type.
In the case of loads, a sign extension mode \(\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}\) is then required to select appropriate behavior.
The static address offset is added to the dynamic address operand, yielding a 33 bit effective address that is the zero-based index at which the memory is accessed.
All values are read and written in little endian byte order.
A trap results if any of the accessed memory bytes lies outside the address range implied by the memory’s current size.
Note
Future version of WebAssembly might provide memory instructions with 64 bit address ranges.
The \(\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.size}}\) instruction returns the current size of a memory.
The \(\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.grow}}\) instruction grows memory by a given delta and returns the previous size, or \(-1\) if enough memory cannot be allocated.
Both instructions operate in units of page size.
The \(\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.fill}}\) instruction sets all values in a region to a given byte.
The \(\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.copy}}\) instruction copies data from a source memory region to a possibly overlapping destination region.
The \(\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.init}}\) instruction copies data from a passive data segment into a memory.
The \(\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{data.drop}}\) instruction prevents further use of a passive data segment. This instruction is intended to be used as an optimization hint. After a data segment is dropped its data can no longer be retrieved, so the memory used by this segment may be freed.
Note
In the current version of WebAssembly,
all memory instructions implicitly operate on memory index \(0\).
This restriction may be lifted in future versions.
Control Instructions
Instructions in this group affect the flow of control.
\[\begin{split}\begin{array}{llcl}
\def\mathdef1619#1{{}}\mathdef1619{block type} & \href{../syntax/instructions.html#syntax-blocktype}{\mathit{blocktype}} &::=&
\href{../syntax/modules.html#syntax-typeidx}{\mathit{typeidx}} ~|~ \href{../syntax/types.html#syntax-valtype}{\mathit{valtype}}^? \\
\def\mathdef1619#1{{}}\mathdef1619{instruction} & \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}} &::=&
\dots \\&&|&
\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{nop}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{unreachable}} \\&&|&
\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}} \\&&|&
\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}} \\&&|&
\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}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{else}}~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br}}~\href{../syntax/modules.html#syntax-labelidx}{\mathit{labelidx}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br\_if}}~\href{../syntax/modules.html#syntax-labelidx}{\mathit{labelidx}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br\_table}}~\href{../syntax/conventions.html#syntax-vec}{\mathit{vec}}(\href{../syntax/modules.html#syntax-labelidx}{\mathit{labelidx}})~\href{../syntax/modules.html#syntax-labelidx}{\mathit{labelidx}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{return}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{call}}~\href{../syntax/modules.html#syntax-funcidx}{\mathit{funcidx}} \\&&|&
\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{call\_indirect}}~\href{../syntax/modules.html#syntax-tableidx}{\mathit{tableidx}}~\href{../syntax/modules.html#syntax-typeidx}{\mathit{typeidx}} \\
\end{array}\end{split}\]
The \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{nop}}\) instruction does nothing.
The \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{unreachable}}\) instruction causes an unconditional trap.
The \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{block}}\), \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{loop}}\) and \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{if}}\) instructions are structured instructions.
They bracket nested sequences of instructions, called blocks, terminated with, or separated by, \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}\) or \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{else}}\) pseudo-instructions.
As the grammar prescribes, they must be well-nested.
A structured instruction can consume input and produce output on the operand stack according to its annotated block type.
It is given either as a type index that refers to a suitable function type, or as an optional value type inline, which is a shorthand for the function type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-valtype}{\mathit{valtype}}^?]\).
Each structured control instruction introduces an implicit label.
Labels are targets for branch instructions that reference them with label indices.
Unlike with other index spaces, indexing of labels is relative by nesting depth,
that is, label \(0\) refers to the innermost structured control instruction enclosing the referring branch instruction,
while increasing indices refer to those farther out.
Consequently, labels can only be referenced from within the associated structured control instruction.
This also implies that branches can only be directed outwards,
“breaking” from the block of the control construct they target.
The exact effect depends on that control construct.
In case of \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{block}}\) or \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{if}}\) it is a forward jump,
resuming execution after the matching \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}\).
In case of \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{loop}}\) it is a backward jump to the beginning of the loop.
Note
This enforces structured control flow.
Intuitively, a branch targeting a \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{block}}\) or \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{if}}\) behaves like a \(\mathsf{break}\) statement in most C-like languages,
while a branch targeting a \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{loop}}\) behaves like a \(\mathsf{continue}\) statement.
Branch instructions come in several flavors:
\(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br}}\) performs an unconditional branch,
\(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br\_if}}\) performs a conditional branch,
and \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br\_table}}\) performs an indirect branch through an operand indexing into the label vector that is an immediate to the instruction, or to a default target if the operand is out of bounds.
The \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{return}}\) instruction is a shortcut for an unconditional branch to the outermost block, which implicitly is the body of the current function.
Taking a branch unwinds the operand stack up to the height where the targeted structured control instruction was entered.
However, branches may additionally consume operands themselves, which they push back on the operand stack after unwinding.
Forward branches require operands according to the output of the targeted block’s type, i.e., represent the values produced by the terminated block.
Backward branches require operands according to the input of the targeted block’s type, i.e., represent the values consumed by the restarted block.
The \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{call}}\) instruction invokes another function, consuming the necessary arguments from the stack and returning the result values of the call.
The \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{call\_indirect}}\) instruction calls a function indirectly through an operand indexing into a table that is denoted by a table index and must have type \(\href{../syntax/types.html#syntax-reftype}{\mathsf{funcref}}\).
Since it may contain functions of heterogeneous type,
the callee is dynamically checked against the function type indexed by the instruction’s second immediate, and the call is aborted with a trap if it does not match.
Expressions
Function bodies, initialization values for globals, and offsets of element or data segments are given as expressions, which are sequences of instructions terminated by an \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}\) marker.
\[\begin{split}\begin{array}{llll}
\def\mathdef1619#1{{}}\mathdef1619{expression} & \href{../syntax/instructions.html#syntax-expr}{\mathit{expr}} &::=&
\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} \\
\end{array}\end{split}\]
In some places, validation restricts expressions to be constant, which limits the set of allowable instructions.