Add "select" expression.
[smatch.git] / show-parse.c
blobab49d517b095a455fc8e779dd80f2d7b45dd3bf6
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" },
182 { &incomplete_ctype, "incomplete type" },
185 if (!sym)
186 return;
188 for (i = 0; i < sizeof(typenames)/sizeof(typenames[0]); i++) {
189 if (typenames[i].sym == sym) {
190 int len = strlen(typenames[i].name);
191 *--name->start = ' ';
192 name->start -= len;
193 memcpy(name->start, typenames[i].name, len);
194 return;
198 /* Prepend */
199 switch (sym->type) {
200 case SYM_PTR:
201 prepend(name, "*");
202 break;
203 case SYM_FN:
204 prepend(name, "( ");
205 break;
206 case SYM_STRUCT:
207 prepend(name, "struct %s ", show_ident(sym->ident));
208 return;
210 case SYM_UNION:
211 prepend(name, "union %s ", show_ident(sym->ident));
212 return;
214 case SYM_ENUM:
215 prepend(name, "enum %s ", show_ident(sym->ident));
216 return;
218 case SYM_NODE:
219 append(name, "%s", show_ident(sym->ident));
220 break;
222 case SYM_BITFIELD:
223 append(name, ":%d", sym->fieldwidth);
224 break;
226 case SYM_LABEL:
227 append(name, "label(%s:%p)", show_ident(sym->ident), sym);
228 break;
230 case SYM_ARRAY:
231 break;
233 default:
234 prepend(name, "unknown type %d", sym->type);
235 return;
238 mod = modifier_string(sym->ctype.modifiers);
239 modlen = strlen(mod);
240 name->start -= modlen;
241 memcpy(name->start, mod, modlen);
243 do_show_type(sym->ctype.base_type, name);
245 /* Postpend */
246 if (sym->ctype.as)
247 append(name, "<asn:%d>", sym->ctype.as);
249 switch (sym->type) {
250 case SYM_PTR:
251 return;
253 case SYM_FN:
254 append(name, " )( ... )");
255 return;
257 case SYM_ARRAY:
258 append(name, "[%lld]", get_expression_value(sym->array_size));
259 return;
260 default:
261 break;
265 void show_type(struct symbol *sym)
267 char array[200];
268 struct type_name name;
270 name.start = name.end = array+100;
271 do_show_type(sym, &name);
272 *name.end = 0;
273 printf("%s", name.start);
276 const char *show_typename(struct symbol *sym)
278 static char array[200];
279 struct type_name name;
281 name.start = name.end = array+100;
282 do_show_type(sym, &name);
283 *name.end = 0;
284 return name.start;
287 void show_symbol(struct symbol *sym)
289 struct symbol *type;
291 if (!sym)
292 return;
294 if (sym->ctype.alignment)
295 printf(".align %ld\n", sym->ctype.alignment);
297 show_type(sym);
298 type = sym->ctype.base_type;
299 if (!type)
300 return;
303 * Show actual implementation information
305 switch (type->type) {
306 case SYM_STRUCT:
307 symbol_iterate(type->symbol_list, show_struct_member, NULL);
308 break;
310 case SYM_UNION:
311 symbol_iterate(type->symbol_list, show_struct_member, NULL);
312 break;
314 case SYM_FN: {
315 struct statement *stmt = type->stmt;
316 if (stmt) {
317 int val;
318 printf("\n");
319 val = show_statement(stmt);
320 if (val)
321 printf("\tmov.%d\t\tretval,%d\n", stmt->ret->bit_size, val);
322 printf("\tret\n");
324 break;
327 default:
328 break;
331 if (sym->initializer) {
332 printf(" = \n");
333 show_expression(sym->initializer);
337 static int show_symbol_init(struct symbol *sym);
339 static int new_pseudo(void)
341 static int nr = 0;
342 return ++nr;
345 static int new_label(void)
347 static int label = 0;
348 return ++label;
351 static void show_switch_statement(struct statement *stmt)
353 int val = show_expression(stmt->switch_expression);
354 struct symbol *sym;
355 printf("\tswitch v%d\n", val);
358 * Debugging only: Check that the case list is correct
359 * by printing it out.
361 * This is where a _real_ back-end would go through the
362 * cases to decide whether to use a lookup table or a
363 * series of comparisons etc
365 printf("# case table:\n");
366 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
367 struct statement *case_stmt = sym->stmt;
368 struct expression *expr = case_stmt->case_expression;
369 struct expression *to = case_stmt->case_to;
371 if (!expr) {
372 printf(" default");
373 } else {
374 if (expr->type == EXPR_VALUE) {
375 printf(" case %lld", expr->value);
376 if (to) {
377 if (to->type == EXPR_VALUE) {
378 printf(" .. %lld", to->value);
379 } else {
380 printf(" .. what?");
383 } else
384 printf(" what?");
386 printf(": .L%p\n", sym->bb_target);
387 } END_FOR_EACH_PTR;
388 printf("# end case table\n");
390 show_statement(stmt->switch_statement);
392 if (stmt->switch_break->used)
393 printf(".L%p:\n", stmt->switch_break->bb_target);
396 static void show_symbol_decl(struct symbol_list *syms)
398 struct symbol *sym;
399 FOR_EACH_PTR(syms, sym) {
400 show_symbol_init(sym);
401 } END_FOR_EACH_PTR;
404 static int show_return_stmt(struct statement *stmt);
407 * Print out a statement
409 int show_statement(struct statement *stmt)
411 if (!stmt)
412 return 0;
413 switch (stmt->type) {
414 case STMT_RETURN:
415 return show_return_stmt(stmt);
416 case STMT_COMPOUND: {
417 struct statement *s;
418 int last = 0;
420 show_symbol_decl(stmt->syms);
421 FOR_EACH_PTR(stmt->stmts, s) {
422 last = show_statement(s);
423 } END_FOR_EACH_PTR;
424 if (stmt->ret) {
425 int addr, bits;
426 printf(".L%p:\n", stmt->ret);
427 addr = show_symbol_expr(stmt->ret);
428 bits = stmt->ret->bit_size;
429 last = new_pseudo();
430 printf("\tld.%d\t\tv%d,[v%d]\n", bits, last, addr);
432 return last;
435 case STMT_EXPRESSION:
436 return show_expression(stmt->expression);
437 case STMT_IF: {
438 int val, target;
439 struct expression *cond = stmt->if_conditional;
441 /* This is only valid if nobody can jump into the "dead" statement */
442 #if 0
443 if (cond->type == EXPR_VALUE) {
444 struct statement *s = stmt->if_true;
445 if (!cond->value)
446 s = stmt->if_false;
447 show_statement(s);
448 break;
450 #endif
451 val = show_expression(cond);
452 target = new_label();
453 printf("\tje\t\tv%d,.L%d\n", val, target);
454 show_statement(stmt->if_true);
455 if (stmt->if_false) {
456 int last = new_label();
457 printf("\tjmp\t\t.L%d\n", last);
458 printf(".L%d:\n", target);
459 target = last;
460 show_statement(stmt->if_false);
462 printf(".L%d:\n", target);
463 break;
465 case STMT_SWITCH:
466 show_switch_statement(stmt);
467 break;
469 case STMT_CASE:
470 printf(".L%p:\n", stmt->case_label);
471 show_statement(stmt->case_statement);
472 break;
474 case STMT_ITERATOR: {
475 struct statement *pre_statement = stmt->iterator_pre_statement;
476 struct expression *pre_condition = stmt->iterator_pre_condition;
477 struct statement *statement = stmt->iterator_statement;
478 struct statement *post_statement = stmt->iterator_post_statement;
479 struct expression *post_condition = stmt->iterator_post_condition;
480 int val, loop_top = 0, loop_bottom = 0;
482 show_symbol_decl(stmt->iterator_syms);
483 show_statement(pre_statement);
484 if (pre_condition) {
485 if (pre_condition->type == EXPR_VALUE) {
486 if (!pre_condition->value) {
487 loop_bottom = new_label();
488 printf("\tjmp\t\t.L%d\n", loop_bottom);
490 } else {
491 loop_bottom = new_label();
492 val = show_expression(pre_condition);
493 printf("\tje\t\tv%d, .L%d\n", val, loop_bottom);
496 if (!post_condition || post_condition->type != EXPR_VALUE || post_condition->value) {
497 loop_top = new_label();
498 printf(".L%d:\n", loop_top);
500 show_statement(statement);
501 if (stmt->iterator_continue->used)
502 printf(".L%p:\n", stmt->iterator_continue);
503 show_statement(post_statement);
504 if (!post_condition) {
505 printf("\tjmp\t\t.L%d\n", loop_top);
506 } else if (post_condition->type == EXPR_VALUE) {
507 if (post_condition->value)
508 printf("\tjmp\t\t.L%d\n", loop_top);
509 } else {
510 val = show_expression(post_condition);
511 printf("\tjne\t\tv%d, .L%d\n", val, loop_top);
513 if (stmt->iterator_break->used)
514 printf(".L%p:\n", stmt->iterator_break);
515 if (loop_bottom)
516 printf(".L%d:\n", loop_bottom);
517 break;
519 case STMT_NONE:
520 break;
522 case STMT_LABEL:
523 printf(".L%p:\n", stmt->label_identifier);
524 show_statement(stmt->label_statement);
525 break;
527 case STMT_GOTO:
528 if (stmt->goto_expression) {
529 int val = show_expression(stmt->goto_expression);
530 printf("\tgoto\t\t*v%d\n", val);
531 } else {
532 printf("\tgoto\t\t.L%p\n", stmt->goto_label->bb_target);
534 break;
535 case STMT_ASM:
536 printf("\tasm( .... )\n");
537 break;
540 return 0;
543 static void show_one_statement(struct statement *stmt, void *sep, int flags)
545 show_statement(stmt);
546 if (!(flags & ITERATE_LAST))
547 printf("%s", (const char *)sep);
550 void show_statement_list(struct statement_list *stmt, const char *sep)
552 statement_iterate(stmt, show_one_statement, (void *)sep);
555 static void show_one_expression(struct expression *expr, void *sep, int flags)
557 show_expression(expr);
558 if (!(flags & ITERATE_LAST))
559 printf("%s", (const char *)sep);
562 void show_expression_list(struct expression_list *list, const char *sep)
564 expression_iterate(list, show_one_expression, (void *)sep);
567 static int show_call_expression(struct expression *expr)
569 struct symbol *direct;
570 struct expression *arg, *fn;
571 int fncall, retval;
572 int framesize;
574 if (!expr->ctype) {
575 warn(expr->pos, "\tcall with no type!");
576 return 0;
579 framesize = 0;
580 FOR_EACH_PTR_REVERSE(expr->args, arg) {
581 int new = show_expression(arg);
582 int size = arg->ctype->bit_size;
583 printf("\tpush.%d\t\tv%d\n", size, new);
584 framesize += size >> 3;
585 } END_FOR_EACH_PTR_REVERSE;
587 fn = expr->fn;
589 /* Remove dereference, if any */
590 direct = NULL;
591 if (fn->type == EXPR_PREOP) {
592 if (fn->unop->type == EXPR_SYMBOL) {
593 struct symbol *sym = fn->unop->symbol;
594 if (sym->ctype.base_type->type == SYM_FN)
595 direct = sym;
598 if (direct) {
599 printf("\tcall\t\t%s\n", show_ident(direct->ident));
600 } else {
601 fncall = show_expression(fn);
602 printf("\tcall\t\t*v%d\n", fncall);
604 if (framesize)
605 printf("\tadd.%d\t\tvSP,vSP,$%d\n", bits_in_pointer, framesize);
607 retval = new_pseudo();
608 printf("\tmov.%d\t\tv%d,retval\n", expr->ctype->bit_size, retval);
609 return retval;
612 static int show_binop(struct expression *expr)
614 int left = show_expression(expr->left);
615 int right = show_expression(expr->right);
616 int new = new_pseudo();
617 const char *opname;
618 static const char *name[] = {
619 ['+'] = "add", ['-'] = "sub",
620 ['*'] = "mul", ['/'] = "div",
621 ['%'] = "mod", ['&'] = "and",
622 ['|'] = "lor", ['^'] = "xor"
624 unsigned int op = expr->op;
626 opname = show_special(op);
627 if (op < sizeof(name)/sizeof(*name))
628 opname = name[op];
629 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname,
630 expr->ctype->bit_size,
631 new, left, right);
632 return new;
635 static int show_regular_preop(struct expression *expr)
637 int target = show_expression(expr->unop);
638 int new = new_pseudo();
639 static const char *name[] = {
640 ['!'] = "nonzero", ['-'] = "neg",
641 ['~'] = "not",
643 unsigned int op = expr->op;
644 const char *opname;
646 opname = show_special(op);
647 if (op < sizeof(name)/sizeof(*name))
648 opname = name[op];
649 printf("\t%s.%d\t\tv%d,v%d\n", opname, expr->ctype->bit_size, new, target);
650 return new;
654 * FIXME! Not all accesses are memory loads. We should
655 * check what kind of symbol is behind the dereference.
657 static int show_address_gen(struct expression *expr)
659 if (expr->type == EXPR_PREOP)
660 return show_expression(expr->unop);
661 return show_expression(expr->address);
664 static int show_load_gen(int bits, struct expression *expr, int addr)
666 int new = new_pseudo();
668 printf("\tld.%d\t\tv%d,[v%d]\n", bits, new, addr);
669 if (expr->type == EXPR_PREOP)
670 return new;
672 /* bitfield load! */
673 if (expr->bitpos)
674 printf("\tshr.%d\t\tv%d,v%d,$%d\n", bits, new, new, expr->bitpos);
675 printf("\tandi.%d\t\tv%d,v%d,$%llu\n", bits, new, new, (1ULL << expr->nrbits)-1);
676 return new;
679 static void show_store_gen(int bits, int value, struct expression *expr, int addr)
681 /* FIXME!!! Bitfield store! */
682 printf("\tst.%d\t\tv%d,[v%d]\n", bits, value, addr);
685 static int show_assignment(struct expression *expr)
687 struct expression *target = expr->left;
688 int val, addr, bits;
690 if (!expr->ctype)
691 return 0;
693 bits = expr->ctype->bit_size;
694 val = show_expression(expr->right);
695 addr = show_address_gen(target);
696 show_store_gen(bits, val, target, addr);
697 return val;
700 static int show_return_stmt(struct statement *stmt)
702 struct expression *expr = stmt->ret_value;
703 struct symbol *target = stmt->ret_target;
705 if (expr && expr->ctype) {
706 int val = show_expression(expr);
707 int bits = expr->ctype->bit_size;
708 int addr = show_symbol_expr(target);
709 show_store_gen(bits, val, NULL, addr);
711 printf("\tret\t\t(%p)\n", target);
712 return 0;
715 static int show_initialization(struct symbol *sym, struct expression *expr)
717 int val, addr, bits;
719 if (!expr->ctype)
720 return 0;
722 bits = expr->ctype->bit_size;
723 val = show_expression(expr);
724 addr = show_symbol_expr(sym);
725 // FIXME! The "target" expression is for bitfield store information.
726 // Leave it NULL, which works fine.
727 show_store_gen(bits, val, NULL, addr);
728 return 0;
731 static int show_access(struct expression *expr)
733 int addr = show_address_gen(expr);
734 return show_load_gen(expr->ctype->bit_size, expr, addr);
737 static int show_inc_dec(struct expression *expr, int postop)
739 int addr = show_address_gen(expr->unop);
740 int retval, new;
741 const char *opname = expr->op == SPECIAL_INCREMENT ? "add" : "sub";
742 int bits = expr->ctype->bit_size;
744 retval = show_load_gen(bits, expr->unop, addr);
745 new = retval;
746 if (postop)
747 new = new_pseudo();
748 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname, bits, new, retval);
749 show_store_gen(bits, new, expr->unop, addr);
750 return retval;
753 static int show_preop(struct expression *expr)
756 * '*' is an lvalue access, and is fundamentally different
757 * from an arithmetic operation. Maybe it should have an
758 * expression type of its own..
760 if (expr->op == '*')
761 return show_access(expr);
762 if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
763 return show_inc_dec(expr, 0);
764 return show_regular_preop(expr);
767 static int show_postop(struct expression *expr)
769 return show_inc_dec(expr, 1);
772 static int show_symbol_expr(struct symbol *sym)
774 int new = new_pseudo();
776 if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC)) {
777 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer, new, show_ident(sym->ident));
778 return new;
780 if (sym->ctype.modifiers & MOD_ADDRESSABLE) {
781 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer, new, sym->value);
782 return new;
784 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer, new, show_ident(sym->ident), sym);
785 return new;
788 static int show_symbol_init(struct symbol *sym)
790 struct expression *expr = sym->initializer;
792 if (expr) {
793 int val, addr, bits;
795 bits = expr->ctype->bit_size;
796 val = show_expression(expr);
797 addr = show_symbol_expr(sym);
798 show_store_gen(bits, val, NULL, addr);
800 return 0;
803 static int type_is_signed(struct symbol *sym)
805 if (sym->type == SYM_NODE)
806 sym = sym->ctype.base_type;
807 if (sym->type == SYM_PTR)
808 return 0;
809 return !(sym->ctype.modifiers & MOD_UNSIGNED);
812 static int show_cast_expr(struct expression *expr)
814 struct symbol *old_type, *new_type;
815 int op = show_expression(expr->cast_expression);
816 int oldbits, newbits;
817 int new, is_signed;
819 old_type = expr->cast_expression->ctype;
820 new_type = expr->cast_type;
822 oldbits = old_type->bit_size;
823 newbits = new_type->bit_size;
824 if (oldbits >= newbits)
825 return op;
826 new = new_pseudo();
827 is_signed = type_is_signed(old_type);
828 if (is_signed) {
829 printf("\tsext%d.%d\tv%d,v%d\n", oldbits, newbits, new, op);
830 } else {
831 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits, new, op, (1UL << oldbits)-1);
833 return new;
836 static int show_value(struct expression *expr)
838 int new = new_pseudo();
839 unsigned long long value = expr->value;
841 printf("\tmovi.%d\t\tv%d,$%llu\n", expr->ctype->bit_size, new, value);
842 return new;
845 static int show_fvalue(struct expression *expr)
847 int new = new_pseudo();
848 long double value = expr->fvalue;
850 printf("\tmovf.%d\t\tv%d,$%Lf\n", expr->ctype->bit_size, new, value);
851 return new;
854 static int show_string_expr(struct expression *expr)
856 int new = new_pseudo();
858 printf("\tmovi.%d\t\tv%d,&%s\n", bits_in_pointer, new, show_string(expr->string));
859 return new;
862 static int show_bitfield_expr(struct expression *expr)
864 return show_access(expr);
867 int show_label_expr(struct expression *expr)
869 int new = new_pseudo();
870 printf("\tmovi.%d\t\tv%d,.L%p\n",bits_in_pointer, new, expr->label_symbol);
871 return new;
874 static int show_conditional_expr(struct expression *expr)
876 int cond = show_expression(expr->conditional);
877 int true = show_expression(expr->cond_true);
878 int false = show_expression(expr->cond_false);
879 int new = new_pseudo();
881 if (!true)
882 true = cond;
883 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond, expr->ctype->bit_size, new, true, false);
884 return new;
887 static int show_statement_expr(struct expression *expr)
889 return show_statement(expr->statement);
892 static int show_position_expr(struct expression *expr, struct symbol *base)
894 int new = show_expression(expr->init_expr);
895 struct symbol *ctype = expr->init_sym;
897 printf("\tinsert v%d at [%d:%d] of %s\n", new,
898 expr->init_offset, ctype->bit_offset,
899 show_ident(base->ident));
900 return 0;
903 static int show_initializer_expr(struct expression *expr, struct symbol *ctype)
905 struct expression *entry;
907 FOR_EACH_PTR(expr->expr_list, entry) {
908 // Nested initializers have their positions already
909 // recursively calculated - just output them too
910 if (entry->type == EXPR_INITIALIZER) {
911 show_initializer_expr(entry, ctype);
912 continue;
915 // Ignore initializer indexes and identifiers - the
916 // evaluator has taken them into account
917 if (entry->type == EXPR_IDENTIFIER || entry->type == EXPR_INDEX)
918 continue;
919 if (entry->type == EXPR_POS) {
920 show_position_expr(entry, ctype);
921 continue;
923 show_initialization(ctype, entry);
924 } END_FOR_EACH_PTR;
925 return 0;
928 int show_symbol_expr_init(struct symbol *sym)
930 struct expression *expr = sym->initializer;
932 if (expr)
933 show_expression(expr);
934 return show_symbol_expr(sym);
938 * Print out an expression. Return the pseudo that contains the
939 * variable.
941 int show_expression(struct expression *expr)
943 if (!expr)
944 return 0;
946 if (!expr->ctype) {
947 struct position *pos = &expr->pos;
948 printf("\tno type at %s:%d:%d\n",
949 input_streams[pos->stream].name,
950 pos->line, pos->pos);
951 return 0;
954 switch (expr->type) {
955 case EXPR_CALL:
956 return show_call_expression(expr);
958 case EXPR_ASSIGNMENT:
959 return show_assignment(expr);
961 case EXPR_BINOP:
962 case EXPR_COMMA:
963 case EXPR_COMPARE:
964 case EXPR_LOGICAL:
965 return show_binop(expr);
966 case EXPR_PREOP:
967 return show_preop(expr);
968 case EXPR_POSTOP:
969 return show_postop(expr);
970 case EXPR_SYMBOL:
971 return show_symbol_expr(expr->symbol);
972 case EXPR_DEREF:
973 case EXPR_SIZEOF:
974 case EXPR_ALIGNOF:
975 warn(expr->pos, "invalid expression after evaluation");
976 return 0;
977 case EXPR_CAST:
978 return show_cast_expr(expr);
979 case EXPR_VALUE:
980 return show_value(expr);
981 case EXPR_FVALUE:
982 return show_fvalue(expr);
983 case EXPR_STRING:
984 return show_string_expr(expr);
985 case EXPR_BITFIELD:
986 return show_bitfield_expr(expr);
987 case EXPR_INITIALIZER:
988 return show_initializer_expr(expr, expr->ctype);
989 case EXPR_SELECT:
990 case EXPR_CONDITIONAL:
991 return show_conditional_expr(expr);
992 case EXPR_STATEMENT:
993 return show_statement_expr(expr);
994 case EXPR_LABEL:
995 return show_label_expr(expr);
997 // None of these should exist as direct expressions: they are only
998 // valid as sub-expressions of initializers.
999 case EXPR_POS:
1000 warn(expr->pos, "unable to show plain initializer position expression");
1001 return 0;
1002 case EXPR_IDENTIFIER:
1003 warn(expr->pos, "unable to show identifier expression");
1004 return 0;
1005 case EXPR_INDEX:
1006 warn(expr->pos, "unable to show index expression");
1007 return 0;
1008 case EXPR_TYPE:
1009 warn(expr->pos, "unable to show type expression");
1010 return 0;
1012 return 0;