mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/internal/obj: minor refactor of wasmimport code
This CL does some minor refactoring of the code handling wasmimport. - Put the WasmImport aux reading and writing code together for symmetry. - Define WasmFuncType, embedded in WasmImport. WasmFuncType could also be used (later) for wasmexport. - Move code generation code to a separate function. The containing function is already pretty large. - Simplify linker code a little bit. The loader convention is to return the 0 Sym for nonexistent symbol, instead of a separate boolean. No change in generated code. Passes toolstash -cmp (GOARCH=wasm GOOS=wasip1 go build -toolexec "toolstash -cmp" -a std cmd). Change-Id: Idc2514f84a08621333841ae4034b81130e0ce411 Reviewed-on: https://go-review.googlesource.com/c/go/+/603135 Reviewed-by: Than McIntosh <thanm@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
parent
a7c7ec5995
commit
03e5d83ca7
6 changed files with 222 additions and 194 deletions
|
|
@ -12,7 +12,6 @@ import (
|
|||
"cmd/link/internal/ld"
|
||||
"cmd/link/internal/loader"
|
||||
"cmd/link/internal/sym"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"internal/abi"
|
||||
"internal/buildcfg"
|
||||
|
|
@ -61,55 +60,8 @@ type wasmFuncType struct {
|
|||
}
|
||||
|
||||
func readWasmImport(ldr *loader.Loader, s loader.Sym) obj.WasmImport {
|
||||
reportError := func(err error) { panic(fmt.Sprintf("failed to read WASM import in sym %v: %v", s, err)) }
|
||||
|
||||
data := ldr.Data(s)
|
||||
|
||||
readUint32 := func() (v uint32) {
|
||||
v = binary.LittleEndian.Uint32(data)
|
||||
data = data[4:]
|
||||
return
|
||||
}
|
||||
|
||||
readUint64 := func() (v uint64) {
|
||||
v = binary.LittleEndian.Uint64(data)
|
||||
data = data[8:]
|
||||
return
|
||||
}
|
||||
|
||||
readByte := func() byte {
|
||||
if len(data) == 0 {
|
||||
reportError(io.EOF)
|
||||
}
|
||||
|
||||
b := data[0]
|
||||
data = data[1:]
|
||||
return b
|
||||
}
|
||||
|
||||
readString := func() string {
|
||||
n := readUint32()
|
||||
|
||||
s := string(data[:n])
|
||||
|
||||
data = data[n:]
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
var wi obj.WasmImport
|
||||
wi.Module = readString()
|
||||
wi.Name = readString()
|
||||
wi.Params = make([]obj.WasmField, readUint32())
|
||||
for i := range wi.Params {
|
||||
wi.Params[i].Type = obj.WasmFieldType(readByte())
|
||||
wi.Params[i].Offset = int64(readUint64())
|
||||
}
|
||||
wi.Results = make([]obj.WasmField, readUint32())
|
||||
for i := range wi.Results {
|
||||
wi.Results[i].Type = obj.WasmFieldType(readByte())
|
||||
wi.Results[i].Offset = int64(readUint64())
|
||||
}
|
||||
wi.Read(ldr.Data(s))
|
||||
return wi
|
||||
}
|
||||
|
||||
|
|
@ -207,8 +159,8 @@ func asmb2(ctxt *ld.Link, ldr *loader.Loader) {
|
|||
for ri := 0; ri < relocs.Count(); ri++ {
|
||||
r := relocs.At(ri)
|
||||
if r.Type() == objabi.R_WASMIMPORT {
|
||||
if lsym, ok := ldr.WasmImportSym(fn); ok {
|
||||
wi := readWasmImport(ldr, lsym)
|
||||
if wsym := ldr.WasmImportSym(fn); wsym != 0 {
|
||||
wi := readWasmImport(ldr, wsym)
|
||||
hostImportMap[fn] = int64(len(hostImports))
|
||||
hostImports = append(hostImports, &wasmFunc{
|
||||
Module: wi.Module,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue