wasm

package
v0.5.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OpUnreachable byte = 0x00
	OpNop         byte = 0x01
	OpBlock       byte = 0x02
	OpLoop        byte = 0x03
	OpIf          byte = 0x04
	OpElse        byte = 0x05

	// Legacy exception-handling proposal (what clang/wasi-sdk emits for
	// setjmp/longjmp under `-mllvm -wasm-enable-sjlj`). try opens a handler
	// region with a blocktype; catch/catch_all begin handlers; throw raises a
	// tag; rethrow/delegate re-raise. These carry the setjmp/longjmp lowering.
	OpTry      byte = 0x06
	OpCatch    byte = 0x07
	OpThrow    byte = 0x08
	OpRethrow  byte = 0x09
	OpDelegate byte = 0x18
	OpCatchAll byte = 0x19

	OpEnd          byte = 0x0b
	OpBr           byte = 0x0c
	OpBrIf         byte = 0x0d
	OpBrTable      byte = 0x0e
	OpReturn       byte = 0x0f
	OpCall         byte = 0x10
	OpCallIndirect byte = 0x11

	OpDrop    byte = 0x1a
	OpSelect  byte = 0x1b
	OpSelectT byte = 0x1c

	OpLocalGet  byte = 0x20
	OpLocalSet  byte = 0x21
	OpLocalTee  byte = 0x22
	OpGlobalGet byte = 0x23
	OpGlobalSet byte = 0x24

	OpI32Load    byte = 0x28
	OpI64Load    byte = 0x29
	OpF32Load    byte = 0x2a
	OpF64Load    byte = 0x2b
	OpI32Load8S  byte = 0x2c
	OpI32Load8U  byte = 0x2d
	OpI32Load16S byte = 0x2e
	OpI32Load16U byte = 0x2f
	OpI64Load8S  byte = 0x30
	OpI64Load8U  byte = 0x31
	OpI64Load16S byte = 0x32
	OpI64Load16U byte = 0x33
	OpI64Load32S byte = 0x34
	OpI64Load32U byte = 0x35
	OpI32Store   byte = 0x36
	OpI64Store   byte = 0x37
	OpF32Store   byte = 0x38
	OpF64Store   byte = 0x39
	OpI32Store8  byte = 0x3a
	OpI32Store16 byte = 0x3b
	OpI64Store8  byte = 0x3c
	OpI64Store16 byte = 0x3d
	OpI64Store32 byte = 0x3e
	OpMemSize    byte = 0x3f
	OpMemGrow    byte = 0x40

	OpI32Const byte = 0x41
	OpI64Const byte = 0x42
	OpF32Const byte = 0x43
	OpF64Const byte = 0x44

	OpI32Eqz byte = 0x45
	OpI32Eq  byte = 0x46
	OpI32Ne  byte = 0x47
	OpI32LtS byte = 0x48
	OpI32LtU byte = 0x49
	OpI32GtS byte = 0x4a
	OpI32GtU byte = 0x4b
	OpI32LeS byte = 0x4c
	OpI32LeU byte = 0x4d
	OpI32GeS byte = 0x4e
	OpI32GeU byte = 0x4f
	OpI64Eqz byte = 0x50
	OpI64Eq  byte = 0x51
	OpI64Ne  byte = 0x52
	OpI64LtS byte = 0x53
	OpI64LtU byte = 0x54
	OpI64GtS byte = 0x55
	OpI64GtU byte = 0x56
	OpI64LeS byte = 0x57
	OpI64LeU byte = 0x58
	OpI64GeS byte = 0x59
	OpI64GeU byte = 0x5a
	OpF32Eq  byte = 0x5b
	OpF32Ne  byte = 0x5c
	OpF32Lt  byte = 0x5d
	OpF32Gt  byte = 0x5e
	OpF32Le  byte = 0x5f
	OpF32Ge  byte = 0x60
	OpF64Eq  byte = 0x61
	OpF64Ne  byte = 0x62
	OpF64Lt  byte = 0x63
	OpF64Gt  byte = 0x64
	OpF64Le  byte = 0x65
	OpF64Ge  byte = 0x66

	OpI32Clz    byte = 0x67
	OpI32Ctz    byte = 0x68
	OpI32Popcnt byte = 0x69
	OpI32Add    byte = 0x6a
	OpI32Sub    byte = 0x6b
	OpI32Mul    byte = 0x6c
	OpI32DivS   byte = 0x6d
	OpI32DivU   byte = 0x6e
	OpI32RemS   byte = 0x6f
	OpI32RemU   byte = 0x70
	OpI32And    byte = 0x71
	OpI32Or     byte = 0x72
	OpI32Xor    byte = 0x73
	OpI32Shl    byte = 0x74
	OpI32ShrS   byte = 0x75
	OpI32ShrU   byte = 0x76
	OpI32Rotl   byte = 0x77
	OpI32Rotr   byte = 0x78
	OpI64Clz    byte = 0x79
	OpI64Ctz    byte = 0x7a
	OpI64Popcnt byte = 0x7b
	OpI64Add    byte = 0x7c
	OpI64Sub    byte = 0x7d
	OpI64Mul    byte = 0x7e
	OpI64DivS   byte = 0x7f
	OpI64DivU   byte = 0x80
	OpI64RemS   byte = 0x81
	OpI64RemU   byte = 0x82
	OpI64And    byte = 0x83
	OpI64Or     byte = 0x84
	OpI64Xor    byte = 0x85
	OpI64Shl    byte = 0x86
	OpI64ShrS   byte = 0x87
	OpI64ShrU   byte = 0x88
	OpI64Rotl   byte = 0x89
	OpI64Rotr   byte = 0x8a

	OpF32Abs      byte = 0x8b
	OpF32Neg      byte = 0x8c
	OpF32Ceil     byte = 0x8d
	OpF32Floor    byte = 0x8e
	OpF32Trunc    byte = 0x8f
	OpF32Nearest  byte = 0x90
	OpF32Sqrt     byte = 0x91
	OpF32Add      byte = 0x92
	OpF32Sub      byte = 0x93
	OpF32Mul      byte = 0x94
	OpF32Div      byte = 0x95
	OpF32Min      byte = 0x96
	OpF32Max      byte = 0x97
	OpF32Copysign byte = 0x98
	OpF64Abs      byte = 0x99
	OpF64Neg      byte = 0x9a
	OpF64Ceil     byte = 0x9b
	OpF64Floor    byte = 0x9c
	OpF64Trunc    byte = 0x9d
	OpF64Nearest  byte = 0x9e
	OpF64Sqrt     byte = 0x9f
	OpF64Add      byte = 0xa0
	OpF64Sub      byte = 0xa1
	OpF64Mul      byte = 0xa2
	OpF64Div      byte = 0xa3
	OpF64Min      byte = 0xa4
	OpF64Max      byte = 0xa5
	OpF64Copysign byte = 0xa6

	OpI32WrapI64        byte = 0xa7
	OpI32TruncF32S      byte = 0xa8
	OpI32TruncF32U      byte = 0xa9
	OpI32TruncF64S      byte = 0xaa
	OpI32TruncF64U      byte = 0xab
	OpI64ExtendI32S     byte = 0xac
	OpI64ExtendI32U     byte = 0xad
	OpI64TruncF32S      byte = 0xae
	OpI64TruncF32U      byte = 0xaf
	OpI64TruncF64S      byte = 0xb0
	OpI64TruncF64U      byte = 0xb1
	OpF32ConvertI32S    byte = 0xb2
	OpF32ConvertI32U    byte = 0xb3
	OpF32ConvertI64S    byte = 0xb4
	OpF32ConvertI64U    byte = 0xb5
	OpF32DemoteF64      byte = 0xb6
	OpF64ConvertI32S    byte = 0xb7
	OpF64ConvertI32U    byte = 0xb8
	OpF64ConvertI64S    byte = 0xb9
	OpF64ConvertI64U    byte = 0xba
	OpF64PromoteF32     byte = 0xbb
	OpI32ReinterpretF32 byte = 0xbc
	OpI64ReinterpretF64 byte = 0xbd
	OpF32ReinterpretI32 byte = 0xbe
	OpF64ReinterpretI64 byte = 0xbf

	OpI32Extend8S  byte = 0xc0
	OpI32Extend16S byte = 0xc1
	OpI64Extend8S  byte = 0xc2
	OpI64Extend16S byte = 0xc3
	OpI64Extend32S byte = 0xc4

	// OpRefNull / OpRefFunc model reference-typed operands. wasm2go does not
	// implement reference types, but the multi-package call-graph scanner
	// recognises OpRefFunc as a function reference so it can record those
	// edges alongside direct OpCall edges.
	OpRefNull byte = 0xd0
	OpRefFunc byte = 0xd2

	OpPrefixFC byte = 0xfc // saturating-trunc, bulk-memory
	OpPrefixFD byte = 0xfd // SIMD proposal: v128 ops
	OpPrefixFE byte = 0xfe // threads proposal: atomics
)

