Workaround old GCC viz indexed string literals
[tinycc.git] / tests / tcctest.c
blobb6f266d59ca219e6186e6f2537dfc52df744e3de
1 /*
2 * TCC auto test program
3 */
4 #include "config.h"
6 /* Unfortunately, gcc version < 3 does not handle that! */
7 #define ALL_ISOC99
9 /* only gcc 3 handles _Bool correctly */
10 #define BOOL_ISOC99
12 /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
13 #define CORRECT_CR_HANDLING
15 /* __VA_ARGS__ and __func__ support */
16 #define C99_MACROS
18 /* deprecated and no longer supported in gcc 3.3 */
19 //#define ACCEPT_CR_IN_STRINGS
21 #ifndef __TINYC__
22 typedef __SIZE_TYPE__ uintptr_t;
23 #endif
25 #if defined(_WIN32)
26 #define LONG_LONG_FORMAT "%lld"
27 #define ULONG_LONG_FORMAT "%llu"
28 #else
29 #define LONG_LONG_FORMAT "%Ld"
30 #define ULONG_LONG_FORMAT "%Lu"
31 #endif
33 // MinGW has 80-bit rather than 64-bit long double which isn't compatible with TCC or MSVC
34 #if defined(_WIN32) && defined(__GNUC__)
35 #define LONG_DOUBLE double
36 #define LONG_DOUBLE_LITERAL(x) x
37 #else
38 #define LONG_DOUBLE long double
39 #define LONG_DOUBLE_LITERAL(x) x ## L
40 #endif
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 #ifdef __clang__
68 /* clang's preprocessor is broken in this regard and adds spaces
69 to the tokens 'incdir' and 'funnyname' when expanding */
70 #define incname <tests/42test.h>
71 #else
72 #define incname < incdir funnyname >
73 #endif
74 #define __stringify(x) #x
75 #define stringify(x) __stringify(x)
76 #include INC(42test)
77 #include incname
78 #include stringify(funnyname)
80 void intdiv_test();
81 void string_test();
82 void expr_test();
83 void macro_test();
84 void recursive_macro_test();
85 void scope_test();
86 void scope_test2();
87 void forward_test();
88 void funcptr_test();
89 void if_test();
90 void loop_test();
91 void switch_test();
92 void goto_test();
93 void enum_test();
94 void typedef_test();
95 void struct_test();
96 void array_test();
97 void expr_ptr_test();
98 void bool_test();
99 void optimize_out();
100 void expr2_test();
101 void constant_expr_test();
102 void expr_cmp_test();
103 void char_short_test();
104 void init_test(void);
105 void compound_literal_test(void);
106 int kr_test();
107 void struct_assign_test(void);
108 void cast_test(void);
109 void bitfield_test(void);
110 void c99_bool_test(void);
111 void float_test(void);
112 void longlong_test(void);
113 void manyarg_test(void);
114 void stdarg_test(void);
115 void whitespace_test(void);
116 void relocation_test(void);
117 void old_style_function(void);
118 void alloca_test(void);
119 void c99_vla_test(int size1, int size2);
120 void sizeof_test(void);
121 void typeof_test(void);
122 void local_label_test(void);
123 void statement_expr_test(void);
124 void asm_test(void);
125 void builtin_test(void);
126 void weak_test(void);
127 void global_data_test(void);
128 void cmp_comparison_test(void);
129 void math_cmp_test(void);
130 void callsave_test(void);
131 void builtin_frame_address_test(void);
132 void attrib_test(void);
133 void bounds_check1_test(void);
135 int fib(int n);
136 void num(int n);
137 void forward_ref(void);
138 int isid(int c);
140 /* Line joining happens before tokenization, so the following
141 must be parsed as ellipsis. */
142 void funny_line_continuation (int, ..\
143 . );
145 char via_volatile (char);
147 #define A 2
148 #define N 1234 + A
149 #define pf printf
150 #define M1(a, b) (a) + (b)
152 #define str\
153 (s) # s
154 #define glue(a, b) a ## b
155 #define xglue(a, b) glue(a, b)
156 #define HIGHLOW "hello"
157 #define LOW LOW ", world"
159 static int onetwothree = 123;
160 #define onetwothree4 onetwothree
161 #define onetwothree xglue(onetwothree,4)
163 #define min(a, b) ((a) < (b) ? (a) : (b))
165 #ifdef C99_MACROS
166 #define dprintf(level,...) printf(__VA_ARGS__)
167 #endif
169 /* gcc vararg macros */
170 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
172 #define MACRO_NOARGS()
174 #define AAA 3
175 #undef AAA
176 #define AAA 4
178 #if 1
179 #define B3 1
180 #elif 1
181 #define B3 2
182 #elif 0
183 #define B3 3
184 #else
185 #define B3 4
186 #endif
188 #ifdef __TINYC__
189 /* We try to handle this syntax. Make at least sure it doesn't segfault. */
190 char invalid_function_def()[] {return 0;}
191 #endif
193 #define __INT64_C(c) c ## LL
194 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
196 int qq(int x)
198 return x + 40;
200 #define qq(x) x
202 #define spin_lock(lock) do { } while (0)
203 #define wq_spin_lock spin_lock
204 #define TEST2() wq_spin_lock(a)
206 #define UINT_MAX ((unsigned) -1)
208 void intdiv_test(void)
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", (UINT_MAX/2+3)/-2);
238 printf("(UINT_MAX/2+3)%%-2=%u\n", (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);
255 printf("-18/-21=%u\n", -18/-21);
256 printf("-18%%-21=%u\n", -18%-21);
257 printf("-41/-21=%u\n", -41/-21);
258 printf("-41%%-21=%u\n", -41%-21);
259 printf("-42/-21=%u\n", -42/-21);
260 printf("-42%%-21=%u\n", -42%-21);
261 printf("-43/-21=%u\n", -43/-21);
262 printf("-43%%-21=%u\n", -43%-21);
263 printf("-126/-21=%u\n", -126/-21);
264 printf("-126%%-21=%u\n", -126%-21);
265 printf("-131/-21=%u\n", -131/-21);
266 printf("-131%%-21=%u\n", -131%-21);
267 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX/2+3))/-2);
268 printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX/2+3))%-2);
271 void macro_test(void)
273 printf("macro:\n");\f\v
274 pf("N=%d\n", N);
275 printf("aaa=%d\n", AAA);
277 printf("min=%d\n", min(1, min(2, -1)));
279 printf("s1=%s\n", glue(HIGH, LOW));
280 printf("s2=%s\n", xglue(HIGH, LOW));
281 printf("s3=%s\n", str("c"));
282 printf("s4=%s\n", str(a1));
283 printf("B3=%d\n", B3);
285 printf("onetwothree=%d\n", onetwothree);
287 #ifdef A
288 printf("A defined\n");
289 #endif
290 #ifdef B
291 printf("B defined\n");
292 #endif
293 #ifdef A
294 printf("A defined\n");
295 #else
296 printf("A not defined\n");
297 #endif
298 #ifdef B
299 printf("B defined\n");
300 #else
301 printf("B not defined\n");
302 #endif
304 #ifdef A
305 printf("A defined\n");
306 #ifdef B
307 printf("B1 defined\n");
308 #else
309 printf("B1 not defined\n");
310 #endif
311 #else
312 printf("A not defined\n");
313 #ifdef B
314 printf("B2 defined\n");
315 #else
316 printf("B2 not defined\n");
317 #endif
318 #endif
320 #if 1+1
321 printf("test true1\n");
322 #endif
323 #if 0
324 printf("test true2\n");
325 #endif
326 #if 1-1
327 printf("test true3\n");
328 #endif
329 #if defined(A)
330 printf("test trueA\n");
331 #endif
332 #if defined(B)
333 printf("test trueB\n");
334 #endif
336 #if 0
337 printf("test 0\n");
338 #elif 0
339 printf("test 1\n");
340 #elif 2
341 printf("test 2\n");
342 #else
343 printf("test 3\n");
344 #endif
346 MACRO_NOARGS();
348 #ifdef __LINE__
349 printf("__LINE__ defined\n");
350 #endif
352 printf("__LINE__=%d __FILE__=%s\n",
353 __LINE__, __FILE__);
354 #if 0
355 #line 200
356 printf("__LINE__=%d __FILE__=%s\n",
357 __LINE__, __FILE__);
358 #line 203 "test"
359 printf("__LINE__=%d __FILE__=%s\n",
360 __LINE__, __FILE__);
361 #line 227 "tcctest.c"
362 #endif
364 /* not strictly preprocessor, but we test it there */
365 #ifdef C99_MACROS
366 printf("__func__ = %s\n", __func__);
367 dprintf(1, "vaarg=%d\n", 1);
368 #endif
369 dprintf1(1, "vaarg1\n");
370 dprintf1(1, "vaarg1=%d\n", 2);
371 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
373 /* gcc extension */
374 printf("func='%s'\n", __FUNCTION__);
376 /* complicated macros in glibc */
377 printf("INT64_MIN=" LONG_LONG_FORMAT "\n", INT64_MIN);
379 int a;
380 a = 1;
381 glue(a+, +);
382 printf("a=%d\n", a);
383 glue(a <, <= 2);
384 printf("a=%d\n", a);
387 /* macro function with argument outside the macro string */
388 #define MF_s MF_hello
389 #define MF_hello(msg) printf("%s\n",msg)
391 #define MF_t printf("tralala\n"); MF_hello
393 MF_s("hi");
394 MF_t("hi");
396 /* test macro substitution inside args (should not eat stream) */
397 printf("qq=%d\n", qq(qq)(2));
399 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
400 null argument without a space. gcc 3.2 fixes that. */
402 #define qq1(x) 1
403 printf("qq1=%d\n", qq1( ));
405 /* comment with stray handling *\
407 /* this is a valid *\/ comment */
408 /* this is a valid comment *\*/
409 // this is a valid\
410 comment
412 /* test function macro substitution when the function name is
413 substituted */
414 TEST2();
416 /* And again when the name and parentheses are separated by a
417 comment. */
418 TEST2 /* the comment */ ();
420 printf("basefromheader %s\n", get_basefile_from_header());
421 printf("base %s\n", __BASE_FILE__);
423 /* Some compilers (clang) prepend './' to __FILE__ from included
424 files. */
425 const char *fn = get_file_from_header();
426 if (fn[0] == '.' && fn[1] == '/')
427 fn += 2;
428 printf("filefromheader %s\n", fn);
430 printf("file %s\n", __FILE__);
432 /* Check that funnily named include was in fact included */
433 have_included_42test_h = 1;
434 have_included_42test_h_second = 1;
435 have_included_42test_h_third = 1;
437 /* Check that we don't complain about stray \ here */
438 printf("print a backslash: %s\n", stringify(\\));
442 static void print_num(char *fn, int line, int num) {
443 printf("fn %s, line %d, num %d\n", fn, line, num);
446 void recursive_macro_test(void)
449 #define ELF32_ST_TYPE(val) ((val) & 0xf)
450 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
451 #define STB_WEAK 2 /* Weak symbol */
452 #define ELFW(type) ELF##32##_##type
453 printf("%d\n", ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(123)));
455 #define WRAP(x) x
457 #define print_num(x) print_num(__FILE__,__LINE__,x)
458 print_num(123);
459 WRAP(print_num(123));
460 WRAP(WRAP(print_num(123)));
462 static struct recursive_macro { int rm_field; } G;
463 #define rm_field (G.rm_field)
464 printf("rm_field = %d\n", rm_field);
465 printf("rm_field = %d\n", WRAP(rm_field));
466 WRAP((printf("rm_field = %d %d\n", rm_field, WRAP(rm_field))));
469 int op(a,b)
471 return a / b;
474 int ret(a)
476 if (a == 2)
477 return 1;
478 if (a == 3)
479 return 2;
480 return 0;
483 #if !defined(__TINYC__) && (__GNUC__ >= 8)
484 /* Old GCCs don't regard "foo"[1] as constant, even in GNU dialect. */
485 #define CONSTANTINDEXEDSTRLIT
486 #endif
487 char str_ag1[] = "b";
488 char str_ag2[] = { "b" };
489 /*char str_bg1[] = ("cccc"); GCC accepts this with pedantic warning, TCC not */
490 #ifdef CONSTANTINDEXEDSTRLIT
491 char str_ag3[] = { "ab"[1], 0 };
492 char str_x[2] = { "xy" "z"[2], 0 };
493 #endif
494 char *str_ar[] = { "one", "two" };
495 struct str_SS {unsigned char a[3], b; };
496 struct str_SS str_sinit15 = { "r" };
497 struct str_SS str_sinit16[] = { { "q" }, 2 };
499 static void string_test2()
501 char *p = "hello";
502 char a3[2] = { "p" };
503 char a4[2] = { "ab" "c"[2], 0 };
504 char *pa1 = "def" + 1;
505 char *pa2 = { "xyz" + 1 };
506 int i = 0;
507 struct str_SS ss = { { [0 ... 1] = 'a' }, 0 };
508 #ifndef CONSTANTINDEXEDSTRLIT
509 char str_ag3[] = { "ab"[1], 0 };
510 char str_x[2] = { "xy" "z"[2], 0 };
511 #endif
512 puts("string_test2");
513 puts(str_ag1);
514 puts(str_ag2);
515 /*puts(str_bg1);*/
516 puts(str_ag3);
517 puts(str_x);
518 puts(str_sinit15.a);
519 puts(str_sinit16[0].a);
520 puts(a3);
521 puts(a4);
522 puts(p);
523 puts("world");
524 printf("%s\n", "bla");
525 puts(str_ar[0]);
526 puts(str_ar[1]);
527 puts(ss.a);
528 puts(i >= 0 ? "one" : "two");
529 puts(pa1);
530 puts(pa2);
533 void ps(const char *s)
535 int c;
536 while (1) {
537 c = *s;
538 if (c == 0)
539 break;
540 printf("%c", c);
541 s++;
545 const char foo1_string[] = "\
546 bar\n\
547 test\14\
550 void string_test()
552 unsigned int b;
553 printf("string:\n");
554 printf("\141\1423\143\n");/* dezdez test */
555 printf("\x41\x42\x43\x3a\n");
556 printf("c=%c\n", 'r');
557 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
558 printf("foo1_string='%s'\n", foo1_string);
559 #if 0
560 printf("wstring=%S\n", L"abc");
561 printf("wstring=%S\n", L"abc" L"def" "ghi");
562 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
563 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
564 #endif
565 ps("test\n");
566 b = 32;
567 while ((b = b + 1) < 96) {
568 printf("%c", b);
570 printf("\n");
571 printf("fib=%d\n", fib(33));
572 b = 262144;
573 while (b != 0x80000000) {
574 num(b);
575 b = b * 2;
577 string_test2();
581 void if1t(int n, int a, int b, int c)
583 if (a && b) printf("if1t: %d 1 %d %d\n", n, a, b);
584 if (a && !b) printf("if1t: %d 2 %d %d\n", n, a, b);
585 if (!a && b) printf("if1t: %d 3 %d %d\n", n, a, b);
586 if (!a && !b) printf("if1t: %d 4 %d %d\n", n, a, b);
587 if (a || b) printf("if1t: %d 5 %d %d\n", n, a, b);
588 if (a || !b) printf("if1t: %d 6 %d %d\n", n, a, b);
589 if (!a || b) printf("if1t: %d 7 %d %d\n", n, a, b);
590 if (!a || !b) printf("if1t: %d 8 %d %d\n", n, a, b);
591 if (a && b || c) printf("if1t: %d 9 %d %d %d\n", n, a, b, c);
592 if (a || b && c) printf("if1t: %d 10 %d %d %d\n", n, a, b, c);
593 if (a > b - 1 && c) printf("if1t: %d 11 %d %d %d\n", n, a, b, c);
594 if (a > b - 1 || c) printf("if1t: %d 12 %d %d %d\n", n, a, b, c);
595 if (a > 0 && 1) printf("if1t: %d 13 %d %d %d\n", n, a, b, c);
596 if (a > 0 || 0) printf("if1t: %d 14 %d %d %d\n", n, a, b, c);
599 void if2t(void)
601 if (0 && 1 || printf("if2t:ok\n") || 1)
602 printf("if2t:ok2\n");
603 printf("if2t:ok3\n");
606 void if_test(void)
608 if1t(1, 0, 0, 0);
609 if1t(2, 0, 3, 0);
610 if1t(3, 2, 0, 0);
611 if1t(4, 2, 3, 0);
612 if2t();
615 void loop_test()
617 int i;
618 i = 0;
619 while (i < 10)
620 printf("%d", i++);
621 printf("\n");
622 for(i = 0; i < 10;i++)
623 printf("%d", i);
624 printf("\n");
625 i = 0;
626 do {
627 printf("%d", i++);
628 } while (i < 10);
629 printf("\n");
631 char count = 123;
632 /* c99 for loop init test */
633 for (size_t count = 1; count < 3; count++)
634 printf("count=%d\n", count);
635 printf("count = %d\n", count);
637 /* break/continue tests */
638 i = 0;
639 while (1) {
640 if (i == 6)
641 break;
642 i++;
643 if (i == 3)
644 continue;
645 printf("%d", i);
647 printf("\n");
649 /* break/continue tests */
650 i = 0;
651 do {
652 if (i == 6)
653 break;
654 i++;
655 if (i == 3)
656 continue;
657 printf("%d", i);
658 } while(1);
659 printf("\n");
661 for(i = 0;i < 10;i++) {
662 if (i == 3)
663 continue;
664 printf("%d", i);
666 printf("\n");
669 typedef int typedef_and_label;
671 void goto_test()
673 int i;
674 static void *label_table[3] = { &&label1, &&label2, &&label3 };
676 printf("goto:\n");
677 i = 0;
678 /* This needs to parse as label, not as start of decl. */
679 typedef_and_label x;
680 typedef_and_label:
681 s_loop:
682 if (i >= 10)
683 goto s_end;
684 printf("%d", i);
685 i++;
686 goto s_loop;
687 s_end:
688 printf("\n");
690 /* we also test computed gotos (GCC extension) */
691 for(i=0;i<3;i++) {
692 goto *label_table[i];
693 label1:
694 printf("label1\n");
695 goto next;
696 label2:
697 printf("label2\n");
698 goto next;
699 label3:
700 printf("label3\n");
701 next: ;
705 enum {
707 E1 = 2,
708 E2 = 4,
713 enum test {
714 E5 = 1000,
717 struct S_enum {
718 enum {E6 = 42, E7, E8} e:8;
721 enum ELong {
722 /* This is either 0 on L32 machines, or a large number
723 on L64 machines. We should be able to store this. */
724 EL_large = ((unsigned long)0xf000 << 31) << 1,
727 enum { BIASU = -1U<<31 };
728 enum { BIASS = -1 << 31 };
730 static int getint(int i)
732 if (i)
733 return 0;
734 else
735 return (int)(-1U << 31);
738 void enum_test()
740 enum test b1;
741 /* The following should give no warning */
742 unsigned *p = &b1;
743 struct S_enum s = {E7};
744 printf("enum: %d\n", s.e);
745 printf("enum:\n%d %d %d %d %d %d\n",
746 E0, E1, E2, E3, E4, E5);
747 b1 = 1;
748 printf("b1=%d\n", b1);
749 printf("enum large: %ld\n", EL_large);
751 if (getint(0) == BIASU)
752 printf("enum unsigned: ok\n");
753 else
754 printf("enum unsigned: wrong\n");
755 if (getint(0) == BIASS)
756 printf("enum unsigned: ok\n");
757 else
758 printf("enum unsigned: wrong\n");
761 typedef int *my_ptr;
763 typedef int mytype1;
764 typedef int mytype2;
766 void typedef_test()
768 my_ptr a;
769 mytype1 mytype2;
770 int b;
772 a = &b;
773 *a = 1234;
774 printf("typedef:\n");
775 printf("a=%d\n", *a);
776 mytype2 = 2;
777 printf("mytype2=%d\n", mytype2);
780 void forward_test()
782 printf("forward:\n");
783 forward_ref();
784 forward_ref();
788 void forward_ref(void)
790 printf("forward ok\n");
793 typedef struct struct1 {
794 int f1;
795 int f2, f3;
796 union union1 {
797 int v1;
798 int v2;
799 } u;
800 char str[3];
801 } struct1;
803 struct struct2 {
804 int a;
805 char b;
808 union union2 {
809 int w1;
810 int w2;
813 struct struct1 st1, st2;
815 struct empty_mem {
816 /* nothing */ ;
817 int x;
820 int main(int argc, char **argv)
822 string_test();
823 expr_test();
824 macro_test();
825 recursive_macro_test();
826 scope_test();
827 scope_test2();
828 forward_test();
829 funcptr_test();
830 if_test();
831 loop_test();
832 switch_test();
833 goto_test();
834 enum_test();
835 typedef_test();
836 struct_test();
837 array_test();
838 expr_ptr_test();
839 bool_test();
840 optimize_out();
841 expr2_test();
842 constant_expr_test();
843 expr_cmp_test();
844 char_short_test();
845 init_test();
846 compound_literal_test();
847 kr_test();
848 struct_assign_test();
849 cast_test();
850 bitfield_test();
851 c99_bool_test();
852 float_test();
853 longlong_test();
854 manyarg_test();
855 stdarg_test();
856 whitespace_test();
857 relocation_test();
858 old_style_function();
859 alloca_test();
860 c99_vla_test(5, 2);
861 sizeof_test();
862 typeof_test();
863 statement_expr_test();
864 local_label_test();
865 asm_test();
866 builtin_test();
867 #ifndef _WIN32
868 weak_test();
869 #endif
870 global_data_test();
871 cmp_comparison_test();
872 math_cmp_test();
873 callsave_test();
874 builtin_frame_address_test();
875 intdiv_test();
876 if (via_volatile (42) != 42)
877 printf ("via_volatile broken\n");
878 attrib_test();
879 bounds_check1_test();
880 return 0;
883 int tab[3];
884 int tab2[3][2];
886 int g;
888 void f1(g)
890 printf("g1=%d\n", g);
893 void scope_test()
895 printf("scope:\n");
896 g = 2;
897 f1(1);
898 printf("g2=%d\n", g);
900 int g;
901 g = 3;
902 printf("g3=%d\n", g);
904 int g;
905 g = 4;
906 printf("g4=%d\n", g);
909 printf("g5=%d\n", g);
912 int st2_i;
913 int *st2_p = &st2_i;
914 void scope_test2()
916 char a[50];
917 st2_i = 42;
918 for (int st2_i = 1; st2_i < 10; st2_i++) {
919 extern int st2_i;
920 st2_i++;
921 printf("exloc: %d\n", st2_i);
923 printf("exloc: %d\n", *st2_p);
926 /* C has tentative definition, and they may be repeated. */
927 extern int st_global1;
928 int st_global1=42;
929 extern int st_global1;
930 int st_global1;
931 extern int st_global2;
932 int st_global2;
933 extern int st_global2;
934 int st_global2;
936 void array_test()
938 int i, j, a[4];
940 printf("array:\n");
941 printf("sizeof(a) = %d\n", sizeof(a));
942 printf("sizeof(\"a\") = %d\n", sizeof("a"));
943 #ifdef C99_MACROS
944 printf("sizeof(__func__) = %d\n", sizeof(__func__));
945 #endif
946 printf("sizeof tab %d\n", sizeof(tab));
947 printf("sizeof tab2 %d\n", sizeof tab2);
948 tab[0] = 1;
949 tab[1] = 2;
950 tab[2] = 3;
951 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
952 for(i=0;i<3;i++)
953 for(j=0;j<2;j++)
954 tab2[i][j] = 10 * i + j;
955 for(i=0;i<3*2;i++) {
956 printf(" %3d", ((int *)tab2)[i]);
958 printf("\n");
959 printf("sizeof(size_t)=%d\n", sizeof(size_t));
960 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
963 void expr_test()
965 int a, b;
966 a = 0;
967 printf("%d\n", a += 1);
968 printf("%d\n", a -= 2);
969 printf("%d\n", a *= 31232132);
970 printf("%d\n", a /= 4);
971 printf("%d\n", a %= 20);
972 printf("%d\n", a &= 6);
973 printf("%d\n", a ^= 7);
974 printf("%d\n", a |= 8);
975 printf("%d\n", a >>= 3);
976 printf("%d\n", a <<= 4);
978 a = 22321;
979 b = -22321;
980 printf("%d\n", a + 1);
981 printf("%d\n", a - 2);
982 printf("%d\n", a * 312);
983 printf("%d\n", a / 4);
984 printf("%d\n", b / 4);
985 printf("%d\n", (unsigned)b / 4);
986 printf("%d\n", a % 20);
987 printf("%d\n", b % 20);
988 printf("%d\n", (unsigned)b % 20);
989 printf("%d\n", a & 6);
990 printf("%d\n", a ^ 7);
991 printf("%d\n", a | 8);
992 printf("%d\n", a >> 3);
993 printf("%d\n", b >> 3);
994 printf("%d\n", (unsigned)b >> 3);
995 printf("%d\n", a << 4);
996 printf("%d\n", ~a);
997 printf("%d\n", -a);
998 printf("%d\n", +a);
1000 printf("%d\n", 12 + 1);
1001 printf("%d\n", 12 - 2);
1002 printf("%d\n", 12 * 312);
1003 printf("%d\n", 12 / 4);
1004 printf("%d\n", 12 % 20);
1005 printf("%d\n", 12 & 6);
1006 printf("%d\n", 12 ^ 7);
1007 printf("%d\n", 12 | 8);
1008 printf("%d\n", 12 >> 2);
1009 printf("%d\n", 12 << 4);
1010 printf("%d\n", ~12);
1011 printf("%d\n", -12);
1012 printf("%d\n", +12);
1013 printf("%d %d %d %d\n",
1014 isid('a'),
1015 isid('g'),
1016 isid('T'),
1017 isid('('));
1020 int isid(int c)
1022 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
1025 /**********************/
1027 int vstack[10], *vstack_ptr;
1029 void vpush(int vt, int vc)
1031 *vstack_ptr++ = vt;
1032 *vstack_ptr++ = vc;
1035 void vpop(int *ft, int *fc)
1037 *fc = *--vstack_ptr;
1038 *ft = *--vstack_ptr;
1041 void expr2_test()
1043 int a, b;
1045 printf("expr2:\n");
1046 vstack_ptr = vstack;
1047 vpush(1432432, 2);
1048 vstack_ptr[-2] &= ~0xffffff80;
1049 vpop(&a, &b);
1050 printf("res= %d %d\n", a, b);
1053 int const_len_ar[sizeof(1/0)]; /* div-by-zero, but in unevaluated context */
1055 void constant_expr_test()
1057 int a;
1058 printf("constant_expr:\n");
1059 a = 3;
1060 printf("%d\n", a * 16);
1061 printf("%d\n", a * 1);
1062 printf("%d\n", a + 0);
1063 printf("%d\n", sizeof(const_len_ar));
1066 int tab4[10];
1068 void expr_ptr_test()
1070 int *p, *q;
1071 int i = -1;
1073 printf("expr_ptr:\n");
1074 p = tab4;
1075 q = tab4 + 10;
1076 printf("diff=%d\n", q - p);
1077 p++;
1078 printf("inc=%d\n", p - tab4);
1079 p--;
1080 printf("dec=%d\n", p - tab4);
1081 ++p;
1082 printf("inc=%d\n", p - tab4);
1083 --p;
1084 printf("dec=%d\n", p - tab4);
1085 printf("add=%d\n", p + 3 - tab4);
1086 printf("add=%d\n", 3 + p - tab4);
1088 /* check if 64bit support is ok */
1089 q = p = 0;
1090 q += i;
1091 printf("%p %p %ld\n", q, p, p-q);
1092 printf("%d %d %d %d %d %d\n",
1093 p == q, p != q, p < q, p <= q, p >= q, p > q);
1094 i = 0xf0000000;
1095 p += i;
1096 printf("%p %p %ld\n", q, p, p-q);
1097 printf("%d %d %d %d %d %d\n",
1098 p == q, p != q, p < q, p <= q, p >= q, p > q);
1099 p = (int *)((char *)p + 0xf0000000);
1100 printf("%p %p %ld\n", q, p, p-q);
1101 printf("%d %d %d %d %d %d\n",
1102 p == q, p != q, p < q, p <= q, p >= q, p > q);
1103 p += 0xf0000000;
1104 printf("%p %p %ld\n", q, p, p-q);
1105 printf("%d %d %d %d %d %d\n",
1106 p == q, p != q, p < q, p <= q, p >= q, p > q);
1108 struct size12 {
1109 int i, j, k;
1111 struct size12 s[2], *sp = s;
1112 int i, j;
1113 sp->i = 42;
1114 sp++;
1115 j = -1;
1116 printf("%d\n", sp[j].i);
1118 #ifdef __LP64__
1119 i = 1;
1120 p = (int*)0x100000000UL + i;
1121 i = ((long)p) >> 32;
1122 printf("largeptr: %p %d\n", p, i);
1123 #endif
1126 void expr_cmp_test()
1128 int a, b;
1129 printf("constant_expr:\n");
1130 a = -1;
1131 b = 1;
1132 printf("%d\n", a == a);
1133 printf("%d\n", a != a);
1135 printf("%d\n", a < b);
1136 printf("%d\n", a <= b);
1137 printf("%d\n", a <= a);
1138 printf("%d\n", b >= a);
1139 printf("%d\n", a >= a);
1140 printf("%d\n", b > a);
1142 printf("%d\n", (unsigned)a < b);
1143 printf("%d\n", (unsigned)a <= b);
1144 printf("%d\n", (unsigned)a <= a);
1145 printf("%d\n", (unsigned)b >= a);
1146 printf("%d\n", (unsigned)a >= a);
1147 printf("%d\n", (unsigned)b > a);
1150 struct empty {
1153 struct aligntest1 {
1154 char a[10];
1157 struct aligntest2 {
1158 int a;
1159 char b[10];
1162 struct aligntest3 {
1163 double a, b;
1166 struct aligntest4 {
1167 double a[0];
1170 struct __attribute__((aligned(16))) aligntest5
1172 int i;
1174 struct aligntest6
1176 int i;
1177 } __attribute__((aligned(16)));
1178 struct aligntest7
1180 int i;
1182 struct aligntest5 altest5[2];
1183 struct aligntest6 altest6[2];
1184 int pad1;
1185 /* altest7 is correctly aligned to 16 bytes also with TCC,
1186 but __alignof__ returns the wrong result (4) because we
1187 can't store the alignment yet when specified on symbols
1188 directly (it's stored in the type so we'd need to make
1189 a copy of it). -- FIXED */
1190 struct aligntest7 altest7[2] __attribute__((aligned(16)));
1192 struct aligntest8
1194 int i;
1195 } __attribute__((aligned(4096)));
1197 struct Large {
1198 unsigned long flags;
1199 union {
1200 void *u1;
1201 int *u2;
1204 struct {
1205 union {
1206 unsigned long index;
1207 void *freelist;
1209 union {
1210 unsigned long counters;
1211 struct {
1212 int bla;
1217 union {
1218 struct {
1219 long u3;
1220 long u4;
1222 void *u5;
1223 struct {
1224 unsigned long compound_head;
1225 unsigned int compound_dtor;
1226 unsigned int compound_order;
1229 } __attribute__((aligned(2 * sizeof(long))));
1231 typedef unsigned long long __attribute__((aligned(4))) unaligned_u64;
1233 struct aligntest9 {
1234 unsigned int buf_nr;
1235 unaligned_u64 start_lba;
1238 struct aligntest10 {
1239 unsigned int buf_nr;
1240 unsigned long long start_lba;
1243 void struct_test()
1245 struct1 *s;
1246 union union2 u;
1247 struct Large ls;
1249 printf("struct:\n");
1250 printf("sizes: %d %d %d %d\n",
1251 sizeof(struct struct1),
1252 sizeof(struct struct2),
1253 sizeof(union union1),
1254 sizeof(union union2));
1255 printf("offsets: %d\n", (int)((char*)&st1.u.v1 - (char*)&st1));
1256 st1.f1 = 1;
1257 st1.f2 = 2;
1258 st1.f3 = 3;
1259 printf("st1: %d %d %d\n",
1260 st1.f1, st1.f2, st1.f3);
1261 st1.u.v1 = 1;
1262 st1.u.v2 = 2;
1263 printf("union1: %d\n", st1.u.v1);
1264 u.w1 = 1;
1265 u.w2 = 2;
1266 printf("union2: %d\n", u.w1);
1267 s = &st2;
1268 s->f1 = 3;
1269 s->f2 = 2;
1270 s->f3 = 1;
1271 printf("st2: %d %d %d\n",
1272 s->f1, s->f2, s->f3);
1273 printf("str_addr=%x\n", (int)(uintptr_t)st1.str - (int)(uintptr_t)&st1.f1);
1275 /* align / size tests */
1276 printf("aligntest1 sizeof=%d alignof=%d\n",
1277 sizeof(struct aligntest1), __alignof__(struct aligntest1));
1278 printf("aligntest2 sizeof=%d alignof=%d\n",
1279 sizeof(struct aligntest2), __alignof__(struct aligntest2));
1280 printf("aligntest3 sizeof=%d alignof=%d\n",
1281 sizeof(struct aligntest3), __alignof__(struct aligntest3));
1282 printf("aligntest4 sizeof=%d alignof=%d\n",
1283 sizeof(struct aligntest4), __alignof__(struct aligntest4));
1284 printf("aligntest5 sizeof=%d alignof=%d\n",
1285 sizeof(struct aligntest5), __alignof__(struct aligntest5));
1286 printf("aligntest6 sizeof=%d alignof=%d\n",
1287 sizeof(struct aligntest6), __alignof__(struct aligntest6));
1288 printf("aligntest7 sizeof=%d alignof=%d\n",
1289 sizeof(struct aligntest7), __alignof__(struct aligntest7));
1290 printf("aligntest8 sizeof=%d alignof=%d\n",
1291 sizeof(struct aligntest8), __alignof__(struct aligntest8));
1292 printf("aligntest9 sizeof=%d alignof=%d\n",
1293 sizeof(struct aligntest9), __alignof__(struct aligntest9));
1294 printf("aligntest10 sizeof=%d alignof=%d\n",
1295 sizeof(struct aligntest10), __alignof__(struct aligntest10));
1296 printf("altest5 sizeof=%d alignof=%d\n",
1297 sizeof(altest5), __alignof__(altest5));
1298 printf("altest6 sizeof=%d alignof=%d\n",
1299 sizeof(altest6), __alignof__(altest6));
1300 printf("altest7 sizeof=%d alignof=%d\n",
1301 sizeof(altest7), __alignof__(altest7));
1303 /* empty structures (GCC extension) */
1304 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
1305 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
1307 printf("Large: sizeof=%d\n", sizeof(ls));
1308 memset(&ls, 0, sizeof(ls));
1309 ls.compound_head = 42;
1310 printf("Large: offsetof(compound_head)=%d\n", (int)((char*)&ls.compound_head - (char*)&ls));
1313 /* simulate char/short return value with undefined upper bits */
1314 static int __csf(int x) { return x; }
1315 static void *_csf = __csf;
1316 #define csf(t,n) ((t(*)(int))_csf)(n)
1318 /* XXX: depend on endianness */
1319 void char_short_test()
1321 int var1, var2;
1322 signed char var3;
1323 long long var4;
1325 printf("char_short:\n");
1327 var1 = 0x01020304;
1328 var2 = 0xfffefdfc;
1329 printf("s8=%d %d\n",
1330 *(signed char *)&var1, *(signed char *)&var2);
1331 printf("u8=%d %d\n",
1332 *(unsigned char *)&var1, *(unsigned char *)&var2);
1333 printf("s16=%d %d\n",
1334 *(short *)&var1, *(short *)&var2);
1335 printf("u16=%d %d\n",
1336 *(unsigned short *)&var1, *(unsigned short *)&var2);
1337 printf("s32=%d %d\n",
1338 *(int *)&var1, *(int *)&var2);
1339 printf("u32=%d %d\n",
1340 *(unsigned int *)&var1, *(unsigned int *)&var2);
1341 *(signed char *)&var1 = 0x08;
1342 printf("var1=%x\n", var1);
1343 *(short *)&var1 = 0x0809;
1344 printf("var1=%x\n", var1);
1345 *(int *)&var1 = 0x08090a0b;
1346 printf("var1=%x\n", var1);
1348 var1 = 0x778899aa;
1349 var4 = 0x11223344aa998877ULL;
1350 var1 = var3 = var1 + 1;
1351 var4 = var3 = var4 + 1;
1352 printf("promote char/short assign %d "LONG_LONG_FORMAT"\n", var1, var4);
1353 var1 = 0x778899aa;
1354 var4 = 0x11223344aa998877ULL;
1355 printf("promote char/short assign VA %d %d\n", var3 = var1 + 1, var3 = var4 + 1);
1356 printf("promote char/short cast VA %d %d\n", (signed char)(var1 + 1), (signed char)(var4 + 1));
1357 #if !defined(__arm__)
1358 /* We can't really express GCC behaviour of return type promotion in
1359 the presence of undefined behaviour (like __csf is). */
1360 var1 = csf(unsigned char,0x89898989);
1361 var4 = csf(signed char,0xabababab);
1362 printf("promote char/short funcret %d "LONG_LONG_FORMAT"\n", var1, var4);
1363 printf("promote char/short fumcret VA %d %d %d %d\n",
1364 csf(unsigned short,0xcdcdcdcd),
1365 csf(short,0xefefefef),
1366 csf(_Bool,0x33221100),
1367 csf(_Bool,0x33221101));
1368 #endif
1369 var3 = -10;
1370 var1 = (signed char)(unsigned char)(var3 + 1);
1371 var4 = (signed char)(unsigned char)(var3 + 1);
1372 printf("promote multicast (char)(unsigned char) %d "LONG_LONG_FORMAT"\n", var1, var4);
1373 var4 = 0x11223344aa998877ULL;
1374 var4 = (unsigned)(int)(var4 + 1);
1375 printf("promote multicast (unsigned)(int) "LONG_LONG_FORMAT"\n", var4);
1376 var4 = 0x11223344bbaa9988ULL;
1377 var4 = (unsigned)(signed char)(var4 + 1);
1378 printf("promote multicast (unsigned)(char) "LONG_LONG_FORMAT"\n", var4);
1381 /******************/
1383 typedef struct Sym {
1384 int v;
1385 int t;
1386 int c;
1387 struct Sym *next;
1388 struct Sym *prev;
1389 } Sym;
1391 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1392 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1394 static int toupper1(int a)
1396 return TOUPPER(a);
1399 static unsigned int calc_vm_flags(unsigned int prot)
1401 unsigned int prot_bits;
1402 /* This used to segfault in some revisions: */
1403 prot_bits = ((0x1==0x00000001)?(prot&0x1):(prot&0x1)?0x00000001:0);
1404 return prot_bits;
1407 void bool_test()
1409 int *s, a, b, t, f, i;
1411 a = 0;
1412 s = (void*)0;
1413 printf("!s=%d\n", !s);
1415 if (!s || !s[0])
1416 a = 1;
1417 printf("a=%d\n", a);
1419 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1420 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1421 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1422 #if 1 && 1
1423 printf("a1\n");
1424 #endif
1425 #if 1 || 0
1426 printf("a2\n");
1427 #endif
1428 #if 1 ? 0 : 1
1429 printf("a3\n");
1430 #endif
1431 #if 0 ? 0 : 1
1432 printf("a4\n");
1433 #endif
1435 a = 4;
1436 printf("b=%d\n", a + (0 ? 1 : a / 2));
1438 /* test register spilling */
1439 a = 10;
1440 b = 10;
1441 a = (a + b) * ((a < b) ?
1442 ((b - a) * (a - b)): a + b);
1443 printf("a=%d\n", a);
1445 /* test complex || or && expressions */
1446 t = 1;
1447 f = 0;
1448 a = 32;
1449 printf("exp=%d\n", f == (32 <= a && a <= 3));
1450 printf("r=%d\n", (t || f) + (t && f));
1452 /* test ? : cast */
1454 int aspect_on;
1455 int aspect_native = 65536;
1456 double bfu_aspect = 1.0;
1457 int aspect;
1458 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
1459 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
1460 printf("aspect=%d\n", aspect);
1464 /* test ? : GCC extension */
1466 static int v1 = 34 ? : -1; /* constant case */
1467 static int v2 = 0 ? : -1; /* constant case */
1468 int a = 30;
1470 printf("%d %d\n", v1, v2);
1471 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
1474 /* again complex expression */
1475 for(i=0;i<256;i++) {
1476 if (toupper1 (i) != TOUPPER (i))
1477 printf("error %d\n", i);
1479 printf ("bits = 0x%x\n", calc_vm_flags (0x1));
1482 extern int undefined_function(void);
1483 extern int defined_function(void);
1485 static inline void refer_to_undefined(void)
1487 undefined_function();
1490 void optimize_out(void)
1492 int i = 0 ? undefined_function() : defined_function();
1493 printf ("oo:%d\n", i);
1494 int j = 1 ? defined_function() : undefined_function();
1495 printf ("oo:%d\n", j);
1496 if (0)
1497 printf("oo:%d\n", undefined_function());
1498 else
1499 printf("oo:%d\n", defined_function());
1500 if (1)
1501 printf("oo:%d\n", defined_function());
1502 else
1503 printf("oo:%d\n", undefined_function());
1504 while (1) {
1505 printf("oow:%d\n", defined_function());
1506 break;
1507 printf("oow:%d\n", undefined_function());
1509 j = 1;
1510 /* Following is a switch without {} block intentionally. */
1511 switch (j)
1512 case 1: break;
1513 printf ("oos:%d\n", defined_function());
1514 /* The following break shouldn't lead to disabled code after
1515 the while. */
1516 while (1)
1517 break;
1518 printf ("ool1:%d\n", defined_function());
1519 /* Same for the other types of loops. */
1521 break;
1522 while (1);
1523 printf ("ool2:%d\n", defined_function());
1524 for (;;)
1525 break;
1526 printf ("ool3:%d\n", defined_function());
1527 /* Normal {} blocks without controlling statements
1528 shouldn't reactivate code emission */
1529 while (1) {
1531 break;
1533 printf ("ool4:%d\n", undefined_function());
1535 j = 1;
1536 while (j) {
1537 if (j == 0)
1538 break; /* this break shouldn't disable code outside the if. */
1539 printf("ool5:%d\n", defined_function());
1540 j--;
1543 j = 1;
1544 while (j) {
1545 if (1)
1546 j--;
1547 else
1548 breakhere: break;
1549 printf("ool6:%d\n", defined_function());
1550 goto breakhere;
1552 j = 1;
1553 while (j) {
1554 j--;
1555 continue;
1556 printf("ool7:%d\n", undefined_function());
1559 /* Test that constants in logical && are optimized: */
1560 i = 0 && undefined_function();
1561 i = defined_function() && 0 && undefined_function();
1562 if (0 && undefined_function())
1563 undefined_function();
1564 if (defined_function() && 0)
1565 undefined_function();
1566 if (0 && 0)
1567 undefined_function();
1568 if (defined_function() && 0 && undefined_function())
1569 undefined_function();
1570 /* The same for || : */
1571 i = 1 || undefined_function();
1572 i = defined_function() || 1 || undefined_function();
1573 if (1 || undefined_function())
1575 else
1576 undefined_function();
1577 if (defined_function() || 1)
1579 else
1580 undefined_function();
1581 if (1 || 1)
1583 else
1584 undefined_function();
1585 if (defined_function() || 1 || undefined_function())
1587 else
1588 undefined_function();
1590 if (defined_function() && 0)
1591 refer_to_undefined();
1593 if (0) {
1594 (void)sizeof( ({
1595 do { } while (0);
1597 }) );
1598 undefined_function();
1601 /* Leave the "if(1)return; printf()" in this order and last in the function */
1602 if (1)
1603 return;
1604 printf ("oor:%d\n", undefined_function());
1607 int defined_function(void)
1609 static int i = 40;
1610 return i++;
1613 /* GCC accepts that */
1614 static int tab_reinit[];
1615 static int tab_reinit[10];
1617 static int tentative_ar[];
1618 static int tentative_ar[] = {1,2,3};
1620 //int cinit1; /* a global variable can be defined several times without error ! */
1621 int cinit1;
1622 int cinit1;
1623 int cinit1 = 0;
1624 int *cinit2 = (int []){3, 2, 1};
1626 void compound_literal_test(void)
1628 int *p, i;
1629 char *q, *q3;
1631 printf("compound_test:\n");
1633 p = (int []){1, 2, 3};
1634 for(i=0;i<3;i++)
1635 printf(" %d", p[i]);
1636 printf("\n");
1638 for(i=0;i<3;i++)
1639 printf("%d", cinit2[i]);
1640 printf("\n");
1642 q = "tralala1";
1643 printf("q1=%s\n", q);
1645 q = (char *){ "tralala2" };
1646 printf("q2=%s\n", q);
1648 q3 = (char *){ q };
1649 printf("q3=%s\n", q3);
1651 q = (char []){ "tralala3" };
1652 printf("q4=%s\n", q);
1654 #ifdef ALL_ISOC99
1655 p = (int []){1, 2, cinit1 + 3};
1656 for(i=0;i<3;i++)
1657 printf(" %d", p[i]);
1658 printf("\n");
1660 for(i=0;i<3;i++) {
1661 p = (int []){1, 2, 4 + i};
1662 printf("%d %d %d\n",
1663 p[0],
1664 p[1],
1665 p[2]);
1667 #endif
1670 /* K & R protos */
1672 kr_func1(a, b)
1674 return a + b;
1677 int kr_func2(a, b)
1679 return a + b;
1682 kr_test()
1684 printf("kr_test:\n");
1685 printf("func1=%d\n", kr_func1(3, 4));
1686 printf("func2=%d\n", kr_func2(3, 4));
1687 return 0;
1690 void num(int n)
1692 char *tab, *p;
1693 tab = (char*)malloc(20);
1694 p = tab;
1695 while (1) {
1696 *p = 48 + (n % 10);
1697 p++;
1698 n = n / 10;
1699 if (n == 0)
1700 break;
1702 while (p != tab) {
1703 p--;
1704 printf("%c", *p);
1706 printf("\n");
1707 free(tab);
1710 /* structure assignment tests */
1711 struct structa1 {
1712 int f1;
1713 char f2;
1716 struct structa1 ssta1;
1718 void struct_assign_test1(struct structa1 s1, int t, float f)
1720 printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
1723 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1725 s1.f1 += t;
1726 s1.f2 -= t;
1727 return s1;
1730 void struct_assign_test(void)
1732 struct S {
1733 struct structa1 lsta1, lsta2;
1734 int i;
1735 } s, *ps;
1737 ps = &s;
1738 ps->i = 4;
1739 #if 0
1740 printf("struct_assign_test:\n");
1742 s.lsta1.f1 = 1;
1743 s.lsta1.f2 = 2;
1744 printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
1745 s.lsta2 = s.lsta1;
1746 printf("%d %d\n", s.lsta2.f1, s.lsta2.f2);
1747 #else
1748 s.lsta2.f1 = 1;
1749 s.lsta2.f2 = 2;
1750 #endif
1751 struct_assign_test1(ps->lsta2, 3, 4.5);
1753 printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
1754 ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
1755 printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
1757 static struct {
1758 void (*elem)();
1759 } t[] = {
1760 /* XXX: we should allow this even without braces */
1761 { struct_assign_test }
1763 printf("%d\n", struct_assign_test == t[0].elem);
1766 /* casts to short/char */
1768 void cast1(char a, short b, unsigned char c, unsigned short d)
1770 printf("%d %d %d %d\n", a, b, c, d);
1773 char bcast;
1774 short scast;
1776 void cast_test()
1778 int a;
1779 char c;
1780 char tab[10];
1781 unsigned b,d;
1782 short s;
1783 char *p = NULL;
1784 unsigned long ul = 0x80000000UL;
1785 p -= 0x700000000042;
1787 printf("cast_test:\n");
1788 a = 0xfffff;
1789 cast1(a, a, a, a);
1790 a = 0xffffe;
1791 printf("%d %d %d %d\n",
1792 (char)(a + 1),
1793 (short)(a + 1),
1794 (unsigned char)(a + 1),
1795 (unsigned short)(a + 1));
1796 printf("%d %d %d %d\n",
1797 (char)0xfffff,
1798 (short)0xfffff,
1799 (unsigned char)0xfffff,
1800 (unsigned short)0xfffff);
1802 a = (bcast = 128) + 1;
1803 printf("%d\n", a);
1804 a = (scast = 65536) + 1;
1805 printf("%d\n", a);
1807 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1809 /* test cast from unsigned to signed short to int */
1810 b = 0xf000;
1811 d = (short)b;
1812 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1813 b = 0xf0f0;
1814 d = (char)b;
1815 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1817 /* test implicit int casting for array accesses */
1818 c = 0;
1819 tab[1] = 2;
1820 tab[c] = 1;
1821 printf("%d %d\n", tab[0], tab[1]);
1823 /* test implicit casting on some operators */
1824 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1825 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1826 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1828 /* from pointer to integer types */
1829 printf("%d %d %ld %ld %lld %lld\n",
1830 (int)p, (unsigned int)p,
1831 (long)p, (unsigned long)p,
1832 (long long)p, (unsigned long long)p);
1834 /* from integers to pointers */
1835 printf("%p %p %p %p\n",
1836 (void *)a, (void *)b, (void *)c, (void *)d);
1838 /* int to int with sign set */
1839 printf("0x%lx\n", (unsigned long)(int)ul);
1842 /* initializers tests */
1843 struct structinit1 {
1844 int f1;
1845 char f2;
1846 short f3;
1847 int farray[3];
1850 int sinit1 = 2;
1851 int sinit2 = { 3 };
1852 int sinit3[3] = { 1, 2, {{3}}, };
1853 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1854 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1855 int sinit6[] = { 1, 2, 3 };
1856 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1857 char sinit8[] = "hello" "trala";
1859 struct structinit1 sinit9 = { 1, 2, 3 };
1860 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1861 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1862 #ifdef ALL_ISOC99
1863 .farray[0] = 10,
1864 .farray[1] = 11,
1865 .farray[2] = 12,
1866 #endif
1869 char *sinit12 = "hello world";
1870 char *sinit13[] = {
1871 "test1",
1872 "test2",
1873 "test3",
1875 char sinit14[10] = { "abc" };
1876 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1878 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1880 struct bar {
1881 char *s;
1882 int len;
1883 } sinit17[] = {
1884 "a1", 4,
1885 "a2", 1
1888 int sinit18[10] = {
1889 [2 ... 5] = 20,
1891 [8] = 10,
1894 struct complexinit0 {
1895 int a;
1896 int b;
1899 struct complexinit {
1900 int a;
1901 const struct complexinit0 *b;
1904 const static struct complexinit cix[] = {
1905 [0] = {
1906 .a = 2000,
1907 .b = (const struct complexinit0[]) {
1908 { 2001, 2002 },
1909 { 2003, 2003 },
1915 struct complexinit2 {
1916 int a;
1917 int b[];
1920 struct complexinit2 cix20;
1922 struct complexinit2 cix21 = {
1923 .a = 3000,
1924 .b = { 3001, 3002, 3003 }
1927 struct complexinit2 cix22 = {
1928 .a = 4000,
1929 .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1932 typedef int arrtype1[];
1933 arrtype1 sinit19 = {1};
1934 arrtype1 sinit20 = {2,3};
1935 typedef int arrtype2[3];
1936 arrtype2 sinit21 = {4};
1937 arrtype2 sinit22 = {5,6,7};
1939 /* Address comparisons of non-weak symbols with zero can be const-folded */
1940 int sinit23[2] = { "astring" ? sizeof("astring") : -1,
1941 &sinit23 ? 42 : -1 };
1943 int sinit24 = 2 || 1 / 0; /* exception in constant but unevaluated context */
1945 extern int external_inited = 42;
1947 void init_test(void)
1949 int linit1 = 2;
1950 int linit2 = { 3 };
1951 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1952 int linit6[] = { 1, 2, 3 };
1953 int i, j;
1954 char linit8[] = "hello" "trala";
1955 int linit12[10] = { 1, 2 };
1956 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1957 char linit14[10] = "abc";
1958 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1959 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1960 int linit17 = sizeof(linit17);
1961 int zero = 0;
1962 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1963 int linit18[2] = {&zero ? 1 : -1, zero ? -1 : 1 };
1965 printf("init_test:\n");
1967 printf("sinit1=%d\n", sinit1);
1968 printf("sinit2=%d\n", sinit2);
1969 printf("sinit3=%d %d %d %d\n",
1970 sizeof(sinit3),
1971 sinit3[0],
1972 sinit3[1],
1973 sinit3[2]
1975 printf("sinit6=%d\n", sizeof(sinit6));
1976 printf("sinit7=%d %d %d %d\n",
1977 sizeof(sinit7),
1978 sinit7[0],
1979 sinit7[1],
1980 sinit7[2]
1982 printf("sinit8=%s\n", sinit8);
1983 printf("sinit9=%d %d %d\n",
1984 sinit9.f1,
1985 sinit9.f2,
1986 sinit9.f3
1988 printf("sinit10=%d %d %d\n",
1989 sinit10.f1,
1990 sinit10.f2,
1991 sinit10.f3
1993 printf("sinit11=%d %d %d %d %d %d\n",
1994 sinit11.f1,
1995 sinit11.f2,
1996 sinit11.f3,
1997 sinit11.farray[0],
1998 sinit11.farray[1],
1999 sinit11.farray[2]
2002 for(i=0;i<3;i++)
2003 for(j=0;j<2;j++)
2004 printf("[%d][%d] = %d %d %d\n",
2005 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
2006 printf("linit1=%d\n", linit1);
2007 printf("linit2=%d\n", linit2);
2008 printf("linit6=%d\n", sizeof(linit6));
2009 printf("linit8=%d %s\n", sizeof(linit8), linit8);
2011 printf("sinit12=%s\n", sinit12);
2012 printf("sinit13=%d %s %s %s\n",
2013 sizeof(sinit13),
2014 sinit13[0],
2015 sinit13[1],
2016 sinit13[2]);
2017 printf("sinit14=%s\n", sinit14);
2019 for(i=0;i<10;i++) printf(" %d", linit12[i]);
2020 printf("\n");
2021 for(i=0;i<10;i++) printf(" %d", linit13[i]);
2022 printf("\n");
2023 for(i=0;i<10;i++) printf(" %d", linit14[i]);
2024 printf("\n");
2025 for(i=0;i<10;i++) printf(" %d", linit15[i]);
2026 printf("\n");
2027 printf("%d %d %d %d\n",
2028 linit16.a1,
2029 linit16.a2,
2030 linit16.a3,
2031 linit16.a4);
2032 /* test that initialisation is done after variable declare */
2033 printf("linit17=%d\n", linit17);
2034 printf("sinit15=%d\n", sinit15[0]);
2035 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
2036 printf("sinit17=%s %d %s %d\n",
2037 sinit17[0].s, sinit17[0].len,
2038 sinit17[1].s, sinit17[1].len);
2039 for(i=0;i<10;i++)
2040 printf("%x ", sinit18[i]);
2041 printf("\n");
2042 /* complex init check */
2043 printf("cix: %d %d %d %d %d %d %d\n",
2044 cix[0].a,
2045 cix[0].b[0].a, cix[0].b[0].b,
2046 cix[0].b[1].a, cix[0].b[1].b,
2047 cix[0].b[2].a, cix[0].b[2].b);
2048 printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
2049 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
2051 printf("arrtype1: %d %d %d\n", sinit19[0], sinit20[0], sinit20[1]);
2052 printf("arrtype2: %d %d\n", sizeof(sinit19), sizeof(sinit20));
2053 printf("arrtype3: %d %d %d\n", sinit21[0], sinit21[1], sinit21[2]);
2054 printf("arrtype4: %d %d %d\n", sinit22[0], sinit22[1], sinit22[2]);
2055 printf("arrtype5: %d %d\n", sizeof(sinit21), sizeof(sinit22));
2056 printf("arrtype6: %d\n", sizeof(arrtype2));
2058 printf("sinit23= %d %d\n", sinit23[0], sinit23[1]);
2059 printf("sinit24=%d\n", sinit24);
2060 printf("linit18= %d %d\n", linit18[0], linit18[1]);
2063 void switch_uc(unsigned char uc)
2065 switch (uc) {
2066 case 0xfb ... 0xfe:
2067 printf("ucsw:1\n");
2068 break;
2069 case 0xff:
2070 printf("ucsw:2\n");
2071 break;
2072 case 0 ... 5:
2073 printf("ucsw:3\n");
2074 break;
2075 default:
2076 printf("ucsw: broken!\n");
2080 void switch_sc(signed char sc)
2082 switch (sc) {
2083 case -5 ... -2:
2084 printf("scsw:1\n");
2085 break;
2086 case -1:
2087 printf("scsw:2\n");
2088 break;
2089 case 0 ... 5:
2090 printf("scsw:3\n");
2091 break;
2092 default:
2093 printf("scsw: broken!\n");
2097 void switch_test()
2099 int i;
2100 unsigned long long ull;
2101 long long ll;
2103 for(i=0;i<15;i++) {
2104 switch(i) {
2105 case 0:
2106 case 1:
2107 printf("a");
2108 break;
2109 default:
2110 printf("%d", i);
2111 break;
2112 case 8 ... 12:
2113 printf("c");
2114 break;
2115 case 3:
2116 printf("b");
2117 break;
2118 case 0xc33c6b9fU:
2119 case 0x7c9eeeb9U:
2120 break;
2123 printf("\n");
2125 for (i = 1; i <= 5; i++) {
2126 ull = (unsigned long long)i << 61;
2127 switch (ull) {
2128 case 1ULL << 61:
2129 printf("ullsw:1\n");
2130 break;
2131 case 2ULL << 61:
2132 printf("ullsw:2\n");
2133 break;
2134 case 3ULL << 61:
2135 printf("ullsw:3\n");
2136 break;
2137 case 4ULL << 61:
2138 printf("ullsw:4\n");
2139 break;
2140 case 5ULL << 61:
2141 printf("ullsw:5\n");
2142 break;
2143 default:
2144 printf("ullsw: broken!\n");
2148 for (i = 1; i <= 5; i++) {
2149 ll = (long long)i << 61;
2150 switch (ll) {
2151 case 1LL << 61:
2152 printf("llsw:1\n");
2153 break;
2154 case 2LL << 61:
2155 printf("llsw:2\n");
2156 break;
2157 case 3LL << 61:
2158 printf("llsw:3\n");
2159 break;
2160 case 4LL << 61:
2161 printf("llsw:4\n");
2162 break;
2163 case 5LL << 61:
2164 printf("llsw:5\n");
2165 break;
2166 default:
2167 printf("llsw: broken!\n");
2171 for (i = -5; i <= 5; i++) {
2172 switch_uc((unsigned char)i);
2175 for (i = -5; i <= 5; i++) {
2176 switch_sc ((signed char)i);
2180 /* ISOC99 _Bool type */
2181 void c99_bool_test(void)
2183 #ifdef BOOL_ISOC99
2184 int a;
2185 _Bool b, b2;
2187 printf("bool_test:\n");
2188 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
2189 a = 3;
2190 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
2191 b = 3;
2192 printf("b = %d\n", b);
2193 b++;
2194 printf("b = %d\n", b);
2195 b2 = 0;
2196 printf("sizeof(x ? _Bool : _Bool) = %d (should be sizeof int)\n",
2197 sizeof((volatile)a ? b : b2));
2198 #endif
2201 void bitfield_test(void)
2203 int a;
2204 short sa;
2205 unsigned char ca;
2206 struct sbf1 {
2207 int f1 : 3;
2208 int : 2;
2209 int f2 : 1;
2210 int : 0;
2211 int f3 : 5;
2212 int f4 : 7;
2213 unsigned int f5 : 7;
2214 } st1;
2215 printf("bitfield_test:");
2216 printf("sizeof(st1) = %d\n", sizeof(st1));
2218 st1.f1 = 3;
2219 st1.f2 = 1;
2220 st1.f3 = 15;
2221 a = 120;
2222 st1.f4 = a;
2223 st1.f5 = a;
2224 st1.f5++;
2225 printf("%d %d %d %d %d\n",
2226 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
2227 sa = st1.f5;
2228 ca = st1.f5;
2229 printf("%d %d\n", sa, ca);
2231 st1.f1 = 7;
2232 if (st1.f1 == -1)
2233 printf("st1.f1 == -1\n");
2234 else
2235 printf("st1.f1 != -1\n");
2236 if (st1.f2 == -1)
2237 printf("st1.f2 == -1\n");
2238 else
2239 printf("st1.f2 != -1\n");
2241 struct sbf2 {
2242 long long f1 : 45;
2243 long long : 2;
2244 long long f2 : 35;
2245 unsigned long long f3 : 38;
2246 } st2;
2247 st2.f1 = 0x123456789ULL;
2248 a = 120;
2249 st2.f2 = (long long)a << 25;
2250 st2.f3 = a;
2251 st2.f2++;
2252 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
2254 #if 0
2255 Disabled for now until further clarification re GCC compatibility
2256 struct sbf3 {
2257 int f1 : 7;
2258 int f2 : 1;
2259 char f3;
2260 int f4 : 8;
2261 int f5 : 1;
2262 int f6 : 16;
2263 } st3;
2264 printf("sizeof(st3) = %d\n", sizeof(st3));
2265 #endif
2267 struct sbf4 {
2268 int x : 31;
2269 char y : 2;
2270 } st4;
2271 st4.y = 1;
2272 printf("st4.y == %d\n", st4.y);
2273 struct sbf5 {
2274 int a;
2275 char b;
2276 int x : 12, y : 4, : 0, : 4, z : 3;
2277 char c;
2278 } st5 = { 1, 2, 3, 4, -3, 6 };
2279 printf("st5 = %d %d %d %d %d %d\n", st5.a, st5.b, st5.x, st5.y, st5.z, st5.c);
2280 struct sbf6 {
2281 short x : 12;
2282 unsigned char y : 2;
2283 } st6;
2284 st6.y = 1;
2285 printf("st6.y == %d\n", st6.y);
2288 #ifdef __x86_64__
2289 #define FLOAT_FMT "%f\n"
2290 #else
2291 /* x86's float isn't compatible with GCC */
2292 #define FLOAT_FMT "%.5f\n"
2293 #endif
2295 /* declare strto* functions as they are C99 */
2296 double strtod(const char *nptr, char **endptr);
2298 #if defined(_WIN32)
2299 float strtof(const char *nptr, char **endptr) {return (float)strtod(nptr, endptr);}
2300 LONG_DOUBLE strtold(const char *nptr, char **endptr) {return (LONG_DOUBLE)strtod(nptr, endptr);}
2301 #else
2302 float strtof(const char *nptr, char **endptr);
2303 LONG_DOUBLE strtold(const char *nptr, char **endptr);
2304 #endif
2306 #define FTEST(prefix, typename, type, fmt)\
2307 void prefix ## cmp(type a, type b)\
2309 printf("%d %d %d %d %d %d\n",\
2310 a == b,\
2311 a != b,\
2312 a < b,\
2313 a > b,\
2314 a >= b,\
2315 a <= b);\
2316 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
2319 a + b,\
2320 a - b,\
2321 a * b,\
2322 a / b,\
2323 -a);\
2324 printf(fmt "\n", ++a);\
2325 printf(fmt "\n", a++);\
2326 printf(fmt "\n", a);\
2327 b = 0;\
2328 printf("%d %d\n", !a, !b);\
2330 void prefix ## fcast(type a)\
2332 float fa;\
2333 double da;\
2334 LONG_DOUBLE la;\
2335 int ia;\
2336 long long llia;\
2337 unsigned int ua;\
2338 unsigned long long llua;\
2339 type b;\
2340 fa = a;\
2341 da = a;\
2342 la = a;\
2343 printf("ftof: %f %f %Lf\n", fa, da, la);\
2344 ia = (int)a;\
2345 llia = (long long)a;\
2346 a = (a >= 0) ? a : -a;\
2347 ua = (unsigned int)a;\
2348 llua = (unsigned long long)a;\
2349 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
2350 ia = -1234;\
2351 ua = 0x81234500;\
2352 llia = -0x123456789012345LL;\
2353 llua = 0xf123456789012345LLU;\
2354 b = ia;\
2355 printf("itof: " fmt "\n", b);\
2356 b = ua;\
2357 printf("utof: " fmt "\n", b);\
2358 b = llia;\
2359 printf("lltof: " fmt "\n", b);\
2360 b = llua;\
2361 printf("ulltof: " fmt "\n", b);\
2364 float prefix ## retf(type a) { return a; }\
2365 double prefix ## retd(type a) { return a; }\
2366 LONG_DOUBLE prefix ## retld(type a) { return a; }\
2368 void prefix ## call(void)\
2370 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
2371 printf("double: %f\n", prefix ## retd(42.123456789));\
2372 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
2373 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
2376 void prefix ## signed_zeros(void) \
2378 type x = 0.0, y = -0.0, n, p;\
2379 if (x == y)\
2380 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
2381 1.0 / x != 1.0 / y);\
2382 else\
2383 printf ("x != y; this is wrong!\n");\
2385 n = -x;\
2386 if (x == n)\
2387 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
2388 1.0 / x != 1.0 / n);\
2389 else\
2390 printf ("x != -x; this is wrong!\n");\
2392 p = +y;\
2393 if (x == p)\
2394 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
2395 1.0 / x != 1.0 / p);\
2396 else\
2397 printf ("x != +y; this is wrong!\n");\
2398 p = -y;\
2399 if (x == p)\
2400 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
2401 1.0 / x != 1.0 / p);\
2402 else\
2403 printf ("x != -y; this is wrong!\n");\
2405 void prefix ## test(void)\
2407 printf("testing '%s'\n", #typename);\
2408 prefix ## cmp(1, 2.5);\
2409 prefix ## cmp(2, 1.5);\
2410 prefix ## cmp(1, 1);\
2411 prefix ## fcast(234.6);\
2412 prefix ## fcast(-2334.6);\
2413 prefix ## call();\
2414 prefix ## signed_zeros();\
2417 FTEST(f, float, float, "%f")
2418 FTEST(d, double, double, "%f")
2419 FTEST(ld, long double, LONG_DOUBLE, "%Lf")
2421 double ftab1[3] = { 1.2, 3.4, -5.6 };
2424 void float_test(void)
2426 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
2427 float fa, fb;
2428 double da, db;
2429 int a;
2430 unsigned int b;
2431 static double nan2 = 0.0/0.0;
2432 static double inf1 = 1.0/0.0;
2433 static double inf2 = 1e5000;
2435 printf("float_test:\n");
2436 printf("sizeof(float) = %d\n", sizeof(float));
2437 printf("sizeof(double) = %d\n", sizeof(double));
2438 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));
2439 ftest();
2440 dtest();
2441 ldtest();
2442 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
2443 printf("%f %f %f\n", 2.12, .5, 2.3e10);
2444 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
2445 da = 123;
2446 printf("da=%f\n", da);
2447 fa = 123;
2448 printf("fa=%f\n", fa);
2449 a = 4000000000;
2450 da = a;
2451 printf("da = %f\n", da);
2452 b = 4000000000;
2453 db = b;
2454 printf("db = %f\n", db);
2455 printf("nan != nan = %d, inf1 = %f, inf2 = %f\n", nan2 != nan2, inf1, inf2);
2456 #endif
2459 int fib(int n)
2461 if (n <= 2)
2462 return 1;
2463 else
2464 return fib(n-1) + fib(n-2);
2467 #if __GNUC__ == 3
2468 # define aligned_function 0
2469 #else
2470 void __attribute__((aligned(16))) aligned_function(int i) {}
2471 #endif
2473 void funcptr_test()
2475 void (*func)(int);
2476 int a;
2477 struct {
2478 int dummy;
2479 void (*func)(int);
2480 } st1;
2481 long diff;
2483 printf("funcptr:\n");
2484 func = &num;
2485 (*func)(12345);
2486 func = num;
2487 a = 1;
2488 a = 1;
2489 func(12345);
2490 /* more complicated pointer computation */
2491 st1.func = num;
2492 st1.func(12346);
2493 printf("sizeof1 = %d\n", sizeof(funcptr_test));
2494 printf("sizeof2 = %d\n", sizeof funcptr_test);
2495 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
2496 printf("sizeof4 = %d\n", sizeof &funcptr_test);
2497 a = 0;
2498 func = num + a;
2499 diff = func - num;
2500 func(42);
2501 (func + diff)(42);
2502 (num + a)(43);
2504 /* Check that we can align functions */
2505 func = aligned_function;
2506 printf("aligned_function (should be zero): %d\n", ((int)(uintptr_t)func) & 15);
2509 void lloptest(long long a, long long b)
2511 unsigned long long ua, ub;
2513 ua = a;
2514 ub = b;
2515 /* arith */
2516 printf("arith: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2517 a + b,
2518 a - b,
2519 a * b);
2521 if (b != 0) {
2522 printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2523 a / b,
2524 a % b);
2527 /* binary */
2528 printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2529 a & b,
2530 a | b,
2531 a ^ b);
2533 /* tests */
2534 printf("test: %d %d %d %d %d %d\n",
2535 a == b,
2536 a != b,
2537 a < b,
2538 a > b,
2539 a >= b,
2540 a <= b);
2542 printf("utest: %d %d %d %d %d %d\n",
2543 ua == ub,
2544 ua != ub,
2545 ua < ub,
2546 ua > ub,
2547 ua >= ub,
2548 ua <= ub);
2550 /* arith2 */
2551 a++;
2552 b++;
2553 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2554 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a++, b++);
2555 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", --a, --b);
2556 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2557 b = ub = 0;
2558 printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
2561 void llshift(long long a, int b)
2563 printf("shift: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2564 (unsigned long long)a >> b,
2565 a >> b,
2566 a << b);
2567 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2568 (unsigned long long)a >> 3,
2569 a >> 3,
2570 a << 3);
2571 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2572 (unsigned long long)a >> 35,
2573 a >> 35,
2574 a << 35);
2577 void llfloat(void)
2579 float fa;
2580 double da;
2581 LONG_DOUBLE lda;
2582 long long la, lb, lc;
2583 unsigned long long ula, ulb, ulc;
2584 la = 0x12345678;
2585 ula = 0x72345678;
2586 la = (la << 20) | 0x12345;
2587 ula = ula << 33;
2588 printf("la=" LONG_LONG_FORMAT " ula=" ULONG_LONG_FORMAT "\n", la, ula);
2590 fa = la;
2591 da = la;
2592 lda = la;
2593 printf("lltof: %f %f %Lf\n", fa, da, lda);
2595 la = fa;
2596 lb = da;
2597 lc = lda;
2598 printf("ftoll: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", la, lb, lc);
2600 fa = ula;
2601 da = ula;
2602 lda = ula;
2603 printf("ulltof: %f %f %Lf\n", fa, da, lda);
2605 ula = fa;
2606 ulb = da;
2607 ulc = lda;
2608 printf("ftoull: " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT "\n", ula, ulb, ulc);
2611 long long llfunc1(int a)
2613 return a * 2;
2616 struct S {
2617 int id;
2618 char item;
2621 long long int value(struct S *v)
2623 return ((long long int)v->item);
2626 long long llfunc2(long long x, long long y, int z)
2628 return x * y * z;
2631 void check_opl_save_regs(char *a, long long b, int c)
2633 *a = b < 0 && !c;
2636 void longlong_test(void)
2638 long long a, b, c;
2639 int ia;
2640 unsigned int ua;
2641 printf("longlong_test:\n");
2642 printf("sizeof(long long) = %d\n", sizeof(long long));
2643 ia = -1;
2644 ua = -2;
2645 a = ia;
2646 b = ua;
2647 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2648 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %Lx\n",
2649 (long long)1,
2650 (long long)-2,
2651 1LL,
2652 0x1234567812345679);
2653 a = llfunc1(-3);
2654 printf(LONG_LONG_FORMAT "\n", a);
2656 lloptest(1000, 23);
2657 lloptest(0xff, 0x1234);
2658 b = 0x72345678 << 10;
2659 lloptest(-3, b);
2660 llshift(0x123, 5);
2661 llshift(-23, 5);
2662 b = 0x72345678LL << 10;
2663 llshift(b, 47);
2665 llfloat();
2666 #if 1
2667 b = 0x12345678;
2668 a = -1;
2669 c = a + b;
2670 printf("%Lx\n", c);
2671 #endif
2673 /* long long reg spill test */
2675 struct S a;
2677 a.item = 3;
2678 printf("%lld\n", value(&a));
2680 lloptest(0x80000000, 0);
2683 long long *p, v, **pp;
2684 v = 1;
2685 p = &v;
2686 p[0]++;
2687 printf("another long long spill test : %lld\n", *p);
2688 pp = &p;
2690 v = llfunc2(**pp, **pp, ia);
2691 printf("a long long function (arm-)reg-args test : %lld\n", v);
2693 a = 68719476720LL;
2694 b = 4294967295LL;
2695 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
2697 printf(LONG_LONG_FORMAT "\n", 0x123456789LLU);
2699 /* long long pointer deref in argument passing test */
2700 a = 0x123;
2701 long long *p = &a;
2702 llshift(*p, 5);
2704 /* shortening followed by widening */
2705 unsigned long long u = 0x8000000000000001ULL;
2706 u = (unsigned)(u + 1);
2707 printf("long long u=" ULONG_LONG_FORMAT "\n", u);
2708 u = 0x11223344aa998877ULL;
2709 u = (unsigned)(int)(u + 1);
2710 printf("long long u=" ULONG_LONG_FORMAT "\n", u);
2712 /* was a problem with missing save_regs in gen_opl on 32-bit platforms */
2713 char cc = 78;
2714 check_opl_save_regs(&cc, -1, 0);
2715 printf("check_opl_save_regs: %d\n", cc);
2718 void manyarg_test(void)
2720 LONG_DOUBLE ld = 1234567891234LL;
2721 printf("manyarg_test:\n");
2722 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2723 1, 2, 3, 4, 5, 6, 7, 8,
2724 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2725 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2726 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2727 1, 2, 3, 4, 5, 6, 7, 8,
2728 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2729 1234567891234LL, 987654321986LL,
2730 42.0, 43.0);
2731 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2732 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2733 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2734 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2735 1234567891234LL, 987654321986LL,
2736 42.0, 43.0);
2737 printf("%d %d %d %d %d %d %d %d %Lf\n",
2738 1, 2, 3, 4, 5, 6, 7, 8, ld);
2739 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2740 LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n",
2741 1, 2, 3, 4, 5, 6, 7, 8,
2742 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2743 1234567891234LL, 987654321986LL,
2744 42.0, 43.0, ld);
2745 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2746 "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n",
2747 1, 2, 3, 4, 5, 6, 7, 8,
2748 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2749 ld, 1234567891234LL, 987654321986LL,
2750 42.0, 43.0, ld);
2753 void*
2754 va_arg_with_struct_ptr(va_list ap) {
2756 * This was a BUG identified with FFTW-3.3.8 on arm64.
2757 * The test case only checks it compiles on all supported
2758 * architectures. This function is not currently called.
2760 struct X { int _x; };
2761 struct X *x = va_arg(ap, struct X *);
2762 return x;
2765 void vprintf1(const char *fmt, ...)
2767 va_list ap, aq;
2768 const char *p;
2769 int c, i;
2770 double d;
2771 long long ll;
2772 LONG_DOUBLE ld;
2774 va_start(aq, fmt);
2775 va_copy(ap, aq);
2777 p = fmt;
2778 for(;;) {
2779 c = *p;
2780 if (c == '\0')
2781 break;
2782 p++;
2783 if (c == '%') {
2784 c = *p;
2785 switch(c) {
2786 case '\0':
2787 goto the_end;
2788 case 'd':
2789 i = va_arg(ap, int);
2790 printf("%d", i);
2791 break;
2792 case 'f':
2793 d = va_arg(ap, double);
2794 printf("%f", d);
2795 break;
2796 case 'l':
2797 ll = va_arg(ap, long long);
2798 printf(LONG_LONG_FORMAT, ll);
2799 break;
2800 case 'F':
2801 ld = va_arg(ap, LONG_DOUBLE);
2802 printf("%Lf", ld);
2803 break;
2805 p++;
2806 } else {
2807 putchar(c);
2810 the_end:
2811 va_end(aq);
2812 va_end(ap);
2815 struct myspace {
2816 short int profile;
2819 void stdarg_for_struct(struct myspace bob, ...)
2821 struct myspace george, bill;
2822 va_list ap;
2823 short int validate;
2825 va_start(ap, bob);
2826 bill = va_arg(ap, struct myspace);
2827 george = va_arg(ap, struct myspace);
2828 validate = va_arg(ap, int);
2829 printf("stdarg_for_struct: %d %d %d %d\n",
2830 bob.profile, bill.profile, george.profile, validate);
2831 va_end(ap);
2834 void stdarg_for_libc(const char *fmt, ...)
2836 va_list args;
2837 va_start(args, fmt);
2838 vprintf(fmt, args);
2839 va_end(args);
2842 void stdarg_syntax(int n, ...)
2844 int i;
2845 va_list ap;
2846 if (1)
2847 va_start(ap, n);
2848 else
2850 i = va_arg(ap, int);
2851 printf("stdarg_void_expr: %d\n", i);
2852 (va_end(ap));
2855 void stdarg_test(void)
2857 LONG_DOUBLE ld = 1234567891234LL;
2858 struct myspace bob;
2860 vprintf1("%d %d %d\n", 1, 2, 3);
2861 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2862 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2863 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2864 vprintf1("%d %f %l %F %d %f %l %F\n",
2865 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2866 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2867 1, 2, 3, 4, 5, 6, 7, 8,
2868 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2869 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2870 1, 2, 3, 4, 5, 6, 7, 8,
2871 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2872 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2873 "%l %l %f %f\n",
2874 1, 2, 3, 4, 5, 6, 7, 8,
2875 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2876 1234567891234LL, 987654321986LL,
2877 42.0, 43.0);
2878 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2879 "%l %l %f %f\n",
2880 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2881 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2882 1234567891234LL, 987654321986LL,
2883 42.0, 43.0);
2884 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2885 1, 2, 3, 4, 5, 6, 7, 8, ld);
2886 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2887 "%l %l %f %f %F\n",
2888 1, 2, 3, 4, 5, 6, 7, 8,
2889 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2890 1234567891234LL, 987654321986LL,
2891 42.0, 43.0, ld);
2892 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2893 "%F %l %l %f %f %F\n",
2894 1, 2, 3, 4, 5, 6, 7, 8,
2895 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2896 ld, 1234567891234LL, 987654321986LL,
2897 42.0, 43.0, ld);
2899 bob.profile = 42;
2900 stdarg_for_struct(bob, bob, bob, bob.profile);
2901 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2902 stdarg_syntax(1, 17);
2905 void whitespace_test(void)
2907 char *str;
2909 \f\v #if 1
2910 pri\
2911 ntf("whitspace:\n");\f\v
2912 #endif
2913 pf("N=%d\n", 2);
2915 #ifdef CORRECT_CR_HANDLING
2916 pri\
2917 ntf("aaa=%d\n", 3);
2918 #endif
2920 pri\
2922 ntf("min=%d\n", 4);
2924 #ifdef ACCEPT_CR_IN_STRINGS
2925 printf("len1=%d\n", strlen("
2926 "));
2927 #ifdef CORRECT_CR_HANDLING
2928 str = "
2930 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
2931 #endif
2932 printf("len1=%d\n", strlen(" a
2933 "));
2934 #endif /* ACCEPT_CR_IN_STRINGS */
2937 int reltab[3] = { 1, 2, 3 };
2939 int *rel1 = &reltab[1];
2940 int *rel2 = &reltab[2];
2942 #ifdef _WIN64
2943 void relocation_test(void) {}
2944 #else
2945 void getmyaddress(void)
2947 printf("in getmyaddress\n");
2950 #ifdef __LP64__
2951 long __pa_symbol(void)
2953 /* This 64bit constant was handled incorrectly, it was used as addend
2954 (which can hold 64bit just fine) in connection with a symbol,
2955 and TCC generates wrong code for that (displacements are 32bit only).
2956 This effectively is "+ 0x80000000", and if addresses of globals
2957 are below 2GB the result should be a number without high 32 bits set. */
2958 return ((long)(((unsigned long)(&rel1))) - (0xffffffff80000000UL));
2960 #endif
2962 unsigned long theaddress = (unsigned long)getmyaddress;
2963 void relocation_test(void)
2965 void (*fptr)(void) = (void (*)(void))theaddress;
2966 printf("*rel1=%d\n", *rel1);
2967 printf("*rel2=%d\n", *rel2);
2968 fptr();
2969 #ifdef __LP64__
2970 printf("pa_symbol=0x%lx\n", __pa_symbol() >> 63);
2971 #endif
2973 #endif
2975 void old_style_f(a,b,c)
2976 int a, b;
2977 double c;
2979 printf("a=%d b=%d b=%f\n", a, b, c);
2982 void decl_func1(int cmpfn())
2984 printf("cmpfn=%lx\n", (long)cmpfn);
2987 void decl_func2(cmpfn)
2988 int cmpfn();
2990 printf("cmpfn=%lx\n", (long)cmpfn);
2993 void old_style_function(void)
2995 old_style_f((void *)1, 2, 3.0);
2996 decl_func1(NULL);
2997 decl_func2(NULL);
3000 void alloca_test()
3002 #if defined __i386__ || defined __x86_64__ || defined __arm__
3003 char *p = alloca(16);
3004 strcpy(p,"123456789012345");
3005 printf("alloca: p is %s\n", p);
3006 char *demo = "This is only a test.\n";
3007 /* Test alloca embedded in a larger expression */
3008 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
3009 #endif
3012 void *bounds_checking_is_enabled()
3014 char ca[10], *cp = ca-1;
3015 return (ca != cp + 1) ? cp : NULL;
3018 typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
3020 void c99_vla_test(int size1, int size2)
3022 #if defined __i386__ || defined __x86_64__
3023 int size = size1 * size2;
3024 int tab1[size][2], tab2[10][2];
3025 void *tab1_ptr, *tab2_ptr, *bad_ptr;
3027 /* "size" should have been 'captured' at tab1 declaration,
3028 so modifying it should have no effect on VLA behaviour. */
3029 size = size-1;
3031 printf("Test C99 VLA 1 (sizeof): ");
3032 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
3033 tab1_ptr = tab1;
3034 tab2_ptr = tab2;
3035 printf("Test C99 VLA 2 (ptrs subtract): ");
3036 printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
3037 printf("Test C99 VLA 3 (ptr add): ");
3038 printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
3039 printf("Test C99 VLA 4 (ptr access): ");
3040 tab1[size1][1] = 42;
3041 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
3043 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
3044 if (bad_ptr = bounds_checking_is_enabled()) {
3045 int *t1 = &tab1[size1 * size2 - 1][3];
3046 int *t2 = &tab2[9][3];
3047 printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
3048 printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
3050 char*c1 = 1 + sizeof(tab1) + (char*)tab1;
3051 char*c2 = 1 + sizeof(tab2) + (char*)tab2;
3052 printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
3053 printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
3055 int *i1 = tab1[-1];
3056 int *i2 = tab2[-1];
3057 printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
3058 printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
3060 int *x1 = tab1[size1 * size2 + 1];
3061 int *x2 = tab2[10 + 1];
3062 printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
3063 printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
3064 } else {
3065 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
3067 printf("\n");
3068 #endif
3071 void sizeof_test(void)
3073 int a;
3074 int **ptr;
3076 printf("sizeof(int) = %d\n", sizeof(int));
3077 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
3078 printf("sizeof(long) = %d\n", sizeof(long));
3079 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
3080 printf("sizeof(short) = %d\n", sizeof(short));
3081 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
3082 printf("sizeof(char) = %d\n", sizeof(char));
3083 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
3084 printf("sizeof(func) = %d\n", sizeof sizeof_test());
3085 a = 1;
3086 printf("sizeof(a++) = %d\n", sizeof a++);
3087 printf("a=%d\n", a);
3088 ptr = NULL;
3089 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
3091 /* The type of sizeof should be as large as a pointer, actually
3092 it should be size_t. */
3093 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
3094 uintptr_t t = 1;
3095 uintptr_t t2;
3096 /* Effectively <<32, but defined also on 32bit machines. */
3097 t <<= 16;
3098 t <<= 16;
3099 t++;
3100 /* This checks that sizeof really can be used to manipulate
3101 uintptr_t objects, without truncation. */
3102 t2 = t & -sizeof(uintptr_t);
3103 printf ("%lu %lu\n", t, t2);
3105 /* some alignof tests */
3106 printf("__alignof__(int) = %d\n", __alignof__(int));
3107 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
3108 printf("__alignof__(short) = %d\n", __alignof__(short));
3109 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
3110 printf("__alignof__(char) = %d\n", __alignof__(char));
3111 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
3112 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
3114 /* sizes of VLAs need to be evaluated even inside sizeof: */
3115 a = 2;
3116 printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a]));
3117 /* And checking if sizeof compound literal works. Parenthesized: */
3118 printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n",
3119 sizeof( (struct {int i; int j;}){4,5} ));
3120 /* And as direct sizeof argument (as unary expression): */
3121 printf("sizeof (struct {short i; short j;}){4,5} = %d\n",
3122 sizeof (struct {short i; short j;}){4,5} );
3124 /* sizeof(x && y) should be sizeof(int), even if constant
3125 evaluating is possible. */
3126 printf("sizeof(t && 0) = %d\n", sizeof(t && 0));
3127 printf("sizeof(1 && 1) = %d\n", sizeof(1 && 1));
3128 printf("sizeof(t || 1) = %d\n", sizeof(t || 1));
3129 printf("sizeof(0 || 0) = %d\n", sizeof(0 || 0));
3132 void typeof_test(void)
3134 double a;
3135 typeof(a) b;
3136 typeof(float) c;
3138 a = 1.5;
3139 b = 2.5;
3140 c = 3.5;
3141 printf("a=%f b=%f c=%f\n", a, b, c);
3145 struct hlist_node;
3146 struct hlist_head {
3147 struct hlist_node *first, *last;
3150 void consume_ulong (unsigned long i)
3152 i = 0;
3155 void statement_expr_test(void)
3157 int a, i;
3159 /* Basic stmt expr test */
3160 a = 0;
3161 for(i=0;i<10;i++) {
3162 a += 1 +
3163 ( { int b, j;
3164 b = 0;
3165 for(j=0;j<5;j++)
3166 b += j; b;
3167 } );
3169 printf("a=%d\n", a);
3171 /* Test that symbols aren't freed prematurely.
3172 With SYM_DEBUG valgrind will show a read from a freed
3173 symbol, and tcc will show an (invalid) warning on the initialization
3174 of 'ptr' below, if symbols are popped after the stmt expr. */
3175 void *v = (void*)39;
3176 typeof(({
3177 (struct hlist_node *)v;
3178 })) x;
3179 typeof (x)
3180 ptr = (struct hlist_node *)v;
3182 /* This part used to segfault when symbols were popped prematurely.
3183 The symbols for the static local would be overwritten with
3184 helper symbols from the pre-processor expansions in between. */
3185 #define some_attr __attribute__((aligned(1)))
3186 #define tps(str) ({ \
3187 static const char *t some_attr = str; \
3188 t; \
3190 printf ("stmtexpr: %s %s\n",
3191 tps("somerandomlongstring"),
3192 tps("anotherlongstring"));
3194 /* Test that the three decls of 't' don't interact. */
3195 int t = 40;
3196 int b = ({ int t = 41; t; });
3197 int c = ({ int t = 42; t; });
3199 /* Test that aggregate return values work. */
3200 struct hlist_head h
3201 = ({
3202 typedef struct hlist_head T;
3203 long pre = 48;
3204 T t = { (void*)43, (void*)44 };
3205 long post = 49;
3208 printf ("stmtexpr: %d %d %d\n", t, b, c);
3209 printf ("stmtexpr: %ld %ld\n", (long)h.first, (long)h.last);
3211 /* Test that we can give out addresses of local labels. */
3212 consume_ulong(({ __label__ __here; __here: (unsigned long)&&__here; }));
3214 /* Test interaction between local and global label stacks and the
3215 need to defer popping symbol from them when within statement
3216 expressions. Note how the labels are both named LBL. */
3217 i = 0;
3220 __label__ LBL;
3221 LBL: if (i++ == 0) goto LBL;
3223 /* jump to a classical label out of an expr-stmt that had previously
3224 overshadowed that classical label */
3225 goto LBL;
3227 LBL:
3228 printf("stmtexpr: %d should be 2\n", i);
3231 void local_label_test(void)
3233 int a;
3234 goto l1;
3236 a = 1 + ({
3237 __label__ l1, l2, l3, l4;
3238 goto l1;
3240 printf("aa1\n");
3241 goto l3;
3243 printf("aa3\n");
3244 goto l4;
3246 printf("aa2\n");
3247 goto l2;
3248 l3:;
3251 printf("a=%d\n", a);
3252 return;
3254 printf("bb1\n");
3255 goto l2;
3257 printf("bb2\n");
3258 goto l4;
3261 /* inline assembler test */
3262 #if defined(__i386__) || defined(__x86_64__)
3264 /* from linux kernel */
3265 static char * strncat1(char * dest,const char * src,size_t count)
3267 long d0, d1, d2, d3;
3268 __asm__ __volatile__(
3269 "repne\n\t"
3270 "scasb\n\t"
3271 "dec %1\n\t"
3272 "mov %8,%3\n"
3273 "1:\tdec %3\n\t"
3274 "js 2f\n\t"
3275 "lodsb\n\t"
3276 "stosb\n\t"
3277 "testb %%al,%%al\n\t"
3278 "jne 1b\n"
3279 "2:\txor %2,%2\n\t"
3280 "stosb"
3281 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
3282 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
3283 : "memory");
3284 return dest;
3287 static char * strncat2(char * dest,const char * src,size_t count)
3289 long d0, d1, d2, d3;
3290 __asm__ __volatile__(
3291 "repne scasb\n\t" /* one-line repne prefix + string op */
3292 "dec %1\n\t"
3293 "mov %8,%3\n"
3294 "1:\tdec %3\n\t"
3295 "js 2f\n\t"
3296 "lodsb\n\t"
3297 "stosb\n\t"
3298 "testb %%al,%%al\n\t"
3299 "jne 1b\n"
3300 "2:\txor %2,%2\n\t"
3301 "stosb"
3302 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
3303 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
3304 : "memory");
3305 return dest;
3308 static inline void * memcpy1(void * to, const void * from, size_t n)
3310 long d0, d1, d2;
3311 __asm__ __volatile__(
3312 "rep ; movsl\n\t"
3313 "testb $2,%b4\n\t"
3314 "je 1f\n\t"
3315 "movsw\n"
3316 "1:\ttestb $1,%b4\n\t"
3317 "je 2f\n\t"
3318 "movsb\n"
3319 "2:"
3320 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
3321 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
3322 : "memory");
3323 return (to);
3326 static inline void * memcpy2(void * to, const void * from, size_t n)
3328 long d0, d1, d2;
3329 __asm__ __volatile__(
3330 "rep movsl\n\t" /* one-line rep prefix + string op */
3331 "testb $2,%b4\n\t"
3332 "je 1f\n\t"
3333 "movsw\n"
3334 "1:\ttestb $1,%b4\n\t"
3335 "je 2f\n\t"
3336 "movsb\n"
3337 "2:"
3338 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
3339 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
3340 : "memory");
3341 return (to);
3344 static __inline__ void sigaddset1(unsigned int *set, int _sig)
3346 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
3349 static __inline__ void sigdelset1(unsigned int *set, int _sig)
3351 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc", "flags");
3354 #ifndef __APPLE__
3355 /* clang's inline asm is uncapable of 'xchgb %b0,%h0' */
3356 static __inline__ __const__ unsigned int swab32(unsigned int x)
3358 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
3359 "rorl $16,%0\n\t" /* swap words */
3360 "xchgb %b0,%h0" /* swap higher bytes */
3361 :"=" "q" (x)
3362 : "0" (x));
3363 return x;
3365 #endif
3367 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
3369 unsigned long long res;
3370 #ifdef __x86_64__
3371 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
3372 but still test the 32bit->64bit mull. */
3373 unsigned int resh, resl;
3374 __asm__("mull %2" : "=a" (resl), "=d" (resh) : "a" (a), "r" (b));
3375 res = ((unsigned long long)resh << 32) | resl;
3376 #else
3377 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
3378 #endif
3379 return res;
3382 static __inline__ unsigned long long inc64(unsigned long long a)
3384 unsigned long long res;
3385 #ifdef __x86_64__
3386 /* Using the A constraint is wrong, and increments are tested
3387 elsewhere. */
3388 res = a + 1;
3389 #else
3390 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
3391 #endif
3392 return res;
3395 struct struct123 {
3396 int a;
3397 int b;
3399 struct struct1231 {
3400 unsigned long addr;
3403 unsigned long mconstraint_test(struct struct1231 *r)
3405 unsigned long ret;
3406 unsigned int a[2];
3407 a[0] = 0;
3408 __asm__ volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
3409 : "=&r" (ret), "=m" (a)
3410 : "m" (*(struct struct123 *)r->addr));
3411 return ret + a[0];
3414 #ifdef __x86_64__
3415 int fls64(unsigned long long x)
3417 int bitpos = -1;
3418 asm("bsrq %1,%q0"
3419 : "+r" (bitpos)
3420 : "rm" (x));
3421 return bitpos + 1;
3423 #endif
3425 void other_constraints_test(void)
3427 unsigned long ret;
3428 int var;
3429 #ifndef _WIN64
3430 __asm__ volatile ("mov %P1,%0" : "=r" (ret) : "p" (&var));
3431 printf ("oc1: %d\n", ret == (unsigned long)&var);
3432 #endif
3435 #ifndef _WIN32
3436 /* Test global asm blocks playing with aliases. */
3437 void base_func(void)
3439 printf ("asmc: base\n");
3442 #ifndef __APPLE__
3443 extern void override_func1 (void);
3444 extern void override_func2 (void);
3446 asm(".weak override_func1\n.set override_func1, base_func");
3447 asm(".set override_func1, base_func");
3448 asm(".set override_func2, base_func");
3450 void override_func2 (void)
3452 printf ("asmc: override2\n");
3455 /* This checks a construct used by the linux kernel to encode
3456 references to strings by PC relative references. */
3457 extern int bug_table[] __attribute__((section("__bug_table")));
3458 char * get_asm_string (void)
3460 /* On i386 when -fPIC is enabled this would cause a compile error with GCC,
3461 the problem being the "i" constraint used with a symbolic operand
3462 resolving to a local label. That check is overly zealous as the code
3463 within the asm makes sure to use it only in PIC-possible contexts,
3464 but all GCC versions behave like so. We arrange for PIC to be disabled
3465 for compiling tcctest.c in the Makefile.
3467 Additionally the usage of 'c' in "%c0" in the template is actually wrong,
3468 as that would expect an operand that is a condition code. The operand
3469 as is (a local label) is accepted by GCC in non-PIC mode, and on x86-64.
3470 What the linux kernel really wanted is 'p' to disable the addition of '$'
3471 to the printed operand (as in "$.LC0" where the template only wants the
3472 bare operand ".LC0"). But the code below is what the linux kernel
3473 happens to use and as such is the one we want to test. */
3474 extern int some_symbol;
3475 asm volatile (".globl some_symbol\n"
3476 "jmp .+6\n"
3477 "1:\n"
3478 "some_symbol: .long 0\n"
3479 ".pushsection __bug_table, \"a\"\n"
3480 ".globl bug_table\n"
3481 "bug_table:\n"
3482 /* The first entry (1b-2b) is unused in this test,
3483 but we include it to check if cross-section
3484 PC-relative references work. */
3485 "2:\t.long 1b - 2b, %c0 - 2b\n"
3486 ".popsection\n" : : "i" ("A string"));
3487 char * str = ((char*)bug_table) + bug_table[1];
3488 return str;
3491 /* This checks another constructs with local labels. */
3492 extern unsigned char alld_stuff[];
3493 asm(".data\n"
3494 ".byte 41\n"
3495 "alld_stuff:\n"
3496 "661:\n"
3497 ".byte 42\n"
3498 "662:\n"
3499 ".pushsection .data.ignore\n"
3500 ".long 661b - .\n" /* This reference to 661 generates an external sym
3501 which shouldn't somehow overwrite the offset that's
3502 already determined for it. */
3503 ".popsection\n"
3504 ".byte 662b - 661b\n" /* So that this value is undeniably 1. */);
3506 void asm_local_label_diff (void)
3508 printf ("asm_local_label_diff: %d %d\n", alld_stuff[0], alld_stuff[1]);
3510 #endif
3512 /* This checks that static local variables are available from assembler. */
3513 void asm_local_statics (void)
3515 static int localint = 41;
3516 asm("incl %0" : "+m" (localint));
3517 printf ("asm_local_statics: %d\n", localint);
3519 #endif
3521 static
3522 unsigned int set;
3524 void fancy_copy (unsigned *in, unsigned *out)
3526 asm volatile ("" : "=r" (*out) : "0" (*in));
3529 void fancy_copy2 (unsigned *in, unsigned *out)
3531 asm volatile ("mov %0,(%1)" : : "r" (*in), "r" (out) : "memory");
3534 #if defined __x86_64__ && !defined _WIN64
3535 void clobber_r12(void)
3537 asm volatile("mov $1, %%r12" ::: "r12");
3539 #endif
3541 void test_high_clobbers_really(void)
3543 #if defined __x86_64__ && !defined _WIN64
3544 register long val asm("r12");
3545 long val2;
3546 /* This tests if asm clobbers correctly save/restore callee saved
3547 registers if they are clobbered and if it's the high 8 x86-64
3548 registers. This is fragile for GCC as the constraints do not
3549 correctly capture the data flow, but good enough for us. */
3550 asm volatile("mov $0x4542, %%r12" : "=r" (val):: "memory");
3551 clobber_r12();
3552 asm volatile("mov %%r12, %0" : "=r" (val2) : "r" (val): "memory");
3553 printf("asmhc: 0x%x\n", val2);
3554 #endif
3557 void test_high_clobbers(void)
3559 #if defined __x86_64__ && !defined _WIN64
3560 long x1, x2;
3561 asm volatile("mov %%r12,%0" :: "m" (x1)); /* save r12 */
3562 test_high_clobbers_really();
3563 asm volatile("mov %%r12,%0" :: "m" (x2)); /* new r12 */
3564 asm volatile("mov %0,%%r12" :: "m" (x1)); /* restore r12 */
3565 /* should be 0 but tcc doesn't save r12 automatically, which has
3566 bad effects when gcc helds TCCState *s in r12 in tcc.c:main */
3567 //printf("r12-clobber-diff: %lx\n", x2 - x1);
3568 #endif
3571 static long cpu_number;
3572 void trace_console(long len, long len2)
3574 #ifdef __x86_64__
3575 /* This generated invalid code when the emission of the switch
3576 table isn't disabled. The asms are necessary to show the bug,
3577 normal statements don't work (they need to generate some code
3578 even under nocode_wanted, which normal statements don't do,
3579 but asms do). Also at least these number of cases is necessary
3580 to generate enough "random" bytes. They ultimately are enough
3581 to create invalid instruction patterns to which the first
3582 skip-to-decision-table jump jumps. If decision table emission
3583 is disabled all of this is no problem.
3585 It also is necessary that the switches are in a statement expression
3586 (which has the property of not being enterable from outside. no
3587 matter what). */
3588 if (0
3591 long pscr_ret__;
3592 switch(len) {
3593 case 4:
3595 long pfo_ret__;
3596 switch (len2) {
3597 case 8: printf("bla"); pfo_ret__ = 42; break;
3599 pscr_ret__ = pfo_ret__;
3601 break;
3602 case 8:
3604 long pfo_ret__;
3605 switch (len2) {
3606 case 1:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3607 case 2:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3608 case 4:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3609 case 8:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3610 default: printf("impossible\n");
3612 pscr_ret__ = pfo_ret__;
3614 break;
3616 pscr_ret__;
3619 printf("huh?\n");
3621 #endif
3624 void test_asm_dead_code(void)
3626 long rdi;
3627 /* Try to make sure that xdi contains a zero, and hence will
3628 lead to a segfault if the next asm is evaluated without
3629 arguments being set up. */
3630 asm volatile ("" : "=D" (rdi) : "0" (0));
3631 (void)sizeof (({
3632 int var;
3633 /* This shouldn't trigger a segfault, either the argument
3634 registers need to be set up and the asm emitted despite
3635 this being in an unevaluated context, or both the argument
3636 setup _and_ the asm emission need to be suppressed. The latter
3637 is better. Disabling asm code gen when suppression is on
3638 also fixes the above trace_console bug, but that came earlier
3639 than asm suppression. */
3640 asm volatile ("movl $0,(%0)" : : "D" (&var) : "memory");
3641 var;
3642 }));
3645 void test_asm_call(void)
3647 #if defined __x86_64__ && !defined _WIN64
3648 static char str[] = "PATH";
3649 char *s;
3650 /* This tests if a reference to an undefined symbol from an asm
3651 block, which isn't otherwise referenced in this file, is correctly
3652 regarded as global symbol, so that it's resolved by other object files
3653 or libraries. We chose getenv here, which isn't used anywhere else
3654 in this file. (If we used e.g. printf, which is used we already
3655 would have a global symbol entry, not triggering the bug which is
3656 tested here). */
3657 /* two pushes so stack remains aligned */
3658 asm volatile ("push %%rdi; push %%rdi; mov %0, %%rdi;"
3659 #if 1 && !defined(__TINYC__) && (defined(__PIC__) || defined(__PIE__)) && !defined(__APPLE__)
3660 "call getenv@plt;"
3661 #elif defined(__APPLE__)
3662 "call _getenv;"
3663 #else
3664 "call getenv;"
3665 #endif
3666 "pop %%rdi; pop %%rdi"
3667 : "=a" (s) : "r" (str));
3668 printf("asmd: %s\n", s);
3669 #endif
3672 #if defined __x86_64__
3673 # define RX "(%rip)"
3674 #else
3675 # define RX
3676 #endif
3678 void asm_dot_test(void)
3680 int x;
3681 for (x = 1;; ++x) {
3682 int r = x;
3683 switch (x) {
3684 case 1:
3685 asm(".text; lea S"RX",%eax; lea ."RX",%ecx; sub %ecx,%eax; S=.; jmp p0");
3686 case 2:
3687 #ifndef __APPLE__
3688 /* clangs internal assembler is broken */
3689 asm(".text; jmp .+6; .int 123; mov .-4"RX",%eax; jmp p0");
3690 #else
3691 asm(".text; mov $0, %eax; jmp p0");
3692 #endif
3693 case 3:
3694 #if !defined(_WIN32) && !defined(__APPLE__)
3695 asm(".pushsection \".data\"; Y=.; .int 999; X=Y; .int 456; X=.-4; .popsection");
3696 #else
3697 asm(".data; Y=.; .int 999; X=Y; .int 456; X=.-4; .text");
3698 #endif
3699 asm(".text; mov X"RX",%eax; jmp p0");
3700 case 4:
3701 #ifdef __APPLE__
3702 /* Bah! Clang! Doesn't want to redefine 'X' */
3703 asm(".text; mov $123,%eax; jmp p0");
3704 #else
3705 #ifndef _WIN32
3706 asm(".data; X=.; .int 789; Y=.; .int 999; .previous");
3707 #else
3708 asm(".data; X=.; .int 789; Y=.; .int 999; .text");
3709 #endif
3710 asm(".text; mov X"RX",%eax; X=Y; jmp p0");
3711 #endif
3712 case 0:
3713 asm(".text; p0=.; mov %%eax,%0;" : "=m"(r)); break;
3715 if (r == x)
3716 break;
3717 printf("asm_dot_test %d: %d\n", x, r);
3721 void asm_test(void)
3723 char buf[128];
3724 unsigned int val, val2;
3725 struct struct123 s1;
3726 struct struct1231 s2 = { (unsigned long)&s1 };
3727 /* Hide the outer base_func, but check later that the inline
3728 asm block gets the outer one. */
3729 int base_func = 42;
3730 void override_func3 (void);
3731 unsigned long asmret;
3732 #ifdef BOOL_ISOC99
3733 _Bool somebool;
3734 #endif
3735 register int regvar asm("%esi");
3737 printf("inline asm:\n");
3739 // parse 0x1E-1 as 3 tokens in asm mode
3740 asm volatile ("mov $0x1E-1,%eax");
3742 /* test the no operand case */
3743 asm volatile ("xorl %eax, %eax");
3745 memcpy1(buf, "hello", 6);
3746 strncat1(buf, " worldXXXXX", 3);
3747 printf("%s\n", buf);
3749 memcpy2(buf, "hello", 6);
3750 strncat2(buf, " worldXXXXX", 3);
3751 printf("%s\n", buf);
3753 /* 'A' constraint test */
3754 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
3755 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
3757 s1.a = 42;
3758 s1.b = 43;
3759 printf("mconstraint: %d", mconstraint_test(&s2));
3760 printf(" %d %d\n", s1.a, s1.b);
3761 other_constraints_test();
3762 set = 0xff;
3763 sigdelset1(&set, 2);
3764 sigaddset1(&set, 16);
3765 /* NOTE: we test here if C labels are correctly restored after the
3766 asm statement */
3767 goto label1;
3768 label2:
3769 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
3770 printf("set=0x%x\n", set);
3771 val = 0x01020304;
3772 #ifndef __APPLE__
3773 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
3774 #endif
3775 #ifndef _WIN32
3776 #ifndef __APPLE__
3777 override_func1();
3778 override_func2();
3779 /* The base_func ref from the following inline asm should find
3780 the global one, not the local decl from this function. */
3781 asm volatile(".weak override_func3\n.set override_func3, base_func");
3782 override_func3();
3783 printf("asmstr: %s\n", get_asm_string());
3784 asm_local_label_diff();
3785 #endif
3786 asm_local_statics();
3787 #endif
3788 #ifndef __APPLE__
3789 /* clang can't deal with the type change */
3790 /* Check that we can also load structs of appropriate layout
3791 into registers. */
3792 asm volatile("" : "=r" (asmret) : "0"(s2));
3793 if (asmret != s2.addr)
3794 printf("asmstr: failed\n");
3795 #endif
3796 #ifdef BOOL_ISOC99
3797 /* Check that the typesize correctly sets the register size to
3798 8 bit. */
3799 asm volatile("cmp %1,%2; sete %0" : "=a"(somebool) : "r"(1), "r"(2));
3800 if (!somebool)
3801 printf("asmbool: failed\n");
3802 #endif
3803 val = 43;
3804 fancy_copy (&val, &val2);
3805 printf ("fancycpy(%d)=%d\n", val, val2);
3806 val = 44;
3807 fancy_copy2 (&val, &val2);
3808 printf ("fancycpy2(%d)=%d\n", val, val2);
3809 asm volatile ("mov $0x4243, %%esi" : "=r" (regvar));
3810 printf ("regvar=%x\n", regvar);
3811 test_high_clobbers();
3812 trace_console(8, 8);
3813 test_asm_dead_code();
3814 test_asm_call();
3815 asm_dot_test();
3816 return;
3817 label1:
3818 goto label2;
3821 #else
3823 void asm_test(void)
3827 #endif
3829 #define COMPAT_TYPE(type1, type2) \
3831 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
3832 __builtin_types_compatible_p (type1, type2));\
3835 int constant_p_var;
3837 void builtin_test(void)
3839 short s;
3840 int i;
3841 long long ll;
3842 #if GCC_MAJOR >= 3
3843 COMPAT_TYPE(int, int);
3844 COMPAT_TYPE(int, unsigned int);
3845 COMPAT_TYPE(int, char);
3846 COMPAT_TYPE(int, const int);
3847 COMPAT_TYPE(int, volatile int);
3848 COMPAT_TYPE(int *, int *);
3849 COMPAT_TYPE(int *, void *);
3850 COMPAT_TYPE(int *, const int *);
3851 COMPAT_TYPE(char *, unsigned char *);
3852 COMPAT_TYPE(char *, signed char *);
3853 COMPAT_TYPE(char *, char *);
3854 /* space is needed because tcc preprocessor introduces a space between each token */
3855 COMPAT_TYPE(char * *, void *);
3856 #endif
3857 printf("res1 = %d\n", __builtin_constant_p(1));
3858 printf("res2 = %d\n", __builtin_constant_p(1 + 2));
3859 printf("res3 = %d\n", __builtin_constant_p(&constant_p_var));
3860 printf("res4 = %d\n", __builtin_constant_p(constant_p_var));
3861 printf("res5 = %d\n", __builtin_constant_p(100000 / constant_p_var));
3862 #ifndef __APPLE__
3863 /* clang doesn't regard this as constant expression */
3864 printf("res6 = %d\n", __builtin_constant_p(i && 0));
3865 #endif
3866 printf("res7 = %d\n", __builtin_constant_p(i && 1));
3867 #ifndef __APPLE__
3868 printf("res8 = %d\n", __builtin_constant_p(i && 0 ? i : 34));
3869 #endif
3870 s = 1;
3871 ll = 2;
3872 i = __builtin_choose_expr (1 != 0, ll, s);
3873 printf("bce: %d\n", i);
3874 i = __builtin_choose_expr (1 != 1, ll, s);
3875 printf("bce: %d\n", i);
3876 i = sizeof (__builtin_choose_expr (1, ll, s));
3877 printf("bce: %d\n", i);
3878 i = sizeof (__builtin_choose_expr (0, ll, s));
3879 printf("bce: %d\n", i);
3881 //printf("bera: %p\n", __builtin_extract_return_addr((void*)43));
3884 #ifndef _WIN32
3885 extern int __attribute__((weak)) weak_f1(void);
3886 extern int __attribute__((weak)) weak_f2(void);
3887 extern int weak_f3(void);
3888 extern int __attribute__((weak)) weak_v1;
3889 extern int __attribute__((weak)) weak_v2;
3890 extern int weak_v3;
3892 extern int (*weak_fpa)() __attribute__((weak));
3893 extern int __attribute__((weak)) (*weak_fpb)();
3894 extern __attribute__((weak)) int (*weak_fpc)();
3896 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
3897 extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
3898 extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
3899 extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
3900 extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
3901 extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
3903 #ifndef __APPLE__
3904 static const size_t dummy = 0;
3905 extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
3906 extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
3907 extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
3908 #endif
3910 int some_lib_func(void);
3911 int dummy_impl_of_slf(void) { return 444; }
3912 #ifndef __APPLE__
3913 int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
3914 #endif
3916 int weak_toolate() __attribute__((weak));
3917 int weak_toolate() { return 0; }
3919 void __attribute__((weak)) weak_test(void)
3921 printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
3922 printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
3923 printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
3924 printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
3925 printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
3926 printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
3928 printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
3929 printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
3930 printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
3932 printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
3933 printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
3934 printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
3935 printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
3936 printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
3937 printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
3938 #ifndef __APPLE__
3939 printf("some_lib_func=%d\n", &some_lib_func ? some_lib_func() : 0);
3940 #endif
3943 int __attribute__((weak)) weak_f2() { return 222; }
3944 int __attribute__((weak)) weak_f3() { return 333; }
3945 int __attribute__((weak)) weak_v2 = 222;
3946 int __attribute__((weak)) weak_v3 = 333;
3947 #endif
3949 void const_func(const int a)
3953 void const_warn_test(void)
3955 const_func(1);
3958 struct condstruct {
3959 int i;
3962 int getme (struct condstruct *s, int i)
3964 int i1 = (i == 0 ? 0 : s)->i;
3965 int i2 = (i == 0 ? s : 0)->i;
3966 int i3 = (i == 0 ? (void*)0 : s)->i;
3967 int i4 = (i == 0 ? s : (void*)0)->i;
3968 return i1 + i2 + i3 + i4;
3971 struct global_data
3973 int a[40];
3974 int *b[40];
3977 struct global_data global_data;
3979 int global_data_getstuff (int *, int);
3981 void global_data_callit (int i)
3983 *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
3986 int global_data_getstuff (int *p, int i)
3988 return *p + i;
3991 void global_data_test (void)
3993 global_data.a[0] = 42;
3994 global_data.b[0] = &global_data.a[0];
3995 global_data_callit (0);
3996 printf ("%d\n", global_data.a[0]);
3999 struct cmpcmpS
4001 unsigned char fill : 3;
4002 unsigned char b1 : 1;
4003 unsigned char b2 : 1;
4004 unsigned char fill2 : 3;
4007 int glob1, glob2, glob3;
4009 void compare_comparisons (struct cmpcmpS *s)
4011 if (s->b1 != (glob1 == glob2)
4012 || (s->b2 != (glob1 == glob3)))
4013 printf ("comparing comparisons broken\n");
4016 void cmp_comparison_test(void)
4018 struct cmpcmpS s;
4019 s.b1 = 1;
4020 glob1 = 42; glob2 = 42;
4021 s.b2 = 0;
4022 glob3 = 43;
4023 compare_comparisons (&s);
4026 int fcompare (double a, double b, int code)
4028 switch (code) {
4029 case 0: return a == b;
4030 case 1: return a != b;
4031 case 2: return a < b;
4032 case 3: return a >= b;
4033 case 4: return a > b;
4034 case 5: return a <= b;
4036 return 0;
4039 void math_cmp_test(void)
4041 double nan = 0.0/0.0;
4042 double one = 1.0;
4043 double two = 2.0;
4044 int comp = 0;
4045 int v;
4046 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
4048 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
4049 And it does this in various ways so that all code generation paths
4050 are checked (generating inverted tests, or non-inverted tests, or
4051 producing a 0/1 value without jumps (that's done in the fcompare
4052 function). */
4053 #define FCMP(a,b,op,iop,code) \
4054 if (fcompare (a,b,code)) \
4055 bug (a,b,op,iop,1); \
4056 if (a op b) \
4057 bug (a,b,op,iop,2); \
4058 if (a iop b) \
4060 else \
4061 bug (a,b,op,iop,3); \
4062 if ((a op b) || comp) \
4063 bug (a,b,op,iop,4); \
4064 if ((a iop b) || comp) \
4066 else \
4067 bug (a,b,op,iop,5); \
4068 if (v = !(a op b), !v) bug(a,b,op,iop,7);
4070 /* Equality tests. */
4071 FCMP(nan, nan, ==, !=, 0);
4072 FCMP(one, two, ==, !=, 0);
4073 FCMP(one, one, !=, ==, 1);
4074 /* Non-equality is a bit special. */
4075 if (!fcompare (nan, nan, 1))
4076 bug (nan, nan, !=, ==, 6);
4078 /* Relational tests on numbers. */
4079 FCMP(two, one, <, >=, 2);
4080 FCMP(one, two, >=, <, 3);
4081 FCMP(one, two, >, <=, 4);
4082 FCMP(two, one, <=, >, 5);
4084 /* Relational tests on NaNs. Note that the inverse op here is
4085 always !=, there's no operator in C that is equivalent to !(a < b),
4086 when NaNs are involved, same for the other relational ops. */
4087 FCMP(nan, nan, <, !=, 2);
4088 FCMP(nan, nan, >=, !=, 3);
4089 FCMP(nan, nan, >, !=, 4);
4090 FCMP(nan, nan, <=, !=, 5);
4093 double get100 () { return 100.0; }
4095 void callsave_test(void)
4097 #if defined __i386__ || defined __x86_64__ || defined __arm__
4098 int i, s; double *d; double t;
4099 s = sizeof (double);
4100 printf ("callsavetest: %d\n", s);
4101 d = alloca (sizeof(double));
4102 d[0] = 10.0;
4103 /* x86-64 had a bug were the next call to get100 would evict
4104 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
4105 in int type, not pointer type. When alloca returns a pointer
4106 with the high 32 bit set (which is likely on x86-64) the access
4107 generates a segfault. */
4108 i = d[0] > get100 ();
4109 printf ("%d\n", i);
4110 #endif
4114 void bfa3(ptrdiff_t str_offset)
4116 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
4118 void bfa2(ptrdiff_t str_offset)
4120 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
4121 bfa3(str_offset);
4123 void bfa1(ptrdiff_t str_offset)
4125 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
4126 bfa2(str_offset);
4129 void builtin_frame_address_test(void)
4131 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
4132 #ifndef __arm__
4133 char str[] = "__builtin_frame_address";
4134 char *fp0 = __builtin_frame_address(0);
4136 printf("str: %s\n", str);
4137 #ifndef __riscv
4138 bfa1(str-fp0);
4139 #endif
4140 #endif
4143 char via_volatile (char i)
4145 char volatile vi;
4146 vi = i;
4147 return vi;
4150 struct __attribute__((__packed__)) Spacked {
4151 char a;
4152 short b;
4153 int c;
4155 struct Spacked spacked;
4156 typedef struct __attribute__((__packed__)) {
4157 char a;
4158 short b;
4159 int c;
4160 } Spacked2;
4161 Spacked2 spacked2;
4162 typedef struct Spacked3_s {
4163 char a;
4164 short b;
4165 int c;
4166 } __attribute__((__packed__)) Spacked3;
4167 Spacked3 spacked3;
4168 struct gate_struct64 {
4169 unsigned short offset_low;
4170 unsigned short segment;
4171 unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1;
4172 unsigned short offset_middle;
4173 unsigned offset_high;
4174 unsigned zero1;
4175 } __attribute__((packed));
4176 typedef struct gate_struct64 gate_desc;
4177 gate_desc a_gate_desc;
4178 void attrib_test(void)
4180 #ifndef _WIN32
4181 printf("attr: %d %d %d %d\n", sizeof(struct Spacked),
4182 sizeof(spacked), sizeof(Spacked2), sizeof(spacked2));
4183 printf("attr: %d %d\n", sizeof(Spacked3), sizeof(spacked3));
4184 printf("attr: %d %d\n", sizeof(gate_desc), sizeof(a_gate_desc));
4185 #endif
4187 extern __attribute__((__unused__)) char * __attribute__((__unused__)) *
4188 strange_attrib_placement (void);
4190 void * __attribute__((__unused__)) get_void_ptr (void *a)
4192 return a;
4195 /* This part checks for a bug in TOK_GET (used for inline expansion),
4196 where the large long long constant left the the high bits set for
4197 the integer constant token. */
4198 static inline
4199 int __get_order(unsigned long long size)
4201 int order;
4202 size -= 0xffff880000000000ULL; // this const left high bits set in the token
4204 struct S { int i : 1; } s; // constructed for this '1'
4206 order = size;
4207 return order;
4210 /* This just forces the above inline function to be actually emitted. */
4211 int force_get_order(unsigned long s)
4213 return __get_order(s);
4216 #define pv(m) printf(sizeof (s->m + 0) == 8 ? "%016llx\n" : "%02x\n", s->m)
4218 /* Test failed when using bounds checking */
4219 void bounds_check1_test (void)
4221 struct s {
4222 int x;
4223 long long y;
4224 } _s, *s = &_s;
4225 s->x = 10;
4226 s->y = 20;
4227 pv(x);
4228 pv(y);