]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/obj: add tool to generate Cnames string
authorchenguoqi <chenguoqi@loongson.cn>
Thu, 24 Oct 2024 03:16:00 +0000 (11:16 +0800)
committerGopher Robot <gobot@golang.org>
Wed, 13 Nov 2024 05:02:13 +0000 (05:02 +0000)
Add cmd/internal/obj/mkcnames.go to do the generation and update
the architecture packages to use it to maintain the Cnames tables.

Currently works correctly on arm64,loong64,mips,ppc64 and s390x.

Change-Id: I5220b0ba6d8a8a5fcc4d9774731eb2af69a671af
Reviewed-on: https://go-review.googlesource.com/c/go/+/622256
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Meidan Li <limeidan@loongson.cn>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Qiqi Huang <huangqiqi@loongson.cn>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: sophie zhao <zhaoxiaolin@loongson.cn>
Commit-Queue: Ian Lance Taylor <iant@golang.org>

src/cmd/internal/obj/arm64/a.out.go
src/cmd/internal/obj/arm64/anames7.go
src/cmd/internal/obj/loong64/a.out.go
src/cmd/internal/obj/loong64/cnames.go
src/cmd/internal/obj/mips/a.out.go
src/cmd/internal/obj/mips/anames0.go
src/cmd/internal/obj/mkcnames.go [new file with mode: 0644]
src/cmd/internal/obj/ppc64/a.out.go
src/cmd/internal/obj/ppc64/anames9.go
src/cmd/internal/obj/s390x/a.out.go
src/cmd/internal/obj/s390x/anamesz.go

index 39b9f164b9380f006285643b55bb2b40187ba62f..ad00e4842cdfc64509d0c1b8ea57d960fc79b58c 100644 (file)
@@ -337,6 +337,7 @@ const (
        NOSCHED
 )
 
