From db9796dac50c65d300d2b262f2f107d86bf1703f Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Mon, 20 Jun 2016 08:18:22 -0700 Subject: [PATCH] cmd/compile: simplify staticname Add docs. Give it a more natural signature. Passes toolstash -cmp. Change-Id: Iab368dd10e8f16e41b725c2980020bbead2cdefb Reviewed-on: https://go-review.googlesource.com/26756 Run-TryBot: Josh Bleecher Snyder Reviewed-by: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/cmd/compile/internal/gc/sinit.go | 31 ++++++++++++++++------------ src/cmd/compile/internal/gc/walk.go | 3 ++- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/cmd/compile/internal/gc/sinit.go b/src/cmd/compile/internal/gc/sinit.go index a801db9aa5..b480a8a211 100644 --- a/src/cmd/compile/internal/gc/sinit.go +++ b/src/cmd/compile/internal/gc/sinit.go @@ -400,7 +400,7 @@ func staticassign(l *Node, r *Node, out *[]*Node) bool { switch r.Left.Op { case OARRAYLIT, OSLICELIT, OMAPLIT, OSTRUCTLIT: // Init pointer. - a := staticname(r.Left.Type, inNonInitFunction) + a := staticname(r.Left.Type) inittemps[r] = a gdata(l, Nod(OADDR, a, nil), int(l.Type.Width)) @@ -425,7 +425,7 @@ func staticassign(l *Node, r *Node, out *[]*Node) bool { // Init slice. bound := r.Right.Int64() ta := typArray(r.Type.Elem(), bound) - a := staticname(ta, inNonInitFunction) + a := staticname(ta) inittemps[r] = a n := *l n.Xoffset = l.Xoffset + int64(Array_array) @@ -507,12 +507,13 @@ const ( // most of the work is to generate // data statements for the constant // part of the composite literal. -func staticname(t *Type, ctxt initContext) *Node { + +// staticname return a name backed by a static data symbol. +// Callers should set n.Name.Readonly = true on the +// returned node for readonly nodes. +func staticname(t *Type) *Node { n := newname(LookupN("statictmp_", statuniqgen)) statuniqgen++ - if ctxt == inInitFunction { - n.Name.Readonly = true - } addvar(n, t, PEXTERN) return n } @@ -684,7 +685,7 @@ func slicelit(ctxt initContext, n *Node, var_ *Node, init *Nodes) { if ctxt == inNonInitFunction { // put everything into static array - vstat := staticname(t, ctxt) + vstat := staticname(t) fixedlit(ctxt, initKindStatic, n, vstat, init) fixedlit(ctxt, initKindDynamic, n, vstat, init) @@ -724,7 +725,10 @@ func slicelit(ctxt initContext, n *Node, var_ *Node, init *Nodes) { mode := getdyn(n, true) if mode&initConst != 0 { - vstat = staticname(t, ctxt) + vstat = staticname(t) + if ctxt == inInitFunction { + vstat.Name.Readonly = true + } fixedlit(ctxt, initKindStatic, n, vstat, init) } @@ -819,8 +823,6 @@ func slicelit(ctxt initContext, n *Node, var_ *Node, init *Nodes) { } func maplit(ctxt initContext, n *Node, m *Node, init *Nodes) { - ctxt = inInitFunction - // make the map var nerr := nerrors @@ -852,8 +854,10 @@ func maplit(ctxt initContext, n *Node, m *Node, init *Nodes) { dowidth(tv) // make and initialize static arrays - vstatk := staticname(tk, ctxt) - vstatv := staticname(tv, ctxt) + vstatk := staticname(tk) + vstatk.Name.Readonly = true + vstatv := staticname(tv) + vstatv.Name.Readonly = true b := int64(0) for _, r := range n.List.Slice() { @@ -1005,7 +1009,8 @@ func anylit(ctxt initContext, n *Node, var_ *Node, init *Nodes) { if var_.isSimpleName() && n.List.Len() > 4 { if ctxt == inInitFunction { // lay out static data - vstat := staticname(t, ctxt) + vstat := staticname(t) + vstat.Name.Readonly = true litctxt := ctxt if n.Op == OARRAYLIT { diff --git a/src/cmd/compile/internal/gc/walk.go b/src/cmd/compile/internal/gc/walk.go index f38e7282ba..28fe549694 100644 --- a/src/cmd/compile/internal/gc/walk.go +++ b/src/cmd/compile/internal/gc/walk.go @@ -1636,7 +1636,8 @@ opswitch: if isStaticCompositeLiteral(n) { // n can be directly represented in the read-only data section. // Make direct reference to the static data. See issue 12841. - vstat := staticname(n.Type, inInitFunction) + vstat := staticname(n.Type) + vstat.Name.Readonly = true fixedlit(inInitFunction, initKindStatic, n, vstat, init) n = vstat n = typecheck(n, Erv) -- 2.48.1