tcc testprogram
[libfirm] / ir / be / test / tcc / tcctest.c
1 /*
2  * TCC auto test program
3  */
4 //#include "config.h"
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdarg.h>
8
9 #if GCC_MAJOR >= 3
10
11 /* Unfortunately, gcc version < 3 does not handle that! */
12 #define ALL_ISOC99
13
14 /* only gcc 3 handles _Bool correctly */
15 #define BOOL_ISOC99
16
17 /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
18 /* Matze: removed this test as I couldn't committ it to svn... */
19 /* #define CORRECT_CR_HANDLING */
20
21 #endif
22
23 /* deprecated and no longer supported in gcc 3.3 */
24 //#define ACCEPT_CR_IN_STRINGS
25
26 /* __VA_ARGS__ and __func__ support */
27 #define C99_MACROS
28
29 /* test various include syntaxes */
30
31 #define TCCLIB_INC <tcclib.h>
32 #define TCCLIB_INC1 <tcclib
33 #define TCCLIB_INC2 h>
34 #define TCCLIB_INC3 "tcclib"
35
36 #include TCCLIB_INC
37
38 #include TCCLIB_INC1.TCCLIB_INC2
39
40 #include TCCLIB_INC1.h>
41
42 /* gcc 3.2 does not accept that (bug ?) */
43 //#include TCCLIB_INC3 ".h"
44
45 #include "tcclib.h"
46
47 #include <tcclib.h>
48
49 void string_test();
50 void expr_test();
51 void macro_test();
52 void scope_test();
53 void forward_test();
54 void funcptr_test();
55 void loop_test();
56 void switch_test();
57 void goto_test();
58 void enum_test();
59 void typedef_test();
60 void struct_test();
61 void array_test();
62 void expr_ptr_test();
63 void bool_test();
64 void expr2_test();
65 void constant_expr_test();
66 void expr_cmp_test();
67 void char_short_test();
68 void init_test(void);
69 void compound_literal_test(void);
70 int kr_test();
71 void struct_assign_test(void);
72 void cast_test(void);
73 void bitfield_test(void);
74 void c99_bool_test(void);
75 void float_test(void);
76 void longlong_test(void);
77 void stdarg_test(void);
78 void whitespace_test(void);
79 void relocation_test(void);
80 void old_style_function(void);
81 void sizeof_test(void);
82 void typeof_test(void);
83 void local_label_test(void);
84 void statement_expr_test(void);
85 void asm_test(void);
86 void builtin_test(void);
87 void static_test(void);
88
89 int fib(int n);
90 void num(int n);
91 void forward_ref(void);
92 int isid(int c);
93
94 #define A 2
95 #define N 1234 + A
96 #define pf printf
97 #define M1(a, b)  (a) + (b)
98
99 #define str\
100 (s) # s
101 #define glue(a, b) a ## b
102 #define xglue(a, b) glue(a, b)
103 #define HIGHLOW "hello"
104 #define LOW LOW ", world"
105
106 #define min(a, b) ((a) < (b) ? (a) : (b))
107
108 #ifdef C99_MACROS
109 #define dprintf(level,...) printf(__VA_ARGS__)
110 #endif
111
112 /* gcc vararg macros */
113 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
114
115 #define MACRO_NOARGS()
116
117 #define AAA 3
118 #undef AAA
119 #define AAA 4
120
121 #if 1
122 #define B3 1
123 #elif 1
124 #define B3 2
125 #elif 0
126 #define B3 3
127 #else
128 #define B3 4
129 #endif
130
131 #define __INT64_C(c)    c ## LL
132 #define INT64_MIN       (-__INT64_C(9223372036854775807)-1)
133
134 int qq(int x)
135 {
136     return x + 40;
137 }
138 #define qq(x) x
139
140 #define spin_lock(lock) do { } while (0)
141 #define wq_spin_lock spin_lock
142 #define TEST2() wq_spin_lock(a)
143
144 void macro_test(void)
145 {
146     printf("macro:\n");\f\v
147     pf("N=%d\n", N);
148     printf("aaa=%d\n", AAA);
149
150     printf("min=%d\n", min(1, min(2, -1)));
151
152     printf("s1=%s\n", glue(HIGH, LOW));
153     printf("s2=%s\n", xglue(HIGH, LOW));
154     printf("s3=%s\n", str("c"));
155     printf("s4=%s\n", str(a1));
156     printf("B3=%d\n", B3);
157
158 #ifdef A
159     printf("A defined\n");
160 #endif
161 #ifdef B
162     printf("B defined\n");
163 #endif
164 #ifdef A
165     printf("A defined\n");
166 #else
167     printf("A not defined\n");
168 #endif
169 #ifdef B
170     printf("B defined\n");
171 #else
172     printf("B not defined\n");
173 #endif
174
175 #ifdef A
176     printf("A defined\n");
177 #ifdef B
178     printf("B1 defined\n");
179 #else
180     printf("B1 not defined\n");
181 #endif
182 #else
183     printf("A not defined\n");
184 #ifdef B
185     printf("B2 defined\n");
186 #else
187     printf("B2 not defined\n");
188 #endif
189 #endif
190
191 #if 1+1
192     printf("test true1\n");
193 #endif
194 #if 0
195     printf("test true2\n");
196 #endif
197 #if 1-1
198     printf("test true3\n");
199 #endif
200 #if defined(A)
201     printf("test trueA\n");
202 #endif
203 #if defined(B)
204     printf("test trueB\n");
205 #endif
206
207 #if 0
208     printf("test 0\n");
209 #elif 0
210     printf("test 1\n");
211 #elif 2
212     printf("test 2\n");
213 #else
214     printf("test 3\n");
215 #endif
216
217     MACRO_NOARGS();
218
219 #ifdef __LINE__
220     printf("__LINE__ defined\n");
221 #endif
222
223     printf("__LINE__=%d __FILE__=%s\n",
224            __LINE__, __FILE__);
225 #line 200
226     printf("__LINE__=%d __FILE__=%s\n",
227            __LINE__, __FILE__);
228 #line 203 "test"
229     printf("__LINE__=%d __FILE__=%s\n",
230            __LINE__, __FILE__);
231 #line 220 "tcctest.c"
232
233     /* not strictly preprocessor, but we test it there */
234 #ifdef C99_MACROS
235     printf("__func__ = %s\n", __func__);
236     dprintf(1, "vaarg=%d\n", 1);
237 #endif
238     dprintf1(1, "vaarg1\n");
239     dprintf1(1, "vaarg1=%d\n", 2);
240     dprintf1(1, "vaarg1=%d %d\n", 1, 2);
241
242     /* gcc extension */
243     printf("func='%s'\n", __FUNCTION__);
244
245     /* complicated macros in glibc */
246     printf("INT64_MIN=%Ld\n", INT64_MIN);
247     {
248         int a;
249         a = 1;
250         glue(a+, +);
251         printf("a=%d\n", a);
252         glue(a <, <= 2);
253         printf("a=%d\n", a);
254     }
255
256     /* macro function with argument outside the macro string */
257 #define MF_s MF_hello
258 #define MF_hello(msg) printf("%s\n",msg)
259
260 #define MF_t printf("tralala\n"); MF_hello
261
262     MF_s("hi");
263     MF_t("hi");
264
265     /* test macro substituion inside args (should not eat stream) */
266     printf("qq=%d\n", qq(qq)(2));
267
268     /* test zero argument case. NOTE: gcc 2.95.x does not accept a
269        null argument without a space. gcc 3.2 fixes that. */
270
271 #define qq1(x) 1
272     printf("qq1=%d\n", qq1( ));
273
274     /* comment with stray handling *\
275 /
276        /* this is a valid *\/ comment */
277        /* this is a valid comment *\*/
278     //  this is a valid\
279 comment
280
281     /* test function macro substitution when the function name is
282        substituted */
283     TEST2();
284
285 #define PART_ONE(n) (!!((n) & 0xF))
286 #define PART_TWO(n) (PART_ONE(n))
287 #define PART_THREE(f) PART_TWO(0x##f##U)
288     printf("Nested macros: %d == 0\n", PART_THREE(0));
289
290 }
291
292 int some_fn(int x)
293 {
294     return x;
295 }
296
297 int other_fn(int x)
298 {
299     return x*2;
300 }
301
302 int op(a,b)
303 {
304     return a / b;
305 }
306
307 int ret(a)
308 {
309     if (a == 2)
310         return 1;
311     if (a == 3)
312         return 2;
313     return 0;
314 }
315
316 void ps(const char *s)
317 {
318     int c;
319     while (1) {
320         c = *s;
321         if (c == 0)
322             break;
323         printf("%c", c);
324         s++;
325     }
326 }
327
328 const char foo1_string[] = "\
329 bar\n\
330 test\14\
331 1";
332
333 void string_test()
334 {
335     int b;
336     printf("string:\n");
337     printf("\141\1423\143\n");/* dezdez test */
338     printf("\x41\x42\x43\x3a\n");
339     printf("c=%c\n", 'r');
340     printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
341     printf("foo1_string='%s'\n", foo1_string);
342 #if 0
343     printf("wstring=%S\n", L"abc");
344     printf("wstring=%S\n", L"abc" L"def" "ghi");
345     printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
346     printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
347 #endif
348     ps("test\n");
349     b = 32;
350     while ((b = b + 1) < 96) {
351         printf("%c", b);
352     }
353     printf("\n");
354     printf("fib=%d\n", fib(33));
355     b = 262144;
356     while (b != 0x80000000) {
357         num(b);
358         b = b * 2;
359     }
360 }
361
362 void loop_test()
363 {
364     int i;
365     i = 0;
366     while (i < 10)
367         printf("%d", i++);
368     printf("\n");
369     for(i = 0; i < 10;i++)
370         printf("%d", i);
371     printf("\n");
372     i = 0;
373     do {
374         printf("%d", i++);
375     } while (i < 10);
376     printf("\n");
377
378     /* break/continue tests */
379     i = 0;
380     while (1) {
381         if (i == 6)
382             break;
383         i++;
384         if (i == 3)
385             continue;
386         printf("%d", i);
387     }
388     printf("\n");
389
390     /* break/continue tests */
391     i = 0;
392     do {
393         if (i == 6)
394             break;
395         i++;
396         if (i == 3)
397             continue;
398         printf("%d", i);
399     } while(1);
400     printf("\n");
401
402     for(i = 0;i < 10;i++) {
403         if (i == 3)
404             continue;
405         printf("%d", i);
406     }
407     printf("\n");
408 }
409
410
411 void goto_test()
412 {
413     int i;
414     static void *label_table[3] = { &&label1, &&label2, &&label3 };
415
416     printf("goto:\n");
417     i = 0;
418  s_loop:
419     if (i >= 10)
420         goto s_end;
421     printf("%d", i);
422     i++;
423     goto s_loop;
424  s_end:
425     printf("\n");
426
427     /* we also test computed gotos (GCC extension) */
428     for(i=0;i<3;i++) {
429         goto *label_table[i];
430     label1:
431         printf("label1\n");
432         goto next;
433     label2:
434         printf("label2\n");
435         goto next;
436     label3:
437         printf("label3\n");
438     next: ;
439     }
440 }
441
442 enum {
443     E0,
444     E1 = 2,
445     E2 = 4,
446     E3,
447     E4,
448 };
449
450 enum test {
451     E5 = 1000,
452 };
453
454 void enum_test()
455 {
456     enum test b1;
457     printf("enum:\n%d %d %d %d %d %d\n",
458            E0, E1, E2, E3, E4, E5);
459     b1 = 1;
460     printf("b1=%d\n", b1);
461 }
462
463 typedef int *my_ptr;
464
465 void typedef_test()
466 {
467     my_ptr a;
468     int b;
469     a = &b;
470     *a = 1234;
471     printf("typedef:\n");
472     printf("a=%d\n", *a);
473 }
474
475 void forward_test()
476 {
477     printf("forward:\n");
478     forward_ref();
479     forward_ref();
480 }
481
482
483 void forward_ref(void)
484 {
485     printf("forward ok\n");
486 }
487
488 typedef struct struct1 {
489     int f1;
490     int f2, f3;
491     union union1 {
492         int v1;
493         int v2;
494     } u;
495     char str[3];
496 } struct1;
497
498 struct struct2 {
499     int a;
500     char b;
501 };
502
503 union union2 {
504     int w1;
505     int w2;
506 };
507
508 struct struct1 st1, st2;
509
510 int main(int argc, char **argv)
511 {
512     string_test();
513     expr_test();
514     macro_test();
515     scope_test();
516     forward_test();
517     funcptr_test();
518     loop_test();
519     switch_test();
520     goto_test();
521     enum_test();
522     typedef_test();
523     struct_test();
524     array_test();
525     expr_ptr_test();
526     bool_test();
527     expr2_test();
528     constant_expr_test();
529     expr_cmp_test();
530     char_short_test();
531     init_test();
532     compound_literal_test();
533     kr_test();
534     struct_assign_test();
535     cast_test();
536     bitfield_test();
537     c99_bool_test();
538     float_test();
539     longlong_test();
540     stdarg_test();
541     whitespace_test();
542     relocation_test();
543     old_style_function();
544     sizeof_test();
545     typeof_test();
546     statement_expr_test();
547     local_label_test();
548     asm_test();
549     builtin_test();
550     static_test();
551     return 0;
552 }
553
554 int tab[3];
555 int tab2[3][2];
556
557 int g;
558
559 void f1(g)
560 {
561     printf("g1=%d\n", g);
562 }
563
564 void scope_test()
565 {
566     printf("scope:\n");
567     g = 2;
568     f1(1);
569     printf("g2=%d\n", g);
570     {
571         int g;
572         g = 3;
573         printf("g3=%d\n", g);
574         {
575             int g;
576             g = 4;
577             printf("g4=%d\n", g);
578         }
579     }
580     printf("g5=%d\n", g);
581 }
582
583 void array_test(int a[4])
584 {
585     int i, j;
586
587     printf("array:\n");
588     printf("sizeof(a) = %d\n", sizeof(a));
589     printf("sizeof(\"a\") = %d\n", sizeof("a"));
590 #ifdef C99_MACROS
591     printf("sizeof(__func__) = %d\n", sizeof(__func__));
592 #endif
593     printf("sizeof tab %d\n", sizeof(tab));
594     printf("sizeof tab2 %d\n", sizeof tab2);
595     tab[0] = 1;
596     tab[1] = 2;
597     tab[2] = 3;
598     printf("%d %d %d\n", tab[0], tab[1], tab[2]);
599     for(i=0;i<3;i++)
600         for(j=0;j<2;j++)
601             tab2[i][j] = 10 * i + j;
602     for(i=0;i<3*2;i++) {
603         printf(" %3d", ((int *)tab2)[i]);
604     }
605     printf("\n");
606 }
607
608 void expr_test()
609 {
610     int a, b;
611     a = 0;
612     printf("%d\n", a += 1);
613     printf("%d\n", a -= 2);
614     printf("%d\n", a *= 31232132);
615     printf("%d\n", a /= 4);
616     printf("%d\n", a %= 20);
617     printf("%d\n", a &= 6);
618     printf("%d\n", a ^= 7);
619     printf("%d\n", a |= 8);
620     printf("%d\n", a >>= 3);
621     printf("%d\n", a <<= 4);
622
623     a = 22321;
624     b = -22321;
625     printf("%d\n", a + 1);
626     printf("%d\n", a - 2);
627     printf("%d\n", a * 312);
628     printf("%d\n", a / 4);
629     printf("%d\n", b / 4);
630     printf("%d\n", (unsigned)b / 4);
631     printf("%d\n", a % 20);
632     printf("%d\n", b % 20);
633     printf("%d\n", (unsigned)b % 20);
634     printf("%d\n", a & 6);
635     printf("%d\n", a ^ 7);
636     printf("%d\n", a | 8);
637     printf("%d\n", a >> 3);
638     printf("%d\n", b >> 3);
639     printf("%d\n", (unsigned)b >> 3);
640     printf("%d\n", a << 4);
641     printf("%d\n", ~a);
642     printf("%d\n", -a);
643     printf("%d\n", +a);
644
645     printf("%d\n", 12 + 1);
646     printf("%d\n", 12 - 2);
647     printf("%d\n", 12 * 312);
648     printf("%d\n", 12 / 4);
649     printf("%d\n", 12 % 20);
650     printf("%d\n", 12 & 6);
651     printf("%d\n", 12 ^ 7);
652     printf("%d\n", 12 | 8);
653     printf("%d\n", 12 >> 2);
654     printf("%d\n", 12 << 4);
655     printf("%d\n", ~12);
656     printf("%d\n", -12);
657     printf("%d\n", +12);
658     printf("%d %d %d %d\n",
659            isid('a'),
660            isid('g'),
661            isid('T'),
662            isid('('));
663
664     {
665         /* Check that tcc saves registers before a conditional jump */
666         /* Addresses bug "grischka case_2" */
667         struct test_str { int a, b, c; };
668         struct test_str t1 = {0,0,0};
669         struct test_str t2 = {1,1,1};
670         struct test_str *p1 = &t1;
671         struct test_str *p2 = &t2;
672         int f = 0;
673         int g = 0;
674
675         p1->b = f==0 || isid(0);
676         printf("case_2.1: Expect 0 1 0 -> %d %d %d\n", p1->a, p1->b, p1->c);
677         p1->c = !f || isid(0);
678         printf("case_2.2: Expect 0 1 1 -> %d %d %d\n", p1->a, p1->b, p1->c);
679
680         /* This will crash old versions of tcc during compilation: */
681         p2->b = (f==1) && isid(0);
682         printf("case_2.1AND: Expect 0 1 0 -> %d %d %d\n", p2->a, p2->b, p2->c);
683         p2->b = (!(f==1)) && isid(0);
684         printf("case_2.2AND: Expect 0 1 0 -> %d %d %d\n", p2->a, p2->b, p2->c);
685     }
686 }
687
688 int isid(int c)
689 {
690     return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
691 }
692
693 /**********************/
694
695 int vstack[10], *vstack_ptr;
696
697 void vpush(int vt, int vc)
698 {
699     *vstack_ptr++ = vt;
700     *vstack_ptr++ = vc;
701 }
702
703 void vpop(int *ft, int *fc)
704 {
705     *fc = *--vstack_ptr;
706     *ft = *--vstack_ptr;
707 }
708
709 void expr2_test()
710 {
711     int a, b;
712
713     printf("expr2:\n");
714     vstack_ptr = vstack;
715     vpush(1432432, 2);
716     vstack_ptr[-2] &= ~0xffffff80;
717     vpop(&a, &b);
718     printf("res= %d %d\n", a, b);
719 }
720
721 void constant_expr_test()
722 {
723     int a;
724     printf("constant_expr:\n");
725     a = 3;
726     printf("%d\n", a * 16);
727     printf("%d\n", a * 1);
728     printf("%d\n", a + 0);
729 }
730
731 int tab4[10];
732
733 void expr_ptr_test()
734 {
735     int *p, *q;
736
737     printf("expr_ptr:\n");
738     p = tab4;
739     q = tab4 + 10;
740     printf("diff=%d\n", q - p);
741     p++;
742     printf("inc=%d\n", p - tab4);
743     p--;
744     printf("dec=%d\n", p - tab4);
745     ++p;
746     printf("inc=%d\n", p - tab4);
747     --p;
748     printf("dec=%d\n", p - tab4);
749     printf("add=%d\n", p + 3 - tab4);
750     printf("add=%d\n", 3 + p - tab4);
751 }
752
753 void expr_cmp_test()
754 {
755     int a, b;
756     printf("constant_expr:\n");
757     a = -1;
758     b = 1;
759     printf("%d\n", a == a);
760     printf("%d\n", a != a);
761
762     printf("%d\n", a < b);
763     printf("%d\n", a <= b);
764     printf("%d\n", a <= a);
765     printf("%d\n", b >= a);
766     printf("%d\n", a >= a);
767     printf("%d\n", b > a);
768
769     printf("%d\n", (unsigned)a < b);
770     printf("%d\n", (unsigned)a <= b);
771     printf("%d\n", (unsigned)a <= a);
772     printf("%d\n", (unsigned)b >= a);
773     printf("%d\n", (unsigned)a >= a);
774     printf("%d\n", (unsigned)b > a);
775 }
776
777 struct empty {
778 };
779
780 struct aligntest1 {
781     char a[10];
782 };
783
784 struct aligntest2 {
785     int a;
786     char b[10];
787 };
788
789 struct aligntest3 {
790     double a, b;
791 };
792
793 struct aligntest4 {
794     double a[0];
795 };
796
797 void struct_test()
798 {
799     struct1 *s;
800     union union2 u;
801
802     printf("struct:\n");
803     printf("sizes: %d %d %d %d\n",
804            sizeof(struct struct1),
805            sizeof(struct struct2),
806            sizeof(union union1),
807            sizeof(union union2));
808     st1.f1 = 1;
809     st1.f2 = 2;
810     st1.f3 = 3;
811     printf("st1: %d %d %d\n",
812            st1.f1, st1.f2, st1.f3);
813     st1.u.v1 = 1;
814     st1.u.v2 = 2;
815     printf("union1: %d\n", st1.u.v1);
816     u.w1 = 1;
817     u.w2 = 2;
818     printf("union2: %d\n", u.w1);
819     s = &st2;
820     s->f1 = 3;
821     s->f2 = 2;
822     s->f3 = 1;
823     printf("st2: %d %d %d\n",
824            s->f1, s->f2, s->f3);
825     printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
826
827     /* align / size tests */
828     printf("aligntest1 sizeof=%d alignof=%d\n",
829            sizeof(struct aligntest1), __alignof__(struct aligntest1));
830     printf("aligntest2 sizeof=%d alignof=%d\n",
831            sizeof(struct aligntest2), __alignof__(struct aligntest2));
832     printf("aligntest3 sizeof=%d alignof=%d\n",
833            sizeof(struct aligntest3), __alignof__(struct aligntest3));
834     printf("aligntest4 sizeof=%d alignof=%d\n",
835            sizeof(struct aligntest4), __alignof__(struct aligntest4));
836
837     /* empty structures (GCC extension) */
838     printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
839     printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
840 }
841
842 /* XXX: depend on endianness */
843 void char_short_test()
844 {
845     int var1, var2;
846
847     printf("char_short:\n");
848
849     var1 = 0x01020304;
850     var2 = 0xfffefdfc;
851     printf("s8=%d %d\n",
852            *(char *)&var1, *(char *)&var2);
853     printf("u8=%d %d\n",
854            *(unsigned char *)&var1, *(unsigned char *)&var2);
855     printf("s16=%d %d\n",
856            *(short *)&var1, *(short *)&var2);
857     printf("u16=%d %d\n",
858            *(unsigned short *)&var1, *(unsigned short *)&var2);
859     printf("s32=%d %d\n",
860            *(int *)&var1, *(int *)&var2);
861     printf("u32=%d %d\n",
862            *(unsigned int *)&var1, *(unsigned int *)&var2);
863     *(char *)&var1 = 0x08;
864     printf("var1=%x\n", var1);
865     *(short *)&var1 = 0x0809;
866     printf("var1=%x\n", var1);
867     *(int *)&var1 = 0x08090a0b;
868     printf("var1=%x\n", var1);
869 }
870
871 /******************/
872
873 typedef struct Sym {
874     int v;
875     int t;
876     int c;
877     struct Sym *next;
878     struct Sym *prev;
879 } Sym;
880
881 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
882 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
883
884 static int toupper1(int a)
885 {
886     return TOUPPER(a);
887 }
888
889 void bool_test()
890 {
891     int *s, a, b, t, f, i;
892
893     a = 0;
894     s = (void*)0;
895     printf("!s=%d\n", !s);
896
897     if (!s || !s[0])
898         a = 1;
899     printf("a=%d\n", a);
900
901     printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
902     printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
903     printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
904 #if 1 && 1
905     printf("a1\n");
906 #endif
907 #if 1 || 0
908     printf("a2\n");
909 #endif
910 #if 1 ? 0 : 1
911     printf("a3\n");
912 #endif
913 #if 0 ? 0 : 1
914     printf("a4\n");
915 #endif
916
917     a = 4;
918     printf("b=%d\n", a + (0 ? 1 : a / 2));
919
920     /* test register spilling */
921     a = 10;
922     b = 10;
923     a = (a + b) * ((a < b) ?
924                    ((b - a) * (a - b)): a + b);
925     printf("a=%d\n", a);
926
927     /* test complex || or && expressions */
928     t = 1;
929     f = 0;
930     a = 32;
931     printf("exp=%d\n", f == (32 <= a && a <= 3));
932     printf("r=%d\n", (t || f) + (t && f));
933
934     /* test ? : cast */
935     {
936         int aspect_on;
937         int aspect_native = 65536;
938         double bfu_aspect = 1.0;
939         int aspect;
940         for(aspect_on = 0; aspect_on < 2; aspect_on++) {
941             aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
942             printf("aspect=%d\n", aspect);
943         }
944     }
945
946     /* Test passing structs & function pointers though conditional
947      * operator ? :.  This is bug grischka-20050929 case_1 */
948     {
949         struct test1 { int a, b, c; };
950         struct test1 t0 = {10,20,30};
951         struct test1 t1 = {11,21,31};
952         struct test1 tx = {0,0,0};
953         int (*pfn)(int);
954         int f = 0;
955
956         tx = f==0 ? t0 : t1;
957         printf("case_1.1: 10,20,30 -> %d,%d,%d\n", tx.a, tx.b, tx.c);
958
959         /* This tests to see that function pointers correctly pass through
960            the conditional operator ?:.  This tests for
961            grischka-20050929 case_1.2.  Note that this was already FIXED
962            in rl-1.0.0, but we want to TEST for it too. */
963         pfn = f ? some_fn : other_fn;
964         printf("case_1.2: other -> %s\n", 1==pfn(1)?"some":"other");
965     }
966
967
968     /* test ? : GCC extension */
969     {
970         static int v1 = 34 ? : -1; /* constant case */
971         static int v2 = 0 ? : -1; /* constant case */
972         int a = 30;
973
974         printf("%d %d\n", v1, v2);
975         printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
976     }
977
978     /* again complex expression */
979     for(i=0;i<256;i++) {
980         if (toupper1 (i) != TOUPPER (i))
981             printf("error %d\n", i);
982     }
983
984 }
985
986 /* GCC accepts that */
987 static int tab_reinit[];
988 static int tab_reinit[10];
989
990 //int cinit1; /* a global variable can be defined several times without error ! */
991 int cinit1;
992 int cinit1;
993 int cinit1 = 0;
994 int *cinit2 = (int []){3, 2, 1};
995
996 void compound_literal_test(void)
997 {
998     int *p, i;
999     char *q, *q3;
1000
1001     printf("compound_test:\n");
1002
1003     p = (int []){1, 2, 3};
1004     for(i=0;i<3;i++)
1005         printf(" %d", p[i]);
1006     printf("\n");
1007
1008     for(i=0;i<3;i++)
1009         printf("%d", cinit2[i]);
1010     printf("\n");
1011
1012     q = "tralala1";
1013     printf("q1=%s\n", q);
1014
1015     q = (char *){ "tralala2" };
1016     printf("q2=%s\n", q);
1017
1018     q3 = (char *){ q };
1019     printf("q3=%s\n", q3);
1020
1021     q = (char []){ "tralala3" };
1022     printf("q4=%s\n", q);
1023
1024 #ifdef ALL_ISOC99
1025     p = (int []){1, 2, cinit1 + 3};
1026     for(i=0;i<3;i++)
1027         printf(" %d", p[i]);
1028     printf("\n");
1029
1030     for(i=0;i<3;i++) {
1031         p = (int []){1, 2, 4 + i};
1032         printf("%d %d %d\n",
1033                p[0],
1034                p[1],
1035                p[2]);
1036     }
1037 #endif
1038 }
1039
1040 /* K & R protos */
1041
1042 kr_func1(a, b)
1043 {
1044     return a + b;
1045 }
1046
1047 int kr_func2(a, b)
1048 {
1049     return a + b;
1050 }
1051
1052 kr_test()
1053 {
1054     printf("kr_test:\n");
1055     printf("func1=%d\n", kr_func1(3, 4));
1056     printf("func2=%d\n", kr_func2(3, 4));
1057     return 0;
1058 }
1059
1060 void num(int n)
1061 {
1062     char *tab, *p;
1063     tab = (char*)malloc(20);
1064     p = tab;
1065     while (1) {
1066         *p = 48 + (n % 10);
1067         p++;
1068         n = n / 10;
1069         if (n == 0)
1070             break;
1071     }
1072     while (p != tab) {
1073         p--;
1074         printf("%c", *p);
1075     }
1076     printf("\n");
1077 }
1078
1079 /* structure assignment tests */
1080 struct structa1 {
1081     int f1;
1082     char f2;
1083 };
1084
1085 struct structa1 ssta1;
1086
1087 void struct_assign_test1(struct structa1 s1, int t)
1088 {
1089     printf("%d %d %d\n", s1.f1, s1.f2, t);
1090 }
1091
1092 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1093 {
1094     s1.f1 += t;
1095     s1.f2 -= t;
1096     return s1;
1097 }
1098
1099 void struct_assign_test3(void)
1100 {
1101     // incompatible assigment
1102     struct _s1 { int a, b, c; } *p1 = NULL;
1103     struct _s2 { int a, b, c; } *p2 = p1; //warning
1104     printf("struct assign, 0 -> %x\n", p2);
1105 }
1106
1107 void struct_assign_test(void)
1108 {
1109     struct structa1 lsta1, lsta2;
1110
1111 #if 0
1112     printf("struct_assign_test:\n");
1113
1114     lsta1.f1 = 1;
1115     lsta1.f2 = 2;
1116     printf("%d %d\n", lsta1.f1, lsta1.f2);
1117     lsta2 = lsta1;
1118     printf("%d %d\n", lsta2.f1, lsta2.f2);
1119 #else
1120     lsta2.f1 = 1;
1121     lsta2.f2 = 2;
1122 #endif
1123     struct_assign_test1(lsta2, 3);
1124
1125     printf("before call: %d %d\n", lsta2.f1, lsta2.f2);
1126     lsta2 = struct_assign_test2(lsta2, 4);
1127     printf("after call: %d %d\n", lsta2.f1, lsta2.f2);
1128     struct_assign_test3();
1129 }
1130
1131 /* casts to short/char */
1132
1133 void cast1(char a, short b, unsigned char c, unsigned short d)
1134 {
1135     printf("%d %d %d %d\n", a, b, c, d);
1136 }
1137
1138 char bcast;
1139 short scast;
1140
1141 void cast_test()
1142 {
1143     int a;
1144     char c;
1145     char tab[10];
1146     unsigned b,d;
1147     short s;
1148     double g = 0.1;
1149
1150     printf("cast_test:\n");
1151     a = 0xfffff;
1152     cast1(a, a, a, a);
1153     a = 0xffffe;
1154     printf("%d %d %d %d\n",
1155            (char)(a + 1),
1156            (short)(a + 1),
1157            (unsigned char)(a + 1),
1158            (unsigned short)(a + 1));
1159     printf("%d %d %d %d\n",
1160            (char)0xfffff,
1161            (short)0xfffff,
1162            (unsigned char)0xfffff,
1163            (unsigned short)0xfffff);
1164
1165     a = (bcast = 128) + 1;
1166     printf("%d\n", a);
1167     a = (scast = 65536) + 1;
1168     printf("%d\n", a);
1169
1170     printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1171
1172     /* test cast from unsigned to signed short to int */
1173     b = 0xf000;
1174     d = (short)b;
1175     printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1176     b = 0xf0f0;
1177     d = (char)b;
1178     printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1179
1180     /* Try casting pointer to short or _Bool (grishka case_6.1). This
1181      * is lossy, so tcc will print a warning.  This capability is needed
1182      * to compile gcc 2.95 as well as other programs.  */
1183     {
1184         void *p = (void *) 3;
1185         printf("Expect 3 1 -> %hd %hhd\n",
1186             (short) p, (_Bool) p); /* Expect warning */
1187     }
1188
1189
1190     /* test implicit int casting for array accesses */
1191     c = 0;
1192     tab[1] = 2;
1193     tab[c] = 1;
1194     printf("%d %d\n", tab[0], tab[1]);
1195
1196     /* test implicit casting on some operators */
1197     printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1198     printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1199     printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1200
1201     /* Cast float to bool */
1202     printf("%d\n", (_Bool) 0.1);
1203     /* Cast non-constant double 0.1 to bool, should be 1, grischka case 6.2 */
1204     printf("1 -> %d\n", (_Bool) g);
1205
1206     /* Cast with sign extension */
1207     a = (short)0xf000;
1208     printf("0x%x == 0xfffff000\n", a);
1209     a = (signed char)0xf0f0;
1210     printf("0x%x == 0xfffffff0\n", a);
1211 }
1212
1213 /* initializers tests */
1214 struct structinit1 {
1215     int f1;
1216     char f2;
1217     short f3;
1218     int farray[3];
1219 };
1220
1221 int sinit1 = 2;
1222 int sinit2 = { 3 };
1223 int sinit3[3] = { 1, 2, {{3}}, };
1224 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1225 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1226 int sinit6[] = { 1, 2, 3 };
1227 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1228 char sinit8[] = "hello" "trala";
1229
1230 struct structinit1 sinit9 = { 1, 2, 3 };
1231 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1232 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1233 #ifdef ALL_ISOC99
1234                                .farray[0] = 10,
1235                                .farray[1] = 11,
1236                                .farray[2] = 12,
1237 #endif
1238 };
1239
1240 char *sinit12 = "hello world";
1241 char *sinit13[] = {
1242     "test1",
1243     "test2",
1244     "test3",
1245 };
1246 char sinit14[10] = { "abc" };
1247 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1248
1249 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1250
1251 struct bar {
1252         char *s;
1253         int len;
1254 } sinit17[] = {
1255         "a1", 4,
1256         "a2", 1
1257 };
1258
1259 int sinit18[10] = {
1260     [2 ... 5] = 20,
1261     2,
1262     [8] = 10,
1263 };
1264
1265 void init_test(void)
1266 {
1267     int linit1 = 2;
1268     int linit2 = { 3 };
1269     int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1270     int linit6[] = { 1, 2, 3 };
1271     int i, j;
1272     char linit8[] = "hello" "trala";
1273     int linit12[10] = { 1, 2 };
1274     int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1275     char linit14[10] = "abc";
1276     int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1277     struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1278     int linit17 = sizeof(linit17);
1279
1280     printf("init_test:\n");
1281
1282     printf("sinit1=%d\n", sinit1);
1283     printf("sinit2=%d\n", sinit2);
1284     printf("sinit3=%d %d %d %d\n",
1285            sizeof(sinit3),
1286            sinit3[0],
1287            sinit3[1],
1288            sinit3[2]
1289            );
1290     printf("sinit6=%d\n", sizeof(sinit6));
1291     printf("sinit7=%d %d %d %d\n",
1292            sizeof(sinit7),
1293            sinit7[0],
1294            sinit7[1],
1295            sinit7[2]
1296            );
1297     printf("sinit8=%s\n", sinit8);
1298     printf("sinit9=%d %d %d\n",
1299            sinit9.f1,
1300            sinit9.f2,
1301            sinit9.f3
1302            );
1303     printf("sinit10=%d %d %d\n",
1304            sinit10.f1,
1305            sinit10.f2,
1306            sinit10.f3
1307            );
1308     printf("sinit11=%d %d %d %d %d %d\n",
1309            sinit11.f1,
1310            sinit11.f2,
1311            sinit11.f3,
1312            sinit11.farray[0],
1313            sinit11.farray[1],
1314            sinit11.farray[2]
1315            );
1316
1317     for(i=0;i<3;i++)
1318         for(j=0;j<2;j++)
1319             printf("[%d][%d] = %d %d %d\n",
1320                    i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1321     printf("linit1=%d\n", linit1);
1322     printf("linit2=%d\n", linit2);
1323     printf("linit6=%d\n", sizeof(linit6));
1324     printf("linit8=%d %s\n", sizeof(linit8), linit8);
1325
1326     printf("sinit12=%s\n", sinit12);
1327     printf("sinit13=%d %s %s %s\n",
1328            sizeof(sinit13),
1329            sinit13[0],
1330            sinit13[1],
1331            sinit13[2]);
1332     printf("sinit14=%s\n", sinit14);
1333
1334     for(i=0;i<10;i++) printf(" %d", linit12[i]);
1335     printf("\n");
1336     for(i=0;i<10;i++) printf(" %d", linit13[i]);
1337     printf("\n");
1338     for(i=0;i<10;i++) printf(" %d", linit14[i]);
1339     printf("\n");
1340     for(i=0;i<10;i++) printf(" %d", linit15[i]);
1341     printf("\n");
1342     printf("%d %d %d %d\n",
1343            linit16.a1,
1344            linit16.a2,
1345            linit16.a3,
1346            linit16.a4);
1347     /* test that initialisation is done after variable declare */
1348     printf("linit17=%d\n", linit17);
1349     printf("sinit15=%d\n", sinit15[0]);
1350     printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1351     printf("sinit17=%s %d %s %d\n",
1352            sinit17[0].s, sinit17[0].len,
1353            sinit17[1].s, sinit17[1].len);
1354     for(i=0;i<10;i++)
1355         printf("%x ", sinit18[i]);
1356     printf("\n");
1357 }
1358
1359
1360 void switch_test()
1361 {
1362     int i;
1363
1364     for(i=0;i<15;i++) {
1365         switch(i) {
1366         case 0:
1367         case 1:
1368             printf("a");
1369             break;
1370         default:
1371             printf("%d", i);
1372             break;
1373         case 8 ... 12:
1374             printf("c");
1375             break;
1376         case 3:
1377             printf("b");
1378             break;
1379         }
1380     }
1381     printf("\n");
1382 }
1383
1384 /* ISOC99 _Bool type */
1385 void c99_bool_test(void)
1386 {
1387 #ifdef BOOL_ISOC99
1388     int a;
1389     _Bool b;
1390
1391     printf("bool_test:\n");
1392     printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1393     a = 3;
1394     printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1395     b = 3;
1396     printf("b = %d\n", b);
1397     b++;
1398     printf("b = %d\n", b);
1399 #endif
1400 }
1401
1402 void bitfield_test(void)
1403 {
1404     int a;
1405     struct sbf1 {
1406         int f1 : 3;
1407         int : 2;
1408         int f2 : 1;
1409         int : 0;
1410         int f3 : 5;
1411         int f4 : 7;
1412         unsigned int f5 : 7;
1413     } st1;
1414     printf("bitfield_test:");
1415     printf("sizeof(st1) = %d\n", sizeof(st1));
1416
1417     st1.f1 = 3;
1418     st1.f2 = 1;
1419     st1.f3 = 15;
1420     a = 120;
1421     st1.f4 = a;
1422     st1.f5 = a;
1423     st1.f5++;
1424     printf("%d %d %d %d %d\n",
1425            st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1426
1427     st1.f1 = 7;
1428     if (st1.f1 == -1)
1429         printf("st1.f1 == -1\n");
1430     else
1431         printf("st1.f1 != -1\n");
1432     if (st1.f2 == -1)
1433         printf("st1.f2 == -1\n");
1434     else
1435         printf("st1.f2 != -1\n");
1436
1437     /* Do bitfield assignments return correct rvalue? This is bug
1438      * grischka-2005-09-29 case_3 */
1439     {
1440         struct test1 { unsigned a:1, b:1, c:1, d:1; };
1441         struct test1 t1 = {0, 1, 0, 1};
1442         struct test1 *p = &t1;
1443
1444         printf("case_3.1: 0101 -> %d%d%d%d\n", p->a, p->b, p->c, p->d);
1445         p->b = p->d = 0;
1446         p->a = p->c = 1;
1447         printf("case_3.2: 1010 -> %d%d%d%d\n", p->a, p->b, p->c, p->d);
1448     }
1449
1450 }
1451
1452 #define FTEST(prefix, type, fmt)\
1453 void prefix ## cmp(type a, type b)\
1454 {\
1455     printf("%d %d %d %d %d %d\n",\
1456            a == b,\
1457            a != b,\
1458            a < b,\
1459            a > b,\
1460            a >= b,\
1461            a <= b);\
1462     printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1463            a,\
1464            b,\
1465            a + b,\
1466            a - b,\
1467            a * b,\
1468            a / b,\
1469            -a);\
1470     printf(fmt "\n", ++a);\
1471     printf(fmt "\n", a++);\
1472     printf(fmt "\n", a);\
1473 }\
1474 void prefix ## fcast(type a)\
1475 {\
1476     float fa;\
1477     double da;\
1478     long double la;\
1479     int ia;\
1480     unsigned int ua;\
1481     type b;\
1482     fa = a;\
1483     da = a;\
1484     la = a;\
1485     printf("ftof: %f %f %Lf\n", fa, da, la);\
1486     ia = (int)a;\
1487     ua = (unsigned int)a;\
1488     printf("ftoi: %d %u\n", ia, ua);\
1489     ia = -1234;\
1490     ua = 0x81234500;\
1491     b = ia;\
1492     printf("itof: " fmt "\n", b);\
1493     b = ua;\
1494     printf("utof: " fmt "\n", b);\
1495 }\
1496 \
1497 void prefix ## test(void)\
1498 {\
1499     printf("testing '%s'\n", #type);\
1500     prefix ## cmp(1, 2.5);\
1501     prefix ## cmp(2, 1.5);\
1502     prefix ## cmp(1, 1);\
1503     prefix ## fcast(234.6);\
1504     prefix ## fcast(-2334.6);\
1505 }
1506
1507 FTEST(f, float, "%f")
1508 FTEST(d, double, "%f")
1509 FTEST(ld, long double, "%Lf")
1510
1511 double ftab1[3] = { 1.2, 3.4, -5.6 };
1512
1513
1514 void float_test(void)
1515 {
1516     float fa, fb;
1517     double da, db;
1518     int a;
1519     unsigned int b;
1520
1521     printf("float_test:\n");
1522     printf("sizeof(float) = %d\n", sizeof(float));
1523     printf("sizeof(double) = %d\n", sizeof(double));
1524     printf("sizeof(long double) = %d\n", sizeof(long double));
1525     ftest();
1526     dtest();
1527     ldtest();
1528     printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1529     printf("%f %f %f\n", 2.12, .5, 2.3e10);
1530     //    printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1531     da = 123;
1532     printf("da=%f\n", da);
1533     fa = 123;
1534     printf("fa=%f\n", fa);
1535     a = 4000000000;
1536     da = a;
1537     printf("da = %f\n", da);
1538     b = 4000000000;
1539     db = b;
1540     printf("db = %f\n", db);
1541 }
1542
1543 int fib(int n)
1544 {
1545     if (n <= 2)
1546         return 1;
1547     else
1548         return fib(n-1) + fib(n-2);
1549 }
1550
1551 int reply_self(int x)
1552 {
1553     return x;
1554 }
1555
1556 void funcptr_test()
1557 {
1558     void (*func)(int);
1559     int a;
1560     struct {
1561         int dummy;
1562         void (*func)(int);
1563     } st1;
1564
1565     printf("funcptr:\n");
1566     func = &num;
1567     (*func)(12345);
1568     func = num;
1569     a = 1;
1570     a = 1;
1571     func(12345);
1572     /* more complicated pointer computation */
1573     st1.func = num;
1574     st1.func(12346);
1575     printf("sizeof1 = %d\n", sizeof(funcptr_test));
1576     printf("sizeof2 = %d\n", sizeof funcptr_test);
1577     printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1578     printf("sizeof4 = %d\n", sizeof &funcptr_test);
1579
1580     /* Test function pointer indirection */
1581     {
1582         int (*pfn)(int) = reply_self;
1583         printf("case_9: 1 (7,8) -> %d (%d,%d)\n",
1584             (int) (reply_self == *pfn), pfn(7), (******pfn)(8));
1585     }
1586
1587 }
1588
1589 void lloptest(long long a, long long b)
1590 {
1591     unsigned long long ua, ub;
1592
1593     ua = a;
1594     ub = b;
1595     /* arith */
1596     printf("arith: %Ld %Ld %Ld\n",
1597            a + b,
1598            a - b,
1599            a * b);
1600
1601     if (b != 0) {
1602         printf("arith1: %Ld %Ld\n",
1603            a / b,
1604            a % b);
1605     }
1606
1607     /* binary */
1608     printf("bin: %Ld %Ld %Ld\n",
1609            a & b,
1610            a | b,
1611            a ^ b);
1612
1613     /* tests */
1614     printf("test: %d %d %d %d %d %d\n",
1615            a == b,
1616            a != b,
1617            a < b,
1618            a > b,
1619            a >= b,
1620            a <= b);
1621
1622     printf("utest: %d %d %d %d %d %d\n",
1623            ua == ub,
1624            ua != ub,
1625            ua < ub,
1626            ua > ub,
1627            ua >= ub,
1628            ua <= ub);
1629
1630     /* arith2 */
1631     a++;
1632     b++;
1633     printf("arith2: %Ld %Ld\n", a, b);
1634     printf("arith2: %Ld %Ld\n", a++, b++);
1635     printf("arith2: %Ld %Ld\n", --a, --b);
1636     printf("arith2: %Ld %Ld\n", a, b);
1637 }
1638
1639 void llshift(long long a, int b)
1640 {
1641     printf("shift: %Ld %Ld %Ld\n",
1642            (unsigned long long)a >> b,
1643            a >> b,
1644            a << b);
1645     printf("shiftc: %Ld %Ld %Ld\n",
1646            (unsigned long long)a >> 3,
1647            a >> 3,
1648            a << 3);
1649     printf("shiftc: %Ld %Ld %Ld\n",
1650            (unsigned long long)a >> 35,
1651            a >> 35,
1652            a << 35);
1653 }
1654
1655 void llfloat(void)
1656 {
1657     float fa;
1658     double da;
1659     long double lda;
1660     long long la, lb, lc;
1661     unsigned long long ula, ulb, ulc;
1662     la = 0x12345678;
1663     ula = 0x72345678;
1664     la = (la << 20) | 0x12345;
1665     ula = ula << 33;
1666     printf("la=%Ld ula=%Lu\n", la, ula);
1667
1668     fa = la;
1669     da = la;
1670     lda = la;
1671     printf("lltof: %f %f %Lf\n", fa, da, lda);
1672
1673     la = fa;
1674     lb = da;
1675     lc = lda;
1676     printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
1677
1678     fa = ula;
1679     da = ula;
1680     lda = ula;
1681     printf("ulltof: %f %f %Lf\n", fa, da, lda);
1682
1683     ula = fa;
1684     ulb = da;
1685     ulc = lda;
1686     printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
1687 }
1688
1689 long long llfunc1(int a)
1690 {
1691     return a * 2;
1692 }
1693
1694 struct S {
1695     int id;
1696     char item;
1697 };
1698
1699 long long int value(struct S *v)
1700 {
1701     return ((long long int)v->item);
1702 }
1703
1704 void longlong_test(void)
1705 {
1706     long long a, b, c;
1707     unsigned long long ull;
1708     int ia;
1709     unsigned int ua;
1710     printf("longlong_test:\n");
1711     printf("sizeof(long long) = %d\n", sizeof(long long));
1712     ia = -1;
1713     ua = -2;
1714     a = ia;
1715     b = ua;
1716     printf("%Ld %Ld\n", a, b);
1717     printf("%Ld %Ld %Ld %Lx\n",
1718            (long long)1,
1719            (long long)-2,
1720            1LL,
1721            0x1234567812345679);
1722     a = llfunc1(-3);
1723     printf("%Ld\n", a);
1724
1725     lloptest(1000, 23);
1726     lloptest(0xff, 0x1234);
1727     b = 0x72345678 << 10;
1728     lloptest(-3, b);
1729     llshift(0x123, 5);
1730     llshift(-23, 5);
1731     b = 0x72345678LL << 10;
1732     llshift(b, 47);
1733
1734     llfloat();
1735 #if 1
1736     b = 0x12345678;
1737     a = -1;
1738     c = a + b;
1739     printf("%Lx\n", c);
1740 #endif
1741
1742     /* long long reg spill test */
1743     {
1744           struct S a;
1745
1746           a.item = 3;
1747           printf("%lld\n", value(&a));
1748     }
1749     lloptest(0x80000000, 0);
1750
1751     /* another long long spill test */
1752     {
1753         long long *p, v;
1754         v = 1;
1755         p = &v;
1756         p[0]++;
1757         printf("%lld\n", *p);
1758     }
1759
1760     /* Make sure long long comparison tests upper 32 bits */
1761     ull = 0xffffffff00000000ULL;
1762     printf("ull=%llx\n",ull);
1763     printf("ull!=0  ", (ull != 0) ? "true" : "false");
1764     printf("ull     ", ull ? "true" : "false");
1765
1766     /* check double -> ull cast */
1767     {
1768         double d = 2.4e18;
1769         unsigned long long ull = d;
1770         unsigned long long r = 100000000000LL;
1771         printf("\ndouble->ull (grischka case 10): %.0f -> %ld\n",
1772             d/r, (long)(ull/r));
1773     }
1774
1775 }
1776
1777 void vprintf1(const char *fmt, ...)
1778 {
1779     va_list ap;
1780     const char *p;
1781     int c, i;
1782     double d;
1783     long long ll;
1784
1785     va_start(ap, fmt);
1786
1787     p = fmt;
1788     for(;;) {
1789         c = *p;
1790         if (c == '\0')
1791             break;
1792         p++;
1793         if (c == '%') {
1794             c = *p;
1795             switch(c) {
1796             case '\0':
1797                 goto the_end;
1798             case 'd':
1799                 i = va_arg(ap, int);
1800                 printf("%d", i);
1801                 break;
1802             case 'f':
1803                 d = va_arg(ap, double);
1804                 printf("%f", d);
1805                 break;
1806             case 'l':
1807                 ll = va_arg(ap, long long);
1808                 printf("%Ld", ll);
1809                 break;
1810             }
1811             p++;
1812         } else {
1813             putchar(c);
1814         }
1815     }
1816  the_end:
1817     va_end(ap);
1818 }
1819
1820
1821 void stdarg_test(void)
1822 {
1823     vprintf1("%d %d %d\n", 1, 2, 3);
1824     vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1825     vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1826 }
1827
1828 void whitespace_test(void)
1829 {
1830     char *str;
1831
1832 #if 1
1833         printf("whitspace:\n");
1834 #endif
1835     pf("N=%d\n", 2);
1836
1837 #ifdef CORRECT_CR_HANDLING
1838     printf("aaa=%d\n", 3);
1839 #endif
1840
1841     pri\
1842 \
1843 ntf("min=%d\n", 4);
1844
1845 #ifdef ACCEPT_CR_IN_STRINGS
1846     printf("len1=%d\n", strlen("
1847 "));
1848 #ifdef CORRECT_CR_HANDLING
1849     str = "
1850 ";
1851     printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
1852 #endif
1853     printf("len1=%d\n", strlen("\ra
1854 "));
1855 #endif /* ACCEPT_CR_IN_STRINGS */
1856 }
1857
1858 int reltab[3] = { 1, 2, 3 };
1859
1860 int *rel1 = &reltab[1];
1861 int *rel2 = &reltab[2];
1862
1863 void relocation_test(void)
1864 {
1865     printf("*rel1=%d\n", *rel1);
1866     printf("*rel2=%d\n", *rel2);
1867 }
1868
1869 void old_style_f(a,b,c)
1870      int a, b;
1871      double c;
1872 {
1873     printf("a=%d b=%d b=%f\n", a, b, c);
1874 }
1875
1876 void decl_func1(int cmpfn())
1877 {
1878     printf("cmpfn=%lx\n", (long)cmpfn);
1879 }
1880
1881 void decl_func2(cmpfn)
1882 int cmpfn();
1883 {
1884     printf("cmpfn=%lx\n", (long)cmpfn);
1885 }
1886
1887 void old_style_function(void)
1888 {
1889     old_style_f((void *)1, 2, 3.0);
1890     decl_func1(NULL);
1891     decl_func2(NULL);
1892 }
1893
1894 void sizeof_test(void)
1895 {
1896     int a;
1897     int **ptr;
1898
1899     printf("sizeof(int) = %d\n", sizeof(int));
1900     printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1901     printf("sizeof(short) = %d\n", sizeof(short));
1902     printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1903     printf("sizeof(char) = %d\n", sizeof(char));
1904     printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1905     printf("sizeof(func) = %d\n", sizeof sizeof_test());
1906     a = 1;
1907     printf("sizeof(a++) = %d\n", sizeof a++);
1908     printf("a=%d\n", a);
1909     ptr = NULL;
1910     printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
1911     printf("-1>sizeof(int) = %d\n", -1>sizeof(int));
1912
1913     /* some alignof tests */
1914     printf("__alignof__(int) = %d\n", __alignof__(int));
1915     printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1916     printf("__alignof__(short) = %d\n", __alignof__(short));
1917     printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1918     printf("__alignof__(char) = %d\n", __alignof__(char));
1919     printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1920     printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
1921 }
1922
1923 void typeof_test(void)
1924 {
1925     double a;
1926     typeof(a) b;
1927     typeof(float) c;
1928
1929     a = 1.5;
1930     b = 2.5;
1931     c = 3.5;
1932     printf("a=%f b=%f c=%f\n", a, b, c);
1933 }
1934
1935 void statement_expr_test(void)
1936 {
1937     int a, i;
1938
1939     a = 0;
1940     for(i=0;i<10;i++) {
1941         a += 1 +
1942             ( { int b, j;
1943                 b = 0;
1944                 for(j=0;j<5;j++)
1945                     b += j; b;
1946             } );
1947     }
1948     printf("a=%d\n", a);
1949
1950 }
1951
1952 void local_label_test(void)
1953 {
1954     int a;
1955     goto l1;
1956  l2:
1957     a = 1 + ({
1958         __label__ l1, l2, l3, l4;
1959         goto l1;
1960     l4:
1961         printf("aa1\n");
1962         goto l3;
1963     l2:
1964         printf("aa3\n");
1965         goto l4;
1966     l1:
1967         printf("aa2\n");
1968         goto l2;
1969     l3:;
1970         1;
1971     });
1972     printf("a=%d\n", a);
1973     return;
1974  l4:
1975     printf("bb1\n");
1976     goto l2;
1977  l1:
1978     printf("bb2\n");
1979     goto l4;
1980 }
1981
1982 /* inline assembler test */
1983 #ifdef __i386__
1984
1985 /* from linux kernel */
1986 static char * strncat1(char * dest,const char * src,size_t count)
1987 {
1988 int d0, d1, d2, d3;
1989 __asm__ __volatile__(
1990         "repne\n\t"
1991         "scasb\n\t"
1992         "decl %1\n\t"
1993         "movl %8,%3\n"
1994         "1:\tdecl %3\n\t"
1995         "js 2f\n\t"
1996         "lodsb\n\t"
1997         "stosb\n\t"
1998         "testb %%al,%%al\n\t"
1999         "jne 1b\n"
2000         "2:\txorl %2,%2\n\t"
2001         "stosb"
2002         : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2003         : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2004         : "memory");
2005 return dest;
2006 }
2007
2008 static inline void * memcpy1(void * to, const void * from, size_t n)
2009 {
2010 int d0, d1, d2;
2011 __asm__ __volatile__(
2012         "rep ; movsl\n\t"
2013         "testb $2,%b4\n\t"
2014         "je 1f\n\t"
2015         "movsw\n"
2016         "1:\ttestb $1,%b4\n\t"
2017         "je 2f\n\t"
2018         "movsb\n"
2019         "2:"
2020         : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2021         :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2022         : "memory");
2023 return (to);
2024 }
2025
2026 static __inline__ void sigaddset1(unsigned int *set, int _sig)
2027 {
2028         __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2029 }
2030
2031 static __inline__ void sigdelset1(unsigned int *set, int _sig)
2032 {
2033         asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2034 }
2035
2036 static __inline__ __const__ unsigned int swab32(unsigned int x)
2037 {
2038         __asm__("xchgb %b0,%h0\n\t"     /* swap lower bytes     */
2039                 "rorl $16,%0\n\t"       /* swap words           */
2040                 "xchgb %b0,%h0"         /* swap higher bytes    */
2041                 :"=q" (x)
2042                 : "0" (x));
2043         return x;
2044 }
2045
2046 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
2047 {
2048     unsigned long long res;
2049     __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
2050     return res;
2051 }
2052
2053 static __inline__ unsigned long long inc64(unsigned long long a)
2054 {
2055     unsigned long long res;
2056     __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
2057     return res;
2058 }
2059
2060 unsigned int set;
2061
2062 void asm_test(void)
2063 {
2064     char buf[128];
2065     unsigned int val;
2066
2067     printf("inline asm:\n");
2068     /* test the no operand case */
2069     asm volatile ("xorl %eax, %eax");
2070
2071     memcpy1(buf, "hello", 6);
2072     strncat1(buf, " worldXXXXX", 3);
2073     printf("%s\n", buf);
2074
2075     /* 'A' constraint test */
2076     printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2077     printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2078
2079     set = 0xff;
2080     sigdelset1(&set, 2);
2081     sigaddset1(&set, 16);
2082     /* NOTE: we test here if C labels are correctly restored after the
2083        asm statement */
2084     goto label1;
2085  label2:
2086     __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
2087     printf("set=0x%x\n", set);
2088     val = 0x01020304;
2089     printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
2090     return;
2091  label1:
2092     goto label2;
2093 }
2094
2095 #else
2096
2097 void asm_test(void)
2098 {
2099 }
2100
2101 #endif
2102
2103 #define COMPAT_TYPE(type1, type2) \
2104 {\
2105     printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2106            __builtin_types_compatible_p (type1, type2));\
2107 }
2108
2109 int constant_p_var;
2110
2111 void builtin_test(void)
2112 {
2113 #if GCC_MAJOR >= 3
2114     COMPAT_TYPE(int, int);
2115     COMPAT_TYPE(int, unsigned int);
2116     COMPAT_TYPE(int, char);
2117     COMPAT_TYPE(int, const int);
2118     COMPAT_TYPE(int, volatile int);
2119     COMPAT_TYPE(int *, int *);
2120     COMPAT_TYPE(int *, void *);
2121     COMPAT_TYPE(int *, const int *);
2122     COMPAT_TYPE(char *, unsigned char *);
2123 /* space is needed because tcc preprocessor introduces a space between each token */
2124     COMPAT_TYPE(char * *, void *);
2125 #endif
2126     printf("res = %d\n", __builtin_constant_p(1));
2127     printf("res = %d\n", __builtin_constant_p(1 + 2));
2128     printf("res = %d\n", __builtin_constant_p(&constant_p_var));
2129     printf("res = %d\n", __builtin_constant_p(constant_p_var));
2130 }
2131
2132
2133 /* static_stub1 takes a pointer to a function, and returns a pointer to
2134  * a function; that function must accept no parameters, and return nothing. */
2135 void ((*static_stub1(void ((*p)(void)))) (void))
2136 {
2137     static void (*pfn)(void);
2138     void (*mytemp)(void);
2139     mytemp = pfn;
2140     pfn = p;
2141     return mytemp;
2142 }
2143
2144 /* Another stub.  The static "pfn" is in a different function, so it'd better
2145    not be mapped to the same memory location. */
2146 void ((*static_stub2(void ((*p)(void)))) (void))
2147 {
2148     static void (*pfn)(void);
2149     void (*mytemp)(void);
2150     mytemp = pfn;
2151     pfn = p;
2152     return mytemp;
2153 }
2154
2155
2156 void static_test(void)
2157 {
2158     void (*result)(void);
2159     result = static_stub1(static_test);
2160     printf("static_test Expect 1 -> %d\n", result == 0);
2161     result = static_stub1(static_test);
2162     printf("static_test Expect 1 -> %d\n", result == static_test);
2163     result = static_stub2(static_test);
2164     printf("static_test Expect 1 -> %d\n", result == 0);
2165     result = static_stub2(static_test);
2166     printf("static_test Expect 1 -> %d\n", result == static_test);
2167 }
2168
2169
2170 void const_func(const int a)
2171 {
2172 }
2173
2174 void const_warn_test(void)
2175 {
2176     const_func(1);
2177 }