From: chenguoqi Date: Thu, 24 Oct 2024 03:16:00 +0000 (+0800) Subject: cmd/internal/obj: add tool to generate Cnames string X-Git-Tag: go1.24rc1~419 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=84e58c84fdbe4681b4cbfeb615aaba3437c8ac85;p=gostls13.git cmd/internal/obj: add tool to generate Cnames string 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 Reviewed-by: Cherry Mui Reviewed-by: Meidan Li LUCI-TryBot-Result: Go LUCI Reviewed-by: Qiqi Huang Auto-Submit: Ian Lance Taylor Reviewed-by: sophie zhao Commit-Queue: Ian Lance Taylor --- diff --git a/src/cmd/internal/obj/arm64/a.out.go b/src/cmd/internal/obj/arm64/a.out.go index 39b9f164b9..ad00e4842c 100644 --- a/src/cmd/internal/obj/arm64/a.out.go +++ b/src/cmd/internal/obj/arm64/a.out.go @@ -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. diff --git a/src/cmd/internal/obj/arm64/anames7.go b/src/cmd/internal/obj/arm64/anames7.go index 5f2e3a6f74..bf06ac3db2 100644 --- a/src/cmd/internal/obj/arm64/anames7.go +++ b/src/cmd/internal/obj/arm64/anames7.go @@ -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", diff --git a/src/cmd/internal/obj/loong64/a.out.go b/src/cmd/internal/obj/loong64/a.out.go index efd5b1b3cc..e6984dcba7 100644 --- a/src/cmd/internal/obj/loong64/a.out.go +++ b/src/cmd/internal/obj/loong64/a.out.go @@ -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 diff --git a/src/cmd/internal/obj/loong64/cnames.go b/src/cmd/internal/obj/loong64/cnames.go index 6e7101860a..ce76109d2a 100644 --- a/src/cmd/internal/obj/loong64/cnames.go +++ b/src/cmd/internal/obj/loong64/cnames.go @@ -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 diff --git a/src/cmd/internal/obj/mips/a.out.go b/src/cmd/internal/obj/mips/a.out.go index cd6131332a..5439f0e4aa 100644 --- a/src/cmd/internal/obj/mips/a.out.go +++ b/src/cmd/internal/obj/mips/a.out.go @@ -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 diff --git a/src/cmd/internal/obj/mips/anames0.go b/src/cmd/internal/obj/mips/anames0.go index c300696730..ec07492375 100644 --- a/src/cmd/internal/obj/mips/anames0.go +++ b/src/cmd/internal/obj/mips/anames0.go @@ -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 index 0000000000..72b27300c7 --- /dev/null +++ b/src/cmd/internal/obj/mkcnames.go @@ -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. +` diff --git a/src/cmd/internal/obj/ppc64/a.out.go b/src/cmd/internal/obj/ppc64/a.out.go index 137c6d2ff2..0c980ef476 100644 --- a/src/cmd/internal/obj/ppc64/a.out.go +++ b/src/cmd/internal/obj/ppc64/a.out.go @@ -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 */ diff --git a/src/cmd/internal/obj/ppc64/anames9.go b/src/cmd/internal/obj/ppc64/anames9.go index 81f73dcea6..c42125984c 100644 --- a/src/cmd/internal/obj/ppc64/anames9.go +++ b/src/cmd/internal/obj/ppc64/anames9.go @@ -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", diff --git a/src/cmd/internal/obj/s390x/a.out.go b/src/cmd/internal/obj/s390x/a.out.go index 1c86fe1463..08ade51924 100644 --- a/src/cmd/internal/obj/s390x/a.out.go +++ b/src/cmd/internal/obj/s390x/a.out.go @@ -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) diff --git a/src/cmd/internal/obj/s390x/anamesz.go b/src/cmd/internal/obj/s390x/anamesz.go index 9c9b4d584c..93dcfa99be 100644 --- a/src/cmd/internal/obj/s390x/anamesz.go +++ b/src/cmd/internal/obj/s390x/anamesz.go @@ -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",