runtime: rethrow/panic; llgo/ssa: DeferData; Null => Nil

This commit is contained in:
xushiwei
2024-06-12 17:26:07 +08:00
parent 60dd33b48f
commit b787de0163
11 changed files with 176 additions and 71 deletions

View File

@@ -19,6 +19,7 @@ package runtime
import (
"unsafe"
"github.com/goplus/llgo/c/pthread"
"github.com/goplus/llgo/internal/abi"
"github.com/goplus/llgo/internal/runtime/c"
)
@@ -27,19 +28,43 @@ import (
// Defer presents defer statements in a function.
type Defer struct {
Addr unsafe.Pointer
Addr unsafe.Pointer // sigjmpbuf
Bits uintptr
Link *Defer
Rund int // index of RunDefers
}
// -----------------------------------------------------------------------------
// Panic panics with a value.
func Panic(v Eface) {
ptr := c.Malloc(unsafe.Sizeof(v))
*(*Eface)(ptr) = v
excepKey.Set(ptr)
// Zeroinit initializes memory to zero.
func Zeroinit(p unsafe.Pointer, size uintptr) unsafe.Pointer {
return c.Memset(p, 0, size)
Rethrow((*Defer)(c.GoDeferData()))
}
// Rethrow rethrows a panic.
func Rethrow(link *Defer) {
if link == nil {
ptr := excepKey.Get()
TracePanic(*(*Eface)(ptr))
c.Free(ptr)
c.Unreachable()
} else {
c.Siglongjmp(link.Addr, 1)
}
}
var (
excepKey pthread.Key
)
func init() {
excepKey.Create(nil)
}
// -----------------------------------------------------------------------------
// TracePanic prints panic message.
func TracePanic(v Eface) {
kind := v._type.Kind()
@@ -56,3 +81,10 @@ func stringTracef(fp c.FilePtr, format *c.Char, s String) {
}
// -----------------------------------------------------------------------------
// Zeroinit initializes memory to zero.
func Zeroinit(p unsafe.Pointer, size uintptr) unsafe.Pointer {
return c.Memset(p, 0, size)
}
// -----------------------------------------------------------------------------