compiler: update integer type sizes

* Use 64-bit integers on 64-bit platforms, just like gc and gccgo:
  https://golang.org/doc/go1.1#int
* Do not use a separate length type. Instead, use uintptr everywhere a
  length is expected.
This commit is contained in:
Ayke van Laethem
2018-11-14 14:01:04 +01:00
parent 101d41dc58
commit ee7c276493
10 changed files with 68 additions and 80 deletions
-3
View File
@@ -4,8 +4,5 @@ package runtime
const GOARCH = "amd64"
// The length type used inside strings and slices.
type lenType uint32
// The bitness of the CPU (e.g. 8, 32, 64).
const TargetBits = 64
-3
View File
@@ -8,9 +8,6 @@ import (
const GOARCH = "avr"
// The length type used inside strings and slices.
type lenType uint16
// The bitness of the CPU (e.g. 8, 32, 64).
const TargetBits = 8
-3
View File
@@ -8,9 +8,6 @@ import (
const GOARCH = "arm"
// The length type used inside strings and slices.
type lenType uint32
// The bitness of the CPU (e.g. 8, 32, 64).
const TargetBits = 32
-3
View File
@@ -8,9 +8,6 @@ import (
const GOARCH = "wasm"
// The length type used inside strings and slices.
type lenType uint32
// The bitness of the CPU (e.g. 8, 32, 64).
const TargetBits = 32
+1 -1
View File
@@ -13,7 +13,7 @@ import (
type hashmap struct {
next *hashmap // hashmap after evacuate (for iterators)
buckets unsafe.Pointer // pointer to array of buckets
count lenType
count uintptr
keySize uint8 // maybe this can store the key type as well? E.g. keysize == 5 means string?
valueSize uint8
bucketBits uint8
+4 -4
View File
@@ -28,7 +28,7 @@ func _recover() interface{} {
}
// Check for bounds in *ssa.Index, *ssa.IndexAddr and *ssa.Lookup.
func lookupBoundsCheck(length lenType, index int) {
func lookupBoundsCheck(length uintptr, index int) {
if index < 0 || index >= int(length) {
runtimePanic("index out of range")
}
@@ -36,21 +36,21 @@ func lookupBoundsCheck(length lenType, index int) {
// Check for bounds in *ssa.Index, *ssa.IndexAddr and *ssa.Lookup.
// Supports 64-bit indexes.
func lookupBoundsCheckLong(length lenType, index int64) {
func lookupBoundsCheckLong(length uintptr, index int64) {
if index < 0 || index >= int64(length) {
runtimePanic("index out of range")
}
}
// Check for bounds in *ssa.Slice.
func sliceBoundsCheck(capacity lenType, low, high uint) {
func sliceBoundsCheck(capacity uintptr, low, high uint) {
if !(0 <= low && low <= high && high <= uint(capacity)) {
runtimePanic("slice out of range")
}
}
// Check for bounds in *ssa.Slice. Supports 64-bit indexes.
func sliceBoundsCheckLong(capacity lenType, low, high uint64) {
func sliceBoundsCheckLong(capacity uintptr, low, high uint64) {
if !(0 <= low && low <= high && high <= uint64(capacity)) {
runtimePanic("slice out of range")
}
+1 -1
View File
@@ -217,7 +217,7 @@ func printmap(m *hashmap) {
if m == nil {
print("nil")
} else {
print(m.count)
print(uint(m.count))
}
putchar(']')
}
+6 -6
View File
@@ -8,7 +8,7 @@ import (
// Builtin append(src, elements...) function: append elements to src and return
// the modified (possibly expanded) slice.
func sliceAppend(srcBuf, elemsBuf unsafe.Pointer, srcLen, srcCap, elemsLen lenType, elemSize uintptr) (unsafe.Pointer, lenType, lenType) {
func sliceAppend(srcBuf, elemsBuf unsafe.Pointer, srcLen, srcCap, elemsLen uintptr, elemSize uintptr) (unsafe.Pointer, uintptr, uintptr) {
if elemsLen == 0 {
// Nothing to append, return the input slice.
return srcBuf, srcLen, srcCap
@@ -27,27 +27,27 @@ func sliceAppend(srcBuf, elemsBuf unsafe.Pointer, srcLen, srcCap, elemsLen lenTy
// programs).
srcCap *= 2
}
buf := alloc(uintptr(srcCap) * elemSize)
buf := alloc(srcCap * elemSize)
// Copy the old slice to the new slice.
if srcLen != 0 {
memmove(buf, srcBuf, uintptr(srcLen)*elemSize)
memmove(buf, srcBuf, srcLen*elemSize)
}
srcBuf = buf
}
// The slice fits (after possibly allocating a new one), append it in-place.
memmove(unsafe.Pointer(uintptr(srcBuf)+uintptr(srcLen)*elemSize), elemsBuf, uintptr(elemsLen)*elemSize)
memmove(unsafe.Pointer(uintptr(srcBuf)+srcLen*elemSize), elemsBuf, elemsLen*elemSize)
return srcBuf, srcLen + elemsLen, srcCap
}
// Builtin copy(dst, src) function: copy bytes from dst to src.
func sliceCopy(dst, src unsafe.Pointer, dstLen, srcLen lenType, elemSize uintptr) lenType {
func sliceCopy(dst, src unsafe.Pointer, dstLen, srcLen uintptr, elemSize uintptr) uintptr {
// n = min(srcLen, dstLen)
n := srcLen
if n > dstLen {
n = dstLen
}
memmove(dst, src, uintptr(n)*elemSize)
memmove(dst, src, n*elemSize)
return n
}
+19 -19
View File
@@ -9,13 +9,13 @@ import (
// The underlying struct for the Go string type.
type _string struct {
ptr *byte
length lenType
length uintptr
}
// The iterator state for a range over a string.
type stringIterator struct {
byteindex lenType
rangeindex lenType
byteindex uintptr
rangeindex uintptr
}
// Return true iff the strings match.
@@ -57,33 +57,33 @@ func stringConcat(x, y _string) _string {
} else if y.length == 0 {
return x
} else {
length := uintptr(x.length + y.length)
length := x.length + y.length
buf := alloc(length)
memcpy(buf, unsafe.Pointer(x.ptr), uintptr(x.length))
memcpy(unsafe.Pointer(uintptr(buf)+uintptr(x.length)), unsafe.Pointer(y.ptr), uintptr(y.length))
return _string{ptr: (*byte)(buf), length: lenType(length)}
memcpy(buf, unsafe.Pointer(x.ptr), x.length)
memcpy(unsafe.Pointer(uintptr(buf)+x.length), unsafe.Pointer(y.ptr), y.length)
return _string{ptr: (*byte)(buf), length: length}
}
}
// Create a string from a []byte slice.
func stringFromBytes(x struct {
ptr *byte
len lenType
cap lenType
len uintptr
cap uintptr
}) _string {
buf := alloc(uintptr(x.len))
memcpy(buf, unsafe.Pointer(x.ptr), uintptr(x.len))
return _string{ptr: (*byte)(buf), length: lenType(x.len)}
buf := alloc(x.len)
memcpy(buf, unsafe.Pointer(x.ptr), x.len)
return _string{ptr: (*byte)(buf), length: x.len}
}
// Convert a string to a []byte slice.
func stringToBytes(x _string) (slice struct {
ptr *byte
len lenType
cap lenType
len uintptr
cap uintptr
}) {
buf := alloc(uintptr(x.length))
memcpy(buf, unsafe.Pointer(x.ptr), uintptr(x.length))
buf := alloc(x.length)
memcpy(buf, unsafe.Pointer(x.ptr), x.length)
slice.ptr = (*byte)(buf)
slice.len = x.length
slice.cap = x.length
@@ -112,7 +112,7 @@ func stringNext(s string, it *stringIterator) (bool, int, rune) {
}
// Convert a Unicode code point into an array of bytes and its length.
func encodeUTF8(x rune) ([4]byte, lenType) {
func encodeUTF8(x rune) ([4]byte, uintptr) {
// https://stackoverflow.com/questions/6240055/manually-converting-unicode-codepoints-into-utf-8-and-utf-16
// Note: this code can probably be optimized (in size and speed).
switch {
@@ -141,8 +141,8 @@ func encodeUTF8(x rune) ([4]byte, lenType) {
// Decode a single UTF-8 character from a string.
//go:nobounds
func decodeUTF8(s string, index lenType) (rune, lenType) {
remaining := lenType(len(s)) - index // must be >= 1 before calling this function
func decodeUTF8(s string, index uintptr) (rune, uintptr) {
remaining := uintptr(len(s)) - index // must be >= 1 before calling this function
x := s[index]
switch {
case x&0x80 == 0x00: // 0xxxxxxx