| 1 |
// Copyright 2009 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package ssagen |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"internal/buildcfg" |
| 10 |
"log" |
| 11 |
"os" |
| 12 |
"strings" |
| 13 |
|
| 14 |
"cmd/compile/internal/abi" |
| 15 |
"cmd/compile/internal/base" |
| 16 |
"cmd/compile/internal/ir" |
| 17 |
"cmd/compile/internal/objw" |
| 18 |
"cmd/compile/internal/typecheck" |
| 19 |
"cmd/compile/internal/types" |
| 20 |
"cmd/internal/obj" |
| 21 |
"cmd/internal/obj/wasm" |
| 22 |
|
| 23 |
rtabi "internal/abi" |
| 24 |
) |
| 25 |
|
| 26 |
// SymABIs records information provided by the assembler about symbol |
| 27 |
// definition ABIs and reference ABIs. |
| 28 |
type SymABIs struct { |
| 29 |
defs map[string]obj.ABI |
| 30 |
refs map[string]obj.ABISet |
| 31 |
} |
| 32 |
|
| 33 |
func NewSymABIs() *SymABIs { |
| 34 |
return &SymABIs{ |
| 35 |
defs: make(map[string]obj.ABI), |
| 36 |
refs: make(map[string]obj.ABISet), |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// canonicalize returns the canonical name used for a linker symbol in |
| 41 |
// s's maps. Symbols in this package may be written either as "".X or |
| 42 |
// with the package's import path already in the symbol. This rewrites |
| 43 |
// both to use the full path, which matches compiler-generated linker |
| 44 |
// symbol names. |
| 45 |
func (s *SymABIs) canonicalize(linksym string) string { |
| 46 |
if strings.HasPrefix(linksym, `"".`) { |
| 47 |
panic("non-canonical symbol name: " + linksym) |
| 48 |
} |
| 49 |
return linksym |
| 50 |
} |
| 51 |
|
| 52 |
// ReadSymABIs reads a symabis file that specifies definitions and |
| 53 |
// references of text symbols by ABI. |
| 54 |
// |
| 55 |
// The symabis format is a set of lines, where each line is a sequence |
| 56 |
// of whitespace-separated fields. The first field is a verb and is |
| 57 |
// either "def" for defining a symbol ABI or "ref" for referencing a |
| 58 |
// symbol using an ABI. For both "def" and "ref", the second field is |
| 59 |
// the symbol name and the third field is the ABI name, as one of the |
| 60 |
// named cmd/internal/obj.ABI constants. |
| 61 |
func (s *SymABIs) ReadSymABIs(file string) { |
| 62 |
data, err := os.ReadFile(file) |
| 63 |
if err != nil { |
| 64 |
log.Fatalf("-symabis: %v", err) |
| 65 |
} |
| 66 |
|
| 67 |
for lineNum, line := range strings.Split(string(data), "\n") { |
| 68 |
lineNum++ // 1-based |
| 69 |
line = strings.TrimSpace(line) |
| 70 |
if line == "" || strings.HasPrefix(line, "#") { |
| 71 |
continue |
| 72 |
} |
| 73 |
|
| 74 |
parts := strings.Fields(line) |
| 75 |
switch parts[0] { |
| 76 |
case "def", "ref": |
| 77 |
// Parse line. |
| 78 |
if len(parts) != 3 { |
| 79 |
log.Fatalf(`%s:%d: invalid symabi: syntax is "%s sym abi"`, file, lineNum, parts[0]) |
| 80 |
} |
| 81 |
sym, abistr := parts[1], parts[2] |
| 82 |
abi, valid := obj.ParseABI(abistr) |
| 83 |
if !valid { |
| 84 |
log.Fatalf(`%s:%d: invalid symabi: unknown abi "%s"`, file, lineNum, abistr) |
| 85 |
} |
| 86 |
|
| 87 |
sym = s.canonicalize(sym) |
| 88 |
|
| 89 |
// Record for later. |
| 90 |
if parts[0] == "def" { |
| 91 |
s.defs[sym] = abi |
| 92 |
base.Ctxt.DwTextCount++ |
| 93 |
} else { |
| 94 |
s.refs[sym] |= obj.ABISetOf(abi) |
| 95 |
} |
| 96 |
default: |
| 97 |
log.Fatalf(`%s:%d: invalid symabi type "%s"`, file, lineNum, parts[0]) |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// GenABIWrappers applies ABI information to Funcs and generates ABI |
| 103 |
// wrapper functions where necessary. |
| 104 |
func (s *SymABIs) GenABIWrappers() { |
| 105 |
// For cgo exported symbols, we tell the linker to export the |
| 106 |
// definition ABI to C. That also means that we don't want to |
| 107 |
// create ABI wrappers even if there's a linkname. |
| 108 |
// |
| 109 |
// TODO(austin): Maybe we want to create the ABI wrappers, but |
| 110 |
// ensure the linker exports the right ABI definition under |
| 111 |
// the unmangled name? |
| 112 |
cgoExports := make(map[string][]*[]string) |
| 113 |
for i, prag := range typecheck.Target.CgoPragmas { |
| 114 |
switch prag[0] { |
| 115 |
case "cgo_export_static", "cgo_export_dynamic": |
| 116 |
symName := s.canonicalize(prag[1]) |
| 117 |
pprag := &typecheck.Target.CgoPragmas[i] |
| 118 |
cgoExports[symName] = append(cgoExports[symName], pprag) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// Apply ABI defs and refs to Funcs and generate wrappers. |
| 123 |
// |
| 124 |
// This may generate new decls for the wrappers, but we |
| 125 |
// specifically *don't* want to visit those, lest we create |
| 126 |
// wrappers for wrappers. |
| 127 |
for _, fn := range typecheck.Target.Funcs { |
| 128 |
nam := fn.Nname |
| 129 |
if ir.IsBlank(nam) { |
| 130 |
continue |
| 131 |
} |
| 132 |
sym := nam.Sym() |
| 133 |
|
| 134 |
symName := sym.Linkname |
| 135 |
if symName == "" { |
| 136 |
symName = sym.Pkg.Prefix + "." + sym.Name |
| 137 |
} |
| 138 |
symName = s.canonicalize(symName) |
| 139 |
|
| 140 |
// Apply definitions. |
| 141 |
defABI, hasDefABI := s.defs[symName] |
| 142 |
if hasDefABI { |
| 143 |
if len(fn.Body) != 0 { |
| 144 |
base.ErrorfAt(fn.Pos(), 0, "%v defined in both Go and assembly", fn) |
| 145 |
} |
| 146 |
fn.ABI = defABI |
| 147 |
} |
| 148 |
|
| 149 |
if fn.Pragma&ir.CgoUnsafeArgs != 0 { |
| 150 |
// CgoUnsafeArgs indicates the function (or its callee) uses |
| 151 |
// offsets to dispatch arguments, which currently using ABI0 |
| 152 |
// frame layout. Pin it to ABI0. |
| 153 |
fn.ABI = obj.ABI0 |
| 154 |
// Propagate linkname attribute, which was set on the ABIInternal |
| 155 |
// symbol. |
| 156 |
if sym.Linksym().IsLinkname() { |
| 157 |
sym.LinksymABI(fn.ABI).Set(obj.AttrLinkname, true) |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// If cgo-exported, add the definition ABI to the cgo |
| 162 |
// pragmas. |
| 163 |
cgoExport := cgoExports[symName] |
| 164 |
for _, pprag := range cgoExport { |
| 165 |
// The export pragmas have the form: |
| 166 |
// |
| 167 |
// cgo_export_* <local> [<remote>] |
| 168 |
// |
| 169 |
// If <remote> is omitted, it's the same as |
| 170 |
// <local>. |
| 171 |
// |
| 172 |
// Expand to |
| 173 |
// |
| 174 |
// cgo_export_* <local> <remote> <ABI> |
| 175 |
if len(*pprag) == 2 { |
| 176 |
*pprag = append(*pprag, (*pprag)[1]) |
| 177 |
} |
| 178 |
// Add the ABI argument. |
| 179 |
*pprag = append(*pprag, fn.ABI.String()) |
| 180 |
} |
| 181 |
|
| 182 |
// Apply references. |
| 183 |
if abis, ok := s.refs[symName]; ok { |
| 184 |
fn.ABIRefs |= abis |
| 185 |
} |
| 186 |
// Assume all functions are referenced at least as |
| 187 |
// ABIInternal, since they may be referenced from |
| 188 |
// other packages. |
| 189 |
fn.ABIRefs.Set(obj.ABIInternal, true) |
| 190 |
|
| 191 |
// If a symbol is defined in this package (either in |
| 192 |
// Go or assembly) and given a linkname, it may be |
| 193 |
// referenced from another package, so make it |
| 194 |
// callable via any ABI. It's important that we know |
| 195 |
// it's defined in this package since other packages |
| 196 |
// may "pull" symbols using linkname and we don't want |
| 197 |
// to create duplicate ABI wrappers. |
| 198 |
// |
| 199 |
// However, if it's given a linkname for exporting to |
| 200 |
// C, then we don't make ABI wrappers because the cgo |
| 201 |
// tool wants the original definition. |
| 202 |
hasBody := len(fn.Body) != 0 |
| 203 |
if sym.Linkname != "" && (hasBody || hasDefABI) && len(cgoExport) == 0 { |
| 204 |
fn.ABIRefs |= obj.ABISetCallable |
| 205 |
} |
| 206 |
|
| 207 |
// Double check that cgo-exported symbols don't get |
| 208 |
// any wrappers. |
| 209 |
if len(cgoExport) > 0 && fn.ABIRefs&^obj.ABISetOf(fn.ABI) != 0 { |
| 210 |
base.Fatalf("cgo exported function %v cannot have ABI wrappers", fn) |
| 211 |
} |
| 212 |
|
| 213 |
if !buildcfg.Experiment.RegabiWrappers { |
| 214 |
continue |
| 215 |
} |
| 216 |
|
| 217 |
forEachWrapperABI(fn, makeABIWrapper) |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
func forEachWrapperABI(fn *ir.Func, cb func(fn *ir.Func, wrapperABI obj.ABI)) { |
| 222 |
need := fn.ABIRefs &^ obj.ABISetOf(fn.ABI) |
| 223 |
if need == 0 { |
| 224 |
return |
| 225 |
} |
| 226 |
|
| 227 |
for wrapperABI := obj.ABI(0); wrapperABI < obj.ABICount; wrapperABI++ { |
| 228 |
if !need.Get(wrapperABI) { |
| 229 |
continue |
| 230 |
} |
| 231 |
cb(fn, wrapperABI) |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
// makeABIWrapper creates a new function that will be called with |
| 236 |
// wrapperABI and calls "f" using f.ABI. |
| 237 |
func makeABIWrapper(f *ir.Func, wrapperABI obj.ABI) { |
| 238 |
if base.Debug.ABIWrap != 0 { |
| 239 |
fmt.Fprintf(os.Stderr, "=-= %v to %v wrapper for %v\n", wrapperABI, f.ABI, f) |
| 240 |
} |
| 241 |
|
| 242 |
// Q: is this needed? |
| 243 |
savepos := base.Pos |
| 244 |
savedcurfn := ir.CurFunc |
| 245 |
|
| 246 |
pos := base.AutogeneratedPos |
| 247 |
base.Pos = pos |
| 248 |
|
| 249 |
// At the moment we don't support wrapping a method, we'd need machinery |
| 250 |
// below to handle the receiver. Panic if we see this scenario. |
| 251 |
ft := f.Nname.Type() |
| 252 |
if ft.NumRecvs() != 0 { |
| 253 |
base.ErrorfAt(f.Pos(), 0, "makeABIWrapper support for wrapping methods not implemented") |
| 254 |
return |
| 255 |
} |
| 256 |
|
| 257 |
// Reuse f's types.Sym to create a new ODCLFUNC/function. |
| 258 |
// TODO(mdempsky): Means we can't set sym.Def in Declfunc, ugh. |
| 259 |
fn := ir.NewFunc(pos, pos, f.Sym(), types.NewSignature(nil, |
| 260 |
typecheck.NewFuncParams(ft.Params()), |
| 261 |
typecheck.NewFuncParams(ft.Results()))) |
| 262 |
fn.ABI = wrapperABI |
| 263 |
typecheck.DeclFunc(fn) |
| 264 |
|
| 265 |
fn.SetABIWrapper(true) |
| 266 |
fn.SetDupok(true) |
| 267 |
|
| 268 |
// ABI0-to-ABIInternal wrappers will be mainly loading params from |
| 269 |
// stack into registers (and/or storing stack locations back to |
| 270 |
// registers after the wrapped call); in most cases they won't |
| 271 |
// need to allocate stack space, so it should be OK to mark them |
| 272 |
// as NOSPLIT in these cases. In addition, my assumption is that |
| 273 |
// functions written in assembly are NOSPLIT in most (but not all) |
| 274 |
// cases. In the case of an ABIInternal target that has too many |
| 275 |
// parameters to fit into registers, the wrapper would need to |
| 276 |
// allocate stack space, but this seems like an unlikely scenario. |
| 277 |
// Hence: mark these wrappers NOSPLIT. |
| 278 |
// |
| 279 |
// ABIInternal-to-ABI0 wrappers on the other hand will be taking |
| 280 |
// things in registers and pushing them onto the stack prior to |
| 281 |
// the ABI0 call, meaning that they will always need to allocate |
| 282 |
// stack space. If the compiler marks them as NOSPLIT this seems |
| 283 |
// as though it could lead to situations where the linker's |
| 284 |
// nosplit-overflow analysis would trigger a link failure. On the |
| 285 |
// other hand if they not tagged NOSPLIT then this could cause |
| 286 |
// problems when building the runtime (since there may be calls to |
| 287 |
// asm routine in cases where it's not safe to grow the stack). In |
| 288 |
// most cases the wrapper would be (in effect) inlined, but are |
| 289 |
// there (perhaps) indirect calls from the runtime that could run |
| 290 |
// into trouble here. |
| 291 |
// FIXME: at the moment all.bash does not pass when I leave out |
| 292 |
// NOSPLIT for these wrappers, so all are currently tagged with NOSPLIT. |
| 293 |
fn.Pragma |= ir.Nosplit |
| 294 |
|
| 295 |
// Generate call. Use tail call if no params and no returns, |
| 296 |
// but a regular call otherwise. |
| 297 |
// |
| 298 |
// Note: ideally we would be using a tail call in cases where |
| 299 |
// there are params but no returns for ABI0->ABIInternal wrappers, |
| 300 |
// provided that all params fit into registers (e.g. we don't have |
| 301 |
// to allocate any stack space). Doing this will require some |
| 302 |
// extra work in typecheck/walk/ssa, might want to add a new node |
| 303 |
// OTAILCALL or something to this effect. |
| 304 |
tailcall := fn.Type().NumResults() == 0 && fn.Type().NumParams() == 0 && fn.Type().NumRecvs() == 0 |
| 305 |
if base.Ctxt.Arch.Name == "ppc64le" && base.Ctxt.Flag_dynlink { |
| 306 |
// cannot tailcall on PPC64 with dynamic linking, as we need |
| 307 |
// to restore R2 after call. |
| 308 |
tailcall = false |
| 309 |
} |
| 310 |
if base.Ctxt.Arch.Name == "amd64" && wrapperABI == obj.ABIInternal { |
| 311 |
// cannot tailcall from ABIInternal to ABI0 on AMD64, as we need |
| 312 |
// to special registers (X15) when returning to ABIInternal. |
| 313 |
tailcall = false |
| 314 |
} |
| 315 |
|
| 316 |
var tail ir.Node |
| 317 |
call := ir.NewCallExpr(base.Pos, ir.OCALL, f.Nname, nil) |
| 318 |
call.Args = ir.ParamNames(fn.Type()) |
| 319 |
call.IsDDD = fn.Type().IsVariadic() |
| 320 |
tail = call |
| 321 |
if tailcall { |
| 322 |
tail = ir.NewTailCallStmt(base.Pos, call) |
| 323 |
} else if fn.Type().NumResults() > 0 { |
| 324 |
n := ir.NewReturnStmt(base.Pos, nil) |
| 325 |
n.Results = []ir.Node{call} |
| 326 |
tail = n |
| 327 |
} |
| 328 |
fn.Body.Append(tail) |
| 329 |
|
| 330 |
typecheck.FinishFuncBody() |
| 331 |
|
| 332 |
ir.CurFunc = fn |
| 333 |
typecheck.Stmts(fn.Body) |
| 334 |
|
| 335 |
// Restore previous context. |
| 336 |
base.Pos = savepos |
| 337 |
ir.CurFunc = savedcurfn |
| 338 |
} |
| 339 |
|
| 340 |
// CreateWasmImportWrapper creates a wrapper for imported WASM functions to |
| 341 |
// adapt them to the Go calling convention. The body for this function is |
| 342 |
// generated in cmd/internal/obj/wasm/wasmobj.go |
| 343 |
func CreateWasmImportWrapper(fn *ir.Func) bool { |
| 344 |
if fn.WasmImport == nil { |
| 345 |
return false |
| 346 |
} |
| 347 |
if buildcfg.GOARCH != "wasm" { |
| 348 |
base.FatalfAt(fn.Pos(), "CreateWasmImportWrapper call not supported on %s: func was %v", buildcfg.GOARCH, fn) |
| 349 |
} |
| 350 |
|
| 351 |
ir.InitLSym(fn, true) |
| 352 |
|
| 353 |
setupWasmImport(fn) |
| 354 |
|
| 355 |
pp := objw.NewProgs(fn, 0) |
| 356 |
defer pp.Free() |
| 357 |
pp.Text.To.Type = obj.TYPE_TEXTSIZE |
| 358 |
pp.Text.To.Val = int32(types.RoundUp(fn.Type().ArgWidth(), int64(types.RegSize))) |
| 359 |
// Wrapper functions never need their own stack frame |
| 360 |
pp.Text.To.Offset = 0 |
| 361 |
pp.Flush() |
| 362 |
|
| 363 |
return true |
| 364 |
} |
| 365 |
|
| 366 |
func GenWasmExportWrapper(wrapped *ir.Func) { |
| 367 |
if wrapped.WasmExport == nil { |
| 368 |
return |
| 369 |
} |
| 370 |
if buildcfg.GOARCH != "wasm" { |
| 371 |
base.FatalfAt(wrapped.Pos(), "GenWasmExportWrapper call not supported on %s: func was %v", buildcfg.GOARCH, wrapped) |
| 372 |
} |
| 373 |
|
| 374 |
pos := base.AutogeneratedPos |
| 375 |
sym := &types.Sym{ |
| 376 |
Name: wrapped.WasmExport.Name, |
| 377 |
Linkname: wrapped.WasmExport.Name, |
| 378 |
} |
| 379 |
ft := wrapped.Nname.Type() |
| 380 |
fn := ir.NewFunc(pos, pos, sym, types.NewSignature(nil, |
| 381 |
typecheck.NewFuncParams(ft.Params()), |
| 382 |
typecheck.NewFuncParams(ft.Results()))) |
| 383 |
fn.ABI = obj.ABI0 // actually wasm ABI |
| 384 |
// The wrapper function has a special calling convention that |
| 385 |
// morestack currently doesn't handle. For now we require that |
| 386 |
// the argument size fits in StackSmall, which we know we have |
| 387 |
// on stack, so we don't need to split stack. |
| 388 |
// cmd/internal/obj/wasm supports only 16 argument "registers" |
| 389 |
// anyway. |
| 390 |
if ft.ArgWidth() > rtabi.StackSmall { |
| 391 |
base.ErrorfAt(wrapped.Pos(), 0, "wasmexport function argument too large") |
| 392 |
} |
| 393 |
fn.Pragma |= ir.Nosplit |
| 394 |
|
| 395 |
ir.InitLSym(fn, true) |
| 396 |
|
| 397 |
setupWasmExport(fn, wrapped) |
| 398 |
|
| 399 |
pp := objw.NewProgs(fn, 0) |
| 400 |
defer pp.Free() |
| 401 |
// TEXT. Has a frame to pass args on stack to the Go function. |
| 402 |
pp.Text.To.Type = obj.TYPE_TEXTSIZE |
| 403 |
pp.Text.To.Val = int32(0) |
| 404 |
pp.Text.To.Offset = types.RoundUp(ft.ArgWidth(), int64(types.RegSize)) |
| 405 |
// No locals. (Callee's args are covered in the callee's stackmap.) |
| 406 |
p := pp.Prog(obj.AFUNCDATA) |
| 407 |
p.From.SetConst(rtabi.FUNCDATA_LocalsPointerMaps) |
| 408 |
p.To.Type = obj.TYPE_MEM |
| 409 |
p.To.Name = obj.NAME_EXTERN |
| 410 |
p.To.Sym = base.Ctxt.Lookup("no_pointers_stackmap") |
| 411 |
pp.Flush() |
| 412 |
// Actual code geneneration is in cmd/internal/obj/wasm. |
| 413 |
} |
| 414 |
|
| 415 |
func paramsToWasmFields(f *ir.Func, pragma string, result *abi.ABIParamResultInfo, abiParams []abi.ABIParamAssignment) []obj.WasmField { |
| 416 |
wfs := make([]obj.WasmField, 0, len(abiParams)) |
| 417 |
for _, p := range abiParams { |
| 418 |
t := p.Type |
| 419 |
var wt obj.WasmFieldType |
| 420 |
switch t.Kind() { |
| 421 |
case types.TINT32, types.TUINT32: |
| 422 |
wt = obj.WasmI32 |
| 423 |
case types.TINT64, types.TUINT64: |
| 424 |
wt = obj.WasmI64 |
| 425 |
case types.TFLOAT32: |
| 426 |
wt = obj.WasmF32 |
| 427 |
case types.TFLOAT64: |
| 428 |
wt = obj.WasmF64 |
| 429 |
case types.TUNSAFEPTR, types.TUINTPTR: |
| 430 |
wt = obj.WasmPtr |
| 431 |
case types.TBOOL: |
| 432 |
wt = obj.WasmBool |
| 433 |
case types.TSTRING: |
| 434 |
// Two parts, (ptr, len) |
| 435 |
wt = obj.WasmPtr |
| 436 |
wfs = append(wfs, obj.WasmField{Type: wt, Offset: p.FrameOffset(result)}) |
| 437 |
wfs = append(wfs, obj.WasmField{Type: wt, Offset: p.FrameOffset(result) + int64(types.PtrSize)}) |
| 438 |
continue |
| 439 |
case types.TPTR: |
| 440 |
if wasmElemTypeAllowed(t.Elem()) { |
| 441 |
wt = obj.WasmPtr |
| 442 |
break |
| 443 |
} |
| 444 |
fallthrough |
| 445 |
default: |
| 446 |
base.ErrorfAt(f.Pos(), 0, "%s: unsupported parameter type %s", pragma, t.String()) |
| 447 |
} |
| 448 |
wfs = append(wfs, obj.WasmField{Type: wt, Offset: p.FrameOffset(result)}) |
| 449 |
} |
| 450 |
return wfs |
| 451 |
} |
| 452 |
|
| 453 |
func resultsToWasmFields(f *ir.Func, pragma string, result *abi.ABIParamResultInfo, abiParams []abi.ABIParamAssignment) []obj.WasmField { |
| 454 |
if len(abiParams) > 1 { |
| 455 |
base.ErrorfAt(f.Pos(), 0, "%s: too many return values", pragma) |
| 456 |
return nil |
| 457 |
} |
| 458 |
wfs := make([]obj.WasmField, len(abiParams)) |
| 459 |
for i, p := range abiParams { |
| 460 |
t := p.Type |
| 461 |
switch t.Kind() { |
| 462 |
case types.TINT32, types.TUINT32: |
| 463 |
wfs[i].Type = obj.WasmI32 |
| 464 |
case types.TINT64, types.TUINT64: |
| 465 |
wfs[i].Type = obj.WasmI64 |
| 466 |
case types.TFLOAT32: |
| 467 |
wfs[i].Type = obj.WasmF32 |
| 468 |
case types.TFLOAT64: |
| 469 |
wfs[i].Type = obj.WasmF64 |
| 470 |
case types.TUNSAFEPTR, types.TUINTPTR: |
| 471 |
wfs[i].Type = obj.WasmPtr |
| 472 |
case types.TBOOL: |
| 473 |
wfs[i].Type = obj.WasmBool |
| 474 |
case types.TPTR: |
| 475 |
if wasmElemTypeAllowed(t.Elem()) { |
| 476 |
wfs[i].Type = obj.WasmPtr |
| 477 |
break |
| 478 |
} |
| 479 |
fallthrough |
| 480 |
default: |
| 481 |
base.ErrorfAt(f.Pos(), 0, "%s: unsupported result type %s", pragma, t.String()) |
| 482 |
} |
| 483 |
wfs[i].Offset = p.FrameOffset(result) |
| 484 |
} |
| 485 |
return wfs |
| 486 |
} |
| 487 |
|
| 488 |
// wasmElemTypeAllowed reports whether t is allowed to be passed in memory |
| 489 |
// (as a pointer's element type, a field of it, etc.) between the Go wasm |
| 490 |
// module and the host. |
| 491 |
func wasmElemTypeAllowed(t *types.Type) bool { |
| 492 |
switch t.Kind() { |
| 493 |
case types.TINT8, types.TUINT8, types.TINT16, types.TUINT16, |
| 494 |
types.TINT32, types.TUINT32, types.TINT64, types.TUINT64, |
| 495 |
types.TFLOAT32, types.TFLOAT64, types.TBOOL: |
| 496 |
return true |
| 497 |
case types.TARRAY: |
| 498 |
return wasmElemTypeAllowed(t.Elem()) |
| 499 |
case types.TSTRUCT: |
| 500 |
if len(t.Fields()) == 0 { |
| 501 |
return true |
| 502 |
} |
| 503 |
seenHostLayout := false |
| 504 |
for _, f := range t.Fields() { |
| 505 |
sym := f.Type.Sym() |
| 506 |
if sym != nil && sym.Name == "HostLayout" && sym.Pkg.Path == "structs" { |
| 507 |
seenHostLayout = true |
| 508 |
continue |
| 509 |
} |
| 510 |
if !wasmElemTypeAllowed(f.Type) { |
| 511 |
return false |
| 512 |
} |
| 513 |
} |
| 514 |
return seenHostLayout |
| 515 |
} |
| 516 |
// Pointer, and all pointerful types are not allowed, as pointers have |
| 517 |
// different width on the Go side and the host side. (It will be allowed |
| 518 |
// on GOARCH=wasm32.) |
| 519 |
return false |
| 520 |
} |
| 521 |
|
| 522 |
// setupWasmImport calculates the params and results in terms of WebAssembly values for the given function, |
| 523 |
// and sets up the wasmimport metadata. |
| 524 |
func setupWasmImport(f *ir.Func) { |
| 525 |
wi := obj.WasmImport{ |
| 526 |
Module: f.WasmImport.Module, |
| 527 |
Name: f.WasmImport.Name, |
| 528 |
} |
| 529 |
if wi.Module == wasm.GojsModule { |
| 530 |
// Functions that are imported from the "gojs" module use a special |
| 531 |
// ABI that just accepts the stack pointer. |
| 532 |
// Example: |
| 533 |
// |
| 534 |
// //go:wasmimport gojs add |
| 535 |
// func importedAdd(a, b uint) uint |
| 536 |
// |
| 537 |
// will roughly become |
| 538 |
// |
| 539 |
// (import "gojs" "add" (func (param i32))) |
| 540 |
wi.Params = []obj.WasmField{{Type: obj.WasmI32}} |
| 541 |
} else { |
| 542 |
// All other imported functions use the normal WASM ABI. |
| 543 |
// Example: |
| 544 |
// |
| 545 |
// //go:wasmimport a_module add |
| 546 |
// func importedAdd(a, b uint) uint |
| 547 |
// |
| 548 |
// will roughly become |
| 549 |
// |
| 550 |
// (import "a_module" "add" (func (param i32 i32) (result i32))) |
| 551 |
abiConfig := AbiForBodylessFuncStackMap(f) |
| 552 |
abiInfo := abiConfig.ABIAnalyzeFuncType(f.Type()) |
| 553 |
wi.Params = paramsToWasmFields(f, "go:wasmimport", abiInfo, abiInfo.InParams()) |
| 554 |
wi.Results = resultsToWasmFields(f, "go:wasmimport", abiInfo, abiInfo.OutParams()) |
| 555 |
} |
| 556 |
f.LSym.Func().WasmImport = &wi |
| 557 |
} |
| 558 |
|
| 559 |
// setupWasmExport calculates the params and results in terms of WebAssembly values for the given function, |
| 560 |
// and sets up the wasmexport metadata. |
| 561 |
func setupWasmExport(f, wrapped *ir.Func) { |
| 562 |
we := obj.WasmExport{ |
| 563 |
WrappedSym: wrapped.LSym, |
| 564 |
} |
| 565 |
abiConfig := AbiForBodylessFuncStackMap(wrapped) |
| 566 |
abiInfo := abiConfig.ABIAnalyzeFuncType(wrapped.Type()) |
| 567 |
we.Params = paramsToWasmFields(wrapped, "go:wasmexport", abiInfo, abiInfo.InParams()) |
| 568 |
we.Results = resultsToWasmFields(wrapped, "go:wasmexport", abiInfo, abiInfo.OutParams()) |
| 569 |
f.LSym.Func().WasmExport = &we |
| 570 |
} |