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);
1486 int undefined_function(void) {}
1489 static inline void refer_to_undefined(void)
1491 undefined_function();
1494 void optimize_out(void)
1496 int i
= 0 ? undefined_function() : defined_function();
1497 printf ("oo:%d\n", i
);
1498 int j
= 1 ? defined_function() : undefined_function();
1499 printf ("oo:%d\n", j
);
1501 printf("oo:%d\n", undefined_function());
1503 printf("oo:%d\n", defined_function());
1505 printf("oo:%d\n", defined_function());
1507 printf("oo:%d\n", undefined_function());
1509 printf("oow:%d\n", defined_function());
1511 printf("oow:%d\n", undefined_function());
1514 /* Following is a switch without {} block intentionally. */
1517 printf ("oos:%d\n", defined_function());
1518 /* The following break shouldn't lead to disabled code after
1522 printf ("ool1:%d\n", defined_function());
1523 /* Same for the other types of loops. */
1527 printf ("ool2:%d\n", defined_function());
1530 printf ("ool3:%d\n", defined_function());
1531 /* Normal {} blocks without controlling statements
1532 shouldn't reactivate code emission */
1537 printf ("ool4:%d\n", undefined_function());
1542 break; /* this break shouldn't disable code outside the if. */
1543 printf("ool5:%d\n", defined_function());
1553 printf("ool6:%d\n", defined_function());
1560 printf("ool7:%d\n", undefined_function());
1563 /* Test that constants in logical && are optimized: */
1564 i
= 0 && undefined_function();
1565 i
= defined_function() && 0 && undefined_function();
1566 if (0 && undefined_function())
1567 undefined_function();
1568 if (defined_function() && 0)
1569 undefined_function();
1571 undefined_function();
1572 if (defined_function() && 0 && undefined_function())
1573 undefined_function();
1574 /* The same for || : */
1575 i
= 1 || undefined_function();
1576 i
= defined_function() || 1 || undefined_function();
1577 if (1 || undefined_function())
1580 undefined_function();
1581 if (defined_function() || 1)
1584 undefined_function();
1588 undefined_function();
1589 if (defined_function() || 1 || undefined_function())
1592 undefined_function();
1594 if (defined_function() && 0)
1595 refer_to_undefined();
1602 undefined_function();
1605 /* Leave the "if(1)return; printf()" in this order and last in the function */
1608 printf ("oor:%d\n", undefined_function());
1611 int defined_function(void)
1617 /* GCC accepts that */
1618 static int tab_reinit
[];
1619 static int tab_reinit
[10];
1621 static int tentative_ar
[];
1622 static int tentative_ar
[] = {1,2,3};
1624 //int cinit1; /* a global variable can be defined several times without error ! */
1628 int *cinit2
= (int []){3, 2, 1};
1630 void compound_literal_test(void)
1635 printf("compound_test:\n");
1637 p
= (int []){1, 2, 3};
1639 printf(" %d", p
[i
]);
1643 printf("%d", cinit2
[i
]);
1647 printf("q1=%s\n", q
);
1649 q
= (char *){ "tralala2" };
1650 printf("q2=%s\n", q
);
1653 printf("q3=%s\n", q3
);
1655 q
= (char []){ "tralala3" };
1656 printf("q4=%s\n", q
);
1659 p
= (int []){1, 2, cinit1
+ 3};
1661 printf(" %d", p
[i
]);
1665 p
= (int []){1, 2, 4 + i
};
1666 printf("%d %d %d\n",
1688 printf("kr_test:\n");
1689 printf("func1=%d\n", kr_func1(3, 4));
1690 printf("func2=%d\n", kr_func2(3, 4));
1697 tab
= (char*)malloc(20);
1714 /* structure assignment tests */
1720 struct structa1 ssta1
;
1722 void struct_assign_test1(struct structa1 s1
, int t
, float f
)
1724 printf("%d %d %d %f\n", s1
.f1
, s1
.f2
, t
, f
);
1727 struct structa1
struct_assign_test2(struct structa1 s1
, int t
)
1734 void struct_assign_test(void)
1737 struct structa1 lsta1
, lsta2
;
1744 printf("struct_assign_test:\n");
1748 printf("%d %d\n", s
.lsta1
.f1
, s
.lsta1
.f2
);
1750 printf("%d %d\n", s
.lsta2
.f1
, s
.lsta2
.f2
);
1755 struct_assign_test1(ps
->lsta2
, 3, 4.5);
1757 printf("before call: %d %d\n", s
.lsta2
.f1
, s
.lsta2
.f2
);
1758 ps
->lsta2
= struct_assign_test2(ps
->lsta2
, ps
->i
);
1759 printf("after call: %d %d\n", ps
->lsta2
.f1
, ps
->lsta2
.f2
);
1764 /* XXX: we should allow this even without braces */
1765 { struct_assign_test
}
1767 printf("%d\n", struct_assign_test
== t
[0].elem
);
1770 /* casts to short/char */
1772 void cast1(char a
, short b
, unsigned char c
, unsigned short d
)
1774 printf("%d %d %d %d\n", a
, b
, c
, d
);
1788 unsigned long ul
= 0x80000000UL
;
1789 p
-= 0x700000000042;
1791 printf("cast_test:\n");
1795 printf("%d %d %d %d\n",
1798 (unsigned char)(a
+ 1),
1799 (unsigned short)(a
+ 1));
1800 printf("%d %d %d %d\n",
1803 (unsigned char)0xfffff,
1804 (unsigned short)0xfffff);
1806 a
= (bcast
= 128) + 1;
1808 a
= (scast
= 65536) + 1;
1811 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
1813 /* test cast from unsigned to signed short to int */
1816 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b
, d
);
1819 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b
, d
);
1821 /* test implicit int casting for array accesses */
1825 printf("%d %d\n", tab
[0], tab
[1]);
1827 /* test implicit casting on some operators */
1828 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1829 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1830 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1832 /* from pointer to integer types */
1833 printf("%d %d %ld %ld %lld %lld\n",
1834 (int)p
, (unsigned int)p
,
1835 (long)p
, (unsigned long)p
,
1836 (long long)p
, (unsigned long long)p
);
1838 /* from integers to pointers */
1839 printf("%p %p %p %p\n",
1840 (void *)a
, (void *)b
, (void *)c
, (void *)d
);
1842 /* int to int with sign set */
1843 printf("0x%lx\n", (unsigned long)(int)ul
);
1846 /* initializers tests */
1847 struct structinit1
{
1856 int sinit3
[3] = { 1, 2, {{3}}, };
1857 int sinit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1858 int sinit5
[3][2] = { 1, 2, 3, 4, 5, 6 };
1859 int sinit6
[] = { 1, 2, 3 };
1860 int sinit7
[] = { [2] = 3, [0] = 1, 2 };
1861 char sinit8
[] = "hello" "trala";
1863 struct structinit1 sinit9
= { 1, 2, 3 };
1864 struct structinit1 sinit10
= { .f2
= 2, 3, .f1
= 1 };
1865 struct structinit1 sinit11
= { .f2
= 2, 3, .f1
= 1,
1873 char *sinit12
= "hello world";
1879 char sinit14
[10] = { "abc" };
1880 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
1882 struct { int a
[3], b
; } sinit16
[] = { { 1 }, 2 };
1898 struct complexinit0
{
1903 struct complexinit
{
1905 const struct complexinit0
*b
;
1908 const static struct complexinit cix
[] = {
1911 .b
= (const struct complexinit0
[]) {
1919 struct complexinit2
{
1924 struct complexinit2 cix20
;
1926 struct complexinit2 cix21
= {
1928 .b
= { 3001, 3002, 3003 }
1931 struct complexinit2 cix22
= {
1933 .b
= { 4001, 4002, 4003, 4004, 4005, 4006 }
1936 typedef int arrtype1
[];
1937 arrtype1 sinit19
= {1};
1938 arrtype1 sinit20
= {2,3};
1939 typedef int arrtype2
[3];
1940 arrtype2 sinit21
= {4};
1941 arrtype2 sinit22
= {5,6,7};
1943 /* Address comparisons of non-weak symbols with zero can be const-folded */
1944 int sinit23
[2] = { "astring" ? sizeof("astring") : -1,
1945 &sinit23
? 42 : -1 };
1947 int sinit24
= 2 || 1 / 0; /* exception in constant but unevaluated context */
1949 extern int external_inited
= 42;
1951 void init_test(void)
1955 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1956 int linit6
[] = { 1, 2, 3 };
1958 char linit8
[] = "hello" "trala";
1959 int linit12
[10] = { 1, 2 };
1960 int linit13
[10] = { 1, 2, [7] = 3, [3] = 4, };
1961 char linit14
[10] = "abc";
1962 int linit15
[10] = { linit1
, linit1
+ 1, [6] = linit1
+ 2, };
1963 struct linit16
{ int a1
, a2
, a3
, a4
; } linit16
= { 1, .a3
= 2 };
1964 int linit17
= sizeof(linit17
);
1966 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1967 int linit18
[2] = {&zero
? 1 : -1, zero
? -1 : 1 };
1969 printf("init_test:\n");
1971 printf("sinit1=%d\n", sinit1
);
1972 printf("sinit2=%d\n", sinit2
);
1973 printf("sinit3=%d %d %d %d\n",
1979 printf("sinit6=%d\n", sizeof(sinit6
));
1980 printf("sinit7=%d %d %d %d\n",
1986 printf("sinit8=%s\n", sinit8
);
1987 printf("sinit9=%d %d %d\n",
1992 printf("sinit10=%d %d %d\n",
1997 printf("sinit11=%d %d %d %d %d %d\n",
2008 printf("[%d][%d] = %d %d %d\n",
2009 i
, j
, sinit4
[i
][j
], sinit5
[i
][j
], linit4
[i
][j
]);
2010 printf("linit1=%d\n", linit1
);
2011 printf("linit2=%d\n", linit2
);
2012 printf("linit6=%d\n", sizeof(linit6
));
2013 printf("linit8=%d %s\n", sizeof(linit8
), linit8
);
2015 printf("sinit12=%s\n", sinit12
);
2016 printf("sinit13=%d %s %s %s\n",
2021 printf("sinit14=%s\n", sinit14
);
2023 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
2025 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
2027 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
2029 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
2031 printf("%d %d %d %d\n",
2036 /* test that initialisation is done after variable declare */
2037 printf("linit17=%d\n", linit17
);
2038 printf("sinit15=%d\n", sinit15
[0]);
2039 printf("sinit16=%d %d\n", sinit16
[0].a
[0], sinit16
[1].a
[0]);
2040 printf("sinit17=%s %d %s %d\n",
2041 sinit17
[0].s
, sinit17
[0].len
,
2042 sinit17
[1].s
, sinit17
[1].len
);
2044 printf("%x ", sinit18
[i
]);
2046 /* complex init check */
2047 printf("cix: %d %d %d %d %d %d %d\n",
2049 cix
[0].b
[0].a
, cix
[0].b
[0].b
,
2050 cix
[0].b
[1].a
, cix
[0].b
[1].b
,
2051 cix
[0].b
[2].a
, cix
[0].b
[2].b
);
2052 printf("cix2: %d %d\n", cix21
.b
[2], cix22
.b
[5]);
2053 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20
, sizeof cix21
, sizeof cix22
);
2055 printf("arrtype1: %d %d %d\n", sinit19
[0], sinit20
[0], sinit20
[1]);
2056 printf("arrtype2: %d %d\n", sizeof(sinit19
), sizeof(sinit20
));
2057 printf("arrtype3: %d %d %d\n", sinit21
[0], sinit21
[1], sinit21
[2]);
2058 printf("arrtype4: %d %d %d\n", sinit22
[0], sinit22
[1], sinit22
[2]);
2059 printf("arrtype5: %d %d\n", sizeof(sinit21
), sizeof(sinit22
));
2060 printf("arrtype6: %d\n", sizeof(arrtype2
));
2062 printf("sinit23= %d %d\n", sinit23
[0], sinit23
[1]);
2063 printf("sinit24=%d\n", sinit24
);
2064 printf("linit18= %d %d\n", linit18
[0], linit18
[1]);
2067 void switch_uc(unsigned char uc
)
2080 printf("ucsw: broken!\n");
2084 void switch_sc(signed char sc
)
2097 printf("scsw: broken!\n");
2104 unsigned long long ull
;
2129 for (i
= 1; i
<= 5; i
++) {
2130 ull
= (unsigned long long)i
<< 61;
2133 printf("ullsw:1\n");
2136 printf("ullsw:2\n");
2139 printf("ullsw:3\n");
2142 printf("ullsw:4\n");
2145 printf("ullsw:5\n");
2148 printf("ullsw: broken!\n");
2152 for (i
= 1; i
<= 5; i
++) {
2153 ll
= (long long)i
<< 61;
2171 printf("llsw: broken!\n");
2175 for (i
= -5; i
<= 5; i
++) {
2176 switch_uc((unsigned char)i
);
2179 for (i
= -5; i
<= 5; i
++) {
2180 switch_sc ((signed char)i
);
2184 /* ISOC99 _Bool type */
2185 void c99_bool_test(void)
2191 printf("bool_test:\n");
2192 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
2194 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
2196 printf("b = %d\n", b
);
2198 printf("b = %d\n", b
);
2200 printf("sizeof(x ? _Bool : _Bool) = %d (should be sizeof int)\n",
2201 sizeof((volatile)a
? b
: b2
));
2205 void bitfield_test(void)
2217 unsigned int f5
: 7;
2219 printf("bitfield_test:");
2220 printf("sizeof(st1) = %d\n", sizeof(st1
));
2229 printf("%d %d %d %d %d\n",
2230 st1
.f1
, st1
.f2
, st1
.f3
, st1
.f4
, st1
.f5
);
2233 printf("%d %d\n", sa
, ca
);
2237 printf("st1.f1 == -1\n");
2239 printf("st1.f1 != -1\n");
2241 printf("st1.f2 == -1\n");
2243 printf("st1.f2 != -1\n");
2249 unsigned long long f3
: 38;
2251 st2
.f1
= 0x123456789ULL
;
2253 st2
.f2
= (long long)a
<< 25;
2256 printf("%lld %lld %lld\n", st2
.f1
, st2
.f2
, st2
.f3
);
2259 Disabled
for now until further clarification re GCC compatibility
2268 printf("sizeof(st3) = %d\n", sizeof(st3
));
2276 printf("st4.y == %d\n", st4
.y
);
2280 int x
: 12, y
: 4, : 0, : 4, z
: 3;
2282 } st5
= { 1, 2, 3, 4, -3, 6 };
2283 printf("st5 = %d %d %d %d %d %d\n", st5
.a
, st5
.b
, st5
.x
, st5
.y
, st5
.z
, st5
.c
);
2286 unsigned char y
: 2;
2289 printf("st6.y == %d\n", st6
.y
);
2293 #define FLOAT_FMT "%f\n"
2295 /* x86's float isn't compatible with GCC */
2296 #define FLOAT_FMT "%.5f\n"
2299 /* declare strto* functions as they are C99 */
2300 double strtod(const char *nptr
, char **endptr
);
2303 float strtof(const char *nptr
, char **endptr
) {return (float)strtod(nptr
, endptr
);}
2304 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
) {return (LONG_DOUBLE
)strtod(nptr
, endptr
);}
2306 float strtof(const char *nptr
, char **endptr
);
2307 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
);
2310 #define FTEST(prefix, typename, type, fmt)\
2311 void prefix ## cmp(type a, type b)\
2313 printf("%d %d %d %d %d %d\n",\
2320 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
2328 printf(fmt "\n", ++a);\
2329 printf(fmt "\n", a++);\
2330 printf(fmt "\n", a);\
2332 printf("%d %d\n", !a, !b);\
2334 void prefix ## fcast(type a)\
2342 unsigned long long llua;\
2347 printf("ftof: %f %f %Lf\n", fa, da, la);\
2349 llia = (long long)a;\
2350 a = (a >= 0) ? a : -a;\
2351 ua = (unsigned int)a;\
2352 llua = (unsigned long long)a;\
2353 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
2356 llia = -0x123456789012345LL;\
2357 llua = 0xf123456789012345LLU;\
2359 printf("itof: " fmt "\n", b);\
2361 printf("utof: " fmt "\n", b);\
2363 printf("lltof: " fmt "\n", b);\
2365 printf("ulltof: " fmt "\n", b);\
2368 float prefix ## retf(type a) { return a; }\
2369 double prefix ## retd(type a) { return a; }\
2370 LONG_DOUBLE prefix ## retld(type a) { return a; }\
2372 void prefix ## call(void)\
2374 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
2375 printf("double: %f\n", prefix ## retd(42.123456789));\
2376 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
2377 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
2380 void prefix ## signed_zeros(void) \
2382 type x = 0.0, y = -0.0, n, p;\
2384 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
2385 1.0 / x != 1.0 / y);\
2387 printf ("x != y; this is wrong!\n");\
2391 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
2392 1.0 / x != 1.0 / n);\
2394 printf ("x != -x; this is wrong!\n");\
2398 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
2399 1.0 / x != 1.0 / p);\
2401 printf ("x != +y; this is wrong!\n");\
2404 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
2405 1.0 / x != 1.0 / p);\
2407 printf ("x != -y; this is wrong!\n");\
2409 void prefix ## test(void)\
2411 printf("testing '%s'\n", #typename);\
2412 prefix ## cmp(1, 2.5);\
2413 prefix ## cmp(2, 1.5);\
2414 prefix ## cmp(1, 1);\
2415 prefix ## fcast(234.6);\
2416 prefix ## fcast(-2334.6);\
2418 prefix ## signed_zeros();\
2421 FTEST(f
, float, float, "%f")
2422 FTEST(d
, double, double, "%f")
2423 FTEST(ld
, long double, LONG_DOUBLE
, "%Lf")
2425 double ftab1
[3] = { 1.2, 3.4, -5.6 };
2428 void float_test(void)
2430 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
2435 static double nan2
= 0.0/0.0;
2436 static double inf1
= 1.0/0.0;
2437 static double inf2
= 1e5000
;
2439 printf("float_test:\n");
2440 printf("sizeof(float) = %d\n", sizeof(float));
2441 printf("sizeof(double) = %d\n", sizeof(double));
2442 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE
));
2446 printf("%f %f %f\n", ftab1
[0], ftab1
[1], ftab1
[2]);
2447 printf("%f %f %f\n", 2.12, .5, 2.3e10
);
2448 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
2450 printf("da=%f\n", da
);
2452 printf("fa=%f\n", fa
);
2455 printf("da = %f\n", da
);
2458 printf("db = %f\n", db
);
2459 printf("nan != nan = %d, inf1 = %f, inf2 = %f\n", nan2
!= nan2
, inf1
, inf2
);
2468 return fib(n
-1) + fib(n
-2);
2472 # define aligned_function 0
2474 void __attribute__((aligned(16))) aligned_function(int i
) {}
2487 printf("funcptr:\n");
2494 /* more complicated pointer computation */
2497 printf("sizeof1 = %d\n", sizeof(funcptr_test
));
2498 printf("sizeof2 = %d\n", sizeof funcptr_test
);
2499 printf("sizeof3 = %d\n", sizeof(&funcptr_test
));
2500 printf("sizeof4 = %d\n", sizeof &funcptr_test
);
2508 /* Check that we can align functions */
2509 func
= aligned_function
;
2510 printf("aligned_function (should be zero): %d\n", ((int)(uintptr_t)func
) & 15);
2513 void lloptest(long long a
, long long b
)
2515 unsigned long long ua
, ub
;
2520 printf("arith: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2526 printf("arith1: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2532 printf("bin: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2538 printf("test: %d %d %d %d %d %d\n",
2546 printf("utest: %d %d %d %d %d %d\n",
2557 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2558 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
++, b
++);
2559 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", --a
, --b
);
2560 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2562 printf("not: %d %d %d %d\n", !a
, !ua
, !b
, !ub
);
2565 void llshift(long long a
, int b
)
2567 printf("shift: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2568 (unsigned long long)a
>> b
,
2571 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2572 (unsigned long long)a
>> 3,
2575 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2576 (unsigned long long)a
>> 35,
2586 long long la
, lb
, lc
;
2587 unsigned long long ula
, ulb
, ulc
;
2590 la
= (la
<< 20) | 0x12345;
2592 printf("la=" LONG_LONG_FORMAT
" ula=" ULONG_LONG_FORMAT
"\n", la
, ula
);
2597 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
2602 printf("ftoll: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", la
, lb
, lc
);
2607 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
2612 printf("ftoull: " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
"\n", ula
, ulb
, ulc
);
2615 long long llfunc1(int a
)
2625 long long int value(struct S
*v
)
2627 return ((long long int)v
->item
);
2630 long long llfunc2(long long x
, long long y
, int z
)
2635 void check_opl_save_regs(char *a
, long long b
, int c
)
2640 void longlong_test(void)
2645 printf("longlong_test:\n");
2646 printf("sizeof(long long) = %d\n", sizeof(long long));
2651 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2652 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %Lx\n",
2656 0x1234567812345679);
2658 printf(LONG_LONG_FORMAT
"\n", a
);
2661 lloptest(0xff, 0x1234);
2662 b
= 0x72345678 << 10;
2666 b
= 0x72345678LL
<< 10;
2677 /* long long reg spill test */
2682 printf("%lld\n", value(&a
));
2684 lloptest(0x80000000, 0);
2687 long long *p
, v
, **pp
;
2691 printf("another long long spill test : %lld\n", *p
);
2694 v
= llfunc2(**pp
, **pp
, ia
);
2695 printf("a long long function (arm-)reg-args test : %lld\n", v
);
2699 printf("%d %d %d %d\n", a
> b
, a
< b
, a
>= b
, a
<= b
);
2701 printf(LONG_LONG_FORMAT
"\n", 0x123456789LLU
);
2703 /* long long pointer deref in argument passing test */
2708 /* shortening followed by widening */
2709 unsigned long long u
= 0x8000000000000001ULL
;
2710 u
= (unsigned)(u
+ 1);
2711 printf("long long u=" ULONG_LONG_FORMAT
"\n", u
);
2712 u
= 0x11223344aa998877ULL
;
2713 u
= (unsigned)(int)(u
+ 1);
2714 printf("long long u=" ULONG_LONG_FORMAT
"\n", u
);
2716 /* was a problem with missing save_regs in gen_opl on 32-bit platforms */
2718 check_opl_save_regs(&cc
, -1, 0);
2719 printf("check_opl_save_regs: %d\n", cc
);
2722 void manyarg_test(void)
2724 LONG_DOUBLE ld
= 1234567891234LL;
2725 printf("manyarg_test:\n");
2726 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %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 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2730 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2731 1, 2, 3, 4, 5, 6, 7, 8,
2732 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2733 1234567891234LL, 987654321986LL,
2735 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2736 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2737 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2738 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2739 1234567891234LL, 987654321986LL,
2741 printf("%d %d %d %d %d %d %d %d %Lf\n",
2742 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2743 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2744 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"%f %f %Lf\n",
2745 1, 2, 3, 4, 5, 6, 7, 8,
2746 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2747 1234567891234LL, 987654321986LL,
2749 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2750 "%Lf " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f %Lf\n",
2751 1, 2, 3, 4, 5, 6, 7, 8,
2752 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2753 ld
, 1234567891234LL, 987654321986LL,
2758 va_arg_with_struct_ptr(va_list ap
) {
2760 * This was a BUG identified with FFTW-3.3.8 on arm64.
2761 * The test case only checks it compiles on all supported
2762 * architectures. This function is not currently called.
2764 struct X
{ int _x
; };
2765 struct X
*x
= va_arg(ap
, struct X
*);
2769 void vprintf1(const char *fmt
, ...)
2793 i
= va_arg(ap
, int);
2797 d
= va_arg(ap
, double);
2801 ll
= va_arg(ap
, long long);
2802 printf(LONG_LONG_FORMAT
, ll
);
2805 ld
= va_arg(ap
, LONG_DOUBLE
);
2823 void stdarg_for_struct(struct myspace bob
, ...)
2825 struct myspace george
, bill
;
2830 bill
= va_arg(ap
, struct myspace
);
2831 george
= va_arg(ap
, struct myspace
);
2832 validate
= va_arg(ap
, int);
2833 printf("stdarg_for_struct: %d %d %d %d\n",
2834 bob
.profile
, bill
.profile
, george
.profile
, validate
);
2838 void stdarg_for_libc(const char *fmt
, ...)
2841 va_start(args
, fmt
);
2846 void stdarg_syntax(int n
, ...)
2854 i
= va_arg(ap
, int);
2855 printf("stdarg_void_expr: %d\n", i
);
2859 void stdarg_test(void)
2861 LONG_DOUBLE ld
= 1234567891234LL;
2864 vprintf1("%d %d %d\n", 1, 2, 3);
2865 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2866 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2867 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2868 vprintf1("%d %f %l %F %d %f %l %F\n",
2869 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2870 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2871 1, 2, 3, 4, 5, 6, 7, 8,
2872 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2873 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
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 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2878 1, 2, 3, 4, 5, 6, 7, 8,
2879 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2880 1234567891234LL, 987654321986LL,
2882 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2884 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2885 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2886 1234567891234LL, 987654321986LL,
2888 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2889 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2890 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2892 1, 2, 3, 4, 5, 6, 7, 8,
2893 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2894 1234567891234LL, 987654321986LL,
2896 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2897 "%F %l %l %f %f %F\n",
2898 1, 2, 3, 4, 5, 6, 7, 8,
2899 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2900 ld
, 1234567891234LL, 987654321986LL,
2904 stdarg_for_struct(bob
, bob
, bob
, bob
.profile
);
2905 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2906 stdarg_syntax(1, 17);
2909 void whitespace_test(void)
2915 ntf("whitspace:\n");\f\v
2919 #ifdef CORRECT_CR_HANDLING
2928 #ifdef ACCEPT_CR_IN_STRINGS
2929 printf("len1=%d\n", strlen("
2931 #ifdef CORRECT_CR_HANDLING
2934 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
2936 printf("len1=%d\n", strlen("
a
2938 #endif /* ACCEPT_CR_IN_STRINGS */
2941 int reltab
[3] = { 1, 2, 3 };
2943 int *rel1
= &reltab
[1];
2944 int *rel2
= &reltab
[2];
2947 void relocation_test(void) {}
2949 void getmyaddress(void)
2951 printf("in getmyaddress\n");
2955 long __pa_symbol(void)
2957 /* This 64bit constant was handled incorrectly, it was used as addend
2958 (which can hold 64bit just fine) in connection with a symbol,
2959 and TCC generates wrong code for that (displacements are 32bit only).
2960 This effectively is "+ 0x80000000", and if addresses of globals
2961 are below 2GB the result should be a number without high 32 bits set. */
2962 return ((long)(((unsigned long)(&rel1
))) - (0xffffffff80000000UL
));
2966 unsigned long theaddress
= (unsigned long)getmyaddress
;
2967 void relocation_test(void)
2969 void (*fptr
)(void) = (void (*)(void))theaddress
;
2970 printf("*rel1=%d\n", *rel1
);
2971 printf("*rel2=%d\n", *rel2
);
2974 printf("pa_symbol=0x%lx\n", __pa_symbol() >> 63);
2979 void old_style_f(a
,b
,c
)
2983 printf("a=%d b=%d b=%f\n", a
, b
, c
);
2986 void decl_func1(int cmpfn())
2988 printf("cmpfn=%lx\n", (long)cmpfn
);
2991 void decl_func2(cmpfn
)
2994 printf("cmpfn=%lx\n", (long)cmpfn
);
2997 void old_style_function(void)
2999 old_style_f((void *)1, 2, 3.0);
3006 #if defined __i386__ || defined __x86_64__ || defined __arm__
3007 char *p
= alloca(16);
3008 strcpy(p
,"123456789012345");
3009 printf("alloca: p is %s\n", p
);
3010 char *demo
= "This is only a test.\n";
3011 /* Test alloca embedded in a larger expression */
3012 printf("alloca: %s\n", strcpy(alloca(strlen(demo
)+1),demo
) );
3016 void *bounds_checking_is_enabled()
3018 char ca
[10], *cp
= ca
-1;
3019 return (ca
!= cp
+ 1) ? cp
: NULL
;
3022 typedef int constant_negative_array_size_as_compile_time_assertion_idiom
[(1 ? 2 : 0) - 1];
3024 void c99_vla_test(int size1
, int size2
)
3026 #if defined __i386__ || defined __x86_64__
3027 int size
= size1
* size2
;
3028 int tab1
[size
][2], tab2
[10][2];
3029 void *tab1_ptr
, *tab2_ptr
, *bad_ptr
;
3031 /* "size" should have been 'captured' at tab1 declaration,
3032 so modifying it should have no effect on VLA behaviour. */
3035 printf("Test C99 VLA 1 (sizeof): ");
3036 printf("%s\n", (sizeof tab1
== size1
* size2
* 2 * sizeof(int)) ? "PASSED" : "FAILED");
3039 printf("Test C99 VLA 2 (ptrs subtract): ");
3040 printf("%s\n", (tab2
- tab1
== (tab2_ptr
- tab1_ptr
) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
3041 printf("Test C99 VLA 3 (ptr add): ");
3042 printf("%s\n", &tab1
[5][1] == (tab1_ptr
+ (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
3043 printf("Test C99 VLA 4 (ptr access): ");
3044 tab1
[size1
][1] = 42;
3045 printf("%s\n", (*((int *) (tab1_ptr
+ (size1
* 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
3047 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
3048 if (bad_ptr
= bounds_checking_is_enabled()) {
3049 int *t1
= &tab1
[size1
* size2
- 1][3];
3050 int *t2
= &tab2
[9][3];
3051 printf("%s ", bad_ptr
== t1
? "PASSED" : "FAILED");
3052 printf("%s ", bad_ptr
== t2
? "PASSED" : "FAILED");
3054 char*c1
= 1 + sizeof(tab1
) + (char*)tab1
;
3055 char*c2
= 1 + sizeof(tab2
) + (char*)tab2
;
3056 printf("%s ", bad_ptr
== c1
? "PASSED" : "FAILED");
3057 printf("%s ", bad_ptr
== c2
? "PASSED" : "FAILED");
3061 printf("%s ", bad_ptr
== i1
? "PASSED" : "FAILED");
3062 printf("%s ", bad_ptr
== i2
? "PASSED" : "FAILED");
3064 int *x1
= tab1
[size1
* size2
+ 1];
3065 int *x2
= tab2
[10 + 1];
3066 printf("%s ", bad_ptr
== x1
? "PASSED" : "FAILED");
3067 printf("%s ", bad_ptr
== x2
? "PASSED" : "FAILED");
3069 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
3075 void sizeof_test(void)
3080 printf("sizeof(int) = %d\n", sizeof(int));
3081 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
3082 printf("sizeof(long) = %d\n", sizeof(long));
3083 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
3084 printf("sizeof(short) = %d\n", sizeof(short));
3085 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
3086 printf("sizeof(char) = %d\n", sizeof(char));
3087 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
3088 printf("sizeof(func) = %d\n", sizeof sizeof_test());
3090 printf("sizeof(a++) = %d\n", sizeof a
++);
3091 printf("a=%d\n", a
);
3093 printf("sizeof(**ptr) = %d\n", sizeof (**ptr
));
3095 /* The type of sizeof should be as large as a pointer, actually
3096 it should be size_t. */
3097 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
3100 /* Effectively <<32, but defined also on 32bit machines. */
3104 /* This checks that sizeof really can be used to manipulate
3105 uintptr_t objects, without truncation. */
3106 t2
= t
& -sizeof(uintptr_t);
3107 printf ("%lu %lu\n", t
, t2
);
3109 /* some alignof tests */
3110 printf("__alignof__(int) = %d\n", __alignof__(int));
3111 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
3112 printf("__alignof__(short) = %d\n", __alignof__(short));
3113 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
3114 printf("__alignof__(char) = %d\n", __alignof__(char));
3115 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
3116 printf("__alignof__(func) = %d\n", __alignof__
sizeof_test());
3118 /* sizes of VLAs need to be evaluated even inside sizeof: */
3120 printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a
]));
3121 /* And checking if sizeof compound literal works. Parenthesized: */
3122 printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n",
3123 sizeof( (struct {int i
; int j
;}){4,5} ));
3124 /* And as direct sizeof argument (as unary expression): */
3125 printf("sizeof (struct {short i; short j;}){4,5} = %d\n",
3126 sizeof (struct {short i
; short j
;}){4,5} );
3128 /* sizeof(x && y) should be sizeof(int), even if constant
3129 evaluating is possible. */
3130 printf("sizeof(t && 0) = %d\n", sizeof(t
&& 0));
3131 printf("sizeof(1 && 1) = %d\n", sizeof(1 && 1));
3132 printf("sizeof(t || 1) = %d\n", sizeof(t
|| 1));
3133 printf("sizeof(0 || 0) = %d\n", sizeof(0 || 0));
3136 void typeof_test(void)
3145 printf("a=%f b=%f c=%f\n", a
, b
, c
);
3151 struct hlist_node
*first
, *last
;
3154 void consume_ulong (unsigned long i
)
3159 void statement_expr_test(void)
3163 /* Basic stmt expr test */
3173 printf("a=%d\n", a
);
3175 /* Test that symbols aren't freed prematurely.
3176 With SYM_DEBUG valgrind will show a read from a freed
3177 symbol, and tcc will show an (invalid) warning on the initialization
3178 of 'ptr' below, if symbols are popped after the stmt expr. */
3179 void *v
= (void*)39;
3181 (struct hlist_node
*)v
;
3184 ptr
= (struct hlist_node
*)v
;
3186 /* This part used to segfault when symbols were popped prematurely.
3187 The symbols for the static local would be overwritten with
3188 helper symbols from the pre-processor expansions in between. */
3189 #define some_attr __attribute__((aligned(1)))
3190 #define tps(str) ({ \
3191 static const char *t some_attr = str; \
3194 printf ("stmtexpr: %s %s\n",
3195 tps("somerandomlongstring"),
3196 tps("anotherlongstring"));
3198 /* Test that the three decls of 't' don't interact. */
3200 int b
= ({ int t
= 41; t
; });
3201 int c
= ({ int t
= 42; t
; });
3203 /* Test that aggregate return values work. */
3206 typedef struct hlist_head T
;
3208 T t
= { (void*)43, (void*)44 };
3212 printf ("stmtexpr: %d %d %d\n", t
, b
, c
);
3213 printf ("stmtexpr: %ld %ld\n", (long)h
.first
, (long)h
.last
);
3215 /* Test that we can give out addresses of local labels. */
3216 consume_ulong(({ __label__ __here
; __here
: (unsigned long)&&__here
; }));
3218 /* Test interaction between local and global label stacks and the
3219 need to defer popping symbol from them when within statement
3220 expressions. Note how the labels are both named LBL. */
3225 LBL
: if (i
++ == 0) goto LBL
;
3227 /* jump to a classical label out of an expr-stmt that had previously
3228 overshadowed that classical label */
3232 printf("stmtexpr: %d should be 2\n", i
);
3235 void local_label_test(void)
3241 __label__ l1
, l2
, l3
, l4
;
3255 printf("a=%d\n", a
);
3265 /* inline assembler test */
3266 #if defined(__i386__) || defined(__x86_64__)
3268 /* from linux kernel */
3269 static char * strncat1(char * dest
,const char * src
,size_t count
)
3271 long d0
, d1
, d2
, d3
;
3272 __asm__
__volatile__(
3281 "testb %%al,%%al\n\t"
3285 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
3286 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
3291 static char * strncat2(char * dest
,const char * src
,size_t count
)
3293 long d0
, d1
, d2
, d3
;
3294 __asm__
__volatile__(
3295 "repne scasb\n\t" /* one-line repne prefix + string op */
3302 "testb %%al,%%al\n\t"
3306 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
3307 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
3312 static inline void * memcpy1(void * to
, const void * from
, size_t n
)
3315 __asm__
__volatile__(
3320 "1:\ttestb $1,%b4\n\t"
3324 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
3325 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
3330 static inline void * memcpy2(void * to
, const void * from
, size_t n
)
3333 __asm__
__volatile__(
3334 "rep movsl\n\t" /* one-line rep prefix + string op */
3338 "1:\ttestb $1,%b4\n\t"
3342 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
3343 :"0" (n
/4), "q" (n
),"1" ((long) to
),"2" ((long) from
)
3348 static __inline__
void sigaddset1(unsigned int *set
, int _sig
)
3350 __asm__("btsl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
3353 static __inline__
void sigdelset1(unsigned int *set
, int _sig
)
3355 asm("btrl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc", "flags");
3359 /* clang's inline asm is uncapable of 'xchgb %b0,%h0' */
3360 static __inline__ __const__
unsigned int swab32(unsigned int x
)
3362 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
3363 "rorl $16,%0\n\t" /* swap words */
3364 "xchgb %b0,%h0" /* swap higher bytes */
3371 static __inline__
unsigned long long mul64(unsigned int a
, unsigned int b
)
3373 unsigned long long res
;
3375 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
3376 but still test the 32bit->64bit mull. */
3377 unsigned int resh
, resl
;
3378 __asm__("mull %2" : "=a" (resl
), "=d" (resh
) : "a" (a
), "r" (b
));
3379 res
= ((unsigned long long)resh
<< 32) | resl
;
3381 __asm__("mull %2" : "=A" (res
) : "a" (a
), "r" (b
));
3386 static __inline__
unsigned long long inc64(unsigned long long a
)
3388 unsigned long long res
;
3390 /* Using the A constraint is wrong, and increments are tested
3394 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res
) : "A" (a
));
3407 unsigned long mconstraint_test(struct struct1231
*r
)
3412 __asm__
volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
3413 : "=&r" (ret
), "=m" (a
)
3414 : "m" (*(struct struct123
*)r
->addr
));
3419 int fls64(unsigned long long x
)
3429 void other_constraints_test(void)
3434 __asm__
volatile ("mov %P1,%0" : "=r" (ret
) : "p" (&var
));
3435 printf ("oc1: %d\n", ret
== (unsigned long)&var
);
3440 /* Test global asm blocks playing with aliases. */
3441 void base_func(void)
3443 printf ("asmc: base\n");
3447 extern void override_func1 (void);
3448 extern void override_func2 (void);
3450 asm(".weak override_func1\n.set override_func1, base_func");
3451 asm(".set override_func1, base_func");
3452 asm(".set override_func2, base_func");
3454 void override_func2 (void)
3456 printf ("asmc: override2\n");
3459 /* This checks a construct used by the linux kernel to encode
3460 references to strings by PC relative references. */
3461 extern int bug_table
[] __attribute__((section("__bug_table")));
3462 char * get_asm_string (void)
3464 /* On i386 when -fPIC is enabled this would cause a compile error with GCC,
3465 the problem being the "i" constraint used with a symbolic operand
3466 resolving to a local label. That check is overly zealous as the code
3467 within the asm makes sure to use it only in PIC-possible contexts,
3468 but all GCC versions behave like so. We arrange for PIC to be disabled
3469 for compiling tcctest.c in the Makefile.
3471 Additionally the usage of 'c' in "%c0" in the template is actually wrong,
3472 as that would expect an operand that is a condition code. The operand
3473 as is (a local label) is accepted by GCC in non-PIC mode, and on x86-64.
3474 What the linux kernel really wanted is 'p' to disable the addition of '$'
3475 to the printed operand (as in "$.LC0" where the template only wants the
3476 bare operand ".LC0"). But the code below is what the linux kernel
3477 happens to use and as such is the one we want to test. */
3479 extern int some_symbol
;
3480 asm volatile (".globl some_symbol\n"
3483 "some_symbol: .long 0\n"
3484 ".pushsection __bug_table, \"a\"\n"
3485 ".globl bug_table\n"
3487 /* The first entry (1b-2b) is unused in this test,
3488 but we include it to check if cross-section
3489 PC-relative references work. */
3490 "2:\t.long 1b - 2b, %c0 - 2b\n"
3491 ".popsection\n" : : "i" ("A string"));
3492 char * str
= ((char*)bug_table
) + bug_table
[1];
3495 return (char *) "A string";
3499 /* This checks another constructs with local labels. */
3500 extern unsigned char alld_stuff
[];
3507 ".pushsection .data.ignore\n"
3508 ".long 661b - .\n" /* This reference to 661 generates an external sym
3509 which shouldn't somehow overwrite the offset that's
3510 already determined for it. */
3512 ".byte 662b - 661b\n" /* So that this value is undeniably 1. */);
3514 void asm_local_label_diff (void)
3516 printf ("asm_local_label_diff: %d %d\n", alld_stuff
[0], alld_stuff
[1]);
3520 /* This checks that static local variables are available from assembler. */
3521 void asm_local_statics (void)
3523 static int localint
= 41;
3524 asm("incl %0" : "+m" (localint
));
3525 printf ("asm_local_statics: %d\n", localint
);
3532 void fancy_copy (unsigned *in
, unsigned *out
)
3534 asm volatile ("" : "=r" (*out
) : "0" (*in
));
3537 void fancy_copy2 (unsigned *in
, unsigned *out
)
3539 asm volatile ("mov %0,(%1)" : : "r" (*in
), "r" (out
) : "memory");
3542 #if defined __x86_64__ && !defined _WIN64
3543 void clobber_r12(void)
3545 asm volatile("mov $1, %%r12" ::: "r12");
3549 void test_high_clobbers_really(void)
3551 #if defined __x86_64__ && !defined _WIN64
3552 register long val
asm("r12");
3554 /* This tests if asm clobbers correctly save/restore callee saved
3555 registers if they are clobbered and if it's the high 8 x86-64
3556 registers. This is fragile for GCC as the constraints do not
3557 correctly capture the data flow, but good enough for us. */
3558 asm volatile("mov $0x4542, %%r12" : "=r" (val
):: "memory");
3560 asm volatile("mov %%r12, %0" : "=r" (val2
) : "r" (val
): "memory");
3561 printf("asmhc: 0x%x\n", val2
);
3565 void test_high_clobbers(void)
3567 #if defined __x86_64__ && !defined _WIN64
3569 asm volatile("mov %%r12,%0" :: "m" (x1
)); /* save r12 */
3570 test_high_clobbers_really();
3571 asm volatile("mov %%r12,%0" :: "m" (x2
)); /* new r12 */
3572 asm volatile("mov %0,%%r12" :: "m" (x1
)); /* restore r12 */
3573 /* should be 0 but tcc doesn't save r12 automatically, which has
3574 bad effects when gcc helds TCCState *s in r12 in tcc.c:main */
3575 //printf("r12-clobber-diff: %lx\n", x2 - x1);
3579 static long cpu_number
;
3580 void trace_console(long len
, long len2
)
3583 /* This generated invalid code when the emission of the switch
3584 table isn't disabled. The asms are necessary to show the bug,
3585 normal statements don't work (they need to generate some code
3586 even under nocode_wanted, which normal statements don't do,
3587 but asms do). Also at least these number of cases is necessary
3588 to generate enough "random" bytes. They ultimately are enough
3589 to create invalid instruction patterns to which the first
3590 skip-to-decision-table jump jumps. If decision table emission
3591 is disabled all of this is no problem.
3593 It also is necessary that the switches are in a statement expression
3594 (which has the property of not being enterable from outside. no
3605 case 8: printf("bla"); pfo_ret__
= 42; break;
3607 pscr_ret__
= pfo_ret__
;
3614 case 1:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3615 case 2:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3616 case 4:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3617 case 8:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3618 default: printf("impossible\n");
3620 pscr_ret__
= pfo_ret__
;
3632 void test_asm_dead_code(void)
3635 /* Try to make sure that xdi contains a zero, and hence will
3636 lead to a segfault if the next asm is evaluated without
3637 arguments being set up. */
3638 asm volatile ("" : "=D" (rdi
) : "0" (0));
3641 /* This shouldn't trigger a segfault, either the argument
3642 registers need to be set up and the asm emitted despite
3643 this being in an unevaluated context, or both the argument
3644 setup _and_ the asm emission need to be suppressed. The latter
3645 is better. Disabling asm code gen when suppression is on
3646 also fixes the above trace_console bug, but that came earlier
3647 than asm suppression. */
3648 asm volatile ("movl $0,(%0)" : : "D" (&var
) : "memory");
3653 void test_asm_call(void)
3655 #if defined __x86_64__ && !defined _WIN64
3656 static char str
[] = "PATH";
3658 /* This tests if a reference to an undefined symbol from an asm
3659 block, which isn't otherwise referenced in this file, is correctly
3660 regarded as global symbol, so that it's resolved by other object files
3661 or libraries. We chose getenv here, which isn't used anywhere else
3662 in this file. (If we used e.g. printf, which is used we already
3663 would have a global symbol entry, not triggering the bug which is
3665 /* two pushes so stack remains aligned */
3666 asm volatile ("push %%rdi; push %%rdi; mov %0, %%rdi;"
3667 #if 1 && !defined(__TINYC__) && (defined(__PIC__) || defined(__PIE__)) && !defined(__APPLE__)
3669 #elif defined(__APPLE__)
3674 "pop %%rdi; pop %%rdi"
3675 : "=a" (s
) : "r" (str
));
3676 printf("asmd: %s\n", s
);
3680 #if defined __x86_64__
3681 # define RX "(%rip)"
3686 void asm_dot_test(void)
3694 asm(".text; lea S"RX
",%eax; lea ."RX
",%ecx; sub %ecx,%eax; S=.; jmp p0");
3697 /* clangs internal assembler is broken */
3698 asm(".text; jmp .+6; .int 123; mov .-4"RX
",%eax; jmp p0");
3700 asm(".text; mov $123, %eax; jmp p0");
3703 #if !defined(_WIN32) && !defined(__clang__)
3704 asm(".pushsection \".data\"; Y=.; .int 999; X=Y; .int 456; X=.-4; .popsection");
3706 asm(".data; Y=.; .int 999; X=Y; .int 456; X=.-4; .text");
3708 asm(".text; mov X"RX
",%eax; jmp p0");
3711 /* Bah! Clang! Doesn't want to redefine 'X' */
3712 asm(".text; mov $789,%eax; jmp p0");
3715 asm(".data; X=.; .int 789; Y=.; .int 999; .previous");
3717 asm(".data; X=.; .int 789; Y=.; .int 999; .text");
3719 asm(".text; mov X"RX
",%eax; X=Y; jmp p0");
3722 asm(".text; p0=.; mov %%eax,%0;" : "=m"(r
)); break;
3726 printf("asm_dot_test %d: %d\n", x
, r
);
3734 unsigned int val
, val2
;
3735 struct struct123 s1
;
3736 struct struct1231 s2
= { (unsigned long)&s1
};
3737 /* Hide the outer base_func, but check later that the inline
3738 asm block gets the outer one. */
3740 void override_func3 (void);
3741 unsigned long asmret
;
3745 register int regvar
asm("%esi");
3747 printf("inline asm:\n");
3749 // parse 0x1E-1 as 3 tokens in asm mode
3750 asm volatile ("mov $0x1E-1,%eax");
3752 /* test the no operand case */
3753 asm volatile ("xorl %eax, %eax");
3755 memcpy1(buf
, "hello", 6);
3756 strncat1(buf
, " worldXXXXX", 3);
3757 printf("%s\n", buf
);
3759 memcpy2(buf
, "hello", 6);
3760 strncat2(buf
, " worldXXXXX", 3);
3761 printf("%s\n", buf
);
3763 /* 'A' constraint test */
3764 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
3765 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
3769 printf("mconstraint: %d", mconstraint_test(&s2
));
3770 printf(" %d %d\n", s1
.a
, s1
.b
);
3771 other_constraints_test();
3773 sigdelset1(&set
, 2);
3774 sigaddset1(&set
, 16);
3775 /* NOTE: we test here if C labels are correctly restored after the
3779 __asm__("btsl %1,%0" : "=m"(set
) : "Ir"(20) : "cc");
3780 printf("set=0x%x\n", set
);
3783 printf("swab32(0x%08x) = 0x%0x\n", val
, swab32(val
));
3789 /* The base_func ref from the following inline asm should find
3790 the global one, not the local decl from this function. */
3791 asm volatile(".weak override_func3\n.set override_func3, base_func");
3793 printf("asmstr: %s\n", get_asm_string());
3794 asm_local_label_diff();
3796 asm_local_statics();
3799 /* clang can't deal with the type change */
3800 /* Check that we can also load structs of appropriate layout
3802 asm volatile("" : "=r" (asmret
) : "0"(s2
));
3803 if (asmret
!= s2
.addr
)
3804 printf("asmstr: failed\n");
3807 /* Check that the typesize correctly sets the register size to
3809 asm volatile("cmp %1,%2; sete %0" : "=a"(somebool
) : "r"(1), "r"(2));
3811 printf("asmbool: failed\n");
3814 fancy_copy (&val
, &val2
);
3815 printf ("fancycpy(%d)=%d\n", val
, val2
);
3817 fancy_copy2 (&val
, &val2
);
3818 printf ("fancycpy2(%d)=%d\n", val
, val2
);
3819 asm volatile ("mov $0x4243, %%esi" : "=r" (regvar
));
3820 printf ("regvar=%x\n", regvar
);
3821 test_high_clobbers();
3822 trace_console(8, 8);
3823 test_asm_dead_code();
3839 #define COMPAT_TYPE(type1, type2) \
3841 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
3842 __builtin_types_compatible_p (type1, type2));\
3847 void builtin_test(void)
3853 COMPAT_TYPE(int, int);
3854 COMPAT_TYPE(int, unsigned int);
3855 COMPAT_TYPE(int, char);
3856 COMPAT_TYPE(int, const int);
3857 COMPAT_TYPE(int, volatile int);
3858 COMPAT_TYPE(int *, int *);
3859 COMPAT_TYPE(int *, void *);
3860 COMPAT_TYPE(int *, const int *);
3861 COMPAT_TYPE(char *, unsigned char *);
3862 COMPAT_TYPE(char *, signed char *);
3863 COMPAT_TYPE(char *, char *);
3864 /* space is needed because tcc preprocessor introduces a space between each token */
3865 COMPAT_TYPE(char * *, void *);
3867 printf("res1 = %d\n", __builtin_constant_p(1));
3868 printf("res2 = %d\n", __builtin_constant_p(1 + 2));
3869 printf("res3 = %d\n", __builtin_constant_p(&constant_p_var
));
3870 printf("res4 = %d\n", __builtin_constant_p(constant_p_var
));
3871 printf("res5 = %d\n", __builtin_constant_p(100000 / constant_p_var
));
3873 /* clang doesn't regard this as constant expression */
3874 printf("res6 = 1\n");
3876 printf("res6 = %d\n", __builtin_constant_p(i
&& 0));
3878 printf("res7 = %d\n", __builtin_constant_p(i
&& 1));
3880 /* clang doesn't regard this as constant expression */
3881 printf("res8 = 1\n");
3883 printf("res8 = %d\n", __builtin_constant_p(i
&& 0 ? i
: 34));
3887 i
= __builtin_choose_expr (1 != 0, ll
, s
);
3888 printf("bce: %d\n", i
);
3889 i
= __builtin_choose_expr (1 != 1, ll
, s
);
3890 printf("bce: %d\n", i
);
3891 i
= sizeof (__builtin_choose_expr (1, ll
, s
));
3892 printf("bce: %d\n", i
);
3893 i
= sizeof (__builtin_choose_expr (0, ll
, s
));
3894 printf("bce: %d\n", i
);
3896 //printf("bera: %p\n", __builtin_extract_return_addr((void*)43));
3900 extern int __attribute__((weak
)) weak_f1(void);
3901 extern int __attribute__((weak
)) weak_f2(void);
3902 extern int weak_f3(void);
3903 extern int __attribute__((weak
)) weak_v1
;
3904 extern int __attribute__((weak
)) weak_v2
;
3907 extern int (*weak_fpa
)() __attribute__((weak
));
3908 extern int __attribute__((weak
)) (*weak_fpb
)();
3909 extern __attribute__((weak
)) int (*weak_fpc
)();
3911 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak
));
3912 extern int __attribute((weak
)) weak_asm_f2(void) asm("weak_asm_f2x") ;
3913 extern int __attribute((weak
)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak
));
3914 extern int weak_asm_v1
asm("weak_asm_v1x") __attribute((weak
));
3915 extern int __attribute((weak
)) weak_asm_v2
asm("weak_asm_v2x") ;
3916 extern int __attribute((weak
)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak
));
3919 static const size_t dummy
= 0;
3920 extern __typeof(dummy
) weak_dummy1
__attribute__((weak
, alias("dummy")));
3921 extern __typeof(dummy
) __attribute__((weak
, alias("dummy"))) weak_dummy2
;
3922 extern __attribute__((weak
, alias("dummy"))) __typeof(dummy
) weak_dummy3
;
3925 int some_lib_func(void);
3926 int dummy_impl_of_slf(void) { return 444; }
3928 int some_lib_func(void) __attribute__((weak
, alias("dummy_impl_of_slf")));
3931 int weak_toolate() __attribute__((weak
));
3932 int weak_toolate() { return 0; }
3934 void __attribute__((weak
)) weak_test(void)
3936 printf("weak_f1=%d\n", weak_f1
? weak_f1() : 123);
3937 printf("weak_f2=%d\n", weak_f2
? weak_f2() : 123);
3938 printf("weak_f3=%d\n", weak_f3
? weak_f3() : 123);
3939 printf("weak_v1=%d\n",&weak_v1
? weak_v1
: 123);
3940 printf("weak_v2=%d\n",&weak_v2
? weak_v2
: 123);
3941 printf("weak_v3=%d\n",&weak_v3
? weak_v3
: 123);
3943 printf("weak_fpa=%d\n",&weak_fpa
? weak_fpa() : 123);
3944 printf("weak_fpb=%d\n",&weak_fpb
? weak_fpb() : 123);
3945 printf("weak_fpc=%d\n",&weak_fpc
? weak_fpc() : 123);
3947 printf("weak_asm_f1=%d\n", weak_asm_f1
!= NULL
);
3948 printf("weak_asm_f2=%d\n", weak_asm_f2
!= NULL
);
3949 printf("weak_asm_f3=%d\n", weak_asm_f3
!= NULL
);
3950 printf("weak_asm_v1=%d\n",&weak_asm_v1
!= NULL
);
3951 printf("weak_asm_v2=%d\n",&weak_asm_v2
!= NULL
);
3952 printf("weak_asm_v3=%d\n",&weak_asm_v3
!= NULL
);
3954 printf("some_lib_func=444\n");
3956 printf("some_lib_func=%d\n", &some_lib_func
? some_lib_func() : 0);
3960 int __attribute__((weak
)) weak_f2() { return 222; }
3961 int __attribute__((weak
)) weak_f3() { return 333; }
3962 int __attribute__((weak
)) weak_v2
= 222;
3963 int __attribute__((weak
)) weak_v3
= 333;
3966 void const_func(const int a
)
3970 void const_warn_test(void)
3979 int getme (struct condstruct
*s
, int i
)
3981 int i1
= (i
== 0 ? 0 : s
)->i
;
3982 int i2
= (i
== 0 ? s
: 0)->i
;
3983 int i3
= (i
== 0 ? (void*)0 : s
)->i
;
3984 int i4
= (i
== 0 ? s
: (void*)0)->i
;
3985 return i1
+ i2
+ i3
+ i4
;
3994 struct global_data global_data
;
3996 int global_data_getstuff (int *, int);
3998 void global_data_callit (int i
)
4000 *global_data
.b
[i
] = global_data_getstuff (global_data
.b
[i
], 1);
4003 int global_data_getstuff (int *p
, int i
)
4008 void global_data_test (void)
4010 global_data
.a
[0] = 42;
4011 global_data
.b
[0] = &global_data
.a
[0];
4012 global_data_callit (0);
4013 printf ("%d\n", global_data
.a
[0]);
4018 unsigned char fill
: 3;
4019 unsigned char b1
: 1;
4020 unsigned char b2
: 1;
4021 unsigned char fill2
: 3;
4024 int glob1
, glob2
, glob3
;
4026 void compare_comparisons (struct cmpcmpS
*s
)
4028 if (s
->b1
!= (glob1
== glob2
)
4029 || (s
->b2
!= (glob1
== glob3
)))
4030 printf ("comparing comparisons broken\n");
4033 void cmp_comparison_test(void)
4037 glob1
= 42; glob2
= 42;
4040 compare_comparisons (&s
);
4043 int fcompare (double a
, double b
, int code
)
4046 case 0: return a
== b
;
4047 case 1: return a
!= b
;
4048 case 2: return a
< b
;
4049 case 3: return a
>= b
;
4050 case 4: return a
> b
;
4051 case 5: return a
<= b
;
4056 void math_cmp_test(void)
4058 double nan
= 0.0/0.0;
4063 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
4065 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
4066 And it does this in various ways so that all code generation paths
4067 are checked (generating inverted tests, or non-inverted tests, or
4068 producing a 0/1 value without jumps (that's done in the fcompare
4070 #define FCMP(a,b,op,iop,code) \
4071 if (fcompare (a,b,code)) \
4072 bug (a,b,op,iop,1); \
4074 bug (a,b,op,iop,2); \
4078 bug (a,b,op,iop,3); \
4079 if ((a op b) || comp) \
4080 bug (a,b,op,iop,4); \
4081 if ((a iop b) || comp) \
4084 bug (a,b,op,iop,5); \
4085 if (v = !(a op b), !v) bug(a,b,op,iop,7);
4087 /* Equality tests. */
4088 FCMP(nan
, nan
, ==, !=, 0);
4089 FCMP(one
, two
, ==, !=, 0);
4090 FCMP(one
, one
, !=, ==, 1);
4091 /* Non-equality is a bit special. */
4092 if (!fcompare (nan
, nan
, 1))
4093 bug (nan
, nan
, !=, ==, 6);
4095 /* Relational tests on numbers. */
4096 FCMP(two
, one
, <, >=, 2);
4097 FCMP(one
, two
, >=, <, 3);
4098 FCMP(one
, two
, >, <=, 4);
4099 FCMP(two
, one
, <=, >, 5);
4101 /* Relational tests on NaNs. Note that the inverse op here is
4102 always !=, there's no operator in C that is equivalent to !(a < b),
4103 when NaNs are involved, same for the other relational ops. */
4104 FCMP(nan
, nan
, <, !=, 2);
4105 FCMP(nan
, nan
, >=, !=, 3);
4106 FCMP(nan
, nan
, >, !=, 4);
4107 FCMP(nan
, nan
, <=, !=, 5);
4110 double get100 () { return 100.0; }
4112 void callsave_test(void)
4114 #if defined __i386__ || defined __x86_64__ || defined __arm__
4115 int i
, s
; double *d
; double t
;
4116 s
= sizeof (double);
4117 printf ("callsavetest: %d\n", s
);
4118 d
= alloca (sizeof(double));
4120 /* x86-64 had a bug were the next call to get100 would evict
4121 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
4122 in int type, not pointer type. When alloca returns a pointer
4123 with the high 32 bit set (which is likely on x86-64) the access
4124 generates a segfault. */
4125 i
= d
[0] > get100 ();
4131 void bfa3(ptrdiff_t str_offset
)
4133 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset
);
4135 void bfa2(ptrdiff_t str_offset
)
4137 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset
);
4140 void bfa1(ptrdiff_t str_offset
)
4142 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset
);
4146 void builtin_frame_address_test(void)
4148 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
4150 char str
[] = "__builtin_frame_address";
4151 char *fp0
= __builtin_frame_address(0);
4153 printf("str: %s\n", str
);
4160 char via_volatile (char i
)
4167 struct __attribute__((__packed__
)) Spacked
{
4172 struct Spacked spacked
;
4173 typedef struct __attribute__((__packed__
)) {
4179 typedef struct Spacked3_s
{
4183 } __attribute__((__packed__
)) Spacked3
;
4185 struct gate_struct64
{
4186 unsigned short offset_low
;
4187 unsigned short segment
;
4188 unsigned ist
: 3, zero0
: 5, type
: 5, dpl
: 2, p
: 1;
4189 unsigned short offset_middle
;
4190 unsigned offset_high
;
4192 } __attribute__((packed
));
4193 typedef struct gate_struct64 gate_desc
;
4194 gate_desc a_gate_desc
;
4195 void attrib_test(void)
4198 printf("attr: %d %d %d %d\n", sizeof(struct Spacked
),
4199 sizeof(spacked
), sizeof(Spacked2
), sizeof(spacked2
));
4200 printf("attr: %d %d\n", sizeof(Spacked3
), sizeof(spacked3
));
4201 printf("attr: %d %d\n", sizeof(gate_desc
), sizeof(a_gate_desc
));
4204 extern __attribute__((__unused__
)) char * __attribute__((__unused__
)) *
4205 strange_attrib_placement (void);
4207 void * __attribute__((__unused__
)) get_void_ptr (void *a
)
4212 /* This part checks for a bug in TOK_GET (used for inline expansion),
4213 where the large long long constant left the the high bits set for
4214 the integer constant token. */
4216 int __get_order(unsigned long long size
)
4219 size
-= 0xffff880000000000ULL
; // this const left high bits set in the token
4221 struct S
{ int i
: 1; } s
; // constructed for this '1'
4227 /* This just forces the above inline function to be actually emitted. */
4228 int force_get_order(unsigned long s
)
4230 return __get_order(s
);
4233 #define pv(m) printf(sizeof (s->m + 0) == 8 ? "%016llx\n" : "%02x\n", s->m)
4235 /* Test failed when using bounds checking */
4236 void bounds_check1_test (void)