]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/gc: remove namebuf variable
authorMatthew Dempsky <mdempsky@google.com>
Fri, 6 Mar 2015 20:02:24 +0000 (12:02 -0800)
committerIan Lance Taylor <iant@golang.org>
Fri, 13 Mar 2015 23:35:11 +0000 (23:35 +0000)
namebuf was a global char buffer in the C version of gc, which was
useful for providing common storage for constructing symbol and file
names.  However, now that it's just a global Go string and the string
data is dynamically allocated anyway, it doesn't serve any purpose
except to force extra write barriers everytime it's assigned to.

Also, introduce Lookupf(fmt, args...) as shorthand for
Lookup(fmt.Sprintf(fmt, args...)), which was a very common pattern for
using namebuf.

Passes "go build -toolexec 'toolstash -cmp' -a std".

Notably, this CL shrinks 6g's text section by ~15kB:

$ size toolstash/6g tool/linux_amd64/6g
   text    data     bss     dec     hex filename
4600805  605968  342988 5549761  54aec1 toolstash/6g
4585547  605968  342956 5534471  547307 tool/linux_amd64/6g

Change-Id: I98abb44fc7f43a2e2e48425cc9f215cd0be37442
Reviewed-on: https://go-review.googlesource.com/7080
Reviewed-by: Ian Lance Taylor <iant@golang.org>
12 files changed:
src/cmd/internal/gc/closure.go
src/cmd/internal/gc/dcl.go
src/cmd/internal/gc/gen.go
src/cmd/internal/gc/go.go
src/cmd/internal/gc/init.go
src/cmd/internal/gc/inl.go
src/cmd/internal/gc/lex.go
src/cmd/internal/gc/obj.go
src/cmd/internal/gc/pgen.go
src/cmd/internal/gc/sinit.go
src/cmd/internal/gc/subr.go
src/cmd/internal/gc/walk.go

index 3155b69a21e9423c3ea3cfd169e8bd2433bd55a6..df89544162537ed834f6aa8fa93a0934a5130a26 100644 (file)
@@ -174,8 +174,7 @@ func closurename(n *Node) *Sym {
        } else {
                Fatal("closurename called for %v", Nconv(n, obj.FmtShort))
        }
-       namebuf = fmt.Sprintf("%s.%s%d", outer, prefix, gen)
-       n.Sym = Lookup(namebuf)
+       n.Sym = Lookupf("%s.%s%d", outer, prefix, gen)
        return n.Sym
 }
 
