1
0
mirror of https://github.com/golang/go synced 2024-11-22 05:04:40 -07:00

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 <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>
This commit is contained in:
chenguoqi 2024-10-24 11:16:00 +08:00 committed by Gopher Robot
parent 239dbd7dba
commit 84e58c84fd
11 changed files with 153 additions and 19 deletions

View 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.

View 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",

View 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

View 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

View 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

View 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",

View File

@ -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.
`

View 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 */

View 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",

View 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)

View 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",