authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-08-26 21:01:05+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-08-26 21:01:05+02:00
log91dba79c48f0a309e014062f2fe311a5c46b1084
treebc869dd10ccb9bce56136e238cb4dfd1d9032ff5
parent8bcb962ada2ea5cfeeff303e1063937ee13af6ad
signaturelock-open Commit is signed but in an unrecognized format.

wasm: fix abi size of c_longdouble

According to https://github.com/WebAssembly/tool-conventions/blob/main/BasicCABI.md the size of c's long double is 16 bytes for Wasm, rather than 8 bytes which was the value previously in the compiler. This ensures we not only pass the correct value, but also creates the correct function signature needed to pass the Wasm validator. This also adds an additional test case in c_abi tests.

3 files changed, 15 insertions(+), 0 deletions(-)

src/type.zig+4
...@@ -6592,6 +6592,8 @@ pub const CType = enum {...@@ -6592,6 +6592,8 @@ pub const CType = enum {
6592 .powerpcle,6592 .powerpcle,
6593 .powerpc64,6593 .powerpc64,
6594 .powerpc64le,6594 .powerpc64le,
6595 .wasm32,
6596 .wasm64,
6595 => return 128,6597 => return 128,
65966598
6597 else => return 64,6599 else => return 64,
...@@ -6640,6 +6642,8 @@ pub const CType = enum {...@@ -6640,6 +6642,8 @@ pub const CType = enum {
6640 .powerpcle,6642 .powerpcle,
6641 .powerpc64,6643 .powerpc64,
6642 .powerpc64le,6644 .powerpc64le,
6645 .wasm32,
6646 .wasm64,
6643 => return 128,6647 => return 128,
66446648
6645 else => return 64,6649 else => return 64,
test/c_abi/cfuncs.c+6
...@@ -33,6 +33,7 @@ void zig_five_integers(int32_t, int32_t, int32_t, int32_t, int32_t);...@@ -33,6 +33,7 @@ void zig_five_integers(int32_t, int32_t, int32_t, int32_t, int32_t);
3333
34void zig_f32(float);34void zig_f32(float);
35void zig_f64(double);35void zig_f64(double);
36void zig_longdouble(long double);
36void zig_five_floats(float, float, float, float, float);37void zig_five_floats(float, float, float, float, float);
3738
38bool zig_ret_bool();39bool zig_ret_bool();
...@@ -157,6 +158,7 @@ void run_c_tests(void) {...@@ -157,6 +158,7 @@ void run_c_tests(void) {
157158
158 zig_f32(12.34f);159 zig_f32(12.34f);
159 zig_f64(56.78);160 zig_f64(56.78);
161 zig_longdouble(12.34l);
160 zig_five_floats(1.0f, 2.0f, 3.0f, 4.0f, 5.0f);162 zig_five_floats(1.0f, 2.0f, 3.0f, 4.0f, 5.0f);
161163
162 zig_ptr((void*)0xdeadbeefL);164 zig_ptr((void*)0xdeadbeefL);
...@@ -271,6 +273,10 @@ void c_f64(double x) {...@@ -271,6 +273,10 @@ void c_f64(double x) {
271 assert_or_panic(x == 56.78);273 assert_or_panic(x == 56.78);
272}274}
273275
276void c_long_double(long double x) {
277 assert_or_panic(x == 12.34l);
278}
279
274void c_ptr(void *x) {280void c_ptr(void *x) {
275 assert_or_panic(x == (void*)0xdeadbeefL);281 assert_or_panic(x == (void*)0xdeadbeefL);
276}282}
test/c_abi/main.zig+5
...@@ -89,6 +89,7 @@ export fn zig_struct_u128(a: U128) void {...@@ -89,6 +89,7 @@ export fn zig_struct_u128(a: U128) void {
8989
90extern fn c_f32(f32) void;90extern fn c_f32(f32) void;
91extern fn c_f64(f64) void;91extern fn c_f64(f64) void;
92extern fn c_long_double(c_longdouble) void;
9293
93// On windows x64, the first 4 are passed via registers, others on the stack.94// On windows x64, the first 4 are passed via registers, others on the stack.
94extern fn c_five_floats(f32, f32, f32, f32, f32) void;95extern fn c_five_floats(f32, f32, f32, f32, f32) void;
...@@ -105,6 +106,7 @@ test "C ABI floats" {...@@ -105,6 +106,7 @@ test "C ABI floats" {
105 c_f32(12.34);106 c_f32(12.34);
106 c_f64(56.78);107 c_f64(56.78);
107 c_five_floats(1.0, 2.0, 3.0, 4.0, 5.0);108 c_five_floats(1.0, 2.0, 3.0, 4.0, 5.0);
109 c_long_double(12.34);
108}110}
109111
110export fn zig_f32(x: f32) void {112export fn zig_f32(x: f32) void {
...@@ -113,6 +115,9 @@ export fn zig_f32(x: f32) void {...@@ -113,6 +115,9 @@ export fn zig_f32(x: f32) void {
113export fn zig_f64(x: f64) void {115export fn zig_f64(x: f64) void {
114 expect(x == 56.78) catch @panic("test failure: zig_f64");116 expect(x == 56.78) catch @panic("test failure: zig_f64");
115}117}
118export fn zig_longdouble(x: c_longdouble) void {
119 expect(x == 12.34) catch @panic("test failure: zig_longdouble");
120}
116121
117extern fn c_ptr(*anyopaque) void;122extern fn c_ptr(*anyopaque) void;
118123