2 * TCC auto test program
6 /* identify the configured reference compiler in use */
11 /* Unfortunately, gcc version < 3 does not handle that! */
14 /* only gcc 3 handles _Bool correctly */
17 /* __VA_ARGS__ and __func__ support */
21 typedef __SIZE_TYPE__
uintptr_t;
25 || (defined(__arm__) \
26 && (defined(__FreeBSD__) \
27 || defined(__OpenBSD__) \
28 || defined(__NetBSD__) \
29 || defined __ANDROID__))
30 #define LONG_LONG_FORMAT "%lld"
31 #define ULONG_LONG_FORMAT "%llu"
32 #define XLONG_LONG_FORMAT "%llx"
34 #define LONG_LONG_FORMAT "%Ld"
35 #define ULONG_LONG_FORMAT "%Lu"
36 #define XLONG_LONG_FORMAT "%Lx"
39 // MinGW has 80-bit rather than 64-bit long double which isn't compatible with TCC or MSVC
40 #if defined(_WIN32) && defined(__GNUC__)
41 #define LONG_DOUBLE double
42 #define LONG_DOUBLE_LITERAL(x) x
44 #define LONG_DOUBLE long double
45 #define LONG_DOUBLE_LITERAL(x) x ## L
48 /* test various include syntaxes */
50 #define TCCLIB_INC <tcclib.h>
51 #define TCCLIB_INC1 <tcclib
52 #define TCCLIB_INC2 h>
53 #define TCCLIB_INC3 "tcclib.h"
57 #include TCCLIB_INC1.TCCLIB_INC2
59 #include TCCLIB_INC1.h>
69 /* Test two more ways to include a file named like a pp-number */
70 #define INC(name) <tests/name.h>
71 #define funnyname 42test.h
74 /* clang's preprocessor is broken in this regard and adds spaces
75 to the tokens 'incdir' and 'funnyname' when expanding */
76 #define incname <tests/42test.h>
78 #define incname < incdir funnyname >
80 #define __stringify(x) #x
81 #define stringify(x) __stringify(x)
84 #include stringify(funnyname)
86 int puts(const char *s
);
87 void *alloca(size_t size
);
91 void forward_ref(void);
94 /* Line joining happens before tokenization, so the following
95 must be parsed as ellipsis. */
96 void funny_line_continuation (int, ..\
102 #define M1(a, b) (a) + (b)
106 #define glue(a, b) a ## b
107 #define xglue(a, b) glue(a, b)
108 #define HIGHLOW "hello"
109 #define LOW LOW ", world"
111 static int onetwothree
= 123;
112 #define onetwothree4 onetwothree
113 #define onetwothree xglue(onetwothree,4)
115 #define min(a, b) ((a) < (b) ? (a) : (b))
118 #define dprintf(level,...) printf(__VA_ARGS__)
121 /* gcc vararg macros */
122 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
124 #define MACRO_NOARGS()
126 #define TEST_CALL(f, ...) f(__VA_ARGS__)
127 #define TEST_CONST() 123
144 /* We try to handle this syntax. Make at least sure it doesn't segfault. */
145 char invalid_function_def()[] {return 0;}
148 #define __INT64_C(c) c ## LL
149 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
157 #define spin_lock(lock) do { } while (0)
158 #define wq_spin_lock spin_lock
159 #define TEST2() wq_spin_lock(a)
161 void macro_test(void)
164 printf("aaa=%d\n", AAA
);
166 printf("min=%d\n", min(1, min(2, -1)));
168 printf("s1=%s\n", glue(HIGH
, LOW
));
169 printf("s2=%s\n", xglue(HIGH
, LOW
));
170 printf("s3=%s\n", str("c"));
171 printf("s4=%s\n", str(a1
));
172 printf("B3=%d\n", B3
);
174 printf("onetwothree=%d\n", onetwothree
);
177 printf("A defined\n");
180 printf("B defined\n");
183 printf("A defined\n");
185 printf("A not defined\n");
188 printf("B defined\n");
190 printf("B not defined\n");
194 printf("A defined\n");
196 printf("B1 defined\n");
198 printf("B1 not defined\n");
201 printf("A not defined\n");
203 printf("B2 defined\n");
205 printf("B2 not defined\n");
210 printf("test true1\n");
213 printf("test true2\n");
216 printf("test true3\n");
219 printf("test trueA\n");
222 printf("test trueB\n");
237 printf("%d\n", TEST_CALL(TEST_CONST
));
239 /* not strictly preprocessor, but we test it there */
241 printf("__func__ = %s\n", __func__
);
242 dprintf(1, "vaarg=%d\n", 1);
244 dprintf1(1, "vaarg1\n");
245 dprintf1(1, "vaarg1=%d\n", 2);
246 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
249 printf("func='%s'\n", __FUNCTION__
);
251 /* complicated macros in glibc */
252 printf("INT64_MIN=" LONG_LONG_FORMAT
"\n", INT64_MIN
);
262 /* macro function with argument outside the macro string */
263 #define MF_s MF_hello
264 #define MF_hello(msg) printf("%s\n",msg)
266 #define MF_t printf("tralala\n"); MF_hello
271 /* test macro substitution inside args (should not eat stream) */
272 printf("qq=%d\n", qq(qq
)(2));
274 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
275 null argument without a space. gcc 3.2 fixes that. */
278 printf("qq1=%d\n", qq1( ));
280 /* comment with stray handling *\
282 /* this is a valid *\/ comment */
283 /* this is a valid comment *\*/
287 /* test function macro substitution when the function name is
291 /* And again when the name and parentheses are separated by a
293 TEST2
/* the comment */ ();
295 printf("basefromheader %s\n", get_basefile_from_header());
296 printf("base %s\n", __BASE_FILE__
);
298 /* Some compilers (clang) prepend './' to __FILE__ from included
300 const char *fn
= get_file_from_header();
301 if (fn
[0] == '.' && fn
[1] == '/')
303 printf("filefromheader %s\n", fn
);
305 printf("file %s\n", __FILE__
);
307 /* Check that funnily named include was in fact included */
308 have_included_42test_h
= 1;
309 have_included_42test_h_second
= 1;
310 have_included_42test_h_third
= 1;
312 /* Check that we don't complain about stray \ here */
313 printf("print a backslash: %s\n", stringify(\\));
317 static void print_num(char *fn
, int line
, int num
) {
318 printf("fn %s, line %d, num %d\n", fn
, line
, num
);
321 void recursive_macro_test(void)
324 #define ELF32_ST_TYPE(val) ((val) & 0xf)
325 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
326 #define STB_WEAK 2 /* Weak symbol */
327 #define ELFW(type) ELF##32##_##type
328 printf("%d\n", ELFW(ST_INFO
)(STB_WEAK
, ELFW(ST_TYPE
)(123)));
332 #define print_num(x) print_num(__FILE__,__LINE__,x)
334 WRAP(print_num(123));
335 WRAP(WRAP(print_num(123)));
337 static struct recursive_macro
{ int rm_field
; } G
;
338 #define rm_field (G.rm_field)
339 printf("rm_field = %d\n", rm_field
);
340 printf("rm_field = %d\n", WRAP(rm_field
));
341 WRAP((printf("rm_field = %d %d\n", rm_field
, WRAP(rm_field
))));
358 #if !defined(__TINYC__) && (__GNUC__ >= 8)
359 /* Old GCCs don't regard "foo"[1] as constant, even in GNU dialect. */
360 #define CONSTANTINDEXEDSTRLIT
362 char str_ag1
[] = "b";
363 char str_ag2
[] = { "b" };
364 /*char str_bg1[] = ("cccc"); GCC accepts this with pedantic warning, TCC not */
365 #ifdef CONSTANTINDEXEDSTRLIT
366 char str_ag3
[] = { "ab"[1], 0 };
367 char str_x
[2] = { "xy" "z"[2], 0 };
369 char *str_ar
[] = { "one", "two" };
370 struct str_SS
{unsigned char a
[3], b
; };
371 struct str_SS str_sinit15
= { "r" };
372 struct str_SS str_sinit16
[] = { { "q" }, 2 };
374 static void string_test2()
377 char a3
[2] = { "p" };
378 char a4
[2] = { "ab" "c"[2], 0 };
379 char *pa1
= "def" + 1;
380 char *pa2
= { "xyz" + 1 };
382 struct str_SS ss
= { { [0 ... 1] = 'a' }, 0 };
383 #ifndef CONSTANTINDEXEDSTRLIT
384 char str_ag3
[] = { "ab"[1], 0 };
385 char str_x
[2] = { "xy" "z"[2], 0 };
387 puts("string_test2");
394 puts(str_sinit16
[0].a
);
399 printf("%s\n", "bla");
403 puts(i
>= 0 ? "one" : "two");
408 void ps(const char *s
)
420 const char foo1_string
[] = "\
429 printf("\141\1423\143\n");/* dezdez test */
430 printf("\x41\x42\x43\x3a\n");
431 printf("c=%c\n", 'r');
432 printf("wc=%C 0x%lx %C\n", L
'a', L
'\x1234', L
'c');
433 printf("foo1_string='%s'\n", foo1_string
);
435 printf("wstring=%S\n", L
"abc");
436 printf("wstring=%S\n", L
"abc" L
"def" "ghi");
437 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
438 printf("L'\\377'=%d L'\\xff'=%d\n", L
'\377', L
'\xff');
442 while ((b
= b
+ 1) < 96) {
446 printf("fib=%d\n", fib(33));
448 while (b
!= 0x80000000) {
456 void if1t(int n
, int a
, int b
, int c
)
458 if (a
&& b
) printf("if1t: %d 1 %d %d\n", n
, a
, b
);
459 if (a
&& !b
) printf("if1t: %d 2 %d %d\n", n
, a
, b
);
460 if (!a
&& b
) printf("if1t: %d 3 %d %d\n", n
, a
, b
);
461 if (!a
&& !b
) printf("if1t: %d 4 %d %d\n", n
, a
, b
);
462 if (a
|| b
) printf("if1t: %d 5 %d %d\n", n
, a
, b
);
463 if (a
|| !b
) printf("if1t: %d 6 %d %d\n", n
, a
, b
);
464 if (!a
|| b
) printf("if1t: %d 7 %d %d\n", n
, a
, b
);
465 if (!a
|| !b
) printf("if1t: %d 8 %d %d\n", n
, a
, b
);
466 if (a
&& b
|| c
) printf("if1t: %d 9 %d %d %d\n", n
, a
, b
, c
);
467 if (a
|| b
&& c
) printf("if1t: %d 10 %d %d %d\n", n
, a
, b
, c
);
468 if (a
> b
- 1 && c
) printf("if1t: %d 11 %d %d %d\n", n
, a
, b
, c
);
469 if (a
> b
- 1 || c
) printf("if1t: %d 12 %d %d %d\n", n
, a
, b
, c
);
470 if (a
> 0 && 1) printf("if1t: %d 13 %d %d %d\n", n
, a
, b
, c
);
471 if (a
> 0 || 0) printf("if1t: %d 14 %d %d %d\n", n
, a
, b
, c
);
476 if (0 && 1 || printf("if2t:ok\n") || 1)
477 printf("if2t:ok2\n");
478 printf("if2t:ok3\n");
483 volatile long long i
= 1;
484 if (i
<= 18446744073709551615ULL)
487 printf ("if3t:wrong 1\n");
507 for(i
= 0; i
< 10;i
++)
517 /* c99 for loop init test */
518 for (size_t count
= 1; count
< 3; count
++)
519 printf("count=%d\n", count
);
520 printf("count = %d\n", count
);
522 /* break/continue tests */
534 /* break/continue tests */
546 for(i
= 0;i
< 10;i
++) {
554 typedef int typedef_and_label
;
559 static void *label_table
[3] = { &&label1
, &&label2
, &&label3
};
562 /* This needs to parse as typedef, not as label. */
563 typedef_and_label
: 32;
568 /* This is a normal decl. */
570 /* This needs to parse as label, not as start of decl. */
581 /* we also test computed gotos (GCC extension) */
583 goto *label_table
[i
];
609 enum {E6
= 42, E7
, E8
} e
:8;
613 /* This is either 0 on L32 machines, or a large number
614 on L64 machines. We should be able to store this. */
615 EL_large
= ((unsigned long)0xf000 << 31) << 1,
618 enum { BIASU
= -1U<<31 };
619 enum { BIASS
= -1 << 31 };
621 static int getint(int i
)
626 return (int)(-1U << 31);
632 /* The following should give no warning */
634 struct S_enum s
= {E7
};
635 printf("%d %d %d %d %d %d %d\n", s
.e
,
636 E0
, E1
, E2
, E3
, E4
, E5
);
638 printf("b1=%d\n", b1
);
639 printf("enum large: %ld\n", EL_large
);
641 if (getint(0) == BIASU
)
642 printf("enum unsigned: ok\n");
644 printf("enum unsigned: wrong\n");
645 if (getint(0) == BIASS
)
646 printf("enum unsigned: ok\n");
648 printf("enum unsigned: wrong\n");
664 printf("a=%d\n", *a
);
666 printf("mytype2=%d\n", mytype2
);
676 void forward_ref(void)
678 printf("forward ok\n");
681 typedef struct struct1
{
701 struct struct1 st1
, st2
;
715 printf("g1=%d\n", g
);
722 printf("g2=%d\n", g
);
726 printf("g3=%d\n", g
);
730 printf("g4=%d\n", g
);
733 printf("g5=%d\n", g
);
742 for (int st2_i
= 1; st2_i
< 10; st2_i
++) {
745 printf("exloc: %d\n", st2_i
);
747 printf("exloc: %d\n", *st2_p
);
750 /* C has tentative definition, and they may be repeated. */
751 extern int st_global1
;
753 extern int st_global1
;
755 extern int st_global2
;
757 extern int st_global2
;
764 printf("sizeof(a) = %d\n", sizeof(a
));
765 printf("sizeof(\"a\") = %d\n", sizeof("a"));
767 printf("sizeof(__func__) = %d\n", sizeof(__func__
));
769 printf("sizeof tab %d\n", sizeof(tab
));
770 printf("sizeof tab2 %d\n", sizeof tab2
);
774 printf("%d %d %d\n", tab
[0], tab
[1], tab
[2]);
777 tab2
[i
][j
] = 10 * i
+ j
;
779 printf(" %3d", ((int *)tab2
)[i
]);
782 printf("sizeof(size_t)=%d\n", sizeof(size_t));
783 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
790 printf("%d\n", a
+= 1);
791 printf("%d\n", a
-= 2);
792 printf("%d\n", a
*= 31232132);
793 printf("%d\n", a
/= 4);
794 printf("%d\n", a
%= 20);
795 printf("%d\n", a
&= 6);
796 printf("%d\n", a
^= 7);
797 printf("%d\n", a
|= 8);
798 printf("%d\n", a
>>= 3);
799 printf("%d\n", a
<<= 4);
803 printf("%d\n", a
+ 1);
804 printf("%d\n", a
- 2);
805 printf("%d\n", a
* 312);
806 printf("%d\n", a
/ 4);
807 printf("%d\n", b
/ 4);
808 printf("%d\n", (unsigned)b
/ 4);
809 printf("%d\n", a
% 20);
810 printf("%d\n", b
% 20);
811 printf("%d\n", (unsigned)b
% 20);
812 printf("%d\n", a
& 6);
813 printf("%d\n", a
^ 7);
814 printf("%d\n", a
| 8);
815 printf("%d\n", a
>> 3);
816 printf("%d\n", b
>> 3);
817 printf("%d\n", (unsigned)b
>> 3);
818 printf("%d\n", a
<< 4);
823 printf("%d\n", 12 + 1);
824 printf("%d\n", 12 - 2);
825 printf("%d\n", 12 * 312);
826 printf("%d\n", 12 / 4);
827 printf("%d\n", 12 % 20);
828 printf("%d\n", 12 & 6);
829 printf("%d\n", 12 ^ 7);
830 printf("%d\n", 12 | 8);
831 printf("%d\n", 12 >> 2);
832 printf("%d\n", 12 << 4);
836 printf("%d %d %d %d\n",
845 return (c
>= 'a' & c
<= 'z') | (c
>= 'A' & c
<= 'Z') | c
== '_';
848 /**********************/
850 int vstack
[10], *vstack_ptr
;
852 void vpush(int vt
, int vc
)
858 void vpop(int *ft
, int *fc
)
870 vstack_ptr
[-2] &= ~0xffffff80;
872 printf("res= %d %d\n", a
, b
);
875 int const_len_ar
[sizeof(1/0)]; /* div-by-zero, but in unevaluated context */
877 void constant_expr_test()
881 printf("%d\n", a
* 16);
882 printf("%d\n", a
* 1);
883 printf("%d\n", a
+ 0);
884 printf("%d\n", sizeof(const_len_ar
));
896 printf("diff=%d\n", q
- p
);
898 printf("inc=%d\n", p
- tab4
);
900 printf("dec=%d\n", p
- tab4
);
902 printf("inc=%d\n", p
- tab4
);
904 printf("dec=%d\n", p
- tab4
);
905 printf("add=%d\n", p
+ 3 - tab4
);
906 printf("add=%d\n", 3 + p
- tab4
);
908 /* check if 64bit support is ok */
911 printf("%p %p %ld\n", q
, p
, p
-q
);
912 printf("%d %d %d %d %d %d\n",
913 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
916 printf("%p %p %ld\n", q
, p
, p
-q
);
917 printf("%d %d %d %d %d %d\n",
918 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
919 p
= (int *)((char *)p
+ 0xf0000000);
920 printf("%p %p %ld\n", q
, p
, p
-q
);
921 printf("%d %d %d %d %d %d\n",
922 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
924 printf("%p %p %ld\n", q
, p
, p
-q
);
925 printf("%d %d %d %d %d %d\n",
926 p
== q
, p
!= q
, p
< q
, p
<= q
, p
>= q
, p
> q
);
931 struct size12 s
[2], *sp
= s
;
936 printf("%d\n", sp
[j
].i
);
940 p
= (int*)0x100000000UL
+ i
;
942 printf("largeptr: %p %d\n", p
, i
);
951 printf("%d\n", a
== a
);
952 printf("%d\n", a
!= a
);
954 printf("%d\n", a
< b
);
955 printf("%d\n", a
<= b
);
956 printf("%d\n", a
<= a
);
957 printf("%d\n", b
>= a
);
958 printf("%d\n", a
>= a
);
959 printf("%d\n", b
> a
);
961 printf("%d\n", (unsigned)a
< b
);
962 printf("%d\n", (unsigned)a
<= b
);
963 printf("%d\n", (unsigned)a
<= a
);
964 printf("%d\n", (unsigned)b
>= a
);
965 printf("%d\n", (unsigned)a
>= a
);
966 printf("%d\n", (unsigned)b
> a
);
989 struct __attribute__((aligned(16))) aligntest5
996 } __attribute__((aligned(16)));
1001 struct aligntest5 altest5
[2];
1002 struct aligntest6 altest6
[2];
1004 /* altest7 is correctly aligned to 16 bytes also with TCC,
1005 but __alignof__ returns the wrong result (4) because we
1006 can't store the alignment yet when specified on symbols
1007 directly (it's stored in the type so we'd need to make
1008 a copy of it). -- FIXED */
1009 struct aligntest7 altest7
[2] __attribute__((aligned(16)));
1014 } __attribute__((aligned(4096)));
1017 unsigned long flags
;
1025 unsigned long index
;
1029 unsigned long counters
;
1043 unsigned long compound_head
;
1044 unsigned int compound_dtor
;
1045 unsigned int compound_order
;
1048 } __attribute__((aligned(2 * sizeof(long))));
1050 typedef unsigned long long __attribute__((aligned(4))) unaligned_u64
;
1053 unsigned int buf_nr
;
1054 unaligned_u64 start_lba
;
1057 struct aligntest10
{
1058 unsigned int buf_nr
;
1059 unsigned long long start_lba
;
1068 printf("sizes: %d %d %d %d\n",
1069 sizeof(struct struct1
),
1070 sizeof(struct struct2
),
1071 sizeof(union union1
),
1072 sizeof(union union2
));
1073 printf("offsets: %d\n", (int)((char*)&st1
.u
.v1
- (char*)&st1
));
1077 printf("st1: %d %d %d\n",
1078 st1
.f1
, st1
.f2
, st1
.f3
);
1081 printf("union1: %d\n", st1
.u
.v1
);
1084 printf("union2: %d\n", u
.w1
);
1089 printf("st2: %d %d %d\n",
1090 s
->f1
, s
->f2
, s
->f3
);
1091 printf("str_addr=%x\n", (int)(uintptr_t)st1
.str
- (int)(uintptr_t)&st1
.f1
);
1093 /* align / size tests */
1094 printf("aligntest1 sizeof=%d alignof=%d\n",
1095 sizeof(struct aligntest1
), __alignof__(struct aligntest1
));
1096 printf("aligntest2 sizeof=%d alignof=%d\n",
1097 sizeof(struct aligntest2
), __alignof__(struct aligntest2
));
1098 printf("aligntest3 sizeof=%d alignof=%d\n",
1099 sizeof(struct aligntest3
), __alignof__(struct aligntest3
));
1100 printf("aligntest4 sizeof=%d alignof=%d\n",
1101 sizeof(struct aligntest4
), __alignof__(struct aligntest4
));
1102 printf("aligntest5 sizeof=%d alignof=%d\n",
1103 sizeof(struct aligntest5
), __alignof__(struct aligntest5
));
1104 printf("aligntest6 sizeof=%d alignof=%d\n",
1105 sizeof(struct aligntest6
), __alignof__(struct aligntest6
));
1106 printf("aligntest7 sizeof=%d alignof=%d\n",
1107 sizeof(struct aligntest7
), __alignof__(struct aligntest7
));
1108 printf("aligntest8 sizeof=%d alignof=%d\n",
1109 sizeof(struct aligntest8
), __alignof__(struct aligntest8
));
1110 printf("aligntest9 sizeof=%d alignof=%d\n",
1111 sizeof(struct aligntest9
), __alignof__(struct aligntest9
));
1112 printf("aligntest10 sizeof=%d alignof=%d\n",
1113 sizeof(struct aligntest10
), __alignof__(struct aligntest10
));
1114 printf("altest5 sizeof=%d alignof=%d\n",
1115 sizeof(altest5
), __alignof__(altest5
));
1116 printf("altest6 sizeof=%d alignof=%d\n",
1117 sizeof(altest6
), __alignof__(altest6
));
1118 printf("altest7 sizeof=%d alignof=%d\n",
1119 sizeof(altest7
), __alignof__(altest7
));
1121 /* empty structures (GCC extension) */
1122 printf("sizeof(struct empty) = %d\n", sizeof(struct empty
));
1123 printf("alignof(struct empty) = %d\n", __alignof__(struct empty
));
1125 printf("Large: sizeof=%d\n", sizeof(ls
));
1126 memset(&ls
, 0, sizeof(ls
));
1127 ls
.compound_head
= 42;
1128 printf("Large: offsetof(compound_head)=%d\n", (int)((char*)&ls
.compound_head
- (char*)&ls
));
1131 /* simulate char/short return value with undefined upper bits */
1132 static int __csf(int x
) { return x
; }
1133 static void *_csf
= __csf
;
1134 #define csf(t,n) ((t(*)(int))_csf)(n)
1136 /* XXX: depend on endianness */
1137 void char_short_test()
1145 printf("s8=%d %d\n",
1146 *(signed char *)&var1
, *(signed char *)&var2
);
1147 printf("u8=%d %d\n",
1148 *(unsigned char *)&var1
, *(unsigned char *)&var2
);
1149 printf("s16=%d %d\n",
1150 *(short *)&var1
, *(short *)&var2
);
1151 printf("u16=%d %d\n",
1152 *(unsigned short *)&var1
, *(unsigned short *)&var2
);
1153 printf("s32=%d %d\n",
1154 *(int *)&var1
, *(int *)&var2
);
1155 printf("u32=%d %d\n",
1156 *(unsigned int *)&var1
, *(unsigned int *)&var2
);
1157 *(signed char *)&var1
= 0x08;
1158 printf("var1=%x\n", var1
);
1159 *(short *)&var1
= 0x0809;
1160 printf("var1=%x\n", var1
);
1161 *(int *)&var1
= 0x08090a0b;
1162 printf("var1=%x\n", var1
);
1165 var4
= 0x11223344aa998877ULL
;
1166 var1
= var3
= var1
+ 1;
1167 var4
= var3
= var4
+ 1;
1168 printf("promote char/short assign %d "LONG_LONG_FORMAT
"\n", var1
, var4
);
1170 var4
= 0x11223344aa998877ULL
;
1171 printf("promote char/short assign VA %d %d\n", var3
= var1
+ 1, var3
= var4
+ 1);
1172 printf("promote char/short cast VA %d %d\n", (signed char)(var1
+ 1), (signed char)(var4
+ 1));
1173 #if !defined(__arm__)
1174 /* We can't really express GCC behaviour of return type promotion in
1175 the presence of undefined behaviour (like __csf is). */
1176 var1
= csf(unsigned char,0x89898989);
1177 var4
= csf(signed char,0xabababab);
1178 printf("promote char/short funcret %d "LONG_LONG_FORMAT
"\n", var1
, var4
);
1179 printf("promote char/short fumcret VA %d %d %d %d\n",
1180 csf(unsigned short,0xcdcdcdcd),
1181 csf(short,0xefefefef),
1182 csf(_Bool
,0x33221100),
1183 csf(_Bool
,0x33221101));
1186 var1
= (signed char)(unsigned char)(var3
+ 1);
1187 var4
= (signed char)(unsigned char)(var3
+ 1);
1188 printf("promote multicast (char)(unsigned char) %d "LONG_LONG_FORMAT
"\n", var1
, var4
);
1189 var4
= 0x11223344aa998877ULL
;
1190 var4
= (unsigned)(int)(var4
+ 1);
1191 printf("promote multicast (unsigned)(int) "LONG_LONG_FORMAT
"\n", var4
);
1192 var4
= 0x11223344bbaa9988ULL
;
1193 var4
= (unsigned)(signed char)(var4
+ 1);
1194 printf("promote multicast (unsigned)(char) "LONG_LONG_FORMAT
"\n", var4
);
1197 /******************/
1199 typedef struct Sym
{
1207 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1208 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1210 static int toupper1(int a
)
1215 static unsigned int calc_vm_flags(unsigned int prot
)
1217 unsigned int prot_bits
;
1218 /* This used to segfault in some revisions: */
1219 prot_bits
= ((0x1==0x00000001)?(prot
&0x1):(prot
&0x1)?0x00000001:0);
1223 enum cast_enum
{ FIRST
, LAST
};
1225 static void tst_cast(enum cast_enum ce
)
1232 int *s
, a
, b
, t
, f
, i
;
1236 printf("!s=%d\n", !s
);
1240 printf("a=%d\n", a
);
1242 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1243 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1244 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1259 printf("b=%d\n", a
+ (0 ? 1 : a
/ 2));
1261 /* test register spilling */
1264 a
= (a
+ b
) * ((a
< b
) ?
1265 ((b
- a
) * (a
- b
)): a
+ b
);
1266 printf("a=%d\n", a
);
1268 /* test complex || or && expressions */
1272 printf("exp=%d\n", f
== (32 <= a
&& a
<= 3));
1273 printf("r=%d\n", (t
|| f
) + (t
&& f
));
1275 /* check that types of casted &&/|| are preserved (here the unsignedness) */
1277 printf("type of bool: %d\n", (int) ( (~ ((unsigned int) (t
&& 1))) / 2) );
1278 tst_cast(t
>= 0 ? FIRST
: LAST
);
1280 printf("type of cond: %d\n", (~(t
? 0U : (unsigned int)0)) / 2 );
1284 int aspect_native
= 65536;
1285 double bfu_aspect
= 1.0;
1287 for(aspect_on
= 0; aspect_on
< 2; aspect_on
++) {
1288 aspect
=aspect_on
?(aspect_native
*bfu_aspect
+0.5):65535UL;
1289 printf("aspect=%d\n", aspect
);
1293 /* test ? : GCC extension */
1295 static int v1
= 34 ? : -1; /* constant case */
1296 static int v2
= 0 ? : -1; /* constant case */
1299 printf("%d %d\n", v1
, v2
);
1300 printf("%d %d\n", a
- 30 ? : a
* 2, a
+ 1 ? : a
* 2);
1303 /* again complex expression */
1304 for(i
=0;i
<256;i
++) {
1305 if (toupper1 (i
) != TOUPPER (i
))
1306 printf("error %d\n", i
);
1308 printf ("bits = 0x%x\n", calc_vm_flags (0x1));
1311 extern int undefined_function(void);
1312 extern int defined_function(void);
1315 int undefined_function(void) {}
1318 static inline void refer_to_undefined(void)
1320 undefined_function();
1323 void optimize_out_test(void)
1325 int i
= 0 ? undefined_function() : defined_function();
1326 printf ("oo:%d\n", i
);
1327 int j
= 1 ? defined_function() : undefined_function();
1328 printf ("oo:%d\n", j
);
1330 printf("oo:%d\n", undefined_function());
1332 printf("oo:%d\n", defined_function());
1334 printf("oo:%d\n", defined_function());
1336 printf("oo:%d\n", undefined_function());
1338 printf("oow:%d\n", defined_function());
1340 printf("oow:%d\n", undefined_function());
1343 /* Following is a switch without {} block intentionally. */
1346 printf ("oos:%d\n", defined_function());
1347 /* The following break shouldn't lead to disabled code after
1351 printf ("ool1:%d\n", defined_function());
1352 /* Same for the other types of loops. */
1356 printf ("ool2:%d\n", defined_function());
1359 printf ("ool3:%d\n", defined_function());
1360 /* Normal {} blocks without controlling statements
1361 shouldn't reactivate code emission */
1366 printf ("ool4:%d\n", undefined_function());
1371 break; /* this break shouldn't disable code outside the if. */
1372 printf("ool5:%d\n", defined_function());
1382 printf("ool6:%d\n", defined_function());
1389 printf("ool7:%d\n", undefined_function());
1392 /* Test that constants in logical && are optimized: */
1393 i
= 0 && undefined_function();
1394 i
= defined_function() && 0 && undefined_function();
1395 if (0 && undefined_function())
1396 undefined_function();
1397 if (defined_function() && 0)
1398 undefined_function();
1400 undefined_function();
1401 if (defined_function() && 0 && undefined_function())
1402 undefined_function();
1403 /* The same for || : */
1404 i
= 1 || undefined_function();
1405 i
= defined_function() || 1 || undefined_function();
1406 if (1 || undefined_function())
1409 undefined_function();
1410 if (defined_function() || 1)
1413 undefined_function();
1417 undefined_function();
1418 if (defined_function() || 1 || undefined_function())
1421 undefined_function();
1423 if (defined_function() && 0)
1424 refer_to_undefined();
1431 undefined_function();
1435 switch (defined_function()) {
1436 case 0: undefined_function(); break;
1437 default: undefined_function(); break;
1441 /* Leave the "if(1)return; printf()" in this order and last in the function */
1444 printf ("oor:%d\n", undefined_function());
1447 int defined_function(void)
1453 /* GCC accepts that */
1454 static int tab_reinit
[];
1455 static int tab_reinit
[10];
1457 static int tentative_ar
[];
1458 static int tentative_ar
[] = {1,2,3};
1460 //int cinit1; /* a global variable can be defined several times without error ! */
1464 int *cinit2
= (int []){3, 2, 1};
1466 void compound_literal_test(void)
1471 p
= (int []){1, 2, 3};
1473 printf(" %d", p
[i
]);
1477 printf("%d", cinit2
[i
]);
1481 printf("q1=%s\n", q
);
1483 q
= (char *){ "tralala2" };
1484 printf("q2=%s\n", q
);
1487 printf("q3=%s\n", q3
);
1489 q
= (char []){ "tralala3" };
1490 printf("q4=%s\n", q
);
1493 p
= (int []){1, 2, cinit1
+ 3};
1495 printf(" %d", p
[i
]);
1499 p
= (int []){1, 2, 4 + i
};
1500 printf("%d %d %d\n",
1522 printf("func1=%d\n", kr_func1(3, 4));
1523 printf("func2=%d\n", kr_func2(3, 4));
1530 tab
= (char*)malloc(20);
1547 /* structure assignment tests */
1553 void struct_assign_test1(struct structa1 s1
, int t
, float f
)
1555 printf("%d %d %d %f\n", s1
.f1
, s1
.f2
, t
, f
);
1558 struct structa1
struct_assign_test2(struct structa1 s1
, int t
)
1565 void struct_assign_test(void)
1568 struct structa1 lsta1
, lsta2
;
1570 } s
= {{1,2}, {3,4}}, *ps
;
1575 struct_assign_test1(ps
->lsta2
, 3, 4.5);
1576 printf("before call: %d %d\n", s
.lsta2
.f1
, s
.lsta2
.f2
);
1577 ps
->lsta2
= struct_assign_test2(ps
->lsta2
, ps
->i
);
1578 printf("after call: %d %d\n", ps
->lsta2
.f1
, ps
->lsta2
.f2
);
1583 /* XXX: we should allow this even without braces */
1584 { struct_assign_test
}
1586 printf("%d\n", struct_assign_test
== t
[0].elem
);
1588 s
.lsta1
= s
.lsta2
= struct_assign_test2(s
.lsta1
, 1);
1589 printf("%d %d\n", s
.lsta1
.f1
, s
.lsta1
.f2
);
1592 /* casts to short/char */
1594 void cast1(char a
, short b
, unsigned char c
, unsigned short d
)
1596 printf("%d %d %d %d\n", a
, b
, c
, d
);
1610 unsigned long ul
= 0x80000000UL
;
1611 p
-= 0x700000000042;
1616 printf("%d %d %d %d\n",
1619 (unsigned char)(a
+ 1),
1620 (unsigned short)(a
+ 1));
1621 printf("%d %d %d %d\n",
1624 (unsigned char)0xfffff,
1625 (unsigned short)0xfffff);
1627 a
= (bcast
= 128) + 1;
1629 a
= (scast
= 65536) + 1;
1632 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
1634 /* test cast from unsigned to signed short to int */
1637 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b
, d
);
1640 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b
, d
);
1642 /* test implicit int casting for array accesses */
1646 printf("%d %d\n", tab
[0], tab
[1]);
1648 /* test implicit casting on some operators */
1649 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1650 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1651 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1653 #if CC_NAME != CC_clang /* clang doesn't support non-portable conversions */
1654 /* from pointer to integer types */
1655 printf("%d %d %ld %ld %lld %lld\n",
1656 (int)p
, (unsigned int)p
,
1657 (long)p
, (unsigned long)p
,
1658 (long long)p
, (unsigned long long)p
);
1661 /* from integers to pointers */
1662 printf("%p %p %p %p\n",
1663 (void *)a
, (void *)b
, (void *)c
, (void *)d
);
1665 /* int to int with sign set */
1666 printf("0x%lx\n", (unsigned long)(int)ul
);
1669 /* initializers tests */
1670 struct structinit1
{
1679 int sinit3
[3] = { 1, 2, {{3}}, };
1680 int sinit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1681 int sinit5
[3][2] = { 1, 2, 3, 4, 5, 6 };
1682 int sinit6
[] = { 1, 2, 3 };
1683 int sinit7
[] = { [2] = 3, [0] = 1, 2 };
1684 char sinit8
[] = "hello" "trala";
1686 struct structinit1 sinit9
= { 1, 2, 3 };
1687 struct structinit1 sinit10
= { .f2
= 2, 3, .f1
= 1 };
1688 struct structinit1 sinit11
= { .f2
= 2, 3, .f1
= 1,
1696 char *sinit12
= "hello world";
1702 char sinit14
[10] = { "abc" };
1703 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
1705 struct { int a
[3], b
; } sinit16
[] = { { 1 }, 2 };
1721 struct complexinit0
{
1726 struct complexinit
{
1728 const struct complexinit0
*b
;
1731 const static struct complexinit cix
[] = {
1734 .b
= (const struct complexinit0
[]) {
1742 struct complexinit2
{
1747 struct complexinit2 cix20
;
1749 struct complexinit2 cix21
= {
1751 .b
= { 3001, 3002, 3003 }
1754 struct complexinit2 cix22
= {
1756 .b
= { 4001, 4002, 4003, 4004, 4005, 4006 }
1759 typedef int arrtype1
[];
1760 arrtype1 sinit19
= {1};
1761 arrtype1 sinit20
= {2,3};
1762 typedef int arrtype2
[3];
1763 arrtype2 sinit21
= {4};
1764 arrtype2 sinit22
= {5,6,7};
1766 /* Address comparisons of non-weak symbols with zero can be const-folded */
1767 int sinit23
[2] = { "astring" ? sizeof("astring") : -1,
1768 &sinit23
? 42 : -1 };
1770 int sinit24
= 2 || 1 / 0; /* exception in constant but unevaluated context */
1773 struct bf_SS
{unsigned int bit
:1,bits31
:31; };
1774 struct bf_SS bf_init
= { .bit
= 1 };
1775 struct bfn_SS
{int a
,b
; struct bf_SS c
; int d
,e
; };
1776 struct bfn_SS bfn_init
= { .c
.bit
= 1 };
1777 struct bfa_SS
{int a
,b
; struct bf_SS c
[3]; int d
,e
; };
1778 struct bfa_SS bfa_init
= { .c
[1].bit
= 1 };
1779 struct bf_SS bfaa_init
[3] = { [1].bit
= 1 };
1780 struct bf_SS bfaa_vinit
[] = { [2].bit
= 1 };
1781 struct b2_SS
{long long int field
: 52; long long int pad
: 12; };
1782 struct b2_SS bf_init2
= {0xFFF000FFF000FLL
, 0x123};
1784 extern int external_inited
= 42;
1786 void init_test(void)
1790 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1791 int linit6
[] = { 1, 2, 3 };
1793 char linit8
[] = "hello" "trala";
1794 int linit12
[10] = { 1, 2 };
1795 int linit13
[10] = { 1, 2, [7] = 3, [3] = 4, };
1796 char linit14
[10] = "abc";
1797 int linit15
[10] = { linit1
, linit1
+ 1, [6] = linit1
+ 2, };
1798 struct linit16
{ int a1
, a2
, a3
, a4
; } linit16
= { 1, .a3
= 2 };
1799 int linit17
= sizeof(linit17
);
1801 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1802 int linit18
[2] = {&zero
? 1 : -1, zero
? -1 : 1 };
1803 struct bf_SS bf_finit
= { .bit
= 1 };
1804 struct bfn_SS bfn_finit
= { .c
.bit
= 1 };
1805 struct bfa_SS bfa_finit
= { .c
[1].bit
= 1 };
1806 struct bf_SS bfaa_finit
[3] = { [1].bit
= 1 };
1807 struct bf_SS bfaa_fvinit
[] = { [2].bit
= 1 };
1808 struct b2_SS bf_finit2
= {0xFFF000FFF000FLL
, 0x123};
1810 printf("sinit1=%d\n", sinit1
);
1811 printf("sinit2=%d\n", sinit2
);
1812 printf("sinit3=%d %d %d %d\n",
1818 printf("sinit6=%d\n", sizeof(sinit6
));
1819 printf("sinit7=%d %d %d %d\n",
1825 printf("sinit8=%s\n", sinit8
);
1826 printf("sinit9=%d %d %d\n",
1831 printf("sinit10=%d %d %d\n",
1836 printf("sinit11=%d %d %d %d %d %d\n",
1847 printf("[%d][%d] = %d %d %d\n",
1848 i
, j
, sinit4
[i
][j
], sinit5
[i
][j
], linit4
[i
][j
]);
1849 printf("linit1=%d\n", linit1
);
1850 printf("linit2=%d\n", linit2
);
1851 printf("linit6=%d\n", sizeof(linit6
));
1852 printf("linit8=%d %s\n", sizeof(linit8
), linit8
);
1854 printf("sinit12=%s\n", sinit12
);
1855 printf("sinit13=%d %s %s %s\n",
1860 printf("sinit14=%s\n", sinit14
);
1862 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
1864 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
1866 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
1868 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
1870 printf("%d %d %d %d\n",
1875 /* test that initialisation is done after variable declare */
1876 printf("linit17=%d\n", linit17
);
1877 printf("sinit15=%d\n", sinit15
[0]);
1878 printf("sinit16=%d %d\n", sinit16
[0].a
[0], sinit16
[1].a
[0]);
1879 printf("sinit17=%s %d %s %d\n",
1880 sinit17
[0].s
, sinit17
[0].len
,
1881 sinit17
[1].s
, sinit17
[1].len
);
1883 printf("%x ", sinit18
[i
]);
1885 /* complex init check */
1886 printf("cix: %d %d %d %d %d %d %d\n",
1888 cix
[0].b
[0].a
, cix
[0].b
[0].b
,
1889 cix
[0].b
[1].a
, cix
[0].b
[1].b
,
1890 cix
[0].b
[2].a
, cix
[0].b
[2].b
);
1891 printf("cix2: %d %d\n", cix21
.b
[2], cix22
.b
[5]);
1892 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20
, sizeof cix21
, sizeof cix22
);
1894 printf("arrtype1: %d %d %d\n", sinit19
[0], sinit20
[0], sinit20
[1]);
1895 printf("arrtype2: %d %d\n", sizeof(sinit19
), sizeof(sinit20
));
1896 printf("arrtype3: %d %d %d\n", sinit21
[0], sinit21
[1], sinit21
[2]);
1897 printf("arrtype4: %d %d %d\n", sinit22
[0], sinit22
[1], sinit22
[2]);
1898 printf("arrtype5: %d %d\n", sizeof(sinit21
), sizeof(sinit22
));
1899 printf("arrtype6: %d\n", sizeof(arrtype2
));
1901 printf("sinit23= %d %d\n", sinit23
[0], sinit23
[1]);
1902 printf("sinit24=%d\n", sinit24
);
1903 printf("linit18= %d %d\n", linit18
[0], linit18
[1]);
1904 printf ("bf1: %u %u\n", bf_init
.bit
, bf_init
.bits31
);
1905 printf ("bf2: %u %u\n", bf_finit
.bit
, bf_finit
.bits31
);
1906 printf ("bf3: %u %u\n", bfn_init
.c
.bit
, bfn_init
.c
.bits31
);
1907 printf ("bf4: %u %u\n", bfn_finit
.c
.bit
, bfn_finit
.c
.bits31
);
1908 for (i
= 0; i
< 3; i
++)
1909 printf ("bf5[%d]: %u %u\n", i
, bfa_init
.c
[i
].bit
, bfa_init
.c
[i
].bits31
);
1910 for (i
= 0; i
< 3; i
++)
1911 printf ("bf6[%d]: %u %u\n", i
, bfa_finit
.c
[i
].bit
, bfa_finit
.c
[i
].bits31
);
1912 for (i
= 0; i
< 3; i
++)
1913 printf ("bf7[%d]: %u %u\n", i
, bfaa_init
[i
].bit
, bfaa_init
[i
].bits31
);
1914 for (i
= 0; i
< 3; i
++)
1915 printf ("bf8[%d]: %u %u\n", i
, bfaa_finit
[i
].bit
, bfaa_finit
[i
].bits31
);
1916 for (i
= 0; i
< 3; i
++)
1917 printf ("bf9[%d]: %u %u\n", i
, bfaa_vinit
[i
].bit
, bfaa_vinit
[i
].bits31
);
1918 for (i
= 0; i
< 3; i
++)
1919 printf ("bf10[%d]: %u %u\n", i
, bfaa_fvinit
[i
].bit
, bfaa_fvinit
[i
].bits31
);
1922 void switch_uc(unsigned char uc
)
1935 printf("ucsw: broken!\n");
1939 void switch_sc(signed char sc
)
1952 printf("scsw: broken!\n");
1959 unsigned long long ull
;
1984 for (i
= 1; i
<= 5; i
++) {
1985 ull
= (unsigned long long)i
<< 61;
1988 printf("ullsw:1\n");
1991 printf("ullsw:2\n");
1994 printf("ullsw:3\n");
1997 printf("ullsw:4\n");
2000 printf("ullsw:5\n");
2003 printf("ullsw: broken!\n");
2007 for (i
= 1; i
<= 5; i
++) {
2008 ll
= (long long)i
<< 61;
2026 printf("llsw: broken!\n");
2030 for (i
= -5; i
<= 5; i
++) {
2031 switch_uc((unsigned char)i
);
2034 for (i
= -5; i
<= 5; i
++) {
2035 switch_sc ((signed char)i
);
2039 /* ISOC99 _Bool type */
2040 void c99_bool_test(void)
2046 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
2048 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
2050 printf("b = %d\n", b
);
2052 printf("b = %d\n", b
);
2054 printf("sizeof(x ? _Bool : _Bool) = %d (should be sizeof int)\n",
2055 sizeof((volatile)a
? b
: b2
));
2059 void bitfield_test(void)
2071 unsigned int f5
: 7;
2073 printf("sizeof(st1) = %d\n", sizeof(st1
));
2082 printf("%d %d %d %d %d\n",
2083 st1
.f1
, st1
.f2
, st1
.f3
, st1
.f4
, st1
.f5
);
2086 printf("%d %d\n", sa
, ca
);
2090 printf("st1.f1 == -1\n");
2092 printf("st1.f1 != -1\n");
2094 printf("st1.f2 == -1\n");
2096 printf("st1.f2 != -1\n");
2102 unsigned long long f3
: 38;
2104 st2
.f1
= 0x123456789ULL
;
2106 st2
.f2
= (long long)a
<< 25;
2109 printf("%lld %lld %lld\n", st2
.f1
, st2
.f2
, st2
.f3
);
2112 Disabled
for now until further clarification re GCC compatibility
2121 printf("sizeof(st3) = %d\n", sizeof(st3
));
2129 printf("st4.y == %d\n", st4
.y
);
2133 int x
: 12, y
: 4, : 0, : 4, z
: 3;
2135 } st5
= { 1, 2, 3, 4, -3, 6 };
2136 printf("st5 = %d %d %d %d %d %d\n", st5
.a
, st5
.b
, st5
.x
, st5
.y
, st5
.z
, st5
.c
);
2139 unsigned char y
: 2;
2142 printf("st6.y == %d\n", st6
.y
);
2146 #define FLOAT_FMT "%f\n"
2148 /* x86's float isn't compatible with GCC */
2149 #define FLOAT_FMT "%.5f\n"
2152 /* declare strto* functions as they are C99 */
2153 double strtod(const char *nptr
, char **endptr
);
2156 float strtof(const char *nptr
, char **endptr
) {return (float)strtod(nptr
, endptr
);}
2157 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
) {return (LONG_DOUBLE
)strtod(nptr
, endptr
);}
2159 float strtof(const char *nptr
, char **endptr
);
2160 LONG_DOUBLE
strtold(const char *nptr
, char **endptr
);
2163 #if CC_NAME == CC_clang
2164 /* In clang 0.0/0.0 is nan and not -nan.
2165 Also some older clang version do v=-v
2167 static char enable_nan_test
= 0;
2169 static char enable_nan_test
= 1;
2172 #define FTEST(prefix, typename, type, fmt)\
2173 void prefix ## cmp(type a, type b)\
2175 printf("%d %d %d %d %d %d\n",\
2182 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
2190 printf(fmt "\n", ++a);\
2191 printf(fmt "\n", a++);\
2192 printf(fmt "\n", a);\
2194 printf("%d %d\n", !a, !b);\
2196 void prefix ## fcast(type a)\
2204 unsigned long long llua;\
2209 printf("ftof: %f %f %Lf\n", fa, da, la);\
2211 llia = (long long)a;\
2212 a = (a >= 0) ? a : -a;\
2213 ua = (unsigned int)a;\
2214 llua = (unsigned long long)a;\
2215 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
2218 llia = -0x123456789012345LL;\
2219 llua = 0xf123456789012345LLU;\
2221 printf("itof: " fmt "\n", b);\
2223 printf("utof: " fmt "\n", b);\
2225 printf("lltof: " fmt "\n", b);\
2227 printf("ulltof: " fmt "\n", b);\
2230 float prefix ## retf(type a) { return a; }\
2231 double prefix ## retd(type a) { return a; }\
2232 LONG_DOUBLE prefix ## retld(type a) { return a; }\
2234 void prefix ## call(void)\
2236 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
2237 printf("double: %f\n", prefix ## retd(42.123456789));\
2238 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
2239 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
2242 void prefix ## signed_zeros(void) \
2244 type x = 0.0, y = -0.0, n, p;\
2246 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
2247 1.0 / x != 1.0 / y);\
2249 printf ("x != y; this is wrong!\n");\
2253 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
2254 1.0 / x != 1.0 / n);\
2256 printf ("x != -x; this is wrong!\n");\
2260 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
2261 1.0 / x != 1.0 / p);\
2263 printf ("x != +y; this is wrong!\n");\
2266 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
2267 1.0 / x != 1.0 / p);\
2269 printf ("x != -y; this is wrong!\n");\
2271 void prefix ## nan(void)\
2273 type nan = 0.0/0.0;\
2275 printf("nantest: " fmt " " fmt "\n", nan, nnan);\
2277 void prefix ## test(void)\
2279 printf("testing '%s'\n", #typename);\
2280 prefix ## cmp(1, 2.5);\
2281 prefix ## cmp(2, 1.5);\
2282 prefix ## cmp(1, 1);\
2283 prefix ## fcast(234.6);\
2284 prefix ## fcast(-2334.6);\
2286 prefix ## signed_zeros();\
2287 if (enable_nan_test) prefix ## nan();\
2290 FTEST(f
, float, float, "%f")
2291 FTEST(d
, double, double, "%f")
2292 FTEST(ld
, long double, LONG_DOUBLE
, "%Lf")
2294 double ftab1
[3] = { 1.2, 3.4, -5.6 };
2297 void float_test(void)
2299 #if !defined(__arm__) || defined(__ARM_PCS_VFP) || defined __ANDROID__
2300 volatile float fa
, fb
;
2301 volatile double da
, db
;
2304 static double nan2
= 0.0/0.0;
2305 static double inf1
= 1.0/0.0;
2306 static double inf2
= 1e5000
;
2307 volatile LONG_DOUBLE la
;
2309 printf("sizeof(float) = %d\n", sizeof(float));
2310 printf("sizeof(double) = %d\n", sizeof(double));
2311 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE
));
2315 printf("%f %f %f\n", ftab1
[0], ftab1
[1], ftab1
[2]);
2316 printf("%f %f %f\n", 2.12, .5, 2.3e10
);
2317 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
2319 printf("da=%f\n", da
);
2321 printf("fa=%f\n", fa
);
2324 printf("da = %f\n", da
);
2327 printf("db = %f\n", db
);
2328 printf("nan != nan = %d, inf1 = %f, inf2 = %f\n", nan2
!= nan2
, inf1
, inf2
);
2329 da
= 0x0.88p
-1022; /* a subnormal */
2331 printf ("da subnormal = %a\n", da
);
2332 printf ("da subnormal = %.40g\n", da
);
2333 printf ("la subnormal = %La\n", la
);
2334 printf ("la subnormal = %.40Lg\n", la
);
2337 printf ("da/2 subnormal = %a\n", da
);
2338 printf ("da/2 subnormal = %.40g\n", da
);
2339 printf ("la/2 subnormal = %La\n", la
);
2340 printf ("la/2 subnormal = %.40Lg\n", la
);
2341 fa
= 0x0.88p
-126f
; /* a subnormal */
2343 printf ("fa subnormal = %a\n", fa
);
2344 printf ("fa subnormal = %.40g\n", fa
);
2345 printf ("la subnormal = %La\n", la
);
2346 printf ("la subnormal = %.40Lg\n", la
);
2349 printf ("fa/2 subnormal = %a\n", fa
);
2350 printf ("fa/2 subnormal = %.40g\n", fa
);
2351 printf ("la/2 subnormal = %La\n", la
);
2352 printf ("la/2 subnormal = %.40Lg\n", la
);
2361 return fib(n
-1) + fib(n
-2);
2364 #if __GNUC__ == 3 || __GNUC__ == 4
2365 # define aligned_function 0
2367 void __attribute__((aligned(16))) aligned_function(int i
) {}
2386 /* more complicated pointer computation */
2389 printf("sizeof1 = %d\n", sizeof(funcptr_test
));
2390 printf("sizeof2 = %d\n", sizeof funcptr_test
);
2391 printf("sizeof3 = %d\n", sizeof(&funcptr_test
));
2392 printf("sizeof4 = %d\n", sizeof &funcptr_test
);
2400 /* Check that we can align functions */
2401 func
= aligned_function
;
2402 printf("aligned_function (should be zero): %d\n", ((int)(uintptr_t)func
) & 15);
2405 void lloptest(long long a
, long long b
)
2407 unsigned long long ua
, ub
;
2412 printf("arith: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2418 printf("arith1: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2424 printf("bin: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2430 printf("test: %d %d %d %d %d %d\n",
2438 printf("utest: %d %d %d %d %d %d\n",
2449 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2450 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
++, b
++);
2451 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", --a
, --b
);
2452 printf("arith2: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2454 printf("not: %d %d %d %d\n", !a
, !ua
, !b
, !ub
);
2457 void llshift(long long a
, int b
)
2459 printf("shift: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2460 (unsigned long long)a
>> b
,
2463 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2464 (unsigned long long)a
>> 3,
2467 printf("shiftc: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n",
2468 (unsigned long long)a
>> 35,
2478 long long la
, lb
, lc
;
2479 unsigned long long ula
, ulb
, ulc
;
2482 la
= (la
<< 20) | 0x12345;
2484 printf("la=" LONG_LONG_FORMAT
" ula=" ULONG_LONG_FORMAT
"\n", la
, ula
);
2489 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
2494 printf("ftoll: " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", la
, lb
, lc
);
2499 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
2504 printf("ftoull: " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
" " ULONG_LONG_FORMAT
"\n", ula
, ulb
, ulc
);
2507 long long llfunc1(int a
)
2517 long long int value(struct S
*v
)
2519 return ((long long int)v
->item
);
2522 long long llfunc2(long long x
, long long y
, int z
)
2527 void check_opl_save_regs(char *a
, long long b
, int c
)
2532 void longlong_test(void)
2537 printf("sizeof(long long) = %d\n", sizeof(long long));
2542 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"\n", a
, b
);
2543 printf(LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %Lx\n",
2547 0x1234567812345679);
2549 printf(LONG_LONG_FORMAT
"\n", a
);
2552 lloptest(0xff, 0x1234);
2553 b
= 0x72345678 << 10;
2557 b
= 0x72345678LL
<< 10;
2565 printf(XLONG_LONG_FORMAT
"\n", c
);
2568 /* long long reg spill test */
2573 printf("%lld\n", value(&a
));
2575 lloptest(0x80000000, 0);
2578 long long *p
, v
, **pp
;
2582 printf("another long long spill test : %lld\n", *p
);
2585 v
= llfunc2(**pp
, **pp
, ia
);
2586 printf("a long long function (arm-)reg-args test : %lld\n", v
);
2590 printf("%d %d %d %d\n", a
> b
, a
< b
, a
>= b
, a
<= b
);
2592 printf(LONG_LONG_FORMAT
"\n", 0x123456789LLU
);
2594 /* long long pointer deref in argument passing test */
2599 /* shortening followed by widening */
2600 unsigned long long u
= 0x8000000000000001ULL
;
2601 u
= (unsigned)(u
+ 1);
2602 printf("long long u=" ULONG_LONG_FORMAT
"\n", u
);
2603 u
= 0x11223344aa998877ULL
;
2604 u
= (unsigned)(int)(u
+ 1);
2605 printf("long long u=" ULONG_LONG_FORMAT
"\n", u
);
2607 /* was a problem with missing save_regs in gen_opl on 32-bit platforms */
2609 check_opl_save_regs(&cc
, -1, 0);
2610 printf("check_opl_save_regs: %d\n", cc
);
2613 void manyarg_test(void)
2615 LONG_DOUBLE ld
= 1234567891234LL;
2616 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2617 1, 2, 3, 4, 5, 6, 7, 8,
2618 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2619 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2620 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2621 1, 2, 3, 4, 5, 6, 7, 8,
2622 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2623 1234567891234LL, 987654321986LL,
2625 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2626 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f\n",
2627 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2628 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2629 1234567891234LL, 987654321986LL,
2631 printf("%d %d %d %d %d %d %d %d %Lf\n",
2632 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2633 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2634 LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
"%f %f %Lf\n",
2635 1, 2, 3, 4, 5, 6, 7, 8,
2636 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2637 1234567891234LL, 987654321986LL,
2639 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2640 "%Lf " LONG_LONG_FORMAT
" " LONG_LONG_FORMAT
" %f %f %Lf\n",
2641 1, 2, 3, 4, 5, 6, 7, 8,
2642 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2643 ld
, 1234567891234LL, 987654321986LL,
2648 va_arg_with_struct_ptr(va_list ap
) {
2650 * This was a BUG identified with FFTW-3.3.8 on arm64.
2651 * The test case only checks it compiles on all supported
2652 * architectures. This function is not currently called.
2654 struct X
{ int _x
; };
2655 struct X
*x
= va_arg(ap
, struct X
*);
2659 void vprintf1(const char *fmt
, ...)
2683 i
= va_arg(ap
, int);
2687 d
= va_arg(ap
, double);
2691 ll
= va_arg(ap
, long long);
2692 printf(LONG_LONG_FORMAT
, ll
);
2695 ld
= va_arg(ap
, LONG_DOUBLE
);
2713 #if CC_NAME == CC_clang /* clang7 doesn't support zero sized structs */
2726 void *foo
, *bar
, *baz
;
2729 struct mytest
stdarg_for_struct(struct myspace bob
, ...)
2731 struct myspace george
, bill
;
2732 struct myspace2 alex1
;
2733 struct myspace3 alex2
;
2734 struct myspace4 alex3
;
2739 alex1
= va_arg(ap
, struct myspace2
);
2740 alex2
= va_arg(ap
, struct myspace3
);
2741 alex3
= va_arg(ap
, struct myspace4
);
2742 bill
= va_arg(ap
, struct myspace
);
2743 george
= va_arg(ap
, struct myspace
);
2744 validate
= va_arg(ap
, int);
2745 printf("stdarg_for_struct: %d %d %d %d %d %d %d\n",
2746 alex2
.a
[0], alex3
.a
[0], alex3
.a
[1],
2747 bob
.profile
, bill
.profile
, george
.profile
, validate
);
2749 return (struct mytest
) {};
2752 void stdarg_for_libc(const char *fmt
, ...)
2755 va_start(args
, fmt
);
2760 void stdarg_syntax(int n
, ...)
2768 i
= va_arg(ap
, int);
2769 printf("stdarg_void_expr: %d\n", i
);
2776 point pts
[]={{1.0,2.0},{3.0,4.0},{5.0,6.0},{7.0,8.0},{9.0,10.0},{11.0,12.0}};
2778 static void stdarg_double_struct(int nargs
, int posd
,...)
2785 printf ("stdarg_double_struct: %d\n", posd
);
2786 va_start(args
,posd
);
2787 for(i
= 0; i
< nargs
; i
++) {
2789 d
= va_arg (args
, double);
2790 printf ("d %d = %g\n", i
, d
);
2793 pi
= va_arg (args
, point
);
2794 printf ("pts[%d] = %g %g\n", i
, pi
.x
, pi
.y
);
2800 void stdarg_test(void)
2802 LONG_DOUBLE ld
= 1234567891234LL;
2804 struct myspace2 bob2
;
2805 struct myspace3 bob3
;
2806 struct myspace4 bob4
;
2808 vprintf1("%d %d %d\n", 1, 2, 3);
2809 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2810 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2811 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2812 vprintf1("%d %f %l %F %d %f %l %F\n",
2813 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2814 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2815 1, 2, 3, 4, 5, 6, 7, 8,
2816 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2817 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2818 1, 2, 3, 4, 5, 6, 7, 8,
2819 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2820 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2822 1, 2, 3, 4, 5, 6, 7, 8,
2823 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2824 1234567891234LL, 987654321986LL,
2826 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2828 ld
, 1, 2, 3, 4, 5, 6, 7, 8,
2829 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2830 1234567891234LL, 987654321986LL,
2832 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2833 1, 2, 3, 4, 5, 6, 7, 8, ld
);
2834 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2836 1, 2, 3, 4, 5, 6, 7, 8,
2837 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2838 1234567891234LL, 987654321986LL,
2840 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2841 "%F %l %l %f %f %F\n",
2842 1, 2, 3, 4, 5, 6, 7, 8,
2843 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2844 ld
, 1234567891234LL, 987654321986LL,
2851 stdarg_for_struct(bob
, bob2
, bob3
, bob4
, bob
, bob
, bob
.profile
);
2852 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2853 stdarg_syntax(1, 17);
2854 stdarg_double_struct(6,-1,pts
[0],pts
[1],pts
[2],pts
[3],pts
[4],pts
[5]);
2855 stdarg_double_struct(7,1,pts
[0],-1.0,pts
[1],pts
[2],pts
[3],pts
[4],pts
[5]);
2856 stdarg_double_struct(7,2,pts
[0],pts
[1],-1.0,pts
[2],pts
[3],pts
[4],pts
[5]);
2857 stdarg_double_struct(7,3,pts
[0],pts
[1],pts
[2],-1.0,pts
[3],pts
[4],pts
[5]);
2858 stdarg_double_struct(7,4,pts
[0],pts
[1],pts
[2],pts
[3],-1.0,pts
[4],pts
[5]);
2859 stdarg_double_struct(7,5,pts
[0],pts
[1],pts
[2],pts
[3],pts
[4],-1.0,pts
[5]);
2862 int reltab
[3] = { 1, 2, 3 };
2864 int *rel1
= &reltab
[1];
2865 int *rel2
= &reltab
[2];
2867 void getmyaddress(void)
2869 printf("in getmyaddress\n");
2873 long __pa_symbol(void)
2875 /* This 64bit constant was handled incorrectly, it was used as addend
2876 (which can hold 64bit just fine) in connection with a symbol,
2877 and TCC generates wrong code for that (displacements are 32bit only).
2878 This effectively is "+ 0x80000000", and if addresses of globals
2879 are below 2GB the result should be a number without high 32 bits set. */
2880 return ((long)(((unsigned long)(&rel1
))) - (0xffffffff80000000UL
));
2884 uintptr_t theaddress
= (uintptr_t)getmyaddress
;
2885 void relocation_test(void)
2887 void (*fptr
)(void) = (void (*)(void))theaddress
;
2888 printf("*rel1=%d\n", *rel1
);
2889 printf("*rel2=%d\n", *rel2
);
2892 // compare 'addend' displacement versus conventional arithmetics
2893 printf("pa_symbol: %d\n", (long)&rel1
== __pa_symbol() - 0x80000000);
2897 void old_style_f(a
,b
,c
)
2901 printf("a=%d b=%d b=%f\n", a
, b
, c
);
2904 void decl_func1(int cmpfn())
2906 printf("cmpfn=%lx\n", (long)cmpfn
);
2909 void decl_func2(cmpfn
)
2912 printf("cmpfn=%lx\n", (long)cmpfn
);
2915 void old_style_function_test(void)
2917 #if CC_NAME == CC_clang
2918 /* recent clang versions (at least 15.0) raise an error:
2919 incompatible pointer to integer conversion passing 'void *'
2920 For the purpose of this test, pass 1 instead.
2922 old_style_f(1, 2, 3.0);
2924 old_style_f((void *)1, 2, 3.0);
2932 #if defined __i386__ || defined __x86_64__ || defined __arm__
2933 char *p
= alloca(16);
2934 strcpy(p
,"123456789012345");
2935 printf("alloca: p is %s\n", p
);
2936 char *demo
= "This is only a test.\n";
2937 /* Test alloca embedded in a larger expression */
2938 printf("alloca: %s\n", strcpy(alloca(strlen(demo
)+1),demo
) );
2942 void *bounds_checking_is_enabled()
2944 char ca
[10], *cp
= ca
-1;
2945 return (ca
!= cp
+ 1) ? cp
: NULL
;
2948 typedef int constant_negative_array_size_as_compile_time_assertion_idiom
[(1 ? 2 : 0) - 1];
2950 void c99_vla_test_1(int size1
, int size2
)
2952 int size
= size1
* size2
;
2953 int tab1
[size
][2], tab2
[10][2];
2954 void *tab1_ptr
, *tab2_ptr
, *bad_ptr
;
2956 /* "size" should have been 'captured' at tab1 declaration,
2957 so modifying it should have no effect on VLA behaviour. */
2960 printf("Test C99 VLA 1 (sizeof): ");
2961 printf("%s\n", (sizeof tab1
== size1
* size2
* 2 * sizeof(int)) ? "PASSED" : "FAILED");
2964 printf("Test C99 VLA 2 (ptrs subtract): ");
2965 printf("%s\n", (tab2
- tab1
== (tab2_ptr
- tab1_ptr
) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2966 printf("Test C99 VLA 3 (ptr add): ");
2967 printf("%s\n", &tab1
[5][1] == (tab1_ptr
+ (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2968 printf("Test C99 VLA 4 (ptr access): ");
2969 tab1
[size1
][1] = 42;
2970 printf("%s\n", (*((int *) (tab1_ptr
+ (size1
* 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2972 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2973 if (bad_ptr
= bounds_checking_is_enabled()) {
2974 int *t1
= &tab1
[size1
* size2
- 1][3];
2975 int *t2
= &tab2
[9][3];
2976 printf("%s ", bad_ptr
== t1
? "PASSED" : "FAILED");
2977 printf("%s ", bad_ptr
== t2
? "PASSED" : "FAILED");
2979 char*c1
= 1 + sizeof(tab1
) + (char*)tab1
;
2980 char*c2
= 1 + sizeof(tab2
) + (char*)tab2
;
2981 printf("%s ", bad_ptr
== c1
? "PASSED" : "FAILED");
2982 printf("%s ", bad_ptr
== c2
? "PASSED" : "FAILED");
2986 printf("%s ", bad_ptr
== i1
? "PASSED" : "FAILED");
2987 printf("%s ", bad_ptr
== i2
? "PASSED" : "FAILED");
2989 int *x1
= tab1
[size1
* size2
+ 1];
2990 int *x2
= tab2
[10 + 1];
2991 printf("%s ", bad_ptr
== x1
? "PASSED" : "FAILED");
2992 printf("%s ", bad_ptr
== x2
? "PASSED" : "FAILED");
2994 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2999 void c99_vla_test_2(int d
, int h
, int w
)
3002 int (*arr
)[h
][w
] = malloc(sizeof(int) * d
*h
*w
);
3004 static int (*starr
)[h
][w
];
3006 printf("Test C99 VLA 6 (pointer)\n");
3008 for (z
=0; z
<d
; z
++) {
3009 for (y
=0; y
<h
; y
++) {
3010 for (x
=0; x
<w
; x
++) {
3015 for (z
=0; z
<d
; z
++) {
3016 for (y
=0; y
<h
; y
++) {
3017 for (x
=0; x
<w
; x
++) {
3018 printf(" %2d", arr
[z
][y
][x
]);
3025 printf(" sizes : %d %d %d\n"
3027 " tests : %d %d %d\n",
3028 sizeof (*arr
), sizeof (*arr
)[0], sizeof (*arr
)[0][0],
3029 arr
+ 2 - arr
, *arr
+ 3 - *arr
,
3030 0 == sizeof (*arr
+ 1) - sizeof arr
,
3031 0 == sizeof sizeof *arr
- sizeof arr
,
3032 starr
[0][2][3] == arr
[1][2][3]
3037 void c99_vla_test_3a (int arr
[2][3][4])
3039 printf ("%d\n", arr
[1][2][3]);
3042 void c99_vla_test_3b(int s
, int arr
[s
][3][4])
3044 printf ("%d\n", arr
[1][2][3]);
3047 void c99_vla_test_3c(int s
, int arr
[2][s
][4])
3049 printf ("%d\n", arr
[1][2][3]);
3052 void c99_vla_test_3d(int s
, int arr
[2][3][s
])
3054 printf ("%d\n", arr
[1][2][3]);
3057 void c99_vla_test_3e(int s
, int arr
[][3][--s
])
3059 printf ("%d %d\n", s
, arr
[1][2][3]);
3062 void c99_vla_test_3(void)
3066 memset (a
, 0, sizeof(a
));
3069 c99_vla_test_3b(2, a
);
3070 c99_vla_test_3c(3, a
);
3071 c99_vla_test_3d(4, a
);
3072 c99_vla_test_3e(5, a
);
3075 void c99_vla_test(void)
3077 c99_vla_test_1(5, 2);
3078 c99_vla_test_2(3, 4, 5);
3083 void sizeof_test(void)
3088 printf("sizeof(int) = %d\n", sizeof(int));
3089 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
3090 printf("sizeof(long) = %d\n", sizeof(long));
3091 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
3092 printf("sizeof(short) = %d\n", sizeof(short));
3093 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
3094 printf("sizeof(char) = %d\n", sizeof(char));
3095 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
3096 printf("sizeof(func) = %d\n", sizeof sizeof_test());
3098 printf("sizeof(a++) = %d\n", sizeof a
++);
3099 printf("a=%d\n", a
);
3101 printf("sizeof(**ptr) = %d\n", sizeof (**ptr
));
3103 /* The type of sizeof should be as large as a pointer, actually
3104 it should be size_t. */
3105 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
3108 /* Effectively <<32, but defined also on 32bit machines. */
3112 /* This checks that sizeof really can be used to manipulate
3113 uintptr_t objects, without truncation. */
3114 t2
= t
& -sizeof(uintptr_t);
3115 printf ("%lu %lu\n", t
, t2
);
3117 /* some alignof tests */
3118 printf("__alignof__(int) = %d\n", __alignof__(int));
3119 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
3120 printf("__alignof__(short) = %d\n", __alignof__(short));
3121 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
3122 printf("__alignof__(char) = %d\n", __alignof__(char));
3123 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
3124 printf("__alignof__(func) = %d\n", __alignof__
sizeof_test());
3126 /* sizes of VLAs need to be evaluated even inside sizeof: */
3128 printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a
]));
3129 /* And checking if sizeof compound literal works. Parenthesized: */
3130 printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n",
3131 sizeof( (struct {int i
; int j
;}){4,5} ));
3132 /* And as direct sizeof argument (as unary expression): */
3133 printf("sizeof (struct {short i; short j;}){4,5} = %d\n",
3134 sizeof (struct {short i
; short j
;}){4,5} );
3136 /* sizeof(x && y) should be sizeof(int), even if constant
3137 evaluating is possible. */
3138 printf("sizeof(t && 0) = %d\n", sizeof(t
&& 0));
3139 printf("sizeof(1 && 1) = %d\n", sizeof(1 && 1));
3140 printf("sizeof(t || 1) = %d\n", sizeof(t
|| 1));
3141 printf("sizeof(0 || 0) = %d\n", sizeof(0 || 0));
3144 void typeof_test(void)
3153 printf("a=%f b=%f c=%f\n", a
, b
, c
);
3159 struct hlist_node
*first
, *last
;
3162 void consume_ulong (unsigned long i
)
3167 void statement_expr_test(void)
3171 /* Basic stmt expr test */
3181 printf("a=%d\n", a
);
3183 /* Test that symbols aren't freed prematurely.
3184 With SYM_DEBUG valgrind will show a read from a freed
3185 symbol, and tcc will show an (invalid) warning on the initialization
3186 of 'ptr' below, if symbols are popped after the stmt expr. */
3187 void *v
= (void*)39;
3189 (struct hlist_node
*)v
;
3192 ptr
= (struct hlist_node
*)v
;
3194 /* This part used to segfault when symbols were popped prematurely.
3195 The symbols for the static local would be overwritten with
3196 helper symbols from the pre-processor expansions in between. */
3197 #define some_attr __attribute__((aligned(1)))
3198 #define tps(str) ({ \
3199 static const char *t some_attr = str; \
3202 printf ("stmtexpr: %s %s\n",
3203 tps("somerandomlongstring"),
3204 tps("anotherlongstring"));
3206 /* Test that the three decls of 't' don't interact. */
3208 int b
= ({ int t
= 41; t
; });
3209 int c
= ({ int t
= 42; t
; });
3211 /* Test that aggregate return values work. */
3214 typedef struct hlist_head T
;
3216 T t
= { (void*)43, (void*)44 };
3220 printf ("stmtexpr: %d %d %d\n", t
, b
, c
);
3221 printf ("stmtexpr: %ld %ld\n", (long)h
.first
, (long)h
.last
);
3223 /* Test that we can give out addresses of local labels. */
3224 consume_ulong(({ __label__ __here
; __here
: (unsigned long)&&__here
; }));
3226 /* Test interaction between local and global label stacks and the
3227 need to defer popping symbol from them when within statement
3228 expressions. Note how the labels are both named LBL. */
3233 LBL
: if (i
++ == 0) goto LBL
;
3235 /* jump to a classical label out of an expr-stmt that had previously
3236 overshadowed that classical label */
3240 printf("stmtexpr: %d should be 2\n", i
);
3243 void local_label_test(void)
3249 __label__ l1
, l2
, l3
, l4
;
3263 printf("a=%d\n", a
);
3273 /* inline assembler test */
3274 #if defined(__i386__) || defined(__x86_64__)
3276 typedef __SIZE_TYPE__ word
;
3278 /* from linux kernel */
3279 static char * strncat1(char * dest
,const char * src
,size_t count
)
3281 word d0
, d1
, d2
, d3
;
3282 __asm__
__volatile__(
3291 "testb %%al,%%al\n\t"
3295 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
3296 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
3301 static char * strncat2(char * dest
,const char * src
,size_t count
)
3303 word d0
, d1
, d2
, d3
;
3304 __asm__
__volatile__(
3305 "repne scasb\n\t" /* one-line repne prefix + string op */
3312 "testb %%al,%%al\n\t"
3316 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
3317 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffff), "g" (count
)
3322 static inline void * memcpy1(void * to
, const void * from
, size_t n
)
3325 __asm__
__volatile__(
3330 "1:\ttestb $1,%b4\n\t"
3334 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
3335 :"0" (n
/4), "q" (n
),"1" ((word
) to
),"2" ((word
) from
)
3340 static inline void * memcpy2(void * to
, const void * from
, size_t n
)
3343 __asm__
__volatile__(
3344 "rep movsl\n\t" /* one-line rep prefix + string op */
3348 "1:\ttestb $1,%b4\n\t"
3352 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
3353 :"0" (n
/4), "q" (n
),"1" ((word
) to
),"2" ((word
) from
)
3358 static __inline__
void sigaddset1(unsigned int *set
, int _sig
)
3360 __asm__("btsl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc");
3363 static __inline__
void sigdelset1(unsigned int *set
, int _sig
)
3365 asm("btrl %1,%0" : "=m"(*set
) : "Ir"(_sig
- 1) : "cc", "flags");
3369 /* clang's inline asm is uncapable of 'xchgb %b0,%h0' */
3370 static __inline__ __const__
unsigned int swab32(unsigned int x
)
3372 return ((x
>> 24) & 0xff) |
3373 ((x
>> 8) & 0xff00) |
3374 ((x
<< 8) & 0xff0000) |
3375 ((x
<< 24) & 0xff000000);
3378 static __inline__ __const__
unsigned int swab32(unsigned int x
)
3380 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
3381 "rorl $16,%0\n\t" /* swap words */
3382 "xchgb %b0,%h0" /* swap higher bytes */
3389 static __inline__
unsigned long long mul64(unsigned int a
, unsigned int b
)
3391 unsigned long long res
;
3393 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
3394 but still test the 32bit->64bit mull. */
3395 unsigned int resh
, resl
;
3396 __asm__("mull %2" : "=a" (resl
), "=d" (resh
) : "a" (a
), "r" (b
));
3397 res
= ((unsigned long long)resh
<< 32) | resl
;
3399 __asm__("mull %2" : "=A" (res
) : "a" (a
), "r" (b
));
3404 static __inline__
unsigned long long inc64(unsigned long long a
)
3406 unsigned long long res
;
3408 /* Using the A constraint is wrong, and increments are tested
3412 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res
) : "A" (a
));
3425 word
mconstraint_test(struct struct1231
*r
)
3430 __asm__
volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
3431 : "=&r" (ret
), "=m" (a
)
3432 : "m" (*(struct struct123
*)r
->addr
));
3437 int fls64(unsigned long long x
)
3447 void other_constraints_test(void)
3451 #if CC_NAME != CC_clang
3452 __asm__
volatile ("mov %P1,%0" : "=r" (ret
) : "p" (&var
));
3453 printf ("oc1: %d\n", ret
== (word
)&var
);
3458 /* Test global asm blocks playing with aliases. */
3459 void base_func(void)
3461 printf ("asmc: base\n");
3465 extern void override_func1 (void);
3466 extern void override_func2 (void);
3468 asm(".weak override_func1\n.set override_func1, base_func");
3469 asm(".set override_func1, base_func");
3470 asm(".set override_func2, base_func");
3472 void override_func2 (void)
3474 printf ("asmc: override2\n");
3477 /* This checks a construct used by the linux kernel to encode
3478 references to strings by PC relative references. */
3479 extern int bug_table
[] __attribute__((section("__bug_table")));
3480 char * get_asm_string (void)
3482 /* On i386 when -fPIC is enabled this would cause a compile error with GCC,
3483 the problem being the "i" constraint used with a symbolic operand
3484 resolving to a local label. That check is overly zealous as the code
3485 within the asm makes sure to use it only in PIC-possible contexts,
3486 but all GCC versions behave like so. We arrange for PIC to be disabled
3487 for compiling tcctest.c in the Makefile.
3489 Additionally the usage of 'c' in "%c0" in the template is actually wrong,
3490 as that would expect an operand that is a condition code. The operand
3491 as is (a local label) is accepted by GCC in non-PIC mode, and on x86-64.
3492 What the linux kernel really wanted is 'p' to disable the addition of '$'
3493 to the printed operand (as in "$.LC0" where the template only wants the
3494 bare operand ".LC0"). But the code below is what the linux kernel
3495 happens to use and as such is the one we want to test. */
3497 extern int some_symbol
;
3498 asm volatile (".globl some_symbol\n"
3501 "some_symbol: .long 0\n"
3502 ".pushsection __bug_table, \"a\"\n"
3503 ".globl bug_table\n"
3505 /* The first entry (1b-2b) is unused in this test,
3506 but we include it to check if cross-section
3507 PC-relative references work. */
3508 "2:\t.long 1b - 2b, %c0 - 2b\n"
3509 ".popsection\n" : : "i" ("A string"));
3510 char * str
= ((char*)bug_table
) + bug_table
[1];
3513 return (char *) "A string";
3517 /* This checks another constructs with local labels. */
3518 extern unsigned char alld_stuff
[];
3525 ".pushsection .data.ignore\n"
3526 ".long 661b - .\n" /* This reference to 661 generates an external sym
3527 which shouldn't somehow overwrite the offset that's
3528 already determined for it. */
3530 ".byte 662b - 661b\n" /* So that this value is undeniably 1. */);
3532 void asm_local_label_diff (void)
3534 printf ("asm_local_label_diff: %d %d\n", alld_stuff
[0], alld_stuff
[1]);
3539 /* This checks that static local variables are available from assembler. */
3540 void asm_local_statics (void)
3542 static int localint
= 41;
3543 asm("incl %0" : "+m" (localint
));
3544 printf ("asm_local_statics: %d\n", localint
);
3550 void fancy_copy (unsigned *in
, unsigned *out
)
3552 asm volatile ("" : "=r" (*out
) : "0" (*in
));
3555 void fancy_copy2 (unsigned *in
, unsigned *out
)
3557 asm volatile ("mov %0,(%1)" : : "r" (*in
), "r" (out
) : "memory");
3560 #if defined __x86_64__
3561 void clobber_r12(void)
3563 asm volatile("mov $1, %%r12" ::: "r12");
3567 void test_high_clobbers_really(void)
3569 #if defined __x86_64__
3570 register word val
asm("r12");
3572 /* This tests if asm clobbers correctly save/restore callee saved
3573 registers if they are clobbered and if it's the high 8 x86-64
3574 registers. This is fragile for GCC as the constraints do not
3575 correctly capture the data flow, but good enough for us. */
3576 asm volatile("mov $0x4542, %%r12" : "=r" (val
):: "memory");
3578 asm volatile("mov %%r12, %0" : "=r" (val2
) : "r" (val
): "memory");
3579 printf("asmhc: 0x%x\n", val2
);
3583 void test_high_clobbers(void)
3585 #if defined __x86_64__
3587 asm volatile("mov %%r12,%0" :: "m" (x1
)); /* save r12 */
3588 test_high_clobbers_really();
3589 asm volatile("mov %%r12,%0" :: "m" (x2
)); /* new r12 */
3590 asm volatile("mov %0,%%r12" :: "m" (x1
)); /* restore r12 */
3591 /* should be 0 but tcc doesn't save r12 automatically, which has
3592 bad effects when gcc helds TCCState *s in r12 in tcc.c:main */
3593 //printf("r12-clobber-diff: %lx\n", x2 - x1);
3597 static long cpu_number
;
3598 void trace_console(long len
, long len2
)
3601 /* This generated invalid code when the emission of the switch
3602 table isn't disabled. The asms are necessary to show the bug,
3603 normal statements don't work (they need to generate some code
3604 even under nocode_wanted, which normal statements don't do,
3605 but asms do). Also at least these number of cases is necessary
3606 to generate enough "random" bytes. They ultimately are enough
3607 to create invalid instruction patterns to which the first
3608 skip-to-decision-table jump jumps. If decision table emission
3609 is disabled all of this is no problem.
3611 It also is necessary that the switches are in a statement expression
3612 (which has the property of not being enterable from outside. no
3623 case 8: printf("bla"); pfo_ret__
= 42; break;
3625 pscr_ret__
= pfo_ret__
;
3632 case 1:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3633 case 2:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3634 case 4:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3635 case 8:asm("movq %1,%0": "=r" (pfo_ret__
) : "m" (cpu_number
)); break;
3636 default: printf("impossible\n");
3638 pscr_ret__
= pfo_ret__
;
3650 void test_asm_dead_code(void)
3653 /* Try to make sure that xdi contains a zero, and hence will
3654 lead to a segfault if the next asm is evaluated without
3655 arguments being set up. */
3656 asm volatile ("" : "=D" (rdi
) : "0" (0));
3659 /* This shouldn't trigger a segfault, either the argument
3660 registers need to be set up and the asm emitted despite
3661 this being in an unevaluated context, or both the argument
3662 setup _and_ the asm emission need to be suppressed. The latter
3663 is better. Disabling asm code gen when suppression is on
3664 also fixes the above trace_console bug, but that came earlier
3665 than asm suppression. */
3666 asm volatile ("movl $0,(%0)" : : "D" (&var
) : "memory");
3671 void test_asm_call(void)
3673 #if defined __x86_64__ && !defined _WIN64 && !defined(__APPLE__)
3674 static char str
[] = "PATH";
3676 /* This tests if a reference to an undefined symbol from an asm
3677 block, which isn't otherwise referenced in this file, is correctly
3678 regarded as global symbol, so that it's resolved by other object files
3679 or libraries. We chose getenv here, which isn't used anywhere else
3680 in this file. (If we used e.g. printf, which is used we already
3681 would have a global symbol entry, not triggering the bug which is
3683 /* two pushes so stack remains aligned */
3684 asm volatile ("push %%rdi; push %%rdi; mov %0, %%rdi;"
3685 #if 1 && !defined(__TINYC__) && (defined(__PIC__) || defined(__PIE__))
3690 "pop %%rdi; pop %%rdi"
3691 : "=a" (s
) : "r" (str
));
3692 printf("asmd: %s\n", s
);
3696 #if defined __x86_64__
3697 # define RX "(%rip)"
3702 void asm_dot_test(void)
3710 asm(".text; lea S"RX
",%eax; lea ."RX
",%ecx; sub %ecx,%eax; S=.; jmp p0");
3713 /* clangs internal assembler is broken */
3714 asm(".text; jmp .+6; .int 123; mov .-4"RX
",%eax; jmp p0");
3716 asm(".text; mov $123, %eax; jmp p0");
3719 #if !defined(_WIN32) && !defined(__clang__)
3720 asm(".pushsection \".data\"; Y=.; .int 999; X=Y; .int 456; X=.-4; .popsection");
3722 asm(".data; Y=.; .int 999; X=Y; .int 456; X=.-4; .text");
3724 asm(".text; mov X"RX
",%eax; jmp p0");
3727 /* Bah! Clang! Doesn't want to redefine 'X' */
3728 asm(".text; mov $789,%eax; jmp p0");
3731 asm(".data; X=.; .int 789; Y=.; .int 999; .previous");
3733 asm(".data; X=.; .int 789; Y=.; .int 999; .text");
3735 asm(".text; mov X"RX
",%eax; X=Y; jmp p0");
3738 asm(".text; p0=.; mov %%eax,%0;" : "=m"(r
)); break;
3742 printf("asm_dot_test %d: %d\n", x
, r
);
3750 unsigned int val
, val2
;
3751 struct struct123 s1
;
3752 struct struct1231 s2
= { (word
)&s1
};
3753 /* Hide the outer base_func, but check later that the inline
3754 asm block gets the outer one. */
3756 void override_func3 (void);
3761 register int regvar
asm("%esi");
3763 // parse 0x1E-1 as 3 tokens in asm mode
3764 asm volatile ("mov $0x1E-1,%eax");
3766 /* test the no operand case */
3767 asm volatile ("xorl %eax, %eax");
3769 memcpy1(buf
, "hello", 6);
3770 strncat1(buf
, " worldXXXXX", 3);
3771 printf("%s\n", buf
);
3773 memcpy2(buf
, "hello", 6);
3774 strncat2(buf
, " worldXXXXX", 3);
3775 printf("%s\n", buf
);
3777 /* 'A' constraint test */
3778 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
3779 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
3783 printf("mconstraint: %d", mconstraint_test(&s2
));
3784 printf(" %d %d\n", s1
.a
, s1
.b
);
3785 other_constraints_test();
3787 sigdelset1(&set
, 2);
3788 sigaddset1(&set
, 16);
3789 /* NOTE: we test here if C labels are correctly restored after the
3793 __asm__("btsl %1,%0" : "=m"(set
) : "Ir"(20) : "cc");
3794 printf("set=0x%x\n", set
);
3796 printf("swab32(0x%08x) = 0x%0x\n", val
, swab32(val
));
3801 /* The base_func ref from the following inline asm should find
3802 the global one, not the local decl from this function. */
3803 asm volatile(".weak override_func3\n.set override_func3, base_func");
3805 printf("asmstr: %s\n", get_asm_string());
3806 asm_local_label_diff();
3809 asm_local_statics();
3811 /* clang can't deal with the type change */
3812 /* Check that we can also load structs of appropriate layout
3814 asm volatile("" : "=r" (asmret
) : "0"(s2
));
3815 if (asmret
!= s2
.addr
)
3816 printf("asmstr: failed\n");
3819 /* Check that the typesize correctly sets the register size to
3821 asm volatile("cmp %1,%2; sete %0" : "=a"(somebool
) : "r"(1), "r"(2));
3823 printf("asmbool: failed\n");
3826 fancy_copy (&val
, &val2
);
3827 printf ("fancycpy(%d)=%d\n", val
, val2
);
3829 fancy_copy2 (&val
, &val2
);
3830 printf ("fancycpy2(%d)=%d\n", val
, val2
);
3831 asm volatile ("mov $0x4243, %%esi" : "=r" (regvar
));
3832 printf ("regvar=%x\n", regvar
);
3833 test_high_clobbers();
3834 trace_console(8, 8);
3835 test_asm_dead_code();
3851 #define COMPAT_TYPE(type1, type2) \
3853 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
3854 __builtin_types_compatible_p (type1, type2));\
3862 /* __builtin_clz and __builtin_ctz return random values for 0 */
3863 static void builtin_test_bits(unsigned long long x
, int cnt
[])
3866 cnt
[0] += __builtin_ffs(x
);
3867 cnt
[1] += __builtin_ffsl(x
);
3868 cnt
[2] += __builtin_ffsll(x
);
3870 if ((unsigned int) x
) cnt
[3] += __builtin_clz(x
);
3871 if ((unsigned long) x
) cnt
[4] += __builtin_clzl(x
);
3872 if ((unsigned long long) x
) cnt
[5] += __builtin_clzll(x
);
3874 if ((unsigned int) x
) cnt
[6] += __builtin_ctz(x
);
3875 if ((unsigned long) x
) cnt
[7] += __builtin_ctzl(x
);
3876 if ((unsigned long long) x
) cnt
[8] += __builtin_ctzll(x
);
3878 #if CC_NAME != CC_clang || GCC_MAJOR >= 11
3879 /* Apple clang 10 does not have __builtin_clrsb[l[l]] */
3880 cnt
[9] += __builtin_clrsb(x
);
3881 cnt
[10] += __builtin_clrsbl(x
);
3882 cnt
[11] += __builtin_clrsbll(x
);
3885 cnt
[12] += __builtin_popcount(x
);
3886 cnt
[13] += __builtin_popcountl(x
);
3887 cnt
[14] += __builtin_popcountll(x
);
3889 cnt
[15] += __builtin_parity(x
);
3890 cnt
[16] += __builtin_parityl(x
);
3891 cnt
[17] += __builtin_parityll(x
);
3895 void builtin_test(void)
3901 COMPAT_TYPE(int, int);
3902 COMPAT_TYPE(int, unsigned int);
3903 COMPAT_TYPE(int, char);
3904 COMPAT_TYPE(int, const int);
3905 COMPAT_TYPE(int, volatile int);
3906 COMPAT_TYPE(int *, int *);
3907 COMPAT_TYPE(int *, void *);
3908 COMPAT_TYPE(int *, const int *);
3909 COMPAT_TYPE(char *, unsigned char *);
3910 COMPAT_TYPE(char *, signed char *);
3911 COMPAT_TYPE(char *, char *);
3912 /* space is needed because tcc preprocessor introduces a space between each token */
3913 COMPAT_TYPE(char * *, void *);
3915 printf("res1 = %d\n", __builtin_constant_p(1));
3916 printf("res2 = %d\n", __builtin_constant_p(1 + 2));
3917 printf("res3 = %d\n", __builtin_constant_p(&constant_p_var
));
3918 printf("res4 = %d\n", __builtin_constant_p(constant_p_var
));
3919 printf("res5 = %d\n", __builtin_constant_p(100000 / constant_p_var
));
3921 /* clang doesn't regard this as constant expression */
3922 printf("res6 = 1\n");
3924 printf("res6 = %d\n", __builtin_constant_p(i
&& 0));
3926 printf("res7 = %d\n", __builtin_constant_p(i
&& 1));
3928 /* clang doesn't regard this as constant expression */
3929 printf("res8 = 1\n");
3931 printf("res8 = %d\n", __builtin_constant_p(i
&& 0 ? i
: 34));
3933 printf("res9 = %d\n", __builtin_constant_p("hi"));
3934 printf("res10 = %d\n", __builtin_constant_p(func()));
3935 printf("res11 = %d\n", __builtin_constant_p((i
++, 7)));
3938 i
= __builtin_choose_expr (1 != 0, ll
, s
);
3939 printf("bce: %d\n", i
);
3940 i
= __builtin_choose_expr (1 != 1, ll
, s
);
3941 printf("bce: %d\n", i
);
3942 i
= sizeof (__builtin_choose_expr (1, ll
, s
));
3943 printf("bce: %d\n", i
);
3944 i
= sizeof (__builtin_choose_expr (0, ll
, s
));
3945 printf("bce: %d\n", i
);
3947 //printf("bera: %p\n", __builtin_extract_return_addr((void*)43));
3951 unsigned long long r
= 0;
3953 memset(cnt
, 0, sizeof(cnt
));
3954 builtin_test_bits(0, cnt
);
3955 builtin_test_bits(0xffffffffffffffffull
, cnt
);
3956 for (i
= 0; i
< 64; i
++)
3957 builtin_test_bits(1ull << i
, cnt
);
3958 for (i
= 0; i
< 1000; i
++) {
3959 r
= 0x5851f42d4c957f2dull
* r
+ 0x14057b7ef767814full
;
3960 builtin_test_bits(r
, cnt
);
3962 for (i
= 0; i
< 18; i
++)
3963 printf ("%d %d\n", i
, cnt
[i
]);
3968 void weak_test(void) {}
3970 extern int __attribute__((weak
)) weak_f1(void);
3971 extern int __attribute__((weak
)) weak_f2(void);
3972 extern int weak_f3(void);
3973 extern int __attribute__((weak
)) weak_v1
;
3974 extern int __attribute__((weak
)) weak_v2
;
3977 extern int (*weak_fpa
)() __attribute__((weak
));
3978 extern int __attribute__((weak
)) (*weak_fpb
)();
3979 extern __attribute__((weak
)) int (*weak_fpc
)();
3981 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak
));
3982 extern int __attribute((weak
)) weak_asm_f2(void) asm("weak_asm_f2x") ;
3983 extern int __attribute((weak
)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak
));
3984 extern int weak_asm_v1
asm("weak_asm_v1x") __attribute((weak
));
3985 extern int __attribute((weak
)) weak_asm_v2
asm("weak_asm_v2x") ;
3986 extern int __attribute((weak
)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak
));
3989 static const size_t dummy
= 0;
3990 extern __typeof(dummy
) weak_dummy1
__attribute__((weak
, alias("dummy")));
3991 extern __typeof(dummy
) __attribute__((weak
, alias("dummy"))) weak_dummy2
;
3992 extern __attribute__((weak
, alias("dummy"))) __typeof(dummy
) weak_dummy3
;
3995 int some_lib_func(void);
3996 int dummy_impl_of_slf(void) { return 444; }
3998 int some_lib_func(void) __attribute__((weak
, alias("dummy_impl_of_slf")));
4001 int weak_toolate() __attribute__((weak
));
4002 int weak_toolate() { return 0; }
4004 void __attribute__((weak
)) weak_test(void)
4006 printf("weak_f1=%d\n", weak_f1
? weak_f1() : 123);
4007 printf("weak_f2=%d\n", weak_f2
? weak_f2() : 123);
4008 printf("weak_f3=%d\n", weak_f3
? weak_f3() : 123);
4009 printf("weak_v1=%d\n",&weak_v1
? weak_v1
: 123);
4010 printf("weak_v2=%d\n",&weak_v2
? weak_v2
: 123);
4011 printf("weak_v3=%d\n",&weak_v3
? weak_v3
: 123);
4013 printf("weak_fpa=%d\n",&weak_fpa
? weak_fpa() : 123);
4014 printf("weak_fpb=%d\n",&weak_fpb
? weak_fpb() : 123);
4015 printf("weak_fpc=%d\n",&weak_fpc
? weak_fpc() : 123);
4017 printf("weak_asm_f1=%d\n", weak_asm_f1
!= NULL
);
4018 printf("weak_asm_f2=%d\n", weak_asm_f2
!= NULL
);
4019 printf("weak_asm_f3=%d\n", weak_asm_f3
!= NULL
);
4020 printf("weak_asm_v1=%d\n",&weak_asm_v1
!= NULL
);
4021 printf("weak_asm_v2=%d\n",&weak_asm_v2
!= NULL
);
4022 printf("weak_asm_v3=%d\n",&weak_asm_v3
!= NULL
);
4024 printf("some_lib_func=444\n");
4026 printf("some_lib_func=%d\n", &some_lib_func
? some_lib_func() : 0);
4030 int __attribute__((weak
)) weak_f2() { return 222; }
4031 int __attribute__((weak
)) weak_f3() { return 333; }
4032 int __attribute__((weak
)) weak_v2
= 222;
4033 int __attribute__((weak
)) weak_v3
= 333;
4036 void const_func(const int a
)
4040 void const_warn_test(void)
4049 int getme (struct condstruct
*s
, int i
)
4051 int i1
= (i
== 0 ? 0 : s
)->i
;
4052 int i2
= (i
== 0 ? s
: 0)->i
;
4053 int i3
= (i
== 0 ? (void*)0 : s
)->i
;
4054 int i4
= (i
== 0 ? s
: (void*)0)->i
;
4055 return i1
+ i2
+ i3
+ i4
;
4064 struct global_data global_data
;
4066 int global_data_getstuff (int *, int);
4068 void global_data_callit (int i
)
4070 *global_data
.b
[i
] = global_data_getstuff (global_data
.b
[i
], 1);
4073 int global_data_getstuff (int *p
, int i
)
4078 void global_data_test (void)
4080 global_data
.a
[0] = 42;
4081 global_data
.b
[0] = &global_data
.a
[0];
4082 global_data_callit (0);
4083 printf ("%d\n", global_data
.a
[0]);
4088 unsigned char fill
: 3;
4089 unsigned char b1
: 1;
4090 unsigned char b2
: 1;
4091 unsigned char fill2
: 3;
4094 int glob1
, glob2
, glob3
;
4096 void compare_comparisons (struct cmpcmpS
*s
)
4098 if (s
->b1
!= (glob1
== glob2
)
4099 || (s
->b2
!= (glob1
== glob3
)))
4100 printf ("comparing comparisons broken\n");
4103 void cmp_comparison_test(void)
4107 glob1
= 42; glob2
= 42;
4110 compare_comparisons (&s
);
4113 int fcompare (double a
, double b
, int code
)
4116 case 0: return a
== b
;
4117 case 1: return a
!= b
;
4118 case 2: return a
< b
;
4119 case 3: return a
>= b
;
4120 case 4: return a
> b
;
4121 case 5: return a
<= b
;
4126 void math_cmp_test(void)
4128 double nan
= 0.0/0.0;
4133 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
4135 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
4136 And it does this in various ways so that all code generation paths
4137 are checked (generating inverted tests, or non-inverted tests, or
4138 producing a 0/1 value without jumps (that's done in the fcompare
4140 #define FCMP(a,b,op,iop,code) \
4141 if (fcompare (a,b,code)) \
4142 bug (a,b,op,iop,1); \
4144 bug (a,b,op,iop,2); \
4148 bug (a,b,op,iop,3); \
4149 if ((a op b) || comp) \
4150 bug (a,b,op,iop,4); \
4151 if ((a iop b) || comp) \
4154 bug (a,b,op,iop,5); \
4155 if (v = !(a op b), !v) bug(a,b,op,iop,7);
4157 /* Equality tests. */
4158 FCMP(nan
, nan
, ==, !=, 0);
4159 FCMP(one
, two
, ==, !=, 0);
4160 FCMP(one
, one
, !=, ==, 1);
4161 /* Non-equality is a bit special. */
4162 if (!fcompare (nan
, nan
, 1))
4163 bug (nan
, nan
, !=, ==, 6);
4165 /* Relational tests on numbers. */
4166 FCMP(two
, one
, <, >=, 2);
4167 FCMP(one
, two
, >=, <, 3);
4168 FCMP(one
, two
, >, <=, 4);
4169 FCMP(two
, one
, <=, >, 5);
4171 /* Relational tests on NaNs. Note that the inverse op here is
4172 always !=, there's no operator in C that is equivalent to !(a < b),
4173 when NaNs are involved, same for the other relational ops. */
4174 FCMP(nan
, nan
, <, !=, 2);
4175 FCMP(nan
, nan
, >=, !=, 3);
4176 FCMP(nan
, nan
, >, !=, 4);
4177 FCMP(nan
, nan
, <=, !=, 5);
4180 double get100 () { return 100.0; }
4182 void callsave_test(void)
4184 #if defined __i386__ || defined __x86_64__ || defined __arm__
4185 int i
, s
; double *d
; double t
;
4186 s
= sizeof (double);
4187 printf ("callsavetest: %d\n", s
);
4188 d
= alloca (sizeof(double));
4190 /* x86-64 had a bug were the next call to get100 would evict
4191 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
4192 in int type, not pointer type. When alloca returns a pointer
4193 with the high 32 bit set (which is likely on x86-64) the access
4194 generates a segfault. */
4195 i
= d
[0] > get100 ();
4201 void bfa3(ptrdiff_t str_offset
)
4203 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset
);
4205 void bfa2(ptrdiff_t str_offset
)
4207 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset
);
4210 void bfa1(ptrdiff_t str_offset
)
4212 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset
);
4216 void builtin_frame_address_test(void)
4218 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
4220 char str
[] = "__builtin_frame_address";
4221 char *fp0
= __builtin_frame_address(0);
4223 printf("str: %s\n", str
);
4224 #ifndef __riscv // gcc dumps core. tcc, clang work
4230 char via_volatile (char i
)
4237 void volatile_test(void)
4239 if (via_volatile (42) != 42)
4240 printf (" broken\n");
4245 struct __attribute__((__packed__
)) Spacked
{
4250 struct Spacked spacked
;
4251 typedef struct __attribute__((__packed__
)) {
4257 typedef struct Spacked3_s
{
4261 } __attribute__((__packed__
)) Spacked3
;
4263 struct gate_struct64
{
4264 unsigned short offset_low
;
4265 unsigned short segment
;
4266 unsigned ist
: 3, zero0
: 5, type
: 5, dpl
: 2, p
: 1;
4267 unsigned short offset_middle
;
4268 unsigned offset_high
;
4270 } __attribute__((packed
));
4271 typedef struct gate_struct64 gate_desc
;
4272 gate_desc a_gate_desc
;
4273 void attrib_test(void)
4276 printf("attr: %d %d %d %d\n", sizeof(struct Spacked
),
4277 sizeof(spacked
), sizeof(Spacked2
), sizeof(spacked2
));
4278 printf("attr: %d %d\n", sizeof(Spacked3
), sizeof(spacked3
));
4279 printf("attr: %d %d\n", sizeof(gate_desc
), sizeof(a_gate_desc
));
4282 extern __attribute__((__unused__
)) char * __attribute__((__unused__
)) *
4283 strange_attrib_placement (void);
4285 void * __attribute__((__unused__
)) get_void_ptr (void *a
)
4290 /* This part checks for a bug in TOK_GET (used for inline expansion),
4291 where the large long long constant left the the high bits set for
4292 the integer constant token. */
4294 int __get_order(unsigned long long size
)
4297 size
-= 0xffff880000000000ULL
; // this const left high bits set in the token
4299 struct S
{ int i
: 1; } s
; // constructed for this '1'
4305 /* This just forces the above inline function to be actually emitted. */
4306 int force_get_order(unsigned long s
)
4308 return __get_order(s
);
4311 #define pv(m) printf(sizeof (s->m + 0) == 8 ? "%016llx\n" : "%02x\n", s->m)
4313 /* Test failed when using bounds checking */
4314 void bounds_check1_test (void)
4326 /* This failed on arm64/riscv64 */
4327 void map_add(int a
, int b
, int c
, int d
, int e
, int f
, int g
, int h
, int i
)
4329 printf ("%d %d %d %d %d %d %d %d %d\n", a
, b
, c
, d
, e
, f
, g
, h
, i
);
4332 void func_arg_test(void)
4336 map_add(0, 1, 2, 3, 4, 5, 6, 7, a
&& b
);
4339 /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
4340 #define CORRECT_CR_HANDLING
4342 /* deprecated and no longer supported in gcc 3.3 */
4343 /* no longer supported by default in TinyCC */
4345 /* # define ACCEPT_LF_IN_STRINGS */
4350 /* keep this as the last test because GCC messes up line-numbers
4351 with the ^L^K^M characters below */
4352 void whitespace_test(void)
4359 ntf("whitspace:\n");\f\v
4363 #ifdef CORRECT_CR_HANDLING
4372 #ifdef ACCEPT_LF_IN_STRINGS
4373 printf("len1=%d\n", strlen("
4375 #ifdef CORRECT_CR_HANDLING
4378 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
4380 printf("len1=%d\n", strlen("
a
4383 printf("len1=1\nlen1=1 str[0]=10\nlen1=3\n");
4384 #endif /* ACCEPT_LF_IN_STRINGS */
4387 printf("__LINE__ defined\n");
4391 /* wrong with GCC */
4392 printf("__LINE__=%d __FILE__=%s\n", __LINE__
, __FILE__
);
4394 printf("__LINE__=%d __FILE__=%s\n", __LINE__
, __FILE__
);
4396 printf("__LINE__=%d __FILE__=%s\n", __LINE__
, __FILE__
);
4412 #define RUN(test) puts("---- " #test " ----"), test(), puts("")
4414 int main(int argc, char **argv)
4416 RUN(whitespace_test);
4418 RUN(recursive_macro_test);
4435 RUN(optimize_out_test);
4437 RUN(constant_expr_test);
4439 RUN(char_short_test);
4441 RUN(compound_literal_test);
4443 RUN(struct_assign_test);
4451 RUN(relocation_test);
4452 RUN(old_style_function_test);
4457 RUN(statement_expr_test);
4458 RUN(local_label_test);
4462 RUN(global_data_test);
4463 RUN(cmp_comparison_test);
4466 RUN(builtin_frame_address_test);
4469 RUN(bounds_check1_test);