diff --git a/internal/cabi/_testdata/arch/amd64/empty.ll b/internal/cabi/_testdata/arch/amd64/empty.ll new file mode 100644 index 00000000..12759287 --- /dev/null +++ b/internal/cabi/_testdata/arch/amd64/empty.ll @@ -0,0 +1,54 @@ +; ModuleID = '../../wrap/empty.c' +source_filename = "../../wrap/empty.c" +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "amd64-unknown-linux-gnu" + +%struct.empty = type {} + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local void @demo1() #0 { + %1 = alloca %struct.empty, align 1 + %2 = alloca %struct.empty, align 1 + %3 = bitcast %struct.empty* %1 to i8* + %4 = bitcast %struct.empty* %2 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %3, i8* align 1 %4, i64 0, i1 false) + ret void +} + +; Function Attrs: argmemonly nofree nounwind willreturn +declare void @llvm.memcpy.p0i8.p0i8.i64(i8* noalias nocapture writeonly, i8* noalias nocapture readonly, i64, i1 immarg) #1 + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local i32 @demo2(i32 noundef %0) #0 { + %2 = alloca %struct.empty, align 1 + %3 = alloca i32, align 4 + store i32 %0, i32* %3, align 4 + %4 = load i32, i32* %3, align 4 + ret i32 %4 +} + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local i32 @demo3(i32 noundef %0, i32 noundef %1) #0 { + %3 = alloca %struct.empty, align 1 + %4 = alloca i32, align 4 + %5 = alloca i32, align 4 + store i32 %0, i32* %4, align 4 + store i32 %1, i32* %5, align 4 + %6 = load i32, i32* %4, align 4 + %7 = load i32, i32* %5, align 4 + %8 = add nsw i32 %6, %7 + ret i32 %8 +} + +attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #1 = { argmemonly nofree nounwind willreturn } + +!llvm.module.flags = !{!0, !1, !2, !3, !4} +!llvm.ident = !{!5} + +!0 = !{i32 1, !"wchar_size", i32 4} +!1 = !{i32 7, !"PIC Level", i32 2} +!2 = !{i32 7, !"PIE Level", i32 2} +!3 = !{i32 7, !"uwtable", i32 2} +!4 = !{i32 7, !"frame-pointer", i32 2} +!5 = !{!"Apple clang version 14.0.3 (clang-1403.0.22.14.1)"} diff --git a/internal/cabi/_testdata/arch/arm64/empty.ll b/internal/cabi/_testdata/arch/arm64/empty.ll new file mode 100644 index 00000000..7917b49c --- /dev/null +++ b/internal/cabi/_testdata/arch/arm64/empty.ll @@ -0,0 +1,58 @@ +; ModuleID = '../../wrap/empty.c' +source_filename = "../../wrap/empty.c" +target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" +target triple = "aarch64-unknown-linux-gnu" + +%struct.empty = type {} + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local void @demo1() #0 { + %1 = alloca %struct.empty, align 1 + %2 = alloca %struct.empty, align 1 + %3 = bitcast %struct.empty* %1 to i8* + %4 = bitcast %struct.empty* %2 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %3, i8* align 1 %4, i64 0, i1 false) + ret void +} + +; Function Attrs: argmemonly nofree nounwind willreturn +declare void @llvm.memcpy.p0i8.p0i8.i64(i8* noalias nocapture writeonly, i8* noalias nocapture readonly, i64, i1 immarg) #1 + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local i32 @demo2(i32 noundef %0) #0 { + %2 = alloca %struct.empty, align 1 + %3 = alloca i32, align 4 + store i32 %0, i32* %3, align 4 + %4 = load i32, i32* %3, align 4 + ret i32 %4 +} + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local i32 @demo3(i32 noundef %0, i32 noundef %1) #0 { + %3 = alloca %struct.empty, align 1 + %4 = alloca i32, align 4 + %5 = alloca i32, align 4 + store i32 %0, i32* %4, align 4 + store i32 %1, i32* %5, align 4 + %6 = load i32, i32* %4, align 4 + %7 = load i32, i32* %5, align 4 + %8 = add nsw i32 %6, %7 + ret i32 %8 +} + +attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="non-leaf" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic" "target-features"="+neon,+v8a" } +attributes #1 = { argmemonly nofree nounwind willreturn } + +!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} +!llvm.ident = !{!9} + +!0 = !{i32 1, !"wchar_size", i32 4} +!1 = !{i32 8, !"branch-target-enforcement", i32 0} +!2 = !{i32 8, !"sign-return-address", i32 0} +!3 = !{i32 8, !"sign-return-address-all", i32 0} +!4 = !{i32 8, !"sign-return-address-with-bkey", i32 0} +!5 = !{i32 7, !"PIC Level", i32 2} +!6 = !{i32 7, !"PIE Level", i32 2} +!7 = !{i32 7, !"uwtable", i32 2} +!8 = !{i32 7, !"frame-pointer", i32 1} +!9 = !{!"Apple clang version 14.0.3 (clang-1403.0.22.14.1)"} diff --git a/internal/cabi/_testdata/arch/armv6/empty.ll b/internal/cabi/_testdata/arch/armv6/empty.ll new file mode 100644 index 00000000..db1d6eab --- /dev/null +++ b/internal/cabi/_testdata/arch/armv6/empty.ll @@ -0,0 +1,58 @@ +; ModuleID = '../../wrap/empty.c' +source_filename = "../../wrap/empty.c" +target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64" +target triple = "armv6kz-unknown-linux-gnueabihf" + +%struct.empty = type {} + +; Function Attrs: noinline nounwind optnone +define dso_local void @demo1() #0 { + %1 = alloca %struct.empty, align 1 + %2 = alloca %struct.empty, align 1 + %3 = bitcast %struct.empty* %1 to i8* + %4 = bitcast %struct.empty* %2 to i8* + call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 1 %3, i8* align 1 %4, i32 0, i1 false) + ret void +} + +; Function Attrs: argmemonly nofree nounwind willreturn +declare void @llvm.memcpy.p0i8.p0i8.i32(i8* noalias nocapture writeonly, i8* noalias nocapture readonly, i32, i1 immarg) #1 + +; Function Attrs: noinline nounwind optnone +define dso_local i32 @demo2(i32 noundef %0) #0 { + %2 = alloca %struct.empty, align 1 + %3 = alloca i32, align 4 + store i32 %0, i32* %3, align 4 + %4 = load i32, i32* %3, align 4 + ret i32 %4 +} + +; Function Attrs: noinline nounwind optnone +define dso_local i32 @demo3(i32 noundef %0, i32 noundef %1) #0 { + %3 = alloca %struct.empty, align 1 + %4 = alloca i32, align 4 + %5 = alloca i32, align 4 + store i32 %0, i32* %4, align 4 + store i32 %1, i32* %5, align 4 + %6 = load i32, i32* %4, align 4 + %7 = load i32, i32* %5, align 4 + %8 = add nsw i32 %6, %7 + ret i32 %8 +} + +attributes #0 = { noinline nounwind optnone "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="arm1176jzf-s" "target-features"="+armv6kz,+dsp,+fp64,+strict-align,+vfp2,+vfp2sp,-aes,-d32,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fullfp16,-neon,-sha2,-thumb-mode,-vfp3,-vfp3d16,-vfp3d16sp,-vfp3sp,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp" } +attributes #1 = { argmemonly nofree nounwind willreturn } + +!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} +!llvm.ident = !{!9} + +!0 = !{i32 1, !"wchar_size", i32 4} +!1 = !{i32 1, !"min_enum_size", i32 4} +!2 = !{i32 8, !"branch-target-enforcement", i32 0} +!3 = !{i32 8, !"sign-return-address", i32 0} +!4 = !{i32 8, !"sign-return-address-all", i32 0} +!5 = !{i32 8, !"sign-return-address-with-bkey", i32 0} +!6 = !{i32 7, !"PIC Level", i32 2} +!7 = !{i32 7, !"PIE Level", i32 2} +!8 = !{i32 7, !"frame-pointer", i32 2} +!9 = !{!"Apple clang version 14.0.3 (clang-1403.0.22.14.1)"} diff --git a/internal/cabi/_testdata/arch/i386/empty.ll b/internal/cabi/_testdata/arch/i386/empty.ll new file mode 100644 index 00000000..118fc29a --- /dev/null +++ b/internal/cabi/_testdata/arch/i386/empty.ll @@ -0,0 +1,57 @@ +; ModuleID = '../../wrap/empty.c' +source_filename = "../../wrap/empty.c" +target datalayout = "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-f64:32:64-f80:32-n8:16:32-S128" +target triple = "i386-unknown-linux-gnu" + +%struct.empty = type {} + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local void @demo1(%struct.empty* noalias sret(%struct.empty) align 1 %0) #0 { + %2 = alloca i8*, align 4 + %3 = alloca %struct.empty, align 1 + %4 = bitcast %struct.empty* %0 to i8* + store i8* %4, i8** %2, align 4 + %5 = bitcast %struct.empty* %0 to i8* + %6 = bitcast %struct.empty* %3 to i8* + call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 1 %5, i8* align 1 %6, i32 0, i1 false) + ret void +} + +; Function Attrs: argmemonly nofree nounwind willreturn +declare void @llvm.memcpy.p0i8.p0i8.i32(i8* noalias nocapture writeonly, i8* noalias nocapture readonly, i32, i1 immarg) #1 + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local i32 @demo2(i32 noundef %0) #0 { + %2 = alloca %struct.empty, align 1 + %3 = alloca i32, align 4 + store i32 %0, i32* %3, align 4 + %4 = load i32, i32* %3, align 4 + ret i32 %4 +} + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local i32 @demo3(i32 noundef %0, i32 noundef %1) #0 { + %3 = alloca %struct.empty, align 1 + %4 = alloca i32, align 4 + %5 = alloca i32, align 4 + store i32 %0, i32* %4, align 4 + store i32 %1, i32* %5, align 4 + %6 = load i32, i32* %4, align 4 + %7 = load i32, i32* %5, align 4 + %8 = add nsw i32 %6, %7 + ret i32 %8 +} + +attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="pentium4" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #1 = { argmemonly nofree nounwind willreturn } + +!llvm.module.flags = !{!0, !1, !2, !3, !4, !5} +!llvm.ident = !{!6} + +!0 = !{i32 1, !"NumRegisterParameters", i32 0} +!1 = !{i32 1, !"wchar_size", i32 4} +!2 = !{i32 7, !"PIC Level", i32 2} +!3 = !{i32 7, !"PIE Level", i32 2} +!4 = !{i32 7, !"uwtable", i32 2} +!5 = !{i32 7, !"frame-pointer", i32 2} +!6 = !{!"Apple clang version 14.0.3 (clang-1403.0.22.14.1)"} diff --git a/internal/cabi/_testdata/arch/riscv64/empty.ll b/internal/cabi/_testdata/arch/riscv64/empty.ll new file mode 100644 index 00000000..6772b3d8 --- /dev/null +++ b/internal/cabi/_testdata/arch/riscv64/empty.ll @@ -0,0 +1,53 @@ +; ModuleID = '../../wrap/empty.c' +source_filename = "../../wrap/empty.c" +target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n64-S128" +target triple = "riscv64-unknown-unknown-elf" + +%struct.empty = type {} + +; Function Attrs: noinline nounwind optnone +define dso_local void @demo1() #0 { + %1 = alloca %struct.empty, align 1 + %2 = alloca %struct.empty, align 1 + %3 = bitcast %struct.empty* %1 to i8* + %4 = bitcast %struct.empty* %2 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %3, i8* align 1 %4, i64 0, i1 false) + ret void +} + +; Function Attrs: argmemonly nofree nounwind willreturn +declare void @llvm.memcpy.p0i8.p0i8.i64(i8* noalias nocapture writeonly, i8* noalias nocapture readonly, i64, i1 immarg) #1 + +; Function Attrs: noinline nounwind optnone +define dso_local signext i32 @demo2(i32 noundef signext %0) #0 { + %2 = alloca %struct.empty, align 1 + %3 = alloca i32, align 4 + store i32 %0, i32* %3, align 4 + %4 = load i32, i32* %3, align 4 + ret i32 %4 +} + +; Function Attrs: noinline nounwind optnone +define dso_local signext i32 @demo3(i32 noundef signext %0, i32 noundef signext %1) #0 { + %3 = alloca %struct.empty, align 1 + %4 = alloca i32, align 4 + %5 = alloca i32, align 4 + store i32 %0, i32* %4, align 4 + store i32 %1, i32* %5, align 4 + %6 = load i32, i32* %4, align 4 + %7 = load i32, i32* %5, align 4 + %8 = add nsw i32 %6, %7 + ret i32 %8 +} + +attributes #0 = { noinline nounwind optnone "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+64bit,+a,+c,+m,+relax,-save-restore" } +attributes #1 = { argmemonly nofree nounwind willreturn } + +!llvm.module.flags = !{!0, !1, !2, !3} +!llvm.ident = !{!4} + +!0 = !{i32 1, !"wchar_size", i32 4} +!1 = !{i32 1, !"target-abi", !"lp64"} +!2 = !{i32 7, !"frame-pointer", i32 2} +!3 = !{i32 1, !"SmallDataLimit", i32 8} +!4 = !{!"Apple clang version 14.0.3 (clang-1403.0.22.14.1)"} diff --git a/internal/cabi/_testdata/arch/wasm32/empty.ll b/internal/cabi/_testdata/arch/wasm32/empty.ll new file mode 100644 index 00000000..8e6de221 --- /dev/null +++ b/internal/cabi/_testdata/arch/wasm32/empty.ll @@ -0,0 +1,50 @@ +; ModuleID = '../../wrap/empty.c' +source_filename = "../../wrap/empty.c" +target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-f128:64-n32:64-S128-ni:1:10:20" +target triple = "wasm32-unknown-emscripten" + +%struct.empty = type {} + +; Function Attrs: noinline nounwind optnone +define hidden void @demo1() #0 { + %1 = alloca %struct.empty, align 1 + %2 = alloca %struct.empty, align 1 + %3 = bitcast %struct.empty* %1 to i8* + %4 = bitcast %struct.empty* %2 to i8* + call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 1 %3, i8* align 1 %4, i32 0, i1 false) + ret void +} + +; Function Attrs: argmemonly nofree nounwind willreturn +declare void @llvm.memcpy.p0i8.p0i8.i32(i8* noalias nocapture writeonly, i8* noalias nocapture readonly, i32, i1 immarg) #1 + +; Function Attrs: noinline nounwind optnone +define hidden i32 @demo2(i32 noundef %0) #0 { + %2 = alloca %struct.empty, align 1 + %3 = alloca i32, align 4 + store i32 %0, i32* %3, align 4 + %4 = load i32, i32* %3, align 4 + ret i32 %4 +} + +; Function Attrs: noinline nounwind optnone +define hidden i32 @demo3(i32 noundef %0, i32 noundef %1) #0 { + %3 = alloca %struct.empty, align 1 + %4 = alloca i32, align 4 + %5 = alloca i32, align 4 + store i32 %0, i32* %4, align 4 + store i32 %1, i32* %5, align 4 + %6 = load i32, i32* %4, align 4 + %7 = load i32, i32* %5, align 4 + %8 = add nsw i32 %6, %7 + ret i32 %8 +} + +attributes #0 = { noinline nounwind optnone "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic" } +attributes #1 = { argmemonly nofree nounwind willreturn } + +!llvm.module.flags = !{!0} +!llvm.ident = !{!1} + +!0 = !{i32 1, !"wchar_size", i32 4} +!1 = !{!"Apple clang version 14.0.3 (clang-1403.0.22.14.1)"} diff --git a/internal/cabi/_testdata/demo/empty.go b/internal/cabi/_testdata/demo/empty.go new file mode 100644 index 00000000..76c56335 --- /dev/null +++ b/internal/cabi/_testdata/demo/empty.go @@ -0,0 +1,57 @@ +package main + +import "unsafe" + +const ( + LLGoFiles = "../wrap/empty.c" +) + +//go:linkname printf C.printf +func printf(format *byte, __llgo_va_list ...any) int32 + +func assert(info string, b bool) { + if !b { + printf(unsafe.StringData("Assertion failed: %s\n\000"), unsafe.StringData(info)) + } +} + +func main() {} + +type empty struct { +} + +//go:linkname cdemo1 C.demo1 +func cdemo1(empty) empty + +func demo1(a empty) empty { + return a +} + +func init() { + assert("cdemo1", cdemo1(empty{}) == empty{}) + assert("demo1", demo1(empty{}) == empty{}) +} + +//go:linkname cdemo2 C.demo2 +func cdemo2(int32, empty) int32 + +func demo2(v int32, a empty) int32 { + return v +} + +func init() { + assert("cdemo2", cdemo2(100, empty{}) == 100) + assert("demo2", demo2(100, empty{}) == 100) +} + +//go:linkname cdemo3 C.demo3 +func cdemo3(int32, empty, int32) int32 + +func demo3(v int32, a empty, v2 int32) int32 { + return v + v2 +} + +func init() { + assert("cdemo3", cdemo3(1, empty{}, 100) == 101) + assert("demo3", demo3(1, empty{}, 100) == 101) +} diff --git a/internal/cabi/_testdata/wrap/empty.c b/internal/cabi/_testdata/wrap/empty.c new file mode 100644 index 00000000..558b2fed --- /dev/null +++ b/internal/cabi/_testdata/wrap/empty.c @@ -0,0 +1,14 @@ +struct empty { +}; + +struct empty demo1(struct empty a) { + return a; +} + +int demo2(int v, struct empty a) { + return v; +} + +int demo3(int v, struct empty a, int v2) { + return v + v2; +} \ No newline at end of file diff --git a/internal/cabi/arch.go b/internal/cabi/arch.go index 393eea54..7c1b0e79 100644 --- a/internal/cabi/arch.go +++ b/internal/cabi/arch.go @@ -78,7 +78,6 @@ func (p *TypeInfoAmd64) GetTypeInfo(ctx llvm.Context, typ llvm.Type, bret bool) if typ.TypeKind() == llvm.VoidTypeKind { info.Kind = AttrVoid return info - } else if typ.TypeKind() == llvm.PointerTypeKind { } info.Size = p.Sizeof(typ) info.Align = p.Alignof(typ) diff --git a/internal/cabi/cabi.go b/internal/cabi/cabi.go index 4490a580..8b755139 100644 --- a/internal/cabi/cabi.go +++ b/internal/cabi/cabi.go @@ -128,11 +128,11 @@ type TypeInfoSys interface { type AttrKind int const ( - AttrNone AttrKind = iota - AttrVoid // return type void - AttrPointer // type => type* - AttrWidthType // type => width int i16/i24/i32/i40/i48/i56/i64 float/double - AttrWidthType2 // type => width two int {i64,i16} float/double + AttrNone AttrKind = iota // keep org type + AttrVoid // return type void / param type void (size == 0) skip + AttrPointer // type => type* + AttrWidthType // type => width int i16/i24/i32/i40/i48/i56/i64 float/double + AttrWidthType2 // type => width two int {i64,i16} float/double ) type FuncInfo struct { @@ -146,7 +146,7 @@ func (p *FuncInfo) HasWrap() bool { return true } for _, t := range p.Params { - if t.Kind > AttrVoid { + if t.Kind > AttrNone { return true } } @@ -178,6 +178,9 @@ func funcInlineHint(ctx llvm.Context) llvm.Attribute { func (p *Transformer) IsWrapType(ctx llvm.Context, typ llvm.Type, bret bool) bool { if p.sys != nil { + if !bret && (typ.TypeKind() == llvm.VoidTypeKind || p.Sizeof(typ) == 0) { + return true + } return p.sys.IsWrapType(ctx, typ, bret) } return false @@ -185,6 +188,14 @@ func (p *Transformer) IsWrapType(ctx llvm.Context, typ llvm.Type, bret bool) boo func (p *Transformer) GetTypeInfo(ctx llvm.Context, typ llvm.Type, bret bool) *TypeInfo { if p.sys != nil { + if typ.TypeKind() == llvm.VoidTypeKind { + return &TypeInfo{Type: typ, Kind: AttrVoid, Type1: ctx.VoidType()} + } else if p.Sizeof(typ) == 0 { + if bret { + return &TypeInfo{Type: typ, Kind: AttrNone, Type1: typ} + } + return &TypeInfo{Type: typ, Kind: AttrVoid, Type1: ctx.VoidType()} + } return p.sys.GetTypeInfo(ctx, typ, bret) } panic("not implment: " + p.GOARCH) @@ -228,6 +239,8 @@ func (p *Transformer) transformFuncType(ctx llvm.Context, info *FuncInfo) (llvm. for _, ti := range info.Params { switch ti.Kind { + case AttrVoid: + // skip case AttrNone, AttrWidthType: paramTypes = append(paramTypes, ti.Type1) case AttrPointer: @@ -294,9 +307,15 @@ func (p *Transformer) transformFuncBody(ctx llvm.Context, info *FuncInfo, fn llv index++ } for i, ti := range info.Params { - nv := params[index] + var nv llvm.Value switch ti.Kind { default: + nv = params[index] + case AttrVoid: + nv = llvm.ConstNull(ti.Type) + fn.Param(i).ReplaceAllUsesWith(nv) + // skip + continue case AttrPointer: nv = b.CreateLoad(ti.Type, params[index], "") case AttrWidthType: @@ -366,6 +385,8 @@ func (p *Transformer) transformCallInstr(ctx llvm.Context, call llvm.Value) bool switch ti.Kind { default: nparams = append(nparams, param) + case AttrVoid: + // none case AttrPointer: ptr := llvm.CreateAlloca(b, ti.Type) b.CreateStore(param, ptr) @@ -510,6 +531,8 @@ func (p *Transformer) transformCallbackFunc(m llvm.Module, fn llvm.Value) (wrap for _, ti := range info.Params { switch ti.Kind { default: + case AttrVoid: + // none case AttrPointer: nparams = append(nparams, b.CreateLoad(ti.Type, params[index], "")) case AttrWidthType: diff --git a/internal/cabi/cabi_test.go b/internal/cabi/cabi_test.go index 7754fd5b..92fc75bc 100644 --- a/internal/cabi/cabi_test.go +++ b/internal/cabi/cabi_test.go @@ -128,7 +128,7 @@ func testFunc(t *testing.T, ctx context, td llvm.TargetData, fn llvm.Value, cfn t.Fatalf("%v %v: bad param type %v != %v", ctx, fn.Name(), ft, cft) } for i, pt := range pts { - if !checkType(td, pt, cpts[i]) { + if !checkType(td, pt, cpts[i], false) { t.Fatalf("%v %v: bad param type %v != %v", ctx, fn.Name(), ft, cft) } if i == 0 { @@ -140,18 +140,25 @@ func testFunc(t *testing.T, ctx context, td llvm.TargetData, fn llvm.Value, cfn t.Fatalf("%v %v: bad param attr type %v != %v", ctx, fn.Name(), ft, cft) } } - if !checkType(td, ft.ReturnType(), cft.ReturnType()) { + if !checkType(td, ft.ReturnType(), cft.ReturnType(), true) { t.Fatalf("%v %v: bad return type %v != %v", ctx, fn.Name(), ft, cft) } } -func checkType(td llvm.TargetData, ft llvm.Type, cft llvm.Type) bool { +func checkType(td llvm.TargetData, ft llvm.Type, cft llvm.Type, bret bool) bool { if ft == cft { return true } - if ft.TypeKind() == llvm.VoidTypeKind && cft.TypeKind() == llvm.VoidTypeKind { + if bret { + if ft.TypeKind() == llvm.VoidTypeKind && (cft.TypeKind() == llvm.VoidTypeKind || td.TypeAllocSize(cft) == 0) { + return true + } else if cft.TypeKind() == llvm.VoidTypeKind && (ft.TypeKind() == llvm.VoidTypeKind || td.TypeAllocSize(ft) == 0) { + return true + } + } else if ft.TypeKind() == llvm.VoidTypeKind && cft.TypeKind() == llvm.VoidTypeKind { return true - } else if ft.TypeKind() == llvm.VoidTypeKind || cft.TypeKind() == llvm.VoidTypeKind { + } + if ft.TypeKind() == llvm.VoidTypeKind || cft.TypeKind() == llvm.VoidTypeKind { return false } if td.ABITypeAlignment(ft) != td.ABITypeAlignment(cft) { @@ -169,7 +176,7 @@ func checkType(td llvm.TargetData, ft llvm.Type, cft llvm.Type) bool { return true } for i, t := range et { - if !checkType(td, t, cet[i]) { + if !checkType(td, t, cet[i], bret) { return false } }