@@ -333,9 +332,7 @@ func transformclosure(xfunc *Node) {
                                // we introduce function param &v *T
                                // and v remains PPARAMREF with &v heapaddr
                                // (accesses will implicitly deref &v).
-                               namebuf = fmt.Sprintf("&%s", v.Sym.Name)
-
-                               addr = newname(Lookup(namebuf))
+                               addr = newname(Lookupf("&%s", v.Sym.Name))
                                addr.Type = Ptrto(v.Type)
                                addr.Class = PPARAM
                                v.Heapaddr = addr
@@ -397,9 +394,7 @@ func transformclosure(xfunc *Node) {
                        } else {
                                // Declare variable holding addresses taken from closure
                                // and initialize in entry prologue.
-                               namebuf = fmt.Sprintf("&%s", v.Sym.Name)
-
-                               addr = newname(Lookup(namebuf))
+                               addr = newname(Lookupf("&%s", v.Sym.Name))
                                addr.Ntype = Nod(OIND, typenod(v.Type), nil)
                                addr.Class = PAUTO
                                addr.Used = true
@@ -557,9 +552,8 @@ func makepartialcall(fn *Node, t0 *Type, meth *Node) *Node {
        var fld *Node
        var n *Node
        for t := getinargx(t0).Type; t != nil; t = t.Down {
-               namebuf = fmt.Sprintf("a%d", i)
+               n = newname(Lookupf("a%d", i))
                i++
-               n = newname(Lookup(namebuf))
                n.Class = PPARAM
                xfunc.Dcl = list(xfunc.Dcl, n)
                callargs = list(callargs, n)
@@ -577,9 +571,8 @@ func makepartialcall(fn *Node, t0 *Type, meth *Node) *Node {
        l = nil
        var retargs *NodeList
        for t := getoutargx(t0).Type; t != nil; t = t.Down {
-               namebuf = fmt.Sprintf("r%d", i)
+               n = newname(Lookupf("r%d", i))
                i++
-               n = newname(Lookup(namebuf))
                n.Class = PPARAMOUT
                xfunc.Dcl = list(xfunc.Dcl, n)
                retargs = list(retargs, n)
index 7194c123a25acf57971200301a57460ef382f17d..fa955ba929e1686622e1366bce4b97c00368d62b 100644 (file)
@@ -645,10 +645,8 @@ func funcargs(nt *Node) {
 
                if n.Left == nil {
                        // Name so that escape analysis can track it. ~r stands for 'result'.
-                       namebuf = fmt.Sprintf("~r%d", gen)
+                       n.Left = newname(Lookupf("~r%d", gen))
                        gen++
-
-                       n.Left = newname(Lookup(namebuf))
                }
 
                // TODO: n->left->missing = 1;
@@ -667,9 +665,8 @@ func funcargs(nt *Node) {
 
                        *nn = *n.Left
                        nn.Orig = nn
-                       namebuf = fmt.Sprintf("~b%d", gen)
+                       nn.Sym = Lookupf("~b%d", gen)
                        gen++
-                       nn.Sym = Lookup(namebuf)
                        n.Left = nn
                }
 
index 70c218e667cbd06e21b922bbf8e38b13341c8295..3777cc310dee35941982f3ddccd65d4b28208208 100644 (file)
@@ -590,10 +590,8 @@ func Tempname(nn *Node, t *Type) {
 
        // give each tmp a different name so that there
        // a chance to registerizer them
-       namebuf = fmt.Sprintf("autotmp_%.4d", statuniqgen)
-
+       s := Lookupf("autotmp_%.4d", statuniqgen)
        statuniqgen++
-       s := Lookup(namebuf)
        n := Nod(ONAME, nil, nil)
        n.Sym = s
        s.Def = n
index 348dd833e0388d71d6637c372e5b09735d454fa0..e9412d1ff270804c2c780db9871ca1b82763b970 100644 (file)
@@ -500,8 +500,6 @@ var safemode int
 
 var nolocalimports int
 
-var namebuf string
-
 var lexbuf bytes.Buffer
 var strbuf bytes.Buffer
 
index c57e50b13dc959b8f55131c0050279a04ff9c223..8aaed8c63c87058e986b061685bda6093ffe2f7a 100644 (file)
@@ -4,8 +4,6 @@
 
 package gc
 
-import "fmt"
-
 //     case OADD:
 //             if(n->right->op == OLITERAL) {
 //                     v = n->right->vconst;
@@ -31,8 +29,7 @@ var renameinit_initgen int
 
 func renameinit() *Sym {
        renameinit_initgen++
-       namebuf = fmt.Sprintf("init.%d", renameinit_initgen)
-       return Lookup(namebuf)
+       return Lookupf("init.%d", renameinit_initgen)
 }
 
 /*
@@ -112,18 +109,14 @@ func fninit(n *NodeList) {
        var r *NodeList
 
        // (1)
-       namebuf = "initdoneĀ·"
-
-       gatevar := newname(Lookup(namebuf))
+       gatevar := newname(Lookup("initdoneĀ·"))
        addvar(gatevar, Types[TUINT8], PEXTERN)
 
        // (2)
        Maxarg = 0
 
-       namebuf = "init"
-
        fn := Nod(ODCLFUNC, nil, nil)
-       initsym := Lookup(namebuf)
+       initsym := Lookup("init")
        fn.Nname = newname(initsym)
        fn.Nname.Defn = fn
        fn.Nname.Ntype = Nod(OTFUNC, nil, nil)
@@ -169,8 +162,7 @@ func fninit(n *NodeList) {
        // (9)
        // could check that it is fn of no args/returns
        for i := 1; ; i++ {
-               namebuf = fmt.Sprintf("init.%d", i)
-               s := Lookup(namebuf)
+               s := Lookupf("init.%d", i)
                if s.Def == nil {
                        break
                }
index c266f7d7aa3458ba1e47b10921ea323fb37e1ba5..cdd709ed7702c7b658c23f384efdab79770c802c 100644 (file)
@@ -859,8 +859,7 @@ func inlvar(var_ *Node) *Node {
 
 // Synthesize a variable to store the inlined function's results in.
 func retvar(t *Type, i int) *Node {
-       namebuf = fmt.Sprintf("~r%d", i)
-       n := newname(Lookup(namebuf))
+       n := newname(Lookupf("~r%d", i))
        n.Type = t.Type
        n.Class = PAUTO
        n.Used = true
@@ -872,8 +871,7 @@ func retvar(t *Type, i int) *Node {
 // Synthesize a variable to store the inlined function's arguments
 // when they come from a multiple return call.
 func argvar(t *Type, i int) *Node {
-       namebuf = fmt.Sprintf("~arg%d", i)
-       n := newname(Lookup(namebuf))
+       n := newname(Lookupf("~arg%d", i))
        n.Type = t.Type
        n.Class = PAUTO
        n.Used = true
@@ -886,8 +884,7 @@ var newlabel_inl_label int
 
 func newlabel_inl() *Node {
        newlabel_inl_label++
-       namebuf = fmt.Sprintf(".inlret%.6d", newlabel_inl_label)
-       n := newname(Lookup(namebuf))
+       n := newname(Lookupf(".inlret%.6d", newlabel_inl_label))
        n.Etype = 1 // flag 'safe' for escape analysis (no backjumps)
        return n
 }
index 816e67e89a45e24f3a0bbb79181096883931efb4..03e874929d6bdd9ea2e8bf559a63c2dbeb8b8fb6 100644 (file)
@@ -559,25 +559,24 @@ func islocalname(name string) bool {
                strings.HasPrefix(name, "../") || name == ".."
 }
 
-func findpkg(name string) bool {
+func findpkg(name string) (file string, ok bool) {
        if islocalname(name) {
                if safemode != 0 || nolocalimports != 0 {
-                       return false
+                       return "", false
                }
 
                // try .a before .6.  important for building libraries:
                // if there is an array.6 in the array.a library,
                // want to find all of array.a, not just array.6.
-               namebuf = fmt.Sprintf("%s.a", name)
-
-               if obj.Access(namebuf, 0) >= 0 {
-                       return true
+               file = fmt.Sprintf("%s.a", name)
+               if obj.Access(file, 0) >= 0 {
+                       return file, true
                }
-               namebuf = fmt.Sprintf("%s.%c", name, Thearch.Thechar)
-               if obj.Access(namebuf, 0) >= 0 {
-                       return true
+               file = fmt.Sprintf("%s.%c", name, Thearch.Thechar)
+               if obj.Access(file, 0) >= 0 {
+                       return file, true
                }
-               return false
+               return "", false
        }
 
        // local imports should be canonicalized already.
@@ -587,17 +586,17 @@ func findpkg(name string) bool {
        _ = q
        if path.Clean(name) != name {
                Yyerror("non-canonical import path %q (should be %q)", name, q)
-               return false
+               return "", false
        }
 
        for p := idirs; p != nil; p = p.link {
-               namebuf = fmt.Sprintf("%s/%s.a", p.dir, name)
-               if obj.Access(namebuf, 0) >= 0 {
-                       return true
+               file = fmt.Sprintf("%s/%s.a", p.dir, name)
+               if obj.Access(file, 0) >= 0 {
+                       return file, true
                }
-               namebuf = fmt.Sprintf("%s/%s.%c", p.dir, name, Thearch.Thechar)
-               if obj.Access(namebuf, 0) >= 0 {
-                       return true
+               file = fmt.Sprintf("%s/%s.%c", p.dir, name, Thearch.Thechar)
+               if obj.Access(file, 0) >= 0 {
+                       return file, true
                }
        }
 
@@ -612,17 +611,17 @@ func findpkg(name string) bool {
                        suffix = "race"
                }
 
-               namebuf = fmt.Sprintf("%s/pkg/%s_%s%s%s/%s.a", goroot, goos, goarch, suffixsep, suffix, name)
-               if obj.Access(namebuf, 0) >= 0 {
-                       return true
+               file = fmt.Sprintf("%s/pkg/%s_%s%s%s/%s.a", goroot, goos, goarch, suffixsep, suffix, name)
+               if obj.Access(file, 0) >= 0 {
+                       return file, true
                }
-               namebuf = fmt.Sprintf("%s/pkg/%s_%s%s%s/%s.%c", goroot, goos, goarch, suffixsep, suffix, name, Thearch.Thechar)
-               if obj.Access(namebuf, 0) >= 0 {
-                       return true
+               file = fmt.Sprintf("%s/pkg/%s_%s%s%s/%s.%c", goroot, goos, goarch, suffixsep, suffix, name, Thearch.Thechar)
+               if obj.Access(file, 0) >= 0 {
+                       return file, true
                }
        }
 
-       return false
+       return "", false
 }
 
 func fakeimport() {
@@ -698,7 +697,8 @@ func importfile(f *Val, line int) {
                }
        }
 
-       if !findpkg(path_) {
+       file, found := findpkg(path_)
+       if !found {
                Yyerror("can't find import: %q", f.U.Sval)
                errorexit()
        }
@@ -708,7 +708,6 @@ func importfile(f *Val, line int) {
        // If we already saw that package, feed a dummy statement
        // to the lexer to avoid parsing export data twice.
        if importpkg.Imported != 0 {
-               file := namebuf
                tag := ""
                if importpkg.Safe {
                        tag = "safe"
@@ -723,16 +722,13 @@ func importfile(f *Val, line int) {
 
        var err error
        var imp *obj.Biobuf
-       imp, err = obj.Bopenr(namebuf)
+       imp, err = obj.Bopenr(file)
        if err != nil {
                Yyerror("can't open import: %q: %v", f.U.Sval, err)
                errorexit()
        }
 
-       file := namebuf
-
-       n := len(namebuf)
-       if n > 2 && namebuf[n-2] == '.' && namebuf[n-1] == 'a' {
+       if strings.HasSuffix(file, ".a") {
                if !skiptopkgdef(imp) {
                        Yyerror("import %s: not a package file", file)
                        errorexit()
@@ -757,7 +753,7 @@ func importfile(f *Val, line int) {
 
        // assume files move (get installed)
        // so don't record the full path.
-       linehist(file[n-len(path_)-2:], -1, 1) // acts as #pragma lib
+       linehist(file[len(file)-len(path_)-2:], -1, 1) // acts as #pragma lib
 
        /*
         * position the input right
@@ -3162,10 +3158,9 @@ func mkpackage(pkgname string) {
                                p = p[i+1:]
                        }
                }
-               namebuf = p
-               if i := strings.LastIndex(namebuf, "."); i >= 0 {
-                       namebuf = namebuf[:i]
+               if i := strings.LastIndex(p, "."); i >= 0 {
+                       p = p[:i]
                }
-               outfile = fmt.Sprintf("%s.%c", namebuf, Thearch.Thechar)
+               outfile = fmt.Sprintf("%s.%c", p, Thearch.Thechar)
        }
 }
index d59898f28611556dc7af9efcc94702da9d1cbdea..077b89b500ad742c2f1618a71d255446481f7c0e 100644 (file)
@@ -101,8 +101,8 @@ func dumpobj() {
                        obj.Bputc(bout, 0)
                }
                obj.Bseek(bout, startobj-ArhdrSize, 0)
-               namebuf = fmt.Sprintf("_go_.%c", Thearch.Thechar)
-               formathdr(arhdr[:], namebuf, size)
+               name := fmt.Sprintf("_go_.%c", Thearch.Thechar)
+               formathdr(arhdr[:], name, size)
                obj.Bwrite(bout, arhdr[:])
        }
 
@@ -199,22 +199,23 @@ func duintptr(s *Sym, off int, v uint64) int {
 var stringsym_gen int
 
 func stringsym(s string) *Sym {
+       var symname string
        var pkg *Pkg
        if len(s) > 100 {
                // huge strings are made static to avoid long names
                stringsym_gen++
-               namebuf = fmt.Sprintf(".gostring.%d", stringsym_gen)
+               symname = fmt.Sprintf(".gostring.%d", stringsym_gen)
 
                pkg = localpkg
        } else {
                // small strings get named by their contents,
                // so that multiple modules using the same string
                // can share it.
-               namebuf = fmt.Sprintf("%q", s)
+               symname = fmt.Sprintf("%q", s)
                pkg = gostringpkg
        }
 
-       sym := Pkglookup(namebuf, pkg)
+       sym := Pkglookup(symname, pkg)
 
        // SymUniq flag indicates that data is generated already
        if sym.Flags&SymUniq != 0 {
@@ -252,8 +253,8 @@ func slicebytes(nam *Node, s string, len int) {
        var m int
 
        slicebytes_gen++
-       namebuf = fmt.Sprintf(".gobytes.%d", slicebytes_gen)
-       sym := Pkglookup(namebuf, localpkg)
+       symname := fmt.Sprintf(".gobytes.%d", slicebytes_gen)
+       sym := Pkglookup(symname, localpkg)
        sym.Def = newname(sym)
 
        off := 0
index 208ecb80ff0cc1bdb42b9d6569e5a7dc7ce5249f..06239048e2b48fad4b91799ea14c9cc2e855d7a7 100644 (file)
@@ -18,9 +18,8 @@ var makefuncdatasym_nsym int32
 func makefuncdatasym(namefmt string, funcdatakind int64) *Sym {
        var nod Node
 
-       namebuf = fmt.Sprintf(namefmt, makefuncdatasym_nsym)
+       sym := Lookupf(namefmt, makefuncdatasym_nsym)
        makefuncdatasym_nsym++
-       sym := Lookup(namebuf)
        pnod := newname(sym)
        pnod.Class = PEXTERN
        Nodconst(&nod, Types[TINT32], funcdatakind)
index c5e006b160fcd389691f46dc6bf2a54b90ded078..2dec7572bde13af4dbeb98749224e4f3660a2a5e 100644 (file)
@@ -517,9 +517,8 @@ func staticassign(l *Node, r *Node, out **NodeList) bool {
  * part of the composite literal.
  */
 func staticname(t *Type, ctxt int) *Node {
-       namebuf = fmt.Sprintf("statictmp_%.4d", statuniqgen)
+       n := newname(Lookupf("statictmp_%.4d", statuniqgen))
        statuniqgen++
-       n := newname(Lookup(namebuf))
        if ctxt == 0 {
                n.Readonly = true
        }
index c59b7e6447e066658a799fcb1ddcc1cbf0a630d5..f77d1511fce1836066a04307703d2a624e317b5e 100644 (file)
@@ -281,6 +281,10 @@ func Lookup(name string) *Sym {
        return localpkg.Lookup(name)
 }
 
+func Lookupf(format string, a ...interface{}) *Sym {
+       return Lookup(fmt.Sprintf(format, a...))
+}
+
 func LookupBytes(name []byte) *Sym {
        return localpkg.LookupBytes(name)
 }
index e88b4c28b09cc70bdb33d3fbf8a96abd122a5d46..e4abc282c2529a8d0f1ab76bc84ec0725b9bc650 100644 (file)
@@ -1109,17 +1109,18 @@ func walkexpr(np **Node, init **NodeList) {
                        if Widthreg >= 8 || (et != TUINT64 && et != TINT64) {
                                goto ret
                        }
+                       var fn string
                        if et == TINT64 {
-                               namebuf = "int64"
+                               fn = "int64"
                        } else {
-                               namebuf = "uint64"
+                               fn = "uint64"
                        }
                        if n.Op == ODIV {
-                               namebuf += "div"
+                               fn += "div"
                        } else {
-                               namebuf += "mod"
+                               fn += "mod"
                        }
-                       n = mkcall(namebuf, n.Type, init, conv(n.Left, Types[et]), conv(n.Right, Types[et]))
+                       n = mkcall(fn, n.Type, init, conv(n.Left, Types[et]), conv(n.Right, Types[et]))
 
                default:
                        break
@@ -2882,13 +2883,14 @@ func addstr(n *Node, init **NodeList) *Node {
                args = list(args, conv(l.N, Types[TSTRING]))
        }
 
+       var fn string
        if c <= 5 {
                // small numbers of strings use direct runtime helpers.
                // note: orderexpr knows this cutoff too.
-               namebuf = fmt.Sprintf("concatstring%d", c)
+               fn = fmt.Sprintf("concatstring%d", c)
        } else {
                // large numbers of strings are passed to the runtime as a slice.
-               namebuf = "concatstrings"
+               fn = "concatstrings"
 
                t := typ(TARRAY)
                t.Type = Types[TSTRING]
@@ -2901,7 +2903,7 @@ func addstr(n *Node, init **NodeList) *Node {
                slice.Esc = EscNone
        }
 
-       cat := syslook(namebuf, 1)
+       cat := syslook(fn, 1)
        r := Nod(OCALL, cat, nil)
        r.List = args
        typecheck(&r, Erv)