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
) {
23 tcc_set_lib_path(s
, tccdir
);
24 if (tcc_set_output_type(s
, TCC_OUTPUT_MEMORY
) == -1)
26 if (tcc_compile_string(s
, src
) == -1)
28 if (tcc_relocate(s
, TCC_RELOCATE_AUTO
) == -1)
31 ptr
= tcc_get_symbol(s
, "f");
34 result
= callback(ptr
);
41 #define RET_PRIMITIVE_TEST(name, type, val) \
42 static int ret_ ## name ## _test_callback(void *ptr) { \
43 type (*callback) (type) = (type(*)(type))ptr; \
45 type y = callback(x); \
46 return (y == x+x) ? 0 : -1; \
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)
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
;
74 return ((r
.x
== a
.x
*5) && (r
.y
== a
.y
*3)) ? 0 : -1;
77 static int ret_2float_test(void) {
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"
85 return run_callback(src
, ret_2float_test_callback
);
91 * On x86-64, a struct with 2 doubles should be passed in two SSE
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
;
102 return ((r
.x
== a
.x
*5) && (r
.y
== a
.y
*3)) ? 0 : -1;
105 static int ret_2double_test(void) {
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"
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
;
128 return ((r
.x
== a
.x
*5) && (r
.y
== a
.y
*3)) ? 0 : -1;
131 static int reg_pack_test(void) {
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"
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
;
154 return ((r
.x
== a
.x
*5) && (r
.y
== a
.y
*3)) ? 0 : -1;
157 static int reg_pack_longlong_test(void) {
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"
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) {
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"
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
;
207 return (b
.x
== a
.x
*2) ? 0 : -1;
210 static int one_member_union_test(void) {
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"
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
;
234 return (b
.x
== a
.x
*2) ? 0 : -1;
237 static int two_member_union_test(void) {
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"
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
;
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) {
266 "#include <stdarg.h>\n"
267 "void f(int n_int, int n_float, ...) {\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"
280 return run_callback(src
, stdarg_test_callback
);
283 #define RUN_TEST(t) \
284 if (!testname || (strcmp(#t, testname) == 0)) { \
285 fputs(#t "... ", stdout); \
288 fputs("success\n", stdout); \
290 fputs("failure\n", stdout); \
291 retval = EXIT_FAILURE; \
295 int main(int argc
, char **argv
) {
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
);
318 RUN_TEST(one_member_union_test
);
319 RUN_TEST(two_member_union_test
);
320 RUN_TEST(stdarg_test
);