Fix sizeof(char[a])
[tinycc.git] / tests / tcctest.c
blob4e51ea1216f57a8693785779a0675ba28bdb2d8f
1 /*
2 * TCC auto test program
3 */
4 #include "config.h"
6 #if GCC_MAJOR >= 3
8 /* Unfortunately, gcc version < 3 does not handle that! */
9 #define ALL_ISOC99
11 /* only gcc 3 handles _Bool correctly */
12 #define BOOL_ISOC99
14 /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
15 #define CORRECT_CR_HANDLING
17 #endif
19 #if defined(_WIN32)
20 #define LONG_LONG_FORMAT "%lld"
21 #define ULONG_LONG_FORMAT "%llu"
22 #else
23 #define LONG_LONG_FORMAT "%Ld"
24 #define ULONG_LONG_FORMAT "%Lu"
25 #endif
27 // MinGW has 80-bit rather than 64-bit long double which isn't compatible with TCC or MSVC
28 #if defined(_WIN32) && defined(__GNUC__)
29 #define LONG_DOUBLE double
30 #define LONG_DOUBLE_LITERAL(x) x
31 #else
32 #define LONG_DOUBLE long double
33 #define LONG_DOUBLE_LITERAL(x) x ## L
34 #endif
36 /* deprecated and no longer supported in gcc 3.3 */
37 //#define ACCEPT_CR_IN_STRINGS
39 /* __VA_ARGS__ and __func__ support */
40 #define C99_MACROS
42 /* test various include syntaxes */
44 #define TCCLIB_INC <tcclib.h>
45 #define TCCLIB_INC1 <tcclib
46 #define TCCLIB_INC2 h>
47 #define TCCLIB_INC3 "tcclib.h"
49 #include TCCLIB_INC
51 #include TCCLIB_INC1.TCCLIB_INC2
53 #include TCCLIB_INC1.h>
55 #include TCCLIB_INC3
57 #include <tcclib.h>
59 #include "tcclib.h"
61 #include "tcctest.h"
63 /* Test two more ways to include a file named like a pp-number */
64 #define INC(name) <tests/name.h>
65 #define funnyname 42test.h
66 #define incdir tests/
67 #define incname < incdir funnyname >
68 #define __stringify(x) #x
69 #define stringify(x) __stringify(x)
70 #include INC(42test)
71 #include incname
72 #include stringify(funnyname)
74 void intdiv_test();
75 void string_test();
76 void expr_test();
77 void macro_test();
78 void recursive_macro_test();
79 void scope_test();
80 void forward_test();
81 void funcptr_test();
82 void loop_test();
83 void switch_test();
84 void goto_test();
85 void enum_test();
86 void typedef_test();
87 void struct_test();
88 void array_test();
89 void expr_ptr_test();
90 void bool_test();
91 void expr2_test();
92 void constant_expr_test();
93 void expr_cmp_test();
94 void char_short_test();
95 void init_test(void);
96 void compound_literal_test(void);
97 int kr_test();
98 void struct_assign_test(void);
99 void cast_test(void);
100 void bitfield_test(void);
101 void c99_bool_test(void);
102 void float_test(void);
103 void longlong_test(void);
104 void manyarg_test(void);
105 void stdarg_test(void);
106 void whitespace_test(void);
107 void relocation_test(void);
108 void old_style_function(void);
109 void alloca_test(void);
110 void c99_vla_test(int size1, int size2);
111 void sizeof_test(void);
112 void typeof_test(void);
113 void local_label_test(void);
114 void statement_expr_test(void);
115 void asm_test(void);
116 void builtin_test(void);
117 void weak_test(void);
118 void global_data_test(void);
119 void cmp_comparison_test(void);
120 void math_cmp_test(void);
121 void callsave_test(void);
122 void builtin_frame_address_test(void);
123 void attrib_test(void);
125 int fib(int n);
126 void num(int n);
127 void forward_ref(void);
128 int isid(int c);
130 /* Line joining happens before tokenization, so the following
131 must be parsed as ellipsis. */
132 void funny_line_continuation (int, ..\
133 . );
135 char via_volatile (char);
137 #define A 2
138 #define N 1234 + A
139 #define pf printf
140 #define M1(a, b) (a) + (b)
142 #define str\
143 (s) # s
144 #define glue(a, b) a ## b
145 #define xglue(a, b) glue(a, b)
146 #define HIGHLOW "hello"
147 #define LOW LOW ", world"
149 static int onetwothree = 123;
150 #define onetwothree4 onetwothree
151 #define onetwothree xglue(onetwothree,4)
153 #define min(a, b) ((a) < (b) ? (a) : (b))
155 #ifdef C99_MACROS
156 #define dprintf(level,...) printf(__VA_ARGS__)
157 #endif
159 /* gcc vararg macros */
160 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
162 #define MACRO_NOARGS()
164 #define AAA 3
165 #undef AAA
166 #define AAA 4
168 #if 1
169 #define B3 1
170 #elif 1
171 #define B3 2
172 #elif 0
173 #define B3 3
174 #else
175 #define B3 4
176 #endif
178 #define __INT64_C(c) c ## LL
179 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
181 int qq(int x)
183 return x + 40;
185 #define qq(x) x
187 #define spin_lock(lock) do { } while (0)
188 #define wq_spin_lock spin_lock
189 #define TEST2() wq_spin_lock(a)
191 #define UINT_MAX ((unsigned) -1)
193 void intdiv_test(void)
195 printf("18/21=%u\n", 18/21);
196 printf("18%%21=%u\n", 18%21);
197 printf("41/21=%u\n", 41/21);
198 printf("41%%21=%u\n", 41%21);
199 printf("42/21=%u\n", 42/21);
200 printf("42%%21=%u\n", 42%21);
201 printf("43/21=%u\n", 43/21);
202 printf("43%%21=%u\n", 43%21);
203 printf("126/21=%u\n", 126/21);
204 printf("126%%21=%u\n", 126%21);
205 printf("131/21=%u\n", 131/21);
206 printf("131%%21=%u\n", 131%21);
207 printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX/2+3)/2);
208 printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX/2+3)%2);
210 printf("18/-21=%u\n", 18/-21);
211 printf("18%%-21=%u\n", 18%-21);
212 printf("41/-21=%u\n", 41/-21);
213 printf("41%%-21=%u\n", 41%-21);
214 printf("42/-21=%u\n", 42/-21);
215 printf("42%%-21=%u\n", 42%-21);
216 printf("43/-21=%u\n", 43/-21);
217 printf("43%%-21=%u\n", 43%-21);
218 printf("126/-21=%u\n", 126/-21);
219 printf("126%%-21=%u\n", 126%-21);
220 printf("131/-21=%u\n", 131/-21);
221 printf("131%%-21=%u\n", 131%-21);
222 printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX/2+3)/-2);
223 printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX/2+3)%-2);
225 printf("-18/21=%u\n", -18/21);
226 printf("-18%%21=%u\n", -18%21);
227 printf("-41/21=%u\n", -41/21);
228 printf("-41%%21=%u\n", -41%21);
229 printf("-42/21=%u\n", -42/21);
230 printf("-42%%21=%u\n", -42%21);
231 printf("-43/21=%u\n", -43/21);
232 printf("-43%%21=%u\n", -43%21);
233 printf("-126/21=%u\n", -126/21);
234 printf("-126%%21=%u\n", -126%21);
235 printf("-131/21=%u\n", -131/21);
236 printf("-131%%21=%u\n", -131%21);
237 printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX/2+3))/2);
238 printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX/2+3))%2);
240 printf("-18/-21=%u\n", -18/-21);
241 printf("-18%%-21=%u\n", -18%-21);
242 printf("-41/-21=%u\n", -41/-21);
243 printf("-41%%-21=%u\n", -41%-21);
244 printf("-42/-21=%u\n", -42/-21);
245 printf("-42%%-21=%u\n", -42%-21);
246 printf("-43/-21=%u\n", -43/-21);
247 printf("-43%%-21=%u\n", -43%-21);
248 printf("-126/-21=%u\n", -126/-21);
249 printf("-126%%-21=%u\n", -126%-21);
250 printf("-131/-21=%u\n", -131/-21);
251 printf("-131%%-21=%u\n", -131%-21);
252 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX/2+3))/-2);
253 printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX/2+3))%-2);
256 void macro_test(void)
258 printf("macro:\n");\f\v
259 pf("N=%d\n", N);
260 printf("aaa=%d\n", AAA);
262 printf("min=%d\n", min(1, min(2, -1)));
264 printf("s1=%s\n", glue(HIGH, LOW));
265 printf("s2=%s\n", xglue(HIGH, LOW));
266 printf("s3=%s\n", str("c"));
267 printf("s4=%s\n", str(a1));
268 printf("B3=%d\n", B3);
270 printf("onetwothree=%d\n", onetwothree);
272 #ifdef A
273 printf("A defined\n");
274 #endif
275 #ifdef B
276 printf("B defined\n");
277 #endif
278 #ifdef A
279 printf("A defined\n");
280 #else
281 printf("A not defined\n");
282 #endif
283 #ifdef B
284 printf("B defined\n");
285 #else
286 printf("B not defined\n");
287 #endif
289 #ifdef A
290 printf("A defined\n");
291 #ifdef B
292 printf("B1 defined\n");
293 #else
294 printf("B1 not defined\n");
295 #endif
296 #else
297 printf("A not defined\n");
298 #ifdef B
299 printf("B2 defined\n");
300 #else
301 printf("B2 not defined\n");
302 #endif
303 #endif
305 #if 1+1
306 printf("test true1\n");
307 #endif
308 #if 0
309 printf("test true2\n");
310 #endif
311 #if 1-1
312 printf("test true3\n");
313 #endif
314 #if defined(A)
315 printf("test trueA\n");
316 #endif
317 #if defined(B)
318 printf("test trueB\n");
319 #endif
321 #if 0
322 printf("test 0\n");
323 #elif 0
324 printf("test 1\n");
325 #elif 2
326 printf("test 2\n");
327 #else
328 printf("test 3\n");
329 #endif
331 MACRO_NOARGS();
333 #ifdef __LINE__
334 printf("__LINE__ defined\n");
335 #endif
337 printf("__LINE__=%d __FILE__=%s\n",
338 __LINE__, __FILE__);
339 #if 0
340 #line 200
341 printf("__LINE__=%d __FILE__=%s\n",
342 __LINE__, __FILE__);
343 #line 203 "test"
344 printf("__LINE__=%d __FILE__=%s\n",
345 __LINE__, __FILE__);
346 #line 227 "tcctest.c"
347 #endif
349 /* not strictly preprocessor, but we test it there */
350 #ifdef C99_MACROS
351 printf("__func__ = %s\n", __func__);
352 dprintf(1, "vaarg=%d\n", 1);
353 #endif
354 dprintf1(1, "vaarg1\n");
355 dprintf1(1, "vaarg1=%d\n", 2);
356 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
358 /* gcc extension */
359 printf("func='%s'\n", __FUNCTION__);
361 /* complicated macros in glibc */
362 printf("INT64_MIN=" LONG_LONG_FORMAT "\n", INT64_MIN);
364 int a;
365 a = 1;
366 glue(a+, +);
367 printf("a=%d\n", a);
368 glue(a <, <= 2);
369 printf("a=%d\n", a);
372 /* macro function with argument outside the macro string */
373 #define MF_s MF_hello
374 #define MF_hello(msg) printf("%s\n",msg)
376 #define MF_t printf("tralala\n"); MF_hello
378 MF_s("hi");
379 MF_t("hi");
381 /* test macro substituion inside args (should not eat stream) */
382 printf("qq=%d\n", qq(qq)(2));
384 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
385 null argument without a space. gcc 3.2 fixes that. */
387 #define qq1(x) 1
388 printf("qq1=%d\n", qq1( ));
390 /* comment with stray handling *\
392 /* this is a valid *\/ comment */
393 /* this is a valid comment *\*/
394 // this is a valid\
395 comment
397 /* test function macro substitution when the function name is
398 substituted */
399 TEST2();
401 /* And again when the name and parenthes are separated by a
402 comment. */
403 TEST2 /* the comment */ ();
405 printf("%s\n", get_basefile_from_header());
406 printf("%s\n", __BASE_FILE__);
407 printf("%s\n", get_file_from_header());
408 printf("%s\n", __FILE__);
410 /* Check that funnily named include was in fact included */
411 have_included_42test_h = 1;
412 have_included_42test_h_second = 1;
413 have_included_42test_h_third = 1;
417 static void print_num(char *fn, int line, int num) {
418 printf("fn %s, line %d, num %d\n", fn, line, num);
421 void recursive_macro_test(void)
424 #define ELF32_ST_TYPE(val) ((val) & 0xf)
425 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
426 #define STB_WEAK 2 /* Weak symbol */
427 #define ELFW(type) ELF##32##_##type
428 printf("%d\n", ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(123)));
430 #define WRAP(x) x
432 #define print_num(x) print_num(__FILE__,__LINE__,x)
433 print_num(123);
434 WRAP(print_num(123));
435 WRAP(WRAP(print_num(123)));
437 static struct recursive_macro { int rm_field; } G;
438 #define rm_field (G.rm_field)
439 printf("rm_field = %d\n", rm_field);
440 printf("rm_field = %d\n", WRAP(rm_field));
441 WRAP((printf("rm_field = %d %d\n", rm_field, WRAP(rm_field))));
444 int op(a,b)
446 return a / b;
449 int ret(a)
451 if (a == 2)
452 return 1;
453 if (a == 3)
454 return 2;
455 return 0;
458 void ps(const char *s)
460 int c;
461 while (1) {
462 c = *s;
463 if (c == 0)
464 break;
465 printf("%c", c);
466 s++;
470 const char foo1_string[] = "\
471 bar\n\
472 test\14\
475 void string_test()
477 unsigned int b;
478 printf("string:\n");
479 printf("\141\1423\143\n");/* dezdez test */
480 printf("\x41\x42\x43\x3a\n");
481 printf("c=%c\n", 'r');
482 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
483 printf("foo1_string='%s'\n", foo1_string);
484 #if 0
485 printf("wstring=%S\n", L"abc");
486 printf("wstring=%S\n", L"abc" L"def" "ghi");
487 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
488 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
489 #endif
490 ps("test\n");
491 b = 32;
492 while ((b = b + 1) < 96) {
493 printf("%c", b);
495 printf("\n");
496 printf("fib=%d\n", fib(33));
497 b = 262144;
498 while (b != 0x80000000) {
499 num(b);
500 b = b * 2;
504 void loop_test()
506 int i;
507 i = 0;
508 while (i < 10)
509 printf("%d", i++);
510 printf("\n");
511 for(i = 0; i < 10;i++)
512 printf("%d", i);
513 printf("\n");
514 i = 0;
515 do {
516 printf("%d", i++);
517 } while (i < 10);
518 printf("\n");
520 char count = 123;
521 /* c99 for loop init test */
522 for (size_t count = 1; count < 3; count++)
523 printf("count=%d\n", count);
524 printf("count = %d\n", count);
526 /* break/continue tests */
527 i = 0;
528 while (1) {
529 if (i == 6)
530 break;
531 i++;
532 if (i == 3)
533 continue;
534 printf("%d", i);
536 printf("\n");
538 /* break/continue tests */
539 i = 0;
540 do {
541 if (i == 6)
542 break;
543 i++;
544 if (i == 3)
545 continue;
546 printf("%d", i);
547 } while(1);
548 printf("\n");
550 for(i = 0;i < 10;i++) {
551 if (i == 3)
552 continue;
553 printf("%d", i);
555 printf("\n");
558 typedef int typedef_and_label;
560 void goto_test()
562 int i;
563 static void *label_table[3] = { &&label1, &&label2, &&label3 };
565 printf("goto:\n");
566 i = 0;
567 /* This needs to parse as label, not as start of decl. */
568 typedef_and_label:
569 s_loop:
570 if (i >= 10)
571 goto s_end;
572 printf("%d", i);
573 i++;
574 goto s_loop;
575 s_end:
576 printf("\n");
578 /* we also test computed gotos (GCC extension) */
579 for(i=0;i<3;i++) {
580 goto *label_table[i];
581 label1:
582 printf("label1\n");
583 goto next;
584 label2:
585 printf("label2\n");
586 goto next;
587 label3:
588 printf("label3\n");
589 next: ;
593 enum {
595 E1 = 2,
596 E2 = 4,
601 enum test {
602 E5 = 1000,
605 struct S_enum {
606 enum {E6 = 42, E7, E8} e:8;
608 void enum_test()
610 enum test b1;
611 /* The following should give no warning */
612 unsigned *p = &b1;
613 struct S_enum s = {E7};
614 printf("enum: %d\n", s.e);
615 printf("enum:\n%d %d %d %d %d %d\n",
616 E0, E1, E2, E3, E4, E5);
617 b1 = 1;
618 printf("b1=%d\n", b1);
621 typedef int *my_ptr;
623 typedef int mytype1;
624 typedef int mytype2;
626 void typedef_test()
628 my_ptr a;
629 mytype1 mytype2;
630 int b;
632 a = &b;
633 *a = 1234;
634 printf("typedef:\n");
635 printf("a=%d\n", *a);
636 mytype2 = 2;
637 printf("mytype2=%d\n", mytype2);
640 void forward_test()
642 printf("forward:\n");
643 forward_ref();
644 forward_ref();
648 void forward_ref(void)
650 printf("forward ok\n");
653 typedef struct struct1 {
654 int f1;
655 int f2, f3;
656 union union1 {
657 int v1;
658 int v2;
659 } u;
660 char str[3];
661 } struct1;
663 struct struct2 {
664 int a;
665 char b;
668 union union2 {
669 int w1;
670 int w2;
673 struct struct1 st1, st2;
675 struct empty_mem {
676 /* nothing */ ;
677 int x;
680 int main(int argc, char **argv)
682 string_test();
683 expr_test();
684 macro_test();
685 recursive_macro_test();
686 scope_test();
687 forward_test();
688 funcptr_test();
689 loop_test();
690 switch_test();
691 goto_test();
692 enum_test();
693 typedef_test();
694 struct_test();
695 array_test();
696 expr_ptr_test();
697 bool_test();
698 expr2_test();
699 constant_expr_test();
700 expr_cmp_test();
701 char_short_test();
702 init_test();
703 compound_literal_test();
704 kr_test();
705 struct_assign_test();
706 cast_test();
707 bitfield_test();
708 c99_bool_test();
709 float_test();
710 longlong_test();
711 manyarg_test();
712 stdarg_test();
713 whitespace_test();
714 relocation_test();
715 old_style_function();
716 alloca_test();
717 c99_vla_test(5, 2);
718 sizeof_test();
719 typeof_test();
720 statement_expr_test();
721 local_label_test();
722 asm_test();
723 builtin_test();
724 #ifndef _WIN32
725 weak_test();
726 #endif
727 global_data_test();
728 cmp_comparison_test();
729 math_cmp_test();
730 callsave_test();
731 builtin_frame_address_test();
732 intdiv_test();
733 if (via_volatile (42) != 42)
734 printf ("via_volatile broken\n");
735 attrib_test();
736 return 0;
739 int tab[3];
740 int tab2[3][2];
742 int g;
744 void f1(g)
746 printf("g1=%d\n", g);
749 void scope_test()
751 printf("scope:\n");
752 g = 2;
753 f1(1);
754 printf("g2=%d\n", g);
756 int g;
757 g = 3;
758 printf("g3=%d\n", g);
760 int g;
761 g = 4;
762 printf("g4=%d\n", g);
765 printf("g5=%d\n", g);
768 void array_test()
770 int i, j, a[4];
772 printf("array:\n");
773 printf("sizeof(a) = %d\n", sizeof(a));
774 printf("sizeof(\"a\") = %d\n", sizeof("a"));
775 #ifdef C99_MACROS
776 printf("sizeof(__func__) = %d\n", sizeof(__func__));
777 #endif
778 printf("sizeof tab %d\n", sizeof(tab));
779 printf("sizeof tab2 %d\n", sizeof tab2);
780 tab[0] = 1;
781 tab[1] = 2;
782 tab[2] = 3;
783 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
784 for(i=0;i<3;i++)
785 for(j=0;j<2;j++)
786 tab2[i][j] = 10 * i + j;
787 for(i=0;i<3*2;i++) {
788 printf(" %3d", ((int *)tab2)[i]);
790 printf("\n");
791 printf("sizeof(size_t)=%d\n", sizeof(size_t));
792 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
795 void expr_test()
797 int a, b;
798 a = 0;
799 printf("%d\n", a += 1);
800 printf("%d\n", a -= 2);
801 printf("%d\n", a *= 31232132);
802 printf("%d\n", a /= 4);
803 printf("%d\n", a %= 20);
804 printf("%d\n", a &= 6);
805 printf("%d\n", a ^= 7);
806 printf("%d\n", a |= 8);
807 printf("%d\n", a >>= 3);
808 printf("%d\n", a <<= 4);
810 a = 22321;
811 b = -22321;
812 printf("%d\n", a + 1);
813 printf("%d\n", a - 2);
814 printf("%d\n", a * 312);
815 printf("%d\n", a / 4);
816 printf("%d\n", b / 4);
817 printf("%d\n", (unsigned)b / 4);
818 printf("%d\n", a % 20);
819 printf("%d\n", b % 20);
820 printf("%d\n", (unsigned)b % 20);
821 printf("%d\n", a & 6);
822 printf("%d\n", a ^ 7);
823 printf("%d\n", a | 8);
824 printf("%d\n", a >> 3);
825 printf("%d\n", b >> 3);
826 printf("%d\n", (unsigned)b >> 3);
827 printf("%d\n", a << 4);
828 printf("%d\n", ~a);
829 printf("%d\n", -a);
830 printf("%d\n", +a);
832 printf("%d\n", 12 + 1);
833 printf("%d\n", 12 - 2);
834 printf("%d\n", 12 * 312);
835 printf("%d\n", 12 / 4);
836 printf("%d\n", 12 % 20);
837 printf("%d\n", 12 & 6);
838 printf("%d\n", 12 ^ 7);
839 printf("%d\n", 12 | 8);
840 printf("%d\n", 12 >> 2);
841 printf("%d\n", 12 << 4);
842 printf("%d\n", ~12);
843 printf("%d\n", -12);
844 printf("%d\n", +12);
845 printf("%d %d %d %d\n",
846 isid('a'),
847 isid('g'),
848 isid('T'),
849 isid('('));
852 int isid(int c)
854 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
857 /**********************/
859 int vstack[10], *vstack_ptr;
861 void vpush(int vt, int vc)
863 *vstack_ptr++ = vt;
864 *vstack_ptr++ = vc;
867 void vpop(int *ft, int *fc)
869 *fc = *--vstack_ptr;
870 *ft = *--vstack_ptr;
873 void expr2_test()
875 int a, b;
877 printf("expr2:\n");
878 vstack_ptr = vstack;
879 vpush(1432432, 2);
880 vstack_ptr[-2] &= ~0xffffff80;
881 vpop(&a, &b);
882 printf("res= %d %d\n", a, b);
885 void constant_expr_test()
887 int a;
888 printf("constant_expr:\n");
889 a = 3;
890 printf("%d\n", a * 16);
891 printf("%d\n", a * 1);
892 printf("%d\n", a + 0);
895 int tab4[10];
897 void expr_ptr_test()
899 int *p, *q;
900 int i = -1;
902 printf("expr_ptr:\n");
903 p = tab4;
904 q = tab4 + 10;
905 printf("diff=%d\n", q - p);
906 p++;
907 printf("inc=%d\n", p - tab4);
908 p--;
909 printf("dec=%d\n", p - tab4);
910 ++p;
911 printf("inc=%d\n", p - tab4);
912 --p;
913 printf("dec=%d\n", p - tab4);
914 printf("add=%d\n", p + 3 - tab4);
915 printf("add=%d\n", 3 + p - tab4);
917 /* check if 64bit support is ok */
918 q = p = 0;
919 q += i;
920 printf("%p %p %ld\n", q, p, p-q);
921 printf("%d %d %d %d %d %d\n",
922 p == q, p != q, p < q, p <= q, p >= q, p > q);
923 i = 0xf0000000;
924 p += i;
925 printf("%p %p %ld\n", q, p, p-q);
926 printf("%d %d %d %d %d %d\n",
927 p == q, p != q, p < q, p <= q, p >= q, p > q);
928 p = (int *)((char *)p + 0xf0000000);
929 printf("%p %p %ld\n", q, p, p-q);
930 printf("%d %d %d %d %d %d\n",
931 p == q, p != q, p < q, p <= q, p >= q, p > q);
932 p += 0xf0000000;
933 printf("%p %p %ld\n", q, p, p-q);
934 printf("%d %d %d %d %d %d\n",
935 p == q, p != q, p < q, p <= q, p >= q, p > q);
937 struct size12 {
938 int i, j, k;
940 struct size12 s[2], *sp = s;
941 int i, j;
942 sp->i = 42;
943 sp++;
944 j = -1;
945 printf("%d\n", sp[j].i);
949 void expr_cmp_test()
951 int a, b;
952 printf("constant_expr:\n");
953 a = -1;
954 b = 1;
955 printf("%d\n", a == a);
956 printf("%d\n", a != a);
958 printf("%d\n", a < b);
959 printf("%d\n", a <= b);
960 printf("%d\n", a <= a);
961 printf("%d\n", b >= a);
962 printf("%d\n", a >= a);
963 printf("%d\n", b > a);
965 printf("%d\n", (unsigned)a < b);
966 printf("%d\n", (unsigned)a <= b);
967 printf("%d\n", (unsigned)a <= a);
968 printf("%d\n", (unsigned)b >= a);
969 printf("%d\n", (unsigned)a >= a);
970 printf("%d\n", (unsigned)b > a);
973 struct empty {
976 struct aligntest1 {
977 char a[10];
980 struct aligntest2 {
981 int a;
982 char b[10];
985 struct aligntest3 {
986 double a, b;
989 struct aligntest4 {
990 double a[0];
993 void struct_test()
995 struct1 *s;
996 union union2 u;
998 printf("struct:\n");
999 printf("sizes: %d %d %d %d\n",
1000 sizeof(struct struct1),
1001 sizeof(struct struct2),
1002 sizeof(union union1),
1003 sizeof(union union2));
1004 st1.f1 = 1;
1005 st1.f2 = 2;
1006 st1.f3 = 3;
1007 printf("st1: %d %d %d\n",
1008 st1.f1, st1.f2, st1.f3);
1009 st1.u.v1 = 1;
1010 st1.u.v2 = 2;
1011 printf("union1: %d\n", st1.u.v1);
1012 u.w1 = 1;
1013 u.w2 = 2;
1014 printf("union2: %d\n", u.w1);
1015 s = &st2;
1016 s->f1 = 3;
1017 s->f2 = 2;
1018 s->f3 = 1;
1019 printf("st2: %d %d %d\n",
1020 s->f1, s->f2, s->f3);
1021 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
1023 /* align / size tests */
1024 printf("aligntest1 sizeof=%d alignof=%d\n",
1025 sizeof(struct aligntest1), __alignof__(struct aligntest1));
1026 printf("aligntest2 sizeof=%d alignof=%d\n",
1027 sizeof(struct aligntest2), __alignof__(struct aligntest2));
1028 printf("aligntest3 sizeof=%d alignof=%d\n",
1029 sizeof(struct aligntest3), __alignof__(struct aligntest3));
1030 printf("aligntest4 sizeof=%d alignof=%d\n",
1031 sizeof(struct aligntest4), __alignof__(struct aligntest4));
1033 /* empty structures (GCC extension) */
1034 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
1035 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
1038 /* XXX: depend on endianness */
1039 void char_short_test()
1041 int var1, var2;
1043 printf("char_short:\n");
1045 var1 = 0x01020304;
1046 var2 = 0xfffefdfc;
1047 printf("s8=%d %d\n",
1048 *(char *)&var1, *(char *)&var2);
1049 printf("u8=%d %d\n",
1050 *(unsigned char *)&var1, *(unsigned char *)&var2);
1051 printf("s16=%d %d\n",
1052 *(short *)&var1, *(short *)&var2);
1053 printf("u16=%d %d\n",
1054 *(unsigned short *)&var1, *(unsigned short *)&var2);
1055 printf("s32=%d %d\n",
1056 *(int *)&var1, *(int *)&var2);
1057 printf("u32=%d %d\n",
1058 *(unsigned int *)&var1, *(unsigned int *)&var2);
1059 *(char *)&var1 = 0x08;
1060 printf("var1=%x\n", var1);
1061 *(short *)&var1 = 0x0809;
1062 printf("var1=%x\n", var1);
1063 *(int *)&var1 = 0x08090a0b;
1064 printf("var1=%x\n", var1);
1067 /******************/
1069 typedef struct Sym {
1070 int v;
1071 int t;
1072 int c;
1073 struct Sym *next;
1074 struct Sym *prev;
1075 } Sym;
1077 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1078 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1080 static int toupper1(int a)
1082 return TOUPPER(a);
1085 static unsigned int calc_vm_flags(unsigned int prot)
1087 unsigned int prot_bits;
1088 /* This used to segfault in some revisions: */
1089 prot_bits = ((0x1==0x00000001)?(prot&0x1):(prot&0x1)?0x00000001:0);
1090 return prot_bits;
1093 void bool_test()
1095 int *s, a, b, t, f, i;
1097 a = 0;
1098 s = (void*)0;
1099 printf("!s=%d\n", !s);
1101 if (!s || !s[0])
1102 a = 1;
1103 printf("a=%d\n", a);
1105 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1106 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1107 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1108 #if 1 && 1
1109 printf("a1\n");
1110 #endif
1111 #if 1 || 0
1112 printf("a2\n");
1113 #endif
1114 #if 1 ? 0 : 1
1115 printf("a3\n");
1116 #endif
1117 #if 0 ? 0 : 1
1118 printf("a4\n");
1119 #endif
1121 a = 4;
1122 printf("b=%d\n", a + (0 ? 1 : a / 2));
1124 /* test register spilling */
1125 a = 10;
1126 b = 10;
1127 a = (a + b) * ((a < b) ?
1128 ((b - a) * (a - b)): a + b);
1129 printf("a=%d\n", a);
1131 /* test complex || or && expressions */
1132 t = 1;
1133 f = 0;
1134 a = 32;
1135 printf("exp=%d\n", f == (32 <= a && a <= 3));
1136 printf("r=%d\n", (t || f) + (t && f));
1138 /* test ? : cast */
1140 int aspect_on;
1141 int aspect_native = 65536;
1142 double bfu_aspect = 1.0;
1143 int aspect;
1144 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
1145 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
1146 printf("aspect=%d\n", aspect);
1150 /* test ? : GCC extension */
1152 static int v1 = 34 ? : -1; /* constant case */
1153 static int v2 = 0 ? : -1; /* constant case */
1154 int a = 30;
1156 printf("%d %d\n", v1, v2);
1157 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
1160 /* again complex expression */
1161 for(i=0;i<256;i++) {
1162 if (toupper1 (i) != TOUPPER (i))
1163 printf("error %d\n", i);
1165 printf ("bits = 0x%x\n", calc_vm_flags (0x1));
1168 /* GCC accepts that */
1169 static int tab_reinit[];
1170 static int tab_reinit[10];
1172 //int cinit1; /* a global variable can be defined several times without error ! */
1173 int cinit1;
1174 int cinit1;
1175 int cinit1 = 0;
1176 int *cinit2 = (int []){3, 2, 1};
1178 void compound_literal_test(void)
1180 int *p, i;
1181 char *q, *q3;
1183 printf("compound_test:\n");
1185 p = (int []){1, 2, 3};
1186 for(i=0;i<3;i++)
1187 printf(" %d", p[i]);
1188 printf("\n");
1190 for(i=0;i<3;i++)
1191 printf("%d", cinit2[i]);
1192 printf("\n");
1194 q = "tralala1";
1195 printf("q1=%s\n", q);
1197 q = (char *){ "tralala2" };
1198 printf("q2=%s\n", q);
1200 q3 = (char *){ q };
1201 printf("q3=%s\n", q3);
1203 q = (char []){ "tralala3" };
1204 printf("q4=%s\n", q);
1206 #ifdef ALL_ISOC99
1207 p = (int []){1, 2, cinit1 + 3};
1208 for(i=0;i<3;i++)
1209 printf(" %d", p[i]);
1210 printf("\n");
1212 for(i=0;i<3;i++) {
1213 p = (int []){1, 2, 4 + i};
1214 printf("%d %d %d\n",
1215 p[0],
1216 p[1],
1217 p[2]);
1219 #endif
1222 /* K & R protos */
1224 kr_func1(a, b)
1226 return a + b;
1229 int kr_func2(a, b)
1231 return a + b;
1234 kr_test()
1236 printf("kr_test:\n");
1237 printf("func1=%d\n", kr_func1(3, 4));
1238 printf("func2=%d\n", kr_func2(3, 4));
1239 return 0;
1242 void num(int n)
1244 char *tab, *p;
1245 tab = (char*)malloc(20);
1246 p = tab;
1247 while (1) {
1248 *p = 48 + (n % 10);
1249 p++;
1250 n = n / 10;
1251 if (n == 0)
1252 break;
1254 while (p != tab) {
1255 p--;
1256 printf("%c", *p);
1258 printf("\n");
1259 free(tab);
1262 /* structure assignment tests */
1263 struct structa1 {
1264 int f1;
1265 char f2;
1268 struct structa1 ssta1;
1270 void struct_assign_test1(struct structa1 s1, int t, float f)
1272 printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
1275 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1277 s1.f1 += t;
1278 s1.f2 -= t;
1279 return s1;
1282 void struct_assign_test(void)
1284 struct S {
1285 struct structa1 lsta1, lsta2;
1286 int i;
1287 } s, *ps;
1289 ps = &s;
1290 ps->i = 4;
1291 #if 0
1292 printf("struct_assign_test:\n");
1294 s.lsta1.f1 = 1;
1295 s.lsta1.f2 = 2;
1296 printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
1297 s.lsta2 = s.lsta1;
1298 printf("%d %d\n", s.lsta2.f1, s.lsta2.f2);
1299 #else
1300 s.lsta2.f1 = 1;
1301 s.lsta2.f2 = 2;
1302 #endif
1303 struct_assign_test1(ps->lsta2, 3, 4.5);
1305 printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
1306 ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
1307 printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
1309 static struct {
1310 void (*elem)();
1311 } t[] = {
1312 /* XXX: we should allow this even without braces */
1313 { struct_assign_test }
1315 printf("%d\n", struct_assign_test == t[0].elem);
1318 /* casts to short/char */
1320 void cast1(char a, short b, unsigned char c, unsigned short d)
1322 printf("%d %d %d %d\n", a, b, c, d);
1325 char bcast;
1326 short scast;
1328 void cast_test()
1330 int a;
1331 char c;
1332 char tab[10];
1333 unsigned b,d;
1334 short s;
1335 char *p = NULL;
1336 p -= 0x700000000042;
1338 printf("cast_test:\n");
1339 a = 0xfffff;
1340 cast1(a, a, a, a);
1341 a = 0xffffe;
1342 printf("%d %d %d %d\n",
1343 (char)(a + 1),
1344 (short)(a + 1),
1345 (unsigned char)(a + 1),
1346 (unsigned short)(a + 1));
1347 printf("%d %d %d %d\n",
1348 (char)0xfffff,
1349 (short)0xfffff,
1350 (unsigned char)0xfffff,
1351 (unsigned short)0xfffff);
1353 a = (bcast = 128) + 1;
1354 printf("%d\n", a);
1355 a = (scast = 65536) + 1;
1356 printf("%d\n", a);
1358 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1360 /* test cast from unsigned to signed short to int */
1361 b = 0xf000;
1362 d = (short)b;
1363 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1364 b = 0xf0f0;
1365 d = (char)b;
1366 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1368 /* test implicit int casting for array accesses */
1369 c = 0;
1370 tab[1] = 2;
1371 tab[c] = 1;
1372 printf("%d %d\n", tab[0], tab[1]);
1374 /* test implicit casting on some operators */
1375 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1376 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1377 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1379 /* from pointer to integer types */
1380 printf("%d %d %ld %ld %lld %lld\n",
1381 (int)p, (unsigned int)p,
1382 (long)p, (unsigned long)p,
1383 (long long)p, (unsigned long long)p);
1385 /* from integers to pointers */
1386 printf("%p %p %p %p\n",
1387 (void *)a, (void *)b, (void *)c, (void *)d);
1390 /* initializers tests */
1391 struct structinit1 {
1392 int f1;
1393 char f2;
1394 short f3;
1395 int farray[3];
1398 int sinit1 = 2;
1399 int sinit2 = { 3 };
1400 int sinit3[3] = { 1, 2, {{3}}, };
1401 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1402 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1403 int sinit6[] = { 1, 2, 3 };
1404 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1405 char sinit8[] = "hello" "trala";
1407 struct structinit1 sinit9 = { 1, 2, 3 };
1408 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1409 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1410 #ifdef ALL_ISOC99
1411 .farray[0] = 10,
1412 .farray[1] = 11,
1413 .farray[2] = 12,
1414 #endif
1417 char *sinit12 = "hello world";
1418 char *sinit13[] = {
1419 "test1",
1420 "test2",
1421 "test3",
1423 char sinit14[10] = { "abc" };
1424 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1426 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1428 struct bar {
1429 char *s;
1430 int len;
1431 } sinit17[] = {
1432 "a1", 4,
1433 "a2", 1
1436 int sinit18[10] = {
1437 [2 ... 5] = 20,
1439 [8] = 10,
1442 struct complexinit0 {
1443 int a;
1444 int b;
1447 struct complexinit {
1448 int a;
1449 const struct complexinit0 *b;
1452 const static struct complexinit cix[] = {
1453 [0] = {
1454 .a = 2000,
1455 .b = (const struct complexinit0[]) {
1456 { 2001, 2002 },
1457 { 2003, 2003 },
1463 struct complexinit2 {
1464 int a;
1465 int b[];
1468 struct complexinit2 cix20;
1470 struct complexinit2 cix21 = {
1471 .a = 3000,
1472 .b = { 3001, 3002, 3003 }
1475 struct complexinit2 cix22 = {
1476 .a = 4000,
1477 .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1480 typedef int arrtype1[];
1481 arrtype1 sinit19 = {1};
1482 arrtype1 sinit20 = {2,3};
1483 typedef int arrtype2[3];
1484 arrtype2 sinit21 = {4};
1485 arrtype2 sinit22 = {5,6,7};
1487 /* Address comparisons of non-weak symbols with zero can be const-folded */
1488 int sinit23[2] = { "astring" ? sizeof("astring") : -1,
1489 &sinit23 ? 42 : -1 };
1491 void init_test(void)
1493 int linit1 = 2;
1494 int linit2 = { 3 };
1495 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1496 int linit6[] = { 1, 2, 3 };
1497 int i, j;
1498 char linit8[] = "hello" "trala";
1499 int linit12[10] = { 1, 2 };
1500 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1501 char linit14[10] = "abc";
1502 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1503 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1504 int linit17 = sizeof(linit17);
1505 int zero = 0;
1506 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1507 int linit18[2] = {&zero ? 1 : -1, zero ? -1 : 1 };
1509 printf("init_test:\n");
1511 printf("sinit1=%d\n", sinit1);
1512 printf("sinit2=%d\n", sinit2);
1513 printf("sinit3=%d %d %d %d\n",
1514 sizeof(sinit3),
1515 sinit3[0],
1516 sinit3[1],
1517 sinit3[2]
1519 printf("sinit6=%d\n", sizeof(sinit6));
1520 printf("sinit7=%d %d %d %d\n",
1521 sizeof(sinit7),
1522 sinit7[0],
1523 sinit7[1],
1524 sinit7[2]
1526 printf("sinit8=%s\n", sinit8);
1527 printf("sinit9=%d %d %d\n",
1528 sinit9.f1,
1529 sinit9.f2,
1530 sinit9.f3
1532 printf("sinit10=%d %d %d\n",
1533 sinit10.f1,
1534 sinit10.f2,
1535 sinit10.f3
1537 printf("sinit11=%d %d %d %d %d %d\n",
1538 sinit11.f1,
1539 sinit11.f2,
1540 sinit11.f3,
1541 sinit11.farray[0],
1542 sinit11.farray[1],
1543 sinit11.farray[2]
1546 for(i=0;i<3;i++)
1547 for(j=0;j<2;j++)
1548 printf("[%d][%d] = %d %d %d\n",
1549 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1550 printf("linit1=%d\n", linit1);
1551 printf("linit2=%d\n", linit2);
1552 printf("linit6=%d\n", sizeof(linit6));
1553 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1555 printf("sinit12=%s\n", sinit12);
1556 printf("sinit13=%d %s %s %s\n",
1557 sizeof(sinit13),
1558 sinit13[0],
1559 sinit13[1],
1560 sinit13[2]);
1561 printf("sinit14=%s\n", sinit14);
1563 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1564 printf("\n");
1565 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1566 printf("\n");
1567 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1568 printf("\n");
1569 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1570 printf("\n");
1571 printf("%d %d %d %d\n",
1572 linit16.a1,
1573 linit16.a2,
1574 linit16.a3,
1575 linit16.a4);
1576 /* test that initialisation is done after variable declare */
1577 printf("linit17=%d\n", linit17);
1578 printf("sinit15=%d\n", sinit15[0]);
1579 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1580 printf("sinit17=%s %d %s %d\n",
1581 sinit17[0].s, sinit17[0].len,
1582 sinit17[1].s, sinit17[1].len);
1583 for(i=0;i<10;i++)
1584 printf("%x ", sinit18[i]);
1585 printf("\n");
1586 /* complex init check */
1587 printf("cix: %d %d %d %d %d %d %d\n",
1588 cix[0].a,
1589 cix[0].b[0].a, cix[0].b[0].b,
1590 cix[0].b[1].a, cix[0].b[1].b,
1591 cix[0].b[2].a, cix[0].b[2].b);
1592 printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
1593 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
1595 printf("arrtype1: %d %d %d\n", sinit19[0], sinit20[0], sinit20[1]);
1596 printf("arrtype2: %d %d\n", sizeof(sinit19), sizeof(sinit20));
1597 printf("arrtype3: %d %d %d\n", sinit21[0], sinit21[1], sinit21[2]);
1598 printf("arrtype4: %d %d %d\n", sinit22[0], sinit22[1], sinit22[2]);
1599 printf("arrtype5: %d %d\n", sizeof(sinit21), sizeof(sinit22));
1600 printf("arrtype6: %d\n", sizeof(arrtype2));
1602 printf("sinit23= %d %d\n", sinit23[0], sinit23[1]);
1603 printf("linit18= %d %d\n", linit18[0], linit18[1]);
1607 void switch_test()
1609 int i;
1611 for(i=0;i<15;i++) {
1612 switch(i) {
1613 case 0:
1614 case 1:
1615 printf("a");
1616 break;
1617 default:
1618 printf("%d", i);
1619 break;
1620 case 8 ... 12:
1621 printf("c");
1622 break;
1623 case 3:
1624 printf("b");
1625 break;
1626 case 0xc33c6b9fU:
1627 case 0x7c9eeeb9U:
1628 break;
1631 printf("\n");
1634 /* ISOC99 _Bool type */
1635 void c99_bool_test(void)
1637 #ifdef BOOL_ISOC99
1638 int a;
1639 _Bool b;
1641 printf("bool_test:\n");
1642 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1643 a = 3;
1644 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1645 b = 3;
1646 printf("b = %d\n", b);
1647 b++;
1648 printf("b = %d\n", b);
1649 #endif
1652 void bitfield_test(void)
1654 int a;
1655 short sa;
1656 unsigned char ca;
1657 struct sbf1 {
1658 int f1 : 3;
1659 int : 2;
1660 int f2 : 1;
1661 int : 0;
1662 int f3 : 5;
1663 int f4 : 7;
1664 unsigned int f5 : 7;
1665 } st1;
1666 printf("bitfield_test:");
1667 printf("sizeof(st1) = %d\n", sizeof(st1));
1669 st1.f1 = 3;
1670 st1.f2 = 1;
1671 st1.f3 = 15;
1672 a = 120;
1673 st1.f4 = a;
1674 st1.f5 = a;
1675 st1.f5++;
1676 printf("%d %d %d %d %d\n",
1677 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1678 sa = st1.f5;
1679 ca = st1.f5;
1680 printf("%d %d\n", sa, ca);
1682 st1.f1 = 7;
1683 if (st1.f1 == -1)
1684 printf("st1.f1 == -1\n");
1685 else
1686 printf("st1.f1 != -1\n");
1687 if (st1.f2 == -1)
1688 printf("st1.f2 == -1\n");
1689 else
1690 printf("st1.f2 != -1\n");
1692 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1693 long-long bitfields whose size is not bigger than int */
1694 struct sbf2 {
1695 long long f1 : 45;
1696 long long : 2;
1697 long long f2 : 35;
1698 unsigned long long f3 : 38;
1699 } st2;
1700 st2.f1 = 0x123456789ULL;
1701 a = 120;
1702 st2.f2 = (long long)a << 25;
1703 st2.f3 = a;
1704 st2.f2++;
1705 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
1708 #ifdef __x86_64__
1709 #define FLOAT_FMT "%f\n"
1710 #else
1711 /* x86's float isn't compatible with GCC */
1712 #define FLOAT_FMT "%.5f\n"
1713 #endif
1715 /* declare strto* functions as they are C99 */
1716 double strtod(const char *nptr, char **endptr);
1718 #if defined(_WIN32)
1719 float strtof(const char *nptr, char **endptr) {return (float)strtod(nptr, endptr);}
1720 LONG_DOUBLE strtold(const char *nptr, char **endptr) {return (LONG_DOUBLE)strtod(nptr, endptr);}
1721 #else
1722 float strtof(const char *nptr, char **endptr);
1723 LONG_DOUBLE strtold(const char *nptr, char **endptr);
1724 #endif
1726 #define FTEST(prefix, typename, type, fmt)\
1727 void prefix ## cmp(type a, type b)\
1729 printf("%d %d %d %d %d %d\n",\
1730 a == b,\
1731 a != b,\
1732 a < b,\
1733 a > b,\
1734 a >= b,\
1735 a <= b);\
1736 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1739 a + b,\
1740 a - b,\
1741 a * b,\
1742 a / b,\
1743 -a);\
1744 printf(fmt "\n", ++a);\
1745 printf(fmt "\n", a++);\
1746 printf(fmt "\n", a);\
1747 b = 0;\
1748 printf("%d %d\n", !a, !b);\
1750 void prefix ## fcast(type a)\
1752 float fa;\
1753 double da;\
1754 LONG_DOUBLE la;\
1755 int ia;\
1756 long long llia;\
1757 unsigned int ua;\
1758 unsigned long long llua;\
1759 type b;\
1760 fa = a;\
1761 da = a;\
1762 la = a;\
1763 printf("ftof: %f %f %Lf\n", fa, da, la);\
1764 ia = (int)a;\
1765 llia = (long long)a;\
1766 a = (a >= 0) ? a : -a;\
1767 ua = (unsigned int)a;\
1768 llua = (unsigned long long)a;\
1769 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
1770 ia = -1234;\
1771 ua = 0x81234500;\
1772 llia = -0x123456789012345LL;\
1773 llua = 0xf123456789012345LLU;\
1774 b = ia;\
1775 printf("itof: " fmt "\n", b);\
1776 b = ua;\
1777 printf("utof: " fmt "\n", b);\
1778 b = llia;\
1779 printf("lltof: " fmt "\n", b);\
1780 b = llua;\
1781 printf("ulltof: " fmt "\n", b);\
1784 float prefix ## retf(type a) { return a; }\
1785 double prefix ## retd(type a) { return a; }\
1786 LONG_DOUBLE prefix ## retld(type a) { return a; }\
1788 void prefix ## call(void)\
1790 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1791 printf("double: %f\n", prefix ## retd(42.123456789));\
1792 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1793 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
1796 void prefix ## signed_zeros(void) \
1798 type x = 0.0, y = -0.0, n, p;\
1799 if (x == y)\
1800 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
1801 1.0 / x != 1.0 / y);\
1802 else\
1803 printf ("x != y; this is wrong!\n");\
1805 n = -x;\
1806 if (x == n)\
1807 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
1808 1.0 / x != 1.0 / n);\
1809 else\
1810 printf ("x != -x; this is wrong!\n");\
1812 p = +y;\
1813 if (x == p)\
1814 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
1815 1.0 / x != 1.0 / p);\
1816 else\
1817 printf ("x != +y; this is wrong!\n");\
1818 p = -y;\
1819 if (x == p)\
1820 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
1821 1.0 / x != 1.0 / p);\
1822 else\
1823 printf ("x != -y; this is wrong!\n");\
1825 void prefix ## test(void)\
1827 printf("testing '%s'\n", #typename);\
1828 prefix ## cmp(1, 2.5);\
1829 prefix ## cmp(2, 1.5);\
1830 prefix ## cmp(1, 1);\
1831 prefix ## fcast(234.6);\
1832 prefix ## fcast(-2334.6);\
1833 prefix ## call();\
1834 prefix ## signed_zeros();\
1837 FTEST(f, float, float, "%f")
1838 FTEST(d, double, double, "%f")
1839 FTEST(ld, long double, LONG_DOUBLE, "%Lf")
1841 double ftab1[3] = { 1.2, 3.4, -5.6 };
1844 void float_test(void)
1846 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
1847 float fa, fb;
1848 double da, db;
1849 int a;
1850 unsigned int b;
1852 printf("float_test:\n");
1853 printf("sizeof(float) = %d\n", sizeof(float));
1854 printf("sizeof(double) = %d\n", sizeof(double));
1855 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));
1856 ftest();
1857 dtest();
1858 ldtest();
1859 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1860 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1861 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1862 da = 123;
1863 printf("da=%f\n", da);
1864 fa = 123;
1865 printf("fa=%f\n", fa);
1866 a = 4000000000;
1867 da = a;
1868 printf("da = %f\n", da);
1869 b = 4000000000;
1870 db = b;
1871 printf("db = %f\n", db);
1872 #endif
1875 int fib(int n)
1877 if (n <= 2)
1878 return 1;
1879 else
1880 return fib(n-1) + fib(n-2);
1883 void funcptr_test()
1885 void (*func)(int);
1886 int a;
1887 struct {
1888 int dummy;
1889 void (*func)(int);
1890 } st1;
1891 long diff;
1893 printf("funcptr:\n");
1894 func = &num;
1895 (*func)(12345);
1896 func = num;
1897 a = 1;
1898 a = 1;
1899 func(12345);
1900 /* more complicated pointer computation */
1901 st1.func = num;
1902 st1.func(12346);
1903 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1904 printf("sizeof2 = %d\n", sizeof funcptr_test);
1905 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1906 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1907 a = 0;
1908 func = num + a;
1909 diff = func - num;
1910 func(42);
1911 (func + diff)(42);
1912 (num + a)(43);
1915 void lloptest(long long a, long long b)
1917 unsigned long long ua, ub;
1919 ua = a;
1920 ub = b;
1921 /* arith */
1922 printf("arith: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1923 a + b,
1924 a - b,
1925 a * b);
1927 if (b != 0) {
1928 printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1929 a / b,
1930 a % b);
1933 /* binary */
1934 printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1935 a & b,
1936 a | b,
1937 a ^ b);
1939 /* tests */
1940 printf("test: %d %d %d %d %d %d\n",
1941 a == b,
1942 a != b,
1943 a < b,
1944 a > b,
1945 a >= b,
1946 a <= b);
1948 printf("utest: %d %d %d %d %d %d\n",
1949 ua == ub,
1950 ua != ub,
1951 ua < ub,
1952 ua > ub,
1953 ua >= ub,
1954 ua <= ub);
1956 /* arith2 */
1957 a++;
1958 b++;
1959 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
1960 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a++, b++);
1961 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", --a, --b);
1962 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
1963 b = ub = 0;
1964 printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
1967 void llshift(long long a, int b)
1969 printf("shift: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1970 (unsigned long long)a >> b,
1971 a >> b,
1972 a << b);
1973 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1974 (unsigned long long)a >> 3,
1975 a >> 3,
1976 a << 3);
1977 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1978 (unsigned long long)a >> 35,
1979 a >> 35,
1980 a << 35);
1983 void llfloat(void)
1985 float fa;
1986 double da;
1987 LONG_DOUBLE lda;
1988 long long la, lb, lc;
1989 unsigned long long ula, ulb, ulc;
1990 la = 0x12345678;
1991 ula = 0x72345678;
1992 la = (la << 20) | 0x12345;
1993 ula = ula << 33;
1994 printf("la=" LONG_LONG_FORMAT " ula=" ULONG_LONG_FORMAT "\n", la, ula);
1996 fa = la;
1997 da = la;
1998 lda = la;
1999 printf("lltof: %f %f %Lf\n", fa, da, lda);
2001 la = fa;
2002 lb = da;
2003 lc = lda;
2004 printf("ftoll: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", la, lb, lc);
2006 fa = ula;
2007 da = ula;
2008 lda = ula;
2009 printf("ulltof: %f %f %Lf\n", fa, da, lda);
2011 ula = fa;
2012 ulb = da;
2013 ulc = lda;
2014 printf("ftoull: " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT "\n", ula, ulb, ulc);
2017 long long llfunc1(int a)
2019 return a * 2;
2022 struct S {
2023 int id;
2024 char item;
2027 long long int value(struct S *v)
2029 return ((long long int)v->item);
2032 long long llfunc2(long long x, long long y, int z)
2034 return x * y * z;
2037 void longlong_test(void)
2039 long long a, b, c;
2040 int ia;
2041 unsigned int ua;
2042 printf("longlong_test:\n");
2043 printf("sizeof(long long) = %d\n", sizeof(long long));
2044 ia = -1;
2045 ua = -2;
2046 a = ia;
2047 b = ua;
2048 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2049 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %Lx\n",
2050 (long long)1,
2051 (long long)-2,
2052 1LL,
2053 0x1234567812345679);
2054 a = llfunc1(-3);
2055 printf(LONG_LONG_FORMAT "\n", a);
2057 lloptest(1000, 23);
2058 lloptest(0xff, 0x1234);
2059 b = 0x72345678 << 10;
2060 lloptest(-3, b);
2061 llshift(0x123, 5);
2062 llshift(-23, 5);
2063 b = 0x72345678LL << 10;
2064 llshift(b, 47);
2066 llfloat();
2067 #if 1
2068 b = 0x12345678;
2069 a = -1;
2070 c = a + b;
2071 printf("%Lx\n", c);
2072 #endif
2074 /* long long reg spill test */
2076 struct S a;
2078 a.item = 3;
2079 printf("%lld\n", value(&a));
2081 lloptest(0x80000000, 0);
2084 long long *p, v, **pp;
2085 v = 1;
2086 p = &v;
2087 p[0]++;
2088 printf("another long long spill test : %lld\n", *p);
2089 pp = &p;
2091 v = llfunc2(**pp, **pp, ia);
2092 printf("a long long function (arm-)reg-args test : %lld\n", v);
2094 a = 68719476720LL;
2095 b = 4294967295LL;
2096 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
2098 printf(LONG_LONG_FORMAT "\n", 0x123456789LLU);
2100 /* long long pointer deref in argument passing test */
2101 a = 0x123;
2102 long long *p = &a;
2103 llshift(*p, 5);
2106 void manyarg_test(void)
2108 LONG_DOUBLE ld = 1234567891234LL;
2109 printf("manyarg_test:\n");
2110 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2111 1, 2, 3, 4, 5, 6, 7, 8,
2112 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2113 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2114 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2115 1, 2, 3, 4, 5, 6, 7, 8,
2116 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2117 1234567891234LL, 987654321986LL,
2118 42.0, 43.0);
2119 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2120 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2121 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2122 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2123 1234567891234LL, 987654321986LL,
2124 42.0, 43.0);
2125 printf("%d %d %d %d %d %d %d %d %Lf\n",
2126 1, 2, 3, 4, 5, 6, 7, 8, ld);
2127 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2128 LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n",
2129 1, 2, 3, 4, 5, 6, 7, 8,
2130 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2131 1234567891234LL, 987654321986LL,
2132 42.0, 43.0, ld);
2133 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2134 "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n",
2135 1, 2, 3, 4, 5, 6, 7, 8,
2136 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2137 ld, 1234567891234LL, 987654321986LL,
2138 42.0, 43.0, ld);
2141 void vprintf1(const char *fmt, ...)
2143 va_list ap, aq;
2144 const char *p;
2145 int c, i;
2146 double d;
2147 long long ll;
2148 LONG_DOUBLE ld;
2150 va_start(aq, fmt);
2151 va_copy(ap, aq);
2153 p = fmt;
2154 for(;;) {
2155 c = *p;
2156 if (c == '\0')
2157 break;
2158 p++;
2159 if (c == '%') {
2160 c = *p;
2161 switch(c) {
2162 case '\0':
2163 goto the_end;
2164 case 'd':
2165 i = va_arg(ap, int);
2166 printf("%d", i);
2167 break;
2168 case 'f':
2169 d = va_arg(ap, double);
2170 printf("%f", d);
2171 break;
2172 case 'l':
2173 ll = va_arg(ap, long long);
2174 printf(LONG_LONG_FORMAT, ll);
2175 break;
2176 case 'F':
2177 ld = va_arg(ap, LONG_DOUBLE);
2178 printf("%Lf", ld);
2179 break;
2181 p++;
2182 } else {
2183 putchar(c);
2186 the_end:
2187 va_end(aq);
2188 va_end(ap);
2191 struct myspace {
2192 short int profile;
2195 void stdarg_for_struct(struct myspace bob, ...)
2197 struct myspace george, bill;
2198 va_list ap;
2199 short int validate;
2201 va_start(ap, bob);
2202 bill = va_arg(ap, struct myspace);
2203 george = va_arg(ap, struct myspace);
2204 validate = va_arg(ap, int);
2205 printf("stdarg_for_struct: %d %d %d %d\n",
2206 bob.profile, bill.profile, george.profile, validate);
2207 va_end(ap);
2210 void stdarg_for_libc(const char *fmt, ...)
2212 va_list args;
2213 va_start(args, fmt);
2214 vprintf(fmt, args);
2215 va_end(args);
2218 void stdarg_test(void)
2220 LONG_DOUBLE ld = 1234567891234LL;
2221 struct myspace bob;
2223 vprintf1("%d %d %d\n", 1, 2, 3);
2224 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2225 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2226 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2227 vprintf1("%d %f %l %F %d %f %l %F\n",
2228 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2229 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2230 1, 2, 3, 4, 5, 6, 7, 8,
2231 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2232 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2233 1, 2, 3, 4, 5, 6, 7, 8,
2234 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2235 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2236 "%l %l %f %f\n",
2237 1, 2, 3, 4, 5, 6, 7, 8,
2238 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2239 1234567891234LL, 987654321986LL,
2240 42.0, 43.0);
2241 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2242 "%l %l %f %f\n",
2243 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2244 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2245 1234567891234LL, 987654321986LL,
2246 42.0, 43.0);
2247 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2248 1, 2, 3, 4, 5, 6, 7, 8, ld);
2249 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2250 "%l %l %f %f %F\n",
2251 1, 2, 3, 4, 5, 6, 7, 8,
2252 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2253 1234567891234LL, 987654321986LL,
2254 42.0, 43.0, ld);
2255 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2256 "%F %l %l %f %f %F\n",
2257 1, 2, 3, 4, 5, 6, 7, 8,
2258 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2259 ld, 1234567891234LL, 987654321986LL,
2260 42.0, 43.0, ld);
2262 bob.profile = 42;
2263 stdarg_for_struct(bob, bob, bob, bob.profile);
2264 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2267 void whitespace_test(void)
2269 char *str;
2271 \f\v #if 1
2272 pri\
2273 ntf("whitspace:\n");\f\v
2274 #endif
2275 pf("N=%d\n", 2);
2277 #ifdef CORRECT_CR_HANDLING
2278 pri\
2279 ntf("aaa=%d\n", 3);
2280 #endif
2282 pri\
2284 ntf("min=%d\n", 4);
2286 #ifdef ACCEPT_CR_IN_STRINGS
2287 printf("len1=%d\n", strlen("
2288 "));
2289 #ifdef CORRECT_CR_HANDLING
2290 str = "
2292 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
2293 #endif
2294 printf("len1=%d\n", strlen(" a
2295 "));
2296 #endif /* ACCEPT_CR_IN_STRINGS */
2299 int reltab[3] = { 1, 2, 3 };
2301 int *rel1 = &reltab[1];
2302 int *rel2 = &reltab[2];
2304 void getmyaddress(void)
2306 printf("in getmyaddress\n");
2308 unsigned long theaddress = (unsigned long)getmyaddress;
2309 void relocation_test(void)
2311 void (*fptr)(void) = (void (*)(void))theaddress;
2312 printf("*rel1=%d\n", *rel1);
2313 printf("*rel2=%d\n", *rel2);
2314 fptr();
2317 void old_style_f(a,b,c)
2318 int a, b;
2319 double c;
2321 printf("a=%d b=%d b=%f\n", a, b, c);
2324 void decl_func1(int cmpfn())
2326 printf("cmpfn=%lx\n", (long)cmpfn);
2329 void decl_func2(cmpfn)
2330 int cmpfn();
2332 printf("cmpfn=%lx\n", (long)cmpfn);
2335 void old_style_function(void)
2337 old_style_f((void *)1, 2, 3.0);
2338 decl_func1(NULL);
2339 decl_func2(NULL);
2342 void alloca_test()
2344 #if defined __i386__ || defined __x86_64__ || defined __arm__
2345 char *p = alloca(16);
2346 strcpy(p,"123456789012345");
2347 printf("alloca: p is %s\n", p);
2348 char *demo = "This is only a test.\n";
2349 /* Test alloca embedded in a larger expression */
2350 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
2351 #endif
2354 void *bounds_checking_is_enabled()
2356 char ca[10], *cp = ca-1;
2357 return (ca != cp + 1) ? cp : NULL;
2360 typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
2362 void c99_vla_test(int size1, int size2)
2364 #if defined __i386__ || defined __x86_64__
2365 int size = size1 * size2;
2366 int tab1[size][2], tab2[10][2];
2367 void *tab1_ptr, *tab2_ptr, *bad_ptr;
2369 /* "size" should have been 'captured' at tab1 declaration,
2370 so modifying it should have no effect on VLA behaviour. */
2371 size = size-1;
2373 printf("Test C99 VLA 1 (sizeof): ");
2374 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
2375 tab1_ptr = tab1;
2376 tab2_ptr = tab2;
2377 printf("Test C99 VLA 2 (ptrs subtract): ");
2378 printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2379 printf("Test C99 VLA 3 (ptr add): ");
2380 printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2381 printf("Test C99 VLA 4 (ptr access): ");
2382 tab1[size1][1] = 42;
2383 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2385 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2386 if (bad_ptr = bounds_checking_is_enabled()) {
2387 int *t1 = &tab1[size1 * size2 - 1][3];
2388 int *t2 = &tab2[9][3];
2389 printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
2390 printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
2392 char*c1 = 1 + sizeof(tab1) + (char*)tab1;
2393 char*c2 = 1 + sizeof(tab2) + (char*)tab2;
2394 printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
2395 printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
2397 int *i1 = tab1[-1];
2398 int *i2 = tab2[-1];
2399 printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
2400 printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
2402 int *x1 = tab1[size1 * size2 + 1];
2403 int *x2 = tab2[10 + 1];
2404 printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
2405 printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
2406 } else {
2407 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2409 printf("\n");
2410 #endif
2413 #ifndef __TINYC__
2414 typedef __SIZE_TYPE__ uintptr_t;
2415 #endif
2417 void sizeof_test(void)
2419 int a;
2420 int **ptr;
2422 printf("sizeof(int) = %d\n", sizeof(int));
2423 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2424 printf("sizeof(long) = %d\n", sizeof(long));
2425 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2426 printf("sizeof(short) = %d\n", sizeof(short));
2427 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2428 printf("sizeof(char) = %d\n", sizeof(char));
2429 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2430 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2431 a = 1;
2432 printf("sizeof(a++) = %d\n", sizeof a++);
2433 printf("a=%d\n", a);
2434 ptr = NULL;
2435 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
2437 /* The type of sizeof should be as large as a pointer, actually
2438 it should be size_t. */
2439 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2440 uintptr_t t = 1;
2441 uintptr_t t2;
2442 /* Effectively <<32, but defined also on 32bit machines. */
2443 t <<= 16;
2444 t <<= 16;
2445 t++;
2446 /* This checks that sizeof really can be used to manipulate
2447 uintptr_t objects, without truncation. */
2448 t2 = t & -sizeof(uintptr_t);
2449 printf ("%lu %lu\n", t, t2);
2451 /* some alignof tests */
2452 printf("__alignof__(int) = %d\n", __alignof__(int));
2453 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2454 printf("__alignof__(short) = %d\n", __alignof__(short));
2455 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2456 printf("__alignof__(char) = %d\n", __alignof__(char));
2457 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2458 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
2460 /* sizes of VLAs need to be evaluated even inside sizeof: */
2461 a = 2;
2462 printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a]));
2463 /* And checking if sizeof compound literal works. Parenthesized: */
2464 printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n",
2465 sizeof( (struct {int i; int j;}){4,5} ));
2466 /* And as direct sizeof argument (as unary expression): */
2467 printf("sizeof (struct {short i; short j;}){4,5} = %d\n",
2468 sizeof (struct {short i; short j;}){4,5} );
2471 void typeof_test(void)
2473 double a;
2474 typeof(a) b;
2475 typeof(float) c;
2477 a = 1.5;
2478 b = 2.5;
2479 c = 3.5;
2480 printf("a=%f b=%f c=%f\n", a, b, c);
2484 struct hlist_node;
2485 struct hlist_head {
2486 struct hlist_node *first, *last;
2489 void statement_expr_test(void)
2491 int a, i;
2493 /* Basic stmt expr test */
2494 a = 0;
2495 for(i=0;i<10;i++) {
2496 a += 1 +
2497 ( { int b, j;
2498 b = 0;
2499 for(j=0;j<5;j++)
2500 b += j; b;
2501 } );
2503 printf("a=%d\n", a);
2505 /* Test that symbols aren't freed prematurely.
2506 With SYM_DEBUG valgrind will show a read from a freed
2507 symbol, and tcc will show an (invalid) warning on the initialization
2508 of 'ptr' below, if symbols are popped after the stmt expr. */
2509 void *v = (void*)39;
2510 typeof(({
2511 (struct hlist_node *)v;
2512 })) x;
2513 typeof (x)
2514 ptr = (struct hlist_node *)v;
2516 /* This part used to segfault when symbols were popped prematurely.
2517 The symbols for the static local would be overwritten with
2518 helper symbols from the pre-processor expansions in between. */
2519 #define some_attr __attribute__((aligned(1)))
2520 #define tps(str) ({ \
2521 static const char *t some_attr = str; \
2522 t; \
2524 printf ("stmtexpr: %s %s\n",
2525 tps("somerandomlongstring"),
2526 tps("anotherlongstring"));
2528 /* Test that the three decls of 't' don't interact. */
2529 int t = 40;
2530 int b = ({ int t = 41; t; });
2531 int c = ({ int t = 42; t; });
2533 /* Test that aggregate return values work. */
2534 struct hlist_head h
2535 = ({
2536 typedef struct hlist_head T;
2537 long pre = 48;
2538 T t = { (void*)43, (void*)44 };
2539 long post = 49;
2542 printf ("stmtexpr: %d %d %d\n", t, b, c);
2543 printf ("stmtexpr: %ld %ld\n", (long)h.first, (long)h.last);
2546 void local_label_test(void)
2548 int a;
2549 goto l1;
2551 a = 1 + ({
2552 __label__ l1, l2, l3, l4;
2553 goto l1;
2555 printf("aa1\n");
2556 goto l3;
2558 printf("aa3\n");
2559 goto l4;
2561 printf("aa2\n");
2562 goto l2;
2563 l3:;
2566 printf("a=%d\n", a);
2567 return;
2569 printf("bb1\n");
2570 goto l2;
2572 printf("bb2\n");
2573 goto l4;
2576 /* inline assembler test */
2577 #if defined(__i386__) || defined(__x86_64__)
2579 /* from linux kernel */
2580 static char * strncat1(char * dest,const char * src,size_t count)
2582 long d0, d1, d2, d3;
2583 __asm__ __volatile__(
2584 "repne\n\t"
2585 "scasb\n\t"
2586 "dec %1\n\t"
2587 "mov %8,%3\n"
2588 "1:\tdec %3\n\t"
2589 "js 2f\n\t"
2590 "lodsb\n\t"
2591 "stosb\n\t"
2592 "testb %%al,%%al\n\t"
2593 "jne 1b\n"
2594 "2:\txor %2,%2\n\t"
2595 "stosb"
2596 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2597 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2598 : "memory");
2599 return dest;
2602 static char * strncat2(char * dest,const char * src,size_t count)
2604 long d0, d1, d2, d3;
2605 __asm__ __volatile__(
2606 "repne scasb\n\t" /* one-line repne prefix + string op */
2607 "dec %1\n\t"
2608 "mov %8,%3\n"
2609 "1:\tdec %3\n\t"
2610 "js 2f\n\t"
2611 "lodsb\n\t"
2612 "stosb\n\t"
2613 "testb %%al,%%al\n\t"
2614 "jne 1b\n"
2615 "2:\txor %2,%2\n\t"
2616 "stosb"
2617 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2618 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2619 : "memory");
2620 return dest;
2623 static inline void * memcpy1(void * to, const void * from, size_t n)
2625 long d0, d1, d2;
2626 __asm__ __volatile__(
2627 "rep ; movsl\n\t"
2628 "testb $2,%b4\n\t"
2629 "je 1f\n\t"
2630 "movsw\n"
2631 "1:\ttestb $1,%b4\n\t"
2632 "je 2f\n\t"
2633 "movsb\n"
2634 "2:"
2635 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2636 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2637 : "memory");
2638 return (to);
2641 static inline void * memcpy2(void * to, const void * from, size_t n)
2643 long d0, d1, d2;
2644 __asm__ __volatile__(
2645 "rep movsl\n\t" /* one-line rep prefix + string op */
2646 "testb $2,%b4\n\t"
2647 "je 1f\n\t"
2648 "movsw\n"
2649 "1:\ttestb $1,%b4\n\t"
2650 "je 2f\n\t"
2651 "movsb\n"
2652 "2:"
2653 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2654 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2655 : "memory");
2656 return (to);
2659 static __inline__ void sigaddset1(unsigned int *set, int _sig)
2661 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2664 static __inline__ void sigdelset1(unsigned int *set, int _sig)
2666 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc", "flags");
2669 static __inline__ __const__ unsigned int swab32(unsigned int x)
2671 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2672 "rorl $16,%0\n\t" /* swap words */
2673 "xchgb %b0,%h0" /* swap higher bytes */
2674 :"=" "q" (x)
2675 : "0" (x));
2676 return x;
2679 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
2681 unsigned long long res;
2682 #ifdef __x86_64__
2683 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
2684 but still test the 32bit->64bit mull. */
2685 unsigned int resh, resl;
2686 __asm__("mull %2" : "=a" (resl), "=d" (resh) : "a" (a), "r" (b));
2687 res = ((unsigned long long)resh << 32) | resl;
2688 #else
2689 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
2690 #endif
2691 return res;
2694 static __inline__ unsigned long long inc64(unsigned long long a)
2696 unsigned long long res;
2697 #ifdef __x86_64__
2698 /* Using the A constraint is wrong, and increments are tested
2699 elsewere. */
2700 res = a + 1;
2701 #else
2702 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
2703 #endif
2704 return res;
2707 struct struct123 {
2708 int a;
2709 int b;
2711 struct struct1231 {
2712 unsigned long addr;
2715 unsigned long mconstraint_test(struct struct1231 *r)
2717 unsigned long ret;
2718 unsigned int a[2];
2719 a[0] = 0;
2720 __asm__ volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
2721 : "=&r" (ret), "=m" (a)
2722 : "m" (*(struct struct123 *)r->addr));
2723 return ret + a[0];
2726 #ifdef __x86_64__
2727 int fls64(unsigned long long x)
2729 int bitpos = -1;
2730 asm("bsrq %1,%q0"
2731 : "+r" (bitpos)
2732 : "rm" (x));
2733 return bitpos + 1;
2735 #endif
2737 void other_constraints_test(void)
2739 unsigned long ret;
2740 int var;
2741 __asm__ volatile ("movq %P1,%0" : "=r" (ret) : "p" (&var));
2742 printf ("oc1: %d\n", ret == (unsigned long)&var);
2745 /* Test global asm blocks playing with aliases. */
2746 void base_func(void)
2748 printf ("asmc: base\n");
2751 extern void override_func1 (void);
2752 extern void override_func2 (void);
2754 asm(".weak override_func1\n.set override_func1, base_func");
2755 asm(".set override_func1, base_func");
2756 asm(".set override_func2, base_func");
2758 void override_func2 (void)
2760 printf ("asmc: override2\n");
2763 /* This checks a construct used by the linux kernel to encode
2764 references to strings by PC relative references. */
2765 extern int bug_table[] __attribute__((section("__bug_table")));
2766 char * get_asm_string (void)
2768 extern int some_symbol;
2769 asm volatile (".globl some_symbol\n"
2770 "jmp .+6\n"
2771 "1:\n"
2772 "some_symbol: .long 0\n"
2773 ".pushsection __bug_table, \"a\"\n"
2774 ".globl bug_table\n"
2775 "bug_table:\n"
2776 /* The first entry (1b-2b) is unused in this test,
2777 but we include it to check if cross-section
2778 PC-relative references work. */
2779 "2:\t.long 1b - 2b, %c0 - 2b\n"
2780 ".popsection\n" : : "i" ("A string"));
2781 char * str = ((char*)bug_table) + bug_table[1];
2782 return str;
2785 unsigned int set;
2787 void asm_test(void)
2789 char buf[128];
2790 unsigned int val;
2791 struct struct123 s1;
2792 struct struct1231 s2 = { (unsigned long)&s1 };
2793 /* Hide the outer base_func, but check later that the inline
2794 asm block gets the outer one. */
2795 int base_func = 42;
2796 void override_func3 (void);
2797 unsigned long asmret;
2798 #ifdef BOOL_ISOC99
2799 _Bool somebool;
2800 #endif
2802 printf("inline asm:\n");
2804 // parse 0x1E-1 as 3 tokens in asm mode
2805 asm volatile ("mov $0x1E-1,%eax");
2807 /* test the no operand case */
2808 asm volatile ("xorl %eax, %eax");
2810 memcpy1(buf, "hello", 6);
2811 strncat1(buf, " worldXXXXX", 3);
2812 printf("%s\n", buf);
2814 memcpy2(buf, "hello", 6);
2815 strncat2(buf, " worldXXXXX", 3);
2816 printf("%s\n", buf);
2818 /* 'A' constraint test */
2819 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2820 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2822 s1.a = 42;
2823 s1.b = 43;
2824 printf("mconstraint: %d", mconstraint_test(&s2));
2825 printf(" %d %d\n", s1.a, s1.b);
2826 other_constraints_test();
2827 set = 0xff;
2828 sigdelset1(&set, 2);
2829 sigaddset1(&set, 16);
2830 /* NOTE: we test here if C labels are correctly restored after the
2831 asm statement */
2832 goto label1;
2833 label2:
2834 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
2835 printf("set=0x%x\n", set);
2836 val = 0x01020304;
2837 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
2838 override_func1();
2839 override_func2();
2840 /* The base_func ref from the following inline asm should find
2841 the global one, not the local decl from this function. */
2842 asm volatile(".weak override_func3\n.set override_func3, base_func");
2843 override_func3();
2844 /* Check that we can also load structs of appropriate layout
2845 into registers. */
2846 asm volatile("" : "=r" (asmret) : "0"(s2));
2847 if (asmret != s2.addr)
2848 printf("asmstr: failed\n");
2849 #ifdef BOOL_ISOC99
2850 /* Check that the typesize correctly sets the register size to
2851 8 bit. */
2852 asm volatile("cmp %1,%2; sete %0" : "=a"(somebool) : "r"(1), "r"(2));
2853 if (!somebool)
2854 printf("asmbool: failed\n");
2855 #endif
2856 printf("asmstr: %s\n", get_asm_string());
2857 return;
2858 label1:
2859 goto label2;
2862 #else
2864 void asm_test(void)
2868 #endif
2870 #define COMPAT_TYPE(type1, type2) \
2872 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2873 __builtin_types_compatible_p (type1, type2));\
2876 int constant_p_var;
2878 void builtin_test(void)
2880 short s;
2881 int i;
2882 long long ll;
2883 #if GCC_MAJOR >= 3
2884 COMPAT_TYPE(int, int);
2885 COMPAT_TYPE(int, unsigned int);
2886 COMPAT_TYPE(int, char);
2887 COMPAT_TYPE(int, const int);
2888 COMPAT_TYPE(int, volatile int);
2889 COMPAT_TYPE(int *, int *);
2890 COMPAT_TYPE(int *, void *);
2891 COMPAT_TYPE(int *, const int *);
2892 COMPAT_TYPE(char *, unsigned char *);
2893 COMPAT_TYPE(char *, signed char *);
2894 COMPAT_TYPE(char *, char *);
2895 /* space is needed because tcc preprocessor introduces a space between each token */
2896 COMPAT_TYPE(char * *, void *);
2897 #endif
2898 printf("res = %d\n", __builtin_constant_p(1));
2899 printf("res = %d\n", __builtin_constant_p(1 + 2));
2900 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
2901 printf("res = %d\n", __builtin_constant_p(constant_p_var));
2902 printf("res = %d\n", __builtin_constant_p(100000 / constant_p_var));
2903 s = 1;
2904 ll = 2;
2905 i = __builtin_choose_expr (1 != 0, ll, s);
2906 printf("bce: %d\n", i);
2907 i = __builtin_choose_expr (1 != 1, ll, s);
2908 printf("bce: %d\n", i);
2909 i = sizeof (__builtin_choose_expr (1, ll, s));
2910 printf("bce: %d\n", i);
2911 i = sizeof (__builtin_choose_expr (0, ll, s));
2912 printf("bce: %d\n", i);
2915 #ifndef _WIN32
2916 extern int __attribute__((weak)) weak_f1(void);
2917 extern int __attribute__((weak)) weak_f2(void);
2918 extern int weak_f3(void);
2919 extern int __attribute__((weak)) weak_v1;
2920 extern int __attribute__((weak)) weak_v2;
2921 extern int weak_v3;
2923 extern int (*weak_fpa)() __attribute__((weak));
2924 extern int __attribute__((weak)) (*weak_fpb)();
2925 extern __attribute__((weak)) int (*weak_fpc)();
2927 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
2928 extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
2929 extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
2930 extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
2931 extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
2932 extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
2934 static const size_t dummy = 0;
2935 extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
2936 extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
2937 extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
2939 int some_lib_func(void);
2940 int dummy_impl_of_slf(void) { return 444; }
2941 int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
2943 int weak_toolate() __attribute__((weak));
2944 int weak_toolate() { return 0; }
2946 void __attribute__((weak)) weak_test(void)
2948 printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
2949 printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
2950 printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
2951 printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
2952 printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
2953 printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
2955 printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
2956 printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
2957 printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
2959 printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
2960 printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
2961 printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
2962 printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
2963 printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
2964 printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
2967 int __attribute__((weak)) weak_f2() { return 222; }
2968 int __attribute__((weak)) weak_f3() { return 333; }
2969 int __attribute__((weak)) weak_v2 = 222;
2970 int __attribute__((weak)) weak_v3 = 333;
2971 #endif
2973 void const_func(const int a)
2977 void const_warn_test(void)
2979 const_func(1);
2982 struct condstruct {
2983 int i;
2986 int getme (struct condstruct *s, int i)
2988 int i1 = (i == 0 ? 0 : s)->i;
2989 int i2 = (i == 0 ? s : 0)->i;
2990 int i3 = (i == 0 ? (void*)0 : s)->i;
2991 int i4 = (i == 0 ? s : (void*)0)->i;
2992 return i1 + i2 + i3 + i4;
2995 struct global_data
2997 int a[40];
2998 int *b[40];
3001 struct global_data global_data;
3003 int global_data_getstuff (int *, int);
3005 void global_data_callit (int i)
3007 *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
3010 int global_data_getstuff (int *p, int i)
3012 return *p + i;
3015 void global_data_test (void)
3017 global_data.a[0] = 42;
3018 global_data.b[0] = &global_data.a[0];
3019 global_data_callit (0);
3020 printf ("%d\n", global_data.a[0]);
3023 struct cmpcmpS
3025 unsigned char fill : 3;
3026 unsigned char b1 : 1;
3027 unsigned char b2 : 1;
3028 unsigned char fill2 : 3;
3031 int glob1, glob2, glob3;
3033 void compare_comparisons (struct cmpcmpS *s)
3035 if (s->b1 != (glob1 == glob2)
3036 || (s->b2 != (glob1 == glob3)))
3037 printf ("comparing comparisons broken\n");
3040 void cmp_comparison_test(void)
3042 struct cmpcmpS s;
3043 s.b1 = 1;
3044 glob1 = 42; glob2 = 42;
3045 s.b2 = 0;
3046 glob3 = 43;
3047 compare_comparisons (&s);
3050 int fcompare (double a, double b, int code)
3052 switch (code) {
3053 case 0: return a == b;
3054 case 1: return a != b;
3055 case 2: return a < b;
3056 case 3: return a >= b;
3057 case 4: return a > b;
3058 case 5: return a <= b;
3062 void math_cmp_test(void)
3064 double nan = 0.0/0.0;
3065 double one = 1.0;
3066 double two = 2.0;
3067 int comp = 0;
3068 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
3070 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
3071 And it does this in various ways so that all code generation paths
3072 are checked (generating inverted tests, or non-inverted tests, or
3073 producing a 0/1 value without jumps (that's done in the fcompare
3074 function). */
3075 #define FCMP(a,b,op,iop,code) \
3076 if (fcompare (a,b,code)) \
3077 bug (a,b,op,iop,1); \
3078 if (a op b) \
3079 bug (a,b,op,iop,2); \
3080 if (a iop b) \
3082 else \
3083 bug (a,b,op,iop,3); \
3084 if ((a op b) || comp) \
3085 bug (a,b,op,iop,4); \
3086 if ((a iop b) || comp) \
3088 else \
3089 bug (a,b,op,iop,5);
3091 /* Equality tests. */
3092 FCMP(nan, nan, ==, !=, 0);
3093 FCMP(one, two, ==, !=, 0);
3094 FCMP(one, one, !=, ==, 1);
3095 /* Non-equality is a bit special. */
3096 if (!fcompare (nan, nan, 1))
3097 bug (nan, nan, !=, ==, 6);
3099 /* Relational tests on numbers. */
3100 FCMP(two, one, <, >=, 2);
3101 FCMP(one, two, >=, <, 3);
3102 FCMP(one, two, >, <=, 4);
3103 FCMP(two, one, <=, >, 5);
3105 /* Relational tests on NaNs. Note that the inverse op here is
3106 always !=, there's no operator in C that is equivalent to !(a < b),
3107 when NaNs are involved, same for the other relational ops. */
3108 FCMP(nan, nan, <, !=, 2);
3109 FCMP(nan, nan, >=, !=, 3);
3110 FCMP(nan, nan, >, !=, 4);
3111 FCMP(nan, nan, <=, !=, 5);
3114 double get100 () { return 100.0; }
3116 void callsave_test(void)
3118 #if defined __i386__ || defined __x86_64__ || defined __arm__
3119 int i, s; double *d; double t;
3120 s = sizeof (double);
3121 printf ("callsavetest: %d\n", s);
3122 d = alloca (sizeof(double));
3123 d[0] = 10.0;
3124 /* x86-64 had a bug were the next call to get100 would evict
3125 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
3126 in int type, not pointer type. When alloca returns a pointer
3127 with the high 32 bit set (which is likely on x86-64) the access
3128 generates a segfault. */
3129 i = d[0] > get100 ();
3130 printf ("%d\n", i);
3131 #endif
3135 void bfa3(ptrdiff_t str_offset)
3137 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
3139 void bfa2(ptrdiff_t str_offset)
3141 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
3142 bfa3(str_offset);
3144 void bfa1(ptrdiff_t str_offset)
3146 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
3147 bfa2(str_offset);
3150 void builtin_frame_address_test(void)
3152 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
3153 #ifndef __arm__
3154 char str[] = "__builtin_frame_address";
3155 char *fp0 = __builtin_frame_address(0);
3157 printf("str: %s\n", str);
3158 bfa1(str-fp0);
3159 #endif
3162 char via_volatile (char i)
3164 char volatile vi;
3165 vi = i;
3166 return vi;
3169 struct __attribute__((__packed__)) Spacked {
3170 char a;
3171 short b;
3172 int c;
3174 struct Spacked spacked;
3175 typedef struct __attribute__((__packed__)) {
3176 char a;
3177 short b;
3178 int c;
3179 } Spacked2;
3180 Spacked2 spacked2;
3181 #ifdef BROKEN
3182 /* This doesn't work for now. Requires adjusting field offsets/sizes
3183 after parsing the struct members. */
3184 typedef struct Spacked3_s {
3185 char a;
3186 short b;
3187 int c;
3188 } __attribute__((__packed__)) Spacked3;
3189 Spacked3 spacked3;
3190 #endif
3191 void attrib_test(void)
3193 printf("attr: %d %d %d %d\n", sizeof(struct Spacked),
3194 sizeof(spacked), sizeof(Spacked2), sizeof(spacked2));
3195 #ifdef BROKEN
3196 printf("attr: %d %d\n", sizeof(Spacked3), sizeof(spacked3));
3197 #endif
3199 extern __attribute__((__unused__)) char * __attribute__((__unused__)) *
3200 strange_attrib_placement (void);
3202 void * __attribute__((__unused__)) get_void_ptr (void *a)
3204 return a;