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 unsigned long ul
= 0x80000000UL
;
1637 p
-= 0x700000000042;
1639 printf("cast_test:\n");
1643 printf("%d %d %d %d\n",
1646 (unsigned char)(a
+ 1),
1647 (unsigned short)(a
+ 1));
1648 printf("%d %d %d %d\n",
1651 (unsigned char)0xfffff,
1652 (unsigned short)0xfffff);
1654 a
= (bcast
= 128) + 1;
1656 a
= (scast
= 65536) + 1;
1659 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
1661 /* test cast from unsigned to signed short to int */
1664 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b
, d
);
1667 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b
, d
);
1669 /* test implicit int casting for array accesses */
1673 printf("%d %d\n", tab
[0], tab
[1]);
1675 /* test implicit casting on some operators */
1676 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1677 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1678 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1680 /* from pointer to integer types */
1681 printf("%d %d %ld %ld %lld %lld\n",
1682 (int)p
, (unsigned int)p
,
1683 (long)p
, (unsigned long)p
,
1684 (long long)p
, (unsigned long long)p
);
1686 /* from integers to pointers */
1687 printf("%p %p %p %p\n",
1688 (void *)a
, (void *)b
, (void *)c
, (void *)d
);
1690 /* int to int with sign set */
1691 printf("0x%lx\n", (unsigned long)(int)ul
);
1694 /* initializers tests */
1695 struct structinit1
{
1704 int sinit3
[3] = { 1, 2, {{3}}, };
1705 int sinit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1706 int sinit5
[3][2] = { 1, 2, 3, 4, 5, 6 };
1707 int sinit6
[] = { 1, 2, 3 };
1708 int sinit7
[] = { [2] = 3, [0] = 1, 2 };
1709 char sinit8
[] = "hello" "trala";
1711 struct structinit1 sinit9
= { 1, 2, 3 };
1712 struct structinit1 sinit10
= { .f2
= 2, 3, .f1
= 1 };
1713 struct structinit1 sinit11
= { .f2
= 2, 3, .f1
= 1,
1721 char *sinit12
= "hello world";
1727 char sinit14
[10] = { "abc" };
1728 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
1730 struct { int a
[3], b
; } sinit16
[] = { { 1 }, 2 };
1746 struct complexinit0
{
1751 struct complexinit
{
1753 const struct complexinit0
*b
;
1756 const static struct complexinit cix
[] = {
1759 .b
= (const struct complexinit0
[]) {
1767 struct complexinit2
{
1772 struct complexinit2 cix20
;
1774 struct complexinit2 cix21
= {
1776 .b
= { 3001, 3002, 3003 }
1779 struct complexinit2 cix22
= {
1781 .b
= { 4001, 4002, 4003, 4004, 4005, 4006 }
1784 typedef int arrtype1
[];
1785 arrtype1 sinit19
= {1};
1786 arrtype1 sinit20
= {2,3};
1787 typedef int arrtype2
[3];
1788 arrtype2 sinit21
= {4};
1789 arrtype2 sinit22
= {5,6,7};
1791 /* Address comparisons of non-weak symbols with zero can be const-folded */
1792 int sinit23
[2] = { "astring" ? sizeof("astring") : -1,
1793 &sinit23
? 42 : -1 };
1795 extern int external_inited
= 42;
1797 void init_test(void)
1801 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1802 int linit6
[] = { 1, 2, 3 };
1804 char linit8
[] = "hello" "trala";
1805 int linit12
[10] = { 1, 2 };
1806 int linit13
[10] = { 1, 2, [7] = 3, [3] = 4, };
1807 char linit14
[10] = "abc";
1808 int linit15
[10] = { linit1
, linit1
+ 1, [6] = linit1
+ 2, };
1809 struct linit16
{ int a1
, a2
, a3
, a4
; } linit16
= { 1, .a3
= 2 };
1810 int linit17
= sizeof(linit17
);
1812 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1813 int linit18
[2] = {&zero
? 1 : -1, zero
? -1 : 1 };
1815 printf("init_test:\n");
1817 printf("sinit1=%d\n", sinit1
);
1818 printf("sinit2=%d\n", sinit2
);
1819 printf("sinit3=%d %d %d %d\n",
1825 printf("sinit6=%d\n", sizeof(sinit6
));
1826 printf("sinit7=%d %d %d %d\n",
1832 printf("sinit8=%s\n", sinit8
);
1833 printf("sinit9=%d %d %d\n",
1838 printf("sinit10=%d %d %d\n",
1843 printf("sinit11=%d %d %d %d %d %d\n",
1854 printf("[%d][%d] = %d %d %d\n",
1855 i
, j
, sinit4
[i
][j
], sinit5
[i
][j
], linit4
[i
][j
]);
1856 printf("linit1=%d\n", linit1
);
1857 printf("linit2=%d\n", linit2
);
1858 printf("linit6=%d\n", sizeof(linit6
));
1859 printf("linit8=%d %s\n", sizeof(linit8
), linit8
);
1861 printf("sinit12=%s\n", sinit12
);
1862 printf("sinit13=%d %s %s %s\n",
1867 printf("sinit14=%s\n", sinit14
);
1869 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
1871 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
1873 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
1875 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
1877 printf("%d %d %d %d\n",
1882 /* test that initialisation is done after variable declare */
1883 printf("linit17=%d\n", linit17
);
1884 printf("sinit15=%d\n", sinit15
[0]);
1885 printf("sinit16=%d %d\n", sinit16
[0].a
[0], sinit16
[1].a
[0]);
1886 printf("sinit17=%s %d %s %d\n",
1887 sinit17
[0].s
, sinit17
[0].len
,
1888 sinit17
[1].s
, sinit17
[1].len
);
1890 printf("%x ", sinit18
[i
]);
1892 /* complex init check */
1893 printf("cix: %d %d %d %d %d %d %d\n",
1895 cix
[0].b
[0].a
, cix
[0].b
[0].b
,
1896 cix
[0].b
[1].a
, cix
[0].b
[1].b
,
1897 cix
[0].b
[2].a
, cix
[0].b
[2].b
);
1898 printf("cix2: %d %d\n", cix21
.b
[2], cix22
.b
[5]);
1899 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20
, sizeof cix21
, sizeof cix22
);
1901 printf("arrtype1: %d %d %d\n", sinit19
[0], sinit20
[0], sinit20
[1]);
1902 printf("arrtype2: %d %d\n", sizeof(sinit19
), sizeof(sinit20
));
1903 printf("arrtype3: %d %d %d\n", sinit21
[0], sinit21
[1], sinit21
[2]);
1904 printf("arrtype4: %d %d %d\n", sinit22
[0], sinit22
[1], sinit22
[2]);
1905 printf("arrtype5: %d %d\n", sizeof(sinit21
), sizeof(sinit22
));
1906 printf("arrtype6: %d\n", sizeof(arrtype2
));
1908 printf("sinit23= %d %d\n", sinit23
[0], sinit23
[1]);
1909 printf("linit18= %d %d\n", linit18
[0], linit18
[1]);
1912 void switch_uc(unsigned char uc
)
1925 printf("ucsw: broken!\n");
1929 void switch_sc(signed char sc
)
1942 printf("scsw: broken!\n");
1949 unsigned long long ull
;
1974 for (i
= 1; i
<= 5; i
++) {
1975 ull
= (unsigned long long)i
<< 61;
1978 printf("ullsw:1\n");
1981 printf("ullsw:2\n");
1984 printf("ullsw:3\n");
1987 printf("ullsw:4\n");
1990 printf("ullsw:5\n");
1993 printf("ullsw: broken!\n");
1997 for (i
= 1; i
<= 5; i
++) {
1998 ll
= (long long)i
<< 61;
2016 printf("llsw: broken!\n");
2020 for (i
= -5; i
<= 5; i
++) {
2021 switch_uc((unsigned char)i
);
2024 for (i
= -5; i
<= 5; i
++) {
2025 switch_sc ((signed char)i
);
2029 /* ISOC99 _Bool type */
2030 void c99_bool_test(void)
2036 printf("bool_test:\n");
2037 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
2039 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
2041 printf("b = %d\n", b
);
2043 printf("b = %d\n", b
);
2047 void bitfield_test(void)
2059 unsigned int f5
: 7;
2061 printf("bitfield_test:");
2062 printf("sizeof(st1) = %d\n", sizeof(st1
));
2071 printf("%d %d %d %d %d\n",
2072 st1
.f1
, st1
.f2
, st1
.f3
, st1
.f4
, st1
.f5
);
2075 printf("%d %d\n", sa
, ca
);
2079 printf("st1.f1 == -1\n");
2081 printf("st1.f1 != -1\n");
2083 printf("st1.f2 == -1\n");
2085 printf("st1.f2 != -1\n");
2091 unsigned long long f3
: 38;
2093 st2
.f1
= 0x123456789ULL
;
2095 st2
.f2
= (long long)a
<< 25;
2098 printf("%lld %lld %lld\n", st2
.f1
, st2
.f2
, st2
.f3
);
2101 Disabled
for now until further clarification re GCC compatibility
2110 printf("sizeof(st3) = %d\n", sizeof(st3
));
2118 printf("st4.y == %d\n", st4
.y
);
2122 int x
: 12, y
: 4, : 0, : 4, z
: 3;
2124 } st5
= { 1, 2, 3, 4, -3, 6 };
2125 printf("st5 = %d %d %d %d %d %d\n", st5
.a
, st5
.b
, st5
.x
, st5
.y
, st5
.z
, st5
.c
);
2128 unsigned char y
: 2;
2131 printf("st6.y == %d\n", st6
.y
);
2135 #define FLOAT_FMT "%f\n"
2137 /* x86's float isn't compatible with GCC */
2138 #define FLOAT_FMT "%.5f\n"
2141 /* declare strto* functions as they are C99 */
2142 double strtod(const char *nptr
, char **endptr
);
2145 float strtof(const char *nptr
, char **endptr
) {return (float)strtod(nptr
, endptr
);}
2146 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
) {return (LONG_DOUBLE
)strtod(nptr
, endptr
);}
2148 float strtof(const char *nptr
, char **endptr
);
2149 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
);
2152 #define FTEST(prefix, typename, type, fmt)\
2153 void prefix ## cmp(type a, type b)\
2155 printf("%d %d %d %d %d %d\n",\
2162 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
2170 printf(fmt "\n", ++a);\
2171 printf(fmt "\n", a++);\
2172 printf(fmt "\n", a);\
2174 printf("%d %d\n", !a, !b);\
2176 void prefix ## fcast(type a)\
2184 unsigned long long llua;\
2189 printf("ftof: %f %f %Lf\n", fa, da, la);\
2191 llia = (long long)a;\
2192 a = (a >= 0) ? a : -a;\
2193 ua = (unsigned int)a;\
2194 llua = (unsigned long long)a;\
2195 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
2198 llia = -0x123456789012345LL;\
2199 llua = 0xf123456789012345LLU;\
2201 printf("itof: " fmt "\n", b);\
2203 printf("utof: " fmt "\n", b);\
2205 printf("lltof: " fmt "\n", b);\
2207 printf("ulltof: " fmt "\n", b);\
2210 float prefix ## retf(type a) { return a; }\
2211 double prefix ## retd(type a) { return a; }\
2212 LONG_DOUBLE prefix ## retld(type a) { return a; }\
2214 void prefix ## call(void)\
2216 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
2217 printf("double: %f\n", prefix ## retd(42.123456789));\
2218 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
2219 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
2222 void prefix ## signed_zeros(void) \
2224 type x = 0.0, y = -0.0, n, p;\
2226 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
2227 1.0 / x != 1.0 / y);\
2229 printf ("x != y; this is wrong!\n");\
2233 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
2234 1.0 / x != 1.0 / n);\
2236 printf ("x != -x; this is wrong!\n");\
2240 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
2241 1.0 / x != 1.0 / p);\
2243 printf ("x != +y; this is wrong!\n");\
2246 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
2247 1.0 / x != 1.0 / p);\
2249 printf ("x != -y; this is wrong!\n");\
2251 void prefix ## test(void)\
2253 printf("testing '%s'\n", #typename);\
2254 prefix ## cmp(1, 2.5);\
2255 prefix ## cmp(2, 1.5);\
2256 prefix ## cmp(1, 1);\
2257 prefix ## fcast(234.6);\
2258 prefix ## fcast(-2334.6);\
2260 prefix ## signed_zeros();\
2263 FTEST(f
, float, float, "%f")
2264 FTEST(d
, double, double, "%f")
2265 FTEST(ld
, long double, LONG_DOUBLE
, "%Lf")
2267 double ftab1
[3] = { 1.2, 3.4, -5.6 };
2270 void float_test(void)
2272 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
2277 static double nan2
= 0.0/0.0;
2278 static double inf1
= 1.0/0.0;
2279 static double inf2
= 1e5000
;
2281 printf("float_test:\n");
2282 printf("sizeof(float) = %d\n", sizeof(float));
2283 printf("sizeof(double) = %d\n", sizeof(double));
2284 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE
));
2288 printf("%f %f %f\n", ftab1
[0], ftab1
[1], ftab1
[2]);
2289 printf("%f %f %f\n", 2.12, .5, 2.3e10
);
2290 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
2292 printf("da=%f\n", da
);
2294 printf("fa=%f\n", fa
);
2297 printf("da = %f\n", da
);
2300 printf("db = %f\n", db
);
2301 printf("nan != nan = %d, inf1 = %f, inf2 = %f\n", nan2
!= nan2
, inf1
, inf2
);
2310 return fib(n
-1) + fib(n
-2);
2313 void __attribute__((aligned(16))) aligned_function(int i
) {}
2325 printf("funcptr:\n");
2332 /* more complicated pointer computation */
2335 printf("sizeof1 = %d\n", sizeof(funcptr_test
));
2336 printf("sizeof2 = %d\n", sizeof funcptr_test
);
2337 printf("sizeof3 = %d\n", sizeof(&funcptr_test
));
2338 printf("sizeof4 = %d\n", sizeof &funcptr_test
);
2346 /* Check that we can align functions */
2347 func
= aligned_function
;
2348 printf("aligned_function (should be zero): %d\n", ((int)func
) & 15);
2351 void lloptest(long long a
, long long b
)
2353 unsigned long long ua
, ub
;
2358 printf("arith: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2364 printf("arith1: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2370 printf("bin: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2376 printf("test: %d %d %d %d %d %d\n",
2384 printf("utest: %d %d %d %d %d %d\n",
2395 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2396 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
++, b
++);
2397 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", --a
, --b
);
2398 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2400 printf("not: %d %d %d %d\n", !a
, !ua
, !b
, !ub
);
2403 void llshift(long long a
, int b
)
2405 printf("shift: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2406 (unsigned long long)a
>> b
,
2409 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2410 (unsigned long long)a
>> 3,
2413 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2414 (unsigned long long)a
>> 35,
2424 long long la
, lb
, lc
;
2425 unsigned long long ula
, ulb
, ulc
;
2428 la
= (la
<< 20) | 0x12345;
2430 printf("la=" LONG_LONG_FORMAT
" ula=" ULONG_LONG_FORMAT
"\n", la
, ula
);
2435 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
2440 printf("ftoll: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", la
, lb
, lc
);
2445 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
2450 printf("ftoull: " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
"\n", ula
, ulb
, ulc
);
2453 long long llfunc1(int a
)
2463 long long int value(struct S
*v
)
2465 return ((long long int)v
->item
);
2468 long long llfunc2(long long x
, long long y
, int z
)
2473 void check_opl_save_regs(char *a
, long long b
, int c
)
2478 void longlong_test(void)
2483 printf("longlong_test:\n");
2484 printf("sizeof(long long) = %d\n", sizeof(long long));
2489 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2490 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %Lx\n",
2494 0x1234567812345679);
2496 printf(LONG_LONG_FORMAT
"\n", a
);
2499 lloptest(0xff, 0x1234);
2500 b
= 0x72345678 << 10;
2504 b
= 0x72345678LL
<< 10;
2515 /* long long reg spill test */
2520 printf("%lld\n", value(&a
));
2522 lloptest(0x80000000, 0);
2525 long long *p
, v
, **pp
;
2529 printf("another long long spill test : %lld\n", *p
);
2532 v
= llfunc2(**pp
, **pp
, ia
);
2533 printf("a long long function (arm-)reg-args test : %lld\n", v
);
2537 printf("%d %d %d %d\n", a
> b
, a
< b
, a
>= b
, a
<= b
);
2539 printf(LONG_LONG_FORMAT
"\n", 0x123456789LLU
);
2541 /* long long pointer deref in argument passing test */
2546 /* shortening followed by widening */
2547 unsigned long long u
= 0x8000000000000001ULL
;
2548 u
= (unsigned)(u
+ 1);
2549 printf("long long u=" ULONG_LONG_FORMAT
"\n", u
);
2551 /* was a problem with missing save_regs in gen_opl on 32-bit platforms */
2553 check_opl_save_regs(&cc
, -1, 0);
2554 printf("check_opl_save_regs: %d\n", cc
);
2557 void manyarg_test(void)
2559 LONG_DOUBLE ld
= 1234567891234LL;
2560 printf("manyarg_test:\n");
2561 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %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 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2565 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2566 1, 2, 3, 4, 5, 6, 7, 8,
2567 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2568 1234567891234LL, 987654321986LL,
2570 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2571 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2572 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2573 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2574 1234567891234LL, 987654321986LL,
2576 printf("%d %d %d %d %d %d %d %d %Lf\n",
2577 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2578 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2579 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"%f %f %Lf\n",
2580 1, 2, 3, 4, 5, 6, 7, 8,
2581 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2582 1234567891234LL, 987654321986LL,
2584 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2585 "%Lf " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f %Lf\n",
2586 1, 2, 3, 4, 5, 6, 7, 8,
2587 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2588 ld
, 1234567891234LL, 987654321986LL,
2592 void vprintf1(const char *fmt
, ...)
2616 i
= va_arg(ap
, int);
2620 d
= va_arg(ap
, double);
2624 ll
= va_arg(ap
, long long);
2625 printf(LONG_LONG_FORMAT
, ll
);
2628 ld
= va_arg(ap
, LONG_DOUBLE
);
2646 void stdarg_for_struct(struct myspace bob
, ...)
2648 struct myspace george
, bill
;
2653 bill
= va_arg(ap
, struct myspace
);
2654 george
= va_arg(ap
, struct myspace
);
2655 validate
= va_arg(ap
, int);
2656 printf("stdarg_for_struct: %d %d %d %d\n",
2657 bob
.profile
, bill
.profile
, george
.profile
, validate
);
2661 void stdarg_for_libc(const char *fmt
, ...)
2664 va_start(args
, fmt
);
2669 void stdarg_syntax(int n
, ...)
2677 i
= va_arg(ap
, int);
2678 printf("stdarg_void_expr: %d\n", i
);
2682 void stdarg_test(void)
2684 LONG_DOUBLE ld
= 1234567891234LL;
2687 vprintf1("%d %d %d\n", 1, 2, 3);
2688 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2689 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2690 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2691 vprintf1("%d %f %l %F %d %f %l %F\n",
2692 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2693 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2694 1, 2, 3, 4, 5, 6, 7, 8,
2695 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2696 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
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 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2701 1, 2, 3, 4, 5, 6, 7, 8,
2702 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2703 1234567891234LL, 987654321986LL,
2705 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2707 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2708 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2709 1234567891234LL, 987654321986LL,
2711 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2712 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2713 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2715 1, 2, 3, 4, 5, 6, 7, 8,
2716 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2717 1234567891234LL, 987654321986LL,
2719 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2720 "%F %l %l %f %f %F\n",
2721 1, 2, 3, 4, 5, 6, 7, 8,
2722 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2723 ld
, 1234567891234LL, 987654321986LL,
2727 stdarg_for_struct(bob
, bob
, bob
, bob
.profile
);
2728 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2729 stdarg_syntax(1, 17);
2732 void whitespace_test(void)
2738 ntf("whitspace:\n");\f\v
2742 #ifdef CORRECT_CR_HANDLING
2751 #ifdef ACCEPT_CR_IN_STRINGS
2752 printf("len1=%d\n", strlen("
2754 #ifdef CORRECT_CR_HANDLING
2757 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
2759 printf("len1=%d\n", strlen("
a
2761 #endif /* ACCEPT_CR_IN_STRINGS */
2764 int reltab
[3] = { 1, 2, 3 };
2766 int *rel1
= &reltab
[1];
2767 int *rel2
= &reltab
[2];
2770 void relocation_test(void) {}
2772 void getmyaddress(void)
2774 printf("in getmyaddress\n");
2778 long __pa_symbol(void)
2780 /* This 64bit constant was handled incorrectly, it was used as addend
2781 (which can hold 64bit just fine) in connection with a symbol,
2782 and TCC generates wrong code for that (displacements are 32bit only).
2783 This effectively is "+ 0x80000000", and if addresses of globals
2784 are below 2GB the result should be a number without high 32 bits set. */
2785 return ((long)(((unsigned long)(&rel1
))) - (0xffffffff80000000UL
));
2789 unsigned long theaddress
= (unsigned long)getmyaddress
;
2790 void relocation_test(void)
2792 void (*fptr
)(void) = (void (*)(void))theaddress
;
2793 printf("*rel1=%d\n", *rel1
);
2794 printf("*rel2=%d\n", *rel2
);
2797 printf("pa_symbol=0x%lx\n", __pa_symbol() >> 63);
2802 void old_style_f(a
,b
,c
)
2806 printf("a=%d b=%d b=%f\n", a
, b
, c
);
2809 void decl_func1(int cmpfn())
2811 printf("cmpfn=%lx\n", (long)cmpfn
);
2814 void decl_func2(cmpfn
)
2817 printf("cmpfn=%lx\n", (long)cmpfn
);
2820 void old_style_function(void)
2822 old_style_f((void *)1, 2, 3.0);
2829 #if defined __i386__ || defined __x86_64__ || defined __arm__
2830 char *p
= alloca(16);
2831 strcpy(p
,"123456789012345");
2832 printf("alloca: p is %s\n", p
);
2833 char *demo
= "This is only a test.\n";
2834 /* Test alloca embedded in a larger expression */
2835 printf("alloca: %s\n", strcpy(alloca(strlen(demo
)+1),demo
) );
2839 void *bounds_checking_is_enabled()
2841 char ca
[10], *cp
= ca
-1;
2842 return (ca
!= cp
+ 1) ? cp
: NULL
;
2845 typedef int constant_negative_array_size_as_compile_time_assertion_idiom
[(1 ? 2 : 0) - 1];
2847 void c99_vla_test(int size1
, int size2
)
2849 #if defined __i386__ || defined __x86_64__
2850 int size
= size1
* size2
;
2851 int tab1
[size
][2], tab2
[10][2];
2852 void *tab1_ptr
, *tab2_ptr
, *bad_ptr
;
2854 /* "size" should have been 'captured' at tab1 declaration,
2855 so modifying it should have no effect on VLA behaviour. */
2858 printf("Test C99 VLA 1 (sizeof): ");
2859 printf("%s\n", (sizeof tab1
== size1
* size2
* 2 * sizeof(int)) ? "PASSED" : "FAILED");
2862 printf("Test C99 VLA 2 (ptrs subtract): ");
2863 printf("%s\n", (tab2
- tab1
== (tab2_ptr
- tab1_ptr
) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2864 printf("Test C99 VLA 3 (ptr add): ");
2865 printf("%s\n", &tab1
[5][1] == (tab1_ptr
+ (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2866 printf("Test C99 VLA 4 (ptr access): ");
2867 tab1
[size1
][1] = 42;
2868 printf("%s\n", (*((int *) (tab1_ptr
+ (size1
* 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2870 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2871 if (bad_ptr
= bounds_checking_is_enabled()) {
2872 int *t1
= &tab1
[size1
* size2
- 1][3];
2873 int *t2
= &tab2
[9][3];
2874 printf("%s ", bad_ptr
== t1
? "PASSED" : "FAILED");
2875 printf("%s ", bad_ptr
== t2
? "PASSED" : "FAILED");
2877 char*c1
= 1 + sizeof(tab1
) + (char*)tab1
;
2878 char*c2
= 1 + sizeof(tab2
) + (char*)tab2
;
2879 printf("%s ", bad_ptr
== c1
? "PASSED" : "FAILED");
2880 printf("%s ", bad_ptr
== c2
? "PASSED" : "FAILED");
2884 printf("%s ", bad_ptr
== i1
? "PASSED" : "FAILED");
2885 printf("%s ", bad_ptr
== i2
? "PASSED" : "FAILED");
2887 int *x1
= tab1
[size1
* size2
+ 1];
2888 int *x2
= tab2
[10 + 1];
2889 printf("%s ", bad_ptr
== x1
? "PASSED" : "FAILED");
2890 printf("%s ", bad_ptr
== x2
? "PASSED" : "FAILED");
2892 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2899 typedef __SIZE_TYPE__
uintptr_t;
2902 void sizeof_test(void)
2907 printf("sizeof(int) = %d\n", sizeof(int));
2908 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2909 printf("sizeof(long) = %d\n", sizeof(long));
2910 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2911 printf("sizeof(short) = %d\n", sizeof(short));
2912 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2913 printf("sizeof(char) = %d\n", sizeof(char));
2914 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2915 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2917 printf("sizeof(a++) = %d\n", sizeof a
++);
2918 printf("a=%d\n", a
);
2920 printf("sizeof(**ptr) = %d\n", sizeof (**ptr
));
2922 /* The type of sizeof should be as large as a pointer, actually
2923 it should be size_t. */
2924 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2927 /* Effectively <<32, but defined also on 32bit machines. */
2931 /* This checks that sizeof really can be used to manipulate
2932 uintptr_t objects, without truncation. */
2933 t2
= t
& -sizeof(uintptr_t);
2934 printf ("%lu %lu\n", t
, t2
);
2936 /* some alignof tests */
2937 printf("__alignof__(int) = %d\n", __alignof__(int));
2938 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2939 printf("__alignof__(short) = %d\n", __alignof__(short));
2940 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2941 printf("__alignof__(char) = %d\n", __alignof__(char));
2942 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2943 printf("__alignof__(func) = %d\n", __alignof__
sizeof_test());
2945 /* sizes of VLAs need to be evaluated even inside sizeof: */
2947 printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a
]));
2948 /* And checking if sizeof compound literal works. Parenthesized: */
2949 printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n",
2950 sizeof( (struct {int i
; int j
;}){4,5} ));
2951 /* And as direct sizeof argument (as unary expression): */
2952 printf("sizeof (struct {short i; short j;}){4,5} = %d\n",
2953 sizeof (struct {short i
; short j
;}){4,5} );
2955 /* sizeof(x && y) should be sizeof(int), even if constant
2956 evaluating is possible. */
2957 printf("sizeof(t && 0) = %d\n", sizeof(t
&& 0));
2958 printf("sizeof(1 && 1) = %d\n", sizeof(1 && 1));
2959 printf("sizeof(t || 1) = %d\n", sizeof(t
|| 1));
2960 printf("sizeof(0 || 0) = %d\n", sizeof(0 || 0));
2963 void typeof_test(void)
2972 printf("a=%f b=%f c=%f\n", a
, b
, c
);
2978 struct hlist_node
*first
, *last
;
2981 void consume_ulong (unsigned long i
)
2986 void statement_expr_test(void)
2990 /* Basic stmt expr test */
3000 printf("a=%d\n", a
);
3002 /* Test that symbols aren't freed prematurely.
3003 With SYM_DEBUG valgrind will show a read from a freed
3004 symbol, and tcc will show an (invalid) warning on the initialization
3005 of 'ptr' below, if symbols are popped after the stmt expr. */
3006 void *v
= (void*)39;
3008 (struct hlist_node
*)v
;
3011 ptr
= (struct hlist_node
*)v
;
3013 /* This part used to segfault when symbols were popped prematurely.
3014 The symbols for the static local would be overwritten with
3015 helper symbols from the pre-processor expansions in between. */
3016 #define some_attr __attribute__((aligned(1)))
3017 #define tps(str) ({ \
3018 static const char *t some_attr = str; \
3021 printf ("stmtexpr: %s %s\n",
3022 tps("somerandomlongstring"),
3023 tps("anotherlongstring"));
3025 /* Test that the three decls of 't' don't interact. */
3027 int b
= ({ int t
= 41; t
; });
3028 int c
= ({ int t
= 42; t
; });
3030 /* Test that aggregate return values work. */
3033 typedef struct hlist_head T
;
3035 T t
= { (void*)43, (void*)44 };
3039 printf ("stmtexpr: %d %d %d\n", t
, b
, c
);
3040 printf ("stmtexpr: %ld %ld\n", (long)h
.first
, (long)h
.last
);
3042 /* Test that we can give out addresses of local labels. */
3043 consume_ulong(({ __label__ __here
; __here
: (unsigned long)&&__here
; }));
3046 void local_label_test(void)
3052 __label__ l1
, l2
, l3
, l4
;
3066 printf("a=%d\n", a
);
3076 /* inline assembler test */
3077 #if defined(__i386__) || defined(__x86_64__)
3079 /* from linux kernel */
3080 static char * strncat1(char * dest
,const char * src
,size_t count
)
3082 long d0
, d1
, d2
, d3
;
3083 __asm__
__volatile__(
3092 "testb %%al,%%al\n\t"
3096 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
3097 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
3102 static char * strncat2(char * dest
,const char * src
,size_t count
)
3104 long d0
, d1
, d2
, d3
;
3105 __asm__
__volatile__(
3106 "repne scasb\n\t" /* one-line repne prefix + string op */
3113 "testb %%al,%%al\n\t"
3117 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
3118 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
3123 static inline void * memcpy1(void * to
, const void * from
, size_t n
)
3126 __asm__
__volatile__(
3131 "1:\ttestb $1,%b4\n\t"
3135 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
3136 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
3141 static inline void * memcpy2(void * to
, const void * from
, size_t n
)
3144 __asm__
__volatile__(
3145 "rep movsl\n\t" /* one-line rep prefix + string op */
3149 "1:\ttestb $1,%b4\n\t"
3153 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
3154 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
3159 static __inline__
void sigaddset1(unsigned int *set
, int _sig
)
3161 __asm__("btsl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
3164 static __inline__
void sigdelset1(unsigned int *set
, int _sig
)
3166 asm("btrl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc", "flags");
3169 static __inline__ __const__
unsigned int swab32(unsigned int x
)
3171 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
3172 "rorl $16,%0\n\t" /* swap words */
3173 "xchgb %b0,%h0" /* swap higher bytes */
3179 static __inline__
unsigned long long mul64(unsigned int a
, unsigned int b
)
3181 unsigned long long res
;
3183 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
3184 but still test the 32bit->64bit mull. */
3185 unsigned int resh
, resl
;
3186 __asm__("mull %2" : "=a" (resl
), "=d" (resh
) : "a" (a
), "r" (b
));
3187 res
= ((unsigned long long)resh
<< 32) | resl
;
3189 __asm__("mull %2" : "=A" (res
) : "a" (a
), "r" (b
));
3194 static __inline__
unsigned long long inc64(unsigned long long a
)
3196 unsigned long long res
;
3198 /* Using the A constraint is wrong, and increments are tested
3202 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res
) : "A" (a
));
3215 unsigned long mconstraint_test(struct struct1231
*r
)
3220 __asm__
volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
3221 : "=&r" (ret
), "=m" (a
)
3222 : "m" (*(struct struct123
*)r
->addr
));
3227 int fls64(unsigned long long x
)
3237 void other_constraints_test(void)
3242 __asm__
volatile ("mov %P1,%0" : "=r" (ret
) : "p" (&var
));
3243 printf ("oc1: %d\n", ret
== (unsigned long)&var
);
3248 /* Test global asm blocks playing with aliases. */
3249 void base_func(void)
3251 printf ("asmc: base\n");
3254 extern void override_func1 (void);
3255 extern void override_func2 (void);
3257 asm(".weak override_func1\n.set override_func1, base_func");
3258 asm(".set override_func1, base_func");
3259 asm(".set override_func2, base_func");
3261 void override_func2 (void)
3263 printf ("asmc: override2\n");
3266 /* This checks a construct used by the linux kernel to encode
3267 references to strings by PC relative references. */
3268 extern int bug_table
[] __attribute__((section("__bug_table")));
3269 char * get_asm_string (void)
3271 extern int some_symbol
;
3272 asm volatile (".globl some_symbol\n"
3275 "some_symbol: .long 0\n"
3276 ".pushsection __bug_table, \"a\"\n"
3277 ".globl bug_table\n"
3279 /* The first entry (1b-2b) is unused in this test,
3280 but we include it to check if cross-section
3281 PC-relative references work. */
3282 "2:\t.long 1b - 2b, %c0 - 2b\n"
3283 ".popsection\n" : : "i" ("A string"));
3284 char * str
= ((char*)bug_table
) + bug_table
[1];
3288 /* This checks another constructs with local labels. */
3289 extern unsigned char alld_stuff
[];
3296 ".pushsection .data.ignore\n"
3297 ".long 661b - .\n" /* This reference to 661 generates an external sym
3298 which shouldn't somehow overwrite the offset that's
3299 already determined for it. */
3301 ".byte 662b - 661b\n" /* So that this value is undeniably 1. */);
3303 void asm_local_label_diff (void)
3305 printf ("asm_local_label_diff: %d %d\n", alld_stuff
[0], alld_stuff
[1]);
3308 /* This checks that static local variables are available from assembler. */
3309 void asm_local_statics (void)
3311 static int localint
= 41;
3312 asm("incl %0" : "+m" (localint
));
3313 printf ("asm_local_statics: %d\n", localint
);
3320 void fancy_copy (unsigned *in
, unsigned *out
)
3322 asm volatile ("" : "=r" (*out
) : "0" (*in
));
3325 void fancy_copy2 (unsigned *in
, unsigned *out
)
3327 asm volatile ("mov %0,(%1)" : : "r" (*in
), "r" (out
) : "memory");
3330 #if defined __x86_64__ && !defined _WIN64
3331 void clobber_r12(void)
3333 asm volatile("mov $1, %%r12" ::: "r12");
3337 void test_high_clobbers(void)
3339 #if defined __x86_64__ && !defined _WIN64
3340 register long val
asm("r12");
3342 /* This tests if asm clobbers correctly save/restore callee saved
3343 registers if they are clobbered and if it's the high 8 x86-64
3344 registers. This is fragile for GCC as the constraints do not
3345 correctly capture the data flow, but good enough for us. */
3346 asm volatile("mov $0x4542, %%r12" : "=r" (val
):: "memory");
3348 asm volatile("mov %%r12, %0" : "=r" (val2
) : "r" (val
): "memory");
3349 printf("asmhc: 0x%x\n", val2
);
3353 static long cpu_number
;
3354 void trace_console(long len
, long len2
)
3357 /* This generated invalid code when the emission of the switch
3358 table isn't disabled. The asms are necessary to show the bug,
3359 normal statements don't work (they need to generate some code
3360 even under nocode_wanted, which normal statements don't do,
3361 but asms do). Also at least these number of cases is necessary
3362 to generate enough "random" bytes. They ultimately are enough
3363 to create invalid instruction patterns to which the first
3364 skip-to-decision-table jump jumps. If decision table emission
3365 is disabled all of this is no problem.
3367 It also is necessary that the switches are in a statement expression
3368 (which has the property of not being enterable from outside. no
3379 case 8: printf("bla"); pfo_ret__
= 42; break;
3381 pscr_ret__
= pfo_ret__
;
3388 case 1:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3389 case 2:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3390 case 4:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3391 case 8:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3392 default: printf("impossible\n");
3394 pscr_ret__
= pfo_ret__
;
3406 void test_asm_dead_code(void)
3409 /* Try to make sure that xdi contains a zero, and hence will
3410 lead to a segfault if the next asm is evaluated without
3411 arguments being set up. */
3412 asm volatile ("" : "=D" (rdi
) : "0" (0));
3415 /* This shouldn't trigger a segfault, either the argument
3416 registers need to be set up and the asm emitted despite
3417 this being in an unevaluated context, or both the argument
3418 setup _and_ the asm emission need to be suppressed. The latter
3419 is better. Disabling asm code gen when suppression is on
3420 also fixes the above trace_console bug, but that came earlier
3421 than asm suppression. */
3422 asm volatile ("movl $0,(%0)" : : "D" (&var
) : "memory");
3427 void test_asm_call(void)
3429 #if defined __x86_64__ && !defined _WIN64
3430 static char str
[] = "PATH";
3432 /* This tests if a reference to an undefined symbol from an asm
3433 block, which isn't otherwise referenced in this file, is correctly
3434 regarded as global symbol, so that it's resolved by other object files
3435 or libraries. We chose getenv here, which isn't used anywhere else
3436 in this file. (If we used e.g. printf, which is used we already
3437 would have a global symbol entry, not triggering the bug which is
3439 /* two pushes so stack remains aligned */
3440 asm volatile ("push %%rdi; push %%rdi; mov %0, %%rdi;"
3441 #if 1 && !defined(__TINYC__) && (defined(__PIC__) || defined(__PIE__))
3446 "pop %%rdi; pop %%rdi"
3447 : "=a" (s
) : "r" (str
));
3448 printf("asmd: %s\n", s
);
3452 #if defined __x86_64__
3453 # define RX "(%rip)"
3458 void asm_dot_test(void)
3465 asm(".text; lea S"RX
",%eax; lea ."RX
",%ecx; sub %ecx,%eax; S=.; jmp p0");
3467 asm(".text; jmp .+6; .int 123; mov .-4"RX
",%eax; jmp p0");
3470 asm(".pushsection \".data\"; Y=.; .int 999; X=Y; .int 456; X=.-4; .popsection");
3472 asm(".data; Y=.; .int 999; X=Y; .int 456; X=.-4; .text");
3474 asm(".text; mov X"RX
",%eax; jmp p0");
3477 asm(".data; X=.; .int 789; Y=.; .int 999; .previous");
3479 asm(".data; X=.; .int 789; Y=.; .int 999; .text");
3481 asm(".text; mov X"RX
",%eax; X=Y; jmp p0");
3483 asm(".text; p0=.; mov %%eax,%0;" : "=m"(r
)); break;
3487 printf("asm_dot_test %d: %d\n", x
, r
);
3494 unsigned int val
, val2
;
3495 struct struct123 s1
;
3496 struct struct1231 s2
= { (unsigned long)&s1
};
3497 /* Hide the outer base_func, but check later that the inline
3498 asm block gets the outer one. */
3500 void override_func3 (void);
3501 unsigned long asmret
;
3505 register int regvar
asm("%esi");
3507 printf("inline asm:\n");
3509 // parse 0x1E-1 as 3 tokens in asm mode
3510 asm volatile ("mov $0x1E-1,%eax");
3512 /* test the no operand case */
3513 asm volatile ("xorl %eax, %eax");
3515 memcpy1(buf
, "hello", 6);
3516 strncat1(buf
, " worldXXXXX", 3);
3517 printf("%s\n", buf
);
3519 memcpy2(buf
, "hello", 6);
3520 strncat2(buf
, " worldXXXXX", 3);
3521 printf("%s\n", buf
);
3523 /* 'A' constraint test */
3524 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
3525 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
3529 printf("mconstraint: %d", mconstraint_test(&s2
));
3530 printf(" %d %d\n", s1
.a
, s1
.b
);
3531 other_constraints_test();
3533 sigdelset1(&set
, 2);
3534 sigaddset1(&set
, 16);
3535 /* NOTE: we test here if C labels are correctly restored after the
3539 __asm__("btsl %1,%0" : "=m"(set
) : "Ir"(20) : "cc");
3540 printf("set=0x%x\n", set
);
3542 printf("swab32(0x%08x) = 0x%0x\n", val
, swab32(val
));
3546 /* The base_func ref from the following inline asm should find
3547 the global one, not the local decl from this function. */
3548 asm volatile(".weak override_func3\n.set override_func3, base_func");
3550 printf("asmstr: %s\n", get_asm_string());
3551 asm_local_label_diff();
3552 asm_local_statics();
3554 /* Check that we can also load structs of appropriate layout
3556 asm volatile("" : "=r" (asmret
) : "0"(s2
));
3557 if (asmret
!= s2
.addr
)
3558 printf("asmstr: failed\n");
3560 /* Check that the typesize correctly sets the register size to
3562 asm volatile("cmp %1,%2; sete %0" : "=a"(somebool
) : "r"(1), "r"(2));
3564 printf("asmbool: failed\n");
3567 fancy_copy (&val
, &val2
);
3568 printf ("fancycpy(%d)=%d\n", val
, val2
);
3570 fancy_copy2 (&val
, &val2
);
3571 printf ("fancycpy2(%d)=%d\n", val
, val2
);
3572 asm volatile ("mov $0x4243, %%esi" : "=r" (regvar
));
3573 printf ("regvar=%x\n", regvar
);
3574 test_high_clobbers();
3575 trace_console(8, 8);
3576 test_asm_dead_code();
3592 #define COMPAT_TYPE(type1, type2) \
3594 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
3595 __builtin_types_compatible_p (type1, type2));\
3600 void builtin_test(void)
3606 COMPAT_TYPE(int, int);
3607 COMPAT_TYPE(int, unsigned int);
3608 COMPAT_TYPE(int, char);
3609 COMPAT_TYPE(int, const int);
3610 COMPAT_TYPE(int, volatile int);
3611 COMPAT_TYPE(int *, int *);
3612 COMPAT_TYPE(int *, void *);
3613 COMPAT_TYPE(int *, const int *);
3614 COMPAT_TYPE(char *, unsigned char *);
3615 COMPAT_TYPE(char *, signed char *);
3616 COMPAT_TYPE(char *, char *);
3617 /* space is needed because tcc preprocessor introduces a space between each token */
3618 COMPAT_TYPE(char * *, void *);
3620 printf("res = %d\n", __builtin_constant_p(1));
3621 printf("res = %d\n", __builtin_constant_p(1 + 2));
3622 printf("res = %d\n", __builtin_constant_p(&constant_p_var
));
3623 printf("res = %d\n", __builtin_constant_p(constant_p_var
));
3624 printf("res = %d\n", __builtin_constant_p(100000 / constant_p_var
));
3627 i
= __builtin_choose_expr (1 != 0, ll
, s
);
3628 printf("bce: %d\n", i
);
3629 i
= __builtin_choose_expr (1 != 1, ll
, s
);
3630 printf("bce: %d\n", i
);
3631 i
= sizeof (__builtin_choose_expr (1, ll
, s
));
3632 printf("bce: %d\n", i
);
3633 i
= sizeof (__builtin_choose_expr (0, ll
, s
));
3634 printf("bce: %d\n", i
);
3636 //printf("bera: %p\n", __builtin_extract_return_addr((void*)43));
3640 extern int __attribute__((weak
)) weak_f1(void);
3641 extern int __attribute__((weak
)) weak_f2(void);
3642 extern int weak_f3(void);
3643 extern int __attribute__((weak
)) weak_v1
;
3644 extern int __attribute__((weak
)) weak_v2
;
3647 extern int (*weak_fpa
)() __attribute__((weak
));
3648 extern int __attribute__((weak
)) (*weak_fpb
)();
3649 extern __attribute__((weak
)) int (*weak_fpc
)();
3651 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak
));
3652 extern int __attribute((weak
)) weak_asm_f2(void) asm("weak_asm_f2x") ;
3653 extern int __attribute((weak
)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak
));
3654 extern int weak_asm_v1
asm("weak_asm_v1x") __attribute((weak
));
3655 extern int __attribute((weak
)) weak_asm_v2
asm("weak_asm_v2x") ;
3656 extern int __attribute((weak
)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak
));
3658 static const size_t dummy
= 0;
3659 extern __typeof(dummy
) weak_dummy1
__attribute__((weak
, alias("dummy")));
3660 extern __typeof(dummy
) __attribute__((weak
, alias("dummy"))) weak_dummy2
;
3661 extern __attribute__((weak
, alias("dummy"))) __typeof(dummy
) weak_dummy3
;
3663 int some_lib_func(void);
3664 int dummy_impl_of_slf(void) { return 444; }
3665 int some_lib_func(void) __attribute__((weak
, alias("dummy_impl_of_slf")));
3667 int weak_toolate() __attribute__((weak
));
3668 int weak_toolate() { return 0; }
3670 void __attribute__((weak
)) weak_test(void)
3672 printf("weak_f1=%d\n", weak_f1
? weak_f1() : 123);
3673 printf("weak_f2=%d\n", weak_f2
? weak_f2() : 123);
3674 printf("weak_f3=%d\n", weak_f3
? weak_f3() : 123);
3675 printf("weak_v1=%d\n",&weak_v1
? weak_v1
: 123);
3676 printf("weak_v2=%d\n",&weak_v2
? weak_v2
: 123);
3677 printf("weak_v3=%d\n",&weak_v3
? weak_v3
: 123);
3679 printf("weak_fpa=%d\n",&weak_fpa
? weak_fpa() : 123);
3680 printf("weak_fpb=%d\n",&weak_fpb
? weak_fpb() : 123);
3681 printf("weak_fpc=%d\n",&weak_fpc
? weak_fpc() : 123);
3683 printf("weak_asm_f1=%d\n", weak_asm_f1
!= NULL
);
3684 printf("weak_asm_f2=%d\n", weak_asm_f2
!= NULL
);
3685 printf("weak_asm_f3=%d\n", weak_asm_f3
!= NULL
);
3686 printf("weak_asm_v1=%d\n",&weak_asm_v1
!= NULL
);
3687 printf("weak_asm_v2=%d\n",&weak_asm_v2
!= NULL
);
3688 printf("weak_asm_v3=%d\n",&weak_asm_v3
!= NULL
);
3689 printf("some_lib_func=%d\n", &some_lib_func
? some_lib_func() : 0);
3692 int __attribute__((weak
)) weak_f2() { return 222; }
3693 int __attribute__((weak
)) weak_f3() { return 333; }
3694 int __attribute__((weak
)) weak_v2
= 222;
3695 int __attribute__((weak
)) weak_v3
= 333;
3698 void const_func(const int a
)
3702 void const_warn_test(void)
3711 int getme (struct condstruct
*s
, int i
)
3713 int i1
= (i
== 0 ? 0 : s
)->i
;
3714 int i2
= (i
== 0 ? s
: 0)->i
;
3715 int i3
= (i
== 0 ? (void*)0 : s
)->i
;
3716 int i4
= (i
== 0 ? s
: (void*)0)->i
;
3717 return i1
+ i2
+ i3
+ i4
;
3726 struct global_data global_data
;
3728 int global_data_getstuff (int *, int);
3730 void global_data_callit (int i
)
3732 *global_data
.b
[i
] = global_data_getstuff (global_data
.b
[i
], 1);
3735 int global_data_getstuff (int *p
, int i
)
3740 void global_data_test (void)
3742 global_data
.a
[0] = 42;
3743 global_data
.b
[0] = &global_data
.a
[0];
3744 global_data_callit (0);
3745 printf ("%d\n", global_data
.a
[0]);
3750 unsigned char fill
: 3;
3751 unsigned char b1
: 1;
3752 unsigned char b2
: 1;
3753 unsigned char fill2
: 3;
3756 int glob1
, glob2
, glob3
;
3758 void compare_comparisons (struct cmpcmpS
*s
)
3760 if (s
->b1
!= (glob1
== glob2
)
3761 || (s
->b2
!= (glob1
== glob3
)))
3762 printf ("comparing comparisons broken\n");
3765 void cmp_comparison_test(void)
3769 glob1
= 42; glob2
= 42;
3772 compare_comparisons (&s
);
3775 int fcompare (double a
, double b
, int code
)
3778 case 0: return a
== b
;
3779 case 1: return a
!= b
;
3780 case 2: return a
< b
;
3781 case 3: return a
>= b
;
3782 case 4: return a
> b
;
3783 case 5: return a
<= b
;
3788 void math_cmp_test(void)
3790 double nan
= 0.0/0.0;
3795 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
3797 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
3798 And it does this in various ways so that all code generation paths
3799 are checked (generating inverted tests, or non-inverted tests, or
3800 producing a 0/1 value without jumps (that's done in the fcompare
3802 #define FCMP(a,b,op,iop,code) \
3803 if (fcompare (a,b,code)) \
3804 bug (a,b,op,iop,1); \
3806 bug (a,b,op,iop,2); \
3810 bug (a,b,op,iop,3); \
3811 if ((a op b) || comp) \
3812 bug (a,b,op,iop,4); \
3813 if ((a iop b) || comp) \
3816 bug (a,b,op,iop,5); \
3817 if (v = !(a op b), !v) bug(a,b,op,iop,7);
3819 /* Equality tests. */
3820 FCMP(nan
, nan
, ==, !=, 0);
3821 FCMP(one
, two
, ==, !=, 0);
3822 FCMP(one
, one
, !=, ==, 1);
3823 /* Non-equality is a bit special. */
3824 if (!fcompare (nan
, nan
, 1))
3825 bug (nan
, nan
, !=, ==, 6);
3827 /* Relational tests on numbers. */
3828 FCMP(two
, one
, <, >=, 2);
3829 FCMP(one
, two
, >=, <, 3);
3830 FCMP(one
, two
, >, <=, 4);
3831 FCMP(two
, one
, <=, >, 5);
3833 /* Relational tests on NaNs. Note that the inverse op here is
3834 always !=, there's no operator in C that is equivalent to !(a < b),
3835 when NaNs are involved, same for the other relational ops. */
3836 FCMP(nan
, nan
, <, !=, 2);
3837 FCMP(nan
, nan
, >=, !=, 3);
3838 FCMP(nan
, nan
, >, !=, 4);
3839 FCMP(nan
, nan
, <=, !=, 5);
3842 double get100 () { return 100.0; }
3844 void callsave_test(void)
3846 #if defined __i386__ || defined __x86_64__ || defined __arm__
3847 int i
, s
; double *d
; double t
;
3848 s
= sizeof (double);
3849 printf ("callsavetest: %d\n", s
);
3850 d
= alloca (sizeof(double));
3852 /* x86-64 had a bug were the next call to get100 would evict
3853 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
3854 in int type, not pointer type. When alloca returns a pointer
3855 with the high 32 bit set (which is likely on x86-64) the access
3856 generates a segfault. */
3857 i
= d
[0] > get100 ();
3863 void bfa3(ptrdiff_t str_offset
)
3865 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset
);
3867 void bfa2(ptrdiff_t str_offset
)
3869 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset
);
3872 void bfa1(ptrdiff_t str_offset
)
3874 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset
);
3878 void builtin_frame_address_test(void)
3880 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
3882 char str
[] = "__builtin_frame_address";
3883 char *fp0
= __builtin_frame_address(0);
3885 printf("str: %s\n", str
);
3892 char via_volatile (char i
)
3899 struct __attribute__((__packed__
)) Spacked
{
3904 struct Spacked spacked
;
3905 typedef struct __attribute__((__packed__
)) {
3911 typedef struct Spacked3_s
{
3915 } __attribute__((__packed__
)) Spacked3
;
3917 struct gate_struct64
{
3918 unsigned short offset_low
;
3919 unsigned short segment
;
3920 unsigned ist
: 3, zero0
: 5, type
: 5, dpl
: 2, p
: 1;
3921 unsigned short offset_middle
;
3922 unsigned offset_high
;
3924 } __attribute__((packed
));
3925 typedef struct gate_struct64 gate_desc
;
3926 gate_desc a_gate_desc
;
3927 void attrib_test(void)
3930 printf("attr: %d %d %d %d\n", sizeof(struct Spacked
),
3931 sizeof(spacked
), sizeof(Spacked2
), sizeof(spacked2
));
3932 printf("attr: %d %d\n", sizeof(Spacked3
), sizeof(spacked3
));
3933 printf("attr: %d %d\n", sizeof(gate_desc
), sizeof(a_gate_desc
));
3936 extern __attribute__((__unused__
)) char * __attribute__((__unused__
)) *
3937 strange_attrib_placement (void);
3939 void * __attribute__((__unused__
)) get_void_ptr (void *a
)
3944 /* This part checks for a bug in TOK_GET (used for inline expansion),
3945 where the large long long constant left the the high bits set for
3946 the integer constant token. */
3948 int __get_order(unsigned long long size
)
3951 size
-= 0xffff880000000000ULL
; // this const left high bits set in the token
3953 struct S
{ int i
: 1; } s
; // constructed for this '1'
3959 /* This just forces the above inline function to be actually emitted. */
3960 int force_get_order(unsigned long s
)
3962 return __get_order(s
);