tccelf.c: write section headers before sections
[tinycc.git] / tests / tcctest.c
blobefc31e26ce2b85c32bec051d7d9ba99bdad31ad1
1 /*
2 * TCC auto test program
3 */
4 #include "config.h"
6 /* identify the configured reference compiler in use */
7 #define CC_gcc 1
8 #define CC_clang 2
9 #define CC_tcc 3
11 /* Unfortunately, gcc version < 3 does not handle that! */
12 #define ALL_ISOC99
14 /* only gcc 3 handles _Bool correctly */
15 #define BOOL_ISOC99
17 /* __VA_ARGS__ and __func__ support */
18 #define C99_MACROS
20 #ifndef __TINYC__
21 typedef __SIZE_TYPE__ uintptr_t;
22 #endif
24 #if defined(_WIN32) \
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"
33 #else
34 #define LONG_LONG_FORMAT "%Ld"
35 #define ULONG_LONG_FORMAT "%Lu"
36 #define XLONG_LONG_FORMAT "%Lx"
37 #endif
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
43 #else
44 #define LONG_DOUBLE long double
45 #define LONG_DOUBLE_LITERAL(x) x ## L
46 #endif
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"
55 #include TCCLIB_INC
57 #include TCCLIB_INC1.TCCLIB_INC2
59 #include TCCLIB_INC1.h>
61 #include TCCLIB_INC3
63 #include <tcclib.h>
65 #include "tcclib.h"
67 #include "tcctest.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
72 #define incdir tests/
73 #ifdef __clang__
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>
77 #else
78 #define incname < incdir funnyname >
79 #endif
80 #define __stringify(x) #x
81 #define stringify(x) __stringify(x)
82 #include INC(42test)
83 #include incname
84 #include stringify(funnyname)
86 int puts(const char *s);
87 void *alloca(size_t size);
89 int fib(int n);
90 void num(int n);
91 void forward_ref(void);
92 int isid(int c);
94 /* Line joining happens before tokenization, so the following
95 must be parsed as ellipsis. */
96 void funny_line_continuation (int, ..\
97 . );
99 #define A 2
100 #define N 1234 + A
101 #define pf printf
102 #define M1(a, b) (a) + (b)
104 #define str\
105 (s) # s
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))
117 #ifdef C99_MACROS
118 #define dprintf(level,...) printf(__VA_ARGS__)
119 #endif
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
129 #define AAA 3
130 #undef AAA
131 #define AAA 4
133 #if 1
134 #define B3 1
135 #elif 1
136 #define B3 2
137 #elif 0
138 #define B3 3
139 #else
140 #define B3 4
141 #endif
143 #define __INT64_C(c) c ## LL
144 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
146 int qq(int x)
148 return x + 40;
150 #define qq(x) x
152 #define spin_lock(lock) do { } while (0)
153 #define wq_spin_lock spin_lock
154 #define TEST2() wq_spin_lock(a)
156 void macro_test(void)
158 pf("N=%d\n", N);
159 printf("aaa=%d\n", AAA);
161 printf("min=%d\n", min(1, min(2, -1)));
163 printf("s1=%s\n", glue(HIGH, LOW));
164 printf("s2=%s\n", xglue(HIGH, LOW));
165 printf("s3=%s\n", str("c"));
166 printf("s4=%s\n", str(a1));
167 printf("B3=%d\n", B3);
169 printf("onetwothree=%d\n", onetwothree);
171 #ifdef A
172 printf("A defined\n");
173 #endif
174 #ifdef B
175 printf("B defined\n");
176 #endif
177 #ifdef A
178 printf("A defined\n");
179 #else
180 printf("A not defined\n");
181 #endif
182 #ifdef B
183 printf("B defined\n");
184 #else
185 printf("B not defined\n");
186 #endif
188 #ifdef A
189 printf("A defined\n");
190 #ifdef B
191 printf("B1 defined\n");
192 #else
193 printf("B1 not defined\n");
194 #endif
195 #else
196 printf("A not defined\n");
197 #ifdef B
198 printf("B2 defined\n");
199 #else
200 printf("B2 not defined\n");
201 #endif
202 #endif
204 #if 1+1
205 printf("test true1\n");
206 #endif
207 #if 0
208 printf("test true2\n");
209 #endif
210 #if 1-1
211 printf("test true3\n");
212 #endif
213 #if defined(A)
214 printf("test trueA\n");
215 #endif
216 #if defined(B)
217 printf("test trueB\n");
218 #endif
220 #if 0
221 printf("test 0\n");
222 #elif 0
223 printf("test 1\n");
224 #elif 2
225 printf("test 2\n");
226 #else
227 printf("test 3\n");
228 #endif
230 MACRO_NOARGS();
232 printf("%d\n", TEST_CALL(TEST_CONST));
234 /* not strictly preprocessor, but we test it there */
235 #ifdef C99_MACROS
236 printf("__func__ = %s\n", __func__);
237 dprintf(1, "vaarg=%d\n", 1);
238 #endif
239 dprintf1(1, "vaarg1\n");
240 dprintf1(1, "vaarg1=%d\n", 2);
241 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
243 /* gcc extension */
244 printf("func='%s'\n", __FUNCTION__);
246 /* complicated macros in glibc */
247 printf("INT64_MIN=" LONG_LONG_FORMAT "\n", INT64_MIN);
249 int a;
250 a = 1;
251 glue(a+, +);
252 printf("a=%d\n", a);
253 glue(a <, <= 2);
254 printf("a=%d\n", a);
257 /* macro function with argument outside the macro string */
258 #define MF_s MF_hello
259 #define MF_hello(msg) printf("%s\n",msg)
261 #define MF_t printf("tralala\n"); MF_hello
263 MF_s("hi");
264 MF_t("hi");
266 /* test macro substitution inside args (should not eat stream) */
267 printf("qq=%d\n", qq(qq)(2));
269 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
270 null argument without a space. gcc 3.2 fixes that. */
272 #define qq1(x) 1
273 printf("qq1=%d\n", qq1( ));
275 /* comment with stray handling *\
277 /* this is a valid *\/ comment */
278 /* this is a valid comment *\*/
279 // this is a valid\
280 comment
282 /* test function macro substitution when the function name is
283 substituted */
284 TEST2();
286 /* And again when the name and parentheses are separated by a
287 comment. */
288 TEST2 /* the comment */ ();
290 printf("basefromheader %s\n", get_basefile_from_header());
291 printf("base %s\n", __BASE_FILE__);
293 /* Some compilers (clang) prepend './' to __FILE__ from included
294 files. */
295 const char *fn = get_file_from_header();
296 if (fn[0] == '.' && fn[1] == '/')
297 fn += 2;
298 printf("filefromheader %s\n", fn);
300 printf("file %s\n", __FILE__);
302 /* Check that funnily named include was in fact included */
303 have_included_42test_h = 1;
304 have_included_42test_h_second = 1;
305 have_included_42test_h_third = 1;
307 /* Check that we don't complain about stray \ here */
308 printf("print a backslash: %s\n", stringify(\\));
312 static void print_num(char *fn, int line, int num) {
313 printf("fn %s, line %d, num %d\n", fn, line, num);
316 void recursive_macro_test(void)
319 #define ELF32_ST_TYPE(val) ((val) & 0xf)
320 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
321 #define STB_WEAK 2 /* Weak symbol */
322 #define ELFW(type) ELF##32##_##type
323 printf("%d\n", ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(123)));
325 #define WRAP(x) x
327 #define print_num(x) print_num(__FILE__,__LINE__,x)
328 print_num(123);
329 WRAP(print_num(123));
330 WRAP(WRAP(print_num(123)));
332 static struct recursive_macro { int rm_field; } G;
333 #define rm_field (G.rm_field)
334 printf("rm_field = %d\n", rm_field);
335 printf("rm_field = %d\n", WRAP(rm_field));
336 WRAP((printf("rm_field = %d %d\n", rm_field, WRAP(rm_field))));
339 #if __TINYC__
340 int op(a, b)
342 return a / b;
345 int ret(a)
347 if (a == 2)
348 return 1;
349 if (a == 3)
350 return 2;
351 return 0;
353 #endif
355 #if !defined(__TINYC__) && (__GNUC__ >= 8)
356 /* Old GCCs don't regard "foo"[1] as constant, even in GNU dialect. */
357 #define CONSTANTINDEXEDSTRLIT
358 #endif
359 char str_ag1[] = "b";
360 char str_ag2[] = { "b" };
361 /*char str_bg1[] = ("cccc"); GCC accepts this with pedantic warning, TCC not */
362 #ifdef CONSTANTINDEXEDSTRLIT
363 char str_ag3[] = { "ab"[1], 0 };
364 char str_x[2] = { "xy" "z"[2], 0 };
365 #endif
366 char *str_ar[] = { "one", "two" };
367 struct str_SS {unsigned char a[3], b; };
368 struct str_SS str_sinit15 = { "r" };
369 struct str_SS str_sinit16[] = { { "q" }, 2 };
371 static void string_test2()
373 char *p = "hello";
374 char a3[2] = { "p" };
375 char a4[2] = { "ab" "c"[2], 0 };
376 char *pa1 = "def" + 1;
377 char *pa2 = { "xyz" + 1 };
378 int i = 0;
379 struct str_SS ss = { { [0 ... 1] = 'a' }, 0 };
380 #ifndef CONSTANTINDEXEDSTRLIT
381 char str_ag3[] = { "ab"[1], 0 };
382 char str_x[2] = { "xy" "z"[2], 0 };
383 #endif
384 puts("string_test2");
385 puts(str_ag1);
386 puts(str_ag2);
387 /*puts(str_bg1);*/
388 puts(str_ag3);
389 puts(str_x);
390 puts(str_sinit15.a);
391 puts(str_sinit16[0].a);
392 puts(a3);
393 puts(a4);
394 puts(p);
395 puts("world");
396 printf("%s\n", "bla");
397 puts(str_ar[0]);
398 puts(str_ar[1]);
399 puts(ss.a);
400 puts(i >= 0 ? "one" : "two");
401 puts(pa1);
402 puts(pa2);
405 void ps(const char *s)
407 int c;
408 while (1) {
409 c = *s;
410 if (c == 0)
411 break;
412 printf("%c", c);
413 s++;
417 const char foo1_string[] = "\
418 bar\n\
419 test\14\
422 void string_test()
424 unsigned int b;
425 printf("string:\n");
426 printf("\141\1423\143\n");/* dezdez test */
427 printf("\x41\x42\x43\x3a\n");
428 printf("c=%c\n", 'r');
429 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
430 printf("foo1_string='%s'\n", foo1_string);
431 #if 0
432 printf("wstring=%S\n", L"abc");
433 printf("wstring=%S\n", L"abc" L"def" "ghi");
434 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
435 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
436 #endif
437 ps("test\n");
438 b = 32;
439 while ((b = b + 1) < 96) {
440 printf("%c", b);
442 printf("\n");
443 printf("fib=%d\n", fib(33));
444 b = 262144;
445 while (b != 0x80000000) {
446 num(b);
447 b = b * 2;
449 string_test2();
453 void if1t(int n, int a, int b, int c)
455 if (a && b) printf("if1t: %d 1 %d %d\n", n, a, b);
456 if (a && !b) printf("if1t: %d 2 %d %d\n", n, a, b);
457 if (!a && b) printf("if1t: %d 3 %d %d\n", n, a, b);
458 if (!a && !b) printf("if1t: %d 4 %d %d\n", n, a, b);
459 if (a || b) printf("if1t: %d 5 %d %d\n", n, a, b);
460 if (a || !b) printf("if1t: %d 6 %d %d\n", n, a, b);
461 if (!a || b) printf("if1t: %d 7 %d %d\n", n, a, b);
462 if (!a || !b) printf("if1t: %d 8 %d %d\n", n, a, b);
463 if (a && b || c) printf("if1t: %d 9 %d %d %d\n", n, a, b, c);
464 if (a || b && c) printf("if1t: %d 10 %d %d %d\n", n, a, b, c);
465 if (a > b - 1 && c) printf("if1t: %d 11 %d %d %d\n", n, a, b, c);
466 if (a > b - 1 || c) printf("if1t: %d 12 %d %d %d\n", n, a, b, c);
467 if (a > 0 && 1) printf("if1t: %d 13 %d %d %d\n", n, a, b, c);
468 if (a > 0 || 0) printf("if1t: %d 14 %d %d %d\n", n, a, b, c);
471 void if2t(void)
473 if (0 && 1 || printf("if2t:ok\n") || 1)
474 printf("if2t:ok2\n");
475 printf("if2t:ok3\n");
478 void if3t(void)
480 volatile long long i = 1;
481 if (i <= 18446744073709551615ULL)
483 else
484 printf ("if3t:wrong 1\n");
487 void if_test(void)
489 if1t(1, 0, 0, 0);
490 if1t(2, 0, 3, 0);
491 if1t(3, 2, 0, 0);
492 if1t(4, 2, 3, 0);
493 if2t();
494 if3t();
497 void loop_test()
499 int i;
500 i = 0;
501 while (i < 10)
502 printf("%d", i++);
503 printf("\n");
504 for(i = 0; i < 10;i++)
505 printf("%d", i);
506 printf("\n");
507 i = 0;
508 do {
509 printf("%d", i++);
510 } while (i < 10);
511 printf("\n");
513 char count = 123;
514 /* c99 for loop init test */
515 for (size_t count = 1; count < 3; count++)
516 printf("count=%d\n", count);
517 printf("count = %d\n", count);
519 /* break/continue tests */
520 i = 0;
521 while (1) {
522 if (i == 6)
523 break;
524 i++;
525 if (i == 3)
526 continue;
527 printf("%d", i);
529 printf("\n");
531 /* break/continue tests */
532 i = 0;
533 do {
534 if (i == 6)
535 break;
536 i++;
537 if (i == 3)
538 continue;
539 printf("%d", i);
540 } while(1);
541 printf("\n");
543 for(i = 0;i < 10;i++) {
544 if (i == 3)
545 continue;
546 printf("%d", i);
548 printf("\n");
551 typedef int typedef_and_label;
553 void goto_test()
555 int i;
556 static void *label_table[3] = { &&label1, &&label2, &&label3 };
557 struct {
558 int bla;
559 /* This needs to parse as typedef, not as label. */
560 typedef_and_label : 32;
561 } y = {1};
563 printf("\ngoto:\n");
564 i = 0;
565 /* This is a normal decl. */
566 typedef_and_label x;
567 /* This needs to parse as label, not as start of decl. */
568 typedef_and_label:
569 s_loop:
570 if (i >= 10)
571 goto s_end;
572 printf("%d", i);
573 i++;
574 goto s_loop;
575 s_end:
576 printf("\n");
578 /* we also test computed gotos (GCC extension) */
579 for(i=0;i<3;i++) {
580 goto *label_table[i];
581 label1:
582 printf("label1\n");
583 goto next;
584 label2:
585 printf("label2\n");
586 goto next;
587 label3:
588 printf("label3\n");
589 next: ;
593 enum {
595 E1 = 2,
596 E2 = 4,
601 enum test {
602 E5 = 1000,
605 struct S_enum {
606 enum {E6 = 42, E7, E8} e:8;
609 enum ELong {
610 /* This is either 0 on L32 machines, or a large number
611 on L64 machines. We should be able to store this. */
612 EL_large = ((unsigned long)0xf000 << 31) << 1,
615 enum { BIASU = -1U<<31 };
616 enum { BIASS = -1 << 31 };
618 static int getint(int i)
620 if (i)
621 return 0;
622 else
623 return (int)(-1U << 31);
626 void enum_test()
628 enum test b1;
629 /* The following should give no warning */
630 unsigned *p = &b1;
631 struct S_enum s = {E7};
632 printf("%d %d %d %d %d %d %d\n", s.e,
633 E0, E1, E2, E3, E4, E5);
634 b1 = 1;
635 printf("b1=%d\n", b1);
636 printf("enum large: %ld\n", EL_large);
638 if (getint(0) == BIASU)
639 printf("enum unsigned: ok\n");
640 else
641 printf("enum unsigned: wrong\n");
642 if (getint(0) == BIASS)
643 printf("enum unsigned: ok\n");
644 else
645 printf("enum unsigned: wrong\n");
648 typedef int *my_ptr;
650 typedef int mytype1;
651 typedef int mytype2;
653 void typedef_test()
655 my_ptr a;
656 mytype1 mytype2;
657 int b;
659 a = &b;
660 *a = 1234;
661 printf("a=%d\n", *a);
662 mytype2 = 2;
663 printf("mytype2=%d\n", mytype2);
666 void forward_test()
668 forward_ref();
669 forward_ref();
673 void forward_ref(void)
675 printf("forward ok\n");
678 typedef struct struct1 {
679 int f1;
680 int f2, f3;
681 union union1 {
682 int v1;
683 int v2;
684 } u;
685 char str[3];
686 } struct1;
688 struct struct2 {
689 int a;
690 char b;
693 union union2 {
694 int w1;
695 int w2;
698 struct struct1 st1, st2;
700 struct empty_mem {
701 /* nothing */ ;
702 int x;
705 int tab[3];
706 int tab2[3][2];
708 int g;
710 void f1(int g)
712 printf("g1=%d\n", g);
715 void scope_test()
717 g = 2;
718 f1(1);
719 printf("g2=%d\n", g);
721 int g;
722 g = 3;
723 printf("g3=%d\n", g);
725 int g;
726 g = 4;
727 printf("g4=%d\n", g);
730 printf("g5=%d\n", g);
733 int st2_i;
734 int *st2_p = &st2_i;
735 void scope2_test()
737 char a[50];
738 st2_i = 42;
739 for (int st2_i = 1; st2_i < 10; st2_i++) {
740 extern int st2_i;
741 st2_i++;
742 printf("exloc: %d\n", st2_i);
744 printf("exloc: %d\n", *st2_p);
747 /* C has tentative definition, and they may be repeated. */
748 extern int st_global1;
749 int st_global1=42;
750 extern int st_global1;
751 int st_global1;
752 extern int st_global2;
753 int st_global2;
754 extern int st_global2;
755 int st_global2;
757 void array_test()
759 int i, j, a[4];
761 printf("sizeof(a) = %d\n", sizeof(a));
762 printf("sizeof(\"a\") = %d\n", sizeof("a"));
763 #ifdef C99_MACROS
764 printf("sizeof(__func__) = %d\n", sizeof(__func__));
765 #endif
766 printf("sizeof tab %d\n", sizeof(tab));
767 printf("sizeof tab2 %d\n", sizeof tab2);
768 tab[0] = 1;
769 tab[1] = 2;
770 tab[2] = 3;
771 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
772 for(i=0;i<3;i++)
773 for(j=0;j<2;j++)
774 tab2[i][j] = 10 * i + j;
775 for(i=0;i<3*2;i++) {
776 printf(" %3d", ((int *)tab2)[i]);
778 printf("\n");
779 printf("sizeof(size_t)=%d\n", sizeof(size_t));
780 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
783 void expr_test()
785 int a, b;
786 a = 0;
787 printf("%d\n", a += 1);
788 printf("%d\n", a -= 2);
789 printf("%d\n", a *= 31232132);
790 printf("%d\n", a /= 4);
791 printf("%d\n", a %= 20);
792 printf("%d\n", a &= 6);
793 printf("%d\n", a ^= 7);
794 printf("%d\n", a |= 8);
795 printf("%d\n", a >>= 3);
796 printf("%d\n", a <<= 4);
798 a = 22321;
799 b = -22321;
800 printf("%d\n", a + 1);
801 printf("%d\n", a - 2);
802 printf("%d\n", a * 312);
803 printf("%d\n", a / 4);
804 printf("%d\n", b / 4);
805 printf("%d\n", (unsigned)b / 4);
806 printf("%d\n", a % 20);
807 printf("%d\n", b % 20);
808 printf("%d\n", (unsigned)b % 20);
809 printf("%d\n", a & 6);
810 printf("%d\n", a ^ 7);
811 printf("%d\n", a | 8);
812 printf("%d\n", a >> 3);
813 printf("%d\n", b >> 3);
814 printf("%d\n", (unsigned)b >> 3);
815 printf("%d\n", a << 4);
816 printf("%d\n", ~a);
817 printf("%d\n", -a);
818 printf("%d\n", +a);
820 printf("%d\n", 12 + 1);
821 printf("%d\n", 12 - 2);
822 printf("%d\n", 12 * 312);
823 printf("%d\n", 12 / 4);
824 printf("%d\n", 12 % 20);
825 printf("%d\n", 12 & 6);
826 printf("%d\n", 12 ^ 7);
827 printf("%d\n", 12 | 8);
828 printf("%d\n", 12 >> 2);
829 printf("%d\n", 12 << 4);
830 printf("%d\n", ~12);
831 printf("%d\n", -12);
832 printf("%d\n", +12);
833 printf("%d %d %d %d\n",
834 isid('a'),
835 isid('g'),
836 isid('T'),
837 isid('('));
840 int isid(int c)
842 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
845 /**********************/
847 int vstack[10], *vstack_ptr;
849 void vpush(int vt, int vc)
851 *vstack_ptr++ = vt;
852 *vstack_ptr++ = vc;
855 void vpop(int *ft, int *fc)
857 *fc = *--vstack_ptr;
858 *ft = *--vstack_ptr;
861 void expr2_test()
863 int a, b;
865 vstack_ptr = vstack;
866 vpush(1432432, 2);
867 vstack_ptr[-2] &= ~0xffffff80;
868 vpop(&a, &b);
869 printf("res= %d %d\n", a, b);
872 int const_len_ar[sizeof(1/0)]; /* div-by-zero, but in unevaluated context */
874 void constant_expr_test()
876 int a;
877 a = 3;
878 printf("%d\n", a * 16);
879 printf("%d\n", a * 1);
880 printf("%d\n", a + 0);
881 printf("%d\n", sizeof(const_len_ar));
884 int tab4[10];
886 void expr_ptr_test()
888 int arr[10], *p, *q;
889 int i = -1;
891 p = tab4;
892 q = tab4 + 10;
893 printf("diff=%d\n", q - p);
894 p++;
895 printf("inc=%d\n", p - tab4);
896 p--;
897 printf("dec=%d\n", p - tab4);
898 ++p;
899 printf("inc=%d\n", p - tab4);
900 --p;
901 printf("dec=%d\n", p - tab4);
902 printf("add=%d\n", p + 3 - tab4);
903 printf("add=%d\n", 3 + p - tab4);
905 /* check if 64bit support is ok */
906 q = p = 0;
907 q += i;
908 printf("%p %p %ld\n", q, p, p-q);
909 printf("%d %d %d %d %d %d\n",
910 p == q, p != q, p < q, p <= q, p >= q, p > q);
911 i = 0xf0000000;
912 p += i;
913 printf("%p %p %ld\n", q, p, p-q);
914 printf("%d %d %d %d %d %d\n",
915 p == q, p != q, p < q, p <= q, p >= q, p > q);
916 p = (int *)((char *)p + 0xf0000000);
917 printf("%p %p %ld\n", q, p, p-q);
918 printf("%d %d %d %d %d %d\n",
919 p == q, p != q, p < q, p <= q, p >= q, p > q);
920 p += 0xf0000000;
921 printf("%p %p %ld\n", q, p, p-q);
922 printf("%d %d %d %d %d %d\n",
923 p == q, p != q, p < q, p <= q, p >= q, p > q);
925 struct size12 {
926 int i, j, k;
928 struct size12 s[2], *sp = s;
929 int i, j;
930 sp->i = 42;
931 sp++;
932 j = -1;
933 printf("%d\n", sp[j].i);
935 #ifdef __LP64__
936 i = 1;
937 p = (int*)0x100000000UL + i;
938 i = ((long)p) >> 32;
939 printf("largeptr: %p %d\n", p, i);
940 #endif
941 p = &arr[0];
942 q = p + 3;
943 printf ("%d\n", (int)((p - q) / 3));
946 void expr_cmp_test()
948 int a, b;
949 a = -1;
950 b = 1;
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);
969 struct empty {
972 struct aligntest1 {
973 char a[10];
976 struct aligntest2 {
977 int a;
978 char b[10];
981 struct aligntest3 {
982 double a, b;
985 struct aligntest4 {
986 double a[0];
989 struct __attribute__((aligned(16))) aligntest5
991 int i;
993 struct aligntest6
995 int i;
996 } __attribute__((aligned(16)));
997 struct aligntest7
999 int i;
1001 struct aligntest5 altest5[2];
1002 struct aligntest6 altest6[2];
1003 int pad1;
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)));
1011 struct aligntest8
1013 int i;
1014 } __attribute__((aligned(4096)));
1016 struct Large {
1017 unsigned long flags;
1018 union {
1019 void *u1;
1020 int *u2;
1023 struct {
1024 union {
1025 unsigned long index;
1026 void *freelist;
1028 union {
1029 unsigned long counters;
1030 struct {
1031 int bla;
1036 union {
1037 struct {
1038 long u3;
1039 long u4;
1041 void *u5;
1042 struct {
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;
1052 struct aligntest9 {
1053 unsigned int buf_nr;
1054 unaligned_u64 start_lba;
1057 struct aligntest10 {
1058 unsigned int buf_nr;
1059 unsigned long long start_lba;
1062 void struct_test()
1064 struct1 *s;
1065 union union2 u;
1066 struct Large ls;
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));
1074 st1.f1 = 1;
1075 st1.f2 = 2;
1076 st1.f3 = 3;
1077 printf("st1: %d %d %d\n",
1078 st1.f1, st1.f2, st1.f3);
1079 st1.u.v1 = 1;
1080 st1.u.v2 = 2;
1081 printf("union1: %d\n", st1.u.v1);
1082 u.w1 = 1;
1083 u.w2 = 2;
1084 printf("union2: %d\n", u.w1);
1085 s = &st2;
1086 s->f1 = 3;
1087 s->f2 = 2;
1088 s->f3 = 1;
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()
1139 int var1, var2;
1140 signed char var3;
1141 long long var4;
1143 var1 = 0x01020304;
1144 var2 = 0xfffefdfc;
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);
1164 var1 = 0x778899aa;
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);
1169 var1 = 0x778899aa;
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));
1184 #endif
1185 var3 = -10;
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 {
1200 int v;
1201 int t;
1202 int c;
1203 struct Sym *next;
1204 struct Sym *prev;
1205 } 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)
1212 return TOUPPER(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);
1220 return prot_bits;
1223 enum cast_enum { FIRST, LAST };
1225 static void tst_cast(enum cast_enum ce)
1227 printf("%d\n", ce);
1230 void bool_test()
1232 int *s, a, b, t, f, i;
1234 a = 0;
1235 s = (void*)0;
1236 printf("!s=%d\n", !s);
1238 if (!s || !s[0])
1239 a = 1;
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);
1245 #if 1 && 1
1246 printf("a1\n");
1247 #endif
1248 #if 1 || 0
1249 printf("a2\n");
1250 #endif
1251 #if 1 ? 0 : 1
1252 printf("a3\n");
1253 #endif
1254 #if 0 ? 0 : 1
1255 printf("a4\n");
1256 #endif
1258 a = 4;
1259 printf("b=%d\n", a + (0 ? 1 : a / 2));
1261 /* test register spilling */
1262 a = 10;
1263 b = 10;
1264 a = (a + b) * ((a < b) ?
1265 ((b - a) * (a - b)): a + b);
1266 printf("a=%d\n", a);
1268 /* test complex || or && expressions */
1269 t = 1;
1270 f = 0;
1271 a = 32;
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) */
1276 t = 1;
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 );
1281 /* test ? : cast */
1283 int aspect_on;
1284 int aspect_native = 65536;
1285 double bfu_aspect = 1.0;
1286 int aspect;
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 */
1297 int a = 30;
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);
1314 #ifdef __clang__
1315 int undefined_function(void) {}
1316 #endif
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);
1329 if (0)
1330 printf("oo:%d\n", undefined_function());
1331 else
1332 printf("oo:%d\n", defined_function());
1333 if (1)
1334 printf("oo:%d\n", defined_function());
1335 else
1336 printf("oo:%d\n", undefined_function());
1337 while (1) {
1338 printf("oow:%d\n", defined_function());
1339 break;
1340 printf("oow:%d\n", undefined_function());
1342 j = 1;
1343 /* Following is a switch without {} block intentionally. */
1344 switch (j)
1345 case 1: break;
1346 printf ("oos:%d\n", defined_function());
1347 /* The following break shouldn't lead to disabled code after
1348 the while. */
1349 while (1)
1350 break;
1351 printf ("ool1:%d\n", defined_function());
1352 /* Same for the other types of loops. */
1354 break;
1355 while (1);
1356 printf ("ool2:%d\n", defined_function());
1357 for (;;)
1358 break;
1359 printf ("ool3:%d\n", defined_function());
1360 /* Normal {} blocks without controlling statements
1361 shouldn't reactivate code emission */
1362 while (1) {
1364 break;
1366 printf ("ool4:%d\n", undefined_function());
1368 j = 1;
1369 while (j) {
1370 if (j == 0)
1371 break; /* this break shouldn't disable code outside the if. */
1372 printf("ool5:%d\n", defined_function());
1373 j--;
1376 j = 1;
1377 while (j) {
1378 if (1)
1379 j--;
1380 else
1381 breakhere: break;
1382 printf("ool6:%d\n", defined_function());
1383 goto breakhere;
1385 j = 1;
1386 while (j) {
1387 j--;
1388 continue;
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();
1399 if (0 && 0)
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())
1408 else
1409 undefined_function();
1410 if (defined_function() || 1)
1412 else
1413 undefined_function();
1414 if (1 || 1)
1416 else
1417 undefined_function();
1418 if (defined_function() || 1 || undefined_function())
1420 else
1421 undefined_function();
1423 if (defined_function() && 0)
1424 refer_to_undefined();
1426 if (0) {
1427 (void)sizeof( ({
1428 do { } while (0);
1430 }) );
1431 undefined_function();
1434 if (0) {
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 */
1442 if (1)
1443 return;
1444 printf ("oor:%d\n", undefined_function());
1447 int defined_function(void)
1449 static int i = 40;
1450 return i++;
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 ! */
1461 int cinit1;
1462 int cinit1;
1463 int cinit1 = 0;
1464 int *cinit2 = (int []){3, 2, 1};
1466 void compound_literal_test(void)
1468 int *p, i;
1469 char *q, *q3;
1471 p = (int []){1, 2, 3};
1472 for(i=0;i<3;i++)
1473 printf(" %d", p[i]);
1474 printf("\n");
1476 for(i=0;i<3;i++)
1477 printf("%d", cinit2[i]);
1478 printf("\n");
1480 q = "tralala1";
1481 printf("q1=%s\n", q);
1483 q = (char *){ "tralala2" };
1484 printf("q2=%s\n", q);
1486 q3 = (char *){ q };
1487 printf("q3=%s\n", q3);
1489 q = (char []){ "tralala3" };
1490 printf("q4=%s\n", q);
1492 #ifdef ALL_ISOC99
1493 p = (int []){1, 2, cinit1 + 3};
1494 for(i=0;i<3;i++)
1495 printf(" %d", p[i]);
1496 printf("\n");
1498 for(i=0;i<3;i++) {
1499 p = (int []){1, 2, 4 + i};
1500 printf("%d %d %d\n",
1501 p[0],
1502 p[1],
1503 p[2]);
1505 #endif
1509 #if __TINYC__
1511 /* K & R protos */
1513 kr_func1(a, b)
1515 return a + b;
1518 int kr_func2(a, b)
1520 return a + b;
1523 kr_test()
1525 printf("func1=%d\n", kr_func1(3, 4));
1526 printf("func2=%d\n", kr_func2(3, 4));
1527 return 0;
1530 /* We try to handle this syntax. Make at least sure it doesn't segfault. */
1531 char invalid_function_def()[] {return 0;}
1533 #else
1534 # define kr_test() printf("func1=7\nfunc2=7\n")
1535 #endif
1537 void num(int n)
1539 char *tab, *p;
1540 tab = (char*)malloc(20);
1541 p = tab;
1542 while (1) {
1543 *p = 48 + (n % 10);
1544 p++;
1545 n = n / 10;
1546 if (n == 0)
1547 break;
1549 while (p != tab) {
1550 p--;
1551 printf("%c", *p);
1553 printf("\n");
1554 free(tab);
1557 /* structure assignment tests */
1558 struct structa1 {
1559 int f1;
1560 char f2;
1563 void struct_assign_test1(struct structa1 s1, int t, float f)
1565 printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
1568 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1570 s1.f1 += t;
1571 s1.f2 -= t;
1572 return s1;
1575 void struct_assign_test(void)
1577 struct S {
1578 struct structa1 lsta1, lsta2;
1579 int i;
1580 } s = {{1,2}, {3,4}}, *ps;
1582 ps = &s;
1583 ps->i = 4;
1585 struct_assign_test1(ps->lsta2, 3, 4.5);
1586 printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
1587 ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
1588 printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
1590 static struct {
1591 void (*elem)();
1592 } t[] = {
1593 /* XXX: we should allow this even without braces */
1594 { struct_assign_test }
1596 printf("%d\n", struct_assign_test == t[0].elem);
1598 s.lsta1 = s.lsta2 = struct_assign_test2(s.lsta1, 1);
1599 printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
1602 /* casts to short/char */
1604 void cast1(char a, short b, unsigned char c, unsigned short d)
1606 printf("%d %d %d %d\n", a, b, c, d);
1609 char bcast;
1610 short scast;
1612 void cast_test()
1614 int a;
1615 char c;
1616 char tab[10];
1617 unsigned b,d;
1618 short s;
1619 char *p = NULL;
1620 unsigned long ul = 0x80000000UL;
1621 p -= 0x700000000042;
1623 a = 0xfffff;
1624 cast1(a, a, a, a);
1625 a = 0xffffe;
1626 printf("%d %d %d %d\n",
1627 (char)(a + 1),
1628 (short)(a + 1),
1629 (unsigned char)(a + 1),
1630 (unsigned short)(a + 1));
1631 printf("%d %d %d %d\n",
1632 (char)0xfffff,
1633 (short)0xfffff,
1634 (unsigned char)0xfffff,
1635 (unsigned short)0xfffff);
1637 a = (bcast = 128) + 1;
1638 printf("%d\n", a);
1639 a = (scast = 65536) + 1;
1640 printf("%d\n", a);
1642 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1644 /* test cast from unsigned to signed short to int */
1645 b = 0xf000;
1646 d = (short)b;
1647 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1648 b = 0xf0f0;
1649 d = (char)b;
1650 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1652 /* test implicit int casting for array accesses */
1653 c = 0;
1654 tab[1] = 2;
1655 tab[c] = 1;
1656 printf("%d %d\n", tab[0], tab[1]);
1658 /* test implicit casting on some operators */
1659 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1660 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1661 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1663 #if CC_NAME != CC_clang /* clang doesn't support non-portable conversions */
1664 /* from pointer to integer types */
1665 printf("%d %d %ld %ld %lld %lld\n",
1666 (int)p, (unsigned int)p,
1667 (long)p, (unsigned long)p,
1668 (long long)p, (unsigned long long)p);
1669 #endif
1671 /* from integers to pointers */
1672 printf("%p %p %p %p\n",
1673 (void *)a, (void *)b, (void *)c, (void *)d);
1675 /* int to int with sign set */
1676 printf("0x%lx\n", (unsigned long)(int)ul);
1679 /* initializers tests */
1680 struct structinit1 {
1681 int f1;
1682 char f2;
1683 short f3;
1684 int farray[3];
1687 int sinit1 = 2;
1688 int sinit2 = { 3 };
1689 int sinit3[3] = { 1, 2, {{3}}, };
1690 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1691 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1692 int sinit6[] = { 1, 2, 3 };
1693 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1694 char sinit8[] = "hello" "trala";
1696 struct structinit1 sinit9 = { 1, 2, 3 };
1697 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1698 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1699 #ifdef ALL_ISOC99
1700 .farray[0] = 10,
1701 .farray[1] = 11,
1702 .farray[2] = 12,
1703 #endif
1706 char *sinit12 = "hello world";
1707 char *sinit13[] = {
1708 "test1",
1709 "test2",
1710 "test3",
1712 char sinit14[10] = { "abc" };
1713 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1715 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1717 struct bar {
1718 char *s;
1719 int len;
1720 } sinit17[] = {
1721 "a1", 4,
1722 "a2", 1
1725 int sinit18[10] = {
1726 [2 ... 5] = 20,
1728 [8] = 10,
1731 struct complexinit0 {
1732 int a;
1733 int b;
1736 struct complexinit {
1737 int a;
1738 const struct complexinit0 *b;
1741 const static struct complexinit cix[] = {
1742 [0] = {
1743 .a = 2000,
1744 .b = (const struct complexinit0[]) {
1745 { 2001, 2002 },
1746 { 2003, 2003 },
1752 struct complexinit2 {
1753 int a;
1754 int b[];
1757 struct complexinit2 cix20;
1759 struct complexinit2 cix21 = {
1760 .a = 3000,
1761 .b = { 3001, 3002, 3003 }
1764 struct complexinit2 cix22 = {
1765 .a = 4000,
1766 .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1769 typedef int arrtype1[];
1770 arrtype1 sinit19 = {1};
1771 arrtype1 sinit20 = {2,3};
1772 typedef int arrtype2[3];
1773 arrtype2 sinit21 = {4};
1774 arrtype2 sinit22 = {5,6,7};
1776 /* Address comparisons of non-weak symbols with zero can be const-folded */
1777 int sinit23[2] = { "astring" ? sizeof("astring") : -1,
1778 &sinit23 ? 42 : -1 };
1780 int sinit24 = 2 || 1 / 0; /* exception in constant but unevaluated context */
1782 /* bitfield init */
1783 struct bf_SS {unsigned int bit:1,bits31:31; };
1784 struct bf_SS bf_init = { .bit = 1 };
1785 struct bfn_SS {int a,b; struct bf_SS c; int d,e; };
1786 struct bfn_SS bfn_init = { .c.bit = 1 };
1787 struct bfa_SS {int a,b; struct bf_SS c[3]; int d,e; };
1788 struct bfa_SS bfa_init = { .c[1].bit = 1 };
1789 struct bf_SS bfaa_init[3] = { [1].bit = 1 };
1790 struct bf_SS bfaa_vinit[] = { [2].bit = 1 };
1791 struct b2_SS {long long int field : 52; long long int pad : 12; };
1792 struct b2_SS bf_init2 = {0xFFF000FFF000FLL, 0x123};
1794 extern int external_inited = 42;
1796 void init_test(void)
1798 int linit1 = 2;
1799 int linit2 = { 3 };
1800 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1801 int linit6[] = { 1, 2, 3 };
1802 int i, j;
1803 char linit8[] = "hello" "trala";
1804 int linit12[10] = { 1, 2 };
1805 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1806 char linit14[10] = "abc";
1807 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1808 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1809 int linit17 = sizeof(linit17);
1810 int zero = 0;
1811 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1812 int linit18[2] = {&zero ? 1 : -1, zero ? -1 : 1 };
1813 struct bf_SS bf_finit = { .bit = 1 };
1814 struct bfn_SS bfn_finit = { .c.bit = 1 };
1815 struct bfa_SS bfa_finit = { .c[1].bit = 1 };
1816 struct bf_SS bfaa_finit[3] = { [1].bit = 1 };
1817 struct bf_SS bfaa_fvinit[] = { [2].bit = 1 };
1818 struct b2_SS bf_finit2 = {0xFFF000FFF000FLL, 0x123};
1820 printf("sinit1=%d\n", sinit1);
1821 printf("sinit2=%d\n", sinit2);
1822 printf("sinit3=%d %d %d %d\n",
1823 sizeof(sinit3),
1824 sinit3[0],
1825 sinit3[1],
1826 sinit3[2]
1828 printf("sinit6=%d\n", sizeof(sinit6));
1829 printf("sinit7=%d %d %d %d\n",
1830 sizeof(sinit7),
1831 sinit7[0],
1832 sinit7[1],
1833 sinit7[2]
1835 printf("sinit8=%s\n", sinit8);
1836 printf("sinit9=%d %d %d\n",
1837 sinit9.f1,
1838 sinit9.f2,
1839 sinit9.f3
1841 printf("sinit10=%d %d %d\n",
1842 sinit10.f1,
1843 sinit10.f2,
1844 sinit10.f3
1846 printf("sinit11=%d %d %d %d %d %d\n",
1847 sinit11.f1,
1848 sinit11.f2,
1849 sinit11.f3,
1850 sinit11.farray[0],
1851 sinit11.farray[1],
1852 sinit11.farray[2]
1855 for(i=0;i<3;i++)
1856 for(j=0;j<2;j++)
1857 printf("[%d][%d] = %d %d %d\n",
1858 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1859 printf("linit1=%d\n", linit1);
1860 printf("linit2=%d\n", linit2);
1861 printf("linit6=%d\n", sizeof(linit6));
1862 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1864 printf("sinit12=%s\n", sinit12);
1865 printf("sinit13=%d %s %s %s\n",
1866 sizeof(sinit13),
1867 sinit13[0],
1868 sinit13[1],
1869 sinit13[2]);
1870 printf("sinit14=%s\n", sinit14);
1872 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1873 printf("\n");
1874 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1875 printf("\n");
1876 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1877 printf("\n");
1878 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1879 printf("\n");
1880 printf("%d %d %d %d\n",
1881 linit16.a1,
1882 linit16.a2,
1883 linit16.a3,
1884 linit16.a4);
1885 /* test that initialisation is done after variable declare */
1886 printf("linit17=%d\n", linit17);
1887 printf("sinit15=%d\n", sinit15[0]);
1888 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1889 printf("sinit17=%s %d %s %d\n",
1890 sinit17[0].s, sinit17[0].len,
1891 sinit17[1].s, sinit17[1].len);
1892 for(i=0;i<10;i++)
1893 printf("%x ", sinit18[i]);
1894 printf("\n");
1895 /* complex init check */
1896 printf("cix: %d %d %d %d %d %d %d\n",
1897 cix[0].a,
1898 cix[0].b[0].a, cix[0].b[0].b,
1899 cix[0].b[1].a, cix[0].b[1].b,
1900 cix[0].b[2].a, cix[0].b[2].b);
1901 printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
1902 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
1904 printf("arrtype1: %d %d %d\n", sinit19[0], sinit20[0], sinit20[1]);
1905 printf("arrtype2: %d %d\n", sizeof(sinit19), sizeof(sinit20));
1906 printf("arrtype3: %d %d %d\n", sinit21[0], sinit21[1], sinit21[2]);
1907 printf("arrtype4: %d %d %d\n", sinit22[0], sinit22[1], sinit22[2]);
1908 printf("arrtype5: %d %d\n", sizeof(sinit21), sizeof(sinit22));
1909 printf("arrtype6: %d\n", sizeof(arrtype2));
1911 printf("sinit23= %d %d\n", sinit23[0], sinit23[1]);
1912 printf("sinit24=%d\n", sinit24);
1913 printf("linit18= %d %d\n", linit18[0], linit18[1]);
1914 printf ("bf1: %u %u\n", bf_init.bit, bf_init.bits31);
1915 printf ("bf2: %u %u\n", bf_finit.bit, bf_finit.bits31);
1916 printf ("bf3: %u %u\n", bfn_init.c.bit, bfn_init.c.bits31);
1917 printf ("bf4: %u %u\n", bfn_finit.c.bit, bfn_finit.c.bits31);
1918 for (i = 0; i < 3; i++)
1919 printf ("bf5[%d]: %u %u\n", i, bfa_init.c[i].bit, bfa_init.c[i].bits31);
1920 for (i = 0; i < 3; i++)
1921 printf ("bf6[%d]: %u %u\n", i, bfa_finit.c[i].bit, bfa_finit.c[i].bits31);
1922 for (i = 0; i < 3; i++)
1923 printf ("bf7[%d]: %u %u\n", i, bfaa_init[i].bit, bfaa_init[i].bits31);
1924 for (i = 0; i < 3; i++)
1925 printf ("bf8[%d]: %u %u\n", i, bfaa_finit[i].bit, bfaa_finit[i].bits31);
1926 for (i = 0; i < 3; i++)
1927 printf ("bf9[%d]: %u %u\n", i, bfaa_vinit[i].bit, bfaa_vinit[i].bits31);
1928 for (i = 0; i < 3; i++)
1929 printf ("bf10[%d]: %u %u\n", i, bfaa_fvinit[i].bit, bfaa_fvinit[i].bits31);
1932 void switch_uc(unsigned char uc)
1934 switch (uc) {
1935 case 0xfb ... 0xfe:
1936 printf("ucsw:1\n");
1937 break;
1938 case 0xff:
1939 printf("ucsw:2\n");
1940 break;
1941 case 0 ... 5:
1942 printf("ucsw:3\n");
1943 break;
1944 default:
1945 printf("ucsw: broken!\n");
1949 void switch_sc(signed char sc)
1951 switch (sc) {
1952 case -5 ... -2:
1953 printf("scsw:1\n");
1954 break;
1955 case -1:
1956 printf("scsw:2\n");
1957 break;
1958 case 0 ... 5:
1959 printf("scsw:3\n");
1960 break;
1961 default:
1962 printf("scsw: broken!\n");
1966 void switch_test()
1968 int i;
1969 unsigned long long ull;
1970 long long ll;
1972 for(i=0;i<15;i++) {
1973 switch(i) {
1974 case 0:
1975 case 1:
1976 printf("a");
1977 break;
1978 default:
1979 printf("%d", i);
1980 break;
1981 case 8 ... 12:
1982 printf("c");
1983 break;
1984 case 3:
1985 printf("b");
1986 break;
1987 case 0xc33c6b9fU:
1988 case 0x7c9eeeb9U:
1989 break;
1992 printf("\n");
1994 for (i = 1; i <= 5; i++) {
1995 ull = (unsigned long long)i << 61;
1996 switch (ull) {
1997 case 1ULL << 61:
1998 printf("ullsw:1\n");
1999 break;
2000 case 2ULL << 61:
2001 printf("ullsw:2\n");
2002 break;
2003 case 3ULL << 61:
2004 printf("ullsw:3\n");
2005 break;
2006 case 4ULL << 61:
2007 printf("ullsw:4\n");
2008 break;
2009 case 5ULL << 61:
2010 printf("ullsw:5\n");
2011 break;
2012 default:
2013 printf("ullsw: broken!\n");
2017 for (i = 1; i <= 5; i++) {
2018 ll = (long long)i << 61;
2019 switch (ll) {
2020 case 1LL << 61:
2021 printf("llsw:1\n");
2022 break;
2023 case 2LL << 61:
2024 printf("llsw:2\n");
2025 break;
2026 case 3LL << 61:
2027 printf("llsw:3\n");
2028 break;
2029 case 4LL << 61:
2030 printf("llsw:4\n");
2031 break;
2032 case 5LL << 61:
2033 printf("llsw:5\n");
2034 break;
2035 default:
2036 printf("llsw: broken!\n");
2040 for (i = -5; i <= 5; i++) {
2041 switch_uc((unsigned char)i);
2044 for (i = -5; i <= 5; i++) {
2045 switch_sc ((signed char)i);
2049 /* ISOC99 _Bool type */
2050 void c99_bool_test(void)
2052 #ifdef BOOL_ISOC99
2053 int a;
2054 _Bool b, b2;
2056 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
2057 a = 3;
2058 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
2059 b = 3;
2060 printf("b = %d\n", b);
2061 b++;
2062 printf("b = %d\n", b);
2063 b2 = 0;
2064 printf("sizeof(x ? _Bool : _Bool) = %d (should be sizeof int)\n",
2065 sizeof ((volatile int)a ? b : b2));
2066 #endif
2069 void bitfield_test(void)
2071 int a;
2072 short sa;
2073 unsigned char ca;
2074 struct sbf1 {
2075 int f1 : 3;
2076 int : 2;
2077 int f2 : 1;
2078 int : 0;
2079 int f3 : 5;
2080 int f4 : 7;
2081 unsigned int f5 : 7;
2082 } st1;
2083 printf("sizeof(st1) = %d\n", sizeof(st1));
2085 st1.f1 = 3;
2086 st1.f2 = 1;
2087 st1.f3 = 15;
2088 a = 120;
2089 st1.f4 = a;
2090 st1.f5 = a;
2091 st1.f5++;
2092 printf("%d %d %d %d %d\n",
2093 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
2094 sa = st1.f5;
2095 ca = st1.f5;
2096 printf("%d %d\n", sa, ca);
2098 st1.f1 = 7;
2099 if (st1.f1 == -1)
2100 printf("st1.f1 == -1\n");
2101 else
2102 printf("st1.f1 != -1\n");
2103 if (st1.f2 == -1)
2104 printf("st1.f2 == -1\n");
2105 else
2106 printf("st1.f2 != -1\n");
2108 struct sbf2 {
2109 long long f1 : 45;
2110 long long : 2;
2111 long long f2 : 35;
2112 unsigned long long f3 : 38;
2113 } st2;
2114 st2.f1 = 0x123456789ULL;
2115 a = 120;
2116 st2.f2 = (long long)a << 25;
2117 st2.f3 = a;
2118 st2.f2++;
2119 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
2121 #if 0
2122 Disabled for now until further clarification re GCC compatibility
2123 struct sbf3 {
2124 int f1 : 7;
2125 int f2 : 1;
2126 char f3;
2127 int f4 : 8;
2128 int f5 : 1;
2129 int f6 : 16;
2130 } st3;
2131 printf("sizeof(st3) = %d\n", sizeof(st3));
2132 #endif
2134 struct sbf4 {
2135 int x : 31;
2136 char y : 2;
2137 } st4;
2138 st4.y = 1;
2139 printf("st4.y == %d\n", st4.y);
2140 struct sbf5 {
2141 int a;
2142 char b;
2143 int x : 12, y : 4, : 0, : 4, z : 3;
2144 char c;
2145 } st5 = { 1, 2, 3, 4, -3, 6 };
2146 printf("st5 = %d %d %d %d %d %d\n", st5.a, st5.b, st5.x, st5.y, st5.z, st5.c);
2147 struct sbf6 {
2148 short x : 12;
2149 unsigned char y : 2;
2150 } st6;
2151 st6.y = 1;
2152 printf("st6.y == %d\n", st6.y);
2155 #ifdef __x86_64__
2156 #define FLOAT_FMT "%f\n"
2157 #else
2158 /* x86's float isn't compatible with GCC */
2159 #define FLOAT_FMT "%.5f\n"
2160 #endif
2162 /* declare strto* functions as they are C99 */
2163 double strtod(const char *nptr, char **endptr);
2165 #if defined(_WIN32)
2166 float strtof(const char *nptr, char **endptr) {return (float)strtod(nptr, endptr);}
2167 LONG_DOUBLE strtold(const char *nptr, char **endptr) {return (LONG_DOUBLE)strtod(nptr, endptr);}
2168 #else
2169 float strtof(const char *nptr, char **endptr);
2170 LONG_DOUBLE strtold(const char *nptr, char **endptr);
2171 #endif
2173 #if CC_NAME == CC_clang
2174 /* In clang 0.0/0.0 is nan and not -nan.
2175 Also some older clang version do v=-v
2176 as v = -0 - v */
2177 static char enable_nan_test = 0;
2178 #else
2179 static char enable_nan_test = 1;
2180 #endif
2182 #define FTEST(prefix, typename, type, fmt)\
2183 void prefix ## cmp(type a, type b)\
2185 printf("%d %d %d %d %d %d\n",\
2186 a == b,\
2187 a != b,\
2188 a < b,\
2189 a > b,\
2190 a >= b,\
2191 a <= b);\
2192 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
2195 a + b,\
2196 a - b,\
2197 a * b,\
2198 a / b,\
2199 -a);\
2200 printf(fmt "\n", ++a);\
2201 printf(fmt "\n", a++);\
2202 printf(fmt "\n", a);\
2203 b = 0;\
2204 printf("%d %d\n", !a, !b);\
2206 void prefix ## fcast(type a)\
2208 float fa;\
2209 double da;\
2210 LONG_DOUBLE la;\
2211 int ia;\
2212 long long llia;\
2213 unsigned int ua;\
2214 unsigned long long llua;\
2215 type b;\
2216 fa = a;\
2217 da = a;\
2218 la = a;\
2219 printf("ftof: %f %f %Lf\n", fa, da, la);\
2220 ia = (int)a;\
2221 llia = (long long)a;\
2222 a = (a >= 0) ? a : -a;\
2223 ua = (unsigned int)a;\
2224 llua = (unsigned long long)a;\
2225 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
2226 ia = -1234;\
2227 ua = 0x81234500;\
2228 llia = -0x123456789012345LL;\
2229 llua = 0xf123456789012345LLU;\
2230 b = ia;\
2231 printf("itof: " fmt "\n", b);\
2232 b = ua;\
2233 printf("utof: " fmt "\n", b);\
2234 b = llia;\
2235 printf("lltof: " fmt "\n", b);\
2236 b = llua;\
2237 printf("ulltof: " fmt "\n", b);\
2240 float prefix ## retf(type a) { return a; }\
2241 double prefix ## retd(type a) { return a; }\
2242 LONG_DOUBLE prefix ## retld(type a) { return a; }\
2244 void prefix ## call(void)\
2246 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
2247 printf("double: %f\n", prefix ## retd(42.123456789));\
2248 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
2249 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
2252 void prefix ## signed_zeros(void) \
2254 type x = 0.0, y = -0.0, n, p;\
2255 if (x == y)\
2256 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
2257 1.0 / x != 1.0 / y);\
2258 else\
2259 printf ("x != y; this is wrong!\n");\
2261 n = -x;\
2262 if (x == n)\
2263 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
2264 1.0 / x != 1.0 / n);\
2265 else\
2266 printf ("x != -x; this is wrong!\n");\
2268 p = +y;\
2269 if (x == p)\
2270 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
2271 1.0 / x != 1.0 / p);\
2272 else\
2273 printf ("x != +y; this is wrong!\n");\
2274 p = -y;\
2275 if (x == p)\
2276 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
2277 1.0 / x != 1.0 / p);\
2278 else\
2279 printf ("x != -y; this is wrong!\n");\
2281 void prefix ## nan(void)\
2283 type nan = 0.0/0.0;\
2284 type nnan = -nan; \
2285 printf("nantest: " fmt " " fmt "\n", nan, nnan);\
2287 void prefix ## test(void)\
2289 printf("testing '%s'\n", #typename);\
2290 prefix ## cmp(1, 2.5);\
2291 prefix ## cmp(2, 1.5);\
2292 prefix ## cmp(1, 1);\
2293 prefix ## fcast(234.6);\
2294 prefix ## fcast(-2334.6);\
2295 prefix ## call();\
2296 prefix ## signed_zeros();\
2297 if (enable_nan_test) prefix ## nan();\
2300 FTEST(f, float, float, "%f")
2301 FTEST(d, double, double, "%f")
2302 FTEST(ld, long double, LONG_DOUBLE, "%Lf")
2304 double ftab1[3] = { 1.2, 3.4, -5.6 };
2307 void float_test(void)
2309 #if !defined(__arm__) || defined(__ARM_PCS_VFP) || defined __ANDROID__
2310 volatile float fa, fb;
2311 volatile double da, db;
2312 int a;
2313 unsigned int b;
2314 static double nan2 = 0.0/0.0;
2315 static double inf1 = 1.0/0.0;
2316 static double inf2 = 1e5000;
2317 volatile LONG_DOUBLE la;
2319 printf("sizeof(float) = %d\n", sizeof(float));
2320 printf("sizeof(double) = %d\n", sizeof(double));
2321 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));
2322 ftest();
2323 dtest();
2324 ldtest();
2325 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
2326 printf("%f %f %f\n", 2.12, .5, 2.3e10);
2327 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
2328 da = 123;
2329 printf("da=%f\n", da);
2330 fa = 123;
2331 printf("fa=%f\n", fa);
2332 a = 4000000000;
2333 da = a;
2334 printf("da = %f\n", da);
2335 b = 4000000000;
2336 db = b;
2337 printf("db = %f\n", db);
2338 printf("nan != nan = %d, inf1 = %f, inf2 = %f\n", nan2 != nan2, inf1, inf2);
2339 da = 0x0.88p-1022; /* a subnormal */
2340 la = da;
2341 printf ("da subnormal = %a\n", da);
2342 printf ("da subnormal = %.40g\n", da);
2343 printf ("la subnormal = %La\n", la);
2344 printf ("la subnormal = %.40Lg\n", la);
2345 da /= 2;
2346 la = da;
2347 printf ("da/2 subnormal = %a\n", da);
2348 printf ("da/2 subnormal = %.40g\n", da);
2349 printf ("la/2 subnormal = %La\n", la);
2350 printf ("la/2 subnormal = %.40Lg\n", la);
2351 fa = 0x0.88p-126f; /* a subnormal */
2352 la = fa;
2353 printf ("fa subnormal = %a\n", fa);
2354 printf ("fa subnormal = %.40g\n", fa);
2355 printf ("la subnormal = %La\n", la);
2356 printf ("la subnormal = %.40Lg\n", la);
2357 fa /= 2;
2358 la = fa;
2359 printf ("fa/2 subnormal = %a\n", fa);
2360 printf ("fa/2 subnormal = %.40g\n", fa);
2361 printf ("la/2 subnormal = %La\n", la);
2362 printf ("la/2 subnormal = %.40Lg\n", la);
2363 #endif
2366 int fib(int n)
2368 if (n <= 2)
2369 return 1;
2370 else
2371 return fib(n-1) + fib(n-2);
2374 #if __GNUC__ == 3 || __GNUC__ == 4
2375 # define aligned_function 0
2376 #else
2377 void __attribute__((aligned(16))) aligned_function(int i) {}
2378 #endif
2380 void funcptr_test()
2382 void (*func)(int);
2383 int a;
2384 struct {
2385 int dummy;
2386 void (*func)(int);
2387 } st1;
2388 long diff;
2390 func = &num;
2391 (*func)(12345);
2392 func = num;
2393 a = 1;
2394 a = 1;
2395 func(12345);
2396 /* more complicated pointer computation */
2397 st1.func = num;
2398 st1.func(12346);
2399 printf("sizeof1 = %d\n", sizeof(funcptr_test));
2400 printf("sizeof2 = %d\n", sizeof funcptr_test);
2401 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
2402 printf("sizeof4 = %d\n", sizeof &funcptr_test);
2403 a = 0;
2404 func = num + a;
2405 diff = func - num;
2406 func(42);
2407 (func + diff)(42);
2408 (num + a)(43);
2410 /* Check that we can align functions */
2411 func = aligned_function;
2412 printf("aligned_function (should be zero): %d\n", ((int)(uintptr_t)func) & 15);
2415 void lloptest(long long a, long long b)
2417 unsigned long long ua, ub;
2419 ua = a;
2420 ub = b;
2421 /* arith */
2422 printf("arith: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2423 a + b,
2424 a - b,
2425 a * b);
2427 if (b != 0) {
2428 printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2429 a / b,
2430 a % b);
2433 /* binary */
2434 printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2435 a & b,
2436 a | b,
2437 a ^ b);
2439 /* tests */
2440 printf("test: %d %d %d %d %d %d\n",
2441 a == b,
2442 a != b,
2443 a < b,
2444 a > b,
2445 a >= b,
2446 a <= b);
2448 printf("utest: %d %d %d %d %d %d\n",
2449 ua == ub,
2450 ua != ub,
2451 ua < ub,
2452 ua > ub,
2453 ua >= ub,
2454 ua <= ub);
2456 /* arith2 */
2457 a++;
2458 b++;
2459 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2460 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a++, b++);
2461 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", --a, --b);
2462 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2463 b = ub = 0;
2464 printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
2467 void llshift(long long a, int b)
2469 printf("shift: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2470 (unsigned long long)a >> b,
2471 a >> b,
2472 a << b);
2473 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2474 (unsigned long long)a >> 3,
2475 a >> 3,
2476 a << 3);
2477 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2478 (unsigned long long)a >> 35,
2479 a >> 35,
2480 a << 35);
2483 void llfloat(void)
2485 float fa;
2486 double da;
2487 LONG_DOUBLE lda;
2488 long long la, lb, lc;
2489 unsigned long long ula, ulb, ulc;
2490 la = 0x12345678;
2491 ula = 0x72345678;
2492 la = (la << 20) | 0x12345;
2493 ula = ula << 33;
2494 printf("la=" LONG_LONG_FORMAT " ula=" ULONG_LONG_FORMAT "\n", la, ula);
2496 fa = la;
2497 da = la;
2498 lda = la;
2499 printf("lltof: %f %f %Lf\n", fa, da, lda);
2501 la = fa;
2502 lb = da;
2503 lc = lda;
2504 printf("ftoll: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", la, lb, lc);
2506 fa = ula;
2507 da = ula;
2508 lda = ula;
2509 printf("ulltof: %f %f %Lf\n", fa, da, lda);
2511 ula = fa;
2512 ulb = da;
2513 ulc = lda;
2514 printf("ftoull: " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT "\n", ula, ulb, ulc);
2517 long long llfunc1(int a)
2519 return a * 2;
2522 struct S {
2523 int id;
2524 char item;
2527 long long int value(struct S *v)
2529 return ((long long int)v->item);
2532 long long llfunc2(long long x, long long y, int z)
2534 return x * y * z;
2537 void check_opl_save_regs(char *a, long long b, int c)
2539 *a = b < 0 && !c;
2542 void longlong_test(void)
2544 long long a, b, c;
2545 int ia;
2546 unsigned int ua;
2547 printf("sizeof(long long) = %d\n", sizeof(long long));
2548 ia = -1;
2549 ua = -2;
2550 a = ia;
2551 b = ua;
2552 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2553 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %Lx\n",
2554 (long long)1,
2555 (long long)-2,
2556 1LL,
2557 0x1234567812345679);
2558 a = llfunc1(-3);
2559 printf(LONG_LONG_FORMAT "\n", a);
2561 lloptest(1000, 23);
2562 lloptest(0xff, 0x1234);
2563 b = 0x72345678 << 10;
2564 lloptest(-3, b);
2565 llshift(0x123, 5);
2566 llshift(-23, 5);
2567 b = 0x72345678LL << 10;
2568 llshift(b, 47);
2570 llfloat();
2571 #if 1
2572 b = 0x12345678;
2573 a = -1;
2574 c = a + b;
2575 printf(XLONG_LONG_FORMAT"\n", c);
2576 #endif
2578 /* long long reg spill test */
2580 struct S a;
2582 a.item = 3;
2583 printf("%lld\n", value(&a));
2585 lloptest(0x80000000, 0);
2588 long long *p, v, **pp;
2589 v = 1;
2590 p = &v;
2591 p[0]++;
2592 printf("another long long spill test : %lld\n", *p);
2593 pp = &p;
2595 v = llfunc2(**pp, **pp, ia);
2596 printf("a long long function (arm-)reg-args test : %lld\n", v);
2598 a = 68719476720LL;
2599 b = 4294967295LL;
2600 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
2602 printf(LONG_LONG_FORMAT "\n", 0x123456789LLU);
2604 /* long long pointer deref in argument passing test */
2605 a = 0x123;
2606 long long *p = &a;
2607 llshift(*p, 5);
2609 /* shortening followed by widening */
2610 unsigned long long u = 0x8000000000000001ULL;
2611 u = (unsigned)(u + 1);
2612 printf("long long u=" ULONG_LONG_FORMAT "\n", u);
2613 u = 0x11223344aa998877ULL;
2614 u = (unsigned)(int)(u + 1);
2615 printf("long long u=" ULONG_LONG_FORMAT "\n", u);
2617 /* was a problem with missing save_regs in gen_opl on 32-bit platforms */
2618 char cc = 78;
2619 check_opl_save_regs(&cc, -1, 0);
2620 printf("check_opl_save_regs: %d\n", cc);
2623 void manyarg_test(void)
2625 LONG_DOUBLE ld = 1234567891234LL;
2626 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2627 1, 2, 3, 4, 5, 6, 7, 8,
2628 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2629 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2630 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2631 1, 2, 3, 4, 5, 6, 7, 8,
2632 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2633 1234567891234LL, 987654321986LL,
2634 42.0, 43.0);
2635 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2636 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2637 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2638 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2639 1234567891234LL, 987654321986LL,
2640 42.0, 43.0);
2641 printf("%d %d %d %d %d %d %d %d %Lf\n",
2642 1, 2, 3, 4, 5, 6, 7, 8, ld);
2643 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2644 LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n",
2645 1, 2, 3, 4, 5, 6, 7, 8,
2646 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2647 1234567891234LL, 987654321986LL,
2648 42.0, 43.0, ld);
2649 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2650 "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n",
2651 1, 2, 3, 4, 5, 6, 7, 8,
2652 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2653 ld, 1234567891234LL, 987654321986LL,
2654 42.0, 43.0, ld);
2657 void*
2658 va_arg_with_struct_ptr(va_list ap) {
2660 * This was a BUG identified with FFTW-3.3.8 on arm64.
2661 * The test case only checks it compiles on all supported
2662 * architectures. This function is not currently called.
2664 struct X { int _x; };
2665 struct X *x = va_arg(ap, struct X *);
2666 return x;
2669 void vprintf1(const char *fmt, ...)
2671 va_list ap, aq;
2672 const char *p;
2673 int c, i;
2674 double d;
2675 long long ll;
2676 LONG_DOUBLE ld;
2678 va_start(aq, fmt);
2679 va_copy(ap, aq);
2681 p = fmt;
2682 for(;;) {
2683 c = *p;
2684 if (c == '\0')
2685 break;
2686 p++;
2687 if (c == '%') {
2688 c = *p;
2689 switch(c) {
2690 case '\0':
2691 goto the_end;
2692 case 'd':
2693 i = va_arg(ap, int);
2694 printf("%d", i);
2695 break;
2696 case 'f':
2697 d = va_arg(ap, double);
2698 printf("%f", d);
2699 break;
2700 case 'l':
2701 ll = va_arg(ap, long long);
2702 printf(LONG_LONG_FORMAT, ll);
2703 break;
2704 case 'F':
2705 ld = va_arg(ap, LONG_DOUBLE);
2706 printf("%Lf", ld);
2707 break;
2709 p++;
2710 } else {
2711 putchar(c);
2714 the_end:
2715 va_end(aq);
2716 va_end(ap);
2719 struct myspace {
2720 short int profile;
2722 struct myspace2 {
2723 #if CC_NAME == CC_clang /* clang7 doesn't support zero sized structs */
2724 char a[1];
2725 #else
2726 char a[0];
2727 #endif
2729 struct myspace3 {
2730 char a[1];
2732 struct myspace4 {
2733 char a[2];
2735 struct mytest {
2736 void *foo, *bar, *baz;
2739 struct mytest stdarg_for_struct(struct myspace bob, ...)
2741 struct myspace george, bill;
2742 struct myspace2 alex1;
2743 struct myspace3 alex2;
2744 struct myspace4 alex3;
2745 va_list ap;
2746 short int validate;
2748 va_start(ap, bob);
2749 alex1 = va_arg(ap, struct myspace2);
2750 alex2 = va_arg(ap, struct myspace3);
2751 alex3 = va_arg(ap, struct myspace4);
2752 bill = va_arg(ap, struct myspace);
2753 george = va_arg(ap, struct myspace);
2754 validate = va_arg(ap, int);
2755 printf("stdarg_for_struct: %d %d %d %d %d %d %d\n",
2756 alex2.a[0], alex3.a[0], alex3.a[1],
2757 bob.profile, bill.profile, george.profile, validate);
2758 va_end(ap);
2759 return (struct mytest) {};
2762 void stdarg_for_libc(const char *fmt, ...)
2764 va_list args;
2765 va_start(args, fmt);
2766 vprintf(fmt, args);
2767 va_end(args);
2770 void stdarg_syntax(int n, ...)
2772 int i;
2773 va_list ap;
2774 if (1)
2775 va_start(ap, n);
2776 else
2778 i = va_arg(ap, int);
2779 printf("stdarg_void_expr: %d\n", i);
2780 (va_end(ap));
2783 typedef struct{
2784 double x,y;
2785 } point;
2786 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}};
2788 static void stdarg_double_struct(int nargs, int posd,...)
2790 int i;
2791 double d;
2792 point pi;
2793 va_list args;
2795 printf ("stdarg_double_struct: %d\n", posd);
2796 va_start(args,posd);
2797 for(i = 0; i < nargs; i++) {
2798 if (i == posd) {
2799 d = va_arg (args, double);
2800 printf ("d %d = %g\n", i, d);
2802 else {
2803 pi = va_arg (args, point);
2804 printf ("pts[%d] = %g %g\n", i, pi.x, pi.y);
2807 va_end(args);
2810 void stdarg_test(void)
2812 LONG_DOUBLE ld = 1234567891234LL;
2813 struct myspace bob;
2814 struct myspace2 bob2;
2815 struct myspace3 bob3;
2816 struct myspace4 bob4;
2818 vprintf1("%d %d %d\n", 1, 2, 3);
2819 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2820 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2821 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2822 vprintf1("%d %f %l %F %d %f %l %F\n",
2823 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2824 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2825 1, 2, 3, 4, 5, 6, 7, 8,
2826 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2827 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2828 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 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2831 "%l %l %f %f\n",
2832 1, 2, 3, 4, 5, 6, 7, 8,
2833 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2834 1234567891234LL, 987654321986LL,
2835 42.0, 43.0);
2836 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2837 "%l %l %f %f\n",
2838 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2839 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2840 1234567891234LL, 987654321986LL,
2841 42.0, 43.0);
2842 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2843 1, 2, 3, 4, 5, 6, 7, 8, ld);
2844 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2845 "%l %l %f %f %F\n",
2846 1, 2, 3, 4, 5, 6, 7, 8,
2847 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2848 1234567891234LL, 987654321986LL,
2849 42.0, 43.0, ld);
2850 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2851 "%F %l %l %f %f %F\n",
2852 1, 2, 3, 4, 5, 6, 7, 8,
2853 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2854 ld, 1234567891234LL, 987654321986LL,
2855 42.0, 43.0, ld);
2857 bob.profile = 42;
2858 bob3.a[0] = 1;
2859 bob4.a[0] = 2;
2860 bob4.a[1] = 3;
2861 stdarg_for_struct(bob, bob2, bob3, bob4, bob, bob, bob.profile);
2862 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2863 stdarg_syntax(1, 17);
2864 stdarg_double_struct(6,-1,pts[0],pts[1],pts[2],pts[3],pts[4],pts[5]);
2865 stdarg_double_struct(7,1,pts[0],-1.0,pts[1],pts[2],pts[3],pts[4],pts[5]);
2866 stdarg_double_struct(7,2,pts[0],pts[1],-1.0,pts[2],pts[3],pts[4],pts[5]);
2867 stdarg_double_struct(7,3,pts[0],pts[1],pts[2],-1.0,pts[3],pts[4],pts[5]);
2868 stdarg_double_struct(7,4,pts[0],pts[1],pts[2],pts[3],-1.0,pts[4],pts[5]);
2869 stdarg_double_struct(7,5,pts[0],pts[1],pts[2],pts[3],pts[4],-1.0,pts[5]);
2872 int reltab[3] = { 1, 2, 3 };
2874 int *rel1 = &reltab[1];
2875 int *rel2 = &reltab[2];
2877 void getmyaddress(void)
2879 printf("in getmyaddress\n");
2882 #ifdef __LP64__
2883 long __pa_symbol(void)
2885 /* This 64bit constant was handled incorrectly, it was used as addend
2886 (which can hold 64bit just fine) in connection with a symbol,
2887 and TCC generates wrong code for that (displacements are 32bit only).
2888 This effectively is "+ 0x80000000", and if addresses of globals
2889 are below 2GB the result should be a number without high 32 bits set. */
2890 return ((long)(((unsigned long)(&rel1))) - (0xffffffff80000000UL));
2892 #endif
2894 uintptr_t theaddress = (uintptr_t)getmyaddress;
2895 void relocation_test(void)
2897 void (*fptr)(void) = (void (*)(void))theaddress;
2898 printf("*rel1=%d\n", *rel1);
2899 printf("*rel2=%d\n", *rel2);
2900 fptr();
2901 #ifdef __LP64__
2902 // compare 'addend' displacement versus conventional arithmetics
2903 printf("pa_symbol: %d\n", (long)&rel1 == __pa_symbol() - 0x80000000);
2904 #endif
2907 void old_style_f(a,b,c)
2908 int a, b;
2909 double c;
2911 printf("a=%d b=%d b=%f\n", a, b, c);
2914 void decl_func1(int cmpfn())
2916 printf("cmpfn=%lx\n", (long)cmpfn);
2919 void decl_func2(cmpfn)
2920 int cmpfn();
2922 printf("cmpfn=%lx\n", (long)cmpfn);
2925 void old_style_function_test(void)
2927 #if CC_NAME == CC_clang
2928 /* recent clang versions (at least 15.0) raise an error:
2929 incompatible pointer to integer conversion passing 'void *'
2930 For the purpose of this test, pass 1 instead.
2932 old_style_f(1, 2, 3.0);
2933 #else
2934 old_style_f((void *)1, 2, 3.0);
2935 #endif
2936 decl_func1(NULL);
2937 decl_func2(NULL);
2940 void alloca_test()
2942 #if defined __i386__ || defined __x86_64__ || defined __arm__
2943 char *p = alloca(16);
2944 strcpy(p,"123456789012345");
2945 printf("alloca: p is %s\n", p);
2946 char *demo = "This is only a test.\n";
2947 /* Test alloca embedded in a larger expression */
2948 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
2949 #endif
2952 void *bounds_checking_is_enabled()
2954 char ca[10], *cp = ca-1;
2955 return (ca != cp + 1) ? cp : NULL;
2958 typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
2960 void c99_vla_test_1(int size1, int size2)
2962 int size = size1 * size2;
2963 int tab1[size][2], tab2[10][2];
2964 void *tab1_ptr, *tab2_ptr, *bad_ptr;
2966 /* "size" should have been 'captured' at tab1 declaration,
2967 so modifying it should have no effect on VLA behaviour. */
2968 size = size-1;
2970 printf("Test C99 VLA 1 (sizeof): ");
2971 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
2972 tab1_ptr = tab1;
2973 tab2_ptr = tab2;
2974 printf("Test C99 VLA 2 (ptrs subtract): ");
2975 printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2976 printf("Test C99 VLA 3 (ptr add): ");
2977 printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2978 printf("Test C99 VLA 4 (ptr access): ");
2979 tab1[size1][1] = 42;
2980 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2982 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2983 if (bad_ptr = bounds_checking_is_enabled()) {
2984 int *t1 = &tab1[size1 * size2 - 1][3];
2985 int *t2 = &tab2[9][3];
2986 printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
2987 printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
2989 char*c1 = 1 + sizeof(tab1) + (char*)tab1;
2990 char*c2 = 1 + sizeof(tab2) + (char*)tab2;
2991 printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
2992 printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
2994 int *i1 = tab1[-1];
2995 int *i2 = tab2[-1];
2996 printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
2997 printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
2999 int *x1 = tab1[size1 * size2 + 1];
3000 int *x2 = tab2[10 + 1];
3001 printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
3002 printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
3003 } else {
3004 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
3006 printf("\n");
3009 void c99_vla_test_2(int d, int h, int w)
3011 int x, y, z;
3012 int (*arr)[h][w] = malloc(sizeof(int) * d*h*w);
3013 int c = 1;
3014 static int (*starr)[h][w];
3016 printf("Test C99 VLA 6 (pointer)\n");
3018 for (z=0; z<d; z++) {
3019 for (y=0; y<h; y++) {
3020 for (x=0; x<w; x++) {
3021 arr[z][y][x] = c++;
3025 for (z=0; z<d; z++) {
3026 for (y=0; y<h; y++) {
3027 for (x=0; x<w; x++) {
3028 printf(" %2d", arr[z][y][x]);
3030 puts("");
3032 puts("");
3034 starr = &arr[1];
3035 printf(" sizes : %d %d %d\n"
3036 " pdiff : %d %d\n"
3037 " tests : %d %d %d\n",
3038 sizeof (*arr), sizeof (*arr)[0], sizeof (*arr)[0][0],
3039 arr + 2 - arr, *arr + 3 - *arr,
3040 0 == sizeof (*arr + 1) - sizeof arr,
3041 0 == sizeof sizeof *arr - sizeof arr,
3042 starr[0][2][3] == arr[1][2][3]
3044 free (arr);
3047 void c99_vla_test_3a (int arr[2][3][4])
3049 printf ("%d\n", arr[1][2][3]);
3052 void c99_vla_test_3b(int s, int arr[s][3][4])
3054 printf ("%d\n", arr[1][2][3]);
3057 void c99_vla_test_3c(int s, int arr[2][s][4])
3059 printf ("%d\n", arr[1][2][3]);
3062 void c99_vla_test_3d(int s, int arr[2][3][s])
3064 printf ("%d\n", arr[1][2][3]);
3067 void c99_vla_test_3e(int s, int arr[][3][--s])
3069 printf ("%d %d %d\n", sizeof arr, s, arr[1][2][3]);
3072 void c99_vla_test_3(void)
3074 int a[2][3][4];
3076 memset (a, 0, sizeof(a));
3077 a[1][2][3] = 123;
3078 c99_vla_test_3a(a);
3079 c99_vla_test_3b(2, a);
3080 c99_vla_test_3c(3, a);
3081 c99_vla_test_3d(4, a);
3082 c99_vla_test_3e(5, a);
3085 void c99_vla_test(void)
3087 c99_vla_test_1(5, 2);
3088 c99_vla_test_2(3, 4, 5);
3089 c99_vla_test_3();
3093 void sizeof_test(void)
3095 int a;
3096 int **ptr;
3098 printf("sizeof(int) = %d\n", sizeof(int));
3099 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
3100 printf("sizeof(long) = %d\n", sizeof(long));
3101 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
3102 printf("sizeof(short) = %d\n", sizeof(short));
3103 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
3104 printf("sizeof(char) = %d\n", sizeof(char));
3105 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
3106 printf("sizeof(func) = %d\n", sizeof sizeof_test());
3107 a = 1;
3108 printf("sizeof(a++) = %d\n", sizeof a++);
3109 printf("a=%d\n", a);
3110 ptr = NULL;
3111 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
3113 /* The type of sizeof should be as large as a pointer, actually
3114 it should be size_t. */
3115 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
3116 uintptr_t t = 1;
3117 uintptr_t t2;
3118 /* Effectively <<32, but defined also on 32bit machines. */
3119 t <<= 16;
3120 t <<= 16;
3121 t++;
3122 /* This checks that sizeof really can be used to manipulate
3123 uintptr_t objects, without truncation. */
3124 t2 = t & -sizeof(uintptr_t);
3125 printf ("%lu %lu\n", t, t2);
3127 /* some alignof tests */
3128 printf("__alignof__(int) = %d\n", __alignof__(int));
3129 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
3130 printf("__alignof__(short) = %d\n", __alignof__(short));
3131 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
3132 printf("__alignof__(char) = %d\n", __alignof__(char));
3133 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
3134 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
3136 /* sizes of VLAs need to be evaluated even inside sizeof: */
3137 a = 2;
3138 printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a]));
3139 /* And checking if sizeof compound literal works. Parenthesized: */
3140 printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n",
3141 sizeof( (struct {int i; int j;}){4,5} ));
3142 /* And as direct sizeof argument (as unary expression): */
3143 printf("sizeof (struct {short i; short j;}){4,5} = %d\n",
3144 sizeof (struct {short i; short j;}){4,5} );
3146 /* sizeof(x && y) should be sizeof(int), even if constant
3147 evaluating is possible. */
3148 printf("sizeof(t && 0) = %d\n", sizeof(t && 0));
3149 printf("sizeof(1 && 1) = %d\n", sizeof(1 && 1));
3150 printf("sizeof(t || 1) = %d\n", sizeof(t || 1));
3151 printf("sizeof(0 || 0) = %d\n", sizeof(0 || 0));
3153 int arr[4], fn();
3154 printf("sizeof(0, arr) = %d\n", sizeof(0, arr));
3155 printf("sizeof(0, fn) = %d\n", sizeof(0, fn));
3158 void typeof_test(void)
3160 double a;
3161 typeof(a) b;
3162 typeof(float) c;
3164 a = 1.5;
3165 b = 2.5;
3166 c = 3.5;
3167 printf("a=%f b=%f c=%f\n", a, b, c);
3171 struct hlist_node;
3172 struct hlist_head {
3173 struct hlist_node *first, *last;
3176 void consume_ulong (unsigned long i)
3178 i = 0;
3181 void statement_expr_test(void)
3183 int a, i;
3185 /* Basic stmt expr test */
3186 a = 0;
3187 for(i=0;i<10;i++) {
3188 a += 1 +
3189 ( { int b, j;
3190 b = 0;
3191 for(j=0;j<5;j++)
3192 b += j; b;
3193 } );
3195 printf("a=%d\n", a);
3197 /* Test that symbols aren't freed prematurely.
3198 With SYM_DEBUG valgrind will show a read from a freed
3199 symbol, and tcc will show an (invalid) warning on the initialization
3200 of 'ptr' below, if symbols are popped after the stmt expr. */
3201 void *v = (void*)39;
3202 typeof(({
3203 (struct hlist_node *)v;
3204 })) x;
3205 typeof (x)
3206 ptr = (struct hlist_node *)v;
3208 /* This part used to segfault when symbols were popped prematurely.
3209 The symbols for the static local would be overwritten with
3210 helper symbols from the pre-processor expansions in between. */
3211 #define some_attr __attribute__((aligned(1)))
3212 #define tps(str) ({ \
3213 static const char *t some_attr = str; \
3214 t; \
3216 printf ("stmtexpr: %s %s\n",
3217 tps("somerandomlongstring"),
3218 tps("anotherlongstring"));
3220 /* Test that the three decls of 't' don't interact. */
3221 int t = 40;
3222 int b = ({ int t = 41; t; });
3223 int c = ({ int t = 42; t; });
3225 /* Test that aggregate return values work. */
3226 struct hlist_head h
3227 = ({
3228 typedef struct hlist_head T;
3229 long pre = 48;
3230 T t = { (void*)43, (void*)44 };
3231 long post = 49;
3234 printf ("stmtexpr: %d %d %d\n", t, b, c);
3235 printf ("stmtexpr: %ld %ld\n", (long)h.first, (long)h.last);
3237 /* Test that we can give out addresses of local labels. */
3238 consume_ulong(({ __label__ __here; __here: (unsigned long)&&__here; }));
3240 /* Test interaction between local and global label stacks and the
3241 need to defer popping symbol from them when within statement
3242 expressions. Note how the labels are both named LBL. */
3243 i = 0;
3246 __label__ LBL;
3247 LBL: if (i++ == 0) goto LBL;
3249 /* jump to a classical label out of an expr-stmt that had previously
3250 overshadowed that classical label */
3251 goto LBL;
3253 LBL:
3254 printf("stmtexpr: %d should be 2\n", i);
3257 void local_label_test(void)
3259 int a;
3260 goto l1;
3262 a = 1 + ({
3263 __label__ l1, l2, l3, l4;
3264 goto l1;
3266 printf("aa1\n");
3267 goto l3;
3269 printf("aa3\n");
3270 goto l4;
3272 printf("aa2\n");
3273 goto l2;
3274 l3:;
3277 printf("a=%d\n", a);
3278 return;
3280 printf("bb1\n");
3281 goto l2;
3283 printf("bb2\n");
3284 goto l4;
3287 /* inline assembler test */
3288 #if defined(__i386__) || defined(__x86_64__)
3290 typedef __SIZE_TYPE__ word;
3292 /* from linux kernel */
3293 static char * strncat1(char * dest,const char * src,size_t count)
3295 word d0, d1, d2, d3;
3296 __asm__ __volatile__(
3297 "repne\n\t"
3298 "scasb\n\t"
3299 "dec %1\n\t"
3300 "mov %8,%3\n"
3301 "1:\tdec %3\n\t"
3302 "js 2f\n\t"
3303 "lodsb\n\t"
3304 "stosb\n\t"
3305 "testb %%al,%%al\n\t"
3306 "jne 1b\n"
3307 "2:\txor %2,%2\n\t"
3308 "stosb"
3309 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
3310 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
3311 : "memory");
3312 return dest;
3315 static char * strncat2(char * dest,const char * src,size_t count)
3317 word d0, d1, d2, d3;
3318 __asm__ __volatile__(
3319 "repne scasb\n\t" /* one-line repne prefix + string op */
3320 "dec %1\n\t"
3321 "mov %8,%3\n"
3322 "1:\tdec %3\n\t"
3323 "js 2f\n\t"
3324 "lodsb\n\t"
3325 "stosb\n\t"
3326 "testb %%al,%%al\n\t"
3327 "jne 1b\n"
3328 "2:\txor %2,%2\n\t"
3329 "stosb"
3330 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
3331 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
3332 : "memory");
3333 return dest;
3336 static inline void * memcpy1(void * to, const void * from, size_t n)
3338 word d0, d1, d2;
3339 __asm__ __volatile__(
3340 "rep ; movsl\n\t"
3341 "testb $2,%b4\n\t"
3342 "je 1f\n\t"
3343 "movsw\n"
3344 "1:\ttestb $1,%b4\n\t"
3345 "je 2f\n\t"
3346 "movsb\n"
3347 "2:"
3348 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
3349 :"0" (n/4), "q" (n),"1" ((word) to),"2" ((word) from)
3350 : "memory");
3351 return (to);
3354 static inline void * memcpy2(void * to, const void * from, size_t n)
3356 word d0, d1, d2;
3357 __asm__ __volatile__(
3358 "rep movsl\n\t" /* one-line rep prefix + string op */
3359 "testb $2,%b4\n\t"
3360 "je 1f\n\t"
3361 "movsw\n"
3362 "1:\ttestb $1,%b4\n\t"
3363 "je 2f\n\t"
3364 "movsb\n"
3365 "2:"
3366 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
3367 :"0" (n/4), "q" (n),"1" ((word) to),"2" ((word) from)
3368 : "memory");
3369 return (to);
3372 static __inline__ void sigaddset1(unsigned int *set, int _sig)
3374 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
3377 static __inline__ void sigdelset1(unsigned int *set, int _sig)
3379 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc", "flags");
3382 #ifdef __clang__
3383 /* clang's inline asm is uncapable of 'xchgb %b0,%h0' */
3384 static __inline__ __const__ unsigned int swab32(unsigned int x)
3386 return ((x >> 24) & 0xff) |
3387 ((x >> 8) & 0xff00) |
3388 ((x << 8) & 0xff0000) |
3389 ((x << 24) & 0xff000000);
3391 #else
3392 static __inline__ __const__ unsigned int swab32(unsigned int x)
3394 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
3395 "rorl $16,%0\n\t" /* swap words */
3396 "xchgb %b0,%h0" /* swap higher bytes */
3397 :"=" "q" (x)
3398 : "0" (x));
3399 return x;
3401 #endif
3403 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
3405 unsigned long long res;
3406 #ifdef __x86_64__
3407 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
3408 but still test the 32bit->64bit mull. */
3409 unsigned int resh, resl;
3410 __asm__("mull %2" : "=a" (resl), "=d" (resh) : "a" (a), "r" (b));
3411 res = ((unsigned long long)resh << 32) | resl;
3412 #else
3413 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
3414 #endif
3415 return res;
3418 static __inline__ unsigned long long inc64(unsigned long long a)
3420 unsigned long long res;
3421 #ifdef __x86_64__
3422 /* Using the A constraint is wrong, and increments are tested
3423 elsewhere. */
3424 res = a + 1;
3425 #else
3426 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
3427 #endif
3428 return res;
3431 struct struct123 {
3432 int a;
3433 int b;
3435 struct struct1231 {
3436 word addr;
3439 word mconstraint_test(struct struct1231 *r)
3441 word ret;
3442 unsigned int a[2];
3443 a[0] = 0;
3444 __asm__ volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
3445 : "=&r" (ret), "=m" (a)
3446 : "m" (*(struct struct123 *)r->addr));
3447 return ret + a[0];
3450 #ifdef __x86_64__
3451 int fls64(unsigned long long x)
3453 int bitpos = -1;
3454 asm("bsrq %1,%q0"
3455 : "+r" (bitpos)
3456 : "rm" (x));
3457 return bitpos + 1;
3459 #endif
3461 void other_constraints_test(void)
3463 word ret;
3464 int var;
3465 #if CC_NAME != CC_clang
3466 __asm__ volatile ("mov %P1,%0" : "=r" (ret) : "p" (&var));
3467 printf ("oc1: %d\n", ret == (word)&var);
3468 #endif
3471 #ifndef _WIN32
3472 /* Test global asm blocks playing with aliases. */
3473 void base_func(void)
3475 printf ("asmc: base\n");
3478 #ifndef __APPLE__
3479 extern void override_func1 (void);
3480 extern void override_func2 (void);
3482 asm(".weak override_func1\n.set override_func1, base_func");
3483 asm(".set override_func1, base_func");
3484 asm(".set override_func2, base_func");
3486 void override_func2 (void)
3488 printf ("asmc: override2\n");
3491 /* This checks a construct used by the linux kernel to encode
3492 references to strings by PC relative references. */
3493 extern int bug_table[] __attribute__((section("__bug_table")));
3494 char * get_asm_string (void)
3496 /* On i386 when -fPIC is enabled this would cause a compile error with GCC,
3497 the problem being the "i" constraint used with a symbolic operand
3498 resolving to a local label. That check is overly zealous as the code
3499 within the asm makes sure to use it only in PIC-possible contexts,
3500 but all GCC versions behave like so. We arrange for PIC to be disabled
3501 for compiling tcctest.c in the Makefile.
3503 Additionally the usage of 'c' in "%c0" in the template is actually wrong,
3504 as that would expect an operand that is a condition code. The operand
3505 as is (a local label) is accepted by GCC in non-PIC mode, and on x86-64.
3506 What the linux kernel really wanted is 'p' to disable the addition of '$'
3507 to the printed operand (as in "$.LC0" where the template only wants the
3508 bare operand ".LC0"). But the code below is what the linux kernel
3509 happens to use and as such is the one we want to test. */
3510 #ifndef __clang__
3511 extern int some_symbol;
3512 asm volatile (".globl some_symbol\n"
3513 "jmp .+6\n"
3514 "1:\n"
3515 "some_symbol: .long 0\n"
3516 ".pushsection __bug_table, \"a\"\n"
3517 ".globl bug_table\n"
3518 "bug_table:\n"
3519 /* The first entry (1b-2b) is unused in this test,
3520 but we include it to check if cross-section
3521 PC-relative references work. */
3522 "2:\t.long 1b - 2b, %c0 - 2b\n"
3523 ".popsection\n" : : "i" ("A string"));
3524 char * str = ((char*)bug_table) + bug_table[1];
3525 return str;
3526 #else
3527 return (char *) "A string";
3528 #endif
3531 /* This checks another constructs with local labels. */
3532 extern unsigned char alld_stuff[];
3533 asm(".data\n"
3534 ".byte 41\n"
3535 "alld_stuff:\n"
3536 "661:\n"
3537 ".byte 42\n"
3538 "662:\n"
3539 ".pushsection .data.ignore\n"
3540 ".long 661b - .\n" /* This reference to 661 generates an external sym
3541 which shouldn't somehow overwrite the offset that's
3542 already determined for it. */
3543 ".popsection\n"
3544 ".byte 662b - 661b\n" /* So that this value is undeniably 1. */);
3546 void asm_local_label_diff (void)
3548 printf ("asm_local_label_diff: %d %d\n", alld_stuff[0], alld_stuff[1]);
3550 #endif
3551 #endif
3553 /* This checks that static local variables are available from assembler. */
3554 void asm_local_statics (void)
3556 static int localint = 41;
3557 asm("incl %0" : "+m" (localint));
3558 printf ("asm_local_statics: %d\n", localint);
3561 static
3562 unsigned int set;
3564 void fancy_copy (unsigned *in, unsigned *out)
3566 asm volatile ("" : "=r" (*out) : "0" (*in));
3569 void fancy_copy2 (unsigned *in, unsigned *out)
3571 asm volatile ("mov %0,(%1)" : : "r" (*in), "r" (out) : "memory");
3574 #if defined __x86_64__
3575 void clobber_r12(void)
3577 asm volatile("mov $1, %%r12" ::: "r12");
3579 #endif
3581 void test_high_clobbers_really(void)
3583 #if defined __x86_64__
3584 register word val asm("r12");
3585 word val2;
3586 /* This tests if asm clobbers correctly save/restore callee saved
3587 registers if they are clobbered and if it's the high 8 x86-64
3588 registers. This is fragile for GCC as the constraints do not
3589 correctly capture the data flow, but good enough for us. */
3590 asm volatile("mov $0x4542, %%r12" : "=r" (val):: "memory");
3591 clobber_r12();
3592 asm volatile("mov %%r12, %0" : "=r" (val2) : "r" (val): "memory");
3593 printf("asmhc: 0x%x\n", val2);
3594 #endif
3597 void test_high_clobbers(void)
3599 #if defined __x86_64__
3600 word x1, x2;
3601 asm volatile("mov %%r12,%0" :: "m" (x1)); /* save r12 */
3602 test_high_clobbers_really();
3603 asm volatile("mov %%r12,%0" :: "m" (x2)); /* new r12 */
3604 asm volatile("mov %0,%%r12" :: "m" (x1)); /* restore r12 */
3605 /* should be 0 but tcc doesn't save r12 automatically, which has
3606 bad effects when gcc helds TCCState *s in r12 in tcc.c:main */
3607 //printf("r12-clobber-diff: %lx\n", x2 - x1);
3608 #endif
3611 static long cpu_number;
3612 void trace_console(long len, long len2)
3614 #ifdef __x86_64__
3615 /* This generated invalid code when the emission of the switch
3616 table isn't disabled. The asms are necessary to show the bug,
3617 normal statements don't work (they need to generate some code
3618 even under nocode_wanted, which normal statements don't do,
3619 but asms do). Also at least these number of cases is necessary
3620 to generate enough "random" bytes. They ultimately are enough
3621 to create invalid instruction patterns to which the first
3622 skip-to-decision-table jump jumps. If decision table emission
3623 is disabled all of this is no problem.
3625 It also is necessary that the switches are in a statement expression
3626 (which has the property of not being enterable from outside. no
3627 matter what). */
3628 if (0
3631 long pscr_ret__;
3632 switch(len) {
3633 case 4:
3635 long pfo_ret__;
3636 switch (len2) {
3637 case 8: printf("bla"); pfo_ret__ = 42; break;
3639 pscr_ret__ = pfo_ret__;
3641 break;
3642 case 8:
3644 long pfo_ret__;
3645 switch (len2) {
3646 case 1:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3647 case 2:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3648 case 4:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3649 case 8:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3650 default: printf("impossible\n");
3652 pscr_ret__ = pfo_ret__;
3654 break;
3656 pscr_ret__;
3659 printf("huh?\n");
3661 #endif
3664 void test_asm_dead_code(void)
3666 word rdi;
3667 /* Try to make sure that xdi contains a zero, and hence will
3668 lead to a segfault if the next asm is evaluated without
3669 arguments being set up. */
3670 asm volatile ("" : "=D" (rdi) : "0" (0));
3671 (void)sizeof (({
3672 int var;
3673 /* This shouldn't trigger a segfault, either the argument
3674 registers need to be set up and the asm emitted despite
3675 this being in an unevaluated context, or both the argument
3676 setup _and_ the asm emission need to be suppressed. The latter
3677 is better. Disabling asm code gen when suppression is on
3678 also fixes the above trace_console bug, but that came earlier
3679 than asm suppression. */
3680 asm volatile ("movl $0,(%0)" : : "D" (&var) : "memory");
3681 var;
3682 }));
3685 void test_asm_call(void)
3687 #if defined __x86_64__ && !defined _WIN64 && !defined(__APPLE__)
3688 static char str[] = "PATH";
3689 char *s;
3690 /* This tests if a reference to an undefined symbol from an asm
3691 block, which isn't otherwise referenced in this file, is correctly
3692 regarded as global symbol, so that it's resolved by other object files
3693 or libraries. We chose getenv here, which isn't used anywhere else
3694 in this file. (If we used e.g. printf, which is used we already
3695 would have a global symbol entry, not triggering the bug which is
3696 tested here). */
3697 /* two pushes so stack remains aligned */
3698 asm volatile ("push %%rdi; push %%rdi; mov %0, %%rdi;"
3699 #if 1 && !defined(__TINYC__) && (defined(__PIC__) || defined(__PIE__))
3700 "call getenv@plt;"
3701 #else
3702 "call getenv;"
3703 #endif
3704 "pop %%rdi; pop %%rdi"
3705 : "=a" (s) : "r" (str));
3706 printf("asmd: %s\n", s);
3707 #endif
3710 #if defined __x86_64__
3711 # define RX "(%rip)"
3712 #else
3713 # define RX
3714 #endif
3716 void asm_dot_test(void)
3718 #ifndef __APPLE__
3719 int x;
3720 for (x = 1;; ++x) {
3721 int r = x;
3722 switch (x) {
3723 case 1:
3724 asm(".text; lea S"RX",%eax; lea ."RX",%ecx; sub %ecx,%eax; S=.; jmp p0");
3725 case 2:
3726 #ifndef __clang__
3727 /* clangs internal assembler is broken */
3728 asm(".text; jmp .+6; .int 123; mov .-4"RX",%eax; jmp p0");
3729 #else
3730 asm(".text; mov $123, %eax; jmp p0");
3731 #endif
3732 case 3:
3733 #if !defined(_WIN32) && !defined(__clang__)
3734 asm(".pushsection \".data\"; Y=.; .int 999; X=Y; .int 456; X=.-4; .popsection");
3735 #else
3736 asm(".data; Y=.; .int 999; X=Y; .int 456; X=.-4; .text");
3737 #endif
3738 asm(".text; mov X"RX",%eax; jmp p0");
3739 case 4:
3740 #ifdef __clang__
3741 /* Bah! Clang! Doesn't want to redefine 'X' */
3742 asm(".text; mov $789,%eax; jmp p0");
3743 #else
3744 #ifndef _WIN32
3745 asm(".data; X=.; .int 789; Y=.; .int 999; .previous");
3746 #else
3747 asm(".data; X=.; .int 789; Y=.; .int 999; .text");
3748 #endif
3749 asm(".text; mov X"RX",%eax; X=Y; jmp p0");
3750 #endif
3751 case 0:
3752 asm(".text; p0=.; mov %%eax,%0;" : "=m"(r)); break;
3754 if (r == x)
3755 break;
3756 printf("asm_dot_test %d: %d\n", x, r);
3758 #endif
3761 void asm_pcrel_test(void)
3763 unsigned o1, o2;
3764 /* subtract text-section label from forward or other-section label */
3765 asm("1: mov $2f-1b,%%eax; mov %%eax,%0" : "=m"(o1));
3766 /* verify ... */
3767 asm("2: lea 2b"RX",%eax; lea 1b"RX",%ecx; sub %ecx,%eax");
3768 asm("mov %%eax,%0" : "=m"(o2));
3769 printf("%s : %x\n", __FUNCTION__, o1 - o2); /* should be zero */
3772 void asm_test(void)
3774 char buf[128];
3775 unsigned int val, val2;
3776 struct struct123 s1;
3777 struct struct1231 s2 = { (word)&s1 };
3778 /* Hide the outer base_func, but check later that the inline
3779 asm block gets the outer one. */
3780 int base_func = 42;
3781 void override_func3 (void);
3782 word asmret;
3783 #ifdef BOOL_ISOC99
3784 _Bool somebool;
3785 #endif
3786 register int regvar asm("%esi");
3788 // parse 0x1E-1 as 3 tokens in asm mode
3789 asm volatile ("mov $0x1E-1,%eax");
3791 /* test the no operand case */
3792 asm volatile ("xorl %eax, %eax");
3794 memcpy1(buf, "hello", 6);
3795 strncat1(buf, " worldXXXXX", 3);
3796 printf("%s\n", buf);
3798 memcpy2(buf, "hello", 6);
3799 strncat2(buf, " worldXXXXX", 3);
3800 printf("%s\n", buf);
3802 /* 'A' constraint test */
3803 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
3804 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
3806 s1.a = 42;
3807 s1.b = 43;
3808 printf("mconstraint: %d", mconstraint_test(&s2));
3809 printf(" %d %d\n", s1.a, s1.b);
3810 other_constraints_test();
3811 set = 0xff;
3812 sigdelset1(&set, 2);
3813 sigaddset1(&set, 16);
3814 /* NOTE: we test here if C labels are correctly restored after the
3815 asm statement */
3816 goto label1;
3817 label2:
3818 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
3819 printf("set=0x%x\n", set);
3820 val = 0x01020304;
3821 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
3822 #ifndef _WIN32
3823 #ifndef __APPLE__
3824 override_func1();
3825 override_func2();
3826 /* The base_func ref from the following inline asm should find
3827 the global one, not the local decl from this function. */
3828 asm volatile(".weak override_func3\n.set override_func3, base_func");
3829 override_func3();
3830 printf("asmstr: %s\n", get_asm_string());
3831 asm_local_label_diff();
3832 #endif
3833 #endif
3834 asm_local_statics();
3835 #ifndef __clang__
3836 /* clang can't deal with the type change */
3837 /* Check that we can also load structs of appropriate layout
3838 into registers. */
3839 asm volatile("" : "=r" (asmret) : "0"(s2));
3840 if (asmret != s2.addr)
3841 printf("asmstr: failed\n");
3842 #endif
3843 #ifdef BOOL_ISOC99
3844 /* Check that the typesize correctly sets the register size to
3845 8 bit. */
3846 asm volatile("cmp %1,%2; sete %0" : "=a"(somebool) : "r"(1), "r"(2));
3847 if (!somebool)
3848 printf("asmbool: failed\n");
3849 #endif
3850 val = 43;
3851 fancy_copy (&val, &val2);
3852 printf ("fancycpy(%d)=%d\n", val, val2);
3853 val = 44;
3854 fancy_copy2 (&val, &val2);
3855 printf ("fancycpy2(%d)=%d\n", val, val2);
3856 asm volatile ("mov $0x4243, %%esi" : "=r" (regvar));
3857 printf ("regvar=%x\n", regvar);
3858 test_high_clobbers();
3859 trace_console(8, 8);
3860 test_asm_dead_code();
3861 test_asm_call();
3862 asm_dot_test();
3863 asm_pcrel_test();
3864 return;
3865 label1:
3866 goto label2;
3869 #else
3871 void asm_test(void)
3875 #endif
3877 #define COMPAT_TYPE(type1, type2) \
3879 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
3880 __builtin_types_compatible_p (type1, type2));\
3883 int constant_p_var;
3885 int func(void);
3888 /* __builtin_clz and __builtin_ctz return random values for 0 */
3889 static void builtin_test_bits(unsigned long long x, int cnt[])
3891 #if GCC_MAJOR >= 4
3892 cnt[0] += __builtin_ffs(x);
3893 cnt[1] += __builtin_ffsl(x);
3894 cnt[2] += __builtin_ffsll(x);
3896 if ((unsigned int) x) cnt[3] += __builtin_clz(x);
3897 if ((unsigned long) x) cnt[4] += __builtin_clzl(x);
3898 if ((unsigned long long) x) cnt[5] += __builtin_clzll(x);
3900 if ((unsigned int) x) cnt[6] += __builtin_ctz(x);
3901 if ((unsigned long) x) cnt[7] += __builtin_ctzl(x);
3902 if ((unsigned long long) x) cnt[8] += __builtin_ctzll(x);
3904 #if GCC_MAJOR >= 6 && (CC_NAME != CC_clang || GCC_MAJOR >= 11)
3905 /* Apple clang 10 does not have __builtin_clrsb[l[l]] */
3906 cnt[9] += __builtin_clrsb(x);
3907 cnt[10] += __builtin_clrsbl(x);
3908 cnt[11] += __builtin_clrsbll(x);
3909 #endif
3911 cnt[12] += __builtin_popcount(x);
3912 cnt[13] += __builtin_popcountl(x);
3913 cnt[14] += __builtin_popcountll(x);
3915 cnt[15] += __builtin_parity(x);
3916 cnt[16] += __builtin_parityl(x);
3917 cnt[17] += __builtin_parityll(x);
3918 #endif
3921 void builtin_test(void)
3923 short s;
3924 int i;
3925 long long ll;
3926 #if GCC_MAJOR >= 3
3927 COMPAT_TYPE(int, int);
3928 COMPAT_TYPE(int, unsigned int);
3929 COMPAT_TYPE(int, char);
3930 COMPAT_TYPE(int, const int);
3931 COMPAT_TYPE(int, volatile int);
3932 COMPAT_TYPE(int *, int *);
3933 COMPAT_TYPE(int *, void *);
3934 COMPAT_TYPE(int *, const int *);
3935 COMPAT_TYPE(char *, unsigned char *);
3936 COMPAT_TYPE(char *, signed char *);
3937 COMPAT_TYPE(char *, char *);
3938 COMPAT_TYPE(char **, void *);
3939 #endif
3940 printf("res1 = %d\n", __builtin_constant_p(1));
3941 printf("res2 = %d\n", __builtin_constant_p(1 + 2));
3942 printf("res3 = %d\n", __builtin_constant_p(&constant_p_var));
3943 printf("res4 = %d\n", __builtin_constant_p(constant_p_var));
3944 printf("res5 = %d\n", __builtin_constant_p(100000 / constant_p_var));
3945 printf("res6 = %d\n", __builtin_constant_p(i && 1));
3946 printf("res7 = %d\n", __builtin_constant_p("hi"));
3947 printf("res8 = %d\n", __builtin_constant_p(func()));
3948 #ifndef __clang__
3949 printf("res10 = %d\n", __builtin_constant_p(i && 0));
3950 printf("res11 = %d\n", __builtin_constant_p(i * 0));
3951 printf("res12 = %d\n", __builtin_constant_p(i && 0 ? i : 34));
3952 printf("res13 = %d\n", __builtin_constant_p((1,7)));
3953 #else
3954 /* clang doesn't regard these as constant expression */
3955 printf("res10 = 1\n");
3956 printf("res11 = 1\n");
3957 printf("res12 = 1\n");
3958 printf("res13 = 0\n");
3959 #endif
3961 s = 1;
3962 ll = 2;
3963 i = __builtin_choose_expr (1 != 0, ll, s);
3964 printf("bce: %d\n", i);
3965 i = __builtin_choose_expr (1 != 1, ll, s);
3966 printf("bce: %d\n", i);
3967 i = sizeof (__builtin_choose_expr (1, ll, s));
3968 printf("bce: %d\n", i);
3969 i = sizeof (__builtin_choose_expr (0, ll, s));
3970 printf("bce: %d\n", i);
3972 //printf("bera: %p\n", __builtin_extract_return_addr((void*)43));
3975 int cnt[18];
3976 unsigned long long r = 0;
3978 memset(cnt, 0, sizeof(cnt));
3979 builtin_test_bits(0, cnt);
3980 builtin_test_bits(0xffffffffffffffffull, cnt);
3981 for (i = 0; i < 64; i++)
3982 builtin_test_bits(1ull << i, cnt);
3983 for (i = 0; i < 1000; i++) {
3984 r = 0x5851f42d4c957f2dull * r + 0x14057b7ef767814full;
3985 builtin_test_bits(r, cnt);
3987 for (i = 0; i < 18; i++)
3988 printf ("%d %d\n", i, cnt[i]);
3992 #if defined _WIN32 || (defined __APPLE__ && GCC_MAJOR >= 15)
3993 void weak_test(void) {}
3994 #else
3995 extern int __attribute__((weak)) weak_f1(void);
3996 extern int __attribute__((weak)) weak_f2(void);
3997 extern int weak_f3(void);
3998 extern int __attribute__((weak)) weak_v1;
3999 extern int __attribute__((weak)) weak_v2;
4000 extern int weak_v3;
4002 extern int (*weak_fpa)() __attribute__((weak));
4003 extern int __attribute__((weak)) (*weak_fpb)();
4004 extern __attribute__((weak)) int (*weak_fpc)();
4006 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
4007 extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
4008 extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
4009 extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
4010 extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
4011 extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
4013 #ifndef __clang__
4014 static const size_t dummy = 0;
4015 extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
4016 extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
4017 extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
4018 #endif
4020 int some_lib_func(void);
4021 int dummy_impl_of_slf(void) { return 444; }
4022 #ifndef __clang__
4023 int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
4024 #endif
4026 int weak_toolate() __attribute__((weak));
4027 int weak_toolate() { return 0; }
4029 void __attribute__((weak)) weak_test(void)
4031 printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
4032 printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
4033 printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
4034 printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
4035 printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
4036 printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
4038 printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
4039 printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
4040 printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
4042 printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
4043 printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
4044 printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
4045 printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
4046 printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
4047 printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
4048 #ifdef __clang__
4049 printf("some_lib_func=444\n");
4050 #else
4051 printf("some_lib_func=%d\n", &some_lib_func ? some_lib_func() : 0);
4052 #endif
4055 int __attribute__((weak)) weak_f2() { return 222; }
4056 int __attribute__((weak)) weak_f3() { return 333; }
4057 int __attribute__((weak)) weak_v2 = 222;
4058 int __attribute__((weak)) weak_v3 = 333;
4059 #endif
4061 void const_func(const int a)
4065 void const_warn_test(void)
4067 const_func(1);
4070 struct condstruct {
4071 int i;
4074 int getme (struct condstruct *s, int i)
4076 int i1 = (i == 0 ? 0 : s)->i;
4077 int i2 = (i == 0 ? s : 0)->i;
4078 int i3 = (i == 0 ? (void*)0 : s)->i;
4079 int i4 = (i == 0 ? s : (void*)0)->i;
4080 return i1 + i2 + i3 + i4;
4083 struct global_data
4085 int a[40];
4086 int *b[40];
4089 struct global_data global_data;
4091 int global_data_getstuff (int *, int);
4093 void global_data_callit (int i)
4095 *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
4098 int global_data_getstuff (int *p, int i)
4100 return *p + i;
4103 void global_data_test (void)
4105 global_data.a[0] = 42;
4106 global_data.b[0] = &global_data.a[0];
4107 global_data_callit (0);
4108 printf ("%d\n", global_data.a[0]);
4111 struct cmpcmpS
4113 unsigned char fill : 3;
4114 unsigned char b1 : 1;
4115 unsigned char b2 : 1;
4116 unsigned char fill2 : 3;
4119 int glob1, glob2, glob3;
4121 void compare_comparisons (struct cmpcmpS *s)
4123 if (s->b1 != (glob1 == glob2)
4124 || (s->b2 != (glob1 == glob3)))
4125 printf ("comparing comparisons broken\n");
4128 void cmp_comparison_test(void)
4130 struct cmpcmpS s;
4131 s.b1 = 1;
4132 glob1 = 42; glob2 = 42;
4133 s.b2 = 0;
4134 glob3 = 43;
4135 compare_comparisons (&s);
4138 int fcompare (double a, double b, int code)
4140 switch (code) {
4141 case 0: return a == b;
4142 case 1: return a != b;
4143 case 2: return a < b;
4144 case 3: return a >= b;
4145 case 4: return a > b;
4146 case 5: return a <= b;
4148 return 0;
4151 void math_cmp_test(void)
4153 double nan = 0.0/0.0;
4154 double one = 1.0;
4155 double two = 2.0;
4156 int comp = 0;
4157 int v;
4158 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
4160 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
4161 And it does this in various ways so that all code generation paths
4162 are checked (generating inverted tests, or non-inverted tests, or
4163 producing a 0/1 value without jumps (that's done in the fcompare
4164 function). */
4165 #define FCMP(a,b,op,iop,code) \
4166 if (fcompare (a,b,code)) \
4167 bug (a,b,op,iop,1); \
4168 if (a op b) \
4169 bug (a,b,op,iop,2); \
4170 if (a iop b) \
4172 else \
4173 bug (a,b,op,iop,3); \
4174 if ((a op b) || comp) \
4175 bug (a,b,op,iop,4); \
4176 if ((a iop b) || comp) \
4178 else \
4179 bug (a,b,op,iop,5); \
4180 if (v = !(a op b), !v) bug(a,b,op,iop,7);
4182 /* Equality tests. */
4183 FCMP(nan, nan, ==, !=, 0);
4184 FCMP(one, two, ==, !=, 0);
4185 FCMP(one, one, !=, ==, 1);
4186 /* Non-equality is a bit special. */
4187 if (!fcompare (nan, nan, 1))
4188 bug (nan, nan, !=, ==, 6);
4190 /* Relational tests on numbers. */
4191 FCMP(two, one, <, >=, 2);
4192 FCMP(one, two, >=, <, 3);
4193 FCMP(one, two, >, <=, 4);
4194 FCMP(two, one, <=, >, 5);
4196 /* Relational tests on NaNs. Note that the inverse op here is
4197 always !=, there's no operator in C that is equivalent to !(a < b),
4198 when NaNs are involved, same for the other relational ops. */
4199 FCMP(nan, nan, <, !=, 2);
4200 FCMP(nan, nan, >=, !=, 3);
4201 FCMP(nan, nan, >, !=, 4);
4202 FCMP(nan, nan, <=, !=, 5);
4205 double get100 () { return 100.0; }
4207 void callsave_test(void)
4209 #if defined __i386__ || defined __x86_64__ || defined __arm__
4210 int i, s; double *d; double t;
4211 s = sizeof (double);
4212 printf ("callsavetest: %d\n", s);
4213 d = alloca (sizeof(double));
4214 d[0] = 10.0;
4215 /* x86-64 had a bug were the next call to get100 would evict
4216 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
4217 in int type, not pointer type. When alloca returns a pointer
4218 with the high 32 bit set (which is likely on x86-64) the access
4219 generates a segfault. */
4220 i = d[0] > get100 ();
4221 printf ("%d\n", i);
4222 #endif
4226 void bfa3(ptrdiff_t str_offset)
4228 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
4230 void bfa2(ptrdiff_t str_offset)
4232 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
4233 bfa3(str_offset);
4235 void bfa1(ptrdiff_t str_offset)
4237 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
4238 bfa2(str_offset);
4241 void builtin_frame_address_test(void)
4243 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
4244 #ifndef __arm__
4245 char str[] = "__builtin_frame_address";
4246 char *fp0 = __builtin_frame_address(0);
4248 printf("str: %s\n", str);
4249 #ifndef __riscv // gcc dumps core. tcc, clang work
4250 bfa1(str-fp0);
4251 #endif
4252 #endif
4255 char via_volatile (char i)
4257 char volatile vi;
4258 vi = i;
4259 return vi;
4262 void volatile_test(void)
4264 if (via_volatile (42) != 42)
4265 printf (" broken\n");
4266 else
4267 printf (" ok\n");
4270 struct __attribute__((__packed__)) Spacked {
4271 char a;
4272 short b;
4273 int c;
4275 struct Spacked spacked;
4276 typedef struct __attribute__((__packed__)) {
4277 char a;
4278 short b;
4279 int c;
4280 } Spacked2;
4281 Spacked2 spacked2;
4282 typedef struct Spacked3_s {
4283 char a;
4284 short b;
4285 int c;
4286 } __attribute__((__packed__)) Spacked3;
4287 Spacked3 spacked3;
4288 struct gate_struct64 {
4289 unsigned short offset_low;
4290 unsigned short segment;
4291 unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1;
4292 unsigned short offset_middle;
4293 unsigned offset_high;
4294 unsigned zero1;
4295 } __attribute__((packed));
4296 typedef struct gate_struct64 gate_desc;
4297 gate_desc a_gate_desc;
4298 void attrib_test(void)
4300 #ifndef _WIN32
4301 printf("attr: %d %d %d %d\n", sizeof(struct Spacked),
4302 sizeof(spacked), sizeof(Spacked2), sizeof(spacked2));
4303 printf("attr: %d %d\n", sizeof(Spacked3), sizeof(spacked3));
4304 printf("attr: %d %d\n", sizeof(gate_desc), sizeof(a_gate_desc));
4305 #endif
4307 extern __attribute__((__unused__)) char * __attribute__((__unused__)) *
4308 strange_attrib_placement (void);
4310 void * __attribute__((__unused__)) get_void_ptr (void *a)
4312 return a;
4315 /* This part checks for a bug in TOK_GET (used for inline expansion),
4316 where the large long long constant left the the high bits set for
4317 the integer constant token. */
4318 static inline
4319 int __get_order(unsigned long long size)
4321 int order;
4322 size -= 0xffff880000000000ULL; // this const left high bits set in the token
4324 struct S { int i : 1; } s; // constructed for this '1'
4326 order = size;
4327 return order;
4330 /* This just forces the above inline function to be actually emitted. */
4331 int force_get_order(unsigned long s)
4333 return __get_order(s);
4336 #define pv(m) printf(sizeof (s->m + 0) == 8 ? "%016llx\n" : "%02x\n", s->m)
4338 /* Test failed when using bounds checking */
4339 void bounds_check1_test (void)
4341 struct s {
4342 int x;
4343 long long y;
4344 } _s, *s = &_s;
4345 s->x = 10;
4346 s->y = 20;
4347 pv(x);
4348 pv(y);
4351 /* This failed on arm64/riscv64 */
4352 void map_add(int a, int b, int c, int d, int e, int f, int g, int h, int i)
4354 printf ("%d %d %d %d %d %d %d %d %d\n", a, b, c, d, e, f, g, h, i);
4357 void func_arg_test(void)
4359 int a = 0;
4360 int b = 1;
4361 map_add(0, 1, 2, 3, 4, 5, 6, 7, a && b);
4364 /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
4365 #define CORRECT_CR_HANDLING
4367 /* deprecated and no longer supported in gcc 3.3 */
4368 /* no longer supported by default in TinyCC */
4369 #ifdef __TINYC__
4370 /* # define ACCEPT_LF_IN_STRINGS */
4371 #endif
4373 #define tcc_test()
4375 /* keep this as the last test because GCC messes up line-numbers
4376 with the ^L^K^M characters below */
4377 void whitespace_test(void)
4379 char *str;
4380 int tcc_test = 1;
4382 \f\v #if 1
4383 pri\
4384 ntf("whitspace:\n");\f\v
4385 #endif
4386 pf("N=%d\n", 2);
4388 #ifdef CORRECT_CR_HANDLING
4389 pri\
4390 ntf("aaa=%d\n", 3);
4391 #endif
4393 pri\
4395 ntf("min=%d\n", 4);
4397 #ifdef ACCEPT_LF_IN_STRINGS
4398 printf("len1=%d\n", strlen("
4399 "));
4400 #ifdef CORRECT_CR_HANDLING
4401 str = "
4403 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
4404 #endif
4405 printf("len1=%d\n", strlen(" a
4406 "));
4407 #else
4408 printf("len1=1\nlen1=1 str[0]=10\nlen1=3\n");
4409 #endif /* ACCEPT_LF_IN_STRINGS */
4411 #ifdef __LINE__
4412 printf("__LINE__ defined\n");
4413 #endif
4415 #if 0
4416 /* wrong with GCC */
4417 printf("__LINE__=%d __FILE__=%s\n", __LINE__, __FILE__);
4418 #line 1111
4419 printf("__LINE__=%d __FILE__=%s\n", __LINE__, __FILE__);
4420 #line 2222 "test"
4421 printf("__LINE__=%d __FILE__=%s\n", __LINE__, __FILE__);
4422 #endif
4424 printf("\\
4425 "12\\
4426 063\\
4427 n 456\"\n");
4429 printf ("%d\n",
4430 #if 1
4431 tcc_test
4432 #endif
4437 #define RUN(test) puts("---- " #test " ----"), test(), puts("")
4439 int main(int argc, char **argv)
4441 RUN(whitespace_test);
4442 RUN(macro_test);
4443 RUN(recursive_macro_test);
4444 RUN(string_test);
4445 RUN(expr_test);
4446 RUN(scope_test);
4447 RUN(scope2_test);
4448 RUN(forward_test);
4449 RUN(funcptr_test);
4450 RUN(if_test);
4451 RUN(loop_test);
4452 RUN(switch_test);
4453 RUN(goto_test);
4454 RUN(enum_test);
4455 RUN(typedef_test);
4456 RUN(struct_test);
4457 RUN(array_test);
4458 RUN(expr_ptr_test);
4459 RUN(bool_test);
4460 RUN(optimize_out_test);
4461 RUN(expr2_test);
4462 RUN(constant_expr_test);
4463 RUN(expr_cmp_test);
4464 RUN(char_short_test);
4465 RUN(init_test);
4466 RUN(compound_literal_test);
4467 RUN(kr_test);
4468 RUN(struct_assign_test);
4469 RUN(cast_test);
4470 RUN(bitfield_test);
4471 RUN(c99_bool_test);
4472 RUN(float_test);
4473 RUN(longlong_test);
4474 RUN(manyarg_test);
4475 RUN(stdarg_test);
4476 RUN(relocation_test);
4477 RUN(old_style_function_test);
4478 RUN(alloca_test);
4479 RUN(c99_vla_test);
4480 RUN(sizeof_test);
4481 RUN(typeof_test);
4482 RUN(statement_expr_test);
4483 RUN(local_label_test);
4484 RUN(asm_test);
4485 RUN(builtin_test);
4486 RUN(weak_test);
4487 RUN(global_data_test);
4488 RUN(cmp_comparison_test);
4489 RUN(math_cmp_test);
4490 RUN(callsave_test);
4491 RUN(builtin_frame_address_test);
4492 RUN(volatile_test);
4493 RUN(attrib_test);
4494 RUN(bounds_check1_test);
4495 RUN(func_arg_test);
4497 return 0;