WebAssembly MVP opcodes plus the 0xfc prefix. For the full list see https://webassembly.github.io/spec/core/binary/instructions.html

Variables

This section is empty.

Functions

This section is empty.

Types

type DataSegment

type DataSegment struct {
	MemIdx uint32
	Offset []byte // raw const-expr including terminating 0x0b; nil when Passive
	Bytes  []byte
	// Passive segments (flag 0x01) have no offset: they sit inert until a
	// memory.init copies from them (data.drop then discards them). LLVM emits
	// ALL data passive for shared-memory builds — the threads proposal forbids
	// active segments re-initializing memory on every thread's instantiation —
	// and initializes exactly once from a __wasm_init_memory start function.
	Passive bool
}

DataSegment is a (subset of) wasm data segment. Active segments only.

type ElementSegment

type ElementSegment struct {
	TableIdx uint32
	Offset   []byte // raw const-expr including terminating 0x0b
	FuncIdxs []uint32
}

ElementSegment is a (subset of) wasm element segment. We currently model active segments with a const-expr offset and a vec(funcidx) payload, the shape emitted by clang/wasi-libc for indirect-call tables. Other flavors panic at parse time.

type Export

type Export struct {
	Name  string
	Kind  ExportKind
	Index uint32
}

Export is a single export entry.

