Skip to content

Commit 4808fc4

Browse files
committed
[dev.inline] cmd/internal/src: replace src.Pos with syntax.Pos
This replaces the src.Pos LineHist-based position tracking with the syntax.Pos implementation and updates all uses. The LineHist table is not used anymore - the respective code is still there but should be removed eventually. CL forthcoming. Passes toolstash -cmp when comparing to the master repo (with the exception of a couple of swapped assembly instructions, likely due to different instruction scheduling because the line-based sorting has changed; though this is won't affect correctness). The sizes of various important compiler data structures have increased significantly (see the various sizes_test.go files); this is probably the reason for an increase of compilation times (to be addressed). Here are the results of compilebench -count 5, run on a "quiet" machine (no apps running besides a terminal): name old time/op new time/op delta Template 256ms ± 1% 280ms ±15% +9.54% (p=0.008 n=5+5) Unicode 132ms ± 1% 132ms ± 1% ~ (p=0.690 n=5+5) GoTypes 891ms ± 1% 917ms ± 2% +2.88% (p=0.008 n=5+5) Compiler 3.84s ± 2% 3.99s ± 2% +3.95% (p=0.016 n=5+5) MakeBash 47.1s ± 1% 47.2s ± 2% ~ (p=0.841 n=5+5) name old user-ns/op new user-ns/op delta Template 309M ± 1% 326M ± 2% +5.18% (p=0.008 n=5+5) Unicode 165M ± 1% 168M ± 4% ~ (p=0.421 n=5+5) GoTypes 1.14G ± 2% 1.18G ± 1% +3.47% (p=0.008 n=5+5) Compiler 5.00G ± 1% 5.16G ± 1% +3.12% (p=0.008 n=5+5) Change-Id: I241c4246cdff627d7ecb95cac23060b38f9775ec Reviewed-on: https://go-review.googlesource.com/34273 Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 2d429f0 commit 4808fc4

38 files changed

+372
-390
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"cmd/asm/internal/flags"
1414
"cmd/asm/internal/lex"
1515
"cmd/internal/obj"
16-
"cmd/internal/src"
1716
"cmd/internal/sys"
1817
)
1918

