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