abitest.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. #include <libtcc.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdarg.h>
  6. // MinGW has 80-bit rather than 64-bit long double which isn't compatible with TCC or MSVC
  7. #if defined(_WIN32) && defined(__GNUC__)
  8. #define LONG_DOUBLE double
  9. #define LONG_DOUBLE_LITERAL(x) x
  10. #else
  11. #define LONG_DOUBLE long double
  12. #define LONG_DOUBLE_LITERAL(x) x ## L
  13. #endif
  14. static int g_argc;
  15. static char **g_argv;
  16. static void set_options(TCCState *s, int argc, char **argv)
  17. {
  18. int i;
  19. for (i = 1; i < argc; ++i) {
  20. char *a = argv[i];
  21. if (a[0] == '-') {
  22. if (a[1] == 'B')
  23. tcc_set_lib_path(s, a+2);
  24. else if (a[1] == 'I')
  25. tcc_add_include_path(s, a+2);
  26. else if (a[1] == 'L')
  27. tcc_add_library_path(s, a+2);
  28. }
  29. }
  30. }
  31. typedef int (*callback_type) (void*);
  32. /*
  33. * Compile source code and call a callback with a pointer to the symbol "f".
  34. */
  35. static int run_callback(const char *src, callback_type callback) {
  36. TCCState *s;
  37. int result;
  38. void *ptr;
  39. s = tcc_new();
  40. if (!s)
  41. return -1;
  42. set_options(s, g_argc, g_argv);
  43. if (tcc_set_output_type(s, TCC_OUTPUT_MEMORY) == -1)
  44. return -1;
  45. if (tcc_compile_string(s, src) == -1)
  46. return -1;
  47. if (tcc_relocate(s, TCC_RELOCATE_AUTO) == -1)
  48. return -1;
  49. ptr = tcc_get_symbol(s, "f");
  50. if (!ptr)
  51. return -1;
  52. result = callback(ptr);
  53. tcc_delete(s);
  54. return result;
  55. }
  56. #define STR2(x) #x
  57. #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. } \
  65. \
  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); \
  69. }
  70. RET_PRIMITIVE_TEST(int, int, 70000)
  71. RET_PRIMITIVE_TEST(longlong, long long, 4333369356528LL)
  72. RET_PRIMITIVE_TEST(float, float, 63.0)
  73. RET_PRIMITIVE_TEST(double, double, 14789798.0)
  74. RET_PRIMITIVE_TEST(longdouble, LONG_DOUBLE, LONG_DOUBLE_LITERAL(378943892.0))
  75. /*
  76. * ret_2float_test:
  77. *
  78. * On x86-64, a struct with 2 floats should be packed into a single
  79. * SSE register (VT_DOUBLE is used for this purpose).
  80. */
  81. typedef struct ret_2float_test_type_s {float x, y;} ret_2float_test_type;
  82. typedef ret_2float_test_type (*ret_2float_test_function_type) (ret_2float_test_type);
  83. static int ret_2float_test_callback(void *ptr) {
  84. ret_2float_test_function_type f = (ret_2float_test_function_type)ptr;
  85. ret_2float_test_type a = {10, 35};
  86. ret_2float_test_type r;
  87. r = f(a);
  88. return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
  89. }
  90. static int ret_2float_test(void) {
  91. const char *src =
  92. "typedef struct ret_2float_test_type_s {float x, y;} ret_2float_test_type;"
  93. "ret_2float_test_type f(ret_2float_test_type a) {\n"
  94. " ret_2float_test_type r = {a.x*5, a.y*3};\n"
  95. " return r;\n"
  96. "}\n";
  97. return run_callback(src, ret_2float_test_callback);
  98. }
  99. /*
  100. * ret_2double_test:
  101. *
  102. * On x86-64, a struct with 2 doubles should be passed in two SSE
  103. * registers.
  104. */
  105. typedef struct ret_2double_test_type_s {double x, y;} ret_2double_test_type;
  106. typedef ret_2double_test_type (*ret_2double_test_function_type) (ret_2double_test_type);
  107. static int ret_2double_test_callback(void *ptr) {
  108. ret_2double_test_function_type f = (ret_2double_test_function_type)ptr;
  109. ret_2double_test_type a = {10, 35};
  110. ret_2double_test_type r;
  111. r = f(a);
  112. return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
  113. }
  114. static int ret_2double_test(void) {
  115. const char *src =
  116. "typedef struct ret_2double_test_type_s {double x, y;} ret_2double_test_type;"
  117. "ret_2double_test_type f(ret_2double_test_type a) {\n"
  118. " ret_2double_test_type r = {a.x*5, a.y*3};\n"
  119. " return r;\n"
  120. "}\n";
  121. return run_callback(src, ret_2double_test_callback);
  122. }
  123. /*
  124. * ret_8plus2double_test:
  125. *
  126. * This catches a corner case in the x86_64 ABI code: the first 7
  127. * arguments fit into registers, the 8th doesn't, but the 9th argument
  128. * fits into the 8th XMM register.
  129. *
  130. * Note that the purpose of the 10th argument is to avoid a situation
  131. * in which gcc would accidentally put the double at the right
  132. * address, thus causing a success message even though TCC actually
  133. * generated incorrect code.
  134. */
  135. typedef ret_2double_test_type (*ret_8plus2double_test_function_type) (double, double, double, double, double, double, double, ret_2double_test_type, double, double);
  136. static int ret_8plus2double_test_callback(void *ptr) {
  137. ret_8plus2double_test_function_type f = (ret_8plus2double_test_function_type)ptr;
  138. ret_2double_test_type a = {10, 35};
  139. ret_2double_test_type r;
  140. r = f(0, 0, 0, 0, 0, 0, 0, a, 37, 38);
  141. return ((r.x == 37) && (r.y == 37)) ? 0 : -1;
  142. }
  143. static int ret_8plus2double_test(void) {
  144. const char *src =
  145. "typedef struct ret_2double_test_type_s {double x, y;} ret_2double_test_type;"
  146. "ret_2double_test_type f(double x1, double x2, double x3, double x4, double x5, double x6, double x7, ret_2double_test_type a, double x8, double x9) {\n"
  147. " ret_2double_test_type r = { x8, x8 };\n"
  148. " return r;\n"
  149. "}\n";
  150. return run_callback(src, ret_8plus2double_test_callback);
  151. }
  152. /*
  153. * ret_mixed_test:
  154. *
  155. * On x86-64, a struct with a double and a 64-bit integer should be
  156. * passed in one SSE register and one integer register.
  157. */
  158. typedef struct ret_mixed_test_type_s {double x; long long y;} ret_mixed_test_type;
  159. typedef ret_mixed_test_type (*ret_mixed_test_function_type) (ret_mixed_test_type);
  160. static int ret_mixed_test_callback(void *ptr) {
  161. ret_mixed_test_function_type f = (ret_mixed_test_function_type)ptr;
  162. ret_mixed_test_type a = {10, 35};
  163. ret_mixed_test_type r;
  164. r = f(a);
  165. return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
  166. }
  167. static int ret_mixed_test(void) {
  168. const char *src =
  169. "typedef struct ret_mixed_test_type_s {double x; long long y;} ret_mixed_test_type;"
  170. "ret_mixed_test_type f(ret_mixed_test_type a) {\n"
  171. " ret_mixed_test_type r = {a.x*5, a.y*3};\n"
  172. " return r;\n"
  173. "}\n";
  174. return run_callback(src, ret_mixed_test_callback);
  175. }
  176. /*
  177. * ret_mixed2_test:
  178. *
  179. * On x86-64, a struct with two floats and two 32-bit integers should
  180. * be passed in one SSE register and one integer register.
  181. */
  182. typedef struct ret_mixed2_test_type_s {float x,x2; int y,y2;} ret_mixed2_test_type;
  183. typedef ret_mixed2_test_type (*ret_mixed2_test_function_type) (ret_mixed2_test_type);
  184. static int ret_mixed2_test_callback(void *ptr) {
  185. ret_mixed2_test_function_type f = (ret_mixed2_test_function_type)ptr;
  186. ret_mixed2_test_type a = {10, 5, 35, 7 };
  187. ret_mixed2_test_type r;
  188. r = f(a);
  189. return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
  190. }
  191. static int ret_mixed2_test(void) {
  192. const char *src =
  193. "typedef struct ret_mixed2_test_type_s {float x, x2; int y,y2;} ret_mixed2_test_type;"
  194. "ret_mixed2_test_type f(ret_mixed2_test_type a) {\n"
  195. " ret_mixed2_test_type r = {a.x*5, 0, a.y*3, 0};\n"
  196. " return r;\n"
  197. "}\n";
  198. return run_callback(src, ret_mixed2_test_callback);
  199. }
  200. /*
  201. * ret_mixed3_test:
  202. *
  203. * On x86-64, this struct should be passed in two integer registers.
  204. */
  205. typedef struct ret_mixed3_test_type_s {float x; int y; float x2; int y2;} ret_mixed3_test_type;
  206. typedef ret_mixed3_test_type (*ret_mixed3_test_function_type) (ret_mixed3_test_type);
  207. static int ret_mixed3_test_callback(void *ptr) {
  208. ret_mixed3_test_function_type f = (ret_mixed3_test_function_type)ptr;
  209. ret_mixed3_test_type a = {10, 5, 35, 7 };
  210. ret_mixed3_test_type r;
  211. r = f(a);
  212. return ((r.x == a.x*5) && (r.y2 == a.y*3)) ? 0 : -1;
  213. }
  214. static int ret_mixed3_test(void) {
  215. const char *src =
  216. "typedef struct ret_mixed3_test_type_s {float x; int y; float x2; int y2;} ret_mixed3_test_type;"
  217. "ret_mixed3_test_type f(ret_mixed3_test_type a) {\n"
  218. " ret_mixed3_test_type r = {a.x*5, 0, 0, a.y*3};\n"
  219. " return r;\n"
  220. "}\n";
  221. return run_callback(src, ret_mixed3_test_callback);
  222. }
  223. /*
  224. * reg_pack_test: return a small struct which should be packed into
  225. * registers (Win32) during return.
  226. */
  227. typedef struct reg_pack_test_type_s {int x, y;} reg_pack_test_type;
  228. typedef reg_pack_test_type (*reg_pack_test_function_type) (reg_pack_test_type);
  229. static int reg_pack_test_callback(void *ptr) {
  230. reg_pack_test_function_type f = (reg_pack_test_function_type)ptr;
  231. reg_pack_test_type a = {10, 35};
  232. reg_pack_test_type r;
  233. r = f(a);
  234. return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
  235. }
  236. static int reg_pack_test(void) {
  237. const char *src =
  238. "typedef struct reg_pack_test_type_s {int x, y;} reg_pack_test_type;"
  239. "reg_pack_test_type f(reg_pack_test_type a) {\n"
  240. " reg_pack_test_type r = {a.x*5, a.y*3};\n"
  241. " return r;\n"
  242. "}\n";
  243. return run_callback(src, reg_pack_test_callback);
  244. }
  245. /*
  246. * reg_pack_longlong_test: return a small struct which should be packed into
  247. * registers (x86-64) during return.
  248. */
  249. typedef struct reg_pack_longlong_test_type_s {long long x, y;} reg_pack_longlong_test_type;
  250. typedef reg_pack_longlong_test_type (*reg_pack_longlong_test_function_type) (reg_pack_longlong_test_type);
  251. static int reg_pack_longlong_test_callback(void *ptr) {
  252. reg_pack_longlong_test_function_type f = (reg_pack_longlong_test_function_type)ptr;
  253. reg_pack_longlong_test_type a = {10, 35};
  254. reg_pack_longlong_test_type r;
  255. r = f(a);
  256. return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
  257. }
  258. static int reg_pack_longlong_test(void) {
  259. const char *src =
  260. "typedef struct reg_pack_longlong_test_type_s {long long x, y;} reg_pack_longlong_test_type;"
  261. "reg_pack_longlong_test_type f(reg_pack_longlong_test_type a) {\n"
  262. " reg_pack_longlong_test_type r = {a.x*5, a.y*3};\n"
  263. " return r;\n"
  264. "}\n";
  265. return run_callback(src, reg_pack_longlong_test_callback);
  266. }
  267. /*
  268. * ret_6plus2longlong_test:
  269. *
  270. * This catches a corner case in the x86_64 ABI code: the first 5
  271. * arguments fit into registers, the 6th doesn't, but the 7th argument
  272. * fits into the 6th argument integer register, %r9.
  273. *
  274. * Note that the purpose of the 10th argument is to avoid a situation
  275. * in which gcc would accidentally put the longlong at the right
  276. * address, thus causing a success message even though TCC actually
  277. * generated incorrect code.
  278. */
  279. typedef reg_pack_longlong_test_type (*ret_6plus2longlong_test_function_type) (long long, long long, long long, long long, long long, reg_pack_longlong_test_type, long long, long long);
  280. static int ret_6plus2longlong_test_callback(void *ptr) {
  281. ret_6plus2longlong_test_function_type f = (ret_6plus2longlong_test_function_type)ptr;
  282. reg_pack_longlong_test_type a = {10, 35};
  283. reg_pack_longlong_test_type r;
  284. r = f(0, 0, 0, 0, 0, a, 37, 38);
  285. return ((r.x == 37) && (r.y == 37)) ? 0 : -1;
  286. }
  287. static int ret_6plus2longlong_test(void) {
  288. const char *src =
  289. "typedef struct reg_pack_longlong_test_type_s {long long x, y;} reg_pack_longlong_test_type;"
  290. "reg_pack_longlong_test_type f(long long x1, long long x2, long long x3, long long x4, long long x5, reg_pack_longlong_test_type a, long long x8, long long x9) {\n"
  291. " reg_pack_longlong_test_type r = { x8, x8 };\n"
  292. " return r;\n"
  293. "}\n";
  294. return run_callback(src, ret_6plus2longlong_test_callback);
  295. }
  296. /*
  297. * sret_test: Create a struct large enough to be returned via sret
  298. * (hidden pointer as first function argument)
  299. */
  300. typedef struct sret_test_type_s {long long a, b, c;} sret_test_type;
  301. typedef sret_test_type (*sret_test_function_type) (sret_test_type);
  302. static int sret_test_callback(void *ptr) {
  303. sret_test_function_type f = (sret_test_function_type)(ptr);
  304. sret_test_type x = {5436LL, 658277698LL, 43878957LL};
  305. sret_test_type r = f(x);
  306. return ((r.a==x.a*35)&&(r.b==x.b*19)&&(r.c==x.c*21)) ? 0 : -1;
  307. }
  308. static int sret_test(void) {
  309. const char *src =
  310. "typedef struct sret_test_type_s {long long a, b, c;} sret_test_type;\n"
  311. "sret_test_type f(sret_test_type x) {\n"
  312. " sret_test_type r = {x.a*35, x.b*19, x.c*21};\n"
  313. " return r;\n"
  314. "}\n";
  315. return run_callback(src, sret_test_callback);
  316. }
  317. /*
  318. * one_member_union_test:
  319. *
  320. * In the x86-64 ABI a union should always be passed on the stack. However
  321. * it appears that a single member union is treated by GCC as its member.
  322. */
  323. typedef union one_member_union_test_type_u {int x;} one_member_union_test_type;
  324. typedef one_member_union_test_type (*one_member_union_test_function_type) (one_member_union_test_type);
  325. static int one_member_union_test_callback(void *ptr) {
  326. one_member_union_test_function_type f = (one_member_union_test_function_type)ptr;
  327. one_member_union_test_type a, b;
  328. a.x = 34;
  329. b = f(a);
  330. return (b.x == a.x*2) ? 0 : -1;
  331. }
  332. static int one_member_union_test(void) {
  333. const char *src =
  334. "typedef union one_member_union_test_type_u {int x;} one_member_union_test_type;\n"
  335. "one_member_union_test_type f(one_member_union_test_type a) {\n"
  336. " one_member_union_test_type b;\n"
  337. " b.x = a.x * 2;\n"
  338. " return b;\n"
  339. "}\n";
  340. return run_callback(src, one_member_union_test_callback);
  341. }
  342. /*
  343. * two_member_union_test:
  344. *
  345. * In the x86-64 ABI a union should always be passed on the stack.
  346. */
  347. typedef union two_member_union_test_type_u {int x; long y;} two_member_union_test_type;
  348. typedef two_member_union_test_type (*two_member_union_test_function_type) (two_member_union_test_type);
  349. static int two_member_union_test_callback(void *ptr) {
  350. two_member_union_test_function_type f = (two_member_union_test_function_type)ptr;
  351. two_member_union_test_type a, b;
  352. a.x = 34;
  353. b = f(a);
  354. return (b.x == a.x*2) ? 0 : -1;
  355. }
  356. static int two_member_union_test(void) {
  357. const char *src =
  358. "typedef union two_member_union_test_type_u {int x; long y;} two_member_union_test_type;\n"
  359. "two_member_union_test_type f(two_member_union_test_type a) {\n"
  360. " two_member_union_test_type b;\n"
  361. " b.x = a.x * 2;\n"
  362. " return b;\n"
  363. "}\n";
  364. return run_callback(src, two_member_union_test_callback);
  365. }
  366. /*
  367. * Win64 calling convention test.
  368. */
  369. typedef struct many_struct_test_type_s {long long a, b, c;} many_struct_test_type;
  370. 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);
  371. static int many_struct_test_callback(void *ptr) {
  372. many_struct_test_function_type f = (many_struct_test_function_type)ptr;
  373. many_struct_test_type v = {1, 2, 3};
  374. many_struct_test_type r = f(v,v,v,v,v,v);
  375. return ((r.a == 6) && (r.b == 12) && (r.c == 18))?0:-1;
  376. }
  377. static int many_struct_test(void) {
  378. const char *src =
  379. "typedef struct many_struct_test_type_s {long long a, b, c;} many_struct_test_type;\n"
  380. "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"
  381. " many_struct_test_type y;\n"
  382. " y.a = x1.a + x2.a + x3.a + x4.a + x5.a + x6.a;\n"
  383. " y.b = x1.b + x2.b + x3.b + x4.b + x5.b + x6.b;\n"
  384. " y.c = x1.c + x2.c + x3.c + x4.c + x5.c + x6.c;\n"
  385. " return y;\n"
  386. "}\n";
  387. return run_callback(src, many_struct_test_callback);
  388. }
  389. /*
  390. * Win64 calling convention test.
  391. */
  392. typedef struct many_struct_test_2_type_s {int a, b;} many_struct_test_2_type;
  393. 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);
  394. static int many_struct_test_2_callback(void *ptr) {
  395. many_struct_test_2_function_type f = (many_struct_test_2_function_type)ptr;
  396. many_struct_test_2_type v = {1,2};
  397. many_struct_test_2_type r = f(v,v,v,v,v,v);
  398. return ((r.a == 6) && (r.b == 12))?0:-1;
  399. }
  400. static int many_struct_test_2(void) {
  401. const char *src =
  402. "typedef struct many_struct_test_2_type_s {int a, b;} many_struct_test_2_type;\n"
  403. "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"
  404. " many_struct_test_2_type y;\n"
  405. " y.a = x1.a + x2.a + x3.a + x4.a + x5.a + x6.a;\n"
  406. " y.b = x1.b + x2.b + x3.b + x4.b + x5.b + x6.b;\n"
  407. " return y;\n"
  408. "}\n";
  409. return run_callback(src, many_struct_test_2_callback);
  410. }
  411. /*
  412. * Win64 calling convention test.
  413. */
  414. typedef struct many_struct_test_3_type_s {int a, b;} many_struct_test_3_type;
  415. typedef many_struct_test_3_type (*many_struct_test_3_function_type) (many_struct_test_3_type,many_struct_test_3_type,many_struct_test_3_type,many_struct_test_3_type,many_struct_test_3_type,many_struct_test_3_type, ...);
  416. typedef struct many_struct_test_3_struct_type { many_struct_test_3_function_type f; many_struct_test_3_function_type *f2; } many_struct_test_3_struct_type;
  417. static void many_struct_test_3_dummy(double d, ...)
  418. {
  419. volatile double x = d;
  420. }
  421. static int many_struct_test_3_callback(void *ptr) {
  422. many_struct_test_3_struct_type s = { ptr, };
  423. many_struct_test_3_struct_type *s2 = &s;
  424. s2->f2 = &s2->f;
  425. many_struct_test_3_dummy(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, &s2);
  426. many_struct_test_3_function_type f = *(s2->f2);
  427. many_struct_test_3_type v = {1,2};
  428. many_struct_test_3_type r = (*((s2->f2=&f)+0))(v,v,v,v,v,v,1.0);
  429. return ((r.a == 6) && (r.b == 12))?0:-1;
  430. }
  431. static int many_struct_test_3(void) {
  432. const char *src =
  433. "typedef struct many_struct_test_3_type_s {int a, b;} many_struct_test_3_type;\n"
  434. "many_struct_test_3_type f(many_struct_test_3_type x1, many_struct_test_3_type x2, many_struct_test_3_type x3, many_struct_test_3_type x4, many_struct_test_3_type x5, many_struct_test_3_type x6, ...) {\n"
  435. " many_struct_test_3_type y;\n"
  436. " y.a = x1.a + x2.a + x3.a + x4.a + x5.a + x6.a;\n"
  437. " y.b = x1.b + x2.b + x3.b + x4.b + x5.b + x6.b;\n"
  438. " return y;\n"
  439. "}\n";
  440. return run_callback(src, many_struct_test_3_callback);
  441. }
  442. /*
  443. * stdarg_test: Test variable argument list ABI
  444. */
  445. typedef struct {long long a, b, c;} stdarg_test_struct_type;
  446. typedef void (*stdarg_test_function_type) (int,int,int,...);
  447. static int stdarg_test_callback(void *ptr) {
  448. stdarg_test_function_type f = (stdarg_test_function_type)ptr;
  449. int x;
  450. double y;
  451. stdarg_test_struct_type z = {1, 2, 3}, w;
  452. f(10, 10, 5,
  453. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, &x,
  454. 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, &y,
  455. z, z, z, z, z, &w);
  456. return ((x == 55) && (y == 55) && (w.a == 5) && (w.b == 10) && (w.c == 15)) ? 0 : -1;
  457. }
  458. static int stdarg_test(void) {
  459. const char *src =
  460. "#include <stdarg.h>\n"
  461. "typedef struct {long long a, b, c;} stdarg_test_struct_type;\n"
  462. "void f(int n_int, int n_float, int n_struct, ...) {\n"
  463. " int i, ti = 0;\n"
  464. " double td = 0.0;\n"
  465. " stdarg_test_struct_type ts = {0,0,0}, tmp;\n"
  466. " va_list ap;\n"
  467. " va_start(ap, n_struct);\n"
  468. " for (i = 0, ti = 0; i < n_int; ++i)\n"
  469. " ti += va_arg(ap, int);\n"
  470. " *va_arg(ap, int*) = ti;\n"
  471. " for (i = 0, td = 0; i < n_float; ++i)\n"
  472. " td += va_arg(ap, double);\n"
  473. " *va_arg(ap, double*) = td;\n"
  474. " for (i = 0; i < n_struct; ++i) {\n"
  475. " tmp = va_arg(ap, stdarg_test_struct_type);\n"
  476. " ts.a += tmp.a; ts.b += tmp.b; ts.c += tmp.c;"
  477. " }\n"
  478. " *va_arg(ap, stdarg_test_struct_type*) = ts;\n"
  479. " va_end(ap);"
  480. "}\n";
  481. return run_callback(src, stdarg_test_callback);
  482. }
  483. typedef struct {long long a, b;} stdarg_many_test_struct_type;
  484. typedef void (*stdarg_many_test_function_type) (int, int, int, int, int,
  485. stdarg_many_test_struct_type,
  486. int, int, ...);
  487. static int stdarg_many_test_callback(void *ptr)
  488. {
  489. stdarg_many_test_function_type f = (stdarg_many_test_function_type)ptr;
  490. int x;
  491. stdarg_many_test_struct_type l = {10, 11};
  492. f(1, 2, 3, 4, 5, l, 6, 7, &x, 44);
  493. return x == 44 ? 0 : -1;
  494. }
  495. static int stdarg_many_test(void)
  496. {
  497. const char *src =
  498. "#include <stdarg.h>\n"
  499. "typedef struct {long long a, b;} stdarg_many_test_struct_type;\n"
  500. "void f (int a, int b, int c, int d, int e, stdarg_many_test_struct_type l, int f, int g, ...){\n"
  501. " va_list ap;\n"
  502. " int *p;\n"
  503. " va_start (ap, g);\n"
  504. " p = va_arg(ap, int*);\n"
  505. " *p = va_arg(ap, int);\n"
  506. " va_end (ap);\n"
  507. "}\n";
  508. return run_callback(src, stdarg_many_test_callback);
  509. }
  510. /*
  511. * Test Win32 stdarg handling, since the calling convention will pass a pointer
  512. * to the struct and the stdarg pointer must point to that pointer initially.
  513. */
  514. typedef struct {long long a, b, c;} stdarg_struct_test_struct_type;
  515. typedef int (*stdarg_struct_test_function_type) (stdarg_struct_test_struct_type a, ...);
  516. static int stdarg_struct_test_callback(void *ptr) {
  517. stdarg_struct_test_function_type f = (stdarg_struct_test_function_type)ptr;
  518. stdarg_struct_test_struct_type v = {10, 35, 99};
  519. int x = f(v, 234);
  520. return (x == 378) ? 0 : -1;
  521. }
  522. static int stdarg_struct_test(void) {
  523. const char *src =
  524. "#include <stdarg.h>\n"
  525. "typedef struct {long long a, b, c;} stdarg_struct_test_struct_type;\n"
  526. "int f(stdarg_struct_test_struct_type a, ...) {\n"
  527. " va_list ap;\n"
  528. " va_start(ap, a);\n"
  529. " int z = va_arg(ap, int);\n"
  530. " va_end(ap);\n"
  531. " return z + a.a + a.b + a.c;\n"
  532. "}\n";
  533. return run_callback(src, stdarg_struct_test_callback);
  534. }
  535. /* Test that x86-64 arranges the stack correctly for arguments with alignment >8 bytes */
  536. typedef LONG_DOUBLE (*arg_align_test_callback_type) (LONG_DOUBLE,int,LONG_DOUBLE,int,LONG_DOUBLE);
  537. static int arg_align_test_callback(void *ptr) {
  538. arg_align_test_callback_type f = (arg_align_test_callback_type)ptr;
  539. long double x = f(12, 0, 25, 0, 37);
  540. return (x == 74) ? 0 : -1;
  541. }
  542. static int arg_align_test(void) {
  543. const char *src =
  544. "long double f(long double a, int b, long double c, int d, long double e) {\n"
  545. " return a + c + e;\n"
  546. "}\n";
  547. return run_callback(src, arg_align_test_callback);
  548. }
  549. #define RUN_TEST(t) \
  550. if (!testname || (strcmp(#t, testname) == 0)) { \
  551. fputs(#t "... ", stdout); \
  552. fflush(stdout); \
  553. if (t() == 0) { \
  554. fputs("success\n", stdout); \
  555. } else { \
  556. fputs("failure\n", stdout); \
  557. retval = EXIT_FAILURE; \
  558. } \
  559. }
  560. int main(int argc, char **argv) {
  561. int i;
  562. const char *testname = NULL;
  563. int retval = EXIT_SUCCESS;
  564. /* if tcclib.h and libtcc1.a are not installed, where can we find them */
  565. for (i = 1; i < argc; ++i) {
  566. if (!memcmp(argv[i], "run_test=", 9))
  567. testname = argv[i] + 9;
  568. }
  569. g_argv = argv, g_argc = argc;
  570. RUN_TEST(ret_int_test);
  571. RUN_TEST(ret_longlong_test);
  572. RUN_TEST(ret_float_test);
  573. RUN_TEST(ret_double_test);
  574. RUN_TEST(ret_longdouble_test);
  575. RUN_TEST(ret_2float_test);
  576. RUN_TEST(ret_2double_test);
  577. RUN_TEST(ret_8plus2double_test);
  578. RUN_TEST(ret_6plus2longlong_test);
  579. #if !defined __x86_64__ || defined _WIN32
  580. /* currently broken on x86_64 linux */
  581. RUN_TEST(ret_mixed_test);
  582. RUN_TEST(ret_mixed2_test);
  583. #endif
  584. RUN_TEST(ret_mixed3_test);
  585. RUN_TEST(reg_pack_test);
  586. RUN_TEST(reg_pack_longlong_test);
  587. RUN_TEST(sret_test);
  588. RUN_TEST(one_member_union_test);
  589. RUN_TEST(two_member_union_test);
  590. RUN_TEST(many_struct_test);
  591. RUN_TEST(many_struct_test_2);
  592. RUN_TEST(many_struct_test_3);
  593. RUN_TEST(stdarg_test);
  594. RUN_TEST(stdarg_many_test);
  595. RUN_TEST(stdarg_struct_test);
  596. RUN_TEST(arg_align_test);
  597. return retval;
  598. }