2 * TCC auto test program
8 /* Unfortunately, gcc version < 3 does not handle that! */
11 /* only gcc 3 handles _Bool correctly */
14 /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
15 #define CORRECT_CR_HANDLING
19 /* deprecated and no longer supported in gcc 3.3 */
20 //#define ACCEPT_CR_IN_STRINGS
22 /* __VA_ARGS__ and __func__ support */
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"
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"
61 void constant_expr_test();
63 void char_short_test();
65 void compound_literal_test(void);
67 void struct_assign_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 manyarg_test(void);
74 void stdarg_test(void);
75 void whitespace_test(void);
76 void relocation_test(void);
77 void old_style_function(void);
78 void alloca_test(void);
79 void sizeof_test(void);
80 void typeof_test(void);
81 void local_label_test(void);
82 void statement_expr_test(void);
84 void builtin_test(void);
88 void forward_ref(void);
94 #define M1(a, b) (a) + (b)
98 #define glue(a, b) a ## b
99 #define xglue(a, b) glue(a, b)
100 #define HIGHLOW "hello"
101 #define LOW LOW ", world"
103 #define min(a, b) ((a) < (b) ? (a) : (b))
106 #define dprintf(level,...) printf(__VA_ARGS__)
109 /* gcc vararg macros */
110 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
112 #define MACRO_NOARGS()
128 #define __INT64_C(c) c ## LL
129 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
137 #define spin_lock(lock) do { } while (0)
138 #define wq_spin_lock spin_lock
139 #define TEST2() wq_spin_lock(a)
141 void macro_test(void)
143 printf("macro:\n");\f\v
145 printf("aaa=%d\n", AAA
);
147 printf("min=%d\n", min(1, min(2, -1)));
149 printf("s1=%s\n", glue(HIGH
, LOW
));
150 printf("s2=%s\n", xglue(HIGH
, LOW
));
151 printf("s3=%s\n", str("c"));
152 printf("s4=%s\n", str(a1
));
153 printf("B3=%d\n", B3
);
156 printf("A defined\n");
159 printf("B defined\n");
162 printf("A defined\n");
164 printf("A not defined\n");
167 printf("B defined\n");
169 printf("B not defined\n");
173 printf("A defined\n");
175 printf("B1 defined\n");
177 printf("B1 not defined\n");
180 printf("A not defined\n");
182 printf("B2 defined\n");
184 printf("B2 not defined\n");
189 printf("test true1\n");
192 printf("test true2\n");
195 printf("test true3\n");
198 printf("test trueA\n");
201 printf("test trueB\n");
217 printf("__LINE__ defined\n");
220 printf("__LINE__=%d __FILE__=%s\n",
223 printf("__LINE__=%d __FILE__=%s\n",
226 printf("__LINE__=%d __FILE__=%s\n",
228 #line 227 "tcctest.c"
230 /* not strictly preprocessor, but we test it there */
232 printf("__func__ = %s\n", __func__
);
233 dprintf(1, "vaarg=%d\n", 1);
235 dprintf1(1, "vaarg1\n");
236 dprintf1(1, "vaarg1=%d\n", 2);
237 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
240 printf("func='%s'\n", __FUNCTION__
);
242 /* complicated macros in glibc */
243 printf("INT64_MIN=%Ld\n", INT64_MIN
);
253 /* macro function with argument outside the macro string */
254 #define MF_s MF_hello
255 #define MF_hello(msg) printf("%s\n",msg)
257 #define MF_t printf("tralala\n"); MF_hello
262 /* test macro substituion inside args (should not eat stream) */
263 printf("qq=%d\n", qq(qq
)(2));
265 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
266 null argument without a space. gcc 3.2 fixes that. */
269 printf("qq1=%d\n", qq1( ));
271 /* comment with stray handling *\
273 /* this is a valid *\/ comment */
274 /* this is a valid comment *\*/
278 /* test function macro substitution when the function name is
297 void ps(const char *s
)
309 const char foo1_string
[] = "\
318 printf("\141\1423\143\n");/* dezdez test */
319 printf("\x41\x42\x43\x3a\n");
320 printf("c=%c\n", 'r');
321 printf("wc=%C 0x%lx %C\n", L
'a', L
'\x1234', L
'c');
322 printf("foo1_string='%s'\n", foo1_string
);
324 printf("wstring=%S\n", L
"abc");
325 printf("wstring=%S\n", L
"abc" L
"def" "ghi");
326 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
327 printf("L'\\377'=%d L'\\xff'=%d\n", L
'\377', L
'\xff');
331 while ((b
= b
+ 1) < 96) {
335 printf("fib=%d\n", fib(33));
337 while (b
!= 0x80000000) {
350 for(i
= 0; i
< 10;i
++)
359 /* break/continue tests */
371 /* break/continue tests */
383 for(i
= 0;i
< 10;i
++) {
395 static void *label_table
[3] = { &&label1
, &&label2
, &&label3
};
408 /* we also test computed gotos (GCC extension) */
410 goto *label_table
[i
];
438 printf("enum:\n%d %d %d %d %d %d\n",
439 E0
, E1
, E2
, E3
, E4
, E5
);
441 printf("b1=%d\n", b1
);
457 printf("typedef:\n");
458 printf("a=%d\n", *a
);
460 printf("mytype2=%d\n", mytype2
);
465 printf("forward:\n");
471 void forward_ref(void)
473 printf("forward ok\n");
476 typedef struct struct1
{
496 struct struct1 st1
, st2
;
498 int main(int argc
, char **argv
)
516 constant_expr_test();
520 compound_literal_test();
522 struct_assign_test();
532 old_style_function();
536 statement_expr_test();
550 printf("g1=%d\n", g
);
558 printf("g2=%d\n", g
);
562 printf("g3=%d\n", g
);
566 printf("g4=%d\n", g
);
569 printf("g5=%d\n", g
);
572 void array_test(int a
[4])
577 printf("sizeof(a) = %d\n", sizeof(a
));
578 printf("sizeof(\"a\") = %d\n", sizeof("a"));
580 printf("sizeof(__func__) = %d\n", sizeof(__func__
));
582 printf("sizeof tab %d\n", sizeof(tab
));
583 printf("sizeof tab2 %d\n", sizeof tab2
);
587 printf("%d %d %d\n", tab
[0], tab
[1], tab
[2]);
590 tab2
[i
][j
] = 10 * i
+ j
;
592 printf(" %3d", ((int *)tab2
)[i
]);
595 printf("sizeof(size_t)=%d\n", sizeof(size_t));
596 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
603 printf("%d\n", a
+= 1);
604 printf("%d\n", a
-= 2);
605 printf("%d\n", a
*= 31232132);
606 printf("%d\n", a
/= 4);
607 printf("%d\n", a
%= 20);
608 printf("%d\n", a
&= 6);
609 printf("%d\n", a
^= 7);
610 printf("%d\n", a
|= 8);
611 printf("%d\n", a
>>= 3);
612 printf("%d\n", a
<<= 4);
616 printf("%d\n", a
+ 1);
617 printf("%d\n", a
- 2);
618 printf("%d\n", a
* 312);
619 printf("%d\n", a
/ 4);
620 printf("%d\n", b
/ 4);
621 printf("%d\n", (unsigned)b
/ 4);
622 printf("%d\n", a
% 20);
623 printf("%d\n", b
% 20);
624 printf("%d\n", (unsigned)b
% 20);
625 printf("%d\n", a
& 6);
626 printf("%d\n", a
^ 7);
627 printf("%d\n", a
| 8);
628 printf("%d\n", a
>> 3);
629 printf("%d\n", b
>> 3);
630 printf("%d\n", (unsigned)b
>> 3);
631 printf("%d\n", a
<< 4);
636 printf("%d\n", 12 + 1);
637 printf("%d\n", 12 - 2);
638 printf("%d\n", 12 * 312);
639 printf("%d\n", 12 / 4);
640 printf("%d\n", 12 % 20);
641 printf("%d\n", 12 & 6);
642 printf("%d\n", 12 ^ 7);
643 printf("%d\n", 12 | 8);
644 printf("%d\n", 12 >> 2);
645 printf("%d\n", 12 << 4);
649 printf("%d %d %d %d\n",
658 return (c
>= 'a' & c
<= 'z') | (c
>= 'A' & c
<= 'Z') | c
== '_';
661 /**********************/
663 int vstack
[10], *vstack_ptr
;
665 void vpush(int vt
, int vc
)
671 void vpop(int *ft
, int *fc
)
684 vstack_ptr
[-2] &= ~0xffffff80;
686 printf("res= %d %d\n", a
, b
);
689 void constant_expr_test()
692 printf("constant_expr:\n");
694 printf("%d\n", a
* 16);
695 printf("%d\n", a
* 1);
696 printf("%d\n", a
+ 0);
706 printf("expr_ptr:\n");
709 printf("diff=%d\n", q
- p
);
711 printf("inc=%d\n", p
- tab4
);
713 printf("dec=%d\n", p
- tab4
);
715 printf("inc=%d\n", p
- tab4
);
717 printf("dec=%d\n", p
- tab4
);
718 printf("add=%d\n", p
+ 3 - tab4
);
719 printf("add=%d\n", 3 + p
- tab4
);
721 /* check if 64bit support is ok */
724 printf("%p %p %ld\n", q
, p
, p
-q
);
725 printf("%d %d %d %d %d %d\n",
726 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
729 printf("%p %p %ld\n", q
, p
, p
-q
);
730 printf("%d %d %d %d %d %d\n",
731 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
732 p
= (int *)((char *)p
+ 0xf0000000);
733 printf("%p %p %ld\n", q
, p
, p
-q
);
734 printf("%d %d %d %d %d %d\n",
735 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
737 printf("%p %p %ld\n", q
, p
, p
-q
);
738 printf("%d %d %d %d %d %d\n",
739 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
744 struct size12 s
[2], *sp
= s
;
749 printf("%d\n", sp
[j
].i
);
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 ? : GCC extension */
948 static int v1
= 34 ? : -1; /* constant case */
949 static int v2
= 0 ? : -1; /* constant case */
952 printf("%d %d\n", v1
, v2
);
953 printf("%d %d\n", a
- 30 ? : a
* 2, a
+ 1 ? : a
* 2);
956 /* again complex expression */
958 if (toupper1 (i
) != TOUPPER (i
))
959 printf("error %d\n", i
);
963 /* GCC accepts that */
964 static int tab_reinit
[];
965 static int tab_reinit
[10];
967 //int cinit1; /* a global variable can be defined several times without error ! */
971 int *cinit2
= (int []){3, 2, 1};
973 void compound_literal_test(void)
978 printf("compound_test:\n");
980 p
= (int []){1, 2, 3};
986 printf("%d", cinit2
[i
]);
990 printf("q1=%s\n", q
);
992 q
= (char *){ "tralala2" };
993 printf("q2=%s\n", q
);
996 printf("q3=%s\n", q3
);
998 q
= (char []){ "tralala3" };
999 printf("q4=%s\n", q
);
1002 p
= (int []){1, 2, cinit1
+ 3};
1004 printf(" %d", p
[i
]);
1008 p
= (int []){1, 2, 4 + i
};
1009 printf("%d %d %d\n",
1031 printf("kr_test:\n");
1032 printf("func1=%d\n", kr_func1(3, 4));
1033 printf("func2=%d\n", kr_func2(3, 4));
1040 tab
= (char*)malloc(20);
1056 /* structure assignment tests */
1062 struct structa1 ssta1
;
1064 void struct_assign_test1(struct structa1 s1
, int t
)
1066 printf("%d %d %d\n", s1
.f1
, s1
.f2
, t
);
1069 struct structa1
struct_assign_test2(struct structa1 s1
, int t
)
1076 void struct_assign_test(void)
1078 struct structa1 lsta1
, lsta2
;
1081 printf("struct_assign_test:\n");
1085 printf("%d %d\n", lsta1
.f1
, lsta1
.f2
);
1087 printf("%d %d\n", lsta2
.f1
, lsta2
.f2
);
1092 struct_assign_test1(lsta2
, 3);
1094 printf("before call: %d %d\n", lsta2
.f1
, lsta2
.f2
);
1095 lsta2
= struct_assign_test2(lsta2
, 4);
1096 printf("after call: %d %d\n", lsta2
.f1
, lsta2
.f2
);
1099 /* casts to short/char */
1101 void cast1(char a
, short b
, unsigned char c
, unsigned short d
)
1103 printf("%d %d %d %d\n", a
, b
, c
, d
);
1117 p
-= 0x700000000042;
1119 printf("cast_test:\n");
1123 printf("%d %d %d %d\n",
1126 (unsigned char)(a
+ 1),
1127 (unsigned short)(a
+ 1));
1128 printf("%d %d %d %d\n",
1131 (unsigned char)0xfffff,
1132 (unsigned short)0xfffff);
1134 a
= (bcast
= 128) + 1;
1136 a
= (scast
= 65536) + 1;
1139 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
1141 /* test cast from unsigned to signed short to int */
1144 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b
, d
);
1147 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b
, d
);
1149 /* test implicit int casting for array accesses */
1153 printf("%d %d\n", tab
[0], tab
[1]);
1155 /* test implicit casting on some operators */
1156 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1157 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1158 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1160 /* from pointer to integer types */
1161 printf("%d %d %ld %ld %lld %lld\n",
1162 (int)p
, (unsigned int)p
,
1163 (long)p
, (unsigned long)p
,
1164 (long long)p
, (unsigned long long)p
);
1166 /* from integers to pointers */
1167 printf("%p %p %p %p\n",
1168 (void *)a
, (void *)b
, (void *)c
, (void *)d
);
1171 /* initializers tests */
1172 struct structinit1
{
1181 int sinit3
[3] = { 1, 2, {{3}}, };
1182 int sinit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1183 int sinit5
[3][2] = { 1, 2, 3, 4, 5, 6 };
1184 int sinit6
[] = { 1, 2, 3 };
1185 int sinit7
[] = { [2] = 3, [0] = 1, 2 };
1186 char sinit8
[] = "hello" "trala";
1188 struct structinit1 sinit9
= { 1, 2, 3 };
1189 struct structinit1 sinit10
= { .f2
= 2, 3, .f1
= 1 };
1190 struct structinit1 sinit11
= { .f2
= 2, 3, .f1
= 1,
1198 char *sinit12
= "hello world";
1204 char sinit14
[10] = { "abc" };
1205 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
1207 struct { int a
[3], b
; } sinit16
[] = { { 1 }, 2 };
1223 void init_test(void)
1227 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1228 int linit6
[] = { 1, 2, 3 };
1230 char linit8
[] = "hello" "trala";
1231 int linit12
[10] = { 1, 2 };
1232 int linit13
[10] = { 1, 2, [7] = 3, [3] = 4, };
1233 char linit14
[10] = "abc";
1234 int linit15
[10] = { linit1
, linit1
+ 1, [6] = linit1
+ 2, };
1235 struct linit16
{ int a1
, a2
, a3
, a4
; } linit16
= { 1, .a3
= 2 };
1236 int linit17
= sizeof(linit17
);
1238 printf("init_test:\n");
1240 printf("sinit1=%d\n", sinit1
);
1241 printf("sinit2=%d\n", sinit2
);
1242 printf("sinit3=%d %d %d %d\n",
1248 printf("sinit6=%d\n", sizeof(sinit6
));
1249 printf("sinit7=%d %d %d %d\n",
1255 printf("sinit8=%s\n", sinit8
);
1256 printf("sinit9=%d %d %d\n",
1261 printf("sinit10=%d %d %d\n",
1266 printf("sinit11=%d %d %d %d %d %d\n",
1277 printf("[%d][%d] = %d %d %d\n",
1278 i
, j
, sinit4
[i
][j
], sinit5
[i
][j
], linit4
[i
][j
]);
1279 printf("linit1=%d\n", linit1
);
1280 printf("linit2=%d\n", linit2
);
1281 printf("linit6=%d\n", sizeof(linit6
));
1282 printf("linit8=%d %s\n", sizeof(linit8
), linit8
);
1284 printf("sinit12=%s\n", sinit12
);
1285 printf("sinit13=%d %s %s %s\n",
1290 printf("sinit14=%s\n", sinit14
);
1292 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
1294 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
1296 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
1298 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
1300 printf("%d %d %d %d\n",
1305 /* test that initialisation is done after variable declare */
1306 printf("linit17=%d\n", linit17
);
1307 printf("sinit15=%d\n", sinit15
[0]);
1308 printf("sinit16=%d %d\n", sinit16
[0].a
[0], sinit16
[1].a
[0]);
1309 printf("sinit17=%s %d %s %d\n",
1310 sinit17
[0].s
, sinit17
[0].len
,
1311 sinit17
[1].s
, sinit17
[1].len
);
1313 printf("%x ", sinit18
[i
]);
1342 /* ISOC99 _Bool type */
1343 void c99_bool_test(void)
1349 printf("bool_test:\n");
1350 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
1352 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
1354 printf("b = %d\n", b
);
1356 printf("b = %d\n", b
);
1360 void bitfield_test(void)
1370 unsigned int f5
: 7;
1372 printf("bitfield_test:");
1373 printf("sizeof(st1) = %d\n", sizeof(st1
));
1382 printf("%d %d %d %d %d\n",
1383 st1
.f1
, st1
.f2
, st1
.f3
, st1
.f4
, st1
.f5
);
1387 printf("st1.f1 == -1\n");
1389 printf("st1.f1 != -1\n");
1391 printf("st1.f2 == -1\n");
1393 printf("st1.f2 != -1\n");
1395 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1396 long-long bitfields whose size is not bigger than int */
1401 unsigned long long f3
: 38;
1403 st2
.f1
= 0x123456789ULL
;
1405 st2
.f2
= (long long)a
<< 25;
1408 printf("%lld %lld %lld\n", st2
.f1
, st2
.f2
, st2
.f3
);
1412 #define FLOAT_FMT "%f\n"
1414 /* x86's float isn't compatible with GCC */
1415 #define FLOAT_FMT "%.5f\n"
1418 /* declare strto* functions as they are C99 */
1419 double strtod(const char *nptr
, char **endptr
);
1420 float strtof(const char *nptr
, char **endptr
);
1421 long double strtold(const char *nptr
, char **endptr
);
1423 #define FTEST(prefix, type, fmt)\
1424 void prefix ## cmp(type a, type b)\
1426 printf("%d %d %d %d %d %d\n",\
1433 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1441 printf(fmt "\n", ++a);\
1442 printf(fmt "\n", a++);\
1443 printf(fmt "\n", a);\
1445 printf("%d %d\n", !a, !b);\
1447 void prefix ## fcast(type a)\
1458 printf("ftof: %f %f %Lf\n", fa, da, la);\
1460 ua = (unsigned int)a;\
1461 printf("ftoi: %d %u\n", ia, ua);\
1465 printf("itof: " fmt "\n", b);\
1467 printf("utof: " fmt "\n", b);\
1470 float prefix ## retf(type a) { return a; }\
1471 double prefix ## retd(type a) { return a; }\
1472 long double prefix ## retld(type a) { return a; }\
1474 void prefix ## call(void)\
1476 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1477 printf("double: %f\n", prefix ## retd(42.123456789));\
1478 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1479 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
1482 void prefix ## test(void)\
1484 printf("testing '%s'\n", #type);\
1485 prefix ## cmp(1, 2.5);\
1486 prefix ## cmp(2, 1.5);\
1487 prefix ## cmp(1, 1);\
1488 prefix ## fcast(234.6);\
1489 prefix ## fcast(-2334.6);\
1493 FTEST(f
, float, "%f")
1494 FTEST(d
, double, "%f")
1495 FTEST(ld
, long double, "%Lf")
1497 double ftab1
[3] = { 1.2, 3.4, -5.6 };
1500 void float_test(void)
1507 printf("float_test:\n");
1508 printf("sizeof(float) = %d\n", sizeof(float));
1509 printf("sizeof(double) = %d\n", sizeof(double));
1510 printf("sizeof(long double) = %d\n", sizeof(long double));
1514 printf("%f %f %f\n", ftab1
[0], ftab1
[1], ftab1
[2]);
1515 printf("%f %f %f\n", 2.12, .5, 2.3e10
);
1516 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1518 printf("da=%f\n", da
);
1520 printf("fa=%f\n", fa
);
1523 printf("da = %f\n", da
);
1526 printf("db = %f\n", db
);
1534 return fib(n
-1) + fib(n
-2);
1546 printf("funcptr:\n");
1553 /* more complicated pointer computation */
1556 printf("sizeof1 = %d\n", sizeof(funcptr_test
));
1557 printf("sizeof2 = %d\n", sizeof funcptr_test
);
1558 printf("sizeof3 = %d\n", sizeof(&funcptr_test
));
1559 printf("sizeof4 = %d\n", sizeof &funcptr_test
);
1562 void lloptest(long long a
, long long b
)
1564 unsigned long long ua
, ub
;
1569 printf("arith: %Ld %Ld %Ld\n",
1575 printf("arith1: %Ld %Ld\n",
1581 printf("bin: %Ld %Ld %Ld\n",
1587 printf("test: %d %d %d %d %d %d\n",
1595 printf("utest: %d %d %d %d %d %d\n",
1606 printf("arith2: %Ld %Ld\n", a
, b
);
1607 printf("arith2: %Ld %Ld\n", a
++, b
++);
1608 printf("arith2: %Ld %Ld\n", --a
, --b
);
1609 printf("arith2: %Ld %Ld\n", a
, b
);
1611 printf("not: %d %d %d %d\n", !a
, !ua
, !b
, !ub
);
1614 void llshift(long long a
, int b
)
1616 printf("shift: %Ld %Ld %Ld\n",
1617 (unsigned long long)a
>> b
,
1620 printf("shiftc: %Ld %Ld %Ld\n",
1621 (unsigned long long)a
>> 3,
1624 printf("shiftc: %Ld %Ld %Ld\n",
1625 (unsigned long long)a
>> 35,
1635 long long la
, lb
, lc
;
1636 unsigned long long ula
, ulb
, ulc
;
1639 la
= (la
<< 20) | 0x12345;
1641 printf("la=%Ld ula=%Lu\n", la
, ula
);
1646 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
1651 printf("ftoll: %Ld %Ld %Ld\n", la
, lb
, lc
);
1656 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
1661 printf("ftoull: %Lu %Lu %Lu\n", ula
, ulb
, ulc
);
1664 long long llfunc1(int a
)
1674 long long int value(struct S
*v
)
1676 return ((long long int)v
->item
);
1679 void longlong_test(void)
1684 printf("longlong_test:\n");
1685 printf("sizeof(long long) = %d\n", sizeof(long long));
1690 printf("%Ld %Ld\n", a
, b
);
1691 printf("%Ld %Ld %Ld %Lx\n",
1695 0x1234567812345679);
1700 lloptest(0xff, 0x1234);
1701 b
= 0x72345678 << 10;
1705 b
= 0x72345678LL
<< 10;
1716 /* long long reg spill test */
1721 printf("%lld\n", value(&a
));
1723 lloptest(0x80000000, 0);
1725 /* another long long spill test */
1731 printf("%lld\n", *p
);
1736 printf("%d %d %d %d\n", a
> b
, a
< b
, a
>= b
, a
<= b
);
1738 printf("%Ld\n", 0x123456789LLU
);
1741 void manyarg_test(void)
1743 long double ld
= 1234567891234LL;
1744 printf("manyarg_test:\n");
1745 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
1746 1, 2, 3, 4, 5, 6, 7, 8,
1747 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
1748 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1750 1, 2, 3, 4, 5, 6, 7, 8,
1751 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1752 1234567891234LL, 987654321986LL,
1754 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1756 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
1757 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1758 1234567891234LL, 987654321986LL,
1760 /* XXX: known bug of x86-64 */
1762 printf("%d %d %d %d %d %d %d %d %Lf\n",
1763 1, 2, 3, 4, 5, 6, 7, 8, ld
);
1764 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1765 "%Ld %Ld %f %f %Lf\n",
1766 1, 2, 3, 4, 5, 6, 7, 8,
1767 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1768 1234567891234LL, 987654321986LL,
1770 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1771 "%Lf %Ld %Ld %f %f %Lf\n",
1772 1, 2, 3, 4, 5, 6, 7, 8,
1773 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1774 ld
, 1234567891234LL, 987654321986LL,
1779 void vprintf1(const char *fmt
, ...)
1802 i
= va_arg(ap
, int);
1806 d
= va_arg(ap
, double);
1810 ll
= va_arg(ap
, long long);
1814 ld
= va_arg(ap
, long double);
1828 void stdarg_test(void)
1830 long double ld
= 1234567891234LL;
1831 vprintf1("%d %d %d\n", 1, 2, 3);
1832 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1833 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1834 vprintf1("%F %F %F\n", 1.2L, 2.3L, 3.4L);
1836 /* a bug of x86's TCC */
1837 vprintf1("%d %f %l %F %d %f %l %F\n",
1838 1, 1.2, 3L, 4.5L, 6, 7.8, 9L, 0.1L);
1840 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
1841 1, 2, 3, 4, 5, 6, 7, 8,
1842 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
1843 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
1844 1, 2, 3, 4, 5, 6, 7, 8,
1845 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
1846 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1848 1, 2, 3, 4, 5, 6, 7, 8,
1849 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1850 1234567891234LL, 987654321986LL,
1852 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1854 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
1855 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1856 1234567891234LL, 987654321986LL,
1858 vprintf1("%d %d %d %d %d %d %d %d %F\n",
1859 1, 2, 3, 4, 5, 6, 7, 8, ld
);
1860 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1862 1, 2, 3, 4, 5, 6, 7, 8,
1863 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1864 1234567891234LL, 987654321986LL,
1866 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1867 "%F %l %l %f %f %F\n",
1868 1, 2, 3, 4, 5, 6, 7, 8,
1869 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1870 ld
, 1234567891234LL, 987654321986LL,
1874 void whitespace_test(void)
1880 ntf("whitspace:\n");\f\v
1884 #ifdef CORRECT_CR_HANDLING
1893 #ifdef ACCEPT_CR_IN_STRINGS
1894 printf("len1=%d\n", strlen("
1896 #ifdef CORRECT_CR_HANDLING
1899 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
1901 printf("len1=%d\n", strlen("
a
1903 #endif /* ACCEPT_CR_IN_STRINGS */
1906 int reltab
[3] = { 1, 2, 3 };
1908 int *rel1
= &reltab
[1];
1909 int *rel2
= &reltab
[2];
1911 void relocation_test(void)
1913 printf("*rel1=%d\n", *rel1
);
1914 printf("*rel2=%d\n", *rel2
);
1917 void old_style_f(a
,b
,c
)
1921 printf("a=%d b=%d b=%f\n", a
, b
, c
);
1924 void decl_func1(int cmpfn())
1926 printf("cmpfn=%lx\n", (long)cmpfn
);
1929 void decl_func2(cmpfn
)
1932 printf("cmpfn=%lx\n", (long)cmpfn
);
1935 void old_style_function(void)
1937 old_style_f((void *)1, 2, 3.0);
1944 #if defined __i386__ || defined __x86_64__
1945 char *p
= alloca(16);
1946 strcpy(p
,"123456789012345");
1947 printf("alloca: p is %s\n", p
);
1948 char *demo
= "This is only a test.\n";
1949 /* Test alloca embedded in a larger expression */
1950 printf("alloca: %s\n", strcpy(alloca(strlen(demo
)+1),demo
) );
1954 void sizeof_test(void)
1959 printf("sizeof(int) = %d\n", sizeof(int));
1960 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1961 printf("sizeof(long) = %d\n", sizeof(long));
1962 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
1963 printf("sizeof(short) = %d\n", sizeof(short));
1964 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1965 printf("sizeof(char) = %d\n", sizeof(char));
1966 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1967 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1969 printf("sizeof(a++) = %d\n", sizeof a
++);
1970 printf("a=%d\n", a
);
1972 printf("sizeof(**ptr) = %d\n", sizeof (**ptr
));
1974 /* some alignof tests */
1975 printf("__alignof__(int) = %d\n", __alignof__(int));
1976 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1977 printf("__alignof__(short) = %d\n", __alignof__(short));
1978 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1979 printf("__alignof__(char) = %d\n", __alignof__(char));
1980 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1981 printf("__alignof__(func) = %d\n", __alignof__
sizeof_test());
1984 void typeof_test(void)
1993 printf("a=%f b=%f c=%f\n", a
, b
, c
);
1996 void statement_expr_test(void)
2009 printf("a=%d\n", a
);
2013 void local_label_test(void)
2019 __label__ l1
, l2
, l3
, l4
;
2033 printf("a=%d\n", a
);
2043 /* inline assembler test */
2046 /* from linux kernel */
2047 static char * strncat1(char * dest
,const char * src
,size_t count
)
2050 __asm__
__volatile__(
2059 "testb %%al,%%al\n\t"
2061 "2:\txorl %2,%2\n\t"
2063 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
2064 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
2069 static inline void * memcpy1(void * to
, const void * from
, size_t n
)
2072 __asm__
__volatile__(
2077 "1:\ttestb $1,%b4\n\t"
2081 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
2082 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
2087 static __inline__
void sigaddset1(unsigned int *set
, int _sig
)
2089 __asm__("btsl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
2092 static __inline__
void sigdelset1(unsigned int *set
, int _sig
)
2094 asm("btrl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
2097 static __inline__ __const__
unsigned int swab32(unsigned int x
)
2099 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2100 "rorl $16,%0\n\t" /* swap words */
2101 "xchgb %b0,%h0" /* swap higher bytes */
2107 static __inline__
unsigned long long mul64(unsigned int a
, unsigned int b
)
2109 unsigned long long res
;
2110 __asm__("mull %2" : "=A" (res
) : "a" (a
), "r" (b
));
2114 static __inline__
unsigned long long inc64(unsigned long long a
)
2116 unsigned long long res
;
2117 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res
) : "A" (a
));
2128 printf("inline asm:\n");
2129 /* test the no operand case */
2130 asm volatile ("xorl %eax, %eax");
2132 memcpy1(buf
, "hello", 6);
2133 strncat1(buf
, " worldXXXXX", 3);
2134 printf("%s\n", buf
);
2136 /* 'A' constraint test */
2137 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2138 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2141 sigdelset1(&set
, 2);
2142 sigaddset1(&set
, 16);
2143 /* NOTE: we test here if C labels are correctly restored after the
2147 __asm__("btsl %1,%0" : "=m"(set
) : "Ir"(20) : "cc");
2148 #ifdef __GNUC__ // works strange with GCC 4.3
2151 printf("set=0x%x\n", set
);
2153 printf("swab32(0x%08x) = 0x%0x\n", val
, swab32(val
));
2167 #define COMPAT_TYPE(type1, type2) \
2169 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2170 __builtin_types_compatible_p (type1, type2));\
2175 void builtin_test(void)
2178 COMPAT_TYPE(int, int);
2179 COMPAT_TYPE(int, unsigned int);
2180 COMPAT_TYPE(int, char);
2181 COMPAT_TYPE(int, const int);
2182 COMPAT_TYPE(int, volatile int);
2183 COMPAT_TYPE(int *, int *);
2184 COMPAT_TYPE(int *, void *);
2185 COMPAT_TYPE(int *, const int *);
2186 COMPAT_TYPE(char *, unsigned char *);
2187 /* space is needed because tcc preprocessor introduces a space between each token */
2188 COMPAT_TYPE(char * *, void *);
2190 printf("res = %d\n", __builtin_constant_p(1));
2191 printf("res = %d\n", __builtin_constant_p(1 + 2));
2192 printf("res = %d\n", __builtin_constant_p(&constant_p_var
));
2193 printf("res = %d\n", __builtin_constant_p(constant_p_var
));
2197 void const_func(const int a
)
2201 void const_warn_test(void)