Implement __builtin_choose_expr
[tinycc.git] / tests / tcctest.c
blob10033a45c833e229008cfe98cf1f54f16dfedff3
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"
49 #include TCCLIB_INC
51 #include TCCLIB_INC1.TCCLIB_INC2
53 #include TCCLIB_INC1.h>
55 /* gcc 3.2 does not accept that (bug ?) */
56 //#include TCCLIB_INC3 ".h"
58 #include <tcclib.h>
60 #include "tcclib.h"
62 void intdiv_test();
63 void string_test();
64 void expr_test();
65 void macro_test();
66 void recursive_macro_test();
67 void scope_test();
68 void forward_test();
69 void funcptr_test();
70 void loop_test();
71 void switch_test();
72 void goto_test();
73 void enum_test();
74 void typedef_test();
75 void struct_test();
76 void array_test();
77 void expr_ptr_test();
78 void bool_test();
79 void expr2_test();
80 void constant_expr_test();
81 void expr_cmp_test();
82 void char_short_test();
83 void init_test(void);
84 void compound_literal_test(void);
85 int kr_test();
86 void struct_assign_test(void);
87 void cast_test(void);
88 void bitfield_test(void);
89 void c99_bool_test(void);
90 void float_test(void);
91 void longlong_test(void);
92 void manyarg_test(void);
93 void stdarg_test(void);
94 void whitespace_test(void);
95 void relocation_test(void);
96 void old_style_function(void);
97 void alloca_test(void);
98 void c99_vla_test(int size1, int size2);
99 void sizeof_test(void);
100 void typeof_test(void);
101 void local_label_test(void);
102 void statement_expr_test(void);
103 void asm_test(void);
104 void builtin_test(void);
105 void weak_test(void);
106 void global_data_test(void);
107 void cmp_comparison_test(void);
108 void math_cmp_test(void);
109 void callsave_test(void);
110 void builtin_frame_address_test(void);
111 void attrib_test(void);
113 int fib(int n);
114 void num(int n);
115 void forward_ref(void);
116 int isid(int c);
118 /* Line joining happens before tokenization, so the following
119 must be parsed as ellipsis. */
120 void funny_line_continuation (int, ..\
121 . );
123 char via_volatile (char);
125 #define A 2
126 #define N 1234 + A
127 #define pf printf
128 #define M1(a, b) (a) + (b)
130 #define str\
131 (s) # s
132 #define glue(a, b) a ## b
133 #define xglue(a, b) glue(a, b)
134 #define HIGHLOW "hello"
135 #define LOW LOW ", world"
137 static int onetwothree = 123;
138 #define onetwothree4 onetwothree
139 #define onetwothree xglue(onetwothree,4)
141 #define min(a, b) ((a) < (b) ? (a) : (b))
143 #ifdef C99_MACROS
144 #define dprintf(level,...) printf(__VA_ARGS__)
145 #endif
147 /* gcc vararg macros */
148 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
150 #define MACRO_NOARGS()
152 #define AAA 3
153 #undef AAA
154 #define AAA 4
156 #if 1
157 #define B3 1
158 #elif 1
159 #define B3 2
160 #elif 0
161 #define B3 3
162 #else
163 #define B3 4
164 #endif
166 #define __INT64_C(c) c ## LL
167 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
169 int qq(int x)
171 return x + 40;
173 #define qq(x) x
175 #define spin_lock(lock) do { } while (0)
176 #define wq_spin_lock spin_lock
177 #define TEST2() wq_spin_lock(a)
179 #define UINT_MAX ((unsigned) -1)
181 void intdiv_test(void)
183 printf("18/21=%u\n", 18/21);
184 printf("18%%21=%u\n", 18%21);
185 printf("41/21=%u\n", 41/21);
186 printf("41%%21=%u\n", 41%21);
187 printf("42/21=%u\n", 42/21);
188 printf("42%%21=%u\n", 42%21);
189 printf("43/21=%u\n", 43/21);
190 printf("43%%21=%u\n", 43%21);
191 printf("126/21=%u\n", 126/21);
192 printf("126%%21=%u\n", 126%21);
193 printf("131/21=%u\n", 131/21);
194 printf("131%%21=%u\n", 131%21);
195 printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX/2+3)/2);
196 printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX/2+3)%2);
198 printf("18/-21=%u\n", 18/-21);
199 printf("18%%-21=%u\n", 18%-21);
200 printf("41/-21=%u\n", 41/-21);
201 printf("41%%-21=%u\n", 41%-21);
202 printf("42/-21=%u\n", 42/-21);
203 printf("42%%-21=%u\n", 42%-21);
204 printf("43/-21=%u\n", 43/-21);
205 printf("43%%-21=%u\n", 43%-21);
206 printf("126/-21=%u\n", 126/-21);
207 printf("126%%-21=%u\n", 126%-21);
208 printf("131/-21=%u\n", 131/-21);
209 printf("131%%-21=%u\n", 131%-21);
210 printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX/2+3)/-2);
211 printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX/2+3)%-2);
213 printf("-18/21=%u\n", -18/21);
214 printf("-18%%21=%u\n", -18%21);
215 printf("-41/21=%u\n", -41/21);
216 printf("-41%%21=%u\n", -41%21);
217 printf("-42/21=%u\n", -42/21);
218 printf("-42%%21=%u\n", -42%21);
219 printf("-43/21=%u\n", -43/21);
220 printf("-43%%21=%u\n", -43%21);
221 printf("-126/21=%u\n", -126/21);
222 printf("-126%%21=%u\n", -126%21);
223 printf("-131/21=%u\n", -131/21);
224 printf("-131%%21=%u\n", -131%21);
225 printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX/2+3))/2);
226 printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX/2+3))%2);
228 printf("-18/-21=%u\n", -18/-21);
229 printf("-18%%-21=%u\n", -18%-21);
230 printf("-41/-21=%u\n", -41/-21);
231 printf("-41%%-21=%u\n", -41%-21);
232 printf("-42/-21=%u\n", -42/-21);
233 printf("-42%%-21=%u\n", -42%-21);
234 printf("-43/-21=%u\n", -43/-21);
235 printf("-43%%-21=%u\n", -43%-21);
236 printf("-126/-21=%u\n", -126/-21);
237 printf("-126%%-21=%u\n", -126%-21);
238 printf("-131/-21=%u\n", -131/-21);
239 printf("-131%%-21=%u\n", -131%-21);
240 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX/2+3))/-2);
241 printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX/2+3))%-2);
244 void macro_test(void)
246 printf("macro:\n");\f\v
247 pf("N=%d\n", N);
248 printf("aaa=%d\n", AAA);
250 printf("min=%d\n", min(1, min(2, -1)));
252 printf("s1=%s\n", glue(HIGH, LOW));
253 printf("s2=%s\n", xglue(HIGH, LOW));
254 printf("s3=%s\n", str("c"));
255 printf("s4=%s\n", str(a1));
256 printf("B3=%d\n", B3);
258 printf("onetwothree=%d\n", onetwothree);
260 #ifdef A
261 printf("A defined\n");
262 #endif
263 #ifdef B
264 printf("B defined\n");
265 #endif
266 #ifdef A
267 printf("A defined\n");
268 #else
269 printf("A not defined\n");
270 #endif
271 #ifdef B
272 printf("B defined\n");
273 #else
274 printf("B not defined\n");
275 #endif
277 #ifdef A
278 printf("A defined\n");
279 #ifdef B
280 printf("B1 defined\n");
281 #else
282 printf("B1 not defined\n");
283 #endif
284 #else
285 printf("A not defined\n");
286 #ifdef B
287 printf("B2 defined\n");
288 #else
289 printf("B2 not defined\n");
290 #endif
291 #endif
293 #if 1+1
294 printf("test true1\n");
295 #endif
296 #if 0
297 printf("test true2\n");
298 #endif
299 #if 1-1
300 printf("test true3\n");
301 #endif
302 #if defined(A)
303 printf("test trueA\n");
304 #endif
305 #if defined(B)
306 printf("test trueB\n");
307 #endif
309 #if 0
310 printf("test 0\n");
311 #elif 0
312 printf("test 1\n");
313 #elif 2
314 printf("test 2\n");
315 #else
316 printf("test 3\n");
317 #endif
319 MACRO_NOARGS();
321 #ifdef __LINE__
322 printf("__LINE__ defined\n");
323 #endif
325 printf("__LINE__=%d __FILE__=%s\n",
326 __LINE__, __FILE__);
327 #if 0
328 #line 200
329 printf("__LINE__=%d __FILE__=%s\n",
330 __LINE__, __FILE__);
331 #line 203 "test"
332 printf("__LINE__=%d __FILE__=%s\n",
333 __LINE__, __FILE__);
334 #line 227 "tcctest.c"
335 #endif
337 /* not strictly preprocessor, but we test it there */
338 #ifdef C99_MACROS
339 printf("__func__ = %s\n", __func__);
340 dprintf(1, "vaarg=%d\n", 1);
341 #endif
342 dprintf1(1, "vaarg1\n");
343 dprintf1(1, "vaarg1=%d\n", 2);
344 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
346 /* gcc extension */
347 printf("func='%s'\n", __FUNCTION__);
349 /* complicated macros in glibc */
350 printf("INT64_MIN=" LONG_LONG_FORMAT "\n", INT64_MIN);
352 int a;
353 a = 1;
354 glue(a+, +);
355 printf("a=%d\n", a);
356 glue(a <, <= 2);
357 printf("a=%d\n", a);
360 /* macro function with argument outside the macro string */
361 #define MF_s MF_hello
362 #define MF_hello(msg) printf("%s\n",msg)
364 #define MF_t printf("tralala\n"); MF_hello
366 MF_s("hi");
367 MF_t("hi");
369 /* test macro substituion inside args (should not eat stream) */
370 printf("qq=%d\n", qq(qq)(2));
372 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
373 null argument without a space. gcc 3.2 fixes that. */
375 #define qq1(x) 1
376 printf("qq1=%d\n", qq1( ));
378 /* comment with stray handling *\
380 /* this is a valid *\/ comment */
381 /* this is a valid comment *\*/
382 // this is a valid\
383 comment
385 /* test function macro substitution when the function name is
386 substituted */
387 TEST2();
389 /* And again when the name and parenthes are separated by a
390 comment. */
391 TEST2 /* the comment */ ();
395 static void print_num(char *fn, int line, int num) {
396 printf("fn %s, line %d, num %d\n", fn, line, num);
399 void recursive_macro_test(void)
402 #define ELF32_ST_TYPE(val) ((val) & 0xf)
403 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
404 #define STB_WEAK 2 /* Weak symbol */
405 #define ELFW(type) ELF##32##_##type
406 printf("%d\n", ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(123)));
408 #define WRAP(x) x
410 #define print_num(x) print_num(__FILE__,__LINE__,x)
411 print_num(123);
412 WRAP(print_num(123));
413 WRAP(WRAP(print_num(123)));
415 static struct recursive_macro { int rm_field; } G;
416 #define rm_field (G.rm_field)
417 printf("rm_field = %d\n", rm_field);
418 printf("rm_field = %d\n", WRAP(rm_field));
419 WRAP((printf("rm_field = %d %d\n", rm_field, WRAP(rm_field))));
422 int op(a,b)
424 return a / b;
427 int ret(a)
429 if (a == 2)
430 return 1;
431 if (a == 3)
432 return 2;
433 return 0;
436 void ps(const char *s)
438 int c;
439 while (1) {
440 c = *s;
441 if (c == 0)
442 break;
443 printf("%c", c);
444 s++;
448 const char foo1_string[] = "\
449 bar\n\
450 test\14\
453 void string_test()
455 unsigned int b;
456 printf("string:\n");
457 printf("\141\1423\143\n");/* dezdez test */
458 printf("\x41\x42\x43\x3a\n");
459 printf("c=%c\n", 'r');
460 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
461 printf("foo1_string='%s'\n", foo1_string);
462 #if 0
463 printf("wstring=%S\n", L"abc");
464 printf("wstring=%S\n", L"abc" L"def" "ghi");
465 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
466 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
467 #endif
468 ps("test\n");
469 b = 32;
470 while ((b = b + 1) < 96) {
471 printf("%c", b);
473 printf("\n");
474 printf("fib=%d\n", fib(33));
475 b = 262144;
476 while (b != 0x80000000) {
477 num(b);
478 b = b * 2;
482 void loop_test()
484 int i;
485 i = 0;
486 while (i < 10)
487 printf("%d", i++);
488 printf("\n");
489 for(i = 0; i < 10;i++)
490 printf("%d", i);
491 printf("\n");
492 i = 0;
493 do {
494 printf("%d", i++);
495 } while (i < 10);
496 printf("\n");
498 char count = 123;
499 /* c99 for loop init test */
500 for (size_t count = 1; count < 3; count++)
501 printf("count=%d\n", count);
502 printf("count = %d\n", count);
504 /* break/continue tests */
505 i = 0;
506 while (1) {
507 if (i == 6)
508 break;
509 i++;
510 if (i == 3)
511 continue;
512 printf("%d", i);
514 printf("\n");
516 /* break/continue tests */
517 i = 0;
518 do {
519 if (i == 6)
520 break;
521 i++;
522 if (i == 3)
523 continue;
524 printf("%d", i);
525 } while(1);
526 printf("\n");
528 for(i = 0;i < 10;i++) {
529 if (i == 3)
530 continue;
531 printf("%d", i);
533 printf("\n");
536 typedef int typedef_and_label;
538 void goto_test()
540 int i;
541 static void *label_table[3] = { &&label1, &&label2, &&label3 };
543 printf("goto:\n");
544 i = 0;
545 /* This needs to parse as label, not as start of decl. */
546 typedef_and_label:
547 s_loop:
548 if (i >= 10)
549 goto s_end;
550 printf("%d", i);
551 i++;
552 goto s_loop;
553 s_end:
554 printf("\n");
556 /* we also test computed gotos (GCC extension) */
557 for(i=0;i<3;i++) {
558 goto *label_table[i];
559 label1:
560 printf("label1\n");
561 goto next;
562 label2:
563 printf("label2\n");
564 goto next;
565 label3:
566 printf("label3\n");
567 next: ;
571 enum {
573 E1 = 2,
574 E2 = 4,
579 enum test {
580 E5 = 1000,
583 void enum_test()
585 enum test b1;
586 printf("enum:\n%d %d %d %d %d %d\n",
587 E0, E1, E2, E3, E4, E5);
588 b1 = 1;
589 printf("b1=%d\n", b1);
592 typedef int *my_ptr;
594 typedef int mytype1;
595 typedef int mytype2;
597 void typedef_test()
599 my_ptr a;
600 mytype1 mytype2;
601 int b;
603 a = &b;
604 *a = 1234;
605 printf("typedef:\n");
606 printf("a=%d\n", *a);
607 mytype2 = 2;
608 printf("mytype2=%d\n", mytype2);
611 void forward_test()
613 printf("forward:\n");
614 forward_ref();
615 forward_ref();
619 void forward_ref(void)
621 printf("forward ok\n");
624 typedef struct struct1 {
625 int f1;
626 int f2, f3;
627 union union1 {
628 int v1;
629 int v2;
630 } u;
631 char str[3];
632 } struct1;
634 struct struct2 {
635 int a;
636 char b;
639 union union2 {
640 int w1;
641 int w2;
644 struct struct1 st1, st2;
646 struct empty_mem {
647 /* nothing */ ;
648 int x;
651 int main(int argc, char **argv)
653 string_test();
654 expr_test();
655 macro_test();
656 recursive_macro_test();
657 scope_test();
658 forward_test();
659 funcptr_test();
660 loop_test();
661 switch_test();
662 goto_test();
663 enum_test();
664 typedef_test();
665 struct_test();
666 array_test();
667 expr_ptr_test();
668 bool_test();
669 expr2_test();
670 constant_expr_test();
671 expr_cmp_test();
672 char_short_test();
673 init_test();
674 compound_literal_test();
675 kr_test();
676 struct_assign_test();
677 cast_test();
678 bitfield_test();
679 c99_bool_test();
680 float_test();
681 longlong_test();
682 manyarg_test();
683 stdarg_test();
684 whitespace_test();
685 relocation_test();
686 old_style_function();
687 alloca_test();
688 c99_vla_test(5, 2);
689 sizeof_test();
690 typeof_test();
691 statement_expr_test();
692 local_label_test();
693 asm_test();
694 builtin_test();
695 #ifndef _WIN32
696 weak_test();
697 #endif
698 global_data_test();
699 cmp_comparison_test();
700 math_cmp_test();
701 callsave_test();
702 builtin_frame_address_test();
703 intdiv_test();
704 if (via_volatile (42) != 42)
705 printf ("via_volatile broken\n");
706 attrib_test();
707 return 0;
710 int tab[3];
711 int tab2[3][2];
713 int g;
715 void f1(g)
717 printf("g1=%d\n", g);
720 void scope_test()
722 printf("scope:\n");
723 g = 2;
724 f1(1);
725 printf("g2=%d\n", g);
727 int g;
728 g = 3;
729 printf("g3=%d\n", g);
731 int g;
732 g = 4;
733 printf("g4=%d\n", g);
736 printf("g5=%d\n", g);
739 void array_test()
741 int i, j, a[4];
743 printf("array:\n");
744 printf("sizeof(a) = %d\n", sizeof(a));
745 printf("sizeof(\"a\") = %d\n", sizeof("a"));
746 #ifdef C99_MACROS
747 printf("sizeof(__func__) = %d\n", sizeof(__func__));
748 #endif
749 printf("sizeof tab %d\n", sizeof(tab));
750 printf("sizeof tab2 %d\n", sizeof tab2);
751 tab[0] = 1;
752 tab[1] = 2;
753 tab[2] = 3;
754 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
755 for(i=0;i<3;i++)
756 for(j=0;j<2;j++)
757 tab2[i][j] = 10 * i + j;
758 for(i=0;i<3*2;i++) {
759 printf(" %3d", ((int *)tab2)[i]);
761 printf("\n");
762 printf("sizeof(size_t)=%d\n", sizeof(size_t));
763 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
766 void expr_test()
768 int a, b;
769 a = 0;
770 printf("%d\n", a += 1);
771 printf("%d\n", a -= 2);
772 printf("%d\n", a *= 31232132);
773 printf("%d\n", a /= 4);
774 printf("%d\n", a %= 20);
775 printf("%d\n", a &= 6);
776 printf("%d\n", a ^= 7);
777 printf("%d\n", a |= 8);
778 printf("%d\n", a >>= 3);
779 printf("%d\n", a <<= 4);
781 a = 22321;
782 b = -22321;
783 printf("%d\n", a + 1);
784 printf("%d\n", a - 2);
785 printf("%d\n", a * 312);
786 printf("%d\n", a / 4);
787 printf("%d\n", b / 4);
788 printf("%d\n", (unsigned)b / 4);
789 printf("%d\n", a % 20);
790 printf("%d\n", b % 20);
791 printf("%d\n", (unsigned)b % 20);
792 printf("%d\n", a & 6);
793 printf("%d\n", a ^ 7);
794 printf("%d\n", a | 8);
795 printf("%d\n", a >> 3);
796 printf("%d\n", b >> 3);
797 printf("%d\n", (unsigned)b >> 3);
798 printf("%d\n", a << 4);
799 printf("%d\n", ~a);
800 printf("%d\n", -a);
801 printf("%d\n", +a);
803 printf("%d\n", 12 + 1);
804 printf("%d\n", 12 - 2);
805 printf("%d\n", 12 * 312);
806 printf("%d\n", 12 / 4);
807 printf("%d\n", 12 % 20);
808 printf("%d\n", 12 & 6);
809 printf("%d\n", 12 ^ 7);
810 printf("%d\n", 12 | 8);
811 printf("%d\n", 12 >> 2);
812 printf("%d\n", 12 << 4);
813 printf("%d\n", ~12);
814 printf("%d\n", -12);
815 printf("%d\n", +12);
816 printf("%d %d %d %d\n",
817 isid('a'),
818 isid('g'),
819 isid('T'),
820 isid('('));
823 int isid(int c)
825 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
828 /**********************/
830 int vstack[10], *vstack_ptr;
832 void vpush(int vt, int vc)
834 *vstack_ptr++ = vt;
835 *vstack_ptr++ = vc;
838 void vpop(int *ft, int *fc)
840 *fc = *--vstack_ptr;
841 *ft = *--vstack_ptr;
844 void expr2_test()
846 int a, b;
848 printf("expr2:\n");
849 vstack_ptr = vstack;
850 vpush(1432432, 2);
851 vstack_ptr[-2] &= ~0xffffff80;
852 vpop(&a, &b);
853 printf("res= %d %d\n", a, b);
856 void constant_expr_test()
858 int a;
859 printf("constant_expr:\n");
860 a = 3;
861 printf("%d\n", a * 16);
862 printf("%d\n", a * 1);
863 printf("%d\n", a + 0);
866 int tab4[10];
868 void expr_ptr_test()
870 int *p, *q;
871 int i = -1;
873 printf("expr_ptr:\n");
874 p = tab4;
875 q = tab4 + 10;
876 printf("diff=%d\n", q - p);
877 p++;
878 printf("inc=%d\n", p - tab4);
879 p--;
880 printf("dec=%d\n", p - tab4);
881 ++p;
882 printf("inc=%d\n", p - tab4);
883 --p;
884 printf("dec=%d\n", p - tab4);
885 printf("add=%d\n", p + 3 - tab4);
886 printf("add=%d\n", 3 + p - tab4);
888 /* check if 64bit support is ok */
889 q = p = 0;
890 q += i;
891 printf("%p %p %ld\n", q, p, p-q);
892 printf("%d %d %d %d %d %d\n",
893 p == q, p != q, p < q, p <= q, p >= q, p > q);
894 i = 0xf0000000;
895 p += i;
896 printf("%p %p %ld\n", q, p, p-q);
897 printf("%d %d %d %d %d %d\n",
898 p == q, p != q, p < q, p <= q, p >= q, p > q);
899 p = (int *)((char *)p + 0xf0000000);
900 printf("%p %p %ld\n", q, p, p-q);
901 printf("%d %d %d %d %d %d\n",
902 p == q, p != q, p < q, p <= q, p >= q, p > q);
903 p += 0xf0000000;
904 printf("%p %p %ld\n", q, p, p-q);
905 printf("%d %d %d %d %d %d\n",
906 p == q, p != q, p < q, p <= q, p >= q, p > q);
908 struct size12 {
909 int i, j, k;
911 struct size12 s[2], *sp = s;
912 int i, j;
913 sp->i = 42;
914 sp++;
915 j = -1;
916 printf("%d\n", sp[j].i);
920 void expr_cmp_test()
922 int a, b;
923 printf("constant_expr:\n");
924 a = -1;
925 b = 1;
926 printf("%d\n", a == a);
927 printf("%d\n", a != a);
929 printf("%d\n", a < b);
930 printf("%d\n", a <= b);
931 printf("%d\n", a <= a);
932 printf("%d\n", b >= a);
933 printf("%d\n", a >= a);
934 printf("%d\n", b > a);
936 printf("%d\n", (unsigned)a < b);
937 printf("%d\n", (unsigned)a <= b);
938 printf("%d\n", (unsigned)a <= a);
939 printf("%d\n", (unsigned)b >= a);
940 printf("%d\n", (unsigned)a >= a);
941 printf("%d\n", (unsigned)b > a);
944 struct empty {
947 struct aligntest1 {
948 char a[10];
951 struct aligntest2 {
952 int a;
953 char b[10];
956 struct aligntest3 {
957 double a, b;
960 struct aligntest4 {
961 double a[0];
964 void struct_test()
966 struct1 *s;
967 union union2 u;
969 printf("struct:\n");
970 printf("sizes: %d %d %d %d\n",
971 sizeof(struct struct1),
972 sizeof(struct struct2),
973 sizeof(union union1),
974 sizeof(union union2));
975 st1.f1 = 1;
976 st1.f2 = 2;
977 st1.f3 = 3;
978 printf("st1: %d %d %d\n",
979 st1.f1, st1.f2, st1.f3);
980 st1.u.v1 = 1;
981 st1.u.v2 = 2;
982 printf("union1: %d\n", st1.u.v1);
983 u.w1 = 1;
984 u.w2 = 2;
985 printf("union2: %d\n", u.w1);
986 s = &st2;
987 s->f1 = 3;
988 s->f2 = 2;
989 s->f3 = 1;
990 printf("st2: %d %d %d\n",
991 s->f1, s->f2, s->f3);
992 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
994 /* align / size tests */
995 printf("aligntest1 sizeof=%d alignof=%d\n",
996 sizeof(struct aligntest1), __alignof__(struct aligntest1));
997 printf("aligntest2 sizeof=%d alignof=%d\n",
998 sizeof(struct aligntest2), __alignof__(struct aligntest2));
999 printf("aligntest3 sizeof=%d alignof=%d\n",
1000 sizeof(struct aligntest3), __alignof__(struct aligntest3));
1001 printf("aligntest4 sizeof=%d alignof=%d\n",
1002 sizeof(struct aligntest4), __alignof__(struct aligntest4));
1004 /* empty structures (GCC extension) */
1005 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
1006 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
1009 /* XXX: depend on endianness */
1010 void char_short_test()
1012 int var1, var2;
1014 printf("char_short:\n");
1016 var1 = 0x01020304;
1017 var2 = 0xfffefdfc;
1018 printf("s8=%d %d\n",
1019 *(char *)&var1, *(char *)&var2);
1020 printf("u8=%d %d\n",
1021 *(unsigned char *)&var1, *(unsigned char *)&var2);
1022 printf("s16=%d %d\n",
1023 *(short *)&var1, *(short *)&var2);
1024 printf("u16=%d %d\n",
1025 *(unsigned short *)&var1, *(unsigned short *)&var2);
1026 printf("s32=%d %d\n",
1027 *(int *)&var1, *(int *)&var2);
1028 printf("u32=%d %d\n",
1029 *(unsigned int *)&var1, *(unsigned int *)&var2);
1030 *(char *)&var1 = 0x08;
1031 printf("var1=%x\n", var1);
1032 *(short *)&var1 = 0x0809;
1033 printf("var1=%x\n", var1);
1034 *(int *)&var1 = 0x08090a0b;
1035 printf("var1=%x\n", var1);
1038 /******************/
1040 typedef struct Sym {
1041 int v;
1042 int t;
1043 int c;
1044 struct Sym *next;
1045 struct Sym *prev;
1046 } Sym;
1048 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1049 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1051 static int toupper1(int a)
1053 return TOUPPER(a);
1056 void bool_test()
1058 int *s, a, b, t, f, i;
1060 a = 0;
1061 s = (void*)0;
1062 printf("!s=%d\n", !s);
1064 if (!s || !s[0])
1065 a = 1;
1066 printf("a=%d\n", a);
1068 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1069 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1070 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1071 #if 1 && 1
1072 printf("a1\n");
1073 #endif
1074 #if 1 || 0
1075 printf("a2\n");
1076 #endif
1077 #if 1 ? 0 : 1
1078 printf("a3\n");
1079 #endif
1080 #if 0 ? 0 : 1
1081 printf("a4\n");
1082 #endif
1084 a = 4;
1085 printf("b=%d\n", a + (0 ? 1 : a / 2));
1087 /* test register spilling */
1088 a = 10;
1089 b = 10;
1090 a = (a + b) * ((a < b) ?
1091 ((b - a) * (a - b)): a + b);
1092 printf("a=%d\n", a);
1094 /* test complex || or && expressions */
1095 t = 1;
1096 f = 0;
1097 a = 32;
1098 printf("exp=%d\n", f == (32 <= a && a <= 3));
1099 printf("r=%d\n", (t || f) + (t && f));
1101 /* test ? : cast */
1103 int aspect_on;
1104 int aspect_native = 65536;
1105 double bfu_aspect = 1.0;
1106 int aspect;
1107 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
1108 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
1109 printf("aspect=%d\n", aspect);
1113 /* test ? : GCC extension */
1115 static int v1 = 34 ? : -1; /* constant case */
1116 static int v2 = 0 ? : -1; /* constant case */
1117 int a = 30;
1119 printf("%d %d\n", v1, v2);
1120 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
1123 /* again complex expression */
1124 for(i=0;i<256;i++) {
1125 if (toupper1 (i) != TOUPPER (i))
1126 printf("error %d\n", i);
1130 /* GCC accepts that */
1131 static int tab_reinit[];
1132 static int tab_reinit[10];
1134 //int cinit1; /* a global variable can be defined several times without error ! */
1135 int cinit1;
1136 int cinit1;
1137 int cinit1 = 0;
1138 int *cinit2 = (int []){3, 2, 1};
1140 void compound_literal_test(void)
1142 int *p, i;
1143 char *q, *q3;
1145 printf("compound_test:\n");
1147 p = (int []){1, 2, 3};
1148 for(i=0;i<3;i++)
1149 printf(" %d", p[i]);
1150 printf("\n");
1152 for(i=0;i<3;i++)
1153 printf("%d", cinit2[i]);
1154 printf("\n");
1156 q = "tralala1";
1157 printf("q1=%s\n", q);
1159 q = (char *){ "tralala2" };
1160 printf("q2=%s\n", q);
1162 q3 = (char *){ q };
1163 printf("q3=%s\n", q3);
1165 q = (char []){ "tralala3" };
1166 printf("q4=%s\n", q);
1168 #ifdef ALL_ISOC99
1169 p = (int []){1, 2, cinit1 + 3};
1170 for(i=0;i<3;i++)
1171 printf(" %d", p[i]);
1172 printf("\n");
1174 for(i=0;i<3;i++) {
1175 p = (int []){1, 2, 4 + i};
1176 printf("%d %d %d\n",
1177 p[0],
1178 p[1],
1179 p[2]);
1181 #endif
1184 /* K & R protos */
1186 kr_func1(a, b)
1188 return a + b;
1191 int kr_func2(a, b)
1193 return a + b;
1196 kr_test()
1198 printf("kr_test:\n");
1199 printf("func1=%d\n", kr_func1(3, 4));
1200 printf("func2=%d\n", kr_func2(3, 4));
1201 return 0;
1204 void num(int n)
1206 char *tab, *p;
1207 tab = (char*)malloc(20);
1208 p = tab;
1209 while (1) {
1210 *p = 48 + (n % 10);
1211 p++;
1212 n = n / 10;
1213 if (n == 0)
1214 break;
1216 while (p != tab) {
1217 p--;
1218 printf("%c", *p);
1220 printf("\n");
1221 free(tab);
1224 /* structure assignment tests */
1225 struct structa1 {
1226 int f1;
1227 char f2;
1230 struct structa1 ssta1;
1232 void struct_assign_test1(struct structa1 s1, int t, float f)
1234 printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
1237 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1239 s1.f1 += t;
1240 s1.f2 -= t;
1241 return s1;
1244 void struct_assign_test(void)
1246 struct S {
1247 struct structa1 lsta1, lsta2;
1248 int i;
1249 } s, *ps;
1251 ps = &s;
1252 ps->i = 4;
1253 #if 0
1254 printf("struct_assign_test:\n");
1256 s.lsta1.f1 = 1;
1257 s.lsta1.f2 = 2;
1258 printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
1259 s.lsta2 = s.lsta1;
1260 printf("%d %d\n", s.lsta2.f1, s.lsta2.f2);
1261 #else
1262 s.lsta2.f1 = 1;
1263 s.lsta2.f2 = 2;
1264 #endif
1265 struct_assign_test1(ps->lsta2, 3, 4.5);
1267 printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
1268 ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
1269 printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
1271 static struct {
1272 void (*elem)();
1273 } t[] = {
1274 /* XXX: we should allow this even without braces */
1275 { struct_assign_test }
1277 printf("%d\n", struct_assign_test == t[0].elem);
1280 /* casts to short/char */
1282 void cast1(char a, short b, unsigned char c, unsigned short d)
1284 printf("%d %d %d %d\n", a, b, c, d);
1287 char bcast;
1288 short scast;
1290 void cast_test()
1292 int a;
1293 char c;
1294 char tab[10];
1295 unsigned b,d;
1296 short s;
1297 char *p = NULL;
1298 p -= 0x700000000042;
1300 printf("cast_test:\n");
1301 a = 0xfffff;
1302 cast1(a, a, a, a);
1303 a = 0xffffe;
1304 printf("%d %d %d %d\n",
1305 (char)(a + 1),
1306 (short)(a + 1),
1307 (unsigned char)(a + 1),
1308 (unsigned short)(a + 1));
1309 printf("%d %d %d %d\n",
1310 (char)0xfffff,
1311 (short)0xfffff,
1312 (unsigned char)0xfffff,
1313 (unsigned short)0xfffff);
1315 a = (bcast = 128) + 1;
1316 printf("%d\n", a);
1317 a = (scast = 65536) + 1;
1318 printf("%d\n", a);
1320 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1322 /* test cast from unsigned to signed short to int */
1323 b = 0xf000;
1324 d = (short)b;
1325 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1326 b = 0xf0f0;
1327 d = (char)b;
1328 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1330 /* test implicit int casting for array accesses */
1331 c = 0;
1332 tab[1] = 2;
1333 tab[c] = 1;
1334 printf("%d %d\n", tab[0], tab[1]);
1336 /* test implicit casting on some operators */
1337 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1338 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1339 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1341 /* from pointer to integer types */
1342 printf("%d %d %ld %ld %lld %lld\n",
1343 (int)p, (unsigned int)p,
1344 (long)p, (unsigned long)p,
1345 (long long)p, (unsigned long long)p);
1347 /* from integers to pointers */
1348 printf("%p %p %p %p\n",
1349 (void *)a, (void *)b, (void *)c, (void *)d);
1352 /* initializers tests */
1353 struct structinit1 {
1354 int f1;
1355 char f2;
1356 short f3;
1357 int farray[3];
1360 int sinit1 = 2;
1361 int sinit2 = { 3 };
1362 int sinit3[3] = { 1, 2, {{3}}, };
1363 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1364 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1365 int sinit6[] = { 1, 2, 3 };
1366 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1367 char sinit8[] = "hello" "trala";
1369 struct structinit1 sinit9 = { 1, 2, 3 };
1370 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1371 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1372 #ifdef ALL_ISOC99
1373 .farray[0] = 10,
1374 .farray[1] = 11,
1375 .farray[2] = 12,
1376 #endif
1379 char *sinit12 = "hello world";
1380 char *sinit13[] = {
1381 "test1",
1382 "test2",
1383 "test3",
1385 char sinit14[10] = { "abc" };
1386 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1388 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1390 struct bar {
1391 char *s;
1392 int len;
1393 } sinit17[] = {
1394 "a1", 4,
1395 "a2", 1
1398 int sinit18[10] = {
1399 [2 ... 5] = 20,
1401 [8] = 10,
1404 struct complexinit0 {
1405 int a;
1406 int b;
1409 struct complexinit {
1410 int a;
1411 const struct complexinit0 *b;
1414 const static struct complexinit cix[] = {
1415 [0] = {
1416 .a = 2000,
1417 .b = (const struct complexinit0[]) {
1418 { 2001, 2002 },
1419 { 2003, 2003 },
1425 struct complexinit2 {
1426 int a;
1427 int b[];
1430 struct complexinit2 cix20;
1432 struct complexinit2 cix21 = {
1433 .a = 3000,
1434 .b = { 3001, 3002, 3003 }
1437 struct complexinit2 cix22 = {
1438 .a = 4000,
1439 .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1442 void init_test(void)
1444 int linit1 = 2;
1445 int linit2 = { 3 };
1446 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1447 int linit6[] = { 1, 2, 3 };
1448 int i, j;
1449 char linit8[] = "hello" "trala";
1450 int linit12[10] = { 1, 2 };
1451 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1452 char linit14[10] = "abc";
1453 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1454 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1455 int linit17 = sizeof(linit17);
1457 printf("init_test:\n");
1459 printf("sinit1=%d\n", sinit1);
1460 printf("sinit2=%d\n", sinit2);
1461 printf("sinit3=%d %d %d %d\n",
1462 sizeof(sinit3),
1463 sinit3[0],
1464 sinit3[1],
1465 sinit3[2]
1467 printf("sinit6=%d\n", sizeof(sinit6));
1468 printf("sinit7=%d %d %d %d\n",
1469 sizeof(sinit7),
1470 sinit7[0],
1471 sinit7[1],
1472 sinit7[2]
1474 printf("sinit8=%s\n", sinit8);
1475 printf("sinit9=%d %d %d\n",
1476 sinit9.f1,
1477 sinit9.f2,
1478 sinit9.f3
1480 printf("sinit10=%d %d %d\n",
1481 sinit10.f1,
1482 sinit10.f2,
1483 sinit10.f3
1485 printf("sinit11=%d %d %d %d %d %d\n",
1486 sinit11.f1,
1487 sinit11.f2,
1488 sinit11.f3,
1489 sinit11.farray[0],
1490 sinit11.farray[1],
1491 sinit11.farray[2]
1494 for(i=0;i<3;i++)
1495 for(j=0;j<2;j++)
1496 printf("[%d][%d] = %d %d %d\n",
1497 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1498 printf("linit1=%d\n", linit1);
1499 printf("linit2=%d\n", linit2);
1500 printf("linit6=%d\n", sizeof(linit6));
1501 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1503 printf("sinit12=%s\n", sinit12);
1504 printf("sinit13=%d %s %s %s\n",
1505 sizeof(sinit13),
1506 sinit13[0],
1507 sinit13[1],
1508 sinit13[2]);
1509 printf("sinit14=%s\n", sinit14);
1511 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1512 printf("\n");
1513 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1514 printf("\n");
1515 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1516 printf("\n");
1517 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1518 printf("\n");
1519 printf("%d %d %d %d\n",
1520 linit16.a1,
1521 linit16.a2,
1522 linit16.a3,
1523 linit16.a4);
1524 /* test that initialisation is done after variable declare */
1525 printf("linit17=%d\n", linit17);
1526 printf("sinit15=%d\n", sinit15[0]);
1527 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1528 printf("sinit17=%s %d %s %d\n",
1529 sinit17[0].s, sinit17[0].len,
1530 sinit17[1].s, sinit17[1].len);
1531 for(i=0;i<10;i++)
1532 printf("%x ", sinit18[i]);
1533 printf("\n");
1534 /* complex init check */
1535 printf("cix: %d %d %d %d %d %d %d\n",
1536 cix[0].a,
1537 cix[0].b[0].a, cix[0].b[0].b,
1538 cix[0].b[1].a, cix[0].b[1].b,
1539 cix[0].b[2].a, cix[0].b[2].b);
1540 printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
1541 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
1545 void switch_test()
1547 int i;
1549 for(i=0;i<15;i++) {
1550 switch(i) {
1551 case 0:
1552 case 1:
1553 printf("a");
1554 break;
1555 default:
1556 printf("%d", i);
1557 break;
1558 case 8 ... 12:
1559 printf("c");
1560 break;
1561 case 3:
1562 printf("b");
1563 break;
1564 case 0xc33c6b9fU:
1565 case 0x7c9eeeb9U:
1566 break;
1569 printf("\n");
1572 /* ISOC99 _Bool type */
1573 void c99_bool_test(void)
1575 #ifdef BOOL_ISOC99
1576 int a;
1577 _Bool b;
1579 printf("bool_test:\n");
1580 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1581 a = 3;
1582 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1583 b = 3;
1584 printf("b = %d\n", b);
1585 b++;
1586 printf("b = %d\n", b);
1587 #endif
1590 void bitfield_test(void)
1592 int a;
1593 short sa;
1594 unsigned char ca;
1595 struct sbf1 {
1596 int f1 : 3;
1597 int : 2;
1598 int f2 : 1;
1599 int : 0;
1600 int f3 : 5;
1601 int f4 : 7;
1602 unsigned int f5 : 7;
1603 } st1;
1604 printf("bitfield_test:");
1605 printf("sizeof(st1) = %d\n", sizeof(st1));
1607 st1.f1 = 3;
1608 st1.f2 = 1;
1609 st1.f3 = 15;
1610 a = 120;
1611 st1.f4 = a;
1612 st1.f5 = a;
1613 st1.f5++;
1614 printf("%d %d %d %d %d\n",
1615 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1616 sa = st1.f5;
1617 ca = st1.f5;
1618 printf("%d %d\n", sa, ca);
1620 st1.f1 = 7;
1621 if (st1.f1 == -1)
1622 printf("st1.f1 == -1\n");
1623 else
1624 printf("st1.f1 != -1\n");
1625 if (st1.f2 == -1)
1626 printf("st1.f2 == -1\n");
1627 else
1628 printf("st1.f2 != -1\n");
1630 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1631 long-long bitfields whose size is not bigger than int */
1632 struct sbf2 {
1633 long long f1 : 45;
1634 long long : 2;
1635 long long f2 : 35;
1636 unsigned long long f3 : 38;
1637 } st2;
1638 st2.f1 = 0x123456789ULL;
1639 a = 120;
1640 st2.f2 = (long long)a << 25;
1641 st2.f3 = a;
1642 st2.f2++;
1643 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
1646 #ifdef __x86_64__
1647 #define FLOAT_FMT "%f\n"
1648 #else
1649 /* x86's float isn't compatible with GCC */
1650 #define FLOAT_FMT "%.5f\n"
1651 #endif
1653 /* declare strto* functions as they are C99 */
1654 double strtod(const char *nptr, char **endptr);
1656 #if defined(_WIN32)
1657 float strtof(const char *nptr, char **endptr) {return (float)strtod(nptr, endptr);}
1658 LONG_DOUBLE strtold(const char *nptr, char **endptr) {return (LONG_DOUBLE)strtod(nptr, endptr);}
1659 #else
1660 float strtof(const char *nptr, char **endptr);
1661 LONG_DOUBLE strtold(const char *nptr, char **endptr);
1662 #endif
1664 #define FTEST(prefix, typename, type, fmt)\
1665 void prefix ## cmp(type a, type b)\
1667 printf("%d %d %d %d %d %d\n",\
1668 a == b,\
1669 a != b,\
1670 a < b,\
1671 a > b,\
1672 a >= b,\
1673 a <= b);\
1674 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1677 a + b,\
1678 a - b,\
1679 a * b,\
1680 a / b,\
1681 -a);\
1682 printf(fmt "\n", ++a);\
1683 printf(fmt "\n", a++);\
1684 printf(fmt "\n", a);\
1685 b = 0;\
1686 printf("%d %d\n", !a, !b);\
1688 void prefix ## fcast(type a)\
1690 float fa;\
1691 double da;\
1692 LONG_DOUBLE la;\
1693 int ia;\
1694 long long llia;\
1695 unsigned int ua;\
1696 unsigned long long llua;\
1697 type b;\
1698 fa = a;\
1699 da = a;\
1700 la = a;\
1701 printf("ftof: %f %f %Lf\n", fa, da, la);\
1702 ia = (int)a;\
1703 llia = (long long)a;\
1704 a = (a >= 0) ? a : -a;\
1705 ua = (unsigned int)a;\
1706 llua = (unsigned long long)a;\
1707 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
1708 ia = -1234;\
1709 ua = 0x81234500;\
1710 llia = -0x123456789012345LL;\
1711 llua = 0xf123456789012345LLU;\
1712 b = ia;\
1713 printf("itof: " fmt "\n", b);\
1714 b = ua;\
1715 printf("utof: " fmt "\n", b);\
1716 b = llia;\
1717 printf("lltof: " fmt "\n", b);\
1718 b = llua;\
1719 printf("ulltof: " fmt "\n", b);\
1722 float prefix ## retf(type a) { return a; }\
1723 double prefix ## retd(type a) { return a; }\
1724 LONG_DOUBLE prefix ## retld(type a) { return a; }\
1726 void prefix ## call(void)\
1728 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1729 printf("double: %f\n", prefix ## retd(42.123456789));\
1730 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1731 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
1734 void prefix ## signed_zeros(void) \
1736 type x = 0.0, y = -0.0, n, p;\
1737 if (x == y)\
1738 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
1739 1.0 / x != 1.0 / y);\
1740 else\
1741 printf ("x != y; this is wrong!\n");\
1743 n = -x;\
1744 if (x == n)\
1745 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
1746 1.0 / x != 1.0 / n);\
1747 else\
1748 printf ("x != -x; this is wrong!\n");\
1750 p = +y;\
1751 if (x == p)\
1752 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
1753 1.0 / x != 1.0 / p);\
1754 else\
1755 printf ("x != +y; this is wrong!\n");\
1756 p = -y;\
1757 if (x == p)\
1758 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
1759 1.0 / x != 1.0 / p);\
1760 else\
1761 printf ("x != -y; this is wrong!\n");\
1763 void prefix ## test(void)\
1765 printf("testing '%s'\n", #typename);\
1766 prefix ## cmp(1, 2.5);\
1767 prefix ## cmp(2, 1.5);\
1768 prefix ## cmp(1, 1);\
1769 prefix ## fcast(234.6);\
1770 prefix ## fcast(-2334.6);\
1771 prefix ## call();\
1772 prefix ## signed_zeros();\
1775 FTEST(f, float, float, "%f")
1776 FTEST(d, double, double, "%f")
1777 FTEST(ld, long double, LONG_DOUBLE, "%Lf")
1779 double ftab1[3] = { 1.2, 3.4, -5.6 };
1782 void float_test(void)
1784 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
1785 float fa, fb;
1786 double da, db;
1787 int a;
1788 unsigned int b;
1790 printf("float_test:\n");
1791 printf("sizeof(float) = %d\n", sizeof(float));
1792 printf("sizeof(double) = %d\n", sizeof(double));
1793 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));
1794 ftest();
1795 dtest();
1796 ldtest();
1797 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1798 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1799 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1800 da = 123;
1801 printf("da=%f\n", da);
1802 fa = 123;
1803 printf("fa=%f\n", fa);
1804 a = 4000000000;
1805 da = a;
1806 printf("da = %f\n", da);
1807 b = 4000000000;
1808 db = b;
1809 printf("db = %f\n", db);
1810 #endif
1813 int fib(int n)
1815 if (n <= 2)
1816 return 1;
1817 else
1818 return fib(n-1) + fib(n-2);
1821 void funcptr_test()
1823 void (*func)(int);
1824 int a;
1825 struct {
1826 int dummy;
1827 void (*func)(int);
1828 } st1;
1830 printf("funcptr:\n");
1831 func = &num;
1832 (*func)(12345);
1833 func = num;
1834 a = 1;
1835 a = 1;
1836 func(12345);
1837 /* more complicated pointer computation */
1838 st1.func = num;
1839 st1.func(12346);
1840 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1841 printf("sizeof2 = %d\n", sizeof funcptr_test);
1842 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1843 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1846 void lloptest(long long a, long long b)
1848 unsigned long long ua, ub;
1850 ua = a;
1851 ub = b;
1852 /* arith */
1853 printf("arith: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1854 a + b,
1855 a - b,
1856 a * b);
1858 if (b != 0) {
1859 printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1860 a / b,
1861 a % b);
1864 /* binary */
1865 printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1866 a & b,
1867 a | b,
1868 a ^ b);
1870 /* tests */
1871 printf("test: %d %d %d %d %d %d\n",
1872 a == b,
1873 a != b,
1874 a < b,
1875 a > b,
1876 a >= b,
1877 a <= b);
1879 printf("utest: %d %d %d %d %d %d\n",
1880 ua == ub,
1881 ua != ub,
1882 ua < ub,
1883 ua > ub,
1884 ua >= ub,
1885 ua <= ub);
1887 /* arith2 */
1888 a++;
1889 b++;
1890 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
1891 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a++, b++);
1892 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", --a, --b);
1893 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
1894 b = ub = 0;
1895 printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
1898 void llshift(long long a, int b)
1900 printf("shift: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1901 (unsigned long long)a >> b,
1902 a >> b,
1903 a << b);
1904 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1905 (unsigned long long)a >> 3,
1906 a >> 3,
1907 a << 3);
1908 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1909 (unsigned long long)a >> 35,
1910 a >> 35,
1911 a << 35);
1914 void llfloat(void)
1916 float fa;
1917 double da;
1918 LONG_DOUBLE lda;
1919 long long la, lb, lc;
1920 unsigned long long ula, ulb, ulc;
1921 la = 0x12345678;
1922 ula = 0x72345678;
1923 la = (la << 20) | 0x12345;
1924 ula = ula << 33;
1925 printf("la=" LONG_LONG_FORMAT " ula=" ULONG_LONG_FORMAT "\n", la, ula);
1927 fa = la;
1928 da = la;
1929 lda = la;
1930 printf("lltof: %f %f %Lf\n", fa, da, lda);
1932 la = fa;
1933 lb = da;
1934 lc = lda;
1935 printf("ftoll: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", la, lb, lc);
1937 fa = ula;
1938 da = ula;
1939 lda = ula;
1940 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1942 ula = fa;
1943 ulb = da;
1944 ulc = lda;
1945 printf("ftoull: " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT "\n", ula, ulb, ulc);
1948 long long llfunc1(int a)
1950 return a * 2;
1953 struct S {
1954 int id;
1955 char item;
1958 long long int value(struct S *v)
1960 return ((long long int)v->item);
1963 long long llfunc2(long long x, long long y, int z)
1965 return x * y * z;
1968 void longlong_test(void)
1970 long long a, b, c;
1971 int ia;
1972 unsigned int ua;
1973 printf("longlong_test:\n");
1974 printf("sizeof(long long) = %d\n", sizeof(long long));
1975 ia = -1;
1976 ua = -2;
1977 a = ia;
1978 b = ua;
1979 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
1980 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %Lx\n",
1981 (long long)1,
1982 (long long)-2,
1983 1LL,
1984 0x1234567812345679);
1985 a = llfunc1(-3);
1986 printf(LONG_LONG_FORMAT "\n", a);
1988 lloptest(1000, 23);
1989 lloptest(0xff, 0x1234);
1990 b = 0x72345678 << 10;
1991 lloptest(-3, b);
1992 llshift(0x123, 5);
1993 llshift(-23, 5);
1994 b = 0x72345678LL << 10;
1995 llshift(b, 47);
1997 llfloat();
1998 #if 1
1999 b = 0x12345678;
2000 a = -1;
2001 c = a + b;
2002 printf("%Lx\n", c);
2003 #endif
2005 /* long long reg spill test */
2007 struct S a;
2009 a.item = 3;
2010 printf("%lld\n", value(&a));
2012 lloptest(0x80000000, 0);
2015 long long *p, v, **pp;
2016 v = 1;
2017 p = &v;
2018 p[0]++;
2019 printf("another long long spill test : %lld\n", *p);
2020 pp = &p;
2022 v = llfunc2(**pp, **pp, ia);
2023 printf("a long long function (arm-)reg-args test : %lld\n", v);
2025 a = 68719476720LL;
2026 b = 4294967295LL;
2027 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
2029 printf(LONG_LONG_FORMAT "\n", 0x123456789LLU);
2031 /* long long pointer deref in argument passing test */
2032 a = 0x123;
2033 long long *p = &a;
2034 llshift(*p, 5);
2037 void manyarg_test(void)
2039 LONG_DOUBLE ld = 1234567891234LL;
2040 printf("manyarg_test:\n");
2041 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2042 1, 2, 3, 4, 5, 6, 7, 8,
2043 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2044 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2045 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2046 1, 2, 3, 4, 5, 6, 7, 8,
2047 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2048 1234567891234LL, 987654321986LL,
2049 42.0, 43.0);
2050 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2051 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2052 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2053 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2054 1234567891234LL, 987654321986LL,
2055 42.0, 43.0);
2056 printf("%d %d %d %d %d %d %d %d %Lf\n",
2057 1, 2, 3, 4, 5, 6, 7, 8, ld);
2058 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2059 LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n",
2060 1, 2, 3, 4, 5, 6, 7, 8,
2061 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2062 1234567891234LL, 987654321986LL,
2063 42.0, 43.0, ld);
2064 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2065 "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n",
2066 1, 2, 3, 4, 5, 6, 7, 8,
2067 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2068 ld, 1234567891234LL, 987654321986LL,
2069 42.0, 43.0, ld);
2072 void vprintf1(const char *fmt, ...)
2074 va_list ap, aq;
2075 const char *p;
2076 int c, i;
2077 double d;
2078 long long ll;
2079 LONG_DOUBLE ld;
2081 va_start(aq, fmt);
2082 va_copy(ap, aq);
2084 p = fmt;
2085 for(;;) {
2086 c = *p;
2087 if (c == '\0')
2088 break;
2089 p++;
2090 if (c == '%') {
2091 c = *p;
2092 switch(c) {
2093 case '\0':
2094 goto the_end;
2095 case 'd':
2096 i = va_arg(ap, int);
2097 printf("%d", i);
2098 break;
2099 case 'f':
2100 d = va_arg(ap, double);
2101 printf("%f", d);
2102 break;
2103 case 'l':
2104 ll = va_arg(ap, long long);
2105 printf(LONG_LONG_FORMAT, ll);
2106 break;
2107 case 'F':
2108 ld = va_arg(ap, LONG_DOUBLE);
2109 printf("%Lf", ld);
2110 break;
2112 p++;
2113 } else {
2114 putchar(c);
2117 the_end:
2118 va_end(aq);
2119 va_end(ap);
2122 struct myspace {
2123 short int profile;
2126 void stdarg_for_struct(struct myspace bob, ...)
2128 struct myspace george, bill;
2129 va_list ap;
2130 short int validate;
2132 va_start(ap, bob);
2133 bill = va_arg(ap, struct myspace);
2134 george = va_arg(ap, struct myspace);
2135 validate = va_arg(ap, int);
2136 printf("stdarg_for_struct: %d %d %d %d\n",
2137 bob.profile, bill.profile, george.profile, validate);
2138 va_end(ap);
2141 void stdarg_for_libc(const char *fmt, ...)
2143 va_list args;
2144 va_start(args, fmt);
2145 vprintf(fmt, args);
2146 va_end(args);
2149 void stdarg_test(void)
2151 LONG_DOUBLE ld = 1234567891234LL;
2152 struct myspace bob;
2154 vprintf1("%d %d %d\n", 1, 2, 3);
2155 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2156 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2157 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2158 vprintf1("%d %f %l %F %d %f %l %F\n",
2159 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2160 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2161 1, 2, 3, 4, 5, 6, 7, 8,
2162 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2163 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2164 1, 2, 3, 4, 5, 6, 7, 8,
2165 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2166 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2167 "%l %l %f %f\n",
2168 1, 2, 3, 4, 5, 6, 7, 8,
2169 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2170 1234567891234LL, 987654321986LL,
2171 42.0, 43.0);
2172 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2173 "%l %l %f %f\n",
2174 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2175 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2176 1234567891234LL, 987654321986LL,
2177 42.0, 43.0);
2178 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2179 1, 2, 3, 4, 5, 6, 7, 8, ld);
2180 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2181 "%l %l %f %f %F\n",
2182 1, 2, 3, 4, 5, 6, 7, 8,
2183 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2184 1234567891234LL, 987654321986LL,
2185 42.0, 43.0, ld);
2186 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2187 "%F %l %l %f %f %F\n",
2188 1, 2, 3, 4, 5, 6, 7, 8,
2189 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2190 ld, 1234567891234LL, 987654321986LL,
2191 42.0, 43.0, ld);
2193 bob.profile = 42;
2194 stdarg_for_struct(bob, bob, bob, bob.profile);
2195 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2198 void whitespace_test(void)
2200 char *str;
2202 \f\v #if 1
2203 pri\
2204 ntf("whitspace:\n");\f\v
2205 #endif
2206 pf("N=%d\n", 2);
2208 #ifdef CORRECT_CR_HANDLING
2209 pri\
2210 ntf("aaa=%d\n", 3);
2211 #endif
2213 pri\
2215 ntf("min=%d\n", 4);
2217 #ifdef ACCEPT_CR_IN_STRINGS
2218 printf("len1=%d\n", strlen("
2219 "));
2220 #ifdef CORRECT_CR_HANDLING
2221 str = "
2223 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
2224 #endif
2225 printf("len1=%d\n", strlen(" a
2226 "));
2227 #endif /* ACCEPT_CR_IN_STRINGS */
2230 int reltab[3] = { 1, 2, 3 };
2232 int *rel1 = &reltab[1];
2233 int *rel2 = &reltab[2];
2235 void getmyaddress(void)
2237 printf("in getmyaddress\n");
2239 unsigned long theaddress = (unsigned long)getmyaddress;
2240 void relocation_test(void)
2242 void (*fptr)(void) = (void (*)(void))theaddress;
2243 printf("*rel1=%d\n", *rel1);
2244 printf("*rel2=%d\n", *rel2);
2245 fptr();
2248 void old_style_f(a,b,c)
2249 int a, b;
2250 double c;
2252 printf("a=%d b=%d b=%f\n", a, b, c);
2255 void decl_func1(int cmpfn())
2257 printf("cmpfn=%lx\n", (long)cmpfn);
2260 void decl_func2(cmpfn)
2261 int cmpfn();
2263 printf("cmpfn=%lx\n", (long)cmpfn);
2266 void old_style_function(void)
2268 old_style_f((void *)1, 2, 3.0);
2269 decl_func1(NULL);
2270 decl_func2(NULL);
2273 void alloca_test()
2275 #if defined __i386__ || defined __x86_64__ || defined __arm__
2276 char *p = alloca(16);
2277 strcpy(p,"123456789012345");
2278 printf("alloca: p is %s\n", p);
2279 char *demo = "This is only a test.\n";
2280 /* Test alloca embedded in a larger expression */
2281 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
2282 #endif
2285 void *bounds_checking_is_enabled()
2287 char ca[10], *cp = ca-1;
2288 return (ca != cp + 1) ? cp : NULL;
2291 typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
2293 void c99_vla_test(int size1, int size2)
2295 #if defined __i386__ || defined __x86_64__
2296 int size = size1 * size2;
2297 int tab1[size][2], tab2[10][2];
2298 void *tab1_ptr, *tab2_ptr, *bad_ptr;
2300 /* "size" should have been 'captured' at tab1 declaration,
2301 so modifying it should have no effect on VLA behaviour. */
2302 size = size-1;
2304 printf("Test C99 VLA 1 (sizeof): ");
2305 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
2306 tab1_ptr = tab1;
2307 tab2_ptr = tab2;
2308 printf("Test C99 VLA 2 (ptrs subtract): ");
2309 printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2310 printf("Test C99 VLA 3 (ptr add): ");
2311 printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2312 printf("Test C99 VLA 4 (ptr access): ");
2313 tab1[size1][1] = 42;
2314 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2316 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2317 if (bad_ptr = bounds_checking_is_enabled()) {
2318 int *t1 = &tab1[size1 * size2 - 1][3];
2319 int *t2 = &tab2[9][3];
2320 printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
2321 printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
2323 char*c1 = 1 + sizeof(tab1) + (char*)tab1;
2324 char*c2 = 1 + sizeof(tab2) + (char*)tab2;
2325 printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
2326 printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
2328 int *i1 = tab1[-1];
2329 int *i2 = tab2[-1];
2330 printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
2331 printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
2333 int *x1 = tab1[size1 * size2 + 1];
2334 int *x2 = tab2[10 + 1];
2335 printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
2336 printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
2337 } else {
2338 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2340 printf("\n");
2341 #endif
2344 #ifndef __TINYC__
2345 typedef __SIZE_TYPE__ uintptr_t;
2346 #endif
2348 void sizeof_test(void)
2350 int a;
2351 int **ptr;
2353 printf("sizeof(int) = %d\n", sizeof(int));
2354 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2355 printf("sizeof(long) = %d\n", sizeof(long));
2356 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2357 printf("sizeof(short) = %d\n", sizeof(short));
2358 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2359 printf("sizeof(char) = %d\n", sizeof(char));
2360 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2361 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2362 a = 1;
2363 printf("sizeof(a++) = %d\n", sizeof a++);
2364 printf("a=%d\n", a);
2365 ptr = NULL;
2366 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
2368 /* The type of sizeof should be as large as a pointer, actually
2369 it should be size_t. */
2370 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2371 uintptr_t t = 1;
2372 uintptr_t t2;
2373 /* Effectively <<32, but defined also on 32bit machines. */
2374 t <<= 16;
2375 t <<= 16;
2376 t++;
2377 /* This checks that sizeof really can be used to manipulate
2378 uintptr_t objects, without truncation. */
2379 t2 = t & -sizeof(uintptr_t);
2380 printf ("%lu %lu\n", t, t2);
2382 /* some alignof tests */
2383 printf("__alignof__(int) = %d\n", __alignof__(int));
2384 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2385 printf("__alignof__(short) = %d\n", __alignof__(short));
2386 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2387 printf("__alignof__(char) = %d\n", __alignof__(char));
2388 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2389 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
2392 void typeof_test(void)
2394 double a;
2395 typeof(a) b;
2396 typeof(float) c;
2398 a = 1.5;
2399 b = 2.5;
2400 c = 3.5;
2401 printf("a=%f b=%f c=%f\n", a, b, c);
2404 void statement_expr_test(void)
2406 int a, i;
2408 a = 0;
2409 for(i=0;i<10;i++) {
2410 a += 1 +
2411 ( { int b, j;
2412 b = 0;
2413 for(j=0;j<5;j++)
2414 b += j; b;
2415 } );
2417 printf("a=%d\n", a);
2421 void local_label_test(void)
2423 int a;
2424 goto l1;
2426 a = 1 + ({
2427 __label__ l1, l2, l3, l4;
2428 goto l1;
2430 printf("aa1\n");
2431 goto l3;
2433 printf("aa3\n");
2434 goto l4;
2436 printf("aa2\n");
2437 goto l2;
2438 l3:;
2441 printf("a=%d\n", a);
2442 return;
2444 printf("bb1\n");
2445 goto l2;
2447 printf("bb2\n");
2448 goto l4;
2451 /* inline assembler test */
2452 #if defined(__i386__) || defined(__x86_64__)
2454 /* from linux kernel */
2455 static char * strncat1(char * dest,const char * src,size_t count)
2457 long d0, d1, d2, d3;
2458 __asm__ __volatile__(
2459 "repne\n\t"
2460 "scasb\n\t"
2461 "dec %1\n\t"
2462 "mov %8,%3\n"
2463 "1:\tdec %3\n\t"
2464 "js 2f\n\t"
2465 "lodsb\n\t"
2466 "stosb\n\t"
2467 "testb %%al,%%al\n\t"
2468 "jne 1b\n"
2469 "2:\txor %2,%2\n\t"
2470 "stosb"
2471 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2472 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2473 : "memory");
2474 return dest;
2477 static char * strncat2(char * dest,const char * src,size_t count)
2479 long d0, d1, d2, d3;
2480 __asm__ __volatile__(
2481 "repne scasb\n\t" /* one-line repne prefix + string op */
2482 "dec %1\n\t"
2483 "mov %8,%3\n"
2484 "1:\tdec %3\n\t"
2485 "js 2f\n\t"
2486 "lodsb\n\t"
2487 "stosb\n\t"
2488 "testb %%al,%%al\n\t"
2489 "jne 1b\n"
2490 "2:\txor %2,%2\n\t"
2491 "stosb"
2492 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2493 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2494 : "memory");
2495 return dest;
2498 static inline void * memcpy1(void * to, const void * from, size_t n)
2500 long d0, d1, d2;
2501 __asm__ __volatile__(
2502 "rep ; movsl\n\t"
2503 "testb $2,%b4\n\t"
2504 "je 1f\n\t"
2505 "movsw\n"
2506 "1:\ttestb $1,%b4\n\t"
2507 "je 2f\n\t"
2508 "movsb\n"
2509 "2:"
2510 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2511 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2512 : "memory");
2513 return (to);
2516 static inline void * memcpy2(void * to, const void * from, size_t n)
2518 long d0, d1, d2;
2519 __asm__ __volatile__(
2520 "rep movsl\n\t" /* one-line rep prefix + string op */
2521 "testb $2,%b4\n\t"
2522 "je 1f\n\t"
2523 "movsw\n"
2524 "1:\ttestb $1,%b4\n\t"
2525 "je 2f\n\t"
2526 "movsb\n"
2527 "2:"
2528 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2529 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2530 : "memory");
2531 return (to);
2534 static __inline__ void sigaddset1(unsigned int *set, int _sig)
2536 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2539 static __inline__ void sigdelset1(unsigned int *set, int _sig)
2541 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2544 static __inline__ __const__ unsigned int swab32(unsigned int x)
2546 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2547 "rorl $16,%0\n\t" /* swap words */
2548 "xchgb %b0,%h0" /* swap higher bytes */
2549 :"=" "q" (x)
2550 : "0" (x));
2551 return x;
2554 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
2556 unsigned long long res;
2557 #ifdef __x86_64__
2558 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
2559 but still test the 32bit->64bit mull. */
2560 unsigned int resh, resl;
2561 __asm__("mull %2" : "=a" (resl), "=d" (resh) : "a" (a), "r" (b));
2562 res = ((unsigned long long)resh << 32) | resl;
2563 #else
2564 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
2565 #endif
2566 return res;
2569 static __inline__ unsigned long long inc64(unsigned long long a)
2571 unsigned long long res;
2572 #ifdef __x86_64__
2573 /* Using the A constraint is wrong, and increments are tested
2574 elsewere. */
2575 res = a + 1;
2576 #else
2577 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
2578 #endif
2579 return res;
2582 struct struct123 {
2583 int a;
2584 int b;
2586 struct struct1231 {
2587 unsigned long addr;
2590 unsigned long mconstraint_test(struct struct1231 *r)
2592 unsigned long ret;
2593 unsigned int a[2];
2594 a[0] = 0;
2595 __asm__ volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
2596 : "=&r" (ret), "=m" (a)
2597 : "m" (*(struct struct123 *)r->addr));
2598 return ret + a[0];
2601 #ifdef __x86_64__
2602 int fls64(unsigned long long x)
2604 int bitpos = -1;
2605 asm("bsrq %1,%q0"
2606 : "+r" (bitpos)
2607 : "rm" (x));
2608 return bitpos + 1;
2610 #endif
2612 void other_constraints_test(void)
2614 unsigned long ret;
2615 int var;
2616 __asm__ volatile ("movq %P1,%0" : "=r" (ret) : "p" (&var));
2617 printf ("oc1: %d\n", ret == (unsigned long)&var);
2620 unsigned int set;
2622 void asm_test(void)
2624 char buf[128];
2625 unsigned int val;
2626 struct struct123 s1;
2627 struct struct1231 s2 = { (unsigned long)&s1 };
2629 printf("inline asm:\n");
2631 // parse 0x1E-1 as 3 tokens in asm mode
2632 asm volatile ("mov $0x1E-1,%eax");
2634 /* test the no operand case */
2635 asm volatile ("xorl %eax, %eax");
2637 memcpy1(buf, "hello", 6);
2638 strncat1(buf, " worldXXXXX", 3);
2639 printf("%s\n", buf);
2641 memcpy2(buf, "hello", 6);
2642 strncat2(buf, " worldXXXXX", 3);
2643 printf("%s\n", buf);
2645 /* 'A' constraint test */
2646 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2647 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2649 s1.a = 42;
2650 s1.b = 43;
2651 printf("mconstraint: %d", mconstraint_test(&s2));
2652 printf(" %d %d\n", s1.a, s1.b);
2653 other_constraints_test();
2654 set = 0xff;
2655 sigdelset1(&set, 2);
2656 sigaddset1(&set, 16);
2657 /* NOTE: we test here if C labels are correctly restored after the
2658 asm statement */
2659 goto label1;
2660 label2:
2661 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
2662 printf("set=0x%x\n", set);
2663 val = 0x01020304;
2664 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
2665 return;
2666 label1:
2667 goto label2;
2670 #else
2672 void asm_test(void)
2676 #endif
2678 #define COMPAT_TYPE(type1, type2) \
2680 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2681 __builtin_types_compatible_p (type1, type2));\
2684 int constant_p_var;
2686 void builtin_test(void)
2688 short s;
2689 int i;
2690 long long ll;
2691 #if GCC_MAJOR >= 3
2692 COMPAT_TYPE(int, int);
2693 COMPAT_TYPE(int, unsigned int);
2694 COMPAT_TYPE(int, char);
2695 COMPAT_TYPE(int, const int);
2696 COMPAT_TYPE(int, volatile int);
2697 COMPAT_TYPE(int *, int *);
2698 COMPAT_TYPE(int *, void *);
2699 COMPAT_TYPE(int *, const int *);
2700 COMPAT_TYPE(char *, unsigned char *);
2701 COMPAT_TYPE(char *, signed char *);
2702 COMPAT_TYPE(char *, char *);
2703 /* space is needed because tcc preprocessor introduces a space between each token */
2704 COMPAT_TYPE(char * *, void *);
2705 #endif
2706 printf("res = %d\n", __builtin_constant_p(1));
2707 printf("res = %d\n", __builtin_constant_p(1 + 2));
2708 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
2709 printf("res = %d\n", __builtin_constant_p(constant_p_var));
2710 s = 1;
2711 ll = 2;
2712 i = __builtin_choose_expr (1 != 0, ll, s);
2713 printf("bce: %d\n", i);
2714 i = __builtin_choose_expr (1 != 1, ll, s);
2715 printf("bce: %d\n", i);
2716 i = sizeof (__builtin_choose_expr (1, ll, s));
2717 printf("bce: %d\n", i);
2718 i = sizeof (__builtin_choose_expr (0, ll, s));
2719 printf("bce: %d\n", i);
2722 #ifndef _WIN32
2723 extern int __attribute__((weak)) weak_f1(void);
2724 extern int __attribute__((weak)) weak_f2(void);
2725 extern int weak_f3(void);
2726 extern int __attribute__((weak)) weak_v1;
2727 extern int __attribute__((weak)) weak_v2;
2728 extern int weak_v3;
2730 extern int (*weak_fpa)() __attribute__((weak));
2731 extern int __attribute__((weak)) (*weak_fpb)();
2732 extern __attribute__((weak)) int (*weak_fpc)();
2734 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
2735 extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
2736 extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
2737 extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
2738 extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
2739 extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
2741 static const size_t dummy = 0;
2742 extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
2743 extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
2744 extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
2746 int some_lib_func(void);
2747 int dummy_impl_of_slf(void) { return 444; }
2748 int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
2750 int weak_toolate() __attribute__((weak));
2751 int weak_toolate() { return 0; }
2753 void __attribute__((weak)) weak_test(void)
2755 printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
2756 printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
2757 printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
2758 printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
2759 printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
2760 printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
2762 printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
2763 printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
2764 printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
2766 printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
2767 printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
2768 printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
2769 printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
2770 printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
2771 printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
2774 int __attribute__((weak)) weak_f2() { return 222; }
2775 int __attribute__((weak)) weak_f3() { return 333; }
2776 int __attribute__((weak)) weak_v2 = 222;
2777 int __attribute__((weak)) weak_v3 = 333;
2778 #endif
2780 void const_func(const int a)
2784 void const_warn_test(void)
2786 const_func(1);
2789 struct condstruct {
2790 int i;
2793 int getme (struct condstruct *s, int i)
2795 int i1 = (i == 0 ? 0 : s)->i;
2796 int i2 = (i == 0 ? s : 0)->i;
2797 int i3 = (i == 0 ? (void*)0 : s)->i;
2798 int i4 = (i == 0 ? s : (void*)0)->i;
2799 return i1 + i2 + i3 + i4;
2802 struct global_data
2804 int a[40];
2805 int *b[40];
2808 struct global_data global_data;
2810 int global_data_getstuff (int *, int);
2812 void global_data_callit (int i)
2814 *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
2817 int global_data_getstuff (int *p, int i)
2819 return *p + i;
2822 void global_data_test (void)
2824 global_data.a[0] = 42;
2825 global_data.b[0] = &global_data.a[0];
2826 global_data_callit (0);
2827 printf ("%d\n", global_data.a[0]);
2830 struct cmpcmpS
2832 unsigned char fill : 3;
2833 unsigned char b1 : 1;
2834 unsigned char b2 : 1;
2835 unsigned char fill2 : 3;
2838 int glob1, glob2, glob3;
2840 void compare_comparisons (struct cmpcmpS *s)
2842 if (s->b1 != (glob1 == glob2)
2843 || (s->b2 != (glob1 == glob3)))
2844 printf ("comparing comparisons broken\n");
2847 void cmp_comparison_test(void)
2849 struct cmpcmpS s;
2850 s.b1 = 1;
2851 glob1 = 42; glob2 = 42;
2852 s.b2 = 0;
2853 glob3 = 43;
2854 compare_comparisons (&s);
2857 int fcompare (double a, double b, int code)
2859 switch (code) {
2860 case 0: return a == b;
2861 case 1: return a != b;
2862 case 2: return a < b;
2863 case 3: return a >= b;
2864 case 4: return a > b;
2865 case 5: return a <= b;
2869 void math_cmp_test(void)
2871 double nan = 0.0/0.0;
2872 double one = 1.0;
2873 double two = 2.0;
2874 int comp = 0;
2875 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
2877 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
2878 And it does this in various ways so that all code generation paths
2879 are checked (generating inverted tests, or non-inverted tests, or
2880 producing a 0/1 value without jumps (that's done in the fcompare
2881 function). */
2882 #define FCMP(a,b,op,iop,code) \
2883 if (fcompare (a,b,code)) \
2884 bug (a,b,op,iop,1); \
2885 if (a op b) \
2886 bug (a,b,op,iop,2); \
2887 if (a iop b) \
2889 else \
2890 bug (a,b,op,iop,3); \
2891 if ((a op b) || comp) \
2892 bug (a,b,op,iop,4); \
2893 if ((a iop b) || comp) \
2895 else \
2896 bug (a,b,op,iop,5);
2898 /* Equality tests. */
2899 FCMP(nan, nan, ==, !=, 0);
2900 FCMP(one, two, ==, !=, 0);
2901 FCMP(one, one, !=, ==, 1);
2902 /* Non-equality is a bit special. */
2903 if (!fcompare (nan, nan, 1))
2904 bug (nan, nan, !=, ==, 6);
2906 /* Relational tests on numbers. */
2907 FCMP(two, one, <, >=, 2);
2908 FCMP(one, two, >=, <, 3);
2909 FCMP(one, two, >, <=, 4);
2910 FCMP(two, one, <=, >, 5);
2912 /* Relational tests on NaNs. Note that the inverse op here is
2913 always !=, there's no operator in C that is equivalent to !(a < b),
2914 when NaNs are involved, same for the other relational ops. */
2915 FCMP(nan, nan, <, !=, 2);
2916 FCMP(nan, nan, >=, !=, 3);
2917 FCMP(nan, nan, >, !=, 4);
2918 FCMP(nan, nan, <=, !=, 5);
2921 double get100 () { return 100.0; }
2923 void callsave_test(void)
2925 #if defined __i386__ || defined __x86_64__ || defined __arm__
2926 int i, s; double *d; double t;
2927 s = sizeof (double);
2928 printf ("callsavetest: %d\n", s);
2929 d = alloca (sizeof(double));
2930 d[0] = 10.0;
2931 /* x86-64 had a bug were the next call to get100 would evict
2932 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
2933 in int type, not pointer type. When alloca returns a pointer
2934 with the high 32 bit set (which is likely on x86-64) the access
2935 generates a segfault. */
2936 i = d[0] > get100 ();
2937 printf ("%d\n", i);
2938 #endif
2942 void bfa3(ptrdiff_t str_offset)
2944 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
2946 void bfa2(ptrdiff_t str_offset)
2948 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
2949 bfa3(str_offset);
2951 void bfa1(ptrdiff_t str_offset)
2953 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
2954 bfa2(str_offset);
2957 void builtin_frame_address_test(void)
2959 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
2960 #ifndef __arm__
2961 char str[] = "__builtin_frame_address";
2962 char *fp0 = __builtin_frame_address(0);
2964 printf("str: %s\n", str);
2965 bfa1(str-fp0);
2966 #endif
2969 char via_volatile (char i)
2971 char volatile vi;
2972 vi = i;
2973 return vi;
2976 struct __attribute__((__packed__)) Spacked {
2977 char a;
2978 short b;
2979 int c;
2981 struct Spacked spacked;
2982 typedef struct __attribute__((__packed__)) {
2983 char a;
2984 short b;
2985 int c;
2986 } Spacked2;
2987 Spacked2 spacked2;
2988 #ifdef BROKEN
2989 /* This doesn't work for now. Requires adjusting field offsets/sizes
2990 after parsing the struct members. */
2991 typedef struct Spacked3_s {
2992 char a;
2993 short b;
2994 int c;
2995 } __attribute__((__packed__)) Spacked3;
2996 Spacked3 spacked3;
2997 #endif
2998 void attrib_test(void)
3000 printf("attr: %d %d %d %d\n", sizeof(struct Spacked),
3001 sizeof(spacked), sizeof(Spacked2), sizeof(spacked2));
3002 #ifdef BROKEN
3003 printf("attr: %d %d\n", sizeof(Spacked3), sizeof(spacked3));
3004 #endif