Implement printing of int8/uint8/pointers

This commit is contained in:
Ayke van Laethem
2018-06-03 16:39:24 +02:00
parent e171f32493
commit 320c583221
3 changed files with 80 additions and 6 deletions
+42
View File
@@ -1,12 +1,40 @@
package runtime
import (
"unsafe"
)
func printstring(s string) {
for i := 0; i < len(s); i++ {
putchar(s[i])
}
}
func printuint8(n uint8) {
if TargetBits >= 32 {
printuint32(uint32(n))
} else {
prevdigits := n / 10
if prevdigits != 0 {
printuint8(prevdigits)
}
putchar(byte((n % 10) + '0'))
}
}
func printint8(n int8) {
if TargetBits >= 32 {
printint32(int32(n))
} else {
if n < 0 {
putchar('-')
n = -n
}
printuint8(uint8(n))
}
}
func printuint32(n uint32) {
// TODO: don't recurse, but still be compact (and don't divide/mod
// more than necessary).
@@ -64,3 +92,17 @@ func printitf(msg interface{}) {
print("???")
}
}
func printptr(ptr uintptr) {
putchar('0')
putchar('x')
for i := 0; i < int(unsafe.Sizeof(ptr)) * 2; i++ {
nibble := byte(ptr >> (unsafe.Sizeof(ptr) * 8 - 4))
if nibble < 10 {
putchar(nibble + '0')
} else {
putchar(nibble - 10 + 'a')
}
ptr <<= 4
}
}
+3
View File
@@ -3,6 +3,9 @@ package runtime
const Compiler = "tgo"
// The bitness of the CPU (e.g. 8, 32, 64). Set by the compiler as a constant.
var TargetBits uint8
func _panic(message interface{}) {
printstring("panic: ")
printitf(message)