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 #define spin_lock(lock) do { } while (0)
136 #define wq_spin_lock spin_lock
137 #define TEST2() wq_spin_lock(a)
139 void macro_test(void)
141 printf("macro:\n");\f\v
143 printf("aaa=%d\n", AAA
);
145 printf("min=%d\n", min(1, min(2, -1)));
147 printf("s1=%s\n", glue(HIGH
, LOW
));
148 printf("s2=%s\n", xglue(HIGH
, LOW
));
149 printf("s3=%s\n", str("c"));
150 printf("s4=%s\n", str(a1
));
151 printf("B3=%d\n", B3
);
154 printf("A defined\n");
157 printf("B defined\n");
160 printf("A defined\n");
162 printf("A not defined\n");
165 printf("B defined\n");
167 printf("B not defined\n");
171 printf("A defined\n");
173 printf("B1 defined\n");
175 printf("B1 not defined\n");
178 printf("A not defined\n");
180 printf("B2 defined\n");
182 printf("B2 not defined\n");
187 printf("test true1\n");
190 printf("test true2\n");
193 printf("test true3\n");
196 printf("test trueA\n");
199 printf("test trueB\n");
215 printf("__LINE__ defined\n");
218 printf("__LINE__=%d __FILE__=%s\n",
221 printf("__LINE__=%d __FILE__=%s\n",
224 printf("__LINE__=%d __FILE__=%s\n",
226 #line 227 "tcctest.c"
228 /* not strictly preprocessor, but we test it there */
230 printf("__func__ = %s\n", __func__
);
231 dprintf(1, "vaarg=%d\n", 1);
233 dprintf1(1, "vaarg1\n");
234 dprintf1(1, "vaarg1=%d\n", 2);
235 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
238 printf("func='%s'\n", __FUNCTION__
);
240 /* complicated macros in glibc */
241 printf("INT64_MIN=%Ld\n", INT64_MIN
);
251 /* macro function with argument outside the macro string */
252 #define MF_s MF_hello
253 #define MF_hello(msg) printf("%s\n",msg)
255 #define MF_t printf("tralala\n"); MF_hello
260 /* test macro substituion inside args (should not eat stream) */
261 printf("qq=%d\n", qq(qq
)(2));
263 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
264 null argument without a space. gcc 3.2 fixes that. */
267 printf("qq1=%d\n", qq1( ));
269 /* comment with stray handling *\
271 /* this is a valid *\/ comment */
272 /* this is a valid comment *\*/
276 /* test function macro substitution when the function name is
295 void ps(const char *s
)
307 const char foo1_string
[] = "\
316 printf("\141\1423\143\n");/* dezdez test */
317 printf("\x41\x42\x43\x3a\n");
318 printf("c=%c\n", 'r');
319 printf("wc=%C 0x%lx %C\n", L
'a', L
'\x1234', L
'c');
320 printf("foo1_string='%s'\n", foo1_string
);
322 printf("wstring=%S\n", L
"abc");
323 printf("wstring=%S\n", L
"abc" L
"def" "ghi");
324 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
325 printf("L'\\377'=%d L'\\xff'=%d\n", L
'\377', L
'\xff');
329 while ((b
= b
+ 1) < 96) {
333 printf("fib=%d\n", fib(33));
335 while (b
!= 0x80000000) {
348 for(i
= 0; i
< 10;i
++)
357 /* break/continue tests */
369 /* break/continue tests */
381 for(i
= 0;i
< 10;i
++) {
393 static void *label_table
[3] = { &&label1
, &&label2
, &&label3
};
406 /* we also test computed gotos (GCC extension) */
408 goto *label_table
[i
];
436 printf("enum:\n%d %d %d %d %d %d\n",
437 E0
, E1
, E2
, E3
, E4
, E5
);
439 printf("b1=%d\n", b1
);
455 printf("typedef:\n");
456 printf("a=%d\n", *a
);
458 printf("mytype2=%d\n", mytype2
);
463 printf("forward:\n");
469 void forward_ref(void)
471 printf("forward ok\n");
474 typedef struct struct1
{
494 struct struct1 st1
, st2
;
496 int main(int argc
, char **argv
)
514 constant_expr_test();
518 compound_literal_test();
520 struct_assign_test();
529 old_style_function();
532 statement_expr_test();
546 printf("g1=%d\n", g
);
554 printf("g2=%d\n", g
);
558 printf("g3=%d\n", g
);
562 printf("g4=%d\n", g
);
565 printf("g5=%d\n", g
);
568 void array_test(int a
[4])
573 printf("sizeof(a) = %d\n", sizeof(a
));
574 printf("sizeof(\"a\") = %d\n", sizeof("a"));
576 printf("sizeof(__func__) = %d\n", sizeof(__func__
));
578 printf("sizeof tab %d\n", sizeof(tab
));
579 printf("sizeof tab2 %d\n", sizeof tab2
);
583 printf("%d %d %d\n", tab
[0], tab
[1], tab
[2]);
586 tab2
[i
][j
] = 10 * i
+ j
;
588 printf(" %3d", ((int *)tab2
)[i
]);
597 printf("%d\n", a
+= 1);
598 printf("%d\n", a
-= 2);
599 printf("%d\n", a
*= 31232132);
600 printf("%d\n", a
/= 4);
601 printf("%d\n", a
%= 20);
602 printf("%d\n", a
&= 6);
603 printf("%d\n", a
^= 7);
604 printf("%d\n", a
|= 8);
605 printf("%d\n", a
>>= 3);
606 printf("%d\n", a
<<= 4);
610 printf("%d\n", a
+ 1);
611 printf("%d\n", a
- 2);
612 printf("%d\n", a
* 312);
613 printf("%d\n", a
/ 4);
614 printf("%d\n", b
/ 4);
615 printf("%d\n", (unsigned)b
/ 4);
616 printf("%d\n", a
% 20);
617 printf("%d\n", b
% 20);
618 printf("%d\n", (unsigned)b
% 20);
619 printf("%d\n", a
& 6);
620 printf("%d\n", a
^ 7);
621 printf("%d\n", a
| 8);
622 printf("%d\n", a
>> 3);
623 printf("%d\n", b
>> 3);
624 printf("%d\n", (unsigned)b
>> 3);
625 printf("%d\n", a
<< 4);
630 printf("%d\n", 12 + 1);
631 printf("%d\n", 12 - 2);
632 printf("%d\n", 12 * 312);
633 printf("%d\n", 12 / 4);
634 printf("%d\n", 12 % 20);
635 printf("%d\n", 12 & 6);
636 printf("%d\n", 12 ^ 7);
637 printf("%d\n", 12 | 8);
638 printf("%d\n", 12 >> 2);
639 printf("%d\n", 12 << 4);
643 printf("%d %d %d %d\n",
652 return (c
>= 'a' & c
<= 'z') | (c
>= 'A' & c
<= 'Z') | c
== '_';
655 /**********************/
657 int vstack
[10], *vstack_ptr
;
659 void vpush(int vt
, int vc
)
665 void vpop(int *ft
, int *fc
)
678 vstack_ptr
[-2] &= ~0xffffff80;
680 printf("res= %d %d\n", a
, b
);
683 void constant_expr_test()
686 printf("constant_expr:\n");
688 printf("%d\n", a
* 16);
689 printf("%d\n", a
* 1);
690 printf("%d\n", a
+ 0);
699 printf("expr_ptr:\n");
702 printf("diff=%d\n", q
- p
);
704 printf("inc=%d\n", p
- tab4
);
706 printf("dec=%d\n", p
- tab4
);
708 printf("inc=%d\n", p
- tab4
);
710 printf("dec=%d\n", p
- tab4
);
711 printf("add=%d\n", p
+ 3 - tab4
);
712 printf("add=%d\n", 3 + p
- tab4
);
718 printf("constant_expr:\n");
721 printf("%d\n", a
== a
);
722 printf("%d\n", a
!= a
);
724 printf("%d\n", a
< b
);
725 printf("%d\n", a
<= b
);
726 printf("%d\n", a
<= a
);
727 printf("%d\n", b
>= a
);
728 printf("%d\n", a
>= a
);
729 printf("%d\n", b
> a
);
731 printf("%d\n", (unsigned)a
< b
);
732 printf("%d\n", (unsigned)a
<= b
);
733 printf("%d\n", (unsigned)a
<= a
);
734 printf("%d\n", (unsigned)b
>= a
);
735 printf("%d\n", (unsigned)a
>= a
);
736 printf("%d\n", (unsigned)b
> a
);
765 printf("sizes: %d %d %d %d\n",
766 sizeof(struct struct1
),
767 sizeof(struct struct2
),
768 sizeof(union union1
),
769 sizeof(union union2
));
773 printf("st1: %d %d %d\n",
774 st1
.f1
, st1
.f2
, st1
.f3
);
777 printf("union1: %d\n", st1
.u
.v1
);
780 printf("union2: %d\n", u
.w1
);
785 printf("st2: %d %d %d\n",
786 s
->f1
, s
->f2
, s
->f3
);
787 printf("str_addr=%x\n", (int)st1
.str
- (int)&st1
.f1
);
789 /* align / size tests */
790 printf("aligntest1 sizeof=%d alignof=%d\n",
791 sizeof(struct aligntest1
), __alignof__(struct aligntest1
));
792 printf("aligntest2 sizeof=%d alignof=%d\n",
793 sizeof(struct aligntest2
), __alignof__(struct aligntest2
));
794 printf("aligntest3 sizeof=%d alignof=%d\n",
795 sizeof(struct aligntest3
), __alignof__(struct aligntest3
));
796 printf("aligntest4 sizeof=%d alignof=%d\n",
797 sizeof(struct aligntest4
), __alignof__(struct aligntest4
));
799 /* empty structures (GCC extension) */
800 printf("sizeof(struct empty) = %d\n", sizeof(struct empty
));
801 printf("alignof(struct empty) = %d\n", __alignof__(struct empty
));
804 /* XXX: depend on endianness */
805 void char_short_test()
809 printf("char_short:\n");
814 *(char *)&var1
, *(char *)&var2
);
816 *(unsigned char *)&var1
, *(unsigned char *)&var2
);
817 printf("s16=%d %d\n",
818 *(short *)&var1
, *(short *)&var2
);
819 printf("u16=%d %d\n",
820 *(unsigned short *)&var1
, *(unsigned short *)&var2
);
821 printf("s32=%d %d\n",
822 *(int *)&var1
, *(int *)&var2
);
823 printf("u32=%d %d\n",
824 *(unsigned int *)&var1
, *(unsigned int *)&var2
);
825 *(char *)&var1
= 0x08;
826 printf("var1=%x\n", var1
);
827 *(short *)&var1
= 0x0809;
828 printf("var1=%x\n", var1
);
829 *(int *)&var1
= 0x08090a0b;
830 printf("var1=%x\n", var1
);
843 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
844 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
846 static int toupper1(int a
)
853 int *s
, a
, b
, t
, f
, i
;
857 printf("!s=%d\n", !s
);
863 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
864 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
865 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
880 printf("b=%d\n", a
+ (0 ? 1 : a
/ 2));
882 /* test register spilling */
885 a
= (a
+ b
) * ((a
< b
) ?
886 ((b
- a
) * (a
- b
)): a
+ b
);
889 /* test complex || or && expressions */
893 printf("exp=%d\n", f
== (32 <= a
&& a
<= 3));
894 printf("r=%d\n", (t
|| f
) + (t
&& f
));
899 int aspect_native
= 65536;
900 double bfu_aspect
= 1.0;
902 for(aspect_on
= 0; aspect_on
< 2; aspect_on
++) {
903 aspect
=aspect_on
?(aspect_native
*bfu_aspect
+0.5):65535UL;
904 printf("aspect=%d\n", aspect
);
908 /* test ? : GCC extension */
910 static int v1
= 34 ? : -1; /* constant case */
911 static int v2
= 0 ? : -1; /* constant case */
914 printf("%d %d\n", v1
, v2
);
915 printf("%d %d\n", a
- 30 ? : a
* 2, a
+ 1 ? : a
* 2);
918 /* again complex expression */
920 if (toupper1 (i
) != TOUPPER (i
))
921 printf("error %d\n", i
);
925 /* GCC accepts that */
926 static int tab_reinit
[];
927 static int tab_reinit
[10];
929 //int cinit1; /* a global variable can be defined several times without error ! */
933 int *cinit2
= (int []){3, 2, 1};
935 void compound_literal_test(void)
940 printf("compound_test:\n");
942 p
= (int []){1, 2, 3};
948 printf("%d", cinit2
[i
]);
952 printf("q1=%s\n", q
);
954 q
= (char *){ "tralala2" };
955 printf("q2=%s\n", q
);
958 printf("q3=%s\n", q3
);
960 q
= (char []){ "tralala3" };
961 printf("q4=%s\n", q
);
964 p
= (int []){1, 2, cinit1
+ 3};
970 p
= (int []){1, 2, 4 + i
};
993 printf("kr_test:\n");
994 printf("func1=%d\n", kr_func1(3, 4));
995 printf("func2=%d\n", kr_func2(3, 4));
1002 tab
= (char*)malloc(20);
1018 /* structure assignment tests */
1024 struct structa1 ssta1
;
1026 void struct_assign_test1(struct structa1 s1
, int t
)
1028 printf("%d %d %d\n", s1
.f1
, s1
.f2
, t
);
1031 struct structa1
struct_assign_test2(struct structa1 s1
, int t
)
1038 void struct_assign_test(void)
1040 struct structa1 lsta1
, lsta2
;
1043 printf("struct_assign_test:\n");
1047 printf("%d %d\n", lsta1
.f1
, lsta1
.f2
);
1049 printf("%d %d\n", lsta2
.f1
, lsta2
.f2
);
1054 struct_assign_test1(lsta2
, 3);
1056 printf("before call: %d %d\n", lsta2
.f1
, lsta2
.f2
);
1057 lsta2
= struct_assign_test2(lsta2
, 4);
1058 printf("after call: %d %d\n", lsta2
.f1
, lsta2
.f2
);
1061 /* casts to short/char */
1063 void cast1(char a
, short b
, unsigned char c
, unsigned short d
)
1065 printf("%d %d %d %d\n", a
, b
, c
, d
);
1079 printf("cast_test:\n");
1083 printf("%d %d %d %d\n",
1086 (unsigned char)(a
+ 1),
1087 (unsigned short)(a
+ 1));
1088 printf("%d %d %d %d\n",
1091 (unsigned char)0xfffff,
1092 (unsigned short)0xfffff);
1094 a
= (bcast
= 128) + 1;
1096 a
= (scast
= 65536) + 1;
1099 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
1101 /* test cast from unsigned to signed short to int */
1104 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b
, d
);
1107 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b
, d
);
1109 /* test implicit int casting for array accesses */
1113 printf("%d %d\n", tab
[0], tab
[1]);
1115 /* test implicit casting on some operators */
1116 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1117 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1118 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1121 /* initializers tests */
1122 struct structinit1
{
1131 int sinit3
[3] = { 1, 2, {{3}}, };
1132 int sinit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1133 int sinit5
[3][2] = { 1, 2, 3, 4, 5, 6 };
1134 int sinit6
[] = { 1, 2, 3 };
1135 int sinit7
[] = { [2] = 3, [0] = 1, 2 };
1136 char sinit8
[] = "hello" "trala";
1138 struct structinit1 sinit9
= { 1, 2, 3 };
1139 struct structinit1 sinit10
= { .f2
= 2, 3, .f1
= 1 };
1140 struct structinit1 sinit11
= { .f2
= 2, 3, .f1
= 1,
1148 char *sinit12
= "hello world";
1154 char sinit14
[10] = { "abc" };
1155 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
1157 struct { int a
[3], b
; } sinit16
[] = { { 1 }, 2 };
1173 void init_test(void)
1177 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1178 int linit6
[] = { 1, 2, 3 };
1180 char linit8
[] = "hello" "trala";
1181 int linit12
[10] = { 1, 2 };
1182 int linit13
[10] = { 1, 2, [7] = 3, [3] = 4, };
1183 char linit14
[10] = "abc";
1184 int linit15
[10] = { linit1
, linit1
+ 1, [6] = linit1
+ 2, };
1185 struct linit16
{ int a1
, a2
, a3
, a4
; } linit16
= { 1, .a3
= 2 };
1186 int linit17
= sizeof(linit17
);
1188 printf("init_test:\n");
1190 printf("sinit1=%d\n", sinit1
);
1191 printf("sinit2=%d\n", sinit2
);
1192 printf("sinit3=%d %d %d %d\n",
1198 printf("sinit6=%d\n", sizeof(sinit6
));
1199 printf("sinit7=%d %d %d %d\n",
1205 printf("sinit8=%s\n", sinit8
);
1206 printf("sinit9=%d %d %d\n",
1211 printf("sinit10=%d %d %d\n",
1216 printf("sinit11=%d %d %d %d %d %d\n",
1227 printf("[%d][%d] = %d %d %d\n",
1228 i
, j
, sinit4
[i
][j
], sinit5
[i
][j
], linit4
[i
][j
]);
1229 printf("linit1=%d\n", linit1
);
1230 printf("linit2=%d\n", linit2
);
1231 printf("linit6=%d\n", sizeof(linit6
));
1232 printf("linit8=%d %s\n", sizeof(linit8
), linit8
);
1234 printf("sinit12=%s\n", sinit12
);
1235 printf("sinit13=%d %s %s %s\n",
1240 printf("sinit14=%s\n", sinit14
);
1242 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
1244 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
1246 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
1248 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
1250 printf("%d %d %d %d\n",
1255 /* test that initialisation is done after variable declare */
1256 printf("linit17=%d\n", linit17
);
1257 printf("sinit15=%d\n", sinit15
[0]);
1258 printf("sinit16=%d %d\n", sinit16
[0].a
[0], sinit16
[1].a
[0]);
1259 printf("sinit17=%s %d %s %d\n",
1260 sinit17
[0].s
, sinit17
[0].len
,
1261 sinit17
[1].s
, sinit17
[1].len
);
1263 printf("%x ", sinit18
[i
]);
1292 /* ISOC99 _Bool type */
1293 void c99_bool_test(void)
1299 printf("bool_test:\n");
1300 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
1302 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
1304 printf("b = %d\n", b
);
1306 printf("b = %d\n", b
);
1310 void bitfield_test(void)
1320 unsigned int f5
: 7;
1322 printf("bitfield_test:");
1323 printf("sizeof(st1) = %d\n", sizeof(st1
));
1332 printf("%d %d %d %d %d\n",
1333 st1
.f1
, st1
.f2
, st1
.f3
, st1
.f4
, st1
.f5
);
1337 printf("st1.f1 == -1\n");
1339 printf("st1.f1 != -1\n");
1341 printf("st1.f2 == -1\n");
1343 printf("st1.f2 != -1\n");
1346 #define FTEST(prefix, type, fmt)\
1347 void prefix ## cmp(type a, type b)\
1349 printf("%d %d %d %d %d %d\n",\
1356 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1364 printf(fmt "\n", ++a);\
1365 printf(fmt "\n", a++);\
1366 printf(fmt "\n", a);\
1368 void prefix ## fcast(type a)\
1379 printf("ftof: %f %f %Lf\n", fa, da, la);\
1381 ua = (unsigned int)a;\
1382 printf("ftoi: %d %u\n", ia, ua);\
1386 printf("itof: " fmt "\n", b);\
1388 printf("utof: " fmt "\n", b);\
1391 void prefix ## test(void)\
1393 printf("testing '%s'\n", #type);\
1394 prefix ## cmp(1, 2.5);\
1395 prefix ## cmp(2, 1.5);\
1396 prefix ## cmp(1, 1);\
1397 prefix ## fcast(234.6);\
1398 prefix ## fcast(-2334.6);\
1401 FTEST(f
, float, "%f")
1402 FTEST(d
, double, "%f")
1403 FTEST(ld
, long double, "%Lf")
1405 double ftab1
[3] = { 1.2, 3.4, -5.6 };
1408 void float_test(void)
1415 printf("float_test:\n");
1416 printf("sizeof(float) = %d\n", sizeof(float));
1417 printf("sizeof(double) = %d\n", sizeof(double));
1418 printf("sizeof(long double) = %d\n", sizeof(long double));
1422 printf("%f %f %f\n", ftab1
[0], ftab1
[1], ftab1
[2]);
1423 printf("%f %f %f\n", 2.12, .5, 2.3e10
);
1424 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1426 printf("da=%f\n", da
);
1428 printf("fa=%f\n", fa
);
1431 printf("da = %f\n", da
);
1434 printf("db = %f\n", db
);
1442 return fib(n
-1) + fib(n
-2);
1454 printf("funcptr:\n");
1461 /* more complicated pointer computation */
1464 printf("sizeof1 = %d\n", sizeof(funcptr_test
));
1465 printf("sizeof2 = %d\n", sizeof funcptr_test
);
1466 printf("sizeof3 = %d\n", sizeof(&funcptr_test
));
1467 printf("sizeof4 = %d\n", sizeof &funcptr_test
);
1470 void lloptest(long long a
, long long b
)
1472 unsigned long long ua
, ub
;
1477 printf("arith: %Ld %Ld %Ld\n",
1483 printf("arith1: %Ld %Ld\n",
1489 printf("bin: %Ld %Ld %Ld\n",
1495 printf("test: %d %d %d %d %d %d\n",
1503 printf("utest: %d %d %d %d %d %d\n",
1514 printf("arith2: %Ld %Ld\n", a
, b
);
1515 printf("arith2: %Ld %Ld\n", a
++, b
++);
1516 printf("arith2: %Ld %Ld\n", --a
, --b
);
1517 printf("arith2: %Ld %Ld\n", a
, b
);
1520 void llshift(long long a
, int b
)
1522 printf("shift: %Ld %Ld %Ld\n",
1523 (unsigned long long)a
>> b
,
1526 printf("shiftc: %Ld %Ld %Ld\n",
1527 (unsigned long long)a
>> 3,
1530 printf("shiftc: %Ld %Ld %Ld\n",
1531 (unsigned long long)a
>> 35,
1541 long long la
, lb
, lc
;
1542 unsigned long long ula
, ulb
, ulc
;
1545 la
= (la
<< 20) | 0x12345;
1547 printf("la=%Ld ula=%Lu\n", la
, ula
);
1552 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
1557 printf("ftoll: %Ld %Ld %Ld\n", la
, lb
, lc
);
1562 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
1567 printf("ftoull: %Lu %Lu %Lu\n", ula
, ulb
, ulc
);
1570 long long llfunc1(int a
)
1580 long long int value(struct S
*v
)
1582 return ((long long int)v
->item
);
1585 void longlong_test(void)
1590 printf("longlong_test:\n");
1591 printf("sizeof(long long) = %d\n", sizeof(long long));
1596 printf("%Ld %Ld\n", a
, b
);
1597 printf("%Ld %Ld %Ld %Lx\n",
1601 0x1234567812345679);
1606 lloptest(0xff, 0x1234);
1607 b
= 0x72345678 << 10;
1611 b
= 0x72345678LL
<< 10;
1622 /* long long reg spill test */
1627 printf("%lld\n", value(&a
));
1629 lloptest(0x80000000, 0);
1631 /* another long long spill test */
1637 printf("%lld\n", *p
);
1641 void vprintf1(const char *fmt
, ...)
1663 i
= va_arg(ap
, int);
1667 d
= va_arg(ap
, double);
1671 ll
= va_arg(ap
, long long);
1685 void stdarg_test(void)
1687 vprintf1("%d %d %d\n", 1, 2, 3);
1688 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1689 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1692 void whitespace_test(void)
1698 ntf("whitspace:\n");\f\v
1702 #ifdef CORRECT_CR_HANDLING
1711 #ifdef ACCEPT_CR_IN_STRINGS
1712 printf("len1=%d\n", strlen("
1714 #ifdef CORRECT_CR_HANDLING
1717 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
1719 printf("len1=%d\n", strlen("
a
1721 #endif /* ACCEPT_CR_IN_STRINGS */
1724 int reltab
[3] = { 1, 2, 3 };
1726 int *rel1
= &reltab
[1];
1727 int *rel2
= &reltab
[2];
1729 void relocation_test(void)
1731 printf("*rel1=%d\n", *rel1
);
1732 printf("*rel2=%d\n", *rel2
);
1735 void old_style_f(a
,b
,c
)
1739 printf("a=%d b=%d b=%f\n", a
, b
, c
);
1742 void decl_func1(int cmpfn())
1744 printf("cmpfn=%lx\n", (long)cmpfn
);
1747 void decl_func2(cmpfn
)
1750 printf("cmpfn=%lx\n", (long)cmpfn
);
1753 void old_style_function(void)
1755 old_style_f((void *)1, 2, 3.0);
1760 void sizeof_test(void)
1765 printf("sizeof(int) = %d\n", sizeof(int));
1766 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1767 printf("sizeof(short) = %d\n", sizeof(short));
1768 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1769 printf("sizeof(char) = %d\n", sizeof(char));
1770 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1771 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1773 printf("sizeof(a++) = %d\n", sizeof a
++);
1774 printf("a=%d\n", a
);
1776 printf("sizeof(**ptr) = %d\n", sizeof (**ptr
));
1778 /* some alignof tests */
1779 printf("__alignof__(int) = %d\n", __alignof__(int));
1780 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1781 printf("__alignof__(short) = %d\n", __alignof__(short));
1782 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1783 printf("__alignof__(char) = %d\n", __alignof__(char));
1784 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1785 printf("__alignof__(func) = %d\n", __alignof__
sizeof_test());
1788 void typeof_test(void)
1797 printf("a=%f b=%f c=%f\n", a
, b
, c
);
1800 void statement_expr_test(void)
1813 printf("a=%d\n", a
);
1817 void local_label_test(void)
1823 __label__ l1
, l2
, l3
, l4
;
1837 printf("a=%d\n", a
);
1847 /* inline assembler test */
1850 /* from linux kernel */
1851 static char * strncat1(char * dest
,const char * src
,size_t count
)
1854 __asm__
__volatile__(
1863 "testb %%al,%%al\n\t"
1865 "2:\txorl %2,%2\n\t"
1867 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
1868 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
1873 static inline void * memcpy1(void * to
, const void * from
, size_t n
)
1876 __asm__
__volatile__(
1881 "1:\ttestb $1,%b4\n\t"
1885 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
1886 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
1891 static __inline__
void sigaddset1(unsigned int *set
, int _sig
)
1893 __asm__("btsl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
1896 static __inline__
void sigdelset1(unsigned int *set
, int _sig
)
1898 asm("btrl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
1901 static __inline__ __const__
unsigned int swab32(unsigned int x
)
1903 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
1904 "rorl $16,%0\n\t" /* swap words */
1905 "xchgb %b0,%h0" /* swap higher bytes */
1911 static __inline__
unsigned long long mul64(unsigned int a
, unsigned int b
)
1913 unsigned long long res
;
1914 __asm__("mull %2" : "=A" (res
) : "a" (a
), "r" (b
));
1918 static __inline__
unsigned long long inc64(unsigned long long a
)
1920 unsigned long long res
;
1921 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res
) : "A" (a
));
1932 printf("inline asm:\n");
1933 /* test the no operand case */
1934 asm volatile ("xorl %eax, %eax");
1936 memcpy1(buf
, "hello", 6);
1937 strncat1(buf
, " worldXXXXX", 3);
1938 printf("%s\n", buf
);
1940 /* 'A' constraint test */
1941 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
1942 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
1945 sigdelset1(&set
, 2);
1946 sigaddset1(&set
, 16);
1947 /* NOTE: we test here if C labels are correctly restored after the
1951 __asm__("btsl %1,%0" : "=m"(set
) : "Ir"(20) : "cc");
1952 printf("set=0x%x\n", set
);
1954 printf("swab32(0x%08x) = 0x%0x\n", val
, swab32(val
));
1968 #define COMPAT_TYPE(type1, type2) \
1970 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
1971 __builtin_types_compatible_p (type1, type2));\
1976 void builtin_test(void)
1979 COMPAT_TYPE(int, int);
1980 COMPAT_TYPE(int, unsigned int);
1981 COMPAT_TYPE(int, char);
1982 COMPAT_TYPE(int, const int);
1983 COMPAT_TYPE(int, volatile int);
1984 COMPAT_TYPE(int *, int *);
1985 COMPAT_TYPE(int *, void *);
1986 COMPAT_TYPE(int *, const int *);
1987 COMPAT_TYPE(char *, unsigned char *);
1988 /* space is needed because tcc preprocessor introduces a space between each token */
1989 COMPAT_TYPE(char * *, void *);
1991 printf("res = %d\n", __builtin_constant_p(1));
1992 printf("res = %d\n", __builtin_constant_p(1 + 2));
1993 printf("res = %d\n", __builtin_constant_p(&constant_p_var
));
1994 printf("res = %d\n", __builtin_constant_p(constant_p_var
));
1998 void const_func(const int a
)
2002 void const_warn_test(void)