authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-20 15:59:29+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-20 20:11:12+03:00
log2609e33ab08850405682a79f60cca66c13f9a40d
tree7e66fd7c20ddd367dec08ca90c026ecf20b5b0a2
parent646d927c792dbdd6db4a5bbee3cf5847283fe861

make C ABI tests compile on arm, mips and riscv

x86_64 24/25 x86 15/25 aarch64 25/25 - all arm 18/25 mips 10/24 riscv64 13/25 wasm32 25/25 - all

2 files changed, 71 insertions(+), 9 deletions(-)

test/c_abi/cfuncs.c+27-3
......@@ -16,10 +16,22 @@ static void assert_or_panic(bool ok) {
1616# define ZIG_NO_I128
1717#endif
1818
19#ifdef __arm__
20# define ZIG_NO_I128
21#endif
22
23#ifdef __mips__
24# define ZIG_NO_I128
25#endif
26
1927#ifdef __i386__
2028# define ZIG_NO_COMPLEX
2129#endif
2230
31#ifdef __mips__
32# define ZIG_NO_COMPLEX
33#endif
34
2335#ifndef ZIG_NO_I128
2436struct i128 {
2537 __int128 value;
......@@ -237,12 +249,14 @@ void run_c_tests(void) {
237249 }
238250#endif
239251
252#if !defined __mips__ && !defined __riscv
240253 {
241254 struct BigStruct s = {1, 2, 3, 4, 5};
242255 zig_big_struct(s);
243256 }
257#endif
244258
245#ifndef __i386__
259#if !defined __i386__ && !defined __arm__ && !defined __mips__ && !defined __riscv
246260 {
247261 struct SmallStructInts s = {1, 2, 3, 4};
248262 zig_small_struct_ints(s);
......@@ -267,25 +281,28 @@ void run_c_tests(void) {
267281 zig_small_packed_struct(s);
268282 }
269283
270#ifndef __i386__
284#if !defined __i386__ && !defined __arm__ && !defined __mips__ && !defined __riscv
271285 {
272286 struct SplitStructInts s = {1234, 100, 1337};
273287 zig_split_struct_ints(s);
274288 }
275289#endif
276290
291#if !defined __arm__ && !defined __riscv
277292 {
278293 struct MedStructMixed s = {1234, 100.0f, 1337.0f};
279294 zig_med_struct_mixed(s);
280295 }
296#endif
281297
282#ifndef __i386__
298#if !defined __i386__ && !defined __arm__ && !defined __mips__ && !defined __riscv
283299 {
284300 struct SplitStructMixed s = {1234, 100, 1337.0f};
285301 zig_split_struct_mixed(s);
286302 }
287303#endif
288304
305#if !defined __mips__ && !defined __riscv
289306 {
290307 struct BigStruct s = {30, 31, 32, 33, 34};
291308 struct BigStruct res = zig_big_struct_both(s);
......@@ -295,25 +312,32 @@ void run_c_tests(void) {
295312 assert_or_panic(res.d == 23);
296313 assert_or_panic(res.e == 24);
297314 }
315#endif
298316
317#ifndef __riscv
299318 {
300319 struct Rect r1 = {1, 21, 16, 4};
301320 struct Rect r2 = {178, 189, 21, 15};
302321 zig_multiple_struct_ints(r1, r2);
303322 }
323#endif
304324
325#if !defined __mips__ && !defined __riscv
305326 {
306327 struct FloatRect r1 = {1, 21, 16, 4};
307328 struct FloatRect r2 = {178, 189, 21, 15};
308329 zig_multiple_struct_floats(r1, r2);
309330 }
331#endif
310332
311333 {
312334 assert_or_panic(zig_ret_bool() == 1);
313335
314336 assert_or_panic(zig_ret_u8() == 0xff);
315337 assert_or_panic(zig_ret_u16() == 0xffff);
338#ifndef __riscv
316339 assert_or_panic(zig_ret_u32() == 0xffffffff);
340#endif
317341 assert_or_panic(zig_ret_u64() == 0xffffffffffffffff);
318342
319343 assert_or_panic(zig_ret_i8() == -1);
test/c_abi/main.zig+44-6
......@@ -2,7 +2,8 @@ const std = @import("std");
22const builtin = @import("builtin");
33const print = std.debug.print;
44const expect = std.testing.expect;
5const has_i128 = builtin.cpu.arch != .i386;
5const has_i128 = builtin.cpu.arch != .i386 and !builtin.cpu.arch.isARM() and
6 !builtin.cpu.arch.isMIPS();
67
78extern fn run_c_tests() void;
89
......@@ -111,7 +112,6 @@ test "C ABI floats" {
111112}
112113
113114test "C ABI long double" {
114 if (!builtin.cpu.arch.isWasm() and !builtin.cpu.arch.isAARCH64()) return error.SkipZigTest;
115115 c_long_double(12.34);
116116}
117117
......@@ -167,8 +167,11 @@ extern fn c_cmultd_comp(a_r: f64, a_i: f64, b_r: f64, b_i: f64) ComplexDouble;
167167extern fn c_cmultf(a: ComplexFloat, b: ComplexFloat) ComplexFloat;
168168extern fn c_cmultd(a: ComplexDouble, b: ComplexDouble) ComplexDouble;
169169
170const complex_abi_compatible = builtin.cpu.arch != .i386 and !builtin.cpu.arch.isMIPS();
171
170172test "C ABI complex float" {
171 if (true) return error.SkipZigTest; // See https://github.com/ziglang/zig/issues/8465
173 if (!complex_abi_compatible) return error.SkipZigTest;
174 if (builtin.cpu.arch == .x86_64) return error.SkipZigTest; // See https://github.com/ziglang/zig/issues/8465
172175
173176 const a = ComplexFloat{ .real = 1.25, .imag = 2.6 };
174177 const b = ComplexFloat{ .real = 11.3, .imag = -1.5 };
......@@ -179,7 +182,7 @@ test "C ABI complex float" {
179182}
180183
181184test "C ABI complex float by component" {
182 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
185 if (!complex_abi_compatible) return error.SkipZigTest;
183186
184187 const a = ComplexFloat{ .real = 1.25, .imag = 2.6 };
185188 const b = ComplexFloat{ .real = 11.3, .imag = -1.5 };
......@@ -190,7 +193,7 @@ test "C ABI complex float by component" {
190193}
191194
192195test "C ABI complex double" {
193 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
196 if (!complex_abi_compatible) return error.SkipZigTest;
194197
195198 const a = ComplexDouble{ .real = 1.25, .imag = 2.6 };
196199 const b = ComplexDouble{ .real = 11.3, .imag = -1.5 };
......@@ -201,7 +204,7 @@ test "C ABI complex double" {
201204}
202205
203206test "C ABI complex double by component" {
204 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
207 if (!complex_abi_compatible) return error.SkipZigTest;
205208
206209 const a = ComplexDouble{ .real = 1.25, .imag = 2.6 };
207210 const b = ComplexDouble{ .real = 11.3, .imag = -1.5 };
......@@ -257,6 +260,9 @@ const BigStruct = extern struct {
257260extern fn c_big_struct(BigStruct) void;
258261
259262test "C ABI big struct" {
263 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
264 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
265
260266 var s = BigStruct{
261267 .a = 1,
262268 .b = 2,
......@@ -281,6 +287,8 @@ const BigUnion = extern union {
281287extern fn c_big_union(BigUnion) void;
282288
283289test "C ABI big union" {
290 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
291
284292 var x = BigUnion{
285293 .a = BigStruct{
286294 .a = 1,
......@@ -312,6 +320,9 @@ extern fn c_ret_med_struct_mixed() MedStructMixed;
312320
313321test "C ABI medium struct of ints and floats" {
314322 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
323 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
324 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
325 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
315326
316327 var s = MedStructMixed{
317328 .a = 1234,
......@@ -342,6 +353,9 @@ extern fn c_ret_small_struct_ints() SmallStructInts;
342353
343354test "C ABI small struct of ints" {
344355 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
356 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
357 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
358 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
345359
346360 var s = SmallStructInts{
347361 .a = 1,
......@@ -421,6 +435,9 @@ extern fn c_split_struct_ints(SplitStructInt) void;
421435
422436test "C ABI split struct of ints" {
423437 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
438 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
439 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
440 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
424441
425442 var s = SplitStructInt{
426443 .a = 1234,
......@@ -446,6 +463,9 @@ extern fn c_ret_split_struct_mixed() SplitStructMixed;
446463
447464test "C ABI split struct of ints and floats" {
448465 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
466 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
467 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
468 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
449469
450470 var s = SplitStructMixed{
451471 .a = 1234,
......@@ -471,6 +491,9 @@ extern fn c_multiple_struct_ints(Rect, Rect) void;
471491extern fn c_multiple_struct_floats(FloatRect, FloatRect) void;
472492
473493test "C ABI sret and byval together" {
494 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
495 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
496
474497 var s = BigStruct{
475498 .a = 1,
476499 .b = 2,
......@@ -520,6 +543,10 @@ const Vector5 = extern struct {
520543extern fn c_big_struct_floats(Vector5) void;
521544
522545test "C ABI structs of floats as parameter" {
546 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
547 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
548 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
549
523550 var v3 = Vector3{
524551 .x = 3.0,
525552 .y = 6.0,
......@@ -557,6 +584,8 @@ export fn zig_multiple_struct_ints(x: Rect, y: Rect) void {
557584}
558585
559586test "C ABI structs of ints as multiple parameters" {
587 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
588
560589 var r1 = Rect{
561590 .left = 1,
562591 .right = 21,
......@@ -591,6 +620,9 @@ export fn zig_multiple_struct_floats(x: FloatRect, y: FloatRect) void {
591620}
592621
593622test "C ABI structs of floats as multiple parameters" {
623 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
624 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
625
594626 var r1 = FloatRect{
595627 .left = 1,
596628 .right = 21,
......@@ -693,6 +725,9 @@ extern fn c_ret_struct_with_array() StructWithArray;
693725
694726test "Struct with array as padding." {
695727 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
728 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
729 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
730 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
696731
697732 c_struct_with_array(.{ .a = 1, .padding = undefined, .b = 2 });
698733
......@@ -716,6 +751,9 @@ extern fn c_float_array_struct(FloatArrayStruct) void;
716751extern fn c_ret_float_array_struct() FloatArrayStruct;
717752
718753test "Float array like struct" {
754 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
755 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
756
719757 c_float_array_struct(.{
720758 .origin = .{
721759 .x = 5,