+//go:generate go run ../mkcnames.go -i a.out.go -o anames7.go -p arm64
 const (
        // optab is sorted based on the order of these constants
        // and the first match is chosen.
index 5f2e3a6f74489d6539edb0ef6354122cbc72fbe1..bf06ac3db29b8c11c07e72ffcc9ccb921d30d7cd 100644 (file)
@@ -1,10 +1,8 @@
-// Copyright 2015 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.
+// Code generated by mkcnames -i a.out.go -o anames7.go -p arm64; DO NOT EDIT.
 
 package arm64
 
-// This order should be strictly consistent to that in a.out.go
+// This order should be strictly consistent to that in a.out.go.
 var cnames7 = []string{
        "", // C_NONE starts from 1
        "NONE",
@@ -17,8 +15,8 @@ var cnames7 = []string{
        "SHIFT",
        "EXTREG",
        "SPR",
-       "SPOP",
        "COND",
+       "SPOP",
        "ARNG",
        "ELEM",
        "LIST",
@@ -72,7 +70,7 @@ var cnames7 = []string{
        "UAUTO16K_16",
        "UAUTO16K_8",
        "UAUTO16K",
-       "UAUTO32K_8",
+       "UAUTO32K_16",
        "UAUTO32K",
        "UAUTO64K",
        "LAUTOPOOL",
index efd5b1b3cc0357393e218f556a3571e315d0b474..e6984dcba794cf561a5274b635132f14413f9318 100644 (file)
@@ -316,6 +316,7 @@ const (
        REG_LAST = REG_ELEM_END // the last defined register
 )
 
+//go:generate go run ../mkcnames.go -i a.out.go -o cnames.go -p loong64
 const (
        C_NONE = iota
        C_REG
index 6e7101860af374331b804efa8409bf20627e490f..ce76109d2a18eed908753914f9f73b9deba58080 100644 (file)
@@ -1,6 +1,4 @@
-// Copyright 2022 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.
+// Code generated by mkcnames -i a.out.go -o cnames.go -p loong64; DO NOT EDIT.
 
 package loong64
 
index cd6131332ac834723f40257f36a7c8dbfba2d7c8..5439f0e4aaf1f456bf68234fbbfd96a78a8dd5f5 100644 (file)
@@ -275,6 +275,7 @@ const (
        NSCHED = 20
 )
 
+//go:generate go run ../mkcnames.go -i a.out.go -o anames0.go -p mips
 const (
        C_NONE = iota
        C_REG
index c30069673092bade5d465f92cc3101a0eb074125..ec07492375fb4938a2d4644e4a6acb1ff03a47f0 100644 (file)
@@ -1,9 +1,8 @@
-// Copyright 2015 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.
+// Code generated by mkcnames -i a.out.go -o anames0.go -p mips; DO NOT EDIT.
 
 package mips
 
+// This order should be strictly consistent to that in a.out.go.
 var cnames0 = []string{
        "NONE",
        "REG",
diff --git a/src/cmd/internal/obj/mkcnames.go b/src/cmd/internal/obj/mkcnames.go
new file mode 100644 (file)
index 0000000..72b2730
--- /dev/null
@@ -0,0 +1,136 @@
+// Copyright 2024 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.
+
+//go:build ignore
+
+// This is based on the implementation of src/cmd/internal/obj/stringer.go.
+// This is a mini version of the stringer tool customized for the Cnames
+// table in the architecture support for obj.
+
+package main
+
+import (
+       "bufio"
+       "flag"
+       "fmt"
+       "log"
+       "os"
+       "regexp"
+       "strings"
+)
+
+var (
+       input  = flag.String("i", "", "input file name")
+       output = flag.String("o", "", "output file name")
+       pkg    = flag.String("p", "", "package name")
+)
+
+var cnameExp = regexp.MustCompile(`^\tC_([A-Za-z0-9_]+)`)
+
+func main() {
+       flag.Parse()
+       if *input == "" || *output == "" || *pkg == "" {
+               flag.Usage()
+               os.Exit(2)
+       }
+
+       start := ""
+       switch *pkg {
+       case "arm64":
+               start = "var cnames7 = []string{\n\t\"\", // C_NONE starts from 1\n"
+       case "loong64", "mips":
+               start = "var cnames0 = []string{\n"
+       case "ppc64":
+               start = "var cnames9 = []string{\n"
+       case "s390x":
+               start = "var cnamesz = []string{\n"
+       default:
+               fmt.Printf("Only supports generating Cnames for arm64,loong64,mips,ppc64,s390x.")
+               os.Exit(0)
+       }
+
+       in, err := os.Open(*input)
+       if err != nil {
+               log.Fatal(err)
+       }
+       fd, err := os.Create(*output)
+       if err != nil {
+               log.Fatal(err)
+       }
+       out := bufio.NewWriter(fd)
+       closeOut := func() {
+               if err = out.Flush(); err != nil {
+                       log.Fatal(err)
+               }
+
+               if err = fd.Close(); err != nil {
+                       log.Fatal(err)
+               }
+       }
+       defer closeOut()
+
+       on := false
+       s := bufio.NewScanner(in)
+       for s.Scan() {
+               line := s.Text()
+               if !on {
+                       // First relevant line contains "C_NONE = iota".
+                       // If we find it, delete the "=" so we don't stop immediately.
+                       const first = "C_NONE"
+                       if !strings.Contains(line, first) {
+                               continue
+                       }
+
+                       const suffix = "= iota"
+                       index := strings.Index(line, suffix)
+                       if index < 0 {
+                               continue
+                       }
+                       line = line[:index]
+
+                       // It's on. Start with the header.
+                       fmt.Fprintf(out, header, *input, *output, *pkg, *pkg)
+                       fmt.Fprintf(out, start)
+                       on = true
+               }
+
+               // Strip comments so their text won't defeat our heuristic.
+               index := strings.Index(line, "//")
+               if index > 0 {
+                       line = line[:index]
+               }
+               index = strings.Index(line, "/*")
+               if index > 0 {
+                       comments := line[index:]
+                       if !strings.Contains(comments, "*/") {
+                               log.Fatalf("invalid comment: %s\n", comments)
+                       }
+                       line = line[:index]
+               }
+
+               // Termination condition: Any line with an = changes the sequence,
+               // so stop there, and stop at a closing brace.
+               if strings.HasPrefix(line, "}") || strings.ContainsRune(line, '=') {
+                       break
+               }
+
+               sub := cnameExp.FindStringSubmatch(line)
+               if len(sub) < 2 {
+                       continue
+               } else {
+                       fmt.Fprintf(out, "\t%q,\n", sub[1])
+               }
+       }
+       fmt.Fprintln(out, "}")
+       if s.Err() != nil {
+               log.Fatal(err)
+       }
+}
+
+const header = `// Code generated by mkcnames -i %s -o %s -p %s; DO NOT EDIT.
+
+package %s
+
+// This order should be strictly consistent to that in a.out.go.
+`
index 137c6d2ff21ae6182cfc5b631790712777d9e267..0c980ef4764749d0417dd8797fb13b6f14a064d8 100644 (file)
@@ -399,6 +399,7 @@ const (
        C_COND_SO        // 3 summary overflow or FP compare w/ NaN
 )
 
+//go:generate go run ../mkcnames.go -i a.out.go -o anames9.go -p ppc64
 const (
        C_NONE     = iota
        C_REGP     /* An even numbered gpr which can be used a gpr pair argument */
index 81f73dcea6ee13bdd5be36bda39399d9e58d5c58..c42125984cdfa6058e69f37e7fa0f373d6c8175a 100644 (file)
@@ -1,9 +1,8 @@
-// Copyright 2015 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.
+// Code generated by mkcnames -i a.out.go -o anames9.go -p ppc64; DO NOT EDIT.
 
 package ppc64
 
+// This order should be strictly consistent to that in a.out.go.
 var cnames9 = []string{
        "NONE",
        "REGP",
@@ -16,7 +15,7 @@ var cnames9 = []string{
        "CREG",
        "CRBIT",
        "SPR",
-       "MREG",
+       "AREG",
        "ZCON",
        "U1CON",
        "U2CON",
index 1c86fe14635249823aea2f68acf68589f73d79de..08ade519240df941c47a7bdeeae8c368b8059651 100644 (file)
@@ -189,6 +189,7 @@ const (
        USETMP // generated code of this Prog uses REGTMP
 )
 
+//go:generate go run ../mkcnames.go -i a.out.go -o anamesz.go -p s390x
 const ( // comments from func aclass in asmz.go
        C_NONE     = iota
        C_REG      // general-purpose register (64-bit)
index 9c9b4d584c49f0830536ea8564014787457e3f2d..93dcfa99be73953f3e813606f982226f6c6f8833 100644 (file)
@@ -1,9 +1,8 @@
-// Copyright 2016 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.
+// Code generated by mkcnames -i a.out.go -o anamesz.go -p s390x; DO NOT EDIT.
 
 package s390x
 
+// This order should be strictly consistent to that in a.out.go.
 var cnamesz = []string{
        "NONE",
        "REG",