riscv: fix more sign/zero-extension problems
[tinycc.git] / tests / tcctest.c
blobe4645bb5f065be33aa0ab7c4f99f09adb737a49a
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 unsigned long ul = 0x80000000UL;
1637 p -= 0x700000000042;
1639 printf("cast_test:\n");
1640 a = 0xfffff;
1641 cast1(a, a, a, a);
1642 a = 0xffffe;
1643 printf("%d %d %d %d\n",
1644 (char)(a + 1),
1645 (short)(a + 1),
1646 (unsigned char)(a + 1),
1647 (unsigned short)(a + 1));
1648 printf("%d %d %d %d\n",
1649 (char)0xfffff,
1650 (short)0xfffff,
1651 (unsigned char)0xfffff,
1652 (unsigned short)0xfffff);
1654 a = (bcast = 128) + 1;
1655 printf("%d\n", a);
1656 a = (scast = 65536) + 1;
1657 printf("%d\n", a);
1659 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1661 /* test cast from unsigned to signed short to int */
1662 b = 0xf000;
1663 d = (short)b;
1664 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1665 b = 0xf0f0;
1666 d = (char)b;
1667 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1669 /* test implicit int casting for array accesses */
1670 c = 0;
1671 tab[1] = 2;
1672 tab[c] = 1;
1673 printf("%d %d\n", tab[0], tab[1]);
1675 /* test implicit casting on some operators */
1676 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1677 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1678 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1680 /* from pointer to integer types */
1681 printf("%d %d %ld %ld %lld %lld\n",
1682 (int)p, (unsigned int)p,
1683 (long)p, (unsigned long)p,
1684 (long long)p, (unsigned long long)p);
1686 /* from integers to pointers */
1687 printf("%p %p %p %p\n",
1688 (void *)a, (void *)b, (void *)c, (void *)d);
1690 /* int to int with sign set */
1691 printf("0x%lx\n", (unsigned long)(int)ul);
1694 /* initializers tests */
1695 struct structinit1 {
1696 int f1;
1697 char f2;
1698 short f3;
1699 int farray[3];
1702 int sinit1 = 2;
1703 int sinit2 = { 3 };
1704 int sinit3[3] = { 1, 2, {{3}}, };
1705 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1706 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1707 int sinit6[] = { 1, 2, 3 };
1708 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1709 char sinit8[] = "hello" "trala";
1711 struct structinit1 sinit9 = { 1, 2, 3 };
1712 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1713 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1714 #ifdef ALL_ISOC99
1715 .farray[0] = 10,
1716 .farray[1] = 11,
1717 .farray[2] = 12,
1718 #endif
1721 char *sinit12 = "hello world";
1722 char *sinit13[] = {
1723 "test1",
1724 "test2",
1725 "test3",
1727 char sinit14[10] = { "abc" };
1728 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1730 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1732 struct bar {
1733 char *s;
1734 int len;
1735 } sinit17[] = {
1736 "a1", 4,
1737 "a2", 1
1740 int sinit18[10] = {
1741 [2 ... 5] = 20,
1743 [8] = 10,
1746 struct complexinit0 {
1747 int a;
1748 int b;
1751 struct complexinit {
1752 int a;
1753 const struct complexinit0 *b;
1756 const static struct complexinit cix[] = {
1757 [0] = {
1758 .a = 2000,
1759 .b = (const struct complexinit0[]) {
1760 { 2001, 2002 },
1761 { 2003, 2003 },
1767 struct complexinit2 {
1768 int a;
1769 int b[];
1772 struct complexinit2 cix20;
1774 struct complexinit2 cix21 = {
1775 .a = 3000,
1776 .b = { 3001, 3002, 3003 }
1779 struct complexinit2 cix22 = {
1780 .a = 4000,
1781 .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1784 typedef int arrtype1[];
1785 arrtype1 sinit19 = {1};
1786 arrtype1 sinit20 = {2,3};
1787 typedef int arrtype2[3];
1788 arrtype2 sinit21 = {4};
1789 arrtype2 sinit22 = {5,6,7};
1791 /* Address comparisons of non-weak symbols with zero can be const-folded */
1792 int sinit23[2] = { "astring" ? sizeof("astring") : -1,
1793 &sinit23 ? 42 : -1 };
1795 extern int external_inited = 42;
1797 void init_test(void)
1799 int linit1 = 2;
1800 int linit2 = { 3 };
1801 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1802 int linit6[] = { 1, 2, 3 };
1803 int i, j;
1804 char linit8[] = "hello" "trala";
1805 int linit12[10] = { 1, 2 };
1806 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1807 char linit14[10] = "abc";
1808 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1809 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1810 int linit17 = sizeof(linit17);
1811 int zero = 0;
1812 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1813 int linit18[2] = {&zero ? 1 : -1, zero ? -1 : 1 };
1815 printf("init_test:\n");
1817 printf("sinit1=%d\n", sinit1);
1818 printf("sinit2=%d\n", sinit2);
1819 printf("sinit3=%d %d %d %d\n",
1820 sizeof(sinit3),
1821 sinit3[0],
1822 sinit3[1],
1823 sinit3[2]
1825 printf("sinit6=%d\n", sizeof(sinit6));
1826 printf("sinit7=%d %d %d %d\n",
1827 sizeof(sinit7),
1828 sinit7[0],
1829 sinit7[1],
1830 sinit7[2]
1832 printf("sinit8=%s\n", sinit8);
1833 printf("sinit9=%d %d %d\n",
1834 sinit9.f1,
1835 sinit9.f2,
1836 sinit9.f3
1838 printf("sinit10=%d %d %d\n",
1839 sinit10.f1,
1840 sinit10.f2,
1841 sinit10.f3
1843 printf("sinit11=%d %d %d %d %d %d\n",
1844 sinit11.f1,
1845 sinit11.f2,
1846 sinit11.f3,
1847 sinit11.farray[0],
1848 sinit11.farray[1],
1849 sinit11.farray[2]
1852 for(i=0;i<3;i++)
1853 for(j=0;j<2;j++)
1854 printf("[%d][%d] = %d %d %d\n",
1855 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1856 printf("linit1=%d\n", linit1);
1857 printf("linit2=%d\n", linit2);
1858 printf("linit6=%d\n", sizeof(linit6));
1859 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1861 printf("sinit12=%s\n", sinit12);
1862 printf("sinit13=%d %s %s %s\n",
1863 sizeof(sinit13),
1864 sinit13[0],
1865 sinit13[1],
1866 sinit13[2]);
1867 printf("sinit14=%s\n", sinit14);
1869 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1870 printf("\n");
1871 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1872 printf("\n");
1873 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1874 printf("\n");
1875 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1876 printf("\n");
1877 printf("%d %d %d %d\n",
1878 linit16.a1,
1879 linit16.a2,
1880 linit16.a3,
1881 linit16.a4);
1882 /* test that initialisation is done after variable declare */
1883 printf("linit17=%d\n", linit17);
1884 printf("sinit15=%d\n", sinit15[0]);
1885 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1886 printf("sinit17=%s %d %s %d\n",
1887 sinit17[0].s, sinit17[0].len,
1888 sinit17[1].s, sinit17[1].len);
1889 for(i=0;i<10;i++)
1890 printf("%x ", sinit18[i]);
1891 printf("\n");
1892 /* complex init check */
1893 printf("cix: %d %d %d %d %d %d %d\n",
1894 cix[0].a,
1895 cix[0].b[0].a, cix[0].b[0].b,
1896 cix[0].b[1].a, cix[0].b[1].b,
1897 cix[0].b[2].a, cix[0].b[2].b);
1898 printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
1899 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
1901 printf("arrtype1: %d %d %d\n", sinit19[0], sinit20[0], sinit20[1]);
1902 printf("arrtype2: %d %d\n", sizeof(sinit19), sizeof(sinit20));
1903 printf("arrtype3: %d %d %d\n", sinit21[0], sinit21[1], sinit21[2]);
1904 printf("arrtype4: %d %d %d\n", sinit22[0], sinit22[1], sinit22[2]);
1905 printf("arrtype5: %d %d\n", sizeof(sinit21), sizeof(sinit22));
1906 printf("arrtype6: %d\n", sizeof(arrtype2));
1908 printf("sinit23= %d %d\n", sinit23[0], sinit23[1]);
1909 printf("linit18= %d %d\n", linit18[0], linit18[1]);
1912 void switch_uc(unsigned char uc)
1914 switch (uc) {
1915 case 0xfb ... 0xfe:
1916 printf("ucsw:1\n");
1917 break;
1918 case 0xff:
1919 printf("ucsw:2\n");
1920 break;
1921 case 0 ... 5:
1922 printf("ucsw:3\n");
1923 break;
1924 default:
1925 printf("ucsw: broken!\n");
1929 void switch_sc(signed char sc)
1931 switch (sc) {
1932 case -5 ... -2:
1933 printf("scsw:1\n");
1934 break;
1935 case -1:
1936 printf("scsw:2\n");
1937 break;
1938 case 0 ... 5:
1939 printf("scsw:3\n");
1940 break;
1941 default:
1942 printf("scsw: broken!\n");
1946 void switch_test()
1948 int i;
1949 unsigned long long ull;
1950 long long ll;
1952 for(i=0;i<15;i++) {
1953 switch(i) {
1954 case 0:
1955 case 1:
1956 printf("a");
1957 break;
1958 default:
1959 printf("%d", i);
1960 break;
1961 case 8 ... 12:
1962 printf("c");
1963 break;
1964 case 3:
1965 printf("b");
1966 break;
1967 case 0xc33c6b9fU:
1968 case 0x7c9eeeb9U:
1969 break;
1972 printf("\n");
1974 for (i = 1; i <= 5; i++) {
1975 ull = (unsigned long long)i << 61;
1976 switch (ull) {
1977 case 1ULL << 61:
1978 printf("ullsw:1\n");
1979 break;
1980 case 2ULL << 61:
1981 printf("ullsw:2\n");
1982 break;
1983 case 3ULL << 61:
1984 printf("ullsw:3\n");
1985 break;
1986 case 4ULL << 61:
1987 printf("ullsw:4\n");
1988 break;
1989 case 5ULL << 61:
1990 printf("ullsw:5\n");
1991 break;
1992 default:
1993 printf("ullsw: broken!\n");
1997 for (i = 1; i <= 5; i++) {
1998 ll = (long long)i << 61;
1999 switch (ll) {
2000 case 1LL << 61:
2001 printf("llsw:1\n");
2002 break;
2003 case 2LL << 61:
2004 printf("llsw:2\n");
2005 break;
2006 case 3LL << 61:
2007 printf("llsw:3\n");
2008 break;
2009 case 4LL << 61:
2010 printf("llsw:4\n");
2011 break;
2012 case 5LL << 61:
2013 printf("llsw:5\n");
2014 break;
2015 default:
2016 printf("llsw: broken!\n");
2020 for (i = -5; i <= 5; i++) {
2021 switch_uc((unsigned char)i);
2024 for (i = -5; i <= 5; i++) {
2025 switch_sc ((signed char)i);
2029 /* ISOC99 _Bool type */
2030 void c99_bool_test(void)
2032 #ifdef BOOL_ISOC99
2033 int a;
2034 _Bool b;
2036 printf("bool_test:\n");
2037 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
2038 a = 3;
2039 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
2040 b = 3;
2041 printf("b = %d\n", b);
2042 b++;
2043 printf("b = %d\n", b);
2044 #endif
2047 void bitfield_test(void)
2049 int a;
2050 short sa;
2051 unsigned char ca;
2052 struct sbf1 {
2053 int f1 : 3;
2054 int : 2;
2055 int f2 : 1;
2056 int : 0;
2057 int f3 : 5;
2058 int f4 : 7;
2059 unsigned int f5 : 7;
2060 } st1;
2061 printf("bitfield_test:");
2062 printf("sizeof(st1) = %d\n", sizeof(st1));
2064 st1.f1 = 3;
2065 st1.f2 = 1;
2066 st1.f3 = 15;
2067 a = 120;
2068 st1.f4 = a;
2069 st1.f5 = a;
2070 st1.f5++;
2071 printf("%d %d %d %d %d\n",
2072 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
2073 sa = st1.f5;
2074 ca = st1.f5;
2075 printf("%d %d\n", sa, ca);
2077 st1.f1 = 7;
2078 if (st1.f1 == -1)
2079 printf("st1.f1 == -1\n");
2080 else
2081 printf("st1.f1 != -1\n");
2082 if (st1.f2 == -1)
2083 printf("st1.f2 == -1\n");
2084 else
2085 printf("st1.f2 != -1\n");
2087 struct sbf2 {
2088 long long f1 : 45;
2089 long long : 2;
2090 long long f2 : 35;
2091 unsigned long long f3 : 38;
2092 } st2;
2093 st2.f1 = 0x123456789ULL;
2094 a = 120;
2095 st2.f2 = (long long)a << 25;
2096 st2.f3 = a;
2097 st2.f2++;
2098 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
2100 #if 0
2101 Disabled for now until further clarification re GCC compatibility
2102 struct sbf3 {
2103 int f1 : 7;
2104 int f2 : 1;
2105 char f3;
2106 int f4 : 8;
2107 int f5 : 1;
2108 int f6 : 16;
2109 } st3;
2110 printf("sizeof(st3) = %d\n", sizeof(st3));
2111 #endif
2113 struct sbf4 {
2114 int x : 31;
2115 char y : 2;
2116 } st4;
2117 st4.y = 1;
2118 printf("st4.y == %d\n", st4.y);
2119 struct sbf5 {
2120 int a;
2121 char b;
2122 int x : 12, y : 4, : 0, : 4, z : 3;
2123 char c;
2124 } st5 = { 1, 2, 3, 4, -3, 6 };
2125 printf("st5 = %d %d %d %d %d %d\n", st5.a, st5.b, st5.x, st5.y, st5.z, st5.c);
2126 struct sbf6 {
2127 short x : 12;
2128 unsigned char y : 2;
2129 } st6;
2130 st6.y = 1;
2131 printf("st6.y == %d\n", st6.y);
2134 #ifdef __x86_64__
2135 #define FLOAT_FMT "%f\n"
2136 #else
2137 /* x86's float isn't compatible with GCC */
2138 #define FLOAT_FMT "%.5f\n"
2139 #endif
2141 /* declare strto* functions as they are C99 */
2142 double strtod(const char *nptr, char **endptr);
2144 #if defined(_WIN32)
2145 float strtof(const char *nptr, char **endptr) {return (float)strtod(nptr, endptr);}
2146 LONG_DOUBLE strtold(const char *nptr, char **endptr) {return (LONG_DOUBLE)strtod(nptr, endptr);}
2147 #else
2148 float strtof(const char *nptr, char **endptr);
2149 LONG_DOUBLE strtold(const char *nptr, char **endptr);
2150 #endif
2152 #define FTEST(prefix, typename, type, fmt)\
2153 void prefix ## cmp(type a, type b)\
2155 printf("%d %d %d %d %d %d\n",\
2156 a == b,\
2157 a != b,\
2158 a < b,\
2159 a > b,\
2160 a >= b,\
2161 a <= b);\
2162 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
2165 a + b,\
2166 a - b,\
2167 a * b,\
2168 a / b,\
2169 -a);\
2170 printf(fmt "\n", ++a);\
2171 printf(fmt "\n", a++);\
2172 printf(fmt "\n", a);\
2173 b = 0;\
2174 printf("%d %d\n", !a, !b);\
2176 void prefix ## fcast(type a)\
2178 float fa;\
2179 double da;\
2180 LONG_DOUBLE la;\
2181 int ia;\
2182 long long llia;\
2183 unsigned int ua;\
2184 unsigned long long llua;\
2185 type b;\
2186 fa = a;\
2187 da = a;\
2188 la = a;\
2189 printf("ftof: %f %f %Lf\n", fa, da, la);\
2190 ia = (int)a;\
2191 llia = (long long)a;\
2192 a = (a >= 0) ? a : -a;\
2193 ua = (unsigned int)a;\
2194 llua = (unsigned long long)a;\
2195 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
2196 ia = -1234;\
2197 ua = 0x81234500;\
2198 llia = -0x123456789012345LL;\
2199 llua = 0xf123456789012345LLU;\
2200 b = ia;\
2201 printf("itof: " fmt "\n", b);\
2202 b = ua;\
2203 printf("utof: " fmt "\n", b);\
2204 b = llia;\
2205 printf("lltof: " fmt "\n", b);\
2206 b = llua;\
2207 printf("ulltof: " fmt "\n", b);\
2210 float prefix ## retf(type a) { return a; }\
2211 double prefix ## retd(type a) { return a; }\
2212 LONG_DOUBLE prefix ## retld(type a) { return a; }\
2214 void prefix ## call(void)\
2216 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
2217 printf("double: %f\n", prefix ## retd(42.123456789));\
2218 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
2219 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
2222 void prefix ## signed_zeros(void) \
2224 type x = 0.0, y = -0.0, n, p;\
2225 if (x == y)\
2226 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
2227 1.0 / x != 1.0 / y);\
2228 else\
2229 printf ("x != y; this is wrong!\n");\
2231 n = -x;\
2232 if (x == n)\
2233 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
2234 1.0 / x != 1.0 / n);\
2235 else\
2236 printf ("x != -x; this is wrong!\n");\
2238 p = +y;\
2239 if (x == p)\
2240 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
2241 1.0 / x != 1.0 / p);\
2242 else\
2243 printf ("x != +y; this is wrong!\n");\
2244 p = -y;\
2245 if (x == p)\
2246 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
2247 1.0 / x != 1.0 / p);\
2248 else\
2249 printf ("x != -y; this is wrong!\n");\
2251 void prefix ## test(void)\
2253 printf("testing '%s'\n", #typename);\
2254 prefix ## cmp(1, 2.5);\
2255 prefix ## cmp(2, 1.5);\
2256 prefix ## cmp(1, 1);\
2257 prefix ## fcast(234.6);\
2258 prefix ## fcast(-2334.6);\
2259 prefix ## call();\
2260 prefix ## signed_zeros();\
2263 FTEST(f, float, float, "%f")
2264 FTEST(d, double, double, "%f")
2265 FTEST(ld, long double, LONG_DOUBLE, "%Lf")
2267 double ftab1[3] = { 1.2, 3.4, -5.6 };
2270 void float_test(void)
2272 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
2273 float fa, fb;
2274 double da, db;
2275 int a;
2276 unsigned int b;
2277 static double nan2 = 0.0/0.0;
2278 static double inf1 = 1.0/0.0;
2279 static double inf2 = 1e5000;
2281 printf("float_test:\n");
2282 printf("sizeof(float) = %d\n", sizeof(float));
2283 printf("sizeof(double) = %d\n", sizeof(double));
2284 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));
2285 ftest();
2286 dtest();
2287 ldtest();
2288 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
2289 printf("%f %f %f\n", 2.12, .5, 2.3e10);
2290 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
2291 da = 123;
2292 printf("da=%f\n", da);
2293 fa = 123;
2294 printf("fa=%f\n", fa);
2295 a = 4000000000;
2296 da = a;
2297 printf("da = %f\n", da);
2298 b = 4000000000;
2299 db = b;
2300 printf("db = %f\n", db);
2301 printf("nan != nan = %d, inf1 = %f, inf2 = %f\n", nan2 != nan2, inf1, inf2);
2302 #endif
2305 int fib(int n)
2307 if (n <= 2)
2308 return 1;
2309 else
2310 return fib(n-1) + fib(n-2);
2313 void __attribute__((aligned(16))) aligned_function(int i) {}
2315 void funcptr_test()
2317 void (*func)(int);
2318 int a;
2319 struct {
2320 int dummy;
2321 void (*func)(int);
2322 } st1;
2323 long diff;
2325 printf("funcptr:\n");
2326 func = &num;
2327 (*func)(12345);
2328 func = num;
2329 a = 1;
2330 a = 1;
2331 func(12345);
2332 /* more complicated pointer computation */
2333 st1.func = num;
2334 st1.func(12346);
2335 printf("sizeof1 = %d\n", sizeof(funcptr_test));
2336 printf("sizeof2 = %d\n", sizeof funcptr_test);
2337 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
2338 printf("sizeof4 = %d\n", sizeof &funcptr_test);
2339 a = 0;
2340 func = num + a;
2341 diff = func - num;
2342 func(42);
2343 (func + diff)(42);
2344 (num + a)(43);
2346 /* Check that we can align functions */
2347 func = aligned_function;
2348 printf("aligned_function (should be zero): %d\n", ((int)func) & 15);
2351 void lloptest(long long a, long long b)
2353 unsigned long long ua, ub;
2355 ua = a;
2356 ub = b;
2357 /* arith */
2358 printf("arith: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2359 a + b,
2360 a - b,
2361 a * b);
2363 if (b != 0) {
2364 printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2365 a / b,
2366 a % b);
2369 /* binary */
2370 printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2371 a & b,
2372 a | b,
2373 a ^ b);
2375 /* tests */
2376 printf("test: %d %d %d %d %d %d\n",
2377 a == b,
2378 a != b,
2379 a < b,
2380 a > b,
2381 a >= b,
2382 a <= b);
2384 printf("utest: %d %d %d %d %d %d\n",
2385 ua == ub,
2386 ua != ub,
2387 ua < ub,
2388 ua > ub,
2389 ua >= ub,
2390 ua <= ub);
2392 /* arith2 */
2393 a++;
2394 b++;
2395 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2396 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a++, b++);
2397 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", --a, --b);
2398 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2399 b = ub = 0;
2400 printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
2403 void llshift(long long a, int b)
2405 printf("shift: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2406 (unsigned long long)a >> b,
2407 a >> b,
2408 a << b);
2409 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2410 (unsigned long long)a >> 3,
2411 a >> 3,
2412 a << 3);
2413 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2414 (unsigned long long)a >> 35,
2415 a >> 35,
2416 a << 35);
2419 void llfloat(void)
2421 float fa;
2422 double da;
2423 LONG_DOUBLE lda;
2424 long long la, lb, lc;
2425 unsigned long long ula, ulb, ulc;
2426 la = 0x12345678;
2427 ula = 0x72345678;
2428 la = (la << 20) | 0x12345;
2429 ula = ula << 33;
2430 printf("la=" LONG_LONG_FORMAT " ula=" ULONG_LONG_FORMAT "\n", la, ula);
2432 fa = la;
2433 da = la;
2434 lda = la;
2435 printf("lltof: %f %f %Lf\n", fa, da, lda);
2437 la = fa;
2438 lb = da;
2439 lc = lda;
2440 printf("ftoll: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", la, lb, lc);
2442 fa = ula;
2443 da = ula;
2444 lda = ula;
2445 printf("ulltof: %f %f %Lf\n", fa, da, lda);
2447 ula = fa;
2448 ulb = da;
2449 ulc = lda;
2450 printf("ftoull: " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT "\n", ula, ulb, ulc);
2453 long long llfunc1(int a)
2455 return a * 2;
2458 struct S {
2459 int id;
2460 char item;
2463 long long int value(struct S *v)
2465 return ((long long int)v->item);
2468 long long llfunc2(long long x, long long y, int z)
2470 return x * y * z;
2473 void check_opl_save_regs(char *a, long long b, int c)
2475 *a = b < 0 && !c;
2478 void longlong_test(void)
2480 long long a, b, c;
2481 int ia;
2482 unsigned int ua;
2483 printf("longlong_test:\n");
2484 printf("sizeof(long long) = %d\n", sizeof(long long));
2485 ia = -1;
2486 ua = -2;
2487 a = ia;
2488 b = ua;
2489 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2490 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %Lx\n",
2491 (long long)1,
2492 (long long)-2,
2493 1LL,
2494 0x1234567812345679);
2495 a = llfunc1(-3);
2496 printf(LONG_LONG_FORMAT "\n", a);
2498 lloptest(1000, 23);
2499 lloptest(0xff, 0x1234);
2500 b = 0x72345678 << 10;
2501 lloptest(-3, b);
2502 llshift(0x123, 5);
2503 llshift(-23, 5);
2504 b = 0x72345678LL << 10;
2505 llshift(b, 47);
2507 llfloat();
2508 #if 1
2509 b = 0x12345678;
2510 a = -1;
2511 c = a + b;
2512 printf("%Lx\n", c);
2513 #endif
2515 /* long long reg spill test */
2517 struct S a;
2519 a.item = 3;
2520 printf("%lld\n", value(&a));
2522 lloptest(0x80000000, 0);
2525 long long *p, v, **pp;
2526 v = 1;
2527 p = &v;
2528 p[0]++;
2529 printf("another long long spill test : %lld\n", *p);
2530 pp = &p;
2532 v = llfunc2(**pp, **pp, ia);
2533 printf("a long long function (arm-)reg-args test : %lld\n", v);
2535 a = 68719476720LL;
2536 b = 4294967295LL;
2537 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
2539 printf(LONG_LONG_FORMAT "\n", 0x123456789LLU);
2541 /* long long pointer deref in argument passing test */
2542 a = 0x123;
2543 long long *p = &a;
2544 llshift(*p, 5);
2546 /* shortening followed by widening */
2547 unsigned long long u = 0x8000000000000001ULL;
2548 u = (unsigned)(u + 1);
2549 printf("long long u=" ULONG_LONG_FORMAT "\n", u);
2551 /* was a problem with missing save_regs in gen_opl on 32-bit platforms */
2552 char cc = 78;
2553 check_opl_save_regs(&cc, -1, 0);
2554 printf("check_opl_save_regs: %d\n", cc);
2557 void manyarg_test(void)
2559 LONG_DOUBLE ld = 1234567891234LL;
2560 printf("manyarg_test:\n");
2561 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %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 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2565 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2566 1, 2, 3, 4, 5, 6, 7, 8,
2567 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2568 1234567891234LL, 987654321986LL,
2569 42.0, 43.0);
2570 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2571 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2572 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2573 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2574 1234567891234LL, 987654321986LL,
2575 42.0, 43.0);
2576 printf("%d %d %d %d %d %d %d %d %Lf\n",
2577 1, 2, 3, 4, 5, 6, 7, 8, ld);
2578 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2579 LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n",
2580 1, 2, 3, 4, 5, 6, 7, 8,
2581 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2582 1234567891234LL, 987654321986LL,
2583 42.0, 43.0, ld);
2584 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2585 "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n",
2586 1, 2, 3, 4, 5, 6, 7, 8,
2587 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2588 ld, 1234567891234LL, 987654321986LL,
2589 42.0, 43.0, ld);
2592 void vprintf1(const char *fmt, ...)
2594 va_list ap, aq;
2595 const char *p;
2596 int c, i;
2597 double d;
2598 long long ll;
2599 LONG_DOUBLE ld;
2601 va_start(aq, fmt);
2602 va_copy(ap, aq);
2604 p = fmt;
2605 for(;;) {
2606 c = *p;
2607 if (c == '\0')
2608 break;
2609 p++;
2610 if (c == '%') {
2611 c = *p;
2612 switch(c) {
2613 case '\0':
2614 goto the_end;
2615 case 'd':
2616 i = va_arg(ap, int);
2617 printf("%d", i);
2618 break;
2619 case 'f':
2620 d = va_arg(ap, double);
2621 printf("%f", d);
2622 break;
2623 case 'l':
2624 ll = va_arg(ap, long long);
2625 printf(LONG_LONG_FORMAT, ll);
2626 break;
2627 case 'F':
2628 ld = va_arg(ap, LONG_DOUBLE);
2629 printf("%Lf", ld);
2630 break;
2632 p++;
2633 } else {
2634 putchar(c);
2637 the_end:
2638 va_end(aq);
2639 va_end(ap);
2642 struct myspace {
2643 short int profile;
2646 void stdarg_for_struct(struct myspace bob, ...)
2648 struct myspace george, bill;
2649 va_list ap;
2650 short int validate;
2652 va_start(ap, bob);
2653 bill = va_arg(ap, struct myspace);
2654 george = va_arg(ap, struct myspace);
2655 validate = va_arg(ap, int);
2656 printf("stdarg_for_struct: %d %d %d %d\n",
2657 bob.profile, bill.profile, george.profile, validate);
2658 va_end(ap);
2661 void stdarg_for_libc(const char *fmt, ...)
2663 va_list args;
2664 va_start(args, fmt);
2665 vprintf(fmt, args);
2666 va_end(args);
2669 void stdarg_syntax(int n, ...)
2671 int i;
2672 va_list ap;
2673 if (1)
2674 va_start(ap, n);
2675 else
2677 i = va_arg(ap, int);
2678 printf("stdarg_void_expr: %d\n", i);
2679 (va_end(ap));
2682 void stdarg_test(void)
2684 LONG_DOUBLE ld = 1234567891234LL;
2685 struct myspace bob;
2687 vprintf1("%d %d %d\n", 1, 2, 3);
2688 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2689 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2690 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2691 vprintf1("%d %f %l %F %d %f %l %F\n",
2692 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2693 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2694 1, 2, 3, 4, 5, 6, 7, 8,
2695 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2696 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %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 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2700 "%l %l %f %f\n",
2701 1, 2, 3, 4, 5, 6, 7, 8,
2702 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2703 1234567891234LL, 987654321986LL,
2704 42.0, 43.0);
2705 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2706 "%l %l %f %f\n",
2707 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2708 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2709 1234567891234LL, 987654321986LL,
2710 42.0, 43.0);
2711 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2712 1, 2, 3, 4, 5, 6, 7, 8, ld);
2713 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2714 "%l %l %f %f %F\n",
2715 1, 2, 3, 4, 5, 6, 7, 8,
2716 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2717 1234567891234LL, 987654321986LL,
2718 42.0, 43.0, ld);
2719 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2720 "%F %l %l %f %f %F\n",
2721 1, 2, 3, 4, 5, 6, 7, 8,
2722 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2723 ld, 1234567891234LL, 987654321986LL,
2724 42.0, 43.0, ld);
2726 bob.profile = 42;
2727 stdarg_for_struct(bob, bob, bob, bob.profile);
2728 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2729 stdarg_syntax(1, 17);
2732 void whitespace_test(void)
2734 char *str;
2736 \f\v #if 1
2737 pri\
2738 ntf("whitspace:\n");\f\v
2739 #endif
2740 pf("N=%d\n", 2);
2742 #ifdef CORRECT_CR_HANDLING
2743 pri\
2744 ntf("aaa=%d\n", 3);
2745 #endif
2747 pri\
2749 ntf("min=%d\n", 4);
2751 #ifdef ACCEPT_CR_IN_STRINGS
2752 printf("len1=%d\n", strlen("
2753 "));
2754 #ifdef CORRECT_CR_HANDLING
2755 str = "
2757 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
2758 #endif
2759 printf("len1=%d\n", strlen(" a
2760 "));
2761 #endif /* ACCEPT_CR_IN_STRINGS */
2764 int reltab[3] = { 1, 2, 3 };
2766 int *rel1 = &reltab[1];
2767 int *rel2 = &reltab[2];
2769 #ifdef _WIN64
2770 void relocation_test(void) {}
2771 #else
2772 void getmyaddress(void)
2774 printf("in getmyaddress\n");
2777 #ifdef __LP64__
2778 long __pa_symbol(void)
2780 /* This 64bit constant was handled incorrectly, it was used as addend
2781 (which can hold 64bit just fine) in connection with a symbol,
2782 and TCC generates wrong code for that (displacements are 32bit only).
2783 This effectively is "+ 0x80000000", and if addresses of globals
2784 are below 2GB the result should be a number without high 32 bits set. */
2785 return ((long)(((unsigned long)(&rel1))) - (0xffffffff80000000UL));
2787 #endif
2789 unsigned long theaddress = (unsigned long)getmyaddress;
2790 void relocation_test(void)
2792 void (*fptr)(void) = (void (*)(void))theaddress;
2793 printf("*rel1=%d\n", *rel1);
2794 printf("*rel2=%d\n", *rel2);
2795 fptr();
2796 #ifdef __LP64__
2797 printf("pa_symbol=0x%lx\n", __pa_symbol() >> 63);
2798 #endif
2800 #endif
2802 void old_style_f(a,b,c)
2803 int a, b;
2804 double c;
2806 printf("a=%d b=%d b=%f\n", a, b, c);
2809 void decl_func1(int cmpfn())
2811 printf("cmpfn=%lx\n", (long)cmpfn);
2814 void decl_func2(cmpfn)
2815 int cmpfn();
2817 printf("cmpfn=%lx\n", (long)cmpfn);
2820 void old_style_function(void)
2822 old_style_f((void *)1, 2, 3.0);
2823 decl_func1(NULL);
2824 decl_func2(NULL);
2827 void alloca_test()
2829 #if defined __i386__ || defined __x86_64__ || defined __arm__
2830 char *p = alloca(16);
2831 strcpy(p,"123456789012345");
2832 printf("alloca: p is %s\n", p);
2833 char *demo = "This is only a test.\n";
2834 /* Test alloca embedded in a larger expression */
2835 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
2836 #endif
2839 void *bounds_checking_is_enabled()
2841 char ca[10], *cp = ca-1;
2842 return (ca != cp + 1) ? cp : NULL;
2845 typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
2847 void c99_vla_test(int size1, int size2)
2849 #if defined __i386__ || defined __x86_64__
2850 int size = size1 * size2;
2851 int tab1[size][2], tab2[10][2];
2852 void *tab1_ptr, *tab2_ptr, *bad_ptr;
2854 /* "size" should have been 'captured' at tab1 declaration,
2855 so modifying it should have no effect on VLA behaviour. */
2856 size = size-1;
2858 printf("Test C99 VLA 1 (sizeof): ");
2859 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
2860 tab1_ptr = tab1;
2861 tab2_ptr = tab2;
2862 printf("Test C99 VLA 2 (ptrs subtract): ");
2863 printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2864 printf("Test C99 VLA 3 (ptr add): ");
2865 printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2866 printf("Test C99 VLA 4 (ptr access): ");
2867 tab1[size1][1] = 42;
2868 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2870 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2871 if (bad_ptr = bounds_checking_is_enabled()) {
2872 int *t1 = &tab1[size1 * size2 - 1][3];
2873 int *t2 = &tab2[9][3];
2874 printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
2875 printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
2877 char*c1 = 1 + sizeof(tab1) + (char*)tab1;
2878 char*c2 = 1 + sizeof(tab2) + (char*)tab2;
2879 printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
2880 printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
2882 int *i1 = tab1[-1];
2883 int *i2 = tab2[-1];
2884 printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
2885 printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
2887 int *x1 = tab1[size1 * size2 + 1];
2888 int *x2 = tab2[10 + 1];
2889 printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
2890 printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
2891 } else {
2892 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2894 printf("\n");
2895 #endif
2898 #ifndef __TINYC__
2899 typedef __SIZE_TYPE__ uintptr_t;
2900 #endif
2902 void sizeof_test(void)
2904 int a;
2905 int **ptr;
2907 printf("sizeof(int) = %d\n", sizeof(int));
2908 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2909 printf("sizeof(long) = %d\n", sizeof(long));
2910 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2911 printf("sizeof(short) = %d\n", sizeof(short));
2912 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2913 printf("sizeof(char) = %d\n", sizeof(char));
2914 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2915 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2916 a = 1;
2917 printf("sizeof(a++) = %d\n", sizeof a++);
2918 printf("a=%d\n", a);
2919 ptr = NULL;
2920 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
2922 /* The type of sizeof should be as large as a pointer, actually
2923 it should be size_t. */
2924 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2925 uintptr_t t = 1;
2926 uintptr_t t2;
2927 /* Effectively <<32, but defined also on 32bit machines. */
2928 t <<= 16;
2929 t <<= 16;
2930 t++;
2931 /* This checks that sizeof really can be used to manipulate
2932 uintptr_t objects, without truncation. */
2933 t2 = t & -sizeof(uintptr_t);
2934 printf ("%lu %lu\n", t, t2);
2936 /* some alignof tests */
2937 printf("__alignof__(int) = %d\n", __alignof__(int));
2938 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2939 printf("__alignof__(short) = %d\n", __alignof__(short));
2940 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2941 printf("__alignof__(char) = %d\n", __alignof__(char));
2942 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2943 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
2945 /* sizes of VLAs need to be evaluated even inside sizeof: */
2946 a = 2;
2947 printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a]));
2948 /* And checking if sizeof compound literal works. Parenthesized: */
2949 printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n",
2950 sizeof( (struct {int i; int j;}){4,5} ));
2951 /* And as direct sizeof argument (as unary expression): */
2952 printf("sizeof (struct {short i; short j;}){4,5} = %d\n",
2953 sizeof (struct {short i; short j;}){4,5} );
2955 /* sizeof(x && y) should be sizeof(int), even if constant
2956 evaluating is possible. */
2957 printf("sizeof(t && 0) = %d\n", sizeof(t && 0));
2958 printf("sizeof(1 && 1) = %d\n", sizeof(1 && 1));
2959 printf("sizeof(t || 1) = %d\n", sizeof(t || 1));
2960 printf("sizeof(0 || 0) = %d\n", sizeof(0 || 0));
2963 void typeof_test(void)
2965 double a;
2966 typeof(a) b;
2967 typeof(float) c;
2969 a = 1.5;
2970 b = 2.5;
2971 c = 3.5;
2972 printf("a=%f b=%f c=%f\n", a, b, c);
2976 struct hlist_node;
2977 struct hlist_head {
2978 struct hlist_node *first, *last;
2981 void consume_ulong (unsigned long i)
2983 i = 0;
2986 void statement_expr_test(void)
2988 int a, i;
2990 /* Basic stmt expr test */
2991 a = 0;
2992 for(i=0;i<10;i++) {
2993 a += 1 +
2994 ( { int b, j;
2995 b = 0;
2996 for(j=0;j<5;j++)
2997 b += j; b;
2998 } );
3000 printf("a=%d\n", a);
3002 /* Test that symbols aren't freed prematurely.
3003 With SYM_DEBUG valgrind will show a read from a freed
3004 symbol, and tcc will show an (invalid) warning on the initialization
3005 of 'ptr' below, if symbols are popped after the stmt expr. */
3006 void *v = (void*)39;
3007 typeof(({
3008 (struct hlist_node *)v;
3009 })) x;
3010 typeof (x)
3011 ptr = (struct hlist_node *)v;
3013 /* This part used to segfault when symbols were popped prematurely.
3014 The symbols for the static local would be overwritten with
3015 helper symbols from the pre-processor expansions in between. */
3016 #define some_attr __attribute__((aligned(1)))
3017 #define tps(str) ({ \
3018 static const char *t some_attr = str; \
3019 t; \
3021 printf ("stmtexpr: %s %s\n",
3022 tps("somerandomlongstring"),
3023 tps("anotherlongstring"));
3025 /* Test that the three decls of 't' don't interact. */
3026 int t = 40;
3027 int b = ({ int t = 41; t; });
3028 int c = ({ int t = 42; t; });
3030 /* Test that aggregate return values work. */
3031 struct hlist_head h
3032 = ({
3033 typedef struct hlist_head T;
3034 long pre = 48;
3035 T t = { (void*)43, (void*)44 };
3036 long post = 49;
3039 printf ("stmtexpr: %d %d %d\n", t, b, c);
3040 printf ("stmtexpr: %ld %ld\n", (long)h.first, (long)h.last);
3042 /* Test that we can give out addresses of local labels. */
3043 consume_ulong(({ __label__ __here; __here: (unsigned long)&&__here; }));
3046 void local_label_test(void)
3048 int a;
3049 goto l1;
3051 a = 1 + ({
3052 __label__ l1, l2, l3, l4;
3053 goto l1;
3055 printf("aa1\n");
3056 goto l3;
3058 printf("aa3\n");
3059 goto l4;
3061 printf("aa2\n");
3062 goto l2;
3063 l3:;
3066 printf("a=%d\n", a);
3067 return;
3069 printf("bb1\n");
3070 goto l2;
3072 printf("bb2\n");
3073 goto l4;
3076 /* inline assembler test */
3077 #if defined(__i386__) || defined(__x86_64__)
3079 /* from linux kernel */
3080 static char * strncat1(char * dest,const char * src,size_t count)
3082 long d0, d1, d2, d3;
3083 __asm__ __volatile__(
3084 "repne\n\t"
3085 "scasb\n\t"
3086 "dec %1\n\t"
3087 "mov %8,%3\n"
3088 "1:\tdec %3\n\t"
3089 "js 2f\n\t"
3090 "lodsb\n\t"
3091 "stosb\n\t"
3092 "testb %%al,%%al\n\t"
3093 "jne 1b\n"
3094 "2:\txor %2,%2\n\t"
3095 "stosb"
3096 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
3097 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
3098 : "memory");
3099 return dest;
3102 static char * strncat2(char * dest,const char * src,size_t count)
3104 long d0, d1, d2, d3;
3105 __asm__ __volatile__(
3106 "repne scasb\n\t" /* one-line repne prefix + string op */
3107 "dec %1\n\t"
3108 "mov %8,%3\n"
3109 "1:\tdec %3\n\t"
3110 "js 2f\n\t"
3111 "lodsb\n\t"
3112 "stosb\n\t"
3113 "testb %%al,%%al\n\t"
3114 "jne 1b\n"
3115 "2:\txor %2,%2\n\t"
3116 "stosb"
3117 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
3118 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
3119 : "memory");
3120 return dest;
3123 static inline void * memcpy1(void * to, const void * from, size_t n)
3125 long d0, d1, d2;
3126 __asm__ __volatile__(
3127 "rep ; movsl\n\t"
3128 "testb $2,%b4\n\t"
3129 "je 1f\n\t"
3130 "movsw\n"
3131 "1:\ttestb $1,%b4\n\t"
3132 "je 2f\n\t"
3133 "movsb\n"
3134 "2:"
3135 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
3136 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
3137 : "memory");
3138 return (to);
3141 static inline void * memcpy2(void * to, const void * from, size_t n)
3143 long d0, d1, d2;
3144 __asm__ __volatile__(
3145 "rep movsl\n\t" /* one-line rep prefix + string op */
3146 "testb $2,%b4\n\t"
3147 "je 1f\n\t"
3148 "movsw\n"
3149 "1:\ttestb $1,%b4\n\t"
3150 "je 2f\n\t"
3151 "movsb\n"
3152 "2:"
3153 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
3154 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
3155 : "memory");
3156 return (to);
3159 static __inline__ void sigaddset1(unsigned int *set, int _sig)
3161 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
3164 static __inline__ void sigdelset1(unsigned int *set, int _sig)
3166 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc", "flags");
3169 static __inline__ __const__ unsigned int swab32(unsigned int x)
3171 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
3172 "rorl $16,%0\n\t" /* swap words */
3173 "xchgb %b0,%h0" /* swap higher bytes */
3174 :"=" "q" (x)
3175 : "0" (x));
3176 return x;
3179 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
3181 unsigned long long res;
3182 #ifdef __x86_64__
3183 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
3184 but still test the 32bit->64bit mull. */
3185 unsigned int resh, resl;
3186 __asm__("mull %2" : "=a" (resl), "=d" (resh) : "a" (a), "r" (b));
3187 res = ((unsigned long long)resh << 32) | resl;
3188 #else
3189 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
3190 #endif
3191 return res;
3194 static __inline__ unsigned long long inc64(unsigned long long a)
3196 unsigned long long res;
3197 #ifdef __x86_64__
3198 /* Using the A constraint is wrong, and increments are tested
3199 elsewhere. */
3200 res = a + 1;
3201 #else
3202 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
3203 #endif
3204 return res;
3207 struct struct123 {
3208 int a;
3209 int b;
3211 struct struct1231 {
3212 unsigned long addr;
3215 unsigned long mconstraint_test(struct struct1231 *r)
3217 unsigned long ret;
3218 unsigned int a[2];
3219 a[0] = 0;
3220 __asm__ volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
3221 : "=&r" (ret), "=m" (a)
3222 : "m" (*(struct struct123 *)r->addr));
3223 return ret + a[0];
3226 #ifdef __x86_64__
3227 int fls64(unsigned long long x)
3229 int bitpos = -1;
3230 asm("bsrq %1,%q0"
3231 : "+r" (bitpos)
3232 : "rm" (x));
3233 return bitpos + 1;
3235 #endif
3237 void other_constraints_test(void)
3239 unsigned long ret;
3240 int var;
3241 #ifndef _WIN64
3242 __asm__ volatile ("mov %P1,%0" : "=r" (ret) : "p" (&var));
3243 printf ("oc1: %d\n", ret == (unsigned long)&var);
3244 #endif
3247 #ifndef _WIN32
3248 /* Test global asm blocks playing with aliases. */
3249 void base_func(void)
3251 printf ("asmc: base\n");
3254 extern void override_func1 (void);
3255 extern void override_func2 (void);
3257 asm(".weak override_func1\n.set override_func1, base_func");
3258 asm(".set override_func1, base_func");
3259 asm(".set override_func2, base_func");
3261 void override_func2 (void)
3263 printf ("asmc: override2\n");
3266 /* This checks a construct used by the linux kernel to encode
3267 references to strings by PC relative references. */
3268 extern int bug_table[] __attribute__((section("__bug_table")));
3269 char * get_asm_string (void)
3271 extern int some_symbol;
3272 asm volatile (".globl some_symbol\n"
3273 "jmp .+6\n"
3274 "1:\n"
3275 "some_symbol: .long 0\n"
3276 ".pushsection __bug_table, \"a\"\n"
3277 ".globl bug_table\n"
3278 "bug_table:\n"
3279 /* The first entry (1b-2b) is unused in this test,
3280 but we include it to check if cross-section
3281 PC-relative references work. */
3282 "2:\t.long 1b - 2b, %c0 - 2b\n"
3283 ".popsection\n" : : "i" ("A string"));
3284 char * str = ((char*)bug_table) + bug_table[1];
3285 return str;
3288 /* This checks another constructs with local labels. */
3289 extern unsigned char alld_stuff[];
3290 asm(".data\n"
3291 ".byte 41\n"
3292 "alld_stuff:\n"
3293 "661:\n"
3294 ".byte 42\n"
3295 "662:\n"
3296 ".pushsection .data.ignore\n"
3297 ".long 661b - .\n" /* This reference to 661 generates an external sym
3298 which shouldn't somehow overwrite the offset that's
3299 already determined for it. */
3300 ".popsection\n"
3301 ".byte 662b - 661b\n" /* So that this value is undeniably 1. */);
3303 void asm_local_label_diff (void)
3305 printf ("asm_local_label_diff: %d %d\n", alld_stuff[0], alld_stuff[1]);
3308 /* This checks that static local variables are available from assembler. */
3309 void asm_local_statics (void)
3311 static int localint = 41;
3312 asm("incl %0" : "+m" (localint));
3313 printf ("asm_local_statics: %d\n", localint);
3315 #endif
3317 static
3318 unsigned int set;
3320 void fancy_copy (unsigned *in, unsigned *out)
3322 asm volatile ("" : "=r" (*out) : "0" (*in));
3325 void fancy_copy2 (unsigned *in, unsigned *out)
3327 asm volatile ("mov %0,(%1)" : : "r" (*in), "r" (out) : "memory");
3330 #if defined __x86_64__ && !defined _WIN64
3331 void clobber_r12(void)
3333 asm volatile("mov $1, %%r12" ::: "r12");
3335 #endif
3337 void test_high_clobbers(void)
3339 #if defined __x86_64__ && !defined _WIN64
3340 register long val asm("r12");
3341 long val2;
3342 /* This tests if asm clobbers correctly save/restore callee saved
3343 registers if they are clobbered and if it's the high 8 x86-64
3344 registers. This is fragile for GCC as the constraints do not
3345 correctly capture the data flow, but good enough for us. */
3346 asm volatile("mov $0x4542, %%r12" : "=r" (val):: "memory");
3347 clobber_r12();
3348 asm volatile("mov %%r12, %0" : "=r" (val2) : "r" (val): "memory");
3349 printf("asmhc: 0x%x\n", val2);
3350 #endif
3353 static long cpu_number;
3354 void trace_console(long len, long len2)
3356 #ifdef __x86_64__
3357 /* This generated invalid code when the emission of the switch
3358 table isn't disabled. The asms are necessary to show the bug,
3359 normal statements don't work (they need to generate some code
3360 even under nocode_wanted, which normal statements don't do,
3361 but asms do). Also at least these number of cases is necessary
3362 to generate enough "random" bytes. They ultimately are enough
3363 to create invalid instruction patterns to which the first
3364 skip-to-decision-table jump jumps. If decision table emission
3365 is disabled all of this is no problem.
3367 It also is necessary that the switches are in a statement expression
3368 (which has the property of not being enterable from outside. no
3369 matter what). */
3370 if (0
3373 long pscr_ret__;
3374 switch(len) {
3375 case 4:
3377 long pfo_ret__;
3378 switch (len2) {
3379 case 8: printf("bla"); pfo_ret__ = 42; break;
3381 pscr_ret__ = pfo_ret__;
3383 break;
3384 case 8:
3386 long pfo_ret__;
3387 switch (len2) {
3388 case 1:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3389 case 2:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3390 case 4:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3391 case 8:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3392 default: printf("impossible\n");
3394 pscr_ret__ = pfo_ret__;
3396 break;
3398 pscr_ret__;
3401 printf("huh?\n");
3403 #endif
3406 void test_asm_dead_code(void)
3408 long rdi;
3409 /* Try to make sure that xdi contains a zero, and hence will
3410 lead to a segfault if the next asm is evaluated without
3411 arguments being set up. */
3412 asm volatile ("" : "=D" (rdi) : "0" (0));
3413 (void)sizeof (({
3414 int var;
3415 /* This shouldn't trigger a segfault, either the argument
3416 registers need to be set up and the asm emitted despite
3417 this being in an unevaluated context, or both the argument
3418 setup _and_ the asm emission need to be suppressed. The latter
3419 is better. Disabling asm code gen when suppression is on
3420 also fixes the above trace_console bug, but that came earlier
3421 than asm suppression. */
3422 asm volatile ("movl $0,(%0)" : : "D" (&var) : "memory");
3423 var;
3424 }));
3427 void test_asm_call(void)
3429 #if defined __x86_64__ && !defined _WIN64
3430 static char str[] = "PATH";
3431 char *s;
3432 /* This tests if a reference to an undefined symbol from an asm
3433 block, which isn't otherwise referenced in this file, is correctly
3434 regarded as global symbol, so that it's resolved by other object files
3435 or libraries. We chose getenv here, which isn't used anywhere else
3436 in this file. (If we used e.g. printf, which is used we already
3437 would have a global symbol entry, not triggering the bug which is
3438 tested here). */
3439 /* two pushes so stack remains aligned */
3440 asm volatile ("push %%rdi; push %%rdi; mov %0, %%rdi;"
3441 #if 1 && !defined(__TINYC__) && (defined(__PIC__) || defined(__PIE__))
3442 "call getenv@plt;"
3443 #else
3444 "call getenv;"
3445 #endif
3446 "pop %%rdi; pop %%rdi"
3447 : "=a" (s) : "r" (str));
3448 printf("asmd: %s\n", s);
3449 #endif
3452 #if defined __x86_64__
3453 # define RX "(%rip)"
3454 #else
3455 # define RX
3456 #endif
3458 void asm_dot_test(void)
3460 int x;
3461 for (x = 1;; ++x) {
3462 int r = x;
3463 switch (x) {
3464 case 1:
3465 asm(".text; lea S"RX",%eax; lea ."RX",%ecx; sub %ecx,%eax; S=.; jmp p0");
3466 case 2:
3467 asm(".text; jmp .+6; .int 123; mov .-4"RX",%eax; jmp p0");
3468 case 3:
3469 #ifndef _WIN32
3470 asm(".pushsection \".data\"; Y=.; .int 999; X=Y; .int 456; X=.-4; .popsection");
3471 #else
3472 asm(".data; Y=.; .int 999; X=Y; .int 456; X=.-4; .text");
3473 #endif
3474 asm(".text; mov X"RX",%eax; jmp p0");
3475 case 4:
3476 #ifndef _WIN32
3477 asm(".data; X=.; .int 789; Y=.; .int 999; .previous");
3478 #else
3479 asm(".data; X=.; .int 789; Y=.; .int 999; .text");
3480 #endif
3481 asm(".text; mov X"RX",%eax; X=Y; jmp p0");
3482 case 0:
3483 asm(".text; p0=.; mov %%eax,%0;" : "=m"(r)); break;
3485 if (r == x)
3486 break;
3487 printf("asm_dot_test %d: %d\n", x, r);
3491 void asm_test(void)
3493 char buf[128];
3494 unsigned int val, val2;
3495 struct struct123 s1;
3496 struct struct1231 s2 = { (unsigned long)&s1 };
3497 /* Hide the outer base_func, but check later that the inline
3498 asm block gets the outer one. */
3499 int base_func = 42;
3500 void override_func3 (void);
3501 unsigned long asmret;
3502 #ifdef BOOL_ISOC99
3503 _Bool somebool;
3504 #endif
3505 register int regvar asm("%esi");
3507 printf("inline asm:\n");
3509 // parse 0x1E-1 as 3 tokens in asm mode
3510 asm volatile ("mov $0x1E-1,%eax");
3512 /* test the no operand case */
3513 asm volatile ("xorl %eax, %eax");
3515 memcpy1(buf, "hello", 6);
3516 strncat1(buf, " worldXXXXX", 3);
3517 printf("%s\n", buf);
3519 memcpy2(buf, "hello", 6);
3520 strncat2(buf, " worldXXXXX", 3);
3521 printf("%s\n", buf);
3523 /* 'A' constraint test */
3524 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
3525 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
3527 s1.a = 42;
3528 s1.b = 43;
3529 printf("mconstraint: %d", mconstraint_test(&s2));
3530 printf(" %d %d\n", s1.a, s1.b);
3531 other_constraints_test();
3532 set = 0xff;
3533 sigdelset1(&set, 2);
3534 sigaddset1(&set, 16);
3535 /* NOTE: we test here if C labels are correctly restored after the
3536 asm statement */
3537 goto label1;
3538 label2:
3539 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
3540 printf("set=0x%x\n", set);
3541 val = 0x01020304;
3542 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
3543 #ifndef _WIN32
3544 override_func1();
3545 override_func2();
3546 /* The base_func ref from the following inline asm should find
3547 the global one, not the local decl from this function. */
3548 asm volatile(".weak override_func3\n.set override_func3, base_func");
3549 override_func3();
3550 printf("asmstr: %s\n", get_asm_string());
3551 asm_local_label_diff();
3552 asm_local_statics();
3553 #endif
3554 /* Check that we can also load structs of appropriate layout
3555 into registers. */
3556 asm volatile("" : "=r" (asmret) : "0"(s2));
3557 if (asmret != s2.addr)
3558 printf("asmstr: failed\n");
3559 #ifdef BOOL_ISOC99
3560 /* Check that the typesize correctly sets the register size to
3561 8 bit. */
3562 asm volatile("cmp %1,%2; sete %0" : "=a"(somebool) : "r"(1), "r"(2));
3563 if (!somebool)
3564 printf("asmbool: failed\n");
3565 #endif
3566 val = 43;
3567 fancy_copy (&val, &val2);
3568 printf ("fancycpy(%d)=%d\n", val, val2);
3569 val = 44;
3570 fancy_copy2 (&val, &val2);
3571 printf ("fancycpy2(%d)=%d\n", val, val2);
3572 asm volatile ("mov $0x4243, %%esi" : "=r" (regvar));
3573 printf ("regvar=%x\n", regvar);
3574 test_high_clobbers();
3575 trace_console(8, 8);
3576 test_asm_dead_code();
3577 test_asm_call();
3578 asm_dot_test();
3579 return;
3580 label1:
3581 goto label2;
3584 #else
3586 void asm_test(void)
3590 #endif
3592 #define COMPAT_TYPE(type1, type2) \
3594 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
3595 __builtin_types_compatible_p (type1, type2));\
3598 int constant_p_var;
3600 void builtin_test(void)
3602 short s;
3603 int i;
3604 long long ll;
3605 #if GCC_MAJOR >= 3
3606 COMPAT_TYPE(int, int);
3607 COMPAT_TYPE(int, unsigned int);
3608 COMPAT_TYPE(int, char);
3609 COMPAT_TYPE(int, const int);
3610 COMPAT_TYPE(int, volatile int);
3611 COMPAT_TYPE(int *, int *);
3612 COMPAT_TYPE(int *, void *);
3613 COMPAT_TYPE(int *, const int *);
3614 COMPAT_TYPE(char *, unsigned char *);
3615 COMPAT_TYPE(char *, signed char *);
3616 COMPAT_TYPE(char *, char *);
3617 /* space is needed because tcc preprocessor introduces a space between each token */
3618 COMPAT_TYPE(char * *, void *);
3619 #endif
3620 printf("res = %d\n", __builtin_constant_p(1));
3621 printf("res = %d\n", __builtin_constant_p(1 + 2));
3622 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
3623 printf("res = %d\n", __builtin_constant_p(constant_p_var));
3624 printf("res = %d\n", __builtin_constant_p(100000 / constant_p_var));
3625 s = 1;
3626 ll = 2;
3627 i = __builtin_choose_expr (1 != 0, ll, s);
3628 printf("bce: %d\n", i);
3629 i = __builtin_choose_expr (1 != 1, ll, s);
3630 printf("bce: %d\n", i);
3631 i = sizeof (__builtin_choose_expr (1, ll, s));
3632 printf("bce: %d\n", i);
3633 i = sizeof (__builtin_choose_expr (0, ll, s));
3634 printf("bce: %d\n", i);
3636 //printf("bera: %p\n", __builtin_extract_return_addr((void*)43));
3639 #ifndef _WIN32
3640 extern int __attribute__((weak)) weak_f1(void);
3641 extern int __attribute__((weak)) weak_f2(void);
3642 extern int weak_f3(void);
3643 extern int __attribute__((weak)) weak_v1;
3644 extern int __attribute__((weak)) weak_v2;
3645 extern int weak_v3;
3647 extern int (*weak_fpa)() __attribute__((weak));
3648 extern int __attribute__((weak)) (*weak_fpb)();
3649 extern __attribute__((weak)) int (*weak_fpc)();
3651 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
3652 extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
3653 extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
3654 extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
3655 extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
3656 extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
3658 static const size_t dummy = 0;
3659 extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
3660 extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
3661 extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
3663 int some_lib_func(void);
3664 int dummy_impl_of_slf(void) { return 444; }
3665 int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
3667 int weak_toolate() __attribute__((weak));
3668 int weak_toolate() { return 0; }
3670 void __attribute__((weak)) weak_test(void)
3672 printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
3673 printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
3674 printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
3675 printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
3676 printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
3677 printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
3679 printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
3680 printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
3681 printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
3683 printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
3684 printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
3685 printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
3686 printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
3687 printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
3688 printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
3689 printf("some_lib_func=%d\n", &some_lib_func ? some_lib_func() : 0);
3692 int __attribute__((weak)) weak_f2() { return 222; }
3693 int __attribute__((weak)) weak_f3() { return 333; }
3694 int __attribute__((weak)) weak_v2 = 222;
3695 int __attribute__((weak)) weak_v3 = 333;
3696 #endif
3698 void const_func(const int a)
3702 void const_warn_test(void)
3704 const_func(1);
3707 struct condstruct {
3708 int i;
3711 int getme (struct condstruct *s, int i)
3713 int i1 = (i == 0 ? 0 : s)->i;
3714 int i2 = (i == 0 ? s : 0)->i;
3715 int i3 = (i == 0 ? (void*)0 : s)->i;
3716 int i4 = (i == 0 ? s : (void*)0)->i;
3717 return i1 + i2 + i3 + i4;
3720 struct global_data
3722 int a[40];
3723 int *b[40];
3726 struct global_data global_data;
3728 int global_data_getstuff (int *, int);
3730 void global_data_callit (int i)
3732 *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
3735 int global_data_getstuff (int *p, int i)
3737 return *p + i;
3740 void global_data_test (void)
3742 global_data.a[0] = 42;
3743 global_data.b[0] = &global_data.a[0];
3744 global_data_callit (0);
3745 printf ("%d\n", global_data.a[0]);
3748 struct cmpcmpS
3750 unsigned char fill : 3;
3751 unsigned char b1 : 1;
3752 unsigned char b2 : 1;
3753 unsigned char fill2 : 3;
3756 int glob1, glob2, glob3;
3758 void compare_comparisons (struct cmpcmpS *s)
3760 if (s->b1 != (glob1 == glob2)
3761 || (s->b2 != (glob1 == glob3)))
3762 printf ("comparing comparisons broken\n");
3765 void cmp_comparison_test(void)
3767 struct cmpcmpS s;
3768 s.b1 = 1;
3769 glob1 = 42; glob2 = 42;
3770 s.b2 = 0;
3771 glob3 = 43;
3772 compare_comparisons (&s);
3775 int fcompare (double a, double b, int code)
3777 switch (code) {
3778 case 0: return a == b;
3779 case 1: return a != b;
3780 case 2: return a < b;
3781 case 3: return a >= b;
3782 case 4: return a > b;
3783 case 5: return a <= b;
3785 return 0;
3788 void math_cmp_test(void)
3790 double nan = 0.0/0.0;
3791 double one = 1.0;
3792 double two = 2.0;
3793 int comp = 0;
3794 int v;
3795 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
3797 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
3798 And it does this in various ways so that all code generation paths
3799 are checked (generating inverted tests, or non-inverted tests, or
3800 producing a 0/1 value without jumps (that's done in the fcompare
3801 function). */
3802 #define FCMP(a,b,op,iop,code) \
3803 if (fcompare (a,b,code)) \
3804 bug (a,b,op,iop,1); \
3805 if (a op b) \
3806 bug (a,b,op,iop,2); \
3807 if (a iop b) \
3809 else \
3810 bug (a,b,op,iop,3); \
3811 if ((a op b) || comp) \
3812 bug (a,b,op,iop,4); \
3813 if ((a iop b) || comp) \
3815 else \
3816 bug (a,b,op,iop,5); \
3817 if (v = !(a op b), !v) bug(a,b,op,iop,7);
3819 /* Equality tests. */
3820 FCMP(nan, nan, ==, !=, 0);
3821 FCMP(one, two, ==, !=, 0);
3822 FCMP(one, one, !=, ==, 1);
3823 /* Non-equality is a bit special. */
3824 if (!fcompare (nan, nan, 1))
3825 bug (nan, nan, !=, ==, 6);
3827 /* Relational tests on numbers. */
3828 FCMP(two, one, <, >=, 2);
3829 FCMP(one, two, >=, <, 3);
3830 FCMP(one, two, >, <=, 4);
3831 FCMP(two, one, <=, >, 5);
3833 /* Relational tests on NaNs. Note that the inverse op here is
3834 always !=, there's no operator in C that is equivalent to !(a < b),
3835 when NaNs are involved, same for the other relational ops. */
3836 FCMP(nan, nan, <, !=, 2);
3837 FCMP(nan, nan, >=, !=, 3);
3838 FCMP(nan, nan, >, !=, 4);
3839 FCMP(nan, nan, <=, !=, 5);
3842 double get100 () { return 100.0; }
3844 void callsave_test(void)
3846 #if defined __i386__ || defined __x86_64__ || defined __arm__
3847 int i, s; double *d; double t;
3848 s = sizeof (double);
3849 printf ("callsavetest: %d\n", s);
3850 d = alloca (sizeof(double));
3851 d[0] = 10.0;
3852 /* x86-64 had a bug were the next call to get100 would evict
3853 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
3854 in int type, not pointer type. When alloca returns a pointer
3855 with the high 32 bit set (which is likely on x86-64) the access
3856 generates a segfault. */
3857 i = d[0] > get100 ();
3858 printf ("%d\n", i);
3859 #endif
3863 void bfa3(ptrdiff_t str_offset)
3865 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
3867 void bfa2(ptrdiff_t str_offset)
3869 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
3870 bfa3(str_offset);
3872 void bfa1(ptrdiff_t str_offset)
3874 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
3875 bfa2(str_offset);
3878 void builtin_frame_address_test(void)
3880 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
3881 #ifndef __arm__
3882 char str[] = "__builtin_frame_address";
3883 char *fp0 = __builtin_frame_address(0);
3885 printf("str: %s\n", str);
3886 #ifndef __riscv
3887 bfa1(str-fp0);
3888 #endif
3889 #endif
3892 char via_volatile (char i)
3894 char volatile vi;
3895 vi = i;
3896 return vi;
3899 struct __attribute__((__packed__)) Spacked {
3900 char a;
3901 short b;
3902 int c;
3904 struct Spacked spacked;
3905 typedef struct __attribute__((__packed__)) {
3906 char a;
3907 short b;
3908 int c;
3909 } Spacked2;
3910 Spacked2 spacked2;
3911 typedef struct Spacked3_s {
3912 char a;
3913 short b;
3914 int c;
3915 } __attribute__((__packed__)) Spacked3;
3916 Spacked3 spacked3;
3917 struct gate_struct64 {
3918 unsigned short offset_low;
3919 unsigned short segment;
3920 unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1;
3921 unsigned short offset_middle;
3922 unsigned offset_high;
3923 unsigned zero1;
3924 } __attribute__((packed));
3925 typedef struct gate_struct64 gate_desc;
3926 gate_desc a_gate_desc;
3927 void attrib_test(void)
3929 #ifndef _WIN32
3930 printf("attr: %d %d %d %d\n", sizeof(struct Spacked),
3931 sizeof(spacked), sizeof(Spacked2), sizeof(spacked2));
3932 printf("attr: %d %d\n", sizeof(Spacked3), sizeof(spacked3));
3933 printf("attr: %d %d\n", sizeof(gate_desc), sizeof(a_gate_desc));
3934 #endif
3936 extern __attribute__((__unused__)) char * __attribute__((__unused__)) *
3937 strange_attrib_placement (void);
3939 void * __attribute__((__unused__)) get_void_ptr (void *a)
3941 return a;
3944 /* This part checks for a bug in TOK_GET (used for inline expansion),
3945 where the large long long constant left the the high bits set for
3946 the integer constant token. */
3947 static inline
3948 int __get_order(unsigned long long size)
3950 int order;
3951 size -= 0xffff880000000000ULL; // this const left high bits set in the token
3953 struct S { int i : 1; } s; // constructed for this '1'
3955 order = size;
3956 return order;
3959 /* This just forces the above inline function to be actually emitted. */
3960 int force_get_order(unsigned long s)
3962 return __get_order(s);