kwasm.format.text.instruction

Kwasm.format.text.instruction

package kwasm.format.text.instruction

Methods

parseControlInstruction

fun List<Token>.parseControlInstruction(fromIndex: Int): ParseResult<ControlInstruction>?

Attempts to parse a ControlInstruction from the receiving Token. Returns null if none was found.

From the docs:


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)
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′={})

Receiver

Name Description
List<Token>

Parameters

Name Description
fromIndex: Int

ReturnValue

Name Description
ParseResult<ControlInstruction>?

parseExpression

fun List<Token>.parseExpression(fromIndex: Int): ParseResult<Expression>

Parses an Expression from the receiving List of Tokens.

Receiver

Name Description
List<Token>

Parameters

Name Description
fromIndex: Int

ReturnValue

Name Description
ParseResult<Expression>

parseFoldedInstructions

fun List<Token>.parseFoldedInstructions(fromIndex: Int): ParseResult<AstNodeList<Instruction>>

Parses a possibly-empty AstNodeList of folded Instructions into an unwrapped list of non-folded instructions.

See parseFoldedInstruction.

Receiver

Name Description
List<Token>

Parameters

Name Description
fromIndex: Int

ReturnValue

Name Description
ParseResult<AstNodeList<Instruction>>

parseFoldedInstruction

fun List<Token>.parseFoldedInstruction(fromIndex: Int): ParseResult<AstNodeList<Instruction>>?

From the docs:

Instructions can be written as S-expressions by grouping them into folded form. In that notation, an instruction is wrapped in parentheses and optionally includes nested folded instructions to indicate its operands.

In the case of kwasm.ast.ControlInstruction.Block/kwasm.ast.ControlInstruction.Loop instructions, the folded form omits the end delimiter. For kwasm.ast.ControlInstruction.If instructions, both branches have to be wrapped into nested S-expressions, headed by the keywords then and else.

Receiver

Name Description
List<Token>

Parameters

Name Description
fromIndex: Int

ReturnValue

Name Description
ParseResult<AstNodeList<Instruction>>?

parseInstructions

fun List<Token>.parseInstructions(fromIndex: Int, min: Int, max: Int): ParseResult<AstNodeList<Instruction>>

Parses a List of Instructions from the Tokens.

Throws ParseException if fewer than min are found, and returns at most max.

Receiver

Name Description
List<Token>

Parameters

Name Description
fromIndex: Int
min: Int
max: Int

ReturnValue

Name Description
ParseResult<AstNodeList<Instruction>>

parseInstruction

fun List<Token>.parseInstruction(fromIndex: Int): ParseResult<Instruction>?

Parses an Instruction from the receiving Tokens List.

Receiver

Name Description
List<Token>

Parameters

Name Description
fromIndex: Int

ReturnValue

Name Description
ParseResult<Instruction>?

parseLabel

fun List<Token>.parseLabel(startIndex: Int): ParseResult<Label>

Parses an Identifier.Label from the tokens.

From the docs:


labelI ::= v:id    => compose(, I) (if v ∉ I.labels)
ϵ       => compose({ labels (ϵ) }, I)

When the Token at startIndex is not an kwasm.format.text.token.Identifier, this function:

Note: Composition will be left up to the interpreter.

Receiver

Name Description
List<Token>

Parameters

Name Description
startIndex: Int

ReturnValue

Name Description
ParseResult<Label>

parseMemarg

fun List<Token>.parseMemarg(fromIndex: Int, expectedMaxBytes: Int): ParseResult<MemArg>

Parses a MemArg from the receiving List of Tokens.

From the docs:


memarg_N   ::= o:offset a:align_N  => { align n, offset o } (if a = 2^n)
offset     ::= ‘offset=’ o:u32     => o
ϵ => 0
align_N    ::= ‘align=’ a:u32      => a
ϵ => N

Receiver

Name Description
List<Token>

Parameters

Name Description
fromIndex: Int
expectedMaxBytes: Int

ReturnValue

Name Description
ParseResult<MemArg>

parseMemoryInstruction

