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 printf("cast_test:\n");
1117 printf("%d %d %d %d\n",
1120 (unsigned char)(a
+ 1),
1121 (unsigned short)(a
+ 1));
1122 printf("%d %d %d %d\n",
1125 (unsigned char)0xfffff,
1126 (unsigned short)0xfffff);
1128 a
= (bcast
= 128) + 1;
1130 a
= (scast
= 65536) + 1;
1133 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
1135 /* test cast from unsigned to signed short to int */
1138 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b
, d
);
1141 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b
, d
);
1143 /* test implicit int casting for array accesses */
1147 printf("%d %d\n", tab
[0], tab
[1]);
1149 /* test implicit casting on some operators */
1150 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1151 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1152 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1155 /* initializers tests */
1156 struct structinit1
{
1165 int sinit3
[3] = { 1, 2, {{3}}, };
1166 int sinit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1167 int sinit5
[3][2] = { 1, 2, 3, 4, 5, 6 };
1168 int sinit6
[] = { 1, 2, 3 };
1169 int sinit7
[] = { [2] = 3, [0] = 1, 2 };
1170 char sinit8
[] = "hello" "trala";
1172 struct structinit1 sinit9
= { 1, 2, 3 };
1173 struct structinit1 sinit10
= { .f2
= 2, 3, .f1
= 1 };
1174 struct structinit1 sinit11
= { .f2
= 2, 3, .f1
= 1,
1182 char *sinit12
= "hello world";
1188 char sinit14
[10] = { "abc" };
1189 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
1191 struct { int a
[3], b
; } sinit16
[] = { { 1 }, 2 };
1207 void init_test(void)
1211 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1212 int linit6
[] = { 1, 2, 3 };
1214 char linit8
[] = "hello" "trala";
1215 int linit12
[10] = { 1, 2 };
1216 int linit13
[10] = { 1, 2, [7] = 3, [3] = 4, };
1217 char linit14
[10] = "abc";
1218 int linit15
[10] = { linit1
, linit1
+ 1, [6] = linit1
+ 2, };
1219 struct linit16
{ int a1
, a2
, a3
, a4
; } linit16
= { 1, .a3
= 2 };
1220 int linit17
= sizeof(linit17
);
1222 printf("init_test:\n");
1224 printf("sinit1=%d\n", sinit1
);
1225 printf("sinit2=%d\n", sinit2
);
1226 printf("sinit3=%d %d %d %d\n",
1232 printf("sinit6=%d\n", sizeof(sinit6
));
1233 printf("sinit7=%d %d %d %d\n",
1239 printf("sinit8=%s\n", sinit8
);
1240 printf("sinit9=%d %d %d\n",
1245 printf("sinit10=%d %d %d\n",
1250 printf("sinit11=%d %d %d %d %d %d\n",
1261 printf("[%d][%d] = %d %d %d\n",
1262 i
, j
, sinit4
[i
][j
], sinit5
[i
][j
], linit4
[i
][j
]);
1263 printf("linit1=%d\n", linit1
);
1264 printf("linit2=%d\n", linit2
);
1265 printf("linit6=%d\n", sizeof(linit6
));
1266 printf("linit8=%d %s\n", sizeof(linit8
), linit8
);
1268 printf("sinit12=%s\n", sinit12
);
1269 printf("sinit13=%d %s %s %s\n",
1274 printf("sinit14=%s\n", sinit14
);
1276 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
1278 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
1280 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
1282 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
1284 printf("%d %d %d %d\n",
1289 /* test that initialisation is done after variable declare */
1290 printf("linit17=%d\n", linit17
);
1291 printf("sinit15=%d\n", sinit15
[0]);
1292 printf("sinit16=%d %d\n", sinit16
[0].a
[0], sinit16
[1].a
[0]);
1293 printf("sinit17=%s %d %s %d\n",
1294 sinit17
[0].s
, sinit17
[0].len
,
1295 sinit17
[1].s
, sinit17
[1].len
);
1297 printf("%x ", sinit18
[i
]);
1326 /* ISOC99 _Bool type */
1327 void c99_bool_test(void)
1333 printf("bool_test:\n");
1334 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
1336 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
1338 printf("b = %d\n", b
);
1340 printf("b = %d\n", b
);
1344 void bitfield_test(void)
1354 unsigned int f5
: 7;
1356 printf("bitfield_test:");
1357 printf("sizeof(st1) = %d\n", sizeof(st1
));
1366 printf("%d %d %d %d %d\n",
1367 st1
.f1
, st1
.f2
, st1
.f3
, st1
.f4
, st1
.f5
);
1371 printf("st1.f1 == -1\n");
1373 printf("st1.f1 != -1\n");
1375 printf("st1.f2 == -1\n");
1377 printf("st1.f2 != -1\n");
1379 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1380 long-long bitfields whose size is not bigger than int */
1385 unsigned long long f3
: 38;
1387 st2
.f1
= 0x123456789ULL
;
1389 st2
.f2
= (long long)a
<< 25;
1392 printf("%lld %lld %lld\n", st2
.f1
, st2
.f2
, st2
.f3
);
1396 #define FLOAT_FMT "%f\n"
1398 /* x86's float isn't compatible with GCC */
1399 #define FLOAT_FMT "%.5f\n"
1402 #define FTEST(prefix, type, fmt)\
1403 void prefix ## cmp(type a, type b)\
1405 printf("%d %d %d %d %d %d\n",\
1412 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1420 printf(fmt "\n", ++a);\
1421 printf(fmt "\n", a++);\
1422 printf(fmt "\n", a);\
1424 void prefix ## fcast(type a)\
1435 printf("ftof: %f %f %Lf\n", fa, da, la);\
1437 ua = (unsigned int)a;\
1438 printf("ftoi: %d %u\n", ia, ua);\
1442 printf("itof: " fmt "\n", b);\
1444 printf("utof: " fmt "\n", b);\
1447 float prefix ## retf(type a) { return a; }\
1448 double prefix ## retd(type a) { return a; }\
1449 long double prefix ## retld(type a) { return a; }\
1451 void prefix ## call(void)\
1453 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1454 printf("double: %f\n", prefix ## retd(42.123456789));\
1455 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1458 void prefix ## test(void)\
1460 printf("testing '%s'\n", #type);\
1461 prefix ## cmp(1, 2.5);\
1462 prefix ## cmp(2, 1.5);\
1463 prefix ## cmp(1, 1);\
1464 prefix ## fcast(234.6);\
1465 prefix ## fcast(-2334.6);\
1469 FTEST(f
, float, "%f")
1470 FTEST(d
, double, "%f")
1471 FTEST(ld
, long double, "%Lf")
1473 double ftab1
[3] = { 1.2, 3.4, -5.6 };
1476 void float_test(void)
1483 printf("float_test:\n");
1484 printf("sizeof(float) = %d\n", sizeof(float));
1485 printf("sizeof(double) = %d\n", sizeof(double));
1486 printf("sizeof(long double) = %d\n", sizeof(long double));
1490 printf("%f %f %f\n", ftab1
[0], ftab1
[1], ftab1
[2]);
1491 printf("%f %f %f\n", 2.12, .5, 2.3e10
);
1492 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1494 printf("da=%f\n", da
);
1496 printf("fa=%f\n", fa
);
1499 printf("da = %f\n", da
);
1502 printf("db = %f\n", db
);
1510 return fib(n
-1) + fib(n
-2);
1522 printf("funcptr:\n");
1529 /* more complicated pointer computation */
1532 printf("sizeof1 = %d\n", sizeof(funcptr_test
));
1533 printf("sizeof2 = %d\n", sizeof funcptr_test
);
1534 printf("sizeof3 = %d\n", sizeof(&funcptr_test
));
1535 printf("sizeof4 = %d\n", sizeof &funcptr_test
);
1538 void lloptest(long long a
, long long b
)
1540 unsigned long long ua
, ub
;
1545 printf("arith: %Ld %Ld %Ld\n",
1551 printf("arith1: %Ld %Ld\n",
1557 printf("bin: %Ld %Ld %Ld\n",
1563 printf("test: %d %d %d %d %d %d\n",
1571 printf("utest: %d %d %d %d %d %d\n",
1582 printf("arith2: %Ld %Ld\n", a
, b
);
1583 printf("arith2: %Ld %Ld\n", a
++, b
++);
1584 printf("arith2: %Ld %Ld\n", --a
, --b
);
1585 printf("arith2: %Ld %Ld\n", a
, b
);
1588 void llshift(long long a
, int b
)
1590 printf("shift: %Ld %Ld %Ld\n",
1591 (unsigned long long)a
>> b
,
1594 printf("shiftc: %Ld %Ld %Ld\n",
1595 (unsigned long long)a
>> 3,
1598 printf("shiftc: %Ld %Ld %Ld\n",
1599 (unsigned long long)a
>> 35,
1609 long long la
, lb
, lc
;
1610 unsigned long long ula
, ulb
, ulc
;
1613 la
= (la
<< 20) | 0x12345;
1615 printf("la=%Ld ula=%Lu\n", la
, ula
);
1620 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
1625 printf("ftoll: %Ld %Ld %Ld\n", la
, lb
, lc
);
1630 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
1635 printf("ftoull: %Lu %Lu %Lu\n", ula
, ulb
, ulc
);
1638 long long llfunc1(int a
)
1648 long long int value(struct S
*v
)
1650 return ((long long int)v
->item
);
1653 void longlong_test(void)
1658 printf("longlong_test:\n");
1659 printf("sizeof(long long) = %d\n", sizeof(long long));
1664 printf("%Ld %Ld\n", a
, b
);
1665 printf("%Ld %Ld %Ld %Lx\n",
1669 0x1234567812345679);
1674 lloptest(0xff, 0x1234);
1675 b
= 0x72345678 << 10;
1679 b
= 0x72345678LL
<< 10;
1690 /* long long reg spill test */
1695 printf("%lld\n", value(&a
));
1697 lloptest(0x80000000, 0);
1699 /* another long long spill test */
1705 printf("%lld\n", *p
);
1710 printf("%d %d %d %d\n", a
> b
, a
< b
, a
>= b
, a
<= b
);
1712 printf("%Ld\n", 0x123456789LLU
);
1715 void manyarg_test(void)
1717 long double ld
= 1234567891234LL;
1718 printf("manyarg_test:\n");
1719 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
1720 1, 2, 3, 4, 5, 6, 7, 8,
1721 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
1722 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1724 1, 2, 3, 4, 5, 6, 7, 8,
1725 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1726 1234567891234LL, 987654321986LL,
1728 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1730 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
1731 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1732 1234567891234LL, 987654321986LL,
1734 /* XXX: known bug of x86-64 */
1736 printf("%d %d %d %d %d %d %d %d %Lf\n",
1737 1, 2, 3, 4, 5, 6, 7, 8, ld
);
1738 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1739 "%Ld %Ld %f %f %Lf\n",
1740 1, 2, 3, 4, 5, 6, 7, 8,
1741 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1742 1234567891234LL, 987654321986LL,
1744 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1745 "%Lf %Ld %Ld %f %f %Lf\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 ld
, 1234567891234LL, 987654321986LL,
1753 void vprintf1(const char *fmt
, ...)
1776 i
= va_arg(ap
, int);
1780 d
= va_arg(ap
, double);
1784 ll
= va_arg(ap
, long long);
1788 ld
= va_arg(ap
, long double);
1802 void stdarg_test(void)
1804 long double ld
= 1234567891234LL;
1805 vprintf1("%d %d %d\n", 1, 2, 3);
1806 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1807 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1808 vprintf1("%F %F %F\n", 1.2L, 2.3L, 3.4L);
1810 /* a bug of x86's TCC */
1811 vprintf1("%d %f %l %F %d %f %l %F\n",
1812 1, 1.2, 3L, 4.5L, 6, 7.8, 9L, 0.1L);
1814 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
1815 1, 2, 3, 4, 5, 6, 7, 8,
1816 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
1817 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
1818 1, 2, 3, 4, 5, 6, 7, 8,
1819 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
1820 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1822 1, 2, 3, 4, 5, 6, 7, 8,
1823 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1824 1234567891234LL, 987654321986LL,
1826 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1828 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
1829 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1830 1234567891234LL, 987654321986LL,
1832 vprintf1("%d %d %d %d %d %d %d %d %F\n",
1833 1, 2, 3, 4, 5, 6, 7, 8, ld
);
1834 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1836 1, 2, 3, 4, 5, 6, 7, 8,
1837 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1838 1234567891234LL, 987654321986LL,
1840 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1841 "%F %l %l %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 ld
, 1234567891234LL, 987654321986LL,
1848 void whitespace_test(void)
1854 ntf("whitspace:\n");\f\v
1858 #ifdef CORRECT_CR_HANDLING
1867 #ifdef ACCEPT_CR_IN_STRINGS
1868 printf("len1=%d\n", strlen("
1870 #ifdef CORRECT_CR_HANDLING
1873 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
1875 printf("len1=%d\n", strlen("
a
1877 #endif /* ACCEPT_CR_IN_STRINGS */
1880 int reltab
[3] = { 1, 2, 3 };
1882 int *rel1
= &reltab
[1];
1883 int *rel2
= &reltab
[2];
1885 void relocation_test(void)
1887 printf("*rel1=%d\n", *rel1
);
1888 printf("*rel2=%d\n", *rel2
);
1891 void old_style_f(a
,b
,c
)
1895 printf("a=%d b=%d b=%f\n", a
, b
, c
);
1898 void decl_func1(int cmpfn())
1900 printf("cmpfn=%lx\n", (long)cmpfn
);
1903 void decl_func2(cmpfn
)
1906 printf("cmpfn=%lx\n", (long)cmpfn
);
1909 void old_style_function(void)
1911 old_style_f((void *)1, 2, 3.0);
1916 void sizeof_test(void)
1921 printf("sizeof(int) = %d\n", sizeof(int));
1922 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1923 printf("sizeof(long) = %d\n", sizeof(long));
1924 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
1925 printf("sizeof(short) = %d\n", sizeof(short));
1926 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1927 printf("sizeof(char) = %d\n", sizeof(char));
1928 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1929 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1931 printf("sizeof(a++) = %d\n", sizeof a
++);
1932 printf("a=%d\n", a
);
1934 printf("sizeof(**ptr) = %d\n", sizeof (**ptr
));
1936 /* some alignof tests */
1937 printf("__alignof__(int) = %d\n", __alignof__(int));
1938 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1939 printf("__alignof__(short) = %d\n", __alignof__(short));
1940 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1941 printf("__alignof__(char) = %d\n", __alignof__(char));
1942 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1943 printf("__alignof__(func) = %d\n", __alignof__
sizeof_test());
1946 void typeof_test(void)
1955 printf("a=%f b=%f c=%f\n", a
, b
, c
);
1958 void statement_expr_test(void)
1971 printf("a=%d\n", a
);
1975 void local_label_test(void)
1981 __label__ l1
, l2
, l3
, l4
;
1995 printf("a=%d\n", a
);
2005 /* inline assembler test */
2008 /* from linux kernel */
2009 static char * strncat1(char * dest
,const char * src
,size_t count
)
2012 __asm__
__volatile__(
2021 "testb %%al,%%al\n\t"
2023 "2:\txorl %2,%2\n\t"
2025 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
2026 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
2031 static inline void * memcpy1(void * to
, const void * from
, size_t n
)
2034 __asm__
__volatile__(
2039 "1:\ttestb $1,%b4\n\t"
2043 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
2044 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
2049 static __inline__
void sigaddset1(unsigned int *set
, int _sig
)
2051 __asm__("btsl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
2054 static __inline__
void sigdelset1(unsigned int *set
, int _sig
)
2056 asm("btrl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
2059 static __inline__ __const__
unsigned int swab32(unsigned int x
)
2061 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2062 "rorl $16,%0\n\t" /* swap words */
2063 "xchgb %b0,%h0" /* swap higher bytes */
2069 static __inline__
unsigned long long mul64(unsigned int a
, unsigned int b
)
2071 unsigned long long res
;
2072 __asm__("mull %2" : "=A" (res
) : "a" (a
), "r" (b
));
2076 static __inline__
unsigned long long inc64(unsigned long long a
)
2078 unsigned long long res
;
2079 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res
) : "A" (a
));
2090 printf("inline asm:\n");
2091 /* test the no operand case */
2092 asm volatile ("xorl %eax, %eax");
2094 memcpy1(buf
, "hello", 6);
2095 strncat1(buf
, " worldXXXXX", 3);
2096 printf("%s\n", buf
);
2098 /* 'A' constraint test */
2099 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2100 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2103 sigdelset1(&set
, 2);
2104 sigaddset1(&set
, 16);
2105 /* NOTE: we test here if C labels are correctly restored after the
2109 __asm__("btsl %1,%0" : "=m"(set
) : "Ir"(20) : "cc");
2110 printf("set=0x%x\n", set
);
2112 printf("swab32(0x%08x) = 0x%0x\n", val
, swab32(val
));
2126 #define COMPAT_TYPE(type1, type2) \
2128 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2129 __builtin_types_compatible_p (type1, type2));\
2134 void builtin_test(void)
2137 COMPAT_TYPE(int, int);
2138 COMPAT_TYPE(int, unsigned int);
2139 COMPAT_TYPE(int, char);
2140 COMPAT_TYPE(int, const int);
2141 COMPAT_TYPE(int, volatile int);
2142 COMPAT_TYPE(int *, int *);
2143 COMPAT_TYPE(int *, void *);
2144 COMPAT_TYPE(int *, const int *);
2145 COMPAT_TYPE(char *, unsigned char *);
2146 /* space is needed because tcc preprocessor introduces a space between each token */
2147 COMPAT_TYPE(char * *, void *);
2149 printf("res = %d\n", __builtin_constant_p(1));
2150 printf("res = %d\n", __builtin_constant_p(1 + 2));
2151 printf("res = %d\n", __builtin_constant_p(&constant_p_var
));
2152 printf("res = %d\n", __builtin_constant_p(constant_p_var
));
2156 void const_func(const int a
)
2160 void const_warn_test(void)