riscv: Handle some usual relocs
[tinycc.git] / tests / tcctest.c
blob5a959c2dc7440dbf7e2aea363af51672e52cb70d
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 scope_test2();
81 void forward_test();
82 void funcptr_test();
83 void loop_test();
84 void switch_test();
85 void goto_test();
86 void enum_test();
87 void typedef_test();
88 void struct_test();
89 void array_test();
90 void expr_ptr_test();
91 void bool_test();
92 void optimize_out();
93 void expr2_test();
94 void constant_expr_test();
95 void expr_cmp_test();
96 void char_short_test();
97 void init_test(void);
98 void compound_literal_test(void);
99 int kr_test();
100 void struct_assign_test(void);
101 void cast_test(void);
102 void bitfield_test(void);
103 void c99_bool_test(void);
104 void float_test(void);
105 void longlong_test(void);
106 void manyarg_test(void);
107 void stdarg_test(void);
108 void whitespace_test(void);
109 void relocation_test(void);
110 void old_style_function(void);
111 void alloca_test(void);
112 void c99_vla_test(int size1, int size2);
113 void sizeof_test(void);
114 void typeof_test(void);
115 void local_label_test(void);
116 void statement_expr_test(void);
117 void asm_test(void);
118 void builtin_test(void);
119 void weak_test(void);
120 void global_data_test(void);
121 void cmp_comparison_test(void);
122 void math_cmp_test(void);
123 void callsave_test(void);
124 void builtin_frame_address_test(void);
125 void attrib_test(void);
127 int fib(int n);
128 void num(int n);
129 void forward_ref(void);
130 int isid(int c);
132 /* Line joining happens before tokenization, so the following
133 must be parsed as ellipsis. */
134 void funny_line_continuation (int, ..\
135 . );
137 char via_volatile (char);
139 #define A 2
140 #define N 1234 + A
141 #define pf printf
142 #define M1(a, b) (a) + (b)
144 #define str\
145 (s) # s
146 #define glue(a, b) a ## b
147 #define xglue(a, b) glue(a, b)
148 #define HIGHLOW "hello"
149 #define LOW LOW ", world"
151 static int onetwothree = 123;
152 #define onetwothree4 onetwothree
153 #define onetwothree xglue(onetwothree,4)
155 #define min(a, b) ((a) < (b) ? (a) : (b))
157 #ifdef C99_MACROS
158 #define dprintf(level,...) printf(__VA_ARGS__)
159 #endif
161 /* gcc vararg macros */
162 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
164 #define MACRO_NOARGS()
166 #define AAA 3
167 #undef AAA
168 #define AAA 4
170 #if 1
171 #define B3 1
172 #elif 1
173 #define B3 2
174 #elif 0
175 #define B3 3
176 #else
177 #define B3 4
178 #endif
180 #ifdef __TINYC__
181 /* We try to handle this syntax. Make at least sure it doesn't segfault. */
182 char invalid_function_def()[] {return 0;}
183 #endif
185 #define __INT64_C(c) c ## LL
186 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
188 int qq(int x)
190 return x + 40;
192 #define qq(x) x
194 #define spin_lock(lock) do { } while (0)
195 #define wq_spin_lock spin_lock
196 #define TEST2() wq_spin_lock(a)
198 #define UINT_MAX ((unsigned) -1)
200 void intdiv_test(void)
202 printf("18/21=%u\n", 18/21);
203 printf("18%%21=%u\n", 18%21);
204 printf("41/21=%u\n", 41/21);
205 printf("41%%21=%u\n", 41%21);
206 printf("42/21=%u\n", 42/21);
207 printf("42%%21=%u\n", 42%21);
208 printf("43/21=%u\n", 43/21);
209 printf("43%%21=%u\n", 43%21);
210 printf("126/21=%u\n", 126/21);
211 printf("126%%21=%u\n", 126%21);
212 printf("131/21=%u\n", 131/21);
213 printf("131%%21=%u\n", 131%21);
214 printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX/2+3)/2);
215 printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX/2+3)%2);
217 printf("18/-21=%u\n", 18/-21);
218 printf("18%%-21=%u\n", 18%-21);
219 printf("41/-21=%u\n", 41/-21);
220 printf("41%%-21=%u\n", 41%-21);
221 printf("42/-21=%u\n", 42/-21);
222 printf("42%%-21=%u\n", 42%-21);
223 printf("43/-21=%u\n", 43/-21);
224 printf("43%%-21=%u\n", 43%-21);
225 printf("126/-21=%u\n", 126/-21);
226 printf("126%%-21=%u\n", 126%-21);
227 printf("131/-21=%u\n", 131/-21);
228 printf("131%%-21=%u\n", 131%-21);
229 printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX/2+3)/-2);
230 printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX/2+3)%-2);
232 printf("-18/21=%u\n", -18/21);
233 printf("-18%%21=%u\n", -18%21);
234 printf("-41/21=%u\n", -41/21);
235 printf("-41%%21=%u\n", -41%21);
236 printf("-42/21=%u\n", -42/21);
237 printf("-42%%21=%u\n", -42%21);
238 printf("-43/21=%u\n", -43/21);
239 printf("-43%%21=%u\n", -43%21);
240 printf("-126/21=%u\n", -126/21);
241 printf("-126%%21=%u\n", -126%21);
242 printf("-131/21=%u\n", -131/21);
243 printf("-131%%21=%u\n", -131%21);
244 printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX/2+3))/2);
245 printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX/2+3))%2);
247 printf("-18/-21=%u\n", -18/-21);
248 printf("-18%%-21=%u\n", -18%-21);
249 printf("-41/-21=%u\n", -41/-21);
250 printf("-41%%-21=%u\n", -41%-21);
251 printf("-42/-21=%u\n", -42/-21);
252 printf("-42%%-21=%u\n", -42%-21);
253 printf("-43/-21=%u\n", -43/-21);
254 printf("-43%%-21=%u\n", -43%-21);
255 printf("-126/-21=%u\n", -126/-21);
256 printf("-126%%-21=%u\n", -126%-21);
257 printf("-131/-21=%u\n", -131/-21);
258 printf("-131%%-21=%u\n", -131%-21);
259 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX/2+3))/-2);
260 printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX/2+3))%-2);
263 void macro_test(void)
265 printf("macro:\n");\f\v
266 pf("N=%d\n", N);
267 printf("aaa=%d\n", AAA);
269 printf("min=%d\n", min(1, min(2, -1)));
271 printf("s1=%s\n", glue(HIGH, LOW));
272 printf("s2=%s\n", xglue(HIGH, LOW));
273 printf("s3=%s\n", str("c"));
274 printf("s4=%s\n", str(a1));
275 printf("B3=%d\n", B3);
277 printf("onetwothree=%d\n", onetwothree);
279 #ifdef A
280 printf("A defined\n");
281 #endif
282 #ifdef B
283 printf("B defined\n");
284 #endif
285 #ifdef A
286 printf("A defined\n");
287 #else
288 printf("A not defined\n");
289 #endif
290 #ifdef B
291 printf("B defined\n");
292 #else
293 printf("B not defined\n");
294 #endif
296 #ifdef A
297 printf("A defined\n");
298 #ifdef B
299 printf("B1 defined\n");
300 #else
301 printf("B1 not defined\n");
302 #endif
303 #else
304 printf("A not defined\n");
305 #ifdef B
306 printf("B2 defined\n");
307 #else
308 printf("B2 not defined\n");
309 #endif
310 #endif
312 #if 1+1
313 printf("test true1\n");
314 #endif
315 #if 0
316 printf("test true2\n");
317 #endif
318 #if 1-1
319 printf("test true3\n");
320 #endif
321 #if defined(A)
322 printf("test trueA\n");
323 #endif
324 #if defined(B)
325 printf("test trueB\n");
326 #endif
328 #if 0
329 printf("test 0\n");
330 #elif 0
331 printf("test 1\n");
332 #elif 2
333 printf("test 2\n");
334 #else
335 printf("test 3\n");
336 #endif
338 MACRO_NOARGS();
340 #ifdef __LINE__
341 printf("__LINE__ defined\n");
342 #endif
344 printf("__LINE__=%d __FILE__=%s\n",
345 __LINE__, __FILE__);
346 #if 0
347 #line 200
348 printf("__LINE__=%d __FILE__=%s\n",
349 __LINE__, __FILE__);
350 #line 203 "test"
351 printf("__LINE__=%d __FILE__=%s\n",
352 __LINE__, __FILE__);
353 #line 227 "tcctest.c"
354 #endif
356 /* not strictly preprocessor, but we test it there */
357 #ifdef C99_MACROS
358 printf("__func__ = %s\n", __func__);
359 dprintf(1, "vaarg=%d\n", 1);
360 #endif
361 dprintf1(1, "vaarg1\n");
362 dprintf1(1, "vaarg1=%d\n", 2);
363 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
365 /* gcc extension */
366 printf("func='%s'\n", __FUNCTION__);
368 /* complicated macros in glibc */
369 printf("INT64_MIN=" LONG_LONG_FORMAT "\n", INT64_MIN);
371 int a;
372 a = 1;
373 glue(a+, +);
374 printf("a=%d\n", a);
375 glue(a <, <= 2);
376 printf("a=%d\n", a);
379 /* macro function with argument outside the macro string */
380 #define MF_s MF_hello
381 #define MF_hello(msg) printf("%s\n",msg)
383 #define MF_t printf("tralala\n"); MF_hello
385 MF_s("hi");
386 MF_t("hi");
388 /* test macro substitution inside args (should not eat stream) */
389 printf("qq=%d\n", qq(qq)(2));
391 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
392 null argument without a space. gcc 3.2 fixes that. */
394 #define qq1(x) 1
395 printf("qq1=%d\n", qq1( ));
397 /* comment with stray handling *\
399 /* this is a valid *\/ comment */
400 /* this is a valid comment *\*/
401 // this is a valid\
402 comment
404 /* test function macro substitution when the function name is
405 substituted */
406 TEST2();
408 /* And again when the name and parentheses are separated by a
409 comment. */
410 TEST2 /* the comment */ ();
412 printf("%s\n", get_basefile_from_header());
413 printf("%s\n", __BASE_FILE__);
414 printf("%s\n", get_file_from_header());
415 printf("%s\n", __FILE__);
417 /* Check that funnily named include was in fact included */
418 have_included_42test_h = 1;
419 have_included_42test_h_second = 1;
420 have_included_42test_h_third = 1;
424 static void print_num(char *fn, int line, int num) {
425 printf("fn %s, line %d, num %d\n", fn, line, num);
428 void recursive_macro_test(void)
431 #define ELF32_ST_TYPE(val) ((val) & 0xf)
432 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
433 #define STB_WEAK 2 /* Weak symbol */
434 #define ELFW(type) ELF##32##_##type
435 printf("%d\n", ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(123)));
437 #define WRAP(x) x
439 #define print_num(x) print_num(__FILE__,__LINE__,x)
440 print_num(123);
441 WRAP(print_num(123));
442 WRAP(WRAP(print_num(123)));
444 static struct recursive_macro { int rm_field; } G;
445 #define rm_field (G.rm_field)
446 printf("rm_field = %d\n", rm_field);
447 printf("rm_field = %d\n", WRAP(rm_field));
448 WRAP((printf("rm_field = %d %d\n", rm_field, WRAP(rm_field))));
451 int op(a,b)
453 return a / b;
456 int ret(a)
458 if (a == 2)
459 return 1;
460 if (a == 3)
461 return 2;
462 return 0;
465 void ps(const char *s)
467 int c;
468 while (1) {
469 c = *s;
470 if (c == 0)
471 break;
472 printf("%c", c);
473 s++;
477 const char foo1_string[] = "\
478 bar\n\
479 test\14\
482 void string_test()
484 unsigned int b;
485 printf("string:\n");
486 printf("\141\1423\143\n");/* dezdez test */
487 printf("\x41\x42\x43\x3a\n");
488 printf("c=%c\n", 'r');
489 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
490 printf("foo1_string='%s'\n", foo1_string);
491 #if 0
492 printf("wstring=%S\n", L"abc");
493 printf("wstring=%S\n", L"abc" L"def" "ghi");
494 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
495 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
496 #endif
497 ps("test\n");
498 b = 32;
499 while ((b = b + 1) < 96) {
500 printf("%c", b);
502 printf("\n");
503 printf("fib=%d\n", fib(33));
504 b = 262144;
505 while (b != 0x80000000) {
506 num(b);
507 b = b * 2;
511 void loop_test()
513 int i;
514 i = 0;
515 while (i < 10)
516 printf("%d", i++);
517 printf("\n");
518 for(i = 0; i < 10;i++)
519 printf("%d", i);
520 printf("\n");
521 i = 0;
522 do {
523 printf("%d", i++);
524 } while (i < 10);
525 printf("\n");
527 char count = 123;
528 /* c99 for loop init test */
529 for (size_t count = 1; count < 3; count++)
530 printf("count=%d\n", count);
531 printf("count = %d\n", count);
533 /* break/continue tests */
534 i = 0;
535 while (1) {
536 if (i == 6)
537 break;
538 i++;
539 if (i == 3)
540 continue;
541 printf("%d", i);
543 printf("\n");
545 /* break/continue tests */
546 i = 0;
547 do {
548 if (i == 6)
549 break;
550 i++;
551 if (i == 3)
552 continue;
553 printf("%d", i);
554 } while(1);
555 printf("\n");
557 for(i = 0;i < 10;i++) {
558 if (i == 3)
559 continue;
560 printf("%d", i);
562 printf("\n");
565 typedef int typedef_and_label;
567 void goto_test()
569 int i;
570 static void *label_table[3] = { &&label1, &&label2, &&label3 };
572 printf("goto:\n");
573 i = 0;
574 /* This needs to parse as label, not as start of decl. */
575 typedef_and_label x;
576 typedef_and_label:
577 s_loop:
578 if (i >= 10)
579 goto s_end;
580 printf("%d", i);
581 i++;
582 goto s_loop;
583 s_end:
584 printf("\n");
586 /* we also test computed gotos (GCC extension) */
587 for(i=0;i<3;i++) {
588 goto *label_table[i];
589 label1:
590 printf("label1\n");
591 goto next;
592 label2:
593 printf("label2\n");
594 goto next;
595 label3:
596 printf("label3\n");
597 next: ;
601 enum {
603 E1 = 2,
604 E2 = 4,
609 enum test {
610 E5 = 1000,
613 struct S_enum {
614 enum {E6 = 42, E7, E8} e:8;
617 enum ELong {
618 /* This is either 0 on L32 machines, or a large number
619 on L64 machines. We should be able to store this. */
620 EL_large = ((unsigned long)0xf000 << 31) << 1,
623 enum { BIASU = -1U<<31 };
624 enum { BIASS = -1 << 31 };
626 static int getint(int i)
628 if (i)
629 return 0;
630 else
631 return (int)(-1U << 31);
634 void enum_test()
636 enum test b1;
637 /* The following should give no warning */
638 unsigned *p = &b1;
639 struct S_enum s = {E7};
640 printf("enum: %d\n", s.e);
641 printf("enum:\n%d %d %d %d %d %d\n",
642 E0, E1, E2, E3, E4, E5);
643 b1 = 1;
644 printf("b1=%d\n", b1);
645 printf("enum large: %ld\n", EL_large);
647 if (getint(0) == BIASU)
648 printf("enum unsigned: ok\n");
649 else
650 printf("enum unsigned: wrong\n");
651 if (getint(0) == BIASS)
652 printf("enum unsigned: ok\n");
653 else
654 printf("enum unsigned: wrong\n");
657 typedef int *my_ptr;
659 typedef int mytype1;
660 typedef int mytype2;
662 void typedef_test()
664 my_ptr a;
665 mytype1 mytype2;
666 int b;
668 a = &b;
669 *a = 1234;
670 printf("typedef:\n");
671 printf("a=%d\n", *a);
672 mytype2 = 2;
673 printf("mytype2=%d\n", mytype2);
676 void forward_test()
678 printf("forward:\n");
679 forward_ref();
680 forward_ref();
684 void forward_ref(void)
686 printf("forward ok\n");
689 typedef struct struct1 {
690 int f1;
691 int f2, f3;
692 union union1 {
693 int v1;
694 int v2;
695 } u;
696 char str[3];
697 } struct1;
699 struct struct2 {
700 int a;
701 char b;
704 union union2 {
705 int w1;
706 int w2;
709 struct struct1 st1, st2;
711 struct empty_mem {
712 /* nothing */ ;
713 int x;
716 int main(int argc, char **argv)
718 string_test();
719 expr_test();
720 macro_test();
721 recursive_macro_test();
722 scope_test();
723 scope_test2();
724 forward_test();
725 funcptr_test();
726 loop_test();
727 switch_test();
728 goto_test();
729 enum_test();
730 typedef_test();
731 struct_test();
732 array_test();
733 expr_ptr_test();
734 bool_test();
735 optimize_out();
736 expr2_test();
737 constant_expr_test();
738 expr_cmp_test();
739 char_short_test();
740 init_test();
741 compound_literal_test();
742 kr_test();
743 struct_assign_test();
744 cast_test();
745 bitfield_test();
746 c99_bool_test();
747 float_test();
748 longlong_test();
749 manyarg_test();
750 stdarg_test();
751 whitespace_test();
752 relocation_test();
753 old_style_function();
754 alloca_test();
755 c99_vla_test(5, 2);
756 sizeof_test();
757 typeof_test();
758 statement_expr_test();
759 local_label_test();
760 asm_test();
761 builtin_test();
762 #ifndef _WIN32
763 weak_test();
764 #endif
765 global_data_test();
766 cmp_comparison_test();
767 math_cmp_test();
768 callsave_test();
769 builtin_frame_address_test();
770 intdiv_test();
771 if (via_volatile (42) != 42)
772 printf ("via_volatile broken\n");
773 attrib_test();
774 return 0;
777 int tab[3];
778 int tab2[3][2];
780 int g;
782 void f1(g)
784 printf("g1=%d\n", g);
787 void scope_test()
789 printf("scope:\n");
790 g = 2;
791 f1(1);
792 printf("g2=%d\n", g);
794 int g;
795 g = 3;
796 printf("g3=%d\n", g);
798 int g;
799 g = 4;
800 printf("g4=%d\n", g);
803 printf("g5=%d\n", g);
806 int st2_i;
807 int *st2_p = &st2_i;
808 void scope_test2()
810 char a[50];
811 st2_i = 42;
812 for (int st2_i = 1; st2_i < 10; st2_i++) {
813 extern int st2_i;
814 st2_i++;
815 printf("exloc: %d\n", st2_i);
817 printf("exloc: %d\n", *st2_p);
820 /* C has tentative definition, and they may be repeated. */
821 extern int st_global1;
822 int st_global1=42;
823 extern int st_global1;
824 int st_global1;
825 extern int st_global2;
826 int st_global2;
827 extern int st_global2;
828 int st_global2;
830 void array_test()
832 int i, j, a[4];
834 printf("array:\n");
835 printf("sizeof(a) = %d\n", sizeof(a));
836 printf("sizeof(\"a\") = %d\n", sizeof("a"));
837 #ifdef C99_MACROS
838 printf("sizeof(__func__) = %d\n", sizeof(__func__));
839 #endif
840 printf("sizeof tab %d\n", sizeof(tab));
841 printf("sizeof tab2 %d\n", sizeof tab2);
842 tab[0] = 1;
843 tab[1] = 2;
844 tab[2] = 3;
845 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
846 for(i=0;i<3;i++)
847 for(j=0;j<2;j++)
848 tab2[i][j] = 10 * i + j;
849 for(i=0;i<3*2;i++) {
850 printf(" %3d", ((int *)tab2)[i]);
852 printf("\n");
853 printf("sizeof(size_t)=%d\n", sizeof(size_t));
854 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
857 void expr_test()
859 int a, b;
860 a = 0;
861 printf("%d\n", a += 1);
862 printf("%d\n", a -= 2);
863 printf("%d\n", a *= 31232132);
864 printf("%d\n", a /= 4);
865 printf("%d\n", a %= 20);
866 printf("%d\n", a &= 6);
867 printf("%d\n", a ^= 7);
868 printf("%d\n", a |= 8);
869 printf("%d\n", a >>= 3);
870 printf("%d\n", a <<= 4);
872 a = 22321;
873 b = -22321;
874 printf("%d\n", a + 1);
875 printf("%d\n", a - 2);
876 printf("%d\n", a * 312);
877 printf("%d\n", a / 4);
878 printf("%d\n", b / 4);
879 printf("%d\n", (unsigned)b / 4);
880 printf("%d\n", a % 20);
881 printf("%d\n", b % 20);
882 printf("%d\n", (unsigned)b % 20);
883 printf("%d\n", a & 6);
884 printf("%d\n", a ^ 7);
885 printf("%d\n", a | 8);
886 printf("%d\n", a >> 3);
887 printf("%d\n", b >> 3);
888 printf("%d\n", (unsigned)b >> 3);
889 printf("%d\n", a << 4);
890 printf("%d\n", ~a);
891 printf("%d\n", -a);
892 printf("%d\n", +a);
894 printf("%d\n", 12 + 1);
895 printf("%d\n", 12 - 2);
896 printf("%d\n", 12 * 312);
897 printf("%d\n", 12 / 4);
898 printf("%d\n", 12 % 20);
899 printf("%d\n", 12 & 6);
900 printf("%d\n", 12 ^ 7);
901 printf("%d\n", 12 | 8);
902 printf("%d\n", 12 >> 2);
903 printf("%d\n", 12 << 4);
904 printf("%d\n", ~12);
905 printf("%d\n", -12);
906 printf("%d\n", +12);
907 printf("%d %d %d %d\n",
908 isid('a'),
909 isid('g'),
910 isid('T'),
911 isid('('));
914 int isid(int c)
916 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
919 /**********************/
921 int vstack[10], *vstack_ptr;
923 void vpush(int vt, int vc)
925 *vstack_ptr++ = vt;
926 *vstack_ptr++ = vc;
929 void vpop(int *ft, int *fc)
931 *fc = *--vstack_ptr;
932 *ft = *--vstack_ptr;
935 void expr2_test()
937 int a, b;
939 printf("expr2:\n");
940 vstack_ptr = vstack;
941 vpush(1432432, 2);
942 vstack_ptr[-2] &= ~0xffffff80;
943 vpop(&a, &b);
944 printf("res= %d %d\n", a, b);
947 void constant_expr_test()
949 int a;
950 printf("constant_expr:\n");
951 a = 3;
952 printf("%d\n", a * 16);
953 printf("%d\n", a * 1);
954 printf("%d\n", a + 0);
957 int tab4[10];
959 void expr_ptr_test()
961 int *p, *q;
962 int i = -1;
964 printf("expr_ptr:\n");
965 p = tab4;
966 q = tab4 + 10;
967 printf("diff=%d\n", q - p);
968 p++;
969 printf("inc=%d\n", p - tab4);
970 p--;
971 printf("dec=%d\n", p - tab4);
972 ++p;
973 printf("inc=%d\n", p - tab4);
974 --p;
975 printf("dec=%d\n", p - tab4);
976 printf("add=%d\n", p + 3 - tab4);
977 printf("add=%d\n", 3 + p - tab4);
979 /* check if 64bit support is ok */
980 q = p = 0;
981 q += i;
982 printf("%p %p %ld\n", q, p, p-q);
983 printf("%d %d %d %d %d %d\n",
984 p == q, p != q, p < q, p <= q, p >= q, p > q);
985 i = 0xf0000000;
986 p += i;
987 printf("%p %p %ld\n", q, p, p-q);
988 printf("%d %d %d %d %d %d\n",
989 p == q, p != q, p < q, p <= q, p >= q, p > q);
990 p = (int *)((char *)p + 0xf0000000);
991 printf("%p %p %ld\n", q, p, p-q);
992 printf("%d %d %d %d %d %d\n",
993 p == q, p != q, p < q, p <= q, p >= q, p > q);
994 p += 0xf0000000;
995 printf("%p %p %ld\n", q, p, p-q);
996 printf("%d %d %d %d %d %d\n",
997 p == q, p != q, p < q, p <= q, p >= q, p > q);
999 struct size12 {
1000 int i, j, k;
1002 struct size12 s[2], *sp = s;
1003 int i, j;
1004 sp->i = 42;
1005 sp++;
1006 j = -1;
1007 printf("%d\n", sp[j].i);
1009 #ifdef __LP64__
1010 i = 1;
1011 p = (int*)0x100000000UL + i;
1012 i = ((long)p) >> 32;
1013 printf("largeptr: %p %d\n", p, i);
1014 #endif
1017 void expr_cmp_test()
1019 int a, b;
1020 printf("constant_expr:\n");
1021 a = -1;
1022 b = 1;
1023 printf("%d\n", a == a);
1024 printf("%d\n", a != a);
1026 printf("%d\n", a < b);
1027 printf("%d\n", a <= b);
1028 printf("%d\n", a <= a);
1029 printf("%d\n", b >= a);
1030 printf("%d\n", a >= a);
1031 printf("%d\n", b > a);
1033 printf("%d\n", (unsigned)a < b);
1034 printf("%d\n", (unsigned)a <= b);
1035 printf("%d\n", (unsigned)a <= a);
1036 printf("%d\n", (unsigned)b >= a);
1037 printf("%d\n", (unsigned)a >= a);
1038 printf("%d\n", (unsigned)b > a);
1041 struct empty {
1044 struct aligntest1 {
1045 char a[10];
1048 struct aligntest2 {
1049 int a;
1050 char b[10];
1053 struct aligntest3 {
1054 double a, b;
1057 struct aligntest4 {
1058 double a[0];
1061 struct __attribute__((aligned(16))) aligntest5
1063 int i;
1065 struct aligntest6
1067 int i;
1068 } __attribute__((aligned(16)));
1069 struct aligntest7
1071 int i;
1073 struct aligntest5 altest5[2];
1074 struct aligntest6 altest6[2];
1075 int pad1;
1076 /* altest7 is correctly aligned to 16 bytes also with TCC,
1077 but __alignof__ returns the wrong result (4) because we
1078 can't store the alignment yet when specified on symbols
1079 directly (it's stored in the type so we'd need to make
1080 a copy of it). -- FIXED */
1081 struct aligntest7 altest7[2] __attribute__((aligned(16)));
1083 struct aligntest8
1085 int i;
1086 } __attribute__((aligned(4096)));
1088 struct Large {
1089 unsigned long flags;
1090 union {
1091 void *u1;
1092 int *u2;
1095 struct {
1096 union {
1097 unsigned long index;
1098 void *freelist;
1100 union {
1101 unsigned long counters;
1102 struct {
1103 int bla;
1108 union {
1109 struct {
1110 long u3;
1111 long u4;
1113 void *u5;
1114 struct {
1115 unsigned long compound_head;
1116 unsigned int compound_dtor;
1117 unsigned int compound_order;
1120 } __attribute__((aligned(2 * sizeof(long))));
1122 typedef unsigned long long __attribute__((aligned(4))) unaligned_u64;
1124 struct aligntest9 {
1125 unsigned int buf_nr;
1126 unaligned_u64 start_lba;
1129 struct aligntest10 {
1130 unsigned int buf_nr;
1131 unsigned long long start_lba;
1134 void struct_test()
1136 struct1 *s;
1137 union union2 u;
1138 struct Large ls;
1140 printf("struct:\n");
1141 printf("sizes: %d %d %d %d\n",
1142 sizeof(struct struct1),
1143 sizeof(struct struct2),
1144 sizeof(union union1),
1145 sizeof(union union2));
1146 printf("offsets: %d\n", (int)((char*)&st1.u.v1 - (char*)&st1));
1147 st1.f1 = 1;
1148 st1.f2 = 2;
1149 st1.f3 = 3;
1150 printf("st1: %d %d %d\n",
1151 st1.f1, st1.f2, st1.f3);
1152 st1.u.v1 = 1;
1153 st1.u.v2 = 2;
1154 printf("union1: %d\n", st1.u.v1);
1155 u.w1 = 1;
1156 u.w2 = 2;
1157 printf("union2: %d\n", u.w1);
1158 s = &st2;
1159 s->f1 = 3;
1160 s->f2 = 2;
1161 s->f3 = 1;
1162 printf("st2: %d %d %d\n",
1163 s->f1, s->f2, s->f3);
1164 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
1166 /* align / size tests */
1167 printf("aligntest1 sizeof=%d alignof=%d\n",
1168 sizeof(struct aligntest1), __alignof__(struct aligntest1));
1169 printf("aligntest2 sizeof=%d alignof=%d\n",
1170 sizeof(struct aligntest2), __alignof__(struct aligntest2));
1171 printf("aligntest3 sizeof=%d alignof=%d\n",
1172 sizeof(struct aligntest3), __alignof__(struct aligntest3));
1173 printf("aligntest4 sizeof=%d alignof=%d\n",
1174 sizeof(struct aligntest4), __alignof__(struct aligntest4));
1175 printf("aligntest5 sizeof=%d alignof=%d\n",
1176 sizeof(struct aligntest5), __alignof__(struct aligntest5));
1177 printf("aligntest6 sizeof=%d alignof=%d\n",
1178 sizeof(struct aligntest6), __alignof__(struct aligntest6));
1179 printf("aligntest7 sizeof=%d alignof=%d\n",
1180 sizeof(struct aligntest7), __alignof__(struct aligntest7));
1181 printf("aligntest8 sizeof=%d alignof=%d\n",
1182 sizeof(struct aligntest8), __alignof__(struct aligntest8));
1183 printf("aligntest9 sizeof=%d alignof=%d\n",
1184 sizeof(struct aligntest9), __alignof__(struct aligntest9));
1185 printf("aligntest10 sizeof=%d alignof=%d\n",
1186 sizeof(struct aligntest10), __alignof__(struct aligntest10));
1187 printf("altest5 sizeof=%d alignof=%d\n",
1188 sizeof(altest5), __alignof__(altest5));
1189 printf("altest6 sizeof=%d alignof=%d\n",
1190 sizeof(altest6), __alignof__(altest6));
1191 printf("altest7 sizeof=%d alignof=%d\n",
1192 sizeof(altest7), __alignof__(altest7));
1194 /* empty structures (GCC extension) */
1195 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
1196 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
1198 printf("Large: sizeof=%d\n", sizeof(ls));
1199 memset(&ls, 0, sizeof(ls));
1200 ls.compound_head = 42;
1201 printf("Large: offsetof(compound_head)=%d\n", (int)((char*)&ls.compound_head - (char*)&ls));
1204 /* XXX: depend on endianness */
1205 void char_short_test()
1207 int var1, var2;
1209 printf("char_short:\n");
1211 var1 = 0x01020304;
1212 var2 = 0xfffefdfc;
1213 printf("s8=%d %d\n",
1214 *(char *)&var1, *(char *)&var2);
1215 printf("u8=%d %d\n",
1216 *(unsigned char *)&var1, *(unsigned char *)&var2);
1217 printf("s16=%d %d\n",
1218 *(short *)&var1, *(short *)&var2);
1219 printf("u16=%d %d\n",
1220 *(unsigned short *)&var1, *(unsigned short *)&var2);
1221 printf("s32=%d %d\n",
1222 *(int *)&var1, *(int *)&var2);
1223 printf("u32=%d %d\n",
1224 *(unsigned int *)&var1, *(unsigned int *)&var2);
1225 *(char *)&var1 = 0x08;
1226 printf("var1=%x\n", var1);
1227 *(short *)&var1 = 0x0809;
1228 printf("var1=%x\n", var1);
1229 *(int *)&var1 = 0x08090a0b;
1230 printf("var1=%x\n", var1);
1233 /******************/
1235 typedef struct Sym {
1236 int v;
1237 int t;
1238 int c;
1239 struct Sym *next;
1240 struct Sym *prev;
1241 } Sym;
1243 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1244 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1246 static int toupper1(int a)
1248 return TOUPPER(a);
1251 static unsigned int calc_vm_flags(unsigned int prot)
1253 unsigned int prot_bits;
1254 /* This used to segfault in some revisions: */
1255 prot_bits = ((0x1==0x00000001)?(prot&0x1):(prot&0x1)?0x00000001:0);
1256 return prot_bits;
1259 void bool_test()
1261 int *s, a, b, t, f, i;
1263 a = 0;
1264 s = (void*)0;
1265 printf("!s=%d\n", !s);
1267 if (!s || !s[0])
1268 a = 1;
1269 printf("a=%d\n", a);
1271 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1272 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1273 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1274 #if 1 && 1
1275 printf("a1\n");
1276 #endif
1277 #if 1 || 0
1278 printf("a2\n");
1279 #endif
1280 #if 1 ? 0 : 1
1281 printf("a3\n");
1282 #endif
1283 #if 0 ? 0 : 1
1284 printf("a4\n");
1285 #endif
1287 a = 4;
1288 printf("b=%d\n", a + (0 ? 1 : a / 2));
1290 /* test register spilling */
1291 a = 10;
1292 b = 10;
1293 a = (a + b) * ((a < b) ?
1294 ((b - a) * (a - b)): a + b);
1295 printf("a=%d\n", a);
1297 /* test complex || or && expressions */
1298 t = 1;
1299 f = 0;
1300 a = 32;
1301 printf("exp=%d\n", f == (32 <= a && a <= 3));
1302 printf("r=%d\n", (t || f) + (t && f));
1304 /* test ? : cast */
1306 int aspect_on;
1307 int aspect_native = 65536;
1308 double bfu_aspect = 1.0;
1309 int aspect;
1310 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
1311 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
1312 printf("aspect=%d\n", aspect);
1316 /* test ? : GCC extension */
1318 static int v1 = 34 ? : -1; /* constant case */
1319 static int v2 = 0 ? : -1; /* constant case */
1320 int a = 30;
1322 printf("%d %d\n", v1, v2);
1323 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
1326 /* again complex expression */
1327 for(i=0;i<256;i++) {
1328 if (toupper1 (i) != TOUPPER (i))
1329 printf("error %d\n", i);
1331 printf ("bits = 0x%x\n", calc_vm_flags (0x1));
1334 extern int undefined_function(void);
1335 extern int defined_function(void);
1337 static inline void refer_to_undefined(void)
1339 undefined_function();
1342 void optimize_out(void)
1344 int i = 0 ? undefined_function() : defined_function();
1345 printf ("oo:%d\n", i);
1346 int j = 1 ? defined_function() : undefined_function();
1347 printf ("oo:%d\n", j);
1348 if (0)
1349 printf("oo:%d\n", undefined_function());
1350 else
1351 printf("oo:%d\n", defined_function());
1352 if (1)
1353 printf("oo:%d\n", defined_function());
1354 else
1355 printf("oo:%d\n", undefined_function());
1356 while (1) {
1357 printf("oow:%d\n", defined_function());
1358 break;
1359 printf("oow:%d\n", undefined_function());
1361 j = 1;
1362 /* Following is a switch without {} block intentionally. */
1363 switch (j)
1364 case 1: break;
1365 printf ("oos:%d\n", defined_function());
1366 /* The following break shouldn't lead to disabled code after
1367 the while. */
1368 while (1)
1369 break;
1370 printf ("ool1:%d\n", defined_function());
1371 /* Same for the other types of loops. */
1373 break;
1374 while (1);
1375 printf ("ool2:%d\n", defined_function());
1376 for (;;)
1377 break;
1378 printf ("ool3:%d\n", defined_function());
1379 /* Normal {} blocks without controlling statements
1380 shouldn't reactivate code emission */
1381 while (1) {
1383 break;
1385 printf ("ool4:%d\n", undefined_function());
1387 j = 1;
1388 while (j) {
1389 if (j == 0)
1390 break; /* this break shouldn't disable code outside the if. */
1391 printf("ool5:%d\n", defined_function());
1392 j--;
1395 j = 1;
1396 while (j) {
1397 if (1)
1398 j--;
1399 else
1400 breakhere: break;
1401 printf("ool6:%d\n", defined_function());
1402 goto breakhere;
1404 j = 1;
1405 while (j) {
1406 j--;
1407 continue;
1408 printf("ool7:%d\n", undefined_function());
1411 /* Test that constants in logical && are optimized: */
1412 i = 0 && undefined_function();
1413 i = defined_function() && 0 && undefined_function();
1414 if (0 && undefined_function())
1415 undefined_function();
1416 if (defined_function() && 0)
1417 undefined_function();
1418 if (0 && 0)
1419 undefined_function();
1420 if (defined_function() && 0 && undefined_function())
1421 undefined_function();
1422 /* The same for || : */
1423 i = 1 || undefined_function();
1424 i = defined_function() || 1 || undefined_function();
1425 if (1 || undefined_function())
1427 else
1428 undefined_function();
1429 if (defined_function() || 1)
1431 else
1432 undefined_function();
1433 if (1 || 1)
1435 else
1436 undefined_function();
1437 if (defined_function() || 1 || undefined_function())
1439 else
1440 undefined_function();
1442 if (defined_function() && 0)
1443 refer_to_undefined();
1445 if (0) {
1446 (void)sizeof( ({
1447 do { } while (0);
1449 }) );
1450 undefined_function();
1453 /* Leave the "if(1)return; printf()" in this order and last in the function */
1454 if (1)
1455 return;
1456 printf ("oor:%d\n", undefined_function());
1459 int defined_function(void)
1461 static int i = 40;
1462 return i++;
1465 /* GCC accepts that */
1466 static int tab_reinit[];
1467 static int tab_reinit[10];
1469 static int tentative_ar[];
1470 static int tentative_ar[] = {1,2,3};
1472 //int cinit1; /* a global variable can be defined several times without error ! */
1473 int cinit1;
1474 int cinit1;
1475 int cinit1 = 0;
1476 int *cinit2 = (int []){3, 2, 1};
1478 void compound_literal_test(void)
1480 int *p, i;
1481 char *q, *q3;
1483 printf("compound_test:\n");
1485 p = (int []){1, 2, 3};
1486 for(i=0;i<3;i++)
1487 printf(" %d", p[i]);
1488 printf("\n");
1490 for(i=0;i<3;i++)
1491 printf("%d", cinit2[i]);
1492 printf("\n");
1494 q = "tralala1";
1495 printf("q1=%s\n", q);
1497 q = (char *){ "tralala2" };
1498 printf("q2=%s\n", q);
1500 q3 = (char *){ q };
1501 printf("q3=%s\n", q3);
1503 q = (char []){ "tralala3" };
1504 printf("q4=%s\n", q);
1506 #ifdef ALL_ISOC99
1507 p = (int []){1, 2, cinit1 + 3};
1508 for(i=0;i<3;i++)
1509 printf(" %d", p[i]);
1510 printf("\n");
1512 for(i=0;i<3;i++) {
1513 p = (int []){1, 2, 4 + i};
1514 printf("%d %d %d\n",
1515 p[0],
1516 p[1],
1517 p[2]);
1519 #endif
1522 /* K & R protos */
1524 kr_func1(a, b)
1526 return a + b;
1529 int kr_func2(a, b)
1531 return a + b;
1534 kr_test()
1536 printf("kr_test:\n");
1537 printf("func1=%d\n", kr_func1(3, 4));
1538 printf("func2=%d\n", kr_func2(3, 4));
1539 return 0;
1542 void num(int n)
1544 char *tab, *p;
1545 tab = (char*)malloc(20);
1546 p = tab;
1547 while (1) {
1548 *p = 48 + (n % 10);
1549 p++;
1550 n = n / 10;
1551 if (n == 0)
1552 break;
1554 while (p != tab) {
1555 p--;
1556 printf("%c", *p);
1558 printf("\n");
1559 free(tab);
1562 /* structure assignment tests */
1563 struct structa1 {
1564 int f1;
1565 char f2;
1568 struct structa1 ssta1;
1570 void struct_assign_test1(struct structa1 s1, int t, float f)
1572 printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
1575 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1577 s1.f1 += t;
1578 s1.f2 -= t;
1579 return s1;
1582 void struct_assign_test(void)
1584 struct S {
1585 struct structa1 lsta1, lsta2;
1586 int i;
1587 } s, *ps;
1589 ps = &s;
1590 ps->i = 4;
1591 #if 0
1592 printf("struct_assign_test:\n");
1594 s.lsta1.f1 = 1;
1595 s.lsta1.f2 = 2;
1596 printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
1597 s.lsta2 = s.lsta1;
1598 printf("%d %d\n", s.lsta2.f1, s.lsta2.f2);
1599 #else
1600 s.lsta2.f1 = 1;
1601 s.lsta2.f2 = 2;
1602 #endif
1603 struct_assign_test1(ps->lsta2, 3, 4.5);
1605 printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
1606 ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
1607 printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
1609 static struct {
1610 void (*elem)();
1611 } t[] = {
1612 /* XXX: we should allow this even without braces */
1613 { struct_assign_test }
1615 printf("%d\n", struct_assign_test == t[0].elem);
1618 /* casts to short/char */
1620 void cast1(char a, short b, unsigned char c, unsigned short d)
1622 printf("%d %d %d %d\n", a, b, c, d);
1625 char bcast;
1626 short scast;
1628 void cast_test()
1630 int a;
1631 char c;
1632 char tab[10];
1633 unsigned b,d;
1634 short s;
1635 char *p = NULL;
1636 p -= 0x700000000042;
1638 printf("cast_test:\n");
1639 a = 0xfffff;
1640 cast1(a, a, a, a);
1641 a = 0xffffe;
1642 printf("%d %d %d %d\n",
1643 (char)(a + 1),
1644 (short)(a + 1),
1645 (unsigned char)(a + 1),
1646 (unsigned short)(a + 1));
1647 printf("%d %d %d %d\n",
1648 (char)0xfffff,
1649 (short)0xfffff,
1650 (unsigned char)0xfffff,
1651 (unsigned short)0xfffff);
1653 a = (bcast = 128) + 1;
1654 printf("%d\n", a);
1655 a = (scast = 65536) + 1;
1656 printf("%d\n", a);
1658 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1660 /* test cast from unsigned to signed short to int */
1661 b = 0xf000;
1662 d = (short)b;
1663 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1664 b = 0xf0f0;
1665 d = (char)b;
1666 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1668 /* test implicit int casting for array accesses */
1669 c = 0;
1670 tab[1] = 2;
1671 tab[c] = 1;
1672 printf("%d %d\n", tab[0], tab[1]);
1674 /* test implicit casting on some operators */
1675 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1676 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1677 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1679 /* from pointer to integer types */
1680 printf("%d %d %ld %ld %lld %lld\n",
1681 (int)p, (unsigned int)p,
1682 (long)p, (unsigned long)p,
1683 (long long)p, (unsigned long long)p);
1685 /* from integers to pointers */
1686 printf("%p %p %p %p\n",
1687 (void *)a, (void *)b, (void *)c, (void *)d);
1690 /* initializers tests */
1691 struct structinit1 {
1692 int f1;
1693 char f2;
1694 short f3;
1695 int farray[3];
1698 int sinit1 = 2;
1699 int sinit2 = { 3 };
1700 int sinit3[3] = { 1, 2, {{3}}, };
1701 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1702 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1703 int sinit6[] = { 1, 2, 3 };
1704 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1705 char sinit8[] = "hello" "trala";
1707 struct structinit1 sinit9 = { 1, 2, 3 };
1708 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1709 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1710 #ifdef ALL_ISOC99
1711 .farray[0] = 10,
1712 .farray[1] = 11,
1713 .farray[2] = 12,
1714 #endif
1717 char *sinit12 = "hello world";
1718 char *sinit13[] = {
1719 "test1",
1720 "test2",
1721 "test3",
1723 char sinit14[10] = { "abc" };
1724 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1726 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1728 struct bar {
1729 char *s;
1730 int len;
1731 } sinit17[] = {
1732 "a1", 4,
1733 "a2", 1
1736 int sinit18[10] = {
1737 [2 ... 5] = 20,
1739 [8] = 10,
1742 struct complexinit0 {
1743 int a;
1744 int b;
1747 struct complexinit {
1748 int a;
1749 const struct complexinit0 *b;
1752 const static struct complexinit cix[] = {
1753 [0] = {
1754 .a = 2000,
1755 .b = (const struct complexinit0[]) {
1756 { 2001, 2002 },
1757 { 2003, 2003 },
1763 struct complexinit2 {
1764 int a;
1765 int b[];
1768 struct complexinit2 cix20;
1770 struct complexinit2 cix21 = {
1771 .a = 3000,
1772 .b = { 3001, 3002, 3003 }
1775 struct complexinit2 cix22 = {
1776 .a = 4000,
1777 .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1780 typedef int arrtype1[];
1781 arrtype1 sinit19 = {1};
1782 arrtype1 sinit20 = {2,3};
1783 typedef int arrtype2[3];
1784 arrtype2 sinit21 = {4};
1785 arrtype2 sinit22 = {5,6,7};
1787 /* Address comparisons of non-weak symbols with zero can be const-folded */
1788 int sinit23[2] = { "astring" ? sizeof("astring") : -1,
1789 &sinit23 ? 42 : -1 };
1791 extern int external_inited = 42;
1793 void init_test(void)
1795 int linit1 = 2;
1796 int linit2 = { 3 };
1797 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1798 int linit6[] = { 1, 2, 3 };
1799 int i, j;
1800 char linit8[] = "hello" "trala";
1801 int linit12[10] = { 1, 2 };
1802 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1803 char linit14[10] = "abc";
1804 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1805 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1806 int linit17 = sizeof(linit17);
1807 int zero = 0;
1808 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1809 int linit18[2] = {&zero ? 1 : -1, zero ? -1 : 1 };
1811 printf("init_test:\n");
1813 printf("sinit1=%d\n", sinit1);
1814 printf("sinit2=%d\n", sinit2);
1815 printf("sinit3=%d %d %d %d\n",
1816 sizeof(sinit3),
1817 sinit3[0],
1818 sinit3[1],
1819 sinit3[2]
1821 printf("sinit6=%d\n", sizeof(sinit6));
1822 printf("sinit7=%d %d %d %d\n",
1823 sizeof(sinit7),
1824 sinit7[0],
1825 sinit7[1],
1826 sinit7[2]
1828 printf("sinit8=%s\n", sinit8);
1829 printf("sinit9=%d %d %d\n",
1830 sinit9.f1,
1831 sinit9.f2,
1832 sinit9.f3
1834 printf("sinit10=%d %d %d\n",
1835 sinit10.f1,
1836 sinit10.f2,
1837 sinit10.f3
1839 printf("sinit11=%d %d %d %d %d %d\n",
1840 sinit11.f1,
1841 sinit11.f2,
1842 sinit11.f3,
1843 sinit11.farray[0],
1844 sinit11.farray[1],
1845 sinit11.farray[2]
1848 for(i=0;i<3;i++)
1849 for(j=0;j<2;j++)
1850 printf("[%d][%d] = %d %d %d\n",
1851 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1852 printf("linit1=%d\n", linit1);
1853 printf("linit2=%d\n", linit2);
1854 printf("linit6=%d\n", sizeof(linit6));
1855 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1857 printf("sinit12=%s\n", sinit12);
1858 printf("sinit13=%d %s %s %s\n",
1859 sizeof(sinit13),
1860 sinit13[0],
1861 sinit13[1],
1862 sinit13[2]);
1863 printf("sinit14=%s\n", sinit14);
1865 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1866 printf("\n");
1867 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1868 printf("\n");
1869 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1870 printf("\n");
1871 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1872 printf("\n");
1873 printf("%d %d %d %d\n",
1874 linit16.a1,
1875 linit16.a2,
1876 linit16.a3,
1877 linit16.a4);
1878 /* test that initialisation is done after variable declare */
1879 printf("linit17=%d\n", linit17);
1880 printf("sinit15=%d\n", sinit15[0]);
1881 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1882 printf("sinit17=%s %d %s %d\n",
1883 sinit17[0].s, sinit17[0].len,
1884 sinit17[1].s, sinit17[1].len);
1885 for(i=0;i<10;i++)
1886 printf("%x ", sinit18[i]);
1887 printf("\n");
1888 /* complex init check */
1889 printf("cix: %d %d %d %d %d %d %d\n",
1890 cix[0].a,
1891 cix[0].b[0].a, cix[0].b[0].b,
1892 cix[0].b[1].a, cix[0].b[1].b,
1893 cix[0].b[2].a, cix[0].b[2].b);
1894 printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
1895 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
1897 printf("arrtype1: %d %d %d\n", sinit19[0], sinit20[0], sinit20[1]);
1898 printf("arrtype2: %d %d\n", sizeof(sinit19), sizeof(sinit20));
1899 printf("arrtype3: %d %d %d\n", sinit21[0], sinit21[1], sinit21[2]);
1900 printf("arrtype4: %d %d %d\n", sinit22[0], sinit22[1], sinit22[2]);
1901 printf("arrtype5: %d %d\n", sizeof(sinit21), sizeof(sinit22));
1902 printf("arrtype6: %d\n", sizeof(arrtype2));
1904 printf("sinit23= %d %d\n", sinit23[0], sinit23[1]);
1905 printf("linit18= %d %d\n", linit18[0], linit18[1]);
1908 void switch_uc(unsigned char uc)
1910 switch (uc) {
1911 case 0xfb ... 0xfe:
1912 printf("ucsw:1\n");
1913 break;
1914 case 0xff:
1915 printf("ucsw:2\n");
1916 break;
1917 case 0 ... 5:
1918 printf("ucsw:3\n");
1919 break;
1920 default:
1921 printf("ucsw: broken!\n");
1925 void switch_sc(signed char sc)
1927 switch (sc) {
1928 case -5 ... -2:
1929 printf("scsw:1\n");
1930 break;
1931 case -1:
1932 printf("scsw:2\n");
1933 break;
1934 case 0 ... 5:
1935 printf("scsw:3\n");
1936 break;
1937 default:
1938 printf("scsw: broken!\n");
1942 void switch_test()
1944 int i;
1945 unsigned long long ull;
1946 long long ll;
1948 for(i=0;i<15;i++) {
1949 switch(i) {
1950 case 0:
1951 case 1:
1952 printf("a");
1953 break;
1954 default:
1955 printf("%d", i);
1956 break;
1957 case 8 ... 12:
1958 printf("c");
1959 break;
1960 case 3:
1961 printf("b");
1962 break;
1963 case 0xc33c6b9fU:
1964 case 0x7c9eeeb9U:
1965 break;
1968 printf("\n");
1970 for (i = 1; i <= 5; i++) {
1971 ull = (unsigned long long)i << 61;
1972 switch (ull) {
1973 case 1ULL << 61:
1974 printf("ullsw:1\n");
1975 break;
1976 case 2ULL << 61:
1977 printf("ullsw:2\n");
1978 break;
1979 case 3ULL << 61:
1980 printf("ullsw:3\n");
1981 break;
1982 case 4ULL << 61:
1983 printf("ullsw:4\n");
1984 break;
1985 case 5ULL << 61:
1986 printf("ullsw:5\n");
1987 break;
1988 default:
1989 printf("ullsw: broken!\n");
1993 for (i = 1; i <= 5; i++) {
1994 ll = (long long)i << 61;
1995 switch (ll) {
1996 case 1LL << 61:
1997 printf("llsw:1\n");
1998 break;
1999 case 2LL << 61:
2000 printf("llsw:2\n");
2001 break;
2002 case 3LL << 61:
2003 printf("llsw:3\n");
2004 break;
2005 case 4LL << 61:
2006 printf("llsw:4\n");
2007 break;
2008 case 5LL << 61:
2009 printf("llsw:5\n");
2010 break;
2011 default:
2012 printf("llsw: broken!\n");
2016 for (i = -5; i <= 5; i++) {
2017 switch_uc((unsigned char)i);
2020 for (i = -5; i <= 5; i++) {
2021 switch_sc ((signed char)i);
2025 /* ISOC99 _Bool type */
2026 void c99_bool_test(void)
2028 #ifdef BOOL_ISOC99
2029 int a;
2030 _Bool b;
2032 printf("bool_test:\n");
2033 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
2034 a = 3;
2035 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
2036 b = 3;
2037 printf("b = %d\n", b);
2038 b++;
2039 printf("b = %d\n", b);
2040 #endif
2043 void bitfield_test(void)
2045 int a;
2046 short sa;
2047 unsigned char ca;
2048 struct sbf1 {
2049 int f1 : 3;
2050 int : 2;
2051 int f2 : 1;
2052 int : 0;
2053 int f3 : 5;
2054 int f4 : 7;
2055 unsigned int f5 : 7;
2056 } st1;
2057 printf("bitfield_test:");
2058 printf("sizeof(st1) = %d\n", sizeof(st1));
2060 st1.f1 = 3;
2061 st1.f2 = 1;
2062 st1.f3 = 15;
2063 a = 120;
2064 st1.f4 = a;
2065 st1.f5 = a;
2066 st1.f5++;
2067 printf("%d %d %d %d %d\n",
2068 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
2069 sa = st1.f5;
2070 ca = st1.f5;
2071 printf("%d %d\n", sa, ca);
2073 st1.f1 = 7;
2074 if (st1.f1 == -1)
2075 printf("st1.f1 == -1\n");
2076 else
2077 printf("st1.f1 != -1\n");
2078 if (st1.f2 == -1)
2079 printf("st1.f2 == -1\n");
2080 else
2081 printf("st1.f2 != -1\n");
2083 struct sbf2 {
2084 long long f1 : 45;
2085 long long : 2;
2086 long long f2 : 35;
2087 unsigned long long f3 : 38;
2088 } st2;
2089 st2.f1 = 0x123456789ULL;
2090 a = 120;
2091 st2.f2 = (long long)a << 25;
2092 st2.f3 = a;
2093 st2.f2++;
2094 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
2096 #if 0
2097 Disabled for now until further clarification re GCC compatibility
2098 struct sbf3 {
2099 int f1 : 7;
2100 int f2 : 1;
2101 char f3;
2102 int f4 : 8;
2103 int f5 : 1;
2104 int f6 : 16;
2105 } st3;
2106 printf("sizeof(st3) = %d\n", sizeof(st3));
2107 #endif
2109 struct sbf4 {
2110 int x : 31;
2111 char y : 2;
2112 } st4;
2113 st4.y = 1;
2114 printf("st4.y == %d\n", st4.y);
2115 struct sbf5 {
2116 int a;
2117 char b;
2118 int x : 12, y : 4, : 0, : 4, z : 3;
2119 char c;
2120 } st5 = { 1, 2, 3, 4, -3, 6 };
2121 printf("st5 = %d %d %d %d %d %d\n", st5.a, st5.b, st5.x, st5.y, st5.z, st5.c);
2122 struct sbf6 {
2123 short x : 12;
2124 unsigned char y : 2;
2125 } st6;
2126 st6.y = 1;
2127 printf("st6.y == %d\n", st6.y);
2130 #ifdef __x86_64__
2131 #define FLOAT_FMT "%f\n"
2132 #else
2133 /* x86's float isn't compatible with GCC */
2134 #define FLOAT_FMT "%.5f\n"
2135 #endif
2137 /* declare strto* functions as they are C99 */
2138 double strtod(const char *nptr, char **endptr);
2140 #if defined(_WIN32)
2141 float strtof(const char *nptr, char **endptr) {return (float)strtod(nptr, endptr);}
2142 LONG_DOUBLE strtold(const char *nptr, char **endptr) {return (LONG_DOUBLE)strtod(nptr, endptr);}
2143 #else
2144 float strtof(const char *nptr, char **endptr);
2145 LONG_DOUBLE strtold(const char *nptr, char **endptr);
2146 #endif
2148 #define FTEST(prefix, typename, type, fmt)\
2149 void prefix ## cmp(type a, type b)\
2151 printf("%d %d %d %d %d %d\n",\
2152 a == b,\
2153 a != b,\
2154 a < b,\
2155 a > b,\
2156 a >= b,\
2157 a <= b);\
2158 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
2161 a + b,\
2162 a - b,\
2163 a * b,\
2164 a / b,\
2165 -a);\
2166 printf(fmt "\n", ++a);\
2167 printf(fmt "\n", a++);\
2168 printf(fmt "\n", a);\
2169 b = 0;\
2170 printf("%d %d\n", !a, !b);\
2172 void prefix ## fcast(type a)\
2174 float fa;\
2175 double da;\
2176 LONG_DOUBLE la;\
2177 int ia;\
2178 long long llia;\
2179 unsigned int ua;\
2180 unsigned long long llua;\
2181 type b;\
2182 fa = a;\
2183 da = a;\
2184 la = a;\
2185 printf("ftof: %f %f %Lf\n", fa, da, la);\
2186 ia = (int)a;\
2187 llia = (long long)a;\
2188 a = (a >= 0) ? a : -a;\
2189 ua = (unsigned int)a;\
2190 llua = (unsigned long long)a;\
2191 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
2192 ia = -1234;\
2193 ua = 0x81234500;\
2194 llia = -0x123456789012345LL;\
2195 llua = 0xf123456789012345LLU;\
2196 b = ia;\
2197 printf("itof: " fmt "\n", b);\
2198 b = ua;\
2199 printf("utof: " fmt "\n", b);\
2200 b = llia;\
2201 printf("lltof: " fmt "\n", b);\
2202 b = llua;\
2203 printf("ulltof: " fmt "\n", b);\
2206 float prefix ## retf(type a) { return a; }\
2207 double prefix ## retd(type a) { return a; }\
2208 LONG_DOUBLE prefix ## retld(type a) { return a; }\
2210 void prefix ## call(void)\
2212 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
2213 printf("double: %f\n", prefix ## retd(42.123456789));\
2214 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
2215 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
2218 void prefix ## signed_zeros(void) \
2220 type x = 0.0, y = -0.0, n, p;\
2221 if (x == y)\
2222 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
2223 1.0 / x != 1.0 / y);\
2224 else\
2225 printf ("x != y; this is wrong!\n");\
2227 n = -x;\
2228 if (x == n)\
2229 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
2230 1.0 / x != 1.0 / n);\
2231 else\
2232 printf ("x != -x; this is wrong!\n");\
2234 p = +y;\
2235 if (x == p)\
2236 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
2237 1.0 / x != 1.0 / p);\
2238 else\
2239 printf ("x != +y; this is wrong!\n");\
2240 p = -y;\
2241 if (x == p)\
2242 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
2243 1.0 / x != 1.0 / p);\
2244 else\
2245 printf ("x != -y; this is wrong!\n");\
2247 void prefix ## test(void)\
2249 printf("testing '%s'\n", #typename);\
2250 prefix ## cmp(1, 2.5);\
2251 prefix ## cmp(2, 1.5);\
2252 prefix ## cmp(1, 1);\
2253 prefix ## fcast(234.6);\
2254 prefix ## fcast(-2334.6);\
2255 prefix ## call();\
2256 prefix ## signed_zeros();\
2259 FTEST(f, float, float, "%f")
2260 FTEST(d, double, double, "%f")
2261 FTEST(ld, long double, LONG_DOUBLE, "%Lf")
2263 double ftab1[3] = { 1.2, 3.4, -5.6 };
2266 void float_test(void)
2268 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
2269 float fa, fb;
2270 double da, db;
2271 int a;
2272 unsigned int b;
2273 static double nan2 = 0.0/0.0;
2274 static double inf1 = 1.0/0.0;
2275 static double inf2 = 1e5000;
2277 printf("float_test:\n");
2278 printf("sizeof(float) = %d\n", sizeof(float));
2279 printf("sizeof(double) = %d\n", sizeof(double));
2280 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));
2281 ftest();
2282 dtest();
2283 ldtest();
2284 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
2285 printf("%f %f %f\n", 2.12, .5, 2.3e10);
2286 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
2287 da = 123;
2288 printf("da=%f\n", da);
2289 fa = 123;
2290 printf("fa=%f\n", fa);
2291 a = 4000000000;
2292 da = a;
2293 printf("da = %f\n", da);
2294 b = 4000000000;
2295 db = b;
2296 printf("db = %f\n", db);
2297 printf("nan != nan = %d, inf1 = %f, inf2 = %f\n", nan2 != nan2, inf1, inf2);
2298 #endif
2301 int fib(int n)
2303 if (n <= 2)
2304 return 1;
2305 else
2306 return fib(n-1) + fib(n-2);
2309 void __attribute__((aligned(16))) aligned_function(int i) {}
2311 void funcptr_test()
2313 void (*func)(int);
2314 int a;
2315 struct {
2316 int dummy;
2317 void (*func)(int);
2318 } st1;
2319 long diff;
2321 printf("funcptr:\n");
2322 func = &num;
2323 (*func)(12345);
2324 func = num;
2325 a = 1;
2326 a = 1;
2327 func(12345);
2328 /* more complicated pointer computation */
2329 st1.func = num;
2330 st1.func(12346);
2331 printf("sizeof1 = %d\n", sizeof(funcptr_test));
2332 printf("sizeof2 = %d\n", sizeof funcptr_test);
2333 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
2334 printf("sizeof4 = %d\n", sizeof &funcptr_test);
2335 a = 0;
2336 func = num + a;
2337 diff = func - num;
2338 func(42);
2339 (func + diff)(42);
2340 (num + a)(43);
2342 /* Check that we can align functions */
2343 func = aligned_function;
2344 printf("aligned_function (should be zero): %d\n", ((int)func) & 15);
2347 void lloptest(long long a, long long b)
2349 unsigned long long ua, ub;
2351 ua = a;
2352 ub = b;
2353 /* arith */
2354 printf("arith: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2355 a + b,
2356 a - b,
2357 a * b);
2359 if (b != 0) {
2360 printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2361 a / b,
2362 a % b);
2365 /* binary */
2366 printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2367 a & b,
2368 a | b,
2369 a ^ b);
2371 /* tests */
2372 printf("test: %d %d %d %d %d %d\n",
2373 a == b,
2374 a != b,
2375 a < b,
2376 a > b,
2377 a >= b,
2378 a <= b);
2380 printf("utest: %d %d %d %d %d %d\n",
2381 ua == ub,
2382 ua != ub,
2383 ua < ub,
2384 ua > ub,
2385 ua >= ub,
2386 ua <= ub);
2388 /* arith2 */
2389 a++;
2390 b++;
2391 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2392 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a++, b++);
2393 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", --a, --b);
2394 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2395 b = ub = 0;
2396 printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
2399 void llshift(long long a, int b)
2401 printf("shift: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2402 (unsigned long long)a >> b,
2403 a >> b,
2404 a << b);
2405 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2406 (unsigned long long)a >> 3,
2407 a >> 3,
2408 a << 3);
2409 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2410 (unsigned long long)a >> 35,
2411 a >> 35,
2412 a << 35);
2415 void llfloat(void)
2417 float fa;
2418 double da;
2419 LONG_DOUBLE lda;
2420 long long la, lb, lc;
2421 unsigned long long ula, ulb, ulc;
2422 la = 0x12345678;
2423 ula = 0x72345678;
2424 la = (la << 20) | 0x12345;
2425 ula = ula << 33;
2426 printf("la=" LONG_LONG_FORMAT " ula=" ULONG_LONG_FORMAT "\n", la, ula);
2428 fa = la;
2429 da = la;
2430 lda = la;
2431 printf("lltof: %f %f %Lf\n", fa, da, lda);
2433 la = fa;
2434 lb = da;
2435 lc = lda;
2436 printf("ftoll: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", la, lb, lc);
2438 fa = ula;
2439 da = ula;
2440 lda = ula;
2441 printf("ulltof: %f %f %Lf\n", fa, da, lda);
2443 ula = fa;
2444 ulb = da;
2445 ulc = lda;
2446 printf("ftoull: " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT "\n", ula, ulb, ulc);
2449 long long llfunc1(int a)
2451 return a * 2;
2454 struct S {
2455 int id;
2456 char item;
2459 long long int value(struct S *v)
2461 return ((long long int)v->item);
2464 long long llfunc2(long long x, long long y, int z)
2466 return x * y * z;
2469 void check_opl_save_regs(char *a, long long b, int c)
2471 *a = b < 0 && !c;
2474 void longlong_test(void)
2476 long long a, b, c;
2477 int ia;
2478 unsigned int ua;
2479 printf("longlong_test:\n");
2480 printf("sizeof(long long) = %d\n", sizeof(long long));
2481 ia = -1;
2482 ua = -2;
2483 a = ia;
2484 b = ua;
2485 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2486 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %Lx\n",
2487 (long long)1,
2488 (long long)-2,
2489 1LL,
2490 0x1234567812345679);
2491 a = llfunc1(-3);
2492 printf(LONG_LONG_FORMAT "\n", a);
2494 lloptest(1000, 23);
2495 lloptest(0xff, 0x1234);
2496 b = 0x72345678 << 10;
2497 lloptest(-3, b);
2498 llshift(0x123, 5);
2499 llshift(-23, 5);
2500 b = 0x72345678LL << 10;
2501 llshift(b, 47);
2503 llfloat();
2504 #if 1
2505 b = 0x12345678;
2506 a = -1;
2507 c = a + b;
2508 printf("%Lx\n", c);
2509 #endif
2511 /* long long reg spill test */
2513 struct S a;
2515 a.item = 3;
2516 printf("%lld\n", value(&a));
2518 lloptest(0x80000000, 0);
2521 long long *p, v, **pp;
2522 v = 1;
2523 p = &v;
2524 p[0]++;
2525 printf("another long long spill test : %lld\n", *p);
2526 pp = &p;
2528 v = llfunc2(**pp, **pp, ia);
2529 printf("a long long function (arm-)reg-args test : %lld\n", v);
2531 a = 68719476720LL;
2532 b = 4294967295LL;
2533 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
2535 printf(LONG_LONG_FORMAT "\n", 0x123456789LLU);
2537 /* long long pointer deref in argument passing test */
2538 a = 0x123;
2539 long long *p = &a;
2540 llshift(*p, 5);
2542 /* shortening followed by widening */
2543 unsigned long long u = 0x8000000000000001ULL;
2544 u = (unsigned)(u + 1);
2545 printf("long long u=" ULONG_LONG_FORMAT "\n", u);
2547 /* was a problem with missing save_regs in gen_opl on 32-bit platforms */
2548 char cc = 78;
2549 check_opl_save_regs(&cc, -1, 0);
2550 printf("check_opl_save_regs: %d\n", cc);
2553 void manyarg_test(void)
2555 LONG_DOUBLE ld = 1234567891234LL;
2556 printf("manyarg_test:\n");
2557 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2558 1, 2, 3, 4, 5, 6, 7, 8,
2559 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2560 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2561 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2562 1, 2, 3, 4, 5, 6, 7, 8,
2563 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2564 1234567891234LL, 987654321986LL,
2565 42.0, 43.0);
2566 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2567 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2568 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2569 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2570 1234567891234LL, 987654321986LL,
2571 42.0, 43.0);
2572 printf("%d %d %d %d %d %d %d %d %Lf\n",
2573 1, 2, 3, 4, 5, 6, 7, 8, ld);
2574 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2575 LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n",
2576 1, 2, 3, 4, 5, 6, 7, 8,
2577 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2578 1234567891234LL, 987654321986LL,
2579 42.0, 43.0, ld);
2580 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2581 "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n",
2582 1, 2, 3, 4, 5, 6, 7, 8,
2583 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2584 ld, 1234567891234LL, 987654321986LL,
2585 42.0, 43.0, ld);
2588 void vprintf1(const char *fmt, ...)
2590 va_list ap, aq;
2591 const char *p;
2592 int c, i;
2593 double d;
2594 long long ll;
2595 LONG_DOUBLE ld;
2597 va_start(aq, fmt);
2598 va_copy(ap, aq);
2600 p = fmt;
2601 for(;;) {
2602 c = *p;
2603 if (c == '\0')
2604 break;
2605 p++;
2606 if (c == '%') {
2607 c = *p;
2608 switch(c) {
2609 case '\0':
2610 goto the_end;
2611 case 'd':
2612 i = va_arg(ap, int);
2613 printf("%d", i);
2614 break;
2615 case 'f':
2616 d = va_arg(ap, double);
2617 printf("%f", d);
2618 break;
2619 case 'l':
2620 ll = va_arg(ap, long long);
2621 printf(LONG_LONG_FORMAT, ll);
2622 break;
2623 case 'F':
2624 ld = va_arg(ap, LONG_DOUBLE);
2625 printf("%Lf", ld);
2626 break;
2628 p++;
2629 } else {
2630 putchar(c);
2633 the_end:
2634 va_end(aq);
2635 va_end(ap);
2638 struct myspace {
2639 short int profile;
2642 void stdarg_for_struct(struct myspace bob, ...)
2644 struct myspace george, bill;
2645 va_list ap;
2646 short int validate;
2648 va_start(ap, bob);
2649 bill = va_arg(ap, struct myspace);
2650 george = va_arg(ap, struct myspace);
2651 validate = va_arg(ap, int);
2652 printf("stdarg_for_struct: %d %d %d %d\n",
2653 bob.profile, bill.profile, george.profile, validate);
2654 va_end(ap);
2657 void stdarg_for_libc(const char *fmt, ...)
2659 va_list args;
2660 va_start(args, fmt);
2661 vprintf(fmt, args);
2662 va_end(args);
2665 void stdarg_syntax(int n, ...)
2667 int i;
2668 va_list ap;
2669 if (1)
2670 va_start(ap, n);
2671 else
2673 i = va_arg(ap, int);
2674 printf("stdarg_void_expr: %d\n", i);
2675 (va_end(ap));
2678 void stdarg_test(void)
2680 LONG_DOUBLE ld = 1234567891234LL;
2681 struct myspace bob;
2683 vprintf1("%d %d %d\n", 1, 2, 3);
2684 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2685 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2686 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2687 vprintf1("%d %f %l %F %d %f %l %F\n",
2688 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2689 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2690 1, 2, 3, 4, 5, 6, 7, 8,
2691 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2692 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2693 1, 2, 3, 4, 5, 6, 7, 8,
2694 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2695 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2696 "%l %l %f %f\n",
2697 1, 2, 3, 4, 5, 6, 7, 8,
2698 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2699 1234567891234LL, 987654321986LL,
2700 42.0, 43.0);
2701 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2702 "%l %l %f %f\n",
2703 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2704 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2705 1234567891234LL, 987654321986LL,
2706 42.0, 43.0);
2707 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2708 1, 2, 3, 4, 5, 6, 7, 8, ld);
2709 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2710 "%l %l %f %f %F\n",
2711 1, 2, 3, 4, 5, 6, 7, 8,
2712 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2713 1234567891234LL, 987654321986LL,
2714 42.0, 43.0, ld);
2715 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2716 "%F %l %l %f %f %F\n",
2717 1, 2, 3, 4, 5, 6, 7, 8,
2718 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2719 ld, 1234567891234LL, 987654321986LL,
2720 42.0, 43.0, ld);
2722 bob.profile = 42;
2723 stdarg_for_struct(bob, bob, bob, bob.profile);
2724 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2725 stdarg_syntax(1, 17);
2728 void whitespace_test(void)
2730 char *str;
2732 \f\v #if 1
2733 pri\
2734 ntf("whitspace:\n");\f\v
2735 #endif
2736 pf("N=%d\n", 2);
2738 #ifdef CORRECT_CR_HANDLING
2739 pri\
2740 ntf("aaa=%d\n", 3);
2741 #endif
2743 pri\
2745 ntf("min=%d\n", 4);
2747 #ifdef ACCEPT_CR_IN_STRINGS
2748 printf("len1=%d\n", strlen("
2749 "));
2750 #ifdef CORRECT_CR_HANDLING
2751 str = "
2753 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
2754 #endif
2755 printf("len1=%d\n", strlen(" a
2756 "));
2757 #endif /* ACCEPT_CR_IN_STRINGS */
2760 int reltab[3] = { 1, 2, 3 };
2762 int *rel1 = &reltab[1];
2763 int *rel2 = &reltab[2];
2765 #ifdef _WIN64
2766 void relocation_test(void) {}
2767 #else
2768 void getmyaddress(void)
2770 printf("in getmyaddress\n");
2773 #ifdef __LP64__
2774 long __pa_symbol(void)
2776 /* This 64bit constant was handled incorrectly, it was used as addend
2777 (which can hold 64bit just fine) in connection with a symbol,
2778 and TCC generates wrong code for that (displacements are 32bit only).
2779 This effectively is "+ 0x80000000", and if addresses of globals
2780 are below 2GB the result should be a number without high 32 bits set. */
2781 return ((long)(((unsigned long)(&rel1))) - (0xffffffff80000000UL));
2783 #endif
2785 unsigned long theaddress = (unsigned long)getmyaddress;
2786 void relocation_test(void)
2788 void (*fptr)(void) = (void (*)(void))theaddress;
2789 printf("*rel1=%d\n", *rel1);
2790 printf("*rel2=%d\n", *rel2);
2791 fptr();
2792 #ifdef __LP64__
2793 printf("pa_symbol=0x%lx\n", __pa_symbol() >> 63);
2794 #endif
2796 #endif
2798 void old_style_f(a,b,c)
2799 int a, b;
2800 double c;
2802 printf("a=%d b=%d b=%f\n", a, b, c);
2805 void decl_func1(int cmpfn())
2807 printf("cmpfn=%lx\n", (long)cmpfn);
2810 void decl_func2(cmpfn)
2811 int cmpfn();
2813 printf("cmpfn=%lx\n", (long)cmpfn);
2816 void old_style_function(void)
2818 old_style_f((void *)1, 2, 3.0);
2819 decl_func1(NULL);
2820 decl_func2(NULL);
2823 void alloca_test()
2825 #if defined __i386__ || defined __x86_64__ || defined __arm__
2826 char *p = alloca(16);
2827 strcpy(p,"123456789012345");
2828 printf("alloca: p is %s\n", p);
2829 char *demo = "This is only a test.\n";
2830 /* Test alloca embedded in a larger expression */
2831 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
2832 #endif
2835 void *bounds_checking_is_enabled()
2837 char ca[10], *cp = ca-1;
2838 return (ca != cp + 1) ? cp : NULL;
2841 typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
2843 void c99_vla_test(int size1, int size2)
2845 #if defined __i386__ || defined __x86_64__
2846 int size = size1 * size2;
2847 int tab1[size][2], tab2[10][2];
2848 void *tab1_ptr, *tab2_ptr, *bad_ptr;
2850 /* "size" should have been 'captured' at tab1 declaration,
2851 so modifying it should have no effect on VLA behaviour. */
2852 size = size-1;
2854 printf("Test C99 VLA 1 (sizeof): ");
2855 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
2856 tab1_ptr = tab1;
2857 tab2_ptr = tab2;
2858 printf("Test C99 VLA 2 (ptrs subtract): ");
2859 printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2860 printf("Test C99 VLA 3 (ptr add): ");
2861 printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2862 printf("Test C99 VLA 4 (ptr access): ");
2863 tab1[size1][1] = 42;
2864 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2866 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2867 if (bad_ptr = bounds_checking_is_enabled()) {
2868 int *t1 = &tab1[size1 * size2 - 1][3];
2869 int *t2 = &tab2[9][3];
2870 printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
2871 printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
2873 char*c1 = 1 + sizeof(tab1) + (char*)tab1;
2874 char*c2 = 1 + sizeof(tab2) + (char*)tab2;
2875 printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
2876 printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
2878 int *i1 = tab1[-1];
2879 int *i2 = tab2[-1];
2880 printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
2881 printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
2883 int *x1 = tab1[size1 * size2 + 1];
2884 int *x2 = tab2[10 + 1];
2885 printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
2886 printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
2887 } else {
2888 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2890 printf("\n");
2891 #endif
2894 #ifndef __TINYC__
2895 typedef __SIZE_TYPE__ uintptr_t;
2896 #endif
2898 void sizeof_test(void)
2900 int a;
2901 int **ptr;
2903 printf("sizeof(int) = %d\n", sizeof(int));
2904 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2905 printf("sizeof(long) = %d\n", sizeof(long));
2906 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2907 printf("sizeof(short) = %d\n", sizeof(short));
2908 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2909 printf("sizeof(char) = %d\n", sizeof(char));
2910 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2911 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2912 a = 1;
2913 printf("sizeof(a++) = %d\n", sizeof a++);
2914 printf("a=%d\n", a);
2915 ptr = NULL;
2916 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
2918 /* The type of sizeof should be as large as a pointer, actually
2919 it should be size_t. */
2920 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2921 uintptr_t t = 1;
2922 uintptr_t t2;
2923 /* Effectively <<32, but defined also on 32bit machines. */
2924 t <<= 16;
2925 t <<= 16;
2926 t++;
2927 /* This checks that sizeof really can be used to manipulate
2928 uintptr_t objects, without truncation. */
2929 t2 = t & -sizeof(uintptr_t);
2930 printf ("%lu %lu\n", t, t2);
2932 /* some alignof tests */
2933 printf("__alignof__(int) = %d\n", __alignof__(int));
2934 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2935 printf("__alignof__(short) = %d\n", __alignof__(short));
2936 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2937 printf("__alignof__(char) = %d\n", __alignof__(char));
2938 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2939 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
2941 /* sizes of VLAs need to be evaluated even inside sizeof: */
2942 a = 2;
2943 printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a]));
2944 /* And checking if sizeof compound literal works. Parenthesized: */
2945 printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n",
2946 sizeof( (struct {int i; int j;}){4,5} ));
2947 /* And as direct sizeof argument (as unary expression): */
2948 printf("sizeof (struct {short i; short j;}){4,5} = %d\n",
2949 sizeof (struct {short i; short j;}){4,5} );
2951 /* sizeof(x && y) should be sizeof(int), even if constant
2952 evaluating is possible. */
2953 printf("sizeof(t && 0) = %d\n", sizeof(t && 0));
2954 printf("sizeof(1 && 1) = %d\n", sizeof(1 && 1));
2955 printf("sizeof(t || 1) = %d\n", sizeof(t || 1));
2956 printf("sizeof(0 || 0) = %d\n", sizeof(0 || 0));
2959 void typeof_test(void)
2961 double a;
2962 typeof(a) b;
2963 typeof(float) c;
2965 a = 1.5;
2966 b = 2.5;
2967 c = 3.5;
2968 printf("a=%f b=%f c=%f\n", a, b, c);
2972 struct hlist_node;
2973 struct hlist_head {
2974 struct hlist_node *first, *last;
2977 void consume_ulong (unsigned long i)
2979 i = 0;
2982 void statement_expr_test(void)
2984 int a, i;
2986 /* Basic stmt expr test */
2987 a = 0;
2988 for(i=0;i<10;i++) {
2989 a += 1 +
2990 ( { int b, j;
2991 b = 0;
2992 for(j=0;j<5;j++)
2993 b += j; b;
2994 } );
2996 printf("a=%d\n", a);
2998 /* Test that symbols aren't freed prematurely.
2999 With SYM_DEBUG valgrind will show a read from a freed
3000 symbol, and tcc will show an (invalid) warning on the initialization
3001 of 'ptr' below, if symbols are popped after the stmt expr. */
3002 void *v = (void*)39;
3003 typeof(({
3004 (struct hlist_node *)v;
3005 })) x;
3006 typeof (x)
3007 ptr = (struct hlist_node *)v;
3009 /* This part used to segfault when symbols were popped prematurely.
3010 The symbols for the static local would be overwritten with
3011 helper symbols from the pre-processor expansions in between. */
3012 #define some_attr __attribute__((aligned(1)))
3013 #define tps(str) ({ \
3014 static const char *t some_attr = str; \
3015 t; \
3017 printf ("stmtexpr: %s %s\n",
3018 tps("somerandomlongstring"),
3019 tps("anotherlongstring"));
3021 /* Test that the three decls of 't' don't interact. */
3022 int t = 40;
3023 int b = ({ int t = 41; t; });
3024 int c = ({ int t = 42; t; });
3026 /* Test that aggregate return values work. */
3027 struct hlist_head h
3028 = ({
3029 typedef struct hlist_head T;
3030 long pre = 48;
3031 T t = { (void*)43, (void*)44 };
3032 long post = 49;
3035 printf ("stmtexpr: %d %d %d\n", t, b, c);
3036 printf ("stmtexpr: %ld %ld\n", (long)h.first, (long)h.last);
3038 /* Test that we can give out addresses of local labels. */
3039 consume_ulong(({ __label__ __here; __here: (unsigned long)&&__here; }));
3042 void local_label_test(void)
3044 int a;
3045 goto l1;
3047 a = 1 + ({
3048 __label__ l1, l2, l3, l4;
3049 goto l1;
3051 printf("aa1\n");
3052 goto l3;
3054 printf("aa3\n");
3055 goto l4;
3057 printf("aa2\n");
3058 goto l2;
3059 l3:;
3062 printf("a=%d\n", a);
3063 return;
3065 printf("bb1\n");
3066 goto l2;
3068 printf("bb2\n");
3069 goto l4;
3072 /* inline assembler test */
3073 #if defined(__i386__) || defined(__x86_64__)
3075 /* from linux kernel */
3076 static char * strncat1(char * dest,const char * src,size_t count)
3078 long d0, d1, d2, d3;
3079 __asm__ __volatile__(
3080 "repne\n\t"
3081 "scasb\n\t"
3082 "dec %1\n\t"
3083 "mov %8,%3\n"
3084 "1:\tdec %3\n\t"
3085 "js 2f\n\t"
3086 "lodsb\n\t"
3087 "stosb\n\t"
3088 "testb %%al,%%al\n\t"
3089 "jne 1b\n"
3090 "2:\txor %2,%2\n\t"
3091 "stosb"
3092 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
3093 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
3094 : "memory");
3095 return dest;
3098 static char * strncat2(char * dest,const char * src,size_t count)
3100 long d0, d1, d2, d3;
3101 __asm__ __volatile__(
3102 "repne scasb\n\t" /* one-line repne prefix + string op */
3103 "dec %1\n\t"
3104 "mov %8,%3\n"
3105 "1:\tdec %3\n\t"
3106 "js 2f\n\t"
3107 "lodsb\n\t"
3108 "stosb\n\t"
3109 "testb %%al,%%al\n\t"
3110 "jne 1b\n"
3111 "2:\txor %2,%2\n\t"
3112 "stosb"
3113 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
3114 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
3115 : "memory");
3116 return dest;
3119 static inline void * memcpy1(void * to, const void * from, size_t n)
3121 long d0, d1, d2;
3122 __asm__ __volatile__(
3123 "rep ; movsl\n\t"
3124 "testb $2,%b4\n\t"
3125 "je 1f\n\t"
3126 "movsw\n"
3127 "1:\ttestb $1,%b4\n\t"
3128 "je 2f\n\t"
3129 "movsb\n"
3130 "2:"
3131 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
3132 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
3133 : "memory");
3134 return (to);
3137 static inline void * memcpy2(void * to, const void * from, size_t n)
3139 long d0, d1, d2;
3140 __asm__ __volatile__(
3141 "rep movsl\n\t" /* one-line rep prefix + string op */
3142 "testb $2,%b4\n\t"
3143 "je 1f\n\t"
3144 "movsw\n"
3145 "1:\ttestb $1,%b4\n\t"
3146 "je 2f\n\t"
3147 "movsb\n"
3148 "2:"
3149 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
3150 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
3151 : "memory");
3152 return (to);
3155 static __inline__ void sigaddset1(unsigned int *set, int _sig)
3157 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
3160 static __inline__ void sigdelset1(unsigned int *set, int _sig)
3162 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc", "flags");
3165 static __inline__ __const__ unsigned int swab32(unsigned int x)
3167 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
3168 "rorl $16,%0\n\t" /* swap words */
3169 "xchgb %b0,%h0" /* swap higher bytes */
3170 :"=" "q" (x)
3171 : "0" (x));
3172 return x;
3175 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
3177 unsigned long long res;
3178 #ifdef __x86_64__
3179 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
3180 but still test the 32bit->64bit mull. */
3181 unsigned int resh, resl;
3182 __asm__("mull %2" : "=a" (resl), "=d" (resh) : "a" (a), "r" (b));
3183 res = ((unsigned long long)resh << 32) | resl;
3184 #else
3185 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
3186 #endif
3187 return res;
3190 static __inline__ unsigned long long inc64(unsigned long long a)
3192 unsigned long long res;
3193 #ifdef __x86_64__
3194 /* Using the A constraint is wrong, and increments are tested
3195 elsewhere. */
3196 res = a + 1;
3197 #else
3198 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
3199 #endif
3200 return res;
3203 struct struct123 {
3204 int a;
3205 int b;
3207 struct struct1231 {
3208 unsigned long addr;
3211 unsigned long mconstraint_test(struct struct1231 *r)
3213 unsigned long ret;
3214 unsigned int a[2];
3215 a[0] = 0;
3216 __asm__ volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
3217 : "=&r" (ret), "=m" (a)
3218 : "m" (*(struct struct123 *)r->addr));
3219 return ret + a[0];
3222 #ifdef __x86_64__
3223 int fls64(unsigned long long x)
3225 int bitpos = -1;
3226 asm("bsrq %1,%q0"
3227 : "+r" (bitpos)
3228 : "rm" (x));
3229 return bitpos + 1;
3231 #endif
3233 void other_constraints_test(void)
3235 unsigned long ret;
3236 int var;
3237 #ifndef _WIN64
3238 __asm__ volatile ("mov %P1,%0" : "=r" (ret) : "p" (&var));
3239 printf ("oc1: %d\n", ret == (unsigned long)&var);
3240 #endif
3243 #ifndef _WIN32
3244 /* Test global asm blocks playing with aliases. */
3245 void base_func(void)
3247 printf ("asmc: base\n");
3250 extern void override_func1 (void);
3251 extern void override_func2 (void);
3253 asm(".weak override_func1\n.set override_func1, base_func");
3254 asm(".set override_func1, base_func");
3255 asm(".set override_func2, base_func");
3257 void override_func2 (void)
3259 printf ("asmc: override2\n");
3262 /* This checks a construct used by the linux kernel to encode
3263 references to strings by PC relative references. */
3264 extern int bug_table[] __attribute__((section("__bug_table")));
3265 char * get_asm_string (void)
3267 extern int some_symbol;
3268 asm volatile (".globl some_symbol\n"
3269 "jmp .+6\n"
3270 "1:\n"
3271 "some_symbol: .long 0\n"
3272 ".pushsection __bug_table, \"a\"\n"
3273 ".globl bug_table\n"
3274 "bug_table:\n"
3275 /* The first entry (1b-2b) is unused in this test,
3276 but we include it to check if cross-section
3277 PC-relative references work. */
3278 "2:\t.long 1b - 2b, %c0 - 2b\n"
3279 ".popsection\n" : : "i" ("A string"));
3280 char * str = ((char*)bug_table) + bug_table[1];
3281 return str;
3284 /* This checks another constructs with local labels. */
3285 extern unsigned char alld_stuff[];
3286 asm(".data\n"
3287 ".byte 41\n"
3288 "alld_stuff:\n"
3289 "661:\n"
3290 ".byte 42\n"
3291 "662:\n"
3292 ".pushsection .data.ignore\n"
3293 ".long 661b - .\n" /* This reference to 661 generates an external sym
3294 which shouldn't somehow overwrite the offset that's
3295 already determined for it. */
3296 ".popsection\n"
3297 ".byte 662b - 661b\n" /* So that this value is undeniably 1. */);
3299 void asm_local_label_diff (void)
3301 printf ("asm_local_label_diff: %d %d\n", alld_stuff[0], alld_stuff[1]);
3304 /* This checks that static local variables are available from assembler. */
3305 void asm_local_statics (void)
3307 static int localint = 41;
3308 asm("incl %0" : "+m" (localint));
3309 printf ("asm_local_statics: %d\n", localint);
3311 #endif
3313 static
3314 unsigned int set;
3316 void fancy_copy (unsigned *in, unsigned *out)
3318 asm volatile ("" : "=r" (*out) : "0" (*in));
3321 void fancy_copy2 (unsigned *in, unsigned *out)
3323 asm volatile ("mov %0,(%1)" : : "r" (*in), "r" (out) : "memory");
3326 #if defined __x86_64__ && !defined _WIN64
3327 void clobber_r12(void)
3329 asm volatile("mov $1, %%r12" ::: "r12");
3331 #endif
3333 void test_high_clobbers(void)
3335 #if defined __x86_64__ && !defined _WIN64
3336 register long val asm("r12");
3337 long val2;
3338 /* This tests if asm clobbers correctly save/restore callee saved
3339 registers if they are clobbered and if it's the high 8 x86-64
3340 registers. This is fragile for GCC as the constraints do not
3341 correctly capture the data flow, but good enough for us. */
3342 asm volatile("mov $0x4542, %%r12" : "=r" (val):: "memory");
3343 clobber_r12();
3344 asm volatile("mov %%r12, %0" : "=r" (val2) : "r" (val): "memory");
3345 printf("asmhc: 0x%x\n", val2);
3346 #endif
3349 static long cpu_number;
3350 void trace_console(long len, long len2)
3352 #ifdef __x86_64__
3353 /* This generated invalid code when the emission of the switch
3354 table isn't disabled. The asms are necessary to show the bug,
3355 normal statements don't work (they need to generate some code
3356 even under nocode_wanted, which normal statements don't do,
3357 but asms do). Also at least these number of cases is necessary
3358 to generate enough "random" bytes. They ultimately are enough
3359 to create invalid instruction patterns to which the first
3360 skip-to-decision-table jump jumps. If decision table emission
3361 is disabled all of this is no problem.
3363 It also is necessary that the switches are in a statement expression
3364 (which has the property of not being enterable from outside. no
3365 matter what). */
3366 if (0
3369 long pscr_ret__;
3370 switch(len) {
3371 case 4:
3373 long pfo_ret__;
3374 switch (len2) {
3375 case 8: printf("bla"); pfo_ret__ = 42; break;
3377 pscr_ret__ = pfo_ret__;
3379 break;
3380 case 8:
3382 long pfo_ret__;
3383 switch (len2) {
3384 case 1:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3385 case 2:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3386 case 4:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3387 case 8:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3388 default: printf("impossible\n");
3390 pscr_ret__ = pfo_ret__;
3392 break;
3394 pscr_ret__;
3397 printf("huh?\n");
3399 #endif
3402 void test_asm_dead_code(void)
3404 long rdi;
3405 /* Try to make sure that xdi contains a zero, and hence will
3406 lead to a segfault if the next asm is evaluated without
3407 arguments being set up. */
3408 asm volatile ("" : "=D" (rdi) : "0" (0));
3409 (void)sizeof (({
3410 int var;
3411 /* This shouldn't trigger a segfault, either the argument
3412 registers need to be set up and the asm emitted despite
3413 this being in an unevaluated context, or both the argument
3414 setup _and_ the asm emission need to be suppressed. The latter
3415 is better. Disabling asm code gen when suppression is on
3416 also fixes the above trace_console bug, but that came earlier
3417 than asm suppression. */
3418 asm volatile ("movl $0,(%0)" : : "D" (&var) : "memory");
3419 var;
3420 }));
3423 void test_asm_call(void)
3425 #if defined __x86_64__ && !defined _WIN64
3426 static char str[] = "PATH";
3427 char *s;
3428 /* This tests if a reference to an undefined symbol from an asm
3429 block, which isn't otherwise referenced in this file, is correctly
3430 regarded as global symbol, so that it's resolved by other object files
3431 or libraries. We chose getenv here, which isn't used anywhere else
3432 in this file. (If we used e.g. printf, which is used we already
3433 would have a global symbol entry, not triggering the bug which is
3434 tested here). */
3435 /* two pushes so stack remains aligned */
3436 asm volatile ("push %%rdi; push %%rdi; mov %0, %%rdi;"
3437 #if 1 && !defined(__TINYC__) && (defined(__PIC__) || defined(__PIE__))
3438 "call getenv@plt;"
3439 #else
3440 "call getenv;"
3441 #endif
3442 "pop %%rdi; pop %%rdi"
3443 : "=a" (s) : "r" (str));
3444 printf("asmd: %s\n", s);
3445 #endif
3448 #if defined __x86_64__
3449 # define RX "(%rip)"
3450 #else
3451 # define RX
3452 #endif
3454 void asm_dot_test(void)
3456 int x;
3457 for (x = 1;; ++x) {
3458 int r = x;
3459 switch (x) {
3460 case 1:
3461 asm(".text; lea S"RX",%eax; lea ."RX",%ecx; sub %ecx,%eax; S=.; jmp p0");
3462 case 2:
3463 asm(".text; jmp .+6; .int 123; mov .-4"RX",%eax; jmp p0");
3464 case 3:
3465 #ifndef _WIN32
3466 asm(".pushsection \".data\"; Y=.; .int 999; X=Y; .int 456; X=.-4; .popsection");
3467 #else
3468 asm(".data; Y=.; .int 999; X=Y; .int 456; X=.-4; .text");
3469 #endif
3470 asm(".text; mov X"RX",%eax; jmp p0");
3471 case 4:
3472 #ifndef _WIN32
3473 asm(".data; X=.; .int 789; Y=.; .int 999; .previous");
3474 #else
3475 asm(".data; X=.; .int 789; Y=.; .int 999; .text");
3476 #endif
3477 asm(".text; mov X"RX",%eax; X=Y; jmp p0");
3478 case 0:
3479 asm(".text; p0=.; mov %%eax,%0;" : "=m"(r)); break;
3481 if (r == x)
3482 break;
3483 printf("asm_dot_test %d: %d\n", x, r);
3487 void asm_test(void)
3489 char buf[128];
3490 unsigned int val, val2;
3491 struct struct123 s1;
3492 struct struct1231 s2 = { (unsigned long)&s1 };
3493 /* Hide the outer base_func, but check later that the inline
3494 asm block gets the outer one. */
3495 int base_func = 42;
3496 void override_func3 (void);
3497 unsigned long asmret;
3498 #ifdef BOOL_ISOC99
3499 _Bool somebool;
3500 #endif
3501 register int regvar asm("%esi");
3503 printf("inline asm:\n");
3505 // parse 0x1E-1 as 3 tokens in asm mode
3506 asm volatile ("mov $0x1E-1,%eax");
3508 /* test the no operand case */
3509 asm volatile ("xorl %eax, %eax");
3511 memcpy1(buf, "hello", 6);
3512 strncat1(buf, " worldXXXXX", 3);
3513 printf("%s\n", buf);
3515 memcpy2(buf, "hello", 6);
3516 strncat2(buf, " worldXXXXX", 3);
3517 printf("%s\n", buf);
3519 /* 'A' constraint test */
3520 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
3521 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
3523 s1.a = 42;
3524 s1.b = 43;
3525 printf("mconstraint: %d", mconstraint_test(&s2));
3526 printf(" %d %d\n", s1.a, s1.b);
3527 other_constraints_test();
3528 set = 0xff;
3529 sigdelset1(&set, 2);
3530 sigaddset1(&set, 16);
3531 /* NOTE: we test here if C labels are correctly restored after the
3532 asm statement */
3533 goto label1;
3534 label2:
3535 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
3536 printf("set=0x%x\n", set);
3537 val = 0x01020304;
3538 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
3539 #ifndef _WIN32
3540 override_func1();
3541 override_func2();
3542 /* The base_func ref from the following inline asm should find
3543 the global one, not the local decl from this function. */
3544 asm volatile(".weak override_func3\n.set override_func3, base_func");
3545 override_func3();
3546 printf("asmstr: %s\n", get_asm_string());
3547 asm_local_label_diff();
3548 asm_local_statics();
3549 #endif
3550 /* Check that we can also load structs of appropriate layout
3551 into registers. */
3552 asm volatile("" : "=r" (asmret) : "0"(s2));
3553 if (asmret != s2.addr)
3554 printf("asmstr: failed\n");
3555 #ifdef BOOL_ISOC99
3556 /* Check that the typesize correctly sets the register size to
3557 8 bit. */
3558 asm volatile("cmp %1,%2; sete %0" : "=a"(somebool) : "r"(1), "r"(2));
3559 if (!somebool)
3560 printf("asmbool: failed\n");
3561 #endif
3562 val = 43;
3563 fancy_copy (&val, &val2);
3564 printf ("fancycpy(%d)=%d\n", val, val2);
3565 val = 44;
3566 fancy_copy2 (&val, &val2);
3567 printf ("fancycpy2(%d)=%d\n", val, val2);
3568 asm volatile ("mov $0x4243, %%esi" : "=r" (regvar));
3569 printf ("regvar=%x\n", regvar);
3570 test_high_clobbers();
3571 trace_console(8, 8);
3572 test_asm_dead_code();
3573 test_asm_call();
3574 asm_dot_test();
3575 return;
3576 label1:
3577 goto label2;
3580 #else
3582 void asm_test(void)
3586 #endif
3588 #define COMPAT_TYPE(type1, type2) \
3590 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
3591 __builtin_types_compatible_p (type1, type2));\
3594 int constant_p_var;
3596 void builtin_test(void)
3598 short s;
3599 int i;
3600 long long ll;
3601 #if GCC_MAJOR >= 3
3602 COMPAT_TYPE(int, int);
3603 COMPAT_TYPE(int, unsigned int);
3604 COMPAT_TYPE(int, char);
3605 COMPAT_TYPE(int, const int);
3606 COMPAT_TYPE(int, volatile int);
3607 COMPAT_TYPE(int *, int *);
3608 COMPAT_TYPE(int *, void *);
3609 COMPAT_TYPE(int *, const int *);
3610 COMPAT_TYPE(char *, unsigned char *);
3611 COMPAT_TYPE(char *, signed char *);
3612 COMPAT_TYPE(char *, char *);
3613 /* space is needed because tcc preprocessor introduces a space between each token */
3614 COMPAT_TYPE(char * *, void *);
3615 #endif
3616 printf("res = %d\n", __builtin_constant_p(1));
3617 printf("res = %d\n", __builtin_constant_p(1 + 2));
3618 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
3619 printf("res = %d\n", __builtin_constant_p(constant_p_var));
3620 printf("res = %d\n", __builtin_constant_p(100000 / constant_p_var));
3621 s = 1;
3622 ll = 2;
3623 i = __builtin_choose_expr (1 != 0, ll, s);
3624 printf("bce: %d\n", i);
3625 i = __builtin_choose_expr (1 != 1, ll, s);
3626 printf("bce: %d\n", i);
3627 i = sizeof (__builtin_choose_expr (1, ll, s));
3628 printf("bce: %d\n", i);
3629 i = sizeof (__builtin_choose_expr (0, ll, s));
3630 printf("bce: %d\n", i);
3632 //printf("bera: %p\n", __builtin_extract_return_addr((void*)43));
3635 #ifndef _WIN32
3636 extern int __attribute__((weak)) weak_f1(void);
3637 extern int __attribute__((weak)) weak_f2(void);
3638 extern int weak_f3(void);
3639 extern int __attribute__((weak)) weak_v1;
3640 extern int __attribute__((weak)) weak_v2;
3641 extern int weak_v3;
3643 extern int (*weak_fpa)() __attribute__((weak));
3644 extern int __attribute__((weak)) (*weak_fpb)();
3645 extern __attribute__((weak)) int (*weak_fpc)();
3647 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
3648 extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
3649 extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
3650 extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
3651 extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
3652 extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
3654 static const size_t dummy = 0;
3655 extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
3656 extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
3657 extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
3659 int some_lib_func(void);
3660 int dummy_impl_of_slf(void) { return 444; }
3661 int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
3663 int weak_toolate() __attribute__((weak));
3664 int weak_toolate() { return 0; }
3666 void __attribute__((weak)) weak_test(void)
3668 printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
3669 printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
3670 printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
3671 printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
3672 printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
3673 printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
3675 printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
3676 printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
3677 printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
3679 printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
3680 printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
3681 printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
3682 printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
3683 printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
3684 printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
3685 printf("some_lib_func=%d\n", &some_lib_func ? some_lib_func() : 0);
3688 int __attribute__((weak)) weak_f2() { return 222; }
3689 int __attribute__((weak)) weak_f3() { return 333; }
3690 int __attribute__((weak)) weak_v2 = 222;
3691 int __attribute__((weak)) weak_v3 = 333;
3692 #endif
3694 void const_func(const int a)
3698 void const_warn_test(void)
3700 const_func(1);
3703 struct condstruct {
3704 int i;
3707 int getme (struct condstruct *s, int i)
3709 int i1 = (i == 0 ? 0 : s)->i;
3710 int i2 = (i == 0 ? s : 0)->i;
3711 int i3 = (i == 0 ? (void*)0 : s)->i;
3712 int i4 = (i == 0 ? s : (void*)0)->i;
3713 return i1 + i2 + i3 + i4;
3716 struct global_data
3718 int a[40];
3719 int *b[40];
3722 struct global_data global_data;
3724 int global_data_getstuff (int *, int);
3726 void global_data_callit (int i)
3728 *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
3731 int global_data_getstuff (int *p, int i)
3733 return *p + i;
3736 void global_data_test (void)
3738 global_data.a[0] = 42;
3739 global_data.b[0] = &global_data.a[0];
3740 global_data_callit (0);
3741 printf ("%d\n", global_data.a[0]);
3744 struct cmpcmpS
3746 unsigned char fill : 3;
3747 unsigned char b1 : 1;
3748 unsigned char b2 : 1;
3749 unsigned char fill2 : 3;
3752 int glob1, glob2, glob3;
3754 void compare_comparisons (struct cmpcmpS *s)
3756 if (s->b1 != (glob1 == glob2)
3757 || (s->b2 != (glob1 == glob3)))
3758 printf ("comparing comparisons broken\n");
3761 void cmp_comparison_test(void)
3763 struct cmpcmpS s;
3764 s.b1 = 1;
3765 glob1 = 42; glob2 = 42;
3766 s.b2 = 0;
3767 glob3 = 43;
3768 compare_comparisons (&s);
3771 int fcompare (double a, double b, int code)
3773 switch (code) {
3774 case 0: return a == b;
3775 case 1: return a != b;
3776 case 2: return a < b;
3777 case 3: return a >= b;
3778 case 4: return a > b;
3779 case 5: return a <= b;
3781 return 0;
3784 void math_cmp_test(void)
3786 double nan = 0.0/0.0;
3787 double one = 1.0;
3788 double two = 2.0;
3789 int comp = 0;
3790 int v;
3791 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
3793 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
3794 And it does this in various ways so that all code generation paths
3795 are checked (generating inverted tests, or non-inverted tests, or
3796 producing a 0/1 value without jumps (that's done in the fcompare
3797 function). */
3798 #define FCMP(a,b,op,iop,code) \
3799 if (fcompare (a,b,code)) \
3800 bug (a,b,op,iop,1); \
3801 if (a op b) \
3802 bug (a,b,op,iop,2); \
3803 if (a iop b) \
3805 else \
3806 bug (a,b,op,iop,3); \
3807 if ((a op b) || comp) \
3808 bug (a,b,op,iop,4); \
3809 if ((a iop b) || comp) \
3811 else \
3812 bug (a,b,op,iop,5); \
3813 if (v = !(a op b), !v) bug(a,b,op,iop,7);
3815 /* Equality tests. */
3816 FCMP(nan, nan, ==, !=, 0);
3817 FCMP(one, two, ==, !=, 0);
3818 FCMP(one, one, !=, ==, 1);
3819 /* Non-equality is a bit special. */
3820 if (!fcompare (nan, nan, 1))
3821 bug (nan, nan, !=, ==, 6);
3823 /* Relational tests on numbers. */
3824 FCMP(two, one, <, >=, 2);
3825 FCMP(one, two, >=, <, 3);
3826 FCMP(one, two, >, <=, 4);
3827 FCMP(two, one, <=, >, 5);
3829 /* Relational tests on NaNs. Note that the inverse op here is
3830 always !=, there's no operator in C that is equivalent to !(a < b),
3831 when NaNs are involved, same for the other relational ops. */
3832 FCMP(nan, nan, <, !=, 2);
3833 FCMP(nan, nan, >=, !=, 3);
3834 FCMP(nan, nan, >, !=, 4);
3835 FCMP(nan, nan, <=, !=, 5);
3838 double get100 () { return 100.0; }
3840 void callsave_test(void)
3842 #if defined __i386__ || defined __x86_64__ || defined __arm__
3843 int i, s; double *d; double t;
3844 s = sizeof (double);
3845 printf ("callsavetest: %d\n", s);
3846 d = alloca (sizeof(double));
3847 d[0] = 10.0;
3848 /* x86-64 had a bug were the next call to get100 would evict
3849 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
3850 in int type, not pointer type. When alloca returns a pointer
3851 with the high 32 bit set (which is likely on x86-64) the access
3852 generates a segfault. */
3853 i = d[0] > get100 ();
3854 printf ("%d\n", i);
3855 #endif
3859 void bfa3(ptrdiff_t str_offset)
3861 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
3863 void bfa2(ptrdiff_t str_offset)
3865 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
3866 bfa3(str_offset);
3868 void bfa1(ptrdiff_t str_offset)
3870 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
3871 bfa2(str_offset);
3874 void builtin_frame_address_test(void)
3876 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
3877 #ifndef __arm__
3878 char str[] = "__builtin_frame_address";
3879 char *fp0 = __builtin_frame_address(0);
3881 printf("str: %s\n", str);
3882 #ifndef __riscv
3883 bfa1(str-fp0);
3884 #endif
3885 #endif
3888 char via_volatile (char i)
3890 char volatile vi;
3891 vi = i;
3892 return vi;
3895 struct __attribute__((__packed__)) Spacked {
3896 char a;
3897 short b;
3898 int c;
3900 struct Spacked spacked;
3901 typedef struct __attribute__((__packed__)) {
3902 char a;
3903 short b;
3904 int c;
3905 } Spacked2;
3906 Spacked2 spacked2;
3907 typedef struct Spacked3_s {
3908 char a;
3909 short b;
3910 int c;
3911 } __attribute__((__packed__)) Spacked3;
3912 Spacked3 spacked3;
3913 struct gate_struct64 {
3914 unsigned short offset_low;
3915 unsigned short segment;
3916 unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1;
3917 unsigned short offset_middle;
3918 unsigned offset_high;
3919 unsigned zero1;
3920 } __attribute__((packed));
3921 typedef struct gate_struct64 gate_desc;
3922 gate_desc a_gate_desc;
3923 void attrib_test(void)
3925 #ifndef _WIN32
3926 printf("attr: %d %d %d %d\n", sizeof(struct Spacked),
3927 sizeof(spacked), sizeof(Spacked2), sizeof(spacked2));
3928 printf("attr: %d %d\n", sizeof(Spacked3), sizeof(spacked3));
3929 printf("attr: %d %d\n", sizeof(gate_desc), sizeof(a_gate_desc));
3930 #endif
3932 extern __attribute__((__unused__)) char * __attribute__((__unused__)) *
3933 strange_attrib_placement (void);
3935 void * __attribute__((__unused__)) get_void_ptr (void *a)
3937 return a;
3940 /* This part checks for a bug in TOK_GET (used for inline expansion),
3941 where the large long long constant left the the high bits set for
3942 the integer constant token. */
3943 static inline
3944 int __get_order(unsigned long long size)
3946 int order;
3947 size -= 0xffff880000000000ULL; // this const left high bits set in the token
3949 struct S { int i : 1; } s; // constructed for this '1'
3951 order = size;
3952 return order;
3955 /* This just forces the above inline function to be actually emitted. */
3956 int force_get_order(unsigned long s)
3958 return __get_order(s);