mirror of
https://github.com/golang/go
synced 2024-11-19 23:44:43 -07:00
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
This commit is contained in:
parent
d11bb3b177
commit
4af796fb6e
@ -14,6 +14,7 @@ char *runtimeimport =
|
|||||||
"func @\"\".printbool (? bool)\n"
|
"func @\"\".printbool (? bool)\n"
|
||||||
"func @\"\".printfloat (? float64)\n"
|
"func @\"\".printfloat (? float64)\n"
|
||||||
"func @\"\".printint (? int64)\n"
|
"func @\"\".printint (? int64)\n"
|
||||||
|
"func @\"\".printhex (? uint64)\n"
|
||||||
"func @\"\".printuint (? uint64)\n"
|
"func @\"\".printuint (? uint64)\n"
|
||||||
"func @\"\".printcomplex (? complex128)\n"
|
"func @\"\".printcomplex (? complex128)\n"
|
||||||
"func @\"\".printstring (? string)\n"
|
"func @\"\".printstring (? string)\n"
|
||||||
@ -95,12 +96,12 @@ char *runtimeimport =
|
|||||||
"func @\"\".makeslice (@\"\".typ·2 *byte, @\"\".nel·3 int64, @\"\".cap·4 int64) (@\"\".ary·1 []any)\n"
|
"func @\"\".makeslice (@\"\".typ·2 *byte, @\"\".nel·3 int64, @\"\".cap·4 int64) (@\"\".ary·1 []any)\n"
|
||||||
"func @\"\".growslice (@\"\".typ·2 *byte, @\"\".old·3 []any, @\"\".n·4 int64) (@\"\".ary·1 []any)\n"
|
"func @\"\".growslice (@\"\".typ·2 *byte, @\"\".old·3 []any, @\"\".n·4 int64) (@\"\".ary·1 []any)\n"
|
||||||
"func @\"\".memmove (@\"\".to·1 *any, @\"\".frm·2 *any, @\"\".length·3 uintptr)\n"
|
"func @\"\".memmove (@\"\".to·1 *any, @\"\".frm·2 *any, @\"\".length·3 uintptr)\n"
|
||||||
"func @\"\".memequal (@\"\".x·1 *any, @\"\".y·2 *any, @\"\".size·3 uintptr) (? bool)\n"
|
"func @\"\".memequal (@\"\".x·2 *any, @\"\".y·3 *any, @\"\".size·4 uintptr) (? bool)\n"
|
||||||
"func @\"\".memequal8 (@\"\".x·1 *any, @\"\".y·2 *any, @\"\".size·3 uintptr) (? bool)\n"
|
"func @\"\".memequal8 (@\"\".x·2 *any, @\"\".y·3 *any, @\"\".size·4 uintptr) (? bool)\n"
|
||||||
"func @\"\".memequal16 (@\"\".x·1 *any, @\"\".y·2 *any, @\"\".size·3 uintptr) (? bool)\n"
|
"func @\"\".memequal16 (@\"\".x·2 *any, @\"\".y·3 *any, @\"\".size·4 uintptr) (? bool)\n"
|
||||||
"func @\"\".memequal32 (@\"\".x·1 *any, @\"\".y·2 *any, @\"\".size·3 uintptr) (? bool)\n"
|
"func @\"\".memequal32 (@\"\".x·2 *any, @\"\".y·3 *any, @\"\".size·4 uintptr) (? bool)\n"
|
||||||
"func @\"\".memequal64 (@\"\".x·1 *any, @\"\".y·2 *any, @\"\".size·3 uintptr) (? bool)\n"
|
"func @\"\".memequal64 (@\"\".x·2 *any, @\"\".y·3 *any, @\"\".size·4 uintptr) (? bool)\n"
|
||||||
"func @\"\".memequal128 (@\"\".x·1 *any, @\"\".y·2 *any, @\"\".size·3 uintptr) (? bool)\n"
|
"func @\"\".memequal128 (@\"\".x·2 *any, @\"\".y·3 *any, @\"\".size·4 uintptr) (? bool)\n"
|
||||||
"func @\"\".int64div (? int64, ? int64) (? int64)\n"
|
"func @\"\".int64div (? int64, ? int64) (? int64)\n"
|
||||||
"func @\"\".uint64div (? uint64, ? uint64) (? uint64)\n"
|
"func @\"\".uint64div (? uint64, ? uint64) (? uint64)\n"
|
||||||
"func @\"\".int64mod (? int64, ? int64) (? int64)\n"
|
"func @\"\".int64mod (? int64, ? int64) (? int64)\n"
|
||||||
|
@ -26,6 +26,7 @@ func recover(*int32) interface{}
|
|||||||
func printbool(bool)
|
func printbool(bool)
|
||||||
func printfloat(float64)
|
func printfloat(float64)
|
||||||
func printint(int64)
|
func printint(int64)
|
||||||
|
func printhex(uint64)
|
||||||
func printuint(uint64)
|
func printuint(uint64)
|
||||||
func printcomplex(complex128)
|
func printcomplex(complex128)
|
||||||
func printstring(string)
|
func printstring(string)
|
||||||
|
@ -1837,9 +1837,12 @@ walkprint(Node *nn, NodeList **init, int defer)
|
|||||||
t = types[TINT64];
|
t = types[TINT64];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if(et == TUINT64)
|
if(et == TUINT64) {
|
||||||
on = syslook("printuint", 0);
|
if(t->sym->pkg == runtimepkg && strcmp(t->sym->name, "hex") == 0)
|
||||||
else
|
on = syslook("printhex", 0);
|
||||||
|
else
|
||||||
|
on = syslook("printuint", 0);
|
||||||
|
} else
|
||||||
on = syslook("printint", 0);
|
on = syslook("printint", 0);
|
||||||
}
|
}
|
||||||
} else if(isfloat[et]) {
|
} else if(isfloat[et]) {
|
||||||
|
@ -6,6 +6,10 @@ package runtime
|
|||||||
|
|
||||||
import "unsafe"
|
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
|
//go:noescape
|
||||||
func gostring(*byte) string
|
func gostring(*byte) string
|
||||||
|
|
||||||
@ -178,8 +182,7 @@ func vprintf(str string, arg unsafe.Pointer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func printpc(p unsafe.Pointer) {
|
func printpc(p unsafe.Pointer) {
|
||||||
print("PC=")
|
print("PC=", hex(uintptr(p)))
|
||||||
printhex(uint64(getcallerpc(p)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func printbool(v bool) {
|
func printbool(v bool) {
|
||||||
|
Loading…
Reference in New Issue
Block a user