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);
117 /* Line joining happens before tokenization, so the following
118 must be parsed as ellipsis. */
119 void funny_line_continuation (int, ..\
122 char via_volatile (char);
127 #define M1(a, b) (a) + (b)
131 #define glue(a, b) a ## b
132 #define xglue(a, b) glue(a, b)
133 #define HIGHLOW "hello"
134 #define LOW LOW ", world"
136 static int onetwothree
= 123;
137 #define onetwothree4 onetwothree
138 #define onetwothree xglue(onetwothree,4)
140 #define min(a, b) ((a) < (b) ? (a) : (b))
143 #define dprintf(level,...) printf(__VA_ARGS__)
146 /* gcc vararg macros */
147 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
149 #define MACRO_NOARGS()
165 #define __INT64_C(c) c ## LL
166 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
174 #define spin_lock(lock) do { } while (0)
175 #define wq_spin_lock spin_lock
176 #define TEST2() wq_spin_lock(a)
178 #define UINT_MAX ((unsigned) -1)
180 void intdiv_test(void)
182 printf("18/21=%u\n", 18/21);
183 printf("18%%21=%u\n", 18%21);
184 printf("41/21=%u\n", 41/21);
185 printf("41%%21=%u\n", 41%21);
186 printf("42/21=%u\n", 42/21);
187 printf("42%%21=%u\n", 42%21);
188 printf("43/21=%u\n", 43/21);
189 printf("43%%21=%u\n", 43%21);
190 printf("126/21=%u\n", 126/21);
191 printf("126%%21=%u\n", 126%21);
192 printf("131/21=%u\n", 131/21);
193 printf("131%%21=%u\n", 131%21);
194 printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX
/2+3)/2);
195 printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX
/2+3)%2);
197 printf("18/-21=%u\n", 18/-21);
198 printf("18%%-21=%u\n", 18%-21);
199 printf("41/-21=%u\n", 41/-21);
200 printf("41%%-21=%u\n", 41%-21);
201 printf("42/-21=%u\n", 42/-21);
202 printf("42%%-21=%u\n", 42%-21);
203 printf("43/-21=%u\n", 43/-21);
204 printf("43%%-21=%u\n", 43%-21);
205 printf("126/-21=%u\n", 126/-21);
206 printf("126%%-21=%u\n", 126%-21);
207 printf("131/-21=%u\n", 131/-21);
208 printf("131%%-21=%u\n", 131%-21);
209 printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX
/2+3)/-2);
210 printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX
/2+3)%-2);
212 printf("-18/21=%u\n", -18/21);
213 printf("-18%%21=%u\n", -18%21);
214 printf("-41/21=%u\n", -41/21);
215 printf("-41%%21=%u\n", -41%21);
216 printf("-42/21=%u\n", -42/21);
217 printf("-42%%21=%u\n", -42%21);
218 printf("-43/21=%u\n", -43/21);
219 printf("-43%%21=%u\n", -43%21);
220 printf("-126/21=%u\n", -126/21);
221 printf("-126%%21=%u\n", -126%21);
222 printf("-131/21=%u\n", -131/21);
223 printf("-131%%21=%u\n", -131%21);
224 printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX
/2+3))/2);
225 printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX
/2+3))%2);
227 printf("-18/-21=%u\n", -18/-21);
228 printf("-18%%-21=%u\n", -18%-21);
229 printf("-41/-21=%u\n", -41/-21);
230 printf("-41%%-21=%u\n", -41%-21);
231 printf("-42/-21=%u\n", -42/-21);
232 printf("-42%%-21=%u\n", -42%-21);
233 printf("-43/-21=%u\n", -43/-21);
234 printf("-43%%-21=%u\n", -43%-21);
235 printf("-126/-21=%u\n", -126/-21);
236 printf("-126%%-21=%u\n", -126%-21);
237 printf("-131/-21=%u\n", -131/-21);
238 printf("-131%%-21=%u\n", -131%-21);
239 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX
/2+3))/-2);
240 printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX
/2+3))%-2);
243 void macro_test(void)
245 printf("macro:\n");\f\v
247 printf("aaa=%d\n", AAA
);
249 printf("min=%d\n", min(1, min(2, -1)));
251 printf("s1=%s\n", glue(HIGH
, LOW
));
252 printf("s2=%s\n", xglue(HIGH
, LOW
));
253 printf("s3=%s\n", str("c"));
254 printf("s4=%s\n", str(a1
));
255 printf("B3=%d\n", B3
);
257 printf("onetwothree=%d\n", onetwothree
);
260 printf("A defined\n");
263 printf("B defined\n");
266 printf("A defined\n");
268 printf("A not defined\n");
271 printf("B defined\n");
273 printf("B not defined\n");
277 printf("A defined\n");
279 printf("B1 defined\n");
281 printf("B1 not defined\n");
284 printf("A not defined\n");
286 printf("B2 defined\n");
288 printf("B2 not defined\n");
293 printf("test true1\n");
296 printf("test true2\n");
299 printf("test true3\n");
302 printf("test trueA\n");
305 printf("test trueB\n");
321 printf("__LINE__ defined\n");
324 printf("__LINE__=%d __FILE__=%s\n",
328 printf("__LINE__=%d __FILE__=%s\n",
331 printf("__LINE__=%d __FILE__=%s\n",
333 #line 227 "tcctest.c"
336 /* not strictly preprocessor, but we test it there */
338 printf("__func__ = %s\n", __func__
);
339 dprintf(1, "vaarg=%d\n", 1);
341 dprintf1(1, "vaarg1\n");
342 dprintf1(1, "vaarg1=%d\n", 2);
343 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
346 printf("func='%s'\n", __FUNCTION__
);
348 /* complicated macros in glibc */
349 printf("INT64_MIN=" LONG_LONG_FORMAT
"\n", INT64_MIN
);
359 /* macro function with argument outside the macro string */
360 #define MF_s MF_hello
361 #define MF_hello(msg) printf("%s\n",msg)
363 #define MF_t printf("tralala\n"); MF_hello
368 /* test macro substituion inside args (should not eat stream) */
369 printf("qq=%d\n", qq(qq
)(2));
371 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
372 null argument without a space. gcc 3.2 fixes that. */
375 printf("qq1=%d\n", qq1( ));
377 /* comment with stray handling *\
379 /* this is a valid *\/ comment */
380 /* this is a valid comment *\*/
384 /* test function macro substitution when the function name is
388 /* And again when the name and parenthes are separated by a
390 TEST2
/* the comment */ ();
394 static void print_num(char *fn
, int line
, int num
) {
395 printf("fn %s, line %d, num %d\n", fn
, line
, num
);
398 void recursive_macro_test(void)
401 #define ELF32_ST_TYPE(val) ((val) & 0xf)
402 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
403 #define STB_WEAK 2 /* Weak symbol */
404 #define ELFW(type) ELF##32##_##type
405 printf("%d\n", ELFW(ST_INFO
)(STB_WEAK
, ELFW(ST_TYPE
)(123)));
409 #define print_num(x) print_num(__FILE__,__LINE__,x)
411 WRAP(print_num(123));
412 WRAP(WRAP(print_num(123)));
414 static struct recursive_macro
{ int rm_field
; } G
;
415 #define rm_field (G.rm_field)
416 printf("rm_field = %d\n", rm_field
);
417 printf("rm_field = %d\n", WRAP(rm_field
));
418 WRAP((printf("rm_field = %d %d\n", rm_field
, WRAP(rm_field
))));
435 void ps(const char *s
)
447 const char foo1_string
[] = "\
456 printf("\141\1423\143\n");/* dezdez test */
457 printf("\x41\x42\x43\x3a\n");
458 printf("c=%c\n", 'r');
459 printf("wc=%C 0x%lx %C\n", L
'a', L
'\x1234', L
'c');
460 printf("foo1_string='%s'\n", foo1_string
);
462 printf("wstring=%S\n", L
"abc");
463 printf("wstring=%S\n", L
"abc" L
"def" "ghi");
464 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
465 printf("L'\\377'=%d L'\\xff'=%d\n", L
'\377', L
'\xff');
469 while ((b
= b
+ 1) < 96) {
473 printf("fib=%d\n", fib(33));
475 while (b
!= 0x80000000) {
488 for(i
= 0; i
< 10;i
++)
498 /* c99 for loop init test */
499 for (size_t count
= 1; count
< 3; count
++)
500 printf("count=%d\n", count
);
501 printf("count = %d\n", count
);
503 /* break/continue tests */
515 /* break/continue tests */
527 for(i
= 0;i
< 10;i
++) {
535 typedef int typedef_and_label
;
540 static void *label_table
[3] = { &&label1
, &&label2
, &&label3
};
544 /* This needs to parse as label, not as start of decl. */
555 /* we also test computed gotos (GCC extension) */
557 goto *label_table
[i
];
585 printf("enum:\n%d %d %d %d %d %d\n",
586 E0
, E1
, E2
, E3
, E4
, E5
);
588 printf("b1=%d\n", b1
);
604 printf("typedef:\n");
605 printf("a=%d\n", *a
);
607 printf("mytype2=%d\n", mytype2
);
612 printf("forward:\n");
618 void forward_ref(void)
620 printf("forward ok\n");
623 typedef struct struct1
{
643 struct struct1 st1
, st2
;
645 int main(int argc
, char **argv
)
650 recursive_macro_test();
664 constant_expr_test();
668 compound_literal_test();
670 struct_assign_test();
680 old_style_function();
685 statement_expr_test();
693 cmp_comparison_test();
696 builtin_frame_address_test();
698 if (via_volatile (42) != 42)
699 printf ("via_volatile broken\n");
710 printf("g1=%d\n", g
);
718 printf("g2=%d\n", g
);
722 printf("g3=%d\n", g
);
726 printf("g4=%d\n", g
);
729 printf("g5=%d\n", g
);
737 printf("sizeof(a) = %d\n", sizeof(a
));
738 printf("sizeof(\"a\") = %d\n", sizeof("a"));
740 printf("sizeof(__func__) = %d\n", sizeof(__func__
));
742 printf("sizeof tab %d\n", sizeof(tab
));
743 printf("sizeof tab2 %d\n", sizeof tab2
);
747 printf("%d %d %d\n", tab
[0], tab
[1], tab
[2]);
750 tab2
[i
][j
] = 10 * i
+ j
;
752 printf(" %3d", ((int *)tab2
)[i
]);
755 printf("sizeof(size_t)=%d\n", sizeof(size_t));
756 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
763 printf("%d\n", a
+= 1);
764 printf("%d\n", a
-= 2);
765 printf("%d\n", a
*= 31232132);
766 printf("%d\n", a
/= 4);
767 printf("%d\n", a
%= 20);
768 printf("%d\n", a
&= 6);
769 printf("%d\n", a
^= 7);
770 printf("%d\n", a
|= 8);
771 printf("%d\n", a
>>= 3);
772 printf("%d\n", a
<<= 4);
776 printf("%d\n", a
+ 1);
777 printf("%d\n", a
- 2);
778 printf("%d\n", a
* 312);
779 printf("%d\n", a
/ 4);
780 printf("%d\n", b
/ 4);
781 printf("%d\n", (unsigned)b
/ 4);
782 printf("%d\n", a
% 20);
783 printf("%d\n", b
% 20);
784 printf("%d\n", (unsigned)b
% 20);
785 printf("%d\n", a
& 6);
786 printf("%d\n", a
^ 7);
787 printf("%d\n", a
| 8);
788 printf("%d\n", a
>> 3);
789 printf("%d\n", b
>> 3);
790 printf("%d\n", (unsigned)b
>> 3);
791 printf("%d\n", a
<< 4);
796 printf("%d\n", 12 + 1);
797 printf("%d\n", 12 - 2);
798 printf("%d\n", 12 * 312);
799 printf("%d\n", 12 / 4);
800 printf("%d\n", 12 % 20);
801 printf("%d\n", 12 & 6);
802 printf("%d\n", 12 ^ 7);
803 printf("%d\n", 12 | 8);
804 printf("%d\n", 12 >> 2);
805 printf("%d\n", 12 << 4);
809 printf("%d %d %d %d\n",
818 return (c
>= 'a' & c
<= 'z') | (c
>= 'A' & c
<= 'Z') | c
== '_';
821 /**********************/
823 int vstack
[10], *vstack_ptr
;
825 void vpush(int vt
, int vc
)
831 void vpop(int *ft
, int *fc
)
844 vstack_ptr
[-2] &= ~0xffffff80;
846 printf("res= %d %d\n", a
, b
);
849 void constant_expr_test()
852 printf("constant_expr:\n");
854 printf("%d\n", a
* 16);
855 printf("%d\n", a
* 1);
856 printf("%d\n", a
+ 0);
866 printf("expr_ptr:\n");
869 printf("diff=%d\n", q
- p
);
871 printf("inc=%d\n", p
- tab4
);
873 printf("dec=%d\n", p
- tab4
);
875 printf("inc=%d\n", p
- tab4
);
877 printf("dec=%d\n", p
- tab4
);
878 printf("add=%d\n", p
+ 3 - tab4
);
879 printf("add=%d\n", 3 + p
- tab4
);
881 /* check if 64bit support is ok */
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
);
889 printf("%p %p %ld\n", q
, p
, p
-q
);
890 printf("%d %d %d %d %d %d\n",
891 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
892 p
= (int *)((char *)p
+ 0xf0000000);
893 printf("%p %p %ld\n", q
, p
, p
-q
);
894 printf("%d %d %d %d %d %d\n",
895 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
897 printf("%p %p %ld\n", q
, p
, p
-q
);
898 printf("%d %d %d %d %d %d\n",
899 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
904 struct size12 s
[2], *sp
= s
;
909 printf("%d\n", sp
[j
].i
);
916 printf("constant_expr:\n");
919 printf("%d\n", a
== a
);
920 printf("%d\n", a
!= a
);
922 printf("%d\n", a
< b
);
923 printf("%d\n", a
<= b
);
924 printf("%d\n", a
<= a
);
925 printf("%d\n", b
>= a
);
926 printf("%d\n", a
>= a
);
927 printf("%d\n", b
> a
);
929 printf("%d\n", (unsigned)a
< b
);
930 printf("%d\n", (unsigned)a
<= b
);
931 printf("%d\n", (unsigned)a
<= a
);
932 printf("%d\n", (unsigned)b
>= a
);
933 printf("%d\n", (unsigned)a
>= a
);
934 printf("%d\n", (unsigned)b
> a
);
963 printf("sizes: %d %d %d %d\n",
964 sizeof(struct struct1
),
965 sizeof(struct struct2
),
966 sizeof(union union1
),
967 sizeof(union union2
));
971 printf("st1: %d %d %d\n",
972 st1
.f1
, st1
.f2
, st1
.f3
);
975 printf("union1: %d\n", st1
.u
.v1
);
978 printf("union2: %d\n", u
.w1
);
983 printf("st2: %d %d %d\n",
984 s
->f1
, s
->f2
, s
->f3
);
985 printf("str_addr=%x\n", (int)st1
.str
- (int)&st1
.f1
);
987 /* align / size tests */
988 printf("aligntest1 sizeof=%d alignof=%d\n",
989 sizeof(struct aligntest1
), __alignof__(struct aligntest1
));
990 printf("aligntest2 sizeof=%d alignof=%d\n",
991 sizeof(struct aligntest2
), __alignof__(struct aligntest2
));
992 printf("aligntest3 sizeof=%d alignof=%d\n",
993 sizeof(struct aligntest3
), __alignof__(struct aligntest3
));
994 printf("aligntest4 sizeof=%d alignof=%d\n",
995 sizeof(struct aligntest4
), __alignof__(struct aligntest4
));
997 /* empty structures (GCC extension) */
998 printf("sizeof(struct empty) = %d\n", sizeof(struct empty
));
999 printf("alignof(struct empty) = %d\n", __alignof__(struct empty
));
1002 /* XXX: depend on endianness */
1003 void char_short_test()
1007 printf("char_short:\n");
1011 printf("s8=%d %d\n",
1012 *(char *)&var1
, *(char *)&var2
);
1013 printf("u8=%d %d\n",
1014 *(unsigned char *)&var1
, *(unsigned char *)&var2
);
1015 printf("s16=%d %d\n",
1016 *(short *)&var1
, *(short *)&var2
);
1017 printf("u16=%d %d\n",
1018 *(unsigned short *)&var1
, *(unsigned short *)&var2
);
1019 printf("s32=%d %d\n",
1020 *(int *)&var1
, *(int *)&var2
);
1021 printf("u32=%d %d\n",
1022 *(unsigned int *)&var1
, *(unsigned int *)&var2
);
1023 *(char *)&var1
= 0x08;
1024 printf("var1=%x\n", var1
);
1025 *(short *)&var1
= 0x0809;
1026 printf("var1=%x\n", var1
);
1027 *(int *)&var1
= 0x08090a0b;
1028 printf("var1=%x\n", var1
);
1031 /******************/
1033 typedef struct Sym
{
1041 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1042 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1044 static int toupper1(int a
)
1051 int *s
, a
, b
, t
, f
, i
;
1055 printf("!s=%d\n", !s
);
1059 printf("a=%d\n", a
);
1061 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1062 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1063 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1078 printf("b=%d\n", a
+ (0 ? 1 : a
/ 2));
1080 /* test register spilling */
1083 a
= (a
+ b
) * ((a
< b
) ?
1084 ((b
- a
) * (a
- b
)): a
+ b
);
1085 printf("a=%d\n", a
);
1087 /* test complex || or && expressions */
1091 printf("exp=%d\n", f
== (32 <= a
&& a
<= 3));
1092 printf("r=%d\n", (t
|| f
) + (t
&& f
));
1097 int aspect_native
= 65536;
1098 double bfu_aspect
= 1.0;
1100 for(aspect_on
= 0; aspect_on
< 2; aspect_on
++) {
1101 aspect
=aspect_on
?(aspect_native
*bfu_aspect
+0.5):65535UL;
1102 printf("aspect=%d\n", aspect
);
1106 /* test ? : GCC extension */
1108 static int v1
= 34 ? : -1; /* constant case */
1109 static int v2
= 0 ? : -1; /* constant case */
1112 printf("%d %d\n", v1
, v2
);
1113 printf("%d %d\n", a
- 30 ? : a
* 2, a
+ 1 ? : a
* 2);
1116 /* again complex expression */
1117 for(i
=0;i
<256;i
++) {
1118 if (toupper1 (i
) != TOUPPER (i
))
1119 printf("error %d\n", i
);
1123 /* GCC accepts that */
1124 static int tab_reinit
[];
1125 static int tab_reinit
[10];
1127 //int cinit1; /* a global variable can be defined several times without error ! */
1131 int *cinit2
= (int []){3, 2, 1};
1133 void compound_literal_test(void)
1138 printf("compound_test:\n");
1140 p
= (int []){1, 2, 3};
1142 printf(" %d", p
[i
]);
1146 printf("%d", cinit2
[i
]);
1150 printf("q1=%s\n", q
);
1152 q
= (char *){ "tralala2" };
1153 printf("q2=%s\n", q
);
1156 printf("q3=%s\n", q3
);
1158 q
= (char []){ "tralala3" };
1159 printf("q4=%s\n", q
);
1162 p
= (int []){1, 2, cinit1
+ 3};
1164 printf(" %d", p
[i
]);
1168 p
= (int []){1, 2, 4 + i
};
1169 printf("%d %d %d\n",
1191 printf("kr_test:\n");
1192 printf("func1=%d\n", kr_func1(3, 4));
1193 printf("func2=%d\n", kr_func2(3, 4));
1200 tab
= (char*)malloc(20);
1217 /* structure assignment tests */
1223 struct structa1 ssta1
;
1225 void struct_assign_test1(struct structa1 s1
, int t
, float f
)
1227 printf("%d %d %d %f\n", s1
.f1
, s1
.f2
, t
, f
);
1230 struct structa1
struct_assign_test2(struct structa1 s1
, int t
)
1237 void struct_assign_test(void)
1240 struct structa1 lsta1
, lsta2
;
1247 printf("struct_assign_test:\n");
1251 printf("%d %d\n", s
.lsta1
.f1
, s
.lsta1
.f2
);
1253 printf("%d %d\n", s
.lsta2
.f1
, s
.lsta2
.f2
);
1258 struct_assign_test1(ps
->lsta2
, 3, 4.5);
1260 printf("before call: %d %d\n", s
.lsta2
.f1
, s
.lsta2
.f2
);
1261 ps
->lsta2
= struct_assign_test2(ps
->lsta2
, ps
->i
);
1262 printf("after call: %d %d\n", ps
->lsta2
.f1
, ps
->lsta2
.f2
);
1267 /* XXX: we should allow this even without braces */
1268 { struct_assign_test
}
1270 printf("%d\n", struct_assign_test
== t
[0].elem
);
1273 /* casts to short/char */
1275 void cast1(char a
, short b
, unsigned char c
, unsigned short d
)
1277 printf("%d %d %d %d\n", a
, b
, c
, d
);
1291 p
-= 0x700000000042;
1293 printf("cast_test:\n");
1297 printf("%d %d %d %d\n",
1300 (unsigned char)(a
+ 1),
1301 (unsigned short)(a
+ 1));
1302 printf("%d %d %d %d\n",
1305 (unsigned char)0xfffff,
1306 (unsigned short)0xfffff);
1308 a
= (bcast
= 128) + 1;
1310 a
= (scast
= 65536) + 1;
1313 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
1315 /* test cast from unsigned to signed short to int */
1318 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b
, d
);
1321 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b
, d
);
1323 /* test implicit int casting for array accesses */
1327 printf("%d %d\n", tab
[0], tab
[1]);
1329 /* test implicit casting on some operators */
1330 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1331 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1332 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1334 /* from pointer to integer types */
1335 printf("%d %d %ld %ld %lld %lld\n",
1336 (int)p
, (unsigned int)p
,
1337 (long)p
, (unsigned long)p
,
1338 (long long)p
, (unsigned long long)p
);
1340 /* from integers to pointers */
1341 printf("%p %p %p %p\n",
1342 (void *)a
, (void *)b
, (void *)c
, (void *)d
);
1345 /* initializers tests */
1346 struct structinit1
{
1355 int sinit3
[3] = { 1, 2, {{3}}, };
1356 int sinit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1357 int sinit5
[3][2] = { 1, 2, 3, 4, 5, 6 };
1358 int sinit6
[] = { 1, 2, 3 };
1359 int sinit7
[] = { [2] = 3, [0] = 1, 2 };
1360 char sinit8
[] = "hello" "trala";
1362 struct structinit1 sinit9
= { 1, 2, 3 };
1363 struct structinit1 sinit10
= { .f2
= 2, 3, .f1
= 1 };
1364 struct structinit1 sinit11
= { .f2
= 2, 3, .f1
= 1,
1372 char *sinit12
= "hello world";
1378 char sinit14
[10] = { "abc" };
1379 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
1381 struct { int a
[3], b
; } sinit16
[] = { { 1 }, 2 };
1397 struct complexinit0
{
1402 struct complexinit
{
1404 const struct complexinit0
*b
;
1407 const static struct complexinit cix
[] = {
1410 .b
= (const struct complexinit0
[]) {
1418 struct complexinit2
{
1423 struct complexinit2 cix20
;
1425 struct complexinit2 cix21
= {
1427 .b
= { 3001, 3002, 3003 }
1430 struct complexinit2 cix22
= {
1432 .b
= { 4001, 4002, 4003, 4004, 4005, 4006 }
1435 void init_test(void)
1439 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1440 int linit6
[] = { 1, 2, 3 };
1442 char linit8
[] = "hello" "trala";
1443 int linit12
[10] = { 1, 2 };
1444 int linit13
[10] = { 1, 2, [7] = 3, [3] = 4, };
1445 char linit14
[10] = "abc";
1446 int linit15
[10] = { linit1
, linit1
+ 1, [6] = linit1
+ 2, };
1447 struct linit16
{ int a1
, a2
, a3
, a4
; } linit16
= { 1, .a3
= 2 };
1448 int linit17
= sizeof(linit17
);
1450 printf("init_test:\n");
1452 printf("sinit1=%d\n", sinit1
);
1453 printf("sinit2=%d\n", sinit2
);
1454 printf("sinit3=%d %d %d %d\n",
1460 printf("sinit6=%d\n", sizeof(sinit6
));
1461 printf("sinit7=%d %d %d %d\n",
1467 printf("sinit8=%s\n", sinit8
);
1468 printf("sinit9=%d %d %d\n",
1473 printf("sinit10=%d %d %d\n",
1478 printf("sinit11=%d %d %d %d %d %d\n",
1489 printf("[%d][%d] = %d %d %d\n",
1490 i
, j
, sinit4
[i
][j
], sinit5
[i
][j
], linit4
[i
][j
]);
1491 printf("linit1=%d\n", linit1
);
1492 printf("linit2=%d\n", linit2
);
1493 printf("linit6=%d\n", sizeof(linit6
));
1494 printf("linit8=%d %s\n", sizeof(linit8
), linit8
);
1496 printf("sinit12=%s\n", sinit12
);
1497 printf("sinit13=%d %s %s %s\n",
1502 printf("sinit14=%s\n", sinit14
);
1504 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
1506 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
1508 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
1510 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
1512 printf("%d %d %d %d\n",
1517 /* test that initialisation is done after variable declare */
1518 printf("linit17=%d\n", linit17
);
1519 printf("sinit15=%d\n", sinit15
[0]);
1520 printf("sinit16=%d %d\n", sinit16
[0].a
[0], sinit16
[1].a
[0]);
1521 printf("sinit17=%s %d %s %d\n",
1522 sinit17
[0].s
, sinit17
[0].len
,
1523 sinit17
[1].s
, sinit17
[1].len
);
1525 printf("%x ", sinit18
[i
]);
1527 /* complex init check */
1528 printf("cix: %d %d %d %d %d %d %d\n",
1530 cix
[0].b
[0].a
, cix
[0].b
[0].b
,
1531 cix
[0].b
[1].a
, cix
[0].b
[1].b
,
1532 cix
[0].b
[2].a
, cix
[0].b
[2].b
);
1533 printf("cix2: %d %d\n", cix21
.b
[2], cix22
.b
[5]);
1534 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20
, sizeof cix21
, sizeof cix22
);
1562 /* ISOC99 _Bool type */
1563 void c99_bool_test(void)
1569 printf("bool_test:\n");
1570 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
1572 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
1574 printf("b = %d\n", b
);
1576 printf("b = %d\n", b
);
1580 void bitfield_test(void)
1592 unsigned int f5
: 7;
1594 printf("bitfield_test:");
1595 printf("sizeof(st1) = %d\n", sizeof(st1
));
1604 printf("%d %d %d %d %d\n",
1605 st1
.f1
, st1
.f2
, st1
.f3
, st1
.f4
, st1
.f5
);
1608 printf("%d %d\n", sa
, ca
);
1612 printf("st1.f1 == -1\n");
1614 printf("st1.f1 != -1\n");
1616 printf("st1.f2 == -1\n");
1618 printf("st1.f2 != -1\n");
1620 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1621 long-long bitfields whose size is not bigger than int */
1626 unsigned long long f3
: 38;
1628 st2
.f1
= 0x123456789ULL
;
1630 st2
.f2
= (long long)a
<< 25;
1633 printf("%lld %lld %lld\n", st2
.f1
, st2
.f2
, st2
.f3
);
1637 #define FLOAT_FMT "%f\n"
1639 /* x86's float isn't compatible with GCC */
1640 #define FLOAT_FMT "%.5f\n"
1643 /* declare strto* functions as they are C99 */
1644 double strtod(const char *nptr
, char **endptr
);
1647 float strtof(const char *nptr
, char **endptr
) {return (float)strtod(nptr
, endptr
);}
1648 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
) {return (LONG_DOUBLE
)strtod(nptr
, endptr
);}
1650 float strtof(const char *nptr
, char **endptr
);
1651 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
);
1654 #define FTEST(prefix, typename, type, fmt)\
1655 void prefix ## cmp(type a, type b)\
1657 printf("%d %d %d %d %d %d\n",\
1664 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1672 printf(fmt "\n", ++a);\
1673 printf(fmt "\n", a++);\
1674 printf(fmt "\n", a);\
1676 printf("%d %d\n", !a, !b);\
1678 void prefix ## fcast(type a)\
1686 unsigned long long llua;\
1691 printf("ftof: %f %f %Lf\n", fa, da, la);\
1693 llia = (long long)a;\
1694 a = (a >= 0) ? a : -a;\
1695 ua = (unsigned int)a;\
1696 llua = (unsigned long long)a;\
1697 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
1700 llia = -0x123456789012345LL;\
1701 llua = 0xf123456789012345LLU;\
1703 printf("itof: " fmt "\n", b);\
1705 printf("utof: " fmt "\n", b);\
1707 printf("lltof: " fmt "\n", b);\
1709 printf("ulltof: " fmt "\n", b);\
1712 float prefix ## retf(type a) { return a; }\
1713 double prefix ## retd(type a) { return a; }\
1714 LONG_DOUBLE prefix ## retld(type a) { return a; }\
1716 void prefix ## call(void)\
1718 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1719 printf("double: %f\n", prefix ## retd(42.123456789));\
1720 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1721 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
1724 void prefix ## signed_zeros(void) \
1726 type x = 0.0, y = -0.0, n, p;\
1728 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
1729 1.0 / x != 1.0 / y);\
1731 printf ("x != y; this is wrong!\n");\
1735 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
1736 1.0 / x != 1.0 / n);\
1738 printf ("x != -x; this is wrong!\n");\
1742 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
1743 1.0 / x != 1.0 / p);\
1745 printf ("x != +y; this is wrong!\n");\
1748 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
1749 1.0 / x != 1.0 / p);\
1751 printf ("x != -y; this is wrong!\n");\
1753 void prefix ## test(void)\
1755 printf("testing '%s'\n", #typename);\
1756 prefix ## cmp(1, 2.5);\
1757 prefix ## cmp(2, 1.5);\
1758 prefix ## cmp(1, 1);\
1759 prefix ## fcast(234.6);\
1760 prefix ## fcast(-2334.6);\
1762 prefix ## signed_zeros();\
1765 FTEST(f
, float, float, "%f")
1766 FTEST(d
, double, double, "%f")
1767 FTEST(ld
, long double, LONG_DOUBLE
, "%Lf")
1769 double ftab1
[3] = { 1.2, 3.4, -5.6 };
1772 void float_test(void)
1774 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
1780 printf("float_test:\n");
1781 printf("sizeof(float) = %d\n", sizeof(float));
1782 printf("sizeof(double) = %d\n", sizeof(double));
1783 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE
));
1787 printf("%f %f %f\n", ftab1
[0], ftab1
[1], ftab1
[2]);
1788 printf("%f %f %f\n", 2.12, .5, 2.3e10
);
1789 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1791 printf("da=%f\n", da
);
1793 printf("fa=%f\n", fa
);
1796 printf("da = %f\n", da
);
1799 printf("db = %f\n", db
);
1808 return fib(n
-1) + fib(n
-2);
1820 printf("funcptr:\n");
1827 /* more complicated pointer computation */
1830 printf("sizeof1 = %d\n", sizeof(funcptr_test
));
1831 printf("sizeof2 = %d\n", sizeof funcptr_test
);
1832 printf("sizeof3 = %d\n", sizeof(&funcptr_test
));
1833 printf("sizeof4 = %d\n", sizeof &funcptr_test
);
1836 void lloptest(long long a
, long long b
)
1838 unsigned long long ua
, ub
;
1843 printf("arith: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
1849 printf("arith1: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
1855 printf("bin: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
1861 printf("test: %d %d %d %d %d %d\n",
1869 printf("utest: %d %d %d %d %d %d\n",
1880 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
1881 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
++, b
++);
1882 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", --a
, --b
);
1883 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
1885 printf("not: %d %d %d %d\n", !a
, !ua
, !b
, !ub
);
1888 void llshift(long long a
, int b
)
1890 printf("shift: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
1891 (unsigned long long)a
>> b
,
1894 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
1895 (unsigned long long)a
>> 3,
1898 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
1899 (unsigned long long)a
>> 35,
1909 long long la
, lb
, lc
;
1910 unsigned long long ula
, ulb
, ulc
;
1913 la
= (la
<< 20) | 0x12345;
1915 printf("la=" LONG_LONG_FORMAT
" ula=" ULONG_LONG_FORMAT
"\n", la
, ula
);
1920 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
1925 printf("ftoll: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", la
, lb
, lc
);
1930 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
1935 printf("ftoull: " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
"\n", ula
, ulb
, ulc
);
1938 long long llfunc1(int a
)
1948 long long int value(struct S
*v
)
1950 return ((long long int)v
->item
);
1953 void longlong_test(void)
1958 printf("longlong_test:\n");
1959 printf("sizeof(long long) = %d\n", sizeof(long long));
1964 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
1965 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %Lx\n",
1969 0x1234567812345679);
1971 printf(LONG_LONG_FORMAT
"\n", a
);
1974 lloptest(0xff, 0x1234);
1975 b
= 0x72345678 << 10;
1979 b
= 0x72345678LL
<< 10;
1990 /* long long reg spill test */
1995 printf("%lld\n", value(&a
));
1997 lloptest(0x80000000, 0);
1999 /* another long long spill test */
2005 printf("%lld\n", *p
);
2010 printf("%d %d %d %d\n", a
> b
, a
< b
, a
>= b
, a
<= b
);
2012 printf(LONG_LONG_FORMAT
"\n", 0x123456789LLU
);
2015 void manyarg_test(void)
2017 LONG_DOUBLE ld
= 1234567891234LL;
2018 printf("manyarg_test:\n");
2019 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2020 1, 2, 3, 4, 5, 6, 7, 8,
2021 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2022 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2023 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2024 1, 2, 3, 4, 5, 6, 7, 8,
2025 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2026 1234567891234LL, 987654321986LL,
2028 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2029 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2030 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2031 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2032 1234567891234LL, 987654321986LL,
2034 printf("%d %d %d %d %d %d %d %d %Lf\n",
2035 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2036 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2037 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"%f %f %Lf\n",
2038 1, 2, 3, 4, 5, 6, 7, 8,
2039 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2040 1234567891234LL, 987654321986LL,
2042 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2043 "%Lf " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f %Lf\n",
2044 1, 2, 3, 4, 5, 6, 7, 8,
2045 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2046 ld
, 1234567891234LL, 987654321986LL,
2050 void vprintf1(const char *fmt
, ...)
2074 i
= va_arg(ap
, int);
2078 d
= va_arg(ap
, double);
2082 ll
= va_arg(ap
, long long);
2083 printf(LONG_LONG_FORMAT
, ll
);
2086 ld
= va_arg(ap
, LONG_DOUBLE
);
2104 void stdarg_for_struct(struct myspace bob
, ...)
2106 struct myspace george
, bill
;
2111 bill
= va_arg(ap
, struct myspace
);
2112 george
= va_arg(ap
, struct myspace
);
2113 validate
= va_arg(ap
, int);
2114 printf("stdarg_for_struct: %d %d %d %d\n",
2115 bob
.profile
, bill
.profile
, george
.profile
, validate
);
2119 void stdarg_test(void)
2121 LONG_DOUBLE ld
= 1234567891234LL;
2124 vprintf1("%d %d %d\n", 1, 2, 3);
2125 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2126 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2127 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2128 vprintf1("%d %f %l %F %d %f %l %F\n",
2129 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2130 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2131 1, 2, 3, 4, 5, 6, 7, 8,
2132 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2133 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2134 1, 2, 3, 4, 5, 6, 7, 8,
2135 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2136 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2138 1, 2, 3, 4, 5, 6, 7, 8,
2139 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2140 1234567891234LL, 987654321986LL,
2142 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2144 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2145 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2146 1234567891234LL, 987654321986LL,
2148 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2149 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2150 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2152 1, 2, 3, 4, 5, 6, 7, 8,
2153 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2154 1234567891234LL, 987654321986LL,
2156 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2157 "%F %l %l %f %f %F\n",
2158 1, 2, 3, 4, 5, 6, 7, 8,
2159 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2160 ld
, 1234567891234LL, 987654321986LL,
2164 stdarg_for_struct(bob
, bob
, bob
, bob
.profile
);
2167 void whitespace_test(void)
2173 ntf("whitspace:\n");\f\v
2177 #ifdef CORRECT_CR_HANDLING
2186 #ifdef ACCEPT_CR_IN_STRINGS
2187 printf("len1=%d\n", strlen("
2189 #ifdef CORRECT_CR_HANDLING
2192 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
2194 printf("len1=%d\n", strlen("
a
2196 #endif /* ACCEPT_CR_IN_STRINGS */
2199 int reltab
[3] = { 1, 2, 3 };
2201 int *rel1
= &reltab
[1];
2202 int *rel2
= &reltab
[2];
2204 void relocation_test(void)
2206 printf("*rel1=%d\n", *rel1
);
2207 printf("*rel2=%d\n", *rel2
);
2210 void old_style_f(a
,b
,c
)
2214 printf("a=%d b=%d b=%f\n", a
, b
, c
);
2217 void decl_func1(int cmpfn())
2219 printf("cmpfn=%lx\n", (long)cmpfn
);
2222 void decl_func2(cmpfn
)
2225 printf("cmpfn=%lx\n", (long)cmpfn
);
2228 void old_style_function(void)
2230 old_style_f((void *)1, 2, 3.0);
2237 #if defined __i386__ || defined __x86_64__ || defined __arm__
2238 char *p
= alloca(16);
2239 strcpy(p
,"123456789012345");
2240 printf("alloca: p is %s\n", p
);
2241 char *demo
= "This is only a test.\n";
2242 /* Test alloca embedded in a larger expression */
2243 printf("alloca: %s\n", strcpy(alloca(strlen(demo
)+1),demo
) );
2247 void *bounds_checking_is_enabled()
2249 char ca
[10], *cp
= ca
-1;
2250 return (ca
!= cp
+ 1) ? cp
: NULL
;
2253 typedef int constant_negative_array_size_as_compile_time_assertion_idiom
[(1 ? 2 : 0) - 1];
2255 void c99_vla_test(int size1
, int size2
)
2257 #if defined __i386__ || defined __x86_64__
2258 int size
= size1
* size2
;
2259 int tab1
[size
][2], tab2
[10][2];
2260 void *tab1_ptr
, *tab2_ptr
, *bad_ptr
;
2262 /* "size" should have been 'captured' at tab1 declaration,
2263 so modifying it should have no effect on VLA behaviour. */
2266 printf("Test C99 VLA 1 (sizeof): ");
2267 printf("%s\n", (sizeof tab1
== size1
* size2
* 2 * sizeof(int)) ? "PASSED" : "FAILED");
2270 printf("Test C99 VLA 2 (ptrs subtract): ");
2271 printf("%s\n", (tab2
- tab1
== (tab2_ptr
- tab1_ptr
) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2272 printf("Test C99 VLA 3 (ptr add): ");
2273 printf("%s\n", &tab1
[5][1] == (tab1_ptr
+ (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2274 printf("Test C99 VLA 4 (ptr access): ");
2275 tab1
[size1
][1] = 42;
2276 printf("%s\n", (*((int *) (tab1_ptr
+ (size1
* 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2278 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2279 if (bad_ptr
= bounds_checking_is_enabled()) {
2280 int *t1
= &tab1
[size1
* size2
- 1][3];
2281 int *t2
= &tab2
[9][3];
2282 printf("%s ", bad_ptr
== t1
? "PASSED" : "FAILED");
2283 printf("%s ", bad_ptr
== t2
? "PASSED" : "FAILED");
2285 char*c1
= 1 + sizeof(tab1
) + (char*)tab1
;
2286 char*c2
= 1 + sizeof(tab2
) + (char*)tab2
;
2287 printf("%s ", bad_ptr
== c1
? "PASSED" : "FAILED");
2288 printf("%s ", bad_ptr
== c2
? "PASSED" : "FAILED");
2292 printf("%s ", bad_ptr
== i1
? "PASSED" : "FAILED");
2293 printf("%s ", bad_ptr
== i2
? "PASSED" : "FAILED");
2295 int *x1
= tab1
[size1
* size2
+ 1];
2296 int *x2
= tab2
[10 + 1];
2297 printf("%s ", bad_ptr
== x1
? "PASSED" : "FAILED");
2298 printf("%s ", bad_ptr
== x2
? "PASSED" : "FAILED");
2300 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2307 typedef __SIZE_TYPE__
uintptr_t;
2310 void sizeof_test(void)
2315 printf("sizeof(int) = %d\n", sizeof(int));
2316 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2317 printf("sizeof(long) = %d\n", sizeof(long));
2318 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2319 printf("sizeof(short) = %d\n", sizeof(short));
2320 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2321 printf("sizeof(char) = %d\n", sizeof(char));
2322 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2323 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2325 printf("sizeof(a++) = %d\n", sizeof a
++);
2326 printf("a=%d\n", a
);
2328 printf("sizeof(**ptr) = %d\n", sizeof (**ptr
));
2330 /* The type of sizeof should be as large as a pointer, actually
2331 it should be size_t. */
2332 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2335 /* Effectively <<32, but defined also on 32bit machines. */
2339 /* This checks that sizeof really can be used to manipulate
2340 uintptr_t objects, without truncation. */
2341 t2
= t
& -sizeof(uintptr_t);
2342 printf ("%lu %lu\n", t
, t2
);
2344 /* some alignof tests */
2345 printf("__alignof__(int) = %d\n", __alignof__(int));
2346 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2347 printf("__alignof__(short) = %d\n", __alignof__(short));
2348 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2349 printf("__alignof__(char) = %d\n", __alignof__(char));
2350 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2351 printf("__alignof__(func) = %d\n", __alignof__
sizeof_test());
2354 void typeof_test(void)
2363 printf("a=%f b=%f c=%f\n", a
, b
, c
);
2366 void statement_expr_test(void)
2379 printf("a=%d\n", a
);
2383 void local_label_test(void)
2389 __label__ l1
, l2
, l3
, l4
;
2403 printf("a=%d\n", a
);
2413 /* inline assembler test */
2416 /* from linux kernel */
2417 static char * strncat1(char * dest
,const char * src
,size_t count
)
2420 __asm__
__volatile__(
2429 "testb %%al,%%al\n\t"
2431 "2:\txorl %2,%2\n\t"
2433 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
2434 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
2439 static char * strncat2(char * dest
,const char * src
,size_t count
)
2442 __asm__
__volatile__(
2443 "repne scasb\n\t" /* one-line repne prefix + string op */
2450 "testb %%al,%%al\n\t"
2452 "2:\txorl %2,%2\n\t"
2454 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
2455 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
2460 static inline void * memcpy1(void * to
, const void * from
, size_t n
)
2463 __asm__
__volatile__(
2468 "1:\ttestb $1,%b4\n\t"
2472 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
2473 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
2478 static inline void * memcpy2(void * to
, const void * from
, size_t n
)
2481 __asm__
__volatile__(
2482 "rep movsl\n\t" /* one-line rep prefix + string op */
2486 "1:\ttestb $1,%b4\n\t"
2490 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
2491 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
2496 static __inline__
void sigaddset1(unsigned int *set
, int _sig
)
2498 __asm__("btsl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
2501 static __inline__
void sigdelset1(unsigned int *set
, int _sig
)
2503 asm("btrl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
2506 static __inline__ __const__
unsigned int swab32(unsigned int x
)
2508 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2509 "rorl $16,%0\n\t" /* swap words */
2510 "xchgb %b0,%h0" /* swap higher bytes */
2516 static __inline__
unsigned long long mul64(unsigned int a
, unsigned int b
)
2518 unsigned long long res
;
2519 __asm__("mull %2" : "=A" (res
) : "a" (a
), "r" (b
));
2523 static __inline__
unsigned long long inc64(unsigned long long a
)
2525 unsigned long long res
;
2526 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res
) : "A" (a
));
2537 printf("inline asm:\n");
2538 /* test the no operand case */
2539 asm volatile ("xorl %eax, %eax");
2541 memcpy1(buf
, "hello", 6);
2542 strncat1(buf
, " worldXXXXX", 3);
2543 printf("%s\n", buf
);
2545 memcpy2(buf
, "hello", 6);
2546 strncat2(buf
, " worldXXXXX", 3);
2547 printf("%s\n", buf
);
2549 /* 'A' constraint test */
2550 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2551 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2554 sigdelset1(&set
, 2);
2555 sigaddset1(&set
, 16);
2556 /* NOTE: we test here if C labels are correctly restored after the
2560 __asm__("btsl %1,%0" : "=m"(set
) : "Ir"(20) : "cc");
2561 #ifdef __GNUC__ // works strange with GCC 4.3
2564 printf("set=0x%x\n", set
);
2566 printf("swab32(0x%08x) = 0x%0x\n", val
, swab32(val
));
2580 #define COMPAT_TYPE(type1, type2) \
2582 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2583 __builtin_types_compatible_p (type1, type2));\
2588 void builtin_test(void)
2591 COMPAT_TYPE(int, int);
2592 COMPAT_TYPE(int, unsigned int);
2593 COMPAT_TYPE(int, char);
2594 COMPAT_TYPE(int, const int);
2595 COMPAT_TYPE(int, volatile int);
2596 COMPAT_TYPE(int *, int *);
2597 COMPAT_TYPE(int *, void *);
2598 COMPAT_TYPE(int *, const int *);
2599 COMPAT_TYPE(char *, unsigned char *);
2600 COMPAT_TYPE(char *, signed char *);
2601 COMPAT_TYPE(char *, char *);
2602 /* space is needed because tcc preprocessor introduces a space between each token */
2603 COMPAT_TYPE(char * *, void *);
2605 printf("res = %d\n", __builtin_constant_p(1));
2606 printf("res = %d\n", __builtin_constant_p(1 + 2));
2607 printf("res = %d\n", __builtin_constant_p(&constant_p_var
));
2608 printf("res = %d\n", __builtin_constant_p(constant_p_var
));
2612 extern int __attribute__((weak
)) weak_f1(void);
2613 extern int __attribute__((weak
)) weak_f2(void);
2614 extern int weak_f3(void);
2615 extern int __attribute__((weak
)) weak_v1
;
2616 extern int __attribute__((weak
)) weak_v2
;
2619 extern int (*weak_fpa
)() __attribute__((weak
));
2620 extern int __attribute__((weak
)) (*weak_fpb
)();
2621 extern __attribute__((weak
)) int (*weak_fpc
)();
2623 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak
));
2624 extern int __attribute((weak
)) weak_asm_f2(void) asm("weak_asm_f2x") ;
2625 extern int __attribute((weak
)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak
));
2626 extern int weak_asm_v1
asm("weak_asm_v1x") __attribute((weak
));
2627 extern int __attribute((weak
)) weak_asm_v2
asm("weak_asm_v2x") ;
2628 extern int __attribute((weak
)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak
));
2630 static const size_t dummy
= 0;
2631 extern __typeof(dummy
) weak_dummy1
__attribute__((weak
, alias("dummy")));
2632 extern __typeof(dummy
) __attribute__((weak
, alias("dummy"))) weak_dummy2
;
2633 extern __attribute__((weak
, alias("dummy"))) __typeof(dummy
) weak_dummy3
;
2635 int some_lib_func(void);
2636 int dummy_impl_of_slf(void) { return 444; }
2637 int some_lib_func(void) __attribute__((weak
, alias("dummy_impl_of_slf")));
2639 int weak_toolate() __attribute__((weak
));
2640 int weak_toolate() { return 0; }
2642 void __attribute__((weak
)) weak_test(void)
2644 printf("weak_f1=%d\n", weak_f1
? weak_f1() : 123);
2645 printf("weak_f2=%d\n", weak_f2
? weak_f2() : 123);
2646 printf("weak_f3=%d\n", weak_f3
? weak_f3() : 123);
2647 printf("weak_v1=%d\n",&weak_v1
? weak_v1
: 123);
2648 printf("weak_v2=%d\n",&weak_v2
? weak_v2
: 123);
2649 printf("weak_v3=%d\n",&weak_v3
? weak_v3
: 123);
2651 printf("weak_fpa=%d\n",&weak_fpa
? weak_fpa() : 123);
2652 printf("weak_fpb=%d\n",&weak_fpb
? weak_fpb() : 123);
2653 printf("weak_fpc=%d\n",&weak_fpc
? weak_fpc() : 123);
2655 printf("weak_asm_f1=%d\n", weak_asm_f1
!= NULL
);
2656 printf("weak_asm_f2=%d\n", weak_asm_f2
!= NULL
);
2657 printf("weak_asm_f3=%d\n", weak_asm_f3
!= NULL
);
2658 printf("weak_asm_v1=%d\n",&weak_asm_v1
!= NULL
);
2659 printf("weak_asm_v2=%d\n",&weak_asm_v2
!= NULL
);
2660 printf("weak_asm_v3=%d\n",&weak_asm_v3
!= NULL
);
2663 int __attribute__((weak
)) weak_f2() { return 222; }
2664 int __attribute__((weak
)) weak_f3() { return 333; }
2665 int __attribute__((weak
)) weak_v2
= 222;
2666 int __attribute__((weak
)) weak_v3
= 333;
2669 void const_func(const int a
)
2673 void const_warn_test(void)
2682 int getme (struct condstruct
*s
, int i
)
2684 int i1
= (i
== 0 ? 0 : s
)->i
;
2685 int i2
= (i
== 0 ? s
: 0)->i
;
2686 int i3
= (i
== 0 ? (void*)0 : s
)->i
;
2687 int i4
= (i
== 0 ? s
: (void*)0)->i
;
2688 return i1
+ i2
+ i3
+ i4
;
2697 struct global_data global_data
;
2699 int global_data_getstuff (int *, int);
2701 void global_data_callit (int i
)
2703 *global_data
.b
[i
] = global_data_getstuff (global_data
.b
[i
], 1);
2706 int global_data_getstuff (int *p
, int i
)
2711 void global_data_test (void)
2713 global_data
.a
[0] = 42;
2714 global_data
.b
[0] = &global_data
.a
[0];
2715 global_data_callit (0);
2716 printf ("%d\n", global_data
.a
[0]);
2721 unsigned char fill
: 3;
2722 unsigned char b1
: 1;
2723 unsigned char b2
: 1;
2724 unsigned char fill2
: 3;
2727 int glob1
, glob2
, glob3
;
2729 void compare_comparisons (struct cmpcmpS
*s
)
2731 if (s
->b1
!= (glob1
== glob2
)
2732 || (s
->b2
!= (glob1
== glob3
)))
2733 printf ("comparing comparisons broken\n");
2736 void cmp_comparison_test(void)
2740 glob1
= 42; glob2
= 42;
2743 compare_comparisons (&s
);
2746 int fcompare (double a
, double b
, int code
)
2749 case 0: return a
== b
;
2750 case 1: return a
!= b
;
2751 case 2: return a
< b
;
2752 case 3: return a
>= b
;
2753 case 4: return a
> b
;
2754 case 5: return a
<= b
;
2758 void math_cmp_test(void)
2760 double nan
= 0.0/0.0;
2764 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
2766 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
2767 And it does this in various ways so that all code generation paths
2768 are checked (generating inverted tests, or non-inverted tests, or
2769 producing a 0/1 value without jumps (that's done in the fcompare
2771 #define FCMP(a,b,op,iop,code) \
2772 if (fcompare (a,b,code)) \
2773 bug (a,b,op,iop,1); \
2775 bug (a,b,op,iop,2); \
2779 bug (a,b,op,iop,3); \
2780 if ((a op b) || comp) \
2781 bug (a,b,op,iop,4); \
2782 if ((a iop b) || comp) \
2787 /* Equality tests. */
2788 FCMP(nan
, nan
, ==, !=, 0);
2789 FCMP(one
, two
, ==, !=, 0);
2790 FCMP(one
, one
, !=, ==, 1);
2791 /* Non-equality is a bit special. */
2792 if (!fcompare (nan
, nan
, 1))
2793 bug (nan
, nan
, !=, ==, 6);
2795 /* Relational tests on numbers. */
2796 FCMP(two
, one
, <, >=, 2);
2797 FCMP(one
, two
, >=, <, 3);
2798 FCMP(one
, two
, >, <=, 4);
2799 FCMP(two
, one
, <=, >, 5);
2801 /* Relational tests on NaNs. Note that the inverse op here is
2802 always !=, there's no operator in C that is equivalent to !(a < b),
2803 when NaNs are involved, same for the other relational ops. */
2804 FCMP(nan
, nan
, <, !=, 2);
2805 FCMP(nan
, nan
, >=, !=, 3);
2806 FCMP(nan
, nan
, >, !=, 4);
2807 FCMP(nan
, nan
, <=, !=, 5);
2810 double get100 () { return 100.0; }
2812 void callsave_test(void)
2814 #if defined __i386__ || defined __x86_64__ || defined __arm__
2815 int i
, s
; double *d
; double t
;
2816 s
= sizeof (double);
2817 printf ("callsavetest: %d\n", s
);
2818 d
= alloca (sizeof(double));
2820 /* x86-64 had a bug were the next call to get100 would evict
2821 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
2822 in int type, not pointer type. When alloca returns a pointer
2823 with the high 32 bit set (which is likely on x86-64) the access
2824 generates a segfault. */
2825 i
= d
[0] > get100 ();
2831 void bfa3(ptrdiff_t str_offset
)
2833 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset
);
2835 void bfa2(ptrdiff_t str_offset
)
2837 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset
);
2840 void bfa1(ptrdiff_t str_offset
)
2842 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset
);
2846 void builtin_frame_address_test(void)
2848 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
2850 char str
[] = "__builtin_frame_address";
2851 char *fp0
= __builtin_frame_address(0);
2853 printf("str: %s\n", str
);
2858 char via_volatile (char i
)