Remove a bit-field TODO
[tinycc.git] / tests / tcctest.c
blobdc6a06018b9b59d31e89e09fc52fdcbd0d972868
1 /*
2 * TCC auto test program
3 */
4 #include "config.h"
6 #if GCC_MAJOR >= 3
8 /* Unfortunately, gcc version < 3 does not handle that! */
9 #define ALL_ISOC99
11 /* only gcc 3 handles _Bool correctly */
12 #define BOOL_ISOC99
14 /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
15 #define CORRECT_CR_HANDLING
17 #endif
19 #if defined(_WIN32)
20 #define LONG_LONG_FORMAT "%lld"
21 #define ULONG_LONG_FORMAT "%llu"
22 #else
23 #define LONG_LONG_FORMAT "%Ld"
24 #define ULONG_LONG_FORMAT "%Lu"
25 #endif
27 // MinGW has 80-bit rather than 64-bit long double which isn't compatible with TCC or MSVC
28 #if defined(_WIN32) && defined(__GNUC__)
29 #define LONG_DOUBLE double
30 #define LONG_DOUBLE_LITERAL(x) x
31 #else
32 #define LONG_DOUBLE long double
33 #define LONG_DOUBLE_LITERAL(x) x ## L
34 #endif
36 /* deprecated and no longer supported in gcc 3.3 */
37 //#define ACCEPT_CR_IN_STRINGS
39 /* __VA_ARGS__ and __func__ support */
40 #define C99_MACROS
42 /* test various include syntaxes */
44 #define TCCLIB_INC <tcclib.h>
45 #define TCCLIB_INC1 <tcclib
46 #define TCCLIB_INC2 h>
47 #define TCCLIB_INC3 "tcclib.h"
49 #include TCCLIB_INC
51 #include TCCLIB_INC1.TCCLIB_INC2
53 #include TCCLIB_INC1.h>
55 #include TCCLIB_INC3
57 #include <tcclib.h>
59 #include "tcclib.h"
61 #include "tcctest.h"
63 /* Test two more ways to include a file named like a pp-number */
64 #define INC(name) <tests/name.h>
65 #define funnyname 42test.h
66 #define incdir tests/
67 #define incname < incdir funnyname >
68 #define __stringify(x) #x
69 #define stringify(x) __stringify(x)
70 #include INC(42test)
71 #include incname
72 #include stringify(funnyname)
74 void intdiv_test();
75 void string_test();
76 void expr_test();
77 void macro_test();
78 void recursive_macro_test();
79 void scope_test();
80 void forward_test();
81 void funcptr_test();
82 void loop_test();
83 void switch_test();
84 void goto_test();
85 void enum_test();
86 void typedef_test();
87 void struct_test();
88 void array_test();
89 void expr_ptr_test();
90 void bool_test();
91 void optimize_out();
92 void expr2_test();
93 void constant_expr_test();
94 void expr_cmp_test();
95 void char_short_test();
96 void init_test(void);
97 void compound_literal_test(void);
98 int kr_test();
99 void struct_assign_test(void);
100 void cast_test(void);
101 void bitfield_test(void);
102 void c99_bool_test(void);
103 void float_test(void);
104 void longlong_test(void);
105 void manyarg_test(void);
106 void stdarg_test(void);
107 void whitespace_test(void);
108 void relocation_test(void);
109 void old_style_function(void);
110 void alloca_test(void);
111 void c99_vla_test(int size1, int size2);
112 void sizeof_test(void);
113 void typeof_test(void);
114 void local_label_test(void);
115 void statement_expr_test(void);
116 void asm_test(void);
117 void builtin_test(void);
118 void weak_test(void);
119 void global_data_test(void);
120 void cmp_comparison_test(void);
121 void math_cmp_test(void);
122 void callsave_test(void);
123 void builtin_frame_address_test(void);
124 void attrib_test(void);
126 int fib(int n);
127 void num(int n);
128 void forward_ref(void);
129 int isid(int c);
131 /* Line joining happens before tokenization, so the following
132 must be parsed as ellipsis. */
133 void funny_line_continuation (int, ..\
134 . );
136 char via_volatile (char);
138 #define A 2
139 #define N 1234 + A
140 #define pf printf
141 #define M1(a, b) (a) + (b)
143 #define str\
144 (s) # s
145 #define glue(a, b) a ## b
146 #define xglue(a, b) glue(a, b)
147 #define HIGHLOW "hello"
148 #define LOW LOW ", world"
150 static int onetwothree = 123;
151 #define onetwothree4 onetwothree
152 #define onetwothree xglue(onetwothree,4)
154 #define min(a, b) ((a) < (b) ? (a) : (b))
156 #ifdef C99_MACROS
157 #define dprintf(level,...) printf(__VA_ARGS__)
158 #endif
160 /* gcc vararg macros */
161 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
163 #define MACRO_NOARGS()
165 #define AAA 3
166 #undef AAA
167 #define AAA 4
169 #if 1
170 #define B3 1
171 #elif 1
172 #define B3 2
173 #elif 0
174 #define B3 3
175 #else
176 #define B3 4
177 #endif
179 #define __INT64_C(c) c ## LL
180 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
182 int qq(int x)
184 return x + 40;
186 #define qq(x) x
188 #define spin_lock(lock) do { } while (0)
189 #define wq_spin_lock spin_lock
190 #define TEST2() wq_spin_lock(a)
192 #define UINT_MAX ((unsigned) -1)
194 void intdiv_test(void)
196 printf("18/21=%u\n", 18/21);
197 printf("18%%21=%u\n", 18%21);
198 printf("41/21=%u\n", 41/21);
199 printf("41%%21=%u\n", 41%21);
200 printf("42/21=%u\n", 42/21);
201 printf("42%%21=%u\n", 42%21);
202 printf("43/21=%u\n", 43/21);
203 printf("43%%21=%u\n", 43%21);
204 printf("126/21=%u\n", 126/21);
205 printf("126%%21=%u\n", 126%21);
206 printf("131/21=%u\n", 131/21);
207 printf("131%%21=%u\n", 131%21);
208 printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX/2+3)/2);
209 printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX/2+3)%2);
211 printf("18/-21=%u\n", 18/-21);
212 printf("18%%-21=%u\n", 18%-21);
213 printf("41/-21=%u\n", 41/-21);
214 printf("41%%-21=%u\n", 41%-21);
215 printf("42/-21=%u\n", 42/-21);
216 printf("42%%-21=%u\n", 42%-21);
217 printf("43/-21=%u\n", 43/-21);
218 printf("43%%-21=%u\n", 43%-21);
219 printf("126/-21=%u\n", 126/-21);
220 printf("126%%-21=%u\n", 126%-21);
221 printf("131/-21=%u\n", 131/-21);
222 printf("131%%-21=%u\n", 131%-21);
223 printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX/2+3)/-2);
224 printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX/2+3)%-2);
226 printf("-18/21=%u\n", -18/21);
227 printf("-18%%21=%u\n", -18%21);
228 printf("-41/21=%u\n", -41/21);
229 printf("-41%%21=%u\n", -41%21);
230 printf("-42/21=%u\n", -42/21);
231 printf("-42%%21=%u\n", -42%21);
232 printf("-43/21=%u\n", -43/21);
233 printf("-43%%21=%u\n", -43%21);
234 printf("-126/21=%u\n", -126/21);
235 printf("-126%%21=%u\n", -126%21);
236 printf("-131/21=%u\n", -131/21);
237 printf("-131%%21=%u\n", -131%21);
238 printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX/2+3))/2);
239 printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX/2+3))%2);
241 printf("-18/-21=%u\n", -18/-21);
242 printf("-18%%-21=%u\n", -18%-21);
243 printf("-41/-21=%u\n", -41/-21);
244 printf("-41%%-21=%u\n", -41%-21);
245 printf("-42/-21=%u\n", -42/-21);
246 printf("-42%%-21=%u\n", -42%-21);
247 printf("-43/-21=%u\n", -43/-21);
248 printf("-43%%-21=%u\n", -43%-21);
249 printf("-126/-21=%u\n", -126/-21);
250 printf("-126%%-21=%u\n", -126%-21);
251 printf("-131/-21=%u\n", -131/-21);
252 printf("-131%%-21=%u\n", -131%-21);
253 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX/2+3))/-2);
254 printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX/2+3))%-2);
257 void macro_test(void)
259 printf("macro:\n");\f\v
260 pf("N=%d\n", N);
261 printf("aaa=%d\n", AAA);
263 printf("min=%d\n", min(1, min(2, -1)));
265 printf("s1=%s\n", glue(HIGH, LOW));
266 printf("s2=%s\n", xglue(HIGH, LOW));
267 printf("s3=%s\n", str("c"));
268 printf("s4=%s\n", str(a1));
269 printf("B3=%d\n", B3);
271 printf("onetwothree=%d\n", onetwothree);
273 #ifdef A
274 printf("A defined\n");
275 #endif
276 #ifdef B
277 printf("B defined\n");
278 #endif
279 #ifdef A
280 printf("A defined\n");
281 #else
282 printf("A not defined\n");
283 #endif
284 #ifdef B
285 printf("B defined\n");
286 #else
287 printf("B not defined\n");
288 #endif
290 #ifdef A
291 printf("A defined\n");
292 #ifdef B
293 printf("B1 defined\n");
294 #else
295 printf("B1 not defined\n");
296 #endif
297 #else
298 printf("A not defined\n");
299 #ifdef B
300 printf("B2 defined\n");
301 #else
302 printf("B2 not defined\n");
303 #endif
304 #endif
306 #if 1+1
307 printf("test true1\n");
308 #endif
309 #if 0
310 printf("test true2\n");
311 #endif
312 #if 1-1
313 printf("test true3\n");
314 #endif
315 #if defined(A)
316 printf("test trueA\n");
317 #endif
318 #if defined(B)
319 printf("test trueB\n");
320 #endif
322 #if 0
323 printf("test 0\n");
324 #elif 0
325 printf("test 1\n");
326 #elif 2
327 printf("test 2\n");
328 #else
329 printf("test 3\n");
330 #endif
332 MACRO_NOARGS();
334 #ifdef __LINE__
335 printf("__LINE__ defined\n");
336 #endif
338 printf("__LINE__=%d __FILE__=%s\n",
339 __LINE__, __FILE__);
340 #if 0
341 #line 200
342 printf("__LINE__=%d __FILE__=%s\n",
343 __LINE__, __FILE__);
344 #line 203 "test"
345 printf("__LINE__=%d __FILE__=%s\n",
346 __LINE__, __FILE__);
347 #line 227 "tcctest.c"
348 #endif
350 /* not strictly preprocessor, but we test it there */
351 #ifdef C99_MACROS
352 printf("__func__ = %s\n", __func__);
353 dprintf(1, "vaarg=%d\n", 1);
354 #endif
355 dprintf1(1, "vaarg1\n");
356 dprintf1(1, "vaarg1=%d\n", 2);
357 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
359 /* gcc extension */
360 printf("func='%s'\n", __FUNCTION__);
362 /* complicated macros in glibc */
363 printf("INT64_MIN=" LONG_LONG_FORMAT "\n", INT64_MIN);
365 int a;
366 a = 1;
367 glue(a+, +);
368 printf("a=%d\n", a);
369 glue(a <, <= 2);
370 printf("a=%d\n", a);
373 /* macro function with argument outside the macro string */
374 #define MF_s MF_hello
375 #define MF_hello(msg) printf("%s\n",msg)
377 #define MF_t printf("tralala\n"); MF_hello
379 MF_s("hi");
380 MF_t("hi");
382 /* test macro substituion inside args (should not eat stream) */
383 printf("qq=%d\n", qq(qq)(2));
385 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
386 null argument without a space. gcc 3.2 fixes that. */
388 #define qq1(x) 1
389 printf("qq1=%d\n", qq1( ));
391 /* comment with stray handling *\
393 /* this is a valid *\/ comment */
394 /* this is a valid comment *\*/
395 // this is a valid\
396 comment
398 /* test function macro substitution when the function name is
399 substituted */
400 TEST2();
402 /* And again when the name and parenthes are separated by a
403 comment. */
404 TEST2 /* the comment */ ();
406 printf("%s\n", get_basefile_from_header());
407 printf("%s\n", __BASE_FILE__);
408 printf("%s\n", get_file_from_header());
409 printf("%s\n", __FILE__);
411 /* Check that funnily named include was in fact included */
412 have_included_42test_h = 1;
413 have_included_42test_h_second = 1;
414 have_included_42test_h_third = 1;
418 static void print_num(char *fn, int line, int num) {
419 printf("fn %s, line %d, num %d\n", fn, line, num);
422 void recursive_macro_test(void)
425 #define ELF32_ST_TYPE(val) ((val) & 0xf)
426 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
427 #define STB_WEAK 2 /* Weak symbol */
428 #define ELFW(type) ELF##32##_##type
429 printf("%d\n", ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(123)));
431 #define WRAP(x) x
433 #define print_num(x) print_num(__FILE__,__LINE__,x)
434 print_num(123);
435 WRAP(print_num(123));
436 WRAP(WRAP(print_num(123)));
438 static struct recursive_macro { int rm_field; } G;
439 #define rm_field (G.rm_field)
440 printf("rm_field = %d\n", rm_field);
441 printf("rm_field = %d\n", WRAP(rm_field));
442 WRAP((printf("rm_field = %d %d\n", rm_field, WRAP(rm_field))));
445 int op(a,b)
447 return a / b;
450 int ret(a)
452 if (a == 2)
453 return 1;
454 if (a == 3)
455 return 2;
456 return 0;
459 void ps(const char *s)
461 int c;
462 while (1) {
463 c = *s;
464 if (c == 0)
465 break;
466 printf("%c", c);
467 s++;
471 const char foo1_string[] = "\
472 bar\n\
473 test\14\
476 void string_test()
478 unsigned int b;
479 printf("string:\n");
480 printf("\141\1423\143\n");/* dezdez test */
481 printf("\x41\x42\x43\x3a\n");
482 printf("c=%c\n", 'r');
483 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
484 printf("foo1_string='%s'\n", foo1_string);
485 #if 0
486 printf("wstring=%S\n", L"abc");
487 printf("wstring=%S\n", L"abc" L"def" "ghi");
488 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
489 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
490 #endif
491 ps("test\n");
492 b = 32;
493 while ((b = b + 1) < 96) {
494 printf("%c", b);
496 printf("\n");
497 printf("fib=%d\n", fib(33));
498 b = 262144;
499 while (b != 0x80000000) {
500 num(b);
501 b = b * 2;
505 void loop_test()
507 int i;
508 i = 0;
509 while (i < 10)
510 printf("%d", i++);
511 printf("\n");
512 for(i = 0; i < 10;i++)
513 printf("%d", i);
514 printf("\n");
515 i = 0;
516 do {
517 printf("%d", i++);
518 } while (i < 10);
519 printf("\n");
521 char count = 123;
522 /* c99 for loop init test */
523 for (size_t count = 1; count < 3; count++)
524 printf("count=%d\n", count);
525 printf("count = %d\n", count);
527 /* break/continue tests */
528 i = 0;
529 while (1) {
530 if (i == 6)
531 break;
532 i++;
533 if (i == 3)
534 continue;
535 printf("%d", i);
537 printf("\n");
539 /* break/continue tests */
540 i = 0;
541 do {
542 if (i == 6)
543 break;
544 i++;
545 if (i == 3)
546 continue;
547 printf("%d", i);
548 } while(1);
549 printf("\n");
551 for(i = 0;i < 10;i++) {
552 if (i == 3)
553 continue;
554 printf("%d", i);
556 printf("\n");
559 typedef int typedef_and_label;
561 void goto_test()
563 int i;
564 static void *label_table[3] = { &&label1, &&label2, &&label3 };
566 printf("goto:\n");
567 i = 0;
568 /* This needs to parse as label, not as start of decl. */
569 typedef_and_label:
570 s_loop:
571 if (i >= 10)
572 goto s_end;
573 printf("%d", i);
574 i++;
575 goto s_loop;
576 s_end:
577 printf("\n");
579 /* we also test computed gotos (GCC extension) */
580 for(i=0;i<3;i++) {
581 goto *label_table[i];
582 label1:
583 printf("label1\n");
584 goto next;
585 label2:
586 printf("label2\n");
587 goto next;
588 label3:
589 printf("label3\n");
590 next: ;
594 enum {
596 E1 = 2,
597 E2 = 4,
602 enum test {
603 E5 = 1000,
606 struct S_enum {
607 enum {E6 = 42, E7, E8} e:8;
610 enum ELong {
611 /* This is either 0 on L32 machines, or a large number
612 on L64 machines. We should be able to store this. */
613 EL_large = ((unsigned long)0xf000 << 31) << 1,
616 enum { BIASU = -1U<<31 };
617 enum { BIASS = -1 << 31 };
619 static int getint(int i)
621 if (i)
622 return 0;
623 else
624 return (int)(-1U << 31);
627 void enum_test()
629 enum test b1;
630 /* The following should give no warning */
631 unsigned *p = &b1;
632 struct S_enum s = {E7};
633 printf("enum: %d\n", s.e);
634 printf("enum:\n%d %d %d %d %d %d\n",
635 E0, E1, E2, E3, E4, E5);
636 b1 = 1;
637 printf("b1=%d\n", b1);
638 printf("enum large: %ld\n", EL_large);
640 if (getint(0) == BIASU)
641 printf("enum unsigned: ok\n");
642 else
643 printf("enum unsigned: wrong\n");
644 if (getint(0) == BIASS)
645 printf("enum unsigned: ok\n");
646 else
647 printf("enum unsigned: wrong\n");
650 typedef int *my_ptr;
652 typedef int mytype1;
653 typedef int mytype2;
655 void typedef_test()
657 my_ptr a;
658 mytype1 mytype2;
659 int b;
661 a = &b;
662 *a = 1234;
663 printf("typedef:\n");
664 printf("a=%d\n", *a);
665 mytype2 = 2;
666 printf("mytype2=%d\n", mytype2);
669 void forward_test()
671 printf("forward:\n");
672 forward_ref();
673 forward_ref();
677 void forward_ref(void)
679 printf("forward ok\n");
682 typedef struct struct1 {
683 int f1;
684 int f2, f3;
685 union union1 {
686 int v1;
687 int v2;
688 } u;
689 char str[3];
690 } struct1;
692 struct struct2 {
693 int a;
694 char b;
697 union union2 {
698 int w1;
699 int w2;
702 struct struct1 st1, st2;
704 struct empty_mem {
705 /* nothing */ ;
706 int x;
709 int main(int argc, char **argv)
711 string_test();
712 expr_test();
713 macro_test();
714 recursive_macro_test();
715 scope_test();
716 forward_test();
717 funcptr_test();
718 loop_test();
719 switch_test();
720 goto_test();
721 enum_test();
722 typedef_test();
723 struct_test();
724 array_test();
725 expr_ptr_test();
726 bool_test();
727 optimize_out();
728 expr2_test();
729 constant_expr_test();
730 expr_cmp_test();
731 char_short_test();
732 init_test();
733 compound_literal_test();
734 kr_test();
735 struct_assign_test();
736 cast_test();
737 bitfield_test();
738 c99_bool_test();
739 float_test();
740 longlong_test();
741 manyarg_test();
742 stdarg_test();
743 whitespace_test();
744 relocation_test();
745 old_style_function();
746 alloca_test();
747 c99_vla_test(5, 2);
748 sizeof_test();
749 typeof_test();
750 statement_expr_test();
751 local_label_test();
752 asm_test();
753 builtin_test();
754 #ifndef _WIN32
755 weak_test();
756 #endif
757 global_data_test();
758 cmp_comparison_test();
759 math_cmp_test();
760 callsave_test();
761 builtin_frame_address_test();
762 intdiv_test();
763 if (via_volatile (42) != 42)
764 printf ("via_volatile broken\n");
765 attrib_test();
766 return 0;
769 int tab[3];
770 int tab2[3][2];
772 int g;
774 void f1(g)
776 printf("g1=%d\n", g);
779 void scope_test()
781 printf("scope:\n");
782 g = 2;
783 f1(1);
784 printf("g2=%d\n", g);
786 int g;
787 g = 3;
788 printf("g3=%d\n", g);
790 int g;
791 g = 4;
792 printf("g4=%d\n", g);
795 printf("g5=%d\n", g);
798 void array_test()
800 int i, j, a[4];
802 printf("array:\n");
803 printf("sizeof(a) = %d\n", sizeof(a));
804 printf("sizeof(\"a\") = %d\n", sizeof("a"));
805 #ifdef C99_MACROS
806 printf("sizeof(__func__) = %d\n", sizeof(__func__));
807 #endif
808 printf("sizeof tab %d\n", sizeof(tab));
809 printf("sizeof tab2 %d\n", sizeof tab2);
810 tab[0] = 1;
811 tab[1] = 2;
812 tab[2] = 3;
813 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
814 for(i=0;i<3;i++)
815 for(j=0;j<2;j++)
816 tab2[i][j] = 10 * i + j;
817 for(i=0;i<3*2;i++) {
818 printf(" %3d", ((int *)tab2)[i]);
820 printf("\n");
821 printf("sizeof(size_t)=%d\n", sizeof(size_t));
822 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
825 void expr_test()
827 int a, b;
828 a = 0;
829 printf("%d\n", a += 1);
830 printf("%d\n", a -= 2);
831 printf("%d\n", a *= 31232132);
832 printf("%d\n", a /= 4);
833 printf("%d\n", a %= 20);
834 printf("%d\n", a &= 6);
835 printf("%d\n", a ^= 7);
836 printf("%d\n", a |= 8);
837 printf("%d\n", a >>= 3);
838 printf("%d\n", a <<= 4);
840 a = 22321;
841 b = -22321;
842 printf("%d\n", a + 1);
843 printf("%d\n", a - 2);
844 printf("%d\n", a * 312);
845 printf("%d\n", a / 4);
846 printf("%d\n", b / 4);
847 printf("%d\n", (unsigned)b / 4);
848 printf("%d\n", a % 20);
849 printf("%d\n", b % 20);
850 printf("%d\n", (unsigned)b % 20);
851 printf("%d\n", a & 6);
852 printf("%d\n", a ^ 7);
853 printf("%d\n", a | 8);
854 printf("%d\n", a >> 3);
855 printf("%d\n", b >> 3);
856 printf("%d\n", (unsigned)b >> 3);
857 printf("%d\n", a << 4);
858 printf("%d\n", ~a);
859 printf("%d\n", -a);
860 printf("%d\n", +a);
862 printf("%d\n", 12 + 1);
863 printf("%d\n", 12 - 2);
864 printf("%d\n", 12 * 312);
865 printf("%d\n", 12 / 4);
866 printf("%d\n", 12 % 20);
867 printf("%d\n", 12 & 6);
868 printf("%d\n", 12 ^ 7);
869 printf("%d\n", 12 | 8);
870 printf("%d\n", 12 >> 2);
871 printf("%d\n", 12 << 4);
872 printf("%d\n", ~12);
873 printf("%d\n", -12);
874 printf("%d\n", +12);
875 printf("%d %d %d %d\n",
876 isid('a'),
877 isid('g'),
878 isid('T'),
879 isid('('));
882 int isid(int c)
884 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
887 /**********************/
889 int vstack[10], *vstack_ptr;
891 void vpush(int vt, int vc)
893 *vstack_ptr++ = vt;
894 *vstack_ptr++ = vc;
897 void vpop(int *ft, int *fc)
899 *fc = *--vstack_ptr;
900 *ft = *--vstack_ptr;
903 void expr2_test()
905 int a, b;
907 printf("expr2:\n");
908 vstack_ptr = vstack;
909 vpush(1432432, 2);
910 vstack_ptr[-2] &= ~0xffffff80;
911 vpop(&a, &b);
912 printf("res= %d %d\n", a, b);
915 void constant_expr_test()
917 int a;
918 printf("constant_expr:\n");
919 a = 3;
920 printf("%d\n", a * 16);
921 printf("%d\n", a * 1);
922 printf("%d\n", a + 0);
925 int tab4[10];
927 void expr_ptr_test()
929 int *p, *q;
930 int i = -1;
932 printf("expr_ptr:\n");
933 p = tab4;
934 q = tab4 + 10;
935 printf("diff=%d\n", q - p);
936 p++;
937 printf("inc=%d\n", p - tab4);
938 p--;
939 printf("dec=%d\n", p - tab4);
940 ++p;
941 printf("inc=%d\n", p - tab4);
942 --p;
943 printf("dec=%d\n", p - tab4);
944 printf("add=%d\n", p + 3 - tab4);
945 printf("add=%d\n", 3 + p - tab4);
947 /* check if 64bit support is ok */
948 q = p = 0;
949 q += i;
950 printf("%p %p %ld\n", q, p, p-q);
951 printf("%d %d %d %d %d %d\n",
952 p == q, p != q, p < q, p <= q, p >= q, p > q);
953 i = 0xf0000000;
954 p += i;
955 printf("%p %p %ld\n", q, p, p-q);
956 printf("%d %d %d %d %d %d\n",
957 p == q, p != q, p < q, p <= q, p >= q, p > q);
958 p = (int *)((char *)p + 0xf0000000);
959 printf("%p %p %ld\n", q, p, p-q);
960 printf("%d %d %d %d %d %d\n",
961 p == q, p != q, p < q, p <= q, p >= q, p > q);
962 p += 0xf0000000;
963 printf("%p %p %ld\n", q, p, p-q);
964 printf("%d %d %d %d %d %d\n",
965 p == q, p != q, p < q, p <= q, p >= q, p > q);
967 struct size12 {
968 int i, j, k;
970 struct size12 s[2], *sp = s;
971 int i, j;
972 sp->i = 42;
973 sp++;
974 j = -1;
975 printf("%d\n", sp[j].i);
977 #ifdef __LP64__
978 i = 1;
979 p = (int*)0x100000000UL + i;
980 i = ((long)p) >> 32;
981 printf("largeptr: %p %d\n", p, i);
982 #endif
985 void expr_cmp_test()
987 int a, b;
988 printf("constant_expr:\n");
989 a = -1;
990 b = 1;
991 printf("%d\n", a == a);
992 printf("%d\n", a != a);
994 printf("%d\n", a < b);
995 printf("%d\n", a <= b);
996 printf("%d\n", a <= a);
997 printf("%d\n", b >= a);
998 printf("%d\n", a >= a);
999 printf("%d\n", b > a);
1001 printf("%d\n", (unsigned)a < b);
1002 printf("%d\n", (unsigned)a <= b);
1003 printf("%d\n", (unsigned)a <= a);
1004 printf("%d\n", (unsigned)b >= a);
1005 printf("%d\n", (unsigned)a >= a);
1006 printf("%d\n", (unsigned)b > a);
1009 struct empty {
1012 struct aligntest1 {
1013 char a[10];
1016 struct aligntest2 {
1017 int a;
1018 char b[10];
1021 struct aligntest3 {
1022 double a, b;
1025 struct aligntest4 {
1026 double a[0];
1029 struct __attribute__((aligned(16))) aligntest5
1031 int i;
1033 struct aligntest6
1035 int i;
1036 } __attribute__((aligned(16)));
1037 struct aligntest7
1039 int i;
1041 struct aligntest5 altest5[2];
1042 struct aligntest6 altest6[2];
1043 int pad1;
1044 /* altest7 is correctly aligned to 16 bytes also with TCC,
1045 but __alignof__ returns the wrong result (4) because we
1046 can't store the alignment yet when specified on symbols
1047 directly (it's stored in the type so we'd need to make
1048 a copy of it).
1049 struct aligntest7 altest7[2] __attribute__((aligned(16)));*/
1051 struct aligntest8
1053 int i;
1054 } __attribute__((aligned(4096)));
1056 struct Large {
1057 unsigned long flags;
1058 union {
1059 void *u1;
1060 int *u2;
1063 struct {
1064 union {
1065 unsigned long index;
1066 void *freelist;
1068 union {
1069 unsigned long counters;
1070 struct {
1071 int bla;
1076 union {
1077 struct {
1078 long u3;
1079 long u4;
1081 void *u5;
1082 struct {
1083 unsigned long compound_head;
1084 unsigned int compound_dtor;
1085 unsigned int compound_order;
1088 } __attribute__((aligned(2 * sizeof(long))));
1090 typedef unsigned long long __attribute__((aligned(4))) unaligned_u64;
1092 struct aligntest9 {
1093 unsigned int buf_nr;
1094 unaligned_u64 start_lba;
1097 struct aligntest10 {
1098 unsigned int buf_nr;
1099 unsigned long long start_lba;
1102 void struct_test()
1104 struct1 *s;
1105 union union2 u;
1106 struct Large ls;
1108 printf("struct:\n");
1109 printf("sizes: %d %d %d %d\n",
1110 sizeof(struct struct1),
1111 sizeof(struct struct2),
1112 sizeof(union union1),
1113 sizeof(union union2));
1114 printf("offsets: %d\n", (int)((char*)&st1.u.v1 - (char*)&st1));
1115 st1.f1 = 1;
1116 st1.f2 = 2;
1117 st1.f3 = 3;
1118 printf("st1: %d %d %d\n",
1119 st1.f1, st1.f2, st1.f3);
1120 st1.u.v1 = 1;
1121 st1.u.v2 = 2;
1122 printf("union1: %d\n", st1.u.v1);
1123 u.w1 = 1;
1124 u.w2 = 2;
1125 printf("union2: %d\n", u.w1);
1126 s = &st2;
1127 s->f1 = 3;
1128 s->f2 = 2;
1129 s->f3 = 1;
1130 printf("st2: %d %d %d\n",
1131 s->f1, s->f2, s->f3);
1132 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
1134 /* align / size tests */
1135 printf("aligntest1 sizeof=%d alignof=%d\n",
1136 sizeof(struct aligntest1), __alignof__(struct aligntest1));
1137 printf("aligntest2 sizeof=%d alignof=%d\n",
1138 sizeof(struct aligntest2), __alignof__(struct aligntest2));
1139 printf("aligntest3 sizeof=%d alignof=%d\n",
1140 sizeof(struct aligntest3), __alignof__(struct aligntest3));
1141 printf("aligntest4 sizeof=%d alignof=%d\n",
1142 sizeof(struct aligntest4), __alignof__(struct aligntest4));
1143 printf("aligntest5 sizeof=%d alignof=%d\n",
1144 sizeof(struct aligntest5), __alignof__(struct aligntest5));
1145 printf("aligntest6 sizeof=%d alignof=%d\n",
1146 sizeof(struct aligntest6), __alignof__(struct aligntest6));
1147 printf("aligntest7 sizeof=%d alignof=%d\n",
1148 sizeof(struct aligntest7), __alignof__(struct aligntest7));
1149 printf("aligntest8 sizeof=%d alignof=%d\n",
1150 sizeof(struct aligntest8), __alignof__(struct aligntest8));
1151 printf("aligntest9 sizeof=%d alignof=%d\n",
1152 sizeof(struct aligntest9), __alignof__(struct aligntest9));
1153 printf("aligntest10 sizeof=%d alignof=%d\n",
1154 sizeof(struct aligntest10), __alignof__(struct aligntest10));
1155 printf("altest5 sizeof=%d alignof=%d\n",
1156 sizeof(altest5), __alignof__(altest5));
1157 printf("altest6 sizeof=%d alignof=%d\n",
1158 sizeof(altest6), __alignof__(altest6));
1159 /*printf("altest7 sizeof=%d alignof=%d\n",
1160 sizeof(altest7), __alignof__(altest7));*/
1162 /* empty structures (GCC extension) */
1163 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
1164 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
1166 printf("Large: sizeof=%d\n", sizeof(ls));
1167 memset(&ls, 0, sizeof(ls));
1168 ls.compound_head = 42;
1169 printf("Large: offsetof(compound_head)=%d\n", (int)((char*)&ls.compound_head - (char*)&ls));
1172 /* XXX: depend on endianness */
1173 void char_short_test()
1175 int var1, var2;
1177 printf("char_short:\n");
1179 var1 = 0x01020304;
1180 var2 = 0xfffefdfc;
1181 printf("s8=%d %d\n",
1182 *(char *)&var1, *(char *)&var2);
1183 printf("u8=%d %d\n",
1184 *(unsigned char *)&var1, *(unsigned char *)&var2);
1185 printf("s16=%d %d\n",
1186 *(short *)&var1, *(short *)&var2);
1187 printf("u16=%d %d\n",
1188 *(unsigned short *)&var1, *(unsigned short *)&var2);
1189 printf("s32=%d %d\n",
1190 *(int *)&var1, *(int *)&var2);
1191 printf("u32=%d %d\n",
1192 *(unsigned int *)&var1, *(unsigned int *)&var2);
1193 *(char *)&var1 = 0x08;
1194 printf("var1=%x\n", var1);
1195 *(short *)&var1 = 0x0809;
1196 printf("var1=%x\n", var1);
1197 *(int *)&var1 = 0x08090a0b;
1198 printf("var1=%x\n", var1);
1201 /******************/
1203 typedef struct Sym {
1204 int v;
1205 int t;
1206 int c;
1207 struct Sym *next;
1208 struct Sym *prev;
1209 } Sym;
1211 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1212 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1214 static int toupper1(int a)
1216 return TOUPPER(a);
1219 static unsigned int calc_vm_flags(unsigned int prot)
1221 unsigned int prot_bits;
1222 /* This used to segfault in some revisions: */
1223 prot_bits = ((0x1==0x00000001)?(prot&0x1):(prot&0x1)?0x00000001:0);
1224 return prot_bits;
1227 void bool_test()
1229 int *s, a, b, t, f, i;
1231 a = 0;
1232 s = (void*)0;
1233 printf("!s=%d\n", !s);
1235 if (!s || !s[0])
1236 a = 1;
1237 printf("a=%d\n", a);
1239 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1240 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1241 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1242 #if 1 && 1
1243 printf("a1\n");
1244 #endif
1245 #if 1 || 0
1246 printf("a2\n");
1247 #endif
1248 #if 1 ? 0 : 1
1249 printf("a3\n");
1250 #endif
1251 #if 0 ? 0 : 1
1252 printf("a4\n");
1253 #endif
1255 a = 4;
1256 printf("b=%d\n", a + (0 ? 1 : a / 2));
1258 /* test register spilling */
1259 a = 10;
1260 b = 10;
1261 a = (a + b) * ((a < b) ?
1262 ((b - a) * (a - b)): a + b);
1263 printf("a=%d\n", a);
1265 /* test complex || or && expressions */
1266 t = 1;
1267 f = 0;
1268 a = 32;
1269 printf("exp=%d\n", f == (32 <= a && a <= 3));
1270 printf("r=%d\n", (t || f) + (t && f));
1272 /* test ? : cast */
1274 int aspect_on;
1275 int aspect_native = 65536;
1276 double bfu_aspect = 1.0;
1277 int aspect;
1278 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
1279 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
1280 printf("aspect=%d\n", aspect);
1284 /* test ? : GCC extension */
1286 static int v1 = 34 ? : -1; /* constant case */
1287 static int v2 = 0 ? : -1; /* constant case */
1288 int a = 30;
1290 printf("%d %d\n", v1, v2);
1291 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
1294 /* again complex expression */
1295 for(i=0;i<256;i++) {
1296 if (toupper1 (i) != TOUPPER (i))
1297 printf("error %d\n", i);
1299 printf ("bits = 0x%x\n", calc_vm_flags (0x1));
1302 extern int undefined_function(void);
1303 extern int defined_function(void);
1305 static inline void refer_to_undefined(void)
1307 undefined_function();
1310 void optimize_out(void)
1312 int i = 0 ? undefined_function() : defined_function();
1313 printf ("oo:%d\n", i);
1314 int j = 1 ? defined_function() : undefined_function();
1315 printf ("oo:%d\n", j);
1316 if (0)
1317 printf("oo:%d\n", undefined_function());
1318 else
1319 printf("oo:%d\n", defined_function());
1320 if (1)
1321 printf("oo:%d\n", defined_function());
1322 else
1323 printf("oo:%d\n", undefined_function());
1324 while (1) {
1325 printf("oow:%d\n", defined_function());
1326 break;
1327 printf("oow:%d\n", undefined_function());
1329 j = 1;
1330 /* Following is a switch without {} block intentionally. */
1331 switch (j)
1332 case 1: break;
1333 printf ("oos:%d\n", defined_function());
1334 /* The following break shouldn't lead to disabled code after
1335 the while. */
1336 while (1)
1337 break;
1338 printf ("ool1:%d\n", defined_function());
1339 /* Same for the other types of loops. */
1341 break;
1342 while (1);
1343 printf ("ool2:%d\n", defined_function());
1344 for (;;)
1345 break;
1346 printf ("ool3:%d\n", defined_function());
1347 /* Normal {} blocks without controlling statements
1348 shouldn't reactivate code emission */
1349 while (1) {
1351 break;
1353 printf ("ool4:%d\n", undefined_function());
1355 j = 1;
1356 while (j) {
1357 if (j == 0)
1358 break; /* this break shouldn't disable code outside the if. */
1359 printf("ool5:%d\n", defined_function());
1360 j--;
1363 j = 1;
1364 while (j) {
1365 if (1)
1366 j--;
1367 else
1368 breakhere: break;
1369 printf("ool6:%d\n", defined_function());
1370 goto breakhere;
1373 /* Test that constants in logical && are optimized: */
1374 i = 0 && undefined_function();
1375 i = defined_function() && 0 && undefined_function();
1376 if (0 && undefined_function())
1377 undefined_function();
1378 if (defined_function() && 0)
1379 undefined_function();
1380 if (0 && 0)
1381 undefined_function();
1382 if (defined_function() && 0 && undefined_function())
1383 undefined_function();
1384 /* The same for || : */
1385 i = 1 || undefined_function();
1386 i = defined_function() || 1 || undefined_function();
1387 if (1 || undefined_function())
1389 else
1390 undefined_function();
1391 if (defined_function() || 1)
1393 else
1394 undefined_function();
1395 if (1 || 1)
1397 else
1398 undefined_function();
1399 if (defined_function() || 1 || undefined_function())
1401 else
1402 undefined_function();
1404 if (defined_function() && 0)
1405 refer_to_undefined();
1407 if (0) {
1408 (void)sizeof( ({
1409 do { } while (0);
1411 }) );
1412 undefined_function();
1415 /* Leave the "if(1)return; printf()" in this order and last in the function */
1416 if (1)
1417 return;
1418 printf ("oor:%d\n", undefined_function());
1421 int defined_function(void)
1423 static int i = 40;
1424 return i++;
1427 /* GCC accepts that */
1428 static int tab_reinit[];
1429 static int tab_reinit[10];
1431 //int cinit1; /* a global variable can be defined several times without error ! */
1432 int cinit1;
1433 int cinit1;
1434 int cinit1 = 0;
1435 int *cinit2 = (int []){3, 2, 1};
1437 void compound_literal_test(void)
1439 int *p, i;
1440 char *q, *q3;
1442 printf("compound_test:\n");
1444 p = (int []){1, 2, 3};
1445 for(i=0;i<3;i++)
1446 printf(" %d", p[i]);
1447 printf("\n");
1449 for(i=0;i<3;i++)
1450 printf("%d", cinit2[i]);
1451 printf("\n");
1453 q = "tralala1";
1454 printf("q1=%s\n", q);
1456 q = (char *){ "tralala2" };
1457 printf("q2=%s\n", q);
1459 q3 = (char *){ q };
1460 printf("q3=%s\n", q3);
1462 q = (char []){ "tralala3" };
1463 printf("q4=%s\n", q);
1465 #ifdef ALL_ISOC99
1466 p = (int []){1, 2, cinit1 + 3};
1467 for(i=0;i<3;i++)
1468 printf(" %d", p[i]);
1469 printf("\n");
1471 for(i=0;i<3;i++) {
1472 p = (int []){1, 2, 4 + i};
1473 printf("%d %d %d\n",
1474 p[0],
1475 p[1],
1476 p[2]);
1478 #endif
1481 /* K & R protos */
1483 kr_func1(a, b)
1485 return a + b;
1488 int kr_func2(a, b)
1490 return a + b;
1493 kr_test()
1495 printf("kr_test:\n");
1496 printf("func1=%d\n", kr_func1(3, 4));
1497 printf("func2=%d\n", kr_func2(3, 4));
1498 return 0;
1501 void num(int n)
1503 char *tab, *p;
1504 tab = (char*)malloc(20);
1505 p = tab;
1506 while (1) {
1507 *p = 48 + (n % 10);
1508 p++;
1509 n = n / 10;
1510 if (n == 0)
1511 break;
1513 while (p != tab) {
1514 p--;
1515 printf("%c", *p);
1517 printf("\n");
1518 free(tab);
1521 /* structure assignment tests */
1522 struct structa1 {
1523 int f1;
1524 char f2;
1527 struct structa1 ssta1;
1529 void struct_assign_test1(struct structa1 s1, int t, float f)
1531 printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
1534 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1536 s1.f1 += t;
1537 s1.f2 -= t;
1538 return s1;
1541 void struct_assign_test(void)
1543 struct S {
1544 struct structa1 lsta1, lsta2;
1545 int i;
1546 } s, *ps;
1548 ps = &s;
1549 ps->i = 4;
1550 #if 0
1551 printf("struct_assign_test:\n");
1553 s.lsta1.f1 = 1;
1554 s.lsta1.f2 = 2;
1555 printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
1556 s.lsta2 = s.lsta1;
1557 printf("%d %d\n", s.lsta2.f1, s.lsta2.f2);
1558 #else
1559 s.lsta2.f1 = 1;
1560 s.lsta2.f2 = 2;
1561 #endif
1562 struct_assign_test1(ps->lsta2, 3, 4.5);
1564 printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
1565 ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
1566 printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
1568 static struct {
1569 void (*elem)();
1570 } t[] = {
1571 /* XXX: we should allow this even without braces */
1572 { struct_assign_test }
1574 printf("%d\n", struct_assign_test == t[0].elem);
1577 /* casts to short/char */
1579 void cast1(char a, short b, unsigned char c, unsigned short d)
1581 printf("%d %d %d %d\n", a, b, c, d);
1584 char bcast;
1585 short scast;
1587 void cast_test()
1589 int a;
1590 char c;
1591 char tab[10];
1592 unsigned b,d;
1593 short s;
1594 char *p = NULL;
1595 p -= 0x700000000042;
1597 printf("cast_test:\n");
1598 a = 0xfffff;
1599 cast1(a, a, a, a);
1600 a = 0xffffe;
1601 printf("%d %d %d %d\n",
1602 (char)(a + 1),
1603 (short)(a + 1),
1604 (unsigned char)(a + 1),
1605 (unsigned short)(a + 1));
1606 printf("%d %d %d %d\n",
1607 (char)0xfffff,
1608 (short)0xfffff,
1609 (unsigned char)0xfffff,
1610 (unsigned short)0xfffff);
1612 a = (bcast = 128) + 1;
1613 printf("%d\n", a);
1614 a = (scast = 65536) + 1;
1615 printf("%d\n", a);
1617 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1619 /* test cast from unsigned to signed short to int */
1620 b = 0xf000;
1621 d = (short)b;
1622 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1623 b = 0xf0f0;
1624 d = (char)b;
1625 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1627 /* test implicit int casting for array accesses */
1628 c = 0;
1629 tab[1] = 2;
1630 tab[c] = 1;
1631 printf("%d %d\n", tab[0], tab[1]);
1633 /* test implicit casting on some operators */
1634 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1635 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1636 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1638 /* from pointer to integer types */
1639 printf("%d %d %ld %ld %lld %lld\n",
1640 (int)p, (unsigned int)p,
1641 (long)p, (unsigned long)p,
1642 (long long)p, (unsigned long long)p);
1644 /* from integers to pointers */
1645 printf("%p %p %p %p\n",
1646 (void *)a, (void *)b, (void *)c, (void *)d);
1649 /* initializers tests */
1650 struct structinit1 {
1651 int f1;
1652 char f2;
1653 short f3;
1654 int farray[3];
1657 int sinit1 = 2;
1658 int sinit2 = { 3 };
1659 int sinit3[3] = { 1, 2, {{3}}, };
1660 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1661 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1662 int sinit6[] = { 1, 2, 3 };
1663 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1664 char sinit8[] = "hello" "trala";
1666 struct structinit1 sinit9 = { 1, 2, 3 };
1667 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1668 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1669 #ifdef ALL_ISOC99
1670 .farray[0] = 10,
1671 .farray[1] = 11,
1672 .farray[2] = 12,
1673 #endif
1676 char *sinit12 = "hello world";
1677 char *sinit13[] = {
1678 "test1",
1679 "test2",
1680 "test3",
1682 char sinit14[10] = { "abc" };
1683 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1685 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1687 struct bar {
1688 char *s;
1689 int len;
1690 } sinit17[] = {
1691 "a1", 4,
1692 "a2", 1
1695 int sinit18[10] = {
1696 [2 ... 5] = 20,
1698 [8] = 10,
1701 struct complexinit0 {
1702 int a;
1703 int b;
1706 struct complexinit {
1707 int a;
1708 const struct complexinit0 *b;
1711 const static struct complexinit cix[] = {
1712 [0] = {
1713 .a = 2000,
1714 .b = (const struct complexinit0[]) {
1715 { 2001, 2002 },
1716 { 2003, 2003 },
1722 struct complexinit2 {
1723 int a;
1724 int b[];
1727 struct complexinit2 cix20;
1729 struct complexinit2 cix21 = {
1730 .a = 3000,
1731 .b = { 3001, 3002, 3003 }
1734 struct complexinit2 cix22 = {
1735 .a = 4000,
1736 .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1739 typedef int arrtype1[];
1740 arrtype1 sinit19 = {1};
1741 arrtype1 sinit20 = {2,3};
1742 typedef int arrtype2[3];
1743 arrtype2 sinit21 = {4};
1744 arrtype2 sinit22 = {5,6,7};
1746 /* Address comparisons of non-weak symbols with zero can be const-folded */
1747 int sinit23[2] = { "astring" ? sizeof("astring") : -1,
1748 &sinit23 ? 42 : -1 };
1750 void init_test(void)
1752 int linit1 = 2;
1753 int linit2 = { 3 };
1754 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1755 int linit6[] = { 1, 2, 3 };
1756 int i, j;
1757 char linit8[] = "hello" "trala";
1758 int linit12[10] = { 1, 2 };
1759 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1760 char linit14[10] = "abc";
1761 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1762 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1763 int linit17 = sizeof(linit17);
1764 int zero = 0;
1765 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1766 int linit18[2] = {&zero ? 1 : -1, zero ? -1 : 1 };
1768 printf("init_test:\n");
1770 printf("sinit1=%d\n", sinit1);
1771 printf("sinit2=%d\n", sinit2);
1772 printf("sinit3=%d %d %d %d\n",
1773 sizeof(sinit3),
1774 sinit3[0],
1775 sinit3[1],
1776 sinit3[2]
1778 printf("sinit6=%d\n", sizeof(sinit6));
1779 printf("sinit7=%d %d %d %d\n",
1780 sizeof(sinit7),
1781 sinit7[0],
1782 sinit7[1],
1783 sinit7[2]
1785 printf("sinit8=%s\n", sinit8);
1786 printf("sinit9=%d %d %d\n",
1787 sinit9.f1,
1788 sinit9.f2,
1789 sinit9.f3
1791 printf("sinit10=%d %d %d\n",
1792 sinit10.f1,
1793 sinit10.f2,
1794 sinit10.f3
1796 printf("sinit11=%d %d %d %d %d %d\n",
1797 sinit11.f1,
1798 sinit11.f2,
1799 sinit11.f3,
1800 sinit11.farray[0],
1801 sinit11.farray[1],
1802 sinit11.farray[2]
1805 for(i=0;i<3;i++)
1806 for(j=0;j<2;j++)
1807 printf("[%d][%d] = %d %d %d\n",
1808 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1809 printf("linit1=%d\n", linit1);
1810 printf("linit2=%d\n", linit2);
1811 printf("linit6=%d\n", sizeof(linit6));
1812 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1814 printf("sinit12=%s\n", sinit12);
1815 printf("sinit13=%d %s %s %s\n",
1816 sizeof(sinit13),
1817 sinit13[0],
1818 sinit13[1],
1819 sinit13[2]);
1820 printf("sinit14=%s\n", sinit14);
1822 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1823 printf("\n");
1824 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1825 printf("\n");
1826 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1827 printf("\n");
1828 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1829 printf("\n");
1830 printf("%d %d %d %d\n",
1831 linit16.a1,
1832 linit16.a2,
1833 linit16.a3,
1834 linit16.a4);
1835 /* test that initialisation is done after variable declare */
1836 printf("linit17=%d\n", linit17);
1837 printf("sinit15=%d\n", sinit15[0]);
1838 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1839 printf("sinit17=%s %d %s %d\n",
1840 sinit17[0].s, sinit17[0].len,
1841 sinit17[1].s, sinit17[1].len);
1842 for(i=0;i<10;i++)
1843 printf("%x ", sinit18[i]);
1844 printf("\n");
1845 /* complex init check */
1846 printf("cix: %d %d %d %d %d %d %d\n",
1847 cix[0].a,
1848 cix[0].b[0].a, cix[0].b[0].b,
1849 cix[0].b[1].a, cix[0].b[1].b,
1850 cix[0].b[2].a, cix[0].b[2].b);
1851 printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
1852 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
1854 printf("arrtype1: %d %d %d\n", sinit19[0], sinit20[0], sinit20[1]);
1855 printf("arrtype2: %d %d\n", sizeof(sinit19), sizeof(sinit20));
1856 printf("arrtype3: %d %d %d\n", sinit21[0], sinit21[1], sinit21[2]);
1857 printf("arrtype4: %d %d %d\n", sinit22[0], sinit22[1], sinit22[2]);
1858 printf("arrtype5: %d %d\n", sizeof(sinit21), sizeof(sinit22));
1859 printf("arrtype6: %d\n", sizeof(arrtype2));
1861 printf("sinit23= %d %d\n", sinit23[0], sinit23[1]);
1862 printf("linit18= %d %d\n", linit18[0], linit18[1]);
1865 void switch_uc(unsigned char uc)
1867 switch (uc) {
1868 case 0xfb ... 0xfe:
1869 printf("ucsw:1\n");
1870 break;
1871 case 0xff:
1872 printf("ucsw:2\n");
1873 break;
1874 case 0 ... 5:
1875 printf("ucsw:3\n");
1876 break;
1877 default:
1878 printf("ucsw: broken!\n");
1882 void switch_sc(signed char sc)
1884 switch (sc) {
1885 case -5 ... -2:
1886 printf("scsw:1\n");
1887 break;
1888 case -1:
1889 printf("scsw:2\n");
1890 break;
1891 case 0 ... 5:
1892 printf("scsw:3\n");
1893 break;
1894 default:
1895 printf("scsw: broken!\n");
1899 void switch_test()
1901 int i;
1902 unsigned long long ull;
1903 long long ll;
1905 for(i=0;i<15;i++) {
1906 switch(i) {
1907 case 0:
1908 case 1:
1909 printf("a");
1910 break;
1911 default:
1912 printf("%d", i);
1913 break;
1914 case 8 ... 12:
1915 printf("c");
1916 break;
1917 case 3:
1918 printf("b");
1919 break;
1920 case 0xc33c6b9fU:
1921 case 0x7c9eeeb9U:
1922 break;
1925 printf("\n");
1927 for (i = 1; i <= 5; i++) {
1928 ull = (unsigned long long)i << 61;
1929 switch (ull) {
1930 case 1ULL << 61:
1931 printf("ullsw:1\n");
1932 break;
1933 case 2ULL << 61:
1934 printf("ullsw:2\n");
1935 break;
1936 case 3ULL << 61:
1937 printf("ullsw:3\n");
1938 break;
1939 case 4ULL << 61:
1940 printf("ullsw:4\n");
1941 break;
1942 case 5ULL << 61:
1943 printf("ullsw:5\n");
1944 break;
1945 default:
1946 printf("ullsw: broken!\n");
1950 for (i = 1; i <= 5; i++) {
1951 ll = (long long)i << 61;
1952 switch (ll) {
1953 case 1LL << 61:
1954 printf("llsw:1\n");
1955 break;
1956 case 2LL << 61:
1957 printf("llsw:2\n");
1958 break;
1959 case 3LL << 61:
1960 printf("llsw:3\n");
1961 break;
1962 case 4LL << 61:
1963 printf("llsw:4\n");
1964 break;
1965 case 5LL << 61:
1966 printf("llsw:5\n");
1967 break;
1968 default:
1969 printf("llsw: broken!\n");
1973 for (i = -5; i <= 5; i++) {
1974 switch_uc((unsigned char)i);
1977 for (i = -5; i <= 5; i++) {
1978 switch_sc ((signed char)i);
1982 /* ISOC99 _Bool type */
1983 void c99_bool_test(void)
1985 #ifdef BOOL_ISOC99
1986 int a;
1987 _Bool b;
1989 printf("bool_test:\n");
1990 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1991 a = 3;
1992 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1993 b = 3;
1994 printf("b = %d\n", b);
1995 b++;
1996 printf("b = %d\n", b);
1997 #endif
2000 void bitfield_test(void)
2002 int a;
2003 short sa;
2004 unsigned char ca;
2005 struct sbf1 {
2006 int f1 : 3;
2007 int : 2;
2008 int f2 : 1;
2009 int : 0;
2010 int f3 : 5;
2011 int f4 : 7;
2012 unsigned int f5 : 7;
2013 } st1;
2014 printf("bitfield_test:");
2015 printf("sizeof(st1) = %d\n", sizeof(st1));
2017 st1.f1 = 3;
2018 st1.f2 = 1;
2019 st1.f3 = 15;
2020 a = 120;
2021 st1.f4 = a;
2022 st1.f5 = a;
2023 st1.f5++;
2024 printf("%d %d %d %d %d\n",
2025 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
2026 sa = st1.f5;
2027 ca = st1.f5;
2028 printf("%d %d\n", sa, ca);
2030 st1.f1 = 7;
2031 if (st1.f1 == -1)
2032 printf("st1.f1 == -1\n");
2033 else
2034 printf("st1.f1 != -1\n");
2035 if (st1.f2 == -1)
2036 printf("st1.f2 == -1\n");
2037 else
2038 printf("st1.f2 != -1\n");
2040 #ifndef __i386__
2041 /* on i386 we don't correctly support long long bit-fields.
2042 The bitfields can straddle long long boundaries (at least with
2043 GCC bitfield layout) and code generation isn't prepared for this
2044 (would have to work with two words in that case). */
2045 /* bit sizes below must be bigger than 32 since GCC doesn't allow
2046 long-long bitfields whose size is not bigger than int */
2047 struct sbf2 {
2048 long long f1 : 45;
2049 long long : 2;
2050 long long f2 : 35;
2051 unsigned long long f3 : 38;
2052 } st2;
2053 st2.f1 = 0x123456789ULL;
2054 a = 120;
2055 st2.f2 = (long long)a << 25;
2056 st2.f3 = a;
2057 st2.f2++;
2058 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
2059 #endif
2060 #if 0
2061 Disabled for now until further clarification re GCC compatibility
2062 struct sbf3 {
2063 int f1 : 7;
2064 int f2 : 1;
2065 char f3;
2066 int f4 : 8;
2067 int f5 : 1;
2068 int f6 : 16;
2069 } st3;
2070 printf("sizeof(st3) = %d\n", sizeof(st3));
2071 #endif
2072 struct sbf4 {
2073 int x : 31;
2074 char y : 2;
2075 } st4;
2076 st4.y = 1;
2077 printf("st4.y == %d\n", st4.y);
2078 struct sbf5 {
2079 int a;
2080 char b;
2081 int x : 12, y : 4, : 0, : 4, z : 3;
2082 char c;
2083 } st5 = { 1, 2, 3, 4, -3, 6 };
2084 printf("st5 = %d %d %d %d %d %d\n", st5.a, st5.b, st5.x, st5.y, st5.z, st5.c);
2087 #ifdef __x86_64__
2088 #define FLOAT_FMT "%f\n"
2089 #else
2090 /* x86's float isn't compatible with GCC */
2091 #define FLOAT_FMT "%.5f\n"
2092 #endif
2094 /* declare strto* functions as they are C99 */
2095 double strtod(const char *nptr, char **endptr);
2097 #if defined(_WIN32)
2098 float strtof(const char *nptr, char **endptr) {return (float)strtod(nptr, endptr);}
2099 LONG_DOUBLE strtold(const char *nptr, char **endptr) {return (LONG_DOUBLE)strtod(nptr, endptr);}
2100 #else
2101 float strtof(const char *nptr, char **endptr);
2102 LONG_DOUBLE strtold(const char *nptr, char **endptr);
2103 #endif
2105 #define FTEST(prefix, typename, type, fmt)\
2106 void prefix ## cmp(type a, type b)\
2108 printf("%d %d %d %d %d %d\n",\
2109 a == b,\
2110 a != b,\
2111 a < b,\
2112 a > b,\
2113 a >= b,\
2114 a <= b);\
2115 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
2118 a + b,\
2119 a - b,\
2120 a * b,\
2121 a / b,\
2122 -a);\
2123 printf(fmt "\n", ++a);\
2124 printf(fmt "\n", a++);\
2125 printf(fmt "\n", a);\
2126 b = 0;\
2127 printf("%d %d\n", !a, !b);\
2129 void prefix ## fcast(type a)\
2131 float fa;\
2132 double da;\
2133 LONG_DOUBLE la;\
2134 int ia;\
2135 long long llia;\
2136 unsigned int ua;\
2137 unsigned long long llua;\
2138 type b;\
2139 fa = a;\
2140 da = a;\
2141 la = a;\
2142 printf("ftof: %f %f %Lf\n", fa, da, la);\
2143 ia = (int)a;\
2144 llia = (long long)a;\
2145 a = (a >= 0) ? a : -a;\
2146 ua = (unsigned int)a;\
2147 llua = (unsigned long long)a;\
2148 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
2149 ia = -1234;\
2150 ua = 0x81234500;\
2151 llia = -0x123456789012345LL;\
2152 llua = 0xf123456789012345LLU;\
2153 b = ia;\
2154 printf("itof: " fmt "\n", b);\
2155 b = ua;\
2156 printf("utof: " fmt "\n", b);\
2157 b = llia;\
2158 printf("lltof: " fmt "\n", b);\
2159 b = llua;\
2160 printf("ulltof: " fmt "\n", b);\
2163 float prefix ## retf(type a) { return a; }\
2164 double prefix ## retd(type a) { return a; }\
2165 LONG_DOUBLE prefix ## retld(type a) { return a; }\
2167 void prefix ## call(void)\
2169 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
2170 printf("double: %f\n", prefix ## retd(42.123456789));\
2171 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
2172 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
2175 void prefix ## signed_zeros(void) \
2177 type x = 0.0, y = -0.0, n, p;\
2178 if (x == y)\
2179 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
2180 1.0 / x != 1.0 / y);\
2181 else\
2182 printf ("x != y; this is wrong!\n");\
2184 n = -x;\
2185 if (x == n)\
2186 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
2187 1.0 / x != 1.0 / n);\
2188 else\
2189 printf ("x != -x; this is wrong!\n");\
2191 p = +y;\
2192 if (x == p)\
2193 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
2194 1.0 / x != 1.0 / p);\
2195 else\
2196 printf ("x != +y; this is wrong!\n");\
2197 p = -y;\
2198 if (x == p)\
2199 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
2200 1.0 / x != 1.0 / p);\
2201 else\
2202 printf ("x != -y; this is wrong!\n");\
2204 void prefix ## test(void)\
2206 printf("testing '%s'\n", #typename);\
2207 prefix ## cmp(1, 2.5);\
2208 prefix ## cmp(2, 1.5);\
2209 prefix ## cmp(1, 1);\
2210 prefix ## fcast(234.6);\
2211 prefix ## fcast(-2334.6);\
2212 prefix ## call();\
2213 prefix ## signed_zeros();\
2216 FTEST(f, float, float, "%f")
2217 FTEST(d, double, double, "%f")
2218 FTEST(ld, long double, LONG_DOUBLE, "%Lf")
2220 double ftab1[3] = { 1.2, 3.4, -5.6 };
2223 void float_test(void)
2225 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
2226 float fa, fb;
2227 double da, db;
2228 int a;
2229 unsigned int b;
2231 printf("float_test:\n");
2232 printf("sizeof(float) = %d\n", sizeof(float));
2233 printf("sizeof(double) = %d\n", sizeof(double));
2234 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));
2235 ftest();
2236 dtest();
2237 ldtest();
2238 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
2239 printf("%f %f %f\n", 2.12, .5, 2.3e10);
2240 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
2241 da = 123;
2242 printf("da=%f\n", da);
2243 fa = 123;
2244 printf("fa=%f\n", fa);
2245 a = 4000000000;
2246 da = a;
2247 printf("da = %f\n", da);
2248 b = 4000000000;
2249 db = b;
2250 printf("db = %f\n", db);
2251 #endif
2254 int fib(int n)
2256 if (n <= 2)
2257 return 1;
2258 else
2259 return fib(n-1) + fib(n-2);
2262 void funcptr_test()
2264 void (*func)(int);
2265 int a;
2266 struct {
2267 int dummy;
2268 void (*func)(int);
2269 } st1;
2270 long diff;
2272 printf("funcptr:\n");
2273 func = &num;
2274 (*func)(12345);
2275 func = num;
2276 a = 1;
2277 a = 1;
2278 func(12345);
2279 /* more complicated pointer computation */
2280 st1.func = num;
2281 st1.func(12346);
2282 printf("sizeof1 = %d\n", sizeof(funcptr_test));
2283 printf("sizeof2 = %d\n", sizeof funcptr_test);
2284 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
2285 printf("sizeof4 = %d\n", sizeof &funcptr_test);
2286 a = 0;
2287 func = num + a;
2288 diff = func - num;
2289 func(42);
2290 (func + diff)(42);
2291 (num + a)(43);
2294 void lloptest(long long a, long long b)
2296 unsigned long long ua, ub;
2298 ua = a;
2299 ub = b;
2300 /* arith */
2301 printf("arith: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2302 a + b,
2303 a - b,
2304 a * b);
2306 if (b != 0) {
2307 printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2308 a / b,
2309 a % b);
2312 /* binary */
2313 printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2314 a & b,
2315 a | b,
2316 a ^ b);
2318 /* tests */
2319 printf("test: %d %d %d %d %d %d\n",
2320 a == b,
2321 a != b,
2322 a < b,
2323 a > b,
2324 a >= b,
2325 a <= b);
2327 printf("utest: %d %d %d %d %d %d\n",
2328 ua == ub,
2329 ua != ub,
2330 ua < ub,
2331 ua > ub,
2332 ua >= ub,
2333 ua <= ub);
2335 /* arith2 */
2336 a++;
2337 b++;
2338 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2339 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a++, b++);
2340 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", --a, --b);
2341 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2342 b = ub = 0;
2343 printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
2346 void llshift(long long a, int b)
2348 printf("shift: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2349 (unsigned long long)a >> b,
2350 a >> b,
2351 a << b);
2352 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2353 (unsigned long long)a >> 3,
2354 a >> 3,
2355 a << 3);
2356 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2357 (unsigned long long)a >> 35,
2358 a >> 35,
2359 a << 35);
2362 void llfloat(void)
2364 float fa;
2365 double da;
2366 LONG_DOUBLE lda;
2367 long long la, lb, lc;
2368 unsigned long long ula, ulb, ulc;
2369 la = 0x12345678;
2370 ula = 0x72345678;
2371 la = (la << 20) | 0x12345;
2372 ula = ula << 33;
2373 printf("la=" LONG_LONG_FORMAT " ula=" ULONG_LONG_FORMAT "\n", la, ula);
2375 fa = la;
2376 da = la;
2377 lda = la;
2378 printf("lltof: %f %f %Lf\n", fa, da, lda);
2380 la = fa;
2381 lb = da;
2382 lc = lda;
2383 printf("ftoll: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", la, lb, lc);
2385 fa = ula;
2386 da = ula;
2387 lda = ula;
2388 printf("ulltof: %f %f %Lf\n", fa, da, lda);
2390 ula = fa;
2391 ulb = da;
2392 ulc = lda;
2393 printf("ftoull: " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT "\n", ula, ulb, ulc);
2396 long long llfunc1(int a)
2398 return a * 2;
2401 struct S {
2402 int id;
2403 char item;
2406 long long int value(struct S *v)
2408 return ((long long int)v->item);
2411 long long llfunc2(long long x, long long y, int z)
2413 return x * y * z;
2416 void longlong_test(void)
2418 long long a, b, c;
2419 int ia;
2420 unsigned int ua;
2421 printf("longlong_test:\n");
2422 printf("sizeof(long long) = %d\n", sizeof(long long));
2423 ia = -1;
2424 ua = -2;
2425 a = ia;
2426 b = ua;
2427 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2428 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %Lx\n",
2429 (long long)1,
2430 (long long)-2,
2431 1LL,
2432 0x1234567812345679);
2433 a = llfunc1(-3);
2434 printf(LONG_LONG_FORMAT "\n", a);
2436 lloptest(1000, 23);
2437 lloptest(0xff, 0x1234);
2438 b = 0x72345678 << 10;
2439 lloptest(-3, b);
2440 llshift(0x123, 5);
2441 llshift(-23, 5);
2442 b = 0x72345678LL << 10;
2443 llshift(b, 47);
2445 llfloat();
2446 #if 1
2447 b = 0x12345678;
2448 a = -1;
2449 c = a + b;
2450 printf("%Lx\n", c);
2451 #endif
2453 /* long long reg spill test */
2455 struct S a;
2457 a.item = 3;
2458 printf("%lld\n", value(&a));
2460 lloptest(0x80000000, 0);
2463 long long *p, v, **pp;
2464 v = 1;
2465 p = &v;
2466 p[0]++;
2467 printf("another long long spill test : %lld\n", *p);
2468 pp = &p;
2470 v = llfunc2(**pp, **pp, ia);
2471 printf("a long long function (arm-)reg-args test : %lld\n", v);
2473 a = 68719476720LL;
2474 b = 4294967295LL;
2475 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
2477 printf(LONG_LONG_FORMAT "\n", 0x123456789LLU);
2479 /* long long pointer deref in argument passing test */
2480 a = 0x123;
2481 long long *p = &a;
2482 llshift(*p, 5);
2485 void manyarg_test(void)
2487 LONG_DOUBLE ld = 1234567891234LL;
2488 printf("manyarg_test:\n");
2489 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2490 1, 2, 3, 4, 5, 6, 7, 8,
2491 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2492 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2493 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2494 1, 2, 3, 4, 5, 6, 7, 8,
2495 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2496 1234567891234LL, 987654321986LL,
2497 42.0, 43.0);
2498 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2499 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2500 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2501 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2502 1234567891234LL, 987654321986LL,
2503 42.0, 43.0);
2504 printf("%d %d %d %d %d %d %d %d %Lf\n",
2505 1, 2, 3, 4, 5, 6, 7, 8, ld);
2506 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2507 LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n",
2508 1, 2, 3, 4, 5, 6, 7, 8,
2509 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2510 1234567891234LL, 987654321986LL,
2511 42.0, 43.0, ld);
2512 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2513 "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n",
2514 1, 2, 3, 4, 5, 6, 7, 8,
2515 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2516 ld, 1234567891234LL, 987654321986LL,
2517 42.0, 43.0, ld);
2520 void vprintf1(const char *fmt, ...)
2522 va_list ap, aq;
2523 const char *p;
2524 int c, i;
2525 double d;
2526 long long ll;
2527 LONG_DOUBLE ld;
2529 va_start(aq, fmt);
2530 va_copy(ap, aq);
2532 p = fmt;
2533 for(;;) {
2534 c = *p;
2535 if (c == '\0')
2536 break;
2537 p++;
2538 if (c == '%') {
2539 c = *p;
2540 switch(c) {
2541 case '\0':
2542 goto the_end;
2543 case 'd':
2544 i = va_arg(ap, int);
2545 printf("%d", i);
2546 break;
2547 case 'f':
2548 d = va_arg(ap, double);
2549 printf("%f", d);
2550 break;
2551 case 'l':
2552 ll = va_arg(ap, long long);
2553 printf(LONG_LONG_FORMAT, ll);
2554 break;
2555 case 'F':
2556 ld = va_arg(ap, LONG_DOUBLE);
2557 printf("%Lf", ld);
2558 break;
2560 p++;
2561 } else {
2562 putchar(c);
2565 the_end:
2566 va_end(aq);
2567 va_end(ap);
2570 struct myspace {
2571 short int profile;
2574 void stdarg_for_struct(struct myspace bob, ...)
2576 struct myspace george, bill;
2577 va_list ap;
2578 short int validate;
2580 va_start(ap, bob);
2581 bill = va_arg(ap, struct myspace);
2582 george = va_arg(ap, struct myspace);
2583 validate = va_arg(ap, int);
2584 printf("stdarg_for_struct: %d %d %d %d\n",
2585 bob.profile, bill.profile, george.profile, validate);
2586 va_end(ap);
2589 void stdarg_for_libc(const char *fmt, ...)
2591 va_list args;
2592 va_start(args, fmt);
2593 vprintf(fmt, args);
2594 va_end(args);
2597 void stdarg_test(void)
2599 LONG_DOUBLE ld = 1234567891234LL;
2600 struct myspace bob;
2602 vprintf1("%d %d %d\n", 1, 2, 3);
2603 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2604 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2605 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2606 vprintf1("%d %f %l %F %d %f %l %F\n",
2607 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2608 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2609 1, 2, 3, 4, 5, 6, 7, 8,
2610 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2611 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2612 1, 2, 3, 4, 5, 6, 7, 8,
2613 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2614 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2615 "%l %l %f %f\n",
2616 1, 2, 3, 4, 5, 6, 7, 8,
2617 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2618 1234567891234LL, 987654321986LL,
2619 42.0, 43.0);
2620 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2621 "%l %l %f %f\n",
2622 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2623 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2624 1234567891234LL, 987654321986LL,
2625 42.0, 43.0);
2626 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2627 1, 2, 3, 4, 5, 6, 7, 8, ld);
2628 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2629 "%l %l %f %f %F\n",
2630 1, 2, 3, 4, 5, 6, 7, 8,
2631 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2632 1234567891234LL, 987654321986LL,
2633 42.0, 43.0, ld);
2634 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2635 "%F %l %l %f %f %F\n",
2636 1, 2, 3, 4, 5, 6, 7, 8,
2637 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2638 ld, 1234567891234LL, 987654321986LL,
2639 42.0, 43.0, ld);
2641 bob.profile = 42;
2642 stdarg_for_struct(bob, bob, bob, bob.profile);
2643 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2646 void whitespace_test(void)
2648 char *str;
2650 \f\v #if 1
2651 pri\
2652 ntf("whitspace:\n");\f\v
2653 #endif
2654 pf("N=%d\n", 2);
2656 #ifdef CORRECT_CR_HANDLING
2657 pri\
2658 ntf("aaa=%d\n", 3);
2659 #endif
2661 pri\
2663 ntf("min=%d\n", 4);
2665 #ifdef ACCEPT_CR_IN_STRINGS
2666 printf("len1=%d\n", strlen("
2667 "));
2668 #ifdef CORRECT_CR_HANDLING
2669 str = "
2671 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
2672 #endif
2673 printf("len1=%d\n", strlen(" a
2674 "));
2675 #endif /* ACCEPT_CR_IN_STRINGS */
2678 int reltab[3] = { 1, 2, 3 };
2680 int *rel1 = &reltab[1];
2681 int *rel2 = &reltab[2];
2683 #ifdef _WIN64
2684 void relocation_test(void) {}
2685 #else
2686 void getmyaddress(void)
2688 printf("in getmyaddress\n");
2691 #ifdef __LP64__
2692 long __pa_symbol(void)
2694 /* This 64bit constant was handled incorrectly, it was used as addend
2695 (which can hold 64bit just fine) in connection with a symbol,
2696 and TCC generates wrong code for that (displacements are 32bit only).
2697 This effectively is "+ 0x80000000", and if addresses of globals
2698 are below 2GB the result should be a number without high 32 bits set. */
2699 return ((long)(((unsigned long)(&rel1))) - (0xffffffff80000000UL));
2701 #endif
2703 unsigned long theaddress = (unsigned long)getmyaddress;
2704 void relocation_test(void)
2706 void (*fptr)(void) = (void (*)(void))theaddress;
2707 printf("*rel1=%d\n", *rel1);
2708 printf("*rel2=%d\n", *rel2);
2709 fptr();
2710 #ifdef __LP64__
2711 printf("pa_symbol=0x%lx\n", __pa_symbol() >> 63);
2712 #endif
2714 #endif
2716 void old_style_f(a,b,c)
2717 int a, b;
2718 double c;
2720 printf("a=%d b=%d b=%f\n", a, b, c);
2723 void decl_func1(int cmpfn())
2725 printf("cmpfn=%lx\n", (long)cmpfn);
2728 void decl_func2(cmpfn)
2729 int cmpfn();
2731 printf("cmpfn=%lx\n", (long)cmpfn);
2734 void old_style_function(void)
2736 old_style_f((void *)1, 2, 3.0);
2737 decl_func1(NULL);
2738 decl_func2(NULL);
2741 void alloca_test()
2743 #if defined __i386__ || defined __x86_64__ || defined __arm__
2744 char *p = alloca(16);
2745 strcpy(p,"123456789012345");
2746 printf("alloca: p is %s\n", p);
2747 char *demo = "This is only a test.\n";
2748 /* Test alloca embedded in a larger expression */
2749 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
2750 #endif
2753 void *bounds_checking_is_enabled()
2755 char ca[10], *cp = ca-1;
2756 return (ca != cp + 1) ? cp : NULL;
2759 typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
2761 void c99_vla_test(int size1, int size2)
2763 #if defined __i386__ || defined __x86_64__
2764 int size = size1 * size2;
2765 int tab1[size][2], tab2[10][2];
2766 void *tab1_ptr, *tab2_ptr, *bad_ptr;
2768 /* "size" should have been 'captured' at tab1 declaration,
2769 so modifying it should have no effect on VLA behaviour. */
2770 size = size-1;
2772 printf("Test C99 VLA 1 (sizeof): ");
2773 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
2774 tab1_ptr = tab1;
2775 tab2_ptr = tab2;
2776 printf("Test C99 VLA 2 (ptrs subtract): ");
2777 printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2778 printf("Test C99 VLA 3 (ptr add): ");
2779 printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2780 printf("Test C99 VLA 4 (ptr access): ");
2781 tab1[size1][1] = 42;
2782 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2784 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2785 if (bad_ptr = bounds_checking_is_enabled()) {
2786 int *t1 = &tab1[size1 * size2 - 1][3];
2787 int *t2 = &tab2[9][3];
2788 printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
2789 printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
2791 char*c1 = 1 + sizeof(tab1) + (char*)tab1;
2792 char*c2 = 1 + sizeof(tab2) + (char*)tab2;
2793 printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
2794 printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
2796 int *i1 = tab1[-1];
2797 int *i2 = tab2[-1];
2798 printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
2799 printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
2801 int *x1 = tab1[size1 * size2 + 1];
2802 int *x2 = tab2[10 + 1];
2803 printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
2804 printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
2805 } else {
2806 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2808 printf("\n");
2809 #endif
2812 #ifndef __TINYC__
2813 typedef __SIZE_TYPE__ uintptr_t;
2814 #endif
2816 void sizeof_test(void)
2818 int a;
2819 int **ptr;
2821 printf("sizeof(int) = %d\n", sizeof(int));
2822 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2823 printf("sizeof(long) = %d\n", sizeof(long));
2824 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2825 printf("sizeof(short) = %d\n", sizeof(short));
2826 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2827 printf("sizeof(char) = %d\n", sizeof(char));
2828 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2829 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2830 a = 1;
2831 printf("sizeof(a++) = %d\n", sizeof a++);
2832 printf("a=%d\n", a);
2833 ptr = NULL;
2834 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
2836 /* The type of sizeof should be as large as a pointer, actually
2837 it should be size_t. */
2838 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2839 uintptr_t t = 1;
2840 uintptr_t t2;
2841 /* Effectively <<32, but defined also on 32bit machines. */
2842 t <<= 16;
2843 t <<= 16;
2844 t++;
2845 /* This checks that sizeof really can be used to manipulate
2846 uintptr_t objects, without truncation. */
2847 t2 = t & -sizeof(uintptr_t);
2848 printf ("%lu %lu\n", t, t2);
2850 /* some alignof tests */
2851 printf("__alignof__(int) = %d\n", __alignof__(int));
2852 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2853 printf("__alignof__(short) = %d\n", __alignof__(short));
2854 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2855 printf("__alignof__(char) = %d\n", __alignof__(char));
2856 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2857 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
2859 /* sizes of VLAs need to be evaluated even inside sizeof: */
2860 a = 2;
2861 printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a]));
2862 /* And checking if sizeof compound literal works. Parenthesized: */
2863 printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n",
2864 sizeof( (struct {int i; int j;}){4,5} ));
2865 /* And as direct sizeof argument (as unary expression): */
2866 printf("sizeof (struct {short i; short j;}){4,5} = %d\n",
2867 sizeof (struct {short i; short j;}){4,5} );
2869 /* sizeof(x && y) should be sizeof(int), even if constant
2870 evaluating is possible. */
2871 printf("sizeof(t && 0) = %d\n", sizeof(t && 0));
2872 printf("sizeof(1 && 1) = %d\n", sizeof(1 && 1));
2873 printf("sizeof(t || 1) = %d\n", sizeof(t || 1));
2874 printf("sizeof(0 || 0) = %d\n", sizeof(0 || 0));
2877 void typeof_test(void)
2879 double a;
2880 typeof(a) b;
2881 typeof(float) c;
2883 a = 1.5;
2884 b = 2.5;
2885 c = 3.5;
2886 printf("a=%f b=%f c=%f\n", a, b, c);
2890 struct hlist_node;
2891 struct hlist_head {
2892 struct hlist_node *first, *last;
2895 void statement_expr_test(void)
2897 int a, i;
2899 /* Basic stmt expr test */
2900 a = 0;
2901 for(i=0;i<10;i++) {
2902 a += 1 +
2903 ( { int b, j;
2904 b = 0;
2905 for(j=0;j<5;j++)
2906 b += j; b;
2907 } );
2909 printf("a=%d\n", a);
2911 /* Test that symbols aren't freed prematurely.
2912 With SYM_DEBUG valgrind will show a read from a freed
2913 symbol, and tcc will show an (invalid) warning on the initialization
2914 of 'ptr' below, if symbols are popped after the stmt expr. */
2915 void *v = (void*)39;
2916 typeof(({
2917 (struct hlist_node *)v;
2918 })) x;
2919 typeof (x)
2920 ptr = (struct hlist_node *)v;
2922 /* This part used to segfault when symbols were popped prematurely.
2923 The symbols for the static local would be overwritten with
2924 helper symbols from the pre-processor expansions in between. */
2925 #define some_attr __attribute__((aligned(1)))
2926 #define tps(str) ({ \
2927 static const char *t some_attr = str; \
2928 t; \
2930 printf ("stmtexpr: %s %s\n",
2931 tps("somerandomlongstring"),
2932 tps("anotherlongstring"));
2934 /* Test that the three decls of 't' don't interact. */
2935 int t = 40;
2936 int b = ({ int t = 41; t; });
2937 int c = ({ int t = 42; t; });
2939 /* Test that aggregate return values work. */
2940 struct hlist_head h
2941 = ({
2942 typedef struct hlist_head T;
2943 long pre = 48;
2944 T t = { (void*)43, (void*)44 };
2945 long post = 49;
2948 printf ("stmtexpr: %d %d %d\n", t, b, c);
2949 printf ("stmtexpr: %ld %ld\n", (long)h.first, (long)h.last);
2952 void local_label_test(void)
2954 int a;
2955 goto l1;
2957 a = 1 + ({
2958 __label__ l1, l2, l3, l4;
2959 goto l1;
2961 printf("aa1\n");
2962 goto l3;
2964 printf("aa3\n");
2965 goto l4;
2967 printf("aa2\n");
2968 goto l2;
2969 l3:;
2972 printf("a=%d\n", a);
2973 return;
2975 printf("bb1\n");
2976 goto l2;
2978 printf("bb2\n");
2979 goto l4;
2982 /* inline assembler test */
2983 #if defined(__i386__) || defined(__x86_64__)
2985 /* from linux kernel */
2986 static char * strncat1(char * dest,const char * src,size_t count)
2988 long d0, d1, d2, d3;
2989 __asm__ __volatile__(
2990 "repne\n\t"
2991 "scasb\n\t"
2992 "dec %1\n\t"
2993 "mov %8,%3\n"
2994 "1:\tdec %3\n\t"
2995 "js 2f\n\t"
2996 "lodsb\n\t"
2997 "stosb\n\t"
2998 "testb %%al,%%al\n\t"
2999 "jne 1b\n"
3000 "2:\txor %2,%2\n\t"
3001 "stosb"
3002 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
3003 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
3004 : "memory");
3005 return dest;
3008 static char * strncat2(char * dest,const char * src,size_t count)
3010 long d0, d1, d2, d3;
3011 __asm__ __volatile__(
3012 "repne scasb\n\t" /* one-line repne prefix + string op */
3013 "dec %1\n\t"
3014 "mov %8,%3\n"
3015 "1:\tdec %3\n\t"
3016 "js 2f\n\t"
3017 "lodsb\n\t"
3018 "stosb\n\t"
3019 "testb %%al,%%al\n\t"
3020 "jne 1b\n"
3021 "2:\txor %2,%2\n\t"
3022 "stosb"
3023 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
3024 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
3025 : "memory");
3026 return dest;
3029 static inline void * memcpy1(void * to, const void * from, size_t n)
3031 long d0, d1, d2;
3032 __asm__ __volatile__(
3033 "rep ; movsl\n\t"
3034 "testb $2,%b4\n\t"
3035 "je 1f\n\t"
3036 "movsw\n"
3037 "1:\ttestb $1,%b4\n\t"
3038 "je 2f\n\t"
3039 "movsb\n"
3040 "2:"
3041 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
3042 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
3043 : "memory");
3044 return (to);
3047 static inline void * memcpy2(void * to, const void * from, size_t n)
3049 long d0, d1, d2;
3050 __asm__ __volatile__(
3051 "rep movsl\n\t" /* one-line rep prefix + string op */
3052 "testb $2,%b4\n\t"
3053 "je 1f\n\t"
3054 "movsw\n"
3055 "1:\ttestb $1,%b4\n\t"
3056 "je 2f\n\t"
3057 "movsb\n"
3058 "2:"
3059 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
3060 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
3061 : "memory");
3062 return (to);
3065 static __inline__ void sigaddset1(unsigned int *set, int _sig)
3067 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
3070 static __inline__ void sigdelset1(unsigned int *set, int _sig)
3072 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc", "flags");
3075 static __inline__ __const__ unsigned int swab32(unsigned int x)
3077 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
3078 "rorl $16,%0\n\t" /* swap words */
3079 "xchgb %b0,%h0" /* swap higher bytes */
3080 :"=" "q" (x)
3081 : "0" (x));
3082 return x;
3085 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
3087 unsigned long long res;
3088 #ifdef __x86_64__
3089 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
3090 but still test the 32bit->64bit mull. */
3091 unsigned int resh, resl;
3092 __asm__("mull %2" : "=a" (resl), "=d" (resh) : "a" (a), "r" (b));
3093 res = ((unsigned long long)resh << 32) | resl;
3094 #else
3095 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
3096 #endif
3097 return res;
3100 static __inline__ unsigned long long inc64(unsigned long long a)
3102 unsigned long long res;
3103 #ifdef __x86_64__
3104 /* Using the A constraint is wrong, and increments are tested
3105 elsewere. */
3106 res = a + 1;
3107 #else
3108 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
3109 #endif
3110 return res;
3113 struct struct123 {
3114 int a;
3115 int b;
3117 struct struct1231 {
3118 unsigned long addr;
3121 unsigned long mconstraint_test(struct struct1231 *r)
3123 unsigned long ret;
3124 unsigned int a[2];
3125 a[0] = 0;
3126 __asm__ volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
3127 : "=&r" (ret), "=m" (a)
3128 : "m" (*(struct struct123 *)r->addr));
3129 return ret + a[0];
3132 #ifdef __x86_64__
3133 int fls64(unsigned long long x)
3135 int bitpos = -1;
3136 asm("bsrq %1,%q0"
3137 : "+r" (bitpos)
3138 : "rm" (x));
3139 return bitpos + 1;
3141 #endif
3143 void other_constraints_test(void)
3145 unsigned long ret;
3146 int var;
3147 #ifndef _WIN64
3148 __asm__ volatile ("mov %P1,%0" : "=r" (ret) : "p" (&var));
3149 printf ("oc1: %d\n", ret == (unsigned long)&var);
3150 #endif
3153 #ifndef _WIN32
3154 /* Test global asm blocks playing with aliases. */
3155 void base_func(void)
3157 printf ("asmc: base\n");
3160 extern void override_func1 (void);
3161 extern void override_func2 (void);
3163 asm(".weak override_func1\n.set override_func1, base_func");
3164 asm(".set override_func1, base_func");
3165 asm(".set override_func2, base_func");
3167 void override_func2 (void)
3169 printf ("asmc: override2\n");
3172 /* This checks a construct used by the linux kernel to encode
3173 references to strings by PC relative references. */
3174 extern int bug_table[] __attribute__((section("__bug_table")));
3175 char * get_asm_string (void)
3177 extern int some_symbol;
3178 asm volatile (".globl some_symbol\n"
3179 "jmp .+6\n"
3180 "1:\n"
3181 "some_symbol: .long 0\n"
3182 ".pushsection __bug_table, \"a\"\n"
3183 ".globl bug_table\n"
3184 "bug_table:\n"
3185 /* The first entry (1b-2b) is unused in this test,
3186 but we include it to check if cross-section
3187 PC-relative references work. */
3188 "2:\t.long 1b - 2b, %c0 - 2b\n"
3189 ".popsection\n" : : "i" ("A string"));
3190 char * str = ((char*)bug_table) + bug_table[1];
3191 return str;
3193 #endif
3195 unsigned int set;
3197 void fancy_copy (unsigned *in, unsigned *out)
3199 asm volatile ("" : "=r" (*out) : "0" (*in));
3202 void fancy_copy2 (unsigned *in, unsigned *out)
3204 asm volatile ("mov %0,(%1)" : : "r" (*in), "r" (out) : "memory");
3207 #ifdef __x86_64__
3208 void clobber_r12(void)
3210 asm volatile("mov $1, %%r12" ::: "r12");
3212 #endif
3214 void test_high_clobbers(void)
3216 #ifdef __x86_64__
3217 register long val asm("r12");
3218 long val2;
3219 /* This tests if asm clobbers correctly save/restore callee saved
3220 registers if they are clobbered and if it's the high 8 x86-64
3221 registers. This is fragile for GCC as the constraints do not
3222 correctly capture the data flow, but good enough for us. */
3223 asm volatile("mov $0x4542, %%r12" : "=r" (val):: "memory");
3224 clobber_r12();
3225 #ifndef _WIN64
3226 asm volatile("mov %%r12, %0" : "=r" (val2) : "r" (val): "memory");
3227 printf("asmhc: 0x%x\n", val2);
3228 #endif
3229 #endif
3232 static long cpu_number;
3233 void trace_console(long len, long len2)
3235 #ifdef __x86_64__
3236 /* This generated invalid code when the emission of the switch
3237 table isn't disabled. The asms are necessary to show the bug,
3238 normal statements don't work (they need to generate some code
3239 even under nocode_wanted, which normal statements don't do,
3240 but asms do). Also at least these number of cases is necessary
3241 to generate enough "random" bytes. They ultimately are enough
3242 to create invalid instruction patterns to which the first
3243 skip-to-decision-table jump jumps. If decision table emission
3244 is disabled all of this is no problem.
3246 It also is necessary that the switches are in a statement expression
3247 (which has the property of not being enterable from outside. no
3248 matter what). */
3249 if (0
3252 long pscr_ret__;
3253 switch(len) {
3254 case 4:
3256 long pfo_ret__;
3257 switch (len2) {
3258 case 8: printf("bla"); pfo_ret__ = 42; break;
3260 pscr_ret__ = pfo_ret__;
3262 break;
3263 case 8:
3265 long pfo_ret__;
3266 switch (len2) {
3267 case 1:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3268 case 2:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3269 case 4:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3270 case 8:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3271 default: printf("impossible\n");
3273 pscr_ret__ = pfo_ret__;
3275 break;
3277 pscr_ret__;
3280 printf("huh?\n");
3282 #endif
3285 void test_asm_dead_code(void)
3287 long rdi;
3288 /* Try to make sure that xdi contains a zero, and hence will
3289 lead to a segfault if the next asm is evaluated without
3290 arguments being set up. */
3291 asm volatile ("" : "=D" (rdi) : "0" (0));
3292 (void)sizeof (({
3293 int var;
3294 /* This shouldn't trigger a segfault, either the argument
3295 registers need to be set up and the asm emitted despite
3296 this being in an unevaluated context, or both the argument
3297 setup _and_ the asm emission need to be suppressed. The latter
3298 is better. Disabling asm code gen when suppression is on
3299 also fixes the above trace_console bug, but that came earlier
3300 than asm suppression. */
3301 asm volatile ("movl $0,(%0)" : : "D" (&var) : "memory");
3302 var;
3303 }));
3306 void asm_test(void)
3308 char buf[128];
3309 unsigned int val, val2;
3310 struct struct123 s1;
3311 struct struct1231 s2 = { (unsigned long)&s1 };
3312 /* Hide the outer base_func, but check later that the inline
3313 asm block gets the outer one. */
3314 int base_func = 42;
3315 void override_func3 (void);
3316 unsigned long asmret;
3317 #ifdef BOOL_ISOC99
3318 _Bool somebool;
3319 #endif
3320 register int regvar asm("%esi");
3322 printf("inline asm:\n");
3324 // parse 0x1E-1 as 3 tokens in asm mode
3325 asm volatile ("mov $0x1E-1,%eax");
3327 /* test the no operand case */
3328 asm volatile ("xorl %eax, %eax");
3330 memcpy1(buf, "hello", 6);
3331 strncat1(buf, " worldXXXXX", 3);
3332 printf("%s\n", buf);
3334 memcpy2(buf, "hello", 6);
3335 strncat2(buf, " worldXXXXX", 3);
3336 printf("%s\n", buf);
3338 /* 'A' constraint test */
3339 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
3340 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
3342 s1.a = 42;
3343 s1.b = 43;
3344 printf("mconstraint: %d", mconstraint_test(&s2));
3345 printf(" %d %d\n", s1.a, s1.b);
3346 other_constraints_test();
3347 set = 0xff;
3348 sigdelset1(&set, 2);
3349 sigaddset1(&set, 16);
3350 /* NOTE: we test here if C labels are correctly restored after the
3351 asm statement */
3352 goto label1;
3353 label2:
3354 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
3355 printf("set=0x%x\n", set);
3356 val = 0x01020304;
3357 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
3358 #ifndef _WIN32
3359 override_func1();
3360 override_func2();
3361 /* The base_func ref from the following inline asm should find
3362 the global one, not the local decl from this function. */
3363 asm volatile(".weak override_func3\n.set override_func3, base_func");
3364 override_func3();
3365 printf("asmstr: %s\n", get_asm_string());
3366 #endif
3367 /* Check that we can also load structs of appropriate layout
3368 into registers. */
3369 asm volatile("" : "=r" (asmret) : "0"(s2));
3370 if (asmret != s2.addr)
3371 printf("asmstr: failed\n");
3372 #ifdef BOOL_ISOC99
3373 /* Check that the typesize correctly sets the register size to
3374 8 bit. */
3375 asm volatile("cmp %1,%2; sete %0" : "=a"(somebool) : "r"(1), "r"(2));
3376 if (!somebool)
3377 printf("asmbool: failed\n");
3378 #endif
3379 val = 43;
3380 fancy_copy (&val, &val2);
3381 printf ("fancycpy(%d)=%d\n", val, val2);
3382 val = 44;
3383 fancy_copy2 (&val, &val2);
3384 printf ("fancycpy2(%d)=%d\n", val, val2);
3385 asm volatile ("mov $0x4243, %%esi" : "=r" (regvar));
3386 printf ("regvar=%x\n", regvar);
3387 test_high_clobbers();
3388 trace_console(8, 8);
3389 test_asm_dead_code();
3390 return;
3391 label1:
3392 goto label2;
3395 #else
3397 void asm_test(void)
3401 #endif
3403 #define COMPAT_TYPE(type1, type2) \
3405 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
3406 __builtin_types_compatible_p (type1, type2));\
3409 int constant_p_var;
3411 void builtin_test(void)
3413 short s;
3414 int i;
3415 long long ll;
3416 #if GCC_MAJOR >= 3
3417 COMPAT_TYPE(int, int);
3418 COMPAT_TYPE(int, unsigned int);
3419 COMPAT_TYPE(int, char);
3420 COMPAT_TYPE(int, const int);
3421 COMPAT_TYPE(int, volatile int);
3422 COMPAT_TYPE(int *, int *);
3423 COMPAT_TYPE(int *, void *);
3424 COMPAT_TYPE(int *, const int *);
3425 COMPAT_TYPE(char *, unsigned char *);
3426 COMPAT_TYPE(char *, signed char *);
3427 COMPAT_TYPE(char *, char *);
3428 /* space is needed because tcc preprocessor introduces a space between each token */
3429 COMPAT_TYPE(char * *, void *);
3430 #endif
3431 printf("res = %d\n", __builtin_constant_p(1));
3432 printf("res = %d\n", __builtin_constant_p(1 + 2));
3433 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
3434 printf("res = %d\n", __builtin_constant_p(constant_p_var));
3435 printf("res = %d\n", __builtin_constant_p(100000 / constant_p_var));
3436 s = 1;
3437 ll = 2;
3438 i = __builtin_choose_expr (1 != 0, ll, s);
3439 printf("bce: %d\n", i);
3440 i = __builtin_choose_expr (1 != 1, ll, s);
3441 printf("bce: %d\n", i);
3442 i = sizeof (__builtin_choose_expr (1, ll, s));
3443 printf("bce: %d\n", i);
3444 i = sizeof (__builtin_choose_expr (0, ll, s));
3445 printf("bce: %d\n", i);
3447 //printf("bera: %p\n", __builtin_extract_return_addr((void*)43));
3450 #ifndef _WIN32
3451 extern int __attribute__((weak)) weak_f1(void);
3452 extern int __attribute__((weak)) weak_f2(void);
3453 extern int weak_f3(void);
3454 extern int __attribute__((weak)) weak_v1;
3455 extern int __attribute__((weak)) weak_v2;
3456 extern int weak_v3;
3458 extern int (*weak_fpa)() __attribute__((weak));
3459 extern int __attribute__((weak)) (*weak_fpb)();
3460 extern __attribute__((weak)) int (*weak_fpc)();
3462 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
3463 extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
3464 extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
3465 extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
3466 extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
3467 extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
3469 static const size_t dummy = 0;
3470 extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
3471 extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
3472 extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
3474 int some_lib_func(void);
3475 int dummy_impl_of_slf(void) { return 444; }
3476 int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
3478 int weak_toolate() __attribute__((weak));
3479 int weak_toolate() { return 0; }
3481 void __attribute__((weak)) weak_test(void)
3483 printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
3484 printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
3485 printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
3486 printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
3487 printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
3488 printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
3490 printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
3491 printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
3492 printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
3494 printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
3495 printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
3496 printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
3497 printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
3498 printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
3499 printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
3500 printf("some_lib_func=%d\n", &some_lib_func ? some_lib_func() : 0);
3503 int __attribute__((weak)) weak_f2() { return 222; }
3504 int __attribute__((weak)) weak_f3() { return 333; }
3505 int __attribute__((weak)) weak_v2 = 222;
3506 int __attribute__((weak)) weak_v3 = 333;
3507 #endif
3509 void const_func(const int a)
3513 void const_warn_test(void)
3515 const_func(1);
3518 struct condstruct {
3519 int i;
3522 int getme (struct condstruct *s, int i)
3524 int i1 = (i == 0 ? 0 : s)->i;
3525 int i2 = (i == 0 ? s : 0)->i;
3526 int i3 = (i == 0 ? (void*)0 : s)->i;
3527 int i4 = (i == 0 ? s : (void*)0)->i;
3528 return i1 + i2 + i3 + i4;
3531 struct global_data
3533 int a[40];
3534 int *b[40];
3537 struct global_data global_data;
3539 int global_data_getstuff (int *, int);
3541 void global_data_callit (int i)
3543 *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
3546 int global_data_getstuff (int *p, int i)
3548 return *p + i;
3551 void global_data_test (void)
3553 global_data.a[0] = 42;
3554 global_data.b[0] = &global_data.a[0];
3555 global_data_callit (0);
3556 printf ("%d\n", global_data.a[0]);
3559 struct cmpcmpS
3561 unsigned char fill : 3;
3562 unsigned char b1 : 1;
3563 unsigned char b2 : 1;
3564 unsigned char fill2 : 3;
3567 int glob1, glob2, glob3;
3569 void compare_comparisons (struct cmpcmpS *s)
3571 if (s->b1 != (glob1 == glob2)
3572 || (s->b2 != (glob1 == glob3)))
3573 printf ("comparing comparisons broken\n");
3576 void cmp_comparison_test(void)
3578 struct cmpcmpS s;
3579 s.b1 = 1;
3580 glob1 = 42; glob2 = 42;
3581 s.b2 = 0;
3582 glob3 = 43;
3583 compare_comparisons (&s);
3586 int fcompare (double a, double b, int code)
3588 switch (code) {
3589 case 0: return a == b;
3590 case 1: return a != b;
3591 case 2: return a < b;
3592 case 3: return a >= b;
3593 case 4: return a > b;
3594 case 5: return a <= b;
3598 void math_cmp_test(void)
3600 double nan = 0.0/0.0;
3601 double one = 1.0;
3602 double two = 2.0;
3603 int comp = 0;
3604 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
3606 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
3607 And it does this in various ways so that all code generation paths
3608 are checked (generating inverted tests, or non-inverted tests, or
3609 producing a 0/1 value without jumps (that's done in the fcompare
3610 function). */
3611 #define FCMP(a,b,op,iop,code) \
3612 if (fcompare (a,b,code)) \
3613 bug (a,b,op,iop,1); \
3614 if (a op b) \
3615 bug (a,b,op,iop,2); \
3616 if (a iop b) \
3618 else \
3619 bug (a,b,op,iop,3); \
3620 if ((a op b) || comp) \
3621 bug (a,b,op,iop,4); \
3622 if ((a iop b) || comp) \
3624 else \
3625 bug (a,b,op,iop,5);
3627 /* Equality tests. */
3628 FCMP(nan, nan, ==, !=, 0);
3629 FCMP(one, two, ==, !=, 0);
3630 FCMP(one, one, !=, ==, 1);
3631 /* Non-equality is a bit special. */
3632 if (!fcompare (nan, nan, 1))
3633 bug (nan, nan, !=, ==, 6);
3635 /* Relational tests on numbers. */
3636 FCMP(two, one, <, >=, 2);
3637 FCMP(one, two, >=, <, 3);
3638 FCMP(one, two, >, <=, 4);
3639 FCMP(two, one, <=, >, 5);
3641 /* Relational tests on NaNs. Note that the inverse op here is
3642 always !=, there's no operator in C that is equivalent to !(a < b),
3643 when NaNs are involved, same for the other relational ops. */
3644 FCMP(nan, nan, <, !=, 2);
3645 FCMP(nan, nan, >=, !=, 3);
3646 FCMP(nan, nan, >, !=, 4);
3647 FCMP(nan, nan, <=, !=, 5);
3650 double get100 () { return 100.0; }
3652 void callsave_test(void)
3654 #if defined __i386__ || defined __x86_64__ || defined __arm__
3655 int i, s; double *d; double t;
3656 s = sizeof (double);
3657 printf ("callsavetest: %d\n", s);
3658 d = alloca (sizeof(double));
3659 d[0] = 10.0;
3660 /* x86-64 had a bug were the next call to get100 would evict
3661 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
3662 in int type, not pointer type. When alloca returns a pointer
3663 with the high 32 bit set (which is likely on x86-64) the access
3664 generates a segfault. */
3665 i = d[0] > get100 ();
3666 printf ("%d\n", i);
3667 #endif
3671 void bfa3(ptrdiff_t str_offset)
3673 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
3675 void bfa2(ptrdiff_t str_offset)
3677 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
3678 bfa3(str_offset);
3680 void bfa1(ptrdiff_t str_offset)
3682 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
3683 bfa2(str_offset);
3686 void builtin_frame_address_test(void)
3688 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
3689 #ifndef __arm__
3690 char str[] = "__builtin_frame_address";
3691 char *fp0 = __builtin_frame_address(0);
3693 printf("str: %s\n", str);
3694 bfa1(str-fp0);
3695 #endif
3698 char via_volatile (char i)
3700 char volatile vi;
3701 vi = i;
3702 return vi;
3705 struct __attribute__((__packed__)) Spacked {
3706 char a;
3707 short b;
3708 int c;
3710 struct Spacked spacked;
3711 typedef struct __attribute__((__packed__)) {
3712 char a;
3713 short b;
3714 int c;
3715 } Spacked2;
3716 Spacked2 spacked2;
3717 /* This doesn't work for now. Requires adjusting field offsets/sizes
3718 after parsing the struct members. */
3719 typedef struct Spacked3_s {
3720 char a;
3721 short b;
3722 int c;
3723 } __attribute__((__packed__)) Spacked3;
3724 Spacked3 spacked3;
3725 struct gate_struct64 {
3726 unsigned short offset_low;
3727 unsigned short segment;
3728 unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1;
3729 unsigned short offset_middle;
3730 unsigned offset_high;
3731 unsigned zero1;
3732 } __attribute__((packed));
3733 typedef struct gate_struct64 gate_desc;
3734 gate_desc a_gate_desc;
3735 void attrib_test(void)
3737 #ifndef _WIN32
3738 printf("attr: %d %d %d %d\n", sizeof(struct Spacked),
3739 sizeof(spacked), sizeof(Spacked2), sizeof(spacked2));
3740 printf("attr: %d %d\n", sizeof(Spacked3), sizeof(spacked3));
3741 printf("attr: %d %d\n", sizeof(gate_desc), sizeof(a_gate_desc));
3742 #endif
3744 extern __attribute__((__unused__)) char * __attribute__((__unused__)) *
3745 strange_attrib_placement (void);
3747 void * __attribute__((__unused__)) get_void_ptr (void *a)
3749 return a;