From 62f6542ca9e09f9cbd1aa6701fc461aa864c9fd2 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Wed, 1 May 2024 16:56:26 +0800 Subject: [PATCH] llgo/ssa: builder.Alloc sizeof(t) bugfix --- cl/_testrt/gblarray/in.go | 3 -- cl/_testrt/gblarray/out.ll | 25 +++------ internal/runtime/llgo_autogen.ll | 93 +++++++++++--------------------- internal/runtime/z_c.go | 1 - internal/runtime/z_iface.go | 16 ++---- internal/runtime/z_type.go | 11 +--- ssa/expr.go | 5 +- ssa/package.go | 9 +++- 8 files changed, 56 insertions(+), 107 deletions(-) diff --git a/cl/_testrt/gblarray/in.go b/cl/_testrt/gblarray/in.go index 3b60cf24..dcb43160 100644 --- a/cl/_testrt/gblarray/in.go +++ b/cl/_testrt/gblarray/in.go @@ -2,7 +2,6 @@ package main import ( "github.com/goplus/llgo/internal/abi" - "github.com/goplus/llgo/internal/runtime" "github.com/goplus/llgo/internal/runtime/c" ) @@ -29,7 +28,5 @@ func basicType(kind abi.Kind) *abi.Type { func main() { t := Basic(abi.String) - t2 := runtime.Basic(abi.String) c.Printf(c.Str("Kind: %d, Size: %d\n"), int(t.Kind_), t.Size_) - c.Printf(c.Str("Kind: %d, Size: %d\n"), int(t2.Kind_), t2.Size_) } diff --git a/cl/_testrt/gblarray/out.ll b/cl/_testrt/gblarray/out.ll index 773b0723..c03b7420 100644 --- a/cl/_testrt/gblarray/out.ll +++ b/cl/_testrt/gblarray/out.ll @@ -7,7 +7,6 @@ source_filename = "main" @"main.init$guard" = global ptr null @main.sizeBasicTypes = global ptr null @0 = private unnamed_addr constant [20 x i8] c"Kind: %d, Size: %d\0A\00", align 1 -@1 = private unnamed_addr constant [20 x i8] c"Kind: %d, Size: %d\0A\00", align 1 define ptr @main.Basic(i64 %0) { _llgo_0: @@ -18,7 +17,7 @@ _llgo_0: define ptr @main.basicType(i64 %0) { _llgo_0: - %1 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) + %1 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 48) %2 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %1, i32 0, i32 0 %3 = getelementptr inbounds i64, ptr @main.sizeBasicTypes, i64 %0 %4 = load i64, ptr %3, align 4 @@ -40,7 +39,6 @@ _llgo_0: _llgo_1: ; preds = %_llgo_0 store i1 true, ptr @"main.init$guard", align 1 call void @"github.com/goplus/llgo/internal/abi.init"() - call void @"github.com/goplus/llgo/internal/runtime.init"() store i64 16, ptr getelementptr inbounds (i64, ptr @main.sizeBasicTypes, i64 24), align 4 %1 = call ptr @main.basicType(i64 24) store ptr %1, ptr getelementptr inbounds (ptr, ptr @main.basicTypes, i64 24), align 8 @@ -55,19 +53,12 @@ _llgo_0: call void @"github.com/goplus/llgo/internal/runtime.init"() call void @main.init() %0 = call ptr @main.Basic(i64 24) - %1 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 24) - %2 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %0, i32 0, i32 6 - %3 = load i8, ptr %2, align 1 - %4 = sext i8 %3 to i64 - %5 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %0, i32 0, i32 0 - %6 = load i64, ptr %5, align 4 - %7 = call i32 (ptr, ...) @printf(ptr @0, i64 %4, i64 %6) - %8 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %1, i32 0, i32 6 - %9 = load i8, ptr %8, align 1 - %10 = sext i8 %9 to i64 - %11 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %1, i32 0, i32 0 - %12 = load i64, ptr %11, align 4 - %13 = call i32 (ptr, ...) @printf(ptr @1, i64 %10, i64 %12) + %1 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %0, i32 0, i32 6 + %2 = load i8, ptr %1, align 1 + %3 = sext i8 %2 to i64 + %4 = getelementptr inbounds %"github.com/goplus/llgo/internal/abi.Type", ptr %0, i32 0, i32 0 + %5 = load i64, ptr %4, align 4 + %6 = call i32 (ptr, ...) @printf(ptr @0, i64 %3, i64 %5) ret void } @@ -77,6 +68,4 @@ declare void @"github.com/goplus/llgo/internal/abi.init"() declare void @"github.com/goplus/llgo/internal/runtime.init"() -declare ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64) - declare i32 @printf(ptr, ...) diff --git a/internal/runtime/llgo_autogen.ll b/internal/runtime/llgo_autogen.ll index 2cde6fb9..a51d9cbe 100644 --- a/internal/runtime/llgo_autogen.ll +++ b/internal/runtime/llgo_autogen.ll @@ -1,23 +1,19 @@ ; 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 [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 +@0 = private unnamed_addr constant [21 x i8] c"I2Int: type mismatch\00", align 1 +@1 = private unnamed_addr constant [11 x i8] c"panic: %s\0A\00", align 1 define ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 %0) { _llgo_0: @@ -29,16 +25,6 @@ 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 } @@ -121,7 +107,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 @1, i64 20) + %11 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @0, 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 @@ -129,7 +115,7 @@ _llgo_2: ; preds = %_llgo_0 define %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAny"(ptr %0, ptr %1) { _llgo_0: - %2 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) + %2 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 32) %3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %2, i32 0, i32 0 %4 = load ptr, ptr @"github.com/goplus/llgo/internal/runtime.TyAny", align 8 %5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %2, i32 0, i32 1 @@ -151,7 +137,7 @@ _llgo_0: define %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyInt"(ptr %0, i64 %1) { _llgo_0: - %2 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) + %2 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 32) %3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %2, i32 0, i32 0 %4 = load ptr, ptr @"github.com/goplus/llgo/internal/runtime.TyAny", align 8 %5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %2, i32 0, i32 1 @@ -176,8 +162,8 @@ define %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo _llgo_0: %1 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) store %"github.com/goplus/llgo/internal/runtime.String" %0, ptr %1, align 8 - %2 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 24) - %3 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) + %2 = load ptr, ptr getelementptr inbounds (ptr, ptr @"github.com/goplus/llgo/internal/runtime.basicTypes", i64 24), align 8 + %3 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 32) %4 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %3, i32 0, i32 0 %5 = load ptr, ptr @"github.com/goplus/llgo/internal/runtime.TyAny", align 8 %6 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %3, i32 0, i32 1 @@ -188,22 +174,18 @@ _llgo_0: store ptr %2, ptr %6, align 8 store i32 0, ptr %7, align 4 store i64 0, ptr %9, align 4 - %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 + %10 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8 + %11 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %10, i32 0, i32 0 + %12 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %10, i32 0, i32 1 + store ptr %3, ptr %11, align 8 + store ptr %1, ptr %12, align 8 + %13 = load %"github.com/goplus/llgo/internal/runtime.iface", ptr %10, align 8 + ret %"github.com/goplus/llgo/internal/runtime.iface" %13 } define %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeInterface"(ptr %0, ptr %1, ptr %2) { _llgo_0: - %3 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) + %3 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 32) %4 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %3, i32 0, i32 0 %5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %3, i32 0, i32 1 %6 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.itab", ptr %3, i32 0, i32 2 @@ -312,29 +294,28 @@ _llgo_0: %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 + %9 = icmp eq i64 %8, 24 + br i1 %9, label %_llgo_2, label %_llgo_1 _llgo_1: ; preds = %_llgo_2, %_llgo_0 ret void _llgo_2: ; preds = %_llgo_0 - %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) + %10 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %1, i32 0, i32 1 + %11 = load ptr, ptr %10, align 8 + %12 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %11, i32 0, i32 1 + %13 = load i64, ptr %12, align 4 + %14 = add i64 %13, 1 + %15 = alloca i8, i64 %14, align 1 + %16 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %11, align 8 + %17 = call ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr %15, %"github.com/goplus/llgo/internal/runtime.String" %16) + %18 = call i32 (ptr, ...) @printf(ptr @1, ptr %17) br label %_llgo_1 } define ptr @"github.com/goplus/llgo/internal/runtime.basicType"(i64 %0) { _llgo_0: - %1 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) + %1 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 48) %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 @@ -345,16 +326,6 @@ _llgo_0: store i64 %4, ptr %2, align 4 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 } @@ -368,7 +339,7 @@ _llgo_0: _llgo_1: ; preds = %_llgo_0 store i1 true, ptr @"github.com/goplus/llgo/internal/runtime.init$guard", align 1 call void @"github.com/goplus/llgo/internal/abi.init"() - %1 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) + %1 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 80) store ptr %1, ptr @"github.com/goplus/llgo/internal/runtime.TyAny", align 8 store i64 1, ptr getelementptr inbounds (i64, ptr @"github.com/goplus/llgo/internal/runtime.sizeBasicTypes", i64 1), align 4 store i64 8, ptr getelementptr inbounds (i64, ptr @"github.com/goplus/llgo/internal/runtime.sizeBasicTypes", i64 2), align 4 @@ -435,7 +406,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) + %0 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 48) %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 @@ -444,8 +415,8 @@ _llgo_0: declare ptr @malloc(i64) -declare i32 @printf(ptr, ...) - declare ptr @memcpy(ptr, ptr, i64) +declare i32 @printf(ptr, ...) + declare void @"github.com/goplus/llgo/internal/abi.init"() diff --git a/internal/runtime/z_c.go b/internal/runtime/z_c.go index c6810222..829ba0cd 100644 --- a/internal/runtime/z_c.go +++ b/internal/runtime/z_c.go @@ -31,7 +31,6 @@ func Alloc(size uintptr) unsafe.Pointer { // TracePanic prints panic message. func TracePanic(v Interface) { 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 0c5b7924..9c8f4f6f 100644 --- a/internal/runtime/z_iface.go +++ b/internal/runtime/z_iface.go @@ -20,7 +20,6 @@ import ( "unsafe" "github.com/goplus/llgo/internal/abi" - "github.com/goplus/llgo/internal/runtime/c" ) // ----------------------------------------------------------------------------- @@ -38,34 +37,29 @@ type Interface = iface func MakeAnyInt(typ *Type, data uintptr) Interface { tab := &itab{inter: TyAny, _type: typ, hash: 0, fun: [1]uintptr{0}} return Interface{ - tab: tab, - data: unsafe.Pointer(data), + tab: tab, data: unsafe.Pointer(data), } } func MakeAnyString(data string) Interface { - typ := Basic(abi.String) + typ := basicTypes[abi.String] tab := &itab{inter: TyAny, _type: typ, hash: 0, fun: [1]uintptr{0}} - c.Printf(c.Str("MakeAnyString(%p): %d, %d\n"), typ, int(typ.Kind_), abi.String) return Interface{ - tab: tab, - data: unsafe.Pointer(&data), + tab: tab, data: unsafe.Pointer(&data), } } func MakeAny(typ *Type, data unsafe.Pointer) Interface { tab := &itab{inter: TyAny, _type: typ, hash: 0, fun: [1]uintptr{0}} return Interface{ - tab: tab, - data: data, + tab: tab, data: data, } } func MakeInterface(inter *InterfaceType, typ *Type, data unsafe.Pointer) Interface { tab := &itab{inter: inter, _type: typ, hash: 0, fun: [1]uintptr{0}} return Interface{ - tab: tab, - data: data, + tab: tab, data: data, } } diff --git a/internal/runtime/z_type.go b/internal/runtime/z_type.go index 349396ae..e70a81ff 100644 --- a/internal/runtime/z_type.go +++ b/internal/runtime/z_type.go @@ -20,7 +20,6 @@ import ( "unsafe" "github.com/goplus/llgo/internal/abi" - "github.com/goplus/llgo/internal/runtime/c" ) // ----------------------------------------------------------------------------- @@ -29,10 +28,7 @@ type Kind = abi.Kind type Type = abi.Type func Basic(kind Kind) *Type { - 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 + return basicTypes[kind] } var ( @@ -80,14 +76,11 @@ var ( ) func basicType(kind abi.Kind) *Type { - ret := &Type{ + return &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 } // ----------------------------------------------------------------------------- diff --git a/ssa/expr.go b/ssa/expr.go index 714fd34c..c19ee41d 100644 --- a/ssa/expr.go +++ b/ssa/expr.go @@ -23,7 +23,6 @@ import ( "go/token" "go/types" "log" - "unsafe" "github.com/goplus/llvm" ) @@ -531,8 +530,8 @@ func (b Builder) Alloc(t *types.Pointer, heap bool) (ret Expr) { telem := t.Elem() if heap { pkg := b.fn.pkg - size := unsafe.Sizeof(telem) - ret = b.Call(pkg.rtFunc("Alloc"), prog.Val(size)) + size := prog.sizs.Sizeof(telem) + ret = b.Call(pkg.rtFunc("Alloc"), prog.Val(uintptr(size))) } else { ret.impl = llvm.CreateAlloca(b.impl, prog.Type(telem).ll) } diff --git a/ssa/package.go b/ssa/package.go index 3e87c029..900c7da9 100644 --- a/ssa/package.go +++ b/ssa/package.go @@ -19,6 +19,7 @@ package ssa import ( "go/constant" "go/types" + "runtime" "github.com/goplus/llvm" "golang.org/x/tools/go/types/typeutil" @@ -97,6 +98,7 @@ func Initialize(flags InitFlags) { type aProgram struct { ctx llvm.Context typs typeutil.Map + sizs types.Sizes rt *types.Package rtget func() *types.Package @@ -139,11 +141,16 @@ func NewProgram(target *Target) Program { if target == nil { target = &Target{} } + arch := target.GOARCH + if arch == "" { + arch = runtime.GOARCH + } ctx := llvm.NewContext() + sizes := types.SizesFor("gc", arch) // TODO(xsw): Finalize may cause panic, so comment it. // ctx.Finalize() td := llvm.NewTargetData("") // TODO(xsw): target config - return &aProgram{ctx: ctx, target: target, td: td} + return &aProgram{ctx: ctx, sizs: sizes, target: target, td: td} } // SetRuntime sets the runtime.