# Instructions¶

WebAssembly computation is performed by executing individual instructions.

## Numeric Instructions¶

Numeric instructions are defined in terms of the generic numeric operators. The mapping of numeric instructions to their underlying operators is expressed by the following definition:

$\begin{split}\begin{array}{lll@{\qquad}l} \mathit{op}_{\mathsf{i}N}(n_1,\dots,n_k) &=& \mathrm{i}\mathit{op}_N(n_1,\dots,n_k) \\ \mathit{op}_{\mathsf{f}N}(z_1,\dots,z_k) &=& \mathrm{f}\mathit{op}_N(z_1,\dots,z_k) \\ \end{array}\end{split}$

And for conversion operators:

$\begin{split}\begin{array}{lll@{\qquad}l} \mathit{cvtop}^{\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}^?}_{t_1,t_2}(c) &=& \mathit{cvtop}^{\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}^?}_{|t_1|,|t_2|}(c) \\ \end{array}\end{split}$

Where the underlying operators are partial, the corresponding instruction will trap when the result is not defined. Where the underlying operators are non-deterministic, because they may return one of multiple possible NaN values, so are the corresponding instructions.

Note

For example, the result of instruction $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{add}}$$ applied to operands $$i_1, i_2$$ invokes $$\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{add}}_{\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}}(i_1, i_2)$$, which maps to the generic $$\href{../exec/numerics.html#op-iadd}{\mathrm{iadd}}_{32}(i_1, i_2)$$ via the above definition. Similarly, $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i64}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{trunc}}\mathsf{\_}\href{../syntax/types.html#syntax-valtype}{\mathsf{f32}}\mathsf{\_s}$$ applied to $$z$$ invokes $$\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{trunc}}^{\mathsf{s}}_{\href{../syntax/types.html#syntax-valtype}{\mathsf{f32}},\href{../syntax/types.html#syntax-valtype}{\mathsf{i64}}}(z)$$, which maps to the generic $$\href{../exec/numerics.html#op-trunc-s}{\mathrm{trunc}^{\mathsf{s}}}_{32,64}(z)$$.

### $$t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c$$¶

1. Push the value $$t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c$$ to the stack.

Note

No formal reduction rule is required for this instruction, since $$\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}$$ instructions coincide with values.

### $$t\mathsf{.}\href{../syntax/instructions.html#syntax-unop}{\mathit{unop}}$$¶

1. Assert: due to validation, a value of value type $$t$$ is on the top of the stack.

2. Pop the value $$t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_1$$ from the stack.