fun List<Token>.parseMemoryInstruction(fromIndex: Int): ParseResult<MemoryInstruction>?

Attempts to parse a MemoryInstruction from the receiving List of Tokens. If none is found at the given start index (fromIndex), null is returned.

From the docs:


plaininstrI    ::= ‘i32.load’ m:memarg4        => i32.load m
‘i64.load’ m:memarg8        => i64.load m
‘f32.load’ m:memarg4        => f32.load m
‘f64.load’ m:memarg8        => f64.load m
‘i32.load8_s’ m:memarg1     => i32.load8_s m
‘i32.load8_u’ m:memarg1     => i32.load8_u m
‘i32.load16_s’ m:memarg2    => i32.load16_s m
‘i32.load16_u’ m:memarg2    => i32.load16_u m
‘i64.load8_s’ m:memarg1     => i64.load8_s m
‘i64.load8_u’ m:memarg1     => i64.load8_u m
‘i64.load16_s’ m:memarg2    => i64.load16_s m
‘i64.load16_u’ m:memarg2    => i64.load16_u m
‘i64.load32_s’ m:memarg4    => i64.load32_s m
‘i64.load32_u’ m:memarg4    => i64.load32_u m
‘i32.store’ m:memarg4       => i32.store m
‘i64.store’ m:memarg8       => i64.store m
‘f32.store’ m:memarg4       => f32.store m
‘f64.store’ m:memarg8       => f64.store m
‘i32.store8’ m:memarg1      => i32.store8 m
‘i32.store16’ m:memarg2     => i32.store16 m
‘i64.store8’ m:memarg1      => i64.store8 m
‘i64.store16’ m:memarg2     => i64.store16 m
‘i64.store32’ m:memarg4     => i64.store32 m
‘memory.size’               => memory.size
‘memory.grow’               => memory.grow

Receiver

Name Description
List<Token>

Parameters

Name Description
fromIndex: Int

ReturnValue

Name Description
ParseResult<MemoryInstruction>?

parseNumericConstant

fun List<Token>.parseNumericConstant(fromIndex: Int): ParseResult<NumericConstantInstruction<*>>?

Attempts to parse a NumericConstantInstruction from the receiving List of Tokens.

From the docs:


plaininstrI    ::= ‘i32.const’ n:i32 => i32.const n
‘i64.const’ n:i64 => i64.const n
‘f32.const’ z:f32 => f32.const z
‘f64.const’ z:f64 => f64.const z

Receiver

Name Description
List<Token>

Parameters

Name Description
fromIndex: Int

ReturnValue

Name Description
ParseResult<NumericConstantInstruction<*>>?

parseNumericInstruction

fun List<Token>.parseNumericInstruction(fromIndex: Int): ParseResult<NumericInstruction>?

Parses a NumericInstruction from the receiving List of Tokens.

See the docs for details.

Receiver

Name Description
List<Token>

Parameters

Name Description
fromIndex: Int

ReturnValue

Name Description
ParseResult<NumericInstruction>?

parseParametricInstruction

fun List<Token>.parseParametricInstruction(fromIndex: Int): ParseResult<ParametricInstruction>?

Parses a ParametricInstruction from the receiving List of Tokens.

See the docs for details.

Receiver

Name Description
List<Token>

Parameters

Name Description
fromIndex: Int

ReturnValue

Name Description
ParseResult<ParametricInstruction>?

parseVariableInstruction

fun List<Token>.parseVariableInstruction(fromIndex: Int): ParseResult<VariableInstruction>?

Attempts to parse a VariableInstruction from the receiving List of Tokens.

From the docs:


plaininstrI    ::= ‘local.get’ x:localidx => local.get x
‘local.set’ x:localidx => local.set x
‘local.tee’ x:localidx => local.tee x
‘global.get’ x:globalidx => global.get x
‘global.set’ x:globalidx => global.set x

Receiver

Name Description
List<Token>

Parameters

Name Description
fromIndex: Int

ReturnValue

Name Description
ParseResult<VariableInstruction>?