Fixes include the double quotes bug
[tinycc.git] / tests / abitest.c
blobd840d857a61ea19133f127dfe0830e97cd79d2c2
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) && (f(a).x == a.x*5) && (f(a).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) && (f(a).x == a.x*5) && (f(a).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);
133 typedef struct ret_longdouble_test_type_s2 {LONG_DOUBLE x;} ret_longdouble_test_type;
134 typedef ret_longdouble_test_type (*ret_longdouble_test_function_type) (ret_longdouble_test_type);
136 static int ret_longdouble_test_callback2(void *ptr) {
137 ret_longdouble_test_function_type f = (ret_longdouble_test_function_type)ptr;
138 ret_longdouble_test_type a = {10};
139 ret_longdouble_test_type r;
140 r = f(a);
141 return ((r.x == a.x*5) && (f(a).x == a.x*5)) ? 0 : -1;
144 static int ret_longdouble_test2(void) {
145 const char *src =
146 "typedef struct ret_longdouble_test_type_s2 {long double x;} ret_longdouble_test_type;"
147 "ret_longdouble_test_type f(ret_longdouble_test_type a) {\n"
148 " ret_longdouble_test_type r = {a.x*5};\n"
149 " return r;\n"
150 "}\n";
152 return run_callback(src, ret_longdouble_test_callback2);
155 typedef struct ret_longlong_test_type_s2 {int x[4];} ret_longlong_test_type;
156 typedef ret_longlong_test_type (*ret_longlong_test_function_type) (ret_longlong_test_type);
158 static int ret_longlong_test_callback2(void *ptr) {
159 ret_longlong_test_function_type f = (ret_longlong_test_function_type)ptr;
160 ret_longlong_test_type a = {{10,11,12,13}};
161 ret_longlong_test_type r;
162 r = f(a);
163 return ((r.x[2] == a.x[2]*5) && (f(a).x[2] == a.x[2]*5)) ? 0 : -1;
166 static int ret_longlong_test2(void) {
167 const char *src =
168 "typedef struct ret_longlong_test_type_s2 {int x[4];} ret_longlong_test_type;"
169 "ret_longlong_test_type f(ret_longlong_test_type a) {\n"
170 " ret_longlong_test_type r = {.x[2] = a.x[2]*5};\n"
171 " return r;\n"
172 "}\n";
174 return run_callback(src, ret_longlong_test_callback2);
178 * reg_pack_test: return a small struct which should be packed into
179 * registers (Win32) during return.
181 typedef struct reg_pack_test_type_s {int x, y;} reg_pack_test_type;
182 typedef reg_pack_test_type (*reg_pack_test_function_type) (reg_pack_test_type);
184 static int reg_pack_test_callback(void *ptr) {
185 reg_pack_test_function_type f = (reg_pack_test_function_type)ptr;
186 reg_pack_test_type a = {10, 35};
187 reg_pack_test_type r;
188 r = f(a);
189 return ((r.x == a.x*5) && (r.y == a.y*3) && (f(a).x == a.x*5) && (f(a).y == a.y*3)) ? 0 : -1;
192 static int reg_pack_test(void) {
193 const char *src =
194 "typedef struct reg_pack_test_type_s {int x, y;} reg_pack_test_type;"
195 "reg_pack_test_type f(reg_pack_test_type a) {\n"
196 " reg_pack_test_type r = {a.x*5, a.y*3};\n"
197 " return r;\n"
198 "}\n";
200 return run_callback(src, reg_pack_test_callback);
204 * reg_pack_longlong_test: return a small struct which should be packed into
205 * registers (x86-64) during return.
207 typedef struct reg_pack_longlong_test_type_s {long long x, y;} reg_pack_longlong_test_type;
208 typedef reg_pack_longlong_test_type (*reg_pack_longlong_test_function_type) (reg_pack_longlong_test_type);
210 static int reg_pack_longlong_test_callback(void *ptr) {
211 reg_pack_longlong_test_function_type f = (reg_pack_longlong_test_function_type)ptr;
212 reg_pack_longlong_test_type a = {10, 35};
213 reg_pack_longlong_test_type r;
214 r = f(a);
215 return ((r.x == a.x*5) && (r.y == a.y*3) && (f(a).x == a.x*5) && (f(a).y == a.y*3)) ? 0 : -1;
218 static int reg_pack_longlong_test(void) {
219 const char *src =
220 "typedef struct reg_pack_longlong_test_type_s {long long x, y;} reg_pack_longlong_test_type;"
221 "reg_pack_longlong_test_type f(reg_pack_longlong_test_type a) {\n"
222 " reg_pack_longlong_test_type r = {a.x*5, a.y*3};\n"
223 " return r;\n"
224 "}\n";
226 return run_callback(src, reg_pack_longlong_test_callback);
230 * sret_test: Create a struct large enough to be returned via sret
231 * (hidden pointer as first function argument)
233 typedef struct sret_test_type_s {long long a, b, c;} sret_test_type;
234 typedef sret_test_type (*sret_test_function_type) (sret_test_type);
236 static int sret_test_callback(void *ptr) {
237 sret_test_function_type f = (sret_test_function_type)(ptr);
238 sret_test_type x = {5436LL, 658277698LL, 43878957LL};
239 sret_test_type r = f(x);
240 return ((r.a==x.a*35)&&(r.b==x.b*19)&&(r.c==x.c*21)) ? 0 : -1;
243 static int sret_test(void) {
244 const char *src =
245 "typedef struct sret_test_type_s {long long a, b, c;} sret_test_type;\n"
246 "sret_test_type f(sret_test_type x) {\n"
247 " sret_test_type r = {x.a*35, x.b*19, x.c*21};\n"
248 " return r;\n"
249 "}\n";
251 return run_callback(src, sret_test_callback);
255 * one_member_union_test:
257 * In the x86-64 ABI a union should always be passed on the stack. However
258 * it appears that a single member union is treated by GCC as its member.
260 typedef union one_member_union_test_type_u {int x;} one_member_union_test_type;
261 typedef one_member_union_test_type (*one_member_union_test_function_type) (one_member_union_test_type);
263 static int one_member_union_test_callback(void *ptr) {
264 one_member_union_test_function_type f = (one_member_union_test_function_type)ptr;
265 one_member_union_test_type a, b;
266 a.x = 34;
267 b = f(a);
268 return (b.x == a.x*2) ? 0 : -1;
271 static int one_member_union_test(void) {
272 const char *src =
273 "typedef union one_member_union_test_type_u {int x;} one_member_union_test_type;\n"
274 "one_member_union_test_type f(one_member_union_test_type a) {\n"
275 " one_member_union_test_type b;\n"
276 " b.x = a.x * 2;\n"
277 " return b;\n"
278 "}\n";
279 return run_callback(src, one_member_union_test_callback);
283 * two_member_union_test:
285 * In the x86-64 ABI a union should always be passed on the stack.
287 typedef union two_member_union_test_type_u {int x; long y;} two_member_union_test_type;
288 typedef two_member_union_test_type (*two_member_union_test_function_type) (two_member_union_test_type);
290 static int two_member_union_test_callback(void *ptr) {
291 two_member_union_test_function_type f = (two_member_union_test_function_type)ptr;
292 two_member_union_test_type a, b;
293 a.x = 34;
294 b = f(a);
295 return ((b.x == a.x*2) && (f(a).x == a.x*2)) ? 0 : -1;
298 static int two_member_union_test(void) {
299 const char *src =
300 "typedef union two_member_union_test_type_u {int x; long y;} two_member_union_test_type;\n"
301 "two_member_union_test_type f(two_member_union_test_type a) {\n"
302 " two_member_union_test_type b;\n"
303 " b.x = a.x * 2;\n"
304 " return b;\n"
305 "}\n";
306 return run_callback(src, two_member_union_test_callback);
310 * Win64 calling convetntion test.
313 typedef struct many_struct_test_type_s {long long a, b, c;} many_struct_test_type;
314 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);
316 static int many_struct_test_callback(void *ptr) {
317 many_struct_test_function_type f = (many_struct_test_function_type)ptr;
318 many_struct_test_type v = {1, 2, 3};
319 many_struct_test_type r = f(v,v,v,v,v,v);
320 return ((r.a == 6) && (r.b == 12) && (r.c == 18))?0:-1;
323 static int many_struct_test(void) {
324 const char *src =
325 "typedef struct many_struct_test_type_s {long long a, b, c;} many_struct_test_type;\n"
326 "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"
327 " many_struct_test_type y;\n"
328 " y.a = x1.a + x2.a + x3.a + x4.a + x5.a + x6.a;\n"
329 " y.b = x1.b + x2.b + x3.b + x4.b + x5.b + x6.b;\n"
330 " y.c = x1.c + x2.c + x3.c + x4.c + x5.c + x6.c;\n"
331 " return y;\n"
332 "}\n";
333 return run_callback(src, many_struct_test_callback);
337 * Win64 calling convention test.
340 typedef struct many_struct_test_2_type_s {int a, b;} many_struct_test_2_type;
341 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);
343 static int many_struct_test_2_callback(void *ptr) {
344 many_struct_test_2_function_type f = (many_struct_test_2_function_type)ptr;
345 many_struct_test_2_type v = {1,2};
346 many_struct_test_2_type r = f(v,v,v,v,v,v);
347 return ((r.a == 6) && (r.b == 12))?0:-1;
350 static int many_struct_test_2(void) {
351 const char *src =
352 "typedef struct many_struct_test_2_type_s {int a, b;} many_struct_test_2_type;\n"
353 "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"
354 " many_struct_test_2_type y;\n"
355 " y.a = x1.a + x2.a + x3.a + x4.a + x5.a + x6.a;\n"
356 " y.b = x1.b + x2.b + x3.b + x4.b + x5.b + x6.b;\n"
357 " return y;\n"
358 "}\n";
359 return run_callback(src, many_struct_test_2_callback);
363 * stdarg_test: Test variable argument list ABI
366 typedef struct {long long a, b, c;} stdarg_test_struct_type;
367 typedef void (*stdarg_test_function_type) (int,int,int,...);
369 static int stdarg_test_callback(void *ptr) {
370 stdarg_test_function_type f = (stdarg_test_function_type)ptr;
371 int x;
372 double y;
373 stdarg_test_struct_type z = {1, 2, 3}, w;
374 f(10, 10, 5,
375 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, &x,
376 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, &y,
377 z, z, z, z, z, &w);
378 return ((x == 55) && (y == 55) && (w.a == 5) && (w.b == 10) && (w.c == 15)) ? 0 : -1;
381 static int stdarg_test(void) {
382 const char *src =
383 "#include <stdarg.h>\n"
384 "typedef struct {long long a, b, c;} stdarg_test_struct_type;\n"
385 "void f(int n_int, int n_float, int n_struct, ...) {\n"
386 " int i, ti = 0;\n"
387 " double td = 0.0;\n"
388 " stdarg_test_struct_type ts = {0,0,0}, tmp;\n"
389 " va_list ap;\n"
390 " va_start(ap, n_struct);\n"
391 " for (i = 0, ti = 0; i < n_int; ++i)\n"
392 " ti += va_arg(ap, int);\n"
393 " *va_arg(ap, int*) = ti;\n"
394 " for (i = 0, td = 0; i < n_float; ++i)\n"
395 " td += va_arg(ap, double);\n"
396 " *va_arg(ap, double*) = td;\n"
397 " for (i = 0; i < n_struct; ++i) {\n"
398 " tmp = va_arg(ap, stdarg_test_struct_type);\n"
399 " ts.a += tmp.a; ts.b += tmp.b; ts.c += tmp.c;"
400 " }\n"
401 " *va_arg(ap, stdarg_test_struct_type*) = ts;\n"
402 " va_end(ap);"
403 "}\n";
404 return run_callback(src, stdarg_test_callback);
408 * Test Win32 stdarg handling, since the calling convention will pass a pointer
409 * to the struct and the stdarg pointer must point to that pointer initially.
412 typedef struct {long long a, b, c;} stdarg_struct_test_struct_type;
413 typedef int (*stdarg_struct_test_function_type) (stdarg_struct_test_struct_type a, ...);
415 static int stdarg_struct_test_callback(void *ptr) {
416 stdarg_struct_test_function_type f = (stdarg_struct_test_function_type)ptr;
417 stdarg_struct_test_struct_type v = {10, 35, 99};
418 int x = f(v, 234);
419 return (x == 378) ? 0 : -1;
422 static int stdarg_struct_test(void) {
423 const char *src =
424 "#include <stdarg.h>\n"
425 "typedef struct {long long a, b, c;} stdarg_struct_test_struct_type;\n"
426 "int f(stdarg_struct_test_struct_type a, ...) {\n"
427 " va_list ap;\n"
428 " va_start(ap, a);\n"
429 " int z = va_arg(ap, int);\n"
430 " va_end(ap);\n"
431 " return z + a.a + a.b + a.c;\n"
432 "}\n";
433 return run_callback(src, stdarg_struct_test_callback);
436 /* Test that x86-64 arranges the stack correctly for arguments with alignment >8 bytes */
438 typedef LONG_DOUBLE (*arg_align_test_callback_type) (LONG_DOUBLE,int,LONG_DOUBLE,int,LONG_DOUBLE);
440 static int arg_align_test_callback(void *ptr) {
441 arg_align_test_callback_type f = (arg_align_test_callback_type)ptr;
442 long double x = f(12, 0, 25, 0, 37);
443 return (x == 74) ? 0 : -1;
446 static int arg_align_test(void) {
447 const char *src =
448 "long double f(long double a, int b, long double c, int d, long double e) {\n"
449 " return a + c + e;\n"
450 "}\n";
451 return run_callback(src, arg_align_test_callback);
454 #define RUN_TEST(t) \
455 if (!testname || (strcmp(#t, testname) == 0)) { \
456 fputs(#t "... ", stdout); \
457 fflush(stdout); \
458 if (t() == 0) { \
459 fputs("success\n", stdout); \
460 } else { \
461 fputs("failure\n", stdout); \
462 retval = EXIT_FAILURE; \
466 int main(int argc, char **argv) {
467 int i;
468 const char *testname = NULL;
469 int retval = EXIT_SUCCESS;
471 /* if tcclib.h and libcrt.a are not installed, where can we find them */
472 for (i = 1; i < argc; ++i) {
473 if (!memcmp(argv[i], "lib_path=",9))
474 tccdir = argv[i] + 9;
475 else if (!memcmp(argv[i], "run_test=", 9))
476 testname = argv[i] + 9;
477 else if (!memcmp(argv[i], "include=", 8))
478 include_dir = argv[i] + 8;
481 RUN_TEST(ret_int_test);
482 RUN_TEST(ret_longlong_test);
483 RUN_TEST(ret_float_test);
484 RUN_TEST(ret_double_test);
485 RUN_TEST(ret_longdouble_test);
486 RUN_TEST(ret_2float_test);
487 RUN_TEST(ret_2double_test);
488 RUN_TEST(ret_longlong_test2);
489 RUN_TEST(ret_longdouble_test2);
490 RUN_TEST(reg_pack_test);
491 RUN_TEST(reg_pack_longlong_test);
492 RUN_TEST(sret_test);
493 RUN_TEST(one_member_union_test);
494 RUN_TEST(two_member_union_test);
495 RUN_TEST(many_struct_test);
496 RUN_TEST(many_struct_test_2);
497 RUN_TEST(stdarg_test);
498 RUN_TEST(stdarg_struct_test);
499 RUN_TEST(arg_align_test);
500 return retval;