Skip to content

Commit eaae2d4

Browse files
mdempskygopherbot
authored andcommitted
cmd: simplify some handling of package paths
We have obj.Link.Pkgpath, so we don't need to pass it redundantly in places where we already have an *obj.Link. Also, renaming the parser's "compilingRuntime" field to "allowABI", to match the "AllowAsmABI" name used by objabi.LookupPkgSpecial. Finally, push the handling of GOEXPERIMENT_* flags up to cmd/asm's main entry point, by simply appending them to flags.D. Change-Id: I6ada134522b0cbc90d35bcb145fbe045338fefb7 Reviewed-on: https://go-review.googlesource.com/c/go/+/523297 Run-TryBot: Matthew Dempsky <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Auto-Submit: Matthew Dempsky <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent f7f1c4f commit eaae2d4

File tree

16 files changed

+85
-91
lines changed

16 files changed

+85
-91
lines changed

src/cmd/asm/internal/asm/endtoend_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func testEndToEnd(t *testing.T, goarch, file string) {
2929
input := filepath.Join("testdata", file+".s")
3030
architecture, ctxt := setArch(goarch)
3131
architecture.Init(ctxt)
32-
lexer := lex.NewLexer(input, false)
33-
parser := NewParser(ctxt, architecture, lexer, false)
32+
lexer := lex.NewLexer(input)
33+
parser := NewParser(ctxt, architecture, lexer)
3434
pList := new(obj.Plist)
3535
var ok bool
3636
testOut = new(strings.Builder) // The assembler writes test output to this buffer.
@@ -191,7 +191,7 @@ Diff:
191191
t.Errorf(format, args...)
192192
ok = false
193193
}
194-
obj.Flushplist(ctxt, pList, nil, "")
194+
obj.Flushplist(ctxt, pList, nil)
195195

