2 * TCC auto test program
11 /* Unfortunately, gcc version < 3 does not handle that! */
14 /* only gcc 3 handles _Bool correctly */
17 /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
18 /* Matze: removed this test as I couldn't committ it to svn... */
19 /* #define CORRECT_CR_HANDLING */
23 /* deprecated and no longer supported in gcc 3.3 */
24 //#define ACCEPT_CR_IN_STRINGS
26 /* __VA_ARGS__ and __func__ support */
29 /* test various include syntaxes */
31 #define TCCLIB_INC <tcclib.h>
32 #define TCCLIB_INC1 <tcclib
33 #define TCCLIB_INC2 h>
34 #define TCCLIB_INC3 "tcclib"
38 #include TCCLIB_INC1.TCCLIB_INC2
40 #include TCCLIB_INC1.h>
42 /* gcc 3.2 does not accept that (bug ?) */
43 //#include TCCLIB_INC3 ".h"
65 void constant_expr_test();
67 void char_short_test();
69 void compound_literal_test(void);
71 void struct_assign_test(void);
73 void bitfield_test(void);
74 void c99_bool_test(void);
75 void float_test(void);
76 void longlong_test(void);
77 void stdarg_test(void);
78 void whitespace_test(void);
79 void relocation_test(void);
80 void old_style_function(void);
81 void sizeof_test(void);
82 void typeof_test(void);
83 void local_label_test(void);
84 void statement_expr_test(void);
86 void builtin_test(void);
87 void static_test(void);
91 void forward_ref(void);
97 #define M1(a, b) (a) + (b)
101 #define glue(a, b) a ## b
102 #define xglue(a, b) glue(a, b)
103 #define HIGHLOW "hello"
104 #define LOW LOW ", world"
106 #define min(a, b) ((a) < (b) ? (a) : (b))
109 #define dprintf(level,...) printf(__VA_ARGS__)
112 /* gcc vararg macros */
113 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
115 #define MACRO_NOARGS()
131 #define __INT64_C(c) c ## LL
132 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
140 #define spin_lock(lock) do { } while (0)
141 #define wq_spin_lock spin_lock
142 #define TEST2() wq_spin_lock(a)
144 void macro_test(void)
146 printf("macro:\n");
\f\v
148 printf("aaa=%d\n", AAA);
150 printf("min=%d\n", min(1, min(2, -1)));
152 printf("s1=%s\n", glue(HIGH, LOW));
153 printf("s2=%s\n", xglue(HIGH, LOW));
154 printf("s3=%s\n", str("c"));
155 printf("s4=%s\n", str(a1));
156 printf("B3=%d\n", B3);
159 printf("A defined\n");
162 printf("B defined\n");
165 printf("A defined\n");
167 printf("A not defined\n");
170 printf("B defined\n");
172 printf("B not defined\n");
176 printf("A defined\n");
178 printf("B1 defined\n");
180 printf("B1 not defined\n");
183 printf("A not defined\n");
185 printf("B2 defined\n");
187 printf("B2 not defined\n");
192 printf("test true1\n");
195 printf("test true2\n");
198 printf("test true3\n");
201 printf("test trueA\n");
204 printf("test trueB\n");
220 printf("__LINE__ defined\n");
223 printf("__LINE__=%d __FILE__=%s\n",
226 printf("__LINE__=%d __FILE__=%s\n",
229 printf("__LINE__=%d __FILE__=%s\n",
231 #line 220 "tcctest.c"
233 /* not strictly preprocessor, but we test it there */
235 printf("__func__ = %s\n", __func__);
236 dprintf(1, "vaarg=%d\n", 1);
238 dprintf1(1, "vaarg1\n");
239 dprintf1(1, "vaarg1=%d\n", 2);
240 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
243 printf("func='%s'\n", __FUNCTION__);
245 /* complicated macros in glibc */
246 printf("INT64_MIN=%Ld\n", INT64_MIN);
256 /* macro function with argument outside the macro string */
257 #define MF_s MF_hello
258 #define MF_hello(msg) printf("%s\n",msg)
260 #define MF_t printf("tralala\n"); MF_hello
265 /* test macro substituion inside args (should not eat stream) */
266 printf("qq=%d\n", qq(qq)(2));
268 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
269 null argument without a space. gcc 3.2 fixes that. */
272 printf("qq1=%d\n", qq1( ));
274 /* comment with stray handling *\
276 /* this is a valid *\/ comment */
277 /* this is a valid comment *\*/
281 /* test function macro substitution when the function name is
285 #define PART_ONE(n) (!!((n) & 0xF))
286 #define PART_TWO(n) (PART_ONE(n))
287 #define PART_THREE(f) PART_TWO(0x##f##U)
288 printf("Nested macros: %d == 0\n", PART_THREE(0));
316 void ps(const char *s)
328 const char foo1_string[] = "\
337 printf("\141\1423\143\n");/* dezdez test */
338 printf("\x41\x42\x43\x3a\n");
339 printf("c=%c\n", 'r');
340 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
341 printf("foo1_string='%s'\n", foo1_string);
343 printf("wstring=%S\n", L"abc");
344 printf("wstring=%S\n", L"abc" L"def" "ghi");
345 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
346 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
350 while ((b = b + 1) < 96) {
354 printf("fib=%d\n", fib(33));
356 while (b != 0x80000000) {
369 for(i = 0; i < 10;i++)
378 /* break/continue tests */
390 /* break/continue tests */
402 for(i = 0;i < 10;i++) {
414 static void *label_table[3] = { &&label1, &&label2, &&label3 };
427 /* we also test computed gotos (GCC extension) */
429 goto *label_table[i];
457 printf("enum:\n%d %d %d %d %d %d\n",
458 E0, E1, E2, E3, E4, E5);
460 printf("b1=%d\n", b1);
471 printf("typedef:\n");
472 printf("a=%d\n", *a);
477 printf("forward:\n");
483 void forward_ref(void)
485 printf("forward ok\n");
488 typedef struct struct1 {
508 struct struct1 st1, st2;
510 int main(int argc, char **argv)
528 constant_expr_test();
532 compound_literal_test();
534 struct_assign_test();
543 old_style_function();
546 statement_expr_test();
561 printf("g1=%d\n", g);
569 printf("g2=%d\n", g);
573 printf("g3=%d\n", g);
577 printf("g4=%d\n", g);
580 printf("g5=%d\n", g);
583 void array_test(int a[4])
588 printf("sizeof(a) = %d\n", sizeof(a));
589 printf("sizeof(\"a\") = %d\n", sizeof("a"));
591 printf("sizeof(__func__) = %d\n", sizeof(__func__));
593 printf("sizeof tab %d\n", sizeof(tab));
594 printf("sizeof tab2 %d\n", sizeof tab2);
598 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
601 tab2[i][j] = 10 * i + j;
603 printf(" %3d", ((int *)tab2)[i]);
612 printf("%d\n", a += 1);
613 printf("%d\n", a -= 2);
614 printf("%d\n", a *= 31232132);
615 printf("%d\n", a /= 4);
616 printf("%d\n", a %= 20);
617 printf("%d\n", a &= 6);
618 printf("%d\n", a ^= 7);
619 printf("%d\n", a |= 8);
620 printf("%d\n", a >>= 3);
621 printf("%d\n", a <<= 4);
625 printf("%d\n", a + 1);
626 printf("%d\n", a - 2);
627 printf("%d\n", a * 312);
628 printf("%d\n", a / 4);
629 printf("%d\n", b / 4);
630 printf("%d\n", (unsigned)b / 4);
631 printf("%d\n", a % 20);
632 printf("%d\n", b % 20);
633 printf("%d\n", (unsigned)b % 20);
634 printf("%d\n", a & 6);
635 printf("%d\n", a ^ 7);
636 printf("%d\n", a | 8);
637 printf("%d\n", a >> 3);
638 printf("%d\n", b >> 3);
639 printf("%d\n", (unsigned)b >> 3);
640 printf("%d\n", a << 4);
645 printf("%d\n", 12 + 1);
646 printf("%d\n", 12 - 2);
647 printf("%d\n", 12 * 312);
648 printf("%d\n", 12 / 4);
649 printf("%d\n", 12 % 20);
650 printf("%d\n", 12 & 6);
651 printf("%d\n", 12 ^ 7);
652 printf("%d\n", 12 | 8);
653 printf("%d\n", 12 >> 2);
654 printf("%d\n", 12 << 4);
658 printf("%d %d %d %d\n",
665 /* Check that tcc saves registers before a conditional jump */
666 /* Addresses bug "grischka case_2" */
667 struct test_str { int a, b, c; };
668 struct test_str t1 = {0,0,0};
669 struct test_str t2 = {1,1,1};
670 struct test_str *p1 = &t1;
671 struct test_str *p2 = &t2;
675 p1->b = f==0 || isid(0);
676 printf("case_2.1: Expect 0 1 0 -> %d %d %d\n", p1->a, p1->b, p1->c);
677 p1->c = !f || isid(0);
678 printf("case_2.2: Expect 0 1 1 -> %d %d %d\n", p1->a, p1->b, p1->c);
680 /* This will crash old versions of tcc during compilation: */
681 p2->b = (f==1) && isid(0);
682 printf("case_2.1AND: Expect 0 1 0 -> %d %d %d\n", p2->a, p2->b, p2->c);
683 p2->b = (!(f==1)) && isid(0);
684 printf("case_2.2AND: Expect 0 1 0 -> %d %d %d\n", p2->a, p2->b, p2->c);
690 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
693 /**********************/
695 int vstack[10], *vstack_ptr;
697 void vpush(int vt, int vc)
703 void vpop(int *ft, int *fc)
716 vstack_ptr[-2] &= ~0xffffff80;
718 printf("res= %d %d\n", a, b);
721 void constant_expr_test()
724 printf("constant_expr:\n");
726 printf("%d\n", a * 16);
727 printf("%d\n", a * 1);
728 printf("%d\n", a + 0);
737 printf("expr_ptr:\n");
740 printf("diff=%d\n", q - p);
742 printf("inc=%d\n", p - tab4);
744 printf("dec=%d\n", p - tab4);
746 printf("inc=%d\n", p - tab4);
748 printf("dec=%d\n", p - tab4);
749 printf("add=%d\n", p + 3 - tab4);
750 printf("add=%d\n", 3 + p - tab4);
756 printf("constant_expr:\n");
759 printf("%d\n", a == a);
760 printf("%d\n", a != a);
762 printf("%d\n", a < b);
763 printf("%d\n", a <= b);
764 printf("%d\n", a <= a);
765 printf("%d\n", b >= a);
766 printf("%d\n", a >= a);
767 printf("%d\n", b > a);
769 printf("%d\n", (unsigned)a < b);
770 printf("%d\n", (unsigned)a <= b);
771 printf("%d\n", (unsigned)a <= a);
772 printf("%d\n", (unsigned)b >= a);
773 printf("%d\n", (unsigned)a >= a);
774 printf("%d\n", (unsigned)b > a);
803 printf("sizes: %d %d %d %d\n",
804 sizeof(struct struct1),
805 sizeof(struct struct2),
806 sizeof(union union1),
807 sizeof(union union2));
811 printf("st1: %d %d %d\n",
812 st1.f1, st1.f2, st1.f3);
815 printf("union1: %d\n", st1.u.v1);
818 printf("union2: %d\n", u.w1);
823 printf("st2: %d %d %d\n",
824 s->f1, s->f2, s->f3);
825 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
827 /* align / size tests */
828 printf("aligntest1 sizeof=%d alignof=%d\n",
829 sizeof(struct aligntest1), __alignof__(struct aligntest1));
830 printf("aligntest2 sizeof=%d alignof=%d\n",
831 sizeof(struct aligntest2), __alignof__(struct aligntest2));
832 printf("aligntest3 sizeof=%d alignof=%d\n",
833 sizeof(struct aligntest3), __alignof__(struct aligntest3));
834 printf("aligntest4 sizeof=%d alignof=%d\n",
835 sizeof(struct aligntest4), __alignof__(struct aligntest4));
837 /* empty structures (GCC extension) */
838 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
839 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
842 /* XXX: depend on endianness */
843 void char_short_test()
847 printf("char_short:\n");
852 *(char *)&var1, *(char *)&var2);
854 *(unsigned char *)&var1, *(unsigned char *)&var2);
855 printf("s16=%d %d\n",
856 *(short *)&var1, *(short *)&var2);
857 printf("u16=%d %d\n",
858 *(unsigned short *)&var1, *(unsigned short *)&var2);
859 printf("s32=%d %d\n",
860 *(int *)&var1, *(int *)&var2);
861 printf("u32=%d %d\n",
862 *(unsigned int *)&var1, *(unsigned int *)&var2);
863 *(char *)&var1 = 0x08;
864 printf("var1=%x\n", var1);
865 *(short *)&var1 = 0x0809;
866 printf("var1=%x\n", var1);
867 *(int *)&var1 = 0x08090a0b;
868 printf("var1=%x\n", var1);
881 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
882 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
884 static int toupper1(int a)
891 int *s, a, b, t, f, i;
895 printf("!s=%d\n", !s);
901 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
902 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
903 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
918 printf("b=%d\n", a + (0 ? 1 : a / 2));
920 /* test register spilling */
923 a = (a + b) * ((a < b) ?
924 ((b - a) * (a - b)): a + b);
927 /* test complex || or && expressions */
931 printf("exp=%d\n", f == (32 <= a && a <= 3));
932 printf("r=%d\n", (t || f) + (t && f));
937 int aspect_native = 65536;
938 double bfu_aspect = 1.0;
940 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
941 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
942 printf("aspect=%d\n", aspect);
946 /* Test passing structs & function pointers though conditional
947 * operator ? :. This is bug grischka-20050929 case_1 */
949 struct test1 { int a, b, c; };
950 struct test1 t0 = {10,20,30};
951 struct test1 t1 = {11,21,31};
952 struct test1 tx = {0,0,0};
957 printf("case_1.1: 10,20,30 -> %d,%d,%d\n", tx.a, tx.b, tx.c);
959 /* This tests to see that function pointers correctly pass through
960 the conditional operator ?:. This tests for
961 grischka-20050929 case_1.2. Note that this was already FIXED
962 in rl-1.0.0, but we want to TEST for it too. */
963 pfn = f ? some_fn : other_fn;
964 printf("case_1.2: other -> %s\n", 1==pfn(1)?"some":"other");
968 /* test ? : GCC extension */
970 static int v1 = 34 ? : -1; /* constant case */
971 static int v2 = 0 ? : -1; /* constant case */
974 printf("%d %d\n", v1, v2);
975 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
978 /* again complex expression */
980 if (toupper1 (i) != TOUPPER (i))
981 printf("error %d\n", i);
986 /* GCC accepts that */
987 static int tab_reinit[];
988 static int tab_reinit[10];
990 //int cinit1; /* a global variable can be defined several times without error ! */
994 int *cinit2 = (int []){3, 2, 1};
996 void compound_literal_test(void)
1001 printf("compound_test:\n");
1003 p = (int []){1, 2, 3};
1005 printf(" %d", p[i]);
1009 printf("%d", cinit2[i]);
1013 printf("q1=%s\n", q);
1015 q = (char *){ "tralala2" };
1016 printf("q2=%s\n", q);
1019 printf("q3=%s\n", q3);
1021 q = (char []){ "tralala3" };
1022 printf("q4=%s\n", q);
1025 p = (int []){1, 2, cinit1 + 3};
1027 printf(" %d", p[i]);
1031 p = (int []){1, 2, 4 + i};
1032 printf("%d %d %d\n",
1054 printf("kr_test:\n");
1055 printf("func1=%d\n", kr_func1(3, 4));
1056 printf("func2=%d\n", kr_func2(3, 4));
1063 tab = (char*)malloc(20);
1079 /* structure assignment tests */
1085 struct structa1 ssta1;
1087 void struct_assign_test1(struct structa1 s1, int t)
1089 printf("%d %d %d\n", s1.f1, s1.f2, t);
1092 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1099 void struct_assign_test3(void)
1101 // incompatible assigment
1102 struct _s1 { int a, b, c; } *p1 = NULL;
1103 struct _s2 { int a, b, c; } *p2 = p1; //warning
1104 printf("struct assign, 0 -> %x\n", p2);
1107 void struct_assign_test(void)
1109 struct structa1 lsta1, lsta2;
1112 printf("struct_assign_test:\n");
1116 printf("%d %d\n", lsta1.f1, lsta1.f2);
1118 printf("%d %d\n", lsta2.f1, lsta2.f2);
1123 struct_assign_test1(lsta2, 3);
1125 printf("before call: %d %d\n", lsta2.f1, lsta2.f2);
1126 lsta2 = struct_assign_test2(lsta2, 4);
1127 printf("after call: %d %d\n", lsta2.f1, lsta2.f2);
1128 struct_assign_test3();
1131 /* casts to short/char */
1133 void cast1(char a, short b, unsigned char c, unsigned short d)
1135 printf("%d %d %d %d\n", a, b, c, d);
1150 printf("cast_test:\n");
1154 printf("%d %d %d %d\n",
1157 (unsigned char)(a + 1),
1158 (unsigned short)(a + 1));
1159 printf("%d %d %d %d\n",
1162 (unsigned char)0xfffff,
1163 (unsigned short)0xfffff);
1165 a = (bcast = 128) + 1;
1167 a = (scast = 65536) + 1;
1170 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1172 /* test cast from unsigned to signed short to int */
1175 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1178 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1180 /* Try casting pointer to short or _Bool (grishka case_6.1). This
1181 * is lossy, so tcc will print a warning. This capability is needed
1182 * to compile gcc 2.95 as well as other programs. */
1184 void *p = (void *) 3;
1185 printf("Expect 3 1 -> %hd %hhd\n",
1186 (short) p, (_Bool) p); /* Expect warning */
1190 /* test implicit int casting for array accesses */
1194 printf("%d %d\n", tab[0], tab[1]);
1196 /* test implicit casting on some operators */
1197 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1198 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1199 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1201 /* Cast float to bool */
1202 printf("%d\n", (_Bool) 0.1);
1203 /* Cast non-constant double 0.1 to bool, should be 1, grischka case 6.2 */
1204 printf("1 -> %d\n", (_Bool) g);
1206 /* Cast with sign extension */
1208 printf("0x%x == 0xfffff000\n", a);
1209 a = (signed char)0xf0f0;
1210 printf("0x%x == 0xfffffff0\n", a);
1213 /* initializers tests */
1214 struct structinit1 {
1223 int sinit3[3] = { 1, 2, {{3}}, };
1224 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1225 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1226 int sinit6[] = { 1, 2, 3 };
1227 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1228 char sinit8[] = "hello" "trala";
1230 struct structinit1 sinit9 = { 1, 2, 3 };
1231 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1232 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1240 char *sinit12 = "hello world";
1246 char sinit14[10] = { "abc" };
1247 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1249 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1265 void init_test(void)
1269 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1270 int linit6[] = { 1, 2, 3 };
1272 char linit8[] = "hello" "trala";
1273 int linit12[10] = { 1, 2 };
1274 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1275 char linit14[10] = "abc";
1276 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1277 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1278 int linit17 = sizeof(linit17);
1280 printf("init_test:\n");
1282 printf("sinit1=%d\n", sinit1);
1283 printf("sinit2=%d\n", sinit2);
1284 printf("sinit3=%d %d %d %d\n",
1290 printf("sinit6=%d\n", sizeof(sinit6));
1291 printf("sinit7=%d %d %d %d\n",
1297 printf("sinit8=%s\n", sinit8);
1298 printf("sinit9=%d %d %d\n",
1303 printf("sinit10=%d %d %d\n",
1308 printf("sinit11=%d %d %d %d %d %d\n",
1319 printf("[%d][%d] = %d %d %d\n",
1320 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1321 printf("linit1=%d\n", linit1);
1322 printf("linit2=%d\n", linit2);
1323 printf("linit6=%d\n", sizeof(linit6));
1324 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1326 printf("sinit12=%s\n", sinit12);
1327 printf("sinit13=%d %s %s %s\n",
1332 printf("sinit14=%s\n", sinit14);
1334 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1336 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1338 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1340 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1342 printf("%d %d %d %d\n",
1347 /* test that initialisation is done after variable declare */
1348 printf("linit17=%d\n", linit17);
1349 printf("sinit15=%d\n", sinit15[0]);
1350 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1351 printf("sinit17=%s %d %s %d\n",
1352 sinit17[0].s, sinit17[0].len,
1353 sinit17[1].s, sinit17[1].len);
1355 printf("%x ", sinit18[i]);
1384 /* ISOC99 _Bool type */
1385 void c99_bool_test(void)
1391 printf("bool_test:\n");
1392 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1394 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1396 printf("b = %d\n", b);
1398 printf("b = %d\n", b);
1402 void bitfield_test(void)
1412 unsigned int f5 : 7;
1414 printf("bitfield_test:");
1415 printf("sizeof(st1) = %d\n", sizeof(st1));
1424 printf("%d %d %d %d %d\n",
1425 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1429 printf("st1.f1 == -1\n");
1431 printf("st1.f1 != -1\n");
1433 printf("st1.f2 == -1\n");
1435 printf("st1.f2 != -1\n");
1437 /* Do bitfield assignments return correct rvalue? This is bug
1438 * grischka-2005-09-29 case_3 */
1440 struct test1 { unsigned a:1, b:1, c:1, d:1; };
1441 struct test1 t1 = {0, 1, 0, 1};
1442 struct test1 *p = &t1;
1444 printf("case_3.1: 0101 -> %d%d%d%d\n", p->a, p->b, p->c, p->d);
1447 printf("case_3.2: 1010 -> %d%d%d%d\n", p->a, p->b, p->c, p->d);
1452 #define FTEST(prefix, type, fmt)\
1453 void prefix ## cmp(type a, type b)\
1455 printf("%d %d %d %d %d %d\n",\
1462 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1470 printf(fmt "\n", ++a);\
1471 printf(fmt "\n", a++);\
1472 printf(fmt "\n", a);\
1474 void prefix ## fcast(type a)\
1485 printf("ftof: %f %f %Lf\n", fa, da, la);\
1487 ua = (unsigned int)a;\
1488 printf("ftoi: %d %u\n", ia, ua);\
1492 printf("itof: " fmt "\n", b);\
1494 printf("utof: " fmt "\n", b);\
1497 void prefix ## test(void)\
1499 printf("testing '%s'\n", #type);\
1500 prefix ## cmp(1, 2.5);\
1501 prefix ## cmp(2, 1.5);\
1502 prefix ## cmp(1, 1);\
1503 prefix ## fcast(234.6);\
1504 prefix ## fcast(-2334.6);\
1507 FTEST(f, float, "%f")
1508 FTEST(d, double, "%f")
1509 FTEST(ld, long double, "%Lf")
1511 double ftab1[3] = { 1.2, 3.4, -5.6 };
1514 void float_test(void)
1521 printf("float_test:\n");
1522 printf("sizeof(float) = %d\n", sizeof(float));
1523 printf("sizeof(double) = %d\n", sizeof(double));
1524 printf("sizeof(long double) = %d\n", sizeof(long double));
1528 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1529 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1530 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1532 printf("da=%f\n", da);
1534 printf("fa=%f\n", fa);
1537 printf("da = %f\n", da);
1540 printf("db = %f\n", db);
1548 return fib(n-1) + fib(n-2);
1551 int reply_self(int x)
1565 printf("funcptr:\n");
1572 /* more complicated pointer computation */
1575 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1576 printf("sizeof2 = %d\n", sizeof funcptr_test);
1577 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1578 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1580 /* Test function pointer indirection */
1582 int (*pfn)(int) = reply_self;
1583 printf("case_9: 1 (7,8) -> %d (%d,%d)\n",
1584 (int) (reply_self == *pfn), pfn(7), (******pfn)(8));
1589 void lloptest(long long a, long long b)
1591 unsigned long long ua, ub;
1596 printf("arith: %Ld %Ld %Ld\n",
1602 printf("arith1: %Ld %Ld\n",
1608 printf("bin: %Ld %Ld %Ld\n",
1614 printf("test: %d %d %d %d %d %d\n",
1622 printf("utest: %d %d %d %d %d %d\n",
1633 printf("arith2: %Ld %Ld\n", a, b);
1634 printf("arith2: %Ld %Ld\n", a++, b++);
1635 printf("arith2: %Ld %Ld\n", --a, --b);
1636 printf("arith2: %Ld %Ld\n", a, b);
1639 void llshift(long long a, int b)
1641 printf("shift: %Ld %Ld %Ld\n",
1642 (unsigned long long)a >> b,
1645 printf("shiftc: %Ld %Ld %Ld\n",
1646 (unsigned long long)a >> 3,
1649 printf("shiftc: %Ld %Ld %Ld\n",
1650 (unsigned long long)a >> 35,
1660 long long la, lb, lc;
1661 unsigned long long ula, ulb, ulc;
1664 la = (la << 20) | 0x12345;
1666 printf("la=%Ld ula=%Lu\n", la, ula);
1671 printf("lltof: %f %f %Lf\n", fa, da, lda);
1676 printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
1681 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1686 printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
1689 long long llfunc1(int a)
1699 long long int value(struct S *v)
1701 return ((long long int)v->item);
1704 void longlong_test(void)
1707 unsigned long long ull;
1710 printf("longlong_test:\n");
1711 printf("sizeof(long long) = %d\n", sizeof(long long));
1716 printf("%Ld %Ld\n", a, b);
1717 printf("%Ld %Ld %Ld %Lx\n",
1721 0x1234567812345679);
1726 lloptest(0xff, 0x1234);
1727 b = 0x72345678 << 10;
1731 b = 0x72345678LL << 10;
1742 /* long long reg spill test */
1747 printf("%lld\n", value(&a));
1749 lloptest(0x80000000, 0);
1751 /* another long long spill test */
1757 printf("%lld\n", *p);
1760 /* Make sure long long comparison tests upper 32 bits */
1761 ull = 0xffffffff00000000ULL;
1762 printf("ull=%llx\n",ull);
1763 printf("ull!=0 ", (ull != 0) ? "true" : "false");
1764 printf("ull ", ull ? "true" : "false");
1766 /* check double -> ull cast */
1769 unsigned long long ull = d;
1770 unsigned long long r = 100000000000LL;
1771 printf("\ndouble->ull (grischka case 10): %.0f -> %ld\n",
1772 d/r, (long)(ull/r));
1777 void vprintf1(const char *fmt, ...)
1799 i = va_arg(ap, int);
1803 d = va_arg(ap, double);
1807 ll = va_arg(ap, long long);
1821 void stdarg_test(void)
1823 vprintf1("%d %d %d\n", 1, 2, 3);
1824 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1825 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1828 void whitespace_test(void)
1833 printf("whitspace:\n");
1837 #ifdef CORRECT_CR_HANDLING
1838 printf("aaa=%d\n", 3);
1845 #ifdef ACCEPT_CR_IN_STRINGS
1846 printf("len1=%d\n", strlen("
1848 #ifdef CORRECT_CR_HANDLING
1851 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
1853 printf("len1=%d\n", strlen("
\ra
1855 #endif /* ACCEPT_CR_IN_STRINGS */
1858 int reltab[3] = { 1, 2, 3 };
1860 int *rel1 = &reltab[1];
1861 int *rel2 = &reltab[2];
1863 void relocation_test(void)
1865 printf("*rel1=%d\n", *rel1);
1866 printf("*rel2=%d\n", *rel2);
1869 void old_style_f(a,b,c)
1873 printf("a=%d b=%d b=%f\n", a, b, c);
1876 void decl_func1(int cmpfn())
1878 printf("cmpfn=%lx\n", (long)cmpfn);
1881 void decl_func2(cmpfn)
1884 printf("cmpfn=%lx\n", (long)cmpfn);
1887 void old_style_function(void)
1889 old_style_f((void *)1, 2, 3.0);
1894 void sizeof_test(void)
1899 printf("sizeof(int) = %d\n", sizeof(int));
1900 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1901 printf("sizeof(short) = %d\n", sizeof(short));
1902 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1903 printf("sizeof(char) = %d\n", sizeof(char));
1904 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1905 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1907 printf("sizeof(a++) = %d\n", sizeof a++);
1908 printf("a=%d\n", a);
1910 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
1911 printf("-1>sizeof(int) = %d\n", -1>sizeof(int));
1913 /* some alignof tests */
1914 printf("__alignof__(int) = %d\n", __alignof__(int));
1915 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1916 printf("__alignof__(short) = %d\n", __alignof__(short));
1917 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1918 printf("__alignof__(char) = %d\n", __alignof__(char));
1919 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1920 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
1923 void typeof_test(void)
1932 printf("a=%f b=%f c=%f\n", a, b, c);
1935 void statement_expr_test(void)
1948 printf("a=%d\n", a);
1952 void local_label_test(void)
1958 __label__ l1, l2, l3, l4;
1972 printf("a=%d\n", a);
1982 /* inline assembler test */
1985 /* from linux kernel */
1986 static char * strncat1(char * dest,const char * src,size_t count)
1989 __asm__ __volatile__(
1998 "testb %%al,%%al\n\t"
2000 "2:\txorl %2,%2\n\t"
2002 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2003 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2008 static inline void * memcpy1(void * to, const void * from, size_t n)
2011 __asm__ __volatile__(
2016 "1:\ttestb $1,%b4\n\t"
2020 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2021 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2026 static __inline__ void sigaddset1(unsigned int *set, int _sig)
2028 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2031 static __inline__ void sigdelset1(unsigned int *set, int _sig)
2033 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2036 static __inline__ __const__ unsigned int swab32(unsigned int x)
2038 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2039 "rorl $16,%0\n\t" /* swap words */
2040 "xchgb %b0,%h0" /* swap higher bytes */
2046 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
2048 unsigned long long res;
2049 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
2053 static __inline__ unsigned long long inc64(unsigned long long a)
2055 unsigned long long res;
2056 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
2067 printf("inline asm:\n");
2068 /* test the no operand case */
2069 asm volatile ("xorl %eax, %eax");
2071 memcpy1(buf, "hello", 6);
2072 strncat1(buf, " worldXXXXX", 3);
2073 printf("%s\n", buf);
2075 /* 'A' constraint test */
2076 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2077 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2080 sigdelset1(&set, 2);
2081 sigaddset1(&set, 16);
2082 /* NOTE: we test here if C labels are correctly restored after the
2086 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
2087 printf("set=0x%x\n", set);
2089 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
2103 #define COMPAT_TYPE(type1, type2) \
2105 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2106 __builtin_types_compatible_p (type1, type2));\
2111 void builtin_test(void)
2114 COMPAT_TYPE(int, int);
2115 COMPAT_TYPE(int, unsigned int);
2116 COMPAT_TYPE(int, char);
2117 COMPAT_TYPE(int, const int);
2118 COMPAT_TYPE(int, volatile int);
2119 COMPAT_TYPE(int *, int *);
2120 COMPAT_TYPE(int *, void *);
2121 COMPAT_TYPE(int *, const int *);
2122 COMPAT_TYPE(char *, unsigned char *);
2123 /* space is needed because tcc preprocessor introduces a space between each token */
2124 COMPAT_TYPE(char * *, void *);
2126 printf("res = %d\n", __builtin_constant_p(1));
2127 printf("res = %d\n", __builtin_constant_p(1 + 2));
2128 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
2129 printf("res = %d\n", __builtin_constant_p(constant_p_var));
2133 /* static_stub1 takes a pointer to a function, and returns a pointer to
2134 * a function; that function must accept no parameters, and return nothing. */
2135 void ((*static_stub1(void ((*p)(void)))) (void))
2137 static void (*pfn)(void);
2138 void (*mytemp)(void);
2144 /* Another stub. The static "pfn" is in a different function, so it'd better
2145 not be mapped to the same memory location. */
2146 void ((*static_stub2(void ((*p)(void)))) (void))
2148 static void (*pfn)(void);
2149 void (*mytemp)(void);
2156 void static_test(void)
2158 void (*result)(void);
2159 result = static_stub1(static_test);
2160 printf("static_test Expect 1 -> %d\n", result == 0);
2161 result = static_stub1(static_test);
2162 printf("static_test Expect 1 -> %d\n", result == static_test);
2163 result = static_stub2(static_test);
2164 printf("static_test Expect 1 -> %d\n", result == 0);
2165 result = static_stub2(static_test);
2166 printf("static_test Expect 1 -> %d\n", result == static_test);
2170 void const_func(const int a)
2174 void const_warn_test(void)