diff --git a/internal/cabi/_testdata/arch/amd64/demo.ll b/internal/cabi/_testdata/arch/amd64/demo.ll new file mode 100644 index 00000000..0b36b739 --- /dev/null +++ b/internal/cabi/_testdata/arch/amd64/demo.ll @@ -0,0 +1,36 @@ +; ModuleID = '../../wrap/demo.c' +source_filename = "../../wrap/demo.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.st1 = type { i32, i32 } + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local i64 @fn1(i64 %0) #0 { + %2 = alloca %struct.st1, align 4 + %3 = alloca %struct.st1, align 4 + %4 = bitcast %struct.st1* %3 to i64* + store i64 %0, i64* %4, align 4 + %5 = bitcast %struct.st1* %2 to i8* + %6 = bitcast %struct.st1* %3 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %5, i8* align 4 %6, i64 8, i1 false) + %7 = bitcast %struct.st1* %2 to i64* + %8 = load i64, i64* %7, align 4 + ret i64 %8 +} + +; 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 + +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/demo.ll b/internal/cabi/_testdata/arch/arm64/demo.ll new file mode 100644 index 00000000..7cfdb107 --- /dev/null +++ b/internal/cabi/_testdata/arch/arm64/demo.ll @@ -0,0 +1,40 @@ +; ModuleID = '../../wrap/demo.c' +source_filename = "../../wrap/demo.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.st1 = type { i32, i32 } + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local i64 @fn1(i64 %0) #0 { + %2 = alloca %struct.st1, align 4 + %3 = alloca %struct.st1, align 4 + %4 = bitcast %struct.st1* %3 to i64* + store i64 %0, i64* %4, align 4 + %5 = bitcast %struct.st1* %2 to i8* + %6 = bitcast %struct.st1* %3 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %5, i8* align 4 %6, i64 8, i1 false) + %7 = bitcast %struct.st1* %2 to i64* + %8 = load i64, i64* %7, align 4 + ret i64 %8 +} + +; 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 + +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/demo.ll b/internal/cabi/_testdata/arch/armv6/demo.ll new file mode 100644 index 00000000..cef73bbc --- /dev/null +++ b/internal/cabi/_testdata/arch/armv6/demo.ll @@ -0,0 +1,37 @@ +; ModuleID = '../../wrap/demo.c' +source_filename = "../../wrap/demo.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.st1 = type { i32, i32 } + +; Function Attrs: noinline nounwind optnone +define dso_local void @fn1(%struct.st1* noalias sret(%struct.st1) align 4 %0, [2 x i32] %1) #0 { + %3 = alloca %struct.st1, align 4 + %4 = bitcast %struct.st1* %3 to [2 x i32]* + store [2 x i32] %1, [2 x i32]* %4, align 4 + %5 = bitcast %struct.st1* %0 to i8* + %6 = bitcast %struct.st1* %3 to i8* + call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 %5, i8* align 4 %6, i32 8, 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 + +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/demo.ll b/internal/cabi/_testdata/arch/i386/demo.ll new file mode 100644 index 00000000..1b5b84fe --- /dev/null +++ b/internal/cabi/_testdata/arch/i386/demo.ll @@ -0,0 +1,39 @@ +; ModuleID = '../../wrap/demo.c' +source_filename = "../../wrap/demo.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.st1 = type { i32, i32 } + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local void @fn1(%struct.st1* noalias sret(%struct.st1) align 4 %0, i32 %1, i32 %2) #0 { + %4 = alloca i8*, align 4 + %5 = alloca %struct.st1, align 4 + %6 = bitcast %struct.st1* %0 to i8* + store i8* %6, i8** %4, align 4 + %7 = getelementptr inbounds %struct.st1, %struct.st1* %5, i32 0, i32 0 + store i32 %1, i32* %7, align 4 + %8 = getelementptr inbounds %struct.st1, %struct.st1* %5, i32 0, i32 1 + store i32 %2, i32* %8, align 4 + %9 = bitcast %struct.st1* %0 to i8* + %10 = bitcast %struct.st1* %5 to i8* + call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 %9, i8* align 4 %10, i32 8, 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 + +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/demo.ll b/internal/cabi/_testdata/arch/riscv64/demo.ll new file mode 100644 index 00000000..c022c427 --- /dev/null +++ b/internal/cabi/_testdata/arch/riscv64/demo.ll @@ -0,0 +1,35 @@ +; ModuleID = '../../wrap/demo.c' +source_filename = "../../wrap/demo.c" +target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n64-S128" +target triple = "riscv64-unknown-unknown-elf" + +%struct.st1 = type { i32, i32 } + +; Function Attrs: noinline nounwind optnone +define dso_local i64 @fn1(i64 %0) #0 { + %2 = alloca %struct.st1, align 4 + %3 = alloca %struct.st1, align 4 + %4 = bitcast %struct.st1* %3 to i64* + store i64 %0, i64* %4, align 4 + %5 = bitcast %struct.st1* %2 to i8* + %6 = bitcast %struct.st1* %3 to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %5, i8* align 4 %6, i64 8, i1 false) + %7 = bitcast %struct.st1* %2 to i64* + %8 = load i64, i64* %7, align 4 + ret i64 %8 +} + +; 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 + +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/demo.ll b/internal/cabi/_testdata/arch/wasm32/demo.ll new file mode 100644 index 00000000..9826f6c5 --- /dev/null +++ b/internal/cabi/_testdata/arch/wasm32/demo.ll @@ -0,0 +1,26 @@ +; ModuleID = '../../wrap/demo.c' +source_filename = "../../wrap/demo.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.st1 = type { i32, i32 } + +; Function Attrs: noinline nounwind optnone +define hidden void @fn1(%struct.st1* noalias sret(%struct.st1) align 4 %0, %struct.st1* noundef byval(%struct.st1) align 4 %1) #0 { + %3 = bitcast %struct.st1* %0 to i8* + %4 = bitcast %struct.st1* %1 to i8* + call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 %3, i8* align 4 %4, i32 8, 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 + +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/arch.go b/internal/cabi/arch.go index 7e0b4733..120f99e0 100644 --- a/internal/cabi/arch.go +++ b/internal/cabi/arch.go @@ -4,6 +4,8 @@ import ( "github.com/goplus/llvm" ) +const skip_same_type = false + func elementTypesCount(typ llvm.Type) int { switch typ.TypeKind() { case llvm.VoidTypeKind: @@ -93,7 +95,7 @@ func (p *TypeInfoAmd64) GetTypeInfo(ctx llvm.Context, typ llvm.Type, bret bool) } } else { types := elementTypes(p.td, typ) - if n == 2 { + if n == 2 && skip_same_type { // skip (float32,float32) if types[0] == ctx.FloatType() && types[1] == ctx.FloatType() { return info @@ -214,9 +216,6 @@ func (p *TypeInfoArm) SupportByVal() bool { func (p *TypeInfoArm) IsWrapType(ctx llvm.Context, typ llvm.Type, bret bool) bool { switch typ.TypeKind() { case llvm.StructTypeKind, llvm.ArrayTypeKind: - if bret && elementTypesCount(typ) == 1 { - return false - } return true default: return false @@ -238,9 +237,6 @@ func (p *TypeInfoArm) GetTypeInfo(ctx llvm.Context, typ llvm.Type, bret bool) *T case llvm.StructTypeKind, llvm.ArrayTypeKind: types := elementTypes(p.td, typ) n := len(types) - if bret && n == 1 { - return info - } if n <= 4 { if checkTypes(types, ctx.FloatType()) || checkTypes(types, ctx.DoubleType()) { return info @@ -252,7 +248,7 @@ func (p *TypeInfoArm) GetTypeInfo(ctx llvm.Context, typ llvm.Type, bret bool) *T info.Type1 = llvm.PointerType(typ, 0) } else { info.Kind = AttrWidthType - info.Type1 = ctx.Int32Type() + info.Type1 = ctx.IntType(info.Size * 8) } } else { if info.Size > 64 { @@ -261,9 +257,11 @@ func (p *TypeInfoArm) GetTypeInfo(ctx llvm.Context, typ llvm.Type, bret bool) *T } else { info.Kind = AttrWidthType if hasTypes(types, ctx.Int64Type()) || hasTypes(types, ctx.DoubleType()) { - info.Type1 = llvm.ArrayType(ctx.Int64Type(), info.Size/8) + size := (info.Size + 7) &^ 7 + info.Type1 = llvm.ArrayType(ctx.Int64Type(), size/8) } else { - info.Type1 = llvm.ArrayType(ctx.Int32Type(), info.Size/4) + size := (info.Size + 3) &^ 3 + info.Type1 = llvm.ArrayType(ctx.Int32Type(), size/4) } } } diff --git a/internal/cabi/cabi_test.go b/internal/cabi/cabi_test.go index c28f5fa5..72f7f02e 100644 --- a/internal/cabi/cabi_test.go +++ b/internal/cabi/cabi_test.go @@ -28,7 +28,7 @@ func TestBuild(t *testing.T) { conf.AbiMode = mode conf.Goarch = arch conf.Goos = "linux" - _, err := build.Do([]string{"./_testdata/demo/main.go"}, conf) + _, err := build.Do([]string{"./_testdata/demo/demo.go"}, conf) if err != nil { t.Fatalf("build error: %v-%v %v", arch, mode, err) } @@ -105,23 +105,23 @@ func testFunc(t *testing.T, ctx context, td llvm.TargetData, fn llvm.Value, cfn pts := ft.ParamTypes() cpts := cft.ParamTypes() if len(pts) != len(cpts) { - t.Fatalf("%v %v: bad param types count %v != %v", ctx, fn.Name(), len(pts), len(cpts)) + 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]) { - t.Fatalf("%v %v: bad param type %v != %v", ctx, fn.Name(), pt, cpts[i]) + t.Fatalf("%v %v: bad param type %v != %v", ctx, fn.Name(), ft, cft) } if i == 0 { if fn.GetStringAttributeAtIndex(1, "sret") != cfn.GetStringAttributeAtIndex(1, "sret") { - t.Fatalf("%v %v: bad sret attr", ctx, fn.Name()) + t.Fatalf("%v %v: bad param attr type %v != %v", ctx, fn.Name(), ft, cft) } } if fn.GetStringAttributeAtIndex(1, "byval") != cfn.GetStringAttributeAtIndex(1, "byval") { - t.Fatalf("%v %v: bad byval attr %v", ctx, fn.Name(), i) + t.Fatalf("%v %v: bad param attr type %v != %v", ctx, fn.Name(), ft, cft) } } if !checkType(td, ft.ReturnType(), cft.ReturnType()) { - t.Fatalf("%v %v: bad return type %v != %v", ctx, fn.Name(), ft.ReturnType(), cft.ReturnType()) + t.Fatalf("%v %v: bad return type %v != %v", ctx, fn.Name(), ft, cft) } }