added -funsigned-char, -fsigned-char and -Wimplicit-function-declaration
[tinycc.git] / tcctest.c
blob2b759168964eb5126d23e52b9bfd6b642d1e2baf
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 /* __VA_ARGS__ and __func__ support */
20 #define C99_MACROS
22 /* test various include syntaxes */
24 #define TCCLIB_INC <tcclib.h>
25 #define TCCLIB_INC1 <tcclib
26 #define TCCLIB_INC2 h>
27 #define TCCLIB_INC3 "tcclib"
29 #include TCCLIB_INC
31 #include TCCLIB_INC1.TCCLIB_INC2
33 #include TCCLIB_INC1.h>
35 /* gcc 3.2 does not accept that (bug ?) */
36 //#include TCCLIB_INC3 ".h"
38 #include <tcclib.h>
40 #include "tcclib.h"
42 void string_test();
43 void expr_test();
44 void macro_test();
45 void scope_test();
46 void forward_test();
47 void funcptr_test();
48 void loop_test();
49 void switch_test();
50 void goto_test();
51 void enum_test();
52 void typedef_test();
53 void struct_test();
54 void array_test();
55 void expr_ptr_test();
56 void bool_test();
57 void expr2_test();
58 void constant_expr_test();
59 void expr_cmp_test();
60 void char_short_test();
61 void init_test(void);
62 void compound_literal_test(void);
63 int kr_test();
64 void struct_assign_test(void);
65 void cast_test(void);
66 void bitfield_test(void);
67 void c99_bool_test(void);
68 void float_test(void);
69 void longlong_test(void);
70 void stdarg_test(void);
71 void whitespace_test(void);
72 void relocation_test(void);
73 void old_style_function(void);
74 void sizeof_test(void);
75 void typeof_test(void);
76 void local_label_test(void);
77 void statement_expr_test(void);
78 void asm_test(void);
79 void builtin_test(void);
81 int fib(int n);
82 void num(int n);
83 void forward_ref(void);
84 int isid(int c);
86 #define A 2
87 #define N 1234 + A
88 #define pf printf
89 #define M1(a, b) (a) + (b)
91 #define str\
92 (s) # s
93 #define glue(a, b) a ## b
94 #define xglue(a, b) glue(a, b)
95 #define HIGHLOW "hello"
96 #define LOW LOW ", world"
98 #define min(a, b) ((a) < (b) ? (a) : (b))
100 #ifdef C99_MACROS
101 #define dprintf(level,...) printf(__VA_ARGS__)
102 #endif
104 /* gcc vararg macros */
105 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
107 #define MACRO_NOARGS()
109 #define AAA 3
110 #undef AAA
111 #define AAA 4
113 #if 1
114 #define B3 1
115 #elif 1
116 #define B3 2
117 #elif 0
118 #define B3 3
119 #else
120 #define B3 4
121 #endif
123 #define __INT64_C(c) c ## LL
124 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
126 int qq(int x)
128 return x + 40;
130 #define qq(x) x
132 void macro_test(void)
134 printf("macro:\n");\f\v
135 pf("N=%d\n", N);
136 printf("aaa=%d\n", AAA);
138 printf("min=%d\n", min(1, min(2, -1)));
140 printf("s1=%s\n", glue(HIGH, LOW));
141 printf("s2=%s\n", xglue(HIGH, LOW));
142 printf("s3=%s\n", str("c"));
143 printf("s4=%s\n", str(a1));
144 printf("B3=%d\n", B3);
146 #ifdef A
147 printf("A defined\n");
148 #endif
149 #ifdef B
150 printf("B defined\n");
151 #endif
152 #ifdef A
153 printf("A defined\n");
154 #else
155 printf("A not defined\n");
156 #endif
157 #ifdef B
158 printf("B defined\n");
159 #else
160 printf("B not defined\n");
161 #endif
163 #ifdef A
164 printf("A defined\n");
165 #ifdef B
166 printf("B1 defined\n");
167 #else
168 printf("B1 not defined\n");
169 #endif
170 #else
171 printf("A not defined\n");
172 #ifdef B
173 printf("B2 defined\n");
174 #else
175 printf("B2 not defined\n");
176 #endif
177 #endif
179 #if 1+1
180 printf("test true1\n");
181 #endif
182 #if 0
183 printf("test true2\n");
184 #endif
185 #if 1-1
186 printf("test true3\n");
187 #endif
188 #if defined(A)
189 printf("test trueA\n");
190 #endif
191 #if defined(B)
192 printf("test trueB\n");
193 #endif
195 #if 0
196 printf("test 0\n");
197 #elif 0
198 printf("test 1\n");
199 #elif 2
200 printf("test 2\n");
201 #else
202 printf("test 3\n");
203 #endif
205 MACRO_NOARGS();
207 #ifdef __LINE__
208 printf("__LINE__ defined\n");
209 #endif
211 printf("__LINE__=%d __FILE__=%s\n",
212 __LINE__, __FILE__);
213 #line 200
214 printf("__LINE__=%d __FILE__=%s\n",
215 __LINE__, __FILE__);
216 #line 203 "test"
217 printf("__LINE__=%d __FILE__=%s\n",
218 __LINE__, __FILE__);
219 #line 220 "tcctest.c"
221 /* not strictly preprocessor, but we test it there */
222 #ifdef C99_MACROS
223 printf("__func__ = %s\n", __func__);
224 dprintf(1, "vaarg=%d\n", 1);
225 #endif
226 dprintf1(1, "vaarg1\n");
227 dprintf1(1, "vaarg1=%d\n", 2);
228 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
230 /* gcc extension */
231 printf("func='%s'\n", __FUNCTION__);
233 /* complicated macros in glibc */
234 printf("INT64_MIN=%Ld\n", INT64_MIN);
236 int a;
237 a = 1;
238 glue(a+, +);
239 printf("a=%d\n", a);
240 glue(a <, <= 2);
241 printf("a=%d\n", a);
244 /* macro function with argument outside the macro string */
245 #define MF_s MF_hello
246 #define MF_hello(msg) printf("%s\n",msg)
248 #define MF_t printf("tralala\n"); MF_hello
250 MF_s("hi");
251 MF_t("hi");
253 /* test macro substituion inside args (should not eat stream) */
254 printf("qq=%d\n", qq(qq)(2));
256 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
257 null argument without a space. gcc 3.2 fixes that. */
259 #define qq1(x) 1
260 printf("qq1=%d\n", qq1( ));
262 /* comment with stray handling *\
264 /* this is a valid *\/ comment */
265 /* this is a valid comment *\*/
266 // this is a valid\
267 comment
270 int op(a,b)
272 return a / b;
275 int ret(a)
277 if (a == 2)
278 return 1;
279 if (a == 3)
280 return 2;
281 return 0;
284 void ps(const char *s)
286 int c;
287 while (1) {
288 c = *s;
289 if (c == 0)
290 break;
291 printf("%c", c);
292 s++;
296 const char foo1_string[] = "\
297 bar\n\
298 test\14\
301 void string_test()
303 int b;
304 printf("string:\n");
305 printf("\141\1423\143\n");/* dezdez test */
306 printf("\x41\x42\x43\x3a\n");
307 printf("c=%c\n", 'r');
308 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
309 printf("foo1_string='%s'\n", foo1_string);
310 #if 0
311 printf("wstring=%S\n", L"abc");
312 printf("wstring=%S\n", L"abc" L"def" "ghi");
313 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
314 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
315 #endif
316 ps("test\n");
317 b = 32;
318 while ((b = b + 1) < 96) {
319 printf("%c", b);
321 printf("\n");
322 printf("fib=%d\n", fib(33));
323 b = 262144;
324 while (b != 0x80000000) {
325 num(b);
326 b = b * 2;
330 void loop_test()
332 int i;
333 i = 0;
334 while (i < 10)
335 printf("%d", i++);
336 printf("\n");
337 for(i = 0; i < 10;i++)
338 printf("%d", i);
339 printf("\n");
340 i = 0;
341 do {
342 printf("%d", i++);
343 } while (i < 10);
344 printf("\n");
346 /* break/continue tests */
347 i = 0;
348 while (1) {
349 if (i == 6)
350 break;
351 i++;
352 if (i == 3)
353 continue;
354 printf("%d", i);
356 printf("\n");
358 /* break/continue tests */
359 i = 0;
360 do {
361 if (i == 6)
362 break;
363 i++;
364 if (i == 3)
365 continue;
366 printf("%d", i);
367 } while(1);
368 printf("\n");
370 for(i = 0;i < 10;i++) {
371 if (i == 3)
372 continue;
373 printf("%d", i);
375 printf("\n");
379 void goto_test()
381 int i;
382 static void *label_table[3] = { &&label1, &&label2, &&label3 };
384 printf("goto:\n");
385 i = 0;
386 s_loop:
387 if (i >= 10)
388 goto s_end;
389 printf("%d", i);
390 i++;
391 goto s_loop;
392 s_end:
393 printf("\n");
395 /* we also test computed gotos (GCC extension) */
396 for(i=0;i<3;i++) {
397 goto *label_table[i];
398 label1:
399 printf("label1\n");
400 goto next;
401 label2:
402 printf("label2\n");
403 goto next;
404 label3:
405 printf("label3\n");
406 next: ;
410 enum {
412 E1 = 2,
413 E2 = 4,
418 enum test {
419 E5 = 1000,
422 void enum_test()
424 enum test b1;
425 printf("enum:\n%d %d %d %d %d %d\n",
426 E0, E1, E2, E3, E4, E5);
427 b1 = 1;
428 printf("b1=%d\n", b1);
431 typedef int *my_ptr;
433 void typedef_test()
435 my_ptr a;
436 int b;
437 a = &b;
438 *a = 1234;
439 printf("typedef:\n");
440 printf("a=%d\n", *a);
443 void forward_test()
445 printf("forward:\n");
446 forward_ref();
447 forward_ref();
451 void forward_ref(void)
453 printf("forward ok\n");
456 typedef struct struct1 {
457 int f1;
458 int f2, f3;
459 union union1 {
460 int v1;
461 int v2;
462 } u;
463 char str[3];
464 } struct1;
466 struct struct2 {
467 int a;
468 char b;
471 union union2 {
472 int w1;
473 int w2;
476 struct struct1 st1, st2;
478 int main(int argc, char **argv)
480 string_test();
481 expr_test();
482 macro_test();
483 scope_test();
484 forward_test();
485 funcptr_test();
486 loop_test();
487 switch_test();
488 goto_test();
489 enum_test();
490 typedef_test();
491 struct_test();
492 array_test();
493 expr_ptr_test();
494 bool_test();
495 expr2_test();
496 constant_expr_test();
497 expr_cmp_test();
498 char_short_test();
499 init_test();
500 compound_literal_test();
501 kr_test();
502 struct_assign_test();
503 cast_test();
504 bitfield_test();
505 c99_bool_test();
506 float_test();
507 longlong_test();
508 stdarg_test();
509 whitespace_test();
510 relocation_test();
511 old_style_function();
512 sizeof_test();
513 typeof_test();
514 statement_expr_test();
515 local_label_test();
516 asm_test();
517 builtin_test();
518 return 0;
521 int tab[3];
522 int tab2[3][2];
524 int g;
526 void f1(g)
528 printf("g1=%d\n", g);
531 void scope_test()
533 printf("scope:\n");
534 g = 2;
535 f1(1);
536 printf("g2=%d\n", g);
538 int g;
539 g = 3;
540 printf("g3=%d\n", g);
542 int g;
543 g = 4;
544 printf("g4=%d\n", g);
547 printf("g5=%d\n", g);
550 void array_test(int a[4])
552 int i, j;
554 printf("array:\n");
555 printf("sizeof(a) = %d\n", sizeof(a));
556 printf("sizeof(\"a\") = %d\n", sizeof("a"));
557 #ifdef C99_MACROS
558 printf("sizeof(__func__) = %d\n", sizeof(__func__));
559 #endif
560 printf("sizeof tab %d\n", sizeof(tab));
561 printf("sizeof tab2 %d\n", sizeof tab2);
562 tab[0] = 1;
563 tab[1] = 2;
564 tab[2] = 3;
565 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
566 for(i=0;i<3;i++)
567 for(j=0;j<2;j++)
568 tab2[i][j] = 10 * i + j;
569 for(i=0;i<3*2;i++) {
570 printf(" %3d", ((int *)tab2)[i]);
572 printf("\n");
575 void expr_test()
577 int a, b;
578 a = 0;
579 printf("%d\n", a += 1);
580 printf("%d\n", a -= 2);
581 printf("%d\n", a *= 31232132);
582 printf("%d\n", a /= 4);
583 printf("%d\n", a %= 20);
584 printf("%d\n", a &= 6);
585 printf("%d\n", a ^= 7);
586 printf("%d\n", a |= 8);
587 printf("%d\n", a >>= 3);
588 printf("%d\n", a <<= 4);
590 a = 22321;
591 b = -22321;
592 printf("%d\n", a + 1);
593 printf("%d\n", a - 2);
594 printf("%d\n", a * 312);
595 printf("%d\n", a / 4);
596 printf("%d\n", b / 4);
597 printf("%d\n", (unsigned)b / 4);
598 printf("%d\n", a % 20);
599 printf("%d\n", b % 20);
600 printf("%d\n", (unsigned)b % 20);
601 printf("%d\n", a & 6);
602 printf("%d\n", a ^ 7);
603 printf("%d\n", a | 8);
604 printf("%d\n", a >> 3);
605 printf("%d\n", b >> 3);
606 printf("%d\n", (unsigned)b >> 3);
607 printf("%d\n", a << 4);
608 printf("%d\n", ~a);
609 printf("%d\n", -a);
610 printf("%d\n", +a);
612 printf("%d\n", 12 + 1);
613 printf("%d\n", 12 - 2);
614 printf("%d\n", 12 * 312);
615 printf("%d\n", 12 / 4);
616 printf("%d\n", 12 % 20);
617 printf("%d\n", 12 & 6);
618 printf("%d\n", 12 ^ 7);
619 printf("%d\n", 12 | 8);
620 printf("%d\n", 12 >> 2);
621 printf("%d\n", 12 << 4);
622 printf("%d\n", ~12);
623 printf("%d\n", -12);
624 printf("%d\n", +12);
625 printf("%d %d %d %d\n",
626 isid('a'),
627 isid('g'),
628 isid('T'),
629 isid('('));
632 int isid(int c)
634 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
637 /**********************/
639 int vstack[10], *vstack_ptr;
641 void vpush(int vt, int vc)
643 *vstack_ptr++ = vt;
644 *vstack_ptr++ = vc;
647 void vpop(int *ft, int *fc)
649 *fc = *--vstack_ptr;
650 *ft = *--vstack_ptr;
653 void expr2_test()
655 int a, b;
657 printf("expr2:\n");
658 vstack_ptr = vstack;
659 vpush(1432432, 2);
660 vstack_ptr[-2] &= ~0xffffff80;
661 vpop(&a, &b);
662 printf("res= %d %d\n", a, b);
665 void constant_expr_test()
667 int a;
668 printf("constant_expr:\n");
669 a = 3;
670 printf("%d\n", a * 16);
671 printf("%d\n", a * 1);
672 printf("%d\n", a + 0);
675 int tab4[10];
677 void expr_ptr_test()
679 int *p, *q;
681 printf("expr_ptr:\n");
682 p = tab4;
683 q = tab4 + 10;
684 printf("diff=%d\n", q - p);
685 p++;
686 printf("inc=%d\n", p - tab4);
687 p--;
688 printf("dec=%d\n", p - tab4);
689 ++p;
690 printf("inc=%d\n", p - tab4);
691 --p;
692 printf("dec=%d\n", p - tab4);
693 printf("add=%d\n", p + 3 - tab4);
694 printf("add=%d\n", 3 + p - tab4);
697 void expr_cmp_test()
699 int a, b;
700 printf("constant_expr:\n");
701 a = -1;
702 b = 1;
703 printf("%d\n", a == a);
704 printf("%d\n", a != a);
706 printf("%d\n", a < b);
707 printf("%d\n", a <= b);
708 printf("%d\n", a <= a);
709 printf("%d\n", b >= a);
710 printf("%d\n", a >= a);
711 printf("%d\n", b > a);
713 printf("%d\n", (unsigned)a < b);
714 printf("%d\n", (unsigned)a <= b);
715 printf("%d\n", (unsigned)a <= a);
716 printf("%d\n", (unsigned)b >= a);
717 printf("%d\n", (unsigned)a >= a);
718 printf("%d\n", (unsigned)b > a);
721 struct empty {
724 struct aligntest1 {
725 char a[10];
728 struct aligntest2 {
729 int a;
730 char b[10];
733 struct aligntest3 {
734 double a, b;
737 struct aligntest4 {
738 double a[0];
741 void struct_test()
743 struct1 *s;
744 union union2 u;
746 printf("struct:\n");
747 printf("sizes: %d %d %d %d\n",
748 sizeof(struct struct1),
749 sizeof(struct struct2),
750 sizeof(union union1),
751 sizeof(union union2));
752 st1.f1 = 1;
753 st1.f2 = 2;
754 st1.f3 = 3;
755 printf("st1: %d %d %d\n",
756 st1.f1, st1.f2, st1.f3);
757 st1.u.v1 = 1;
758 st1.u.v2 = 2;
759 printf("union1: %d\n", st1.u.v1);
760 u.w1 = 1;
761 u.w2 = 2;
762 printf("union2: %d\n", u.w1);
763 s = &st2;
764 s->f1 = 3;
765 s->f2 = 2;
766 s->f3 = 1;
767 printf("st2: %d %d %d\n",
768 s->f1, s->f2, s->f3);
769 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
771 /* align / size tests */
772 printf("aligntest1 sizeof=%d alignof=%d\n",
773 sizeof(struct aligntest1), __alignof__(struct aligntest1));
774 printf("aligntest2 sizeof=%d alignof=%d\n",
775 sizeof(struct aligntest2), __alignof__(struct aligntest2));
776 printf("aligntest3 sizeof=%d alignof=%d\n",
777 sizeof(struct aligntest3), __alignof__(struct aligntest3));
778 printf("aligntest4 sizeof=%d alignof=%d\n",
779 sizeof(struct aligntest4), __alignof__(struct aligntest4));
781 /* empty structures (GCC extension) */
782 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
783 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
786 /* XXX: depend on endianness */
787 void char_short_test()
789 int var1, var2;
791 printf("char_short:\n");
793 var1 = 0x01020304;
794 var2 = 0xfffefdfc;
795 printf("s8=%d %d\n",
796 *(char *)&var1, *(char *)&var2);
797 printf("u8=%d %d\n",
798 *(unsigned char *)&var1, *(unsigned char *)&var2);
799 printf("s16=%d %d\n",
800 *(short *)&var1, *(short *)&var2);
801 printf("u16=%d %d\n",
802 *(unsigned short *)&var1, *(unsigned short *)&var2);
803 printf("s32=%d %d\n",
804 *(int *)&var1, *(int *)&var2);
805 printf("u32=%d %d\n",
806 *(unsigned int *)&var1, *(unsigned int *)&var2);
807 *(char *)&var1 = 0x08;
808 printf("var1=%x\n", var1);
809 *(short *)&var1 = 0x0809;
810 printf("var1=%x\n", var1);
811 *(int *)&var1 = 0x08090a0b;
812 printf("var1=%x\n", var1);
815 /******************/
817 typedef struct Sym {
818 int v;
819 int t;
820 int c;
821 struct Sym *next;
822 struct Sym *prev;
823 } Sym;
825 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
826 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
828 static int toupper1(int a)
830 return TOUPPER(a);
833 void bool_test()
835 int *s, a, b, t, f, i;
837 a = 0;
838 s = (void*)0;
839 printf("!s=%d\n", !s);
841 if (!s || !s[0])
842 a = 1;
843 printf("a=%d\n", a);
845 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
846 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
847 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
848 #if 1 && 1
849 printf("a1\n");
850 #endif
851 #if 1 || 0
852 printf("a2\n");
853 #endif
854 #if 1 ? 0 : 1
855 printf("a3\n");
856 #endif
857 #if 0 ? 0 : 1
858 printf("a4\n");
859 #endif
861 a = 4;
862 printf("b=%d\n", a + (0 ? 1 : a / 2));
864 /* test register spilling */
865 a = 10;
866 b = 10;
867 a = (a + b) * ((a < b) ?
868 ((b - a) * (a - b)): a + b);
869 printf("a=%d\n", a);
871 /* test complex || or && expressions */
872 t = 1;
873 f = 0;
874 a = 32;
875 printf("exp=%d\n", f == (32 <= a && a <= 3));
876 printf("r=%d\n", (t || f) + (t && f));
878 /* test ? : cast */
880 int aspect_on;
881 int aspect_native = 65536;
882 double bfu_aspect = 1.0;
883 int aspect;
884 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
885 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
886 printf("aspect=%d\n", aspect);
890 /* test ? : GCC extension */
892 static int v1 = 34 ? : -1; /* constant case */
893 static int v2 = 0 ? : -1; /* constant case */
894 int a = 30;
896 printf("%d %d\n", v1, v2);
897 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
900 /* again complex expression */
901 for(i=0;i<256;i++) {
902 if (toupper1 (i) != TOUPPER (i))
903 printf("error %d\n", i);
907 /* GCC accepts that */
908 static int tab_reinit[];
909 static int tab_reinit[10];
911 //int cinit1; /* a global variable can be defined several times without error ! */
912 int cinit1;
913 int cinit1;
914 int cinit1 = 0;
915 int *cinit2 = (int []){3, 2, 1};
917 void compound_literal_test(void)
919 int *p, i;
920 char *q, *q3;
922 printf("compound_test:\n");
924 p = (int []){1, 2, 3};
925 for(i=0;i<3;i++)
926 printf(" %d", p[i]);
927 printf("\n");
929 for(i=0;i<3;i++)
930 printf("%d", cinit2[i]);
931 printf("\n");
933 q = "tralala1";
934 printf("q1=%s\n", q);
936 q = (char *){ "tralala2" };
937 printf("q2=%s\n", q);
939 q3 = (char *){ q };
940 printf("q3=%s\n", q3);
942 q = (char []){ "tralala3" };
943 printf("q4=%s\n", q);
945 #ifdef ALL_ISOC99
946 p = (int []){1, 2, cinit1 + 3};
947 for(i=0;i<3;i++)
948 printf(" %d", p[i]);
949 printf("\n");
951 for(i=0;i<3;i++) {
952 p = (int []){1, 2, 4 + i};
953 printf("%d %d %d\n",
954 p[0],
955 p[1],
956 p[2]);
958 #endif
961 /* K & R protos */
963 kr_func1(a, b)
965 return a + b;
968 int kr_func2(a, b)
970 return a + b;
973 kr_test()
975 printf("kr_test:\n");
976 printf("func1=%d\n", kr_func1(3, 4));
977 printf("func2=%d\n", kr_func2(3, 4));
978 return 0;
981 void num(int n)
983 char *tab, *p;
984 tab = (char*)malloc(20);
985 p = tab;
986 while (1) {
987 *p = 48 + (n % 10);
988 p++;
989 n = n / 10;
990 if (n == 0)
991 break;
993 while (p != tab) {
994 p--;
995 printf("%c", *p);
997 printf("\n");
1000 /* structure assignment tests */
1001 struct structa1 {
1002 int f1;
1003 char f2;
1006 struct structa1 ssta1;
1008 void struct_assign_test1(struct structa1 s1, int t)
1010 printf("%d %d %d\n", s1.f1, s1.f2, t);
1013 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1015 s1.f1 += t;
1016 s1.f2 -= t;
1017 return s1;
1020 void struct_assign_test(void)
1022 struct structa1 lsta1, lsta2;
1024 #if 0
1025 printf("struct_assign_test:\n");
1027 lsta1.f1 = 1;
1028 lsta1.f2 = 2;
1029 printf("%d %d\n", lsta1.f1, lsta1.f2);
1030 lsta2 = lsta1;
1031 printf("%d %d\n", lsta2.f1, lsta2.f2);
1032 #else
1033 lsta2.f1 = 1;
1034 lsta2.f2 = 2;
1035 #endif
1036 struct_assign_test1(lsta2, 3);
1038 printf("before call: %d %d\n", lsta2.f1, lsta2.f2);
1039 lsta2 = struct_assign_test2(lsta2, 4);
1040 printf("after call: %d %d\n", lsta2.f1, lsta2.f2);
1043 /* casts to short/char */
1045 void cast1(char a, short b, unsigned char c, unsigned short d)
1047 printf("%d %d %d %d\n", a, b, c, d);
1050 char bcast;
1051 short scast;
1053 void cast_test()
1055 int a;
1056 char c;
1057 char tab[10];
1059 printf("cast_test:\n");
1060 a = 0xfffff;
1061 cast1(a, a, a, a);
1062 a = 0xffffe;
1063 printf("%d %d %d %d\n",
1064 (char)(a + 1),
1065 (short)(a + 1),
1066 (unsigned char)(a + 1),
1067 (unsigned short)(a + 1));
1068 printf("%d %d %d %d\n",
1069 (char)0xfffff,
1070 (short)0xfffff,
1071 (unsigned char)0xfffff,
1072 (unsigned short)0xfffff);
1074 a = (bcast = 128) + 1;
1075 printf("%d\n", a);
1076 a = (scast = 65536) + 1;
1077 printf("%d\n", a);
1079 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1081 /* test implicit int casting for array accesses */
1082 c = 0;
1083 tab[1] = 2;
1084 tab[c] = 1;
1085 printf("%d %d\n", tab[0], tab[1]);
1087 /* test implicit casting on some operators */
1088 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1089 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1090 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1093 /* initializers tests */
1094 struct structinit1 {
1095 int f1;
1096 char f2;
1097 short f3;
1098 int farray[3];
1101 int sinit1 = 2;
1102 int sinit2 = { 3 };
1103 int sinit3[3] = { 1, 2, {{3}}, };
1104 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1105 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1106 int sinit6[] = { 1, 2, 3 };
1107 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1108 char sinit8[] = "hello" "trala";
1110 struct structinit1 sinit9 = { 1, 2, 3 };
1111 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1112 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1113 #ifdef ALL_ISOC99
1114 .farray[0] = 10,
1115 .farray[1] = 11,
1116 .farray[2] = 12,
1117 #endif
1120 char *sinit12 = "hello world";
1121 char *sinit13[] = {
1122 "test1",
1123 "test2",
1124 "test3",
1126 char sinit14[10] = { "abc" };
1127 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1129 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1131 struct bar {
1132 char *s;
1133 int len;
1134 } sinit17[] = {
1135 "a1", 4,
1136 "a2", 1
1139 int sinit18[10] = {
1140 [2 ... 5] = 20,
1142 [8] = 10,
1145 void init_test(void)
1147 int linit1 = 2;
1148 int linit2 = { 3 };
1149 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1150 int linit6[] = { 1, 2, 3 };
1151 int i, j;
1152 char linit8[] = "hello" "trala";
1153 int linit12[10] = { 1, 2 };
1154 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1155 char linit14[10] = "abc";
1156 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1157 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1158 int linit17 = sizeof(linit17);
1160 printf("init_test:\n");
1162 printf("sinit1=%d\n", sinit1);
1163 printf("sinit2=%d\n", sinit2);
1164 printf("sinit3=%d %d %d %d\n",
1165 sizeof(sinit3),
1166 sinit3[0],
1167 sinit3[1],
1168 sinit3[2]
1170 printf("sinit6=%d\n", sizeof(sinit6));
1171 printf("sinit7=%d %d %d %d\n",
1172 sizeof(sinit7),
1173 sinit7[0],
1174 sinit7[1],
1175 sinit7[2]
1177 printf("sinit8=%s\n", sinit8);
1178 printf("sinit9=%d %d %d\n",
1179 sinit9.f1,
1180 sinit9.f2,
1181 sinit9.f3
1183 printf("sinit10=%d %d %d\n",
1184 sinit10.f1,
1185 sinit10.f2,
1186 sinit10.f3
1188 printf("sinit11=%d %d %d %d %d %d\n",
1189 sinit11.f1,
1190 sinit11.f2,
1191 sinit11.f3,
1192 sinit11.farray[0],
1193 sinit11.farray[1],
1194 sinit11.farray[2]
1197 for(i=0;i<3;i++)
1198 for(j=0;j<2;j++)
1199 printf("[%d][%d] = %d %d %d\n",
1200 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1201 printf("linit1=%d\n", linit1);
1202 printf("linit2=%d\n", linit2);
1203 printf("linit6=%d\n", sizeof(linit6));
1204 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1206 printf("sinit12=%s\n", sinit12);
1207 printf("sinit13=%d %s %s %s\n",
1208 sizeof(sinit13),
1209 sinit13[0],
1210 sinit13[1],
1211 sinit13[2]);
1212 printf("sinit14=%s\n", sinit14);
1214 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1215 printf("\n");
1216 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1217 printf("\n");
1218 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1219 printf("\n");
1220 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1221 printf("\n");
1222 printf("%d %d %d %d\n",
1223 linit16.a1,
1224 linit16.a2,
1225 linit16.a3,
1226 linit16.a4);
1227 /* test that initialisation is done after variable declare */
1228 printf("linit17=%d\n", linit17);
1229 printf("sinit15=%d\n", sinit15[0]);
1230 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1231 printf("sinit17=%s %d %s %d\n",
1232 sinit17[0].s, sinit17[0].len,
1233 sinit17[1].s, sinit17[1].len);
1234 for(i=0;i<10;i++)
1235 printf("%x ", sinit18[i]);
1236 printf("\n");
1240 void switch_test()
1242 int i;
1244 for(i=0;i<15;i++) {
1245 switch(i) {
1246 case 0:
1247 case 1:
1248 printf("a");
1249 break;
1250 default:
1251 printf("%d", i);
1252 break;
1253 case 8 ... 12:
1254 printf("c");
1255 break;
1256 case 3:
1257 printf("b");
1258 break;
1261 printf("\n");
1264 /* ISOC99 _Bool type */
1265 void c99_bool_test(void)
1267 #ifdef BOOL_ISOC99
1268 int a;
1269 _Bool b;
1271 printf("bool_test:\n");
1272 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1273 a = 3;
1274 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1275 b = 3;
1276 printf("b = %d\n", b);
1277 b++;
1278 printf("b = %d\n", b);
1279 #endif
1282 void bitfield_test(void)
1284 int a;
1285 struct sbf1 {
1286 int f1 : 3;
1287 int : 2;
1288 int f2 : 1;
1289 int : 0;
1290 int f3 : 5;
1291 int f4 : 7;
1292 unsigned int f5 : 7;
1293 } st1;
1294 printf("bitfield_test:");
1295 printf("sizeof(st1) = %d\n", sizeof(st1));
1297 st1.f1 = 3;
1298 st1.f2 = 1;
1299 st1.f3 = 15;
1300 a = 120;
1301 st1.f4 = a;
1302 st1.f5 = a;
1303 st1.f5++;
1304 printf("%d %d %d %d %d\n",
1305 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1308 #define FTEST(prefix, type, fmt)\
1309 void prefix ## cmp(type a, type b)\
1311 printf("%d %d %d %d %d %d\n",\
1312 a == b,\
1313 a != b,\
1314 a < b,\
1315 a > b,\
1316 a >= b,\
1317 a <= b);\
1318 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1321 a + b,\
1322 a - b,\
1323 a * b,\
1324 a / b,\
1325 -a);\
1326 printf(fmt "\n", ++a);\
1327 printf(fmt "\n", a++);\
1328 printf(fmt "\n", a);\
1330 void prefix ## fcast(type a)\
1332 float fa;\
1333 double da;\
1334 long double la;\
1335 int ia;\
1336 unsigned int ua;\
1337 type b;\
1338 fa = a;\
1339 da = a;\
1340 la = a;\
1341 printf("ftof: %f %f %Lf\n", fa, da, la);\
1342 ia = (int)a;\
1343 ua = (unsigned int)a;\
1344 printf("ftoi: %d %u\n", ia, ua);\
1345 ia = -1234;\
1346 ua = 0x81234500;\
1347 b = ia;\
1348 printf("itof: " fmt "\n", b);\
1349 b = ua;\
1350 printf("utof: " fmt "\n", b);\
1353 void prefix ## test(void)\
1355 printf("testing '%s'\n", #type);\
1356 prefix ## cmp(1, 2.5);\
1357 prefix ## cmp(2, 1.5);\
1358 prefix ## cmp(1, 1);\
1359 prefix ## fcast(234.6);\
1360 prefix ## fcast(-2334.6);\
1363 FTEST(f, float, "%f")
1364 FTEST(d, double, "%f")
1365 FTEST(ld, long double, "%Lf")
1367 double ftab1[3] = { 1.2, 3.4, -5.6 };
1370 void float_test(void)
1372 float fa, fb;
1373 double da, db;
1374 int a;
1375 unsigned int b;
1377 printf("float_test:\n");
1378 printf("sizeof(float) = %d\n", sizeof(float));
1379 printf("sizeof(double) = %d\n", sizeof(double));
1380 printf("sizeof(long double) = %d\n", sizeof(long double));
1381 ftest();
1382 dtest();
1383 ldtest();
1384 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1385 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1386 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1387 da = 123;
1388 printf("da=%f\n", da);
1389 fa = 123;
1390 printf("fa=%f\n", fa);
1391 a = 4000000000;
1392 da = a;
1393 printf("da = %f\n", da);
1394 b = 4000000000;
1395 db = b;
1396 printf("db = %f\n", db);
1399 int fib(int n)
1401 if (n <= 2)
1402 return 1;
1403 else
1404 return fib(n-1) + fib(n-2);
1407 void funcptr_test()
1409 void (*func)(int);
1410 int a;
1411 struct {
1412 int dummy;
1413 void (*func)(int);
1414 } st1;
1416 printf("funcptr:\n");
1417 func = &num;
1418 (*func)(12345);
1419 func = num;
1420 a = 1;
1421 a = 1;
1422 func(12345);
1423 /* more complicated pointer computation */
1424 st1.func = num;
1425 st1.func(12346);
1426 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1427 printf("sizeof2 = %d\n", sizeof funcptr_test);
1428 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1429 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1432 void lloptest(long long a, long long b)
1434 unsigned long long ua, ub;
1436 ua = a;
1437 ub = b;
1438 /* arith */
1439 printf("arith: %Ld %Ld %Ld\n",
1440 a + b,
1441 a - b,
1442 a * b);
1444 if (b != 0) {
1445 printf("arith1: %Ld %Ld\n",
1446 a / b,
1447 a % b);
1450 /* binary */
1451 printf("bin: %Ld %Ld %Ld\n",
1452 a & b,
1453 a | b,
1454 a ^ b);
1456 /* tests */
1457 printf("test: %d %d %d %d %d %d\n",
1458 a == b,
1459 a != b,
1460 a < b,
1461 a > b,
1462 a >= b,
1463 a <= b);
1465 printf("utest: %d %d %d %d %d %d\n",
1466 ua == ub,
1467 ua != ub,
1468 ua < ub,
1469 ua > ub,
1470 ua >= ub,
1471 ua <= ub);
1473 /* arith2 */
1474 a++;
1475 b++;
1476 printf("arith2: %Ld %Ld\n", a, b);
1477 printf("arith2: %Ld %Ld\n", a++, b++);
1478 printf("arith2: %Ld %Ld\n", --a, --b);
1479 printf("arith2: %Ld %Ld\n", a, b);
1482 void llshift(long long a, int b)
1484 printf("shift: %Ld %Ld %Ld\n",
1485 (unsigned long long)a >> b,
1486 a >> b,
1487 a << b);
1488 printf("shiftc: %Ld %Ld %Ld\n",
1489 (unsigned long long)a >> 3,
1490 a >> 3,
1491 a << 3);
1492 printf("shiftc: %Ld %Ld %Ld\n",
1493 (unsigned long long)a >> 35,
1494 a >> 35,
1495 a << 35);
1498 void llfloat(void)
1500 float fa;
1501 double da;
1502 long double lda;
1503 long long la, lb, lc;
1504 unsigned long long ula, ulb, ulc;
1505 la = 0x12345678;
1506 ula = 0x72345678;
1507 la = (la << 20) | 0x12345;
1508 ula = ula << 33;
1509 printf("la=%Ld ula=%Lu\n", la, ula);
1511 fa = la;
1512 da = la;
1513 lda = la;
1514 printf("lltof: %f %f %Lf\n", fa, da, lda);
1516 la = fa;
1517 lb = da;
1518 lc = lda;
1519 printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
1521 fa = ula;
1522 da = ula;
1523 lda = ula;
1524 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1526 ula = fa;
1527 ulb = da;
1528 ulc = lda;
1529 printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
1532 long long llfunc1(int a)
1534 return a * 2;
1537 struct S {
1538 int id;
1539 char item;
1542 long long int value(struct S *v)
1544 return ((long long int)v->item);
1547 void longlong_test(void)
1549 long long a, b, c;
1550 int ia;
1551 unsigned int ua;
1552 printf("longlong_test:\n");
1553 printf("sizeof(long long) = %d\n", sizeof(long long));
1554 ia = -1;
1555 ua = -2;
1556 a = ia;
1557 b = ua;
1558 printf("%Ld %Ld\n", a, b);
1559 printf("%Ld %Ld %Ld %Lx\n",
1560 (long long)1,
1561 (long long)-2,
1562 1LL,
1563 0x1234567812345679);
1564 a = llfunc1(-3);
1565 printf("%Ld\n", a);
1567 lloptest(1000, 23);
1568 lloptest(0xff, 0x1234);
1569 b = 0x72345678 << 10;
1570 lloptest(-3, b);
1571 llshift(0x123, 5);
1572 llshift(-23, 5);
1573 b = 0x72345678LL << 10;
1574 llshift(b, 47);
1576 llfloat();
1577 #if 1
1578 b = 0x12345678;
1579 a = -1;
1580 c = a + b;
1581 printf("%Lx\n", c);
1582 #endif
1584 /* long long reg spill test */
1586 struct S a;
1588 a.item = 3;
1589 printf("%lld\n", value(&a));
1591 lloptest(0x80000000, 0);
1594 void vprintf1(const char *fmt, ...)
1596 va_list ap;
1597 const char *p;
1598 int c, i;
1599 double d;
1600 long long ll;
1602 va_start(ap, fmt);
1604 p = fmt;
1605 for(;;) {
1606 c = *p;
1607 if (c == '\0')
1608 break;
1609 p++;
1610 if (c == '%') {
1611 c = *p;
1612 switch(c) {
1613 case '\0':
1614 goto the_end;
1615 case 'd':
1616 i = va_arg(ap, int);
1617 printf("%d", i);
1618 break;
1619 case 'f':
1620 d = va_arg(ap, double);
1621 printf("%f", d);
1622 break;
1623 case 'l':
1624 ll = va_arg(ap, long long);
1625 printf("%Ld", ll);
1626 break;
1628 p++;
1629 } else {
1630 putchar(c);
1633 the_end:
1634 va_end(ap);
1638 void stdarg_test(void)
1640 vprintf1("%d %d %d\n", 1, 2, 3);
1641 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1642 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1645 void whitespace_test(void)
1647 char *str;
1649 \f\v #if 1
1650 pri\
1651 ntf("whitspace:\n");\f\v
1652 #endif
1653 pf("N=%d\n", 2);
1655 #ifdef CORRECT_CR_HANDLING
1656 pri\
1657 ntf("aaa=%d\n", 3);
1658 #endif
1660 pri\
1662 ntf("min=%d\n", 4);
1663 printf("len1=%d\n", strlen("
1664 "));
1665 #ifdef CORRECT_CR_HANDLING
1666 str = "
1668 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
1669 #endif
1670 printf("len1=%d\n", strlen(" a
1671 "));
1674 int reltab[3] = { 1, 2, 3 };
1676 int *rel1 = &reltab[1];
1677 int *rel2 = &reltab[2];
1679 void relocation_test(void)
1681 printf("*rel1=%d\n", *rel1);
1682 printf("*rel2=%d\n", *rel2);
1685 void old_style_f(a,b,c)
1686 int a, b;
1687 double c;
1689 printf("a=%d b=%d b=%f\n", a, b, c);
1692 void decl_func1(int cmpfn())
1694 printf("cmpfn=%lx\n", (long)cmpfn);
1697 void decl_func2(cmpfn)
1698 int cmpfn();
1700 printf("cmpfn=%lx\n", (long)cmpfn);
1703 void old_style_function(void)
1705 old_style_f((void *)1, 2, 3.0);
1706 decl_func1(NULL);
1707 decl_func2(NULL);
1710 void sizeof_test(void)
1712 int a;
1713 int **ptr;
1715 printf("sizeof(int) = %d\n", sizeof(int));
1716 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1717 printf("sizeof(short) = %d\n", sizeof(short));
1718 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1719 printf("sizeof(char) = %d\n", sizeof(char));
1720 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1721 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1722 a = 1;
1723 printf("sizeof(a++) = %d\n", sizeof a++);
1724 printf("a=%d\n", a);
1725 ptr = NULL;
1726 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
1728 /* some alignof tests */
1729 printf("__alignof__(int) = %d\n", __alignof__(int));
1730 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1731 printf("__alignof__(short) = %d\n", __alignof__(short));
1732 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1733 printf("__alignof__(char) = %d\n", __alignof__(char));
1734 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1735 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
1738 void typeof_test(void)
1740 double a;
1741 typeof(a) b;
1742 typeof(float) c;
1744 a = 1.5;
1745 b = 2.5;
1746 c = 3.5;
1747 printf("a=%f b=%f c=%f\n", a, b, c);
1750 void statement_expr_test(void)
1752 int a, i;
1754 a = 0;
1755 for(i=0;i<10;i++) {
1756 a += 1 +
1757 ( { int b, j;
1758 b = 0;
1759 for(j=0;j<5;j++)
1760 b += j; b;
1761 } );
1763 printf("a=%d\n", a);
1767 void local_label_test(void)
1769 int a;
1770 goto l1;
1772 a = 1 + ({
1773 __label__ l1, l2, l3;
1774 goto l4;
1776 printf("aa1\n");
1777 goto l1;
1779 printf("aa3\n");
1780 goto l3;
1782 printf("aa2\n");
1783 goto l2;
1784 l3:;
1787 printf("a=%d\n", a);
1788 return;
1790 printf("bb1\n");
1791 goto l2;
1793 printf("bb2\n");
1794 goto l5;
1797 /* inline assembler test */
1798 #ifdef __i386__
1800 /* from linux kernel */
1801 static char * strncat1(char * dest,const char * src,size_t count)
1803 int d0, d1, d2, d3;
1804 __asm__ __volatile__(
1805 "repne\n\t"
1806 "scasb\n\t"
1807 "decl %1\n\t"
1808 "movl %8,%3\n"
1809 "1:\tdecl %3\n\t"
1810 "js 2f\n\t"
1811 "lodsb\n\t"
1812 "stosb\n\t"
1813 "testb %%al,%%al\n\t"
1814 "jne 1b\n"
1815 "2:\txorl %2,%2\n\t"
1816 "stosb"
1817 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
1818 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
1819 : "memory");
1820 return dest;
1823 static inline void * memcpy1(void * to, const void * from, size_t n)
1825 int d0, d1, d2;
1826 __asm__ __volatile__(
1827 "rep ; movsl\n\t"
1828 "testb $2,%b4\n\t"
1829 "je 1f\n\t"
1830 "movsw\n"
1831 "1:\ttestb $1,%b4\n\t"
1832 "je 2f\n\t"
1833 "movsb\n"
1834 "2:"
1835 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
1836 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
1837 : "memory");
1838 return (to);
1841 static __inline__ void sigaddset1(unsigned int *set, int _sig)
1843 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
1846 static __inline__ void sigdelset1(unsigned int *set, int _sig)
1848 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
1851 static __inline__ __const__ unsigned int swab32(unsigned int x)
1853 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
1854 "rorl $16,%0\n\t" /* swap words */
1855 "xchgb %b0,%h0" /* swap higher bytes */
1856 :"=q" (x)
1857 : "0" (x));
1858 return x;
1861 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
1863 unsigned long long res;
1864 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
1865 return res;
1868 static __inline__ unsigned long long inc64(unsigned long long a)
1870 unsigned long long res;
1871 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
1872 return res;
1875 unsigned int set;
1877 void asm_test(void)
1879 char buf[128];
1880 unsigned int val;
1882 printf("inline asm:\n");
1883 /* test the no operand case */
1884 asm volatile ("xorl %eax, %eax");
1886 memcpy1(buf, "hello", 6);
1887 strncat1(buf, " worldXXXXX", 3);
1888 printf("%s\n", buf);
1890 /* 'A' constraint test */
1891 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
1892 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
1894 set = 0xff;
1895 sigdelset1(&set, 2);
1896 sigaddset1(&set, 16);
1897 /* NOTE: we test here if C labels are correctly restored after the
1898 asm statement */
1899 goto label1;
1900 label2:
1901 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
1902 printf("set=0x%x\n", set);
1903 val = 0x01020304;
1904 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
1905 return;
1906 label1:
1907 goto label2;
1910 #else
1912 void asm_test(void)
1916 #endif
1918 #define COMPAT_TYPE(type1, type2) \
1920 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
1921 __builtin_types_compatible_p (type1, type2));\
1924 int constant_p_var;
1926 void builtin_test(void)
1928 #if GCC_MAJOR >= 3
1929 COMPAT_TYPE(int, int);
1930 COMPAT_TYPE(int, unsigned int);
1931 COMPAT_TYPE(int, char);
1932 COMPAT_TYPE(int, const int);
1933 COMPAT_TYPE(int, volatile int);
1934 COMPAT_TYPE(int *, int *);
1935 COMPAT_TYPE(int *, void *);
1936 COMPAT_TYPE(int *, const int *);
1937 COMPAT_TYPE(char *, unsigned char *);
1938 /* space is needed because tcc preprocessor introduces a space between each token */
1939 COMPAT_TYPE(char * *, void *);
1940 #endif
1941 printf("res = %d\n", __builtin_constant_p(1));
1942 printf("res = %d\n", __builtin_constant_p(1 + 2));
1943 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
1944 printf("res = %d\n", __builtin_constant_p(constant_p_var));
1948 void const_func(const int a)
1952 void const_warn_test(void)
1954 const_func(1);