196196
for p := top; p != nil; p = p.Link {
197197
if p.As == obj.ATEXT {
@@ -278,8 +278,8 @@ func testErrors(t *testing.T, goarch, file string, flags ...string) {
278278
input := filepath.Join("testdata", file+".s")
279279
architecture, ctxt := setArch(goarch)
280280
architecture.Init(ctxt)
281-
lexer := lex.NewLexer(input, false)
282-
parser := NewParser(ctxt, architecture, lexer, false)
281+
lexer := lex.NewLexer(input)
282+
parser := NewParser(ctxt, architecture, lexer)
283283
pList := new(obj.Plist)
284284
var ok bool
285285
ctxt.Bso = bufio.NewWriter(os.Stdout)
@@ -305,7 +305,7 @@ func testErrors(t *testing.T, goarch, file string, flags ...string) {
305305
}
306306
}
307307
pList.Firstpc, ok = parser.Parse()
308-
obj.Flushplist(ctxt, pList, nil, "")
308+
obj.Flushplist(ctxt, pList, nil)
309309
if ok && !failed {
310310
t.Errorf("asm: %s had no errors", file)
311311
}

src/cmd/asm/internal/asm/expr_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ var exprTests = []exprTest{
5757
}
5858

5959
func TestExpr(t *testing.T) {
60-
p := NewParser(nil, nil, nil, false) // Expression evaluation uses none of these fields of the parser.
60+
p := NewParser(nil, nil, nil) // Expression evaluation uses none of these fields of the parser.
6161
for i, test := range exprTests {
6262
p.start(lex.Tokenize(test.input))
6363
result := int64(p.expr())
@@ -113,7 +113,7 @@ func TestBadExpr(t *testing.T) {
113113
}
114114

115115
func runBadTest(i int, test badExprTest, t *testing.T) (err error) {
116-
p := NewParser(nil, nil, nil, false) // Expression evaluation uses none of these fields of the parser.
116+
p := NewParser(nil, nil, nil) // Expression evaluation uses none of these fields of the parser.
117117
p.start(lex.Tokenize(test.input))
118118
return tryParse(t, func() {
119119
p.expr()

src/cmd/asm/internal/asm/line_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func testBadInstParser(t *testing.T, goarch string, tests []badInstTest) {
3939
for i, test := range tests {
4040
arch, ctxt := setArch(goarch)
4141
tokenizer := lex.NewTokenizer("", strings.NewReader(test.input+"\n"), nil)
42-
parser := NewParser(ctxt, arch, tokenizer, false)
42+
parser := NewParser(ctxt, arch, tokenizer)
4343

4444
err := tryParse(t, func() {
4545
parser.Parse()

src/cmd/asm/internal/asm/operand_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func setArch(goarch string) (*arch.Arch, *obj.Link) {
2828

2929
func newParser(goarch string) *Parser {
3030
architecture, ctxt := setArch(goarch)
31-
return NewParser(ctxt, architecture, nil, false)
31+
return NewParser(ctxt, architecture, nil)
3232
}
3333

3434
// tryParse executes parse func in panicOnError=true context.
@@ -76,7 +76,7 @@ func testOperandParser(t *testing.T, parser *Parser, tests []operandTest) {
7676
addr := obj.Addr{}
7777
parser.operand(&addr)
7878
var result string
79-
if parser.compilingRuntime {
79+
if parser.allowABI {
8080
result = obj.DconvWithABIDetail(&emptyProg, &addr)
8181
} else {
8282
result = obj.Dconv(&emptyProg, &addr)
@@ -91,7 +91,7 @@ func TestAMD64OperandParser(t *testing.T) {
9191
parser := newParser("amd64")
9292
testOperandParser(t, parser, amd64OperandTests)
9393
testBadOperandParser(t, parser, amd64BadOperandTests)
94-
parser.compilingRuntime = true
94+
parser.allowABI = true
9595
testOperandParser(t, parser, amd64RuntimeOperandTests)
9696
testBadOperandParser(t, parser, amd64BadOperandRuntimeTests)
9797
}

src/cmd/asm/internal/asm/parse.go

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,48 @@ import (
2121
"cmd/internal/obj"
2222
"cmd/internal/obj/arm64"
2323
"cmd/internal/obj/x86"
24+
"cmd/internal/objabi"
2425
"cmd/internal/src"
2526
"cmd/internal/sys"
2627
)
2728

2829
type Parser struct {
29-
lex lex.TokenReader
30-
lineNum int // Line number in source file.
31-
errorLine int // Line number of last error.
32-
errorCount int // Number of errors.
33-
sawCode bool // saw code in this file (as opposed to comments and blank lines)
34-
pc int64 // virtual PC; count of Progs; doesn't advance for GLOBL or DATA.
35-
input []lex.Token
36-
inputPos int
37-
pendingLabels []string // Labels to attach to next instruction.
38-
labels map[string]*obj.Prog
39-
toPatch []Patch
40-
addr []obj.Addr
41-
arch *arch.Arch
42-
ctxt *obj.Link
43-
firstProg *obj.Prog
44-
lastProg *obj.Prog
45-
dataAddr map[string]int64 // Most recent address for DATA for this symbol.
46-
isJump bool // Instruction being assembled is a jump.
47-
compilingRuntime bool
48-
errorWriter io.Writer
30+
lex lex.TokenReader
31+
lineNum int // Line number in source file.
32+
errorLine int // Line number of last error.
33+
errorCount int // Number of errors.
34+
sawCode bool // saw code in this file (as opposed to comments and blank lines)
35+
pc int64 // virtual PC; count of Progs; doesn't advance for GLOBL or DATA.
36+
input []lex.Token
37+
inputPos int
38+
pendingLabels []string // Labels to attach to next instruction.
39+
labels map[string]*obj.Prog
40+
toPatch []Patch
41+
addr []obj.Addr
42+
arch *arch.Arch
43+
ctxt *obj.Link
44+
firstProg *obj.Prog
45+
lastProg *obj.Prog
46+
dataAddr map[string]int64 // Most recent address for DATA for this symbol.
47+
isJump bool // Instruction being assembled is a jump.
48+
allowABI bool // Whether ABI selectors are allowed.
49+
errorWriter io.Writer
4950
}
5051

5152
type Patch struct {
5253
addr *obj.Addr
5354
label string
5455
}
5556

56-
func NewParser(ctxt *obj.Link, ar *arch.Arch, lexer lex.TokenReader, compilingRuntime bool) *Parser {
57+
func NewParser(ctxt *obj.Link, ar *arch.Arch, lexer lex.TokenReader) *Parser {
5758
return &Parser{
58-
ctxt: ctxt,
59-
arch: ar,
60-
lex: lexer,
61-
labels: make(map[string]*obj.Prog),
62-
dataAddr: make(map[string]int64),
63-
errorWriter: os.Stderr,
64-
compilingRuntime: compilingRuntime,
59+
ctxt: ctxt,
60+
arch: ar,
61+
lex: lexer,
62+
labels: make(map[string]*obj.Prog),
63+
dataAddr: make(map[string]int64),
64+
errorWriter: os.Stderr,
65+
allowABI: ctxt != nil && objabi.LookupPkgSpecial(ctxt.Pkgpath).AllowAsmABI,
6566
}
6667
}
6768

@@ -864,7 +865,7 @@ func (p *Parser) symRefAttrs(name string, issueError bool) (bool, obj.ABI) {
864865
isStatic = true
865866
} else if tok == scanner.Ident {
866867
abistr := p.get(scanner.Ident).String()
867-
if !p.compilingRuntime {
868+
if !p.allowABI {
868869
if issueError {
869870
p.errorf("ABI selector only permitted when compiling runtime, reference was to %q", name)
870871
}

src/cmd/asm/internal/asm/pseudo_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ func TestErroneous(t *testing.T) {
6464
}
6565

6666
testcats := []struct {
67-
compilingRuntime bool
68-
tests []errtest
67+
allowABI bool
68+
tests []errtest
6969
}{
7070
{
71-
compilingRuntime: false,
72-
tests: nonRuntimeTests,
71+
allowABI: false,
72+
tests: nonRuntimeTests,
7373
},
7474
{
75-
compilingRuntime: true,
76-
tests: runtimeTests,
75+
allowABI: true,
76+
tests: runtimeTests,
7777
},
7878
}
7979

@@ -85,7 +85,7 @@ func TestErroneous(t *testing.T) {
8585

8686
for _, cat := range testcats {
8787
for _, test := range cat.tests {
88-
parser.compilingRuntime = cat.compilingRuntime
88+
parser.allowABI = cat.allowABI
8989
parser.errorCount = 0
9090
parser.lineNum++
9191
if !parser.pseudo(test.pseudo, tokenize(test.operands)) {

src/cmd/asm/internal/lex/input.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package lex
66

77
import (
88
"fmt"
9-
"internal/buildcfg"
109
"os"
1110
"path/filepath"
1211
"strconv"
@@ -34,33 +33,18 @@ type Input struct {
3433
}
3534

3635
// NewInput returns an Input from the given path.
37-
func NewInput(name string, compilingRuntime bool) *Input {
36+
func NewInput(name string) *Input {
3837
return &Input{
3938
// include directories: look in source dir, then -I directories.
4039
includes: append([]string{filepath.Dir(name)}, flags.I...),
4140
beginningOfLine: true,
42-
macros: predefine(flags.D, compilingRuntime),
41+
macros: predefine(flags.D),
4342
}
4443
}
4544

4645
// predefine installs the macros set by the -D flag on the command line.
47-
func predefine(defines flags.MultiFlag, compilingRuntime bool) map[string]*Macro {
46+
func predefine(defines flags.MultiFlag) map[string]*Macro {
4847
macros := make(map[string]*Macro)
49-
50-
// Set macros for GOEXPERIMENTs so we can easily switch
51-
// runtime assembly code based on them.
52-
if compilingRuntime {
53-
for _, exp := range buildcfg.Experiment.Enabled() {
54-
// Define macro.
55-
name := "GOEXPERIMENT_" + exp
56-
macros[name] = &Macro{
57-
name: name,
58-
args: nil,
59-
tokens: Tokenize("1"),
60-
}
61-
}
62-
}
63-
6448
for _, name := range defines {
6549
value := "1"
6650
i := strings.IndexRune(name, '=')

src/cmd/asm/internal/lex/lex.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ func (t ScanToken) String() string {
6060
}
6161

6262
// NewLexer returns a lexer for the named file and the given link context.
63-
func NewLexer(name string, compilingRuntime bool) TokenReader {
64-
input := NewInput(name, compilingRuntime)
63+
func NewLexer(name string) TokenReader {
64+
input := NewInput(name)
6565
fd, err := os.Open(name)
6666
if err != nil {
6767
log.Fatalf("%s\n", err)

src/cmd/asm/internal/lex/lex_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ var lexTests = []lexTest{
258258

259259
func TestLex(t *testing.T) {
260260
for _, test := range lexTests {
261-
input := NewInput(test.name, false)
261+
input := NewInput(test.name)
262262
input.Push(NewTokenizer(test.name, strings.NewReader(test.input), nil))
263263
result := drain(input)
264264
if result != test.output {
@@ -328,7 +328,7 @@ var badLexTests = []badLexTest{
328328

329329
func TestBadLex(t *testing.T) {
330330
for _, test := range badLexTests {
331-
input := NewInput(test.error, false)
331+
input := NewInput(test.error)
332332
input.Push(NewTokenizer(test.error, strings.NewReader(test.input), nil))
333333
err := firstError(input)
334334
if err == nil {

src/cmd/asm/main.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ func main() {
3535
if architecture == nil {
3636
log.Fatalf("unrecognized architecture %s", GOARCH)
3737
}
38-
compilingRuntime := objabi.LookupPkgSpecial(*flags.Importpath).AllowAsmABI
39-
4038
ctxt := obj.Linknew(architecture.LinkArch)
4139
ctxt.Debugasm = flags.PrintOut
4240
ctxt.Debugvlog = flags.DebugV
@@ -77,12 +75,19 @@ func main() {
7775
fmt.Fprintf(buf, "!\n")
7876
}
7977

78+
// Set macros for GOEXPERIMENTs so we can easily switch
79+
// runtime assembly code based on them.
80+
if objabi.LookupPkgSpecial(ctxt.Pkgpath).AllowAsmABI {
81+
for _, exp := range buildcfg.Experiment.Enabled() {
82+
flags.D = append(flags.D, "GOEXPERIMENT_"+exp)
83+
}
84+
}
85+
8086
var ok, diag bool
8187
var failedFile string
8288
for _, f := range flag.Args() {
83-
lexer := lex.NewLexer(f, compilingRuntime)
84-
parser := asm.NewParser(ctxt, architecture, lexer,
85-
compilingRuntime)
89+
lexer := lex.NewLexer(f)
90+
parser := asm.NewParser(ctxt, architecture, lexer)
8691
ctxt.DiagFunc = func(format string, args ...interface{}) {
8792
diag = true
8893
log.Printf(format, args...)
@@ -94,7 +99,7 @@ func main() {
9499
pList.Firstpc, ok = parser.Parse()
95100
// reports errors to parser.Errorf
96101
if ok {
97-
obj.Flushplist(ctxt, pList, nil, *flags.Importpath)
102+
obj.Flushplist(ctxt, pList, nil)
98103
}
99104
}
100105
if !ok {

src/cmd/compile/internal/dwarfgen/dwinl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func AbstractFunc(fn *obj.LSym) {
217217
if base.Debug.DwarfInl != 0 {
218218
base.Ctxt.Logf("DwarfAbstractFunc(%v)\n", fn.Name)
219219
}
220-
base.Ctxt.DwarfAbstractFunc(ifn, fn, base.Ctxt.Pkgpath)
220+
base.Ctxt.DwarfAbstractFunc(ifn, fn)
221221
}
222222

223223
// Undo any versioning performed when a name was written

src/cmd/compile/internal/gc/obj.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func dumpGlobal(n *ir.Name) {
158158
if n.CoverageCounter() || n.CoverageAuxVar() || n.Linksym().Static() {
159159
return
160160
}
161-
base.Ctxt.DwarfGlobal(base.Ctxt.Pkgpath, types.TypeSymName(n.Type()), n.Linksym())
161+
base.Ctxt.DwarfGlobal(types.TypeSymName(n.Type()), n.Linksym())
162162
}
163163

164164
func dumpGlobalConst(n *ir.Name) {
@@ -186,7 +186,7 @@ func dumpGlobalConst(n *ir.Name) {
186186
// that type so the linker knows about it. See issue 51245.
187187
_ = reflectdata.TypeLinksym(t)
188188
}
189-
base.Ctxt.DwarfIntConst(base.Ctxt.Pkgpath, n.Sym().Name, types.TypeSymName(t), ir.IntVal(t, v))
189+
base.Ctxt.DwarfIntConst(n.Sym().Name, types.TypeSymName(t), ir.IntVal(t, v))
190190
}
191191

192192
// addGCLocals adds gcargs, gclocals, gcregs, and stack object symbols to Ctxt.Data.

src/cmd/compile/internal/objw/prog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (pp *Progs) NewProg() *obj.Prog {
109109
// Flush converts from pp to machine code.
110110
func (pp *Progs) Flush() {
111111
plist := &obj.Plist{Firstpc: pp.Text, Curfn: pp.CurFunc}
112-
obj.Flushplist(base.Ctxt, plist, pp.NewProg, base.Ctxt.Pkgpath)
112+
obj.Flushplist(base.Ctxt, plist, pp.NewProg)
113113
}
114114

115115
// Free clears pp and any associated resources.

src/cmd/internal/dwarf/dwarf.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,6 @@ func putPrunedScopes(ctxt Context, s *FnState, fnabbrev int) error {
12331233
// DIE (as a space-saving measure, so that name/type etc doesn't have
12341234
// to be repeated for each inlined copy).
12351235
func PutAbstractFunc(ctxt Context, s *FnState) error {
1236-
12371236
if logDwarf {
12381237
ctxt.Logf("PutAbstractFunc(%v)\n", s.Absfn)
12391238
}

0 commit comments

Comments
 (0)