3. If $$\href{../syntax/instructions.html#syntax-unop}{\mathit{unop}}_t(c_1)$$ is defined, then:

1. Let $$c$$ be a possible result of computing $$\href{../syntax/instructions.html#syntax-unop}{\mathit{unop}}_t(c_1)$$.

2. Push the value $$t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c$$ to the stack.

4. Else:

1. Trap.

$\begin{split}\begin{array}{lcl@{\qquad}l} (t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_1)~t\mathsf{.}\href{../syntax/instructions.html#syntax-unop}{\mathit{unop}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& (t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c) & (\mathrel{\mbox{if}} c \in \href{../syntax/instructions.html#syntax-unop}{\mathit{unop}}_t(c_1)) \\ (t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_1)~t\mathsf{.}\href{../syntax/instructions.html#syntax-unop}{\mathit{unop}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& \href{../exec/runtime.html#syntax-trap}{\mathsf{trap}} & (\mathrel{\mbox{if}} \href{../syntax/instructions.html#syntax-unop}{\mathit{unop}}_{t}(c_1) = \{\}) \end{array}\end{split}$

### $$t\mathsf{.}\href{../syntax/instructions.html#syntax-binop}{\mathit{binop}}$$¶

1. Assert: due to validation, two values of value type $$t$$ are on the top of the stack.

2. Pop the value $$t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_2$$ from the stack.

3. Pop the value $$t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_1$$ from the stack.

4. If $$\href{../syntax/instructions.html#syntax-binop}{\mathit{binop}}_t(c_1, c_2)$$ is defined, then:

1. Let $$c$$ be a possible result of computing $$\href{../syntax/instructions.html#syntax-binop}{\mathit{binop}}_t(c_1, c_2)$$.

2. Push the value $$t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c$$ to the stack.

5. Else:

1. Trap.

$\begin{split}\begin{array}{lcl@{\qquad}l} (t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_1)~(t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_2)~t\mathsf{.}\href{../syntax/instructions.html#syntax-binop}{\mathit{binop}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& (t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c) & (\mathrel{\mbox{if}} c \in \href{../syntax/instructions.html#syntax-binop}{\mathit{binop}}_t(c_1,c_2)) \\ (t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_1)~(t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_2)~t\mathsf{.}\href{../syntax/instructions.html#syntax-binop}{\mathit{binop}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& \href{../exec/runtime.html#syntax-trap}{\mathsf{trap}} & (\mathrel{\mbox{if}} \href{../syntax/instructions.html#syntax-binop}{\mathit{binop}}_{t}(c_1,c2) = \{\}) \end{array}\end{split}$

### $$t\mathsf{.}\href{../syntax/instructions.html#syntax-testop}{\mathit{testop}}$$¶

1. Assert: due to validation, a value of value type $$t$$ is on the top of the stack.

2. Pop the value $$t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_1$$ from the stack.

3. Let $$c$$ be the result of computing $$\href{../syntax/instructions.html#syntax-testop}{\mathit{testop}}_t(c_1)$$.

4. Push the value $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c$$ to the stack.

$\begin{split}\begin{array}{lcl@{\qquad}l} (t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_1)~t\mathsf{.}\href{../syntax/instructions.html#syntax-testop}{\mathit{testop}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c) & (\mathrel{\mbox{if}} c = \href{../syntax/instructions.html#syntax-testop}{\mathit{testop}}_t(c_1)) \\ \end{array}\end{split}$

### $$t\mathsf{.}\href{../syntax/instructions.html#syntax-relop}{\mathit{relop}}$$¶

1. Assert: due to validation, two values of value type $$t$$ are on the top of the stack.

2. Pop the value $$t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_2$$ from the stack.

3. Pop the value $$t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_1$$ from the stack.

4. Let $$c$$ be the result of computing $$\href{../syntax/instructions.html#syntax-relop}{\mathit{relop}}_t(c_1, c_2)$$.

5. Push the value $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c$$ to the stack.

$\begin{split}\begin{array}{lcl@{\qquad}l} (t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_1)~(t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_2)~t\mathsf{.}\href{../syntax/instructions.html#syntax-relop}{\mathit{relop}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c) & (\mathrel{\mbox{if}} c = \href{../syntax/instructions.html#syntax-relop}{\mathit{relop}}_t(c_1,c_2)) \\ \end{array}\end{split}$

### $$t_2\mathsf{.}\href{../syntax/instructions.html#syntax-cvtop}{\mathit{cvtop}}\mathsf{\_}t_1\mathsf{\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}^?$$¶

1. Assert: due to validation, a value of value type $$t_1$$ is on the top of the stack.

2. Pop the value $$t_1.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_1$$ from the stack.

3. If $$\href{../syntax/instructions.html#syntax-cvtop}{\mathit{cvtop}}^{\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}^?}_{t_1,t_2}(c_1)$$ is defined:

1. Let $$c_2$$ be a possible result of computing $$\href{../syntax/instructions.html#syntax-cvtop}{\mathit{cvtop}}^{\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}^?}_{t_1,t_2}(c_1)$$.

2. Push the value $$t_2.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_2$$ to the stack.

4. Else:

1. Trap.

$\begin{split}\begin{array}{lcl@{\qquad}l} (t_1\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_1)~t_2\mathsf{.}\href{../syntax/instructions.html#syntax-cvtop}{\mathit{cvtop}}\mathsf{\_}t_1\mathsf{\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}^? &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& (t_2\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_2) & (\mathrel{\mbox{if}} c_2 \in \href{../syntax/instructions.html#syntax-cvtop}{\mathit{cvtop}}^{\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}^?}_{t_1,t_2}(c_1)) \\ (t_1\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c_1)~t_2\mathsf{.}\href{../syntax/instructions.html#syntax-cvtop}{\mathit{cvtop}}\mathsf{\_}t_1\mathsf{\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}^? &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& \href{../exec/runtime.html#syntax-trap}{\mathsf{trap}} & (\mathrel{\mbox{if}} \href{../syntax/instructions.html#syntax-cvtop}{\mathit{cvtop}}^{\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}^?}_{t_1,t_2}(c_1) = \{\}) \end{array}\end{split}$

## Parametric Instructions¶

### $$\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{drop}}$$¶

1. Assert: due to validation, a value is on the top of the stack.

2. Pop the value $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}$$ from the stack.

$\begin{array}{lcl@{\qquad}l} \href{../exec/runtime.html#syntax-val}{\mathit{val}}~~\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{drop}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& \epsilon \end{array}$

### $$\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}}$$¶

1. Assert: due to validation, a value of value type $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}$$ is on the top of the stack.

2. Pop the value $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c$$ from the stack.

3. Assert: due to validation, two more values (of the same value type) are on the top of the stack.

4. Pop the value $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}_2$$ from the stack.

5. Pop the value $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}_1$$ from the stack.

6. If $$c$$ is not $$0$$, then:

1. Push the value $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}_1$$ back to the stack.

7. Else:

1. Push the value $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}_2$$ back to the stack.

$\begin{split}\begin{array}{lcl@{\qquad}l} \href{../exec/runtime.html#syntax-val}{\mathit{val}}_1~\href{../exec/runtime.html#syntax-val}{\mathit{val}}_2~(\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c)~\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& \href{../exec/runtime.html#syntax-val}{\mathit{val}}_1 & (\mathrel{\mbox{if}} c \neq 0) \\ \href{../exec/runtime.html#syntax-val}{\mathit{val}}_1~\href{../exec/runtime.html#syntax-val}{\mathit{val}}_2~(\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}\mathsf{.}\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c)~\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& \href{../exec/runtime.html#syntax-val}{\mathit{val}}_2 & (\mathrel{\mbox{if}} c = 0) \\ \end{array}\end{split}$

## Variable Instructions¶

### $$\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{local.get}}~x$$¶

1. Let $$F$$ be the current frame.

2. Assert: due to validation, $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{locals}}[x]$$ exists.

3. Let $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}$$ be the value $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{locals}}[x]$$.

4. Push the value $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}$$ to the stack.

$\begin{split}\begin{array}{lcl@{\qquad}l} F; (\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{local.get}}~x) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& F; \href{../exec/runtime.html#syntax-val}{\mathit{val}} & (\mathrel{\mbox{if}} F.\href{../exec/runtime.html#syntax-frame}{\mathsf{locals}}[x] = \href{../exec/runtime.html#syntax-val}{\mathit{val}}) \\ \end{array}\end{split}$

### $$\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{local.set}}~x$$¶

1. Let $$F$$ be the current frame.

2. Assert: due to validation, $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{locals}}[x]$$ exists.

3. Assert: due to validation, a value is on the top of the stack.

4. Pop the value $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}$$ from the stack.

5. Replace $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{locals}}[x]$$ with the value $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}$$.

$\begin{split}\begin{array}{lcl@{\qquad}l} F; \href{../exec/runtime.html#syntax-val}{\mathit{val}}~(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{local.set}}~x) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& F'; \epsilon & (\mathrel{\mbox{if}} F' = F \href{../syntax/conventions.html#notation-replace}{\mathrel{\mbox{with}}} \href{../exec/runtime.html#syntax-frame}{\mathsf{locals}}[x] = \href{../exec/runtime.html#syntax-val}{\mathit{val}}) \\ \end{array}\end{split}$

### $$\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{local.tee}}~x$$¶

1. Assert: due to validation, a value is on the top of the stack.

2. Pop the value $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}$$ from the stack.

3. Push the value $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}$$ to the stack.

4. Push the value $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}$$ to the stack.

5. Execute the instruction $$(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{local.set}}~x)$$.

$\begin{array}{lcl@{\qquad}l} \href{../exec/runtime.html#syntax-val}{\mathit{val}}~(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{local.tee}}~x) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& \href{../exec/runtime.html#syntax-val}{\mathit{val}}~\href{../exec/runtime.html#syntax-val}{\mathit{val}}~(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{local.set}}~x) \end{array}$

### $$\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{global.get}}~x$$¶

1. Let $$F$$ be the current frame.

2. Assert: due to validation, $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{globaladdrs}}[x]$$ exists.

3. Let $$a$$ be the global address $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{globaladdrs}}[x]$$.

