stage2: fix x86_64 C ABI of struct with array member

Closes #12185
This commit is contained in:
Veikka Tuominen
2022-10-20 10:37:05 +03:00
parent ce95cc170b
commit 51491186cb
3 changed files with 44 additions and 0 deletions

View File

@@ -388,6 +388,19 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class {
}
return result;
},
.Array => {
const ty_size = ty.abiSize(target);
if (ty_size <= 64) {
result[0] = .integer;
return result;
}
if (ty_size <= 128) {
result[0] = .integer;
result[1] = .integer;
return result;
}
return memory_class;
},
else => unreachable,
}
}

View File

@@ -596,3 +596,18 @@ int32_t c_ret_i32() {
int64_t c_ret_i64() {
return -1;
}
typedef struct {
uint32_t a;
uint8_t padding[4];
uint64_t b;
} StructWithArray;
void c_struct_with_array(StructWithArray x) {
assert_or_panic(x.a == 1);
assert_or_panic(x.b == 2);
}
StructWithArray c_ret_struct_with_array() {
return (StructWithArray) { 4, {}, 155 };
}

View File

@@ -665,3 +665,19 @@ test "C ABI integer return types" {
try expect(c_ret_i32() == -1);
try expect(c_ret_i64() == -1);
}
const StructWithArray = extern struct {
a: i32,
padding: [4]u8,
b: i64,
};
extern fn c_struct_with_array(StructWithArray) void;
extern fn c_ret_struct_with_array() StructWithArray;
test "Struct with array as padding." {
c_struct_with_array(.{ .a = 1, .padding = undefined, .b = 2 });
var x = c_ret_struct_with_array();
try std.testing.expect(x.a == 4);
try std.testing.expect(x.b == 155);
}