cleanup: put prototypes in front of libtcc.c
[tinycc/k1w1.git] / tests / tcctest.c
bloba2d481a6aba391fc7f00507ac5e62398afbacfb2
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 /* deprecated and no longer supported in gcc 3.3 */
20 //#define ACCEPT_CR_IN_STRINGS
22 /* __VA_ARGS__ and __func__ support */
23 #define C99_MACROS
25 /* test various include syntaxes */
27 #define TCCLIB_INC <tcclib.h>
28 #define TCCLIB_INC1 <tcclib
29 #define TCCLIB_INC2 h>
30 #define TCCLIB_INC3 "tcclib"
32 #include TCCLIB_INC
34 #include TCCLIB_INC1.TCCLIB_INC2
36 #include TCCLIB_INC1.h>
38 /* gcc 3.2 does not accept that (bug ?) */
39 //#include TCCLIB_INC3 ".h"
41 #include <tcclib.h>
43 #include "tcclib.h"
45 void string_test();
46 void expr_test();
47 void macro_test();
48 void scope_test();
49 void forward_test();
50 void funcptr_test();
51 void loop_test();
52 void switch_test();
53 void goto_test();
54 void enum_test();
55 void typedef_test();
56 void struct_test();
57 void array_test();
58 void expr_ptr_test();
59 void bool_test();
60 void expr2_test();
61 void constant_expr_test();
62 void expr_cmp_test();
63 void char_short_test();
64 void init_test(void);
65 void compound_literal_test(void);
66 int kr_test();
67 void struct_assign_test(void);
68 void cast_test(void);
69 void bitfield_test(void);
70 void c99_bool_test(void);
71 void float_test(void);
72 void longlong_test(void);
73 void manyarg_test(void);
74 void stdarg_test(void);
75 void whitespace_test(void);
76 void relocation_test(void);
77 void old_style_function(void);
78 void alloca_test(void);
79 void sizeof_test(void);
80 void typeof_test(void);
81 void local_label_test(void);
82 void statement_expr_test(void);
83 void asm_test(void);
84 void builtin_test(void);
86 int fib(int n);
87 void num(int n);
88 void forward_ref(void);
89 int isid(int c);
91 #define A 2
92 #define N 1234 + A
93 #define pf printf
94 #define M1(a, b) (a) + (b)
96 #define str\
97 (s) # s
98 #define glue(a, b) a ## b
99 #define xglue(a, b) glue(a, b)
100 #define HIGHLOW "hello"
101 #define LOW LOW ", world"
103 #define min(a, b) ((a) < (b) ? (a) : (b))
105 #ifdef C99_MACROS
106 #define dprintf(level,...) printf(__VA_ARGS__)
107 #endif
109 /* gcc vararg macros */
110 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
112 #define MACRO_NOARGS()
114 #define AAA 3
115 #undef AAA
116 #define AAA 4
118 #if 1
119 #define B3 1
120 #elif 1
121 #define B3 2
122 #elif 0
123 #define B3 3
124 #else
125 #define B3 4
126 #endif
128 #define __INT64_C(c) c ## LL
129 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
131 int qq(int x)
133 return x + 40;
135 #define qq(x) x
137 #define spin_lock(lock) do { } while (0)
138 #define wq_spin_lock spin_lock
139 #define TEST2() wq_spin_lock(a)
141 void macro_test(void)
143 printf("macro:\n");\f\v
144 pf("N=%d\n", N);
145 printf("aaa=%d\n", AAA);
147 printf("min=%d\n", min(1, min(2, -1)));
149 printf("s1=%s\n", glue(HIGH, LOW));
150 printf("s2=%s\n", xglue(HIGH, LOW));
151 printf("s3=%s\n", str("c"));
152 printf("s4=%s\n", str(a1));
153 printf("B3=%d\n", B3);
155 #ifdef A
156 printf("A defined\n");
157 #endif
158 #ifdef B
159 printf("B defined\n");
160 #endif
161 #ifdef A
162 printf("A defined\n");
163 #else
164 printf("A not defined\n");
165 #endif
166 #ifdef B
167 printf("B defined\n");
168 #else
169 printf("B not defined\n");
170 #endif
172 #ifdef A
173 printf("A defined\n");
174 #ifdef B
175 printf("B1 defined\n");
176 #else
177 printf("B1 not defined\n");
178 #endif
179 #else
180 printf("A not defined\n");
181 #ifdef B
182 printf("B2 defined\n");
183 #else
184 printf("B2 not defined\n");
185 #endif
186 #endif
188 #if 1+1
189 printf("test true1\n");
190 #endif
191 #if 0
192 printf("test true2\n");
193 #endif
194 #if 1-1
195 printf("test true3\n");
196 #endif
197 #if defined(A)
198 printf("test trueA\n");
199 #endif
200 #if defined(B)
201 printf("test trueB\n");
202 #endif
204 #if 0
205 printf("test 0\n");
206 #elif 0
207 printf("test 1\n");
208 #elif 2
209 printf("test 2\n");
210 #else
211 printf("test 3\n");
212 #endif
214 MACRO_NOARGS();
216 #ifdef __LINE__
217 printf("__LINE__ defined\n");
218 #endif
220 printf("__LINE__=%d __FILE__=%s\n",
221 __LINE__, __FILE__);
222 #line 200
223 printf("__LINE__=%d __FILE__=%s\n",
224 __LINE__, __FILE__);
225 #line 203 "test"
226 printf("__LINE__=%d __FILE__=%s\n",
227 __LINE__, __FILE__);
228 #line 227 "tcctest.c"
230 /* not strictly preprocessor, but we test it there */
231 #ifdef C99_MACROS
232 printf("__func__ = %s\n", __func__);
233 dprintf(1, "vaarg=%d\n", 1);
234 #endif
235 dprintf1(1, "vaarg1\n");
236 dprintf1(1, "vaarg1=%d\n", 2);
237 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
239 /* gcc extension */
240 printf("func='%s'\n", __FUNCTION__);
242 /* complicated macros in glibc */
243 printf("INT64_MIN=%Ld\n", INT64_MIN);
245 int a;
246 a = 1;
247 glue(a+, +);
248 printf("a=%d\n", a);
249 glue(a <, <= 2);
250 printf("a=%d\n", a);
253 /* macro function with argument outside the macro string */
254 #define MF_s MF_hello
255 #define MF_hello(msg) printf("%s\n",msg)
257 #define MF_t printf("tralala\n"); MF_hello
259 MF_s("hi");
260 MF_t("hi");
262 /* test macro substituion inside args (should not eat stream) */
263 printf("qq=%d\n", qq(qq)(2));
265 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
266 null argument without a space. gcc 3.2 fixes that. */
268 #define qq1(x) 1
269 printf("qq1=%d\n", qq1( ));
271 /* comment with stray handling *\
273 /* this is a valid *\/ comment */
274 /* this is a valid comment *\*/
275 // this is a valid\
276 comment
278 /* test function macro substitution when the function name is
279 substituted */
280 TEST2();
283 int op(a,b)
285 return a / b;
288 int ret(a)
290 if (a == 2)
291 return 1;
292 if (a == 3)
293 return 2;
294 return 0;
297 void ps(const char *s)
299 int c;
300 while (1) {
301 c = *s;
302 if (c == 0)
303 break;
304 printf("%c", c);
305 s++;
309 const char foo1_string[] = "\
310 bar\n\
311 test\14\
314 void string_test()
316 unsigned int b;
317 printf("string:\n");
318 printf("\141\1423\143\n");/* dezdez test */
319 printf("\x41\x42\x43\x3a\n");
320 printf("c=%c\n", 'r');
321 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
322 printf("foo1_string='%s'\n", foo1_string);
323 #if 0
324 printf("wstring=%S\n", L"abc");
325 printf("wstring=%S\n", L"abc" L"def" "ghi");
326 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
327 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
328 #endif
329 ps("test\n");
330 b = 32;
331 while ((b = b + 1) < 96) {
332 printf("%c", b);
334 printf("\n");
335 printf("fib=%d\n", fib(33));
336 b = 262144;
337 while (b != 0x80000000) {
338 num(b);
339 b = b * 2;
343 void loop_test()
345 int i;
346 i = 0;
347 while (i < 10)
348 printf("%d", i++);
349 printf("\n");
350 for(i = 0; i < 10;i++)
351 printf("%d", i);
352 printf("\n");
353 i = 0;
354 do {
355 printf("%d", i++);
356 } while (i < 10);
357 printf("\n");
359 /* break/continue tests */
360 i = 0;
361 while (1) {
362 if (i == 6)
363 break;
364 i++;
365 if (i == 3)
366 continue;
367 printf("%d", i);
369 printf("\n");
371 /* break/continue tests */
372 i = 0;
373 do {
374 if (i == 6)
375 break;
376 i++;
377 if (i == 3)
378 continue;
379 printf("%d", i);
380 } while(1);
381 printf("\n");
383 for(i = 0;i < 10;i++) {
384 if (i == 3)
385 continue;
386 printf("%d", i);
388 printf("\n");
392 void goto_test()
394 int i;
395 static void *label_table[3] = { &&label1, &&label2, &&label3 };
397 printf("goto:\n");
398 i = 0;
399 s_loop:
400 if (i >= 10)
401 goto s_end;
402 printf("%d", i);
403 i++;
404 goto s_loop;
405 s_end:
406 printf("\n");
408 /* we also test computed gotos (GCC extension) */
409 for(i=0;i<3;i++) {
410 goto *label_table[i];
411 label1:
412 printf("label1\n");
413 goto next;
414 label2:
415 printf("label2\n");
416 goto next;
417 label3:
418 printf("label3\n");
419 next: ;
423 enum {
425 E1 = 2,
426 E2 = 4,
431 enum test {
432 E5 = 1000,
435 void enum_test()
437 enum test b1;
438 printf("enum:\n%d %d %d %d %d %d\n",
439 E0, E1, E2, E3, E4, E5);
440 b1 = 1;
441 printf("b1=%d\n", b1);
444 typedef int *my_ptr;
446 typedef int mytype1;
447 typedef int mytype2;
449 void typedef_test()
451 my_ptr a;
452 mytype1 mytype2;
453 int b;
455 a = &b;
456 *a = 1234;
457 printf("typedef:\n");
458 printf("a=%d\n", *a);
459 mytype2 = 2;
460 printf("mytype2=%d\n", mytype2);
463 void forward_test()
465 printf("forward:\n");
466 forward_ref();
467 forward_ref();
471 void forward_ref(void)
473 printf("forward ok\n");
476 typedef struct struct1 {
477 int f1;
478 int f2, f3;
479 union union1 {
480 int v1;
481 int v2;
482 } u;
483 char str[3];
484 } struct1;
486 struct struct2 {
487 int a;
488 char b;
491 union union2 {
492 int w1;
493 int w2;
496 struct struct1 st1, st2;
498 int main(int argc, char **argv)
500 string_test();
501 expr_test();
502 macro_test();
503 scope_test();
504 forward_test();
505 funcptr_test();
506 loop_test();
507 switch_test();
508 goto_test();
509 enum_test();
510 typedef_test();
511 struct_test();
512 array_test();
513 expr_ptr_test();
514 bool_test();
515 expr2_test();
516 constant_expr_test();
517 expr_cmp_test();
518 char_short_test();
519 init_test();
520 compound_literal_test();
521 kr_test();
522 struct_assign_test();
523 cast_test();
524 bitfield_test();
525 c99_bool_test();
526 float_test();
527 longlong_test();
528 manyarg_test();
529 stdarg_test();
530 whitespace_test();
531 relocation_test();
532 old_style_function();
533 alloca_test();
534 sizeof_test();
535 typeof_test();
536 statement_expr_test();
537 local_label_test();
538 asm_test();
539 builtin_test();
540 return 0;
543 int tab[3];
544 int tab2[3][2];
546 int g;
548 void f1(g)
550 printf("g1=%d\n", g);
553 void scope_test()
555 printf("scope:\n");
556 g = 2;
557 f1(1);
558 printf("g2=%d\n", g);
560 int g;
561 g = 3;
562 printf("g3=%d\n", g);
564 int g;
565 g = 4;
566 printf("g4=%d\n", g);
569 printf("g5=%d\n", g);
572 void array_test(int a[4])
574 int i, j;
576 printf("array:\n");
577 printf("sizeof(a) = %d\n", sizeof(a));
578 printf("sizeof(\"a\") = %d\n", sizeof("a"));
579 #ifdef C99_MACROS
580 printf("sizeof(__func__) = %d\n", sizeof(__func__));
581 #endif
582 printf("sizeof tab %d\n", sizeof(tab));
583 printf("sizeof tab2 %d\n", sizeof tab2);
584 tab[0] = 1;
585 tab[1] = 2;
586 tab[2] = 3;
587 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
588 for(i=0;i<3;i++)
589 for(j=0;j<2;j++)
590 tab2[i][j] = 10 * i + j;
591 for(i=0;i<3*2;i++) {
592 printf(" %3d", ((int *)tab2)[i]);
594 printf("\n");
597 void expr_test()
599 int a, b;
600 a = 0;
601 printf("%d\n", a += 1);
602 printf("%d\n", a -= 2);
603 printf("%d\n", a *= 31232132);
604 printf("%d\n", a /= 4);
605 printf("%d\n", a %= 20);
606 printf("%d\n", a &= 6);
607 printf("%d\n", a ^= 7);
608 printf("%d\n", a |= 8);
609 printf("%d\n", a >>= 3);
610 printf("%d\n", a <<= 4);
612 a = 22321;
613 b = -22321;
614 printf("%d\n", a + 1);
615 printf("%d\n", a - 2);
616 printf("%d\n", a * 312);
617 printf("%d\n", a / 4);
618 printf("%d\n", b / 4);
619 printf("%d\n", (unsigned)b / 4);
620 printf("%d\n", a % 20);
621 printf("%d\n", b % 20);
622 printf("%d\n", (unsigned)b % 20);
623 printf("%d\n", a & 6);
624 printf("%d\n", a ^ 7);
625 printf("%d\n", a | 8);
626 printf("%d\n", a >> 3);
627 printf("%d\n", b >> 3);
628 printf("%d\n", (unsigned)b >> 3);
629 printf("%d\n", a << 4);
630 printf("%d\n", ~a);
631 printf("%d\n", -a);
632 printf("%d\n", +a);
634 printf("%d\n", 12 + 1);
635 printf("%d\n", 12 - 2);
636 printf("%d\n", 12 * 312);
637 printf("%d\n", 12 / 4);
638 printf("%d\n", 12 % 20);
639 printf("%d\n", 12 & 6);
640 printf("%d\n", 12 ^ 7);
641 printf("%d\n", 12 | 8);
642 printf("%d\n", 12 >> 2);
643 printf("%d\n", 12 << 4);
644 printf("%d\n", ~12);
645 printf("%d\n", -12);
646 printf("%d\n", +12);
647 printf("%d %d %d %d\n",
648 isid('a'),
649 isid('g'),
650 isid('T'),
651 isid('('));
654 int isid(int c)
656 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
659 /**********************/
661 int vstack[10], *vstack_ptr;
663 void vpush(int vt, int vc)
665 *vstack_ptr++ = vt;
666 *vstack_ptr++ = vc;
669 void vpop(int *ft, int *fc)
671 *fc = *--vstack_ptr;
672 *ft = *--vstack_ptr;
675 void expr2_test()
677 int a, b;
679 printf("expr2:\n");
680 vstack_ptr = vstack;
681 vpush(1432432, 2);
682 vstack_ptr[-2] &= ~0xffffff80;
683 vpop(&a, &b);
684 printf("res= %d %d\n", a, b);
687 void constant_expr_test()
689 int a;
690 printf("constant_expr:\n");
691 a = 3;
692 printf("%d\n", a * 16);
693 printf("%d\n", a * 1);
694 printf("%d\n", a + 0);
697 int tab4[10];
699 void expr_ptr_test()
701 int *p, *q;
702 int i = -1;
704 printf("expr_ptr:\n");
705 p = tab4;
706 q = tab4 + 10;
707 printf("diff=%d\n", q - p);
708 p++;
709 printf("inc=%d\n", p - tab4);
710 p--;
711 printf("dec=%d\n", p - tab4);
712 ++p;
713 printf("inc=%d\n", p - tab4);
714 --p;
715 printf("dec=%d\n", p - tab4);
716 printf("add=%d\n", p + 3 - tab4);
717 printf("add=%d\n", 3 + p - tab4);
719 /* check if 64bit support is ok */
720 q = p = 0;
721 q += i;
722 printf("%p %p %ld\n", q, p, p-q);
723 printf("%d %d %d %d %d %d\n",
724 p == q, p != q, p < q, p <= q, p >= q, p > q);
725 i = 0xf0000000;
726 p += i;
727 printf("%p %p %ld\n", q, p, p-q);
728 printf("%d %d %d %d %d %d\n",
729 p == q, p != q, p < q, p <= q, p >= q, p > q);
730 p = (int *)((char *)p + 0xf0000000);
731 printf("%p %p %ld\n", q, p, p-q);
732 printf("%d %d %d %d %d %d\n",
733 p == q, p != q, p < q, p <= q, p >= q, p > q);
734 p += 0xf0000000;
735 printf("%p %p %ld\n", q, p, p-q);
736 printf("%d %d %d %d %d %d\n",
737 p == q, p != q, p < q, p <= q, p >= q, p > q);
739 struct size12 {
740 int i, j, k;
742 struct size12 s[2], *sp = s;
743 int i, j;
744 sp->i = 42;
745 sp++;
746 j = -1;
747 printf("%d\n", sp[j].i);
751 void expr_cmp_test()
753 int a, b;
754 printf("constant_expr:\n");
755 a = -1;
756 b = 1;
757 printf("%d\n", a == a);
758 printf("%d\n", a != a);
760 printf("%d\n", a < b);
761 printf("%d\n", a <= b);
762 printf("%d\n", a <= a);
763 printf("%d\n", b >= a);
764 printf("%d\n", a >= a);
765 printf("%d\n", b > a);
767 printf("%d\n", (unsigned)a < b);
768 printf("%d\n", (unsigned)a <= b);
769 printf("%d\n", (unsigned)a <= a);
770 printf("%d\n", (unsigned)b >= a);
771 printf("%d\n", (unsigned)a >= a);
772 printf("%d\n", (unsigned)b > a);
775 struct empty {
778 struct aligntest1 {
779 char a[10];
782 struct aligntest2 {
783 int a;
784 char b[10];
787 struct aligntest3 {
788 double a, b;
791 struct aligntest4 {
792 double a[0];
795 void struct_test()
797 struct1 *s;
798 union union2 u;
800 printf("struct:\n");
801 printf("sizes: %d %d %d %d\n",
802 sizeof(struct struct1),
803 sizeof(struct struct2),
804 sizeof(union union1),
805 sizeof(union union2));
806 st1.f1 = 1;
807 st1.f2 = 2;
808 st1.f3 = 3;
809 printf("st1: %d %d %d\n",
810 st1.f1, st1.f2, st1.f3);
811 st1.u.v1 = 1;
812 st1.u.v2 = 2;
813 printf("union1: %d\n", st1.u.v1);
814 u.w1 = 1;
815 u.w2 = 2;
816 printf("union2: %d\n", u.w1);
817 s = &st2;
818 s->f1 = 3;
819 s->f2 = 2;
820 s->f3 = 1;
821 printf("st2: %d %d %d\n",
822 s->f1, s->f2, s->f3);
823 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
825 /* align / size tests */
826 printf("aligntest1 sizeof=%d alignof=%d\n",
827 sizeof(struct aligntest1), __alignof__(struct aligntest1));
828 printf("aligntest2 sizeof=%d alignof=%d\n",
829 sizeof(struct aligntest2), __alignof__(struct aligntest2));
830 printf("aligntest3 sizeof=%d alignof=%d\n",
831 sizeof(struct aligntest3), __alignof__(struct aligntest3));
832 printf("aligntest4 sizeof=%d alignof=%d\n",
833 sizeof(struct aligntest4), __alignof__(struct aligntest4));
835 /* empty structures (GCC extension) */
836 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
837 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
840 /* XXX: depend on endianness */
841 void char_short_test()
843 int var1, var2;
845 printf("char_short:\n");
847 var1 = 0x01020304;
848 var2 = 0xfffefdfc;
849 printf("s8=%d %d\n",
850 *(char *)&var1, *(char *)&var2);
851 printf("u8=%d %d\n",
852 *(unsigned char *)&var1, *(unsigned char *)&var2);
853 printf("s16=%d %d\n",
854 *(short *)&var1, *(short *)&var2);
855 printf("u16=%d %d\n",
856 *(unsigned short *)&var1, *(unsigned short *)&var2);
857 printf("s32=%d %d\n",
858 *(int *)&var1, *(int *)&var2);
859 printf("u32=%d %d\n",
860 *(unsigned int *)&var1, *(unsigned int *)&var2);
861 *(char *)&var1 = 0x08;
862 printf("var1=%x\n", var1);
863 *(short *)&var1 = 0x0809;
864 printf("var1=%x\n", var1);
865 *(int *)&var1 = 0x08090a0b;
866 printf("var1=%x\n", var1);
869 /******************/
871 typedef struct Sym {
872 int v;
873 int t;
874 int c;
875 struct Sym *next;
876 struct Sym *prev;
877 } Sym;
879 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
880 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
882 static int toupper1(int a)
884 return TOUPPER(a);
887 void bool_test()
889 int *s, a, b, t, f, i;
891 a = 0;
892 s = (void*)0;
893 printf("!s=%d\n", !s);
895 if (!s || !s[0])
896 a = 1;
897 printf("a=%d\n", a);
899 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
900 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
901 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
902 #if 1 && 1
903 printf("a1\n");
904 #endif
905 #if 1 || 0
906 printf("a2\n");
907 #endif
908 #if 1 ? 0 : 1
909 printf("a3\n");
910 #endif
911 #if 0 ? 0 : 1
912 printf("a4\n");
913 #endif
915 a = 4;
916 printf("b=%d\n", a + (0 ? 1 : a / 2));
918 /* test register spilling */
919 a = 10;
920 b = 10;
921 a = (a + b) * ((a < b) ?
922 ((b - a) * (a - b)): a + b);
923 printf("a=%d\n", a);
925 /* test complex || or && expressions */
926 t = 1;
927 f = 0;
928 a = 32;
929 printf("exp=%d\n", f == (32 <= a && a <= 3));
930 printf("r=%d\n", (t || f) + (t && f));
932 /* test ? : cast */
934 int aspect_on;
935 int aspect_native = 65536;
936 double bfu_aspect = 1.0;
937 int aspect;
938 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
939 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
940 printf("aspect=%d\n", aspect);
944 /* test ? : GCC extension */
946 static int v1 = 34 ? : -1; /* constant case */
947 static int v2 = 0 ? : -1; /* constant case */
948 int a = 30;
950 printf("%d %d\n", v1, v2);
951 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
954 /* again complex expression */
955 for(i=0;i<256;i++) {
956 if (toupper1 (i) != TOUPPER (i))
957 printf("error %d\n", i);
961 /* GCC accepts that */
962 static int tab_reinit[];
963 static int tab_reinit[10];
965 //int cinit1; /* a global variable can be defined several times without error ! */
966 int cinit1;
967 int cinit1;
968 int cinit1 = 0;
969 int *cinit2 = (int []){3, 2, 1};
971 void compound_literal_test(void)
973 int *p, i;
974 char *q, *q3;
976 printf("compound_test:\n");
978 p = (int []){1, 2, 3};
979 for(i=0;i<3;i++)
980 printf(" %d", p[i]);
981 printf("\n");
983 for(i=0;i<3;i++)
984 printf("%d", cinit2[i]);
985 printf("\n");
987 q = "tralala1";
988 printf("q1=%s\n", q);
990 q = (char *){ "tralala2" };
991 printf("q2=%s\n", q);
993 q3 = (char *){ q };
994 printf("q3=%s\n", q3);
996 q = (char []){ "tralala3" };
997 printf("q4=%s\n", q);
999 #ifdef ALL_ISOC99
1000 p = (int []){1, 2, cinit1 + 3};
1001 for(i=0;i<3;i++)
1002 printf(" %d", p[i]);
1003 printf("\n");
1005 for(i=0;i<3;i++) {
1006 p = (int []){1, 2, 4 + i};
1007 printf("%d %d %d\n",
1008 p[0],
1009 p[1],
1010 p[2]);
1012 #endif
1015 /* K & R protos */
1017 kr_func1(a, b)
1019 return a + b;
1022 int kr_func2(a, b)
1024 return a + b;
1027 kr_test()
1029 printf("kr_test:\n");
1030 printf("func1=%d\n", kr_func1(3, 4));
1031 printf("func2=%d\n", kr_func2(3, 4));
1032 return 0;
1035 void num(int n)
1037 char *tab, *p;
1038 tab = (char*)malloc(20);
1039 p = tab;
1040 while (1) {
1041 *p = 48 + (n % 10);
1042 p++;
1043 n = n / 10;
1044 if (n == 0)
1045 break;
1047 while (p != tab) {
1048 p--;
1049 printf("%c", *p);
1051 printf("\n");
1054 /* structure assignment tests */
1055 struct structa1 {
1056 int f1;
1057 char f2;
1060 struct structa1 ssta1;
1062 void struct_assign_test1(struct structa1 s1, int t)
1064 printf("%d %d %d\n", s1.f1, s1.f2, t);
1067 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1069 s1.f1 += t;
1070 s1.f2 -= t;
1071 return s1;
1074 void struct_assign_test(void)
1076 struct structa1 lsta1, lsta2;
1078 #if 0
1079 printf("struct_assign_test:\n");
1081 lsta1.f1 = 1;
1082 lsta1.f2 = 2;
1083 printf("%d %d\n", lsta1.f1, lsta1.f2);
1084 lsta2 = lsta1;
1085 printf("%d %d\n", lsta2.f1, lsta2.f2);
1086 #else
1087 lsta2.f1 = 1;
1088 lsta2.f2 = 2;
1089 #endif
1090 struct_assign_test1(lsta2, 3);
1092 printf("before call: %d %d\n", lsta2.f1, lsta2.f2);
1093 lsta2 = struct_assign_test2(lsta2, 4);
1094 printf("after call: %d %d\n", lsta2.f1, lsta2.f2);
1097 /* casts to short/char */
1099 void cast1(char a, short b, unsigned char c, unsigned short d)
1101 printf("%d %d %d %d\n", a, b, c, d);
1104 char bcast;
1105 short scast;
1107 void cast_test()
1109 int a;
1110 char c;
1111 char tab[10];
1112 unsigned b,d;
1113 short s;
1114 char *p = NULL;
1115 p -= 0x700000000042;
1117 printf("cast_test:\n");
1118 a = 0xfffff;
1119 cast1(a, a, a, a);
1120 a = 0xffffe;
1121 printf("%d %d %d %d\n",
1122 (char)(a + 1),
1123 (short)(a + 1),
1124 (unsigned char)(a + 1),
1125 (unsigned short)(a + 1));
1126 printf("%d %d %d %d\n",
1127 (char)0xfffff,
1128 (short)0xfffff,
1129 (unsigned char)0xfffff,
1130 (unsigned short)0xfffff);
1132 a = (bcast = 128) + 1;
1133 printf("%d\n", a);
1134 a = (scast = 65536) + 1;
1135 printf("%d\n", a);
1137 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1139 /* test cast from unsigned to signed short to int */
1140 b = 0xf000;
1141 d = (short)b;
1142 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1143 b = 0xf0f0;
1144 d = (char)b;
1145 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1147 /* test implicit int casting for array accesses */
1148 c = 0;
1149 tab[1] = 2;
1150 tab[c] = 1;
1151 printf("%d %d\n", tab[0], tab[1]);
1153 /* test implicit casting on some operators */
1154 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1155 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1156 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1158 /* from pointer to integer types */
1159 printf("%d %d %ld %ld %lld %lld\n",
1160 (int)p, (unsigned int)p,
1161 (long)p, (unsigned long)p,
1162 (long long)p, (unsigned long long)p);
1164 /* from integers to pointers */
1165 printf("%p %p %p %p\n",
1166 (void *)a, (void *)b, (void *)c, (void *)d);
1169 /* initializers tests */
1170 struct structinit1 {
1171 int f1;
1172 char f2;
1173 short f3;
1174 int farray[3];
1177 int sinit1 = 2;
1178 int sinit2 = { 3 };
1179 int sinit3[3] = { 1, 2, {{3}}, };
1180 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1181 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1182 int sinit6[] = { 1, 2, 3 };
1183 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1184 char sinit8[] = "hello" "trala";
1186 struct structinit1 sinit9 = { 1, 2, 3 };
1187 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1188 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1189 #ifdef ALL_ISOC99
1190 .farray[0] = 10,
1191 .farray[1] = 11,
1192 .farray[2] = 12,
1193 #endif
1196 char *sinit12 = "hello world";
1197 char *sinit13[] = {
1198 "test1",
1199 "test2",
1200 "test3",
1202 char sinit14[10] = { "abc" };
1203 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1205 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1207 struct bar {
1208 char *s;
1209 int len;
1210 } sinit17[] = {
1211 "a1", 4,
1212 "a2", 1
1215 int sinit18[10] = {
1216 [2 ... 5] = 20,
1218 [8] = 10,
1221 void init_test(void)
1223 int linit1 = 2;
1224 int linit2 = { 3 };
1225 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1226 int linit6[] = { 1, 2, 3 };
1227 int i, j;
1228 char linit8[] = "hello" "trala";
1229 int linit12[10] = { 1, 2 };
1230 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1231 char linit14[10] = "abc";
1232 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1233 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1234 int linit17 = sizeof(linit17);
1236 printf("init_test:\n");
1238 printf("sinit1=%d\n", sinit1);
1239 printf("sinit2=%d\n", sinit2);
1240 printf("sinit3=%d %d %d %d\n",
1241 sizeof(sinit3),
1242 sinit3[0],
1243 sinit3[1],
1244 sinit3[2]
1246 printf("sinit6=%d\n", sizeof(sinit6));
1247 printf("sinit7=%d %d %d %d\n",
1248 sizeof(sinit7),
1249 sinit7[0],
1250 sinit7[1],
1251 sinit7[2]
1253 printf("sinit8=%s\n", sinit8);
1254 printf("sinit9=%d %d %d\n",
1255 sinit9.f1,
1256 sinit9.f2,
1257 sinit9.f3
1259 printf("sinit10=%d %d %d\n",
1260 sinit10.f1,
1261 sinit10.f2,
1262 sinit10.f3
1264 printf("sinit11=%d %d %d %d %d %d\n",
1265 sinit11.f1,
1266 sinit11.f2,
1267 sinit11.f3,
1268 sinit11.farray[0],
1269 sinit11.farray[1],
1270 sinit11.farray[2]
1273 for(i=0;i<3;i++)
1274 for(j=0;j<2;j++)
1275 printf("[%d][%d] = %d %d %d\n",
1276 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1277 printf("linit1=%d\n", linit1);
1278 printf("linit2=%d\n", linit2);
1279 printf("linit6=%d\n", sizeof(linit6));
1280 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1282 printf("sinit12=%s\n", sinit12);
1283 printf("sinit13=%d %s %s %s\n",
1284 sizeof(sinit13),
1285 sinit13[0],
1286 sinit13[1],
1287 sinit13[2]);
1288 printf("sinit14=%s\n", sinit14);
1290 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1291 printf("\n");
1292 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1293 printf("\n");
1294 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1295 printf("\n");
1296 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1297 printf("\n");
1298 printf("%d %d %d %d\n",
1299 linit16.a1,
1300 linit16.a2,
1301 linit16.a3,
1302 linit16.a4);
1303 /* test that initialisation is done after variable declare */
1304 printf("linit17=%d\n", linit17);
1305 printf("sinit15=%d\n", sinit15[0]);
1306 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1307 printf("sinit17=%s %d %s %d\n",
1308 sinit17[0].s, sinit17[0].len,
1309 sinit17[1].s, sinit17[1].len);
1310 for(i=0;i<10;i++)
1311 printf("%x ", sinit18[i]);
1312 printf("\n");
1316 void switch_test()
1318 int i;
1320 for(i=0;i<15;i++) {
1321 switch(i) {
1322 case 0:
1323 case 1:
1324 printf("a");
1325 break;
1326 default:
1327 printf("%d", i);
1328 break;
1329 case 8 ... 12:
1330 printf("c");
1331 break;
1332 case 3:
1333 printf("b");
1334 break;
1337 printf("\n");
1340 /* ISOC99 _Bool type */
1341 void c99_bool_test(void)
1343 #ifdef BOOL_ISOC99
1344 int a;
1345 _Bool b;
1347 printf("bool_test:\n");
1348 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1349 a = 3;
1350 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1351 b = 3;
1352 printf("b = %d\n", b);
1353 b++;
1354 printf("b = %d\n", b);
1355 #endif
1358 void bitfield_test(void)
1360 int a;
1361 struct sbf1 {
1362 int f1 : 3;
1363 int : 2;
1364 int f2 : 1;
1365 int : 0;
1366 int f3 : 5;
1367 int f4 : 7;
1368 unsigned int f5 : 7;
1369 } st1;
1370 printf("bitfield_test:");
1371 printf("sizeof(st1) = %d\n", sizeof(st1));
1373 st1.f1 = 3;
1374 st1.f2 = 1;
1375 st1.f3 = 15;
1376 a = 120;
1377 st1.f4 = a;
1378 st1.f5 = a;
1379 st1.f5++;
1380 printf("%d %d %d %d %d\n",
1381 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1383 st1.f1 = 7;
1384 if (st1.f1 == -1)
1385 printf("st1.f1 == -1\n");
1386 else
1387 printf("st1.f1 != -1\n");
1388 if (st1.f2 == -1)
1389 printf("st1.f2 == -1\n");
1390 else
1391 printf("st1.f2 != -1\n");
1393 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1394 long-long bitfields whose size is not bigger than int */
1395 struct sbf2 {
1396 long long f1 : 45;
1397 long long : 2;
1398 long long f2 : 35;
1399 unsigned long long f3 : 38;
1400 } st2;
1401 st2.f1 = 0x123456789ULL;
1402 a = 120;
1403 st2.f2 = (long long)a << 25;
1404 st2.f3 = a;
1405 st2.f2++;
1406 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
1409 #ifdef __x86_64__
1410 #define FLOAT_FMT "%f\n"
1411 #else
1412 /* x86's float isn't compatible with GCC */
1413 #define FLOAT_FMT "%.5f\n"
1414 #endif
1416 /* declare strto* functions as they are C99 */
1417 double strtod(const char *nptr, char **endptr);
1418 float strtof(const char *nptr, char **endptr);
1419 long double strtold(const char *nptr, char **endptr);
1421 #define FTEST(prefix, type, fmt)\
1422 void prefix ## cmp(type a, type b)\
1424 printf("%d %d %d %d %d %d\n",\
1425 a == b,\
1426 a != b,\
1427 a < b,\
1428 a > b,\
1429 a >= b,\
1430 a <= b);\
1431 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1434 a + b,\
1435 a - b,\
1436 a * b,\
1437 a / b,\
1438 -a);\
1439 printf(fmt "\n", ++a);\
1440 printf(fmt "\n", a++);\
1441 printf(fmt "\n", a);\
1442 b = 0;\
1443 printf("%d %d\n", !a, !b);\
1445 void prefix ## fcast(type a)\
1447 float fa;\
1448 double da;\
1449 long double la;\
1450 int ia;\
1451 unsigned int ua;\
1452 type b;\
1453 fa = a;\
1454 da = a;\
1455 la = a;\
1456 printf("ftof: %f %f %Lf\n", fa, da, la);\
1457 ia = (int)a;\
1458 ua = (unsigned int)a;\
1459 printf("ftoi: %d %u\n", ia, ua);\
1460 ia = -1234;\
1461 ua = 0x81234500;\
1462 b = ia;\
1463 printf("itof: " fmt "\n", b);\
1464 b = ua;\
1465 printf("utof: " fmt "\n", b);\
1468 float prefix ## retf(type a) { return a; }\
1469 double prefix ## retd(type a) { return a; }\
1470 long double prefix ## retld(type a) { return a; }\
1472 void prefix ## call(void)\
1474 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1475 printf("double: %f\n", prefix ## retd(42.123456789));\
1476 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1477 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
1480 void prefix ## test(void)\
1482 printf("testing '%s'\n", #type);\
1483 prefix ## cmp(1, 2.5);\
1484 prefix ## cmp(2, 1.5);\
1485 prefix ## cmp(1, 1);\
1486 prefix ## fcast(234.6);\
1487 prefix ## fcast(-2334.6);\
1488 prefix ## call();\
1491 FTEST(f, float, "%f")
1492 FTEST(d, double, "%f")
1493 FTEST(ld, long double, "%Lf")
1495 double ftab1[3] = { 1.2, 3.4, -5.6 };
1498 void float_test(void)
1500 float fa, fb;
1501 double da, db;
1502 int a;
1503 unsigned int b;
1505 printf("float_test:\n");
1506 printf("sizeof(float) = %d\n", sizeof(float));
1507 printf("sizeof(double) = %d\n", sizeof(double));
1508 printf("sizeof(long double) = %d\n", sizeof(long double));
1509 ftest();
1510 dtest();
1511 ldtest();
1512 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1513 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1514 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1515 da = 123;
1516 printf("da=%f\n", da);
1517 fa = 123;
1518 printf("fa=%f\n", fa);
1519 a = 4000000000;
1520 da = a;
1521 printf("da = %f\n", da);
1522 b = 4000000000;
1523 db = b;
1524 printf("db = %f\n", db);
1527 int fib(int n)
1529 if (n <= 2)
1530 return 1;
1531 else
1532 return fib(n-1) + fib(n-2);
1535 void funcptr_test()
1537 void (*func)(int);
1538 int a;
1539 struct {
1540 int dummy;
1541 void (*func)(int);
1542 } st1;
1544 printf("funcptr:\n");
1545 func = &num;
1546 (*func)(12345);
1547 func = num;
1548 a = 1;
1549 a = 1;
1550 func(12345);
1551 /* more complicated pointer computation */
1552 st1.func = num;
1553 st1.func(12346);
1554 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1555 printf("sizeof2 = %d\n", sizeof funcptr_test);
1556 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1557 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1560 void lloptest(long long a, long long b)
1562 unsigned long long ua, ub;
1564 ua = a;
1565 ub = b;
1566 /* arith */
1567 printf("arith: %Ld %Ld %Ld\n",
1568 a + b,
1569 a - b,
1570 a * b);
1572 if (b != 0) {
1573 printf("arith1: %Ld %Ld\n",
1574 a / b,
1575 a % b);
1578 /* binary */
1579 printf("bin: %Ld %Ld %Ld\n",
1580 a & b,
1581 a | b,
1582 a ^ b);
1584 /* tests */
1585 printf("test: %d %d %d %d %d %d\n",
1586 a == b,
1587 a != b,
1588 a < b,
1589 a > b,
1590 a >= b,
1591 a <= b);
1593 printf("utest: %d %d %d %d %d %d\n",
1594 ua == ub,
1595 ua != ub,
1596 ua < ub,
1597 ua > ub,
1598 ua >= ub,
1599 ua <= ub);
1601 /* arith2 */
1602 a++;
1603 b++;
1604 printf("arith2: %Ld %Ld\n", a, b);
1605 printf("arith2: %Ld %Ld\n", a++, b++);
1606 printf("arith2: %Ld %Ld\n", --a, --b);
1607 printf("arith2: %Ld %Ld\n", a, b);
1608 b = ub = 0;
1609 printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
1612 void llshift(long long a, int b)
1614 printf("shift: %Ld %Ld %Ld\n",
1615 (unsigned long long)a >> b,
1616 a >> b,
1617 a << b);
1618 printf("shiftc: %Ld %Ld %Ld\n",
1619 (unsigned long long)a >> 3,
1620 a >> 3,
1621 a << 3);
1622 printf("shiftc: %Ld %Ld %Ld\n",
1623 (unsigned long long)a >> 35,
1624 a >> 35,
1625 a << 35);
1628 void llfloat(void)
1630 float fa;
1631 double da;
1632 long double lda;
1633 long long la, lb, lc;
1634 unsigned long long ula, ulb, ulc;
1635 la = 0x12345678;
1636 ula = 0x72345678;
1637 la = (la << 20) | 0x12345;
1638 ula = ula << 33;
1639 printf("la=%Ld ula=%Lu\n", la, ula);
1641 fa = la;
1642 da = la;
1643 lda = la;
1644 printf("lltof: %f %f %Lf\n", fa, da, lda);
1646 la = fa;
1647 lb = da;
1648 lc = lda;
1649 printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
1651 fa = ula;
1652 da = ula;
1653 lda = ula;
1654 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1656 ula = fa;
1657 ulb = da;
1658 ulc = lda;
1659 printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
1662 long long llfunc1(int a)
1664 return a * 2;
1667 struct S {
1668 int id;
1669 char item;
1672 long long int value(struct S *v)
1674 return ((long long int)v->item);
1677 void longlong_test(void)
1679 long long a, b, c;
1680 int ia;
1681 unsigned int ua;
1682 printf("longlong_test:\n");
1683 printf("sizeof(long long) = %d\n", sizeof(long long));
1684 ia = -1;
1685 ua = -2;
1686 a = ia;
1687 b = ua;
1688 printf("%Ld %Ld\n", a, b);
1689 printf("%Ld %Ld %Ld %Lx\n",
1690 (long long)1,
1691 (long long)-2,
1692 1LL,
1693 0x1234567812345679);
1694 a = llfunc1(-3);
1695 printf("%Ld\n", a);
1697 lloptest(1000, 23);
1698 lloptest(0xff, 0x1234);
1699 b = 0x72345678 << 10;
1700 lloptest(-3, b);
1701 llshift(0x123, 5);
1702 llshift(-23, 5);
1703 b = 0x72345678LL << 10;
1704 llshift(b, 47);
1706 llfloat();
1707 #if 1
1708 b = 0x12345678;
1709 a = -1;
1710 c = a + b;
1711 printf("%Lx\n", c);
1712 #endif
1714 /* long long reg spill test */
1716 struct S a;
1718 a.item = 3;
1719 printf("%lld\n", value(&a));
1721 lloptest(0x80000000, 0);
1723 /* another long long spill test */
1725 long long *p, v;
1726 v = 1;
1727 p = &v;
1728 p[0]++;
1729 printf("%lld\n", *p);
1732 a = 68719476720LL;
1733 b = 4294967295LL;
1734 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
1736 printf("%Ld\n", 0x123456789LLU);
1739 void manyarg_test(void)
1741 long double ld = 1234567891234LL;
1742 printf("manyarg_test:\n");
1743 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
1744 1, 2, 3, 4, 5, 6, 7, 8,
1745 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
1746 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1747 "%Ld %Ld %f %f\n",
1748 1, 2, 3, 4, 5, 6, 7, 8,
1749 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1750 1234567891234LL, 987654321986LL,
1751 42.0, 43.0);
1752 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1753 "%Ld %Ld %f %f\n",
1754 ld, 1, 2, 3, 4, 5, 6, 7, 8,
1755 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1756 1234567891234LL, 987654321986LL,
1757 42.0, 43.0);
1758 /* XXX: known bug of x86-64 */
1759 #ifndef __x86_64__
1760 printf("%d %d %d %d %d %d %d %d %Lf\n",
1761 1, 2, 3, 4, 5, 6, 7, 8, ld);
1762 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1763 "%Ld %Ld %f %f %Lf\n",
1764 1, 2, 3, 4, 5, 6, 7, 8,
1765 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1766 1234567891234LL, 987654321986LL,
1767 42.0, 43.0, ld);
1768 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1769 "%Lf %Ld %Ld %f %f %Lf\n",
1770 1, 2, 3, 4, 5, 6, 7, 8,
1771 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1772 ld, 1234567891234LL, 987654321986LL,
1773 42.0, 43.0, ld);
1774 #endif
1777 void vprintf1(const char *fmt, ...)
1779 va_list ap;
1780 const char *p;
1781 int c, i;
1782 double d;
1783 long long ll;
1784 long double ld;
1786 va_start(ap, fmt);
1788 p = fmt;
1789 for(;;) {
1790 c = *p;
1791 if (c == '\0')
1792 break;
1793 p++;
1794 if (c == '%') {
1795 c = *p;
1796 switch(c) {
1797 case '\0':
1798 goto the_end;
1799 case 'd':
1800 i = va_arg(ap, int);
1801 printf("%d", i);
1802 break;
1803 case 'f':
1804 d = va_arg(ap, double);
1805 printf("%f", d);
1806 break;
1807 case 'l':
1808 ll = va_arg(ap, long long);
1809 printf("%Ld", ll);
1810 break;
1811 case 'F':
1812 ld = va_arg(ap, long double);
1813 printf("%Lf", ld);
1814 break;
1816 p++;
1817 } else {
1818 putchar(c);
1821 the_end:
1822 va_end(ap);
1826 void stdarg_test(void)
1828 long double ld = 1234567891234LL;
1829 vprintf1("%d %d %d\n", 1, 2, 3);
1830 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1831 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1832 vprintf1("%F %F %F\n", 1.2L, 2.3L, 3.4L);
1833 #ifdef __x86_64__
1834 /* a bug of x86's TCC */
1835 vprintf1("%d %f %l %F %d %f %l %F\n",
1836 1, 1.2, 3L, 4.5L, 6, 7.8, 9L, 0.1L);
1837 #endif
1838 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
1839 1, 2, 3, 4, 5, 6, 7, 8,
1840 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
1841 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
1842 1, 2, 3, 4, 5, 6, 7, 8,
1843 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
1844 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1845 "%l %l %f %f\n",
1846 1, 2, 3, 4, 5, 6, 7, 8,
1847 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1848 1234567891234LL, 987654321986LL,
1849 42.0, 43.0);
1850 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1851 "%l %l %f %f\n",
1852 ld, 1, 2, 3, 4, 5, 6, 7, 8,
1853 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1854 1234567891234LL, 987654321986LL,
1855 42.0, 43.0);
1856 vprintf1("%d %d %d %d %d %d %d %d %F\n",
1857 1, 2, 3, 4, 5, 6, 7, 8, ld);
1858 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1859 "%l %l %f %f %F\n",
1860 1, 2, 3, 4, 5, 6, 7, 8,
1861 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1862 1234567891234LL, 987654321986LL,
1863 42.0, 43.0, ld);
1864 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1865 "%F %l %l %f %f %F\n",
1866 1, 2, 3, 4, 5, 6, 7, 8,
1867 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1868 ld, 1234567891234LL, 987654321986LL,
1869 42.0, 43.0, ld);
1872 void whitespace_test(void)
1874 char *str;
1876 \f\v #if 1
1877 pri\
1878 ntf("whitspace:\n");\f\v
1879 #endif
1880 pf("N=%d\n", 2);
1882 #ifdef CORRECT_CR_HANDLING
1883 pri\
1884 ntf("aaa=%d\n", 3);
1885 #endif
1887 pri\
1889 ntf("min=%d\n", 4);
1891 #ifdef ACCEPT_CR_IN_STRINGS
1892 printf("len1=%d\n", strlen("
1893 "));
1894 #ifdef CORRECT_CR_HANDLING
1895 str = "
1897 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
1898 #endif
1899 printf("len1=%d\n", strlen(" a
1900 "));
1901 #endif /* ACCEPT_CR_IN_STRINGS */
1904 int reltab[3] = { 1, 2, 3 };
1906 int *rel1 = &reltab[1];
1907 int *rel2 = &reltab[2];
1909 void relocation_test(void)
1911 printf("*rel1=%d\n", *rel1);
1912 printf("*rel2=%d\n", *rel2);
1915 void old_style_f(a,b,c)
1916 int a, b;
1917 double c;
1919 printf("a=%d b=%d b=%f\n", a, b, c);
1922 void decl_func1(int cmpfn())
1924 printf("cmpfn=%lx\n", (long)cmpfn);
1927 void decl_func2(cmpfn)
1928 int cmpfn();
1930 printf("cmpfn=%lx\n", (long)cmpfn);
1933 void old_style_function(void)
1935 old_style_f((void *)1, 2, 3.0);
1936 decl_func1(NULL);
1937 decl_func2(NULL);
1940 void alloca_test()
1942 #if defined __i386__ || defined __x86_64__
1943 char *p = alloca(16);
1944 strcpy(p,"123456789012345");
1945 printf("alloca: p is %s\n", p);
1946 char *demo = "This is only a test.\n";
1947 /* Test alloca embedded in a larger expression */
1948 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
1949 #endif
1952 void sizeof_test(void)
1954 int a;
1955 int **ptr;
1957 printf("sizeof(int) = %d\n", sizeof(int));
1958 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1959 printf("sizeof(long) = %d\n", sizeof(long));
1960 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
1961 printf("sizeof(short) = %d\n", sizeof(short));
1962 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1963 printf("sizeof(char) = %d\n", sizeof(char));
1964 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1965 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1966 a = 1;
1967 printf("sizeof(a++) = %d\n", sizeof a++);
1968 printf("a=%d\n", a);
1969 ptr = NULL;
1970 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
1972 /* some alignof tests */
1973 printf("__alignof__(int) = %d\n", __alignof__(int));
1974 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1975 printf("__alignof__(short) = %d\n", __alignof__(short));
1976 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1977 printf("__alignof__(char) = %d\n", __alignof__(char));
1978 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1979 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
1982 void typeof_test(void)
1984 double a;
1985 typeof(a) b;
1986 typeof(float) c;
1988 a = 1.5;
1989 b = 2.5;
1990 c = 3.5;
1991 printf("a=%f b=%f c=%f\n", a, b, c);
1994 void statement_expr_test(void)
1996 int a, i;
1998 a = 0;
1999 for(i=0;i<10;i++) {
2000 a += 1 +
2001 ( { int b, j;
2002 b = 0;
2003 for(j=0;j<5;j++)
2004 b += j; b;
2005 } );
2007 printf("a=%d\n", a);
2011 void local_label_test(void)
2013 int a;
2014 goto l1;
2016 a = 1 + ({
2017 __label__ l1, l2, l3, l4;
2018 goto l1;
2020 printf("aa1\n");
2021 goto l3;
2023 printf("aa3\n");
2024 goto l4;
2026 printf("aa2\n");
2027 goto l2;
2028 l3:;
2031 printf("a=%d\n", a);
2032 return;
2034 printf("bb1\n");
2035 goto l2;
2037 printf("bb2\n");
2038 goto l4;
2041 /* inline assembler test */
2042 #ifdef __i386__
2044 /* from linux kernel */
2045 static char * strncat1(char * dest,const char * src,size_t count)
2047 int d0, d1, d2, d3;
2048 __asm__ __volatile__(
2049 "repne\n\t"
2050 "scasb\n\t"
2051 "decl %1\n\t"
2052 "movl %8,%3\n"
2053 "1:\tdecl %3\n\t"
2054 "js 2f\n\t"
2055 "lodsb\n\t"
2056 "stosb\n\t"
2057 "testb %%al,%%al\n\t"
2058 "jne 1b\n"
2059 "2:\txorl %2,%2\n\t"
2060 "stosb"
2061 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2062 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2063 : "memory");
2064 return dest;
2067 static inline void * memcpy1(void * to, const void * from, size_t n)
2069 int d0, d1, d2;
2070 __asm__ __volatile__(
2071 "rep ; movsl\n\t"
2072 "testb $2,%b4\n\t"
2073 "je 1f\n\t"
2074 "movsw\n"
2075 "1:\ttestb $1,%b4\n\t"
2076 "je 2f\n\t"
2077 "movsb\n"
2078 "2:"
2079 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2080 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2081 : "memory");
2082 return (to);
2085 static __inline__ void sigaddset1(unsigned int *set, int _sig)
2087 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2090 static __inline__ void sigdelset1(unsigned int *set, int _sig)
2092 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2095 static __inline__ __const__ unsigned int swab32(unsigned int x)
2097 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2098 "rorl $16,%0\n\t" /* swap words */
2099 "xchgb %b0,%h0" /* swap higher bytes */
2100 :"=q" (x)
2101 : "0" (x));
2102 return x;
2105 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
2107 unsigned long long res;
2108 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
2109 return res;
2112 static __inline__ unsigned long long inc64(unsigned long long a)
2114 unsigned long long res;
2115 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
2116 return res;
2119 unsigned int set;
2121 void asm_test(void)
2123 char buf[128];
2124 unsigned int val;
2126 printf("inline asm:\n");
2127 /* test the no operand case */
2128 asm volatile ("xorl %eax, %eax");
2130 memcpy1(buf, "hello", 6);
2131 strncat1(buf, " worldXXXXX", 3);
2132 printf("%s\n", buf);
2134 /* 'A' constraint test */
2135 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2136 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2138 set = 0xff;
2139 sigdelset1(&set, 2);
2140 sigaddset1(&set, 16);
2141 /* NOTE: we test here if C labels are correctly restored after the
2142 asm statement */
2143 goto label1;
2144 label2:
2145 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
2146 #ifdef __GNUC__ // works strange with GCC 4.3
2147 set=0x1080fd;
2148 #endif
2149 printf("set=0x%x\n", set);
2150 val = 0x01020304;
2151 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
2152 return;
2153 label1:
2154 goto label2;
2157 #else
2159 void asm_test(void)
2163 #endif
2165 #define COMPAT_TYPE(type1, type2) \
2167 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2168 __builtin_types_compatible_p (type1, type2));\
2171 int constant_p_var;
2173 void builtin_test(void)
2175 #if GCC_MAJOR >= 3
2176 COMPAT_TYPE(int, int);
2177 COMPAT_TYPE(int, unsigned int);
2178 COMPAT_TYPE(int, char);
2179 COMPAT_TYPE(int, const int);
2180 COMPAT_TYPE(int, volatile int);
2181 COMPAT_TYPE(int *, int *);
2182 COMPAT_TYPE(int *, void *);
2183 COMPAT_TYPE(int *, const int *);
2184 COMPAT_TYPE(char *, unsigned char *);
2185 /* space is needed because tcc preprocessor introduces a space between each token */
2186 COMPAT_TYPE(char * *, void *);
2187 #endif
2188 printf("res = %d\n", __builtin_constant_p(1));
2189 printf("res = %d\n", __builtin_constant_p(1 + 2));
2190 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
2191 printf("res = %d\n", __builtin_constant_p(constant_p_var));
2195 void const_func(const int a)
2199 void const_warn_test(void)
2201 const_func(1);