Commit 61ed4fe07a

Andrew Kelley <andrew@ziglang.org>
2022-02-07 22:32:24
stage1: fix x86 i128 C ABI for extern structs
closes #10445
1 parent 7c10617
Changed files (3)
src
test
stage1
src/stage1/analyze.cpp
@@ -8684,14 +8684,23 @@ static Error resolve_llvm_c_abi_type(CodeGen *g, ZigType *ty) {
                 if (ty->data.structure.fields[i]->offset >= 8) {
                     eightbyte_index = 1;
                 }
-                X64CABIClass field_class = type_c_abi_x86_64_class(g, ty->data.structure.fields[i]->type_entry);
+                ZigType *field_ty = ty->data.structure.fields[i]->type_entry;
+                X64CABIClass field_class = type_c_abi_x86_64_class(g, field_ty);
 
                 if (field_class == X64CABIClass_INTEGER) {
                     type_classes[eightbyte_index] = X64CABIClass_INTEGER;
                 } else if (type_classes[eightbyte_index] == X64CABIClass_Unknown) {
                     type_classes[eightbyte_index] = field_class;
                 }
-                type_sizes[eightbyte_index] += ty->data.structure.fields[i]->type_entry->abi_size;
+                if (field_ty->abi_size > 8) {
+                    assert(eightbyte_index == 0);
+                    type_sizes[0] = 8;
+                    type_sizes[1] = field_ty->abi_size - 8;
+                    type_classes[1] = type_classes[0];
+                    eightbyte_index = 1;
+                } else {
+                    type_sizes[eightbyte_index] += field_ty->abi_size;
+                }
             }
 
             LLVMTypeRef return_elem_types[] = {
test/stage1/c_abi/cfuncs.c
@@ -11,14 +11,26 @@ static void assert_or_panic(bool ok) {
     }
 }
 
+struct i128 {
+    __int128 value;
+};
+
+struct u128 {
+    unsigned __int128 value;
+};
+
 void zig_u8(uint8_t);
 void zig_u16(uint16_t);
 void zig_u32(uint32_t);
 void zig_u64(uint64_t);
+void zig_u128(unsigned __int128);
+void zig_struct_u128(struct u128);
 void zig_i8(int8_t);
 void zig_i16(int16_t);
 void zig_i32(int32_t);
 void zig_i64(int64_t);
+void zig_i128(__int128);
+void zig_struct_i128(struct i128);
 void zig_five_integers(int32_t, int32_t, int32_t, int32_t, int32_t);
 
 void zig_f32(float);
@@ -130,11 +142,21 @@ void run_c_tests(void) {
     zig_u16(0xfffe);
     zig_u32(0xfffffffd);
     zig_u64(0xfffffffffffffffc);
+    zig_u128(0xfffffffffffffffc);
+    {
+        struct u128 s = {0xfffffffffffffffc};
+        zig_struct_u128(s);
+    }
 
     zig_i8(-1);
     zig_i16(-2);
     zig_i32(-3);
     zig_i64(-4);
+    zig_i128(-5);
+    {
+        struct i128 s = {-6};
+        zig_struct_i128(s);
+    }
     zig_five_integers(12, 34, 56, 78, 90);
 
     zig_f32(12.34f);
@@ -221,6 +243,14 @@ void c_u64(uint64_t x) {
     assert_or_panic(x == 0xfffffffffffffffcULL);
 }
 
+void c_u128(unsigned __int128 x) {
+    assert_or_panic(x == 0xfffffffffffffffcULL);
+}
+
+void c_struct_u128(struct u128 x) {
+    assert_or_panic(x.value == 0xfffffffffffffffcULL);
+}
+
 void c_i8(int8_t x) {
     assert_or_panic(x == -1);
 }
@@ -237,6 +267,14 @@ void c_i64(int64_t x) {
     assert_or_panic(x == -4);
 }
 
+void c_i128(__int128 x) {
+    assert_or_panic(x == -5);
+}
+
+void c_struct_i128(struct i128 x) {
+    assert_or_panic(x.value == -6);
+}
+
 void c_f32(float x) {
     assert_or_panic(x == 12.34f);
 }
test/stage1/c_abi/main.zig
@@ -16,10 +16,14 @@ extern fn c_u8(u8) void;
 extern fn c_u16(u16) void;
 extern fn c_u32(u32) void;
 extern fn c_u64(u64) void;
+extern fn c_u128(u128) void;
+extern fn c_struct_u128(U128) void;
 extern fn c_i8(i8) void;
 extern fn c_i16(i16) void;
 extern fn c_i32(i32) void;
 extern fn c_i64(i64) void;
+extern fn c_i128(i128) void;
+extern fn c_struct_i128(I128) void;
 
 // On windows x64, the first 4 are passed via registers, others on the stack.
 extern fn c_five_integers(i32, i32, i32, i32, i32) void;
@@ -37,11 +41,15 @@ test "C ABI integers" {
     c_u16(0xfffe);
     c_u32(0xfffffffd);
     c_u64(0xfffffffffffffffc);
+    c_u128(0xfffffffffffffffc);
+    c_struct_u128(.{ .value = 0xfffffffffffffffc });
 
     c_i8(-1);
     c_i16(-2);
     c_i32(-3);
     c_i64(-4);
+    c_i128(-5);
+    c_struct_i128(.{ .value = -6 });
     c_five_integers(12, 34, 56, 78, 90);
 }
 
@@ -57,6 +65,9 @@ export fn zig_u32(x: u32) void {
 export fn zig_u64(x: u64) void {
     expect(x == 0xfffffffffffffffc) catch @panic("test failure");
 }
+export fn zig_u128(x: u128) void {
+    expect(x == 0xfffffffffffffffc) catch @panic("test failure");
+}
 export fn zig_i8(x: i8) void {
     expect(x == -1) catch @panic("test failure");
 }
@@ -69,6 +80,22 @@ export fn zig_i32(x: i32) void {
 export fn zig_i64(x: i64) void {
     expect(x == -4) catch @panic("test failure");
 }
+export fn zig_i128(x: i128) void {
+    expect(x == -5) catch @panic("test failure");
+}
+
+const I128 = extern struct {
+    value: i128,
+};
+const U128 = extern struct {
+    value: u128,
+};
+export fn zig_struct_i128(a: I128) void {
+    expect(a.value == -6) catch @panic("test failure");
+}
+export fn zig_struct_u128(a: U128) void {
+    expect(a.value == 0xfffffffffffffffc) catch @panic("test failure");
+}
 
 extern fn c_f32(f32) void;
 extern fn c_f64(f64) void;