Fix signed integer division in ARM runtime ABI
[tinycc.git] / tests / abitest.c
blobd3e151f22fa34b48a4ffd3b6e5ddf2bd7c54931a
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 * reg_pack_test: return a small struct which should be packed into
135 * registers (Win32) during return.
137 typedef struct reg_pack_test_type_s {int x, y;} reg_pack_test_type;
138 typedef reg_pack_test_type (*reg_pack_test_function_type) (reg_pack_test_type);
140 static int reg_pack_test_callback(void *ptr) {
141 reg_pack_test_function_type f = (reg_pack_test_function_type)ptr;
142 reg_pack_test_type a = {10, 35};
143 reg_pack_test_type r;
144 r = f(a);
145 return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
148 static int reg_pack_test(void) {
149 const char *src =
150 "typedef struct reg_pack_test_type_s {int x, y;} reg_pack_test_type;"
151 "reg_pack_test_type f(reg_pack_test_type a) {\n"
152 " reg_pack_test_type r = {a.x*5, a.y*3};\n"
153 " return r;\n"
154 "}\n";
156 return run_callback(src, reg_pack_test_callback);
160 * reg_pack_longlong_test: return a small struct which should be packed into
161 * registers (x86-64) during return.
163 typedef struct reg_pack_longlong_test_type_s {long long x, y;} reg_pack_longlong_test_type;
164 typedef reg_pack_longlong_test_type (*reg_pack_longlong_test_function_type) (reg_pack_longlong_test_type);
166 static int reg_pack_longlong_test_callback(void *ptr) {
167 reg_pack_longlong_test_function_type f = (reg_pack_longlong_test_function_type)ptr;
168 reg_pack_longlong_test_type a = {10, 35};
169 reg_pack_longlong_test_type r;
170 r = f(a);
171 return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
174 static int reg_pack_longlong_test(void) {
175 const char *src =
176 "typedef struct reg_pack_longlong_test_type_s {long long x, y;} reg_pack_longlong_test_type;"
177 "reg_pack_longlong_test_type f(reg_pack_longlong_test_type a) {\n"
178 " reg_pack_longlong_test_type r = {a.x*5, a.y*3};\n"
179 " return r;\n"
180 "}\n";
182 return run_callback(src, reg_pack_longlong_test_callback);
186 * sret_test: Create a struct large enough to be returned via sret
187 * (hidden pointer as first function argument)
189 typedef struct sret_test_type_s {long long a, b, c;} sret_test_type;
190 typedef sret_test_type (*sret_test_function_type) (sret_test_type);
192 static int sret_test_callback(void *ptr) {
193 sret_test_function_type f = (sret_test_function_type)(ptr);
194 sret_test_type x = {5436LL, 658277698LL, 43878957LL};
195 sret_test_type r = f(x);
196 return ((r.a==x.a*35)&&(r.b==x.b*19)&&(r.c==x.c*21)) ? 0 : -1;
199 static int sret_test(void) {
200 const char *src =
201 "typedef struct sret_test_type_s {long long a, b, c;} sret_test_type;\n"
202 "sret_test_type f(sret_test_type x) {\n"
203 " sret_test_type r = {x.a*35, x.b*19, x.c*21};\n"
204 " return r;\n"
205 "}\n";
207 return run_callback(src, sret_test_callback);
211 * one_member_union_test:
213 * In the x86-64 ABI a union should always be passed on the stack. However
214 * it appears that a single member union is treated by GCC as its member.
216 typedef union one_member_union_test_type_u {int x;} one_member_union_test_type;
217 typedef one_member_union_test_type (*one_member_union_test_function_type) (one_member_union_test_type);
219 static int one_member_union_test_callback(void *ptr) {
220 one_member_union_test_function_type f = (one_member_union_test_function_type)ptr;
221 one_member_union_test_type a, b;
222 a.x = 34;
223 b = f(a);
224 return (b.x == a.x*2) ? 0 : -1;
227 static int one_member_union_test(void) {
228 const char *src =
229 "typedef union one_member_union_test_type_u {int x;} one_member_union_test_type;\n"
230 "one_member_union_test_type f(one_member_union_test_type a) {\n"
231 " one_member_union_test_type b;\n"
232 " b.x = a.x * 2;\n"
233 " return b;\n"
234 "}\n";
235 return run_callback(src, one_member_union_test_callback);
239 * two_member_union_test:
241 * In the x86-64 ABI a union should always be passed on the stack.
243 typedef union two_member_union_test_type_u {int x; long y;} two_member_union_test_type;
244 typedef two_member_union_test_type (*two_member_union_test_function_type) (two_member_union_test_type);
246 static int two_member_union_test_callback(void *ptr) {
247 two_member_union_test_function_type f = (two_member_union_test_function_type)ptr;
248 two_member_union_test_type a, b;
249 a.x = 34;
250 b = f(a);
251 return (b.x == a.x*2) ? 0 : -1;
254 static int two_member_union_test(void) {
255 const char *src =
256 "typedef union two_member_union_test_type_u {int x; long y;} two_member_union_test_type;\n"
257 "two_member_union_test_type f(two_member_union_test_type a) {\n"
258 " two_member_union_test_type b;\n"
259 " b.x = a.x * 2;\n"
260 " return b;\n"
261 "}\n";
262 return run_callback(src, two_member_union_test_callback);
266 * Win64 calling convetntion test.
269 typedef struct many_struct_test_type_s {long long a, b, c;} many_struct_test_type;
270 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);
272 static int many_struct_test_callback(void *ptr) {
273 many_struct_test_function_type f = (many_struct_test_function_type)ptr;
274 many_struct_test_type v = {1, 2, 3};
275 many_struct_test_type r = f(v,v,v,v,v,v);
276 return ((r.a == 6) && (r.b == 12) && (r.c == 18))?0:-1;
279 static int many_struct_test(void) {
280 const char *src =
281 "typedef struct many_struct_test_type_s {long long a, b, c;} many_struct_test_type;\n"
282 "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"
283 " many_struct_test_type y;\n"
284 " y.a = x1.a + x2.a + x3.a + x4.a + x5.a + x6.a;\n"
285 " y.b = x1.b + x2.b + x3.b + x4.b + x5.b + x6.b;\n"
286 " y.c = x1.c + x2.c + x3.c + x4.c + x5.c + x6.c;\n"
287 " return y;\n"
288 "}\n";
289 return run_callback(src, many_struct_test_callback);
293 * Win64 calling convention test.
296 typedef struct many_struct_test_2_type_s {int a, b;} many_struct_test_2_type;
297 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);
299 static int many_struct_test_2_callback(void *ptr) {
300 many_struct_test_2_function_type f = (many_struct_test_2_function_type)ptr;
301 many_struct_test_2_type v = {1,2};
302 many_struct_test_2_type r = f(v,v,v,v,v,v);
303 return ((r.a == 6) && (r.b == 12))?0:-1;
306 static int many_struct_test_2(void) {
307 const char *src =
308 "typedef struct many_struct_test_2_type_s {int a, b;} many_struct_test_2_type;\n"
309 "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"
310 " many_struct_test_2_type y;\n"
311 " y.a = x1.a + x2.a + x3.a + x4.a + x5.a + x6.a;\n"
312 " y.b = x1.b + x2.b + x3.b + x4.b + x5.b + x6.b;\n"
313 " return y;\n"
314 "}\n";
315 return run_callback(src, many_struct_test_2_callback);
319 * stdarg_test: Test variable argument list ABI
322 typedef struct {long long a, b, c;} stdarg_test_struct_type;
323 typedef void (*stdarg_test_function_type) (int,int,int,...);
325 static int stdarg_test_callback(void *ptr) {
326 stdarg_test_function_type f = (stdarg_test_function_type)ptr;
327 int x;
328 double y;
329 stdarg_test_struct_type z = {1, 2, 3}, w;
330 f(10, 10, 5,
331 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, &x,
332 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, &y,
333 z, z, z, z, z, &w);
334 return ((x == 55) && (y == 55) && (w.a == 5) && (w.b == 10) && (w.c == 15)) ? 0 : -1;
337 static int stdarg_test(void) {
338 const char *src =
339 "#include <stdarg.h>\n"
340 "typedef struct {long long a, b, c;} stdarg_test_struct_type;\n"
341 "void f(int n_int, int n_float, int n_struct, ...) {\n"
342 " int i, ti = 0;\n"
343 " double td = 0.0;\n"
344 " stdarg_test_struct_type ts = {0,0,0}, tmp;\n"
345 " va_list ap;\n"
346 " va_start(ap, n_struct);\n"
347 " for (i = 0, ti = 0; i < n_int; ++i)\n"
348 " ti += va_arg(ap, int);\n"
349 " *va_arg(ap, int*) = ti;\n"
350 " for (i = 0, td = 0; i < n_float; ++i)\n"
351 " td += va_arg(ap, double);\n"
352 " *va_arg(ap, double*) = td;\n"
353 " for (i = 0; i < n_struct; ++i) {\n"
354 " tmp = va_arg(ap, stdarg_test_struct_type);\n"
355 " ts.a += tmp.a; ts.b += tmp.b; ts.c += tmp.c;"
356 " }\n"
357 " *va_arg(ap, stdarg_test_struct_type*) = ts;\n"
358 " va_end(ap);"
359 "}\n";
360 return run_callback(src, stdarg_test_callback);
364 * Test Win32 stdarg handling, since the calling convention will pass a pointer
365 * to the struct and the stdarg pointer must point to that pointer initially.
368 typedef struct {long long a, b, c;} stdarg_struct_test_struct_type;
369 typedef int (*stdarg_struct_test_function_type) (stdarg_struct_test_struct_type a, ...);
371 static int stdarg_struct_test_callback(void *ptr) {
372 stdarg_struct_test_function_type f = (stdarg_struct_test_function_type)ptr;
373 stdarg_struct_test_struct_type v = {10, 35, 99};
374 int x = f(v, 234);
375 return (x == 378) ? 0 : -1;
378 static int stdarg_struct_test(void) {
379 const char *src =
380 "#include <stdarg.h>\n"
381 "typedef struct {long long a, b, c;} stdarg_struct_test_struct_type;\n"
382 "int f(stdarg_struct_test_struct_type a, ...) {\n"
383 " va_list ap;\n"
384 " va_start(ap, a);\n"
385 " int z = va_arg(ap, int);\n"
386 " va_end(ap);\n"
387 " return z + a.a + a.b + a.c;\n"
388 "}\n";
389 return run_callback(src, stdarg_struct_test_callback);
392 /* Test that x86-64 arranges the stack correctly for arguments with alignment >8 bytes */
394 typedef LONG_DOUBLE (*arg_align_test_callback_type) (LONG_DOUBLE,int,LONG_DOUBLE,int,LONG_DOUBLE);
396 static int arg_align_test_callback(void *ptr) {
397 arg_align_test_callback_type f = (arg_align_test_callback_type)ptr;
398 long double x = f(12, 0, 25, 0, 37);
399 return (x == 74) ? 0 : -1;
402 static int arg_align_test(void) {
403 const char *src =
404 "long double f(long double a, int b, long double c, int d, long double e) {\n"
405 " return a + c + e;\n"
406 "}\n";
407 return run_callback(src, arg_align_test_callback);
410 #define RUN_TEST(t) \
411 if (!testname || (strcmp(#t, testname) == 0)) { \
412 fputs(#t "... ", stdout); \
413 fflush(stdout); \
414 if (t() == 0) { \
415 fputs("success\n", stdout); \
416 } else { \
417 fputs("failure\n", stdout); \
418 retval = EXIT_FAILURE; \
422 int main(int argc, char **argv) {
423 int i;
424 const char *testname = NULL;
425 int retval = EXIT_SUCCESS;
427 /* if tcclib.h and libtcc1.a are not installed, where can we find them */
428 for (i = 1; i < argc; ++i) {
429 if (!memcmp(argv[i], "lib_path=",9))
430 tccdir = argv[i] + 9;
431 else if (!memcmp(argv[i], "run_test=", 9))
432 testname = argv[i] + 9;
433 else if (!memcmp(argv[i], "include=", 8))
434 include_dir = argv[i] + 8;
437 RUN_TEST(ret_int_test);
438 RUN_TEST(ret_longlong_test);
439 RUN_TEST(ret_float_test);
440 RUN_TEST(ret_double_test);
441 RUN_TEST(ret_longdouble_test);
442 RUN_TEST(ret_2float_test);
443 RUN_TEST(ret_2double_test);
444 RUN_TEST(reg_pack_test);
445 RUN_TEST(reg_pack_longlong_test);
446 RUN_TEST(sret_test);
447 RUN_TEST(one_member_union_test);
448 RUN_TEST(two_member_union_test);
449 RUN_TEST(many_struct_test);
450 RUN_TEST(many_struct_test_2);
451 RUN_TEST(stdarg_test);
452 RUN_TEST(stdarg_struct_test);
453 RUN_TEST(arg_align_test);
454 return retval;