]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gc: allow runtime to define a hex integer type for printing
authorRuss Cox <rsc@golang.org>
Fri, 29 Aug 2014 17:22:17 +0000 (13:22 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 29 Aug 2014 17:22:17 +0000 (13:22 -0400)
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

src/cmd/gc/builtin.c
src/cmd/gc/runtime.go
src/cmd/gc/walk.c
src/pkg/runtime/print1.go

index eba919954436dcbba455710625d77f6d627710e1..c5bce2ead3b0d8fc195122ac8a74826a941b3184 100644 (file)
@@ -14,6 +14,7 @@ char *runtimeimport =
        "func @\"\".printbool (? bool)\n"
        "func @\"\".printfloat (? float64)\n"
        "func @\"\".printint (? int64)\n"
+       "func @\"\".printhex (? uint64)\n"
        "func @\"\".printuint (? uint64)\n"
        "func @\"\".printcomplex (? complex128)\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 @\"\".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 @\"\".memequal (@\"\".x·1 *any, @\"\".y·2 *any, @\"\".size·3 uintptr) (? bool)\n"
-       "func @\"\".memequal8 (@\"\".x·1 *any, @\"\".y·2 *any, @\"\".size·3 uintptr) (? bool)\n"
-       "func @\"\".memequal16 (@\"\".x·1 *any, @\"\".y·2 *any, @\"\".size·3 uintptr) (? bool)\n"
-       "func @\"\".memequal32 (@\"\".x·1 *any, @\"\".y·2 *any, @\"\".size·3 uintptr) (? bool)\n"
-       "func @\"\".memequal64 (@\"\".x·1 *any, @\"\".y·2 *any, @\"\".size·3 uintptr) (? bool)\n"
-       "func @\"\".memequal128 (@\"\".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·2 *any, @\"\".y·3 *any, @\"\".size·4 uintptr) (? bool)\n"
+       "func @\"\".memequal16 (@\"\".x·2 *any, @\"\".y·3 *any, @\"\".size·4 uintptr) (? bool)\n"
+       "func @\"\".memequal32 (@\"\".x·2 *any, @\"\".y·3 *any, @\"\".size·4 uintptr) (? bool)\n"
+       "func @\"\".memequal64 (@\"\".x·2 *any, @\"\".y·3 *any, @\"\".size·4 uintptr) (? bool)\n"
+       "func @\"\".memequal128 (@\"\".x·2 *any, @\"\".y·3 *any, @\"\".size·4 uintptr) (? bool)\n"
        "func @\"\".int64div (? int64, ? int64) (? int64)\n"
        "func @\"\".uint64div (? uint64, ? uint64) (? uint64)\n"
        "func @\"\".int64mod (? int64, ? int64) (? int64)\n"
index 7617eddd601718296b506e8ce2646bdc92ad5930..646cb68aa955113dcf7a481c106a415951b39ef9 100644 (file)
@@ -26,6 +26,7 @@ func recover(*int32) interface{}
 func printbool(bool)
 func printfloat(float64)
 func printint(int64)
+func printhex(uint64)
 func printuint(uint64)
 func printcomplex(complex128)
 func printstring(string)
index c251af660e5c4c38a85916a078d406dedf1ffcb0..26d8011240fbdeaf57f7b2302722a8f8a1c234c3 100644 (file)
@@ -1837,9 +1837,12 @@ walkprint(Node *nn, NodeList **init, int defer)
                                        t = types[TINT64];
                                }
                        } else {
-                               if(et == TUINT64)
-                                       on = syslook("printuint", 0);
-                               else
+                               if(et == TUINT64) {
+                                       if(t->sym->pkg == runtimepkg && strcmp(t->sym->name, "hex") == 0)
+                                               on = syslook("printhex", 0);
+                                       else
+                                               on = syslook("printuint", 0);
+                               } else
                                        on = syslook("printint", 0);
                        }
                } else if(isfloat[et]) {
index f19cc1da59a0c42765a81e565b5f0f3eb51ae04f..28faa7cbbb464f0d65f196f7c3a1f1b1ae010b6a 100644 (file)
@@ -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) {