type ExportKind

type ExportKind byte

ExportKind selects the kind of an export.

const (
	ExportFunc   ExportKind = 0x00
	ExportTable  ExportKind = 0x01
	ExportMemory ExportKind = 0x02
	ExportGlobal ExportKind = 0x03
)

type FuncType

type FuncType struct {
	Params  []ValType
	Results []ValType
}

FuncType is a wasm function signature.

type Function

type Function struct {
	TypeIdx uint32
	Body    []byte
}

Function is a defined wasm function. Body is the raw bytes of the function's "code" entry: locals declarations followed by the instruction stream and the terminating 0x0b. Codegen decodes on demand.

type Global

type Global struct {
	Type GlobalType
	Init []byte // raw init expression bytes (terminating 0x0b included)
}

Global is a defined global with an init expression.

type GlobalType

type GlobalType struct {
	Type    ValType
	Mutable bool
}

GlobalType describes a global's type and mutability.

type Import

type Import struct {
	Module  string
	Name    string
	Kind    ImportKind
	TypeIdx uint32     // for ImportFunc
	Table   TableType  // for ImportTable
	Memory  MemoryType // for ImportMemory
	Global  GlobalType // for ImportGlobal
	Tag     Tag        // for ImportTag
}

Import is a single import entry.

type ImportKind

type ImportKind byte

ImportKind selects the kind of an import.

const (
	ImportFunc   ImportKind = 0x00
	ImportTable  ImportKind = 0x01
	ImportMemory ImportKind = 0x02
	ImportGlobal ImportKind = 0x03
	ImportTag    ImportKind = 0x04
)

type InstrReader added in v0.2.0

type InstrReader struct {
	// contains filtered or unexported fields
}

InstrReader is a stateful reader over a wasm function body's instruction stream. It is the per-instruction counterpart of the module-level Parser; downstream stages (SSA lowering, the multi-package call-graph scanner) consume the byte stream through the same primitives.

func NewInstrReader added in v0.2.0

func NewInstrReader(src []byte) *InstrReader

NewInstrReader returns a reader positioned at the start of src.

func (*InstrReader) EOF added in v0.2.0

func (r *InstrReader) EOF() bool

EOF reports whether the reader has consumed the entire stream.

func (*InstrReader) Pos added in v0.2.0

func (r *InstrReader) Pos() int

Pos returns the current byte offset.

func (*InstrReader) ReadByte added in v0.2.0

func (r *InstrReader) ReadByte() (byte, error)

ReadByte consumes and returns one byte.

func (*InstrReader) ReadF32 added in v0.2.0

func (r *InstrReader) ReadF32() (float32, error)

ReadF32 consumes four little-endian bytes as a float32.

func (*InstrReader) ReadF64 added in v0.2.0

func (r *InstrReader) ReadF64() (float64, error)

ReadF64 consumes eight little-endian bytes as a float64.

func (*InstrReader) ReadMemArg added in v0.2.0

func (r *InstrReader) ReadMemArg() (align uint32, offset uint64, err error)

ReadMemArg reads (align: u32, offset: u64). Both are unsigned LEB128; the offset is 64-bit so memory64 memargs decode losslessly (wasm32 offsets simply never exceed 32 bits).

func (*InstrReader) ReadS32 added in v0.2.0

func (r *InstrReader) ReadS32() (int32, error)

