From 8ae97f73d07af2c4e78adda817cd2cc34d92b251 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Wed, 1 May 2024 12:32:09 +0800 Subject: [PATCH] debug why runtime.Basic doesn't work --- internal/llgen/llgenf.go | 4 ++ internal/runtime/c/c.go | 20 ++++-- internal/runtime/llgo_autogen.ll | 108 ++++++++++++++++++------------- internal/runtime/stubs.go | 13 ++-- internal/runtime/z_c.go | 2 +- internal/runtime/z_iface.go | 2 +- internal/runtime/z_type.go | 12 +++- 7 files changed, 98 insertions(+), 63 deletions(-) diff --git a/internal/llgen/llgenf.go b/internal/llgen/llgenf.go index 046910e5..7326e468 100644 --- a/internal/llgen/llgenf.go +++ b/internal/llgen/llgenf.go @@ -57,6 +57,10 @@ func GenFrom(fileOrPkg string) string { return rt[0].Types }) + if Verbose { + ssaPkg.WriteTo(os.Stderr) + } + ret, err := cl.NewPackage(prog, ssaPkg, pkg.Syntax) check(err) diff --git a/internal/runtime/c/c.go b/internal/runtime/c/c.go index 9b77a734..79763a1b 100644 --- a/internal/runtime/c/c.go +++ b/internal/runtime/c/c.go @@ -23,26 +23,32 @@ const ( LLGoPackage = "decl" ) +type ( + Char = int8 + Int = C.int + Pointer = unsafe.Pointer +) + //go:linkname Str llgo.cstr -func Str(string) *int8 +func Str(string) *Char //go:linkname Alloca llgo.alloca -func Alloca(size uintptr) unsafe.Pointer +func Alloca(size uintptr) Pointer //go:linkname AllocaCStr llgo.allocaCStr -func AllocaCStr(s string) *int8 +func AllocaCStr(s string) *Char //go:linkname Unreachable llgo.unreachable func Unreachable() //go:linkname Rand C.rand -func Rand() C.int +func Rand() Int //go:linkname Malloc C.malloc -func Malloc(size uintptr) unsafe.Pointer +func Malloc(size uintptr) Pointer //go:linkname Memcpy C.memcpy -func Memcpy(dst, src unsafe.Pointer, n uintptr) unsafe.Pointer +func Memcpy(dst, src Pointer, n uintptr) Pointer //go:linkname Printf C.printf -func Printf(format *int8, __llgo_va_list ...any) C.int +func Printf(format *Char, __llgo_va_list ...any) Int diff --git a/internal/runtime/llgo_autogen.ll b/internal/runtime/llgo_autogen.ll index 4af3bc58..2cde6fb9 100644 --- a/internal/runtime/llgo_autogen.ll +++ b/internal/runtime/llgo_autogen.ll @@ -1,21 +1,23 @@ ; ModuleID = 'github.com/goplus/llgo/internal/runtime' source_filename = "github.com/goplus/llgo/internal/runtime" +%"github.com/goplus/llgo/internal/abi.Type" = type { i64, i64, i32, i8, i8, i8, i8, ptr, ptr, i32, i32 } %"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 } %"github.com/goplus/llgo/internal/runtime.iface" = type { ptr, ptr } %"github.com/goplus/llgo/internal/runtime.itab" = type { ptr, ptr, i32, [4 x i8], [1 x i64] } %"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 } -%"github.com/goplus/llgo/internal/abi.Type" = type { i64, i64, i32, i8, i8, i8, i8, ptr, ptr, i32, i32 } %"github.com/goplus/llgo/internal/runtime.hmap" = type { i64, i8, i8, i16, i32, ptr, ptr, i64, ptr } @"github.com/goplus/llgo/internal/runtime.TyAny" = global ptr null @"github.com/goplus/llgo/internal/runtime.basicTypes" = global ptr null @"github.com/goplus/llgo/internal/runtime.init$guard" = global ptr null @"github.com/goplus/llgo/internal/runtime.sizeBasicTypes" = global ptr null -@0 = private unnamed_addr constant [21 x i8] c"I2Int: type mismatch\00", align 1 -@1 = private unnamed_addr constant [19 x i8] c"MakeAnyString(%p)\0A\00", align 1 -@2 = private unnamed_addr constant [11 x i8] c"Panic(%p)\0A\00", align 1 -@3 = private unnamed_addr constant [11 x i8] c"panic: %s\0A\00", align 1 +@0 = private unnamed_addr constant [27 x i8] c"Basic: %p, %d, %d, %d, %d\0A\00", align 1 +@1 = private unnamed_addr constant [21 x i8] c"I2Int: type mismatch\00", align 1 +@2 = private unnamed_addr constant [27 x i8] c"MakeAnyString(%p): %d, %d\0A\00", align 1 +@3 = private unnamed_addr constant [11 x i8] c"Panic(%d)\0A\00", align 1 +@4 = private unnamed_addr constant [11 x i8] c"panic: %s\0A\00", align 1 +@5 = private unnamed_addr constant [31 x i8] c"basicType: %p, %d, %d, %d, %d\0A\00", align 1 define ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 %0) { _llgo_0: @@ -27,6 +29,16 @@ define ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 %0) { _llgo_0: %1 = getelementptr inbounds ptr, ptr @"github.com/goplus/llgo/internal/runtime.basicTypes", i64 %0 %2 = load ptr, ptr %1, align 8 + %3 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %2, i32 0, i32 2 + %4 = load i32, ptr %3, align 4 + %5 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %2, i32 0, i32 6 + %6 = load i8, ptr %5, align 1 + %7 = sext i8 %6 to i32 + %8 = trunc i64 %0 to i32 + %9 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %2, i32 0, i32 0 + %10 = load i64, ptr %9, align 4 + %11 = trunc i64 %10 to i32 + %12 = call i32 (ptr, ...) @printf(ptr @0, ptr %2, i32 %4, i32 %7, i32 %8, i32 %11) ret ptr %2 } @@ -109,7 +121,7 @@ _llgo_1: ; preds = %_llgo_0 ret i64 %10 _llgo_2: ; preds = %_llgo_0 - %11 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @0, i64 20) + %11 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @1, i64 20) %12 = call %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyString"(%"github.com/goplus/llgo/internal/runtime.String" %11) call void @"github.com/goplus/llgo/internal/runtime.TracePanic"(%"github.com/goplus/llgo/internal/runtime.iface" %12) unreachable @@ -176,14 +188,17 @@ _llgo_0: store ptr %2, ptr %6, align 8 store i32 0, ptr %7, align 4 store i64 0, ptr %9, align 4 - %10 = call i32 (ptr, ...) @printf(ptr @1, ptr %2) - %11 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8 - %12 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %11, i32 0, i32 0 - %13 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %11, i32 0, i32 1 - store ptr %3, ptr %12, align 8 - store ptr %1, ptr %13, align 8 - %14 = load %"github.com/goplus/llgo/internal/runtime.iface", ptr %11, align 8 - ret %"github.com/goplus/llgo/internal/runtime.iface" %14 + %10 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %2, i32 0, i32 6 + %11 = load i8, ptr %10, align 1 + %12 = sext i8 %11 to i64 + %13 = call i32 (ptr, ...) @printf(ptr @2, ptr %2, i64 %12, i64 24) + %14 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8 + %15 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %14, i32 0, i32 0 + %16 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %14, i32 0, i32 1 + store ptr %3, ptr %15, align 8 + store ptr %1, ptr %16, align 8 + %17 = load %"github.com/goplus/llgo/internal/runtime.iface", ptr %14, align 8 + ret %"github.com/goplus/llgo/internal/runtime.iface" %17 } define %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeInterface"(ptr %0, ptr %1, ptr %2) { @@ -294,30 +309,26 @@ _llgo_0: %3 = load ptr, ptr %2, align 8 %4 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %3, i32 0, i32 1 %5 = load ptr, ptr %4, align 8 - %6 = call i32 (ptr, ...) @printf(ptr @2, ptr %5) - %7 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %1, i32 0, i32 0 - %8 = load ptr, ptr %7, align 8 - %9 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %8, i32 0, i32 1 - %10 = load ptr, ptr %9, align 8 - %11 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %10, i32 0, i32 6 - %12 = load i8, ptr %11, align 1 - %13 = sext i8 %12 to i64 - %14 = icmp eq i64 %13, 24 - br i1 %14, label %_llgo_2, label %_llgo_1 + %6 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %5, i32 0, i32 6 + %7 = load i8, ptr %6, align 1 + %8 = sext i8 %7 to i64 + %9 = call i32 (ptr, ...) @printf(ptr @3, i64 %8) + %10 = icmp eq i64 %8, 24 + br i1 %10, label %_llgo_2, label %_llgo_1 _llgo_1: ; preds = %_llgo_2, %_llgo_0 ret void _llgo_2: ; preds = %_llgo_0 - %15 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %1, i32 0, i32 1 - %16 = load ptr, ptr %15, align 8 - %17 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %16, i32 0, i32 1 - %18 = load i64, ptr %17, align 4 - %19 = add i64 %18, 1 - %20 = alloca i8, i64 %19, align 1 - %21 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %16, align 8 - %22 = call ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr %20, %"github.com/goplus/llgo/internal/runtime.String" %21) - %23 = call i32 (ptr, ...) @printf(ptr @3, ptr %22) + %11 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %1, i32 0, i32 1 + %12 = load ptr, ptr %11, align 8 + %13 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %12, i32 0, i32 1 + %14 = load i64, ptr %13, align 4 + %15 = add i64 %14, 1 + %16 = alloca i8, i64 %15, align 1 + %17 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %12, align 8 + %18 = call ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr %16, %"github.com/goplus/llgo/internal/runtime.String" %17) + %19 = call i32 (ptr, ...) @printf(ptr @4, ptr %18) br label %_llgo_1 } @@ -327,18 +338,27 @@ _llgo_0: %2 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %1, i32 0, i32 0 %3 = getelementptr inbounds i64, ptr @"github.com/goplus/llgo/internal/runtime.sizeBasicTypes", i64 %0 %4 = load i64, ptr %3, align 4 - %5 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %1, i32 0, i32 6 - %6 = trunc i64 %0 to i8 + %5 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %1, i32 0, i32 2 + %6 = trunc i64 %0 to i32 + %7 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %1, i32 0, i32 6 + %8 = trunc i64 %0 to i8 store i64 %4, ptr %2, align 4 - store i8 %6, ptr %5, align 1 + store i32 %6, ptr %5, align 4 + store i8 %8, ptr %7, align 1 + %9 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %1, i32 0, i32 2 + %10 = load i32, ptr %9, align 4 + %11 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %1, i32 0, i32 6 + %12 = load i8, ptr %11, align 1 + %13 = sext i8 %12 to i32 + %14 = trunc i64 %0 to i32 + %15 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %1, i32 0, i32 0 + %16 = load i64, ptr %15, align 4 + %17 = trunc i64 %16 to i32 + %18 = call i32 (ptr, ...) @printf(ptr @5, ptr %1, i32 %10, i32 %13, i32 %14, i32 %17) ret ptr %1 } -define i32 @"github.com/goplus/llgo/internal/runtime.fastrand"() { -_llgo_0: - %0 = call i32 @rand() - ret i32 %0 -} +declare i32 @rand() define void @"github.com/goplus/llgo/internal/runtime.init"() { _llgo_0: @@ -416,7 +436,7 @@ _llgo_0: define ptr @"github.com/goplus/llgo/internal/runtime.makemap_small"() { _llgo_0: %0 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) - %1 = call i32 @"github.com/goplus/llgo/internal/runtime.fastrand"() + %1 = call i32 @rand() %2 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.hmap", ptr %0, i32 0, i32 4 store i32 %1, ptr %2, align 4 ret ptr %0 @@ -424,10 +444,8 @@ _llgo_0: declare ptr @malloc(i64) -declare ptr @memcpy(ptr, ptr, i64) - declare i32 @printf(ptr, ...) -declare i32 @rand() +declare ptr @memcpy(ptr, ptr, i64) declare void @"github.com/goplus/llgo/internal/abi.init"() diff --git a/internal/runtime/stubs.go b/internal/runtime/stubs.go index 3049817c..ed8b9a8d 100644 --- a/internal/runtime/stubs.go +++ b/internal/runtime/stubs.go @@ -4,14 +4,13 @@ package runtime -import ( - "github.com/goplus/llgo/internal/runtime/c" -) +import _ "unsafe" +//go:linkname fastrand C.rand +func fastrand() uint32 + +/* TODO(xsw): func fastrand() uint32 { - return uint32(c.Rand()) - /* TODO(xsw): - mp := getg().m // Implement wyrand: https://github.com/wangyi-fudan/wyhash // Only the platform that math.Mul64 can be lowered @@ -35,5 +34,5 @@ func fastrand() uint32 { s1 = s1 ^ s0 ^ s1>>7 ^ s0>>16 t[0], t[1] = s0, s1 return s0 + s1 - */ } +*/ diff --git a/internal/runtime/z_c.go b/internal/runtime/z_c.go index 46de4e6a..c6810222 100644 --- a/internal/runtime/z_c.go +++ b/internal/runtime/z_c.go @@ -30,8 +30,8 @@ func Alloc(size uintptr) unsafe.Pointer { // TracePanic prints panic message. func TracePanic(v Interface) { - c.Printf(c.Str("Panic(%p)\n"), v.tab._type) kind := abi.Kind(v.tab._type.Kind_) + c.Printf(c.Str("Panic(%d)\n"), kind) switch { case kind == abi.String: s := (*String)(v.data) diff --git a/internal/runtime/z_iface.go b/internal/runtime/z_iface.go index c926c3f4..0c5b7924 100644 --- a/internal/runtime/z_iface.go +++ b/internal/runtime/z_iface.go @@ -46,7 +46,7 @@ func MakeAnyInt(typ *Type, data uintptr) Interface { func MakeAnyString(data string) Interface { typ := Basic(abi.String) tab := &itab{inter: TyAny, _type: typ, hash: 0, fun: [1]uintptr{0}} - c.Printf(c.Str("MakeAnyString(%p)\n"), typ) + c.Printf(c.Str("MakeAnyString(%p): %d, %d\n"), typ, int(typ.Kind_), abi.String) return Interface{ tab: tab, data: unsafe.Pointer(&data), diff --git a/internal/runtime/z_type.go b/internal/runtime/z_type.go index 85ee1b66..349396ae 100644 --- a/internal/runtime/z_type.go +++ b/internal/runtime/z_type.go @@ -20,6 +20,7 @@ import ( "unsafe" "github.com/goplus/llgo/internal/abi" + "github.com/goplus/llgo/internal/runtime/c" ) // ----------------------------------------------------------------------------- @@ -28,7 +29,10 @@ type Kind = abi.Kind type Type = abi.Type func Basic(kind Kind) *Type { - return basicTypes[kind] + ret := basicTypes[kind] + c.Printf(c.Str("Basic: %p, %d, %d, %d, %d\n"), + ret, c.Int(ret.Hash), c.Int(ret.Kind_), c.Int(kind), c.Int(ret.Size_)) + return ret } var ( @@ -76,10 +80,14 @@ var ( ) func basicType(kind abi.Kind) *Type { - return &abi.Type{ + ret := &Type{ Size_: sizeBasicTypes[kind], + Hash: uint32(kind), Kind_: uint8(kind), } + c.Printf(c.Str("basicType: %p, %d, %d, %d, %d\n"), + ret, c.Int(ret.Hash), c.Int(ret.Kind_), c.Int(kind), c.Int(ret.Size_)) + return ret } // -----------------------------------------------------------------------------