2 * TCC auto test program
6 /* Unfortunately, gcc version < 3 does not handle that! */
9 /* only gcc 3 handles _Bool correctly */
12 /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
13 #define CORRECT_CR_HANDLING
15 /* __VA_ARGS__ and __func__ support */
18 /* deprecated and no longer supported in gcc 3.3 */
19 //#define ACCEPT_CR_IN_STRINGS
22 typedef __SIZE_TYPE__
uintptr_t;
26 #define LONG_LONG_FORMAT "%lld"
27 #define ULONG_LONG_FORMAT "%llu"
29 #define LONG_LONG_FORMAT "%Ld"
30 #define ULONG_LONG_FORMAT "%Lu"
33 // MinGW has 80-bit rather than 64-bit long double which isn't compatible with TCC or MSVC
34 #if defined(_WIN32) && defined(__GNUC__)
35 #define LONG_DOUBLE double
36 #define LONG_DOUBLE_LITERAL(x) x
38 #define LONG_DOUBLE long double
39 #define LONG_DOUBLE_LITERAL(x) x ## L
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
68 /* clang's preprocessor is broken in this regard and adds spaces
69 to the tokens 'incdir' and 'funnyname' when expanding */
70 #define incname <tests/42test.h>
72 #define incname < incdir funnyname >
74 #define __stringify(x) #x
75 #define stringify(x) __stringify(x)
78 #include stringify(funnyname)
84 void recursive_macro_test();
101 void constant_expr_test();
102 void expr_cmp_test();
103 void char_short_test();
104 void init_test(void);
105 void compound_literal_test(void);
107 void struct_assign_test(void);
108 void cast_test(void);
109 void bitfield_test(void);
110 void c99_bool_test(void);
111 void float_test(void);
112 void longlong_test(void);
113 void manyarg_test(void);
114 void stdarg_test(void);
115 void whitespace_test(void);
116 void relocation_test(void);
117 void old_style_function(void);
118 void alloca_test(void);
119 void c99_vla_test(int size1
, int size2
);
120 void sizeof_test(void);
121 void typeof_test(void);
122 void local_label_test(void);
123 void statement_expr_test(void);
125 void builtin_test(void);
126 void weak_test(void);
127 void global_data_test(void);
128 void cmp_comparison_test(void);
129 void math_cmp_test(void);
130 void callsave_test(void);
131 void builtin_frame_address_test(void);
132 void attrib_test(void);
133 void bounds_check1_test(void);
137 void forward_ref(void);
140 /* Line joining happens before tokenization, so the following
141 must be parsed as ellipsis. */
142 void funny_line_continuation (int, ..\
145 char via_volatile (char);
150 #define M1(a, b) (a) + (b)
154 #define glue(a, b) a ## b
155 #define xglue(a, b) glue(a, b)
156 #define HIGHLOW "hello"
157 #define LOW LOW ", world"
159 static int onetwothree
= 123;
160 #define onetwothree4 onetwothree
161 #define onetwothree xglue(onetwothree,4)
163 #define min(a, b) ((a) < (b) ? (a) : (b))
166 #define dprintf(level,...) printf(__VA_ARGS__)
169 /* gcc vararg macros */
170 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
172 #define MACRO_NOARGS()
189 /* We try to handle this syntax. Make at least sure it doesn't segfault. */
190 char invalid_function_def()[] {return 0;}
193 #define __INT64_C(c) c ## LL
194 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
202 #define spin_lock(lock) do { } while (0)
203 #define wq_spin_lock spin_lock
204 #define TEST2() wq_spin_lock(a)
206 #define UINT_MAX ((unsigned) -1)
208 void intdiv_test(void)
210 printf("18/21=%u\n", 18/21);
211 printf("18%%21=%u\n", 18%21);
212 printf("41/21=%u\n", 41/21);
213 printf("41%%21=%u\n", 41%21);
214 printf("42/21=%u\n", 42/21);
215 printf("42%%21=%u\n", 42%21);
216 printf("43/21=%u\n", 43/21);
217 printf("43%%21=%u\n", 43%21);
218 printf("126/21=%u\n", 126/21);
219 printf("126%%21=%u\n", 126%21);
220 printf("131/21=%u\n", 131/21);
221 printf("131%%21=%u\n", 131%21);
222 printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX
/2+3)/2);
223 printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX
/2+3)%2);
225 printf("18/-21=%u\n", 18/-21);
226 printf("18%%-21=%u\n", 18%-21);
227 printf("41/-21=%u\n", 41/-21);
228 printf("41%%-21=%u\n", 41%-21);
229 printf("42/-21=%u\n", 42/-21);
230 printf("42%%-21=%u\n", 42%-21);
231 printf("43/-21=%u\n", 43/-21);
232 printf("43%%-21=%u\n", 43%-21);
233 printf("126/-21=%u\n", 126/-21);
234 printf("126%%-21=%u\n", 126%-21);
235 printf("131/-21=%u\n", 131/-21);
236 printf("131%%-21=%u\n", 131%-21);
237 printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX
/2+3)/-2);
238 printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX
/2+3)%-2);
240 printf("-18/21=%u\n", -18/21);
241 printf("-18%%21=%u\n", -18%21);
242 printf("-41/21=%u\n", -41/21);
243 printf("-41%%21=%u\n", -41%21);
244 printf("-42/21=%u\n", -42/21);
245 printf("-42%%21=%u\n", -42%21);
246 printf("-43/21=%u\n", -43/21);
247 printf("-43%%21=%u\n", -43%21);
248 printf("-126/21=%u\n", -126/21);
249 printf("-126%%21=%u\n", -126%21);
250 printf("-131/21=%u\n", -131/21);
251 printf("-131%%21=%u\n", -131%21);
252 printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX
/2+3))/2);
253 printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX
/2+3))%2);
255 printf("-18/-21=%u\n", -18/-21);
256 printf("-18%%-21=%u\n", -18%-21);
257 printf("-41/-21=%u\n", -41/-21);
258 printf("-41%%-21=%u\n", -41%-21);
259 printf("-42/-21=%u\n", -42/-21);
260 printf("-42%%-21=%u\n", -42%-21);
261 printf("-43/-21=%u\n", -43/-21);
262 printf("-43%%-21=%u\n", -43%-21);
263 printf("-126/-21=%u\n", -126/-21);
264 printf("-126%%-21=%u\n", -126%-21);
265 printf("-131/-21=%u\n", -131/-21);
266 printf("-131%%-21=%u\n", -131%-21);
267 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX
/2+3))/-2);
268 printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX
/2+3))%-2);
271 void macro_test(void)
273 printf("macro:\n");\f\v
275 printf("aaa=%d\n", AAA
);
277 printf("min=%d\n", min(1, min(2, -1)));
279 printf("s1=%s\n", glue(HIGH
, LOW
));
280 printf("s2=%s\n", xglue(HIGH
, LOW
));
281 printf("s3=%s\n", str("c"));
282 printf("s4=%s\n", str(a1
));
283 printf("B3=%d\n", B3
);
285 printf("onetwothree=%d\n", onetwothree
);
288 printf("A defined\n");
291 printf("B defined\n");
294 printf("A defined\n");
296 printf("A not defined\n");
299 printf("B defined\n");
301 printf("B not defined\n");
305 printf("A defined\n");
307 printf("B1 defined\n");
309 printf("B1 not defined\n");
312 printf("A not defined\n");
314 printf("B2 defined\n");
316 printf("B2 not defined\n");
321 printf("test true1\n");
324 printf("test true2\n");
327 printf("test true3\n");
330 printf("test trueA\n");
333 printf("test trueB\n");
349 printf("__LINE__ defined\n");
352 printf("__LINE__=%d __FILE__=%s\n",
356 printf("__LINE__=%d __FILE__=%s\n",
359 printf("__LINE__=%d __FILE__=%s\n",
361 #line 227 "tcctest.c"
364 /* not strictly preprocessor, but we test it there */
366 printf("__func__ = %s\n", __func__
);
367 dprintf(1, "vaarg=%d\n", 1);
369 dprintf1(1, "vaarg1\n");
370 dprintf1(1, "vaarg1=%d\n", 2);
371 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
374 printf("func='%s'\n", __FUNCTION__
);
376 /* complicated macros in glibc */
377 printf("INT64_MIN=" LONG_LONG_FORMAT
"\n", INT64_MIN
);
387 /* macro function with argument outside the macro string */
388 #define MF_s MF_hello
389 #define MF_hello(msg) printf("%s\n",msg)
391 #define MF_t printf("tralala\n"); MF_hello
396 /* test macro substitution inside args (should not eat stream) */
397 printf("qq=%d\n", qq(qq
)(2));
399 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
400 null argument without a space. gcc 3.2 fixes that. */
403 printf("qq1=%d\n", qq1( ));
405 /* comment with stray handling *\
407 /* this is a valid *\/ comment */
408 /* this is a valid comment *\*/
412 /* test function macro substitution when the function name is
416 /* And again when the name and parentheses are separated by a
418 TEST2
/* the comment */ ();
420 printf("basefromheader %s\n", get_basefile_from_header());
421 printf("base %s\n", __BASE_FILE__
);
423 /* Some compilers (clang) prepend './' to __FILE__ from included
425 const char *fn
= get_file_from_header();
426 if (fn
[0] == '.' && fn
[1] == '/')
428 printf("filefromheader %s\n", fn
);
430 printf("file %s\n", __FILE__
);
432 /* Check that funnily named include was in fact included */
433 have_included_42test_h
= 1;
434 have_included_42test_h_second
= 1;
435 have_included_42test_h_third
= 1;
437 /* Check that we don't complain about stray \ here */
438 printf("print a backslash: %s\n", stringify(\\));
442 static void print_num(char *fn
, int line
, int num
) {
443 printf("fn %s, line %d, num %d\n", fn
, line
, num
);
446 void recursive_macro_test(void)
449 #define ELF32_ST_TYPE(val) ((val) & 0xf)
450 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
451 #define STB_WEAK 2 /* Weak symbol */
452 #define ELFW(type) ELF##32##_##type
453 printf("%d\n", ELFW(ST_INFO
)(STB_WEAK
, ELFW(ST_TYPE
)(123)));
457 #define print_num(x) print_num(__FILE__,__LINE__,x)
459 WRAP(print_num(123));
460 WRAP(WRAP(print_num(123)));
462 static struct recursive_macro
{ int rm_field
; } G
;
463 #define rm_field (G.rm_field)
464 printf("rm_field = %d\n", rm_field
);
465 printf("rm_field = %d\n", WRAP(rm_field
));
466 WRAP((printf("rm_field = %d %d\n", rm_field
, WRAP(rm_field
))));
483 #if !defined(__TINYC__) && (__GNUC__ >= 8)
484 /* Old GCCs don't regard "foo"[1] as constant, even in GNU dialect. */
485 #define CONSTANTINDEXEDSTRLIT
487 char str_ag1
[] = "b";
488 char str_ag2
[] = { "b" };
489 /*char str_bg1[] = ("cccc"); GCC accepts this with pedantic warning, TCC not */
490 #ifdef CONSTANTINDEXEDSTRLIT
491 char str_ag3
[] = { "ab"[1], 0 };
492 char str_x
[2] = { "xy" "z"[2], 0 };
494 char *str_ar
[] = { "one", "two" };
495 struct str_SS
{unsigned char a
[3], b
; };
496 struct str_SS str_sinit15
= { "r" };
497 struct str_SS str_sinit16
[] = { { "q" }, 2 };
499 static void string_test2()
502 char a3
[2] = { "p" };
503 char a4
[2] = { "ab" "c"[2], 0 };
504 char *pa1
= "def" + 1;
505 char *pa2
= { "xyz" + 1 };
507 struct str_SS ss
= { { [0 ... 1] = 'a' }, 0 };
508 #ifndef CONSTANTINDEXEDSTRLIT
509 char str_ag3
[] = { "ab"[1], 0 };
510 char str_x
[2] = { "xy" "z"[2], 0 };
512 puts("string_test2");
519 puts(str_sinit16
[0].a
);
524 printf("%s\n", "bla");
528 puts(i
>= 0 ? "one" : "two");
533 void ps(const char *s
)
545 const char foo1_string
[] = "\
554 printf("\141\1423\143\n");/* dezdez test */
555 printf("\x41\x42\x43\x3a\n");
556 printf("c=%c\n", 'r');
557 printf("wc=%C 0x%lx %C\n", L
'a', L
'\x1234', L
'c');
558 printf("foo1_string='%s'\n", foo1_string
);
560 printf("wstring=%S\n", L
"abc");
561 printf("wstring=%S\n", L
"abc" L
"def" "ghi");
562 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
563 printf("L'\\377'=%d L'\\xff'=%d\n", L
'\377', L
'\xff');
567 while ((b
= b
+ 1) < 96) {
571 printf("fib=%d\n", fib(33));
573 while (b
!= 0x80000000) {
581 void if1t(int n
, int a
, int b
, int c
)
583 if (a
&& b
) printf("if1t: %d 1 %d %d\n", n
, a
, b
);
584 if (a
&& !b
) printf("if1t: %d 2 %d %d\n", n
, a
, b
);
585 if (!a
&& b
) printf("if1t: %d 3 %d %d\n", n
, a
, b
);
586 if (!a
&& !b
) printf("if1t: %d 4 %d %d\n", n
, a
, b
);
587 if (a
|| b
) printf("if1t: %d 5 %d %d\n", n
, a
, b
);
588 if (a
|| !b
) printf("if1t: %d 6 %d %d\n", n
, a
, b
);
589 if (!a
|| b
) printf("if1t: %d 7 %d %d\n", n
, a
, b
);
590 if (!a
|| !b
) printf("if1t: %d 8 %d %d\n", n
, a
, b
);
591 if (a
&& b
|| c
) printf("if1t: %d 9 %d %d %d\n", n
, a
, b
, c
);
592 if (a
|| b
&& c
) printf("if1t: %d 10 %d %d %d\n", n
, a
, b
, c
);
593 if (a
> b
- 1 && c
) printf("if1t: %d 11 %d %d %d\n", n
, a
, b
, c
);
594 if (a
> b
- 1 || c
) printf("if1t: %d 12 %d %d %d\n", n
, a
, b
, c
);
595 if (a
> 0 && 1) printf("if1t: %d 13 %d %d %d\n", n
, a
, b
, c
);
596 if (a
> 0 || 0) printf("if1t: %d 14 %d %d %d\n", n
, a
, b
, c
);
601 if (0 && 1 || printf("if2t:ok\n") || 1)
602 printf("if2t:ok2\n");
603 printf("if2t:ok3\n");
622 for(i
= 0; i
< 10;i
++)
632 /* c99 for loop init test */
633 for (size_t count
= 1; count
< 3; count
++)
634 printf("count=%d\n", count
);
635 printf("count = %d\n", count
);
637 /* break/continue tests */
649 /* break/continue tests */
661 for(i
= 0;i
< 10;i
++) {
669 typedef int typedef_and_label
;
674 static void *label_table
[3] = { &&label1
, &&label2
, &&label3
};
678 /* This needs to parse as label, not as start of decl. */
690 /* we also test computed gotos (GCC extension) */
692 goto *label_table
[i
];
718 enum {E6
= 42, E7
, E8
} e
:8;
722 /* This is either 0 on L32 machines, or a large number
723 on L64 machines. We should be able to store this. */
724 EL_large
= ((unsigned long)0xf000 << 31) << 1,
727 enum { BIASU
= -1U<<31 };
728 enum { BIASS
= -1 << 31 };
730 static int getint(int i
)
735 return (int)(-1U << 31);
741 /* The following should give no warning */
743 struct S_enum s
= {E7
};
744 printf("enum: %d\n", s
.e
);
745 printf("enum:\n%d %d %d %d %d %d\n",
746 E0
, E1
, E2
, E3
, E4
, E5
);
748 printf("b1=%d\n", b1
);
749 printf("enum large: %ld\n", EL_large
);
751 if (getint(0) == BIASU
)
752 printf("enum unsigned: ok\n");
754 printf("enum unsigned: wrong\n");
755 if (getint(0) == BIASS
)
756 printf("enum unsigned: ok\n");
758 printf("enum unsigned: wrong\n");
774 printf("typedef:\n");
775 printf("a=%d\n", *a
);
777 printf("mytype2=%d\n", mytype2
);
782 printf("forward:\n");
788 void forward_ref(void)
790 printf("forward ok\n");
793 typedef struct struct1
{
813 struct struct1 st1
, st2
;
820 int main(int argc
, char **argv
)
825 recursive_macro_test();
842 constant_expr_test();
846 compound_literal_test();
848 struct_assign_test();
858 old_style_function();
863 statement_expr_test();
871 cmp_comparison_test();
874 builtin_frame_address_test();
876 if (via_volatile (42) != 42)
877 printf ("via_volatile broken\n");
879 bounds_check1_test();
890 printf("g1=%d\n", g
);
898 printf("g2=%d\n", g
);
902 printf("g3=%d\n", g
);
906 printf("g4=%d\n", g
);
909 printf("g5=%d\n", g
);
918 for (int st2_i
= 1; st2_i
< 10; st2_i
++) {
921 printf("exloc: %d\n", st2_i
);
923 printf("exloc: %d\n", *st2_p
);
926 /* C has tentative definition, and they may be repeated. */
927 extern int st_global1
;
929 extern int st_global1
;
931 extern int st_global2
;
933 extern int st_global2
;
941 printf("sizeof(a) = %d\n", sizeof(a
));
942 printf("sizeof(\"a\") = %d\n", sizeof("a"));
944 printf("sizeof(__func__) = %d\n", sizeof(__func__
));
946 printf("sizeof tab %d\n", sizeof(tab
));
947 printf("sizeof tab2 %d\n", sizeof tab2
);
951 printf("%d %d %d\n", tab
[0], tab
[1], tab
[2]);
954 tab2
[i
][j
] = 10 * i
+ j
;
956 printf(" %3d", ((int *)tab2
)[i
]);
959 printf("sizeof(size_t)=%d\n", sizeof(size_t));
960 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
967 printf("%d\n", a
+= 1);
968 printf("%d\n", a
-= 2);
969 printf("%d\n", a
*= 31232132);
970 printf("%d\n", a
/= 4);
971 printf("%d\n", a
%= 20);
972 printf("%d\n", a
&= 6);
973 printf("%d\n", a
^= 7);
974 printf("%d\n", a
|= 8);
975 printf("%d\n", a
>>= 3);
976 printf("%d\n", a
<<= 4);
980 printf("%d\n", a
+ 1);
981 printf("%d\n", a
- 2);
982 printf("%d\n", a
* 312);
983 printf("%d\n", a
/ 4);
984 printf("%d\n", b
/ 4);
985 printf("%d\n", (unsigned)b
/ 4);
986 printf("%d\n", a
% 20);
987 printf("%d\n", b
% 20);
988 printf("%d\n", (unsigned)b
% 20);
989 printf("%d\n", a
& 6);
990 printf("%d\n", a
^ 7);
991 printf("%d\n", a
| 8);
992 printf("%d\n", a
>> 3);
993 printf("%d\n", b
>> 3);
994 printf("%d\n", (unsigned)b
>> 3);
995 printf("%d\n", a
<< 4);
1000 printf("%d\n", 12 + 1);
1001 printf("%d\n", 12 - 2);
1002 printf("%d\n", 12 * 312);
1003 printf("%d\n", 12 / 4);
1004 printf("%d\n", 12 % 20);
1005 printf("%d\n", 12 & 6);
1006 printf("%d\n", 12 ^ 7);
1007 printf("%d\n", 12 | 8);
1008 printf("%d\n", 12 >> 2);
1009 printf("%d\n", 12 << 4);
1010 printf("%d\n", ~12);
1011 printf("%d\n", -12);
1012 printf("%d\n", +12);
1013 printf("%d %d %d %d\n",
1022 return (c
>= 'a' & c
<= 'z') | (c
>= 'A' & c
<= 'Z') | c
== '_';
1025 /**********************/
1027 int vstack
[10], *vstack_ptr
;
1029 void vpush(int vt
, int vc
)
1035 void vpop(int *ft
, int *fc
)
1037 *fc
= *--vstack_ptr
;
1038 *ft
= *--vstack_ptr
;
1046 vstack_ptr
= vstack
;
1048 vstack_ptr
[-2] &= ~0xffffff80;
1050 printf("res= %d %d\n", a
, b
);
1053 int const_len_ar
[sizeof(1/0)]; /* div-by-zero, but in unevaluated context */
1055 void constant_expr_test()
1058 printf("constant_expr:\n");
1060 printf("%d\n", a
* 16);
1061 printf("%d\n", a
* 1);
1062 printf("%d\n", a
+ 0);
1063 printf("%d\n", sizeof(const_len_ar
));
1068 void expr_ptr_test()
1073 printf("expr_ptr:\n");
1076 printf("diff=%d\n", q
- p
);
1078 printf("inc=%d\n", p
- tab4
);
1080 printf("dec=%d\n", p
- tab4
);
1082 printf("inc=%d\n", p
- tab4
);
1084 printf("dec=%d\n", p
- tab4
);
1085 printf("add=%d\n", p
+ 3 - tab4
);
1086 printf("add=%d\n", 3 + p
- tab4
);
1088 /* check if 64bit support is ok */
1091 printf("%p %p %ld\n", q
, p
, p
-q
);
1092 printf("%d %d %d %d %d %d\n",
1093 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
1096 printf("%p %p %ld\n", q
, p
, p
-q
);
1097 printf("%d %d %d %d %d %d\n",
1098 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
1099 p
= (int *)((char *)p
+ 0xf0000000);
1100 printf("%p %p %ld\n", q
, p
, p
-q
);
1101 printf("%d %d %d %d %d %d\n",
1102 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
1104 printf("%p %p %ld\n", q
, p
, p
-q
);
1105 printf("%d %d %d %d %d %d\n",
1106 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
1111 struct size12 s
[2], *sp
= s
;
1116 printf("%d\n", sp
[j
].i
);
1120 p
= (int*)0x100000000UL
+ i
;
1121 i
= ((long)p
) >> 32;
1122 printf("largeptr: %p %d\n", p
, i
);
1126 void expr_cmp_test()
1129 printf("constant_expr:\n");
1132 printf("%d\n", a
== a
);
1133 printf("%d\n", a
!= a
);
1135 printf("%d\n", a
< b
);
1136 printf("%d\n", a
<= b
);
1137 printf("%d\n", a
<= a
);
1138 printf("%d\n", b
>= a
);
1139 printf("%d\n", a
>= a
);
1140 printf("%d\n", b
> a
);
1142 printf("%d\n", (unsigned)a
< b
);
1143 printf("%d\n", (unsigned)a
<= b
);
1144 printf("%d\n", (unsigned)a
<= a
);
1145 printf("%d\n", (unsigned)b
>= a
);
1146 printf("%d\n", (unsigned)a
>= a
);
1147 printf("%d\n", (unsigned)b
> a
);
1170 struct __attribute__((aligned(16))) aligntest5
1177 } __attribute__((aligned(16)));
1182 struct aligntest5 altest5
[2];
1183 struct aligntest6 altest6
[2];
1185 /* altest7 is correctly aligned to 16 bytes also with TCC,
1186 but __alignof__ returns the wrong result (4) because we
1187 can't store the alignment yet when specified on symbols
1188 directly (it's stored in the type so we'd need to make
1189 a copy of it). -- FIXED */
1190 struct aligntest7 altest7
[2] __attribute__((aligned(16)));
1195 } __attribute__((aligned(4096)));
1198 unsigned long flags
;
1206 unsigned long index
;
1210 unsigned long counters
;
1224 unsigned long compound_head
;
1225 unsigned int compound_dtor
;
1226 unsigned int compound_order
;
1229 } __attribute__((aligned(2 * sizeof(long))));
1231 typedef unsigned long long __attribute__((aligned(4))) unaligned_u64
;
1234 unsigned int buf_nr
;
1235 unaligned_u64 start_lba
;
1238 struct aligntest10
{
1239 unsigned int buf_nr
;
1240 unsigned long long start_lba
;
1249 printf("struct:\n");
1250 printf("sizes: %d %d %d %d\n",
1251 sizeof(struct struct1
),
1252 sizeof(struct struct2
),
1253 sizeof(union union1
),
1254 sizeof(union union2
));
1255 printf("offsets: %d\n", (int)((char*)&st1
.u
.v1
- (char*)&st1
));
1259 printf("st1: %d %d %d\n",
1260 st1
.f1
, st1
.f2
, st1
.f3
);
1263 printf("union1: %d\n", st1
.u
.v1
);
1266 printf("union2: %d\n", u
.w1
);
1271 printf("st2: %d %d %d\n",
1272 s
->f1
, s
->f2
, s
->f3
);
1273 printf("str_addr=%x\n", (int)(uintptr_t)st1
.str
- (int)(uintptr_t)&st1
.f1
);
1275 /* align / size tests */
1276 printf("aligntest1 sizeof=%d alignof=%d\n",
1277 sizeof(struct aligntest1
), __alignof__(struct aligntest1
));
1278 printf("aligntest2 sizeof=%d alignof=%d\n",
1279 sizeof(struct aligntest2
), __alignof__(struct aligntest2
));
1280 printf("aligntest3 sizeof=%d alignof=%d\n",
1281 sizeof(struct aligntest3
), __alignof__(struct aligntest3
));
1282 printf("aligntest4 sizeof=%d alignof=%d\n",
1283 sizeof(struct aligntest4
), __alignof__(struct aligntest4
));
1284 printf("aligntest5 sizeof=%d alignof=%d\n",
1285 sizeof(struct aligntest5
), __alignof__(struct aligntest5
));
1286 printf("aligntest6 sizeof=%d alignof=%d\n",
1287 sizeof(struct aligntest6
), __alignof__(struct aligntest6
));
1288 printf("aligntest7 sizeof=%d alignof=%d\n",
1289 sizeof(struct aligntest7
), __alignof__(struct aligntest7
));
1290 printf("aligntest8 sizeof=%d alignof=%d\n",
1291 sizeof(struct aligntest8
), __alignof__(struct aligntest8
));
1292 printf("aligntest9 sizeof=%d alignof=%d\n",
1293 sizeof(struct aligntest9
), __alignof__(struct aligntest9
));
1294 printf("aligntest10 sizeof=%d alignof=%d\n",
1295 sizeof(struct aligntest10
), __alignof__(struct aligntest10
));
1296 printf("altest5 sizeof=%d alignof=%d\n",
1297 sizeof(altest5
), __alignof__(altest5
));
1298 printf("altest6 sizeof=%d alignof=%d\n",
1299 sizeof(altest6
), __alignof__(altest6
));
1300 printf("altest7 sizeof=%d alignof=%d\n",
1301 sizeof(altest7
), __alignof__(altest7
));
1303 /* empty structures (GCC extension) */
1304 printf("sizeof(struct empty) = %d\n", sizeof(struct empty
));
1305 printf("alignof(struct empty) = %d\n", __alignof__(struct empty
));
1307 printf("Large: sizeof=%d\n", sizeof(ls
));
1308 memset(&ls
, 0, sizeof(ls
));
1309 ls
.compound_head
= 42;
1310 printf("Large: offsetof(compound_head)=%d\n", (int)((char*)&ls
.compound_head
- (char*)&ls
));
1313 /* simulate char/short return value with undefined upper bits */
1314 static int __csf(int x
) { return x
; }
1315 static void *_csf
= __csf
;
1316 #define csf(t,n) ((t(*)(int))_csf)(n)
1318 /* XXX: depend on endianness */
1319 void char_short_test()
1325 printf("char_short:\n");
1329 printf("s8=%d %d\n",
1330 *(signed char *)&var1
, *(signed char *)&var2
);
1331 printf("u8=%d %d\n",
1332 *(unsigned char *)&var1
, *(unsigned char *)&var2
);
1333 printf("s16=%d %d\n",
1334 *(short *)&var1
, *(short *)&var2
);
1335 printf("u16=%d %d\n",
1336 *(unsigned short *)&var1
, *(unsigned short *)&var2
);
1337 printf("s32=%d %d\n",
1338 *(int *)&var1
, *(int *)&var2
);
1339 printf("u32=%d %d\n",
1340 *(unsigned int *)&var1
, *(unsigned int *)&var2
);
1341 *(signed char *)&var1
= 0x08;
1342 printf("var1=%x\n", var1
);
1343 *(short *)&var1
= 0x0809;
1344 printf("var1=%x\n", var1
);
1345 *(int *)&var1
= 0x08090a0b;
1346 printf("var1=%x\n", var1
);
1349 var4
= 0x11223344aa998877ULL
;
1350 var1
= var3
= var1
+ 1;
1351 var4
= var3
= var4
+ 1;
1352 printf("promote char/short assign %d "LONG_LONG_FORMAT
"\n", var1
, var4
);
1354 var4
= 0x11223344aa998877ULL
;
1355 printf("promote char/short assign VA %d %d\n", var3
= var1
+ 1, var3
= var4
+ 1);
1356 printf("promote char/short cast VA %d %d\n", (signed char)(var1
+ 1), (signed char)(var4
+ 1));
1357 #if !defined(__arm__)
1358 /* We can't really express GCC behaviour of return type promotion in
1359 the presence of undefined behaviour (like __csf is). */
1360 var1
= csf(unsigned char,0x89898989);
1361 var4
= csf(signed char,0xabababab);
1362 printf("promote char/short funcret %d "LONG_LONG_FORMAT
"\n", var1
, var4
);
1363 printf("promote char/short fumcret VA %d %d %d %d\n",
1364 csf(unsigned short,0xcdcdcdcd),
1365 csf(short,0xefefefef),
1366 csf(_Bool
,0x33221100),
1367 csf(_Bool
,0x33221101));
1370 var1
= (signed char)(unsigned char)(var3
+ 1);
1371 var4
= (signed char)(unsigned char)(var3
+ 1);
1372 printf("promote multicast (char)(unsigned char) %d "LONG_LONG_FORMAT
"\n", var1
, var4
);
1373 var4
= 0x11223344aa998877ULL
;
1374 var4
= (unsigned)(int)(var4
+ 1);
1375 printf("promote multicast (unsigned)(int) "LONG_LONG_FORMAT
"\n", var4
);
1376 var4
= 0x11223344bbaa9988ULL
;
1377 var4
= (unsigned)(signed char)(var4
+ 1);
1378 printf("promote multicast (unsigned)(char) "LONG_LONG_FORMAT
"\n", var4
);
1381 /******************/
1383 typedef struct Sym
{
1391 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1392 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1394 static int toupper1(int a
)
1399 static unsigned int calc_vm_flags(unsigned int prot
)
1401 unsigned int prot_bits
;
1402 /* This used to segfault in some revisions: */
1403 prot_bits
= ((0x1==0x00000001)?(prot
&0x1):(prot
&0x1)?0x00000001:0);
1409 int *s
, a
, b
, t
, f
, i
;
1413 printf("!s=%d\n", !s
);
1417 printf("a=%d\n", a
);
1419 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1420 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1421 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1436 printf("b=%d\n", a
+ (0 ? 1 : a
/ 2));
1438 /* test register spilling */
1441 a
= (a
+ b
) * ((a
< b
) ?
1442 ((b
- a
) * (a
- b
)): a
+ b
);
1443 printf("a=%d\n", a
);
1445 /* test complex || or && expressions */
1449 printf("exp=%d\n", f
== (32 <= a
&& a
<= 3));
1450 printf("r=%d\n", (t
|| f
) + (t
&& f
));
1455 int aspect_native
= 65536;
1456 double bfu_aspect
= 1.0;
1458 for(aspect_on
= 0; aspect_on
< 2; aspect_on
++) {
1459 aspect
=aspect_on
?(aspect_native
*bfu_aspect
+0.5):65535UL;
1460 printf("aspect=%d\n", aspect
);
1464 /* test ? : GCC extension */
1466 static int v1
= 34 ? : -1; /* constant case */
1467 static int v2
= 0 ? : -1; /* constant case */
1470 printf("%d %d\n", v1
, v2
);
1471 printf("%d %d\n", a
- 30 ? : a
* 2, a
+ 1 ? : a
* 2);
1474 /* again complex expression */
1475 for(i
=0;i
<256;i
++) {
1476 if (toupper1 (i
) != TOUPPER (i
))
1477 printf("error %d\n", i
);
1479 printf ("bits = 0x%x\n", calc_vm_flags (0x1));
1482 extern int undefined_function(void);
1483 extern int defined_function(void);
1485 static inline void refer_to_undefined(void)
1487 undefined_function();
1490 void optimize_out(void)
1492 int i
= 0 ? undefined_function() : defined_function();
1493 printf ("oo:%d\n", i
);
1494 int j
= 1 ? defined_function() : undefined_function();
1495 printf ("oo:%d\n", j
);
1497 printf("oo:%d\n", undefined_function());
1499 printf("oo:%d\n", defined_function());
1501 printf("oo:%d\n", defined_function());
1503 printf("oo:%d\n", undefined_function());
1505 printf("oow:%d\n", defined_function());
1507 printf("oow:%d\n", undefined_function());
1510 /* Following is a switch without {} block intentionally. */
1513 printf ("oos:%d\n", defined_function());
1514 /* The following break shouldn't lead to disabled code after
1518 printf ("ool1:%d\n", defined_function());
1519 /* Same for the other types of loops. */
1523 printf ("ool2:%d\n", defined_function());
1526 printf ("ool3:%d\n", defined_function());
1527 /* Normal {} blocks without controlling statements
1528 shouldn't reactivate code emission */
1533 printf ("ool4:%d\n", undefined_function());
1538 break; /* this break shouldn't disable code outside the if. */
1539 printf("ool5:%d\n", defined_function());
1549 printf("ool6:%d\n", defined_function());
1556 printf("ool7:%d\n", undefined_function());
1559 /* Test that constants in logical && are optimized: */
1560 i
= 0 && undefined_function();
1561 i
= defined_function() && 0 && undefined_function();
1562 if (0 && undefined_function())
1563 undefined_function();
1564 if (defined_function() && 0)
1565 undefined_function();
1567 undefined_function();
1568 if (defined_function() && 0 && undefined_function())
1569 undefined_function();
1570 /* The same for || : */
1571 i
= 1 || undefined_function();
1572 i
= defined_function() || 1 || undefined_function();
1573 if (1 || undefined_function())
1576 undefined_function();
1577 if (defined_function() || 1)
1580 undefined_function();
1584 undefined_function();
1585 if (defined_function() || 1 || undefined_function())
1588 undefined_function();
1590 if (defined_function() && 0)
1591 refer_to_undefined();
1598 undefined_function();
1601 /* Leave the "if(1)return; printf()" in this order and last in the function */
1604 printf ("oor:%d\n", undefined_function());
1607 int defined_function(void)
1613 /* GCC accepts that */
1614 static int tab_reinit
[];
1615 static int tab_reinit
[10];
1617 static int tentative_ar
[];
1618 static int tentative_ar
[] = {1,2,3};
1620 //int cinit1; /* a global variable can be defined several times without error ! */
1624 int *cinit2
= (int []){3, 2, 1};
1626 void compound_literal_test(void)
1631 printf("compound_test:\n");
1633 p
= (int []){1, 2, 3};
1635 printf(" %d", p
[i
]);
1639 printf("%d", cinit2
[i
]);
1643 printf("q1=%s\n", q
);
1645 q
= (char *){ "tralala2" };
1646 printf("q2=%s\n", q
);
1649 printf("q3=%s\n", q3
);
1651 q
= (char []){ "tralala3" };
1652 printf("q4=%s\n", q
);
1655 p
= (int []){1, 2, cinit1
+ 3};
1657 printf(" %d", p
[i
]);
1661 p
= (int []){1, 2, 4 + i
};
1662 printf("%d %d %d\n",
1684 printf("kr_test:\n");
1685 printf("func1=%d\n", kr_func1(3, 4));
1686 printf("func2=%d\n", kr_func2(3, 4));
1693 tab
= (char*)malloc(20);
1710 /* structure assignment tests */
1716 struct structa1 ssta1
;
1718 void struct_assign_test1(struct structa1 s1
, int t
, float f
)
1720 printf("%d %d %d %f\n", s1
.f1
, s1
.f2
, t
, f
);
1723 struct structa1
struct_assign_test2(struct structa1 s1
, int t
)
1730 void struct_assign_test(void)
1733 struct structa1 lsta1
, lsta2
;
1740 printf("struct_assign_test:\n");
1744 printf("%d %d\n", s
.lsta1
.f1
, s
.lsta1
.f2
);
1746 printf("%d %d\n", s
.lsta2
.f1
, s
.lsta2
.f2
);
1751 struct_assign_test1(ps
->lsta2
, 3, 4.5);
1753 printf("before call: %d %d\n", s
.lsta2
.f1
, s
.lsta2
.f2
);
1754 ps
->lsta2
= struct_assign_test2(ps
->lsta2
, ps
->i
);
1755 printf("after call: %d %d\n", ps
->lsta2
.f1
, ps
->lsta2
.f2
);
1760 /* XXX: we should allow this even without braces */
1761 { struct_assign_test
}
1763 printf("%d\n", struct_assign_test
== t
[0].elem
);
1766 /* casts to short/char */
1768 void cast1(char a
, short b
, unsigned char c
, unsigned short d
)
1770 printf("%d %d %d %d\n", a
, b
, c
, d
);
1784 unsigned long ul
= 0x80000000UL
;
1785 p
-= 0x700000000042;
1787 printf("cast_test:\n");
1791 printf("%d %d %d %d\n",
1794 (unsigned char)(a
+ 1),
1795 (unsigned short)(a
+ 1));
1796 printf("%d %d %d %d\n",
1799 (unsigned char)0xfffff,
1800 (unsigned short)0xfffff);
1802 a
= (bcast
= 128) + 1;
1804 a
= (scast
= 65536) + 1;
1807 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
1809 /* test cast from unsigned to signed short to int */
1812 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b
, d
);
1815 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b
, d
);
1817 /* test implicit int casting for array accesses */
1821 printf("%d %d\n", tab
[0], tab
[1]);
1823 /* test implicit casting on some operators */
1824 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1825 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1826 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1828 /* from pointer to integer types */
1829 printf("%d %d %ld %ld %lld %lld\n",
1830 (int)p
, (unsigned int)p
,
1831 (long)p
, (unsigned long)p
,
1832 (long long)p
, (unsigned long long)p
);
1834 /* from integers to pointers */
1835 printf("%p %p %p %p\n",
1836 (void *)a
, (void *)b
, (void *)c
, (void *)d
);
1838 /* int to int with sign set */
1839 printf("0x%lx\n", (unsigned long)(int)ul
);
1842 /* initializers tests */
1843 struct structinit1
{
1852 int sinit3
[3] = { 1, 2, {{3}}, };
1853 int sinit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1854 int sinit5
[3][2] = { 1, 2, 3, 4, 5, 6 };
1855 int sinit6
[] = { 1, 2, 3 };
1856 int sinit7
[] = { [2] = 3, [0] = 1, 2 };
1857 char sinit8
[] = "hello" "trala";
1859 struct structinit1 sinit9
= { 1, 2, 3 };
1860 struct structinit1 sinit10
= { .f2
= 2, 3, .f1
= 1 };
1861 struct structinit1 sinit11
= { .f2
= 2, 3, .f1
= 1,
1869 char *sinit12
= "hello world";
1875 char sinit14
[10] = { "abc" };
1876 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
1878 struct { int a
[3], b
; } sinit16
[] = { { 1 }, 2 };
1894 struct complexinit0
{
1899 struct complexinit
{
1901 const struct complexinit0
*b
;
1904 const static struct complexinit cix
[] = {
1907 .b
= (const struct complexinit0
[]) {
1915 struct complexinit2
{
1920 struct complexinit2 cix20
;
1922 struct complexinit2 cix21
= {
1924 .b
= { 3001, 3002, 3003 }
1927 struct complexinit2 cix22
= {
1929 .b
= { 4001, 4002, 4003, 4004, 4005, 4006 }
1932 typedef int arrtype1
[];
1933 arrtype1 sinit19
= {1};
1934 arrtype1 sinit20
= {2,3};
1935 typedef int arrtype2
[3];
1936 arrtype2 sinit21
= {4};
1937 arrtype2 sinit22
= {5,6,7};
1939 /* Address comparisons of non-weak symbols with zero can be const-folded */
1940 int sinit23
[2] = { "astring" ? sizeof("astring") : -1,
1941 &sinit23
? 42 : -1 };
1943 int sinit24
= 2 || 1 / 0; /* exception in constant but unevaluated context */
1945 extern int external_inited
= 42;
1947 void init_test(void)
1951 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1952 int linit6
[] = { 1, 2, 3 };
1954 char linit8
[] = "hello" "trala";
1955 int linit12
[10] = { 1, 2 };
1956 int linit13
[10] = { 1, 2, [7] = 3, [3] = 4, };
1957 char linit14
[10] = "abc";
1958 int linit15
[10] = { linit1
, linit1
+ 1, [6] = linit1
+ 2, };
1959 struct linit16
{ int a1
, a2
, a3
, a4
; } linit16
= { 1, .a3
= 2 };
1960 int linit17
= sizeof(linit17
);
1962 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1963 int linit18
[2] = {&zero
? 1 : -1, zero
? -1 : 1 };
1965 printf("init_test:\n");
1967 printf("sinit1=%d\n", sinit1
);
1968 printf("sinit2=%d\n", sinit2
);
1969 printf("sinit3=%d %d %d %d\n",
1975 printf("sinit6=%d\n", sizeof(sinit6
));
1976 printf("sinit7=%d %d %d %d\n",
1982 printf("sinit8=%s\n", sinit8
);
1983 printf("sinit9=%d %d %d\n",
1988 printf("sinit10=%d %d %d\n",
1993 printf("sinit11=%d %d %d %d %d %d\n",
2004 printf("[%d][%d] = %d %d %d\n",
2005 i
, j
, sinit4
[i
][j
], sinit5
[i
][j
], linit4
[i
][j
]);
2006 printf("linit1=%d\n", linit1
);
2007 printf("linit2=%d\n", linit2
);
2008 printf("linit6=%d\n", sizeof(linit6
));
2009 printf("linit8=%d %s\n", sizeof(linit8
), linit8
);
2011 printf("sinit12=%s\n", sinit12
);
2012 printf("sinit13=%d %s %s %s\n",
2017 printf("sinit14=%s\n", sinit14
);
2019 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
2021 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
2023 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
2025 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
2027 printf("%d %d %d %d\n",
2032 /* test that initialisation is done after variable declare */
2033 printf("linit17=%d\n", linit17
);
2034 printf("sinit15=%d\n", sinit15
[0]);
2035 printf("sinit16=%d %d\n", sinit16
[0].a
[0], sinit16
[1].a
[0]);
2036 printf("sinit17=%s %d %s %d\n",
2037 sinit17
[0].s
, sinit17
[0].len
,
2038 sinit17
[1].s
, sinit17
[1].len
);
2040 printf("%x ", sinit18
[i
]);
2042 /* complex init check */
2043 printf("cix: %d %d %d %d %d %d %d\n",
2045 cix
[0].b
[0].a
, cix
[0].b
[0].b
,
2046 cix
[0].b
[1].a
, cix
[0].b
[1].b
,
2047 cix
[0].b
[2].a
, cix
[0].b
[2].b
);
2048 printf("cix2: %d %d\n", cix21
.b
[2], cix22
.b
[5]);
2049 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20
, sizeof cix21
, sizeof cix22
);
2051 printf("arrtype1: %d %d %d\n", sinit19
[0], sinit20
[0], sinit20
[1]);
2052 printf("arrtype2: %d %d\n", sizeof(sinit19
), sizeof(sinit20
));
2053 printf("arrtype3: %d %d %d\n", sinit21
[0], sinit21
[1], sinit21
[2]);
2054 printf("arrtype4: %d %d %d\n", sinit22
[0], sinit22
[1], sinit22
[2]);
2055 printf("arrtype5: %d %d\n", sizeof(sinit21
), sizeof(sinit22
));
2056 printf("arrtype6: %d\n", sizeof(arrtype2
));
2058 printf("sinit23= %d %d\n", sinit23
[0], sinit23
[1]);
2059 printf("sinit24=%d\n", sinit24
);
2060 printf("linit18= %d %d\n", linit18
[0], linit18
[1]);
2063 void switch_uc(unsigned char uc
)
2076 printf("ucsw: broken!\n");
2080 void switch_sc(signed char sc
)
2093 printf("scsw: broken!\n");
2100 unsigned long long ull
;
2125 for (i
= 1; i
<= 5; i
++) {
2126 ull
= (unsigned long long)i
<< 61;
2129 printf("ullsw:1\n");
2132 printf("ullsw:2\n");
2135 printf("ullsw:3\n");
2138 printf("ullsw:4\n");
2141 printf("ullsw:5\n");
2144 printf("ullsw: broken!\n");
2148 for (i
= 1; i
<= 5; i
++) {
2149 ll
= (long long)i
<< 61;
2167 printf("llsw: broken!\n");
2171 for (i
= -5; i
<= 5; i
++) {
2172 switch_uc((unsigned char)i
);
2175 for (i
= -5; i
<= 5; i
++) {
2176 switch_sc ((signed char)i
);
2180 /* ISOC99 _Bool type */
2181 void c99_bool_test(void)
2187 printf("bool_test:\n");
2188 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
2190 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
2192 printf("b = %d\n", b
);
2194 printf("b = %d\n", b
);
2196 printf("sizeof(x ? _Bool : _Bool) = %d (should be sizeof int)\n",
2197 sizeof((volatile)a
? b
: b2
));
2201 void bitfield_test(void)
2213 unsigned int f5
: 7;
2215 printf("bitfield_test:");
2216 printf("sizeof(st1) = %d\n", sizeof(st1
));
2225 printf("%d %d %d %d %d\n",
2226 st1
.f1
, st1
.f2
, st1
.f3
, st1
.f4
, st1
.f5
);
2229 printf("%d %d\n", sa
, ca
);
2233 printf("st1.f1 == -1\n");
2235 printf("st1.f1 != -1\n");
2237 printf("st1.f2 == -1\n");
2239 printf("st1.f2 != -1\n");
2245 unsigned long long f3
: 38;
2247 st2
.f1
= 0x123456789ULL
;
2249 st2
.f2
= (long long)a
<< 25;
2252 printf("%lld %lld %lld\n", st2
.f1
, st2
.f2
, st2
.f3
);
2255 Disabled
for now until further clarification re GCC compatibility
2264 printf("sizeof(st3) = %d\n", sizeof(st3
));
2272 printf("st4.y == %d\n", st4
.y
);
2276 int x
: 12, y
: 4, : 0, : 4, z
: 3;
2278 } st5
= { 1, 2, 3, 4, -3, 6 };
2279 printf("st5 = %d %d %d %d %d %d\n", st5
.a
, st5
.b
, st5
.x
, st5
.y
, st5
.z
, st5
.c
);
2282 unsigned char y
: 2;
2285 printf("st6.y == %d\n", st6
.y
);
2289 #define FLOAT_FMT "%f\n"
2291 /* x86's float isn't compatible with GCC */
2292 #define FLOAT_FMT "%.5f\n"
2295 /* declare strto* functions as they are C99 */
2296 double strtod(const char *nptr
, char **endptr
);
2299 float strtof(const char *nptr
, char **endptr
) {return (float)strtod(nptr
, endptr
);}
2300 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
) {return (LONG_DOUBLE
)strtod(nptr
, endptr
);}
2302 float strtof(const char *nptr
, char **endptr
);
2303 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
);
2306 #define FTEST(prefix, typename, type, fmt)\
2307 void prefix ## cmp(type a, type b)\
2309 printf("%d %d %d %d %d %d\n",\
2316 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
2324 printf(fmt "\n", ++a);\
2325 printf(fmt "\n", a++);\
2326 printf(fmt "\n", a);\
2328 printf("%d %d\n", !a, !b);\
2330 void prefix ## fcast(type a)\
2338 unsigned long long llua;\
2343 printf("ftof: %f %f %Lf\n", fa, da, la);\
2345 llia = (long long)a;\
2346 a = (a >= 0) ? a : -a;\
2347 ua = (unsigned int)a;\
2348 llua = (unsigned long long)a;\
2349 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
2352 llia = -0x123456789012345LL;\
2353 llua = 0xf123456789012345LLU;\
2355 printf("itof: " fmt "\n", b);\
2357 printf("utof: " fmt "\n", b);\
2359 printf("lltof: " fmt "\n", b);\
2361 printf("ulltof: " fmt "\n", b);\
2364 float prefix ## retf(type a) { return a; }\
2365 double prefix ## retd(type a) { return a; }\
2366 LONG_DOUBLE prefix ## retld(type a) { return a; }\
2368 void prefix ## call(void)\
2370 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
2371 printf("double: %f\n", prefix ## retd(42.123456789));\
2372 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
2373 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
2376 void prefix ## signed_zeros(void) \
2378 type x = 0.0, y = -0.0, n, p;\
2380 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
2381 1.0 / x != 1.0 / y);\
2383 printf ("x != y; this is wrong!\n");\
2387 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
2388 1.0 / x != 1.0 / n);\
2390 printf ("x != -x; this is wrong!\n");\
2394 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
2395 1.0 / x != 1.0 / p);\
2397 printf ("x != +y; this is wrong!\n");\
2400 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
2401 1.0 / x != 1.0 / p);\
2403 printf ("x != -y; this is wrong!\n");\
2405 void prefix ## test(void)\
2407 printf("testing '%s'\n", #typename);\
2408 prefix ## cmp(1, 2.5);\
2409 prefix ## cmp(2, 1.5);\
2410 prefix ## cmp(1, 1);\
2411 prefix ## fcast(234.6);\
2412 prefix ## fcast(-2334.6);\
2414 prefix ## signed_zeros();\
2417 FTEST(f
, float, float, "%f")
2418 FTEST(d
, double, double, "%f")
2419 FTEST(ld
, long double, LONG_DOUBLE
, "%Lf")
2421 double ftab1
[3] = { 1.2, 3.4, -5.6 };
2424 void float_test(void)
2426 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
2431 static double nan2
= 0.0/0.0;
2432 static double inf1
= 1.0/0.0;
2433 static double inf2
= 1e5000
;
2435 printf("float_test:\n");
2436 printf("sizeof(float) = %d\n", sizeof(float));
2437 printf("sizeof(double) = %d\n", sizeof(double));
2438 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE
));
2442 printf("%f %f %f\n", ftab1
[0], ftab1
[1], ftab1
[2]);
2443 printf("%f %f %f\n", 2.12, .5, 2.3e10
);
2444 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
2446 printf("da=%f\n", da
);
2448 printf("fa=%f\n", fa
);
2451 printf("da = %f\n", da
);
2454 printf("db = %f\n", db
);
2455 printf("nan != nan = %d, inf1 = %f, inf2 = %f\n", nan2
!= nan2
, inf1
, inf2
);
2464 return fib(n
-1) + fib(n
-2);
2468 # define aligned_function 0
2470 void __attribute__((aligned(16))) aligned_function(int i
) {}
2483 printf("funcptr:\n");
2490 /* more complicated pointer computation */
2493 printf("sizeof1 = %d\n", sizeof(funcptr_test
));
2494 printf("sizeof2 = %d\n", sizeof funcptr_test
);
2495 printf("sizeof3 = %d\n", sizeof(&funcptr_test
));
2496 printf("sizeof4 = %d\n", sizeof &funcptr_test
);
2504 /* Check that we can align functions */
2505 func
= aligned_function
;
2506 printf("aligned_function (should be zero): %d\n", ((int)(uintptr_t)func
) & 15);
2509 void lloptest(long long a
, long long b
)
2511 unsigned long long ua
, ub
;
2516 printf("arith: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2522 printf("arith1: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2528 printf("bin: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2534 printf("test: %d %d %d %d %d %d\n",
2542 printf("utest: %d %d %d %d %d %d\n",
2553 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2554 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
++, b
++);
2555 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", --a
, --b
);
2556 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2558 printf("not: %d %d %d %d\n", !a
, !ua
, !b
, !ub
);
2561 void llshift(long long a
, int b
)
2563 printf("shift: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2564 (unsigned long long)a
>> b
,
2567 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2568 (unsigned long long)a
>> 3,
2571 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2572 (unsigned long long)a
>> 35,
2582 long long la
, lb
, lc
;
2583 unsigned long long ula
, ulb
, ulc
;
2586 la
= (la
<< 20) | 0x12345;
2588 printf("la=" LONG_LONG_FORMAT
" ula=" ULONG_LONG_FORMAT
"\n", la
, ula
);
2593 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
2598 printf("ftoll: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", la
, lb
, lc
);
2603 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
2608 printf("ftoull: " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
"\n", ula
, ulb
, ulc
);
2611 long long llfunc1(int a
)
2621 long long int value(struct S
*v
)
2623 return ((long long int)v
->item
);
2626 long long llfunc2(long long x
, long long y
, int z
)
2631 void check_opl_save_regs(char *a
, long long b
, int c
)
2636 void longlong_test(void)
2641 printf("longlong_test:\n");
2642 printf("sizeof(long long) = %d\n", sizeof(long long));
2647 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2648 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %Lx\n",
2652 0x1234567812345679);
2654 printf(LONG_LONG_FORMAT
"\n", a
);
2657 lloptest(0xff, 0x1234);
2658 b
= 0x72345678 << 10;
2662 b
= 0x72345678LL
<< 10;
2673 /* long long reg spill test */
2678 printf("%lld\n", value(&a
));
2680 lloptest(0x80000000, 0);
2683 long long *p
, v
, **pp
;
2687 printf("another long long spill test : %lld\n", *p
);
2690 v
= llfunc2(**pp
, **pp
, ia
);
2691 printf("a long long function (arm-)reg-args test : %lld\n", v
);
2695 printf("%d %d %d %d\n", a
> b
, a
< b
, a
>= b
, a
<= b
);
2697 printf(LONG_LONG_FORMAT
"\n", 0x123456789LLU
);
2699 /* long long pointer deref in argument passing test */
2704 /* shortening followed by widening */
2705 unsigned long long u
= 0x8000000000000001ULL
;
2706 u
= (unsigned)(u
+ 1);
2707 printf("long long u=" ULONG_LONG_FORMAT
"\n", u
);
2708 u
= 0x11223344aa998877ULL
;
2709 u
= (unsigned)(int)(u
+ 1);
2710 printf("long long u=" ULONG_LONG_FORMAT
"\n", u
);
2712 /* was a problem with missing save_regs in gen_opl on 32-bit platforms */
2714 check_opl_save_regs(&cc
, -1, 0);
2715 printf("check_opl_save_regs: %d\n", cc
);
2718 void manyarg_test(void)
2720 LONG_DOUBLE ld
= 1234567891234LL;
2721 printf("manyarg_test:\n");
2722 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2723 1, 2, 3, 4, 5, 6, 7, 8,
2724 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2725 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2726 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2727 1, 2, 3, 4, 5, 6, 7, 8,
2728 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2729 1234567891234LL, 987654321986LL,
2731 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2732 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2733 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2734 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2735 1234567891234LL, 987654321986LL,
2737 printf("%d %d %d %d %d %d %d %d %Lf\n",
2738 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2739 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2740 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"%f %f %Lf\n",
2741 1, 2, 3, 4, 5, 6, 7, 8,
2742 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2743 1234567891234LL, 987654321986LL,
2745 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2746 "%Lf " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f %Lf\n",
2747 1, 2, 3, 4, 5, 6, 7, 8,
2748 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2749 ld
, 1234567891234LL, 987654321986LL,
2754 va_arg_with_struct_ptr(va_list ap
) {
2756 * This was a BUG identified with FFTW-3.3.8 on arm64.
2757 * The test case only checks it compiles on all supported
2758 * architectures. This function is not currently called.
2760 struct X
{ int _x
; };
2761 struct X
*x
= va_arg(ap
, struct X
*);
2765 void vprintf1(const char *fmt
, ...)
2789 i
= va_arg(ap
, int);
2793 d
= va_arg(ap
, double);
2797 ll
= va_arg(ap
, long long);
2798 printf(LONG_LONG_FORMAT
, ll
);
2801 ld
= va_arg(ap
, LONG_DOUBLE
);
2819 void stdarg_for_struct(struct myspace bob
, ...)
2821 struct myspace george
, bill
;
2826 bill
= va_arg(ap
, struct myspace
);
2827 george
= va_arg(ap
, struct myspace
);
2828 validate
= va_arg(ap
, int);
2829 printf("stdarg_for_struct: %d %d %d %d\n",
2830 bob
.profile
, bill
.profile
, george
.profile
, validate
);
2834 void stdarg_for_libc(const char *fmt
, ...)
2837 va_start(args
, fmt
);
2842 void stdarg_syntax(int n
, ...)
2850 i
= va_arg(ap
, int);
2851 printf("stdarg_void_expr: %d\n", i
);
2855 void stdarg_test(void)
2857 LONG_DOUBLE ld
= 1234567891234LL;
2860 vprintf1("%d %d %d\n", 1, 2, 3);
2861 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2862 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2863 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2864 vprintf1("%d %f %l %F %d %f %l %F\n",
2865 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2866 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2867 1, 2, 3, 4, 5, 6, 7, 8,
2868 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2869 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2870 1, 2, 3, 4, 5, 6, 7, 8,
2871 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2872 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2874 1, 2, 3, 4, 5, 6, 7, 8,
2875 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2876 1234567891234LL, 987654321986LL,
2878 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2880 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2881 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2882 1234567891234LL, 987654321986LL,
2884 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2885 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2886 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2888 1, 2, 3, 4, 5, 6, 7, 8,
2889 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2890 1234567891234LL, 987654321986LL,
2892 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2893 "%F %l %l %f %f %F\n",
2894 1, 2, 3, 4, 5, 6, 7, 8,
2895 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2896 ld
, 1234567891234LL, 987654321986LL,
2900 stdarg_for_struct(bob
, bob
, bob
, bob
.profile
);
2901 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2902 stdarg_syntax(1, 17);
2905 void whitespace_test(void)
2911 ntf("whitspace:\n");\f\v
2915 #ifdef CORRECT_CR_HANDLING
2924 #ifdef ACCEPT_CR_IN_STRINGS
2925 printf("len1=%d\n", strlen("
2927 #ifdef CORRECT_CR_HANDLING
2930 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
2932 printf("len1=%d\n", strlen("
a
2934 #endif /* ACCEPT_CR_IN_STRINGS */
2937 int reltab
[3] = { 1, 2, 3 };
2939 int *rel1
= &reltab
[1];
2940 int *rel2
= &reltab
[2];
2943 void relocation_test(void) {}
2945 void getmyaddress(void)
2947 printf("in getmyaddress\n");
2951 long __pa_symbol(void)
2953 /* This 64bit constant was handled incorrectly, it was used as addend
2954 (which can hold 64bit just fine) in connection with a symbol,
2955 and TCC generates wrong code for that (displacements are 32bit only).
2956 This effectively is "+ 0x80000000", and if addresses of globals
2957 are below 2GB the result should be a number without high 32 bits set. */
2958 return ((long)(((unsigned long)(&rel1
))) - (0xffffffff80000000UL
));
2962 unsigned long theaddress
= (unsigned long)getmyaddress
;
2963 void relocation_test(void)
2965 void (*fptr
)(void) = (void (*)(void))theaddress
;
2966 printf("*rel1=%d\n", *rel1
);
2967 printf("*rel2=%d\n", *rel2
);
2970 printf("pa_symbol=0x%lx\n", __pa_symbol() >> 63);
2975 void old_style_f(a
,b
,c
)
2979 printf("a=%d b=%d b=%f\n", a
, b
, c
);
2982 void decl_func1(int cmpfn())
2984 printf("cmpfn=%lx\n", (long)cmpfn
);
2987 void decl_func2(cmpfn
)
2990 printf("cmpfn=%lx\n", (long)cmpfn
);
2993 void old_style_function(void)
2995 old_style_f((void *)1, 2, 3.0);
3002 #if defined __i386__ || defined __x86_64__ || defined __arm__
3003 char *p
= alloca(16);
3004 strcpy(p
,"123456789012345");
3005 printf("alloca: p is %s\n", p
);
3006 char *demo
= "This is only a test.\n";
3007 /* Test alloca embedded in a larger expression */
3008 printf("alloca: %s\n", strcpy(alloca(strlen(demo
)+1),demo
) );
3012 void *bounds_checking_is_enabled()
3014 char ca
[10], *cp
= ca
-1;
3015 return (ca
!= cp
+ 1) ? cp
: NULL
;
3018 typedef int constant_negative_array_size_as_compile_time_assertion_idiom
[(1 ? 2 : 0) - 1];
3020 void c99_vla_test(int size1
, int size2
)
3022 #if defined __i386__ || defined __x86_64__
3023 int size
= size1
* size2
;
3024 int tab1
[size
][2], tab2
[10][2];
3025 void *tab1_ptr
, *tab2_ptr
, *bad_ptr
;
3027 /* "size" should have been 'captured' at tab1 declaration,
3028 so modifying it should have no effect on VLA behaviour. */
3031 printf("Test C99 VLA 1 (sizeof): ");
3032 printf("%s\n", (sizeof tab1
== size1
* size2
* 2 * sizeof(int)) ? "PASSED" : "FAILED");
3035 printf("Test C99 VLA 2 (ptrs subtract): ");
3036 printf("%s\n", (tab2
- tab1
== (tab2_ptr
- tab1_ptr
) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
3037 printf("Test C99 VLA 3 (ptr add): ");
3038 printf("%s\n", &tab1
[5][1] == (tab1_ptr
+ (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
3039 printf("Test C99 VLA 4 (ptr access): ");
3040 tab1
[size1
][1] = 42;
3041 printf("%s\n", (*((int *) (tab1_ptr
+ (size1
* 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
3043 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
3044 if (bad_ptr
= bounds_checking_is_enabled()) {
3045 int *t1
= &tab1
[size1
* size2
- 1][3];
3046 int *t2
= &tab2
[9][3];
3047 printf("%s ", bad_ptr
== t1
? "PASSED" : "FAILED");
3048 printf("%s ", bad_ptr
== t2
? "PASSED" : "FAILED");
3050 char*c1
= 1 + sizeof(tab1
) + (char*)tab1
;
3051 char*c2
= 1 + sizeof(tab2
) + (char*)tab2
;
3052 printf("%s ", bad_ptr
== c1
? "PASSED" : "FAILED");
3053 printf("%s ", bad_ptr
== c2
? "PASSED" : "FAILED");
3057 printf("%s ", bad_ptr
== i1
? "PASSED" : "FAILED");
3058 printf("%s ", bad_ptr
== i2
? "PASSED" : "FAILED");
3060 int *x1
= tab1
[size1
* size2
+ 1];
3061 int *x2
= tab2
[10 + 1];
3062 printf("%s ", bad_ptr
== x1
? "PASSED" : "FAILED");
3063 printf("%s ", bad_ptr
== x2
? "PASSED" : "FAILED");
3065 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
3071 void sizeof_test(void)
3076 printf("sizeof(int) = %d\n", sizeof(int));
3077 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
3078 printf("sizeof(long) = %d\n", sizeof(long));
3079 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
3080 printf("sizeof(short) = %d\n", sizeof(short));
3081 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
3082 printf("sizeof(char) = %d\n", sizeof(char));
3083 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
3084 printf("sizeof(func) = %d\n", sizeof sizeof_test());
3086 printf("sizeof(a++) = %d\n", sizeof a
++);
3087 printf("a=%d\n", a
);
3089 printf("sizeof(**ptr) = %d\n", sizeof (**ptr
));
3091 /* The type of sizeof should be as large as a pointer, actually
3092 it should be size_t. */
3093 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
3096 /* Effectively <<32, but defined also on 32bit machines. */
3100 /* This checks that sizeof really can be used to manipulate
3101 uintptr_t objects, without truncation. */
3102 t2
= t
& -sizeof(uintptr_t);
3103 printf ("%lu %lu\n", t
, t2
);
3105 /* some alignof tests */
3106 printf("__alignof__(int) = %d\n", __alignof__(int));
3107 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
3108 printf("__alignof__(short) = %d\n", __alignof__(short));
3109 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
3110 printf("__alignof__(char) = %d\n", __alignof__(char));
3111 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
3112 printf("__alignof__(func) = %d\n", __alignof__
sizeof_test());
3114 /* sizes of VLAs need to be evaluated even inside sizeof: */
3116 printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a
]));
3117 /* And checking if sizeof compound literal works. Parenthesized: */
3118 printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n",
3119 sizeof( (struct {int i
; int j
;}){4,5} ));
3120 /* And as direct sizeof argument (as unary expression): */
3121 printf("sizeof (struct {short i; short j;}){4,5} = %d\n",
3122 sizeof (struct {short i
; short j
;}){4,5} );
3124 /* sizeof(x && y) should be sizeof(int), even if constant
3125 evaluating is possible. */
3126 printf("sizeof(t && 0) = %d\n", sizeof(t
&& 0));
3127 printf("sizeof(1 && 1) = %d\n", sizeof(1 && 1));
3128 printf("sizeof(t || 1) = %d\n", sizeof(t
|| 1));
3129 printf("sizeof(0 || 0) = %d\n", sizeof(0 || 0));
3132 void typeof_test(void)
3141 printf("a=%f b=%f c=%f\n", a
, b
, c
);
3147 struct hlist_node
*first
, *last
;
3150 void consume_ulong (unsigned long i
)
3155 void statement_expr_test(void)
3159 /* Basic stmt expr test */
3169 printf("a=%d\n", a
);
3171 /* Test that symbols aren't freed prematurely.
3172 With SYM_DEBUG valgrind will show a read from a freed
3173 symbol, and tcc will show an (invalid) warning on the initialization
3174 of 'ptr' below, if symbols are popped after the stmt expr. */
3175 void *v
= (void*)39;
3177 (struct hlist_node
*)v
;
3180 ptr
= (struct hlist_node
*)v
;
3182 /* This part used to segfault when symbols were popped prematurely.
3183 The symbols for the static local would be overwritten with
3184 helper symbols from the pre-processor expansions in between. */
3185 #define some_attr __attribute__((aligned(1)))
3186 #define tps(str) ({ \
3187 static const char *t some_attr = str; \
3190 printf ("stmtexpr: %s %s\n",
3191 tps("somerandomlongstring"),
3192 tps("anotherlongstring"));
3194 /* Test that the three decls of 't' don't interact. */
3196 int b
= ({ int t
= 41; t
; });
3197 int c
= ({ int t
= 42; t
; });
3199 /* Test that aggregate return values work. */
3202 typedef struct hlist_head T
;
3204 T t
= { (void*)43, (void*)44 };
3208 printf ("stmtexpr: %d %d %d\n", t
, b
, c
);
3209 printf ("stmtexpr: %ld %ld\n", (long)h
.first
, (long)h
.last
);
3211 /* Test that we can give out addresses of local labels. */
3212 consume_ulong(({ __label__ __here
; __here
: (unsigned long)&&__here
; }));
3214 /* Test interaction between local and global label stacks and the
3215 need to defer popping symbol from them when within statement
3216 expressions. Note how the labels are both named LBL. */
3221 LBL
: if (i
++ == 0) goto LBL
;
3223 /* jump to a classical label out of an expr-stmt that had previously
3224 overshadowed that classical label */
3228 printf("stmtexpr: %d should be 2\n", i
);
3231 void local_label_test(void)
3237 __label__ l1
, l2
, l3
, l4
;
3251 printf("a=%d\n", a
);
3261 /* inline assembler test */
3262 #if defined(__i386__) || defined(__x86_64__)
3264 /* from linux kernel */
3265 static char * strncat1(char * dest
,const char * src
,size_t count
)
3267 long d0
, d1
, d2
, d3
;
3268 __asm__
__volatile__(
3277 "testb %%al,%%al\n\t"
3281 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
3282 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
3287 static char * strncat2(char * dest
,const char * src
,size_t count
)
3289 long d0
, d1
, d2
, d3
;
3290 __asm__
__volatile__(
3291 "repne scasb\n\t" /* one-line repne prefix + string op */
3298 "testb %%al,%%al\n\t"
3302 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
3303 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
3308 static inline void * memcpy1(void * to
, const void * from
, size_t n
)
3311 __asm__
__volatile__(
3316 "1:\ttestb $1,%b4\n\t"
3320 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
3321 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
3326 static inline void * memcpy2(void * to
, const void * from
, size_t n
)
3329 __asm__
__volatile__(
3330 "rep movsl\n\t" /* one-line rep prefix + string op */
3334 "1:\ttestb $1,%b4\n\t"
3338 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
3339 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
3344 static __inline__
void sigaddset1(unsigned int *set
, int _sig
)
3346 __asm__("btsl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
3349 static __inline__
void sigdelset1(unsigned int *set
, int _sig
)
3351 asm("btrl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc", "flags");
3355 /* clang's inline asm is uncapable of 'xchgb %b0,%h0' */
3356 static __inline__ __const__
unsigned int swab32(unsigned int x
)
3358 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
3359 "rorl $16,%0\n\t" /* swap words */
3360 "xchgb %b0,%h0" /* swap higher bytes */
3367 static __inline__
unsigned long long mul64(unsigned int a
, unsigned int b
)
3369 unsigned long long res
;
3371 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
3372 but still test the 32bit->64bit mull. */
3373 unsigned int resh
, resl
;
3374 __asm__("mull %2" : "=a" (resl
), "=d" (resh
) : "a" (a
), "r" (b
));
3375 res
= ((unsigned long long)resh
<< 32) | resl
;
3377 __asm__("mull %2" : "=A" (res
) : "a" (a
), "r" (b
));
3382 static __inline__
unsigned long long inc64(unsigned long long a
)
3384 unsigned long long res
;
3386 /* Using the A constraint is wrong, and increments are tested
3390 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res
) : "A" (a
));
3403 unsigned long mconstraint_test(struct struct1231
*r
)
3408 __asm__
volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
3409 : "=&r" (ret
), "=m" (a
)
3410 : "m" (*(struct struct123
*)r
->addr
));
3415 int fls64(unsigned long long x
)
3425 void other_constraints_test(void)
3430 __asm__
volatile ("mov %P1,%0" : "=r" (ret
) : "p" (&var
));
3431 printf ("oc1: %d\n", ret
== (unsigned long)&var
);
3436 /* Test global asm blocks playing with aliases. */
3437 void base_func(void)
3439 printf ("asmc: base\n");
3443 extern void override_func1 (void);
3444 extern void override_func2 (void);
3446 asm(".weak override_func1\n.set override_func1, base_func");
3447 asm(".set override_func1, base_func");
3448 asm(".set override_func2, base_func");
3450 void override_func2 (void)
3452 printf ("asmc: override2\n");
3455 /* This checks a construct used by the linux kernel to encode
3456 references to strings by PC relative references. */
3457 extern int bug_table
[] __attribute__((section("__bug_table")));
3458 char * get_asm_string (void)
3460 /* On i386 when -fPIC is enabled this would cause a compile error with GCC,
3461 the problem being the "i" constraint used with a symbolic operand
3462 resolving to a local label. That check is overly zealous as the code
3463 within the asm makes sure to use it only in PIC-possible contexts,
3464 but all GCC versions behave like so. We arrange for PIC to be disabled
3465 for compiling tcctest.c in the Makefile.
3467 Additionally the usage of 'c' in "%c0" in the template is actually wrong,
3468 as that would expect an operand that is a condition code. The operand
3469 as is (a local label) is accepted by GCC in non-PIC mode, and on x86-64.
3470 What the linux kernel really wanted is 'p' to disable the addition of '$'
3471 to the printed operand (as in "$.LC0" where the template only wants the
3472 bare operand ".LC0"). But the code below is what the linux kernel
3473 happens to use and as such is the one we want to test. */
3474 extern int some_symbol
;
3475 asm volatile (".globl some_symbol\n"
3478 "some_symbol: .long 0\n"
3479 ".pushsection __bug_table, \"a\"\n"
3480 ".globl bug_table\n"
3482 /* The first entry (1b-2b) is unused in this test,
3483 but we include it to check if cross-section
3484 PC-relative references work. */
3485 "2:\t.long 1b - 2b, %c0 - 2b\n"
3486 ".popsection\n" : : "i" ("A string"));
3487 char * str
= ((char*)bug_table
) + bug_table
[1];
3491 /* This checks another constructs with local labels. */
3492 extern unsigned char alld_stuff
[];
3499 ".pushsection .data.ignore\n"
3500 ".long 661b - .\n" /* This reference to 661 generates an external sym
3501 which shouldn't somehow overwrite the offset that's
3502 already determined for it. */
3504 ".byte 662b - 661b\n" /* So that this value is undeniably 1. */);
3506 void asm_local_label_diff (void)
3508 printf ("asm_local_label_diff: %d %d\n", alld_stuff
[0], alld_stuff
[1]);
3512 /* This checks that static local variables are available from assembler. */
3513 void asm_local_statics (void)
3515 static int localint
= 41;
3516 asm("incl %0" : "+m" (localint
));
3517 printf ("asm_local_statics: %d\n", localint
);
3524 void fancy_copy (unsigned *in
, unsigned *out
)
3526 asm volatile ("" : "=r" (*out
) : "0" (*in
));
3529 void fancy_copy2 (unsigned *in
, unsigned *out
)
3531 asm volatile ("mov %0,(%1)" : : "r" (*in
), "r" (out
) : "memory");
3534 #if defined __x86_64__ && !defined _WIN64
3535 void clobber_r12(void)
3537 asm volatile("mov $1, %%r12" ::: "r12");
3541 void test_high_clobbers_really(void)
3543 #if defined __x86_64__ && !defined _WIN64
3544 register long val
asm("r12");
3546 /* This tests if asm clobbers correctly save/restore callee saved
3547 registers if they are clobbered and if it's the high 8 x86-64
3548 registers. This is fragile for GCC as the constraints do not
3549 correctly capture the data flow, but good enough for us. */
3550 asm volatile("mov $0x4542, %%r12" : "=r" (val
):: "memory");
3552 asm volatile("mov %%r12, %0" : "=r" (val2
) : "r" (val
): "memory");
3553 printf("asmhc: 0x%x\n", val2
);
3557 void test_high_clobbers(void)
3559 #if defined __x86_64__ && !defined _WIN64
3561 asm volatile("mov %%r12,%0" :: "m" (x1
)); /* save r12 */
3562 test_high_clobbers_really();
3563 asm volatile("mov %%r12,%0" :: "m" (x2
)); /* new r12 */
3564 asm volatile("mov %0,%%r12" :: "m" (x1
)); /* restore r12 */
3565 /* should be 0 but tcc doesn't save r12 automatically, which has
3566 bad effects when gcc helds TCCState *s in r12 in tcc.c:main */
3567 //printf("r12-clobber-diff: %lx\n", x2 - x1);
3571 static long cpu_number
;
3572 void trace_console(long len
, long len2
)
3575 /* This generated invalid code when the emission of the switch
3576 table isn't disabled. The asms are necessary to show the bug,
3577 normal statements don't work (they need to generate some code
3578 even under nocode_wanted, which normal statements don't do,
3579 but asms do). Also at least these number of cases is necessary
3580 to generate enough "random" bytes. They ultimately are enough
3581 to create invalid instruction patterns to which the first
3582 skip-to-decision-table jump jumps. If decision table emission
3583 is disabled all of this is no problem.
3585 It also is necessary that the switches are in a statement expression
3586 (which has the property of not being enterable from outside. no
3597 case 8: printf("bla"); pfo_ret__
= 42; break;
3599 pscr_ret__
= pfo_ret__
;
3606 case 1:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3607 case 2:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3608 case 4:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3609 case 8:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3610 default: printf("impossible\n");
3612 pscr_ret__
= pfo_ret__
;
3624 void test_asm_dead_code(void)
3627 /* Try to make sure that xdi contains a zero, and hence will
3628 lead to a segfault if the next asm is evaluated without
3629 arguments being set up. */
3630 asm volatile ("" : "=D" (rdi
) : "0" (0));
3633 /* This shouldn't trigger a segfault, either the argument
3634 registers need to be set up and the asm emitted despite
3635 this being in an unevaluated context, or both the argument
3636 setup _and_ the asm emission need to be suppressed. The latter
3637 is better. Disabling asm code gen when suppression is on
3638 also fixes the above trace_console bug, but that came earlier
3639 than asm suppression. */
3640 asm volatile ("movl $0,(%0)" : : "D" (&var
) : "memory");
3645 void test_asm_call(void)
3647 #if defined __x86_64__ && !defined _WIN64
3648 static char str
[] = "PATH";
3650 /* This tests if a reference to an undefined symbol from an asm
3651 block, which isn't otherwise referenced in this file, is correctly
3652 regarded as global symbol, so that it's resolved by other object files
3653 or libraries. We chose getenv here, which isn't used anywhere else
3654 in this file. (If we used e.g. printf, which is used we already
3655 would have a global symbol entry, not triggering the bug which is
3657 /* two pushes so stack remains aligned */
3658 asm volatile ("push %%rdi; push %%rdi; mov %0, %%rdi;"
3659 #if 1 && !defined(__TINYC__) && (defined(__PIC__) || defined(__PIE__)) && !defined(__APPLE__)
3661 #elif defined(__APPLE__)
3666 "pop %%rdi; pop %%rdi"
3667 : "=a" (s
) : "r" (str
));
3668 printf("asmd: %s\n", s
);
3672 #if defined __x86_64__
3673 # define RX "(%rip)"
3678 void asm_dot_test(void)
3685 asm(".text; lea S"RX
",%eax; lea ."RX
",%ecx; sub %ecx,%eax; S=.; jmp p0");
3688 /* clangs internal assembler is broken */
3689 asm(".text; jmp .+6; .int 123; mov .-4"RX
",%eax; jmp p0");
3691 asm(".text; mov $0, %eax; jmp p0");
3694 #if !defined(_WIN32) && !defined(__APPLE__)
3695 asm(".pushsection \".data\"; Y=.; .int 999; X=Y; .int 456; X=.-4; .popsection");
3697 asm(".data; Y=.; .int 999; X=Y; .int 456; X=.-4; .text");
3699 asm(".text; mov X"RX
",%eax; jmp p0");
3702 /* Bah! Clang! Doesn't want to redefine 'X' */
3703 asm(".text; mov $123,%eax; jmp p0");
3706 asm(".data; X=.; .int 789; Y=.; .int 999; .previous");
3708 asm(".data; X=.; .int 789; Y=.; .int 999; .text");
3710 asm(".text; mov X"RX
",%eax; X=Y; jmp p0");
3713 asm(".text; p0=.; mov %%eax,%0;" : "=m"(r
)); break;
3717 printf("asm_dot_test %d: %d\n", x
, r
);
3724 unsigned int val
, val2
;
3725 struct struct123 s1
;
3726 struct struct1231 s2
= { (unsigned long)&s1
};
3727 /* Hide the outer base_func, but check later that the inline
3728 asm block gets the outer one. */
3730 void override_func3 (void);
3731 unsigned long asmret
;
3735 register int regvar
asm("%esi");
3737 printf("inline asm:\n");
3739 // parse 0x1E-1 as 3 tokens in asm mode
3740 asm volatile ("mov $0x1E-1,%eax");
3742 /* test the no operand case */
3743 asm volatile ("xorl %eax, %eax");
3745 memcpy1(buf
, "hello", 6);
3746 strncat1(buf
, " worldXXXXX", 3);
3747 printf("%s\n", buf
);
3749 memcpy2(buf
, "hello", 6);
3750 strncat2(buf
, " worldXXXXX", 3);
3751 printf("%s\n", buf
);
3753 /* 'A' constraint test */
3754 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
3755 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
3759 printf("mconstraint: %d", mconstraint_test(&s2
));
3760 printf(" %d %d\n", s1
.a
, s1
.b
);
3761 other_constraints_test();
3763 sigdelset1(&set
, 2);
3764 sigaddset1(&set
, 16);
3765 /* NOTE: we test here if C labels are correctly restored after the
3769 __asm__("btsl %1,%0" : "=m"(set
) : "Ir"(20) : "cc");
3770 printf("set=0x%x\n", set
);
3773 printf("swab32(0x%08x) = 0x%0x\n", val
, swab32(val
));
3779 /* The base_func ref from the following inline asm should find
3780 the global one, not the local decl from this function. */
3781 asm volatile(".weak override_func3\n.set override_func3, base_func");
3783 printf("asmstr: %s\n", get_asm_string());
3784 asm_local_label_diff();
3786 asm_local_statics();
3789 /* clang can't deal with the type change */
3790 /* Check that we can also load structs of appropriate layout
3792 asm volatile("" : "=r" (asmret
) : "0"(s2
));
3793 if (asmret
!= s2
.addr
)
3794 printf("asmstr: failed\n");
3797 /* Check that the typesize correctly sets the register size to
3799 asm volatile("cmp %1,%2; sete %0" : "=a"(somebool
) : "r"(1), "r"(2));
3801 printf("asmbool: failed\n");
3804 fancy_copy (&val
, &val2
);
3805 printf ("fancycpy(%d)=%d\n", val
, val2
);
3807 fancy_copy2 (&val
, &val2
);
3808 printf ("fancycpy2(%d)=%d\n", val
, val2
);
3809 asm volatile ("mov $0x4243, %%esi" : "=r" (regvar
));
3810 printf ("regvar=%x\n", regvar
);
3811 test_high_clobbers();
3812 trace_console(8, 8);
3813 test_asm_dead_code();
3829 #define COMPAT_TYPE(type1, type2) \
3831 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
3832 __builtin_types_compatible_p (type1, type2));\
3837 void builtin_test(void)
3843 COMPAT_TYPE(int, int);
3844 COMPAT_TYPE(int, unsigned int);
3845 COMPAT_TYPE(int, char);
3846 COMPAT_TYPE(int, const int);
3847 COMPAT_TYPE(int, volatile int);
3848 COMPAT_TYPE(int *, int *);
3849 COMPAT_TYPE(int *, void *);
3850 COMPAT_TYPE(int *, const int *);
3851 COMPAT_TYPE(char *, unsigned char *);
3852 COMPAT_TYPE(char *, signed char *);
3853 COMPAT_TYPE(char *, char *);
3854 /* space is needed because tcc preprocessor introduces a space between each token */
3855 COMPAT_TYPE(char * *, void *);
3857 printf("res1 = %d\n", __builtin_constant_p(1));
3858 printf("res2 = %d\n", __builtin_constant_p(1 + 2));
3859 printf("res3 = %d\n", __builtin_constant_p(&constant_p_var
));
3860 printf("res4 = %d\n", __builtin_constant_p(constant_p_var
));
3861 printf("res5 = %d\n", __builtin_constant_p(100000 / constant_p_var
));
3863 /* clang doesn't regard this as constant expression */
3864 printf("res6 = %d\n", __builtin_constant_p(i
&& 0));
3866 printf("res7 = %d\n", __builtin_constant_p(i
&& 1));
3868 printf("res8 = %d\n", __builtin_constant_p(i
&& 0 ? i
: 34));
3872 i
= __builtin_choose_expr (1 != 0, ll
, s
);
3873 printf("bce: %d\n", i
);
3874 i
= __builtin_choose_expr (1 != 1, ll
, s
);
3875 printf("bce: %d\n", i
);
3876 i
= sizeof (__builtin_choose_expr (1, ll
, s
));
3877 printf("bce: %d\n", i
);
3878 i
= sizeof (__builtin_choose_expr (0, ll
, s
));
3879 printf("bce: %d\n", i
);
3881 //printf("bera: %p\n", __builtin_extract_return_addr((void*)43));
3885 extern int __attribute__((weak
)) weak_f1(void);
3886 extern int __attribute__((weak
)) weak_f2(void);
3887 extern int weak_f3(void);
3888 extern int __attribute__((weak
)) weak_v1
;
3889 extern int __attribute__((weak
)) weak_v2
;
3892 extern int (*weak_fpa
)() __attribute__((weak
));
3893 extern int __attribute__((weak
)) (*weak_fpb
)();
3894 extern __attribute__((weak
)) int (*weak_fpc
)();
3896 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak
));
3897 extern int __attribute((weak
)) weak_asm_f2(void) asm("weak_asm_f2x") ;
3898 extern int __attribute((weak
)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak
));
3899 extern int weak_asm_v1
asm("weak_asm_v1x") __attribute((weak
));
3900 extern int __attribute((weak
)) weak_asm_v2
asm("weak_asm_v2x") ;
3901 extern int __attribute((weak
)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak
));
3904 static const size_t dummy
= 0;
3905 extern __typeof(dummy
) weak_dummy1
__attribute__((weak
, alias("dummy")));
3906 extern __typeof(dummy
) __attribute__((weak
, alias("dummy"))) weak_dummy2
;
3907 extern __attribute__((weak
, alias("dummy"))) __typeof(dummy
) weak_dummy3
;
3910 int some_lib_func(void);
3911 int dummy_impl_of_slf(void) { return 444; }
3913 int some_lib_func(void) __attribute__((weak
, alias("dummy_impl_of_slf")));
3916 int weak_toolate() __attribute__((weak
));
3917 int weak_toolate() { return 0; }
3919 void __attribute__((weak
)) weak_test(void)
3921 printf("weak_f1=%d\n", weak_f1
? weak_f1() : 123);
3922 printf("weak_f2=%d\n", weak_f2
? weak_f2() : 123);
3923 printf("weak_f3=%d\n", weak_f3
? weak_f3() : 123);
3924 printf("weak_v1=%d\n",&weak_v1
? weak_v1
: 123);
3925 printf("weak_v2=%d\n",&weak_v2
? weak_v2
: 123);
3926 printf("weak_v3=%d\n",&weak_v3
? weak_v3
: 123);
3928 printf("weak_fpa=%d\n",&weak_fpa
? weak_fpa() : 123);
3929 printf("weak_fpb=%d\n",&weak_fpb
? weak_fpb() : 123);
3930 printf("weak_fpc=%d\n",&weak_fpc
? weak_fpc() : 123);
3932 printf("weak_asm_f1=%d\n", weak_asm_f1
!= NULL
);
3933 printf("weak_asm_f2=%d\n", weak_asm_f2
!= NULL
);
3934 printf("weak_asm_f3=%d\n", weak_asm_f3
!= NULL
);
3935 printf("weak_asm_v1=%d\n",&weak_asm_v1
!= NULL
);
3936 printf("weak_asm_v2=%d\n",&weak_asm_v2
!= NULL
);
3937 printf("weak_asm_v3=%d\n",&weak_asm_v3
!= NULL
);
3939 printf("some_lib_func=%d\n", &some_lib_func
? some_lib_func() : 0);
3943 int __attribute__((weak
)) weak_f2() { return 222; }
3944 int __attribute__((weak
)) weak_f3() { return 333; }
3945 int __attribute__((weak
)) weak_v2
= 222;
3946 int __attribute__((weak
)) weak_v3
= 333;
3949 void const_func(const int a
)
3953 void const_warn_test(void)
3962 int getme (struct condstruct
*s
, int i
)
3964 int i1
= (i
== 0 ? 0 : s
)->i
;
3965 int i2
= (i
== 0 ? s
: 0)->i
;
3966 int i3
= (i
== 0 ? (void*)0 : s
)->i
;
3967 int i4
= (i
== 0 ? s
: (void*)0)->i
;
3968 return i1
+ i2
+ i3
+ i4
;
3977 struct global_data global_data
;
3979 int global_data_getstuff (int *, int);
3981 void global_data_callit (int i
)
3983 *global_data
.b
[i
] = global_data_getstuff (global_data
.b
[i
], 1);
3986 int global_data_getstuff (int *p
, int i
)
3991 void global_data_test (void)
3993 global_data
.a
[0] = 42;
3994 global_data
.b
[0] = &global_data
.a
[0];
3995 global_data_callit (0);
3996 printf ("%d\n", global_data
.a
[0]);
4001 unsigned char fill
: 3;
4002 unsigned char b1
: 1;
4003 unsigned char b2
: 1;
4004 unsigned char fill2
: 3;
4007 int glob1
, glob2
, glob3
;
4009 void compare_comparisons (struct cmpcmpS
*s
)
4011 if (s
->b1
!= (glob1
== glob2
)
4012 || (s
->b2
!= (glob1
== glob3
)))
4013 printf ("comparing comparisons broken\n");
4016 void cmp_comparison_test(void)
4020 glob1
= 42; glob2
= 42;
4023 compare_comparisons (&s
);
4026 int fcompare (double a
, double b
, int code
)
4029 case 0: return a
== b
;
4030 case 1: return a
!= b
;
4031 case 2: return a
< b
;
4032 case 3: return a
>= b
;
4033 case 4: return a
> b
;
4034 case 5: return a
<= b
;
4039 void math_cmp_test(void)
4041 double nan
= 0.0/0.0;
4046 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
4048 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
4049 And it does this in various ways so that all code generation paths
4050 are checked (generating inverted tests, or non-inverted tests, or
4051 producing a 0/1 value without jumps (that's done in the fcompare
4053 #define FCMP(a,b,op,iop,code) \
4054 if (fcompare (a,b,code)) \
4055 bug (a,b,op,iop,1); \
4057 bug (a,b,op,iop,2); \
4061 bug (a,b,op,iop,3); \
4062 if ((a op b) || comp) \
4063 bug (a,b,op,iop,4); \
4064 if ((a iop b) || comp) \
4067 bug (a,b,op,iop,5); \
4068 if (v = !(a op b), !v) bug(a,b,op,iop,7);
4070 /* Equality tests. */
4071 FCMP(nan
, nan
, ==, !=, 0);
4072 FCMP(one
, two
, ==, !=, 0);
4073 FCMP(one
, one
, !=, ==, 1);
4074 /* Non-equality is a bit special. */
4075 if (!fcompare (nan
, nan
, 1))
4076 bug (nan
, nan
, !=, ==, 6);
4078 /* Relational tests on numbers. */
4079 FCMP(two
, one
, <, >=, 2);
4080 FCMP(one
, two
, >=, <, 3);
4081 FCMP(one
, two
, >, <=, 4);
4082 FCMP(two
, one
, <=, >, 5);
4084 /* Relational tests on NaNs. Note that the inverse op here is
4085 always !=, there's no operator in C that is equivalent to !(a < b),
4086 when NaNs are involved, same for the other relational ops. */
4087 FCMP(nan
, nan
, <, !=, 2);
4088 FCMP(nan
, nan
, >=, !=, 3);
4089 FCMP(nan
, nan
, >, !=, 4);
4090 FCMP(nan
, nan
, <=, !=, 5);
4093 double get100 () { return 100.0; }
4095 void callsave_test(void)
4097 #if defined __i386__ || defined __x86_64__ || defined __arm__
4098 int i
, s
; double *d
; double t
;
4099 s
= sizeof (double);
4100 printf ("callsavetest: %d\n", s
);
4101 d
= alloca (sizeof(double));
4103 /* x86-64 had a bug were the next call to get100 would evict
4104 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
4105 in int type, not pointer type. When alloca returns a pointer
4106 with the high 32 bit set (which is likely on x86-64) the access
4107 generates a segfault. */
4108 i
= d
[0] > get100 ();
4114 void bfa3(ptrdiff_t str_offset
)
4116 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset
);
4118 void bfa2(ptrdiff_t str_offset
)
4120 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset
);
4123 void bfa1(ptrdiff_t str_offset
)
4125 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset
);
4129 void builtin_frame_address_test(void)
4131 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
4133 char str
[] = "__builtin_frame_address";
4134 char *fp0
= __builtin_frame_address(0);
4136 printf("str: %s\n", str
);
4143 char via_volatile (char i
)
4150 struct __attribute__((__packed__
)) Spacked
{
4155 struct Spacked spacked
;
4156 typedef struct __attribute__((__packed__
)) {
4162 typedef struct Spacked3_s
{
4166 } __attribute__((__packed__
)) Spacked3
;
4168 struct gate_struct64
{
4169 unsigned short offset_low
;
4170 unsigned short segment
;
4171 unsigned ist
: 3, zero0
: 5, type
: 5, dpl
: 2, p
: 1;
4172 unsigned short offset_middle
;
4173 unsigned offset_high
;
4175 } __attribute__((packed
));
4176 typedef struct gate_struct64 gate_desc
;
4177 gate_desc a_gate_desc
;
4178 void attrib_test(void)
4181 printf("attr: %d %d %d %d\n", sizeof(struct Spacked
),
4182 sizeof(spacked
), sizeof(Spacked2
), sizeof(spacked2
));
4183 printf("attr: %d %d\n", sizeof(Spacked3
), sizeof(spacked3
));
4184 printf("attr: %d %d\n", sizeof(gate_desc
), sizeof(a_gate_desc
));
4187 extern __attribute__((__unused__
)) char * __attribute__((__unused__
)) *
4188 strange_attrib_placement (void);
4190 void * __attribute__((__unused__
)) get_void_ptr (void *a
)
4195 /* This part checks for a bug in TOK_GET (used for inline expansion),
4196 where the large long long constant left the the high bits set for
4197 the integer constant token. */
4199 int __get_order(unsigned long long size
)
4202 size
-= 0xffff880000000000ULL
; // this const left high bits set in the token
4204 struct S
{ int i
: 1; } s
; // constructed for this '1'
4210 /* This just forces the above inline function to be actually emitted. */
4211 int force_get_order(unsigned long s
)
4213 return __get_order(s
);
4216 #define pv(m) printf(sizeof (s->m + 0) == 8 ? "%016llx\n" : "%02x\n", s->m)
4218 /* Test failed when using bounds checking */
4219 void bounds_check1_test (void)