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) {...@@ -16,10 +16,22 @@ static void assert_or_panic(bool ok) {
16# define ZIG_NO_I12816# define ZIG_NO_I128
17#endif17#endif
1818
19#ifdef __arm__
20# define ZIG_NO_I128
21#endif
22
23#ifdef __mips__
24# define ZIG_NO_I128
25#endif
26
19#ifdef __i386__27#ifdef __i386__
20# define ZIG_NO_COMPLEX28# define ZIG_NO_COMPLEX
21#endif29#endif
2230
31#ifdef __mips__
32# define ZIG_NO_COMPLEX
33#endif
34
23#ifndef ZIG_NO_I12835#ifndef ZIG_NO_I128
24struct i128 {36struct i128 {
25 __int128 value;37 __int128 value;
...@@ -237,12 +249,14 @@ void run_c_tests(void) {...@@ -237,12 +249,14 @@ void run_c_tests(void) {
237 }249 }
238#endif250#endif
239251
252#if !defined __mips__ && !defined __riscv
240 {253 {
241 struct BigStruct s = {1, 2, 3, 4, 5};254 struct BigStruct s = {1, 2, 3, 4, 5};
242 zig_big_struct(s);255 zig_big_struct(s);
243 }256 }
257#endif
244258
245#ifndef __i386__259#if !defined __i386__ && !defined __arm__ && !defined __mips__ && !defined __riscv
246 {260 {
247 struct SmallStructInts s = {1, 2, 3, 4};261 struct SmallStructInts s = {1, 2, 3, 4};
248 zig_small_struct_ints(s);262 zig_small_struct_ints(s);
...@@ -267,25 +281,28 @@ void run_c_tests(void) {...@@ -267,25 +281,28 @@ void run_c_tests(void) {
267 zig_small_packed_struct(s);281 zig_small_packed_struct(s);
268 }282 }
269283
270#ifndef __i386__284#if !defined __i386__ && !defined __arm__ && !defined __mips__ && !defined __riscv
271 {285 {
272 struct SplitStructInts s = {1234, 100, 1337};286 struct SplitStructInts s = {1234, 100, 1337};
273 zig_split_struct_ints(s);287 zig_split_struct_ints(s);
274 }288 }
275#endif289#endif
276290
291#if !defined __arm__ && !defined __riscv
277 {292 {
278 struct MedStructMixed s = {1234, 100.0f, 1337.0f};293 struct MedStructMixed s = {1234, 100.0f, 1337.0f};
279 zig_med_struct_mixed(s);294 zig_med_struct_mixed(s);
280 }295 }
296#endif
281297
282#ifndef __i386__298#if !defined __i386__ && !defined __arm__ && !defined __mips__ && !defined __riscv
283 {299 {
284 struct SplitStructMixed s = {1234, 100, 1337.0f};300 struct SplitStructMixed s = {1234, 100, 1337.0f};
285 zig_split_struct_mixed(s);301 zig_split_struct_mixed(s);
286 }302 }
287#endif303#endif
288304
305#if !defined __mips__ && !defined __riscv
289 {306 {
290 struct BigStruct s = {30, 31, 32, 33, 34};307 struct BigStruct s = {30, 31, 32, 33, 34};
291 struct BigStruct res = zig_big_struct_both(s);308 struct BigStruct res = zig_big_struct_both(s);
...@@ -295,25 +312,32 @@ void run_c_tests(void) {...@@ -295,25 +312,32 @@ void run_c_tests(void) {
295 assert_or_panic(res.d == 23);312 assert_or_panic(res.d == 23);
296 assert_or_panic(res.e == 24);313 assert_or_panic(res.e == 24);
297 }314 }
315#endif
298316
317#ifndef __riscv
299 {318 {
300 struct Rect r1 = {1, 21, 16, 4};319 struct Rect r1 = {1, 21, 16, 4};
301 struct Rect r2 = {178, 189, 21, 15};320 struct Rect r2 = {178, 189, 21, 15};
302 zig_multiple_struct_ints(r1, r2);321 zig_multiple_struct_ints(r1, r2);
303 }322 }
323#endif
304324
325#if !defined __mips__ && !defined __riscv
305 {326 {
306 struct FloatRect r1 = {1, 21, 16, 4};327 struct FloatRect r1 = {1, 21, 16, 4};
307 struct FloatRect r2 = {178, 189, 21, 15};328 struct FloatRect r2 = {178, 189, 21, 15};
308 zig_multiple_struct_floats(r1, r2);329 zig_multiple_struct_floats(r1, r2);
309 }330 }
331#endif
310332
311 {333 {
312 assert_or_panic(zig_ret_bool() == 1);334 assert_or_panic(zig_ret_bool() == 1);
313335
314 assert_or_panic(zig_ret_u8() == 0xff);336 assert_or_panic(zig_ret_u8() == 0xff);
315 assert_or_panic(zig_ret_u16() == 0xffff);337 assert_or_panic(zig_ret_u16() == 0xffff);
338#ifndef __riscv
316 assert_or_panic(zig_ret_u32() == 0xffffffff);339 assert_or_panic(zig_ret_u32() == 0xffffffff);
340#endif
317 assert_or_panic(zig_ret_u64() == 0xffffffffffffffff);341 assert_or_panic(zig_ret_u64() == 0xffffffffffffffff);
318342
319 assert_or_panic(zig_ret_i8() == -1);343 assert_or_panic(zig_ret_i8() == -1);
test/c_abi/main.zig+44-6
...@@ -2,7 +2,8 @@ const std = @import("std");...@@ -2,7 +2,8 @@ const std = @import("std");
2const builtin = @import("builtin");2const builtin = @import("builtin");
3const print = std.debug.print;3const print = std.debug.print;
4const expect = std.testing.expect;4const 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
7extern fn run_c_tests() void;8extern fn run_c_tests() void;
89
...@@ -111,7 +112,6 @@ test "C ABI floats" {...@@ -111,7 +112,6 @@ test "C ABI floats" {
111}112}
112113
113test "C ABI long double" {114test "C ABI long double" {
114 if (!builtin.cpu.arch.isWasm() and !builtin.cpu.arch.isAARCH64()) return error.SkipZigTest;
115 c_long_double(12.34);115 c_long_double(12.34);
116}116}
117117
...@@ -167,8 +167,11 @@ extern fn c_cmultd_comp(a_r: f64, a_i: f64, b_r: f64, b_i: f64) ComplexDouble;...@@ -167,8 +167,11 @@ extern fn c_cmultd_comp(a_r: f64, a_i: f64, b_r: f64, b_i: f64) ComplexDouble;
167extern fn c_cmultf(a: ComplexFloat, b: ComplexFloat) ComplexFloat;167extern fn c_cmultf(a: ComplexFloat, b: ComplexFloat) ComplexFloat;
168extern fn c_cmultd(a: ComplexDouble, b: ComplexDouble) ComplexDouble;168extern fn c_cmultd(a: ComplexDouble, b: ComplexDouble) ComplexDouble;
169169
170const complex_abi_compatible = builtin.cpu.arch != .i386 and !builtin.cpu.arch.isMIPS();
171
170test "C ABI complex float" {172test "C ABI complex float" {
171 if (true) return error.SkipZigTest; // See https://github.com/ziglang/zig/issues/8465173 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
173 const a = ComplexFloat{ .real = 1.25, .imag = 2.6 };176 const a = ComplexFloat{ .real = 1.25, .imag = 2.6 };
174 const b = ComplexFloat{ .real = 11.3, .imag = -1.5 };177 const b = ComplexFloat{ .real = 11.3, .imag = -1.5 };
...@@ -179,7 +182,7 @@ test "C ABI complex float" {...@@ -179,7 +182,7 @@ test "C ABI complex float" {
179}182}
180183
181test "C ABI complex float by component" {184test "C ABI complex float by component" {
182 if (builtin.cpu.arch == .i386) return error.SkipZigTest;185 if (!complex_abi_compatible) return error.SkipZigTest;
183186
184 const a = ComplexFloat{ .real = 1.25, .imag = 2.6 };187 const a = ComplexFloat{ .real = 1.25, .imag = 2.6 };
185 const b = ComplexFloat{ .real = 11.3, .imag = -1.5 };188 const b = ComplexFloat{ .real = 11.3, .imag = -1.5 };
...@@ -190,7 +193,7 @@ test "C ABI complex float by component" {...@@ -190,7 +193,7 @@ test "C ABI complex float by component" {
190}193}
191194
192test "C ABI complex double" {195test "C ABI complex double" {
193 if (builtin.cpu.arch == .i386) return error.SkipZigTest;196 if (!complex_abi_compatible) return error.SkipZigTest;
194197
195 const a = ComplexDouble{ .real = 1.25, .imag = 2.6 };198 const a = ComplexDouble{ .real = 1.25, .imag = 2.6 };
196 const b = ComplexDouble{ .real = 11.3, .imag = -1.5 };199 const b = ComplexDouble{ .real = 11.3, .imag = -1.5 };
...@@ -201,7 +204,7 @@ test "C ABI complex double" {...@@ -201,7 +204,7 @@ test "C ABI complex double" {
201}204}
202205
203test "C ABI complex double by component" {206test "C ABI complex double by component" {
204 if (builtin.cpu.arch == .i386) return error.SkipZigTest;207 if (!complex_abi_compatible) return error.SkipZigTest;
205208
206 const a = ComplexDouble{ .real = 1.25, .imag = 2.6 };209 const a = ComplexDouble{ .real = 1.25, .imag = 2.6 };
207 const b = ComplexDouble{ .real = 11.3, .imag = -1.5 };210 const b = ComplexDouble{ .real = 11.3, .imag = -1.5 };
...@@ -257,6 +260,9 @@ const BigStruct = extern struct {...@@ -257,6 +260,9 @@ const BigStruct = extern struct {
257extern fn c_big_struct(BigStruct) void;260extern fn c_big_struct(BigStruct) void;
258261
259test "C ABI big struct" {262test "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
260 var s = BigStruct{266 var s = BigStruct{
261 .a = 1,267 .a = 1,
262 .b = 2,268 .b = 2,
...@@ -281,6 +287,8 @@ const BigUnion = extern union {...@@ -281,6 +287,8 @@ const BigUnion = extern union {
281extern fn c_big_union(BigUnion) void;287extern fn c_big_union(BigUnion) void;
282288
283test "C ABI big union" {289test "C ABI big union" {
290 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
291
284 var x = BigUnion{292 var x = BigUnion{
285 .a = BigStruct{293 .a = BigStruct{
286 .a = 1,294 .a = 1,
...@@ -312,6 +320,9 @@ extern fn c_ret_med_struct_mixed() MedStructMixed;...@@ -312,6 +320,9 @@ extern fn c_ret_med_struct_mixed() MedStructMixed;
312320
313test "C ABI medium struct of ints and floats" {321test "C ABI medium struct of ints and floats" {
314 if (builtin.cpu.arch == .i386) return error.SkipZigTest;322 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
316 var s = MedStructMixed{327 var s = MedStructMixed{
317 .a = 1234,328 .a = 1234,
...@@ -342,6 +353,9 @@ extern fn c_ret_small_struct_ints() SmallStructInts;...@@ -342,6 +353,9 @@ extern fn c_ret_small_struct_ints() SmallStructInts;
342353
343test "C ABI small struct of ints" {354test "C ABI small struct of ints" {
344 if (builtin.cpu.arch == .i386) return error.SkipZigTest;355 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
346 var s = SmallStructInts{360 var s = SmallStructInts{
347 .a = 1,361 .a = 1,
...@@ -421,6 +435,9 @@ extern fn c_split_struct_ints(SplitStructInt) void;...@@ -421,6 +435,9 @@ extern fn c_split_struct_ints(SplitStructInt) void;
421435
422test "C ABI split struct of ints" {436test "C ABI split struct of ints" {
423 if (builtin.cpu.arch == .i386) return error.SkipZigTest;437 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
425 var s = SplitStructInt{442 var s = SplitStructInt{
426 .a = 1234,443 .a = 1234,
...@@ -446,6 +463,9 @@ extern fn c_ret_split_struct_mixed() SplitStructMixed;...@@ -446,6 +463,9 @@ extern fn c_ret_split_struct_mixed() SplitStructMixed;
446463
447test "C ABI split struct of ints and floats" {464test "C ABI split struct of ints and floats" {
448 if (builtin.cpu.arch == .i386) return error.SkipZigTest;465 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
450 var s = SplitStructMixed{470 var s = SplitStructMixed{
451 .a = 1234,471 .a = 1234,
...@@ -471,6 +491,9 @@ extern fn c_multiple_struct_ints(Rect, Rect) void;...@@ -471,6 +491,9 @@ extern fn c_multiple_struct_ints(Rect, Rect) void;
471extern fn c_multiple_struct_floats(FloatRect, FloatRect) void;491extern fn c_multiple_struct_floats(FloatRect, FloatRect) void;
472492
473test "C ABI sret and byval together" {493test "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
474 var s = BigStruct{497 var s = BigStruct{
475 .a = 1,498 .a = 1,
476 .b = 2,499 .b = 2,
...@@ -520,6 +543,10 @@ const Vector5 = extern struct {...@@ -520,6 +543,10 @@ const Vector5 = extern struct {
520extern fn c_big_struct_floats(Vector5) void;543extern fn c_big_struct_floats(Vector5) void;
521544
522test "C ABI structs of floats as parameter" {545test "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
523 var v3 = Vector3{550 var v3 = Vector3{
524 .x = 3.0,551 .x = 3.0,
525 .y = 6.0,552 .y = 6.0,
...@@ -557,6 +584,8 @@ export fn zig_multiple_struct_ints(x: Rect, y: Rect) void {...@@ -557,6 +584,8 @@ export fn zig_multiple_struct_ints(x: Rect, y: Rect) void {
557}584}
558585
559test "C ABI structs of ints as multiple parameters" {586test "C ABI structs of ints as multiple parameters" {
587 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
588
560 var r1 = Rect{589 var r1 = Rect{
561 .left = 1,590 .left = 1,
562 .right = 21,591 .right = 21,
...@@ -591,6 +620,9 @@ export fn zig_multiple_struct_floats(x: FloatRect, y: FloatRect) void {...@@ -591,6 +620,9 @@ export fn zig_multiple_struct_floats(x: FloatRect, y: FloatRect) void {
591}620}
592621
593test "C ABI structs of floats as multiple parameters" {622test "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
594 var r1 = FloatRect{626 var r1 = FloatRect{
595 .left = 1,627 .left = 1,
596 .right = 21,628 .right = 21,
...@@ -693,6 +725,9 @@ extern fn c_ret_struct_with_array() StructWithArray;...@@ -693,6 +725,9 @@ extern fn c_ret_struct_with_array() StructWithArray;
693725
694test "Struct with array as padding." {726test "Struct with array as padding." {
695 if (builtin.cpu.arch == .i386) return error.SkipZigTest;727 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
697 c_struct_with_array(.{ .a = 1, .padding = undefined, .b = 2 });732 c_struct_with_array(.{ .a = 1, .padding = undefined, .b = 2 });
698733
...@@ -716,6 +751,9 @@ extern fn c_float_array_struct(FloatArrayStruct) void;...@@ -716,6 +751,9 @@ extern fn c_float_array_struct(FloatArrayStruct) void;
716extern fn c_ret_float_array_struct() FloatArrayStruct;751extern fn c_ret_float_array_struct() FloatArrayStruct;
717752
718test "Float array like struct" {753test "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
719 c_float_array_struct(.{757 c_float_array_struct(.{
720 .origin = .{758 .origin = .{
721 .x = 5,759 .x = 5,