compile

package
v0.36.0 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2022 License: BSD-3-Clause Imports: 25 Imported by: 8

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:

  1. Space for runtime-specific variables.
  2. Space for signal stacks and red zones.
  3. 240 bytes for use by trap handlers and vector-based import functions.
  4. 8 bytes for trap handler return address (included in call stack).
  5. 8 bytes for an extra function call (included in call stack).
  6. The rest of the call stack (size must be a multiple of 8).
  7. Start function address (4 bytes padded to 8).
  8. Entry function address (4 bytes padded to 8).

Address of region 3 must be aligned so that the threshold between regions 5 and 6 (the stack limit) is a multiple of 256.

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).

Index

Constants

View Source
const (
	MaxTextSize   = 512 * 1024 * 1024
	MaxMemorySize = maxMemoryPages * wa.PageSize
)
View Source
const ObjectVersion = 1

ABI version of generated machine code.

Variables

This section is empty.

Functions

func LoadCodeSection added in v0.32.0

func LoadCodeSection(config *CodeConfig, r Loader, mod Module, lib Library) (err error)

LoadCodeSection reads a WebAssembly module's code section and generates machine code.

If CodeBuffer panics with an error, it will be returned by this function.

func LoadCustomSections added in v0.32.0

func LoadCustomSections(config *Config, r Loader) (err error)

LoadCustomSections reads WebAssembly module's extension sections.

func LoadDataSection added in v0.32.0

func LoadDataSection(config *DataConfig, r Loader, 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 panics with an error, it will be returned by this function.

func ValidateDataSection added in v0.32.0

func ValidateDataSection(config *Config, r Loader, mod Module) (err error)

ValidateDataSection reads a WebAssembly module's data section.

Types

type Breakpoint added in v0.32.0

type Breakpoint = gen.Breakpoint

type CodeBuffer added in v0.32.0

type CodeBuffer = code.Buffer

type CodeConfig added in v0.32.0

type CodeConfig struct {
	MaxTextSize  int        // Set to MaxTextSize if unspecified or too large.
	Text         CodeBuffer // Initialized with default implementation if nil.
	Mapper       ObjectMapper
	EventHandler func(event.Event)
	LastInitFunc uint32
	Breakpoints  map[uint32]Breakpoint
	Config
}

CodeConfig for a single compiler invocation.

MaxTextSize field limits memory allocations only when Text field is not specified. To limit memory allocations when providing a custom CodeBuffer implementation, the implementation must take care of it.

type Config added in v0.32.0

type Config struct {
	// ModuleMapper (if set) is called for every section (standard or custom).
	ModuleMapper ModuleMapper

	// CustomSectionLoader (if set) is invoked for every custom section.  It
	// must read exactly payloadSize bytes, or return an error.  ModuleMapper
	// has been invoked just before it.
	//
	// If the section.Unwrapped error is returned, the consumed length may be
	// less than payloadSize.  The rest of the payload will be treated as
	// another section.  (The Unwrapped value itself must be returned, not an
	// error wrapping it.)
	CustomSectionLoader func(r Reader, payloadSize uint32) error
}

Config for loading WebAssembly module sections.

type DataBuffer

type DataBuffer = data.Buffer

type DataConfig added in v0.32.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 DebugObjectMapper added in v0.32.0

type DebugObjectMapper = obj.DebugObjectMapper

type Library added in v0.32.0

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

func (*Library) ExportFunc added in v0.32.0

func (lib *Library) ExportFunc(field string) (funcIndex uint32, sig wa.FuncType, found bool)

func (*Library) ImportFunc added in v0.32.0

func (lib *Library) ImportFunc(i int) (module, field string, sig wa.FuncType)

func (*Library) LoadSections added in v0.32.0

func (lib *Library) LoadSections(r Loader) (err error)

func (*Library) NumImportFuncs added in v0.32.0

func (lib *Library) NumImportFuncs() int

func (*Library) SetImportFunc added in v0.32.0

func (lib *Library) SetImportFunc(i, vectorIndex int)

type Loader added in v0.35.0

type Loader = loader.Loader

Loader is suitable for use with module loading functions.

func NewLoader added in v0.35.0

func NewLoader(r binary.Reader) Loader

NewLoader creates a WebAssembly module loader.

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.32.0

func LoadInitialSections(config *ModuleConfig, r Loader) (m Module, err error)

LoadInitialSections reads module header and all sections preceding code and data.

func (*Module) AsLibrary added in v0.32.0

func (m *Module) AsLibrary() (lib Library, err error)

func (*Module) ExportFunc added in v0.32.0

func (m *Module) ExportFunc(field string) (funcIndex uint32, sig wa.FuncType, found bool)

func (*Module) ExportFuncs added in v0.32.0

func (m *Module) ExportFuncs() map[string]uint32

func (*Module) FuncTypeIndexes added in v0.32.0

func (m *Module) FuncTypeIndexes() []uint32

func (*Module) FuncTypes added in v0.32.0

func (m *Module) FuncTypes() []wa.FuncType

func (*Module) GlobalTypes added in v0.32.0

func (m *Module) GlobalTypes() []wa.GlobalType

func (*Module) GlobalsSize

func (m *Module) GlobalsSize() int

func (*Module) ImportFunc added in v0.32.0

func (m *Module) ImportFunc(i int) (module, field string, sig wa.FuncType)

func (*Module) ImportGlobal added in v0.32.0

func (m *Module) ImportGlobal(i int) (module, field string, t wa.Type)

func (*Module) InitialMemorySize added in v0.32.0

func (m *Module) InitialMemorySize() int

func (*Module) MemorySizeLimit added in v0.32.0

func (m *Module) MemorySizeLimit() int

func (*Module) NumImportFuncs added in v0.32.0

func (m *Module) NumImportFuncs() int

func (*Module) NumImportGlobals added in v0.32.0

func (m *Module) NumImportGlobals() int

func (*Module) SetImportFunc added in v0.32.0

func (m *Module) SetImportFunc(i int, libFunc uint32)

func (*Module) SetImportGlobal added in v0.32.0

func (m *Module) SetImportGlobal(i int, value uint64)

func (*Module) StartFunc added in v0.32.0

func (m *Module) StartFunc() (funcIndex uint32, defined bool)

func (*Module) Types added in v0.32.0

func (m *Module) Types() []wa.FuncType

type ModuleConfig added in v0.32.0

type ModuleConfig struct {
	MaxExports int
	Config
}

ModuleConfig for a single compiler invocation.

type ModuleMapper added in v0.34.0

type ModuleMapper = section.ModuleMapper

type ObjectMapper added in v0.32.0

type ObjectMapper = obj.ObjectMapper

type Reader

type Reader = binary.Reader

Reader is suitable for reading a module.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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