added _Bool in bit-fields
[tinycc/miki.git] / tcctest.c
blobf1ebfacd6aec3f77888f073b29ba077c49c1bf79
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);
1307 st1.f1 = 7;
1308 if (st1.f1 == -1)
1309 printf("st1.f1 == -1\n");
1310 else
1311 printf("st1.f1 != -1\n");
1312 if (st1.f2 == -1)
1313 printf("st1.f2 == -1\n");
1314 else
1315 printf("st1.f2 != -1\n");
1318 #define FTEST(prefix, type, fmt)\
1319 void prefix ## cmp(type a, type b)\
1321 printf("%d %d %d %d %d %d\n",\
1322 a == b,\
1323 a != b,\
1324 a < b,\
1325 a > b,\
1326 a >= b,\
1327 a <= b);\
1328 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1331 a + b,\
1332 a - b,\
1333 a * b,\
1334 a / b,\
1335 -a);\
1336 printf(fmt "\n", ++a);\
1337 printf(fmt "\n", a++);\
1338 printf(fmt "\n", a);\
1340 void prefix ## fcast(type a)\
1342 float fa;\
1343 double da;\
1344 long double la;\
1345 int ia;\
1346 unsigned int ua;\
1347 type b;\
1348 fa = a;\
1349 da = a;\
1350 la = a;\
1351 printf("ftof: %f %f %Lf\n", fa, da, la);\
1352 ia = (int)a;\
1353 ua = (unsigned int)a;\
1354 printf("ftoi: %d %u\n", ia, ua);\
1355 ia = -1234;\
1356 ua = 0x81234500;\
1357 b = ia;\
1358 printf("itof: " fmt "\n", b);\
1359 b = ua;\
1360 printf("utof: " fmt "\n", b);\
1363 void prefix ## test(void)\
1365 printf("testing '%s'\n", #type);\
1366 prefix ## cmp(1, 2.5);\
1367 prefix ## cmp(2, 1.5);\
1368 prefix ## cmp(1, 1);\
1369 prefix ## fcast(234.6);\
1370 prefix ## fcast(-2334.6);\
1373 FTEST(f, float, "%f")
1374 FTEST(d, double, "%f")
1375 FTEST(ld, long double, "%Lf")
1377 double ftab1[3] = { 1.2, 3.4, -5.6 };
1380 void float_test(void)
1382 float fa, fb;
1383 double da, db;
1384 int a;
1385 unsigned int b;
1387 printf("float_test:\n");
1388 printf("sizeof(float) = %d\n", sizeof(float));
1389 printf("sizeof(double) = %d\n", sizeof(double));
1390 printf("sizeof(long double) = %d\n", sizeof(long double));
1391 ftest();
1392 dtest();
1393 ldtest();
1394 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1395 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1396 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1397 da = 123;
1398 printf("da=%f\n", da);
1399 fa = 123;
1400 printf("fa=%f\n", fa);
1401 a = 4000000000;
1402 da = a;
1403 printf("da = %f\n", da);
1404 b = 4000000000;
1405 db = b;
1406 printf("db = %f\n", db);
1409 int fib(int n)
1411 if (n <= 2)
1412 return 1;
1413 else
1414 return fib(n-1) + fib(n-2);
1417 void funcptr_test()
1419 void (*func)(int);
1420 int a;
1421 struct {
1422 int dummy;
1423 void (*func)(int);
1424 } st1;
1426 printf("funcptr:\n");
1427 func = &num;
1428 (*func)(12345);
1429 func = num;
1430 a = 1;
1431 a = 1;
1432 func(12345);
1433 /* more complicated pointer computation */
1434 st1.func = num;
1435 st1.func(12346);
1436 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1437 printf("sizeof2 = %d\n", sizeof funcptr_test);
1438 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1439 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1442 void lloptest(long long a, long long b)
1444 unsigned long long ua, ub;
1446 ua = a;
1447 ub = b;
1448 /* arith */
1449 printf("arith: %Ld %Ld %Ld\n",
1450 a + b,
1451 a - b,
1452 a * b);
1454 if (b != 0) {
1455 printf("arith1: %Ld %Ld\n",
1456 a / b,
1457 a % b);
1460 /* binary */
1461 printf("bin: %Ld %Ld %Ld\n",
1462 a & b,
1463 a | b,
1464 a ^ b);
1466 /* tests */
1467 printf("test: %d %d %d %d %d %d\n",
1468 a == b,
1469 a != b,
1470 a < b,
1471 a > b,
1472 a >= b,
1473 a <= b);
1475 printf("utest: %d %d %d %d %d %d\n",
1476 ua == ub,
1477 ua != ub,
1478 ua < ub,
1479 ua > ub,
1480 ua >= ub,
1481 ua <= ub);
1483 /* arith2 */
1484 a++;
1485 b++;
1486 printf("arith2: %Ld %Ld\n", a, b);
1487 printf("arith2: %Ld %Ld\n", a++, b++);
1488 printf("arith2: %Ld %Ld\n", --a, --b);
1489 printf("arith2: %Ld %Ld\n", a, b);
1492 void llshift(long long a, int b)
1494 printf("shift: %Ld %Ld %Ld\n",
1495 (unsigned long long)a >> b,
1496 a >> b,
1497 a << b);
1498 printf("shiftc: %Ld %Ld %Ld\n",
1499 (unsigned long long)a >> 3,
1500 a >> 3,
1501 a << 3);
1502 printf("shiftc: %Ld %Ld %Ld\n",
1503 (unsigned long long)a >> 35,
1504 a >> 35,
1505 a << 35);
1508 void llfloat(void)
1510 float fa;
1511 double da;
1512 long double lda;
1513 long long la, lb, lc;
1514 unsigned long long ula, ulb, ulc;
1515 la = 0x12345678;
1516 ula = 0x72345678;
1517 la = (la << 20) | 0x12345;
1518 ula = ula << 33;
1519 printf("la=%Ld ula=%Lu\n", la, ula);
1521 fa = la;
1522 da = la;
1523 lda = la;
1524 printf("lltof: %f %f %Lf\n", fa, da, lda);
1526 la = fa;
1527 lb = da;
1528 lc = lda;
1529 printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
1531 fa = ula;
1532 da = ula;
1533 lda = ula;
1534 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1536 ula = fa;
1537 ulb = da;
1538 ulc = lda;
1539 printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
1542 long long llfunc1(int a)
1544 return a * 2;
1547 struct S {
1548 int id;
1549 char item;
1552 long long int value(struct S *v)
1554 return ((long long int)v->item);
1557 void longlong_test(void)
1559 long long a, b, c;
1560 int ia;
1561 unsigned int ua;
1562 printf("longlong_test:\n");
1563 printf("sizeof(long long) = %d\n", sizeof(long long));
1564 ia = -1;
1565 ua = -2;
1566 a = ia;
1567 b = ua;
1568 printf("%Ld %Ld\n", a, b);
1569 printf("%Ld %Ld %Ld %Lx\n",
1570 (long long)1,
1571 (long long)-2,
1572 1LL,
1573 0x1234567812345679);
1574 a = llfunc1(-3);
1575 printf("%Ld\n", a);
1577 lloptest(1000, 23);
1578 lloptest(0xff, 0x1234);
1579 b = 0x72345678 << 10;
1580 lloptest(-3, b);
1581 llshift(0x123, 5);
1582 llshift(-23, 5);
1583 b = 0x72345678LL << 10;
1584 llshift(b, 47);
1586 llfloat();
1587 #if 1
1588 b = 0x12345678;
1589 a = -1;
1590 c = a + b;
1591 printf("%Lx\n", c);
1592 #endif
1594 /* long long reg spill test */
1596 struct S a;
1598 a.item = 3;
1599 printf("%lld\n", value(&a));
1601 lloptest(0x80000000, 0);
1603 /* another long long spill test */
1605 long long *p, v;
1606 v = 1;
1607 p = &v;
1608 p[0]++;
1609 printf("%lld\n", *p);
1613 void vprintf1(const char *fmt, ...)
1615 va_list ap;
1616 const char *p;
1617 int c, i;
1618 double d;
1619 long long ll;
1621 va_start(ap, fmt);
1623 p = fmt;
1624 for(;;) {
1625 c = *p;
1626 if (c == '\0')
1627 break;
1628 p++;
1629 if (c == '%') {
1630 c = *p;
1631 switch(c) {
1632 case '\0':
1633 goto the_end;
1634 case 'd':
1635 i = va_arg(ap, int);
1636 printf("%d", i);
1637 break;
1638 case 'f':
1639 d = va_arg(ap, double);
1640 printf("%f", d);
1641 break;
1642 case 'l':
1643 ll = va_arg(ap, long long);
1644 printf("%Ld", ll);
1645 break;
1647 p++;
1648 } else {
1649 putchar(c);
1652 the_end:
1653 va_end(ap);
1657 void stdarg_test(void)
1659 vprintf1("%d %d %d\n", 1, 2, 3);
1660 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1661 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1664 void whitespace_test(void)
1666 char *str;
1668 \f\v #if 1
1669 pri\
1670 ntf("whitspace:\n");\f\v
1671 #endif
1672 pf("N=%d\n", 2);
1674 #ifdef CORRECT_CR_HANDLING
1675 pri\
1676 ntf("aaa=%d\n", 3);
1677 #endif
1679 pri\
1681 ntf("min=%d\n", 4);
1682 printf("len1=%d\n", strlen("
1683 "));
1684 #ifdef CORRECT_CR_HANDLING
1685 str = "
1687 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
1688 #endif
1689 printf("len1=%d\n", strlen(" a
1690 "));
1693 int reltab[3] = { 1, 2, 3 };
1695 int *rel1 = &reltab[1];
1696 int *rel2 = &reltab[2];
1698 void relocation_test(void)
1700 printf("*rel1=%d\n", *rel1);
1701 printf("*rel2=%d\n", *rel2);
1704 void old_style_f(a,b,c)
1705 int a, b;
1706 double c;
1708 printf("a=%d b=%d b=%f\n", a, b, c);
1711 void decl_func1(int cmpfn())
1713 printf("cmpfn=%lx\n", (long)cmpfn);
1716 void decl_func2(cmpfn)
1717 int cmpfn();
1719 printf("cmpfn=%lx\n", (long)cmpfn);
1722 void old_style_function(void)
1724 old_style_f((void *)1, 2, 3.0);
1725 decl_func1(NULL);
1726 decl_func2(NULL);
1729 void sizeof_test(void)
1731 int a;
1732 int **ptr;
1734 printf("sizeof(int) = %d\n", sizeof(int));
1735 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1736 printf("sizeof(short) = %d\n", sizeof(short));
1737 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1738 printf("sizeof(char) = %d\n", sizeof(char));
1739 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1740 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1741 a = 1;
1742 printf("sizeof(a++) = %d\n", sizeof a++);
1743 printf("a=%d\n", a);
1744 ptr = NULL;
1745 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
1747 /* some alignof tests */
1748 printf("__alignof__(int) = %d\n", __alignof__(int));
1749 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1750 printf("__alignof__(short) = %d\n", __alignof__(short));
1751 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1752 printf("__alignof__(char) = %d\n", __alignof__(char));
1753 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1754 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
1757 void typeof_test(void)
1759 double a;
1760 typeof(a) b;
1761 typeof(float) c;
1763 a = 1.5;
1764 b = 2.5;
1765 c = 3.5;
1766 printf("a=%f b=%f c=%f\n", a, b, c);
1769 void statement_expr_test(void)
1771 int a, i;
1773 a = 0;
1774 for(i=0;i<10;i++) {
1775 a += 1 +
1776 ( { int b, j;
1777 b = 0;
1778 for(j=0;j<5;j++)
1779 b += j; b;
1780 } );
1782 printf("a=%d\n", a);
1786 void local_label_test(void)
1788 int a;
1789 goto l1;
1791 a = 1 + ({
1792 __label__ l1, l2, l3;
1793 goto l4;
1795 printf("aa1\n");
1796 goto l1;
1798 printf("aa3\n");
1799 goto l3;
1801 printf("aa2\n");
1802 goto l2;
1803 l3:;
1806 printf("a=%d\n", a);
1807 return;
1809 printf("bb1\n");
1810 goto l2;
1812 printf("bb2\n");
1813 goto l5;
1816 /* inline assembler test */
1817 #ifdef __i386__
1819 /* from linux kernel */
1820 static char * strncat1(char * dest,const char * src,size_t count)
1822 int d0, d1, d2, d3;
1823 __asm__ __volatile__(
1824 "repne\n\t"
1825 "scasb\n\t"
1826 "decl %1\n\t"
1827 "movl %8,%3\n"
1828 "1:\tdecl %3\n\t"
1829 "js 2f\n\t"
1830 "lodsb\n\t"
1831 "stosb\n\t"
1832 "testb %%al,%%al\n\t"
1833 "jne 1b\n"
1834 "2:\txorl %2,%2\n\t"
1835 "stosb"
1836 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
1837 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
1838 : "memory");
1839 return dest;
1842 static inline void * memcpy1(void * to, const void * from, size_t n)
1844 int d0, d1, d2;
1845 __asm__ __volatile__(
1846 "rep ; movsl\n\t"
1847 "testb $2,%b4\n\t"
1848 "je 1f\n\t"
1849 "movsw\n"
1850 "1:\ttestb $1,%b4\n\t"
1851 "je 2f\n\t"
1852 "movsb\n"
1853 "2:"
1854 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
1855 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
1856 : "memory");
1857 return (to);
1860 static __inline__ void sigaddset1(unsigned int *set, int _sig)
1862 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
1865 static __inline__ void sigdelset1(unsigned int *set, int _sig)
1867 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
1870 static __inline__ __const__ unsigned int swab32(unsigned int x)
1872 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
1873 "rorl $16,%0\n\t" /* swap words */
1874 "xchgb %b0,%h0" /* swap higher bytes */
1875 :"=q" (x)
1876 : "0" (x));
1877 return x;
1880 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
1882 unsigned long long res;
1883 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
1884 return res;
1887 static __inline__ unsigned long long inc64(unsigned long long a)
1889 unsigned long long res;
1890 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
1891 return res;
1894 unsigned int set;
1896 void asm_test(void)
1898 char buf[128];
1899 unsigned int val;
1901 printf("inline asm:\n");
1902 /* test the no operand case */
1903 asm volatile ("xorl %eax, %eax");
1905 memcpy1(buf, "hello", 6);
1906 strncat1(buf, " worldXXXXX", 3);
1907 printf("%s\n", buf);
1909 /* 'A' constraint test */
1910 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
1911 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
1913 set = 0xff;
1914 sigdelset1(&set, 2);
1915 sigaddset1(&set, 16);
1916 /* NOTE: we test here if C labels are correctly restored after the
1917 asm statement */
1918 goto label1;
1919 label2:
1920 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
1921 printf("set=0x%x\n", set);
1922 val = 0x01020304;
1923 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
1924 return;
1925 label1:
1926 goto label2;
1929 #else
1931 void asm_test(void)
1935 #endif
1937 #define COMPAT_TYPE(type1, type2) \
1939 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
1940 __builtin_types_compatible_p (type1, type2));\
1943 int constant_p_var;
1945 void builtin_test(void)
1947 #if GCC_MAJOR >= 3
1948 COMPAT_TYPE(int, int);
1949 COMPAT_TYPE(int, unsigned int);
1950 COMPAT_TYPE(int, char);
1951 COMPAT_TYPE(int, const int);
1952 COMPAT_TYPE(int, volatile int);
1953 COMPAT_TYPE(int *, int *);
1954 COMPAT_TYPE(int *, void *);
1955 COMPAT_TYPE(int *, const int *);
1956 COMPAT_TYPE(char *, unsigned char *);
1957 /* space is needed because tcc preprocessor introduces a space between each token */
1958 COMPAT_TYPE(char * *, void *);
1959 #endif
1960 printf("res = %d\n", __builtin_constant_p(1));
1961 printf("res = %d\n", __builtin_constant_p(1 + 2));
1962 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
1963 printf("res = %d\n", __builtin_constant_p(constant_p_var));
1967 void const_func(const int a)
1971 void const_warn_test(void)
1973 const_func(1);