6 static const char *tccdir
= NULL
;
8 typedef int (*callback_type
) (void*);
11 * Compile source code and call a callback with a pointer to the symbol "f".
13 static int run_callback(const char *src
, callback_type callback
) {
22 tcc_set_lib_path(s
, tccdir
);
23 if (tcc_set_output_type(s
, TCC_OUTPUT_MEMORY
) == -1)
25 if (tcc_compile_string(s
, src
) == -1)
27 if (tcc_relocate(s
, TCC_RELOCATE_AUTO
) == -1)
30 ptr
= tcc_get_symbol(s
, "f");
33 result
= callback(ptr
);
41 * reg_pack_test: return a small struct which should be packed into
42 * registers (Win32) during return.
44 typedef struct reg_pack_test_type_s
{int x
, y
;} reg_pack_test_type
;
45 typedef reg_pack_test_type (*reg_pack_test_function_type
) (reg_pack_test_type
);
47 static int reg_pack_test_callback(void *ptr
) {
48 reg_pack_test_function_type f
= (reg_pack_test_function_type
)ptr
;
49 reg_pack_test_type a
= {10, 35};
52 return ((r
.x
== a
.x
*5) && (r
.y
== a
.y
*3)) ? 0 : -1;
55 static int reg_pack_test(void) {
57 "typedef struct reg_pack_test_type_s {int x, y;} reg_pack_test_type;"
58 "reg_pack_test_type f(reg_pack_test_type a) {\n"
59 " reg_pack_test_type r = {a.x*5, a.y*3};\n"
63 return run_callback(src
, reg_pack_test_callback
);
67 * sret_test: Create a struct large enough to be returned via sret
68 * (hidden pointer as first function argument)
70 typedef struct sret_test_type_s
{
75 typedef sret_test_type (*sret_test_function_type
) (sret_test_type
);
77 static int sret_test_callback(void *ptr
) {
78 sret_test_function_type f
= (sret_test_function_type
)(ptr
);
79 sret_test_type x
= {5436LL, 658277698LL};
80 sret_test_type r
= f(x
);
81 return ((r
.a
==x
.a
*35)&&(r
.b
==x
.b
*19)) ? 0 : -1;
84 static int sret_test(void) {
86 "typedef struct sret_test_type_s {\n"
90 "sret_test_type f(sret_test_type x) {\n"
91 " sret_test_type r = {x.a*35, x.b*19};\n"
95 return run_callback(src
, sret_test_callback
);
100 fputs(#t "... ", stdout); \
103 fputs("success\n", stdout); \
105 fputs("failure\n", stdout); \
106 retval = EXIT_FAILURE; \
110 int main(int argc
, char **argv
) {
111 int retval
= EXIT_SUCCESS
;
112 /* if tcclib.h and libtcc1.a are not installed, where can we find them */
113 if (argc
== 2 && !memcmp(argv
[1], "lib_path=",9))
114 tccdir
= argv
[1] + 9;
116 RUN_TEST(reg_pack_test
);