Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

impl

std › wasm › impl

WebAssembly instructions: a representative subset of control, variable and i32 instructions, each emitted as its binary encoding.

from std.wasm.impl import *

local_get(0)
i32_const(-1)
i32_add
end
20 00 41 7f 6a 0b

WebAssembly’s dotted names aren’t identifiers here, so i32.const is i32_const and local.get is local_get. Names that are keywords in other languages end in _: if_, else_, return_. std.wasm.module builds the module around them.

Macros

unreachable

Traps immediately.

SyntaxParametersResultDescription
unreachableemits Byte

nop

Does nothing.

SyntaxParametersResultDescription
nopemits Byte

block

Starts a block. br to it jumps to its end. blocktype is EMPTY_BLOCKTYPE or the type of the value it produces.

SyntaxParametersResultDescription
block blocktypeblocktype: Byteemits OpWithImm<1>

loop

Starts a loop. br to it jumps back to its start.

SyntaxParametersResultDescription
loop blocktypeblocktype: Byteemits OpWithImm<1>

if_

Starts an if: runs what follows if the i32 it pops isn’t zero, and the else_ part (if any) otherwise.

SyntaxParametersResultDescription
if_ blocktypeblocktype: Byteemits OpWithImm<1>

else_

Starts the else part of an if_.

SyntaxParametersResultDescription
else_emits Byte

end

Ends a block, loop, if_ or function body.

SyntaxParametersResultDescription
endemits Byte

br

Branches to the enclosing block depth levels out: br(0) is the innermost.

SyntaxParametersResultDescription
br depthdepth: intemits OpWithImm<...>

br_if

Pops an i32, and does br(depth) if it isn’t zero.

SyntaxParametersResultDescription
br_if depthdepth: intemits OpWithImm<...>

return_

Returns from the current function.

SyntaxParametersResultDescription
return_emits Byte

call

Calls function number func_index.

SyntaxParametersResultDescription
call func_indexfunc_index: intemits OpWithImm<...>

drop

Pops a value and discards it.

SyntaxParametersResultDescription
dropemits Byte

local_get

Pushes local variable number index.

SyntaxParametersResultDescription
local_get indexindex: intemits OpWithImm<...>

local_set

Pops a value into local variable number index.

SyntaxParametersResultDescription
local_set indexindex: intemits OpWithImm<...>

local_tee

Stores the top of the stack in local variable number index, without popping it.

SyntaxParametersResultDescription
local_tee indexindex: intemits OpWithImm<...>

i32_load

Pops an address and pushes the i32 at address + offset. align is the alignment the address is promised to have, as a power of two: 2, a 4-byte boundary, by default.

SyntaxParametersResultDescription
i32_load offset, alignoffset: int = 0, align: int = 2emits MemOp<...>

i32_store

Pops an i32 value, then an address, and stores the value at address + offset. align means what it does for i32_load.

SyntaxParametersResultDescription
i32_store offset, alignoffset: int = 0, align: int = 2emits MemOp<...>

i32_const

Pushes value as an i32.

SyntaxParametersResultDescription
i32_const valuevalue: intemits OpWithImm<...>

i32_eqz

Pops an i32 and pushes 1 if it’s zero, else 0.

SyntaxParametersResultDescription
i32_eqzemits Byte

i32_lt_s

Pops two i32s and pushes 1 if the first is less than the second as signed numbers, else 0.

SyntaxParametersResultDescription
i32_lt_semits Byte

i32_add

Pops two i32s and pushes their sum.

SyntaxParametersResultDescription
i32_addemits Byte

i32_sub

Pops two i32s and pushes the first minus the second.

SyntaxParametersResultDescription
i32_subemits Byte

i32_mul

Pops two i32s and pushes their product.

SyntaxParametersResultDescription
i32_mulemits Byte

Types

OpWithImm

struct OpWithImm<const N: int>

An opcode followed by N bytes of immediates.

MemOp

struct MemOp<const N: int>

A load or store: its opcode, then its memory operand, the alignment as a power of two and the offset added to the address.

Constants

ConstantTypeValueDescription
I32ByteByte(0x7F)The i32 value type.
I64ByteByte(0x7E)The i64 value type.
F32ByteByte(0x7D)The f32 value type.
F64ByteByte(0x7C)The f64 value type.
EMPTY_BLOCKTYPEByteByte(0x40)The block type of a block, loop or if_ that produces no value. A value type such as I32 means it produces one value of that type.