Fix sub-int returns on x86-64 and i386
[tinycc.git] / tests / tcctest.c
blob940e7d0778ee78a53bcb64e130fa67dad1fbd08d
1 /*
2 * TCC auto test program
3 */
4 #include "config.h"
6 #if GCC_MAJOR >= 3
8 /* Unfortunately, gcc version < 3 does not handle that! */
9 #define ALL_ISOC99
11 /* only gcc 3 handles _Bool correctly */
12 #define BOOL_ISOC99
14 /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
15 #define CORRECT_CR_HANDLING
17 #endif
19 #if defined(_WIN32)
20 #define LONG_LONG_FORMAT "%lld"
21 #define ULONG_LONG_FORMAT "%llu"
22 #else
23 #define LONG_LONG_FORMAT "%Ld"
24 #define ULONG_LONG_FORMAT "%Lu"
25 #endif
27 // MinGW has 80-bit rather than 64-bit long double which isn't compatible with TCC or MSVC
28 #if defined(_WIN32) && defined(__GNUC__)
29 #define LONG_DOUBLE double
30 #define LONG_DOUBLE_LITERAL(x) x
31 #else
32 #define LONG_DOUBLE long double
33 #define LONG_DOUBLE_LITERAL(x) x ## L
34 #endif
36 /* deprecated and no longer supported in gcc 3.3 */
37 //#define ACCEPT_CR_IN_STRINGS
39 /* __VA_ARGS__ and __func__ support */
40 #define C99_MACROS
42 /* test various include syntaxes */
44 #define TCCLIB_INC <tcclib.h>
45 #define TCCLIB_INC1 <tcclib
46 #define TCCLIB_INC2 h>
47 #define TCCLIB_INC3 "tcclib.h"
49 #include TCCLIB_INC
51 #include TCCLIB_INC1.TCCLIB_INC2
53 #include TCCLIB_INC1.h>
55 #include TCCLIB_INC3
57 #include <tcclib.h>
59 #include "tcclib.h"
61 #include "tcctest.h"
63 /* Test two more ways to include a file named like a pp-number */
64 #define INC(name) <tests/name.h>
65 #define funnyname 42test.h
66 #define incdir tests/
67 #define incname < incdir funnyname >
68 #define __stringify(x) #x
69 #define stringify(x) __stringify(x)
70 #include INC(42test)
71 #include incname
72 #include stringify(funnyname)
74 void intdiv_test();
75 void string_test();
76 void expr_test();
77 void macro_test();
78 void recursive_macro_test();
79 void scope_test();
80 void forward_test();
81 void funcptr_test();
82 void loop_test();
83 void switch_test();
84 void goto_test();
85 void enum_test();
86 void typedef_test();
87 void struct_test();
88 void array_test();
89 void expr_ptr_test();
90 void bool_test();
91 void optimize_out();
92 void expr2_test();
93 void constant_expr_test();
94 void expr_cmp_test();
95 void char_short_test();
96 void init_test(void);
97 void compound_literal_test(void);
98 int kr_test();
99 void struct_assign_test(void);
100 void cast_test(void);
101 void bitfield_test(void);
102 void c99_bool_test(void);
103 void float_test(void);
104 void longlong_test(void);
105 void manyarg_test(void);
106 void stdarg_test(void);
107 void whitespace_test(void);
108 void relocation_test(void);
109 void old_style_function(void);
110 void alloca_test(void);
111 void c99_vla_test(int size1, int size2);
112 void sizeof_test(void);
113 void typeof_test(void);
114 void local_label_test(void);
115 void statement_expr_test(void);
116 void asm_test(void);
117 void builtin_test(void);
118 void weak_test(void);
119 void global_data_test(void);
120 void cmp_comparison_test(void);
121 void math_cmp_test(void);
122 void callsave_test(void);
123 void builtin_frame_address_test(void);
124 void attrib_test(void);
126 int fib(int n);
127 void num(int n);
128 void forward_ref(void);
129 int isid(int c);
131 /* Line joining happens before tokenization, so the following
132 must be parsed as ellipsis. */
133 void funny_line_continuation (int, ..\
134 . );
136 char via_volatile (char);
138 #define A 2
139 #define N 1234 + A
140 #define pf printf
141 #define M1(a, b) (a) + (b)
143 #define str\
144 (s) # s
145 #define glue(a, b) a ## b
146 #define xglue(a, b) glue(a, b)
147 #define HIGHLOW "hello"
148 #define LOW LOW ", world"
150 static int onetwothree = 123;
151 #define onetwothree4 onetwothree
152 #define onetwothree xglue(onetwothree,4)
154 #define min(a, b) ((a) < (b) ? (a) : (b))
156 #ifdef C99_MACROS
157 #define dprintf(level,...) printf(__VA_ARGS__)
158 #endif
160 /* gcc vararg macros */
161 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
163 #define MACRO_NOARGS()
165 #define AAA 3
166 #undef AAA
167 #define AAA 4
169 #if 1
170 #define B3 1
171 #elif 1
172 #define B3 2
173 #elif 0
174 #define B3 3
175 #else
176 #define B3 4
177 #endif
179 #ifdef __TINYC__
180 /* We try to handle this syntax. Make at least sure it doesn't segfault. */
181 char invalid_function_def()[] {}
182 #endif
184 #define __INT64_C(c) c ## LL
185 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
187 int qq(int x)
189 return x + 40;
191 #define qq(x) x
193 #define spin_lock(lock) do { } while (0)
194 #define wq_spin_lock spin_lock
195 #define TEST2() wq_spin_lock(a)
197 #define UINT_MAX ((unsigned) -1)
199 void intdiv_test(void)
201 printf("18/21=%u\n", 18/21);
202 printf("18%%21=%u\n", 18%21);
203 printf("41/21=%u\n", 41/21);
204 printf("41%%21=%u\n", 41%21);
205 printf("42/21=%u\n", 42/21);
206 printf("42%%21=%u\n", 42%21);
207 printf("43/21=%u\n", 43/21);
208 printf("43%%21=%u\n", 43%21);
209 printf("126/21=%u\n", 126/21);
210 printf("126%%21=%u\n", 126%21);
211 printf("131/21=%u\n", 131/21);
212 printf("131%%21=%u\n", 131%21);
213 printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX/2+3)/2);
214 printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX/2+3)%2);
216 printf("18/-21=%u\n", 18/-21);
217 printf("18%%-21=%u\n", 18%-21);
218 printf("41/-21=%u\n", 41/-21);
219 printf("41%%-21=%u\n", 41%-21);
220 printf("42/-21=%u\n", 42/-21);
221 printf("42%%-21=%u\n", 42%-21);
222 printf("43/-21=%u\n", 43/-21);
223 printf("43%%-21=%u\n", 43%-21);
224 printf("126/-21=%u\n", 126/-21);
225 printf("126%%-21=%u\n", 126%-21);
226 printf("131/-21=%u\n", 131/-21);
227 printf("131%%-21=%u\n", 131%-21);
228 printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX/2+3)/-2);
229 printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX/2+3)%-2);
231 printf("-18/21=%u\n", -18/21);
232 printf("-18%%21=%u\n", -18%21);
233 printf("-41/21=%u\n", -41/21);
234 printf("-41%%21=%u\n", -41%21);
235 printf("-42/21=%u\n", -42/21);
236 printf("-42%%21=%u\n", -42%21);
237 printf("-43/21=%u\n", -43/21);
238 printf("-43%%21=%u\n", -43%21);
239 printf("-126/21=%u\n", -126/21);
240 printf("-126%%21=%u\n", -126%21);
241 printf("-131/21=%u\n", -131/21);
242 printf("-131%%21=%u\n", -131%21);
243 printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX/2+3))/2);
244 printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX/2+3))%2);
246 printf("-18/-21=%u\n", -18/-21);
247 printf("-18%%-21=%u\n", -18%-21);
248 printf("-41/-21=%u\n", -41/-21);
249 printf("-41%%-21=%u\n", -41%-21);
250 printf("-42/-21=%u\n", -42/-21);
251 printf("-42%%-21=%u\n", -42%-21);
252 printf("-43/-21=%u\n", -43/-21);
253 printf("-43%%-21=%u\n", -43%-21);
254 printf("-126/-21=%u\n", -126/-21);
255 printf("-126%%-21=%u\n", -126%-21);
256 printf("-131/-21=%u\n", -131/-21);
257 printf("-131%%-21=%u\n", -131%-21);
258 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX/2+3))/-2);
259 printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX/2+3))%-2);
262 void macro_test(void)
264 printf("macro:\n");\f\v
265 pf("N=%d\n", N);
266 printf("aaa=%d\n", AAA);
268 printf("min=%d\n", min(1, min(2, -1)));
270 printf("s1=%s\n", glue(HIGH, LOW));
271 printf("s2=%s\n", xglue(HIGH, LOW));
272 printf("s3=%s\n", str("c"));
273 printf("s4=%s\n", str(a1));
274 printf("B3=%d\n", B3);
276 printf("onetwothree=%d\n", onetwothree);
278 #ifdef A
279 printf("A defined\n");
280 #endif
281 #ifdef B
282 printf("B defined\n");
283 #endif
284 #ifdef A
285 printf("A defined\n");
286 #else
287 printf("A not defined\n");
288 #endif
289 #ifdef B
290 printf("B defined\n");
291 #else
292 printf("B not defined\n");
293 #endif
295 #ifdef A
296 printf("A defined\n");
297 #ifdef B
298 printf("B1 defined\n");
299 #else
300 printf("B1 not defined\n");
301 #endif
302 #else
303 printf("A not defined\n");
304 #ifdef B
305 printf("B2 defined\n");
306 #else
307 printf("B2 not defined\n");
308 #endif
309 #endif
311 #if 1+1
312 printf("test true1\n");
313 #endif
314 #if 0
315 printf("test true2\n");
316 #endif
317 #if 1-1
318 printf("test true3\n");
319 #endif
320 #if defined(A)
321 printf("test trueA\n");
322 #endif
323 #if defined(B)
324 printf("test trueB\n");
325 #endif
327 #if 0
328 printf("test 0\n");
329 #elif 0
330 printf("test 1\n");
331 #elif 2
332 printf("test 2\n");
333 #else
334 printf("test 3\n");
335 #endif
337 MACRO_NOARGS();
339 #ifdef __LINE__
340 printf("__LINE__ defined\n");
341 #endif
343 printf("__LINE__=%d __FILE__=%s\n",
344 __LINE__, __FILE__);
345 #if 0
346 #line 200
347 printf("__LINE__=%d __FILE__=%s\n",
348 __LINE__, __FILE__);
349 #line 203 "test"
350 printf("__LINE__=%d __FILE__=%s\n",
351 __LINE__, __FILE__);
352 #line 227 "tcctest.c"
353 #endif
355 /* not strictly preprocessor, but we test it there */
356 #ifdef C99_MACROS
357 printf("__func__ = %s\n", __func__);
358 dprintf(1, "vaarg=%d\n", 1);
359 #endif
360 dprintf1(1, "vaarg1\n");
361 dprintf1(1, "vaarg1=%d\n", 2);
362 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
364 /* gcc extension */
365 printf("func='%s'\n", __FUNCTION__);
367 /* complicated macros in glibc */
368 printf("INT64_MIN=" LONG_LONG_FORMAT "\n", INT64_MIN);
370 int a;
371 a = 1;
372 glue(a+, +);
373 printf("a=%d\n", a);
374 glue(a <, <= 2);
375 printf("a=%d\n", a);
378 /* macro function with argument outside the macro string */
379 #define MF_s MF_hello
380 #define MF_hello(msg) printf("%s\n",msg)
382 #define MF_t printf("tralala\n"); MF_hello
384 MF_s("hi");
385 MF_t("hi");
387 /* test macro substitution inside args (should not eat stream) */
388 printf("qq=%d\n", qq(qq)(2));
390 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
391 null argument without a space. gcc 3.2 fixes that. */
393 #define qq1(x) 1
394 printf("qq1=%d\n", qq1( ));
396 /* comment with stray handling *\
398 /* this is a valid *\/ comment */
399 /* this is a valid comment *\*/
400 // this is a valid\
401 comment
403 /* test function macro substitution when the function name is
404 substituted */
405 TEST2();
407 /* And again when the name and parentheses are separated by a
408 comment. */
409 TEST2 /* the comment */ ();
411 printf("%s\n", get_basefile_from_header());
412 printf("%s\n", __BASE_FILE__);
413 printf("%s\n", get_file_from_header());
414 printf("%s\n", __FILE__);
416 /* Check that funnily named include was in fact included */
417 have_included_42test_h = 1;
418 have_included_42test_h_second = 1;
419 have_included_42test_h_third = 1;
423 static void print_num(char *fn, int line, int num) {
424 printf("fn %s, line %d, num %d\n", fn, line, num);
427 void recursive_macro_test(void)
430 #define ELF32_ST_TYPE(val) ((val) & 0xf)
431 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
432 #define STB_WEAK 2 /* Weak symbol */
433 #define ELFW(type) ELF##32##_##type
434 printf("%d\n", ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(123)));
436 #define WRAP(x) x
438 #define print_num(x) print_num(__FILE__,__LINE__,x)
439 print_num(123);
440 WRAP(print_num(123));
441 WRAP(WRAP(print_num(123)));
443 static struct recursive_macro { int rm_field; } G;
444 #define rm_field (G.rm_field)
445 printf("rm_field = %d\n", rm_field);
446 printf("rm_field = %d\n", WRAP(rm_field));
447 WRAP((printf("rm_field = %d %d\n", rm_field, WRAP(rm_field))));
450 int op(a,b)
452 return a / b;
455 int ret(a)
457 if (a == 2)
458 return 1;
459 if (a == 3)
460 return 2;
461 return 0;
464 void ps(const char *s)
466 int c;
467 while (1) {
468 c = *s;
469 if (c == 0)
470 break;
471 printf("%c", c);
472 s++;
476 const char foo1_string[] = "\
477 bar\n\
478 test\14\
481 void string_test()
483 unsigned int b;
484 printf("string:\n");
485 printf("\141\1423\143\n");/* dezdez test */
486 printf("\x41\x42\x43\x3a\n");
487 printf("c=%c\n", 'r');
488 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
489 printf("foo1_string='%s'\n", foo1_string);
490 #if 0
491 printf("wstring=%S\n", L"abc");
492 printf("wstring=%S\n", L"abc" L"def" "ghi");
493 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
494 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
495 #endif
496 ps("test\n");
497 b = 32;
498 while ((b = b + 1) < 96) {
499 printf("%c", b);
501 printf("\n");
502 printf("fib=%d\n", fib(33));
503 b = 262144;
504 while (b != 0x80000000) {
505 num(b);
506 b = b * 2;
510 void loop_test()
512 int i;
513 i = 0;
514 while (i < 10)
515 printf("%d", i++);
516 printf("\n");
517 for(i = 0; i < 10;i++)
518 printf("%d", i);
519 printf("\n");
520 i = 0;
521 do {
522 printf("%d", i++);
523 } while (i < 10);
524 printf("\n");
526 char count = 123;
527 /* c99 for loop init test */
528 for (size_t count = 1; count < 3; count++)
529 printf("count=%d\n", count);
530 printf("count = %d\n", count);
532 /* break/continue tests */
533 i = 0;
534 while (1) {
535 if (i == 6)
536 break;
537 i++;
538 if (i == 3)
539 continue;
540 printf("%d", i);
542 printf("\n");
544 /* break/continue tests */
545 i = 0;
546 do {
547 if (i == 6)
548 break;
549 i++;
550 if (i == 3)
551 continue;
552 printf("%d", i);
553 } while(1);
554 printf("\n");
556 for(i = 0;i < 10;i++) {
557 if (i == 3)
558 continue;
559 printf("%d", i);
561 printf("\n");
564 typedef int typedef_and_label;
566 void goto_test()
568 int i;
569 static void *label_table[3] = { &&label1, &&label2, &&label3 };
571 printf("goto:\n");
572 i = 0;
573 /* This needs to parse as label, not as start of decl. */
574 typedef_and_label:
575 s_loop:
576 if (i >= 10)
577 goto s_end;
578 printf("%d", i);
579 i++;
580 goto s_loop;
581 s_end:
582 printf("\n");
584 /* we also test computed gotos (GCC extension) */
585 for(i=0;i<3;i++) {
586 goto *label_table[i];
587 label1:
588 printf("label1\n");
589 goto next;
590 label2:
591 printf("label2\n");
592 goto next;
593 label3:
594 printf("label3\n");
595 next: ;
599 enum {
601 E1 = 2,
602 E2 = 4,
607 enum test {
608 E5 = 1000,
611 struct S_enum {
612 enum {E6 = 42, E7, E8} e:8;
615 enum ELong {
616 /* This is either 0 on L32 machines, or a large number
617 on L64 machines. We should be able to store this. */
618 EL_large = ((unsigned long)0xf000 << 31) << 1,
621 enum { BIASU = -1U<<31 };
622 enum { BIASS = -1 << 31 };
624 static int getint(int i)
626 if (i)
627 return 0;
628 else
629 return (int)(-1U << 31);
632 void enum_test()
634 enum test b1;
635 /* The following should give no warning */
636 unsigned *p = &b1;
637 struct S_enum s = {E7};
638 printf("enum: %d\n", s.e);
639 printf("enum:\n%d %d %d %d %d %d\n",
640 E0, E1, E2, E3, E4, E5);
641 b1 = 1;
642 printf("b1=%d\n", b1);
643 printf("enum large: %ld\n", EL_large);
645 if (getint(0) == BIASU)
646 printf("enum unsigned: ok\n");
647 else
648 printf("enum unsigned: wrong\n");
649 if (getint(0) == BIASS)
650 printf("enum unsigned: ok\n");
651 else
652 printf("enum unsigned: wrong\n");
655 typedef int *my_ptr;
657 typedef int mytype1;
658 typedef int mytype2;
660 void typedef_test()
662 my_ptr a;
663 mytype1 mytype2;
664 int b;
666 a = &b;
667 *a = 1234;
668 printf("typedef:\n");
669 printf("a=%d\n", *a);
670 mytype2 = 2;
671 printf("mytype2=%d\n", mytype2);
674 void forward_test()
676 printf("forward:\n");
677 forward_ref();
678 forward_ref();
682 void forward_ref(void)
684 printf("forward ok\n");
687 typedef struct struct1 {
688 int f1;
689 int f2, f3;
690 union union1 {
691 int v1;
692 int v2;
693 } u;
694 char str[3];
695 } struct1;
697 struct struct2 {
698 int a;
699 char b;
702 union union2 {
703 int w1;
704 int w2;
707 struct struct1 st1, st2;
709 struct empty_mem {
710 /* nothing */ ;
711 int x;
714 int main(int argc, char **argv)
716 string_test();
717 expr_test();
718 macro_test();
719 recursive_macro_test();
720 scope_test();
721 forward_test();
722 funcptr_test();
723 loop_test();
724 switch_test();
725 goto_test();
726 enum_test();
727 typedef_test();
728 struct_test();
729 array_test();
730 expr_ptr_test();
731 bool_test();
732 optimize_out();
733 expr2_test();
734 constant_expr_test();
735 expr_cmp_test();
736 char_short_test();
737 init_test();
738 compound_literal_test();
739 kr_test();
740 struct_assign_test();
741 cast_test();
742 bitfield_test();
743 c99_bool_test();
744 float_test();
745 longlong_test();
746 manyarg_test();
747 stdarg_test();
748 whitespace_test();
749 relocation_test();
750 old_style_function();
751 alloca_test();
752 c99_vla_test(5, 2);
753 sizeof_test();
754 typeof_test();
755 statement_expr_test();
756 local_label_test();
757 asm_test();
758 builtin_test();
759 #ifndef _WIN32
760 weak_test();
761 #endif
762 global_data_test();
763 cmp_comparison_test();
764 math_cmp_test();
765 callsave_test();
766 builtin_frame_address_test();
767 intdiv_test();
768 if (via_volatile (42) != 42)
769 printf ("via_volatile broken\n");
770 attrib_test();
771 return 0;
774 int tab[3];
775 int tab2[3][2];
777 int g;
779 void f1(g)
781 printf("g1=%d\n", g);
784 void scope_test()
786 printf("scope:\n");
787 g = 2;
788 f1(1);
789 printf("g2=%d\n", g);
791 int g;
792 g = 3;
793 printf("g3=%d\n", g);
795 int g;
796 g = 4;
797 printf("g4=%d\n", g);
800 printf("g5=%d\n", g);
803 void array_test()
805 int i, j, a[4];
807 printf("array:\n");
808 printf("sizeof(a) = %d\n", sizeof(a));
809 printf("sizeof(\"a\") = %d\n", sizeof("a"));
810 #ifdef C99_MACROS
811 printf("sizeof(__func__) = %d\n", sizeof(__func__));
812 #endif
813 printf("sizeof tab %d\n", sizeof(tab));
814 printf("sizeof tab2 %d\n", sizeof tab2);
815 tab[0] = 1;
816 tab[1] = 2;
817 tab[2] = 3;
818 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
819 for(i=0;i<3;i++)
820 for(j=0;j<2;j++)
821 tab2[i][j] = 10 * i + j;
822 for(i=0;i<3*2;i++) {
823 printf(" %3d", ((int *)tab2)[i]);
825 printf("\n");
826 printf("sizeof(size_t)=%d\n", sizeof(size_t));
827 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
830 void expr_test()
832 int a, b;
833 a = 0;
834 printf("%d\n", a += 1);
835 printf("%d\n", a -= 2);
836 printf("%d\n", a *= 31232132);
837 printf("%d\n", a /= 4);
838 printf("%d\n", a %= 20);
839 printf("%d\n", a &= 6);
840 printf("%d\n", a ^= 7);
841 printf("%d\n", a |= 8);
842 printf("%d\n", a >>= 3);
843 printf("%d\n", a <<= 4);
845 a = 22321;
846 b = -22321;
847 printf("%d\n", a + 1);
848 printf("%d\n", a - 2);
849 printf("%d\n", a * 312);
850 printf("%d\n", a / 4);
851 printf("%d\n", b / 4);
852 printf("%d\n", (unsigned)b / 4);
853 printf("%d\n", a % 20);
854 printf("%d\n", b % 20);
855 printf("%d\n", (unsigned)b % 20);
856 printf("%d\n", a & 6);
857 printf("%d\n", a ^ 7);
858 printf("%d\n", a | 8);
859 printf("%d\n", a >> 3);
860 printf("%d\n", b >> 3);
861 printf("%d\n", (unsigned)b >> 3);
862 printf("%d\n", a << 4);
863 printf("%d\n", ~a);
864 printf("%d\n", -a);
865 printf("%d\n", +a);
867 printf("%d\n", 12 + 1);
868 printf("%d\n", 12 - 2);
869 printf("%d\n", 12 * 312);
870 printf("%d\n", 12 / 4);
871 printf("%d\n", 12 % 20);
872 printf("%d\n", 12 & 6);
873 printf("%d\n", 12 ^ 7);
874 printf("%d\n", 12 | 8);
875 printf("%d\n", 12 >> 2);
876 printf("%d\n", 12 << 4);
877 printf("%d\n", ~12);
878 printf("%d\n", -12);
879 printf("%d\n", +12);
880 printf("%d %d %d %d\n",
881 isid('a'),
882 isid('g'),
883 isid('T'),
884 isid('('));
887 int isid(int c)
889 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
892 /**********************/
894 int vstack[10], *vstack_ptr;
896 void vpush(int vt, int vc)
898 *vstack_ptr++ = vt;
899 *vstack_ptr++ = vc;
902 void vpop(int *ft, int *fc)
904 *fc = *--vstack_ptr;
905 *ft = *--vstack_ptr;
908 void expr2_test()
910 int a, b;
912 printf("expr2:\n");
913 vstack_ptr = vstack;
914 vpush(1432432, 2);
915 vstack_ptr[-2] &= ~0xffffff80;
916 vpop(&a, &b);
917 printf("res= %d %d\n", a, b);
920 void constant_expr_test()
922 int a;
923 printf("constant_expr:\n");
924 a = 3;
925 printf("%d\n", a * 16);
926 printf("%d\n", a * 1);
927 printf("%d\n", a + 0);
930 int tab4[10];
932 void expr_ptr_test()
934 int *p, *q;
935 int i = -1;
937 printf("expr_ptr:\n");
938 p = tab4;
939 q = tab4 + 10;
940 printf("diff=%d\n", q - p);
941 p++;
942 printf("inc=%d\n", p - tab4);
943 p--;
944 printf("dec=%d\n", p - tab4);
945 ++p;
946 printf("inc=%d\n", p - tab4);
947 --p;
948 printf("dec=%d\n", p - tab4);
949 printf("add=%d\n", p + 3 - tab4);
950 printf("add=%d\n", 3 + p - tab4);
952 /* check if 64bit support is ok */
953 q = p = 0;
954 q += i;
955 printf("%p %p %ld\n", q, p, p-q);
956 printf("%d %d %d %d %d %d\n",
957 p == q, p != q, p < q, p <= q, p >= q, p > q);
958 i = 0xf0000000;
959 p += i;
960 printf("%p %p %ld\n", q, p, p-q);
961 printf("%d %d %d %d %d %d\n",
962 p == q, p != q, p < q, p <= q, p >= q, p > q);
963 p = (int *)((char *)p + 0xf0000000);
964 printf("%p %p %ld\n", q, p, p-q);
965 printf("%d %d %d %d %d %d\n",
966 p == q, p != q, p < q, p <= q, p >= q, p > q);
967 p += 0xf0000000;
968 printf("%p %p %ld\n", q, p, p-q);
969 printf("%d %d %d %d %d %d\n",
970 p == q, p != q, p < q, p <= q, p >= q, p > q);
972 struct size12 {
973 int i, j, k;
975 struct size12 s[2], *sp = s;
976 int i, j;
977 sp->i = 42;
978 sp++;
979 j = -1;
980 printf("%d\n", sp[j].i);
982 #ifdef __LP64__
983 i = 1;
984 p = (int*)0x100000000UL + i;
985 i = ((long)p) >> 32;
986 printf("largeptr: %p %d\n", p, i);
987 #endif
990 void expr_cmp_test()
992 int a, b;
993 printf("constant_expr:\n");
994 a = -1;
995 b = 1;
996 printf("%d\n", a == a);
997 printf("%d\n", a != a);
999 printf("%d\n", a < b);
1000 printf("%d\n", a <= b);
1001 printf("%d\n", a <= a);
1002 printf("%d\n", b >= a);
1003 printf("%d\n", a >= a);
1004 printf("%d\n", b > a);
1006 printf("%d\n", (unsigned)a < b);
1007 printf("%d\n", (unsigned)a <= b);
1008 printf("%d\n", (unsigned)a <= a);
1009 printf("%d\n", (unsigned)b >= a);
1010 printf("%d\n", (unsigned)a >= a);
1011 printf("%d\n", (unsigned)b > a);
1014 struct empty {
1017 struct aligntest1 {
1018 char a[10];
1021 struct aligntest2 {
1022 int a;
1023 char b[10];
1026 struct aligntest3 {
1027 double a, b;
1030 struct aligntest4 {
1031 double a[0];
1034 struct __attribute__((aligned(16))) aligntest5
1036 int i;
1038 struct aligntest6
1040 int i;
1041 } __attribute__((aligned(16)));
1042 struct aligntest7
1044 int i;
1046 struct aligntest5 altest5[2];
1047 struct aligntest6 altest6[2];
1048 int pad1;
1049 /* altest7 is correctly aligned to 16 bytes also with TCC,
1050 but __alignof__ returns the wrong result (4) because we
1051 can't store the alignment yet when specified on symbols
1052 directly (it's stored in the type so we'd need to make
1053 a copy of it). -- FIXED */
1054 struct aligntest7 altest7[2] __attribute__((aligned(16)));
1056 struct aligntest8
1058 int i;
1059 } __attribute__((aligned(4096)));
1061 struct Large {
1062 unsigned long flags;
1063 union {
1064 void *u1;
1065 int *u2;
1068 struct {
1069 union {
1070 unsigned long index;
1071 void *freelist;
1073 union {
1074 unsigned long counters;
1075 struct {
1076 int bla;
1081 union {
1082 struct {
1083 long u3;
1084 long u4;
1086 void *u5;
1087 struct {
1088 unsigned long compound_head;
1089 unsigned int compound_dtor;
1090 unsigned int compound_order;
1093 } __attribute__((aligned(2 * sizeof(long))));
1095 typedef unsigned long long __attribute__((aligned(4))) unaligned_u64;
1097 struct aligntest9 {
1098 unsigned int buf_nr;
1099 unaligned_u64 start_lba;
1102 struct aligntest10 {
1103 unsigned int buf_nr;
1104 unsigned long long start_lba;
1107 void struct_test()
1109 struct1 *s;
1110 union union2 u;
1111 struct Large ls;
1113 printf("struct:\n");
1114 printf("sizes: %d %d %d %d\n",
1115 sizeof(struct struct1),
1116 sizeof(struct struct2),
1117 sizeof(union union1),
1118 sizeof(union union2));
1119 printf("offsets: %d\n", (int)((char*)&st1.u.v1 - (char*)&st1));
1120 st1.f1 = 1;
1121 st1.f2 = 2;
1122 st1.f3 = 3;
1123 printf("st1: %d %d %d\n",
1124 st1.f1, st1.f2, st1.f3);
1125 st1.u.v1 = 1;
1126 st1.u.v2 = 2;
1127 printf("union1: %d\n", st1.u.v1);
1128 u.w1 = 1;
1129 u.w2 = 2;
1130 printf("union2: %d\n", u.w1);
1131 s = &st2;
1132 s->f1 = 3;
1133 s->f2 = 2;
1134 s->f3 = 1;
1135 printf("st2: %d %d %d\n",
1136 s->f1, s->f2, s->f3);
1137 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
1139 /* align / size tests */
1140 printf("aligntest1 sizeof=%d alignof=%d\n",
1141 sizeof(struct aligntest1), __alignof__(struct aligntest1));
1142 printf("aligntest2 sizeof=%d alignof=%d\n",
1143 sizeof(struct aligntest2), __alignof__(struct aligntest2));
1144 printf("aligntest3 sizeof=%d alignof=%d\n",
1145 sizeof(struct aligntest3), __alignof__(struct aligntest3));
1146 printf("aligntest4 sizeof=%d alignof=%d\n",
1147 sizeof(struct aligntest4), __alignof__(struct aligntest4));
1148 printf("aligntest5 sizeof=%d alignof=%d\n",
1149 sizeof(struct aligntest5), __alignof__(struct aligntest5));
1150 printf("aligntest6 sizeof=%d alignof=%d\n",
1151 sizeof(struct aligntest6), __alignof__(struct aligntest6));
1152 printf("aligntest7 sizeof=%d alignof=%d\n",
1153 sizeof(struct aligntest7), __alignof__(struct aligntest7));
1154 printf("aligntest8 sizeof=%d alignof=%d\n",
1155 sizeof(struct aligntest8), __alignof__(struct aligntest8));
1156 printf("aligntest9 sizeof=%d alignof=%d\n",
1157 sizeof(struct aligntest9), __alignof__(struct aligntest9));
1158 printf("aligntest10 sizeof=%d alignof=%d\n",
1159 sizeof(struct aligntest10), __alignof__(struct aligntest10));
1160 printf("altest5 sizeof=%d alignof=%d\n",
1161 sizeof(altest5), __alignof__(altest5));
1162 printf("altest6 sizeof=%d alignof=%d\n",
1163 sizeof(altest6), __alignof__(altest6));
1164 printf("altest7 sizeof=%d alignof=%d\n",
1165 sizeof(altest7), __alignof__(altest7));
1167 /* empty structures (GCC extension) */
1168 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
1169 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
1171 printf("Large: sizeof=%d\n", sizeof(ls));
1172 memset(&ls, 0, sizeof(ls));
1173 ls.compound_head = 42;
1174 printf("Large: offsetof(compound_head)=%d\n", (int)((char*)&ls.compound_head - (char*)&ls));
1177 /* XXX: depend on endianness */
1178 void char_short_test()
1180 int var1, var2;
1182 printf("char_short:\n");
1184 var1 = 0x01020304;
1185 var2 = 0xfffefdfc;
1186 printf("s8=%d %d\n",
1187 *(char *)&var1, *(char *)&var2);
1188 printf("u8=%d %d\n",
1189 *(unsigned char *)&var1, *(unsigned char *)&var2);
1190 printf("s16=%d %d\n",
1191 *(short *)&var1, *(short *)&var2);
1192 printf("u16=%d %d\n",
1193 *(unsigned short *)&var1, *(unsigned short *)&var2);
1194 printf("s32=%d %d\n",
1195 *(int *)&var1, *(int *)&var2);
1196 printf("u32=%d %d\n",
1197 *(unsigned int *)&var1, *(unsigned int *)&var2);
1198 *(char *)&var1 = 0x08;
1199 printf("var1=%x\n", var1);
1200 *(short *)&var1 = 0x0809;
1201 printf("var1=%x\n", var1);
1202 *(int *)&var1 = 0x08090a0b;
1203 printf("var1=%x\n", var1);
1206 /******************/
1208 typedef struct Sym {
1209 int v;
1210 int t;
1211 int c;
1212 struct Sym *next;
1213 struct Sym *prev;
1214 } Sym;
1216 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1217 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1219 static int toupper1(int a)
1221 return TOUPPER(a);
1224 static unsigned int calc_vm_flags(unsigned int prot)
1226 unsigned int prot_bits;
1227 /* This used to segfault in some revisions: */
1228 prot_bits = ((0x1==0x00000001)?(prot&0x1):(prot&0x1)?0x00000001:0);
1229 return prot_bits;
1232 void bool_test()
1234 int *s, a, b, t, f, i;
1236 a = 0;
1237 s = (void*)0;
1238 printf("!s=%d\n", !s);
1240 if (!s || !s[0])
1241 a = 1;
1242 printf("a=%d\n", a);
1244 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1245 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1246 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1247 #if 1 && 1
1248 printf("a1\n");
1249 #endif
1250 #if 1 || 0
1251 printf("a2\n");
1252 #endif
1253 #if 1 ? 0 : 1
1254 printf("a3\n");
1255 #endif
1256 #if 0 ? 0 : 1
1257 printf("a4\n");
1258 #endif
1260 a = 4;
1261 printf("b=%d\n", a + (0 ? 1 : a / 2));
1263 /* test register spilling */
1264 a = 10;
1265 b = 10;
1266 a = (a + b) * ((a < b) ?
1267 ((b - a) * (a - b)): a + b);
1268 printf("a=%d\n", a);
1270 /* test complex || or && expressions */
1271 t = 1;
1272 f = 0;
1273 a = 32;
1274 printf("exp=%d\n", f == (32 <= a && a <= 3));
1275 printf("r=%d\n", (t || f) + (t && f));
1277 /* test ? : cast */
1279 int aspect_on;
1280 int aspect_native = 65536;
1281 double bfu_aspect = 1.0;
1282 int aspect;
1283 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
1284 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
1285 printf("aspect=%d\n", aspect);
1289 /* test ? : GCC extension */
1291 static int v1 = 34 ? : -1; /* constant case */
1292 static int v2 = 0 ? : -1; /* constant case */
1293 int a = 30;
1295 printf("%d %d\n", v1, v2);
1296 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
1299 /* again complex expression */
1300 for(i=0;i<256;i++) {
1301 if (toupper1 (i) != TOUPPER (i))
1302 printf("error %d\n", i);
1304 printf ("bits = 0x%x\n", calc_vm_flags (0x1));
1307 extern int undefined_function(void);
1308 extern int defined_function(void);
1310 static inline void refer_to_undefined(void)
1312 undefined_function();
1315 void optimize_out(void)
1317 int i = 0 ? undefined_function() : defined_function();
1318 printf ("oo:%d\n", i);
1319 int j = 1 ? defined_function() : undefined_function();
1320 printf ("oo:%d\n", j);
1321 if (0)
1322 printf("oo:%d\n", undefined_function());
1323 else
1324 printf("oo:%d\n", defined_function());
1325 if (1)
1326 printf("oo:%d\n", defined_function());
1327 else
1328 printf("oo:%d\n", undefined_function());
1329 while (1) {
1330 printf("oow:%d\n", defined_function());
1331 break;
1332 printf("oow:%d\n", undefined_function());
1334 j = 1;
1335 /* Following is a switch without {} block intentionally. */
1336 switch (j)
1337 case 1: break;
1338 printf ("oos:%d\n", defined_function());
1339 /* The following break shouldn't lead to disabled code after
1340 the while. */
1341 while (1)
1342 break;
1343 printf ("ool1:%d\n", defined_function());
1344 /* Same for the other types of loops. */
1346 break;
1347 while (1);
1348 printf ("ool2:%d\n", defined_function());
1349 for (;;)
1350 break;
1351 printf ("ool3:%d\n", defined_function());
1352 /* Normal {} blocks without controlling statements
1353 shouldn't reactivate code emission */
1354 while (1) {
1356 break;
1358 printf ("ool4:%d\n", undefined_function());
1360 j = 1;
1361 while (j) {
1362 if (j == 0)
1363 break; /* this break shouldn't disable code outside the if. */
1364 printf("ool5:%d\n", defined_function());
1365 j--;
1368 j = 1;
1369 while (j) {
1370 if (1)
1371 j--;
1372 else
1373 breakhere: break;
1374 printf("ool6:%d\n", defined_function());
1375 goto breakhere;
1377 j = 1;
1378 while (j) {
1379 j--;
1380 continue;
1381 printf("ool7:%d\n", undefined_function());
1384 /* Test that constants in logical && are optimized: */
1385 i = 0 && undefined_function();
1386 i = defined_function() && 0 && undefined_function();
1387 if (0 && undefined_function())
1388 undefined_function();
1389 if (defined_function() && 0)
1390 undefined_function();
1391 if (0 && 0)
1392 undefined_function();
1393 if (defined_function() && 0 && undefined_function())
1394 undefined_function();
1395 /* The same for || : */
1396 i = 1 || undefined_function();
1397 i = defined_function() || 1 || undefined_function();
1398 if (1 || undefined_function())
1400 else
1401 undefined_function();
1402 if (defined_function() || 1)
1404 else
1405 undefined_function();
1406 if (1 || 1)
1408 else
1409 undefined_function();
1410 if (defined_function() || 1 || undefined_function())
1412 else
1413 undefined_function();
1415 if (defined_function() && 0)
1416 refer_to_undefined();
1418 if (0) {
1419 (void)sizeof( ({
1420 do { } while (0);
1422 }) );
1423 undefined_function();
1426 /* Leave the "if(1)return; printf()" in this order and last in the function */
1427 if (1)
1428 return;
1429 printf ("oor:%d\n", undefined_function());
1432 int defined_function(void)
1434 static int i = 40;
1435 return i++;
1438 /* GCC accepts that */
1439 static int tab_reinit[];
1440 static int tab_reinit[10];
1442 //int cinit1; /* a global variable can be defined several times without error ! */
1443 int cinit1;
1444 int cinit1;
1445 int cinit1 = 0;
1446 int *cinit2 = (int []){3, 2, 1};
1448 void compound_literal_test(void)
1450 int *p, i;
1451 char *q, *q3;
1453 printf("compound_test:\n");
1455 p = (int []){1, 2, 3};
1456 for(i=0;i<3;i++)
1457 printf(" %d", p[i]);
1458 printf("\n");
1460 for(i=0;i<3;i++)
1461 printf("%d", cinit2[i]);
1462 printf("\n");
1464 q = "tralala1";
1465 printf("q1=%s\n", q);
1467 q = (char *){ "tralala2" };
1468 printf("q2=%s\n", q);
1470 q3 = (char *){ q };
1471 printf("q3=%s\n", q3);
1473 q = (char []){ "tralala3" };
1474 printf("q4=%s\n", q);
1476 #ifdef ALL_ISOC99
1477 p = (int []){1, 2, cinit1 + 3};
1478 for(i=0;i<3;i++)
1479 printf(" %d", p[i]);
1480 printf("\n");
1482 for(i=0;i<3;i++) {
1483 p = (int []){1, 2, 4 + i};
1484 printf("%d %d %d\n",
1485 p[0],
1486 p[1],
1487 p[2]);
1489 #endif
1492 /* K & R protos */
1494 kr_func1(a, b)
1496 return a + b;
1499 int kr_func2(a, b)
1501 return a + b;
1504 kr_test()
1506 printf("kr_test:\n");
1507 printf("func1=%d\n", kr_func1(3, 4));
1508 printf("func2=%d\n", kr_func2(3, 4));
1509 return 0;
1512 void num(int n)
1514 char *tab, *p;
1515 tab = (char*)malloc(20);
1516 p = tab;
1517 while (1) {
1518 *p = 48 + (n % 10);
1519 p++;
1520 n = n / 10;
1521 if (n == 0)
1522 break;
1524 while (p != tab) {
1525 p--;
1526 printf("%c", *p);
1528 printf("\n");
1529 free(tab);
1532 /* structure assignment tests */
1533 struct structa1 {
1534 int f1;
1535 char f2;
1538 struct structa1 ssta1;
1540 void struct_assign_test1(struct structa1 s1, int t, float f)
1542 printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
1545 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1547 s1.f1 += t;
1548 s1.f2 -= t;
1549 return s1;
1552 void struct_assign_test(void)
1554 struct S {
1555 struct structa1 lsta1, lsta2;
1556 int i;
1557 } s, *ps;
1559 ps = &s;
1560 ps->i = 4;
1561 #if 0
1562 printf("struct_assign_test:\n");
1564 s.lsta1.f1 = 1;
1565 s.lsta1.f2 = 2;
1566 printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
1567 s.lsta2 = s.lsta1;
1568 printf("%d %d\n", s.lsta2.f1, s.lsta2.f2);
1569 #else
1570 s.lsta2.f1 = 1;
1571 s.lsta2.f2 = 2;
1572 #endif
1573 struct_assign_test1(ps->lsta2, 3, 4.5);
1575 printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
1576 ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
1577 printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
1579 static struct {
1580 void (*elem)();
1581 } t[] = {
1582 /* XXX: we should allow this even without braces */
1583 { struct_assign_test }
1585 printf("%d\n", struct_assign_test == t[0].elem);
1588 /* casts to short/char */
1590 void cast1(char a, short b, unsigned char c, unsigned short d)
1592 printf("%d %d %d %d\n", a, b, c, d);
1595 char bcast;
1596 short scast;
1598 void cast_test()
1600 int a;
1601 char c;
1602 char tab[10];
1603 unsigned b,d;
1604 short s;
1605 char *p = NULL;
1606 p -= 0x700000000042;
1608 printf("cast_test:\n");
1609 a = 0xfffff;
1610 cast1(a, a, a, a);
1611 a = 0xffffe;
1612 printf("%d %d %d %d\n",
1613 (char)(a + 1),
1614 (short)(a + 1),
1615 (unsigned char)(a + 1),
1616 (unsigned short)(a + 1));
1617 printf("%d %d %d %d\n",
1618 (char)0xfffff,
1619 (short)0xfffff,
1620 (unsigned char)0xfffff,
1621 (unsigned short)0xfffff);
1623 a = (bcast = 128) + 1;
1624 printf("%d\n", a);
1625 a = (scast = 65536) + 1;
1626 printf("%d\n", a);
1628 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1630 /* test cast from unsigned to signed short to int */
1631 b = 0xf000;
1632 d = (short)b;
1633 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1634 b = 0xf0f0;
1635 d = (char)b;
1636 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1638 /* test implicit int casting for array accesses */
1639 c = 0;
1640 tab[1] = 2;
1641 tab[c] = 1;
1642 printf("%d %d\n", tab[0], tab[1]);
1644 /* test implicit casting on some operators */
1645 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1646 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1647 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1649 /* from pointer to integer types */
1650 printf("%d %d %ld %ld %lld %lld\n",
1651 (int)p, (unsigned int)p,
1652 (long)p, (unsigned long)p,
1653 (long long)p, (unsigned long long)p);
1655 /* from integers to pointers */
1656 printf("%p %p %p %p\n",
1657 (void *)a, (void *)b, (void *)c, (void *)d);
1660 /* initializers tests */
1661 struct structinit1 {
1662 int f1;
1663 char f2;
1664 short f3;
1665 int farray[3];
1668 int sinit1 = 2;
1669 int sinit2 = { 3 };
1670 int sinit3[3] = { 1, 2, {{3}}, };
1671 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1672 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1673 int sinit6[] = { 1, 2, 3 };
1674 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1675 char sinit8[] = "hello" "trala";
1677 struct structinit1 sinit9 = { 1, 2, 3 };
1678 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1679 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1680 #ifdef ALL_ISOC99
1681 .farray[0] = 10,
1682 .farray[1] = 11,
1683 .farray[2] = 12,
1684 #endif
1687 char *sinit12 = "hello world";
1688 char *sinit13[] = {
1689 "test1",
1690 "test2",
1691 "test3",
1693 char sinit14[10] = { "abc" };
1694 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1696 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1698 struct bar {
1699 char *s;
1700 int len;
1701 } sinit17[] = {
1702 "a1", 4,
1703 "a2", 1
1706 int sinit18[10] = {
1707 [2 ... 5] = 20,
1709 [8] = 10,
1712 struct complexinit0 {
1713 int a;
1714 int b;
1717 struct complexinit {
1718 int a;
1719 const struct complexinit0 *b;
1722 const static struct complexinit cix[] = {
1723 [0] = {
1724 .a = 2000,
1725 .b = (const struct complexinit0[]) {
1726 { 2001, 2002 },
1727 { 2003, 2003 },
1733 struct complexinit2 {
1734 int a;
1735 int b[];
1738 struct complexinit2 cix20;
1740 struct complexinit2 cix21 = {
1741 .a = 3000,
1742 .b = { 3001, 3002, 3003 }
1745 struct complexinit2 cix22 = {
1746 .a = 4000,
1747 .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1750 typedef int arrtype1[];
1751 arrtype1 sinit19 = {1};
1752 arrtype1 sinit20 = {2,3};
1753 typedef int arrtype2[3];
1754 arrtype2 sinit21 = {4};
1755 arrtype2 sinit22 = {5,6,7};
1757 /* Address comparisons of non-weak symbols with zero can be const-folded */
1758 int sinit23[2] = { "astring" ? sizeof("astring") : -1,
1759 &sinit23 ? 42 : -1 };
1761 extern int external_inited = 42;
1763 void init_test(void)
1765 int linit1 = 2;
1766 int linit2 = { 3 };
1767 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1768 int linit6[] = { 1, 2, 3 };
1769 int i, j;
1770 char linit8[] = "hello" "trala";
1771 int linit12[10] = { 1, 2 };
1772 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1773 char linit14[10] = "abc";
1774 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1775 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1776 int linit17 = sizeof(linit17);
1777 int zero = 0;
1778 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1779 int linit18[2] = {&zero ? 1 : -1, zero ? -1 : 1 };
1781 printf("init_test:\n");
1783 printf("sinit1=%d\n", sinit1);
1784 printf("sinit2=%d\n", sinit2);
1785 printf("sinit3=%d %d %d %d\n",
1786 sizeof(sinit3),
1787 sinit3[0],
1788 sinit3[1],
1789 sinit3[2]
1791 printf("sinit6=%d\n", sizeof(sinit6));
1792 printf("sinit7=%d %d %d %d\n",
1793 sizeof(sinit7),
1794 sinit7[0],
1795 sinit7[1],
1796 sinit7[2]
1798 printf("sinit8=%s\n", sinit8);
1799 printf("sinit9=%d %d %d\n",
1800 sinit9.f1,
1801 sinit9.f2,
1802 sinit9.f3
1804 printf("sinit10=%d %d %d\n",
1805 sinit10.f1,
1806 sinit10.f2,
1807 sinit10.f3
1809 printf("sinit11=%d %d %d %d %d %d\n",
1810 sinit11.f1,
1811 sinit11.f2,
1812 sinit11.f3,
1813 sinit11.farray[0],
1814 sinit11.farray[1],
1815 sinit11.farray[2]
1818 for(i=0;i<3;i++)
1819 for(j=0;j<2;j++)
1820 printf("[%d][%d] = %d %d %d\n",
1821 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1822 printf("linit1=%d\n", linit1);
1823 printf("linit2=%d\n", linit2);
1824 printf("linit6=%d\n", sizeof(linit6));
1825 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1827 printf("sinit12=%s\n", sinit12);
1828 printf("sinit13=%d %s %s %s\n",
1829 sizeof(sinit13),
1830 sinit13[0],
1831 sinit13[1],
1832 sinit13[2]);
1833 printf("sinit14=%s\n", sinit14);
1835 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1836 printf("\n");
1837 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1838 printf("\n");
1839 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1840 printf("\n");
1841 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1842 printf("\n");
1843 printf("%d %d %d %d\n",
1844 linit16.a1,
1845 linit16.a2,
1846 linit16.a3,
1847 linit16.a4);
1848 /* test that initialisation is done after variable declare */
1849 printf("linit17=%d\n", linit17);
1850 printf("sinit15=%d\n", sinit15[0]);
1851 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1852 printf("sinit17=%s %d %s %d\n",
1853 sinit17[0].s, sinit17[0].len,
1854 sinit17[1].s, sinit17[1].len);
1855 for(i=0;i<10;i++)
1856 printf("%x ", sinit18[i]);
1857 printf("\n");
1858 /* complex init check */
1859 printf("cix: %d %d %d %d %d %d %d\n",
1860 cix[0].a,
1861 cix[0].b[0].a, cix[0].b[0].b,
1862 cix[0].b[1].a, cix[0].b[1].b,
1863 cix[0].b[2].a, cix[0].b[2].b);
1864 printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
1865 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
1867 printf("arrtype1: %d %d %d\n", sinit19[0], sinit20[0], sinit20[1]);
1868 printf("arrtype2: %d %d\n", sizeof(sinit19), sizeof(sinit20));
1869 printf("arrtype3: %d %d %d\n", sinit21[0], sinit21[1], sinit21[2]);
1870 printf("arrtype4: %d %d %d\n", sinit22[0], sinit22[1], sinit22[2]);
1871 printf("arrtype5: %d %d\n", sizeof(sinit21), sizeof(sinit22));
1872 printf("arrtype6: %d\n", sizeof(arrtype2));
1874 printf("sinit23= %d %d\n", sinit23[0], sinit23[1]);
1875 printf("linit18= %d %d\n", linit18[0], linit18[1]);
1878 void switch_uc(unsigned char uc)
1880 switch (uc) {
1881 case 0xfb ... 0xfe:
1882 printf("ucsw:1\n");
1883 break;
1884 case 0xff:
1885 printf("ucsw:2\n");
1886 break;
1887 case 0 ... 5:
1888 printf("ucsw:3\n");
1889 break;
1890 default:
1891 printf("ucsw: broken!\n");
1895 void switch_sc(signed char sc)
1897 switch (sc) {
1898 case -5 ... -2:
1899 printf("scsw:1\n");
1900 break;
1901 case -1:
1902 printf("scsw:2\n");
1903 break;
1904 case 0 ... 5:
1905 printf("scsw:3\n");
1906 break;
1907 default:
1908 printf("scsw: broken!\n");
1912 void switch_test()
1914 int i;
1915 unsigned long long ull;
1916 long long ll;
1918 for(i=0;i<15;i++) {
1919 switch(i) {
1920 case 0:
1921 case 1:
1922 printf("a");
1923 break;
1924 default:
1925 printf("%d", i);
1926 break;
1927 case 8 ... 12:
1928 printf("c");
1929 break;
1930 case 3:
1931 printf("b");
1932 break;
1933 case 0xc33c6b9fU:
1934 case 0x7c9eeeb9U:
1935 break;
1938 printf("\n");
1940 for (i = 1; i <= 5; i++) {
1941 ull = (unsigned long long)i << 61;
1942 switch (ull) {
1943 case 1ULL << 61:
1944 printf("ullsw:1\n");
1945 break;
1946 case 2ULL << 61:
1947 printf("ullsw:2\n");
1948 break;
1949 case 3ULL << 61:
1950 printf("ullsw:3\n");
1951 break;
1952 case 4ULL << 61:
1953 printf("ullsw:4\n");
1954 break;
1955 case 5ULL << 61:
1956 printf("ullsw:5\n");
1957 break;
1958 default:
1959 printf("ullsw: broken!\n");
1963 for (i = 1; i <= 5; i++) {
1964 ll = (long long)i << 61;
1965 switch (ll) {
1966 case 1LL << 61:
1967 printf("llsw:1\n");
1968 break;
1969 case 2LL << 61:
1970 printf("llsw:2\n");
1971 break;
1972 case 3LL << 61:
1973 printf("llsw:3\n");
1974 break;
1975 case 4LL << 61:
1976 printf("llsw:4\n");
1977 break;
1978 case 5LL << 61:
1979 printf("llsw:5\n");
1980 break;
1981 default:
1982 printf("llsw: broken!\n");
1986 for (i = -5; i <= 5; i++) {
1987 switch_uc((unsigned char)i);
1990 for (i = -5; i <= 5; i++) {
1991 switch_sc ((signed char)i);
1995 /* ISOC99 _Bool type */
1996 void c99_bool_test(void)
1998 #ifdef BOOL_ISOC99
1999 int a;
2000 _Bool b;
2002 printf("bool_test:\n");
2003 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
2004 a = 3;
2005 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
2006 b = 3;
2007 printf("b = %d\n", b);
2008 b++;
2009 printf("b = %d\n", b);
2010 #endif
2013 void bitfield_test(void)
2015 int a;
2016 short sa;
2017 unsigned char ca;
2018 struct sbf1 {
2019 int f1 : 3;
2020 int : 2;
2021 int f2 : 1;
2022 int : 0;
2023 int f3 : 5;
2024 int f4 : 7;
2025 unsigned int f5 : 7;
2026 } st1;
2027 printf("bitfield_test:");
2028 printf("sizeof(st1) = %d\n", sizeof(st1));
2030 st1.f1 = 3;
2031 st1.f2 = 1;
2032 st1.f3 = 15;
2033 a = 120;
2034 st1.f4 = a;
2035 st1.f5 = a;
2036 st1.f5++;
2037 printf("%d %d %d %d %d\n",
2038 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
2039 sa = st1.f5;
2040 ca = st1.f5;
2041 printf("%d %d\n", sa, ca);
2043 st1.f1 = 7;
2044 if (st1.f1 == -1)
2045 printf("st1.f1 == -1\n");
2046 else
2047 printf("st1.f1 != -1\n");
2048 if (st1.f2 == -1)
2049 printf("st1.f2 == -1\n");
2050 else
2051 printf("st1.f2 != -1\n");
2053 struct sbf2 {
2054 long long f1 : 45;
2055 long long : 2;
2056 long long f2 : 35;
2057 unsigned long long f3 : 38;
2058 } st2;
2059 st2.f1 = 0x123456789ULL;
2060 a = 120;
2061 st2.f2 = (long long)a << 25;
2062 st2.f3 = a;
2063 st2.f2++;
2064 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
2066 #if 0
2067 Disabled for now until further clarification re GCC compatibility
2068 struct sbf3 {
2069 int f1 : 7;
2070 int f2 : 1;
2071 char f3;
2072 int f4 : 8;
2073 int f5 : 1;
2074 int f6 : 16;
2075 } st3;
2076 printf("sizeof(st3) = %d\n", sizeof(st3));
2077 #endif
2079 struct sbf4 {
2080 int x : 31;
2081 char y : 2;
2082 } st4;
2083 st4.y = 1;
2084 printf("st4.y == %d\n", st4.y);
2085 struct sbf5 {
2086 int a;
2087 char b;
2088 int x : 12, y : 4, : 0, : 4, z : 3;
2089 char c;
2090 } st5 = { 1, 2, 3, 4, -3, 6 };
2091 printf("st5 = %d %d %d %d %d %d\n", st5.a, st5.b, st5.x, st5.y, st5.z, st5.c);
2092 struct sbf6 {
2093 short x : 12;
2094 unsigned char y : 2;
2095 } st6;
2096 st6.y = 1;
2097 printf("st6.y == %d\n", st6.y);
2100 #ifdef __x86_64__
2101 #define FLOAT_FMT "%f\n"
2102 #else
2103 /* x86's float isn't compatible with GCC */
2104 #define FLOAT_FMT "%.5f\n"
2105 #endif
2107 /* declare strto* functions as they are C99 */
2108 double strtod(const char *nptr, char **endptr);
2110 #if defined(_WIN32)
2111 float strtof(const char *nptr, char **endptr) {return (float)strtod(nptr, endptr);}
2112 LONG_DOUBLE strtold(const char *nptr, char **endptr) {return (LONG_DOUBLE)strtod(nptr, endptr);}
2113 #else
2114 float strtof(const char *nptr, char **endptr);
2115 LONG_DOUBLE strtold(const char *nptr, char **endptr);
2116 #endif
2118 #define FTEST(prefix, typename, type, fmt)\
2119 void prefix ## cmp(type a, type b)\
2121 printf("%d %d %d %d %d %d\n",\
2122 a == b,\
2123 a != b,\
2124 a < b,\
2125 a > b,\
2126 a >= b,\
2127 a <= b);\
2128 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
2131 a + b,\
2132 a - b,\
2133 a * b,\
2134 a / b,\
2135 -a);\
2136 printf(fmt "\n", ++a);\
2137 printf(fmt "\n", a++);\
2138 printf(fmt "\n", a);\
2139 b = 0;\
2140 printf("%d %d\n", !a, !b);\
2142 void prefix ## fcast(type a)\
2144 float fa;\
2145 double da;\
2146 LONG_DOUBLE la;\
2147 int ia;\
2148 long long llia;\
2149 unsigned int ua;\
2150 unsigned long long llua;\
2151 type b;\
2152 fa = a;\
2153 da = a;\
2154 la = a;\
2155 printf("ftof: %f %f %Lf\n", fa, da, la);\
2156 ia = (int)a;\
2157 llia = (long long)a;\
2158 a = (a >= 0) ? a : -a;\
2159 ua = (unsigned int)a;\
2160 llua = (unsigned long long)a;\
2161 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
2162 ia = -1234;\
2163 ua = 0x81234500;\
2164 llia = -0x123456789012345LL;\
2165 llua = 0xf123456789012345LLU;\
2166 b = ia;\
2167 printf("itof: " fmt "\n", b);\
2168 b = ua;\
2169 printf("utof: " fmt "\n", b);\
2170 b = llia;\
2171 printf("lltof: " fmt "\n", b);\
2172 b = llua;\
2173 printf("ulltof: " fmt "\n", b);\
2176 float prefix ## retf(type a) { return a; }\
2177 double prefix ## retd(type a) { return a; }\
2178 LONG_DOUBLE prefix ## retld(type a) { return a; }\
2180 void prefix ## call(void)\
2182 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
2183 printf("double: %f\n", prefix ## retd(42.123456789));\
2184 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
2185 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
2188 void prefix ## signed_zeros(void) \
2190 type x = 0.0, y = -0.0, n, p;\
2191 if (x == y)\
2192 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
2193 1.0 / x != 1.0 / y);\
2194 else\
2195 printf ("x != y; this is wrong!\n");\
2197 n = -x;\
2198 if (x == n)\
2199 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
2200 1.0 / x != 1.0 / n);\
2201 else\
2202 printf ("x != -x; this is wrong!\n");\
2204 p = +y;\
2205 if (x == p)\
2206 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
2207 1.0 / x != 1.0 / p);\
2208 else\
2209 printf ("x != +y; this is wrong!\n");\
2210 p = -y;\
2211 if (x == p)\
2212 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
2213 1.0 / x != 1.0 / p);\
2214 else\
2215 printf ("x != -y; this is wrong!\n");\
2217 void prefix ## test(void)\
2219 printf("testing '%s'\n", #typename);\
2220 prefix ## cmp(1, 2.5);\
2221 prefix ## cmp(2, 1.5);\
2222 prefix ## cmp(1, 1);\
2223 prefix ## fcast(234.6);\
2224 prefix ## fcast(-2334.6);\
2225 prefix ## call();\
2226 prefix ## signed_zeros();\
2229 FTEST(f, float, float, "%f")
2230 FTEST(d, double, double, "%f")
2231 FTEST(ld, long double, LONG_DOUBLE, "%Lf")
2233 double ftab1[3] = { 1.2, 3.4, -5.6 };
2236 void float_test(void)
2238 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
2239 float fa, fb;
2240 double da, db;
2241 int a;
2242 unsigned int b;
2243 static double nan2 = 0.0/0.0;
2244 static double inf1 = 1.0/0.0;
2245 static double inf2 = 1e5000;
2247 printf("float_test:\n");
2248 printf("sizeof(float) = %d\n", sizeof(float));
2249 printf("sizeof(double) = %d\n", sizeof(double));
2250 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));
2251 ftest();
2252 dtest();
2253 ldtest();
2254 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
2255 printf("%f %f %f\n", 2.12, .5, 2.3e10);
2256 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
2257 da = 123;
2258 printf("da=%f\n", da);
2259 fa = 123;
2260 printf("fa=%f\n", fa);
2261 a = 4000000000;
2262 da = a;
2263 printf("da = %f\n", da);
2264 b = 4000000000;
2265 db = b;
2266 printf("db = %f\n", db);
2267 printf("nan != nan = %d, inf1 = %f, inf2 = %f\n", nan2 != nan2, inf1, inf2);
2268 #endif
2271 int fib(int n)
2273 if (n <= 2)
2274 return 1;
2275 else
2276 return fib(n-1) + fib(n-2);
2279 void __attribute__((aligned(16))) aligned_function(int i) {}
2281 void funcptr_test()
2283 void (*func)(int);
2284 int a;
2285 struct {
2286 int dummy;
2287 void (*func)(int);
2288 } st1;
2289 long diff;
2291 printf("funcptr:\n");
2292 func = &num;
2293 (*func)(12345);
2294 func = num;
2295 a = 1;
2296 a = 1;
2297 func(12345);
2298 /* more complicated pointer computation */
2299 st1.func = num;
2300 st1.func(12346);
2301 printf("sizeof1 = %d\n", sizeof(funcptr_test));
2302 printf("sizeof2 = %d\n", sizeof funcptr_test);
2303 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
2304 printf("sizeof4 = %d\n", sizeof &funcptr_test);
2305 a = 0;
2306 func = num + a;
2307 diff = func - num;
2308 func(42);
2309 (func + diff)(42);
2310 (num + a)(43);
2312 /* Check that we can align functions */
2313 func = aligned_function;
2314 printf("aligned_function (should be zero): %d\n", ((int)func) & 15);
2317 void lloptest(long long a, long long b)
2319 unsigned long long ua, ub;
2321 ua = a;
2322 ub = b;
2323 /* arith */
2324 printf("arith: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2325 a + b,
2326 a - b,
2327 a * b);
2329 if (b != 0) {
2330 printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2331 a / b,
2332 a % b);
2335 /* binary */
2336 printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2337 a & b,
2338 a | b,
2339 a ^ b);
2341 /* tests */
2342 printf("test: %d %d %d %d %d %d\n",
2343 a == b,
2344 a != b,
2345 a < b,
2346 a > b,
2347 a >= b,
2348 a <= b);
2350 printf("utest: %d %d %d %d %d %d\n",
2351 ua == ub,
2352 ua != ub,
2353 ua < ub,
2354 ua > ub,
2355 ua >= ub,
2356 ua <= ub);
2358 /* arith2 */
2359 a++;
2360 b++;
2361 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2362 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a++, b++);
2363 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", --a, --b);
2364 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2365 b = ub = 0;
2366 printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
2369 void llshift(long long a, int b)
2371 printf("shift: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2372 (unsigned long long)a >> b,
2373 a >> b,
2374 a << b);
2375 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2376 (unsigned long long)a >> 3,
2377 a >> 3,
2378 a << 3);
2379 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2380 (unsigned long long)a >> 35,
2381 a >> 35,
2382 a << 35);
2385 void llfloat(void)
2387 float fa;
2388 double da;
2389 LONG_DOUBLE lda;
2390 long long la, lb, lc;
2391 unsigned long long ula, ulb, ulc;
2392 la = 0x12345678;
2393 ula = 0x72345678;
2394 la = (la << 20) | 0x12345;
2395 ula = ula << 33;
2396 printf("la=" LONG_LONG_FORMAT " ula=" ULONG_LONG_FORMAT "\n", la, ula);
2398 fa = la;
2399 da = la;
2400 lda = la;
2401 printf("lltof: %f %f %Lf\n", fa, da, lda);
2403 la = fa;
2404 lb = da;
2405 lc = lda;
2406 printf("ftoll: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", la, lb, lc);
2408 fa = ula;
2409 da = ula;
2410 lda = ula;
2411 printf("ulltof: %f %f %Lf\n", fa, da, lda);
2413 ula = fa;
2414 ulb = da;
2415 ulc = lda;
2416 printf("ftoull: " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT "\n", ula, ulb, ulc);
2419 long long llfunc1(int a)
2421 return a * 2;
2424 struct S {
2425 int id;
2426 char item;
2429 long long int value(struct S *v)
2431 return ((long long int)v->item);
2434 long long llfunc2(long long x, long long y, int z)
2436 return x * y * z;
2439 void longlong_test(void)
2441 long long a, b, c;
2442 int ia;
2443 unsigned int ua;
2444 printf("longlong_test:\n");
2445 printf("sizeof(long long) = %d\n", sizeof(long long));
2446 ia = -1;
2447 ua = -2;
2448 a = ia;
2449 b = ua;
2450 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2451 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %Lx\n",
2452 (long long)1,
2453 (long long)-2,
2454 1LL,
2455 0x1234567812345679);
2456 a = llfunc1(-3);
2457 printf(LONG_LONG_FORMAT "\n", a);
2459 lloptest(1000, 23);
2460 lloptest(0xff, 0x1234);
2461 b = 0x72345678 << 10;
2462 lloptest(-3, b);
2463 llshift(0x123, 5);
2464 llshift(-23, 5);
2465 b = 0x72345678LL << 10;
2466 llshift(b, 47);
2468 llfloat();
2469 #if 1
2470 b = 0x12345678;
2471 a = -1;
2472 c = a + b;
2473 printf("%Lx\n", c);
2474 #endif
2476 /* long long reg spill test */
2478 struct S a;
2480 a.item = 3;
2481 printf("%lld\n", value(&a));
2483 lloptest(0x80000000, 0);
2486 long long *p, v, **pp;
2487 v = 1;
2488 p = &v;
2489 p[0]++;
2490 printf("another long long spill test : %lld\n", *p);
2491 pp = &p;
2493 v = llfunc2(**pp, **pp, ia);
2494 printf("a long long function (arm-)reg-args test : %lld\n", v);
2496 a = 68719476720LL;
2497 b = 4294967295LL;
2498 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
2500 printf(LONG_LONG_FORMAT "\n", 0x123456789LLU);
2502 /* long long pointer deref in argument passing test */
2503 a = 0x123;
2504 long long *p = &a;
2505 llshift(*p, 5);
2507 /* shortening followed by widening */
2508 unsigned long long u = 0x8000000000000001ULL;
2509 u = (unsigned)(u + 1);
2510 printf("long long u=" ULONG_LONG_FORMAT "\n", u);
2513 void manyarg_test(void)
2515 LONG_DOUBLE ld = 1234567891234LL;
2516 printf("manyarg_test:\n");
2517 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2518 1, 2, 3, 4, 5, 6, 7, 8,
2519 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2520 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2521 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2522 1, 2, 3, 4, 5, 6, 7, 8,
2523 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2524 1234567891234LL, 987654321986LL,
2525 42.0, 43.0);
2526 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2527 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2528 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2529 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2530 1234567891234LL, 987654321986LL,
2531 42.0, 43.0);
2532 printf("%d %d %d %d %d %d %d %d %Lf\n",
2533 1, 2, 3, 4, 5, 6, 7, 8, ld);
2534 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2535 LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n",
2536 1, 2, 3, 4, 5, 6, 7, 8,
2537 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2538 1234567891234LL, 987654321986LL,
2539 42.0, 43.0, ld);
2540 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2541 "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n",
2542 1, 2, 3, 4, 5, 6, 7, 8,
2543 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2544 ld, 1234567891234LL, 987654321986LL,
2545 42.0, 43.0, ld);
2548 void vprintf1(const char *fmt, ...)
2550 va_list ap, aq;
2551 const char *p;
2552 int c, i;
2553 double d;
2554 long long ll;
2555 LONG_DOUBLE ld;
2557 va_start(aq, fmt);
2558 va_copy(ap, aq);
2560 p = fmt;
2561 for(;;) {
2562 c = *p;
2563 if (c == '\0')
2564 break;
2565 p++;
2566 if (c == '%') {
2567 c = *p;
2568 switch(c) {
2569 case '\0':
2570 goto the_end;
2571 case 'd':
2572 i = va_arg(ap, int);
2573 printf("%d", i);
2574 break;
2575 case 'f':
2576 d = va_arg(ap, double);
2577 printf("%f", d);
2578 break;
2579 case 'l':
2580 ll = va_arg(ap, long long);
2581 printf(LONG_LONG_FORMAT, ll);
2582 break;
2583 case 'F':
2584 ld = va_arg(ap, LONG_DOUBLE);
2585 printf("%Lf", ld);
2586 break;
2588 p++;
2589 } else {
2590 putchar(c);
2593 the_end:
2594 va_end(aq);
2595 va_end(ap);
2598 struct myspace {
2599 short int profile;
2602 void stdarg_for_struct(struct myspace bob, ...)
2604 struct myspace george, bill;
2605 va_list ap;
2606 short int validate;
2608 va_start(ap, bob);
2609 bill = va_arg(ap, struct myspace);
2610 george = va_arg(ap, struct myspace);
2611 validate = va_arg(ap, int);
2612 printf("stdarg_for_struct: %d %d %d %d\n",
2613 bob.profile, bill.profile, george.profile, validate);
2614 va_end(ap);
2617 void stdarg_for_libc(const char *fmt, ...)
2619 va_list args;
2620 va_start(args, fmt);
2621 vprintf(fmt, args);
2622 va_end(args);
2625 void stdarg_test(void)
2627 LONG_DOUBLE ld = 1234567891234LL;
2628 struct myspace bob;
2630 vprintf1("%d %d %d\n", 1, 2, 3);
2631 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2632 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2633 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2634 vprintf1("%d %f %l %F %d %f %l %F\n",
2635 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2636 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2637 1, 2, 3, 4, 5, 6, 7, 8,
2638 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2639 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2640 1, 2, 3, 4, 5, 6, 7, 8,
2641 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2642 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2643 "%l %l %f %f\n",
2644 1, 2, 3, 4, 5, 6, 7, 8,
2645 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2646 1234567891234LL, 987654321986LL,
2647 42.0, 43.0);
2648 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2649 "%l %l %f %f\n",
2650 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2651 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2652 1234567891234LL, 987654321986LL,
2653 42.0, 43.0);
2654 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2655 1, 2, 3, 4, 5, 6, 7, 8, ld);
2656 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2657 "%l %l %f %f %F\n",
2658 1, 2, 3, 4, 5, 6, 7, 8,
2659 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2660 1234567891234LL, 987654321986LL,
2661 42.0, 43.0, ld);
2662 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2663 "%F %l %l %f %f %F\n",
2664 1, 2, 3, 4, 5, 6, 7, 8,
2665 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2666 ld, 1234567891234LL, 987654321986LL,
2667 42.0, 43.0, ld);
2669 bob.profile = 42;
2670 stdarg_for_struct(bob, bob, bob, bob.profile);
2671 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2674 void whitespace_test(void)
2676 char *str;
2678 \f\v #if 1
2679 pri\
2680 ntf("whitspace:\n");\f\v
2681 #endif
2682 pf("N=%d\n", 2);
2684 #ifdef CORRECT_CR_HANDLING
2685 pri\
2686 ntf("aaa=%d\n", 3);
2687 #endif
2689 pri\
2691 ntf("min=%d\n", 4);
2693 #ifdef ACCEPT_CR_IN_STRINGS
2694 printf("len1=%d\n", strlen("
2695 "));
2696 #ifdef CORRECT_CR_HANDLING
2697 str = "
2699 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
2700 #endif
2701 printf("len1=%d\n", strlen(" a
2702 "));
2703 #endif /* ACCEPT_CR_IN_STRINGS */
2706 int reltab[3] = { 1, 2, 3 };
2708 int *rel1 = &reltab[1];
2709 int *rel2 = &reltab[2];
2711 #ifdef _WIN64
2712 void relocation_test(void) {}
2713 #else
2714 void getmyaddress(void)
2716 printf("in getmyaddress\n");
2719 #ifdef __LP64__
2720 long __pa_symbol(void)
2722 /* This 64bit constant was handled incorrectly, it was used as addend
2723 (which can hold 64bit just fine) in connection with a symbol,
2724 and TCC generates wrong code for that (displacements are 32bit only).
2725 This effectively is "+ 0x80000000", and if addresses of globals
2726 are below 2GB the result should be a number without high 32 bits set. */
2727 return ((long)(((unsigned long)(&rel1))) - (0xffffffff80000000UL));
2729 #endif
2731 unsigned long theaddress = (unsigned long)getmyaddress;
2732 void relocation_test(void)
2734 void (*fptr)(void) = (void (*)(void))theaddress;
2735 printf("*rel1=%d\n", *rel1);
2736 printf("*rel2=%d\n", *rel2);
2737 fptr();
2738 #ifdef __LP64__
2739 printf("pa_symbol=0x%lx\n", __pa_symbol() >> 63);
2740 #endif
2742 #endif
2744 void old_style_f(a,b,c)
2745 int a, b;
2746 double c;
2748 printf("a=%d b=%d b=%f\n", a, b, c);
2751 void decl_func1(int cmpfn())
2753 printf("cmpfn=%lx\n", (long)cmpfn);
2756 void decl_func2(cmpfn)
2757 int cmpfn();
2759 printf("cmpfn=%lx\n", (long)cmpfn);
2762 void old_style_function(void)
2764 old_style_f((void *)1, 2, 3.0);
2765 decl_func1(NULL);
2766 decl_func2(NULL);
2769 void alloca_test()
2771 #if defined __i386__ || defined __x86_64__ || defined __arm__
2772 char *p = alloca(16);
2773 strcpy(p,"123456789012345");
2774 printf("alloca: p is %s\n", p);
2775 char *demo = "This is only a test.\n";
2776 /* Test alloca embedded in a larger expression */
2777 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
2778 #endif
2781 void *bounds_checking_is_enabled()
2783 char ca[10], *cp = ca-1;
2784 return (ca != cp + 1) ? cp : NULL;
2787 typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
2789 void c99_vla_test(int size1, int size2)
2791 #if defined __i386__ || defined __x86_64__
2792 int size = size1 * size2;
2793 int tab1[size][2], tab2[10][2];
2794 void *tab1_ptr, *tab2_ptr, *bad_ptr;
2796 /* "size" should have been 'captured' at tab1 declaration,
2797 so modifying it should have no effect on VLA behaviour. */
2798 size = size-1;
2800 printf("Test C99 VLA 1 (sizeof): ");
2801 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
2802 tab1_ptr = tab1;
2803 tab2_ptr = tab2;
2804 printf("Test C99 VLA 2 (ptrs subtract): ");
2805 printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2806 printf("Test C99 VLA 3 (ptr add): ");
2807 printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2808 printf("Test C99 VLA 4 (ptr access): ");
2809 tab1[size1][1] = 42;
2810 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2812 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2813 if (bad_ptr = bounds_checking_is_enabled()) {
2814 int *t1 = &tab1[size1 * size2 - 1][3];
2815 int *t2 = &tab2[9][3];
2816 printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
2817 printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
2819 char*c1 = 1 + sizeof(tab1) + (char*)tab1;
2820 char*c2 = 1 + sizeof(tab2) + (char*)tab2;
2821 printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
2822 printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
2824 int *i1 = tab1[-1];
2825 int *i2 = tab2[-1];
2826 printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
2827 printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
2829 int *x1 = tab1[size1 * size2 + 1];
2830 int *x2 = tab2[10 + 1];
2831 printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
2832 printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
2833 } else {
2834 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2836 printf("\n");
2837 #endif
2840 #ifndef __TINYC__
2841 typedef __SIZE_TYPE__ uintptr_t;
2842 #endif
2844 void sizeof_test(void)
2846 int a;
2847 int **ptr;
2849 printf("sizeof(int) = %d\n", sizeof(int));
2850 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2851 printf("sizeof(long) = %d\n", sizeof(long));
2852 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2853 printf("sizeof(short) = %d\n", sizeof(short));
2854 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2855 printf("sizeof(char) = %d\n", sizeof(char));
2856 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2857 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2858 a = 1;
2859 printf("sizeof(a++) = %d\n", sizeof a++);
2860 printf("a=%d\n", a);
2861 ptr = NULL;
2862 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
2864 /* The type of sizeof should be as large as a pointer, actually
2865 it should be size_t. */
2866 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2867 uintptr_t t = 1;
2868 uintptr_t t2;
2869 /* Effectively <<32, but defined also on 32bit machines. */
2870 t <<= 16;
2871 t <<= 16;
2872 t++;
2873 /* This checks that sizeof really can be used to manipulate
2874 uintptr_t objects, without truncation. */
2875 t2 = t & -sizeof(uintptr_t);
2876 printf ("%lu %lu\n", t, t2);
2878 /* some alignof tests */
2879 printf("__alignof__(int) = %d\n", __alignof__(int));
2880 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2881 printf("__alignof__(short) = %d\n", __alignof__(short));
2882 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2883 printf("__alignof__(char) = %d\n", __alignof__(char));
2884 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2885 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
2887 /* sizes of VLAs need to be evaluated even inside sizeof: */
2888 a = 2;
2889 printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a]));
2890 /* And checking if sizeof compound literal works. Parenthesized: */
2891 printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n",
2892 sizeof( (struct {int i; int j;}){4,5} ));
2893 /* And as direct sizeof argument (as unary expression): */
2894 printf("sizeof (struct {short i; short j;}){4,5} = %d\n",
2895 sizeof (struct {short i; short j;}){4,5} );
2897 /* sizeof(x && y) should be sizeof(int), even if constant
2898 evaluating is possible. */
2899 printf("sizeof(t && 0) = %d\n", sizeof(t && 0));
2900 printf("sizeof(1 && 1) = %d\n", sizeof(1 && 1));
2901 printf("sizeof(t || 1) = %d\n", sizeof(t || 1));
2902 printf("sizeof(0 || 0) = %d\n", sizeof(0 || 0));
2905 void typeof_test(void)
2907 double a;
2908 typeof(a) b;
2909 typeof(float) c;
2911 a = 1.5;
2912 b = 2.5;
2913 c = 3.5;
2914 printf("a=%f b=%f c=%f\n", a, b, c);
2918 struct hlist_node;
2919 struct hlist_head {
2920 struct hlist_node *first, *last;
2923 void consume_ulong (unsigned long i)
2925 i = 0;
2928 void statement_expr_test(void)
2930 int a, i;
2932 /* Basic stmt expr test */
2933 a = 0;
2934 for(i=0;i<10;i++) {
2935 a += 1 +
2936 ( { int b, j;
2937 b = 0;
2938 for(j=0;j<5;j++)
2939 b += j; b;
2940 } );
2942 printf("a=%d\n", a);
2944 /* Test that symbols aren't freed prematurely.
2945 With SYM_DEBUG valgrind will show a read from a freed
2946 symbol, and tcc will show an (invalid) warning on the initialization
2947 of 'ptr' below, if symbols are popped after the stmt expr. */
2948 void *v = (void*)39;
2949 typeof(({
2950 (struct hlist_node *)v;
2951 })) x;
2952 typeof (x)
2953 ptr = (struct hlist_node *)v;
2955 /* This part used to segfault when symbols were popped prematurely.
2956 The symbols for the static local would be overwritten with
2957 helper symbols from the pre-processor expansions in between. */
2958 #define some_attr __attribute__((aligned(1)))
2959 #define tps(str) ({ \
2960 static const char *t some_attr = str; \
2961 t; \
2963 printf ("stmtexpr: %s %s\n",
2964 tps("somerandomlongstring"),
2965 tps("anotherlongstring"));
2967 /* Test that the three decls of 't' don't interact. */
2968 int t = 40;
2969 int b = ({ int t = 41; t; });
2970 int c = ({ int t = 42; t; });
2972 /* Test that aggregate return values work. */
2973 struct hlist_head h
2974 = ({
2975 typedef struct hlist_head T;
2976 long pre = 48;
2977 T t = { (void*)43, (void*)44 };
2978 long post = 49;
2981 printf ("stmtexpr: %d %d %d\n", t, b, c);
2982 printf ("stmtexpr: %ld %ld\n", (long)h.first, (long)h.last);
2984 /* Test that we can give out addresses of local labels. */
2985 consume_ulong(({ __label__ __here; __here: (unsigned long)&&__here; }));
2988 void local_label_test(void)
2990 int a;
2991 goto l1;
2993 a = 1 + ({
2994 __label__ l1, l2, l3, l4;
2995 goto l1;
2997 printf("aa1\n");
2998 goto l3;
3000 printf("aa3\n");
3001 goto l4;
3003 printf("aa2\n");
3004 goto l2;
3005 l3:;
3008 printf("a=%d\n", a);
3009 return;
3011 printf("bb1\n");
3012 goto l2;
3014 printf("bb2\n");
3015 goto l4;
3018 /* inline assembler test */
3019 #if defined(__i386__) || defined(__x86_64__)
3021 /* from linux kernel */
3022 static char * strncat1(char * dest,const char * src,size_t count)
3024 long d0, d1, d2, d3;
3025 __asm__ __volatile__(
3026 "repne\n\t"
3027 "scasb\n\t"
3028 "dec %1\n\t"
3029 "mov %8,%3\n"
3030 "1:\tdec %3\n\t"
3031 "js 2f\n\t"
3032 "lodsb\n\t"
3033 "stosb\n\t"
3034 "testb %%al,%%al\n\t"
3035 "jne 1b\n"
3036 "2:\txor %2,%2\n\t"
3037 "stosb"
3038 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
3039 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
3040 : "memory");
3041 return dest;
3044 static char * strncat2(char * dest,const char * src,size_t count)
3046 long d0, d1, d2, d3;
3047 __asm__ __volatile__(
3048 "repne scasb\n\t" /* one-line repne prefix + string op */
3049 "dec %1\n\t"
3050 "mov %8,%3\n"
3051 "1:\tdec %3\n\t"
3052 "js 2f\n\t"
3053 "lodsb\n\t"
3054 "stosb\n\t"
3055 "testb %%al,%%al\n\t"
3056 "jne 1b\n"
3057 "2:\txor %2,%2\n\t"
3058 "stosb"
3059 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
3060 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
3061 : "memory");
3062 return dest;
3065 static inline void * memcpy1(void * to, const void * from, size_t n)
3067 long d0, d1, d2;
3068 __asm__ __volatile__(
3069 "rep ; movsl\n\t"
3070 "testb $2,%b4\n\t"
3071 "je 1f\n\t"
3072 "movsw\n"
3073 "1:\ttestb $1,%b4\n\t"
3074 "je 2f\n\t"
3075 "movsb\n"
3076 "2:"
3077 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
3078 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
3079 : "memory");
3080 return (to);
3083 static inline void * memcpy2(void * to, const void * from, size_t n)
3085 long d0, d1, d2;
3086 __asm__ __volatile__(
3087 "rep movsl\n\t" /* one-line rep prefix + string op */
3088 "testb $2,%b4\n\t"
3089 "je 1f\n\t"
3090 "movsw\n"
3091 "1:\ttestb $1,%b4\n\t"
3092 "je 2f\n\t"
3093 "movsb\n"
3094 "2:"
3095 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
3096 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
3097 : "memory");
3098 return (to);
3101 static __inline__ void sigaddset1(unsigned int *set, int _sig)
3103 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
3106 static __inline__ void sigdelset1(unsigned int *set, int _sig)
3108 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc", "flags");
3111 static __inline__ __const__ unsigned int swab32(unsigned int x)
3113 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
3114 "rorl $16,%0\n\t" /* swap words */
3115 "xchgb %b0,%h0" /* swap higher bytes */
3116 :"=" "q" (x)
3117 : "0" (x));
3118 return x;
3121 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
3123 unsigned long long res;
3124 #ifdef __x86_64__
3125 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
3126 but still test the 32bit->64bit mull. */
3127 unsigned int resh, resl;
3128 __asm__("mull %2" : "=a" (resl), "=d" (resh) : "a" (a), "r" (b));
3129 res = ((unsigned long long)resh << 32) | resl;
3130 #else
3131 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
3132 #endif
3133 return res;
3136 static __inline__ unsigned long long inc64(unsigned long long a)
3138 unsigned long long res;
3139 #ifdef __x86_64__
3140 /* Using the A constraint is wrong, and increments are tested
3141 elsewhere. */
3142 res = a + 1;
3143 #else
3144 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
3145 #endif
3146 return res;
3149 struct struct123 {
3150 int a;
3151 int b;
3153 struct struct1231 {
3154 unsigned long addr;
3157 unsigned long mconstraint_test(struct struct1231 *r)
3159 unsigned long ret;
3160 unsigned int a[2];
3161 a[0] = 0;
3162 __asm__ volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
3163 : "=&r" (ret), "=m" (a)
3164 : "m" (*(struct struct123 *)r->addr));
3165 return ret + a[0];
3168 #ifdef __x86_64__
3169 int fls64(unsigned long long x)
3171 int bitpos = -1;
3172 asm("bsrq %1,%q0"
3173 : "+r" (bitpos)
3174 : "rm" (x));
3175 return bitpos + 1;
3177 #endif
3179 void other_constraints_test(void)
3181 unsigned long ret;
3182 int var;
3183 #ifndef _WIN64
3184 __asm__ volatile ("mov %P1,%0" : "=r" (ret) : "p" (&var));
3185 printf ("oc1: %d\n", ret == (unsigned long)&var);
3186 #endif
3189 #ifndef _WIN32
3190 /* Test global asm blocks playing with aliases. */
3191 void base_func(void)
3193 printf ("asmc: base\n");
3196 extern void override_func1 (void);
3197 extern void override_func2 (void);
3199 asm(".weak override_func1\n.set override_func1, base_func");
3200 asm(".set override_func1, base_func");
3201 asm(".set override_func2, base_func");
3203 void override_func2 (void)
3205 printf ("asmc: override2\n");
3208 /* This checks a construct used by the linux kernel to encode
3209 references to strings by PC relative references. */
3210 extern int bug_table[] __attribute__((section("__bug_table")));
3211 char * get_asm_string (void)
3213 extern int some_symbol;
3214 asm volatile (".globl some_symbol\n"
3215 "jmp .+6\n"
3216 "1:\n"
3217 "some_symbol: .long 0\n"
3218 ".pushsection __bug_table, \"a\"\n"
3219 ".globl bug_table\n"
3220 "bug_table:\n"
3221 /* The first entry (1b-2b) is unused in this test,
3222 but we include it to check if cross-section
3223 PC-relative references work. */
3224 "2:\t.long 1b - 2b, %c0 - 2b\n"
3225 ".popsection\n" : : "i" ("A string"));
3226 char * str = ((char*)bug_table) + bug_table[1];
3227 return str;
3230 /* This checks another constructs with local labels. */
3231 extern unsigned char alld_stuff[];
3232 asm(".data\n"
3233 ".byte 41\n"
3234 "alld_stuff:\n"
3235 "661:\n"
3236 ".byte 42\n"
3237 "662:\n"
3238 ".pushsection .data.ignore\n"
3239 ".long 661b - .\n" /* This reference to 661 generates an external sym
3240 which shouldn't somehow overwrite the offset that's
3241 already determined for it. */
3242 ".popsection\n"
3243 ".byte 662b - 661b\n" /* So that this value is undeniably 1. */);
3245 void asm_local_label_diff (void)
3247 printf ("asm_local_label_diff: %d %d\n", alld_stuff[0], alld_stuff[1]);
3250 /* This checks that static local variables are available from assembler. */
3251 void asm_local_statics (void)
3253 static int localint = 41;
3254 asm("incl %0" : "+m" (localint));
3255 printf ("asm_local_statics: %d\n", localint);
3257 #endif
3259 static
3260 unsigned int set;
3262 void fancy_copy (unsigned *in, unsigned *out)
3264 asm volatile ("" : "=r" (*out) : "0" (*in));
3267 void fancy_copy2 (unsigned *in, unsigned *out)
3269 asm volatile ("mov %0,(%1)" : : "r" (*in), "r" (out) : "memory");
3272 #if defined __x86_64__ && !defined _WIN64
3273 void clobber_r12(void)
3275 asm volatile("mov $1, %%r12" ::: "r12");
3277 #endif
3279 void test_high_clobbers(void)
3281 #if defined __x86_64__ && !defined _WIN64
3282 register long val asm("r12");
3283 long val2;
3284 /* This tests if asm clobbers correctly save/restore callee saved
3285 registers if they are clobbered and if it's the high 8 x86-64
3286 registers. This is fragile for GCC as the constraints do not
3287 correctly capture the data flow, but good enough for us. */
3288 asm volatile("mov $0x4542, %%r12" : "=r" (val):: "memory");
3289 clobber_r12();
3290 asm volatile("mov %%r12, %0" : "=r" (val2) : "r" (val): "memory");
3291 printf("asmhc: 0x%x\n", val2);
3292 #endif
3295 static long cpu_number;
3296 void trace_console(long len, long len2)
3298 #ifdef __x86_64__
3299 /* This generated invalid code when the emission of the switch
3300 table isn't disabled. The asms are necessary to show the bug,
3301 normal statements don't work (they need to generate some code
3302 even under nocode_wanted, which normal statements don't do,
3303 but asms do). Also at least these number of cases is necessary
3304 to generate enough "random" bytes. They ultimately are enough
3305 to create invalid instruction patterns to which the first
3306 skip-to-decision-table jump jumps. If decision table emission
3307 is disabled all of this is no problem.
3309 It also is necessary that the switches are in a statement expression
3310 (which has the property of not being enterable from outside. no
3311 matter what). */
3312 if (0
3315 long pscr_ret__;
3316 switch(len) {
3317 case 4:
3319 long pfo_ret__;
3320 switch (len2) {
3321 case 8: printf("bla"); pfo_ret__ = 42; break;
3323 pscr_ret__ = pfo_ret__;
3325 break;
3326 case 8:
3328 long pfo_ret__;
3329 switch (len2) {
3330 case 1:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3331 case 2:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3332 case 4:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3333 case 8:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3334 default: printf("impossible\n");
3336 pscr_ret__ = pfo_ret__;
3338 break;
3340 pscr_ret__;
3343 printf("huh?\n");
3345 #endif
3348 void test_asm_dead_code(void)
3350 long rdi;
3351 /* Try to make sure that xdi contains a zero, and hence will
3352 lead to a segfault if the next asm is evaluated without
3353 arguments being set up. */
3354 asm volatile ("" : "=D" (rdi) : "0" (0));
3355 (void)sizeof (({
3356 int var;
3357 /* This shouldn't trigger a segfault, either the argument
3358 registers need to be set up and the asm emitted despite
3359 this being in an unevaluated context, or both the argument
3360 setup _and_ the asm emission need to be suppressed. The latter
3361 is better. Disabling asm code gen when suppression is on
3362 also fixes the above trace_console bug, but that came earlier
3363 than asm suppression. */
3364 asm volatile ("movl $0,(%0)" : : "D" (&var) : "memory");
3365 var;
3366 }));
3369 void test_asm_call(void)
3371 #if defined __x86_64__ && !defined _WIN64
3372 static char str[] = "PATH";
3373 char *s;
3374 /* This tests if a reference to an undefined symbol from an asm
3375 block, which isn't otherwise referenced in this file, is correctly
3376 regarded as global symbol, so that it's resolved by other object files
3377 or libraries. We chose getenv here, which isn't used anywhere else
3378 in this file. (If we used e.g. printf, which is used we already
3379 would have a global symbol entry, not triggering the bug which is
3380 tested here). */
3381 /* two pushes so stack remains aligned */
3382 asm volatile ("push %%rdi; push %%rdi; mov %0, %%rdi;"
3383 #if 1 && !defined(__TINYC__) && (defined(__PIC__) || defined(__PIE__))
3384 "call getenv@plt;"
3385 #else
3386 "call getenv;"
3387 #endif
3388 "pop %%rdi; pop %%rdi"
3389 : "=a" (s) : "r" (str));
3390 printf("asmd: %s\n", s);
3391 #endif
3394 #if defined __x86_64__
3395 # define RX "(%rip)"
3396 #else
3397 # define RX
3398 #endif
3400 void asm_dot_test(void)
3402 int x;
3403 for (x = 1;; ++x) {
3404 int r = x;
3405 switch (x) {
3406 case 1:
3407 asm(".text; lea S"RX",%eax; lea ."RX",%ecx; sub %ecx,%eax; S=.; jmp p0");
3408 case 2:
3409 asm(".text; jmp .+6; .int 123; mov .-4"RX",%eax; jmp p0");
3410 case 3:
3411 asm(".data; Y=.; .int 999; X=Y; .int 456; X=.-4");
3412 asm(".text; mov X"RX",%eax; jmp p0");
3413 case 4:
3414 asm(".data; X=.; .int 789; Y=.; .int 999");
3415 asm(".text; mov X"RX",%eax; X=Y; jmp p0");
3416 case 0:
3417 asm(".text; p0=.; mov %%eax,%0;" : "=m"(r)); break;
3419 if (r == x)
3420 break;
3421 printf("asm_dot_test %d: %d\n", x, r);
3425 void asm_test(void)
3427 char buf[128];
3428 unsigned int val, val2;
3429 struct struct123 s1;
3430 struct struct1231 s2 = { (unsigned long)&s1 };
3431 /* Hide the outer base_func, but check later that the inline
3432 asm block gets the outer one. */
3433 int base_func = 42;
3434 void override_func3 (void);
3435 unsigned long asmret;
3436 #ifdef BOOL_ISOC99
3437 _Bool somebool;
3438 #endif
3439 register int regvar asm("%esi");
3441 printf("inline asm:\n");
3443 // parse 0x1E-1 as 3 tokens in asm mode
3444 asm volatile ("mov $0x1E-1,%eax");
3446 /* test the no operand case */
3447 asm volatile ("xorl %eax, %eax");
3449 memcpy1(buf, "hello", 6);
3450 strncat1(buf, " worldXXXXX", 3);
3451 printf("%s\n", buf);
3453 memcpy2(buf, "hello", 6);
3454 strncat2(buf, " worldXXXXX", 3);
3455 printf("%s\n", buf);
3457 /* 'A' constraint test */
3458 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
3459 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
3461 s1.a = 42;
3462 s1.b = 43;
3463 printf("mconstraint: %d", mconstraint_test(&s2));
3464 printf(" %d %d\n", s1.a, s1.b);
3465 other_constraints_test();
3466 set = 0xff;
3467 sigdelset1(&set, 2);
3468 sigaddset1(&set, 16);
3469 /* NOTE: we test here if C labels are correctly restored after the
3470 asm statement */
3471 goto label1;
3472 label2:
3473 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
3474 printf("set=0x%x\n", set);
3475 val = 0x01020304;
3476 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
3477 #ifndef _WIN32
3478 override_func1();
3479 override_func2();
3480 /* The base_func ref from the following inline asm should find
3481 the global one, not the local decl from this function. */
3482 asm volatile(".weak override_func3\n.set override_func3, base_func");
3483 override_func3();
3484 printf("asmstr: %s\n", get_asm_string());
3485 asm_local_label_diff();
3486 asm_local_statics();
3487 #endif
3488 /* Check that we can also load structs of appropriate layout
3489 into registers. */
3490 asm volatile("" : "=r" (asmret) : "0"(s2));
3491 if (asmret != s2.addr)
3492 printf("asmstr: failed\n");
3493 #ifdef BOOL_ISOC99
3494 /* Check that the typesize correctly sets the register size to
3495 8 bit. */
3496 asm volatile("cmp %1,%2; sete %0" : "=a"(somebool) : "r"(1), "r"(2));
3497 if (!somebool)
3498 printf("asmbool: failed\n");
3499 #endif
3500 val = 43;
3501 fancy_copy (&val, &val2);
3502 printf ("fancycpy(%d)=%d\n", val, val2);
3503 val = 44;
3504 fancy_copy2 (&val, &val2);
3505 printf ("fancycpy2(%d)=%d\n", val, val2);
3506 asm volatile ("mov $0x4243, %%esi" : "=r" (regvar));
3507 printf ("regvar=%x\n", regvar);
3508 test_high_clobbers();
3509 trace_console(8, 8);
3510 test_asm_dead_code();
3511 test_asm_call();
3512 asm_dot_test();
3513 return;
3514 label1:
3515 goto label2;
3518 #else
3520 void asm_test(void)
3524 #endif
3526 #define COMPAT_TYPE(type1, type2) \
3528 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
3529 __builtin_types_compatible_p (type1, type2));\
3532 int constant_p_var;
3534 void builtin_test(void)
3536 short s;
3537 int i;
3538 long long ll;
3539 #if GCC_MAJOR >= 3
3540 COMPAT_TYPE(int, int);
3541 COMPAT_TYPE(int, unsigned int);
3542 COMPAT_TYPE(int, char);
3543 COMPAT_TYPE(int, const int);
3544 COMPAT_TYPE(int, volatile int);
3545 COMPAT_TYPE(int *, int *);
3546 COMPAT_TYPE(int *, void *);
3547 COMPAT_TYPE(int *, const int *);
3548 COMPAT_TYPE(char *, unsigned char *);
3549 COMPAT_TYPE(char *, signed char *);
3550 COMPAT_TYPE(char *, char *);
3551 /* space is needed because tcc preprocessor introduces a space between each token */
3552 COMPAT_TYPE(char * *, void *);
3553 #endif
3554 printf("res = %d\n", __builtin_constant_p(1));
3555 printf("res = %d\n", __builtin_constant_p(1 + 2));
3556 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
3557 printf("res = %d\n", __builtin_constant_p(constant_p_var));
3558 printf("res = %d\n", __builtin_constant_p(100000 / constant_p_var));
3559 s = 1;
3560 ll = 2;
3561 i = __builtin_choose_expr (1 != 0, ll, s);
3562 printf("bce: %d\n", i);
3563 i = __builtin_choose_expr (1 != 1, ll, s);
3564 printf("bce: %d\n", i);
3565 i = sizeof (__builtin_choose_expr (1, ll, s));
3566 printf("bce: %d\n", i);
3567 i = sizeof (__builtin_choose_expr (0, ll, s));
3568 printf("bce: %d\n", i);
3570 //printf("bera: %p\n", __builtin_extract_return_addr((void*)43));
3573 #ifndef _WIN32
3574 extern int __attribute__((weak)) weak_f1(void);
3575 extern int __attribute__((weak)) weak_f2(void);
3576 extern int weak_f3(void);
3577 extern int __attribute__((weak)) weak_v1;
3578 extern int __attribute__((weak)) weak_v2;
3579 extern int weak_v3;
3581 extern int (*weak_fpa)() __attribute__((weak));
3582 extern int __attribute__((weak)) (*weak_fpb)();
3583 extern __attribute__((weak)) int (*weak_fpc)();
3585 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
3586 extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
3587 extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
3588 extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
3589 extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
3590 extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
3592 static const size_t dummy = 0;
3593 extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
3594 extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
3595 extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
3597 int some_lib_func(void);
3598 int dummy_impl_of_slf(void) { return 444; }
3599 int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
3601 int weak_toolate() __attribute__((weak));
3602 int weak_toolate() { return 0; }
3604 void __attribute__((weak)) weak_test(void)
3606 printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
3607 printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
3608 printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
3609 printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
3610 printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
3611 printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
3613 printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
3614 printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
3615 printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
3617 printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
3618 printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
3619 printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
3620 printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
3621 printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
3622 printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
3623 printf("some_lib_func=%d\n", &some_lib_func ? some_lib_func() : 0);
3626 int __attribute__((weak)) weak_f2() { return 222; }
3627 int __attribute__((weak)) weak_f3() { return 333; }
3628 int __attribute__((weak)) weak_v2 = 222;
3629 int __attribute__((weak)) weak_v3 = 333;
3630 #endif
3632 void const_func(const int a)
3636 void const_warn_test(void)
3638 const_func(1);
3641 struct condstruct {
3642 int i;
3645 int getme (struct condstruct *s, int i)
3647 int i1 = (i == 0 ? 0 : s)->i;
3648 int i2 = (i == 0 ? s : 0)->i;
3649 int i3 = (i == 0 ? (void*)0 : s)->i;
3650 int i4 = (i == 0 ? s : (void*)0)->i;
3651 return i1 + i2 + i3 + i4;
3654 struct global_data
3656 int a[40];
3657 int *b[40];
3660 struct global_data global_data;
3662 int global_data_getstuff (int *, int);
3664 void global_data_callit (int i)
3666 *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
3669 int global_data_getstuff (int *p, int i)
3671 return *p + i;
3674 void global_data_test (void)
3676 global_data.a[0] = 42;
3677 global_data.b[0] = &global_data.a[0];
3678 global_data_callit (0);
3679 printf ("%d\n", global_data.a[0]);
3682 struct cmpcmpS
3684 unsigned char fill : 3;
3685 unsigned char b1 : 1;
3686 unsigned char b2 : 1;
3687 unsigned char fill2 : 3;
3690 int glob1, glob2, glob3;
3692 void compare_comparisons (struct cmpcmpS *s)
3694 if (s->b1 != (glob1 == glob2)
3695 || (s->b2 != (glob1 == glob3)))
3696 printf ("comparing comparisons broken\n");
3699 void cmp_comparison_test(void)
3701 struct cmpcmpS s;
3702 s.b1 = 1;
3703 glob1 = 42; glob2 = 42;
3704 s.b2 = 0;
3705 glob3 = 43;
3706 compare_comparisons (&s);
3709 int fcompare (double a, double b, int code)
3711 switch (code) {
3712 case 0: return a == b;
3713 case 1: return a != b;
3714 case 2: return a < b;
3715 case 3: return a >= b;
3716 case 4: return a > b;
3717 case 5: return a <= b;
3721 void math_cmp_test(void)
3723 double nan = 0.0/0.0;
3724 double one = 1.0;
3725 double two = 2.0;
3726 int comp = 0;
3727 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
3729 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
3730 And it does this in various ways so that all code generation paths
3731 are checked (generating inverted tests, or non-inverted tests, or
3732 producing a 0/1 value without jumps (that's done in the fcompare
3733 function). */
3734 #define FCMP(a,b,op,iop,code) \
3735 if (fcompare (a,b,code)) \
3736 bug (a,b,op,iop,1); \
3737 if (a op b) \
3738 bug (a,b,op,iop,2); \
3739 if (a iop b) \
3741 else \
3742 bug (a,b,op,iop,3); \
3743 if ((a op b) || comp) \
3744 bug (a,b,op,iop,4); \
3745 if ((a iop b) || comp) \
3747 else \
3748 bug (a,b,op,iop,5);
3750 /* Equality tests. */
3751 FCMP(nan, nan, ==, !=, 0);
3752 FCMP(one, two, ==, !=, 0);
3753 FCMP(one, one, !=, ==, 1);
3754 /* Non-equality is a bit special. */
3755 if (!fcompare (nan, nan, 1))
3756 bug (nan, nan, !=, ==, 6);
3758 /* Relational tests on numbers. */
3759 FCMP(two, one, <, >=, 2);
3760 FCMP(one, two, >=, <, 3);
3761 FCMP(one, two, >, <=, 4);
3762 FCMP(two, one, <=, >, 5);
3764 /* Relational tests on NaNs. Note that the inverse op here is
3765 always !=, there's no operator in C that is equivalent to !(a < b),
3766 when NaNs are involved, same for the other relational ops. */
3767 FCMP(nan, nan, <, !=, 2);
3768 FCMP(nan, nan, >=, !=, 3);
3769 FCMP(nan, nan, >, !=, 4);
3770 FCMP(nan, nan, <=, !=, 5);
3773 double get100 () { return 100.0; }
3775 void callsave_test(void)
3777 #if defined __i386__ || defined __x86_64__ || defined __arm__
3778 int i, s; double *d; double t;
3779 s = sizeof (double);
3780 printf ("callsavetest: %d\n", s);
3781 d = alloca (sizeof(double));
3782 d[0] = 10.0;
3783 /* x86-64 had a bug were the next call to get100 would evict
3784 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
3785 in int type, not pointer type. When alloca returns a pointer
3786 with the high 32 bit set (which is likely on x86-64) the access
3787 generates a segfault. */
3788 i = d[0] > get100 ();
3789 printf ("%d\n", i);
3790 #endif
3794 void bfa3(ptrdiff_t str_offset)
3796 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
3798 void bfa2(ptrdiff_t str_offset)
3800 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
3801 bfa3(str_offset);
3803 void bfa1(ptrdiff_t str_offset)
3805 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
3806 bfa2(str_offset);
3809 void builtin_frame_address_test(void)
3811 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
3812 #ifndef __arm__
3813 char str[] = "__builtin_frame_address";
3814 char *fp0 = __builtin_frame_address(0);
3816 printf("str: %s\n", str);
3817 bfa1(str-fp0);
3818 #endif
3821 char via_volatile (char i)
3823 char volatile vi;
3824 vi = i;
3825 return vi;
3828 struct __attribute__((__packed__)) Spacked {
3829 char a;
3830 short b;
3831 int c;
3833 struct Spacked spacked;
3834 typedef struct __attribute__((__packed__)) {
3835 char a;
3836 short b;
3837 int c;
3838 } Spacked2;
3839 Spacked2 spacked2;
3840 typedef struct Spacked3_s {
3841 char a;
3842 short b;
3843 int c;
3844 } __attribute__((__packed__)) Spacked3;
3845 Spacked3 spacked3;
3846 struct gate_struct64 {
3847 unsigned short offset_low;
3848 unsigned short segment;
3849 unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1;
3850 unsigned short offset_middle;
3851 unsigned offset_high;
3852 unsigned zero1;
3853 } __attribute__((packed));
3854 typedef struct gate_struct64 gate_desc;
3855 gate_desc a_gate_desc;
3856 void attrib_test(void)
3858 #ifndef _WIN32
3859 printf("attr: %d %d %d %d\n", sizeof(struct Spacked),
3860 sizeof(spacked), sizeof(Spacked2), sizeof(spacked2));
3861 printf("attr: %d %d\n", sizeof(Spacked3), sizeof(spacked3));
3862 printf("attr: %d %d\n", sizeof(gate_desc), sizeof(a_gate_desc));
3863 #endif
3865 extern __attribute__((__unused__)) char * __attribute__((__unused__)) *
3866 strange_attrib_placement (void);
3868 void * __attribute__((__unused__)) get_void_ptr (void *a)
3870 return a;
3873 /* This part checks for a bug in TOK_GET (used for inline expansion),
3874 where the large long long constant left the the high bits set for
3875 the integer constant token. */
3876 static inline
3877 int __get_order(unsigned long long size)
3879 int order;
3880 size -= 0xffff880000000000ULL; // this const left high bits set in the token
3882 struct S { int i : 1; } s; // constructed for this '1'
3884 order = size;
3885 return order;
3888 /* This just forces the above inline function to be actually emitted. */
3889 int force_get_order(unsigned long s)
3891 return __get_order(s);