4. Assert: due to validation, $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{globals}}[a]$$ exists.

5. Let $$\mathit{glob}$$ be the global instance $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{globals}}[a]$$.

6. Let $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}$$ be the value $$\mathit{glob}.\href{../exec/runtime.html#syntax-globalinst}{\mathsf{value}}$$.

7. Push the value $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}$$ to the stack.

$\begin{split}\begin{array}{l} \begin{array}{lcl@{\qquad}l} S; F; (\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{global.get}}~x) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S; F; \href{../exec/runtime.html#syntax-val}{\mathit{val}} \end{array} \\ \qquad (\mathrel{\mbox{if}} S.\href{../exec/runtime.html#syntax-store}{\mathsf{globals}}[F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{globaladdrs}}[x]].\href{../exec/runtime.html#syntax-globalinst}{\mathsf{value}} = \href{../exec/runtime.html#syntax-val}{\mathit{val}}) \\ \end{array}\end{split}$

### $$\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{global.set}}~x$$¶

1. Let $$F$$ be the current frame.

2. Assert: due to validation, $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{globaladdrs}}[x]$$ exists.

3. Let $$a$$ be the global address $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{globaladdrs}}[x]$$.

4. Assert: due to validation, $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{globals}}[a]$$ exists.

5. Let $$\mathit{glob}$$ be the global instance $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{globals}}[a]$$.

6. Assert: due to validation, a value is on the top of the stack.

7. Pop the value $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}$$ from the stack.

8. Replace $$\mathit{glob}.\href{../exec/runtime.html#syntax-globalinst}{\mathsf{value}}$$ with the value $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}$$.

$\begin{split}\begin{array}{l} \begin{array}{lcl@{\qquad}l} S; F; \href{../exec/runtime.html#syntax-val}{\mathit{val}}~(\href{../syntax/instructions.html#syntax-instr-variable}{\mathsf{global.set}}~x) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S'; F; \epsilon \end{array} \\ \qquad (\mathrel{\mbox{if}} S' = S \href{../syntax/conventions.html#notation-replace}{\mathrel{\mbox{with}}} \href{../exec/runtime.html#syntax-store}{\mathsf{globals}}[F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{globaladdrs}}[x]].\href{../exec/runtime.html#syntax-globalinst}{\mathsf{value}} = \href{../exec/runtime.html#syntax-val}{\mathit{val}}) \\ \end{array}\end{split}$

Note

Validation ensures that the global is, in fact, marked as mutable.

## Memory Instructions¶

Note

The alignment \href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{align}} in load and store instructions does not affect the semantics. It is an indication that the offset $$\mathit{ea}$$ at which the memory is accessed is intended to satisfy the property \mathit{ea} \mathbin{\mathrm{mod}} 2^{\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{align}}} = 0. A WebAssembly implementation can use this hint to optimize for the intended use. Unaligned access violating that property is still allowed and must succeed regardless of the annotation. However, it may be substantially slower on some hardware.

### $$t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{load}}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}$$ and $$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}}$$¶

1. Let $$F$$ be the current frame.

2. Assert: due to validation, $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0]$$ exists.

3. Let $$a$$ be the memory address $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0]$$.

4. Assert: due to validation, $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[a]$$ exists.

5. Let $$\mathit{mem}$$ be the memory instance $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[a]$$.

6. Assert: due to validation, a value of value type $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}$$ is on the top of the stack.

7. Pop the value $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~i$$ from the stack.

8. Let $$\mathit{ea}$$ be the integer $$i + \href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{offset}}$$.

9. If $$N$$ is not part of the instruction, then:

1. Let $$N$$ be the bit width $$|t|$$ of value type $$t$$.

10. If $$\mathit{ea} + N/8$$ is larger than the length of $$\mathit{mem}.\href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}$$, then:

1. Trap.

11. Let $$b^\ast$$ be the byte sequence $$\mathit{mem}.\href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}[\mathit{ea} \href{../syntax/conventions.html#notation-slice}{\mathrel{\mathbf{:}}} N/8]$$.

12. If $$N$$ and $$\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}$$ are part of the instruction, then:

1. Let $$n$$ be the integer for which $$\href{../exec/numerics.html#aux-bytes}{\mathrm{bytes}}_{\href{../syntax/values.html#syntax-int}{\mathit{i}N}}(n) = b^\ast$$.

2. Let $$c$$ be the result of computing $$\href{../exec/numerics.html#op-extend-u}{\mathrm{extend}}\mathrm{\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}_{N,|t|}(n)$$.

13. Else:

1. Let $$c$$ be the constant for which $$\href{../exec/numerics.html#aux-bytes}{\mathrm{bytes}}_t(c) = b^\ast$$.

14. Push the value $$t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c$$ to the stack.

$\begin{split}~\\[-1ex] \begin{array}{l} \begin{array}{lcl@{\qquad}l} S; F; (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~i)~(t.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{load}}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S; F; (t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c) \end{array} \\ \qquad \begin{array}[t]{@{}r@{~}l@{}} (\mathrel{\mbox{if}} & \mathit{ea} = i + \href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{offset}} \\ \wedge & \mathit{ea} + |t|/8 \leq |S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0]].\href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}| \\ \wedge & \href{../exec/numerics.html#aux-bytes}{\mathrm{bytes}}_t(c) = S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0]].\href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}[\mathit{ea} \href{../syntax/conventions.html#notation-slice}{\mathrel{\mathbf{:}}} |t|/8]) \end{array} \\[1ex] \begin{array}{lcl@{\qquad}l} S; F; (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~i)~(t.\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}}) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S; F; (t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~\href{../exec/numerics.html#op-extend-u}{\mathrm{extend}}\mathrm{\_}\href{../syntax/instructions.html#syntax-sx}{\mathit{sx}}_{N,|t|}(n)) \end{array} \\ \qquad \begin{array}[t]{@{}r@{~}l@{}} (\mathrel{\mbox{if}} & \mathit{ea} = i + \href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{offset}} \\ \wedge & \mathit{ea} + N/8 \leq |S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0]].\href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}| \\ \wedge & \href{../exec/numerics.html#aux-bytes}{\mathrm{bytes}}_{\href{../syntax/values.html#syntax-int}{\mathit{i}N}}(n) = S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0]].\href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}[\mathit{ea} \href{../syntax/conventions.html#notation-slice}{\mathrel{\mathbf{:}}} N/8]) \end{array} \\[1ex] \begin{array}{lcl@{\qquad}l} S; F; (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~k)~(t.\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}}) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S; F; \href{../exec/runtime.html#syntax-trap}{\mathsf{trap}} \end{array} \\ \qquad (\mathrel{\mbox{otherwise}}) \\ \end{array}\end{split}$

### $$t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{store}}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}$$ and $$t\mathsf{.}\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{store}}{N}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}$$¶

1. Let $$F$$ be the current frame.

2. Assert: due to validation, $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0]$$ exists.

3. Let $$a$$ be the memory address $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0]$$.

