package kwasm.ast.instruction


Name Description
sealed class ControlInstruction

Base class for all control Instruction implementations.

From the docs:

Structured control instructions can bind an optional symbolic label identifier. The same label identifier may optionally be repeated after the corresponding end and else pseudo instructions, to indicate the matching delimiters.

blockinstr(I) ::=
‘block’ I′:label(I) rt:resulttype (in:instr(I′))* ‘end’ id?
=> block rt in* end (if id? = ϵ ∨ id? = label)
‘loop’ I′:label(I) rt:resulttype (in:instr(I′))* ‘end’ id?
=> loop rt in* end (if id? = ϵ ∨ id? = label)
‘if’ I′:label(I) rt:resulttype (in^1:instr(I′))* (‘else’ id?^1 (in^2:instr(I')))? ‘end’ id?^2
=> if rt in^1 else in2 end (if id?^1 = ϵ ∨ id?^1 = label, id?^2 = ϵ ∨ id?^2 = label)

All other control instruction are represented verbatim.

plaininstr(I)  ::= ‘unreachable’                                   => unreachable
‘nop’                                           => nop
‘br’ l:labelidx(I)                              => br l
‘br_if’ l:labelidx(I)                           => br_if l
‘br_table' l:vec(labelidx(I)) l^N:labelidx(I)  => br_table l∗ l^N
‘return’                                        => return
‘call’ x:funcidx(I)                             => call x
‘call_indirect’ x,I′:typeuse(I)                 => call_indirect x (if I′={})

Note The side condition stating that the identifier context I′ must be empty in the rule for call_indirect enforces that no identifier can be bound in any param declaration appearing in the type annotation.

data class Expression

Representation of an "expression" in WASM.

According to the docs, an Expression is a possibly-empty list of Instructions.

interface Instruction

Base for all instruction AstNode implementations.

interface BlockInstruction

Base for all instruction AstNode implementations which are "block" instructions.

interface MarkerInstruction

Represents a marker (either start or end) of a flattened BlockInstruction.

interface BlockStart

Marker of the start of a flattened BlockInstruction. The endPosition is used to know which instruction should be jumped-to when leaving the BlockInstruction.

interface BlockEnd

Marker of the end of BlockInstruction, during execution, the startPosition should be used to locate the start of the block which is then used to locate the first instruction after the original BlockInstruction (may not always be the next instruction after a BlockEnd, for example, the BlockEnds used for each branch of an if instruction).

interface Argument

Defines an AstNode which represents an argument to an Instruction.

class MemArg

Represents an Argument to a memory Instruction.

From the docs:

The offset and alignment immediates to memory instructions are optional. The offset defaults to 0 and the alignment to the storage size of the respective memory access, which is its natural alignment.

sealed class MemoryInstruction

Base class for all Instructions dealing with memory.


the docs:

Instructions in this group are concerned with linear memory.

Memory is accessed with load and store instructions for the different value types. They all take a memory immediate 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 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.

instr  ::= inn.load memarg
           fnn.load memarg
           inn.load8_sx memarg
           inn.load16_sx memarg
           i64.load32_sx memarg
           inn.store8 memarg
           inn.store16 memarg
           i64.store32 memarg
sealed class NumericConstantInstruction

Base for all numeric Constant implementations.

See the docs for more information.

sealed class NumericInstruction

Defines the various numeric Instruction variants.

See more in the docs.

sealed class ParametricInstruction

Defines the various parametric Instruction variants.

See more in the docs.

sealed class VariableInstruction

Base for all variable Instruction implementations.

See the docs for more information.



fun List<Instruction>.flatten(startPosition: Int): List<Instruction>

Flattens the receiving List of Instructions into a new list, keeping track of the startPosition and appropriately passing it to the children, so that BlockStart and BlockEnd instances can be created correctly.


Name Description


Name Description
startPosition: Int


Name Description