Fixed 64-bit integer bug introduced by x86-64 ABI work.
[tinycc.git] / tests / abitest.c
blob7e3d34a857734dcadd38425a3b9ca46a23d7ddd1
1 #include <libtcc.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdarg.h>
7 static const char *tccdir = NULL;
9 typedef int (*callback_type) (void*);
12 * Compile source code and call a callback with a pointer to the symbol "f".
14 static int run_callback(const char *src, callback_type callback) {
15 TCCState *s;
16 int result;
17 void *ptr;
19 s = tcc_new();
20 if (!s)
21 return -1;
22 if (tccdir)
23 tcc_set_lib_path(s, tccdir);
24 if (tcc_set_output_type(s, TCC_OUTPUT_MEMORY) == -1)
25 return -1;
26 if (tcc_compile_string(s, src) == -1)
27 return -1;
28 if (tcc_relocate(s, TCC_RELOCATE_AUTO) == -1)
29 return -1;
31 ptr = tcc_get_symbol(s, "f");
32 if (!ptr)
33 return -1;
34 result = callback(ptr);
36 tcc_delete(s);
38 return result;
41 #define RET_PRIMITIVE_TEST(name, type, val) \
42 static int ret_ ## name ## _test_callback(void *ptr) { \
43 type (*callback) (type) = (type(*)(type))ptr; \
44 type x = val; \
45 type y = callback(x); \
46 return (y == x+x) ? 0 : -1; \
47 } \
49 static int ret_ ## name ## _test(void) { \
50 const char *src = #type " f(" #type " x) {return x+x;}"; \
51 return run_callback(src, ret_ ## name ## _test_callback); \
54 RET_PRIMITIVE_TEST(int, int, 70000)
55 RET_PRIMITIVE_TEST(longlong, long long, 4333369356528LL)
56 RET_PRIMITIVE_TEST(float, float, 63.0)
57 RET_PRIMITIVE_TEST(double, double, 14789798.0)
58 RET_PRIMITIVE_TEST(longdouble, long double, 378943892.0)
61 * ret_2float_test:
63 * On x86-64, a struct with 2 floats should be packed into a single
64 * SSE register (VT_DOUBLE is used for this purpose).
66 typedef struct ret_2float_test_type_s {float x, y;} ret_2float_test_type;
67 typedef ret_2float_test_type (*ret_2float_test_function_type) (ret_2float_test_type);
69 static int ret_2float_test_callback(void *ptr) {
70 ret_2float_test_function_type f = (ret_2float_test_function_type)ptr;
71 ret_2float_test_type a = {10, 35};
72 ret_2float_test_type r;
73 r = f(a);
74 return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
77 static int ret_2float_test(void) {
78 const char *src =
79 "typedef struct ret_2float_test_type_s {float x, y;} ret_2float_test_type;"
80 "ret_2float_test_type f(ret_2float_test_type a) {\n"
81 " ret_2float_test_type r = {a.x*5, a.y*3};\n"
82 " return r;\n"
83 "}\n";
85 return run_callback(src, ret_2float_test_callback);
89 * ret_2double_test:
91 * On x86-64, a struct with 2 doubles should be passed in two SSE
92 * registers.
94 typedef struct ret_2double_test_type_s {double x, y;} ret_2double_test_type;
95 typedef ret_2double_test_type (*ret_2double_test_function_type) (ret_2double_test_type);
97 static int ret_2double_test_callback(void *ptr) {
98 ret_2double_test_function_type f = (ret_2double_test_function_type)ptr;
99 ret_2double_test_type a = {10, 35};
100 ret_2double_test_type r;
101 r = f(a);
102 return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
105 static int ret_2double_test(void) {
106 const char *src =
107 "typedef struct ret_2double_test_type_s {double x, y;} ret_2double_test_type;"
108 "ret_2double_test_type f(ret_2double_test_type a) {\n"
109 " ret_2double_test_type r = {a.x*5, a.y*3};\n"
110 " return r;\n"
111 "}\n";
113 return run_callback(src, ret_2double_test_callback);
117 * reg_pack_test: return a small struct which should be packed into
118 * registers (Win32) during return.
120 typedef struct reg_pack_test_type_s {int x, y;} reg_pack_test_type;
121 typedef reg_pack_test_type (*reg_pack_test_function_type) (reg_pack_test_type);
123 static int reg_pack_test_callback(void *ptr) {
124 reg_pack_test_function_type f = (reg_pack_test_function_type)ptr;
125 reg_pack_test_type a = {10, 35};
126 reg_pack_test_type r;
127 r = f(a);
128 return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
131 static int reg_pack_test(void) {
132 const char *src =
133 "typedef struct reg_pack_test_type_s {int x, y;} reg_pack_test_type;"
134 "reg_pack_test_type f(reg_pack_test_type a) {\n"
135 " reg_pack_test_type r = {a.x*5, a.y*3};\n"
136 " return r;\n"
137 "}\n";
139 return run_callback(src, reg_pack_test_callback);
143 * reg_pack_longlong_test: return a small struct which should be packed into
144 * registers (x86-64) during return.
146 typedef struct reg_pack_longlong_test_type_s {long long x, y;} reg_pack_longlong_test_type;
147 typedef reg_pack_longlong_test_type (*reg_pack_longlong_test_function_type) (reg_pack_longlong_test_type);
149 static int reg_pack_longlong_test_callback(void *ptr) {
150 reg_pack_longlong_test_function_type f = (reg_pack_longlong_test_function_type)ptr;
151 reg_pack_longlong_test_type a = {10, 35};
152 reg_pack_longlong_test_type r;
153 r = f(a);
154 return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
157 static int reg_pack_longlong_test(void) {
158 const char *src =
159 "typedef struct reg_pack_longlong_test_type_s {long long x, y;} reg_pack_longlong_test_type;"
160 "reg_pack_longlong_test_type f(reg_pack_longlong_test_type a) {\n"
161 " reg_pack_longlong_test_type r = {a.x*5, a.y*3};\n"
162 " return r;\n"
163 "}\n";
165 return run_callback(src, reg_pack_longlong_test_callback);
169 * sret_test: Create a struct large enough to be returned via sret
170 * (hidden pointer as first function argument)
172 typedef struct sret_test_type_s {long long a, b, c;} sret_test_type;
173 typedef sret_test_type (*sret_test_function_type) (sret_test_type);
175 static int sret_test_callback(void *ptr) {
176 sret_test_function_type f = (sret_test_function_type)(ptr);
177 sret_test_type x = {5436LL, 658277698LL, 43878957LL};
178 sret_test_type r = f(x);
179 return ((r.a==x.a*35)&&(r.b==x.b*19)&&(r.c==x.c*21)) ? 0 : -1;
182 static int sret_test(void) {
183 const char *src =
184 "typedef struct sret_test_type_s {long long a, b, c;} sret_test_type;\n"
185 "sret_test_type f(sret_test_type x) {\n"
186 " sret_test_type r = {x.a*35, x.b*19, x.c*21};\n"
187 " return r;\n"
188 "}\n";
190 return run_callback(src, sret_test_callback);
194 * one_member_union_test:
196 * In the x86-64 ABI a union should always be passed on the stack. However
197 * it appears that a single member union is treated by GCC as its member.
199 typedef union one_member_union_test_type_u {int x;} one_member_union_test_type;
200 typedef one_member_union_test_type (*one_member_union_test_function_type) (one_member_union_test_type);
202 static int one_member_union_test_callback(void *ptr) {
203 one_member_union_test_function_type f = (one_member_union_test_function_type)ptr;
204 one_member_union_test_type a, b;
205 a.x = 34;
206 b = f(a);
207 return (b.x == a.x*2) ? 0 : -1;
210 static int one_member_union_test(void) {
211 const char *src =
212 "typedef union one_member_union_test_type_u {int x;} one_member_union_test_type;\n"
213 "one_member_union_test_type f(one_member_union_test_type a) {\n"
214 " one_member_union_test_type b;\n"
215 " b.x = a.x * 2;\n"
216 " return b;\n"
217 "}\n";
218 return run_callback(src, one_member_union_test_callback);
222 * two_member_union_test:
224 * In the x86-64 ABI a union should always be passed on the stack.
226 typedef union two_member_union_test_type_u {int x; long y;} two_member_union_test_type;
227 typedef two_member_union_test_type (*two_member_union_test_function_type) (two_member_union_test_type);
229 static int two_member_union_test_callback(void *ptr) {
230 two_member_union_test_function_type f = (two_member_union_test_function_type)ptr;
231 two_member_union_test_type a, b;
232 a.x = 34;
233 b = f(a);
234 return (b.x == a.x*2) ? 0 : -1;
237 static int two_member_union_test(void) {
238 const char *src =
239 "typedef union two_member_union_test_type_u {int x; long y;} two_member_union_test_type;\n"
240 "two_member_union_test_type f(two_member_union_test_type a) {\n"
241 " two_member_union_test_type b;\n"
242 " b.x = a.x * 2;\n"
243 " return b;\n"
244 "}\n";
245 return run_callback(src, two_member_union_test_callback);
249 * stdarg_test: Test variable argument list ABI
252 typedef void (*stdarg_test_function_type) (int,int,...);
254 static int stdarg_test_callback(void *ptr) {
255 stdarg_test_function_type f = (stdarg_test_function_type)ptr;
256 int x;
257 double y;
258 f(10, 10,
259 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, &x,
260 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, &y);
261 return ((x == 55) && (y == 55)) ? 0 : -1;
264 static int stdarg_test(void) {
265 const char *src =
266 "#include <stdarg.h>\n"
267 "void f(int n_int, int n_float, ...) {\n"
268 " int i, ti;\n"
269 " double td;\n"
270 " va_list ap;\n"
271 " va_start(ap, n_float);\n"
272 " for (i = 0, ti = 0; i < n_int; ++i)\n"
273 " ti += va_arg(ap, int);\n"
274 " *va_arg(ap, int*) = ti;\n"
275 " for (i = 0, td = 0; i < n_float; ++i)\n"
276 " td += va_arg(ap, double);\n"
277 " *va_arg(ap, double*) = td;\n"
278 " va_end(ap);"
279 "}\n";
280 return run_callback(src, stdarg_test_callback);
283 #define RUN_TEST(t) \
284 if (!testname || (strcmp(#t, testname) == 0)) { \
285 fputs(#t "... ", stdout); \
286 fflush(stdout); \
287 if (t() == 0) { \
288 fputs("success\n", stdout); \
289 } else { \
290 fputs("failure\n", stdout); \
291 retval = EXIT_FAILURE; \
295 int main(int argc, char **argv) {
296 int i;
297 const char *testname = NULL;
298 int retval = EXIT_SUCCESS;
300 /* if tcclib.h and libtcc1.a are not installed, where can we find them */
301 for (i = 1; i < argc; ++i) {
302 if (!memcmp(argv[i], "lib_path=",9))
303 tccdir = argv[i] + 9;
304 else if (!memcmp(argv[i], "run_test=", 9))
305 testname = argv[i] + 9;
308 RUN_TEST(ret_int_test);
309 RUN_TEST(ret_longlong_test);
310 RUN_TEST(ret_float_test);
311 RUN_TEST(ret_double_test);
312 RUN_TEST(ret_longdouble_test);
313 RUN_TEST(ret_2float_test);
314 RUN_TEST(ret_2double_test);
315 RUN_TEST(reg_pack_test);
316 RUN_TEST(reg_pack_longlong_test);
317 RUN_TEST(sret_test);
318 RUN_TEST(one_member_union_test);
319 RUN_TEST(two_member_union_test);
320 RUN_TEST(stdarg_test);
321 return retval;