[dev.regabi] cmd/compile: simplify fmt handling of Nodes

The existing code introduces many types in what appears to be an
attempt to avoid allocation when converting formatting argument lists.
Simplify by accepting that allocation is going to happen, especially
when Node itself turns into an interface.

Change-Id: I3c0d45ca01eace4924deb43c0ea7dc6d65943d08
Reviewed-on: https://go-review.googlesource.com/c/go/+/272929
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Russ Cox 2020-11-24 23:58:36 -05:00
parent d166ef6876
commit 6e583d65ab
1 changed files with 53 additions and 138 deletions

View File

@ -238,71 +238,48 @@ func (o Op) oconv(s fmt.State, flag FmtFlag, mode fmtMode) {
fmt.Fprint(s, o.String()) fmt.Fprint(s, o.String())
} }
type ( type fmtMode int
fmtMode int
fmtNodeErr Node type fmtNode struct {
fmtNodeDbg Node x *Node
fmtNodeTypeId Node m fmtMode
fmtNodeTypeIdName Node }
fmtOpErr Op func (f *fmtNode) Format(s fmt.State, verb rune) { f.x.format(s, verb, f.m) }
fmtOpDbg Op
fmtOpTypeId Op
fmtOpTypeIdName Op
fmtTypeErr types.Type type fmtOp struct {
fmtTypeDbg types.Type x Op
fmtTypeTypeId types.Type m fmtMode
fmtTypeTypeIdName types.Type }
fmtSymErr types.Sym func (f *fmtOp) Format(s fmt.State, verb rune) { f.x.format(s, verb, f.m) }
fmtSymDbg types.Sym
fmtSymTypeId types.Sym
fmtSymTypeIdName types.Sym
fmtNodesErr Nodes type fmtType struct {
fmtNodesDbg Nodes x *types.Type
fmtNodesTypeId Nodes m fmtMode
fmtNodesTypeIdName Nodes }
)
func (f *fmtType) Format(s fmt.State, verb rune) { typeFormat(f.x, s, verb, f.m) }
type fmtSym struct {
x *types.Sym
m fmtMode
}
func (f *fmtSym) Format(s fmt.State, verb rune) { symFormat(f.x, s, verb, f.m) }
type fmtNodes struct {
x Nodes
m fmtMode
}
func (f *fmtNodes) Format(s fmt.State, verb rune) { f.x.format(s, verb, f.m) }
func (n *fmtNodeErr) Format(s fmt.State, verb rune) { (*Node)(n).format(s, verb, FErr) }
func (n *fmtNodeDbg) Format(s fmt.State, verb rune) { (*Node)(n).format(s, verb, FDbg) }
func (n *fmtNodeTypeId) Format(s fmt.State, verb rune) { (*Node)(n).format(s, verb, FTypeId) }
func (n *fmtNodeTypeIdName) Format(s fmt.State, verb rune) { (*Node)(n).format(s, verb, FTypeIdName) }
func (n *Node) Format(s fmt.State, verb rune) { n.format(s, verb, FErr) } func (n *Node) Format(s fmt.State, verb rune) { n.format(s, verb, FErr) }
func (o fmtOpErr) Format(s fmt.State, verb rune) { Op(o).format(s, verb, FErr) }
func (o fmtOpDbg) Format(s fmt.State, verb rune) { Op(o).format(s, verb, FDbg) }
func (o fmtOpTypeId) Format(s fmt.State, verb rune) { Op(o).format(s, verb, FTypeId) }
func (o fmtOpTypeIdName) Format(s fmt.State, verb rune) { Op(o).format(s, verb, FTypeIdName) }
func (o Op) Format(s fmt.State, verb rune) { o.format(s, verb, FErr) } func (o Op) Format(s fmt.State, verb rune) { o.format(s, verb, FErr) }
func (t *fmtTypeErr) Format(s fmt.State, verb rune) { typeFormat((*types.Type)(t), s, verb, FErr) }
func (t *fmtTypeDbg) Format(s fmt.State, verb rune) { typeFormat((*types.Type)(t), s, verb, FDbg) }
func (t *fmtTypeTypeId) Format(s fmt.State, verb rune) {
typeFormat((*types.Type)(t), s, verb, FTypeId)
}
func (t *fmtTypeTypeIdName) Format(s fmt.State, verb rune) {
typeFormat((*types.Type)(t), s, verb, FTypeIdName)
}
// func (t *types.Type) Format(s fmt.State, verb rune) // in package types // func (t *types.Type) Format(s fmt.State, verb rune) // in package types
func (y *fmtSymErr) Format(s fmt.State, verb rune) { symFormat((*types.Sym)(y), s, verb, FErr) }
func (y *fmtSymDbg) Format(s fmt.State, verb rune) { symFormat((*types.Sym)(y), s, verb, FDbg) }
func (y *fmtSymTypeId) Format(s fmt.State, verb rune) { symFormat((*types.Sym)(y), s, verb, FTypeId) }
func (y *fmtSymTypeIdName) Format(s fmt.State, verb rune) {
symFormat((*types.Sym)(y), s, verb, FTypeIdName)
}
// func (y *types.Sym) Format(s fmt.State, verb rune) // in package types { y.format(s, verb, FErr) } // func (y *types.Sym) Format(s fmt.State, verb rune) // in package types { y.format(s, verb, FErr) }
func (n fmtNodesErr) Format(s fmt.State, verb rune) { (Nodes)(n).format(s, verb, FErr) }
func (n fmtNodesDbg) Format(s fmt.State, verb rune) { (Nodes)(n).format(s, verb, FDbg) }
func (n fmtNodesTypeId) Format(s fmt.State, verb rune) { (Nodes)(n).format(s, verb, FTypeId) }
func (n fmtNodesTypeIdName) Format(s fmt.State, verb rune) { (Nodes)(n).format(s, verb, FTypeIdName) }
func (n Nodes) Format(s fmt.State, verb rune) { n.format(s, verb, FErr) } func (n Nodes) Format(s fmt.State, verb rune) { n.format(s, verb, FErr) }
func (m fmtMode) Fprintf(s fmt.State, format string, args ...interface{}) { func (m fmtMode) Fprintf(s fmt.State, format string, args ...interface{}) {
@ -321,86 +298,24 @@ func (m fmtMode) Sprint(args ...interface{}) string {
} }
func (m fmtMode) prepareArgs(args []interface{}) { func (m fmtMode) prepareArgs(args []interface{}) {
switch m {
case FErr:
for i, arg := range args { for i, arg := range args {
switch arg := arg.(type) { switch arg := arg.(type) {
case Op: case Op:
args[i] = fmtOpErr(arg) args[i] = &fmtOp{arg, m}
case *Node: case *Node:
args[i] = (*fmtNodeErr)(arg) args[i] = &fmtNode{arg, m}
case *types.Type: case *types.Type:
args[i] = (*fmtTypeErr)(arg) args[i] = &fmtType{arg, m}
case *types.Sym: case *types.Sym:
args[i] = (*fmtSymErr)(arg) args[i] = &fmtSym{arg, m}
case Nodes: case Nodes:
args[i] = fmtNodesErr(arg) args[i] = &fmtNodes{arg, m}
case int32, int64, string, types.EType, constant.Value: case int32, int64, string, types.EType, constant.Value:
// OK: printing these types doesn't depend on mode // OK: printing these types doesn't depend on mode
default: default:
Fatalf("mode.prepareArgs type %T", arg) Fatalf("mode.prepareArgs type %T", arg)
} }
} }
case FDbg:
for i, arg := range args {
switch arg := arg.(type) {
case Op:
args[i] = fmtOpDbg(arg)
case *Node:
args[i] = (*fmtNodeDbg)(arg)
case *types.Type:
args[i] = (*fmtTypeDbg)(arg)
case *types.Sym:
args[i] = (*fmtSymDbg)(arg)
case Nodes:
args[i] = fmtNodesDbg(arg)
case int32, int64, string, types.EType, constant.Value:
// OK: printing these types doesn't depend on mode
default:
Fatalf("mode.prepareArgs type %T", arg)
}
}
case FTypeId:
for i, arg := range args {
switch arg := arg.(type) {
case Op:
args[i] = fmtOpTypeId(arg)
case *Node:
args[i] = (*fmtNodeTypeId)(arg)
case *types.Type:
args[i] = (*fmtTypeTypeId)(arg)
case *types.Sym:
args[i] = (*fmtSymTypeId)(arg)
case Nodes:
args[i] = fmtNodesTypeId(arg)
case int32, int64, string, types.EType, constant.Value:
// OK: printing these types doesn't depend on mode
default:
Fatalf("mode.prepareArgs type %T", arg)
}
}
case FTypeIdName:
for i, arg := range args {
switch arg := arg.(type) {
case Op:
args[i] = fmtOpTypeIdName(arg)
case *Node:
args[i] = (*fmtNodeTypeIdName)(arg)
case *types.Type:
args[i] = (*fmtTypeTypeIdName)(arg)
case *types.Sym:
args[i] = (*fmtSymTypeIdName)(arg)
case Nodes:
args[i] = fmtNodesTypeIdName(arg)
case int32, int64, string, types.EType, constant.Value:
// OK: printing these types doesn't depend on mode
default:
Fatalf("mode.prepareArgs type %T", arg)
}
}
default:
Fatalf("mode.prepareArgs mode %d", m)
}
} }
func (n *Node) format(s fmt.State, verb rune, mode fmtMode) { func (n *Node) format(s fmt.State, verb rune, mode fmtMode) {