Skip to content

Commit 3700383

Browse files
committed
runtime: tracePanic
1 parent 2e6312e commit 3700383

File tree

4 files changed

+82
-7
lines changed

4 files changed

+82
-7
lines changed

c/bitcast/_cast/cast.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
typedef union {
2+
double d;
3+
float f;
4+
long v;
5+
} castUnion;
6+
7+
double bitcastToFloat64(long v) {
8+
castUnion k;
9+
k.v = v;
10+
return k.d;
11+
}
12+
13+
float bitcastToFloat32(long v) {
14+
castUnion k;
15+
k.v = v;
16+
return k.f;
17+
}

c/bitcast/bitcast.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package bitcast
2+
3+
import _ "unsafe"
4+
5+
const (
6+
LLGoFiles = "_cast/cast.c"
7+
LLGoPackage = "link"
8+
)
9+
10+
//go:linkname ToFloat64 C.bitcastToFloat64
11+
func ToFloat64(v uintptr) float64
12+
13+
//go:linkname ToFloat32 C.bitcastToFloat32
14+
func ToFloat32(v uintptr) float32

c/bitcast/llgo_autogen.lla

405 Bytes
Binary file not shown.

internal/runtime/z_rt.go

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"unsafe"
2121

2222
"github.com/goplus/llgo/c"
23+
"github.com/goplus/llgo/c/bitcast"
2324
"github.com/goplus/llgo/c/pthread"
2425
"github.com/goplus/llgo/internal/abi"
2526
)
@@ -58,7 +59,7 @@ func Panic(v any) {
5859
func Rethrow(link *Defer) {
5960
if ptr := excepKey.Get(); ptr != nil {
6061
if link == nil {
61-
TracePanic(*(*Eface)(ptr))
62+
TracePanic(*(*any)(ptr))
6263
c.Free(ptr)
6364
c.Exit(2)
6465
} else {
@@ -77,14 +78,57 @@ func init() {
7778

7879
// -----------------------------------------------------------------------------
7980

81+
func unpackEface(i any) *eface {
82+
return (*eface)(unsafe.Pointer(&i))
83+
}
84+
8085
// TracePanic prints panic message.
81-
func TracePanic(v Eface) {
82-
kind := v._type.Kind()
83-
switch {
84-
case kind == abi.String:
85-
stringTracef(c.Stderr, c.Str("panic: %s\n"), *(*String)(v.data))
86+
func TracePanic(v any) {
87+
print("panic: ")
88+
switch e := v.(type) {
89+
case nil:
90+
println("nil")
91+
return
92+
case (interface{ Error() string }):
93+
println(e.Error())
94+
return
95+
case (interface{ String() string }):
96+
println(e.String())
97+
return
98+
}
99+
e := unpackEface(v)
100+
switch e.Kind() {
101+
case abi.Int, abi.Int8, abi.Int16, abi.Int32, abi.Int64:
102+
if isDirectIface(e._type) {
103+
println(int64(uintptr(e.data)))
104+
} else {
105+
println(*(*int64)(e.data))
106+
}
107+
case abi.Uint, abi.Uint8, abi.Uint16, abi.Uint32, abi.Uint64, abi.Uintptr:
108+
if isDirectIface(e._type) {
109+
println(uint64(uintptr(e.data)))
110+
} else {
111+
println(*(*uint64)(e.data))
112+
}
113+
case abi.Float32:
114+
if isDirectIface(e._type) {
115+
println(bitcast.ToFloat32((uintptr(e.data))))
116+
} else {
117+
println(*(*float32)(e.data))
118+
}
119+
case abi.Float64:
120+
if isDirectIface(e._type) {
121+
println(bitcast.ToFloat64(uintptr(e.data)))
122+
} else {
123+
println(*(*float64)(e.data))
124+
}
125+
case abi.String:
126+
println(*(*string)(e.data))
127+
default:
128+
// TODO kind to e._type.Str_
129+
print("(", e.Kind(), ") ")
130+
println(e.data)
86131
}
87-
// TODO(xsw): other message type
88132
}
89133

90134
func stringTracef(fp c.FilePtr, format *c.Char, s String) {

0 commit comments

Comments
 (0)