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
12 #define LONG_DOUBLE long double
13 #define LONG_DOUBLE_LITERAL(x) x ## L
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
) {
33 tcc_set_lib_path(s
, tccdir
);
35 if (tcc_add_include_path(s
, include_dir
) == -1)
38 if (tcc_set_output_type(s
, TCC_OUTPUT_MEMORY
) == -1)
40 if (tcc_compile_string(s
, src
) == -1)
42 if (tcc_relocate(s
, TCC_RELOCATE_AUTO
) == -1)
45 ptr
= tcc_get_symbol(s
, "f");
48 result
= callback(ptr
);
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; \
62 type y = callback(x); \
63 return (y == x+x) ? 0 : -1; \
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))
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
;
91 return ((r
.x
== a
.x
*5) && (r
.y
== a
.y
*3)) ? 0 : -1;
94 static int ret_2float_test(void) {
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"
102 return run_callback(src
, ret_2float_test_callback
);
108 * On x86-64, a struct with 2 doubles should be passed in two SSE
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
;
119 return ((r
.x
== a
.x
*5) && (r
.y
== a
.y
*3)) ? 0 : -1;
122 static int ret_2double_test(void) {
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"
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
;
145 return ((r
.x
== a
.x
*5) && (r
.y
== a
.y
*3)) ? 0 : -1;
148 static int reg_pack_test(void) {
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"
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
;
171 return ((r
.x
== a
.x
*5) && (r
.y
== a
.y
*3)) ? 0 : -1;
174 static int reg_pack_longlong_test(void) {
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"
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) {
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"
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
;
224 return (b
.x
== a
.x
*2) ? 0 : -1;
227 static int one_member_union_test(void) {
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"
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
;
251 return (b
.x
== a
.x
*2) ? 0 : -1;
254 static int two_member_union_test(void) {
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"
262 return run_callback(src
, two_member_union_test_callback
);
266 * stdarg_test: Test variable argument list ABI
269 typedef void (*stdarg_test_function_type
) (int,int,...);
271 static int stdarg_test_callback(void *ptr
) {
272 stdarg_test_function_type f
= (stdarg_test_function_type
)ptr
;
276 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, &x
,
277 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, &y
);
278 return ((x
== 55) && (y
== 55)) ? 0 : -1;
281 static int stdarg_test(void) {
283 "#include <stdarg.h>\n"
284 "void f(int n_int, int n_float, ...) {\n"
288 " va_start(ap, n_float);\n"
289 " for (i = 0, ti = 0; i < n_int; ++i)\n"
290 " ti += va_arg(ap, int);\n"
291 " *va_arg(ap, int*) = ti;\n"
292 " for (i = 0, td = 0; i < n_float; ++i)\n"
293 " td += va_arg(ap, double);\n"
294 " *va_arg(ap, double*) = td;\n"
297 return run_callback(src
, stdarg_test_callback
);
300 #define RUN_TEST(t) \
301 if (!testname || (strcmp(#t, testname) == 0)) { \
302 fputs(#t "... ", stdout); \
305 fputs("success\n", stdout); \
307 fputs("failure\n", stdout); \
308 retval = EXIT_FAILURE; \
312 int main(int argc
, char **argv
) {
314 const char *testname
= NULL
;
315 int retval
= EXIT_SUCCESS
;
317 /* if tcclib.h and libtcc1.a are not installed, where can we find them */
318 for (i
= 1; i
< argc
; ++i
) {
319 if (!memcmp(argv
[i
], "lib_path=",9))
320 tccdir
= argv
[i
] + 9;
321 else if (!memcmp(argv
[i
], "run_test=", 9))
322 testname
= argv
[i
] + 9;
323 else if (!memcmp(argv
[i
], "include=", 8))
324 include_dir
= argv
[i
] + 8;
327 RUN_TEST(ret_int_test
);
328 RUN_TEST(ret_longlong_test
);
329 RUN_TEST(ret_float_test
);
330 RUN_TEST(ret_double_test
);
331 RUN_TEST(ret_longdouble_test
);
332 RUN_TEST(ret_2float_test
);
333 RUN_TEST(ret_2double_test
);
334 RUN_TEST(reg_pack_test
);
335 RUN_TEST(reg_pack_longlong_test
);
337 RUN_TEST(one_member_union_test
);
338 RUN_TEST(two_member_union_test
);
339 RUN_TEST(stdarg_test
);