x86-64-asm: Accept high register in clobbers
[tinycc.git] / tests / tcctest.c
blob5124a4fbc11f1445f5a4f4b2fb7180f1e89245d8
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;
609 void enum_test()
611 enum test b1;
612 /* The following should give no warning */
613 unsigned *p = &b1;
614 struct S_enum s = {E7};
615 printf("enum: %d\n", s.e);
616 printf("enum:\n%d %d %d %d %d %d\n",
617 E0, E1, E2, E3, E4, E5);
618 b1 = 1;
619 printf("b1=%d\n", b1);
622 typedef int *my_ptr;
624 typedef int mytype1;
625 typedef int mytype2;
627 void typedef_test()
629 my_ptr a;
630 mytype1 mytype2;
631 int b;
633 a = &b;
634 *a = 1234;
635 printf("typedef:\n");
636 printf("a=%d\n", *a);
637 mytype2 = 2;
638 printf("mytype2=%d\n", mytype2);
641 void forward_test()
643 printf("forward:\n");
644 forward_ref();
645 forward_ref();
649 void forward_ref(void)
651 printf("forward ok\n");
654 typedef struct struct1 {
655 int f1;
656 int f2, f3;
657 union union1 {
658 int v1;
659 int v2;
660 } u;
661 char str[3];
662 } struct1;
664 struct struct2 {
665 int a;
666 char b;
669 union union2 {
670 int w1;
671 int w2;
674 struct struct1 st1, st2;
676 struct empty_mem {
677 /* nothing */ ;
678 int x;
681 int main(int argc, char **argv)
683 string_test();
684 expr_test();
685 macro_test();
686 recursive_macro_test();
687 scope_test();
688 forward_test();
689 funcptr_test();
690 loop_test();
691 switch_test();
692 goto_test();
693 enum_test();
694 typedef_test();
695 struct_test();
696 array_test();
697 expr_ptr_test();
698 bool_test();
699 optimize_out();
700 expr2_test();
701 constant_expr_test();
702 expr_cmp_test();
703 char_short_test();
704 init_test();
705 compound_literal_test();
706 kr_test();
707 struct_assign_test();
708 cast_test();
709 bitfield_test();
710 c99_bool_test();
711 float_test();
712 longlong_test();
713 manyarg_test();
714 stdarg_test();
715 whitespace_test();
716 relocation_test();
717 old_style_function();
718 alloca_test();
719 c99_vla_test(5, 2);
720 sizeof_test();
721 typeof_test();
722 statement_expr_test();
723 local_label_test();
724 asm_test();
725 builtin_test();
726 #ifndef _WIN32
727 weak_test();
728 #endif
729 global_data_test();
730 cmp_comparison_test();
731 math_cmp_test();
732 callsave_test();
733 builtin_frame_address_test();
734 intdiv_test();
735 if (via_volatile (42) != 42)
736 printf ("via_volatile broken\n");
737 attrib_test();
738 return 0;
741 int tab[3];
742 int tab2[3][2];
744 int g;
746 void f1(g)
748 printf("g1=%d\n", g);
751 void scope_test()
753 printf("scope:\n");
754 g = 2;
755 f1(1);
756 printf("g2=%d\n", g);
758 int g;
759 g = 3;
760 printf("g3=%d\n", g);
762 int g;
763 g = 4;
764 printf("g4=%d\n", g);
767 printf("g5=%d\n", g);
770 void array_test()
772 int i, j, a[4];
774 printf("array:\n");
775 printf("sizeof(a) = %d\n", sizeof(a));
776 printf("sizeof(\"a\") = %d\n", sizeof("a"));
777 #ifdef C99_MACROS
778 printf("sizeof(__func__) = %d\n", sizeof(__func__));
779 #endif
780 printf("sizeof tab %d\n", sizeof(tab));
781 printf("sizeof tab2 %d\n", sizeof tab2);
782 tab[0] = 1;
783 tab[1] = 2;
784 tab[2] = 3;
785 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
786 for(i=0;i<3;i++)
787 for(j=0;j<2;j++)
788 tab2[i][j] = 10 * i + j;
789 for(i=0;i<3*2;i++) {
790 printf(" %3d", ((int *)tab2)[i]);
792 printf("\n");
793 printf("sizeof(size_t)=%d\n", sizeof(size_t));
794 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
797 void expr_test()
799 int a, b;
800 a = 0;
801 printf("%d\n", a += 1);
802 printf("%d\n", a -= 2);
803 printf("%d\n", a *= 31232132);
804 printf("%d\n", a /= 4);
805 printf("%d\n", a %= 20);
806 printf("%d\n", a &= 6);
807 printf("%d\n", a ^= 7);
808 printf("%d\n", a |= 8);
809 printf("%d\n", a >>= 3);
810 printf("%d\n", a <<= 4);
812 a = 22321;
813 b = -22321;
814 printf("%d\n", a + 1);
815 printf("%d\n", a - 2);
816 printf("%d\n", a * 312);
817 printf("%d\n", a / 4);
818 printf("%d\n", b / 4);
819 printf("%d\n", (unsigned)b / 4);
820 printf("%d\n", a % 20);
821 printf("%d\n", b % 20);
822 printf("%d\n", (unsigned)b % 20);
823 printf("%d\n", a & 6);
824 printf("%d\n", a ^ 7);
825 printf("%d\n", a | 8);
826 printf("%d\n", a >> 3);
827 printf("%d\n", b >> 3);
828 printf("%d\n", (unsigned)b >> 3);
829 printf("%d\n", a << 4);
830 printf("%d\n", ~a);
831 printf("%d\n", -a);
832 printf("%d\n", +a);
834 printf("%d\n", 12 + 1);
835 printf("%d\n", 12 - 2);
836 printf("%d\n", 12 * 312);
837 printf("%d\n", 12 / 4);
838 printf("%d\n", 12 % 20);
839 printf("%d\n", 12 & 6);
840 printf("%d\n", 12 ^ 7);
841 printf("%d\n", 12 | 8);
842 printf("%d\n", 12 >> 2);
843 printf("%d\n", 12 << 4);
844 printf("%d\n", ~12);
845 printf("%d\n", -12);
846 printf("%d\n", +12);
847 printf("%d %d %d %d\n",
848 isid('a'),
849 isid('g'),
850 isid('T'),
851 isid('('));
854 int isid(int c)
856 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
859 /**********************/
861 int vstack[10], *vstack_ptr;
863 void vpush(int vt, int vc)
865 *vstack_ptr++ = vt;
866 *vstack_ptr++ = vc;
869 void vpop(int *ft, int *fc)
871 *fc = *--vstack_ptr;
872 *ft = *--vstack_ptr;
875 void expr2_test()
877 int a, b;
879 printf("expr2:\n");
880 vstack_ptr = vstack;
881 vpush(1432432, 2);
882 vstack_ptr[-2] &= ~0xffffff80;
883 vpop(&a, &b);
884 printf("res= %d %d\n", a, b);
887 void constant_expr_test()
889 int a;
890 printf("constant_expr:\n");
891 a = 3;
892 printf("%d\n", a * 16);
893 printf("%d\n", a * 1);
894 printf("%d\n", a + 0);
897 int tab4[10];
899 void expr_ptr_test()
901 int *p, *q;
902 int i = -1;
904 printf("expr_ptr:\n");
905 p = tab4;
906 q = tab4 + 10;
907 printf("diff=%d\n", q - p);
908 p++;
909 printf("inc=%d\n", p - tab4);
910 p--;
911 printf("dec=%d\n", p - tab4);
912 ++p;
913 printf("inc=%d\n", p - tab4);
914 --p;
915 printf("dec=%d\n", p - tab4);
916 printf("add=%d\n", p + 3 - tab4);
917 printf("add=%d\n", 3 + p - tab4);
919 /* check if 64bit support is ok */
920 q = p = 0;
921 q += i;
922 printf("%p %p %ld\n", q, p, p-q);
923 printf("%d %d %d %d %d %d\n",
924 p == q, p != q, p < q, p <= q, p >= q, p > q);
925 i = 0xf0000000;
926 p += i;
927 printf("%p %p %ld\n", q, p, p-q);
928 printf("%d %d %d %d %d %d\n",
929 p == q, p != q, p < q, p <= q, p >= q, p > q);
930 p = (int *)((char *)p + 0xf0000000);
931 printf("%p %p %ld\n", q, p, p-q);
932 printf("%d %d %d %d %d %d\n",
933 p == q, p != q, p < q, p <= q, p >= q, p > q);
934 p += 0xf0000000;
935 printf("%p %p %ld\n", q, p, p-q);
936 printf("%d %d %d %d %d %d\n",
937 p == q, p != q, p < q, p <= q, p >= q, p > q);
939 struct size12 {
940 int i, j, k;
942 struct size12 s[2], *sp = s;
943 int i, j;
944 sp->i = 42;
945 sp++;
946 j = -1;
947 printf("%d\n", sp[j].i);
949 #ifdef __LP64__
950 i = 1;
951 p = (int*)0x100000000UL + i;
952 i = ((long)p) >> 32;
953 printf("largeptr: %p %d\n", p, i);
954 #endif
957 void expr_cmp_test()
959 int a, b;
960 printf("constant_expr:\n");
961 a = -1;
962 b = 1;
963 printf("%d\n", a == a);
964 printf("%d\n", a != a);
966 printf("%d\n", a < b);
967 printf("%d\n", a <= b);
968 printf("%d\n", a <= a);
969 printf("%d\n", b >= a);
970 printf("%d\n", a >= a);
971 printf("%d\n", b > a);
973 printf("%d\n", (unsigned)a < b);
974 printf("%d\n", (unsigned)a <= b);
975 printf("%d\n", (unsigned)a <= a);
976 printf("%d\n", (unsigned)b >= a);
977 printf("%d\n", (unsigned)a >= a);
978 printf("%d\n", (unsigned)b > a);
981 struct empty {
984 struct aligntest1 {
985 char a[10];
988 struct aligntest2 {
989 int a;
990 char b[10];
993 struct aligntest3 {
994 double a, b;
997 struct aligntest4 {
998 double a[0];
1001 void struct_test()
1003 struct1 *s;
1004 union union2 u;
1006 printf("struct:\n");
1007 printf("sizes: %d %d %d %d\n",
1008 sizeof(struct struct1),
1009 sizeof(struct struct2),
1010 sizeof(union union1),
1011 sizeof(union union2));
1012 st1.f1 = 1;
1013 st1.f2 = 2;
1014 st1.f3 = 3;
1015 printf("st1: %d %d %d\n",
1016 st1.f1, st1.f2, st1.f3);
1017 st1.u.v1 = 1;
1018 st1.u.v2 = 2;
1019 printf("union1: %d\n", st1.u.v1);
1020 u.w1 = 1;
1021 u.w2 = 2;
1022 printf("union2: %d\n", u.w1);
1023 s = &st2;
1024 s->f1 = 3;
1025 s->f2 = 2;
1026 s->f3 = 1;
1027 printf("st2: %d %d %d\n",
1028 s->f1, s->f2, s->f3);
1029 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
1031 /* align / size tests */
1032 printf("aligntest1 sizeof=%d alignof=%d\n",
1033 sizeof(struct aligntest1), __alignof__(struct aligntest1));
1034 printf("aligntest2 sizeof=%d alignof=%d\n",
1035 sizeof(struct aligntest2), __alignof__(struct aligntest2));
1036 printf("aligntest3 sizeof=%d alignof=%d\n",
1037 sizeof(struct aligntest3), __alignof__(struct aligntest3));
1038 printf("aligntest4 sizeof=%d alignof=%d\n",
1039 sizeof(struct aligntest4), __alignof__(struct aligntest4));
1041 /* empty structures (GCC extension) */
1042 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
1043 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
1046 /* XXX: depend on endianness */
1047 void char_short_test()
1049 int var1, var2;
1051 printf("char_short:\n");
1053 var1 = 0x01020304;
1054 var2 = 0xfffefdfc;
1055 printf("s8=%d %d\n",
1056 *(char *)&var1, *(char *)&var2);
1057 printf("u8=%d %d\n",
1058 *(unsigned char *)&var1, *(unsigned char *)&var2);
1059 printf("s16=%d %d\n",
1060 *(short *)&var1, *(short *)&var2);
1061 printf("u16=%d %d\n",
1062 *(unsigned short *)&var1, *(unsigned short *)&var2);
1063 printf("s32=%d %d\n",
1064 *(int *)&var1, *(int *)&var2);
1065 printf("u32=%d %d\n",
1066 *(unsigned int *)&var1, *(unsigned int *)&var2);
1067 *(char *)&var1 = 0x08;
1068 printf("var1=%x\n", var1);
1069 *(short *)&var1 = 0x0809;
1070 printf("var1=%x\n", var1);
1071 *(int *)&var1 = 0x08090a0b;
1072 printf("var1=%x\n", var1);
1075 /******************/
1077 typedef struct Sym {
1078 int v;
1079 int t;
1080 int c;
1081 struct Sym *next;
1082 struct Sym *prev;
1083 } Sym;
1085 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1086 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1088 static int toupper1(int a)
1090 return TOUPPER(a);
1093 static unsigned int calc_vm_flags(unsigned int prot)
1095 unsigned int prot_bits;
1096 /* This used to segfault in some revisions: */
1097 prot_bits = ((0x1==0x00000001)?(prot&0x1):(prot&0x1)?0x00000001:0);
1098 return prot_bits;
1101 void bool_test()
1103 int *s, a, b, t, f, i;
1105 a = 0;
1106 s = (void*)0;
1107 printf("!s=%d\n", !s);
1109 if (!s || !s[0])
1110 a = 1;
1111 printf("a=%d\n", a);
1113 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1114 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1115 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1116 #if 1 && 1
1117 printf("a1\n");
1118 #endif
1119 #if 1 || 0
1120 printf("a2\n");
1121 #endif
1122 #if 1 ? 0 : 1
1123 printf("a3\n");
1124 #endif
1125 #if 0 ? 0 : 1
1126 printf("a4\n");
1127 #endif
1129 a = 4;
1130 printf("b=%d\n", a + (0 ? 1 : a / 2));
1132 /* test register spilling */
1133 a = 10;
1134 b = 10;
1135 a = (a + b) * ((a < b) ?
1136 ((b - a) * (a - b)): a + b);
1137 printf("a=%d\n", a);
1139 /* test complex || or && expressions */
1140 t = 1;
1141 f = 0;
1142 a = 32;
1143 printf("exp=%d\n", f == (32 <= a && a <= 3));
1144 printf("r=%d\n", (t || f) + (t && f));
1146 /* test ? : cast */
1148 int aspect_on;
1149 int aspect_native = 65536;
1150 double bfu_aspect = 1.0;
1151 int aspect;
1152 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
1153 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
1154 printf("aspect=%d\n", aspect);
1158 /* test ? : GCC extension */
1160 static int v1 = 34 ? : -1; /* constant case */
1161 static int v2 = 0 ? : -1; /* constant case */
1162 int a = 30;
1164 printf("%d %d\n", v1, v2);
1165 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
1168 /* again complex expression */
1169 for(i=0;i<256;i++) {
1170 if (toupper1 (i) != TOUPPER (i))
1171 printf("error %d\n", i);
1173 printf ("bits = 0x%x\n", calc_vm_flags (0x1));
1176 extern int undefined_function(void);
1177 extern int defined_function(void);
1179 static inline void refer_to_undefined(void)
1181 undefined_function();
1184 void optimize_out(void)
1186 int i = 0 ? undefined_function() : defined_function();
1187 printf ("oo:%d\n", i);
1188 int j = 1 ? defined_function() : undefined_function();
1189 printf ("oo:%d\n", j);
1190 if (0)
1191 printf("oo:%d\n", undefined_function());
1192 else
1193 printf("oo:%d\n", defined_function());
1194 if (1)
1195 printf("oo:%d\n", defined_function());
1196 else
1197 printf("oo:%d\n", undefined_function());
1198 while (1) {
1199 printf("oow:%d\n", defined_function());
1200 break;
1201 printf("oow:%d\n", undefined_function());
1203 j = 1;
1204 /* Following is a switch without {} block intentionally. */
1205 switch (j)
1206 case 1: break;
1207 printf ("oos:%d\n", defined_function());
1208 /* The following break shouldn't lead to disabled code after
1209 the while. */
1210 while (1)
1211 break;
1212 printf ("ool1:%d\n", defined_function());
1213 /* Same for the other types of loops. */
1215 break;
1216 while (1);
1217 printf ("ool2:%d\n", defined_function());
1218 for (;;)
1219 break;
1220 printf ("ool3:%d\n", defined_function());
1221 /* Normal {} blocks without controlling statements
1222 shouldn't reactivate code emission */
1223 while (1) {
1225 break;
1227 printf ("ool4:%d\n", undefined_function());
1229 j = 1;
1230 while (j) {
1231 if (j == 0)
1232 break; /* this break shouldn't disable code outside the if. */
1233 printf("ool5:%d\n", defined_function());
1234 j--;
1237 j = 1;
1238 while (j) {
1239 if (1)
1240 j--;
1241 else
1242 breakhere: break;
1243 printf("ool6:%d\n", defined_function());
1244 goto breakhere;
1247 /* Test that constants in logical && are optimized: */
1248 i = 0 && undefined_function();
1249 i = defined_function() && 0 && undefined_function();
1250 if (0 && undefined_function())
1251 undefined_function();
1252 if (defined_function() && 0)
1253 undefined_function();
1254 if (0 && 0)
1255 undefined_function();
1256 if (defined_function() && 0 && undefined_function())
1257 undefined_function();
1258 /* The same for || : */
1259 i = 1 || undefined_function();
1260 i = defined_function() || 1 || undefined_function();
1261 if (1 || undefined_function())
1263 else
1264 undefined_function();
1265 if (defined_function() || 1)
1267 else
1268 undefined_function();
1269 if (1 || 1)
1271 else
1272 undefined_function();
1273 if (defined_function() || 1 || undefined_function())
1275 else
1276 undefined_function();
1278 if (defined_function() && 0)
1279 refer_to_undefined();
1281 if (1)
1282 return;
1283 printf ("oor:%d\n", undefined_function());
1286 int defined_function(void)
1288 static int i = 40;
1289 return i++;
1292 /* GCC accepts that */
1293 static int tab_reinit[];
1294 static int tab_reinit[10];
1296 //int cinit1; /* a global variable can be defined several times without error ! */
1297 int cinit1;
1298 int cinit1;
1299 int cinit1 = 0;
1300 int *cinit2 = (int []){3, 2, 1};
1302 void compound_literal_test(void)
1304 int *p, i;
1305 char *q, *q3;
1307 printf("compound_test:\n");
1309 p = (int []){1, 2, 3};
1310 for(i=0;i<3;i++)
1311 printf(" %d", p[i]);
1312 printf("\n");
1314 for(i=0;i<3;i++)
1315 printf("%d", cinit2[i]);
1316 printf("\n");
1318 q = "tralala1";
1319 printf("q1=%s\n", q);
1321 q = (char *){ "tralala2" };
1322 printf("q2=%s\n", q);
1324 q3 = (char *){ q };
1325 printf("q3=%s\n", q3);
1327 q = (char []){ "tralala3" };
1328 printf("q4=%s\n", q);
1330 #ifdef ALL_ISOC99
1331 p = (int []){1, 2, cinit1 + 3};
1332 for(i=0;i<3;i++)
1333 printf(" %d", p[i]);
1334 printf("\n");
1336 for(i=0;i<3;i++) {
1337 p = (int []){1, 2, 4 + i};
1338 printf("%d %d %d\n",
1339 p[0],
1340 p[1],
1341 p[2]);
1343 #endif
1346 /* K & R protos */
1348 kr_func1(a, b)
1350 return a + b;
1353 int kr_func2(a, b)
1355 return a + b;
1358 kr_test()
1360 printf("kr_test:\n");
1361 printf("func1=%d\n", kr_func1(3, 4));
1362 printf("func2=%d\n", kr_func2(3, 4));
1363 return 0;
1366 void num(int n)
1368 char *tab, *p;
1369 tab = (char*)malloc(20);
1370 p = tab;
1371 while (1) {
1372 *p = 48 + (n % 10);
1373 p++;
1374 n = n / 10;
1375 if (n == 0)
1376 break;
1378 while (p != tab) {
1379 p--;
1380 printf("%c", *p);
1382 printf("\n");
1383 free(tab);
1386 /* structure assignment tests */
1387 struct structa1 {
1388 int f1;
1389 char f2;
1392 struct structa1 ssta1;
1394 void struct_assign_test1(struct structa1 s1, int t, float f)
1396 printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
1399 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1401 s1.f1 += t;
1402 s1.f2 -= t;
1403 return s1;
1406 void struct_assign_test(void)
1408 struct S {
1409 struct structa1 lsta1, lsta2;
1410 int i;
1411 } s, *ps;
1413 ps = &s;
1414 ps->i = 4;
1415 #if 0
1416 printf("struct_assign_test:\n");
1418 s.lsta1.f1 = 1;
1419 s.lsta1.f2 = 2;
1420 printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
1421 s.lsta2 = s.lsta1;
1422 printf("%d %d\n", s.lsta2.f1, s.lsta2.f2);
1423 #else
1424 s.lsta2.f1 = 1;
1425 s.lsta2.f2 = 2;
1426 #endif
1427 struct_assign_test1(ps->lsta2, 3, 4.5);
1429 printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
1430 ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
1431 printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
1433 static struct {
1434 void (*elem)();
1435 } t[] = {
1436 /* XXX: we should allow this even without braces */
1437 { struct_assign_test }
1439 printf("%d\n", struct_assign_test == t[0].elem);
1442 /* casts to short/char */
1444 void cast1(char a, short b, unsigned char c, unsigned short d)
1446 printf("%d %d %d %d\n", a, b, c, d);
1449 char bcast;
1450 short scast;
1452 void cast_test()
1454 int a;
1455 char c;
1456 char tab[10];
1457 unsigned b,d;
1458 short s;
1459 char *p = NULL;
1460 p -= 0x700000000042;
1462 printf("cast_test:\n");
1463 a = 0xfffff;
1464 cast1(a, a, a, a);
1465 a = 0xffffe;
1466 printf("%d %d %d %d\n",
1467 (char)(a + 1),
1468 (short)(a + 1),
1469 (unsigned char)(a + 1),
1470 (unsigned short)(a + 1));
1471 printf("%d %d %d %d\n",
1472 (char)0xfffff,
1473 (short)0xfffff,
1474 (unsigned char)0xfffff,
1475 (unsigned short)0xfffff);
1477 a = (bcast = 128) + 1;
1478 printf("%d\n", a);
1479 a = (scast = 65536) + 1;
1480 printf("%d\n", a);
1482 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1484 /* test cast from unsigned to signed short to int */
1485 b = 0xf000;
1486 d = (short)b;
1487 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1488 b = 0xf0f0;
1489 d = (char)b;
1490 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1492 /* test implicit int casting for array accesses */
1493 c = 0;
1494 tab[1] = 2;
1495 tab[c] = 1;
1496 printf("%d %d\n", tab[0], tab[1]);
1498 /* test implicit casting on some operators */
1499 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1500 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1501 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1503 /* from pointer to integer types */
1504 printf("%d %d %ld %ld %lld %lld\n",
1505 (int)p, (unsigned int)p,
1506 (long)p, (unsigned long)p,
1507 (long long)p, (unsigned long long)p);
1509 /* from integers to pointers */
1510 printf("%p %p %p %p\n",
1511 (void *)a, (void *)b, (void *)c, (void *)d);
1514 /* initializers tests */
1515 struct structinit1 {
1516 int f1;
1517 char f2;
1518 short f3;
1519 int farray[3];
1522 int sinit1 = 2;
1523 int sinit2 = { 3 };
1524 int sinit3[3] = { 1, 2, {{3}}, };
1525 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1526 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1527 int sinit6[] = { 1, 2, 3 };
1528 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1529 char sinit8[] = "hello" "trala";
1531 struct structinit1 sinit9 = { 1, 2, 3 };
1532 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1533 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1534 #ifdef ALL_ISOC99
1535 .farray[0] = 10,
1536 .farray[1] = 11,
1537 .farray[2] = 12,
1538 #endif
1541 char *sinit12 = "hello world";
1542 char *sinit13[] = {
1543 "test1",
1544 "test2",
1545 "test3",
1547 char sinit14[10] = { "abc" };
1548 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1550 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1552 struct bar {
1553 char *s;
1554 int len;
1555 } sinit17[] = {
1556 "a1", 4,
1557 "a2", 1
1560 int sinit18[10] = {
1561 [2 ... 5] = 20,
1563 [8] = 10,
1566 struct complexinit0 {
1567 int a;
1568 int b;
1571 struct complexinit {
1572 int a;
1573 const struct complexinit0 *b;
1576 const static struct complexinit cix[] = {
1577 [0] = {
1578 .a = 2000,
1579 .b = (const struct complexinit0[]) {
1580 { 2001, 2002 },
1581 { 2003, 2003 },
1587 struct complexinit2 {
1588 int a;
1589 int b[];
1592 struct complexinit2 cix20;
1594 struct complexinit2 cix21 = {
1595 .a = 3000,
1596 .b = { 3001, 3002, 3003 }
1599 struct complexinit2 cix22 = {
1600 .a = 4000,
1601 .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1604 typedef int arrtype1[];
1605 arrtype1 sinit19 = {1};
1606 arrtype1 sinit20 = {2,3};
1607 typedef int arrtype2[3];
1608 arrtype2 sinit21 = {4};
1609 arrtype2 sinit22 = {5,6,7};
1611 /* Address comparisons of non-weak symbols with zero can be const-folded */
1612 int sinit23[2] = { "astring" ? sizeof("astring") : -1,
1613 &sinit23 ? 42 : -1 };
1615 void init_test(void)
1617 int linit1 = 2;
1618 int linit2 = { 3 };
1619 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1620 int linit6[] = { 1, 2, 3 };
1621 int i, j;
1622 char linit8[] = "hello" "trala";
1623 int linit12[10] = { 1, 2 };
1624 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1625 char linit14[10] = "abc";
1626 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1627 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1628 int linit17 = sizeof(linit17);
1629 int zero = 0;
1630 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1631 int linit18[2] = {&zero ? 1 : -1, zero ? -1 : 1 };
1633 printf("init_test:\n");
1635 printf("sinit1=%d\n", sinit1);
1636 printf("sinit2=%d\n", sinit2);
1637 printf("sinit3=%d %d %d %d\n",
1638 sizeof(sinit3),
1639 sinit3[0],
1640 sinit3[1],
1641 sinit3[2]
1643 printf("sinit6=%d\n", sizeof(sinit6));
1644 printf("sinit7=%d %d %d %d\n",
1645 sizeof(sinit7),
1646 sinit7[0],
1647 sinit7[1],
1648 sinit7[2]
1650 printf("sinit8=%s\n", sinit8);
1651 printf("sinit9=%d %d %d\n",
1652 sinit9.f1,
1653 sinit9.f2,
1654 sinit9.f3
1656 printf("sinit10=%d %d %d\n",
1657 sinit10.f1,
1658 sinit10.f2,
1659 sinit10.f3
1661 printf("sinit11=%d %d %d %d %d %d\n",
1662 sinit11.f1,
1663 sinit11.f2,
1664 sinit11.f3,
1665 sinit11.farray[0],
1666 sinit11.farray[1],
1667 sinit11.farray[2]
1670 for(i=0;i<3;i++)
1671 for(j=0;j<2;j++)
1672 printf("[%d][%d] = %d %d %d\n",
1673 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1674 printf("linit1=%d\n", linit1);
1675 printf("linit2=%d\n", linit2);
1676 printf("linit6=%d\n", sizeof(linit6));
1677 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1679 printf("sinit12=%s\n", sinit12);
1680 printf("sinit13=%d %s %s %s\n",
1681 sizeof(sinit13),
1682 sinit13[0],
1683 sinit13[1],
1684 sinit13[2]);
1685 printf("sinit14=%s\n", sinit14);
1687 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1688 printf("\n");
1689 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1690 printf("\n");
1691 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1692 printf("\n");
1693 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1694 printf("\n");
1695 printf("%d %d %d %d\n",
1696 linit16.a1,
1697 linit16.a2,
1698 linit16.a3,
1699 linit16.a4);
1700 /* test that initialisation is done after variable declare */
1701 printf("linit17=%d\n", linit17);
1702 printf("sinit15=%d\n", sinit15[0]);
1703 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1704 printf("sinit17=%s %d %s %d\n",
1705 sinit17[0].s, sinit17[0].len,
1706 sinit17[1].s, sinit17[1].len);
1707 for(i=0;i<10;i++)
1708 printf("%x ", sinit18[i]);
1709 printf("\n");
1710 /* complex init check */
1711 printf("cix: %d %d %d %d %d %d %d\n",
1712 cix[0].a,
1713 cix[0].b[0].a, cix[0].b[0].b,
1714 cix[0].b[1].a, cix[0].b[1].b,
1715 cix[0].b[2].a, cix[0].b[2].b);
1716 printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
1717 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
1719 printf("arrtype1: %d %d %d\n", sinit19[0], sinit20[0], sinit20[1]);
1720 printf("arrtype2: %d %d\n", sizeof(sinit19), sizeof(sinit20));
1721 printf("arrtype3: %d %d %d\n", sinit21[0], sinit21[1], sinit21[2]);
1722 printf("arrtype4: %d %d %d\n", sinit22[0], sinit22[1], sinit22[2]);
1723 printf("arrtype5: %d %d\n", sizeof(sinit21), sizeof(sinit22));
1724 printf("arrtype6: %d\n", sizeof(arrtype2));
1726 printf("sinit23= %d %d\n", sinit23[0], sinit23[1]);
1727 printf("linit18= %d %d\n", linit18[0], linit18[1]);
1731 void switch_test()
1733 int i;
1735 for(i=0;i<15;i++) {
1736 switch(i) {
1737 case 0:
1738 case 1:
1739 printf("a");
1740 break;
1741 default:
1742 printf("%d", i);
1743 break;
1744 case 8 ... 12:
1745 printf("c");
1746 break;
1747 case 3:
1748 printf("b");
1749 break;
1750 case 0xc33c6b9fU:
1751 case 0x7c9eeeb9U:
1752 break;
1755 printf("\n");
1758 /* ISOC99 _Bool type */
1759 void c99_bool_test(void)
1761 #ifdef BOOL_ISOC99
1762 int a;
1763 _Bool b;
1765 printf("bool_test:\n");
1766 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1767 a = 3;
1768 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1769 b = 3;
1770 printf("b = %d\n", b);
1771 b++;
1772 printf("b = %d\n", b);
1773 #endif
1776 void bitfield_test(void)
1778 int a;
1779 short sa;
1780 unsigned char ca;
1781 struct sbf1 {
1782 int f1 : 3;
1783 int : 2;
1784 int f2 : 1;
1785 int : 0;
1786 int f3 : 5;
1787 int f4 : 7;
1788 unsigned int f5 : 7;
1789 } st1;
1790 printf("bitfield_test:");
1791 printf("sizeof(st1) = %d\n", sizeof(st1));
1793 st1.f1 = 3;
1794 st1.f2 = 1;
1795 st1.f3 = 15;
1796 a = 120;
1797 st1.f4 = a;
1798 st1.f5 = a;
1799 st1.f5++;
1800 printf("%d %d %d %d %d\n",
1801 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1802 sa = st1.f5;
1803 ca = st1.f5;
1804 printf("%d %d\n", sa, ca);
1806 st1.f1 = 7;
1807 if (st1.f1 == -1)
1808 printf("st1.f1 == -1\n");
1809 else
1810 printf("st1.f1 != -1\n");
1811 if (st1.f2 == -1)
1812 printf("st1.f2 == -1\n");
1813 else
1814 printf("st1.f2 != -1\n");
1816 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1817 long-long bitfields whose size is not bigger than int */
1818 struct sbf2 {
1819 long long f1 : 45;
1820 long long : 2;
1821 long long f2 : 35;
1822 unsigned long long f3 : 38;
1823 } st2;
1824 st2.f1 = 0x123456789ULL;
1825 a = 120;
1826 st2.f2 = (long long)a << 25;
1827 st2.f3 = a;
1828 st2.f2++;
1829 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
1832 #ifdef __x86_64__
1833 #define FLOAT_FMT "%f\n"
1834 #else
1835 /* x86's float isn't compatible with GCC */
1836 #define FLOAT_FMT "%.5f\n"
1837 #endif
1839 /* declare strto* functions as they are C99 */
1840 double strtod(const char *nptr, char **endptr);
1842 #if defined(_WIN32)
1843 float strtof(const char *nptr, char **endptr) {return (float)strtod(nptr, endptr);}
1844 LONG_DOUBLE strtold(const char *nptr, char **endptr) {return (LONG_DOUBLE)strtod(nptr, endptr);}
1845 #else
1846 float strtof(const char *nptr, char **endptr);
1847 LONG_DOUBLE strtold(const char *nptr, char **endptr);
1848 #endif
1850 #define FTEST(prefix, typename, type, fmt)\
1851 void prefix ## cmp(type a, type b)\
1853 printf("%d %d %d %d %d %d\n",\
1854 a == b,\
1855 a != b,\
1856 a < b,\
1857 a > b,\
1858 a >= b,\
1859 a <= b);\
1860 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1863 a + b,\
1864 a - b,\
1865 a * b,\
1866 a / b,\
1867 -a);\
1868 printf(fmt "\n", ++a);\
1869 printf(fmt "\n", a++);\
1870 printf(fmt "\n", a);\
1871 b = 0;\
1872 printf("%d %d\n", !a, !b);\
1874 void prefix ## fcast(type a)\
1876 float fa;\
1877 double da;\
1878 LONG_DOUBLE la;\
1879 int ia;\
1880 long long llia;\
1881 unsigned int ua;\
1882 unsigned long long llua;\
1883 type b;\
1884 fa = a;\
1885 da = a;\
1886 la = a;\
1887 printf("ftof: %f %f %Lf\n", fa, da, la);\
1888 ia = (int)a;\
1889 llia = (long long)a;\
1890 a = (a >= 0) ? a : -a;\
1891 ua = (unsigned int)a;\
1892 llua = (unsigned long long)a;\
1893 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
1894 ia = -1234;\
1895 ua = 0x81234500;\
1896 llia = -0x123456789012345LL;\
1897 llua = 0xf123456789012345LLU;\
1898 b = ia;\
1899 printf("itof: " fmt "\n", b);\
1900 b = ua;\
1901 printf("utof: " fmt "\n", b);\
1902 b = llia;\
1903 printf("lltof: " fmt "\n", b);\
1904 b = llua;\
1905 printf("ulltof: " fmt "\n", b);\
1908 float prefix ## retf(type a) { return a; }\
1909 double prefix ## retd(type a) { return a; }\
1910 LONG_DOUBLE prefix ## retld(type a) { return a; }\
1912 void prefix ## call(void)\
1914 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1915 printf("double: %f\n", prefix ## retd(42.123456789));\
1916 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1917 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
1920 void prefix ## signed_zeros(void) \
1922 type x = 0.0, y = -0.0, n, p;\
1923 if (x == y)\
1924 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
1925 1.0 / x != 1.0 / y);\
1926 else\
1927 printf ("x != y; this is wrong!\n");\
1929 n = -x;\
1930 if (x == n)\
1931 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
1932 1.0 / x != 1.0 / n);\
1933 else\
1934 printf ("x != -x; this is wrong!\n");\
1936 p = +y;\
1937 if (x == p)\
1938 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
1939 1.0 / x != 1.0 / p);\
1940 else\
1941 printf ("x != +y; this is wrong!\n");\
1942 p = -y;\
1943 if (x == p)\
1944 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
1945 1.0 / x != 1.0 / p);\
1946 else\
1947 printf ("x != -y; this is wrong!\n");\
1949 void prefix ## test(void)\
1951 printf("testing '%s'\n", #typename);\
1952 prefix ## cmp(1, 2.5);\
1953 prefix ## cmp(2, 1.5);\
1954 prefix ## cmp(1, 1);\
1955 prefix ## fcast(234.6);\
1956 prefix ## fcast(-2334.6);\
1957 prefix ## call();\
1958 prefix ## signed_zeros();\
1961 FTEST(f, float, float, "%f")
1962 FTEST(d, double, double, "%f")
1963 FTEST(ld, long double, LONG_DOUBLE, "%Lf")
1965 double ftab1[3] = { 1.2, 3.4, -5.6 };
1968 void float_test(void)
1970 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
1971 float fa, fb;
1972 double da, db;
1973 int a;
1974 unsigned int b;
1976 printf("float_test:\n");
1977 printf("sizeof(float) = %d\n", sizeof(float));
1978 printf("sizeof(double) = %d\n", sizeof(double));
1979 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));
1980 ftest();
1981 dtest();
1982 ldtest();
1983 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1984 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1985 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1986 da = 123;
1987 printf("da=%f\n", da);
1988 fa = 123;
1989 printf("fa=%f\n", fa);
1990 a = 4000000000;
1991 da = a;
1992 printf("da = %f\n", da);
1993 b = 4000000000;
1994 db = b;
1995 printf("db = %f\n", db);
1996 #endif
1999 int fib(int n)
2001 if (n <= 2)
2002 return 1;
2003 else
2004 return fib(n-1) + fib(n-2);
2007 void funcptr_test()
2009 void (*func)(int);
2010 int a;
2011 struct {
2012 int dummy;
2013 void (*func)(int);
2014 } st1;
2015 long diff;
2017 printf("funcptr:\n");
2018 func = &num;
2019 (*func)(12345);
2020 func = num;
2021 a = 1;
2022 a = 1;
2023 func(12345);
2024 /* more complicated pointer computation */
2025 st1.func = num;
2026 st1.func(12346);
2027 printf("sizeof1 = %d\n", sizeof(funcptr_test));
2028 printf("sizeof2 = %d\n", sizeof funcptr_test);
2029 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
2030 printf("sizeof4 = %d\n", sizeof &funcptr_test);
2031 a = 0;
2032 func = num + a;
2033 diff = func - num;
2034 func(42);
2035 (func + diff)(42);
2036 (num + a)(43);
2039 void lloptest(long long a, long long b)
2041 unsigned long long ua, ub;
2043 ua = a;
2044 ub = b;
2045 /* arith */
2046 printf("arith: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2047 a + b,
2048 a - b,
2049 a * b);
2051 if (b != 0) {
2052 printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2053 a / b,
2054 a % b);
2057 /* binary */
2058 printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2059 a & b,
2060 a | b,
2061 a ^ b);
2063 /* tests */
2064 printf("test: %d %d %d %d %d %d\n",
2065 a == b,
2066 a != b,
2067 a < b,
2068 a > b,
2069 a >= b,
2070 a <= b);
2072 printf("utest: %d %d %d %d %d %d\n",
2073 ua == ub,
2074 ua != ub,
2075 ua < ub,
2076 ua > ub,
2077 ua >= ub,
2078 ua <= ub);
2080 /* arith2 */
2081 a++;
2082 b++;
2083 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2084 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a++, b++);
2085 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", --a, --b);
2086 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2087 b = ub = 0;
2088 printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
2091 void llshift(long long a, int b)
2093 printf("shift: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2094 (unsigned long long)a >> b,
2095 a >> b,
2096 a << b);
2097 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2098 (unsigned long long)a >> 3,
2099 a >> 3,
2100 a << 3);
2101 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2102 (unsigned long long)a >> 35,
2103 a >> 35,
2104 a << 35);
2107 void llfloat(void)
2109 float fa;
2110 double da;
2111 LONG_DOUBLE lda;
2112 long long la, lb, lc;
2113 unsigned long long ula, ulb, ulc;
2114 la = 0x12345678;
2115 ula = 0x72345678;
2116 la = (la << 20) | 0x12345;
2117 ula = ula << 33;
2118 printf("la=" LONG_LONG_FORMAT " ula=" ULONG_LONG_FORMAT "\n", la, ula);
2120 fa = la;
2121 da = la;
2122 lda = la;
2123 printf("lltof: %f %f %Lf\n", fa, da, lda);
2125 la = fa;
2126 lb = da;
2127 lc = lda;
2128 printf("ftoll: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", la, lb, lc);
2130 fa = ula;
2131 da = ula;
2132 lda = ula;
2133 printf("ulltof: %f %f %Lf\n", fa, da, lda);
2135 ula = fa;
2136 ulb = da;
2137 ulc = lda;
2138 printf("ftoull: " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT "\n", ula, ulb, ulc);
2141 long long llfunc1(int a)
2143 return a * 2;
2146 struct S {
2147 int id;
2148 char item;
2151 long long int value(struct S *v)
2153 return ((long long int)v->item);
2156 long long llfunc2(long long x, long long y, int z)
2158 return x * y * z;
2161 void longlong_test(void)
2163 long long a, b, c;
2164 int ia;
2165 unsigned int ua;
2166 printf("longlong_test:\n");
2167 printf("sizeof(long long) = %d\n", sizeof(long long));
2168 ia = -1;
2169 ua = -2;
2170 a = ia;
2171 b = ua;
2172 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2173 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %Lx\n",
2174 (long long)1,
2175 (long long)-2,
2176 1LL,
2177 0x1234567812345679);
2178 a = llfunc1(-3);
2179 printf(LONG_LONG_FORMAT "\n", a);
2181 lloptest(1000, 23);
2182 lloptest(0xff, 0x1234);
2183 b = 0x72345678 << 10;
2184 lloptest(-3, b);
2185 llshift(0x123, 5);
2186 llshift(-23, 5);
2187 b = 0x72345678LL << 10;
2188 llshift(b, 47);
2190 llfloat();
2191 #if 1
2192 b = 0x12345678;
2193 a = -1;
2194 c = a + b;
2195 printf("%Lx\n", c);
2196 #endif
2198 /* long long reg spill test */
2200 struct S a;
2202 a.item = 3;
2203 printf("%lld\n", value(&a));
2205 lloptest(0x80000000, 0);
2208 long long *p, v, **pp;
2209 v = 1;
2210 p = &v;
2211 p[0]++;
2212 printf("another long long spill test : %lld\n", *p);
2213 pp = &p;
2215 v = llfunc2(**pp, **pp, ia);
2216 printf("a long long function (arm-)reg-args test : %lld\n", v);
2218 a = 68719476720LL;
2219 b = 4294967295LL;
2220 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
2222 printf(LONG_LONG_FORMAT "\n", 0x123456789LLU);
2224 /* long long pointer deref in argument passing test */
2225 a = 0x123;
2226 long long *p = &a;
2227 llshift(*p, 5);
2230 void manyarg_test(void)
2232 LONG_DOUBLE ld = 1234567891234LL;
2233 printf("manyarg_test:\n");
2234 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2235 1, 2, 3, 4, 5, 6, 7, 8,
2236 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2237 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2238 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2239 1, 2, 3, 4, 5, 6, 7, 8,
2240 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2241 1234567891234LL, 987654321986LL,
2242 42.0, 43.0);
2243 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2244 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2245 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2246 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2247 1234567891234LL, 987654321986LL,
2248 42.0, 43.0);
2249 printf("%d %d %d %d %d %d %d %d %Lf\n",
2250 1, 2, 3, 4, 5, 6, 7, 8, ld);
2251 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2252 LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n",
2253 1, 2, 3, 4, 5, 6, 7, 8,
2254 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2255 1234567891234LL, 987654321986LL,
2256 42.0, 43.0, ld);
2257 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2258 "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n",
2259 1, 2, 3, 4, 5, 6, 7, 8,
2260 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2261 ld, 1234567891234LL, 987654321986LL,
2262 42.0, 43.0, ld);
2265 void vprintf1(const char *fmt, ...)
2267 va_list ap, aq;
2268 const char *p;
2269 int c, i;
2270 double d;
2271 long long ll;
2272 LONG_DOUBLE ld;
2274 va_start(aq, fmt);
2275 va_copy(ap, aq);
2277 p = fmt;
2278 for(;;) {
2279 c = *p;
2280 if (c == '\0')
2281 break;
2282 p++;
2283 if (c == '%') {
2284 c = *p;
2285 switch(c) {
2286 case '\0':
2287 goto the_end;
2288 case 'd':
2289 i = va_arg(ap, int);
2290 printf("%d", i);
2291 break;
2292 case 'f':
2293 d = va_arg(ap, double);
2294 printf("%f", d);
2295 break;
2296 case 'l':
2297 ll = va_arg(ap, long long);
2298 printf(LONG_LONG_FORMAT, ll);
2299 break;
2300 case 'F':
2301 ld = va_arg(ap, LONG_DOUBLE);
2302 printf("%Lf", ld);
2303 break;
2305 p++;
2306 } else {
2307 putchar(c);
2310 the_end:
2311 va_end(aq);
2312 va_end(ap);
2315 struct myspace {
2316 short int profile;
2319 void stdarg_for_struct(struct myspace bob, ...)
2321 struct myspace george, bill;
2322 va_list ap;
2323 short int validate;
2325 va_start(ap, bob);
2326 bill = va_arg(ap, struct myspace);
2327 george = va_arg(ap, struct myspace);
2328 validate = va_arg(ap, int);
2329 printf("stdarg_for_struct: %d %d %d %d\n",
2330 bob.profile, bill.profile, george.profile, validate);
2331 va_end(ap);
2334 void stdarg_for_libc(const char *fmt, ...)
2336 va_list args;
2337 va_start(args, fmt);
2338 vprintf(fmt, args);
2339 va_end(args);
2342 void stdarg_test(void)
2344 LONG_DOUBLE ld = 1234567891234LL;
2345 struct myspace bob;
2347 vprintf1("%d %d %d\n", 1, 2, 3);
2348 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2349 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2350 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2351 vprintf1("%d %f %l %F %d %f %l %F\n",
2352 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2353 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2354 1, 2, 3, 4, 5, 6, 7, 8,
2355 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2356 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2357 1, 2, 3, 4, 5, 6, 7, 8,
2358 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2359 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2360 "%l %l %f %f\n",
2361 1, 2, 3, 4, 5, 6, 7, 8,
2362 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2363 1234567891234LL, 987654321986LL,
2364 42.0, 43.0);
2365 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2366 "%l %l %f %f\n",
2367 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2368 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2369 1234567891234LL, 987654321986LL,
2370 42.0, 43.0);
2371 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2372 1, 2, 3, 4, 5, 6, 7, 8, ld);
2373 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2374 "%l %l %f %f %F\n",
2375 1, 2, 3, 4, 5, 6, 7, 8,
2376 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2377 1234567891234LL, 987654321986LL,
2378 42.0, 43.0, ld);
2379 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2380 "%F %l %l %f %f %F\n",
2381 1, 2, 3, 4, 5, 6, 7, 8,
2382 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2383 ld, 1234567891234LL, 987654321986LL,
2384 42.0, 43.0, ld);
2386 bob.profile = 42;
2387 stdarg_for_struct(bob, bob, bob, bob.profile);
2388 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2391 void whitespace_test(void)
2393 char *str;
2395 \f\v #if 1
2396 pri\
2397 ntf("whitspace:\n");\f\v
2398 #endif
2399 pf("N=%d\n", 2);
2401 #ifdef CORRECT_CR_HANDLING
2402 pri\
2403 ntf("aaa=%d\n", 3);
2404 #endif
2406 pri\
2408 ntf("min=%d\n", 4);
2410 #ifdef ACCEPT_CR_IN_STRINGS
2411 printf("len1=%d\n", strlen("
2412 "));
2413 #ifdef CORRECT_CR_HANDLING
2414 str = "
2416 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
2417 #endif
2418 printf("len1=%d\n", strlen(" a
2419 "));
2420 #endif /* ACCEPT_CR_IN_STRINGS */
2423 int reltab[3] = { 1, 2, 3 };
2425 int *rel1 = &reltab[1];
2426 int *rel2 = &reltab[2];
2428 void getmyaddress(void)
2430 printf("in getmyaddress\n");
2432 unsigned long theaddress = (unsigned long)getmyaddress;
2433 void relocation_test(void)
2435 void (*fptr)(void) = (void (*)(void))theaddress;
2436 printf("*rel1=%d\n", *rel1);
2437 printf("*rel2=%d\n", *rel2);
2438 fptr();
2441 void old_style_f(a,b,c)
2442 int a, b;
2443 double c;
2445 printf("a=%d b=%d b=%f\n", a, b, c);
2448 void decl_func1(int cmpfn())
2450 printf("cmpfn=%lx\n", (long)cmpfn);
2453 void decl_func2(cmpfn)
2454 int cmpfn();
2456 printf("cmpfn=%lx\n", (long)cmpfn);
2459 void old_style_function(void)
2461 old_style_f((void *)1, 2, 3.0);
2462 decl_func1(NULL);
2463 decl_func2(NULL);
2466 void alloca_test()
2468 #if defined __i386__ || defined __x86_64__ || defined __arm__
2469 char *p = alloca(16);
2470 strcpy(p,"123456789012345");
2471 printf("alloca: p is %s\n", p);
2472 char *demo = "This is only a test.\n";
2473 /* Test alloca embedded in a larger expression */
2474 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
2475 #endif
2478 void *bounds_checking_is_enabled()
2480 char ca[10], *cp = ca-1;
2481 return (ca != cp + 1) ? cp : NULL;
2484 typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
2486 void c99_vla_test(int size1, int size2)
2488 #if defined __i386__ || defined __x86_64__
2489 int size = size1 * size2;
2490 int tab1[size][2], tab2[10][2];
2491 void *tab1_ptr, *tab2_ptr, *bad_ptr;
2493 /* "size" should have been 'captured' at tab1 declaration,
2494 so modifying it should have no effect on VLA behaviour. */
2495 size = size-1;
2497 printf("Test C99 VLA 1 (sizeof): ");
2498 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
2499 tab1_ptr = tab1;
2500 tab2_ptr = tab2;
2501 printf("Test C99 VLA 2 (ptrs subtract): ");
2502 printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2503 printf("Test C99 VLA 3 (ptr add): ");
2504 printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2505 printf("Test C99 VLA 4 (ptr access): ");
2506 tab1[size1][1] = 42;
2507 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2509 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2510 if (bad_ptr = bounds_checking_is_enabled()) {
2511 int *t1 = &tab1[size1 * size2 - 1][3];
2512 int *t2 = &tab2[9][3];
2513 printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
2514 printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
2516 char*c1 = 1 + sizeof(tab1) + (char*)tab1;
2517 char*c2 = 1 + sizeof(tab2) + (char*)tab2;
2518 printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
2519 printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
2521 int *i1 = tab1[-1];
2522 int *i2 = tab2[-1];
2523 printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
2524 printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
2526 int *x1 = tab1[size1 * size2 + 1];
2527 int *x2 = tab2[10 + 1];
2528 printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
2529 printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
2530 } else {
2531 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2533 printf("\n");
2534 #endif
2537 #ifndef __TINYC__
2538 typedef __SIZE_TYPE__ uintptr_t;
2539 #endif
2541 void sizeof_test(void)
2543 int a;
2544 int **ptr;
2546 printf("sizeof(int) = %d\n", sizeof(int));
2547 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2548 printf("sizeof(long) = %d\n", sizeof(long));
2549 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2550 printf("sizeof(short) = %d\n", sizeof(short));
2551 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2552 printf("sizeof(char) = %d\n", sizeof(char));
2553 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2554 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2555 a = 1;
2556 printf("sizeof(a++) = %d\n", sizeof a++);
2557 printf("a=%d\n", a);
2558 ptr = NULL;
2559 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
2561 /* The type of sizeof should be as large as a pointer, actually
2562 it should be size_t. */
2563 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2564 uintptr_t t = 1;
2565 uintptr_t t2;
2566 /* Effectively <<32, but defined also on 32bit machines. */
2567 t <<= 16;
2568 t <<= 16;
2569 t++;
2570 /* This checks that sizeof really can be used to manipulate
2571 uintptr_t objects, without truncation. */
2572 t2 = t & -sizeof(uintptr_t);
2573 printf ("%lu %lu\n", t, t2);
2575 /* some alignof tests */
2576 printf("__alignof__(int) = %d\n", __alignof__(int));
2577 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2578 printf("__alignof__(short) = %d\n", __alignof__(short));
2579 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2580 printf("__alignof__(char) = %d\n", __alignof__(char));
2581 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2582 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
2584 /* sizes of VLAs need to be evaluated even inside sizeof: */
2585 a = 2;
2586 printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a]));
2587 /* And checking if sizeof compound literal works. Parenthesized: */
2588 printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n",
2589 sizeof( (struct {int i; int j;}){4,5} ));
2590 /* And as direct sizeof argument (as unary expression): */
2591 printf("sizeof (struct {short i; short j;}){4,5} = %d\n",
2592 sizeof (struct {short i; short j;}){4,5} );
2594 /* sizeof(x && y) should be sizeof(int), even if constant
2595 evaluating is possible. */
2596 printf("sizeof(t && 0) = %d\n", sizeof(t && 0));
2597 printf("sizeof(1 && 1) = %d\n", sizeof(1 && 1));
2598 printf("sizeof(t || 1) = %d\n", sizeof(t || 1));
2599 printf("sizeof(0 || 0) = %d\n", sizeof(0 || 0));
2602 void typeof_test(void)
2604 double a;
2605 typeof(a) b;
2606 typeof(float) c;
2608 a = 1.5;
2609 b = 2.5;
2610 c = 3.5;
2611 printf("a=%f b=%f c=%f\n", a, b, c);
2615 struct hlist_node;
2616 struct hlist_head {
2617 struct hlist_node *first, *last;
2620 void statement_expr_test(void)
2622 int a, i;
2624 /* Basic stmt expr test */
2625 a = 0;
2626 for(i=0;i<10;i++) {
2627 a += 1 +
2628 ( { int b, j;
2629 b = 0;
2630 for(j=0;j<5;j++)
2631 b += j; b;
2632 } );
2634 printf("a=%d\n", a);
2636 /* Test that symbols aren't freed prematurely.
2637 With SYM_DEBUG valgrind will show a read from a freed
2638 symbol, and tcc will show an (invalid) warning on the initialization
2639 of 'ptr' below, if symbols are popped after the stmt expr. */
2640 void *v = (void*)39;
2641 typeof(({
2642 (struct hlist_node *)v;
2643 })) x;
2644 typeof (x)
2645 ptr = (struct hlist_node *)v;
2647 /* This part used to segfault when symbols were popped prematurely.
2648 The symbols for the static local would be overwritten with
2649 helper symbols from the pre-processor expansions in between. */
2650 #define some_attr __attribute__((aligned(1)))
2651 #define tps(str) ({ \
2652 static const char *t some_attr = str; \
2653 t; \
2655 printf ("stmtexpr: %s %s\n",
2656 tps("somerandomlongstring"),
2657 tps("anotherlongstring"));
2659 /* Test that the three decls of 't' don't interact. */
2660 int t = 40;
2661 int b = ({ int t = 41; t; });
2662 int c = ({ int t = 42; t; });
2664 /* Test that aggregate return values work. */
2665 struct hlist_head h
2666 = ({
2667 typedef struct hlist_head T;
2668 long pre = 48;
2669 T t = { (void*)43, (void*)44 };
2670 long post = 49;
2673 printf ("stmtexpr: %d %d %d\n", t, b, c);
2674 printf ("stmtexpr: %ld %ld\n", (long)h.first, (long)h.last);
2677 void local_label_test(void)
2679 int a;
2680 goto l1;
2682 a = 1 + ({
2683 __label__ l1, l2, l3, l4;
2684 goto l1;
2686 printf("aa1\n");
2687 goto l3;
2689 printf("aa3\n");
2690 goto l4;
2692 printf("aa2\n");
2693 goto l2;
2694 l3:;
2697 printf("a=%d\n", a);
2698 return;
2700 printf("bb1\n");
2701 goto l2;
2703 printf("bb2\n");
2704 goto l4;
2707 /* inline assembler test */
2708 #if defined(__i386__) || defined(__x86_64__)
2710 /* from linux kernel */
2711 static char * strncat1(char * dest,const char * src,size_t count)
2713 long d0, d1, d2, d3;
2714 __asm__ __volatile__(
2715 "repne\n\t"
2716 "scasb\n\t"
2717 "dec %1\n\t"
2718 "mov %8,%3\n"
2719 "1:\tdec %3\n\t"
2720 "js 2f\n\t"
2721 "lodsb\n\t"
2722 "stosb\n\t"
2723 "testb %%al,%%al\n\t"
2724 "jne 1b\n"
2725 "2:\txor %2,%2\n\t"
2726 "stosb"
2727 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2728 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2729 : "memory");
2730 return dest;
2733 static char * strncat2(char * dest,const char * src,size_t count)
2735 long d0, d1, d2, d3;
2736 __asm__ __volatile__(
2737 "repne scasb\n\t" /* one-line repne prefix + string op */
2738 "dec %1\n\t"
2739 "mov %8,%3\n"
2740 "1:\tdec %3\n\t"
2741 "js 2f\n\t"
2742 "lodsb\n\t"
2743 "stosb\n\t"
2744 "testb %%al,%%al\n\t"
2745 "jne 1b\n"
2746 "2:\txor %2,%2\n\t"
2747 "stosb"
2748 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2749 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2750 : "memory");
2751 return dest;
2754 static inline void * memcpy1(void * to, const void * from, size_t n)
2756 long d0, d1, d2;
2757 __asm__ __volatile__(
2758 "rep ; movsl\n\t"
2759 "testb $2,%b4\n\t"
2760 "je 1f\n\t"
2761 "movsw\n"
2762 "1:\ttestb $1,%b4\n\t"
2763 "je 2f\n\t"
2764 "movsb\n"
2765 "2:"
2766 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2767 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2768 : "memory");
2769 return (to);
2772 static inline void * memcpy2(void * to, const void * from, size_t n)
2774 long d0, d1, d2;
2775 __asm__ __volatile__(
2776 "rep movsl\n\t" /* one-line rep prefix + string op */
2777 "testb $2,%b4\n\t"
2778 "je 1f\n\t"
2779 "movsw\n"
2780 "1:\ttestb $1,%b4\n\t"
2781 "je 2f\n\t"
2782 "movsb\n"
2783 "2:"
2784 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2785 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2786 : "memory");
2787 return (to);
2790 static __inline__ void sigaddset1(unsigned int *set, int _sig)
2792 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2795 static __inline__ void sigdelset1(unsigned int *set, int _sig)
2797 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc", "flags");
2800 static __inline__ __const__ unsigned int swab32(unsigned int x)
2802 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2803 "rorl $16,%0\n\t" /* swap words */
2804 "xchgb %b0,%h0" /* swap higher bytes */
2805 :"=" "q" (x)
2806 : "0" (x));
2807 return x;
2810 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
2812 unsigned long long res;
2813 #ifdef __x86_64__
2814 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
2815 but still test the 32bit->64bit mull. */
2816 unsigned int resh, resl;
2817 __asm__("mull %2" : "=a" (resl), "=d" (resh) : "a" (a), "r" (b));
2818 res = ((unsigned long long)resh << 32) | resl;
2819 #else
2820 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
2821 #endif
2822 return res;
2825 static __inline__ unsigned long long inc64(unsigned long long a)
2827 unsigned long long res;
2828 #ifdef __x86_64__
2829 /* Using the A constraint is wrong, and increments are tested
2830 elsewere. */
2831 res = a + 1;
2832 #else
2833 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
2834 #endif
2835 return res;
2838 struct struct123 {
2839 int a;
2840 int b;
2842 struct struct1231 {
2843 unsigned long addr;
2846 unsigned long mconstraint_test(struct struct1231 *r)
2848 unsigned long ret;
2849 unsigned int a[2];
2850 a[0] = 0;
2851 __asm__ volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
2852 : "=&r" (ret), "=m" (a)
2853 : "m" (*(struct struct123 *)r->addr));
2854 return ret + a[0];
2857 #ifdef __x86_64__
2858 int fls64(unsigned long long x)
2860 int bitpos = -1;
2861 asm("bsrq %1,%q0"
2862 : "+r" (bitpos)
2863 : "rm" (x));
2864 return bitpos + 1;
2866 #endif
2868 void other_constraints_test(void)
2870 unsigned long ret;
2871 int var;
2872 __asm__ volatile ("movq %P1,%0" : "=r" (ret) : "p" (&var));
2873 printf ("oc1: %d\n", ret == (unsigned long)&var);
2876 /* Test global asm blocks playing with aliases. */
2877 void base_func(void)
2879 printf ("asmc: base\n");
2882 extern void override_func1 (void);
2883 extern void override_func2 (void);
2885 asm(".weak override_func1\n.set override_func1, base_func");
2886 asm(".set override_func1, base_func");
2887 asm(".set override_func2, base_func");
2889 void override_func2 (void)
2891 printf ("asmc: override2\n");
2894 /* This checks a construct used by the linux kernel to encode
2895 references to strings by PC relative references. */
2896 extern int bug_table[] __attribute__((section("__bug_table")));
2897 char * get_asm_string (void)
2899 extern int some_symbol;
2900 asm volatile (".globl some_symbol\n"
2901 "jmp .+6\n"
2902 "1:\n"
2903 "some_symbol: .long 0\n"
2904 ".pushsection __bug_table, \"a\"\n"
2905 ".globl bug_table\n"
2906 "bug_table:\n"
2907 /* The first entry (1b-2b) is unused in this test,
2908 but we include it to check if cross-section
2909 PC-relative references work. */
2910 "2:\t.long 1b - 2b, %c0 - 2b\n"
2911 ".popsection\n" : : "i" ("A string"));
2912 char * str = ((char*)bug_table) + bug_table[1];
2913 return str;
2916 unsigned int set;
2918 void fancy_copy (unsigned *in, unsigned *out)
2920 asm volatile ("" : "=r" (*out) : "0" (*in));
2923 void fancy_copy2 (unsigned *in, unsigned *out)
2925 asm volatile ("mov %0,(%1)" : : "r" (*in), "r" (out) : "memory");
2928 #ifdef __x86_64__
2929 void clobber_r12(void)
2931 asm volatile("mov $1, %%r12" ::: "r12");
2933 #endif
2935 void test_high_clobbers(void)
2937 #ifdef __x86_64__
2938 register long val asm("r12");
2939 long val2;
2940 /* This tests if asm clobbers correctly save/restore callee saved
2941 registers if they are clobbered and if it's the high 8 x86-64
2942 registers. This is fragile for GCC as the constraints do not
2943 correctly capture the data flow, but good enough for us. */
2944 asm volatile("mov $0x4542, %%r12" : "=r" (val):: "memory");
2945 clobber_r12();
2946 asm volatile("mov %%r12, %0" : "=r" (val2) : "r" (val): "memory");
2947 printf("asmhc: 0x%x\n", val2);
2948 #endif
2951 void asm_test(void)
2953 char buf[128];
2954 unsigned int val, val2;
2955 struct struct123 s1;
2956 struct struct1231 s2 = { (unsigned long)&s1 };
2957 /* Hide the outer base_func, but check later that the inline
2958 asm block gets the outer one. */
2959 int base_func = 42;
2960 void override_func3 (void);
2961 unsigned long asmret;
2962 #ifdef BOOL_ISOC99
2963 _Bool somebool;
2964 #endif
2965 register int regvar asm("%esi");
2967 printf("inline asm:\n");
2969 // parse 0x1E-1 as 3 tokens in asm mode
2970 asm volatile ("mov $0x1E-1,%eax");
2972 /* test the no operand case */
2973 asm volatile ("xorl %eax, %eax");
2975 memcpy1(buf, "hello", 6);
2976 strncat1(buf, " worldXXXXX", 3);
2977 printf("%s\n", buf);
2979 memcpy2(buf, "hello", 6);
2980 strncat2(buf, " worldXXXXX", 3);
2981 printf("%s\n", buf);
2983 /* 'A' constraint test */
2984 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2985 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2987 s1.a = 42;
2988 s1.b = 43;
2989 printf("mconstraint: %d", mconstraint_test(&s2));
2990 printf(" %d %d\n", s1.a, s1.b);
2991 other_constraints_test();
2992 set = 0xff;
2993 sigdelset1(&set, 2);
2994 sigaddset1(&set, 16);
2995 /* NOTE: we test here if C labels are correctly restored after the
2996 asm statement */
2997 goto label1;
2998 label2:
2999 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
3000 printf("set=0x%x\n", set);
3001 val = 0x01020304;
3002 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
3003 override_func1();
3004 override_func2();
3005 /* The base_func ref from the following inline asm should find
3006 the global one, not the local decl from this function. */
3007 asm volatile(".weak override_func3\n.set override_func3, base_func");
3008 override_func3();
3009 /* Check that we can also load structs of appropriate layout
3010 into registers. */
3011 asm volatile("" : "=r" (asmret) : "0"(s2));
3012 if (asmret != s2.addr)
3013 printf("asmstr: failed\n");
3014 #ifdef BOOL_ISOC99
3015 /* Check that the typesize correctly sets the register size to
3016 8 bit. */
3017 asm volatile("cmp %1,%2; sete %0" : "=a"(somebool) : "r"(1), "r"(2));
3018 if (!somebool)
3019 printf("asmbool: failed\n");
3020 #endif
3021 printf("asmstr: %s\n", get_asm_string());
3022 val = 43;
3023 fancy_copy (&val, &val2);
3024 printf ("fancycpy(%d)=%d\n", val, val2);
3025 val = 44;
3026 fancy_copy2 (&val, &val2);
3027 printf ("fancycpy2(%d)=%d\n", val, val2);
3028 asm volatile ("mov $0x4243, %%esi" : "=r" (regvar));
3029 printf ("regvar=%x\n", regvar);
3030 test_high_clobbers();
3031 return;
3032 label1:
3033 goto label2;
3036 #else
3038 void asm_test(void)
3042 #endif
3044 #define COMPAT_TYPE(type1, type2) \
3046 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
3047 __builtin_types_compatible_p (type1, type2));\
3050 int constant_p_var;
3052 void builtin_test(void)
3054 short s;
3055 int i;
3056 long long ll;
3057 #if GCC_MAJOR >= 3
3058 COMPAT_TYPE(int, int);
3059 COMPAT_TYPE(int, unsigned int);
3060 COMPAT_TYPE(int, char);
3061 COMPAT_TYPE(int, const int);
3062 COMPAT_TYPE(int, volatile int);
3063 COMPAT_TYPE(int *, int *);
3064 COMPAT_TYPE(int *, void *);
3065 COMPAT_TYPE(int *, const int *);
3066 COMPAT_TYPE(char *, unsigned char *);
3067 COMPAT_TYPE(char *, signed char *);
3068 COMPAT_TYPE(char *, char *);
3069 /* space is needed because tcc preprocessor introduces a space between each token */
3070 COMPAT_TYPE(char * *, void *);
3071 #endif
3072 printf("res = %d\n", __builtin_constant_p(1));
3073 printf("res = %d\n", __builtin_constant_p(1 + 2));
3074 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
3075 printf("res = %d\n", __builtin_constant_p(constant_p_var));
3076 printf("res = %d\n", __builtin_constant_p(100000 / constant_p_var));
3077 s = 1;
3078 ll = 2;
3079 i = __builtin_choose_expr (1 != 0, ll, s);
3080 printf("bce: %d\n", i);
3081 i = __builtin_choose_expr (1 != 1, ll, s);
3082 printf("bce: %d\n", i);
3083 i = sizeof (__builtin_choose_expr (1, ll, s));
3084 printf("bce: %d\n", i);
3085 i = sizeof (__builtin_choose_expr (0, ll, s));
3086 printf("bce: %d\n", i);
3088 printf("bera: %p\n", __builtin_extract_return_addr((void*)43));
3091 #ifndef _WIN32
3092 extern int __attribute__((weak)) weak_f1(void);
3093 extern int __attribute__((weak)) weak_f2(void);
3094 extern int weak_f3(void);
3095 extern int __attribute__((weak)) weak_v1;
3096 extern int __attribute__((weak)) weak_v2;
3097 extern int weak_v3;
3099 extern int (*weak_fpa)() __attribute__((weak));
3100 extern int __attribute__((weak)) (*weak_fpb)();
3101 extern __attribute__((weak)) int (*weak_fpc)();
3103 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
3104 extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
3105 extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
3106 extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
3107 extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
3108 extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
3110 static const size_t dummy = 0;
3111 extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
3112 extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
3113 extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
3115 int some_lib_func(void);
3116 int dummy_impl_of_slf(void) { return 444; }
3117 int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
3119 int weak_toolate() __attribute__((weak));
3120 int weak_toolate() { return 0; }
3122 void __attribute__((weak)) weak_test(void)
3124 printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
3125 printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
3126 printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
3127 printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
3128 printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
3129 printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
3131 printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
3132 printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
3133 printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
3135 printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
3136 printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
3137 printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
3138 printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
3139 printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
3140 printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
3141 printf("some_lib_func=%d\n", &some_lib_func ? some_lib_func() : 0);
3144 int __attribute__((weak)) weak_f2() { return 222; }
3145 int __attribute__((weak)) weak_f3() { return 333; }
3146 int __attribute__((weak)) weak_v2 = 222;
3147 int __attribute__((weak)) weak_v3 = 333;
3148 #endif
3150 void const_func(const int a)
3154 void const_warn_test(void)
3156 const_func(1);
3159 struct condstruct {
3160 int i;
3163 int getme (struct condstruct *s, int i)
3165 int i1 = (i == 0 ? 0 : s)->i;
3166 int i2 = (i == 0 ? s : 0)->i;
3167 int i3 = (i == 0 ? (void*)0 : s)->i;
3168 int i4 = (i == 0 ? s : (void*)0)->i;
3169 return i1 + i2 + i3 + i4;
3172 struct global_data
3174 int a[40];
3175 int *b[40];
3178 struct global_data global_data;
3180 int global_data_getstuff (int *, int);
3182 void global_data_callit (int i)
3184 *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
3187 int global_data_getstuff (int *p, int i)
3189 return *p + i;
3192 void global_data_test (void)
3194 global_data.a[0] = 42;
3195 global_data.b[0] = &global_data.a[0];
3196 global_data_callit (0);
3197 printf ("%d\n", global_data.a[0]);
3200 struct cmpcmpS
3202 unsigned char fill : 3;
3203 unsigned char b1 : 1;
3204 unsigned char b2 : 1;
3205 unsigned char fill2 : 3;
3208 int glob1, glob2, glob3;
3210 void compare_comparisons (struct cmpcmpS *s)
3212 if (s->b1 != (glob1 == glob2)
3213 || (s->b2 != (glob1 == glob3)))
3214 printf ("comparing comparisons broken\n");
3217 void cmp_comparison_test(void)
3219 struct cmpcmpS s;
3220 s.b1 = 1;
3221 glob1 = 42; glob2 = 42;
3222 s.b2 = 0;
3223 glob3 = 43;
3224 compare_comparisons (&s);
3227 int fcompare (double a, double b, int code)
3229 switch (code) {
3230 case 0: return a == b;
3231 case 1: return a != b;
3232 case 2: return a < b;
3233 case 3: return a >= b;
3234 case 4: return a > b;
3235 case 5: return a <= b;
3239 void math_cmp_test(void)
3241 double nan = 0.0/0.0;
3242 double one = 1.0;
3243 double two = 2.0;
3244 int comp = 0;
3245 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
3247 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
3248 And it does this in various ways so that all code generation paths
3249 are checked (generating inverted tests, or non-inverted tests, or
3250 producing a 0/1 value without jumps (that's done in the fcompare
3251 function). */
3252 #define FCMP(a,b,op,iop,code) \
3253 if (fcompare (a,b,code)) \
3254 bug (a,b,op,iop,1); \
3255 if (a op b) \
3256 bug (a,b,op,iop,2); \
3257 if (a iop b) \
3259 else \
3260 bug (a,b,op,iop,3); \
3261 if ((a op b) || comp) \
3262 bug (a,b,op,iop,4); \
3263 if ((a iop b) || comp) \
3265 else \
3266 bug (a,b,op,iop,5);
3268 /* Equality tests. */
3269 FCMP(nan, nan, ==, !=, 0);
3270 FCMP(one, two, ==, !=, 0);
3271 FCMP(one, one, !=, ==, 1);
3272 /* Non-equality is a bit special. */
3273 if (!fcompare (nan, nan, 1))
3274 bug (nan, nan, !=, ==, 6);
3276 /* Relational tests on numbers. */
3277 FCMP(two, one, <, >=, 2);
3278 FCMP(one, two, >=, <, 3);
3279 FCMP(one, two, >, <=, 4);
3280 FCMP(two, one, <=, >, 5);
3282 /* Relational tests on NaNs. Note that the inverse op here is
3283 always !=, there's no operator in C that is equivalent to !(a < b),
3284 when NaNs are involved, same for the other relational ops. */
3285 FCMP(nan, nan, <, !=, 2);
3286 FCMP(nan, nan, >=, !=, 3);
3287 FCMP(nan, nan, >, !=, 4);
3288 FCMP(nan, nan, <=, !=, 5);
3291 double get100 () { return 100.0; }
3293 void callsave_test(void)
3295 #if defined __i386__ || defined __x86_64__ || defined __arm__
3296 int i, s; double *d; double t;
3297 s = sizeof (double);
3298 printf ("callsavetest: %d\n", s);
3299 d = alloca (sizeof(double));
3300 d[0] = 10.0;
3301 /* x86-64 had a bug were the next call to get100 would evict
3302 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
3303 in int type, not pointer type. When alloca returns a pointer
3304 with the high 32 bit set (which is likely on x86-64) the access
3305 generates a segfault. */
3306 i = d[0] > get100 ();
3307 printf ("%d\n", i);
3308 #endif
3312 void bfa3(ptrdiff_t str_offset)
3314 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
3316 void bfa2(ptrdiff_t str_offset)
3318 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
3319 bfa3(str_offset);
3321 void bfa1(ptrdiff_t str_offset)
3323 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
3324 bfa2(str_offset);
3327 void builtin_frame_address_test(void)
3329 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
3330 #ifndef __arm__
3331 char str[] = "__builtin_frame_address";
3332 char *fp0 = __builtin_frame_address(0);
3334 printf("str: %s\n", str);
3335 bfa1(str-fp0);
3336 #endif
3339 char via_volatile (char i)
3341 char volatile vi;
3342 vi = i;
3343 return vi;
3346 struct __attribute__((__packed__)) Spacked {
3347 char a;
3348 short b;
3349 int c;
3351 struct Spacked spacked;
3352 typedef struct __attribute__((__packed__)) {
3353 char a;
3354 short b;
3355 int c;
3356 } Spacked2;
3357 Spacked2 spacked2;
3358 #ifdef BROKEN
3359 /* This doesn't work for now. Requires adjusting field offsets/sizes
3360 after parsing the struct members. */
3361 typedef struct Spacked3_s {
3362 char a;
3363 short b;
3364 int c;
3365 } __attribute__((__packed__)) Spacked3;
3366 Spacked3 spacked3;
3367 #endif
3368 void attrib_test(void)
3370 printf("attr: %d %d %d %d\n", sizeof(struct Spacked),
3371 sizeof(spacked), sizeof(Spacked2), sizeof(spacked2));
3372 #ifdef BROKEN
3373 printf("attr: %d %d\n", sizeof(Spacked3), sizeof(spacked3));
3374 #endif
3376 extern __attribute__((__unused__)) char * __attribute__((__unused__)) *
3377 strange_attrib_placement (void);
3379 void * __attribute__((__unused__)) get_void_ptr (void *a)
3381 return a;