Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * TCC auto test program
  3.  */
  4. #include "../config.h"
  5. #include <stddef.h>
  6. #include <stdarg.h>
  7.  
  8. #if GCC_MAJOR >= 3
  9.  
  10. /* Unfortunately, gcc version < 3 does not handle that! */
  11. #define ALL_ISOC99
  12.  
  13. /* only gcc 3 handles _Bool correctly */
  14. #define BOOL_ISOC99
  15.  
  16. /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
  17. #define CORRECT_CR_HANDLING
  18.  
  19. #endif
  20.  
  21. #if defined(_WIN32)
  22. #define LONG_LONG_FORMAT "%lld"
  23. #define ULONG_LONG_FORMAT "%llu"
  24. #else
  25. #define LONG_LONG_FORMAT "%Ld"
  26. #define ULONG_LONG_FORMAT "%Lu"
  27. #endif
  28.  
  29. // MinGW has 80-bit rather than 64-bit long double which isn't compatible with TCC or MSVC
  30. #if defined(_WIN32) && defined(__GNUC__)
  31. #define LONG_DOUBLE double
  32. #define LONG_DOUBLE_LITERAL(x) x
  33. #else
  34. #define LONG_DOUBLE long double
  35. #define LONG_DOUBLE_LITERAL(x) x ## L
  36. #endif
  37.  
  38. /* deprecated and no longer supported in gcc 3.3 */
  39. //#define ACCEPT_CR_IN_STRINGS
  40.  
  41. /* __VA_ARGS__ and __func__ support */
  42. #define C99_MACROS
  43.  
  44. /* test various include syntaxes */
  45.  
  46. #define TCCLIB_INC <tcclib.h>
  47.  
  48. #define TCCLIB_INC1 <tcclib
  49. #define TCCLIB_INC2 h>
  50. #define TCCLIB_INC3 "tcclib"
  51.  
  52. #include TCCLIB_INC
  53.  
  54. #include TCCLIB_INC1.TCCLIB_INC2
  55.  
  56. #include TCCLIB_INC1.h>
  57.  
  58. /* gcc 3.2 does not accept that (bug ?) */
  59. //#include TCCLIB_INC3 ".h"
  60.  
  61. #include <tcclib.h>
  62.  
  63. #include "tcclib.h"
  64.  
  65. void intdiv_test();
  66. void string_test();
  67. void expr_test();
  68. void macro_test();
  69. void recursive_macro_test();
  70. void scope_test();
  71. void forward_test();
  72. void funcptr_test();
  73. void loop_test();
  74. void switch_test();
  75. void goto_test();
  76. void enum_test();
  77. void typedef_test();
  78. void struct_test();
  79. void array_test();
  80. void expr_ptr_test();
  81. void bool_test();
  82. void expr2_test();
  83. void constant_expr_test();
  84. void expr_cmp_test();
  85. void char_short_test();
  86. void init_test(void);
  87. void compound_literal_test(void);
  88. int kr_test();
  89. void struct_assign_test(void);
  90. void cast_test(void);
  91. void bitfield_test(void);
  92. void c99_bool_test(void);
  93. void float_test(void);
  94. void longlong_test(void);
  95. void manyarg_test(void);
  96. void stdarg_test(void);
  97. void whitespace_test(void);
  98. void relocation_test(void);
  99. void old_style_function(void);
  100. void alloca_test(void);
  101. void c99_vla_test(int size1, int size2);
  102. void sizeof_test(void);
  103. void typeof_test(void);
  104. void local_label_test(void);
  105. void statement_expr_test(void);
  106. void asm_test(void);
  107. void builtin_test(void);
  108. void weak_test(void);
  109. void global_data_test(void);
  110. void cmp_comparison_test(void);
  111. void math_cmp_test(void);
  112. void callsave_test(void);
  113. void builtin_frame_address_test(void);
  114.  
  115. int fib(int n);
  116. void num(int n);
  117. void forward_ref(void);
  118. int isid(int c);
  119.  
  120. /* Line joining happens before tokenization, so the following
  121.    must be parsed as ellipsis.  */
  122. void funny_line_continuation (int, ..\
  123. . );
  124.  
  125. char via_volatile (char);
  126.  
  127. #define A 2
  128. #define N 1234 + A
  129. #define pf printf
  130. #define M1(a, b)  (a) + (b)
  131.  
  132. #define str\
  133. (s) # s
  134. #define glue(a, b) a ## b
  135. #define xglue(a, b) glue(a, b)
  136. #define HIGHLOW "hello"
  137. #define LOW LOW ", world"
  138.  
  139. static int onetwothree = 123;
  140. #define onetwothree4 onetwothree
  141. #define onetwothree xglue(onetwothree,4)
  142.  
  143. #define min(a, b) ((a) < (b) ? (a) : (b))
  144.  
  145. #ifdef C99_MACROS
  146. #define dprintf(level,...) printf(__VA_ARGS__)
  147. #endif
  148.  
  149. /* gcc vararg macros */
  150. #define dprintf1(level, fmt, args...) printf(fmt, ## args)
  151.  
  152. #define MACRO_NOARGS()
  153.  
  154. #define AAA 3
  155. #undef AAA
  156. #define AAA 4
  157.  
  158. #if 1
  159. #define B3 1
  160. #elif 1
  161. #define B3 2
  162. #elif 0
  163. #define B3 3
  164. #else
  165. #define B3 4
  166. #endif
  167.  
  168. #define __INT64_C(c)    c ## LL
  169. #define INT64_MIN       (-__INT64_C(9223372036854775807)-1)
  170.  
  171. int qq(int x)
  172. {
  173.     return x + 40;
  174. }
  175. #define qq(x) x
  176.  
  177. #define spin_lock(lock) do { } while (0)
  178. #define wq_spin_lock spin_lock
  179. #define TEST2() wq_spin_lock(a)
  180.  
  181. #define UINT_MAX ((unsigned) -1)
  182.  
  183. void intdiv_test(void)
  184. {
  185.     printf("18/21=%u\n", 18/21);
  186.     printf("18%%21=%u\n", 18%21);
  187.     printf("41/21=%u\n", 41/21);
  188.     printf("41%%21=%u\n", 41%21);
  189.     printf("42/21=%u\n", 42/21);
  190.     printf("42%%21=%u\n", 42%21);
  191.     printf("43/21=%u\n", 43/21);
  192.     printf("43%%21=%u\n", 43%21);
  193.     printf("126/21=%u\n", 126/21);
  194.     printf("126%%21=%u\n", 126%21);
  195.     printf("131/21=%u\n", 131/21);
  196.     printf("131%%21=%u\n", 131%21);
  197.     printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX/2+3)/2);
  198.     printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX/2+3)%2);
  199.  
  200.     printf("18/-21=%u\n", 18/-21);
  201.     printf("18%%-21=%u\n", 18%-21);
  202.     printf("41/-21=%u\n", 41/-21);
  203.     printf("41%%-21=%u\n", 41%-21);
  204.     printf("42/-21=%u\n", 42/-21);
  205.     printf("42%%-21=%u\n", 42%-21);
  206.     printf("43/-21=%u\n", 43/-21);
  207.     printf("43%%-21=%u\n", 43%-21);
  208.     printf("126/-21=%u\n", 126/-21);
  209.     printf("126%%-21=%u\n", 126%-21);
  210.     printf("131/-21=%u\n", 131/-21);
  211.     printf("131%%-21=%u\n", 131%-21);
  212.     printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX/2+3)/-2);
  213.     printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX/2+3)%-2);
  214.  
  215.     printf("-18/21=%u\n", -18/21);
  216.     printf("-18%%21=%u\n", -18%21);
  217.     printf("-41/21=%u\n", -41/21);
  218.     printf("-41%%21=%u\n", -41%21);
  219.     printf("-42/21=%u\n", -42/21);
  220.     printf("-42%%21=%u\n", -42%21);
  221.     printf("-43/21=%u\n", -43/21);
  222.     printf("-43%%21=%u\n", -43%21);
  223.     printf("-126/21=%u\n", -126/21);
  224.     printf("-126%%21=%u\n", -126%21);
  225.     printf("-131/21=%u\n", -131/21);
  226.     printf("-131%%21=%u\n", -131%21);
  227.     printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX/2+3))/2);
  228.     printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX/2+3))%2);
  229.  
  230.     printf("-18/-21=%u\n", -18/-21);
  231.     printf("-18%%-21=%u\n", -18%-21);
  232.     printf("-41/-21=%u\n", -41/-21);
  233.     printf("-41%%-21=%u\n", -41%-21);
  234.     printf("-42/-21=%u\n", -42/-21);
  235.     printf("-42%%-21=%u\n", -42%-21);
  236.     printf("-43/-21=%u\n", -43/-21);
  237.     printf("-43%%-21=%u\n", -43%-21);
  238.     printf("-126/-21=%u\n", -126/-21);
  239.     printf("-126%%-21=%u\n", -126%-21);
  240.     printf("-131/-21=%u\n", -131/-21);
  241.     printf("-131%%-21=%u\n", -131%-21);
  242.     printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX/2+3))/-2);
  243.     printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX/2+3))%-2);
  244. }
  245.  
  246. void macro_test(void)
  247. {
  248.     printf("macro:\n");
  249.     pf("N=%d\n", N);
  250.     printf("aaa=%d\n", AAA);
  251.  
  252.     printf("min=%d\n", min(1, min(2, -1)));
  253.  
  254.     printf("s1=%s\n", glue(HIGH, LOW));
  255.     printf("s2=%s\n", xglue(HIGH, LOW));
  256.     printf("s3=%s\n", str("c"));
  257.     printf("s4=%s\n", str(a1));
  258.     printf("B3=%d\n", B3);
  259.  
  260.     printf("onetwothree=%d\n", onetwothree);
  261.  
  262. #ifdef A
  263.     printf("A defined\n");
  264. #endif
  265. #ifdef B
  266.     printf("B defined\n");
  267. #endif
  268. #ifdef A
  269.     printf("A defined\n");
  270. #else
  271.     printf("A not defined\n");
  272. #endif
  273. #ifdef B
  274.     printf("B defined\n");
  275. #else
  276.     printf("B not defined\n");
  277. #endif
  278.  
  279. #ifdef A
  280.     printf("A defined\n");
  281. #ifdef B
  282.     printf("B1 defined\n");
  283. #else
  284.     printf("B1 not defined\n");
  285. #endif
  286. #else
  287.     printf("A not defined\n");
  288. #ifdef B
  289.     printf("B2 defined\n");
  290. #else
  291.     printf("B2 not defined\n");
  292. #endif
  293. #endif
  294.  
  295. #if 1+1
  296.     printf("test true1\n");
  297. #endif
  298. #if 0
  299.     printf("test true2\n");
  300. #endif
  301. #if 1-1
  302.     printf("test true3\n");
  303. #endif
  304. #if defined(A)
  305.     printf("test trueA\n");
  306. #endif
  307. #if defined(B)
  308.     printf("test trueB\n");
  309. #endif
  310.  
  311. #if 0
  312.     printf("test 0\n");
  313. #elif 0
  314.     printf("test 1\n");
  315. #elif 2
  316.     printf("test 2\n");
  317. #else
  318.     printf("test 3\n");
  319. #endif
  320.  
  321.     MACRO_NOARGS();
  322.  
  323. #ifdef __LINE__
  324.     printf("__LINE__ defined\n");
  325. #endif
  326.  
  327.     printf("__LINE__=%d __FILE__=%s\n",
  328.            __LINE__, __FILE__);
  329. #if 0
  330. #line 200
  331.     printf("__LINE__=%d __FILE__=%s\n",
  332.            __LINE__, __FILE__);
  333. #line 203 "test"
  334.     printf("__LINE__=%d __FILE__=%s\n",
  335.            __LINE__, __FILE__);
  336. #line 227 "tcctest.c"
  337. #endif
  338.  
  339.     /* not strictly preprocessor, but we test it there */
  340. #ifdef C99_MACROS
  341.     printf("__func__ = %s\n", __func__);
  342.     dprintf(1, "vaarg=%d\n", 1);
  343. #endif
  344.     dprintf1(1, "vaarg1\n");
  345.     dprintf1(1, "vaarg1=%d\n", 2);
  346.     dprintf1(1, "vaarg1=%d %d\n", 1, 2);
  347.  
  348.     /* gcc extension */
  349.     printf("func='%s'\n", __FUNCTION__);
  350.  
  351.     /* complicated macros in glibc */
  352.     printf("INT64_MIN=" LONG_LONG_FORMAT "\n", INT64_MIN);
  353.     {
  354.         int a;
  355.         a = 1;
  356.         glue(a+, +);
  357.         printf("a=%d\n", a);
  358.         glue(a <, <= 2);
  359.         printf("a=%d\n", a);
  360.     }
  361.    
  362.     /* macro function with argument outside the macro string */
  363. #define MF_s MF_hello
  364. #define MF_hello(msg) printf("%s\n",msg)
  365.  
  366. #define MF_t printf("tralala\n"); MF_hello
  367.  
  368.     MF_s("hi");
  369.     MF_t("hi");
  370.    
  371.     /* test macro substituion inside args (should not eat stream) */
  372.     printf("qq=%d\n", qq(qq)(2));
  373.  
  374.     /* test zero argument case. NOTE: gcc 2.95.x does not accept a
  375.        null argument without a space. gcc 3.2 fixes that. */
  376.  
  377. #define qq1(x) 1
  378.     printf("qq1=%d\n", qq1( ));
  379.  
  380.     /* comment with stray handling *\
  381. /
  382.        /* this is a valid *\/ comment */
  383.        /* this is a valid comment *\*/
  384.     //  this is a valid\
  385. comment
  386.  
  387.     /* test function macro substitution when the function name is
  388.        substituted */
  389.     TEST2();
  390.  
  391.     /* And again when the name and parenthes are separated by a
  392.        comment.  */
  393.     TEST2 /* the comment */ ();
  394. }
  395.  
  396.  
  397. static void print_num(char *fn, int line, int num) {
  398.     printf("fn %s, line %d, num %d\n", fn, line, num);
  399. }
  400.  
  401. void recursive_macro_test(void)
  402. {
  403.  
  404. #define ELF32_ST_TYPE(val)              ((val) & 0xf)
  405. #define ELF32_ST_INFO(bind, type)       (((bind) << 4) + ((type) & 0xf))
  406. #define STB_WEAK        2               /* Weak symbol */
  407. #define ELFW(type) ELF##32##_##type
  408.     printf("%d\n", ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(123)));
  409.  
  410. #define WRAP(x) x
  411.    
  412. #define print_num(x) print_num(__FILE__,__LINE__,x)
  413.     print_num(123);
  414.     WRAP(print_num(123));
  415.     WRAP(WRAP(print_num(123)));
  416.  
  417. static struct recursive_macro { int rm_field; } G;
  418. #define rm_field (G.rm_field)
  419.     printf("rm_field = %d\n", rm_field);
  420.     printf("rm_field = %d\n", WRAP(rm_field));
  421.     WRAP((printf("rm_field = %d %d\n", rm_field, WRAP(rm_field))));
  422. }
  423.  
  424. int op(a,b)
  425. {
  426.     return a / b;
  427. }
  428.  
  429. int ret(a)
  430. {
  431.     if (a == 2)
  432.         return 1;
  433.     if (a == 3)
  434.         return 2;
  435.     return 0;
  436. }
  437.  
  438. void ps(const char *s)
  439. {
  440.     int c;
  441.     while (1) {
  442.         c = *s;
  443.         if (c == 0)
  444.             break;
  445.         printf("%c", c);
  446.         s++;
  447.     }
  448. }
  449.  
  450. const char foo1_string[] = "\
  451. bar\n\
  452. test\14\
  453. 1";
  454.  
  455. void string_test()
  456. {
  457.     unsigned int b;
  458.     printf("string:\n");
  459.     printf("\141\1423\143\n");/* dezdez test */
  460.     printf("\x41\x42\x43\x3a\n");
  461.     printf("c=%c\n", 'r');
  462.     printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
  463.     printf("foo1_string='%s'\n", foo1_string);
  464. #if 0
  465.     printf("wstring=%S\n", L"abc");
  466.     printf("wstring=%S\n", L"abc" L"def" "ghi");
  467.     printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
  468.     printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
  469. #endif
  470.     ps("test\n");
  471.     b = 32;
  472.     while ((b = b + 1) < 96) {
  473.         printf("%c", b);
  474.     }
  475.     printf("\n");
  476.     printf("fib=%d\n", fib(33));
  477.     b = 262144;
  478.     while (b != 0x80000000) {
  479.         num(b);
  480.         b = b * 2;
  481.     }
  482. }
  483.  
  484. void loop_test()
  485. {
  486.     int i;
  487.     i = 0;
  488.     while (i < 10)
  489.         printf("%d", i++);
  490.     printf("\n");
  491.     for(i = 0; i < 10;i++)
  492.         printf("%d", i);
  493.     printf("\n");
  494.     i = 0;
  495.     do {
  496.         printf("%d", i++);
  497.     } while (i < 10);
  498.     printf("\n");
  499.  
  500.     char count = 123;
  501.     /* c99 for loop init test */
  502.     for (size_t count = 1; count < 3; count++)
  503.         printf("count=%d\n", count);
  504.     printf("count = %d\n", count);
  505.  
  506.     /* break/continue tests */
  507.     i = 0;
  508.     while (1) {
  509.         if (i == 6)
  510.             break;
  511.         i++;
  512.         if (i == 3)
  513.             continue;
  514.         printf("%d", i);
  515.     }
  516.     printf("\n");
  517.  
  518.     /* break/continue tests */
  519.     i = 0;
  520.     do {
  521.         if (i == 6)
  522.             break;
  523.         i++;
  524.         if (i == 3)
  525.             continue;
  526.         printf("%d", i);
  527.     } while(1);
  528.     printf("\n");
  529.  
  530.     for(i = 0;i < 10;i++) {
  531.         if (i == 3)
  532.             continue;
  533.         printf("%d", i);
  534.     }
  535.     printf("\n");
  536. }
  537.  
  538. typedef int typedef_and_label;
  539.  
  540. void goto_test()
  541. {
  542.     int i;
  543.     static void *label_table[3] = { &&label1, &&label2, &&label3 };
  544.  
  545.     printf("goto:\n");
  546.     i = 0;
  547.     /* This needs to parse as label, not as start of decl.  */
  548.  typedef_and_label:
  549.  s_loop:
  550.     if (i >= 10)
  551.         goto s_end;
  552.     printf("%d", i);
  553.     i++;
  554.     goto s_loop;
  555.  s_end:
  556.     printf("\n");
  557.  
  558.     /* we also test computed gotos (GCC extension) */
  559.     for(i=0;i<3;i++) {
  560.         goto *label_table[i];
  561.     label1:
  562.         printf("label1\n");
  563.         goto next;
  564.     label2:
  565.         printf("label2\n");
  566.         goto next;
  567.     label3:
  568.         printf("label3\n");
  569.     next: ;
  570.     }
  571. }
  572.  
  573. enum {
  574.     E0,
  575.     E1 = 2,
  576.     E2 = 4,
  577.     E3,
  578.     E4,
  579. };
  580.  
  581. enum test {
  582.     E5 = 1000,
  583. };
  584.  
  585. void enum_test()
  586. {
  587.     enum test b1;
  588.     printf("enum:\n%d %d %d %d %d %d\n",
  589.            E0, E1, E2, E3, E4, E5);
  590.     b1 = 1;
  591.     printf("b1=%d\n", b1);
  592. }
  593.  
  594. typedef int *my_ptr;
  595.  
  596. typedef int mytype1;
  597. typedef int mytype2;
  598.  
  599. void typedef_test()
  600. {
  601.     my_ptr a;
  602.     mytype1 mytype2;
  603.     int b;
  604.  
  605.     a = &b;
  606.     *a = 1234;
  607.     printf("typedef:\n");
  608.     printf("a=%d\n", *a);
  609.     mytype2 = 2;
  610.     printf("mytype2=%d\n", mytype2);
  611. }
  612.  
  613. void forward_test()
  614. {
  615.     printf("forward:\n");
  616.     forward_ref();
  617.     forward_ref();
  618. }
  619.  
  620.  
  621. void forward_ref(void)
  622. {
  623.     printf("forward ok\n");
  624. }
  625.  
  626. typedef struct struct1 {
  627.     int f1;
  628.     int f2, f3;
  629.     union union1 {
  630.         int v1;
  631.         int v2;
  632.     } u;
  633.     char str[3];
  634. } struct1;
  635.  
  636. struct struct2 {
  637.     int a;
  638.     char b;
  639. };
  640.  
  641. union union2 {
  642.     int w1;
  643.     int w2;
  644. };
  645.  
  646. struct struct1 st1, st2;
  647.  
  648. int main(int argc, char **argv)
  649. {
  650.     string_test();
  651.     expr_test();
  652.     macro_test();
  653.     recursive_macro_test();
  654.     scope_test();
  655.     forward_test();
  656.     funcptr_test();
  657.     loop_test();
  658.     switch_test();
  659.     goto_test();
  660.     enum_test();
  661.     typedef_test();
  662.     struct_test();
  663.     array_test();
  664.     expr_ptr_test();
  665.     bool_test();
  666.     expr2_test();
  667.     constant_expr_test();
  668.     expr_cmp_test();
  669.     char_short_test();
  670.     init_test();
  671.     compound_literal_test();
  672.     kr_test();
  673.     struct_assign_test();
  674.     cast_test();
  675.     bitfield_test();
  676.     c99_bool_test();
  677.     float_test();
  678.     longlong_test();
  679.     manyarg_test();
  680.     stdarg_test();
  681.     whitespace_test();
  682.     relocation_test();
  683.     old_style_function();
  684. //    alloca_test();
  685.     c99_vla_test(5, 2);
  686.     sizeof_test();
  687.     typeof_test();
  688.     statement_expr_test();
  689.     local_label_test();
  690.     asm_test();
  691.     builtin_test();
  692. #ifndef _WIN32
  693. //    weak_test();
  694. #endif
  695.     global_data_test();
  696.     cmp_comparison_test();
  697.     math_cmp_test();
  698.     callsave_test();
  699.     builtin_frame_address_test();
  700.     intdiv_test();
  701.     if (via_volatile (42) != 42)
  702.       printf ("via_volatile broken\n");
  703.     return 0;
  704. }
  705.  
  706. int tab[3];
  707. int tab2[3][2];
  708.  
  709. int g;
  710.  
  711. void f1(g)
  712. {
  713.     printf("g1=%d\n", g);
  714. }
  715.  
  716. void scope_test()
  717. {
  718.     printf("scope:\n");
  719.     g = 2;
  720.     f1(1);
  721.     printf("g2=%d\n", g);
  722.     {
  723.         int g;
  724.         g = 3;
  725.         printf("g3=%d\n", g);
  726.         {
  727.             int g;
  728.             g = 4;
  729.             printf("g4=%d\n", g);
  730.         }
  731.     }
  732.     printf("g5=%d\n", g);
  733. }
  734.  
  735. void array_test()
  736. {
  737.     int i, j, a[4];
  738.  
  739.     printf("array:\n");
  740.     printf("sizeof(a) = %d\n", sizeof(a));
  741.     printf("sizeof(\"a\") = %d\n", sizeof("a"));
  742. #ifdef C99_MACROS
  743.     printf("sizeof(__func__) = %d\n", sizeof(__func__));
  744. #endif
  745.     printf("sizeof tab %d\n", sizeof(tab));
  746.     printf("sizeof tab2 %d\n", sizeof tab2);
  747.     tab[0] = 1;
  748.     tab[1] = 2;
  749.     tab[2] = 3;
  750.     printf("%d %d %d\n", tab[0], tab[1], tab[2]);
  751.     for(i=0;i<3;i++)
  752.         for(j=0;j<2;j++)
  753.             tab2[i][j] = 10 * i + j;
  754.     for(i=0;i<3*2;i++) {
  755.         printf(" %3d", ((int *)tab2)[i]);
  756.     }
  757.     printf("\n");
  758.     printf("sizeof(size_t)=%d\n", sizeof(size_t));
  759.     printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
  760. }
  761.  
  762. void expr_test()
  763. {
  764.     int a, b;
  765.     a = 0;
  766.     printf("%d\n", a += 1);
  767.     printf("%d\n", a -= 2);
  768.     printf("%d\n", a *= 31232132);
  769.     printf("%d\n", a /= 4);
  770.     printf("%d\n", a %= 20);
  771.     printf("%d\n", a &= 6);
  772.     printf("%d\n", a ^= 7);
  773.     printf("%d\n", a |= 8);
  774.     printf("%d\n", a >>= 3);
  775.     printf("%d\n", a <<= 4);
  776.  
  777.     a = 22321;
  778.     b = -22321;
  779.     printf("%d\n", a + 1);
  780.     printf("%d\n", a - 2);
  781.     printf("%d\n", a * 312);
  782.     printf("%d\n", a / 4);
  783.     printf("%d\n", b / 4);
  784.     printf("%d\n", (unsigned)b / 4);
  785.     printf("%d\n", a % 20);
  786.     printf("%d\n", b % 20);
  787.     printf("%d\n", (unsigned)b % 20);
  788.     printf("%d\n", a & 6);
  789.     printf("%d\n", a ^ 7);
  790.     printf("%d\n", a | 8);
  791.     printf("%d\n", a >> 3);
  792.     printf("%d\n", b >> 3);
  793.     printf("%d\n", (unsigned)b >> 3);
  794.     printf("%d\n", a << 4);
  795.     printf("%d\n", ~a);
  796.     printf("%d\n", -a);
  797.     printf("%d\n", +a);
  798.  
  799.     printf("%d\n", 12 + 1);
  800.     printf("%d\n", 12 - 2);
  801.     printf("%d\n", 12 * 312);
  802.     printf("%d\n", 12 / 4);
  803.     printf("%d\n", 12 % 20);
  804.     printf("%d\n", 12 & 6);
  805.     printf("%d\n", 12 ^ 7);
  806.     printf("%d\n", 12 | 8);
  807.     printf("%d\n", 12 >> 2);
  808.     printf("%d\n", 12 << 4);
  809.     printf("%d\n", ~12);
  810.     printf("%d\n", -12);
  811.     printf("%d\n", +12);
  812.     printf("%d %d %d %d\n",
  813.            isid('a'),
  814.            isid('g'),
  815.            isid('T'),
  816.            isid('('));
  817. }
  818.  
  819. int isid(int c)
  820. {
  821.     return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
  822. }
  823.  
  824. /**********************/
  825.  
  826. int vstack[10], *vstack_ptr;
  827.  
  828. void vpush(int vt, int vc)
  829. {
  830.     *vstack_ptr++ = vt;
  831.     *vstack_ptr++ = vc;
  832. }
  833.  
  834. void vpop(int *ft, int *fc)
  835. {
  836.     *fc = *--vstack_ptr;
  837.     *ft = *--vstack_ptr;
  838. }
  839.  
  840. void expr2_test()
  841. {
  842.     int a, b;
  843.  
  844.     printf("expr2:\n");
  845.     vstack_ptr = vstack;
  846.     vpush(1432432, 2);
  847.     vstack_ptr[-2] &= ~0xffffff80;
  848.     vpop(&a, &b);
  849.     printf("res= %d %d\n", a, b);
  850. }
  851.  
  852. void constant_expr_test()
  853. {
  854.     int a;
  855.     printf("constant_expr:\n");
  856.     a = 3;
  857.     printf("%d\n", a * 16);
  858.     printf("%d\n", a * 1);
  859.     printf("%d\n", a + 0);
  860. }
  861.  
  862. int tab4[10];
  863.  
  864. void expr_ptr_test()
  865. {
  866.     int *p, *q;
  867.     int i = -1;
  868.  
  869.     printf("expr_ptr:\n");
  870.     p = tab4;
  871.     q = tab4 + 10;
  872.     printf("diff=%d\n", q - p);
  873.     p++;
  874.     printf("inc=%d\n", p - tab4);
  875.     p--;
  876.     printf("dec=%d\n", p - tab4);
  877.     ++p;
  878.     printf("inc=%d\n", p - tab4);
  879.     --p;
  880.     printf("dec=%d\n", p - tab4);
  881.     printf("add=%d\n", p + 3 - tab4);
  882.     printf("add=%d\n", 3 + p - tab4);
  883.  
  884.     /* check if 64bit support is ok */
  885.     q = p = 0;
  886.     q += i;
  887.     printf("%p %p %ld\n", q, p, p-q);
  888.     printf("%d %d %d %d %d %d\n",
  889.            p == q, p != q, p < q, p <= q, p >= q, p > q);
  890.     i = 0xf0000000;
  891.     p += i;
  892.     printf("%p %p %ld\n", q, p, p-q);
  893.     printf("%d %d %d %d %d %d\n",
  894.            p == q, p != q, p < q, p <= q, p >= q, p > q);
  895.     p = (int *)((char *)p + 0xf0000000);
  896.     printf("%p %p %ld\n", q, p, p-q);
  897.     printf("%d %d %d %d %d %d\n",
  898.            p == q, p != q, p < q, p <= q, p >= q, p > q);
  899.     p += 0xf0000000;
  900.     printf("%p %p %ld\n", q, p, p-q);
  901.     printf("%d %d %d %d %d %d\n",
  902.            p == q, p != q, p < q, p <= q, p >= q, p > q);
  903.     {
  904.         struct size12 {
  905.             int i, j, k;
  906.         };
  907.         struct size12 s[2], *sp = s;
  908.         int i, j;
  909.         sp->i = 42;
  910.         sp++;
  911.         j = -1;
  912.         printf("%d\n", sp[j].i);
  913.     }
  914. }
  915.  
  916. void expr_cmp_test()
  917. {
  918.     int a, b;
  919.     printf("constant_expr:\n");
  920.     a = -1;
  921.     b = 1;
  922.     printf("%d\n", a == a);
  923.     printf("%d\n", a != a);
  924.  
  925.     printf("%d\n", a < b);
  926.     printf("%d\n", a <= b);
  927.     printf("%d\n", a <= a);
  928.     printf("%d\n", b >= a);
  929.     printf("%d\n", a >= a);
  930.     printf("%d\n", b > a);
  931.  
  932.     printf("%d\n", (unsigned)a < b);
  933.     printf("%d\n", (unsigned)a <= b);
  934.     printf("%d\n", (unsigned)a <= a);
  935.     printf("%d\n", (unsigned)b >= a);
  936.     printf("%d\n", (unsigned)a >= a);
  937.     printf("%d\n", (unsigned)b > a);
  938. }
  939.  
  940. struct empty {
  941. };
  942.  
  943. struct aligntest1 {
  944.     char a[10];
  945. };
  946.  
  947. struct aligntest2 {
  948.     int a;
  949.     char b[10];
  950. };
  951.  
  952. struct aligntest3 {
  953.     double a, b;
  954. };
  955.  
  956. struct aligntest4 {
  957.     double a[0];
  958. };
  959.  
  960. void struct_test()
  961. {
  962.     struct1 *s;
  963.     union union2 u;
  964.  
  965.     printf("struct:\n");
  966.     printf("sizes: %d %d %d %d\n",
  967.            sizeof(struct struct1),
  968.            sizeof(struct struct2),
  969.            sizeof(union union1),
  970.            sizeof(union union2));
  971.     st1.f1 = 1;
  972.     st1.f2 = 2;
  973.     st1.f3 = 3;
  974.     printf("st1: %d %d %d\n",
  975.            st1.f1, st1.f2, st1.f3);
  976.     st1.u.v1 = 1;
  977.     st1.u.v2 = 2;
  978.     printf("union1: %d\n", st1.u.v1);
  979.     u.w1 = 1;
  980.     u.w2 = 2;
  981.     printf("union2: %d\n", u.w1);
  982.     s = &st2;
  983.     s->f1 = 3;
  984.     s->f2 = 2;
  985.     s->f3 = 1;
  986.     printf("st2: %d %d %d\n",
  987.            s->f1, s->f2, s->f3);
  988.     printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
  989.  
  990.     /* align / size tests */
  991.     printf("aligntest1 sizeof=%d alignof=%d\n",
  992.            sizeof(struct aligntest1), __alignof__(struct aligntest1));
  993.     printf("aligntest2 sizeof=%d alignof=%d\n",
  994.            sizeof(struct aligntest2), __alignof__(struct aligntest2));
  995.     printf("aligntest3 sizeof=%d alignof=%d\n",
  996.            sizeof(struct aligntest3), __alignof__(struct aligntest3));
  997.     printf("aligntest4 sizeof=%d alignof=%d\n",
  998.            sizeof(struct aligntest4), __alignof__(struct aligntest4));
  999.            
  1000.     /* empty structures (GCC extension) */
  1001.     printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
  1002.     printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
  1003. }
  1004.  
  1005. /* XXX: depend on endianness */
  1006. void char_short_test()
  1007. {
  1008.     int var1, var2;
  1009.  
  1010.     printf("char_short:\n");
  1011.  
  1012.     var1 = 0x01020304;
  1013.     var2 = 0xfffefdfc;
  1014.     printf("s8=%d %d\n",
  1015.            *(char *)&var1, *(char *)&var2);
  1016.     printf("u8=%d %d\n",
  1017.            *(unsigned char *)&var1, *(unsigned char *)&var2);
  1018.     printf("s16=%d %d\n",
  1019.            *(short *)&var1, *(short *)&var2);
  1020.     printf("u16=%d %d\n",
  1021.            *(unsigned short *)&var1, *(unsigned short *)&var2);
  1022.     printf("s32=%d %d\n",
  1023.            *(int *)&var1, *(int *)&var2);
  1024.     printf("u32=%d %d\n",
  1025.            *(unsigned int *)&var1, *(unsigned int *)&var2);
  1026.     *(char *)&var1 = 0x08;
  1027.     printf("var1=%x\n", var1);
  1028.     *(short *)&var1 = 0x0809;
  1029.     printf("var1=%x\n", var1);
  1030.     *(int *)&var1 = 0x08090a0b;
  1031.     printf("var1=%x\n", var1);
  1032. }
  1033.  
  1034. /******************/
  1035.  
  1036. typedef struct Sym {
  1037.     int v;
  1038.     int t;
  1039.     int c;
  1040.     struct Sym *next;
  1041.     struct Sym *prev;
  1042. } Sym;
  1043.  
  1044. #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
  1045. #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
  1046.  
  1047. static int toupper1(int a)
  1048. {
  1049.     return TOUPPER(a);
  1050. }
  1051.  
  1052. void bool_test()
  1053. {
  1054.     int *s, a, b, t, f, i;
  1055.  
  1056.     a = 0;
  1057.     s = (void*)0;
  1058.     printf("!s=%d\n", !s);
  1059.  
  1060.     if (!s || !s[0])
  1061.         a = 1;
  1062.     printf("a=%d\n", a);
  1063.  
  1064.     printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
  1065.     printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
  1066.     printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
  1067. #if 1 && 1
  1068.     printf("a1\n");
  1069. #endif
  1070. #if 1 || 0
  1071.     printf("a2\n");
  1072. #endif
  1073. #if 1 ? 0 : 1
  1074.     printf("a3\n");
  1075. #endif
  1076. #if 0 ? 0 : 1
  1077.     printf("a4\n");
  1078. #endif
  1079.  
  1080.     a = 4;
  1081.     printf("b=%d\n", a + (0 ? 1 : a / 2));
  1082.  
  1083.     /* test register spilling */
  1084.     a = 10;
  1085.     b = 10;
  1086.     a = (a + b) * ((a < b) ?
  1087.                    ((b - a) * (a - b)): a + b);
  1088.     printf("a=%d\n", a);
  1089.  
  1090.     /* test complex || or && expressions */
  1091.     t = 1;
  1092.     f = 0;
  1093.     a = 32;
  1094.     printf("exp=%d\n", f == (32 <= a && a <= 3));
  1095.     printf("r=%d\n", (t || f) + (t && f));
  1096.  
  1097.     /* test ? : cast */
  1098.     {
  1099.         int aspect_on;
  1100.         int aspect_native = 65536;
  1101.         double bfu_aspect = 1.0;
  1102.         int aspect;
  1103.         for(aspect_on = 0; aspect_on < 2; aspect_on++) {
  1104.             aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
  1105.             printf("aspect=%d\n", aspect);
  1106.         }
  1107.     }
  1108.  
  1109.     /* test ? : GCC extension */
  1110.     {
  1111.         static int v1 = 34 ? : -1; /* constant case */
  1112.         static int v2 = 0 ? : -1; /* constant case */
  1113.         int a = 30;
  1114.        
  1115.         printf("%d %d\n", v1, v2);
  1116.         printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
  1117.     }
  1118.  
  1119.     /* again complex expression */
  1120.     for(i=0;i<256;i++) {
  1121.         if (toupper1 (i) != TOUPPER (i))
  1122.             printf("error %d\n", i);
  1123.     }
  1124. }
  1125.  
  1126. /* GCC accepts that */
  1127. static int tab_reinit[];
  1128. static int tab_reinit[10];
  1129.  
  1130. //int cinit1; /* a global variable can be defined several times without error ! */
  1131. int cinit1;
  1132. int cinit1;
  1133. int cinit1 = 0;
  1134. int *cinit2 = (int []){3, 2, 1};
  1135.  
  1136. void compound_literal_test(void)
  1137. {
  1138.     int *p, i;
  1139.     char *q, *q3;
  1140.  
  1141.     printf("compound_test:\n");
  1142.  
  1143.     p = (int []){1, 2, 3};
  1144.     for(i=0;i<3;i++)
  1145.         printf(" %d", p[i]);
  1146.     printf("\n");
  1147.  
  1148.     for(i=0;i<3;i++)
  1149.         printf("%d", cinit2[i]);
  1150.     printf("\n");
  1151.  
  1152.     q = "tralala1";
  1153.     printf("q1=%s\n", q);
  1154.  
  1155.     q = (char *){ "tralala2" };
  1156.     printf("q2=%s\n", q);
  1157.  
  1158.     q3 = (char *){ q };
  1159.     printf("q3=%s\n", q3);
  1160.  
  1161.     q = (char []){ "tralala3" };
  1162.     printf("q4=%s\n", q);
  1163.  
  1164. #ifdef ALL_ISOC99
  1165.     p = (int []){1, 2, cinit1 + 3};
  1166.     for(i=0;i<3;i++)
  1167.         printf(" %d", p[i]);
  1168.     printf("\n");
  1169.  
  1170.     for(i=0;i<3;i++) {
  1171.         p = (int []){1, 2, 4 + i};
  1172.         printf("%d %d %d\n",
  1173.                p[0],
  1174.                p[1],
  1175.                p[2]);
  1176.     }
  1177. #endif
  1178. }
  1179.  
  1180. /* K & R protos */
  1181.  
  1182. kr_func1(a, b)
  1183. {
  1184.     return a + b;
  1185. }
  1186.  
  1187. int kr_func2(a, b)
  1188. {
  1189.     return a + b;
  1190. }
  1191.  
  1192. kr_test()
  1193. {
  1194.     printf("kr_test:\n");
  1195.     printf("func1=%d\n", kr_func1(3, 4));
  1196.     printf("func2=%d\n", kr_func2(3, 4));
  1197.     return 0;
  1198. }
  1199.  
  1200. void num(int n)
  1201. {
  1202.     char *tab, *p;
  1203.     tab = (char*)malloc(20);
  1204.     p = tab;
  1205.     while (1) {
  1206.         *p = 48 + (n % 10);
  1207.         p++;
  1208.         n = n / 10;
  1209.         if (n == 0)
  1210.             break;
  1211.     }
  1212.     while (p != tab) {
  1213.         p--;
  1214.         printf("%c", *p);
  1215.     }
  1216.     printf("\n");
  1217.     free(tab);
  1218. }
  1219.  
  1220. /* structure assignment tests */
  1221. struct structa1 {
  1222.     int f1;
  1223.     char f2;
  1224. };
  1225.  
  1226. struct structa1 ssta1;
  1227.  
  1228. void struct_assign_test1(struct structa1 s1, int t, float f)
  1229. {
  1230.     printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
  1231. }
  1232.  
  1233. struct structa1 struct_assign_test2(struct structa1 s1, int t)
  1234. {
  1235.     s1.f1 += t;
  1236.     s1.f2 -= t;
  1237.     return s1;
  1238. }
  1239.  
  1240. void struct_assign_test(void)
  1241. {
  1242.     struct S {
  1243.       struct structa1 lsta1, lsta2;
  1244.       int i;
  1245.     } s, *ps;
  1246.    
  1247.     ps = &s;
  1248.     ps->i = 4;
  1249. #if 0
  1250.     printf("struct_assign_test:\n");
  1251.  
  1252.     s.lsta1.f1 = 1;
  1253.     s.lsta1.f2 = 2;
  1254.     printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
  1255.     s.lsta2 = s.lsta1;
  1256.     printf("%d %d\n", s.lsta2.f1, s.lsta2.f2);
  1257. #else
  1258.     s.lsta2.f1 = 1;
  1259.     s.lsta2.f2 = 2;
  1260. #endif
  1261.     struct_assign_test1(ps->lsta2, 3, 4.5);
  1262.    
  1263.     printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
  1264.     ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
  1265.     printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
  1266.  
  1267.     static struct {
  1268.         void (*elem)();
  1269.     } t[] = {
  1270.         /* XXX: we should allow this even without braces */
  1271.         { struct_assign_test }
  1272.     };
  1273.     printf("%d\n", struct_assign_test == t[0].elem);
  1274. }
  1275.  
  1276. /* casts to short/char */
  1277.  
  1278. void cast1(char a, short b, unsigned char c, unsigned short d)
  1279. {
  1280.     printf("%d %d %d %d\n", a, b, c, d);
  1281. }
  1282.  
  1283. char bcast;
  1284. short scast;
  1285.  
  1286. void cast_test()
  1287. {
  1288.     int a;
  1289.     char c;
  1290.     char tab[10];
  1291.     unsigned b,d;
  1292.     short s;
  1293.     char *p = NULL;
  1294.     p -= 0x700000000042;
  1295.  
  1296.     printf("cast_test:\n");
  1297.     a = 0xfffff;
  1298.     cast1(a, a, a, a);
  1299.     a = 0xffffe;
  1300.     printf("%d %d %d %d\n",
  1301.            (char)(a + 1),
  1302.            (short)(a + 1),
  1303.            (unsigned char)(a + 1),
  1304.            (unsigned short)(a + 1));
  1305.     printf("%d %d %d %d\n",
  1306.            (char)0xfffff,
  1307.            (short)0xfffff,
  1308.            (unsigned char)0xfffff,
  1309.            (unsigned short)0xfffff);
  1310.  
  1311.     a = (bcast = 128) + 1;
  1312.     printf("%d\n", a);
  1313.     a = (scast = 65536) + 1;
  1314.     printf("%d\n", a);
  1315.    
  1316.     printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
  1317.    
  1318.     /* test cast from unsigned to signed short to int */
  1319.     b = 0xf000;
  1320.     d = (short)b;
  1321.     printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
  1322.     b = 0xf0f0;
  1323.     d = (char)b;
  1324.     printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
  1325.    
  1326.     /* test implicit int casting for array accesses */
  1327.     c = 0;
  1328.     tab[1] = 2;
  1329.     tab[c] = 1;
  1330.     printf("%d %d\n", tab[0], tab[1]);
  1331.  
  1332.     /* test implicit casting on some operators */
  1333.     printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
  1334.     printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
  1335.     printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
  1336.  
  1337.     /* from pointer to integer types */
  1338.     printf("%d %d %ld %ld %lld %lld\n",
  1339.            (int)p, (unsigned int)p,
  1340.            (long)p, (unsigned long)p,
  1341.            (long long)p, (unsigned long long)p);
  1342.  
  1343.     /* from integers to pointers */
  1344.     printf("%p %p %p %p\n",
  1345.            (void *)a, (void *)b, (void *)c, (void *)d);
  1346. }
  1347.  
  1348. /* initializers tests */
  1349. struct structinit1 {
  1350.     int f1;
  1351.     char f2;
  1352.     short f3;
  1353.     int farray[3];
  1354. };
  1355.  
  1356. int sinit1 = 2;
  1357. int sinit2 = { 3 };
  1358. int sinit3[3] = { 1, 2, {{3}}, };
  1359. int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
  1360. int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
  1361. int sinit6[] = { 1, 2, 3 };
  1362. int sinit7[] = { [2] = 3, [0] = 1, 2 };
  1363. char sinit8[] = "hello" "trala";
  1364.  
  1365. struct structinit1 sinit9 = { 1, 2, 3 };
  1366. struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
  1367. struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
  1368. #ifdef ALL_ISOC99
  1369.                                .farray[0] = 10,
  1370.                                .farray[1] = 11,
  1371.                                .farray[2] = 12,
  1372. #endif
  1373. };
  1374.  
  1375. char *sinit12 = "hello world";
  1376. char *sinit13[] = {
  1377.     "test1",
  1378.     "test2",
  1379.     "test3",
  1380. };
  1381. char sinit14[10] = { "abc" };
  1382. int sinit15[3] = { sizeof(sinit15), 1, 2 };
  1383.  
  1384. struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
  1385.  
  1386. struct bar {
  1387.         char *s;
  1388.         int len;
  1389. } sinit17[] = {
  1390.         "a1", 4,
  1391.         "a2", 1
  1392. };
  1393.  
  1394. int sinit18[10] = {
  1395.     [2 ... 5] = 20,
  1396.     2,
  1397.     [8] = 10,
  1398. };
  1399.  
  1400. struct complexinit0 {
  1401.     int a;
  1402.     int b;
  1403. };
  1404.  
  1405. struct complexinit {
  1406.     int a;
  1407.     const struct complexinit0 *b;
  1408. };
  1409.  
  1410. const static struct complexinit cix[] = {
  1411.     [0] = {
  1412.         .a = 2000,
  1413.         .b = (const struct complexinit0[]) {
  1414.                 { 2001, 2002 },
  1415.                 { 2003, 2003 },
  1416.                 {}
  1417.         }
  1418.     }
  1419. };
  1420.  
  1421. struct complexinit2 {
  1422.         int a;
  1423.         int b[];
  1424. };
  1425.  
  1426. struct complexinit2 cix20;
  1427.  
  1428. struct complexinit2 cix21 = {
  1429.         .a = 3000,
  1430.         .b = { 3001, 3002, 3003 }
  1431. };
  1432.  
  1433. struct complexinit2 cix22 = {
  1434.         .a = 4000,
  1435.         .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
  1436. };
  1437.  
  1438. void init_test(void)
  1439. {
  1440.     int linit1 = 2;
  1441.     int linit2 = { 3 };
  1442.     int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
  1443.     int linit6[] = { 1, 2, 3 };
  1444.     int i, j;
  1445.     char linit8[] = "hello" "trala";
  1446.     int linit12[10] = { 1, 2 };
  1447.     int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
  1448.     char linit14[10] = "abc";
  1449.     int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
  1450.     struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
  1451.     int linit17 = sizeof(linit17);
  1452.    
  1453.     printf("init_test:\n");
  1454.  
  1455.     printf("sinit1=%d\n", sinit1);
  1456.     printf("sinit2=%d\n", sinit2);
  1457.     printf("sinit3=%d %d %d %d\n",
  1458.            sizeof(sinit3),
  1459.            sinit3[0],
  1460.            sinit3[1],
  1461.            sinit3[2]
  1462.            );
  1463.     printf("sinit6=%d\n", sizeof(sinit6));
  1464.     printf("sinit7=%d %d %d %d\n",
  1465.            sizeof(sinit7),
  1466.            sinit7[0],
  1467.            sinit7[1],
  1468.            sinit7[2]
  1469.            );
  1470.     printf("sinit8=%s\n", sinit8);
  1471.     printf("sinit9=%d %d %d\n",
  1472.            sinit9.f1,
  1473.            sinit9.f2,
  1474.            sinit9.f3
  1475.            );
  1476.     printf("sinit10=%d %d %d\n",
  1477.            sinit10.f1,
  1478.            sinit10.f2,
  1479.            sinit10.f3
  1480.            );
  1481.     printf("sinit11=%d %d %d %d %d %d\n",
  1482.            sinit11.f1,
  1483.            sinit11.f2,
  1484.            sinit11.f3,
  1485.            sinit11.farray[0],
  1486.            sinit11.farray[1],
  1487.            sinit11.farray[2]
  1488.            );
  1489.  
  1490.     for(i=0;i<3;i++)
  1491.         for(j=0;j<2;j++)
  1492.             printf("[%d][%d] = %d %d %d\n",
  1493.                    i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
  1494.     printf("linit1=%d\n", linit1);
  1495.     printf("linit2=%d\n", linit2);
  1496.     printf("linit6=%d\n", sizeof(linit6));
  1497.     printf("linit8=%d %s\n", sizeof(linit8), linit8);
  1498.  
  1499.     printf("sinit12=%s\n", sinit12);
  1500.     printf("sinit13=%d %s %s %s\n",
  1501.            sizeof(sinit13),
  1502.            sinit13[0],
  1503.            sinit13[1],
  1504.            sinit13[2]);
  1505.     printf("sinit14=%s\n", sinit14);
  1506.  
  1507.     for(i=0;i<10;i++) printf(" %d", linit12[i]);
  1508.     printf("\n");
  1509.     for(i=0;i<10;i++) printf(" %d", linit13[i]);
  1510.     printf("\n");
  1511.     for(i=0;i<10;i++) printf(" %d", linit14[i]);
  1512.     printf("\n");
  1513.     for(i=0;i<10;i++) printf(" %d", linit15[i]);
  1514.     printf("\n");
  1515.     printf("%d %d %d %d\n",
  1516.            linit16.a1,
  1517.            linit16.a2,
  1518.            linit16.a3,
  1519.            linit16.a4);
  1520.     /* test that initialisation is done after variable declare */
  1521.     printf("linit17=%d\n", linit17);
  1522.     printf("sinit15=%d\n", sinit15[0]);
  1523.     printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
  1524.     printf("sinit17=%s %d %s %d\n",
  1525.            sinit17[0].s, sinit17[0].len,
  1526.            sinit17[1].s, sinit17[1].len);
  1527.     for(i=0;i<10;i++)
  1528.         printf("%x ", sinit18[i]);
  1529.     printf("\n");
  1530.     /* complex init check */
  1531.     printf("cix: %d %d %d %d %d %d %d\n",
  1532.         cix[0].a,
  1533.         cix[0].b[0].a, cix[0].b[0].b,
  1534.         cix[0].b[1].a, cix[0].b[1].b,
  1535.         cix[0].b[2].a, cix[0].b[2].b);
  1536.     printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
  1537.     printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
  1538. }
  1539.  
  1540.  
  1541. void switch_test()
  1542. {
  1543.     int i;
  1544.  
  1545.     for(i=0;i<15;i++) {
  1546.         switch(i) {
  1547.         case 0:
  1548.         case 1:
  1549.             printf("a");
  1550.             break;
  1551.         default:
  1552.             printf("%d", i);
  1553.             break;
  1554.         case 8 ... 12:
  1555.             printf("c");
  1556.             break;
  1557.         case 3:
  1558.             printf("b");
  1559.             break;
  1560.         }
  1561.     }
  1562.     printf("\n");
  1563. }
  1564.  
  1565. /* ISOC99 _Bool type */
  1566. void c99_bool_test(void)
  1567. {
  1568. #ifdef BOOL_ISOC99
  1569.     int a;
  1570.     _Bool b;
  1571.  
  1572.     printf("bool_test:\n");
  1573.     printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
  1574.     a = 3;
  1575.     printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
  1576.     b = 3;
  1577.     printf("b = %d\n", b);
  1578.     b++;
  1579.     printf("b = %d\n", b);
  1580. #endif
  1581. }
  1582.  
  1583. void bitfield_test(void)
  1584. {
  1585.     int a;
  1586.     short sa;
  1587.     unsigned char ca;
  1588.     struct sbf1 {
  1589.         int f1 : 3;
  1590.         int : 2;
  1591.         int f2 : 1;
  1592.         int : 0;
  1593.         int f3 : 5;
  1594.         int f4 : 7;
  1595.         unsigned int f5 : 7;
  1596.     } st1;
  1597.     printf("bitfield_test:");
  1598.     printf("sizeof(st1) = %d\n", sizeof(st1));
  1599.  
  1600.     st1.f1 = 3;
  1601.     st1.f2 = 1;
  1602.     st1.f3 = 15;
  1603.     a = 120;
  1604.     st1.f4 = a;
  1605.     st1.f5 = a;
  1606.     st1.f5++;
  1607.     printf("%d %d %d %d %d\n",
  1608.            st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
  1609.     sa = st1.f5;
  1610.     ca = st1.f5;
  1611.     printf("%d %d\n", sa, ca);
  1612.  
  1613.     st1.f1 = 7;
  1614.     if (st1.f1 == -1)
  1615.         printf("st1.f1 == -1\n");
  1616.     else
  1617.         printf("st1.f1 != -1\n");
  1618.     if (st1.f2 == -1)
  1619.         printf("st1.f2 == -1\n");
  1620.     else
  1621.         printf("st1.f2 != -1\n");
  1622.  
  1623.     /* bit sizes below must be bigger than 32 since GCC doesn't allow
  1624.        long-long bitfields whose size is not bigger than int */
  1625.     struct sbf2 {
  1626.         long long f1 : 45;
  1627.         long long : 2;
  1628.         long long f2 : 35;
  1629.         unsigned long long f3 : 38;
  1630.     } st2;
  1631.     st2.f1 = 0x123456789ULL;
  1632.     a = 120;
  1633.     st2.f2 = (long long)a << 25;
  1634.     st2.f3 = a;
  1635.     st2.f2++;
  1636.     printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
  1637. }
  1638.  
  1639. #ifdef __x86_64__
  1640. #define FLOAT_FMT "%f\n"
  1641. #else
  1642. /* x86's float isn't compatible with GCC */
  1643. #define FLOAT_FMT "%.5f\n"
  1644. #endif
  1645.  
  1646. /* declare strto* functions as they are C99 */
  1647. double strtod(const char *nptr, char **endptr);
  1648.  
  1649. #if defined(_WIN32)
  1650. float strtof(const char *nptr, char **endptr) {return (float)strtod(nptr, endptr);}
  1651. LONG_DOUBLE strtold(const char *nptr, char **endptr) {return (LONG_DOUBLE)strtod(nptr, endptr);}
  1652. #else
  1653. float strtof(const char *nptr, char **endptr);
  1654. LONG_DOUBLE strtold(const char *nptr, char **endptr);
  1655. #endif
  1656.  
  1657. #define FTEST(prefix, typename, type, fmt)\
  1658. void prefix ## cmp(type a, type b)\
  1659. {\
  1660.     printf("%d %d %d %d %d %d\n",\
  1661.            a == b,\
  1662.            a != b,\
  1663.            a < b,\
  1664.            a > b,\
  1665.            a >= b,\
  1666.            a <= b);\
  1667.     printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
  1668.            a,\
  1669.            b,\
  1670.            a + b,\
  1671.            a - b,\
  1672.            a * b,\
  1673.            a / b,\
  1674.            -a);\
  1675.     printf(fmt "\n", ++a);\
  1676.     printf(fmt "\n", a++);\
  1677.     printf(fmt "\n", a);\
  1678.     b = 0;\
  1679.     printf("%d %d\n", !a, !b);\
  1680. }\
  1681. void prefix ## fcast(type a)\
  1682. {\
  1683.     float fa;\
  1684.     double da;\
  1685.     LONG_DOUBLE la;\
  1686.     int ia;\
  1687.     long long llia;\
  1688.     unsigned int ua;\
  1689.     unsigned long long llua;\
  1690.     type b;\
  1691.     fa = a;\
  1692.     da = a;\
  1693.     la = a;\
  1694.     printf("ftof: %f %f %Lf\n", fa, da, la);\
  1695.     ia = (int)a;\
  1696.     llia = (long long)a;\
  1697.     a = (a >= 0) ? a : -a;\
  1698.     ua = (unsigned int)a;\
  1699.     llua = (unsigned long long)a;\
  1700.     printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
  1701.     ia = -1234;\
  1702.     ua = 0x81234500;\
  1703.     llia = -0x123456789012345LL;\
  1704.     llua = 0xf123456789012345LLU;\
  1705.     b = ia;\
  1706.     printf("itof: " fmt "\n", b);\
  1707.     b = ua;\
  1708.     printf("utof: " fmt "\n", b);\
  1709.     b = llia;\
  1710.     printf("lltof: " fmt "\n", b);\
  1711.     b = llua;\
  1712.     printf("ulltof: " fmt "\n", b);\
  1713. }\
  1714. \
  1715. float prefix ## retf(type a) { return a; }\
  1716. double prefix ## retd(type a) { return a; }\
  1717. LONG_DOUBLE prefix ## retld(type a) { return a; }\
  1718. \
  1719. void prefix ## call(void)\
  1720. {\
  1721.     printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
  1722.     printf("double: %f\n", prefix ## retd(42.123456789));\
  1723.     printf("long double: %Lf\n", prefix ## retld(42.123456789));\
  1724.     printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
  1725. }\
  1726. \
  1727. void prefix ## signed_zeros(void) \
  1728. {\
  1729.   type x = 0.0, y = -0.0, n, p;\
  1730.   if (x == y)\
  1731.     printf ("Test 1.0 / x != 1.0 / y  returns %d (should be 1).\n",\
  1732.             1.0 / x != 1.0 / y);\
  1733.   else\
  1734.     printf ("x != y; this is wrong!\n");\
  1735. \
  1736.   n = -x;\
  1737.   if (x == n)\
  1738.     printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
  1739.             1.0 / x != 1.0 / n);\
  1740.   else\
  1741.     printf ("x != -x; this is wrong!\n");\
  1742. \
  1743.   p = +y;\
  1744.   if (x == p)\
  1745.     printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
  1746.             1.0 / x != 1.0 / p);\
  1747.   else\
  1748.     printf ("x != +y; this is wrong!\n");\
  1749.   p = -y;\
  1750.   if (x == p)\
  1751.     printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
  1752.             1.0 / x != 1.0 / p);\
  1753.   else\
  1754.     printf ("x != -y; this is wrong!\n");\
  1755. }\
  1756. void prefix ## test(void)\
  1757. {\
  1758.     printf("testing '%s'\n", #typename);\
  1759.     prefix ## cmp(1, 2.5);\
  1760.     prefix ## cmp(2, 1.5);\
  1761.     prefix ## cmp(1, 1);\
  1762.     prefix ## fcast(234.6);\
  1763.     prefix ## fcast(-2334.6);\
  1764.     prefix ## call();\
  1765.     prefix ## signed_zeros();\
  1766. }
  1767.  
  1768. FTEST(f, float, float, "%f")
  1769. FTEST(d, double, double, "%f")
  1770. FTEST(ld, long double, LONG_DOUBLE, "%Lf")
  1771.  
  1772. double ftab1[3] = { 1.2, 3.4, -5.6 };
  1773.  
  1774.  
  1775. void float_test(void)
  1776. {
  1777. #if !defined(__arm__) || defined(__ARM_PCS_VFP)
  1778.     float fa, fb;
  1779.     double da, db;
  1780.     int a;
  1781.     unsigned int b;
  1782.  
  1783.     printf("float_test:\n");
  1784.     printf("sizeof(float) = %d\n", sizeof(float));
  1785.     printf("sizeof(double) = %d\n", sizeof(double));
  1786.     printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));
  1787.     ftest();
  1788.     dtest();
  1789.     ldtest();
  1790.     printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
  1791.     printf("%f %f %f\n", 2.12, .5, 2.3e10);
  1792.     //    printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
  1793.     da = 123;
  1794.     printf("da=%f\n", da);
  1795.     fa = 123;
  1796.     printf("fa=%f\n", fa);
  1797.     a = 4000000000;
  1798.     da = a;
  1799.     printf("da = %f\n", da);
  1800.     b = 4000000000;
  1801.     db = b;
  1802.     printf("db = %f\n", db);
  1803. #endif
  1804. }
  1805.  
  1806. int fib(int n)
  1807. {
  1808.     if (n <= 2)
  1809.         return 1;
  1810.     else
  1811.         return fib(n-1) + fib(n-2);
  1812. }
  1813.  
  1814. void funcptr_test()
  1815. {
  1816.     void (*func)(int);
  1817.     int a;
  1818.     struct {
  1819.         int dummy;
  1820.         void (*func)(int);
  1821.     } st1;
  1822.  
  1823.     printf("funcptr:\n");
  1824.     func = &num;
  1825.     (*func)(12345);
  1826.     func = num;
  1827.     a = 1;
  1828.     a = 1;
  1829.     func(12345);
  1830.     /* more complicated pointer computation */
  1831.     st1.func = num;
  1832.     st1.func(12346);
  1833.     printf("sizeof1 = %d\n", sizeof(funcptr_test));
  1834.     printf("sizeof2 = %d\n", sizeof funcptr_test);
  1835.     printf("sizeof3 = %d\n", sizeof(&funcptr_test));
  1836.     printf("sizeof4 = %d\n", sizeof &funcptr_test);
  1837. }
  1838.  
  1839. void lloptest(long long a, long long b)
  1840. {
  1841.     unsigned long long ua, ub;
  1842.  
  1843.     ua = a;
  1844.     ub = b;
  1845.     /* arith */
  1846.     printf("arith: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
  1847.            a + b,
  1848.            a - b,
  1849.            a * b);
  1850.    
  1851.     if (b != 0) {
  1852.         printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
  1853.            a / b,
  1854.            a % b);
  1855.     }
  1856.  
  1857.     /* binary */
  1858.     printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
  1859.            a & b,
  1860.            a | b,
  1861.            a ^ b);
  1862.  
  1863.     /* tests */
  1864.     printf("test: %d %d %d %d %d %d\n",
  1865.            a == b,
  1866.            a != b,
  1867.            a < b,
  1868.            a > b,
  1869.            a >= b,
  1870.            a <= b);
  1871.    
  1872.     printf("utest: %d %d %d %d %d %d\n",
  1873.            ua == ub,
  1874.            ua != ub,
  1875.            ua < ub,
  1876.            ua > ub,
  1877.            ua >= ub,
  1878.            ua <= ub);
  1879.  
  1880.     /* arith2 */
  1881.     a++;
  1882.     b++;
  1883.     printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
  1884.     printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a++, b++);
  1885.     printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", --a, --b);
  1886.     printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
  1887.     b = ub = 0;
  1888.     printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
  1889. }
  1890.  
  1891. void llshift(long long a, int b)
  1892. {
  1893.     printf("shift: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
  1894.            (unsigned long long)a >> b,
  1895.            a >> b,
  1896.            a << b);
  1897.     printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
  1898.            (unsigned long long)a >> 3,
  1899.            a >> 3,
  1900.            a << 3);
  1901.     printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
  1902.            (unsigned long long)a >> 35,
  1903.            a >> 35,
  1904.            a << 35);
  1905. }
  1906.  
  1907. void llfloat(void)
  1908. {
  1909.     float fa;
  1910.     double da;
  1911.     LONG_DOUBLE lda;
  1912.     long long la, lb, lc;
  1913.     unsigned long long ula, ulb, ulc;
  1914.     la = 0x12345678;
  1915.     ula = 0x72345678;
  1916.     la = (la << 20) | 0x12345;
  1917.     ula = ula << 33;
  1918.     printf("la=" LONG_LONG_FORMAT " ula=" ULONG_LONG_FORMAT "\n", la, ula);
  1919.  
  1920.     fa = la;
  1921.     da = la;
  1922.     lda = la;
  1923.     printf("lltof: %f %f %Lf\n", fa, da, lda);
  1924.  
  1925.     la = fa;
  1926.     lb = da;
  1927.     lc = lda;
  1928.     printf("ftoll: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", la, lb, lc);
  1929.  
  1930.     fa = ula;
  1931.     da = ula;
  1932.     lda = ula;
  1933.     printf("ulltof: %f %f %Lf\n", fa, da, lda);
  1934.  
  1935.     ula = fa;
  1936.     ulb = da;
  1937.     ulc = lda;
  1938.     printf("ftoull: " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT "\n", ula, ulb, ulc);
  1939. }
  1940.  
  1941. long long llfunc1(int a)
  1942. {
  1943.     return a * 2;
  1944. }
  1945.  
  1946. struct S {
  1947.     int id;
  1948.     char item;
  1949. };
  1950.  
  1951. long long int value(struct S *v)
  1952. {
  1953.     return ((long long int)v->item);
  1954. }
  1955.  
  1956. void longlong_test(void)
  1957. {
  1958.     long long a, b, c;
  1959.     int ia;
  1960.     unsigned int ua;
  1961.     printf("longlong_test:\n");
  1962.     printf("sizeof(long long) = %d\n", sizeof(long long));
  1963.     ia = -1;
  1964.     ua = -2;
  1965.     a = ia;
  1966.     b = ua;
  1967.     printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
  1968.     printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %Lx\n",
  1969.            (long long)1,
  1970.            (long long)-2,
  1971.            1LL,
  1972.            0x1234567812345679);
  1973.     a = llfunc1(-3);
  1974.     printf(LONG_LONG_FORMAT "\n", a);
  1975.  
  1976.     lloptest(1000, 23);
  1977.     lloptest(0xff, 0x1234);
  1978.     b = 0x72345678 << 10;
  1979.     lloptest(-3, b);
  1980.     llshift(0x123, 5);
  1981.     llshift(-23, 5);
  1982.     b = 0x72345678LL << 10;
  1983.     llshift(b, 47);
  1984.  
  1985.     llfloat();
  1986. #if 1
  1987.     b = 0x12345678;
  1988.     a = -1;
  1989.     c = a + b;
  1990.     printf("%Lx\n", c);
  1991. #endif
  1992.  
  1993.     /* long long reg spill test */
  1994.     {
  1995.           struct S a;
  1996.  
  1997.           a.item = 3;
  1998.           printf("%lld\n", value(&a));
  1999.     }
  2000.     lloptest(0x80000000, 0);
  2001.  
  2002.     /* another long long spill test */
  2003.     {
  2004.         long long *p, v;
  2005.         v = 1;
  2006.         p = &v;
  2007.         p[0]++;
  2008.         printf("%lld\n", *p);
  2009.     }
  2010.  
  2011.     a = 68719476720LL;
  2012.     b = 4294967295LL;
  2013.     printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
  2014.  
  2015.     printf(LONG_LONG_FORMAT "\n", 0x123456789LLU);
  2016. }
  2017.  
  2018. void manyarg_test(void)
  2019. {
  2020.     LONG_DOUBLE ld = 1234567891234LL;
  2021.     printf("manyarg_test:\n");
  2022.     printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
  2023.            1, 2, 3, 4, 5, 6, 7, 8,
  2024.            0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
  2025.     printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  2026.            LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
  2027.            1, 2, 3, 4, 5, 6, 7, 8,
  2028.            0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  2029.            1234567891234LL, 987654321986LL,
  2030.            42.0, 43.0);
  2031.     printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  2032.            LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
  2033.            ld, 1, 2, 3, 4, 5, 6, 7, 8,
  2034.            0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  2035.            1234567891234LL, 987654321986LL,
  2036.            42.0, 43.0);
  2037.     printf("%d %d %d %d %d %d %d %d %Lf\n",
  2038.            1, 2, 3, 4, 5, 6, 7, 8, ld);
  2039.     printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  2040.            LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n",
  2041.            1, 2, 3, 4, 5, 6, 7, 8,
  2042.            0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  2043.            1234567891234LL, 987654321986LL,
  2044.            42.0, 43.0, ld);
  2045.     printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  2046.            "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n",
  2047.            1, 2, 3, 4, 5, 6, 7, 8,
  2048.            0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  2049.            ld, 1234567891234LL, 987654321986LL,
  2050.            42.0, 43.0, ld);
  2051. }
  2052.  
  2053. void vprintf1(const char *fmt, ...)
  2054. {
  2055.     va_list ap, aq;
  2056.     const char *p;
  2057.     int c, i;
  2058.     double d;
  2059.     long long ll;
  2060.     LONG_DOUBLE ld;
  2061.  
  2062.     va_start(aq, fmt);
  2063.     va_copy(ap, aq);
  2064.    
  2065.     p = fmt;
  2066.     for(;;) {
  2067.         c = *p;
  2068.         if (c == '\0')
  2069.             break;
  2070.         p++;
  2071.         if (c == '%') {
  2072.             c = *p;
  2073.             switch(c) {
  2074.             case '\0':
  2075.                 goto the_end;
  2076.             case 'd':
  2077.                 i = va_arg(ap, int);
  2078.                 printf("%d", i);
  2079.                 break;
  2080.             case 'f':
  2081.                 d = va_arg(ap, double);
  2082.                 printf("%f", d);
  2083.                 break;
  2084.             case 'l':
  2085.                 ll = va_arg(ap, long long);
  2086.                 printf(LONG_LONG_FORMAT, ll);
  2087.                 break;
  2088.             case 'F':
  2089.                 ld = va_arg(ap, LONG_DOUBLE);
  2090.                 printf("%Lf", ld);
  2091.                 break;
  2092.             }
  2093.             p++;
  2094.         } else {
  2095.             putchar(c);
  2096.         }
  2097.     }
  2098.  the_end:
  2099.     va_end(aq);
  2100.     va_end(ap);
  2101. }
  2102.  
  2103. struct myspace {
  2104.     short int profile;
  2105. };
  2106.  
  2107. void stdarg_for_struct(struct myspace bob, ...)
  2108. {
  2109.     struct myspace george, bill;
  2110.     va_list ap;
  2111.     short int validate;
  2112.  
  2113.     va_start(ap, bob);
  2114.     bill     = va_arg(ap, struct myspace);
  2115.     george   = va_arg(ap, struct myspace);
  2116.     validate = va_arg(ap, int);
  2117.     printf("stdarg_for_struct: %d %d %d %d\n",
  2118.            bob.profile, bill.profile, george.profile, validate);
  2119.     va_end(ap);
  2120. }
  2121.  
  2122. void stdarg_test(void)
  2123. {
  2124.     LONG_DOUBLE ld = 1234567891234LL;
  2125.     struct myspace bob;
  2126.  
  2127.     vprintf1("%d %d %d\n", 1, 2, 3);
  2128.     vprintf1("%f %d %f\n", 1.0, 2, 3.0);
  2129.     vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
  2130.     vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
  2131.     vprintf1("%d %f %l %F %d %f %l %F\n",
  2132.              1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
  2133.     vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
  2134.              1, 2, 3, 4, 5, 6, 7, 8,
  2135.              0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
  2136.     vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
  2137.              1, 2, 3, 4, 5, 6, 7, 8,
  2138.              0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
  2139.     vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  2140.              "%l %l %f %f\n",
  2141.              1, 2, 3, 4, 5, 6, 7, 8,
  2142.              0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  2143.              1234567891234LL, 987654321986LL,
  2144.              42.0, 43.0);
  2145.     vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  2146.              "%l %l %f %f\n",
  2147.              ld, 1, 2, 3, 4, 5, 6, 7, 8,
  2148.              0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  2149.              1234567891234LL, 987654321986LL,
  2150.              42.0, 43.0);
  2151.     vprintf1("%d %d %d %d %d %d %d %d %F\n",
  2152.              1, 2, 3, 4, 5, 6, 7, 8, ld);
  2153.     vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  2154.              "%l %l %f %f %F\n",
  2155.              1, 2, 3, 4, 5, 6, 7, 8,
  2156.              0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  2157.              1234567891234LL, 987654321986LL,
  2158.              42.0, 43.0, ld);
  2159.     vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  2160.              "%F %l %l %f %f %F\n",
  2161.              1, 2, 3, 4, 5, 6, 7, 8,
  2162.              0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  2163.              ld, 1234567891234LL, 987654321986LL,
  2164.              42.0, 43.0, ld);
  2165.  
  2166.     bob.profile = 42;
  2167.     stdarg_for_struct(bob, bob, bob, bob.profile);
  2168. }
  2169.  
  2170. void whitespace_test(void)
  2171. {
  2172.     char *str;
  2173.  
  2174. #if 1
  2175.     pri\
  2176. ntf("whitspace:\n");
  2177. #endif
  2178.     pf("N=%d\n", 2);
  2179.  
  2180. #ifdef CORRECT_CR_HANDLING
  2181.     pri\
  2182. ntf("aaa=%d\n", 3);
  2183. #endif
  2184.  
  2185.     pri\
  2186. \
  2187. ntf("min=%d\n", 4);
  2188.  
  2189. #ifdef ACCEPT_CR_IN_STRINGS
  2190.     printf("len1=%d\n", strlen("
  2191. "));
  2192. #ifdef CORRECT_CR_HANDLING
  2193.     str = "
  2194. ";
  2195.     printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
  2196. #endif
  2197.     printf("len1=%d\n", strlen("
  2198. a
  2199. "));
  2200. #endif /* ACCEPT_CR_IN_STRINGS */
  2201. }
  2202.  
  2203. int reltab[3] = { 1, 2, 3 };
  2204.  
  2205. int *rel1 = &reltab[1];
  2206. int *rel2 = &reltab[2];
  2207.  
  2208. void relocation_test(void)
  2209. {
  2210.     printf("*rel1=%d\n", *rel1);
  2211.     printf("*rel2=%d\n", *rel2);
  2212. }
  2213.  
  2214. void old_style_f(a,b,c)
  2215.      int a, b;
  2216.      double c;
  2217. {
  2218.     printf("a=%d b=%d b=%f\n", a, b, c);
  2219. }
  2220.  
  2221. void decl_func1(int cmpfn())
  2222. {
  2223.     printf("cmpfn=%lx\n", (long)cmpfn);
  2224. }
  2225.  
  2226. void decl_func2(cmpfn)
  2227. int cmpfn();
  2228. {
  2229.     printf("cmpfn=%lx\n", (long)cmpfn);
  2230. }
  2231.  
  2232. void old_style_function(void)
  2233. {
  2234.     old_style_f((void *)1, 2, 3.0);
  2235.     decl_func1(NULL);
  2236.     decl_func2(NULL);
  2237. }
  2238.  
  2239. #ifdef _0
  2240. void alloca_test()
  2241. {
  2242. #if defined __i386__ || defined __x86_64__ || defined __arm__
  2243.     char *p = alloca(16);
  2244.     strcpy(p,"123456789012345");
  2245.     printf("alloca: p is %s\n", p);
  2246.     char *demo = "This is only a test.\n";
  2247.     /* Test alloca embedded in a larger expression */
  2248.     printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
  2249. #endif
  2250. }
  2251. #endif
  2252.  
  2253. void *bounds_checking_is_enabled()
  2254. {
  2255.     char ca[10], *cp = ca-1;
  2256.     return (ca != cp + 1) ? cp : NULL;
  2257. }
  2258.  
  2259. typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
  2260.  
  2261. void c99_vla_test(int size1, int size2)
  2262. {
  2263. #if defined __i386__ || defined __x86_64__
  2264.     int size = size1 * size2;
  2265.     int tab1[size][2], tab2[10][2];
  2266.     void *tab1_ptr, *tab2_ptr, *bad_ptr;
  2267.  
  2268.     /* "size" should have been 'captured' at tab1 declaration,
  2269.         so modifying it should have no effect on VLA behaviour. */
  2270.     size = size-1;
  2271.    
  2272.     printf("Test C99 VLA 1 (sizeof): ");
  2273.     printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
  2274.     tab1_ptr = tab1;
  2275.     tab2_ptr = tab2;
  2276.     printf("Test C99 VLA 2 (ptrs subtract): ");
  2277.     printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
  2278.     printf("Test C99 VLA 3 (ptr add): ");
  2279.     printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
  2280.     printf("Test C99 VLA 4 (ptr access): ");
  2281.     tab1[size1][1] = 42;
  2282.     printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
  2283.  
  2284.     printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
  2285.     if (bad_ptr = bounds_checking_is_enabled()) {
  2286.         int *t1 = &tab1[size1 * size2 - 1][3];
  2287.         int *t2 = &tab2[9][3];
  2288.         printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
  2289.         printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
  2290.  
  2291.         char*c1 = 1 + sizeof(tab1) + (char*)tab1;
  2292.         char*c2 = 1 + sizeof(tab2) + (char*)tab2;
  2293.         printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
  2294.         printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
  2295.  
  2296.         int *i1 = tab1[-1];
  2297.         int *i2 = tab2[-1];
  2298.         printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
  2299.         printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
  2300.  
  2301.         int *x1 = tab1[size1 * size2 + 1];
  2302.         int *x2 = tab2[10 + 1];
  2303.         printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
  2304.         printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
  2305.     } else {
  2306.         printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
  2307.     }
  2308.     printf("\n");
  2309. #endif
  2310. }
  2311.  
  2312. typedef __SIZE_TYPE__ uintptr_t;
  2313.  
  2314. void sizeof_test(void)
  2315. {
  2316.     int a;
  2317.     int **ptr;
  2318.  
  2319.     printf("sizeof(int) = %d\n", sizeof(int));
  2320.     printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
  2321.     printf("sizeof(long) = %d\n", sizeof(long));
  2322.     printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
  2323.     printf("sizeof(short) = %d\n", sizeof(short));
  2324.     printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
  2325.     printf("sizeof(char) = %d\n", sizeof(char));
  2326.     printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
  2327.     printf("sizeof(func) = %d\n", sizeof sizeof_test());
  2328.     a = 1;
  2329.     printf("sizeof(a++) = %d\n", sizeof a++);
  2330.     printf("a=%d\n", a);
  2331.     ptr = NULL;
  2332.     printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
  2333.  
  2334.     /* The type of sizeof should be as large as a pointer, actually
  2335.        it should be size_t.  */
  2336.     printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
  2337.     uintptr_t t = 1;
  2338.     uintptr_t t2;
  2339.     /* Effectively <<32, but defined also on 32bit machines.  */
  2340.     t <<= 16;
  2341.     t <<= 16;
  2342.     t++;
  2343.     /* This checks that sizeof really can be used to manipulate
  2344.        uintptr_t objects, without truncation.  */
  2345.     t2 = t & -sizeof(uintptr_t);
  2346.     printf ("%lu %lu\n", t, t2);
  2347.  
  2348.     /* some alignof tests */
  2349.     printf("__alignof__(int) = %d\n", __alignof__(int));
  2350.     printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
  2351.     printf("__alignof__(short) = %d\n", __alignof__(short));
  2352.     printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
  2353.     printf("__alignof__(char) = %d\n", __alignof__(char));
  2354.     printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
  2355.     printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
  2356. }
  2357.  
  2358. void typeof_test(void)
  2359. {
  2360.     double a;
  2361.     typeof(a) b;
  2362.     typeof(float) c;
  2363.  
  2364.     a = 1.5;
  2365.     b = 2.5;
  2366.     c = 3.5;
  2367.     printf("a=%f b=%f c=%f\n", a, b, c);
  2368. }
  2369.  
  2370. void statement_expr_test(void)
  2371. {
  2372.     int a, i;
  2373.  
  2374.     a = 0;
  2375.     for(i=0;i<10;i++) {
  2376.         a += 1 +
  2377.             ( { int b, j;
  2378.                 b = 0;
  2379.                 for(j=0;j<5;j++)
  2380.                     b += j; b;
  2381.             } );
  2382.     }
  2383.     printf("a=%d\n", a);
  2384.    
  2385. }
  2386.  
  2387. void local_label_test(void)
  2388. {
  2389.     int a;
  2390.     goto l1;
  2391.  l2:
  2392.     a = 1 + ({
  2393.         __label__ l1, l2, l3, l4;
  2394.         goto l1;
  2395.     l4:
  2396.         printf("aa1\n");
  2397.         goto l3;
  2398.     l2:
  2399.         printf("aa3\n");
  2400.         goto l4;
  2401.     l1:
  2402.         printf("aa2\n");
  2403.         goto l2;
  2404.     l3:;
  2405.         1;
  2406.     });
  2407.     printf("a=%d\n", a);
  2408.     return;
  2409.  l4:
  2410.     printf("bb1\n");
  2411.     goto l2;
  2412.  l1:
  2413.     printf("bb2\n");
  2414.     goto l4;
  2415. }
  2416.  
  2417. /* inline assembler test */
  2418. #ifdef __i386__
  2419.  
  2420. /* from linux kernel */
  2421. static char * strncat1(char * dest,const char * src,size_t count)
  2422. {
  2423. int d0, d1, d2, d3;
  2424. __asm__ __volatile__(
  2425.         "repne\n\t"
  2426.         "scasb\n\t"
  2427.         "decl %1\n\t"
  2428.         "movl %8,%3\n"
  2429.         "1:\tdecl %3\n\t"
  2430.         "js 2f\n\t"
  2431.         "lodsb\n\t"
  2432.         "stosb\n\t"
  2433.         "testb %%al,%%al\n\t"
  2434.         "jne 1b\n"
  2435.         "2:\txorl %2,%2\n\t"
  2436.         "stosb"
  2437.         : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
  2438.         : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
  2439.         : "memory");
  2440. return dest;
  2441. }
  2442.  
  2443. static char * strncat2(char * dest,const char * src,size_t count)
  2444. {
  2445. int d0, d1, d2, d3;
  2446. __asm__ __volatile__(
  2447.         "repne scasb\n\t" /* one-line repne prefix + string op */
  2448.         "decl %1\n\t"
  2449.         "movl %8,%3\n"
  2450.         "1:\tdecl %3\n\t"
  2451.         "js 2f\n\t"
  2452.         "lodsb\n\t"
  2453.         "stosb\n\t"
  2454.         "testb %%al,%%al\n\t"
  2455.         "jne 1b\n"
  2456.         "2:\txorl %2,%2\n\t"
  2457.         "stosb"
  2458.         : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
  2459.         : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
  2460.         : "memory");
  2461. return dest;
  2462. }
  2463.  
  2464. static inline void * memcpy1(void * to, const void * from, size_t n)
  2465. {
  2466. int d0, d1, d2;
  2467. __asm__ __volatile__(
  2468.         "rep ; movsl\n\t"
  2469.         "testb $2,%b4\n\t"
  2470.         "je 1f\n\t"
  2471.         "movsw\n"
  2472.         "1:\ttestb $1,%b4\n\t"
  2473.         "je 2f\n\t"
  2474.         "movsb\n"
  2475.         "2:"
  2476.         : "=&c" (d0), "=&D" (d1), "=&S" (d2)
  2477.         :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
  2478.         : "memory");
  2479. return (to);
  2480. }
  2481.  
  2482. static inline void * memcpy2(void * to, const void * from, size_t n)
  2483. {
  2484. int d0, d1, d2;
  2485. __asm__ __volatile__(
  2486.         "rep movsl\n\t"  /* one-line rep prefix + string op */
  2487.         "testb $2,%b4\n\t"
  2488.         "je 1f\n\t"
  2489.         "movsw\n"
  2490.         "1:\ttestb $1,%b4\n\t"
  2491.         "je 2f\n\t"
  2492.         "movsb\n"
  2493.         "2:"
  2494.         : "=&c" (d0), "=&D" (d1), "=&S" (d2)
  2495.         :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
  2496.         : "memory");
  2497. return (to);
  2498. }
  2499.  
  2500. static __inline__ void sigaddset1(unsigned int *set, int _sig)
  2501. {
  2502.         __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
  2503. }
  2504.  
  2505. static __inline__ void sigdelset1(unsigned int *set, int _sig)
  2506. {
  2507.         asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
  2508. }
  2509.  
  2510. static __inline__ __const__ unsigned int swab32(unsigned int x)
  2511. {
  2512.         __asm__("xchgb %b0,%h0\n\t"     /* swap lower bytes     */
  2513.                 "rorl $16,%0\n\t"       /* swap words           */
  2514.                 "xchgb %b0,%h0"         /* swap higher bytes    */
  2515.                 :"=q" (x)
  2516.                 : "0" (x));
  2517.         return x;
  2518. }
  2519.  
  2520. static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
  2521. {
  2522.     unsigned long long res;
  2523.     __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
  2524.     return res;
  2525. }
  2526.  
  2527. static __inline__ unsigned long long inc64(unsigned long long a)
  2528. {
  2529.     unsigned long long res;
  2530.     __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
  2531.     return res;
  2532. }
  2533.  
  2534. unsigned int set;
  2535.  
  2536. void asm_test(void)
  2537. {
  2538.     char buf[128];
  2539.     unsigned int val;
  2540.  
  2541.     printf("inline asm:\n");
  2542.     /* test the no operand case */
  2543.     asm volatile ("xorl %eax, %eax");
  2544.  
  2545.     memcpy1(buf, "hello", 6);
  2546.     strncat1(buf, " worldXXXXX", 3);
  2547.     printf("%s\n", buf);
  2548.  
  2549.     memcpy2(buf, "hello", 6);
  2550.     strncat2(buf, " worldXXXXX", 3);
  2551.     printf("%s\n", buf);
  2552.  
  2553.     /* 'A' constraint test */
  2554.     printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
  2555.     printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
  2556.  
  2557.     set = 0xff;
  2558.     sigdelset1(&set, 2);
  2559.     sigaddset1(&set, 16);
  2560.     /* NOTE: we test here if C labels are correctly restored after the
  2561.        asm statement */
  2562.     goto label1;
  2563.  label2:
  2564.     __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
  2565. #ifdef __GNUC__ // works strange with GCC 4.3
  2566.     set=0x1080fd;
  2567. #endif
  2568.     printf("set=0x%x\n", set);
  2569.     val = 0x01020304;
  2570.     printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
  2571.     return;
  2572.  label1:
  2573.     goto label2;
  2574. }
  2575.  
  2576. #else
  2577.  
  2578. void asm_test(void)
  2579. {
  2580. }
  2581.  
  2582. #endif
  2583.  
  2584. #define COMPAT_TYPE(type1, type2) \
  2585. {\
  2586.     printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
  2587.            __builtin_types_compatible_p (type1, type2));\
  2588. }
  2589.  
  2590. int constant_p_var;
  2591.  
  2592. void builtin_test(void)
  2593. {
  2594. #if GCC_MAJOR >= 3
  2595.     COMPAT_TYPE(int, int);
  2596.     COMPAT_TYPE(int, unsigned int);
  2597.     COMPAT_TYPE(int, char);
  2598.     COMPAT_TYPE(int, const int);
  2599.     COMPAT_TYPE(int, volatile int);
  2600.     COMPAT_TYPE(int *, int *);
  2601.     COMPAT_TYPE(int *, void *);
  2602.     COMPAT_TYPE(int *, const int *);
  2603.     COMPAT_TYPE(char *, unsigned char *);
  2604.     COMPAT_TYPE(char *, signed char *);
  2605.     COMPAT_TYPE(char *, char *);
  2606. /* space is needed because tcc preprocessor introduces a space between each token */
  2607.     COMPAT_TYPE(char * *, void *);
  2608. #endif
  2609.     printf("res = %d\n", __builtin_constant_p(1));
  2610.     printf("res = %d\n", __builtin_constant_p(1 + 2));
  2611.     printf("res = %d\n", __builtin_constant_p(&constant_p_var));
  2612.     printf("res = %d\n", __builtin_constant_p(constant_p_var));
  2613. }
  2614.  
  2615. //#ifndef _WIN32
  2616. #ifdef __0
  2617. extern int __attribute__((weak)) weak_f1(void);
  2618. extern int __attribute__((weak)) weak_f2(void);
  2619. extern int                       weak_f3(void);
  2620. extern int __attribute__((weak)) weak_v1;
  2621. extern int __attribute__((weak)) weak_v2;
  2622. extern int                       weak_v3;
  2623.  
  2624. extern int                           (*weak_fpa)() __attribute__((weak));
  2625. extern int __attribute__((weak))     (*weak_fpb)();
  2626. extern     __attribute__((weak)) int (*weak_fpc)();
  2627.  
  2628. extern int                     weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
  2629. extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x")                    ;
  2630. extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
  2631. extern int                     weak_asm_v1       asm("weak_asm_v1x") __attribute((weak));
  2632. extern int __attribute((weak)) weak_asm_v2       asm("weak_asm_v2x")                    ;
  2633. extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
  2634.  
  2635. static const size_t dummy = 0;
  2636. extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
  2637. extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
  2638. extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
  2639.  
  2640. int some_lib_func(void);
  2641. int dummy_impl_of_slf(void) { return 444; }
  2642. int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
  2643.  
  2644. int weak_toolate() __attribute__((weak));
  2645. int weak_toolate() { return 0; }
  2646.  
  2647. void __attribute__((weak)) weak_test(void)
  2648. {
  2649.         printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
  2650.         printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
  2651.         printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
  2652.         printf("weak_v1=%d\n",&weak_v1 ? weak_v1   : 123);
  2653.         printf("weak_v2=%d\n",&weak_v2 ? weak_v2   : 123);
  2654.         printf("weak_v3=%d\n",&weak_v3 ? weak_v3   : 123);
  2655.  
  2656.         printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
  2657.         printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
  2658.         printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
  2659.        
  2660.         printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
  2661.         printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
  2662.         printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
  2663.         printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
  2664.         printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
  2665.         printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
  2666. }
  2667.  
  2668. int __attribute__((weak)) weak_f2() { return 222; }
  2669. int __attribute__((weak)) weak_f3() { return 333; }
  2670. int __attribute__((weak)) weak_v2 = 222;
  2671. int __attribute__((weak)) weak_v3 = 333;
  2672. #endif
  2673.  
  2674. void const_func(const int a)
  2675. {
  2676. }
  2677.  
  2678. void const_warn_test(void)
  2679. {
  2680.     const_func(1);
  2681. }
  2682.  
  2683. struct condstruct {
  2684.   int i;
  2685. };
  2686.  
  2687. int getme (struct condstruct *s, int i)
  2688. {
  2689.   int i1 = (i == 0 ? 0 : s)->i;
  2690.   int i2 = (i == 0 ? s : 0)->i;
  2691.   int i3 = (i == 0 ? (void*)0 : s)->i;
  2692.   int i4 = (i == 0 ? s : (void*)0)->i;
  2693.   return i1 + i2 + i3 + i4;
  2694. }
  2695.  
  2696. struct global_data
  2697. {
  2698.   int a[40];
  2699.   int *b[40];
  2700. };
  2701.  
  2702. struct global_data global_data;
  2703.  
  2704. int global_data_getstuff (int *, int);
  2705.  
  2706. void global_data_callit (int i)
  2707. {
  2708.   *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
  2709. }
  2710.  
  2711. int global_data_getstuff (int *p, int i)
  2712. {
  2713.   return *p + i;
  2714. }
  2715.  
  2716. void global_data_test (void)
  2717. {
  2718.   global_data.a[0] = 42;
  2719.   global_data.b[0] = &global_data.a[0];
  2720.   global_data_callit (0);
  2721.   printf ("%d\n", global_data.a[0]);
  2722. }
  2723.  
  2724. struct cmpcmpS
  2725. {
  2726.   unsigned char fill : 3;
  2727.   unsigned char b1 : 1;
  2728.   unsigned char b2 : 1;
  2729.   unsigned char fill2 : 3;
  2730. };
  2731.  
  2732. int glob1, glob2, glob3;
  2733.  
  2734. void compare_comparisons (struct cmpcmpS *s)
  2735. {
  2736.   if (s->b1 != (glob1 == glob2)
  2737.       || (s->b2 != (glob1 == glob3)))
  2738.     printf ("comparing comparisons broken\n");
  2739. }
  2740.  
  2741. void cmp_comparison_test(void)
  2742. {
  2743.   struct cmpcmpS s;
  2744.   s.b1 = 1;
  2745.   glob1 = 42; glob2 = 42;
  2746.   s.b2 = 0;
  2747.   glob3 = 43;
  2748.   compare_comparisons (&s);
  2749. }
  2750.  
  2751. int fcompare (double a, double b, int code)
  2752. {
  2753.   switch (code) {
  2754.     case 0: return a == b;
  2755.     case 1: return a != b;
  2756.     case 2: return a < b;
  2757.     case 3: return a >= b;
  2758.     case 4: return a > b;
  2759.     case 5: return a <= b;
  2760.   }
  2761. }
  2762.  
  2763. void math_cmp_test(void)
  2764. {
  2765.   double nan = 0.0/0.0;
  2766.   double one = 1.0;
  2767.   double two = 2.0;
  2768.   int comp = 0;
  2769. #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
  2770.  
  2771.   /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
  2772.      And it does this in various ways so that all code generation paths
  2773.      are checked (generating inverted tests, or non-inverted tests, or
  2774.      producing a 0/1 value without jumps (that's done in the fcompare
  2775.      function).  */
  2776. #define FCMP(a,b,op,iop,code) \
  2777.   if (fcompare (a,b,code))    \
  2778.     bug (a,b,op,iop,1); \
  2779.   if (a op b) \
  2780.     bug (a,b,op,iop,2); \
  2781.   if (a iop b) \
  2782.     ; \
  2783.   else \
  2784.     bug (a,b,op,iop,3); \
  2785.   if ((a op b) || comp) \
  2786.     bug (a,b,op,iop,4); \
  2787.   if ((a iop b) || comp) \
  2788.     ; \
  2789.   else \
  2790.     bug (a,b,op,iop,5);
  2791.  
  2792.   /* Equality tests.  */
  2793.   FCMP(nan, nan, ==, !=, 0);
  2794.   FCMP(one, two, ==, !=, 0);
  2795.   FCMP(one, one, !=, ==, 1);
  2796.   /* Non-equality is a bit special.  */
  2797.   if (!fcompare (nan, nan, 1))
  2798.     bug (nan, nan, !=, ==, 6);
  2799.  
  2800.   /* Relational tests on numbers.  */
  2801.   FCMP(two, one, <, >=, 2);
  2802.   FCMP(one, two, >=, <, 3);
  2803.   FCMP(one, two, >, <=, 4);
  2804.   FCMP(two, one, <=, >, 5);
  2805.  
  2806.   /* Relational tests on NaNs.  Note that the inverse op here is
  2807.      always !=, there's no operator in C that is equivalent to !(a < b),
  2808.      when NaNs are involved, same for the other relational ops.  */
  2809.   FCMP(nan, nan, <, !=, 2);
  2810.   FCMP(nan, nan, >=, !=, 3);
  2811.   FCMP(nan, nan, >, !=, 4);
  2812.   FCMP(nan, nan, <=, !=, 5);
  2813. }
  2814.  
  2815. double get100 () { return 100.0; }
  2816.  
  2817. void callsave_test(void)
  2818. {
  2819. #if defined __i386__ || defined __x86_64__ || defined __arm__
  2820.   int i, s; double *d; double t;
  2821.   s = sizeof (double);
  2822.   printf ("callsavetest: %d\n", s);
  2823.   d = alloca (sizeof(double));
  2824.   d[0] = 10.0;
  2825.   /* x86-64 had a bug were the next call to get100 would evict
  2826.      the lvalue &d[0] as VT_LLOCAL, and the reload would be done
  2827.      in int type, not pointer type.  When alloca returns a pointer
  2828.      with the high 32 bit set (which is likely on x86-64) the access
  2829.      generates a segfault.  */
  2830.   i = d[0] > get100 ();
  2831.   printf ("%d\n", i);
  2832. #endif
  2833. }
  2834.  
  2835.  
  2836. void bfa3(ptrdiff_t str_offset)
  2837. {
  2838.     printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
  2839. }
  2840. void bfa2(ptrdiff_t str_offset)
  2841. {
  2842.     printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
  2843.     bfa3(str_offset);
  2844. }
  2845. void bfa1(ptrdiff_t str_offset)
  2846. {
  2847.     printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
  2848.     bfa2(str_offset);
  2849. }
  2850.  
  2851. void builtin_frame_address_test(void)
  2852. {
  2853. /* builtin_frame_address fails on ARM with gcc which make test3 fail */
  2854. #ifndef __arm__
  2855.     char str[] = "__builtin_frame_address";
  2856.     char *fp0 = __builtin_frame_address(0);
  2857.  
  2858.     printf("str: %s\n", str);
  2859.     bfa1(str-fp0);
  2860. #endif
  2861. }
  2862.  
  2863. char via_volatile (char i)
  2864. {
  2865.   char volatile vi;
  2866.   vi = i;
  2867.   return vi;
  2868. }
  2869.