Commit e8aa9a53 authored by Daniel Martí's avatar Daniel Martí

cmd/internal/obj: various code cleanups

Mostly replacing C-Style loops with range expressions, but also other
simplifications like the introduction of writeBool and unindenting some
code.

Passes toolstash -cmp on std cmd.

Change-Id: I799bccd4e5d411428dcf122b8588a564a9217e7c
Reviewed-on: https://go-review.googlesource.com/104936
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarMarvin Stenger <marvin.stenger94@gmail.com>
Reviewed-by: default avatarIlya Tocar <ilya.tocar@intel.com>
parent 47427d67
...@@ -56,8 +56,8 @@ func (w *objWriter) addLengths(s *LSym) { ...@@ -56,8 +56,8 @@ func (w *objWriter) addLengths(s *LSym) {
data += len(pc.Pcfile.P) data += len(pc.Pcfile.P)
data += len(pc.Pcline.P) data += len(pc.Pcline.P)
data += len(pc.Pcinline.P) data += len(pc.Pcinline.P)
for i := 0; i < len(pc.Pcdata); i++ { for _, pcd := range pc.Pcdata {
data += len(pc.Pcdata[i].P) data += len(pcd.P)
} }
w.nData += data w.nData += data
...@@ -124,8 +124,8 @@ func WriteObjFile(ctxt *Link, b *bufio.Writer) { ...@@ -124,8 +124,8 @@ func WriteObjFile(ctxt *Link, b *bufio.Writer) {
w.wr.Write(pc.Pcfile.P) w.wr.Write(pc.Pcfile.P)
w.wr.Write(pc.Pcline.P) w.wr.Write(pc.Pcline.P)
w.wr.Write(pc.Pcinline.P) w.wr.Write(pc.Pcinline.P)
for i := 0; i < len(pc.Pcdata); i++ { for _, pcd := range pc.Pcdata {
w.wr.Write(pc.Pcdata[i].P) w.wr.Write(pcd.P)
} }
} }
for _, s := range ctxt.Data { for _, s := range ctxt.Data {
...@@ -175,11 +175,7 @@ func (w *objWriter) writeRef(s *LSym, isPath bool) { ...@@ -175,11 +175,7 @@ func (w *objWriter) writeRef(s *LSym, isPath bool) {
w.writeString(s.Name) w.writeString(s.Name)
} }
// Write "version". // Write "version".
if s.Static() { w.writeBool(s.Static())
w.writeInt(1)
} else {
w.writeInt(0)
}
w.nRefs++ w.nRefs++
s.RefIdx = w.nRefs s.RefIdx = w.nRefs
m[s.Name] = w.nRefs m[s.Name] = w.nRefs
...@@ -188,8 +184,8 @@ func (w *objWriter) writeRef(s *LSym, isPath bool) { ...@@ -188,8 +184,8 @@ func (w *objWriter) writeRef(s *LSym, isPath bool) {
func (w *objWriter) writeRefs(s *LSym) { func (w *objWriter) writeRefs(s *LSym) {
w.writeRef(s, false) w.writeRef(s, false)
w.writeRef(s.Gotype, false) w.writeRef(s.Gotype, false)
for i := range s.R { for _, r := range s.R {
w.writeRef(s.R[i].Sym, false) w.writeRef(r.Sym, false)
} }
if s.Type == objabi.STEXT { if s.Type == objabi.STEXT {
...@@ -309,7 +305,7 @@ func (w *objWriter) writeSym(s *LSym) { ...@@ -309,7 +305,7 @@ func (w *objWriter) writeSym(s *LSym) {
w.writeInt(int64(len(s.R))) w.writeInt(int64(len(s.R)))
var r *Reloc var r *Reloc
for i := 0; i < len(s.R); i++ { for i := range s.R {
r = &s.R[i] r = &s.R[i]
w.writeInt(int64(r.Off)) w.writeInt(int64(r.Off))
w.writeInt(int64(r.Siz)) w.writeInt(int64(r.Siz))
...@@ -324,11 +320,7 @@ func (w *objWriter) writeSym(s *LSym) { ...@@ -324,11 +320,7 @@ func (w *objWriter) writeSym(s *LSym) {
w.writeInt(int64(s.Func.Args)) w.writeInt(int64(s.Func.Args))
w.writeInt(int64(s.Func.Locals)) w.writeInt(int64(s.Func.Locals))
if s.NoSplit() { w.writeBool(s.NoSplit())
w.writeInt(1)
} else {
w.writeInt(0)
}
flags = int64(0) flags = int64(0)
if s.Leaf() { if s.Leaf() {
flags |= 1 flags |= 1
...@@ -365,14 +357,14 @@ func (w *objWriter) writeSym(s *LSym) { ...@@ -365,14 +357,14 @@ func (w *objWriter) writeSym(s *LSym) {
w.writeInt(int64(len(pc.Pcline.P))) w.writeInt(int64(len(pc.Pcline.P)))
w.writeInt(int64(len(pc.Pcinline.P))) w.writeInt(int64(len(pc.Pcinline.P)))
w.writeInt(int64(len(pc.Pcdata))) w.writeInt(int64(len(pc.Pcdata)))
for i := 0; i < len(pc.Pcdata); i++ { for _, pcd := range pc.Pcdata {
w.writeInt(int64(len(pc.Pcdata[i].P))) w.writeInt(int64(len(pcd.P)))
} }
w.writeInt(int64(len(pc.Funcdataoff))) w.writeInt(int64(len(pc.Funcdataoff)))
for i := 0; i < len(pc.Funcdataoff); i++ { for i := range pc.Funcdataoff {
w.writeRefIndex(pc.Funcdata[i]) w.writeRefIndex(pc.Funcdata[i])
} }
for i := 0; i < len(pc.Funcdataoff); i++ { for i := range pc.Funcdataoff {
w.writeInt(pc.Funcdataoff[i]) w.writeInt(pc.Funcdataoff[i])
} }
w.writeInt(int64(len(pc.File))) w.writeInt(int64(len(pc.File)))
...@@ -391,6 +383,14 @@ func (w *objWriter) writeSym(s *LSym) { ...@@ -391,6 +383,14 @@ func (w *objWriter) writeSym(s *LSym) {
} }
} }
func (w *objWriter) writeBool(b bool) {
if b {
w.writeInt(1)
} else {
w.writeInt(0)
}
}
func (w *objWriter) writeInt(sval int64) { func (w *objWriter) writeInt(sval int64) {
var v uint64 var v uint64
uv := (uint64(sval) << 1) ^ uint64(sval>>63) uv := (uint64(sval) << 1) ^ uint64(sval>>63)
...@@ -766,7 +766,7 @@ func (ft *DwarfFixupTable) RegisterChildDIEOffsets(s *LSym, vars []*dwarf.Var, c ...@@ -766,7 +766,7 @@ func (ft *DwarfFixupTable) RegisterChildDIEOffsets(s *LSym, vars []*dwarf.Var, c
// Generate the slice of declOffset's based in vars/coffsets // Generate the slice of declOffset's based in vars/coffsets
doffsets := make([]declOffset, len(coffsets)) doffsets := make([]declOffset, len(coffsets))
for i := 0; i < len(coffsets); i++ { for i := range coffsets {
doffsets[i].dclIdx = vars[i].ChildIndex doffsets[i].dclIdx = vars[i].ChildIndex
doffsets[i].offset = coffsets[i] doffsets[i].offset = coffsets[i]
} }
...@@ -791,9 +791,9 @@ func (ft *DwarfFixupTable) processFixups(slot int, s *LSym) { ...@@ -791,9 +791,9 @@ func (ft *DwarfFixupTable) processFixups(slot int, s *LSym) {
sf := &ft.svec[slot] sf := &ft.svec[slot]
for _, f := range sf.fixups { for _, f := range sf.fixups {
dfound := false dfound := false
for i := 0; i < len(sf.doffsets); i++ { for _, doffset := range sf.doffsets {
if sf.doffsets[i].dclIdx == f.dclidx { if doffset.dclIdx == f.dclidx {
f.refsym.R[f.relidx].Add += int64(sf.doffsets[i].offset) f.refsym.R[f.relidx].Add += int64(doffset.offset)
dfound = true dfound = true
break break
} }
...@@ -833,7 +833,7 @@ func (ft *DwarfFixupTable) Finalize(myimportpath string, trace bool) { ...@@ -833,7 +833,7 @@ func (ft *DwarfFixupTable) Finalize(myimportpath string, trace bool) {
// resulting list (don't want to rely on map ordering here). // resulting list (don't want to rely on map ordering here).
fns := make([]*LSym, len(ft.precursor)) fns := make([]*LSym, len(ft.precursor))
idx := 0 idx := 0
for fn, _ := range ft.precursor { for fn := range ft.precursor {
fns[idx] = fn fns[idx] = fn
idx++ idx++
} }
...@@ -845,8 +845,7 @@ func (ft *DwarfFixupTable) Finalize(myimportpath string, trace bool) { ...@@ -845,8 +845,7 @@ func (ft *DwarfFixupTable) Finalize(myimportpath string, trace bool) {
} }
// Generate any missing abstract functions. // Generate any missing abstract functions.
for i := 0; i < len(fns); i++ { for _, s := range fns {
s := fns[i]
absfn := ft.AbsFuncDwarfSym(s) absfn := ft.AbsFuncDwarfSym(s)
slot, found := ft.symtab[absfn] slot, found := ft.symtab[absfn]
if !found || !ft.svec[slot].defseen { if !found || !ft.svec[slot].defseen {
...@@ -855,8 +854,7 @@ func (ft *DwarfFixupTable) Finalize(myimportpath string, trace bool) { ...@@ -855,8 +854,7 @@ func (ft *DwarfFixupTable) Finalize(myimportpath string, trace bool) {
} }
// Apply fixups. // Apply fixups.
for i := 0; i < len(fns); i++ { for _, s := range fns {
s := fns[i]
absfn := ft.AbsFuncDwarfSym(s) absfn := ft.AbsFuncDwarfSym(s)
slot, found := ft.symtab[absfn] slot, found := ft.symtab[absfn]
if !found { if !found {
......
...@@ -118,10 +118,6 @@ func checkaddr(ctxt *Link, p *Prog, a *Addr) { ...@@ -118,10 +118,6 @@ func checkaddr(ctxt *Link, p *Prog, a *Addr) {
} }
func linkpatch(ctxt *Link, sym *LSym, newprog ProgAlloc) { func linkpatch(ctxt *Link, sym *LSym, newprog ProgAlloc) {
var c int32
var name string
var q *Prog
for p := sym.Func.Text; p != nil; p = p.Link { for p := sym.Func.Text; p != nil; p = p.Link {
checkaddr(ctxt, p, &p.From) checkaddr(ctxt, p, &p.From)
if p.GetFrom3() != nil { if p.GetFrom3() != nil {
...@@ -144,12 +140,12 @@ func linkpatch(ctxt *Link, sym *LSym, newprog ProgAlloc) { ...@@ -144,12 +140,12 @@ func linkpatch(ctxt *Link, sym *LSym, newprog ProgAlloc) {
if p.To.Sym != nil { if p.To.Sym != nil {
continue continue
} }
c = int32(p.To.Offset) q := sym.Func.Text
for q = sym.Func.Text; q != nil; { for q != nil {
if int64(c) == q.Pc { if p.To.Offset == q.Pc {
break break
} }
if q.Forwd != nil && int64(c) >= q.Forwd.Pc { if q.Forwd != nil && p.To.Offset >= q.Forwd.Pc {
q = q.Forwd q = q.Forwd
} else { } else {
q = q.Link q = q.Link
...@@ -157,11 +153,11 @@ func linkpatch(ctxt *Link, sym *LSym, newprog ProgAlloc) { ...@@ -157,11 +153,11 @@ func linkpatch(ctxt *Link, sym *LSym, newprog ProgAlloc) {
} }
if q == nil { if q == nil {
name = "<nil>" name := "<nil>"
if p.To.Sym != nil { if p.To.Sym != nil {
name = p.To.Sym.Name name = p.To.Sym.Name
} }
ctxt.Diag("branch out of range (%#x)\n%v [%s]", uint32(c), p, name) ctxt.Diag("branch out of range (%#x)\n%v [%s]", uint32(p.To.Offset), p, name)
p.To.Type = TYPE_NONE p.To.Type = TYPE_NONE
} }
......
...@@ -118,8 +118,8 @@ func funcpctab(ctxt *Link, dst *Pcdata, func_ *LSym, desc string, valfunc func(* ...@@ -118,8 +118,8 @@ func funcpctab(ctxt *Link, dst *Pcdata, func_ *LSym, desc string, valfunc func(*
if dbg { if dbg {
ctxt.Logf("wrote %d bytes to %p\n", len(dst.P), dst) ctxt.Logf("wrote %d bytes to %p\n", len(dst.P), dst)
for i := 0; i < len(dst.P); i++ { for _, p := range dst.P {
ctxt.Logf(" %02x", dst.P[i]) ctxt.Logf(" %02x", p)
} }
ctxt.Logf("\n") ctxt.Logf("\n")
} }
...@@ -342,10 +342,11 @@ func linkpcln(ctxt *Link, cursym *LSym) { ...@@ -342,10 +342,11 @@ func linkpcln(ctxt *Link, cursym *LSym) {
// funcdata // funcdata
if nfuncdata > 0 { if nfuncdata > 0 {
var i int
for p := cursym.Func.Text; p != nil; p = p.Link { for p := cursym.Func.Text; p != nil; p = p.Link {
if p.As == AFUNCDATA { if p.As != AFUNCDATA {
i = int(p.From.Offset) continue
}
i := int(p.From.Offset)
pcln.Funcdataoff[i] = p.To.Offset pcln.Funcdataoff[i] = p.To.Offset
if p.To.Type != TYPE_CONST { if p.To.Type != TYPE_CONST {
// TODO: Dedup. // TODO: Dedup.
...@@ -354,5 +355,4 @@ func linkpcln(ctxt *Link, cursym *LSym) { ...@@ -354,5 +355,4 @@ func linkpcln(ctxt *Link, cursym *LSym) {
} }
} }
} }
}
} }
...@@ -165,10 +165,6 @@ func (ctxt *Link) CanReuseProgs() bool { ...@@ -165,10 +165,6 @@ func (ctxt *Link) CanReuseProgs() bool {
return !ctxt.Debugasm return !ctxt.Debugasm
} }
func (ctxt *Link) Dconv(a *Addr) string {
return Dconv(nil, a)
}
func Dconv(p *Prog, a *Addr) string { func Dconv(p *Prog, a *Addr) string {
var str string var str string
......
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