Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

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