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 sizeof_test(void);
79 void typeof_test(void);
80 void local_label_test(void);
81 void statement_expr_test(void);
83 void builtin_test(void);
87 void forward_ref(void);
93 #define M1(a, b) (a) + (b)
97 #define glue(a, b) a ## b
98 #define xglue(a, b) glue(a, b)
99 #define HIGHLOW "hello"
100 #define LOW LOW ", world"
102 #define min(a, b) ((a) < (b) ? (a) : (b))
105 #define dprintf(level,...) printf(__VA_ARGS__)
108 /* gcc vararg macros */
109 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
111 #define MACRO_NOARGS()
127 #define __INT64_C(c) c ## LL
128 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
136 #define spin_lock(lock) do { } while (0)
137 #define wq_spin_lock spin_lock
138 #define TEST2() wq_spin_lock(a)
140 void macro_test(void)
142 printf("macro:\n");\f\v
144 printf("aaa=%d\n", AAA
);
146 printf("min=%d\n", min(1, min(2, -1)));
148 printf("s1=%s\n", glue(HIGH
, LOW
));
149 printf("s2=%s\n", xglue(HIGH
, LOW
));
150 printf("s3=%s\n", str("c"));
151 printf("s4=%s\n", str(a1
));
152 printf("B3=%d\n", B3
);
155 printf("A defined\n");
158 printf("B defined\n");
161 printf("A defined\n");
163 printf("A not defined\n");
166 printf("B defined\n");
168 printf("B not defined\n");
172 printf("A defined\n");
174 printf("B1 defined\n");
176 printf("B1 not defined\n");
179 printf("A not defined\n");
181 printf("B2 defined\n");
183 printf("B2 not defined\n");
188 printf("test true1\n");
191 printf("test true2\n");
194 printf("test true3\n");
197 printf("test trueA\n");
200 printf("test trueB\n");
216 printf("__LINE__ defined\n");
219 printf("__LINE__=%d __FILE__=%s\n",
222 printf("__LINE__=%d __FILE__=%s\n",
225 printf("__LINE__=%d __FILE__=%s\n",
227 #line 227 "tcctest.c"
229 /* not strictly preprocessor, but we test it there */
231 printf("__func__ = %s\n", __func__
);
232 dprintf(1, "vaarg=%d\n", 1);
234 dprintf1(1, "vaarg1\n");
235 dprintf1(1, "vaarg1=%d\n", 2);
236 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
239 printf("func='%s'\n", __FUNCTION__
);
241 /* complicated macros in glibc */
242 printf("INT64_MIN=%Ld\n", INT64_MIN
);
252 /* macro function with argument outside the macro string */
253 #define MF_s MF_hello
254 #define MF_hello(msg) printf("%s\n",msg)
256 #define MF_t printf("tralala\n"); MF_hello
261 /* test macro substituion inside args (should not eat stream) */
262 printf("qq=%d\n", qq(qq
)(2));
264 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
265 null argument without a space. gcc 3.2 fixes that. */
268 printf("qq1=%d\n", qq1( ));
270 /* comment with stray handling *\
272 /* this is a valid *\/ comment */
273 /* this is a valid comment *\*/
277 /* test function macro substitution when the function name is
296 void ps(const char *s
)
308 const char foo1_string
[] = "\
317 printf("\141\1423\143\n");/* dezdez test */
318 printf("\x41\x42\x43\x3a\n");
319 printf("c=%c\n", 'r');
320 printf("wc=%C 0x%lx %C\n", L
'a', L
'\x1234', L
'c');
321 printf("foo1_string='%s'\n", foo1_string
);
323 printf("wstring=%S\n", L
"abc");
324 printf("wstring=%S\n", L
"abc" L
"def" "ghi");
325 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
326 printf("L'\\377'=%d L'\\xff'=%d\n", L
'\377', L
'\xff');
330 while ((b
= b
+ 1) < 96) {
334 printf("fib=%d\n", fib(33));
336 while (b
!= 0x80000000) {
349 for(i
= 0; i
< 10;i
++)
358 /* break/continue tests */
370 /* break/continue tests */
382 for(i
= 0;i
< 10;i
++) {
394 static void *label_table
[3] = { &&label1
, &&label2
, &&label3
};
407 /* we also test computed gotos (GCC extension) */
409 goto *label_table
[i
];
437 printf("enum:\n%d %d %d %d %d %d\n",
438 E0
, E1
, E2
, E3
, E4
, E5
);
440 printf("b1=%d\n", b1
);
456 printf("typedef:\n");
457 printf("a=%d\n", *a
);
459 printf("mytype2=%d\n", mytype2
);
464 printf("forward:\n");
470 void forward_ref(void)
472 printf("forward ok\n");
475 typedef struct struct1
{
495 struct struct1 st1
, st2
;
497 int main(int argc
, char **argv
)
515 constant_expr_test();
519 compound_literal_test();
521 struct_assign_test();
531 old_style_function();
534 statement_expr_test();
548 printf("g1=%d\n", g
);
556 printf("g2=%d\n", g
);
560 printf("g3=%d\n", g
);
564 printf("g4=%d\n", g
);
567 printf("g5=%d\n", g
);
570 void array_test(int a
[4])
575 printf("sizeof(a) = %d\n", sizeof(a
));
576 printf("sizeof(\"a\") = %d\n", sizeof("a"));
578 printf("sizeof(__func__) = %d\n", sizeof(__func__
));
580 printf("sizeof tab %d\n", sizeof(tab
));
581 printf("sizeof tab2 %d\n", sizeof tab2
);
585 printf("%d %d %d\n", tab
[0], tab
[1], tab
[2]);
588 tab2
[i
][j
] = 10 * i
+ j
;
590 printf(" %3d", ((int *)tab2
)[i
]);
599 printf("%d\n", a
+= 1);
600 printf("%d\n", a
-= 2);
601 printf("%d\n", a
*= 31232132);
602 printf("%d\n", a
/= 4);
603 printf("%d\n", a
%= 20);
604 printf("%d\n", a
&= 6);
605 printf("%d\n", a
^= 7);
606 printf("%d\n", a
|= 8);
607 printf("%d\n", a
>>= 3);
608 printf("%d\n", a
<<= 4);
612 printf("%d\n", a
+ 1);
613 printf("%d\n", a
- 2);
614 printf("%d\n", a
* 312);
615 printf("%d\n", a
/ 4);
616 printf("%d\n", b
/ 4);
617 printf("%d\n", (unsigned)b
/ 4);
618 printf("%d\n", a
% 20);
619 printf("%d\n", b
% 20);
620 printf("%d\n", (unsigned)b
% 20);
621 printf("%d\n", a
& 6);
622 printf("%d\n", a
^ 7);
623 printf("%d\n", a
| 8);
624 printf("%d\n", a
>> 3);
625 printf("%d\n", b
>> 3);
626 printf("%d\n", (unsigned)b
>> 3);
627 printf("%d\n", a
<< 4);
632 printf("%d\n", 12 + 1);
633 printf("%d\n", 12 - 2);
634 printf("%d\n", 12 * 312);
635 printf("%d\n", 12 / 4);
636 printf("%d\n", 12 % 20);
637 printf("%d\n", 12 & 6);
638 printf("%d\n", 12 ^ 7);
639 printf("%d\n", 12 | 8);
640 printf("%d\n", 12 >> 2);
641 printf("%d\n", 12 << 4);
645 printf("%d %d %d %d\n",
654 return (c
>= 'a' & c
<= 'z') | (c
>= 'A' & c
<= 'Z') | c
== '_';
657 /**********************/
659 int vstack
[10], *vstack_ptr
;
661 void vpush(int vt
, int vc
)
667 void vpop(int *ft
, int *fc
)
680 vstack_ptr
[-2] &= ~0xffffff80;
682 printf("res= %d %d\n", a
, b
);
685 void constant_expr_test()
688 printf("constant_expr:\n");
690 printf("%d\n", a
* 16);
691 printf("%d\n", a
* 1);
692 printf("%d\n", a
+ 0);
702 printf("expr_ptr:\n");
705 printf("diff=%d\n", q
- p
);
707 printf("inc=%d\n", p
- tab4
);
709 printf("dec=%d\n", p
- tab4
);
711 printf("inc=%d\n", p
- tab4
);
713 printf("dec=%d\n", p
- tab4
);
714 printf("add=%d\n", p
+ 3 - tab4
);
715 printf("add=%d\n", 3 + p
- tab4
);
717 /* check if 64bit support is ok */
720 printf("%p %p %ld\n", q
, p
, p
-q
);
721 printf("%d %d %d %d %d %d\n",
722 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
725 printf("%p %p %ld\n", q
, p
, p
-q
);
726 printf("%d %d %d %d %d %d\n",
727 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
728 p
= (int *)((char *)p
+ 0xf0000000);
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
);
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
);
740 struct size12 s
[2], *sp
= s
;
745 printf("%d\n", sp
[j
].i
);
752 printf("constant_expr:\n");
755 printf("%d\n", a
== a
);
756 printf("%d\n", a
!= a
);
758 printf("%d\n", a
< b
);
759 printf("%d\n", a
<= b
);
760 printf("%d\n", a
<= a
);
761 printf("%d\n", b
>= a
);
762 printf("%d\n", a
>= a
);
763 printf("%d\n", b
> a
);
765 printf("%d\n", (unsigned)a
< b
);
766 printf("%d\n", (unsigned)a
<= b
);
767 printf("%d\n", (unsigned)a
<= a
);
768 printf("%d\n", (unsigned)b
>= a
);
769 printf("%d\n", (unsigned)a
>= a
);
770 printf("%d\n", (unsigned)b
> a
);
799 printf("sizes: %d %d %d %d\n",
800 sizeof(struct struct1
),
801 sizeof(struct struct2
),
802 sizeof(union union1
),
803 sizeof(union union2
));
807 printf("st1: %d %d %d\n",
808 st1
.f1
, st1
.f2
, st1
.f3
);
811 printf("union1: %d\n", st1
.u
.v1
);
814 printf("union2: %d\n", u
.w1
);
819 printf("st2: %d %d %d\n",
820 s
->f1
, s
->f2
, s
->f3
);
821 printf("str_addr=%x\n", (int)st1
.str
- (int)&st1
.f1
);
823 /* align / size tests */
824 printf("aligntest1 sizeof=%d alignof=%d\n",
825 sizeof(struct aligntest1
), __alignof__(struct aligntest1
));
826 printf("aligntest2 sizeof=%d alignof=%d\n",
827 sizeof(struct aligntest2
), __alignof__(struct aligntest2
));
828 printf("aligntest3 sizeof=%d alignof=%d\n",
829 sizeof(struct aligntest3
), __alignof__(struct aligntest3
));
830 printf("aligntest4 sizeof=%d alignof=%d\n",
831 sizeof(struct aligntest4
), __alignof__(struct aligntest4
));
833 /* empty structures (GCC extension) */
834 printf("sizeof(struct empty) = %d\n", sizeof(struct empty
));
835 printf("alignof(struct empty) = %d\n", __alignof__(struct empty
));
838 /* XXX: depend on endianness */
839 void char_short_test()
843 printf("char_short:\n");
848 *(char *)&var1
, *(char *)&var2
);
850 *(unsigned char *)&var1
, *(unsigned char *)&var2
);
851 printf("s16=%d %d\n",
852 *(short *)&var1
, *(short *)&var2
);
853 printf("u16=%d %d\n",
854 *(unsigned short *)&var1
, *(unsigned short *)&var2
);
855 printf("s32=%d %d\n",
856 *(int *)&var1
, *(int *)&var2
);
857 printf("u32=%d %d\n",
858 *(unsigned int *)&var1
, *(unsigned int *)&var2
);
859 *(char *)&var1
= 0x08;
860 printf("var1=%x\n", var1
);
861 *(short *)&var1
= 0x0809;
862 printf("var1=%x\n", var1
);
863 *(int *)&var1
= 0x08090a0b;
864 printf("var1=%x\n", var1
);
877 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
878 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
880 static int toupper1(int a
)
887 int *s
, a
, b
, t
, f
, i
;
891 printf("!s=%d\n", !s
);
897 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
898 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
899 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
914 printf("b=%d\n", a
+ (0 ? 1 : a
/ 2));
916 /* test register spilling */
919 a
= (a
+ b
) * ((a
< b
) ?
920 ((b
- a
) * (a
- b
)): a
+ b
);
923 /* test complex || or && expressions */
927 printf("exp=%d\n", f
== (32 <= a
&& a
<= 3));
928 printf("r=%d\n", (t
|| f
) + (t
&& f
));
933 int aspect_native
= 65536;
934 double bfu_aspect
= 1.0;
936 for(aspect_on
= 0; aspect_on
< 2; aspect_on
++) {
937 aspect
=aspect_on
?(aspect_native
*bfu_aspect
+0.5):65535UL;
938 printf("aspect=%d\n", aspect
);
942 /* test ? : GCC extension */
944 static int v1
= 34 ? : -1; /* constant case */
945 static int v2
= 0 ? : -1; /* constant case */
948 printf("%d %d\n", v1
, v2
);
949 printf("%d %d\n", a
- 30 ? : a
* 2, a
+ 1 ? : a
* 2);
952 /* again complex expression */
954 if (toupper1 (i
) != TOUPPER (i
))
955 printf("error %d\n", i
);
959 /* GCC accepts that */
960 static int tab_reinit
[];
961 static int tab_reinit
[10];
963 //int cinit1; /* a global variable can be defined several times without error ! */
967 int *cinit2
= (int []){3, 2, 1};
969 void compound_literal_test(void)
974 printf("compound_test:\n");
976 p
= (int []){1, 2, 3};
982 printf("%d", cinit2
[i
]);
986 printf("q1=%s\n", q
);
988 q
= (char *){ "tralala2" };
989 printf("q2=%s\n", q
);
992 printf("q3=%s\n", q3
);
994 q
= (char []){ "tralala3" };
995 printf("q4=%s\n", q
);
998 p
= (int []){1, 2, cinit1
+ 3};
1000 printf(" %d", p
[i
]);
1004 p
= (int []){1, 2, 4 + i
};
1005 printf("%d %d %d\n",
1027 printf("kr_test:\n");
1028 printf("func1=%d\n", kr_func1(3, 4));
1029 printf("func2=%d\n", kr_func2(3, 4));
1036 tab
= (char*)malloc(20);
1052 /* structure assignment tests */
1058 struct structa1 ssta1
;
1060 void struct_assign_test1(struct structa1 s1
, int t
)
1062 printf("%d %d %d\n", s1
.f1
, s1
.f2
, t
);
1065 struct structa1
struct_assign_test2(struct structa1 s1
, int t
)
1072 void struct_assign_test(void)
1074 struct structa1 lsta1
, lsta2
;
1077 printf("struct_assign_test:\n");
1081 printf("%d %d\n", lsta1
.f1
, lsta1
.f2
);
1083 printf("%d %d\n", lsta2
.f1
, lsta2
.f2
);
1088 struct_assign_test1(lsta2
, 3);
1090 printf("before call: %d %d\n", lsta2
.f1
, lsta2
.f2
);
1091 lsta2
= struct_assign_test2(lsta2
, 4);
1092 printf("after call: %d %d\n", lsta2
.f1
, lsta2
.f2
);
1095 /* casts to short/char */
1097 void cast1(char a
, short b
, unsigned char c
, unsigned short d
)
1099 printf("%d %d %d %d\n", a
, b
, c
, d
);
1113 p
-= 0x700000000042;
1115 printf("cast_test:\n");
1119 printf("%d %d %d %d\n",
1122 (unsigned char)(a
+ 1),
1123 (unsigned short)(a
+ 1));
1124 printf("%d %d %d %d\n",
1127 (unsigned char)0xfffff,
1128 (unsigned short)0xfffff);
1130 a
= (bcast
= 128) + 1;
1132 a
= (scast
= 65536) + 1;
1135 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
1137 /* test cast from unsigned to signed short to int */
1140 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b
, d
);
1143 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b
, d
);
1145 /* test implicit int casting for array accesses */
1149 printf("%d %d\n", tab
[0], tab
[1]);
1151 /* test implicit casting on some operators */
1152 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1153 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1154 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1156 /* from pointer to integer types */
1157 printf("%d %d %ld %ld %lld %lld\n",
1158 (int)p
, (unsigned int)p
,
1159 (long)p
, (unsigned long)p
,
1160 (long long)p
, (unsigned long long)p
);
1162 /* from integers to pointers */
1163 printf("%p %p %p %p\n",
1164 (void *)a
, (void *)b
, (void *)c
, (void *)d
);
1167 /* initializers tests */
1168 struct structinit1
{
1177 int sinit3
[3] = { 1, 2, {{3}}, };
1178 int sinit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1179 int sinit5
[3][2] = { 1, 2, 3, 4, 5, 6 };
1180 int sinit6
[] = { 1, 2, 3 };
1181 int sinit7
[] = { [2] = 3, [0] = 1, 2 };
1182 char sinit8
[] = "hello" "trala";
1184 struct structinit1 sinit9
= { 1, 2, 3 };
1185 struct structinit1 sinit10
= { .f2
= 2, 3, .f1
= 1 };
1186 struct structinit1 sinit11
= { .f2
= 2, 3, .f1
= 1,
1194 char *sinit12
= "hello world";
1200 char sinit14
[10] = { "abc" };
1201 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
1203 struct { int a
[3], b
; } sinit16
[] = { { 1 }, 2 };
1219 void init_test(void)
1223 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1224 int linit6
[] = { 1, 2, 3 };
1226 char linit8
[] = "hello" "trala";
1227 int linit12
[10] = { 1, 2 };
1228 int linit13
[10] = { 1, 2, [7] = 3, [3] = 4, };
1229 char linit14
[10] = "abc";
1230 int linit15
[10] = { linit1
, linit1
+ 1, [6] = linit1
+ 2, };
1231 struct linit16
{ int a1
, a2
, a3
, a4
; } linit16
= { 1, .a3
= 2 };
1232 int linit17
= sizeof(linit17
);
1234 printf("init_test:\n");
1236 printf("sinit1=%d\n", sinit1
);
1237 printf("sinit2=%d\n", sinit2
);
1238 printf("sinit3=%d %d %d %d\n",
1244 printf("sinit6=%d\n", sizeof(sinit6
));
1245 printf("sinit7=%d %d %d %d\n",
1251 printf("sinit8=%s\n", sinit8
);
1252 printf("sinit9=%d %d %d\n",
1257 printf("sinit10=%d %d %d\n",
1262 printf("sinit11=%d %d %d %d %d %d\n",
1273 printf("[%d][%d] = %d %d %d\n",
1274 i
, j
, sinit4
[i
][j
], sinit5
[i
][j
], linit4
[i
][j
]);
1275 printf("linit1=%d\n", linit1
);
1276 printf("linit2=%d\n", linit2
);
1277 printf("linit6=%d\n", sizeof(linit6
));
1278 printf("linit8=%d %s\n", sizeof(linit8
), linit8
);
1280 printf("sinit12=%s\n", sinit12
);
1281 printf("sinit13=%d %s %s %s\n",
1286 printf("sinit14=%s\n", sinit14
);
1288 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
1290 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
1292 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
1294 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
1296 printf("%d %d %d %d\n",
1301 /* test that initialisation is done after variable declare */
1302 printf("linit17=%d\n", linit17
);
1303 printf("sinit15=%d\n", sinit15
[0]);
1304 printf("sinit16=%d %d\n", sinit16
[0].a
[0], sinit16
[1].a
[0]);
1305 printf("sinit17=%s %d %s %d\n",
1306 sinit17
[0].s
, sinit17
[0].len
,
1307 sinit17
[1].s
, sinit17
[1].len
);
1309 printf("%x ", sinit18
[i
]);
1338 /* ISOC99 _Bool type */
1339 void c99_bool_test(void)
1345 printf("bool_test:\n");
1346 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
1348 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
1350 printf("b = %d\n", b
);
1352 printf("b = %d\n", b
);
1356 void bitfield_test(void)
1366 unsigned int f5
: 7;
1368 printf("bitfield_test:");
1369 printf("sizeof(st1) = %d\n", sizeof(st1
));
1378 printf("%d %d %d %d %d\n",
1379 st1
.f1
, st1
.f2
, st1
.f3
, st1
.f4
, st1
.f5
);
1383 printf("st1.f1 == -1\n");
1385 printf("st1.f1 != -1\n");
1387 printf("st1.f2 == -1\n");
1389 printf("st1.f2 != -1\n");
1391 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1392 long-long bitfields whose size is not bigger than int */
1397 unsigned long long f3
: 38;
1399 st2
.f1
= 0x123456789ULL
;
1401 st2
.f2
= (long long)a
<< 25;
1404 printf("%lld %lld %lld\n", st2
.f1
, st2
.f2
, st2
.f3
);
1408 #define FLOAT_FMT "%f\n"
1410 /* x86's float isn't compatible with GCC */
1411 #define FLOAT_FMT "%.5f\n"
1414 /* declare strto* functions as they are C99 */
1415 double strtod(const char *nptr
, char **endptr
);
1416 float strtof(const char *nptr
, char **endptr
);
1417 long double strtold(const char *nptr
, char **endptr
);
1419 #define FTEST(prefix, type, fmt)\
1420 void prefix ## cmp(type a, type b)\
1422 printf("%d %d %d %d %d %d\n",\
1429 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1437 printf(fmt "\n", ++a);\
1438 printf(fmt "\n", a++);\
1439 printf(fmt "\n", a);\
1441 printf("%d %d\n", !a, !b);\
1443 void prefix ## fcast(type a)\
1454 printf("ftof: %f %f %Lf\n", fa, da, la);\
1456 ua = (unsigned int)a;\
1457 printf("ftoi: %d %u\n", ia, ua);\
1461 printf("itof: " fmt "\n", b);\
1463 printf("utof: " fmt "\n", b);\
1466 float prefix ## retf(type a) { return a; }\
1467 double prefix ## retd(type a) { return a; }\
1468 long double prefix ## retld(type a) { return a; }\
1470 void prefix ## call(void)\
1472 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1473 printf("double: %f\n", prefix ## retd(42.123456789));\
1474 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1475 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
1478 void prefix ## test(void)\
1480 printf("testing '%s'\n", #type);\
1481 prefix ## cmp(1, 2.5);\
1482 prefix ## cmp(2, 1.5);\
1483 prefix ## cmp(1, 1);\
1484 prefix ## fcast(234.6);\
1485 prefix ## fcast(-2334.6);\
1489 FTEST(f
, float, "%f")
1490 FTEST(d
, double, "%f")
1491 FTEST(ld
, long double, "%Lf")
1493 double ftab1
[3] = { 1.2, 3.4, -5.6 };
1496 void float_test(void)
1503 printf("float_test:\n");
1504 printf("sizeof(float) = %d\n", sizeof(float));
1505 printf("sizeof(double) = %d\n", sizeof(double));
1506 printf("sizeof(long double) = %d\n", sizeof(long double));
1510 printf("%f %f %f\n", ftab1
[0], ftab1
[1], ftab1
[2]);
1511 printf("%f %f %f\n", 2.12, .5, 2.3e10
);
1512 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1514 printf("da=%f\n", da
);
1516 printf("fa=%f\n", fa
);
1519 printf("da = %f\n", da
);
1522 printf("db = %f\n", db
);
1530 return fib(n
-1) + fib(n
-2);
1542 printf("funcptr:\n");
1549 /* more complicated pointer computation */
1552 printf("sizeof1 = %d\n", sizeof(funcptr_test
));
1553 printf("sizeof2 = %d\n", sizeof funcptr_test
);
1554 printf("sizeof3 = %d\n", sizeof(&funcptr_test
));
1555 printf("sizeof4 = %d\n", sizeof &funcptr_test
);
1558 void lloptest(long long a
, long long b
)
1560 unsigned long long ua
, ub
;
1565 printf("arith: %Ld %Ld %Ld\n",
1571 printf("arith1: %Ld %Ld\n",
1577 printf("bin: %Ld %Ld %Ld\n",
1583 printf("test: %d %d %d %d %d %d\n",
1591 printf("utest: %d %d %d %d %d %d\n",
1602 printf("arith2: %Ld %Ld\n", a
, b
);
1603 printf("arith2: %Ld %Ld\n", a
++, b
++);
1604 printf("arith2: %Ld %Ld\n", --a
, --b
);
1605 printf("arith2: %Ld %Ld\n", a
, b
);
1607 printf("not: %d %d %d %d\n", !a
, !ua
, !b
, !ub
);
1610 void llshift(long long a
, int b
)
1612 printf("shift: %Ld %Ld %Ld\n",
1613 (unsigned long long)a
>> b
,
1616 printf("shiftc: %Ld %Ld %Ld\n",
1617 (unsigned long long)a
>> 3,
1620 printf("shiftc: %Ld %Ld %Ld\n",
1621 (unsigned long long)a
>> 35,
1631 long long la
, lb
, lc
;
1632 unsigned long long ula
, ulb
, ulc
;
1635 la
= (la
<< 20) | 0x12345;
1637 printf("la=%Ld ula=%Lu\n", la
, ula
);
1642 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
1647 printf("ftoll: %Ld %Ld %Ld\n", la
, lb
, lc
);
1652 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
1657 printf("ftoull: %Lu %Lu %Lu\n", ula
, ulb
, ulc
);
1660 long long llfunc1(int a
)
1670 long long int value(struct S
*v
)
1672 return ((long long int)v
->item
);
1675 void longlong_test(void)
1680 printf("longlong_test:\n");
1681 printf("sizeof(long long) = %d\n", sizeof(long long));
1686 printf("%Ld %Ld\n", a
, b
);
1687 printf("%Ld %Ld %Ld %Lx\n",
1691 0x1234567812345679);
1696 lloptest(0xff, 0x1234);
1697 b
= 0x72345678 << 10;
1701 b
= 0x72345678LL
<< 10;
1712 /* long long reg spill test */
1717 printf("%lld\n", value(&a
));
1719 lloptest(0x80000000, 0);
1721 /* another long long spill test */
1727 printf("%lld\n", *p
);
1732 printf("%d %d %d %d\n", a
> b
, a
< b
, a
>= b
, a
<= b
);
1734 printf("%Ld\n", 0x123456789LLU
);
1737 void manyarg_test(void)
1739 long double ld
= 1234567891234LL;
1740 printf("manyarg_test:\n");
1741 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
1742 1, 2, 3, 4, 5, 6, 7, 8,
1743 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
1744 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
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 1234567891234LL, 987654321986LL,
1750 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1752 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
1753 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1754 1234567891234LL, 987654321986LL,
1756 /* XXX: known bug of x86-64 */
1758 printf("%d %d %d %d %d %d %d %d %Lf\n",
1759 1, 2, 3, 4, 5, 6, 7, 8, ld
);
1760 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1761 "%Ld %Ld %f %f %Lf\n",
1762 1, 2, 3, 4, 5, 6, 7, 8,
1763 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1764 1234567891234LL, 987654321986LL,
1766 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1767 "%Lf %Ld %Ld %f %f %Lf\n",
1768 1, 2, 3, 4, 5, 6, 7, 8,
1769 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1770 ld
, 1234567891234LL, 987654321986LL,
1775 void vprintf1(const char *fmt
, ...)
1798 i
= va_arg(ap
, int);
1802 d
= va_arg(ap
, double);
1806 ll
= va_arg(ap
, long long);
1810 ld
= va_arg(ap
, long double);
1824 void stdarg_test(void)
1826 long double ld
= 1234567891234LL;
1827 vprintf1("%d %d %d\n", 1, 2, 3);
1828 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1829 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1830 vprintf1("%F %F %F\n", 1.2L, 2.3L, 3.4L);
1832 /* a bug of x86's TCC */
1833 vprintf1("%d %f %l %F %d %f %l %F\n",
1834 1, 1.2, 3L, 4.5L, 6, 7.8, 9L, 0.1L);
1836 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
1837 1, 2, 3, 4, 5, 6, 7, 8,
1838 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
1839 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
1840 1, 2, 3, 4, 5, 6, 7, 8,
1841 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
1842 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
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 1234567891234LL, 987654321986LL,
1848 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1850 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
1851 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1852 1234567891234LL, 987654321986LL,
1854 vprintf1("%d %d %d %d %d %d %d %d %F\n",
1855 1, 2, 3, 4, 5, 6, 7, 8, ld
);
1856 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1858 1, 2, 3, 4, 5, 6, 7, 8,
1859 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1860 1234567891234LL, 987654321986LL,
1862 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1863 "%F %l %l %f %f %F\n",
1864 1, 2, 3, 4, 5, 6, 7, 8,
1865 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1866 ld
, 1234567891234LL, 987654321986LL,
1870 void whitespace_test(void)
1876 ntf("whitspace:\n");\f\v
1880 #ifdef CORRECT_CR_HANDLING
1889 #ifdef ACCEPT_CR_IN_STRINGS
1890 printf("len1=%d\n", strlen("
1892 #ifdef CORRECT_CR_HANDLING
1895 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
1897 printf("len1=%d\n", strlen("
a
1899 #endif /* ACCEPT_CR_IN_STRINGS */
1902 int reltab
[3] = { 1, 2, 3 };
1904 int *rel1
= &reltab
[1];
1905 int *rel2
= &reltab
[2];
1907 void relocation_test(void)
1909 printf("*rel1=%d\n", *rel1
);
1910 printf("*rel2=%d\n", *rel2
);
1913 void old_style_f(a
,b
,c
)
1917 printf("a=%d b=%d b=%f\n", a
, b
, c
);
1920 void decl_func1(int cmpfn())
1922 printf("cmpfn=%lx\n", (long)cmpfn
);
1925 void decl_func2(cmpfn
)
1928 printf("cmpfn=%lx\n", (long)cmpfn
);
1931 void old_style_function(void)
1933 old_style_f((void *)1, 2, 3.0);
1938 void sizeof_test(void)
1943 printf("sizeof(int) = %d\n", sizeof(int));
1944 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1945 printf("sizeof(long) = %d\n", sizeof(long));
1946 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
1947 printf("sizeof(short) = %d\n", sizeof(short));
1948 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1949 printf("sizeof(char) = %d\n", sizeof(char));
1950 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1951 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1953 printf("sizeof(a++) = %d\n", sizeof a
++);
1954 printf("a=%d\n", a
);
1956 printf("sizeof(**ptr) = %d\n", sizeof (**ptr
));
1958 /* some alignof tests */
1959 printf("__alignof__(int) = %d\n", __alignof__(int));
1960 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1961 printf("__alignof__(short) = %d\n", __alignof__(short));
1962 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1963 printf("__alignof__(char) = %d\n", __alignof__(char));
1964 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1965 printf("__alignof__(func) = %d\n", __alignof__
sizeof_test());
1968 void typeof_test(void)
1977 printf("a=%f b=%f c=%f\n", a
, b
, c
);
1980 void statement_expr_test(void)
1993 printf("a=%d\n", a
);
1997 void local_label_test(void)
2003 __label__ l1
, l2
, l3
, l4
;
2017 printf("a=%d\n", a
);
2027 /* inline assembler test */
2030 /* from linux kernel */
2031 static char * strncat1(char * dest
,const char * src
,size_t count
)
2034 __asm__
__volatile__(
2043 "testb %%al,%%al\n\t"
2045 "2:\txorl %2,%2\n\t"
2047 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
2048 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
2053 static inline void * memcpy1(void * to
, const void * from
, size_t n
)
2056 __asm__
__volatile__(
2061 "1:\ttestb $1,%b4\n\t"
2065 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
2066 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
2071 static __inline__
void sigaddset1(unsigned int *set
, int _sig
)
2073 __asm__("btsl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
2076 static __inline__
void sigdelset1(unsigned int *set
, int _sig
)
2078 asm("btrl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
2081 static __inline__ __const__
unsigned int swab32(unsigned int x
)
2083 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2084 "rorl $16,%0\n\t" /* swap words */
2085 "xchgb %b0,%h0" /* swap higher bytes */
2091 static __inline__
unsigned long long mul64(unsigned int a
, unsigned int b
)
2093 unsigned long long res
;
2094 __asm__("mull %2" : "=A" (res
) : "a" (a
), "r" (b
));
2098 static __inline__
unsigned long long inc64(unsigned long long a
)
2100 unsigned long long res
;
2101 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res
) : "A" (a
));
2112 printf("inline asm:\n");
2113 /* test the no operand case */
2114 asm volatile ("xorl %eax, %eax");
2116 memcpy1(buf
, "hello", 6);
2117 strncat1(buf
, " worldXXXXX", 3);
2118 printf("%s\n", buf
);
2120 /* 'A' constraint test */
2121 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2122 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2125 sigdelset1(&set
, 2);
2126 sigaddset1(&set
, 16);
2127 /* NOTE: we test here if C labels are correctly restored after the
2131 __asm__("btsl %1,%0" : "=m"(set
) : "Ir"(20) : "cc");
2132 printf("set=0x%x\n", set
);
2134 printf("swab32(0x%08x) = 0x%0x\n", val
, swab32(val
));
2148 #define COMPAT_TYPE(type1, type2) \
2150 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2151 __builtin_types_compatible_p (type1, type2));\
2156 void builtin_test(void)
2159 COMPAT_TYPE(int, int);
2160 COMPAT_TYPE(int, unsigned int);
2161 COMPAT_TYPE(int, char);
2162 COMPAT_TYPE(int, const int);
2163 COMPAT_TYPE(int, volatile int);
2164 COMPAT_TYPE(int *, int *);
2165 COMPAT_TYPE(int *, void *);
2166 COMPAT_TYPE(int *, const int *);
2167 COMPAT_TYPE(char *, unsigned char *);
2168 /* space is needed because tcc preprocessor introduces a space between each token */
2169 COMPAT_TYPE(char * *, void *);
2171 printf("res = %d\n", __builtin_constant_p(1));
2172 printf("res = %d\n", __builtin_constant_p(1 + 2));
2173 printf("res = %d\n", __builtin_constant_p(&constant_p_var
));
2174 printf("res = %d\n", __builtin_constant_p(constant_p_var
));
2178 void const_func(const int a
)
2182 void const_warn_test(void)