fix end-of-scope for self-referential macros
[tinycc.git] / tests / tcctest.c
blob22f3fdb87dfbbfdae3fc2f222d0cf19bd3971344
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 /* deprecated and no longer supported in gcc 3.3 */
20 //#define ACCEPT_CR_IN_STRINGS
22 /* __VA_ARGS__ and __func__ support */
23 #define C99_MACROS
25 /* test various include syntaxes */
27 #define TCCLIB_INC <tcclib.h>
28 #define TCCLIB_INC1 <tcclib
29 #define TCCLIB_INC2 h>
30 #define TCCLIB_INC3 "tcclib"
32 #include TCCLIB_INC
34 #include TCCLIB_INC1.TCCLIB_INC2
36 #include TCCLIB_INC1.h>
38 /* gcc 3.2 does not accept that (bug ?) */
39 //#include TCCLIB_INC3 ".h"
41 #include <tcclib.h>
43 #include "tcclib.h"
45 void string_test();
46 void expr_test();
47 void macro_test();
48 void recursive_macro_test();
49 void scope_test();
50 void forward_test();
51 void funcptr_test();
52 void loop_test();
53 void switch_test();
54 void goto_test();
55 void enum_test();
56 void typedef_test();
57 void struct_test();
58 void array_test();
59 void expr_ptr_test();
60 void bool_test();
61 void expr2_test();
62 void constant_expr_test();
63 void expr_cmp_test();
64 void char_short_test();
65 void init_test(void);
66 void compound_literal_test(void);
67 int kr_test();
68 void struct_assign_test(void);
69 void cast_test(void);
70 void bitfield_test(void);
71 void c99_bool_test(void);
72 void float_test(void);
73 void longlong_test(void);
74 void manyarg_test(void);
75 void stdarg_test(void);
76 void whitespace_test(void);
77 void relocation_test(void);
78 void old_style_function(void);
79 void alloca_test(void);
80 void c99_vla_test(int size1, int size2);
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 weak_test(void);
89 int fib(int n);
90 void num(int n);
91 void forward_ref(void);
92 int isid(int c);
94 #define A 2
95 #define N 1234 + A
96 #define pf printf
97 #define M1(a, b) (a) + (b)
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"
106 #define min(a, b) ((a) < (b) ? (a) : (b))
108 #ifdef C99_MACROS
109 #define dprintf(level,...) printf(__VA_ARGS__)
110 #endif
112 /* gcc vararg macros */
113 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
115 #define MACRO_NOARGS()
117 #define AAA 3
118 #undef AAA
119 #define AAA 4
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
131 #define __INT64_C(c) c ## LL
132 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
134 int qq(int x)
136 return x + 40;
138 #define qq(x) x
140 #define spin_lock(lock) do { } while (0)
141 #define wq_spin_lock spin_lock
142 #define TEST2() wq_spin_lock(a)
144 void macro_test(void)
146 printf("macro:\n");\f\v
147 pf("N=%d\n", N);
148 printf("aaa=%d\n", AAA);
150 printf("min=%d\n", min(1, min(2, -1)));
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);
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
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
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
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
217 MACRO_NOARGS();
219 #ifdef __LINE__
220 printf("__LINE__ defined\n");
221 #endif
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 227 "tcctest.c"
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);
242 /* gcc extension */
243 printf("func='%s'\n", __FUNCTION__);
245 /* complicated macros in glibc */
246 printf("INT64_MIN=%Ld\n", INT64_MIN);
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);
256 /* macro function with argument outside the macro string */
257 #define MF_s MF_hello
258 #define MF_hello(msg) printf("%s\n",msg)
260 #define MF_t printf("tralala\n"); MF_hello
262 MF_s("hi");
263 MF_t("hi");
265 /* test macro substituion inside args (should not eat stream) */
266 printf("qq=%d\n", qq(qq)(2));
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. */
271 #define qq1(x) 1
272 printf("qq1=%d\n", qq1( ));
274 /* comment with stray handling *\
276 /* this is a valid *\/ comment */
277 /* this is a valid comment *\*/
278 // this is a valid\
279 comment
281 /* test function macro substitution when the function name is
282 substituted */
283 TEST2();
287 static void print_num(char *fn, int line, int num) {
288 printf("fn %s, line %d, num %d\n", fn, line, num);
291 void recursive_macro_test(void)
294 #define ELF32_ST_TYPE(val) ((val) & 0xf)
295 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
296 #define STB_WEAK 2 /* Weak symbol */
297 #define ELFW(type) ELF##32##_##type
298 printf("%d\n", ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(123)));
300 #define WRAP(x) x
302 #define print_num(x) print_num(__FILE__,__LINE__,x)
303 print_num(123);
304 WRAP(print_num(123));
305 WRAP(WRAP(print_num(123)));
307 static struct recursive_macro { int rm_field; } G;
308 #define rm_field (G.rm_field)
309 printf("rm_field = %d\n", rm_field);
310 printf("rm_field = %d\n", WRAP(rm_field));
311 WRAP((printf("rm_field = %d %d\n", rm_field, WRAP(rm_field))));
314 int op(a,b)
316 return a / b;
319 int ret(a)
321 if (a == 2)
322 return 1;
323 if (a == 3)
324 return 2;
325 return 0;
328 void ps(const char *s)
330 int c;
331 while (1) {
332 c = *s;
333 if (c == 0)
334 break;
335 printf("%c", c);
336 s++;
340 const char foo1_string[] = "\
341 bar\n\
342 test\14\
345 void string_test()
347 unsigned int b;
348 printf("string:\n");
349 printf("\141\1423\143\n");/* dezdez test */
350 printf("\x41\x42\x43\x3a\n");
351 printf("c=%c\n", 'r');
352 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
353 printf("foo1_string='%s'\n", foo1_string);
354 #if 0
355 printf("wstring=%S\n", L"abc");
356 printf("wstring=%S\n", L"abc" L"def" "ghi");
357 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
358 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
359 #endif
360 ps("test\n");
361 b = 32;
362 while ((b = b + 1) < 96) {
363 printf("%c", b);
365 printf("\n");
366 printf("fib=%d\n", fib(33));
367 b = 262144;
368 while (b != 0x80000000) {
369 num(b);
370 b = b * 2;
374 void loop_test()
376 int i;
377 i = 0;
378 while (i < 10)
379 printf("%d", i++);
380 printf("\n");
381 for(i = 0; i < 10;i++)
382 printf("%d", i);
383 printf("\n");
384 i = 0;
385 do {
386 printf("%d", i++);
387 } while (i < 10);
388 printf("\n");
390 char count = 123;
391 /* c99 for loop init test */
392 for (size_t count = 1; count < 3; count++)
393 printf("count=%d\n", count);
394 printf("count = %d\n", count);
396 /* break/continue tests */
397 i = 0;
398 while (1) {
399 if (i == 6)
400 break;
401 i++;
402 if (i == 3)
403 continue;
404 printf("%d", i);
406 printf("\n");
408 /* break/continue tests */
409 i = 0;
410 do {
411 if (i == 6)
412 break;
413 i++;
414 if (i == 3)
415 continue;
416 printf("%d", i);
417 } while(1);
418 printf("\n");
420 for(i = 0;i < 10;i++) {
421 if (i == 3)
422 continue;
423 printf("%d", i);
425 printf("\n");
429 void goto_test()
431 int i;
432 static void *label_table[3] = { &&label1, &&label2, &&label3 };
434 printf("goto:\n");
435 i = 0;
436 s_loop:
437 if (i >= 10)
438 goto s_end;
439 printf("%d", i);
440 i++;
441 goto s_loop;
442 s_end:
443 printf("\n");
445 /* we also test computed gotos (GCC extension) */
446 for(i=0;i<3;i++) {
447 goto *label_table[i];
448 label1:
449 printf("label1\n");
450 goto next;
451 label2:
452 printf("label2\n");
453 goto next;
454 label3:
455 printf("label3\n");
456 next: ;
460 enum {
462 E1 = 2,
463 E2 = 4,
468 enum test {
469 E5 = 1000,
472 void enum_test()
474 enum test b1;
475 printf("enum:\n%d %d %d %d %d %d\n",
476 E0, E1, E2, E3, E4, E5);
477 b1 = 1;
478 printf("b1=%d\n", b1);
481 typedef int *my_ptr;
483 typedef int mytype1;
484 typedef int mytype2;
486 void typedef_test()
488 my_ptr a;
489 mytype1 mytype2;
490 int b;
492 a = &b;
493 *a = 1234;
494 printf("typedef:\n");
495 printf("a=%d\n", *a);
496 mytype2 = 2;
497 printf("mytype2=%d\n", mytype2);
500 void forward_test()
502 printf("forward:\n");
503 forward_ref();
504 forward_ref();
508 void forward_ref(void)
510 printf("forward ok\n");
513 typedef struct struct1 {
514 int f1;
515 int f2, f3;
516 union union1 {
517 int v1;
518 int v2;
519 } u;
520 char str[3];
521 } struct1;
523 struct struct2 {
524 int a;
525 char b;
528 union union2 {
529 int w1;
530 int w2;
533 struct struct1 st1, st2;
535 int main(int argc, char **argv)
537 string_test();
538 expr_test();
539 macro_test();
540 recursive_macro_test();
541 scope_test();
542 forward_test();
543 funcptr_test();
544 loop_test();
545 switch_test();
546 goto_test();
547 enum_test();
548 typedef_test();
549 struct_test();
550 array_test();
551 expr_ptr_test();
552 bool_test();
553 expr2_test();
554 constant_expr_test();
555 expr_cmp_test();
556 char_short_test();
557 init_test();
558 compound_literal_test();
559 kr_test();
560 struct_assign_test();
561 cast_test();
562 bitfield_test();
563 c99_bool_test();
564 float_test();
565 longlong_test();
566 manyarg_test();
567 stdarg_test();
568 whitespace_test();
569 relocation_test();
570 old_style_function();
571 alloca_test();
572 c99_vla_test(5, 2);
573 sizeof_test();
574 typeof_test();
575 statement_expr_test();
576 local_label_test();
577 asm_test();
578 builtin_test();
579 weak_test();
580 return 0;
583 int tab[3];
584 int tab2[3][2];
586 int g;
588 void f1(g)
590 printf("g1=%d\n", g);
593 void scope_test()
595 printf("scope:\n");
596 g = 2;
597 f1(1);
598 printf("g2=%d\n", g);
600 int g;
601 g = 3;
602 printf("g3=%d\n", g);
604 int g;
605 g = 4;
606 printf("g4=%d\n", g);
609 printf("g5=%d\n", g);
612 void array_test(int a[4])
614 int i, j;
616 printf("array:\n");
617 printf("sizeof(a) = %d\n", sizeof(a));
618 printf("sizeof(\"a\") = %d\n", sizeof("a"));
619 #ifdef C99_MACROS
620 printf("sizeof(__func__) = %d\n", sizeof(__func__));
621 #endif
622 printf("sizeof tab %d\n", sizeof(tab));
623 printf("sizeof tab2 %d\n", sizeof tab2);
624 tab[0] = 1;
625 tab[1] = 2;
626 tab[2] = 3;
627 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
628 for(i=0;i<3;i++)
629 for(j=0;j<2;j++)
630 tab2[i][j] = 10 * i + j;
631 for(i=0;i<3*2;i++) {
632 printf(" %3d", ((int *)tab2)[i]);
634 printf("\n");
635 printf("sizeof(size_t)=%d\n", sizeof(size_t));
636 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
639 void expr_test()
641 int a, b;
642 a = 0;
643 printf("%d\n", a += 1);
644 printf("%d\n", a -= 2);
645 printf("%d\n", a *= 31232132);
646 printf("%d\n", a /= 4);
647 printf("%d\n", a %= 20);
648 printf("%d\n", a &= 6);
649 printf("%d\n", a ^= 7);
650 printf("%d\n", a |= 8);
651 printf("%d\n", a >>= 3);
652 printf("%d\n", a <<= 4);
654 a = 22321;
655 b = -22321;
656 printf("%d\n", a + 1);
657 printf("%d\n", a - 2);
658 printf("%d\n", a * 312);
659 printf("%d\n", a / 4);
660 printf("%d\n", b / 4);
661 printf("%d\n", (unsigned)b / 4);
662 printf("%d\n", a % 20);
663 printf("%d\n", b % 20);
664 printf("%d\n", (unsigned)b % 20);
665 printf("%d\n", a & 6);
666 printf("%d\n", a ^ 7);
667 printf("%d\n", a | 8);
668 printf("%d\n", a >> 3);
669 printf("%d\n", b >> 3);
670 printf("%d\n", (unsigned)b >> 3);
671 printf("%d\n", a << 4);
672 printf("%d\n", ~a);
673 printf("%d\n", -a);
674 printf("%d\n", +a);
676 printf("%d\n", 12 + 1);
677 printf("%d\n", 12 - 2);
678 printf("%d\n", 12 * 312);
679 printf("%d\n", 12 / 4);
680 printf("%d\n", 12 % 20);
681 printf("%d\n", 12 & 6);
682 printf("%d\n", 12 ^ 7);
683 printf("%d\n", 12 | 8);
684 printf("%d\n", 12 >> 2);
685 printf("%d\n", 12 << 4);
686 printf("%d\n", ~12);
687 printf("%d\n", -12);
688 printf("%d\n", +12);
689 printf("%d %d %d %d\n",
690 isid('a'),
691 isid('g'),
692 isid('T'),
693 isid('('));
696 int isid(int c)
698 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
701 /**********************/
703 int vstack[10], *vstack_ptr;
705 void vpush(int vt, int vc)
707 *vstack_ptr++ = vt;
708 *vstack_ptr++ = vc;
711 void vpop(int *ft, int *fc)
713 *fc = *--vstack_ptr;
714 *ft = *--vstack_ptr;
717 void expr2_test()
719 int a, b;
721 printf("expr2:\n");
722 vstack_ptr = vstack;
723 vpush(1432432, 2);
724 vstack_ptr[-2] &= ~0xffffff80;
725 vpop(&a, &b);
726 printf("res= %d %d\n", a, b);
729 void constant_expr_test()
731 int a;
732 printf("constant_expr:\n");
733 a = 3;
734 printf("%d\n", a * 16);
735 printf("%d\n", a * 1);
736 printf("%d\n", a + 0);
739 int tab4[10];
741 void expr_ptr_test()
743 int *p, *q;
744 int i = -1;
746 printf("expr_ptr:\n");
747 p = tab4;
748 q = tab4 + 10;
749 printf("diff=%d\n", q - p);
750 p++;
751 printf("inc=%d\n", p - tab4);
752 p--;
753 printf("dec=%d\n", p - tab4);
754 ++p;
755 printf("inc=%d\n", p - tab4);
756 --p;
757 printf("dec=%d\n", p - tab4);
758 printf("add=%d\n", p + 3 - tab4);
759 printf("add=%d\n", 3 + p - tab4);
761 /* check if 64bit support is ok */
762 q = p = 0;
763 q += i;
764 printf("%p %p %ld\n", q, p, p-q);
765 printf("%d %d %d %d %d %d\n",
766 p == q, p != q, p < q, p <= q, p >= q, p > q);
767 i = 0xf0000000;
768 p += i;
769 printf("%p %p %ld\n", q, p, p-q);
770 printf("%d %d %d %d %d %d\n",
771 p == q, p != q, p < q, p <= q, p >= q, p > q);
772 p = (int *)((char *)p + 0xf0000000);
773 printf("%p %p %ld\n", q, p, p-q);
774 printf("%d %d %d %d %d %d\n",
775 p == q, p != q, p < q, p <= q, p >= q, p > q);
776 p += 0xf0000000;
777 printf("%p %p %ld\n", q, p, p-q);
778 printf("%d %d %d %d %d %d\n",
779 p == q, p != q, p < q, p <= q, p >= q, p > q);
781 struct size12 {
782 int i, j, k;
784 struct size12 s[2], *sp = s;
785 int i, j;
786 sp->i = 42;
787 sp++;
788 j = -1;
789 printf("%d\n", sp[j].i);
793 void expr_cmp_test()
795 int a, b;
796 printf("constant_expr:\n");
797 a = -1;
798 b = 1;
799 printf("%d\n", a == a);
800 printf("%d\n", a != a);
802 printf("%d\n", a < b);
803 printf("%d\n", a <= b);
804 printf("%d\n", a <= a);
805 printf("%d\n", b >= a);
806 printf("%d\n", a >= a);
807 printf("%d\n", b > a);
809 printf("%d\n", (unsigned)a < b);
810 printf("%d\n", (unsigned)a <= b);
811 printf("%d\n", (unsigned)a <= a);
812 printf("%d\n", (unsigned)b >= a);
813 printf("%d\n", (unsigned)a >= a);
814 printf("%d\n", (unsigned)b > a);
817 struct empty {
820 struct aligntest1 {
821 char a[10];
824 struct aligntest2 {
825 int a;
826 char b[10];
829 struct aligntest3 {
830 double a, b;
833 struct aligntest4 {
834 double a[0];
837 void struct_test()
839 struct1 *s;
840 union union2 u;
842 printf("struct:\n");
843 printf("sizes: %d %d %d %d\n",
844 sizeof(struct struct1),
845 sizeof(struct struct2),
846 sizeof(union union1),
847 sizeof(union union2));
848 st1.f1 = 1;
849 st1.f2 = 2;
850 st1.f3 = 3;
851 printf("st1: %d %d %d\n",
852 st1.f1, st1.f2, st1.f3);
853 st1.u.v1 = 1;
854 st1.u.v2 = 2;
855 printf("union1: %d\n", st1.u.v1);
856 u.w1 = 1;
857 u.w2 = 2;
858 printf("union2: %d\n", u.w1);
859 s = &st2;
860 s->f1 = 3;
861 s->f2 = 2;
862 s->f3 = 1;
863 printf("st2: %d %d %d\n",
864 s->f1, s->f2, s->f3);
865 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
867 /* align / size tests */
868 printf("aligntest1 sizeof=%d alignof=%d\n",
869 sizeof(struct aligntest1), __alignof__(struct aligntest1));
870 printf("aligntest2 sizeof=%d alignof=%d\n",
871 sizeof(struct aligntest2), __alignof__(struct aligntest2));
872 printf("aligntest3 sizeof=%d alignof=%d\n",
873 sizeof(struct aligntest3), __alignof__(struct aligntest3));
874 printf("aligntest4 sizeof=%d alignof=%d\n",
875 sizeof(struct aligntest4), __alignof__(struct aligntest4));
877 /* empty structures (GCC extension) */
878 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
879 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
882 /* XXX: depend on endianness */
883 void char_short_test()
885 int var1, var2;
887 printf("char_short:\n");
889 var1 = 0x01020304;
890 var2 = 0xfffefdfc;
891 printf("s8=%d %d\n",
892 *(char *)&var1, *(char *)&var2);
893 printf("u8=%d %d\n",
894 *(unsigned char *)&var1, *(unsigned char *)&var2);
895 printf("s16=%d %d\n",
896 *(short *)&var1, *(short *)&var2);
897 printf("u16=%d %d\n",
898 *(unsigned short *)&var1, *(unsigned short *)&var2);
899 printf("s32=%d %d\n",
900 *(int *)&var1, *(int *)&var2);
901 printf("u32=%d %d\n",
902 *(unsigned int *)&var1, *(unsigned int *)&var2);
903 *(char *)&var1 = 0x08;
904 printf("var1=%x\n", var1);
905 *(short *)&var1 = 0x0809;
906 printf("var1=%x\n", var1);
907 *(int *)&var1 = 0x08090a0b;
908 printf("var1=%x\n", var1);
911 /******************/
913 typedef struct Sym {
914 int v;
915 int t;
916 int c;
917 struct Sym *next;
918 struct Sym *prev;
919 } Sym;
921 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
922 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
924 static int toupper1(int a)
926 return TOUPPER(a);
929 void bool_test()
931 int *s, a, b, t, f, i;
933 a = 0;
934 s = (void*)0;
935 printf("!s=%d\n", !s);
937 if (!s || !s[0])
938 a = 1;
939 printf("a=%d\n", a);
941 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
942 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
943 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
944 #if 1 && 1
945 printf("a1\n");
946 #endif
947 #if 1 || 0
948 printf("a2\n");
949 #endif
950 #if 1 ? 0 : 1
951 printf("a3\n");
952 #endif
953 #if 0 ? 0 : 1
954 printf("a4\n");
955 #endif
957 a = 4;
958 printf("b=%d\n", a + (0 ? 1 : a / 2));
960 /* test register spilling */
961 a = 10;
962 b = 10;
963 a = (a + b) * ((a < b) ?
964 ((b - a) * (a - b)): a + b);
965 printf("a=%d\n", a);
967 /* test complex || or && expressions */
968 t = 1;
969 f = 0;
970 a = 32;
971 printf("exp=%d\n", f == (32 <= a && a <= 3));
972 printf("r=%d\n", (t || f) + (t && f));
974 /* test ? : cast */
976 int aspect_on;
977 int aspect_native = 65536;
978 double bfu_aspect = 1.0;
979 int aspect;
980 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
981 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
982 printf("aspect=%d\n", aspect);
986 /* test ? : GCC extension */
988 static int v1 = 34 ? : -1; /* constant case */
989 static int v2 = 0 ? : -1; /* constant case */
990 int a = 30;
992 printf("%d %d\n", v1, v2);
993 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
996 /* again complex expression */
997 for(i=0;i<256;i++) {
998 if (toupper1 (i) != TOUPPER (i))
999 printf("error %d\n", i);
1003 /* GCC accepts that */
1004 static int tab_reinit[];
1005 static int tab_reinit[10];
1007 //int cinit1; /* a global variable can be defined several times without error ! */
1008 int cinit1;
1009 int cinit1;
1010 int cinit1 = 0;
1011 int *cinit2 = (int []){3, 2, 1};
1013 void compound_literal_test(void)
1015 int *p, i;
1016 char *q, *q3;
1018 printf("compound_test:\n");
1020 p = (int []){1, 2, 3};
1021 for(i=0;i<3;i++)
1022 printf(" %d", p[i]);
1023 printf("\n");
1025 for(i=0;i<3;i++)
1026 printf("%d", cinit2[i]);
1027 printf("\n");
1029 q = "tralala1";
1030 printf("q1=%s\n", q);
1032 q = (char *){ "tralala2" };
1033 printf("q2=%s\n", q);
1035 q3 = (char *){ q };
1036 printf("q3=%s\n", q3);
1038 q = (char []){ "tralala3" };
1039 printf("q4=%s\n", q);
1041 #ifdef ALL_ISOC99
1042 p = (int []){1, 2, cinit1 + 3};
1043 for(i=0;i<3;i++)
1044 printf(" %d", p[i]);
1045 printf("\n");
1047 for(i=0;i<3;i++) {
1048 p = (int []){1, 2, 4 + i};
1049 printf("%d %d %d\n",
1050 p[0],
1051 p[1],
1052 p[2]);
1054 #endif
1057 /* K & R protos */
1059 kr_func1(a, b)
1061 return a + b;
1064 int kr_func2(a, b)
1066 return a + b;
1069 kr_test()
1071 printf("kr_test:\n");
1072 printf("func1=%d\n", kr_func1(3, 4));
1073 printf("func2=%d\n", kr_func2(3, 4));
1074 return 0;
1077 void num(int n)
1079 char *tab, *p;
1080 tab = (char*)malloc(20);
1081 p = tab;
1082 while (1) {
1083 *p = 48 + (n % 10);
1084 p++;
1085 n = n / 10;
1086 if (n == 0)
1087 break;
1089 while (p != tab) {
1090 p--;
1091 printf("%c", *p);
1093 printf("\n");
1094 free(tab);
1097 /* structure assignment tests */
1098 struct structa1 {
1099 int f1;
1100 char f2;
1103 struct structa1 ssta1;
1105 void struct_assign_test1(struct structa1 s1, int t, float f)
1107 printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
1110 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1112 s1.f1 += t;
1113 s1.f2 -= t;
1114 return s1;
1117 void struct_assign_test(void)
1119 struct structa1 lsta1, lsta2;
1121 #if 0
1122 printf("struct_assign_test:\n");
1124 lsta1.f1 = 1;
1125 lsta1.f2 = 2;
1126 printf("%d %d\n", lsta1.f1, lsta1.f2);
1127 lsta2 = lsta1;
1128 printf("%d %d\n", lsta2.f1, lsta2.f2);
1129 #else
1130 lsta2.f1 = 1;
1131 lsta2.f2 = 2;
1132 #endif
1133 struct_assign_test1(lsta2, 3, 4.5);
1135 printf("before call: %d %d\n", lsta2.f1, lsta2.f2);
1136 lsta2 = struct_assign_test2(lsta2, 4);
1137 printf("after call: %d %d\n", lsta2.f1, lsta2.f2);
1139 static struct {
1140 void (*elem)();
1141 } t[] = {
1142 /* XXX: we should allow this even without braces */
1143 { struct_assign_test }
1145 printf("%d\n", struct_assign_test == t[0].elem);
1148 /* casts to short/char */
1150 void cast1(char a, short b, unsigned char c, unsigned short d)
1152 printf("%d %d %d %d\n", a, b, c, d);
1155 char bcast;
1156 short scast;
1158 void cast_test()
1160 int a;
1161 char c;
1162 char tab[10];
1163 unsigned b,d;
1164 short s;
1165 char *p = NULL;
1166 p -= 0x700000000042;
1168 printf("cast_test:\n");
1169 a = 0xfffff;
1170 cast1(a, a, a, a);
1171 a = 0xffffe;
1172 printf("%d %d %d %d\n",
1173 (char)(a + 1),
1174 (short)(a + 1),
1175 (unsigned char)(a + 1),
1176 (unsigned short)(a + 1));
1177 printf("%d %d %d %d\n",
1178 (char)0xfffff,
1179 (short)0xfffff,
1180 (unsigned char)0xfffff,
1181 (unsigned short)0xfffff);
1183 a = (bcast = 128) + 1;
1184 printf("%d\n", a);
1185 a = (scast = 65536) + 1;
1186 printf("%d\n", a);
1188 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1190 /* test cast from unsigned to signed short to int */
1191 b = 0xf000;
1192 d = (short)b;
1193 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1194 b = 0xf0f0;
1195 d = (char)b;
1196 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1198 /* test implicit int casting for array accesses */
1199 c = 0;
1200 tab[1] = 2;
1201 tab[c] = 1;
1202 printf("%d %d\n", tab[0], tab[1]);
1204 /* test implicit casting on some operators */
1205 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1206 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1207 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1209 /* from pointer to integer types */
1210 printf("%d %d %ld %ld %lld %lld\n",
1211 (int)p, (unsigned int)p,
1212 (long)p, (unsigned long)p,
1213 (long long)p, (unsigned long long)p);
1215 /* from integers to pointers */
1216 printf("%p %p %p %p\n",
1217 (void *)a, (void *)b, (void *)c, (void *)d);
1220 /* initializers tests */
1221 struct structinit1 {
1222 int f1;
1223 char f2;
1224 short f3;
1225 int farray[3];
1228 int sinit1 = 2;
1229 int sinit2 = { 3 };
1230 int sinit3[3] = { 1, 2, {{3}}, };
1231 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1232 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1233 int sinit6[] = { 1, 2, 3 };
1234 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1235 char sinit8[] = "hello" "trala";
1237 struct structinit1 sinit9 = { 1, 2, 3 };
1238 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1239 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1240 #ifdef ALL_ISOC99
1241 .farray[0] = 10,
1242 .farray[1] = 11,
1243 .farray[2] = 12,
1244 #endif
1247 char *sinit12 = "hello world";
1248 char *sinit13[] = {
1249 "test1",
1250 "test2",
1251 "test3",
1253 char sinit14[10] = { "abc" };
1254 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1256 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1258 struct bar {
1259 char *s;
1260 int len;
1261 } sinit17[] = {
1262 "a1", 4,
1263 "a2", 1
1266 int sinit18[10] = {
1267 [2 ... 5] = 20,
1269 [8] = 10,
1272 struct complexinit0 {
1273 int a;
1274 int b;
1277 struct complexinit {
1278 int a;
1279 const struct complexinit0 *b;
1282 const static struct complexinit cix[] = {
1283 [0] = {
1284 .a = 2000,
1285 .b = (const struct complexinit0[]) {
1286 { 2001, 2002 },
1287 { 2003, 2003 },
1293 struct complexinit2 {
1294 int a;
1295 int b[];
1298 struct complexinit2 cix21 = {
1299 .a = 3000,
1300 .b = { 3001, 3002, 3003 }
1303 struct complexinit2 cix22 = {
1304 .a = 4000,
1305 .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1308 void init_test(void)
1310 int linit1 = 2;
1311 int linit2 = { 3 };
1312 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1313 int linit6[] = { 1, 2, 3 };
1314 int i, j;
1315 char linit8[] = "hello" "trala";
1316 int linit12[10] = { 1, 2 };
1317 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1318 char linit14[10] = "abc";
1319 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1320 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1321 int linit17 = sizeof(linit17);
1323 printf("init_test:\n");
1325 printf("sinit1=%d\n", sinit1);
1326 printf("sinit2=%d\n", sinit2);
1327 printf("sinit3=%d %d %d %d\n",
1328 sizeof(sinit3),
1329 sinit3[0],
1330 sinit3[1],
1331 sinit3[2]
1333 printf("sinit6=%d\n", sizeof(sinit6));
1334 printf("sinit7=%d %d %d %d\n",
1335 sizeof(sinit7),
1336 sinit7[0],
1337 sinit7[1],
1338 sinit7[2]
1340 printf("sinit8=%s\n", sinit8);
1341 printf("sinit9=%d %d %d\n",
1342 sinit9.f1,
1343 sinit9.f2,
1344 sinit9.f3
1346 printf("sinit10=%d %d %d\n",
1347 sinit10.f1,
1348 sinit10.f2,
1349 sinit10.f3
1351 printf("sinit11=%d %d %d %d %d %d\n",
1352 sinit11.f1,
1353 sinit11.f2,
1354 sinit11.f3,
1355 sinit11.farray[0],
1356 sinit11.farray[1],
1357 sinit11.farray[2]
1360 for(i=0;i<3;i++)
1361 for(j=0;j<2;j++)
1362 printf("[%d][%d] = %d %d %d\n",
1363 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1364 printf("linit1=%d\n", linit1);
1365 printf("linit2=%d\n", linit2);
1366 printf("linit6=%d\n", sizeof(linit6));
1367 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1369 printf("sinit12=%s\n", sinit12);
1370 printf("sinit13=%d %s %s %s\n",
1371 sizeof(sinit13),
1372 sinit13[0],
1373 sinit13[1],
1374 sinit13[2]);
1375 printf("sinit14=%s\n", sinit14);
1377 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1378 printf("\n");
1379 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1380 printf("\n");
1381 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1382 printf("\n");
1383 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1384 printf("\n");
1385 printf("%d %d %d %d\n",
1386 linit16.a1,
1387 linit16.a2,
1388 linit16.a3,
1389 linit16.a4);
1390 /* test that initialisation is done after variable declare */
1391 printf("linit17=%d\n", linit17);
1392 printf("sinit15=%d\n", sinit15[0]);
1393 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1394 printf("sinit17=%s %d %s %d\n",
1395 sinit17[0].s, sinit17[0].len,
1396 sinit17[1].s, sinit17[1].len);
1397 for(i=0;i<10;i++)
1398 printf("%x ", sinit18[i]);
1399 printf("\n");
1400 /* complex init check */
1401 printf("cix: %d %d %d %d %d %d %d\n",
1402 cix[0].a,
1403 cix[0].b[0].a, cix[0].b[0].b,
1404 cix[0].b[1].a, cix[0].b[1].b,
1405 cix[0].b[2].a, cix[0].b[2].b);
1406 printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
1407 printf("sizeof cix21 %d, sizeof cix22 %d\n", sizeof cix21, sizeof cix22);
1411 void switch_test()
1413 int i;
1415 for(i=0;i<15;i++) {
1416 switch(i) {
1417 case 0:
1418 case 1:
1419 printf("a");
1420 break;
1421 default:
1422 printf("%d", i);
1423 break;
1424 case 8 ... 12:
1425 printf("c");
1426 break;
1427 case 3:
1428 printf("b");
1429 break;
1432 printf("\n");
1435 /* ISOC99 _Bool type */
1436 void c99_bool_test(void)
1438 #ifdef BOOL_ISOC99
1439 int a;
1440 _Bool b;
1442 printf("bool_test:\n");
1443 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1444 a = 3;
1445 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1446 b = 3;
1447 printf("b = %d\n", b);
1448 b++;
1449 printf("b = %d\n", b);
1450 #endif
1453 void bitfield_test(void)
1455 int a;
1456 struct sbf1 {
1457 int f1 : 3;
1458 int : 2;
1459 int f2 : 1;
1460 int : 0;
1461 int f3 : 5;
1462 int f4 : 7;
1463 unsigned int f5 : 7;
1464 } st1;
1465 printf("bitfield_test:");
1466 printf("sizeof(st1) = %d\n", sizeof(st1));
1468 st1.f1 = 3;
1469 st1.f2 = 1;
1470 st1.f3 = 15;
1471 a = 120;
1472 st1.f4 = a;
1473 st1.f5 = a;
1474 st1.f5++;
1475 printf("%d %d %d %d %d\n",
1476 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1478 st1.f1 = 7;
1479 if (st1.f1 == -1)
1480 printf("st1.f1 == -1\n");
1481 else
1482 printf("st1.f1 != -1\n");
1483 if (st1.f2 == -1)
1484 printf("st1.f2 == -1\n");
1485 else
1486 printf("st1.f2 != -1\n");
1488 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1489 long-long bitfields whose size is not bigger than int */
1490 struct sbf2 {
1491 long long f1 : 45;
1492 long long : 2;
1493 long long f2 : 35;
1494 unsigned long long f3 : 38;
1495 } st2;
1496 st2.f1 = 0x123456789ULL;
1497 a = 120;
1498 st2.f2 = (long long)a << 25;
1499 st2.f3 = a;
1500 st2.f2++;
1501 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
1504 #ifdef __x86_64__
1505 #define FLOAT_FMT "%f\n"
1506 #else
1507 /* x86's float isn't compatible with GCC */
1508 #define FLOAT_FMT "%.5f\n"
1509 #endif
1511 /* declare strto* functions as they are C99 */
1512 double strtod(const char *nptr, char **endptr);
1513 float strtof(const char *nptr, char **endptr);
1514 long double strtold(const char *nptr, char **endptr);
1516 #define FTEST(prefix, type, fmt)\
1517 void prefix ## cmp(type a, type b)\
1519 printf("%d %d %d %d %d %d\n",\
1520 a == b,\
1521 a != b,\
1522 a < b,\
1523 a > b,\
1524 a >= b,\
1525 a <= b);\
1526 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1529 a + b,\
1530 a - b,\
1531 a * b,\
1532 a / b,\
1533 -a);\
1534 printf(fmt "\n", ++a);\
1535 printf(fmt "\n", a++);\
1536 printf(fmt "\n", a);\
1537 b = 0;\
1538 printf("%d %d\n", !a, !b);\
1540 void prefix ## fcast(type a)\
1542 float fa;\
1543 double da;\
1544 long double la;\
1545 int ia;\
1546 unsigned int ua;\
1547 type b;\
1548 fa = a;\
1549 da = a;\
1550 la = a;\
1551 printf("ftof: %f %f %Lf\n", fa, da, la);\
1552 ia = (int)a;\
1553 ua = (unsigned int)a;\
1554 printf("ftoi: %d %u\n", ia, ua);\
1555 ia = -1234;\
1556 ua = 0x81234500;\
1557 b = ia;\
1558 printf("itof: " fmt "\n", b);\
1559 b = ua;\
1560 printf("utof: " fmt "\n", b);\
1563 float prefix ## retf(type a) { return a; }\
1564 double prefix ## retd(type a) { return a; }\
1565 long double prefix ## retld(type a) { return a; }\
1567 void prefix ## call(void)\
1569 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1570 printf("double: %f\n", prefix ## retd(42.123456789));\
1571 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1572 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
1575 void prefix ## test(void)\
1577 printf("testing '%s'\n", #type);\
1578 prefix ## cmp(1, 2.5);\
1579 prefix ## cmp(2, 1.5);\
1580 prefix ## cmp(1, 1);\
1581 prefix ## fcast(234.6);\
1582 prefix ## fcast(-2334.6);\
1583 prefix ## call();\
1586 FTEST(f, float, "%f")
1587 FTEST(d, double, "%f")
1588 FTEST(ld, long double, "%Lf")
1590 double ftab1[3] = { 1.2, 3.4, -5.6 };
1593 void float_test(void)
1595 float fa, fb;
1596 double da, db;
1597 int a;
1598 unsigned int b;
1600 printf("float_test:\n");
1601 printf("sizeof(float) = %d\n", sizeof(float));
1602 printf("sizeof(double) = %d\n", sizeof(double));
1603 printf("sizeof(long double) = %d\n", sizeof(long double));
1604 ftest();
1605 dtest();
1606 ldtest();
1607 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1608 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1609 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1610 da = 123;
1611 printf("da=%f\n", da);
1612 fa = 123;
1613 printf("fa=%f\n", fa);
1614 a = 4000000000;
1615 da = a;
1616 printf("da = %f\n", da);
1617 b = 4000000000;
1618 db = b;
1619 printf("db = %f\n", db);
1622 int fib(int n)
1624 if (n <= 2)
1625 return 1;
1626 else
1627 return fib(n-1) + fib(n-2);
1630 void funcptr_test()
1632 void (*func)(int);
1633 int a;
1634 struct {
1635 int dummy;
1636 void (*func)(int);
1637 } st1;
1639 printf("funcptr:\n");
1640 func = &num;
1641 (*func)(12345);
1642 func = num;
1643 a = 1;
1644 a = 1;
1645 func(12345);
1646 /* more complicated pointer computation */
1647 st1.func = num;
1648 st1.func(12346);
1649 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1650 printf("sizeof2 = %d\n", sizeof funcptr_test);
1651 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1652 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1655 void lloptest(long long a, long long b)
1657 unsigned long long ua, ub;
1659 ua = a;
1660 ub = b;
1661 /* arith */
1662 printf("arith: %Ld %Ld %Ld\n",
1663 a + b,
1664 a - b,
1665 a * b);
1667 if (b != 0) {
1668 printf("arith1: %Ld %Ld\n",
1669 a / b,
1670 a % b);
1673 /* binary */
1674 printf("bin: %Ld %Ld %Ld\n",
1675 a & b,
1676 a | b,
1677 a ^ b);
1679 /* tests */
1680 printf("test: %d %d %d %d %d %d\n",
1681 a == b,
1682 a != b,
1683 a < b,
1684 a > b,
1685 a >= b,
1686 a <= b);
1688 printf("utest: %d %d %d %d %d %d\n",
1689 ua == ub,
1690 ua != ub,
1691 ua < ub,
1692 ua > ub,
1693 ua >= ub,
1694 ua <= ub);
1696 /* arith2 */
1697 a++;
1698 b++;
1699 printf("arith2: %Ld %Ld\n", a, b);
1700 printf("arith2: %Ld %Ld\n", a++, b++);
1701 printf("arith2: %Ld %Ld\n", --a, --b);
1702 printf("arith2: %Ld %Ld\n", a, b);
1703 b = ub = 0;
1704 printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
1707 void llshift(long long a, int b)
1709 printf("shift: %Ld %Ld %Ld\n",
1710 (unsigned long long)a >> b,
1711 a >> b,
1712 a << b);
1713 printf("shiftc: %Ld %Ld %Ld\n",
1714 (unsigned long long)a >> 3,
1715 a >> 3,
1716 a << 3);
1717 printf("shiftc: %Ld %Ld %Ld\n",
1718 (unsigned long long)a >> 35,
1719 a >> 35,
1720 a << 35);
1723 void llfloat(void)
1725 float fa;
1726 double da;
1727 long double lda;
1728 long long la, lb, lc;
1729 unsigned long long ula, ulb, ulc;
1730 la = 0x12345678;
1731 ula = 0x72345678;
1732 la = (la << 20) | 0x12345;
1733 ula = ula << 33;
1734 printf("la=%Ld ula=%Lu\n", la, ula);
1736 fa = la;
1737 da = la;
1738 lda = la;
1739 printf("lltof: %f %f %Lf\n", fa, da, lda);
1741 la = fa;
1742 lb = da;
1743 lc = lda;
1744 printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
1746 fa = ula;
1747 da = ula;
1748 lda = ula;
1749 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1751 ula = fa;
1752 ulb = da;
1753 ulc = lda;
1754 printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
1757 long long llfunc1(int a)
1759 return a * 2;
1762 struct S {
1763 int id;
1764 char item;
1767 long long int value(struct S *v)
1769 return ((long long int)v->item);
1772 void longlong_test(void)
1774 long long a, b, c;
1775 int ia;
1776 unsigned int ua;
1777 printf("longlong_test:\n");
1778 printf("sizeof(long long) = %d\n", sizeof(long long));
1779 ia = -1;
1780 ua = -2;
1781 a = ia;
1782 b = ua;
1783 printf("%Ld %Ld\n", a, b);
1784 printf("%Ld %Ld %Ld %Lx\n",
1785 (long long)1,
1786 (long long)-2,
1787 1LL,
1788 0x1234567812345679);
1789 a = llfunc1(-3);
1790 printf("%Ld\n", a);
1792 lloptest(1000, 23);
1793 lloptest(0xff, 0x1234);
1794 b = 0x72345678 << 10;
1795 lloptest(-3, b);
1796 llshift(0x123, 5);
1797 llshift(-23, 5);
1798 b = 0x72345678LL << 10;
1799 llshift(b, 47);
1801 llfloat();
1802 #if 1
1803 b = 0x12345678;
1804 a = -1;
1805 c = a + b;
1806 printf("%Lx\n", c);
1807 #endif
1809 /* long long reg spill test */
1811 struct S a;
1813 a.item = 3;
1814 printf("%lld\n", value(&a));
1816 lloptest(0x80000000, 0);
1818 /* another long long spill test */
1820 long long *p, v;
1821 v = 1;
1822 p = &v;
1823 p[0]++;
1824 printf("%lld\n", *p);
1827 a = 68719476720LL;
1828 b = 4294967295LL;
1829 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
1831 printf("%Ld\n", 0x123456789LLU);
1834 void manyarg_test(void)
1836 long double ld = 1234567891234LL;
1837 printf("manyarg_test:\n");
1838 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
1839 1, 2, 3, 4, 5, 6, 7, 8,
1840 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
1841 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1842 "%Ld %Ld %f %f\n",
1843 1, 2, 3, 4, 5, 6, 7, 8,
1844 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1845 1234567891234LL, 987654321986LL,
1846 42.0, 43.0);
1847 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1848 "%Ld %Ld %f %f\n",
1849 ld, 1, 2, 3, 4, 5, 6, 7, 8,
1850 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1851 1234567891234LL, 987654321986LL,
1852 42.0, 43.0);
1853 /* XXX: known bug of x86-64 */
1854 #ifndef __x86_64__
1855 printf("%d %d %d %d %d %d %d %d %Lf\n",
1856 1, 2, 3, 4, 5, 6, 7, 8, ld);
1857 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1858 "%Ld %Ld %f %f %Lf\n",
1859 1, 2, 3, 4, 5, 6, 7, 8,
1860 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1861 1234567891234LL, 987654321986LL,
1862 42.0, 43.0, ld);
1863 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1864 "%Lf %Ld %Ld %f %f %Lf\n",
1865 1, 2, 3, 4, 5, 6, 7, 8,
1866 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1867 ld, 1234567891234LL, 987654321986LL,
1868 42.0, 43.0, ld);
1869 #endif
1872 void vprintf1(const char *fmt, ...)
1874 va_list ap, aq;
1875 const char *p;
1876 int c, i;
1877 double d;
1878 long long ll;
1879 long double ld;
1881 va_start(aq, fmt);
1882 va_copy(ap, aq);
1884 p = fmt;
1885 for(;;) {
1886 c = *p;
1887 if (c == '\0')
1888 break;
1889 p++;
1890 if (c == '%') {
1891 c = *p;
1892 switch(c) {
1893 case '\0':
1894 goto the_end;
1895 case 'd':
1896 i = va_arg(ap, int);
1897 printf("%d", i);
1898 break;
1899 case 'f':
1900 d = va_arg(ap, double);
1901 printf("%f", d);
1902 break;
1903 case 'l':
1904 ll = va_arg(ap, long long);
1905 printf("%Ld", ll);
1906 break;
1907 case 'F':
1908 ld = va_arg(ap, long double);
1909 printf("%Lf", ld);
1910 break;
1912 p++;
1913 } else {
1914 putchar(c);
1917 the_end:
1918 va_end(aq);
1919 va_end(ap);
1922 struct myspace {
1923 short int profile;
1926 void stdarg_for_struct(struct myspace bob, ...)
1928 struct myspace george, bill;
1929 va_list ap;
1930 short int validate;
1932 va_start(ap, bob);
1933 bill = va_arg(ap, struct myspace);
1934 george = va_arg(ap, struct myspace);
1935 validate = va_arg(ap, int);
1936 printf("stdarg_for_struct: %d %d %d %d\n",
1937 bob.profile, bill.profile, george.profile, validate);
1938 va_end(ap);
1941 void stdarg_test(void)
1943 long double ld = 1234567891234LL;
1944 struct myspace bob;
1946 vprintf1("%d %d %d\n", 1, 2, 3);
1947 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1948 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1949 vprintf1("%F %F %F\n", 1.2L, 2.3L, 3.4L);
1950 #ifdef __x86_64__
1951 /* a bug of x86's TCC */
1952 vprintf1("%d %f %l %F %d %f %l %F\n",
1953 1, 1.2, 3L, 4.5L, 6, 7.8, 9L, 0.1L);
1954 #endif
1955 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
1956 1, 2, 3, 4, 5, 6, 7, 8,
1957 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
1958 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
1959 1, 2, 3, 4, 5, 6, 7, 8,
1960 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
1961 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1962 "%l %l %f %f\n",
1963 1, 2, 3, 4, 5, 6, 7, 8,
1964 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1965 1234567891234LL, 987654321986LL,
1966 42.0, 43.0);
1967 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1968 "%l %l %f %f\n",
1969 ld, 1, 2, 3, 4, 5, 6, 7, 8,
1970 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1971 1234567891234LL, 987654321986LL,
1972 42.0, 43.0);
1973 vprintf1("%d %d %d %d %d %d %d %d %F\n",
1974 1, 2, 3, 4, 5, 6, 7, 8, ld);
1975 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1976 "%l %l %f %f %F\n",
1977 1, 2, 3, 4, 5, 6, 7, 8,
1978 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1979 1234567891234LL, 987654321986LL,
1980 42.0, 43.0, ld);
1981 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
1982 "%F %l %l %f %f %F\n",
1983 1, 2, 3, 4, 5, 6, 7, 8,
1984 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
1985 ld, 1234567891234LL, 987654321986LL,
1986 42.0, 43.0, ld);
1988 bob.profile = 42;
1989 stdarg_for_struct(bob, bob, bob, bob.profile);
1992 void whitespace_test(void)
1994 char *str;
1996 \f\v #if 1
1997 pri\
1998 ntf("whitspace:\n");\f\v
1999 #endif
2000 pf("N=%d\n", 2);
2002 #ifdef CORRECT_CR_HANDLING
2003 pri\
2004 ntf("aaa=%d\n", 3);
2005 #endif
2007 pri\
2009 ntf("min=%d\n", 4);
2011 #ifdef ACCEPT_CR_IN_STRINGS
2012 printf("len1=%d\n", strlen("
2013 "));
2014 #ifdef CORRECT_CR_HANDLING
2015 str = "
2017 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
2018 #endif
2019 printf("len1=%d\n", strlen(" a
2020 "));
2021 #endif /* ACCEPT_CR_IN_STRINGS */
2024 int reltab[3] = { 1, 2, 3 };
2026 int *rel1 = &reltab[1];
2027 int *rel2 = &reltab[2];
2029 void relocation_test(void)
2031 printf("*rel1=%d\n", *rel1);
2032 printf("*rel2=%d\n", *rel2);
2035 void old_style_f(a,b,c)
2036 int a, b;
2037 double c;
2039 printf("a=%d b=%d b=%f\n", a, b, c);
2042 void decl_func1(int cmpfn())
2044 printf("cmpfn=%lx\n", (long)cmpfn);
2047 void decl_func2(cmpfn)
2048 int cmpfn();
2050 printf("cmpfn=%lx\n", (long)cmpfn);
2053 void old_style_function(void)
2055 old_style_f((void *)1, 2, 3.0);
2056 decl_func1(NULL);
2057 decl_func2(NULL);
2060 void alloca_test()
2062 #if defined __i386__ || defined __x86_64__
2063 char *p = alloca(16);
2064 strcpy(p,"123456789012345");
2065 printf("alloca: p is %s\n", p);
2066 char *demo = "This is only a test.\n";
2067 /* Test alloca embedded in a larger expression */
2068 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
2069 #endif
2072 void *bounds_checking_is_enabled()
2074 char ca[10], *cp = ca-1;
2075 return (ca != cp + 1) ? cp : NULL;
2078 void c99_vla_test(int size1, int size2)
2080 #if defined __i386__ || defined __x86_64__
2081 int size = size1 * size2;
2082 int tab1[size][2], tab2[10][2];
2083 void *tab1_ptr, *tab2_ptr, *bad_ptr;
2085 /* "size" should have been 'captured' at tab1 declaration,
2086 so modifying it should have no effect on VLA behaviour. */
2087 size = size-1;
2089 printf("Test C99 VLA 1 (sizeof): ");
2090 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
2091 tab1_ptr = tab1;
2092 tab2_ptr = tab2;
2093 printf("Test C99 VLA 2 (ptrs substract): ");
2094 printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2095 printf("Test C99 VLA 3 (ptr add): ");
2096 printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2097 printf("Test C99 VLA 4 (ptr access): ");
2098 tab1[size1][1] = 42;
2099 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2101 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2102 if (bad_ptr = bounds_checking_is_enabled()) {
2103 int *t1 = &tab1[size1 * size2 - 1][3];
2104 int *t2 = &tab2[9][3];
2105 printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
2106 printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
2108 char*c1 = 1 + sizeof(tab1) + (char*)tab1;
2109 char*c2 = 1 + sizeof(tab2) + (char*)tab2;
2110 printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
2111 printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
2113 int *i1 = tab1[-1];
2114 int *i2 = tab2[-1];
2115 printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
2116 printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
2118 int *x1 = tab1[size1 * size2 + 1];
2119 int *x2 = tab2[10 + 1];
2120 printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
2121 printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
2122 } else {
2123 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2125 printf("\n");
2126 #endif
2129 void sizeof_test(void)
2131 int a;
2132 int **ptr;
2134 printf("sizeof(int) = %d\n", sizeof(int));
2135 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2136 printf("sizeof(long) = %d\n", sizeof(long));
2137 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2138 printf("sizeof(short) = %d\n", sizeof(short));
2139 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2140 printf("sizeof(char) = %d\n", sizeof(char));
2141 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2142 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2143 a = 1;
2144 printf("sizeof(a++) = %d\n", sizeof a++);
2145 printf("a=%d\n", a);
2146 ptr = NULL;
2147 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
2149 /* some alignof tests */
2150 printf("__alignof__(int) = %d\n", __alignof__(int));
2151 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2152 printf("__alignof__(short) = %d\n", __alignof__(short));
2153 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2154 printf("__alignof__(char) = %d\n", __alignof__(char));
2155 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2156 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
2159 void typeof_test(void)
2161 double a;
2162 typeof(a) b;
2163 typeof(float) c;
2165 a = 1.5;
2166 b = 2.5;
2167 c = 3.5;
2168 printf("a=%f b=%f c=%f\n", a, b, c);
2171 void statement_expr_test(void)
2173 int a, i;
2175 a = 0;
2176 for(i=0;i<10;i++) {
2177 a += 1 +
2178 ( { int b, j;
2179 b = 0;
2180 for(j=0;j<5;j++)
2181 b += j; b;
2182 } );
2184 printf("a=%d\n", a);
2188 void local_label_test(void)
2190 int a;
2191 goto l1;
2193 a = 1 + ({
2194 __label__ l1, l2, l3, l4;
2195 goto l1;
2197 printf("aa1\n");
2198 goto l3;
2200 printf("aa3\n");
2201 goto l4;
2203 printf("aa2\n");
2204 goto l2;
2205 l3:;
2208 printf("a=%d\n", a);
2209 return;
2211 printf("bb1\n");
2212 goto l2;
2214 printf("bb2\n");
2215 goto l4;
2218 /* inline assembler test */
2219 #ifdef __i386__
2221 /* from linux kernel */
2222 static char * strncat1(char * dest,const char * src,size_t count)
2224 int d0, d1, d2, d3;
2225 __asm__ __volatile__(
2226 "repne\n\t"
2227 "scasb\n\t"
2228 "decl %1\n\t"
2229 "movl %8,%3\n"
2230 "1:\tdecl %3\n\t"
2231 "js 2f\n\t"
2232 "lodsb\n\t"
2233 "stosb\n\t"
2234 "testb %%al,%%al\n\t"
2235 "jne 1b\n"
2236 "2:\txorl %2,%2\n\t"
2237 "stosb"
2238 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2239 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2240 : "memory");
2241 return dest;
2244 static char * strncat2(char * dest,const char * src,size_t count)
2246 int d0, d1, d2, d3;
2247 __asm__ __volatile__(
2248 "repne scasb\n\t" /* one-line repne prefix + string op */
2249 "decl %1\n\t"
2250 "movl %8,%3\n"
2251 "1:\tdecl %3\n\t"
2252 "js 2f\n\t"
2253 "lodsb\n\t"
2254 "stosb\n\t"
2255 "testb %%al,%%al\n\t"
2256 "jne 1b\n"
2257 "2:\txorl %2,%2\n\t"
2258 "stosb"
2259 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2260 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2261 : "memory");
2262 return dest;
2265 static inline void * memcpy1(void * to, const void * from, size_t n)
2267 int d0, d1, d2;
2268 __asm__ __volatile__(
2269 "rep ; movsl\n\t"
2270 "testb $2,%b4\n\t"
2271 "je 1f\n\t"
2272 "movsw\n"
2273 "1:\ttestb $1,%b4\n\t"
2274 "je 2f\n\t"
2275 "movsb\n"
2276 "2:"
2277 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2278 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2279 : "memory");
2280 return (to);
2283 static inline void * memcpy2(void * to, const void * from, size_t n)
2285 int d0, d1, d2;
2286 __asm__ __volatile__(
2287 "rep movsl\n\t" /* one-line rep prefix + string op */
2288 "testb $2,%b4\n\t"
2289 "je 1f\n\t"
2290 "movsw\n"
2291 "1:\ttestb $1,%b4\n\t"
2292 "je 2f\n\t"
2293 "movsb\n"
2294 "2:"
2295 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2296 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2297 : "memory");
2298 return (to);
2301 static __inline__ void sigaddset1(unsigned int *set, int _sig)
2303 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2306 static __inline__ void sigdelset1(unsigned int *set, int _sig)
2308 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2311 static __inline__ __const__ unsigned int swab32(unsigned int x)
2313 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2314 "rorl $16,%0\n\t" /* swap words */
2315 "xchgb %b0,%h0" /* swap higher bytes */
2316 :"=q" (x)
2317 : "0" (x));
2318 return x;
2321 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
2323 unsigned long long res;
2324 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
2325 return res;
2328 static __inline__ unsigned long long inc64(unsigned long long a)
2330 unsigned long long res;
2331 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
2332 return res;
2335 unsigned int set;
2337 void asm_test(void)
2339 char buf[128];
2340 unsigned int val;
2342 printf("inline asm:\n");
2343 /* test the no operand case */
2344 asm volatile ("xorl %eax, %eax");
2346 memcpy1(buf, "hello", 6);
2347 strncat1(buf, " worldXXXXX", 3);
2348 printf("%s\n", buf);
2350 memcpy2(buf, "hello", 6);
2351 strncat2(buf, " worldXXXXX", 3);
2352 printf("%s\n", buf);
2354 /* 'A' constraint test */
2355 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2356 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2358 set = 0xff;
2359 sigdelset1(&set, 2);
2360 sigaddset1(&set, 16);
2361 /* NOTE: we test here if C labels are correctly restored after the
2362 asm statement */
2363 goto label1;
2364 label2:
2365 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
2366 #ifdef __GNUC__ // works strange with GCC 4.3
2367 set=0x1080fd;
2368 #endif
2369 printf("set=0x%x\n", set);
2370 val = 0x01020304;
2371 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
2372 return;
2373 label1:
2374 goto label2;
2377 #else
2379 void asm_test(void)
2383 #endif
2385 #define COMPAT_TYPE(type1, type2) \
2387 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2388 __builtin_types_compatible_p (type1, type2));\
2391 int constant_p_var;
2393 void builtin_test(void)
2395 #if GCC_MAJOR >= 3
2396 COMPAT_TYPE(int, int);
2397 COMPAT_TYPE(int, unsigned int);
2398 COMPAT_TYPE(int, char);
2399 COMPAT_TYPE(int, const int);
2400 COMPAT_TYPE(int, volatile int);
2401 COMPAT_TYPE(int *, int *);
2402 COMPAT_TYPE(int *, void *);
2403 COMPAT_TYPE(int *, const int *);
2404 COMPAT_TYPE(char *, unsigned char *);
2405 /* space is needed because tcc preprocessor introduces a space between each token */
2406 COMPAT_TYPE(char * *, void *);
2407 #endif
2408 printf("res = %d\n", __builtin_constant_p(1));
2409 printf("res = %d\n", __builtin_constant_p(1 + 2));
2410 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
2411 printf("res = %d\n", __builtin_constant_p(constant_p_var));
2415 extern int __attribute__((weak)) weak_f1(void);
2416 extern int __attribute__((weak)) weak_f2(void);
2417 extern int weak_f3(void);
2418 extern int __attribute__((weak)) weak_v1;
2419 extern int __attribute__((weak)) weak_v2;
2420 extern int weak_v3;
2422 extern int (*weak_fpa)() __attribute__((weak));
2423 extern int __attribute__((weak)) (*weak_fpb)();
2424 extern __attribute__((weak)) int (*weak_fpc)();
2426 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
2427 extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
2428 extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
2429 extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
2430 extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
2431 extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
2433 static const size_t dummy = 0;
2434 extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
2435 extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
2436 extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
2438 int some_lib_func(void);
2439 int dummy_impl_of_slf(void) { return 444; }
2440 int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
2442 int weak_toolate() { return 0; }
2443 int weak_toolate() __attribute__((weak));
2445 void __attribute__((weak)) weak_test(void)
2447 printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
2448 printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
2449 printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
2450 printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
2451 printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
2452 printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
2454 printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
2455 printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
2456 printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
2458 printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
2459 printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
2460 printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
2461 printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
2462 printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
2463 printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
2466 int __attribute__((weak)) weak_f2() { return 222; }
2467 int __attribute__((weak)) weak_f3() { return 333; }
2468 int __attribute__((weak)) weak_v2 = 222;
2469 int __attribute__((weak)) weak_v3 = 333;
2471 void const_func(const int a)
2475 void const_warn_test(void)
2477 const_func(1);