]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.regabi] cmd/compile: make ir.Func the ODCLFUNC Node implementation
authorRuss Cox <rsc@golang.org>
Sat, 28 Nov 2020 12:23:50 +0000 (07:23 -0500)
committerRuss Cox <rsc@golang.org>
Mon, 30 Nov 2020 18:34:00 +0000 (18:34 +0000)
Before this CL, an ODCLFUNC Node was represented by both
a node struct and a Func struct (and a Name for the ONAME,
which isn't changing here). Now Func can be repurposed as
the ODCLFUNC implementation, replacing the two structs
totaling 280+144 = 424 bytes (64-bit) with a single 320-byte struct.

Using the *Func as the node also gives us a clear, typed answer to
“which node should we use to represent functions?”
The next CL will clean up uses. This CL is just the trivial
change in representation.

Passes buildall w/ toolstash -cmp.

Change-Id: Ie6d670da91d6eb8d67a85f8f83630b9586dc7443
Reviewed-on: https://go-review.googlesource.com/c/go/+/274096
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/ir/fmt.go
src/cmd/compile/internal/ir/func.go
src/cmd/compile/internal/ir/node.go
src/cmd/compile/internal/ir/sizeof_test.go

index e749778030c97fa4573c8b8e2826741797e70e13..3822c4c73bd77dbc13b1e547aecf4e7789c54952 100644 (file)
@@ -1269,6 +1269,13 @@ func exprFmt(n Node, s fmt.State, prec int, mode FmtMode) {
                        mode.Fprintf(s, ")")
                }
 
+       case ODCLFUNC:
+               if sym := n.Sym(); sym != nil {
+                       fmt.Fprint(s, smodeString(sym, mode))
+                       return
+               }
+               mode.Fprintf(s, "<unnamed Func>")
+
        case ONAME:
                // Special case: name used as local variable in export.
                // _ becomes ~b%d internally; print as _ for export
index 15661259554830d36d099407514b3b68dcabc793..57ec0707e9fc41173c6efb11f44ec3c4556b67d6 100644 (file)
@@ -9,6 +9,7 @@ import (
        "cmd/compile/internal/types"
        "cmd/internal/obj"
        "cmd/internal/src"
+       "fmt"
 )
 
 // A Func corresponds to a single function in a Go program
@@ -47,6 +48,11 @@ import (
 // the generated ODCLFUNC (as n.Func.Decl), but there is no
 // pointer from the Func back to the OCALLPART.
 type Func struct {
+       miniNode
+       typ  *types.Type
+       body Nodes
+       iota int64
+
        Nname    Node // ONAME node
        Decl     Node // ODCLFUNC node
        OClosure Node // OCLOSURE node
@@ -102,6 +108,34 @@ type Func struct {
        NWBRCalls *[]SymAndPos
 }
 
+func NewFunc(pos src.XPos) *Func {
+       f := new(Func)
+       f.pos = pos
+       f.op = ODCLFUNC
+       f.Decl = f
+       f.iota = -1
+       return f
+}
+
+func (f *Func) String() string                { return fmt.Sprint(f) }
+func (f *Func) Format(s fmt.State, verb rune) { FmtNode(f, s, verb) }
+func (f *Func) RawCopy() Node                 { panic(f.no("RawCopy")) }
+func (f *Func) Func() *Func                   { return f }
+func (f *Func) Body() Nodes                   { return f.body }
+func (f *Func) PtrBody() *Nodes               { return &f.body }
+func (f *Func) SetBody(x Nodes)               { f.body = x }
+func (f *Func) Type() *types.Type             { return f.typ }
+func (f *Func) SetType(x *types.Type)         { f.typ = x }
+func (f *Func) Iota() int64                   { return f.iota }
+func (f *Func) SetIota(x int64)               { f.iota = x }
+
+func (f *Func) Sym() *types.Sym {
+       if f.Nname != nil {
+               return f.Nname.Sym()
+       }
+       return nil
+}
+
 // An Inline holds fields used for function bodies that can be inlined.
 type Inline struct {
        Cost int32 // heuristic cost of inlining this function
index 1b01032c9b395d9b530dc41fb29df696cb3322ae..02a5d7769a4c3d8c33f25bf6aeb30c91f5b1c9ca 100644 (file)
@@ -1106,13 +1106,7 @@ func NodAt(pos src.XPos, op Op, nleft, nright Node) Node {
        var n *node
        switch op {
        case ODCLFUNC:
-               var x struct {
-                       n node
-                       f Func
-               }
-               n = &x.n
-               n.SetFunc(&x.f)
-               n.Func().Decl = n
+               return NewFunc(pos)
        case OPACK:
                return NewPkgName(pos, nil, nil)
        case OEMPTY:
@@ -1179,7 +1173,6 @@ var okForNod = [OEND]bool{
        ODCL:           true,
        ODCLCONST:      true,
        ODCLFIELD:      true,
-       ODCLFUNC:       true,
        ODCLTYPE:       true,
        ODDD:           true,
        ODEFER:         true,
index 8a0b078b9b9f65547245242e7b6d11aa63062cb8..8597ad492ad32b05b7fa6efd5a3d28a1011b631d 100644 (file)
@@ -20,7 +20,7 @@ func TestSizeof(t *testing.T) {
                _32bit uintptr     // size on 32bit platforms
                _64bit uintptr     // size on 64bit platforms
        }{
-               {Func{}, 152, 280},
+               {Func{}, 180, 320},
                {Name{}, 132, 232},
                {node{}, 84, 144},
        }