4. Assert: due to validation, $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[a]$$ exists.

5. Let $$\mathit{mem}$$ be the memory instance $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[a]$$.

6. Assert: due to validation, a value of value type $$t$$ is on the top of the stack.

7. Pop the value $$t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c$$ from the stack.

8. Assert: due to validation, a value of value type $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}$$ is on the top of the stack.

9. Pop the value $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~i$$ from the stack.

10. Let $$\mathit{ea}$$ be the integer $$i + \href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{offset}}$$.

11. If $$N$$ is not part of the instruction, then:

1. Let $$N$$ be the bit width $$|t|$$ of value type $$t$$.

12. If $$\mathit{ea} + N/8$$ is larger than the length of $$\mathit{mem}.\href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}$$, then:

1. Trap.

13. If $$N$$ is part of the instruction, then:

1. Let $$n$$ be the result of computing $$\href{../exec/numerics.html#op-wrap}{\mathrm{wrap}}_{|t|,N}(c)$$.

2. Let $$b^\ast$$ be the byte sequence $$\href{../exec/numerics.html#aux-bytes}{\mathrm{bytes}}_{\href{../syntax/values.html#syntax-int}{\mathit{i}N}}(n)$$.

14. Else:

1. Let $$b^\ast$$ be the byte sequence $$\href{../exec/numerics.html#aux-bytes}{\mathrm{bytes}}_t(c)$$.

15. Replace the bytes $$\mathit{mem}.\href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}[\mathit{ea} \href{../syntax/conventions.html#notation-slice}{\mathrel{\mathbf{:}}} N/8]$$ with $$b^\ast$$.

$\begin{split}~\\[-1ex] \begin{array}{l} \begin{array}{lcl@{\qquad}l} S; F; (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~i)~(t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c)~(t.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{store}}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S'; F; \epsilon \end{array} \\ \qquad \begin{array}[t]{@{}r@{~}l@{}} (\mathrel{\mbox{if}} & \mathit{ea} = i + \href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{offset}} \\ \wedge & \mathit{ea} + |t|/8 \leq |S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0]].\href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}| \\ \wedge & S' = S \href{../syntax/conventions.html#notation-replace}{\mathrel{\mbox{with}}} \href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0]].\href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}[\mathit{ea} \href{../syntax/conventions.html#notation-slice}{\mathrel{\mathbf{:}}} |t|/8] = \href{../exec/numerics.html#aux-bytes}{\mathrm{bytes}}_t(c) \end{array} \\[1ex] \begin{array}{lcl@{\qquad}l} S; F; (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~i)~(t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c)~(t.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{store}}{N}~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S'; F; \epsilon \end{array} \\ \qquad \begin{array}[t]{@{}r@{~}l@{}} (\mathrel{\mbox{if}} & \mathit{ea} = i + \href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{offset}} \\ \wedge & \mathit{ea} + N/8 \leq |S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0]].\href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}| \\ \wedge & S' = S \href{../syntax/conventions.html#notation-replace}{\mathrel{\mbox{with}}} \href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0]].\href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}[\mathit{ea} \href{../syntax/conventions.html#notation-slice}{\mathrel{\mathbf{:}}} N/8] = \href{../exec/numerics.html#aux-bytes}{\mathrm{bytes}}_{\href{../syntax/values.html#syntax-int}{\mathit{i}N}}(\href{../exec/numerics.html#op-wrap}{\mathrm{wrap}}_{|t|,N}(c)) \end{array} \\[1ex] \begin{array}{lcl@{\qquad}l} S; F; (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~k)~(t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c)~(t.\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{store}}{N}^?~\href{../syntax/instructions.html#syntax-memarg}{\mathit{memarg}}) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S; F; \href{../exec/runtime.html#syntax-trap}{\mathsf{trap}} \end{array} \\ \qquad (\mathrel{\mbox{otherwise}}) \\ \end{array}\end{split}$

### $$\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.size}}$$¶

1. Let $$F$$ be the current frame.

2. Assert: due to validation, $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0]$$ exists.

3. Let $$a$$ be the memory address $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0]$$.

4. Assert: due to validation, $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[a]$$ exists.

5. Let $$\mathit{mem}$$ be the memory instance $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[a]$$.

6. Let $$\mathit{sz}$$ be the length of $$\mathit{mem}.\href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}$$ divided by the page size.

7. Push the value $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~\mathit{sz}$$ to the stack.

$\begin{split}\begin{array}{l} \begin{array}{lcl@{\qquad}l} S; F; \href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.size}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S; F; (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~\mathit{sz}) \end{array} \\ \qquad (\mathrel{\mbox{if}} |S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0]].\href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}| = \mathit{sz}\cdot64\,\mathrm{Ki}) \\ \end{array}\end{split}$

### $$\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.grow}}$$¶

1. Let $$F$$ be the current frame.

2. Assert: due to validation, $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0]$$ exists.

3. Let $$a$$ be the memory address $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0]$$.

4. Assert: due to validation, $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[a]$$ exists.

5. Let $$\mathit{mem}$$ be the memory instance $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[a]$$.

6. Let $$\mathit{sz}$$ be the length of $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[a]$$ divided by the page size.

7. Assert: due to validation, a value of value type $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}$$ is on the top of the stack.

8. Pop the value $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~n$$ from the stack.

