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) {...@@ -12,6 +12,15 @@ static void assert_or_panic(bool ok) {
12 }12 }
13}13}
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
15struct i128 {24struct i128 {
16 __int128 value;25 __int128 value;
17};26};
...@@ -19,17 +28,22 @@ struct i128 {...@@ -19,17 +28,22 @@ struct i128 {
19struct u128 {28struct u128 {
20 unsigned __int128 value;29 unsigned __int128 value;
21};30};
31#endif
2232
23void zig_u8(uint8_t);33void zig_u8(uint8_t);
24void zig_u16(uint16_t);34void zig_u16(uint16_t);
25void zig_u32(uint32_t);35void zig_u32(uint32_t);
26void zig_u64(uint64_t);36void zig_u64(uint64_t);
37#ifndef ZIG_NO_I128
27void zig_struct_u128(struct u128);38void zig_struct_u128(struct u128);
39#endif
28void zig_i8(int8_t);40void zig_i8(int8_t);
29void zig_i16(int16_t);41void zig_i16(int16_t);
30void zig_i32(int32_t);42void zig_i32(int32_t);
31void zig_i64(int64_t);43void zig_i64(int64_t);
44#ifndef ZIG_NO_I128
32void zig_struct_i128(struct i128);45void zig_struct_i128(struct i128);
46#endif
33void zig_five_integers(int32_t, int32_t, int32_t, int32_t, int32_t);47void zig_five_integers(int32_t, int32_t, int32_t, int32_t, int32_t);
3448
35void zig_f32(float);49void zig_f32(float);
...@@ -95,7 +109,9 @@ void zig_med_struct_mixed(struct MedStructMixed);...@@ -95,7 +109,9 @@ void zig_med_struct_mixed(struct MedStructMixed);
95struct MedStructMixed zig_ret_med_struct_mixed();109struct MedStructMixed zig_ret_med_struct_mixed();
96110
97void zig_small_packed_struct(uint8_t);111void zig_small_packed_struct(uint8_t);
112#ifndef ZIG_NO_I128
98void zig_big_packed_struct(__int128);113void zig_big_packed_struct(__int128);
114#endif
99115
100struct SplitStructInts {116struct SplitStructInts {
101 uint64_t a;117 uint64_t a;
...@@ -151,19 +167,26 @@ void run_c_tests(void) {...@@ -151,19 +167,26 @@ void run_c_tests(void) {
151 zig_u16(0xfffe);167 zig_u16(0xfffe);
152 zig_u32(0xfffffffd);168 zig_u32(0xfffffffd);
153 zig_u64(0xfffffffffffffffc);169 zig_u64(0xfffffffffffffffc);
170
171#ifndef ZIG_NO_I128
154 {172 {
155 struct u128 s = {0xfffffffffffffffc};173 struct u128 s = {0xfffffffffffffffc};
156 zig_struct_u128(s);174 zig_struct_u128(s);
157 }175 }
176#endif
158177
159 zig_i8(-1);178 zig_i8(-1);
160 zig_i16(-2);179 zig_i16(-2);
161 zig_i32(-3);180 zig_i32(-3);
162 zig_i64(-4);181 zig_i64(-4);
182
183#ifndef ZIG_NO_I128
163 {184 {
164 struct i128 s = {-6};185 struct i128 s = {-6};
165 zig_struct_i128(s);186 zig_struct_i128(s);
166 }187 }
188#endif
189
167 zig_five_integers(12, 34, 56, 78, 90);190 zig_five_integers(12, 34, 56, 78, 90);
168191
169 zig_f32(12.34f);192 zig_f32(12.34f);
...@@ -175,6 +198,7 @@ void run_c_tests(void) {...@@ -175,6 +198,7 @@ void run_c_tests(void) {
175198
176 zig_bool(true);199 zig_bool(true);
177200
201#ifndef ZIG_NO_COMPLEX
178 // TODO: Resolve https://github.com/ziglang/zig/issues/8465202 // TODO: Resolve https://github.com/ziglang/zig/issues/8465
179 //{203 //{
180 // float complex a = 1.25f + I * 2.6f;204 // float complex a = 1.25f + I * 2.6f;
...@@ -211,23 +235,28 @@ void run_c_tests(void) {...@@ -211,23 +235,28 @@ void run_c_tests(void) {
211 assert_or_panic(creal(z) == 1.5);235 assert_or_panic(creal(z) == 1.5);
212 assert_or_panic(cimag(z) == 13.5);236 assert_or_panic(cimag(z) == 13.5);
213 }237 }
238#endif
214239
215 {240 {
216 struct BigStruct s = {1, 2, 3, 4, 5};241 struct BigStruct s = {1, 2, 3, 4, 5};
217 zig_big_struct(s);242 zig_big_struct(s);
218 }243 }
219244
245#ifndef __i386__
220 {246 {
221 struct SmallStructInts s = {1, 2, 3, 4};247 struct SmallStructInts s = {1, 2, 3, 4};
222 zig_small_struct_ints(s);248 zig_small_struct_ints(s);
223 }249 }
250#endif
224251
252#ifndef ZIG_NO_I128
225 {253 {
226 __int128 s = 0;254 __int128 s = 0;
227 s |= 1 << 0;255 s |= 1 << 0;
228 s |= (__int128)2 << 64;256 s |= (__int128)2 << 64;
229 zig_big_packed_struct(s);257 zig_big_packed_struct(s);
230 }258 }
259#endif
231260
232 {261 {
233 uint8_t s = 0;262 uint8_t s = 0;
...@@ -238,20 +267,24 @@ void run_c_tests(void) {...@@ -238,20 +267,24 @@ void run_c_tests(void) {
238 zig_small_packed_struct(s);267 zig_small_packed_struct(s);
239 }268 }
240269
270#ifndef __i386__
241 {271 {
242 struct SplitStructInts s = {1234, 100, 1337};272 struct SplitStructInts s = {1234, 100, 1337};
243 zig_split_struct_ints(s);273 zig_split_struct_ints(s);
244 }274 }
275#endif
245276
246 {277 {
247 struct MedStructMixed s = {1234, 100.0f, 1337.0f};278 struct MedStructMixed s = {1234, 100.0f, 1337.0f};
248 zig_med_struct_mixed(s);279 zig_med_struct_mixed(s);
249 }280 }
250281
282#ifndef __i386__
251 {283 {
252 struct SplitStructMixed s = {1234, 100, 1337.0f};284 struct SplitStructMixed s = {1234, 100, 1337.0f};
253 zig_split_struct_mixed(s);285 zig_split_struct_mixed(s);
254 }286 }
287#endif
255288
256 {289 {
257 struct BigStruct s = {30, 31, 32, 33, 34};290 struct BigStruct s = {30, 31, 32, 33, 34};
...@@ -306,9 +339,11 @@ void c_u64(uint64_t x) {...@@ -306,9 +339,11 @@ void c_u64(uint64_t x) {
306 assert_or_panic(x == 0xfffffffffffffffcULL);339 assert_or_panic(x == 0xfffffffffffffffcULL);
307}340}
308341
342#ifndef ZIG_NO_I128
309void c_struct_u128(struct u128 x) {343void c_struct_u128(struct u128 x) {
310 assert_or_panic(x.value == 0xfffffffffffffffcULL);344 assert_or_panic(x.value == 0xfffffffffffffffcULL);
311}345}
346#endif
312347
313void c_i8(int8_t x) {348void c_i8(int8_t x) {
314 assert_or_panic(x == -1);349 assert_or_panic(x == -1);
...@@ -326,9 +361,11 @@ void c_i64(int64_t x) {...@@ -326,9 +361,11 @@ void c_i64(int64_t x) {
326 assert_or_panic(x == -4);361 assert_or_panic(x == -4);
327}362}
328363
364#ifndef ZIG_NO_I128
329void c_struct_i128(struct i128 x) {365void c_struct_i128(struct i128 x) {
330 assert_or_panic(x.value == -6);366 assert_or_panic(x.value == -6);
331}367}
368#endif
332369
333void c_f32(float x) {370void c_f32(float x) {
334 assert_or_panic(x == 12.34f);371 assert_or_panic(x == 12.34f);
...@@ -495,6 +532,7 @@ void c_small_packed_struct(uint8_t x) {...@@ -495,6 +532,7 @@ void c_small_packed_struct(uint8_t x) {
495 assert_or_panic(((x >> 6) & 0x3) == 3);532 assert_or_panic(((x >> 6) & 0x3) == 3);
496}533}
497534
535#ifndef ZIG_NO_I128
498__int128 c_ret_big_packed_struct() {536__int128 c_ret_big_packed_struct() {
499 __int128 s = 0;537 __int128 s = 0;
500 s |= 1 << 0;538 s |= 1 << 0;
...@@ -506,6 +544,7 @@ void c_big_packed_struct(__int128 x) {...@@ -506,6 +544,7 @@ void c_big_packed_struct(__int128 x) {
506 assert_or_panic(((x >> 0) & 0xFFFFFFFFFFFFFFFF) == 1);544 assert_or_panic(((x >> 0) & 0xFFFFFFFFFFFFFFFF) == 1);
507 assert_or_panic(((x >> 64) & 0xFFFFFFFFFFFFFFFF) == 2);545 assert_or_panic(((x >> 64) & 0xFFFFFFFFFFFFFFFF) == 2);
508}546}
547#endif
509548
510struct SplitStructMixed c_ret_split_struct_mixed() {549struct SplitStructMixed c_ret_split_struct_mixed() {
511 struct SplitStructMixed s = {550 struct SplitStructMixed s = {
test/c_abi/main.zig+21-2
...@@ -2,6 +2,7 @@ const std = @import("std");...@@ -2,6 +2,7 @@ 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;
56
6extern fn run_c_tests() void;7extern fn run_c_tests() void;
78
...@@ -40,13 +41,13 @@ test "C ABI integers" {...@@ -40,13 +41,13 @@ test "C ABI integers" {
40 c_u16(0xfffe);41 c_u16(0xfffe);
41 c_u32(0xfffffffd);42 c_u32(0xfffffffd);
42 c_u64(0xfffffffffffffffc);43 c_u64(0xfffffffffffffffc);
43 c_struct_u128(.{ .value = 0xfffffffffffffffc });44 if (has_i128) c_struct_u128(.{ .value = 0xfffffffffffffffc });
4445
45 c_i8(-1);46 c_i8(-1);
46 c_i16(-2);47 c_i16(-2);
47 c_i32(-3);48 c_i32(-3);
48 c_i64(-4);49 c_i64(-4);
49 c_struct_i128(.{ .value = -6 });50 if (has_i128) c_struct_i128(.{ .value = -6 });
50 c_five_integers(12, 34, 56, 78, 90);51 c_five_integers(12, 34, 56, 78, 90);
51}52}
5253
...@@ -178,6 +179,8 @@ test "C ABI complex float" {...@@ -178,6 +179,8 @@ test "C ABI complex float" {
178}179}
179180
180test "C ABI complex float by component" {181test "C ABI complex float by component" {
182 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
183
181 const a = ComplexFloat{ .real = 1.25, .imag = 2.6 };184 const a = ComplexFloat{ .real = 1.25, .imag = 2.6 };
182 const b = ComplexFloat{ .real = 11.3, .imag = -1.5 };185 const b = ComplexFloat{ .real = 11.3, .imag = -1.5 };
183186
...@@ -187,6 +190,8 @@ test "C ABI complex float by component" {...@@ -187,6 +190,8 @@ test "C ABI complex float by component" {
187}190}
188191
189test "C ABI complex double" {192test "C ABI complex double" {
193 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
194
190 const a = ComplexDouble{ .real = 1.25, .imag = 2.6 };195 const a = ComplexDouble{ .real = 1.25, .imag = 2.6 };
191 const b = ComplexDouble{ .real = 11.3, .imag = -1.5 };196 const b = ComplexDouble{ .real = 11.3, .imag = -1.5 };
192197
...@@ -196,6 +201,8 @@ test "C ABI complex double" {...@@ -196,6 +201,8 @@ test "C ABI complex double" {
196}201}
197202
198test "C ABI complex double by component" {203test "C ABI complex double by component" {
204 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
205
199 const a = ComplexDouble{ .real = 1.25, .imag = 2.6 };206 const a = ComplexDouble{ .real = 1.25, .imag = 2.6 };
200 const b = ComplexDouble{ .real = 11.3, .imag = -1.5 };207 const b = ComplexDouble{ .real = 11.3, .imag = -1.5 };
201208
...@@ -304,6 +311,8 @@ extern fn c_med_struct_mixed(MedStructMixed) void;...@@ -304,6 +311,8 @@ extern fn c_med_struct_mixed(MedStructMixed) void;
304extern fn c_ret_med_struct_mixed() MedStructMixed;311extern fn c_ret_med_struct_mixed() MedStructMixed;
305312
306test "C ABI medium struct of ints and floats" {313test "C ABI medium struct of ints and floats" {
314 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
315
307 var s = MedStructMixed{316 var s = MedStructMixed{
308 .a = 1234,317 .a = 1234,
309 .b = 100.0,318 .b = 100.0,
...@@ -332,6 +341,8 @@ extern fn c_small_struct_ints(SmallStructInts) void;...@@ -332,6 +341,8 @@ extern fn c_small_struct_ints(SmallStructInts) void;
332extern fn c_ret_small_struct_ints() SmallStructInts;341extern fn c_ret_small_struct_ints() SmallStructInts;
333342
334test "C ABI small struct of ints" {343test "C ABI small struct of ints" {
344 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
345
335 var s = SmallStructInts{346 var s = SmallStructInts{
336 .a = 1,347 .a = 1,
337 .b = 2,348 .b = 2,
...@@ -392,6 +403,8 @@ export fn zig_big_packed_struct(x: BigPackedStruct) void {...@@ -392,6 +403,8 @@ export fn zig_big_packed_struct(x: BigPackedStruct) void {
392}403}
393404
394test "C ABI big packed struct" {405test "C ABI big packed struct" {
406 if (!has_i128) return error.SkipZigTest;
407
395 var s = BigPackedStruct{ .a = 1, .b = 2 };408 var s = BigPackedStruct{ .a = 1, .b = 2 };
396 c_big_packed_struct(s);409 c_big_packed_struct(s);
397 var s2 = c_ret_big_packed_struct();410 var s2 = c_ret_big_packed_struct();
...@@ -407,6 +420,8 @@ const SplitStructInt = extern struct {...@@ -407,6 +420,8 @@ const SplitStructInt = extern struct {
407extern fn c_split_struct_ints(SplitStructInt) void;420extern fn c_split_struct_ints(SplitStructInt) void;
408421
409test "C ABI split struct of ints" {422test "C ABI split struct of ints" {
423 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
424
410 var s = SplitStructInt{425 var s = SplitStructInt{
411 .a = 1234,426 .a = 1234,
412 .b = 100,427 .b = 100,
...@@ -430,6 +445,8 @@ extern fn c_split_struct_mixed(SplitStructMixed) void;...@@ -430,6 +445,8 @@ extern fn c_split_struct_mixed(SplitStructMixed) void;
430extern fn c_ret_split_struct_mixed() SplitStructMixed;445extern fn c_ret_split_struct_mixed() SplitStructMixed;
431446
432test "C ABI split struct of ints and floats" {447test "C ABI split struct of ints and floats" {
448 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
449
433 var s = SplitStructMixed{450 var s = SplitStructMixed{
434 .a = 1234,451 .a = 1234,
435 .b = 100,452 .b = 100,
...@@ -675,6 +692,8 @@ extern fn c_struct_with_array(StructWithArray) void;...@@ -675,6 +692,8 @@ extern fn c_struct_with_array(StructWithArray) void;
675extern fn c_ret_struct_with_array() StructWithArray;692extern fn c_ret_struct_with_array() StructWithArray;
676693
677test "Struct with array as padding." {694test "Struct with array as padding." {
695 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
696
678 c_struct_with_array(.{ .a = 1, .padding = undefined, .b = 2 });697 c_struct_with_array(.{ .a = 1, .padding = undefined, .b = 2 });
679698
680 var x = c_ret_struct_with_array();699 var x = c_ret_struct_with_array();