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 stdarg_test(void);
74 void whitespace_test(void);
75 void relocation_test(void);
76 void old_style_function(void);
77 void sizeof_test(void);
78 void typeof_test(void);
79 void local_label_test(void);
80 void statement_expr_test(void);
82 void builtin_test(void);
86 void forward_ref(void);
92 #define M1(a, b) (a) + (b)
96 #define glue(a, b) a ## b
97 #define xglue(a, b) glue(a, b)
98 #define HIGHLOW "hello"
99 #define LOW LOW ", world"
101 #define min(a, b) ((a) < (b) ? (a) : (b))
104 #define dprintf(level,...) printf(__VA_ARGS__)
107 /* gcc vararg macros */
108 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
110 #define MACRO_NOARGS()
126 #define __INT64_C(c) c ## LL
127 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
135 void macro_test(void)
137 printf("macro:\n");\f\v
139 printf("aaa=%d\n", AAA
);
141 printf("min=%d\n", min(1, min(2, -1)));
143 printf("s1=%s\n", glue(HIGH
, LOW
));
144 printf("s2=%s\n", xglue(HIGH
, LOW
));
145 printf("s3=%s\n", str("c"));
146 printf("s4=%s\n", str(a1
));
147 printf("B3=%d\n", B3
);
150 printf("A defined\n");
153 printf("B defined\n");
156 printf("A defined\n");
158 printf("A not defined\n");
161 printf("B defined\n");
163 printf("B not defined\n");
167 printf("A defined\n");
169 printf("B1 defined\n");
171 printf("B1 not defined\n");
174 printf("A not defined\n");
176 printf("B2 defined\n");
178 printf("B2 not defined\n");
183 printf("test true1\n");
186 printf("test true2\n");
189 printf("test true3\n");
192 printf("test trueA\n");
195 printf("test trueB\n");
211 printf("__LINE__ defined\n");
214 printf("__LINE__=%d __FILE__=%s\n",
217 printf("__LINE__=%d __FILE__=%s\n",
220 printf("__LINE__=%d __FILE__=%s\n",
222 #line 220 "tcctest.c"
224 /* not strictly preprocessor, but we test it there */
226 printf("__func__ = %s\n", __func__
);
227 dprintf(1, "vaarg=%d\n", 1);
229 dprintf1(1, "vaarg1\n");
230 dprintf1(1, "vaarg1=%d\n", 2);
231 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
234 printf("func='%s'\n", __FUNCTION__
);
236 /* complicated macros in glibc */
237 printf("INT64_MIN=%Ld\n", INT64_MIN
);
247 /* macro function with argument outside the macro string */
248 #define MF_s MF_hello
249 #define MF_hello(msg) printf("%s\n",msg)
251 #define MF_t printf("tralala\n"); MF_hello
256 /* test macro substituion inside args (should not eat stream) */
257 printf("qq=%d\n", qq(qq
)(2));
259 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
260 null argument without a space. gcc 3.2 fixes that. */
263 printf("qq1=%d\n", qq1( ));
265 /* comment with stray handling *\
267 /* this is a valid *\/ comment */
268 /* this is a valid comment *\*/
287 void ps(const char *s
)
299 const char foo1_string
[] = "\
308 printf("\141\1423\143\n");/* dezdez test */
309 printf("\x41\x42\x43\x3a\n");
310 printf("c=%c\n", 'r');
311 printf("wc=%C 0x%lx %C\n", L
'a', L
'\x1234', L
'c');
312 printf("foo1_string='%s'\n", foo1_string
);
314 printf("wstring=%S\n", L
"abc");
315 printf("wstring=%S\n", L
"abc" L
"def" "ghi");
316 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
317 printf("L'\\377'=%d L'\\xff'=%d\n", L
'\377', L
'\xff');
321 while ((b
= b
+ 1) < 96) {
325 printf("fib=%d\n", fib(33));
327 while (b
!= 0x80000000) {
340 for(i
= 0; i
< 10;i
++)
349 /* break/continue tests */
361 /* break/continue tests */
373 for(i
= 0;i
< 10;i
++) {
385 static void *label_table
[3] = { &&label1
, &&label2
, &&label3
};
398 /* we also test computed gotos (GCC extension) */
400 goto *label_table
[i
];
428 printf("enum:\n%d %d %d %d %d %d\n",
429 E0
, E1
, E2
, E3
, E4
, E5
);
431 printf("b1=%d\n", b1
);
442 printf("typedef:\n");
443 printf("a=%d\n", *a
);
448 printf("forward:\n");
454 void forward_ref(void)
456 printf("forward ok\n");
459 typedef struct struct1
{
479 struct struct1 st1
, st2
;
481 int main(int argc
, char **argv
)
499 constant_expr_test();
503 compound_literal_test();
505 struct_assign_test();
514 old_style_function();
517 statement_expr_test();
531 printf("g1=%d\n", g
);
539 printf("g2=%d\n", g
);
543 printf("g3=%d\n", g
);
547 printf("g4=%d\n", g
);
550 printf("g5=%d\n", g
);
553 void array_test(int a
[4])
558 printf("sizeof(a) = %d\n", sizeof(a
));
559 printf("sizeof(\"a\") = %d\n", sizeof("a"));
561 printf("sizeof(__func__) = %d\n", sizeof(__func__
));
563 printf("sizeof tab %d\n", sizeof(tab
));
564 printf("sizeof tab2 %d\n", sizeof tab2
);
568 printf("%d %d %d\n", tab
[0], tab
[1], tab
[2]);
571 tab2
[i
][j
] = 10 * i
+ j
;
573 printf(" %3d", ((int *)tab2
)[i
]);
582 printf("%d\n", a
+= 1);
583 printf("%d\n", a
-= 2);
584 printf("%d\n", a
*= 31232132);
585 printf("%d\n", a
/= 4);
586 printf("%d\n", a
%= 20);
587 printf("%d\n", a
&= 6);
588 printf("%d\n", a
^= 7);
589 printf("%d\n", a
|= 8);
590 printf("%d\n", a
>>= 3);
591 printf("%d\n", a
<<= 4);
595 printf("%d\n", a
+ 1);
596 printf("%d\n", a
- 2);
597 printf("%d\n", a
* 312);
598 printf("%d\n", a
/ 4);
599 printf("%d\n", b
/ 4);
600 printf("%d\n", (unsigned)b
/ 4);
601 printf("%d\n", a
% 20);
602 printf("%d\n", b
% 20);
603 printf("%d\n", (unsigned)b
% 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", b
>> 3);
609 printf("%d\n", (unsigned)b
>> 3);
610 printf("%d\n", a
<< 4);
615 printf("%d\n", 12 + 1);
616 printf("%d\n", 12 - 2);
617 printf("%d\n", 12 * 312);
618 printf("%d\n", 12 / 4);
619 printf("%d\n", 12 % 20);
620 printf("%d\n", 12 & 6);
621 printf("%d\n", 12 ^ 7);
622 printf("%d\n", 12 | 8);
623 printf("%d\n", 12 >> 2);
624 printf("%d\n", 12 << 4);
628 printf("%d %d %d %d\n",
637 return (c
>= 'a' & c
<= 'z') | (c
>= 'A' & c
<= 'Z') | c
== '_';
640 /**********************/
642 int vstack
[10], *vstack_ptr
;
644 void vpush(int vt
, int vc
)
650 void vpop(int *ft
, int *fc
)
663 vstack_ptr
[-2] &= ~0xffffff80;
665 printf("res= %d %d\n", a
, b
);
668 void constant_expr_test()
671 printf("constant_expr:\n");
673 printf("%d\n", a
* 16);
674 printf("%d\n", a
* 1);
675 printf("%d\n", a
+ 0);
684 printf("expr_ptr:\n");
687 printf("diff=%d\n", q
- p
);
689 printf("inc=%d\n", p
- tab4
);
691 printf("dec=%d\n", p
- tab4
);
693 printf("inc=%d\n", p
- tab4
);
695 printf("dec=%d\n", p
- tab4
);
696 printf("add=%d\n", p
+ 3 - tab4
);
697 printf("add=%d\n", 3 + p
- tab4
);
703 printf("constant_expr:\n");
706 printf("%d\n", a
== a
);
707 printf("%d\n", a
!= a
);
709 printf("%d\n", a
< b
);
710 printf("%d\n", a
<= b
);
711 printf("%d\n", a
<= a
);
712 printf("%d\n", b
>= a
);
713 printf("%d\n", a
>= a
);
714 printf("%d\n", b
> a
);
716 printf("%d\n", (unsigned)a
< b
);
717 printf("%d\n", (unsigned)a
<= b
);
718 printf("%d\n", (unsigned)a
<= a
);
719 printf("%d\n", (unsigned)b
>= a
);
720 printf("%d\n", (unsigned)a
>= a
);
721 printf("%d\n", (unsigned)b
> a
);
750 printf("sizes: %d %d %d %d\n",
751 sizeof(struct struct1
),
752 sizeof(struct struct2
),
753 sizeof(union union1
),
754 sizeof(union union2
));
758 printf("st1: %d %d %d\n",
759 st1
.f1
, st1
.f2
, st1
.f3
);
762 printf("union1: %d\n", st1
.u
.v1
);
765 printf("union2: %d\n", u
.w1
);
770 printf("st2: %d %d %d\n",
771 s
->f1
, s
->f2
, s
->f3
);
772 printf("str_addr=%x\n", (int)st1
.str
- (int)&st1
.f1
);
774 /* align / size tests */
775 printf("aligntest1 sizeof=%d alignof=%d\n",
776 sizeof(struct aligntest1
), __alignof__(struct aligntest1
));
777 printf("aligntest2 sizeof=%d alignof=%d\n",
778 sizeof(struct aligntest2
), __alignof__(struct aligntest2
));
779 printf("aligntest3 sizeof=%d alignof=%d\n",
780 sizeof(struct aligntest3
), __alignof__(struct aligntest3
));
781 printf("aligntest4 sizeof=%d alignof=%d\n",
782 sizeof(struct aligntest4
), __alignof__(struct aligntest4
));
784 /* empty structures (GCC extension) */
785 printf("sizeof(struct empty) = %d\n", sizeof(struct empty
));
786 printf("alignof(struct empty) = %d\n", __alignof__(struct empty
));
789 /* XXX: depend on endianness */
790 void char_short_test()
794 printf("char_short:\n");
799 *(char *)&var1
, *(char *)&var2
);
801 *(unsigned char *)&var1
, *(unsigned char *)&var2
);
802 printf("s16=%d %d\n",
803 *(short *)&var1
, *(short *)&var2
);
804 printf("u16=%d %d\n",
805 *(unsigned short *)&var1
, *(unsigned short *)&var2
);
806 printf("s32=%d %d\n",
807 *(int *)&var1
, *(int *)&var2
);
808 printf("u32=%d %d\n",
809 *(unsigned int *)&var1
, *(unsigned int *)&var2
);
810 *(char *)&var1
= 0x08;
811 printf("var1=%x\n", var1
);
812 *(short *)&var1
= 0x0809;
813 printf("var1=%x\n", var1
);
814 *(int *)&var1
= 0x08090a0b;
815 printf("var1=%x\n", var1
);
828 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
829 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
831 static int toupper1(int a
)
838 int *s
, a
, b
, t
, f
, i
;
842 printf("!s=%d\n", !s
);
848 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
849 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
850 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
865 printf("b=%d\n", a
+ (0 ? 1 : a
/ 2));
867 /* test register spilling */
870 a
= (a
+ b
) * ((a
< b
) ?
871 ((b
- a
) * (a
- b
)): a
+ b
);
874 /* test complex || or && expressions */
878 printf("exp=%d\n", f
== (32 <= a
&& a
<= 3));
879 printf("r=%d\n", (t
|| f
) + (t
&& f
));
884 int aspect_native
= 65536;
885 double bfu_aspect
= 1.0;
887 for(aspect_on
= 0; aspect_on
< 2; aspect_on
++) {
888 aspect
=aspect_on
?(aspect_native
*bfu_aspect
+0.5):65535UL;
889 printf("aspect=%d\n", aspect
);
893 /* test ? : GCC extension */
895 static int v1
= 34 ? : -1; /* constant case */
896 static int v2
= 0 ? : -1; /* constant case */
899 printf("%d %d\n", v1
, v2
);
900 printf("%d %d\n", a
- 30 ? : a
* 2, a
+ 1 ? : a
* 2);
903 /* again complex expression */
905 if (toupper1 (i
) != TOUPPER (i
))
906 printf("error %d\n", i
);
910 /* GCC accepts that */
911 static int tab_reinit
[];
912 static int tab_reinit
[10];
914 //int cinit1; /* a global variable can be defined several times without error ! */
918 int *cinit2
= (int []){3, 2, 1};
920 void compound_literal_test(void)
925 printf("compound_test:\n");
927 p
= (int []){1, 2, 3};
933 printf("%d", cinit2
[i
]);
937 printf("q1=%s\n", q
);
939 q
= (char *){ "tralala2" };
940 printf("q2=%s\n", q
);
943 printf("q3=%s\n", q3
);
945 q
= (char []){ "tralala3" };
946 printf("q4=%s\n", q
);
949 p
= (int []){1, 2, cinit1
+ 3};
955 p
= (int []){1, 2, 4 + i
};
978 printf("kr_test:\n");
979 printf("func1=%d\n", kr_func1(3, 4));
980 printf("func2=%d\n", kr_func2(3, 4));
987 tab
= (char*)malloc(20);
1003 /* structure assignment tests */
1009 struct structa1 ssta1
;
1011 void struct_assign_test1(struct structa1 s1
, int t
)
1013 printf("%d %d %d\n", s1
.f1
, s1
.f2
, t
);
1016 struct structa1
struct_assign_test2(struct structa1 s1
, int t
)
1023 void struct_assign_test(void)
1025 struct structa1 lsta1
, lsta2
;
1028 printf("struct_assign_test:\n");
1032 printf("%d %d\n", lsta1
.f1
, lsta1
.f2
);
1034 printf("%d %d\n", lsta2
.f1
, lsta2
.f2
);
1039 struct_assign_test1(lsta2
, 3);
1041 printf("before call: %d %d\n", lsta2
.f1
, lsta2
.f2
);
1042 lsta2
= struct_assign_test2(lsta2
, 4);
1043 printf("after call: %d %d\n", lsta2
.f1
, lsta2
.f2
);
1046 /* casts to short/char */
1048 void cast1(char a
, short b
, unsigned char c
, unsigned short d
)
1050 printf("%d %d %d %d\n", a
, b
, c
, d
);
1062 printf("cast_test:\n");
1066 printf("%d %d %d %d\n",
1069 (unsigned char)(a
+ 1),
1070 (unsigned short)(a
+ 1));
1071 printf("%d %d %d %d\n",
1074 (unsigned char)0xfffff,
1075 (unsigned short)0xfffff);
1077 a
= (bcast
= 128) + 1;
1079 a
= (scast
= 65536) + 1;
1082 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
1084 /* test implicit int casting for array accesses */
1088 printf("%d %d\n", tab
[0], tab
[1]);
1090 /* test implicit casting on some operators */
1091 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1092 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1093 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1096 /* initializers tests */
1097 struct structinit1
{
1106 int sinit3
[3] = { 1, 2, {{3}}, };
1107 int sinit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1108 int sinit5
[3][2] = { 1, 2, 3, 4, 5, 6 };
1109 int sinit6
[] = { 1, 2, 3 };
1110 int sinit7
[] = { [2] = 3, [0] = 1, 2 };
1111 char sinit8
[] = "hello" "trala";
1113 struct structinit1 sinit9
= { 1, 2, 3 };
1114 struct structinit1 sinit10
= { .f2
= 2, 3, .f1
= 1 };
1115 struct structinit1 sinit11
= { .f2
= 2, 3, .f1
= 1,
1123 char *sinit12
= "hello world";
1129 char sinit14
[10] = { "abc" };
1130 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
1132 struct { int a
[3], b
; } sinit16
[] = { { 1 }, 2 };
1148 void init_test(void)
1152 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1153 int linit6
[] = { 1, 2, 3 };
1155 char linit8
[] = "hello" "trala";
1156 int linit12
[10] = { 1, 2 };
1157 int linit13
[10] = { 1, 2, [7] = 3, [3] = 4, };
1158 char linit14
[10] = "abc";
1159 int linit15
[10] = { linit1
, linit1
+ 1, [6] = linit1
+ 2, };
1160 struct linit16
{ int a1
, a2
, a3
, a4
; } linit16
= { 1, .a3
= 2 };
1161 int linit17
= sizeof(linit17
);
1163 printf("init_test:\n");
1165 printf("sinit1=%d\n", sinit1
);
1166 printf("sinit2=%d\n", sinit2
);
1167 printf("sinit3=%d %d %d %d\n",
1173 printf("sinit6=%d\n", sizeof(sinit6
));
1174 printf("sinit7=%d %d %d %d\n",
1180 printf("sinit8=%s\n", sinit8
);
1181 printf("sinit9=%d %d %d\n",
1186 printf("sinit10=%d %d %d\n",
1191 printf("sinit11=%d %d %d %d %d %d\n",
1202 printf("[%d][%d] = %d %d %d\n",
1203 i
, j
, sinit4
[i
][j
], sinit5
[i
][j
], linit4
[i
][j
]);
1204 printf("linit1=%d\n", linit1
);
1205 printf("linit2=%d\n", linit2
);
1206 printf("linit6=%d\n", sizeof(linit6
));
1207 printf("linit8=%d %s\n", sizeof(linit8
), linit8
);
1209 printf("sinit12=%s\n", sinit12
);
1210 printf("sinit13=%d %s %s %s\n",
1215 printf("sinit14=%s\n", sinit14
);
1217 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
1219 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
1221 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
1223 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
1225 printf("%d %d %d %d\n",
1230 /* test that initialisation is done after variable declare */
1231 printf("linit17=%d\n", linit17
);
1232 printf("sinit15=%d\n", sinit15
[0]);
1233 printf("sinit16=%d %d\n", sinit16
[0].a
[0], sinit16
[1].a
[0]);
1234 printf("sinit17=%s %d %s %d\n",
1235 sinit17
[0].s
, sinit17
[0].len
,
1236 sinit17
[1].s
, sinit17
[1].len
);
1238 printf("%x ", sinit18
[i
]);
1267 /* ISOC99 _Bool type */
1268 void c99_bool_test(void)
1274 printf("bool_test:\n");
1275 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
1277 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
1279 printf("b = %d\n", b
);
1281 printf("b = %d\n", b
);
1285 void bitfield_test(void)
1295 unsigned int f5
: 7;
1297 printf("bitfield_test:");
1298 printf("sizeof(st1) = %d\n", sizeof(st1
));
1307 printf("%d %d %d %d %d\n",
1308 st1
.f1
, st1
.f2
, st1
.f3
, st1
.f4
, st1
.f5
);
1312 printf("st1.f1 == -1\n");
1314 printf("st1.f1 != -1\n");
1316 printf("st1.f2 == -1\n");
1318 printf("st1.f2 != -1\n");
1321 #define FTEST(prefix, type, fmt)\
1322 void prefix ## cmp(type a, type b)\
1324 printf("%d %d %d %d %d %d\n",\
1331 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1339 printf(fmt "\n", ++a);\
1340 printf(fmt "\n", a++);\
1341 printf(fmt "\n", a);\
1343 void prefix ## fcast(type a)\
1354 printf("ftof: %f %f %Lf\n", fa, da, la);\
1356 ua = (unsigned int)a;\
1357 printf("ftoi: %d %u\n", ia, ua);\
1361 printf("itof: " fmt "\n", b);\
1363 printf("utof: " fmt "\n", b);\
1366 void prefix ## test(void)\
1368 printf("testing '%s'\n", #type);\
1369 prefix ## cmp(1, 2.5);\
1370 prefix ## cmp(2, 1.5);\
1371 prefix ## cmp(1, 1);\
1372 prefix ## fcast(234.6);\
1373 prefix ## fcast(-2334.6);\
1376 FTEST(f
, float, "%f")
1377 FTEST(d
, double, "%f")
1378 FTEST(ld
, long double, "%Lf")
1380 double ftab1
[3] = { 1.2, 3.4, -5.6 };
1383 void float_test(void)
1390 printf("float_test:\n");
1391 printf("sizeof(float) = %d\n", sizeof(float));
1392 printf("sizeof(double) = %d\n", sizeof(double));
1393 printf("sizeof(long double) = %d\n", sizeof(long double));
1397 printf("%f %f %f\n", ftab1
[0], ftab1
[1], ftab1
[2]);
1398 printf("%f %f %f\n", 2.12, .5, 2.3e10
);
1399 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1401 printf("da=%f\n", da
);
1403 printf("fa=%f\n", fa
);
1406 printf("da = %f\n", da
);
1409 printf("db = %f\n", db
);
1417 return fib(n
-1) + fib(n
-2);
1429 printf("funcptr:\n");
1436 /* more complicated pointer computation */
1439 printf("sizeof1 = %d\n", sizeof(funcptr_test
));
1440 printf("sizeof2 = %d\n", sizeof funcptr_test
);
1441 printf("sizeof3 = %d\n", sizeof(&funcptr_test
));
1442 printf("sizeof4 = %d\n", sizeof &funcptr_test
);
1445 void lloptest(long long a
, long long b
)
1447 unsigned long long ua
, ub
;
1452 printf("arith: %Ld %Ld %Ld\n",
1458 printf("arith1: %Ld %Ld\n",
1464 printf("bin: %Ld %Ld %Ld\n",
1470 printf("test: %d %d %d %d %d %d\n",
1478 printf("utest: %d %d %d %d %d %d\n",
1489 printf("arith2: %Ld %Ld\n", a
, b
);
1490 printf("arith2: %Ld %Ld\n", a
++, b
++);
1491 printf("arith2: %Ld %Ld\n", --a
, --b
);
1492 printf("arith2: %Ld %Ld\n", a
, b
);
1495 void llshift(long long a
, int b
)
1497 printf("shift: %Ld %Ld %Ld\n",
1498 (unsigned long long)a
>> b
,
1501 printf("shiftc: %Ld %Ld %Ld\n",
1502 (unsigned long long)a
>> 3,
1505 printf("shiftc: %Ld %Ld %Ld\n",
1506 (unsigned long long)a
>> 35,
1516 long long la
, lb
, lc
;
1517 unsigned long long ula
, ulb
, ulc
;
1520 la
= (la
<< 20) | 0x12345;
1522 printf("la=%Ld ula=%Lu\n", la
, ula
);
1527 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
1532 printf("ftoll: %Ld %Ld %Ld\n", la
, lb
, lc
);
1537 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
1542 printf("ftoull: %Lu %Lu %Lu\n", ula
, ulb
, ulc
);
1545 long long llfunc1(int a
)
1555 long long int value(struct S
*v
)
1557 return ((long long int)v
->item
);
1560 void longlong_test(void)
1565 printf("longlong_test:\n");
1566 printf("sizeof(long long) = %d\n", sizeof(long long));
1571 printf("%Ld %Ld\n", a
, b
);
1572 printf("%Ld %Ld %Ld %Lx\n",
1576 0x1234567812345679);
1581 lloptest(0xff, 0x1234);
1582 b
= 0x72345678 << 10;
1586 b
= 0x72345678LL
<< 10;
1597 /* long long reg spill test */
1602 printf("%lld\n", value(&a
));
1604 lloptest(0x80000000, 0);
1606 /* another long long spill test */
1612 printf("%lld\n", *p
);
1616 void vprintf1(const char *fmt
, ...)
1638 i
= va_arg(ap
, int);
1642 d
= va_arg(ap
, double);
1646 ll
= va_arg(ap
, long long);
1660 void stdarg_test(void)
1662 vprintf1("%d %d %d\n", 1, 2, 3);
1663 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1664 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1667 void whitespace_test(void)
1673 ntf("whitspace:\n");\f\v
1677 #ifdef CORRECT_CR_HANDLING
1686 #ifdef ACCEPT_CR_IN_STRINGS
1687 printf("len1=%d\n", strlen("
1689 #ifdef CORRECT_CR_HANDLING
1692 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
1694 printf("len1=%d\n", strlen("
a
1696 #endif /* ACCEPT_CR_IN_STRINGS */
1699 int reltab
[3] = { 1, 2, 3 };
1701 int *rel1
= &reltab
[1];
1702 int *rel2
= &reltab
[2];
1704 void relocation_test(void)
1706 printf("*rel1=%d\n", *rel1
);
1707 printf("*rel2=%d\n", *rel2
);
1710 void old_style_f(a
,b
,c
)
1714 printf("a=%d b=%d b=%f\n", a
, b
, c
);
1717 void decl_func1(int cmpfn())
1719 printf("cmpfn=%lx\n", (long)cmpfn
);
1722 void decl_func2(cmpfn
)
1725 printf("cmpfn=%lx\n", (long)cmpfn
);
1728 void old_style_function(void)
1730 old_style_f((void *)1, 2, 3.0);
1735 void sizeof_test(void)
1740 printf("sizeof(int) = %d\n", sizeof(int));
1741 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1742 printf("sizeof(short) = %d\n", sizeof(short));
1743 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1744 printf("sizeof(char) = %d\n", sizeof(char));
1745 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1746 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1748 printf("sizeof(a++) = %d\n", sizeof a
++);
1749 printf("a=%d\n", a
);
1751 printf("sizeof(**ptr) = %d\n", sizeof (**ptr
));
1753 /* some alignof tests */
1754 printf("__alignof__(int) = %d\n", __alignof__(int));
1755 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1756 printf("__alignof__(short) = %d\n", __alignof__(short));
1757 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1758 printf("__alignof__(char) = %d\n", __alignof__(char));
1759 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1760 printf("__alignof__(func) = %d\n", __alignof__
sizeof_test());
1763 void typeof_test(void)
1772 printf("a=%f b=%f c=%f\n", a
, b
, c
);
1775 void statement_expr_test(void)
1788 printf("a=%d\n", a
);
1792 void local_label_test(void)
1798 __label__ l1
, l2
, l3
;
1812 printf("a=%d\n", a
);
1822 /* inline assembler test */
1825 /* from linux kernel */
1826 static char * strncat1(char * dest
,const char * src
,size_t count
)
1829 __asm__
__volatile__(
1838 "testb %%al,%%al\n\t"
1840 "2:\txorl %2,%2\n\t"
1842 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
1843 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
1848 static inline void * memcpy1(void * to
, const void * from
, size_t n
)
1851 __asm__
__volatile__(
1856 "1:\ttestb $1,%b4\n\t"
1860 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
1861 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
1866 static __inline__
void sigaddset1(unsigned int *set
, int _sig
)
1868 __asm__("btsl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
1871 static __inline__
void sigdelset1(unsigned int *set
, int _sig
)
1873 asm("btrl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
1876 static __inline__ __const__
unsigned int swab32(unsigned int x
)
1878 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
1879 "rorl $16,%0\n\t" /* swap words */
1880 "xchgb %b0,%h0" /* swap higher bytes */
1886 static __inline__
unsigned long long mul64(unsigned int a
, unsigned int b
)
1888 unsigned long long res
;
1889 __asm__("mull %2" : "=A" (res
) : "a" (a
), "r" (b
));
1893 static __inline__
unsigned long long inc64(unsigned long long a
)
1895 unsigned long long res
;
1896 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res
) : "A" (a
));
1907 printf("inline asm:\n");
1908 /* test the no operand case */
1909 asm volatile ("xorl %eax, %eax");
1911 memcpy1(buf
, "hello", 6);
1912 strncat1(buf
, " worldXXXXX", 3);
1913 printf("%s\n", buf
);
1915 /* 'A' constraint test */
1916 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
1917 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
1920 sigdelset1(&set
, 2);
1921 sigaddset1(&set
, 16);
1922 /* NOTE: we test here if C labels are correctly restored after the
1926 __asm__("btsl %1,%0" : "=m"(set
) : "Ir"(20) : "cc");
1927 printf("set=0x%x\n", set
);
1929 printf("swab32(0x%08x) = 0x%0x\n", val
, swab32(val
));
1943 #define COMPAT_TYPE(type1, type2) \
1945 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
1946 __builtin_types_compatible_p (type1, type2));\
1951 void builtin_test(void)
1954 COMPAT_TYPE(int, int);
1955 COMPAT_TYPE(int, unsigned int);
1956 COMPAT_TYPE(int, char);
1957 COMPAT_TYPE(int, const int);
1958 COMPAT_TYPE(int, volatile int);
1959 COMPAT_TYPE(int *, int *);
1960 COMPAT_TYPE(int *, void *);
1961 COMPAT_TYPE(int *, const int *);
1962 COMPAT_TYPE(char *, unsigned char *);
1963 /* space is needed because tcc preprocessor introduces a space between each token */
1964 COMPAT_TYPE(char * *, void *);
1966 printf("res = %d\n", __builtin_constant_p(1));
1967 printf("res = %d\n", __builtin_constant_p(1 + 2));
1968 printf("res = %d\n", __builtin_constant_p(&constant_p_var
));
1969 printf("res = %d\n", __builtin_constant_p(constant_p_var
));
1973 void const_func(const int a
)
1977 void const_warn_test(void)