mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
all: implement wasmimport directive
Go programs can now use the //go:wasmimport module_name function_name directive to import functions from the WebAssembly runtime. For now, the directive is restricted to the runtime and syscall/js packages. * Derived from CL 350737 * Original work modified to work with changes to the IR conversion code. * Modification of CL 350737 changes to fully exist in Unified IR path (emp) * Original work modified to work with changes to the ABI configuration code. * Fixes #38248 Co-authored-by: Vedant Roy <vroy101@gmail.com> Co-authored-by: Richard Musiol <mail@richard-musiol.de> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> Change-Id: I740719735d91c306ac718a435a78e1ee9686bc16 Reviewed-on: https://go-review.googlesource.com/c/go/+/463018 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
This commit is contained in:
parent
af9f21289f
commit
02411bcd7c
29 changed files with 585 additions and 145 deletions
|
|
@ -37,6 +37,7 @@ import (
|
|||
"cmd/internal/objabi"
|
||||
"cmd/internal/src"
|
||||
"cmd/internal/sys"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
|
@ -499,7 +500,9 @@ type FuncInfo struct {
|
|||
WrapInfo *LSym // for wrapper, info of wrapped function
|
||||
JumpTables []JumpTable
|
||||
|
||||
FuncInfoSym *LSym
|
||||
FuncInfoSym *LSym
|
||||
WasmImportSym *LSym
|
||||
WasmImport *WasmImport
|
||||
}
|
||||
|
||||
// JumpTable represents a table used for implementing multi-way
|
||||
|
|
@ -558,6 +561,75 @@ func (s *LSym) File() *FileInfo {
|
|||
return f
|
||||
}
|
||||
|
||||
// WasmImport represents a WebAssembly (WASM) imported function with
|
||||
// parameters and results translated into WASM types based on the Go function
|
||||
// declaration.
|
||||
type WasmImport struct {
|
||||
// Module holds the WASM module name specified by the //go:wasmimport
|
||||
// directive.
|
||||
Module string
|
||||
// Name holds the WASM imported function name specified by the
|
||||
// //go:wasmimport directive.
|
||||
Name string
|
||||
// Params holds the imported function parameter fields.
|
||||
Params []WasmField
|
||||
// Results holds the imported function result fields.
|
||||
Results []WasmField
|
||||
}
|
||||
|
||||
func (wi *WasmImport) CreateSym(ctxt *Link) *LSym {
|
||||
var sym LSym
|
||||
|
||||
var b [8]byte
|
||||
writeByte := func(x byte) {
|
||||
sym.WriteBytes(ctxt, sym.Size, []byte{x})
|
||||
}
|
||||
writeUint32 := func(x uint32) {
|
||||
binary.LittleEndian.PutUint32(b[:], x)
|
||||
sym.WriteBytes(ctxt, sym.Size, b[:4])
|
||||
}
|
||||
writeInt64 := func(x int64) {
|
||||
binary.LittleEndian.PutUint64(b[:], uint64(x))
|
||||
sym.WriteBytes(ctxt, sym.Size, b[:])
|
||||
}
|
||||
writeString := func(s string) {
|
||||
writeUint32(uint32(len(s)))
|
||||
sym.WriteString(ctxt, sym.Size, len(s), s)
|
||||
}
|
||||
writeString(wi.Module)
|
||||
writeString(wi.Name)
|
||||
writeUint32(uint32(len(wi.Params)))
|
||||
for _, f := range wi.Params {
|
||||
writeByte(byte(f.Type))
|
||||
writeInt64(f.Offset)
|
||||
}
|
||||
writeUint32(uint32(len(wi.Results)))
|
||||
for _, f := range wi.Results {
|
||||
writeByte(byte(f.Type))
|
||||
writeInt64(f.Offset)
|
||||
}
|
||||
|
||||
return &sym
|
||||
}
|
||||
|
||||
type WasmField struct {
|
||||
Type WasmFieldType
|
||||
// Offset holds the frame-pointer-relative locations for Go's stack-based
|
||||
// ABI. This is used by the src/cmd/internal/wasm package to map WASM
|
||||
// import parameters to the Go stack in a wrapper function.
|
||||
Offset int64
|
||||
}
|
||||
|
||||
type WasmFieldType byte
|
||||
|
||||
const (
|
||||
WasmI32 WasmFieldType = iota
|
||||
WasmI64
|
||||
WasmF32
|
||||
WasmF64
|
||||
WasmPtr
|
||||
)
|
||||
|
||||
type InlMark struct {
|
||||
// When unwinding from an instruction in an inlined body, mark
|
||||
// where we should unwind to.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue