tccpp: signal missing #endif error
[tinycc/kirr.git] / tests / tcctest.c
blob536f5aa9f32988e1597585613918daa96066bc31
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");
595 printf("sizeof(size_t)=%d\n", sizeof(size_t));
596 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
599 void expr_test()
601 int a, b;
602 a = 0;
603 printf("%d\n", a += 1);
604 printf("%d\n", a -= 2);
605 printf("%d\n", a *= 31232132);
606 printf("%d\n", a /= 4);
607 printf("%d\n", a %= 20);
608 printf("%d\n", a &= 6);
609 printf("%d\n", a ^= 7);
610 printf("%d\n", a |= 8);
611 printf("%d\n", a >>= 3);
612 printf("%d\n", a <<= 4);
614 a = 22321;
615 b = -22321;
616 printf("%d\n", a + 1);
617 printf("%d\n", a - 2);
618 printf("%d\n", a * 312);
619 printf("%d\n", a / 4);
620 printf("%d\n", b / 4);
621 printf("%d\n", (unsigned)b / 4);
622 printf("%d\n", a % 20);
623 printf("%d\n", b % 20);
624 printf("%d\n", (unsigned)b % 20);
625 printf("%d\n", a & 6);
626 printf("%d\n", a ^ 7);
627 printf("%d\n", a | 8);
628 printf("%d\n", a >> 3);
629 printf("%d\n", b >> 3);
630 printf("%d\n", (unsigned)b >> 3);
631 printf("%d\n", a << 4);
632 printf("%d\n", ~a);
633 printf("%d\n", -a);
634 printf("%d\n", +a);
636 printf("%d\n", 12 + 1);
637 printf("%d\n", 12 - 2);
638 printf("%d\n", 12 * 312);
639 printf("%d\n", 12 / 4);
640 printf("%d\n", 12 % 20);
641 printf("%d\n", 12 & 6);
642 printf("%d\n", 12 ^ 7);
643 printf("%d\n", 12 | 8);
644 printf("%d\n", 12 >> 2);
645 printf("%d\n", 12 << 4);
646 printf("%d\n", ~12);
647 printf("%d\n", -12);
648 printf("%d\n", +12);
649 printf("%d %d %d %d\n",
650 isid('a'),
651 isid('g'),
652 isid('T'),
653 isid('('));
656 int isid(int c)
658 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
661 /**********************/
663 int vstack[10], *vstack_ptr;
665 void vpush(int vt, int vc)
667 *vstack_ptr++ = vt;
668 *vstack_ptr++ = vc;
671 void vpop(int *ft, int *fc)
673 *fc = *--vstack_ptr;
674 *ft = *--vstack_ptr;
677 void expr2_test()
679 int a, b;
681 printf("expr2:\n");
682 vstack_ptr = vstack;
683 vpush(1432432, 2);
684 vstack_ptr[-2] &= ~0xffffff80;
685 vpop(&a, &b);
686 printf("res= %d %d\n", a, b);
689 void constant_expr_test()
691 int a;
692 printf("constant_expr:\n");
693 a = 3;
694 printf("%d\n", a * 16);
695 printf("%d\n", a * 1);
696 printf("%d\n", a + 0);
699 int tab4[10];
701 void expr_ptr_test()
703 int *p, *q;
704 int i = -1;
706 printf("expr_ptr:\n");
707 p = tab4;
708 q = tab4 + 10;
709 printf("diff=%d\n", q - p);
710 p++;
711 printf("inc=%d\n", p - tab4);
712 p--;
713 printf("dec=%d\n", p - tab4);
714 ++p;
715 printf("inc=%d\n", p - tab4);
716 --p;
717 printf("dec=%d\n", p - tab4);
718 printf("add=%d\n", p + 3 - tab4);
719 printf("add=%d\n", 3 + p - tab4);
721 /* check if 64bit support is ok */
722 q = p = 0;
723 q += i;
724 printf("%p %p %ld\n", q, p, p-q);
725 printf("%d %d %d %d %d %d\n",
726 p == q, p != q, p < q, p <= q, p >= q, p > q);
727 i = 0xf0000000;
728 p += i;
729 printf("%p %p %ld\n", q, p, p-q);
730 printf("%d %d %d %d %d %d\n",
731 p == q, p != q, p < q, p <= q, p >= q, p > q);
732 p = (int *)((char *)p + 0xf0000000);
733 printf("%p %p %ld\n", q, p, p-q);
734 printf("%d %d %d %d %d %d\n",
735 p == q, p != q, p < q, p <= q, p >= q, p > q);
736 p += 0xf0000000;
737 printf("%p %p %ld\n", q, p, p-q);
738 printf("%d %d %d %d %d %d\n",
739 p == q, p != q, p < q, p <= q, p >= q, p > q);
741 struct size12 {
742 int i, j, k;
744 struct size12 s[2], *sp = s;
745 int i, j;
746 sp->i = 42;
747 sp++;
748 j = -1;
749 printf("%d\n", sp[j].i);
753 void expr_cmp_test()
755 int a, b;
756 printf("constant_expr:\n");
757 a = -1;
758 b = 1;
759 printf("%d\n", a == a);
760 printf("%d\n", a != a);
762 printf("%d\n", a < b);
763 printf("%d\n", a <= b);
764 printf("%d\n", a <= a);
765 printf("%d\n", b >= a);
766 printf("%d\n", a >= a);
767 printf("%d\n", b > a);
769 printf("%d\n", (unsigned)a < b);
770 printf("%d\n", (unsigned)a <= b);
771 printf("%d\n", (unsigned)a <= a);
772 printf("%d\n", (unsigned)b >= a);
773 printf("%d\n", (unsigned)a >= a);
774 printf("%d\n", (unsigned)b > a);
777 struct empty {
780 struct aligntest1 {
781 char a[10];
784 struct aligntest2 {
785 int a;
786 char b[10];
789 struct aligntest3 {
790 double a, b;
793 struct aligntest4 {
794 double a[0];
797 void struct_test()
799 struct1 *s;
800 union union2 u;
802 printf("struct:\n");
803 printf("sizes: %d %d %d %d\n",
804 sizeof(struct struct1),
805 sizeof(struct struct2),
806 sizeof(union union1),
807 sizeof(union union2));
808 st1.f1 = 1;
809 st1.f2 = 2;
810 st1.f3 = 3;
811 printf("st1: %d %d %d\n",
812 st1.f1, st1.f2, st1.f3);
813 st1.u.v1 = 1;
814 st1.u.v2 = 2;
815 printf("union1: %d\n", st1.u.v1);
816 u.w1 = 1;
817 u.w2 = 2;
818 printf("union2: %d\n", u.w1);
819 s = &st2;
820 s->f1 = 3;
821 s->f2 = 2;
822 s->f3 = 1;
823 printf("st2: %d %d %d\n",
824 s->f1, s->f2, s->f3);
825 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
827 /* align / size tests */
828 printf("aligntest1 sizeof=%d alignof=%d\n",
829 sizeof(struct aligntest1), __alignof__(struct aligntest1));
830 printf("aligntest2 sizeof=%d alignof=%d\n",
831 sizeof(struct aligntest2), __alignof__(struct aligntest2));
832 printf("aligntest3 sizeof=%d alignof=%d\n",
833 sizeof(struct aligntest3), __alignof__(struct aligntest3));
834 printf("aligntest4 sizeof=%d alignof=%d\n",
835 sizeof(struct aligntest4), __alignof__(struct aligntest4));
837 /* empty structures (GCC extension) */
838 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
839 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
842 /* XXX: depend on endianness */
843 void char_short_test()
845 int var1, var2;
847 printf("char_short:\n");
849 var1 = 0x01020304;
850 var2 = 0xfffefdfc;
851 printf("s8=%d %d\n",
852 *(char *)&var1, *(char *)&var2);
853 printf("u8=%d %d\n",
854 *(unsigned char *)&var1, *(unsigned char *)&var2);
855 printf("s16=%d %d\n",
856 *(short *)&var1, *(short *)&var2);
857 printf("u16=%d %d\n",
858 *(unsigned short *)&var1, *(unsigned short *)&var2);
859 printf("s32=%d %d\n",
860 *(int *)&var1, *(int *)&var2);
861 printf("u32=%d %d\n",
862 *(unsigned int *)&var1, *(unsigned int *)&var2);
863 *(char *)&var1 = 0x08;
864 printf("var1=%x\n", var1);
865 *(short *)&var1 = 0x0809;
866 printf("var1=%x\n", var1);
867 *(int *)&var1 = 0x08090a0b;
868 printf("var1=%x\n", var1);
871 /******************/
873 typedef struct Sym {
874 int v;
875 int t;
876 int c;
877 struct Sym *next;
878 struct Sym *prev;
879 } Sym;
881 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
882 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
884 static int toupper1(int a)
886 return TOUPPER(a);
889 void bool_test()
891 int *s, a, b, t, f, i;
893 a = 0;
894 s = (void*)0;
895 printf("!s=%d\n", !s);
897 if (!s || !s[0])
898 a = 1;
899 printf("a=%d\n", a);
901 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
902 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
903 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
904 #if 1 && 1
905 printf("a1\n");
906 #endif
907 #if 1 || 0
908 printf("a2\n");
909 #endif
910 #if 1 ? 0 : 1
911 printf("a3\n");
912 #endif
913 #if 0 ? 0 : 1
914 printf("a4\n");
915 #endif
917 a = 4;
918 printf("b=%d\n", a + (0 ? 1 : a / 2));
920 /* test register spilling */
921 a = 10;
922 b = 10;
923 a = (a + b) * ((a < b) ?
924 ((b - a) * (a - b)): a + b);
925 printf("a=%d\n", a);
927 /* test complex || or && expressions */
928 t = 1;
929 f = 0;
930 a = 32;
931 printf("exp=%d\n", f == (32 <= a && a <= 3));
932 printf("r=%d\n", (t || f) + (t && f));
934 /* test ? : cast */
936 int aspect_on;
937 int aspect_native = 65536;
938 double bfu_aspect = 1.0;
939 int aspect;
940 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
941 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
942 printf("aspect=%d\n", aspect);
946 /* test ? : GCC extension */
948 static int v1 = 34 ? : -1; /* constant case */
949 static int v2 = 0 ? : -1; /* constant case */
950 int a = 30;
952 printf("%d %d\n", v1, v2);
953 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
956 /* again complex expression */
957 for(i=0;i<256;i++) {
958 if (toupper1 (i) != TOUPPER (i))
959 printf("error %d\n", i);
963 /* GCC accepts that */
964 static int tab_reinit[];
965 static int tab_reinit[10];
967 //int cinit1; /* a global variable can be defined several times without error ! */
968 int cinit1;
969 int cinit1;
970 int cinit1 = 0;
971 int *cinit2 = (int []){3, 2, 1};
973 void compound_literal_test(void)
975 int *p, i;
976 char *q, *q3;
978 printf("compound_test:\n");
980 p = (int []){1, 2, 3};
981 for(i=0;i<3;i++)
982 printf(" %d", p[i]);
983 printf("\n");
985 for(i=0;i<3;i++)
986 printf("%d", cinit2[i]);
987 printf("\n");
989 q = "tralala1";
990 printf("q1=%s\n", q);
992 q = (char *){ "tralala2" };
993 printf("q2=%s\n", q);
995 q3 = (char *){ q };
996 printf("q3=%s\n", q3);
998 q = (char []){ "tralala3" };
999 printf("q4=%s\n", q);
1001 #ifdef ALL_ISOC99
1002 p = (int []){1, 2, cinit1 + 3};
1003 for(i=0;i<3;i++)
1004 printf(" %d", p[i]);
1005 printf("\n");
1007 for(i=0;i<3;i++) {
1008 p = (int []){1, 2, 4 + i};
1009 printf("%d %d %d\n",
1010 p[0],
1011 p[1],
1012 p[2]);
1014 #endif
1017 /* K & R protos */
1019 kr_func1(a, b)
1021 return a + b;
1024 int kr_func2(a, b)
1026 return a + b;
1029 kr_test()
1031 printf("kr_test:\n");
1032 printf("func1=%d\n", kr_func1(3, 4));
1033 printf("func2=%d\n", kr_func2(3, 4));
1034 return 0;
1037 void num(int n)
1039 char *tab, *p;
1040 tab = (char*)malloc(20);
1041 p = tab;
1042 while (1) {
1043 *p = 48 + (n % 10);
1044 p++;
1045 n = n / 10;
1046 if (n == 0)
1047 break;
1049 while (p != tab) {
1050 p--;
1051 printf("%c", *p);
1053 printf("\n");
1056 /* structure assignment tests */
1057 struct structa1 {
1058 int f1;
1059 char f2;
1062 struct structa1 ssta1;
1064 void struct_assign_test1(struct structa1 s1, int t)
1066 printf("%d %d %d\n", s1.f1, s1.f2, t);
1069 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1071 s1.f1 += t;
1072 s1.f2 -= t;
1073 return s1;
1076 void struct_assign_test(void)
1078 struct structa1 lsta1, lsta2;
1080 #if 0
1081 printf("struct_assign_test:\n");
1083 lsta1.f1 = 1;
1084 lsta1.f2 = 2;
1085 printf("%d %d\n", lsta1.f1, lsta1.f2);
1086 lsta2 = lsta1;
1087 printf("%d %d\n", lsta2.f1, lsta2.f2);
1088 #else
1089 lsta2.f1 = 1;
1090 lsta2.f2 = 2;
1091 #endif
1092 struct_assign_test1(lsta2, 3);
1094 printf("before call: %d %d\n", lsta2.f1, lsta2.f2);
1095 lsta2 = struct_assign_test2(lsta2, 4);
1096 printf("after call: %d %d\n", lsta2.f1, lsta2.f2);
1099 /* casts to short/char */
1101 void cast1(char a, short b, unsigned char c, unsigned short d)
1103 printf("%d %d %d %d\n", a, b, c, d);
1106 char bcast;
1107 short scast;
1109 void cast_test()
1111 int a;
1112 char c;
1113 char tab[10];
1114 unsigned b,d;
1115 short s;
1116 char *p = NULL;
1117 p -= 0x700000000042;
1119 printf("cast_test:\n");
1120 a = 0xfffff;
1121 cast1(a, a, a, a);
1122 a = 0xffffe;
1123 printf("%d %d %d %d\n",
1124 (char)(a + 1),
1125 (short)(a + 1),
1126 (unsigned char)(a + 1),
1127 (unsigned short)(a + 1));
1128 printf("%d %d %d %d\n",
1129 (char)0xfffff,
1130 (short)0xfffff,
1131 (unsigned char)0xfffff,
1132 (unsigned short)0xfffff);
1134 a = (bcast = 128) + 1;
1135 printf("%d\n", a);
1136 a = (scast = 65536) + 1;
1137 printf("%d\n", a);
1139 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1141 /* test cast from unsigned to signed short to int */
1142 b = 0xf000;
1143 d = (short)b;
1144 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1145 b = 0xf0f0;
1146 d = (char)b;
1147 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1149 /* test implicit int casting for array accesses */
1150 c = 0;
1151 tab[1] = 2;
1152 tab[c] = 1;
1153 printf("%d %d\n", tab[0], tab[1]);
1155 /* test implicit casting on some operators */
1156 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1157 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1158 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1160 /* from pointer to integer types */
1161 printf("%d %d %ld %ld %lld %lld\n",
1162 (int)p, (unsigned int)p,
1163 (long)p, (unsigned long)p,
1164 (long long)p, (unsigned long long)p);
1166 /* from integers to pointers */
1167 printf("%p %p %p %p\n",
1168 (void *)a, (void *)b, (void *)c, (void *)d);
1171 /* initializers tests */
1172 struct structinit1 {
1173 int f1;
1174 char f2;
1175 short f3;
1176 int farray[3];
1179 int sinit1 = 2;
1180 int sinit2 = { 3 };
1181 int sinit3[3] = { 1, 2, {{3}}, };
1182 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1183 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1184 int sinit6[] = { 1, 2, 3 };
1185 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1186 char sinit8[] = "hello" "trala";
1188 struct structinit1 sinit9 = { 1, 2, 3 };
1189 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1190 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1191 #ifdef ALL_ISOC99
1192 .farray[0] = 10,
1193 .farray[1] = 11,
1194 .farray[2] = 12,
1195 #endif
1198 char *sinit12 = "hello world";
1199 char *sinit13[] = {
1200 "test1",
1201 "test2",
1202 "test3",
1204 char sinit14[10] = { "abc" };
1205 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1207 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1209 struct bar {
1210 char *s;
1211 int len;
1212 } sinit17[] = {
1213 "a1", 4,
1214 "a2", 1
1217 int sinit18[10] = {
1218 [2 ... 5] = 20,
1220 [8] = 10,
1223 void init_test(void)
1225 int linit1 = 2;
1226 int linit2 = { 3 };
1227 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1228 int linit6[] = { 1, 2, 3 };
1229 int i, j;
1230 char linit8[] = "hello" "trala";
1231 int linit12[10] = { 1, 2 };
1232 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1233 char linit14[10] = "abc";
1234 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1235 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1236 int linit17 = sizeof(linit17);
1238 printf("init_test:\n");
1240 printf("sinit1=%d\n", sinit1);
1241 printf("sinit2=%d\n", sinit2);
1242 printf("sinit3=%d %d %d %d\n",
1243 sizeof(sinit3),
1244 sinit3[0],
1245 sinit3[1],
1246 sinit3[2]
1248 printf("sinit6=%d\n", sizeof(sinit6));
1249 printf("sinit7=%d %d %d %d\n",
1250 sizeof(sinit7),
1251 sinit7[0],
1252 sinit7[1],
1253 sinit7[2]
1255 printf("sinit8=%s\n", sinit8);
1256 printf("sinit9=%d %d %d\n",
1257 sinit9.f1,
1258 sinit9.f2,
1259 sinit9.f3
1261 printf("sinit10=%d %d %d\n",
1262 sinit10.f1,
1263 sinit10.f2,
1264 sinit10.f3
1266 printf("sinit11=%d %d %d %d %d %d\n",
1267 sinit11.f1,
1268 sinit11.f2,
1269 sinit11.f3,
1270 sinit11.farray[0],
1271 sinit11.farray[1],
1272 sinit11.farray[2]
1275 for(i=0;i<3;i++)
1276 for(j=0;j<2;j++)
1277 printf("[%d][%d] = %d %d %d\n",
1278 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1279 printf("linit1=%d\n", linit1);
1280 printf("linit2=%d\n", linit2);
1281 printf("linit6=%d\n", sizeof(linit6));
1282 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1284 printf("sinit12=%s\n", sinit12);
1285 printf("sinit13=%d %s %s %s\n",
1286 sizeof(sinit13),
1287 sinit13[0],
1288 sinit13[1],
1289 sinit13[2]);
1290 printf("sinit14=%s\n", sinit14);
1292 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1293 printf("\n");
1294 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1295 printf("\n");
1296 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1297 printf("\n");
1298 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1299 printf("\n");
1300 printf("%d %d %d %d\n",
1301 linit16.a1,
1302 linit16.a2,
1303 linit16.a3,
1304 linit16.a4);
1305 /* test that initialisation is done after variable declare */
1306 printf("linit17=%d\n", linit17);
1307 printf("sinit15=%d\n", sinit15[0]);
1308 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1309 printf("sinit17=%s %d %s %d\n",
1310 sinit17[0].s, sinit17[0].len,
1311 sinit17[1].s, sinit17[1].len);
1312 for(i=0;i<10;i++)
1313 printf("%x ", sinit18[i]);
1314 printf("\n");
1318 void switch_test()
1320 int i;
1322 for(i=0;i<15;i++) {
1323 switch(i) {
1324 case 0:
1325 case 1:
1326 printf("a");
1327 break;
1328 default:
1329 printf("%d", i);
1330 break;
1331 case 8 ... 12:
1332 printf("c");
1333 break;
1334 case 3:
1335 printf("b");
1336 break;
1339 printf("\n");
1342 /* ISOC99 _Bool type */
1343 void c99_bool_test(void)
1345 #ifdef BOOL_ISOC99
1346 int a;
1347 _Bool b;
1349 printf("bool_test:\n");
1350 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1351 a = 3;
1352 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1353 b = 3;
1354 printf("b = %d\n", b);
1355 b++;
1356 printf("b = %d\n", b);
1357 #endif
1360 void bitfield_test(void)
1362 int a;
1363 struct sbf1 {
1364 int f1 : 3;
1365 int : 2;
1366 int f2 : 1;
1367 int : 0;
1368 int f3 : 5;
1369 int f4 : 7;
1370 unsigned int f5 : 7;
1371 } st1;
1372 printf("bitfield_test:");
1373 printf("sizeof(st1) = %d\n", sizeof(st1));
1375 st1.f1 = 3;
1376 st1.f2 = 1;
1377 st1.f3 = 15;
1378 a = 120;
1379 st1.f4 = a;
1380 st1.f5 = a;
1381 st1.f5++;
1382 printf("%d %d %d %d %d\n",
1383 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1385 st1.f1 = 7;
1386 if (st1.f1 == -1)
1387 printf("st1.f1 == -1\n");
1388 else
1389 printf("st1.f1 != -1\n");
1390 if (st1.f2 == -1)
1391 printf("st1.f2 == -1\n");
1392 else
1393 printf("st1.f2 != -1\n");
1395 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1396 long-long bitfields whose size is not bigger than int */
1397 struct sbf2 {
1398 long long f1 : 45;
1399 long long : 2;
1400 long long f2 : 35;
1401 unsigned long long f3 : 38;
1402 } st2;
1403 st2.f1 = 0x123456789ULL;
1404 a = 120;
1405 st2.f2 = (long long)a << 25;
1406 st2.f3 = a;
1407 st2.f2++;
1408 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
1411 #ifdef __x86_64__
1412 #define FLOAT_FMT "%f\n"
1413 #else
1414 /* x86's float isn't compatible with GCC */
1415 #define FLOAT_FMT "%.5f\n"
1416 #endif
1418 /* declare strto* functions as they are C99 */
1419 double strtod(const char *nptr, char **endptr);
1420 float strtof(const char *nptr, char **endptr);
1421 long double strtold(const char *nptr, char **endptr);
1423 #define FTEST(prefix, type, fmt)\
1424 void prefix ## cmp(type a, type b)\
1426 printf("%d %d %d %d %d %d\n",\
1427 a == b,\
1428 a != b,\
1429 a < b,\
1430 a > b,\
1431 a >= b,\
1432 a <= b);\
1433 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1436 a + b,\
1437 a - b,\
1438 a * b,\
1439 a / b,\
1440 -a);\
1441 printf(fmt "\n", ++a);\
1442 printf(fmt "\n", a++);\
1443 printf(fmt "\n", a);\
1444 b = 0;\
1445 printf("%d %d\n", !a, !b);\
1447 void prefix ## fcast(type a)\
1449 float fa;\
1450 double da;\
1451 long double la;\
1452 int ia;\
1453 unsigned int ua;\
1454 type b;\
1455 fa = a;\
1456 da = a;\
1457 la = a;\
1458 printf("ftof: %f %f %Lf\n", fa, da, la);\
1459 ia = (int)a;\
1460 ua = (unsigned int)a;\
1461 printf("ftoi: %d %u\n", ia, ua);\
1462 ia = -1234;\
1463 ua = 0x81234500;\
1464 b = ia;\
1465 printf("itof: " fmt "\n", b);\
1466 b = ua;\
1467 printf("utof: " fmt "\n", b);\
1470 float prefix ## retf(type a) { return a; }\
1471 double prefix ## retd(type a) { return a; }\
1472 long double prefix ## retld(type a) { return a; }\
1474 void prefix ## call(void)\
1476 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1477 printf("double: %f\n", prefix ## retd(42.123456789));\
1478 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1479 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
1482 void prefix ## test(void)\
1484 printf("testing '%s'\n", #type);\
1485 prefix ## cmp(1, 2.5);\
1486 prefix ## cmp(2, 1.5);\
1487 prefix ## cmp(1, 1);\
1488 prefix ## fcast(234.6);\
1489 prefix ## fcast(-2334.6);\
1490 prefix ## call();\
1493 FTEST(f, float, "%f")
1494 FTEST(d, double, "%f")
1495 FTEST(ld, long double, "%Lf")
1497 double ftab1[3] = { 1.2, 3.4, -5.6 };
1500 void float_test(void)
1502 float fa, fb;
1503 double da, db;
1504 int a;
1505 unsigned int b;
1507 printf("float_test:\n");
1508 printf("sizeof(float) = %d\n", sizeof(float));
1509 printf("sizeof(double) = %d\n", sizeof(double));
1510 printf("sizeof(long double) = %d\n", sizeof(long double));
1511 ftest();
1512 dtest();
1513 ldtest();
1514 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1515 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1516 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1517 da = 123;
1518 printf("da=%f\n", da);
1519 fa = 123;
1520 printf("fa=%f\n", fa);
1521 a = 4000000000;
1522 da = a;
1523 printf("da = %f\n", da);
1524 b = 4000000000;
1525 db = b;
1526 printf("db = %f\n", db);
1529 int fib(int n)
1531 if (n <= 2)
1532 return 1;
1533 else
1534 return fib(n-1) + fib(n-2);
1537 void funcptr_test()
1539 void (*func)(int);
1540 int a;
1541 struct {
1542 int dummy;
1543 void (*func)(int);
1544 } st1;
1546 printf("funcptr:\n");
1547 func = &num;
1548 (*func)(12345);
1549 func = num;
1550 a = 1;
1551 a = 1;
1552 func(12345);
1553 /* more complicated pointer computation */
1554 st1.func = num;
1555 st1.func(12346);
1556 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1557 printf("sizeof2 = %d\n", sizeof funcptr_test);
1558 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1559 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1562 void lloptest(long long a, long long b)
1564 unsigned long long ua, ub;
1566 ua = a;
1567 ub = b;
1568 /* arith */
1569 printf("arith: %Ld %Ld %Ld\n",
1570 a + b,
1571 a - b,
1572 a * b);
1574 if (b != 0) {
1575 printf("arith1: %Ld %Ld\n",
1576 a / b,
1577 a % b);
1580 /* binary */
1581 printf("bin: %Ld %Ld %Ld\n",
1582 a & b,
1583 a | b,
1584 a ^ b);
1586 /* tests */
1587 printf("test: %d %d %d %d %d %d\n",
1588 a == b,
1589 a != b,
1590 a < b,
1591 a > b,
1592 a >= b,
1593 a <= b);
1595 printf("utest: %d %d %d %d %d %d\n",
1596 ua == ub,
1597 ua != ub,
1598 ua < ub,
1599 ua > ub,
1600 ua >= ub,
1601 ua <= ub);
1603 /* arith2 */
1604 a++;
1605 b++;
1606 printf("arith2: %Ld %Ld\n", a, b);
1607 printf("arith2: %Ld %Ld\n", a++, b++);
1608 printf("arith2: %Ld %Ld\n", --a, --b);
1609 printf("arith2: %Ld %Ld\n", a, b);
1610 b = ub = 0;
1611 printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
1614 void llshift(long long a, int b)
1616 printf("shift: %Ld %Ld %Ld\n",
1617 (unsigned long long)a >> b,
1618 a >> b,
1619 a << b);
1620 printf("shiftc: %Ld %Ld %Ld\n",
1621 (unsigned long long)a >> 3,
1622 a >> 3,
1623 a << 3);
1624 printf("shiftc: %Ld %Ld %Ld\n",
1625 (unsigned long long)a >> 35,
1626 a >> 35,
1627 a << 35);
1630 void llfloat(void)
1632 float fa;
1633 double da;
1634 long double lda;
1635 long long la, lb, lc;
1636 unsigned long long ula, ulb, ulc;
1637 la = 0x12345678;
1638 ula = 0x72345678;
1639 la = (la << 20) | 0x12345;
1640 ula = ula << 33;
1641 printf("la=%Ld ula=%Lu\n", la, ula);
1643 fa = la;
1644 da = la;
1645 lda = la;
1646 printf("lltof: %f %f %Lf\n", fa, da, lda);
1648 la = fa;
1649 lb = da;
1650 lc = lda;
1651 printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
1653 fa = ula;
1654 da = ula;
1655 lda = ula;
1656 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1658 ula = fa;
1659 ulb = da;
1660 ulc = lda;
1661 printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
1664 long long llfunc1(int a)
1666 return a * 2;
1669 struct S {
1670 int id;
1671 char item;
1674 long long int value(struct S *v)
1676 return ((long long int)v->item);
1679 void longlong_test(void)
1681 long long a, b, c;
1682 int ia;
1683 unsigned int ua;
1684 printf("longlong_test:\n");
1685 printf("sizeof(long long) = %d\n", sizeof(long long));
1686 ia = -1;
1687 ua = -2;
1688 a = ia;
1689 b = ua;
1690 printf("%Ld %Ld\n", a, b);
1691 printf("%Ld %Ld %Ld %Lx\n",
1692 (long long)1,
1693 (long long)-2,
1694 1LL,
1695 0x1234567812345679);
1696 a = llfunc1(-3);
1697 printf("%Ld\n", a);
1699 lloptest(1000, 23);
1700 lloptest(0xff, 0x1234);
1701 b = 0x72345678 << 10;
1702 lloptest(-3, b);
1703 llshift(0x123, 5);
1704 llshift(-23, 5);
1705 b = 0x72345678LL << 10;
1706 llshift(b, 47);
1708 llfloat();
1709 #if 1
1710 b = 0x12345678;
1711 a = -1;
1712 c = a + b;
1713 printf("%Lx\n", c);
1714 #endif
1716 /* long long reg spill test */
1718 struct S a;
1720 a.item = 3;
1721 printf("%lld\n", value(&a));
1723 lloptest(0x80000000, 0);
1725 /* another long long spill test */
1727 long long *p, v;
1728 v = 1;
1729 p = &v;
1730 p[0]++;
1731 printf("%lld\n", *p);
1734 a = 68719476720LL;
1735 b = 4294967295LL;
1736 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
1738 printf("%Ld\n", 0x123456789LLU);
1741 void manyarg_test(void)
1743 long double ld = 1234567891234LL;
1744 printf("manyarg_test:\n");
1745 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
1746 1, 2, 3, 4, 5, 6, 7, 8,
1747 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
1748 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1749 "%Ld %Ld %f %f\n",
1750 1, 2, 3, 4, 5, 6, 7, 8,
1751 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1752 1234567891234LL, 987654321986LL,
1753 42.0, 43.0);
1754 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1755 "%Ld %Ld %f %f\n",
1756 ld, 1, 2, 3, 4, 5, 6, 7, 8,
1757 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1758 1234567891234LL, 987654321986LL,
1759 42.0, 43.0);
1760 /* XXX: known bug of x86-64 */
1761 #ifndef __x86_64__
1762 printf("%d %d %d %d %d %d %d %d %Lf\n",
1763 1, 2, 3, 4, 5, 6, 7, 8, ld);
1764 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1765 "%Ld %Ld %f %f %Lf\n",
1766 1, 2, 3, 4, 5, 6, 7, 8,
1767 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1768 1234567891234LL, 987654321986LL,
1769 42.0, 43.0, ld);
1770 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1771 "%Lf %Ld %Ld %f %f %Lf\n",
1772 1, 2, 3, 4, 5, 6, 7, 8,
1773 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1774 ld, 1234567891234LL, 987654321986LL,
1775 42.0, 43.0, ld);
1776 #endif
1779 void vprintf1(const char *fmt, ...)
1781 va_list ap;
1782 const char *p;
1783 int c, i;
1784 double d;
1785 long long ll;
1786 long double ld;
1788 va_start(ap, fmt);
1790 p = fmt;
1791 for(;;) {
1792 c = *p;
1793 if (c == '\0')
1794 break;
1795 p++;
1796 if (c == '%') {
1797 c = *p;
1798 switch(c) {
1799 case '\0':
1800 goto the_end;
1801 case 'd':
1802 i = va_arg(ap, int);
1803 printf("%d", i);
1804 break;
1805 case 'f':
1806 d = va_arg(ap, double);
1807 printf("%f", d);
1808 break;
1809 case 'l':
1810 ll = va_arg(ap, long long);
1811 printf("%Ld", ll);
1812 break;
1813 case 'F':
1814 ld = va_arg(ap, long double);
1815 printf("%Lf", ld);
1816 break;
1818 p++;
1819 } else {
1820 putchar(c);
1823 the_end:
1824 va_end(ap);
1828 void stdarg_test(void)
1830 long double ld = 1234567891234LL;
1831 vprintf1("%d %d %d\n", 1, 2, 3);
1832 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1833 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1834 vprintf1("%F %F %F\n", 1.2L, 2.3L, 3.4L);
1835 #ifdef __x86_64__
1836 /* a bug of x86's TCC */
1837 vprintf1("%d %f %l %F %d %f %l %F\n",
1838 1, 1.2, 3L, 4.5L, 6, 7.8, 9L, 0.1L);
1839 #endif
1840 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
1841 1, 2, 3, 4, 5, 6, 7, 8,
1842 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
1843 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
1844 1, 2, 3, 4, 5, 6, 7, 8,
1845 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
1846 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1847 "%l %l %f %f\n",
1848 1, 2, 3, 4, 5, 6, 7, 8,
1849 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1850 1234567891234LL, 987654321986LL,
1851 42.0, 43.0);
1852 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1853 "%l %l %f %f\n",
1854 ld, 1, 2, 3, 4, 5, 6, 7, 8,
1855 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1856 1234567891234LL, 987654321986LL,
1857 42.0, 43.0);
1858 vprintf1("%d %d %d %d %d %d %d %d %F\n",
1859 1, 2, 3, 4, 5, 6, 7, 8, ld);
1860 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1861 "%l %l %f %f %F\n",
1862 1, 2, 3, 4, 5, 6, 7, 8,
1863 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1864 1234567891234LL, 987654321986LL,
1865 42.0, 43.0, ld);
1866 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1867 "%F %l %l %f %f %F\n",
1868 1, 2, 3, 4, 5, 6, 7, 8,
1869 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1870 ld, 1234567891234LL, 987654321986LL,
1871 42.0, 43.0, ld);
1874 void whitespace_test(void)
1876 char *str;
1878 \f\v #if 1
1879 pri\
1880 ntf("whitspace:\n");\f\v
1881 #endif
1882 pf("N=%d\n", 2);
1884 #ifdef CORRECT_CR_HANDLING
1885 pri\
1886 ntf("aaa=%d\n", 3);
1887 #endif
1889 pri\
1891 ntf("min=%d\n", 4);
1893 #ifdef ACCEPT_CR_IN_STRINGS
1894 printf("len1=%d\n", strlen("
1895 "));
1896 #ifdef CORRECT_CR_HANDLING
1897 str = "
1899 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
1900 #endif
1901 printf("len1=%d\n", strlen(" a
1902 "));
1903 #endif /* ACCEPT_CR_IN_STRINGS */
1906 int reltab[3] = { 1, 2, 3 };
1908 int *rel1 = &reltab[1];
1909 int *rel2 = &reltab[2];
1911 void relocation_test(void)
1913 printf("*rel1=%d\n", *rel1);
1914 printf("*rel2=%d\n", *rel2);
1917 void old_style_f(a,b,c)
1918 int a, b;
1919 double c;
1921 printf("a=%d b=%d b=%f\n", a, b, c);
1924 void decl_func1(int cmpfn())
1926 printf("cmpfn=%lx\n", (long)cmpfn);
1929 void decl_func2(cmpfn)
1930 int cmpfn();
1932 printf("cmpfn=%lx\n", (long)cmpfn);
1935 void old_style_function(void)
1937 old_style_f((void *)1, 2, 3.0);
1938 decl_func1(NULL);
1939 decl_func2(NULL);
1942 void alloca_test()
1944 #if defined __i386__ || defined __x86_64__
1945 char *p = alloca(16);
1946 strcpy(p,"123456789012345");
1947 printf("alloca: p is %s\n", p);
1948 char *demo = "This is only a test.\n";
1949 /* Test alloca embedded in a larger expression */
1950 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
1951 #endif
1954 void sizeof_test(void)
1956 int a;
1957 int **ptr;
1959 printf("sizeof(int) = %d\n", sizeof(int));
1960 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1961 printf("sizeof(long) = %d\n", sizeof(long));
1962 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
1963 printf("sizeof(short) = %d\n", sizeof(short));
1964 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1965 printf("sizeof(char) = %d\n", sizeof(char));
1966 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1967 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1968 a = 1;
1969 printf("sizeof(a++) = %d\n", sizeof a++);
1970 printf("a=%d\n", a);
1971 ptr = NULL;
1972 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
1974 /* some alignof tests */
1975 printf("__alignof__(int) = %d\n", __alignof__(int));
1976 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1977 printf("__alignof__(short) = %d\n", __alignof__(short));
1978 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1979 printf("__alignof__(char) = %d\n", __alignof__(char));
1980 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1981 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
1984 void typeof_test(void)
1986 double a;
1987 typeof(a) b;
1988 typeof(float) c;
1990 a = 1.5;
1991 b = 2.5;
1992 c = 3.5;
1993 printf("a=%f b=%f c=%f\n", a, b, c);
1996 void statement_expr_test(void)
1998 int a, i;
2000 a = 0;
2001 for(i=0;i<10;i++) {
2002 a += 1 +
2003 ( { int b, j;
2004 b = 0;
2005 for(j=0;j<5;j++)
2006 b += j; b;
2007 } );
2009 printf("a=%d\n", a);
2013 void local_label_test(void)
2015 int a;
2016 goto l1;
2018 a = 1 + ({
2019 __label__ l1, l2, l3, l4;
2020 goto l1;
2022 printf("aa1\n");
2023 goto l3;
2025 printf("aa3\n");
2026 goto l4;
2028 printf("aa2\n");
2029 goto l2;
2030 l3:;
2033 printf("a=%d\n", a);
2034 return;
2036 printf("bb1\n");
2037 goto l2;
2039 printf("bb2\n");
2040 goto l4;
2043 /* inline assembler test */
2044 #ifdef __i386__
2046 /* from linux kernel */
2047 static char * strncat1(char * dest,const char * src,size_t count)
2049 int d0, d1, d2, d3;
2050 __asm__ __volatile__(
2051 "repne\n\t"
2052 "scasb\n\t"
2053 "decl %1\n\t"
2054 "movl %8,%3\n"
2055 "1:\tdecl %3\n\t"
2056 "js 2f\n\t"
2057 "lodsb\n\t"
2058 "stosb\n\t"
2059 "testb %%al,%%al\n\t"
2060 "jne 1b\n"
2061 "2:\txorl %2,%2\n\t"
2062 "stosb"
2063 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2064 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2065 : "memory");
2066 return dest;
2069 static inline void * memcpy1(void * to, const void * from, size_t n)
2071 int d0, d1, d2;
2072 __asm__ __volatile__(
2073 "rep ; movsl\n\t"
2074 "testb $2,%b4\n\t"
2075 "je 1f\n\t"
2076 "movsw\n"
2077 "1:\ttestb $1,%b4\n\t"
2078 "je 2f\n\t"
2079 "movsb\n"
2080 "2:"
2081 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2082 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2083 : "memory");
2084 return (to);
2087 static __inline__ void sigaddset1(unsigned int *set, int _sig)
2089 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2092 static __inline__ void sigdelset1(unsigned int *set, int _sig)
2094 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2097 static __inline__ __const__ unsigned int swab32(unsigned int x)
2099 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2100 "rorl $16,%0\n\t" /* swap words */
2101 "xchgb %b0,%h0" /* swap higher bytes */
2102 :"=q" (x)
2103 : "0" (x));
2104 return x;
2107 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
2109 unsigned long long res;
2110 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
2111 return res;
2114 static __inline__ unsigned long long inc64(unsigned long long a)
2116 unsigned long long res;
2117 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
2118 return res;
2121 unsigned int set;
2123 void asm_test(void)
2125 char buf[128];
2126 unsigned int val;
2128 printf("inline asm:\n");
2129 /* test the no operand case */
2130 asm volatile ("xorl %eax, %eax");
2132 memcpy1(buf, "hello", 6);
2133 strncat1(buf, " worldXXXXX", 3);
2134 printf("%s\n", buf);
2136 /* 'A' constraint test */
2137 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2138 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2140 set = 0xff;
2141 sigdelset1(&set, 2);
2142 sigaddset1(&set, 16);
2143 /* NOTE: we test here if C labels are correctly restored after the
2144 asm statement */
2145 goto label1;
2146 label2:
2147 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
2148 #ifdef __GNUC__ // works strange with GCC 4.3
2149 set=0x1080fd;
2150 #endif
2151 printf("set=0x%x\n", set);
2152 val = 0x01020304;
2153 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
2154 return;
2155 label1:
2156 goto label2;
2159 #else
2161 void asm_test(void)
2165 #endif
2167 #define COMPAT_TYPE(type1, type2) \
2169 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2170 __builtin_types_compatible_p (type1, type2));\
2173 int constant_p_var;
2175 void builtin_test(void)
2177 #if GCC_MAJOR >= 3
2178 COMPAT_TYPE(int, int);
2179 COMPAT_TYPE(int, unsigned int);
2180 COMPAT_TYPE(int, char);
2181 COMPAT_TYPE(int, const int);
2182 COMPAT_TYPE(int, volatile int);
2183 COMPAT_TYPE(int *, int *);
2184 COMPAT_TYPE(int *, void *);
2185 COMPAT_TYPE(int *, const int *);
2186 COMPAT_TYPE(char *, unsigned char *);
2187 /* space is needed because tcc preprocessor introduces a space between each token */
2188 COMPAT_TYPE(char * *, void *);
2189 #endif
2190 printf("res = %d\n", __builtin_constant_p(1));
2191 printf("res = %d\n", __builtin_constant_p(1 + 2));
2192 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
2193 printf("res = %d\n", __builtin_constant_p(constant_p_var));
2197 void const_func(const int a)
2201 void const_warn_test(void)
2203 const_func(1);