Fix cast_test for clang.
[tinycc.git] / tests / tcctest.c
blobc90c02da0b6403f1d7b27a2443632a8c74a173eb
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 #ifdef __clang__
1486 int undefined_function(void) {}
1487 #endif
1489 static inline void refer_to_undefined(void)
1491 undefined_function();
1494 void optimize_out(void)
1496 int i = 0 ? undefined_function() : defined_function();
1497 printf ("oo:%d\n", i);
1498 int j = 1 ? defined_function() : undefined_function();
1499 printf ("oo:%d\n", j);
1500 if (0)
1501 printf("oo:%d\n", undefined_function());
1502 else
1503 printf("oo:%d\n", defined_function());
1504 if (1)
1505 printf("oo:%d\n", defined_function());
1506 else
1507 printf("oo:%d\n", undefined_function());
1508 while (1) {
1509 printf("oow:%d\n", defined_function());
1510 break;
1511 printf("oow:%d\n", undefined_function());
1513 j = 1;
1514 /* Following is a switch without {} block intentionally. */
1515 switch (j)
1516 case 1: break;
1517 printf ("oos:%d\n", defined_function());
1518 /* The following break shouldn't lead to disabled code after
1519 the while. */
1520 while (1)
1521 break;
1522 printf ("ool1:%d\n", defined_function());
1523 /* Same for the other types of loops. */
1525 break;
1526 while (1);
1527 printf ("ool2:%d\n", defined_function());
1528 for (;;)
1529 break;
1530 printf ("ool3:%d\n", defined_function());
1531 /* Normal {} blocks without controlling statements
1532 shouldn't reactivate code emission */
1533 while (1) {
1535 break;
1537 printf ("ool4:%d\n", undefined_function());
1539 j = 1;
1540 while (j) {
1541 if (j == 0)
1542 break; /* this break shouldn't disable code outside the if. */
1543 printf("ool5:%d\n", defined_function());
1544 j--;
1547 j = 1;
1548 while (j) {
1549 if (1)
1550 j--;
1551 else
1552 breakhere: break;
1553 printf("ool6:%d\n", defined_function());
1554 goto breakhere;
1556 j = 1;
1557 while (j) {
1558 j--;
1559 continue;
1560 printf("ool7:%d\n", undefined_function());
1563 /* Test that constants in logical && are optimized: */
1564 i = 0 && undefined_function();
1565 i = defined_function() && 0 && undefined_function();
1566 if (0 && undefined_function())
1567 undefined_function();
1568 if (defined_function() && 0)
1569 undefined_function();
1570 if (0 && 0)
1571 undefined_function();
1572 if (defined_function() && 0 && undefined_function())
1573 undefined_function();
1574 /* The same for || : */
1575 i = 1 || undefined_function();
1576 i = defined_function() || 1 || undefined_function();
1577 if (1 || undefined_function())
1579 else
1580 undefined_function();
1581 if (defined_function() || 1)
1583 else
1584 undefined_function();
1585 if (1 || 1)
1587 else
1588 undefined_function();
1589 if (defined_function() || 1 || undefined_function())
1591 else
1592 undefined_function();
1594 if (defined_function() && 0)
1595 refer_to_undefined();
1597 if (0) {
1598 (void)sizeof( ({
1599 do { } while (0);
1601 }) );
1602 undefined_function();
1605 /* Leave the "if(1)return; printf()" in this order and last in the function */
1606 if (1)
1607 return;
1608 printf ("oor:%d\n", undefined_function());
1611 int defined_function(void)
1613 static int i = 40;
1614 return i++;
1617 /* GCC accepts that */
1618 static int tab_reinit[];
1619 static int tab_reinit[10];
1621 static int tentative_ar[];
1622 static int tentative_ar[] = {1,2,3};
1624 //int cinit1; /* a global variable can be defined several times without error ! */
1625 int cinit1;
1626 int cinit1;
1627 int cinit1 = 0;
1628 int *cinit2 = (int []){3, 2, 1};
1630 void compound_literal_test(void)
1632 int *p, i;
1633 char *q, *q3;
1635 printf("compound_test:\n");
1637 p = (int []){1, 2, 3};
1638 for(i=0;i<3;i++)
1639 printf(" %d", p[i]);
1640 printf("\n");
1642 for(i=0;i<3;i++)
1643 printf("%d", cinit2[i]);
1644 printf("\n");
1646 q = "tralala1";
1647 printf("q1=%s\n", q);
1649 q = (char *){ "tralala2" };
1650 printf("q2=%s\n", q);
1652 q3 = (char *){ q };
1653 printf("q3=%s\n", q3);
1655 q = (char []){ "tralala3" };
1656 printf("q4=%s\n", q);
1658 #ifdef ALL_ISOC99
1659 p = (int []){1, 2, cinit1 + 3};
1660 for(i=0;i<3;i++)
1661 printf(" %d", p[i]);
1662 printf("\n");
1664 for(i=0;i<3;i++) {
1665 p = (int []){1, 2, 4 + i};
1666 printf("%d %d %d\n",
1667 p[0],
1668 p[1],
1669 p[2]);
1671 #endif
1674 /* K & R protos */
1676 kr_func1(a, b)
1678 return a + b;
1681 int kr_func2(a, b)
1683 return a + b;
1686 kr_test()
1688 printf("kr_test:\n");
1689 printf("func1=%d\n", kr_func1(3, 4));
1690 printf("func2=%d\n", kr_func2(3, 4));
1691 return 0;
1694 void num(int n)
1696 char *tab, *p;
1697 tab = (char*)malloc(20);
1698 p = tab;
1699 while (1) {
1700 *p = 48 + (n % 10);
1701 p++;
1702 n = n / 10;
1703 if (n == 0)
1704 break;
1706 while (p != tab) {
1707 p--;
1708 printf("%c", *p);
1710 printf("\n");
1711 free(tab);
1714 /* structure assignment tests */
1715 struct structa1 {
1716 int f1;
1717 char f2;
1720 struct structa1 ssta1;
1722 void struct_assign_test1(struct structa1 s1, int t, float f)
1724 printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
1727 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1729 s1.f1 += t;
1730 s1.f2 -= t;
1731 return s1;
1734 void struct_assign_test(void)
1736 struct S {
1737 struct structa1 lsta1, lsta2;
1738 int i;
1739 } s, *ps;
1741 ps = &s;
1742 ps->i = 4;
1743 #if 0
1744 printf("struct_assign_test:\n");
1746 s.lsta1.f1 = 1;
1747 s.lsta1.f2 = 2;
1748 printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
1749 s.lsta2 = s.lsta1;
1750 printf("%d %d\n", s.lsta2.f1, s.lsta2.f2);
1751 #else
1752 s.lsta2.f1 = 1;
1753 s.lsta2.f2 = 2;
1754 #endif
1755 struct_assign_test1(ps->lsta2, 3, 4.5);
1757 printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
1758 ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
1759 printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
1761 static struct {
1762 void (*elem)();
1763 } t[] = {
1764 /* XXX: we should allow this even without braces */
1765 { struct_assign_test }
1767 printf("%d\n", struct_assign_test == t[0].elem);
1770 /* casts to short/char */
1772 void cast1(char a, short b, unsigned char c, unsigned short d)
1774 printf("%d %d %d %d\n", a, b, c, d);
1777 char bcast;
1778 short scast;
1780 void cast_test()
1782 int a;
1783 char c;
1784 char tab[10];
1785 unsigned b,d;
1786 short s;
1787 char *p = NULL;
1788 unsigned long ul = 0x80000000UL;
1789 p -= 0x700000000042;
1791 printf("cast_test:\n");
1792 a = 0xfffff;
1793 cast1(a, a, a, a);
1794 a = 0xffffe;
1795 printf("%d %d %d %d\n",
1796 (char)(a + 1),
1797 (short)(a + 1),
1798 (unsigned char)(a + 1),
1799 (unsigned short)(a + 1));
1800 printf("%d %d %d %d\n",
1801 (char)0xfffff,
1802 (short)0xfffff,
1803 (unsigned char)0xfffff,
1804 (unsigned short)0xfffff);
1806 a = (bcast = 128) + 1;
1807 printf("%d\n", a);
1808 a = (scast = 65536) + 1;
1809 printf("%d\n", a);
1811 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1813 /* test cast from unsigned to signed short to int */
1814 b = 0xf000;
1815 d = (short)b;
1816 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1817 b = 0xf0f0;
1818 d = (char)b;
1819 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1821 /* test implicit int casting for array accesses */
1822 c = 0;
1823 tab[1] = 2;
1824 tab[c] = 1;
1825 printf("%d %d\n", tab[0], tab[1]);
1827 /* test implicit casting on some operators */
1828 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1829 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1830 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1832 /* from pointer to integer types */
1833 printf("%d %d %ld %ld %lld %lld\n",
1834 (int)(long)p, (unsigned int)(long)p,
1835 (long)p, (unsigned long)p,
1836 (long long)(long)p, (unsigned long long)(long)p);
1838 /* from integers to pointers */
1839 printf("%p %p %p %p\n",
1840 (void *)a, (void *)b, (void *)c, (void *)d);
1842 /* int to int with sign set */
1843 printf("0x%lx\n", (unsigned long)(int)ul);
1846 /* initializers tests */
1847 struct structinit1 {
1848 int f1;
1849 char f2;
1850 short f3;
1851 int farray[3];
1854 int sinit1 = 2;
1855 int sinit2 = { 3 };
1856 int sinit3[3] = { 1, 2, {{3}}, };
1857 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1858 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1859 int sinit6[] = { 1, 2, 3 };
1860 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1861 char sinit8[] = "hello" "trala";
1863 struct structinit1 sinit9 = { 1, 2, 3 };
1864 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1865 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1866 #ifdef ALL_ISOC99
1867 .farray[0] = 10,
1868 .farray[1] = 11,
1869 .farray[2] = 12,
1870 #endif
1873 char *sinit12 = "hello world";
1874 char *sinit13[] = {
1875 "test1",
1876 "test2",
1877 "test3",
1879 char sinit14[10] = { "abc" };
1880 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1882 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1884 struct bar {
1885 char *s;
1886 int len;
1887 } sinit17[] = {
1888 "a1", 4,
1889 "a2", 1
1892 int sinit18[10] = {
1893 [2 ... 5] = 20,
1895 [8] = 10,
1898 struct complexinit0 {
1899 int a;
1900 int b;
1903 struct complexinit {
1904 int a;
1905 const struct complexinit0 *b;
1908 const static struct complexinit cix[] = {
1909 [0] = {
1910 .a = 2000,
1911 .b = (const struct complexinit0[]) {
1912 { 2001, 2002 },
1913 { 2003, 2003 },
1919 struct complexinit2 {
1920 int a;
1921 int b[];
1924 struct complexinit2 cix20;
1926 struct complexinit2 cix21 = {
1927 .a = 3000,
1928 .b = { 3001, 3002, 3003 }
1931 struct complexinit2 cix22 = {
1932 .a = 4000,
1933 .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1936 typedef int arrtype1[];
1937 arrtype1 sinit19 = {1};
1938 arrtype1 sinit20 = {2,3};
1939 typedef int arrtype2[3];
1940 arrtype2 sinit21 = {4};
1941 arrtype2 sinit22 = {5,6,7};
1943 /* Address comparisons of non-weak symbols with zero can be const-folded */
1944 int sinit23[2] = { "astring" ? sizeof("astring") : -1,
1945 &sinit23 ? 42 : -1 };
1947 int sinit24 = 2 || 1 / 0; /* exception in constant but unevaluated context */
1949 extern int external_inited = 42;
1951 void init_test(void)
1953 int linit1 = 2;
1954 int linit2 = { 3 };
1955 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1956 int linit6[] = { 1, 2, 3 };
1957 int i, j;
1958 char linit8[] = "hello" "trala";
1959 int linit12[10] = { 1, 2 };
1960 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1961 char linit14[10] = "abc";
1962 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1963 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1964 int linit17 = sizeof(linit17);
1965 int zero = 0;
1966 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1967 int linit18[2] = {&zero ? 1 : -1, zero ? -1 : 1 };
1969 printf("init_test:\n");
1971 printf("sinit1=%d\n", sinit1);
1972 printf("sinit2=%d\n", sinit2);
1973 printf("sinit3=%d %d %d %d\n",
1974 sizeof(sinit3),
1975 sinit3[0],
1976 sinit3[1],
1977 sinit3[2]
1979 printf("sinit6=%d\n", sizeof(sinit6));
1980 printf("sinit7=%d %d %d %d\n",
1981 sizeof(sinit7),
1982 sinit7[0],
1983 sinit7[1],
1984 sinit7[2]
1986 printf("sinit8=%s\n", sinit8);
1987 printf("sinit9=%d %d %d\n",
1988 sinit9.f1,
1989 sinit9.f2,
1990 sinit9.f3
1992 printf("sinit10=%d %d %d\n",
1993 sinit10.f1,
1994 sinit10.f2,
1995 sinit10.f3
1997 printf("sinit11=%d %d %d %d %d %d\n",
1998 sinit11.f1,
1999 sinit11.f2,
2000 sinit11.f3,
2001 sinit11.farray[0],
2002 sinit11.farray[1],
2003 sinit11.farray[2]
2006 for(i=0;i<3;i++)
2007 for(j=0;j<2;j++)
2008 printf("[%d][%d] = %d %d %d\n",
2009 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
2010 printf("linit1=%d\n", linit1);
2011 printf("linit2=%d\n", linit2);
2012 printf("linit6=%d\n", sizeof(linit6));
2013 printf("linit8=%d %s\n", sizeof(linit8), linit8);
2015 printf("sinit12=%s\n", sinit12);
2016 printf("sinit13=%d %s %s %s\n",
2017 sizeof(sinit13),
2018 sinit13[0],
2019 sinit13[1],
2020 sinit13[2]);
2021 printf("sinit14=%s\n", sinit14);
2023 for(i=0;i<10;i++) printf(" %d", linit12[i]);
2024 printf("\n");
2025 for(i=0;i<10;i++) printf(" %d", linit13[i]);
2026 printf("\n");
2027 for(i=0;i<10;i++) printf(" %d", linit14[i]);
2028 printf("\n");
2029 for(i=0;i<10;i++) printf(" %d", linit15[i]);
2030 printf("\n");
2031 printf("%d %d %d %d\n",
2032 linit16.a1,
2033 linit16.a2,
2034 linit16.a3,
2035 linit16.a4);
2036 /* test that initialisation is done after variable declare */
2037 printf("linit17=%d\n", linit17);
2038 printf("sinit15=%d\n", sinit15[0]);
2039 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
2040 printf("sinit17=%s %d %s %d\n",
2041 sinit17[0].s, sinit17[0].len,
2042 sinit17[1].s, sinit17[1].len);
2043 for(i=0;i<10;i++)
2044 printf("%x ", sinit18[i]);
2045 printf("\n");
2046 /* complex init check */
2047 printf("cix: %d %d %d %d %d %d %d\n",
2048 cix[0].a,
2049 cix[0].b[0].a, cix[0].b[0].b,
2050 cix[0].b[1].a, cix[0].b[1].b,
2051 cix[0].b[2].a, cix[0].b[2].b);
2052 printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
2053 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
2055 printf("arrtype1: %d %d %d\n", sinit19[0], sinit20[0], sinit20[1]);
2056 printf("arrtype2: %d %d\n", sizeof(sinit19), sizeof(sinit20));
2057 printf("arrtype3: %d %d %d\n", sinit21[0], sinit21[1], sinit21[2]);
2058 printf("arrtype4: %d %d %d\n", sinit22[0], sinit22[1], sinit22[2]);
2059 printf("arrtype5: %d %d\n", sizeof(sinit21), sizeof(sinit22));
2060 printf("arrtype6: %d\n", sizeof(arrtype2));
2062 printf("sinit23= %d %d\n", sinit23[0], sinit23[1]);
2063 printf("sinit24=%d\n", sinit24);
2064 printf("linit18= %d %d\n", linit18[0], linit18[1]);
2067 void switch_uc(unsigned char uc)
2069 switch (uc) {
2070 case 0xfb ... 0xfe:
2071 printf("ucsw:1\n");
2072 break;
2073 case 0xff:
2074 printf("ucsw:2\n");
2075 break;
2076 case 0 ... 5:
2077 printf("ucsw:3\n");
2078 break;
2079 default:
2080 printf("ucsw: broken!\n");
2084 void switch_sc(signed char sc)
2086 switch (sc) {
2087 case -5 ... -2:
2088 printf("scsw:1\n");
2089 break;
2090 case -1:
2091 printf("scsw:2\n");
2092 break;
2093 case 0 ... 5:
2094 printf("scsw:3\n");
2095 break;
2096 default:
2097 printf("scsw: broken!\n");
2101 void switch_test()
2103 int i;
2104 unsigned long long ull;
2105 long long ll;
2107 for(i=0;i<15;i++) {
2108 switch(i) {
2109 case 0:
2110 case 1:
2111 printf("a");
2112 break;
2113 default:
2114 printf("%d", i);
2115 break;
2116 case 8 ... 12:
2117 printf("c");
2118 break;
2119 case 3:
2120 printf("b");
2121 break;
2122 case 0xc33c6b9fU:
2123 case 0x7c9eeeb9U:
2124 break;
2127 printf("\n");
2129 for (i = 1; i <= 5; i++) {
2130 ull = (unsigned long long)i << 61;
2131 switch (ull) {
2132 case 1ULL << 61:
2133 printf("ullsw:1\n");
2134 break;
2135 case 2ULL << 61:
2136 printf("ullsw:2\n");
2137 break;
2138 case 3ULL << 61:
2139 printf("ullsw:3\n");
2140 break;
2141 case 4ULL << 61:
2142 printf("ullsw:4\n");
2143 break;
2144 case 5ULL << 61:
2145 printf("ullsw:5\n");
2146 break;
2147 default:
2148 printf("ullsw: broken!\n");
2152 for (i = 1; i <= 5; i++) {
2153 ll = (long long)i << 61;
2154 switch (ll) {
2155 case 1LL << 61:
2156 printf("llsw:1\n");
2157 break;
2158 case 2LL << 61:
2159 printf("llsw:2\n");
2160 break;
2161 case 3LL << 61:
2162 printf("llsw:3\n");
2163 break;
2164 case 4LL << 61:
2165 printf("llsw:4\n");
2166 break;
2167 case 5LL << 61:
2168 printf("llsw:5\n");
2169 break;
2170 default:
2171 printf("llsw: broken!\n");
2175 for (i = -5; i <= 5; i++) {
2176 switch_uc((unsigned char)i);
2179 for (i = -5; i <= 5; i++) {
2180 switch_sc ((signed char)i);
2184 /* ISOC99 _Bool type */
2185 void c99_bool_test(void)
2187 #ifdef BOOL_ISOC99
2188 int a;
2189 _Bool b, b2;
2191 printf("bool_test:\n");
2192 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
2193 a = 3;
2194 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
2195 b = 3;
2196 printf("b = %d\n", b);
2197 b++;
2198 printf("b = %d\n", b);
2199 b2 = 0;
2200 printf("sizeof(x ? _Bool : _Bool) = %d (should be sizeof int)\n",
2201 sizeof((volatile)a ? b : b2));
2202 #endif
2205 void bitfield_test(void)
2207 int a;
2208 short sa;
2209 unsigned char ca;
2210 struct sbf1 {
2211 int f1 : 3;
2212 int : 2;
2213 int f2 : 1;
2214 int : 0;
2215 int f3 : 5;
2216 int f4 : 7;
2217 unsigned int f5 : 7;
2218 } st1;
2219 printf("bitfield_test:");
2220 printf("sizeof(st1) = %d\n", sizeof(st1));
2222 st1.f1 = 3;
2223 st1.f2 = 1;
2224 st1.f3 = 15;
2225 a = 120;
2226 st1.f4 = a;
2227 st1.f5 = a;
2228 st1.f5++;
2229 printf("%d %d %d %d %d\n",
2230 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
2231 sa = st1.f5;
2232 ca = st1.f5;
2233 printf("%d %d\n", sa, ca);
2235 st1.f1 = 7;
2236 if (st1.f1 == -1)
2237 printf("st1.f1 == -1\n");
2238 else
2239 printf("st1.f1 != -1\n");
2240 if (st1.f2 == -1)
2241 printf("st1.f2 == -1\n");
2242 else
2243 printf("st1.f2 != -1\n");
2245 struct sbf2 {
2246 long long f1 : 45;
2247 long long : 2;
2248 long long f2 : 35;
2249 unsigned long long f3 : 38;
2250 } st2;
2251 st2.f1 = 0x123456789ULL;
2252 a = 120;
2253 st2.f2 = (long long)a << 25;
2254 st2.f3 = a;
2255 st2.f2++;
2256 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
2258 #if 0
2259 Disabled for now until further clarification re GCC compatibility
2260 struct sbf3 {
2261 int f1 : 7;
2262 int f2 : 1;
2263 char f3;
2264 int f4 : 8;
2265 int f5 : 1;
2266 int f6 : 16;
2267 } st3;
2268 printf("sizeof(st3) = %d\n", sizeof(st3));
2269 #endif
2271 struct sbf4 {
2272 int x : 31;
2273 char y : 2;
2274 } st4;
2275 st4.y = 1;
2276 printf("st4.y == %d\n", st4.y);
2277 struct sbf5 {
2278 int a;
2279 char b;
2280 int x : 12, y : 4, : 0, : 4, z : 3;
2281 char c;
2282 } st5 = { 1, 2, 3, 4, -3, 6 };
2283 printf("st5 = %d %d %d %d %d %d\n", st5.a, st5.b, st5.x, st5.y, st5.z, st5.c);
2284 struct sbf6 {
2285 short x : 12;
2286 unsigned char y : 2;
2287 } st6;
2288 st6.y = 1;
2289 printf("st6.y == %d\n", st6.y);
2292 #ifdef __x86_64__
2293 #define FLOAT_FMT "%f\n"
2294 #else
2295 /* x86's float isn't compatible with GCC */
2296 #define FLOAT_FMT "%.5f\n"
2297 #endif
2299 /* declare strto* functions as they are C99 */
2300 double strtod(const char *nptr, char **endptr);
2302 #if defined(_WIN32)
2303 float strtof(const char *nptr, char **endptr) {return (float)strtod(nptr, endptr);}
2304 LONG_DOUBLE strtold(const char *nptr, char **endptr) {return (LONG_DOUBLE)strtod(nptr, endptr);}
2305 #else
2306 float strtof(const char *nptr, char **endptr);
2307 LONG_DOUBLE strtold(const char *nptr, char **endptr);
2308 #endif
2310 #define FTEST(prefix, typename, type, fmt)\
2311 void prefix ## cmp(type a, type b)\
2313 printf("%d %d %d %d %d %d\n",\
2314 a == b,\
2315 a != b,\
2316 a < b,\
2317 a > b,\
2318 a >= b,\
2319 a <= b);\
2320 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
2323 a + b,\
2324 a - b,\
2325 a * b,\
2326 a / b,\
2327 -a);\
2328 printf(fmt "\n", ++a);\
2329 printf(fmt "\n", a++);\
2330 printf(fmt "\n", a);\
2331 b = 0;\
2332 printf("%d %d\n", !a, !b);\
2334 void prefix ## fcast(type a)\
2336 float fa;\
2337 double da;\
2338 LONG_DOUBLE la;\
2339 int ia;\
2340 long long llia;\
2341 unsigned int ua;\
2342 unsigned long long llua;\
2343 type b;\
2344 fa = a;\
2345 da = a;\
2346 la = a;\
2347 printf("ftof: %f %f %Lf\n", fa, da, la);\
2348 ia = (int)a;\
2349 llia = (long long)a;\
2350 a = (a >= 0) ? a : -a;\
2351 ua = (unsigned int)a;\
2352 llua = (unsigned long long)a;\
2353 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
2354 ia = -1234;\
2355 ua = 0x81234500;\
2356 llia = -0x123456789012345LL;\
2357 llua = 0xf123456789012345LLU;\
2358 b = ia;\
2359 printf("itof: " fmt "\n", b);\
2360 b = ua;\
2361 printf("utof: " fmt "\n", b);\
2362 b = llia;\
2363 printf("lltof: " fmt "\n", b);\
2364 b = llua;\
2365 printf("ulltof: " fmt "\n", b);\
2368 float prefix ## retf(type a) { return a; }\
2369 double prefix ## retd(type a) { return a; }\
2370 LONG_DOUBLE prefix ## retld(type a) { return a; }\
2372 void prefix ## call(void)\
2374 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
2375 printf("double: %f\n", prefix ## retd(42.123456789));\
2376 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
2377 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
2380 void prefix ## signed_zeros(void) \
2382 type x = 0.0, y = -0.0, n, p;\
2383 if (x == y)\
2384 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
2385 1.0 / x != 1.0 / y);\
2386 else\
2387 printf ("x != y; this is wrong!\n");\
2389 n = -x;\
2390 if (x == n)\
2391 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
2392 1.0 / x != 1.0 / n);\
2393 else\
2394 printf ("x != -x; this is wrong!\n");\
2396 p = +y;\
2397 if (x == p)\
2398 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
2399 1.0 / x != 1.0 / p);\
2400 else\
2401 printf ("x != +y; this is wrong!\n");\
2402 p = -y;\
2403 if (x == p)\
2404 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
2405 1.0 / x != 1.0 / p);\
2406 else\
2407 printf ("x != -y; this is wrong!\n");\
2409 void prefix ## test(void)\
2411 printf("testing '%s'\n", #typename);\
2412 prefix ## cmp(1, 2.5);\
2413 prefix ## cmp(2, 1.5);\
2414 prefix ## cmp(1, 1);\
2415 prefix ## fcast(234.6);\
2416 prefix ## fcast(-2334.6);\
2417 prefix ## call();\
2418 prefix ## signed_zeros();\
2421 FTEST(f, float, float, "%f")
2422 FTEST(d, double, double, "%f")
2423 FTEST(ld, long double, LONG_DOUBLE, "%Lf")
2425 double ftab1[3] = { 1.2, 3.4, -5.6 };
2428 void float_test(void)
2430 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
2431 float fa, fb;
2432 double da, db;
2433 int a;
2434 unsigned int b;
2435 static double nan2 = 0.0/0.0;
2436 static double inf1 = 1.0/0.0;
2437 static double inf2 = 1e5000;
2439 printf("float_test:\n");
2440 printf("sizeof(float) = %d\n", sizeof(float));
2441 printf("sizeof(double) = %d\n", sizeof(double));
2442 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));
2443 ftest();
2444 dtest();
2445 ldtest();
2446 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
2447 printf("%f %f %f\n", 2.12, .5, 2.3e10);
2448 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
2449 da = 123;
2450 printf("da=%f\n", da);
2451 fa = 123;
2452 printf("fa=%f\n", fa);
2453 a = 4000000000;
2454 da = a;
2455 printf("da = %f\n", da);
2456 b = 4000000000;
2457 db = b;
2458 printf("db = %f\n", db);
2459 printf("nan != nan = %d, inf1 = %f, inf2 = %f\n", nan2 != nan2, inf1, inf2);
2460 #endif
2463 int fib(int n)
2465 if (n <= 2)
2466 return 1;
2467 else
2468 return fib(n-1) + fib(n-2);
2471 #if __GNUC__ == 3
2472 # define aligned_function 0
2473 #else
2474 void __attribute__((aligned(16))) aligned_function(int i) {}
2475 #endif
2477 void funcptr_test()
2479 void (*func)(int);
2480 int a;
2481 struct {
2482 int dummy;
2483 void (*func)(int);
2484 } st1;
2485 long diff;
2487 printf("funcptr:\n");
2488 func = &num;
2489 (*func)(12345);
2490 func = num;
2491 a = 1;
2492 a = 1;
2493 func(12345);
2494 /* more complicated pointer computation */
2495 st1.func = num;
2496 st1.func(12346);
2497 printf("sizeof1 = %d\n", sizeof(funcptr_test));
2498 printf("sizeof2 = %d\n", sizeof funcptr_test);
2499 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
2500 printf("sizeof4 = %d\n", sizeof &funcptr_test);
2501 a = 0;
2502 func = num + a;
2503 diff = func - num;
2504 func(42);
2505 (func + diff)(42);
2506 (num + a)(43);
2508 /* Check that we can align functions */
2509 func = aligned_function;
2510 printf("aligned_function (should be zero): %d\n", ((int)(uintptr_t)func) & 15);
2513 void lloptest(long long a, long long b)
2515 unsigned long long ua, ub;
2517 ua = a;
2518 ub = b;
2519 /* arith */
2520 printf("arith: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2521 a + b,
2522 a - b,
2523 a * b);
2525 if (b != 0) {
2526 printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2527 a / b,
2528 a % b);
2531 /* binary */
2532 printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2533 a & b,
2534 a | b,
2535 a ^ b);
2537 /* tests */
2538 printf("test: %d %d %d %d %d %d\n",
2539 a == b,
2540 a != b,
2541 a < b,
2542 a > b,
2543 a >= b,
2544 a <= b);
2546 printf("utest: %d %d %d %d %d %d\n",
2547 ua == ub,
2548 ua != ub,
2549 ua < ub,
2550 ua > ub,
2551 ua >= ub,
2552 ua <= ub);
2554 /* arith2 */
2555 a++;
2556 b++;
2557 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2558 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a++, b++);
2559 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", --a, --b);
2560 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2561 b = ub = 0;
2562 printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
2565 void llshift(long long a, int b)
2567 printf("shift: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2568 (unsigned long long)a >> b,
2569 a >> b,
2570 a << b);
2571 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2572 (unsigned long long)a >> 3,
2573 a >> 3,
2574 a << 3);
2575 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
2576 (unsigned long long)a >> 35,
2577 a >> 35,
2578 a << 35);
2581 void llfloat(void)
2583 float fa;
2584 double da;
2585 LONG_DOUBLE lda;
2586 long long la, lb, lc;
2587 unsigned long long ula, ulb, ulc;
2588 la = 0x12345678;
2589 ula = 0x72345678;
2590 la = (la << 20) | 0x12345;
2591 ula = ula << 33;
2592 printf("la=" LONG_LONG_FORMAT " ula=" ULONG_LONG_FORMAT "\n", la, ula);
2594 fa = la;
2595 da = la;
2596 lda = la;
2597 printf("lltof: %f %f %Lf\n", fa, da, lda);
2599 la = fa;
2600 lb = da;
2601 lc = lda;
2602 printf("ftoll: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", la, lb, lc);
2604 fa = ula;
2605 da = ula;
2606 lda = ula;
2607 printf("ulltof: %f %f %Lf\n", fa, da, lda);
2609 ula = fa;
2610 ulb = da;
2611 ulc = lda;
2612 printf("ftoull: " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT "\n", ula, ulb, ulc);
2615 long long llfunc1(int a)
2617 return a * 2;
2620 struct S {
2621 int id;
2622 char item;
2625 long long int value(struct S *v)
2627 return ((long long int)v->item);
2630 long long llfunc2(long long x, long long y, int z)
2632 return x * y * z;
2635 void check_opl_save_regs(char *a, long long b, int c)
2637 *a = b < 0 && !c;
2640 void longlong_test(void)
2642 long long a, b, c;
2643 int ia;
2644 unsigned int ua;
2645 printf("longlong_test:\n");
2646 printf("sizeof(long long) = %d\n", sizeof(long long));
2647 ia = -1;
2648 ua = -2;
2649 a = ia;
2650 b = ua;
2651 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2652 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %Lx\n",
2653 (long long)1,
2654 (long long)-2,
2655 1LL,
2656 0x1234567812345679);
2657 a = llfunc1(-3);
2658 printf(LONG_LONG_FORMAT "\n", a);
2660 lloptest(1000, 23);
2661 lloptest(0xff, 0x1234);
2662 b = 0x72345678 << 10;
2663 lloptest(-3, b);
2664 llshift(0x123, 5);
2665 llshift(-23, 5);
2666 b = 0x72345678LL << 10;
2667 llshift(b, 47);
2669 llfloat();
2670 #if 1
2671 b = 0x12345678;
2672 a = -1;
2673 c = a + b;
2674 printf("%Lx\n", c);
2675 #endif
2677 /* long long reg spill test */
2679 struct S a;
2681 a.item = 3;
2682 printf("%lld\n", value(&a));
2684 lloptest(0x80000000, 0);
2687 long long *p, v, **pp;
2688 v = 1;
2689 p = &v;
2690 p[0]++;
2691 printf("another long long spill test : %lld\n", *p);
2692 pp = &p;
2694 v = llfunc2(**pp, **pp, ia);
2695 printf("a long long function (arm-)reg-args test : %lld\n", v);
2697 a = 68719476720LL;
2698 b = 4294967295LL;
2699 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
2701 printf(LONG_LONG_FORMAT "\n", 0x123456789LLU);
2703 /* long long pointer deref in argument passing test */
2704 a = 0x123;
2705 long long *p = &a;
2706 llshift(*p, 5);
2708 /* shortening followed by widening */
2709 unsigned long long u = 0x8000000000000001ULL;
2710 u = (unsigned)(u + 1);
2711 printf("long long u=" ULONG_LONG_FORMAT "\n", u);
2712 u = 0x11223344aa998877ULL;
2713 u = (unsigned)(int)(u + 1);
2714 printf("long long u=" ULONG_LONG_FORMAT "\n", u);
2716 /* was a problem with missing save_regs in gen_opl on 32-bit platforms */
2717 char cc = 78;
2718 check_opl_save_regs(&cc, -1, 0);
2719 printf("check_opl_save_regs: %d\n", cc);
2722 void manyarg_test(void)
2724 LONG_DOUBLE ld = 1234567891234LL;
2725 printf("manyarg_test:\n");
2726 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %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 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2730 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2731 1, 2, 3, 4, 5, 6, 7, 8,
2732 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2733 1234567891234LL, 987654321986LL,
2734 42.0, 43.0);
2735 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2736 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2737 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2738 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2739 1234567891234LL, 987654321986LL,
2740 42.0, 43.0);
2741 printf("%d %d %d %d %d %d %d %d %Lf\n",
2742 1, 2, 3, 4, 5, 6, 7, 8, ld);
2743 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2744 LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n",
2745 1, 2, 3, 4, 5, 6, 7, 8,
2746 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2747 1234567891234LL, 987654321986LL,
2748 42.0, 43.0, ld);
2749 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2750 "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n",
2751 1, 2, 3, 4, 5, 6, 7, 8,
2752 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2753 ld, 1234567891234LL, 987654321986LL,
2754 42.0, 43.0, ld);
2757 void*
2758 va_arg_with_struct_ptr(va_list ap) {
2760 * This was a BUG identified with FFTW-3.3.8 on arm64.
2761 * The test case only checks it compiles on all supported
2762 * architectures. This function is not currently called.
2764 struct X { int _x; };
2765 struct X *x = va_arg(ap, struct X *);
2766 return x;
2769 void vprintf1(const char *fmt, ...)
2771 va_list ap, aq;
2772 const char *p;
2773 int c, i;
2774 double d;
2775 long long ll;
2776 LONG_DOUBLE ld;
2778 va_start(aq, fmt);
2779 va_copy(ap, aq);
2781 p = fmt;
2782 for(;;) {
2783 c = *p;
2784 if (c == '\0')
2785 break;
2786 p++;
2787 if (c == '%') {
2788 c = *p;
2789 switch(c) {
2790 case '\0':
2791 goto the_end;
2792 case 'd':
2793 i = va_arg(ap, int);
2794 printf("%d", i);
2795 break;
2796 case 'f':
2797 d = va_arg(ap, double);
2798 printf("%f", d);
2799 break;
2800 case 'l':
2801 ll = va_arg(ap, long long);
2802 printf(LONG_LONG_FORMAT, ll);
2803 break;
2804 case 'F':
2805 ld = va_arg(ap, LONG_DOUBLE);
2806 printf("%Lf", ld);
2807 break;
2809 p++;
2810 } else {
2811 putchar(c);
2814 the_end:
2815 va_end(aq);
2816 va_end(ap);
2819 struct myspace {
2820 short int profile;
2823 void stdarg_for_struct(struct myspace bob, ...)
2825 struct myspace george, bill;
2826 va_list ap;
2827 short int validate;
2829 va_start(ap, bob);
2830 bill = va_arg(ap, struct myspace);
2831 george = va_arg(ap, struct myspace);
2832 validate = va_arg(ap, int);
2833 printf("stdarg_for_struct: %d %d %d %d\n",
2834 bob.profile, bill.profile, george.profile, validate);
2835 va_end(ap);
2838 void stdarg_for_libc(const char *fmt, ...)
2840 va_list args;
2841 va_start(args, fmt);
2842 vprintf(fmt, args);
2843 va_end(args);
2846 void stdarg_syntax(int n, ...)
2848 int i;
2849 va_list ap;
2850 if (1)
2851 va_start(ap, n);
2852 else
2854 i = va_arg(ap, int);
2855 printf("stdarg_void_expr: %d\n", i);
2856 (va_end(ap));
2859 void stdarg_test(void)
2861 LONG_DOUBLE ld = 1234567891234LL;
2862 struct myspace bob;
2864 vprintf1("%d %d %d\n", 1, 2, 3);
2865 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2866 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2867 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2868 vprintf1("%d %f %l %F %d %f %l %F\n",
2869 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2870 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2871 1, 2, 3, 4, 5, 6, 7, 8,
2872 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2873 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %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 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2877 "%l %l %f %f\n",
2878 1, 2, 3, 4, 5, 6, 7, 8,
2879 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2880 1234567891234LL, 987654321986LL,
2881 42.0, 43.0);
2882 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2883 "%l %l %f %f\n",
2884 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2885 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2886 1234567891234LL, 987654321986LL,
2887 42.0, 43.0);
2888 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2889 1, 2, 3, 4, 5, 6, 7, 8, ld);
2890 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2891 "%l %l %f %f %F\n",
2892 1, 2, 3, 4, 5, 6, 7, 8,
2893 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2894 1234567891234LL, 987654321986LL,
2895 42.0, 43.0, ld);
2896 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2897 "%F %l %l %f %f %F\n",
2898 1, 2, 3, 4, 5, 6, 7, 8,
2899 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2900 ld, 1234567891234LL, 987654321986LL,
2901 42.0, 43.0, ld);
2903 bob.profile = 42;
2904 stdarg_for_struct(bob, bob, bob, bob.profile);
2905 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2906 stdarg_syntax(1, 17);
2909 void whitespace_test(void)
2911 char *str;
2913 \f\v #if 1
2914 pri\
2915 ntf("whitspace:\n");\f\v
2916 #endif
2917 pf("N=%d\n", 2);
2919 #ifdef CORRECT_CR_HANDLING
2920 pri\
2921 ntf("aaa=%d\n", 3);
2922 #endif
2924 pri\
2926 ntf("min=%d\n", 4);
2928 #ifdef ACCEPT_CR_IN_STRINGS
2929 printf("len1=%d\n", strlen("
2930 "));
2931 #ifdef CORRECT_CR_HANDLING
2932 str = "
2934 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
2935 #endif
2936 printf("len1=%d\n", strlen(" a
2937 "));
2938 #endif /* ACCEPT_CR_IN_STRINGS */
2941 int reltab[3] = { 1, 2, 3 };
2943 int *rel1 = &reltab[1];
2944 int *rel2 = &reltab[2];
2946 #ifdef _WIN64
2947 void relocation_test(void) {}
2948 #else
2949 void getmyaddress(void)
2951 printf("in getmyaddress\n");
2954 #ifdef __LP64__
2955 long __pa_symbol(void)
2957 /* This 64bit constant was handled incorrectly, it was used as addend
2958 (which can hold 64bit just fine) in connection with a symbol,
2959 and TCC generates wrong code for that (displacements are 32bit only).
2960 This effectively is "+ 0x80000000", and if addresses of globals
2961 are below 2GB the result should be a number without high 32 bits set. */
2962 return ((long)(((unsigned long)(&rel1))) - (0xffffffff80000000UL));
2964 #endif
2966 unsigned long theaddress = (unsigned long)getmyaddress;
2967 void relocation_test(void)
2969 void (*fptr)(void) = (void (*)(void))theaddress;
2970 printf("*rel1=%d\n", *rel1);
2971 printf("*rel2=%d\n", *rel2);
2972 fptr();
2973 #ifdef __LP64__
2974 printf("pa_symbol=0x%lx\n", __pa_symbol() >> 63);
2975 #endif
2977 #endif
2979 void old_style_f(a,b,c)
2980 int a, b;
2981 double c;
2983 printf("a=%d b=%d b=%f\n", a, b, c);
2986 void decl_func1(int cmpfn())
2988 printf("cmpfn=%lx\n", (long)cmpfn);
2991 void decl_func2(cmpfn)
2992 int cmpfn();
2994 printf("cmpfn=%lx\n", (long)cmpfn);
2997 void old_style_function(void)
2999 old_style_f((void *)1, 2, 3.0);
3000 decl_func1(NULL);
3001 decl_func2(NULL);
3004 void alloca_test()
3006 #if defined __i386__ || defined __x86_64__ || defined __arm__
3007 char *p = alloca(16);
3008 strcpy(p,"123456789012345");
3009 printf("alloca: p is %s\n", p);
3010 char *demo = "This is only a test.\n";
3011 /* Test alloca embedded in a larger expression */
3012 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
3013 #endif
3016 void *bounds_checking_is_enabled()
3018 char ca[10], *cp = ca-1;
3019 return (ca != cp + 1) ? cp : NULL;
3022 typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
3024 void c99_vla_test(int size1, int size2)
3026 #if defined __i386__ || defined __x86_64__
3027 int size = size1 * size2;
3028 int tab1[size][2], tab2[10][2];
3029 void *tab1_ptr, *tab2_ptr, *bad_ptr;
3031 /* "size" should have been 'captured' at tab1 declaration,
3032 so modifying it should have no effect on VLA behaviour. */
3033 size = size-1;
3035 printf("Test C99 VLA 1 (sizeof): ");
3036 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
3037 tab1_ptr = tab1;
3038 tab2_ptr = tab2;
3039 printf("Test C99 VLA 2 (ptrs subtract): ");
3040 printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
3041 printf("Test C99 VLA 3 (ptr add): ");
3042 printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
3043 printf("Test C99 VLA 4 (ptr access): ");
3044 tab1[size1][1] = 42;
3045 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
3047 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
3048 if (bad_ptr = bounds_checking_is_enabled()) {
3049 int *t1 = &tab1[size1 * size2 - 1][3];
3050 int *t2 = &tab2[9][3];
3051 printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
3052 printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
3054 char*c1 = 1 + sizeof(tab1) + (char*)tab1;
3055 char*c2 = 1 + sizeof(tab2) + (char*)tab2;
3056 printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
3057 printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
3059 int *i1 = tab1[-1];
3060 int *i2 = tab2[-1];
3061 printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
3062 printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
3064 int *x1 = tab1[size1 * size2 + 1];
3065 int *x2 = tab2[10 + 1];
3066 printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
3067 printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
3068 } else {
3069 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
3071 printf("\n");
3072 #endif
3075 void sizeof_test(void)
3077 int a;
3078 int **ptr;
3080 printf("sizeof(int) = %d\n", sizeof(int));
3081 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
3082 printf("sizeof(long) = %d\n", sizeof(long));
3083 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
3084 printf("sizeof(short) = %d\n", sizeof(short));
3085 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
3086 printf("sizeof(char) = %d\n", sizeof(char));
3087 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
3088 printf("sizeof(func) = %d\n", sizeof sizeof_test());
3089 a = 1;
3090 printf("sizeof(a++) = %d\n", sizeof a++);
3091 printf("a=%d\n", a);
3092 ptr = NULL;
3093 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
3095 /* The type of sizeof should be as large as a pointer, actually
3096 it should be size_t. */
3097 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
3098 uintptr_t t = 1;
3099 uintptr_t t2;
3100 /* Effectively <<32, but defined also on 32bit machines. */
3101 t <<= 16;
3102 t <<= 16;
3103 t++;
3104 /* This checks that sizeof really can be used to manipulate
3105 uintptr_t objects, without truncation. */
3106 t2 = t & -sizeof(uintptr_t);
3107 printf ("%lu %lu\n", t, t2);
3109 /* some alignof tests */
3110 printf("__alignof__(int) = %d\n", __alignof__(int));
3111 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
3112 printf("__alignof__(short) = %d\n", __alignof__(short));
3113 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
3114 printf("__alignof__(char) = %d\n", __alignof__(char));
3115 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
3116 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
3118 /* sizes of VLAs need to be evaluated even inside sizeof: */
3119 a = 2;
3120 printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a]));
3121 /* And checking if sizeof compound literal works. Parenthesized: */
3122 printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n",
3123 sizeof( (struct {int i; int j;}){4,5} ));
3124 /* And as direct sizeof argument (as unary expression): */
3125 printf("sizeof (struct {short i; short j;}){4,5} = %d\n",
3126 sizeof (struct {short i; short j;}){4,5} );
3128 /* sizeof(x && y) should be sizeof(int), even if constant
3129 evaluating is possible. */
3130 printf("sizeof(t && 0) = %d\n", sizeof(t && 0));
3131 printf("sizeof(1 && 1) = %d\n", sizeof(1 && 1));
3132 printf("sizeof(t || 1) = %d\n", sizeof(t || 1));
3133 printf("sizeof(0 || 0) = %d\n", sizeof(0 || 0));
3136 void typeof_test(void)
3138 double a;
3139 typeof(a) b;
3140 typeof(float) c;
3142 a = 1.5;
3143 b = 2.5;
3144 c = 3.5;
3145 printf("a=%f b=%f c=%f\n", a, b, c);
3149 struct hlist_node;
3150 struct hlist_head {
3151 struct hlist_node *first, *last;
3154 void consume_ulong (unsigned long i)
3156 i = 0;
3159 void statement_expr_test(void)
3161 int a, i;
3163 /* Basic stmt expr test */
3164 a = 0;
3165 for(i=0;i<10;i++) {
3166 a += 1 +
3167 ( { int b, j;
3168 b = 0;
3169 for(j=0;j<5;j++)
3170 b += j; b;
3171 } );
3173 printf("a=%d\n", a);
3175 /* Test that symbols aren't freed prematurely.
3176 With SYM_DEBUG valgrind will show a read from a freed
3177 symbol, and tcc will show an (invalid) warning on the initialization
3178 of 'ptr' below, if symbols are popped after the stmt expr. */
3179 void *v = (void*)39;
3180 typeof(({
3181 (struct hlist_node *)v;
3182 })) x;
3183 typeof (x)
3184 ptr = (struct hlist_node *)v;
3186 /* This part used to segfault when symbols were popped prematurely.
3187 The symbols for the static local would be overwritten with
3188 helper symbols from the pre-processor expansions in between. */
3189 #define some_attr __attribute__((aligned(1)))
3190 #define tps(str) ({ \
3191 static const char *t some_attr = str; \
3192 t; \
3194 printf ("stmtexpr: %s %s\n",
3195 tps("somerandomlongstring"),
3196 tps("anotherlongstring"));
3198 /* Test that the three decls of 't' don't interact. */
3199 int t = 40;
3200 int b = ({ int t = 41; t; });
3201 int c = ({ int t = 42; t; });
3203 /* Test that aggregate return values work. */
3204 struct hlist_head h
3205 = ({
3206 typedef struct hlist_head T;
3207 long pre = 48;
3208 T t = { (void*)43, (void*)44 };
3209 long post = 49;
3212 printf ("stmtexpr: %d %d %d\n", t, b, c);
3213 printf ("stmtexpr: %ld %ld\n", (long)h.first, (long)h.last);
3215 /* Test that we can give out addresses of local labels. */
3216 consume_ulong(({ __label__ __here; __here: (unsigned long)&&__here; }));
3218 /* Test interaction between local and global label stacks and the
3219 need to defer popping symbol from them when within statement
3220 expressions. Note how the labels are both named LBL. */
3221 i = 0;
3224 __label__ LBL;
3225 LBL: if (i++ == 0) goto LBL;
3227 /* jump to a classical label out of an expr-stmt that had previously
3228 overshadowed that classical label */
3229 goto LBL;
3231 LBL:
3232 printf("stmtexpr: %d should be 2\n", i);
3235 void local_label_test(void)
3237 int a;
3238 goto l1;
3240 a = 1 + ({
3241 __label__ l1, l2, l3, l4;
3242 goto l1;
3244 printf("aa1\n");
3245 goto l3;
3247 printf("aa3\n");
3248 goto l4;
3250 printf("aa2\n");
3251 goto l2;
3252 l3:;
3255 printf("a=%d\n", a);
3256 return;
3258 printf("bb1\n");
3259 goto l2;
3261 printf("bb2\n");
3262 goto l4;
3265 /* inline assembler test */
3266 #if defined(__i386__) || defined(__x86_64__)
3268 /* from linux kernel */
3269 static char * strncat1(char * dest,const char * src,size_t count)
3271 long d0, d1, d2, d3;
3272 __asm__ __volatile__(
3273 "repne\n\t"
3274 "scasb\n\t"
3275 "dec %1\n\t"
3276 "mov %8,%3\n"
3277 "1:\tdec %3\n\t"
3278 "js 2f\n\t"
3279 "lodsb\n\t"
3280 "stosb\n\t"
3281 "testb %%al,%%al\n\t"
3282 "jne 1b\n"
3283 "2:\txor %2,%2\n\t"
3284 "stosb"
3285 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
3286 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
3287 : "memory");
3288 return dest;
3291 static char * strncat2(char * dest,const char * src,size_t count)
3293 long d0, d1, d2, d3;
3294 __asm__ __volatile__(
3295 "repne scasb\n\t" /* one-line repne prefix + string op */
3296 "dec %1\n\t"
3297 "mov %8,%3\n"
3298 "1:\tdec %3\n\t"
3299 "js 2f\n\t"
3300 "lodsb\n\t"
3301 "stosb\n\t"
3302 "testb %%al,%%al\n\t"
3303 "jne 1b\n"
3304 "2:\txor %2,%2\n\t"
3305 "stosb"
3306 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
3307 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
3308 : "memory");
3309 return dest;
3312 static inline void * memcpy1(void * to, const void * from, size_t n)
3314 long d0, d1, d2;
3315 __asm__ __volatile__(
3316 "rep ; movsl\n\t"
3317 "testb $2,%b4\n\t"
3318 "je 1f\n\t"
3319 "movsw\n"
3320 "1:\ttestb $1,%b4\n\t"
3321 "je 2f\n\t"
3322 "movsb\n"
3323 "2:"
3324 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
3325 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
3326 : "memory");
3327 return (to);
3330 static inline void * memcpy2(void * to, const void * from, size_t n)
3332 long d0, d1, d2;
3333 __asm__ __volatile__(
3334 "rep movsl\n\t" /* one-line rep prefix + string op */
3335 "testb $2,%b4\n\t"
3336 "je 1f\n\t"
3337 "movsw\n"
3338 "1:\ttestb $1,%b4\n\t"
3339 "je 2f\n\t"
3340 "movsb\n"
3341 "2:"
3342 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
3343 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
3344 : "memory");
3345 return (to);
3348 static __inline__ void sigaddset1(unsigned int *set, int _sig)
3350 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
3353 static __inline__ void sigdelset1(unsigned int *set, int _sig)
3355 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc", "flags");
3358 #ifndef __APPLE__
3359 /* clang's inline asm is uncapable of 'xchgb %b0,%h0' */
3360 static __inline__ __const__ unsigned int swab32(unsigned int x)
3362 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
3363 "rorl $16,%0\n\t" /* swap words */
3364 "xchgb %b0,%h0" /* swap higher bytes */
3365 :"=" "q" (x)
3366 : "0" (x));
3367 return x;
3369 #endif
3371 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
3373 unsigned long long res;
3374 #ifdef __x86_64__
3375 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
3376 but still test the 32bit->64bit mull. */
3377 unsigned int resh, resl;
3378 __asm__("mull %2" : "=a" (resl), "=d" (resh) : "a" (a), "r" (b));
3379 res = ((unsigned long long)resh << 32) | resl;
3380 #else
3381 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
3382 #endif
3383 return res;
3386 static __inline__ unsigned long long inc64(unsigned long long a)
3388 unsigned long long res;
3389 #ifdef __x86_64__
3390 /* Using the A constraint is wrong, and increments are tested
3391 elsewhere. */
3392 res = a + 1;
3393 #else
3394 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
3395 #endif
3396 return res;
3399 struct struct123 {
3400 int a;
3401 int b;
3403 struct struct1231 {
3404 unsigned long addr;
3407 unsigned long mconstraint_test(struct struct1231 *r)
3409 unsigned long ret;
3410 unsigned int a[2];
3411 a[0] = 0;
3412 __asm__ volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
3413 : "=&r" (ret), "=m" (a)
3414 : "m" (*(struct struct123 *)r->addr));
3415 return ret + a[0];
3418 #ifdef __x86_64__
3419 int fls64(unsigned long long x)
3421 int bitpos = -1;
3422 asm("bsrq %1,%q0"
3423 : "+r" (bitpos)
3424 : "rm" (x));
3425 return bitpos + 1;
3427 #endif
3429 void other_constraints_test(void)
3431 unsigned long ret;
3432 int var;
3433 #ifndef _WIN64
3434 __asm__ volatile ("mov %P1,%0" : "=r" (ret) : "p" (&var));
3435 printf ("oc1: %d\n", ret == (unsigned long)&var);
3436 #endif
3439 #ifndef _WIN32
3440 /* Test global asm blocks playing with aliases. */
3441 void base_func(void)
3443 printf ("asmc: base\n");
3446 #ifndef __APPLE__
3447 extern void override_func1 (void);
3448 extern void override_func2 (void);
3450 asm(".weak override_func1\n.set override_func1, base_func");
3451 asm(".set override_func1, base_func");
3452 asm(".set override_func2, base_func");
3454 void override_func2 (void)
3456 printf ("asmc: override2\n");
3459 /* This checks a construct used by the linux kernel to encode
3460 references to strings by PC relative references. */
3461 extern int bug_table[] __attribute__((section("__bug_table")));
3462 char * get_asm_string (void)
3464 /* On i386 when -fPIC is enabled this would cause a compile error with GCC,
3465 the problem being the "i" constraint used with a symbolic operand
3466 resolving to a local label. That check is overly zealous as the code
3467 within the asm makes sure to use it only in PIC-possible contexts,
3468 but all GCC versions behave like so. We arrange for PIC to be disabled
3469 for compiling tcctest.c in the Makefile.
3471 Additionally the usage of 'c' in "%c0" in the template is actually wrong,
3472 as that would expect an operand that is a condition code. The operand
3473 as is (a local label) is accepted by GCC in non-PIC mode, and on x86-64.
3474 What the linux kernel really wanted is 'p' to disable the addition of '$'
3475 to the printed operand (as in "$.LC0" where the template only wants the
3476 bare operand ".LC0"). But the code below is what the linux kernel
3477 happens to use and as such is the one we want to test. */
3478 #ifndef __clang__
3479 extern int some_symbol;
3480 asm volatile (".globl some_symbol\n"
3481 "jmp .+6\n"
3482 "1:\n"
3483 "some_symbol: .long 0\n"
3484 ".pushsection __bug_table, \"a\"\n"
3485 ".globl bug_table\n"
3486 "bug_table:\n"
3487 /* The first entry (1b-2b) is unused in this test,
3488 but we include it to check if cross-section
3489 PC-relative references work. */
3490 "2:\t.long 1b - 2b, %c0 - 2b\n"
3491 ".popsection\n" : : "i" ("A string"));
3492 char * str = ((char*)bug_table) + bug_table[1];
3493 return str;
3494 #else
3495 return (char *) "A string";
3496 #endif
3499 /* This checks another constructs with local labels. */
3500 extern unsigned char alld_stuff[];
3501 asm(".data\n"
3502 ".byte 41\n"
3503 "alld_stuff:\n"
3504 "661:\n"
3505 ".byte 42\n"
3506 "662:\n"
3507 ".pushsection .data.ignore\n"
3508 ".long 661b - .\n" /* This reference to 661 generates an external sym
3509 which shouldn't somehow overwrite the offset that's
3510 already determined for it. */
3511 ".popsection\n"
3512 ".byte 662b - 661b\n" /* So that this value is undeniably 1. */);
3514 void asm_local_label_diff (void)
3516 printf ("asm_local_label_diff: %d %d\n", alld_stuff[0], alld_stuff[1]);
3518 #endif
3520 /* This checks that static local variables are available from assembler. */
3521 void asm_local_statics (void)
3523 static int localint = 41;
3524 asm("incl %0" : "+m" (localint));
3525 printf ("asm_local_statics: %d\n", localint);
3527 #endif
3529 static
3530 unsigned int set;
3532 void fancy_copy (unsigned *in, unsigned *out)
3534 asm volatile ("" : "=r" (*out) : "0" (*in));
3537 void fancy_copy2 (unsigned *in, unsigned *out)
3539 asm volatile ("mov %0,(%1)" : : "r" (*in), "r" (out) : "memory");
3542 #if defined __x86_64__ && !defined _WIN64
3543 void clobber_r12(void)
3545 asm volatile("mov $1, %%r12" ::: "r12");
3547 #endif
3549 void test_high_clobbers_really(void)
3551 #if defined __x86_64__ && !defined _WIN64
3552 register long val asm("r12");
3553 long val2;
3554 /* This tests if asm clobbers correctly save/restore callee saved
3555 registers if they are clobbered and if it's the high 8 x86-64
3556 registers. This is fragile for GCC as the constraints do not
3557 correctly capture the data flow, but good enough for us. */
3558 asm volatile("mov $0x4542, %%r12" : "=r" (val):: "memory");
3559 clobber_r12();
3560 asm volatile("mov %%r12, %0" : "=r" (val2) : "r" (val): "memory");
3561 printf("asmhc: 0x%x\n", val2);
3562 #endif
3565 void test_high_clobbers(void)
3567 #if defined __x86_64__ && !defined _WIN64
3568 long x1, x2;
3569 asm volatile("mov %%r12,%0" :: "m" (x1)); /* save r12 */
3570 test_high_clobbers_really();
3571 asm volatile("mov %%r12,%0" :: "m" (x2)); /* new r12 */
3572 asm volatile("mov %0,%%r12" :: "m" (x1)); /* restore r12 */
3573 /* should be 0 but tcc doesn't save r12 automatically, which has
3574 bad effects when gcc helds TCCState *s in r12 in tcc.c:main */
3575 //printf("r12-clobber-diff: %lx\n", x2 - x1);
3576 #endif
3579 static long cpu_number;
3580 void trace_console(long len, long len2)
3582 #ifdef __x86_64__
3583 /* This generated invalid code when the emission of the switch
3584 table isn't disabled. The asms are necessary to show the bug,
3585 normal statements don't work (they need to generate some code
3586 even under nocode_wanted, which normal statements don't do,
3587 but asms do). Also at least these number of cases is necessary
3588 to generate enough "random" bytes. They ultimately are enough
3589 to create invalid instruction patterns to which the first
3590 skip-to-decision-table jump jumps. If decision table emission
3591 is disabled all of this is no problem.
3593 It also is necessary that the switches are in a statement expression
3594 (which has the property of not being enterable from outside. no
3595 matter what). */
3596 if (0
3599 long pscr_ret__;
3600 switch(len) {
3601 case 4:
3603 long pfo_ret__;
3604 switch (len2) {
3605 case 8: printf("bla"); pfo_ret__ = 42; break;
3607 pscr_ret__ = pfo_ret__;
3609 break;
3610 case 8:
3612 long pfo_ret__;
3613 switch (len2) {
3614 case 1:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3615 case 2:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3616 case 4:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3617 case 8:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break;
3618 default: printf("impossible\n");
3620 pscr_ret__ = pfo_ret__;
3622 break;
3624 pscr_ret__;
3627 printf("huh?\n");
3629 #endif
3632 void test_asm_dead_code(void)
3634 long rdi;
3635 /* Try to make sure that xdi contains a zero, and hence will
3636 lead to a segfault if the next asm is evaluated without
3637 arguments being set up. */
3638 asm volatile ("" : "=D" (rdi) : "0" (0));
3639 (void)sizeof (({
3640 int var;
3641 /* This shouldn't trigger a segfault, either the argument
3642 registers need to be set up and the asm emitted despite
3643 this being in an unevaluated context, or both the argument
3644 setup _and_ the asm emission need to be suppressed. The latter
3645 is better. Disabling asm code gen when suppression is on
3646 also fixes the above trace_console bug, but that came earlier
3647 than asm suppression. */
3648 asm volatile ("movl $0,(%0)" : : "D" (&var) : "memory");
3649 var;
3650 }));
3653 void test_asm_call(void)
3655 #if defined __x86_64__ && !defined _WIN64
3656 static char str[] = "PATH";
3657 char *s;
3658 /* This tests if a reference to an undefined symbol from an asm
3659 block, which isn't otherwise referenced in this file, is correctly
3660 regarded as global symbol, so that it's resolved by other object files
3661 or libraries. We chose getenv here, which isn't used anywhere else
3662 in this file. (If we used e.g. printf, which is used we already
3663 would have a global symbol entry, not triggering the bug which is
3664 tested here). */
3665 /* two pushes so stack remains aligned */
3666 asm volatile ("push %%rdi; push %%rdi; mov %0, %%rdi;"
3667 #if 1 && !defined(__TINYC__) && (defined(__PIC__) || defined(__PIE__)) && !defined(__APPLE__)
3668 "call getenv@plt;"
3669 #elif defined(__APPLE__)
3670 "call _getenv;"
3671 #else
3672 "call getenv;"
3673 #endif
3674 "pop %%rdi; pop %%rdi"
3675 : "=a" (s) : "r" (str));
3676 printf("asmd: %s\n", s);
3677 #endif
3680 #if defined __x86_64__
3681 # define RX "(%rip)"
3682 #else
3683 # define RX
3684 #endif
3686 void asm_dot_test(void)
3688 #ifndef __APPLE__
3689 int x;
3690 for (x = 1;; ++x) {
3691 int r = x;
3692 switch (x) {
3693 case 1:
3694 asm(".text; lea S"RX",%eax; lea ."RX",%ecx; sub %ecx,%eax; S=.; jmp p0");
3695 case 2:
3696 #ifndef __clang__
3697 /* clangs internal assembler is broken */
3698 asm(".text; jmp .+6; .int 123; mov .-4"RX",%eax; jmp p0");
3699 #else
3700 asm(".text; mov $123, %eax; jmp p0");
3701 #endif
3702 case 3:
3703 #if !defined(_WIN32) && !defined(__clang__)
3704 asm(".pushsection \".data\"; Y=.; .int 999; X=Y; .int 456; X=.-4; .popsection");
3705 #else
3706 asm(".data; Y=.; .int 999; X=Y; .int 456; X=.-4; .text");
3707 #endif
3708 asm(".text; mov X"RX",%eax; jmp p0");
3709 case 4:
3710 #ifdef __clang__
3711 /* Bah! Clang! Doesn't want to redefine 'X' */
3712 asm(".text; mov $789,%eax; jmp p0");
3713 #else
3714 #ifndef _WIN32
3715 asm(".data; X=.; .int 789; Y=.; .int 999; .previous");
3716 #else
3717 asm(".data; X=.; .int 789; Y=.; .int 999; .text");
3718 #endif
3719 asm(".text; mov X"RX",%eax; X=Y; jmp p0");
3720 #endif
3721 case 0:
3722 asm(".text; p0=.; mov %%eax,%0;" : "=m"(r)); break;
3724 if (r == x)
3725 break;
3726 printf("asm_dot_test %d: %d\n", x, r);
3728 #endif
3731 void asm_test(void)
3733 char buf[128];
3734 unsigned int val, val2;
3735 struct struct123 s1;
3736 struct struct1231 s2 = { (unsigned long)&s1 };
3737 /* Hide the outer base_func, but check later that the inline
3738 asm block gets the outer one. */
3739 int base_func = 42;
3740 void override_func3 (void);
3741 unsigned long asmret;
3742 #ifdef BOOL_ISOC99
3743 _Bool somebool;
3744 #endif
3745 register int regvar asm("%esi");
3747 printf("inline asm:\n");
3749 // parse 0x1E-1 as 3 tokens in asm mode
3750 asm volatile ("mov $0x1E-1,%eax");
3752 /* test the no operand case */
3753 asm volatile ("xorl %eax, %eax");
3755 memcpy1(buf, "hello", 6);
3756 strncat1(buf, " worldXXXXX", 3);
3757 printf("%s\n", buf);
3759 memcpy2(buf, "hello", 6);
3760 strncat2(buf, " worldXXXXX", 3);
3761 printf("%s\n", buf);
3763 /* 'A' constraint test */
3764 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
3765 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
3767 s1.a = 42;
3768 s1.b = 43;
3769 printf("mconstraint: %d", mconstraint_test(&s2));
3770 printf(" %d %d\n", s1.a, s1.b);
3771 other_constraints_test();
3772 set = 0xff;
3773 sigdelset1(&set, 2);
3774 sigaddset1(&set, 16);
3775 /* NOTE: we test here if C labels are correctly restored after the
3776 asm statement */
3777 goto label1;
3778 label2:
3779 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
3780 printf("set=0x%x\n", set);
3781 val = 0x01020304;
3782 #ifndef __APPLE__
3783 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
3784 #endif
3785 #ifndef _WIN32
3786 #ifndef __APPLE__
3787 override_func1();
3788 override_func2();
3789 /* The base_func ref from the following inline asm should find
3790 the global one, not the local decl from this function. */
3791 asm volatile(".weak override_func3\n.set override_func3, base_func");
3792 override_func3();
3793 printf("asmstr: %s\n", get_asm_string());
3794 asm_local_label_diff();
3795 #endif
3796 asm_local_statics();
3797 #endif
3798 #ifndef __clang__
3799 /* clang can't deal with the type change */
3800 /* Check that we can also load structs of appropriate layout
3801 into registers. */
3802 asm volatile("" : "=r" (asmret) : "0"(s2));
3803 if (asmret != s2.addr)
3804 printf("asmstr: failed\n");
3805 #endif
3806 #ifdef BOOL_ISOC99
3807 /* Check that the typesize correctly sets the register size to
3808 8 bit. */
3809 asm volatile("cmp %1,%2; sete %0" : "=a"(somebool) : "r"(1), "r"(2));
3810 if (!somebool)
3811 printf("asmbool: failed\n");
3812 #endif
3813 val = 43;
3814 fancy_copy (&val, &val2);
3815 printf ("fancycpy(%d)=%d\n", val, val2);
3816 val = 44;
3817 fancy_copy2 (&val, &val2);
3818 printf ("fancycpy2(%d)=%d\n", val, val2);
3819 asm volatile ("mov $0x4243, %%esi" : "=r" (regvar));
3820 printf ("regvar=%x\n", regvar);
3821 test_high_clobbers();
3822 trace_console(8, 8);
3823 test_asm_dead_code();
3824 test_asm_call();
3825 asm_dot_test();
3826 return;
3827 label1:
3828 goto label2;
3831 #else
3833 void asm_test(void)
3837 #endif
3839 #define COMPAT_TYPE(type1, type2) \
3841 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
3842 __builtin_types_compatible_p (type1, type2));\
3845 int constant_p_var;
3847 void builtin_test(void)
3849 short s;
3850 int i;
3851 long long ll;
3852 #if GCC_MAJOR >= 3
3853 COMPAT_TYPE(int, int);
3854 COMPAT_TYPE(int, unsigned int);
3855 COMPAT_TYPE(int, char);
3856 COMPAT_TYPE(int, const int);
3857 COMPAT_TYPE(int, volatile int);
3858 COMPAT_TYPE(int *, int *);
3859 COMPAT_TYPE(int *, void *);
3860 COMPAT_TYPE(int *, const int *);
3861 COMPAT_TYPE(char *, unsigned char *);
3862 COMPAT_TYPE(char *, signed char *);
3863 COMPAT_TYPE(char *, char *);
3864 /* space is needed because tcc preprocessor introduces a space between each token */
3865 COMPAT_TYPE(char * *, void *);
3866 #endif
3867 printf("res1 = %d\n", __builtin_constant_p(1));
3868 printf("res2 = %d\n", __builtin_constant_p(1 + 2));
3869 printf("res3 = %d\n", __builtin_constant_p(&constant_p_var));
3870 printf("res4 = %d\n", __builtin_constant_p(constant_p_var));
3871 printf("res5 = %d\n", __builtin_constant_p(100000 / constant_p_var));
3872 #ifdef __clang__
3873 /* clang doesn't regard this as constant expression */
3874 printf("res6 = 1\n");
3875 #else
3876 printf("res6 = %d\n", __builtin_constant_p(i && 0));
3877 #endif
3878 printf("res7 = %d\n", __builtin_constant_p(i && 1));
3879 #ifdef __clang__
3880 /* clang doesn't regard this as constant expression */
3881 printf("res8 = 1\n");
3882 #else
3883 printf("res8 = %d\n", __builtin_constant_p(i && 0 ? i : 34));
3884 #endif
3885 s = 1;
3886 ll = 2;
3887 i = __builtin_choose_expr (1 != 0, ll, s);
3888 printf("bce: %d\n", i);
3889 i = __builtin_choose_expr (1 != 1, ll, s);
3890 printf("bce: %d\n", i);
3891 i = sizeof (__builtin_choose_expr (1, ll, s));
3892 printf("bce: %d\n", i);
3893 i = sizeof (__builtin_choose_expr (0, ll, s));
3894 printf("bce: %d\n", i);
3896 //printf("bera: %p\n", __builtin_extract_return_addr((void*)43));
3899 #ifndef _WIN32
3900 extern int __attribute__((weak)) weak_f1(void);
3901 extern int __attribute__((weak)) weak_f2(void);
3902 extern int weak_f3(void);
3903 extern int __attribute__((weak)) weak_v1;
3904 extern int __attribute__((weak)) weak_v2;
3905 extern int weak_v3;
3907 extern int (*weak_fpa)() __attribute__((weak));
3908 extern int __attribute__((weak)) (*weak_fpb)();
3909 extern __attribute__((weak)) int (*weak_fpc)();
3911 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
3912 extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
3913 extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
3914 extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
3915 extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
3916 extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
3918 #ifndef __clang__
3919 static const size_t dummy = 0;
3920 extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
3921 extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
3922 extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
3923 #endif
3925 int some_lib_func(void);
3926 int dummy_impl_of_slf(void) { return 444; }
3927 #ifndef __clang__
3928 int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
3929 #endif
3931 int weak_toolate() __attribute__((weak));
3932 int weak_toolate() { return 0; }
3934 void __attribute__((weak)) weak_test(void)
3936 printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
3937 printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
3938 printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
3939 printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
3940 printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
3941 printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
3943 printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
3944 printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
3945 printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
3947 printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
3948 printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
3949 printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
3950 printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
3951 printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
3952 printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
3953 #ifdef __clang__
3954 printf("some_lib_func=444\n");
3955 #else
3956 printf("some_lib_func=%d\n", &some_lib_func ? some_lib_func() : 0);
3957 #endif
3960 int __attribute__((weak)) weak_f2() { return 222; }
3961 int __attribute__((weak)) weak_f3() { return 333; }
3962 int __attribute__((weak)) weak_v2 = 222;
3963 int __attribute__((weak)) weak_v3 = 333;
3964 #endif
3966 void const_func(const int a)
3970 void const_warn_test(void)
3972 const_func(1);
3975 struct condstruct {
3976 int i;
3979 int getme (struct condstruct *s, int i)
3981 int i1 = (i == 0 ? 0 : s)->i;
3982 int i2 = (i == 0 ? s : 0)->i;
3983 int i3 = (i == 0 ? (void*)0 : s)->i;
3984 int i4 = (i == 0 ? s : (void*)0)->i;
3985 return i1 + i2 + i3 + i4;
3988 struct global_data
3990 int a[40];
3991 int *b[40];
3994 struct global_data global_data;
3996 int global_data_getstuff (int *, int);
3998 void global_data_callit (int i)
4000 *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
4003 int global_data_getstuff (int *p, int i)
4005 return *p + i;
4008 void global_data_test (void)
4010 global_data.a[0] = 42;
4011 global_data.b[0] = &global_data.a[0];
4012 global_data_callit (0);
4013 printf ("%d\n", global_data.a[0]);
4016 struct cmpcmpS
4018 unsigned char fill : 3;
4019 unsigned char b1 : 1;
4020 unsigned char b2 : 1;
4021 unsigned char fill2 : 3;
4024 int glob1, glob2, glob3;
4026 void compare_comparisons (struct cmpcmpS *s)
4028 if (s->b1 != (glob1 == glob2)
4029 || (s->b2 != (glob1 == glob3)))
4030 printf ("comparing comparisons broken\n");
4033 void cmp_comparison_test(void)
4035 struct cmpcmpS s;
4036 s.b1 = 1;
4037 glob1 = 42; glob2 = 42;
4038 s.b2 = 0;
4039 glob3 = 43;
4040 compare_comparisons (&s);
4043 int fcompare (double a, double b, int code)
4045 switch (code) {
4046 case 0: return a == b;
4047 case 1: return a != b;
4048 case 2: return a < b;
4049 case 3: return a >= b;
4050 case 4: return a > b;
4051 case 5: return a <= b;
4053 return 0;
4056 void math_cmp_test(void)
4058 double nan = 0.0/0.0;
4059 double one = 1.0;
4060 double two = 2.0;
4061 int comp = 0;
4062 int v;
4063 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
4065 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
4066 And it does this in various ways so that all code generation paths
4067 are checked (generating inverted tests, or non-inverted tests, or
4068 producing a 0/1 value without jumps (that's done in the fcompare
4069 function). */
4070 #define FCMP(a,b,op,iop,code) \
4071 if (fcompare (a,b,code)) \
4072 bug (a,b,op,iop,1); \
4073 if (a op b) \
4074 bug (a,b,op,iop,2); \
4075 if (a iop b) \
4077 else \
4078 bug (a,b,op,iop,3); \
4079 if ((a op b) || comp) \
4080 bug (a,b,op,iop,4); \
4081 if ((a iop b) || comp) \
4083 else \
4084 bug (a,b,op,iop,5); \
4085 if (v = !(a op b), !v) bug(a,b,op,iop,7);
4087 /* Equality tests. */
4088 FCMP(nan, nan, ==, !=, 0);
4089 FCMP(one, two, ==, !=, 0);
4090 FCMP(one, one, !=, ==, 1);
4091 /* Non-equality is a bit special. */
4092 if (!fcompare (nan, nan, 1))
4093 bug (nan, nan, !=, ==, 6);
4095 /* Relational tests on numbers. */
4096 FCMP(two, one, <, >=, 2);
4097 FCMP(one, two, >=, <, 3);
4098 FCMP(one, two, >, <=, 4);
4099 FCMP(two, one, <=, >, 5);
4101 /* Relational tests on NaNs. Note that the inverse op here is
4102 always !=, there's no operator in C that is equivalent to !(a < b),
4103 when NaNs are involved, same for the other relational ops. */
4104 FCMP(nan, nan, <, !=, 2);
4105 FCMP(nan, nan, >=, !=, 3);
4106 FCMP(nan, nan, >, !=, 4);
4107 FCMP(nan, nan, <=, !=, 5);
4110 double get100 () { return 100.0; }
4112 void callsave_test(void)
4114 #if defined __i386__ || defined __x86_64__ || defined __arm__
4115 int i, s; double *d; double t;
4116 s = sizeof (double);
4117 printf ("callsavetest: %d\n", s);
4118 d = alloca (sizeof(double));
4119 d[0] = 10.0;
4120 /* x86-64 had a bug were the next call to get100 would evict
4121 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
4122 in int type, not pointer type. When alloca returns a pointer
4123 with the high 32 bit set (which is likely on x86-64) the access
4124 generates a segfault. */
4125 i = d[0] > get100 ();
4126 printf ("%d\n", i);
4127 #endif
4131 void bfa3(ptrdiff_t str_offset)
4133 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
4135 void bfa2(ptrdiff_t str_offset)
4137 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
4138 bfa3(str_offset);
4140 void bfa1(ptrdiff_t str_offset)
4142 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
4143 bfa2(str_offset);
4146 void builtin_frame_address_test(void)
4148 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
4149 #ifndef __arm__
4150 char str[] = "__builtin_frame_address";
4151 char *fp0 = __builtin_frame_address(0);
4153 printf("str: %s\n", str);
4154 #ifndef __riscv
4155 bfa1(str-fp0);
4156 #endif
4157 #endif
4160 char via_volatile (char i)
4162 char volatile vi;
4163 vi = i;
4164 return vi;
4167 struct __attribute__((__packed__)) Spacked {
4168 char a;
4169 short b;
4170 int c;
4172 struct Spacked spacked;
4173 typedef struct __attribute__((__packed__)) {
4174 char a;
4175 short b;
4176 int c;
4177 } Spacked2;
4178 Spacked2 spacked2;
4179 typedef struct Spacked3_s {
4180 char a;
4181 short b;
4182 int c;
4183 } __attribute__((__packed__)) Spacked3;
4184 Spacked3 spacked3;
4185 struct gate_struct64 {
4186 unsigned short offset_low;
4187 unsigned short segment;
4188 unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1;
4189 unsigned short offset_middle;
4190 unsigned offset_high;
4191 unsigned zero1;
4192 } __attribute__((packed));
4193 typedef struct gate_struct64 gate_desc;
4194 gate_desc a_gate_desc;
4195 void attrib_test(void)
4197 #ifndef _WIN32
4198 printf("attr: %d %d %d %d\n", sizeof(struct Spacked),
4199 sizeof(spacked), sizeof(Spacked2), sizeof(spacked2));
4200 printf("attr: %d %d\n", sizeof(Spacked3), sizeof(spacked3));
4201 printf("attr: %d %d\n", sizeof(gate_desc), sizeof(a_gate_desc));
4202 #endif
4204 extern __attribute__((__unused__)) char * __attribute__((__unused__)) *
4205 strange_attrib_placement (void);
4207 void * __attribute__((__unused__)) get_void_ptr (void *a)
4209 return a;
4212 /* This part checks for a bug in TOK_GET (used for inline expansion),
4213 where the large long long constant left the the high bits set for
4214 the integer constant token. */
4215 static inline
4216 int __get_order(unsigned long long size)
4218 int order;
4219 size -= 0xffff880000000000ULL; // this const left high bits set in the token
4221 struct S { int i : 1; } s; // constructed for this '1'
4223 order = size;
4224 return order;
4227 /* This just forces the above inline function to be actually emitted. */
4228 int force_get_order(unsigned long s)
4230 return __get_order(s);
4233 #define pv(m) printf(sizeof (s->m + 0) == 8 ? "%016llx\n" : "%02x\n", s->m)
4235 /* Test failed when using bounds checking */
4236 void bounds_check1_test (void)
4238 struct s {
4239 int x;
4240 long long y;
4241 } _s, *s = &_s;
4242 s->x = 10;
4243 s->y = 20;
4244 pv(x);
4245 pv(y);