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 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
257 null argument without a space. gcc 3.2 fixes that. */
260 printf("qq1=%d\n", qq1( ));
262 /* comment with stray handling *\
264 /* this is a valid *\/ comment */
265 /* this is a valid comment *\*/
284 void ps(const char *s
)
296 const char foo1_string
[] = "\
305 printf("\141\1423\143\n");/* dezdez test */
306 printf("\x41\x42\x43\x3a\n");
307 printf("c=%c\n", 'r');
308 printf("wc=%C 0x%lx %C\n", L
'a', L
'\x1234', L
'c');
309 printf("foo1_string='%s'\n", foo1_string
);
311 printf("wstring=%S\n", L
"abc");
312 printf("wstring=%S\n", L
"abc" L
"def" "ghi");
313 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
314 printf("L'\\377'=%d L'\\xff'=%d\n", L
'\377', L
'\xff');
318 while ((b
= b
+ 1) < 96) {
322 printf("fib=%d\n", fib(33));
324 while (b
!= 0x80000000) {
337 for(i
= 0; i
< 10;i
++)
346 /* break/continue tests */
358 /* break/continue tests */
370 for(i
= 0;i
< 10;i
++) {
382 static void *label_table
[3] = { &&label1
, &&label2
, &&label3
};
395 /* we also test computed gotos (GCC extension) */
397 goto *label_table
[i
];
425 printf("enum:\n%d %d %d %d %d %d\n",
426 E0
, E1
, E2
, E3
, E4
, E5
);
428 printf("b1=%d\n", b1
);
439 printf("typedef:\n");
440 printf("a=%d\n", *a
);
445 printf("forward:\n");
451 void forward_ref(void)
453 printf("forward ok\n");
456 typedef struct struct1
{
476 struct struct1 st1
, st2
;
478 int main(int argc
, char **argv
)
496 constant_expr_test();
500 compound_literal_test();
502 struct_assign_test();
511 old_style_function();
514 statement_expr_test();
528 printf("g1=%d\n", g
);
536 printf("g2=%d\n", g
);
540 printf("g3=%d\n", g
);
544 printf("g4=%d\n", g
);
547 printf("g5=%d\n", g
);
550 void array_test(int a
[4])
555 printf("sizeof(a) = %d\n", sizeof(a
));
556 printf("sizeof(\"a\") = %d\n", sizeof("a"));
558 printf("sizeof(__func__) = %d\n", sizeof(__func__
));
560 printf("sizeof tab %d\n", sizeof(tab
));
561 printf("sizeof tab2 %d\n", sizeof tab2
);
565 printf("%d %d %d\n", tab
[0], tab
[1], tab
[2]);
568 tab2
[i
][j
] = 10 * i
+ j
;
570 printf(" %3d", ((int *)tab2
)[i
]);
579 printf("%d\n", a
+= 1);
580 printf("%d\n", a
-= 2);
581 printf("%d\n", a
*= 31232132);
582 printf("%d\n", a
/= 4);
583 printf("%d\n", a
%= 20);
584 printf("%d\n", a
&= 6);
585 printf("%d\n", a
^= 7);
586 printf("%d\n", a
|= 8);
587 printf("%d\n", a
>>= 3);
588 printf("%d\n", a
<<= 4);
592 printf("%d\n", a
+ 1);
593 printf("%d\n", a
- 2);
594 printf("%d\n", a
* 312);
595 printf("%d\n", a
/ 4);
596 printf("%d\n", b
/ 4);
597 printf("%d\n", (unsigned)b
/ 4);
598 printf("%d\n", a
% 20);
599 printf("%d\n", b
% 20);
600 printf("%d\n", (unsigned)b
% 20);
601 printf("%d\n", a
& 6);
602 printf("%d\n", a
^ 7);
603 printf("%d\n", a
| 8);
604 printf("%d\n", a
>> 3);
605 printf("%d\n", b
>> 3);
606 printf("%d\n", (unsigned)b
>> 3);
607 printf("%d\n", a
<< 4);
612 printf("%d\n", 12 + 1);
613 printf("%d\n", 12 - 2);
614 printf("%d\n", 12 * 312);
615 printf("%d\n", 12 / 4);
616 printf("%d\n", 12 % 20);
617 printf("%d\n", 12 & 6);
618 printf("%d\n", 12 ^ 7);
619 printf("%d\n", 12 | 8);
620 printf("%d\n", 12 >> 2);
621 printf("%d\n", 12 << 4);
625 printf("%d %d %d %d\n",
634 return (c
>= 'a' & c
<= 'z') | (c
>= 'A' & c
<= 'Z') | c
== '_';
637 /**********************/
639 int vstack
[10], *vstack_ptr
;
641 void vpush(int vt
, int vc
)
647 void vpop(int *ft
, int *fc
)
660 vstack_ptr
[-2] &= ~0xffffff80;
662 printf("res= %d %d\n", a
, b
);
665 void constant_expr_test()
668 printf("constant_expr:\n");
670 printf("%d\n", a
* 16);
671 printf("%d\n", a
* 1);
672 printf("%d\n", a
+ 0);
681 printf("expr_ptr:\n");
684 printf("diff=%d\n", q
- p
);
686 printf("inc=%d\n", p
- tab4
);
688 printf("dec=%d\n", p
- tab4
);
690 printf("inc=%d\n", p
- tab4
);
692 printf("dec=%d\n", p
- tab4
);
693 printf("add=%d\n", p
+ 3 - tab4
);
694 printf("add=%d\n", 3 + p
- tab4
);
700 printf("constant_expr:\n");
703 printf("%d\n", a
== a
);
704 printf("%d\n", a
!= a
);
706 printf("%d\n", a
< b
);
707 printf("%d\n", a
<= b
);
708 printf("%d\n", a
<= a
);
709 printf("%d\n", b
>= a
);
710 printf("%d\n", a
>= a
);
711 printf("%d\n", b
> a
);
713 printf("%d\n", (unsigned)a
< b
);
714 printf("%d\n", (unsigned)a
<= b
);
715 printf("%d\n", (unsigned)a
<= a
);
716 printf("%d\n", (unsigned)b
>= a
);
717 printf("%d\n", (unsigned)a
>= a
);
718 printf("%d\n", (unsigned)b
> a
);
747 printf("sizes: %d %d %d %d\n",
748 sizeof(struct struct1
),
749 sizeof(struct struct2
),
750 sizeof(union union1
),
751 sizeof(union union2
));
755 printf("st1: %d %d %d\n",
756 st1
.f1
, st1
.f2
, st1
.f3
);
759 printf("union1: %d\n", st1
.u
.v1
);
762 printf("union2: %d\n", u
.w1
);
767 printf("st2: %d %d %d\n",
768 s
->f1
, s
->f2
, s
->f3
);
769 printf("str_addr=%x\n", (int)st1
.str
- (int)&st1
.f1
);
771 /* align / size tests */
772 printf("aligntest1 sizeof=%d alignof=%d\n",
773 sizeof(struct aligntest1
), __alignof__(struct aligntest1
));
774 printf("aligntest2 sizeof=%d alignof=%d\n",
775 sizeof(struct aligntest2
), __alignof__(struct aligntest2
));
776 printf("aligntest3 sizeof=%d alignof=%d\n",
777 sizeof(struct aligntest3
), __alignof__(struct aligntest3
));
778 printf("aligntest4 sizeof=%d alignof=%d\n",
779 sizeof(struct aligntest4
), __alignof__(struct aligntest4
));
781 /* empty structures (GCC extension) */
782 printf("sizeof(struct empty) = %d\n", sizeof(struct empty
));
783 printf("alignof(struct empty) = %d\n", __alignof__(struct empty
));
786 /* XXX: depend on endianness */
787 void char_short_test()
791 printf("char_short:\n");
796 *(char *)&var1
, *(char *)&var2
);
798 *(unsigned char *)&var1
, *(unsigned char *)&var2
);
799 printf("s16=%d %d\n",
800 *(short *)&var1
, *(short *)&var2
);
801 printf("u16=%d %d\n",
802 *(unsigned short *)&var1
, *(unsigned short *)&var2
);
803 printf("s32=%d %d\n",
804 *(int *)&var1
, *(int *)&var2
);
805 printf("u32=%d %d\n",
806 *(unsigned int *)&var1
, *(unsigned int *)&var2
);
807 *(char *)&var1
= 0x08;
808 printf("var1=%x\n", var1
);
809 *(short *)&var1
= 0x0809;
810 printf("var1=%x\n", var1
);
811 *(int *)&var1
= 0x08090a0b;
812 printf("var1=%x\n", var1
);
825 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
826 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
828 static int toupper1(int a
)
835 int *s
, a
, b
, t
, f
, i
;
839 printf("!s=%d\n", !s
);
845 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
846 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
847 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
862 printf("b=%d\n", a
+ (0 ? 1 : a
/ 2));
864 /* test register spilling */
867 a
= (a
+ b
) * ((a
< b
) ?
868 ((b
- a
) * (a
- b
)): a
+ b
);
871 /* test complex || or && expressions */
875 printf("exp=%d\n", f
== (32 <= a
&& a
<= 3));
876 printf("r=%d\n", (t
|| f
) + (t
&& f
));
881 int aspect_native
= 65536;
882 double bfu_aspect
= 1.0;
884 for(aspect_on
= 0; aspect_on
< 2; aspect_on
++) {
885 aspect
=aspect_on
?(aspect_native
*bfu_aspect
+0.5):65535UL;
886 printf("aspect=%d\n", aspect
);
890 /* test ? : GCC extension */
892 static int v1
= 34 ? : -1; /* constant case */
893 static int v2
= 0 ? : -1; /* constant case */
896 printf("%d %d\n", v1
, v2
);
897 printf("%d %d\n", a
- 30 ? : a
* 2, a
+ 1 ? : a
* 2);
900 /* again complex expression */
902 if (toupper1 (i
) != TOUPPER (i
))
903 printf("error %d\n", i
);
907 /* GCC accepts that */
908 static int tab_reinit
[];
909 static int tab_reinit
[10];
911 //int cinit1; /* a global variable can be defined several times without error ! */
915 int *cinit2
= (int []){3, 2, 1};
917 void compound_literal_test(void)
922 printf("compound_test:\n");
924 p
= (int []){1, 2, 3};
930 printf("%d", cinit2
[i
]);
934 printf("q1=%s\n", q
);
936 q
= (char *){ "tralala2" };
937 printf("q2=%s\n", q
);
940 printf("q3=%s\n", q3
);
942 q
= (char []){ "tralala3" };
943 printf("q4=%s\n", q
);
946 p
= (int []){1, 2, cinit1
+ 3};
952 p
= (int []){1, 2, 4 + i
};
975 printf("kr_test:\n");
976 printf("func1=%d\n", kr_func1(3, 4));
977 printf("func2=%d\n", kr_func2(3, 4));
984 tab
= (char*)malloc(20);
1000 /* structure assignment tests */
1006 struct structa1 ssta1
;
1008 void struct_assign_test1(struct structa1 s1
, int t
)
1010 printf("%d %d %d\n", s1
.f1
, s1
.f2
, t
);
1013 struct structa1
struct_assign_test2(struct structa1 s1
, int t
)
1020 void struct_assign_test(void)
1022 struct structa1 lsta1
, lsta2
;
1025 printf("struct_assign_test:\n");
1029 printf("%d %d\n", lsta1
.f1
, lsta1
.f2
);
1031 printf("%d %d\n", lsta2
.f1
, lsta2
.f2
);
1036 struct_assign_test1(lsta2
, 3);
1038 printf("before call: %d %d\n", lsta2
.f1
, lsta2
.f2
);
1039 lsta2
= struct_assign_test2(lsta2
, 4);
1040 printf("after call: %d %d\n", lsta2
.f1
, lsta2
.f2
);
1043 /* casts to short/char */
1045 void cast1(char a
, short b
, unsigned char c
, unsigned short d
)
1047 printf("%d %d %d %d\n", a
, b
, c
, d
);
1059 printf("cast_test:\n");
1063 printf("%d %d %d %d\n",
1066 (unsigned char)(a
+ 1),
1067 (unsigned short)(a
+ 1));
1068 printf("%d %d %d %d\n",
1071 (unsigned char)0xfffff,
1072 (unsigned short)0xfffff);
1074 a
= (bcast
= 128) + 1;
1076 a
= (scast
= 65536) + 1;
1079 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
1081 /* test implicit int casting for array accesses */
1085 printf("%d %d\n", tab
[0], tab
[1]);
1087 /* test implicit casting on some operators */
1088 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1089 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1090 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1093 /* initializers tests */
1094 struct structinit1
{
1103 int sinit3
[3] = { 1, 2, {{3}}, };
1104 int sinit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1105 int sinit5
[3][2] = { 1, 2, 3, 4, 5, 6 };
1106 int sinit6
[] = { 1, 2, 3 };
1107 int sinit7
[] = { [2] = 3, [0] = 1, 2 };
1108 char sinit8
[] = "hello" "trala";
1110 struct structinit1 sinit9
= { 1, 2, 3 };
1111 struct structinit1 sinit10
= { .f2
= 2, 3, .f1
= 1 };
1112 struct structinit1 sinit11
= { .f2
= 2, 3, .f1
= 1,
1120 char *sinit12
= "hello world";
1126 char sinit14
[10] = { "abc" };
1127 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
1129 struct { int a
[3], b
; } sinit16
[] = { { 1 }, 2 };
1145 void init_test(void)
1149 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1150 int linit6
[] = { 1, 2, 3 };
1152 char linit8
[] = "hello" "trala";
1153 int linit12
[10] = { 1, 2 };
1154 int linit13
[10] = { 1, 2, [7] = 3, [3] = 4, };
1155 char linit14
[10] = "abc";
1156 int linit15
[10] = { linit1
, linit1
+ 1, [6] = linit1
+ 2, };
1157 struct linit16
{ int a1
, a2
, a3
, a4
; } linit16
= { 1, .a3
= 2 };
1158 int linit17
= sizeof(linit17
);
1160 printf("init_test:\n");
1162 printf("sinit1=%d\n", sinit1
);
1163 printf("sinit2=%d\n", sinit2
);
1164 printf("sinit3=%d %d %d %d\n",
1170 printf("sinit6=%d\n", sizeof(sinit6
));
1171 printf("sinit7=%d %d %d %d\n",
1177 printf("sinit8=%s\n", sinit8
);
1178 printf("sinit9=%d %d %d\n",
1183 printf("sinit10=%d %d %d\n",
1188 printf("sinit11=%d %d %d %d %d %d\n",
1199 printf("[%d][%d] = %d %d %d\n",
1200 i
, j
, sinit4
[i
][j
], sinit5
[i
][j
], linit4
[i
][j
]);
1201 printf("linit1=%d\n", linit1
);
1202 printf("linit2=%d\n", linit2
);
1203 printf("linit6=%d\n", sizeof(linit6
));
1204 printf("linit8=%d %s\n", sizeof(linit8
), linit8
);
1206 printf("sinit12=%s\n", sinit12
);
1207 printf("sinit13=%d %s %s %s\n",
1212 printf("sinit14=%s\n", sinit14
);
1214 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
1216 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
1218 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
1220 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
1222 printf("%d %d %d %d\n",
1227 /* test that initialisation is done after variable declare */
1228 printf("linit17=%d\n", linit17
);
1229 printf("sinit15=%d\n", sinit15
[0]);
1230 printf("sinit16=%d %d\n", sinit16
[0].a
[0], sinit16
[1].a
[0]);
1231 printf("sinit17=%s %d %s %d\n",
1232 sinit17
[0].s
, sinit17
[0].len
,
1233 sinit17
[1].s
, sinit17
[1].len
);
1235 printf("%x ", sinit18
[i
]);
1264 /* ISOC99 _Bool type */
1265 void c99_bool_test(void)
1271 printf("bool_test:\n");
1272 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
1274 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
1276 printf("b = %d\n", b
);
1278 printf("b = %d\n", b
);
1282 void bitfield_test(void)
1292 unsigned int f5
: 7;
1294 printf("bitfield_test:");
1295 printf("sizeof(st1) = %d\n", sizeof(st1
));
1304 printf("%d %d %d %d %d\n",
1305 st1
.f1
, st1
.f2
, st1
.f3
, st1
.f4
, st1
.f5
);
1308 #define FTEST(prefix, type, fmt)\
1309 void prefix ## cmp(type a, type b)\
1311 printf("%d %d %d %d %d %d\n",\
1318 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1326 printf(fmt "\n", ++a);\
1327 printf(fmt "\n", a++);\
1328 printf(fmt "\n", a);\
1330 void prefix ## fcast(type a)\
1341 printf("ftof: %f %f %Lf\n", fa, da, la);\
1343 ua = (unsigned int)a;\
1344 printf("ftoi: %d %u\n", ia, ua);\
1348 printf("itof: " fmt "\n", b);\
1350 printf("utof: " fmt "\n", b);\
1353 void prefix ## test(void)\
1355 printf("testing '%s'\n", #type);\
1356 prefix ## cmp(1, 2.5);\
1357 prefix ## cmp(2, 1.5);\
1358 prefix ## cmp(1, 1);\
1359 prefix ## fcast(234.6);\
1360 prefix ## fcast(-2334.6);\
1363 FTEST(f
, float, "%f")
1364 FTEST(d
, double, "%f")
1365 FTEST(ld
, long double, "%Lf")
1367 double ftab1
[3] = { 1.2, 3.4, -5.6 };
1370 void float_test(void)
1377 printf("float_test:\n");
1378 printf("sizeof(float) = %d\n", sizeof(float));
1379 printf("sizeof(double) = %d\n", sizeof(double));
1380 printf("sizeof(long double) = %d\n", sizeof(long double));
1384 printf("%f %f %f\n", ftab1
[0], ftab1
[1], ftab1
[2]);
1385 printf("%f %f %f\n", 2.12, .5, 2.3e10
);
1386 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1388 printf("da=%f\n", da
);
1390 printf("fa=%f\n", fa
);
1393 printf("da = %f\n", da
);
1396 printf("db = %f\n", db
);
1404 return fib(n
-1) + fib(n
-2);
1416 printf("funcptr:\n");
1423 /* more complicated pointer computation */
1426 printf("sizeof1 = %d\n", sizeof(funcptr_test
));
1427 printf("sizeof2 = %d\n", sizeof funcptr_test
);
1428 printf("sizeof3 = %d\n", sizeof(&funcptr_test
));
1429 printf("sizeof4 = %d\n", sizeof &funcptr_test
);
1432 void lloptest(long long a
, long long b
)
1434 unsigned long long ua
, ub
;
1439 printf("arith: %Ld %Ld %Ld\n",
1445 printf("arith1: %Ld %Ld\n",
1451 printf("bin: %Ld %Ld %Ld\n",
1457 printf("test: %d %d %d %d %d %d\n",
1465 printf("utest: %d %d %d %d %d %d\n",
1476 printf("arith2: %Ld %Ld\n", a
, b
);
1477 printf("arith2: %Ld %Ld\n", a
++, b
++);
1478 printf("arith2: %Ld %Ld\n", --a
, --b
);
1479 printf("arith2: %Ld %Ld\n", a
, b
);
1482 void llshift(long long a
, int b
)
1484 printf("shift: %Ld %Ld %Ld\n",
1485 (unsigned long long)a
>> b
,
1488 printf("shiftc: %Ld %Ld %Ld\n",
1489 (unsigned long long)a
>> 3,
1492 printf("shiftc: %Ld %Ld %Ld\n",
1493 (unsigned long long)a
>> 35,
1503 long long la
, lb
, lc
;
1504 unsigned long long ula
, ulb
, ulc
;
1507 la
= (la
<< 20) | 0x12345;
1509 printf("la=%Ld ula=%Lu\n", la
, ula
);
1514 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
1519 printf("ftoll: %Ld %Ld %Ld\n", la
, lb
, lc
);
1524 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
1529 printf("ftoull: %Lu %Lu %Lu\n", ula
, ulb
, ulc
);
1532 long long llfunc1(int a
)
1542 long long int value(struct S
*v
)
1544 return ((long long int)v
->item
);
1547 void longlong_test(void)
1552 printf("longlong_test:\n");
1553 printf("sizeof(long long) = %d\n", sizeof(long long));
1558 printf("%Ld %Ld\n", a
, b
);
1559 printf("%Ld %Ld %Ld %Lx\n",
1563 0x1234567812345679);
1568 lloptest(0xff, 0x1234);
1569 b
= 0x72345678 << 10;
1573 b
= 0x72345678LL
<< 10;
1584 /* long long reg spill test */
1589 printf("%lld\n", value(&a
));
1591 lloptest(0x80000000, 0);
1594 void vprintf1(const char *fmt
, ...)
1616 i
= va_arg(ap
, int);
1620 d
= va_arg(ap
, double);
1624 ll
= va_arg(ap
, long long);
1638 void stdarg_test(void)
1640 vprintf1("%d %d %d\n", 1, 2, 3);
1641 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1642 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1645 void whitespace_test(void)
1651 ntf("whitspace:\n");\f\v
1655 #ifdef CORRECT_CR_HANDLING
1663 printf("len1=%d\n", strlen("
1665 #ifdef CORRECT_CR_HANDLING
1668 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
1670 printf("len1=%d\n", strlen("
a
1674 int reltab
[3] = { 1, 2, 3 };
1676 int *rel1
= &reltab
[1];
1677 int *rel2
= &reltab
[2];
1679 void relocation_test(void)
1681 printf("*rel1=%d\n", *rel1
);
1682 printf("*rel2=%d\n", *rel2
);
1685 void old_style_f(a
,b
,c
)
1689 printf("a=%d b=%d b=%f\n", a
, b
, c
);
1692 void decl_func1(int cmpfn())
1694 printf("cmpfn=%lx\n", (long)cmpfn
);
1697 void decl_func2(cmpfn
)
1700 printf("cmpfn=%lx\n", (long)cmpfn
);
1703 void old_style_function(void)
1705 old_style_f((void *)1, 2, 3.0);
1710 void sizeof_test(void)
1715 printf("sizeof(int) = %d\n", sizeof(int));
1716 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1717 printf("sizeof(short) = %d\n", sizeof(short));
1718 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1719 printf("sizeof(char) = %d\n", sizeof(char));
1720 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1721 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1723 printf("sizeof(a++) = %d\n", sizeof a
++);
1724 printf("a=%d\n", a
);
1726 printf("sizeof(**ptr) = %d\n", sizeof (**ptr
));
1728 /* some alignof tests */
1729 printf("__alignof__(int) = %d\n", __alignof__(int));
1730 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1731 printf("__alignof__(short) = %d\n", __alignof__(short));
1732 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1733 printf("__alignof__(char) = %d\n", __alignof__(char));
1734 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1735 printf("__alignof__(func) = %d\n", __alignof__
sizeof_test());
1738 void typeof_test(void)
1747 printf("a=%f b=%f c=%f\n", a
, b
, c
);
1750 void statement_expr_test(void)
1763 printf("a=%d\n", a
);
1767 void local_label_test(void)
1773 __label__ l1
, l2
, l3
;
1787 printf("a=%d\n", a
);
1797 /* inline assembler test */
1800 /* from linux kernel */
1801 static char * strncat1(char * dest
,const char * src
,size_t count
)
1804 __asm__
__volatile__(
1813 "testb %%al,%%al\n\t"
1815 "2:\txorl %2,%2\n\t"
1817 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
1818 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
1823 static inline void * memcpy1(void * to
, const void * from
, size_t n
)
1826 __asm__
__volatile__(
1831 "1:\ttestb $1,%b4\n\t"
1835 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
1836 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
1841 static __inline__
void sigaddset1(unsigned int *set
, int _sig
)
1843 __asm__("btsl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
1846 static __inline__
void sigdelset1(unsigned int *set
, int _sig
)
1848 asm("btrl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
1851 static __inline__ __const__
unsigned int swab32(unsigned int x
)
1853 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
1854 "rorl $16,%0\n\t" /* swap words */
1855 "xchgb %b0,%h0" /* swap higher bytes */
1861 static __inline__
unsigned long long mul64(unsigned int a
, unsigned int b
)
1863 unsigned long long res
;
1864 __asm__("mull %2" : "=A" (res
) : "a" (a
), "r" (b
));
1868 static __inline__
unsigned long long inc64(unsigned long long a
)
1870 unsigned long long res
;
1871 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res
) : "A" (a
));
1882 printf("inline asm:\n");
1883 /* test the no operand case */
1884 asm volatile ("xorl %eax, %eax");
1886 memcpy1(buf
, "hello", 6);
1887 strncat1(buf
, " worldXXXXX", 3);
1888 printf("%s\n", buf
);
1890 /* 'A' constraint test */
1891 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
1892 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
1895 sigdelset1(&set
, 2);
1896 sigaddset1(&set
, 16);
1897 /* NOTE: we test here if C labels are correctly restored after the
1901 __asm__("btsl %1,%0" : "=m"(set
) : "Ir"(20) : "cc");
1902 printf("set=0x%x\n", set
);
1904 printf("swab32(0x%08x) = 0x%0x\n", val
, swab32(val
));
1918 #define COMPAT_TYPE(type1, type2) \
1920 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
1921 __builtin_types_compatible_p (type1, type2));\
1926 void builtin_test(void)
1929 COMPAT_TYPE(int, int);
1930 COMPAT_TYPE(int, unsigned int);
1931 COMPAT_TYPE(int, char);
1932 COMPAT_TYPE(int, const int);
1933 COMPAT_TYPE(int, volatile int);
1934 COMPAT_TYPE(int *, int *);
1935 COMPAT_TYPE(int *, void *);
1936 COMPAT_TYPE(int *, const int *);
1937 COMPAT_TYPE(char *, unsigned char *);
1938 /* space is needed because tcc preprocessor introduces a space between each token */
1939 COMPAT_TYPE(char * *, void *);
1941 printf("res = %d\n", __builtin_constant_p(1));
1942 printf("res = %d\n", __builtin_constant_p(1 + 2));
1943 printf("res = %d\n", __builtin_constant_p(&constant_p_var
));
1944 printf("res = %d\n", __builtin_constant_p(constant_p_var
));
1948 void const_func(const int a
)
1952 void const_warn_test(void)