2 * TCC auto test program
8 /* Unfortunately, gcc version < 3 does not handle that! */
11 /* only gcc 3 handles _Bool correctly */
14 /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
15 #define CORRECT_CR_HANDLING
20 #define LONG_LONG_FORMAT "%lld"
21 #define ULONG_LONG_FORMAT "%llu"
23 #define LONG_LONG_FORMAT "%Ld"
24 #define ULONG_LONG_FORMAT "%Lu"
27 // MinGW has 80-bit rather than 64-bit long double which isn't compatible with TCC or MSVC
28 #if defined(_WIN32) && defined(__GNUC__)
29 #define LONG_DOUBLE double
30 #define LONG_DOUBLE_LITERAL(x) x
32 #define LONG_DOUBLE long double
33 #define LONG_DOUBLE_LITERAL(x) x ## L
36 /* deprecated and no longer supported in gcc 3.3 */
37 //#define ACCEPT_CR_IN_STRINGS
39 /* __VA_ARGS__ and __func__ support */
42 /* test various include syntaxes */
44 #define TCCLIB_INC <tcclib.h>
45 #define TCCLIB_INC1 <tcclib
46 #define TCCLIB_INC2 h>
47 #define TCCLIB_INC3 "tcclib.h"
51 #include TCCLIB_INC1.TCCLIB_INC2
53 #include TCCLIB_INC1.h>
63 /* Test two more ways to include a file named like a pp-number */
64 #define INC(name) <tests/name.h>
65 #define funnyname 42test.h
67 #define incname < incdir funnyname >
68 #define __stringify(x) #x
69 #define stringify(x) __stringify(x)
72 #include stringify(funnyname)
78 void recursive_macro_test();
93 void constant_expr_test();
95 void char_short_test();
97 void compound_literal_test(void);
99 void struct_assign_test(void);
100 void cast_test(void);
101 void bitfield_test(void);
102 void c99_bool_test(void);
103 void float_test(void);
104 void longlong_test(void);
105 void manyarg_test(void);
106 void stdarg_test(void);
107 void whitespace_test(void);
108 void relocation_test(void);
109 void old_style_function(void);
110 void alloca_test(void);
111 void c99_vla_test(int size1
, int size2
);
112 void sizeof_test(void);
113 void typeof_test(void);
114 void local_label_test(void);
115 void statement_expr_test(void);
117 void builtin_test(void);
118 void weak_test(void);
119 void global_data_test(void);
120 void cmp_comparison_test(void);
121 void math_cmp_test(void);
122 void callsave_test(void);
123 void builtin_frame_address_test(void);
124 void attrib_test(void);
128 void forward_ref(void);
131 /* Line joining happens before tokenization, so the following
132 must be parsed as ellipsis. */
133 void funny_line_continuation (int, ..\
136 char via_volatile (char);
141 #define M1(a, b) (a) + (b)
145 #define glue(a, b) a ## b
146 #define xglue(a, b) glue(a, b)
147 #define HIGHLOW "hello"
148 #define LOW LOW ", world"
150 static int onetwothree
= 123;
151 #define onetwothree4 onetwothree
152 #define onetwothree xglue(onetwothree,4)
154 #define min(a, b) ((a) < (b) ? (a) : (b))
157 #define dprintf(level,...) printf(__VA_ARGS__)
160 /* gcc vararg macros */
161 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
163 #define MACRO_NOARGS()
180 /* We try to handle this syntax. Make at least sure it doesn't segfault. */
181 char invalid_function_def()[] {}
184 #define __INT64_C(c) c ## LL
185 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
193 #define spin_lock(lock) do { } while (0)
194 #define wq_spin_lock spin_lock
195 #define TEST2() wq_spin_lock(a)
197 #define UINT_MAX ((unsigned) -1)
199 void intdiv_test(void)
201 printf("18/21=%u\n", 18/21);
202 printf("18%%21=%u\n", 18%21);
203 printf("41/21=%u\n", 41/21);
204 printf("41%%21=%u\n", 41%21);
205 printf("42/21=%u\n", 42/21);
206 printf("42%%21=%u\n", 42%21);
207 printf("43/21=%u\n", 43/21);
208 printf("43%%21=%u\n", 43%21);
209 printf("126/21=%u\n", 126/21);
210 printf("126%%21=%u\n", 126%21);
211 printf("131/21=%u\n", 131/21);
212 printf("131%%21=%u\n", 131%21);
213 printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX
/2+3)/2);
214 printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX
/2+3)%2);
216 printf("18/-21=%u\n", 18/-21);
217 printf("18%%-21=%u\n", 18%-21);
218 printf("41/-21=%u\n", 41/-21);
219 printf("41%%-21=%u\n", 41%-21);
220 printf("42/-21=%u\n", 42/-21);
221 printf("42%%-21=%u\n", 42%-21);
222 printf("43/-21=%u\n", 43/-21);
223 printf("43%%-21=%u\n", 43%-21);
224 printf("126/-21=%u\n", 126/-21);
225 printf("126%%-21=%u\n", 126%-21);
226 printf("131/-21=%u\n", 131/-21);
227 printf("131%%-21=%u\n", 131%-21);
228 printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX
/2+3)/-2);
229 printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX
/2+3)%-2);
231 printf("-18/21=%u\n", -18/21);
232 printf("-18%%21=%u\n", -18%21);
233 printf("-41/21=%u\n", -41/21);
234 printf("-41%%21=%u\n", -41%21);
235 printf("-42/21=%u\n", -42/21);
236 printf("-42%%21=%u\n", -42%21);
237 printf("-43/21=%u\n", -43/21);
238 printf("-43%%21=%u\n", -43%21);
239 printf("-126/21=%u\n", -126/21);
240 printf("-126%%21=%u\n", -126%21);
241 printf("-131/21=%u\n", -131/21);
242 printf("-131%%21=%u\n", -131%21);
243 printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX
/2+3))/2);
244 printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX
/2+3))%2);
246 printf("-18/-21=%u\n", -18/-21);
247 printf("-18%%-21=%u\n", -18%-21);
248 printf("-41/-21=%u\n", -41/-21);
249 printf("-41%%-21=%u\n", -41%-21);
250 printf("-42/-21=%u\n", -42/-21);
251 printf("-42%%-21=%u\n", -42%-21);
252 printf("-43/-21=%u\n", -43/-21);
253 printf("-43%%-21=%u\n", -43%-21);
254 printf("-126/-21=%u\n", -126/-21);
255 printf("-126%%-21=%u\n", -126%-21);
256 printf("-131/-21=%u\n", -131/-21);
257 printf("-131%%-21=%u\n", -131%-21);
258 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX
/2+3))/-2);
259 printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX
/2+3))%-2);
262 void macro_test(void)
264 printf("macro:\n");\f\v
266 printf("aaa=%d\n", AAA
);
268 printf("min=%d\n", min(1, min(2, -1)));
270 printf("s1=%s\n", glue(HIGH
, LOW
));
271 printf("s2=%s\n", xglue(HIGH
, LOW
));
272 printf("s3=%s\n", str("c"));
273 printf("s4=%s\n", str(a1
));
274 printf("B3=%d\n", B3
);
276 printf("onetwothree=%d\n", onetwothree
);
279 printf("A defined\n");
282 printf("B defined\n");
285 printf("A defined\n");
287 printf("A not defined\n");
290 printf("B defined\n");
292 printf("B not defined\n");
296 printf("A defined\n");
298 printf("B1 defined\n");
300 printf("B1 not defined\n");
303 printf("A not defined\n");
305 printf("B2 defined\n");
307 printf("B2 not defined\n");
312 printf("test true1\n");
315 printf("test true2\n");
318 printf("test true3\n");
321 printf("test trueA\n");
324 printf("test trueB\n");
340 printf("__LINE__ defined\n");
343 printf("__LINE__=%d __FILE__=%s\n",
347 printf("__LINE__=%d __FILE__=%s\n",
350 printf("__LINE__=%d __FILE__=%s\n",
352 #line 227 "tcctest.c"
355 /* not strictly preprocessor, but we test it there */
357 printf("__func__ = %s\n", __func__
);
358 dprintf(1, "vaarg=%d\n", 1);
360 dprintf1(1, "vaarg1\n");
361 dprintf1(1, "vaarg1=%d\n", 2);
362 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
365 printf("func='%s'\n", __FUNCTION__
);
367 /* complicated macros in glibc */
368 printf("INT64_MIN=" LONG_LONG_FORMAT
"\n", INT64_MIN
);
378 /* macro function with argument outside the macro string */
379 #define MF_s MF_hello
380 #define MF_hello(msg) printf("%s\n",msg)
382 #define MF_t printf("tralala\n"); MF_hello
387 /* test macro substituion inside args (should not eat stream) */
388 printf("qq=%d\n", qq(qq
)(2));
390 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
391 null argument without a space. gcc 3.2 fixes that. */
394 printf("qq1=%d\n", qq1( ));
396 /* comment with stray handling *\
398 /* this is a valid *\/ comment */
399 /* this is a valid comment *\*/
403 /* test function macro substitution when the function name is
407 /* And again when the name and parenthes are separated by a
409 TEST2
/* the comment */ ();
411 printf("%s\n", get_basefile_from_header());
412 printf("%s\n", __BASE_FILE__
);
413 printf("%s\n", get_file_from_header());
414 printf("%s\n", __FILE__
);
416 /* Check that funnily named include was in fact included */
417 have_included_42test_h
= 1;
418 have_included_42test_h_second
= 1;
419 have_included_42test_h_third
= 1;
423 static void print_num(char *fn
, int line
, int num
) {
424 printf("fn %s, line %d, num %d\n", fn
, line
, num
);
427 void recursive_macro_test(void)
430 #define ELF32_ST_TYPE(val) ((val) & 0xf)
431 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
432 #define STB_WEAK 2 /* Weak symbol */
433 #define ELFW(type) ELF##32##_##type
434 printf("%d\n", ELFW(ST_INFO
)(STB_WEAK
, ELFW(ST_TYPE
)(123)));
438 #define print_num(x) print_num(__FILE__,__LINE__,x)
440 WRAP(print_num(123));
441 WRAP(WRAP(print_num(123)));
443 static struct recursive_macro
{ int rm_field
; } G
;
444 #define rm_field (G.rm_field)
445 printf("rm_field = %d\n", rm_field
);
446 printf("rm_field = %d\n", WRAP(rm_field
));
447 WRAP((printf("rm_field = %d %d\n", rm_field
, WRAP(rm_field
))));
464 void ps(const char *s
)
476 const char foo1_string
[] = "\
485 printf("\141\1423\143\n");/* dezdez test */
486 printf("\x41\x42\x43\x3a\n");
487 printf("c=%c\n", 'r');
488 printf("wc=%C 0x%lx %C\n", L
'a', L
'\x1234', L
'c');
489 printf("foo1_string='%s'\n", foo1_string
);
491 printf("wstring=%S\n", L
"abc");
492 printf("wstring=%S\n", L
"abc" L
"def" "ghi");
493 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
494 printf("L'\\377'=%d L'\\xff'=%d\n", L
'\377', L
'\xff');
498 while ((b
= b
+ 1) < 96) {
502 printf("fib=%d\n", fib(33));
504 while (b
!= 0x80000000) {
517 for(i
= 0; i
< 10;i
++)
527 /* c99 for loop init test */
528 for (size_t count
= 1; count
< 3; count
++)
529 printf("count=%d\n", count
);
530 printf("count = %d\n", count
);
532 /* break/continue tests */
544 /* break/continue tests */
556 for(i
= 0;i
< 10;i
++) {
564 typedef int typedef_and_label
;
569 static void *label_table
[3] = { &&label1
, &&label2
, &&label3
};
573 /* This needs to parse as label, not as start of decl. */
584 /* we also test computed gotos (GCC extension) */
586 goto *label_table
[i
];
612 enum {E6
= 42, E7
, E8
} e
:8;
616 /* This is either 0 on L32 machines, or a large number
617 on L64 machines. We should be able to store this. */
618 EL_large
= ((unsigned long)0xf000 << 31) << 1,
621 enum { BIASU
= -1U<<31 };
622 enum { BIASS
= -1 << 31 };
624 static int getint(int i
)
629 return (int)(-1U << 31);
635 /* The following should give no warning */
637 struct S_enum s
= {E7
};
638 printf("enum: %d\n", s
.e
);
639 printf("enum:\n%d %d %d %d %d %d\n",
640 E0
, E1
, E2
, E3
, E4
, E5
);
642 printf("b1=%d\n", b1
);
643 printf("enum large: %ld\n", EL_large
);
645 if (getint(0) == BIASU
)
646 printf("enum unsigned: ok\n");
648 printf("enum unsigned: wrong\n");
649 if (getint(0) == BIASS
)
650 printf("enum unsigned: ok\n");
652 printf("enum unsigned: wrong\n");
668 printf("typedef:\n");
669 printf("a=%d\n", *a
);
671 printf("mytype2=%d\n", mytype2
);
676 printf("forward:\n");
682 void forward_ref(void)
684 printf("forward ok\n");
687 typedef struct struct1
{
707 struct struct1 st1
, st2
;
714 int main(int argc
, char **argv
)
719 recursive_macro_test();
734 constant_expr_test();
738 compound_literal_test();
740 struct_assign_test();
750 old_style_function();
755 statement_expr_test();
763 cmp_comparison_test();
766 builtin_frame_address_test();
768 if (via_volatile (42) != 42)
769 printf ("via_volatile broken\n");
781 printf("g1=%d\n", g
);
789 printf("g2=%d\n", g
);
793 printf("g3=%d\n", g
);
797 printf("g4=%d\n", g
);
800 printf("g5=%d\n", g
);
808 printf("sizeof(a) = %d\n", sizeof(a
));
809 printf("sizeof(\"a\") = %d\n", sizeof("a"));
811 printf("sizeof(__func__) = %d\n", sizeof(__func__
));
813 printf("sizeof tab %d\n", sizeof(tab
));
814 printf("sizeof tab2 %d\n", sizeof tab2
);
818 printf("%d %d %d\n", tab
[0], tab
[1], tab
[2]);
821 tab2
[i
][j
] = 10 * i
+ j
;
823 printf(" %3d", ((int *)tab2
)[i
]);
826 printf("sizeof(size_t)=%d\n", sizeof(size_t));
827 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
834 printf("%d\n", a
+= 1);
835 printf("%d\n", a
-= 2);
836 printf("%d\n", a
*= 31232132);
837 printf("%d\n", a
/= 4);
838 printf("%d\n", a
%= 20);
839 printf("%d\n", a
&= 6);
840 printf("%d\n", a
^= 7);
841 printf("%d\n", a
|= 8);
842 printf("%d\n", a
>>= 3);
843 printf("%d\n", a
<<= 4);
847 printf("%d\n", a
+ 1);
848 printf("%d\n", a
- 2);
849 printf("%d\n", a
* 312);
850 printf("%d\n", a
/ 4);
851 printf("%d\n", b
/ 4);
852 printf("%d\n", (unsigned)b
/ 4);
853 printf("%d\n", a
% 20);
854 printf("%d\n", b
% 20);
855 printf("%d\n", (unsigned)b
% 20);
856 printf("%d\n", a
& 6);
857 printf("%d\n", a
^ 7);
858 printf("%d\n", a
| 8);
859 printf("%d\n", a
>> 3);
860 printf("%d\n", b
>> 3);
861 printf("%d\n", (unsigned)b
>> 3);
862 printf("%d\n", a
<< 4);
867 printf("%d\n", 12 + 1);
868 printf("%d\n", 12 - 2);
869 printf("%d\n", 12 * 312);
870 printf("%d\n", 12 / 4);
871 printf("%d\n", 12 % 20);
872 printf("%d\n", 12 & 6);
873 printf("%d\n", 12 ^ 7);
874 printf("%d\n", 12 | 8);
875 printf("%d\n", 12 >> 2);
876 printf("%d\n", 12 << 4);
880 printf("%d %d %d %d\n",
889 return (c
>= 'a' & c
<= 'z') | (c
>= 'A' & c
<= 'Z') | c
== '_';
892 /**********************/
894 int vstack
[10], *vstack_ptr
;
896 void vpush(int vt
, int vc
)
902 void vpop(int *ft
, int *fc
)
915 vstack_ptr
[-2] &= ~0xffffff80;
917 printf("res= %d %d\n", a
, b
);
920 void constant_expr_test()
923 printf("constant_expr:\n");
925 printf("%d\n", a
* 16);
926 printf("%d\n", a
* 1);
927 printf("%d\n", a
+ 0);
937 printf("expr_ptr:\n");
940 printf("diff=%d\n", q
- p
);
942 printf("inc=%d\n", p
- tab4
);
944 printf("dec=%d\n", p
- tab4
);
946 printf("inc=%d\n", p
- tab4
);
948 printf("dec=%d\n", p
- tab4
);
949 printf("add=%d\n", p
+ 3 - tab4
);
950 printf("add=%d\n", 3 + p
- tab4
);
952 /* check if 64bit support is ok */
955 printf("%p %p %ld\n", q
, p
, p
-q
);
956 printf("%d %d %d %d %d %d\n",
957 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
960 printf("%p %p %ld\n", q
, p
, p
-q
);
961 printf("%d %d %d %d %d %d\n",
962 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
963 p
= (int *)((char *)p
+ 0xf0000000);
964 printf("%p %p %ld\n", q
, p
, p
-q
);
965 printf("%d %d %d %d %d %d\n",
966 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
968 printf("%p %p %ld\n", q
, p
, p
-q
);
969 printf("%d %d %d %d %d %d\n",
970 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
975 struct size12 s
[2], *sp
= s
;
980 printf("%d\n", sp
[j
].i
);
984 p
= (int*)0x100000000UL
+ i
;
986 printf("largeptr: %p %d\n", p
, i
);
993 printf("constant_expr:\n");
996 printf("%d\n", a
== a
);
997 printf("%d\n", a
!= a
);
999 printf("%d\n", a
< b
);
1000 printf("%d\n", a
<= b
);
1001 printf("%d\n", a
<= a
);
1002 printf("%d\n", b
>= a
);
1003 printf("%d\n", a
>= a
);
1004 printf("%d\n", b
> a
);
1006 printf("%d\n", (unsigned)a
< b
);
1007 printf("%d\n", (unsigned)a
<= b
);
1008 printf("%d\n", (unsigned)a
<= a
);
1009 printf("%d\n", (unsigned)b
>= a
);
1010 printf("%d\n", (unsigned)a
>= a
);
1011 printf("%d\n", (unsigned)b
> a
);
1034 struct __attribute__((aligned(16))) aligntest5
1041 } __attribute__((aligned(16)));
1046 struct aligntest5 altest5
[2];
1047 struct aligntest6 altest6
[2];
1049 /* altest7 is correctly aligned to 16 bytes also with TCC,
1050 but __alignof__ returns the wrong result (4) because we
1051 can't store the alignment yet when specified on symbols
1052 directly (it's stored in the type so we'd need to make
1054 struct aligntest7 altest7[2] __attribute__((aligned(16)));*/
1059 } __attribute__((aligned(4096)));
1062 unsigned long flags
;
1070 unsigned long index
;
1074 unsigned long counters
;
1088 unsigned long compound_head
;
1089 unsigned int compound_dtor
;
1090 unsigned int compound_order
;
1093 } __attribute__((aligned(2 * sizeof(long))));
1095 typedef unsigned long long __attribute__((aligned(4))) unaligned_u64
;
1098 unsigned int buf_nr
;
1099 unaligned_u64 start_lba
;
1102 struct aligntest10
{
1103 unsigned int buf_nr
;
1104 unsigned long long start_lba
;
1113 printf("struct:\n");
1114 printf("sizes: %d %d %d %d\n",
1115 sizeof(struct struct1
),
1116 sizeof(struct struct2
),
1117 sizeof(union union1
),
1118 sizeof(union union2
));
1119 printf("offsets: %d\n", (int)((char*)&st1
.u
.v1
- (char*)&st1
));
1123 printf("st1: %d %d %d\n",
1124 st1
.f1
, st1
.f2
, st1
.f3
);
1127 printf("union1: %d\n", st1
.u
.v1
);
1130 printf("union2: %d\n", u
.w1
);
1135 printf("st2: %d %d %d\n",
1136 s
->f1
, s
->f2
, s
->f3
);
1137 printf("str_addr=%x\n", (int)st1
.str
- (int)&st1
.f1
);
1139 /* align / size tests */
1140 printf("aligntest1 sizeof=%d alignof=%d\n",
1141 sizeof(struct aligntest1
), __alignof__(struct aligntest1
));
1142 printf("aligntest2 sizeof=%d alignof=%d\n",
1143 sizeof(struct aligntest2
), __alignof__(struct aligntest2
));
1144 printf("aligntest3 sizeof=%d alignof=%d\n",
1145 sizeof(struct aligntest3
), __alignof__(struct aligntest3
));
1146 printf("aligntest4 sizeof=%d alignof=%d\n",
1147 sizeof(struct aligntest4
), __alignof__(struct aligntest4
));
1148 printf("aligntest5 sizeof=%d alignof=%d\n",
1149 sizeof(struct aligntest5
), __alignof__(struct aligntest5
));
1150 printf("aligntest6 sizeof=%d alignof=%d\n",
1151 sizeof(struct aligntest6
), __alignof__(struct aligntest6
));
1152 printf("aligntest7 sizeof=%d alignof=%d\n",
1153 sizeof(struct aligntest7
), __alignof__(struct aligntest7
));
1154 printf("aligntest8 sizeof=%d alignof=%d\n",
1155 sizeof(struct aligntest8
), __alignof__(struct aligntest8
));
1156 printf("aligntest9 sizeof=%d alignof=%d\n",
1157 sizeof(struct aligntest9
), __alignof__(struct aligntest9
));
1158 printf("aligntest10 sizeof=%d alignof=%d\n",
1159 sizeof(struct aligntest10
), __alignof__(struct aligntest10
));
1160 printf("altest5 sizeof=%d alignof=%d\n",
1161 sizeof(altest5
), __alignof__(altest5
));
1162 printf("altest6 sizeof=%d alignof=%d\n",
1163 sizeof(altest6
), __alignof__(altest6
));
1164 /*printf("altest7 sizeof=%d alignof=%d\n",
1165 sizeof(altest7), __alignof__(altest7));*/
1167 /* empty structures (GCC extension) */
1168 printf("sizeof(struct empty) = %d\n", sizeof(struct empty
));
1169 printf("alignof(struct empty) = %d\n", __alignof__(struct empty
));
1171 printf("Large: sizeof=%d\n", sizeof(ls
));
1172 memset(&ls
, 0, sizeof(ls
));
1173 ls
.compound_head
= 42;
1174 printf("Large: offsetof(compound_head)=%d\n", (int)((char*)&ls
.compound_head
- (char*)&ls
));
1177 /* XXX: depend on endianness */
1178 void char_short_test()
1182 printf("char_short:\n");
1186 printf("s8=%d %d\n",
1187 *(char *)&var1
, *(char *)&var2
);
1188 printf("u8=%d %d\n",
1189 *(unsigned char *)&var1
, *(unsigned char *)&var2
);
1190 printf("s16=%d %d\n",
1191 *(short *)&var1
, *(short *)&var2
);
1192 printf("u16=%d %d\n",
1193 *(unsigned short *)&var1
, *(unsigned short *)&var2
);
1194 printf("s32=%d %d\n",
1195 *(int *)&var1
, *(int *)&var2
);
1196 printf("u32=%d %d\n",
1197 *(unsigned int *)&var1
, *(unsigned int *)&var2
);
1198 *(char *)&var1
= 0x08;
1199 printf("var1=%x\n", var1
);
1200 *(short *)&var1
= 0x0809;
1201 printf("var1=%x\n", var1
);
1202 *(int *)&var1
= 0x08090a0b;
1203 printf("var1=%x\n", var1
);
1206 /******************/
1208 typedef struct Sym
{
1216 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1217 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1219 static int toupper1(int a
)
1224 static unsigned int calc_vm_flags(unsigned int prot
)
1226 unsigned int prot_bits
;
1227 /* This used to segfault in some revisions: */
1228 prot_bits
= ((0x1==0x00000001)?(prot
&0x1):(prot
&0x1)?0x00000001:0);
1234 int *s
, a
, b
, t
, f
, i
;
1238 printf("!s=%d\n", !s
);
1242 printf("a=%d\n", a
);
1244 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1245 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1246 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1261 printf("b=%d\n", a
+ (0 ? 1 : a
/ 2));
1263 /* test register spilling */
1266 a
= (a
+ b
) * ((a
< b
) ?
1267 ((b
- a
) * (a
- b
)): a
+ b
);
1268 printf("a=%d\n", a
);
1270 /* test complex || or && expressions */
1274 printf("exp=%d\n", f
== (32 <= a
&& a
<= 3));
1275 printf("r=%d\n", (t
|| f
) + (t
&& f
));
1280 int aspect_native
= 65536;
1281 double bfu_aspect
= 1.0;
1283 for(aspect_on
= 0; aspect_on
< 2; aspect_on
++) {
1284 aspect
=aspect_on
?(aspect_native
*bfu_aspect
+0.5):65535UL;
1285 printf("aspect=%d\n", aspect
);
1289 /* test ? : GCC extension */
1291 static int v1
= 34 ? : -1; /* constant case */
1292 static int v2
= 0 ? : -1; /* constant case */
1295 printf("%d %d\n", v1
, v2
);
1296 printf("%d %d\n", a
- 30 ? : a
* 2, a
+ 1 ? : a
* 2);
1299 /* again complex expression */
1300 for(i
=0;i
<256;i
++) {
1301 if (toupper1 (i
) != TOUPPER (i
))
1302 printf("error %d\n", i
);
1304 printf ("bits = 0x%x\n", calc_vm_flags (0x1));
1307 extern int undefined_function(void);
1308 extern int defined_function(void);
1310 static inline void refer_to_undefined(void)
1312 undefined_function();
1315 void optimize_out(void)
1317 int i
= 0 ? undefined_function() : defined_function();
1318 printf ("oo:%d\n", i
);
1319 int j
= 1 ? defined_function() : undefined_function();
1320 printf ("oo:%d\n", j
);
1322 printf("oo:%d\n", undefined_function());
1324 printf("oo:%d\n", defined_function());
1326 printf("oo:%d\n", defined_function());
1328 printf("oo:%d\n", undefined_function());
1330 printf("oow:%d\n", defined_function());
1332 printf("oow:%d\n", undefined_function());
1335 /* Following is a switch without {} block intentionally. */
1338 printf ("oos:%d\n", defined_function());
1339 /* The following break shouldn't lead to disabled code after
1343 printf ("ool1:%d\n", defined_function());
1344 /* Same for the other types of loops. */
1348 printf ("ool2:%d\n", defined_function());
1351 printf ("ool3:%d\n", defined_function());
1352 /* Normal {} blocks without controlling statements
1353 shouldn't reactivate code emission */
1358 printf ("ool4:%d\n", undefined_function());
1363 break; /* this break shouldn't disable code outside the if. */
1364 printf("ool5:%d\n", defined_function());
1374 printf("ool6:%d\n", defined_function());
1378 /* Test that constants in logical && are optimized: */
1379 i
= 0 && undefined_function();
1380 i
= defined_function() && 0 && undefined_function();
1381 if (0 && undefined_function())
1382 undefined_function();
1383 if (defined_function() && 0)
1384 undefined_function();
1386 undefined_function();
1387 if (defined_function() && 0 && undefined_function())
1388 undefined_function();
1389 /* The same for || : */
1390 i
= 1 || undefined_function();
1391 i
= defined_function() || 1 || undefined_function();
1392 if (1 || undefined_function())
1395 undefined_function();
1396 if (defined_function() || 1)
1399 undefined_function();
1403 undefined_function();
1404 if (defined_function() || 1 || undefined_function())
1407 undefined_function();
1409 if (defined_function() && 0)
1410 refer_to_undefined();
1417 undefined_function();
1420 /* Leave the "if(1)return; printf()" in this order and last in the function */
1423 printf ("oor:%d\n", undefined_function());
1426 int defined_function(void)
1432 /* GCC accepts that */
1433 static int tab_reinit
[];
1434 static int tab_reinit
[10];
1436 //int cinit1; /* a global variable can be defined several times without error ! */
1440 int *cinit2
= (int []){3, 2, 1};
1442 void compound_literal_test(void)
1447 printf("compound_test:\n");
1449 p
= (int []){1, 2, 3};
1451 printf(" %d", p
[i
]);
1455 printf("%d", cinit2
[i
]);
1459 printf("q1=%s\n", q
);
1461 q
= (char *){ "tralala2" };
1462 printf("q2=%s\n", q
);
1465 printf("q3=%s\n", q3
);
1467 q
= (char []){ "tralala3" };
1468 printf("q4=%s\n", q
);
1471 p
= (int []){1, 2, cinit1
+ 3};
1473 printf(" %d", p
[i
]);
1477 p
= (int []){1, 2, 4 + i
};
1478 printf("%d %d %d\n",
1500 printf("kr_test:\n");
1501 printf("func1=%d\n", kr_func1(3, 4));
1502 printf("func2=%d\n", kr_func2(3, 4));
1509 tab
= (char*)malloc(20);
1526 /* structure assignment tests */
1532 struct structa1 ssta1
;
1534 void struct_assign_test1(struct structa1 s1
, int t
, float f
)
1536 printf("%d %d %d %f\n", s1
.f1
, s1
.f2
, t
, f
);
1539 struct structa1
struct_assign_test2(struct structa1 s1
, int t
)
1546 void struct_assign_test(void)
1549 struct structa1 lsta1
, lsta2
;
1556 printf("struct_assign_test:\n");
1560 printf("%d %d\n", s
.lsta1
.f1
, s
.lsta1
.f2
);
1562 printf("%d %d\n", s
.lsta2
.f1
, s
.lsta2
.f2
);
1567 struct_assign_test1(ps
->lsta2
, 3, 4.5);
1569 printf("before call: %d %d\n", s
.lsta2
.f1
, s
.lsta2
.f2
);
1570 ps
->lsta2
= struct_assign_test2(ps
->lsta2
, ps
->i
);
1571 printf("after call: %d %d\n", ps
->lsta2
.f1
, ps
->lsta2
.f2
);
1576 /* XXX: we should allow this even without braces */
1577 { struct_assign_test
}
1579 printf("%d\n", struct_assign_test
== t
[0].elem
);
1582 /* casts to short/char */
1584 void cast1(char a
, short b
, unsigned char c
, unsigned short d
)
1586 printf("%d %d %d %d\n", a
, b
, c
, d
);
1600 p
-= 0x700000000042;
1602 printf("cast_test:\n");
1606 printf("%d %d %d %d\n",
1609 (unsigned char)(a
+ 1),
1610 (unsigned short)(a
+ 1));
1611 printf("%d %d %d %d\n",
1614 (unsigned char)0xfffff,
1615 (unsigned short)0xfffff);
1617 a
= (bcast
= 128) + 1;
1619 a
= (scast
= 65536) + 1;
1622 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
1624 /* test cast from unsigned to signed short to int */
1627 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b
, d
);
1630 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b
, d
);
1632 /* test implicit int casting for array accesses */
1636 printf("%d %d\n", tab
[0], tab
[1]);
1638 /* test implicit casting on some operators */
1639 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1640 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1641 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1643 /* from pointer to integer types */
1644 printf("%d %d %ld %ld %lld %lld\n",
1645 (int)p
, (unsigned int)p
,
1646 (long)p
, (unsigned long)p
,
1647 (long long)p
, (unsigned long long)p
);
1649 /* from integers to pointers */
1650 printf("%p %p %p %p\n",
1651 (void *)a
, (void *)b
, (void *)c
, (void *)d
);
1654 /* initializers tests */
1655 struct structinit1
{
1664 int sinit3
[3] = { 1, 2, {{3}}, };
1665 int sinit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1666 int sinit5
[3][2] = { 1, 2, 3, 4, 5, 6 };
1667 int sinit6
[] = { 1, 2, 3 };
1668 int sinit7
[] = { [2] = 3, [0] = 1, 2 };
1669 char sinit8
[] = "hello" "trala";
1671 struct structinit1 sinit9
= { 1, 2, 3 };
1672 struct structinit1 sinit10
= { .f2
= 2, 3, .f1
= 1 };
1673 struct structinit1 sinit11
= { .f2
= 2, 3, .f1
= 1,
1681 char *sinit12
= "hello world";
1687 char sinit14
[10] = { "abc" };
1688 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
1690 struct { int a
[3], b
; } sinit16
[] = { { 1 }, 2 };
1706 struct complexinit0
{
1711 struct complexinit
{
1713 const struct complexinit0
*b
;
1716 const static struct complexinit cix
[] = {
1719 .b
= (const struct complexinit0
[]) {
1727 struct complexinit2
{
1732 struct complexinit2 cix20
;
1734 struct complexinit2 cix21
= {
1736 .b
= { 3001, 3002, 3003 }
1739 struct complexinit2 cix22
= {
1741 .b
= { 4001, 4002, 4003, 4004, 4005, 4006 }
1744 typedef int arrtype1
[];
1745 arrtype1 sinit19
= {1};
1746 arrtype1 sinit20
= {2,3};
1747 typedef int arrtype2
[3];
1748 arrtype2 sinit21
= {4};
1749 arrtype2 sinit22
= {5,6,7};
1751 /* Address comparisons of non-weak symbols with zero can be const-folded */
1752 int sinit23
[2] = { "astring" ? sizeof("astring") : -1,
1753 &sinit23
? 42 : -1 };
1755 void init_test(void)
1759 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1760 int linit6
[] = { 1, 2, 3 };
1762 char linit8
[] = "hello" "trala";
1763 int linit12
[10] = { 1, 2 };
1764 int linit13
[10] = { 1, 2, [7] = 3, [3] = 4, };
1765 char linit14
[10] = "abc";
1766 int linit15
[10] = { linit1
, linit1
+ 1, [6] = linit1
+ 2, };
1767 struct linit16
{ int a1
, a2
, a3
, a4
; } linit16
= { 1, .a3
= 2 };
1768 int linit17
= sizeof(linit17
);
1770 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1771 int linit18
[2] = {&zero
? 1 : -1, zero
? -1 : 1 };
1773 printf("init_test:\n");
1775 printf("sinit1=%d\n", sinit1
);
1776 printf("sinit2=%d\n", sinit2
);
1777 printf("sinit3=%d %d %d %d\n",
1783 printf("sinit6=%d\n", sizeof(sinit6
));
1784 printf("sinit7=%d %d %d %d\n",
1790 printf("sinit8=%s\n", sinit8
);
1791 printf("sinit9=%d %d %d\n",
1796 printf("sinit10=%d %d %d\n",
1801 printf("sinit11=%d %d %d %d %d %d\n",
1812 printf("[%d][%d] = %d %d %d\n",
1813 i
, j
, sinit4
[i
][j
], sinit5
[i
][j
], linit4
[i
][j
]);
1814 printf("linit1=%d\n", linit1
);
1815 printf("linit2=%d\n", linit2
);
1816 printf("linit6=%d\n", sizeof(linit6
));
1817 printf("linit8=%d %s\n", sizeof(linit8
), linit8
);
1819 printf("sinit12=%s\n", sinit12
);
1820 printf("sinit13=%d %s %s %s\n",
1825 printf("sinit14=%s\n", sinit14
);
1827 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
1829 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
1831 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
1833 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
1835 printf("%d %d %d %d\n",
1840 /* test that initialisation is done after variable declare */
1841 printf("linit17=%d\n", linit17
);
1842 printf("sinit15=%d\n", sinit15
[0]);
1843 printf("sinit16=%d %d\n", sinit16
[0].a
[0], sinit16
[1].a
[0]);
1844 printf("sinit17=%s %d %s %d\n",
1845 sinit17
[0].s
, sinit17
[0].len
,
1846 sinit17
[1].s
, sinit17
[1].len
);
1848 printf("%x ", sinit18
[i
]);
1850 /* complex init check */
1851 printf("cix: %d %d %d %d %d %d %d\n",
1853 cix
[0].b
[0].a
, cix
[0].b
[0].b
,
1854 cix
[0].b
[1].a
, cix
[0].b
[1].b
,
1855 cix
[0].b
[2].a
, cix
[0].b
[2].b
);
1856 printf("cix2: %d %d\n", cix21
.b
[2], cix22
.b
[5]);
1857 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20
, sizeof cix21
, sizeof cix22
);
1859 printf("arrtype1: %d %d %d\n", sinit19
[0], sinit20
[0], sinit20
[1]);
1860 printf("arrtype2: %d %d\n", sizeof(sinit19
), sizeof(sinit20
));
1861 printf("arrtype3: %d %d %d\n", sinit21
[0], sinit21
[1], sinit21
[2]);
1862 printf("arrtype4: %d %d %d\n", sinit22
[0], sinit22
[1], sinit22
[2]);
1863 printf("arrtype5: %d %d\n", sizeof(sinit21
), sizeof(sinit22
));
1864 printf("arrtype6: %d\n", sizeof(arrtype2
));
1866 printf("sinit23= %d %d\n", sinit23
[0], sinit23
[1]);
1867 printf("linit18= %d %d\n", linit18
[0], linit18
[1]);
1870 void switch_uc(unsigned char uc
)
1883 printf("ucsw: broken!\n");
1887 void switch_sc(signed char sc
)
1900 printf("scsw: broken!\n");
1907 unsigned long long ull
;
1932 for (i
= 1; i
<= 5; i
++) {
1933 ull
= (unsigned long long)i
<< 61;
1936 printf("ullsw:1\n");
1939 printf("ullsw:2\n");
1942 printf("ullsw:3\n");
1945 printf("ullsw:4\n");
1948 printf("ullsw:5\n");
1951 printf("ullsw: broken!\n");
1955 for (i
= 1; i
<= 5; i
++) {
1956 ll
= (long long)i
<< 61;
1974 printf("llsw: broken!\n");
1978 for (i
= -5; i
<= 5; i
++) {
1979 switch_uc((unsigned char)i
);
1982 for (i
= -5; i
<= 5; i
++) {
1983 switch_sc ((signed char)i
);
1987 /* ISOC99 _Bool type */
1988 void c99_bool_test(void)
1994 printf("bool_test:\n");
1995 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
1997 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
1999 printf("b = %d\n", b
);
2001 printf("b = %d\n", b
);
2005 void bitfield_test(void)
2017 unsigned int f5
: 7;
2019 printf("bitfield_test:");
2020 printf("sizeof(st1) = %d\n", sizeof(st1
));
2029 printf("%d %d %d %d %d\n",
2030 st1
.f1
, st1
.f2
, st1
.f3
, st1
.f4
, st1
.f5
);
2033 printf("%d %d\n", sa
, ca
);
2037 printf("st1.f1 == -1\n");
2039 printf("st1.f1 != -1\n");
2041 printf("st1.f2 == -1\n");
2043 printf("st1.f2 != -1\n");
2046 /* on i386 we don't correctly support long long bit-fields.
2047 The bitfields can straddle long long boundaries (at least with
2048 GCC bitfield layout) and code generation isn't prepared for this
2049 (would have to work with two words in that case). */
2050 /* bit sizes below must be bigger than 32 since GCC doesn't allow
2051 long-long bitfields whose size is not bigger than int */
2056 unsigned long long f3
: 38;
2058 st2
.f1
= 0x123456789ULL
;
2060 st2
.f2
= (long long)a
<< 25;
2063 printf("%lld %lld %lld\n", st2
.f1
, st2
.f2
, st2
.f3
);
2066 Disabled
for now until further clarification re GCC compatibility
2075 printf("sizeof(st3) = %d\n", sizeof(st3
));
2082 printf("st4.y == %d\n", st4
.y
);
2086 int x
: 12, y
: 4, : 0, : 4, z
: 3;
2088 } st5
= { 1, 2, 3, 4, -3, 6 };
2089 printf("st5 = %d %d %d %d %d %d\n", st5
.a
, st5
.b
, st5
.x
, st5
.y
, st5
.z
, st5
.c
);
2092 unsigned char y
: 2;
2095 printf("st6.y == %d\n", st6
.y
);
2099 #define FLOAT_FMT "%f\n"
2101 /* x86's float isn't compatible with GCC */
2102 #define FLOAT_FMT "%.5f\n"
2105 /* declare strto* functions as they are C99 */
2106 double strtod(const char *nptr
, char **endptr
);
2109 float strtof(const char *nptr
, char **endptr
) {return (float)strtod(nptr
, endptr
);}
2110 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
) {return (LONG_DOUBLE
)strtod(nptr
, endptr
);}
2112 float strtof(const char *nptr
, char **endptr
);
2113 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
);
2116 #define FTEST(prefix, typename, type, fmt)\
2117 void prefix ## cmp(type a, type b)\
2119 printf("%d %d %d %d %d %d\n",\
2126 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
2134 printf(fmt "\n", ++a);\
2135 printf(fmt "\n", a++);\
2136 printf(fmt "\n", a);\
2138 printf("%d %d\n", !a, !b);\
2140 void prefix ## fcast(type a)\
2148 unsigned long long llua;\
2153 printf("ftof: %f %f %Lf\n", fa, da, la);\
2155 llia = (long long)a;\
2156 a = (a >= 0) ? a : -a;\
2157 ua = (unsigned int)a;\
2158 llua = (unsigned long long)a;\
2159 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
2162 llia = -0x123456789012345LL;\
2163 llua = 0xf123456789012345LLU;\
2165 printf("itof: " fmt "\n", b);\
2167 printf("utof: " fmt "\n", b);\
2169 printf("lltof: " fmt "\n", b);\
2171 printf("ulltof: " fmt "\n", b);\
2174 float prefix ## retf(type a) { return a; }\
2175 double prefix ## retd(type a) { return a; }\
2176 LONG_DOUBLE prefix ## retld(type a) { return a; }\
2178 void prefix ## call(void)\
2180 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
2181 printf("double: %f\n", prefix ## retd(42.123456789));\
2182 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
2183 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
2186 void prefix ## signed_zeros(void) \
2188 type x = 0.0, y = -0.0, n, p;\
2190 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
2191 1.0 / x != 1.0 / y);\
2193 printf ("x != y; this is wrong!\n");\
2197 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
2198 1.0 / x != 1.0 / n);\
2200 printf ("x != -x; this is wrong!\n");\
2204 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
2205 1.0 / x != 1.0 / p);\
2207 printf ("x != +y; this is wrong!\n");\
2210 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
2211 1.0 / x != 1.0 / p);\
2213 printf ("x != -y; this is wrong!\n");\
2215 void prefix ## test(void)\
2217 printf("testing '%s'\n", #typename);\
2218 prefix ## cmp(1, 2.5);\
2219 prefix ## cmp(2, 1.5);\
2220 prefix ## cmp(1, 1);\
2221 prefix ## fcast(234.6);\
2222 prefix ## fcast(-2334.6);\
2224 prefix ## signed_zeros();\
2227 FTEST(f
, float, float, "%f")
2228 FTEST(d
, double, double, "%f")
2229 FTEST(ld
, long double, LONG_DOUBLE
, "%Lf")
2231 double ftab1
[3] = { 1.2, 3.4, -5.6 };
2234 void float_test(void)
2236 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
2242 printf("float_test:\n");
2243 printf("sizeof(float) = %d\n", sizeof(float));
2244 printf("sizeof(double) = %d\n", sizeof(double));
2245 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE
));
2249 printf("%f %f %f\n", ftab1
[0], ftab1
[1], ftab1
[2]);
2250 printf("%f %f %f\n", 2.12, .5, 2.3e10
);
2251 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
2253 printf("da=%f\n", da
);
2255 printf("fa=%f\n", fa
);
2258 printf("da = %f\n", da
);
2261 printf("db = %f\n", db
);
2270 return fib(n
-1) + fib(n
-2);
2283 printf("funcptr:\n");
2290 /* more complicated pointer computation */
2293 printf("sizeof1 = %d\n", sizeof(funcptr_test
));
2294 printf("sizeof2 = %d\n", sizeof funcptr_test
);
2295 printf("sizeof3 = %d\n", sizeof(&funcptr_test
));
2296 printf("sizeof4 = %d\n", sizeof &funcptr_test
);
2305 void lloptest(long long a
, long long b
)
2307 unsigned long long ua
, ub
;
2312 printf("arith: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2318 printf("arith1: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2324 printf("bin: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2330 printf("test: %d %d %d %d %d %d\n",
2338 printf("utest: %d %d %d %d %d %d\n",
2349 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2350 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
++, b
++);
2351 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", --a
, --b
);
2352 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2354 printf("not: %d %d %d %d\n", !a
, !ua
, !b
, !ub
);
2357 void llshift(long long a
, int b
)
2359 printf("shift: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2360 (unsigned long long)a
>> b
,
2363 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2364 (unsigned long long)a
>> 3,
2367 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2368 (unsigned long long)a
>> 35,
2378 long long la
, lb
, lc
;
2379 unsigned long long ula
, ulb
, ulc
;
2382 la
= (la
<< 20) | 0x12345;
2384 printf("la=" LONG_LONG_FORMAT
" ula=" ULONG_LONG_FORMAT
"\n", la
, ula
);
2389 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
2394 printf("ftoll: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", la
, lb
, lc
);
2399 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
2404 printf("ftoull: " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
"\n", ula
, ulb
, ulc
);
2407 long long llfunc1(int a
)
2417 long long int value(struct S
*v
)
2419 return ((long long int)v
->item
);
2422 long long llfunc2(long long x
, long long y
, int z
)
2427 void longlong_test(void)
2432 printf("longlong_test:\n");
2433 printf("sizeof(long long) = %d\n", sizeof(long long));
2438 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2439 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %Lx\n",
2443 0x1234567812345679);
2445 printf(LONG_LONG_FORMAT
"\n", a
);
2448 lloptest(0xff, 0x1234);
2449 b
= 0x72345678 << 10;
2453 b
= 0x72345678LL
<< 10;
2464 /* long long reg spill test */
2469 printf("%lld\n", value(&a
));
2471 lloptest(0x80000000, 0);
2474 long long *p
, v
, **pp
;
2478 printf("another long long spill test : %lld\n", *p
);
2481 v
= llfunc2(**pp
, **pp
, ia
);
2482 printf("a long long function (arm-)reg-args test : %lld\n", v
);
2486 printf("%d %d %d %d\n", a
> b
, a
< b
, a
>= b
, a
<= b
);
2488 printf(LONG_LONG_FORMAT
"\n", 0x123456789LLU
);
2490 /* long long pointer deref in argument passing test */
2496 void manyarg_test(void)
2498 LONG_DOUBLE ld
= 1234567891234LL;
2499 printf("manyarg_test:\n");
2500 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2501 1, 2, 3, 4, 5, 6, 7, 8,
2502 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2503 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2504 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2505 1, 2, 3, 4, 5, 6, 7, 8,
2506 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2507 1234567891234LL, 987654321986LL,
2509 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2510 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2511 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2512 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2513 1234567891234LL, 987654321986LL,
2515 printf("%d %d %d %d %d %d %d %d %Lf\n",
2516 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2517 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2518 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"%f %f %Lf\n",
2519 1, 2, 3, 4, 5, 6, 7, 8,
2520 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2521 1234567891234LL, 987654321986LL,
2523 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2524 "%Lf " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f %Lf\n",
2525 1, 2, 3, 4, 5, 6, 7, 8,
2526 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2527 ld
, 1234567891234LL, 987654321986LL,
2531 void vprintf1(const char *fmt
, ...)
2555 i
= va_arg(ap
, int);
2559 d
= va_arg(ap
, double);
2563 ll
= va_arg(ap
, long long);
2564 printf(LONG_LONG_FORMAT
, ll
);
2567 ld
= va_arg(ap
, LONG_DOUBLE
);
2585 void stdarg_for_struct(struct myspace bob
, ...)
2587 struct myspace george
, bill
;
2592 bill
= va_arg(ap
, struct myspace
);
2593 george
= va_arg(ap
, struct myspace
);
2594 validate
= va_arg(ap
, int);
2595 printf("stdarg_for_struct: %d %d %d %d\n",
2596 bob
.profile
, bill
.profile
, george
.profile
, validate
);
2600 void stdarg_for_libc(const char *fmt
, ...)
2603 va_start(args
, fmt
);
2608 void stdarg_test(void)
2610 LONG_DOUBLE ld
= 1234567891234LL;
2613 vprintf1("%d %d %d\n", 1, 2, 3);
2614 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2615 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2616 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2617 vprintf1("%d %f %l %F %d %f %l %F\n",
2618 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2619 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2620 1, 2, 3, 4, 5, 6, 7, 8,
2621 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2622 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2623 1, 2, 3, 4, 5, 6, 7, 8,
2624 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2625 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2627 1, 2, 3, 4, 5, 6, 7, 8,
2628 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2629 1234567891234LL, 987654321986LL,
2631 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2633 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2634 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2635 1234567891234LL, 987654321986LL,
2637 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2638 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2639 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2641 1, 2, 3, 4, 5, 6, 7, 8,
2642 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2643 1234567891234LL, 987654321986LL,
2645 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2646 "%F %l %l %f %f %F\n",
2647 1, 2, 3, 4, 5, 6, 7, 8,
2648 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2649 ld
, 1234567891234LL, 987654321986LL,
2653 stdarg_for_struct(bob
, bob
, bob
, bob
.profile
);
2654 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2657 void whitespace_test(void)
2663 ntf("whitspace:\n");\f\v
2667 #ifdef CORRECT_CR_HANDLING
2676 #ifdef ACCEPT_CR_IN_STRINGS
2677 printf("len1=%d\n", strlen("
2679 #ifdef CORRECT_CR_HANDLING
2682 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
2684 printf("len1=%d\n", strlen("
a
2686 #endif /* ACCEPT_CR_IN_STRINGS */
2689 int reltab
[3] = { 1, 2, 3 };
2691 int *rel1
= &reltab
[1];
2692 int *rel2
= &reltab
[2];
2695 void relocation_test(void) {}
2697 void getmyaddress(void)
2699 printf("in getmyaddress\n");
2703 long __pa_symbol(void)
2705 /* This 64bit constant was handled incorrectly, it was used as addend
2706 (which can hold 64bit just fine) in connection with a symbol,
2707 and TCC generates wrong code for that (displacements are 32bit only).
2708 This effectively is "+ 0x80000000", and if addresses of globals
2709 are below 2GB the result should be a number without high 32 bits set. */
2710 return ((long)(((unsigned long)(&rel1
))) - (0xffffffff80000000UL
));
2714 unsigned long theaddress
= (unsigned long)getmyaddress
;
2715 void relocation_test(void)
2717 void (*fptr
)(void) = (void (*)(void))theaddress
;
2718 printf("*rel1=%d\n", *rel1
);
2719 printf("*rel2=%d\n", *rel2
);
2722 printf("pa_symbol=0x%lx\n", __pa_symbol() >> 63);
2727 void old_style_f(a
,b
,c
)
2731 printf("a=%d b=%d b=%f\n", a
, b
, c
);
2734 void decl_func1(int cmpfn())
2736 printf("cmpfn=%lx\n", (long)cmpfn
);
2739 void decl_func2(cmpfn
)
2742 printf("cmpfn=%lx\n", (long)cmpfn
);
2745 void old_style_function(void)
2747 old_style_f((void *)1, 2, 3.0);
2754 #if defined __i386__ || defined __x86_64__ || defined __arm__
2755 char *p
= alloca(16);
2756 strcpy(p
,"123456789012345");
2757 printf("alloca: p is %s\n", p
);
2758 char *demo
= "This is only a test.\n";
2759 /* Test alloca embedded in a larger expression */
2760 printf("alloca: %s\n", strcpy(alloca(strlen(demo
)+1),demo
) );
2764 void *bounds_checking_is_enabled()
2766 char ca
[10], *cp
= ca
-1;
2767 return (ca
!= cp
+ 1) ? cp
: NULL
;
2770 typedef int constant_negative_array_size_as_compile_time_assertion_idiom
[(1 ? 2 : 0) - 1];
2772 void c99_vla_test(int size1
, int size2
)
2774 #if defined __i386__ || defined __x86_64__
2775 int size
= size1
* size2
;
2776 int tab1
[size
][2], tab2
[10][2];
2777 void *tab1_ptr
, *tab2_ptr
, *bad_ptr
;
2779 /* "size" should have been 'captured' at tab1 declaration,
2780 so modifying it should have no effect on VLA behaviour. */
2783 printf("Test C99 VLA 1 (sizeof): ");
2784 printf("%s\n", (sizeof tab1
== size1
* size2
* 2 * sizeof(int)) ? "PASSED" : "FAILED");
2787 printf("Test C99 VLA 2 (ptrs subtract): ");
2788 printf("%s\n", (tab2
- tab1
== (tab2_ptr
- tab1_ptr
) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2789 printf("Test C99 VLA 3 (ptr add): ");
2790 printf("%s\n", &tab1
[5][1] == (tab1_ptr
+ (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2791 printf("Test C99 VLA 4 (ptr access): ");
2792 tab1
[size1
][1] = 42;
2793 printf("%s\n", (*((int *) (tab1_ptr
+ (size1
* 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2795 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2796 if (bad_ptr
= bounds_checking_is_enabled()) {
2797 int *t1
= &tab1
[size1
* size2
- 1][3];
2798 int *t2
= &tab2
[9][3];
2799 printf("%s ", bad_ptr
== t1
? "PASSED" : "FAILED");
2800 printf("%s ", bad_ptr
== t2
? "PASSED" : "FAILED");
2802 char*c1
= 1 + sizeof(tab1
) + (char*)tab1
;
2803 char*c2
= 1 + sizeof(tab2
) + (char*)tab2
;
2804 printf("%s ", bad_ptr
== c1
? "PASSED" : "FAILED");
2805 printf("%s ", bad_ptr
== c2
? "PASSED" : "FAILED");
2809 printf("%s ", bad_ptr
== i1
? "PASSED" : "FAILED");
2810 printf("%s ", bad_ptr
== i2
? "PASSED" : "FAILED");
2812 int *x1
= tab1
[size1
* size2
+ 1];
2813 int *x2
= tab2
[10 + 1];
2814 printf("%s ", bad_ptr
== x1
? "PASSED" : "FAILED");
2815 printf("%s ", bad_ptr
== x2
? "PASSED" : "FAILED");
2817 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2824 typedef __SIZE_TYPE__
uintptr_t;
2827 void sizeof_test(void)
2832 printf("sizeof(int) = %d\n", sizeof(int));
2833 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2834 printf("sizeof(long) = %d\n", sizeof(long));
2835 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2836 printf("sizeof(short) = %d\n", sizeof(short));
2837 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2838 printf("sizeof(char) = %d\n", sizeof(char));
2839 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2840 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2842 printf("sizeof(a++) = %d\n", sizeof a
++);
2843 printf("a=%d\n", a
);
2845 printf("sizeof(**ptr) = %d\n", sizeof (**ptr
));
2847 /* The type of sizeof should be as large as a pointer, actually
2848 it should be size_t. */
2849 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2852 /* Effectively <<32, but defined also on 32bit machines. */
2856 /* This checks that sizeof really can be used to manipulate
2857 uintptr_t objects, without truncation. */
2858 t2
= t
& -sizeof(uintptr_t);
2859 printf ("%lu %lu\n", t
, t2
);
2861 /* some alignof tests */
2862 printf("__alignof__(int) = %d\n", __alignof__(int));
2863 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2864 printf("__alignof__(short) = %d\n", __alignof__(short));
2865 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2866 printf("__alignof__(char) = %d\n", __alignof__(char));
2867 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2868 printf("__alignof__(func) = %d\n", __alignof__
sizeof_test());
2870 /* sizes of VLAs need to be evaluated even inside sizeof: */
2872 printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a
]));
2873 /* And checking if sizeof compound literal works. Parenthesized: */
2874 printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n",
2875 sizeof( (struct {int i
; int j
;}){4,5} ));
2876 /* And as direct sizeof argument (as unary expression): */
2877 printf("sizeof (struct {short i; short j;}){4,5} = %d\n",
2878 sizeof (struct {short i
; short j
;}){4,5} );
2880 /* sizeof(x && y) should be sizeof(int), even if constant
2881 evaluating is possible. */
2882 printf("sizeof(t && 0) = %d\n", sizeof(t
&& 0));
2883 printf("sizeof(1 && 1) = %d\n", sizeof(1 && 1));
2884 printf("sizeof(t || 1) = %d\n", sizeof(t
|| 1));
2885 printf("sizeof(0 || 0) = %d\n", sizeof(0 || 0));
2888 void typeof_test(void)
2897 printf("a=%f b=%f c=%f\n", a
, b
, c
);
2903 struct hlist_node
*first
, *last
;
2906 void statement_expr_test(void)
2910 /* Basic stmt expr test */
2920 printf("a=%d\n", a
);
2922 /* Test that symbols aren't freed prematurely.
2923 With SYM_DEBUG valgrind will show a read from a freed
2924 symbol, and tcc will show an (invalid) warning on the initialization
2925 of 'ptr' below, if symbols are popped after the stmt expr. */
2926 void *v
= (void*)39;
2928 (struct hlist_node
*)v
;
2931 ptr
= (struct hlist_node
*)v
;
2933 /* This part used to segfault when symbols were popped prematurely.
2934 The symbols for the static local would be overwritten with
2935 helper symbols from the pre-processor expansions in between. */
2936 #define some_attr __attribute__((aligned(1)))
2937 #define tps(str) ({ \
2938 static const char *t some_attr = str; \
2941 printf ("stmtexpr: %s %s\n",
2942 tps("somerandomlongstring"),
2943 tps("anotherlongstring"));
2945 /* Test that the three decls of 't' don't interact. */
2947 int b
= ({ int t
= 41; t
; });
2948 int c
= ({ int t
= 42; t
; });
2950 /* Test that aggregate return values work. */
2953 typedef struct hlist_head T
;
2955 T t
= { (void*)43, (void*)44 };
2959 printf ("stmtexpr: %d %d %d\n", t
, b
, c
);
2960 printf ("stmtexpr: %ld %ld\n", (long)h
.first
, (long)h
.last
);
2963 void local_label_test(void)
2969 __label__ l1
, l2
, l3
, l4
;
2983 printf("a=%d\n", a
);
2993 /* inline assembler test */
2994 #if defined(__i386__) || defined(__x86_64__)
2996 /* from linux kernel */
2997 static char * strncat1(char * dest
,const char * src
,size_t count
)
2999 long d0
, d1
, d2
, d3
;
3000 __asm__
__volatile__(
3009 "testb %%al,%%al\n\t"
3013 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
3014 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
3019 static char * strncat2(char * dest
,const char * src
,size_t count
)
3021 long d0
, d1
, d2
, d3
;
3022 __asm__
__volatile__(
3023 "repne scasb\n\t" /* one-line repne prefix + string op */
3030 "testb %%al,%%al\n\t"
3034 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
3035 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
3040 static inline void * memcpy1(void * to
, const void * from
, size_t n
)
3043 __asm__
__volatile__(
3048 "1:\ttestb $1,%b4\n\t"
3052 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
3053 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
3058 static inline void * memcpy2(void * to
, const void * from
, size_t n
)
3061 __asm__
__volatile__(
3062 "rep movsl\n\t" /* one-line rep prefix + string op */
3066 "1:\ttestb $1,%b4\n\t"
3070 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
3071 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
3076 static __inline__
void sigaddset1(unsigned int *set
, int _sig
)
3078 __asm__("btsl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
3081 static __inline__
void sigdelset1(unsigned int *set
, int _sig
)
3083 asm("btrl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc", "flags");
3086 static __inline__ __const__
unsigned int swab32(unsigned int x
)
3088 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
3089 "rorl $16,%0\n\t" /* swap words */
3090 "xchgb %b0,%h0" /* swap higher bytes */
3096 static __inline__
unsigned long long mul64(unsigned int a
, unsigned int b
)
3098 unsigned long long res
;
3100 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
3101 but still test the 32bit->64bit mull. */
3102 unsigned int resh
, resl
;
3103 __asm__("mull %2" : "=a" (resl
), "=d" (resh
) : "a" (a
), "r" (b
));
3104 res
= ((unsigned long long)resh
<< 32) | resl
;
3106 __asm__("mull %2" : "=A" (res
) : "a" (a
), "r" (b
));
3111 static __inline__
unsigned long long inc64(unsigned long long a
)
3113 unsigned long long res
;
3115 /* Using the A constraint is wrong, and increments are tested
3119 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res
) : "A" (a
));
3132 unsigned long mconstraint_test(struct struct1231
*r
)
3137 __asm__
volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
3138 : "=&r" (ret
), "=m" (a
)
3139 : "m" (*(struct struct123
*)r
->addr
));
3144 int fls64(unsigned long long x
)
3154 void other_constraints_test(void)
3159 __asm__
volatile ("mov %P1,%0" : "=r" (ret
) : "p" (&var
));
3160 printf ("oc1: %d\n", ret
== (unsigned long)&var
);
3165 /* Test global asm blocks playing with aliases. */
3166 void base_func(void)
3168 printf ("asmc: base\n");
3171 extern void override_func1 (void);
3172 extern void override_func2 (void);
3174 asm(".weak override_func1\n.set override_func1, base_func");
3175 asm(".set override_func1, base_func");
3176 asm(".set override_func2, base_func");
3178 void override_func2 (void)
3180 printf ("asmc: override2\n");
3183 /* This checks a construct used by the linux kernel to encode
3184 references to strings by PC relative references. */
3185 extern int bug_table
[] __attribute__((section("__bug_table")));
3186 char * get_asm_string (void)
3188 extern int some_symbol
;
3189 asm volatile (".globl some_symbol\n"
3192 "some_symbol: .long 0\n"
3193 ".pushsection __bug_table, \"a\"\n"
3194 ".globl bug_table\n"
3196 /* The first entry (1b-2b) is unused in this test,
3197 but we include it to check if cross-section
3198 PC-relative references work. */
3199 "2:\t.long 1b - 2b, %c0 - 2b\n"
3200 ".popsection\n" : : "i" ("A string"));
3201 char * str
= ((char*)bug_table
) + bug_table
[1];
3208 void fancy_copy (unsigned *in
, unsigned *out
)
3210 asm volatile ("" : "=r" (*out
) : "0" (*in
));
3213 void fancy_copy2 (unsigned *in
, unsigned *out
)
3215 asm volatile ("mov %0,(%1)" : : "r" (*in
), "r" (out
) : "memory");
3219 void clobber_r12(void)
3221 asm volatile("mov $1, %%r12" ::: "r12");
3225 void test_high_clobbers(void)
3228 register long val
asm("r12");
3230 /* This tests if asm clobbers correctly save/restore callee saved
3231 registers if they are clobbered and if it's the high 8 x86-64
3232 registers. This is fragile for GCC as the constraints do not
3233 correctly capture the data flow, but good enough for us. */
3234 asm volatile("mov $0x4542, %%r12" : "=r" (val
):: "memory");
3237 asm volatile("mov %%r12, %0" : "=r" (val2
) : "r" (val
): "memory");
3238 printf("asmhc: 0x%x\n", val2
);
3243 static long cpu_number
;
3244 void trace_console(long len
, long len2
)
3247 /* This generated invalid code when the emission of the switch
3248 table isn't disabled. The asms are necessary to show the bug,
3249 normal statements don't work (they need to generate some code
3250 even under nocode_wanted, which normal statements don't do,
3251 but asms do). Also at least these number of cases is necessary
3252 to generate enough "random" bytes. They ultimately are enough
3253 to create invalid instruction patterns to which the first
3254 skip-to-decision-table jump jumps. If decision table emission
3255 is disabled all of this is no problem.
3257 It also is necessary that the switches are in a statement expression
3258 (which has the property of not being enterable from outside. no
3269 case 8: printf("bla"); pfo_ret__
= 42; break;
3271 pscr_ret__
= pfo_ret__
;
3278 case 1:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3279 case 2:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3280 case 4:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3281 case 8:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3282 default: printf("impossible\n");
3284 pscr_ret__
= pfo_ret__
;
3296 void test_asm_dead_code(void)
3299 /* Try to make sure that xdi contains a zero, and hence will
3300 lead to a segfault if the next asm is evaluated without
3301 arguments being set up. */
3302 asm volatile ("" : "=D" (rdi
) : "0" (0));
3305 /* This shouldn't trigger a segfault, either the argument
3306 registers need to be set up and the asm emitted despite
3307 this being in an unevaluated context, or both the argument
3308 setup _and_ the asm emission need to be suppressed. The latter
3309 is better. Disabling asm code gen when suppression is on
3310 also fixes the above trace_console bug, but that came earlier
3311 than asm suppression. */
3312 asm volatile ("movl $0,(%0)" : : "D" (&var
) : "memory");
3320 unsigned int val
, val2
;
3321 struct struct123 s1
;
3322 struct struct1231 s2
= { (unsigned long)&s1
};
3323 /* Hide the outer base_func, but check later that the inline
3324 asm block gets the outer one. */
3326 void override_func3 (void);
3327 unsigned long asmret
;
3331 register int regvar
asm("%esi");
3333 printf("inline asm:\n");
3335 // parse 0x1E-1 as 3 tokens in asm mode
3336 asm volatile ("mov $0x1E-1,%eax");
3338 /* test the no operand case */
3339 asm volatile ("xorl %eax, %eax");
3341 memcpy1(buf
, "hello", 6);
3342 strncat1(buf
, " worldXXXXX", 3);
3343 printf("%s\n", buf
);
3345 memcpy2(buf
, "hello", 6);
3346 strncat2(buf
, " worldXXXXX", 3);
3347 printf("%s\n", buf
);
3349 /* 'A' constraint test */
3350 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
3351 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
3355 printf("mconstraint: %d", mconstraint_test(&s2
));
3356 printf(" %d %d\n", s1
.a
, s1
.b
);
3357 other_constraints_test();
3359 sigdelset1(&set
, 2);
3360 sigaddset1(&set
, 16);
3361 /* NOTE: we test here if C labels are correctly restored after the
3365 __asm__("btsl %1,%0" : "=m"(set
) : "Ir"(20) : "cc");
3366 printf("set=0x%x\n", set
);
3368 printf("swab32(0x%08x) = 0x%0x\n", val
, swab32(val
));
3372 /* The base_func ref from the following inline asm should find
3373 the global one, not the local decl from this function. */
3374 asm volatile(".weak override_func3\n.set override_func3, base_func");
3376 printf("asmstr: %s\n", get_asm_string());
3378 /* Check that we can also load structs of appropriate layout
3380 asm volatile("" : "=r" (asmret
) : "0"(s2
));
3381 if (asmret
!= s2
.addr
)
3382 printf("asmstr: failed\n");
3384 /* Check that the typesize correctly sets the register size to
3386 asm volatile("cmp %1,%2; sete %0" : "=a"(somebool
) : "r"(1), "r"(2));
3388 printf("asmbool: failed\n");
3391 fancy_copy (&val
, &val2
);
3392 printf ("fancycpy(%d)=%d\n", val
, val2
);
3394 fancy_copy2 (&val
, &val2
);
3395 printf ("fancycpy2(%d)=%d\n", val
, val2
);
3396 asm volatile ("mov $0x4243, %%esi" : "=r" (regvar
));
3397 printf ("regvar=%x\n", regvar
);
3398 test_high_clobbers();
3399 trace_console(8, 8);
3400 test_asm_dead_code();
3414 #define COMPAT_TYPE(type1, type2) \
3416 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
3417 __builtin_types_compatible_p (type1, type2));\
3422 void builtin_test(void)
3428 COMPAT_TYPE(int, int);
3429 COMPAT_TYPE(int, unsigned int);
3430 COMPAT_TYPE(int, char);
3431 COMPAT_TYPE(int, const int);
3432 COMPAT_TYPE(int, volatile int);
3433 COMPAT_TYPE(int *, int *);
3434 COMPAT_TYPE(int *, void *);
3435 COMPAT_TYPE(int *, const int *);
3436 COMPAT_TYPE(char *, unsigned char *);
3437 COMPAT_TYPE(char *, signed char *);
3438 COMPAT_TYPE(char *, char *);
3439 /* space is needed because tcc preprocessor introduces a space between each token */
3440 COMPAT_TYPE(char * *, void *);
3442 printf("res = %d\n", __builtin_constant_p(1));
3443 printf("res = %d\n", __builtin_constant_p(1 + 2));
3444 printf("res = %d\n", __builtin_constant_p(&constant_p_var
));
3445 printf("res = %d\n", __builtin_constant_p(constant_p_var
));
3446 printf("res = %d\n", __builtin_constant_p(100000 / constant_p_var
));
3449 i
= __builtin_choose_expr (1 != 0, ll
, s
);
3450 printf("bce: %d\n", i
);
3451 i
= __builtin_choose_expr (1 != 1, ll
, s
);
3452 printf("bce: %d\n", i
);
3453 i
= sizeof (__builtin_choose_expr (1, ll
, s
));
3454 printf("bce: %d\n", i
);
3455 i
= sizeof (__builtin_choose_expr (0, ll
, s
));
3456 printf("bce: %d\n", i
);
3458 //printf("bera: %p\n", __builtin_extract_return_addr((void*)43));
3462 extern int __attribute__((weak
)) weak_f1(void);
3463 extern int __attribute__((weak
)) weak_f2(void);
3464 extern int weak_f3(void);
3465 extern int __attribute__((weak
)) weak_v1
;
3466 extern int __attribute__((weak
)) weak_v2
;
3469 extern int (*weak_fpa
)() __attribute__((weak
));
3470 extern int __attribute__((weak
)) (*weak_fpb
)();
3471 extern __attribute__((weak
)) int (*weak_fpc
)();
3473 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak
));
3474 extern int __attribute((weak
)) weak_asm_f2(void) asm("weak_asm_f2x") ;
3475 extern int __attribute((weak
)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak
));
3476 extern int weak_asm_v1
asm("weak_asm_v1x") __attribute((weak
));
3477 extern int __attribute((weak
)) weak_asm_v2
asm("weak_asm_v2x") ;
3478 extern int __attribute((weak
)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak
));
3480 static const size_t dummy
= 0;
3481 extern __typeof(dummy
) weak_dummy1
__attribute__((weak
, alias("dummy")));
3482 extern __typeof(dummy
) __attribute__((weak
, alias("dummy"))) weak_dummy2
;
3483 extern __attribute__((weak
, alias("dummy"))) __typeof(dummy
) weak_dummy3
;
3485 int some_lib_func(void);
3486 int dummy_impl_of_slf(void) { return 444; }
3487 int some_lib_func(void) __attribute__((weak
, alias("dummy_impl_of_slf")));
3489 int weak_toolate() __attribute__((weak
));
3490 int weak_toolate() { return 0; }
3492 void __attribute__((weak
)) weak_test(void)
3494 printf("weak_f1=%d\n", weak_f1
? weak_f1() : 123);
3495 printf("weak_f2=%d\n", weak_f2
? weak_f2() : 123);
3496 printf("weak_f3=%d\n", weak_f3
? weak_f3() : 123);
3497 printf("weak_v1=%d\n",&weak_v1
? weak_v1
: 123);
3498 printf("weak_v2=%d\n",&weak_v2
? weak_v2
: 123);
3499 printf("weak_v3=%d\n",&weak_v3
? weak_v3
: 123);
3501 printf("weak_fpa=%d\n",&weak_fpa
? weak_fpa() : 123);
3502 printf("weak_fpb=%d\n",&weak_fpb
? weak_fpb() : 123);
3503 printf("weak_fpc=%d\n",&weak_fpc
? weak_fpc() : 123);
3505 printf("weak_asm_f1=%d\n", weak_asm_f1
!= NULL
);
3506 printf("weak_asm_f2=%d\n", weak_asm_f2
!= NULL
);
3507 printf("weak_asm_f3=%d\n", weak_asm_f3
!= NULL
);
3508 printf("weak_asm_v1=%d\n",&weak_asm_v1
!= NULL
);
3509 printf("weak_asm_v2=%d\n",&weak_asm_v2
!= NULL
);
3510 printf("weak_asm_v3=%d\n",&weak_asm_v3
!= NULL
);
3511 printf("some_lib_func=%d\n", &some_lib_func
? some_lib_func() : 0);
3514 int __attribute__((weak
)) weak_f2() { return 222; }
3515 int __attribute__((weak
)) weak_f3() { return 333; }
3516 int __attribute__((weak
)) weak_v2
= 222;
3517 int __attribute__((weak
)) weak_v3
= 333;
3520 void const_func(const int a
)
3524 void const_warn_test(void)
3533 int getme (struct condstruct
*s
, int i
)
3535 int i1
= (i
== 0 ? 0 : s
)->i
;
3536 int i2
= (i
== 0 ? s
: 0)->i
;
3537 int i3
= (i
== 0 ? (void*)0 : s
)->i
;
3538 int i4
= (i
== 0 ? s
: (void*)0)->i
;
3539 return i1
+ i2
+ i3
+ i4
;
3548 struct global_data global_data
;
3550 int global_data_getstuff (int *, int);
3552 void global_data_callit (int i
)
3554 *global_data
.b
[i
] = global_data_getstuff (global_data
.b
[i
], 1);
3557 int global_data_getstuff (int *p
, int i
)
3562 void global_data_test (void)
3564 global_data
.a
[0] = 42;
3565 global_data
.b
[0] = &global_data
.a
[0];
3566 global_data_callit (0);
3567 printf ("%d\n", global_data
.a
[0]);
3572 unsigned char fill
: 3;
3573 unsigned char b1
: 1;
3574 unsigned char b2
: 1;
3575 unsigned char fill2
: 3;
3578 int glob1
, glob2
, glob3
;
3580 void compare_comparisons (struct cmpcmpS
*s
)
3582 if (s
->b1
!= (glob1
== glob2
)
3583 || (s
->b2
!= (glob1
== glob3
)))
3584 printf ("comparing comparisons broken\n");
3587 void cmp_comparison_test(void)
3591 glob1
= 42; glob2
= 42;
3594 compare_comparisons (&s
);
3597 int fcompare (double a
, double b
, int code
)
3600 case 0: return a
== b
;
3601 case 1: return a
!= b
;
3602 case 2: return a
< b
;
3603 case 3: return a
>= b
;
3604 case 4: return a
> b
;
3605 case 5: return a
<= b
;
3609 void math_cmp_test(void)
3611 double nan
= 0.0/0.0;
3615 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
3617 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
3618 And it does this in various ways so that all code generation paths
3619 are checked (generating inverted tests, or non-inverted tests, or
3620 producing a 0/1 value without jumps (that's done in the fcompare
3622 #define FCMP(a,b,op,iop,code) \
3623 if (fcompare (a,b,code)) \
3624 bug (a,b,op,iop,1); \
3626 bug (a,b,op,iop,2); \
3630 bug (a,b,op,iop,3); \
3631 if ((a op b) || comp) \
3632 bug (a,b,op,iop,4); \
3633 if ((a iop b) || comp) \
3638 /* Equality tests. */
3639 FCMP(nan
, nan
, ==, !=, 0);
3640 FCMP(one
, two
, ==, !=, 0);
3641 FCMP(one
, one
, !=, ==, 1);
3642 /* Non-equality is a bit special. */
3643 if (!fcompare (nan
, nan
, 1))
3644 bug (nan
, nan
, !=, ==, 6);
3646 /* Relational tests on numbers. */
3647 FCMP(two
, one
, <, >=, 2);
3648 FCMP(one
, two
, >=, <, 3);
3649 FCMP(one
, two
, >, <=, 4);
3650 FCMP(two
, one
, <=, >, 5);
3652 /* Relational tests on NaNs. Note that the inverse op here is
3653 always !=, there's no operator in C that is equivalent to !(a < b),
3654 when NaNs are involved, same for the other relational ops. */
3655 FCMP(nan
, nan
, <, !=, 2);
3656 FCMP(nan
, nan
, >=, !=, 3);
3657 FCMP(nan
, nan
, >, !=, 4);
3658 FCMP(nan
, nan
, <=, !=, 5);
3661 double get100 () { return 100.0; }
3663 void callsave_test(void)
3665 #if defined __i386__ || defined __x86_64__ || defined __arm__
3666 int i
, s
; double *d
; double t
;
3667 s
= sizeof (double);
3668 printf ("callsavetest: %d\n", s
);
3669 d
= alloca (sizeof(double));
3671 /* x86-64 had a bug were the next call to get100 would evict
3672 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
3673 in int type, not pointer type. When alloca returns a pointer
3674 with the high 32 bit set (which is likely on x86-64) the access
3675 generates a segfault. */
3676 i
= d
[0] > get100 ();
3682 void bfa3(ptrdiff_t str_offset
)
3684 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset
);
3686 void bfa2(ptrdiff_t str_offset
)
3688 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset
);
3691 void bfa1(ptrdiff_t str_offset
)
3693 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset
);
3697 void builtin_frame_address_test(void)
3699 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
3701 char str
[] = "__builtin_frame_address";
3702 char *fp0
= __builtin_frame_address(0);
3704 printf("str: %s\n", str
);
3709 char via_volatile (char i
)
3716 struct __attribute__((__packed__
)) Spacked
{
3721 struct Spacked spacked
;
3722 typedef struct __attribute__((__packed__
)) {
3728 typedef struct Spacked3_s
{
3732 } __attribute__((__packed__
)) Spacked3
;
3734 struct gate_struct64
{
3735 unsigned short offset_low
;
3736 unsigned short segment
;
3737 unsigned ist
: 3, zero0
: 5, type
: 5, dpl
: 2, p
: 1;
3738 unsigned short offset_middle
;
3739 unsigned offset_high
;
3741 } __attribute__((packed
));
3742 typedef struct gate_struct64 gate_desc
;
3743 gate_desc a_gate_desc
;
3744 void attrib_test(void)
3747 printf("attr: %d %d %d %d\n", sizeof(struct Spacked
),
3748 sizeof(spacked
), sizeof(Spacked2
), sizeof(spacked2
));
3749 printf("attr: %d %d\n", sizeof(Spacked3
), sizeof(spacked3
));
3750 printf("attr: %d %d\n", sizeof(gate_desc
), sizeof(a_gate_desc
));
3753 extern __attribute__((__unused__
)) char * __attribute__((__unused__
)) *
3754 strange_attrib_placement (void);
3756 void * __attribute__((__unused__
)) get_void_ptr (void *a
)
3761 /* This part checks for a bug in TOK_GET (used for inline expansion),
3762 where the large long long constant left the the high bits set for
3763 the integer constant token. */
3765 int __get_order(unsigned long long size
)
3768 size
-= 0xffff880000000000ULL
; // this const left high bits set in the token
3770 struct S
{ int i
: 1; } s
; // constructed for this '1'
3776 /* This just forces the above inline function to be actually emitted. */
3777 int force_get_order(unsigned long s
)
3779 return __get_order(s
);