Commit 8b3670ff authored by Matthew Dempsky's avatar Matthew Dempsky Committed by Ian Lance Taylor

cmd/internal/gc: remove namebuf variable

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