First naive DLL support on x86-64.
[tinycc.git] / tcctest.c
blob012a51b9e46b01b1e3cc6010295890fe36e96903
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 sizeof_test(void);
79 void typeof_test(void);
80 void local_label_test(void);
81 void statement_expr_test(void);
82 void asm_test(void);
83 void builtin_test(void);
85 int fib(int n);
86 void num(int n);
87 void forward_ref(void);
88 int isid(int c);
90 #define A 2
91 #define N 1234 + A
92 #define pf printf
93 #define M1(a, b) (a) + (b)
95 #define str\
96 (s) # s
97 #define glue(a, b) a ## b
98 #define xglue(a, b) glue(a, b)
99 #define HIGHLOW "hello"
100 #define LOW LOW ", world"
102 #define min(a, b) ((a) < (b) ? (a) : (b))
104 #ifdef C99_MACROS
105 #define dprintf(level,...) printf(__VA_ARGS__)
106 #endif
108 /* gcc vararg macros */
109 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
111 #define MACRO_NOARGS()
113 #define AAA 3
114 #undef AAA
115 #define AAA 4
117 #if 1
118 #define B3 1
119 #elif 1
120 #define B3 2
121 #elif 0
122 #define B3 3
123 #else
124 #define B3 4
125 #endif
127 #define __INT64_C(c) c ## LL
128 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
130 int qq(int x)
132 return x + 40;
134 #define qq(x) x
136 #define spin_lock(lock) do { } while (0)
137 #define wq_spin_lock spin_lock
138 #define TEST2() wq_spin_lock(a)
140 void macro_test(void)
142 printf("macro:\n");\f\v
143 pf("N=%d\n", N);
144 printf("aaa=%d\n", AAA);
146 printf("min=%d\n", min(1, min(2, -1)));
148 printf("s1=%s\n", glue(HIGH, LOW));
149 printf("s2=%s\n", xglue(HIGH, LOW));
150 printf("s3=%s\n", str("c"));
151 printf("s4=%s\n", str(a1));
152 printf("B3=%d\n", B3);
154 #ifdef A
155 printf("A defined\n");
156 #endif
157 #ifdef B
158 printf("B defined\n");
159 #endif
160 #ifdef A
161 printf("A defined\n");
162 #else
163 printf("A not defined\n");
164 #endif
165 #ifdef B
166 printf("B defined\n");
167 #else
168 printf("B not defined\n");
169 #endif
171 #ifdef A
172 printf("A defined\n");
173 #ifdef B
174 printf("B1 defined\n");
175 #else
176 printf("B1 not defined\n");
177 #endif
178 #else
179 printf("A not defined\n");
180 #ifdef B
181 printf("B2 defined\n");
182 #else
183 printf("B2 not defined\n");
184 #endif
185 #endif
187 #if 1+1
188 printf("test true1\n");
189 #endif
190 #if 0
191 printf("test true2\n");
192 #endif
193 #if 1-1
194 printf("test true3\n");
195 #endif
196 #if defined(A)
197 printf("test trueA\n");
198 #endif
199 #if defined(B)
200 printf("test trueB\n");
201 #endif
203 #if 0
204 printf("test 0\n");
205 #elif 0
206 printf("test 1\n");
207 #elif 2
208 printf("test 2\n");
209 #else
210 printf("test 3\n");
211 #endif
213 MACRO_NOARGS();
215 #ifdef __LINE__
216 printf("__LINE__ defined\n");
217 #endif
219 printf("__LINE__=%d __FILE__=%s\n",
220 __LINE__, __FILE__);
221 #line 200
222 printf("__LINE__=%d __FILE__=%s\n",
223 __LINE__, __FILE__);
224 #line 203 "test"
225 printf("__LINE__=%d __FILE__=%s\n",
226 __LINE__, __FILE__);
227 #line 227 "tcctest.c"
229 /* not strictly preprocessor, but we test it there */
230 #ifdef C99_MACROS
231 printf("__func__ = %s\n", __func__);
232 dprintf(1, "vaarg=%d\n", 1);
233 #endif
234 dprintf1(1, "vaarg1\n");
235 dprintf1(1, "vaarg1=%d\n", 2);
236 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
238 /* gcc extension */
239 printf("func='%s'\n", __FUNCTION__);
241 /* complicated macros in glibc */
242 printf("INT64_MIN=%Ld\n", INT64_MIN);
244 int a;
245 a = 1;
246 glue(a+, +);
247 printf("a=%d\n", a);
248 glue(a <, <= 2);
249 printf("a=%d\n", a);
252 /* macro function with argument outside the macro string */
253 #define MF_s MF_hello
254 #define MF_hello(msg) printf("%s\n",msg)
256 #define MF_t printf("tralala\n"); MF_hello
258 MF_s("hi");
259 MF_t("hi");
261 /* test macro substituion inside args (should not eat stream) */
262 printf("qq=%d\n", qq(qq)(2));
264 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
265 null argument without a space. gcc 3.2 fixes that. */
267 #define qq1(x) 1
268 printf("qq1=%d\n", qq1( ));
270 /* comment with stray handling *\
272 /* this is a valid *\/ comment */
273 /* this is a valid comment *\*/
274 // this is a valid\
275 comment
277 /* test function macro substitution when the function name is
278 substituted */
279 TEST2();
282 int op(a,b)
284 return a / b;
287 int ret(a)
289 if (a == 2)
290 return 1;
291 if (a == 3)
292 return 2;
293 return 0;
296 void ps(const char *s)
298 int c;
299 while (1) {
300 c = *s;
301 if (c == 0)
302 break;
303 printf("%c", c);
304 s++;
308 const char foo1_string[] = "\
309 bar\n\
310 test\14\
313 void string_test()
315 unsigned int b;
316 printf("string:\n");
317 printf("\141\1423\143\n");/* dezdez test */
318 printf("\x41\x42\x43\x3a\n");
319 printf("c=%c\n", 'r');
320 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
321 printf("foo1_string='%s'\n", foo1_string);
322 #if 0
323 printf("wstring=%S\n", L"abc");
324 printf("wstring=%S\n", L"abc" L"def" "ghi");
325 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
326 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
327 #endif
328 ps("test\n");
329 b = 32;
330 while ((b = b + 1) < 96) {
331 printf("%c", b);
333 printf("\n");
334 printf("fib=%d\n", fib(33));
335 b = 262144;
336 while (b != 0x80000000) {
337 num(b);
338 b = b * 2;
342 void loop_test()
344 int i;
345 i = 0;
346 while (i < 10)
347 printf("%d", i++);
348 printf("\n");
349 for(i = 0; i < 10;i++)
350 printf("%d", i);
351 printf("\n");
352 i = 0;
353 do {
354 printf("%d", i++);
355 } while (i < 10);
356 printf("\n");
358 /* break/continue tests */
359 i = 0;
360 while (1) {
361 if (i == 6)
362 break;
363 i++;
364 if (i == 3)
365 continue;
366 printf("%d", i);
368 printf("\n");
370 /* break/continue tests */
371 i = 0;
372 do {
373 if (i == 6)
374 break;
375 i++;
376 if (i == 3)
377 continue;
378 printf("%d", i);
379 } while(1);
380 printf("\n");
382 for(i = 0;i < 10;i++) {
383 if (i == 3)
384 continue;
385 printf("%d", i);
387 printf("\n");
391 void goto_test()
393 int i;
394 static void *label_table[3] = { &&label1, &&label2, &&label3 };
396 printf("goto:\n");
397 i = 0;
398 s_loop:
399 if (i >= 10)
400 goto s_end;
401 printf("%d", i);
402 i++;
403 goto s_loop;
404 s_end:
405 printf("\n");
407 /* we also test computed gotos (GCC extension) */
408 for(i=0;i<3;i++) {
409 goto *label_table[i];
410 label1:
411 printf("label1\n");
412 goto next;
413 label2:
414 printf("label2\n");
415 goto next;
416 label3:
417 printf("label3\n");
418 next: ;
422 enum {
424 E1 = 2,
425 E2 = 4,
430 enum test {
431 E5 = 1000,
434 void enum_test()
436 enum test b1;
437 printf("enum:\n%d %d %d %d %d %d\n",
438 E0, E1, E2, E3, E4, E5);
439 b1 = 1;
440 printf("b1=%d\n", b1);
443 typedef int *my_ptr;
445 typedef int mytype1;
446 typedef int mytype2;
448 void typedef_test()
450 my_ptr a;
451 mytype1 mytype2;
452 int b;
454 a = &b;
455 *a = 1234;
456 printf("typedef:\n");
457 printf("a=%d\n", *a);
458 mytype2 = 2;
459 printf("mytype2=%d\n", mytype2);
462 void forward_test()
464 printf("forward:\n");
465 forward_ref();
466 forward_ref();
470 void forward_ref(void)
472 printf("forward ok\n");
475 typedef struct struct1 {
476 int f1;
477 int f2, f3;
478 union union1 {
479 int v1;
480 int v2;
481 } u;
482 char str[3];
483 } struct1;
485 struct struct2 {
486 int a;
487 char b;
490 union union2 {
491 int w1;
492 int w2;
495 struct struct1 st1, st2;
497 int main(int argc, char **argv)
499 string_test();
500 expr_test();
501 macro_test();
502 scope_test();
503 forward_test();
504 funcptr_test();
505 loop_test();
506 switch_test();
507 goto_test();
508 enum_test();
509 typedef_test();
510 struct_test();
511 array_test();
512 expr_ptr_test();
513 bool_test();
514 expr2_test();
515 constant_expr_test();
516 expr_cmp_test();
517 char_short_test();
518 init_test();
519 compound_literal_test();
520 kr_test();
521 struct_assign_test();
522 cast_test();
523 bitfield_test();
524 c99_bool_test();
525 float_test();
526 longlong_test();
527 manyarg_test();
528 stdarg_test();
529 whitespace_test();
530 relocation_test();
531 old_style_function();
532 sizeof_test();
533 typeof_test();
534 statement_expr_test();
535 local_label_test();
536 asm_test();
537 builtin_test();
538 return 0;
541 int tab[3];
542 int tab2[3][2];
544 int g;
546 void f1(g)
548 printf("g1=%d\n", g);
551 void scope_test()
553 printf("scope:\n");
554 g = 2;
555 f1(1);
556 printf("g2=%d\n", g);
558 int g;
559 g = 3;
560 printf("g3=%d\n", g);
562 int g;
563 g = 4;
564 printf("g4=%d\n", g);
567 printf("g5=%d\n", g);
570 void array_test(int a[4])
572 int i, j;
574 printf("array:\n");
575 printf("sizeof(a) = %d\n", sizeof(a));
576 printf("sizeof(\"a\") = %d\n", sizeof("a"));
577 #ifdef C99_MACROS
578 printf("sizeof(__func__) = %d\n", sizeof(__func__));
579 #endif
580 printf("sizeof tab %d\n", sizeof(tab));
581 printf("sizeof tab2 %d\n", sizeof tab2);
582 tab[0] = 1;
583 tab[1] = 2;
584 tab[2] = 3;
585 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
586 for(i=0;i<3;i++)
587 for(j=0;j<2;j++)
588 tab2[i][j] = 10 * i + j;
589 for(i=0;i<3*2;i++) {
590 printf(" %3d", ((int *)tab2)[i]);
592 printf("\n");
595 void expr_test()
597 int a, b;
598 a = 0;
599 printf("%d\n", a += 1);
600 printf("%d\n", a -= 2);
601 printf("%d\n", a *= 31232132);
602 printf("%d\n", a /= 4);
603 printf("%d\n", a %= 20);
604 printf("%d\n", a &= 6);
605 printf("%d\n", a ^= 7);
606 printf("%d\n", a |= 8);
607 printf("%d\n", a >>= 3);
608 printf("%d\n", a <<= 4);
610 a = 22321;
611 b = -22321;
612 printf("%d\n", a + 1);
613 printf("%d\n", a - 2);
614 printf("%d\n", a * 312);
615 printf("%d\n", a / 4);
616 printf("%d\n", b / 4);
617 printf("%d\n", (unsigned)b / 4);
618 printf("%d\n", a % 20);
619 printf("%d\n", b % 20);
620 printf("%d\n", (unsigned)b % 20);
621 printf("%d\n", a & 6);
622 printf("%d\n", a ^ 7);
623 printf("%d\n", a | 8);
624 printf("%d\n", a >> 3);
625 printf("%d\n", b >> 3);
626 printf("%d\n", (unsigned)b >> 3);
627 printf("%d\n", a << 4);
628 printf("%d\n", ~a);
629 printf("%d\n", -a);
630 printf("%d\n", +a);
632 printf("%d\n", 12 + 1);
633 printf("%d\n", 12 - 2);
634 printf("%d\n", 12 * 312);
635 printf("%d\n", 12 / 4);
636 printf("%d\n", 12 % 20);
637 printf("%d\n", 12 & 6);
638 printf("%d\n", 12 ^ 7);
639 printf("%d\n", 12 | 8);
640 printf("%d\n", 12 >> 2);
641 printf("%d\n", 12 << 4);
642 printf("%d\n", ~12);
643 printf("%d\n", -12);
644 printf("%d\n", +12);
645 printf("%d %d %d %d\n",
646 isid('a'),
647 isid('g'),
648 isid('T'),
649 isid('('));
652 int isid(int c)
654 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
657 /**********************/
659 int vstack[10], *vstack_ptr;
661 void vpush(int vt, int vc)
663 *vstack_ptr++ = vt;
664 *vstack_ptr++ = vc;
667 void vpop(int *ft, int *fc)
669 *fc = *--vstack_ptr;
670 *ft = *--vstack_ptr;
673 void expr2_test()
675 int a, b;
677 printf("expr2:\n");
678 vstack_ptr = vstack;
679 vpush(1432432, 2);
680 vstack_ptr[-2] &= ~0xffffff80;
681 vpop(&a, &b);
682 printf("res= %d %d\n", a, b);
685 void constant_expr_test()
687 int a;
688 printf("constant_expr:\n");
689 a = 3;
690 printf("%d\n", a * 16);
691 printf("%d\n", a * 1);
692 printf("%d\n", a + 0);
695 int tab4[10];
697 void expr_ptr_test()
699 int *p, *q;
700 int i = -1;
702 printf("expr_ptr:\n");
703 p = tab4;
704 q = tab4 + 10;
705 printf("diff=%d\n", q - p);
706 p++;
707 printf("inc=%d\n", p - tab4);
708 p--;
709 printf("dec=%d\n", p - tab4);
710 ++p;
711 printf("inc=%d\n", p - tab4);
712 --p;
713 printf("dec=%d\n", p - tab4);
714 printf("add=%d\n", p + 3 - tab4);
715 printf("add=%d\n", 3 + p - tab4);
717 /* check if 64bit support is ok */
718 q = p = 0;
719 q += i;
720 printf("%p %p %ld\n", q, p, p-q);
721 printf("%d %d %d %d %d %d\n",
722 p == q, p != q, p < q, p <= q, p >= q, p > q);
723 i = 0xf0000000;
724 p += i;
725 printf("%p %p %ld\n", q, p, p-q);
726 printf("%d %d %d %d %d %d\n",
727 p == q, p != q, p < q, p <= q, p >= q, p > q);
728 p = (int *)((char *)p + 0xf0000000);
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 += 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);
737 struct size12 {
738 int i, j, k;
740 struct size12 s[2], *sp = s;
741 int i, j;
742 sp->i = 42;
743 sp++;
744 j = -1;
745 printf("%d\n", sp[j].i);
749 void expr_cmp_test()
751 int a, b;
752 printf("constant_expr:\n");
753 a = -1;
754 b = 1;
755 printf("%d\n", a == a);
756 printf("%d\n", a != a);
758 printf("%d\n", a < b);
759 printf("%d\n", a <= b);
760 printf("%d\n", a <= a);
761 printf("%d\n", b >= a);
762 printf("%d\n", a >= a);
763 printf("%d\n", b > a);
765 printf("%d\n", (unsigned)a < b);
766 printf("%d\n", (unsigned)a <= b);
767 printf("%d\n", (unsigned)a <= a);
768 printf("%d\n", (unsigned)b >= a);
769 printf("%d\n", (unsigned)a >= a);
770 printf("%d\n", (unsigned)b > a);
773 struct empty {
776 struct aligntest1 {
777 char a[10];
780 struct aligntest2 {
781 int a;
782 char b[10];
785 struct aligntest3 {
786 double a, b;
789 struct aligntest4 {
790 double a[0];
793 void struct_test()
795 struct1 *s;
796 union union2 u;
798 printf("struct:\n");
799 printf("sizes: %d %d %d %d\n",
800 sizeof(struct struct1),
801 sizeof(struct struct2),
802 sizeof(union union1),
803 sizeof(union union2));
804 st1.f1 = 1;
805 st1.f2 = 2;
806 st1.f3 = 3;
807 printf("st1: %d %d %d\n",
808 st1.f1, st1.f2, st1.f3);
809 st1.u.v1 = 1;
810 st1.u.v2 = 2;
811 printf("union1: %d\n", st1.u.v1);
812 u.w1 = 1;
813 u.w2 = 2;
814 printf("union2: %d\n", u.w1);
815 s = &st2;
816 s->f1 = 3;
817 s->f2 = 2;
818 s->f3 = 1;
819 printf("st2: %d %d %d\n",
820 s->f1, s->f2, s->f3);
821 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
823 /* align / size tests */
824 printf("aligntest1 sizeof=%d alignof=%d\n",
825 sizeof(struct aligntest1), __alignof__(struct aligntest1));
826 printf("aligntest2 sizeof=%d alignof=%d\n",
827 sizeof(struct aligntest2), __alignof__(struct aligntest2));
828 printf("aligntest3 sizeof=%d alignof=%d\n",
829 sizeof(struct aligntest3), __alignof__(struct aligntest3));
830 printf("aligntest4 sizeof=%d alignof=%d\n",
831 sizeof(struct aligntest4), __alignof__(struct aligntest4));
833 /* empty structures (GCC extension) */
834 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
835 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
838 /* XXX: depend on endianness */
839 void char_short_test()
841 int var1, var2;
843 printf("char_short:\n");
845 var1 = 0x01020304;
846 var2 = 0xfffefdfc;
847 printf("s8=%d %d\n",
848 *(char *)&var1, *(char *)&var2);
849 printf("u8=%d %d\n",
850 *(unsigned char *)&var1, *(unsigned char *)&var2);
851 printf("s16=%d %d\n",
852 *(short *)&var1, *(short *)&var2);
853 printf("u16=%d %d\n",
854 *(unsigned short *)&var1, *(unsigned short *)&var2);
855 printf("s32=%d %d\n",
856 *(int *)&var1, *(int *)&var2);
857 printf("u32=%d %d\n",
858 *(unsigned int *)&var1, *(unsigned int *)&var2);
859 *(char *)&var1 = 0x08;
860 printf("var1=%x\n", var1);
861 *(short *)&var1 = 0x0809;
862 printf("var1=%x\n", var1);
863 *(int *)&var1 = 0x08090a0b;
864 printf("var1=%x\n", var1);
867 /******************/
869 typedef struct Sym {
870 int v;
871 int t;
872 int c;
873 struct Sym *next;
874 struct Sym *prev;
875 } Sym;
877 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
878 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
880 static int toupper1(int a)
882 return TOUPPER(a);
885 void bool_test()
887 int *s, a, b, t, f, i;
889 a = 0;
890 s = (void*)0;
891 printf("!s=%d\n", !s);
893 if (!s || !s[0])
894 a = 1;
895 printf("a=%d\n", a);
897 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
898 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
899 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
900 #if 1 && 1
901 printf("a1\n");
902 #endif
903 #if 1 || 0
904 printf("a2\n");
905 #endif
906 #if 1 ? 0 : 1
907 printf("a3\n");
908 #endif
909 #if 0 ? 0 : 1
910 printf("a4\n");
911 #endif
913 a = 4;
914 printf("b=%d\n", a + (0 ? 1 : a / 2));
916 /* test register spilling */
917 a = 10;
918 b = 10;
919 a = (a + b) * ((a < b) ?
920 ((b - a) * (a - b)): a + b);
921 printf("a=%d\n", a);
923 /* test complex || or && expressions */
924 t = 1;
925 f = 0;
926 a = 32;
927 printf("exp=%d\n", f == (32 <= a && a <= 3));
928 printf("r=%d\n", (t || f) + (t && f));
930 /* test ? : cast */
932 int aspect_on;
933 int aspect_native = 65536;
934 double bfu_aspect = 1.0;
935 int aspect;
936 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
937 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
938 printf("aspect=%d\n", aspect);
942 /* test ? : GCC extension */
944 static int v1 = 34 ? : -1; /* constant case */
945 static int v2 = 0 ? : -1; /* constant case */
946 int a = 30;
948 printf("%d %d\n", v1, v2);
949 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
952 /* again complex expression */
953 for(i=0;i<256;i++) {
954 if (toupper1 (i) != TOUPPER (i))
955 printf("error %d\n", i);
959 /* GCC accepts that */
960 static int tab_reinit[];
961 static int tab_reinit[10];
963 //int cinit1; /* a global variable can be defined several times without error ! */
964 int cinit1;
965 int cinit1;
966 int cinit1 = 0;
967 int *cinit2 = (int []){3, 2, 1};
969 void compound_literal_test(void)
971 int *p, i;
972 char *q, *q3;
974 printf("compound_test:\n");
976 p = (int []){1, 2, 3};
977 for(i=0;i<3;i++)
978 printf(" %d", p[i]);
979 printf("\n");
981 for(i=0;i<3;i++)
982 printf("%d", cinit2[i]);
983 printf("\n");
985 q = "tralala1";
986 printf("q1=%s\n", q);
988 q = (char *){ "tralala2" };
989 printf("q2=%s\n", q);
991 q3 = (char *){ q };
992 printf("q3=%s\n", q3);
994 q = (char []){ "tralala3" };
995 printf("q4=%s\n", q);
997 #ifdef ALL_ISOC99
998 p = (int []){1, 2, cinit1 + 3};
999 for(i=0;i<3;i++)
1000 printf(" %d", p[i]);
1001 printf("\n");
1003 for(i=0;i<3;i++) {
1004 p = (int []){1, 2, 4 + i};
1005 printf("%d %d %d\n",
1006 p[0],
1007 p[1],
1008 p[2]);
1010 #endif
1013 /* K & R protos */
1015 kr_func1(a, b)
1017 return a + b;
1020 int kr_func2(a, b)
1022 return a + b;
1025 kr_test()
1027 printf("kr_test:\n");
1028 printf("func1=%d\n", kr_func1(3, 4));
1029 printf("func2=%d\n", kr_func2(3, 4));
1030 return 0;
1033 void num(int n)
1035 char *tab, *p;
1036 tab = (char*)malloc(20);
1037 p = tab;
1038 while (1) {
1039 *p = 48 + (n % 10);
1040 p++;
1041 n = n / 10;
1042 if (n == 0)
1043 break;
1045 while (p != tab) {
1046 p--;
1047 printf("%c", *p);
1049 printf("\n");
1052 /* structure assignment tests */
1053 struct structa1 {
1054 int f1;
1055 char f2;
1058 struct structa1 ssta1;
1060 void struct_assign_test1(struct structa1 s1, int t)
1062 printf("%d %d %d\n", s1.f1, s1.f2, t);
1065 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1067 s1.f1 += t;
1068 s1.f2 -= t;
1069 return s1;
1072 void struct_assign_test(void)
1074 struct structa1 lsta1, lsta2;
1076 #if 0
1077 printf("struct_assign_test:\n");
1079 lsta1.f1 = 1;
1080 lsta1.f2 = 2;
1081 printf("%d %d\n", lsta1.f1, lsta1.f2);
1082 lsta2 = lsta1;
1083 printf("%d %d\n", lsta2.f1, lsta2.f2);
1084 #else
1085 lsta2.f1 = 1;
1086 lsta2.f2 = 2;
1087 #endif
1088 struct_assign_test1(lsta2, 3);
1090 printf("before call: %d %d\n", lsta2.f1, lsta2.f2);
1091 lsta2 = struct_assign_test2(lsta2, 4);
1092 printf("after call: %d %d\n", lsta2.f1, lsta2.f2);
1095 /* casts to short/char */
1097 void cast1(char a, short b, unsigned char c, unsigned short d)
1099 printf("%d %d %d %d\n", a, b, c, d);
1102 char bcast;
1103 short scast;
1105 void cast_test()
1107 int a;
1108 char c;
1109 char tab[10];
1110 unsigned b,d;
1111 short s;
1113 printf("cast_test:\n");
1114 a = 0xfffff;
1115 cast1(a, a, a, a);
1116 a = 0xffffe;
1117 printf("%d %d %d %d\n",
1118 (char)(a + 1),
1119 (short)(a + 1),
1120 (unsigned char)(a + 1),
1121 (unsigned short)(a + 1));
1122 printf("%d %d %d %d\n",
1123 (char)0xfffff,
1124 (short)0xfffff,
1125 (unsigned char)0xfffff,
1126 (unsigned short)0xfffff);
1128 a = (bcast = 128) + 1;
1129 printf("%d\n", a);
1130 a = (scast = 65536) + 1;
1131 printf("%d\n", a);
1133 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1135 /* test cast from unsigned to signed short to int */
1136 b = 0xf000;
1137 d = (short)b;
1138 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1139 b = 0xf0f0;
1140 d = (char)b;
1141 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1143 /* test implicit int casting for array accesses */
1144 c = 0;
1145 tab[1] = 2;
1146 tab[c] = 1;
1147 printf("%d %d\n", tab[0], tab[1]);
1149 /* test implicit casting on some operators */
1150 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1151 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1152 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1155 /* initializers tests */
1156 struct structinit1 {
1157 int f1;
1158 char f2;
1159 short f3;
1160 int farray[3];
1163 int sinit1 = 2;
1164 int sinit2 = { 3 };
1165 int sinit3[3] = { 1, 2, {{3}}, };
1166 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1167 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1168 int sinit6[] = { 1, 2, 3 };
1169 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1170 char sinit8[] = "hello" "trala";
1172 struct structinit1 sinit9 = { 1, 2, 3 };
1173 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1174 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1175 #ifdef ALL_ISOC99
1176 .farray[0] = 10,
1177 .farray[1] = 11,
1178 .farray[2] = 12,
1179 #endif
1182 char *sinit12 = "hello world";
1183 char *sinit13[] = {
1184 "test1",
1185 "test2",
1186 "test3",
1188 char sinit14[10] = { "abc" };
1189 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1191 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1193 struct bar {
1194 char *s;
1195 int len;
1196 } sinit17[] = {
1197 "a1", 4,
1198 "a2", 1
1201 int sinit18[10] = {
1202 [2 ... 5] = 20,
1204 [8] = 10,
1207 void init_test(void)
1209 int linit1 = 2;
1210 int linit2 = { 3 };
1211 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1212 int linit6[] = { 1, 2, 3 };
1213 int i, j;
1214 char linit8[] = "hello" "trala";
1215 int linit12[10] = { 1, 2 };
1216 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1217 char linit14[10] = "abc";
1218 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1219 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1220 int linit17 = sizeof(linit17);
1222 printf("init_test:\n");
1224 printf("sinit1=%d\n", sinit1);
1225 printf("sinit2=%d\n", sinit2);
1226 printf("sinit3=%d %d %d %d\n",
1227 sizeof(sinit3),
1228 sinit3[0],
1229 sinit3[1],
1230 sinit3[2]
1232 printf("sinit6=%d\n", sizeof(sinit6));
1233 printf("sinit7=%d %d %d %d\n",
1234 sizeof(sinit7),
1235 sinit7[0],
1236 sinit7[1],
1237 sinit7[2]
1239 printf("sinit8=%s\n", sinit8);
1240 printf("sinit9=%d %d %d\n",
1241 sinit9.f1,
1242 sinit9.f2,
1243 sinit9.f3
1245 printf("sinit10=%d %d %d\n",
1246 sinit10.f1,
1247 sinit10.f2,
1248 sinit10.f3
1250 printf("sinit11=%d %d %d %d %d %d\n",
1251 sinit11.f1,
1252 sinit11.f2,
1253 sinit11.f3,
1254 sinit11.farray[0],
1255 sinit11.farray[1],
1256 sinit11.farray[2]
1259 for(i=0;i<3;i++)
1260 for(j=0;j<2;j++)
1261 printf("[%d][%d] = %d %d %d\n",
1262 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1263 printf("linit1=%d\n", linit1);
1264 printf("linit2=%d\n", linit2);
1265 printf("linit6=%d\n", sizeof(linit6));
1266 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1268 printf("sinit12=%s\n", sinit12);
1269 printf("sinit13=%d %s %s %s\n",
1270 sizeof(sinit13),
1271 sinit13[0],
1272 sinit13[1],
1273 sinit13[2]);
1274 printf("sinit14=%s\n", sinit14);
1276 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1277 printf("\n");
1278 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1279 printf("\n");
1280 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1281 printf("\n");
1282 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1283 printf("\n");
1284 printf("%d %d %d %d\n",
1285 linit16.a1,
1286 linit16.a2,
1287 linit16.a3,
1288 linit16.a4);
1289 /* test that initialisation is done after variable declare */
1290 printf("linit17=%d\n", linit17);
1291 printf("sinit15=%d\n", sinit15[0]);
1292 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1293 printf("sinit17=%s %d %s %d\n",
1294 sinit17[0].s, sinit17[0].len,
1295 sinit17[1].s, sinit17[1].len);
1296 for(i=0;i<10;i++)
1297 printf("%x ", sinit18[i]);
1298 printf("\n");
1302 void switch_test()
1304 int i;
1306 for(i=0;i<15;i++) {
1307 switch(i) {
1308 case 0:
1309 case 1:
1310 printf("a");
1311 break;
1312 default:
1313 printf("%d", i);
1314 break;
1315 case 8 ... 12:
1316 printf("c");
1317 break;
1318 case 3:
1319 printf("b");
1320 break;
1323 printf("\n");
1326 /* ISOC99 _Bool type */
1327 void c99_bool_test(void)
1329 #ifdef BOOL_ISOC99
1330 int a;
1331 _Bool b;
1333 printf("bool_test:\n");
1334 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1335 a = 3;
1336 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1337 b = 3;
1338 printf("b = %d\n", b);
1339 b++;
1340 printf("b = %d\n", b);
1341 #endif
1344 void bitfield_test(void)
1346 int a;
1347 struct sbf1 {
1348 int f1 : 3;
1349 int : 2;
1350 int f2 : 1;
1351 int : 0;
1352 int f3 : 5;
1353 int f4 : 7;
1354 unsigned int f5 : 7;
1355 } st1;
1356 printf("bitfield_test:");
1357 printf("sizeof(st1) = %d\n", sizeof(st1));
1359 st1.f1 = 3;
1360 st1.f2 = 1;
1361 st1.f3 = 15;
1362 a = 120;
1363 st1.f4 = a;
1364 st1.f5 = a;
1365 st1.f5++;
1366 printf("%d %d %d %d %d\n",
1367 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1369 st1.f1 = 7;
1370 if (st1.f1 == -1)
1371 printf("st1.f1 == -1\n");
1372 else
1373 printf("st1.f1 != -1\n");
1374 if (st1.f2 == -1)
1375 printf("st1.f2 == -1\n");
1376 else
1377 printf("st1.f2 != -1\n");
1379 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1380 long-long bitfields whose size is not bigger than int */
1381 struct sbf2 {
1382 long long f1 : 45;
1383 long long : 2;
1384 long long f2 : 35;
1385 unsigned long long f3 : 38;
1386 } st2;
1387 st2.f1 = 0x123456789ULL;
1388 a = 120;
1389 st2.f2 = (long long)a << 25;
1390 st2.f3 = a;
1391 st2.f2++;
1392 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
1395 #ifdef __x86_64__
1396 #define FLOAT_FMT "%f\n"
1397 #else
1398 /* x86's float isn't compatible with GCC */
1399 #define FLOAT_FMT "%.5f\n"
1400 #endif
1402 #define FTEST(prefix, type, fmt)\
1403 void prefix ## cmp(type a, type b)\
1405 printf("%d %d %d %d %d %d\n",\
1406 a == b,\
1407 a != b,\
1408 a < b,\
1409 a > b,\
1410 a >= b,\
1411 a <= b);\
1412 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1415 a + b,\
1416 a - b,\
1417 a * b,\
1418 a / b,\
1419 -a);\
1420 printf(fmt "\n", ++a);\
1421 printf(fmt "\n", a++);\
1422 printf(fmt "\n", a);\
1424 void prefix ## fcast(type a)\
1426 float fa;\
1427 double da;\
1428 long double la;\
1429 int ia;\
1430 unsigned int ua;\
1431 type b;\
1432 fa = a;\
1433 da = a;\
1434 la = a;\
1435 printf("ftof: %f %f %Lf\n", fa, da, la);\
1436 ia = (int)a;\
1437 ua = (unsigned int)a;\
1438 printf("ftoi: %d %u\n", ia, ua);\
1439 ia = -1234;\
1440 ua = 0x81234500;\
1441 b = ia;\
1442 printf("itof: " fmt "\n", b);\
1443 b = ua;\
1444 printf("utof: " fmt "\n", b);\
1447 float prefix ## retf(type a) { return a; }\
1448 double prefix ## retd(type a) { return a; }\
1449 long double prefix ## retld(type a) { return a; }\
1451 void prefix ## call(void)\
1453 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1454 printf("double: %f\n", prefix ## retd(42.123456789));\
1455 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1458 void prefix ## test(void)\
1460 printf("testing '%s'\n", #type);\
1461 prefix ## cmp(1, 2.5);\
1462 prefix ## cmp(2, 1.5);\
1463 prefix ## cmp(1, 1);\
1464 prefix ## fcast(234.6);\
1465 prefix ## fcast(-2334.6);\
1466 prefix ## call();\
1469 FTEST(f, float, "%f")
1470 FTEST(d, double, "%f")
1471 FTEST(ld, long double, "%Lf")
1473 double ftab1[3] = { 1.2, 3.4, -5.6 };
1476 void float_test(void)
1478 float fa, fb;
1479 double da, db;
1480 int a;
1481 unsigned int b;
1483 printf("float_test:\n");
1484 printf("sizeof(float) = %d\n", sizeof(float));
1485 printf("sizeof(double) = %d\n", sizeof(double));
1486 printf("sizeof(long double) = %d\n", sizeof(long double));
1487 ftest();
1488 dtest();
1489 ldtest();
1490 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1491 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1492 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1493 da = 123;
1494 printf("da=%f\n", da);
1495 fa = 123;
1496 printf("fa=%f\n", fa);
1497 a = 4000000000;
1498 da = a;
1499 printf("da = %f\n", da);
1500 b = 4000000000;
1501 db = b;
1502 printf("db = %f\n", db);
1505 int fib(int n)
1507 if (n <= 2)
1508 return 1;
1509 else
1510 return fib(n-1) + fib(n-2);
1513 void funcptr_test()
1515 void (*func)(int);
1516 int a;
1517 struct {
1518 int dummy;
1519 void (*func)(int);
1520 } st1;
1522 printf("funcptr:\n");
1523 func = &num;
1524 (*func)(12345);
1525 func = num;
1526 a = 1;
1527 a = 1;
1528 func(12345);
1529 /* more complicated pointer computation */
1530 st1.func = num;
1531 st1.func(12346);
1532 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1533 printf("sizeof2 = %d\n", sizeof funcptr_test);
1534 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1535 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1538 void lloptest(long long a, long long b)
1540 unsigned long long ua, ub;
1542 ua = a;
1543 ub = b;
1544 /* arith */
1545 printf("arith: %Ld %Ld %Ld\n",
1546 a + b,
1547 a - b,
1548 a * b);
1550 if (b != 0) {
1551 printf("arith1: %Ld %Ld\n",
1552 a / b,
1553 a % b);
1556 /* binary */
1557 printf("bin: %Ld %Ld %Ld\n",
1558 a & b,
1559 a | b,
1560 a ^ b);
1562 /* tests */
1563 printf("test: %d %d %d %d %d %d\n",
1564 a == b,
1565 a != b,
1566 a < b,
1567 a > b,
1568 a >= b,
1569 a <= b);
1571 printf("utest: %d %d %d %d %d %d\n",
1572 ua == ub,
1573 ua != ub,
1574 ua < ub,
1575 ua > ub,
1576 ua >= ub,
1577 ua <= ub);
1579 /* arith2 */
1580 a++;
1581 b++;
1582 printf("arith2: %Ld %Ld\n", a, b);
1583 printf("arith2: %Ld %Ld\n", a++, b++);
1584 printf("arith2: %Ld %Ld\n", --a, --b);
1585 printf("arith2: %Ld %Ld\n", a, b);
1588 void llshift(long long a, int b)
1590 printf("shift: %Ld %Ld %Ld\n",
1591 (unsigned long long)a >> b,
1592 a >> b,
1593 a << b);
1594 printf("shiftc: %Ld %Ld %Ld\n",
1595 (unsigned long long)a >> 3,
1596 a >> 3,
1597 a << 3);
1598 printf("shiftc: %Ld %Ld %Ld\n",
1599 (unsigned long long)a >> 35,
1600 a >> 35,
1601 a << 35);
1604 void llfloat(void)
1606 float fa;
1607 double da;
1608 long double lda;
1609 long long la, lb, lc;
1610 unsigned long long ula, ulb, ulc;
1611 la = 0x12345678;
1612 ula = 0x72345678;
1613 la = (la << 20) | 0x12345;
1614 ula = ula << 33;
1615 printf("la=%Ld ula=%Lu\n", la, ula);
1617 fa = la;
1618 da = la;
1619 lda = la;
1620 printf("lltof: %f %f %Lf\n", fa, da, lda);
1622 la = fa;
1623 lb = da;
1624 lc = lda;
1625 printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
1627 fa = ula;
1628 da = ula;
1629 lda = ula;
1630 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1632 ula = fa;
1633 ulb = da;
1634 ulc = lda;
1635 printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
1638 long long llfunc1(int a)
1640 return a * 2;
1643 struct S {
1644 int id;
1645 char item;
1648 long long int value(struct S *v)
1650 return ((long long int)v->item);
1653 void longlong_test(void)
1655 long long a, b, c;
1656 int ia;
1657 unsigned int ua;
1658 printf("longlong_test:\n");
1659 printf("sizeof(long long) = %d\n", sizeof(long long));
1660 ia = -1;
1661 ua = -2;
1662 a = ia;
1663 b = ua;
1664 printf("%Ld %Ld\n", a, b);
1665 printf("%Ld %Ld %Ld %Lx\n",
1666 (long long)1,
1667 (long long)-2,
1668 1LL,
1669 0x1234567812345679);
1670 a = llfunc1(-3);
1671 printf("%Ld\n", a);
1673 lloptest(1000, 23);
1674 lloptest(0xff, 0x1234);
1675 b = 0x72345678 << 10;
1676 lloptest(-3, b);
1677 llshift(0x123, 5);
1678 llshift(-23, 5);
1679 b = 0x72345678LL << 10;
1680 llshift(b, 47);
1682 llfloat();
1683 #if 1
1684 b = 0x12345678;
1685 a = -1;
1686 c = a + b;
1687 printf("%Lx\n", c);
1688 #endif
1690 /* long long reg spill test */
1692 struct S a;
1694 a.item = 3;
1695 printf("%lld\n", value(&a));
1697 lloptest(0x80000000, 0);
1699 /* another long long spill test */
1701 long long *p, v;
1702 v = 1;
1703 p = &v;
1704 p[0]++;
1705 printf("%lld\n", *p);
1708 a = 68719476720LL;
1709 b = 4294967295LL;
1710 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
1712 printf("%Ld\n", 0x123456789LLU);
1715 void manyarg_test(void)
1717 long double ld = 1234567891234LL;
1718 printf("manyarg_test:\n");
1719 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
1720 1, 2, 3, 4, 5, 6, 7, 8,
1721 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
1722 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1723 "%Ld %Ld %f %f\n",
1724 1, 2, 3, 4, 5, 6, 7, 8,
1725 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1726 1234567891234LL, 987654321986LL,
1727 42.0, 43.0);
1728 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1729 "%Ld %Ld %f %f\n",
1730 ld, 1, 2, 3, 4, 5, 6, 7, 8,
1731 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1732 1234567891234LL, 987654321986LL,
1733 42.0, 43.0);
1734 /* XXX: known bug of x86-64 */
1735 #ifndef __x86_64__
1736 printf("%d %d %d %d %d %d %d %d %Lf\n",
1737 1, 2, 3, 4, 5, 6, 7, 8, ld);
1738 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1739 "%Ld %Ld %f %f %Lf\n",
1740 1, 2, 3, 4, 5, 6, 7, 8,
1741 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1742 1234567891234LL, 987654321986LL,
1743 42.0, 43.0, ld);
1744 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1745 "%Lf %Ld %Ld %f %f %Lf\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 ld, 1234567891234LL, 987654321986LL,
1749 42.0, 43.0, ld);
1750 #endif
1753 void vprintf1(const char *fmt, ...)
1755 va_list ap;
1756 const char *p;
1757 int c, i;
1758 double d;
1759 long long ll;
1760 long double ld;
1762 va_start(ap, fmt);
1764 p = fmt;
1765 for(;;) {
1766 c = *p;
1767 if (c == '\0')
1768 break;
1769 p++;
1770 if (c == '%') {
1771 c = *p;
1772 switch(c) {
1773 case '\0':
1774 goto the_end;
1775 case 'd':
1776 i = va_arg(ap, int);
1777 printf("%d", i);
1778 break;
1779 case 'f':
1780 d = va_arg(ap, double);
1781 printf("%f", d);
1782 break;
1783 case 'l':
1784 ll = va_arg(ap, long long);
1785 printf("%Ld", ll);
1786 break;
1787 case 'F':
1788 ld = va_arg(ap, long double);
1789 printf("%Lf", ld);
1790 break;
1792 p++;
1793 } else {
1794 putchar(c);
1797 the_end:
1798 va_end(ap);
1802 void stdarg_test(void)
1804 long double ld = 1234567891234LL;
1805 vprintf1("%d %d %d\n", 1, 2, 3);
1806 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1807 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1808 vprintf1("%F %F %F\n", 1.2L, 2.3L, 3.4L);
1809 #ifdef __x86_64__
1810 /* a bug of x86's TCC */
1811 vprintf1("%d %f %l %F %d %f %l %F\n",
1812 1, 1.2, 3L, 4.5L, 6, 7.8, 9L, 0.1L);
1813 #endif
1814 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
1815 1, 2, 3, 4, 5, 6, 7, 8,
1816 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
1817 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
1818 1, 2, 3, 4, 5, 6, 7, 8,
1819 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
1820 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1821 "%l %l %f %f\n",
1822 1, 2, 3, 4, 5, 6, 7, 8,
1823 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1824 1234567891234LL, 987654321986LL,
1825 42.0, 43.0);
1826 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1827 "%l %l %f %f\n",
1828 ld, 1, 2, 3, 4, 5, 6, 7, 8,
1829 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1830 1234567891234LL, 987654321986LL,
1831 42.0, 43.0);
1832 vprintf1("%d %d %d %d %d %d %d %d %F\n",
1833 1, 2, 3, 4, 5, 6, 7, 8, ld);
1834 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1835 "%l %l %f %f %F\n",
1836 1, 2, 3, 4, 5, 6, 7, 8,
1837 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1838 1234567891234LL, 987654321986LL,
1839 42.0, 43.0, ld);
1840 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1841 "%F %l %l %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 ld, 1234567891234LL, 987654321986LL,
1845 42.0, 43.0, ld);
1848 void whitespace_test(void)
1850 char *str;
1852 \f\v #if 1
1853 pri\
1854 ntf("whitspace:\n");\f\v
1855 #endif
1856 pf("N=%d\n", 2);
1858 #ifdef CORRECT_CR_HANDLING
1859 pri\
1860 ntf("aaa=%d\n", 3);
1861 #endif
1863 pri\
1865 ntf("min=%d\n", 4);
1867 #ifdef ACCEPT_CR_IN_STRINGS
1868 printf("len1=%d\n", strlen("
1869 "));
1870 #ifdef CORRECT_CR_HANDLING
1871 str = "
1873 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
1874 #endif
1875 printf("len1=%d\n", strlen(" a
1876 "));
1877 #endif /* ACCEPT_CR_IN_STRINGS */
1880 int reltab[3] = { 1, 2, 3 };
1882 int *rel1 = &reltab[1];
1883 int *rel2 = &reltab[2];
1885 void relocation_test(void)
1887 printf("*rel1=%d\n", *rel1);
1888 printf("*rel2=%d\n", *rel2);
1891 void old_style_f(a,b,c)
1892 int a, b;
1893 double c;
1895 printf("a=%d b=%d b=%f\n", a, b, c);
1898 void decl_func1(int cmpfn())
1900 printf("cmpfn=%lx\n", (long)cmpfn);
1903 void decl_func2(cmpfn)
1904 int cmpfn();
1906 printf("cmpfn=%lx\n", (long)cmpfn);
1909 void old_style_function(void)
1911 old_style_f((void *)1, 2, 3.0);
1912 decl_func1(NULL);
1913 decl_func2(NULL);
1916 void sizeof_test(void)
1918 int a;
1919 int **ptr;
1921 printf("sizeof(int) = %d\n", sizeof(int));
1922 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1923 printf("sizeof(long) = %d\n", sizeof(long));
1924 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
1925 printf("sizeof(short) = %d\n", sizeof(short));
1926 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1927 printf("sizeof(char) = %d\n", sizeof(char));
1928 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1929 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1930 a = 1;
1931 printf("sizeof(a++) = %d\n", sizeof a++);
1932 printf("a=%d\n", a);
1933 ptr = NULL;
1934 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
1936 /* some alignof tests */
1937 printf("__alignof__(int) = %d\n", __alignof__(int));
1938 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1939 printf("__alignof__(short) = %d\n", __alignof__(short));
1940 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1941 printf("__alignof__(char) = %d\n", __alignof__(char));
1942 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1943 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
1946 void typeof_test(void)
1948 double a;
1949 typeof(a) b;
1950 typeof(float) c;
1952 a = 1.5;
1953 b = 2.5;
1954 c = 3.5;
1955 printf("a=%f b=%f c=%f\n", a, b, c);
1958 void statement_expr_test(void)
1960 int a, i;
1962 a = 0;
1963 for(i=0;i<10;i++) {
1964 a += 1 +
1965 ( { int b, j;
1966 b = 0;
1967 for(j=0;j<5;j++)
1968 b += j; b;
1969 } );
1971 printf("a=%d\n", a);
1975 void local_label_test(void)
1977 int a;
1978 goto l1;
1980 a = 1 + ({
1981 __label__ l1, l2, l3, l4;
1982 goto l1;
1984 printf("aa1\n");
1985 goto l3;
1987 printf("aa3\n");
1988 goto l4;
1990 printf("aa2\n");
1991 goto l2;
1992 l3:;
1995 printf("a=%d\n", a);
1996 return;
1998 printf("bb1\n");
1999 goto l2;
2001 printf("bb2\n");
2002 goto l4;
2005 /* inline assembler test */
2006 #ifdef __i386__
2008 /* from linux kernel */
2009 static char * strncat1(char * dest,const char * src,size_t count)
2011 int d0, d1, d2, d3;
2012 __asm__ __volatile__(
2013 "repne\n\t"
2014 "scasb\n\t"
2015 "decl %1\n\t"
2016 "movl %8,%3\n"
2017 "1:\tdecl %3\n\t"
2018 "js 2f\n\t"
2019 "lodsb\n\t"
2020 "stosb\n\t"
2021 "testb %%al,%%al\n\t"
2022 "jne 1b\n"
2023 "2:\txorl %2,%2\n\t"
2024 "stosb"
2025 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2026 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2027 : "memory");
2028 return dest;
2031 static inline void * memcpy1(void * to, const void * from, size_t n)
2033 int d0, d1, d2;
2034 __asm__ __volatile__(
2035 "rep ; movsl\n\t"
2036 "testb $2,%b4\n\t"
2037 "je 1f\n\t"
2038 "movsw\n"
2039 "1:\ttestb $1,%b4\n\t"
2040 "je 2f\n\t"
2041 "movsb\n"
2042 "2:"
2043 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2044 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2045 : "memory");
2046 return (to);
2049 static __inline__ void sigaddset1(unsigned int *set, int _sig)
2051 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2054 static __inline__ void sigdelset1(unsigned int *set, int _sig)
2056 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2059 static __inline__ __const__ unsigned int swab32(unsigned int x)
2061 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2062 "rorl $16,%0\n\t" /* swap words */
2063 "xchgb %b0,%h0" /* swap higher bytes */
2064 :"=q" (x)
2065 : "0" (x));
2066 return x;
2069 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
2071 unsigned long long res;
2072 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
2073 return res;
2076 static __inline__ unsigned long long inc64(unsigned long long a)
2078 unsigned long long res;
2079 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
2080 return res;
2083 unsigned int set;
2085 void asm_test(void)
2087 char buf[128];
2088 unsigned int val;
2090 printf("inline asm:\n");
2091 /* test the no operand case */
2092 asm volatile ("xorl %eax, %eax");
2094 memcpy1(buf, "hello", 6);
2095 strncat1(buf, " worldXXXXX", 3);
2096 printf("%s\n", buf);
2098 /* 'A' constraint test */
2099 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2100 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2102 set = 0xff;
2103 sigdelset1(&set, 2);
2104 sigaddset1(&set, 16);
2105 /* NOTE: we test here if C labels are correctly restored after the
2106 asm statement */
2107 goto label1;
2108 label2:
2109 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
2110 printf("set=0x%x\n", set);
2111 val = 0x01020304;
2112 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
2113 return;
2114 label1:
2115 goto label2;
2118 #else
2120 void asm_test(void)
2124 #endif
2126 #define COMPAT_TYPE(type1, type2) \
2128 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2129 __builtin_types_compatible_p (type1, type2));\
2132 int constant_p_var;
2134 void builtin_test(void)
2136 #if GCC_MAJOR >= 3
2137 COMPAT_TYPE(int, int);
2138 COMPAT_TYPE(int, unsigned int);
2139 COMPAT_TYPE(int, char);
2140 COMPAT_TYPE(int, const int);
2141 COMPAT_TYPE(int, volatile int);
2142 COMPAT_TYPE(int *, int *);
2143 COMPAT_TYPE(int *, void *);
2144 COMPAT_TYPE(int *, const int *);
2145 COMPAT_TYPE(char *, unsigned char *);
2146 /* space is needed because tcc preprocessor introduces a space between each token */
2147 COMPAT_TYPE(char * *, void *);
2148 #endif
2149 printf("res = %d\n", __builtin_constant_p(1));
2150 printf("res = %d\n", __builtin_constant_p(1 + 2));
2151 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
2152 printf("res = %d\n", __builtin_constant_p(constant_p_var));
2156 void const_func(const int a)
2160 void const_warn_test(void)
2162 const_func(1);