1 | /*
|
---|
2 | * TCC auto test program
|
---|
3 | */
|
---|
4 | #include "../config.h"
|
---|
5 |
|
---|
6 | #if GCC_MAJOR >= 3
|
---|
7 |
|
---|
8 | /* Unfortunately, gcc version < 3 does not handle that! */
|
---|
9 | #define ALL_ISOC99
|
---|
10 |
|
---|
11 | /* only gcc 3 handles _Bool correctly */
|
---|
12 | #define BOOL_ISOC99
|
---|
13 |
|
---|
14 | /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
|
---|
15 | #define CORRECT_CR_HANDLING
|
---|
16 |
|
---|
17 | #endif
|
---|
18 |
|
---|
19 | /* deprecated and no longer supported in gcc 3.3 */
|
---|
20 | //#define ACCEPT_CR_IN_STRINGS
|
---|
21 |
|
---|
22 | /* __VA_ARGS__ and __func__ support */
|
---|
23 | #define C99_MACROS
|
---|
24 |
|
---|
25 | /* test various include syntaxes */
|
---|
26 |
|
---|
27 | #define TCCLIB_INC <tcclib.h>
|
---|
28 | #define TCCLIB_INC1 <tcclib
|
---|
29 | #define TCCLIB_INC2 h>
|
---|
30 | #define TCCLIB_INC3 "tcclib"
|
---|
31 |
|
---|
32 | #include TCCLIB_INC
|
---|
33 |
|
---|
34 | #include TCCLIB_INC1.TCCLIB_INC2
|
---|
35 |
|
---|
36 | #include TCCLIB_INC1.h>
|
---|
37 |
|
---|
38 | /* gcc 3.2 does not accept that (bug ?) */
|
---|
39 | //#include TCCLIB_INC3 ".h"
|
---|
40 |
|
---|
41 | #include <tcclib.h>
|
---|
42 |
|
---|
43 | #include "tcclib.h"
|
---|
44 |
|
---|
45 | void string_test();
|
---|
46 | void expr_test();
|
---|
47 | void macro_test();
|
---|
48 | void recursive_macro_test();
|
---|
49 | void scope_test();
|
---|
50 | void forward_test();
|
---|
51 | void funcptr_test();
|
---|
52 | void loop_test();
|
---|
53 | void switch_test();
|
---|
54 | void goto_test();
|
---|
55 | void enum_test();
|
---|
56 | void typedef_test();
|
---|
57 | void struct_test();
|
---|
58 | void array_test();
|
---|
59 | void expr_ptr_test();
|
---|
60 | void bool_test();
|
---|
61 | void expr2_test();
|
---|
62 | void constant_expr_test();
|
---|
63 | void expr_cmp_test();
|
---|
64 | void char_short_test();
|
---|
65 | void init_test(void);
|
---|
66 | void compound_literal_test(void);
|
---|
67 | int kr_test();
|
---|
68 | void struct_assign_test(void);
|
---|
69 | void cast_test(void);
|
---|
70 | void bitfield_test(void);
|
---|
71 | void c99_bool_test(void);
|
---|
72 | void float_test(void);
|
---|
73 | void longlong_test(void);
|
---|
74 | void manyarg_test(void);
|
---|
75 | void stdarg_test(void);
|
---|
76 | void whitespace_test(void);
|
---|
77 | void relocation_test(void);
|
---|
78 | void old_style_function(void);
|
---|
79 | void alloca_test(void);
|
---|
80 | void c99_vla_test(int size1, int size2);
|
---|
81 | void sizeof_test(void);
|
---|
82 | void typeof_test(void);
|
---|
83 | void local_label_test(void);
|
---|
84 | void statement_expr_test(void);
|
---|
85 | void asm_test(void);
|
---|
86 | void builtin_test(void);
|
---|
87 | void weak_test(void);
|
---|
88 | void global_data_test(void);
|
---|
89 | void cmp_comparison_test(void);
|
---|
90 | void math_cmp_test(void);
|
---|
91 | void callsave_test(void);
|
---|
92 | void builtin_frame_address_test(void);
|
---|
93 |
|
---|
94 | int fib(int n);
|
---|
95 | void num(int n);
|
---|
96 | void forward_ref(void);
|
---|
97 | int isid(int c);
|
---|
98 |
|
---|
99 | #define A 2
|
---|
100 | #define N 1234 + A
|
---|
101 | #define pf printf
|
---|
102 | #define M1(a, b) (a) + (b)
|
---|
103 |
|
---|
104 | #define str\
|
---|
105 | (s) # s
|
---|
106 | #define glue(a, b) a ## b
|
---|
107 | #define xglue(a, b) glue(a, b)
|
---|
108 | #define HIGHLOW "hello"
|
---|
109 | #define LOW LOW ", world"
|
---|
110 |
|
---|
111 | static int onetwothree = 123;
|
---|
112 | #define onetwothree4 onetwothree
|
---|
113 | #define onetwothree xglue(onetwothree,4)
|
---|
114 |
|
---|
115 | #define min(a, b) ((a) < (b) ? (a) : (b))
|
---|
116 |
|
---|
117 | #ifdef C99_MACROS
|
---|
118 | #define dprintf(level,...) printf(__VA_ARGS__)
|
---|
119 | #endif
|
---|
120 |
|
---|
121 | /* gcc vararg macros */
|
---|
122 | #define dprintf1(level, fmt, args...) printf(fmt, ## args)
|
---|
123 |
|
---|
124 | #define MACRO_NOARGS()
|
---|
125 |
|
---|
126 | #define AAA 3
|
---|
127 | #undef AAA
|
---|
128 | #define AAA 4
|
---|
129 |
|
---|
130 | #if 1
|
---|
131 | #define B3 1
|
---|
132 | #elif 1
|
---|
133 | #define B3 2
|
---|
134 | #elif 0
|
---|
135 | #define B3 3
|
---|
136 | #else
|
---|
137 | #define B3 4
|
---|
138 | #endif
|
---|
139 |
|
---|
140 | #define __INT64_C(c) c ## LL
|
---|
141 | #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
|
---|
142 |
|
---|
143 | int qq(int x)
|
---|
144 | {
|
---|
145 | return x + 40;
|
---|
146 | }
|
---|
147 | #define qq(x) x
|
---|
148 |
|
---|
149 | #define spin_lock(lock) do { } while (0)
|
---|
150 | #define wq_spin_lock spin_lock
|
---|
151 | #define TEST2() wq_spin_lock(a)
|
---|
152 |
|
---|
153 | void macro_test(void)
|
---|
154 | {
|
---|
155 | printf("macro:\n"); |
---|
156 | |
---|
157 |
|
---|
158 | pf("N=%d\n", N);
|
---|
159 | printf("aaa=%d\n", AAA);
|
---|
160 |
|
---|
161 | printf("min=%d\n", min(1, min(2, -1)));
|
---|
162 |
|
---|
163 | printf("s1=%s\n", glue(HIGH, LOW));
|
---|
164 | printf("s2=%s\n", xglue(HIGH, LOW));
|
---|
165 | printf("s3=%s\n", str("c"));
|
---|
166 | printf("s4=%s\n", str(a1));
|
---|
167 | printf("B3=%d\n", B3);
|
---|
168 |
|
---|
169 | printf("onetwothree=%d\n", onetwothree);
|
---|
170 |
|
---|
171 | #ifdef A
|
---|
172 | printf("A defined\n");
|
---|
173 | #endif
|
---|
174 | #ifdef B
|
---|
175 | printf("B defined\n");
|
---|
176 | #endif
|
---|
177 | #ifdef A
|
---|
178 | printf("A defined\n");
|
---|
179 | #else
|
---|
180 | printf("A not defined\n");
|
---|
181 | #endif
|
---|
182 | #ifdef B
|
---|
183 | printf("B defined\n");
|
---|
184 | #else
|
---|
185 | printf("B not defined\n");
|
---|
186 | #endif
|
---|
187 |
|
---|
188 | #ifdef A
|
---|
189 | printf("A defined\n");
|
---|
190 | #ifdef B
|
---|
191 | printf("B1 defined\n");
|
---|
192 | #else
|
---|
193 | printf("B1 not defined\n");
|
---|
194 | #endif
|
---|
195 | #else
|
---|
196 | printf("A not defined\n");
|
---|
197 | #ifdef B
|
---|
198 | printf("B2 defined\n");
|
---|
199 | #else
|
---|
200 | printf("B2 not defined\n");
|
---|
201 | #endif
|
---|
202 | #endif
|
---|
203 |
|
---|
204 | #if 1+1
|
---|
205 | printf("test true1\n");
|
---|
206 | #endif
|
---|
207 | #if 0
|
---|
208 | printf("test true2\n");
|
---|
209 | #endif
|
---|
210 | #if 1-1
|
---|
211 | printf("test true3\n");
|
---|
212 | #endif
|
---|
213 | #if defined(A)
|
---|
214 | printf("test trueA\n");
|
---|
215 | #endif
|
---|
216 | #if defined(B)
|
---|
217 | printf("test trueB\n");
|
---|
218 | #endif
|
---|
219 |
|
---|
220 | #if 0
|
---|
221 | printf("test 0\n");
|
---|
222 | #elif 0
|
---|
223 | printf("test 1\n");
|
---|
224 | #elif 2
|
---|
225 | printf("test 2\n");
|
---|
226 | #else
|
---|
227 | printf("test 3\n");
|
---|
228 | #endif
|
---|
229 |
|
---|
230 | MACRO_NOARGS();
|
---|
231 |
|
---|
232 | #ifdef __LINE__
|
---|
233 | printf("__LINE__ defined\n");
|
---|
234 | #endif
|
---|
235 |
|
---|
236 | printf("__LINE__=%d __FILE__=%s\n",
|
---|
237 | __LINE__, __FILE__);
|
---|
238 | #line 200
|
---|
239 | printf("__LINE__=%d __FILE__=%s\n",
|
---|
240 | __LINE__, __FILE__);
|
---|
241 | #line 203 "test"
|
---|
242 | printf("__LINE__=%d __FILE__=%s\n",
|
---|
243 | __LINE__, __FILE__);
|
---|
244 | #line 227 "tcctest.c"
|
---|
245 |
|
---|
246 | /* not strictly preprocessor, but we test it there */
|
---|
247 | #ifdef C99_MACROS
|
---|
248 | printf("__func__ = %s\n", __func__);
|
---|
249 | dprintf(1, "vaarg=%d\n", 1);
|
---|
250 | #endif
|
---|
251 | dprintf1(1, "vaarg1\n");
|
---|
252 | dprintf1(1, "vaarg1=%d\n", 2);
|
---|
253 | dprintf1(1, "vaarg1=%d %d\n", 1, 2);
|
---|
254 |
|
---|
255 | /* gcc extension */
|
---|
256 | printf("func='%s'\n", __FUNCTION__);
|
---|
257 |
|
---|
258 | /* complicated macros in glibc */
|
---|
259 | printf("INT64_MIN=%Ld\n", INT64_MIN);
|
---|
260 | {
|
---|
261 | int a;
|
---|
262 | a = 1;
|
---|
263 | glue(a+, +);
|
---|
264 | printf("a=%d\n", a);
|
---|
265 | glue(a <, <= 2);
|
---|
266 | printf("a=%d\n", a);
|
---|
267 | }
|
---|
268 |
|
---|
269 | /* macro function with argument outside the macro string */
|
---|
270 | #define MF_s MF_hello
|
---|
271 | #define MF_hello(msg) printf("%s\n",msg)
|
---|
272 |
|
---|
273 | #define MF_t printf("tralala\n"); MF_hello
|
---|
274 |
|
---|
275 | MF_s("hi");
|
---|
276 | MF_t("hi");
|
---|
277 |
|
---|
278 | /* test macro substituion inside args (should not eat stream) */
|
---|
279 | printf("qq=%d\n", qq(qq)(2));
|
---|
280 |
|
---|
281 | /* test zero argument case. NOTE: gcc 2.95.x does not accept a
|
---|
282 | null argument without a space. gcc 3.2 fixes that. */
|
---|
283 |
|
---|
284 | #define qq1(x) 1
|
---|
285 | printf("qq1=%d\n", qq1( ));
|
---|
286 |
|
---|
287 | /* comment with stray handling *\
|
---|
288 | /
|
---|
289 | /* this is a valid *\/ comment */
|
---|
290 | /* this is a valid comment *\*/
|
---|
291 | // this is a valid\
|
---|
292 | comment
|
---|
293 |
|
---|
294 | /* test function macro substitution when the function name is
|
---|
295 | substituted */
|
---|
296 | TEST2();
|
---|
297 |
|
---|
298 | /* And again when the name and parenthes are separated by a
|
---|
299 | comment. */
|
---|
300 | TEST2 /* the comment */ ();
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | static void print_num(char *fn, int line, int num) {
|
---|
305 | printf("fn %s, line %d, num %d\n", fn, line, num);
|
---|
306 | }
|
---|
307 |
|
---|
308 | void recursive_macro_test(void)
|
---|
309 | {
|
---|
310 |
|
---|
311 | #define ELF32_ST_TYPE(val) ((val) & 0xf)
|
---|
312 | #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
|
---|
313 | #define STB_WEAK 2 /* Weak symbol */
|
---|
314 | #define ELFW(type) ELF##32##_##type
|
---|
315 | printf("%d\n", ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(123)));
|
---|
316 |
|
---|
317 | #define WRAP(x) x
|
---|
318 |
|
---|
319 | #define print_num(x) print_num(__FILE__,__LINE__,x)
|
---|
320 | print_num(123);
|
---|
321 | WRAP(print_num(123));
|
---|
322 | WRAP(WRAP(print_num(123)));
|
---|
323 |
|
---|
324 | static struct recursive_macro { int rm_field; } G;
|
---|
325 | #define rm_field (G.rm_field)
|
---|
326 | printf("rm_field = %d\n", rm_field);
|
---|
327 | printf("rm_field = %d\n", WRAP(rm_field));
|
---|
328 | WRAP((printf("rm_field = %d %d\n", rm_field, WRAP(rm_field))));
|
---|
329 | }
|
---|
330 |
|
---|
331 | int op(a,b)
|
---|
332 | {
|
---|
333 | return a / b;
|
---|
334 | }
|
---|
335 |
|
---|
336 | int ret(a)
|
---|
337 | {
|
---|
338 | if (a == 2)
|
---|
339 | return 1;
|
---|
340 | if (a == 3)
|
---|
341 | return 2;
|
---|
342 | return 0;
|
---|
343 | }
|
---|
344 |
|
---|
345 | void ps(const char *s)
|
---|
346 | {
|
---|
347 | int c;
|
---|
348 | while (1) {
|
---|
349 | c = *s;
|
---|
350 | if (c == 0)
|
---|
351 | break;
|
---|
352 | printf("%c", c);
|
---|
353 | s++;
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 | const char foo1_string[] = "\
|
---|
358 | bar\n\
|
---|
359 | test\14\
|
---|
360 | 1";
|
---|
361 |
|
---|
362 | void string_test()
|
---|
363 | {
|
---|
364 | unsigned int b;
|
---|
365 | printf("string:\n");
|
---|
366 | printf("\141\1423\143\n");/* dezdez test */
|
---|
367 | printf("\x41\x42\x43\x3a\n");
|
---|
368 | printf("c=%c\n", 'r');
|
---|
369 | printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
|
---|
370 | printf("foo1_string='%s'\n", foo1_string);
|
---|
371 | #if 0
|
---|
372 | printf("wstring=%S\n", L"abc");
|
---|
373 | printf("wstring=%S\n", L"abc" L"def" "ghi");
|
---|
374 | printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
|
---|
375 | printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
|
---|
376 | #endif
|
---|
377 | ps("test\n");
|
---|
378 | b = 32;
|
---|
379 | while ((b = b + 1) < 96) {
|
---|
380 | printf("%c", b);
|
---|
381 | }
|
---|
382 | printf("\n");
|
---|
383 | printf("fib=%d\n", fib(33));
|
---|
384 | b = 262144;
|
---|
385 | while (b != 0x80000000) {
|
---|
386 | num(b);
|
---|
387 | b = b * 2;
|
---|
388 | }
|
---|
389 | }
|
---|
390 |
|
---|
391 | void loop_test()
|
---|
392 | {
|
---|
393 | int i;
|
---|
394 | i = 0;
|
---|
395 | while (i < 10)
|
---|
396 | printf("%d", i++);
|
---|
397 | printf("\n");
|
---|
398 | for(i = 0; i < 10;i++)
|
---|
399 | printf("%d", i);
|
---|
400 | printf("\n");
|
---|
401 | i = 0;
|
---|
402 | do {
|
---|
403 | printf("%d", i++);
|
---|
404 | } while (i < 10);
|
---|
405 | printf("\n");
|
---|
406 |
|
---|
407 | char count = 123;
|
---|
408 | /* c99 for loop init test */
|
---|
409 | for (size_t count = 1; count < 3; count++)
|
---|
410 | printf("count=%d\n", count);
|
---|
411 | printf("count = %d\n", count);
|
---|
412 |
|
---|
413 | /* break/continue tests */
|
---|
414 | i = 0;
|
---|
415 | while (1) {
|
---|
416 | if (i == 6)
|
---|
417 | break;
|
---|
418 | i++;
|
---|
419 | if (i == 3)
|
---|
420 | continue;
|
---|
421 | printf("%d", i);
|
---|
422 | }
|
---|
423 | printf("\n");
|
---|
424 |
|
---|
425 | /* break/continue tests */
|
---|
426 | i = 0;
|
---|
427 | do {
|
---|
428 | if (i == 6)
|
---|
429 | break;
|
---|
430 | i++;
|
---|
431 | if (i == 3)
|
---|
432 | continue;
|
---|
433 | printf("%d", i);
|
---|
434 | } while(1);
|
---|
435 | printf("\n");
|
---|
436 |
|
---|
437 | for(i = 0;i < 10;i++) {
|
---|
438 | if (i == 3)
|
---|
439 | continue;
|
---|
440 | printf("%d", i);
|
---|
441 | }
|
---|
442 | printf("\n");
|
---|
443 | }
|
---|
444 |
|
---|
445 | typedef int typedef_and_label;
|
---|
446 |
|
---|
447 | void goto_test()
|
---|
448 | {
|
---|
449 | int i;
|
---|
450 | static void *label_table[3] = { &&label1, &&label2, &&label3 };
|
---|
451 |
|
---|
452 | printf("goto:\n");
|
---|
453 | i = 0;
|
---|
454 | /* This needs to parse as label, not as start of decl. */
|
---|
455 | typedef_and_label:
|
---|
456 | s_loop:
|
---|
457 | if (i >= 10)
|
---|
458 | goto s_end;
|
---|
459 | printf("%d", i);
|
---|
460 | i++;
|
---|
461 | goto s_loop;
|
---|
462 | s_end:
|
---|
463 | printf("\n");
|
---|
464 |
|
---|
465 | /* we also test computed gotos (GCC extension) */
|
---|
466 | for(i=0;i<3;i++) {
|
---|
467 | goto *label_table[i];
|
---|
468 | label1:
|
---|
469 | printf("label1\n");
|
---|
470 | goto next;
|
---|
471 | label2:
|
---|
472 | printf("label2\n");
|
---|
473 | goto next;
|
---|
474 | label3:
|
---|
475 | printf("label3\n");
|
---|
476 | next: ;
|
---|
477 | }
|
---|
478 | }
|
---|
479 |
|
---|
480 | enum {
|
---|
481 | E0,
|
---|
482 | E1 = 2,
|
---|
483 | E2 = 4,
|
---|
484 | E3,
|
---|
485 | E4,
|
---|
486 | };
|
---|
487 |
|
---|
488 | enum test {
|
---|
489 | E5 = 1000,
|
---|
490 | };
|
---|
491 |
|
---|
492 | void enum_test()
|
---|
493 | {
|
---|
494 | enum test b1;
|
---|
495 | printf("enum:\n%d %d %d %d %d %d\n",
|
---|
496 | E0, E1, E2, E3, E4, E5);
|
---|
497 | b1 = 1;
|
---|
498 | printf("b1=%d\n", b1);
|
---|
499 | }
|
---|
500 |
|
---|
501 | typedef int *my_ptr;
|
---|
502 |
|
---|
503 | typedef int mytype1;
|
---|
504 | typedef int mytype2;
|
---|
505 |
|
---|
506 | void typedef_test()
|
---|
507 | {
|
---|
508 | my_ptr a;
|
---|
509 | mytype1 mytype2;
|
---|
510 | int b;
|
---|
511 |
|
---|
512 | a = &b;
|
---|
513 | *a = 1234;
|
---|
514 | printf("typedef:\n");
|
---|
515 | printf("a=%d\n", *a);
|
---|
516 | mytype2 = 2;
|
---|
517 | printf("mytype2=%d\n", mytype2);
|
---|
518 | }
|
---|
519 |
|
---|
520 | void forward_test()
|
---|
521 | {
|
---|
522 | printf("forward:\n");
|
---|
523 | forward_ref();
|
---|
524 | forward_ref();
|
---|
525 | }
|
---|
526 |
|
---|
527 |
|
---|
528 | void forward_ref(void)
|
---|
529 | {
|
---|
530 | printf("forward ok\n");
|
---|
531 | }
|
---|
532 |
|
---|
533 | typedef struct struct1 {
|
---|
534 | int f1;
|
---|
535 | int f2, f3;
|
---|
536 | union union1 {
|
---|
537 | int v1;
|
---|
538 | int v2;
|
---|
539 | } u;
|
---|
540 | char str[3];
|
---|
541 | } struct1;
|
---|
542 |
|
---|
543 | struct struct2 {
|
---|
544 | int a;
|
---|
545 | char b;
|
---|
546 | };
|
---|
547 |
|
---|
548 | union union2 {
|
---|
549 | int w1;
|
---|
550 | int w2;
|
---|
551 | };
|
---|
552 |
|
---|
553 | struct struct1 st1, st2;
|
---|
554 |
|
---|
555 | int main(int argc, char **argv)
|
---|
556 | {
|
---|
557 | string_test();
|
---|
558 | expr_test();
|
---|
559 | macro_test();
|
---|
560 | recursive_macro_test();
|
---|
561 | scope_test();
|
---|
562 | forward_test();
|
---|
563 | funcptr_test();
|
---|
564 | loop_test();
|
---|
565 | switch_test();
|
---|
566 | goto_test();
|
---|
567 | enum_test();
|
---|
568 | typedef_test();
|
---|
569 | struct_test();
|
---|
570 | array_test();
|
---|
571 | expr_ptr_test();
|
---|
572 | bool_test();
|
---|
573 | expr2_test();
|
---|
574 | constant_expr_test();
|
---|
575 | expr_cmp_test();
|
---|
576 | char_short_test();
|
---|
577 | init_test();
|
---|
578 | compound_literal_test();
|
---|
579 | kr_test();
|
---|
580 | struct_assign_test();
|
---|
581 | cast_test();
|
---|
582 | bitfield_test();
|
---|
583 | c99_bool_test();
|
---|
584 | float_test();
|
---|
585 | longlong_test();
|
---|
586 | manyarg_test();
|
---|
587 | stdarg_test();
|
---|
588 | whitespace_test();
|
---|
589 | relocation_test();
|
---|
590 | old_style_function();
|
---|
591 | alloca_test();
|
---|
592 | c99_vla_test(5, 2);
|
---|
593 | sizeof_test();
|
---|
594 | typeof_test();
|
---|
595 | statement_expr_test();
|
---|
596 | local_label_test();
|
---|
597 | asm_test();
|
---|
598 | builtin_test();
|
---|
599 | #ifndef _WIN32
|
---|
600 | weak_test();
|
---|
601 | #endif
|
---|
602 | global_data_test();
|
---|
603 | cmp_comparison_test();
|
---|
604 | math_cmp_test();
|
---|
605 | callsave_test();
|
---|
606 | builtin_frame_address_test();
|
---|
607 | return 0;
|
---|
608 | }
|
---|
609 |
|
---|
610 | int tab[3];
|
---|
611 | int tab2[3][2];
|
---|
612 |
|
---|
613 | int g;
|
---|
614 |
|
---|
615 | void f1(g)
|
---|
616 | {
|
---|
617 | printf("g1=%d\n", g);
|
---|
618 | }
|
---|
619 |
|
---|
620 | void scope_test()
|
---|
621 | {
|
---|
622 | printf("scope:\n");
|
---|
623 | g = 2;
|
---|
624 | f1(1);
|
---|
625 | printf("g2=%d\n", g);
|
---|
626 | {
|
---|
627 | int g;
|
---|
628 | g = 3;
|
---|
629 | printf("g3=%d\n", g);
|
---|
630 | {
|
---|
631 | int g;
|
---|
632 | g = 4;
|
---|
633 | printf("g4=%d\n", g);
|
---|
634 | }
|
---|
635 | }
|
---|
636 | printf("g5=%d\n", g);
|
---|
637 | }
|
---|
638 |
|
---|
639 | void array_test()
|
---|
640 | {
|
---|
641 | int i, j, a[4];
|
---|
642 |
|
---|
643 | printf("array:\n");
|
---|
644 | printf("sizeof(a) = %d\n", sizeof(a));
|
---|
645 | printf("sizeof(\"a\") = %d\n", sizeof("a"));
|
---|
646 | #ifdef C99_MACROS
|
---|
647 | printf("sizeof(__func__) = %d\n", sizeof(__func__));
|
---|
648 | #endif
|
---|
649 | printf("sizeof tab %d\n", sizeof(tab));
|
---|
650 | printf("sizeof tab2 %d\n", sizeof tab2);
|
---|
651 | tab[0] = 1;
|
---|
652 | tab[1] = 2;
|
---|
653 | tab[2] = 3;
|
---|
654 | printf("%d %d %d\n", tab[0], tab[1], tab[2]);
|
---|
655 | for(i=0;i<3;i++)
|
---|
656 | for(j=0;j<2;j++)
|
---|
657 | tab2[i][j] = 10 * i + j;
|
---|
658 | for(i=0;i<3*2;i++) {
|
---|
659 | printf(" %3d", ((int *)tab2)[i]);
|
---|
660 | }
|
---|
661 | printf("\n");
|
---|
662 | printf("sizeof(size_t)=%d\n", sizeof(size_t));
|
---|
663 | printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
|
---|
664 | }
|
---|
665 |
|
---|
666 | void expr_test()
|
---|
667 | {
|
---|
668 | int a, b;
|
---|
669 | a = 0;
|
---|
670 | printf("%d\n", a += 1);
|
---|
671 | printf("%d\n", a -= 2);
|
---|
672 | printf("%d\n", a *= 31232132);
|
---|
673 | printf("%d\n", a /= 4);
|
---|
674 | printf("%d\n", a %= 20);
|
---|
675 | printf("%d\n", a &= 6);
|
---|
676 | printf("%d\n", a ^= 7);
|
---|
677 | printf("%d\n", a |= 8);
|
---|
678 | printf("%d\n", a >>= 3);
|
---|
679 | printf("%d\n", a <<= 4);
|
---|
680 |
|
---|
681 | a = 22321;
|
---|
682 | b = -22321;
|
---|
683 | printf("%d\n", a + 1);
|
---|
684 | printf("%d\n", a - 2);
|
---|
685 | printf("%d\n", a * 312);
|
---|
686 | printf("%d\n", a / 4);
|
---|
687 | printf("%d\n", b / 4);
|
---|
688 | printf("%d\n", (unsigned)b / 4);
|
---|
689 | printf("%d\n", a % 20);
|
---|
690 | printf("%d\n", b % 20);
|
---|
691 | printf("%d\n", (unsigned)b % 20);
|
---|
692 | printf("%d\n", a & 6);
|
---|
693 | printf("%d\n", a ^ 7);
|
---|
694 | printf("%d\n", a | 8);
|
---|
695 | printf("%d\n", a >> 3);
|
---|
696 | printf("%d\n", b >> 3);
|
---|
697 | printf("%d\n", (unsigned)b >> 3);
|
---|
698 | printf("%d\n", a << 4);
|
---|
699 | printf("%d\n", ~a);
|
---|
700 | printf("%d\n", -a);
|
---|
701 | printf("%d\n", +a);
|
---|
702 |
|
---|
703 | printf("%d\n", 12 + 1);
|
---|
704 | printf("%d\n", 12 - 2);
|
---|
705 | printf("%d\n", 12 * 312);
|
---|
706 | printf("%d\n", 12 / 4);
|
---|
707 | printf("%d\n", 12 % 20);
|
---|
708 | printf("%d\n", 12 & 6);
|
---|
709 | printf("%d\n", 12 ^ 7);
|
---|
710 | printf("%d\n", 12 | 8);
|
---|
711 | printf("%d\n", 12 >> 2);
|
---|
712 | printf("%d\n", 12 << 4);
|
---|
713 | printf("%d\n", ~12);
|
---|
714 | printf("%d\n", -12);
|
---|
715 | printf("%d\n", +12);
|
---|
716 | printf("%d %d %d %d\n",
|
---|
717 | isid('a'),
|
---|
718 | isid('g'),
|
---|
719 | isid('T'),
|
---|
720 | isid('('));
|
---|
721 | }
|
---|
722 |
|
---|
723 | int isid(int c)
|
---|
724 | {
|
---|
725 | return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
|
---|
726 | }
|
---|
727 |
|
---|
728 | /**********************/
|
---|
729 |
|
---|
730 | int vstack[10], *vstack_ptr;
|
---|
731 |
|
---|
732 | void vpush(int vt, int vc)
|
---|
733 | {
|
---|
734 | *vstack_ptr++ = vt;
|
---|
735 | *vstack_ptr++ = vc;
|
---|
736 | }
|
---|
737 |
|
---|
738 | void vpop(int *ft, int *fc)
|
---|
739 | {
|
---|
740 | *fc = *--vstack_ptr;
|
---|
741 | *ft = *--vstack_ptr;
|
---|
742 | }
|
---|
743 |
|
---|
744 | void expr2_test()
|
---|
745 | {
|
---|
746 | int a, b;
|
---|
747 |
|
---|
748 | printf("expr2:\n");
|
---|
749 | vstack_ptr = vstack;
|
---|
750 | vpush(1432432, 2);
|
---|
751 | vstack_ptr[-2] &= ~0xffffff80;
|
---|
752 | vpop(&a, &b);
|
---|
753 | printf("res= %d %d\n", a, b);
|
---|
754 | }
|
---|
755 |
|
---|
756 | void constant_expr_test()
|
---|
757 | {
|
---|
758 | int a;
|
---|
759 | printf("constant_expr:\n");
|
---|
760 | a = 3;
|
---|
761 | printf("%d\n", a * 16);
|
---|
762 | printf("%d\n", a * 1);
|
---|
763 | printf("%d\n", a + 0);
|
---|
764 | }
|
---|
765 |
|
---|
766 | int tab4[10];
|
---|
767 |
|
---|
768 | void expr_ptr_test()
|
---|
769 | {
|
---|
770 | int *p, *q;
|
---|
771 | int i = -1;
|
---|
772 |
|
---|
773 | printf("expr_ptr:\n");
|
---|
774 | p = tab4;
|
---|
775 | q = tab4 + 10;
|
---|
776 | printf("diff=%d\n", q - p);
|
---|
777 | p++;
|
---|
778 | printf("inc=%d\n", p - tab4);
|
---|
779 | p--;
|
---|
780 | printf("dec=%d\n", p - tab4);
|
---|
781 | ++p;
|
---|
782 | printf("inc=%d\n", p - tab4);
|
---|
783 | --p;
|
---|
784 | printf("dec=%d\n", p - tab4);
|
---|
785 | printf("add=%d\n", p + 3 - tab4);
|
---|
786 | printf("add=%d\n", 3 + p - tab4);
|
---|
787 |
|
---|
788 | /* check if 64bit support is ok */
|
---|
789 | q = p = 0;
|
---|
790 | q += i;
|
---|
791 | printf("%p %p %ld\n", q, p, p-q);
|
---|
792 | printf("%d %d %d %d %d %d\n",
|
---|
793 | p == q, p != q, p < q, p <= q, p >= q, p > q);
|
---|
794 | i = 0xf0000000;
|
---|
795 | p += i;
|
---|
796 | printf("%p %p %ld\n", q, p, p-q);
|
---|
797 | printf("%d %d %d %d %d %d\n",
|
---|
798 | p == q, p != q, p < q, p <= q, p >= q, p > q);
|
---|
799 | p = (int *)((char *)p + 0xf0000000);
|
---|
800 | printf("%p %p %ld\n", q, p, p-q);
|
---|
801 | printf("%d %d %d %d %d %d\n",
|
---|
802 | p == q, p != q, p < q, p <= q, p >= q, p > q);
|
---|
803 | p += 0xf0000000;
|
---|
804 | printf("%p %p %ld\n", q, p, p-q);
|
---|
805 | printf("%d %d %d %d %d %d\n",
|
---|
806 | p == q, p != q, p < q, p <= q, p >= q, p > q);
|
---|
807 | {
|
---|
808 | struct size12 {
|
---|
809 | int i, j, k;
|
---|
810 | };
|
---|
811 | struct size12 s[2], *sp = s;
|
---|
812 | int i, j;
|
---|
813 | sp->i = 42;
|
---|
814 | sp++;
|
---|
815 | j = -1;
|
---|
816 | printf("%d\n", sp[j].i);
|
---|
817 | }
|
---|
818 | }
|
---|
819 |
|
---|
820 | void expr_cmp_test()
|
---|
821 | {
|
---|
822 | int a, b;
|
---|
823 | printf("constant_expr:\n");
|
---|
824 | a = -1;
|
---|
825 | b = 1;
|
---|
826 | printf("%d\n", a == a);
|
---|
827 | printf("%d\n", a != a);
|
---|
828 |
|
---|
829 | printf("%d\n", a < b);
|
---|
830 | printf("%d\n", a <= b);
|
---|
831 | printf("%d\n", a <= a);
|
---|
832 | printf("%d\n", b >= a);
|
---|
833 | printf("%d\n", a >= a);
|
---|
834 | printf("%d\n", b > a);
|
---|
835 |
|
---|
836 | printf("%d\n", (unsigned)a < b);
|
---|
837 | printf("%d\n", (unsigned)a <= b);
|
---|
838 | printf("%d\n", (unsigned)a <= a);
|
---|
839 | printf("%d\n", (unsigned)b >= a);
|
---|
840 | printf("%d\n", (unsigned)a >= a);
|
---|
841 | printf("%d\n", (unsigned)b > a);
|
---|
842 | }
|
---|
843 |
|
---|
844 | struct empty {
|
---|
845 | };
|
---|
846 |
|
---|
847 | struct aligntest1 {
|
---|
848 | char a[10];
|
---|
849 | };
|
---|
850 |
|
---|
851 | struct aligntest2 {
|
---|
852 | int a;
|
---|
853 | char b[10];
|
---|
854 | };
|
---|
855 |
|
---|
856 | struct aligntest3 {
|
---|
857 | double a, b;
|
---|
858 | };
|
---|
859 |
|
---|
860 | struct aligntest4 {
|
---|
861 | double a[0];
|
---|
862 | };
|
---|
863 |
|
---|
864 | void struct_test()
|
---|
865 | {
|
---|
866 | struct1 *s;
|
---|
867 | union union2 u;
|
---|
868 |
|
---|
869 | printf("struct:\n");
|
---|
870 | printf("sizes: %d %d %d %d\n",
|
---|
871 | sizeof(struct struct1),
|
---|
872 | sizeof(struct struct2),
|
---|
873 | sizeof(union union1),
|
---|
874 | sizeof(union union2));
|
---|
875 | st1.f1 = 1;
|
---|
876 | st1.f2 = 2;
|
---|
877 | st1.f3 = 3;
|
---|
878 | printf("st1: %d %d %d\n",
|
---|
879 | st1.f1, st1.f2, st1.f3);
|
---|
880 | st1.u.v1 = 1;
|
---|
881 | st1.u.v2 = 2;
|
---|
882 | printf("union1: %d\n", st1.u.v1);
|
---|
883 | u.w1 = 1;
|
---|
884 | u.w2 = 2;
|
---|
885 | printf("union2: %d\n", u.w1);
|
---|
886 | s = &st2;
|
---|
887 | s->f1 = 3;
|
---|
888 | s->f2 = 2;
|
---|
889 | s->f3 = 1;
|
---|
890 | printf("st2: %d %d %d\n",
|
---|
891 | s->f1, s->f2, s->f3);
|
---|
892 | printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
|
---|
893 |
|
---|
894 | /* align / size tests */
|
---|
895 | printf("aligntest1 sizeof=%d alignof=%d\n",
|
---|
896 | sizeof(struct aligntest1), __alignof__(struct aligntest1));
|
---|
897 | printf("aligntest2 sizeof=%d alignof=%d\n",
|
---|
898 | sizeof(struct aligntest2), __alignof__(struct aligntest2));
|
---|
899 | printf("aligntest3 sizeof=%d alignof=%d\n",
|
---|
900 | sizeof(struct aligntest3), __alignof__(struct aligntest3));
|
---|
901 | printf("aligntest4 sizeof=%d alignof=%d\n",
|
---|
902 | sizeof(struct aligntest4), __alignof__(struct aligntest4));
|
---|
903 |
|
---|
904 | /* empty structures (GCC extension) */
|
---|
905 | printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
|
---|
906 | printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
|
---|
907 | }
|
---|
908 |
|
---|
909 | /* XXX: depend on endianness */
|
---|
910 | void char_short_test()
|
---|
911 | {
|
---|
912 | int var1, var2;
|
---|
913 |
|
---|
914 | printf("char_short:\n");
|
---|
915 |
|
---|
916 | var1 = 0x01020304;
|
---|
917 | var2 = 0xfffefdfc;
|
---|
918 | printf("s8=%d %d\n",
|
---|
919 | *(char *)&var1, *(char *)&var2);
|
---|
920 | printf("u8=%d %d\n",
|
---|
921 | *(unsigned char *)&var1, *(unsigned char *)&var2);
|
---|
922 | printf("s16=%d %d\n",
|
---|
923 | *(short *)&var1, *(short *)&var2);
|
---|
924 | printf("u16=%d %d\n",
|
---|
925 | *(unsigned short *)&var1, *(unsigned short *)&var2);
|
---|
926 | printf("s32=%d %d\n",
|
---|
927 | *(int *)&var1, *(int *)&var2);
|
---|
928 | printf("u32=%d %d\n",
|
---|
929 | *(unsigned int *)&var1, *(unsigned int *)&var2);
|
---|
930 | *(char *)&var1 = 0x08;
|
---|
931 | printf("var1=%x\n", var1);
|
---|
932 | *(short *)&var1 = 0x0809;
|
---|
933 | printf("var1=%x\n", var1);
|
---|
934 | *(int *)&var1 = 0x08090a0b;
|
---|
935 | printf("var1=%x\n", var1);
|
---|
936 | }
|
---|
937 |
|
---|
938 | /******************/
|
---|
939 |
|
---|
940 | typedef struct Sym {
|
---|
941 | int v;
|
---|
942 | int t;
|
---|
943 | int c;
|
---|
944 | struct Sym *next;
|
---|
945 | struct Sym *prev;
|
---|
946 | } Sym;
|
---|
947 |
|
---|
948 | #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
|
---|
949 | #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
|
---|
950 |
|
---|
951 | static int toupper1(int a)
|
---|
952 | {
|
---|
953 | return TOUPPER(a);
|
---|
954 | }
|
---|
955 |
|
---|
956 | void bool_test()
|
---|
957 | {
|
---|
958 | int *s, a, b, t, f, i;
|
---|
959 |
|
---|
960 | a = 0;
|
---|
961 | s = (void*)0;
|
---|
962 | printf("!s=%d\n", !s);
|
---|
963 |
|
---|
964 | if (!s || !s[0])
|
---|
965 | a = 1;
|
---|
966 | printf("a=%d\n", a);
|
---|
967 |
|
---|
968 | printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
|
---|
969 | printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
|
---|
970 | printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
|
---|
971 | #if 1 && 1
|
---|
972 | printf("a1\n");
|
---|
973 | #endif
|
---|
974 | #if 1 || 0
|
---|
975 | printf("a2\n");
|
---|
976 | #endif
|
---|
977 | #if 1 ? 0 : 1
|
---|
978 | printf("a3\n");
|
---|
979 | #endif
|
---|
980 | #if 0 ? 0 : 1
|
---|
981 | printf("a4\n");
|
---|
982 | #endif
|
---|
983 |
|
---|
984 | a = 4;
|
---|
985 | printf("b=%d\n", a + (0 ? 1 : a / 2));
|
---|
986 |
|
---|
987 | /* test register spilling */
|
---|
988 | a = 10;
|
---|
989 | b = 10;
|
---|
990 | a = (a + b) * ((a < b) ?
|
---|
991 | ((b - a) * (a - b)): a + b);
|
---|
992 | printf("a=%d\n", a);
|
---|
993 |
|
---|
994 | /* test complex || or && expressions */
|
---|
995 | t = 1;
|
---|
996 | f = 0;
|
---|
997 | a = 32;
|
---|
998 | printf("exp=%d\n", f == (32 <= a && a <= 3));
|
---|
999 | printf("r=%d\n", (t || f) + (t && f));
|
---|
1000 |
|
---|
1001 | /* test ? : cast */
|
---|
1002 | {
|
---|
1003 | int aspect_on;
|
---|
1004 | int aspect_native = 65536;
|
---|
1005 | double bfu_aspect = 1.0;
|
---|
1006 | int aspect;
|
---|
1007 | for(aspect_on = 0; aspect_on < 2; aspect_on++) {
|
---|
1008 | aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
|
---|
1009 | printf("aspect=%d\n", aspect);
|
---|
1010 | }
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | /* test ? : GCC extension */
|
---|
1014 | {
|
---|
1015 | static int v1 = 34 ? : -1; /* constant case */
|
---|
1016 | static int v2 = 0 ? : -1; /* constant case */
|
---|
1017 | int a = 30;
|
---|
1018 |
|
---|
1019 | printf("%d %d\n", v1, v2);
|
---|
1020 | printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | /* again complex expression */
|
---|
1024 | for(i=0;i<256;i++) {
|
---|
1025 | if (toupper1 (i) != TOUPPER (i))
|
---|
1026 | printf("error %d\n", i);
|
---|
1027 | }
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | /* GCC accepts that */
|
---|
1031 | static int tab_reinit[];
|
---|
1032 | static int tab_reinit[10];
|
---|
1033 |
|
---|
1034 | //int cinit1; /* a global variable can be defined several times without error ! */
|
---|
1035 | int cinit1;
|
---|
1036 | int cinit1;
|
---|
1037 | int cinit1 = 0;
|
---|
1038 | int *cinit2 = (int []){3, 2, 1};
|
---|
1039 |
|
---|
1040 | void compound_literal_test(void)
|
---|
1041 | {
|
---|
1042 | int *p, i;
|
---|
1043 | char *q, *q3;
|
---|
1044 |
|
---|
1045 | printf("compound_test:\n");
|
---|
1046 |
|
---|
1047 | p = (int []){1, 2, 3};
|
---|
1048 | for(i=0;i<3;i++)
|
---|
1049 | printf(" %d", p[i]);
|
---|
1050 | printf("\n");
|
---|
1051 |
|
---|
1052 | for(i=0;i<3;i++)
|
---|
1053 | printf("%d", cinit2[i]);
|
---|
1054 | printf("\n");
|
---|
1055 |
|
---|
1056 | q = "tralala1";
|
---|
1057 | printf("q1=%s\n", q);
|
---|
1058 |
|
---|
1059 | q = (char *){ "tralala2" };
|
---|
1060 | printf("q2=%s\n", q);
|
---|
1061 |
|
---|
1062 | q3 = (char *){ q };
|
---|
1063 | printf("q3=%s\n", q3);
|
---|
1064 |
|
---|
1065 | q = (char []){ "tralala3" };
|
---|
1066 | printf("q4=%s\n", q);
|
---|
1067 |
|
---|
1068 | #ifdef ALL_ISOC99
|
---|
1069 | p = (int []){1, 2, cinit1 + 3};
|
---|
1070 | for(i=0;i<3;i++)
|
---|
1071 | printf(" %d", p[i]);
|
---|
1072 | printf("\n");
|
---|
1073 |
|
---|
1074 | for(i=0;i<3;i++) {
|
---|
1075 | p = (int []){1, 2, 4 + i};
|
---|
1076 | printf("%d %d %d\n",
|
---|
1077 | p[0],
|
---|
1078 | p[1],
|
---|
1079 | p[2]);
|
---|
1080 | }
|
---|
1081 | #endif
|
---|
1082 | }
|
---|
1083 |
|
---|
1084 | /* K & R protos */
|
---|
1085 |
|
---|
1086 | kr_func1(a, b)
|
---|
1087 | {
|
---|
1088 | return a + b;
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | int kr_func2(a, b)
|
---|
1092 | {
|
---|
1093 | return a + b;
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | kr_test()
|
---|
1097 | {
|
---|
1098 | printf("kr_test:\n");
|
---|
1099 | printf("func1=%d\n", kr_func1(3, 4));
|
---|
1100 | printf("func2=%d\n", kr_func2(3, 4));
|
---|
1101 | return 0;
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 | void num(int n)
|
---|
1105 | {
|
---|
1106 | char *tab, *p;
|
---|
1107 | tab = (char*)malloc(20);
|
---|
1108 | p = tab;
|
---|
1109 | while (1) {
|
---|
1110 | *p = 48 + (n % 10);
|
---|
1111 | p++;
|
---|
1112 | n = n / 10;
|
---|
1113 | if (n == 0)
|
---|
1114 | break;
|
---|
1115 | }
|
---|
1116 | while (p != tab) {
|
---|
1117 | p--;
|
---|
1118 | printf("%c", *p);
|
---|
1119 | }
|
---|
1120 | printf("\n");
|
---|
1121 | free(tab);
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | /* structure assignment tests */
|
---|
1125 | struct structa1 {
|
---|
1126 | int f1;
|
---|
1127 | char f2;
|
---|
1128 | };
|
---|
1129 |
|
---|
1130 | struct structa1 ssta1;
|
---|
1131 |
|
---|
1132 | void struct_assign_test1(struct structa1 s1, int t, float f)
|
---|
1133 | {
|
---|
1134 | printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | struct structa1 struct_assign_test2(struct structa1 s1, int t)
|
---|
1138 | {
|
---|
1139 | s1.f1 += t;
|
---|
1140 | s1.f2 -= t;
|
---|
1141 | return s1;
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 | void struct_assign_test(void)
|
---|
1145 | {
|
---|
1146 | struct S {
|
---|
1147 | struct structa1 lsta1, lsta2;
|
---|
1148 | int i;
|
---|
1149 | } s, *ps;
|
---|
1150 |
|
---|
1151 | ps = &s;
|
---|
1152 | ps->i = 4;
|
---|
1153 | #if 0
|
---|
1154 | printf("struct_assign_test:\n");
|
---|
1155 |
|
---|
1156 | s.lsta1.f1 = 1;
|
---|
1157 | s.lsta1.f2 = 2;
|
---|
1158 | printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
|
---|
1159 | s.lsta2 = s.lsta1;
|
---|
1160 | printf("%d %d\n", s.lsta2.f1, s.lsta2.f2);
|
---|
1161 | #else
|
---|
1162 | s.lsta2.f1 = 1;
|
---|
1163 | s.lsta2.f2 = 2;
|
---|
1164 | #endif
|
---|
1165 | struct_assign_test1(ps->lsta2, 3, 4.5);
|
---|
1166 |
|
---|
1167 | printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
|
---|
1168 | ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
|
---|
1169 | printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
|
---|
1170 |
|
---|
1171 | static struct {
|
---|
1172 | void (*elem)();
|
---|
1173 | } t[] = {
|
---|
1174 | /* XXX: we should allow this even without braces */
|
---|
1175 | { struct_assign_test }
|
---|
1176 | };
|
---|
1177 | printf("%d\n", struct_assign_test == t[0].elem);
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | /* casts to short/char */
|
---|
1181 |
|
---|
1182 | void cast1(char a, short b, unsigned char c, unsigned short d)
|
---|
1183 | {
|
---|
1184 | printf("%d %d %d %d\n", a, b, c, d);
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | char bcast;
|
---|
1188 | short scast;
|
---|
1189 |
|
---|
1190 | void cast_test()
|
---|
1191 | {
|
---|
1192 | int a;
|
---|
1193 | char c;
|
---|
1194 | char tab[10];
|
---|
1195 | unsigned b,d;
|
---|
1196 | short s;
|
---|
1197 | char *p = NULL;
|
---|
1198 | p -= 0x700000000042;
|
---|
1199 |
|
---|
1200 | printf("cast_test:\n");
|
---|
1201 | a = 0xfffff;
|
---|
1202 | cast1(a, a, a, a);
|
---|
1203 | a = 0xffffe;
|
---|
1204 | printf("%d %d %d %d\n",
|
---|
1205 | (char)(a + 1),
|
---|
1206 | (short)(a + 1),
|
---|
1207 | (unsigned char)(a + 1),
|
---|
1208 | (unsigned short)(a + 1));
|
---|
1209 | printf("%d %d %d %d\n",
|
---|
1210 | (char)0xfffff,
|
---|
1211 | (short)0xfffff,
|
---|
1212 | (unsigned char)0xfffff,
|
---|
1213 | (unsigned short)0xfffff);
|
---|
1214 |
|
---|
1215 | a = (bcast = 128) + 1;
|
---|
1216 | printf("%d\n", a);
|
---|
1217 | a = (scast = 65536) + 1;
|
---|
1218 | printf("%d\n", a);
|
---|
1219 |
|
---|
1220 | printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
|
---|
1221 |
|
---|
1222 | /* test cast from unsigned to signed short to int */
|
---|
1223 | b = 0xf000;
|
---|
1224 | d = (short)b;
|
---|
1225 | printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
|
---|
1226 | b = 0xf0f0;
|
---|
1227 | d = (char)b;
|
---|
1228 | printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
|
---|
1229 |
|
---|
1230 | /* test implicit int casting for array accesses */
|
---|
1231 | c = 0;
|
---|
1232 | tab[1] = 2;
|
---|
1233 | tab[c] = 1;
|
---|
1234 | printf("%d %d\n", tab[0], tab[1]);
|
---|
1235 |
|
---|
1236 | /* test implicit casting on some operators */
|
---|
1237 | printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
|
---|
1238 | printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
|
---|
1239 | printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
|
---|
1240 |
|
---|
1241 | /* from pointer to integer types */
|
---|
1242 | printf("%d %d %ld %ld %lld %lld\n",
|
---|
1243 | (int)p, (unsigned int)p,
|
---|
1244 | (long)p, (unsigned long)p,
|
---|
1245 | (long long)p, (unsigned long long)p);
|
---|
1246 |
|
---|
1247 | /* from integers to pointers */
|
---|
1248 | printf("%p %p %p %p\n",
|
---|
1249 | (void *)a, (void *)b, (void *)c, (void *)d);
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | /* initializers tests */
|
---|
1253 | struct structinit1 {
|
---|
1254 | int f1;
|
---|
1255 | char f2;
|
---|
1256 | short f3;
|
---|
1257 | int farray[3];
|
---|
1258 | };
|
---|
1259 |
|
---|
1260 | int sinit1 = 2;
|
---|
1261 | int sinit2 = { 3 };
|
---|
1262 | int sinit3[3] = { 1, 2, {{3}}, };
|
---|
1263 | int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
|
---|
1264 | int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
|
---|
1265 | int sinit6[] = { 1, 2, 3 };
|
---|
1266 | int sinit7[] = { [2] = 3, [0] = 1, 2 };
|
---|
1267 | char sinit8[] = "hello" "trala";
|
---|
1268 |
|
---|
1269 | struct structinit1 sinit9 = { 1, 2, 3 };
|
---|
1270 | struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
|
---|
1271 | struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
|
---|
1272 | #ifdef ALL_ISOC99
|
---|
1273 | .farray[0] = 10,
|
---|
1274 | .farray[1] = 11,
|
---|
1275 | .farray[2] = 12,
|
---|
1276 | #endif
|
---|
1277 | };
|
---|
1278 |
|
---|
1279 | char *sinit12 = "hello world";
|
---|
1280 | char *sinit13[] = {
|
---|
1281 | "test1",
|
---|
1282 | "test2",
|
---|
1283 | "test3",
|
---|
1284 | };
|
---|
1285 | char sinit14[10] = { "abc" };
|
---|
1286 | int sinit15[3] = { sizeof(sinit15), 1, 2 };
|
---|
1287 |
|
---|
1288 | struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
|
---|
1289 |
|
---|
1290 | struct bar {
|
---|
1291 | char *s;
|
---|
1292 | int len;
|
---|
1293 | } sinit17[] = {
|
---|
1294 | "a1", 4,
|
---|
1295 | "a2", 1
|
---|
1296 | };
|
---|
1297 |
|
---|
1298 | int sinit18[10] = {
|
---|
1299 | [2 ... 5] = 20,
|
---|
1300 | 2,
|
---|
1301 | [8] = 10,
|
---|
1302 | };
|
---|
1303 |
|
---|
1304 | struct complexinit0 {
|
---|
1305 | int a;
|
---|
1306 | int b;
|
---|
1307 | };
|
---|
1308 |
|
---|
1309 | struct complexinit {
|
---|
1310 | int a;
|
---|
1311 | const struct complexinit0 *b;
|
---|
1312 | };
|
---|
1313 |
|
---|
1314 | const static struct complexinit cix[] = {
|
---|
1315 | [0] = {
|
---|
1316 | .a = 2000,
|
---|
1317 | .b = (const struct complexinit0[]) {
|
---|
1318 | { 2001, 2002 },
|
---|
1319 | { 2003, 2003 },
|
---|
1320 | {}
|
---|
1321 | }
|
---|
1322 | }
|
---|
1323 | };
|
---|
1324 |
|
---|
1325 | struct complexinit2 {
|
---|
1326 | int a;
|
---|
1327 | int b[];
|
---|
1328 | };
|
---|
1329 |
|
---|
1330 | struct complexinit2 cix20;
|
---|
1331 |
|
---|
1332 | struct complexinit2 cix21 = {
|
---|
1333 | .a = 3000,
|
---|
1334 | .b = { 3001, 3002, 3003 }
|
---|
1335 | };
|
---|
1336 |
|
---|
1337 | struct complexinit2 cix22 = {
|
---|
1338 | .a = 4000,
|
---|
1339 | .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
|
---|
1340 | };
|
---|
1341 |
|
---|
1342 | void init_test(void)
|
---|
1343 | {
|
---|
1344 | int linit1 = 2;
|
---|
1345 | int linit2 = { 3 };
|
---|
1346 | int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
|
---|
1347 | int linit6[] = { 1, 2, 3 };
|
---|
1348 | int i, j;
|
---|
1349 | char linit8[] = "hello" "trala";
|
---|
1350 | int linit12[10] = { 1, 2 };
|
---|
1351 | int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
|
---|
1352 | char linit14[10] = "abc";
|
---|
1353 | int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
|
---|
1354 | struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
|
---|
1355 | int linit17 = sizeof(linit17);
|
---|
1356 |
|
---|
1357 | printf("init_test:\n");
|
---|
1358 |
|
---|
1359 | printf("sinit1=%d\n", sinit1);
|
---|
1360 | printf("sinit2=%d\n", sinit2);
|
---|
1361 | printf("sinit3=%d %d %d %d\n",
|
---|
1362 | sizeof(sinit3),
|
---|
1363 | sinit3[0],
|
---|
1364 | sinit3[1],
|
---|
1365 | sinit3[2]
|
---|
1366 | );
|
---|
1367 | printf("sinit6=%d\n", sizeof(sinit6));
|
---|
1368 | printf("sinit7=%d %d %d %d\n",
|
---|
1369 | sizeof(sinit7),
|
---|
1370 | sinit7[0],
|
---|
1371 | sinit7[1],
|
---|
1372 | sinit7[2]
|
---|
1373 | );
|
---|
1374 | printf("sinit8=%s\n", sinit8);
|
---|
1375 | printf("sinit9=%d %d %d\n",
|
---|
1376 | sinit9.f1,
|
---|
1377 | sinit9.f2,
|
---|
1378 | sinit9.f3
|
---|
1379 | );
|
---|
1380 | printf("sinit10=%d %d %d\n",
|
---|
1381 | sinit10.f1,
|
---|
1382 | sinit10.f2,
|
---|
1383 | sinit10.f3
|
---|
1384 | );
|
---|
1385 | printf("sinit11=%d %d %d %d %d %d\n",
|
---|
1386 | sinit11.f1,
|
---|
1387 | sinit11.f2,
|
---|
1388 | sinit11.f3,
|
---|
1389 | sinit11.farray[0],
|
---|
1390 | sinit11.farray[1],
|
---|
1391 | sinit11.farray[2]
|
---|
1392 | );
|
---|
1393 |
|
---|
1394 | for(i=0;i<3;i++)
|
---|
1395 | for(j=0;j<2;j++)
|
---|
1396 | printf("[%d][%d] = %d %d %d\n",
|
---|
1397 | i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
|
---|
1398 | printf("linit1=%d\n", linit1);
|
---|
1399 | printf("linit2=%d\n", linit2);
|
---|
1400 | printf("linit6=%d\n", sizeof(linit6));
|
---|
1401 | printf("linit8=%d %s\n", sizeof(linit8), linit8);
|
---|
1402 |
|
---|
1403 | printf("sinit12=%s\n", sinit12);
|
---|
1404 | printf("sinit13=%d %s %s %s\n",
|
---|
1405 | sizeof(sinit13),
|
---|
1406 | sinit13[0],
|
---|
1407 | sinit13[1],
|
---|
1408 | sinit13[2]);
|
---|
1409 | printf("sinit14=%s\n", sinit14);
|
---|
1410 |
|
---|
1411 | for(i=0;i<10;i++) printf(" %d", linit12[i]);
|
---|
1412 | printf("\n");
|
---|
1413 | for(i=0;i<10;i++) printf(" %d", linit13[i]);
|
---|
1414 | printf("\n");
|
---|
1415 | for(i=0;i<10;i++) printf(" %d", linit14[i]);
|
---|
1416 | printf("\n");
|
---|
1417 | for(i=0;i<10;i++) printf(" %d", linit15[i]);
|
---|
1418 | printf("\n");
|
---|
1419 | printf("%d %d %d %d\n",
|
---|
1420 | linit16.a1,
|
---|
1421 | linit16.a2,
|
---|
1422 | linit16.a3,
|
---|
1423 | linit16.a4);
|
---|
1424 | /* test that initialisation is done after variable declare */
|
---|
1425 | printf("linit17=%d\n", linit17);
|
---|
1426 | printf("sinit15=%d\n", sinit15[0]);
|
---|
1427 | printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
|
---|
1428 | printf("sinit17=%s %d %s %d\n",
|
---|
1429 | sinit17[0].s, sinit17[0].len,
|
---|
1430 | sinit17[1].s, sinit17[1].len);
|
---|
1431 | for(i=0;i<10;i++)
|
---|
1432 | printf("%x ", sinit18[i]);
|
---|
1433 | printf("\n");
|
---|
1434 | /* complex init check */
|
---|
1435 | printf("cix: %d %d %d %d %d %d %d\n",
|
---|
1436 | cix[0].a,
|
---|
1437 | cix[0].b[0].a, cix[0].b[0].b,
|
---|
1438 | cix[0].b[1].a, cix[0].b[1].b,
|
---|
1439 | cix[0].b[2].a, cix[0].b[2].b);
|
---|
1440 | printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
|
---|
1441 | printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
|
---|
1442 | }
|
---|
1443 |
|
---|
1444 |
|
---|
1445 | void switch_test()
|
---|
1446 | {
|
---|
1447 | int i;
|
---|
1448 |
|
---|
1449 | for(i=0;i<15;i++) {
|
---|
1450 | switch(i) {
|
---|
1451 | case 0:
|
---|
1452 | case 1:
|
---|
1453 | printf("a");
|
---|
1454 | break;
|
---|
1455 | default:
|
---|
1456 | printf("%d", i);
|
---|
1457 | break;
|
---|
1458 | case 8 ... 12:
|
---|
1459 | printf("c");
|
---|
1460 | break;
|
---|
1461 | case 3:
|
---|
1462 | printf("b");
|
---|
1463 | break;
|
---|
1464 | }
|
---|
1465 | }
|
---|
1466 | printf("\n");
|
---|
1467 | }
|
---|
1468 |
|
---|
1469 | /* ISOC99 _Bool type */
|
---|
1470 | void c99_bool_test(void)
|
---|
1471 | {
|
---|
1472 | #ifdef BOOL_ISOC99
|
---|
1473 | int a;
|
---|
1474 | _Bool b;
|
---|
1475 |
|
---|
1476 | printf("bool_test:\n");
|
---|
1477 | printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
|
---|
1478 | a = 3;
|
---|
1479 | printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
|
---|
1480 | b = 3;
|
---|
1481 | printf("b = %d\n", b);
|
---|
1482 | b++;
|
---|
1483 | printf("b = %d\n", b);
|
---|
1484 | #endif
|
---|
1485 | }
|
---|
1486 |
|
---|
1487 | void bitfield_test(void)
|
---|
1488 | {
|
---|
1489 | int a;
|
---|
1490 | short sa;
|
---|
1491 | unsigned char ca;
|
---|
1492 | struct sbf1 {
|
---|
1493 | int f1 : 3;
|
---|
1494 | int : 2;
|
---|
1495 | int f2 : 1;
|
---|
1496 | int : 0;
|
---|
1497 | int f3 : 5;
|
---|
1498 | int f4 : 7;
|
---|
1499 | unsigned int f5 : 7;
|
---|
1500 | } st1;
|
---|
1501 | printf("bitfield_test:");
|
---|
1502 | printf("sizeof(st1) = %d\n", sizeof(st1));
|
---|
1503 |
|
---|
1504 | st1.f1 = 3;
|
---|
1505 | st1.f2 = 1;
|
---|
1506 | st1.f3 = 15;
|
---|
1507 | a = 120;
|
---|
1508 | st1.f4 = a;
|
---|
1509 | st1.f5 = a;
|
---|
1510 | st1.f5++;
|
---|
1511 | printf("%d %d %d %d %d\n",
|
---|
1512 | st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
|
---|
1513 | sa = st1.f5;
|
---|
1514 | ca = st1.f5;
|
---|
1515 | printf("%d %d\n", sa, ca);
|
---|
1516 |
|
---|
1517 | st1.f1 = 7;
|
---|
1518 | if (st1.f1 == -1)
|
---|
1519 | printf("st1.f1 == -1\n");
|
---|
1520 | else
|
---|
1521 | printf("st1.f1 != -1\n");
|
---|
1522 | if (st1.f2 == -1)
|
---|
1523 | printf("st1.f2 == -1\n");
|
---|
1524 | else
|
---|
1525 | printf("st1.f2 != -1\n");
|
---|
1526 |
|
---|
1527 | /* bit sizes below must be bigger than 32 since GCC doesn't allow
|
---|
1528 | long-long bitfields whose size is not bigger than int */
|
---|
1529 | struct sbf2 {
|
---|
1530 | long long f1 : 45;
|
---|
1531 | long long : 2;
|
---|
1532 | long long f2 : 35;
|
---|
1533 | unsigned long long f3 : 38;
|
---|
1534 | } st2;
|
---|
1535 | st2.f1 = 0x123456789ULL;
|
---|
1536 | a = 120;
|
---|
1537 | st2.f2 = (long long)a << 25;
|
---|
1538 | st2.f3 = a;
|
---|
1539 | st2.f2++;
|
---|
1540 | printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
|
---|
1541 | }
|
---|
1542 |
|
---|
1543 | #ifdef __x86_64__
|
---|
1544 | #define FLOAT_FMT "%f\n"
|
---|
1545 | #else
|
---|
1546 | /* x86's float isn't compatible with GCC */
|
---|
1547 | #define FLOAT_FMT "%.5f\n"
|
---|
1548 | #endif
|
---|
1549 |
|
---|
1550 | /* declare strto* functions as they are C99 */
|
---|
1551 | double strtod(const char *nptr, char **endptr);
|
---|
1552 | float strtof(const char *nptr, char **endptr);
|
---|
1553 | long double strtold(const char *nptr, char **endptr);
|
---|
1554 |
|
---|
1555 | #define FTEST(prefix, type, fmt)\
|
---|
1556 | void prefix ## cmp(type a, type b)\
|
---|
1557 | {\
|
---|
1558 | printf("%d %d %d %d %d %d\n",\
|
---|
1559 | a == b,\
|
---|
1560 | a != b,\
|
---|
1561 | a < b,\
|
---|
1562 | a > b,\
|
---|
1563 | a >= b,\
|
---|
1564 | a <= b);\
|
---|
1565 | printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
|
---|
1566 | a,\
|
---|
1567 | b,\
|
---|
1568 | a + b,\
|
---|
1569 | a - b,\
|
---|
1570 | a * b,\
|
---|
1571 | a / b,\
|
---|
1572 | -a);\
|
---|
1573 | printf(fmt "\n", ++a);\
|
---|
1574 | printf(fmt "\n", a++);\
|
---|
1575 | printf(fmt "\n", a);\
|
---|
1576 | b = 0;\
|
---|
1577 | printf("%d %d\n", !a, !b);\
|
---|
1578 | }\
|
---|
1579 | void prefix ## fcast(type a)\
|
---|
1580 | {\
|
---|
1581 | float fa;\
|
---|
1582 | double da;\
|
---|
1583 | long double la;\
|
---|
1584 | int ia;\
|
---|
1585 | unsigned int ua;\
|
---|
1586 | type b;\
|
---|
1587 | fa = a;\
|
---|
1588 | da = a;\
|
---|
1589 | la = a;\
|
---|
1590 | printf("ftof: %f %f %Lf\n", fa, da, la);\
|
---|
1591 | ia = (int)a;\
|
---|
1592 | ua = (unsigned int)a;\
|
---|
1593 | printf("ftoi: %d %u\n", ia, ua);\
|
---|
1594 | ia = -1234;\
|
---|
1595 | ua = 0x81234500;\
|
---|
1596 | b = ia;\
|
---|
1597 | printf("itof: " fmt "\n", b);\
|
---|
1598 | b = ua;\
|
---|
1599 | printf("utof: " fmt "\n", b);\
|
---|
1600 | }\
|
---|
1601 | \
|
---|
1602 | float prefix ## retf(type a) { return a; }\
|
---|
1603 | double prefix ## retd(type a) { return a; }\
|
---|
1604 | long double prefix ## retld(type a) { return a; }\
|
---|
1605 | \
|
---|
1606 | void prefix ## call(void)\
|
---|
1607 | {\
|
---|
1608 | printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
|
---|
1609 | printf("double: %f\n", prefix ## retd(42.123456789));\
|
---|
1610 | printf("long double: %Lf\n", prefix ## retld(42.123456789));\
|
---|
1611 | printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
|
---|
1612 | }\
|
---|
1613 | \
|
---|
1614 | void prefix ## test(void)\
|
---|
1615 | {\
|
---|
1616 | printf("testing '%s'\n", #type);\
|
---|
1617 | prefix ## cmp(1, 2.5);\
|
---|
1618 | prefix ## cmp(2, 1.5);\
|
---|
1619 | prefix ## cmp(1, 1);\
|
---|
1620 | prefix ## fcast(234.6);\
|
---|
1621 | prefix ## fcast(-2334.6);\
|
---|
1622 | prefix ## call();\
|
---|
1623 | }
|
---|
1624 |
|
---|
1625 | FTEST(f, float, "%f")
|
---|
1626 | FTEST(d, double, "%f")
|
---|
1627 | FTEST(ld, long double, "%Lf")
|
---|
1628 |
|
---|
1629 | double ftab1[3] = { 1.2, 3.4, -5.6 };
|
---|
1630 |
|
---|
1631 |
|
---|
1632 | void float_test(void)
|
---|
1633 | {
|
---|
1634 | float fa, fb;
|
---|
1635 | double da, db;
|
---|
1636 | int a;
|
---|
1637 | unsigned int b;
|
---|
1638 |
|
---|
1639 | printf("float_test:\n");
|
---|
1640 | printf("sizeof(float) = %d\n", sizeof(float));
|
---|
1641 | printf("sizeof(double) = %d\n", sizeof(double));
|
---|
1642 | printf("sizeof(long double) = %d\n", sizeof(long double));
|
---|
1643 | ftest();
|
---|
1644 | dtest();
|
---|
1645 | ldtest();
|
---|
1646 | printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
|
---|
1647 | printf("%f %f %f\n", 2.12, .5, 2.3e10);
|
---|
1648 | // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
|
---|
1649 | da = 123;
|
---|
1650 | printf("da=%f\n", da);
|
---|
1651 | fa = 123;
|
---|
1652 | printf("fa=%f\n", fa);
|
---|
1653 | a = 4000000000;
|
---|
1654 | da = a;
|
---|
1655 | printf("da = %f\n", da);
|
---|
1656 | b = 4000000000;
|
---|
1657 | db = b;
|
---|
1658 | printf("db = %f\n", db);
|
---|
1659 | }
|
---|
1660 |
|
---|
1661 | int fib(int n)
|
---|
1662 | {
|
---|
1663 | if (n <= 2)
|
---|
1664 | return 1;
|
---|
1665 | else
|
---|
1666 | return fib(n-1) + fib(n-2);
|
---|
1667 | }
|
---|
1668 |
|
---|
1669 | void funcptr_test()
|
---|
1670 | {
|
---|
1671 | void (*func)(int);
|
---|
1672 | int a;
|
---|
1673 | struct {
|
---|
1674 | int dummy;
|
---|
1675 | void (*func)(int);
|
---|
1676 | } st1;
|
---|
1677 |
|
---|
1678 | printf("funcptr:\n");
|
---|
1679 | func = #
|
---|
1680 | (*func)(12345);
|
---|
1681 | func = num;
|
---|
1682 | a = 1;
|
---|
1683 | a = 1;
|
---|
1684 | func(12345);
|
---|
1685 | /* more complicated pointer computation */
|
---|
1686 | st1.func = num;
|
---|
1687 | st1.func(12346);
|
---|
1688 | printf("sizeof1 = %d\n", sizeof(funcptr_test));
|
---|
1689 | printf("sizeof2 = %d\n", sizeof funcptr_test);
|
---|
1690 | printf("sizeof3 = %d\n", sizeof(&funcptr_test));
|
---|
1691 | printf("sizeof4 = %d\n", sizeof &funcptr_test);
|
---|
1692 | }
|
---|
1693 |
|
---|
1694 | void lloptest(long long a, long long b)
|
---|
1695 | {
|
---|
1696 | unsigned long long ua, ub;
|
---|
1697 |
|
---|
1698 | ua = a;
|
---|
1699 | ub = b;
|
---|
1700 | /* arith */
|
---|
1701 | printf("arith: %Ld %Ld %Ld\n",
|
---|
1702 | a + b,
|
---|
1703 | a - b,
|
---|
1704 | a * b);
|
---|
1705 |
|
---|
1706 | if (b != 0) {
|
---|
1707 | printf("arith1: %Ld %Ld\n",
|
---|
1708 | a / b,
|
---|
1709 | a % b);
|
---|
1710 | }
|
---|
1711 |
|
---|
1712 | /* binary */
|
---|
1713 | printf("bin: %Ld %Ld %Ld\n",
|
---|
1714 | a & b,
|
---|
1715 | a | b,
|
---|
1716 | a ^ b);
|
---|
1717 |
|
---|
1718 | /* tests */
|
---|
1719 | printf("test: %d %d %d %d %d %d\n",
|
---|
1720 | a == b,
|
---|
1721 | a != b,
|
---|
1722 | a < b,
|
---|
1723 | a > b,
|
---|
1724 | a >= b,
|
---|
1725 | a <= b);
|
---|
1726 |
|
---|
1727 | printf("utest: %d %d %d %d %d %d\n",
|
---|
1728 | ua == ub,
|
---|
1729 | ua != ub,
|
---|
1730 | ua < ub,
|
---|
1731 | ua > ub,
|
---|
1732 | ua >= ub,
|
---|
1733 | ua <= ub);
|
---|
1734 |
|
---|
1735 | /* arith2 */
|
---|
1736 | a++;
|
---|
1737 | b++;
|
---|
1738 | printf("arith2: %Ld %Ld\n", a, b);
|
---|
1739 | printf("arith2: %Ld %Ld\n", a++, b++);
|
---|
1740 | printf("arith2: %Ld %Ld\n", --a, --b);
|
---|
1741 | printf("arith2: %Ld %Ld\n", a, b);
|
---|
1742 | b = ub = 0;
|
---|
1743 | printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
|
---|
1744 | }
|
---|
1745 |
|
---|
1746 | void llshift(long long a, int b)
|
---|
1747 | {
|
---|
1748 | printf("shift: %Ld %Ld %Ld\n",
|
---|
1749 | (unsigned long long)a >> b,
|
---|
1750 | a >> b,
|
---|
1751 | a << b);
|
---|
1752 | printf("shiftc: %Ld %Ld %Ld\n",
|
---|
1753 | (unsigned long long)a >> 3,
|
---|
1754 | a >> 3,
|
---|
1755 | a << 3);
|
---|
1756 | printf("shiftc: %Ld %Ld %Ld\n",
|
---|
1757 | (unsigned long long)a >> 35,
|
---|
1758 | a >> 35,
|
---|
1759 | a << 35);
|
---|
1760 | }
|
---|
1761 |
|
---|
1762 | void llfloat(void)
|
---|
1763 | {
|
---|
1764 | float fa;
|
---|
1765 | double da;
|
---|
1766 | long double lda;
|
---|
1767 | long long la, lb, lc;
|
---|
1768 | unsigned long long ula, ulb, ulc;
|
---|
1769 | la = 0x12345678;
|
---|
1770 | ula = 0x72345678;
|
---|
1771 | la = (la << 20) | 0x12345;
|
---|
1772 | ula = ula << 33;
|
---|
1773 | printf("la=%Ld ula=%Lu\n", la, ula);
|
---|
1774 |
|
---|
1775 | fa = la;
|
---|
1776 | da = la;
|
---|
1777 | lda = la;
|
---|
1778 | printf("lltof: %f %f %Lf\n", fa, da, lda);
|
---|
1779 |
|
---|
1780 | la = fa;
|
---|
1781 | lb = da;
|
---|
1782 | lc = lda;
|
---|
1783 | printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
|
---|
1784 |
|
---|
1785 | fa = ula;
|
---|
1786 | da = ula;
|
---|
1787 | lda = ula;
|
---|
1788 | printf("ulltof: %f %f %Lf\n", fa, da, lda);
|
---|
1789 |
|
---|
1790 | ula = fa;
|
---|
1791 | ulb = da;
|
---|
1792 | ulc = lda;
|
---|
1793 | printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
|
---|
1794 | }
|
---|
1795 |
|
---|
1796 | long long llfunc1(int a)
|
---|
1797 | {
|
---|
1798 | return a * 2;
|
---|
1799 | }
|
---|
1800 |
|
---|
1801 | struct S {
|
---|
1802 | int id;
|
---|
1803 | char item;
|
---|
1804 | };
|
---|
1805 |
|
---|
1806 | long long int value(struct S *v)
|
---|
1807 | {
|
---|
1808 | return ((long long int)v->item);
|
---|
1809 | }
|
---|
1810 |
|
---|
1811 | void longlong_test(void)
|
---|
1812 | {
|
---|
1813 | long long a, b, c;
|
---|
1814 | int ia;
|
---|
1815 | unsigned int ua;
|
---|
1816 | printf("longlong_test:\n");
|
---|
1817 | printf("sizeof(long long) = %d\n", sizeof(long long));
|
---|
1818 | ia = -1;
|
---|
1819 | ua = -2;
|
---|
1820 | a = ia;
|
---|
1821 | b = ua;
|
---|
1822 | printf("%Ld %Ld\n", a, b);
|
---|
1823 | printf("%Ld %Ld %Ld %Lx\n",
|
---|
1824 | (long long)1,
|
---|
1825 | (long long)-2,
|
---|
1826 | 1LL,
|
---|
1827 | 0x1234567812345679);
|
---|
1828 | a = llfunc1(-3);
|
---|
1829 | printf("%Ld\n", a);
|
---|
1830 |
|
---|
1831 | lloptest(1000, 23);
|
---|
1832 | lloptest(0xff, 0x1234);
|
---|
1833 | b = 0x72345678 << 10;
|
---|
1834 | lloptest(-3, b);
|
---|
1835 | llshift(0x123, 5);
|
---|
1836 | llshift(-23, 5);
|
---|
1837 | b = 0x72345678LL << 10;
|
---|
1838 | llshift(b, 47);
|
---|
1839 |
|
---|
1840 | llfloat();
|
---|
1841 | #if 1
|
---|
1842 | b = 0x12345678;
|
---|
1843 | a = -1;
|
---|
1844 | c = a + b;
|
---|
1845 | printf("%Lx\n", c);
|
---|
1846 | #endif
|
---|
1847 |
|
---|
1848 | /* long long reg spill test */
|
---|
1849 | {
|
---|
1850 | struct S a;
|
---|
1851 |
|
---|
1852 | a.item = 3;
|
---|
1853 | printf("%lld\n", value(&a));
|
---|
1854 | }
|
---|
1855 | lloptest(0x80000000, 0);
|
---|
1856 |
|
---|
1857 | /* another long long spill test */
|
---|
1858 | {
|
---|
1859 | long long *p, v;
|
---|
1860 | v = 1;
|
---|
1861 | p = &v;
|
---|
1862 | p[0]++;
|
---|
1863 | printf("%lld\n", *p);
|
---|
1864 | }
|
---|
1865 |
|
---|
1866 | a = 68719476720LL;
|
---|
1867 | b = 4294967295LL;
|
---|
1868 | printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
|
---|
1869 |
|
---|
1870 | printf("%Ld\n", 0x123456789LLU);
|
---|
1871 | }
|
---|
1872 |
|
---|
1873 | void manyarg_test(void)
|
---|
1874 | {
|
---|
1875 | long double ld = 1234567891234LL;
|
---|
1876 | printf("manyarg_test:\n");
|
---|
1877 | printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
|
---|
1878 | 1, 2, 3, 4, 5, 6, 7, 8,
|
---|
1879 | 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
|
---|
1880 | printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
|
---|
1881 | "%Ld %Ld %f %f\n",
|
---|
1882 | 1, 2, 3, 4, 5, 6, 7, 8,
|
---|
1883 | 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
|
---|
1884 | 1234567891234LL, 987654321986LL,
|
---|
1885 | 42.0, 43.0);
|
---|
1886 | printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
|
---|
1887 | "%Ld %Ld %f %f\n",
|
---|
1888 | ld, 1, 2, 3, 4, 5, 6, 7, 8,
|
---|
1889 | 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
|
---|
1890 | 1234567891234LL, 987654321986LL,
|
---|
1891 | 42.0, 43.0);
|
---|
1892 | /* XXX: known bug of x86-64 */
|
---|
1893 | #ifndef __x86_64__
|
---|
1894 | printf("%d %d %d %d %d %d %d %d %Lf\n",
|
---|
1895 | 1, 2, 3, 4, 5, 6, 7, 8, ld);
|
---|
1896 | printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
|
---|
1897 | "%Ld %Ld %f %f %Lf\n",
|
---|
1898 | 1, 2, 3, 4, 5, 6, 7, 8,
|
---|
1899 | 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
|
---|
1900 | 1234567891234LL, 987654321986LL,
|
---|
1901 | 42.0, 43.0, ld);
|
---|
1902 | printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
|
---|
1903 | "%Lf %Ld %Ld %f %f %Lf\n",
|
---|
1904 | 1, 2, 3, 4, 5, 6, 7, 8,
|
---|
1905 | 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
|
---|
1906 | ld, 1234567891234LL, 987654321986LL,
|
---|
1907 | 42.0, 43.0, ld);
|
---|
1908 | #endif
|
---|
1909 | }
|
---|
1910 |
|
---|
1911 | void vprintf1(const char *fmt, ...)
|
---|
1912 | {
|
---|
1913 | va_list ap, aq;
|
---|
1914 | const char *p;
|
---|
1915 | int c, i;
|
---|
1916 | double d;
|
---|
1917 | long long ll;
|
---|
1918 | long double ld;
|
---|
1919 |
|
---|
1920 | va_start(aq, fmt);
|
---|
1921 | va_copy(ap, aq);
|
---|
1922 |
|
---|
1923 | p = fmt;
|
---|
1924 | for(;;) {
|
---|
1925 | c = *p;
|
---|
1926 | if (c == '\0')
|
---|
1927 | break;
|
---|
1928 | p++;
|
---|
1929 | if (c == '%') {
|
---|
1930 | c = *p;
|
---|
1931 | switch(c) {
|
---|
1932 | case '\0':
|
---|
1933 | goto the_end;
|
---|
1934 | case 'd':
|
---|
1935 | i = va_arg(ap, int);
|
---|
1936 | printf("%d", i);
|
---|
1937 | break;
|
---|
1938 | case 'f':
|
---|
1939 | d = va_arg(ap, double);
|
---|
1940 | printf("%f", d);
|
---|
1941 | break;
|
---|
1942 | case 'l':
|
---|
1943 | ll = va_arg(ap, long long);
|
---|
1944 | printf("%Ld", ll);
|
---|
1945 | break;
|
---|
1946 | case 'F':
|
---|
1947 | ld = va_arg(ap, long double);
|
---|
1948 | printf("%Lf", ld);
|
---|
1949 | break;
|
---|
1950 | }
|
---|
1951 | p++;
|
---|
1952 | } else {
|
---|
1953 | putchar(c);
|
---|
1954 | }
|
---|
1955 | }
|
---|
1956 | the_end:
|
---|
1957 | va_end(aq);
|
---|
1958 | va_end(ap);
|
---|
1959 | }
|
---|
1960 |
|
---|
1961 | struct myspace {
|
---|
1962 | short int profile;
|
---|
1963 | };
|
---|
1964 |
|
---|
1965 | void stdarg_for_struct(struct myspace bob, ...)
|
---|
1966 | {
|
---|
1967 | struct myspace george, bill;
|
---|
1968 | va_list ap;
|
---|
1969 | short int validate;
|
---|
1970 |
|
---|
1971 | va_start(ap, bob);
|
---|
1972 | bill = va_arg(ap, struct myspace);
|
---|
1973 | george = va_arg(ap, struct myspace);
|
---|
1974 | validate = va_arg(ap, int);
|
---|
1975 | printf("stdarg_for_struct: %d %d %d %d\n",
|
---|
1976 | bob.profile, bill.profile, george.profile, validate);
|
---|
1977 | va_end(ap);
|
---|
1978 | }
|
---|
1979 |
|
---|
1980 | void stdarg_test(void)
|
---|
1981 | {
|
---|
1982 | long double ld = 1234567891234LL;
|
---|
1983 | struct myspace bob;
|
---|
1984 |
|
---|
1985 | vprintf1("%d %d %d\n", 1, 2, 3);
|
---|
1986 | vprintf1("%f %d %f\n", 1.0, 2, 3.0);
|
---|
1987 | vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
|
---|
1988 | vprintf1("%F %F %F\n", 1.2L, 2.3L, 3.4L);
|
---|
1989 | #ifdef __x86_64__
|
---|
1990 | /* a bug of x86's TCC */
|
---|
1991 | vprintf1("%d %f %l %F %d %f %l %F\n",
|
---|
1992 | 1, 1.2, 3L, 4.5L, 6, 7.8, 9L, 0.1L);
|
---|
1993 | #endif
|
---|
1994 | vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
|
---|
1995 | 1, 2, 3, 4, 5, 6, 7, 8,
|
---|
1996 | 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
|
---|
1997 | vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
|
---|
1998 | 1, 2, 3, 4, 5, 6, 7, 8,
|
---|
1999 | 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
|
---|
2000 | vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
|
---|
2001 | "%l %l %f %f\n",
|
---|
2002 | 1, 2, 3, 4, 5, 6, 7, 8,
|
---|
2003 | 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
|
---|
2004 | 1234567891234LL, 987654321986LL,
|
---|
2005 | 42.0, 43.0);
|
---|
2006 | vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
|
---|
2007 | "%l %l %f %f\n",
|
---|
2008 | ld, 1, 2, 3, 4, 5, 6, 7, 8,
|
---|
2009 | 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
|
---|
2010 | 1234567891234LL, 987654321986LL,
|
---|
2011 | 42.0, 43.0);
|
---|
2012 | vprintf1("%d %d %d %d %d %d %d %d %F\n",
|
---|
2013 | 1, 2, 3, 4, 5, 6, 7, 8, ld);
|
---|
2014 | vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
|
---|
2015 | "%l %l %f %f %F\n",
|
---|
2016 | 1, 2, 3, 4, 5, 6, 7, 8,
|
---|
2017 | 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
|
---|
2018 | 1234567891234LL, 987654321986LL,
|
---|
2019 | 42.0, 43.0, ld);
|
---|
2020 | vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
|
---|
2021 | "%F %l %l %f %f %F\n",
|
---|
2022 | 1, 2, 3, 4, 5, 6, 7, 8,
|
---|
2023 | 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
|
---|
2024 | ld, 1234567891234LL, 987654321986LL,
|
---|
2025 | 42.0, 43.0, ld);
|
---|
2026 |
|
---|
2027 | bob.profile = 42;
|
---|
2028 | stdarg_for_struct(bob, bob, bob, bob.profile);
|
---|
2029 | }
|
---|
2030 |
|
---|
2031 | void whitespace_test(void)
|
---|
2032 | {
|
---|
2033 | char *str;
|
---|
2034 |
|
---|
2035 | |
---|
2036 | |
---|
2037 |
|
---|
2038 | #if 1
|
---|
2039 | pri\
|
---|
2040 | ntf("whitspace:\n"); |
---|
2041 | |
---|
2042 |
|
---|
2043 | #endif
|
---|
2044 | pf("N=%d\n", 2);
|
---|
2045 |
|
---|
2046 | #ifdef CORRECT_CR_HANDLING
|
---|
2047 | pri\
|
---|
2048 | ntf("aaa=%d\n", 3);
|
---|
2049 | #endif
|
---|
2050 |
|
---|
2051 | pri\
|
---|
2052 | \
|
---|
2053 | ntf("min=%d\n", 4);
|
---|
2054 |
|
---|
2055 | #ifdef ACCEPT_CR_IN_STRINGS
|
---|
2056 | printf("len1=%d\n", strlen("
|
---|
2057 | "));
|
---|
2058 | #ifdef CORRECT_CR_HANDLING
|
---|
2059 | str = "
|
---|
2060 | ";
|
---|
2061 | printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
|
---|
2062 | #endif
|
---|
2063 | printf("len1=%d\n", strlen("
|
---|
2064 | a
|
---|
2065 | "));
|
---|
2066 | #endif /* ACCEPT_CR_IN_STRINGS */
|
---|
2067 | }
|
---|
2068 |
|
---|
2069 | int reltab[3] = { 1, 2, 3 };
|
---|
2070 |
|
---|
2071 | int *rel1 = &reltab[1];
|
---|
2072 | int *rel2 = &reltab[2];
|
---|
2073 |
|
---|
2074 | void relocation_test(void)
|
---|
2075 | {
|
---|
2076 | printf("*rel1=%d\n", *rel1);
|
---|
2077 | printf("*rel2=%d\n", *rel2);
|
---|
2078 | }
|
---|
2079 |
|
---|
2080 | void old_style_f(a,b,c)
|
---|
2081 | int a, b;
|
---|
2082 | double c;
|
---|
2083 | {
|
---|
2084 | printf("a=%d b=%d b=%f\n", a, b, c);
|
---|
2085 | }
|
---|
2086 |
|
---|
2087 | void decl_func1(int cmpfn())
|
---|
2088 | {
|
---|
2089 | printf("cmpfn=%lx\n", (long)cmpfn);
|
---|
2090 | }
|
---|
2091 |
|
---|
2092 | void decl_func2(cmpfn)
|
---|
2093 | int cmpfn();
|
---|
2094 | {
|
---|
2095 | printf("cmpfn=%lx\n", (long)cmpfn);
|
---|
2096 | }
|
---|
2097 |
|
---|
2098 | void old_style_function(void)
|
---|
2099 | {
|
---|
2100 | old_style_f((void *)1, 2, 3.0);
|
---|
2101 | decl_func1(NULL);
|
---|
2102 | decl_func2(NULL);
|
---|
2103 | }
|
---|
2104 |
|
---|
2105 | void alloca_test()
|
---|
2106 | {
|
---|
2107 | #if defined __i386__ || defined __x86_64__
|
---|
2108 | char *p = alloca(16);
|
---|
2109 | strcpy(p,"123456789012345");
|
---|
2110 | printf("alloca: p is %s\n", p);
|
---|
2111 | char *demo = "This is only a test.\n";
|
---|
2112 | /* Test alloca embedded in a larger expression */
|
---|
2113 | printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
|
---|
2114 | #endif
|
---|
2115 | }
|
---|
2116 |
|
---|
2117 | void *bounds_checking_is_enabled()
|
---|
2118 | {
|
---|
2119 | char ca[10], *cp = ca-1;
|
---|
2120 | return (ca != cp + 1) ? cp : NULL;
|
---|
2121 | }
|
---|
2122 |
|
---|
2123 | typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
|
---|
2124 |
|
---|
2125 | void c99_vla_test(int size1, int size2)
|
---|
2126 | {
|
---|
2127 | #if defined __i386__ || defined __x86_64__
|
---|
2128 | int size = size1 * size2;
|
---|
2129 | int tab1[size][2], tab2[10][2];
|
---|
2130 | void *tab1_ptr, *tab2_ptr, *bad_ptr;
|
---|
2131 |
|
---|
2132 | /* "size" should have been 'captured' at tab1 declaration,
|
---|
2133 | so modifying it should have no effect on VLA behaviour. */
|
---|
2134 | size = size-1;
|
---|
2135 |
|
---|
2136 | printf("Test C99 VLA 1 (sizeof): ");
|
---|
2137 | printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
|
---|
2138 | tab1_ptr = tab1;
|
---|
2139 | tab2_ptr = tab2;
|
---|
2140 | printf("Test C99 VLA 2 (ptrs substract): ");
|
---|
2141 | printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
|
---|
2142 | printf("Test C99 VLA 3 (ptr add): ");
|
---|
2143 | printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
|
---|
2144 | printf("Test C99 VLA 4 (ptr access): ");
|
---|
2145 | tab1[size1][1] = 42;
|
---|
2146 | printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
|
---|
2147 |
|
---|
2148 | printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
|
---|
2149 | if (bad_ptr = bounds_checking_is_enabled()) {
|
---|
2150 | int *t1 = &tab1[size1 * size2 - 1][3];
|
---|
2151 | int *t2 = &tab2[9][3];
|
---|
2152 | printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
|
---|
2153 | printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
|
---|
2154 |
|
---|
2155 | char*c1 = 1 + sizeof(tab1) + (char*)tab1;
|
---|
2156 | char*c2 = 1 + sizeof(tab2) + (char*)tab2;
|
---|
2157 | printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
|
---|
2158 | printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
|
---|
2159 |
|
---|
2160 | int *i1 = tab1[-1];
|
---|
2161 | int *i2 = tab2[-1];
|
---|
2162 | printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
|
---|
2163 | printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
|
---|
2164 |
|
---|
2165 | int *x1 = tab1[size1 * size2 + 1];
|
---|
2166 | int *x2 = tab2[10 + 1];
|
---|
2167 | printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
|
---|
2168 | printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
|
---|
2169 | } else {
|
---|
2170 | printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
|
---|
2171 | }
|
---|
2172 | printf("\n");
|
---|
2173 | #endif
|
---|
2174 | }
|
---|
2175 |
|
---|
2176 | typedef __SIZE_TYPE__ uintptr_t;
|
---|
2177 |
|
---|
2178 | void sizeof_test(void)
|
---|
2179 | {
|
---|
2180 | int a;
|
---|
2181 | int **ptr;
|
---|
2182 |
|
---|
2183 | printf("sizeof(int) = %d\n", sizeof(int));
|
---|
2184 | printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
|
---|
2185 | printf("sizeof(long) = %d\n", sizeof(long));
|
---|
2186 | printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
|
---|
2187 | printf("sizeof(short) = %d\n", sizeof(short));
|
---|
2188 | printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
|
---|
2189 | printf("sizeof(char) = %d\n", sizeof(char));
|
---|
2190 | printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
|
---|
2191 | printf("sizeof(func) = %d\n", sizeof sizeof_test());
|
---|
2192 | a = 1;
|
---|
2193 | printf("sizeof(a++) = %d\n", sizeof a++);
|
---|
2194 | printf("a=%d\n", a);
|
---|
2195 | ptr = NULL;
|
---|
2196 | printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
|
---|
2197 |
|
---|
2198 | /* The type of sizeof should be as large as a pointer, actually
|
---|
2199 | it should be size_t. */
|
---|
2200 | printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
|
---|
2201 | uintptr_t t = 1;
|
---|
2202 | uintptr_t t2;
|
---|
2203 | /* Effectively <<32, but defined also on 32bit machines. */
|
---|
2204 | t <<= 16;
|
---|
2205 | t <<= 16;
|
---|
2206 | t++;
|
---|
2207 | /* This checks that sizeof really can be used to manipulate
|
---|
2208 | uintptr_t objects, without truncation. */
|
---|
2209 | t2 = t & -sizeof(uintptr_t);
|
---|
2210 | printf ("%lu %lu\n", t, t2);
|
---|
2211 |
|
---|
2212 | /* some alignof tests */
|
---|
2213 | printf("__alignof__(int) = %d\n", __alignof__(int));
|
---|
2214 | printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
|
---|
2215 | printf("__alignof__(short) = %d\n", __alignof__(short));
|
---|
2216 | printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
|
---|
2217 | printf("__alignof__(char) = %d\n", __alignof__(char));
|
---|
2218 | printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
|
---|
2219 | printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
|
---|
2220 | }
|
---|
2221 |
|
---|
2222 | void typeof_test(void)
|
---|
2223 | {
|
---|
2224 | double a;
|
---|
2225 | typeof(a) b;
|
---|
2226 | typeof(float) c;
|
---|
2227 |
|
---|
2228 | a = 1.5;
|
---|
2229 | b = 2.5;
|
---|
2230 | c = 3.5;
|
---|
2231 | printf("a=%f b=%f c=%f\n", a, b, c);
|
---|
2232 | }
|
---|
2233 |
|
---|
2234 | void statement_expr_test(void)
|
---|
2235 | {
|
---|
2236 | int a, i;
|
---|
2237 |
|
---|
2238 | a = 0;
|
---|
2239 | for(i=0;i<10;i++) {
|
---|
2240 | a += 1 +
|
---|
2241 | ( { int b, j;
|
---|
2242 | b = 0;
|
---|
2243 | for(j=0;j<5;j++)
|
---|
2244 | b += j; b;
|
---|
2245 | } );
|
---|
2246 | }
|
---|
2247 | printf("a=%d\n", a);
|
---|
2248 |
|
---|
2249 | }
|
---|
2250 |
|
---|
2251 | void local_label_test(void)
|
---|
2252 | {
|
---|
2253 | int a;
|
---|
2254 | goto l1;
|
---|
2255 | l2:
|
---|
2256 | a = 1 + ({
|
---|
2257 | __label__ l1, l2, l3, l4;
|
---|
2258 | goto l1;
|
---|
2259 | l4:
|
---|
2260 | printf("aa1\n");
|
---|
2261 | goto l3;
|
---|
2262 | l2:
|
---|
2263 | printf("aa3\n");
|
---|
2264 | goto l4;
|
---|
2265 | l1:
|
---|
2266 | printf("aa2\n");
|
---|
2267 | goto l2;
|
---|
2268 | l3:;
|
---|
2269 | 1;
|
---|
2270 | });
|
---|
2271 | printf("a=%d\n", a);
|
---|
2272 | return;
|
---|
2273 | l4:
|
---|
2274 | printf("bb1\n");
|
---|
2275 | goto l2;
|
---|
2276 | l1:
|
---|
2277 | printf("bb2\n");
|
---|
2278 | goto l4;
|
---|
2279 | }
|
---|
2280 |
|
---|
2281 | /* inline assembler test */
|
---|
2282 | #ifdef __i386__
|
---|
2283 |
|
---|
2284 | /* from linux kernel */
|
---|
2285 | static char * strncat1(char * dest,const char * src,size_t count)
|
---|
2286 | {
|
---|
2287 | int d0, d1, d2, d3;
|
---|
2288 | __asm__ __volatile__(
|
---|
2289 | "repne\n\t"
|
---|
2290 | "scasb\n\t"
|
---|
2291 | "decl %1\n\t"
|
---|
2292 | "movl %8,%3\n"
|
---|
2293 | "1:\tdecl %3\n\t"
|
---|
2294 | "js 2f\n\t"
|
---|
2295 | "lodsb\n\t"
|
---|
2296 | "stosb\n\t"
|
---|
2297 | "testb %%al,%%al\n\t"
|
---|
2298 | "jne 1b\n"
|
---|
2299 | "2:\txorl %2,%2\n\t"
|
---|
2300 | "stosb"
|
---|
2301 | : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
|
---|
2302 | : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
|
---|
2303 | : "memory");
|
---|
2304 | return dest;
|
---|
2305 | }
|
---|
2306 |
|
---|
2307 | static char * strncat2(char * dest,const char * src,size_t count)
|
---|
2308 | {
|
---|
2309 | int d0, d1, d2, d3;
|
---|
2310 | __asm__ __volatile__(
|
---|
2311 | "repne scasb\n\t" /* one-line repne prefix + string op */
|
---|
2312 | "decl %1\n\t"
|
---|
2313 | "movl %8,%3\n"
|
---|
2314 | "1:\tdecl %3\n\t"
|
---|
2315 | "js 2f\n\t"
|
---|
2316 | "lodsb\n\t"
|
---|
2317 | "stosb\n\t"
|
---|
2318 | "testb %%al,%%al\n\t"
|
---|
2319 | "jne 1b\n"
|
---|
2320 | "2:\txorl %2,%2\n\t"
|
---|
2321 | "stosb"
|
---|
2322 | : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
|
---|
2323 | : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
|
---|
2324 | : "memory");
|
---|
2325 | return dest;
|
---|
2326 | }
|
---|
2327 |
|
---|
2328 | static inline void * memcpy1(void * to, const void * from, size_t n)
|
---|
2329 | {
|
---|
2330 | int d0, d1, d2;
|
---|
2331 | __asm__ __volatile__(
|
---|
2332 | "rep ; movsl\n\t"
|
---|
2333 | "testb $2,%b4\n\t"
|
---|
2334 | "je 1f\n\t"
|
---|
2335 | "movsw\n"
|
---|
2336 | "1:\ttestb $1,%b4\n\t"
|
---|
2337 | "je 2f\n\t"
|
---|
2338 | "movsb\n"
|
---|
2339 | "2:"
|
---|
2340 | : "=&c" (d0), "=&D" (d1), "=&S" (d2)
|
---|
2341 | :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
|
---|
2342 | : "memory");
|
---|
2343 | return (to);
|
---|
2344 | }
|
---|
2345 |
|
---|
2346 | static inline void * memcpy2(void * to, const void * from, size_t n)
|
---|
2347 | {
|
---|
2348 | int d0, d1, d2;
|
---|
2349 | __asm__ __volatile__(
|
---|
2350 | "rep movsl\n\t" /* one-line rep prefix + string op */
|
---|
2351 | "testb $2,%b4\n\t"
|
---|
2352 | "je 1f\n\t"
|
---|
2353 | "movsw\n"
|
---|
2354 | "1:\ttestb $1,%b4\n\t"
|
---|
2355 | "je 2f\n\t"
|
---|
2356 | "movsb\n"
|
---|
2357 | "2:"
|
---|
2358 | : "=&c" (d0), "=&D" (d1), "=&S" (d2)
|
---|
2359 | :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
|
---|
2360 | : "memory");
|
---|
2361 | return (to);
|
---|
2362 | }
|
---|
2363 |
|
---|
2364 | static __inline__ void sigaddset1(unsigned int *set, int _sig)
|
---|
2365 | {
|
---|
2366 | __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
|
---|
2367 | }
|
---|
2368 |
|
---|
2369 | static __inline__ void sigdelset1(unsigned int *set, int _sig)
|
---|
2370 | {
|
---|
2371 | asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
|
---|
2372 | }
|
---|
2373 |
|
---|
2374 | static __inline__ __const__ unsigned int swab32(unsigned int x)
|
---|
2375 | {
|
---|
2376 | __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
|
---|
2377 | "rorl $16,%0\n\t" /* swap words */
|
---|
2378 | "xchgb %b0,%h0" /* swap higher bytes */
|
---|
2379 | :"=q" (x)
|
---|
2380 | : "0" (x));
|
---|
2381 | return x;
|
---|
2382 | }
|
---|
2383 |
|
---|
2384 | static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
|
---|
2385 | {
|
---|
2386 | unsigned long long res;
|
---|
2387 | __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
|
---|
2388 | return res;
|
---|
2389 | }
|
---|
2390 |
|
---|
2391 | static __inline__ unsigned long long inc64(unsigned long long a)
|
---|
2392 | {
|
---|
2393 | unsigned long long res;
|
---|
2394 | __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
|
---|
2395 | return res;
|
---|
2396 | }
|
---|
2397 |
|
---|
2398 | unsigned int set;
|
---|
2399 |
|
---|
2400 | void asm_test(void)
|
---|
2401 | {
|
---|
2402 | char buf[128];
|
---|
2403 | unsigned int val;
|
---|
2404 |
|
---|
2405 | printf("inline asm:\n");
|
---|
2406 | /* test the no operand case */
|
---|
2407 | asm volatile ("xorl %eax, %eax");
|
---|
2408 |
|
---|
2409 | memcpy1(buf, "hello", 6);
|
---|
2410 | strncat1(buf, " worldXXXXX", 3);
|
---|
2411 | printf("%s\n", buf);
|
---|
2412 |
|
---|
2413 | memcpy2(buf, "hello", 6);
|
---|
2414 | strncat2(buf, " worldXXXXX", 3);
|
---|
2415 | printf("%s\n", buf);
|
---|
2416 |
|
---|
2417 | /* 'A' constraint test */
|
---|
2418 | printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
|
---|
2419 | printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
|
---|
2420 |
|
---|
2421 | set = 0xff;
|
---|
2422 | sigdelset1(&set, 2);
|
---|
2423 | sigaddset1(&set, 16);
|
---|
2424 | /* NOTE: we test here if C labels are correctly restored after the
|
---|
2425 | asm statement */
|
---|
2426 | goto label1;
|
---|
2427 | label2:
|
---|
2428 | __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
|
---|
2429 | #ifdef __GNUC__ // works strange with GCC 4.3
|
---|
2430 | set=0x1080fd;
|
---|
2431 | #endif
|
---|
2432 | printf("set=0x%x\n", set);
|
---|
2433 | val = 0x01020304;
|
---|
2434 | printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
|
---|
2435 | return;
|
---|
2436 | label1:
|
---|
2437 | goto label2;
|
---|
2438 | }
|
---|
2439 |
|
---|
2440 | #else
|
---|
2441 |
|
---|
2442 | void asm_test(void)
|
---|
2443 | {
|
---|
2444 | }
|
---|
2445 |
|
---|
2446 | #endif
|
---|
2447 |
|
---|
2448 | #define COMPAT_TYPE(type1, type2) \
|
---|
2449 | {\
|
---|
2450 | printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
|
---|
2451 | __builtin_types_compatible_p (type1, type2));\
|
---|
2452 | }
|
---|
2453 |
|
---|
2454 | int constant_p_var;
|
---|
2455 |
|
---|
2456 | void builtin_test(void)
|
---|
2457 | {
|
---|
2458 | #if GCC_MAJOR >= 3
|
---|
2459 | COMPAT_TYPE(int, int);
|
---|
2460 | COMPAT_TYPE(int, unsigned int);
|
---|
2461 | COMPAT_TYPE(int, char);
|
---|
2462 | COMPAT_TYPE(int, const int);
|
---|
2463 | COMPAT_TYPE(int, volatile int);
|
---|
2464 | COMPAT_TYPE(int *, int *);
|
---|
2465 | COMPAT_TYPE(int *, void *);
|
---|
2466 | COMPAT_TYPE(int *, const int *);
|
---|
2467 | COMPAT_TYPE(char *, unsigned char *);
|
---|
2468 | /* space is needed because tcc preprocessor introduces a space between each token */
|
---|
2469 | COMPAT_TYPE(char * *, void *);
|
---|
2470 | #endif
|
---|
2471 | printf("res = %d\n", __builtin_constant_p(1));
|
---|
2472 | printf("res = %d\n", __builtin_constant_p(1 + 2));
|
---|
2473 | printf("res = %d\n", __builtin_constant_p(&constant_p_var));
|
---|
2474 | printf("res = %d\n", __builtin_constant_p(constant_p_var));
|
---|
2475 | }
|
---|
2476 |
|
---|
2477 | #ifndef _WIN32
|
---|
2478 | extern int __attribute__((weak)) weak_f1(void);
|
---|
2479 | extern int __attribute__((weak)) weak_f2(void);
|
---|
2480 | extern int weak_f3(void);
|
---|
2481 | extern int __attribute__((weak)) weak_v1;
|
---|
2482 | extern int __attribute__((weak)) weak_v2;
|
---|
2483 | extern int weak_v3;
|
---|
2484 |
|
---|
2485 | extern int (*weak_fpa)() __attribute__((weak));
|
---|
2486 | extern int __attribute__((weak)) (*weak_fpb)();
|
---|
2487 | extern __attribute__((weak)) int (*weak_fpc)();
|
---|
2488 |
|
---|
2489 | extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
|
---|
2490 | extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
|
---|
2491 | extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
|
---|
2492 | extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
|
---|
2493 | extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
|
---|
2494 | extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
|
---|
2495 |
|
---|
2496 | static const size_t dummy = 0;
|
---|
2497 | extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
|
---|
2498 | extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
|
---|
2499 | extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
|
---|
2500 |
|
---|
2501 | int some_lib_func(void);
|
---|
2502 | int dummy_impl_of_slf(void) { return 444; }
|
---|
2503 | int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
|
---|
2504 |
|
---|
2505 | int weak_toolate() __attribute__((weak));
|
---|
2506 | int weak_toolate() { return 0; }
|
---|
2507 |
|
---|
2508 | void __attribute__((weak)) weak_test(void)
|
---|
2509 | {
|
---|
2510 | printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
|
---|
2511 | printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
|
---|
2512 | printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
|
---|
2513 | printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
|
---|
2514 | printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
|
---|
2515 | printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
|
---|
2516 |
|
---|
2517 | printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
|
---|
2518 | printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
|
---|
2519 | printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
|
---|
2520 |
|
---|
2521 | printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
|
---|
2522 | printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
|
---|
2523 | printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
|
---|
2524 | printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
|
---|
2525 | printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
|
---|
2526 | printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
|
---|
2527 | }
|
---|
2528 |
|
---|
2529 | int __attribute__((weak)) weak_f2() { return 222; }
|
---|
2530 | int __attribute__((weak)) weak_f3() { return 333; }
|
---|
2531 | int __attribute__((weak)) weak_v2 = 222;
|
---|
2532 | int __attribute__((weak)) weak_v3 = 333;
|
---|
2533 | #endif
|
---|
2534 |
|
---|
2535 | void const_func(const int a)
|
---|
2536 | {
|
---|
2537 | }
|
---|
2538 |
|
---|
2539 | void const_warn_test(void)
|
---|
2540 | {
|
---|
2541 | const_func(1);
|
---|
2542 | }
|
---|
2543 |
|
---|
2544 | struct condstruct {
|
---|
2545 | int i;
|
---|
2546 | };
|
---|
2547 |
|
---|
2548 | int getme (struct condstruct *s, int i)
|
---|
2549 | {
|
---|
2550 | int i1 = (i == 0 ? 0 : s)->i;
|
---|
2551 | int i2 = (i == 0 ? s : 0)->i;
|
---|
2552 | int i3 = (i == 0 ? (void*)0 : s)->i;
|
---|
2553 | int i4 = (i == 0 ? s : (void*)0)->i;
|
---|
2554 | return i1 + i2 + i3 + i4;
|
---|
2555 | }
|
---|
2556 |
|
---|
2557 | struct global_data
|
---|
2558 | {
|
---|
2559 | int a[40];
|
---|
2560 | int *b[40];
|
---|
2561 | };
|
---|
2562 |
|
---|
2563 | struct global_data global_data;
|
---|
2564 |
|
---|
2565 | int global_data_getstuff (int *, int);
|
---|
2566 |
|
---|
2567 | void global_data_callit (int i)
|
---|
2568 | {
|
---|
2569 | *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
|
---|
2570 | }
|
---|
2571 |
|
---|
2572 | int global_data_getstuff (int *p, int i)
|
---|
2573 | {
|
---|
2574 | return *p + i;
|
---|
2575 | }
|
---|
2576 |
|
---|
2577 | void global_data_test (void)
|
---|
2578 | {
|
---|
2579 | global_data.a[0] = 42;
|
---|
2580 | global_data.b[0] = &global_data.a[0];
|
---|
2581 | global_data_callit (0);
|
---|
2582 | printf ("%d\n", global_data.a[0]);
|
---|
2583 | }
|
---|
2584 |
|
---|
2585 | struct cmpcmpS
|
---|
2586 | {
|
---|
2587 | unsigned char fill : 3;
|
---|
2588 | unsigned char b1 : 1;
|
---|
2589 | unsigned char b2 : 1;
|
---|
2590 | unsigned char fill2 : 3;
|
---|
2591 | };
|
---|
2592 |
|
---|
2593 | int glob1, glob2, glob3;
|
---|
2594 |
|
---|
2595 | void compare_comparisons (struct cmpcmpS *s)
|
---|
2596 | {
|
---|
2597 | if (s->b1 != (glob1 == glob2)
|
---|
2598 | || (s->b2 != (glob1 == glob3)))
|
---|
2599 | printf ("comparing comparisons broken\n");
|
---|
2600 | }
|
---|
2601 |
|
---|
2602 | void cmp_comparison_test(void)
|
---|
2603 | {
|
---|
2604 | struct cmpcmpS s;
|
---|
2605 | s.b1 = 1;
|
---|
2606 | glob1 = 42; glob2 = 42;
|
---|
2607 | s.b2 = 0;
|
---|
2608 | glob3 = 43;
|
---|
2609 | compare_comparisons (&s);
|
---|
2610 | }
|
---|
2611 |
|
---|
2612 | int fcompare (double a, double b, int code)
|
---|
2613 | {
|
---|
2614 | switch (code) {
|
---|
2615 | case 0: return a == b;
|
---|
2616 | case 1: return a != b;
|
---|
2617 | case 2: return a < b;
|
---|
2618 | case 3: return a >= b;
|
---|
2619 | case 4: return a > b;
|
---|
2620 | case 5: return a <= b;
|
---|
2621 | }
|
---|
2622 | }
|
---|
2623 |
|
---|
2624 | void math_cmp_test(void)
|
---|
2625 | {
|
---|
2626 | double nan = 0.0/0.0;
|
---|
2627 | double one = 1.0;
|
---|
2628 | double two = 2.0;
|
---|
2629 | int comp = 0;
|
---|
2630 | #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
|
---|
2631 |
|
---|
2632 | /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
|
---|
2633 | And it does this in various ways so that all code generation paths
|
---|
2634 | are checked (generating inverted tests, or non-inverted tests, or
|
---|
2635 | producing a 0/1 value without jumps (that's done in the fcompare
|
---|
2636 | function). */
|
---|
2637 | #define FCMP(a,b,op,iop,code) \
|
---|
2638 | if (fcompare (a,b,code)) \
|
---|
2639 | bug (a,b,op,iop,1); \
|
---|
2640 | if (a op b) \
|
---|
2641 | bug (a,b,op,iop,2); \
|
---|
2642 | if (a iop b) \
|
---|
2643 | ; \
|
---|
2644 | else \
|
---|
2645 | bug (a,b,op,iop,3); \
|
---|
2646 | if ((a op b) || comp) \
|
---|
2647 | bug (a,b,op,iop,4); \
|
---|
2648 | if ((a iop b) || comp) \
|
---|
2649 | ; \
|
---|
2650 | else \
|
---|
2651 | bug (a,b,op,iop,5);
|
---|
2652 |
|
---|
2653 | /* Equality tests. */
|
---|
2654 | FCMP(nan, nan, ==, !=, 0);
|
---|
2655 | FCMP(one, two, ==, !=, 0);
|
---|
2656 | FCMP(one, one, !=, ==, 1);
|
---|
2657 | /* Non-equality is a bit special. */
|
---|
2658 | if (!fcompare (nan, nan, 1))
|
---|
2659 | bug (nan, nan, !=, ==, 6);
|
---|
2660 |
|
---|
2661 | /* Relational tests on numbers. */
|
---|
2662 | FCMP(two, one, <, >=, 2);
|
---|
2663 | FCMP(one, two, >=, <, 3);
|
---|
2664 | FCMP(one, two, >, <=, 4);
|
---|
2665 | FCMP(two, one, <=, >, 5);
|
---|
2666 |
|
---|
2667 | /* Relational tests on NaNs. Note that the inverse op here is
|
---|
2668 | always !=, there's no operator in C that is equivalent to !(a < b),
|
---|
2669 | when NaNs are involved, same for the other relational ops. */
|
---|
2670 | FCMP(nan, nan, <, !=, 2);
|
---|
2671 | FCMP(nan, nan, >=, !=, 3);
|
---|
2672 | FCMP(nan, nan, >, !=, 4);
|
---|
2673 | FCMP(nan, nan, <=, !=, 5);
|
---|
2674 | }
|
---|
2675 |
|
---|
2676 | double get100 () { return 100.0; }
|
---|
2677 |
|
---|
2678 | void callsave_test(void)
|
---|
2679 | {
|
---|
2680 | #if defined __i386__ || defined __x86_64__
|
---|
2681 | int i, s; double *d; double t;
|
---|
2682 | s = sizeof (double);
|
---|
2683 | printf ("callsavetest: %d\n", s);
|
---|
2684 | d = alloca (sizeof(double));
|
---|
2685 | d[0] = 10.0;
|
---|
2686 | /* x86-64 had a bug were the next call to get100 would evict
|
---|
2687 | the lvalue &d[0] as VT_LLOCAL, and the reload would be done
|
---|
2688 | in int type, not pointer type. When alloca returns a pointer
|
---|
2689 | with the high 32 bit set (which is likely on x86-64) the access
|
---|
2690 | generates a segfault. */
|
---|
2691 | i = d[0] > get100 ();
|
---|
2692 | printf ("%d\n", i);
|
---|
2693 | #endif
|
---|
2694 | }
|
---|
2695 |
|
---|
2696 |
|
---|
2697 | void bfa3(ptrdiff_t str_offset)
|
---|
2698 | {
|
---|
2699 | printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
|
---|
2700 | }
|
---|
2701 | void bfa2(ptrdiff_t str_offset)
|
---|
2702 | {
|
---|
2703 | printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
|
---|
2704 | bfa3(str_offset);
|
---|
2705 | }
|
---|
2706 | void bfa1(ptrdiff_t str_offset)
|
---|
2707 | {
|
---|
2708 | printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
|
---|
2709 | #if defined(__arm__) && !defined(__GNUC__)
|
---|
2710 | bfa2(str_offset);
|
---|
2711 | #endif
|
---|
2712 | }
|
---|
2713 |
|
---|
2714 | void builtin_frame_address_test(void)
|
---|
2715 | {
|
---|
2716 | char str[] = "__builtin_frame_address";
|
---|
2717 | char *fp0 = __builtin_frame_address(0);
|
---|
2718 |
|
---|
2719 | printf("str: %s\n", str);
|
---|
2720 | bfa1(str-fp0);
|
---|
2721 | }
|
---|