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.h"
51 #include TCCLIB_INC1.TCCLIB_INC2
53 #include TCCLIB_INC1.h>
63 /* Test two more ways to include a file named like a pp-number */
64 #define INC(name) <tests/name.h>
65 #define funnyname 42test.h
67 #define incname < incdir funnyname >
68 #define __stringify(x) #x
69 #define stringify(x) __stringify(x)
72 #include stringify(funnyname)
78 void recursive_macro_test();
93 void constant_expr_test();
95 void char_short_test();
97 void compound_literal_test(void);
99 void struct_assign_test(void);
100 void cast_test(void);
101 void bitfield_test(void);
102 void c99_bool_test(void);
103 void float_test(void);
104 void longlong_test(void);
105 void manyarg_test(void);
106 void stdarg_test(void);
107 void whitespace_test(void);
108 void relocation_test(void);
109 void old_style_function(void);
110 void alloca_test(void);
111 void c99_vla_test(int size1
, int size2
);
112 void sizeof_test(void);
113 void typeof_test(void);
114 void local_label_test(void);
115 void statement_expr_test(void);
117 void builtin_test(void);
118 void weak_test(void);
119 void global_data_test(void);
120 void cmp_comparison_test(void);
121 void math_cmp_test(void);
122 void callsave_test(void);
123 void builtin_frame_address_test(void);
124 void attrib_test(void);
128 void forward_ref(void);
131 /* Line joining happens before tokenization, so the following
132 must be parsed as ellipsis. */
133 void funny_line_continuation (int, ..\
136 char via_volatile (char);
141 #define M1(a, b) (a) + (b)
145 #define glue(a, b) a ## b
146 #define xglue(a, b) glue(a, b)
147 #define HIGHLOW "hello"
148 #define LOW LOW ", world"
150 static int onetwothree
= 123;
151 #define onetwothree4 onetwothree
152 #define onetwothree xglue(onetwothree,4)
154 #define min(a, b) ((a) < (b) ? (a) : (b))
157 #define dprintf(level,...) printf(__VA_ARGS__)
160 /* gcc vararg macros */
161 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
163 #define MACRO_NOARGS()
179 #define __INT64_C(c) c ## LL
180 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
188 #define spin_lock(lock) do { } while (0)
189 #define wq_spin_lock spin_lock
190 #define TEST2() wq_spin_lock(a)
192 #define UINT_MAX ((unsigned) -1)
194 void intdiv_test(void)
196 printf("18/21=%u\n", 18/21);
197 printf("18%%21=%u\n", 18%21);
198 printf("41/21=%u\n", 41/21);
199 printf("41%%21=%u\n", 41%21);
200 printf("42/21=%u\n", 42/21);
201 printf("42%%21=%u\n", 42%21);
202 printf("43/21=%u\n", 43/21);
203 printf("43%%21=%u\n", 43%21);
204 printf("126/21=%u\n", 126/21);
205 printf("126%%21=%u\n", 126%21);
206 printf("131/21=%u\n", 131/21);
207 printf("131%%21=%u\n", 131%21);
208 printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX
/2+3)/2);
209 printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX
/2+3)%2);
211 printf("18/-21=%u\n", 18/-21);
212 printf("18%%-21=%u\n", 18%-21);
213 printf("41/-21=%u\n", 41/-21);
214 printf("41%%-21=%u\n", 41%-21);
215 printf("42/-21=%u\n", 42/-21);
216 printf("42%%-21=%u\n", 42%-21);
217 printf("43/-21=%u\n", 43/-21);
218 printf("43%%-21=%u\n", 43%-21);
219 printf("126/-21=%u\n", 126/-21);
220 printf("126%%-21=%u\n", 126%-21);
221 printf("131/-21=%u\n", 131/-21);
222 printf("131%%-21=%u\n", 131%-21);
223 printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX
/2+3)/-2);
224 printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX
/2+3)%-2);
226 printf("-18/21=%u\n", -18/21);
227 printf("-18%%21=%u\n", -18%21);
228 printf("-41/21=%u\n", -41/21);
229 printf("-41%%21=%u\n", -41%21);
230 printf("-42/21=%u\n", -42/21);
231 printf("-42%%21=%u\n", -42%21);
232 printf("-43/21=%u\n", -43/21);
233 printf("-43%%21=%u\n", -43%21);
234 printf("-126/21=%u\n", -126/21);
235 printf("-126%%21=%u\n", -126%21);
236 printf("-131/21=%u\n", -131/21);
237 printf("-131%%21=%u\n", -131%21);
238 printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX
/2+3))/2);
239 printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX
/2+3))%2);
241 printf("-18/-21=%u\n", -18/-21);
242 printf("-18%%-21=%u\n", -18%-21);
243 printf("-41/-21=%u\n", -41/-21);
244 printf("-41%%-21=%u\n", -41%-21);
245 printf("-42/-21=%u\n", -42/-21);
246 printf("-42%%-21=%u\n", -42%-21);
247 printf("-43/-21=%u\n", -43/-21);
248 printf("-43%%-21=%u\n", -43%-21);
249 printf("-126/-21=%u\n", -126/-21);
250 printf("-126%%-21=%u\n", -126%-21);
251 printf("-131/-21=%u\n", -131/-21);
252 printf("-131%%-21=%u\n", -131%-21);
253 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX
/2+3))/-2);
254 printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX
/2+3))%-2);
257 void macro_test(void)
259 printf("macro:\n");\f\v
261 printf("aaa=%d\n", AAA
);
263 printf("min=%d\n", min(1, min(2, -1)));
265 printf("s1=%s\n", glue(HIGH
, LOW
));
266 printf("s2=%s\n", xglue(HIGH
, LOW
));
267 printf("s3=%s\n", str("c"));
268 printf("s4=%s\n", str(a1
));
269 printf("B3=%d\n", B3
);
271 printf("onetwothree=%d\n", onetwothree
);
274 printf("A defined\n");
277 printf("B defined\n");
280 printf("A defined\n");
282 printf("A not defined\n");
285 printf("B defined\n");
287 printf("B not defined\n");
291 printf("A defined\n");
293 printf("B1 defined\n");
295 printf("B1 not defined\n");
298 printf("A not defined\n");
300 printf("B2 defined\n");
302 printf("B2 not defined\n");
307 printf("test true1\n");
310 printf("test true2\n");
313 printf("test true3\n");
316 printf("test trueA\n");
319 printf("test trueB\n");
335 printf("__LINE__ defined\n");
338 printf("__LINE__=%d __FILE__=%s\n",
342 printf("__LINE__=%d __FILE__=%s\n",
345 printf("__LINE__=%d __FILE__=%s\n",
347 #line 227 "tcctest.c"
350 /* not strictly preprocessor, but we test it there */
352 printf("__func__ = %s\n", __func__
);
353 dprintf(1, "vaarg=%d\n", 1);
355 dprintf1(1, "vaarg1\n");
356 dprintf1(1, "vaarg1=%d\n", 2);
357 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
360 printf("func='%s'\n", __FUNCTION__
);
362 /* complicated macros in glibc */
363 printf("INT64_MIN=" LONG_LONG_FORMAT
"\n", INT64_MIN
);
373 /* macro function with argument outside the macro string */
374 #define MF_s MF_hello
375 #define MF_hello(msg) printf("%s\n",msg)
377 #define MF_t printf("tralala\n"); MF_hello
382 /* test macro substituion inside args (should not eat stream) */
383 printf("qq=%d\n", qq(qq
)(2));
385 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
386 null argument without a space. gcc 3.2 fixes that. */
389 printf("qq1=%d\n", qq1( ));
391 /* comment with stray handling *\
393 /* this is a valid *\/ comment */
394 /* this is a valid comment *\*/
398 /* test function macro substitution when the function name is
402 /* And again when the name and parenthes are separated by a
404 TEST2
/* the comment */ ();
406 printf("%s\n", get_basefile_from_header());
407 printf("%s\n", __BASE_FILE__
);
408 printf("%s\n", get_file_from_header());
409 printf("%s\n", __FILE__
);
411 /* Check that funnily named include was in fact included */
412 have_included_42test_h
= 1;
413 have_included_42test_h_second
= 1;
414 have_included_42test_h_third
= 1;
418 static void print_num(char *fn
, int line
, int num
) {
419 printf("fn %s, line %d, num %d\n", fn
, line
, num
);
422 void recursive_macro_test(void)
425 #define ELF32_ST_TYPE(val) ((val) & 0xf)
426 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
427 #define STB_WEAK 2 /* Weak symbol */
428 #define ELFW(type) ELF##32##_##type
429 printf("%d\n", ELFW(ST_INFO
)(STB_WEAK
, ELFW(ST_TYPE
)(123)));
433 #define print_num(x) print_num(__FILE__,__LINE__,x)
435 WRAP(print_num(123));
436 WRAP(WRAP(print_num(123)));
438 static struct recursive_macro
{ int rm_field
; } G
;
439 #define rm_field (G.rm_field)
440 printf("rm_field = %d\n", rm_field
);
441 printf("rm_field = %d\n", WRAP(rm_field
));
442 WRAP((printf("rm_field = %d %d\n", rm_field
, WRAP(rm_field
))));
459 void ps(const char *s
)
471 const char foo1_string
[] = "\
480 printf("\141\1423\143\n");/* dezdez test */
481 printf("\x41\x42\x43\x3a\n");
482 printf("c=%c\n", 'r');
483 printf("wc=%C 0x%lx %C\n", L
'a', L
'\x1234', L
'c');
484 printf("foo1_string='%s'\n", foo1_string
);
486 printf("wstring=%S\n", L
"abc");
487 printf("wstring=%S\n", L
"abc" L
"def" "ghi");
488 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
489 printf("L'\\377'=%d L'\\xff'=%d\n", L
'\377', L
'\xff');
493 while ((b
= b
+ 1) < 96) {
497 printf("fib=%d\n", fib(33));
499 while (b
!= 0x80000000) {
512 for(i
= 0; i
< 10;i
++)
522 /* c99 for loop init test */
523 for (size_t count
= 1; count
< 3; count
++)
524 printf("count=%d\n", count
);
525 printf("count = %d\n", count
);
527 /* break/continue tests */
539 /* break/continue tests */
551 for(i
= 0;i
< 10;i
++) {
559 typedef int typedef_and_label
;
564 static void *label_table
[3] = { &&label1
, &&label2
, &&label3
};
568 /* This needs to parse as label, not as start of decl. */
579 /* we also test computed gotos (GCC extension) */
581 goto *label_table
[i
];
607 enum {E6
= 42, E7
, E8
} e
:8;
612 /* The following should give no warning */
614 struct S_enum s
= {E7
};
615 printf("enum: %d\n", s
.e
);
616 printf("enum:\n%d %d %d %d %d %d\n",
617 E0
, E1
, E2
, E3
, E4
, E5
);
619 printf("b1=%d\n", b1
);
635 printf("typedef:\n");
636 printf("a=%d\n", *a
);
638 printf("mytype2=%d\n", mytype2
);
643 printf("forward:\n");
649 void forward_ref(void)
651 printf("forward ok\n");
654 typedef struct struct1
{
674 struct struct1 st1
, st2
;
681 int main(int argc
, char **argv
)
686 recursive_macro_test();
701 constant_expr_test();
705 compound_literal_test();
707 struct_assign_test();
717 old_style_function();
722 statement_expr_test();
730 cmp_comparison_test();
733 builtin_frame_address_test();
735 if (via_volatile (42) != 42)
736 printf ("via_volatile broken\n");
748 printf("g1=%d\n", g
);
756 printf("g2=%d\n", g
);
760 printf("g3=%d\n", g
);
764 printf("g4=%d\n", g
);
767 printf("g5=%d\n", g
);
775 printf("sizeof(a) = %d\n", sizeof(a
));
776 printf("sizeof(\"a\") = %d\n", sizeof("a"));
778 printf("sizeof(__func__) = %d\n", sizeof(__func__
));
780 printf("sizeof tab %d\n", sizeof(tab
));
781 printf("sizeof tab2 %d\n", sizeof tab2
);
785 printf("%d %d %d\n", tab
[0], tab
[1], tab
[2]);
788 tab2
[i
][j
] = 10 * i
+ j
;
790 printf(" %3d", ((int *)tab2
)[i
]);
793 printf("sizeof(size_t)=%d\n", sizeof(size_t));
794 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
801 printf("%d\n", a
+= 1);
802 printf("%d\n", a
-= 2);
803 printf("%d\n", a
*= 31232132);
804 printf("%d\n", a
/= 4);
805 printf("%d\n", a
%= 20);
806 printf("%d\n", a
&= 6);
807 printf("%d\n", a
^= 7);
808 printf("%d\n", a
|= 8);
809 printf("%d\n", a
>>= 3);
810 printf("%d\n", a
<<= 4);
814 printf("%d\n", a
+ 1);
815 printf("%d\n", a
- 2);
816 printf("%d\n", a
* 312);
817 printf("%d\n", a
/ 4);
818 printf("%d\n", b
/ 4);
819 printf("%d\n", (unsigned)b
/ 4);
820 printf("%d\n", a
% 20);
821 printf("%d\n", b
% 20);
822 printf("%d\n", (unsigned)b
% 20);
823 printf("%d\n", a
& 6);
824 printf("%d\n", a
^ 7);
825 printf("%d\n", a
| 8);
826 printf("%d\n", a
>> 3);
827 printf("%d\n", b
>> 3);
828 printf("%d\n", (unsigned)b
>> 3);
829 printf("%d\n", a
<< 4);
834 printf("%d\n", 12 + 1);
835 printf("%d\n", 12 - 2);
836 printf("%d\n", 12 * 312);
837 printf("%d\n", 12 / 4);
838 printf("%d\n", 12 % 20);
839 printf("%d\n", 12 & 6);
840 printf("%d\n", 12 ^ 7);
841 printf("%d\n", 12 | 8);
842 printf("%d\n", 12 >> 2);
843 printf("%d\n", 12 << 4);
847 printf("%d %d %d %d\n",
856 return (c
>= 'a' & c
<= 'z') | (c
>= 'A' & c
<= 'Z') | c
== '_';
859 /**********************/
861 int vstack
[10], *vstack_ptr
;
863 void vpush(int vt
, int vc
)
869 void vpop(int *ft
, int *fc
)
882 vstack_ptr
[-2] &= ~0xffffff80;
884 printf("res= %d %d\n", a
, b
);
887 void constant_expr_test()
890 printf("constant_expr:\n");
892 printf("%d\n", a
* 16);
893 printf("%d\n", a
* 1);
894 printf("%d\n", a
+ 0);
904 printf("expr_ptr:\n");
907 printf("diff=%d\n", q
- p
);
909 printf("inc=%d\n", p
- tab4
);
911 printf("dec=%d\n", p
- tab4
);
913 printf("inc=%d\n", p
- tab4
);
915 printf("dec=%d\n", p
- tab4
);
916 printf("add=%d\n", p
+ 3 - tab4
);
917 printf("add=%d\n", 3 + p
- tab4
);
919 /* check if 64bit support is ok */
922 printf("%p %p %ld\n", q
, p
, p
-q
);
923 printf("%d %d %d %d %d %d\n",
924 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
927 printf("%p %p %ld\n", q
, p
, p
-q
);
928 printf("%d %d %d %d %d %d\n",
929 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
930 p
= (int *)((char *)p
+ 0xf0000000);
931 printf("%p %p %ld\n", q
, p
, p
-q
);
932 printf("%d %d %d %d %d %d\n",
933 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
935 printf("%p %p %ld\n", q
, p
, p
-q
);
936 printf("%d %d %d %d %d %d\n",
937 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
942 struct size12 s
[2], *sp
= s
;
947 printf("%d\n", sp
[j
].i
);
951 p
= (int*)0x100000000UL
+ i
;
953 printf("largeptr: %p %d\n", p
, i
);
960 printf("constant_expr:\n");
963 printf("%d\n", a
== a
);
964 printf("%d\n", a
!= a
);
966 printf("%d\n", a
< b
);
967 printf("%d\n", a
<= b
);
968 printf("%d\n", a
<= a
);
969 printf("%d\n", b
>= a
);
970 printf("%d\n", a
>= a
);
971 printf("%d\n", b
> a
);
973 printf("%d\n", (unsigned)a
< b
);
974 printf("%d\n", (unsigned)a
<= b
);
975 printf("%d\n", (unsigned)a
<= a
);
976 printf("%d\n", (unsigned)b
>= a
);
977 printf("%d\n", (unsigned)a
>= a
);
978 printf("%d\n", (unsigned)b
> a
);
1006 printf("struct:\n");
1007 printf("sizes: %d %d %d %d\n",
1008 sizeof(struct struct1
),
1009 sizeof(struct struct2
),
1010 sizeof(union union1
),
1011 sizeof(union union2
));
1015 printf("st1: %d %d %d\n",
1016 st1
.f1
, st1
.f2
, st1
.f3
);
1019 printf("union1: %d\n", st1
.u
.v1
);
1022 printf("union2: %d\n", u
.w1
);
1027 printf("st2: %d %d %d\n",
1028 s
->f1
, s
->f2
, s
->f3
);
1029 printf("str_addr=%x\n", (int)st1
.str
- (int)&st1
.f1
);
1031 /* align / size tests */
1032 printf("aligntest1 sizeof=%d alignof=%d\n",
1033 sizeof(struct aligntest1
), __alignof__(struct aligntest1
));
1034 printf("aligntest2 sizeof=%d alignof=%d\n",
1035 sizeof(struct aligntest2
), __alignof__(struct aligntest2
));
1036 printf("aligntest3 sizeof=%d alignof=%d\n",
1037 sizeof(struct aligntest3
), __alignof__(struct aligntest3
));
1038 printf("aligntest4 sizeof=%d alignof=%d\n",
1039 sizeof(struct aligntest4
), __alignof__(struct aligntest4
));
1041 /* empty structures (GCC extension) */
1042 printf("sizeof(struct empty) = %d\n", sizeof(struct empty
));
1043 printf("alignof(struct empty) = %d\n", __alignof__(struct empty
));
1046 /* XXX: depend on endianness */
1047 void char_short_test()
1051 printf("char_short:\n");
1055 printf("s8=%d %d\n",
1056 *(char *)&var1
, *(char *)&var2
);
1057 printf("u8=%d %d\n",
1058 *(unsigned char *)&var1
, *(unsigned char *)&var2
);
1059 printf("s16=%d %d\n",
1060 *(short *)&var1
, *(short *)&var2
);
1061 printf("u16=%d %d\n",
1062 *(unsigned short *)&var1
, *(unsigned short *)&var2
);
1063 printf("s32=%d %d\n",
1064 *(int *)&var1
, *(int *)&var2
);
1065 printf("u32=%d %d\n",
1066 *(unsigned int *)&var1
, *(unsigned int *)&var2
);
1067 *(char *)&var1
= 0x08;
1068 printf("var1=%x\n", var1
);
1069 *(short *)&var1
= 0x0809;
1070 printf("var1=%x\n", var1
);
1071 *(int *)&var1
= 0x08090a0b;
1072 printf("var1=%x\n", var1
);
1075 /******************/
1077 typedef struct Sym
{
1085 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1086 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1088 static int toupper1(int a
)
1093 static unsigned int calc_vm_flags(unsigned int prot
)
1095 unsigned int prot_bits
;
1096 /* This used to segfault in some revisions: */
1097 prot_bits
= ((0x1==0x00000001)?(prot
&0x1):(prot
&0x1)?0x00000001:0);
1103 int *s
, a
, b
, t
, f
, i
;
1107 printf("!s=%d\n", !s
);
1111 printf("a=%d\n", a
);
1113 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1114 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1115 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1130 printf("b=%d\n", a
+ (0 ? 1 : a
/ 2));
1132 /* test register spilling */
1135 a
= (a
+ b
) * ((a
< b
) ?
1136 ((b
- a
) * (a
- b
)): a
+ b
);
1137 printf("a=%d\n", a
);
1139 /* test complex || or && expressions */
1143 printf("exp=%d\n", f
== (32 <= a
&& a
<= 3));
1144 printf("r=%d\n", (t
|| f
) + (t
&& f
));
1149 int aspect_native
= 65536;
1150 double bfu_aspect
= 1.0;
1152 for(aspect_on
= 0; aspect_on
< 2; aspect_on
++) {
1153 aspect
=aspect_on
?(aspect_native
*bfu_aspect
+0.5):65535UL;
1154 printf("aspect=%d\n", aspect
);
1158 /* test ? : GCC extension */
1160 static int v1
= 34 ? : -1; /* constant case */
1161 static int v2
= 0 ? : -1; /* constant case */
1164 printf("%d %d\n", v1
, v2
);
1165 printf("%d %d\n", a
- 30 ? : a
* 2, a
+ 1 ? : a
* 2);
1168 /* again complex expression */
1169 for(i
=0;i
<256;i
++) {
1170 if (toupper1 (i
) != TOUPPER (i
))
1171 printf("error %d\n", i
);
1173 printf ("bits = 0x%x\n", calc_vm_flags (0x1));
1176 extern int undefined_function(void);
1177 extern int defined_function(void);
1179 static inline void refer_to_undefined(void)
1181 undefined_function();
1184 void optimize_out(void)
1186 int i
= 0 ? undefined_function() : defined_function();
1187 printf ("oo:%d\n", i
);
1188 int j
= 1 ? defined_function() : undefined_function();
1189 printf ("oo:%d\n", j
);
1191 printf("oo:%d\n", undefined_function());
1193 printf("oo:%d\n", defined_function());
1195 printf("oo:%d\n", defined_function());
1197 printf("oo:%d\n", undefined_function());
1199 printf("oow:%d\n", defined_function());
1201 printf("oow:%d\n", undefined_function());
1204 /* Following is a switch without {} block intentionally. */
1207 printf ("oos:%d\n", defined_function());
1208 /* The following break shouldn't lead to disabled code after
1212 printf ("ool1:%d\n", defined_function());
1213 /* Same for the other types of loops. */
1217 printf ("ool2:%d\n", defined_function());
1220 printf ("ool3:%d\n", defined_function());
1221 /* Normal {} blocks without controlling statements
1222 shouldn't reactivate code emission */
1227 printf ("ool4:%d\n", undefined_function());
1232 break; /* this break shouldn't disable code outside the if. */
1233 printf("ool5:%d\n", defined_function());
1243 printf("ool6:%d\n", defined_function());
1247 /* Test that constants in logical && are optimized: */
1248 i
= 0 && undefined_function();
1249 i
= defined_function() && 0 && undefined_function();
1250 if (0 && undefined_function())
1251 undefined_function();
1252 if (defined_function() && 0)
1253 undefined_function();
1255 undefined_function();
1256 if (defined_function() && 0 && undefined_function())
1257 undefined_function();
1258 /* The same for || : */
1259 i
= 1 || undefined_function();
1260 i
= defined_function() || 1 || undefined_function();
1261 if (1 || undefined_function())
1264 undefined_function();
1265 if (defined_function() || 1)
1268 undefined_function();
1272 undefined_function();
1273 if (defined_function() || 1 || undefined_function())
1276 undefined_function();
1278 if (defined_function() && 0)
1279 refer_to_undefined();
1283 printf ("oor:%d\n", undefined_function());
1286 int defined_function(void)
1292 /* GCC accepts that */
1293 static int tab_reinit
[];
1294 static int tab_reinit
[10];
1296 //int cinit1; /* a global variable can be defined several times without error ! */
1300 int *cinit2
= (int []){3, 2, 1};
1302 void compound_literal_test(void)
1307 printf("compound_test:\n");
1309 p
= (int []){1, 2, 3};
1311 printf(" %d", p
[i
]);
1315 printf("%d", cinit2
[i
]);
1319 printf("q1=%s\n", q
);
1321 q
= (char *){ "tralala2" };
1322 printf("q2=%s\n", q
);
1325 printf("q3=%s\n", q3
);
1327 q
= (char []){ "tralala3" };
1328 printf("q4=%s\n", q
);
1331 p
= (int []){1, 2, cinit1
+ 3};
1333 printf(" %d", p
[i
]);
1337 p
= (int []){1, 2, 4 + i
};
1338 printf("%d %d %d\n",
1360 printf("kr_test:\n");
1361 printf("func1=%d\n", kr_func1(3, 4));
1362 printf("func2=%d\n", kr_func2(3, 4));
1369 tab
= (char*)malloc(20);
1386 /* structure assignment tests */
1392 struct structa1 ssta1
;
1394 void struct_assign_test1(struct structa1 s1
, int t
, float f
)
1396 printf("%d %d %d %f\n", s1
.f1
, s1
.f2
, t
, f
);
1399 struct structa1
struct_assign_test2(struct structa1 s1
, int t
)
1406 void struct_assign_test(void)
1409 struct structa1 lsta1
, lsta2
;
1416 printf("struct_assign_test:\n");
1420 printf("%d %d\n", s
.lsta1
.f1
, s
.lsta1
.f2
);
1422 printf("%d %d\n", s
.lsta2
.f1
, s
.lsta2
.f2
);
1427 struct_assign_test1(ps
->lsta2
, 3, 4.5);
1429 printf("before call: %d %d\n", s
.lsta2
.f1
, s
.lsta2
.f2
);
1430 ps
->lsta2
= struct_assign_test2(ps
->lsta2
, ps
->i
);
1431 printf("after call: %d %d\n", ps
->lsta2
.f1
, ps
->lsta2
.f2
);
1436 /* XXX: we should allow this even without braces */
1437 { struct_assign_test
}
1439 printf("%d\n", struct_assign_test
== t
[0].elem
);
1442 /* casts to short/char */
1444 void cast1(char a
, short b
, unsigned char c
, unsigned short d
)
1446 printf("%d %d %d %d\n", a
, b
, c
, d
);
1460 p
-= 0x700000000042;
1462 printf("cast_test:\n");
1466 printf("%d %d %d %d\n",
1469 (unsigned char)(a
+ 1),
1470 (unsigned short)(a
+ 1));
1471 printf("%d %d %d %d\n",
1474 (unsigned char)0xfffff,
1475 (unsigned short)0xfffff);
1477 a
= (bcast
= 128) + 1;
1479 a
= (scast
= 65536) + 1;
1482 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
1484 /* test cast from unsigned to signed short to int */
1487 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b
, d
);
1490 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b
, d
);
1492 /* test implicit int casting for array accesses */
1496 printf("%d %d\n", tab
[0], tab
[1]);
1498 /* test implicit casting on some operators */
1499 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1500 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1501 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1503 /* from pointer to integer types */
1504 printf("%d %d %ld %ld %lld %lld\n",
1505 (int)p
, (unsigned int)p
,
1506 (long)p
, (unsigned long)p
,
1507 (long long)p
, (unsigned long long)p
);
1509 /* from integers to pointers */
1510 printf("%p %p %p %p\n",
1511 (void *)a
, (void *)b
, (void *)c
, (void *)d
);
1514 /* initializers tests */
1515 struct structinit1
{
1524 int sinit3
[3] = { 1, 2, {{3}}, };
1525 int sinit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1526 int sinit5
[3][2] = { 1, 2, 3, 4, 5, 6 };
1527 int sinit6
[] = { 1, 2, 3 };
1528 int sinit7
[] = { [2] = 3, [0] = 1, 2 };
1529 char sinit8
[] = "hello" "trala";
1531 struct structinit1 sinit9
= { 1, 2, 3 };
1532 struct structinit1 sinit10
= { .f2
= 2, 3, .f1
= 1 };
1533 struct structinit1 sinit11
= { .f2
= 2, 3, .f1
= 1,
1541 char *sinit12
= "hello world";
1547 char sinit14
[10] = { "abc" };
1548 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
1550 struct { int a
[3], b
; } sinit16
[] = { { 1 }, 2 };
1566 struct complexinit0
{
1571 struct complexinit
{
1573 const struct complexinit0
*b
;
1576 const static struct complexinit cix
[] = {
1579 .b
= (const struct complexinit0
[]) {
1587 struct complexinit2
{
1592 struct complexinit2 cix20
;
1594 struct complexinit2 cix21
= {
1596 .b
= { 3001, 3002, 3003 }
1599 struct complexinit2 cix22
= {
1601 .b
= { 4001, 4002, 4003, 4004, 4005, 4006 }
1604 typedef int arrtype1
[];
1605 arrtype1 sinit19
= {1};
1606 arrtype1 sinit20
= {2,3};
1607 typedef int arrtype2
[3];
1608 arrtype2 sinit21
= {4};
1609 arrtype2 sinit22
= {5,6,7};
1611 /* Address comparisons of non-weak symbols with zero can be const-folded */
1612 int sinit23
[2] = { "astring" ? sizeof("astring") : -1,
1613 &sinit23
? 42 : -1 };
1615 void init_test(void)
1619 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1620 int linit6
[] = { 1, 2, 3 };
1622 char linit8
[] = "hello" "trala";
1623 int linit12
[10] = { 1, 2 };
1624 int linit13
[10] = { 1, 2, [7] = 3, [3] = 4, };
1625 char linit14
[10] = "abc";
1626 int linit15
[10] = { linit1
, linit1
+ 1, [6] = linit1
+ 2, };
1627 struct linit16
{ int a1
, a2
, a3
, a4
; } linit16
= { 1, .a3
= 2 };
1628 int linit17
= sizeof(linit17
);
1630 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1631 int linit18
[2] = {&zero
? 1 : -1, zero
? -1 : 1 };
1633 printf("init_test:\n");
1635 printf("sinit1=%d\n", sinit1
);
1636 printf("sinit2=%d\n", sinit2
);
1637 printf("sinit3=%d %d %d %d\n",
1643 printf("sinit6=%d\n", sizeof(sinit6
));
1644 printf("sinit7=%d %d %d %d\n",
1650 printf("sinit8=%s\n", sinit8
);
1651 printf("sinit9=%d %d %d\n",
1656 printf("sinit10=%d %d %d\n",
1661 printf("sinit11=%d %d %d %d %d %d\n",
1672 printf("[%d][%d] = %d %d %d\n",
1673 i
, j
, sinit4
[i
][j
], sinit5
[i
][j
], linit4
[i
][j
]);
1674 printf("linit1=%d\n", linit1
);
1675 printf("linit2=%d\n", linit2
);
1676 printf("linit6=%d\n", sizeof(linit6
));
1677 printf("linit8=%d %s\n", sizeof(linit8
), linit8
);
1679 printf("sinit12=%s\n", sinit12
);
1680 printf("sinit13=%d %s %s %s\n",
1685 printf("sinit14=%s\n", sinit14
);
1687 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
1689 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
1691 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
1693 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
1695 printf("%d %d %d %d\n",
1700 /* test that initialisation is done after variable declare */
1701 printf("linit17=%d\n", linit17
);
1702 printf("sinit15=%d\n", sinit15
[0]);
1703 printf("sinit16=%d %d\n", sinit16
[0].a
[0], sinit16
[1].a
[0]);
1704 printf("sinit17=%s %d %s %d\n",
1705 sinit17
[0].s
, sinit17
[0].len
,
1706 sinit17
[1].s
, sinit17
[1].len
);
1708 printf("%x ", sinit18
[i
]);
1710 /* complex init check */
1711 printf("cix: %d %d %d %d %d %d %d\n",
1713 cix
[0].b
[0].a
, cix
[0].b
[0].b
,
1714 cix
[0].b
[1].a
, cix
[0].b
[1].b
,
1715 cix
[0].b
[2].a
, cix
[0].b
[2].b
);
1716 printf("cix2: %d %d\n", cix21
.b
[2], cix22
.b
[5]);
1717 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20
, sizeof cix21
, sizeof cix22
);
1719 printf("arrtype1: %d %d %d\n", sinit19
[0], sinit20
[0], sinit20
[1]);
1720 printf("arrtype2: %d %d\n", sizeof(sinit19
), sizeof(sinit20
));
1721 printf("arrtype3: %d %d %d\n", sinit21
[0], sinit21
[1], sinit21
[2]);
1722 printf("arrtype4: %d %d %d\n", sinit22
[0], sinit22
[1], sinit22
[2]);
1723 printf("arrtype5: %d %d\n", sizeof(sinit21
), sizeof(sinit22
));
1724 printf("arrtype6: %d\n", sizeof(arrtype2
));
1726 printf("sinit23= %d %d\n", sinit23
[0], sinit23
[1]);
1727 printf("linit18= %d %d\n", linit18
[0], linit18
[1]);
1758 /* ISOC99 _Bool type */
1759 void c99_bool_test(void)
1765 printf("bool_test:\n");
1766 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
1768 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
1770 printf("b = %d\n", b
);
1772 printf("b = %d\n", b
);
1776 void bitfield_test(void)
1788 unsigned int f5
: 7;
1790 printf("bitfield_test:");
1791 printf("sizeof(st1) = %d\n", sizeof(st1
));
1800 printf("%d %d %d %d %d\n",
1801 st1
.f1
, st1
.f2
, st1
.f3
, st1
.f4
, st1
.f5
);
1804 printf("%d %d\n", sa
, ca
);
1808 printf("st1.f1 == -1\n");
1810 printf("st1.f1 != -1\n");
1812 printf("st1.f2 == -1\n");
1814 printf("st1.f2 != -1\n");
1816 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1817 long-long bitfields whose size is not bigger than int */
1822 unsigned long long f3
: 38;
1824 st2
.f1
= 0x123456789ULL
;
1826 st2
.f2
= (long long)a
<< 25;
1829 printf("%lld %lld %lld\n", st2
.f1
, st2
.f2
, st2
.f3
);
1833 #define FLOAT_FMT "%f\n"
1835 /* x86's float isn't compatible with GCC */
1836 #define FLOAT_FMT "%.5f\n"
1839 /* declare strto* functions as they are C99 */
1840 double strtod(const char *nptr
, char **endptr
);
1843 float strtof(const char *nptr
, char **endptr
) {return (float)strtod(nptr
, endptr
);}
1844 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
) {return (LONG_DOUBLE
)strtod(nptr
, endptr
);}
1846 float strtof(const char *nptr
, char **endptr
);
1847 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
);
1850 #define FTEST(prefix, typename, type, fmt)\
1851 void prefix ## cmp(type a, type b)\
1853 printf("%d %d %d %d %d %d\n",\
1860 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1868 printf(fmt "\n", ++a);\
1869 printf(fmt "\n", a++);\
1870 printf(fmt "\n", a);\
1872 printf("%d %d\n", !a, !b);\
1874 void prefix ## fcast(type a)\
1882 unsigned long long llua;\
1887 printf("ftof: %f %f %Lf\n", fa, da, la);\
1889 llia = (long long)a;\
1890 a = (a >= 0) ? a : -a;\
1891 ua = (unsigned int)a;\
1892 llua = (unsigned long long)a;\
1893 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
1896 llia = -0x123456789012345LL;\
1897 llua = 0xf123456789012345LLU;\
1899 printf("itof: " fmt "\n", b);\
1901 printf("utof: " fmt "\n", b);\
1903 printf("lltof: " fmt "\n", b);\
1905 printf("ulltof: " fmt "\n", b);\
1908 float prefix ## retf(type a) { return a; }\
1909 double prefix ## retd(type a) { return a; }\
1910 LONG_DOUBLE prefix ## retld(type a) { return a; }\
1912 void prefix ## call(void)\
1914 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1915 printf("double: %f\n", prefix ## retd(42.123456789));\
1916 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1917 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
1920 void prefix ## signed_zeros(void) \
1922 type x = 0.0, y = -0.0, n, p;\
1924 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
1925 1.0 / x != 1.0 / y);\
1927 printf ("x != y; this is wrong!\n");\
1931 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
1932 1.0 / x != 1.0 / n);\
1934 printf ("x != -x; this is wrong!\n");\
1938 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
1939 1.0 / x != 1.0 / p);\
1941 printf ("x != +y; this is wrong!\n");\
1944 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
1945 1.0 / x != 1.0 / p);\
1947 printf ("x != -y; this is wrong!\n");\
1949 void prefix ## test(void)\
1951 printf("testing '%s'\n", #typename);\
1952 prefix ## cmp(1, 2.5);\
1953 prefix ## cmp(2, 1.5);\
1954 prefix ## cmp(1, 1);\
1955 prefix ## fcast(234.6);\
1956 prefix ## fcast(-2334.6);\
1958 prefix ## signed_zeros();\
1961 FTEST(f
, float, float, "%f")
1962 FTEST(d
, double, double, "%f")
1963 FTEST(ld
, long double, LONG_DOUBLE
, "%Lf")
1965 double ftab1
[3] = { 1.2, 3.4, -5.6 };
1968 void float_test(void)
1970 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
1976 printf("float_test:\n");
1977 printf("sizeof(float) = %d\n", sizeof(float));
1978 printf("sizeof(double) = %d\n", sizeof(double));
1979 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE
));
1983 printf("%f %f %f\n", ftab1
[0], ftab1
[1], ftab1
[2]);
1984 printf("%f %f %f\n", 2.12, .5, 2.3e10
);
1985 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1987 printf("da=%f\n", da
);
1989 printf("fa=%f\n", fa
);
1992 printf("da = %f\n", da
);
1995 printf("db = %f\n", db
);
2004 return fib(n
-1) + fib(n
-2);
2017 printf("funcptr:\n");
2024 /* more complicated pointer computation */
2027 printf("sizeof1 = %d\n", sizeof(funcptr_test
));
2028 printf("sizeof2 = %d\n", sizeof funcptr_test
);
2029 printf("sizeof3 = %d\n", sizeof(&funcptr_test
));
2030 printf("sizeof4 = %d\n", sizeof &funcptr_test
);
2039 void lloptest(long long a
, long long b
)
2041 unsigned long long ua
, ub
;
2046 printf("arith: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2052 printf("arith1: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2058 printf("bin: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2064 printf("test: %d %d %d %d %d %d\n",
2072 printf("utest: %d %d %d %d %d %d\n",
2083 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2084 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
++, b
++);
2085 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", --a
, --b
);
2086 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2088 printf("not: %d %d %d %d\n", !a
, !ua
, !b
, !ub
);
2091 void llshift(long long a
, int b
)
2093 printf("shift: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2094 (unsigned long long)a
>> b
,
2097 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2098 (unsigned long long)a
>> 3,
2101 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2102 (unsigned long long)a
>> 35,
2112 long long la
, lb
, lc
;
2113 unsigned long long ula
, ulb
, ulc
;
2116 la
= (la
<< 20) | 0x12345;
2118 printf("la=" LONG_LONG_FORMAT
" ula=" ULONG_LONG_FORMAT
"\n", la
, ula
);
2123 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
2128 printf("ftoll: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", la
, lb
, lc
);
2133 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
2138 printf("ftoull: " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
"\n", ula
, ulb
, ulc
);
2141 long long llfunc1(int a
)
2151 long long int value(struct S
*v
)
2153 return ((long long int)v
->item
);
2156 long long llfunc2(long long x
, long long y
, int z
)
2161 void longlong_test(void)
2166 printf("longlong_test:\n");
2167 printf("sizeof(long long) = %d\n", sizeof(long long));
2172 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2173 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %Lx\n",
2177 0x1234567812345679);
2179 printf(LONG_LONG_FORMAT
"\n", a
);
2182 lloptest(0xff, 0x1234);
2183 b
= 0x72345678 << 10;
2187 b
= 0x72345678LL
<< 10;
2198 /* long long reg spill test */
2203 printf("%lld\n", value(&a
));
2205 lloptest(0x80000000, 0);
2208 long long *p
, v
, **pp
;
2212 printf("another long long spill test : %lld\n", *p
);
2215 v
= llfunc2(**pp
, **pp
, ia
);
2216 printf("a long long function (arm-)reg-args test : %lld\n", v
);
2220 printf("%d %d %d %d\n", a
> b
, a
< b
, a
>= b
, a
<= b
);
2222 printf(LONG_LONG_FORMAT
"\n", 0x123456789LLU
);
2224 /* long long pointer deref in argument passing test */
2230 void manyarg_test(void)
2232 LONG_DOUBLE ld
= 1234567891234LL;
2233 printf("manyarg_test:\n");
2234 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2235 1, 2, 3, 4, 5, 6, 7, 8,
2236 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2237 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2238 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2239 1, 2, 3, 4, 5, 6, 7, 8,
2240 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2241 1234567891234LL, 987654321986LL,
2243 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2244 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2245 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2246 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2247 1234567891234LL, 987654321986LL,
2249 printf("%d %d %d %d %d %d %d %d %Lf\n",
2250 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2251 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2252 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"%f %f %Lf\n",
2253 1, 2, 3, 4, 5, 6, 7, 8,
2254 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2255 1234567891234LL, 987654321986LL,
2257 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2258 "%Lf " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f %Lf\n",
2259 1, 2, 3, 4, 5, 6, 7, 8,
2260 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2261 ld
, 1234567891234LL, 987654321986LL,
2265 void vprintf1(const char *fmt
, ...)
2289 i
= va_arg(ap
, int);
2293 d
= va_arg(ap
, double);
2297 ll
= va_arg(ap
, long long);
2298 printf(LONG_LONG_FORMAT
, ll
);
2301 ld
= va_arg(ap
, LONG_DOUBLE
);
2319 void stdarg_for_struct(struct myspace bob
, ...)
2321 struct myspace george
, bill
;
2326 bill
= va_arg(ap
, struct myspace
);
2327 george
= va_arg(ap
, struct myspace
);
2328 validate
= va_arg(ap
, int);
2329 printf("stdarg_for_struct: %d %d %d %d\n",
2330 bob
.profile
, bill
.profile
, george
.profile
, validate
);
2334 void stdarg_for_libc(const char *fmt
, ...)
2337 va_start(args
, fmt
);
2342 void stdarg_test(void)
2344 LONG_DOUBLE ld
= 1234567891234LL;
2347 vprintf1("%d %d %d\n", 1, 2, 3);
2348 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2349 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2350 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2351 vprintf1("%d %f %l %F %d %f %l %F\n",
2352 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2353 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2354 1, 2, 3, 4, 5, 6, 7, 8,
2355 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2356 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2357 1, 2, 3, 4, 5, 6, 7, 8,
2358 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2359 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2361 1, 2, 3, 4, 5, 6, 7, 8,
2362 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2363 1234567891234LL, 987654321986LL,
2365 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2367 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2368 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2369 1234567891234LL, 987654321986LL,
2371 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2372 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2373 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2375 1, 2, 3, 4, 5, 6, 7, 8,
2376 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2377 1234567891234LL, 987654321986LL,
2379 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2380 "%F %l %l %f %f %F\n",
2381 1, 2, 3, 4, 5, 6, 7, 8,
2382 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2383 ld
, 1234567891234LL, 987654321986LL,
2387 stdarg_for_struct(bob
, bob
, bob
, bob
.profile
);
2388 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2391 void whitespace_test(void)
2397 ntf("whitspace:\n");\f\v
2401 #ifdef CORRECT_CR_HANDLING
2410 #ifdef ACCEPT_CR_IN_STRINGS
2411 printf("len1=%d\n", strlen("
2413 #ifdef CORRECT_CR_HANDLING
2416 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
2418 printf("len1=%d\n", strlen("
a
2420 #endif /* ACCEPT_CR_IN_STRINGS */
2423 int reltab
[3] = { 1, 2, 3 };
2425 int *rel1
= &reltab
[1];
2426 int *rel2
= &reltab
[2];
2428 void getmyaddress(void)
2430 printf("in getmyaddress\n");
2434 long __pa_symbol(void)
2436 /* This 64bit constant was handled incorrectly, it was used as addend
2437 (which can hold 64bit just fine) in connection with a symbol,
2438 and TCC generates wrong code for that (displacements are 32bit only).
2439 This effectively is "+ 0x80000000", and if addresses of globals
2440 are below 2GB the result should be a number without high 32 bits set. */
2441 return ((long)(((unsigned long)(&rel1
))) - (0xffffffff80000000UL
));
2445 unsigned long theaddress
= (unsigned long)getmyaddress
;
2446 void relocation_test(void)
2448 void (*fptr
)(void) = (void (*)(void))theaddress
;
2449 printf("*rel1=%d\n", *rel1
);
2450 printf("*rel2=%d\n", *rel2
);
2453 printf("pa_symbol=0x%lx\n", __pa_symbol() >> 63);
2457 void old_style_f(a
,b
,c
)
2461 printf("a=%d b=%d b=%f\n", a
, b
, c
);
2464 void decl_func1(int cmpfn())
2466 printf("cmpfn=%lx\n", (long)cmpfn
);
2469 void decl_func2(cmpfn
)
2472 printf("cmpfn=%lx\n", (long)cmpfn
);
2475 void old_style_function(void)
2477 old_style_f((void *)1, 2, 3.0);
2484 #if defined __i386__ || defined __x86_64__ || defined __arm__
2485 char *p
= alloca(16);
2486 strcpy(p
,"123456789012345");
2487 printf("alloca: p is %s\n", p
);
2488 char *demo
= "This is only a test.\n";
2489 /* Test alloca embedded in a larger expression */
2490 printf("alloca: %s\n", strcpy(alloca(strlen(demo
)+1),demo
) );
2494 void *bounds_checking_is_enabled()
2496 char ca
[10], *cp
= ca
-1;
2497 return (ca
!= cp
+ 1) ? cp
: NULL
;
2500 typedef int constant_negative_array_size_as_compile_time_assertion_idiom
[(1 ? 2 : 0) - 1];
2502 void c99_vla_test(int size1
, int size2
)
2504 #if defined __i386__ || defined __x86_64__
2505 int size
= size1
* size2
;
2506 int tab1
[size
][2], tab2
[10][2];
2507 void *tab1_ptr
, *tab2_ptr
, *bad_ptr
;
2509 /* "size" should have been 'captured' at tab1 declaration,
2510 so modifying it should have no effect on VLA behaviour. */
2513 printf("Test C99 VLA 1 (sizeof): ");
2514 printf("%s\n", (sizeof tab1
== size1
* size2
* 2 * sizeof(int)) ? "PASSED" : "FAILED");
2517 printf("Test C99 VLA 2 (ptrs subtract): ");
2518 printf("%s\n", (tab2
- tab1
== (tab2_ptr
- tab1_ptr
) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2519 printf("Test C99 VLA 3 (ptr add): ");
2520 printf("%s\n", &tab1
[5][1] == (tab1_ptr
+ (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2521 printf("Test C99 VLA 4 (ptr access): ");
2522 tab1
[size1
][1] = 42;
2523 printf("%s\n", (*((int *) (tab1_ptr
+ (size1
* 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2525 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2526 if (bad_ptr
= bounds_checking_is_enabled()) {
2527 int *t1
= &tab1
[size1
* size2
- 1][3];
2528 int *t2
= &tab2
[9][3];
2529 printf("%s ", bad_ptr
== t1
? "PASSED" : "FAILED");
2530 printf("%s ", bad_ptr
== t2
? "PASSED" : "FAILED");
2532 char*c1
= 1 + sizeof(tab1
) + (char*)tab1
;
2533 char*c2
= 1 + sizeof(tab2
) + (char*)tab2
;
2534 printf("%s ", bad_ptr
== c1
? "PASSED" : "FAILED");
2535 printf("%s ", bad_ptr
== c2
? "PASSED" : "FAILED");
2539 printf("%s ", bad_ptr
== i1
? "PASSED" : "FAILED");
2540 printf("%s ", bad_ptr
== i2
? "PASSED" : "FAILED");
2542 int *x1
= tab1
[size1
* size2
+ 1];
2543 int *x2
= tab2
[10 + 1];
2544 printf("%s ", bad_ptr
== x1
? "PASSED" : "FAILED");
2545 printf("%s ", bad_ptr
== x2
? "PASSED" : "FAILED");
2547 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2554 typedef __SIZE_TYPE__
uintptr_t;
2557 void sizeof_test(void)
2562 printf("sizeof(int) = %d\n", sizeof(int));
2563 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2564 printf("sizeof(long) = %d\n", sizeof(long));
2565 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2566 printf("sizeof(short) = %d\n", sizeof(short));
2567 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2568 printf("sizeof(char) = %d\n", sizeof(char));
2569 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2570 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2572 printf("sizeof(a++) = %d\n", sizeof a
++);
2573 printf("a=%d\n", a
);
2575 printf("sizeof(**ptr) = %d\n", sizeof (**ptr
));
2577 /* The type of sizeof should be as large as a pointer, actually
2578 it should be size_t. */
2579 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2582 /* Effectively <<32, but defined also on 32bit machines. */
2586 /* This checks that sizeof really can be used to manipulate
2587 uintptr_t objects, without truncation. */
2588 t2
= t
& -sizeof(uintptr_t);
2589 printf ("%lu %lu\n", t
, t2
);
2591 /* some alignof tests */
2592 printf("__alignof__(int) = %d\n", __alignof__(int));
2593 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2594 printf("__alignof__(short) = %d\n", __alignof__(short));
2595 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2596 printf("__alignof__(char) = %d\n", __alignof__(char));
2597 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2598 printf("__alignof__(func) = %d\n", __alignof__
sizeof_test());
2600 /* sizes of VLAs need to be evaluated even inside sizeof: */
2602 printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a
]));
2603 /* And checking if sizeof compound literal works. Parenthesized: */
2604 printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n",
2605 sizeof( (struct {int i
; int j
;}){4,5} ));
2606 /* And as direct sizeof argument (as unary expression): */
2607 printf("sizeof (struct {short i; short j;}){4,5} = %d\n",
2608 sizeof (struct {short i
; short j
;}){4,5} );
2610 /* sizeof(x && y) should be sizeof(int), even if constant
2611 evaluating is possible. */
2612 printf("sizeof(t && 0) = %d\n", sizeof(t
&& 0));
2613 printf("sizeof(1 && 1) = %d\n", sizeof(1 && 1));
2614 printf("sizeof(t || 1) = %d\n", sizeof(t
|| 1));
2615 printf("sizeof(0 || 0) = %d\n", sizeof(0 || 0));
2618 void typeof_test(void)
2627 printf("a=%f b=%f c=%f\n", a
, b
, c
);
2633 struct hlist_node
*first
, *last
;
2636 void statement_expr_test(void)
2640 /* Basic stmt expr test */
2650 printf("a=%d\n", a
);
2652 /* Test that symbols aren't freed prematurely.
2653 With SYM_DEBUG valgrind will show a read from a freed
2654 symbol, and tcc will show an (invalid) warning on the initialization
2655 of 'ptr' below, if symbols are popped after the stmt expr. */
2656 void *v
= (void*)39;
2658 (struct hlist_node
*)v
;
2661 ptr
= (struct hlist_node
*)v
;
2663 /* This part used to segfault when symbols were popped prematurely.
2664 The symbols for the static local would be overwritten with
2665 helper symbols from the pre-processor expansions in between. */
2666 #define some_attr __attribute__((aligned(1)))
2667 #define tps(str) ({ \
2668 static const char *t some_attr = str; \
2671 printf ("stmtexpr: %s %s\n",
2672 tps("somerandomlongstring"),
2673 tps("anotherlongstring"));
2675 /* Test that the three decls of 't' don't interact. */
2677 int b
= ({ int t
= 41; t
; });
2678 int c
= ({ int t
= 42; t
; });
2680 /* Test that aggregate return values work. */
2683 typedef struct hlist_head T
;
2685 T t
= { (void*)43, (void*)44 };
2689 printf ("stmtexpr: %d %d %d\n", t
, b
, c
);
2690 printf ("stmtexpr: %ld %ld\n", (long)h
.first
, (long)h
.last
);
2693 void local_label_test(void)
2699 __label__ l1
, l2
, l3
, l4
;
2713 printf("a=%d\n", a
);
2723 /* inline assembler test */
2724 #if defined(__i386__) || defined(__x86_64__)
2726 /* from linux kernel */
2727 static char * strncat1(char * dest
,const char * src
,size_t count
)
2729 long d0
, d1
, d2
, d3
;
2730 __asm__
__volatile__(
2739 "testb %%al,%%al\n\t"
2743 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
2744 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
2749 static char * strncat2(char * dest
,const char * src
,size_t count
)
2751 long d0
, d1
, d2
, d3
;
2752 __asm__
__volatile__(
2753 "repne scasb\n\t" /* one-line repne prefix + string op */
2760 "testb %%al,%%al\n\t"
2764 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
2765 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
2770 static inline void * memcpy1(void * to
, const void * from
, size_t n
)
2773 __asm__
__volatile__(
2778 "1:\ttestb $1,%b4\n\t"
2782 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
2783 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
2788 static inline void * memcpy2(void * to
, const void * from
, size_t n
)
2791 __asm__
__volatile__(
2792 "rep movsl\n\t" /* one-line rep prefix + string op */
2796 "1:\ttestb $1,%b4\n\t"
2800 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
2801 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
2806 static __inline__
void sigaddset1(unsigned int *set
, int _sig
)
2808 __asm__("btsl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
2811 static __inline__
void sigdelset1(unsigned int *set
, int _sig
)
2813 asm("btrl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc", "flags");
2816 static __inline__ __const__
unsigned int swab32(unsigned int x
)
2818 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2819 "rorl $16,%0\n\t" /* swap words */
2820 "xchgb %b0,%h0" /* swap higher bytes */
2826 static __inline__
unsigned long long mul64(unsigned int a
, unsigned int b
)
2828 unsigned long long res
;
2830 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
2831 but still test the 32bit->64bit mull. */
2832 unsigned int resh
, resl
;
2833 __asm__("mull %2" : "=a" (resl
), "=d" (resh
) : "a" (a
), "r" (b
));
2834 res
= ((unsigned long long)resh
<< 32) | resl
;
2836 __asm__("mull %2" : "=A" (res
) : "a" (a
), "r" (b
));
2841 static __inline__
unsigned long long inc64(unsigned long long a
)
2843 unsigned long long res
;
2845 /* Using the A constraint is wrong, and increments are tested
2849 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res
) : "A" (a
));
2862 unsigned long mconstraint_test(struct struct1231
*r
)
2867 __asm__
volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
2868 : "=&r" (ret
), "=m" (a
)
2869 : "m" (*(struct struct123
*)r
->addr
));
2874 int fls64(unsigned long long x
)
2884 void other_constraints_test(void)
2888 __asm__
volatile ("movq %P1,%0" : "=r" (ret
) : "p" (&var
));
2889 printf ("oc1: %d\n", ret
== (unsigned long)&var
);
2892 /* Test global asm blocks playing with aliases. */
2893 void base_func(void)
2895 printf ("asmc: base\n");
2898 extern void override_func1 (void);
2899 extern void override_func2 (void);
2901 asm(".weak override_func1\n.set override_func1, base_func");
2902 asm(".set override_func1, base_func");
2903 asm(".set override_func2, base_func");
2905 void override_func2 (void)
2907 printf ("asmc: override2\n");
2910 /* This checks a construct used by the linux kernel to encode
2911 references to strings by PC relative references. */
2912 extern int bug_table
[] __attribute__((section("__bug_table")));
2913 char * get_asm_string (void)
2915 extern int some_symbol
;
2916 asm volatile (".globl some_symbol\n"
2919 "some_symbol: .long 0\n"
2920 ".pushsection __bug_table, \"a\"\n"
2921 ".globl bug_table\n"
2923 /* The first entry (1b-2b) is unused in this test,
2924 but we include it to check if cross-section
2925 PC-relative references work. */
2926 "2:\t.long 1b - 2b, %c0 - 2b\n"
2927 ".popsection\n" : : "i" ("A string"));
2928 char * str
= ((char*)bug_table
) + bug_table
[1];
2934 void fancy_copy (unsigned *in
, unsigned *out
)
2936 asm volatile ("" : "=r" (*out
) : "0" (*in
));
2939 void fancy_copy2 (unsigned *in
, unsigned *out
)
2941 asm volatile ("mov %0,(%1)" : : "r" (*in
), "r" (out
) : "memory");
2945 void clobber_r12(void)
2947 asm volatile("mov $1, %%r12" ::: "r12");
2951 void test_high_clobbers(void)
2954 register long val
asm("r12");
2956 /* This tests if asm clobbers correctly save/restore callee saved
2957 registers if they are clobbered and if it's the high 8 x86-64
2958 registers. This is fragile for GCC as the constraints do not
2959 correctly capture the data flow, but good enough for us. */
2960 asm volatile("mov $0x4542, %%r12" : "=r" (val
):: "memory");
2962 asm volatile("mov %%r12, %0" : "=r" (val2
) : "r" (val
): "memory");
2963 printf("asmhc: 0x%x\n", val2
);
2970 unsigned int val
, val2
;
2971 struct struct123 s1
;
2972 struct struct1231 s2
= { (unsigned long)&s1
};
2973 /* Hide the outer base_func, but check later that the inline
2974 asm block gets the outer one. */
2976 void override_func3 (void);
2977 unsigned long asmret
;
2981 register int regvar
asm("%esi");
2983 printf("inline asm:\n");
2985 // parse 0x1E-1 as 3 tokens in asm mode
2986 asm volatile ("mov $0x1E-1,%eax");
2988 /* test the no operand case */
2989 asm volatile ("xorl %eax, %eax");
2991 memcpy1(buf
, "hello", 6);
2992 strncat1(buf
, " worldXXXXX", 3);
2993 printf("%s\n", buf
);
2995 memcpy2(buf
, "hello", 6);
2996 strncat2(buf
, " worldXXXXX", 3);
2997 printf("%s\n", buf
);
2999 /* 'A' constraint test */
3000 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
3001 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
3005 printf("mconstraint: %d", mconstraint_test(&s2
));
3006 printf(" %d %d\n", s1
.a
, s1
.b
);
3007 other_constraints_test();
3009 sigdelset1(&set
, 2);
3010 sigaddset1(&set
, 16);
3011 /* NOTE: we test here if C labels are correctly restored after the
3015 __asm__("btsl %1,%0" : "=m"(set
) : "Ir"(20) : "cc");
3016 printf("set=0x%x\n", set
);
3018 printf("swab32(0x%08x) = 0x%0x\n", val
, swab32(val
));
3021 /* The base_func ref from the following inline asm should find
3022 the global one, not the local decl from this function. */
3023 asm volatile(".weak override_func3\n.set override_func3, base_func");
3025 /* Check that we can also load structs of appropriate layout
3027 asm volatile("" : "=r" (asmret
) : "0"(s2
));
3028 if (asmret
!= s2
.addr
)
3029 printf("asmstr: failed\n");
3031 /* Check that the typesize correctly sets the register size to
3033 asm volatile("cmp %1,%2; sete %0" : "=a"(somebool
) : "r"(1), "r"(2));
3035 printf("asmbool: failed\n");
3037 printf("asmstr: %s\n", get_asm_string());
3039 fancy_copy (&val
, &val2
);
3040 printf ("fancycpy(%d)=%d\n", val
, val2
);
3042 fancy_copy2 (&val
, &val2
);
3043 printf ("fancycpy2(%d)=%d\n", val
, val2
);
3044 asm volatile ("mov $0x4243, %%esi" : "=r" (regvar
));
3045 printf ("regvar=%x\n", regvar
);
3046 test_high_clobbers();
3060 #define COMPAT_TYPE(type1, type2) \
3062 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
3063 __builtin_types_compatible_p (type1, type2));\
3068 void builtin_test(void)
3074 COMPAT_TYPE(int, int);
3075 COMPAT_TYPE(int, unsigned int);
3076 COMPAT_TYPE(int, char);
3077 COMPAT_TYPE(int, const int);
3078 COMPAT_TYPE(int, volatile int);
3079 COMPAT_TYPE(int *, int *);
3080 COMPAT_TYPE(int *, void *);
3081 COMPAT_TYPE(int *, const int *);
3082 COMPAT_TYPE(char *, unsigned char *);
3083 COMPAT_TYPE(char *, signed char *);
3084 COMPAT_TYPE(char *, char *);
3085 /* space is needed because tcc preprocessor introduces a space between each token */
3086 COMPAT_TYPE(char * *, void *);
3088 printf("res = %d\n", __builtin_constant_p(1));
3089 printf("res = %d\n", __builtin_constant_p(1 + 2));
3090 printf("res = %d\n", __builtin_constant_p(&constant_p_var
));
3091 printf("res = %d\n", __builtin_constant_p(constant_p_var
));
3092 printf("res = %d\n", __builtin_constant_p(100000 / constant_p_var
));
3095 i
= __builtin_choose_expr (1 != 0, ll
, s
);
3096 printf("bce: %d\n", i
);
3097 i
= __builtin_choose_expr (1 != 1, ll
, s
);
3098 printf("bce: %d\n", i
);
3099 i
= sizeof (__builtin_choose_expr (1, ll
, s
));
3100 printf("bce: %d\n", i
);
3101 i
= sizeof (__builtin_choose_expr (0, ll
, s
));
3102 printf("bce: %d\n", i
);
3104 printf("bera: %p\n", __builtin_extract_return_addr((void*)43));
3108 extern int __attribute__((weak
)) weak_f1(void);
3109 extern int __attribute__((weak
)) weak_f2(void);
3110 extern int weak_f3(void);
3111 extern int __attribute__((weak
)) weak_v1
;
3112 extern int __attribute__((weak
)) weak_v2
;
3115 extern int (*weak_fpa
)() __attribute__((weak
));
3116 extern int __attribute__((weak
)) (*weak_fpb
)();
3117 extern __attribute__((weak
)) int (*weak_fpc
)();
3119 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak
));
3120 extern int __attribute((weak
)) weak_asm_f2(void) asm("weak_asm_f2x") ;
3121 extern int __attribute((weak
)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak
));
3122 extern int weak_asm_v1
asm("weak_asm_v1x") __attribute((weak
));
3123 extern int __attribute((weak
)) weak_asm_v2
asm("weak_asm_v2x") ;
3124 extern int __attribute((weak
)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak
));
3126 static const size_t dummy
= 0;
3127 extern __typeof(dummy
) weak_dummy1
__attribute__((weak
, alias("dummy")));
3128 extern __typeof(dummy
) __attribute__((weak
, alias("dummy"))) weak_dummy2
;
3129 extern __attribute__((weak
, alias("dummy"))) __typeof(dummy
) weak_dummy3
;
3131 int some_lib_func(void);
3132 int dummy_impl_of_slf(void) { return 444; }
3133 int some_lib_func(void) __attribute__((weak
, alias("dummy_impl_of_slf")));
3135 int weak_toolate() __attribute__((weak
));
3136 int weak_toolate() { return 0; }
3138 void __attribute__((weak
)) weak_test(void)
3140 printf("weak_f1=%d\n", weak_f1
? weak_f1() : 123);
3141 printf("weak_f2=%d\n", weak_f2
? weak_f2() : 123);
3142 printf("weak_f3=%d\n", weak_f3
? weak_f3() : 123);
3143 printf("weak_v1=%d\n",&weak_v1
? weak_v1
: 123);
3144 printf("weak_v2=%d\n",&weak_v2
? weak_v2
: 123);
3145 printf("weak_v3=%d\n",&weak_v3
? weak_v3
: 123);
3147 printf("weak_fpa=%d\n",&weak_fpa
? weak_fpa() : 123);
3148 printf("weak_fpb=%d\n",&weak_fpb
? weak_fpb() : 123);
3149 printf("weak_fpc=%d\n",&weak_fpc
? weak_fpc() : 123);
3151 printf("weak_asm_f1=%d\n", weak_asm_f1
!= NULL
);
3152 printf("weak_asm_f2=%d\n", weak_asm_f2
!= NULL
);
3153 printf("weak_asm_f3=%d\n", weak_asm_f3
!= NULL
);
3154 printf("weak_asm_v1=%d\n",&weak_asm_v1
!= NULL
);
3155 printf("weak_asm_v2=%d\n",&weak_asm_v2
!= NULL
);
3156 printf("weak_asm_v3=%d\n",&weak_asm_v3
!= NULL
);
3157 printf("some_lib_func=%d\n", &some_lib_func
? some_lib_func() : 0);
3160 int __attribute__((weak
)) weak_f2() { return 222; }
3161 int __attribute__((weak
)) weak_f3() { return 333; }
3162 int __attribute__((weak
)) weak_v2
= 222;
3163 int __attribute__((weak
)) weak_v3
= 333;
3166 void const_func(const int a
)
3170 void const_warn_test(void)
3179 int getme (struct condstruct
*s
, int i
)
3181 int i1
= (i
== 0 ? 0 : s
)->i
;
3182 int i2
= (i
== 0 ? s
: 0)->i
;
3183 int i3
= (i
== 0 ? (void*)0 : s
)->i
;
3184 int i4
= (i
== 0 ? s
: (void*)0)->i
;
3185 return i1
+ i2
+ i3
+ i4
;
3194 struct global_data global_data
;
3196 int global_data_getstuff (int *, int);
3198 void global_data_callit (int i
)
3200 *global_data
.b
[i
] = global_data_getstuff (global_data
.b
[i
], 1);
3203 int global_data_getstuff (int *p
, int i
)
3208 void global_data_test (void)
3210 global_data
.a
[0] = 42;
3211 global_data
.b
[0] = &global_data
.a
[0];
3212 global_data_callit (0);
3213 printf ("%d\n", global_data
.a
[0]);
3218 unsigned char fill
: 3;
3219 unsigned char b1
: 1;
3220 unsigned char b2
: 1;
3221 unsigned char fill2
: 3;
3224 int glob1
, glob2
, glob3
;
3226 void compare_comparisons (struct cmpcmpS
*s
)
3228 if (s
->b1
!= (glob1
== glob2
)
3229 || (s
->b2
!= (glob1
== glob3
)))
3230 printf ("comparing comparisons broken\n");
3233 void cmp_comparison_test(void)
3237 glob1
= 42; glob2
= 42;
3240 compare_comparisons (&s
);
3243 int fcompare (double a
, double b
, int code
)
3246 case 0: return a
== b
;
3247 case 1: return a
!= b
;
3248 case 2: return a
< b
;
3249 case 3: return a
>= b
;
3250 case 4: return a
> b
;
3251 case 5: return a
<= b
;
3255 void math_cmp_test(void)
3257 double nan
= 0.0/0.0;
3261 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
3263 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
3264 And it does this in various ways so that all code generation paths
3265 are checked (generating inverted tests, or non-inverted tests, or
3266 producing a 0/1 value without jumps (that's done in the fcompare
3268 #define FCMP(a,b,op,iop,code) \
3269 if (fcompare (a,b,code)) \
3270 bug (a,b,op,iop,1); \
3272 bug (a,b,op,iop,2); \
3276 bug (a,b,op,iop,3); \
3277 if ((a op b) || comp) \
3278 bug (a,b,op,iop,4); \
3279 if ((a iop b) || comp) \
3284 /* Equality tests. */
3285 FCMP(nan
, nan
, ==, !=, 0);
3286 FCMP(one
, two
, ==, !=, 0);
3287 FCMP(one
, one
, !=, ==, 1);
3288 /* Non-equality is a bit special. */
3289 if (!fcompare (nan
, nan
, 1))
3290 bug (nan
, nan
, !=, ==, 6);
3292 /* Relational tests on numbers. */
3293 FCMP(two
, one
, <, >=, 2);
3294 FCMP(one
, two
, >=, <, 3);
3295 FCMP(one
, two
, >, <=, 4);
3296 FCMP(two
, one
, <=, >, 5);
3298 /* Relational tests on NaNs. Note that the inverse op here is
3299 always !=, there's no operator in C that is equivalent to !(a < b),
3300 when NaNs are involved, same for the other relational ops. */
3301 FCMP(nan
, nan
, <, !=, 2);
3302 FCMP(nan
, nan
, >=, !=, 3);
3303 FCMP(nan
, nan
, >, !=, 4);
3304 FCMP(nan
, nan
, <=, !=, 5);
3307 double get100 () { return 100.0; }
3309 void callsave_test(void)
3311 #if defined __i386__ || defined __x86_64__ || defined __arm__
3312 int i
, s
; double *d
; double t
;
3313 s
= sizeof (double);
3314 printf ("callsavetest: %d\n", s
);
3315 d
= alloca (sizeof(double));
3317 /* x86-64 had a bug were the next call to get100 would evict
3318 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
3319 in int type, not pointer type. When alloca returns a pointer
3320 with the high 32 bit set (which is likely on x86-64) the access
3321 generates a segfault. */
3322 i
= d
[0] > get100 ();
3328 void bfa3(ptrdiff_t str_offset
)
3330 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset
);
3332 void bfa2(ptrdiff_t str_offset
)
3334 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset
);
3337 void bfa1(ptrdiff_t str_offset
)
3339 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset
);
3343 void builtin_frame_address_test(void)
3345 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
3347 char str
[] = "__builtin_frame_address";
3348 char *fp0
= __builtin_frame_address(0);
3350 printf("str: %s\n", str
);
3355 char via_volatile (char i
)
3362 struct __attribute__((__packed__
)) Spacked
{
3367 struct Spacked spacked
;
3368 typedef struct __attribute__((__packed__
)) {
3375 /* This doesn't work for now. Requires adjusting field offsets/sizes
3376 after parsing the struct members. */
3377 typedef struct Spacked3_s
{
3381 } __attribute__((__packed__
)) Spacked3
;
3384 void attrib_test(void)
3386 printf("attr: %d %d %d %d\n", sizeof(struct Spacked
),
3387 sizeof(spacked
), sizeof(Spacked2
), sizeof(spacked2
));
3389 printf("attr: %d %d\n", sizeof(Spacked3
), sizeof(spacked3
));
3392 extern __attribute__((__unused__
)) char * __attribute__((__unused__
)) *
3393 strange_attrib_placement (void);
3395 void * __attribute__((__unused__
)) get_void_ptr (void *a
)