9. Let $$\mathit{err}$$ be the $$\href{../syntax/values.html#syntax-int}{\mathit{i32}}$$ value $$2^{32}-1$$, for which $$\href{../exec/numerics.html#aux-signed}{\mathrm{signed}}_{32}(\mathit{err})$$ is $$-1$$.

10. Either, try growing $$\mathit{mem}$$ by $$n$$ pages:

1. If it succeeds, push the value $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~\mathit{sz}$$ to the stack.

2. Else, push the value $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~\mathit{err}$$ to the stack.

1. Or, push the value $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~\mathit{err}$$ to the stack.

$\begin{split}~\\[-1ex] \begin{array}{l} \begin{array}{lcl@{\qquad}l} S; F; (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~n)~\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.grow}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S'; F; (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~\mathit{sz}) \end{array} \\ \qquad \begin{array}[t]{@{}r@{~}l@{}} (\mathrel{\mbox{if}} & F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}[0] = a \\ \wedge & \mathit{sz} = |S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[a].\href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}|/64\,\mathrm{Ki} \\ \wedge & S' = S \href{../syntax/conventions.html#notation-replace}{\mathrel{\mbox{with}}} \href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[a] = \href{../exec/modules.html#grow-mem}{\mathrm{growmem}}(S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}[a], n)) \\ \end{array} \\[1ex] \begin{array}{lcl@{\qquad}l} S; F; (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~n)~\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.grow}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S; F; (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~\href{../exec/numerics.html#aux-signed}{\mathrm{signed}}_{32}^{-1}(-1)) \end{array} \end{array}\end{split}$

Note

The $$\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory.grow}}$$ instruction is non-deterministic. It may either succeed, returning the old memory size $$\mathit{sz}$$, or fail, returning $${-1}$$. Failure must occur if the referenced memory instance has a maximum size defined that would be exceeded. However, failure can occur in other cases as well. In practice, the choice depends on the resources available to the embedder.

## Control Instructions¶

### $$\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{nop}}$$¶

1. Do nothing.

$\begin{array}{lcl@{\qquad}l} \href{../syntax/instructions.html#syntax-instr-control}{\mathsf{nop}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& \epsilon \end{array}$

### $$\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{unreachable}}$$¶

1. Trap.

$\begin{array}{lcl@{\qquad}l} \href{../syntax/instructions.html#syntax-instr-control}{\mathsf{unreachable}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& \href{../exec/runtime.html#syntax-trap}{\mathsf{trap}} \end{array}$

### $$\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}}$$¶

1. Assert: due to validation, $$\href{../exec/runtime.html#syntax-frame}{\mathrm{expand}}_F(\href{../syntax/instructions.html#syntax-blocktype}{\mathit{blocktype}})$$ is defined.

2. Let $$[t_1^m] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^n]$$ be the function type $$\href{../exec/runtime.html#syntax-frame}{\mathrm{expand}}_F(\href{../syntax/instructions.html#syntax-blocktype}{\mathit{blocktype}})$$.

3. Let $$L$$ be the label whose arity is $$n$$ and whose continuation is the end of the block.

4. Assert: due to validation, there are at least $$m$$ values on the top of the stack.

5. Pop the values $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^m$$ from the stack.

6. Enter the block $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^m~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast$$ with label $$L$$.

$\begin{split}~\\[-1ex] \begin{array}{lcl@{\qquad}l} F; \href{../exec/runtime.html#syntax-val}{\mathit{val}}^m~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{block}}~\mathit{bt}~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& F; \href{../exec/runtime.html#syntax-label}{\mathsf{label}}_n\{\epsilon\}~\href{../exec/runtime.html#syntax-val}{\mathit{val}}^m~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} & (\mathrel{\mbox{if}} \href{../exec/runtime.html#syntax-frame}{\mathrm{expand}}_F(\mathit{bt}) = [t_1^m] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^n]) \end{array}\end{split}$

### $$\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}}$$¶

1. Assert: due to validation, $$\href{../exec/runtime.html#syntax-frame}{\mathrm{expand}}_F(\href{../syntax/instructions.html#syntax-blocktype}{\mathit{blocktype}})$$ is defined.

2. Let $$[t_1^m] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^n]$$ be the function type $$\href{../exec/runtime.html#syntax-frame}{\mathrm{expand}}_F(\href{../syntax/instructions.html#syntax-blocktype}{\mathit{blocktype}})$$.

3. Let $$L$$ be the label whose arity is $$m$$ and whose continuation is the start of the loop.

4. Assert: due to validation, there are at least $$m$$ values on the top of the stack.

5. Pop the values $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^m$$ from the stack.

6. Enter the block $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^m~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast$$ with label $$L$$.

$\begin{split}~\\[-1ex] \begin{array}{lcl@{\qquad}l} F; \href{../exec/runtime.html#syntax-val}{\mathit{val}}^m~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{loop}}~\mathit{bt}~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& F; \href{../exec/runtime.html#syntax-label}{\mathsf{label}}_m\{\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{loop}}~\mathit{bt}~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}\}~\href{../exec/runtime.html#syntax-val}{\mathit{val}}^m~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} & (\mathrel{\mbox{if}} \href{../exec/runtime.html#syntax-frame}{\mathrm{expand}}_F(\mathit{bt}) = [t_1^m] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^n]) \end{array}\end{split}$

### $$\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{if}}~\href{../syntax/instructions.html#syntax-blocktype}{\mathit{blocktype}}~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_1^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{else}}~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_2^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}$$¶

1. Assert: due to validation, $$\href{../exec/runtime.html#syntax-frame}{\mathrm{expand}}_F(\href{../syntax/instructions.html#syntax-blocktype}{\mathit{blocktype}})$$ is defined.

2. Let $$[t_1^m] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^n]$$ be the function type $$\href{../exec/runtime.html#syntax-frame}{\mathrm{expand}}_F(\href{../syntax/instructions.html#syntax-blocktype}{\mathit{blocktype}})$$.

3. Let $$L$$ be the label whose arity is $$n$$ and whose continuation is the end of the $$\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{if}}$$ instruction.

4. Assert: due to validation, a value of value type $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}$$ is on the top of the stack.

5. Pop the value $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c$$ from the stack.

6. Assert: due to validation, there are at least $$m$$ values on the top of the stack.

7. Pop the values $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^m$$ from the stack.

8. If $$c$$ is non-zero, then:

1. Enter the block $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^m~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_1^\ast$$ with label $$L$$.

9. Else:

1. Enter the block $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^m~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_2^\ast$$ with label $$L$$.

$\begin{split}~\\[-1ex] \begin{array}{lcl@{\qquad}l} F; \href{../exec/runtime.html#syntax-val}{\mathit{val}}^m~(\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c)~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{if}}~\mathit{bt}~\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{../exec/conventions.html#formal-notation}{\hookrightarrow}& F; \href{../exec/runtime.html#syntax-label}{\mathsf{label}}_n\{\epsilon\}~\href{../exec/runtime.html#syntax-val}{\mathit{val}}^m~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_1^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} & (\mathrel{\mbox{if}} c \neq 0 \wedge \href{../exec/runtime.html#syntax-frame}{\mathrm{expand}}_F(\mathit{bt}) = [t_1^m] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^n]) \\ F; \href{../exec/runtime.html#syntax-val}{\mathit{val}}^m~(\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c)~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{if}}~\mathit{bt}~\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{../exec/conventions.html#formal-notation}{\hookrightarrow}& F; \href{../exec/runtime.html#syntax-label}{\mathsf{label}}_n\{\epsilon\}~\href{../exec/runtime.html#syntax-val}{\mathit{val}}^m~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_2^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} & (\mathrel{\mbox{if}} c = 0 \wedge \href{../exec/runtime.html#syntax-frame}{\mathrm{expand}}_F(\mathit{bt}) = [t_1^m] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^n]) \\ \end{array}\end{split}$

### $$\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br}}~l$$¶

1. Assert: due to validation, the stack contains at least $$l+1$$ labels.

2. Let $$L$$ be the $$l$$-th label appearing on the stack, starting from the top and counting from zero.

3. Let $$n$$ be the arity of $$L$$.

4. Assert: due to validation, there are at least $$n$$ values on the top of the stack.

5. Pop the values $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^n$$ from the stack.

6. Repeat $$l+1$$ times:

1. While the top of the stack is a value, do:

1. Pop the value from the stack.

2. Assert: due to validation, the top of the stack now is a label.

3. Pop the label from the stack.

7. Push the values $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^n$$ to the stack.

8. Jump to the continuation of $$L$$.

$\begin{split}~\\[-1ex] \begin{array}{lcl@{\qquad}l} \href{../exec/runtime.html#syntax-label}{\mathsf{label}}_n\{\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\}~\href{../exec/runtime.html#syntax-ctxt-block}{B}^l[\href{../exec/runtime.html#syntax-val}{\mathit{val}}^n~(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br}}~l)]~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& \href{../exec/runtime.html#syntax-val}{\mathit{val}}^n~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast \end{array}\end{split}$

### $$\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br\_if}}~l$$¶

1. Assert: due to validation, a value of value type $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}$$ is on the top of the stack.

2. Pop the value $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c$$ from the stack.

3. If $$c$$ is non-zero, then:

1. Execute the instruction $$(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br}}~l)$$.

4. Else:

1. Do nothing.

$\begin{split}~\\[-1ex] \begin{array}{lcl@{\qquad}l} (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c)~(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br\_if}}~l) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& (\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br}}~l) & (\mathrel{\mbox{if}} c \neq 0) \\ (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~c)~(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br\_if}}~l) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& \epsilon & (\mathrel{\mbox{if}} c = 0) \\ \end{array}\end{split}$

