| |
| |
| |
|
|
| package ssagen |
|
|
| import ( |
| "fmt" |
| "internal/buildcfg" |
| "log" |
| "os" |
| "strings" |
|
|
| "cmd/compile/internal/abi" |
| "cmd/compile/internal/base" |
| "cmd/compile/internal/ir" |
| "cmd/compile/internal/objw" |
| "cmd/compile/internal/typecheck" |
| "cmd/compile/internal/types" |
| "cmd/internal/obj" |
| "cmd/internal/obj/wasm" |
|
|
| rtabi "internal/abi" |
| ) |
|
|
| |
| |
| type SymABIs struct { |
| defs map[string]obj.ABI |
| refs map[string]obj.ABISet |
| } |
|
|
| func NewSymABIs() *SymABIs { |
| return &SymABIs{ |
| defs: make(map[string]obj.ABI), |
| refs: make(map[string]obj.ABISet), |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| func (s *SymABIs) canonicalize(linksym string) string { |
| if strings.HasPrefix(linksym, `"".`) { |
| panic("non-canonical symbol name: " + linksym) |
| } |
| return linksym |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func (s *SymABIs) ReadSymABIs(file string) { |
| data, err := os.ReadFile(file) |
| if err != nil { |
| log.Fatalf("-symabis: %v", err) |
| } |
|
|
| for lineNum, line := range strings.Split(string(data), "\n") { |
| lineNum++ |
| line = strings.TrimSpace(line) |
| if line == "" || strings.HasPrefix(line, "#") { |
| continue |
| } |
|
|
| parts := strings.Fields(line) |
| switch parts[0] { |
| case "def", "ref": |
| |
| if len(parts) != 3 { |
| log.Fatalf(`%s:%d: invalid symabi: syntax is "%s sym abi"`, file, lineNum, parts[0]) |
| } |
| sym, abistr := parts[1], parts[2] |
| abi, valid := obj.ParseABI(abistr) |
| if !valid { |
| log.Fatalf(`%s:%d: invalid symabi: unknown abi "%s"`, file, lineNum, abistr) |
| } |
|
|
| sym = s.canonicalize(sym) |
|
|
| |
| if parts[0] == "def" { |
| s.defs[sym] = abi |
| base.Ctxt.DwTextCount++ |
| } else { |
| s.refs[sym] |= obj.ABISetOf(abi) |
| } |
| default: |
| log.Fatalf(`%s:%d: invalid symabi type "%s"`, file, lineNum, parts[0]) |
| } |
| } |
| } |
|
|
| |
| func (s *SymABIs) HasDef(sym *types.Sym) bool { |
| symName := sym.Linkname |
| if symName == "" { |
| symName = sym.Pkg.Prefix + "." + sym.Name |
| } |
| symName = s.canonicalize(symName) |
|
|
| _, hasDefABI := s.defs[symName] |
| return hasDefABI |
| } |
|
|
| |
| |
| func (s *SymABIs) GenABIWrappers() { |
| |
| |
| |
| |
| |
| |
| |
| cgoExports := make(map[string][]*[]string) |
| for i, prag := range typecheck.Target.CgoPragmas { |
| switch prag[0] { |
| case "cgo_export_static", "cgo_export_dynamic": |
| symName := s.canonicalize(prag[1]) |
| pprag := &typecheck.Target.CgoPragmas[i] |
| cgoExports[symName] = append(cgoExports[symName], pprag) |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| for _, fn := range typecheck.Target.Funcs { |
| nam := fn.Nname |
| if ir.IsBlank(nam) { |
| continue |
| } |
| sym := nam.Sym() |
|
|
| symName := sym.Linkname |
| if symName == "" { |
| symName = sym.Pkg.Prefix + "." + sym.Name |
| } |
| symName = s.canonicalize(symName) |
|
|
| |
| defABI, hasDefABI := s.defs[symName] |
| if hasDefABI { |
| if len(fn.Body) != 0 { |
| base.ErrorfAt(fn.Pos(), 0, "%v defined in both Go and assembly", fn) |
| } |
| fn.ABI = defABI |
| } |
|
|
| if fn.Pragma&ir.CgoUnsafeArgs != 0 { |
| |
| |
| |
| fn.ABI = obj.ABI0 |
| |
| |
| if sym.Linksym().IsLinkname() { |
| sym.LinksymABI(fn.ABI).Set(obj.AttrLinkname, true) |
| } |
| } |
|
|
| |
| |
| cgoExport := cgoExports[symName] |
| for _, pprag := range cgoExport { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if len(*pprag) == 2 { |
| *pprag = append(*pprag, (*pprag)[1]) |
| } |
| |
| *pprag = append(*pprag, fn.ABI.String()) |
| } |
|
|
| |
| if abis, ok := s.refs[symName]; ok { |
| fn.ABIRefs |= abis |
| } |
| |
| |
| |
| fn.ABIRefs.Set(obj.ABIInternal, true) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| hasBody := len(fn.Body) != 0 |
| if sym.Linkname != "" && (hasBody || hasDefABI) && len(cgoExport) == 0 { |
| fn.ABIRefs |= obj.ABISetCallable |
| } |
|
|
| |
| |
| if len(cgoExport) > 0 && fn.ABIRefs&^obj.ABISetOf(fn.ABI) != 0 { |
| base.Fatalf("cgo exported function %v cannot have ABI wrappers", fn) |
| } |
|
|
| if !buildcfg.Experiment.RegabiWrappers { |
| continue |
| } |
|
|
| forEachWrapperABI(fn, makeABIWrapper) |
| } |
| } |
|
|
| func forEachWrapperABI(fn *ir.Func, cb func(fn *ir.Func, wrapperABI obj.ABI)) { |
| need := fn.ABIRefs &^ obj.ABISetOf(fn.ABI) |
| if need == 0 { |
| return |
| } |
|
|
| for wrapperABI := obj.ABI(0); wrapperABI < obj.ABICount; wrapperABI++ { |
| if !need.Get(wrapperABI) { |
| continue |
| } |
| cb(fn, wrapperABI) |
| } |
| } |
|
|
| |
| |
| func makeABIWrapper(f *ir.Func, wrapperABI obj.ABI) { |
| if base.Debug.ABIWrap != 0 { |
| fmt.Fprintf(os.Stderr, "=-= %v to %v wrapper for %v\n", wrapperABI, f.ABI, f) |
| } |
|
|
| |
| savepos := base.Pos |
| savedcurfn := ir.CurFunc |
|
|
| pos := base.AutogeneratedPos |
| base.Pos = pos |
|
|
| |
| |
| ft := f.Nname.Type() |
| if ft.NumRecvs() != 0 { |
| base.ErrorfAt(f.Pos(), 0, "makeABIWrapper support for wrapping methods not implemented") |
| return |
| } |
|
|
| |
| |
| fn := ir.NewFunc(pos, pos, f.Sym(), types.NewSignature(nil, |
| typecheck.NewFuncParams(ft.Params()), |
| typecheck.NewFuncParams(ft.Results()))) |
| fn.ABI = wrapperABI |
| typecheck.DeclFunc(fn) |
|
|
| fn.SetABIWrapper(true) |
| fn.SetDupok(true) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| fn.Pragma |= ir.Nosplit |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| tailcall := fn.Type().NumResults() == 0 && fn.Type().NumParams() == 0 && fn.Type().NumRecvs() == 0 |
| if base.Ctxt.Arch.Name == "ppc64le" && base.Ctxt.Flag_dynlink { |
| |
| |
| tailcall = false |
| } |
| if base.Ctxt.Arch.Name == "amd64" && wrapperABI == obj.ABIInternal { |
| |
| |
| tailcall = false |
| } |
|
|
| var tail ir.Node |
| call := ir.NewCallExpr(base.Pos, ir.OCALL, f.Nname, nil) |
| call.Args = ir.ParamNames(fn.Type()) |
| call.IsDDD = fn.Type().IsVariadic() |
| tail = call |
| if tailcall { |
| tail = ir.NewTailCallStmt(base.Pos, call) |
| } else if fn.Type().NumResults() > 0 { |
| n := ir.NewReturnStmt(base.Pos, nil) |
| n.Results = []ir.Node{call} |
| tail = n |
| } |
| fn.Body.Append(tail) |
|
|
| typecheck.FinishFuncBody() |
|
|
| ir.CurFunc = fn |
| typecheck.Stmts(fn.Body) |
|
|
| |
| base.Pos = savepos |
| ir.CurFunc = savedcurfn |
| } |
|
|
| |
| |
| |
| func CreateWasmImportWrapper(fn *ir.Func) bool { |
| if fn.WasmImport == nil { |
| return false |
| } |
| if buildcfg.GOARCH != "wasm" { |
| base.FatalfAt(fn.Pos(), "CreateWasmImportWrapper call not supported on %s: func was %v", buildcfg.GOARCH, fn) |
| } |
|
|
| ir.InitLSym(fn, true) |
|
|
| setupWasmImport(fn) |
|
|
| pp := objw.NewProgs(fn, 0) |
| defer pp.Free() |
| pp.Text.To.Type = obj.TYPE_TEXTSIZE |
| pp.Text.To.Val = int32(types.RoundUp(fn.Type().ArgWidth(), int64(types.RegSize))) |
| |
| pp.Text.To.Offset = 0 |
| pp.Flush() |
|
|
| return true |
| } |
|
|
| func GenWasmExportWrapper(wrapped *ir.Func) { |
| if wrapped.WasmExport == nil { |
| return |
| } |
| if buildcfg.GOARCH != "wasm" { |
| base.FatalfAt(wrapped.Pos(), "GenWasmExportWrapper call not supported on %s: func was %v", buildcfg.GOARCH, wrapped) |
| } |
|
|
| pos := base.AutogeneratedPos |
| sym := &types.Sym{ |
| Name: wrapped.WasmExport.Name, |
| Linkname: wrapped.WasmExport.Name, |
| } |
| ft := wrapped.Nname.Type() |
| fn := ir.NewFunc(pos, pos, sym, types.NewSignature(nil, |
| typecheck.NewFuncParams(ft.Params()), |
| typecheck.NewFuncParams(ft.Results()))) |
| fn.ABI = obj.ABI0 |
| |
| |
| |
| |
| |
| |
| if ft.ArgWidth() > rtabi.StackSmall { |
| base.ErrorfAt(wrapped.Pos(), 0, "wasmexport function argument too large") |
| } |
| fn.Pragma |= ir.Nosplit |
|
|
| ir.InitLSym(fn, true) |
|
|
| setupWasmExport(fn, wrapped) |
|
|
| pp := objw.NewProgs(fn, 0) |
| defer pp.Free() |
| |
| pp.Text.To.Type = obj.TYPE_TEXTSIZE |
| pp.Text.To.Val = int32(0) |
| pp.Text.To.Offset = types.RoundUp(ft.ArgWidth(), int64(types.RegSize)) |
| |
| p := pp.Prog(obj.AFUNCDATA) |
| p.From.SetConst(rtabi.FUNCDATA_LocalsPointerMaps) |
| p.To.Type = obj.TYPE_MEM |
| p.To.Name = obj.NAME_EXTERN |
| p.To.Sym = base.Ctxt.Lookup("no_pointers_stackmap") |
| pp.Flush() |
| |
| } |
|
|
| func paramsToWasmFields(f *ir.Func, pragma string, result *abi.ABIParamResultInfo, abiParams []abi.ABIParamAssignment) []obj.WasmField { |
| wfs := make([]obj.WasmField, 0, len(abiParams)) |
| for _, p := range abiParams { |
| t := p.Type |
| var wt obj.WasmFieldType |
| switch t.Kind() { |
| case types.TINT32, types.TUINT32: |
| wt = obj.WasmI32 |
| case types.TINT64, types.TUINT64: |
| wt = obj.WasmI64 |
| case types.TFLOAT32: |
| wt = obj.WasmF32 |
| case types.TFLOAT64: |
| wt = obj.WasmF64 |
| case types.TUNSAFEPTR, types.TUINTPTR: |
| wt = obj.WasmPtr |
| case types.TBOOL: |
| wt = obj.WasmBool |
| case types.TSTRING: |
| |
| wt = obj.WasmPtr |
| wfs = append(wfs, obj.WasmField{Type: wt, Offset: p.FrameOffset(result)}) |
| wfs = append(wfs, obj.WasmField{Type: wt, Offset: p.FrameOffset(result) + int64(types.PtrSize)}) |
| continue |
| case types.TPTR: |
| if wasmElemTypeAllowed(t.Elem()) { |
| wt = obj.WasmPtr |
| break |
| } |
| fallthrough |
| default: |
| base.ErrorfAt(f.Pos(), 0, "%s: unsupported parameter type %s", pragma, t.String()) |
| } |
| wfs = append(wfs, obj.WasmField{Type: wt, Offset: p.FrameOffset(result)}) |
| } |
| return wfs |
| } |
|
|
| func resultsToWasmFields(f *ir.Func, pragma string, result *abi.ABIParamResultInfo, abiParams []abi.ABIParamAssignment) []obj.WasmField { |
| if len(abiParams) > 1 { |
| base.ErrorfAt(f.Pos(), 0, "%s: too many return values", pragma) |
| return nil |
| } |
| wfs := make([]obj.WasmField, len(abiParams)) |
| for i, p := range abiParams { |
| t := p.Type |
| switch t.Kind() { |
| case types.TINT32, types.TUINT32: |
| wfs[i].Type = obj.WasmI32 |
| case types.TINT64, types.TUINT64: |
| wfs[i].Type = obj.WasmI64 |
| case types.TFLOAT32: |
| wfs[i].Type = obj.WasmF32 |
| case types.TFLOAT64: |
| wfs[i].Type = obj.WasmF64 |
| case types.TUNSAFEPTR, types.TUINTPTR: |
| wfs[i].Type = obj.WasmPtr |
| case types.TBOOL: |
| wfs[i].Type = obj.WasmBool |
| case types.TPTR: |
| if wasmElemTypeAllowed(t.Elem()) { |
| wfs[i].Type = obj.WasmPtr |
| break |
| } |
| fallthrough |
| default: |
| base.ErrorfAt(f.Pos(), 0, "%s: unsupported result type %s", pragma, t.String()) |
| } |
| wfs[i].Offset = p.FrameOffset(result) |
| } |
| return wfs |
| } |
|
|
| |
| |
| |
| func wasmElemTypeAllowed(t *types.Type) bool { |
| switch t.Kind() { |
| case types.TINT8, types.TUINT8, types.TINT16, types.TUINT16, |
| types.TINT32, types.TUINT32, types.TINT64, types.TUINT64, |
| types.TFLOAT32, types.TFLOAT64, types.TBOOL: |
| return true |
| case types.TARRAY: |
| return wasmElemTypeAllowed(t.Elem()) |
| case types.TSTRUCT: |
| if len(t.Fields()) == 0 { |
| return true |
| } |
| seenHostLayout := false |
| for _, f := range t.Fields() { |
| sym := f.Type.Sym() |
| if sym != nil && sym.Name == "HostLayout" && sym.Pkg.Path == "structs" { |
| seenHostLayout = true |
| continue |
| } |
| if !wasmElemTypeAllowed(f.Type) { |
| return false |
| } |
| } |
| return seenHostLayout |
| } |
| |
| |
| |
| return false |
| } |
|
|
| |
| |
| func setupWasmImport(f *ir.Func) { |
| wi := obj.WasmImport{ |
| Module: f.WasmImport.Module, |
| Name: f.WasmImport.Name, |
| } |
| if wi.Module == wasm.GojsModule { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| wi.Params = []obj.WasmField{{Type: obj.WasmI32}} |
| } else { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| abiConfig := AbiForBodylessFuncStackMap(f) |
| abiInfo := abiConfig.ABIAnalyzeFuncType(f.Type()) |
| wi.Params = paramsToWasmFields(f, "go:wasmimport", abiInfo, abiInfo.InParams()) |
| wi.Results = resultsToWasmFields(f, "go:wasmimport", abiInfo, abiInfo.OutParams()) |
| } |
| f.LSym.Func().WasmImport = &wi |
| } |
|
|
| |
| |
| func setupWasmExport(f, wrapped *ir.Func) { |
| we := obj.WasmExport{ |
| WrappedSym: wrapped.LSym, |
| } |
| abiConfig := AbiForBodylessFuncStackMap(wrapped) |
| abiInfo := abiConfig.ABIAnalyzeFuncType(wrapped.Type()) |
| we.Params = paramsToWasmFields(wrapped, "go:wasmexport", abiInfo, abiInfo.InParams()) |
| we.Results = resultsToWasmFields(wrapped, "go:wasmexport", abiInfo, abiInfo.OutParams()) |
| f.LSym.Func().WasmExport = &we |
| } |
|
|