@@ -62,7 +61,7 @@ func (p *Parser) append(prog *obj.Prog, cond string, doLabel bool) {
6261
}
6362
prog.Pc = p.pc
6463
if *flags.Debug {
65-
fmt.Println(p.histLineNum, prog)
64+
fmt.Println(p.lineNum, prog)
6665
}
6766
if testOut != nil {
6867
fmt.Fprintln(testOut, prog)
@@ -164,7 +163,7 @@ func (p *Parser) asmText(word string, operands [][]lex.Token) {
164163
prog := &obj.Prog{
165164
Ctxt: p.ctxt,
166165
As: obj.ATEXT,
167-
Pos: src.MakePos(p.histLineNum),
166+
Pos: p.pos(),
168167
From: nameAddr,
169168
From3: &obj.Addr{
170169
Type: obj.TYPE_CONST,
@@ -297,7 +296,7 @@ func (p *Parser) asmPCData(word string, operands [][]lex.Token) {
297296
prog := &obj.Prog{
298297
Ctxt: p.ctxt,
299298
As: obj.APCDATA,
300-
Pos: src.MakePos(p.histLineNum),
299+
Pos: p.pos(),
301300
From: key,
302301
To: value,
303302
}
@@ -327,7 +326,7 @@ func (p *Parser) asmFuncData(word string, operands [][]lex.Token) {
327326
prog := &obj.Prog{
328327
Ctxt: p.ctxt,
329328
As: obj.AFUNCDATA,
330-
Pos: src.MakePos(p.histLineNum),
329+
Pos: p.pos(),
331330
From: valueAddr,
332331
To: nameAddr,
333332
}
@@ -342,7 +341,7 @@ func (p *Parser) asmJump(op obj.As, cond string, a []obj.Addr) {
342341
var target *obj.Addr
343342
prog := &obj.Prog{
344343
Ctxt: p.ctxt,
345-
Pos: src.MakePos(p.histLineNum),
344+
Pos: p.pos(),
346345
As: op,
347346
}
348347
switch len(a) {
@@ -470,7 +469,7 @@ func (p *Parser) asmInstruction(op obj.As, cond string, a []obj.Addr) {
470469
// fmt.Printf("%s %+v\n", op, a)
471470
prog := &obj.Prog{
472471
Ctxt: p.ctxt,
473-
Pos: src.MakePos(p.histLineNum),
472+
Pos: p.pos(),
474473
As: op,
475474
}
476475
switch len(a) {

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ import (
2626
// result against a golden file.
2727

2828
func testEndToEnd(t *testing.T, goarch, file string) {
29-
lex.InitHist()
3029
input := filepath.Join("testdata", file+".s")
3130
architecture, ctxt := setArch(goarch)
32-
lexer := lex.NewLexer(input, ctxt)
31+
lexer := lex.NewLexer(input)
3332
parser := NewParser(ctxt, architecture, lexer)
3433
pList := obj.Linknewplist(ctxt)
3534
var ok bool
@@ -264,10 +263,9 @@ var (
264263
)
265264

266265
func testErrors(t *testing.T, goarch, file string) {
267-
lex.InitHist()
268266
input := filepath.Join("testdata", file+".s")
269267
architecture, ctxt := setArch(goarch)
270-
lexer := lex.NewLexer(input, ctxt)
268+
lexer := lex.NewLexer(input)
271269
parser := NewParser(ctxt, architecture, lexer)
272270
pList := obj.Linknewplist(ctxt)
273271
var ok bool

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ import (
1919
"cmd/asm/internal/flags"
2020
"cmd/asm/internal/lex"
2121
"cmd/internal/obj"
22+
"cmd/internal/src"
2223
"cmd/internal/sys"
2324
)
2425

2526
type Parser struct {
2627
lex lex.TokenReader
2728
lineNum int // Line number in source file.
28-
histLineNum int32 // Cumulative line number across source files.
29-
errorLine int32 // (Cumulative) line number of last error.
29+
errorLine int // Line number of last error.
3030
errorCount int // Number of errors.
3131
pc int64 // virtual PC; count of Progs; doesn't advance for GLOBL or DATA.
3232
input []lex.Token
@@ -60,19 +60,19 @@ func NewParser(ctxt *obj.Link, ar *arch.Arch, lexer lex.TokenReader) *Parser {
6060
}
6161
}
6262

63-
// panicOnError is enable when testing to abort execution on the first error
63+
// panicOnError is enabled when testing to abort execution on the first error
6464
// and turn it into a recoverable panic.
6565
var panicOnError bool
6666

6767
func (p *Parser) errorf(format string, args ...interface{}) {
6868
if panicOnError {
6969
panic(fmt.Errorf(format, args...))
7070
}
71-
if p.histLineNum == p.errorLine {
71+
if p.lineNum == p.errorLine {
7272
// Only one error per line.
7373
return
7474
}
75-
p.errorLine = p.histLineNum
75+
p.errorLine = p.lineNum
7676
if p.lex != nil {
7777
// Put file and line information on head of message.
7878
format = "%s:%d: " + format + "\n"
@@ -85,6 +85,10 @@ func (p *Parser) errorf(format string, args ...interface{}) {
8585
}
8686
}
8787

88+
func (p *Parser) pos() src.Pos {
89+
return src.MakePos(p.lex.Base(), uint(p.lineNum), 0)
90+
}
91+
8892
func (p *Parser) Parse() (*obj.Prog, bool) {
8993
for p.line() {
9094
}
@@ -105,7 +109,6 @@ func (p *Parser) line() bool {
105109
// are labeled with this line. Otherwise we complain after we've absorbed
106110
// the terminating newline and the line numbers are off by one in errors.
107111
p.lineNum = p.lex.Line()
108-
p.histLineNum = lex.HistLine()
109112
switch tok {
110113
case '\n', ';':
111114
continue

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ func TestErroneous(t *testing.T) {
5656
for _, test := range tests {
5757
parser.errorCount = 0
5858
parser.lineNum++
59-
parser.histLineNum++
6059
if !parser.pseudo(test.pseudo, tokenize(test.operands)) {
6160
t.Fatalf("Wrong pseudo-instruction: %s", test.pseudo)
6261
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"text/scanner"
1414

1515
"cmd/asm/internal/flags"
16+
"cmd/internal/src"
1617
)
1718

1819
// Input is the main input: a stack of readers and some macro definitions.
@@ -290,7 +291,7 @@ func lookup(args []string, arg string) int {
290291
func (in *Input) invokeMacro(macro *Macro) {
291292
// If the macro has no arguments, just substitute the text.
292293
if macro.args == nil {
293-
in.Push(NewSlice(in.File(), in.Line(), macro.tokens))
294+
in.Push(NewSlice(in.Base(), in.Line(), macro.tokens))
294295
return
295296
}
296297
tok := in.Stack.Next()
@@ -300,7 +301,7 @@ func (in *Input) invokeMacro(macro *Macro) {
300301
in.peekToken = tok
301302
in.peekText = in.text
302303
in.peek = true
303-
in.Push(NewSlice(in.File(), in.Line(), []Token{Make(macroName, macro.name)}))
304+
in.Push(NewSlice(in.Base(), in.Line(), []Token{Make(macroName, macro.name)}))
304305
return
305306
}
306307
actuals := in.argsFor(macro)
@@ -317,7 +318,7 @@ func (in *Input) invokeMacro(macro *Macro) {
317318
}
318319
tokens = append(tokens, substitution...)
319320
}
320-
in.Push(NewSlice(in.File(), in.Line(), tokens))
321+
in.Push(NewSlice(in.Base(), in.Line(), tokens))
321322
}
322323

323324
// argsFor returns a map from formal name to actual value for this argumented macro invocation.
@@ -452,8 +453,8 @@ func (in *Input) line() {
452453
if tok != '\n' {
453454
in.Error("unexpected token at end of #line: ", tok)
454455
}
455-
linkCtxt.LineHist.Update(histLine, file, line)
456-
in.Stack.SetPos(line, file)
456+
pos := src.MakePos(in.Base(), uint(in.Line()), uint(in.Col()))
457+
in.Stack.SetBase(src.NewLinePragmaBase(pos, file, uint(line)))
457458
}
458459

459460
// #undef processing

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

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"strings"
1313
"text/scanner"
1414

15-
"cmd/internal/obj"
15+
"cmd/internal/src"
1616
)
1717

1818
// A ScanToken represents an input item. It is a simple wrapping of rune, as
@@ -57,23 +57,8 @@ func (t ScanToken) String() string {
5757
}
5858
}
5959

60-
var (
61-
// It might be nice if these weren't global.
62-
linkCtxt *obj.Link // The link context for all instructions.
63-
histLine int = 1 // The cumulative count of lines processed.
64-
)
65-
66-
// HistLine reports the cumulative source line number of the token,
67-
// for use in the Prog structure for the linker. (It's always handling the
68-
// instruction from the current lex line.)
69-
// It returns int32 because that's what type ../asm prefers.
70-
func HistLine() int32 {
71-
return int32(histLine)
72-
}
73-
7460
// NewLexer returns a lexer for the named file and the given link context.
75-
func NewLexer(name string, ctxt *obj.Link) TokenReader {
76-
linkCtxt = ctxt
61+
func NewLexer(name string) TokenReader {
7762
input := NewInput(name)
7863
fd, err := os.Open(name)
7964
if err != nil {
@@ -83,16 +68,11 @@ func NewLexer(name string, ctxt *obj.Link) TokenReader {
8368
return input
8469
}
8570

86-
// InitHist sets the line count to 1, for reproducible testing.
87-
func InitHist() {
88-
histLine = 1
89-
}
90-
9171
// The other files in this directory each contain an implementation of TokenReader.
9272

9373
// A TokenReader is like a reader, but returns lex tokens of type Token. It also can tell you what
9474
// the text of the most recently returned token is, and where it was found.
95-
// The underlying scanner elides all spaces except newline, so the input looks like a stream of
75+
// The underlying scanner elides all spaces except newline, so the input looks like a stream of
9676
// Tokens; original spacing is lost but we don't need it.
9777
type TokenReader interface {
9878
// Next returns the next token.
@@ -102,12 +82,14 @@ type TokenReader interface {
10282
Text() string
10383
// File reports the source file name of the token.
10484
File() string
85+
// Base reports the position base of the token.
86+
Base() *src.PosBase
87+
// SetBase sets the position base.
88+
SetBase(*src.PosBase)
10589
// Line reports the source line number of the token.
10690
Line() int
10791
// Col reports the source column number of the token.
10892
Col() int
109-
// SetPos sets the file and line number.
110-
SetPos(line int, file string)
11193
// Close does any teardown required.
11294
Close()
11395
}

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

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,26 @@
44

55
package lex
66

7-
import "text/scanner"
7+
import (
8+
"text/scanner"
9+
10+
"cmd/internal/src"
11+
)
812

913
// A Slice reads from a slice of Tokens.
1014
type Slice struct {
11-
tokens []Token
12-
fileName string
13-
line int
14-
pos int
15+
tokens []Token
16+
base *src.PosBase
17+
line int
18+
pos int
1519
}
1620

17-
func NewSlice(fileName string, line int, tokens []Token) *Slice {
21+
func NewSlice(base *src.PosBase, line int, tokens []Token) *Slice {
1822
return &Slice{
19-
tokens: tokens,
20-
fileName: fileName,
21-
line: line,
22-
pos: -1, // Next will advance to zero.
23+
tokens: tokens,
24+
base: base,
25+
line: line,
26+
pos: -1, // Next will advance to zero.
2327
}
2428
}
2529

@@ -36,7 +40,17 @@ func (s *Slice) Text() string {
3640
}
3741

3842
func (s *Slice) File() string {
39-
return s.fileName
43+
return s.base.Filename()
44+
}
45+
46+
func (s *Slice) Base() *src.PosBase {
47+
return s.base
48+
}
49+
50+
func (s *Slice) SetBase(base *src.PosBase) {
51+
// Cannot happen because we only have slices of already-scanned text,
52+
// but be prepared.
53+
s.base = base
4054
}
4155

4256
func (s *Slice) Line() int {
@@ -56,12 +70,5 @@ func (s *Slice) Col() int {
5670
return s.pos
5771
}
5872

59-
func (s *Slice) SetPos(line int, file string) {
60-
// Cannot happen because we only have slices of already-scanned
61-
// text, but be prepared.
62-
s.line = line
63-
s.fileName = file
64-
}
65-
6673
func (s *Slice) Close() {
6774
}

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
package lex
66

7-
import "text/scanner"
7+
import (
8+
"text/scanner"
9+
10+
"cmd/internal/src"
11+
)
812

913
// A Stack is a stack of TokenReaders. As the top TokenReader hits EOF,
1014
// it resumes reading the next one down.
@@ -34,7 +38,15 @@ func (s *Stack) Text() string {
3438
}
3539

3640
func (s *Stack) File() string {
37-
return s.tr[len(s.tr)-1].File()
41+
return s.Base().Filename()
42+
}
43+
44+
func (s *Stack) Base() *src.PosBase {
45+
return s.tr[len(s.tr)-1].Base()
46+
}
47+
48+
func (s *Stack) SetBase(base *src.PosBase) {
49+
s.tr[len(s.tr)-1].SetBase(base)
3850
}
3951

4052
func (s *Stack) Line() int {
@@ -45,9 +57,5 @@ func (s *Stack) Col() int {
4557
return s.tr[len(s.tr)-1].Col()
4658
}
4759

48-
func (s *Stack) SetPos(line int, file string) {
49-
s.tr[len(s.tr)-1].SetPos(line, file)
50-
}
51-
5260
func (s *Stack) Close() { // Unused.
5361
}

0 commit comments

Comments
 (0)