]> gitweb.factorcode.org Git - factor.git/blob - vm/ffi_test.c
compiler.tests.alien: Add structs with uint and ulonglong pairs and return them from...
[factor.git] / vm / ffi_test.c
1 /* This file is linked into the runtime for the sole purpose
2  * of testing FFI code. */
3 #include "ffi_test.h"
4
5 #include <assert.h>
6 #include <string.h>
7
8 void ffi_test_0(void) {}
9
10 int ffi_test_1(void) { return 3; }
11
12 int ffi_test_2(int x, int y) { return x + y; }
13
14 int ffi_test_3(int x, int y, int z, int t) { return x + y + z * t; }
15
16 float ffi_test_4(void) { return 1.5; }
17
18 double ffi_test_5(void) { return 1.5; }
19
20 double ffi_test_6(float x, float y) { return x * y; }
21
22 double ffi_test_7(double x, double y) { return x * y; }
23
24 double ffi_test_8(double x, float y, double z, float t, int w) {
25   return x * y + z * t + w;
26 }
27
28 int ffi_test_9(int a, int b, int c, int d, int e, int f, int g) {
29   return a + b + c + d + e + f + g;
30 }
31
32 int ffi_test_10(int a, int b, double c, int d, float e, int f, int g, int h) {
33   return (int)(a - b - c - d - e - f - g - h);
34 }
35
36 int ffi_test_11(int a, struct foo b, int c) { return a * b.x + c * b.y; }
37
38 int ffi_test_12(int a, int b, struct rect c, int d, int e, int f) {
39   return (int)(a + b + c.x + c.y + c.w + c.h + d + e + f);
40 }
41
42 int ffi_test_13(int a, int b, int c, int d, int e, int f, int g, int h, int i,
43                 int j, int k) {
44   return a + b + c + d + e + f + g + h + i + j + k;
45 }
46
47 struct foo ffi_test_14(int x, int y) {
48   struct foo r;
49   r.x = x;
50   r.y = y;
51   return r;
52 }
53
54 char* ffi_test_15(char* x, char* y) {
55   if (strcmp(x, y))
56     return "foo";
57   else
58     return "bar";
59 }
60
61 struct bar ffi_test_16(long x, long y, long z) {
62   struct bar r;
63   r.x = x;
64   r.y = y;
65   r.z = z;
66   return r;
67 }
68
69 struct tiny ffi_test_17(int x) {
70   struct tiny r;
71   r.x = x;
72   return r;
73 }
74
75 FACTOR_STDCALL(int) ffi_test_18(int x, int y, int z, int t) {
76   return x + y + z * t;
77 }
78
79 FACTOR_STDCALL(struct bar) ffi_test_19(long x, long y, long z) {
80   struct bar r;
81   r.x = x;
82   r.y = y;
83   r.z = z;
84   return r;
85 }
86
87 void ffi_test_20(double x1, double x2, double x3, double y1, double y2,
88                  double y3, double z1, double z2, double z3) {}
89
90 long long ffi_test_21(long x, long y) { return (long long) x * (long long) y; }
91
92 long ffi_test_22(long x, long long y, long long z) { return (long)(x + y / z); }
93
94 float ffi_test_23(float x[3], float y[3]) {
95   return x[0] * y[0] + x[1] * y[1] + x[2] * y[2];
96 }
97
98 struct test_struct_1 ffi_test_24(void) {
99   struct test_struct_1 s;
100   s.x = 1;
101   return s;
102 }
103
104 struct test_struct_2 ffi_test_25(void) {
105   struct test_struct_2 s;
106   s.x = 1;
107   s.y = 2;
108   return s;
109 }
110
111 struct test_struct_3 ffi_test_26(void) {
112   struct test_struct_3 s;
113   s.x = 1;
114   s.y = 2;
115   s.z = 3;
116   return s;
117 }
118
119 struct test_struct_4 ffi_test_27(void) {
120   struct test_struct_4 s;
121   s.x = 1;
122   s.y = 2;
123   s.z = 3;
124   s.a = 4;
125   return s;
126 }
127
128 struct test_struct_5 ffi_test_28(void) {
129   struct test_struct_5 s;
130   s.x = 1;
131   s.y = 2;
132   s.z = 3;
133   s.a = 4;
134   s.b = 5;
135   return s;
136 }
137
138 struct test_struct_6 ffi_test_29(void) {
139   struct test_struct_6 s;
140   s.x = 1;
141   s.y = 2;
142   s.z = 3;
143   s.a = 4;
144   s.b = 5;
145   s.c = 6;
146   return s;
147 }
148
149 struct test_struct_7 ffi_test_30(void) {
150   struct test_struct_7 s;
151   s.x = 1;
152   s.y = 2;
153   s.z = 3;
154   s.a = 4;
155   s.b = 5;
156   s.c = 6;
157   s.d = 7;
158   return s;
159 }
160
161 int ffi_test_31(int x0, int x1, int x2, int x3, int x4, int x5, int x6, int x7,
162                 int x8, int x9, int x10, int x11, int x12, int x13, int x14,
163                 int x15, int x16, int x17, int x18, int x19, int x20, int x21,
164                 int x22, int x23, int x24, int x25, int x26, int x27, int x28,
165                 int x29, int x30, int x31, int x32, int x33, int x34, int x35,
166                 int x36, int x37, int x38, int x39, int x40, int x41) {
167   return x0 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12 +
168          x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24 +
169          x25 + x26 + x27 + x28 + x29 + x30 + x31 + x32 + x33 + x34 + x35 + x36 +
170          x37 + x38 + x39 + x40 + x41;
171 }
172
173 float ffi_test_31_point_5(float x0, float x1, float x2, float x3, float x4,
174                           float x5, float x6, float x7, float x8, float x9,
175                           float x10, float x11, float x12, float x13, float x14,
176                           float x15, float x16, float x17, float x18, float x19,
177                           float x20, float x21, float x22, float x23, float x24,
178                           float x25, float x26, float x27, float x28, float x29,
179                           float x30, float x31, float x32, float x33, float x34,
180                           float x35, float x36, float x37, float x38, float x39,
181                           float x40, float x41) {
182   return x0 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12 +
183          x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24 +
184          x25 + x26 + x27 + x28 + x29 + x30 + x31 + x32 + x33 + x34 + x35 + x36 +
185          x37 + x38 + x39 + x40 + x41;
186 }
187
188 double ffi_test_32(struct test_struct_8 x, int y) { return (x.x + x.y) * y; }
189
190 double ffi_test_33(struct test_struct_9 x, int y) { return (x.x + x.y) * y; }
191
192 double ffi_test_34(struct test_struct_10 x, int y) { return (x.x + x.y) * y; }
193
194 double ffi_test_35(struct test_struct_11 x, int y) { return (x.x + x.y) * y; }
195
196 double ffi_test_36(struct test_struct_12 x) { return x.x; }
197
198 static int global_var;
199
200 void ffi_test_36_point_5(void) { global_var = 0; }
201
202 int ffi_test_37(int (*f)(int, int, int)) {
203   global_var = f(global_var, global_var * 2, global_var * 3);
204   return global_var;
205 }
206
207 unsigned long long ffi_test_38(unsigned long long x, unsigned long long y) {
208   return x * y;
209 }
210
211 int ffi_test_39(long a, long b, struct test_struct_13 s) {
212   assert(a == b);
213   return (int)(s.x1 + s.x2 + s.x3 + s.x4 + s.x5 + s.x6);
214 }
215
216 struct test_struct_14 ffi_test_40(double x1, double x2) {
217   struct test_struct_14 retval;
218   retval.x1 = x1;
219   retval.x2 = x2;
220   return retval;
221 }
222
223 struct test_struct_12 ffi_test_41(int a, double x) {
224   struct test_struct_12 retval;
225   retval.a = a;
226   retval.x = x;
227   return retval;
228 }
229
230 struct test_struct_15 ffi_test_42(float x, float y) {
231   struct test_struct_15 retval;
232   retval.x = x;
233   retval.y = y;
234   return retval;
235 }
236
237 struct test_struct_16 ffi_test_43(float x, int a) {
238   struct test_struct_16 retval;
239   retval.x = x;
240   retval.a = a;
241   return retval;
242 }
243
244 struct test_struct_14 ffi_test_44(void) {
245   struct test_struct_14 retval;
246   retval.x1 = 1.0;
247   retval.x2 = 2.0;
248   return retval;
249 }
250
251 /* C99 features */
252 #ifndef _MSC_VER
253
254 _Complex float ffi_test_45(int x) { return x; }
255
256 _Complex double ffi_test_46(int x) { return x; }
257
258 _Complex float ffi_test_47(_Complex float x, _Complex double y) {
259   return x + 2 * y;
260 }
261
262 short ffi_test_48(struct bool_field_test x) { return x.parents; }
263
264 #endif
265
266 FACTOR_FASTCALL(int) ffi_test_49(int x) { return x + 1; }
267
268 FACTOR_FASTCALL(int) ffi_test_50(int x, int y) { return x + y + 1; }
269
270 FACTOR_FASTCALL(int) ffi_test_51(int x, int y, int z) { return x + y + z + 1; }
271
272 FACTOR_FASTCALL(int) ffi_test_52(int x, float y, int z) {
273   return (int)(x + y + z + 1);
274 }
275
276 FACTOR_FASTCALL(int) ffi_test_53(int x, float y, int z, int w) {
277   return (int)(x + y + z + w + 1);
278 }
279
280 FACTOR_FASTCALL(int) ffi_test_54(struct test_struct_11 x, int y) {
281   return x.x + x.y + y + 1;
282 }
283
284 FACTOR_FASTCALL(int) ffi_test_55(struct test_struct_11 x, int y, int z) {
285   return x.x + x.y + y + z + 1;
286 }
287
288 FACTOR_FASTCALL(int) ffi_test_56(struct test_struct_11 x, int y, int z, int w) {
289   return x.x + x.y + y + z + w + 1;
290 }
291
292 FACTOR_FASTCALL(struct test_struct_11) ffi_test_57(int x, int y) {
293   struct test_struct_11 r = { x + y, x - y };
294   return r;
295 }
296
297 FACTOR_FASTCALL(struct test_struct_11) ffi_test_58(int x, int y, int z) {
298   struct test_struct_11 r = { x + y, y - z };
299   return r;
300 }
301
302 signed long long ffi_test_59(signed long long x) {
303   return x;
304 }
305
306 unsigned long long ffi_test_60(unsigned long long x) {
307   return x;
308 }
309
310 struct bool_and_ptr ffi_test_61() {
311   struct bool_and_ptr bap;
312   bap.b = true;
313   bap.ptr = NULL;
314   return bap;
315 }
316
317 struct uint_pair ffi_test_62() {
318   struct uint_pair uip;
319   uip.a = 0xabcdefab;
320   uip.b = 0x12345678;
321   return uip;
322 }
323
324 struct ulonglong_pair ffi_test_63() {
325   struct ulonglong_pair ullp;
326   ullp.a = 0xabcdefabcdefabcd;
327   ullp.b = 0x1234567891234567;
328   return ullp;
329 }