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();
94 void constant_expr_test();
96 void char_short_test();
98 void compound_literal_test(void);
100 void struct_assign_test(void);
101 void cast_test(void);
102 void bitfield_test(void);
103 void c99_bool_test(void);
104 void float_test(void);
105 void longlong_test(void);
106 void manyarg_test(void);
107 void stdarg_test(void);
108 void whitespace_test(void);
109 void relocation_test(void);
110 void old_style_function(void);
111 void alloca_test(void);
112 void c99_vla_test(int size1
, int size2
);
113 void sizeof_test(void);
114 void typeof_test(void);
115 void local_label_test(void);
116 void statement_expr_test(void);
118 void builtin_test(void);
119 void weak_test(void);
120 void global_data_test(void);
121 void cmp_comparison_test(void);
122 void math_cmp_test(void);
123 void callsave_test(void);
124 void builtin_frame_address_test(void);
125 void attrib_test(void);
129 void forward_ref(void);
132 /* Line joining happens before tokenization, so the following
133 must be parsed as ellipsis. */
134 void funny_line_continuation (int, ..\
137 char via_volatile (char);
142 #define M1(a, b) (a) + (b)
146 #define glue(a, b) a ## b
147 #define xglue(a, b) glue(a, b)
148 #define HIGHLOW "hello"
149 #define LOW LOW ", world"
151 static int onetwothree
= 123;
152 #define onetwothree4 onetwothree
153 #define onetwothree xglue(onetwothree,4)
155 #define min(a, b) ((a) < (b) ? (a) : (b))
158 #define dprintf(level,...) printf(__VA_ARGS__)
161 /* gcc vararg macros */
162 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
164 #define MACRO_NOARGS()
181 /* We try to handle this syntax. Make at least sure it doesn't segfault. */
182 char invalid_function_def()[] {return 0;}
185 #define __INT64_C(c) c ## LL
186 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
194 #define spin_lock(lock) do { } while (0)
195 #define wq_spin_lock spin_lock
196 #define TEST2() wq_spin_lock(a)
198 #define UINT_MAX ((unsigned) -1)
200 void intdiv_test(void)
202 printf("18/21=%u\n", 18/21);
203 printf("18%%21=%u\n", 18%21);
204 printf("41/21=%u\n", 41/21);
205 printf("41%%21=%u\n", 41%21);
206 printf("42/21=%u\n", 42/21);
207 printf("42%%21=%u\n", 42%21);
208 printf("43/21=%u\n", 43/21);
209 printf("43%%21=%u\n", 43%21);
210 printf("126/21=%u\n", 126/21);
211 printf("126%%21=%u\n", 126%21);
212 printf("131/21=%u\n", 131/21);
213 printf("131%%21=%u\n", 131%21);
214 printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX
/2+3)/2);
215 printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX
/2+3)%2);
217 printf("18/-21=%u\n", 18/-21);
218 printf("18%%-21=%u\n", 18%-21);
219 printf("41/-21=%u\n", 41/-21);
220 printf("41%%-21=%u\n", 41%-21);
221 printf("42/-21=%u\n", 42/-21);
222 printf("42%%-21=%u\n", 42%-21);
223 printf("43/-21=%u\n", 43/-21);
224 printf("43%%-21=%u\n", 43%-21);
225 printf("126/-21=%u\n", 126/-21);
226 printf("126%%-21=%u\n", 126%-21);
227 printf("131/-21=%u\n", 131/-21);
228 printf("131%%-21=%u\n", 131%-21);
229 printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX
/2+3)/-2);
230 printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX
/2+3)%-2);
232 printf("-18/21=%u\n", -18/21);
233 printf("-18%%21=%u\n", -18%21);
234 printf("-41/21=%u\n", -41/21);
235 printf("-41%%21=%u\n", -41%21);
236 printf("-42/21=%u\n", -42/21);
237 printf("-42%%21=%u\n", -42%21);
238 printf("-43/21=%u\n", -43/21);
239 printf("-43%%21=%u\n", -43%21);
240 printf("-126/21=%u\n", -126/21);
241 printf("-126%%21=%u\n", -126%21);
242 printf("-131/21=%u\n", -131/21);
243 printf("-131%%21=%u\n", -131%21);
244 printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX
/2+3))/2);
245 printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX
/2+3))%2);
247 printf("-18/-21=%u\n", -18/-21);
248 printf("-18%%-21=%u\n", -18%-21);
249 printf("-41/-21=%u\n", -41/-21);
250 printf("-41%%-21=%u\n", -41%-21);
251 printf("-42/-21=%u\n", -42/-21);
252 printf("-42%%-21=%u\n", -42%-21);
253 printf("-43/-21=%u\n", -43/-21);
254 printf("-43%%-21=%u\n", -43%-21);
255 printf("-126/-21=%u\n", -126/-21);
256 printf("-126%%-21=%u\n", -126%-21);
257 printf("-131/-21=%u\n", -131/-21);
258 printf("-131%%-21=%u\n", -131%-21);
259 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX
/2+3))/-2);
260 printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX
/2+3))%-2);
263 void macro_test(void)
265 printf("macro:\n");\f\v
267 printf("aaa=%d\n", AAA
);
269 printf("min=%d\n", min(1, min(2, -1)));
271 printf("s1=%s\n", glue(HIGH
, LOW
));
272 printf("s2=%s\n", xglue(HIGH
, LOW
));
273 printf("s3=%s\n", str("c"));
274 printf("s4=%s\n", str(a1
));
275 printf("B3=%d\n", B3
);
277 printf("onetwothree=%d\n", onetwothree
);
280 printf("A defined\n");
283 printf("B defined\n");
286 printf("A defined\n");
288 printf("A not defined\n");
291 printf("B defined\n");
293 printf("B not defined\n");
297 printf("A defined\n");
299 printf("B1 defined\n");
301 printf("B1 not defined\n");
304 printf("A not defined\n");
306 printf("B2 defined\n");
308 printf("B2 not defined\n");
313 printf("test true1\n");
316 printf("test true2\n");
319 printf("test true3\n");
322 printf("test trueA\n");
325 printf("test trueB\n");
341 printf("__LINE__ defined\n");
344 printf("__LINE__=%d __FILE__=%s\n",
348 printf("__LINE__=%d __FILE__=%s\n",
351 printf("__LINE__=%d __FILE__=%s\n",
353 #line 227 "tcctest.c"
356 /* not strictly preprocessor, but we test it there */
358 printf("__func__ = %s\n", __func__
);
359 dprintf(1, "vaarg=%d\n", 1);
361 dprintf1(1, "vaarg1\n");
362 dprintf1(1, "vaarg1=%d\n", 2);
363 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
366 printf("func='%s'\n", __FUNCTION__
);
368 /* complicated macros in glibc */
369 printf("INT64_MIN=" LONG_LONG_FORMAT
"\n", INT64_MIN
);
379 /* macro function with argument outside the macro string */
380 #define MF_s MF_hello
381 #define MF_hello(msg) printf("%s\n",msg)
383 #define MF_t printf("tralala\n"); MF_hello
388 /* test macro substitution inside args (should not eat stream) */
389 printf("qq=%d\n", qq(qq
)(2));
391 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
392 null argument without a space. gcc 3.2 fixes that. */
395 printf("qq1=%d\n", qq1( ));
397 /* comment with stray handling *\
399 /* this is a valid *\/ comment */
400 /* this is a valid comment *\*/
404 /* test function macro substitution when the function name is
408 /* And again when the name and parentheses are separated by a
410 TEST2
/* the comment */ ();
412 printf("%s\n", get_basefile_from_header());
413 printf("%s\n", __BASE_FILE__
);
414 printf("%s\n", get_file_from_header());
415 printf("%s\n", __FILE__
);
417 /* Check that funnily named include was in fact included */
418 have_included_42test_h
= 1;
419 have_included_42test_h_second
= 1;
420 have_included_42test_h_third
= 1;
424 static void print_num(char *fn
, int line
, int num
) {
425 printf("fn %s, line %d, num %d\n", fn
, line
, num
);
428 void recursive_macro_test(void)
431 #define ELF32_ST_TYPE(val) ((val) & 0xf)
432 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
433 #define STB_WEAK 2 /* Weak symbol */
434 #define ELFW(type) ELF##32##_##type
435 printf("%d\n", ELFW(ST_INFO
)(STB_WEAK
, ELFW(ST_TYPE
)(123)));
439 #define print_num(x) print_num(__FILE__,__LINE__,x)
441 WRAP(print_num(123));
442 WRAP(WRAP(print_num(123)));
444 static struct recursive_macro
{ int rm_field
; } G
;
445 #define rm_field (G.rm_field)
446 printf("rm_field = %d\n", rm_field
);
447 printf("rm_field = %d\n", WRAP(rm_field
));
448 WRAP((printf("rm_field = %d %d\n", rm_field
, WRAP(rm_field
))));
465 void ps(const char *s
)
477 const char foo1_string
[] = "\
486 printf("\141\1423\143\n");/* dezdez test */
487 printf("\x41\x42\x43\x3a\n");
488 printf("c=%c\n", 'r');
489 printf("wc=%C 0x%lx %C\n", L
'a', L
'\x1234', L
'c');
490 printf("foo1_string='%s'\n", foo1_string
);
492 printf("wstring=%S\n", L
"abc");
493 printf("wstring=%S\n", L
"abc" L
"def" "ghi");
494 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
495 printf("L'\\377'=%d L'\\xff'=%d\n", L
'\377', L
'\xff');
499 while ((b
= b
+ 1) < 96) {
503 printf("fib=%d\n", fib(33));
505 while (b
!= 0x80000000) {
518 for(i
= 0; i
< 10;i
++)
528 /* c99 for loop init test */
529 for (size_t count
= 1; count
< 3; count
++)
530 printf("count=%d\n", count
);
531 printf("count = %d\n", count
);
533 /* break/continue tests */
545 /* break/continue tests */
557 for(i
= 0;i
< 10;i
++) {
565 typedef int typedef_and_label
;
570 static void *label_table
[3] = { &&label1
, &&label2
, &&label3
};
574 /* This needs to parse as label, not as start of decl. */
586 /* we also test computed gotos (GCC extension) */
588 goto *label_table
[i
];
614 enum {E6
= 42, E7
, E8
} e
:8;
618 /* This is either 0 on L32 machines, or a large number
619 on L64 machines. We should be able to store this. */
620 EL_large
= ((unsigned long)0xf000 << 31) << 1,
623 enum { BIASU
= -1U<<31 };
624 enum { BIASS
= -1 << 31 };
626 static int getint(int i
)
631 return (int)(-1U << 31);
637 /* The following should give no warning */
639 struct S_enum s
= {E7
};
640 printf("enum: %d\n", s
.e
);
641 printf("enum:\n%d %d %d %d %d %d\n",
642 E0
, E1
, E2
, E3
, E4
, E5
);
644 printf("b1=%d\n", b1
);
645 printf("enum large: %ld\n", EL_large
);
647 if (getint(0) == BIASU
)
648 printf("enum unsigned: ok\n");
650 printf("enum unsigned: wrong\n");
651 if (getint(0) == BIASS
)
652 printf("enum unsigned: ok\n");
654 printf("enum unsigned: wrong\n");
670 printf("typedef:\n");
671 printf("a=%d\n", *a
);
673 printf("mytype2=%d\n", mytype2
);
678 printf("forward:\n");
684 void forward_ref(void)
686 printf("forward ok\n");
689 typedef struct struct1
{
709 struct struct1 st1
, st2
;
716 int main(int argc
, char **argv
)
721 recursive_macro_test();
737 constant_expr_test();
741 compound_literal_test();
743 struct_assign_test();
753 old_style_function();
758 statement_expr_test();
766 cmp_comparison_test();
769 builtin_frame_address_test();
771 if (via_volatile (42) != 42)
772 printf ("via_volatile broken\n");
784 printf("g1=%d\n", g
);
792 printf("g2=%d\n", g
);
796 printf("g3=%d\n", g
);
800 printf("g4=%d\n", g
);
803 printf("g5=%d\n", g
);
812 for (int st2_i
= 1; st2_i
< 10; st2_i
++) {
815 printf("exloc: %d\n", st2_i
);
817 printf("exloc: %d\n", *st2_p
);
820 /* C has tentative definition, and they may be repeated. */
821 extern int st_global1
;
823 extern int st_global1
;
825 extern int st_global2
;
827 extern int st_global2
;
835 printf("sizeof(a) = %d\n", sizeof(a
));
836 printf("sizeof(\"a\") = %d\n", sizeof("a"));
838 printf("sizeof(__func__) = %d\n", sizeof(__func__
));
840 printf("sizeof tab %d\n", sizeof(tab
));
841 printf("sizeof tab2 %d\n", sizeof tab2
);
845 printf("%d %d %d\n", tab
[0], tab
[1], tab
[2]);
848 tab2
[i
][j
] = 10 * i
+ j
;
850 printf(" %3d", ((int *)tab2
)[i
]);
853 printf("sizeof(size_t)=%d\n", sizeof(size_t));
854 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
861 printf("%d\n", a
+= 1);
862 printf("%d\n", a
-= 2);
863 printf("%d\n", a
*= 31232132);
864 printf("%d\n", a
/= 4);
865 printf("%d\n", a
%= 20);
866 printf("%d\n", a
&= 6);
867 printf("%d\n", a
^= 7);
868 printf("%d\n", a
|= 8);
869 printf("%d\n", a
>>= 3);
870 printf("%d\n", a
<<= 4);
874 printf("%d\n", a
+ 1);
875 printf("%d\n", a
- 2);
876 printf("%d\n", a
* 312);
877 printf("%d\n", a
/ 4);
878 printf("%d\n", b
/ 4);
879 printf("%d\n", (unsigned)b
/ 4);
880 printf("%d\n", a
% 20);
881 printf("%d\n", b
% 20);
882 printf("%d\n", (unsigned)b
% 20);
883 printf("%d\n", a
& 6);
884 printf("%d\n", a
^ 7);
885 printf("%d\n", a
| 8);
886 printf("%d\n", a
>> 3);
887 printf("%d\n", b
>> 3);
888 printf("%d\n", (unsigned)b
>> 3);
889 printf("%d\n", a
<< 4);
894 printf("%d\n", 12 + 1);
895 printf("%d\n", 12 - 2);
896 printf("%d\n", 12 * 312);
897 printf("%d\n", 12 / 4);
898 printf("%d\n", 12 % 20);
899 printf("%d\n", 12 & 6);
900 printf("%d\n", 12 ^ 7);
901 printf("%d\n", 12 | 8);
902 printf("%d\n", 12 >> 2);
903 printf("%d\n", 12 << 4);
907 printf("%d %d %d %d\n",
916 return (c
>= 'a' & c
<= 'z') | (c
>= 'A' & c
<= 'Z') | c
== '_';
919 /**********************/
921 int vstack
[10], *vstack_ptr
;
923 void vpush(int vt
, int vc
)
929 void vpop(int *ft
, int *fc
)
942 vstack_ptr
[-2] &= ~0xffffff80;
944 printf("res= %d %d\n", a
, b
);
947 void constant_expr_test()
950 printf("constant_expr:\n");
952 printf("%d\n", a
* 16);
953 printf("%d\n", a
* 1);
954 printf("%d\n", a
+ 0);
964 printf("expr_ptr:\n");
967 printf("diff=%d\n", q
- p
);
969 printf("inc=%d\n", p
- tab4
);
971 printf("dec=%d\n", p
- tab4
);
973 printf("inc=%d\n", p
- tab4
);
975 printf("dec=%d\n", p
- tab4
);
976 printf("add=%d\n", p
+ 3 - tab4
);
977 printf("add=%d\n", 3 + p
- tab4
);
979 /* check if 64bit support is ok */
982 printf("%p %p %ld\n", q
, p
, p
-q
);
983 printf("%d %d %d %d %d %d\n",
984 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
987 printf("%p %p %ld\n", q
, p
, p
-q
);
988 printf("%d %d %d %d %d %d\n",
989 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
990 p
= (int *)((char *)p
+ 0xf0000000);
991 printf("%p %p %ld\n", q
, p
, p
-q
);
992 printf("%d %d %d %d %d %d\n",
993 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
995 printf("%p %p %ld\n", q
, p
, p
-q
);
996 printf("%d %d %d %d %d %d\n",
997 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
1002 struct size12 s
[2], *sp
= s
;
1007 printf("%d\n", sp
[j
].i
);
1011 p
= (int*)0x100000000UL
+ i
;
1012 i
= ((long)p
) >> 32;
1013 printf("largeptr: %p %d\n", p
, i
);
1017 void expr_cmp_test()
1020 printf("constant_expr:\n");
1023 printf("%d\n", a
== a
);
1024 printf("%d\n", a
!= a
);
1026 printf("%d\n", a
< b
);
1027 printf("%d\n", a
<= b
);
1028 printf("%d\n", a
<= a
);
1029 printf("%d\n", b
>= a
);
1030 printf("%d\n", a
>= a
);
1031 printf("%d\n", b
> a
);
1033 printf("%d\n", (unsigned)a
< b
);
1034 printf("%d\n", (unsigned)a
<= b
);
1035 printf("%d\n", (unsigned)a
<= a
);
1036 printf("%d\n", (unsigned)b
>= a
);
1037 printf("%d\n", (unsigned)a
>= a
);
1038 printf("%d\n", (unsigned)b
> a
);
1061 struct __attribute__((aligned(16))) aligntest5
1068 } __attribute__((aligned(16)));
1073 struct aligntest5 altest5
[2];
1074 struct aligntest6 altest6
[2];
1076 /* altest7 is correctly aligned to 16 bytes also with TCC,
1077 but __alignof__ returns the wrong result (4) because we
1078 can't store the alignment yet when specified on symbols
1079 directly (it's stored in the type so we'd need to make
1080 a copy of it). -- FIXED */
1081 struct aligntest7 altest7
[2] __attribute__((aligned(16)));
1086 } __attribute__((aligned(4096)));
1089 unsigned long flags
;
1097 unsigned long index
;
1101 unsigned long counters
;
1115 unsigned long compound_head
;
1116 unsigned int compound_dtor
;
1117 unsigned int compound_order
;
1120 } __attribute__((aligned(2 * sizeof(long))));
1122 typedef unsigned long long __attribute__((aligned(4))) unaligned_u64
;
1125 unsigned int buf_nr
;
1126 unaligned_u64 start_lba
;
1129 struct aligntest10
{
1130 unsigned int buf_nr
;
1131 unsigned long long start_lba
;
1140 printf("struct:\n");
1141 printf("sizes: %d %d %d %d\n",
1142 sizeof(struct struct1
),
1143 sizeof(struct struct2
),
1144 sizeof(union union1
),
1145 sizeof(union union2
));
1146 printf("offsets: %d\n", (int)((char*)&st1
.u
.v1
- (char*)&st1
));
1150 printf("st1: %d %d %d\n",
1151 st1
.f1
, st1
.f2
, st1
.f3
);
1154 printf("union1: %d\n", st1
.u
.v1
);
1157 printf("union2: %d\n", u
.w1
);
1162 printf("st2: %d %d %d\n",
1163 s
->f1
, s
->f2
, s
->f3
);
1164 printf("str_addr=%x\n", (int)st1
.str
- (int)&st1
.f1
);
1166 /* align / size tests */
1167 printf("aligntest1 sizeof=%d alignof=%d\n",
1168 sizeof(struct aligntest1
), __alignof__(struct aligntest1
));
1169 printf("aligntest2 sizeof=%d alignof=%d\n",
1170 sizeof(struct aligntest2
), __alignof__(struct aligntest2
));
1171 printf("aligntest3 sizeof=%d alignof=%d\n",
1172 sizeof(struct aligntest3
), __alignof__(struct aligntest3
));
1173 printf("aligntest4 sizeof=%d alignof=%d\n",
1174 sizeof(struct aligntest4
), __alignof__(struct aligntest4
));
1175 printf("aligntest5 sizeof=%d alignof=%d\n",
1176 sizeof(struct aligntest5
), __alignof__(struct aligntest5
));
1177 printf("aligntest6 sizeof=%d alignof=%d\n",
1178 sizeof(struct aligntest6
), __alignof__(struct aligntest6
));
1179 printf("aligntest7 sizeof=%d alignof=%d\n",
1180 sizeof(struct aligntest7
), __alignof__(struct aligntest7
));
1181 printf("aligntest8 sizeof=%d alignof=%d\n",
1182 sizeof(struct aligntest8
), __alignof__(struct aligntest8
));
1183 printf("aligntest9 sizeof=%d alignof=%d\n",
1184 sizeof(struct aligntest9
), __alignof__(struct aligntest9
));
1185 printf("aligntest10 sizeof=%d alignof=%d\n",
1186 sizeof(struct aligntest10
), __alignof__(struct aligntest10
));
1187 printf("altest5 sizeof=%d alignof=%d\n",
1188 sizeof(altest5
), __alignof__(altest5
));
1189 printf("altest6 sizeof=%d alignof=%d\n",
1190 sizeof(altest6
), __alignof__(altest6
));
1191 printf("altest7 sizeof=%d alignof=%d\n",
1192 sizeof(altest7
), __alignof__(altest7
));
1194 /* empty structures (GCC extension) */
1195 printf("sizeof(struct empty) = %d\n", sizeof(struct empty
));
1196 printf("alignof(struct empty) = %d\n", __alignof__(struct empty
));
1198 printf("Large: sizeof=%d\n", sizeof(ls
));
1199 memset(&ls
, 0, sizeof(ls
));
1200 ls
.compound_head
= 42;
1201 printf("Large: offsetof(compound_head)=%d\n", (int)((char*)&ls
.compound_head
- (char*)&ls
));
1204 /* XXX: depend on endianness */
1205 void char_short_test()
1209 printf("char_short:\n");
1213 printf("s8=%d %d\n",
1214 *(char *)&var1
, *(char *)&var2
);
1215 printf("u8=%d %d\n",
1216 *(unsigned char *)&var1
, *(unsigned char *)&var2
);
1217 printf("s16=%d %d\n",
1218 *(short *)&var1
, *(short *)&var2
);
1219 printf("u16=%d %d\n",
1220 *(unsigned short *)&var1
, *(unsigned short *)&var2
);
1221 printf("s32=%d %d\n",
1222 *(int *)&var1
, *(int *)&var2
);
1223 printf("u32=%d %d\n",
1224 *(unsigned int *)&var1
, *(unsigned int *)&var2
);
1225 *(char *)&var1
= 0x08;
1226 printf("var1=%x\n", var1
);
1227 *(short *)&var1
= 0x0809;
1228 printf("var1=%x\n", var1
);
1229 *(int *)&var1
= 0x08090a0b;
1230 printf("var1=%x\n", var1
);
1233 /******************/
1235 typedef struct Sym
{
1243 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1244 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1246 static int toupper1(int a
)
1251 static unsigned int calc_vm_flags(unsigned int prot
)
1253 unsigned int prot_bits
;
1254 /* This used to segfault in some revisions: */
1255 prot_bits
= ((0x1==0x00000001)?(prot
&0x1):(prot
&0x1)?0x00000001:0);
1261 int *s
, a
, b
, t
, f
, i
;
1265 printf("!s=%d\n", !s
);
1269 printf("a=%d\n", a
);
1271 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1272 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1273 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1288 printf("b=%d\n", a
+ (0 ? 1 : a
/ 2));
1290 /* test register spilling */
1293 a
= (a
+ b
) * ((a
< b
) ?
1294 ((b
- a
) * (a
- b
)): a
+ b
);
1295 printf("a=%d\n", a
);
1297 /* test complex || or && expressions */
1301 printf("exp=%d\n", f
== (32 <= a
&& a
<= 3));
1302 printf("r=%d\n", (t
|| f
) + (t
&& f
));
1307 int aspect_native
= 65536;
1308 double bfu_aspect
= 1.0;
1310 for(aspect_on
= 0; aspect_on
< 2; aspect_on
++) {
1311 aspect
=aspect_on
?(aspect_native
*bfu_aspect
+0.5):65535UL;
1312 printf("aspect=%d\n", aspect
);
1316 /* test ? : GCC extension */
1318 static int v1
= 34 ? : -1; /* constant case */
1319 static int v2
= 0 ? : -1; /* constant case */
1322 printf("%d %d\n", v1
, v2
);
1323 printf("%d %d\n", a
- 30 ? : a
* 2, a
+ 1 ? : a
* 2);
1326 /* again complex expression */
1327 for(i
=0;i
<256;i
++) {
1328 if (toupper1 (i
) != TOUPPER (i
))
1329 printf("error %d\n", i
);
1331 printf ("bits = 0x%x\n", calc_vm_flags (0x1));
1334 extern int undefined_function(void);
1335 extern int defined_function(void);
1337 static inline void refer_to_undefined(void)
1339 undefined_function();
1342 void optimize_out(void)
1344 int i
= 0 ? undefined_function() : defined_function();
1345 printf ("oo:%d\n", i
);
1346 int j
= 1 ? defined_function() : undefined_function();
1347 printf ("oo:%d\n", j
);
1349 printf("oo:%d\n", undefined_function());
1351 printf("oo:%d\n", defined_function());
1353 printf("oo:%d\n", defined_function());
1355 printf("oo:%d\n", undefined_function());
1357 printf("oow:%d\n", defined_function());
1359 printf("oow:%d\n", undefined_function());
1362 /* Following is a switch without {} block intentionally. */
1365 printf ("oos:%d\n", defined_function());
1366 /* The following break shouldn't lead to disabled code after
1370 printf ("ool1:%d\n", defined_function());
1371 /* Same for the other types of loops. */
1375 printf ("ool2:%d\n", defined_function());
1378 printf ("ool3:%d\n", defined_function());
1379 /* Normal {} blocks without controlling statements
1380 shouldn't reactivate code emission */
1385 printf ("ool4:%d\n", undefined_function());
1390 break; /* this break shouldn't disable code outside the if. */
1391 printf("ool5:%d\n", defined_function());
1401 printf("ool6:%d\n", defined_function());
1408 printf("ool7:%d\n", undefined_function());
1411 /* Test that constants in logical && are optimized: */
1412 i
= 0 && undefined_function();
1413 i
= defined_function() && 0 && undefined_function();
1414 if (0 && undefined_function())
1415 undefined_function();
1416 if (defined_function() && 0)
1417 undefined_function();
1419 undefined_function();
1420 if (defined_function() && 0 && undefined_function())
1421 undefined_function();
1422 /* The same for || : */
1423 i
= 1 || undefined_function();
1424 i
= defined_function() || 1 || undefined_function();
1425 if (1 || undefined_function())
1428 undefined_function();
1429 if (defined_function() || 1)
1432 undefined_function();
1436 undefined_function();
1437 if (defined_function() || 1 || undefined_function())
1440 undefined_function();
1442 if (defined_function() && 0)
1443 refer_to_undefined();
1450 undefined_function();
1453 /* Leave the "if(1)return; printf()" in this order and last in the function */
1456 printf ("oor:%d\n", undefined_function());
1459 int defined_function(void)
1465 /* GCC accepts that */
1466 static int tab_reinit
[];
1467 static int tab_reinit
[10];
1469 static int tentative_ar
[];
1470 static int tentative_ar
[] = {1,2,3};
1472 //int cinit1; /* a global variable can be defined several times without error ! */
1476 int *cinit2
= (int []){3, 2, 1};
1478 void compound_literal_test(void)
1483 printf("compound_test:\n");
1485 p
= (int []){1, 2, 3};
1487 printf(" %d", p
[i
]);
1491 printf("%d", cinit2
[i
]);
1495 printf("q1=%s\n", q
);
1497 q
= (char *){ "tralala2" };
1498 printf("q2=%s\n", q
);
1501 printf("q3=%s\n", q3
);
1503 q
= (char []){ "tralala3" };
1504 printf("q4=%s\n", q
);
1507 p
= (int []){1, 2, cinit1
+ 3};
1509 printf(" %d", p
[i
]);
1513 p
= (int []){1, 2, 4 + i
};
1514 printf("%d %d %d\n",
1536 printf("kr_test:\n");
1537 printf("func1=%d\n", kr_func1(3, 4));
1538 printf("func2=%d\n", kr_func2(3, 4));
1545 tab
= (char*)malloc(20);
1562 /* structure assignment tests */
1568 struct structa1 ssta1
;
1570 void struct_assign_test1(struct structa1 s1
, int t
, float f
)
1572 printf("%d %d %d %f\n", s1
.f1
, s1
.f2
, t
, f
);
1575 struct structa1
struct_assign_test2(struct structa1 s1
, int t
)
1582 void struct_assign_test(void)
1585 struct structa1 lsta1
, lsta2
;
1592 printf("struct_assign_test:\n");
1596 printf("%d %d\n", s
.lsta1
.f1
, s
.lsta1
.f2
);
1598 printf("%d %d\n", s
.lsta2
.f1
, s
.lsta2
.f2
);
1603 struct_assign_test1(ps
->lsta2
, 3, 4.5);
1605 printf("before call: %d %d\n", s
.lsta2
.f1
, s
.lsta2
.f2
);
1606 ps
->lsta2
= struct_assign_test2(ps
->lsta2
, ps
->i
);
1607 printf("after call: %d %d\n", ps
->lsta2
.f1
, ps
->lsta2
.f2
);
1612 /* XXX: we should allow this even without braces */
1613 { struct_assign_test
}
1615 printf("%d\n", struct_assign_test
== t
[0].elem
);
1618 /* casts to short/char */
1620 void cast1(char a
, short b
, unsigned char c
, unsigned short d
)
1622 printf("%d %d %d %d\n", a
, b
, c
, d
);
1636 p
-= 0x700000000042;
1638 printf("cast_test:\n");
1642 printf("%d %d %d %d\n",
1645 (unsigned char)(a
+ 1),
1646 (unsigned short)(a
+ 1));
1647 printf("%d %d %d %d\n",
1650 (unsigned char)0xfffff,
1651 (unsigned short)0xfffff);
1653 a
= (bcast
= 128) + 1;
1655 a
= (scast
= 65536) + 1;
1658 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
1660 /* test cast from unsigned to signed short to int */
1663 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b
, d
);
1666 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b
, d
);
1668 /* test implicit int casting for array accesses */
1672 printf("%d %d\n", tab
[0], tab
[1]);
1674 /* test implicit casting on some operators */
1675 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1676 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1677 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1679 /* from pointer to integer types */
1680 printf("%d %d %ld %ld %lld %lld\n",
1681 (int)p
, (unsigned int)p
,
1682 (long)p
, (unsigned long)p
,
1683 (long long)p
, (unsigned long long)p
);
1685 /* from integers to pointers */
1686 printf("%p %p %p %p\n",
1687 (void *)a
, (void *)b
, (void *)c
, (void *)d
);
1690 /* initializers tests */
1691 struct structinit1
{
1700 int sinit3
[3] = { 1, 2, {{3}}, };
1701 int sinit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1702 int sinit5
[3][2] = { 1, 2, 3, 4, 5, 6 };
1703 int sinit6
[] = { 1, 2, 3 };
1704 int sinit7
[] = { [2] = 3, [0] = 1, 2 };
1705 char sinit8
[] = "hello" "trala";
1707 struct structinit1 sinit9
= { 1, 2, 3 };
1708 struct structinit1 sinit10
= { .f2
= 2, 3, .f1
= 1 };
1709 struct structinit1 sinit11
= { .f2
= 2, 3, .f1
= 1,
1717 char *sinit12
= "hello world";
1723 char sinit14
[10] = { "abc" };
1724 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
1726 struct { int a
[3], b
; } sinit16
[] = { { 1 }, 2 };
1742 struct complexinit0
{
1747 struct complexinit
{
1749 const struct complexinit0
*b
;
1752 const static struct complexinit cix
[] = {
1755 .b
= (const struct complexinit0
[]) {
1763 struct complexinit2
{
1768 struct complexinit2 cix20
;
1770 struct complexinit2 cix21
= {
1772 .b
= { 3001, 3002, 3003 }
1775 struct complexinit2 cix22
= {
1777 .b
= { 4001, 4002, 4003, 4004, 4005, 4006 }
1780 typedef int arrtype1
[];
1781 arrtype1 sinit19
= {1};
1782 arrtype1 sinit20
= {2,3};
1783 typedef int arrtype2
[3];
1784 arrtype2 sinit21
= {4};
1785 arrtype2 sinit22
= {5,6,7};
1787 /* Address comparisons of non-weak symbols with zero can be const-folded */
1788 int sinit23
[2] = { "astring" ? sizeof("astring") : -1,
1789 &sinit23
? 42 : -1 };
1791 extern int external_inited
= 42;
1793 void init_test(void)
1797 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1798 int linit6
[] = { 1, 2, 3 };
1800 char linit8
[] = "hello" "trala";
1801 int linit12
[10] = { 1, 2 };
1802 int linit13
[10] = { 1, 2, [7] = 3, [3] = 4, };
1803 char linit14
[10] = "abc";
1804 int linit15
[10] = { linit1
, linit1
+ 1, [6] = linit1
+ 2, };
1805 struct linit16
{ int a1
, a2
, a3
, a4
; } linit16
= { 1, .a3
= 2 };
1806 int linit17
= sizeof(linit17
);
1808 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1809 int linit18
[2] = {&zero
? 1 : -1, zero
? -1 : 1 };
1811 printf("init_test:\n");
1813 printf("sinit1=%d\n", sinit1
);
1814 printf("sinit2=%d\n", sinit2
);
1815 printf("sinit3=%d %d %d %d\n",
1821 printf("sinit6=%d\n", sizeof(sinit6
));
1822 printf("sinit7=%d %d %d %d\n",
1828 printf("sinit8=%s\n", sinit8
);
1829 printf("sinit9=%d %d %d\n",
1834 printf("sinit10=%d %d %d\n",
1839 printf("sinit11=%d %d %d %d %d %d\n",
1850 printf("[%d][%d] = %d %d %d\n",
1851 i
, j
, sinit4
[i
][j
], sinit5
[i
][j
], linit4
[i
][j
]);
1852 printf("linit1=%d\n", linit1
);
1853 printf("linit2=%d\n", linit2
);
1854 printf("linit6=%d\n", sizeof(linit6
));
1855 printf("linit8=%d %s\n", sizeof(linit8
), linit8
);
1857 printf("sinit12=%s\n", sinit12
);
1858 printf("sinit13=%d %s %s %s\n",
1863 printf("sinit14=%s\n", sinit14
);
1865 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
1867 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
1869 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
1871 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
1873 printf("%d %d %d %d\n",
1878 /* test that initialisation is done after variable declare */
1879 printf("linit17=%d\n", linit17
);
1880 printf("sinit15=%d\n", sinit15
[0]);
1881 printf("sinit16=%d %d\n", sinit16
[0].a
[0], sinit16
[1].a
[0]);
1882 printf("sinit17=%s %d %s %d\n",
1883 sinit17
[0].s
, sinit17
[0].len
,
1884 sinit17
[1].s
, sinit17
[1].len
);
1886 printf("%x ", sinit18
[i
]);
1888 /* complex init check */
1889 printf("cix: %d %d %d %d %d %d %d\n",
1891 cix
[0].b
[0].a
, cix
[0].b
[0].b
,
1892 cix
[0].b
[1].a
, cix
[0].b
[1].b
,
1893 cix
[0].b
[2].a
, cix
[0].b
[2].b
);
1894 printf("cix2: %d %d\n", cix21
.b
[2], cix22
.b
[5]);
1895 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20
, sizeof cix21
, sizeof cix22
);
1897 printf("arrtype1: %d %d %d\n", sinit19
[0], sinit20
[0], sinit20
[1]);
1898 printf("arrtype2: %d %d\n", sizeof(sinit19
), sizeof(sinit20
));
1899 printf("arrtype3: %d %d %d\n", sinit21
[0], sinit21
[1], sinit21
[2]);
1900 printf("arrtype4: %d %d %d\n", sinit22
[0], sinit22
[1], sinit22
[2]);
1901 printf("arrtype5: %d %d\n", sizeof(sinit21
), sizeof(sinit22
));
1902 printf("arrtype6: %d\n", sizeof(arrtype2
));
1904 printf("sinit23= %d %d\n", sinit23
[0], sinit23
[1]);
1905 printf("linit18= %d %d\n", linit18
[0], linit18
[1]);
1908 void switch_uc(unsigned char uc
)
1921 printf("ucsw: broken!\n");
1925 void switch_sc(signed char sc
)
1938 printf("scsw: broken!\n");
1945 unsigned long long ull
;
1970 for (i
= 1; i
<= 5; i
++) {
1971 ull
= (unsigned long long)i
<< 61;
1974 printf("ullsw:1\n");
1977 printf("ullsw:2\n");
1980 printf("ullsw:3\n");
1983 printf("ullsw:4\n");
1986 printf("ullsw:5\n");
1989 printf("ullsw: broken!\n");
1993 for (i
= 1; i
<= 5; i
++) {
1994 ll
= (long long)i
<< 61;
2012 printf("llsw: broken!\n");
2016 for (i
= -5; i
<= 5; i
++) {
2017 switch_uc((unsigned char)i
);
2020 for (i
= -5; i
<= 5; i
++) {
2021 switch_sc ((signed char)i
);
2025 /* ISOC99 _Bool type */
2026 void c99_bool_test(void)
2032 printf("bool_test:\n");
2033 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
2035 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
2037 printf("b = %d\n", b
);
2039 printf("b = %d\n", b
);
2043 void bitfield_test(void)
2055 unsigned int f5
: 7;
2057 printf("bitfield_test:");
2058 printf("sizeof(st1) = %d\n", sizeof(st1
));
2067 printf("%d %d %d %d %d\n",
2068 st1
.f1
, st1
.f2
, st1
.f3
, st1
.f4
, st1
.f5
);
2071 printf("%d %d\n", sa
, ca
);
2075 printf("st1.f1 == -1\n");
2077 printf("st1.f1 != -1\n");
2079 printf("st1.f2 == -1\n");
2081 printf("st1.f2 != -1\n");
2087 unsigned long long f3
: 38;
2089 st2
.f1
= 0x123456789ULL
;
2091 st2
.f2
= (long long)a
<< 25;
2094 printf("%lld %lld %lld\n", st2
.f1
, st2
.f2
, st2
.f3
);
2097 Disabled
for now until further clarification re GCC compatibility
2106 printf("sizeof(st3) = %d\n", sizeof(st3
));
2114 printf("st4.y == %d\n", st4
.y
);
2118 int x
: 12, y
: 4, : 0, : 4, z
: 3;
2120 } st5
= { 1, 2, 3, 4, -3, 6 };
2121 printf("st5 = %d %d %d %d %d %d\n", st5
.a
, st5
.b
, st5
.x
, st5
.y
, st5
.z
, st5
.c
);
2124 unsigned char y
: 2;
2127 printf("st6.y == %d\n", st6
.y
);
2131 #define FLOAT_FMT "%f\n"
2133 /* x86's float isn't compatible with GCC */
2134 #define FLOAT_FMT "%.5f\n"
2137 /* declare strto* functions as they are C99 */
2138 double strtod(const char *nptr
, char **endptr
);
2141 float strtof(const char *nptr
, char **endptr
) {return (float)strtod(nptr
, endptr
);}
2142 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
) {return (LONG_DOUBLE
)strtod(nptr
, endptr
);}
2144 float strtof(const char *nptr
, char **endptr
);
2145 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
);
2148 #define FTEST(prefix, typename, type, fmt)\
2149 void prefix ## cmp(type a, type b)\
2151 printf("%d %d %d %d %d %d\n",\
2158 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
2166 printf(fmt "\n", ++a);\
2167 printf(fmt "\n", a++);\
2168 printf(fmt "\n", a);\
2170 printf("%d %d\n", !a, !b);\
2172 void prefix ## fcast(type a)\
2180 unsigned long long llua;\
2185 printf("ftof: %f %f %Lf\n", fa, da, la);\
2187 llia = (long long)a;\
2188 a = (a >= 0) ? a : -a;\
2189 ua = (unsigned int)a;\
2190 llua = (unsigned long long)a;\
2191 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
2194 llia = -0x123456789012345LL;\
2195 llua = 0xf123456789012345LLU;\
2197 printf("itof: " fmt "\n", b);\
2199 printf("utof: " fmt "\n", b);\
2201 printf("lltof: " fmt "\n", b);\
2203 printf("ulltof: " fmt "\n", b);\
2206 float prefix ## retf(type a) { return a; }\
2207 double prefix ## retd(type a) { return a; }\
2208 LONG_DOUBLE prefix ## retld(type a) { return a; }\
2210 void prefix ## call(void)\
2212 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
2213 printf("double: %f\n", prefix ## retd(42.123456789));\
2214 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
2215 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
2218 void prefix ## signed_zeros(void) \
2220 type x = 0.0, y = -0.0, n, p;\
2222 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
2223 1.0 / x != 1.0 / y);\
2225 printf ("x != y; this is wrong!\n");\
2229 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
2230 1.0 / x != 1.0 / n);\
2232 printf ("x != -x; this is wrong!\n");\
2236 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
2237 1.0 / x != 1.0 / p);\
2239 printf ("x != +y; this is wrong!\n");\
2242 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
2243 1.0 / x != 1.0 / p);\
2245 printf ("x != -y; this is wrong!\n");\
2247 void prefix ## test(void)\
2249 printf("testing '%s'\n", #typename);\
2250 prefix ## cmp(1, 2.5);\
2251 prefix ## cmp(2, 1.5);\
2252 prefix ## cmp(1, 1);\
2253 prefix ## fcast(234.6);\
2254 prefix ## fcast(-2334.6);\
2256 prefix ## signed_zeros();\
2259 FTEST(f
, float, float, "%f")
2260 FTEST(d
, double, double, "%f")
2261 FTEST(ld
, long double, LONG_DOUBLE
, "%Lf")
2263 double ftab1
[3] = { 1.2, 3.4, -5.6 };
2266 void float_test(void)
2268 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
2273 static double nan2
= 0.0/0.0;
2274 static double inf1
= 1.0/0.0;
2275 static double inf2
= 1e5000
;
2277 printf("float_test:\n");
2278 printf("sizeof(float) = %d\n", sizeof(float));
2279 printf("sizeof(double) = %d\n", sizeof(double));
2280 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE
));
2284 printf("%f %f %f\n", ftab1
[0], ftab1
[1], ftab1
[2]);
2285 printf("%f %f %f\n", 2.12, .5, 2.3e10
);
2286 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
2288 printf("da=%f\n", da
);
2290 printf("fa=%f\n", fa
);
2293 printf("da = %f\n", da
);
2296 printf("db = %f\n", db
);
2297 printf("nan != nan = %d, inf1 = %f, inf2 = %f\n", nan2
!= nan2
, inf1
, inf2
);
2306 return fib(n
-1) + fib(n
-2);
2309 void __attribute__((aligned(16))) aligned_function(int i
) {}
2321 printf("funcptr:\n");
2328 /* more complicated pointer computation */
2331 printf("sizeof1 = %d\n", sizeof(funcptr_test
));
2332 printf("sizeof2 = %d\n", sizeof funcptr_test
);
2333 printf("sizeof3 = %d\n", sizeof(&funcptr_test
));
2334 printf("sizeof4 = %d\n", sizeof &funcptr_test
);
2342 /* Check that we can align functions */
2343 func
= aligned_function
;
2344 printf("aligned_function (should be zero): %d\n", ((int)func
) & 15);
2347 void lloptest(long long a
, long long b
)
2349 unsigned long long ua
, ub
;
2354 printf("arith: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2360 printf("arith1: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2366 printf("bin: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2372 printf("test: %d %d %d %d %d %d\n",
2380 printf("utest: %d %d %d %d %d %d\n",
2391 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2392 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
++, b
++);
2393 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", --a
, --b
);
2394 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2396 printf("not: %d %d %d %d\n", !a
, !ua
, !b
, !ub
);
2399 void llshift(long long a
, int b
)
2401 printf("shift: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2402 (unsigned long long)a
>> b
,
2405 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2406 (unsigned long long)a
>> 3,
2409 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2410 (unsigned long long)a
>> 35,
2420 long long la
, lb
, lc
;
2421 unsigned long long ula
, ulb
, ulc
;
2424 la
= (la
<< 20) | 0x12345;
2426 printf("la=" LONG_LONG_FORMAT
" ula=" ULONG_LONG_FORMAT
"\n", la
, ula
);
2431 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
2436 printf("ftoll: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", la
, lb
, lc
);
2441 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
2446 printf("ftoull: " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
"\n", ula
, ulb
, ulc
);
2449 long long llfunc1(int a
)
2459 long long int value(struct S
*v
)
2461 return ((long long int)v
->item
);
2464 long long llfunc2(long long x
, long long y
, int z
)
2469 void check_opl_save_regs(char *a
, long long b
, int c
)
2474 void longlong_test(void)
2479 printf("longlong_test:\n");
2480 printf("sizeof(long long) = %d\n", sizeof(long long));
2485 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2486 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %Lx\n",
2490 0x1234567812345679);
2492 printf(LONG_LONG_FORMAT
"\n", a
);
2495 lloptest(0xff, 0x1234);
2496 b
= 0x72345678 << 10;
2500 b
= 0x72345678LL
<< 10;
2511 /* long long reg spill test */
2516 printf("%lld\n", value(&a
));
2518 lloptest(0x80000000, 0);
2521 long long *p
, v
, **pp
;
2525 printf("another long long spill test : %lld\n", *p
);
2528 v
= llfunc2(**pp
, **pp
, ia
);
2529 printf("a long long function (arm-)reg-args test : %lld\n", v
);
2533 printf("%d %d %d %d\n", a
> b
, a
< b
, a
>= b
, a
<= b
);
2535 printf(LONG_LONG_FORMAT
"\n", 0x123456789LLU
);
2537 /* long long pointer deref in argument passing test */
2542 /* shortening followed by widening */
2543 unsigned long long u
= 0x8000000000000001ULL
;
2544 u
= (unsigned)(u
+ 1);
2545 printf("long long u=" ULONG_LONG_FORMAT
"\n", u
);
2547 /* was a problem with missing save_regs in gen_opl on 32-bit platforms */
2549 check_opl_save_regs(&cc
, -1, 0);
2550 printf("check_opl_save_regs: %d\n", cc
);
2553 void manyarg_test(void)
2555 LONG_DOUBLE ld
= 1234567891234LL;
2556 printf("manyarg_test:\n");
2557 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2558 1, 2, 3, 4, 5, 6, 7, 8,
2559 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2560 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2561 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2562 1, 2, 3, 4, 5, 6, 7, 8,
2563 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2564 1234567891234LL, 987654321986LL,
2566 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2567 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2568 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2569 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2570 1234567891234LL, 987654321986LL,
2572 printf("%d %d %d %d %d %d %d %d %Lf\n",
2573 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2574 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2575 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"%f %f %Lf\n",
2576 1, 2, 3, 4, 5, 6, 7, 8,
2577 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2578 1234567891234LL, 987654321986LL,
2580 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2581 "%Lf " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f %Lf\n",
2582 1, 2, 3, 4, 5, 6, 7, 8,
2583 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2584 ld
, 1234567891234LL, 987654321986LL,
2588 void vprintf1(const char *fmt
, ...)
2612 i
= va_arg(ap
, int);
2616 d
= va_arg(ap
, double);
2620 ll
= va_arg(ap
, long long);
2621 printf(LONG_LONG_FORMAT
, ll
);
2624 ld
= va_arg(ap
, LONG_DOUBLE
);
2642 void stdarg_for_struct(struct myspace bob
, ...)
2644 struct myspace george
, bill
;
2649 bill
= va_arg(ap
, struct myspace
);
2650 george
= va_arg(ap
, struct myspace
);
2651 validate
= va_arg(ap
, int);
2652 printf("stdarg_for_struct: %d %d %d %d\n",
2653 bob
.profile
, bill
.profile
, george
.profile
, validate
);
2657 void stdarg_for_libc(const char *fmt
, ...)
2660 va_start(args
, fmt
);
2665 void stdarg_syntax(int n
, ...)
2673 i
= va_arg(ap
, int);
2674 printf("stdarg_void_expr: %d\n", i
);
2678 void stdarg_test(void)
2680 LONG_DOUBLE ld
= 1234567891234LL;
2683 vprintf1("%d %d %d\n", 1, 2, 3);
2684 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2685 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2686 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2687 vprintf1("%d %f %l %F %d %f %l %F\n",
2688 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2689 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2690 1, 2, 3, 4, 5, 6, 7, 8,
2691 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2692 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2693 1, 2, 3, 4, 5, 6, 7, 8,
2694 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2695 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2697 1, 2, 3, 4, 5, 6, 7, 8,
2698 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2699 1234567891234LL, 987654321986LL,
2701 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2703 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2704 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2705 1234567891234LL, 987654321986LL,
2707 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2708 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2709 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2711 1, 2, 3, 4, 5, 6, 7, 8,
2712 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2713 1234567891234LL, 987654321986LL,
2715 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2716 "%F %l %l %f %f %F\n",
2717 1, 2, 3, 4, 5, 6, 7, 8,
2718 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2719 ld
, 1234567891234LL, 987654321986LL,
2723 stdarg_for_struct(bob
, bob
, bob
, bob
.profile
);
2724 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2725 stdarg_syntax(1, 17);
2728 void whitespace_test(void)
2734 ntf("whitspace:\n");\f\v
2738 #ifdef CORRECT_CR_HANDLING
2747 #ifdef ACCEPT_CR_IN_STRINGS
2748 printf("len1=%d\n", strlen("
2750 #ifdef CORRECT_CR_HANDLING
2753 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
2755 printf("len1=%d\n", strlen("
a
2757 #endif /* ACCEPT_CR_IN_STRINGS */
2760 int reltab
[3] = { 1, 2, 3 };
2762 int *rel1
= &reltab
[1];
2763 int *rel2
= &reltab
[2];
2766 void relocation_test(void) {}
2768 void getmyaddress(void)
2770 printf("in getmyaddress\n");
2774 long __pa_symbol(void)
2776 /* This 64bit constant was handled incorrectly, it was used as addend
2777 (which can hold 64bit just fine) in connection with a symbol,
2778 and TCC generates wrong code for that (displacements are 32bit only).
2779 This effectively is "+ 0x80000000", and if addresses of globals
2780 are below 2GB the result should be a number without high 32 bits set. */
2781 return ((long)(((unsigned long)(&rel1
))) - (0xffffffff80000000UL
));
2785 unsigned long theaddress
= (unsigned long)getmyaddress
;
2786 void relocation_test(void)
2788 void (*fptr
)(void) = (void (*)(void))theaddress
;
2789 printf("*rel1=%d\n", *rel1
);
2790 printf("*rel2=%d\n", *rel2
);
2793 printf("pa_symbol=0x%lx\n", __pa_symbol() >> 63);
2798 void old_style_f(a
,b
,c
)
2802 printf("a=%d b=%d b=%f\n", a
, b
, c
);
2805 void decl_func1(int cmpfn())
2807 printf("cmpfn=%lx\n", (long)cmpfn
);
2810 void decl_func2(cmpfn
)
2813 printf("cmpfn=%lx\n", (long)cmpfn
);
2816 void old_style_function(void)
2818 old_style_f((void *)1, 2, 3.0);
2825 #if defined __i386__ || defined __x86_64__ || defined __arm__
2826 char *p
= alloca(16);
2827 strcpy(p
,"123456789012345");
2828 printf("alloca: p is %s\n", p
);
2829 char *demo
= "This is only a test.\n";
2830 /* Test alloca embedded in a larger expression */
2831 printf("alloca: %s\n", strcpy(alloca(strlen(demo
)+1),demo
) );
2835 void *bounds_checking_is_enabled()
2837 char ca
[10], *cp
= ca
-1;
2838 return (ca
!= cp
+ 1) ? cp
: NULL
;
2841 typedef int constant_negative_array_size_as_compile_time_assertion_idiom
[(1 ? 2 : 0) - 1];
2843 void c99_vla_test(int size1
, int size2
)
2845 #if defined __i386__ || defined __x86_64__
2846 int size
= size1
* size2
;
2847 int tab1
[size
][2], tab2
[10][2];
2848 void *tab1_ptr
, *tab2_ptr
, *bad_ptr
;
2850 /* "size" should have been 'captured' at tab1 declaration,
2851 so modifying it should have no effect on VLA behaviour. */
2854 printf("Test C99 VLA 1 (sizeof): ");
2855 printf("%s\n", (sizeof tab1
== size1
* size2
* 2 * sizeof(int)) ? "PASSED" : "FAILED");
2858 printf("Test C99 VLA 2 (ptrs subtract): ");
2859 printf("%s\n", (tab2
- tab1
== (tab2_ptr
- tab1_ptr
) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2860 printf("Test C99 VLA 3 (ptr add): ");
2861 printf("%s\n", &tab1
[5][1] == (tab1_ptr
+ (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2862 printf("Test C99 VLA 4 (ptr access): ");
2863 tab1
[size1
][1] = 42;
2864 printf("%s\n", (*((int *) (tab1_ptr
+ (size1
* 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2866 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2867 if (bad_ptr
= bounds_checking_is_enabled()) {
2868 int *t1
= &tab1
[size1
* size2
- 1][3];
2869 int *t2
= &tab2
[9][3];
2870 printf("%s ", bad_ptr
== t1
? "PASSED" : "FAILED");
2871 printf("%s ", bad_ptr
== t2
? "PASSED" : "FAILED");
2873 char*c1
= 1 + sizeof(tab1
) + (char*)tab1
;
2874 char*c2
= 1 + sizeof(tab2
) + (char*)tab2
;
2875 printf("%s ", bad_ptr
== c1
? "PASSED" : "FAILED");
2876 printf("%s ", bad_ptr
== c2
? "PASSED" : "FAILED");
2880 printf("%s ", bad_ptr
== i1
? "PASSED" : "FAILED");
2881 printf("%s ", bad_ptr
== i2
? "PASSED" : "FAILED");
2883 int *x1
= tab1
[size1
* size2
+ 1];
2884 int *x2
= tab2
[10 + 1];
2885 printf("%s ", bad_ptr
== x1
? "PASSED" : "FAILED");
2886 printf("%s ", bad_ptr
== x2
? "PASSED" : "FAILED");
2888 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2895 typedef __SIZE_TYPE__
uintptr_t;
2898 void sizeof_test(void)
2903 printf("sizeof(int) = %d\n", sizeof(int));
2904 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2905 printf("sizeof(long) = %d\n", sizeof(long));
2906 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2907 printf("sizeof(short) = %d\n", sizeof(short));
2908 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2909 printf("sizeof(char) = %d\n", sizeof(char));
2910 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2911 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2913 printf("sizeof(a++) = %d\n", sizeof a
++);
2914 printf("a=%d\n", a
);
2916 printf("sizeof(**ptr) = %d\n", sizeof (**ptr
));
2918 /* The type of sizeof should be as large as a pointer, actually
2919 it should be size_t. */
2920 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2923 /* Effectively <<32, but defined also on 32bit machines. */
2927 /* This checks that sizeof really can be used to manipulate
2928 uintptr_t objects, without truncation. */
2929 t2
= t
& -sizeof(uintptr_t);
2930 printf ("%lu %lu\n", t
, t2
);
2932 /* some alignof tests */
2933 printf("__alignof__(int) = %d\n", __alignof__(int));
2934 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2935 printf("__alignof__(short) = %d\n", __alignof__(short));
2936 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2937 printf("__alignof__(char) = %d\n", __alignof__(char));
2938 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2939 printf("__alignof__(func) = %d\n", __alignof__
sizeof_test());
2941 /* sizes of VLAs need to be evaluated even inside sizeof: */
2943 printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a
]));
2944 /* And checking if sizeof compound literal works. Parenthesized: */
2945 printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n",
2946 sizeof( (struct {int i
; int j
;}){4,5} ));
2947 /* And as direct sizeof argument (as unary expression): */
2948 printf("sizeof (struct {short i; short j;}){4,5} = %d\n",
2949 sizeof (struct {short i
; short j
;}){4,5} );
2951 /* sizeof(x && y) should be sizeof(int), even if constant
2952 evaluating is possible. */
2953 printf("sizeof(t && 0) = %d\n", sizeof(t
&& 0));
2954 printf("sizeof(1 && 1) = %d\n", sizeof(1 && 1));
2955 printf("sizeof(t || 1) = %d\n", sizeof(t
|| 1));
2956 printf("sizeof(0 || 0) = %d\n", sizeof(0 || 0));
2959 void typeof_test(void)
2968 printf("a=%f b=%f c=%f\n", a
, b
, c
);
2974 struct hlist_node
*first
, *last
;
2977 void consume_ulong (unsigned long i
)
2982 void statement_expr_test(void)
2986 /* Basic stmt expr test */
2996 printf("a=%d\n", a
);
2998 /* Test that symbols aren't freed prematurely.
2999 With SYM_DEBUG valgrind will show a read from a freed
3000 symbol, and tcc will show an (invalid) warning on the initialization
3001 of 'ptr' below, if symbols are popped after the stmt expr. */
3002 void *v
= (void*)39;
3004 (struct hlist_node
*)v
;
3007 ptr
= (struct hlist_node
*)v
;
3009 /* This part used to segfault when symbols were popped prematurely.
3010 The symbols for the static local would be overwritten with
3011 helper symbols from the pre-processor expansions in between. */
3012 #define some_attr __attribute__((aligned(1)))
3013 #define tps(str) ({ \
3014 static const char *t some_attr = str; \
3017 printf ("stmtexpr: %s %s\n",
3018 tps("somerandomlongstring"),
3019 tps("anotherlongstring"));
3021 /* Test that the three decls of 't' don't interact. */
3023 int b
= ({ int t
= 41; t
; });
3024 int c
= ({ int t
= 42; t
; });
3026 /* Test that aggregate return values work. */
3029 typedef struct hlist_head T
;
3031 T t
= { (void*)43, (void*)44 };
3035 printf ("stmtexpr: %d %d %d\n", t
, b
, c
);
3036 printf ("stmtexpr: %ld %ld\n", (long)h
.first
, (long)h
.last
);
3038 /* Test that we can give out addresses of local labels. */
3039 consume_ulong(({ __label__ __here
; __here
: (unsigned long)&&__here
; }));
3042 void local_label_test(void)
3048 __label__ l1
, l2
, l3
, l4
;
3062 printf("a=%d\n", a
);
3072 /* inline assembler test */
3073 #if defined(__i386__) || defined(__x86_64__)
3075 /* from linux kernel */
3076 static char * strncat1(char * dest
,const char * src
,size_t count
)
3078 long d0
, d1
, d2
, d3
;
3079 __asm__
__volatile__(
3088 "testb %%al,%%al\n\t"
3092 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
3093 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
3098 static char * strncat2(char * dest
,const char * src
,size_t count
)
3100 long d0
, d1
, d2
, d3
;
3101 __asm__
__volatile__(
3102 "repne scasb\n\t" /* one-line repne prefix + string op */
3109 "testb %%al,%%al\n\t"
3113 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
3114 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
3119 static inline void * memcpy1(void * to
, const void * from
, size_t n
)
3122 __asm__
__volatile__(
3127 "1:\ttestb $1,%b4\n\t"
3131 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
3132 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
3137 static inline void * memcpy2(void * to
, const void * from
, size_t n
)
3140 __asm__
__volatile__(
3141 "rep movsl\n\t" /* one-line rep prefix + string op */
3145 "1:\ttestb $1,%b4\n\t"
3149 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
3150 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
3155 static __inline__
void sigaddset1(unsigned int *set
, int _sig
)
3157 __asm__("btsl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
3160 static __inline__
void sigdelset1(unsigned int *set
, int _sig
)
3162 asm("btrl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc", "flags");
3165 static __inline__ __const__
unsigned int swab32(unsigned int x
)
3167 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
3168 "rorl $16,%0\n\t" /* swap words */
3169 "xchgb %b0,%h0" /* swap higher bytes */
3175 static __inline__
unsigned long long mul64(unsigned int a
, unsigned int b
)
3177 unsigned long long res
;
3179 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
3180 but still test the 32bit->64bit mull. */
3181 unsigned int resh
, resl
;
3182 __asm__("mull %2" : "=a" (resl
), "=d" (resh
) : "a" (a
), "r" (b
));
3183 res
= ((unsigned long long)resh
<< 32) | resl
;
3185 __asm__("mull %2" : "=A" (res
) : "a" (a
), "r" (b
));
3190 static __inline__
unsigned long long inc64(unsigned long long a
)
3192 unsigned long long res
;
3194 /* Using the A constraint is wrong, and increments are tested
3198 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res
) : "A" (a
));
3211 unsigned long mconstraint_test(struct struct1231
*r
)
3216 __asm__
volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
3217 : "=&r" (ret
), "=m" (a
)
3218 : "m" (*(struct struct123
*)r
->addr
));
3223 int fls64(unsigned long long x
)
3233 void other_constraints_test(void)
3238 __asm__
volatile ("mov %P1,%0" : "=r" (ret
) : "p" (&var
));
3239 printf ("oc1: %d\n", ret
== (unsigned long)&var
);
3244 /* Test global asm blocks playing with aliases. */
3245 void base_func(void)
3247 printf ("asmc: base\n");
3250 extern void override_func1 (void);
3251 extern void override_func2 (void);
3253 asm(".weak override_func1\n.set override_func1, base_func");
3254 asm(".set override_func1, base_func");
3255 asm(".set override_func2, base_func");
3257 void override_func2 (void)
3259 printf ("asmc: override2\n");
3262 /* This checks a construct used by the linux kernel to encode
3263 references to strings by PC relative references. */
3264 extern int bug_table
[] __attribute__((section("__bug_table")));
3265 char * get_asm_string (void)
3267 extern int some_symbol
;
3268 asm volatile (".globl some_symbol\n"
3271 "some_symbol: .long 0\n"
3272 ".pushsection __bug_table, \"a\"\n"
3273 ".globl bug_table\n"
3275 /* The first entry (1b-2b) is unused in this test,
3276 but we include it to check if cross-section
3277 PC-relative references work. */
3278 "2:\t.long 1b - 2b, %c0 - 2b\n"
3279 ".popsection\n" : : "i" ("A string"));
3280 char * str
= ((char*)bug_table
) + bug_table
[1];
3284 /* This checks another constructs with local labels. */
3285 extern unsigned char alld_stuff
[];
3292 ".pushsection .data.ignore\n"
3293 ".long 661b - .\n" /* This reference to 661 generates an external sym
3294 which shouldn't somehow overwrite the offset that's
3295 already determined for it. */
3297 ".byte 662b - 661b\n" /* So that this value is undeniably 1. */);
3299 void asm_local_label_diff (void)
3301 printf ("asm_local_label_diff: %d %d\n", alld_stuff
[0], alld_stuff
[1]);
3304 /* This checks that static local variables are available from assembler. */
3305 void asm_local_statics (void)
3307 static int localint
= 41;
3308 asm("incl %0" : "+m" (localint
));
3309 printf ("asm_local_statics: %d\n", localint
);
3316 void fancy_copy (unsigned *in
, unsigned *out
)
3318 asm volatile ("" : "=r" (*out
) : "0" (*in
));
3321 void fancy_copy2 (unsigned *in
, unsigned *out
)
3323 asm volatile ("mov %0,(%1)" : : "r" (*in
), "r" (out
) : "memory");
3326 #if defined __x86_64__ && !defined _WIN64
3327 void clobber_r12(void)
3329 asm volatile("mov $1, %%r12" ::: "r12");
3333 void test_high_clobbers(void)
3335 #if defined __x86_64__ && !defined _WIN64
3336 register long val
asm("r12");
3338 /* This tests if asm clobbers correctly save/restore callee saved
3339 registers if they are clobbered and if it's the high 8 x86-64
3340 registers. This is fragile for GCC as the constraints do not
3341 correctly capture the data flow, but good enough for us. */
3342 asm volatile("mov $0x4542, %%r12" : "=r" (val
):: "memory");
3344 asm volatile("mov %%r12, %0" : "=r" (val2
) : "r" (val
): "memory");
3345 printf("asmhc: 0x%x\n", val2
);
3349 static long cpu_number
;
3350 void trace_console(long len
, long len2
)
3353 /* This generated invalid code when the emission of the switch
3354 table isn't disabled. The asms are necessary to show the bug,
3355 normal statements don't work (they need to generate some code
3356 even under nocode_wanted, which normal statements don't do,
3357 but asms do). Also at least these number of cases is necessary
3358 to generate enough "random" bytes. They ultimately are enough
3359 to create invalid instruction patterns to which the first
3360 skip-to-decision-table jump jumps. If decision table emission
3361 is disabled all of this is no problem.
3363 It also is necessary that the switches are in a statement expression
3364 (which has the property of not being enterable from outside. no
3375 case 8: printf("bla"); pfo_ret__
= 42; break;
3377 pscr_ret__
= pfo_ret__
;
3384 case 1:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3385 case 2:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3386 case 4:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3387 case 8:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3388 default: printf("impossible\n");
3390 pscr_ret__
= pfo_ret__
;
3402 void test_asm_dead_code(void)
3405 /* Try to make sure that xdi contains a zero, and hence will
3406 lead to a segfault if the next asm is evaluated without
3407 arguments being set up. */
3408 asm volatile ("" : "=D" (rdi
) : "0" (0));
3411 /* This shouldn't trigger a segfault, either the argument
3412 registers need to be set up and the asm emitted despite
3413 this being in an unevaluated context, or both the argument
3414 setup _and_ the asm emission need to be suppressed. The latter
3415 is better. Disabling asm code gen when suppression is on
3416 also fixes the above trace_console bug, but that came earlier
3417 than asm suppression. */
3418 asm volatile ("movl $0,(%0)" : : "D" (&var
) : "memory");
3423 void test_asm_call(void)
3425 #if defined __x86_64__ && !defined _WIN64
3426 static char str
[] = "PATH";
3428 /* This tests if a reference to an undefined symbol from an asm
3429 block, which isn't otherwise referenced in this file, is correctly
3430 regarded as global symbol, so that it's resolved by other object files
3431 or libraries. We chose getenv here, which isn't used anywhere else
3432 in this file. (If we used e.g. printf, which is used we already
3433 would have a global symbol entry, not triggering the bug which is
3435 /* two pushes so stack remains aligned */
3436 asm volatile ("push %%rdi; push %%rdi; mov %0, %%rdi;"
3437 #if 1 && !defined(__TINYC__) && (defined(__PIC__) || defined(__PIE__))
3442 "pop %%rdi; pop %%rdi"
3443 : "=a" (s
) : "r" (str
));
3444 printf("asmd: %s\n", s
);
3448 #if defined __x86_64__
3449 # define RX "(%rip)"
3454 void asm_dot_test(void)
3461 asm(".text; lea S"RX
",%eax; lea ."RX
",%ecx; sub %ecx,%eax; S=.; jmp p0");
3463 asm(".text; jmp .+6; .int 123; mov .-4"RX
",%eax; jmp p0");
3466 asm(".pushsection \".data\"; Y=.; .int 999; X=Y; .int 456; X=.-4; .popsection");
3468 asm(".data; Y=.; .int 999; X=Y; .int 456; X=.-4; .text");
3470 asm(".text; mov X"RX
",%eax; jmp p0");
3473 asm(".data; X=.; .int 789; Y=.; .int 999; .previous");
3475 asm(".data; X=.; .int 789; Y=.; .int 999; .text");
3477 asm(".text; mov X"RX
",%eax; X=Y; jmp p0");
3479 asm(".text; p0=.; mov %%eax,%0;" : "=m"(r
)); break;
3483 printf("asm_dot_test %d: %d\n", x
, r
);
3490 unsigned int val
, val2
;
3491 struct struct123 s1
;
3492 struct struct1231 s2
= { (unsigned long)&s1
};
3493 /* Hide the outer base_func, but check later that the inline
3494 asm block gets the outer one. */
3496 void override_func3 (void);
3497 unsigned long asmret
;
3501 register int regvar
asm("%esi");
3503 printf("inline asm:\n");
3505 // parse 0x1E-1 as 3 tokens in asm mode
3506 asm volatile ("mov $0x1E-1,%eax");
3508 /* test the no operand case */
3509 asm volatile ("xorl %eax, %eax");
3511 memcpy1(buf
, "hello", 6);
3512 strncat1(buf
, " worldXXXXX", 3);
3513 printf("%s\n", buf
);
3515 memcpy2(buf
, "hello", 6);
3516 strncat2(buf
, " worldXXXXX", 3);
3517 printf("%s\n", buf
);
3519 /* 'A' constraint test */
3520 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
3521 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
3525 printf("mconstraint: %d", mconstraint_test(&s2
));
3526 printf(" %d %d\n", s1
.a
, s1
.b
);
3527 other_constraints_test();
3529 sigdelset1(&set
, 2);
3530 sigaddset1(&set
, 16);
3531 /* NOTE: we test here if C labels are correctly restored after the
3535 __asm__("btsl %1,%0" : "=m"(set
) : "Ir"(20) : "cc");
3536 printf("set=0x%x\n", set
);
3538 printf("swab32(0x%08x) = 0x%0x\n", val
, swab32(val
));
3542 /* The base_func ref from the following inline asm should find
3543 the global one, not the local decl from this function. */
3544 asm volatile(".weak override_func3\n.set override_func3, base_func");
3546 printf("asmstr: %s\n", get_asm_string());
3547 asm_local_label_diff();
3548 asm_local_statics();
3550 /* Check that we can also load structs of appropriate layout
3552 asm volatile("" : "=r" (asmret
) : "0"(s2
));
3553 if (asmret
!= s2
.addr
)
3554 printf("asmstr: failed\n");
3556 /* Check that the typesize correctly sets the register size to
3558 asm volatile("cmp %1,%2; sete %0" : "=a"(somebool
) : "r"(1), "r"(2));
3560 printf("asmbool: failed\n");
3563 fancy_copy (&val
, &val2
);
3564 printf ("fancycpy(%d)=%d\n", val
, val2
);
3566 fancy_copy2 (&val
, &val2
);
3567 printf ("fancycpy2(%d)=%d\n", val
, val2
);
3568 asm volatile ("mov $0x4243, %%esi" : "=r" (regvar
));
3569 printf ("regvar=%x\n", regvar
);
3570 test_high_clobbers();
3571 trace_console(8, 8);
3572 test_asm_dead_code();
3588 #define COMPAT_TYPE(type1, type2) \
3590 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
3591 __builtin_types_compatible_p (type1, type2));\
3596 void builtin_test(void)
3602 COMPAT_TYPE(int, int);
3603 COMPAT_TYPE(int, unsigned int);
3604 COMPAT_TYPE(int, char);
3605 COMPAT_TYPE(int, const int);
3606 COMPAT_TYPE(int, volatile int);
3607 COMPAT_TYPE(int *, int *);
3608 COMPAT_TYPE(int *, void *);
3609 COMPAT_TYPE(int *, const int *);
3610 COMPAT_TYPE(char *, unsigned char *);
3611 COMPAT_TYPE(char *, signed char *);
3612 COMPAT_TYPE(char *, char *);
3613 /* space is needed because tcc preprocessor introduces a space between each token */
3614 COMPAT_TYPE(char * *, void *);
3616 printf("res = %d\n", __builtin_constant_p(1));
3617 printf("res = %d\n", __builtin_constant_p(1 + 2));
3618 printf("res = %d\n", __builtin_constant_p(&constant_p_var
));
3619 printf("res = %d\n", __builtin_constant_p(constant_p_var
));
3620 printf("res = %d\n", __builtin_constant_p(100000 / constant_p_var
));
3623 i
= __builtin_choose_expr (1 != 0, ll
, s
);
3624 printf("bce: %d\n", i
);
3625 i
= __builtin_choose_expr (1 != 1, ll
, s
);
3626 printf("bce: %d\n", i
);
3627 i
= sizeof (__builtin_choose_expr (1, ll
, s
));
3628 printf("bce: %d\n", i
);
3629 i
= sizeof (__builtin_choose_expr (0, ll
, s
));
3630 printf("bce: %d\n", i
);
3632 //printf("bera: %p\n", __builtin_extract_return_addr((void*)43));
3636 extern int __attribute__((weak
)) weak_f1(void);
3637 extern int __attribute__((weak
)) weak_f2(void);
3638 extern int weak_f3(void);
3639 extern int __attribute__((weak
)) weak_v1
;
3640 extern int __attribute__((weak
)) weak_v2
;
3643 extern int (*weak_fpa
)() __attribute__((weak
));
3644 extern int __attribute__((weak
)) (*weak_fpb
)();
3645 extern __attribute__((weak
)) int (*weak_fpc
)();
3647 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak
));
3648 extern int __attribute((weak
)) weak_asm_f2(void) asm("weak_asm_f2x") ;
3649 extern int __attribute((weak
)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak
));
3650 extern int weak_asm_v1
asm("weak_asm_v1x") __attribute((weak
));
3651 extern int __attribute((weak
)) weak_asm_v2
asm("weak_asm_v2x") ;
3652 extern int __attribute((weak
)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak
));
3654 static const size_t dummy
= 0;
3655 extern __typeof(dummy
) weak_dummy1
__attribute__((weak
, alias("dummy")));
3656 extern __typeof(dummy
) __attribute__((weak
, alias("dummy"))) weak_dummy2
;
3657 extern __attribute__((weak
, alias("dummy"))) __typeof(dummy
) weak_dummy3
;
3659 int some_lib_func(void);
3660 int dummy_impl_of_slf(void) { return 444; }
3661 int some_lib_func(void) __attribute__((weak
, alias("dummy_impl_of_slf")));
3663 int weak_toolate() __attribute__((weak
));
3664 int weak_toolate() { return 0; }
3666 void __attribute__((weak
)) weak_test(void)
3668 printf("weak_f1=%d\n", weak_f1
? weak_f1() : 123);
3669 printf("weak_f2=%d\n", weak_f2
? weak_f2() : 123);
3670 printf("weak_f3=%d\n", weak_f3
? weak_f3() : 123);
3671 printf("weak_v1=%d\n",&weak_v1
? weak_v1
: 123);
3672 printf("weak_v2=%d\n",&weak_v2
? weak_v2
: 123);
3673 printf("weak_v3=%d\n",&weak_v3
? weak_v3
: 123);
3675 printf("weak_fpa=%d\n",&weak_fpa
? weak_fpa() : 123);
3676 printf("weak_fpb=%d\n",&weak_fpb
? weak_fpb() : 123);
3677 printf("weak_fpc=%d\n",&weak_fpc
? weak_fpc() : 123);
3679 printf("weak_asm_f1=%d\n", weak_asm_f1
!= NULL
);
3680 printf("weak_asm_f2=%d\n", weak_asm_f2
!= NULL
);
3681 printf("weak_asm_f3=%d\n", weak_asm_f3
!= NULL
);
3682 printf("weak_asm_v1=%d\n",&weak_asm_v1
!= NULL
);
3683 printf("weak_asm_v2=%d\n",&weak_asm_v2
!= NULL
);
3684 printf("weak_asm_v3=%d\n",&weak_asm_v3
!= NULL
);
3685 printf("some_lib_func=%d\n", &some_lib_func
? some_lib_func() : 0);
3688 int __attribute__((weak
)) weak_f2() { return 222; }
3689 int __attribute__((weak
)) weak_f3() { return 333; }
3690 int __attribute__((weak
)) weak_v2
= 222;
3691 int __attribute__((weak
)) weak_v3
= 333;
3694 void const_func(const int a
)
3698 void const_warn_test(void)
3707 int getme (struct condstruct
*s
, int i
)
3709 int i1
= (i
== 0 ? 0 : s
)->i
;
3710 int i2
= (i
== 0 ? s
: 0)->i
;
3711 int i3
= (i
== 0 ? (void*)0 : s
)->i
;
3712 int i4
= (i
== 0 ? s
: (void*)0)->i
;
3713 return i1
+ i2
+ i3
+ i4
;
3722 struct global_data global_data
;
3724 int global_data_getstuff (int *, int);
3726 void global_data_callit (int i
)
3728 *global_data
.b
[i
] = global_data_getstuff (global_data
.b
[i
], 1);
3731 int global_data_getstuff (int *p
, int i
)
3736 void global_data_test (void)
3738 global_data
.a
[0] = 42;
3739 global_data
.b
[0] = &global_data
.a
[0];
3740 global_data_callit (0);
3741 printf ("%d\n", global_data
.a
[0]);
3746 unsigned char fill
: 3;
3747 unsigned char b1
: 1;
3748 unsigned char b2
: 1;
3749 unsigned char fill2
: 3;
3752 int glob1
, glob2
, glob3
;
3754 void compare_comparisons (struct cmpcmpS
*s
)
3756 if (s
->b1
!= (glob1
== glob2
)
3757 || (s
->b2
!= (glob1
== glob3
)))
3758 printf ("comparing comparisons broken\n");
3761 void cmp_comparison_test(void)
3765 glob1
= 42; glob2
= 42;
3768 compare_comparisons (&s
);
3771 int fcompare (double a
, double b
, int code
)
3774 case 0: return a
== b
;
3775 case 1: return a
!= b
;
3776 case 2: return a
< b
;
3777 case 3: return a
>= b
;
3778 case 4: return a
> b
;
3779 case 5: return a
<= b
;
3784 void math_cmp_test(void)
3786 double nan
= 0.0/0.0;
3791 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
3793 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
3794 And it does this in various ways so that all code generation paths
3795 are checked (generating inverted tests, or non-inverted tests, or
3796 producing a 0/1 value without jumps (that's done in the fcompare
3798 #define FCMP(a,b,op,iop,code) \
3799 if (fcompare (a,b,code)) \
3800 bug (a,b,op,iop,1); \
3802 bug (a,b,op,iop,2); \
3806 bug (a,b,op,iop,3); \
3807 if ((a op b) || comp) \
3808 bug (a,b,op,iop,4); \
3809 if ((a iop b) || comp) \
3812 bug (a,b,op,iop,5); \
3813 if (v = !(a op b), !v) bug(a,b,op,iop,7);
3815 /* Equality tests. */
3816 FCMP(nan
, nan
, ==, !=, 0);
3817 FCMP(one
, two
, ==, !=, 0);
3818 FCMP(one
, one
, !=, ==, 1);
3819 /* Non-equality is a bit special. */
3820 if (!fcompare (nan
, nan
, 1))
3821 bug (nan
, nan
, !=, ==, 6);
3823 /* Relational tests on numbers. */
3824 FCMP(two
, one
, <, >=, 2);
3825 FCMP(one
, two
, >=, <, 3);
3826 FCMP(one
, two
, >, <=, 4);
3827 FCMP(two
, one
, <=, >, 5);
3829 /* Relational tests on NaNs. Note that the inverse op here is
3830 always !=, there's no operator in C that is equivalent to !(a < b),
3831 when NaNs are involved, same for the other relational ops. */
3832 FCMP(nan
, nan
, <, !=, 2);
3833 FCMP(nan
, nan
, >=, !=, 3);
3834 FCMP(nan
, nan
, >, !=, 4);
3835 FCMP(nan
, nan
, <=, !=, 5);
3838 double get100 () { return 100.0; }
3840 void callsave_test(void)
3842 #if defined __i386__ || defined __x86_64__ || defined __arm__
3843 int i
, s
; double *d
; double t
;
3844 s
= sizeof (double);
3845 printf ("callsavetest: %d\n", s
);
3846 d
= alloca (sizeof(double));
3848 /* x86-64 had a bug were the next call to get100 would evict
3849 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
3850 in int type, not pointer type. When alloca returns a pointer
3851 with the high 32 bit set (which is likely on x86-64) the access
3852 generates a segfault. */
3853 i
= d
[0] > get100 ();
3859 void bfa3(ptrdiff_t str_offset
)
3861 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset
);
3863 void bfa2(ptrdiff_t str_offset
)
3865 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset
);
3868 void bfa1(ptrdiff_t str_offset
)
3870 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset
);
3874 void builtin_frame_address_test(void)
3876 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
3878 char str
[] = "__builtin_frame_address";
3879 char *fp0
= __builtin_frame_address(0);
3881 printf("str: %s\n", str
);
3886 char via_volatile (char i
)
3893 struct __attribute__((__packed__
)) Spacked
{
3898 struct Spacked spacked
;
3899 typedef struct __attribute__((__packed__
)) {
3905 typedef struct Spacked3_s
{
3909 } __attribute__((__packed__
)) Spacked3
;
3911 struct gate_struct64
{
3912 unsigned short offset_low
;
3913 unsigned short segment
;
3914 unsigned ist
: 3, zero0
: 5, type
: 5, dpl
: 2, p
: 1;
3915 unsigned short offset_middle
;
3916 unsigned offset_high
;
3918 } __attribute__((packed
));
3919 typedef struct gate_struct64 gate_desc
;
3920 gate_desc a_gate_desc
;
3921 void attrib_test(void)
3924 printf("attr: %d %d %d %d\n", sizeof(struct Spacked
),
3925 sizeof(spacked
), sizeof(Spacked2
), sizeof(spacked2
));
3926 printf("attr: %d %d\n", sizeof(Spacked3
), sizeof(spacked3
));
3927 printf("attr: %d %d\n", sizeof(gate_desc
), sizeof(a_gate_desc
));
3930 extern __attribute__((__unused__
)) char * __attribute__((__unused__
)) *
3931 strange_attrib_placement (void);
3933 void * __attribute__((__unused__
)) get_void_ptr (void *a
)
3938 /* This part checks for a bug in TOK_GET (used for inline expansion),
3939 where the large long long constant left the the high bits set for
3940 the integer constant token. */
3942 int __get_order(unsigned long long size
)
3945 size
-= 0xffff880000000000ULL
; // this const left high bits set in the token
3947 struct S
{ int i
: 1; } s
; // constructed for this '1'
3953 /* This just forces the above inline function to be actually emitted. */
3954 int force_get_order(unsigned long s
)
3956 return __get_order(s
);