Make type equality comparisons more generous.
[smatch.git] / show-parse.c
bloba08761f1d5310b6da8ecf32df80f82b5037b87bc
1 /*
2 * sparse/show-parse.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Print out results of parsing for debugging and testing.
11 #include <stdarg.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <unistd.h>
17 #include <fcntl.h>
19 #include "lib.h"
20 #include "token.h"
21 #include "parse.h"
22 #include "symbol.h"
23 #include "scope.h"
24 #include "expression.h"
25 #include "target.h"
27 static int show_symbol_expr(struct symbol *sym);
29 static void do_debug_symbol(struct symbol *sym, int indent)
31 static const char indent_string[] = " ";
32 static const char *typestr[] = {
33 "base", "node", "ptr.", "fn..",
34 "arry", "strt", "unin", "enum",
35 "tdef", "tpof", "memb", "bitf",
36 "labl"
39 if (!sym)
40 return;
41 fprintf(stderr, "%.*s%s%3d:%lu %lx %s (as: %d, context: %x:%x) %p (%s:%d:%d)\n",
42 indent, indent_string, typestr[sym->type],
43 sym->bit_size, sym->ctype.alignment,
44 sym->ctype.modifiers, show_ident(sym->ident),
45 sym->ctype.as, sym->ctype.context, sym->ctype.contextmask,
46 sym, input_streams[sym->pos.stream].name, sym->pos.line, sym->pos.pos);
47 if (sym->type == SYM_FN) {
48 int i = 1;
49 struct symbol *arg;
50 FOR_EACH_PTR(sym->arguments, arg) {
51 fprintf(stderr, "< arg%d:\n", i);
52 do_debug_symbol(arg, 0);
53 fprintf(stderr, " end arg%d >\n", i);
54 i++;
55 } END_FOR_EACH_PTR;
57 do_debug_symbol(sym->ctype.base_type, indent+2);
60 void debug_symbol(struct symbol *sym)
62 do_debug_symbol(sym, 0);
66 * Symbol type printout. The type system is by far the most
67 * complicated part of C - everything else is trivial.
69 const char *modifier_string(unsigned long mod)
71 static char buffer[100];
72 char *p = buffer;
73 const char *res,**ptr, *names[] = {
74 "auto", "register", "static", "extern",
75 "const", "volatile", "[signed]", "[unsigned]",
76 "[char]", "[short]", "[long]", "[long]",
77 "[typdef]", "[structof]", "[unionof]", "[enum]",
78 "[typeof]", "[attribute]", "inline", "[addressable]",
79 "[nocast]", "[noderef]",
80 NULL
82 ptr = names;
83 while ((res = *ptr++) != NULL) {
84 if (mod & 1) {
85 char c;
86 while ((c = *res++) != '\0')
87 *p++ = c;
88 *p++ = ' ';
90 mod >>= 1;
92 *p = 0;
93 return buffer;
96 void show_struct_member(struct symbol *sym, void *data, int flags)
98 if (flags & ITERATE_FIRST)
99 printf(" {\n\t");
100 printf("%s:%d:%ld at offset %ld", show_ident(sym->ident), sym->bit_size, sym->ctype.alignment, sym->offset);
101 if (sym->fieldwidth)
102 printf("[%d..%d]", sym->bit_offset, sym->bit_offset+sym->fieldwidth-1);
103 if (flags & ITERATE_LAST)
104 printf("\n} ");
105 else
106 printf(", ");
109 static void show_one_symbol(struct symbol *sym, void *sep, int flags)
111 show_symbol(sym);
112 if (!(flags & ITERATE_LAST))
113 printf("%s", (const char *)sep);
116 void show_symbol_list(struct symbol_list *list, const char *sep)
118 symbol_iterate(list, show_one_symbol, (void *)sep);
121 struct type_name {
122 char *start;
123 char *end;
126 static void prepend(struct type_name *name, const char *fmt, ...)
128 static char buffer[512];
129 int n;
131 va_list args;
132 va_start(args, fmt);
133 n = vsprintf(buffer, fmt, args);
134 va_end(args);
136 name->start -= n;
137 memcpy(name->start, buffer, n);
140 static void append(struct type_name *name, const char *fmt, ...)
142 static char buffer[512];
143 int n;
145 va_list args;
146 va_start(args, fmt);
147 n = vsprintf(buffer, fmt, args);
148 va_end(args);
150 memcpy(name->end, buffer, n);
151 name->end += n;
154 static void do_show_type(struct symbol *sym, struct type_name *name)
156 int i, modlen;
157 const char *mod;
158 static struct ctype_name {
159 struct symbol *sym;
160 char *name;
161 } typenames[] = {
162 { & char_ctype, "char" },
163 { &uchar_ctype, "unsigned char" },
164 { & short_ctype, "short" },
165 { &ushort_ctype, "unsigned short" },
166 { & int_ctype, "int" },
167 { &uint_ctype, "unsigned int" },
168 { & long_ctype, "long" },
169 { &ulong_ctype, "unsigned long" },
170 { & llong_ctype, "long long" },
171 { &ullong_ctype, "unsigned long long" },
173 { &void_ctype, "void" },
174 { &bool_ctype, "bool" },
175 { &string_ctype, "string" },
177 { &float_ctype, "float" },
178 { &double_ctype, "double" },
179 { &ldouble_ctype,"long double" },
182 if (!sym)
183 return;
185 for (i = 0; i < sizeof(typenames)/sizeof(typenames[0]); i++) {
186 if (typenames[i].sym == sym) {
187 int len = strlen(typenames[i].name);
188 *--name->start = ' ';
189 name->start -= len;
190 memcpy(name->start, typenames[i].name, len);
191 return;
195 /* Prepend */
196 switch (sym->type) {
197 case SYM_PTR:
198 prepend(name, "*");
199 break;
200 case SYM_FN:
201 prepend(name, "( ");
202 break;
203 case SYM_STRUCT:
204 prepend(name, "struct %s ", show_ident(sym->ident));
205 return;
207 case SYM_UNION:
208 prepend(name, "union %s ", show_ident(sym->ident));
209 return;
211 case SYM_ENUM:
212 prepend(name, "enum %s ", show_ident(sym->ident));
213 return;
215 case SYM_NODE:
216 append(name, "%s", show_ident(sym->ident));
217 break;
219 case SYM_BITFIELD:
220 append(name, ":%d", sym->fieldwidth);
221 break;
223 case SYM_LABEL:
224 append(name, "label(%s:%p)", show_ident(sym->ident), sym);
225 break;
227 case SYM_ARRAY:
228 break;
230 default:
231 prepend(name, "unknown type %d", sym->type);
232 return;
235 mod = modifier_string(sym->ctype.modifiers);
236 modlen = strlen(mod);
237 name->start -= modlen;
238 memcpy(name->start, mod, modlen);
240 do_show_type(sym->ctype.base_type, name);
242 /* Postpend */
243 if (sym->ctype.as)
244 append(name, "<asn:%d>", sym->ctype.as);
246 switch (sym->type) {
247 case SYM_PTR:
248 return;
250 case SYM_FN:
251 append(name, " )( ... )");
252 return;
254 case SYM_ARRAY:
255 append(name, "[%lld]", get_expression_value(sym->array_size));
256 return;
257 default:
258 break;
262 void show_type(struct symbol *sym)
264 char array[200];
265 struct type_name name;
267 name.start = name.end = array+100;
268 do_show_type(sym, &name);
269 *name.end = 0;
270 printf("%s", name.start);
273 const char *show_typename(struct symbol *sym)
275 static char array[200];
276 struct type_name name;
278 name.start = name.end = array+100;
279 do_show_type(sym, &name);
280 *name.end = 0;
281 return name.start;
284 void show_symbol(struct symbol *sym)
286 struct symbol *type;
288 if (!sym)
289 return;
291 if (sym->ctype.alignment)
292 printf(".align %ld\n", sym->ctype.alignment);
294 show_type(sym);
295 type = sym->ctype.base_type;
296 if (!type)
297 return;
300 * Show actual implementation information
302 switch (type->type) {
303 case SYM_STRUCT:
304 symbol_iterate(type->symbol_list, show_struct_member, NULL);
305 break;
307 case SYM_UNION:
308 symbol_iterate(type->symbol_list, show_struct_member, NULL);
309 break;
311 case SYM_FN: {
312 struct statement *stmt = type->stmt;
313 if (stmt) {
314 int val;
315 printf("\n");
316 val = show_statement(stmt);
317 if (val)
318 printf("\tmov.%d\t\tretval,%d\n", stmt->ret->bit_size, val);
319 printf("\tret\n");
321 break;
324 default:
325 break;
328 if (sym->initializer) {
329 printf(" = \n");
330 show_expression(sym->initializer);
334 static int show_symbol_init(struct symbol *sym);
336 static int new_pseudo(void)
338 static int nr = 0;
339 return ++nr;
342 static int new_label(void)
344 static int label = 0;
345 return ++label;
348 static void show_switch_statement(struct statement *stmt)
350 int val = show_expression(stmt->switch_expression);
351 struct symbol *sym;
352 printf("\tswitch v%d\n", val);
355 * Debugging only: Check that the case list is correct
356 * by printing it out.
358 * This is where a _real_ back-end would go through the
359 * cases to decide whether to use a lookup table or a
360 * series of comparisons etc
362 printf("# case table:\n");
363 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
364 struct statement *case_stmt = sym->stmt;
365 struct expression *expr = case_stmt->case_expression;
366 struct expression *to = case_stmt->case_to;
368 if (!expr) {
369 printf(" default");
370 } else {
371 if (expr->type == EXPR_VALUE) {
372 printf(" case %lld", expr->value);
373 if (to) {
374 if (to->type == EXPR_VALUE) {
375 printf(" .. %lld", to->value);
376 } else {
377 printf(" .. what?");
380 } else
381 printf(" what?");
383 printf(": .L%p\n", sym->bb_target);
384 } END_FOR_EACH_PTR;
385 printf("# end case table\n");
387 show_statement(stmt->switch_statement);
389 if (stmt->switch_break->used)
390 printf(".L%p:\n", stmt->switch_break->bb_target);
393 static void show_symbol_decl(struct symbol_list *syms)
395 struct symbol *sym;
396 FOR_EACH_PTR(syms, sym) {
397 show_symbol_init(sym);
398 } END_FOR_EACH_PTR;
401 static int show_return_stmt(struct statement *stmt);
404 * Print out a statement
406 int show_statement(struct statement *stmt)
408 if (!stmt)
409 return 0;
410 switch (stmt->type) {
411 case STMT_RETURN:
412 return show_return_stmt(stmt);
413 case STMT_COMPOUND: {
414 struct statement *s;
415 int last = 0;
417 show_symbol_decl(stmt->syms);
418 FOR_EACH_PTR(stmt->stmts, s) {
419 last = show_statement(s);
420 } END_FOR_EACH_PTR;
421 if (stmt->ret) {
422 int addr, bits;
423 printf(".L%p:\n", stmt->ret);
424 addr = show_symbol_expr(stmt->ret);
425 bits = stmt->ret->bit_size;
426 last = new_pseudo();
427 printf("\tld.%d\t\tv%d,[v%d]\n", bits, last, addr);
429 return last;
432 case STMT_EXPRESSION:
433 return show_expression(stmt->expression);
434 case STMT_IF: {
435 int val, target;
436 struct expression *cond = stmt->if_conditional;
438 /* This is only valid if nobody can jump into the "dead" statement */
439 #if 0
440 if (cond->type == EXPR_VALUE) {
441 struct statement *s = stmt->if_true;
442 if (!cond->value)
443 s = stmt->if_false;
444 show_statement(s);
445 break;
447 #endif
448 val = show_expression(cond);
449 target = new_label();
450 printf("\tje\t\tv%d,.L%d\n", val, target);
451 show_statement(stmt->if_true);
452 if (stmt->if_false) {
453 int last = new_label();
454 printf("\tjmp\t\t.L%d\n", last);
455 printf(".L%d:\n", target);
456 target = last;
457 show_statement(stmt->if_false);
459 printf(".L%d:\n", target);
460 break;
462 case STMT_SWITCH:
463 show_switch_statement(stmt);
464 break;
466 case STMT_CASE:
467 printf(".L%p:\n", stmt->case_label);
468 show_statement(stmt->case_statement);
469 break;
471 case STMT_ITERATOR: {
472 struct statement *pre_statement = stmt->iterator_pre_statement;
473 struct expression *pre_condition = stmt->iterator_pre_condition;
474 struct statement *statement = stmt->iterator_statement;
475 struct statement *post_statement = stmt->iterator_post_statement;
476 struct expression *post_condition = stmt->iterator_post_condition;
477 int val, loop_top = 0, loop_bottom = 0;
479 show_symbol_decl(stmt->iterator_syms);
480 show_statement(pre_statement);
481 if (pre_condition) {
482 if (pre_condition->type == EXPR_VALUE) {
483 if (!pre_condition->value) {
484 loop_bottom = new_label();
485 printf("\tjmp\t\t.L%d\n", loop_bottom);
487 } else {
488 loop_bottom = new_label();
489 val = show_expression(pre_condition);
490 printf("\tje\t\tv%d, .L%d\n", val, loop_bottom);
493 if (!post_condition || post_condition->type != EXPR_VALUE || post_condition->value) {
494 loop_top = new_label();
495 printf(".L%d:\n", loop_top);
497 show_statement(statement);
498 if (stmt->iterator_continue->used)
499 printf(".L%p:\n", stmt->iterator_continue);
500 show_statement(post_statement);
501 if (!post_condition) {
502 printf("\tjmp\t\t.L%d\n", loop_top);
503 } else if (post_condition->type == EXPR_VALUE) {
504 if (post_condition->value)
505 printf("\tjmp\t\t.L%d\n", loop_top);
506 } else {
507 val = show_expression(post_condition);
508 printf("\tjne\t\tv%d, .L%d\n", val, loop_top);
510 if (stmt->iterator_break->used)
511 printf(".L%p:\n", stmt->iterator_break);
512 if (loop_bottom)
513 printf(".L%d:\n", loop_bottom);
514 break;
516 case STMT_NONE:
517 break;
519 case STMT_LABEL:
520 printf(".L%p:\n", stmt->label_identifier);
521 show_statement(stmt->label_statement);
522 break;
524 case STMT_GOTO:
525 if (stmt->goto_expression) {
526 int val = show_expression(stmt->goto_expression);
527 printf("\tgoto\t\t*v%d\n", val);
528 } else {
529 printf("\tgoto\t\t.L%p\n", stmt->goto_label->bb_target);
531 break;
532 case STMT_ASM:
533 printf("\tasm( .... )\n");
534 break;
537 return 0;
540 static void show_one_statement(struct statement *stmt, void *sep, int flags)
542 show_statement(stmt);
543 if (!(flags & ITERATE_LAST))
544 printf("%s", (const char *)sep);
547 void show_statement_list(struct statement_list *stmt, const char *sep)
549 statement_iterate(stmt, show_one_statement, (void *)sep);
552 static void show_one_expression(struct expression *expr, void *sep, int flags)
554 show_expression(expr);
555 if (!(flags & ITERATE_LAST))
556 printf("%s", (const char *)sep);
559 void show_expression_list(struct expression_list *list, const char *sep)
561 expression_iterate(list, show_one_expression, (void *)sep);
564 static int show_call_expression(struct expression *expr)
566 struct symbol *direct;
567 struct expression *arg, *fn;
568 int fncall, retval;
569 int framesize;
571 if (!expr->ctype) {
572 warn(expr->pos, "\tcall with no type!");
573 return 0;
576 framesize = 0;
577 FOR_EACH_PTR_REVERSE(expr->args, arg) {
578 int new = show_expression(arg);
579 int size = arg->ctype->bit_size;
580 printf("\tpush.%d\t\tv%d\n", size, new);
581 framesize += size >> 3;
582 } END_FOR_EACH_PTR_REVERSE;
584 fn = expr->fn;
586 /* Remove dereference, if any */
587 direct = NULL;
588 if (fn->type == EXPR_PREOP) {
589 if (fn->unop->type == EXPR_SYMBOL) {
590 struct symbol *sym = fn->unop->symbol;
591 if (sym->ctype.base_type->type == SYM_FN)
592 direct = sym;
595 if (direct) {
596 printf("\tcall\t\t%s\n", show_ident(direct->ident));
597 } else {
598 fncall = show_expression(fn);
599 printf("\tcall\t\t*v%d\n", fncall);
601 if (framesize)
602 printf("\tadd.%d\t\tvSP,vSP,$%d\n", BITS_IN_POINTER, framesize);
604 retval = new_pseudo();
605 printf("\tmov.%d\t\tv%d,retval\n", expr->ctype->bit_size, retval);
606 return retval;
609 static int show_binop(struct expression *expr)
611 int left = show_expression(expr->left);
612 int right = show_expression(expr->right);
613 int new = new_pseudo();
614 const char *opname;
615 static const char *name[] = {
616 ['+'] = "add", ['-'] = "sub",
617 ['*'] = "mul", ['/'] = "div",
618 ['%'] = "mod", ['&'] = "and",
619 ['|'] = "lor", ['^'] = "xor"
621 unsigned int op = expr->op;
623 opname = show_special(op);
624 if (op < sizeof(name)/sizeof(*name))
625 opname = name[op];
626 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname,
627 expr->ctype->bit_size,
628 new, left, right);
629 return new;
632 static int show_regular_preop(struct expression *expr)
634 int target = show_expression(expr->unop);
635 int new = new_pseudo();
636 static const char *name[] = {
637 ['!'] = "nonzero", ['-'] = "neg",
638 ['~'] = "not",
640 unsigned int op = expr->op;
641 const char *opname;
643 opname = show_special(op);
644 if (op < sizeof(name)/sizeof(*name))
645 opname = name[op];
646 printf("\t%s.%d\t\tv%d,v%d\n", opname, expr->ctype->bit_size, new, target);
647 return new;
651 * FIXME! Not all accesses are memory loads. We should
652 * check what kind of symbol is behind the dereference.
654 static int show_address_gen(struct expression *expr)
656 if (expr->type == EXPR_PREOP)
657 return show_expression(expr->unop);
658 return show_expression(expr->address);
661 static int show_load_gen(int bits, struct expression *expr, int addr)
663 int new = new_pseudo();
665 printf("\tld.%d\t\tv%d,[v%d]\n", bits, new, addr);
666 if (expr->type == EXPR_PREOP)
667 return new;
669 /* bitfield load! */
670 if (expr->bitpos)
671 printf("\tshr.%d\t\tv%d,v%d,$%d\n", bits, new, new, expr->bitpos);
672 printf("\tandi.%d\t\tv%d,v%d,$%llu\n", bits, new, new, (1ULL << expr->nrbits)-1);
673 return new;
676 static void show_store_gen(int bits, int value, struct expression *expr, int addr)
678 /* FIXME!!! Bitfield store! */
679 printf("\tst.%d\t\tv%d,[v%d]\n", bits, value, addr);
682 static int show_assignment(struct expression *expr)
684 struct expression *target = expr->left;
685 int val, addr, bits;
687 if (!expr->ctype)
688 return 0;
690 bits = expr->ctype->bit_size;
691 val = show_expression(expr->right);
692 addr = show_address_gen(target);
693 show_store_gen(bits, val, target, addr);
694 return val;
697 static int show_return_stmt(struct statement *stmt)
699 struct expression *expr = stmt->ret_value;
700 struct symbol *target = stmt->ret_target;
702 if (expr && expr->ctype) {
703 int val = show_expression(expr);
704 int bits = expr->ctype->bit_size;
705 int addr = show_symbol_expr(target);
706 show_store_gen(bits, val, NULL, addr);
708 printf("\tret\t\t(%p)\n", target);
709 return 0;
712 static int show_initialization(struct symbol *sym, struct expression *expr)
714 int val, addr, bits;
716 if (!expr->ctype)
717 return 0;
719 bits = expr->ctype->bit_size;
720 val = show_expression(expr);
721 addr = show_symbol_expr(sym);
722 // FIXME! The "target" expression is for bitfield store information.
723 // Leave it NULL, which works fine.
724 show_store_gen(bits, val, NULL, addr);
725 return 0;
728 static int show_access(struct expression *expr)
730 int addr = show_address_gen(expr);
731 return show_load_gen(expr->ctype->bit_size, expr, addr);
734 static int show_inc_dec(struct expression *expr, int postop)
736 int addr = show_address_gen(expr->unop);
737 int retval, new;
738 const char *opname = expr->op == SPECIAL_INCREMENT ? "add" : "sub";
739 int bits = expr->ctype->bit_size;
741 retval = show_load_gen(bits, expr->unop, addr);
742 new = retval;
743 if (postop)
744 new = new_pseudo();
745 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname, bits, new, retval);
746 show_store_gen(bits, new, expr->unop, addr);
747 return retval;
750 static int show_preop(struct expression *expr)
753 * '*' is an lvalue access, and is fundamentally different
754 * from an arithmetic operation. Maybe it should have an
755 * expression type of its own..
757 if (expr->op == '*')
758 return show_access(expr);
759 if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
760 return show_inc_dec(expr, 0);
761 return show_regular_preop(expr);
764 static int show_postop(struct expression *expr)
766 return show_inc_dec(expr, 1);
769 static int show_symbol_expr(struct symbol *sym)
771 int new = new_pseudo();
773 if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC)) {
774 printf("\tmovi.%d\t\tv%d,$%s\n", BITS_IN_POINTER, new, show_ident(sym->ident));
775 return new;
777 if (sym->ctype.modifiers & MOD_ADDRESSABLE) {
778 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", BITS_IN_POINTER, new, sym->value);
779 return new;
781 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", BITS_IN_POINTER, new, show_ident(sym->ident), sym);
782 return new;
785 static int show_symbol_init(struct symbol *sym)
787 struct expression *expr = sym->initializer;
789 if (expr) {
790 int val, addr, bits;
792 bits = expr->ctype->bit_size;
793 val = show_expression(expr);
794 addr = show_symbol_expr(sym);
795 show_store_gen(bits, val, NULL, addr);
797 return 0;
800 static int type_is_signed(struct symbol *sym)
802 if (sym->type == SYM_NODE)
803 sym = sym->ctype.base_type;
804 if (sym->type == SYM_PTR)
805 return 0;
806 return !(sym->ctype.modifiers & MOD_UNSIGNED);
809 static int show_cast_expr(struct expression *expr)
811 struct symbol *old_type, *new_type;
812 int op = show_expression(expr->cast_expression);
813 int oldbits, newbits;
814 int new, is_signed;
816 old_type = expr->cast_expression->ctype;
817 new_type = expr->cast_type;
819 oldbits = old_type->bit_size;
820 newbits = new_type->bit_size;
821 if (oldbits >= newbits)
822 return op;
823 new = new_pseudo();
824 is_signed = type_is_signed(old_type);
825 if (is_signed) {
826 printf("\tsext%d.%d\tv%d,v%d\n", oldbits, newbits, new, op);
827 } else {
828 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits, new, op, (1UL << oldbits)-1);
830 return new;
833 static int show_value(struct expression *expr)
835 int new = new_pseudo();
836 unsigned long long value = expr->value;
838 printf("\tmovi.%d\t\tv%d,$%llu\n", expr->ctype->bit_size, new, value);
839 return new;
842 static int show_string_expr(struct expression *expr)
844 int new = new_pseudo();
846 printf("\tmovi.%d\t\tv%d,&%s\n", BITS_IN_POINTER, new, show_string(expr->string));
847 return new;
850 static int show_bitfield_expr(struct expression *expr)
852 return show_access(expr);
855 int show_label_expr(struct expression *expr)
857 int new = new_pseudo();
858 printf("\tmovi.%d\t\tv%d,.L%p\n",BITS_IN_POINTER, new, expr->label_symbol);
859 return new;
862 static int show_conditional_expr(struct expression *expr)
864 int cond = show_expression(expr->conditional);
865 int true = show_expression(expr->cond_true);
866 int false = show_expression(expr->cond_false);
867 int new = new_pseudo();
869 if (!true)
870 true = cond;
871 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond, expr->ctype->bit_size, new, true, false);
872 return new;
875 static int show_statement_expr(struct expression *expr)
877 return show_statement(expr->statement);
880 static int show_position_expr(struct expression *expr, struct symbol *base)
882 int new = show_expression(expr->init_expr);
883 struct symbol *ctype = expr->init_sym;
885 printf("\tinsert v%d at [%d:%d] of %s\n", new,
886 expr->init_offset, ctype->bit_offset,
887 show_ident(base->ident));
888 return 0;
891 static int show_initializer_expr(struct expression *expr, struct symbol *ctype)
893 struct expression *entry;
895 FOR_EACH_PTR(expr->expr_list, entry) {
896 // Nested initializers have their positions already
897 // recursively calculated - just output them too
898 if (entry->type == EXPR_INITIALIZER) {
899 show_initializer_expr(entry, ctype);
900 continue;
903 // Ignore initializer indexes and identifiers - the
904 // evaluator has taken them into account
905 if (entry->type == EXPR_IDENTIFIER || entry->type == EXPR_INDEX)
906 continue;
907 if (entry->type == EXPR_POS) {
908 show_position_expr(entry, ctype);
909 continue;
911 show_initialization(ctype, entry);
912 } END_FOR_EACH_PTR;
913 return 0;
916 int show_symbol_expr_init(struct symbol *sym)
918 struct expression *expr = sym->initializer;
920 if (expr)
921 show_expression(expr);
922 return show_symbol_expr(sym);
926 * Print out an expression. Return the pseudo that contains the
927 * variable.
929 int show_expression(struct expression *expr)
931 if (!expr)
932 return 0;
934 if (!expr->ctype) {
935 struct position *pos = &expr->pos;
936 printf("\tno type at %s:%d:%d\n",
937 input_streams[pos->stream].name,
938 pos->line, pos->pos);
939 return 0;
942 switch (expr->type) {
943 case EXPR_CALL:
944 return show_call_expression(expr);
946 case EXPR_ASSIGNMENT:
947 return show_assignment(expr);
949 case EXPR_BINOP:
950 case EXPR_COMMA:
951 case EXPR_COMPARE:
952 case EXPR_LOGICAL:
953 return show_binop(expr);
954 case EXPR_PREOP:
955 return show_preop(expr);
956 case EXPR_POSTOP:
957 return show_postop(expr);
958 case EXPR_SYMBOL:
959 return show_symbol_expr(expr->symbol);
960 case EXPR_DEREF:
961 case EXPR_SIZEOF:
962 warn(expr->pos, "invalid expression after evaluation");
963 return 0;
964 case EXPR_CAST:
965 return show_cast_expr(expr);
966 case EXPR_VALUE:
967 return show_value(expr);
968 case EXPR_STRING:
969 return show_string_expr(expr);
970 case EXPR_BITFIELD:
971 return show_bitfield_expr(expr);
972 case EXPR_INITIALIZER:
973 return show_initializer_expr(expr, expr->ctype);
974 case EXPR_CONDITIONAL:
975 return show_conditional_expr(expr);
976 case EXPR_STATEMENT:
977 return show_statement_expr(expr);
978 case EXPR_LABEL:
979 return show_label_expr(expr);
981 // None of these should exist as direct expressions: they are only
982 // valid as sub-expressions of initializers.
983 case EXPR_POS:
984 warn(expr->pos, "unable to show plain initializer position expression");
985 return 0;
986 case EXPR_IDENTIFIER:
987 warn(expr->pos, "unable to show identifier expression");
988 return 0;
989 case EXPR_INDEX:
990 warn(expr->pos, "unable to show index expression");
991 return 0;
992 case EXPR_TYPE:
993 warn(expr->pos, "unable to show type expression");
994 return 0;
996 return 0;