### $$\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br\_table}}~l^\ast~l_N$$¶

1. Assert: due to validation, a value of value type $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}$$ is on the top of the stack.

2. Pop the value $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~i$$ from the stack.

3. If $$i$$ is smaller than the length of $$l^\ast$$, then:

1. Let $$l_i$$ be the label $$l^\ast[i]$$.

2. Execute the instruction $$(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br}}~l_i)$$.

4. Else:

1. Execute the instruction $$(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br}}~l_N)$$.

$\begin{split}~\\[-1ex] \begin{array}{lcl@{\qquad}l} (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~i)~(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br\_table}}~l^\ast~l_N) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& (\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br}}~l_i) & (\mathrel{\mbox{if}} l^\ast[i] = l_i) \\ (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~i)~(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br\_table}}~l^\ast~l_N) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& (\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{br}}~l_N) & (\mathrel{\mbox{if}} |l^\ast| \leq i) \\ \end{array}\end{split}$

### $$\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{return}}$$¶

1. Let $$F$$ be the current frame.

2. Let $$n$$ be the arity of $$F$$.

3. Assert: due to validation, there are at least $$n$$ values on the top of the stack.

4. Pop the results $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^n$$ from the stack.

5. Assert: due to validation, the stack contains at least one frame.

6. While the top of the stack is not a frame, do:

1. Pop the top element from the stack.

7. Assert: the top of the stack is the frame $$F$$.

8. Pop the frame from the stack.

9. Push $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^n$$ to the stack.

10. Jump to the instruction after the original call that pushed the frame.

$\begin{split}~\\[-1ex] \begin{array}{lcl@{\qquad}l} \href{../exec/runtime.html#syntax-frame}{\mathsf{frame}}_n\{F\}~\href{../exec/runtime.html#syntax-ctxt-block}{B}^k[\href{../exec/runtime.html#syntax-val}{\mathit{val}}^n~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{return}}]~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& \href{../exec/runtime.html#syntax-val}{\mathit{val}}^n \end{array}\end{split}$

### $$\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{call}}~x$$¶

1. Let $$F$$ be the current frame.

2. Assert: due to validation, $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{funcaddrs}}[x]$$ exists.

3. Let $$a$$ be the function address $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{funcaddrs}}[x]$$.

4. Invoke the function instance at address $$a$$.

$\begin{array}{lcl@{\qquad}l} F; (\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{call}}~x) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& F; (\href{../exec/runtime.html#syntax-invoke}{\mathsf{invoke}}~a) & (\mathrel{\mbox{if}} F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{funcaddrs}}[x] = a) \end{array}$

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

1. Let $$F$$ be the current frame.

2. Assert: due to validation, $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{tableaddrs}}[0]$$ exists.

3. Let $$\mathit{ta}$$ be the table address $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{tableaddrs}}[0]$$.

4. Assert: due to validation, $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{tables}}[\mathit{ta}]$$ exists.

5. Let $$\mathit{tab}$$ be the table instance $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{tables}}[\mathit{ta}]$$.

6. Assert: due to validation, $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{types}}[x]$$ exists.

7. Let $$\mathit{ft}_{\mathrm{expect}}$$ be the function type $$F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{types}}[x]$$.

8. Assert: due to validation, a value with value type $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}$$ is on the top of the stack.

9. Pop the value $$\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~i$$ from the stack.

