Commit 59413d34 authored by Daniel Martí's avatar Daniel Martí

all: unindent some big chunks of code

Found with mvdan.cc/unindent. Prioritized the ones with the biggest wins
for now.

Change-Id: I2b032e45cdd559fc9ed5b1ee4c4de42c4c92e07b
Reviewed-on: https://go-review.googlesource.com/56470
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent b73d46de
...@@ -285,38 +285,39 @@ func Fields(s []byte) [][]byte { ...@@ -285,38 +285,39 @@ func Fields(s []byte) [][]byte {
wasSpace = isSpace wasSpace = isSpace
} }
if setBits < utf8.RuneSelf { // ASCII fast path if setBits >= utf8.RuneSelf {
a := make([][]byte, n) // Some runes in the input slice are not ASCII.
na := 0 return FieldsFunc(s, unicode.IsSpace)
fieldStart := 0 }
i := 0
// Skip spaces in the front of the input. // ASCII fast path
for i < len(s) && asciiSpace[s[i]] != 0 { a := make([][]byte, n)
na := 0
fieldStart := 0
i := 0
// Skip spaces in the front of the input.
for i < len(s) && asciiSpace[s[i]] != 0 {
i++
}
fieldStart = i
for i < len(s) {
if asciiSpace[s[i]] == 0 {
i++ i++
continue
} }
fieldStart = i a[na] = s[fieldStart:i]
for i < len(s) { na++
if asciiSpace[s[i]] == 0 { i++
i++ // Skip spaces in between fields.
continue for i < len(s) && asciiSpace[s[i]] != 0 {
}
a[na] = s[fieldStart:i]
na++
i++ i++
// Skip spaces in between fields.
for i < len(s) && asciiSpace[s[i]] != 0 {
i++
}
fieldStart = i
} }
if fieldStart < len(s) { // Last field might end at EOF. fieldStart = i
a[na] = s[fieldStart:]
}
return a
} }
if fieldStart < len(s) { // Last field might end at EOF.
// Some runes in the input slice are not ASCII. a[na] = s[fieldStart:]
return FieldsFunc(s, unicode.IsSpace) }
return a
} }
// FieldsFunc interprets s as a sequence of UTF-8-encoded Unicode code points. // FieldsFunc interprets s as a sequence of UTF-8-encoded Unicode code points.
......
...@@ -514,48 +514,49 @@ func (d *Data) readType(name string, r typeReader, off Offset, typeCache map[Off ...@@ -514,48 +514,49 @@ func (d *Data) readType(name string, r typeReader, off Offset, typeCache map[Off
var lastFieldType *Type var lastFieldType *Type
var lastFieldBitOffset int64 var lastFieldBitOffset int64
for kid := next(); kid != nil; kid = next() { for kid := next(); kid != nil; kid = next() {
if kid.Tag == TagMember { if kid.Tag != TagMember {
f := new(StructField) continue
if f.Type = typeOf(kid); err != nil { }
f := new(StructField)
if f.Type = typeOf(kid); err != nil {
goto Error
}
switch loc := kid.Val(AttrDataMemberLoc).(type) {
case []byte:
// TODO: Should have original compilation
// unit here, not unknownFormat.
b := makeBuf(d, unknownFormat{}, "location", 0, loc)
if b.uint8() != opPlusUconst {
err = DecodeError{name, kid.Offset, "unexpected opcode"}
goto Error goto Error
} }
switch loc := kid.Val(AttrDataMemberLoc).(type) { f.ByteOffset = int64(b.uint())
case []byte: if b.err != nil {
// TODO: Should have original compilation err = b.err
// unit here, not unknownFormat. goto Error
b := makeBuf(d, unknownFormat{}, "location", 0, loc)
if b.uint8() != opPlusUconst {
err = DecodeError{name, kid.Offset, "unexpected opcode"}
goto Error
}
f.ByteOffset = int64(b.uint())
if b.err != nil {
err = b.err
goto Error
}
case int64:
f.ByteOffset = loc
} }
case int64:
f.ByteOffset = loc
}
haveBitOffset := false haveBitOffset := false
f.Name, _ = kid.Val(AttrName).(string) f.Name, _ = kid.Val(AttrName).(string)
f.ByteSize, _ = kid.Val(AttrByteSize).(int64) f.ByteSize, _ = kid.Val(AttrByteSize).(int64)
f.BitOffset, haveBitOffset = kid.Val(AttrBitOffset).(int64) f.BitOffset, haveBitOffset = kid.Val(AttrBitOffset).(int64)
f.BitSize, _ = kid.Val(AttrBitSize).(int64) f.BitSize, _ = kid.Val(AttrBitSize).(int64)
t.Field = append(t.Field, f) t.Field = append(t.Field, f)
bito := f.BitOffset bito := f.BitOffset
if !haveBitOffset { if !haveBitOffset {
bito = f.ByteOffset * 8 bito = f.ByteOffset * 8
} }
if bito == lastFieldBitOffset && t.Kind != "union" { if bito == lastFieldBitOffset && t.Kind != "union" {
// Last field was zero width. Fix array length. // Last field was zero width. Fix array length.
// (DWARF writes out 0-length arrays as if they were 1-length arrays.) // (DWARF writes out 0-length arrays as if they were 1-length arrays.)
zeroArray(lastFieldType) zeroArray(lastFieldType)
}
lastFieldType = &f.Type
lastFieldBitOffset = bito
} }
lastFieldType = &f.Type
lastFieldBitOffset = bito
} }
if t.Kind != "union" { if t.Kind != "union" {
b, ok := e.Val(AttrByteSize).(int64) b, ok := e.Val(AttrByteSize).(int64)
......
...@@ -1195,25 +1195,26 @@ func (ctxt *Context) shouldBuild(content []byte, allTags map[string]bool, binary ...@@ -1195,25 +1195,26 @@ func (ctxt *Context) shouldBuild(content []byte, allTags map[string]bool, binary
p = p[len(p):] p = p[len(p):]
} }
line = bytes.TrimSpace(line) line = bytes.TrimSpace(line)
if bytes.HasPrefix(line, slashslash) { if !bytes.HasPrefix(line, slashslash) {
if bytes.Equal(line, binaryOnlyComment) { continue
sawBinaryOnly = true }
} if bytes.Equal(line, binaryOnlyComment) {
line = bytes.TrimSpace(line[len(slashslash):]) sawBinaryOnly = true
if len(line) > 0 && line[0] == '+' { }
// Looks like a comment +line. line = bytes.TrimSpace(line[len(slashslash):])
f := strings.Fields(string(line)) if len(line) > 0 && line[0] == '+' {
if f[0] == "+build" { // Looks like a comment +line.
ok := false f := strings.Fields(string(line))
for _, tok := range f[1:] { if f[0] == "+build" {
if ctxt.match(tok, allTags) { ok := false
ok = true for _, tok := range f[1:] {
} if ctxt.match(tok, allTags) {
} ok = true
if !ok {
allok = false
} }
} }
if !ok {
allok = false
}
} }
} }
} }
......
...@@ -134,47 +134,46 @@ type getter func(x *operand, i int) ...@@ -134,47 +134,46 @@ type getter func(x *operand, i int)
// the incoming getter with that i. // the incoming getter with that i.
// //
func unpack(get getter, n int, allowCommaOk bool) (getter, int, bool) { func unpack(get getter, n int, allowCommaOk bool) (getter, int, bool) {
if n == 1 { if n != 1 {
// possibly result of an n-valued function call or comma,ok value // zero or multiple values
var x0 operand return get, n, false
get(&x0, 0) }
if x0.mode == invalid { // possibly result of an n-valued function call or comma,ok value
return nil, 0, false var x0 operand
} get(&x0, 0)
if x0.mode == invalid {
return nil, 0, false
}
if t, ok := x0.typ.(*Tuple); ok {
// result of an n-valued function call
return func(x *operand, i int) {
x.mode = value
x.expr = x0.expr
x.typ = t.At(i).typ
}, t.Len(), false
}
if t, ok := x0.typ.(*Tuple); ok { if x0.mode == mapindex || x0.mode == commaok {
// result of an n-valued function call // comma-ok value
if allowCommaOk {
a := [2]Type{x0.typ, Typ[UntypedBool]}
return func(x *operand, i int) { return func(x *operand, i int) {
x.mode = value x.mode = value
x.expr = x0.expr x.expr = x0.expr
x.typ = t.At(i).typ x.typ = a[i]
}, t.Len(), false }, 2, true
}
if x0.mode == mapindex || x0.mode == commaok {
// comma-ok value
if allowCommaOk {
a := [2]Type{x0.typ, Typ[UntypedBool]}
return func(x *operand, i int) {
x.mode = value
x.expr = x0.expr
x.typ = a[i]
}, 2, true
}
x0.mode = value
} }
x0.mode = value
// single value
return func(x *operand, i int) {
if i != 0 {
unreachable()
}
*x = x0
}, 1, false
} }
// zero or multiple values // single value
return get, n, false return func(x *operand, i int) {
if i != 0 {
unreachable()
}
*x = x0
}, 1, false
} }
// arguments checks argument passing for the call with the given signature. // arguments checks argument passing for the call with the given signature.
......
...@@ -205,33 +205,33 @@ func expm1(x float64) float64 { ...@@ -205,33 +205,33 @@ func expm1(x float64) float64 {
r1 := 1 + hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5)))) r1 := 1 + hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5))))
t := 3 - r1*hfx t := 3 - r1*hfx
e := hxs * ((r1 - t) / (6.0 - x*t)) e := hxs * ((r1 - t) / (6.0 - x*t))
if k != 0 { if k == 0 {
e = (x*(e-c) - c) return x - (x*e - hxs) // c is 0
e -= hxs }
switch { e = (x*(e-c) - c)
case k == -1: e -= hxs
return 0.5*(x-e) - 0.5 switch {
case k == 1: case k == -1:
if x < -0.25 { return 0.5*(x-e) - 0.5
return -2 * (e - (x + 0.5)) case k == 1:
} if x < -0.25 {
return 1 + 2*(x-e) return -2 * (e - (x + 0.5))
case k <= -2 || k > 56: // suffice to return exp(x)-1
y := 1 - (e - x)
y = Float64frombits(Float64bits(y) + uint64(k)<<52) // add k to y's exponent
return y - 1
}
if k < 20 {
t := Float64frombits(0x3ff0000000000000 - (0x20000000000000 >> uint(k))) // t=1-2**-k
y := t - (e - x)
y = Float64frombits(Float64bits(y) + uint64(k)<<52) // add k to y's exponent
return y
} }
t := Float64frombits(uint64(0x3ff-k) << 52) // 2**-k return 1 + 2*(x-e)
y := x - (e + t) case k <= -2 || k > 56: // suffice to return exp(x)-1
y++ y := 1 - (e - x)
y = Float64frombits(Float64bits(y) + uint64(k)<<52) // add k to y's exponent
return y - 1
}
if k < 20 {
t := Float64frombits(0x3ff0000000000000 - (0x20000000000000 >> uint(k))) // t=1-2**-k
y := t - (e - x)
y = Float64frombits(Float64bits(y) + uint64(k)<<52) // add k to y's exponent y = Float64frombits(Float64bits(y) + uint64(k)<<52) // add k to y's exponent
return y return y
} }
return x - (x*e - hxs) // c is 0 t = Float64frombits(uint64(0x3ff-k) << 52) // 2**-k
y := x - (e + t)
y++
y = Float64frombits(Float64bits(y) + uint64(k)<<52) // add k to y's exponent
return y
} }
...@@ -1129,34 +1129,35 @@ func scavengelist(list *mSpanList, now, limit uint64) uintptr { ...@@ -1129,34 +1129,35 @@ func scavengelist(list *mSpanList, now, limit uint64) uintptr {
var sumreleased uintptr var sumreleased uintptr
for s := list.first; s != nil; s = s.next { for s := list.first; s != nil; s = s.next {
if (now-uint64(s.unusedsince)) > limit && s.npreleased != s.npages { if (now-uint64(s.unusedsince)) <= limit || s.npreleased == s.npages {
start := s.base() continue
end := start + s.npages<<_PageShift }
if physPageSize > _PageSize { start := s.base()
// We can only release pages in end := start + s.npages<<_PageShift
// physPageSize blocks, so round start if physPageSize > _PageSize {
// and end in. (Otherwise, madvise // We can only release pages in
// will round them *out* and release // physPageSize blocks, so round start
// more memory than we want.) // and end in. (Otherwise, madvise
start = (start + physPageSize - 1) &^ (physPageSize - 1) // will round them *out* and release
end &^= physPageSize - 1 // more memory than we want.)
if end <= start { start = (start + physPageSize - 1) &^ (physPageSize - 1)
// start and end don't span a end &^= physPageSize - 1
// whole physical page. if end <= start {
continue // start and end don't span a
} // whole physical page.
}
len := end - start
released := len - (s.npreleased << _PageShift)
if physPageSize > _PageSize && released == 0 {
continue continue
} }
memstats.heap_released += uint64(released)
sumreleased += released
s.npreleased = len >> _PageShift
sysUnused(unsafe.Pointer(start), len)
} }
len := end - start
released := len - (s.npreleased << _PageShift)
if physPageSize > _PageSize && released == 0 {
continue
}
memstats.heap_released += uint64(released)
sumreleased += released
s.npreleased = len >> _PageShift
sysUnused(unsafe.Pointer(start), len)
} }
return sumreleased return sumreleased
} }
......
...@@ -208,45 +208,46 @@ func sysargs(argc int32, argv **byte) { ...@@ -208,45 +208,46 @@ func sysargs(argc int32, argv **byte) {
// now argv+n is auxv // now argv+n is auxv
auxv := (*[1 << 28]uintptr)(add(unsafe.Pointer(argv), uintptr(n)*sys.PtrSize)) auxv := (*[1 << 28]uintptr)(add(unsafe.Pointer(argv), uintptr(n)*sys.PtrSize))
if sysauxv(auxv[:]) == 0 { if sysauxv(auxv[:]) != 0 {
// In some situations we don't get a loader-provided return
// auxv, such as when loaded as a library on Android. }
// Fall back to /proc/self/auxv. // In some situations we don't get a loader-provided
fd := open(&procAuxv[0], 0 /* O_RDONLY */, 0) // auxv, such as when loaded as a library on Android.
if fd < 0 { // Fall back to /proc/self/auxv.
// On Android, /proc/self/auxv might be unreadable (issue 9229), so we fallback to fd := open(&procAuxv[0], 0 /* O_RDONLY */, 0)
// try using mincore to detect the physical page size. if fd < 0 {
// mincore should return EINVAL when address is not a multiple of system page size. // On Android, /proc/self/auxv might be unreadable (issue 9229), so we fallback to
const size = 256 << 10 // size of memory region to allocate // try using mincore to detect the physical page size.
p := mmap(nil, size, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0) // mincore should return EINVAL when address is not a multiple of system page size.
if uintptr(p) < 4096 { const size = 256 << 10 // size of memory region to allocate
return p := mmap(nil, size, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
} if uintptr(p) < 4096 {
var n uintptr
for n = 4 << 10; n < size; n <<= 1 {
err := mincore(unsafe.Pointer(uintptr(p)+n), 1, &addrspace_vec[0])
if err == 0 {
physPageSize = n
break
}
}
if physPageSize == 0 {
physPageSize = size
}
munmap(p, size)
return return
} }
var buf [128]uintptr var n uintptr
n := read(fd, noescape(unsafe.Pointer(&buf[0])), int32(unsafe.Sizeof(buf))) for n = 4 << 10; n < size; n <<= 1 {
closefd(fd) err := mincore(unsafe.Pointer(uintptr(p)+n), 1, &addrspace_vec[0])
if n < 0 { if err == 0 {
return physPageSize = n
break
}
}
if physPageSize == 0 {
physPageSize = size
} }
// Make sure buf is terminated, even if we didn't read munmap(p, size)
// the whole file. return
buf[len(buf)-2] = _AT_NULL }
sysauxv(buf[:]) var buf [128]uintptr
n = read(fd, noescape(unsafe.Pointer(&buf[0])), int32(unsafe.Sizeof(buf)))
closefd(fd)
if n < 0 {
return
} }
// Make sure buf is terminated, even if we didn't read
// the whole file.
buf[len(buf)-2] = _AT_NULL
sysauxv(buf[:])
} }
func sysauxv(auxv []uintptr) int { func sysauxv(auxv []uintptr) int {
......
...@@ -244,36 +244,37 @@ func freedefer(d *_defer) { ...@@ -244,36 +244,37 @@ func freedefer(d *_defer) {
freedeferfn() freedeferfn()
} }
sc := deferclass(uintptr(d.siz)) sc := deferclass(uintptr(d.siz))
if sc < uintptr(len(p{}.deferpool)) { if sc >= uintptr(len(p{}.deferpool)) {
pp := getg().m.p.ptr() return
if len(pp.deferpool[sc]) == cap(pp.deferpool[sc]) { }
// Transfer half of local cache to the central cache. pp := getg().m.p.ptr()
// if len(pp.deferpool[sc]) == cap(pp.deferpool[sc]) {
// Take this slow path on the system stack so // Transfer half of local cache to the central cache.
// we don't grow freedefer's stack. //
systemstack(func() { // Take this slow path on the system stack so
var first, last *_defer // we don't grow freedefer's stack.
for len(pp.deferpool[sc]) > cap(pp.deferpool[sc])/2 { systemstack(func() {
n := len(pp.deferpool[sc]) var first, last *_defer
d := pp.deferpool[sc][n-1] for len(pp.deferpool[sc]) > cap(pp.deferpool[sc])/2 {
pp.deferpool[sc][n-1] = nil n := len(pp.deferpool[sc])
pp.deferpool[sc] = pp.deferpool[sc][:n-1] d := pp.deferpool[sc][n-1]
if first == nil { pp.deferpool[sc][n-1] = nil
first = d pp.deferpool[sc] = pp.deferpool[sc][:n-1]
} else { if first == nil {
last.link = d first = d
} } else {
last = d last.link = d
} }
lock(&sched.deferlock) last = d
last.link = sched.deferpool[sc] }
sched.deferpool[sc] = first lock(&sched.deferlock)
unlock(&sched.deferlock) last.link = sched.deferpool[sc]
}) sched.deferpool[sc] = first
} unlock(&sched.deferlock)
*d = _defer{} })
pp.deferpool[sc] = append(pp.deferpool[sc], d)
} }
*d = _defer{}
pp.deferpool[sc] = append(pp.deferpool[sc], d)
} }
// Separate function so that it can split stack. // Separate function so that it can split stack.
......
...@@ -578,29 +578,30 @@ func adjustpointers(scanp unsafe.Pointer, cbv *bitvector, adjinfo *adjustinfo, f ...@@ -578,29 +578,30 @@ func adjustpointers(scanp unsafe.Pointer, cbv *bitvector, adjinfo *adjustinfo, f
if stackDebug >= 4 { if stackDebug >= 4 {
print(" ", add(scanp, i*sys.PtrSize), ":", ptrnames[ptrbit(&bv, i)], ":", hex(*(*uintptr)(add(scanp, i*sys.PtrSize))), " # ", i, " ", bv.bytedata[i/8], "\n") print(" ", add(scanp, i*sys.PtrSize), ":", ptrnames[ptrbit(&bv, i)], ":", hex(*(*uintptr)(add(scanp, i*sys.PtrSize))), " # ", i, " ", bv.bytedata[i/8], "\n")
} }
if ptrbit(&bv, i) == 1 { if ptrbit(&bv, i) != 1 {
pp := (*uintptr)(add(scanp, i*sys.PtrSize)) continue
retry: }
p := *pp pp := (*uintptr)(add(scanp, i*sys.PtrSize))
if f.valid() && 0 < p && p < minLegalPointer && debug.invalidptr != 0 { retry:
// Looks like a junk value in a pointer slot. p := *pp
// Live analysis wrong? if f.valid() && 0 < p && p < minLegalPointer && debug.invalidptr != 0 {
getg().m.traceback = 2 // Looks like a junk value in a pointer slot.
print("runtime: bad pointer in frame ", funcname(f), " at ", pp, ": ", hex(p), "\n") // Live analysis wrong?
throw("invalid pointer found on stack") getg().m.traceback = 2
print("runtime: bad pointer in frame ", funcname(f), " at ", pp, ": ", hex(p), "\n")
throw("invalid pointer found on stack")
}
if minp <= p && p < maxp {
if stackDebug >= 3 {
print("adjust ptr ", hex(p), " ", funcname(f), "\n")
} }
if minp <= p && p < maxp { if useCAS {
if stackDebug >= 3 { ppu := (*unsafe.Pointer)(unsafe.Pointer(pp))
print("adjust ptr ", hex(p), " ", funcname(f), "\n") if !atomic.Casp1(ppu, unsafe.Pointer(p), unsafe.Pointer(p+delta)) {
} goto retry
if useCAS {
ppu := (*unsafe.Pointer)(unsafe.Pointer(pp))
if !atomic.Casp1(ppu, unsafe.Pointer(p), unsafe.Pointer(p+delta)) {
goto retry
}
} else {
*pp = p + delta
} }
} else {
*pp = p + delta
} }
} }
} }
......
...@@ -333,52 +333,53 @@ func (b *Writer) format(pos0 int, line0, line1 int) (pos int) { ...@@ -333,52 +333,53 @@ func (b *Writer) format(pos0 int, line0, line1 int) (pos int) {
for this := line0; this < line1; this++ { for this := line0; this < line1; this++ {
line := b.lines[this] line := b.lines[this]
if column < len(line)-1 { if column >= len(line)-1 {
// cell exists in this column => this line continue
// has more cells than the previous line }
// (the last cell per line is ignored because cells are // cell exists in this column => this line
// tab-terminated; the last cell per line describes the // has more cells than the previous line
// text before the newline/formfeed and does not belong // (the last cell per line is ignored because cells are
// to a column) // tab-terminated; the last cell per line describes the
// text before the newline/formfeed and does not belong
// print unprinted lines until beginning of block // to a column)
pos = b.writeLines(pos, line0, this)
line0 = this // print unprinted lines until beginning of block
pos = b.writeLines(pos, line0, this)
// column block begin line0 = this
width := b.minwidth // minimal column width
discardable := true // true if all cells in this column are empty and "soft" // column block begin
for ; this < line1; this++ { width := b.minwidth // minimal column width
line = b.lines[this] discardable := true // true if all cells in this column are empty and "soft"
if column < len(line)-1 { for ; this < line1; this++ {
// cell exists in this column line = b.lines[this]
c := line[column] if column < len(line)-1 {
// update width // cell exists in this column
if w := c.width + b.padding; w > width { c := line[column]
width = w // update width
} if w := c.width + b.padding; w > width {
// update discardable width = w
if c.width > 0 || c.htab {
discardable = false
}
} else {
break
} }
// update discardable
if c.width > 0 || c.htab {
discardable = false
}
} else {
break
} }
// column block end }
// column block end
// discard empty columns if necessary
if discardable && b.flags&DiscardEmptyColumns != 0 {
width = 0
}
// format and print all columns to the right of this column // discard empty columns if necessary
// (we know the widths of this column and all columns to the left) if discardable && b.flags&DiscardEmptyColumns != 0 {
b.widths = append(b.widths, width) // push width width = 0
pos = b.format(pos, line0, this)
b.widths = b.widths[0 : len(b.widths)-1] // pop width
line0 = this
} }
// format and print all columns to the right of this column
// (we know the widths of this column and all columns to the left)
b.widths = append(b.widths, width) // push width
pos = b.format(pos, line0, this)
b.widths = b.widths[0 : len(b.widths)-1] // pop width
line0 = this
} }
// print unprinted lines until end // print unprinted lines until end
......
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