forked from ziglang/zig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.zig
5790 lines (5529 loc) · 204 KB
/
main.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Tests for the C ABI.
//! Those tests are passing back and forth struct and values across C ABI
//! by combining Zig code here and its mirror in cfunc.c
//! To run all the tests on the tier 1 architecture you can use:
//! zig build test-c-abi -fqemu
//! To run the tests on a specific architecture:
//! zig test -lc main.zig cfuncs.c -target mips-linux --test-cmd qemu-mips --test-cmd-bin
const std = @import("std");
const builtin = @import("builtin");
const print = std.debug.print;
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const have_i128 = builtin.cpu.arch != .x86 and !builtin.cpu.arch.isARM() and
!builtin.cpu.arch.isMIPS() and !builtin.cpu.arch.isPPC();
const have_f128 = builtin.cpu.arch.isX86() and !builtin.os.tag.isDarwin();
const have_f80 = builtin.cpu.arch.isX86();
extern fn run_c_tests() void;
export fn zig_panic() noreturn {
@panic("zig_panic called from C");
}
test "C importing Zig ABI Tests" {
run_c_tests();
}
extern fn c_u8(u8) void;
extern fn c_u16(u16) void;
extern fn c_u32(u32) void;
extern fn c_u64(u64) void;
extern fn c_struct_u128(U128) void;
extern fn c_i8(i8) void;
extern fn c_i16(i16) void;
extern fn c_i32(i32) void;
extern fn c_i64(i64) void;
extern fn c_struct_i128(I128) void;
// On windows x64, the first 4 are passed via registers, others on the stack.
extern fn c_five_integers(i32, i32, i32, i32, i32) void;
export fn zig_five_integers(a: i32, b: i32, c: i32, d: i32, e: i32) void {
expect(a == 12) catch @panic("test failure: zig_five_integers 12");
expect(b == 34) catch @panic("test failure: zig_five_integers 34");
expect(c == 56) catch @panic("test failure: zig_five_integers 56");
expect(d == 78) catch @panic("test failure: zig_five_integers 78");
expect(e == 90) catch @panic("test failure: zig_five_integers 90");
}
test "C ABI integers" {
c_u8(0xff);
c_u16(0xfffe);
c_u32(0xfffffffd);
c_u64(0xfffffffffffffffc);
if (have_i128) c_struct_u128(.{ .value = 0xfffffffffffffffc });
c_i8(-1);
c_i16(-2);
c_i32(-3);
c_i64(-4);
if (have_i128) c_struct_i128(.{ .value = -6 });
c_five_integers(12, 34, 56, 78, 90);
}
export fn zig_u8(x: u8) void {
expect(x == 0xff) catch @panic("test failure: zig_u8");
}
export fn zig_u16(x: u16) void {
expect(x == 0xfffe) catch @panic("test failure: zig_u16");
}
export fn zig_u32(x: u32) void {
expect(x == 0xfffffffd) catch @panic("test failure: zig_u32");
}
export fn zig_u64(x: u64) void {
expect(x == 0xfffffffffffffffc) catch @panic("test failure: zig_u64");
}
export fn zig_i8(x: i8) void {
expect(x == -1) catch @panic("test failure: zig_i8");
}
export fn zig_i16(x: i16) void {
expect(x == -2) catch @panic("test failure: zig_i16");
}
export fn zig_i32(x: i32) void {
expect(x == -3) catch @panic("test failure: zig_i32");
}
export fn zig_i64(x: i64) void {
expect(x == -4) catch @panic("test failure: zig_i64");
}
const I128 = extern struct {
value: i128,
};
const U128 = extern struct {
value: u128,
};
export fn zig_struct_i128(a: I128) void {
expect(a.value == -6) catch @panic("test failure: zig_struct_i128");
}
export fn zig_struct_u128(a: U128) void {
expect(a.value == 0xfffffffffffffffc) catch @panic("test failure: zig_struct_u128");
}
extern fn c_f32(f32) void;
extern fn c_f64(f64) void;
extern fn c_long_double(c_longdouble) void;
// On windows x64, the first 4 are passed via registers, others on the stack.
extern fn c_five_floats(f32, f32, f32, f32, f32) void;
export fn zig_five_floats(a: f32, b: f32, c: f32, d: f32, e: f32) void {
expect(a == 1.0) catch @panic("test failure: zig_five_floats 1.0");
expect(b == 2.0) catch @panic("test failure: zig_five_floats 2.0");
expect(c == 3.0) catch @panic("test failure: zig_five_floats 3.0");
expect(d == 4.0) catch @panic("test failure: zig_five_floats 4.0");
expect(e == 5.0) catch @panic("test failure: zig_five_floats 5.0");
}
test "C ABI floats" {
c_f32(12.34);
c_f64(56.78);
c_five_floats(1.0, 2.0, 3.0, 4.0, 5.0);
}
test "C ABI long double" {
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC64()) return error.SkipZigTest;
c_long_double(12.34);
}
export fn zig_f32(x: f32) void {
expect(x == 12.34) catch @panic("test failure: zig_f32");
}
export fn zig_f64(x: f64) void {
expect(x == 56.78) catch @panic("test failure: zig_f64");
}
export fn zig_longdouble(x: c_longdouble) void {
if (!builtin.target.isWasm()) return; // waiting for #1481
expect(x == 12.34) catch @panic("test failure: zig_longdouble");
}
extern fn c_ptr(*anyopaque) void;
test "C ABI pointer" {
c_ptr(@as(*anyopaque, @ptrFromInt(0xdeadbeef)));
}
export fn zig_ptr(x: *anyopaque) void {
expect(@intFromPtr(x) == 0xdeadbeef) catch @panic("test failure: zig_ptr");
}
extern fn c_bool(bool) void;
test "C ABI bool" {
c_bool(true);
}
export fn zig_bool(x: bool) void {
expect(x) catch @panic("test failure: zig_bool");
}
// TODO: Replace these with the correct types once we resolve
// https://github.com/ziglang/zig/issues/8465
//
// For now, we have no way of referring to the _Complex C types from Zig,
// so our ABI is unavoidably broken on some platforms (such as x86)
const ComplexFloat = extern struct {
real: f32,
imag: f32,
};
const ComplexDouble = extern struct {
real: f64,
imag: f64,
};
// Note: These two functions match the signature of __mulsc3 and __muldc3 in compiler-rt (and libgcc)
extern fn c_cmultf_comp(a_r: f32, a_i: f32, b_r: f32, b_i: f32) ComplexFloat;
extern fn c_cmultd_comp(a_r: f64, a_i: f64, b_r: f64, b_i: f64) ComplexDouble;
extern fn c_cmultf(a: ComplexFloat, b: ComplexFloat) ComplexFloat;
extern fn c_cmultd(a: ComplexDouble, b: ComplexDouble) ComplexDouble;
const complex_abi_compatible = builtin.cpu.arch != .x86 and !builtin.cpu.arch.isMIPS() and
!builtin.cpu.arch.isARM() and !builtin.cpu.arch.isPPC() and !builtin.cpu.arch.isRISCV();
test "C ABI complex float" {
if (!complex_abi_compatible) return error.SkipZigTest;
const a = ComplexFloat{ .real = 1.25, .imag = 2.6 };
const b = ComplexFloat{ .real = 11.3, .imag = -1.5 };
const z = c_cmultf(a, b);
try expect(z.real == 1.5);
try expect(z.imag == 13.5);
}
test "C ABI complex float by component" {
if (!complex_abi_compatible) return error.SkipZigTest;
const a = ComplexFloat{ .real = 1.25, .imag = 2.6 };
const b = ComplexFloat{ .real = 11.3, .imag = -1.5 };
const z2 = c_cmultf_comp(a.real, a.imag, b.real, b.imag);
try expect(z2.real == 1.5);
try expect(z2.imag == 13.5);
}
test "C ABI complex double" {
if (!complex_abi_compatible) return error.SkipZigTest;
const a = ComplexDouble{ .real = 1.25, .imag = 2.6 };
const b = ComplexDouble{ .real = 11.3, .imag = -1.5 };
const z = c_cmultd(a, b);
try expect(z.real == 1.5);
try expect(z.imag == 13.5);
}
test "C ABI complex double by component" {
if (!complex_abi_compatible) return error.SkipZigTest;
const a = ComplexDouble{ .real = 1.25, .imag = 2.6 };
const b = ComplexDouble{ .real = 11.3, .imag = -1.5 };
const z = c_cmultd_comp(a.real, a.imag, b.real, b.imag);
try expect(z.real == 1.5);
try expect(z.imag == 13.5);
}
export fn zig_cmultf(a: ComplexFloat, b: ComplexFloat) ComplexFloat {
expect(a.real == 1.25) catch @panic("test failure: zig_cmultf 1");
expect(a.imag == 2.6) catch @panic("test failure: zig_cmultf 2");
expect(b.real == 11.3) catch @panic("test failure: zig_cmultf 3");
expect(b.imag == -1.5) catch @panic("test failure: zig_cmultf 4");
return .{ .real = 1.5, .imag = 13.5 };
}
export fn zig_cmultd(a: ComplexDouble, b: ComplexDouble) ComplexDouble {
expect(a.real == 1.25) catch @panic("test failure: zig_cmultd 1");
expect(a.imag == 2.6) catch @panic("test failure: zig_cmultd 2");
expect(b.real == 11.3) catch @panic("test failure: zig_cmultd 3");
expect(b.imag == -1.5) catch @panic("test failure: zig_cmultd 4");
return .{ .real = 1.5, .imag = 13.5 };
}
export fn zig_cmultf_comp(a_r: f32, a_i: f32, b_r: f32, b_i: f32) ComplexFloat {
expect(a_r == 1.25) catch @panic("test failure: zig_cmultf_comp 1");
expect(a_i == 2.6) catch @panic("test failure: zig_cmultf_comp 2");
expect(b_r == 11.3) catch @panic("test failure: zig_cmultf_comp 3");
expect(b_i == -1.5) catch @panic("test failure: zig_cmultf_comp 4");
return .{ .real = 1.5, .imag = 13.5 };
}
export fn zig_cmultd_comp(a_r: f64, a_i: f64, b_r: f64, b_i: f64) ComplexDouble {
expect(a_r == 1.25) catch @panic("test failure: zig_cmultd_comp 1");
expect(a_i == 2.6) catch @panic("test failure: zig_cmultd_comp 2");
expect(b_r == 11.3) catch @panic("test failure: zig_cmultd_comp 3");
expect(b_i == -1.5) catch @panic("test failure: zig_cmultd_comp 4");
return .{ .real = 1.5, .imag = 13.5 };
}
const Struct_u64_u64 = extern struct {
a: u64,
b: u64,
};
export fn zig_ret_struct_u64_u64() Struct_u64_u64 {
return .{ .a = 1, .b = 2 };
}
export fn zig_struct_u64_u64_0(s: Struct_u64_u64) void {
expect(s.a == 3) catch @panic("test failure");
expect(s.b == 4) catch @panic("test failure");
}
export fn zig_struct_u64_u64_1(_: usize, s: Struct_u64_u64) void {
expect(s.a == 5) catch @panic("test failure");
expect(s.b == 6) catch @panic("test failure");
}
export fn zig_struct_u64_u64_2(_: usize, _: usize, s: Struct_u64_u64) void {
expect(s.a == 7) catch @panic("test failure");
expect(s.b == 8) catch @panic("test failure");
}
export fn zig_struct_u64_u64_3(_: usize, _: usize, _: usize, s: Struct_u64_u64) void {
expect(s.a == 9) catch @panic("test failure");
expect(s.b == 10) catch @panic("test failure");
}
export fn zig_struct_u64_u64_4(_: usize, _: usize, _: usize, _: usize, s: Struct_u64_u64) void {
expect(s.a == 11) catch @panic("test failure");
expect(s.b == 12) catch @panic("test failure");
}
export fn zig_struct_u64_u64_5(_: usize, _: usize, _: usize, _: usize, _: usize, s: Struct_u64_u64) void {
expect(s.a == 13) catch @panic("test failure");
expect(s.b == 14) catch @panic("test failure");
}
export fn zig_struct_u64_u64_6(_: usize, _: usize, _: usize, _: usize, _: usize, _: usize, s: Struct_u64_u64) void {
expect(s.a == 15) catch @panic("test failure");
expect(s.b == 16) catch @panic("test failure");
}
export fn zig_struct_u64_u64_7(_: usize, _: usize, _: usize, _: usize, _: usize, _: usize, _: usize, s: Struct_u64_u64) void {
expect(s.a == 17) catch @panic("test failure");
expect(s.b == 18) catch @panic("test failure");
}
export fn zig_struct_u64_u64_8(_: usize, _: usize, _: usize, _: usize, _: usize, _: usize, _: usize, _: usize, s: Struct_u64_u64) void {
expect(s.a == 19) catch @panic("test failure");
expect(s.b == 20) catch @panic("test failure");
}
extern fn c_ret_struct_u64_u64() Struct_u64_u64;
extern fn c_struct_u64_u64_0(Struct_u64_u64) void;
extern fn c_struct_u64_u64_1(usize, Struct_u64_u64) void;
extern fn c_struct_u64_u64_2(usize, usize, Struct_u64_u64) void;
extern fn c_struct_u64_u64_3(usize, usize, usize, Struct_u64_u64) void;
extern fn c_struct_u64_u64_4(usize, usize, usize, usize, Struct_u64_u64) void;
extern fn c_struct_u64_u64_5(usize, usize, usize, usize, usize, Struct_u64_u64) void;
extern fn c_struct_u64_u64_6(usize, usize, usize, usize, usize, usize, Struct_u64_u64) void;
extern fn c_struct_u64_u64_7(usize, usize, usize, usize, usize, usize, usize, Struct_u64_u64) void;
extern fn c_struct_u64_u64_8(usize, usize, usize, usize, usize, usize, usize, usize, Struct_u64_u64) void;
test "C ABI struct u64 u64" {
if (builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
const s = c_ret_struct_u64_u64();
try expect(s.a == 21);
try expect(s.b == 22);
c_struct_u64_u64_0(.{ .a = 23, .b = 24 });
c_struct_u64_u64_1(0, .{ .a = 25, .b = 26 });
c_struct_u64_u64_2(0, 1, .{ .a = 27, .b = 28 });
c_struct_u64_u64_3(0, 1, 2, .{ .a = 29, .b = 30 });
c_struct_u64_u64_4(0, 1, 2, 3, .{ .a = 31, .b = 32 });
c_struct_u64_u64_5(0, 1, 2, 3, 4, .{ .a = 33, .b = 34 });
c_struct_u64_u64_6(0, 1, 2, 3, 4, 5, .{ .a = 35, .b = 36 });
c_struct_u64_u64_7(0, 1, 2, 3, 4, 5, 6, .{ .a = 37, .b = 38 });
c_struct_u64_u64_8(0, 1, 2, 3, 4, 5, 6, 7, .{ .a = 39, .b = 40 });
}
const Struct_f32f32_f32 = extern struct {
a: extern struct { b: f32, c: f32 },
d: f32,
};
export fn zig_ret_struct_f32f32_f32() Struct_f32f32_f32 {
return .{ .a = .{ .b = 1.0, .c = 2.0 }, .d = 3.0 };
}
export fn zig_struct_f32f32_f32(s: Struct_f32f32_f32) void {
expect(s.a.b == 1.0) catch @panic("test failure");
expect(s.a.c == 2.0) catch @panic("test failure");
expect(s.d == 3.0) catch @panic("test failure");
}
extern fn c_ret_struct_f32f32_f32() Struct_f32f32_f32;
extern fn c_struct_f32f32_f32(Struct_f32f32_f32) void;
test "C ABI struct {f32,f32} f32" {
if (builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
const s = c_ret_struct_f32f32_f32();
try expect(s.a.b == 1.0);
try expect(s.a.c == 2.0);
try expect(s.d == 3.0);
c_struct_f32f32_f32(.{ .a = .{ .b = 1.0, .c = 2.0 }, .d = 3.0 });
}
const Struct_f32_f32f32 = extern struct {
a: f32,
b: extern struct { c: f32, d: f32 },
};
export fn zig_ret_struct_f32_f32f32() Struct_f32_f32f32 {
return .{ .a = 1.0, .b = .{ .c = 2.0, .d = 3.0 } };
}
export fn zig_struct_f32_f32f32(s: Struct_f32_f32f32) void {
expect(s.a == 1.0) catch @panic("test failure");
expect(s.b.c == 2.0) catch @panic("test failure");
expect(s.b.d == 3.0) catch @panic("test failure");
}
extern fn c_ret_struct_f32_f32f32() Struct_f32_f32f32;
extern fn c_struct_f32_f32f32(Struct_f32_f32f32) void;
test "C ABI struct f32 {f32,f32}" {
if (builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
const s = c_ret_struct_f32_f32f32();
try expect(s.a == 1.0);
try expect(s.b.c == 2.0);
try expect(s.b.d == 3.0);
c_struct_f32_f32f32(.{ .a = 1.0, .b = .{ .c = 2.0, .d = 3.0 } });
}
const Struct_u32_Union_u32_u32u32 = extern struct {
a: u32,
b: extern union {
c: extern struct {
d: u32,
e: u32,
},
},
};
export fn zig_ret_struct_u32_union_u32_u32u32() Struct_u32_Union_u32_u32u32 {
return .{ .a = 1, .b = .{ .c = .{ .d = 2, .e = 3 } } };
}
export fn zig_struct_u32_union_u32_u32u32(s: Struct_u32_Union_u32_u32u32) void {
expect(s.a == 1) catch @panic("test failure");
expect(s.b.c.d == 2) catch @panic("test failure");
expect(s.b.c.e == 3) catch @panic("test failure");
}
extern fn c_ret_struct_u32_union_u32_u32u32() Struct_u32_Union_u32_u32u32;
extern fn c_struct_u32_union_u32_u32u32(Struct_u32_Union_u32_u32u32) void;
test "C ABI struct{u32,union{u32,struct{u32,u32}}}" {
if (builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC64()) return error.SkipZigTest;
const s = c_ret_struct_u32_union_u32_u32u32();
try expect(s.a == 1);
try expect(s.b.c.d == 2);
try expect(s.b.c.e == 3);
c_struct_u32_union_u32_u32u32(.{ .a = 1, .b = .{ .c = .{ .d = 2, .e = 3 } } });
}
const BigStruct = extern struct {
a: u64,
b: u64,
c: u64,
d: u64,
e: u8,
};
extern fn c_big_struct(BigStruct) void;
test "C ABI big struct" {
if (builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
const s = BigStruct{
.a = 1,
.b = 2,
.c = 3,
.d = 4,
.e = 5,
};
c_big_struct(s);
}
export fn zig_big_struct(x: BigStruct) void {
expect(x.a == 1) catch @panic("test failure: zig_big_struct 1");
expect(x.b == 2) catch @panic("test failure: zig_big_struct 2");
expect(x.c == 3) catch @panic("test failure: zig_big_struct 3");
expect(x.d == 4) catch @panic("test failure: zig_big_struct 4");
expect(x.e == 5) catch @panic("test failure: zig_big_struct 5");
}
const BigUnion = extern union {
a: BigStruct,
};
extern fn c_big_union(BigUnion) void;
test "C ABI big union" {
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
const x = BigUnion{
.a = BigStruct{
.a = 1,
.b = 2,
.c = 3,
.d = 4,
.e = 5,
},
};
c_big_union(x);
}
export fn zig_big_union(x: BigUnion) void {
expect(x.a.a == 1) catch @panic("test failure: zig_big_union a");
expect(x.a.b == 2) catch @panic("test failure: zig_big_union b");
expect(x.a.c == 3) catch @panic("test failure: zig_big_union c");
expect(x.a.d == 4) catch @panic("test failure: zig_big_union d");
expect(x.a.e == 5) catch @panic("test failure: zig_big_union e");
}
const MedStructMixed = extern struct {
a: u32,
b: f32,
c: f32,
d: u32 = 0,
};
extern fn c_med_struct_mixed(MedStructMixed) void;
extern fn c_ret_med_struct_mixed() MedStructMixed;
test "C ABI medium struct of ints and floats" {
if (builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC64()) return error.SkipZigTest;
const s = MedStructMixed{
.a = 1234,
.b = 100.0,
.c = 1337.0,
};
c_med_struct_mixed(s);
const s2 = c_ret_med_struct_mixed();
try expect(s2.a == 1234);
try expect(s2.b == 100.0);
try expect(s2.c == 1337.0);
}
export fn zig_med_struct_mixed(x: MedStructMixed) void {
expect(x.a == 1234) catch @panic("test failure");
expect(x.b == 100.0) catch @panic("test failure");
expect(x.c == 1337.0) catch @panic("test failure");
}
const SmallStructInts = extern struct {
a: u8,
b: u8,
c: u8,
d: u8,
};
extern fn c_small_struct_ints(SmallStructInts) void;
extern fn c_ret_small_struct_ints() SmallStructInts;
test "C ABI small struct of ints" {
if (builtin.cpu.arch == .x86) return error.SkipZigTest;
if (builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC64()) return error.SkipZigTest;
const s = SmallStructInts{
.a = 1,
.b = 2,
.c = 3,
.d = 4,
};
c_small_struct_ints(s);
const s2 = c_ret_small_struct_ints();
try expect(s2.a == 1);
try expect(s2.b == 2);
try expect(s2.c == 3);
try expect(s2.d == 4);
}
export fn zig_small_struct_ints(x: SmallStructInts) void {
expect(x.a == 1) catch @panic("test failure");
expect(x.b == 2) catch @panic("test failure");
expect(x.c == 3) catch @panic("test failure");
expect(x.d == 4) catch @panic("test failure");
}
const MedStructInts = extern struct {
x: i32,
y: i32,
z: i32,
};
extern fn c_med_struct_ints(MedStructInts) void;
extern fn c_ret_med_struct_ints() MedStructInts;
test "C ABI medium struct of ints" {
if (builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC64()) return error.SkipZigTest;
const s = MedStructInts{
.x = 1,
.y = 2,
.z = 3,
};
c_med_struct_ints(s);
const s2 = c_ret_med_struct_ints();
try expect(s2.x == 1);
try expect(s2.y == 2);
try expect(s2.z == 3);
}
export fn zig_med_struct_ints(s: MedStructInts) void {
expect(s.x == 1) catch @panic("test failure");
expect(s.y == 2) catch @panic("test failure");
expect(s.z == 3) catch @panic("test failure");
}
const SmallPackedStruct = packed struct {
a: u2,
b: u2,
c: u2,
d: u2,
};
extern fn c_small_packed_struct(SmallPackedStruct) void;
extern fn c_ret_small_packed_struct() SmallPackedStruct;
export fn zig_small_packed_struct(x: SmallPackedStruct) void {
expect(x.a == 0) catch @panic("test failure");
expect(x.b == 1) catch @panic("test failure");
expect(x.c == 2) catch @panic("test failure");
expect(x.d == 3) catch @panic("test failure");
}
test "C ABI small packed struct" {
const s = SmallPackedStruct{ .a = 0, .b = 1, .c = 2, .d = 3 };
c_small_packed_struct(s);
const s2 = c_ret_small_packed_struct();
try expect(s2.a == 0);
try expect(s2.b == 1);
try expect(s2.c == 2);
try expect(s2.d == 3);
}
const BigPackedStruct = packed struct {
a: u64,
b: u64,
};
extern fn c_big_packed_struct(BigPackedStruct) void;
extern fn c_ret_big_packed_struct() BigPackedStruct;
export fn zig_big_packed_struct(x: BigPackedStruct) void {
expect(x.a == 1) catch @panic("test failure");
expect(x.b == 2) catch @panic("test failure");
}
test "C ABI big packed struct" {
if (!have_i128) return error.SkipZigTest;
const s = BigPackedStruct{ .a = 1, .b = 2 };
c_big_packed_struct(s);
const s2 = c_ret_big_packed_struct();
try expect(s2.a == 1);
try expect(s2.b == 2);
}
const SplitStructInt = extern struct {
a: u64,
b: u8,
c: u32,
};
extern fn c_split_struct_ints(SplitStructInt) void;
test "C ABI split struct of ints" {
if (builtin.cpu.arch == .x86) return error.SkipZigTest;
if (builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC64()) return error.SkipZigTest;
const s = SplitStructInt{
.a = 1234,
.b = 100,
.c = 1337,
};
c_split_struct_ints(s);
}
export fn zig_split_struct_ints(x: SplitStructInt) void {
expect(x.a == 1234) catch @panic("test failure");
expect(x.b == 100) catch @panic("test failure");
expect(x.c == 1337) catch @panic("test failure");
}
const SplitStructMixed = extern struct {
a: u64,
b: u8,
c: f32,
};
extern fn c_split_struct_mixed(SplitStructMixed) void;
extern fn c_ret_split_struct_mixed() SplitStructMixed;
test "C ABI split struct of ints and floats" {
if (builtin.cpu.arch == .x86) return error.SkipZigTest;
if (builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC64()) return error.SkipZigTest;
const s = SplitStructMixed{
.a = 1234,
.b = 100,
.c = 1337.0,
};
c_split_struct_mixed(s);
const s2 = c_ret_split_struct_mixed();
try expect(s2.a == 1234);
try expect(s2.b == 100);
try expect(s2.c == 1337.0);
}
export fn zig_split_struct_mixed(x: SplitStructMixed) void {
expect(x.a == 1234) catch @panic("test failure");
expect(x.b == 100) catch @panic("test failure");
expect(x.c == 1337.0) catch @panic("test failure");
}
extern fn c_big_struct_both(BigStruct) BigStruct;
extern fn c_multiple_struct_ints(Rect, Rect) void;
extern fn c_multiple_struct_floats(FloatRect, FloatRect) void;
test "C ABI sret and byval together" {
if (builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
const s = BigStruct{
.a = 1,
.b = 2,
.c = 3,
.d = 4,
.e = 5,
};
const y = c_big_struct_both(s);
try expect(y.a == 10);
try expect(y.b == 11);
try expect(y.c == 12);
try expect(y.d == 13);
try expect(y.e == 14);
}
export fn zig_big_struct_both(x: BigStruct) BigStruct {
expect(x.a == 30) catch @panic("test failure");
expect(x.b == 31) catch @panic("test failure");
expect(x.c == 32) catch @panic("test failure");
expect(x.d == 33) catch @panic("test failure");
expect(x.e == 34) catch @panic("test failure");
const s = BigStruct{
.a = 20,
.b = 21,
.c = 22,
.d = 23,
.e = 24,
};
return s;
}
const Vector3 = extern struct {
x: f32,
y: f32,
z: f32,
};
extern fn c_small_struct_floats(Vector3) void;
extern fn c_small_struct_floats_extra(Vector3, ?[*]const u8) void;
const Vector5 = extern struct {
x: f32,
y: f32,
z: f32,
w: f32,
q: f32,
};
extern fn c_big_struct_floats(Vector5) void;
test "C ABI structs of floats as parameter" {
if (builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC64()) return error.SkipZigTest;
const v3 = Vector3{
.x = 3.0,
.y = 6.0,
.z = 12.0,
};
c_small_struct_floats(v3);
c_small_struct_floats_extra(v3, "hello");
const v5 = Vector5{
.x = 76.0,
.y = -1.0,
.z = -12.0,
.w = 69.0,
.q = 55,
};
c_big_struct_floats(v5);
}
const Rect = extern struct {
left: u32,
right: u32,
top: u32,
bottom: u32,
};
export fn zig_multiple_struct_ints(x: Rect, y: Rect) void {
expect(x.left == 1) catch @panic("test failure");
expect(x.right == 21) catch @panic("test failure");
expect(x.top == 16) catch @panic("test failure");
expect(x.bottom == 4) catch @panic("test failure");
expect(y.left == 178) catch @panic("test failure");
expect(y.right == 189) catch @panic("test failure");
expect(y.top == 21) catch @panic("test failure");
expect(y.bottom == 15) catch @panic("test failure");
}
test "C ABI structs of ints as multiple parameters" {
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC64()) return error.SkipZigTest;
const r1 = Rect{
.left = 1,
.right = 21,
.top = 16,
.bottom = 4,
};
const r2 = Rect{
.left = 178,
.right = 189,
.top = 21,
.bottom = 15,
};
c_multiple_struct_ints(r1, r2);
}
const FloatRect = extern struct {
left: f32,
right: f32,
top: f32,
bottom: f32,
};
export fn zig_multiple_struct_floats(x: FloatRect, y: FloatRect) void {
expect(x.left == 1) catch @panic("test failure");
expect(x.right == 21) catch @panic("test failure");
expect(x.top == 16) catch @panic("test failure");
expect(x.bottom == 4) catch @panic("test failure");
expect(y.left == 178) catch @panic("test failure");
expect(y.right == 189) catch @panic("test failure");
expect(y.top == 21) catch @panic("test failure");
expect(y.bottom == 15) catch @panic("test failure");
}
test "C ABI structs of floats as multiple parameters" {
if (builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
const r1 = FloatRect{
.left = 1,
.right = 21,
.top = 16,
.bottom = 4,
};
const r2 = FloatRect{
.left = 178,
.right = 189,
.top = 21,
.bottom = 15,
};
c_multiple_struct_floats(r1, r2);
}
export fn zig_ret_bool() bool {
return true;
}
export fn zig_ret_u8() u8 {
return 0xff;
}
export fn zig_ret_u16() u16 {
return 0xffff;
}
export fn zig_ret_u32() u32 {
return 0xffffffff;
}
export fn zig_ret_u64() u64 {
return 0xffffffffffffffff;
}
export fn zig_ret_i8() i8 {
return -1;
}
export fn zig_ret_i16() i16 {
return -1;
}
export fn zig_ret_i32() i32 {
return -1;
}
export fn zig_ret_i64() i64 {
return -1;
}
export fn zig_ret_small_struct_ints() SmallStructInts {
return .{
.a = 1,
.b = 2,
.c = 3,
.d = 4,
};
}
export fn zig_ret_med_struct_ints() MedStructInts {
return .{
.x = 1,
.y = 2,
.z = 3,
};
}
export fn zig_ret_med_struct_mixed() MedStructMixed {
return .{
.a = 1234,
.b = 100.0,
.c = 1337.0,
};
}
export fn zig_ret_split_struct_mixed() SplitStructMixed {
return .{
.a = 1234,
.b = 100,
.c = 1337.0,
};
}
extern fn c_ret_bool() bool;
extern fn c_ret_u8() u8;
extern fn c_ret_u16() u16;
extern fn c_ret_u32() u32;
extern fn c_ret_u64() u64;
extern fn c_ret_i8() i8;
extern fn c_ret_i16() i16;
extern fn c_ret_i32() i32;
extern fn c_ret_i64() i64;
test "C ABI integer return types" {
try expect(c_ret_bool() == true);
try expect(c_ret_u8() == 0xff);
try expect(c_ret_u16() == 0xffff);
try expect(c_ret_u32() == 0xffffffff);
try expect(c_ret_u64() == 0xffffffffffffffff);
try expect(c_ret_i8() == -1);
try expect(c_ret_i16() == -1);
try expect(c_ret_i32() == -1);
try expect(c_ret_i64() == -1);
}
const StructWithArray = extern struct {
a: i32,
padding: [4]u8,
b: i64,
};
extern fn c_struct_with_array(StructWithArray) void;
extern fn c_ret_struct_with_array() StructWithArray;
test "Struct with array as padding." {
if (builtin.cpu.arch == .x86) return error.SkipZigTest;
if (builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC64()) return error.SkipZigTest;
c_struct_with_array(.{ .a = 1, .padding = undefined, .b = 2 });
const x = c_ret_struct_with_array();
try expect(x.a == 4);
try expect(x.b == 155);
}
const FloatArrayStruct = extern struct {
origin: extern struct {
x: f64,
y: f64,
},
size: extern struct {
width: f64,
height: f64,
},
};
extern fn c_float_array_struct(FloatArrayStruct) void;
extern fn c_ret_float_array_struct() FloatArrayStruct;
test "Float array like struct" {
if (builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
if (builtin.cpu.arch.isPPC()) return error.SkipZigTest;
c_float_array_struct(.{
.origin = .{
.x = 5,
.y = 6,
},
.size = .{
.width = 7,
.height = 8,
},
});
const x = c_ret_float_array_struct();
try expect(x.origin.x == 1);
try expect(x.origin.y == 2);
try expect(x.size.width == 3);
try expect(x.size.height == 4);
}
const SmallVec = @Vector(2, u32);