ReadS32 consumes a signed LEB128 and rejects values that don't fit in 32 bits.

func (*InstrReader) ReadS33 added in v0.2.0

func (r *InstrReader) ReadS33() (int64, error)

ReadS33 consumes a signed LEB128 with up to 33 significant bits — used by block-type immediates.

func (*InstrReader) ReadS64 added in v0.2.0

func (r *InstrReader) ReadS64() (int64, error)

ReadS64 consumes a signed LEB128 with up to 64 significant bits.

func (*InstrReader) ReadSN added in v0.2.0

func (r *InstrReader) ReadSN(bits uint) (int64, error)

ReadSN consumes a signed LEB128 with the given bit-width cap.

func (*InstrReader) ReadU32 added in v0.2.0

func (r *InstrReader) ReadU32() (uint32, error)

ReadU32 consumes an unsigned LEB128 and rejects values that don't fit in 32 bits.

func (*InstrReader) ReadU64 added in v0.2.0

func (r *InstrReader) ReadU64() (uint64, error)

ReadU64 consumes an unsigned LEB128 up to 64 bits.

func (*InstrReader) ReadVecU32 added in v0.2.0

func (r *InstrReader) ReadVecU32() ([]uint32, error)

ReadVecU32 reads a vec(u32).

func (*InstrReader) SkipImmediates added in v0.2.0

func (r *InstrReader) SkipImmediates(op byte) error

SkipImmediates advances r past the immediate operands of opcode op without doing any semantic decoding. Mirrors the instruction shapes the SSA lowering pass handles; opcodes whose immediate shape isn't modeled here return an error so callers stay in sync with the instruction set wasm2go actually emits. Silent fall-through used to drop real call edges (e.g. when an unknown opcode was followed by a `call` opcode value that SkipImmediates then misread as raw bytes).

func (*InstrReader) Src added in v0.2.0

func (r *InstrReader) Src() []byte

Src returns the underlying instruction bytes. Useful for callers that want to extract a sub-slice (e.g. for nested-block recursion).

type Limits

type Limits struct {
	Min    uint64
	Max    uint64
	HasMax bool
	Shared bool // threads proposal: memory declared shared (limits flag 0x02)
	Is64   bool // memory64 proposal: 64-bit index space (limits flag 0x04)
}

Limits describes table or memory limits.

type MemoryType

type MemoryType struct {
	Limits Limits
	Is64   bool
}

MemoryType describes a memory.

type Module

type Module struct {
	Types     []FuncType
	Imports   []Import
	Functions []Function
	Tables    []TableType
	Memories  []MemoryType
	Globals   []Global
	Exports   []Export
	Start     *uint32
	Elements  []ElementSegment
	Datas     []DataSegment
	Tags      []Tag // defined tags (section 13); tag index space is imported tags then these

	// Convenience: number of imported funcs (= count of Imports with Kind==ImportFunc).
	// Function index space is [imported funcs ...] then Functions[i].
	NumImportedFuncs   uint32
	NumImportedTables  uint32
	NumImportedMems    uint32
	NumImportedGlobals uint32
	NumImportedTags    uint32
}

Module is the parsed in-memory representation of a wasm binary.

func Parse

func Parse(r io.Reader) (*Module, error)

Parse reads a WebAssembly binary from r and returns the parsed Module. Function bodies are kept as raw byte slices.

func (*Module) FuncTypeOf

func (m *Module) FuncTypeOf(idx uint32) FuncType

FuncTypeOf returns the function type at the given function index, accounting for imported functions.

func (*Module) Memory64 added in v0.5.0

func (m *Module) Memory64() bool

Memory64 reports whether the module's memory (defined or imported) uses the memory64 proposal's 64-bit index space.

type TableType

type TableType struct {
	ElemType ValType
	Limits   Limits
}

TableType describes a table.

type Tag added in v0.4.1

type Tag struct {
	Attribute byte
	TypeIdx   uint32 // index into Types; params are the exception's operand types
}

Tag is an exception-handling tag (section 13 / tag import). The exception-handling proposal models each `throw`able exception as a tag whose operand types are given by a function type (params = operands, results empty). Attribute is 0x00 for exceptions.

type ValType

type ValType byte

ValType is a wasm value type.

const (
	ValI32       ValType = 0x7f
	ValI64       ValType = 0x7e
	ValF32       ValType = 0x7d
	ValF64       ValType = 0x7c
	ValV128      ValType = 0x7b
	ValFuncref   ValType = 0x70
	ValExternref ValType = 0x6f
)

func (ValType) String

func (v ValType) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL