diff options
| author | Cherry Mui <cherryyz@google.com> | 2024-08-03 14:20:58 -0400 |
|---|---|---|
| committer | Cherry Mui <cherryyz@google.com> | 2024-08-09 20:07:54 +0000 |
| commit | 1cf6e31f0d03bb3571cfe034f2d909591a0ae453 (patch) | |
| tree | c89cb9d652377eb4517c497f6c4d4ea4ccf29b70 /src/cmd/internal/obj/objfile.go | |
| parent | ff2a57ba92b9ecc9315c992b332279d0428c36d7 (diff) | |
| download | go-1cf6e31f0d03bb3571cfe034f2d909591a0ae453.tar.xz | |
cmd/compile: add basic wasmexport support
This CL adds a compiler directive go:wasmexport, which applies to
a Go function and makes it an exported function of the Wasm module
being built, so it can be called directly from the host. As
proposed in #65199, parameter and result types are limited to
32-bit and 64-bit integers and floats, and there can be at most
one result.
As the Go and Wasm calling conventions are different, for a
wasmexport function we generate a wrapper function does the ABI
conversion at compile time.
Currently this CL only adds basic support. In particular,
- it only supports executable mode, i.e. the Go wasm module calls
into the host via wasmimport, which then calls back to Go via
wasmexport. Library (c-shared) mode is not implemented yet.
- only supports wasip1, not js.
- if the exported function unwinds stacks (goroutine switch, stack
growth, etc.), it probably doesn't work.
TODO: support stack unwinding, c-shared mode, js.
For #65199.
Change-Id: Id1777c2d44f7d51942c1caed3173c0a82f120cc4
Reviewed-on: https://go-review.googlesource.com/c/go/+/603055
Reviewed-by: Than McIntosh <thanm@golang.org>
Reviewed-by: Randy Reddig <randy.reddig@fastly.com>
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/cmd/internal/obj/objfile.go')
| -rw-r--r-- | src/cmd/internal/obj/objfile.go | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/cmd/internal/obj/objfile.go b/src/cmd/internal/obj/objfile.go index cbdc5a3486..de38349930 100644 --- a/src/cmd/internal/obj/objfile.go +++ b/src/cmd/internal/obj/objfile.go @@ -361,6 +361,9 @@ func (w *writer) Sym(s *LSym) { if s.ABIWrapper() { flag2 |= goobj.SymFlagABIWrapper } + if s.Func() != nil && s.Func().WasmExport != nil { + flag2 |= goobj.SymFlagWasmExport + } if strings.HasPrefix(name, "gofile..") { name = filepath.ToSlash(name) } @@ -627,6 +630,9 @@ func (w *writer) Aux(s *LSym) { } w.aux1(goobj.AuxWasmImport, fn.WasmImport.AuxSym) } + if fn.WasmExport != nil { + w.aux1(goobj.AuxWasmType, fn.WasmExport.AuxSym) + } } else if v := s.VarInfo(); v != nil { if v.dwarfInfoSym != nil && v.dwarfInfoSym.Size != 0 { w.aux1(goobj.AuxDwarfInfo, v.dwarfInfoSym) @@ -737,6 +743,9 @@ func nAuxSym(s *LSym) int { } n++ } + if fn.WasmExport != nil { + n++ + } } else if v := s.VarInfo(); v != nil { if v.dwarfInfoSym != nil && v.dwarfInfoSym.Size != 0 { n++ @@ -801,6 +810,9 @@ func genFuncInfoSyms(ctxt *Link) { if wi := fn.WasmImport; wi != nil { auxsyms = append(auxsyms, wi.AuxSym) } + if we := fn.WasmExport; we != nil { + auxsyms = append(auxsyms, we.AuxSym) + } for _, s := range auxsyms { if s == nil || s.Size == 0 { continue |
