fix VLA/continue issue
[tinycc.git] / tests / abitest.c
blob9cdeb87e93cdf52239f46f9beb72d75074f1a2ab
1 #include <libtcc.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdarg.h>
7 // MinGW has 80-bit rather than 64-bit long double which isn't compatible with TCC or MSVC
8 #if defined(_WIN32) && defined(__GNUC__)
9 #define LONG_DOUBLE double
10 #define LONG_DOUBLE_LITERAL(x) x
11 #else
12 #define LONG_DOUBLE long double
13 #define LONG_DOUBLE_LITERAL(x) x ## L
14 #endif
16 static const char *tccdir = NULL;
17 static const char *include_dir = NULL;
19 typedef int (*callback_type) (void*);
22 * Compile source code and call a callback with a pointer to the symbol "f".
24 static int run_callback(const char *src, callback_type callback) {
25 TCCState *s;
26 int result;
27 void *ptr;
29 s = tcc_new();
30 if (!s)
31 return -1;
32 if (tccdir)
33 tcc_set_lib_path(s, tccdir);
34 if (include_dir) {
35 if (tcc_add_include_path(s, include_dir) == -1)
36 return -1;
38 if (tcc_set_output_type(s, TCC_OUTPUT_MEMORY) == -1)
39 return -1;
40 if (tcc_compile_string(s, src) == -1)
41 return -1;
42 if (tcc_relocate(s, TCC_RELOCATE_AUTO) == -1)
43 return -1;
45 ptr = tcc_get_symbol(s, "f");
46 if (!ptr)
47 return -1;
48 result = callback(ptr);
50 tcc_delete(s);
52 return result;
55 #define STR2(x) #x
56 #define STR(x) STR2(x)
58 #define RET_PRIMITIVE_TEST(name, type, val) \
59 static int ret_ ## name ## _test_callback(void *ptr) { \
60 type (*callback) (type) = (type(*)(type))ptr; \
61 type x = val; \
62 type y = callback(x); \
63 return (y == x+x) ? 0 : -1; \
64 } \
66 static int ret_ ## name ## _test(void) { \
67 const char *src = STR(type) " f(" STR(type) " x) {return x+x;}"; \
68 return run_callback(src, ret_ ## name ## _test_callback); \
71 RET_PRIMITIVE_TEST(int, int, 70000)
72 RET_PRIMITIVE_TEST(longlong, long long, 4333369356528LL)
73 RET_PRIMITIVE_TEST(float, float, 63.0)
74 RET_PRIMITIVE_TEST(double, double, 14789798.0)
75 RET_PRIMITIVE_TEST(longdouble, LONG_DOUBLE, LONG_DOUBLE_LITERAL(378943892.0))
78 * ret_2float_test:
80 * On x86-64, a struct with 2 floats should be packed into a single
81 * SSE register (VT_DOUBLE is used for this purpose).
83 typedef struct ret_2float_test_type_s {float x, y;} ret_2float_test_type;
84 typedef ret_2float_test_type (*ret_2float_test_function_type) (ret_2float_test_type);
86 static int ret_2float_test_callback(void *ptr) {
87 ret_2float_test_function_type f = (ret_2float_test_function_type)ptr;
88 ret_2float_test_type a = {10, 35};
89 ret_2float_test_type r;
90 r = f(a);
91 return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
94 static int ret_2float_test(void) {
95 const char *src =
96 "typedef struct ret_2float_test_type_s {float x, y;} ret_2float_test_type;"
97 "ret_2float_test_type f(ret_2float_test_type a) {\n"
98 " ret_2float_test_type r = {a.x*5, a.y*3};\n"
99 " return r;\n"
100 "}\n";
102 return run_callback(src, ret_2float_test_callback);
106 * ret_2double_test:
108 * On x86-64, a struct with 2 doubles should be passed in two SSE
109 * registers.
111 typedef struct ret_2double_test_type_s {double x, y;} ret_2double_test_type;
112 typedef ret_2double_test_type (*ret_2double_test_function_type) (ret_2double_test_type);
114 static int ret_2double_test_callback(void *ptr) {
115 ret_2double_test_function_type f = (ret_2double_test_function_type)ptr;
116 ret_2double_test_type a = {10, 35};
117 ret_2double_test_type r;
118 r = f(a);
119 return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
122 static int ret_2double_test(void) {
123 const char *src =
124 "typedef struct ret_2double_test_type_s {double x, y;} ret_2double_test_type;"
125 "ret_2double_test_type f(ret_2double_test_type a) {\n"
126 " ret_2double_test_type r = {a.x*5, a.y*3};\n"
127 " return r;\n"
128 "}\n";
130 return run_callback(src, ret_2double_test_callback);
134 * ret_8plus2double_test:
136 * This catches a corner case in the x86_64 ABI code: the first 7
137 * arguments fit into registers, the 8th doesn't, but the 9th argument
138 * fits into the 8th XMM register.
140 * Note that the purpose of the 10th argument is to avoid a situation
141 * in which gcc would accidentally put the double at the right
142 * address, thus causing a success message even though TCC actually
143 * generated incorrect code.
145 typedef ret_2double_test_type (*ret_8plus2double_test_function_type) (double, double, double, double, double, double, double, ret_2double_test_type, double, double);
147 static int ret_8plus2double_test_callback(void *ptr) {
148 ret_8plus2double_test_function_type f = (ret_8plus2double_test_function_type)ptr;
149 ret_2double_test_type a = {10, 35};
150 ret_2double_test_type r;
151 r = f(0, 0, 0, 0, 0, 0, 0, a, 37, 38);
152 return ((r.x == 37) && (r.y == 37)) ? 0 : -1;
155 static int ret_8plus2double_test(void) {
156 const char *src =
157 "typedef struct ret_2double_test_type_s {double x, y;} ret_2double_test_type;"
158 "ret_2double_test_type f(double x1, double x2, double x3, double x4, double x5, double x6, double x7, ret_2double_test_type a, double x8, double x9) {\n"
159 " ret_2double_test_type r = { x8, x8 };\n"
160 " return r;\n"
161 "}\n";
163 return run_callback(src, ret_8plus2double_test_callback);
167 * ret_mixed_test:
169 * On x86-64, a struct with a double and a 64-bit integer should be
170 * passed in one SSE register and one integer register.
172 typedef struct ret_mixed_test_type_s {double x; long long y;} ret_mixed_test_type;
173 typedef ret_mixed_test_type (*ret_mixed_test_function_type) (ret_mixed_test_type);
175 static int ret_mixed_test_callback(void *ptr) {
176 ret_mixed_test_function_type f = (ret_mixed_test_function_type)ptr;
177 ret_mixed_test_type a = {10, 35};
178 ret_mixed_test_type r;
179 r = f(a);
180 return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
183 static int ret_mixed_test(void) {
184 const char *src =
185 "typedef struct ret_mixed_test_type_s {double x; long long y;} ret_mixed_test_type;"
186 "ret_mixed_test_type f(ret_mixed_test_type a) {\n"
187 " ret_mixed_test_type r = {a.x*5, a.y*3};\n"
188 " return r;\n"
189 "}\n";
191 return run_callback(src, ret_mixed_test_callback);
195 * ret_mixed2_test:
197 * On x86-64, a struct with two floats and two 32-bit integers should
198 * be passed in one SSE register and one integer register.
200 typedef struct ret_mixed2_test_type_s {float x,x2; int y,y2;} ret_mixed2_test_type;
201 typedef ret_mixed2_test_type (*ret_mixed2_test_function_type) (ret_mixed2_test_type);
203 static int ret_mixed2_test_callback(void *ptr) {
204 ret_mixed2_test_function_type f = (ret_mixed2_test_function_type)ptr;
205 ret_mixed2_test_type a = {10, 5, 35, 7 };
206 ret_mixed2_test_type r;
207 r = f(a);
208 return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
211 static int ret_mixed2_test(void) {
212 const char *src =
213 "typedef struct ret_mixed2_test_type_s {float x, x2; int y,y2;} ret_mixed2_test_type;"
214 "ret_mixed2_test_type f(ret_mixed2_test_type a) {\n"
215 " ret_mixed2_test_type r = {a.x*5, 0, a.y*3, 0};\n"
216 " return r;\n"
217 "}\n";
219 return run_callback(src, ret_mixed2_test_callback);
223 * ret_mixed3_test:
225 * On x86-64, this struct should be passed in two integer registers.
227 typedef struct ret_mixed3_test_type_s {float x; int y; float x2; int y2;} ret_mixed3_test_type;
228 typedef ret_mixed3_test_type (*ret_mixed3_test_function_type) (ret_mixed3_test_type);
230 static int ret_mixed3_test_callback(void *ptr) {
231 ret_mixed3_test_function_type f = (ret_mixed3_test_function_type)ptr;
232 ret_mixed3_test_type a = {10, 5, 35, 7 };
233 ret_mixed3_test_type r;
234 r = f(a);
235 return ((r.x == a.x*5) && (r.y2 == a.y*3)) ? 0 : -1;
238 static int ret_mixed3_test(void) {
239 const char *src =
240 "typedef struct ret_mixed3_test_type_s {float x; int y; float x2; int y2;} ret_mixed3_test_type;"
241 "ret_mixed3_test_type f(ret_mixed3_test_type a) {\n"
242 " ret_mixed3_test_type r = {a.x*5, 0, 0, a.y*3};\n"
243 " return r;\n"
244 "}\n";
246 return run_callback(src, ret_mixed3_test_callback);
250 * reg_pack_test: return a small struct which should be packed into
251 * registers (Win32) during return.
253 typedef struct reg_pack_test_type_s {int x, y;} reg_pack_test_type;
254 typedef reg_pack_test_type (*reg_pack_test_function_type) (reg_pack_test_type);
256 static int reg_pack_test_callback(void *ptr) {
257 reg_pack_test_function_type f = (reg_pack_test_function_type)ptr;
258 reg_pack_test_type a = {10, 35};
259 reg_pack_test_type r;
260 r = f(a);
261 return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
264 static int reg_pack_test(void) {
265 const char *src =
266 "typedef struct reg_pack_test_type_s {int x, y;} reg_pack_test_type;"
267 "reg_pack_test_type f(reg_pack_test_type a) {\n"
268 " reg_pack_test_type r = {a.x*5, a.y*3};\n"
269 " return r;\n"
270 "}\n";
272 return run_callback(src, reg_pack_test_callback);
276 * reg_pack_longlong_test: return a small struct which should be packed into
277 * registers (x86-64) during return.
279 typedef struct reg_pack_longlong_test_type_s {long long x, y;} reg_pack_longlong_test_type;
280 typedef reg_pack_longlong_test_type (*reg_pack_longlong_test_function_type) (reg_pack_longlong_test_type);
282 static int reg_pack_longlong_test_callback(void *ptr) {
283 reg_pack_longlong_test_function_type f = (reg_pack_longlong_test_function_type)ptr;
284 reg_pack_longlong_test_type a = {10, 35};
285 reg_pack_longlong_test_type r;
286 r = f(a);
287 return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
290 static int reg_pack_longlong_test(void) {
291 const char *src =
292 "typedef struct reg_pack_longlong_test_type_s {long long x, y;} reg_pack_longlong_test_type;"
293 "reg_pack_longlong_test_type f(reg_pack_longlong_test_type a) {\n"
294 " reg_pack_longlong_test_type r = {a.x*5, a.y*3};\n"
295 " return r;\n"
296 "}\n";
298 return run_callback(src, reg_pack_longlong_test_callback);
302 * ret_6plus2longlong_test:
304 * This catches a corner case in the x86_64 ABI code: the first 5
305 * arguments fit into registers, the 6th doesn't, but the 7th argument
306 * fits into the 6th argument integer register, %r9.
308 * Note that the purpose of the 10th argument is to avoid a situation
309 * in which gcc would accidentally put the longlong at the right
310 * address, thus causing a success message even though TCC actually
311 * generated incorrect code.
313 typedef reg_pack_longlong_test_type (*ret_6plus2longlong_test_function_type) (long long, long long, long long, long long, long long, reg_pack_longlong_test_type, long long, long long);
315 static int ret_6plus2longlong_test_callback(void *ptr) {
316 ret_6plus2longlong_test_function_type f = (ret_6plus2longlong_test_function_type)ptr;
317 reg_pack_longlong_test_type a = {10, 35};
318 reg_pack_longlong_test_type r;
319 r = f(0, 0, 0, 0, 0, a, 37, 38);
320 return ((r.x == 37) && (r.y == 37)) ? 0 : -1;
323 static int ret_6plus2longlong_test(void) {
324 const char *src =
325 "typedef struct reg_pack_longlong_test_type_s {long long x, y;} reg_pack_longlong_test_type;"
326 "reg_pack_longlong_test_type f(long long x1, long long x2, long long x3, long long x4, long long x5, reg_pack_longlong_test_type a, long long x8, long long x9) {\n"
327 " reg_pack_longlong_test_type r = { x8, x8 };\n"
328 " return r;\n"
329 "}\n";
331 return run_callback(src, ret_6plus2longlong_test_callback);
335 * sret_test: Create a struct large enough to be returned via sret
336 * (hidden pointer as first function argument)
338 typedef struct sret_test_type_s {long long a, b, c;} sret_test_type;
339 typedef sret_test_type (*sret_test_function_type) (sret_test_type);
341 static int sret_test_callback(void *ptr) {
342 sret_test_function_type f = (sret_test_function_type)(ptr);
343 sret_test_type x = {5436LL, 658277698LL, 43878957LL};
344 sret_test_type r = f(x);
345 return ((r.a==x.a*35)&&(r.b==x.b*19)&&(r.c==x.c*21)) ? 0 : -1;
348 static int sret_test(void) {
349 const char *src =
350 "typedef struct sret_test_type_s {long long a, b, c;} sret_test_type;\n"
351 "sret_test_type f(sret_test_type x) {\n"
352 " sret_test_type r = {x.a*35, x.b*19, x.c*21};\n"
353 " return r;\n"
354 "}\n";
356 return run_callback(src, sret_test_callback);
360 * one_member_union_test:
362 * In the x86-64 ABI a union should always be passed on the stack. However
363 * it appears that a single member union is treated by GCC as its member.
365 typedef union one_member_union_test_type_u {int x;} one_member_union_test_type;
366 typedef one_member_union_test_type (*one_member_union_test_function_type) (one_member_union_test_type);
368 static int one_member_union_test_callback(void *ptr) {
369 one_member_union_test_function_type f = (one_member_union_test_function_type)ptr;
370 one_member_union_test_type a, b;
371 a.x = 34;
372 b = f(a);
373 return (b.x == a.x*2) ? 0 : -1;
376 static int one_member_union_test(void) {
377 const char *src =
378 "typedef union one_member_union_test_type_u {int x;} one_member_union_test_type;\n"
379 "one_member_union_test_type f(one_member_union_test_type a) {\n"
380 " one_member_union_test_type b;\n"
381 " b.x = a.x * 2;\n"
382 " return b;\n"
383 "}\n";
384 return run_callback(src, one_member_union_test_callback);
388 * two_member_union_test:
390 * In the x86-64 ABI a union should always be passed on the stack.
392 typedef union two_member_union_test_type_u {int x; long y;} two_member_union_test_type;
393 typedef two_member_union_test_type (*two_member_union_test_function_type) (two_member_union_test_type);
395 static int two_member_union_test_callback(void *ptr) {
396 two_member_union_test_function_type f = (two_member_union_test_function_type)ptr;
397 two_member_union_test_type a, b;
398 a.x = 34;
399 b = f(a);
400 return (b.x == a.x*2) ? 0 : -1;
403 static int two_member_union_test(void) {
404 const char *src =
405 "typedef union two_member_union_test_type_u {int x; long y;} two_member_union_test_type;\n"
406 "two_member_union_test_type f(two_member_union_test_type a) {\n"
407 " two_member_union_test_type b;\n"
408 " b.x = a.x * 2;\n"
409 " return b;\n"
410 "}\n";
411 return run_callback(src, two_member_union_test_callback);
415 * Win64 calling convetntion test.
418 typedef struct many_struct_test_type_s {long long a, b, c;} many_struct_test_type;
419 typedef many_struct_test_type (*many_struct_test_function_type) (many_struct_test_type,many_struct_test_type,many_struct_test_type,many_struct_test_type,many_struct_test_type,many_struct_test_type);
421 static int many_struct_test_callback(void *ptr) {
422 many_struct_test_function_type f = (many_struct_test_function_type)ptr;
423 many_struct_test_type v = {1, 2, 3};
424 many_struct_test_type r = f(v,v,v,v,v,v);
425 return ((r.a == 6) && (r.b == 12) && (r.c == 18))?0:-1;
428 static int many_struct_test(void) {
429 const char *src =
430 "typedef struct many_struct_test_type_s {long long a, b, c;} many_struct_test_type;\n"
431 "many_struct_test_type f(many_struct_test_type x1, many_struct_test_type x2, many_struct_test_type x3, many_struct_test_type x4, many_struct_test_type x5, many_struct_test_type x6) {\n"
432 " many_struct_test_type y;\n"
433 " y.a = x1.a + x2.a + x3.a + x4.a + x5.a + x6.a;\n"
434 " y.b = x1.b + x2.b + x3.b + x4.b + x5.b + x6.b;\n"
435 " y.c = x1.c + x2.c + x3.c + x4.c + x5.c + x6.c;\n"
436 " return y;\n"
437 "}\n";
438 return run_callback(src, many_struct_test_callback);
442 * Win64 calling convention test.
445 typedef struct many_struct_test_2_type_s {int a, b;} many_struct_test_2_type;
446 typedef many_struct_test_2_type (*many_struct_test_2_function_type) (many_struct_test_2_type,many_struct_test_2_type,many_struct_test_2_type,many_struct_test_2_type,many_struct_test_2_type,many_struct_test_2_type);
448 static int many_struct_test_2_callback(void *ptr) {
449 many_struct_test_2_function_type f = (many_struct_test_2_function_type)ptr;
450 many_struct_test_2_type v = {1,2};
451 many_struct_test_2_type r = f(v,v,v,v,v,v);
452 return ((r.a == 6) && (r.b == 12))?0:-1;
455 static int many_struct_test_2(void) {
456 const char *src =
457 "typedef struct many_struct_test_2_type_s {int a, b;} many_struct_test_2_type;\n"
458 "many_struct_test_2_type f(many_struct_test_2_type x1, many_struct_test_2_type x2, many_struct_test_2_type x3, many_struct_test_2_type x4, many_struct_test_2_type x5, many_struct_test_2_type x6) {\n"
459 " many_struct_test_2_type y;\n"
460 " y.a = x1.a + x2.a + x3.a + x4.a + x5.a + x6.a;\n"
461 " y.b = x1.b + x2.b + x3.b + x4.b + x5.b + x6.b;\n"
462 " return y;\n"
463 "}\n";
464 return run_callback(src, many_struct_test_2_callback);
468 * Win64 calling convention test.
471 typedef struct many_struct_test_3_type_s {int a, b;} many_struct_test_3_type;
472 typedef many_struct_test_3_type (*many_struct_test_3_function_type) (many_struct_test_3_type,many_struct_test_3_type,many_struct_test_3_type,many_struct_test_3_type,many_struct_test_3_type,many_struct_test_3_type, ...);
473 typedef struct many_struct_test_3_struct_type { many_struct_test_3_function_type f; many_struct_test_3_function_type *f2; } many_struct_test_3_struct_type;
475 static void many_struct_test_3_dummy(double d, ...)
477 volatile double x = d;
480 static int many_struct_test_3_callback(void *ptr) {
481 many_struct_test_3_struct_type s = { ptr, };
482 many_struct_test_3_struct_type *s2 = &s;
483 s2->f2 = &s2->f;
484 many_struct_test_3_dummy(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, &s2);
485 many_struct_test_3_function_type f = *(s2->f2);
486 many_struct_test_3_type v = {1,2};
487 many_struct_test_3_type r = (*((s2->f2=&f)+0))(v,v,v,v,v,v,1.0);
488 return ((r.a == 6) && (r.b == 12))?0:-1;
491 static int many_struct_test_3(void) {
492 const char *src =
493 "typedef struct many_struct_test_3_type_s {int a, b;} many_struct_test_3_type;\n"
494 "many_struct_test_3_type f(many_struct_test_3_type x1, many_struct_test_3_type x2, many_struct_test_3_type x3, many_struct_test_3_type x4, many_struct_test_3_type x5, many_struct_test_3_type x6, ...) {\n"
495 " many_struct_test_3_type y;\n"
496 " y.a = x1.a + x2.a + x3.a + x4.a + x5.a + x6.a;\n"
497 " y.b = x1.b + x2.b + x3.b + x4.b + x5.b + x6.b;\n"
498 " return y;\n"
499 "}\n";
500 return run_callback(src, many_struct_test_3_callback);
504 * stdarg_test: Test variable argument list ABI
507 typedef struct {long long a, b, c;} stdarg_test_struct_type;
508 typedef void (*stdarg_test_function_type) (int,int,int,...);
510 static int stdarg_test_callback(void *ptr) {
511 stdarg_test_function_type f = (stdarg_test_function_type)ptr;
512 int x;
513 double y;
514 stdarg_test_struct_type z = {1, 2, 3}, w;
515 f(10, 10, 5,
516 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, &x,
517 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, &y,
518 z, z, z, z, z, &w);
519 return ((x == 55) && (y == 55) && (w.a == 5) && (w.b == 10) && (w.c == 15)) ? 0 : -1;
522 static int stdarg_test(void) {
523 const char *src =
524 "#include <stdarg.h>\n"
525 "typedef struct {long long a, b, c;} stdarg_test_struct_type;\n"
526 "void f(int n_int, int n_float, int n_struct, ...) {\n"
527 " int i, ti = 0;\n"
528 " double td = 0.0;\n"
529 " stdarg_test_struct_type ts = {0,0,0}, tmp;\n"
530 " va_list ap;\n"
531 " va_start(ap, n_struct);\n"
532 " for (i = 0, ti = 0; i < n_int; ++i)\n"
533 " ti += va_arg(ap, int);\n"
534 " *va_arg(ap, int*) = ti;\n"
535 " for (i = 0, td = 0; i < n_float; ++i)\n"
536 " td += va_arg(ap, double);\n"
537 " *va_arg(ap, double*) = td;\n"
538 " for (i = 0; i < n_struct; ++i) {\n"
539 " tmp = va_arg(ap, stdarg_test_struct_type);\n"
540 " ts.a += tmp.a; ts.b += tmp.b; ts.c += tmp.c;"
541 " }\n"
542 " *va_arg(ap, stdarg_test_struct_type*) = ts;\n"
543 " va_end(ap);"
544 "}\n";
545 return run_callback(src, stdarg_test_callback);
549 * Test Win32 stdarg handling, since the calling convention will pass a pointer
550 * to the struct and the stdarg pointer must point to that pointer initially.
553 typedef struct {long long a, b, c;} stdarg_struct_test_struct_type;
554 typedef int (*stdarg_struct_test_function_type) (stdarg_struct_test_struct_type a, ...);
556 static int stdarg_struct_test_callback(void *ptr) {
557 stdarg_struct_test_function_type f = (stdarg_struct_test_function_type)ptr;
558 stdarg_struct_test_struct_type v = {10, 35, 99};
559 int x = f(v, 234);
560 return (x == 378) ? 0 : -1;
563 static int stdarg_struct_test(void) {
564 const char *src =
565 "#include <stdarg.h>\n"
566 "typedef struct {long long a, b, c;} stdarg_struct_test_struct_type;\n"
567 "int f(stdarg_struct_test_struct_type a, ...) {\n"
568 " va_list ap;\n"
569 " va_start(ap, a);\n"
570 " int z = va_arg(ap, int);\n"
571 " va_end(ap);\n"
572 " return z + a.a + a.b + a.c;\n"
573 "}\n";
574 return run_callback(src, stdarg_struct_test_callback);
577 /* Test that x86-64 arranges the stack correctly for arguments with alignment >8 bytes */
579 typedef LONG_DOUBLE (*arg_align_test_callback_type) (LONG_DOUBLE,int,LONG_DOUBLE,int,LONG_DOUBLE);
581 static int arg_align_test_callback(void *ptr) {
582 arg_align_test_callback_type f = (arg_align_test_callback_type)ptr;
583 long double x = f(12, 0, 25, 0, 37);
584 return (x == 74) ? 0 : -1;
587 static int arg_align_test(void) {
588 const char *src =
589 "long double f(long double a, int b, long double c, int d, long double e) {\n"
590 " return a + c + e;\n"
591 "}\n";
592 return run_callback(src, arg_align_test_callback);
595 #define RUN_TEST(t) \
596 if (!testname || (strcmp(#t, testname) == 0)) { \
597 fputs(#t "... ", stdout); \
598 fflush(stdout); \
599 if (t() == 0) { \
600 fputs("success\n", stdout); \
601 } else { \
602 fputs("failure\n", stdout); \
603 retval = EXIT_FAILURE; \
607 int main(int argc, char **argv) {
608 int i;
609 const char *testname = NULL;
610 int retval = EXIT_SUCCESS;
612 /* if tcclib.h and libtcc1.a are not installed, where can we find them */
613 for (i = 1; i < argc; ++i) {
614 if (!memcmp(argv[i], "lib_path=",9))
615 tccdir = argv[i] + 9;
616 else if (!memcmp(argv[i], "run_test=", 9))
617 testname = argv[i] + 9;
618 else if (!memcmp(argv[i], "include=", 8))
619 include_dir = argv[i] + 8;
622 RUN_TEST(ret_int_test);
623 RUN_TEST(ret_longlong_test);
624 RUN_TEST(ret_float_test);
625 RUN_TEST(ret_double_test);
626 RUN_TEST(ret_longdouble_test);
627 RUN_TEST(ret_2float_test);
628 RUN_TEST(ret_2double_test);
629 /* RUN_TEST(ret_8plus2double_test); currently broken on x86_64 */
630 /* RUN_TEST(ret_6plus2longlong_test); currently broken on x86_64 */
631 /* RUN_TEST(ret_mixed_test); currently broken on x86_64 */
632 /* RUN_TEST(ret_mixed2_test); currently broken on x86_64 */
633 RUN_TEST(ret_mixed3_test);
634 RUN_TEST(reg_pack_test);
635 RUN_TEST(reg_pack_longlong_test);
636 RUN_TEST(sret_test);
637 RUN_TEST(one_member_union_test);
638 RUN_TEST(two_member_union_test);
639 RUN_TEST(many_struct_test);
640 RUN_TEST(many_struct_test_2);
641 RUN_TEST(many_struct_test_3);
642 RUN_TEST(stdarg_test);
643 RUN_TEST(stdarg_struct_test);
644 RUN_TEST(arg_align_test);
645 return retval;