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: 's avatarRob Pike <r@golang.org>
parent ebe3d693
......@@ -1544,14 +1544,15 @@ func getlinepragma() int {
}
cp.WriteByte(byte(c))
}
cp = nil
if strings.HasPrefix(lexbuf.String(), "go:cgo_") {
pragcgo(lexbuf.String())
text := lexbuf.String()
if strings.HasPrefix(text, "go:cgo_") {
pragcgo(text)
}
cmd = lexbuf.String()
cmd = text
verb = cmd
if i := strings.Index(verb, " "); i >= 0 {
verb = verb[:i]
......@@ -1630,8 +1631,9 @@ func getlinepragma() int {
if linep == 0 {
return c
}
text := lexbuf.String()
n := 0
for _, c := range lexbuf.String()[linep:] {
for _, c := range text[linep:] {
if c < '0' || c > '9' {
goto out
}
......@@ -1646,15 +1648,7 @@ func getlinepragma() int {
return c
}
// try to avoid allocating file name over and over
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
}
}
name = text[:linep-1]
linehist(name, int32(n), 0)
return c
......
......@@ -11,6 +11,7 @@ import (
func TestLineHist(t *testing.T) {
ctxt := new(Link)
ctxt.Hash = make(map[SymVer]*LSym)
Linklinehist(ctxt, 1, "a.c", 0)
Linklinehist(ctxt, 3, "a.h", 0)
......@@ -22,18 +23,18 @@ func TestLineHist(t *testing.T) {
var expect = []string{
0: "??:0",
1: "/a.c:1",
2: "/a.c:2",
3: "/a.h:1",
4: "/a.h:2",
5: "/a.c:3",
6: "/a.c:4",
7: "/linedir:2",
8: "/linedir:3",
1: "a.c:1",
2: "a.c:2",
3: "a.h:1",
4: "a.h:2",
5: "a.c:3",
6: "a.c:4",
7: "linedir:2",
8: "linedir:3",
9: "??:0",
10: "??:0",
11: "/b.c:1",
12: "/b.c:2",
11: "b.c:1",
12: "b.c:2",
13: "??:0",
14: "??:0",
}
......
......@@ -183,8 +183,8 @@ type Link struct {
Hash map[SymVer]*LSym
Allsym *LSym
Nsymbol int32
Hist *Hist
Ehist *Hist
LineHist LineHist
Imports []string
Plist *Plist
Plast *Plist
Sym_div *LSym
......@@ -580,3 +580,19 @@ const (
)
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) {
Bputc(b, 1) // version
// Emit autolib.
for h := ctxt.Hist; h != nil; h = h.Link {
if h.Offset < 0 {
wrstring(b, h.Name)
}
for _, pkg := range ctxt.Imports {
wrstring(b, pkg)
}
wrstring(b, "")
......
......@@ -142,9 +142,12 @@ func Linknew(arch *LinkArch) *Link {
buf = "/???"
}
buf = filepath.ToSlash(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())
if ctxt.Headtype < 0 {
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