Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: tracePanic #363

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ Common Go syntax is already supported. Except for the following, which needs to

* map (Very limited support)
* chan (Not supported yet)
* generics (Not supported yet)

Here are some examples related to Go syntax:

Expand Down Expand Up @@ -212,7 +211,7 @@ Here are the Go packages that can be imported correctly:
* [strconv](https://pkg.go.dev/strconv)
* [syscall](https://pkg.go.dev/syscall) (partially)
* [sync](https://pkg.go.dev/sync) (partially)
* [sync/atomic](https://pkg.go.dev/sync/atomic) (partially)
* [sync/atomic](https://pkg.go.dev/sync/atomic)


## Dependencies
Expand Down
17 changes: 17 additions & 0 deletions c/bitcast/_cast/cast.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
typedef union {
double d;
float f;
long v;
} castUnion;

double llgoToFloat64(long v) {
castUnion k;
k.v = v;
return k.d;
}

float llgoToFloat32(long v) {
castUnion k;
k.v = v;
return k.f;
}
30 changes: 30 additions & 0 deletions c/bitcast/bitcast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package bitcast

import _ "unsafe"

const (
LLGoFiles = "_cast/cast.c"
LLGoPackage = "link"
)

//go:linkname ToFloat64 C.llgoToFloat64
func ToFloat64(v uintptr) float64

//go:linkname ToFloat32 C.llgoToFloat32
func ToFloat32(v uintptr) float32
Binary file added c/bitcast/llgo_autogen.lla
Binary file not shown.
1 change: 0 additions & 1 deletion internal/lib/sync/atomic/atomic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package atomic

// llgo:skipall
import (
"unsafe"
)
Expand Down
58 changes: 51 additions & 7 deletions internal/runtime/z_rt.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"unsafe"

"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/bitcast"
"github.com/goplus/llgo/c/pthread"
"github.com/goplus/llgo/internal/abi"
)
Expand Down Expand Up @@ -58,7 +59,7 @@ func Panic(v any) {
func Rethrow(link *Defer) {
if ptr := excepKey.Get(); ptr != nil {
if link == nil {
TracePanic(*(*Eface)(ptr))
TracePanic(*(*any)(ptr))
c.Free(ptr)
c.Exit(2)
} else {
Expand All @@ -77,14 +78,57 @@ func init() {

// -----------------------------------------------------------------------------

func unpackEface(i any) *eface {
return (*eface)(unsafe.Pointer(&i))
}

// TracePanic prints panic message.
func TracePanic(v Eface) {
kind := v._type.Kind()
switch {
case kind == abi.String:
stringTracef(c.Stderr, c.Str("panic: %s\n"), *(*String)(v.data))
func TracePanic(v any) {
print("panic: ")
switch e := v.(type) {
case nil:
println("nil")
return
case (interface{ Error() string }):
println(e.Error())
return
case (interface{ String() string }):
println(e.String())
return
}
e := unpackEface(v)
switch e.Kind() {
case abi.Int, abi.Int8, abi.Int16, abi.Int32, abi.Int64:
if isDirectIface(e._type) {
println(int64(uintptr(e.data)))
} else {
println(*(*int64)(e.data))
}
case abi.Uint, abi.Uint8, abi.Uint16, abi.Uint32, abi.Uint64, abi.Uintptr:
if isDirectIface(e._type) {
println(uint64(uintptr(e.data)))
} else {
println(*(*uint64)(e.data))
}
case abi.Float32:
if isDirectIface(e._type) {
println(bitcast.ToFloat32((uintptr(e.data))))
} else {
println(*(*float32)(e.data))
}
case abi.Float64:
if isDirectIface(e._type) {
println(bitcast.ToFloat64(uintptr(e.data)))
} else {
println(*(*float64)(e.data))
}
case abi.String:
println(*(*string)(e.data))
default:
// TODO kind to e._type.Str_
print("(", e.Kind(), ") ")
println(e.data)
}
// TODO(xsw): other message type
}

func stringTracef(fp c.FilePtr, format *c.Char, s String) {
Expand Down
Loading