Commit 86154654 authored by Russ Cox's avatar Russ Cox Committed by Gerrit Code Review

cmd/internal/obj: reimplement line history

In addition to possibly being clearer code,
this replaces an O(n) lookup with an O(log n) lookup.

Change-Id: I0a574c536a965a87f7ad6dcdcc30f737bc771cd5
Reviewed-on: https://go-review.googlesource.com/7623Reviewed-by: default avatarRob Pike <r@golang.org>
parent ebe3d693
...@@ -1544,14 +1544,15 @@ func getlinepragma() int { ...@@ -1544,14 +1544,15 @@ func getlinepragma() int {
} }
cp.WriteByte(byte(c)) cp.WriteByte(byte(c))
} }
cp = nil cp = nil
if strings.HasPrefix(lexbuf.String(), "go:cgo_") { text := lexbuf.String()
pragcgo(lexbuf.String())
if strings.HasPrefix(text, "go:cgo_") {
pragcgo(text)
} }
cmd = lexbuf.String() cmd = text
verb = cmd verb = cmd
if i := strings.Index(verb, " "); i >= 0 { if i := strings.Index(verb, " "); i >= 0 {
verb = verb[:i] verb = verb[:i]
...@@ -1630,8 +1631,9 @@ func getlinepragma() int { ...@@ -1630,8 +1631,9 @@ func getlinepragma() int {
if linep == 0 { if linep == 0 {
return c return c
} }
text := lexbuf.String()
n := 0 n := 0
for _, c := range lexbuf.String()[linep:] { for _, c := range text[linep:] {
if c < '0' || c > '9' { if c < '0' || c > '9' {
goto out goto out
} }
...@@ -1646,15 +1648,7 @@ func getlinepragma() int { ...@@ -1646,15 +1648,7 @@ func getlinepragma() int {
return c return c
} }
// try to avoid allocating file name over and over name = text[:linep-1]
name = lexbuf.String()[:linep-1]
for h := Ctxt.Hist; h != nil; h = h.Link {
if h.Name != "" && h.Name == name {
linehist(h.Name, int32(n), 0)
return c
}
}
linehist(name, int32(n), 0) linehist(name, int32(n), 0)
return c return c
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
func TestLineHist(t *testing.T) { func TestLineHist(t *testing.T) {
ctxt := new(Link) ctxt := new(Link)
ctxt.Hash = make(map[SymVer]*LSym)
Linklinehist(ctxt, 1, "a.c", 0) Linklinehist(ctxt, 1, "a.c", 0)
Linklinehist(ctxt, 3, "a.h", 0) Linklinehist(ctxt, 3, "a.h", 0)
...@@ -22,18 +23,18 @@ func TestLineHist(t *testing.T) { ...@@ -22,18 +23,18 @@ func TestLineHist(t *testing.T) {
var expect = []string{ var expect = []string{
0: "??:0", 0: "??:0",
1: "/a.c:1", 1: "a.c:1",
2: "/a.c:2", 2: "a.c:2",
3: "/a.h:1", 3: "a.h:1",
4: "/a.h:2", 4: "a.h:2",
5: "/a.c:3", 5: "a.c:3",
6: "/a.c:4", 6: "a.c:4",
7: "/linedir:2", 7: "linedir:2",
8: "/linedir:3", 8: "linedir:3",
9: "??:0", 9: "??:0",
10: "??:0", 10: "??:0",
11: "/b.c:1", 11: "b.c:1",
12: "/b.c:2", 12: "b.c:2",
13: "??:0", 13: "??:0",
14: "??:0", 14: "??:0",
} }
......
...@@ -183,8 +183,8 @@ type Link struct { ...@@ -183,8 +183,8 @@ type Link struct {
Hash map[SymVer]*LSym Hash map[SymVer]*LSym
Allsym *LSym Allsym *LSym
Nsymbol int32 Nsymbol int32
Hist *Hist LineHist LineHist
Ehist *Hist Imports []string
Plist *Plist Plist *Plist
Plast *Plist Plast *Plist
Sym_div *LSym Sym_div *LSym
...@@ -580,3 +580,19 @@ const ( ...@@ -580,3 +580,19 @@ const (
) )
var linkbasepointer int var linkbasepointer int
/*
* start a new Prog list.
*/
func Linknewplist(ctxt *Link) *Plist {
pl := new(Plist)
*pl = Plist{}
if ctxt.Plist == nil {
ctxt.Plist = pl
} else {
ctxt.Plast.Link = pl
}
ctxt.Plast = pl
return pl
}
This diff is collapsed.
...@@ -306,10 +306,8 @@ func Writeobjdirect(ctxt *Link, b *Biobuf) { ...@@ -306,10 +306,8 @@ func Writeobjdirect(ctxt *Link, b *Biobuf) {
Bputc(b, 1) // version Bputc(b, 1) // version
// Emit autolib. // Emit autolib.
for h := ctxt.Hist; h != nil; h = h.Link { for _, pkg := range ctxt.Imports {
if h.Offset < 0 { wrstring(b, pkg)
wrstring(b, h.Name)
}
} }
wrstring(b, "") wrstring(b, "")
......
...@@ -142,9 +142,12 @@ func Linknew(arch *LinkArch) *Link { ...@@ -142,9 +142,12 @@ func Linknew(arch *LinkArch) *Link {
buf = "/???" buf = "/???"
} }
buf = filepath.ToSlash(buf) buf = filepath.ToSlash(buf)
ctxt.Pathname = buf ctxt.Pathname = buf
ctxt.LineHist.GOROOT = ctxt.Goroot
ctxt.LineHist.GOROOT_FINAL = ctxt.Goroot_final
ctxt.LineHist.Dir = ctxt.Pathname
ctxt.Headtype = headtype(Getgoos()) ctxt.Headtype = headtype(Getgoos())
if ctxt.Headtype < 0 { if ctxt.Headtype < 0 {
log.Fatalf("unknown goos %s", Getgoos()) log.Fatalf("unknown goos %s", Getgoos())
......
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