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
20 #define LONG_LONG_FORMAT "%lld"
21 #define ULONG_LONG_FORMAT "%llu"
23 #define LONG_LONG_FORMAT "%Ld"
24 #define ULONG_LONG_FORMAT "%Lu"
27 // MinGW has 80-bit rather than 64-bit long double which isn't compatible with TCC or MSVC
28 #if defined(_WIN32) && defined(__GNUC__)
29 #define LONG_DOUBLE double
30 #define LONG_DOUBLE_LITERAL(x) x
32 #define LONG_DOUBLE long double
33 #define LONG_DOUBLE_LITERAL(x) x ## L
36 /* deprecated and no longer supported in gcc 3.3 */
37 //#define ACCEPT_CR_IN_STRINGS
39 /* __VA_ARGS__ and __func__ support */
42 /* test various include syntaxes */
44 #define TCCLIB_INC <tcclib.h>
45 #define TCCLIB_INC1 <tcclib
46 #define TCCLIB_INC2 h>
47 #define TCCLIB_INC3 "tcclib"
51 #include TCCLIB_INC1.TCCLIB_INC2
53 #include TCCLIB_INC1.h>
55 /* gcc 3.2 does not accept that (bug ?) */
56 //#include TCCLIB_INC3 ".h"
66 void recursive_macro_test();
80 void constant_expr_test();
82 void char_short_test();
84 void compound_literal_test(void);
86 void struct_assign_test(void);
88 void bitfield_test(void);
89 void c99_bool_test(void);
90 void float_test(void);
91 void longlong_test(void);
92 void manyarg_test(void);
93 void stdarg_test(void);
94 void whitespace_test(void);
95 void relocation_test(void);
96 void old_style_function(void);
97 void alloca_test(void);
98 void c99_vla_test(int size1
, int size2
);
99 void sizeof_test(void);
100 void typeof_test(void);
101 void local_label_test(void);
102 void statement_expr_test(void);
104 void builtin_test(void);
105 void weak_test(void);
106 void global_data_test(void);
107 void cmp_comparison_test(void);
108 void math_cmp_test(void);
109 void callsave_test(void);
110 void builtin_frame_address_test(void);
114 void forward_ref(void);
120 #define M1(a, b) (a) + (b)
124 #define glue(a, b) a ## b
125 #define xglue(a, b) glue(a, b)
126 #define HIGHLOW "hello"
127 #define LOW LOW ", world"
129 static int onetwothree
= 123;
130 #define onetwothree4 onetwothree
131 #define onetwothree xglue(onetwothree,4)
133 #define min(a, b) ((a) < (b) ? (a) : (b))
136 #define dprintf(level,...) printf(__VA_ARGS__)
139 /* gcc vararg macros */
140 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
142 #define MACRO_NOARGS()
158 #define __INT64_C(c) c ## LL
159 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
167 #define spin_lock(lock) do { } while (0)
168 #define wq_spin_lock spin_lock
169 #define TEST2() wq_spin_lock(a)
171 #define UINT_MAX ((unsigned) -1)
173 void intdiv_test(void)
175 printf("18/21=%u\n", 18/21);
176 printf("18%21=%u\n", 18%21);
177 printf("41/21=%u\n", 41/21);
178 printf("41%21=%u\n", 41%21);
179 printf("42/21=%u\n", 42/21);
180 printf("42%21=%u\n", 42%21);
181 printf("43/21=%u\n", 43/21);
182 printf("43%21=%u\n", 43%21);
183 printf("126/21=%u\n", 126/21);
184 printf("12%/21=%u\n", 126%21);
185 printf("131/21=%u\n", 131/21);
186 printf("131%21=%u\n", 131%21);
187 printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX
/2+3)/2);
188 printf("(UINT_MAX/2+3)%2=%u\n", (UINT_MAX
/2+3)%2);
190 printf("18/-21=%u\n", 18/-21);
191 printf("18%-21=%u\n", 18%-21);
192 printf("41/-21=%u\n", 41/-21);
193 printf("41%-21=%u\n", 41%-21);
194 printf("42/-21=%u\n", 42/-21);
195 printf("42%-21=%u\n", 42%-21);
196 printf("43/-21=%u\n", 43/-21);
197 printf("43%-21=%u\n", 43%-21);
198 printf("126/-21=%u\n", 126/-21);
199 printf("12%/-21=%u\n", 126%-21);
200 printf("131/-21=%u\n", 131/-21);
201 printf("131%-21=%u\n", 131%-21);
202 printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX
/2+3)/-2);
203 printf("(UINT_MAX/2+3)%-2=%u\n", (UINT_MAX
/2+3)%-2);
205 printf("-18/21=%u\n", -18/21);
206 printf("-18%21=%u\n", -18%21);
207 printf("-41/21=%u\n", -41/21);
208 printf("-41%21=%u\n", -41%21);
209 printf("-42/21=%u\n", -42/21);
210 printf("-42%21=%u\n", -42%21);
211 printf("-43/21=%u\n", -43/21);
212 printf("-43%21=%u\n", -43%21);
213 printf("-126/21=%u\n", -126/21);
214 printf("-12%/21=%u\n", -126%21);
215 printf("-131/21=%u\n", -131/21);
216 printf("-131%21=%u\n", -131%21);
217 printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX
/2+3))/2);
218 printf("-(UINT_MAX/2+3)%2=%u\n", (0-(UINT_MAX
/2+3))%2);
220 printf("-18/-21=%u\n", -18/-21);
221 printf("-18%-21=%u\n", -18%-21);
222 printf("-41/-21=%u\n", -41/-21);
223 printf("-41%-21=%u\n", -41%-21);
224 printf("-42/-21=%u\n", -42/-21);
225 printf("-42%-21=%u\n", -42%-21);
226 printf("-43/-21=%u\n", -43/-21);
227 printf("-43%-21=%u\n", -43%-21);
228 printf("-126/-21=%u\n", -126/-21);
229 printf("-12%/-21=%u\n", -126%-21);
230 printf("-131/-21=%u\n", -131/-21);
231 printf("-131%-21=%u\n", -131%-21);
232 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX
/2+3))/-2);
233 printf("-(UINT_MAX/2+3)%-2=%u\n", (0-(UINT_MAX
/2+3))%-2);
236 void macro_test(void)
238 printf("macro:\n");\f\v
240 printf("aaa=%d\n", AAA
);
242 printf("min=%d\n", min(1, min(2, -1)));
244 printf("s1=%s\n", glue(HIGH
, LOW
));
245 printf("s2=%s\n", xglue(HIGH
, LOW
));
246 printf("s3=%s\n", str("c"));
247 printf("s4=%s\n", str(a1
));
248 printf("B3=%d\n", B3
);
250 printf("onetwothree=%d\n", onetwothree
);
253 printf("A defined\n");
256 printf("B defined\n");
259 printf("A defined\n");
261 printf("A not defined\n");
264 printf("B defined\n");
266 printf("B not defined\n");
270 printf("A defined\n");
272 printf("B1 defined\n");
274 printf("B1 not defined\n");
277 printf("A not defined\n");
279 printf("B2 defined\n");
281 printf("B2 not defined\n");
286 printf("test true1\n");
289 printf("test true2\n");
292 printf("test true3\n");
295 printf("test trueA\n");
298 printf("test trueB\n");
314 printf("__LINE__ defined\n");
317 printf("__LINE__=%d __FILE__=%s\n",
321 printf("__LINE__=%d __FILE__=%s\n",
324 printf("__LINE__=%d __FILE__=%s\n",
326 #line 227 "tcctest.c"
329 /* not strictly preprocessor, but we test it there */
331 printf("__func__ = %s\n", __func__
);
332 dprintf(1, "vaarg=%d\n", 1);
334 dprintf1(1, "vaarg1\n");
335 dprintf1(1, "vaarg1=%d\n", 2);
336 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
339 printf("func='%s'\n", __FUNCTION__
);
341 /* complicated macros in glibc */
342 printf("INT64_MIN=" LONG_LONG_FORMAT
"\n", INT64_MIN
);
352 /* macro function with argument outside the macro string */
353 #define MF_s MF_hello
354 #define MF_hello(msg) printf("%s\n",msg)
356 #define MF_t printf("tralala\n"); MF_hello
361 /* test macro substituion inside args (should not eat stream) */
362 printf("qq=%d\n", qq(qq
)(2));
364 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
365 null argument without a space. gcc 3.2 fixes that. */
368 printf("qq1=%d\n", qq1( ));
370 /* comment with stray handling *\
372 /* this is a valid *\/ comment */
373 /* this is a valid comment *\*/
377 /* test function macro substitution when the function name is
381 /* And again when the name and parenthes are separated by a
383 TEST2
/* the comment */ ();
387 static void print_num(char *fn
, int line
, int num
) {
388 printf("fn %s, line %d, num %d\n", fn
, line
, num
);
391 void recursive_macro_test(void)
394 #define ELF32_ST_TYPE(val) ((val) & 0xf)
395 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
396 #define STB_WEAK 2 /* Weak symbol */
397 #define ELFW(type) ELF##32##_##type
398 printf("%d\n", ELFW(ST_INFO
)(STB_WEAK
, ELFW(ST_TYPE
)(123)));
402 #define print_num(x) print_num(__FILE__,__LINE__,x)
404 WRAP(print_num(123));
405 WRAP(WRAP(print_num(123)));
407 static struct recursive_macro
{ int rm_field
; } G
;
408 #define rm_field (G.rm_field)
409 printf("rm_field = %d\n", rm_field
);
410 printf("rm_field = %d\n", WRAP(rm_field
));
411 WRAP((printf("rm_field = %d %d\n", rm_field
, WRAP(rm_field
))));
428 void ps(const char *s
)
440 const char foo1_string
[] = "\
449 printf("\141\1423\143\n");/* dezdez test */
450 printf("\x41\x42\x43\x3a\n");
451 printf("c=%c\n", 'r');
452 printf("wc=%C 0x%lx %C\n", L
'a', L
'\x1234', L
'c');
453 printf("foo1_string='%s'\n", foo1_string
);
455 printf("wstring=%S\n", L
"abc");
456 printf("wstring=%S\n", L
"abc" L
"def" "ghi");
457 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
458 printf("L'\\377'=%d L'\\xff'=%d\n", L
'\377', L
'\xff');
462 while ((b
= b
+ 1) < 96) {
466 printf("fib=%d\n", fib(33));
468 while (b
!= 0x80000000) {
481 for(i
= 0; i
< 10;i
++)
491 /* c99 for loop init test */
492 for (size_t count
= 1; count
< 3; count
++)
493 printf("count=%d\n", count
);
494 printf("count = %d\n", count
);
496 /* break/continue tests */
508 /* break/continue tests */
520 for(i
= 0;i
< 10;i
++) {
528 typedef int typedef_and_label
;
533 static void *label_table
[3] = { &&label1
, &&label2
, &&label3
};
537 /* This needs to parse as label, not as start of decl. */
548 /* we also test computed gotos (GCC extension) */
550 goto *label_table
[i
];
578 printf("enum:\n%d %d %d %d %d %d\n",
579 E0
, E1
, E2
, E3
, E4
, E5
);
581 printf("b1=%d\n", b1
);
597 printf("typedef:\n");
598 printf("a=%d\n", *a
);
600 printf("mytype2=%d\n", mytype2
);
605 printf("forward:\n");
611 void forward_ref(void)
613 printf("forward ok\n");
616 typedef struct struct1
{
636 struct struct1 st1
, st2
;
638 int main(int argc
, char **argv
)
643 recursive_macro_test();
657 constant_expr_test();
661 compound_literal_test();
663 struct_assign_test();
673 old_style_function();
678 statement_expr_test();
686 cmp_comparison_test();
689 builtin_frame_address_test();
701 printf("g1=%d\n", g
);
709 printf("g2=%d\n", g
);
713 printf("g3=%d\n", g
);
717 printf("g4=%d\n", g
);
720 printf("g5=%d\n", g
);
728 printf("sizeof(a) = %d\n", sizeof(a
));
729 printf("sizeof(\"a\") = %d\n", sizeof("a"));
731 printf("sizeof(__func__) = %d\n", sizeof(__func__
));
733 printf("sizeof tab %d\n", sizeof(tab
));
734 printf("sizeof tab2 %d\n", sizeof tab2
);
738 printf("%d %d %d\n", tab
[0], tab
[1], tab
[2]);
741 tab2
[i
][j
] = 10 * i
+ j
;
743 printf(" %3d", ((int *)tab2
)[i
]);
746 printf("sizeof(size_t)=%d\n", sizeof(size_t));
747 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
754 printf("%d\n", a
+= 1);
755 printf("%d\n", a
-= 2);
756 printf("%d\n", a
*= 31232132);
757 printf("%d\n", a
/= 4);
758 printf("%d\n", a
%= 20);
759 printf("%d\n", a
&= 6);
760 printf("%d\n", a
^= 7);
761 printf("%d\n", a
|= 8);
762 printf("%d\n", a
>>= 3);
763 printf("%d\n", a
<<= 4);
767 printf("%d\n", a
+ 1);
768 printf("%d\n", a
- 2);
769 printf("%d\n", a
* 312);
770 printf("%d\n", a
/ 4);
771 printf("%d\n", b
/ 4);
772 printf("%d\n", (unsigned)b
/ 4);
773 printf("%d\n", a
% 20);
774 printf("%d\n", b
% 20);
775 printf("%d\n", (unsigned)b
% 20);
776 printf("%d\n", a
& 6);
777 printf("%d\n", a
^ 7);
778 printf("%d\n", a
| 8);
779 printf("%d\n", a
>> 3);
780 printf("%d\n", b
>> 3);
781 printf("%d\n", (unsigned)b
>> 3);
782 printf("%d\n", a
<< 4);
787 printf("%d\n", 12 + 1);
788 printf("%d\n", 12 - 2);
789 printf("%d\n", 12 * 312);
790 printf("%d\n", 12 / 4);
791 printf("%d\n", 12 % 20);
792 printf("%d\n", 12 & 6);
793 printf("%d\n", 12 ^ 7);
794 printf("%d\n", 12 | 8);
795 printf("%d\n", 12 >> 2);
796 printf("%d\n", 12 << 4);
800 printf("%d %d %d %d\n",
809 return (c
>= 'a' & c
<= 'z') | (c
>= 'A' & c
<= 'Z') | c
== '_';
812 /**********************/
814 int vstack
[10], *vstack_ptr
;
816 void vpush(int vt
, int vc
)
822 void vpop(int *ft
, int *fc
)
835 vstack_ptr
[-2] &= ~0xffffff80;
837 printf("res= %d %d\n", a
, b
);
840 void constant_expr_test()
843 printf("constant_expr:\n");
845 printf("%d\n", a
* 16);
846 printf("%d\n", a
* 1);
847 printf("%d\n", a
+ 0);
857 printf("expr_ptr:\n");
860 printf("diff=%d\n", q
- p
);
862 printf("inc=%d\n", p
- tab4
);
864 printf("dec=%d\n", p
- tab4
);
866 printf("inc=%d\n", p
- tab4
);
868 printf("dec=%d\n", p
- tab4
);
869 printf("add=%d\n", p
+ 3 - tab4
);
870 printf("add=%d\n", 3 + p
- tab4
);
872 /* check if 64bit support is ok */
875 printf("%p %p %ld\n", q
, p
, p
-q
);
876 printf("%d %d %d %d %d %d\n",
877 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
880 printf("%p %p %ld\n", q
, p
, p
-q
);
881 printf("%d %d %d %d %d %d\n",
882 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
883 p
= (int *)((char *)p
+ 0xf0000000);
884 printf("%p %p %ld\n", q
, p
, p
-q
);
885 printf("%d %d %d %d %d %d\n",
886 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
888 printf("%p %p %ld\n", q
, p
, p
-q
);
889 printf("%d %d %d %d %d %d\n",
890 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
895 struct size12 s
[2], *sp
= s
;
900 printf("%d\n", sp
[j
].i
);
907 printf("constant_expr:\n");
910 printf("%d\n", a
== a
);
911 printf("%d\n", a
!= a
);
913 printf("%d\n", a
< b
);
914 printf("%d\n", a
<= b
);
915 printf("%d\n", a
<= a
);
916 printf("%d\n", b
>= a
);
917 printf("%d\n", a
>= a
);
918 printf("%d\n", b
> a
);
920 printf("%d\n", (unsigned)a
< b
);
921 printf("%d\n", (unsigned)a
<= b
);
922 printf("%d\n", (unsigned)a
<= a
);
923 printf("%d\n", (unsigned)b
>= a
);
924 printf("%d\n", (unsigned)a
>= a
);
925 printf("%d\n", (unsigned)b
> a
);
954 printf("sizes: %d %d %d %d\n",
955 sizeof(struct struct1
),
956 sizeof(struct struct2
),
957 sizeof(union union1
),
958 sizeof(union union2
));
962 printf("st1: %d %d %d\n",
963 st1
.f1
, st1
.f2
, st1
.f3
);
966 printf("union1: %d\n", st1
.u
.v1
);
969 printf("union2: %d\n", u
.w1
);
974 printf("st2: %d %d %d\n",
975 s
->f1
, s
->f2
, s
->f3
);
976 printf("str_addr=%x\n", (int)st1
.str
- (int)&st1
.f1
);
978 /* align / size tests */
979 printf("aligntest1 sizeof=%d alignof=%d\n",
980 sizeof(struct aligntest1
), __alignof__(struct aligntest1
));
981 printf("aligntest2 sizeof=%d alignof=%d\n",
982 sizeof(struct aligntest2
), __alignof__(struct aligntest2
));
983 printf("aligntest3 sizeof=%d alignof=%d\n",
984 sizeof(struct aligntest3
), __alignof__(struct aligntest3
));
985 printf("aligntest4 sizeof=%d alignof=%d\n",
986 sizeof(struct aligntest4
), __alignof__(struct aligntest4
));
988 /* empty structures (GCC extension) */
989 printf("sizeof(struct empty) = %d\n", sizeof(struct empty
));
990 printf("alignof(struct empty) = %d\n", __alignof__(struct empty
));
993 /* XXX: depend on endianness */
994 void char_short_test()
998 printf("char_short:\n");
1002 printf("s8=%d %d\n",
1003 *(char *)&var1
, *(char *)&var2
);
1004 printf("u8=%d %d\n",
1005 *(unsigned char *)&var1
, *(unsigned char *)&var2
);
1006 printf("s16=%d %d\n",
1007 *(short *)&var1
, *(short *)&var2
);
1008 printf("u16=%d %d\n",
1009 *(unsigned short *)&var1
, *(unsigned short *)&var2
);
1010 printf("s32=%d %d\n",
1011 *(int *)&var1
, *(int *)&var2
);
1012 printf("u32=%d %d\n",
1013 *(unsigned int *)&var1
, *(unsigned int *)&var2
);
1014 *(char *)&var1
= 0x08;
1015 printf("var1=%x\n", var1
);
1016 *(short *)&var1
= 0x0809;
1017 printf("var1=%x\n", var1
);
1018 *(int *)&var1
= 0x08090a0b;
1019 printf("var1=%x\n", var1
);
1022 /******************/
1024 typedef struct Sym
{
1032 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1033 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1035 static int toupper1(int a
)
1042 int *s
, a
, b
, t
, f
, i
;
1046 printf("!s=%d\n", !s
);
1050 printf("a=%d\n", a
);
1052 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1053 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1054 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1069 printf("b=%d\n", a
+ (0 ? 1 : a
/ 2));
1071 /* test register spilling */
1074 a
= (a
+ b
) * ((a
< b
) ?
1075 ((b
- a
) * (a
- b
)): a
+ b
);
1076 printf("a=%d\n", a
);
1078 /* test complex || or && expressions */
1082 printf("exp=%d\n", f
== (32 <= a
&& a
<= 3));
1083 printf("r=%d\n", (t
|| f
) + (t
&& f
));
1088 int aspect_native
= 65536;
1089 double bfu_aspect
= 1.0;
1091 for(aspect_on
= 0; aspect_on
< 2; aspect_on
++) {
1092 aspect
=aspect_on
?(aspect_native
*bfu_aspect
+0.5):65535UL;
1093 printf("aspect=%d\n", aspect
);
1097 /* test ? : GCC extension */
1099 static int v1
= 34 ? : -1; /* constant case */
1100 static int v2
= 0 ? : -1; /* constant case */
1103 printf("%d %d\n", v1
, v2
);
1104 printf("%d %d\n", a
- 30 ? : a
* 2, a
+ 1 ? : a
* 2);
1107 /* again complex expression */
1108 for(i
=0;i
<256;i
++) {
1109 if (toupper1 (i
) != TOUPPER (i
))
1110 printf("error %d\n", i
);
1114 /* GCC accepts that */
1115 static int tab_reinit
[];
1116 static int tab_reinit
[10];
1118 //int cinit1; /* a global variable can be defined several times without error ! */
1122 int *cinit2
= (int []){3, 2, 1};
1124 void compound_literal_test(void)
1129 printf("compound_test:\n");
1131 p
= (int []){1, 2, 3};
1133 printf(" %d", p
[i
]);
1137 printf("%d", cinit2
[i
]);
1141 printf("q1=%s\n", q
);
1143 q
= (char *){ "tralala2" };
1144 printf("q2=%s\n", q
);
1147 printf("q3=%s\n", q3
);
1149 q
= (char []){ "tralala3" };
1150 printf("q4=%s\n", q
);
1153 p
= (int []){1, 2, cinit1
+ 3};
1155 printf(" %d", p
[i
]);
1159 p
= (int []){1, 2, 4 + i
};
1160 printf("%d %d %d\n",
1182 printf("kr_test:\n");
1183 printf("func1=%d\n", kr_func1(3, 4));
1184 printf("func2=%d\n", kr_func2(3, 4));
1191 tab
= (char*)malloc(20);
1208 /* structure assignment tests */
1214 struct structa1 ssta1
;
1216 void struct_assign_test1(struct structa1 s1
, int t
, float f
)
1218 printf("%d %d %d %f\n", s1
.f1
, s1
.f2
, t
, f
);
1221 struct structa1
struct_assign_test2(struct structa1 s1
, int t
)
1228 void struct_assign_test(void)
1231 struct structa1 lsta1
, lsta2
;
1238 printf("struct_assign_test:\n");
1242 printf("%d %d\n", s
.lsta1
.f1
, s
.lsta1
.f2
);
1244 printf("%d %d\n", s
.lsta2
.f1
, s
.lsta2
.f2
);
1249 struct_assign_test1(ps
->lsta2
, 3, 4.5);
1251 printf("before call: %d %d\n", s
.lsta2
.f1
, s
.lsta2
.f2
);
1252 ps
->lsta2
= struct_assign_test2(ps
->lsta2
, ps
->i
);
1253 printf("after call: %d %d\n", ps
->lsta2
.f1
, ps
->lsta2
.f2
);
1258 /* XXX: we should allow this even without braces */
1259 { struct_assign_test
}
1261 printf("%d\n", struct_assign_test
== t
[0].elem
);
1264 /* casts to short/char */
1266 void cast1(char a
, short b
, unsigned char c
, unsigned short d
)
1268 printf("%d %d %d %d\n", a
, b
, c
, d
);
1282 p
-= 0x700000000042;
1284 printf("cast_test:\n");
1288 printf("%d %d %d %d\n",
1291 (unsigned char)(a
+ 1),
1292 (unsigned short)(a
+ 1));
1293 printf("%d %d %d %d\n",
1296 (unsigned char)0xfffff,
1297 (unsigned short)0xfffff);
1299 a
= (bcast
= 128) + 1;
1301 a
= (scast
= 65536) + 1;
1304 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
1306 /* test cast from unsigned to signed short to int */
1309 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b
, d
);
1312 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b
, d
);
1314 /* test implicit int casting for array accesses */
1318 printf("%d %d\n", tab
[0], tab
[1]);
1320 /* test implicit casting on some operators */
1321 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1322 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1323 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1325 /* from pointer to integer types */
1326 printf("%d %d %ld %ld %lld %lld\n",
1327 (int)p
, (unsigned int)p
,
1328 (long)p
, (unsigned long)p
,
1329 (long long)p
, (unsigned long long)p
);
1331 /* from integers to pointers */
1332 printf("%p %p %p %p\n",
1333 (void *)a
, (void *)b
, (void *)c
, (void *)d
);
1336 /* initializers tests */
1337 struct structinit1
{
1346 int sinit3
[3] = { 1, 2, {{3}}, };
1347 int sinit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1348 int sinit5
[3][2] = { 1, 2, 3, 4, 5, 6 };
1349 int sinit6
[] = { 1, 2, 3 };
1350 int sinit7
[] = { [2] = 3, [0] = 1, 2 };
1351 char sinit8
[] = "hello" "trala";
1353 struct structinit1 sinit9
= { 1, 2, 3 };
1354 struct structinit1 sinit10
= { .f2
= 2, 3, .f1
= 1 };
1355 struct structinit1 sinit11
= { .f2
= 2, 3, .f1
= 1,
1363 char *sinit12
= "hello world";
1369 char sinit14
[10] = { "abc" };
1370 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
1372 struct { int a
[3], b
; } sinit16
[] = { { 1 }, 2 };
1388 struct complexinit0
{
1393 struct complexinit
{
1395 const struct complexinit0
*b
;
1398 const static struct complexinit cix
[] = {
1401 .b
= (const struct complexinit0
[]) {
1409 struct complexinit2
{
1414 struct complexinit2 cix20
;
1416 struct complexinit2 cix21
= {
1418 .b
= { 3001, 3002, 3003 }
1421 struct complexinit2 cix22
= {
1423 .b
= { 4001, 4002, 4003, 4004, 4005, 4006 }
1426 void init_test(void)
1430 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1431 int linit6
[] = { 1, 2, 3 };
1433 char linit8
[] = "hello" "trala";
1434 int linit12
[10] = { 1, 2 };
1435 int linit13
[10] = { 1, 2, [7] = 3, [3] = 4, };
1436 char linit14
[10] = "abc";
1437 int linit15
[10] = { linit1
, linit1
+ 1, [6] = linit1
+ 2, };
1438 struct linit16
{ int a1
, a2
, a3
, a4
; } linit16
= { 1, .a3
= 2 };
1439 int linit17
= sizeof(linit17
);
1441 printf("init_test:\n");
1443 printf("sinit1=%d\n", sinit1
);
1444 printf("sinit2=%d\n", sinit2
);
1445 printf("sinit3=%d %d %d %d\n",
1451 printf("sinit6=%d\n", sizeof(sinit6
));
1452 printf("sinit7=%d %d %d %d\n",
1458 printf("sinit8=%s\n", sinit8
);
1459 printf("sinit9=%d %d %d\n",
1464 printf("sinit10=%d %d %d\n",
1469 printf("sinit11=%d %d %d %d %d %d\n",
1480 printf("[%d][%d] = %d %d %d\n",
1481 i
, j
, sinit4
[i
][j
], sinit5
[i
][j
], linit4
[i
][j
]);
1482 printf("linit1=%d\n", linit1
);
1483 printf("linit2=%d\n", linit2
);
1484 printf("linit6=%d\n", sizeof(linit6
));
1485 printf("linit8=%d %s\n", sizeof(linit8
), linit8
);
1487 printf("sinit12=%s\n", sinit12
);
1488 printf("sinit13=%d %s %s %s\n",
1493 printf("sinit14=%s\n", sinit14
);
1495 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
1497 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
1499 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
1501 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
1503 printf("%d %d %d %d\n",
1508 /* test that initialisation is done after variable declare */
1509 printf("linit17=%d\n", linit17
);
1510 printf("sinit15=%d\n", sinit15
[0]);
1511 printf("sinit16=%d %d\n", sinit16
[0].a
[0], sinit16
[1].a
[0]);
1512 printf("sinit17=%s %d %s %d\n",
1513 sinit17
[0].s
, sinit17
[0].len
,
1514 sinit17
[1].s
, sinit17
[1].len
);
1516 printf("%x ", sinit18
[i
]);
1518 /* complex init check */
1519 printf("cix: %d %d %d %d %d %d %d\n",
1521 cix
[0].b
[0].a
, cix
[0].b
[0].b
,
1522 cix
[0].b
[1].a
, cix
[0].b
[1].b
,
1523 cix
[0].b
[2].a
, cix
[0].b
[2].b
);
1524 printf("cix2: %d %d\n", cix21
.b
[2], cix22
.b
[5]);
1525 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20
, sizeof cix21
, sizeof cix22
);
1553 /* ISOC99 _Bool type */
1554 void c99_bool_test(void)
1560 printf("bool_test:\n");
1561 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
1563 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
1565 printf("b = %d\n", b
);
1567 printf("b = %d\n", b
);
1571 void bitfield_test(void)
1583 unsigned int f5
: 7;
1585 printf("bitfield_test:");
1586 printf("sizeof(st1) = %d\n", sizeof(st1
));
1595 printf("%d %d %d %d %d\n",
1596 st1
.f1
, st1
.f2
, st1
.f3
, st1
.f4
, st1
.f5
);
1599 printf("%d %d\n", sa
, ca
);
1603 printf("st1.f1 == -1\n");
1605 printf("st1.f1 != -1\n");
1607 printf("st1.f2 == -1\n");
1609 printf("st1.f2 != -1\n");
1611 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1612 long-long bitfields whose size is not bigger than int */
1617 unsigned long long f3
: 38;
1619 st2
.f1
= 0x123456789ULL
;
1621 st2
.f2
= (long long)a
<< 25;
1624 printf("%lld %lld %lld\n", st2
.f1
, st2
.f2
, st2
.f3
);
1628 #define FLOAT_FMT "%f\n"
1630 /* x86's float isn't compatible with GCC */
1631 #define FLOAT_FMT "%.5f\n"
1634 /* declare strto* functions as they are C99 */
1635 double strtod(const char *nptr
, char **endptr
);
1638 float strtof(const char *nptr
, char **endptr
) {return (float)strtod(nptr
, endptr
);}
1639 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
) {return (LONG_DOUBLE
)strtod(nptr
, endptr
);}
1641 float strtof(const char *nptr
, char **endptr
);
1642 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
);
1645 #define FTEST(prefix, typename, type, fmt)\
1646 void prefix ## cmp(type a, type b)\
1648 printf("%d %d %d %d %d %d\n",\
1655 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1663 printf(fmt "\n", ++a);\
1664 printf(fmt "\n", a++);\
1665 printf(fmt "\n", a);\
1667 printf("%d %d\n", !a, !b);\
1669 void prefix ## fcast(type a)\
1677 unsigned long long llua;\
1682 printf("ftof: %f %f %Lf\n", fa, da, la);\
1684 llia = (long long)a;\
1685 a = (a >= 0) ? a : -a;\
1686 ua = (unsigned int)a;\
1687 llua = (unsigned long long)a;\
1688 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
1691 llia = -0x123456789012345LL;\
1692 llua = 0xf123456789012345LLU;\
1694 printf("itof: " fmt "\n", b);\
1696 printf("utof: " fmt "\n", b);\
1698 printf("lltof: " fmt "\n", b);\
1700 printf("ulltof: " fmt "\n", b);\
1703 float prefix ## retf(type a) { return a; }\
1704 double prefix ## retd(type a) { return a; }\
1705 LONG_DOUBLE prefix ## retld(type a) { return a; }\
1707 void prefix ## call(void)\
1709 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1710 printf("double: %f\n", prefix ## retd(42.123456789));\
1711 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1712 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
1715 void prefix ## signed_zeros(void) \
1717 type x = 0.0, y = -0.0, n, p;\
1719 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
1720 1.0 / x != 1.0 / y);\
1722 printf ("x != y; this is wrong!\n");\
1726 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
1727 1.0 / x != 1.0 / n);\
1729 printf ("x != -x; this is wrong!\n");\
1733 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
1734 1.0 / x != 1.0 / p);\
1736 printf ("x != +y; this is wrong!\n");\
1739 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
1740 1.0 / x != 1.0 / p);\
1742 printf ("x != -y; this is wrong!\n");\
1744 void prefix ## test(void)\
1746 printf("testing '%s'\n", #typename);\
1747 prefix ## cmp(1, 2.5);\
1748 prefix ## cmp(2, 1.5);\
1749 prefix ## cmp(1, 1);\
1750 prefix ## fcast(234.6);\
1751 prefix ## fcast(-2334.6);\
1753 prefix ## signed_zeros();\
1756 FTEST(f
, float, float, "%f")
1757 FTEST(d
, double, double, "%f")
1758 FTEST(ld
, long double, LONG_DOUBLE
, "%Lf")
1760 double ftab1
[3] = { 1.2, 3.4, -5.6 };
1763 void float_test(void)
1765 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
1771 printf("float_test:\n");
1772 printf("sizeof(float) = %d\n", sizeof(float));
1773 printf("sizeof(double) = %d\n", sizeof(double));
1774 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE
));
1778 printf("%f %f %f\n", ftab1
[0], ftab1
[1], ftab1
[2]);
1779 printf("%f %f %f\n", 2.12, .5, 2.3e10
);
1780 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1782 printf("da=%f\n", da
);
1784 printf("fa=%f\n", fa
);
1787 printf("da = %f\n", da
);
1790 printf("db = %f\n", db
);
1799 return fib(n
-1) + fib(n
-2);
1811 printf("funcptr:\n");
1818 /* more complicated pointer computation */
1821 printf("sizeof1 = %d\n", sizeof(funcptr_test
));
1822 printf("sizeof2 = %d\n", sizeof funcptr_test
);
1823 printf("sizeof3 = %d\n", sizeof(&funcptr_test
));
1824 printf("sizeof4 = %d\n", sizeof &funcptr_test
);
1827 void lloptest(long long a
, long long b
)
1829 unsigned long long ua
, ub
;
1834 printf("arith: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
1840 printf("arith1: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
1846 printf("bin: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
1852 printf("test: %d %d %d %d %d %d\n",
1860 printf("utest: %d %d %d %d %d %d\n",
1871 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
1872 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
++, b
++);
1873 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", --a
, --b
);
1874 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
1876 printf("not: %d %d %d %d\n", !a
, !ua
, !b
, !ub
);
1879 void llshift(long long a
, int b
)
1881 printf("shift: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
1882 (unsigned long long)a
>> b
,
1885 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
1886 (unsigned long long)a
>> 3,
1889 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
1890 (unsigned long long)a
>> 35,
1900 long long la
, lb
, lc
;
1901 unsigned long long ula
, ulb
, ulc
;
1904 la
= (la
<< 20) | 0x12345;
1906 printf("la=" LONG_LONG_FORMAT
" ula=" ULONG_LONG_FORMAT
"\n", la
, ula
);
1911 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
1916 printf("ftoll: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", la
, lb
, lc
);
1921 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
1926 printf("ftoull: " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
"\n", ula
, ulb
, ulc
);
1929 long long llfunc1(int a
)
1939 long long int value(struct S
*v
)
1941 return ((long long int)v
->item
);
1944 void longlong_test(void)
1949 printf("longlong_test:\n");
1950 printf("sizeof(long long) = %d\n", sizeof(long long));
1955 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
1956 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %Lx\n",
1960 0x1234567812345679);
1962 printf(LONG_LONG_FORMAT
"\n", a
);
1965 lloptest(0xff, 0x1234);
1966 b
= 0x72345678 << 10;
1970 b
= 0x72345678LL
<< 10;
1981 /* long long reg spill test */
1986 printf("%lld\n", value(&a
));
1988 lloptest(0x80000000, 0);
1990 /* another long long spill test */
1996 printf("%lld\n", *p
);
2001 printf("%d %d %d %d\n", a
> b
, a
< b
, a
>= b
, a
<= b
);
2003 printf(LONG_LONG_FORMAT
"\n", 0x123456789LLU
);
2006 void manyarg_test(void)
2008 LONG_DOUBLE ld
= 1234567891234LL;
2009 printf("manyarg_test:\n");
2010 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2011 1, 2, 3, 4, 5, 6, 7, 8,
2012 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2013 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2014 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2015 1, 2, 3, 4, 5, 6, 7, 8,
2016 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2017 1234567891234LL, 987654321986LL,
2019 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2020 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2021 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2022 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2023 1234567891234LL, 987654321986LL,
2025 printf("%d %d %d %d %d %d %d %d %Lf\n",
2026 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2027 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2028 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"%f %f %Lf\n",
2029 1, 2, 3, 4, 5, 6, 7, 8,
2030 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2031 1234567891234LL, 987654321986LL,
2033 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2034 "%Lf " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f %Lf\n",
2035 1, 2, 3, 4, 5, 6, 7, 8,
2036 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2037 ld
, 1234567891234LL, 987654321986LL,
2041 void vprintf1(const char *fmt
, ...)
2065 i
= va_arg(ap
, int);
2069 d
= va_arg(ap
, double);
2073 ll
= va_arg(ap
, long long);
2074 printf(LONG_LONG_FORMAT
, ll
);
2077 ld
= va_arg(ap
, LONG_DOUBLE
);
2095 void stdarg_for_struct(struct myspace bob
, ...)
2097 struct myspace george
, bill
;
2102 bill
= va_arg(ap
, struct myspace
);
2103 george
= va_arg(ap
, struct myspace
);
2104 validate
= va_arg(ap
, int);
2105 printf("stdarg_for_struct: %d %d %d %d\n",
2106 bob
.profile
, bill
.profile
, george
.profile
, validate
);
2110 void stdarg_test(void)
2112 LONG_DOUBLE ld
= 1234567891234LL;
2115 vprintf1("%d %d %d\n", 1, 2, 3);
2116 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2117 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2118 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2119 vprintf1("%d %f %l %F %d %f %l %F\n",
2120 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2121 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2122 1, 2, 3, 4, 5, 6, 7, 8,
2123 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2124 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2125 1, 2, 3, 4, 5, 6, 7, 8,
2126 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2127 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2129 1, 2, 3, 4, 5, 6, 7, 8,
2130 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2131 1234567891234LL, 987654321986LL,
2133 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2135 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2136 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2137 1234567891234LL, 987654321986LL,
2139 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2140 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2141 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2143 1, 2, 3, 4, 5, 6, 7, 8,
2144 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2145 1234567891234LL, 987654321986LL,
2147 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2148 "%F %l %l %f %f %F\n",
2149 1, 2, 3, 4, 5, 6, 7, 8,
2150 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2151 ld
, 1234567891234LL, 987654321986LL,
2155 stdarg_for_struct(bob
, bob
, bob
, bob
.profile
);
2158 void whitespace_test(void)
2164 ntf("whitspace:\n");\f\v
2168 #ifdef CORRECT_CR_HANDLING
2177 #ifdef ACCEPT_CR_IN_STRINGS
2178 printf("len1=%d\n", strlen("
2180 #ifdef CORRECT_CR_HANDLING
2183 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
2185 printf("len1=%d\n", strlen("
a
2187 #endif /* ACCEPT_CR_IN_STRINGS */
2190 int reltab
[3] = { 1, 2, 3 };
2192 int *rel1
= &reltab
[1];
2193 int *rel2
= &reltab
[2];
2195 void relocation_test(void)
2197 printf("*rel1=%d\n", *rel1
);
2198 printf("*rel2=%d\n", *rel2
);
2201 void old_style_f(a
,b
,c
)
2205 printf("a=%d b=%d b=%f\n", a
, b
, c
);
2208 void decl_func1(int cmpfn())
2210 printf("cmpfn=%lx\n", (long)cmpfn
);
2213 void decl_func2(cmpfn
)
2216 printf("cmpfn=%lx\n", (long)cmpfn
);
2219 void old_style_function(void)
2221 old_style_f((void *)1, 2, 3.0);
2228 #if defined __i386__ || defined __x86_64__ || defined __arm__
2229 char *p
= alloca(16);
2230 strcpy(p
,"123456789012345");
2231 printf("alloca: p is %s\n", p
);
2232 char *demo
= "This is only a test.\n";
2233 /* Test alloca embedded in a larger expression */
2234 printf("alloca: %s\n", strcpy(alloca(strlen(demo
)+1),demo
) );
2238 void *bounds_checking_is_enabled()
2240 char ca
[10], *cp
= ca
-1;
2241 return (ca
!= cp
+ 1) ? cp
: NULL
;
2244 typedef int constant_negative_array_size_as_compile_time_assertion_idiom
[(1 ? 2 : 0) - 1];
2246 void c99_vla_test(int size1
, int size2
)
2248 #if defined __i386__ || defined __x86_64__
2249 int size
= size1
* size2
;
2250 int tab1
[size
][2], tab2
[10][2];
2251 void *tab1_ptr
, *tab2_ptr
, *bad_ptr
;
2253 /* "size" should have been 'captured' at tab1 declaration,
2254 so modifying it should have no effect on VLA behaviour. */
2257 printf("Test C99 VLA 1 (sizeof): ");
2258 printf("%s\n", (sizeof tab1
== size1
* size2
* 2 * sizeof(int)) ? "PASSED" : "FAILED");
2261 printf("Test C99 VLA 2 (ptrs subtract): ");
2262 printf("%s\n", (tab2
- tab1
== (tab2_ptr
- tab1_ptr
) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2263 printf("Test C99 VLA 3 (ptr add): ");
2264 printf("%s\n", &tab1
[5][1] == (tab1_ptr
+ (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2265 printf("Test C99 VLA 4 (ptr access): ");
2266 tab1
[size1
][1] = 42;
2267 printf("%s\n", (*((int *) (tab1_ptr
+ (size1
* 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2269 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2270 if (bad_ptr
= bounds_checking_is_enabled()) {
2271 int *t1
= &tab1
[size1
* size2
- 1][3];
2272 int *t2
= &tab2
[9][3];
2273 printf("%s ", bad_ptr
== t1
? "PASSED" : "FAILED");
2274 printf("%s ", bad_ptr
== t2
? "PASSED" : "FAILED");
2276 char*c1
= 1 + sizeof(tab1
) + (char*)tab1
;
2277 char*c2
= 1 + sizeof(tab2
) + (char*)tab2
;
2278 printf("%s ", bad_ptr
== c1
? "PASSED" : "FAILED");
2279 printf("%s ", bad_ptr
== c2
? "PASSED" : "FAILED");
2283 printf("%s ", bad_ptr
== i1
? "PASSED" : "FAILED");
2284 printf("%s ", bad_ptr
== i2
? "PASSED" : "FAILED");
2286 int *x1
= tab1
[size1
* size2
+ 1];
2287 int *x2
= tab2
[10 + 1];
2288 printf("%s ", bad_ptr
== x1
? "PASSED" : "FAILED");
2289 printf("%s ", bad_ptr
== x2
? "PASSED" : "FAILED");
2291 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2297 typedef __SIZE_TYPE__
uintptr_t;
2299 void sizeof_test(void)
2304 printf("sizeof(int) = %d\n", sizeof(int));
2305 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2306 printf("sizeof(long) = %d\n", sizeof(long));
2307 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2308 printf("sizeof(short) = %d\n", sizeof(short));
2309 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2310 printf("sizeof(char) = %d\n", sizeof(char));
2311 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2312 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2314 printf("sizeof(a++) = %d\n", sizeof a
++);
2315 printf("a=%d\n", a
);
2317 printf("sizeof(**ptr) = %d\n", sizeof (**ptr
));
2319 /* The type of sizeof should be as large as a pointer, actually
2320 it should be size_t. */
2321 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2324 /* Effectively <<32, but defined also on 32bit machines. */
2328 /* This checks that sizeof really can be used to manipulate
2329 uintptr_t objects, without truncation. */
2330 t2
= t
& -sizeof(uintptr_t);
2331 printf ("%lu %lu\n", t
, t2
);
2333 /* some alignof tests */
2334 printf("__alignof__(int) = %d\n", __alignof__(int));
2335 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2336 printf("__alignof__(short) = %d\n", __alignof__(short));
2337 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2338 printf("__alignof__(char) = %d\n", __alignof__(char));
2339 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2340 printf("__alignof__(func) = %d\n", __alignof__
sizeof_test());
2343 void typeof_test(void)
2352 printf("a=%f b=%f c=%f\n", a
, b
, c
);
2355 void statement_expr_test(void)
2368 printf("a=%d\n", a
);
2372 void local_label_test(void)
2378 __label__ l1
, l2
, l3
, l4
;
2392 printf("a=%d\n", a
);
2402 /* inline assembler test */
2405 /* from linux kernel */
2406 static char * strncat1(char * dest
,const char * src
,size_t count
)
2409 __asm__
__volatile__(
2418 "testb %%al,%%al\n\t"
2420 "2:\txorl %2,%2\n\t"
2422 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
2423 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
2428 static char * strncat2(char * dest
,const char * src
,size_t count
)
2431 __asm__
__volatile__(
2432 "repne scasb\n\t" /* one-line repne prefix + string op */
2439 "testb %%al,%%al\n\t"
2441 "2:\txorl %2,%2\n\t"
2443 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
2444 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
2449 static inline void * memcpy1(void * to
, const void * from
, size_t n
)
2452 __asm__
__volatile__(
2457 "1:\ttestb $1,%b4\n\t"
2461 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
2462 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
2467 static inline void * memcpy2(void * to
, const void * from
, size_t n
)
2470 __asm__
__volatile__(
2471 "rep movsl\n\t" /* one-line rep prefix + string op */
2475 "1:\ttestb $1,%b4\n\t"
2479 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
2480 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
2485 static __inline__
void sigaddset1(unsigned int *set
, int _sig
)
2487 __asm__("btsl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
2490 static __inline__
void sigdelset1(unsigned int *set
, int _sig
)
2492 asm("btrl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
2495 static __inline__ __const__
unsigned int swab32(unsigned int x
)
2497 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2498 "rorl $16,%0\n\t" /* swap words */
2499 "xchgb %b0,%h0" /* swap higher bytes */
2505 static __inline__
unsigned long long mul64(unsigned int a
, unsigned int b
)
2507 unsigned long long res
;
2508 __asm__("mull %2" : "=A" (res
) : "a" (a
), "r" (b
));
2512 static __inline__
unsigned long long inc64(unsigned long long a
)
2514 unsigned long long res
;
2515 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res
) : "A" (a
));
2526 printf("inline asm:\n");
2527 /* test the no operand case */
2528 asm volatile ("xorl %eax, %eax");
2530 memcpy1(buf
, "hello", 6);
2531 strncat1(buf
, " worldXXXXX", 3);
2532 printf("%s\n", buf
);
2534 memcpy2(buf
, "hello", 6);
2535 strncat2(buf
, " worldXXXXX", 3);
2536 printf("%s\n", buf
);
2538 /* 'A' constraint test */
2539 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2540 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2543 sigdelset1(&set
, 2);
2544 sigaddset1(&set
, 16);
2545 /* NOTE: we test here if C labels are correctly restored after the
2549 __asm__("btsl %1,%0" : "=m"(set
) : "Ir"(20) : "cc");
2550 #ifdef __GNUC__ // works strange with GCC 4.3
2553 printf("set=0x%x\n", set
);
2555 printf("swab32(0x%08x) = 0x%0x\n", val
, swab32(val
));
2569 #define COMPAT_TYPE(type1, type2) \
2571 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2572 __builtin_types_compatible_p (type1, type2));\
2577 void builtin_test(void)
2580 COMPAT_TYPE(int, int);
2581 COMPAT_TYPE(int, unsigned int);
2582 COMPAT_TYPE(int, char);
2583 COMPAT_TYPE(int, const int);
2584 COMPAT_TYPE(int, volatile int);
2585 COMPAT_TYPE(int *, int *);
2586 COMPAT_TYPE(int *, void *);
2587 COMPAT_TYPE(int *, const int *);
2588 COMPAT_TYPE(char *, unsigned char *);
2589 COMPAT_TYPE(char *, signed char *);
2590 COMPAT_TYPE(char *, char *);
2591 /* space is needed because tcc preprocessor introduces a space between each token */
2592 COMPAT_TYPE(char * *, void *);
2594 printf("res = %d\n", __builtin_constant_p(1));
2595 printf("res = %d\n", __builtin_constant_p(1 + 2));
2596 printf("res = %d\n", __builtin_constant_p(&constant_p_var
));
2597 printf("res = %d\n", __builtin_constant_p(constant_p_var
));
2601 extern int __attribute__((weak
)) weak_f1(void);
2602 extern int __attribute__((weak
)) weak_f2(void);
2603 extern int weak_f3(void);
2604 extern int __attribute__((weak
)) weak_v1
;
2605 extern int __attribute__((weak
)) weak_v2
;
2608 extern int (*weak_fpa
)() __attribute__((weak
));
2609 extern int __attribute__((weak
)) (*weak_fpb
)();
2610 extern __attribute__((weak
)) int (*weak_fpc
)();
2612 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak
));
2613 extern int __attribute((weak
)) weak_asm_f2(void) asm("weak_asm_f2x") ;
2614 extern int __attribute((weak
)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak
));
2615 extern int weak_asm_v1
asm("weak_asm_v1x") __attribute((weak
));
2616 extern int __attribute((weak
)) weak_asm_v2
asm("weak_asm_v2x") ;
2617 extern int __attribute((weak
)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak
));
2619 static const size_t dummy
= 0;
2620 extern __typeof(dummy
) weak_dummy1
__attribute__((weak
, alias("dummy")));
2621 extern __typeof(dummy
) __attribute__((weak
, alias("dummy"))) weak_dummy2
;
2622 extern __attribute__((weak
, alias("dummy"))) __typeof(dummy
) weak_dummy3
;
2624 int some_lib_func(void);
2625 int dummy_impl_of_slf(void) { return 444; }
2626 int some_lib_func(void) __attribute__((weak
, alias("dummy_impl_of_slf")));
2628 int weak_toolate() __attribute__((weak
));
2629 int weak_toolate() { return 0; }
2631 void __attribute__((weak
)) weak_test(void)
2633 printf("weak_f1=%d\n", weak_f1
? weak_f1() : 123);
2634 printf("weak_f2=%d\n", weak_f2
? weak_f2() : 123);
2635 printf("weak_f3=%d\n", weak_f3
? weak_f3() : 123);
2636 printf("weak_v1=%d\n",&weak_v1
? weak_v1
: 123);
2637 printf("weak_v2=%d\n",&weak_v2
? weak_v2
: 123);
2638 printf("weak_v3=%d\n",&weak_v3
? weak_v3
: 123);
2640 printf("weak_fpa=%d\n",&weak_fpa
? weak_fpa() : 123);
2641 printf("weak_fpb=%d\n",&weak_fpb
? weak_fpb() : 123);
2642 printf("weak_fpc=%d\n",&weak_fpc
? weak_fpc() : 123);
2644 printf("weak_asm_f1=%d\n", weak_asm_f1
!= NULL
);
2645 printf("weak_asm_f2=%d\n", weak_asm_f2
!= NULL
);
2646 printf("weak_asm_f3=%d\n", weak_asm_f3
!= NULL
);
2647 printf("weak_asm_v1=%d\n",&weak_asm_v1
!= NULL
);
2648 printf("weak_asm_v2=%d\n",&weak_asm_v2
!= NULL
);
2649 printf("weak_asm_v3=%d\n",&weak_asm_v3
!= NULL
);
2652 int __attribute__((weak
)) weak_f2() { return 222; }
2653 int __attribute__((weak
)) weak_f3() { return 333; }
2654 int __attribute__((weak
)) weak_v2
= 222;
2655 int __attribute__((weak
)) weak_v3
= 333;
2658 void const_func(const int a
)
2662 void const_warn_test(void)
2671 int getme (struct condstruct
*s
, int i
)
2673 int i1
= (i
== 0 ? 0 : s
)->i
;
2674 int i2
= (i
== 0 ? s
: 0)->i
;
2675 int i3
= (i
== 0 ? (void*)0 : s
)->i
;
2676 int i4
= (i
== 0 ? s
: (void*)0)->i
;
2677 return i1
+ i2
+ i3
+ i4
;
2686 struct global_data global_data
;
2688 int global_data_getstuff (int *, int);
2690 void global_data_callit (int i
)
2692 *global_data
.b
[i
] = global_data_getstuff (global_data
.b
[i
], 1);
2695 int global_data_getstuff (int *p
, int i
)
2700 void global_data_test (void)
2702 global_data
.a
[0] = 42;
2703 global_data
.b
[0] = &global_data
.a
[0];
2704 global_data_callit (0);
2705 printf ("%d\n", global_data
.a
[0]);
2710 unsigned char fill
: 3;
2711 unsigned char b1
: 1;
2712 unsigned char b2
: 1;
2713 unsigned char fill2
: 3;
2716 int glob1
, glob2
, glob3
;
2718 void compare_comparisons (struct cmpcmpS
*s
)
2720 if (s
->b1
!= (glob1
== glob2
)
2721 || (s
->b2
!= (glob1
== glob3
)))
2722 printf ("comparing comparisons broken\n");
2725 void cmp_comparison_test(void)
2729 glob1
= 42; glob2
= 42;
2732 compare_comparisons (&s
);
2735 int fcompare (double a
, double b
, int code
)
2738 case 0: return a
== b
;
2739 case 1: return a
!= b
;
2740 case 2: return a
< b
;
2741 case 3: return a
>= b
;
2742 case 4: return a
> b
;
2743 case 5: return a
<= b
;
2747 void math_cmp_test(void)
2749 double nan
= 0.0/0.0;
2753 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
2755 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
2756 And it does this in various ways so that all code generation paths
2757 are checked (generating inverted tests, or non-inverted tests, or
2758 producing a 0/1 value without jumps (that's done in the fcompare
2760 #define FCMP(a,b,op,iop,code) \
2761 if (fcompare (a,b,code)) \
2762 bug (a,b,op,iop,1); \
2764 bug (a,b,op,iop,2); \
2768 bug (a,b,op,iop,3); \
2769 if ((a op b) || comp) \
2770 bug (a,b,op,iop,4); \
2771 if ((a iop b) || comp) \
2776 /* Equality tests. */
2777 FCMP(nan
, nan
, ==, !=, 0);
2778 FCMP(one
, two
, ==, !=, 0);
2779 FCMP(one
, one
, !=, ==, 1);
2780 /* Non-equality is a bit special. */
2781 if (!fcompare (nan
, nan
, 1))
2782 bug (nan
, nan
, !=, ==, 6);
2784 /* Relational tests on numbers. */
2785 FCMP(two
, one
, <, >=, 2);
2786 FCMP(one
, two
, >=, <, 3);
2787 FCMP(one
, two
, >, <=, 4);
2788 FCMP(two
, one
, <=, >, 5);
2790 /* Relational tests on NaNs. Note that the inverse op here is
2791 always !=, there's no operator in C that is equivalent to !(a < b),
2792 when NaNs are involved, same for the other relational ops. */
2793 FCMP(nan
, nan
, <, !=, 2);
2794 FCMP(nan
, nan
, >=, !=, 3);
2795 FCMP(nan
, nan
, >, !=, 4);
2796 FCMP(nan
, nan
, <=, !=, 5);
2799 double get100 () { return 100.0; }
2801 void callsave_test(void)
2803 #if defined __i386__ || defined __x86_64__ || defined __arm__
2804 int i
, s
; double *d
; double t
;
2805 s
= sizeof (double);
2806 printf ("callsavetest: %d\n", s
);
2807 d
= alloca (sizeof(double));
2809 /* x86-64 had a bug were the next call to get100 would evict
2810 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
2811 in int type, not pointer type. When alloca returns a pointer
2812 with the high 32 bit set (which is likely on x86-64) the access
2813 generates a segfault. */
2814 i
= d
[0] > get100 ();
2820 void bfa3(ptrdiff_t str_offset
)
2822 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset
);
2824 void bfa2(ptrdiff_t str_offset
)
2826 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset
);
2829 void bfa1(ptrdiff_t str_offset
)
2831 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset
);
2835 void builtin_frame_address_test(void)
2837 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
2839 char str
[] = "__builtin_frame_address";
2840 char *fp0
= __builtin_frame_address(0);
2842 printf("str: %s\n", str
);