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
]);
601 printf("%d\n", a
+= 1);
602 printf("%d\n", a
-= 2);
603 printf("%d\n", a
*= 31232132);
604 printf("%d\n", a
/= 4);
605 printf("%d\n", a
%= 20);
606 printf("%d\n", a
&= 6);
607 printf("%d\n", a
^= 7);
608 printf("%d\n", a
|= 8);
609 printf("%d\n", a
>>= 3);
610 printf("%d\n", a
<<= 4);
614 printf("%d\n", a
+ 1);
615 printf("%d\n", a
- 2);
616 printf("%d\n", a
* 312);
617 printf("%d\n", a
/ 4);
618 printf("%d\n", b
/ 4);
619 printf("%d\n", (unsigned)b
/ 4);
620 printf("%d\n", a
% 20);
621 printf("%d\n", b
% 20);
622 printf("%d\n", (unsigned)b
% 20);
623 printf("%d\n", a
& 6);
624 printf("%d\n", a
^ 7);
625 printf("%d\n", a
| 8);
626 printf("%d\n", a
>> 3);
627 printf("%d\n", b
>> 3);
628 printf("%d\n", (unsigned)b
>> 3);
629 printf("%d\n", a
<< 4);
634 printf("%d\n", 12 + 1);
635 printf("%d\n", 12 - 2);
636 printf("%d\n", 12 * 312);
637 printf("%d\n", 12 / 4);
638 printf("%d\n", 12 % 20);
639 printf("%d\n", 12 & 6);
640 printf("%d\n", 12 ^ 7);
641 printf("%d\n", 12 | 8);
642 printf("%d\n", 12 >> 2);
643 printf("%d\n", 12 << 4);
647 printf("%d %d %d %d\n",
656 return (c
>= 'a' & c
<= 'z') | (c
>= 'A' & c
<= 'Z') | c
== '_';
659 /**********************/
661 int vstack
[10], *vstack_ptr
;
663 void vpush(int vt
, int vc
)
669 void vpop(int *ft
, int *fc
)
682 vstack_ptr
[-2] &= ~0xffffff80;
684 printf("res= %d %d\n", a
, b
);
687 void constant_expr_test()
690 printf("constant_expr:\n");
692 printf("%d\n", a
* 16);
693 printf("%d\n", a
* 1);
694 printf("%d\n", a
+ 0);
704 printf("expr_ptr:\n");
707 printf("diff=%d\n", q
- p
);
709 printf("inc=%d\n", p
- tab4
);
711 printf("dec=%d\n", p
- tab4
);
713 printf("inc=%d\n", p
- tab4
);
715 printf("dec=%d\n", p
- tab4
);
716 printf("add=%d\n", p
+ 3 - tab4
);
717 printf("add=%d\n", 3 + p
- tab4
);
719 /* check if 64bit support is ok */
722 printf("%p %p %ld\n", q
, p
, p
-q
);
723 printf("%d %d %d %d %d %d\n",
724 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
727 printf("%p %p %ld\n", q
, p
, p
-q
);
728 printf("%d %d %d %d %d %d\n",
729 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
730 p
= (int *)((char *)p
+ 0xf0000000);
731 printf("%p %p %ld\n", q
, p
, p
-q
);
732 printf("%d %d %d %d %d %d\n",
733 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
735 printf("%p %p %ld\n", q
, p
, p
-q
);
736 printf("%d %d %d %d %d %d\n",
737 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
742 struct size12 s
[2], *sp
= s
;
747 printf("%d\n", sp
[j
].i
);
754 printf("constant_expr:\n");
757 printf("%d\n", a
== a
);
758 printf("%d\n", a
!= a
);
760 printf("%d\n", a
< b
);
761 printf("%d\n", a
<= b
);
762 printf("%d\n", a
<= a
);
763 printf("%d\n", b
>= a
);
764 printf("%d\n", a
>= a
);
765 printf("%d\n", b
> a
);
767 printf("%d\n", (unsigned)a
< b
);
768 printf("%d\n", (unsigned)a
<= b
);
769 printf("%d\n", (unsigned)a
<= a
);
770 printf("%d\n", (unsigned)b
>= a
);
771 printf("%d\n", (unsigned)a
>= a
);
772 printf("%d\n", (unsigned)b
> a
);
801 printf("sizes: %d %d %d %d\n",
802 sizeof(struct struct1
),
803 sizeof(struct struct2
),
804 sizeof(union union1
),
805 sizeof(union union2
));
809 printf("st1: %d %d %d\n",
810 st1
.f1
, st1
.f2
, st1
.f3
);
813 printf("union1: %d\n", st1
.u
.v1
);
816 printf("union2: %d\n", u
.w1
);
821 printf("st2: %d %d %d\n",
822 s
->f1
, s
->f2
, s
->f3
);
823 printf("str_addr=%x\n", (int)st1
.str
- (int)&st1
.f1
);
825 /* align / size tests */
826 printf("aligntest1 sizeof=%d alignof=%d\n",
827 sizeof(struct aligntest1
), __alignof__(struct aligntest1
));
828 printf("aligntest2 sizeof=%d alignof=%d\n",
829 sizeof(struct aligntest2
), __alignof__(struct aligntest2
));
830 printf("aligntest3 sizeof=%d alignof=%d\n",
831 sizeof(struct aligntest3
), __alignof__(struct aligntest3
));
832 printf("aligntest4 sizeof=%d alignof=%d\n",
833 sizeof(struct aligntest4
), __alignof__(struct aligntest4
));
835 /* empty structures (GCC extension) */
836 printf("sizeof(struct empty) = %d\n", sizeof(struct empty
));
837 printf("alignof(struct empty) = %d\n", __alignof__(struct empty
));
840 /* XXX: depend on endianness */
841 void char_short_test()
845 printf("char_short:\n");
850 *(char *)&var1
, *(char *)&var2
);
852 *(unsigned char *)&var1
, *(unsigned char *)&var2
);
853 printf("s16=%d %d\n",
854 *(short *)&var1
, *(short *)&var2
);
855 printf("u16=%d %d\n",
856 *(unsigned short *)&var1
, *(unsigned short *)&var2
);
857 printf("s32=%d %d\n",
858 *(int *)&var1
, *(int *)&var2
);
859 printf("u32=%d %d\n",
860 *(unsigned int *)&var1
, *(unsigned int *)&var2
);
861 *(char *)&var1
= 0x08;
862 printf("var1=%x\n", var1
);
863 *(short *)&var1
= 0x0809;
864 printf("var1=%x\n", var1
);
865 *(int *)&var1
= 0x08090a0b;
866 printf("var1=%x\n", var1
);
879 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
880 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
882 static int toupper1(int a
)
889 int *s
, a
, b
, t
, f
, i
;
893 printf("!s=%d\n", !s
);
899 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
900 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
901 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
916 printf("b=%d\n", a
+ (0 ? 1 : a
/ 2));
918 /* test register spilling */
921 a
= (a
+ b
) * ((a
< b
) ?
922 ((b
- a
) * (a
- b
)): a
+ b
);
925 /* test complex || or && expressions */
929 printf("exp=%d\n", f
== (32 <= a
&& a
<= 3));
930 printf("r=%d\n", (t
|| f
) + (t
&& f
));
935 int aspect_native
= 65536;
936 double bfu_aspect
= 1.0;
938 for(aspect_on
= 0; aspect_on
< 2; aspect_on
++) {
939 aspect
=aspect_on
?(aspect_native
*bfu_aspect
+0.5):65535UL;
940 printf("aspect=%d\n", aspect
);
944 /* test ? : GCC extension */
946 static int v1
= 34 ? : -1; /* constant case */
947 static int v2
= 0 ? : -1; /* constant case */
950 printf("%d %d\n", v1
, v2
);
951 printf("%d %d\n", a
- 30 ? : a
* 2, a
+ 1 ? : a
* 2);
954 /* again complex expression */
956 if (toupper1 (i
) != TOUPPER (i
))
957 printf("error %d\n", i
);
961 /* GCC accepts that */
962 static int tab_reinit
[];
963 static int tab_reinit
[10];
965 //int cinit1; /* a global variable can be defined several times without error ! */
969 int *cinit2
= (int []){3, 2, 1};
971 void compound_literal_test(void)
976 printf("compound_test:\n");
978 p
= (int []){1, 2, 3};
984 printf("%d", cinit2
[i
]);
988 printf("q1=%s\n", q
);
990 q
= (char *){ "tralala2" };
991 printf("q2=%s\n", q
);
994 printf("q3=%s\n", q3
);
996 q
= (char []){ "tralala3" };
997 printf("q4=%s\n", q
);
1000 p
= (int []){1, 2, cinit1
+ 3};
1002 printf(" %d", p
[i
]);
1006 p
= (int []){1, 2, 4 + i
};
1007 printf("%d %d %d\n",
1029 printf("kr_test:\n");
1030 printf("func1=%d\n", kr_func1(3, 4));
1031 printf("func2=%d\n", kr_func2(3, 4));
1038 tab
= (char*)malloc(20);
1054 /* structure assignment tests */
1060 struct structa1 ssta1
;
1062 void struct_assign_test1(struct structa1 s1
, int t
)
1064 printf("%d %d %d\n", s1
.f1
, s1
.f2
, t
);
1067 struct structa1
struct_assign_test2(struct structa1 s1
, int t
)
1074 void struct_assign_test(void)
1076 struct structa1 lsta1
, lsta2
;
1079 printf("struct_assign_test:\n");
1083 printf("%d %d\n", lsta1
.f1
, lsta1
.f2
);
1085 printf("%d %d\n", lsta2
.f1
, lsta2
.f2
);
1090 struct_assign_test1(lsta2
, 3);
1092 printf("before call: %d %d\n", lsta2
.f1
, lsta2
.f2
);
1093 lsta2
= struct_assign_test2(lsta2
, 4);
1094 printf("after call: %d %d\n", lsta2
.f1
, lsta2
.f2
);
1097 /* casts to short/char */
1099 void cast1(char a
, short b
, unsigned char c
, unsigned short d
)
1101 printf("%d %d %d %d\n", a
, b
, c
, d
);
1115 p
-= 0x700000000042;
1117 printf("cast_test:\n");
1121 printf("%d %d %d %d\n",
1124 (unsigned char)(a
+ 1),
1125 (unsigned short)(a
+ 1));
1126 printf("%d %d %d %d\n",
1129 (unsigned char)0xfffff,
1130 (unsigned short)0xfffff);
1132 a
= (bcast
= 128) + 1;
1134 a
= (scast
= 65536) + 1;
1137 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
1139 /* test cast from unsigned to signed short to int */
1142 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b
, d
);
1145 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b
, d
);
1147 /* test implicit int casting for array accesses */
1151 printf("%d %d\n", tab
[0], tab
[1]);
1153 /* test implicit casting on some operators */
1154 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1155 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1156 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1158 /* from pointer to integer types */
1159 printf("%d %d %ld %ld %lld %lld\n",
1160 (int)p
, (unsigned int)p
,
1161 (long)p
, (unsigned long)p
,
1162 (long long)p
, (unsigned long long)p
);
1164 /* from integers to pointers */
1165 printf("%p %p %p %p\n",
1166 (void *)a
, (void *)b
, (void *)c
, (void *)d
);
1169 /* initializers tests */
1170 struct structinit1
{
1179 int sinit3
[3] = { 1, 2, {{3}}, };
1180 int sinit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1181 int sinit5
[3][2] = { 1, 2, 3, 4, 5, 6 };
1182 int sinit6
[] = { 1, 2, 3 };
1183 int sinit7
[] = { [2] = 3, [0] = 1, 2 };
1184 char sinit8
[] = "hello" "trala";
1186 struct structinit1 sinit9
= { 1, 2, 3 };
1187 struct structinit1 sinit10
= { .f2
= 2, 3, .f1
= 1 };
1188 struct structinit1 sinit11
= { .f2
= 2, 3, .f1
= 1,
1196 char *sinit12
= "hello world";
1202 char sinit14
[10] = { "abc" };
1203 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
1205 struct { int a
[3], b
; } sinit16
[] = { { 1 }, 2 };
1221 void init_test(void)
1225 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1226 int linit6
[] = { 1, 2, 3 };
1228 char linit8
[] = "hello" "trala";
1229 int linit12
[10] = { 1, 2 };
1230 int linit13
[10] = { 1, 2, [7] = 3, [3] = 4, };
1231 char linit14
[10] = "abc";
1232 int linit15
[10] = { linit1
, linit1
+ 1, [6] = linit1
+ 2, };
1233 struct linit16
{ int a1
, a2
, a3
, a4
; } linit16
= { 1, .a3
= 2 };
1234 int linit17
= sizeof(linit17
);
1236 printf("init_test:\n");
1238 printf("sinit1=%d\n", sinit1
);
1239 printf("sinit2=%d\n", sinit2
);
1240 printf("sinit3=%d %d %d %d\n",
1246 printf("sinit6=%d\n", sizeof(sinit6
));
1247 printf("sinit7=%d %d %d %d\n",
1253 printf("sinit8=%s\n", sinit8
);
1254 printf("sinit9=%d %d %d\n",
1259 printf("sinit10=%d %d %d\n",
1264 printf("sinit11=%d %d %d %d %d %d\n",
1275 printf("[%d][%d] = %d %d %d\n",
1276 i
, j
, sinit4
[i
][j
], sinit5
[i
][j
], linit4
[i
][j
]);
1277 printf("linit1=%d\n", linit1
);
1278 printf("linit2=%d\n", linit2
);
1279 printf("linit6=%d\n", sizeof(linit6
));
1280 printf("linit8=%d %s\n", sizeof(linit8
), linit8
);
1282 printf("sinit12=%s\n", sinit12
);
1283 printf("sinit13=%d %s %s %s\n",
1288 printf("sinit14=%s\n", sinit14
);
1290 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
1292 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
1294 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
1296 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
1298 printf("%d %d %d %d\n",
1303 /* test that initialisation is done after variable declare */
1304 printf("linit17=%d\n", linit17
);
1305 printf("sinit15=%d\n", sinit15
[0]);
1306 printf("sinit16=%d %d\n", sinit16
[0].a
[0], sinit16
[1].a
[0]);
1307 printf("sinit17=%s %d %s %d\n",
1308 sinit17
[0].s
, sinit17
[0].len
,
1309 sinit17
[1].s
, sinit17
[1].len
);
1311 printf("%x ", sinit18
[i
]);
1340 /* ISOC99 _Bool type */
1341 void c99_bool_test(void)
1347 printf("bool_test:\n");
1348 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
1350 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
1352 printf("b = %d\n", b
);
1354 printf("b = %d\n", b
);
1358 void bitfield_test(void)
1368 unsigned int f5
: 7;
1370 printf("bitfield_test:");
1371 printf("sizeof(st1) = %d\n", sizeof(st1
));
1380 printf("%d %d %d %d %d\n",
1381 st1
.f1
, st1
.f2
, st1
.f3
, st1
.f4
, st1
.f5
);
1385 printf("st1.f1 == -1\n");
1387 printf("st1.f1 != -1\n");
1389 printf("st1.f2 == -1\n");
1391 printf("st1.f2 != -1\n");
1393 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1394 long-long bitfields whose size is not bigger than int */
1399 unsigned long long f3
: 38;
1401 st2
.f1
= 0x123456789ULL
;
1403 st2
.f2
= (long long)a
<< 25;
1406 printf("%lld %lld %lld\n", st2
.f1
, st2
.f2
, st2
.f3
);
1410 #define FLOAT_FMT "%f\n"
1412 /* x86's float isn't compatible with GCC */
1413 #define FLOAT_FMT "%.5f\n"
1416 /* declare strto* functions as they are C99 */
1417 double strtod(const char *nptr
, char **endptr
);
1418 float strtof(const char *nptr
, char **endptr
);
1419 long double strtold(const char *nptr
, char **endptr
);
1421 #define FTEST(prefix, type, fmt)\
1422 void prefix ## cmp(type a, type b)\
1424 printf("%d %d %d %d %d %d\n",\
1431 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1439 printf(fmt "\n", ++a);\
1440 printf(fmt "\n", a++);\
1441 printf(fmt "\n", a);\
1443 printf("%d %d\n", !a, !b);\
1445 void prefix ## fcast(type a)\
1456 printf("ftof: %f %f %Lf\n", fa, da, la);\
1458 ua = (unsigned int)a;\
1459 printf("ftoi: %d %u\n", ia, ua);\
1463 printf("itof: " fmt "\n", b);\
1465 printf("utof: " fmt "\n", b);\
1468 float prefix ## retf(type a) { return a; }\
1469 double prefix ## retd(type a) { return a; }\
1470 long double prefix ## retld(type a) { return a; }\
1472 void prefix ## call(void)\
1474 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1475 printf("double: %f\n", prefix ## retd(42.123456789));\
1476 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1477 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
1480 void prefix ## test(void)\
1482 printf("testing '%s'\n", #type);\
1483 prefix ## cmp(1, 2.5);\
1484 prefix ## cmp(2, 1.5);\
1485 prefix ## cmp(1, 1);\
1486 prefix ## fcast(234.6);\
1487 prefix ## fcast(-2334.6);\
1491 FTEST(f
, float, "%f")
1492 FTEST(d
, double, "%f")
1493 FTEST(ld
, long double, "%Lf")
1495 double ftab1
[3] = { 1.2, 3.4, -5.6 };
1498 void float_test(void)
1505 printf("float_test:\n");
1506 printf("sizeof(float) = %d\n", sizeof(float));
1507 printf("sizeof(double) = %d\n", sizeof(double));
1508 printf("sizeof(long double) = %d\n", sizeof(long double));
1512 printf("%f %f %f\n", ftab1
[0], ftab1
[1], ftab1
[2]);
1513 printf("%f %f %f\n", 2.12, .5, 2.3e10
);
1514 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1516 printf("da=%f\n", da
);
1518 printf("fa=%f\n", fa
);
1521 printf("da = %f\n", da
);
1524 printf("db = %f\n", db
);
1532 return fib(n
-1) + fib(n
-2);
1544 printf("funcptr:\n");
1551 /* more complicated pointer computation */
1554 printf("sizeof1 = %d\n", sizeof(funcptr_test
));
1555 printf("sizeof2 = %d\n", sizeof funcptr_test
);
1556 printf("sizeof3 = %d\n", sizeof(&funcptr_test
));
1557 printf("sizeof4 = %d\n", sizeof &funcptr_test
);
1560 void lloptest(long long a
, long long b
)
1562 unsigned long long ua
, ub
;
1567 printf("arith: %Ld %Ld %Ld\n",
1573 printf("arith1: %Ld %Ld\n",
1579 printf("bin: %Ld %Ld %Ld\n",
1585 printf("test: %d %d %d %d %d %d\n",
1593 printf("utest: %d %d %d %d %d %d\n",
1604 printf("arith2: %Ld %Ld\n", a
, b
);
1605 printf("arith2: %Ld %Ld\n", a
++, b
++);
1606 printf("arith2: %Ld %Ld\n", --a
, --b
);
1607 printf("arith2: %Ld %Ld\n", a
, b
);
1609 printf("not: %d %d %d %d\n", !a
, !ua
, !b
, !ub
);
1612 void llshift(long long a
, int b
)
1614 printf("shift: %Ld %Ld %Ld\n",
1615 (unsigned long long)a
>> b
,
1618 printf("shiftc: %Ld %Ld %Ld\n",
1619 (unsigned long long)a
>> 3,
1622 printf("shiftc: %Ld %Ld %Ld\n",
1623 (unsigned long long)a
>> 35,
1633 long long la
, lb
, lc
;
1634 unsigned long long ula
, ulb
, ulc
;
1637 la
= (la
<< 20) | 0x12345;
1639 printf("la=%Ld ula=%Lu\n", la
, ula
);
1644 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
1649 printf("ftoll: %Ld %Ld %Ld\n", la
, lb
, lc
);
1654 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
1659 printf("ftoull: %Lu %Lu %Lu\n", ula
, ulb
, ulc
);
1662 long long llfunc1(int a
)
1672 long long int value(struct S
*v
)
1674 return ((long long int)v
->item
);
1677 void longlong_test(void)
1682 printf("longlong_test:\n");
1683 printf("sizeof(long long) = %d\n", sizeof(long long));
1688 printf("%Ld %Ld\n", a
, b
);
1689 printf("%Ld %Ld %Ld %Lx\n",
1693 0x1234567812345679);
1698 lloptest(0xff, 0x1234);
1699 b
= 0x72345678 << 10;
1703 b
= 0x72345678LL
<< 10;
1714 /* long long reg spill test */
1719 printf("%lld\n", value(&a
));
1721 lloptest(0x80000000, 0);
1723 /* another long long spill test */
1729 printf("%lld\n", *p
);
1734 printf("%d %d %d %d\n", a
> b
, a
< b
, a
>= b
, a
<= b
);
1736 printf("%Ld\n", 0x123456789LLU
);
1739 void manyarg_test(void)
1741 long double ld
= 1234567891234LL;
1742 printf("manyarg_test:\n");
1743 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
1744 1, 2, 3, 4, 5, 6, 7, 8,
1745 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
1746 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1748 1, 2, 3, 4, 5, 6, 7, 8,
1749 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1750 1234567891234LL, 987654321986LL,
1752 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1754 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
1755 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1756 1234567891234LL, 987654321986LL,
1758 /* XXX: known bug of x86-64 */
1760 printf("%d %d %d %d %d %d %d %d %Lf\n",
1761 1, 2, 3, 4, 5, 6, 7, 8, ld
);
1762 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1763 "%Ld %Ld %f %f %Lf\n",
1764 1, 2, 3, 4, 5, 6, 7, 8,
1765 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1766 1234567891234LL, 987654321986LL,
1768 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1769 "%Lf %Ld %Ld %f %f %Lf\n",
1770 1, 2, 3, 4, 5, 6, 7, 8,
1771 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1772 ld
, 1234567891234LL, 987654321986LL,
1777 void vprintf1(const char *fmt
, ...)
1800 i
= va_arg(ap
, int);
1804 d
= va_arg(ap
, double);
1808 ll
= va_arg(ap
, long long);
1812 ld
= va_arg(ap
, long double);
1826 void stdarg_test(void)
1828 long double ld
= 1234567891234LL;
1829 vprintf1("%d %d %d\n", 1, 2, 3);
1830 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1831 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1832 vprintf1("%F %F %F\n", 1.2L, 2.3L, 3.4L);
1834 /* a bug of x86's TCC */
1835 vprintf1("%d %f %l %F %d %f %l %F\n",
1836 1, 1.2, 3L, 4.5L, 6, 7.8, 9L, 0.1L);
1838 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
1839 1, 2, 3, 4, 5, 6, 7, 8,
1840 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
1841 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
1842 1, 2, 3, 4, 5, 6, 7, 8,
1843 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
1844 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1846 1, 2, 3, 4, 5, 6, 7, 8,
1847 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1848 1234567891234LL, 987654321986LL,
1850 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1852 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
1853 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1854 1234567891234LL, 987654321986LL,
1856 vprintf1("%d %d %d %d %d %d %d %d %F\n",
1857 1, 2, 3, 4, 5, 6, 7, 8, ld
);
1858 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1860 1, 2, 3, 4, 5, 6, 7, 8,
1861 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1862 1234567891234LL, 987654321986LL,
1864 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1865 "%F %l %l %f %f %F\n",
1866 1, 2, 3, 4, 5, 6, 7, 8,
1867 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1868 ld
, 1234567891234LL, 987654321986LL,
1872 void whitespace_test(void)
1878 ntf("whitspace:\n");\f\v
1882 #ifdef CORRECT_CR_HANDLING
1891 #ifdef ACCEPT_CR_IN_STRINGS
1892 printf("len1=%d\n", strlen("
1894 #ifdef CORRECT_CR_HANDLING
1897 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
1899 printf("len1=%d\n", strlen("
a
1901 #endif /* ACCEPT_CR_IN_STRINGS */
1904 int reltab
[3] = { 1, 2, 3 };
1906 int *rel1
= &reltab
[1];
1907 int *rel2
= &reltab
[2];
1909 void relocation_test(void)
1911 printf("*rel1=%d\n", *rel1
);
1912 printf("*rel2=%d\n", *rel2
);
1915 void old_style_f(a
,b
,c
)
1919 printf("a=%d b=%d b=%f\n", a
, b
, c
);
1922 void decl_func1(int cmpfn())
1924 printf("cmpfn=%lx\n", (long)cmpfn
);
1927 void decl_func2(cmpfn
)
1930 printf("cmpfn=%lx\n", (long)cmpfn
);
1933 void old_style_function(void)
1935 old_style_f((void *)1, 2, 3.0);
1942 #if defined __i386__ || defined __x86_64__
1943 char *p
= alloca(16);
1944 strcpy(p
,"123456789012345");
1945 printf("alloca: p is %s\n", p
);
1946 char *demo
= "This is only a test.\n";
1947 /* Test alloca embedded in a larger expression */
1948 printf("alloca: %s\n", strcpy(alloca(strlen(demo
)+1),demo
) );
1952 void sizeof_test(void)
1957 printf("sizeof(int) = %d\n", sizeof(int));
1958 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1959 printf("sizeof(long) = %d\n", sizeof(long));
1960 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
1961 printf("sizeof(short) = %d\n", sizeof(short));
1962 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1963 printf("sizeof(char) = %d\n", sizeof(char));
1964 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1965 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1967 printf("sizeof(a++) = %d\n", sizeof a
++);
1968 printf("a=%d\n", a
);
1970 printf("sizeof(**ptr) = %d\n", sizeof (**ptr
));
1972 /* some alignof tests */
1973 printf("__alignof__(int) = %d\n", __alignof__(int));
1974 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1975 printf("__alignof__(short) = %d\n", __alignof__(short));
1976 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1977 printf("__alignof__(char) = %d\n", __alignof__(char));
1978 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1979 printf("__alignof__(func) = %d\n", __alignof__
sizeof_test());
1982 void typeof_test(void)
1991 printf("a=%f b=%f c=%f\n", a
, b
, c
);
1994 void statement_expr_test(void)
2007 printf("a=%d\n", a
);
2011 void local_label_test(void)
2017 __label__ l1
, l2
, l3
, l4
;
2031 printf("a=%d\n", a
);
2041 /* inline assembler test */
2044 /* from linux kernel */
2045 static char * strncat1(char * dest
,const char * src
,size_t count
)
2048 __asm__
__volatile__(
2057 "testb %%al,%%al\n\t"
2059 "2:\txorl %2,%2\n\t"
2061 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
2062 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
2067 static inline void * memcpy1(void * to
, const void * from
, size_t n
)
2070 __asm__
__volatile__(
2075 "1:\ttestb $1,%b4\n\t"
2079 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
2080 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
2085 static __inline__
void sigaddset1(unsigned int *set
, int _sig
)
2087 __asm__("btsl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
2090 static __inline__
void sigdelset1(unsigned int *set
, int _sig
)
2092 asm("btrl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
2095 static __inline__ __const__
unsigned int swab32(unsigned int x
)
2097 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2098 "rorl $16,%0\n\t" /* swap words */
2099 "xchgb %b0,%h0" /* swap higher bytes */
2105 static __inline__
unsigned long long mul64(unsigned int a
, unsigned int b
)
2107 unsigned long long res
;
2108 __asm__("mull %2" : "=A" (res
) : "a" (a
), "r" (b
));
2112 static __inline__
unsigned long long inc64(unsigned long long a
)
2114 unsigned long long res
;
2115 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res
) : "A" (a
));
2126 printf("inline asm:\n");
2127 /* test the no operand case */
2128 asm volatile ("xorl %eax, %eax");
2130 memcpy1(buf
, "hello", 6);
2131 strncat1(buf
, " worldXXXXX", 3);
2132 printf("%s\n", buf
);
2134 /* 'A' constraint test */
2135 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2136 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2139 sigdelset1(&set
, 2);
2140 sigaddset1(&set
, 16);
2141 /* NOTE: we test here if C labels are correctly restored after the
2145 __asm__("btsl %1,%0" : "=m"(set
) : "Ir"(20) : "cc");
2146 #ifdef __GNUC__ // works strange with GCC 4.3
2149 printf("set=0x%x\n", set
);
2151 printf("swab32(0x%08x) = 0x%0x\n", val
, swab32(val
));
2165 #define COMPAT_TYPE(type1, type2) \
2167 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2168 __builtin_types_compatible_p (type1, type2));\
2173 void builtin_test(void)
2176 COMPAT_TYPE(int, int);
2177 COMPAT_TYPE(int, unsigned int);
2178 COMPAT_TYPE(int, char);
2179 COMPAT_TYPE(int, const int);
2180 COMPAT_TYPE(int, volatile int);
2181 COMPAT_TYPE(int *, int *);
2182 COMPAT_TYPE(int *, void *);
2183 COMPAT_TYPE(int *, const int *);
2184 COMPAT_TYPE(char *, unsigned char *);
2185 /* space is needed because tcc preprocessor introduces a space between each token */
2186 COMPAT_TYPE(char * *, void *);
2188 printf("res = %d\n", __builtin_constant_p(1));
2189 printf("res = %d\n", __builtin_constant_p(1 + 2));
2190 printf("res = %d\n", __builtin_constant_p(&constant_p_var
));
2191 printf("res = %d\n", __builtin_constant_p(constant_p_var
));
2195 void const_func(const int a
)
2199 void const_warn_test(void)