removed warnings
[tinycc/miki.git] / tcctest.c
blobccb943292d605c38725846ba532effda69bf9a51
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 stdarg_test(void);
74 void whitespace_test(void);
75 void relocation_test(void);
76 void old_style_function(void);
77 void sizeof_test(void);
78 void typeof_test(void);
79 void local_label_test(void);
80 void statement_expr_test(void);
81 void asm_test(void);
82 void builtin_test(void);
84 int fib(int n);
85 void num(int n);
86 void forward_ref(void);
87 int isid(int c);
89 #define A 2
90 #define N 1234 + A
91 #define pf printf
92 #define M1(a, b) (a) + (b)
94 #define str\
95 (s) # s
96 #define glue(a, b) a ## b
97 #define xglue(a, b) glue(a, b)
98 #define HIGHLOW "hello"
99 #define LOW LOW ", world"
101 #define min(a, b) ((a) < (b) ? (a) : (b))
103 #ifdef C99_MACROS
104 #define dprintf(level,...) printf(__VA_ARGS__)
105 #endif
107 /* gcc vararg macros */
108 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
110 #define MACRO_NOARGS()
112 #define AAA 3
113 #undef AAA
114 #define AAA 4
116 #if 1
117 #define B3 1
118 #elif 1
119 #define B3 2
120 #elif 0
121 #define B3 3
122 #else
123 #define B3 4
124 #endif
126 #define __INT64_C(c) c ## LL
127 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
129 int qq(int x)
131 return x + 40;
133 #define qq(x) x
135 void macro_test(void)
137 printf("macro:\n");\f\v
138 pf("N=%d\n", N);
139 printf("aaa=%d\n", AAA);
141 printf("min=%d\n", min(1, min(2, -1)));
143 printf("s1=%s\n", glue(HIGH, LOW));
144 printf("s2=%s\n", xglue(HIGH, LOW));
145 printf("s3=%s\n", str("c"));
146 printf("s4=%s\n", str(a1));
147 printf("B3=%d\n", B3);
149 #ifdef A
150 printf("A defined\n");
151 #endif
152 #ifdef B
153 printf("B defined\n");
154 #endif
155 #ifdef A
156 printf("A defined\n");
157 #else
158 printf("A not defined\n");
159 #endif
160 #ifdef B
161 printf("B defined\n");
162 #else
163 printf("B not defined\n");
164 #endif
166 #ifdef A
167 printf("A defined\n");
168 #ifdef B
169 printf("B1 defined\n");
170 #else
171 printf("B1 not defined\n");
172 #endif
173 #else
174 printf("A not defined\n");
175 #ifdef B
176 printf("B2 defined\n");
177 #else
178 printf("B2 not defined\n");
179 #endif
180 #endif
182 #if 1+1
183 printf("test true1\n");
184 #endif
185 #if 0
186 printf("test true2\n");
187 #endif
188 #if 1-1
189 printf("test true3\n");
190 #endif
191 #if defined(A)
192 printf("test trueA\n");
193 #endif
194 #if defined(B)
195 printf("test trueB\n");
196 #endif
198 #if 0
199 printf("test 0\n");
200 #elif 0
201 printf("test 1\n");
202 #elif 2
203 printf("test 2\n");
204 #else
205 printf("test 3\n");
206 #endif
208 MACRO_NOARGS();
210 #ifdef __LINE__
211 printf("__LINE__ defined\n");
212 #endif
214 printf("__LINE__=%d __FILE__=%s\n",
215 __LINE__, __FILE__);
216 #line 200
217 printf("__LINE__=%d __FILE__=%s\n",
218 __LINE__, __FILE__);
219 #line 203 "test"
220 printf("__LINE__=%d __FILE__=%s\n",
221 __LINE__, __FILE__);
222 #line 220 "tcctest.c"
224 /* not strictly preprocessor, but we test it there */
225 #ifdef C99_MACROS
226 printf("__func__ = %s\n", __func__);
227 dprintf(1, "vaarg=%d\n", 1);
228 #endif
229 dprintf1(1, "vaarg1\n");
230 dprintf1(1, "vaarg1=%d\n", 2);
231 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
233 /* gcc extension */
234 printf("func='%s'\n", __FUNCTION__);
236 /* complicated macros in glibc */
237 printf("INT64_MIN=%Ld\n", INT64_MIN);
239 int a;
240 a = 1;
241 glue(a+, +);
242 printf("a=%d\n", a);
243 glue(a <, <= 2);
244 printf("a=%d\n", a);
247 /* macro function with argument outside the macro string */
248 #define MF_s MF_hello
249 #define MF_hello(msg) printf("%s\n",msg)
251 #define MF_t printf("tralala\n"); MF_hello
253 MF_s("hi");
254 MF_t("hi");
256 /* test macro substituion inside args (should not eat stream) */
257 printf("qq=%d\n", qq(qq)(2));
259 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
260 null argument without a space. gcc 3.2 fixes that. */
262 #define qq1(x) 1
263 printf("qq1=%d\n", qq1( ));
265 /* comment with stray handling *\
267 /* this is a valid *\/ comment */
268 /* this is a valid comment *\*/
269 // this is a valid\
270 comment
273 int op(a,b)
275 return a / b;
278 int ret(a)
280 if (a == 2)
281 return 1;
282 if (a == 3)
283 return 2;
284 return 0;
287 void ps(const char *s)
289 int c;
290 while (1) {
291 c = *s;
292 if (c == 0)
293 break;
294 printf("%c", c);
295 s++;
299 const char foo1_string[] = "\
300 bar\n\
301 test\14\
304 void string_test()
306 int b;
307 printf("string:\n");
308 printf("\141\1423\143\n");/* dezdez test */
309 printf("\x41\x42\x43\x3a\n");
310 printf("c=%c\n", 'r');
311 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
312 printf("foo1_string='%s'\n", foo1_string);
313 #if 0
314 printf("wstring=%S\n", L"abc");
315 printf("wstring=%S\n", L"abc" L"def" "ghi");
316 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
317 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
318 #endif
319 ps("test\n");
320 b = 32;
321 while ((b = b + 1) < 96) {
322 printf("%c", b);
324 printf("\n");
325 printf("fib=%d\n", fib(33));
326 b = 262144;
327 while (b != 0x80000000) {
328 num(b);
329 b = b * 2;
333 void loop_test()
335 int i;
336 i = 0;
337 while (i < 10)
338 printf("%d", i++);
339 printf("\n");
340 for(i = 0; i < 10;i++)
341 printf("%d", i);
342 printf("\n");
343 i = 0;
344 do {
345 printf("%d", i++);
346 } while (i < 10);
347 printf("\n");
349 /* break/continue tests */
350 i = 0;
351 while (1) {
352 if (i == 6)
353 break;
354 i++;
355 if (i == 3)
356 continue;
357 printf("%d", i);
359 printf("\n");
361 /* break/continue tests */
362 i = 0;
363 do {
364 if (i == 6)
365 break;
366 i++;
367 if (i == 3)
368 continue;
369 printf("%d", i);
370 } while(1);
371 printf("\n");
373 for(i = 0;i < 10;i++) {
374 if (i == 3)
375 continue;
376 printf("%d", i);
378 printf("\n");
382 void goto_test()
384 int i;
385 static void *label_table[3] = { &&label1, &&label2, &&label3 };
387 printf("goto:\n");
388 i = 0;
389 s_loop:
390 if (i >= 10)
391 goto s_end;
392 printf("%d", i);
393 i++;
394 goto s_loop;
395 s_end:
396 printf("\n");
398 /* we also test computed gotos (GCC extension) */
399 for(i=0;i<3;i++) {
400 goto *label_table[i];
401 label1:
402 printf("label1\n");
403 goto next;
404 label2:
405 printf("label2\n");
406 goto next;
407 label3:
408 printf("label3\n");
409 next: ;
413 enum {
415 E1 = 2,
416 E2 = 4,
421 enum test {
422 E5 = 1000,
425 void enum_test()
427 enum test b1;
428 printf("enum:\n%d %d %d %d %d %d\n",
429 E0, E1, E2, E3, E4, E5);
430 b1 = 1;
431 printf("b1=%d\n", b1);
434 typedef int *my_ptr;
436 void typedef_test()
438 my_ptr a;
439 int b;
440 a = &b;
441 *a = 1234;
442 printf("typedef:\n");
443 printf("a=%d\n", *a);
446 void forward_test()
448 printf("forward:\n");
449 forward_ref();
450 forward_ref();
454 void forward_ref(void)
456 printf("forward ok\n");
459 typedef struct struct1 {
460 int f1;
461 int f2, f3;
462 union union1 {
463 int v1;
464 int v2;
465 } u;
466 char str[3];
467 } struct1;
469 struct struct2 {
470 int a;
471 char b;
474 union union2 {
475 int w1;
476 int w2;
479 struct struct1 st1, st2;
481 int main(int argc, char **argv)
483 string_test();
484 expr_test();
485 macro_test();
486 scope_test();
487 forward_test();
488 funcptr_test();
489 loop_test();
490 switch_test();
491 goto_test();
492 enum_test();
493 typedef_test();
494 struct_test();
495 array_test();
496 expr_ptr_test();
497 bool_test();
498 expr2_test();
499 constant_expr_test();
500 expr_cmp_test();
501 char_short_test();
502 init_test();
503 compound_literal_test();
504 kr_test();
505 struct_assign_test();
506 cast_test();
507 bitfield_test();
508 c99_bool_test();
509 float_test();
510 longlong_test();
511 stdarg_test();
512 whitespace_test();
513 relocation_test();
514 old_style_function();
515 sizeof_test();
516 typeof_test();
517 statement_expr_test();
518 local_label_test();
519 asm_test();
520 builtin_test();
521 return 0;
524 int tab[3];
525 int tab2[3][2];
527 int g;
529 void f1(g)
531 printf("g1=%d\n", g);
534 void scope_test()
536 printf("scope:\n");
537 g = 2;
538 f1(1);
539 printf("g2=%d\n", g);
541 int g;
542 g = 3;
543 printf("g3=%d\n", g);
545 int g;
546 g = 4;
547 printf("g4=%d\n", g);
550 printf("g5=%d\n", g);
553 void array_test(int a[4])
555 int i, j;
557 printf("array:\n");
558 printf("sizeof(a) = %d\n", sizeof(a));
559 printf("sizeof(\"a\") = %d\n", sizeof("a"));
560 #ifdef C99_MACROS
561 printf("sizeof(__func__) = %d\n", sizeof(__func__));
562 #endif
563 printf("sizeof tab %d\n", sizeof(tab));
564 printf("sizeof tab2 %d\n", sizeof tab2);
565 tab[0] = 1;
566 tab[1] = 2;
567 tab[2] = 3;
568 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
569 for(i=0;i<3;i++)
570 for(j=0;j<2;j++)
571 tab2[i][j] = 10 * i + j;
572 for(i=0;i<3*2;i++) {
573 printf(" %3d", ((int *)tab2)[i]);
575 printf("\n");
578 void expr_test()
580 int a, b;
581 a = 0;
582 printf("%d\n", a += 1);
583 printf("%d\n", a -= 2);
584 printf("%d\n", a *= 31232132);
585 printf("%d\n", a /= 4);
586 printf("%d\n", a %= 20);
587 printf("%d\n", a &= 6);
588 printf("%d\n", a ^= 7);
589 printf("%d\n", a |= 8);
590 printf("%d\n", a >>= 3);
591 printf("%d\n", a <<= 4);
593 a = 22321;
594 b = -22321;
595 printf("%d\n", a + 1);
596 printf("%d\n", a - 2);
597 printf("%d\n", a * 312);
598 printf("%d\n", a / 4);
599 printf("%d\n", b / 4);
600 printf("%d\n", (unsigned)b / 4);
601 printf("%d\n", a % 20);
602 printf("%d\n", b % 20);
603 printf("%d\n", (unsigned)b % 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", b >> 3);
609 printf("%d\n", (unsigned)b >> 3);
610 printf("%d\n", a << 4);
611 printf("%d\n", ~a);
612 printf("%d\n", -a);
613 printf("%d\n", +a);
615 printf("%d\n", 12 + 1);
616 printf("%d\n", 12 - 2);
617 printf("%d\n", 12 * 312);
618 printf("%d\n", 12 / 4);
619 printf("%d\n", 12 % 20);
620 printf("%d\n", 12 & 6);
621 printf("%d\n", 12 ^ 7);
622 printf("%d\n", 12 | 8);
623 printf("%d\n", 12 >> 2);
624 printf("%d\n", 12 << 4);
625 printf("%d\n", ~12);
626 printf("%d\n", -12);
627 printf("%d\n", +12);
628 printf("%d %d %d %d\n",
629 isid('a'),
630 isid('g'),
631 isid('T'),
632 isid('('));
635 int isid(int c)
637 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
640 /**********************/
642 int vstack[10], *vstack_ptr;
644 void vpush(int vt, int vc)
646 *vstack_ptr++ = vt;
647 *vstack_ptr++ = vc;
650 void vpop(int *ft, int *fc)
652 *fc = *--vstack_ptr;
653 *ft = *--vstack_ptr;
656 void expr2_test()
658 int a, b;
660 printf("expr2:\n");
661 vstack_ptr = vstack;
662 vpush(1432432, 2);
663 vstack_ptr[-2] &= ~0xffffff80;
664 vpop(&a, &b);
665 printf("res= %d %d\n", a, b);
668 void constant_expr_test()
670 int a;
671 printf("constant_expr:\n");
672 a = 3;
673 printf("%d\n", a * 16);
674 printf("%d\n", a * 1);
675 printf("%d\n", a + 0);
678 int tab4[10];
680 void expr_ptr_test()
682 int *p, *q;
684 printf("expr_ptr:\n");
685 p = tab4;
686 q = tab4 + 10;
687 printf("diff=%d\n", q - p);
688 p++;
689 printf("inc=%d\n", p - tab4);
690 p--;
691 printf("dec=%d\n", p - tab4);
692 ++p;
693 printf("inc=%d\n", p - tab4);
694 --p;
695 printf("dec=%d\n", p - tab4);
696 printf("add=%d\n", p + 3 - tab4);
697 printf("add=%d\n", 3 + p - tab4);
700 void expr_cmp_test()
702 int a, b;
703 printf("constant_expr:\n");
704 a = -1;
705 b = 1;
706 printf("%d\n", a == a);
707 printf("%d\n", a != a);
709 printf("%d\n", a < b);
710 printf("%d\n", a <= b);
711 printf("%d\n", a <= a);
712 printf("%d\n", b >= a);
713 printf("%d\n", a >= a);
714 printf("%d\n", b > a);
716 printf("%d\n", (unsigned)a < b);
717 printf("%d\n", (unsigned)a <= b);
718 printf("%d\n", (unsigned)a <= a);
719 printf("%d\n", (unsigned)b >= a);
720 printf("%d\n", (unsigned)a >= a);
721 printf("%d\n", (unsigned)b > a);
724 struct empty {
727 struct aligntest1 {
728 char a[10];
731 struct aligntest2 {
732 int a;
733 char b[10];
736 struct aligntest3 {
737 double a, b;
740 struct aligntest4 {
741 double a[0];
744 void struct_test()
746 struct1 *s;
747 union union2 u;
749 printf("struct:\n");
750 printf("sizes: %d %d %d %d\n",
751 sizeof(struct struct1),
752 sizeof(struct struct2),
753 sizeof(union union1),
754 sizeof(union union2));
755 st1.f1 = 1;
756 st1.f2 = 2;
757 st1.f3 = 3;
758 printf("st1: %d %d %d\n",
759 st1.f1, st1.f2, st1.f3);
760 st1.u.v1 = 1;
761 st1.u.v2 = 2;
762 printf("union1: %d\n", st1.u.v1);
763 u.w1 = 1;
764 u.w2 = 2;
765 printf("union2: %d\n", u.w1);
766 s = &st2;
767 s->f1 = 3;
768 s->f2 = 2;
769 s->f3 = 1;
770 printf("st2: %d %d %d\n",
771 s->f1, s->f2, s->f3);
772 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
774 /* align / size tests */
775 printf("aligntest1 sizeof=%d alignof=%d\n",
776 sizeof(struct aligntest1), __alignof__(struct aligntest1));
777 printf("aligntest2 sizeof=%d alignof=%d\n",
778 sizeof(struct aligntest2), __alignof__(struct aligntest2));
779 printf("aligntest3 sizeof=%d alignof=%d\n",
780 sizeof(struct aligntest3), __alignof__(struct aligntest3));
781 printf("aligntest4 sizeof=%d alignof=%d\n",
782 sizeof(struct aligntest4), __alignof__(struct aligntest4));
784 /* empty structures (GCC extension) */
785 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
786 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
789 /* XXX: depend on endianness */
790 void char_short_test()
792 int var1, var2;
794 printf("char_short:\n");
796 var1 = 0x01020304;
797 var2 = 0xfffefdfc;
798 printf("s8=%d %d\n",
799 *(char *)&var1, *(char *)&var2);
800 printf("u8=%d %d\n",
801 *(unsigned char *)&var1, *(unsigned char *)&var2);
802 printf("s16=%d %d\n",
803 *(short *)&var1, *(short *)&var2);
804 printf("u16=%d %d\n",
805 *(unsigned short *)&var1, *(unsigned short *)&var2);
806 printf("s32=%d %d\n",
807 *(int *)&var1, *(int *)&var2);
808 printf("u32=%d %d\n",
809 *(unsigned int *)&var1, *(unsigned int *)&var2);
810 *(char *)&var1 = 0x08;
811 printf("var1=%x\n", var1);
812 *(short *)&var1 = 0x0809;
813 printf("var1=%x\n", var1);
814 *(int *)&var1 = 0x08090a0b;
815 printf("var1=%x\n", var1);
818 /******************/
820 typedef struct Sym {
821 int v;
822 int t;
823 int c;
824 struct Sym *next;
825 struct Sym *prev;
826 } Sym;
828 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
829 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
831 static int toupper1(int a)
833 return TOUPPER(a);
836 void bool_test()
838 int *s, a, b, t, f, i;
840 a = 0;
841 s = (void*)0;
842 printf("!s=%d\n", !s);
844 if (!s || !s[0])
845 a = 1;
846 printf("a=%d\n", a);
848 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
849 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
850 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
851 #if 1 && 1
852 printf("a1\n");
853 #endif
854 #if 1 || 0
855 printf("a2\n");
856 #endif
857 #if 1 ? 0 : 1
858 printf("a3\n");
859 #endif
860 #if 0 ? 0 : 1
861 printf("a4\n");
862 #endif
864 a = 4;
865 printf("b=%d\n", a + (0 ? 1 : a / 2));
867 /* test register spilling */
868 a = 10;
869 b = 10;
870 a = (a + b) * ((a < b) ?
871 ((b - a) * (a - b)): a + b);
872 printf("a=%d\n", a);
874 /* test complex || or && expressions */
875 t = 1;
876 f = 0;
877 a = 32;
878 printf("exp=%d\n", f == (32 <= a && a <= 3));
879 printf("r=%d\n", (t || f) + (t && f));
881 /* test ? : cast */
883 int aspect_on;
884 int aspect_native = 65536;
885 double bfu_aspect = 1.0;
886 int aspect;
887 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
888 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
889 printf("aspect=%d\n", aspect);
893 /* test ? : GCC extension */
895 static int v1 = 34 ? : -1; /* constant case */
896 static int v2 = 0 ? : -1; /* constant case */
897 int a = 30;
899 printf("%d %d\n", v1, v2);
900 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
903 /* again complex expression */
904 for(i=0;i<256;i++) {
905 if (toupper1 (i) != TOUPPER (i))
906 printf("error %d\n", i);
910 /* GCC accepts that */
911 static int tab_reinit[];
912 static int tab_reinit[10];
914 //int cinit1; /* a global variable can be defined several times without error ! */
915 int cinit1;
916 int cinit1;
917 int cinit1 = 0;
918 int *cinit2 = (int []){3, 2, 1};
920 void compound_literal_test(void)
922 int *p, i;
923 char *q, *q3;
925 printf("compound_test:\n");
927 p = (int []){1, 2, 3};
928 for(i=0;i<3;i++)
929 printf(" %d", p[i]);
930 printf("\n");
932 for(i=0;i<3;i++)
933 printf("%d", cinit2[i]);
934 printf("\n");
936 q = "tralala1";
937 printf("q1=%s\n", q);
939 q = (char *){ "tralala2" };
940 printf("q2=%s\n", q);
942 q3 = (char *){ q };
943 printf("q3=%s\n", q3);
945 q = (char []){ "tralala3" };
946 printf("q4=%s\n", q);
948 #ifdef ALL_ISOC99
949 p = (int []){1, 2, cinit1 + 3};
950 for(i=0;i<3;i++)
951 printf(" %d", p[i]);
952 printf("\n");
954 for(i=0;i<3;i++) {
955 p = (int []){1, 2, 4 + i};
956 printf("%d %d %d\n",
957 p[0],
958 p[1],
959 p[2]);
961 #endif
964 /* K & R protos */
966 kr_func1(a, b)
968 return a + b;
971 int kr_func2(a, b)
973 return a + b;
976 kr_test()
978 printf("kr_test:\n");
979 printf("func1=%d\n", kr_func1(3, 4));
980 printf("func2=%d\n", kr_func2(3, 4));
981 return 0;
984 void num(int n)
986 char *tab, *p;
987 tab = (char*)malloc(20);
988 p = tab;
989 while (1) {
990 *p = 48 + (n % 10);
991 p++;
992 n = n / 10;
993 if (n == 0)
994 break;
996 while (p != tab) {
997 p--;
998 printf("%c", *p);
1000 printf("\n");
1003 /* structure assignment tests */
1004 struct structa1 {
1005 int f1;
1006 char f2;
1009 struct structa1 ssta1;
1011 void struct_assign_test1(struct structa1 s1, int t)
1013 printf("%d %d %d\n", s1.f1, s1.f2, t);
1016 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1018 s1.f1 += t;
1019 s1.f2 -= t;
1020 return s1;
1023 void struct_assign_test(void)
1025 struct structa1 lsta1, lsta2;
1027 #if 0
1028 printf("struct_assign_test:\n");
1030 lsta1.f1 = 1;
1031 lsta1.f2 = 2;
1032 printf("%d %d\n", lsta1.f1, lsta1.f2);
1033 lsta2 = lsta1;
1034 printf("%d %d\n", lsta2.f1, lsta2.f2);
1035 #else
1036 lsta2.f1 = 1;
1037 lsta2.f2 = 2;
1038 #endif
1039 struct_assign_test1(lsta2, 3);
1041 printf("before call: %d %d\n", lsta2.f1, lsta2.f2);
1042 lsta2 = struct_assign_test2(lsta2, 4);
1043 printf("after call: %d %d\n", lsta2.f1, lsta2.f2);
1046 /* casts to short/char */
1048 void cast1(char a, short b, unsigned char c, unsigned short d)
1050 printf("%d %d %d %d\n", a, b, c, d);
1053 char bcast;
1054 short scast;
1056 void cast_test()
1058 int a;
1059 char c;
1060 char tab[10];
1062 printf("cast_test:\n");
1063 a = 0xfffff;
1064 cast1(a, a, a, a);
1065 a = 0xffffe;
1066 printf("%d %d %d %d\n",
1067 (char)(a + 1),
1068 (short)(a + 1),
1069 (unsigned char)(a + 1),
1070 (unsigned short)(a + 1));
1071 printf("%d %d %d %d\n",
1072 (char)0xfffff,
1073 (short)0xfffff,
1074 (unsigned char)0xfffff,
1075 (unsigned short)0xfffff);
1077 a = (bcast = 128) + 1;
1078 printf("%d\n", a);
1079 a = (scast = 65536) + 1;
1080 printf("%d\n", a);
1082 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1084 /* test implicit int casting for array accesses */
1085 c = 0;
1086 tab[1] = 2;
1087 tab[c] = 1;
1088 printf("%d %d\n", tab[0], tab[1]);
1090 /* test implicit casting on some operators */
1091 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1092 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1093 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1096 /* initializers tests */
1097 struct structinit1 {
1098 int f1;
1099 char f2;
1100 short f3;
1101 int farray[3];
1104 int sinit1 = 2;
1105 int sinit2 = { 3 };
1106 int sinit3[3] = { 1, 2, {{3}}, };
1107 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1108 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1109 int sinit6[] = { 1, 2, 3 };
1110 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1111 char sinit8[] = "hello" "trala";
1113 struct structinit1 sinit9 = { 1, 2, 3 };
1114 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1115 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1116 #ifdef ALL_ISOC99
1117 .farray[0] = 10,
1118 .farray[1] = 11,
1119 .farray[2] = 12,
1120 #endif
1123 char *sinit12 = "hello world";
1124 char *sinit13[] = {
1125 "test1",
1126 "test2",
1127 "test3",
1129 char sinit14[10] = { "abc" };
1130 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1132 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1134 struct bar {
1135 char *s;
1136 int len;
1137 } sinit17[] = {
1138 "a1", 4,
1139 "a2", 1
1142 int sinit18[10] = {
1143 [2 ... 5] = 20,
1145 [8] = 10,
1148 void init_test(void)
1150 int linit1 = 2;
1151 int linit2 = { 3 };
1152 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1153 int linit6[] = { 1, 2, 3 };
1154 int i, j;
1155 char linit8[] = "hello" "trala";
1156 int linit12[10] = { 1, 2 };
1157 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1158 char linit14[10] = "abc";
1159 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1160 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1161 int linit17 = sizeof(linit17);
1163 printf("init_test:\n");
1165 printf("sinit1=%d\n", sinit1);
1166 printf("sinit2=%d\n", sinit2);
1167 printf("sinit3=%d %d %d %d\n",
1168 sizeof(sinit3),
1169 sinit3[0],
1170 sinit3[1],
1171 sinit3[2]
1173 printf("sinit6=%d\n", sizeof(sinit6));
1174 printf("sinit7=%d %d %d %d\n",
1175 sizeof(sinit7),
1176 sinit7[0],
1177 sinit7[1],
1178 sinit7[2]
1180 printf("sinit8=%s\n", sinit8);
1181 printf("sinit9=%d %d %d\n",
1182 sinit9.f1,
1183 sinit9.f2,
1184 sinit9.f3
1186 printf("sinit10=%d %d %d\n",
1187 sinit10.f1,
1188 sinit10.f2,
1189 sinit10.f3
1191 printf("sinit11=%d %d %d %d %d %d\n",
1192 sinit11.f1,
1193 sinit11.f2,
1194 sinit11.f3,
1195 sinit11.farray[0],
1196 sinit11.farray[1],
1197 sinit11.farray[2]
1200 for(i=0;i<3;i++)
1201 for(j=0;j<2;j++)
1202 printf("[%d][%d] = %d %d %d\n",
1203 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1204 printf("linit1=%d\n", linit1);
1205 printf("linit2=%d\n", linit2);
1206 printf("linit6=%d\n", sizeof(linit6));
1207 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1209 printf("sinit12=%s\n", sinit12);
1210 printf("sinit13=%d %s %s %s\n",
1211 sizeof(sinit13),
1212 sinit13[0],
1213 sinit13[1],
1214 sinit13[2]);
1215 printf("sinit14=%s\n", sinit14);
1217 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1218 printf("\n");
1219 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1220 printf("\n");
1221 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1222 printf("\n");
1223 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1224 printf("\n");
1225 printf("%d %d %d %d\n",
1226 linit16.a1,
1227 linit16.a2,
1228 linit16.a3,
1229 linit16.a4);
1230 /* test that initialisation is done after variable declare */
1231 printf("linit17=%d\n", linit17);
1232 printf("sinit15=%d\n", sinit15[0]);
1233 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1234 printf("sinit17=%s %d %s %d\n",
1235 sinit17[0].s, sinit17[0].len,
1236 sinit17[1].s, sinit17[1].len);
1237 for(i=0;i<10;i++)
1238 printf("%x ", sinit18[i]);
1239 printf("\n");
1243 void switch_test()
1245 int i;
1247 for(i=0;i<15;i++) {
1248 switch(i) {
1249 case 0:
1250 case 1:
1251 printf("a");
1252 break;
1253 default:
1254 printf("%d", i);
1255 break;
1256 case 8 ... 12:
1257 printf("c");
1258 break;
1259 case 3:
1260 printf("b");
1261 break;
1264 printf("\n");
1267 /* ISOC99 _Bool type */
1268 void c99_bool_test(void)
1270 #ifdef BOOL_ISOC99
1271 int a;
1272 _Bool b;
1274 printf("bool_test:\n");
1275 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1276 a = 3;
1277 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1278 b = 3;
1279 printf("b = %d\n", b);
1280 b++;
1281 printf("b = %d\n", b);
1282 #endif
1285 void bitfield_test(void)
1287 int a;
1288 struct sbf1 {
1289 int f1 : 3;
1290 int : 2;
1291 int f2 : 1;
1292 int : 0;
1293 int f3 : 5;
1294 int f4 : 7;
1295 unsigned int f5 : 7;
1296 } st1;
1297 printf("bitfield_test:");
1298 printf("sizeof(st1) = %d\n", sizeof(st1));
1300 st1.f1 = 3;
1301 st1.f2 = 1;
1302 st1.f3 = 15;
1303 a = 120;
1304 st1.f4 = a;
1305 st1.f5 = a;
1306 st1.f5++;
1307 printf("%d %d %d %d %d\n",
1308 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1310 st1.f1 = 7;
1311 if (st1.f1 == -1)
1312 printf("st1.f1 == -1\n");
1313 else
1314 printf("st1.f1 != -1\n");
1315 if (st1.f2 == -1)
1316 printf("st1.f2 == -1\n");
1317 else
1318 printf("st1.f2 != -1\n");
1321 #define FTEST(prefix, type, fmt)\
1322 void prefix ## cmp(type a, type b)\
1324 printf("%d %d %d %d %d %d\n",\
1325 a == b,\
1326 a != b,\
1327 a < b,\
1328 a > b,\
1329 a >= b,\
1330 a <= b);\
1331 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1334 a + b,\
1335 a - b,\
1336 a * b,\
1337 a / b,\
1338 -a);\
1339 printf(fmt "\n", ++a);\
1340 printf(fmt "\n", a++);\
1341 printf(fmt "\n", a);\
1343 void prefix ## fcast(type a)\
1345 float fa;\
1346 double da;\
1347 long double la;\
1348 int ia;\
1349 unsigned int ua;\
1350 type b;\
1351 fa = a;\
1352 da = a;\
1353 la = a;\
1354 printf("ftof: %f %f %Lf\n", fa, da, la);\
1355 ia = (int)a;\
1356 ua = (unsigned int)a;\
1357 printf("ftoi: %d %u\n", ia, ua);\
1358 ia = -1234;\
1359 ua = 0x81234500;\
1360 b = ia;\
1361 printf("itof: " fmt "\n", b);\
1362 b = ua;\
1363 printf("utof: " fmt "\n", b);\
1366 void prefix ## test(void)\
1368 printf("testing '%s'\n", #type);\
1369 prefix ## cmp(1, 2.5);\
1370 prefix ## cmp(2, 1.5);\
1371 prefix ## cmp(1, 1);\
1372 prefix ## fcast(234.6);\
1373 prefix ## fcast(-2334.6);\
1376 FTEST(f, float, "%f")
1377 FTEST(d, double, "%f")
1378 FTEST(ld, long double, "%Lf")
1380 double ftab1[3] = { 1.2, 3.4, -5.6 };
1383 void float_test(void)
1385 float fa, fb;
1386 double da, db;
1387 int a;
1388 unsigned int b;
1390 printf("float_test:\n");
1391 printf("sizeof(float) = %d\n", sizeof(float));
1392 printf("sizeof(double) = %d\n", sizeof(double));
1393 printf("sizeof(long double) = %d\n", sizeof(long double));
1394 ftest();
1395 dtest();
1396 ldtest();
1397 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1398 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1399 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1400 da = 123;
1401 printf("da=%f\n", da);
1402 fa = 123;
1403 printf("fa=%f\n", fa);
1404 a = 4000000000;
1405 da = a;
1406 printf("da = %f\n", da);
1407 b = 4000000000;
1408 db = b;
1409 printf("db = %f\n", db);
1412 int fib(int n)
1414 if (n <= 2)
1415 return 1;
1416 else
1417 return fib(n-1) + fib(n-2);
1420 void funcptr_test()
1422 void (*func)(int);
1423 int a;
1424 struct {
1425 int dummy;
1426 void (*func)(int);
1427 } st1;
1429 printf("funcptr:\n");
1430 func = &num;
1431 (*func)(12345);
1432 func = num;
1433 a = 1;
1434 a = 1;
1435 func(12345);
1436 /* more complicated pointer computation */
1437 st1.func = num;
1438 st1.func(12346);
1439 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1440 printf("sizeof2 = %d\n", sizeof funcptr_test);
1441 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1442 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1445 void lloptest(long long a, long long b)
1447 unsigned long long ua, ub;
1449 ua = a;
1450 ub = b;
1451 /* arith */
1452 printf("arith: %Ld %Ld %Ld\n",
1453 a + b,
1454 a - b,
1455 a * b);
1457 if (b != 0) {
1458 printf("arith1: %Ld %Ld\n",
1459 a / b,
1460 a % b);
1463 /* binary */
1464 printf("bin: %Ld %Ld %Ld\n",
1465 a & b,
1466 a | b,
1467 a ^ b);
1469 /* tests */
1470 printf("test: %d %d %d %d %d %d\n",
1471 a == b,
1472 a != b,
1473 a < b,
1474 a > b,
1475 a >= b,
1476 a <= b);
1478 printf("utest: %d %d %d %d %d %d\n",
1479 ua == ub,
1480 ua != ub,
1481 ua < ub,
1482 ua > ub,
1483 ua >= ub,
1484 ua <= ub);
1486 /* arith2 */
1487 a++;
1488 b++;
1489 printf("arith2: %Ld %Ld\n", a, b);
1490 printf("arith2: %Ld %Ld\n", a++, b++);
1491 printf("arith2: %Ld %Ld\n", --a, --b);
1492 printf("arith2: %Ld %Ld\n", a, b);
1495 void llshift(long long a, int b)
1497 printf("shift: %Ld %Ld %Ld\n",
1498 (unsigned long long)a >> b,
1499 a >> b,
1500 a << b);
1501 printf("shiftc: %Ld %Ld %Ld\n",
1502 (unsigned long long)a >> 3,
1503 a >> 3,
1504 a << 3);
1505 printf("shiftc: %Ld %Ld %Ld\n",
1506 (unsigned long long)a >> 35,
1507 a >> 35,
1508 a << 35);
1511 void llfloat(void)
1513 float fa;
1514 double da;
1515 long double lda;
1516 long long la, lb, lc;
1517 unsigned long long ula, ulb, ulc;
1518 la = 0x12345678;
1519 ula = 0x72345678;
1520 la = (la << 20) | 0x12345;
1521 ula = ula << 33;
1522 printf("la=%Ld ula=%Lu\n", la, ula);
1524 fa = la;
1525 da = la;
1526 lda = la;
1527 printf("lltof: %f %f %Lf\n", fa, da, lda);
1529 la = fa;
1530 lb = da;
1531 lc = lda;
1532 printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
1534 fa = ula;
1535 da = ula;
1536 lda = ula;
1537 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1539 ula = fa;
1540 ulb = da;
1541 ulc = lda;
1542 printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
1545 long long llfunc1(int a)
1547 return a * 2;
1550 struct S {
1551 int id;
1552 char item;
1555 long long int value(struct S *v)
1557 return ((long long int)v->item);
1560 void longlong_test(void)
1562 long long a, b, c;
1563 int ia;
1564 unsigned int ua;
1565 printf("longlong_test:\n");
1566 printf("sizeof(long long) = %d\n", sizeof(long long));
1567 ia = -1;
1568 ua = -2;
1569 a = ia;
1570 b = ua;
1571 printf("%Ld %Ld\n", a, b);
1572 printf("%Ld %Ld %Ld %Lx\n",
1573 (long long)1,
1574 (long long)-2,
1575 1LL,
1576 0x1234567812345679);
1577 a = llfunc1(-3);
1578 printf("%Ld\n", a);
1580 lloptest(1000, 23);
1581 lloptest(0xff, 0x1234);
1582 b = 0x72345678 << 10;
1583 lloptest(-3, b);
1584 llshift(0x123, 5);
1585 llshift(-23, 5);
1586 b = 0x72345678LL << 10;
1587 llshift(b, 47);
1589 llfloat();
1590 #if 1
1591 b = 0x12345678;
1592 a = -1;
1593 c = a + b;
1594 printf("%Lx\n", c);
1595 #endif
1597 /* long long reg spill test */
1599 struct S a;
1601 a.item = 3;
1602 printf("%lld\n", value(&a));
1604 lloptest(0x80000000, 0);
1606 /* another long long spill test */
1608 long long *p, v;
1609 v = 1;
1610 p = &v;
1611 p[0]++;
1612 printf("%lld\n", *p);
1616 void vprintf1(const char *fmt, ...)
1618 va_list ap;
1619 const char *p;
1620 int c, i;
1621 double d;
1622 long long ll;
1624 va_start(ap, fmt);
1626 p = fmt;
1627 for(;;) {
1628 c = *p;
1629 if (c == '\0')
1630 break;
1631 p++;
1632 if (c == '%') {
1633 c = *p;
1634 switch(c) {
1635 case '\0':
1636 goto the_end;
1637 case 'd':
1638 i = va_arg(ap, int);
1639 printf("%d", i);
1640 break;
1641 case 'f':
1642 d = va_arg(ap, double);
1643 printf("%f", d);
1644 break;
1645 case 'l':
1646 ll = va_arg(ap, long long);
1647 printf("%Ld", ll);
1648 break;
1650 p++;
1651 } else {
1652 putchar(c);
1655 the_end:
1656 va_end(ap);
1660 void stdarg_test(void)
1662 vprintf1("%d %d %d\n", 1, 2, 3);
1663 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1664 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1667 void whitespace_test(void)
1669 char *str;
1671 \f\v #if 1
1672 pri\
1673 ntf("whitspace:\n");\f\v
1674 #endif
1675 pf("N=%d\n", 2);
1677 #ifdef CORRECT_CR_HANDLING
1678 pri\
1679 ntf("aaa=%d\n", 3);
1680 #endif
1682 pri\
1684 ntf("min=%d\n", 4);
1686 #ifdef ACCEPT_CR_IN_STRINGS
1687 printf("len1=%d\n", strlen("
1688 "));
1689 #ifdef CORRECT_CR_HANDLING
1690 str = "
1692 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
1693 #endif
1694 printf("len1=%d\n", strlen(" a
1695 "));
1696 #endif /* ACCEPT_CR_IN_STRINGS */
1699 int reltab[3] = { 1, 2, 3 };
1701 int *rel1 = &reltab[1];
1702 int *rel2 = &reltab[2];
1704 void relocation_test(void)
1706 printf("*rel1=%d\n", *rel1);
1707 printf("*rel2=%d\n", *rel2);
1710 void old_style_f(a,b,c)
1711 int a, b;
1712 double c;
1714 printf("a=%d b=%d b=%f\n", a, b, c);
1717 void decl_func1(int cmpfn())
1719 printf("cmpfn=%lx\n", (long)cmpfn);
1722 void decl_func2(cmpfn)
1723 int cmpfn();
1725 printf("cmpfn=%lx\n", (long)cmpfn);
1728 void old_style_function(void)
1730 old_style_f((void *)1, 2, 3.0);
1731 decl_func1(NULL);
1732 decl_func2(NULL);
1735 void sizeof_test(void)
1737 int a;
1738 int **ptr;
1740 printf("sizeof(int) = %d\n", sizeof(int));
1741 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1742 printf("sizeof(short) = %d\n", sizeof(short));
1743 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1744 printf("sizeof(char) = %d\n", sizeof(char));
1745 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1746 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1747 a = 1;
1748 printf("sizeof(a++) = %d\n", sizeof a++);
1749 printf("a=%d\n", a);
1750 ptr = NULL;
1751 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
1753 /* some alignof tests */
1754 printf("__alignof__(int) = %d\n", __alignof__(int));
1755 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1756 printf("__alignof__(short) = %d\n", __alignof__(short));
1757 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1758 printf("__alignof__(char) = %d\n", __alignof__(char));
1759 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1760 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
1763 void typeof_test(void)
1765 double a;
1766 typeof(a) b;
1767 typeof(float) c;
1769 a = 1.5;
1770 b = 2.5;
1771 c = 3.5;
1772 printf("a=%f b=%f c=%f\n", a, b, c);
1775 void statement_expr_test(void)
1777 int a, i;
1779 a = 0;
1780 for(i=0;i<10;i++) {
1781 a += 1 +
1782 ( { int b, j;
1783 b = 0;
1784 for(j=0;j<5;j++)
1785 b += j; b;
1786 } );
1788 printf("a=%d\n", a);
1792 void local_label_test(void)
1794 int a;
1795 goto l1;
1797 a = 1 + ({
1798 __label__ l1, l2, l3;
1799 goto l4;
1801 printf("aa1\n");
1802 goto l1;
1804 printf("aa3\n");
1805 goto l3;
1807 printf("aa2\n");
1808 goto l2;
1809 l3:;
1812 printf("a=%d\n", a);
1813 return;
1815 printf("bb1\n");
1816 goto l2;
1818 printf("bb2\n");
1819 goto l5;
1822 /* inline assembler test */
1823 #ifdef __i386__
1825 /* from linux kernel */
1826 static char * strncat1(char * dest,const char * src,size_t count)
1828 int d0, d1, d2, d3;
1829 __asm__ __volatile__(
1830 "repne\n\t"
1831 "scasb\n\t"
1832 "decl %1\n\t"
1833 "movl %8,%3\n"
1834 "1:\tdecl %3\n\t"
1835 "js 2f\n\t"
1836 "lodsb\n\t"
1837 "stosb\n\t"
1838 "testb %%al,%%al\n\t"
1839 "jne 1b\n"
1840 "2:\txorl %2,%2\n\t"
1841 "stosb"
1842 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
1843 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
1844 : "memory");
1845 return dest;
1848 static inline void * memcpy1(void * to, const void * from, size_t n)
1850 int d0, d1, d2;
1851 __asm__ __volatile__(
1852 "rep ; movsl\n\t"
1853 "testb $2,%b4\n\t"
1854 "je 1f\n\t"
1855 "movsw\n"
1856 "1:\ttestb $1,%b4\n\t"
1857 "je 2f\n\t"
1858 "movsb\n"
1859 "2:"
1860 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
1861 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
1862 : "memory");
1863 return (to);
1866 static __inline__ void sigaddset1(unsigned int *set, int _sig)
1868 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
1871 static __inline__ void sigdelset1(unsigned int *set, int _sig)
1873 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
1876 static __inline__ __const__ unsigned int swab32(unsigned int x)
1878 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
1879 "rorl $16,%0\n\t" /* swap words */
1880 "xchgb %b0,%h0" /* swap higher bytes */
1881 :"=q" (x)
1882 : "0" (x));
1883 return x;
1886 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
1888 unsigned long long res;
1889 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
1890 return res;
1893 static __inline__ unsigned long long inc64(unsigned long long a)
1895 unsigned long long res;
1896 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
1897 return res;
1900 unsigned int set;
1902 void asm_test(void)
1904 char buf[128];
1905 unsigned int val;
1907 printf("inline asm:\n");
1908 /* test the no operand case */
1909 asm volatile ("xorl %eax, %eax");
1911 memcpy1(buf, "hello", 6);
1912 strncat1(buf, " worldXXXXX", 3);
1913 printf("%s\n", buf);
1915 /* 'A' constraint test */
1916 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
1917 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
1919 set = 0xff;
1920 sigdelset1(&set, 2);
1921 sigaddset1(&set, 16);
1922 /* NOTE: we test here if C labels are correctly restored after the
1923 asm statement */
1924 goto label1;
1925 label2:
1926 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
1927 printf("set=0x%x\n", set);
1928 val = 0x01020304;
1929 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
1930 return;
1931 label1:
1932 goto label2;
1935 #else
1937 void asm_test(void)
1941 #endif
1943 #define COMPAT_TYPE(type1, type2) \
1945 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
1946 __builtin_types_compatible_p (type1, type2));\
1949 int constant_p_var;
1951 void builtin_test(void)
1953 #if GCC_MAJOR >= 3
1954 COMPAT_TYPE(int, int);
1955 COMPAT_TYPE(int, unsigned int);
1956 COMPAT_TYPE(int, char);
1957 COMPAT_TYPE(int, const int);
1958 COMPAT_TYPE(int, volatile int);
1959 COMPAT_TYPE(int *, int *);
1960 COMPAT_TYPE(int *, void *);
1961 COMPAT_TYPE(int *, const int *);
1962 COMPAT_TYPE(char *, unsigned char *);
1963 /* space is needed because tcc preprocessor introduces a space between each token */
1964 COMPAT_TYPE(char * *, void *);
1965 #endif
1966 printf("res = %d\n", __builtin_constant_p(1));
1967 printf("res = %d\n", __builtin_constant_p(1 + 2));
1968 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
1969 printf("res = %d\n", __builtin_constant_p(constant_p_var));
1973 void const_func(const int a)
1977 void const_warn_test(void)
1979 const_func(1);