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 /* __VA_ARGS__ and __func__ support */
22 /* test various include syntaxes */
24 #define TCCLIB_INC <tcclib.h>
25 #define TCCLIB_INC1 <tcclib
26 #define TCCLIB_INC2 h>
27 #define TCCLIB_INC3 "tcclib"
31 #include TCCLIB_INC1.TCCLIB_INC2
33 #include TCCLIB_INC1.h>
35 /* gcc 3.2 does not accept that (bug ?) */
36 //#include TCCLIB_INC3 ".h"
58 void constant_expr_test();
60 void char_short_test();
62 void compound_literal_test(void);
64 void struct_assign_test(void);
66 void bitfield_test(void);
67 void c99_bool_test(void);
68 void float_test(void);
69 void longlong_test(void);
70 void stdarg_test(void);
71 void whitespace_test(void);
72 void relocation_test(void);
73 void old_style_function(void);
74 void sizeof_test(void);
75 void typeof_test(void);
76 void local_label_test(void);
77 void statement_expr_test(void);
79 void builtin_test(void);
83 void forward_ref(void);
89 #define M1(a, b) (a) + (b)
93 #define glue(a, b) a ## b
94 #define xglue(a, b) glue(a, b)
95 #define HIGHLOW "hello"
96 #define LOW LOW ", world"
98 #define min(a, b) ((a) < (b) ? (a) : (b))
101 #define dprintf(level,...) printf(__VA_ARGS__)
104 /* gcc vararg macros */
105 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
107 #define MACRO_NOARGS()
123 #define __INT64_C(c) c ## LL
124 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
132 void macro_test(void)
134 printf("macro:\n");\f\v
136 printf("aaa=%d\n", AAA
);
138 printf("min=%d\n", min(1, min(2, -1)));
140 printf("s1=%s\n", glue(HIGH
, LOW
));
141 printf("s2=%s\n", xglue(HIGH
, LOW
));
142 printf("s3=%s\n", str("c"));
143 printf("s4=%s\n", str(a1
));
144 printf("B3=%d\n", B3
);
147 printf("A defined\n");
150 printf("B defined\n");
153 printf("A defined\n");
155 printf("A not defined\n");
158 printf("B defined\n");
160 printf("B not defined\n");
164 printf("A defined\n");
166 printf("B1 defined\n");
168 printf("B1 not defined\n");
171 printf("A not defined\n");
173 printf("B2 defined\n");
175 printf("B2 not defined\n");
180 printf("test true1\n");
183 printf("test true2\n");
186 printf("test true3\n");
189 printf("test trueA\n");
192 printf("test trueB\n");
208 printf("__LINE__ defined\n");
211 printf("__LINE__=%d __FILE__=%s\n",
214 printf("__LINE__=%d __FILE__=%s\n",
217 printf("__LINE__=%d __FILE__=%s\n",
219 #line 220 "tcctest.c"
221 /* not strictly preprocessor, but we test it there */
223 printf("__func__ = %s\n", __func__
);
224 dprintf(1, "vaarg=%d\n", 1);
226 dprintf1(1, "vaarg1\n");
227 dprintf1(1, "vaarg1=%d\n", 2);
228 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
231 printf("func='%s'\n", __FUNCTION__
);
233 /* complicated macros in glibc */
234 printf("INT64_MIN=%Ld\n", INT64_MIN
);
244 /* macro function with argument outside the macro string */
245 #define MF_s MF_hello
246 #define MF_hello(msg) printf("%s\n",msg)
248 #define MF_t printf("tralala\n"); MF_hello
253 /* test macro substituion inside args (should not eat stream) */
254 printf("qq=%d\n", qq(qq
)(2));
256 /* comment with stray handling *\
258 /* this is a valid *\/ comment */
259 /* this is a valid comment *\*/
278 void ps(const char *s
)
290 const char foo1_string
[] = "\
299 printf("\141\1423\143\n");/* dezdez test */
300 printf("\x41\x42\x43\x3a\n");
301 printf("c=%c\n", 'r');
302 printf("wc=%C 0x%lx %C\n", L
'a', L
'\x1234', L
'c');
303 printf("foo1_string='%s'\n", foo1_string
);
305 printf("wstring=%S\n", L
"abc");
306 printf("wstring=%S\n", L
"abc" L
"def" "ghi");
307 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
308 printf("L'\\377'=%d L'\\xff'=%d\n", L
'\377', L
'\xff');
312 while ((b
= b
+ 1) < 96) {
316 printf("fib=%d\n", fib(33));
318 while (b
!= 0x80000000) {
331 for(i
= 0; i
< 10;i
++)
340 /* break/continue tests */
352 /* break/continue tests */
364 for(i
= 0;i
< 10;i
++) {
376 static void *label_table
[3] = { &&label1
, &&label2
, &&label3
};
389 /* we also test computed gotos (GCC extension) */
391 goto *label_table
[i
];
419 printf("enum:\n%d %d %d %d %d %d\n",
420 E0
, E1
, E2
, E3
, E4
, E5
);
422 printf("b1=%d\n", b1
);
433 printf("typedef:\n");
434 printf("a=%d\n", *a
);
439 printf("forward:\n");
445 void forward_ref(void)
447 printf("forward ok\n");
450 typedef struct struct1
{
470 struct struct1 st1
, st2
;
472 int main(int argc
, char **argv
)
490 constant_expr_test();
494 compound_literal_test();
496 struct_assign_test();
505 old_style_function();
508 statement_expr_test();
522 printf("g1=%d\n", g
);
530 printf("g2=%d\n", g
);
534 printf("g3=%d\n", g
);
538 printf("g4=%d\n", g
);
541 printf("g5=%d\n", g
);
544 void array_test(int a
[4])
549 printf("sizeof(a) = %d\n", sizeof(a
));
550 printf("sizeof(\"a\") = %d\n", sizeof("a"));
552 printf("sizeof(__func__) = %d\n", sizeof(__func__
));
554 printf("sizeof tab %d\n", sizeof(tab
));
555 printf("sizeof tab2 %d\n", sizeof tab2
);
559 printf("%d %d %d\n", tab
[0], tab
[1], tab
[2]);
562 tab2
[i
][j
] = 10 * i
+ j
;
564 printf(" %3d", ((int *)tab2
)[i
]);
573 printf("%d\n", a
+= 1);
574 printf("%d\n", a
-= 2);
575 printf("%d\n", a
*= 31232132);
576 printf("%d\n", a
/= 4);
577 printf("%d\n", a
%= 20);
578 printf("%d\n", a
&= 6);
579 printf("%d\n", a
^= 7);
580 printf("%d\n", a
|= 8);
581 printf("%d\n", a
>>= 3);
582 printf("%d\n", a
<<= 4);
586 printf("%d\n", a
+ 1);
587 printf("%d\n", a
- 2);
588 printf("%d\n", a
* 312);
589 printf("%d\n", a
/ 4);
590 printf("%d\n", b
/ 4);
591 printf("%d\n", (unsigned)b
/ 4);
592 printf("%d\n", a
% 20);
593 printf("%d\n", b
% 20);
594 printf("%d\n", (unsigned)b
% 20);
595 printf("%d\n", a
& 6);
596 printf("%d\n", a
^ 7);
597 printf("%d\n", a
| 8);
598 printf("%d\n", a
>> 3);
599 printf("%d\n", b
>> 3);
600 printf("%d\n", (unsigned)b
>> 3);
601 printf("%d\n", a
<< 4);
606 printf("%d\n", 12 + 1);
607 printf("%d\n", 12 - 2);
608 printf("%d\n", 12 * 312);
609 printf("%d\n", 12 / 4);
610 printf("%d\n", 12 % 20);
611 printf("%d\n", 12 & 6);
612 printf("%d\n", 12 ^ 7);
613 printf("%d\n", 12 | 8);
614 printf("%d\n", 12 >> 2);
615 printf("%d\n", 12 << 4);
619 printf("%d %d %d %d\n",
628 return (c
>= 'a' & c
<= 'z') | (c
>= 'A' & c
<= 'Z') | c
== '_';
631 /**********************/
633 int vstack
[10], *vstack_ptr
;
635 void vpush(int vt
, int vc
)
641 void vpop(int *ft
, int *fc
)
654 vstack_ptr
[-2] &= ~0xffffff80;
656 printf("res= %d %d\n", a
, b
);
659 void constant_expr_test()
662 printf("constant_expr:\n");
664 printf("%d\n", a
* 16);
665 printf("%d\n", a
* 1);
666 printf("%d\n", a
+ 0);
675 printf("expr_ptr:\n");
678 printf("diff=%d\n", q
- p
);
680 printf("inc=%d\n", p
- tab4
);
682 printf("dec=%d\n", p
- tab4
);
684 printf("inc=%d\n", p
- tab4
);
686 printf("dec=%d\n", p
- tab4
);
687 printf("add=%d\n", p
+ 3 - tab4
);
688 printf("add=%d\n", 3 + p
- tab4
);
694 printf("constant_expr:\n");
697 printf("%d\n", a
== a
);
698 printf("%d\n", a
!= a
);
700 printf("%d\n", a
< b
);
701 printf("%d\n", a
<= b
);
702 printf("%d\n", a
<= a
);
703 printf("%d\n", b
>= a
);
704 printf("%d\n", a
>= a
);
705 printf("%d\n", b
> a
);
707 printf("%d\n", (unsigned)a
< b
);
708 printf("%d\n", (unsigned)a
<= b
);
709 printf("%d\n", (unsigned)a
<= a
);
710 printf("%d\n", (unsigned)b
>= a
);
711 printf("%d\n", (unsigned)a
>= a
);
712 printf("%d\n", (unsigned)b
> a
);
741 printf("sizes: %d %d %d %d\n",
742 sizeof(struct struct1
),
743 sizeof(struct struct2
),
744 sizeof(union union1
),
745 sizeof(union union2
));
749 printf("st1: %d %d %d\n",
750 st1
.f1
, st1
.f2
, st1
.f3
);
753 printf("union1: %d\n", st1
.u
.v1
);
756 printf("union2: %d\n", u
.w1
);
761 printf("st2: %d %d %d\n",
762 s
->f1
, s
->f2
, s
->f3
);
763 printf("str_addr=%x\n", (int)st1
.str
- (int)&st1
.f1
);
765 /* align / size tests */
766 printf("aligntest1 sizeof=%d alignof=%d\n",
767 sizeof(struct aligntest1
), __alignof__(struct aligntest1
));
768 printf("aligntest2 sizeof=%d alignof=%d\n",
769 sizeof(struct aligntest2
), __alignof__(struct aligntest2
));
770 printf("aligntest3 sizeof=%d alignof=%d\n",
771 sizeof(struct aligntest3
), __alignof__(struct aligntest3
));
772 printf("aligntest4 sizeof=%d alignof=%d\n",
773 sizeof(struct aligntest4
), __alignof__(struct aligntest4
));
775 /* empty structures (GCC extension) */
776 printf("sizeof(struct empty) = %d\n", sizeof(struct empty
));
777 printf("alignof(struct empty) = %d\n", __alignof__(struct empty
));
780 /* XXX: depend on endianness */
781 void char_short_test()
785 printf("char_short:\n");
790 *(char *)&var1
, *(char *)&var2
);
792 *(unsigned char *)&var1
, *(unsigned char *)&var2
);
793 printf("s16=%d %d\n",
794 *(short *)&var1
, *(short *)&var2
);
795 printf("u16=%d %d\n",
796 *(unsigned short *)&var1
, *(unsigned short *)&var2
);
797 printf("s32=%d %d\n",
798 *(int *)&var1
, *(int *)&var2
);
799 printf("u32=%d %d\n",
800 *(unsigned int *)&var1
, *(unsigned int *)&var2
);
801 *(char *)&var1
= 0x08;
802 printf("var1=%x\n", var1
);
803 *(short *)&var1
= 0x0809;
804 printf("var1=%x\n", var1
);
805 *(int *)&var1
= 0x08090a0b;
806 printf("var1=%x\n", var1
);
819 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
820 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
822 static int toupper1(int a
)
829 int *s
, a
, b
, t
, f
, i
;
833 printf("!s=%d\n", !s
);
839 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
840 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
841 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
856 printf("b=%d\n", a
+ (0 ? 1 : a
/ 2));
858 /* test register spilling */
861 a
= (a
+ b
) * ((a
< b
) ?
862 ((b
- a
) * (a
- b
)): a
+ b
);
865 /* test complex || or && expressions */
869 printf("exp=%d\n", f
== (32 <= a
&& a
<= 3));
870 printf("r=%d\n", (t
|| f
) + (t
&& f
));
875 int aspect_native
= 65536;
876 double bfu_aspect
= 1.0;
878 for(aspect_on
= 0; aspect_on
< 2; aspect_on
++) {
879 aspect
=aspect_on
?(aspect_native
*bfu_aspect
+0.5):65535UL;
880 printf("aspect=%d\n", aspect
);
884 /* test ? : GCC extension */
886 static int v1
= 34 ? : -1; /* constant case */
887 static int v2
= 0 ? : -1; /* constant case */
890 printf("%d %d\n", v1
, v2
);
891 printf("%d %d\n", a
- 30 ? : a
* 2, a
+ 1 ? : a
* 2);
894 /* again complex expression */
896 if (toupper1 (i
) != TOUPPER (i
))
897 printf("error %d\n", i
);
901 /* GCC accepts that */
902 static int tab_reinit
[];
903 static int tab_reinit
[10];
905 //int cinit1; /* a global variable can be defined several times without error ! */
909 int *cinit2
= (int []){3, 2, 1};
911 void compound_literal_test(void)
916 printf("compound_test:\n");
918 p
= (int []){1, 2, 3};
924 printf("%d", cinit2
[i
]);
928 printf("q1=%s\n", q
);
930 q
= (char *){ "tralala2" };
931 printf("q2=%s\n", q
);
934 printf("q3=%s\n", q3
);
936 q
= (char []){ "tralala3" };
937 printf("q4=%s\n", q
);
940 p
= (int []){1, 2, cinit1
+ 3};
946 p
= (int []){1, 2, 4 + i
};
969 printf("kr_test:\n");
970 printf("func1=%d\n", kr_func1(3, 4));
971 printf("func2=%d\n", kr_func2(3, 4));
978 tab
= (char*)malloc(20);
994 /* structure assignment tests */
1000 struct structa1 ssta1
;
1002 void struct_assign_test1(struct structa1 s1
, int t
)
1004 printf("%d %d %d\n", s1
.f1
, s1
.f2
, t
);
1007 struct structa1
struct_assign_test2(struct structa1 s1
, int t
)
1014 void struct_assign_test(void)
1016 struct structa1 lsta1
, lsta2
;
1019 printf("struct_assign_test:\n");
1023 printf("%d %d\n", lsta1
.f1
, lsta1
.f2
);
1025 printf("%d %d\n", lsta2
.f1
, lsta2
.f2
);
1030 struct_assign_test1(lsta2
, 3);
1032 printf("before call: %d %d\n", lsta2
.f1
, lsta2
.f2
);
1033 lsta2
= struct_assign_test2(lsta2
, 4);
1034 printf("after call: %d %d\n", lsta2
.f1
, lsta2
.f2
);
1037 /* casts to short/char */
1039 void cast1(char a
, short b
, unsigned char c
, unsigned short d
)
1041 printf("%d %d %d %d\n", a
, b
, c
, d
);
1053 printf("cast_test:\n");
1057 printf("%d %d %d %d\n",
1060 (unsigned char)(a
+ 1),
1061 (unsigned short)(a
+ 1));
1062 printf("%d %d %d %d\n",
1065 (unsigned char)0xfffff,
1066 (unsigned short)0xfffff);
1068 a
= (bcast
= 128) + 1;
1070 a
= (scast
= 65536) + 1;
1073 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
1075 /* test implicit int casting for array accesses */
1079 printf("%d %d\n", tab
[0], tab
[1]);
1081 /* test implicit casting on some operators */
1082 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1083 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1084 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1087 /* initializers tests */
1088 struct structinit1
{
1097 int sinit3
[3] = { 1, 2, {{3}}, };
1098 int sinit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1099 int sinit5
[3][2] = { 1, 2, 3, 4, 5, 6 };
1100 int sinit6
[] = { 1, 2, 3 };
1101 int sinit7
[] = { [2] = 3, [0] = 1, 2 };
1102 char sinit8
[] = "hello" "trala";
1104 struct structinit1 sinit9
= { 1, 2, 3 };
1105 struct structinit1 sinit10
= { .f2
= 2, 3, .f1
= 1 };
1106 struct structinit1 sinit11
= { .f2
= 2, 3, .f1
= 1,
1114 char *sinit12
= "hello world";
1120 char sinit14
[10] = { "abc" };
1121 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
1123 struct { int a
[3], b
; } sinit16
[] = { { 1 }, 2 };
1139 void init_test(void)
1143 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1144 int linit6
[] = { 1, 2, 3 };
1146 char linit8
[] = "hello" "trala";
1147 int linit12
[10] = { 1, 2 };
1148 int linit13
[10] = { 1, 2, [7] = 3, [3] = 4, };
1149 char linit14
[10] = "abc";
1150 int linit15
[10] = { linit1
, linit1
+ 1, [6] = linit1
+ 2, };
1151 struct linit16
{ int a1
, a2
, a3
, a4
; } linit16
= { 1, .a3
= 2 };
1152 int linit17
= sizeof(linit17
);
1154 printf("init_test:\n");
1156 printf("sinit1=%d\n", sinit1
);
1157 printf("sinit2=%d\n", sinit2
);
1158 printf("sinit3=%d %d %d %d\n",
1164 printf("sinit6=%d\n", sizeof(sinit6
));
1165 printf("sinit7=%d %d %d %d\n",
1171 printf("sinit8=%s\n", sinit8
);
1172 printf("sinit9=%d %d %d\n",
1177 printf("sinit10=%d %d %d\n",
1182 printf("sinit11=%d %d %d %d %d %d\n",
1193 printf("[%d][%d] = %d %d %d\n",
1194 i
, j
, sinit4
[i
][j
], sinit5
[i
][j
], linit4
[i
][j
]);
1195 printf("linit1=%d\n", linit1
);
1196 printf("linit2=%d\n", linit2
);
1197 printf("linit6=%d\n", sizeof(linit6
));
1198 printf("linit8=%d %s\n", sizeof(linit8
), linit8
);
1200 printf("sinit12=%s\n", sinit12
);
1201 printf("sinit13=%d %s %s %s\n",
1206 printf("sinit14=%s\n", sinit14
);
1208 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
1210 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
1212 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
1214 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
1216 printf("%d %d %d %d\n",
1221 /* test that initialisation is done after variable declare */
1222 printf("linit17=%d\n", linit17
);
1223 printf("sinit15=%d\n", sinit15
[0]);
1224 printf("sinit16=%d %d\n", sinit16
[0].a
[0], sinit16
[1].a
[0]);
1225 printf("sinit17=%s %d %s %d\n",
1226 sinit17
[0].s
, sinit17
[0].len
,
1227 sinit17
[1].s
, sinit17
[1].len
);
1229 printf("%x ", sinit18
[i
]);
1258 /* ISOC99 _Bool type */
1259 void c99_bool_test(void)
1265 printf("bool_test:\n");
1266 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
1268 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
1270 printf("b = %d\n", b
);
1272 printf("b = %d\n", b
);
1276 void bitfield_test(void)
1286 unsigned int f5
: 7;
1288 printf("bitfield_test:");
1289 printf("sizeof(st1) = %d\n", sizeof(st1
));
1298 printf("%d %d %d %d %d\n",
1299 st1
.f1
, st1
.f2
, st1
.f3
, st1
.f4
, st1
.f5
);
1302 #define FTEST(prefix, type, fmt)\
1303 void prefix ## cmp(type a, type b)\
1305 printf("%d %d %d %d %d %d\n",\
1312 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1320 printf(fmt "\n", ++a);\
1321 printf(fmt "\n", a++);\
1322 printf(fmt "\n", a);\
1324 void prefix ## fcast(type a)\
1335 printf("ftof: %f %f %Lf\n", fa, da, la);\
1337 ua = (unsigned int)a;\
1338 printf("ftoi: %d %u\n", ia, ua);\
1342 printf("itof: " fmt "\n", b);\
1344 printf("utof: " fmt "\n", b);\
1347 void prefix ## test(void)\
1349 printf("testing '%s'\n", #type);\
1350 prefix ## cmp(1, 2.5);\
1351 prefix ## cmp(2, 1.5);\
1352 prefix ## cmp(1, 1);\
1353 prefix ## fcast(234.6);\
1354 prefix ## fcast(-2334.6);\
1357 FTEST(f
, float, "%f")
1358 FTEST(d
, double, "%f")
1359 FTEST(ld
, long double, "%Lf")
1361 double ftab1
[3] = { 1.2, 3.4, -5.6 };
1364 void float_test(void)
1371 printf("float_test:\n");
1372 printf("sizeof(float) = %d\n", sizeof(float));
1373 printf("sizeof(double) = %d\n", sizeof(double));
1374 printf("sizeof(long double) = %d\n", sizeof(long double));
1378 printf("%f %f %f\n", ftab1
[0], ftab1
[1], ftab1
[2]);
1379 printf("%f %f %f\n", 2.12, .5, 2.3e10
);
1380 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1382 printf("da=%f\n", da
);
1384 printf("fa=%f\n", fa
);
1387 printf("da = %f\n", da
);
1390 printf("db = %f\n", db
);
1398 return fib(n
-1) + fib(n
-2);
1410 printf("funcptr:\n");
1417 /* more complicated pointer computation */
1420 printf("sizeof1 = %d\n", sizeof(funcptr_test
));
1421 printf("sizeof2 = %d\n", sizeof funcptr_test
);
1422 printf("sizeof3 = %d\n", sizeof(&funcptr_test
));
1423 printf("sizeof4 = %d\n", sizeof &funcptr_test
);
1426 void lloptest(long long a
, long long b
)
1428 unsigned long long ua
, ub
;
1433 printf("arith: %Ld %Ld %Ld\n",
1439 printf("arith1: %Ld %Ld\n",
1445 printf("bin: %Ld %Ld %Ld\n",
1451 printf("test: %d %d %d %d %d %d\n",
1459 printf("utest: %d %d %d %d %d %d\n",
1470 printf("arith2: %Ld %Ld\n", a
, b
);
1471 printf("arith2: %Ld %Ld\n", a
++, b
++);
1472 printf("arith2: %Ld %Ld\n", --a
, --b
);
1473 printf("arith2: %Ld %Ld\n", a
, b
);
1476 void llshift(long long a
, int b
)
1478 printf("shift: %Ld %Ld %Ld\n",
1479 (unsigned long long)a
>> b
,
1482 printf("shiftc: %Ld %Ld %Ld\n",
1483 (unsigned long long)a
>> 3,
1486 printf("shiftc: %Ld %Ld %Ld\n",
1487 (unsigned long long)a
>> 35,
1497 long long la
, lb
, lc
;
1498 unsigned long long ula
, ulb
, ulc
;
1501 la
= (la
<< 20) | 0x12345;
1503 printf("la=%Ld ula=%Lu\n", la
, ula
);
1508 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
1513 printf("ftoll: %Ld %Ld %Ld\n", la
, lb
, lc
);
1518 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
1523 printf("ftoull: %Lu %Lu %Lu\n", ula
, ulb
, ulc
);
1526 long long llfunc1(int a
)
1536 long long int value(struct S
*v
)
1538 return ((long long int)v
->item
);
1541 void longlong_test(void)
1546 printf("longlong_test:\n");
1547 printf("sizeof(long long) = %d\n", sizeof(long long));
1552 printf("%Ld %Ld\n", a
, b
);
1553 printf("%Ld %Ld %Ld %Lx\n",
1557 0x1234567812345679);
1562 lloptest(0xff, 0x1234);
1563 b
= 0x72345678 << 10;
1567 b
= 0x72345678LL
<< 10;
1578 /* long long reg spill test */
1583 printf("%lld\n", value(&a
));
1585 lloptest(0x80000000, 0);
1588 void vprintf1(const char *fmt
, ...)
1610 i
= va_arg(ap
, int);
1614 d
= va_arg(ap
, double);
1618 ll
= va_arg(ap
, long long);
1632 void stdarg_test(void)
1634 vprintf1("%d %d %d\n", 1, 2, 3);
1635 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1636 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1639 void whitespace_test(void)
1645 ntf("whitspace:\n");\f\v
1649 #ifdef CORRECT_CR_HANDLING
1657 printf("len1=%d\n", strlen("
1659 #ifdef CORRECT_CR_HANDLING
1662 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
1664 printf("len1=%d\n", strlen("
a
1668 int reltab
[3] = { 1, 2, 3 };
1670 int *rel1
= &reltab
[1];
1671 int *rel2
= &reltab
[2];
1673 void relocation_test(void)
1675 printf("*rel1=%d\n", *rel1
);
1676 printf("*rel2=%d\n", *rel2
);
1679 void old_style_f(a
,b
,c
)
1683 printf("a=%d b=%d b=%f\n", a
, b
, c
);
1686 void decl_func1(int cmpfn())
1688 printf("cmpfn=%lx\n", (long)cmpfn
);
1691 void decl_func2(cmpfn
)
1694 printf("cmpfn=%lx\n", (long)cmpfn
);
1697 void old_style_function(void)
1699 old_style_f((void *)1, 2, 3.0);
1704 void sizeof_test(void)
1709 printf("sizeof(int) = %d\n", sizeof(int));
1710 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1711 printf("sizeof(short) = %d\n", sizeof(short));
1712 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1713 printf("sizeof(char) = %d\n", sizeof(char));
1714 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1715 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1717 printf("sizeof(a++) = %d\n", sizeof a
++);
1718 printf("a=%d\n", a
);
1720 printf("sizeof(**ptr) = %d\n", sizeof (**ptr
));
1722 /* some alignof tests */
1723 printf("__alignof__(int) = %d\n", __alignof__(int));
1724 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1725 printf("__alignof__(short) = %d\n", __alignof__(short));
1726 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1727 printf("__alignof__(char) = %d\n", __alignof__(char));
1728 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1729 printf("__alignof__(func) = %d\n", __alignof__
sizeof_test());
1732 void typeof_test(void)
1741 printf("a=%f b=%f c=%f\n", a
, b
, c
);
1744 void statement_expr_test(void)
1757 printf("a=%d\n", a
);
1761 void local_label_test(void)
1767 __label__ l1
, l2
, l3
;
1781 printf("a=%d\n", a
);
1791 /* inline assembler test */
1794 /* from linux kernel */
1795 static char * strncat1(char * dest
,const char * src
,size_t count
)
1798 __asm__
__volatile__(
1807 "testb %%al,%%al\n\t"
1809 "2:\txorl %2,%2\n\t"
1811 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
1812 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
1817 static inline void * memcpy1(void * to
, const void * from
, size_t n
)
1820 __asm__
__volatile__(
1825 "1:\ttestb $1,%b4\n\t"
1829 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
1830 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
1835 static __inline__
void sigaddset1(unsigned int *set
, int _sig
)
1837 __asm__("btsl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
1840 static __inline__
void sigdelset1(unsigned int *set
, int _sig
)
1842 asm("btrl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
1845 static __inline__ __const__
unsigned int swab32(unsigned int x
)
1847 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
1848 "rorl $16,%0\n\t" /* swap words */
1849 "xchgb %b0,%h0" /* swap higher bytes */
1855 static __inline__
unsigned long long mul64(unsigned int a
, unsigned int b
)
1857 unsigned long long res
;
1858 __asm__("mull %2" : "=A" (res
) : "a" (a
), "r" (b
));
1862 static __inline__
unsigned long long inc64(unsigned long long a
)
1864 unsigned long long res
;
1865 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res
) : "A" (a
));
1876 printf("inline asm:\n");
1877 /* test the no operand case */
1878 asm volatile ("xorl %eax, %eax");
1880 memcpy1(buf
, "hello", 6);
1881 strncat1(buf
, " worldXXXXX", 3);
1882 printf("%s\n", buf
);
1884 /* 'A' constraint test */
1885 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
1886 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
1889 sigdelset1(&set
, 2);
1890 sigaddset1(&set
, 16);
1891 /* NOTE: we test here if C labels are correctly restored after the
1895 __asm__("btsl %1,%0" : "=m"(set
) : "Ir"(20) : "cc");
1896 printf("set=0x%x\n", set
);
1898 printf("swab32(0x%08x) = 0x%0x\n", val
, swab32(val
));
1912 #define COMPAT_TYPE(type1, type2) \
1914 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
1915 __builtin_types_compatible_p (type1, type2));\
1920 void builtin_test(void)
1923 COMPAT_TYPE(int, int);
1924 COMPAT_TYPE(int, unsigned int);
1925 COMPAT_TYPE(int, char);
1926 COMPAT_TYPE(int, const int);
1927 COMPAT_TYPE(int, volatile int);
1928 COMPAT_TYPE(int *, int *);
1929 COMPAT_TYPE(int *, void *);
1930 COMPAT_TYPE(int *, const int *);
1931 COMPAT_TYPE(char *, unsigned char *);
1932 /* space is needed because tcc preprocessor introduces a space between each token */
1933 COMPAT_TYPE(char * *, void *);
1935 printf("res = %d\n", __builtin_constant_p(1));
1936 printf("res = %d\n", __builtin_constant_p(1 + 2));
1937 printf("res = %d\n", __builtin_constant_p(&constant_p_var
));
1938 printf("res = %d\n", __builtin_constant_p(constant_p_var
));