]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/objabi: generalize "is runtime package" check
authorAustin Clements <austin@google.com>
Fri, 30 Jun 2023 21:11:31 +0000 (17:11 -0400)
committerAustin Clements <austin@google.com>
Tue, 22 Aug 2023 19:18:26 +0000 (19:18 +0000)
There are several implementations of "is this package path a runtime
package". They all have slightly different lists because they all care
about slightly different properties of building the runtime.

To start converging these, we replace objabi.IsRuntimePackagePath with
objabi.LookupPkgSpecial, which returns a struct we can extend with
various special build properties. We'll extend this with several other
flags in the following CLs.

Change-Id: I21959cb8c3d18a350d6060467681c72ea49af712
Reviewed-on: https://go-review.googlesource.com/c/go/+/521698
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Austin Clements <austin@google.com>

src/cmd/asm/main.go
src/cmd/internal/objabi/path.go
src/cmd/internal/objabi/pkgspecial.go [new file with mode: 0644]

index e75aa8664bcb70733b273b055e429c768069425b..84e9388cefbf8a09b05d4a99d4f71d473c5f5913 100644 (file)
@@ -35,7 +35,7 @@ func main() {
        if architecture == nil {
                log.Fatalf("unrecognized architecture %s", GOARCH)
        }
-       compilingRuntime := objabi.IsRuntimePackagePath(*flags.Importpath)
+       compilingRuntime := objabi.LookupPkgSpecial(*flags.Importpath).AllowAsmABI
 
        ctxt := obj.Linknew(architecture.LinkArch)
        ctxt.Debugasm = flags.PrintOut
index cb06dbe58e2c9179b0a935ba3224c6a15bb3e6ee..2a42179a3686b20e0b757d6cd52e02967d9dff69 100644 (file)
@@ -39,27 +39,3 @@ func PathToPrefix(s string) string {
 
        return string(p)
 }
-
-// IsRuntimePackagePath examines 'pkgpath' and returns TRUE if it
-// belongs to the collection of "runtime-related" packages, including
-// "runtime" itself, "reflect", "syscall", and the
-// "runtime/internal/*" packages. The compiler and/or assembler in
-// some cases need to be aware of when they are building such a
-// package, for example to enable features such as ABI selectors in
-// assembly sources.
-func IsRuntimePackagePath(pkgpath string) bool {
-       rval := false
-       switch pkgpath {
-       case "runtime":
-               rval = true
-       case "reflect":
-               rval = true
-       case "syscall":
-               rval = true
-       case "internal/bytealg":
-               rval = true
-       default:
-               rval = strings.HasPrefix(pkgpath, "runtime/internal")
-       }
-       return rval
-}
diff --git a/src/cmd/internal/objabi/pkgspecial.go b/src/cmd/internal/objabi/pkgspecial.go
new file mode 100644 (file)
index 0000000..ac38c1b
--- /dev/null
@@ -0,0 +1,50 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package objabi
+
+import "sync"
+
+// PkgSpecial indicates special build properties of a given runtime-related
+// package.
+type PkgSpecial struct {
+       // AllowAsmABI indicates that assembly in this package is allowed to use ABI
+       // selectors in symbol names. Generally this is needed for packages that
+       // interact closely with the runtime package or have performance-critical
+       // assembly.
+       AllowAsmABI bool
+}
+
+var allowAsmABIPkgs = []string{
+       "runtime",
+       "reflect",
+       "syscall",
+       "internal/bytealg",
+       "runtime/internal/syscall",
+       "runtime/internal/startlinetest",
+}
+
+var (
+       pkgSpecials     map[string]PkgSpecial
+       pkgSpecialsOnce sync.Once
+)
+
+// LookupPkgSpecial returns special build properties for the given package path.
+func LookupPkgSpecial(pkgPath string) PkgSpecial {
+       pkgSpecialsOnce.Do(func() {
+               // Construct pkgSpecials from various package lists. This lets us use
+               // more flexible logic, while keeping the final map simple, and avoids
+               // the init-time cost of a map.
+               pkgSpecials = make(map[string]PkgSpecial)
+               set := func(elt string, f func(*PkgSpecial)) {
+                       s := pkgSpecials[elt]
+                       f(&s)
+                       pkgSpecials[elt] = s
+               }
+               for _, pkg := range allowAsmABIPkgs {
+                       set(pkg, func(ps *PkgSpecial) { ps.AllowAsmABI = true })
+               }
+       })
+       return pkgSpecials[pkgPath]
+}