x86-asm: Implement clflush opcode
[tinycc.git] / tests / tcctest.c
bloba6878db7bede701d3daf1269d976b62c3e9bbf95
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);
112 int fib(int n);
113 void num(int n);
114 void forward_ref(void);
115 int isid(int c);
117 /* Line joining happens before tokenization, so the following
118 must be parsed as ellipsis. */
119 void funny_line_continuation (int, ..\
120 . );
122 char via_volatile (char);
124 #define A 2
125 #define N 1234 + A
126 #define pf printf
127 #define M1(a, b) (a) + (b)
129 #define str\
130 (s) # s
131 #define glue(a, b) a ## b
132 #define xglue(a, b) glue(a, b)
133 #define HIGHLOW "hello"
134 #define LOW LOW ", world"
136 static int onetwothree = 123;
137 #define onetwothree4 onetwothree
138 #define onetwothree xglue(onetwothree,4)
140 #define min(a, b) ((a) < (b) ? (a) : (b))
142 #ifdef C99_MACROS
143 #define dprintf(level,...) printf(__VA_ARGS__)
144 #endif
146 /* gcc vararg macros */
147 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
149 #define MACRO_NOARGS()
151 #define AAA 3
152 #undef AAA
153 #define AAA 4
155 #if 1
156 #define B3 1
157 #elif 1
158 #define B3 2
159 #elif 0
160 #define B3 3
161 #else
162 #define B3 4
163 #endif
165 #define __INT64_C(c) c ## LL
166 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
168 int qq(int x)
170 return x + 40;
172 #define qq(x) x
174 #define spin_lock(lock) do { } while (0)
175 #define wq_spin_lock spin_lock
176 #define TEST2() wq_spin_lock(a)
178 #define UINT_MAX ((unsigned) -1)
180 void intdiv_test(void)
182 printf("18/21=%u\n", 18/21);
183 printf("18%%21=%u\n", 18%21);
184 printf("41/21=%u\n", 41/21);
185 printf("41%%21=%u\n", 41%21);
186 printf("42/21=%u\n", 42/21);
187 printf("42%%21=%u\n", 42%21);
188 printf("43/21=%u\n", 43/21);
189 printf("43%%21=%u\n", 43%21);
190 printf("126/21=%u\n", 126/21);
191 printf("126%%21=%u\n", 126%21);
192 printf("131/21=%u\n", 131/21);
193 printf("131%%21=%u\n", 131%21);
194 printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX/2+3)/2);
195 printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX/2+3)%2);
197 printf("18/-21=%u\n", 18/-21);
198 printf("18%%-21=%u\n", 18%-21);
199 printf("41/-21=%u\n", 41/-21);
200 printf("41%%-21=%u\n", 41%-21);
201 printf("42/-21=%u\n", 42/-21);
202 printf("42%%-21=%u\n", 42%-21);
203 printf("43/-21=%u\n", 43/-21);
204 printf("43%%-21=%u\n", 43%-21);
205 printf("126/-21=%u\n", 126/-21);
206 printf("126%%-21=%u\n", 126%-21);
207 printf("131/-21=%u\n", 131/-21);
208 printf("131%%-21=%u\n", 131%-21);
209 printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX/2+3)/-2);
210 printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX/2+3)%-2);
212 printf("-18/21=%u\n", -18/21);
213 printf("-18%%21=%u\n", -18%21);
214 printf("-41/21=%u\n", -41/21);
215 printf("-41%%21=%u\n", -41%21);
216 printf("-42/21=%u\n", -42/21);
217 printf("-42%%21=%u\n", -42%21);
218 printf("-43/21=%u\n", -43/21);
219 printf("-43%%21=%u\n", -43%21);
220 printf("-126/21=%u\n", -126/21);
221 printf("-126%%21=%u\n", -126%21);
222 printf("-131/21=%u\n", -131/21);
223 printf("-131%%21=%u\n", -131%21);
224 printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX/2+3))/2);
225 printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX/2+3))%2);
227 printf("-18/-21=%u\n", -18/-21);
228 printf("-18%%-21=%u\n", -18%-21);
229 printf("-41/-21=%u\n", -41/-21);
230 printf("-41%%-21=%u\n", -41%-21);
231 printf("-42/-21=%u\n", -42/-21);
232 printf("-42%%-21=%u\n", -42%-21);
233 printf("-43/-21=%u\n", -43/-21);
234 printf("-43%%-21=%u\n", -43%-21);
235 printf("-126/-21=%u\n", -126/-21);
236 printf("-126%%-21=%u\n", -126%-21);
237 printf("-131/-21=%u\n", -131/-21);
238 printf("-131%%-21=%u\n", -131%-21);
239 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX/2+3))/-2);
240 printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX/2+3))%-2);
243 void macro_test(void)
245 printf("macro:\n");\f\v
246 pf("N=%d\n", N);
247 printf("aaa=%d\n", AAA);
249 printf("min=%d\n", min(1, min(2, -1)));
251 printf("s1=%s\n", glue(HIGH, LOW));
252 printf("s2=%s\n", xglue(HIGH, LOW));
253 printf("s3=%s\n", str("c"));
254 printf("s4=%s\n", str(a1));
255 printf("B3=%d\n", B3);
257 printf("onetwothree=%d\n", onetwothree);
259 #ifdef A
260 printf("A defined\n");
261 #endif
262 #ifdef B
263 printf("B defined\n");
264 #endif
265 #ifdef A
266 printf("A defined\n");
267 #else
268 printf("A not defined\n");
269 #endif
270 #ifdef B
271 printf("B defined\n");
272 #else
273 printf("B not defined\n");
274 #endif
276 #ifdef A
277 printf("A defined\n");
278 #ifdef B
279 printf("B1 defined\n");
280 #else
281 printf("B1 not defined\n");
282 #endif
283 #else
284 printf("A not defined\n");
285 #ifdef B
286 printf("B2 defined\n");
287 #else
288 printf("B2 not defined\n");
289 #endif
290 #endif
292 #if 1+1
293 printf("test true1\n");
294 #endif
295 #if 0
296 printf("test true2\n");
297 #endif
298 #if 1-1
299 printf("test true3\n");
300 #endif
301 #if defined(A)
302 printf("test trueA\n");
303 #endif
304 #if defined(B)
305 printf("test trueB\n");
306 #endif
308 #if 0
309 printf("test 0\n");
310 #elif 0
311 printf("test 1\n");
312 #elif 2
313 printf("test 2\n");
314 #else
315 printf("test 3\n");
316 #endif
318 MACRO_NOARGS();
320 #ifdef __LINE__
321 printf("__LINE__ defined\n");
322 #endif
324 printf("__LINE__=%d __FILE__=%s\n",
325 __LINE__, __FILE__);
326 #if 0
327 #line 200
328 printf("__LINE__=%d __FILE__=%s\n",
329 __LINE__, __FILE__);
330 #line 203 "test"
331 printf("__LINE__=%d __FILE__=%s\n",
332 __LINE__, __FILE__);
333 #line 227 "tcctest.c"
334 #endif
336 /* not strictly preprocessor, but we test it there */
337 #ifdef C99_MACROS
338 printf("__func__ = %s\n", __func__);
339 dprintf(1, "vaarg=%d\n", 1);
340 #endif
341 dprintf1(1, "vaarg1\n");
342 dprintf1(1, "vaarg1=%d\n", 2);
343 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
345 /* gcc extension */
346 printf("func='%s'\n", __FUNCTION__);
348 /* complicated macros in glibc */
349 printf("INT64_MIN=" LONG_LONG_FORMAT "\n", INT64_MIN);
351 int a;
352 a = 1;
353 glue(a+, +);
354 printf("a=%d\n", a);
355 glue(a <, <= 2);
356 printf("a=%d\n", a);
359 /* macro function with argument outside the macro string */
360 #define MF_s MF_hello
361 #define MF_hello(msg) printf("%s\n",msg)
363 #define MF_t printf("tralala\n"); MF_hello
365 MF_s("hi");
366 MF_t("hi");
368 /* test macro substituion inside args (should not eat stream) */
369 printf("qq=%d\n", qq(qq)(2));
371 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
372 null argument without a space. gcc 3.2 fixes that. */
374 #define qq1(x) 1
375 printf("qq1=%d\n", qq1( ));
377 /* comment with stray handling *\
379 /* this is a valid *\/ comment */
380 /* this is a valid comment *\*/
381 // this is a valid\
382 comment
384 /* test function macro substitution when the function name is
385 substituted */
386 TEST2();
388 /* And again when the name and parenthes are separated by a
389 comment. */
390 TEST2 /* the comment */ ();
394 static void print_num(char *fn, int line, int num) {
395 printf("fn %s, line %d, num %d\n", fn, line, num);
398 void recursive_macro_test(void)
401 #define ELF32_ST_TYPE(val) ((val) & 0xf)
402 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
403 #define STB_WEAK 2 /* Weak symbol */
404 #define ELFW(type) ELF##32##_##type
405 printf("%d\n", ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(123)));
407 #define WRAP(x) x
409 #define print_num(x) print_num(__FILE__,__LINE__,x)
410 print_num(123);
411 WRAP(print_num(123));
412 WRAP(WRAP(print_num(123)));
414 static struct recursive_macro { int rm_field; } G;
415 #define rm_field (G.rm_field)
416 printf("rm_field = %d\n", rm_field);
417 printf("rm_field = %d\n", WRAP(rm_field));
418 WRAP((printf("rm_field = %d %d\n", rm_field, WRAP(rm_field))));
421 int op(a,b)
423 return a / b;
426 int ret(a)
428 if (a == 2)
429 return 1;
430 if (a == 3)
431 return 2;
432 return 0;
435 void ps(const char *s)
437 int c;
438 while (1) {
439 c = *s;
440 if (c == 0)
441 break;
442 printf("%c", c);
443 s++;
447 const char foo1_string[] = "\
448 bar\n\
449 test\14\
452 void string_test()
454 unsigned int b;
455 printf("string:\n");
456 printf("\141\1423\143\n");/* dezdez test */
457 printf("\x41\x42\x43\x3a\n");
458 printf("c=%c\n", 'r');
459 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
460 printf("foo1_string='%s'\n", foo1_string);
461 #if 0
462 printf("wstring=%S\n", L"abc");
463 printf("wstring=%S\n", L"abc" L"def" "ghi");
464 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
465 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
466 #endif
467 ps("test\n");
468 b = 32;
469 while ((b = b + 1) < 96) {
470 printf("%c", b);
472 printf("\n");
473 printf("fib=%d\n", fib(33));
474 b = 262144;
475 while (b != 0x80000000) {
476 num(b);
477 b = b * 2;
481 void loop_test()
483 int i;
484 i = 0;
485 while (i < 10)
486 printf("%d", i++);
487 printf("\n");
488 for(i = 0; i < 10;i++)
489 printf("%d", i);
490 printf("\n");
491 i = 0;
492 do {
493 printf("%d", i++);
494 } while (i < 10);
495 printf("\n");
497 char count = 123;
498 /* c99 for loop init test */
499 for (size_t count = 1; count < 3; count++)
500 printf("count=%d\n", count);
501 printf("count = %d\n", count);
503 /* break/continue tests */
504 i = 0;
505 while (1) {
506 if (i == 6)
507 break;
508 i++;
509 if (i == 3)
510 continue;
511 printf("%d", i);
513 printf("\n");
515 /* break/continue tests */
516 i = 0;
517 do {
518 if (i == 6)
519 break;
520 i++;
521 if (i == 3)
522 continue;
523 printf("%d", i);
524 } while(1);
525 printf("\n");
527 for(i = 0;i < 10;i++) {
528 if (i == 3)
529 continue;
530 printf("%d", i);
532 printf("\n");
535 typedef int typedef_and_label;
537 void goto_test()
539 int i;
540 static void *label_table[3] = { &&label1, &&label2, &&label3 };
542 printf("goto:\n");
543 i = 0;
544 /* This needs to parse as label, not as start of decl. */
545 typedef_and_label:
546 s_loop:
547 if (i >= 10)
548 goto s_end;
549 printf("%d", i);
550 i++;
551 goto s_loop;
552 s_end:
553 printf("\n");
555 /* we also test computed gotos (GCC extension) */
556 for(i=0;i<3;i++) {
557 goto *label_table[i];
558 label1:
559 printf("label1\n");
560 goto next;
561 label2:
562 printf("label2\n");
563 goto next;
564 label3:
565 printf("label3\n");
566 next: ;
570 enum {
572 E1 = 2,
573 E2 = 4,
578 enum test {
579 E5 = 1000,
582 void enum_test()
584 enum test b1;
585 printf("enum:\n%d %d %d %d %d %d\n",
586 E0, E1, E2, E3, E4, E5);
587 b1 = 1;
588 printf("b1=%d\n", b1);
591 typedef int *my_ptr;
593 typedef int mytype1;
594 typedef int mytype2;
596 void typedef_test()
598 my_ptr a;
599 mytype1 mytype2;
600 int b;
602 a = &b;
603 *a = 1234;
604 printf("typedef:\n");
605 printf("a=%d\n", *a);
606 mytype2 = 2;
607 printf("mytype2=%d\n", mytype2);
610 void forward_test()
612 printf("forward:\n");
613 forward_ref();
614 forward_ref();
618 void forward_ref(void)
620 printf("forward ok\n");
623 typedef struct struct1 {
624 int f1;
625 int f2, f3;
626 union union1 {
627 int v1;
628 int v2;
629 } u;
630 char str[3];
631 } struct1;
633 struct struct2 {
634 int a;
635 char b;
638 union union2 {
639 int w1;
640 int w2;
643 struct struct1 st1, st2;
645 struct empty_mem {
646 /* nothing */ ;
647 int x;
650 int main(int argc, char **argv)
652 string_test();
653 expr_test();
654 macro_test();
655 recursive_macro_test();
656 scope_test();
657 forward_test();
658 funcptr_test();
659 loop_test();
660 switch_test();
661 goto_test();
662 enum_test();
663 typedef_test();
664 struct_test();
665 array_test();
666 expr_ptr_test();
667 bool_test();
668 expr2_test();
669 constant_expr_test();
670 expr_cmp_test();
671 char_short_test();
672 init_test();
673 compound_literal_test();
674 kr_test();
675 struct_assign_test();
676 cast_test();
677 bitfield_test();
678 c99_bool_test();
679 float_test();
680 longlong_test();
681 manyarg_test();
682 stdarg_test();
683 whitespace_test();
684 relocation_test();
685 old_style_function();
686 alloca_test();
687 c99_vla_test(5, 2);
688 sizeof_test();
689 typeof_test();
690 statement_expr_test();
691 local_label_test();
692 asm_test();
693 builtin_test();
694 #ifndef _WIN32
695 weak_test();
696 #endif
697 global_data_test();
698 cmp_comparison_test();
699 math_cmp_test();
700 callsave_test();
701 builtin_frame_address_test();
702 intdiv_test();
703 if (via_volatile (42) != 42)
704 printf ("via_volatile broken\n");
705 return 0;
708 int tab[3];
709 int tab2[3][2];
711 int g;
713 void f1(g)
715 printf("g1=%d\n", g);
718 void scope_test()
720 printf("scope:\n");
721 g = 2;
722 f1(1);
723 printf("g2=%d\n", g);
725 int g;
726 g = 3;
727 printf("g3=%d\n", g);
729 int g;
730 g = 4;
731 printf("g4=%d\n", g);
734 printf("g5=%d\n", g);
737 void array_test()
739 int i, j, a[4];
741 printf("array:\n");
742 printf("sizeof(a) = %d\n", sizeof(a));
743 printf("sizeof(\"a\") = %d\n", sizeof("a"));
744 #ifdef C99_MACROS
745 printf("sizeof(__func__) = %d\n", sizeof(__func__));
746 #endif
747 printf("sizeof tab %d\n", sizeof(tab));
748 printf("sizeof tab2 %d\n", sizeof tab2);
749 tab[0] = 1;
750 tab[1] = 2;
751 tab[2] = 3;
752 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
753 for(i=0;i<3;i++)
754 for(j=0;j<2;j++)
755 tab2[i][j] = 10 * i + j;
756 for(i=0;i<3*2;i++) {
757 printf(" %3d", ((int *)tab2)[i]);
759 printf("\n");
760 printf("sizeof(size_t)=%d\n", sizeof(size_t));
761 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
764 void expr_test()
766 int a, b;
767 a = 0;
768 printf("%d\n", a += 1);
769 printf("%d\n", a -= 2);
770 printf("%d\n", a *= 31232132);
771 printf("%d\n", a /= 4);
772 printf("%d\n", a %= 20);
773 printf("%d\n", a &= 6);
774 printf("%d\n", a ^= 7);
775 printf("%d\n", a |= 8);
776 printf("%d\n", a >>= 3);
777 printf("%d\n", a <<= 4);
779 a = 22321;
780 b = -22321;
781 printf("%d\n", a + 1);
782 printf("%d\n", a - 2);
783 printf("%d\n", a * 312);
784 printf("%d\n", a / 4);
785 printf("%d\n", b / 4);
786 printf("%d\n", (unsigned)b / 4);
787 printf("%d\n", a % 20);
788 printf("%d\n", b % 20);
789 printf("%d\n", (unsigned)b % 20);
790 printf("%d\n", a & 6);
791 printf("%d\n", a ^ 7);
792 printf("%d\n", a | 8);
793 printf("%d\n", a >> 3);
794 printf("%d\n", b >> 3);
795 printf("%d\n", (unsigned)b >> 3);
796 printf("%d\n", a << 4);
797 printf("%d\n", ~a);
798 printf("%d\n", -a);
799 printf("%d\n", +a);
801 printf("%d\n", 12 + 1);
802 printf("%d\n", 12 - 2);
803 printf("%d\n", 12 * 312);
804 printf("%d\n", 12 / 4);
805 printf("%d\n", 12 % 20);
806 printf("%d\n", 12 & 6);
807 printf("%d\n", 12 ^ 7);
808 printf("%d\n", 12 | 8);
809 printf("%d\n", 12 >> 2);
810 printf("%d\n", 12 << 4);
811 printf("%d\n", ~12);
812 printf("%d\n", -12);
813 printf("%d\n", +12);
814 printf("%d %d %d %d\n",
815 isid('a'),
816 isid('g'),
817 isid('T'),
818 isid('('));
821 int isid(int c)
823 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
826 /**********************/
828 int vstack[10], *vstack_ptr;
830 void vpush(int vt, int vc)
832 *vstack_ptr++ = vt;
833 *vstack_ptr++ = vc;
836 void vpop(int *ft, int *fc)
838 *fc = *--vstack_ptr;
839 *ft = *--vstack_ptr;
842 void expr2_test()
844 int a, b;
846 printf("expr2:\n");
847 vstack_ptr = vstack;
848 vpush(1432432, 2);
849 vstack_ptr[-2] &= ~0xffffff80;
850 vpop(&a, &b);
851 printf("res= %d %d\n", a, b);
854 void constant_expr_test()
856 int a;
857 printf("constant_expr:\n");
858 a = 3;
859 printf("%d\n", a * 16);
860 printf("%d\n", a * 1);
861 printf("%d\n", a + 0);
864 int tab4[10];
866 void expr_ptr_test()
868 int *p, *q;
869 int i = -1;
871 printf("expr_ptr:\n");
872 p = tab4;
873 q = tab4 + 10;
874 printf("diff=%d\n", q - p);
875 p++;
876 printf("inc=%d\n", p - tab4);
877 p--;
878 printf("dec=%d\n", p - tab4);
879 ++p;
880 printf("inc=%d\n", p - tab4);
881 --p;
882 printf("dec=%d\n", p - tab4);
883 printf("add=%d\n", p + 3 - tab4);
884 printf("add=%d\n", 3 + p - tab4);
886 /* check if 64bit support is ok */
887 q = p = 0;
888 q += i;
889 printf("%p %p %ld\n", q, p, p-q);
890 printf("%d %d %d %d %d %d\n",
891 p == q, p != q, p < q, p <= q, p >= q, p > q);
892 i = 0xf0000000;
893 p += i;
894 printf("%p %p %ld\n", q, p, p-q);
895 printf("%d %d %d %d %d %d\n",
896 p == q, p != q, p < q, p <= q, p >= q, p > q);
897 p = (int *)((char *)p + 0xf0000000);
898 printf("%p %p %ld\n", q, p, p-q);
899 printf("%d %d %d %d %d %d\n",
900 p == q, p != q, p < q, p <= q, p >= q, p > q);
901 p += 0xf0000000;
902 printf("%p %p %ld\n", q, p, p-q);
903 printf("%d %d %d %d %d %d\n",
904 p == q, p != q, p < q, p <= q, p >= q, p > q);
906 struct size12 {
907 int i, j, k;
909 struct size12 s[2], *sp = s;
910 int i, j;
911 sp->i = 42;
912 sp++;
913 j = -1;
914 printf("%d\n", sp[j].i);
918 void expr_cmp_test()
920 int a, b;
921 printf("constant_expr:\n");
922 a = -1;
923 b = 1;
924 printf("%d\n", a == a);
925 printf("%d\n", a != a);
927 printf("%d\n", a < b);
928 printf("%d\n", a <= b);
929 printf("%d\n", a <= a);
930 printf("%d\n", b >= a);
931 printf("%d\n", a >= a);
932 printf("%d\n", b > a);
934 printf("%d\n", (unsigned)a < b);
935 printf("%d\n", (unsigned)a <= b);
936 printf("%d\n", (unsigned)a <= a);
937 printf("%d\n", (unsigned)b >= a);
938 printf("%d\n", (unsigned)a >= a);
939 printf("%d\n", (unsigned)b > a);
942 struct empty {
945 struct aligntest1 {
946 char a[10];
949 struct aligntest2 {
950 int a;
951 char b[10];
954 struct aligntest3 {
955 double a, b;
958 struct aligntest4 {
959 double a[0];
962 void struct_test()
964 struct1 *s;
965 union union2 u;
967 printf("struct:\n");
968 printf("sizes: %d %d %d %d\n",
969 sizeof(struct struct1),
970 sizeof(struct struct2),
971 sizeof(union union1),
972 sizeof(union union2));
973 st1.f1 = 1;
974 st1.f2 = 2;
975 st1.f3 = 3;
976 printf("st1: %d %d %d\n",
977 st1.f1, st1.f2, st1.f3);
978 st1.u.v1 = 1;
979 st1.u.v2 = 2;
980 printf("union1: %d\n", st1.u.v1);
981 u.w1 = 1;
982 u.w2 = 2;
983 printf("union2: %d\n", u.w1);
984 s = &st2;
985 s->f1 = 3;
986 s->f2 = 2;
987 s->f3 = 1;
988 printf("st2: %d %d %d\n",
989 s->f1, s->f2, s->f3);
990 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
992 /* align / size tests */
993 printf("aligntest1 sizeof=%d alignof=%d\n",
994 sizeof(struct aligntest1), __alignof__(struct aligntest1));
995 printf("aligntest2 sizeof=%d alignof=%d\n",
996 sizeof(struct aligntest2), __alignof__(struct aligntest2));
997 printf("aligntest3 sizeof=%d alignof=%d\n",
998 sizeof(struct aligntest3), __alignof__(struct aligntest3));
999 printf("aligntest4 sizeof=%d alignof=%d\n",
1000 sizeof(struct aligntest4), __alignof__(struct aligntest4));
1002 /* empty structures (GCC extension) */
1003 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
1004 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
1007 /* XXX: depend on endianness */
1008 void char_short_test()
1010 int var1, var2;
1012 printf("char_short:\n");
1014 var1 = 0x01020304;
1015 var2 = 0xfffefdfc;
1016 printf("s8=%d %d\n",
1017 *(char *)&var1, *(char *)&var2);
1018 printf("u8=%d %d\n",
1019 *(unsigned char *)&var1, *(unsigned char *)&var2);
1020 printf("s16=%d %d\n",
1021 *(short *)&var1, *(short *)&var2);
1022 printf("u16=%d %d\n",
1023 *(unsigned short *)&var1, *(unsigned short *)&var2);
1024 printf("s32=%d %d\n",
1025 *(int *)&var1, *(int *)&var2);
1026 printf("u32=%d %d\n",
1027 *(unsigned int *)&var1, *(unsigned int *)&var2);
1028 *(char *)&var1 = 0x08;
1029 printf("var1=%x\n", var1);
1030 *(short *)&var1 = 0x0809;
1031 printf("var1=%x\n", var1);
1032 *(int *)&var1 = 0x08090a0b;
1033 printf("var1=%x\n", var1);
1036 /******************/
1038 typedef struct Sym {
1039 int v;
1040 int t;
1041 int c;
1042 struct Sym *next;
1043 struct Sym *prev;
1044 } Sym;
1046 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1047 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1049 static int toupper1(int a)
1051 return TOUPPER(a);
1054 void bool_test()
1056 int *s, a, b, t, f, i;
1058 a = 0;
1059 s = (void*)0;
1060 printf("!s=%d\n", !s);
1062 if (!s || !s[0])
1063 a = 1;
1064 printf("a=%d\n", a);
1066 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1067 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1068 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1069 #if 1 && 1
1070 printf("a1\n");
1071 #endif
1072 #if 1 || 0
1073 printf("a2\n");
1074 #endif
1075 #if 1 ? 0 : 1
1076 printf("a3\n");
1077 #endif
1078 #if 0 ? 0 : 1
1079 printf("a4\n");
1080 #endif
1082 a = 4;
1083 printf("b=%d\n", a + (0 ? 1 : a / 2));
1085 /* test register spilling */
1086 a = 10;
1087 b = 10;
1088 a = (a + b) * ((a < b) ?
1089 ((b - a) * (a - b)): a + b);
1090 printf("a=%d\n", a);
1092 /* test complex || or && expressions */
1093 t = 1;
1094 f = 0;
1095 a = 32;
1096 printf("exp=%d\n", f == (32 <= a && a <= 3));
1097 printf("r=%d\n", (t || f) + (t && f));
1099 /* test ? : cast */
1101 int aspect_on;
1102 int aspect_native = 65536;
1103 double bfu_aspect = 1.0;
1104 int aspect;
1105 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
1106 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
1107 printf("aspect=%d\n", aspect);
1111 /* test ? : GCC extension */
1113 static int v1 = 34 ? : -1; /* constant case */
1114 static int v2 = 0 ? : -1; /* constant case */
1115 int a = 30;
1117 printf("%d %d\n", v1, v2);
1118 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
1121 /* again complex expression */
1122 for(i=0;i<256;i++) {
1123 if (toupper1 (i) != TOUPPER (i))
1124 printf("error %d\n", i);
1128 /* GCC accepts that */
1129 static int tab_reinit[];
1130 static int tab_reinit[10];
1132 //int cinit1; /* a global variable can be defined several times without error ! */
1133 int cinit1;
1134 int cinit1;
1135 int cinit1 = 0;
1136 int *cinit2 = (int []){3, 2, 1};
1138 void compound_literal_test(void)
1140 int *p, i;
1141 char *q, *q3;
1143 printf("compound_test:\n");
1145 p = (int []){1, 2, 3};
1146 for(i=0;i<3;i++)
1147 printf(" %d", p[i]);
1148 printf("\n");
1150 for(i=0;i<3;i++)
1151 printf("%d", cinit2[i]);
1152 printf("\n");
1154 q = "tralala1";
1155 printf("q1=%s\n", q);
1157 q = (char *){ "tralala2" };
1158 printf("q2=%s\n", q);
1160 q3 = (char *){ q };
1161 printf("q3=%s\n", q3);
1163 q = (char []){ "tralala3" };
1164 printf("q4=%s\n", q);
1166 #ifdef ALL_ISOC99
1167 p = (int []){1, 2, cinit1 + 3};
1168 for(i=0;i<3;i++)
1169 printf(" %d", p[i]);
1170 printf("\n");
1172 for(i=0;i<3;i++) {
1173 p = (int []){1, 2, 4 + i};
1174 printf("%d %d %d\n",
1175 p[0],
1176 p[1],
1177 p[2]);
1179 #endif
1182 /* K & R protos */
1184 kr_func1(a, b)
1186 return a + b;
1189 int kr_func2(a, b)
1191 return a + b;
1194 kr_test()
1196 printf("kr_test:\n");
1197 printf("func1=%d\n", kr_func1(3, 4));
1198 printf("func2=%d\n", kr_func2(3, 4));
1199 return 0;
1202 void num(int n)
1204 char *tab, *p;
1205 tab = (char*)malloc(20);
1206 p = tab;
1207 while (1) {
1208 *p = 48 + (n % 10);
1209 p++;
1210 n = n / 10;
1211 if (n == 0)
1212 break;
1214 while (p != tab) {
1215 p--;
1216 printf("%c", *p);
1218 printf("\n");
1219 free(tab);
1222 /* structure assignment tests */
1223 struct structa1 {
1224 int f1;
1225 char f2;
1228 struct structa1 ssta1;
1230 void struct_assign_test1(struct structa1 s1, int t, float f)
1232 printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
1235 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1237 s1.f1 += t;
1238 s1.f2 -= t;
1239 return s1;
1242 void struct_assign_test(void)
1244 struct S {
1245 struct structa1 lsta1, lsta2;
1246 int i;
1247 } s, *ps;
1249 ps = &s;
1250 ps->i = 4;
1251 #if 0
1252 printf("struct_assign_test:\n");
1254 s.lsta1.f1 = 1;
1255 s.lsta1.f2 = 2;
1256 printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
1257 s.lsta2 = s.lsta1;
1258 printf("%d %d\n", s.lsta2.f1, s.lsta2.f2);
1259 #else
1260 s.lsta2.f1 = 1;
1261 s.lsta2.f2 = 2;
1262 #endif
1263 struct_assign_test1(ps->lsta2, 3, 4.5);
1265 printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
1266 ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
1267 printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
1269 static struct {
1270 void (*elem)();
1271 } t[] = {
1272 /* XXX: we should allow this even without braces */
1273 { struct_assign_test }
1275 printf("%d\n", struct_assign_test == t[0].elem);
1278 /* casts to short/char */
1280 void cast1(char a, short b, unsigned char c, unsigned short d)
1282 printf("%d %d %d %d\n", a, b, c, d);
1285 char bcast;
1286 short scast;
1288 void cast_test()
1290 int a;
1291 char c;
1292 char tab[10];
1293 unsigned b,d;
1294 short s;
1295 char *p = NULL;
1296 p -= 0x700000000042;
1298 printf("cast_test:\n");
1299 a = 0xfffff;
1300 cast1(a, a, a, a);
1301 a = 0xffffe;
1302 printf("%d %d %d %d\n",
1303 (char)(a + 1),
1304 (short)(a + 1),
1305 (unsigned char)(a + 1),
1306 (unsigned short)(a + 1));
1307 printf("%d %d %d %d\n",
1308 (char)0xfffff,
1309 (short)0xfffff,
1310 (unsigned char)0xfffff,
1311 (unsigned short)0xfffff);
1313 a = (bcast = 128) + 1;
1314 printf("%d\n", a);
1315 a = (scast = 65536) + 1;
1316 printf("%d\n", a);
1318 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1320 /* test cast from unsigned to signed short to int */
1321 b = 0xf000;
1322 d = (short)b;
1323 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1324 b = 0xf0f0;
1325 d = (char)b;
1326 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1328 /* test implicit int casting for array accesses */
1329 c = 0;
1330 tab[1] = 2;
1331 tab[c] = 1;
1332 printf("%d %d\n", tab[0], tab[1]);
1334 /* test implicit casting on some operators */
1335 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1336 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1337 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1339 /* from pointer to integer types */
1340 printf("%d %d %ld %ld %lld %lld\n",
1341 (int)p, (unsigned int)p,
1342 (long)p, (unsigned long)p,
1343 (long long)p, (unsigned long long)p);
1345 /* from integers to pointers */
1346 printf("%p %p %p %p\n",
1347 (void *)a, (void *)b, (void *)c, (void *)d);
1350 /* initializers tests */
1351 struct structinit1 {
1352 int f1;
1353 char f2;
1354 short f3;
1355 int farray[3];
1358 int sinit1 = 2;
1359 int sinit2 = { 3 };
1360 int sinit3[3] = { 1, 2, {{3}}, };
1361 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1362 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1363 int sinit6[] = { 1, 2, 3 };
1364 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1365 char sinit8[] = "hello" "trala";
1367 struct structinit1 sinit9 = { 1, 2, 3 };
1368 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1369 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1370 #ifdef ALL_ISOC99
1371 .farray[0] = 10,
1372 .farray[1] = 11,
1373 .farray[2] = 12,
1374 #endif
1377 char *sinit12 = "hello world";
1378 char *sinit13[] = {
1379 "test1",
1380 "test2",
1381 "test3",
1383 char sinit14[10] = { "abc" };
1384 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1386 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1388 struct bar {
1389 char *s;
1390 int len;
1391 } sinit17[] = {
1392 "a1", 4,
1393 "a2", 1
1396 int sinit18[10] = {
1397 [2 ... 5] = 20,
1399 [8] = 10,
1402 struct complexinit0 {
1403 int a;
1404 int b;
1407 struct complexinit {
1408 int a;
1409 const struct complexinit0 *b;
1412 const static struct complexinit cix[] = {
1413 [0] = {
1414 .a = 2000,
1415 .b = (const struct complexinit0[]) {
1416 { 2001, 2002 },
1417 { 2003, 2003 },
1423 struct complexinit2 {
1424 int a;
1425 int b[];
1428 struct complexinit2 cix20;
1430 struct complexinit2 cix21 = {
1431 .a = 3000,
1432 .b = { 3001, 3002, 3003 }
1435 struct complexinit2 cix22 = {
1436 .a = 4000,
1437 .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1440 void init_test(void)
1442 int linit1 = 2;
1443 int linit2 = { 3 };
1444 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1445 int linit6[] = { 1, 2, 3 };
1446 int i, j;
1447 char linit8[] = "hello" "trala";
1448 int linit12[10] = { 1, 2 };
1449 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1450 char linit14[10] = "abc";
1451 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1452 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1453 int linit17 = sizeof(linit17);
1455 printf("init_test:\n");
1457 printf("sinit1=%d\n", sinit1);
1458 printf("sinit2=%d\n", sinit2);
1459 printf("sinit3=%d %d %d %d\n",
1460 sizeof(sinit3),
1461 sinit3[0],
1462 sinit3[1],
1463 sinit3[2]
1465 printf("sinit6=%d\n", sizeof(sinit6));
1466 printf("sinit7=%d %d %d %d\n",
1467 sizeof(sinit7),
1468 sinit7[0],
1469 sinit7[1],
1470 sinit7[2]
1472 printf("sinit8=%s\n", sinit8);
1473 printf("sinit9=%d %d %d\n",
1474 sinit9.f1,
1475 sinit9.f2,
1476 sinit9.f3
1478 printf("sinit10=%d %d %d\n",
1479 sinit10.f1,
1480 sinit10.f2,
1481 sinit10.f3
1483 printf("sinit11=%d %d %d %d %d %d\n",
1484 sinit11.f1,
1485 sinit11.f2,
1486 sinit11.f3,
1487 sinit11.farray[0],
1488 sinit11.farray[1],
1489 sinit11.farray[2]
1492 for(i=0;i<3;i++)
1493 for(j=0;j<2;j++)
1494 printf("[%d][%d] = %d %d %d\n",
1495 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1496 printf("linit1=%d\n", linit1);
1497 printf("linit2=%d\n", linit2);
1498 printf("linit6=%d\n", sizeof(linit6));
1499 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1501 printf("sinit12=%s\n", sinit12);
1502 printf("sinit13=%d %s %s %s\n",
1503 sizeof(sinit13),
1504 sinit13[0],
1505 sinit13[1],
1506 sinit13[2]);
1507 printf("sinit14=%s\n", sinit14);
1509 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1510 printf("\n");
1511 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1512 printf("\n");
1513 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1514 printf("\n");
1515 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1516 printf("\n");
1517 printf("%d %d %d %d\n",
1518 linit16.a1,
1519 linit16.a2,
1520 linit16.a3,
1521 linit16.a4);
1522 /* test that initialisation is done after variable declare */
1523 printf("linit17=%d\n", linit17);
1524 printf("sinit15=%d\n", sinit15[0]);
1525 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1526 printf("sinit17=%s %d %s %d\n",
1527 sinit17[0].s, sinit17[0].len,
1528 sinit17[1].s, sinit17[1].len);
1529 for(i=0;i<10;i++)
1530 printf("%x ", sinit18[i]);
1531 printf("\n");
1532 /* complex init check */
1533 printf("cix: %d %d %d %d %d %d %d\n",
1534 cix[0].a,
1535 cix[0].b[0].a, cix[0].b[0].b,
1536 cix[0].b[1].a, cix[0].b[1].b,
1537 cix[0].b[2].a, cix[0].b[2].b);
1538 printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
1539 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
1543 void switch_test()
1545 int i;
1547 for(i=0;i<15;i++) {
1548 switch(i) {
1549 case 0:
1550 case 1:
1551 printf("a");
1552 break;
1553 default:
1554 printf("%d", i);
1555 break;
1556 case 8 ... 12:
1557 printf("c");
1558 break;
1559 case 3:
1560 printf("b");
1561 break;
1562 case 0xc33c6b9fU:
1563 case 0x7c9eeeb9U:
1564 break;
1567 printf("\n");
1570 /* ISOC99 _Bool type */
1571 void c99_bool_test(void)
1573 #ifdef BOOL_ISOC99
1574 int a;
1575 _Bool b;
1577 printf("bool_test:\n");
1578 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1579 a = 3;
1580 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1581 b = 3;
1582 printf("b = %d\n", b);
1583 b++;
1584 printf("b = %d\n", b);
1585 #endif
1588 void bitfield_test(void)
1590 int a;
1591 short sa;
1592 unsigned char ca;
1593 struct sbf1 {
1594 int f1 : 3;
1595 int : 2;
1596 int f2 : 1;
1597 int : 0;
1598 int f3 : 5;
1599 int f4 : 7;
1600 unsigned int f5 : 7;
1601 } st1;
1602 printf("bitfield_test:");
1603 printf("sizeof(st1) = %d\n", sizeof(st1));
1605 st1.f1 = 3;
1606 st1.f2 = 1;
1607 st1.f3 = 15;
1608 a = 120;
1609 st1.f4 = a;
1610 st1.f5 = a;
1611 st1.f5++;
1612 printf("%d %d %d %d %d\n",
1613 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1614 sa = st1.f5;
1615 ca = st1.f5;
1616 printf("%d %d\n", sa, ca);
1618 st1.f1 = 7;
1619 if (st1.f1 == -1)
1620 printf("st1.f1 == -1\n");
1621 else
1622 printf("st1.f1 != -1\n");
1623 if (st1.f2 == -1)
1624 printf("st1.f2 == -1\n");
1625 else
1626 printf("st1.f2 != -1\n");
1628 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1629 long-long bitfields whose size is not bigger than int */
1630 struct sbf2 {
1631 long long f1 : 45;
1632 long long : 2;
1633 long long f2 : 35;
1634 unsigned long long f3 : 38;
1635 } st2;
1636 st2.f1 = 0x123456789ULL;
1637 a = 120;
1638 st2.f2 = (long long)a << 25;
1639 st2.f3 = a;
1640 st2.f2++;
1641 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
1644 #ifdef __x86_64__
1645 #define FLOAT_FMT "%f\n"
1646 #else
1647 /* x86's float isn't compatible with GCC */
1648 #define FLOAT_FMT "%.5f\n"
1649 #endif
1651 /* declare strto* functions as they are C99 */
1652 double strtod(const char *nptr, char **endptr);
1654 #if defined(_WIN32)
1655 float strtof(const char *nptr, char **endptr) {return (float)strtod(nptr, endptr);}
1656 LONG_DOUBLE strtold(const char *nptr, char **endptr) {return (LONG_DOUBLE)strtod(nptr, endptr);}
1657 #else
1658 float strtof(const char *nptr, char **endptr);
1659 LONG_DOUBLE strtold(const char *nptr, char **endptr);
1660 #endif
1662 #define FTEST(prefix, typename, type, fmt)\
1663 void prefix ## cmp(type a, type b)\
1665 printf("%d %d %d %d %d %d\n",\
1666 a == b,\
1667 a != b,\
1668 a < b,\
1669 a > b,\
1670 a >= b,\
1671 a <= b);\
1672 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1675 a + b,\
1676 a - b,\
1677 a * b,\
1678 a / b,\
1679 -a);\
1680 printf(fmt "\n", ++a);\
1681 printf(fmt "\n", a++);\
1682 printf(fmt "\n", a);\
1683 b = 0;\
1684 printf("%d %d\n", !a, !b);\
1686 void prefix ## fcast(type a)\
1688 float fa;\
1689 double da;\
1690 LONG_DOUBLE la;\
1691 int ia;\
1692 long long llia;\
1693 unsigned int ua;\
1694 unsigned long long llua;\
1695 type b;\
1696 fa = a;\
1697 da = a;\
1698 la = a;\
1699 printf("ftof: %f %f %Lf\n", fa, da, la);\
1700 ia = (int)a;\
1701 llia = (long long)a;\
1702 a = (a >= 0) ? a : -a;\
1703 ua = (unsigned int)a;\
1704 llua = (unsigned long long)a;\
1705 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
1706 ia = -1234;\
1707 ua = 0x81234500;\
1708 llia = -0x123456789012345LL;\
1709 llua = 0xf123456789012345LLU;\
1710 b = ia;\
1711 printf("itof: " fmt "\n", b);\
1712 b = ua;\
1713 printf("utof: " fmt "\n", b);\
1714 b = llia;\
1715 printf("lltof: " fmt "\n", b);\
1716 b = llua;\
1717 printf("ulltof: " fmt "\n", b);\
1720 float prefix ## retf(type a) { return a; }\
1721 double prefix ## retd(type a) { return a; }\
1722 LONG_DOUBLE prefix ## retld(type a) { return a; }\
1724 void prefix ## call(void)\
1726 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1727 printf("double: %f\n", prefix ## retd(42.123456789));\
1728 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1729 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
1732 void prefix ## signed_zeros(void) \
1734 type x = 0.0, y = -0.0, n, p;\
1735 if (x == y)\
1736 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
1737 1.0 / x != 1.0 / y);\
1738 else\
1739 printf ("x != y; this is wrong!\n");\
1741 n = -x;\
1742 if (x == n)\
1743 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
1744 1.0 / x != 1.0 / n);\
1745 else\
1746 printf ("x != -x; this is wrong!\n");\
1748 p = +y;\
1749 if (x == p)\
1750 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
1751 1.0 / x != 1.0 / p);\
1752 else\
1753 printf ("x != +y; this is wrong!\n");\
1754 p = -y;\
1755 if (x == p)\
1756 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
1757 1.0 / x != 1.0 / p);\
1758 else\
1759 printf ("x != -y; this is wrong!\n");\
1761 void prefix ## test(void)\
1763 printf("testing '%s'\n", #typename);\
1764 prefix ## cmp(1, 2.5);\
1765 prefix ## cmp(2, 1.5);\
1766 prefix ## cmp(1, 1);\
1767 prefix ## fcast(234.6);\
1768 prefix ## fcast(-2334.6);\
1769 prefix ## call();\
1770 prefix ## signed_zeros();\
1773 FTEST(f, float, float, "%f")
1774 FTEST(d, double, double, "%f")
1775 FTEST(ld, long double, LONG_DOUBLE, "%Lf")
1777 double ftab1[3] = { 1.2, 3.4, -5.6 };
1780 void float_test(void)
1782 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
1783 float fa, fb;
1784 double da, db;
1785 int a;
1786 unsigned int b;
1788 printf("float_test:\n");
1789 printf("sizeof(float) = %d\n", sizeof(float));
1790 printf("sizeof(double) = %d\n", sizeof(double));
1791 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));
1792 ftest();
1793 dtest();
1794 ldtest();
1795 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1796 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1797 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1798 da = 123;
1799 printf("da=%f\n", da);
1800 fa = 123;
1801 printf("fa=%f\n", fa);
1802 a = 4000000000;
1803 da = a;
1804 printf("da = %f\n", da);
1805 b = 4000000000;
1806 db = b;
1807 printf("db = %f\n", db);
1808 #endif
1811 int fib(int n)
1813 if (n <= 2)
1814 return 1;
1815 else
1816 return fib(n-1) + fib(n-2);
1819 void funcptr_test()
1821 void (*func)(int);
1822 int a;
1823 struct {
1824 int dummy;
1825 void (*func)(int);
1826 } st1;
1828 printf("funcptr:\n");
1829 func = &num;
1830 (*func)(12345);
1831 func = num;
1832 a = 1;
1833 a = 1;
1834 func(12345);
1835 /* more complicated pointer computation */
1836 st1.func = num;
1837 st1.func(12346);
1838 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1839 printf("sizeof2 = %d\n", sizeof funcptr_test);
1840 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1841 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1844 void lloptest(long long a, long long b)
1846 unsigned long long ua, ub;
1848 ua = a;
1849 ub = b;
1850 /* arith */
1851 printf("arith: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1852 a + b,
1853 a - b,
1854 a * b);
1856 if (b != 0) {
1857 printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1858 a / b,
1859 a % b);
1862 /* binary */
1863 printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1864 a & b,
1865 a | b,
1866 a ^ b);
1868 /* tests */
1869 printf("test: %d %d %d %d %d %d\n",
1870 a == b,
1871 a != b,
1872 a < b,
1873 a > b,
1874 a >= b,
1875 a <= b);
1877 printf("utest: %d %d %d %d %d %d\n",
1878 ua == ub,
1879 ua != ub,
1880 ua < ub,
1881 ua > ub,
1882 ua >= ub,
1883 ua <= ub);
1885 /* arith2 */
1886 a++;
1887 b++;
1888 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
1889 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a++, 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 b = ub = 0;
1893 printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
1896 void llshift(long long a, int b)
1898 printf("shift: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1899 (unsigned long long)a >> b,
1900 a >> b,
1901 a << b);
1902 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1903 (unsigned long long)a >> 3,
1904 a >> 3,
1905 a << 3);
1906 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1907 (unsigned long long)a >> 35,
1908 a >> 35,
1909 a << 35);
1912 void llfloat(void)
1914 float fa;
1915 double da;
1916 LONG_DOUBLE lda;
1917 long long la, lb, lc;
1918 unsigned long long ula, ulb, ulc;
1919 la = 0x12345678;
1920 ula = 0x72345678;
1921 la = (la << 20) | 0x12345;
1922 ula = ula << 33;
1923 printf("la=" LONG_LONG_FORMAT " ula=" ULONG_LONG_FORMAT "\n", la, ula);
1925 fa = la;
1926 da = la;
1927 lda = la;
1928 printf("lltof: %f %f %Lf\n", fa, da, lda);
1930 la = fa;
1931 lb = da;
1932 lc = lda;
1933 printf("ftoll: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", la, lb, lc);
1935 fa = ula;
1936 da = ula;
1937 lda = ula;
1938 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1940 ula = fa;
1941 ulb = da;
1942 ulc = lda;
1943 printf("ftoull: " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT "\n", ula, ulb, ulc);
1946 long long llfunc1(int a)
1948 return a * 2;
1951 struct S {
1952 int id;
1953 char item;
1956 long long int value(struct S *v)
1958 return ((long long int)v->item);
1961 long long llfunc2(long long x, long long y, int z)
1963 return x * y * z;
1966 void longlong_test(void)
1968 long long a, b, c;
1969 int ia;
1970 unsigned int ua;
1971 printf("longlong_test:\n");
1972 printf("sizeof(long long) = %d\n", sizeof(long long));
1973 ia = -1;
1974 ua = -2;
1975 a = ia;
1976 b = ua;
1977 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
1978 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %Lx\n",
1979 (long long)1,
1980 (long long)-2,
1981 1LL,
1982 0x1234567812345679);
1983 a = llfunc1(-3);
1984 printf(LONG_LONG_FORMAT "\n", a);
1986 lloptest(1000, 23);
1987 lloptest(0xff, 0x1234);
1988 b = 0x72345678 << 10;
1989 lloptest(-3, b);
1990 llshift(0x123, 5);
1991 llshift(-23, 5);
1992 b = 0x72345678LL << 10;
1993 llshift(b, 47);
1995 llfloat();
1996 #if 1
1997 b = 0x12345678;
1998 a = -1;
1999 c = a + b;
2000 printf("%Lx\n", c);
2001 #endif
2003 /* long long reg spill test */
2005 struct S a;
2007 a.item = 3;
2008 printf("%lld\n", value(&a));
2010 lloptest(0x80000000, 0);
2013 long long *p, v, **pp;
2014 v = 1;
2015 p = &v;
2016 p[0]++;
2017 printf("another long long spill test : %lld\n", *p);
2018 pp = &p;
2020 v = llfunc2(**pp, **pp, ia);
2021 printf("a long long function (arm-)reg-args test : %lld\n", v);
2023 a = 68719476720LL;
2024 b = 4294967295LL;
2025 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
2027 printf(LONG_LONG_FORMAT "\n", 0x123456789LLU);
2029 /* long long pointer deref in argument passing test */
2030 a = 0x123;
2031 long long *p = &a;
2032 llshift(*p, 5);
2035 void manyarg_test(void)
2037 LONG_DOUBLE ld = 1234567891234LL;
2038 printf("manyarg_test:\n");
2039 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2040 1, 2, 3, 4, 5, 6, 7, 8,
2041 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2042 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2043 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2044 1, 2, 3, 4, 5, 6, 7, 8,
2045 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2046 1234567891234LL, 987654321986LL,
2047 42.0, 43.0);
2048 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2049 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2050 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2051 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2052 1234567891234LL, 987654321986LL,
2053 42.0, 43.0);
2054 printf("%d %d %d %d %d %d %d %d %Lf\n",
2055 1, 2, 3, 4, 5, 6, 7, 8, ld);
2056 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2057 LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n",
2058 1, 2, 3, 4, 5, 6, 7, 8,
2059 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2060 1234567891234LL, 987654321986LL,
2061 42.0, 43.0, ld);
2062 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2063 "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n",
2064 1, 2, 3, 4, 5, 6, 7, 8,
2065 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2066 ld, 1234567891234LL, 987654321986LL,
2067 42.0, 43.0, ld);
2070 void vprintf1(const char *fmt, ...)
2072 va_list ap, aq;
2073 const char *p;
2074 int c, i;
2075 double d;
2076 long long ll;
2077 LONG_DOUBLE ld;
2079 va_start(aq, fmt);
2080 va_copy(ap, aq);
2082 p = fmt;
2083 for(;;) {
2084 c = *p;
2085 if (c == '\0')
2086 break;
2087 p++;
2088 if (c == '%') {
2089 c = *p;
2090 switch(c) {
2091 case '\0':
2092 goto the_end;
2093 case 'd':
2094 i = va_arg(ap, int);
2095 printf("%d", i);
2096 break;
2097 case 'f':
2098 d = va_arg(ap, double);
2099 printf("%f", d);
2100 break;
2101 case 'l':
2102 ll = va_arg(ap, long long);
2103 printf(LONG_LONG_FORMAT, ll);
2104 break;
2105 case 'F':
2106 ld = va_arg(ap, LONG_DOUBLE);
2107 printf("%Lf", ld);
2108 break;
2110 p++;
2111 } else {
2112 putchar(c);
2115 the_end:
2116 va_end(aq);
2117 va_end(ap);
2120 struct myspace {
2121 short int profile;
2124 void stdarg_for_struct(struct myspace bob, ...)
2126 struct myspace george, bill;
2127 va_list ap;
2128 short int validate;
2130 va_start(ap, bob);
2131 bill = va_arg(ap, struct myspace);
2132 george = va_arg(ap, struct myspace);
2133 validate = va_arg(ap, int);
2134 printf("stdarg_for_struct: %d %d %d %d\n",
2135 bob.profile, bill.profile, george.profile, validate);
2136 va_end(ap);
2139 void stdarg_for_libc(const char *fmt, ...)
2141 va_list args;
2142 va_start(args, fmt);
2143 vprintf(fmt, args);
2144 va_end(args);
2147 void stdarg_test(void)
2149 LONG_DOUBLE ld = 1234567891234LL;
2150 struct myspace bob;
2152 vprintf1("%d %d %d\n", 1, 2, 3);
2153 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2154 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2155 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2156 vprintf1("%d %f %l %F %d %f %l %F\n",
2157 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2158 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2159 1, 2, 3, 4, 5, 6, 7, 8,
2160 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2161 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2162 1, 2, 3, 4, 5, 6, 7, 8,
2163 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2164 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2165 "%l %l %f %f\n",
2166 1, 2, 3, 4, 5, 6, 7, 8,
2167 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2168 1234567891234LL, 987654321986LL,
2169 42.0, 43.0);
2170 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2171 "%l %l %f %f\n",
2172 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2173 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2174 1234567891234LL, 987654321986LL,
2175 42.0, 43.0);
2176 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2177 1, 2, 3, 4, 5, 6, 7, 8, ld);
2178 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2179 "%l %l %f %f %F\n",
2180 1, 2, 3, 4, 5, 6, 7, 8,
2181 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2182 1234567891234LL, 987654321986LL,
2183 42.0, 43.0, ld);
2184 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2185 "%F %l %l %f %f %F\n",
2186 1, 2, 3, 4, 5, 6, 7, 8,
2187 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2188 ld, 1234567891234LL, 987654321986LL,
2189 42.0, 43.0, ld);
2191 bob.profile = 42;
2192 stdarg_for_struct(bob, bob, bob, bob.profile);
2193 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2196 void whitespace_test(void)
2198 char *str;
2200 \f\v #if 1
2201 pri\
2202 ntf("whitspace:\n");\f\v
2203 #endif
2204 pf("N=%d\n", 2);
2206 #ifdef CORRECT_CR_HANDLING
2207 pri\
2208 ntf("aaa=%d\n", 3);
2209 #endif
2211 pri\
2213 ntf("min=%d\n", 4);
2215 #ifdef ACCEPT_CR_IN_STRINGS
2216 printf("len1=%d\n", strlen("
2217 "));
2218 #ifdef CORRECT_CR_HANDLING
2219 str = "
2221 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
2222 #endif
2223 printf("len1=%d\n", strlen(" a
2224 "));
2225 #endif /* ACCEPT_CR_IN_STRINGS */
2228 int reltab[3] = { 1, 2, 3 };
2230 int *rel1 = &reltab[1];
2231 int *rel2 = &reltab[2];
2233 void getmyaddress(void)
2235 printf("in getmyaddress\n");
2237 unsigned long theaddress = (unsigned long)getmyaddress;
2238 void relocation_test(void)
2240 void (*fptr)(void) = (void (*)(void))theaddress;
2241 printf("*rel1=%d\n", *rel1);
2242 printf("*rel2=%d\n", *rel2);
2243 fptr();
2246 void old_style_f(a,b,c)
2247 int a, b;
2248 double c;
2250 printf("a=%d b=%d b=%f\n", a, b, c);
2253 void decl_func1(int cmpfn())
2255 printf("cmpfn=%lx\n", (long)cmpfn);
2258 void decl_func2(cmpfn)
2259 int cmpfn();
2261 printf("cmpfn=%lx\n", (long)cmpfn);
2264 void old_style_function(void)
2266 old_style_f((void *)1, 2, 3.0);
2267 decl_func1(NULL);
2268 decl_func2(NULL);
2271 void alloca_test()
2273 #if defined __i386__ || defined __x86_64__ || defined __arm__
2274 char *p = alloca(16);
2275 strcpy(p,"123456789012345");
2276 printf("alloca: p is %s\n", p);
2277 char *demo = "This is only a test.\n";
2278 /* Test alloca embedded in a larger expression */
2279 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
2280 #endif
2283 void *bounds_checking_is_enabled()
2285 char ca[10], *cp = ca-1;
2286 return (ca != cp + 1) ? cp : NULL;
2289 typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
2291 void c99_vla_test(int size1, int size2)
2293 #if defined __i386__ || defined __x86_64__
2294 int size = size1 * size2;
2295 int tab1[size][2], tab2[10][2];
2296 void *tab1_ptr, *tab2_ptr, *bad_ptr;
2298 /* "size" should have been 'captured' at tab1 declaration,
2299 so modifying it should have no effect on VLA behaviour. */
2300 size = size-1;
2302 printf("Test C99 VLA 1 (sizeof): ");
2303 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
2304 tab1_ptr = tab1;
2305 tab2_ptr = tab2;
2306 printf("Test C99 VLA 2 (ptrs subtract): ");
2307 printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2308 printf("Test C99 VLA 3 (ptr add): ");
2309 printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2310 printf("Test C99 VLA 4 (ptr access): ");
2311 tab1[size1][1] = 42;
2312 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2314 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2315 if (bad_ptr = bounds_checking_is_enabled()) {
2316 int *t1 = &tab1[size1 * size2 - 1][3];
2317 int *t2 = &tab2[9][3];
2318 printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
2319 printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
2321 char*c1 = 1 + sizeof(tab1) + (char*)tab1;
2322 char*c2 = 1 + sizeof(tab2) + (char*)tab2;
2323 printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
2324 printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
2326 int *i1 = tab1[-1];
2327 int *i2 = tab2[-1];
2328 printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
2329 printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
2331 int *x1 = tab1[size1 * size2 + 1];
2332 int *x2 = tab2[10 + 1];
2333 printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
2334 printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
2335 } else {
2336 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2338 printf("\n");
2339 #endif
2342 #ifndef __TINYC__
2343 typedef __SIZE_TYPE__ uintptr_t;
2344 #endif
2346 void sizeof_test(void)
2348 int a;
2349 int **ptr;
2351 printf("sizeof(int) = %d\n", sizeof(int));
2352 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2353 printf("sizeof(long) = %d\n", sizeof(long));
2354 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2355 printf("sizeof(short) = %d\n", sizeof(short));
2356 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2357 printf("sizeof(char) = %d\n", sizeof(char));
2358 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2359 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2360 a = 1;
2361 printf("sizeof(a++) = %d\n", sizeof a++);
2362 printf("a=%d\n", a);
2363 ptr = NULL;
2364 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
2366 /* The type of sizeof should be as large as a pointer, actually
2367 it should be size_t. */
2368 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2369 uintptr_t t = 1;
2370 uintptr_t t2;
2371 /* Effectively <<32, but defined also on 32bit machines. */
2372 t <<= 16;
2373 t <<= 16;
2374 t++;
2375 /* This checks that sizeof really can be used to manipulate
2376 uintptr_t objects, without truncation. */
2377 t2 = t & -sizeof(uintptr_t);
2378 printf ("%lu %lu\n", t, t2);
2380 /* some alignof tests */
2381 printf("__alignof__(int) = %d\n", __alignof__(int));
2382 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2383 printf("__alignof__(short) = %d\n", __alignof__(short));
2384 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2385 printf("__alignof__(char) = %d\n", __alignof__(char));
2386 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2387 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
2390 void typeof_test(void)
2392 double a;
2393 typeof(a) b;
2394 typeof(float) c;
2396 a = 1.5;
2397 b = 2.5;
2398 c = 3.5;
2399 printf("a=%f b=%f c=%f\n", a, b, c);
2402 void statement_expr_test(void)
2404 int a, i;
2406 a = 0;
2407 for(i=0;i<10;i++) {
2408 a += 1 +
2409 ( { int b, j;
2410 b = 0;
2411 for(j=0;j<5;j++)
2412 b += j; b;
2413 } );
2415 printf("a=%d\n", a);
2419 void local_label_test(void)
2421 int a;
2422 goto l1;
2424 a = 1 + ({
2425 __label__ l1, l2, l3, l4;
2426 goto l1;
2428 printf("aa1\n");
2429 goto l3;
2431 printf("aa3\n");
2432 goto l4;
2434 printf("aa2\n");
2435 goto l2;
2436 l3:;
2439 printf("a=%d\n", a);
2440 return;
2442 printf("bb1\n");
2443 goto l2;
2445 printf("bb2\n");
2446 goto l4;
2449 /* inline assembler test */
2450 #if defined(__i386__) || defined(__x86_64__)
2452 /* from linux kernel */
2453 static char * strncat1(char * dest,const char * src,size_t count)
2455 long d0, d1, d2, d3;
2456 __asm__ __volatile__(
2457 "repne\n\t"
2458 "scasb\n\t"
2459 "dec %1\n\t"
2460 "mov %8,%3\n"
2461 "1:\tdec %3\n\t"
2462 "js 2f\n\t"
2463 "lodsb\n\t"
2464 "stosb\n\t"
2465 "testb %%al,%%al\n\t"
2466 "jne 1b\n"
2467 "2:\txor %2,%2\n\t"
2468 "stosb"
2469 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2470 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2471 : "memory");
2472 return dest;
2475 static char * strncat2(char * dest,const char * src,size_t count)
2477 long d0, d1, d2, d3;
2478 __asm__ __volatile__(
2479 "repne scasb\n\t" /* one-line repne prefix + string op */
2480 "dec %1\n\t"
2481 "mov %8,%3\n"
2482 "1:\tdec %3\n\t"
2483 "js 2f\n\t"
2484 "lodsb\n\t"
2485 "stosb\n\t"
2486 "testb %%al,%%al\n\t"
2487 "jne 1b\n"
2488 "2:\txor %2,%2\n\t"
2489 "stosb"
2490 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2491 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2492 : "memory");
2493 return dest;
2496 static inline void * memcpy1(void * to, const void * from, size_t n)
2498 long d0, d1, d2;
2499 __asm__ __volatile__(
2500 "rep ; movsl\n\t"
2501 "testb $2,%b4\n\t"
2502 "je 1f\n\t"
2503 "movsw\n"
2504 "1:\ttestb $1,%b4\n\t"
2505 "je 2f\n\t"
2506 "movsb\n"
2507 "2:"
2508 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2509 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2510 : "memory");
2511 return (to);
2514 static inline void * memcpy2(void * to, const void * from, size_t n)
2516 long d0, d1, d2;
2517 __asm__ __volatile__(
2518 "rep movsl\n\t" /* one-line rep prefix + string op */
2519 "testb $2,%b4\n\t"
2520 "je 1f\n\t"
2521 "movsw\n"
2522 "1:\ttestb $1,%b4\n\t"
2523 "je 2f\n\t"
2524 "movsb\n"
2525 "2:"
2526 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2527 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2528 : "memory");
2529 return (to);
2532 static __inline__ void sigaddset1(unsigned int *set, int _sig)
2534 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2537 static __inline__ void sigdelset1(unsigned int *set, int _sig)
2539 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2542 static __inline__ __const__ unsigned int swab32(unsigned int x)
2544 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2545 "rorl $16,%0\n\t" /* swap words */
2546 "xchgb %b0,%h0" /* swap higher bytes */
2547 :"=" "q" (x)
2548 : "0" (x));
2549 return x;
2552 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
2554 unsigned long long res;
2555 #ifdef __x86_64__
2556 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
2557 but still test the 32bit->64bit mull. */
2558 unsigned int resh, resl;
2559 __asm__("mull %2" : "=a" (resl), "=d" (resh) : "a" (a), "r" (b));
2560 res = ((unsigned long long)resh << 32) | resl;
2561 #else
2562 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
2563 #endif
2564 return res;
2567 static __inline__ unsigned long long inc64(unsigned long long a)
2569 unsigned long long res;
2570 #ifdef __x86_64__
2571 /* Using the A constraint is wrong, and increments are tested
2572 elsewere. */
2573 res = a + 1;
2574 #else
2575 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
2576 #endif
2577 return res;
2580 struct struct123 {
2581 int a;
2582 int b;
2584 struct struct1231 {
2585 unsigned long addr;
2588 unsigned long mconstraint_test(struct struct1231 *r)
2590 unsigned long ret;
2591 unsigned int a[2];
2592 a[0] = 0;
2593 __asm__ volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
2594 : "=&r" (ret), "=m" (a)
2595 : "m" (*(struct struct123 *)r->addr));
2596 return ret + a[0];
2599 #ifdef __x86_64__
2600 int fls64(unsigned long long x)
2602 int bitpos = -1;
2603 asm("bsrq %1,%q0"
2604 : "+r" (bitpos)
2605 : "rm" (x));
2606 return bitpos + 1;
2608 #endif
2610 void other_constraints_test(void)
2612 unsigned long ret;
2613 int var;
2614 __asm__ volatile ("movq %P1,%0" : "=r" (ret) : "p" (&var));
2615 printf ("oc1: %d\n", ret == (unsigned long)&var);
2618 unsigned int set;
2620 void asm_test(void)
2622 char buf[128];
2623 unsigned int val;
2624 struct struct123 s1;
2625 struct struct1231 s2 = { (unsigned long)&s1 };
2627 printf("inline asm:\n");
2629 // parse 0x1E-1 as 3 tokens in asm mode
2630 asm volatile ("mov $0x1E-1,%eax");
2632 /* test the no operand case */
2633 asm volatile ("xorl %eax, %eax");
2635 memcpy1(buf, "hello", 6);
2636 strncat1(buf, " worldXXXXX", 3);
2637 printf("%s\n", buf);
2639 memcpy2(buf, "hello", 6);
2640 strncat2(buf, " worldXXXXX", 3);
2641 printf("%s\n", buf);
2643 /* 'A' constraint test */
2644 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2645 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2647 s1.a = 42;
2648 s1.b = 43;
2649 printf("mconstraint: %d", mconstraint_test(&s2));
2650 printf(" %d %d\n", s1.a, s1.b);
2651 other_constraints_test();
2652 set = 0xff;
2653 sigdelset1(&set, 2);
2654 sigaddset1(&set, 16);
2655 /* NOTE: we test here if C labels are correctly restored after the
2656 asm statement */
2657 goto label1;
2658 label2:
2659 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
2660 printf("set=0x%x\n", set);
2661 val = 0x01020304;
2662 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
2663 return;
2664 label1:
2665 goto label2;
2668 #else
2670 void asm_test(void)
2674 #endif
2676 #define COMPAT_TYPE(type1, type2) \
2678 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2679 __builtin_types_compatible_p (type1, type2));\
2682 int constant_p_var;
2684 void builtin_test(void)
2686 #if GCC_MAJOR >= 3
2687 COMPAT_TYPE(int, int);
2688 COMPAT_TYPE(int, unsigned int);
2689 COMPAT_TYPE(int, char);
2690 COMPAT_TYPE(int, const int);
2691 COMPAT_TYPE(int, volatile int);
2692 COMPAT_TYPE(int *, int *);
2693 COMPAT_TYPE(int *, void *);
2694 COMPAT_TYPE(int *, const int *);
2695 COMPAT_TYPE(char *, unsigned char *);
2696 COMPAT_TYPE(char *, signed char *);
2697 COMPAT_TYPE(char *, char *);
2698 /* space is needed because tcc preprocessor introduces a space between each token */
2699 COMPAT_TYPE(char * *, void *);
2700 #endif
2701 printf("res = %d\n", __builtin_constant_p(1));
2702 printf("res = %d\n", __builtin_constant_p(1 + 2));
2703 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
2704 printf("res = %d\n", __builtin_constant_p(constant_p_var));
2707 #ifndef _WIN32
2708 extern int __attribute__((weak)) weak_f1(void);
2709 extern int __attribute__((weak)) weak_f2(void);
2710 extern int weak_f3(void);
2711 extern int __attribute__((weak)) weak_v1;
2712 extern int __attribute__((weak)) weak_v2;
2713 extern int weak_v3;
2715 extern int (*weak_fpa)() __attribute__((weak));
2716 extern int __attribute__((weak)) (*weak_fpb)();
2717 extern __attribute__((weak)) int (*weak_fpc)();
2719 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
2720 extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
2721 extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
2722 extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
2723 extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
2724 extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
2726 static const size_t dummy = 0;
2727 extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
2728 extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
2729 extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
2731 int some_lib_func(void);
2732 int dummy_impl_of_slf(void) { return 444; }
2733 int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
2735 int weak_toolate() __attribute__((weak));
2736 int weak_toolate() { return 0; }
2738 void __attribute__((weak)) weak_test(void)
2740 printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
2741 printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
2742 printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
2743 printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
2744 printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
2745 printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
2747 printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
2748 printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
2749 printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
2751 printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
2752 printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
2753 printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
2754 printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
2755 printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
2756 printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
2759 int __attribute__((weak)) weak_f2() { return 222; }
2760 int __attribute__((weak)) weak_f3() { return 333; }
2761 int __attribute__((weak)) weak_v2 = 222;
2762 int __attribute__((weak)) weak_v3 = 333;
2763 #endif
2765 void const_func(const int a)
2769 void const_warn_test(void)
2771 const_func(1);
2774 struct condstruct {
2775 int i;
2778 int getme (struct condstruct *s, int i)
2780 int i1 = (i == 0 ? 0 : s)->i;
2781 int i2 = (i == 0 ? s : 0)->i;
2782 int i3 = (i == 0 ? (void*)0 : s)->i;
2783 int i4 = (i == 0 ? s : (void*)0)->i;
2784 return i1 + i2 + i3 + i4;
2787 struct global_data
2789 int a[40];
2790 int *b[40];
2793 struct global_data global_data;
2795 int global_data_getstuff (int *, int);
2797 void global_data_callit (int i)
2799 *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
2802 int global_data_getstuff (int *p, int i)
2804 return *p + i;
2807 void global_data_test (void)
2809 global_data.a[0] = 42;
2810 global_data.b[0] = &global_data.a[0];
2811 global_data_callit (0);
2812 printf ("%d\n", global_data.a[0]);
2815 struct cmpcmpS
2817 unsigned char fill : 3;
2818 unsigned char b1 : 1;
2819 unsigned char b2 : 1;
2820 unsigned char fill2 : 3;
2823 int glob1, glob2, glob3;
2825 void compare_comparisons (struct cmpcmpS *s)
2827 if (s->b1 != (glob1 == glob2)
2828 || (s->b2 != (glob1 == glob3)))
2829 printf ("comparing comparisons broken\n");
2832 void cmp_comparison_test(void)
2834 struct cmpcmpS s;
2835 s.b1 = 1;
2836 glob1 = 42; glob2 = 42;
2837 s.b2 = 0;
2838 glob3 = 43;
2839 compare_comparisons (&s);
2842 int fcompare (double a, double b, int code)
2844 switch (code) {
2845 case 0: return a == b;
2846 case 1: return a != b;
2847 case 2: return a < b;
2848 case 3: return a >= b;
2849 case 4: return a > b;
2850 case 5: return a <= b;
2854 void math_cmp_test(void)
2856 double nan = 0.0/0.0;
2857 double one = 1.0;
2858 double two = 2.0;
2859 int comp = 0;
2860 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
2862 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
2863 And it does this in various ways so that all code generation paths
2864 are checked (generating inverted tests, or non-inverted tests, or
2865 producing a 0/1 value without jumps (that's done in the fcompare
2866 function). */
2867 #define FCMP(a,b,op,iop,code) \
2868 if (fcompare (a,b,code)) \
2869 bug (a,b,op,iop,1); \
2870 if (a op b) \
2871 bug (a,b,op,iop,2); \
2872 if (a iop b) \
2874 else \
2875 bug (a,b,op,iop,3); \
2876 if ((a op b) || comp) \
2877 bug (a,b,op,iop,4); \
2878 if ((a iop b) || comp) \
2880 else \
2881 bug (a,b,op,iop,5);
2883 /* Equality tests. */
2884 FCMP(nan, nan, ==, !=, 0);
2885 FCMP(one, two, ==, !=, 0);
2886 FCMP(one, one, !=, ==, 1);
2887 /* Non-equality is a bit special. */
2888 if (!fcompare (nan, nan, 1))
2889 bug (nan, nan, !=, ==, 6);
2891 /* Relational tests on numbers. */
2892 FCMP(two, one, <, >=, 2);
2893 FCMP(one, two, >=, <, 3);
2894 FCMP(one, two, >, <=, 4);
2895 FCMP(two, one, <=, >, 5);
2897 /* Relational tests on NaNs. Note that the inverse op here is
2898 always !=, there's no operator in C that is equivalent to !(a < b),
2899 when NaNs are involved, same for the other relational ops. */
2900 FCMP(nan, nan, <, !=, 2);
2901 FCMP(nan, nan, >=, !=, 3);
2902 FCMP(nan, nan, >, !=, 4);
2903 FCMP(nan, nan, <=, !=, 5);
2906 double get100 () { return 100.0; }
2908 void callsave_test(void)
2910 #if defined __i386__ || defined __x86_64__ || defined __arm__
2911 int i, s; double *d; double t;
2912 s = sizeof (double);
2913 printf ("callsavetest: %d\n", s);
2914 d = alloca (sizeof(double));
2915 d[0] = 10.0;
2916 /* x86-64 had a bug were the next call to get100 would evict
2917 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
2918 in int type, not pointer type. When alloca returns a pointer
2919 with the high 32 bit set (which is likely on x86-64) the access
2920 generates a segfault. */
2921 i = d[0] > get100 ();
2922 printf ("%d\n", i);
2923 #endif
2927 void bfa3(ptrdiff_t str_offset)
2929 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
2931 void bfa2(ptrdiff_t str_offset)
2933 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
2934 bfa3(str_offset);
2936 void bfa1(ptrdiff_t str_offset)
2938 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
2939 bfa2(str_offset);
2942 void builtin_frame_address_test(void)
2944 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
2945 #ifndef __arm__
2946 char str[] = "__builtin_frame_address";
2947 char *fp0 = __builtin_frame_address(0);
2949 printf("str: %s\n", str);
2950 bfa1(str-fp0);
2951 #endif
2954 char via_volatile (char i)
2956 char volatile vi;
2957 vi = i;
2958 return vi;