Use _WIN32 for a windows hosted tcc and define it for the PE target.
[tinycc/daniel.git] / tcctest.c
blobfca987d6cf7fbdab0b52c1dd04dc6582a3f3a761
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 #define spin_lock(lock) do { } while (0)
136 #define wq_spin_lock spin_lock
137 #define TEST2() wq_spin_lock(a)
139 void macro_test(void)
141 printf("macro:\n");\f\v
142 pf("N=%d\n", N);
143 printf("aaa=%d\n", AAA);
145 printf("min=%d\n", min(1, min(2, -1)));
147 printf("s1=%s\n", glue(HIGH, LOW));
148 printf("s2=%s\n", xglue(HIGH, LOW));
149 printf("s3=%s\n", str("c"));
150 printf("s4=%s\n", str(a1));
151 printf("B3=%d\n", B3);
153 #ifdef A
154 printf("A defined\n");
155 #endif
156 #ifdef B
157 printf("B defined\n");
158 #endif
159 #ifdef A
160 printf("A defined\n");
161 #else
162 printf("A not defined\n");
163 #endif
164 #ifdef B
165 printf("B defined\n");
166 #else
167 printf("B not defined\n");
168 #endif
170 #ifdef A
171 printf("A defined\n");
172 #ifdef B
173 printf("B1 defined\n");
174 #else
175 printf("B1 not defined\n");
176 #endif
177 #else
178 printf("A not defined\n");
179 #ifdef B
180 printf("B2 defined\n");
181 #else
182 printf("B2 not defined\n");
183 #endif
184 #endif
186 #if 1+1
187 printf("test true1\n");
188 #endif
189 #if 0
190 printf("test true2\n");
191 #endif
192 #if 1-1
193 printf("test true3\n");
194 #endif
195 #if defined(A)
196 printf("test trueA\n");
197 #endif
198 #if defined(B)
199 printf("test trueB\n");
200 #endif
202 #if 0
203 printf("test 0\n");
204 #elif 0
205 printf("test 1\n");
206 #elif 2
207 printf("test 2\n");
208 #else
209 printf("test 3\n");
210 #endif
212 MACRO_NOARGS();
214 #ifdef __LINE__
215 printf("__LINE__ defined\n");
216 #endif
218 printf("__LINE__=%d __FILE__=%s\n",
219 __LINE__, __FILE__);
220 #line 200
221 printf("__LINE__=%d __FILE__=%s\n",
222 __LINE__, __FILE__);
223 #line 203 "test"
224 printf("__LINE__=%d __FILE__=%s\n",
225 __LINE__, __FILE__);
226 #line 220 "tcctest.c"
228 /* not strictly preprocessor, but we test it there */
229 #ifdef C99_MACROS
230 printf("__func__ = %s\n", __func__);
231 dprintf(1, "vaarg=%d\n", 1);
232 #endif
233 dprintf1(1, "vaarg1\n");
234 dprintf1(1, "vaarg1=%d\n", 2);
235 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
237 /* gcc extension */
238 printf("func='%s'\n", __FUNCTION__);
240 /* complicated macros in glibc */
241 printf("INT64_MIN=%Ld\n", INT64_MIN);
243 int a;
244 a = 1;
245 glue(a+, +);
246 printf("a=%d\n", a);
247 glue(a <, <= 2);
248 printf("a=%d\n", a);
251 /* macro function with argument outside the macro string */
252 #define MF_s MF_hello
253 #define MF_hello(msg) printf("%s\n",msg)
255 #define MF_t printf("tralala\n"); MF_hello
257 MF_s("hi");
258 MF_t("hi");
260 /* test macro substituion inside args (should not eat stream) */
261 printf("qq=%d\n", qq(qq)(2));
263 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
264 null argument without a space. gcc 3.2 fixes that. */
266 #define qq1(x) 1
267 printf("qq1=%d\n", qq1( ));
269 /* comment with stray handling *\
271 /* this is a valid *\/ comment */
272 /* this is a valid comment *\*/
273 // this is a valid\
274 comment
276 /* test function macro substitution when the function name is
277 substituted */
278 TEST2();
281 int op(a,b)
283 return a / b;
286 int ret(a)
288 if (a == 2)
289 return 1;
290 if (a == 3)
291 return 2;
292 return 0;
295 void ps(const char *s)
297 int c;
298 while (1) {
299 c = *s;
300 if (c == 0)
301 break;
302 printf("%c", c);
303 s++;
307 const char foo1_string[] = "\
308 bar\n\
309 test\14\
312 void string_test()
314 int b;
315 printf("string:\n");
316 printf("\141\1423\143\n");/* dezdez test */
317 printf("\x41\x42\x43\x3a\n");
318 printf("c=%c\n", 'r');
319 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
320 printf("foo1_string='%s'\n", foo1_string);
321 #if 0
322 printf("wstring=%S\n", L"abc");
323 printf("wstring=%S\n", L"abc" L"def" "ghi");
324 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
325 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
326 #endif
327 ps("test\n");
328 b = 32;
329 while ((b = b + 1) < 96) {
330 printf("%c", b);
332 printf("\n");
333 printf("fib=%d\n", fib(33));
334 b = 262144;
335 while (b != 0x80000000) {
336 num(b);
337 b = b * 2;
341 void loop_test()
343 int i;
344 i = 0;
345 while (i < 10)
346 printf("%d", i++);
347 printf("\n");
348 for(i = 0; i < 10;i++)
349 printf("%d", i);
350 printf("\n");
351 i = 0;
352 do {
353 printf("%d", i++);
354 } while (i < 10);
355 printf("\n");
357 /* break/continue tests */
358 i = 0;
359 while (1) {
360 if (i == 6)
361 break;
362 i++;
363 if (i == 3)
364 continue;
365 printf("%d", i);
367 printf("\n");
369 /* break/continue tests */
370 i = 0;
371 do {
372 if (i == 6)
373 break;
374 i++;
375 if (i == 3)
376 continue;
377 printf("%d", i);
378 } while(1);
379 printf("\n");
381 for(i = 0;i < 10;i++) {
382 if (i == 3)
383 continue;
384 printf("%d", i);
386 printf("\n");
390 void goto_test()
392 int i;
393 static void *label_table[3] = { &&label1, &&label2, &&label3 };
395 printf("goto:\n");
396 i = 0;
397 s_loop:
398 if (i >= 10)
399 goto s_end;
400 printf("%d", i);
401 i++;
402 goto s_loop;
403 s_end:
404 printf("\n");
406 /* we also test computed gotos (GCC extension) */
407 for(i=0;i<3;i++) {
408 goto *label_table[i];
409 label1:
410 printf("label1\n");
411 goto next;
412 label2:
413 printf("label2\n");
414 goto next;
415 label3:
416 printf("label3\n");
417 next: ;
421 enum {
423 E1 = 2,
424 E2 = 4,
429 enum test {
430 E5 = 1000,
433 void enum_test()
435 enum test b1;
436 printf("enum:\n%d %d %d %d %d %d\n",
437 E0, E1, E2, E3, E4, E5);
438 b1 = 1;
439 printf("b1=%d\n", b1);
442 typedef int *my_ptr;
444 typedef int mytype1;
445 typedef int mytype2;
447 void typedef_test()
449 my_ptr a;
450 mytype1 mytype2;
451 int b;
453 a = &b;
454 *a = 1234;
455 printf("typedef:\n");
456 printf("a=%d\n", *a);
457 mytype2 = 2;
458 printf("mytype2=%d\n", mytype2);
461 void forward_test()
463 printf("forward:\n");
464 forward_ref();
465 forward_ref();
469 void forward_ref(void)
471 printf("forward ok\n");
474 typedef struct struct1 {
475 int f1;
476 int f2, f3;
477 union union1 {
478 int v1;
479 int v2;
480 } u;
481 char str[3];
482 } struct1;
484 struct struct2 {
485 int a;
486 char b;
489 union union2 {
490 int w1;
491 int w2;
494 struct struct1 st1, st2;
496 int main(int argc, char **argv)
498 string_test();
499 expr_test();
500 macro_test();
501 scope_test();
502 forward_test();
503 funcptr_test();
504 loop_test();
505 switch_test();
506 goto_test();
507 enum_test();
508 typedef_test();
509 struct_test();
510 array_test();
511 expr_ptr_test();
512 bool_test();
513 expr2_test();
514 constant_expr_test();
515 expr_cmp_test();
516 char_short_test();
517 init_test();
518 compound_literal_test();
519 kr_test();
520 struct_assign_test();
521 cast_test();
522 bitfield_test();
523 c99_bool_test();
524 float_test();
525 longlong_test();
526 stdarg_test();
527 whitespace_test();
528 relocation_test();
529 old_style_function();
530 sizeof_test();
531 typeof_test();
532 statement_expr_test();
533 local_label_test();
534 asm_test();
535 builtin_test();
536 return 0;
539 int tab[3];
540 int tab2[3][2];
542 int g;
544 void f1(g)
546 printf("g1=%d\n", g);
549 void scope_test()
551 printf("scope:\n");
552 g = 2;
553 f1(1);
554 printf("g2=%d\n", g);
556 int g;
557 g = 3;
558 printf("g3=%d\n", g);
560 int g;
561 g = 4;
562 printf("g4=%d\n", g);
565 printf("g5=%d\n", g);
568 void array_test(int a[4])
570 int i, j;
572 printf("array:\n");
573 printf("sizeof(a) = %d\n", sizeof(a));
574 printf("sizeof(\"a\") = %d\n", sizeof("a"));
575 #ifdef C99_MACROS
576 printf("sizeof(__func__) = %d\n", sizeof(__func__));
577 #endif
578 printf("sizeof tab %d\n", sizeof(tab));
579 printf("sizeof tab2 %d\n", sizeof tab2);
580 tab[0] = 1;
581 tab[1] = 2;
582 tab[2] = 3;
583 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
584 for(i=0;i<3;i++)
585 for(j=0;j<2;j++)
586 tab2[i][j] = 10 * i + j;
587 for(i=0;i<3*2;i++) {
588 printf(" %3d", ((int *)tab2)[i]);
590 printf("\n");
593 void expr_test()
595 int a, b;
596 a = 0;
597 printf("%d\n", a += 1);
598 printf("%d\n", a -= 2);
599 printf("%d\n", a *= 31232132);
600 printf("%d\n", a /= 4);
601 printf("%d\n", a %= 20);
602 printf("%d\n", a &= 6);
603 printf("%d\n", a ^= 7);
604 printf("%d\n", a |= 8);
605 printf("%d\n", a >>= 3);
606 printf("%d\n", a <<= 4);
608 a = 22321;
609 b = -22321;
610 printf("%d\n", a + 1);
611 printf("%d\n", a - 2);
612 printf("%d\n", a * 312);
613 printf("%d\n", a / 4);
614 printf("%d\n", b / 4);
615 printf("%d\n", (unsigned)b / 4);
616 printf("%d\n", a % 20);
617 printf("%d\n", b % 20);
618 printf("%d\n", (unsigned)b % 20);
619 printf("%d\n", a & 6);
620 printf("%d\n", a ^ 7);
621 printf("%d\n", a | 8);
622 printf("%d\n", a >> 3);
623 printf("%d\n", b >> 3);
624 printf("%d\n", (unsigned)b >> 3);
625 printf("%d\n", a << 4);
626 printf("%d\n", ~a);
627 printf("%d\n", -a);
628 printf("%d\n", +a);
630 printf("%d\n", 12 + 1);
631 printf("%d\n", 12 - 2);
632 printf("%d\n", 12 * 312);
633 printf("%d\n", 12 / 4);
634 printf("%d\n", 12 % 20);
635 printf("%d\n", 12 & 6);
636 printf("%d\n", 12 ^ 7);
637 printf("%d\n", 12 | 8);
638 printf("%d\n", 12 >> 2);
639 printf("%d\n", 12 << 4);
640 printf("%d\n", ~12);
641 printf("%d\n", -12);
642 printf("%d\n", +12);
643 printf("%d %d %d %d\n",
644 isid('a'),
645 isid('g'),
646 isid('T'),
647 isid('('));
650 int isid(int c)
652 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
655 /**********************/
657 int vstack[10], *vstack_ptr;
659 void vpush(int vt, int vc)
661 *vstack_ptr++ = vt;
662 *vstack_ptr++ = vc;
665 void vpop(int *ft, int *fc)
667 *fc = *--vstack_ptr;
668 *ft = *--vstack_ptr;
671 void expr2_test()
673 int a, b;
675 printf("expr2:\n");
676 vstack_ptr = vstack;
677 vpush(1432432, 2);
678 vstack_ptr[-2] &= ~0xffffff80;
679 vpop(&a, &b);
680 printf("res= %d %d\n", a, b);
683 void constant_expr_test()
685 int a;
686 printf("constant_expr:\n");
687 a = 3;
688 printf("%d\n", a * 16);
689 printf("%d\n", a * 1);
690 printf("%d\n", a + 0);
693 int tab4[10];
695 void expr_ptr_test()
697 int *p, *q;
699 printf("expr_ptr:\n");
700 p = tab4;
701 q = tab4 + 10;
702 printf("diff=%d\n", q - p);
703 p++;
704 printf("inc=%d\n", p - tab4);
705 p--;
706 printf("dec=%d\n", p - tab4);
707 ++p;
708 printf("inc=%d\n", p - tab4);
709 --p;
710 printf("dec=%d\n", p - tab4);
711 printf("add=%d\n", p + 3 - tab4);
712 printf("add=%d\n", 3 + p - tab4);
715 void expr_cmp_test()
717 int a, b;
718 printf("constant_expr:\n");
719 a = -1;
720 b = 1;
721 printf("%d\n", a == a);
722 printf("%d\n", a != a);
724 printf("%d\n", a < b);
725 printf("%d\n", a <= b);
726 printf("%d\n", a <= a);
727 printf("%d\n", b >= a);
728 printf("%d\n", a >= a);
729 printf("%d\n", b > a);
731 printf("%d\n", (unsigned)a < b);
732 printf("%d\n", (unsigned)a <= b);
733 printf("%d\n", (unsigned)a <= a);
734 printf("%d\n", (unsigned)b >= a);
735 printf("%d\n", (unsigned)a >= a);
736 printf("%d\n", (unsigned)b > a);
739 struct empty {
742 struct aligntest1 {
743 char a[10];
746 struct aligntest2 {
747 int a;
748 char b[10];
751 struct aligntest3 {
752 double a, b;
755 struct aligntest4 {
756 double a[0];
759 void struct_test()
761 struct1 *s;
762 union union2 u;
764 printf("struct:\n");
765 printf("sizes: %d %d %d %d\n",
766 sizeof(struct struct1),
767 sizeof(struct struct2),
768 sizeof(union union1),
769 sizeof(union union2));
770 st1.f1 = 1;
771 st1.f2 = 2;
772 st1.f3 = 3;
773 printf("st1: %d %d %d\n",
774 st1.f1, st1.f2, st1.f3);
775 st1.u.v1 = 1;
776 st1.u.v2 = 2;
777 printf("union1: %d\n", st1.u.v1);
778 u.w1 = 1;
779 u.w2 = 2;
780 printf("union2: %d\n", u.w1);
781 s = &st2;
782 s->f1 = 3;
783 s->f2 = 2;
784 s->f3 = 1;
785 printf("st2: %d %d %d\n",
786 s->f1, s->f2, s->f3);
787 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
789 /* align / size tests */
790 printf("aligntest1 sizeof=%d alignof=%d\n",
791 sizeof(struct aligntest1), __alignof__(struct aligntest1));
792 printf("aligntest2 sizeof=%d alignof=%d\n",
793 sizeof(struct aligntest2), __alignof__(struct aligntest2));
794 printf("aligntest3 sizeof=%d alignof=%d\n",
795 sizeof(struct aligntest3), __alignof__(struct aligntest3));
796 printf("aligntest4 sizeof=%d alignof=%d\n",
797 sizeof(struct aligntest4), __alignof__(struct aligntest4));
799 /* empty structures (GCC extension) */
800 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
801 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
804 /* XXX: depend on endianness */
805 void char_short_test()
807 int var1, var2;
809 printf("char_short:\n");
811 var1 = 0x01020304;
812 var2 = 0xfffefdfc;
813 printf("s8=%d %d\n",
814 *(char *)&var1, *(char *)&var2);
815 printf("u8=%d %d\n",
816 *(unsigned char *)&var1, *(unsigned char *)&var2);
817 printf("s16=%d %d\n",
818 *(short *)&var1, *(short *)&var2);
819 printf("u16=%d %d\n",
820 *(unsigned short *)&var1, *(unsigned short *)&var2);
821 printf("s32=%d %d\n",
822 *(int *)&var1, *(int *)&var2);
823 printf("u32=%d %d\n",
824 *(unsigned int *)&var1, *(unsigned int *)&var2);
825 *(char *)&var1 = 0x08;
826 printf("var1=%x\n", var1);
827 *(short *)&var1 = 0x0809;
828 printf("var1=%x\n", var1);
829 *(int *)&var1 = 0x08090a0b;
830 printf("var1=%x\n", var1);
833 /******************/
835 typedef struct Sym {
836 int v;
837 int t;
838 int c;
839 struct Sym *next;
840 struct Sym *prev;
841 } Sym;
843 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
844 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
846 static int toupper1(int a)
848 return TOUPPER(a);
851 void bool_test()
853 int *s, a, b, t, f, i;
855 a = 0;
856 s = (void*)0;
857 printf("!s=%d\n", !s);
859 if (!s || !s[0])
860 a = 1;
861 printf("a=%d\n", a);
863 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
864 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
865 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
866 #if 1 && 1
867 printf("a1\n");
868 #endif
869 #if 1 || 0
870 printf("a2\n");
871 #endif
872 #if 1 ? 0 : 1
873 printf("a3\n");
874 #endif
875 #if 0 ? 0 : 1
876 printf("a4\n");
877 #endif
879 a = 4;
880 printf("b=%d\n", a + (0 ? 1 : a / 2));
882 /* test register spilling */
883 a = 10;
884 b = 10;
885 a = (a + b) * ((a < b) ?
886 ((b - a) * (a - b)): a + b);
887 printf("a=%d\n", a);
889 /* test complex || or && expressions */
890 t = 1;
891 f = 0;
892 a = 32;
893 printf("exp=%d\n", f == (32 <= a && a <= 3));
894 printf("r=%d\n", (t || f) + (t && f));
896 /* test ? : cast */
898 int aspect_on;
899 int aspect_native = 65536;
900 double bfu_aspect = 1.0;
901 int aspect;
902 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
903 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
904 printf("aspect=%d\n", aspect);
908 /* test ? : GCC extension */
910 static int v1 = 34 ? : -1; /* constant case */
911 static int v2 = 0 ? : -1; /* constant case */
912 int a = 30;
914 printf("%d %d\n", v1, v2);
915 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
918 /* again complex expression */
919 for(i=0;i<256;i++) {
920 if (toupper1 (i) != TOUPPER (i))
921 printf("error %d\n", i);
925 /* GCC accepts that */
926 static int tab_reinit[];
927 static int tab_reinit[10];
929 //int cinit1; /* a global variable can be defined several times without error ! */
930 int cinit1;
931 int cinit1;
932 int cinit1 = 0;
933 int *cinit2 = (int []){3, 2, 1};
935 void compound_literal_test(void)
937 int *p, i;
938 char *q, *q3;
940 printf("compound_test:\n");
942 p = (int []){1, 2, 3};
943 for(i=0;i<3;i++)
944 printf(" %d", p[i]);
945 printf("\n");
947 for(i=0;i<3;i++)
948 printf("%d", cinit2[i]);
949 printf("\n");
951 q = "tralala1";
952 printf("q1=%s\n", q);
954 q = (char *){ "tralala2" };
955 printf("q2=%s\n", q);
957 q3 = (char *){ q };
958 printf("q3=%s\n", q3);
960 q = (char []){ "tralala3" };
961 printf("q4=%s\n", q);
963 #ifdef ALL_ISOC99
964 p = (int []){1, 2, cinit1 + 3};
965 for(i=0;i<3;i++)
966 printf(" %d", p[i]);
967 printf("\n");
969 for(i=0;i<3;i++) {
970 p = (int []){1, 2, 4 + i};
971 printf("%d %d %d\n",
972 p[0],
973 p[1],
974 p[2]);
976 #endif
979 /* K & R protos */
981 kr_func1(a, b)
983 return a + b;
986 int kr_func2(a, b)
988 return a + b;
991 kr_test()
993 printf("kr_test:\n");
994 printf("func1=%d\n", kr_func1(3, 4));
995 printf("func2=%d\n", kr_func2(3, 4));
996 return 0;
999 void num(int n)
1001 char *tab, *p;
1002 tab = (char*)malloc(20);
1003 p = tab;
1004 while (1) {
1005 *p = 48 + (n % 10);
1006 p++;
1007 n = n / 10;
1008 if (n == 0)
1009 break;
1011 while (p != tab) {
1012 p--;
1013 printf("%c", *p);
1015 printf("\n");
1018 /* structure assignment tests */
1019 struct structa1 {
1020 int f1;
1021 char f2;
1024 struct structa1 ssta1;
1026 void struct_assign_test1(struct structa1 s1, int t)
1028 printf("%d %d %d\n", s1.f1, s1.f2, t);
1031 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1033 s1.f1 += t;
1034 s1.f2 -= t;
1035 return s1;
1038 void struct_assign_test(void)
1040 struct structa1 lsta1, lsta2;
1042 #if 0
1043 printf("struct_assign_test:\n");
1045 lsta1.f1 = 1;
1046 lsta1.f2 = 2;
1047 printf("%d %d\n", lsta1.f1, lsta1.f2);
1048 lsta2 = lsta1;
1049 printf("%d %d\n", lsta2.f1, lsta2.f2);
1050 #else
1051 lsta2.f1 = 1;
1052 lsta2.f2 = 2;
1053 #endif
1054 struct_assign_test1(lsta2, 3);
1056 printf("before call: %d %d\n", lsta2.f1, lsta2.f2);
1057 lsta2 = struct_assign_test2(lsta2, 4);
1058 printf("after call: %d %d\n", lsta2.f1, lsta2.f2);
1061 /* casts to short/char */
1063 void cast1(char a, short b, unsigned char c, unsigned short d)
1065 printf("%d %d %d %d\n", a, b, c, d);
1068 char bcast;
1069 short scast;
1071 void cast_test()
1073 int a;
1074 char c;
1075 char tab[10];
1076 unsigned b,d;
1077 short s;
1079 printf("cast_test:\n");
1080 a = 0xfffff;
1081 cast1(a, a, a, a);
1082 a = 0xffffe;
1083 printf("%d %d %d %d\n",
1084 (char)(a + 1),
1085 (short)(a + 1),
1086 (unsigned char)(a + 1),
1087 (unsigned short)(a + 1));
1088 printf("%d %d %d %d\n",
1089 (char)0xfffff,
1090 (short)0xfffff,
1091 (unsigned char)0xfffff,
1092 (unsigned short)0xfffff);
1094 a = (bcast = 128) + 1;
1095 printf("%d\n", a);
1096 a = (scast = 65536) + 1;
1097 printf("%d\n", a);
1099 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1101 /* test cast from unsigned to signed short to int */
1102 b = 0xf000;
1103 d = (short)b;
1104 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1105 b = 0xf0f0;
1106 d = (char)b;
1107 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1109 /* test implicit int casting for array accesses */
1110 c = 0;
1111 tab[1] = 2;
1112 tab[c] = 1;
1113 printf("%d %d\n", tab[0], tab[1]);
1115 /* test implicit casting on some operators */
1116 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1117 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1118 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1121 /* initializers tests */
1122 struct structinit1 {
1123 int f1;
1124 char f2;
1125 short f3;
1126 int farray[3];
1129 int sinit1 = 2;
1130 int sinit2 = { 3 };
1131 int sinit3[3] = { 1, 2, {{3}}, };
1132 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1133 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1134 int sinit6[] = { 1, 2, 3 };
1135 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1136 char sinit8[] = "hello" "trala";
1138 struct structinit1 sinit9 = { 1, 2, 3 };
1139 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1140 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1141 #ifdef ALL_ISOC99
1142 .farray[0] = 10,
1143 .farray[1] = 11,
1144 .farray[2] = 12,
1145 #endif
1148 char *sinit12 = "hello world";
1149 char *sinit13[] = {
1150 "test1",
1151 "test2",
1152 "test3",
1154 char sinit14[10] = { "abc" };
1155 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1157 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1159 struct bar {
1160 char *s;
1161 int len;
1162 } sinit17[] = {
1163 "a1", 4,
1164 "a2", 1
1167 int sinit18[10] = {
1168 [2 ... 5] = 20,
1170 [8] = 10,
1173 void init_test(void)
1175 int linit1 = 2;
1176 int linit2 = { 3 };
1177 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1178 int linit6[] = { 1, 2, 3 };
1179 int i, j;
1180 char linit8[] = "hello" "trala";
1181 int linit12[10] = { 1, 2 };
1182 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1183 char linit14[10] = "abc";
1184 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1185 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1186 int linit17 = sizeof(linit17);
1188 printf("init_test:\n");
1190 printf("sinit1=%d\n", sinit1);
1191 printf("sinit2=%d\n", sinit2);
1192 printf("sinit3=%d %d %d %d\n",
1193 sizeof(sinit3),
1194 sinit3[0],
1195 sinit3[1],
1196 sinit3[2]
1198 printf("sinit6=%d\n", sizeof(sinit6));
1199 printf("sinit7=%d %d %d %d\n",
1200 sizeof(sinit7),
1201 sinit7[0],
1202 sinit7[1],
1203 sinit7[2]
1205 printf("sinit8=%s\n", sinit8);
1206 printf("sinit9=%d %d %d\n",
1207 sinit9.f1,
1208 sinit9.f2,
1209 sinit9.f3
1211 printf("sinit10=%d %d %d\n",
1212 sinit10.f1,
1213 sinit10.f2,
1214 sinit10.f3
1216 printf("sinit11=%d %d %d %d %d %d\n",
1217 sinit11.f1,
1218 sinit11.f2,
1219 sinit11.f3,
1220 sinit11.farray[0],
1221 sinit11.farray[1],
1222 sinit11.farray[2]
1225 for(i=0;i<3;i++)
1226 for(j=0;j<2;j++)
1227 printf("[%d][%d] = %d %d %d\n",
1228 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1229 printf("linit1=%d\n", linit1);
1230 printf("linit2=%d\n", linit2);
1231 printf("linit6=%d\n", sizeof(linit6));
1232 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1234 printf("sinit12=%s\n", sinit12);
1235 printf("sinit13=%d %s %s %s\n",
1236 sizeof(sinit13),
1237 sinit13[0],
1238 sinit13[1],
1239 sinit13[2]);
1240 printf("sinit14=%s\n", sinit14);
1242 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1243 printf("\n");
1244 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1245 printf("\n");
1246 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1247 printf("\n");
1248 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1249 printf("\n");
1250 printf("%d %d %d %d\n",
1251 linit16.a1,
1252 linit16.a2,
1253 linit16.a3,
1254 linit16.a4);
1255 /* test that initialisation is done after variable declare */
1256 printf("linit17=%d\n", linit17);
1257 printf("sinit15=%d\n", sinit15[0]);
1258 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1259 printf("sinit17=%s %d %s %d\n",
1260 sinit17[0].s, sinit17[0].len,
1261 sinit17[1].s, sinit17[1].len);
1262 for(i=0;i<10;i++)
1263 printf("%x ", sinit18[i]);
1264 printf("\n");
1268 void switch_test()
1270 int i;
1272 for(i=0;i<15;i++) {
1273 switch(i) {
1274 case 0:
1275 case 1:
1276 printf("a");
1277 break;
1278 default:
1279 printf("%d", i);
1280 break;
1281 case 8 ... 12:
1282 printf("c");
1283 break;
1284 case 3:
1285 printf("b");
1286 break;
1289 printf("\n");
1292 /* ISOC99 _Bool type */
1293 void c99_bool_test(void)
1295 #ifdef BOOL_ISOC99
1296 int a;
1297 _Bool b;
1299 printf("bool_test:\n");
1300 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1301 a = 3;
1302 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1303 b = 3;
1304 printf("b = %d\n", b);
1305 b++;
1306 printf("b = %d\n", b);
1307 #endif
1310 void bitfield_test(void)
1312 int a;
1313 struct sbf1 {
1314 int f1 : 3;
1315 int : 2;
1316 int f2 : 1;
1317 int : 0;
1318 int f3 : 5;
1319 int f4 : 7;
1320 unsigned int f5 : 7;
1321 } st1;
1322 printf("bitfield_test:");
1323 printf("sizeof(st1) = %d\n", sizeof(st1));
1325 st1.f1 = 3;
1326 st1.f2 = 1;
1327 st1.f3 = 15;
1328 a = 120;
1329 st1.f4 = a;
1330 st1.f5 = a;
1331 st1.f5++;
1332 printf("%d %d %d %d %d\n",
1333 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1335 st1.f1 = 7;
1336 if (st1.f1 == -1)
1337 printf("st1.f1 == -1\n");
1338 else
1339 printf("st1.f1 != -1\n");
1340 if (st1.f2 == -1)
1341 printf("st1.f2 == -1\n");
1342 else
1343 printf("st1.f2 != -1\n");
1346 #define FTEST(prefix, type, fmt)\
1347 void prefix ## cmp(type a, type b)\
1349 printf("%d %d %d %d %d %d\n",\
1350 a == b,\
1351 a != b,\
1352 a < b,\
1353 a > b,\
1354 a >= b,\
1355 a <= b);\
1356 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1359 a + b,\
1360 a - b,\
1361 a * b,\
1362 a / b,\
1363 -a);\
1364 printf(fmt "\n", ++a);\
1365 printf(fmt "\n", a++);\
1366 printf(fmt "\n", a);\
1368 void prefix ## fcast(type a)\
1370 float fa;\
1371 double da;\
1372 long double la;\
1373 int ia;\
1374 unsigned int ua;\
1375 type b;\
1376 fa = a;\
1377 da = a;\
1378 la = a;\
1379 printf("ftof: %f %f %Lf\n", fa, da, la);\
1380 ia = (int)a;\
1381 ua = (unsigned int)a;\
1382 printf("ftoi: %d %u\n", ia, ua);\
1383 ia = -1234;\
1384 ua = 0x81234500;\
1385 b = ia;\
1386 printf("itof: " fmt "\n", b);\
1387 b = ua;\
1388 printf("utof: " fmt "\n", b);\
1391 void prefix ## test(void)\
1393 printf("testing '%s'\n", #type);\
1394 prefix ## cmp(1, 2.5);\
1395 prefix ## cmp(2, 1.5);\
1396 prefix ## cmp(1, 1);\
1397 prefix ## fcast(234.6);\
1398 prefix ## fcast(-2334.6);\
1401 FTEST(f, float, "%f")
1402 FTEST(d, double, "%f")
1403 FTEST(ld, long double, "%Lf")
1405 double ftab1[3] = { 1.2, 3.4, -5.6 };
1408 void float_test(void)
1410 float fa, fb;
1411 double da, db;
1412 int a;
1413 unsigned int b;
1415 printf("float_test:\n");
1416 printf("sizeof(float) = %d\n", sizeof(float));
1417 printf("sizeof(double) = %d\n", sizeof(double));
1418 printf("sizeof(long double) = %d\n", sizeof(long double));
1419 ftest();
1420 dtest();
1421 ldtest();
1422 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1423 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1424 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1425 da = 123;
1426 printf("da=%f\n", da);
1427 fa = 123;
1428 printf("fa=%f\n", fa);
1429 a = 4000000000;
1430 da = a;
1431 printf("da = %f\n", da);
1432 b = 4000000000;
1433 db = b;
1434 printf("db = %f\n", db);
1437 int fib(int n)
1439 if (n <= 2)
1440 return 1;
1441 else
1442 return fib(n-1) + fib(n-2);
1445 void funcptr_test()
1447 void (*func)(int);
1448 int a;
1449 struct {
1450 int dummy;
1451 void (*func)(int);
1452 } st1;
1454 printf("funcptr:\n");
1455 func = &num;
1456 (*func)(12345);
1457 func = num;
1458 a = 1;
1459 a = 1;
1460 func(12345);
1461 /* more complicated pointer computation */
1462 st1.func = num;
1463 st1.func(12346);
1464 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1465 printf("sizeof2 = %d\n", sizeof funcptr_test);
1466 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1467 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1470 void lloptest(long long a, long long b)
1472 unsigned long long ua, ub;
1474 ua = a;
1475 ub = b;
1476 /* arith */
1477 printf("arith: %Ld %Ld %Ld\n",
1478 a + b,
1479 a - b,
1480 a * b);
1482 if (b != 0) {
1483 printf("arith1: %Ld %Ld\n",
1484 a / b,
1485 a % b);
1488 /* binary */
1489 printf("bin: %Ld %Ld %Ld\n",
1490 a & b,
1491 a | b,
1492 a ^ b);
1494 /* tests */
1495 printf("test: %d %d %d %d %d %d\n",
1496 a == b,
1497 a != b,
1498 a < b,
1499 a > b,
1500 a >= b,
1501 a <= b);
1503 printf("utest: %d %d %d %d %d %d\n",
1504 ua == ub,
1505 ua != ub,
1506 ua < ub,
1507 ua > ub,
1508 ua >= ub,
1509 ua <= ub);
1511 /* arith2 */
1512 a++;
1513 b++;
1514 printf("arith2: %Ld %Ld\n", a, b);
1515 printf("arith2: %Ld %Ld\n", a++, b++);
1516 printf("arith2: %Ld %Ld\n", --a, --b);
1517 printf("arith2: %Ld %Ld\n", a, b);
1520 void llshift(long long a, int b)
1522 printf("shift: %Ld %Ld %Ld\n",
1523 (unsigned long long)a >> b,
1524 a >> b,
1525 a << b);
1526 printf("shiftc: %Ld %Ld %Ld\n",
1527 (unsigned long long)a >> 3,
1528 a >> 3,
1529 a << 3);
1530 printf("shiftc: %Ld %Ld %Ld\n",
1531 (unsigned long long)a >> 35,
1532 a >> 35,
1533 a << 35);
1536 void llfloat(void)
1538 float fa;
1539 double da;
1540 long double lda;
1541 long long la, lb, lc;
1542 unsigned long long ula, ulb, ulc;
1543 la = 0x12345678;
1544 ula = 0x72345678;
1545 la = (la << 20) | 0x12345;
1546 ula = ula << 33;
1547 printf("la=%Ld ula=%Lu\n", la, ula);
1549 fa = la;
1550 da = la;
1551 lda = la;
1552 printf("lltof: %f %f %Lf\n", fa, da, lda);
1554 la = fa;
1555 lb = da;
1556 lc = lda;
1557 printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
1559 fa = ula;
1560 da = ula;
1561 lda = ula;
1562 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1564 ula = fa;
1565 ulb = da;
1566 ulc = lda;
1567 printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
1570 long long llfunc1(int a)
1572 return a * 2;
1575 struct S {
1576 int id;
1577 char item;
1580 long long int value(struct S *v)
1582 return ((long long int)v->item);
1585 void longlong_test(void)
1587 long long a, b, c;
1588 int ia;
1589 unsigned int ua;
1590 printf("longlong_test:\n");
1591 printf("sizeof(long long) = %d\n", sizeof(long long));
1592 ia = -1;
1593 ua = -2;
1594 a = ia;
1595 b = ua;
1596 printf("%Ld %Ld\n", a, b);
1597 printf("%Ld %Ld %Ld %Lx\n",
1598 (long long)1,
1599 (long long)-2,
1600 1LL,
1601 0x1234567812345679);
1602 a = llfunc1(-3);
1603 printf("%Ld\n", a);
1605 lloptest(1000, 23);
1606 lloptest(0xff, 0x1234);
1607 b = 0x72345678 << 10;
1608 lloptest(-3, b);
1609 llshift(0x123, 5);
1610 llshift(-23, 5);
1611 b = 0x72345678LL << 10;
1612 llshift(b, 47);
1614 llfloat();
1615 #if 1
1616 b = 0x12345678;
1617 a = -1;
1618 c = a + b;
1619 printf("%Lx\n", c);
1620 #endif
1622 /* long long reg spill test */
1624 struct S a;
1626 a.item = 3;
1627 printf("%lld\n", value(&a));
1629 lloptest(0x80000000, 0);
1631 /* another long long spill test */
1633 long long *p, v;
1634 v = 1;
1635 p = &v;
1636 p[0]++;
1637 printf("%lld\n", *p);
1641 void vprintf1(const char *fmt, ...)
1643 va_list ap;
1644 const char *p;
1645 int c, i;
1646 double d;
1647 long long ll;
1649 va_start(ap, fmt);
1651 p = fmt;
1652 for(;;) {
1653 c = *p;
1654 if (c == '\0')
1655 break;
1656 p++;
1657 if (c == '%') {
1658 c = *p;
1659 switch(c) {
1660 case '\0':
1661 goto the_end;
1662 case 'd':
1663 i = va_arg(ap, int);
1664 printf("%d", i);
1665 break;
1666 case 'f':
1667 d = va_arg(ap, double);
1668 printf("%f", d);
1669 break;
1670 case 'l':
1671 ll = va_arg(ap, long long);
1672 printf("%Ld", ll);
1673 break;
1675 p++;
1676 } else {
1677 putchar(c);
1680 the_end:
1681 va_end(ap);
1685 void stdarg_test(void)
1687 vprintf1("%d %d %d\n", 1, 2, 3);
1688 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1689 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1692 void whitespace_test(void)
1694 char *str;
1696 \f\v #if 1
1697 pri\
1698 ntf("whitspace:\n");\f\v
1699 #endif
1700 pf("N=%d\n", 2);
1702 #ifdef CORRECT_CR_HANDLING
1703 pri\
1704 ntf("aaa=%d\n", 3);
1705 #endif
1707 pri\
1709 ntf("min=%d\n", 4);
1711 #ifdef ACCEPT_CR_IN_STRINGS
1712 printf("len1=%d\n", strlen("
1713 "));
1714 #ifdef CORRECT_CR_HANDLING
1715 str = "
1717 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
1718 #endif
1719 printf("len1=%d\n", strlen(" a
1720 "));
1721 #endif /* ACCEPT_CR_IN_STRINGS */
1724 int reltab[3] = { 1, 2, 3 };
1726 int *rel1 = &reltab[1];
1727 int *rel2 = &reltab[2];
1729 void relocation_test(void)
1731 printf("*rel1=%d\n", *rel1);
1732 printf("*rel2=%d\n", *rel2);
1735 void old_style_f(a,b,c)
1736 int a, b;
1737 double c;
1739 printf("a=%d b=%d b=%f\n", a, b, c);
1742 void decl_func1(int cmpfn())
1744 printf("cmpfn=%lx\n", (long)cmpfn);
1747 void decl_func2(cmpfn)
1748 int cmpfn();
1750 printf("cmpfn=%lx\n", (long)cmpfn);
1753 void old_style_function(void)
1755 old_style_f((void *)1, 2, 3.0);
1756 decl_func1(NULL);
1757 decl_func2(NULL);
1760 void sizeof_test(void)
1762 int a;
1763 int **ptr;
1765 printf("sizeof(int) = %d\n", sizeof(int));
1766 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1767 printf("sizeof(short) = %d\n", sizeof(short));
1768 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1769 printf("sizeof(char) = %d\n", sizeof(char));
1770 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1771 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1772 a = 1;
1773 printf("sizeof(a++) = %d\n", sizeof a++);
1774 printf("a=%d\n", a);
1775 ptr = NULL;
1776 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
1778 /* some alignof tests */
1779 printf("__alignof__(int) = %d\n", __alignof__(int));
1780 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1781 printf("__alignof__(short) = %d\n", __alignof__(short));
1782 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1783 printf("__alignof__(char) = %d\n", __alignof__(char));
1784 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1785 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
1788 void typeof_test(void)
1790 double a;
1791 typeof(a) b;
1792 typeof(float) c;
1794 a = 1.5;
1795 b = 2.5;
1796 c = 3.5;
1797 printf("a=%f b=%f c=%f\n", a, b, c);
1800 void statement_expr_test(void)
1802 int a, i;
1804 a = 0;
1805 for(i=0;i<10;i++) {
1806 a += 1 +
1807 ( { int b, j;
1808 b = 0;
1809 for(j=0;j<5;j++)
1810 b += j; b;
1811 } );
1813 printf("a=%d\n", a);
1817 void local_label_test(void)
1819 int a;
1820 goto l1;
1822 a = 1 + ({
1823 __label__ l1, l2, l3, l4;
1824 goto l1;
1826 printf("aa1\n");
1827 goto l3;
1829 printf("aa3\n");
1830 goto l4;
1832 printf("aa2\n");
1833 goto l2;
1834 l3:;
1837 printf("a=%d\n", a);
1838 return;
1840 printf("bb1\n");
1841 goto l2;
1843 printf("bb2\n");
1844 goto l4;
1847 /* inline assembler test */
1848 #ifdef __i386__
1850 /* from linux kernel */
1851 static char * strncat1(char * dest,const char * src,size_t count)
1853 int d0, d1, d2, d3;
1854 __asm__ __volatile__(
1855 "repne\n\t"
1856 "scasb\n\t"
1857 "decl %1\n\t"
1858 "movl %8,%3\n"
1859 "1:\tdecl %3\n\t"
1860 "js 2f\n\t"
1861 "lodsb\n\t"
1862 "stosb\n\t"
1863 "testb %%al,%%al\n\t"
1864 "jne 1b\n"
1865 "2:\txorl %2,%2\n\t"
1866 "stosb"
1867 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
1868 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
1869 : "memory");
1870 return dest;
1873 static inline void * memcpy1(void * to, const void * from, size_t n)
1875 int d0, d1, d2;
1876 __asm__ __volatile__(
1877 "rep ; movsl\n\t"
1878 "testb $2,%b4\n\t"
1879 "je 1f\n\t"
1880 "movsw\n"
1881 "1:\ttestb $1,%b4\n\t"
1882 "je 2f\n\t"
1883 "movsb\n"
1884 "2:"
1885 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
1886 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
1887 : "memory");
1888 return (to);
1891 static __inline__ void sigaddset1(unsigned int *set, int _sig)
1893 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
1896 static __inline__ void sigdelset1(unsigned int *set, int _sig)
1898 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
1901 static __inline__ __const__ unsigned int swab32(unsigned int x)
1903 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
1904 "rorl $16,%0\n\t" /* swap words */
1905 "xchgb %b0,%h0" /* swap higher bytes */
1906 :"=q" (x)
1907 : "0" (x));
1908 return x;
1911 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
1913 unsigned long long res;
1914 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
1915 return res;
1918 static __inline__ unsigned long long inc64(unsigned long long a)
1920 unsigned long long res;
1921 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
1922 return res;
1925 unsigned int set;
1927 void asm_test(void)
1929 char buf[128];
1930 unsigned int val;
1932 printf("inline asm:\n");
1933 /* test the no operand case */
1934 asm volatile ("xorl %eax, %eax");
1936 memcpy1(buf, "hello", 6);
1937 strncat1(buf, " worldXXXXX", 3);
1938 printf("%s\n", buf);
1940 /* 'A' constraint test */
1941 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
1942 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
1944 set = 0xff;
1945 sigdelset1(&set, 2);
1946 sigaddset1(&set, 16);
1947 /* NOTE: we test here if C labels are correctly restored after the
1948 asm statement */
1949 goto label1;
1950 label2:
1951 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
1952 printf("set=0x%x\n", set);
1953 val = 0x01020304;
1954 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
1955 return;
1956 label1:
1957 goto label2;
1960 #else
1962 void asm_test(void)
1966 #endif
1968 #define COMPAT_TYPE(type1, type2) \
1970 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
1971 __builtin_types_compatible_p (type1, type2));\
1974 int constant_p_var;
1976 void builtin_test(void)
1978 #if GCC_MAJOR >= 3
1979 COMPAT_TYPE(int, int);
1980 COMPAT_TYPE(int, unsigned int);
1981 COMPAT_TYPE(int, char);
1982 COMPAT_TYPE(int, const int);
1983 COMPAT_TYPE(int, volatile int);
1984 COMPAT_TYPE(int *, int *);
1985 COMPAT_TYPE(int *, void *);
1986 COMPAT_TYPE(int *, const int *);
1987 COMPAT_TYPE(char *, unsigned char *);
1988 /* space is needed because tcc preprocessor introduces a space between each token */
1989 COMPAT_TYPE(char * *, void *);
1990 #endif
1991 printf("res = %d\n", __builtin_constant_p(1));
1992 printf("res = %d\n", __builtin_constant_p(1 + 2));
1993 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
1994 printf("res = %d\n", __builtin_constant_p(constant_p_var));
1998 void const_func(const int a)
2002 void const_warn_test(void)
2004 const_func(1);