Warn about casts that remove address space information.
[smatch.git] / show-parse.c
blob190e76657182aa02cf4501c47712d816194893fb
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]", "[accessed]", "[toplevel]",
80 "[label]", "[assigned]", "[type]", "[safe]",
81 "[usertype]", "[force]",
82 NULL
84 ptr = names;
85 while ((res = *ptr++) != NULL) {
86 if (mod & 1) {
87 char c;
88 while ((c = *res++) != '\0')
89 *p++ = c;
90 *p++ = ' ';
92 mod >>= 1;
94 *p = 0;
95 return buffer;
98 void show_struct_member(struct symbol *sym, void *data, int flags)
100 if (flags & ITERATE_FIRST)
101 printf(" {\n\t");
102 printf("%s:%d:%ld at offset %ld", show_ident(sym->ident), sym->bit_size, sym->ctype.alignment, sym->offset);
103 if (sym->fieldwidth)
104 printf("[%d..%d]", sym->bit_offset, sym->bit_offset+sym->fieldwidth-1);
105 if (flags & ITERATE_LAST)
106 printf("\n} ");
107 else
108 printf(", ");
111 static void show_one_symbol(struct symbol *sym, void *sep, int flags)
113 show_symbol(sym);
114 if (!(flags & ITERATE_LAST))
115 printf("%s", (const char *)sep);
118 void show_symbol_list(struct symbol_list *list, const char *sep)
120 symbol_iterate(list, show_one_symbol, (void *)sep);
123 struct type_name {
124 char *start;
125 char *end;
128 static void prepend(struct type_name *name, const char *fmt, ...)
130 static char buffer[512];
131 int n;
133 va_list args;
134 va_start(args, fmt);
135 n = vsprintf(buffer, fmt, args);
136 va_end(args);
138 name->start -= n;
139 memcpy(name->start, buffer, n);
142 static void append(struct type_name *name, const char *fmt, ...)
144 static char buffer[512];
145 int n;
147 va_list args;
148 va_start(args, fmt);
149 n = vsprintf(buffer, fmt, args);
150 va_end(args);
152 memcpy(name->end, buffer, n);
153 name->end += n;
156 static void do_show_type(struct symbol *sym, struct type_name *name)
158 int i, modlen;
159 const char *mod;
160 static struct ctype_name {
161 struct symbol *sym;
162 char *name;
163 } typenames[] = {
164 { & char_ctype, "char" },
165 { &uchar_ctype, "unsigned char" },
166 { & short_ctype, "short" },
167 { &ushort_ctype, "unsigned short" },
168 { & int_ctype, "int" },
169 { &uint_ctype, "unsigned int" },
170 { & long_ctype, "long" },
171 { &ulong_ctype, "unsigned long" },
172 { & llong_ctype, "long long" },
173 { &ullong_ctype, "unsigned long long" },
175 { &void_ctype, "void" },
176 { &bool_ctype, "bool" },
177 { &string_ctype, "string" },
179 { &float_ctype, "float" },
180 { &double_ctype, "double" },
181 { &ldouble_ctype,"long double" },
184 if (!sym)
185 return;
187 for (i = 0; i < sizeof(typenames)/sizeof(typenames[0]); i++) {
188 if (typenames[i].sym == sym) {
189 int len = strlen(typenames[i].name);
190 *--name->start = ' ';
191 name->start -= len;
192 memcpy(name->start, typenames[i].name, len);
193 return;
197 /* Prepend */
198 switch (sym->type) {
199 case SYM_PTR:
200 prepend(name, "*");
201 break;
202 case SYM_FN:
203 prepend(name, "( ");
204 break;
205 case SYM_STRUCT:
206 prepend(name, "struct %s ", show_ident(sym->ident));
207 return;
209 case SYM_UNION:
210 prepend(name, "union %s ", show_ident(sym->ident));
211 return;
213 case SYM_ENUM:
214 prepend(name, "enum %s ", show_ident(sym->ident));
215 return;
217 case SYM_NODE:
218 append(name, "%s", show_ident(sym->ident));
219 break;
221 case SYM_BITFIELD:
222 append(name, ":%d", sym->fieldwidth);
223 break;
225 case SYM_LABEL:
226 append(name, "label(%s:%p)", show_ident(sym->ident), sym);
227 break;
229 case SYM_ARRAY:
230 break;
232 default:
233 prepend(name, "unknown type %d", sym->type);
234 return;
237 mod = modifier_string(sym->ctype.modifiers);
238 modlen = strlen(mod);
239 name->start -= modlen;
240 memcpy(name->start, mod, modlen);
242 do_show_type(sym->ctype.base_type, name);
244 /* Postpend */
245 if (sym->ctype.as)
246 append(name, "<asn:%d>", sym->ctype.as);
248 switch (sym->type) {
249 case SYM_PTR:
250 return;
252 case SYM_FN:
253 append(name, " )( ... )");
254 return;
256 case SYM_ARRAY:
257 append(name, "[%lld]", get_expression_value(sym->array_size));
258 return;
259 default:
260 break;
264 void show_type(struct symbol *sym)
266 char array[200];
267 struct type_name name;
269 name.start = name.end = array+100;
270 do_show_type(sym, &name);
271 *name.end = 0;
272 printf("%s", name.start);
275 const char *show_typename(struct symbol *sym)
277 static char array[200];
278 struct type_name name;
280 name.start = name.end = array+100;
281 do_show_type(sym, &name);
282 *name.end = 0;
283 return name.start;
286 void show_symbol(struct symbol *sym)
288 struct symbol *type;
290 if (!sym)
291 return;
293 if (sym->ctype.alignment)
294 printf(".align %ld\n", sym->ctype.alignment);
296 show_type(sym);
297 type = sym->ctype.base_type;
298 if (!type)
299 return;
302 * Show actual implementation information
304 switch (type->type) {
305 case SYM_STRUCT:
306 symbol_iterate(type->symbol_list, show_struct_member, NULL);
307 break;
309 case SYM_UNION:
310 symbol_iterate(type->symbol_list, show_struct_member, NULL);
311 break;
313 case SYM_FN: {
314 struct statement *stmt = type->stmt;
315 if (stmt) {
316 int val;
317 printf("\n");
318 val = show_statement(stmt);
319 if (val)
320 printf("\tmov.%d\t\tretval,%d\n", stmt->ret->bit_size, val);
321 printf("\tret\n");
323 break;
326 default:
327 break;
330 if (sym->initializer) {
331 printf(" = \n");
332 show_expression(sym->initializer);
336 static int show_symbol_init(struct symbol *sym);
338 static int new_pseudo(void)
340 static int nr = 0;
341 return ++nr;
344 static int new_label(void)
346 static int label = 0;
347 return ++label;
350 static void show_switch_statement(struct statement *stmt)
352 int val = show_expression(stmt->switch_expression);
353 struct symbol *sym;
354 printf("\tswitch v%d\n", val);
357 * Debugging only: Check that the case list is correct
358 * by printing it out.
360 * This is where a _real_ back-end would go through the
361 * cases to decide whether to use a lookup table or a
362 * series of comparisons etc
364 printf("# case table:\n");
365 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
366 struct statement *case_stmt = sym->stmt;
367 struct expression *expr = case_stmt->case_expression;
368 struct expression *to = case_stmt->case_to;
370 if (!expr) {
371 printf(" default");
372 } else {
373 if (expr->type == EXPR_VALUE) {
374 printf(" case %lld", expr->value);
375 if (to) {
376 if (to->type == EXPR_VALUE) {
377 printf(" .. %lld", to->value);
378 } else {
379 printf(" .. what?");
382 } else
383 printf(" what?");
385 printf(": .L%p\n", sym->bb_target);
386 } END_FOR_EACH_PTR;
387 printf("# end case table\n");
389 show_statement(stmt->switch_statement);
391 if (stmt->switch_break->used)
392 printf(".L%p:\n", stmt->switch_break->bb_target);
395 static void show_symbol_decl(struct symbol_list *syms)
397 struct symbol *sym;
398 FOR_EACH_PTR(syms, sym) {
399 show_symbol_init(sym);
400 } END_FOR_EACH_PTR;
403 static int show_return_stmt(struct statement *stmt);
406 * Print out a statement
408 int show_statement(struct statement *stmt)
410 if (!stmt)
411 return 0;
412 switch (stmt->type) {
413 case STMT_RETURN:
414 return show_return_stmt(stmt);
415 case STMT_COMPOUND: {
416 struct statement *s;
417 int last = 0;
419 show_symbol_decl(stmt->syms);
420 FOR_EACH_PTR(stmt->stmts, s) {
421 last = show_statement(s);
422 } END_FOR_EACH_PTR;
423 if (stmt->ret) {
424 int addr, bits;
425 printf(".L%p:\n", stmt->ret);
426 addr = show_symbol_expr(stmt->ret);
427 bits = stmt->ret->bit_size;
428 last = new_pseudo();
429 printf("\tld.%d\t\tv%d,[v%d]\n", bits, last, addr);
431 return last;
434 case STMT_EXPRESSION:
435 return show_expression(stmt->expression);
436 case STMT_IF: {
437 int val, target;
438 struct expression *cond = stmt->if_conditional;
440 /* This is only valid if nobody can jump into the "dead" statement */
441 #if 0
442 if (cond->type == EXPR_VALUE) {
443 struct statement *s = stmt->if_true;
444 if (!cond->value)
445 s = stmt->if_false;
446 show_statement(s);
447 break;
449 #endif
450 val = show_expression(cond);
451 target = new_label();
452 printf("\tje\t\tv%d,.L%d\n", val, target);
453 show_statement(stmt->if_true);
454 if (stmt->if_false) {
455 int last = new_label();
456 printf("\tjmp\t\t.L%d\n", last);
457 printf(".L%d:\n", target);
458 target = last;
459 show_statement(stmt->if_false);
461 printf(".L%d:\n", target);
462 break;
464 case STMT_SWITCH:
465 show_switch_statement(stmt);
466 break;
468 case STMT_CASE:
469 printf(".L%p:\n", stmt->case_label);
470 show_statement(stmt->case_statement);
471 break;
473 case STMT_ITERATOR: {
474 struct statement *pre_statement = stmt->iterator_pre_statement;
475 struct expression *pre_condition = stmt->iterator_pre_condition;
476 struct statement *statement = stmt->iterator_statement;
477 struct statement *post_statement = stmt->iterator_post_statement;
478 struct expression *post_condition = stmt->iterator_post_condition;
479 int val, loop_top = 0, loop_bottom = 0;
481 show_symbol_decl(stmt->iterator_syms);
482 show_statement(pre_statement);
483 if (pre_condition) {
484 if (pre_condition->type == EXPR_VALUE) {
485 if (!pre_condition->value) {
486 loop_bottom = new_label();
487 printf("\tjmp\t\t.L%d\n", loop_bottom);
489 } else {
490 loop_bottom = new_label();
491 val = show_expression(pre_condition);
492 printf("\tje\t\tv%d, .L%d\n", val, loop_bottom);
495 if (!post_condition || post_condition->type != EXPR_VALUE || post_condition->value) {
496 loop_top = new_label();
497 printf(".L%d:\n", loop_top);
499 show_statement(statement);
500 if (stmt->iterator_continue->used)
501 printf(".L%p:\n", stmt->iterator_continue);
502 show_statement(post_statement);
503 if (!post_condition) {
504 printf("\tjmp\t\t.L%d\n", loop_top);
505 } else if (post_condition->type == EXPR_VALUE) {
506 if (post_condition->value)
507 printf("\tjmp\t\t.L%d\n", loop_top);
508 } else {
509 val = show_expression(post_condition);
510 printf("\tjne\t\tv%d, .L%d\n", val, loop_top);
512 if (stmt->iterator_break->used)
513 printf(".L%p:\n", stmt->iterator_break);
514 if (loop_bottom)
515 printf(".L%d:\n", loop_bottom);
516 break;
518 case STMT_NONE:
519 break;
521 case STMT_LABEL:
522 printf(".L%p:\n", stmt->label_identifier);
523 show_statement(stmt->label_statement);
524 break;
526 case STMT_GOTO:
527 if (stmt->goto_expression) {
528 int val = show_expression(stmt->goto_expression);
529 printf("\tgoto\t\t*v%d\n", val);
530 } else {
531 printf("\tgoto\t\t.L%p\n", stmt->goto_label->bb_target);
533 break;
534 case STMT_ASM:
535 printf("\tasm( .... )\n");
536 break;
539 return 0;
542 static void show_one_statement(struct statement *stmt, void *sep, int flags)
544 show_statement(stmt);
545 if (!(flags & ITERATE_LAST))
546 printf("%s", (const char *)sep);
549 void show_statement_list(struct statement_list *stmt, const char *sep)
551 statement_iterate(stmt, show_one_statement, (void *)sep);
554 static void show_one_expression(struct expression *expr, void *sep, int flags)
556 show_expression(expr);
557 if (!(flags & ITERATE_LAST))
558 printf("%s", (const char *)sep);
561 void show_expression_list(struct expression_list *list, const char *sep)
563 expression_iterate(list, show_one_expression, (void *)sep);
566 static int show_call_expression(struct expression *expr)
568 struct symbol *direct;
569 struct expression *arg, *fn;
570 int fncall, retval;
571 int framesize;
573 if (!expr->ctype) {
574 warn(expr->pos, "\tcall with no type!");
575 return 0;
578 framesize = 0;
579 FOR_EACH_PTR_REVERSE(expr->args, arg) {
580 int new = show_expression(arg);
581 int size = arg->ctype->bit_size;
582 printf("\tpush.%d\t\tv%d\n", size, new);
583 framesize += size >> 3;
584 } END_FOR_EACH_PTR_REVERSE;
586 fn = expr->fn;
588 /* Remove dereference, if any */
589 direct = NULL;
590 if (fn->type == EXPR_PREOP) {
591 if (fn->unop->type == EXPR_SYMBOL) {
592 struct symbol *sym = fn->unop->symbol;
593 if (sym->ctype.base_type->type == SYM_FN)
594 direct = sym;
597 if (direct) {
598 printf("\tcall\t\t%s\n", show_ident(direct->ident));
599 } else {
600 fncall = show_expression(fn);
601 printf("\tcall\t\t*v%d\n", fncall);
603 if (framesize)
604 printf("\tadd.%d\t\tvSP,vSP,$%d\n", bits_in_pointer, framesize);
606 retval = new_pseudo();
607 printf("\tmov.%d\t\tv%d,retval\n", expr->ctype->bit_size, retval);
608 return retval;
611 static int show_binop(struct expression *expr)
613 int left = show_expression(expr->left);
614 int right = show_expression(expr->right);
615 int new = new_pseudo();
616 const char *opname;
617 static const char *name[] = {
618 ['+'] = "add", ['-'] = "sub",
619 ['*'] = "mul", ['/'] = "div",
620 ['%'] = "mod", ['&'] = "and",
621 ['|'] = "lor", ['^'] = "xor"
623 unsigned int op = expr->op;
625 opname = show_special(op);
626 if (op < sizeof(name)/sizeof(*name))
627 opname = name[op];
628 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname,
629 expr->ctype->bit_size,
630 new, left, right);
631 return new;
634 static int show_regular_preop(struct expression *expr)
636 int target = show_expression(expr->unop);
637 int new = new_pseudo();
638 static const char *name[] = {
639 ['!'] = "nonzero", ['-'] = "neg",
640 ['~'] = "not",
642 unsigned int op = expr->op;
643 const char *opname;
645 opname = show_special(op);
646 if (op < sizeof(name)/sizeof(*name))
647 opname = name[op];
648 printf("\t%s.%d\t\tv%d,v%d\n", opname, expr->ctype->bit_size, new, target);
649 return new;
653 * FIXME! Not all accesses are memory loads. We should
654 * check what kind of symbol is behind the dereference.
656 static int show_address_gen(struct expression *expr)
658 if (expr->type == EXPR_PREOP)
659 return show_expression(expr->unop);
660 return show_expression(expr->address);
663 static int show_load_gen(int bits, struct expression *expr, int addr)
665 int new = new_pseudo();
667 printf("\tld.%d\t\tv%d,[v%d]\n", bits, new, addr);
668 if (expr->type == EXPR_PREOP)
669 return new;
671 /* bitfield load! */
672 if (expr->bitpos)
673 printf("\tshr.%d\t\tv%d,v%d,$%d\n", bits, new, new, expr->bitpos);
674 printf("\tandi.%d\t\tv%d,v%d,$%llu\n", bits, new, new, (1ULL << expr->nrbits)-1);
675 return new;
678 static void show_store_gen(int bits, int value, struct expression *expr, int addr)
680 /* FIXME!!! Bitfield store! */
681 printf("\tst.%d\t\tv%d,[v%d]\n", bits, value, addr);
684 static int show_assignment(struct expression *expr)
686 struct expression *target = expr->left;
687 int val, addr, bits;
689 if (!expr->ctype)
690 return 0;
692 bits = expr->ctype->bit_size;
693 val = show_expression(expr->right);
694 addr = show_address_gen(target);
695 show_store_gen(bits, val, target, addr);
696 return val;
699 static int show_return_stmt(struct statement *stmt)
701 struct expression *expr = stmt->ret_value;
702 struct symbol *target = stmt->ret_target;
704 if (expr && expr->ctype) {
705 int val = show_expression(expr);
706 int bits = expr->ctype->bit_size;
707 int addr = show_symbol_expr(target);
708 show_store_gen(bits, val, NULL, addr);
710 printf("\tret\t\t(%p)\n", target);
711 return 0;
714 static int show_initialization(struct symbol *sym, struct expression *expr)
716 int val, addr, bits;
718 if (!expr->ctype)
719 return 0;
721 bits = expr->ctype->bit_size;
722 val = show_expression(expr);
723 addr = show_symbol_expr(sym);
724 // FIXME! The "target" expression is for bitfield store information.
725 // Leave it NULL, which works fine.
726 show_store_gen(bits, val, NULL, addr);
727 return 0;
730 static int show_access(struct expression *expr)
732 int addr = show_address_gen(expr);
733 return show_load_gen(expr->ctype->bit_size, expr, addr);
736 static int show_inc_dec(struct expression *expr, int postop)
738 int addr = show_address_gen(expr->unop);
739 int retval, new;
740 const char *opname = expr->op == SPECIAL_INCREMENT ? "add" : "sub";
741 int bits = expr->ctype->bit_size;
743 retval = show_load_gen(bits, expr->unop, addr);
744 new = retval;
745 if (postop)
746 new = new_pseudo();
747 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname, bits, new, retval);
748 show_store_gen(bits, new, expr->unop, addr);
749 return retval;
752 static int show_preop(struct expression *expr)
755 * '*' is an lvalue access, and is fundamentally different
756 * from an arithmetic operation. Maybe it should have an
757 * expression type of its own..
759 if (expr->op == '*')
760 return show_access(expr);
761 if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
762 return show_inc_dec(expr, 0);
763 return show_regular_preop(expr);
766 static int show_postop(struct expression *expr)
768 return show_inc_dec(expr, 1);
771 static int show_symbol_expr(struct symbol *sym)
773 int new = new_pseudo();
775 if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC)) {
776 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer, new, show_ident(sym->ident));
777 return new;
779 if (sym->ctype.modifiers & MOD_ADDRESSABLE) {
780 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer, new, sym->value);
781 return new;
783 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer, new, show_ident(sym->ident), sym);
784 return new;
787 static int show_symbol_init(struct symbol *sym)
789 struct expression *expr = sym->initializer;
791 if (expr) {
792 int val, addr, bits;
794 bits = expr->ctype->bit_size;
795 val = show_expression(expr);
796 addr = show_symbol_expr(sym);
797 show_store_gen(bits, val, NULL, addr);
799 return 0;
802 static int type_is_signed(struct symbol *sym)
804 if (sym->type == SYM_NODE)
805 sym = sym->ctype.base_type;
806 if (sym->type == SYM_PTR)
807 return 0;
808 return !(sym->ctype.modifiers & MOD_UNSIGNED);
811 static int show_cast_expr(struct expression *expr)
813 struct symbol *old_type, *new_type;
814 int op = show_expression(expr->cast_expression);
815 int oldbits, newbits;
816 int new, is_signed;
818 old_type = expr->cast_expression->ctype;
819 new_type = expr->cast_type;
821 oldbits = old_type->bit_size;
822 newbits = new_type->bit_size;
823 if (oldbits >= newbits)
824 return op;
825 new = new_pseudo();
826 is_signed = type_is_signed(old_type);
827 if (is_signed) {
828 printf("\tsext%d.%d\tv%d,v%d\n", oldbits, newbits, new, op);
829 } else {
830 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits, new, op, (1UL << oldbits)-1);
832 return new;
835 static int show_value(struct expression *expr)
837 int new = new_pseudo();
838 unsigned long long value = expr->value;
840 printf("\tmovi.%d\t\tv%d,$%llu\n", expr->ctype->bit_size, new, value);
841 return new;
844 static int show_string_expr(struct expression *expr)
846 int new = new_pseudo();
848 printf("\tmovi.%d\t\tv%d,&%s\n", bits_in_pointer, new, show_string(expr->string));
849 return new;
852 static int show_bitfield_expr(struct expression *expr)
854 return show_access(expr);
857 int show_label_expr(struct expression *expr)
859 int new = new_pseudo();
860 printf("\tmovi.%d\t\tv%d,.L%p\n",bits_in_pointer, new, expr->label_symbol);
861 return new;
864 static int show_conditional_expr(struct expression *expr)
866 int cond = show_expression(expr->conditional);
867 int true = show_expression(expr->cond_true);
868 int false = show_expression(expr->cond_false);
869 int new = new_pseudo();
871 if (!true)
872 true = cond;
873 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond, expr->ctype->bit_size, new, true, false);
874 return new;
877 static int show_statement_expr(struct expression *expr)
879 return show_statement(expr->statement);
882 static int show_position_expr(struct expression *expr, struct symbol *base)
884 int new = show_expression(expr->init_expr);
885 struct symbol *ctype = expr->init_sym;
887 printf("\tinsert v%d at [%d:%d] of %s\n", new,
888 expr->init_offset, ctype->bit_offset,
889 show_ident(base->ident));
890 return 0;
893 static int show_initializer_expr(struct expression *expr, struct symbol *ctype)
895 struct expression *entry;
897 FOR_EACH_PTR(expr->expr_list, entry) {
898 // Nested initializers have their positions already
899 // recursively calculated - just output them too
900 if (entry->type == EXPR_INITIALIZER) {
901 show_initializer_expr(entry, ctype);
902 continue;
905 // Ignore initializer indexes and identifiers - the
906 // evaluator has taken them into account
907 if (entry->type == EXPR_IDENTIFIER || entry->type == EXPR_INDEX)
908 continue;
909 if (entry->type == EXPR_POS) {
910 show_position_expr(entry, ctype);
911 continue;
913 show_initialization(ctype, entry);
914 } END_FOR_EACH_PTR;
915 return 0;
918 int show_symbol_expr_init(struct symbol *sym)
920 struct expression *expr = sym->initializer;
922 if (expr)
923 show_expression(expr);
924 return show_symbol_expr(sym);
928 * Print out an expression. Return the pseudo that contains the
929 * variable.
931 int show_expression(struct expression *expr)
933 if (!expr)
934 return 0;
936 if (!expr->ctype) {
937 struct position *pos = &expr->pos;
938 printf("\tno type at %s:%d:%d\n",
939 input_streams[pos->stream].name,
940 pos->line, pos->pos);
941 return 0;
944 switch (expr->type) {
945 case EXPR_CALL:
946 return show_call_expression(expr);
948 case EXPR_ASSIGNMENT:
949 return show_assignment(expr);
951 case EXPR_BINOP:
952 case EXPR_COMMA:
953 case EXPR_COMPARE:
954 case EXPR_LOGICAL:
955 return show_binop(expr);
956 case EXPR_PREOP:
957 return show_preop(expr);
958 case EXPR_POSTOP:
959 return show_postop(expr);
960 case EXPR_SYMBOL:
961 return show_symbol_expr(expr->symbol);
962 case EXPR_DEREF:
963 case EXPR_SIZEOF:
964 warn(expr->pos, "invalid expression after evaluation");
965 return 0;
966 case EXPR_CAST:
967 return show_cast_expr(expr);
968 case EXPR_VALUE:
969 return show_value(expr);
970 case EXPR_STRING:
971 return show_string_expr(expr);
972 case EXPR_BITFIELD:
973 return show_bitfield_expr(expr);
974 case EXPR_INITIALIZER:
975 return show_initializer_expr(expr, expr->ctype);
976 case EXPR_CONDITIONAL:
977 return show_conditional_expr(expr);
978 case EXPR_STATEMENT:
979 return show_statement_expr(expr);
980 case EXPR_LABEL:
981 return show_label_expr(expr);
983 // None of these should exist as direct expressions: they are only
984 // valid as sub-expressions of initializers.
985 case EXPR_POS:
986 warn(expr->pos, "unable to show plain initializer position expression");
987 return 0;
988 case EXPR_IDENTIFIER:
989 warn(expr->pos, "unable to show identifier expression");
990 return 0;
991 case EXPR_INDEX:
992 warn(expr->pos, "unable to show index expression");
993 return 0;
994 case EXPR_TYPE:
995 warn(expr->pos, "unable to show type expression");
996 return 0;
998 return 0;