authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-20 10:52:26+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-20 20:11:12+03:00
log7e946bc790c245242af3716199878cf99c369d48
tree8b102f5d9c350d91b409d1acf31ed1d925673667
parent51491186cb045520ace001e005164a97a8695504

make C ABI tests compile on i386


2 files changed, 60 insertions(+), 2 deletions(-)

test/c_abi/cfuncs.c+39
......@@ -12,6 +12,15 @@ static void assert_or_panic(bool ok) {
1212 }
1313}
1414
15#ifdef __i386__
16# define ZIG_NO_I128
17#endif
18
19#ifdef __i386__
20# define ZIG_NO_COMPLEX
21#endif
22
23#ifndef ZIG_NO_I128
1524struct i128 {
1625 __int128 value;
1726};
......@@ -19,17 +28,22 @@ struct i128 {
1928struct u128 {
2029 unsigned __int128 value;
2130};
31#endif
2232
2333void zig_u8(uint8_t);
2434void zig_u16(uint16_t);
2535void zig_u32(uint32_t);
2636void zig_u64(uint64_t);
37#ifndef ZIG_NO_I128
2738void zig_struct_u128(struct u128);
39#endif
2840void zig_i8(int8_t);
2941void zig_i16(int16_t);
3042void zig_i32(int32_t);
3143void zig_i64(int64_t);
44#ifndef ZIG_NO_I128
3245void zig_struct_i128(struct i128);
46#endif
3347void zig_five_integers(int32_t, int32_t, int32_t, int32_t, int32_t);
3448
3549void zig_f32(float);
......@@ -95,7 +109,9 @@ void zig_med_struct_mixed(struct MedStructMixed);
95109struct MedStructMixed zig_ret_med_struct_mixed();
96110
97111void zig_small_packed_struct(uint8_t);
112#ifndef ZIG_NO_I128
98113void zig_big_packed_struct(__int128);
114#endif
99115
100116struct SplitStructInts {
101117 uint64_t a;
......@@ -151,19 +167,26 @@ void run_c_tests(void) {
151167 zig_u16(0xfffe);
152168 zig_u32(0xfffffffd);
153169 zig_u64(0xfffffffffffffffc);
170
171#ifndef ZIG_NO_I128
154172 {
155173 struct u128 s = {0xfffffffffffffffc};
156174 zig_struct_u128(s);
157175 }
176#endif
158177
159178 zig_i8(-1);
160179 zig_i16(-2);
161180 zig_i32(-3);
162181 zig_i64(-4);
182
183#ifndef ZIG_NO_I128
163184 {
164185 struct i128 s = {-6};
165186 zig_struct_i128(s);
166187 }
188#endif
189
167190 zig_five_integers(12, 34, 56, 78, 90);
168191
169192 zig_f32(12.34f);
......@@ -175,6 +198,7 @@ void run_c_tests(void) {
175198
176199 zig_bool(true);
177200
201#ifndef ZIG_NO_COMPLEX
178202 // TODO: Resolve https://github.com/ziglang/zig/issues/8465
179203 //{
180204 // float complex a = 1.25f + I * 2.6f;
......@@ -211,23 +235,28 @@ void run_c_tests(void) {
211235 assert_or_panic(creal(z) == 1.5);
212236 assert_or_panic(cimag(z) == 13.5);
213237 }
238#endif
214239
215240 {
216241 struct BigStruct s = {1, 2, 3, 4, 5};
217242 zig_big_struct(s);
218243 }
219244
245#ifndef __i386__
220246 {
221247 struct SmallStructInts s = {1, 2, 3, 4};
222248 zig_small_struct_ints(s);
223249 }
250#endif
224251
252#ifndef ZIG_NO_I128
225253 {
226254 __int128 s = 0;
227255 s |= 1 << 0;
228256 s |= (__int128)2 << 64;
229257 zig_big_packed_struct(s);
230258 }
259#endif
231260
232261 {
233262 uint8_t s = 0;
......@@ -238,20 +267,24 @@ void run_c_tests(void) {
238267 zig_small_packed_struct(s);
239268 }
240269
270#ifndef __i386__
241271 {
242272 struct SplitStructInts s = {1234, 100, 1337};
243273 zig_split_struct_ints(s);
244274 }
275#endif
245276
246277 {
247278 struct MedStructMixed s = {1234, 100.0f, 1337.0f};
248279 zig_med_struct_mixed(s);
249280 }
250281
282#ifndef __i386__
251283 {
252284 struct SplitStructMixed s = {1234, 100, 1337.0f};
253285 zig_split_struct_mixed(s);
254286 }
287#endif
255288
256289 {
257290 struct BigStruct s = {30, 31, 32, 33, 34};
......@@ -306,9 +339,11 @@ void c_u64(uint64_t x) {
306339 assert_or_panic(x == 0xfffffffffffffffcULL);
307340}
308341
342#ifndef ZIG_NO_I128
309343void c_struct_u128(struct u128 x) {
310344 assert_or_panic(x.value == 0xfffffffffffffffcULL);
311345}
346#endif
312347
313348void c_i8(int8_t x) {
314349 assert_or_panic(x == -1);
......@@ -326,9 +361,11 @@ void c_i64(int64_t x) {
326361 assert_or_panic(x == -4);
327362}
328363
364#ifndef ZIG_NO_I128
329365void c_struct_i128(struct i128 x) {
330366 assert_or_panic(x.value == -6);
331367}
368#endif
332369
333370void c_f32(float x) {
334371 assert_or_panic(x == 12.34f);
......@@ -495,6 +532,7 @@ void c_small_packed_struct(uint8_t x) {
495532 assert_or_panic(((x >> 6) & 0x3) == 3);
496533}
497534
535#ifndef ZIG_NO_I128
498536__int128 c_ret_big_packed_struct() {
499537 __int128 s = 0;
500538 s |= 1 << 0;
......@@ -506,6 +544,7 @@ void c_big_packed_struct(__int128 x) {
506544 assert_or_panic(((x >> 0) & 0xFFFFFFFFFFFFFFFF) == 1);
507545 assert_or_panic(((x >> 64) & 0xFFFFFFFFFFFFFFFF) == 2);
508546}
547#endif
509548
510549struct SplitStructMixed c_ret_split_struct_mixed() {
511550 struct SplitStructMixed s = {
test/c_abi/main.zig+21-2
......@@ -2,6 +2,7 @@ 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;
56
67extern fn run_c_tests() void;
78
......@@ -40,13 +41,13 @@ test "C ABI integers" {
4041 c_u16(0xfffe);
4142 c_u32(0xfffffffd);
4243 c_u64(0xfffffffffffffffc);
43 c_struct_u128(.{ .value = 0xfffffffffffffffc });
44 if (has_i128) c_struct_u128(.{ .value = 0xfffffffffffffffc });
4445
4546 c_i8(-1);
4647 c_i16(-2);
4748 c_i32(-3);
4849 c_i64(-4);
49 c_struct_i128(.{ .value = -6 });
50 if (has_i128) c_struct_i128(.{ .value = -6 });
5051 c_five_integers(12, 34, 56, 78, 90);
5152}
5253
......@@ -178,6 +179,8 @@ test "C ABI complex float" {
178179}
179180
180181test "C ABI complex float by component" {
182 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
183
181184 const a = ComplexFloat{ .real = 1.25, .imag = 2.6 };
182185 const b = ComplexFloat{ .real = 11.3, .imag = -1.5 };
183186
......@@ -187,6 +190,8 @@ test "C ABI complex float by component" {
187190}
188191
189192test "C ABI complex double" {
193 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
194
190195 const a = ComplexDouble{ .real = 1.25, .imag = 2.6 };
191196 const b = ComplexDouble{ .real = 11.3, .imag = -1.5 };
192197
......@@ -196,6 +201,8 @@ test "C ABI complex double" {
196201}
197202
198203test "C ABI complex double by component" {
204 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
205
199206 const a = ComplexDouble{ .real = 1.25, .imag = 2.6 };
200207 const b = ComplexDouble{ .real = 11.3, .imag = -1.5 };
201208
......@@ -304,6 +311,8 @@ extern fn c_med_struct_mixed(MedStructMixed) void;
304311extern fn c_ret_med_struct_mixed() MedStructMixed;
305312
306313test "C ABI medium struct of ints and floats" {
314 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
315
307316 var s = MedStructMixed{
308317 .a = 1234,
309318 .b = 100.0,
......@@ -332,6 +341,8 @@ extern fn c_small_struct_ints(SmallStructInts) void;
332341extern fn c_ret_small_struct_ints() SmallStructInts;
333342
334343test "C ABI small struct of ints" {
344 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
345
335346 var s = SmallStructInts{
336347 .a = 1,
337348 .b = 2,
......@@ -392,6 +403,8 @@ export fn zig_big_packed_struct(x: BigPackedStruct) void {
392403}
393404
394405test "C ABI big packed struct" {
406 if (!has_i128) return error.SkipZigTest;
407
395408 var s = BigPackedStruct{ .a = 1, .b = 2 };
396409 c_big_packed_struct(s);
397410 var s2 = c_ret_big_packed_struct();
......@@ -407,6 +420,8 @@ const SplitStructInt = extern struct {
407420extern fn c_split_struct_ints(SplitStructInt) void;
408421
409422test "C ABI split struct of ints" {
423 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
424
410425 var s = SplitStructInt{
411426 .a = 1234,
412427 .b = 100,
......@@ -430,6 +445,8 @@ extern fn c_split_struct_mixed(SplitStructMixed) void;
430445extern fn c_ret_split_struct_mixed() SplitStructMixed;
431446
432447test "C ABI split struct of ints and floats" {
448 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
449
433450 var s = SplitStructMixed{
434451 .a = 1234,
435452 .b = 100,
......@@ -675,6 +692,8 @@ extern fn c_struct_with_array(StructWithArray) void;
675692extern fn c_ret_struct_with_array() StructWithArray;
676693
677694test "Struct with array as padding." {
695 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
696
678697 c_struct_with_array(.{ .a = 1, .padding = undefined, .b = 2 });
679698
680699 var x = c_ret_struct_with_array();