10. If $$i$$ is not smaller than the length of $$\mathit{tab}.\href{../exec/runtime.html#syntax-tableinst}{\mathsf{elem}}$$, then:

1. Trap.

11. If $$\mathit{tab}.\href{../exec/runtime.html#syntax-tableinst}{\mathsf{elem}}[i]$$ is uninitialized, then:

1. Trap.

12. Let $$a$$ be the function address $$\mathit{tab}.\href{../exec/runtime.html#syntax-tableinst}{\mathsf{elem}}[i]$$.

13. Assert: due to validation, $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{funcs}}[a]$$ exists.

14. Let $$\mathit{f}$$ be the function instance $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{funcs}}[a]$$.

15. Let $$\mathit{ft}_{\mathrm{actual}}$$ be the function type $$\mathit{f}.\href{../exec/runtime.html#syntax-funcinst}{\mathsf{type}}$$.

16. If $$\mathit{ft}_{\mathrm{actual}}$$ and $$\mathit{ft}_{\mathrm{expect}}$$ differ, then:

1. Trap.

17. Invoke the function instance at address $$a$$.

$\begin{split}~\\[-1ex] \begin{array}{l} \begin{array}{lcl@{\qquad}l} S; F; (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~i)~(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{call\_indirect}}~x) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S; F; (\href{../exec/runtime.html#syntax-invoke}{\mathsf{invoke}}~a) \end{array} \\ \qquad \begin{array}[t]{@{}r@{~}l@{}} (\mathrel{\mbox{if}} & S.\href{../exec/runtime.html#syntax-store}{\mathsf{tables}}[F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{tableaddrs}}[0]].\href{../exec/runtime.html#syntax-tableinst}{\mathsf{elem}}[i] = a \\ \wedge & S.\href{../exec/runtime.html#syntax-store}{\mathsf{funcs}}[a] = f \\ \wedge & F.\href{../exec/runtime.html#syntax-frame}{\mathsf{module}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{types}}[x] = f.\href{../exec/runtime.html#syntax-funcinst}{\mathsf{type}}) \end{array} \\[1ex] \begin{array}{lcl@{\qquad}l} S; F; (\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~i)~(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{call\_indirect}}~x) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S; F; \href{../exec/runtime.html#syntax-trap}{\mathsf{trap}} \end{array} \\ \qquad (\mathrel{\mbox{otherwise}}) \end{array}\end{split}$

## Blocks¶

The following auxiliary rules define the semantics of executing an instruction sequence that forms a block.

### Entering $$\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast$$ with label $$L$$¶

1. Push $$L$$ to the stack.

2. Jump to the start of the instruction sequence $$\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast$$.

Note

No formal reduction rule is needed for entering an instruction sequence, because the label $$L$$ is embedded in the administrative instruction that structured control instructions reduce to directly.

### Exiting $$\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast$$ with label $$L$$¶

When the end of a block is reached without a jump or trap aborting it, then the following steps are performed.

1. Let $$m$$ be the number of values on the top of the stack.

2. Pop the values $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^m$$ from the stack.

3. Assert: due to validation, the label $$L$$ is now on the top of the stack.

4. Pop the label from the stack.

5. Push $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^m$$ back to the stack.

6. Jump to the position after the $$\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}$$ of the structured control instruction associated with the label $$L$$.

$\begin{split}~\\[-1ex] \begin{array}{lcl@{\qquad}l} \href{../exec/runtime.html#syntax-label}{\mathsf{label}}_n\{\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\}~\href{../exec/runtime.html#syntax-val}{\mathit{val}}^m~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& \href{../exec/runtime.html#syntax-val}{\mathit{val}}^m \end{array}\end{split}$

Note

This semantics also applies to the instruction sequence contained in a $$\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{loop}}$$ instruction. Therefore, execution of a loop falls off the end, unless a backwards branch is performed explicitly.

## Function Calls¶

The following auxiliary rules define the semantics of invoking a function instance through one of the call instructions and returning from it.

### Invocation of function address$$a$$¶

1. Assert: due to validation, $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{funcs}}[a]$$ exists.

2. Let $$f$$ be the function instance, $$S.\href{../exec/runtime.html#syntax-store}{\mathsf{funcs}}[a]$$.

3. Let $$[t_1^n] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^m]$$ be the function type $$f.\href{../exec/runtime.html#syntax-funcinst}{\mathsf{type}}$$.

4. Let $$t^\ast$$ be the list of value types $$f.\href{../exec/runtime.html#syntax-funcinst}{\mathsf{code}}.\href{../syntax/modules.html#syntax-func}{\mathsf{locals}}$$.

5. Let $$\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}$$ be the expression $$f.\href{../exec/runtime.html#syntax-funcinst}{\mathsf{code}}.\href{../syntax/modules.html#syntax-func}{\mathsf{body}}$$.

6. Assert: due to validation, $$n$$ values are on the top of the stack.

7. Pop the values $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^n$$ from the stack.

8. Let $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}_0^\ast$$ be the list of zero values of types $$t^\ast$$.

9. Let $$F$$ be the frame $$\{ \href{../exec/runtime.html#syntax-frame}{\mathsf{module}}~f.\href{../exec/runtime.html#syntax-funcinst}{\mathsf{module}}, \href{../exec/runtime.html#syntax-frame}{\mathsf{locals}}~\href{../exec/runtime.html#syntax-val}{\mathit{val}}^n~\href{../exec/runtime.html#syntax-val}{\mathit{val}}_0^\ast \}$$.

10. Push the activation of $$F$$ with arity $$m$$ to the stack.

11. Let $$L$$ be the label whose arity is $$m$$ and whose continuation is the end of the function.

12. Enter the instruction sequence $$\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast$$ with label $$L$$.

$\begin{split}~\\[-1ex] \begin{array}{l} \begin{array}{lcl@{\qquad}l} S; \href{../exec/runtime.html#syntax-val}{\mathit{val}}^n~(\href{../exec/runtime.html#syntax-invoke}{\mathsf{invoke}}~a) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S; \href{../exec/runtime.html#syntax-frame}{\mathsf{frame}}_m\{F\}~\href{../exec/runtime.html#syntax-label}{\mathsf{label}}_m\{\}~\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{end}} \end{array} \\ \qquad \begin{array}[t]{@{}r@{~}l@{}} (\mathrel{\mbox{if}} & S.\href{../exec/runtime.html#syntax-store}{\mathsf{funcs}}[a] = f \\ \wedge & f.\href{../exec/runtime.html#syntax-funcinst}{\mathsf{type}} = [t_1^n] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^m] \\ \wedge & f.\href{../exec/runtime.html#syntax-funcinst}{\mathsf{code}} = \{ \href{../syntax/modules.html#syntax-func}{\mathsf{type}}~x, \href{../syntax/modules.html#syntax-func}{\mathsf{locals}}~t^k, \href{../syntax/modules.html#syntax-func}{\mathsf{body}}~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} \} \\ \wedge & F = \{ \href{../exec/runtime.html#syntax-frame}{\mathsf{module}}~f.\href{../exec/runtime.html#syntax-funcinst}{\mathsf{module}}, ~\href{../exec/runtime.html#syntax-frame}{\mathsf{locals}}~\href{../exec/runtime.html#syntax-val}{\mathit{val}}^n~(t.\href{../syntax/instructions.html#syntax-instr-numeric}{\mathsf{const}}~0)^k \}) \end{array} \\ \end{array}\end{split}$

### Returning from a function¶

When the end of a function is reached without a jump (i.e., $$\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{return}}$$) or trap aborting it, then the following steps are performed.

1. Let $$F$$ be the current frame.

2. Let $$n$$ be the arity of the activation of $$F$$.

3. Assert: due to validation, there are $$n$$ values on the top of the stack.

4. Pop the results $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^n$$ from the stack.

5. Assert: due to validation, the frame $$F$$ is now on the top of the stack.

6. Pop the frame from the stack.

7. Push $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^n$$ back to the stack.

$\begin{split}~\\[-1ex] \begin{array}{lcl@{\qquad}l} \href{../exec/runtime.html#syntax-frame}{\mathsf{frame}}_n\{F\}~\href{../exec/runtime.html#syntax-val}{\mathit{val}}^n~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& \href{../exec/runtime.html#syntax-val}{\mathit{val}}^n \end{array}\end{split}$

### Host Functions¶

Invoking a host function has non-deterministic behavior. It may either terminate with a trap or return regularly. However, in the latter case, it must consume and produce the right number and types of WebAssembly values on the stack, according to its function type.

A host function may also modify the store. However, all store modifications must result in an extension of the original store, i.e., they must only modify mutable contents and must not have instances removed. Furthermore, the resulting store must be valid, i.e., all data and code in it is well-typed.

$\begin{split}~\\[-1ex] \begin{array}{l} \begin{array}{lcl@{\qquad}l} S; \href{../exec/runtime.html#syntax-val}{\mathit{val}}^n~(\href{../exec/runtime.html#syntax-invoke}{\mathsf{invoke}}~a) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S'; \href{../exec/runtime.html#syntax-result}{\mathit{result}} \end{array} \\ \qquad \begin{array}[t]{@{}r@{~}l@{}} (\mathrel{\mbox{if}} & S.\href{../exec/runtime.html#syntax-store}{\mathsf{funcs}}[a] = \{ \href{../exec/runtime.html#syntax-funcinst}{\mathsf{type}}~[t_1^n] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^m], \href{../exec/runtime.html#syntax-funcinst}{\mathsf{hostcode}}~\mathit{hf} \} \\ \wedge & (S'; \href{../exec/runtime.html#syntax-result}{\mathit{result}}) \in \mathit{hf}(S; \href{../exec/runtime.html#syntax-val}{\mathit{val}}^n)) \\ \end{array} \\ \begin{array}{lcl@{\qquad}l} S; \href{../exec/runtime.html#syntax-val}{\mathit{val}}^n~(\href{../exec/runtime.html#syntax-invoke}{\mathsf{invoke}}~a) &\href{../exec/conventions.html#formal-notation}{\hookrightarrow}& S; \href{../exec/runtime.html#syntax-val}{\mathit{val}}^n~(\href{../exec/runtime.html#syntax-invoke}{\mathsf{invoke}}~a) \end{array} \\ \qquad \begin{array}[t]{@{}r@{~}l@{}} (\mathrel{\mbox{if}} & S.\href{../exec/runtime.html#syntax-store}{\mathsf{funcs}}[a] = \{ \href{../exec/runtime.html#syntax-funcinst}{\mathsf{type}}~[t_1^n] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^m], \href{../exec/runtime.html#syntax-funcinst}{\mathsf{hostcode}}~\mathit{hf} \} \\ \wedge & \bot \in \mathit{hf}(S; \href{../exec/runtime.html#syntax-val}{\mathit{val}}^n)) \\ \end{array} \\ \end{array}\end{split}$

Here, $$\mathit{hf}(S; \href{../exec/runtime.html#syntax-val}{\mathit{val}}^n)$$ denotes the implementation-defined execution of host function $$\mathit{hf}$$ in current store $$S$$ with arguments $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^n$$. It yields a set of possible outcomes, where each element is either a pair of a modified store $$S'$$ and a result or the special value $$\bot$$ indicating divergence. A host function is non-deterministic if there is at least one argument for which the set of outcomes is not singular.

For a WebAssembly implementation to be sound in the presence of host functions, every host function instance must be valid, which means that it adheres to suitable pre- and post-conditions: under a valid store $$S$$, and given arguments $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}^n$$ matching the ascribed parameter types $$t_1^n$$, executing the host function must yield a non-empty set of possible outcomes each of which is either divergence or consists of a valid store $$S'$$ that is an extension of $$S$$ and a result matching the ascribed return types $$t_2^m$$. All these notions are made precise in the Appendix.

Note

A host function can call back into WebAssembly by invoking a function exported from a module. However, the effects of any such call are subsumed by the non-deterministic behavior allowed for the host function.

## Expressions¶

An expression is evaluated relative to a current frame pointing to its containing module instance.

1. Jump to the start of the instruction sequence $$\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast$$ of the expression.

2. Execute the instruction sequence.

3. Assert: due to validation, the top of the stack contains a value.

4. Pop the value $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}$$ from the stack.

The value $$\href{../exec/runtime.html#syntax-val}{\mathit{val}}$$ is the result of the evaluation.

$S; F; \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast \href{../exec/conventions.html#formal-notation}{\hookrightarrow} S'; F'; \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}'^\ast \qquad (\mathrel{\mbox{if}} S; F; \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}} \href{../exec/conventions.html#formal-notation}{\hookrightarrow} S'; F'; \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}'^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}})$

Note

Evaluation iterates this reduction rule until reaching a value. Expressions constituting function bodies are executed during function invocation.