switch: fix label sorting
[tinycc.git] / tests / tcctest.c
blob5e9b07f3db7501bd029209ff54f4f4fd89f56d48
1 /*
2 * TCC auto test program
3 */
4 #include "config.h"
6 #if GCC_MAJOR >= 3
8 /* Unfortunately, gcc version < 3 does not handle that! */
9 #define ALL_ISOC99
11 /* only gcc 3 handles _Bool correctly */
12 #define BOOL_ISOC99
14 /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
15 #define CORRECT_CR_HANDLING
17 #endif
19 #if defined(_WIN32)
20 #define LONG_LONG_FORMAT "%lld"
21 #define ULONG_LONG_FORMAT "%llu"
22 #else
23 #define LONG_LONG_FORMAT "%Ld"
24 #define ULONG_LONG_FORMAT "%Lu"
25 #endif
27 // MinGW has 80-bit rather than 64-bit long double which isn't compatible with TCC or MSVC
28 #if defined(_WIN32) && defined(__GNUC__)
29 #define LONG_DOUBLE double
30 #define LONG_DOUBLE_LITERAL(x) x
31 #else
32 #define LONG_DOUBLE long double
33 #define LONG_DOUBLE_LITERAL(x) x ## L
34 #endif
36 /* deprecated and no longer supported in gcc 3.3 */
37 //#define ACCEPT_CR_IN_STRINGS
39 /* __VA_ARGS__ and __func__ support */
40 #define C99_MACROS
42 /* test various include syntaxes */
44 #define TCCLIB_INC <tcclib.h>
45 #define TCCLIB_INC1 <tcclib
46 #define TCCLIB_INC2 h>
47 #define TCCLIB_INC3 "tcclib"
49 #include TCCLIB_INC
51 #include TCCLIB_INC1.TCCLIB_INC2
53 #include TCCLIB_INC1.h>
55 /* gcc 3.2 does not accept that (bug ?) */
56 //#include TCCLIB_INC3 ".h"
58 #include <tcclib.h>
60 #include "tcclib.h"
62 void intdiv_test();
63 void string_test();
64 void expr_test();
65 void macro_test();
66 void recursive_macro_test();
67 void scope_test();
68 void forward_test();
69 void funcptr_test();
70 void loop_test();
71 void switch_test();
72 void goto_test();
73 void enum_test();
74 void typedef_test();
75 void struct_test();
76 void array_test();
77 void expr_ptr_test();
78 void bool_test();
79 void expr2_test();
80 void constant_expr_test();
81 void expr_cmp_test();
82 void char_short_test();
83 void init_test(void);
84 void compound_literal_test(void);
85 int kr_test();
86 void struct_assign_test(void);
87 void cast_test(void);
88 void bitfield_test(void);
89 void c99_bool_test(void);
90 void float_test(void);
91 void longlong_test(void);
92 void manyarg_test(void);
93 void stdarg_test(void);
94 void whitespace_test(void);
95 void relocation_test(void);
96 void old_style_function(void);
97 void alloca_test(void);
98 void c99_vla_test(int size1, int size2);
99 void sizeof_test(void);
100 void typeof_test(void);
101 void local_label_test(void);
102 void statement_expr_test(void);
103 void asm_test(void);
104 void builtin_test(void);
105 void weak_test(void);
106 void global_data_test(void);
107 void cmp_comparison_test(void);
108 void math_cmp_test(void);
109 void callsave_test(void);
110 void builtin_frame_address_test(void);
112 int fib(int n);
113 void num(int n);
114 void forward_ref(void);
115 int isid(int c);
117 /* Line joining happens before tokenization, so the following
118 must be parsed as ellipsis. */
119 void funny_line_continuation (int, ..\
120 . );
122 char via_volatile (char);
124 #define A 2
125 #define N 1234 + A
126 #define pf printf
127 #define M1(a, b) (a) + (b)
129 #define str\
130 (s) # s
131 #define glue(a, b) a ## b
132 #define xglue(a, b) glue(a, b)
133 #define HIGHLOW "hello"
134 #define LOW LOW ", world"
136 static int onetwothree = 123;
137 #define onetwothree4 onetwothree
138 #define onetwothree xglue(onetwothree,4)
140 #define min(a, b) ((a) < (b) ? (a) : (b))
142 #ifdef C99_MACROS
143 #define dprintf(level,...) printf(__VA_ARGS__)
144 #endif
146 /* gcc vararg macros */
147 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
149 #define MACRO_NOARGS()
151 #define AAA 3
152 #undef AAA
153 #define AAA 4
155 #if 1
156 #define B3 1
157 #elif 1
158 #define B3 2
159 #elif 0
160 #define B3 3
161 #else
162 #define B3 4
163 #endif
165 #define __INT64_C(c) c ## LL
166 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
168 int qq(int x)
170 return x + 40;
172 #define qq(x) x
174 #define spin_lock(lock) do { } while (0)
175 #define wq_spin_lock spin_lock
176 #define TEST2() wq_spin_lock(a)
178 #define UINT_MAX ((unsigned) -1)
180 void intdiv_test(void)
182 printf("18/21=%u\n", 18/21);
183 printf("18%%21=%u\n", 18%21);
184 printf("41/21=%u\n", 41/21);
185 printf("41%%21=%u\n", 41%21);
186 printf("42/21=%u\n", 42/21);
187 printf("42%%21=%u\n", 42%21);
188 printf("43/21=%u\n", 43/21);
189 printf("43%%21=%u\n", 43%21);
190 printf("126/21=%u\n", 126/21);
191 printf("126%%21=%u\n", 126%21);
192 printf("131/21=%u\n", 131/21);
193 printf("131%%21=%u\n", 131%21);
194 printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX/2+3)/2);
195 printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX/2+3)%2);
197 printf("18/-21=%u\n", 18/-21);
198 printf("18%%-21=%u\n", 18%-21);
199 printf("41/-21=%u\n", 41/-21);
200 printf("41%%-21=%u\n", 41%-21);
201 printf("42/-21=%u\n", 42/-21);
202 printf("42%%-21=%u\n", 42%-21);
203 printf("43/-21=%u\n", 43/-21);
204 printf("43%%-21=%u\n", 43%-21);
205 printf("126/-21=%u\n", 126/-21);
206 printf("126%%-21=%u\n", 126%-21);
207 printf("131/-21=%u\n", 131/-21);
208 printf("131%%-21=%u\n", 131%-21);
209 printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX/2+3)/-2);
210 printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX/2+3)%-2);
212 printf("-18/21=%u\n", -18/21);
213 printf("-18%%21=%u\n", -18%21);
214 printf("-41/21=%u\n", -41/21);
215 printf("-41%%21=%u\n", -41%21);
216 printf("-42/21=%u\n", -42/21);
217 printf("-42%%21=%u\n", -42%21);
218 printf("-43/21=%u\n", -43/21);
219 printf("-43%%21=%u\n", -43%21);
220 printf("-126/21=%u\n", -126/21);
221 printf("-126%%21=%u\n", -126%21);
222 printf("-131/21=%u\n", -131/21);
223 printf("-131%%21=%u\n", -131%21);
224 printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX/2+3))/2);
225 printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX/2+3))%2);
227 printf("-18/-21=%u\n", -18/-21);
228 printf("-18%%-21=%u\n", -18%-21);
229 printf("-41/-21=%u\n", -41/-21);
230 printf("-41%%-21=%u\n", -41%-21);
231 printf("-42/-21=%u\n", -42/-21);
232 printf("-42%%-21=%u\n", -42%-21);
233 printf("-43/-21=%u\n", -43/-21);
234 printf("-43%%-21=%u\n", -43%-21);
235 printf("-126/-21=%u\n", -126/-21);
236 printf("-126%%-21=%u\n", -126%-21);
237 printf("-131/-21=%u\n", -131/-21);
238 printf("-131%%-21=%u\n", -131%-21);
239 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX/2+3))/-2);
240 printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX/2+3))%-2);
243 void macro_test(void)
245 printf("macro:\n");\f\v
246 pf("N=%d\n", N);
247 printf("aaa=%d\n", AAA);
249 printf("min=%d\n", min(1, min(2, -1)));
251 printf("s1=%s\n", glue(HIGH, LOW));
252 printf("s2=%s\n", xglue(HIGH, LOW));
253 printf("s3=%s\n", str("c"));
254 printf("s4=%s\n", str(a1));
255 printf("B3=%d\n", B3);
257 printf("onetwothree=%d\n", onetwothree);
259 #ifdef A
260 printf("A defined\n");
261 #endif
262 #ifdef B
263 printf("B defined\n");
264 #endif
265 #ifdef A
266 printf("A defined\n");
267 #else
268 printf("A not defined\n");
269 #endif
270 #ifdef B
271 printf("B defined\n");
272 #else
273 printf("B not defined\n");
274 #endif
276 #ifdef A
277 printf("A defined\n");
278 #ifdef B
279 printf("B1 defined\n");
280 #else
281 printf("B1 not defined\n");
282 #endif
283 #else
284 printf("A not defined\n");
285 #ifdef B
286 printf("B2 defined\n");
287 #else
288 printf("B2 not defined\n");
289 #endif
290 #endif
292 #if 1+1
293 printf("test true1\n");
294 #endif
295 #if 0
296 printf("test true2\n");
297 #endif
298 #if 1-1
299 printf("test true3\n");
300 #endif
301 #if defined(A)
302 printf("test trueA\n");
303 #endif
304 #if defined(B)
305 printf("test trueB\n");
306 #endif
308 #if 0
309 printf("test 0\n");
310 #elif 0
311 printf("test 1\n");
312 #elif 2
313 printf("test 2\n");
314 #else
315 printf("test 3\n");
316 #endif
318 MACRO_NOARGS();
320 #ifdef __LINE__
321 printf("__LINE__ defined\n");
322 #endif
324 printf("__LINE__=%d __FILE__=%s\n",
325 __LINE__, __FILE__);
326 #if 0
327 #line 200
328 printf("__LINE__=%d __FILE__=%s\n",
329 __LINE__, __FILE__);
330 #line 203 "test"
331 printf("__LINE__=%d __FILE__=%s\n",
332 __LINE__, __FILE__);
333 #line 227 "tcctest.c"
334 #endif
336 /* not strictly preprocessor, but we test it there */
337 #ifdef C99_MACROS
338 printf("__func__ = %s\n", __func__);
339 dprintf(1, "vaarg=%d\n", 1);
340 #endif
341 dprintf1(1, "vaarg1\n");
342 dprintf1(1, "vaarg1=%d\n", 2);
343 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
345 /* gcc extension */
346 printf("func='%s'\n", __FUNCTION__);
348 /* complicated macros in glibc */
349 printf("INT64_MIN=" LONG_LONG_FORMAT "\n", INT64_MIN);
351 int a;
352 a = 1;
353 glue(a+, +);
354 printf("a=%d\n", a);
355 glue(a <, <= 2);
356 printf("a=%d\n", a);
359 /* macro function with argument outside the macro string */
360 #define MF_s MF_hello
361 #define MF_hello(msg) printf("%s\n",msg)
363 #define MF_t printf("tralala\n"); MF_hello
365 MF_s("hi");
366 MF_t("hi");
368 /* test macro substituion inside args (should not eat stream) */
369 printf("qq=%d\n", qq(qq)(2));
371 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
372 null argument without a space. gcc 3.2 fixes that. */
374 #define qq1(x) 1
375 printf("qq1=%d\n", qq1( ));
377 /* comment with stray handling *\
379 /* this is a valid *\/ comment */
380 /* this is a valid comment *\*/
381 // this is a valid\
382 comment
384 /* test function macro substitution when the function name is
385 substituted */
386 TEST2();
388 /* And again when the name and parenthes are separated by a
389 comment. */
390 TEST2 /* the comment */ ();
394 static void print_num(char *fn, int line, int num) {
395 printf("fn %s, line %d, num %d\n", fn, line, num);
398 void recursive_macro_test(void)
401 #define ELF32_ST_TYPE(val) ((val) & 0xf)
402 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
403 #define STB_WEAK 2 /* Weak symbol */
404 #define ELFW(type) ELF##32##_##type
405 printf("%d\n", ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(123)));
407 #define WRAP(x) x
409 #define print_num(x) print_num(__FILE__,__LINE__,x)
410 print_num(123);
411 WRAP(print_num(123));
412 WRAP(WRAP(print_num(123)));
414 static struct recursive_macro { int rm_field; } G;
415 #define rm_field (G.rm_field)
416 printf("rm_field = %d\n", rm_field);
417 printf("rm_field = %d\n", WRAP(rm_field));
418 WRAP((printf("rm_field = %d %d\n", rm_field, WRAP(rm_field))));
421 int op(a,b)
423 return a / b;
426 int ret(a)
428 if (a == 2)
429 return 1;
430 if (a == 3)
431 return 2;
432 return 0;
435 void ps(const char *s)
437 int c;
438 while (1) {
439 c = *s;
440 if (c == 0)
441 break;
442 printf("%c", c);
443 s++;
447 const char foo1_string[] = "\
448 bar\n\
449 test\14\
452 void string_test()
454 unsigned int b;
455 printf("string:\n");
456 printf("\141\1423\143\n");/* dezdez test */
457 printf("\x41\x42\x43\x3a\n");
458 printf("c=%c\n", 'r');
459 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
460 printf("foo1_string='%s'\n", foo1_string);
461 #if 0
462 printf("wstring=%S\n", L"abc");
463 printf("wstring=%S\n", L"abc" L"def" "ghi");
464 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
465 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
466 #endif
467 ps("test\n");
468 b = 32;
469 while ((b = b + 1) < 96) {
470 printf("%c", b);
472 printf("\n");
473 printf("fib=%d\n", fib(33));
474 b = 262144;
475 while (b != 0x80000000) {
476 num(b);
477 b = b * 2;
481 void loop_test()
483 int i;
484 i = 0;
485 while (i < 10)
486 printf("%d", i++);
487 printf("\n");
488 for(i = 0; i < 10;i++)
489 printf("%d", i);
490 printf("\n");
491 i = 0;
492 do {
493 printf("%d", i++);
494 } while (i < 10);
495 printf("\n");
497 char count = 123;
498 /* c99 for loop init test */
499 for (size_t count = 1; count < 3; count++)
500 printf("count=%d\n", count);
501 printf("count = %d\n", count);
503 /* break/continue tests */
504 i = 0;
505 while (1) {
506 if (i == 6)
507 break;
508 i++;
509 if (i == 3)
510 continue;
511 printf("%d", i);
513 printf("\n");
515 /* break/continue tests */
516 i = 0;
517 do {
518 if (i == 6)
519 break;
520 i++;
521 if (i == 3)
522 continue;
523 printf("%d", i);
524 } while(1);
525 printf("\n");
527 for(i = 0;i < 10;i++) {
528 if (i == 3)
529 continue;
530 printf("%d", i);
532 printf("\n");
535 typedef int typedef_and_label;
537 void goto_test()
539 int i;
540 static void *label_table[3] = { &&label1, &&label2, &&label3 };
542 printf("goto:\n");
543 i = 0;
544 /* This needs to parse as label, not as start of decl. */
545 typedef_and_label:
546 s_loop:
547 if (i >= 10)
548 goto s_end;
549 printf("%d", i);
550 i++;
551 goto s_loop;
552 s_end:
553 printf("\n");
555 /* we also test computed gotos (GCC extension) */
556 for(i=0;i<3;i++) {
557 goto *label_table[i];
558 label1:
559 printf("label1\n");
560 goto next;
561 label2:
562 printf("label2\n");
563 goto next;
564 label3:
565 printf("label3\n");
566 next: ;
570 enum {
572 E1 = 2,
573 E2 = 4,
578 enum test {
579 E5 = 1000,
582 void enum_test()
584 enum test b1;
585 printf("enum:\n%d %d %d %d %d %d\n",
586 E0, E1, E2, E3, E4, E5);
587 b1 = 1;
588 printf("b1=%d\n", b1);
591 typedef int *my_ptr;
593 typedef int mytype1;
594 typedef int mytype2;
596 void typedef_test()
598 my_ptr a;
599 mytype1 mytype2;
600 int b;
602 a = &b;
603 *a = 1234;
604 printf("typedef:\n");
605 printf("a=%d\n", *a);
606 mytype2 = 2;
607 printf("mytype2=%d\n", mytype2);
610 void forward_test()
612 printf("forward:\n");
613 forward_ref();
614 forward_ref();
618 void forward_ref(void)
620 printf("forward ok\n");
623 typedef struct struct1 {
624 int f1;
625 int f2, f3;
626 union union1 {
627 int v1;
628 int v2;
629 } u;
630 char str[3];
631 } struct1;
633 struct struct2 {
634 int a;
635 char b;
638 union union2 {
639 int w1;
640 int w2;
643 struct struct1 st1, st2;
645 int main(int argc, char **argv)
647 string_test();
648 expr_test();
649 macro_test();
650 recursive_macro_test();
651 scope_test();
652 forward_test();
653 funcptr_test();
654 loop_test();
655 switch_test();
656 goto_test();
657 enum_test();
658 typedef_test();
659 struct_test();
660 array_test();
661 expr_ptr_test();
662 bool_test();
663 expr2_test();
664 constant_expr_test();
665 expr_cmp_test();
666 char_short_test();
667 init_test();
668 compound_literal_test();
669 kr_test();
670 struct_assign_test();
671 cast_test();
672 bitfield_test();
673 c99_bool_test();
674 float_test();
675 longlong_test();
676 manyarg_test();
677 stdarg_test();
678 whitespace_test();
679 relocation_test();
680 old_style_function();
681 alloca_test();
682 c99_vla_test(5, 2);
683 sizeof_test();
684 typeof_test();
685 statement_expr_test();
686 local_label_test();
687 asm_test();
688 builtin_test();
689 #ifndef _WIN32
690 weak_test();
691 #endif
692 global_data_test();
693 cmp_comparison_test();
694 math_cmp_test();
695 callsave_test();
696 builtin_frame_address_test();
697 intdiv_test();
698 if (via_volatile (42) != 42)
699 printf ("via_volatile broken\n");
700 return 0;
703 int tab[3];
704 int tab2[3][2];
706 int g;
708 void f1(g)
710 printf("g1=%d\n", g);
713 void scope_test()
715 printf("scope:\n");
716 g = 2;
717 f1(1);
718 printf("g2=%d\n", g);
720 int g;
721 g = 3;
722 printf("g3=%d\n", g);
724 int g;
725 g = 4;
726 printf("g4=%d\n", g);
729 printf("g5=%d\n", g);
732 void array_test()
734 int i, j, a[4];
736 printf("array:\n");
737 printf("sizeof(a) = %d\n", sizeof(a));
738 printf("sizeof(\"a\") = %d\n", sizeof("a"));
739 #ifdef C99_MACROS
740 printf("sizeof(__func__) = %d\n", sizeof(__func__));
741 #endif
742 printf("sizeof tab %d\n", sizeof(tab));
743 printf("sizeof tab2 %d\n", sizeof tab2);
744 tab[0] = 1;
745 tab[1] = 2;
746 tab[2] = 3;
747 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
748 for(i=0;i<3;i++)
749 for(j=0;j<2;j++)
750 tab2[i][j] = 10 * i + j;
751 for(i=0;i<3*2;i++) {
752 printf(" %3d", ((int *)tab2)[i]);
754 printf("\n");
755 printf("sizeof(size_t)=%d\n", sizeof(size_t));
756 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
759 void expr_test()
761 int a, b;
762 a = 0;
763 printf("%d\n", a += 1);
764 printf("%d\n", a -= 2);
765 printf("%d\n", a *= 31232132);
766 printf("%d\n", a /= 4);
767 printf("%d\n", a %= 20);
768 printf("%d\n", a &= 6);
769 printf("%d\n", a ^= 7);
770 printf("%d\n", a |= 8);
771 printf("%d\n", a >>= 3);
772 printf("%d\n", a <<= 4);
774 a = 22321;
775 b = -22321;
776 printf("%d\n", a + 1);
777 printf("%d\n", a - 2);
778 printf("%d\n", a * 312);
779 printf("%d\n", a / 4);
780 printf("%d\n", b / 4);
781 printf("%d\n", (unsigned)b / 4);
782 printf("%d\n", a % 20);
783 printf("%d\n", b % 20);
784 printf("%d\n", (unsigned)b % 20);
785 printf("%d\n", a & 6);
786 printf("%d\n", a ^ 7);
787 printf("%d\n", a | 8);
788 printf("%d\n", a >> 3);
789 printf("%d\n", b >> 3);
790 printf("%d\n", (unsigned)b >> 3);
791 printf("%d\n", a << 4);
792 printf("%d\n", ~a);
793 printf("%d\n", -a);
794 printf("%d\n", +a);
796 printf("%d\n", 12 + 1);
797 printf("%d\n", 12 - 2);
798 printf("%d\n", 12 * 312);
799 printf("%d\n", 12 / 4);
800 printf("%d\n", 12 % 20);
801 printf("%d\n", 12 & 6);
802 printf("%d\n", 12 ^ 7);
803 printf("%d\n", 12 | 8);
804 printf("%d\n", 12 >> 2);
805 printf("%d\n", 12 << 4);
806 printf("%d\n", ~12);
807 printf("%d\n", -12);
808 printf("%d\n", +12);
809 printf("%d %d %d %d\n",
810 isid('a'),
811 isid('g'),
812 isid('T'),
813 isid('('));
816 int isid(int c)
818 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
821 /**********************/
823 int vstack[10], *vstack_ptr;
825 void vpush(int vt, int vc)
827 *vstack_ptr++ = vt;
828 *vstack_ptr++ = vc;
831 void vpop(int *ft, int *fc)
833 *fc = *--vstack_ptr;
834 *ft = *--vstack_ptr;
837 void expr2_test()
839 int a, b;
841 printf("expr2:\n");
842 vstack_ptr = vstack;
843 vpush(1432432, 2);
844 vstack_ptr[-2] &= ~0xffffff80;
845 vpop(&a, &b);
846 printf("res= %d %d\n", a, b);
849 void constant_expr_test()
851 int a;
852 printf("constant_expr:\n");
853 a = 3;
854 printf("%d\n", a * 16);
855 printf("%d\n", a * 1);
856 printf("%d\n", a + 0);
859 int tab4[10];
861 void expr_ptr_test()
863 int *p, *q;
864 int i = -1;
866 printf("expr_ptr:\n");
867 p = tab4;
868 q = tab4 + 10;
869 printf("diff=%d\n", q - p);
870 p++;
871 printf("inc=%d\n", p - tab4);
872 p--;
873 printf("dec=%d\n", p - tab4);
874 ++p;
875 printf("inc=%d\n", p - tab4);
876 --p;
877 printf("dec=%d\n", p - tab4);
878 printf("add=%d\n", p + 3 - tab4);
879 printf("add=%d\n", 3 + p - tab4);
881 /* check if 64bit support is ok */
882 q = p = 0;
883 q += i;
884 printf("%p %p %ld\n", q, p, p-q);
885 printf("%d %d %d %d %d %d\n",
886 p == q, p != q, p < q, p <= q, p >= q, p > q);
887 i = 0xf0000000;
888 p += i;
889 printf("%p %p %ld\n", q, p, p-q);
890 printf("%d %d %d %d %d %d\n",
891 p == q, p != q, p < q, p <= q, p >= q, p > q);
892 p = (int *)((char *)p + 0xf0000000);
893 printf("%p %p %ld\n", q, p, p-q);
894 printf("%d %d %d %d %d %d\n",
895 p == q, p != q, p < q, p <= q, p >= q, p > q);
896 p += 0xf0000000;
897 printf("%p %p %ld\n", q, p, p-q);
898 printf("%d %d %d %d %d %d\n",
899 p == q, p != q, p < q, p <= q, p >= q, p > q);
901 struct size12 {
902 int i, j, k;
904 struct size12 s[2], *sp = s;
905 int i, j;
906 sp->i = 42;
907 sp++;
908 j = -1;
909 printf("%d\n", sp[j].i);
913 void expr_cmp_test()
915 int a, b;
916 printf("constant_expr:\n");
917 a = -1;
918 b = 1;
919 printf("%d\n", a == a);
920 printf("%d\n", a != a);
922 printf("%d\n", a < b);
923 printf("%d\n", a <= b);
924 printf("%d\n", a <= a);
925 printf("%d\n", b >= a);
926 printf("%d\n", a >= a);
927 printf("%d\n", b > a);
929 printf("%d\n", (unsigned)a < b);
930 printf("%d\n", (unsigned)a <= b);
931 printf("%d\n", (unsigned)a <= a);
932 printf("%d\n", (unsigned)b >= a);
933 printf("%d\n", (unsigned)a >= a);
934 printf("%d\n", (unsigned)b > a);
937 struct empty {
940 struct aligntest1 {
941 char a[10];
944 struct aligntest2 {
945 int a;
946 char b[10];
949 struct aligntest3 {
950 double a, b;
953 struct aligntest4 {
954 double a[0];
957 void struct_test()
959 struct1 *s;
960 union union2 u;
962 printf("struct:\n");
963 printf("sizes: %d %d %d %d\n",
964 sizeof(struct struct1),
965 sizeof(struct struct2),
966 sizeof(union union1),
967 sizeof(union union2));
968 st1.f1 = 1;
969 st1.f2 = 2;
970 st1.f3 = 3;
971 printf("st1: %d %d %d\n",
972 st1.f1, st1.f2, st1.f3);
973 st1.u.v1 = 1;
974 st1.u.v2 = 2;
975 printf("union1: %d\n", st1.u.v1);
976 u.w1 = 1;
977 u.w2 = 2;
978 printf("union2: %d\n", u.w1);
979 s = &st2;
980 s->f1 = 3;
981 s->f2 = 2;
982 s->f3 = 1;
983 printf("st2: %d %d %d\n",
984 s->f1, s->f2, s->f3);
985 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
987 /* align / size tests */
988 printf("aligntest1 sizeof=%d alignof=%d\n",
989 sizeof(struct aligntest1), __alignof__(struct aligntest1));
990 printf("aligntest2 sizeof=%d alignof=%d\n",
991 sizeof(struct aligntest2), __alignof__(struct aligntest2));
992 printf("aligntest3 sizeof=%d alignof=%d\n",
993 sizeof(struct aligntest3), __alignof__(struct aligntest3));
994 printf("aligntest4 sizeof=%d alignof=%d\n",
995 sizeof(struct aligntest4), __alignof__(struct aligntest4));
997 /* empty structures (GCC extension) */
998 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
999 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
1002 /* XXX: depend on endianness */
1003 void char_short_test()
1005 int var1, var2;
1007 printf("char_short:\n");
1009 var1 = 0x01020304;
1010 var2 = 0xfffefdfc;
1011 printf("s8=%d %d\n",
1012 *(char *)&var1, *(char *)&var2);
1013 printf("u8=%d %d\n",
1014 *(unsigned char *)&var1, *(unsigned char *)&var2);
1015 printf("s16=%d %d\n",
1016 *(short *)&var1, *(short *)&var2);
1017 printf("u16=%d %d\n",
1018 *(unsigned short *)&var1, *(unsigned short *)&var2);
1019 printf("s32=%d %d\n",
1020 *(int *)&var1, *(int *)&var2);
1021 printf("u32=%d %d\n",
1022 *(unsigned int *)&var1, *(unsigned int *)&var2);
1023 *(char *)&var1 = 0x08;
1024 printf("var1=%x\n", var1);
1025 *(short *)&var1 = 0x0809;
1026 printf("var1=%x\n", var1);
1027 *(int *)&var1 = 0x08090a0b;
1028 printf("var1=%x\n", var1);
1031 /******************/
1033 typedef struct Sym {
1034 int v;
1035 int t;
1036 int c;
1037 struct Sym *next;
1038 struct Sym *prev;
1039 } Sym;
1041 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1042 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1044 static int toupper1(int a)
1046 return TOUPPER(a);
1049 void bool_test()
1051 int *s, a, b, t, f, i;
1053 a = 0;
1054 s = (void*)0;
1055 printf("!s=%d\n", !s);
1057 if (!s || !s[0])
1058 a = 1;
1059 printf("a=%d\n", a);
1061 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1062 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1063 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1064 #if 1 && 1
1065 printf("a1\n");
1066 #endif
1067 #if 1 || 0
1068 printf("a2\n");
1069 #endif
1070 #if 1 ? 0 : 1
1071 printf("a3\n");
1072 #endif
1073 #if 0 ? 0 : 1
1074 printf("a4\n");
1075 #endif
1077 a = 4;
1078 printf("b=%d\n", a + (0 ? 1 : a / 2));
1080 /* test register spilling */
1081 a = 10;
1082 b = 10;
1083 a = (a + b) * ((a < b) ?
1084 ((b - a) * (a - b)): a + b);
1085 printf("a=%d\n", a);
1087 /* test complex || or && expressions */
1088 t = 1;
1089 f = 0;
1090 a = 32;
1091 printf("exp=%d\n", f == (32 <= a && a <= 3));
1092 printf("r=%d\n", (t || f) + (t && f));
1094 /* test ? : cast */
1096 int aspect_on;
1097 int aspect_native = 65536;
1098 double bfu_aspect = 1.0;
1099 int aspect;
1100 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
1101 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
1102 printf("aspect=%d\n", aspect);
1106 /* test ? : GCC extension */
1108 static int v1 = 34 ? : -1; /* constant case */
1109 static int v2 = 0 ? : -1; /* constant case */
1110 int a = 30;
1112 printf("%d %d\n", v1, v2);
1113 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
1116 /* again complex expression */
1117 for(i=0;i<256;i++) {
1118 if (toupper1 (i) != TOUPPER (i))
1119 printf("error %d\n", i);
1123 /* GCC accepts that */
1124 static int tab_reinit[];
1125 static int tab_reinit[10];
1127 //int cinit1; /* a global variable can be defined several times without error ! */
1128 int cinit1;
1129 int cinit1;
1130 int cinit1 = 0;
1131 int *cinit2 = (int []){3, 2, 1};
1133 void compound_literal_test(void)
1135 int *p, i;
1136 char *q, *q3;
1138 printf("compound_test:\n");
1140 p = (int []){1, 2, 3};
1141 for(i=0;i<3;i++)
1142 printf(" %d", p[i]);
1143 printf("\n");
1145 for(i=0;i<3;i++)
1146 printf("%d", cinit2[i]);
1147 printf("\n");
1149 q = "tralala1";
1150 printf("q1=%s\n", q);
1152 q = (char *){ "tralala2" };
1153 printf("q2=%s\n", q);
1155 q3 = (char *){ q };
1156 printf("q3=%s\n", q3);
1158 q = (char []){ "tralala3" };
1159 printf("q4=%s\n", q);
1161 #ifdef ALL_ISOC99
1162 p = (int []){1, 2, cinit1 + 3};
1163 for(i=0;i<3;i++)
1164 printf(" %d", p[i]);
1165 printf("\n");
1167 for(i=0;i<3;i++) {
1168 p = (int []){1, 2, 4 + i};
1169 printf("%d %d %d\n",
1170 p[0],
1171 p[1],
1172 p[2]);
1174 #endif
1177 /* K & R protos */
1179 kr_func1(a, b)
1181 return a + b;
1184 int kr_func2(a, b)
1186 return a + b;
1189 kr_test()
1191 printf("kr_test:\n");
1192 printf("func1=%d\n", kr_func1(3, 4));
1193 printf("func2=%d\n", kr_func2(3, 4));
1194 return 0;
1197 void num(int n)
1199 char *tab, *p;
1200 tab = (char*)malloc(20);
1201 p = tab;
1202 while (1) {
1203 *p = 48 + (n % 10);
1204 p++;
1205 n = n / 10;
1206 if (n == 0)
1207 break;
1209 while (p != tab) {
1210 p--;
1211 printf("%c", *p);
1213 printf("\n");
1214 free(tab);
1217 /* structure assignment tests */
1218 struct structa1 {
1219 int f1;
1220 char f2;
1223 struct structa1 ssta1;
1225 void struct_assign_test1(struct structa1 s1, int t, float f)
1227 printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
1230 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1232 s1.f1 += t;
1233 s1.f2 -= t;
1234 return s1;
1237 void struct_assign_test(void)
1239 struct S {
1240 struct structa1 lsta1, lsta2;
1241 int i;
1242 } s, *ps;
1244 ps = &s;
1245 ps->i = 4;
1246 #if 0
1247 printf("struct_assign_test:\n");
1249 s.lsta1.f1 = 1;
1250 s.lsta1.f2 = 2;
1251 printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
1252 s.lsta2 = s.lsta1;
1253 printf("%d %d\n", s.lsta2.f1, s.lsta2.f2);
1254 #else
1255 s.lsta2.f1 = 1;
1256 s.lsta2.f2 = 2;
1257 #endif
1258 struct_assign_test1(ps->lsta2, 3, 4.5);
1260 printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
1261 ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
1262 printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
1264 static struct {
1265 void (*elem)();
1266 } t[] = {
1267 /* XXX: we should allow this even without braces */
1268 { struct_assign_test }
1270 printf("%d\n", struct_assign_test == t[0].elem);
1273 /* casts to short/char */
1275 void cast1(char a, short b, unsigned char c, unsigned short d)
1277 printf("%d %d %d %d\n", a, b, c, d);
1280 char bcast;
1281 short scast;
1283 void cast_test()
1285 int a;
1286 char c;
1287 char tab[10];
1288 unsigned b,d;
1289 short s;
1290 char *p = NULL;
1291 p -= 0x700000000042;
1293 printf("cast_test:\n");
1294 a = 0xfffff;
1295 cast1(a, a, a, a);
1296 a = 0xffffe;
1297 printf("%d %d %d %d\n",
1298 (char)(a + 1),
1299 (short)(a + 1),
1300 (unsigned char)(a + 1),
1301 (unsigned short)(a + 1));
1302 printf("%d %d %d %d\n",
1303 (char)0xfffff,
1304 (short)0xfffff,
1305 (unsigned char)0xfffff,
1306 (unsigned short)0xfffff);
1308 a = (bcast = 128) + 1;
1309 printf("%d\n", a);
1310 a = (scast = 65536) + 1;
1311 printf("%d\n", a);
1313 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1315 /* test cast from unsigned to signed short to int */
1316 b = 0xf000;
1317 d = (short)b;
1318 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1319 b = 0xf0f0;
1320 d = (char)b;
1321 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1323 /* test implicit int casting for array accesses */
1324 c = 0;
1325 tab[1] = 2;
1326 tab[c] = 1;
1327 printf("%d %d\n", tab[0], tab[1]);
1329 /* test implicit casting on some operators */
1330 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1331 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1332 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1334 /* from pointer to integer types */
1335 printf("%d %d %ld %ld %lld %lld\n",
1336 (int)p, (unsigned int)p,
1337 (long)p, (unsigned long)p,
1338 (long long)p, (unsigned long long)p);
1340 /* from integers to pointers */
1341 printf("%p %p %p %p\n",
1342 (void *)a, (void *)b, (void *)c, (void *)d);
1345 /* initializers tests */
1346 struct structinit1 {
1347 int f1;
1348 char f2;
1349 short f3;
1350 int farray[3];
1353 int sinit1 = 2;
1354 int sinit2 = { 3 };
1355 int sinit3[3] = { 1, 2, {{3}}, };
1356 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1357 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1358 int sinit6[] = { 1, 2, 3 };
1359 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1360 char sinit8[] = "hello" "trala";
1362 struct structinit1 sinit9 = { 1, 2, 3 };
1363 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1364 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1365 #ifdef ALL_ISOC99
1366 .farray[0] = 10,
1367 .farray[1] = 11,
1368 .farray[2] = 12,
1369 #endif
1372 char *sinit12 = "hello world";
1373 char *sinit13[] = {
1374 "test1",
1375 "test2",
1376 "test3",
1378 char sinit14[10] = { "abc" };
1379 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1381 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1383 struct bar {
1384 char *s;
1385 int len;
1386 } sinit17[] = {
1387 "a1", 4,
1388 "a2", 1
1391 int sinit18[10] = {
1392 [2 ... 5] = 20,
1394 [8] = 10,
1397 struct complexinit0 {
1398 int a;
1399 int b;
1402 struct complexinit {
1403 int a;
1404 const struct complexinit0 *b;
1407 const static struct complexinit cix[] = {
1408 [0] = {
1409 .a = 2000,
1410 .b = (const struct complexinit0[]) {
1411 { 2001, 2002 },
1412 { 2003, 2003 },
1418 struct complexinit2 {
1419 int a;
1420 int b[];
1423 struct complexinit2 cix20;
1425 struct complexinit2 cix21 = {
1426 .a = 3000,
1427 .b = { 3001, 3002, 3003 }
1430 struct complexinit2 cix22 = {
1431 .a = 4000,
1432 .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1435 void init_test(void)
1437 int linit1 = 2;
1438 int linit2 = { 3 };
1439 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1440 int linit6[] = { 1, 2, 3 };
1441 int i, j;
1442 char linit8[] = "hello" "trala";
1443 int linit12[10] = { 1, 2 };
1444 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1445 char linit14[10] = "abc";
1446 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1447 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1448 int linit17 = sizeof(linit17);
1450 printf("init_test:\n");
1452 printf("sinit1=%d\n", sinit1);
1453 printf("sinit2=%d\n", sinit2);
1454 printf("sinit3=%d %d %d %d\n",
1455 sizeof(sinit3),
1456 sinit3[0],
1457 sinit3[1],
1458 sinit3[2]
1460 printf("sinit6=%d\n", sizeof(sinit6));
1461 printf("sinit7=%d %d %d %d\n",
1462 sizeof(sinit7),
1463 sinit7[0],
1464 sinit7[1],
1465 sinit7[2]
1467 printf("sinit8=%s\n", sinit8);
1468 printf("sinit9=%d %d %d\n",
1469 sinit9.f1,
1470 sinit9.f2,
1471 sinit9.f3
1473 printf("sinit10=%d %d %d\n",
1474 sinit10.f1,
1475 sinit10.f2,
1476 sinit10.f3
1478 printf("sinit11=%d %d %d %d %d %d\n",
1479 sinit11.f1,
1480 sinit11.f2,
1481 sinit11.f3,
1482 sinit11.farray[0],
1483 sinit11.farray[1],
1484 sinit11.farray[2]
1487 for(i=0;i<3;i++)
1488 for(j=0;j<2;j++)
1489 printf("[%d][%d] = %d %d %d\n",
1490 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1491 printf("linit1=%d\n", linit1);
1492 printf("linit2=%d\n", linit2);
1493 printf("linit6=%d\n", sizeof(linit6));
1494 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1496 printf("sinit12=%s\n", sinit12);
1497 printf("sinit13=%d %s %s %s\n",
1498 sizeof(sinit13),
1499 sinit13[0],
1500 sinit13[1],
1501 sinit13[2]);
1502 printf("sinit14=%s\n", sinit14);
1504 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1505 printf("\n");
1506 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1507 printf("\n");
1508 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1509 printf("\n");
1510 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1511 printf("\n");
1512 printf("%d %d %d %d\n",
1513 linit16.a1,
1514 linit16.a2,
1515 linit16.a3,
1516 linit16.a4);
1517 /* test that initialisation is done after variable declare */
1518 printf("linit17=%d\n", linit17);
1519 printf("sinit15=%d\n", sinit15[0]);
1520 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1521 printf("sinit17=%s %d %s %d\n",
1522 sinit17[0].s, sinit17[0].len,
1523 sinit17[1].s, sinit17[1].len);
1524 for(i=0;i<10;i++)
1525 printf("%x ", sinit18[i]);
1526 printf("\n");
1527 /* complex init check */
1528 printf("cix: %d %d %d %d %d %d %d\n",
1529 cix[0].a,
1530 cix[0].b[0].a, cix[0].b[0].b,
1531 cix[0].b[1].a, cix[0].b[1].b,
1532 cix[0].b[2].a, cix[0].b[2].b);
1533 printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
1534 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
1538 void switch_test()
1540 int i;
1542 for(i=0;i<15;i++) {
1543 switch(i) {
1544 case 0:
1545 case 1:
1546 printf("a");
1547 break;
1548 default:
1549 printf("%d", i);
1550 break;
1551 case 8 ... 12:
1552 printf("c");
1553 break;
1554 case 3:
1555 printf("b");
1556 break;
1557 case 0xc33c6b9fU:
1558 case 0x7c9eeeb9U:
1559 break;
1562 printf("\n");
1565 /* ISOC99 _Bool type */
1566 void c99_bool_test(void)
1568 #ifdef BOOL_ISOC99
1569 int a;
1570 _Bool b;
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
1583 void bitfield_test(void)
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));
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);
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");
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);
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
1646 /* declare strto* functions as they are C99 */
1647 double strtod(const char *nptr, char **endptr);
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
1657 #define FTEST(prefix, typename, type, fmt)\
1658 void prefix ## cmp(type a, type b)\
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",\
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);\
1681 void prefix ## fcast(type a)\
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);\
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; }\
1719 void prefix ## call(void)\
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));\
1727 void prefix ## signed_zeros(void) \
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");\
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");\
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");\
1756 void prefix ## test(void)\
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();\
1768 FTEST(f, float, float, "%f")
1769 FTEST(d, double, double, "%f")
1770 FTEST(ld, long double, LONG_DOUBLE, "%Lf")
1772 double ftab1[3] = { 1.2, 3.4, -5.6 };
1775 void float_test(void)
1777 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
1778 float fa, fb;
1779 double da, db;
1780 int a;
1781 unsigned int b;
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
1806 int fib(int n)
1808 if (n <= 2)
1809 return 1;
1810 else
1811 return fib(n-1) + fib(n-2);
1814 void funcptr_test()
1816 void (*func)(int);
1817 int a;
1818 struct {
1819 int dummy;
1820 void (*func)(int);
1821 } st1;
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);
1839 void lloptest(long long a, long long b)
1841 unsigned long long ua, ub;
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);
1851 if (b != 0) {
1852 printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1853 a / b,
1854 a % b);
1857 /* binary */
1858 printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1859 a & b,
1860 a | b,
1861 a ^ b);
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);
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);
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);
1891 void llshift(long long a, int b)
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);
1907 void llfloat(void)
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);
1920 fa = la;
1921 da = la;
1922 lda = la;
1923 printf("lltof: %f %f %Lf\n", fa, da, lda);
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);
1930 fa = ula;
1931 da = ula;
1932 lda = ula;
1933 printf("ulltof: %f %f %Lf\n", fa, da, lda);
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);
1941 long long llfunc1(int a)
1943 return a * 2;
1946 struct S {
1947 int id;
1948 char item;
1951 long long int value(struct S *v)
1953 return ((long long int)v->item);
1956 void longlong_test(void)
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);
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);
1985 llfloat();
1986 #if 1
1987 b = 0x12345678;
1988 a = -1;
1989 c = a + b;
1990 printf("%Lx\n", c);
1991 #endif
1993 /* long long reg spill test */
1995 struct S a;
1997 a.item = 3;
1998 printf("%lld\n", value(&a));
2000 lloptest(0x80000000, 0);
2002 /* another long long spill test */
2004 long long *p, v;
2005 v = 1;
2006 p = &v;
2007 p[0]++;
2008 printf("%lld\n", *p);
2011 a = 68719476720LL;
2012 b = 4294967295LL;
2013 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
2015 printf(LONG_LONG_FORMAT "\n", 0x123456789LLU);
2017 /* long long pointer deref in argument passing test */
2018 a = 0x123;
2019 long long *p = &a;
2020 llshift(*p, 5);
2023 void manyarg_test(void)
2025 LONG_DOUBLE ld = 1234567891234LL;
2026 printf("manyarg_test:\n");
2027 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2028 1, 2, 3, 4, 5, 6, 7, 8,
2029 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2030 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2031 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2032 1, 2, 3, 4, 5, 6, 7, 8,
2033 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2034 1234567891234LL, 987654321986LL,
2035 42.0, 43.0);
2036 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2037 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2038 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2039 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2040 1234567891234LL, 987654321986LL,
2041 42.0, 43.0);
2042 printf("%d %d %d %d %d %d %d %d %Lf\n",
2043 1, 2, 3, 4, 5, 6, 7, 8, ld);
2044 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2045 LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n",
2046 1, 2, 3, 4, 5, 6, 7, 8,
2047 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2048 1234567891234LL, 987654321986LL,
2049 42.0, 43.0, ld);
2050 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2051 "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n",
2052 1, 2, 3, 4, 5, 6, 7, 8,
2053 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2054 ld, 1234567891234LL, 987654321986LL,
2055 42.0, 43.0, ld);
2058 void vprintf1(const char *fmt, ...)
2060 va_list ap, aq;
2061 const char *p;
2062 int c, i;
2063 double d;
2064 long long ll;
2065 LONG_DOUBLE ld;
2067 va_start(aq, fmt);
2068 va_copy(ap, aq);
2070 p = fmt;
2071 for(;;) {
2072 c = *p;
2073 if (c == '\0')
2074 break;
2075 p++;
2076 if (c == '%') {
2077 c = *p;
2078 switch(c) {
2079 case '\0':
2080 goto the_end;
2081 case 'd':
2082 i = va_arg(ap, int);
2083 printf("%d", i);
2084 break;
2085 case 'f':
2086 d = va_arg(ap, double);
2087 printf("%f", d);
2088 break;
2089 case 'l':
2090 ll = va_arg(ap, long long);
2091 printf(LONG_LONG_FORMAT, ll);
2092 break;
2093 case 'F':
2094 ld = va_arg(ap, LONG_DOUBLE);
2095 printf("%Lf", ld);
2096 break;
2098 p++;
2099 } else {
2100 putchar(c);
2103 the_end:
2104 va_end(aq);
2105 va_end(ap);
2108 struct myspace {
2109 short int profile;
2112 void stdarg_for_struct(struct myspace bob, ...)
2114 struct myspace george, bill;
2115 va_list ap;
2116 short int validate;
2118 va_start(ap, bob);
2119 bill = va_arg(ap, struct myspace);
2120 george = va_arg(ap, struct myspace);
2121 validate = va_arg(ap, int);
2122 printf("stdarg_for_struct: %d %d %d %d\n",
2123 bob.profile, bill.profile, george.profile, validate);
2124 va_end(ap);
2127 void stdarg_for_libc(const char *fmt, ...)
2129 va_list args;
2130 va_start(args, fmt);
2131 vprintf(fmt, args);
2132 va_end(args);
2135 void stdarg_test(void)
2137 LONG_DOUBLE ld = 1234567891234LL;
2138 struct myspace bob;
2140 vprintf1("%d %d %d\n", 1, 2, 3);
2141 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2142 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2143 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2144 vprintf1("%d %f %l %F %d %f %l %F\n",
2145 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2146 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2147 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);
2149 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2150 1, 2, 3, 4, 5, 6, 7, 8,
2151 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2152 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2153 "%l %l %f %f\n",
2154 1, 2, 3, 4, 5, 6, 7, 8,
2155 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2156 1234567891234LL, 987654321986LL,
2157 42.0, 43.0);
2158 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2159 "%l %l %f %f\n",
2160 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2161 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2162 1234567891234LL, 987654321986LL,
2163 42.0, 43.0);
2164 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2165 1, 2, 3, 4, 5, 6, 7, 8, ld);
2166 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2167 "%l %l %f %f %F\n",
2168 1, 2, 3, 4, 5, 6, 7, 8,
2169 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2170 1234567891234LL, 987654321986LL,
2171 42.0, 43.0, ld);
2172 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2173 "%F %l %l %f %f %F\n",
2174 1, 2, 3, 4, 5, 6, 7, 8,
2175 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2176 ld, 1234567891234LL, 987654321986LL,
2177 42.0, 43.0, ld);
2179 bob.profile = 42;
2180 stdarg_for_struct(bob, bob, bob, bob.profile);
2181 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2184 void whitespace_test(void)
2186 char *str;
2188 \f\v #if 1
2189 pri\
2190 ntf("whitspace:\n");\f\v
2191 #endif
2192 pf("N=%d\n", 2);
2194 #ifdef CORRECT_CR_HANDLING
2195 pri\
2196 ntf("aaa=%d\n", 3);
2197 #endif
2199 pri\
2201 ntf("min=%d\n", 4);
2203 #ifdef ACCEPT_CR_IN_STRINGS
2204 printf("len1=%d\n", strlen("
2205 "));
2206 #ifdef CORRECT_CR_HANDLING
2207 str = "
2209 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
2210 #endif
2211 printf("len1=%d\n", strlen(" a
2212 "));
2213 #endif /* ACCEPT_CR_IN_STRINGS */
2216 int reltab[3] = { 1, 2, 3 };
2218 int *rel1 = &reltab[1];
2219 int *rel2 = &reltab[2];
2221 void relocation_test(void)
2223 printf("*rel1=%d\n", *rel1);
2224 printf("*rel2=%d\n", *rel2);
2227 void old_style_f(a,b,c)
2228 int a, b;
2229 double c;
2231 printf("a=%d b=%d b=%f\n", a, b, c);
2234 void decl_func1(int cmpfn())
2236 printf("cmpfn=%lx\n", (long)cmpfn);
2239 void decl_func2(cmpfn)
2240 int cmpfn();
2242 printf("cmpfn=%lx\n", (long)cmpfn);
2245 void old_style_function(void)
2247 old_style_f((void *)1, 2, 3.0);
2248 decl_func1(NULL);
2249 decl_func2(NULL);
2252 void alloca_test()
2254 #if defined __i386__ || defined __x86_64__ || defined __arm__
2255 char *p = alloca(16);
2256 strcpy(p,"123456789012345");
2257 printf("alloca: p is %s\n", p);
2258 char *demo = "This is only a test.\n";
2259 /* Test alloca embedded in a larger expression */
2260 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
2261 #endif
2264 void *bounds_checking_is_enabled()
2266 char ca[10], *cp = ca-1;
2267 return (ca != cp + 1) ? cp : NULL;
2270 typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
2272 void c99_vla_test(int size1, int size2)
2274 #if defined __i386__ || defined __x86_64__
2275 int size = size1 * size2;
2276 int tab1[size][2], tab2[10][2];
2277 void *tab1_ptr, *tab2_ptr, *bad_ptr;
2279 /* "size" should have been 'captured' at tab1 declaration,
2280 so modifying it should have no effect on VLA behaviour. */
2281 size = size-1;
2283 printf("Test C99 VLA 1 (sizeof): ");
2284 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
2285 tab1_ptr = tab1;
2286 tab2_ptr = tab2;
2287 printf("Test C99 VLA 2 (ptrs subtract): ");
2288 printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2289 printf("Test C99 VLA 3 (ptr add): ");
2290 printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2291 printf("Test C99 VLA 4 (ptr access): ");
2292 tab1[size1][1] = 42;
2293 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2295 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2296 if (bad_ptr = bounds_checking_is_enabled()) {
2297 int *t1 = &tab1[size1 * size2 - 1][3];
2298 int *t2 = &tab2[9][3];
2299 printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
2300 printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
2302 char*c1 = 1 + sizeof(tab1) + (char*)tab1;
2303 char*c2 = 1 + sizeof(tab2) + (char*)tab2;
2304 printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
2305 printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
2307 int *i1 = tab1[-1];
2308 int *i2 = tab2[-1];
2309 printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
2310 printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
2312 int *x1 = tab1[size1 * size2 + 1];
2313 int *x2 = tab2[10 + 1];
2314 printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
2315 printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
2316 } else {
2317 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2319 printf("\n");
2320 #endif
2323 #ifndef __TINYC__
2324 typedef __SIZE_TYPE__ uintptr_t;
2325 #endif
2327 void sizeof_test(void)
2329 int a;
2330 int **ptr;
2332 printf("sizeof(int) = %d\n", sizeof(int));
2333 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2334 printf("sizeof(long) = %d\n", sizeof(long));
2335 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2336 printf("sizeof(short) = %d\n", sizeof(short));
2337 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2338 printf("sizeof(char) = %d\n", sizeof(char));
2339 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2340 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2341 a = 1;
2342 printf("sizeof(a++) = %d\n", sizeof a++);
2343 printf("a=%d\n", a);
2344 ptr = NULL;
2345 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
2347 /* The type of sizeof should be as large as a pointer, actually
2348 it should be size_t. */
2349 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2350 uintptr_t t = 1;
2351 uintptr_t t2;
2352 /* Effectively <<32, but defined also on 32bit machines. */
2353 t <<= 16;
2354 t <<= 16;
2355 t++;
2356 /* This checks that sizeof really can be used to manipulate
2357 uintptr_t objects, without truncation. */
2358 t2 = t & -sizeof(uintptr_t);
2359 printf ("%lu %lu\n", t, t2);
2361 /* some alignof tests */
2362 printf("__alignof__(int) = %d\n", __alignof__(int));
2363 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2364 printf("__alignof__(short) = %d\n", __alignof__(short));
2365 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2366 printf("__alignof__(char) = %d\n", __alignof__(char));
2367 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2368 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
2371 void typeof_test(void)
2373 double a;
2374 typeof(a) b;
2375 typeof(float) c;
2377 a = 1.5;
2378 b = 2.5;
2379 c = 3.5;
2380 printf("a=%f b=%f c=%f\n", a, b, c);
2383 void statement_expr_test(void)
2385 int a, i;
2387 a = 0;
2388 for(i=0;i<10;i++) {
2389 a += 1 +
2390 ( { int b, j;
2391 b = 0;
2392 for(j=0;j<5;j++)
2393 b += j; b;
2394 } );
2396 printf("a=%d\n", a);
2400 void local_label_test(void)
2402 int a;
2403 goto l1;
2405 a = 1 + ({
2406 __label__ l1, l2, l3, l4;
2407 goto l1;
2409 printf("aa1\n");
2410 goto l3;
2412 printf("aa3\n");
2413 goto l4;
2415 printf("aa2\n");
2416 goto l2;
2417 l3:;
2420 printf("a=%d\n", a);
2421 return;
2423 printf("bb1\n");
2424 goto l2;
2426 printf("bb2\n");
2427 goto l4;
2430 /* inline assembler test */
2431 #if defined(__i386__) || defined(__x86_64__)
2433 /* from linux kernel */
2434 static char * strncat1(char * dest,const char * src,size_t count)
2436 long d0, d1, d2, d3;
2437 __asm__ __volatile__(
2438 "repne\n\t"
2439 "scasb\n\t"
2440 "dec %1\n\t"
2441 "mov %8,%3\n"
2442 "1:\tdec %3\n\t"
2443 "js 2f\n\t"
2444 "lodsb\n\t"
2445 "stosb\n\t"
2446 "testb %%al,%%al\n\t"
2447 "jne 1b\n"
2448 "2:\txor %2,%2\n\t"
2449 "stosb"
2450 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2451 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2452 : "memory");
2453 return dest;
2456 static char * strncat2(char * dest,const char * src,size_t count)
2458 long d0, d1, d2, d3;
2459 __asm__ __volatile__(
2460 "repne scasb\n\t" /* one-line repne prefix + string op */
2461 "dec %1\n\t"
2462 "mov %8,%3\n"
2463 "1:\tdec %3\n\t"
2464 "js 2f\n\t"
2465 "lodsb\n\t"
2466 "stosb\n\t"
2467 "testb %%al,%%al\n\t"
2468 "jne 1b\n"
2469 "2:\txor %2,%2\n\t"
2470 "stosb"
2471 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2472 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2473 : "memory");
2474 return dest;
2477 static inline void * memcpy1(void * to, const void * from, size_t n)
2479 long d0, d1, d2;
2480 __asm__ __volatile__(
2481 "rep ; movsl\n\t"
2482 "testb $2,%b4\n\t"
2483 "je 1f\n\t"
2484 "movsw\n"
2485 "1:\ttestb $1,%b4\n\t"
2486 "je 2f\n\t"
2487 "movsb\n"
2488 "2:"
2489 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2490 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2491 : "memory");
2492 return (to);
2495 static inline void * memcpy2(void * to, const void * from, size_t n)
2497 long d0, d1, d2;
2498 __asm__ __volatile__(
2499 "rep movsl\n\t" /* one-line rep prefix + string op */
2500 "testb $2,%b4\n\t"
2501 "je 1f\n\t"
2502 "movsw\n"
2503 "1:\ttestb $1,%b4\n\t"
2504 "je 2f\n\t"
2505 "movsb\n"
2506 "2:"
2507 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2508 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2509 : "memory");
2510 return (to);
2513 static __inline__ void sigaddset1(unsigned int *set, int _sig)
2515 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2518 static __inline__ void sigdelset1(unsigned int *set, int _sig)
2520 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2523 static __inline__ __const__ unsigned int swab32(unsigned int x)
2525 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2526 "rorl $16,%0\n\t" /* swap words */
2527 "xchgb %b0,%h0" /* swap higher bytes */
2528 :"=q" (x)
2529 : "0" (x));
2530 return x;
2533 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
2535 unsigned long long res;
2536 #ifdef __x86_64__
2537 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
2538 but still test the 32bit->64bit mull. */
2539 unsigned int resh, resl;
2540 __asm__("mull %2" : "=a" (resl), "=d" (resh) : "a" (a), "r" (b));
2541 res = ((unsigned long long)resh << 32) | resl;
2542 #else
2543 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
2544 #endif
2545 return res;
2548 static __inline__ unsigned long long inc64(unsigned long long a)
2550 unsigned long long res;
2551 #ifdef __x86_64__
2552 /* Using the A constraint is wrong, and increments are tested
2553 elsewere. */
2554 res = a + 1;
2555 #else
2556 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
2557 #endif
2558 return res;
2561 unsigned int set;
2563 void asm_test(void)
2565 char buf[128];
2566 unsigned int val;
2568 printf("inline asm:\n");
2569 /* test the no operand case */
2570 asm volatile ("xorl %eax, %eax");
2572 memcpy1(buf, "hello", 6);
2573 strncat1(buf, " worldXXXXX", 3);
2574 printf("%s\n", buf);
2576 memcpy2(buf, "hello", 6);
2577 strncat2(buf, " worldXXXXX", 3);
2578 printf("%s\n", buf);
2580 /* 'A' constraint test */
2581 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2582 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2584 set = 0xff;
2585 sigdelset1(&set, 2);
2586 sigaddset1(&set, 16);
2587 /* NOTE: we test here if C labels are correctly restored after the
2588 asm statement */
2589 goto label1;
2590 label2:
2591 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
2592 #ifdef __GNUC__ // works strange with GCC 4.3
2593 set=0x1080fd;
2594 #endif
2595 printf("set=0x%x\n", set);
2596 val = 0x01020304;
2597 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
2598 return;
2599 label1:
2600 goto label2;
2603 #else
2605 void asm_test(void)
2609 #endif
2611 #define COMPAT_TYPE(type1, type2) \
2613 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2614 __builtin_types_compatible_p (type1, type2));\
2617 int constant_p_var;
2619 void builtin_test(void)
2621 #if GCC_MAJOR >= 3
2622 COMPAT_TYPE(int, int);
2623 COMPAT_TYPE(int, unsigned int);
2624 COMPAT_TYPE(int, char);
2625 COMPAT_TYPE(int, const int);
2626 COMPAT_TYPE(int, volatile int);
2627 COMPAT_TYPE(int *, int *);
2628 COMPAT_TYPE(int *, void *);
2629 COMPAT_TYPE(int *, const int *);
2630 COMPAT_TYPE(char *, unsigned char *);
2631 COMPAT_TYPE(char *, signed char *);
2632 COMPAT_TYPE(char *, char *);
2633 /* space is needed because tcc preprocessor introduces a space between each token */
2634 COMPAT_TYPE(char * *, void *);
2635 #endif
2636 printf("res = %d\n", __builtin_constant_p(1));
2637 printf("res = %d\n", __builtin_constant_p(1 + 2));
2638 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
2639 printf("res = %d\n", __builtin_constant_p(constant_p_var));
2642 #ifndef _WIN32
2643 extern int __attribute__((weak)) weak_f1(void);
2644 extern int __attribute__((weak)) weak_f2(void);
2645 extern int weak_f3(void);
2646 extern int __attribute__((weak)) weak_v1;
2647 extern int __attribute__((weak)) weak_v2;
2648 extern int weak_v3;
2650 extern int (*weak_fpa)() __attribute__((weak));
2651 extern int __attribute__((weak)) (*weak_fpb)();
2652 extern __attribute__((weak)) int (*weak_fpc)();
2654 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
2655 extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
2656 extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
2657 extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
2658 extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
2659 extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
2661 static const size_t dummy = 0;
2662 extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
2663 extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
2664 extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
2666 int some_lib_func(void);
2667 int dummy_impl_of_slf(void) { return 444; }
2668 int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
2670 int weak_toolate() __attribute__((weak));
2671 int weak_toolate() { return 0; }
2673 void __attribute__((weak)) weak_test(void)
2675 printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
2676 printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
2677 printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
2678 printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
2679 printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
2680 printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
2682 printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
2683 printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
2684 printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
2686 printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
2687 printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
2688 printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
2689 printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
2690 printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
2691 printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
2694 int __attribute__((weak)) weak_f2() { return 222; }
2695 int __attribute__((weak)) weak_f3() { return 333; }
2696 int __attribute__((weak)) weak_v2 = 222;
2697 int __attribute__((weak)) weak_v3 = 333;
2698 #endif
2700 void const_func(const int a)
2704 void const_warn_test(void)
2706 const_func(1);
2709 struct condstruct {
2710 int i;
2713 int getme (struct condstruct *s, int i)
2715 int i1 = (i == 0 ? 0 : s)->i;
2716 int i2 = (i == 0 ? s : 0)->i;
2717 int i3 = (i == 0 ? (void*)0 : s)->i;
2718 int i4 = (i == 0 ? s : (void*)0)->i;
2719 return i1 + i2 + i3 + i4;
2722 struct global_data
2724 int a[40];
2725 int *b[40];
2728 struct global_data global_data;
2730 int global_data_getstuff (int *, int);
2732 void global_data_callit (int i)
2734 *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
2737 int global_data_getstuff (int *p, int i)
2739 return *p + i;
2742 void global_data_test (void)
2744 global_data.a[0] = 42;
2745 global_data.b[0] = &global_data.a[0];
2746 global_data_callit (0);
2747 printf ("%d\n", global_data.a[0]);
2750 struct cmpcmpS
2752 unsigned char fill : 3;
2753 unsigned char b1 : 1;
2754 unsigned char b2 : 1;
2755 unsigned char fill2 : 3;
2758 int glob1, glob2, glob3;
2760 void compare_comparisons (struct cmpcmpS *s)
2762 if (s->b1 != (glob1 == glob2)
2763 || (s->b2 != (glob1 == glob3)))
2764 printf ("comparing comparisons broken\n");
2767 void cmp_comparison_test(void)
2769 struct cmpcmpS s;
2770 s.b1 = 1;
2771 glob1 = 42; glob2 = 42;
2772 s.b2 = 0;
2773 glob3 = 43;
2774 compare_comparisons (&s);
2777 int fcompare (double a, double b, int code)
2779 switch (code) {
2780 case 0: return a == b;
2781 case 1: return a != b;
2782 case 2: return a < b;
2783 case 3: return a >= b;
2784 case 4: return a > b;
2785 case 5: return a <= b;
2789 void math_cmp_test(void)
2791 double nan = 0.0/0.0;
2792 double one = 1.0;
2793 double two = 2.0;
2794 int comp = 0;
2795 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
2797 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
2798 And it does this in various ways so that all code generation paths
2799 are checked (generating inverted tests, or non-inverted tests, or
2800 producing a 0/1 value without jumps (that's done in the fcompare
2801 function). */
2802 #define FCMP(a,b,op,iop,code) \
2803 if (fcompare (a,b,code)) \
2804 bug (a,b,op,iop,1); \
2805 if (a op b) \
2806 bug (a,b,op,iop,2); \
2807 if (a iop b) \
2809 else \
2810 bug (a,b,op,iop,3); \
2811 if ((a op b) || comp) \
2812 bug (a,b,op,iop,4); \
2813 if ((a iop b) || comp) \
2815 else \
2816 bug (a,b,op,iop,5);
2818 /* Equality tests. */
2819 FCMP(nan, nan, ==, !=, 0);
2820 FCMP(one, two, ==, !=, 0);
2821 FCMP(one, one, !=, ==, 1);
2822 /* Non-equality is a bit special. */
2823 if (!fcompare (nan, nan, 1))
2824 bug (nan, nan, !=, ==, 6);
2826 /* Relational tests on numbers. */
2827 FCMP(two, one, <, >=, 2);
2828 FCMP(one, two, >=, <, 3);
2829 FCMP(one, two, >, <=, 4);
2830 FCMP(two, one, <=, >, 5);
2832 /* Relational tests on NaNs. Note that the inverse op here is
2833 always !=, there's no operator in C that is equivalent to !(a < b),
2834 when NaNs are involved, same for the other relational ops. */
2835 FCMP(nan, nan, <, !=, 2);
2836 FCMP(nan, nan, >=, !=, 3);
2837 FCMP(nan, nan, >, !=, 4);
2838 FCMP(nan, nan, <=, !=, 5);
2841 double get100 () { return 100.0; }
2843 void callsave_test(void)
2845 #if defined __i386__ || defined __x86_64__ || defined __arm__
2846 int i, s; double *d; double t;
2847 s = sizeof (double);
2848 printf ("callsavetest: %d\n", s);
2849 d = alloca (sizeof(double));
2850 d[0] = 10.0;
2851 /* x86-64 had a bug were the next call to get100 would evict
2852 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
2853 in int type, not pointer type. When alloca returns a pointer
2854 with the high 32 bit set (which is likely on x86-64) the access
2855 generates a segfault. */
2856 i = d[0] > get100 ();
2857 printf ("%d\n", i);
2858 #endif
2862 void bfa3(ptrdiff_t str_offset)
2864 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
2866 void bfa2(ptrdiff_t str_offset)
2868 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
2869 bfa3(str_offset);
2871 void bfa1(ptrdiff_t str_offset)
2873 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
2874 bfa2(str_offset);
2877 void builtin_frame_address_test(void)
2879 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
2880 #ifndef __arm__
2881 char str[] = "__builtin_frame_address";
2882 char *fp0 = __builtin_frame_address(0);
2884 printf("str: %s\n", str);
2885 bfa1(str-fp0);
2886 #endif
2889 char via_volatile (char i)
2891 char volatile vi;
2892 vi = i;
2893 return vi;