Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
145 | halyavin | 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 |
||
28 | #define TCCLIB_INC1 |
||
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 |
||
42 | |||
43 | #include "tcclib.h" |
||
44 | |||
45 | void string_test(); |
||
46 | void expr_test(); |
||
47 | void macro_test(); |
||
48 | void scope_test(); |
||
49 | void forward_test(); |
||
50 | void funcptr_test(); |
||
51 | void loop_test(); |
||
52 | void switch_test(); |
||
53 | void goto_test(); |
||
54 | void enum_test(); |
||
55 | void typedef_test(); |
||
56 | void struct_test(); |
||
57 | void array_test(); |
||
58 | void expr_ptr_test(); |
||
59 | void bool_test(); |
||
60 | void expr2_test(); |
||
61 | void constant_expr_test(); |
||
62 | void expr_cmp_test(); |
||
63 | void char_short_test(); |
||
64 | void init_test(void); |
||
65 | void compound_literal_test(void); |
||
66 | int kr_test(); |
||
67 | void struct_assign_test(void); |
||
68 | void cast_test(void); |
||
69 | void bitfield_test(void); |
||
70 | void c99_bool_test(void); |
||
71 | void float_test(void); |
||
72 | void longlong_test(void); |
||
73 | void stdarg_test(void); |
||
74 | void whitespace_test(void); |
||
75 | void relocation_test(void); |
||
76 | void old_style_function(void); |
||
77 | void sizeof_test(void); |
||
78 | void typeof_test(void); |
||
79 | void local_label_test(void); |
||
80 | void statement_expr_test(void); |
||
81 | void asm_test(void); |
||
82 | void builtin_test(void); |
||
83 | |||
84 | int fib(int n); |
||
85 | void num(int n); |
||
86 | void forward_ref(void); |
||
87 | int isid(int c); |
||
88 | |||
89 | #define A 2 |
||
90 | #define N 1234 + A |
||
91 | #define pf printf |
||
92 | #define M1(a, b) (a) + (b) |
||
93 | |||
94 | #define str\ |
||
95 | (s) # s |
||
96 | #define glue(a, b) a ## b |
||
97 | #define xglue(a, b) glue(a, b) |
||
98 | #define HIGHLOW "hello" |
||
99 | #define LOW LOW ", world" |
||
100 | |||
101 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
||
102 | |||
103 | #ifdef C99_MACROS |
||
104 | #define dprintf(level,...) printf(__VA_ARGS__) |
||
105 | #endif |
||
106 | |||
107 | /* gcc vararg macros */ |
||
108 | #define dprintf1(level, fmt, args...) printf(fmt, ## args) |
||
109 | |||
110 | #define MACRO_NOARGS() |
||
111 | |||
112 | #define AAA 3 |
||
113 | #undef AAA |
||
114 | #define AAA 4 |
||
115 | |||
116 | #if 1 |
||
117 | #define B3 1 |
||
118 | #elif 1 |
||
119 | #define B3 2 |
||
120 | #elif 0 |
||
121 | #define B3 3 |
||
122 | #else |
||
123 | #define B3 4 |
||
124 | #endif |
||
125 | |||
126 | #define __INT64_C(c) c ## LL |
||
127 | #define INT64_MIN (-__INT64_C(9223372036854775807)-1) |
||
128 | |||
129 | int qq(int x) |
||
130 | { |
||
131 | return x + 40; |
||
132 | } |
||
133 | #define qq(x) x |
||
134 | |||
135 | #define spin_lock(lock) do { } while (0) |
||
136 | #define wq_spin_lock spin_lock |
||
137 | #define TEST2() wq_spin_lock(a) |
||
138 | |||
139 | void macro_test(void) |
||
140 | { |
||
141 | printf("macro:\n"); |
||
142 | pf("N=%d\n", N); |
||
143 | printf("aaa=%d\n", AAA); |
||
144 | |||
145 | printf("min=%d\n", min(1, min(2, -1))); |
||
146 | |||
147 | printf("s1=%s\n", glue(HIGH, LOW)); |
||
148 | printf("s2=%s\n", xglue(HIGH, LOW)); |
||
149 | printf("s3=%s\n", str("c")); |
||
150 | printf("s4=%s\n", str(a1)); |
||
151 | printf("B3=%d\n", B3); |
||
152 | |||
153 | #ifdef A |
||
154 | printf("A defined\n"); |
||
155 | #endif |
||
156 | #ifdef B |
||
157 | printf("B defined\n"); |
||
158 | #endif |
||
159 | #ifdef A |
||
160 | printf("A defined\n"); |
||
161 | #else |
||
162 | printf("A not defined\n"); |
||
163 | #endif |
||
164 | #ifdef B |
||
165 | printf("B defined\n"); |
||
166 | #else |
||
167 | printf("B not defined\n"); |
||
168 | #endif |
||
169 | |||
170 | #ifdef A |
||
171 | printf("A defined\n"); |
||
172 | #ifdef B |
||
173 | printf("B1 defined\n"); |
||
174 | #else |
||
175 | printf("B1 not defined\n"); |
||
176 | #endif |
||
177 | #else |
||
178 | printf("A not defined\n"); |
||
179 | #ifdef B |
||
180 | printf("B2 defined\n"); |
||
181 | #else |
||
182 | printf("B2 not defined\n"); |
||
183 | #endif |
||
184 | #endif |
||
185 | |||
186 | #if 1+1 |
||
187 | printf("test true1\n"); |
||
188 | #endif |
||
189 | #if 0 |
||
190 | printf("test true2\n"); |
||
191 | #endif |
||
192 | #if 1-1 |
||
193 | printf("test true3\n"); |
||
194 | #endif |
||
195 | #if defined(A) |
||
196 | printf("test trueA\n"); |
||
197 | #endif |
||
198 | #if defined(B) |
||
199 | printf("test trueB\n"); |
||
200 | #endif |
||
201 | |||
202 | #if 0 |
||
203 | printf("test 0\n"); |
||
204 | #elif 0 |
||
205 | printf("test 1\n"); |
||
206 | #elif 2 |
||
207 | printf("test 2\n"); |
||
208 | #else |
||
209 | printf("test 3\n"); |
||
210 | #endif |
||
211 | |||
212 | MACRO_NOARGS(); |
||
213 | |||
214 | #ifdef __LINE__ |
||
215 | printf("__LINE__ defined\n"); |
||
216 | #endif |
||
217 | |||
218 | printf("__LINE__=%d __FILE__=%s\n", |
||
219 | __LINE__, __FILE__); |
||
220 | #line 200 |
||
221 | printf("__LINE__=%d __FILE__=%s\n", |
||
222 | __LINE__, __FILE__); |
||
223 | #line 203 "test" |
||
224 | printf("__LINE__=%d __FILE__=%s\n", |
||
225 | __LINE__, __FILE__); |
||
226 | #line 220 "tcctest.c" |
||
227 | |||
228 | /* not strictly preprocessor, but we test it there */ |
||
229 | #ifdef C99_MACROS |
||
230 | printf("__func__ = %s\n", __func__); |
||
231 | dprintf(1, "vaarg=%d\n", 1); |
||
232 | #endif |
||
233 | dprintf1(1, "vaarg1\n"); |
||
234 | dprintf1(1, "vaarg1=%d\n", 2); |
||
235 | dprintf1(1, "vaarg1=%d %d\n", 1, 2); |
||
236 | |||
237 | /* gcc extension */ |
||
238 | printf("func='%s'\n", __FUNCTION__); |
||
239 | |||
240 | /* complicated macros in glibc */ |
||
241 | printf("INT64_MIN=%Ld\n", INT64_MIN); |
||
242 | { |
||
243 | int a; |
||
244 | a = 1; |
||
245 | glue(a+, +); |
||
246 | printf("a=%d\n", a); |
||
247 | glue(a <, <= 2); |
||
248 | printf("a=%d\n", a); |
||
249 | } |
||
250 | |||
251 | /* macro function with argument outside the macro string */ |
||
252 | #define MF_s MF_hello |
||
253 | #define MF_hello(msg) printf("%s\n",msg) |
||
254 | |||
255 | #define MF_t printf("tralala\n"); MF_hello |
||
256 | |||
257 | MF_s("hi"); |
||
258 | MF_t("hi"); |
||
259 | |||
260 | /* test macro substituion inside args (should not eat stream) */ |
||
261 | printf("qq=%d\n", qq(qq)(2)); |
||
262 | |||
263 | /* test zero argument case. NOTE: gcc 2.95.x does not accept a |
||
264 | null argument without a space. gcc 3.2 fixes that. */ |
||
265 | |||
266 | #define qq1(x) 1 |
||
267 | printf("qq1=%d\n", qq1( )); |
||
268 | |||
269 | /* comment with stray handling *\ |
||
270 | / |
||
271 | /* this is a valid *\/ comment */ |
||
272 | /* this is a valid comment *\*/ |
||
273 | // this is a valid\ |
||
274 | comment |
||
275 | |||
276 | /* test function macro substitution when the function name is |
||
277 | substituted */ |
||
278 | TEST2(); |
||
279 | } |
||
280 | |||
281 | int op(a,b) |
||
282 | { |
||
283 | return a / b; |
||
284 | } |
||
285 | |||
286 | int ret(a) |
||
287 | { |
||
288 | if (a == 2) |
||
289 | return 1; |
||
290 | if (a == 3) |
||
291 | return 2; |
||
292 | return 0; |
||
293 | } |
||
294 | |||
295 | void ps(const char *s) |
||
296 | { |
||
297 | int c; |
||
298 | while (1) { |
||
299 | c = *s; |
||
300 | if (c == 0) |
||
301 | break; |
||
302 | printf("%c", c); |
||
303 | s++; |
||
304 | } |
||
305 | } |
||
306 | |||
307 | const char foo1_string[] = "\ |
||
308 | bar\n\ |
||
309 | test\14\ |
||
310 | 1"; |
||
311 | |||
312 | void string_test() |
||
313 | { |
||
314 | int b; |
||
315 | printf("string:\n"); |
||
316 | printf("\141\1423\143\n");/* dezdez test */ |
||
317 | printf("\x41\x42\x43\x3a\n"); |
||
318 | printf("c=%c\n", 'r'); |
||
319 | printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c'); |
||
320 | printf("foo1_string='%s'\n", foo1_string); |
||
321 | #if 0 |
||
322 | printf("wstring=%S\n", L"abc"); |
||
323 | printf("wstring=%S\n", L"abc" L"def" "ghi"); |
||
324 | printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff'); |
||
325 | printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff'); |
||
326 | #endif |
||
327 | ps("test\n"); |
||
328 | b = 32; |
||
329 | while ((b = b + 1) < 96) { |
||
330 | printf("%c", b); |
||
331 | } |
||
332 | printf("\n"); |
||
333 | printf("fib=%d\n", fib(33)); |
||
334 | b = 262144; |
||
335 | while (b != 0x80000000) { |
||
336 | num(b); |
||
337 | b = b * 2; |
||
338 | } |
||
339 | } |
||
340 | |||
341 | void loop_test() |
||
342 | { |
||
343 | int i; |
||
344 | i = 0; |
||
345 | while (i < 10) |
||
346 | printf("%d", i++); |
||
347 | printf("\n"); |
||
348 | for(i = 0; i < 10;i++) |
||
349 | printf("%d", i); |
||
350 | printf("\n"); |
||
351 | i = 0; |
||
352 | do { |
||
353 | printf("%d", i++); |
||
354 | } while (i < 10); |
||
355 | printf("\n"); |
||
356 | |||
357 | /* break/continue tests */ |
||
358 | i = 0; |
||
359 | while (1) { |
||
360 | if (i == 6) |
||
361 | break; |
||
362 | i++; |
||
363 | if (i == 3) |
||
364 | continue; |
||
365 | printf("%d", i); |
||
366 | } |
||
367 | printf("\n"); |
||
368 | |||
369 | /* break/continue tests */ |
||
370 | i = 0; |
||
371 | do { |
||
372 | if (i == 6) |
||
373 | break; |
||
374 | i++; |
||
375 | if (i == 3) |
||
376 | continue; |
||
377 | printf("%d", i); |
||
378 | } while(1); |
||
379 | printf("\n"); |
||
380 | |||
381 | for(i = 0;i < 10;i++) { |
||
382 | if (i == 3) |
||
383 | continue; |
||
384 | printf("%d", i); |
||
385 | } |
||
386 | printf("\n"); |
||
387 | } |
||
388 | |||
389 | |||
390 | void goto_test() |
||
391 | { |
||
392 | int i; |
||
393 | static void *label_table[3] = { &&label1, &&label2, &&label3 }; |
||
394 | |||
395 | printf("goto:\n"); |
||
396 | i = 0; |
||
397 | s_loop: |
||
398 | if (i >= 10) |
||
399 | goto s_end; |
||
400 | printf("%d", i); |
||
401 | i++; |
||
402 | goto s_loop; |
||
403 | s_end: |
||
404 | printf("\n"); |
||
405 | |||
406 | /* we also test computed gotos (GCC extension) */ |
||
407 | for(i=0;i<3;i++) { |
||
408 | goto *label_table[i]; |
||
409 | label1: |
||
410 | printf("label1\n"); |
||
411 | goto next; |
||
412 | label2: |
||
413 | printf("label2\n"); |
||
414 | goto next; |
||
415 | label3: |
||
416 | printf("label3\n"); |
||
417 | next: ; |
||
418 | } |
||
419 | } |
||
420 | |||
421 | enum { |
||
422 | E0, |
||
423 | E1 = 2, |
||
424 | E2 = 4, |
||
425 | E3, |
||
426 | E4, |
||
427 | }; |
||
428 | |||
429 | enum test { |
||
430 | E5 = 1000, |
||
431 | }; |
||
432 | |||
433 | void enum_test() |
||
434 | { |
||
435 | enum test b1; |
||
436 | printf("enum:\n%d %d %d %d %d %d\n", |
||
437 | E0, E1, E2, E3, E4, E5); |
||
438 | b1 = 1; |
||
439 | printf("b1=%d\n", b1); |
||
440 | } |
||
441 | |||
442 | typedef int *my_ptr; |
||
443 | |||
444 | void typedef_test() |
||
445 | { |
||
446 | my_ptr a; |
||
447 | int b; |
||
448 | a = &b; |
||
449 | *a = 1234; |
||
450 | printf("typedef:\n"); |
||
451 | printf("a=%d\n", *a); |
||
452 | } |
||
453 | |||
454 | void forward_test() |
||
455 | { |
||
456 | printf("forward:\n"); |
||
457 | forward_ref(); |
||
458 | forward_ref(); |
||
459 | } |
||
460 | |||
461 | |||
462 | void forward_ref(void) |
||
463 | { |
||
464 | printf("forward ok\n"); |
||
465 | } |
||
466 | |||
467 | typedef struct struct1 { |
||
468 | int f1; |
||
469 | int f2, f3; |
||
470 | union union1 { |
||
471 | int v1; |
||
472 | int v2; |
||
473 | } u; |
||
474 | char str[3]; |
||
475 | } struct1; |
||
476 | |||
477 | struct struct2 { |
||
478 | int a; |
||
479 | char b; |
||
480 | }; |
||
481 | |||
482 | union union2 { |
||
483 | int w1; |
||
484 | int w2; |
||
485 | }; |
||
486 | |||
487 | struct struct1 st1, st2; |
||
488 | |||
489 | int main(int argc, char **argv) |
||
490 | { |
||
491 | string_test(); |
||
492 | expr_test(); |
||
493 | macro_test(); |
||
494 | scope_test(); |
||
495 | forward_test(); |
||
496 | funcptr_test(); |
||
497 | loop_test(); |
||
498 | switch_test(); |
||
499 | goto_test(); |
||
500 | enum_test(); |
||
501 | typedef_test(); |
||
502 | struct_test(); |
||
503 | array_test(); |
||
504 | expr_ptr_test(); |
||
505 | bool_test(); |
||
506 | expr2_test(); |
||
507 | constant_expr_test(); |
||
508 | expr_cmp_test(); |
||
509 | char_short_test(); |
||
510 | init_test(); |
||
511 | compound_literal_test(); |
||
512 | kr_test(); |
||
513 | struct_assign_test(); |
||
514 | cast_test(); |
||
515 | bitfield_test(); |
||
516 | c99_bool_test(); |
||
517 | float_test(); |
||
518 | longlong_test(); |
||
519 | stdarg_test(); |
||
520 | whitespace_test(); |
||
521 | relocation_test(); |
||
522 | old_style_function(); |
||
523 | sizeof_test(); |
||
524 | typeof_test(); |
||
525 | statement_expr_test(); |
||
526 | local_label_test(); |
||
527 | asm_test(); |
||
528 | builtin_test(); |
||
529 | return 0; |
||
530 | } |
||
531 | |||
532 | int tab[3]; |
||
533 | int tab2[3][2]; |
||
534 | |||
535 | int g; |
||
536 | |||
537 | void f1(g) |
||
538 | { |
||
539 | printf("g1=%d\n", g); |
||
540 | } |
||
541 | |||
542 | void scope_test() |
||
543 | { |
||
544 | printf("scope:\n"); |
||
545 | g = 2; |
||
546 | f1(1); |
||
547 | printf("g2=%d\n", g); |
||
548 | { |
||
549 | int g; |
||
550 | g = 3; |
||
551 | printf("g3=%d\n", g); |
||
552 | { |
||
553 | int g; |
||
554 | g = 4; |
||
555 | printf("g4=%d\n", g); |
||
556 | } |
||
557 | } |
||
558 | printf("g5=%d\n", g); |
||
559 | } |
||
560 | |||
561 | void array_test(int a[4]) |
||
562 | { |
||
563 | int i, j; |
||
564 | |||
565 | printf("array:\n"); |
||
566 | printf("sizeof(a) = %d\n", sizeof(a)); |
||
567 | printf("sizeof(\"a\") = %d\n", sizeof("a")); |
||
568 | #ifdef C99_MACROS |
||
569 | printf("sizeof(__func__) = %d\n", sizeof(__func__)); |
||
570 | #endif |
||
571 | printf("sizeof tab %d\n", sizeof(tab)); |
||
572 | printf("sizeof tab2 %d\n", sizeof tab2); |
||
573 | tab[0] = 1; |
||
574 | tab[1] = 2; |
||
575 | tab[2] = 3; |
||
576 | printf("%d %d %d\n", tab[0], tab[1], tab[2]); |
||
577 | for(i=0;i<3;i++) |
||
578 | for(j=0;j<2;j++) |
||
579 | tab2[i][j] = 10 * i + j; |
||
580 | for(i=0;i<3*2;i++) { |
||
581 | printf(" %3d", ((int *)tab2)[i]); |
||
582 | } |
||
583 | printf("\n"); |
||
584 | } |
||
585 | |||
586 | void expr_test() |
||
587 | { |
||
588 | int a, b; |
||
589 | a = 0; |
||
590 | printf("%d\n", a += 1); |
||
591 | printf("%d\n", a -= 2); |
||
592 | printf("%d\n", a *= 31232132); |
||
593 | printf("%d\n", a /= 4); |
||
594 | printf("%d\n", a %= 20); |
||
595 | printf("%d\n", a &= 6); |
||
596 | printf("%d\n", a ^= 7); |
||
597 | printf("%d\n", a |= 8); |
||
598 | printf("%d\n", a >>= 3); |
||
599 | printf("%d\n", a <<= 4); |
||
600 | |||
601 | a = 22321; |
||
602 | b = -22321; |
||
603 | printf("%d\n", a + 1); |
||
604 | printf("%d\n", a - 2); |
||
605 | printf("%d\n", a * 312); |
||
606 | printf("%d\n", a / 4); |
||
607 | printf("%d\n", b / 4); |
||
608 | printf("%d\n", (unsigned)b / 4); |
||
609 | printf("%d\n", a % 20); |
||
610 | printf("%d\n", b % 20); |
||
611 | printf("%d\n", (unsigned)b % 20); |
||
612 | printf("%d\n", a & 6); |
||
613 | printf("%d\n", a ^ 7); |
||
614 | printf("%d\n", a | 8); |
||
615 | printf("%d\n", a >> 3); |
||
616 | printf("%d\n", b >> 3); |
||
617 | printf("%d\n", (unsigned)b >> 3); |
||
618 | printf("%d\n", a << 4); |
||
619 | printf("%d\n", ~a); |
||
620 | printf("%d\n", -a); |
||
621 | printf("%d\n", +a); |
||
622 | |||
623 | printf("%d\n", 12 + 1); |
||
624 | printf("%d\n", 12 - 2); |
||
625 | printf("%d\n", 12 * 312); |
||
626 | printf("%d\n", 12 / 4); |
||
627 | printf("%d\n", 12 % 20); |
||
628 | printf("%d\n", 12 & 6); |
||
629 | printf("%d\n", 12 ^ 7); |
||
630 | printf("%d\n", 12 | 8); |
||
631 | printf("%d\n", 12 >> 2); |
||
632 | printf("%d\n", 12 << 4); |
||
633 | printf("%d\n", ~12); |
||
634 | printf("%d\n", -12); |
||
635 | printf("%d\n", +12); |
||
636 | printf("%d %d %d %d\n", |
||
637 | isid('a'), |
||
638 | isid('g'), |
||
639 | isid('T'), |
||
640 | isid('(')); |
||
641 | } |
||
642 | |||
643 | int isid(int c) |
||
644 | { |
||
645 | return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_'; |
||
646 | } |
||
647 | |||
648 | /**********************/ |
||
649 | |||
650 | int vstack[10], *vstack_ptr; |
||
651 | |||
652 | void vpush(int vt, int vc) |
||
653 | { |
||
654 | *vstack_ptr++ = vt; |
||
655 | *vstack_ptr++ = vc; |
||
656 | } |
||
657 | |||
658 | void vpop(int *ft, int *fc) |
||
659 | { |
||
660 | *fc = *--vstack_ptr; |
||
661 | *ft = *--vstack_ptr; |
||
662 | } |
||
663 | |||
664 | void expr2_test() |
||
665 | { |
||
666 | int a, b; |
||
667 | |||
668 | printf("expr2:\n"); |
||
669 | vstack_ptr = vstack; |
||
670 | vpush(1432432, 2); |
||
671 | vstack_ptr[-2] &= ~0xffffff80; |
||
672 | vpop(&a, &b); |
||
673 | printf("res= %d %d\n", a, b); |
||
674 | } |
||
675 | |||
676 | void constant_expr_test() |
||
677 | { |
||
678 | int a; |
||
679 | printf("constant_expr:\n"); |
||
680 | a = 3; |
||
681 | printf("%d\n", a * 16); |
||
682 | printf("%d\n", a * 1); |
||
683 | printf("%d\n", a + 0); |
||
684 | } |
||
685 | |||
686 | int tab4[10]; |
||
687 | |||
688 | void expr_ptr_test() |
||
689 | { |
||
690 | int *p, *q; |
||
691 | |||
692 | printf("expr_ptr:\n"); |
||
693 | p = tab4; |
||
694 | q = tab4 + 10; |
||
695 | printf("diff=%d\n", q - p); |
||
696 | p++; |
||
697 | printf("inc=%d\n", p - tab4); |
||
698 | p--; |
||
699 | printf("dec=%d\n", p - tab4); |
||
700 | ++p; |
||
701 | printf("inc=%d\n", p - tab4); |
||
702 | --p; |
||
703 | printf("dec=%d\n", p - tab4); |
||
704 | printf("add=%d\n", p + 3 - tab4); |
||
705 | printf("add=%d\n", 3 + p - tab4); |
||
706 | } |
||
707 | |||
708 | void expr_cmp_test() |
||
709 | { |
||
710 | int a, b; |
||
711 | printf("constant_expr:\n"); |
||
712 | a = -1; |
||
713 | b = 1; |
||
714 | printf("%d\n", a == a); |
||
715 | printf("%d\n", a != a); |
||
716 | |||
717 | printf("%d\n", a < b); |
||
718 | printf("%d\n", a <= b); |
||
719 | printf("%d\n", a <= a); |
||
720 | printf("%d\n", b >= a); |
||
721 | printf("%d\n", a >= a); |
||
722 | printf("%d\n", b > a); |
||
723 | |||
724 | printf("%d\n", (unsigned)a < b); |
||
725 | printf("%d\n", (unsigned)a <= b); |
||
726 | printf("%d\n", (unsigned)a <= a); |
||
727 | printf("%d\n", (unsigned)b >= a); |
||
728 | printf("%d\n", (unsigned)a >= a); |
||
729 | printf("%d\n", (unsigned)b > a); |
||
730 | } |
||
731 | |||
732 | struct empty { |
||
733 | }; |
||
734 | |||
735 | struct aligntest1 { |
||
736 | char a[10]; |
||
737 | }; |
||
738 | |||
739 | struct aligntest2 { |
||
740 | int a; |
||
741 | char b[10]; |
||
742 | }; |
||
743 | |||
744 | struct aligntest3 { |
||
745 | double a, b; |
||
746 | }; |
||
747 | |||
748 | struct aligntest4 { |
||
749 | double a[0]; |
||
750 | }; |
||
751 | |||
752 | void struct_test() |
||
753 | { |
||
754 | struct1 *s; |
||
755 | union union2 u; |
||
756 | |||
757 | printf("struct:\n"); |
||
758 | printf("sizes: %d %d %d %d\n", |
||
759 | sizeof(struct struct1), |
||
760 | sizeof(struct struct2), |
||
761 | sizeof(union union1), |
||
762 | sizeof(union union2)); |
||
763 | st1.f1 = 1; |
||
764 | st1.f2 = 2; |
||
765 | st1.f3 = 3; |
||
766 | printf("st1: %d %d %d\n", |
||
767 | st1.f1, st1.f2, st1.f3); |
||
768 | st1.u.v1 = 1; |
||
769 | st1.u.v2 = 2; |
||
770 | printf("union1: %d\n", st1.u.v1); |
||
771 | u.w1 = 1; |
||
772 | u.w2 = 2; |
||
773 | printf("union2: %d\n", u.w1); |
||
774 | s = &st2; |
||
775 | s->f1 = 3; |
||
776 | s->f2 = 2; |
||
777 | s->f3 = 1; |
||
778 | printf("st2: %d %d %d\n", |
||
779 | s->f1, s->f2, s->f3); |
||
780 | printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1); |
||
781 | |||
782 | /* align / size tests */ |
||
783 | printf("aligntest1 sizeof=%d alignof=%d\n", |
||
784 | sizeof(struct aligntest1), __alignof__(struct aligntest1)); |
||
785 | printf("aligntest2 sizeof=%d alignof=%d\n", |
||
786 | sizeof(struct aligntest2), __alignof__(struct aligntest2)); |
||
787 | printf("aligntest3 sizeof=%d alignof=%d\n", |
||
788 | sizeof(struct aligntest3), __alignof__(struct aligntest3)); |
||
789 | printf("aligntest4 sizeof=%d alignof=%d\n", |
||
790 | sizeof(struct aligntest4), __alignof__(struct aligntest4)); |
||
791 | |||
792 | /* empty structures (GCC extension) */ |
||
793 | printf("sizeof(struct empty) = %d\n", sizeof(struct empty)); |
||
794 | printf("alignof(struct empty) = %d\n", __alignof__(struct empty)); |
||
795 | } |
||
796 | |||
797 | /* XXX: depend on endianness */ |
||
798 | void char_short_test() |
||
799 | { |
||
800 | int var1, var2; |
||
801 | |||
802 | printf("char_short:\n"); |
||
803 | |||
804 | var1 = 0x01020304; |
||
805 | var2 = 0xfffefdfc; |
||
806 | printf("s8=%d %d\n", |
||
807 | *(char *)&var1, *(char *)&var2); |
||
808 | printf("u8=%d %d\n", |
||
809 | *(unsigned char *)&var1, *(unsigned char *)&var2); |
||
810 | printf("s16=%d %d\n", |
||
811 | *(short *)&var1, *(short *)&var2); |
||
812 | printf("u16=%d %d\n", |
||
813 | *(unsigned short *)&var1, *(unsigned short *)&var2); |
||
814 | printf("s32=%d %d\n", |
||
815 | *(int *)&var1, *(int *)&var2); |
||
816 | printf("u32=%d %d\n", |
||
817 | *(unsigned int *)&var1, *(unsigned int *)&var2); |
||
818 | *(char *)&var1 = 0x08; |
||
819 | printf("var1=%x\n", var1); |
||
820 | *(short *)&var1 = 0x0809; |
||
821 | printf("var1=%x\n", var1); |
||
822 | *(int *)&var1 = 0x08090a0b; |
||
823 | printf("var1=%x\n", var1); |
||
824 | } |
||
825 | |||
826 | /******************/ |
||
827 | |||
828 | typedef struct Sym { |
||
829 | int v; |
||
830 | int t; |
||
831 | int c; |
||
832 | struct Sym *next; |
||
833 | struct Sym *prev; |
||
834 | } Sym; |
||
835 | |||
836 | #define ISLOWER(c) ('a' <= (c) && (c) <= 'z') |
||
837 | #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c)) |
||
838 | |||
839 | static int toupper1(int a) |
||
840 | { |
||
841 | return TOUPPER(a); |
||
842 | } |
||
843 | |||
844 | void bool_test() |
||
845 | { |
||
846 | int *s, a, b, t, f, i; |
||
847 | |||
848 | a = 0; |
||
849 | s = (void*)0; |
||
850 | printf("!s=%d\n", !s); |
||
851 | |||
852 | if (!s || !s[0]) |
||
853 | a = 1; |
||
854 | printf("a=%d\n", a); |
||
855 | |||
856 | printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1); |
||
857 | printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1); |
||
858 | printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0); |
||
859 | #if 1 && 1 |
||
860 | printf("a1\n"); |
||
861 | #endif |
||
862 | #if 1 || 0 |
||
863 | printf("a2\n"); |
||
864 | #endif |
||
865 | #if 1 ? 0 : 1 |
||
866 | printf("a3\n"); |
||
867 | #endif |
||
868 | #if 0 ? 0 : 1 |
||
869 | printf("a4\n"); |
||
870 | #endif |
||
871 | |||
872 | a = 4; |
||
873 | printf("b=%d\n", a + (0 ? 1 : a / 2)); |
||
874 | |||
875 | /* test register spilling */ |
||
876 | a = 10; |
||
877 | b = 10; |
||
878 | a = (a + b) * ((a < b) ? |
||
879 | ((b - a) * (a - b)): a + b); |
||
880 | printf("a=%d\n", a); |
||
881 | |||
882 | /* test complex || or && expressions */ |
||
883 | t = 1; |
||
884 | f = 0; |
||
885 | a = 32; |
||
886 | printf("exp=%d\n", f == (32 <= a && a <= 3)); |
||
887 | printf("r=%d\n", (t || f) + (t && f)); |
||
888 | |||
889 | /* test ? : cast */ |
||
890 | { |
||
891 | int aspect_on; |
||
892 | int aspect_native = 65536; |
||
893 | double bfu_aspect = 1.0; |
||
894 | int aspect; |
||
895 | for(aspect_on = 0; aspect_on < 2; aspect_on++) { |
||
896 | aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL; |
||
897 | printf("aspect=%d\n", aspect); |
||
898 | } |
||
899 | } |
||
900 | |||
901 | /* test ? : GCC extension */ |
||
902 | { |
||
903 | static int v1 = 34 ? : -1; /* constant case */ |
||
904 | static int v2 = 0 ? : -1; /* constant case */ |
||
905 | int a = 30; |
||
906 | |||
907 | printf("%d %d\n", v1, v2); |
||
908 | printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2); |
||
909 | } |
||
910 | |||
911 | /* again complex expression */ |
||
912 | for(i=0;i<256;i++) { |
||
913 | if (toupper1 (i) != TOUPPER (i)) |
||
914 | printf("error %d\n", i); |
||
915 | } |
||
916 | } |
||
917 | |||
918 | /* GCC accepts that */ |
||
919 | static int tab_reinit[]; |
||
920 | static int tab_reinit[10]; |
||
921 | |||
922 | //int cinit1; /* a global variable can be defined several times without error ! */ |
||
923 | int cinit1; |
||
924 | int cinit1; |
||
925 | int cinit1 = 0; |
||
926 | int *cinit2 = (int []){3, 2, 1}; |
||
927 | |||
928 | void compound_literal_test(void) |
||
929 | { |
||
930 | int *p, i; |
||
931 | char *q, *q3; |
||
932 | |||
933 | printf("compound_test:\n"); |
||
934 | |||
935 | p = (int []){1, 2, 3}; |
||
936 | for(i=0;i<3;i++) |
||
937 | printf(" %d", p[i]); |
||
938 | printf("\n"); |
||
939 | |||
940 | for(i=0;i<3;i++) |
||
941 | printf("%d", cinit2[i]); |
||
942 | printf("\n"); |
||
943 | |||
944 | q = "tralala1"; |
||
945 | printf("q1=%s\n", q); |
||
946 | |||
947 | q = (char *){ "tralala2" }; |
||
948 | printf("q2=%s\n", q); |
||
949 | |||
950 | q3 = (char *){ q }; |
||
951 | printf("q3=%s\n", q3); |
||
952 | |||
953 | q = (char []){ "tralala3" }; |
||
954 | printf("q4=%s\n", q); |
||
955 | |||
956 | #ifdef ALL_ISOC99 |
||
957 | p = (int []){1, 2, cinit1 + 3}; |
||
958 | for(i=0;i<3;i++) |
||
959 | printf(" %d", p[i]); |
||
960 | printf("\n"); |
||
961 | |||
962 | for(i=0;i<3;i++) { |
||
963 | p = (int []){1, 2, 4 + i}; |
||
964 | printf("%d %d %d\n", |
||
965 | p[0], |
||
966 | p[1], |
||
967 | p[2]); |
||
968 | } |
||
969 | #endif |
||
970 | } |
||
971 | |||
972 | /* K & R protos */ |
||
973 | |||
974 | kr_func1(a, b) |
||
975 | { |
||
976 | return a + b; |
||
977 | } |
||
978 | |||
979 | int kr_func2(a, b) |
||
980 | { |
||
981 | return a + b; |
||
982 | } |
||
983 | |||
984 | kr_test() |
||
985 | { |
||
986 | printf("kr_test:\n"); |
||
987 | printf("func1=%d\n", kr_func1(3, 4)); |
||
988 | printf("func2=%d\n", kr_func2(3, 4)); |
||
989 | return 0; |
||
990 | } |
||
991 | |||
992 | void num(int n) |
||
993 | { |
||
994 | char *tab, *p; |
||
995 | tab = (char*)malloc(20); |
||
996 | p = tab; |
||
997 | while (1) { |
||
998 | *p = 48 + (n % 10); |
||
999 | p++; |
||
1000 | n = n / 10; |
||
1001 | if (n == 0) |
||
1002 | break; |
||
1003 | } |
||
1004 | while (p != tab) { |
||
1005 | p--; |
||
1006 | printf("%c", *p); |
||
1007 | } |
||
1008 | printf("\n"); |
||
1009 | } |
||
1010 | |||
1011 | /* structure assignment tests */ |
||
1012 | struct structa1 { |
||
1013 | int f1; |
||
1014 | char f2; |
||
1015 | }; |
||
1016 | |||
1017 | struct structa1 ssta1; |
||
1018 | |||
1019 | void struct_assign_test1(struct structa1 s1, int t) |
||
1020 | { |
||
1021 | printf("%d %d %d\n", s1.f1, s1.f2, t); |
||
1022 | } |
||
1023 | |||
1024 | struct structa1 struct_assign_test2(struct structa1 s1, int t) |
||
1025 | { |
||
1026 | s1.f1 += t; |
||
1027 | s1.f2 -= t; |
||
1028 | return s1; |
||
1029 | } |
||
1030 | |||
1031 | void struct_assign_test(void) |
||
1032 | { |
||
1033 | struct structa1 lsta1, lsta2; |
||
1034 | |||
1035 | #if 0 |
||
1036 | printf("struct_assign_test:\n"); |
||
1037 | |||
1038 | lsta1.f1 = 1; |
||
1039 | lsta1.f2 = 2; |
||
1040 | printf("%d %d\n", lsta1.f1, lsta1.f2); |
||
1041 | lsta2 = lsta1; |
||
1042 | printf("%d %d\n", lsta2.f1, lsta2.f2); |
||
1043 | #else |
||
1044 | lsta2.f1 = 1; |
||
1045 | lsta2.f2 = 2; |
||
1046 | #endif |
||
1047 | struct_assign_test1(lsta2, 3); |
||
1048 | |||
1049 | printf("before call: %d %d\n", lsta2.f1, lsta2.f2); |
||
1050 | lsta2 = struct_assign_test2(lsta2, 4); |
||
1051 | printf("after call: %d %d\n", lsta2.f1, lsta2.f2); |
||
1052 | } |
||
1053 | |||
1054 | /* casts to short/char */ |
||
1055 | |||
1056 | void cast1(char a, short b, unsigned char c, unsigned short d) |
||
1057 | { |
||
1058 | printf("%d %d %d %d\n", a, b, c, d); |
||
1059 | } |
||
1060 | |||
1061 | char bcast; |
||
1062 | short scast; |
||
1063 | |||
1064 | void cast_test() |
||
1065 | { |
||
1066 | int a; |
||
1067 | char c; |
||
1068 | char tab[10]; |
||
1069 | |||
1070 | printf("cast_test:\n"); |
||
1071 | a = 0xfffff; |
||
1072 | cast1(a, a, a, a); |
||
1073 | a = 0xffffe; |
||
1074 | printf("%d %d %d %d\n", |
||
1075 | (char)(a + 1), |
||
1076 | (short)(a + 1), |
||
1077 | (unsigned char)(a + 1), |
||
1078 | (unsigned short)(a + 1)); |
||
1079 | printf("%d %d %d %d\n", |
||
1080 | (char)0xfffff, |
||
1081 | (short)0xfffff, |
||
1082 | (unsigned char)0xfffff, |
||
1083 | (unsigned short)0xfffff); |
||
1084 | |||
1085 | a = (bcast = 128) + 1; |
||
1086 | printf("%d\n", a); |
||
1087 | a = (scast = 65536) + 1; |
||
1088 | printf("%d\n", a); |
||
1089 | |||
1090 | printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c)); |
||
1091 | |||
1092 | /* test implicit int casting for array accesses */ |
||
1093 | c = 0; |
||
1094 | tab[1] = 2; |
||
1095 | tab[c] = 1; |
||
1096 | printf("%d %d\n", tab[0], tab[1]); |
||
1097 | |||
1098 | /* test implicit casting on some operators */ |
||
1099 | printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a')); |
||
1100 | printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a')); |
||
1101 | printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a')); |
||
1102 | } |
||
1103 | |||
1104 | /* initializers tests */ |
||
1105 | struct structinit1 { |
||
1106 | int f1; |
||
1107 | char f2; |
||
1108 | short f3; |
||
1109 | int farray[3]; |
||
1110 | }; |
||
1111 | |||
1112 | int sinit1 = 2; |
||
1113 | int sinit2 = { 3 }; |
||
1114 | int sinit3[3] = { 1, 2, {{3}}, }; |
||
1115 | int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} }; |
||
1116 | int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 }; |
||
1117 | int sinit6[] = { 1, 2, 3 }; |
||
1118 | int sinit7[] = { [2] = 3, [0] = 1, 2 }; |
||
1119 | char sinit8[] = "hello" "trala"; |
||
1120 | |||
1121 | struct structinit1 sinit9 = { 1, 2, 3 }; |
||
1122 | struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 }; |
||
1123 | struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1, |
||
1124 | #ifdef ALL_ISOC99 |
||
1125 | .farray[0] = 10, |
||
1126 | .farray[1] = 11, |
||
1127 | .farray[2] = 12, |
||
1128 | #endif |
||
1129 | }; |
||
1130 | |||
1131 | char *sinit12 = "hello world"; |
||
1132 | char *sinit13[] = { |
||
1133 | "test1", |
||
1134 | "test2", |
||
1135 | "test3", |
||
1136 | }; |
||
1137 | char sinit14[10] = { "abc" }; |
||
1138 | int sinit15[3] = { sizeof(sinit15), 1, 2 }; |
||
1139 | |||
1140 | struct { int a[3], b; } sinit16[] = { { 1 }, 2 }; |
||
1141 | |||
1142 | struct bar { |
||
1143 | char *s; |
||
1144 | int len; |
||
1145 | } sinit17[] = { |
||
1146 | "a1", 4, |
||
1147 | "a2", 1 |
||
1148 | }; |
||
1149 | |||
1150 | int sinit18[10] = { |
||
1151 | [2 ... 5] = 20, |
||
1152 | 2, |
||
1153 | [8] = 10, |
||
1154 | }; |
||
1155 | |||
1156 | void init_test(void) |
||
1157 | { |
||
1158 | int linit1 = 2; |
||
1159 | int linit2 = { 3 }; |
||
1160 | int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} }; |
||
1161 | int linit6[] = { 1, 2, 3 }; |
||
1162 | int i, j; |
||
1163 | char linit8[] = "hello" "trala"; |
||
1164 | int linit12[10] = { 1, 2 }; |
||
1165 | int linit13[10] = { 1, 2, [7] = 3, [3] = 4, }; |
||
1166 | char linit14[10] = "abc"; |
||
1167 | int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, }; |
||
1168 | struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 }; |
||
1169 | int linit17 = sizeof(linit17); |
||
1170 | |||
1171 | printf("init_test:\n"); |
||
1172 | |||
1173 | printf("sinit1=%d\n", sinit1); |
||
1174 | printf("sinit2=%d\n", sinit2); |
||
1175 | printf("sinit3=%d %d %d %d\n", |
||
1176 | sizeof(sinit3), |
||
1177 | sinit3[0], |
||
1178 | sinit3[1], |
||
1179 | sinit3[2] |
||
1180 | ); |
||
1181 | printf("sinit6=%d\n", sizeof(sinit6)); |
||
1182 | printf("sinit7=%d %d %d %d\n", |
||
1183 | sizeof(sinit7), |
||
1184 | sinit7[0], |
||
1185 | sinit7[1], |
||
1186 | sinit7[2] |
||
1187 | ); |
||
1188 | printf("sinit8=%s\n", sinit8); |
||
1189 | printf("sinit9=%d %d %d\n", |
||
1190 | sinit9.f1, |
||
1191 | sinit9.f2, |
||
1192 | sinit9.f3 |
||
1193 | ); |
||
1194 | printf("sinit10=%d %d %d\n", |
||
1195 | sinit10.f1, |
||
1196 | sinit10.f2, |
||
1197 | sinit10.f3 |
||
1198 | ); |
||
1199 | printf("sinit11=%d %d %d %d %d %d\n", |
||
1200 | sinit11.f1, |
||
1201 | sinit11.f2, |
||
1202 | sinit11.f3, |
||
1203 | sinit11.farray[0], |
||
1204 | sinit11.farray[1], |
||
1205 | sinit11.farray[2] |
||
1206 | ); |
||
1207 | |||
1208 | for(i=0;i<3;i++) |
||
1209 | for(j=0;j<2;j++) |
||
1210 | printf("[%d][%d] = %d %d %d\n", |
||
1211 | i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]); |
||
1212 | printf("linit1=%d\n", linit1); |
||
1213 | printf("linit2=%d\n", linit2); |
||
1214 | printf("linit6=%d\n", sizeof(linit6)); |
||
1215 | printf("linit8=%d %s\n", sizeof(linit8), linit8); |
||
1216 | |||
1217 | printf("sinit12=%s\n", sinit12); |
||
1218 | printf("sinit13=%d %s %s %s\n", |
||
1219 | sizeof(sinit13), |
||
1220 | sinit13[0], |
||
1221 | sinit13[1], |
||
1222 | sinit13[2]); |
||
1223 | printf("sinit14=%s\n", sinit14); |
||
1224 | |||
1225 | for(i=0;i<10;i++) printf(" %d", linit12[i]); |
||
1226 | printf("\n"); |
||
1227 | for(i=0;i<10;i++) printf(" %d", linit13[i]); |
||
1228 | printf("\n"); |
||
1229 | for(i=0;i<10;i++) printf(" %d", linit14[i]); |
||
1230 | printf("\n"); |
||
1231 | for(i=0;i<10;i++) printf(" %d", linit15[i]); |
||
1232 | printf("\n"); |
||
1233 | printf("%d %d %d %d\n", |
||
1234 | linit16.a1, |
||
1235 | linit16.a2, |
||
1236 | linit16.a3, |
||
1237 | linit16.a4); |
||
1238 | /* test that initialisation is done after variable declare */ |
||
1239 | printf("linit17=%d\n", linit17); |
||
1240 | printf("sinit15=%d\n", sinit15[0]); |
||
1241 | printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]); |
||
1242 | printf("sinit17=%s %d %s %d\n", |
||
1243 | sinit17[0].s, sinit17[0].len, |
||
1244 | sinit17[1].s, sinit17[1].len); |
||
1245 | for(i=0;i<10;i++) |
||
1246 | printf("%x ", sinit18[i]); |
||
1247 | printf("\n"); |
||
1248 | } |
||
1249 | |||
1250 | |||
1251 | void switch_test() |
||
1252 | { |
||
1253 | int i; |
||
1254 | |||
1255 | for(i=0;i<15;i++) { |
||
1256 | switch(i) { |
||
1257 | case 0: |
||
1258 | case 1: |
||
1259 | printf("a"); |
||
1260 | break; |
||
1261 | default: |
||
1262 | printf("%d", i); |
||
1263 | break; |
||
1264 | case 8 ... 12: |
||
1265 | printf("c"); |
||
1266 | break; |
||
1267 | case 3: |
||
1268 | printf("b"); |
||
1269 | break; |
||
1270 | } |
||
1271 | } |
||
1272 | printf("\n"); |
||
1273 | } |
||
1274 | |||
1275 | /* ISOC99 _Bool type */ |
||
1276 | void c99_bool_test(void) |
||
1277 | { |
||
1278 | #ifdef BOOL_ISOC99 |
||
1279 | int a; |
||
1280 | _Bool b; |
||
1281 | |||
1282 | printf("bool_test:\n"); |
||
1283 | printf("sizeof(_Bool) = %d\n", sizeof(_Bool)); |
||
1284 | a = 3; |
||
1285 | printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a); |
||
1286 | b = 3; |
||
1287 | printf("b = %d\n", b); |
||
1288 | b++; |
||
1289 | printf("b = %d\n", b); |
||
1290 | #endif |
||
1291 | } |
||
1292 | |||
1293 | void bitfield_test(void) |
||
1294 | { |
||
1295 | int a; |
||
1296 | struct sbf1 { |
||
1297 | int f1 : 3; |
||
1298 | int : 2; |
||
1299 | int f2 : 1; |
||
1300 | int : 0; |
||
1301 | int f3 : 5; |
||
1302 | int f4 : 7; |
||
1303 | unsigned int f5 : 7; |
||
1304 | } st1; |
||
1305 | printf("bitfield_test:"); |
||
1306 | printf("sizeof(st1) = %d\n", sizeof(st1)); |
||
1307 | |||
1308 | st1.f1 = 3; |
||
1309 | st1.f2 = 1; |
||
1310 | st1.f3 = 15; |
||
1311 | a = 120; |
||
1312 | st1.f4 = a; |
||
1313 | st1.f5 = a; |
||
1314 | st1.f5++; |
||
1315 | printf("%d %d %d %d %d\n", |
||
1316 | st1.f1, st1.f2, st1.f3, st1.f4, st1.f5); |
||
1317 | |||
1318 | st1.f1 = 7; |
||
1319 | if (st1.f1 == -1) |
||
1320 | printf("st1.f1 == -1\n"); |
||
1321 | else |
||
1322 | printf("st1.f1 != -1\n"); |
||
1323 | if (st1.f2 == -1) |
||
1324 | printf("st1.f2 == -1\n"); |
||
1325 | else |
||
1326 | printf("st1.f2 != -1\n"); |
||
1327 | } |
||
1328 | |||
1329 | #define FTEST(prefix, type, fmt)\ |
||
1330 | void prefix ## cmp(type a, type b)\ |
||
1331 | {\ |
||
1332 | printf("%d %d %d %d %d %d\n",\ |
||
1333 | a == b,\ |
||
1334 | a != b,\ |
||
1335 | a < b,\ |
||
1336 | a > b,\ |
||
1337 | a >= b,\ |
||
1338 | a <= b);\ |
||
1339 | printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\ |
||
1340 | a,\ |
||
1341 | b,\ |
||
1342 | a + b,\ |
||
1343 | a - b,\ |
||
1344 | a * b,\ |
||
1345 | a / b,\ |
||
1346 | -a);\ |
||
1347 | printf(fmt "\n", ++a);\ |
||
1348 | printf(fmt "\n", a++);\ |
||
1349 | printf(fmt "\n", a);\ |
||
1350 | }\ |
||
1351 | void prefix ## fcast(type a)\ |
||
1352 | {\ |
||
1353 | float fa;\ |
||
1354 | double da;\ |
||
1355 | long double la;\ |
||
1356 | int ia;\ |
||
1357 | unsigned int ua;\ |
||
1358 | type b;\ |
||
1359 | fa = a;\ |
||
1360 | da = a;\ |
||
1361 | la = a;\ |
||
1362 | printf("ftof: %f %f %Lf\n", fa, da, la);\ |
||
1363 | ia = (int)a;\ |
||
1364 | ua = (unsigned int)a;\ |
||
1365 | printf("ftoi: %d %u\n", ia, ua);\ |
||
1366 | ia = -1234;\ |
||
1367 | ua = 0x81234500;\ |
||
1368 | b = ia;\ |
||
1369 | printf("itof: " fmt "\n", b);\ |
||
1370 | b = ua;\ |
||
1371 | printf("utof: " fmt "\n", b);\ |
||
1372 | }\ |
||
1373 | \ |
||
1374 | void prefix ## test(void)\ |
||
1375 | {\ |
||
1376 | printf("testing '%s'\n", #type);\ |
||
1377 | prefix ## cmp(1, 2.5);\ |
||
1378 | prefix ## cmp(2, 1.5);\ |
||
1379 | prefix ## cmp(1, 1);\ |
||
1380 | prefix ## fcast(234.6);\ |
||
1381 | prefix ## fcast(-2334.6);\ |
||
1382 | } |
||
1383 | |||
1384 | FTEST(f, float, "%f") |
||
1385 | FTEST(d, double, "%f") |
||
1386 | FTEST(ld, long double, "%Lf") |
||
1387 | |||
1388 | double ftab1[3] = { 1.2, 3.4, -5.6 }; |
||
1389 | |||
1390 | |||
1391 | void float_test(void) |
||
1392 | { |
||
1393 | float fa, fb; |
||
1394 | double da, db; |
||
1395 | int a; |
||
1396 | unsigned int b; |
||
1397 | |||
1398 | printf("float_test:\n"); |
||
1399 | printf("sizeof(float) = %d\n", sizeof(float)); |
||
1400 | printf("sizeof(double) = %d\n", sizeof(double)); |
||
1401 | printf("sizeof(long double) = %d\n", sizeof(long double)); |
||
1402 | ftest(); |
||
1403 | dtest(); |
||
1404 | ldtest(); |
||
1405 | printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]); |
||
1406 | printf("%f %f %f\n", 2.12, .5, 2.3e10); |
||
1407 | // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10); |
||
1408 | da = 123; |
||
1409 | printf("da=%f\n", da); |
||
1410 | fa = 123; |
||
1411 | printf("fa=%f\n", fa); |
||
1412 | a = 4000000000; |
||
1413 | da = a; |
||
1414 | printf("da = %f\n", da); |
||
1415 | b = 4000000000; |
||
1416 | db = b; |
||
1417 | printf("db = %f\n", db); |
||
1418 | } |
||
1419 | |||
1420 | int fib(int n) |
||
1421 | { |
||
1422 | if (n <= 2) |
||
1423 | return 1; |
||
1424 | else |
||
1425 | return fib(n-1) + fib(n-2); |
||
1426 | } |
||
1427 | |||
1428 | void funcptr_test() |
||
1429 | { |
||
1430 | void (*func)(int); |
||
1431 | int a; |
||
1432 | struct { |
||
1433 | int dummy; |
||
1434 | void (*func)(int); |
||
1435 | } st1; |
||
1436 | |||
1437 | printf("funcptr:\n"); |
||
1438 | func = # |
||
1439 | (*func)(12345); |
||
1440 | func = num; |
||
1441 | a = 1; |
||
1442 | a = 1; |
||
1443 | func(12345); |
||
1444 | /* more complicated pointer computation */ |
||
1445 | st1.func = num; |
||
1446 | st1.func(12346); |
||
1447 | printf("sizeof1 = %d\n", sizeof(funcptr_test)); |
||
1448 | printf("sizeof2 = %d\n", sizeof funcptr_test); |
||
1449 | printf("sizeof3 = %d\n", sizeof(&funcptr_test)); |
||
1450 | printf("sizeof4 = %d\n", sizeof &funcptr_test); |
||
1451 | } |
||
1452 | |||
1453 | void lloptest(long long a, long long b) |
||
1454 | { |
||
1455 | unsigned long long ua, ub; |
||
1456 | |||
1457 | ua = a; |
||
1458 | ub = b; |
||
1459 | /* arith */ |
||
1460 | printf("arith: %Ld %Ld %Ld\n", |
||
1461 | a + b, |
||
1462 | a - b, |
||
1463 | a * b); |
||
1464 | |||
1465 | if (b != 0) { |
||
1466 | printf("arith1: %Ld %Ld\n", |
||
1467 | a / b, |
||
1468 | a % b); |
||
1469 | } |
||
1470 | |||
1471 | /* binary */ |
||
1472 | printf("bin: %Ld %Ld %Ld\n", |
||
1473 | a & b, |
||
1474 | a | b, |
||
1475 | a ^ b); |
||
1476 | |||
1477 | /* tests */ |
||
1478 | printf("test: %d %d %d %d %d %d\n", |
||
1479 | a == b, |
||
1480 | a != b, |
||
1481 | a < b, |
||
1482 | a > b, |
||
1483 | a >= b, |
||
1484 | a <= b); |
||
1485 | |||
1486 | printf("utest: %d %d %d %d %d %d\n", |
||
1487 | ua == ub, |
||
1488 | ua != ub, |
||
1489 | ua < ub, |
||
1490 | ua > ub, |
||
1491 | ua >= ub, |
||
1492 | ua <= ub); |
||
1493 | |||
1494 | /* arith2 */ |
||
1495 | a++; |
||
1496 | b++; |
||
1497 | printf("arith2: %Ld %Ld\n", a, b); |
||
1498 | printf("arith2: %Ld %Ld\n", a++, b++); |
||
1499 | printf("arith2: %Ld %Ld\n", --a, --b); |
||
1500 | printf("arith2: %Ld %Ld\n", a, b); |
||
1501 | } |
||
1502 | |||
1503 | void llshift(long long a, int b) |
||
1504 | { |
||
1505 | printf("shift: %Ld %Ld %Ld\n", |
||
1506 | (unsigned long long)a >> b, |
||
1507 | a >> b, |
||
1508 | a << b); |
||
1509 | printf("shiftc: %Ld %Ld %Ld\n", |
||
1510 | (unsigned long long)a >> 3, |
||
1511 | a >> 3, |
||
1512 | a << 3); |
||
1513 | printf("shiftc: %Ld %Ld %Ld\n", |
||
1514 | (unsigned long long)a >> 35, |
||
1515 | a >> 35, |
||
1516 | a << 35); |
||
1517 | } |
||
1518 | |||
1519 | void llfloat(void) |
||
1520 | { |
||
1521 | float fa; |
||
1522 | double da; |
||
1523 | long double lda; |
||
1524 | long long la, lb, lc; |
||
1525 | unsigned long long ula, ulb, ulc; |
||
1526 | la = 0x12345678; |
||
1527 | ula = 0x72345678; |
||
1528 | la = (la << 20) | 0x12345; |
||
1529 | ula = ula << 33; |
||
1530 | printf("la=%Ld ula=%Lu\n", la, ula); |
||
1531 | |||
1532 | fa = la; |
||
1533 | da = la; |
||
1534 | lda = la; |
||
1535 | printf("lltof: %f %f %Lf\n", fa, da, lda); |
||
1536 | |||
1537 | la = fa; |
||
1538 | lb = da; |
||
1539 | lc = lda; |
||
1540 | printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc); |
||
1541 | |||
1542 | fa = ula; |
||
1543 | da = ula; |
||
1544 | lda = ula; |
||
1545 | printf("ulltof: %f %f %Lf\n", fa, da, lda); |
||
1546 | |||
1547 | ula = fa; |
||
1548 | ulb = da; |
||
1549 | ulc = lda; |
||
1550 | printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc); |
||
1551 | } |
||
1552 | |||
1553 | long long llfunc1(int a) |
||
1554 | { |
||
1555 | return a * 2; |
||
1556 | } |
||
1557 | |||
1558 | struct S { |
||
1559 | int id; |
||
1560 | char item; |
||
1561 | }; |
||
1562 | |||
1563 | long long int value(struct S *v) |
||
1564 | { |
||
1565 | return ((long long int)v->item); |
||
1566 | } |
||
1567 | |||
1568 | void longlong_test(void) |
||
1569 | { |
||
1570 | long long a, b, c; |
||
1571 | int ia; |
||
1572 | unsigned int ua; |
||
1573 | printf("longlong_test:\n"); |
||
1574 | printf("sizeof(long long) = %d\n", sizeof(long long)); |
||
1575 | ia = -1; |
||
1576 | ua = -2; |
||
1577 | a = ia; |
||
1578 | b = ua; |
||
1579 | printf("%Ld %Ld\n", a, b); |
||
1580 | printf("%Ld %Ld %Ld %Lx\n", |
||
1581 | (long long)1, |
||
1582 | (long long)-2, |
||
1583 | 1LL, |
||
1584 | 0x1234567812345679); |
||
1585 | a = llfunc1(-3); |
||
1586 | printf("%Ld\n", a); |
||
1587 | |||
1588 | lloptest(1000, 23); |
||
1589 | lloptest(0xff, 0x1234); |
||
1590 | b = 0x72345678 << 10; |
||
1591 | lloptest(-3, b); |
||
1592 | llshift(0x123, 5); |
||
1593 | llshift(-23, 5); |
||
1594 | b = 0x72345678LL << 10; |
||
1595 | llshift(b, 47); |
||
1596 | |||
1597 | llfloat(); |
||
1598 | #if 1 |
||
1599 | b = 0x12345678; |
||
1600 | a = -1; |
||
1601 | c = a + b; |
||
1602 | printf("%Lx\n", c); |
||
1603 | #endif |
||
1604 | |||
1605 | /* long long reg spill test */ |
||
1606 | { |
||
1607 | struct S a; |
||
1608 | |||
1609 | a.item = 3; |
||
1610 | printf("%lld\n", value(&a)); |
||
1611 | } |
||
1612 | lloptest(0x80000000, 0); |
||
1613 | |||
1614 | /* another long long spill test */ |
||
1615 | { |
||
1616 | long long *p, v; |
||
1617 | v = 1; |
||
1618 | p = &v; |
||
1619 | p[0]++; |
||
1620 | printf("%lld\n", *p); |
||
1621 | } |
||
1622 | } |
||
1623 | |||
1624 | void vprintf1(const char *fmt, ...) |
||
1625 | { |
||
1626 | va_list ap; |
||
1627 | const char *p; |
||
1628 | int c, i; |
||
1629 | double d; |
||
1630 | long long ll; |
||
1631 | |||
1632 | va_start(ap, fmt); |
||
1633 | |||
1634 | p = fmt; |
||
1635 | for(;;) { |
||
1636 | c = *p; |
||
1637 | if (c == '\0') |
||
1638 | break; |
||
1639 | p++; |
||
1640 | if (c == '%') { |
||
1641 | c = *p; |
||
1642 | switch(c) { |
||
1643 | case '\0': |
||
1644 | goto the_end; |
||
1645 | case 'd': |
||
1646 | i = va_arg(ap, int); |
||
1647 | printf("%d", i); |
||
1648 | break; |
||
1649 | case 'f': |
||
1650 | d = va_arg(ap, double); |
||
1651 | printf("%f", d); |
||
1652 | break; |
||
1653 | case 'l': |
||
1654 | ll = va_arg(ap, long long); |
||
1655 | printf("%Ld", ll); |
||
1656 | break; |
||
1657 | } |
||
1658 | p++; |
||
1659 | } else { |
||
1660 | putchar(c); |
||
1661 | } |
||
1662 | } |
||
1663 | the_end: |
||
1664 | va_end(ap); |
||
1665 | } |
||
1666 | |||
1667 | |||
1668 | void stdarg_test(void) |
||
1669 | { |
||
1670 | vprintf1("%d %d %d\n", 1, 2, 3); |
||
1671 | vprintf1("%f %d %f\n", 1.0, 2, 3.0); |
||
1672 | vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0); |
||
1673 | } |
||
1674 | |||
1675 | void whitespace_test(void) |
||
1676 | { |
||
1677 | char *str; |
||
1678 | |||
1679 | |||
1680 | pri\ |
||
1681 | ntf("whitspace:\n"); |
||
1682 | #endif |
||
1683 | pf("N=%d\n", 2); |
||
1684 | |||
1685 | |||
1686 | pri\ |
||
1687 | ntf("aaa=%d\n", 3); |
||
1688 | #endif |
||
1689 | |||
1690 | |||
1691 | \ |
||
1692 | ntf("min=%d\n", 4); |
||
1693 | |||
1694 | |||
1695 | printf("len1=%d\n", strlen(" |
||
1696 | ")); |
||
1697 | #ifdef CORRECT_CR_HANDLING |
||
1698 | str = " |
||
1699 | "; |
||
1700 | printf("len1=%d str[0]=%d\n", strlen(str), str[0]); |
||
1701 | #endif |
||
1702 | printf("len1=%d\n", strlen(" a |
||
1703 | ")); |
||
1704 | #endif /* ACCEPT_CR_IN_STRINGS */ |
||
1705 | } |
||
1706 | |||
1707 | int reltab[3] = { 1, 2, 3 }; |
||
1708 | |||
1709 | int *rel1 = &reltab[1]; |
||
1710 | |||
1711 | |||
1712 | void relocation_test(void) |
||
1713 | |||
1714 | printf("*rel1=%d\n", *rel1); |
||
1715 | printf("*rel2=%d\n", *rel2); |
||
1716 | } |
||
1717 | |||
1718 | void old_style_f(a,b,c) |
||
1719 | |||
1720 | double c; |
||
1721 | { |
||
1722 | printf("a=%d b=%d b=%f\n", a, b, c); |
||
1723 | } |
||
1724 | |||
1725 | void decl_func1(int cmpfn()) |
||
1726 | |||
1727 | printf("cmpfn=%lx\n", (long)cmpfn); |
||
1728 | } |
||
1729 | |||
1730 | void decl_func2(cmpfn) |
||
1731 | |||
1732 | { |
||
1733 | printf("cmpfn=%lx\n", (long)cmpfn); |
||
1734 | } |
||
1735 | |||
1736 | void old_style_function(void) |
||
1737 | |||
1738 | old_style_f((void *)1, 2, 3.0); |
||
1739 | decl_func1(NULL); |
||
1740 | decl_func2(NULL); |
||
1741 | } |
||
1742 | |||
1743 | void sizeof_test(void) |
||
1744 | |||
1745 | int a; |
||
1746 | int **ptr; |
||
1747 | |||
1748 | printf("sizeof(int) = %d\n", sizeof(int)); |
||
1749 | |||
1750 | printf("sizeof(short) = %d\n", sizeof(short)); |
||
1751 | printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short)); |
||
1752 | printf("sizeof(char) = %d\n", sizeof(char)); |
||
1753 | printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char)); |
||
1754 | printf("sizeof(func) = %d\n", sizeof sizeof_test()); |
||
1755 | a = 1; |
||
1756 | printf("sizeof(a++) = %d\n", sizeof a++); |
||
1757 | printf("a=%d\n", a); |
||
1758 | ptr = NULL; |
||
1759 | printf("sizeof(**ptr) = %d\n", sizeof (**ptr)); |
||
1760 | |||
1761 | /* some alignof tests */ |
||
1762 | |||
1763 | printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int)); |
||
1764 | printf("__alignof__(short) = %d\n", __alignof__(short)); |
||
1765 | printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short)); |
||
1766 | printf("__alignof__(char) = %d\n", __alignof__(char)); |
||
1767 | printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char)); |
||
1768 | printf("__alignof__(func) = %d\n", __alignof__ sizeof_test()); |
||
1769 | } |
||
1770 | |||
1771 | void typeof_test(void) |
||
1772 | |||
1773 | double a; |
||
1774 | typeof(a) b; |
||
1775 | typeof(float) c; |
||
1776 | |||
1777 | a = 1.5; |
||
1778 | |||
1779 | c = 3.5; |
||
1780 | printf("a=%f b=%f c=%f\n", a, b, c); |
||
1781 | } |
||
1782 | |||
1783 | void statement_expr_test(void) |
||
1784 | |||
1785 | int a, i; |
||
1786 | |||
1787 | a = 0; |
||
1788 | |||
1789 | a += 1 + |
||
1790 | ( { int b, j; |
||
1791 | b = 0; |
||
1792 | for(j=0;j<5;j++) |
||
1793 | b += j; b; |
||
1794 | } ); |
||
1795 | } |
||
1796 | printf("a=%d\n", a); |
||
1797 | |||
1798 | } |
||
1799 | |||
1800 | void local_label_test(void) |
||
1801 | |||
1802 | int a; |
||
1803 | goto l1; |
||
1804 | l2: |
||
1805 | a = 1 + ({ |
||
1806 | __label__ l1, l2, l3; |
||
1807 | goto l4; |
||
1808 | l5: |
||
1809 | printf("aa1\n"); |
||
1810 | goto l1; |
||
1811 | l2: |
||
1812 | printf("aa3\n"); |
||
1813 | goto l3; |
||
1814 | l1: |
||
1815 | printf("aa2\n"); |
||
1816 | goto l2; |
||
1817 | l3:; |
||
1818 | 1; |
||
1819 | }); |
||
1820 | printf("a=%d\n", a); |
||
1821 | return; |
||
1822 | l1: |
||
1823 | printf("bb1\n"); |
||
1824 | goto l2; |
||
1825 | l4: |
||
1826 | printf("bb2\n"); |
||
1827 | goto l5; |
||
1828 | } |
||
1829 | |||
1830 | /* inline assembler test */ |
||
1831 | |||
1832 | |||
1833 | /* from linux kernel */ |
||
1834 | |||
1835 | { |
||
1836 | int d0, d1, d2, d3; |
||
1837 | __asm__ __volatile__( |
||
1838 | "repne\n\t" |
||
1839 | "scasb\n\t" |
||
1840 | "decl %1\n\t" |
||
1841 | "movl %8,%3\n" |
||
1842 | "1:\tdecl %3\n\t" |
||
1843 | "js 2f\n\t" |
||
1844 | "lodsb\n\t" |
||
1845 | "stosb\n\t" |
||
1846 | "testb %%al,%%al\n\t" |
||
1847 | "jne 1b\n" |
||
1848 | "2:\txorl %2,%2\n\t" |
||
1849 | "stosb" |
||
1850 | : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3) |
||
1851 | : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count) |
||
1852 | : "memory"); |
||
1853 | return dest; |
||
1854 | } |
||
1855 | |||
1856 | static inline void * memcpy1(void * to, const void * from, size_t n) |
||
1857 | |||
1858 | int d0, d1, d2; |
||
1859 | __asm__ __volatile__( |
||
1860 | "rep ; movsl\n\t" |
||
1861 | "testb $2,%b4\n\t" |
||
1862 | "je 1f\n\t" |
||
1863 | "movsw\n" |
||
1864 | "1:\ttestb $1,%b4\n\t" |
||
1865 | "je 2f\n\t" |
||
1866 | "movsb\n" |
||
1867 | "2:" |
||
1868 | : "=&c" (d0), "=&D" (d1), "=&S" (d2) |
||
1869 | :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from) |
||
1870 | : "memory"); |
||
1871 | return (to); |
||
1872 | } |
||
1873 | |||
1874 | static __inline__ void sigaddset1(unsigned int *set, int _sig) |
||
1875 | |||
1876 | __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc"); |
||
1877 | } |
||
1878 | |||
1879 | static __inline__ void sigdelset1(unsigned int *set, int _sig) |
||
1880 | |||
1881 | asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc"); |
||
1882 | } |
||
1883 | |||
1884 | static __inline__ __const__ unsigned int swab32(unsigned int x) |
||
1885 | |||
1886 | __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */ |
||
1887 | "rorl $16,%0\n\t" /* swap words */ |
||
1888 | "xchgb %b0,%h0" /* swap higher bytes */ |
||
1889 | :"=q" (x) |
||
1890 | : "0" (x)); |
||
1891 | return x; |
||
1892 | } |
||
1893 | |||
1894 | static __inline__ unsigned long long mul64(unsigned int a, unsigned int b) |
||
1895 | |||
1896 | unsigned long long res; |
||
1897 | __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b)); |
||
1898 | return res; |
||
1899 | } |
||
1900 | |||
1901 | static __inline__ unsigned long long inc64(unsigned long long a) |
||
1902 | |||
1903 | unsigned long long res; |
||
1904 | __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a)); |
||
1905 | return res; |
||
1906 | } |
||
1907 | |||
1908 | unsigned int set; |
||
1909 | |||
1910 | void asm_test(void) |
||
1911 | |||
1912 | char buf[128]; |
||
1913 | unsigned int val; |
||
1914 | |||
1915 | printf("inline asm:\n"); |
||
1916 | |||
1917 | asm volatile ("xorl %eax, %eax"); |
||
1918 | |||
1919 | memcpy1(buf, "hello", 6); |
||
1920 | |||
1921 | printf("%s\n", buf); |
||
1922 | |||
1923 | /* 'A' constraint test */ |
||
1924 | |||
1925 | printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff)); |
||
1926 | |||
1927 | set = 0xff; |
||
1928 | |||
1929 | sigaddset1(&set, 16); |
||
1930 | /* NOTE: we test here if C labels are correctly restored after the |
||
1931 | asm statement */ |
||
1932 | goto label1; |
||
1933 | label2: |
||
1934 | __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc"); |
||
1935 | printf("set=0x%x\n", set); |
||
1936 | val = 0x01020304; |
||
1937 | printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val)); |
||
1938 | return; |
||
1939 | label1: |
||
1940 | goto label2; |
||
1941 | } |
||
1942 | |||
1943 | #else |
||
1944 | |||
1945 | void asm_test(void) |
||
1946 | |||
1947 | } |
||
1948 | |||
1949 | #endif |
||
1950 | |||
1951 | #define COMPAT_TYPE(type1, type2) \ |
||
1952 | |||
1953 | printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \ |
||
1954 | __builtin_types_compatible_p (type1, type2));\ |
||
1955 | } |
||
1956 | |||
1957 | int constant_p_var; |
||
1958 | |||
1959 | void builtin_test(void) |
||
1960 | |||
1961 | #if GCC_MAJOR >= 3 |
||
1962 | COMPAT_TYPE(int, int); |
||
1963 | COMPAT_TYPE(int, unsigned int); |
||
1964 | COMPAT_TYPE(int, char); |
||
1965 | COMPAT_TYPE(int, const int); |
||
1966 | COMPAT_TYPE(int, volatile int); |
||
1967 | COMPAT_TYPE(int *, int *); |
||
1968 | COMPAT_TYPE(int *, void *); |
||
1969 | COMPAT_TYPE(int *, const int *); |
||
1970 | COMPAT_TYPE(char *, unsigned char *); |
||
1971 | /* space is needed because tcc preprocessor introduces a space between each token */ |
||
1972 | COMPAT_TYPE(char * *, void *); |
||
1973 | #endif |
||
1974 | printf("res = %d\n", __builtin_constant_p(1)); |
||
1975 | printf("res = %d\n", __builtin_constant_p(1 + 2)); |
||
1976 | printf("res = %d\n", __builtin_constant_p(&constant_p_var)); |
||
1977 | printf("res = %d\n", __builtin_constant_p(constant_p_var)); |
||
1978 | } |
||
1979 | |||
1980 | |||
1981 | |||
1982 | |||
1983 | } |
||
1984 | |||
1985 | void const_warn_test(void) |
||
1986 | |||
1987 | const_func(1); |
||
1988 | }5;j++)>10;i++)>><>><>><>><>><>><>><>=>>=>>=>=>>15;i++)>10;i++) |
||
1989 | >10;i++)>10;i++)>10;i++)>10;i++)>2;j++) |
||
1990 | >3;i++) |