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