aboutsummaryrefslogtreecommitdiff
path: root/src/pkg
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2014-08-29 13:22:17 -0400
committerRuss Cox <rsc@golang.org>2014-08-29 13:22:17 -0400
commit4af796fb6ee041d2c541f902ba2effbf23978d4a (patch)
tree4c138699e4efd6d89a251541a7fd24079ebba287 /src/pkg
parentd11bb3b177feb47d20e00246fb915af50773904a (diff)
downloadgo-4af796fb6ee041d2c541f902ba2effbf23978d4a.tar.xz
cmd/gc: allow runtime to define a hex integer type for printing
As part of the translation of the runtime, we need to rewrite C printf calls to Go print calls. Consider this C printf: runtime·printf("[signal %x code=%p addr=%p pc=%p]\n", g->sig, g->sigcode0, g->sigcode1, g->sigpc); Today the only way to write that in Go is: print("[signal ") printhex(uint64(g->sig)) print(" code=") printhex(uint64(g->sigcode0)) print(" addr=") printhex(uint64(g->sigcode1)) print(" pc=") printhex(uint64(g->sigpc)) print("]\n") (That's nearly exactly what runtime code looked like in C before I added runtime·printf.) This CL recognizes the unexported type runtime.hex as an integer that should be printed in hexadecimal instead of decimal. It's a little kludgy, but it's restricted to package runtime. Other packages can define type hex with no effect at all. Now we can translate that original printf as the more compact: print("[signal ", hex(g->sig), " code=", hex(g->sigcode0), " addr=", hex(g->sigcode1), " pc=", hex(g->sigpc), "]\n") LGTM=r, iant R=r, iant CC=golang-codereviews https://golang.org/cl/133220043
Diffstat (limited to 'src/pkg')
-rw-r--r--src/pkg/runtime/print1.go7
1 files changed, 5 insertions, 2 deletions
diff --git a/src/pkg/runtime/print1.go b/src/pkg/runtime/print1.go
index f19cc1da59..28faa7cbbb 100644
--- a/src/pkg/runtime/print1.go
+++ b/src/pkg/runtime/print1.go
@@ -6,6 +6,10 @@ package runtime
import "unsafe"
+// The compiler knows that a print of a value of this type
+// should use printhex instead of printuint (decimal).
+type hex uint64
+
//go:noescape
func gostring(*byte) string
@@ -178,8 +182,7 @@ func vprintf(str string, arg unsafe.Pointer) {
}
func printpc(p unsafe.Pointer) {
- print("PC=")
- printhex(uint64(getcallerpc(p)))
+ print("PC=", hex(uintptr(p)))
}
func printbool(v bool) {