Documentation
¶
Overview ¶
Package compile implements a WebAssembly compiler.
Text ¶
Module sections (wasm v1) which affect the immutable text (machine code):
Type Import Function Table Memory (*) Global (*) Element Code
(*) Memory sizes and global values do not affect text, only their counts and types do.
Sections which have no effect on text:
Export Start Data Custom sections
Vector-based import function indexes also affect text, but their addresses are configured at run-time by mapping the vector immediately before text.
Globals and memory ¶
The memory, global and data sections comprise a single mutable buffer.
Stack ¶
Any effect the export and start sections have happens via the run-time stack buffer, which must be initialized with optional start and entry function addresses.
Stack regions from low to high address:
- Space for runtime-specific variables (size must be a multiple of 32).
- Space for signal stacks and red zones (size must be a multiple of 32).
- 240 bytes for use by trap handlers and vector-based import functions.
- 8 bytes for trap handler return address (included in call stack).
- 8 bytes for an extra function call (included in call stack).
- The rest of the call stack (size must be a multiple of 8).
- Start function address (4 bytes padded to 8).
- Entry function address (4 bytes padded to 8).
Function addresses are relative to the start of text. Zero address causes the function to be skipped.
Stack pointer is initially positioned between regions 6 and 7. Function prologue compares the stack pointer against the threshold between regions 5 and 6 (the stack limit).
(Size requirements for regions 1-5 cause their total size to be a multiple of 32, so that the stack limit divided by 16 is an even number. It's necessary for the ARM64 backend's suspension logic.)
Index ¶
- Constants
- func LoadCodeSection(config *CodeConfig, r Reader, mod Module, lib Library) (err error)
- func LoadCustomSections(config *Config, r Reader) (err error)
- func LoadDataSection(config *DataConfig, r Reader, mod Module) (err error)
- func ValidateDataSection(config *Config, r Reader, mod Module) (err error)
- type CodeBuffer
- type CodeConfig
- type Config
- type DataBuffer
- type DataConfig
- type Library
- func (l Library) ExportFunc(field string) (funcIndex uint32, sig wa.FuncType, found bool)
- func (l Library) ImportFunc(i int) (module, field string, sig wa.FuncType)
- func (lib *Library) LoadSections(r Reader) (err error)
- func (l Library) NumImportFuncs() int
- func (l *Library) SetImportFunc(i int, vectorIndex int)
- func (l *Library) XXX_Internal() interface{}
- type Module
- func (mod Module) AsLibrary() (lib Library, err error)
- func (m Module) ExportFunc(field string) (funcIndex uint32, sig wa.FuncType, found bool)
- func (m Module) ExportFuncs() map[string]uint32
- func (m Module) FuncTypeIndexes() []uint32
- func (m Module) FuncTypes() []wa.FuncType
- func (m Module) GlobalTypes() []wa.GlobalType
- func (m Module) GlobalsSize() int
- func (m Module) ImportFunc(i int) (module, field string, sig wa.FuncType)
- func (m Module) ImportGlobal(i int) (module, field string, t wa.Type)
- func (m Module) InitialMemorySize() int
- func (m Module) MemorySizeLimit() int
- func (m Module) NumImportFuncs() int
- func (m Module) NumImportGlobals() int
- func (m *Module) SetImportFunc(i int, libFunc uint32)
- func (m *Module) SetImportGlobal(i int, init uint64)
- func (m Module) StartFunc() (funcIndex uint32, defined bool)
- func (m Module) Types() []wa.FuncType
- type ModuleConfig
- type ObjectMapper
- type Reader
Constants ¶
const (
DefaultMaxTextSize = 0x7fff0000 // below 2 GB to mitigate address calculation bugs
)
const (
MaxMemorySize = math.MaxInt32 / wa.PageSize
)
const ObjectVersion = 0
ABI version of generated machine code.
Variables ¶
This section is empty.
Functions ¶
func LoadCodeSection ¶ added in v0.5.0
func LoadCodeSection(config *CodeConfig, r Reader, mod Module, lib Library) (err error)
LoadCodeSection reads a WebAssembly module's code section and generates machine code.
If CodeBuffer panicks with an error, it will be returned by this function.
func LoadCustomSections ¶ added in v0.8.0
LoadCustomSections reads WebAssembly module's extension sections.
func LoadDataSection ¶ added in v0.5.0
func LoadDataSection(config *DataConfig, r Reader, mod Module) (err error)
LoadDataSection reads a WebAssembly module's data section and generates initial contents of mutable program state (globals and linear memory).
If DataBuffer panicks with an error, it will be returned by this function.
Types ¶
type CodeBuffer ¶ added in v0.5.0
type CodeConfig ¶ added in v0.5.0
type CodeConfig struct {
MaxTextSize int // Effective if Text is nil; defaults to DefaultMaxTextSize.
Text CodeBuffer // Initialized with default implementation if nil.
Mapper ObjectMapper
EventHandler func(event.Event)
LastInitFunc uint32
Config
}
CodeConfig for a single compiler invocation. Either MaxTextSize or Text should be specified, but not both.
type Config ¶ added in v0.6.0
type Config struct {
// SectionMapper is invoked for every section (standard or custom), just
// after the section id byte. It must read and return the payload length
// (varuint32), but not the payload itself.
SectionMapper func(sectionID byte, r Reader) (payloadLen uint32, err error)
// CustomSectionLoader is invoked for every custom section. It must read
// exactly payloadLen bytes, or return an error. SectionMapper (if
// configured) has been invoked just before it.
CustomSectionLoader func(r Reader, payloadLen uint32) error
}
Config for loading WebAssembly module sections.
type DataBuffer ¶
type DataConfig ¶ added in v0.5.0
type DataConfig struct {
GlobalsMemory DataBuffer // Initialized with default implementation if nil.
MemoryAlignment int // Initialized with minimal value if zero.
Config
}
DataConfig for a single compiler invocation.
type Library ¶ added in v0.21.0
type Library struct {
// contains filtered or unexported fields
}
func (Library) ExportFunc ¶ added in v0.21.0
func (Library) ImportFunc ¶ added in v0.21.0
func (*Library) LoadSections ¶ added in v0.21.0
func (Library) NumImportFuncs ¶ added in v0.21.0
func (*Library) SetImportFunc ¶ added in v0.21.0
func (*Library) XXX_Internal ¶ added in v0.21.0
func (l *Library) XXX_Internal() interface{}
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module contains a WebAssembly module specification without code or data.
func LoadInitialSections ¶ added in v0.6.0
func LoadInitialSections(config *ModuleConfig, r Reader) (m Module, err error)
LoadInitialSections reads module header and all sections preceding code and data.
func (Module) ExportFunc ¶ added in v0.8.0
func (Module) ExportFuncs ¶ added in v0.8.0
func (Module) FuncTypeIndexes ¶ added in v0.8.0
func (Module) GlobalTypes ¶ added in v0.12.0
func (m Module) GlobalTypes() []wa.GlobalType
func (Module) GlobalsSize ¶
func (Module) ImportFunc ¶ added in v0.5.0
func (Module) ImportGlobal ¶ added in v0.5.0
func (Module) InitialMemorySize ¶ added in v0.7.0
func (Module) MemorySizeLimit ¶ added in v0.7.0
func (Module) NumImportFuncs ¶ added in v0.7.0
func (Module) NumImportGlobals ¶ added in v0.7.0
func (*Module) SetImportFunc ¶ added in v0.7.0
func (*Module) SetImportGlobal ¶ added in v0.7.0
type ModuleConfig ¶ added in v0.6.0
type ModuleConfig struct {
Config
}
ModuleConfig for a single compiler invocation.
type ObjectMapper ¶ added in v0.8.0
type ObjectMapper = obj.ObjectMapper