cmd/internal/gc: delete Strlit, Zconv

Strlit was just a poor excuse for a Go string.
Use a Go string.
In the one case where it was a string-or-nil (Type.Note), use a *string.

Zconv was a poor excuse for %q. Use %q.
The only important part about Zconv's implementation
was that the compiler and linker agreed on the quoting rules.
Now they both use %q instead of having two Zconvs.

This CL *does* change the generated object files, because the
quoted strings end up in symbol names.
For example the string "\r\n" used to be named go.string."\r\n"
and is now go.string."\x0d\n".

Change-Id: I5c0d38e1570ffc495f0db1a20273c9564104a7e8
Reviewed-on: https://go-review.googlesource.com/6519
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Russ Cox 2015-03-02 16:03:26 -05:00
parent 4bbd7ae8e0
commit bed1f90d08
22 changed files with 179 additions and 289 deletions

View file

@ -387,7 +387,7 @@ func cgen(n *gc.Node, res *gc.Node) {
var n1 gc.Node var n1 gc.Node
regalloc(&n1, gc.Types[gc.Tptr], res) regalloc(&n1, gc.Types[gc.Tptr], res)
p1 := gins(arm.AMOVW, nil, &n1) p1 := gins(arm.AMOVW, nil, &n1)
gc.Datastring(nl.Val.U.Sval.S, &p1.From) gc.Datastring(nl.Val.U.Sval, &p1.From)
gmove(&n1, res) gmove(&n1, res)
regfree(&n1) regfree(&n1)
break break
@ -1075,7 +1075,7 @@ func agenr(n *gc.Node, a *gc.Node, res *gc.Node) {
if gc.Debug['B'] == 0 && !n.Bounded { if gc.Debug['B'] == 0 && !n.Bounded {
// check bounds // check bounds
if gc.Isconst(nl, gc.CTSTR) { if gc.Isconst(nl, gc.CTSTR) {
gc.Nodconst(&n4, gc.Types[gc.TUINT32], int64(len(nl.Val.U.Sval.S))) gc.Nodconst(&n4, gc.Types[gc.TUINT32], int64(len(nl.Val.U.Sval)))
} else if gc.Isslice(nl.Type) || nl.Type.Etype == gc.TSTRING { } else if gc.Isslice(nl.Type) || nl.Type.Etype == gc.TSTRING {
n1 = n3 n1 = n3
n1.Op = gc.OINDREG n1.Op = gc.OINDREG
@ -1102,7 +1102,7 @@ func agenr(n *gc.Node, a *gc.Node, res *gc.Node) {
if gc.Isconst(nl, gc.CTSTR) { if gc.Isconst(nl, gc.CTSTR) {
regalloc(&n3, gc.Types[gc.Tptr], res) regalloc(&n3, gc.Types[gc.Tptr], res)
p1 := gins(arm.AMOVW, nil, &n3) p1 := gins(arm.AMOVW, nil, &n3)
gc.Datastring(nl.Val.U.Sval.S, &p1.From) gc.Datastring(nl.Val.U.Sval, &p1.From)
p1.From.Type = obj.TYPE_ADDR p1.From.Type = obj.TYPE_ADDR
} else if gc.Isslice(nl.Type) || nl.Type.Etype == gc.TSTRING { } else if gc.Isslice(nl.Type) || nl.Type.Etype == gc.TSTRING {
n1 = n3 n1 = n3

View file

@ -8,8 +8,9 @@ import (
"cmd/internal/obj" "cmd/internal/obj"
"cmd/internal/obj/x86" "cmd/internal/obj/x86"
"fmt" "fmt"
"cmd/internal/gc"
) )
import "cmd/internal/gc"
/* /*
* reg.c * reg.c
@ -374,7 +375,7 @@ func cgen(n *gc.Node, res *gc.Node) {
var n1 gc.Node var n1 gc.Node
regalloc(&n1, gc.Types[gc.Tptr], res) regalloc(&n1, gc.Types[gc.Tptr], res)
p1 := gins(x86.ALEAQ, nil, &n1) p1 := gins(x86.ALEAQ, nil, &n1)
gc.Datastring(nl.Val.U.Sval.S, &p1.From) gc.Datastring(nl.Val.U.Sval, &p1.From)
gmove(&n1, res) gmove(&n1, res)
regfree(&n1) regfree(&n1)
break break
@ -794,7 +795,7 @@ func agenr(n *gc.Node, a *gc.Node, res *gc.Node) {
t = gc.Types[gc.TUINT64] t = gc.Types[gc.TUINT64]
} }
if gc.Isconst(nl, gc.CTSTR) { if gc.Isconst(nl, gc.CTSTR) {
gc.Nodconst(&nlen, t, int64(len(nl.Val.U.Sval.S))) gc.Nodconst(&nlen, t, int64(len(nl.Val.U.Sval)))
} else if gc.Isslice(nl.Type) || nl.Type.Etype == gc.TSTRING { } else if gc.Isslice(nl.Type) || nl.Type.Etype == gc.TSTRING {
if gc.Is64(nr.Type) { if gc.Is64(nr.Type) {
var n5 gc.Node var n5 gc.Node
@ -823,7 +824,7 @@ func agenr(n *gc.Node, a *gc.Node, res *gc.Node) {
if gc.Isconst(nl, gc.CTSTR) { if gc.Isconst(nl, gc.CTSTR) {
regalloc(&n3, gc.Types[gc.Tptr], res) regalloc(&n3, gc.Types[gc.Tptr], res)
p1 := gins(x86.ALEAQ, nil, &n3) p1 := gins(x86.ALEAQ, nil, &n3)
gc.Datastring(nl.Val.U.Sval.S, &p1.From) gc.Datastring(nl.Val.U.Sval, &p1.From)
gins(x86.AADDQ, &n2, &n3) gins(x86.AADDQ, &n2, &n3)
goto indexdone goto indexdone
} }

View file

@ -332,7 +332,7 @@ func cgen(n *gc.Node, res *gc.Node) {
var n1 gc.Node var n1 gc.Node
regalloc(&n1, gc.Types[gc.Tptr], res) regalloc(&n1, gc.Types[gc.Tptr], res)
p1 := gins(i386.ALEAL, nil, &n1) p1 := gins(i386.ALEAL, nil, &n1)
gc.Datastring(nl.Val.U.Sval.S, &p1.From) gc.Datastring(nl.Val.U.Sval, &p1.From)
gmove(&n1, res) gmove(&n1, res)
regfree(&n1) regfree(&n1)
break break
@ -726,7 +726,7 @@ func agen(n *gc.Node, res *gc.Node) {
var nlen gc.Node var nlen gc.Node
if gc.Isconst(nl, gc.CTSTR) { if gc.Isconst(nl, gc.CTSTR) {
gc.Nodconst(&nlen, t, int64(len(nl.Val.U.Sval.S))) gc.Nodconst(&nlen, t, int64(len(nl.Val.U.Sval)))
} else if gc.Isslice(nl.Type) || nl.Type.Etype == gc.TSTRING { } else if gc.Isslice(nl.Type) || nl.Type.Etype == gc.TSTRING {
nlen = n3 nlen = n3
nlen.Type = t nlen.Type = t
@ -747,7 +747,7 @@ func agen(n *gc.Node, res *gc.Node) {
if gc.Isconst(nl, gc.CTSTR) { if gc.Isconst(nl, gc.CTSTR) {
regalloc(&n3, gc.Types[gc.Tptr], res) regalloc(&n3, gc.Types[gc.Tptr], res)
p1 := gins(i386.ALEAL, nil, &n3) p1 := gins(i386.ALEAL, nil, &n3)
gc.Datastring(nl.Val.U.Sval.S, &p1.From) gc.Datastring(nl.Val.U.Sval, &p1.From)
p1.From.Scale = 1 p1.From.Scale = 1
p1.From.Index = n2.Val.U.Reg p1.From.Index = n2.Val.U.Reg
goto indexdone goto indexdone

View file

@ -380,7 +380,7 @@ func cgen(n *gc.Node, res *gc.Node) {
var n1 gc.Node var n1 gc.Node
regalloc(&n1, gc.Types[gc.Tptr], res) regalloc(&n1, gc.Types[gc.Tptr], res)
p1 := gins(ppc64.AMOVD, nil, &n1) p1 := gins(ppc64.AMOVD, nil, &n1)
gc.Datastring(nl.Val.U.Sval.S, &p1.From) gc.Datastring(nl.Val.U.Sval, &p1.From)
gmove(&n1, res) gmove(&n1, res)
regfree(&n1) regfree(&n1)
break break
@ -783,7 +783,7 @@ func agenr(n *gc.Node, a *gc.Node, res *gc.Node) {
if gc.Debug['B'] == 0 && !n.Bounded { if gc.Debug['B'] == 0 && !n.Bounded {
// check bounds // check bounds
if gc.Isconst(nl, gc.CTSTR) { if gc.Isconst(nl, gc.CTSTR) {
gc.Nodconst(&n4, gc.Types[gc.TUINT64], int64(len(nl.Val.U.Sval.S))) gc.Nodconst(&n4, gc.Types[gc.TUINT64], int64(len(nl.Val.U.Sval)))
} else if gc.Isslice(nl.Type) || nl.Type.Etype == gc.TSTRING { } else if gc.Isslice(nl.Type) || nl.Type.Etype == gc.TSTRING {
n1 = n3 n1 = n3
n1.Op = gc.OINDREG n1.Op = gc.OINDREG
@ -817,7 +817,7 @@ func agenr(n *gc.Node, a *gc.Node, res *gc.Node) {
if gc.Isconst(nl, gc.CTSTR) { if gc.Isconst(nl, gc.CTSTR) {
regalloc(&n3, gc.Types[gc.Tptr], res) regalloc(&n3, gc.Types[gc.Tptr], res)
p1 := gins(ppc64.AMOVD, nil, &n3) p1 := gins(ppc64.AMOVD, nil, &n3)
gc.Datastring(nl.Val.U.Sval.S, &p1.From) gc.Datastring(nl.Val.U.Sval, &p1.From)
p1.From.Type = obj.TYPE_ADDR p1.From.Type = obj.TYPE_ADDR
} else if gc.Isslice(nl.Type) || nl.Type.Etype == gc.TSTRING { } else if gc.Isslice(nl.Type) || nl.Type.Etype == gc.TSTRING {
n1 = n3 n1 = n3

View file

@ -532,7 +532,7 @@ func makepartialcall(fn *Node, t0 *Type, meth *Node) *Node {
} }
if spkg == nil { if spkg == nil {
if makepartialcall_gopkg == nil { if makepartialcall_gopkg == nil {
makepartialcall_gopkg = mkpkg(newstrlit("go")) makepartialcall_gopkg = mkpkg("go")
} }
spkg = makepartialcall_gopkg spkg = makepartialcall_gopkg
} }

View file

@ -4,7 +4,10 @@
package gc package gc
import "cmd/internal/obj" import (
"cmd/internal/obj"
"strings"
)
/* /*
* truncate float literal fv to 32-bit or 64-bit precision * truncate float literal fv to 32-bit or 64-bit precision
@ -432,11 +435,10 @@ func tostr(v Val) Val {
if Mpcmpfixfix(v.U.Xval, Minintval[TINT]) < 0 || Mpcmpfixfix(v.U.Xval, Maxintval[TINT]) > 0 { if Mpcmpfixfix(v.U.Xval, Minintval[TINT]) < 0 || Mpcmpfixfix(v.U.Xval, Maxintval[TINT]) > 0 {
Yyerror("overflow in int -> string") Yyerror("overflow in int -> string")
} }
rune_ := uint(Mpgetfix(v.U.Xval)) r := uint(Mpgetfix(v.U.Xval))
s := &Strlit{S: string(rune_)}
v = Val{} v = Val{}
v.Ctype = CTSTR v.Ctype = CTSTR
v.U.Sval = s v.U.Sval = string(r)
case CTFLT: case CTFLT:
Yyerror("no float -> string") Yyerror("no float -> string")
@ -445,7 +447,7 @@ func tostr(v Val) Val {
case CTNIL: case CTNIL:
v = Val{} v = Val{}
v.Ctype = CTSTR v.Ctype = CTSTR
v.U.Sval = new(Strlit) v.U.Sval = ""
} }
return v return v
@ -526,16 +528,15 @@ func evconst(n *Node) {
case OADDSTR: case OADDSTR:
var nr *Node var nr *Node
var nl *Node var nl *Node
var str *Strlit
var l2 *NodeList var l2 *NodeList
for l1 := n.List; l1 != nil; l1 = l1.Next { for l1 := n.List; l1 != nil; l1 = l1.Next {
if Isconst(l1.N, CTSTR) && l1.Next != nil && Isconst(l1.Next.N, CTSTR) { if Isconst(l1.N, CTSTR) && l1.Next != nil && Isconst(l1.Next.N, CTSTR) {
// merge from l1 up to but not including l2 // merge from l1 up to but not including l2
str = new(Strlit) var strs []string
l2 = l1 l2 = l1
for l2 != nil && Isconst(l2.N, CTSTR) { for l2 != nil && Isconst(l2.N, CTSTR) {
nr = l2.N nr = l2.N
str.S += nr.Val.U.Sval.S strs = append(strs, nr.Val.U.Sval)
l2 = l2.Next l2 = l2.Next
} }
@ -543,7 +544,7 @@ func evconst(n *Node) {
*nl = *l1.N *nl = *l1.N
nl.Orig = nl nl.Orig = nl
nl.Val.Ctype = CTSTR nl.Val.Ctype = CTSTR
nl.Val.U.Sval = str nl.Val.U.Sval = strings.Join(strs, "")
l1.N = nl l1.N = nl
l1.Next = l2 l1.Next = l2
} }
@ -1334,7 +1335,7 @@ func defaultlit2(lp **Node, rp **Node, force int) {
} }
func cmpslit(l, r *Node) int { func cmpslit(l, r *Node) int {
return stringsCompare(l.Val.U.Sval.S, r.Val.U.Sval.S) return stringsCompare(l.Val.U.Sval, r.Val.U.Sval)
} }
func Smallintconst(n *Node) bool { func Smallintconst(n *Node) bool {

View file

@ -132,14 +132,14 @@ func testdclstack() {
func redeclare(s *Sym, where string) { func redeclare(s *Sym, where string) {
if s.Lastlineno == 0 { if s.Lastlineno == 0 {
var tmp *Strlit var tmp string
if s.Origpkg != nil { if s.Origpkg != nil {
tmp = s.Origpkg.Path tmp = s.Origpkg.Path
} else { } else {
tmp = s.Pkg.Path tmp = s.Pkg.Path
} }
pkgstr := tmp pkgstr := tmp
Yyerror("%v redeclared %s\n"+"\tprevious declaration during import \"%v\"", Sconv(s, 0), where, Zconv(pkgstr, 0)) Yyerror("%v redeclared %s\n"+"\tprevious declaration during import %q", Sconv(s, 0), where, pkgstr)
} else { } else {
line1 := parserline() line1 := parserline()
line2 := int(s.Lastlineno) line2 := int(s.Lastlineno)
@ -823,7 +823,8 @@ func structfield(n *Node) *Type {
switch n.Val.Ctype { switch n.Val.Ctype {
case CTSTR: case CTSTR:
f.Note = n.Val.U.Sval f.Note = new(string)
*f.Note = n.Val.U.Sval
default: default:
Yyerror("field annotation must be string") Yyerror("field annotation must be string")
@ -1284,7 +1285,7 @@ func methodsym(nsym *Sym, t0 *Type, iface int) *Sym {
if spkg == nil { if spkg == nil {
if methodsym_toppkg == nil { if methodsym_toppkg == nil {
methodsym_toppkg = mkpkg(newstrlit("go")) methodsym_toppkg = mkpkg("go")
} }
spkg = methodsym_toppkg spkg = methodsym_toppkg
} }

View file

@ -217,9 +217,9 @@ type EscState struct {
recursive bool recursive bool
} }
var tags [16]*Strlit var tags [16]*string
func mktag(mask int) *Strlit { func mktag(mask int) *string {
switch mask & EscMask { switch mask & EscMask {
case EscNone, case EscNone,
EscReturn: EscReturn:
@ -235,22 +235,18 @@ func mktag(mask int) *Strlit {
return tags[mask] return tags[mask]
} }
buf := fmt.Sprintf("esc:0x%x", mask) s := fmt.Sprintf("esc:0x%x", mask)
s := newstrlit(buf)
if mask < len(tags) { if mask < len(tags) {
tags[mask] = s tags[mask] = &s
} }
return s return &s
} }
func parsetag(note *Strlit) int { func parsetag(note *string) int {
if note == nil { if note == nil || !strings.HasPrefix(*note, "esc:") {
return EscUnknown return EscUnknown
} }
if !strings.HasPrefix(note.S, "esc:") { em := atoi((*note)[4:])
return EscUnknown
}
em := atoi(note.S[4:])
if em == 0 { if em == 0 {
return EscNone return EscNone
} }
@ -941,7 +937,7 @@ func escassign(e *EscState, dst *Node, src *Node) {
lineno = int32(lno) lineno = int32(lno)
} }
func escassignfromtag(e *EscState, note *Strlit, dsts *NodeList, src *Node) int { func escassignfromtag(e *EscState, note *string, dsts *NodeList, src *Node) int {
var em int var em int
em = parsetag(note) em = parsetag(note)
@ -969,7 +965,7 @@ func escassignfromtag(e *EscState, note *Strlit, dsts *NodeList, src *Node) int
} }
if em != 0 && dsts == nil { if em != 0 && dsts == nil {
Fatal("corrupt esc tag %v or messed up escretval list\n", Zconv(note, 0)) Fatal("corrupt esc tag %q or messed up escretval list\n", note)
} }
return em0 return em0
} }

View file

@ -87,7 +87,7 @@ func dumppkg(p *Pkg) {
if p.Direct == 0 { if p.Direct == 0 {
suffix = " // indirect" suffix = " // indirect"
} }
fmt.Fprintf(bout, "\timport %s \"%v\"%s\n", p.Name, Zconv(p.Path, 0), suffix) fmt.Fprintf(bout, "\timport %s %q%s\n", p.Name, p.Path, suffix)
} }
// Look for anything we need for the inline body // Look for anything we need for the inline body
@ -399,7 +399,7 @@ func dumpexport() {
*/ */
func importsym(s *Sym, op int) *Sym { func importsym(s *Sym, op int) *Sym {
if s.Def != nil && int(s.Def.Op) != op { if s.Def != nil && int(s.Def.Op) != op {
pkgstr := fmt.Sprintf("during import \"%v\"", Zconv(importpkg.Path, 0)) pkgstr := fmt.Sprintf("during import %q", importpkg.Path)
redeclare(s, pkgstr) redeclare(s, pkgstr)
} }
@ -432,24 +432,24 @@ func pkgtype(s *Sym) *Type {
return s.Def.Type return s.Def.Type
} }
func importimport(s *Sym, z *Strlit) { func importimport(s *Sym, path string) {
// Informational: record package name // Informational: record package name
// associated with import path, for use in // associated with import path, for use in
// human-readable messages. // human-readable messages.
if isbadimport(z) { if isbadimport(path) {
errorexit() errorexit()
} }
p := mkpkg(z) p := mkpkg(path)
if p.Name == "" { if p.Name == "" {
p.Name = s.Name p.Name = s.Name
Pkglookup(s.Name, nil).Npkg++ Pkglookup(s.Name, nil).Npkg++
} else if p.Name != s.Name { } else if p.Name != s.Name {
Yyerror("conflicting names %s and %s for package \"%v\"", p.Name, s.Name, Zconv(p.Path, 0)) Yyerror("conflicting names %s and %s for package %q", p.Name, s.Name, p.Path)
} }
if incannedimport == 0 && myimportpath != "" && z.S == myimportpath { if incannedimport == 0 && myimportpath != "" && path == myimportpath {
Yyerror("import \"%v\": package depends on \"%v\" (import cycle)", Zconv(importpkg.Path, 0), Zconv(z, 0)) Yyerror("import %q: package depends on %q (import cycle)", importpkg.Path, path)
errorexit() errorexit()
} }
} }
@ -488,7 +488,7 @@ func importvar(s *Sym, t *Type) {
if Eqtype(t, s.Def.Type) { if Eqtype(t, s.Def.Type) {
return return
} }
Yyerror("inconsistent definition for var %v during import\n\t%v (in \"%v\")\n\t%v (in \"%v\")", Sconv(s, 0), Tconv(s.Def.Type, 0), Zconv(s.Importdef.Path, 0), Tconv(t, 0), Zconv(importpkg.Path, 0)) Yyerror("inconsistent definition for var %v during import\n\t%v (in %q)\n\t%v (in %q)", Sconv(s, 0), Tconv(s.Def.Type, 0), s.Importdef.Path, Tconv(t, 0), importpkg.Path)
} }
n := newname(s) n := newname(s)
@ -518,7 +518,7 @@ func importtype(pt *Type, t *Type) {
declare(n, PEXTERN) declare(n, PEXTERN)
checkwidth(pt) checkwidth(pt)
} else if !Eqtype(pt.Orig, t) { } else if !Eqtype(pt.Orig, t) {
Yyerror("inconsistent definition for type %v during import\n\t%v (in \"%v\")\n\t%v (in \"%v\")", Sconv(pt.Sym, 0), Tconv(pt, obj.FmtLong), Zconv(pt.Sym.Importdef.Path, 0), Tconv(t, obj.FmtLong), Zconv(importpkg.Path, 0)) Yyerror("inconsistent definition for type %v during import\n\t%v (in %q)\n\t%v (in %q)", Sconv(pt.Sym, 0), Tconv(pt, obj.FmtLong), pt.Sym.Importdef.Path, Tconv(t, obj.FmtLong), importpkg.Path)
} }
if Debug['E'] != 0 { if Debug['E'] != 0 {

View file

@ -45,8 +45,6 @@ import (
// Flags: those of %N // Flags: those of %N
// ',' separate items with ',' instead of ';' // ',' separate items with ',' instead of ';'
// //
// %Z Strlit* String literals
//
// In mparith1.c: // In mparith1.c:
// %B Mpint* Big integers // %B Mpint* Big integers
// %F Mpflt* Big floats // %F Mpflt* Big floats
@ -350,7 +348,7 @@ func Vconv(v *Val, flag int) string {
case CTSTR: case CTSTR:
var fp string var fp string
fp += fmt.Sprintf("\"%v\"", Zconv(v.U.Sval, 0)) fp += fmt.Sprintf("%q", v.U.Sval)
return fp return fp
case CTBOOL: case CTBOOL:
@ -370,54 +368,6 @@ func Vconv(v *Val, flag int) string {
return fmt.Sprintf("<ctype=%d>", v.Ctype) return fmt.Sprintf("<ctype=%d>", v.Ctype)
} }
// Fmt "%Z": escaped string literals
func Zconv(sp *Strlit, flag int) string {
if sp == nil {
return "<nil>"
}
// NOTE: Keep in sync with ../ld/go.c:/^Zconv.
s := sp.S
var n int
var fp string
for i := 0; i < len(s); i += n {
var r rune
r, n = utf8.DecodeRuneInString(s[i:])
switch r {
case utf8.RuneError:
if n == 1 {
fp += fmt.Sprintf("\\x%02x", s[i])
break
}
fallthrough
// fall through
default:
if r < ' ' {
fp += fmt.Sprintf("\\x%02x", r)
break
}
fp += string(r)
case '\t':
fp += "\\t"
case '\n':
fp += "\\n"
case '"',
'\\':
fp += `\` + string(r)
case 0xFEFF: // BOM, basically disallowed in source code
fp += "\\uFEFF"
}
}
return fp
}
/* /*
s%,%,\n%g s%,%,\n%g
s%\n+%\n%g s%\n+%\n%g
@ -477,7 +427,7 @@ func symfmt(s *Sym, flag int) string {
// If the name was used by multiple packages, display the full path, // If the name was used by multiple packages, display the full path,
if s.Pkg.Name != "" && Pkglookup(s.Pkg.Name, nil).Npkg > 1 { if s.Pkg.Name != "" && Pkglookup(s.Pkg.Name, nil).Npkg > 1 {
return fmt.Sprintf("\"%v\".%s", Zconv(s.Pkg.Path, 0), s.Name) return fmt.Sprintf("%q.%s", s.Pkg.Path, s.Name)
} }
var fp string var fp string
fp += fmt.Sprintf("%s.%s", s.Pkg.Name, s.Name) fp += fmt.Sprintf("%s.%s", s.Pkg.Name, s.Name)
@ -501,7 +451,7 @@ func symfmt(s *Sym, flag int) string {
Fatal("exporting synthetic symbol %s", s.Name) Fatal("exporting synthetic symbol %s", s.Name)
} }
if s.Pkg != builtinpkg { if s.Pkg != builtinpkg {
return fmt.Sprintf("@\"%v\".%s", Zconv(s.Pkg.Path, 0), s.Name) return fmt.Sprintf("@%q.%s", s.Pkg.Path, s.Name)
} }
} }
} }
@ -516,7 +466,7 @@ func symfmt(s *Sym, flag int) string {
// exportname needs to see the name without the prefix too. // exportname needs to see the name without the prefix too.
if (fmtmode == FExp && !exportname(p)) || fmtmode == FDbg { if (fmtmode == FExp && !exportname(p)) || fmtmode == FDbg {
return fmt.Sprintf("@\"%v\".%s", Zconv(s.Pkg.Path, 0), p) return fmt.Sprintf("@%q.%s", s.Pkg.Path, p)
} }
return p return p
@ -791,8 +741,8 @@ func typefmt(t *Type, flag int) string {
//if(t->funarg) //if(t->funarg)
// fmtstrcpy(fp, "_ "); // fmtstrcpy(fp, "_ ");
//else //else
if t.Embedded != 0 && s.Pkg != nil && len(s.Pkg.Path.S) > 0 { if t.Embedded != 0 && s.Pkg != nil && len(s.Pkg.Path) > 0 {
fp += fmt.Sprintf("@\"%v\".? ", Zconv(s.Pkg.Path, 0)) fp += fmt.Sprintf("@%q.? ", s.Pkg.Path)
} else { } else {
fp += "? " fp += "? "
} }
@ -806,7 +756,7 @@ func typefmt(t *Type, flag int) string {
} }
if flag&obj.FmtShort == 0 /*untyped*/ && t.Note != nil { if flag&obj.FmtShort == 0 /*untyped*/ && t.Note != nil {
fp += fmt.Sprintf(" \"%v\"", Zconv(t.Note, 0)) fp += fmt.Sprintf(" %q", *t.Note)
} }
return fp return fp

View file

@ -55,15 +55,6 @@ const (
MaxStackVarSize = 10 * 1024 * 1024 MaxStackVarSize = 10 * 1024 * 1024
) )
/*
* note this is the representation
* of the compilers string literals,
* it is not the runtime representation
*/
type Strlit struct {
S string
}
const ( const (
Mpscale = 29 Mpscale = 29
Mpprec = 16 Mpprec = 16
@ -98,7 +89,7 @@ type Val struct {
Xval *Mpint Xval *Mpint
Fval *Mpflt Fval *Mpflt
Cval *Mpcplx Cval *Mpcplx
Sval *Strlit Sval string
} }
} }
@ -116,7 +107,7 @@ type Bvec struct {
type Pkg struct { type Pkg struct {
Name string Name string
Path *Strlit Path string
Pathsym *Sym Pathsym *Sym
Prefix string Prefix string
Link *Pkg Link *Pkg
@ -279,7 +270,7 @@ type Type struct {
Width int64 Width int64
Down *Type Down *Type
Outer *Type Outer *Type
Note *Strlit Note *string
Bound int64 Bound int64
Bucket *Type Bucket *Type
Hmap *Type Hmap *Type

View file

@ -251,13 +251,13 @@ import_package:
importpkg.Name = $2.Name; importpkg.Name = $2.Name;
Pkglookup($2.Name, nil).Npkg++; Pkglookup($2.Name, nil).Npkg++;
} else if importpkg.Name != $2.Name { } else if importpkg.Name != $2.Name {
Yyerror("conflicting names %s and %s for package \"%v\"", importpkg.Name, $2.Name, Zconv(importpkg.Path, 0)); Yyerror("conflicting names %s and %s for package %q", importpkg.Name, $2.Name, importpkg.Path);
} }
importpkg.Direct = 1; importpkg.Direct = 1;
importpkg.Safe = curio.importsafe importpkg.Safe = curio.importsafe
if safemode != 0 && !curio.importsafe { if safemode != 0 && !curio.importsafe {
Yyerror("cannot import unsafe package \"%v\"", Zconv(importpkg.Path, 0)); Yyerror("cannot import unsafe package %q", importpkg.Path);
} }
} }
@ -1130,7 +1130,7 @@ hidden_importsym:
{ {
var p *Pkg var p *Pkg
if $2.U.Sval.S == "" { if $2.U.Sval == "" {
p = importpkg; p = importpkg;
} else { } else {
if isbadimport($2.U.Sval) { if isbadimport($2.U.Sval) {
@ -1144,7 +1144,7 @@ hidden_importsym:
{ {
var p *Pkg var p *Pkg
if $2.U.Sval.S == "" { if $2.U.Sval == "" {
p = importpkg; p = importpkg;
} else { } else {
if isbadimport($2.U.Sval) { if isbadimport($2.U.Sval) {
@ -1975,7 +1975,7 @@ hidden_import:
importlist = list(importlist, $2); importlist = list(importlist, $2);
if Debug['E'] > 0 { if Debug['E'] > 0 {
print("import [%v] func %lN \n", Zconv(importpkg.Path, 0), $2); print("import [%q] func %lN \n", importpkg.Path, $2);
if Debug['m'] > 2 && $2.Inl != nil { if Debug['m'] > 2 && $2.Inl != nil {
print("inl body:%+H\n", $2.Inl); print("inl body:%+H\n", $2.Inl);
} }

View file

@ -121,50 +121,50 @@ func Main() {
Ctxt.Bso = &bstdout Ctxt.Bso = &bstdout
bstdout = *obj.Binitw(os.Stdout) bstdout = *obj.Binitw(os.Stdout)
localpkg = mkpkg(newstrlit("")) localpkg = mkpkg("")
localpkg.Prefix = "\"\"" localpkg.Prefix = "\"\""
// pseudo-package, for scoping // pseudo-package, for scoping
builtinpkg = mkpkg(newstrlit("go.builtin")) builtinpkg = mkpkg("go.builtin")
builtinpkg.Prefix = "go.builtin" // not go%2ebuiltin builtinpkg.Prefix = "go.builtin" // not go%2ebuiltin
// pseudo-package, accessed by import "unsafe" // pseudo-package, accessed by import "unsafe"
unsafepkg = mkpkg(newstrlit("unsafe")) unsafepkg = mkpkg("unsafe")
unsafepkg.Name = "unsafe" unsafepkg.Name = "unsafe"
// real package, referred to by generated runtime calls // real package, referred to by generated runtime calls
Runtimepkg = mkpkg(newstrlit("runtime")) Runtimepkg = mkpkg("runtime")
Runtimepkg.Name = "runtime" Runtimepkg.Name = "runtime"
// pseudo-packages used in symbol tables // pseudo-packages used in symbol tables
gostringpkg = mkpkg(newstrlit("go.string")) gostringpkg = mkpkg("go.string")
gostringpkg.Name = "go.string" gostringpkg.Name = "go.string"
gostringpkg.Prefix = "go.string" // not go%2estring gostringpkg.Prefix = "go.string" // not go%2estring
itabpkg = mkpkg(newstrlit("go.itab")) itabpkg = mkpkg("go.itab")
itabpkg.Name = "go.itab" itabpkg.Name = "go.itab"
itabpkg.Prefix = "go.itab" // not go%2eitab itabpkg.Prefix = "go.itab" // not go%2eitab
weaktypepkg = mkpkg(newstrlit("go.weak.type")) weaktypepkg = mkpkg("go.weak.type")
weaktypepkg.Name = "go.weak.type" weaktypepkg.Name = "go.weak.type"
weaktypepkg.Prefix = "go.weak.type" // not go%2eweak%2etype weaktypepkg.Prefix = "go.weak.type" // not go%2eweak%2etype
typelinkpkg = mkpkg(newstrlit("go.typelink")) typelinkpkg = mkpkg("go.typelink")
typelinkpkg.Name = "go.typelink" typelinkpkg.Name = "go.typelink"
typelinkpkg.Prefix = "go.typelink" // not go%2etypelink typelinkpkg.Prefix = "go.typelink" // not go%2etypelink
trackpkg = mkpkg(newstrlit("go.track")) trackpkg = mkpkg("go.track")
trackpkg.Name = "go.track" trackpkg.Name = "go.track"
trackpkg.Prefix = "go.track" // not go%2etrack trackpkg.Prefix = "go.track" // not go%2etrack
typepkg = mkpkg(newstrlit("type")) typepkg = mkpkg("type")
typepkg.Name = "type" typepkg.Name = "type"
@ -238,7 +238,7 @@ func Main() {
startProfile() startProfile()
if flag_race != 0 { if flag_race != 0 {
racepkg = mkpkg(newstrlit("runtime/race")) racepkg = mkpkg("runtime/race")
racepkg.Name = "race" racepkg.Name = "race"
} }
@ -549,14 +549,14 @@ func addidir(dir string) {
} }
// is this path a local name? begins with ./ or ../ or / // is this path a local name? begins with ./ or ../ or /
func islocalname(name *Strlit) bool { func islocalname(name string) bool {
return strings.HasPrefix(name.S, "/") || return strings.HasPrefix(name, "/") ||
Ctxt.Windows != 0 && len(name.S) >= 3 && yy_isalpha(int(name.S[0])) && name.S[1] == ':' && name.S[2] == '/' || Ctxt.Windows != 0 && len(name) >= 3 && yy_isalpha(int(name[0])) && name[1] == ':' && name[2] == '/' ||
strings.HasPrefix(name.S, "./") || name.S == "." || strings.HasPrefix(name, "./") || name == "." ||
strings.HasPrefix(name.S, "../") || name.S == ".." strings.HasPrefix(name, "../") || name == ".."
} }
func findpkg(name *Strlit) bool { func findpkg(name string) bool {
if islocalname(name) { if islocalname(name) {
if safemode != 0 || nolocalimports != 0 { if safemode != 0 || nolocalimports != 0 {
return false return false
@ -565,12 +565,12 @@ func findpkg(name *Strlit) bool {
// 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("%v.a", Zconv(name, 0)) namebuf = fmt.Sprintf("%s.a", name)
if obj.Access(namebuf, 0) >= 0 { if obj.Access(namebuf, 0) >= 0 {
return true return true
} }
namebuf = fmt.Sprintf("%v.%c", Zconv(name, 0), Thearch.Thechar) namebuf = fmt.Sprintf("%s.%c", name, Thearch.Thechar)
if obj.Access(namebuf, 0) >= 0 { if obj.Access(namebuf, 0) >= 0 {
return true return true
} }
@ -582,17 +582,17 @@ func findpkg(name *Strlit) bool {
// as different from "encoding/base64". // as different from "encoding/base64".
var q string var q string
_ = q _ = q
if path.Clean(name.S) != name.S { if path.Clean(name) != name {
Yyerror("non-canonical import path %v (should be %s)", Zconv(name, 0), 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/%v.a", p.dir, Zconv(name, 0)) namebuf = fmt.Sprintf("%s/%s.a", p.dir, name)
if obj.Access(namebuf, 0) >= 0 { if obj.Access(namebuf, 0) >= 0 {
return true return true
} }
namebuf = fmt.Sprintf("%s/%v.%c", p.dir, Zconv(name, 0), Thearch.Thechar) namebuf = fmt.Sprintf("%s/%s.%c", p.dir, name, Thearch.Thechar)
if obj.Access(namebuf, 0) >= 0 { if obj.Access(namebuf, 0) >= 0 {
return true return true
} }
@ -609,11 +609,11 @@ func findpkg(name *Strlit) bool {
suffix = "race" suffix = "race"
} }
namebuf = fmt.Sprintf("%s/pkg/%s_%s%s%s/%v.a", goroot, goos, goarch, suffixsep, suffix, Zconv(name, 0)) namebuf = 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(namebuf, 0) >= 0 {
return true return true
} }
namebuf = fmt.Sprintf("%s/pkg/%s_%s%s%s/%v.%c", goroot, goos, goarch, suffixsep, suffix, Zconv(name, 0), Thearch.Thechar) 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 { if obj.Access(namebuf, 0) >= 0 {
return true return true
} }
@ -623,7 +623,7 @@ func findpkg(name *Strlit) bool {
} }
func fakeimport() { func fakeimport() {
importpkg = mkpkg(newstrlit("fake")) importpkg = mkpkg("fake")
cannedimports("fake.6", "$$\n") cannedimports("fake.6", "$$\n")
} }
@ -634,7 +634,7 @@ func importfile(f *Val, line int) {
return return
} }
if len(f.U.Sval.S) == 0 { if len(f.U.Sval) == 0 {
Yyerror("import path is empty") Yyerror("import path is empty")
fakeimport() fakeimport()
return return
@ -649,17 +649,17 @@ func importfile(f *Val, line int) {
// but we reserve the import path "main" to identify // but we reserve the import path "main" to identify
// the main package, just as we reserve the import // the main package, just as we reserve the import
// path "math" to identify the standard math package. // path "math" to identify the standard math package.
if f.U.Sval.S == "main" { if f.U.Sval == "main" {
Yyerror("cannot import \"main\"") Yyerror("cannot import \"main\"")
errorexit() errorexit()
} }
if myimportpath != "" && f.U.Sval.S == myimportpath { if myimportpath != "" && f.U.Sval == myimportpath {
Yyerror("import \"%v\" while compiling that package (import cycle)", Zconv(f.U.Sval, 0)) Yyerror("import %q while compiling that package (import cycle)", f.U.Sval)
errorexit() errorexit()
} }
if f.U.Sval.S == "unsafe" { if f.U.Sval == "unsafe" {
if safemode != 0 { if safemode != 0 {
Yyerror("cannot import package unsafe") Yyerror("cannot import package unsafe")
errorexit() errorexit()
@ -673,7 +673,7 @@ func importfile(f *Val, line int) {
path_ := f.U.Sval path_ := f.U.Sval
if islocalname(path_) { if islocalname(path_) {
if path_.S[0] == '/' { if path_[0] == '/' {
Yyerror("import path cannot be absolute path") Yyerror("import path cannot be absolute path")
fakeimport() fakeimport()
return return
@ -685,9 +685,9 @@ func importfile(f *Val, line int) {
} }
cleanbuf := prefix cleanbuf := prefix
cleanbuf += "/" cleanbuf += "/"
cleanbuf += path_.S cleanbuf += path_
cleanbuf = path.Clean(cleanbuf) cleanbuf = path.Clean(cleanbuf)
path_ = newstrlit(cleanbuf) path_ = cleanbuf
if isbadimport(path_) { if isbadimport(path_) {
fakeimport() fakeimport()
@ -696,7 +696,7 @@ func importfile(f *Val, line int) {
} }
if !findpkg(path_) { if !findpkg(path_) {
Yyerror("can't find import: \"%v\"", Zconv(f.U.Sval, 0)) Yyerror("can't find import: %q", f.U.Sval)
errorexit() errorexit()
} }
@ -722,7 +722,7 @@ func importfile(f *Val, line int) {
var imp *obj.Biobuf var imp *obj.Biobuf
imp, err = obj.Bopenr(namebuf) imp, err = obj.Bopenr(namebuf)
if err != nil { if err != nil {
Yyerror("can't open import: \"%v\": %v", Zconv(f.U.Sval, 0), err) Yyerror("can't open import: %q: %v", f.U.Sval, err)
errorexit() errorexit()
} }
@ -754,7 +754,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_.S)-2:], -1, 1) // acts as #pragma lib linehist(file[n-len(path_)-2:], -1, 1) // acts as #pragma lib
/* /*
* position the input right * position the input right
@ -788,7 +788,7 @@ func importfile(f *Val, line int) {
return return
} }
Yyerror("no import in \"%v\"", Zconv(f.U.Sval, 0)) Yyerror("no import in %q", f.U.Sval)
unimportfile() unimportfile()
} }
@ -1491,7 +1491,7 @@ caseout:
return LLITERAL return LLITERAL
strlit: strlit:
yylval.val.U.Sval = &Strlit{S: cp.String()} yylval.val.U.Sval = internString(cp.Bytes())
yylval.val.Ctype = CTSTR yylval.val.Ctype = CTSTR
if Debug['x'] != 0 { if Debug['x'] != 0 {
fmt.Printf("lex: string literal\n") fmt.Printf("lex: string literal\n")
@ -1500,6 +1500,18 @@ strlit:
return LLITERAL return LLITERAL
} }
var internedStrings = map[string]string{}
func internString(b []byte) string {
s, ok := internedStrings[string(b)] // string(b) here doesn't allocate
if ok {
return s
}
s = string(b)
internedStrings[s] = s
return s
}
func more(pp *string) bool { func more(pp *string) bool {
p := *pp p := *pp
for p != "" && yy_isspace(int(p[0])) { for p != "" && yy_isspace(int(p[0])) {
@ -3080,21 +3092,21 @@ var yytfix = []struct {
}{"','", "comma"}, }{"','", "comma"},
} }
func pkgnotused(lineno int, path_ *Strlit, name string) { func pkgnotused(lineno int, path string, name string) {
// If the package was imported with a name other than the final // If the package was imported with a name other than the final
// import path element, show it explicitly in the error message. // import path element, show it explicitly in the error message.
// Note that this handles both renamed imports and imports of // Note that this handles both renamed imports and imports of
// packages containing unconventional package declarations. // packages containing unconventional package declarations.
// Note that this uses / always, even on Windows, because Go import // Note that this uses / always, even on Windows, because Go import
// paths always use forward slashes. // paths always use forward slashes.
elem := path_.S elem := path
if i := strings.LastIndex(elem, "/"); i >= 0 { if i := strings.LastIndex(elem, "/"); i >= 0 {
elem = elem[i+1:] elem = elem[i+1:]
} }
if name == "" || elem == name { if name == "" || elem == name {
yyerrorl(int(lineno), "imported and not used: \"%v\"", Zconv(path_, 0)) yyerrorl(int(lineno), "imported and not used: %q", path)
} else { } else {
yyerrorl(int(lineno), "imported and not used: \"%v\" as %s", Zconv(path_, 0), name) yyerrorl(int(lineno), "imported and not used: %q as %s", path, name)
} }
} }

View file

@ -199,12 +199,7 @@ 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 tmp struct {
lit Strlit
buf 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++
@ -215,8 +210,7 @@ func stringsym(s string) *Sym {
// 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.
tmp.lit.S = s namebuf = fmt.Sprintf("%q", s)
namebuf = fmt.Sprintf("\"%v\"", Zconv(&tmp.lit, 0))
pkg = gostringpkg pkg = gostringpkg
} }
@ -313,8 +307,8 @@ func Datastring(s string, a *obj.Addr) {
a.Etype = Simtype[TINT] a.Etype = Simtype[TINT]
} }
func datagostring(sval *Strlit, a *obj.Addr) { func datagostring(sval string, a *obj.Addr) {
sym := stringsym(sval.S) sym := stringsym(sval)
a.Type = obj.TYPE_MEM a.Type = obj.TYPE_MEM
a.Name = obj.NAME_EXTERN a.Name = obj.NAME_EXTERN
a.Sym = Linksym(sym) a.Sym = Linksym(sym)
@ -327,19 +321,13 @@ func dgostringptr(s *Sym, off int, str string) int {
if str == "" { if str == "" {
return duintptr(s, off, 0) return duintptr(s, off, 0)
} }
return dgostrlitptr(s, off, &str)
n := len(str)
lit := new(Strlit)
lit.S = str
lit.S = lit.S[:n]
return dgostrlitptr(s, off, lit)
} }
func dgostrlitptr(s *Sym, off int, lit *Strlit) int { func dgostrlitptr(s *Sym, off int, lit *string) int {
if lit == nil { if lit == nil {
return duintptr(s, off, 0) return duintptr(s, off, 0)
} }
off = int(Rnd(int64(off), int64(Widthptr))) off = int(Rnd(int64(off), int64(Widthptr)))
p := Thearch.Gins(obj.ADATA, nil, nil) p := Thearch.Gins(obj.ADATA, nil, nil)
p.From.Type = obj.TYPE_MEM p.From.Type = obj.TYPE_MEM
@ -348,7 +336,7 @@ func dgostrlitptr(s *Sym, off int, lit *Strlit) int {
p.From.Offset = int64(off) p.From.Offset = int64(off)
p.From3.Type = obj.TYPE_CONST p.From3.Type = obj.TYPE_CONST
p.From3.Offset = int64(Widthptr) p.From3.Offset = int64(Widthptr)
datagostring(lit, &p.To) datagostring(*lit, &p.To)
p.To.Type = obj.TYPE_ADDR p.To.Type = obj.TYPE_ADDR
p.To.Etype = Simtype[TINT] p.To.Etype = Simtype[TINT]
off += Widthptr off += Widthptr
@ -425,18 +413,18 @@ func gdatacomplex(nam *Node, cval *Mpcplx) {
p.To.U.Dval = mpgetflt(&cval.Imag) p.To.U.Dval = mpgetflt(&cval.Imag)
} }
func gdatastring(nam *Node, sval *Strlit) { func gdatastring(nam *Node, sval string) {
var nod1 Node var nod1 Node
p := Thearch.Gins(obj.ADATA, nam, nil) p := Thearch.Gins(obj.ADATA, nam, nil)
Datastring(sval.S, &p.To) Datastring(sval, &p.To)
p.From3.Type = obj.TYPE_CONST p.From3.Type = obj.TYPE_CONST
p.From3.Offset = Types[Tptr].Width p.From3.Offset = Types[Tptr].Width
p.To.Type = obj.TYPE_ADDR p.To.Type = obj.TYPE_ADDR
//print("%P\n", p); //print("%P\n", p);
Nodconst(&nod1, Types[TINT], int64(len(sval.S))) Nodconst(&nod1, Types[TINT], int64(len(sval)))
p = Thearch.Gins(obj.ADATA, nam, &nod1) p = Thearch.Gins(obj.ADATA, nam, &nod1)
p.From3.Type = obj.TYPE_CONST p.From3.Type = obj.TYPE_CONST

View file

@ -991,7 +991,7 @@ func orderexpr(np **Node, order *Order) {
haslit := false haslit := false
for l := n.List; l != nil; l = l.Next { for l := n.List; l != nil; l = l.Next {
hasbyte = hasbyte || l.N.Op == OARRAYBYTESTR hasbyte = hasbyte || l.N.Op == OARRAYBYTESTR
haslit = haslit || l.N.Op == OLITERAL && len(l.N.Val.U.Sval.S) != 0 haslit = haslit || l.N.Op == OLITERAL && len(l.N.Val.U.Sval) != 0
} }
if haslit && hasbyte { if haslit && hasbyte {

View file

@ -28,7 +28,7 @@ func sigcmp(a *Sig, b *Sig) int {
if b.pkg == nil { if b.pkg == nil {
return +1 return +1
} }
return stringsCompare(a.pkg.Path.S, b.pkg.Path.S) return stringsCompare(a.pkg.Path, b.pkg.Path)
} }
func lsort(l *Sig, f func(*Sig, *Sig) int) *Sig { func lsort(l *Sig, f func(*Sig, *Sig) int) *Sig {
@ -473,7 +473,7 @@ func dimportpath(p *Pkg) {
} }
if dimportpath_gopkg == nil { if dimportpath_gopkg == nil {
dimportpath_gopkg = mkpkg(newstrlit("go")) dimportpath_gopkg = mkpkg("go")
dimportpath_gopkg.Name = "go" dimportpath_gopkg.Name = "go"
} }
@ -502,7 +502,7 @@ func dgopkgpath(s *Sym, ot int, pkg *Pkg) int {
var ns *Sym var ns *Sym
if ns == nil { if ns == nil {
ns = Pkglookup("importpath.\"\".", mkpkg(newstrlit("go"))) ns = Pkglookup("importpath.\"\".", mkpkg("go"))
} }
return dsymptr(s, ot, ns, 0) return dsymptr(s, ot, ns, 0)
} }
@ -1280,7 +1280,7 @@ func dumptypestructs() {
if flag_race != 0 { if flag_race != 0 {
dimportpath(racepkg) dimportpath(racepkg)
} }
dimportpath(mkpkg(newstrlit("main"))) dimportpath(mkpkg("main"))
} }
} }

View file

@ -443,7 +443,7 @@ func staticassign(l *Node, r *Node, out **NodeList) bool {
case OSTRARRAYBYTE: case OSTRARRAYBYTE:
if l.Class == PEXTERN && r.Left.Op == OLITERAL { if l.Class == PEXTERN && r.Left.Op == OLITERAL {
sval := r.Left.Val.U.Sval sval := r.Left.Val.U.Sval
slicebytes(l, sval.S, len(sval.S)) slicebytes(l, sval, len(sval))
return true return true
} }
@ -1368,7 +1368,7 @@ func iszero(n *Node) bool {
return true return true
case CTSTR: case CTSTR:
return n.Val.U.Sval == nil || len(n.Val.U.Sval.S) == 0 return n.Val.U.Sval == ""
case CTBOOL: case CTBOOL:
return n.Val.U.Bval == 0 return n.Val.U.Bval == 0

View file

@ -353,7 +353,7 @@ func importdot(opkg *Pkg, pack *Node) {
} }
s1 = Lookup(s.Name) s1 = Lookup(s.Name)
if s1.Def != nil { if s1.Def != nil {
pkgerror = fmt.Sprintf("during import \"%v\"", Zconv(opkg.Path, 0)) pkgerror = fmt.Sprintf("during import %q", opkg.Path)
redeclare(s1, pkgerror) redeclare(s1, pkgerror)
continue continue
} }
@ -368,7 +368,7 @@ func importdot(opkg *Pkg, pack *Node) {
if n == 0 { if n == 0 {
// can't possibly be used - there were no symbols // can't possibly be used - there were no symbols
yyerrorl(int(pack.Lineno), "imported and not used: \"%v\"", Zconv(opkg.Path, 0)) yyerrorl(int(pack.Lineno), "imported and not used: %q", opkg.Path)
} }
} }
@ -642,7 +642,7 @@ func (x methcmp) Less(i, j int) bool {
return k < 0 return k < 0
} }
if !exportname(a.Sym.Name) { if !exportname(a.Sym.Name) {
k := stringsCompare(a.Sym.Pkg.Path.S, b.Sym.Pkg.Path.S) k := stringsCompare(a.Sym.Pkg.Path, b.Sym.Pkg.Path)
if k != 0 { if k != 0 {
return k < 0 return k < 0
} }
@ -941,8 +941,8 @@ func cplxsubtype(et int) int {
return 0 return 0
} }
func eqnote(a, b *Strlit) bool { func eqnote(a, b *string) bool {
return a == b || a != nil && b != nil && a.S == b.S return a == b || a != nil && b != nil && *a == *b
} }
type TypePairList struct { type TypePairList struct {
@ -2454,11 +2454,11 @@ func genwrapper(rcvr *Type, method *Type, newnam *Sym, iface int) {
var v Val var v Val
v.Ctype = CTSTR v.Ctype = CTSTR
v.U.Sval = newstrlit(rcvr.Type.Sym.Pkg.Name) // package name v.U.Sval = rcvr.Type.Sym.Pkg.Name // package name
l = list(l, nodlit(v)) l = list(l, nodlit(v))
v.U.Sval = newstrlit(rcvr.Type.Sym.Name) // type name v.U.Sval = rcvr.Type.Sym.Name // type name
l = list(l, nodlit(v)) l = list(l, nodlit(v))
v.U.Sval = newstrlit(method.Sym.Name) v.U.Sval = method.Sym.Name
l = list(l, nodlit(v)) // method name l = list(l, nodlit(v)) // method name
call := Nod(OCALL, syslook("panicwrap", 0), nil) call := Nod(OCALL, syslook("panicwrap", 0), nil)
call.List = l call.List = l
@ -3583,28 +3583,22 @@ func pathtoprefix(s string) string {
return s return s
} }
func mkpkg(path_ *Strlit) *Pkg { func mkpkg(path string) *Pkg {
h := int(stringhash(path_.S) & uint32(len(phash)-1)) h := int(stringhash(path) & uint32(len(phash)-1))
for p := phash[h]; p != nil; p = p.Link { for p := phash[h]; p != nil; p = p.Link {
if p.Path.S == path_.S { if p.Path == path {
return p return p
} }
} }
p := new(Pkg) p := new(Pkg)
p.Path = path_ p.Path = path
p.Prefix = pathtoprefix(path_.S) p.Prefix = pathtoprefix(path)
p.Link = phash[h] p.Link = phash[h]
phash[h] = p phash[h] = p
return p return p
} }
func newstrlit(s string) *Strlit {
return &Strlit{
S: s,
}
}
func addinit(np **Node, init *NodeList) { func addinit(np **Node, init *NodeList) {
if init == nil { if init == nil {
return return
@ -3632,15 +3626,15 @@ var reservedimports = []string{
"type", "type",
} }
func isbadimport(path_ *Strlit) bool { func isbadimport(path string) bool {
if len(path_.S) != len(path_.S) { if strings.Contains(path, "\x00") {
Yyerror("import path contains NUL") Yyerror("import path contains NUL")
return true return true
} }
for i := 0; i < len(reservedimports); i++ { for i := 0; i < len(reservedimports); i++ {
if path_.S == reservedimports[i] { if path == reservedimports[i] {
Yyerror("import path \"%s\" is reserved and cannot be used", path_.S) Yyerror("import path %q is reserved and cannot be used", path)
return true return true
} }
} }
@ -3649,29 +3643,29 @@ func isbadimport(path_ *Strlit) bool {
_ = s _ = s
var r uint var r uint
_ = r _ = r
for _, r := range path_.S { for _, r := range path {
if r == utf8.RuneError { if r == utf8.RuneError {
Yyerror("import path contains invalid UTF-8 sequence: \"%v\"", Zconv(path_, 0)) Yyerror("import path contains invalid UTF-8 sequence: %q", path)
return true return true
} }
if r < 0x20 || r == 0x7f { if r < 0x20 || r == 0x7f {
Yyerror("import path contains control character: \"%v\"", Zconv(path_, 0)) Yyerror("import path contains control character: %q", path)
return true return true
} }
if r == '\\' { if r == '\\' {
Yyerror("import path contains backslash; use slash: \"%v\"", Zconv(path_, 0)) Yyerror("import path contains backslash; use slash: %q", path)
return true return true
} }
if unicode.IsSpace(rune(r)) { if unicode.IsSpace(rune(r)) {
Yyerror("import path contains space character: \"%v\"", Zconv(path_, 0)) Yyerror("import path contains space character: %q", path)
return true return true
} }
if strings.ContainsRune("!\"#$%&'()*,:;<=>?[]^`{|}", r) { if strings.ContainsRune("!\"#$%&'()*,:;<=>?[]^`{|}", r) {
Yyerror("import path contains invalid character '%c': \"%v\"", r, Zconv(path_, 0)) Yyerror("import path contains invalid character '%c': %q", r, path)
return true return true
} }
} }

View file

@ -802,8 +802,8 @@ reswitch:
Yyerror("invalid %s index %v (index must be non-negative)", why, Nconv(n.Right, 0)) Yyerror("invalid %s index %v (index must be non-negative)", why, Nconv(n.Right, 0))
} else if Isfixedarray(t) && t.Bound > 0 && x >= t.Bound { } else if Isfixedarray(t) && t.Bound > 0 && x >= t.Bound {
Yyerror("invalid array index %v (out of bounds for %d-element array)", Nconv(n.Right, 0), t.Bound) Yyerror("invalid array index %v (out of bounds for %d-element array)", Nconv(n.Right, 0), t.Bound)
} else if Isconst(n.Left, CTSTR) && x >= int64(len(n.Left.Val.U.Sval.S)) { } else if Isconst(n.Left, CTSTR) && x >= int64(len(n.Left.Val.U.Sval)) {
Yyerror("invalid string index %v (out of bounds for %d-byte string)", Nconv(n.Right, 0), len(n.Left.Val.U.Sval.S)) Yyerror("invalid string index %v (out of bounds for %d-byte string)", Nconv(n.Right, 0), len(n.Left.Val.U.Sval))
} else if Mpcmpfixfix(n.Right.Val.U.Xval, Maxintval[TINT]) > 0 { } else if Mpcmpfixfix(n.Right.Val.U.Xval, Maxintval[TINT]) > 0 {
Yyerror("invalid %s index %v (index too large)", why, Nconv(n.Right, 0)) Yyerror("invalid %s index %v (index too large)", why, Nconv(n.Right, 0))
} }
@ -1169,7 +1169,7 @@ reswitch:
case TSTRING: case TSTRING:
if Isconst(l, CTSTR) { if Isconst(l, CTSTR) {
r := Nod(OXXX, nil, nil) r := Nod(OXXX, nil, nil)
Nodconst(r, Types[TINT], int64(len(l.Val.U.Sval.S))) Nodconst(r, Types[TINT], int64(len(l.Val.U.Sval)))
r.Orig = n r.Orig = n
n = r n = r
} }
@ -2135,8 +2135,8 @@ func checksliceindex(l *Node, r *Node, tp *Type) int {
} else if tp != nil && tp.Bound > 0 && Mpgetfix(r.Val.U.Xval) > tp.Bound { } else if tp != nil && tp.Bound > 0 && Mpgetfix(r.Val.U.Xval) > tp.Bound {
Yyerror("invalid slice index %v (out of bounds for %d-element array)", Nconv(r, 0), tp.Bound) Yyerror("invalid slice index %v (out of bounds for %d-element array)", Nconv(r, 0), tp.Bound)
return -1 return -1
} else if Isconst(l, CTSTR) && Mpgetfix(r.Val.U.Xval) > int64(len(l.Val.U.Sval.S)) { } else if Isconst(l, CTSTR) && Mpgetfix(r.Val.U.Xval) > int64(len(l.Val.U.Sval)) {
Yyerror("invalid slice index %v (out of bounds for %d-byte string)", Nconv(r, 0), len(l.Val.U.Sval.S)) Yyerror("invalid slice index %v (out of bounds for %d-byte string)", Nconv(r, 0), len(l.Val.U.Sval))
return -1 return -1
} else if Mpcmpfixfix(r.Val.U.Xval, Maxintval[TINT]) > 0 { } else if Mpcmpfixfix(r.Val.U.Xval, Maxintval[TINT]) > 0 {
Yyerror("invalid slice index %v (index too large)", Nconv(r, 0)) Yyerror("invalid slice index %v (index too large)", Nconv(r, 0))
@ -2705,8 +2705,8 @@ func keydup(n *Node, hash []*Node) {
case CTSTR: case CTSTR:
b = 0 b = 0
s := n.Val.U.Sval.S s := n.Val.U.Sval
for i := len(n.Val.U.Sval.S); i > 0; i-- { for i := len(n.Val.U.Sval); i > 0; i-- {
b = b*PRIME1 + uint32(s[0]) b = b*PRIME1 + uint32(s[0])
s = s[1:] s = s[1:]
} }
@ -3444,7 +3444,7 @@ func stringtoarraylit(np **Node) {
Fatal("stringtoarraylit %N", n) Fatal("stringtoarraylit %N", n)
} }
s := n.Left.Val.U.Sval.S s := n.Left.Val.U.Sval
var l *NodeList var l *NodeList
if n.Type.Type.Etype == TUINT8 { if n.Type.Type.Etype == TUINT8 {
// []byte // []byte

View file

@ -1154,7 +1154,7 @@ func walkexpr(np **Node, init **NodeList) {
Yyerror("index out of bounds") Yyerror("index out of bounds")
} }
} else if Isconst(n.Left, CTSTR) { } else if Isconst(n.Left, CTSTR) {
n.Bounded = bounded(r, int64(len(n.Left.Val.U.Sval.S))) n.Bounded = bounded(r, int64(len(n.Left.Val.U.Sval)))
if Debug['m'] != 0 && n.Bounded && !Isconst(n.Right, CTINT) { if Debug['m'] != 0 && n.Bounded && !Isconst(n.Right, CTINT) {
Warn("index bounds check elided") Warn("index bounds check elided")
} }
@ -1167,7 +1167,7 @@ func walkexpr(np **Node, init **NodeList) {
// an ideal constant. // an ideal constant.
v := Mpgetfix(n.Right.Val.U.Xval) v := Mpgetfix(n.Right.Val.U.Xval)
Nodconst(n, n.Type, int64(n.Left.Val.U.Sval.S[v])) Nodconst(n, n.Type, int64(n.Left.Val.U.Sval[v]))
n.Typecheck = 1 n.Typecheck = 1
} }
} }
@ -1308,7 +1308,7 @@ func walkexpr(np **Node, init **NodeList) {
// comparing the lengths instead will yield the same result // comparing the lengths instead will yield the same result
// without the function call. // without the function call.
case OCMPSTR: case OCMPSTR:
if (Isconst(n.Left, CTSTR) && len(n.Left.Val.U.Sval.S) == 0) || (Isconst(n.Right, CTSTR) && len(n.Right.Val.U.Sval.S) == 0) { if (Isconst(n.Left, CTSTR) && len(n.Left.Val.U.Sval) == 0) || (Isconst(n.Right, CTSTR) && len(n.Right.Val.U.Sval) == 0) {
r := Nod(int(n.Etype), Nod(OLEN, n.Left, nil), Nod(OLEN, n.Right, nil)) r := Nod(int(n.Etype), Nod(OLEN, n.Left, nil), Nod(OLEN, n.Right, nil))
typecheck(&r, Erv) typecheck(&r, Erv)
walkexpr(&r, init) walkexpr(&r, init)
@ -2862,7 +2862,7 @@ func addstr(n *Node, init **NodeList) *Node {
sz := int64(0) sz := int64(0)
for l := n.List; l != nil; l = l.Next { for l := n.List; l != nil; l = l.Next {
if n.Op == OLITERAL { if n.Op == OLITERAL {
sz += int64(len(n.Val.U.Sval.S)) sz += int64(len(n.Val.U.Sval))
} }
} }
@ -4100,7 +4100,7 @@ func usefield(n *Node) {
if field == nil { if field == nil {
Fatal("usefield %v %v without paramfld", Tconv(n.Left.Type, 0), Sconv(n.Right.Sym, 0)) Fatal("usefield %v %v without paramfld", Tconv(n.Left.Type, 0), Sconv(n.Right.Sym, 0))
} }
if field.Note == nil || !strings.Contains(field.Note.S, "go:\"track\"") { if field.Note == nil || !strings.Contains(*field.Note, "go:\"track\"") {
return return
} }

View file

@ -1228,13 +1228,13 @@ yydefault:
importpkg.Name = yyDollar[2].sym.Name importpkg.Name = yyDollar[2].sym.Name
Pkglookup(yyDollar[2].sym.Name, nil).Npkg++ Pkglookup(yyDollar[2].sym.Name, nil).Npkg++
} else if importpkg.Name != yyDollar[2].sym.Name { } else if importpkg.Name != yyDollar[2].sym.Name {
Yyerror("conflicting names %s and %s for package \"%v\"", importpkg.Name, yyDollar[2].sym.Name, Zconv(importpkg.Path, 0)) Yyerror("conflicting names %s and %s for package %q", importpkg.Name, yyDollar[2].sym.Name, importpkg.Path)
} }
importpkg.Direct = 1 importpkg.Direct = 1
importpkg.Safe = curio.importsafe importpkg.Safe = curio.importsafe
if safemode != 0 && !curio.importsafe { if safemode != 0 && !curio.importsafe {
Yyerror("cannot import unsafe package \"%v\"", Zconv(importpkg.Path, 0)) Yyerror("cannot import unsafe package %q", importpkg.Path)
} }
} }
case 20: case 20:
@ -2276,7 +2276,7 @@ yydefault:
{ {
var p *Pkg var p *Pkg
if yyDollar[2].val.U.Sval.S == "" { if yyDollar[2].val.U.Sval == "" {
p = importpkg p = importpkg
} else { } else {
if isbadimport(yyDollar[2].val.U.Sval) { if isbadimport(yyDollar[2].val.U.Sval) {
@ -2292,7 +2292,7 @@ yydefault:
{ {
var p *Pkg var p *Pkg
if yyDollar[2].val.U.Sval.S == "" { if yyDollar[2].val.U.Sval == "" {
p = importpkg p = importpkg
} else { } else {
if isbadimport(yyDollar[2].val.U.Sval) { if isbadimport(yyDollar[2].val.U.Sval) {
@ -3232,7 +3232,7 @@ yydefault:
importlist = list(importlist, yyDollar[2].node) importlist = list(importlist, yyDollar[2].node)
if Debug['E'] > 0 { if Debug['E'] > 0 {
print("import [%v] func %lN \n", Zconv(importpkg.Path, 0), yyDollar[2].node) print("import [%q] func %lN \n", importpkg.Path, yyDollar[2].node)
if Debug['m'] > 2 && yyDollar[2].node.Inl != nil { if Debug['m'] > 2 && yyDollar[2].node.Inl != nil {
print("inl body:%+H\n", yyDollar[2].node.Inl) print("inl body:%+H\n", yyDollar[2].node.Inl)
} }

View file

@ -10,7 +10,6 @@ import (
"fmt" "fmt"
"os" "os"
"strings" "strings"
"unicode/utf8"
) )
// go-specific code shared across loaders (5l, 6l, 8l). // go-specific code shared across loaders (5l, 6l, 8l).
@ -757,49 +756,6 @@ func addexport() {
} }
} }
/* %Z from gc, for quoting import paths */
func Zconv(s string, flag int) string {
// NOTE: Keep in sync with gc Zconv.
var n int
var fp string
for i := 0; i < len(s); i += n {
var r rune
r, n = utf8.DecodeRuneInString(s[i:])
switch r {
case utf8.RuneError:
if n == 1 {
fp += fmt.Sprintf("\\x%02x", s[i])
break
}
fallthrough
// fall through
default:
if r < ' ' {
fp += fmt.Sprintf("\\x%02x", r)
break
}
fp += string(r)
case '\t':
fp += "\\t"
case '\n':
fp += "\\n"
case '"',
'\\':
fp += `\` + string(r)
case 0xFEFF: // BOM, basically disallowed in source code
fp += "\\uFEFF"
}
}
return fp
}
type Pkg struct { type Pkg struct {
mark uint8 mark uint8
checked uint8 checked uint8
@ -835,7 +791,7 @@ func imported(pkg string, import_ string) {
return return
} }
pkg = fmt.Sprintf("\"%v\"", Zconv(pkg, 0)) // turn pkg path into quoted form, freed below pkg = fmt.Sprintf("%q", pkg) // turn pkg path into quoted form, freed below
p := getpkg(pkg) p := getpkg(pkg)
i := getpkg(import_) i := getpkg(import_)
i.impby = append(i.impby, p) i.impby = append(i.impby, p)