Do some kind of signed cast too.
[smatch.git] / show-parse.c
blob695a1be860afa7b59c986443d80470da9b71b8d1
1 /*
2 * sparse/show-parse.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 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 "allocate.h"
21 #include "token.h"
22 #include "parse.h"
23 #include "symbol.h"
24 #include "scope.h"
25 #include "expression.h"
26 #include "target.h"
28 static int show_symbol_expr(struct symbol *sym);
29 static int show_string_expr(struct expression *expr);
31 static void do_debug_symbol(struct symbol *sym, int indent)
33 static const char indent_string[] = " ";
34 static const char *typestr[] = {
35 [SYM_UNINITIALIZED] = "none",
36 [SYM_PREPROCESSOR] = "cpp.",
37 [SYM_BASETYPE] = "base",
38 [SYM_NODE] = "node",
39 [SYM_PTR] = "ptr.",
40 [SYM_FN] = "fn..",
41 [SYM_ARRAY] = "arry",
42 [SYM_STRUCT] = "strt",
43 [SYM_UNION] = "unin",
44 [SYM_ENUM] = "enum",
45 [SYM_TYPEDEF] = "tdef",
46 [SYM_TYPEOF] = "tpof",
47 [SYM_MEMBER] = "memb",
48 [SYM_BITFIELD] = "bitf",
49 [SYM_LABEL] = "labl",
50 [SYM_RESTRICT] = "rstr",
51 [SYM_BAD] = "bad.",
54 if (!sym)
55 return;
56 fprintf(stderr, "%.*s%s%3d:%lu %lx %s (as: %d, context: %x:%x) %p (%s:%d:%d)\n",
57 indent, indent_string, typestr[sym->type],
58 sym->bit_size, sym->ctype.alignment,
59 sym->ctype.modifiers, show_ident(sym->ident),
60 sym->ctype.as, sym->ctype.in_context, sym->ctype.out_context,
61 sym, stream_name(sym->pos.stream), sym->pos.line, sym->pos.pos);
62 if (sym->type == SYM_FN) {
63 int i = 1;
64 struct symbol *arg;
65 FOR_EACH_PTR(sym->arguments, arg) {
66 fprintf(stderr, "< arg%d:\n", i);
67 do_debug_symbol(arg, 0);
68 fprintf(stderr, " end arg%d >\n", i);
69 i++;
70 } END_FOR_EACH_PTR(arg);
72 do_debug_symbol(sym->ctype.base_type, indent+2);
75 void debug_symbol(struct symbol *sym)
77 do_debug_symbol(sym, 0);
81 * Symbol type printout. The type system is by far the most
82 * complicated part of C - everything else is trivial.
84 const char *modifier_string(unsigned long mod)
86 static char buffer[100];
87 char *p = buffer;
88 const char *res,**ptr, *names[] = {
89 "auto", "register", "static", "extern",
90 "const", "volatile", "[signed]", "[unsigned]",
91 "[char]", "[short]", "[long]", "[long]",
92 "[typdef]", "[structof]", "[unionof]", "[enum]",
93 "[typeof]", "[attribute]", "inline", "[addressable]",
94 "[nocast]", "[noderef]", "[accessed]", "[toplevel]",
95 "[label]", "[assigned]", "[type]", "[safe]",
96 "[usertype]", "[force]", "[explicitly-signed]",
97 NULL
99 ptr = names;
100 while ((res = *ptr++) != NULL) {
101 if (mod & 1) {
102 char c;
103 while ((c = *res++) != '\0')
104 *p++ = c;
105 *p++ = ' ';
107 mod >>= 1;
109 *p = 0;
110 return buffer;
113 void show_struct_member(struct symbol *sym)
115 printf("\t%s:%d:%ld at offset %ld.%d", show_ident(sym->ident), sym->bit_size, sym->ctype.alignment, sym->offset, sym->bit_offset);
116 printf("\n");
119 void show_symbol_list(struct symbol_list *list, const char *sep)
121 struct symbol *sym;
122 const char *prepend = "";
124 FOR_EACH_PTR(list, sym) {
125 puts(prepend);
126 prepend = ", ";
127 show_symbol(sym);
128 } END_FOR_EACH_PTR(sym);
131 struct type_name {
132 char *start;
133 char *end;
136 static void prepend(struct type_name *name, const char *fmt, ...)
138 static char buffer[512];
139 int n;
141 va_list args;
142 va_start(args, fmt);
143 n = vsprintf(buffer, fmt, args);
144 va_end(args);
146 name->start -= n;
147 memcpy(name->start, buffer, n);
150 static void append(struct type_name *name, const char *fmt, ...)
152 static char buffer[512];
153 int n;
155 va_list args;
156 va_start(args, fmt);
157 n = vsprintf(buffer, fmt, args);
158 va_end(args);
160 memcpy(name->end, buffer, n);
161 name->end += n;
164 static void do_show_type(struct symbol *sym, struct type_name *name)
166 int i, modlen;
167 const char *mod;
168 static struct ctype_name {
169 struct symbol *sym;
170 const char *name;
171 } typenames[] = {
172 { & char_ctype, "char" },
173 { &schar_ctype, "signed char" },
174 { &uchar_ctype, "unsigned char" },
175 { & short_ctype, "short" },
176 { &sshort_ctype, "signed short" },
177 { &ushort_ctype, "unsigned short" },
178 { & int_ctype, "int" },
179 { &sint_ctype, "signed int" },
180 { &uint_ctype, "unsigned int" },
181 { &slong_ctype, "signed long" },
182 { & long_ctype, "long" },
183 { &ulong_ctype, "unsigned long" },
184 { & llong_ctype, "long long" },
185 { &sllong_ctype, "signed long long" },
186 { &ullong_ctype, "unsigned long long" },
188 { &void_ctype, "void" },
189 { &bool_ctype, "bool" },
190 { &string_ctype, "string" },
192 { &float_ctype, "float" },
193 { &double_ctype, "double" },
194 { &ldouble_ctype,"long double" },
195 { &incomplete_ctype, "incomplete type" },
196 { &label_ctype, "label type" },
197 { &bad_ctype, "bad type" },
200 if (!sym)
201 return;
203 for (i = 0; i < sizeof(typenames)/sizeof(typenames[0]); i++) {
204 if (typenames[i].sym == sym) {
205 int len = strlen(typenames[i].name);
206 *--name->start = ' ';
207 name->start -= len;
208 memcpy(name->start, typenames[i].name, len);
209 return;
213 /* Prepend */
214 switch (sym->type) {
215 case SYM_PTR:
216 prepend(name, "*");
217 break;
218 case SYM_FN:
219 prepend(name, "( ");
220 break;
221 case SYM_STRUCT:
222 prepend(name, "struct %s ", show_ident(sym->ident));
223 return;
225 case SYM_UNION:
226 prepend(name, "union %s ", show_ident(sym->ident));
227 return;
229 case SYM_ENUM:
230 prepend(name, "enum %s ", show_ident(sym->ident));
231 break;
233 case SYM_NODE:
234 append(name, "%s", show_ident(sym->ident));
235 break;
237 case SYM_BITFIELD:
238 append(name, ":%d", sym->bit_size);
239 break;
241 case SYM_LABEL:
242 append(name, "label(%s:%p)", show_ident(sym->ident), sym);
243 break;
245 case SYM_ARRAY:
246 break;
248 case SYM_RESTRICT:
249 break;
251 default:
252 prepend(name, "unknown type %d", sym->type);
253 return;
256 mod = modifier_string(sym->ctype.modifiers);
257 modlen = strlen(mod);
258 name->start -= modlen;
259 memcpy(name->start, mod, modlen);
261 do_show_type(sym->ctype.base_type, name);
263 /* Postpend */
264 if (sym->ctype.as)
265 append(name, "<asn:%d>", sym->ctype.as);
267 switch (sym->type) {
268 case SYM_PTR:
269 return;
271 case SYM_FN:
272 append(name, " )( ... )");
273 return;
275 case SYM_ARRAY:
276 append(name, "[%lld]", get_expression_value(sym->array_size));
277 return;
279 case SYM_RESTRICT:
280 prepend(name, "restricted ");
281 return;
283 default:
284 break;
288 void show_type(struct symbol *sym)
290 char array[200];
291 struct type_name name;
293 name.start = name.end = array+100;
294 do_show_type(sym, &name);
295 *name.end = 0;
296 printf("%s", name.start);
299 const char *show_typename(struct symbol *sym)
301 static char array[200];
302 struct type_name name;
304 name.start = name.end = array+100;
305 do_show_type(sym, &name);
306 *name.end = 0;
307 return name.start;
310 void show_symbol(struct symbol *sym)
312 struct symbol *type;
314 if (!sym)
315 return;
317 if (sym->ctype.alignment)
318 printf(".align %ld\n", sym->ctype.alignment);
320 show_type(sym);
321 type = sym->ctype.base_type;
322 if (!type)
323 return;
326 * Show actual implementation information
328 switch (type->type) {
329 struct symbol *member;
331 case SYM_STRUCT:
332 case SYM_UNION:
333 printf(" {\n");
334 FOR_EACH_PTR(type->symbol_list, member) {
335 show_struct_member(member);
336 } END_FOR_EACH_PTR(member);
337 printf("}\n");
338 break;
340 case SYM_FN: {
341 struct statement *stmt = type->stmt;
342 if (stmt) {
343 int val;
344 printf("\n");
345 val = show_statement(stmt);
346 if (val)
347 printf("\tmov.%d\t\tretval,%d\n", stmt->ret->bit_size, val);
348 printf("\tret\n");
350 break;
353 default:
354 break;
357 if (sym->initializer) {
358 printf(" = \n");
359 show_expression(sym->initializer);
363 static int show_symbol_init(struct symbol *sym);
365 static int new_pseudo(void)
367 static int nr = 0;
368 return ++nr;
371 static int new_label(void)
373 static int label = 0;
374 return ++label;
377 static void show_switch_statement(struct statement *stmt)
379 int val = show_expression(stmt->switch_expression);
380 struct symbol *sym;
381 printf("\tswitch v%d\n", val);
384 * Debugging only: Check that the case list is correct
385 * by printing it out.
387 * This is where a _real_ back-end would go through the
388 * cases to decide whether to use a lookup table or a
389 * series of comparisons etc
391 printf("# case table:\n");
392 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
393 struct statement *case_stmt = sym->stmt;
394 struct expression *expr = case_stmt->case_expression;
395 struct expression *to = case_stmt->case_to;
397 if (!expr) {
398 printf(" default");
399 } else {
400 if (expr->type == EXPR_VALUE) {
401 printf(" case %lld", expr->value);
402 if (to) {
403 if (to->type == EXPR_VALUE) {
404 printf(" .. %lld", to->value);
405 } else {
406 printf(" .. what?");
409 } else
410 printf(" what?");
412 printf(": .L%p\n", sym->bb_target);
413 } END_FOR_EACH_PTR(sym);
414 printf("# end case table\n");
416 show_statement(stmt->switch_statement);
418 if (stmt->switch_break->used)
419 printf(".L%p:\n", stmt->switch_break->bb_target);
422 static void show_symbol_decl(struct symbol_list *syms)
424 struct symbol *sym;
425 FOR_EACH_PTR(syms, sym) {
426 show_symbol_init(sym);
427 } END_FOR_EACH_PTR(sym);
430 static int show_return_stmt(struct statement *stmt);
433 * Print out a statement
435 int show_statement(struct statement *stmt)
437 if (!stmt)
438 return 0;
439 switch (stmt->type) {
440 case STMT_RETURN:
441 return show_return_stmt(stmt);
442 case STMT_COMPOUND: {
443 struct statement *s;
444 int last = 0;
446 show_symbol_decl(stmt->syms);
447 FOR_EACH_PTR(stmt->stmts, s) {
448 last = show_statement(s);
449 } END_FOR_EACH_PTR(s);
450 if (stmt->ret) {
451 int addr, bits;
452 printf(".L%p:\n", stmt->ret);
453 addr = show_symbol_expr(stmt->ret);
454 bits = stmt->ret->bit_size;
455 last = new_pseudo();
456 printf("\tld.%d\t\tv%d,[v%d]\n", bits, last, addr);
458 return last;
461 case STMT_EXPRESSION:
462 return show_expression(stmt->expression);
463 case STMT_IF: {
464 int val, target;
465 struct expression *cond = stmt->if_conditional;
467 /* This is only valid if nobody can jump into the "dead" statement */
468 #if 0
469 if (cond->type == EXPR_VALUE) {
470 struct statement *s = stmt->if_true;
471 if (!cond->value)
472 s = stmt->if_false;
473 show_statement(s);
474 break;
476 #endif
477 val = show_expression(cond);
478 target = new_label();
479 printf("\tje\t\tv%d,.L%d\n", val, target);
480 show_statement(stmt->if_true);
481 if (stmt->if_false) {
482 int last = new_label();
483 printf("\tjmp\t\t.L%d\n", last);
484 printf(".L%d:\n", target);
485 target = last;
486 show_statement(stmt->if_false);
488 printf(".L%d:\n", target);
489 break;
491 case STMT_SWITCH:
492 show_switch_statement(stmt);
493 break;
495 case STMT_CASE:
496 printf(".L%p:\n", stmt->case_label);
497 show_statement(stmt->case_statement);
498 break;
500 case STMT_ITERATOR: {
501 struct statement *pre_statement = stmt->iterator_pre_statement;
502 struct expression *pre_condition = stmt->iterator_pre_condition;
503 struct statement *statement = stmt->iterator_statement;
504 struct statement *post_statement = stmt->iterator_post_statement;
505 struct expression *post_condition = stmt->iterator_post_condition;
506 int val, loop_top = 0, loop_bottom = 0;
508 show_symbol_decl(stmt->iterator_syms);
509 show_statement(pre_statement);
510 if (pre_condition) {
511 if (pre_condition->type == EXPR_VALUE) {
512 if (!pre_condition->value) {
513 loop_bottom = new_label();
514 printf("\tjmp\t\t.L%d\n", loop_bottom);
516 } else {
517 loop_bottom = new_label();
518 val = show_expression(pre_condition);
519 printf("\tje\t\tv%d, .L%d\n", val, loop_bottom);
522 if (!post_condition || post_condition->type != EXPR_VALUE || post_condition->value) {
523 loop_top = new_label();
524 printf(".L%d:\n", loop_top);
526 show_statement(statement);
527 if (stmt->iterator_continue->used)
528 printf(".L%p:\n", stmt->iterator_continue);
529 show_statement(post_statement);
530 if (!post_condition) {
531 printf("\tjmp\t\t.L%d\n", loop_top);
532 } else if (post_condition->type == EXPR_VALUE) {
533 if (post_condition->value)
534 printf("\tjmp\t\t.L%d\n", loop_top);
535 } else {
536 val = show_expression(post_condition);
537 printf("\tjne\t\tv%d, .L%d\n", val, loop_top);
539 if (stmt->iterator_break->used)
540 printf(".L%p:\n", stmt->iterator_break);
541 if (loop_bottom)
542 printf(".L%d:\n", loop_bottom);
543 break;
545 case STMT_NONE:
546 break;
548 case STMT_LABEL:
549 printf(".L%p:\n", stmt->label_identifier);
550 show_statement(stmt->label_statement);
551 break;
553 case STMT_GOTO:
554 if (stmt->goto_expression) {
555 int val = show_expression(stmt->goto_expression);
556 printf("\tgoto\t\t*v%d\n", val);
557 } else {
558 printf("\tgoto\t\t.L%p\n", stmt->goto_label->bb_target);
560 break;
561 case STMT_ASM:
562 printf("\tasm( .... )\n");
563 break;
564 case STMT_INTERNAL: {
565 int val = show_expression(stmt->expression);
566 printf("\tINTERNAL( %d )\n", val);
567 break;
570 return 0;
573 static int show_call_expression(struct expression *expr)
575 struct symbol *direct;
576 struct expression *arg, *fn;
577 int fncall, retval;
578 int framesize;
580 if (!expr->ctype) {
581 warning(expr->pos, "\tcall with no type!");
582 return 0;
585 framesize = 0;
586 FOR_EACH_PTR_REVERSE(expr->args, arg) {
587 int new = show_expression(arg);
588 int size = arg->ctype->bit_size;
589 printf("\tpush.%d\t\tv%d\n", size, new);
590 framesize += size >> 3;
591 } END_FOR_EACH_PTR_REVERSE(arg);
593 fn = expr->fn;
595 /* Remove dereference, if any */
596 direct = NULL;
597 if (fn->type == EXPR_PREOP) {
598 if (fn->unop->type == EXPR_SYMBOL) {
599 struct symbol *sym = fn->unop->symbol;
600 if (sym->ctype.base_type->type == SYM_FN)
601 direct = sym;
604 if (direct) {
605 printf("\tcall\t\t%s\n", show_ident(direct->ident));
606 } else {
607 fncall = show_expression(fn);
608 printf("\tcall\t\t*v%d\n", fncall);
610 if (framesize)
611 printf("\tadd.%d\t\tvSP,vSP,$%d\n", bits_in_pointer, framesize);
613 retval = new_pseudo();
614 printf("\tmov.%d\t\tv%d,retval\n", expr->ctype->bit_size, retval);
615 return retval;
618 static int show_binop(struct expression *expr)
620 int left = show_expression(expr->left);
621 int right = show_expression(expr->right);
622 int new = new_pseudo();
623 const char *opname;
624 static const char *name[] = {
625 ['+'] = "add", ['-'] = "sub",
626 ['*'] = "mul", ['/'] = "div",
627 ['%'] = "mod", ['&'] = "and",
628 ['|'] = "lor", ['^'] = "xor"
630 unsigned int op = expr->op;
632 opname = show_special(op);
633 if (op < sizeof(name)/sizeof(*name))
634 opname = name[op];
635 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname,
636 expr->ctype->bit_size,
637 new, left, right);
638 return new;
641 static int show_slice(struct expression *expr)
643 int target = show_expression(expr->base);
644 int new = new_pseudo();
645 printf("\tslice.%d\t\tv%d,v%d,%d\n", expr->r_nrbits, target, new, expr->r_bitpos);
646 return new;
649 static int show_regular_preop(struct expression *expr)
651 int target = show_expression(expr->unop);
652 int new = new_pseudo();
653 static const char *name[] = {
654 ['!'] = "nonzero", ['-'] = "neg",
655 ['~'] = "not",
657 unsigned int op = expr->op;
658 const char *opname;
660 opname = show_special(op);
661 if (op < sizeof(name)/sizeof(*name))
662 opname = name[op];
663 printf("\t%s.%d\t\tv%d,v%d\n", opname, expr->ctype->bit_size, new, target);
664 return new;
668 * FIXME! Not all accesses are memory loads. We should
669 * check what kind of symbol is behind the dereference.
671 static int show_address_gen(struct expression *expr)
673 return show_expression(expr->unop);
676 static int show_load_gen(int bits, struct expression *expr, int addr)
678 int new = new_pseudo();
680 printf("\tld.%d\t\tv%d,[v%d]\n", bits, new, addr);
681 return new;
684 static void show_store_gen(int bits, int value, struct expression *expr, int addr)
686 /* FIXME!!! Bitfield store! */
687 printf("\tst.%d\t\tv%d,[v%d]\n", bits, value, addr);
690 static int show_assignment(struct expression *expr)
692 struct expression *target = expr->left;
693 int val, addr, bits;
695 if (!expr->ctype)
696 return 0;
698 bits = expr->ctype->bit_size;
699 val = show_expression(expr->right);
700 addr = show_address_gen(target);
701 show_store_gen(bits, val, target, addr);
702 return val;
705 static int show_return_stmt(struct statement *stmt)
707 struct expression *expr = stmt->ret_value;
708 struct symbol *target = stmt->ret_target;
710 if (expr && expr->ctype) {
711 int val = show_expression(expr);
712 int bits = expr->ctype->bit_size;
713 int addr = show_symbol_expr(target);
714 show_store_gen(bits, val, NULL, addr);
716 printf("\tret\t\t(%p)\n", target);
717 return 0;
720 static int show_initialization(struct symbol *sym, struct expression *expr)
722 int val, addr, bits;
724 if (!expr->ctype)
725 return 0;
727 bits = expr->ctype->bit_size;
728 val = show_expression(expr);
729 addr = show_symbol_expr(sym);
730 // FIXME! The "target" expression is for bitfield store information.
731 // Leave it NULL, which works fine.
732 show_store_gen(bits, val, NULL, addr);
733 return 0;
736 static int show_access(struct expression *expr)
738 int addr = show_address_gen(expr);
739 return show_load_gen(expr->ctype->bit_size, expr, addr);
742 static int show_inc_dec(struct expression *expr, int postop)
744 int addr = show_address_gen(expr->unop);
745 int retval, new;
746 const char *opname = expr->op == SPECIAL_INCREMENT ? "add" : "sub";
747 int bits = expr->ctype->bit_size;
749 retval = show_load_gen(bits, expr->unop, addr);
750 new = retval;
751 if (postop)
752 new = new_pseudo();
753 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname, bits, new, retval);
754 show_store_gen(bits, new, expr->unop, addr);
755 return retval;
758 static int show_preop(struct expression *expr)
761 * '*' is an lvalue access, and is fundamentally different
762 * from an arithmetic operation. Maybe it should have an
763 * expression type of its own..
765 if (expr->op == '*')
766 return show_access(expr);
767 if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
768 return show_inc_dec(expr, 0);
769 return show_regular_preop(expr);
772 static int show_postop(struct expression *expr)
774 return show_inc_dec(expr, 1);
777 static int show_symbol_expr(struct symbol *sym)
779 int new = new_pseudo();
781 if (sym->initializer && sym->initializer->type == EXPR_STRING)
782 return show_string_expr(sym->initializer);
784 if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC)) {
785 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer, new, show_ident(sym->ident));
786 return new;
788 if (sym->ctype.modifiers & MOD_ADDRESSABLE) {
789 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer, new, sym->value);
790 return new;
792 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer, new, show_ident(sym->ident), sym);
793 return new;
796 static int show_symbol_init(struct symbol *sym)
798 struct expression *expr = sym->initializer;
800 if (expr) {
801 int val, addr, bits;
803 bits = expr->ctype->bit_size;
804 val = show_expression(expr);
805 addr = show_symbol_expr(sym);
806 show_store_gen(bits, val, NULL, addr);
808 return 0;
811 static int type_is_signed(struct symbol *sym)
813 if (sym->type == SYM_NODE)
814 sym = sym->ctype.base_type;
815 if (sym->type == SYM_PTR)
816 return 0;
817 return !(sym->ctype.modifiers & MOD_UNSIGNED);
820 static int show_cast_expr(struct expression *expr)
822 struct symbol *old_type, *new_type;
823 int op = show_expression(expr->cast_expression);
824 int oldbits, newbits;
825 int new, is_signed;
827 old_type = expr->cast_expression->ctype;
828 new_type = expr->cast_type;
830 oldbits = old_type->bit_size;
831 newbits = new_type->bit_size;
832 if (oldbits >= newbits)
833 return op;
834 new = new_pseudo();
835 is_signed = type_is_signed(old_type);
836 if (is_signed) {
837 printf("\tsext%d.%d\tv%d,v%d\n", oldbits, newbits, new, op);
838 } else {
839 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits, new, op, (1UL << oldbits)-1);
841 return new;
844 static int show_value(struct expression *expr)
846 int new = new_pseudo();
847 unsigned long long value = expr->value;
849 printf("\tmovi.%d\t\tv%d,$%llu\n", expr->ctype->bit_size, new, value);
850 return new;
853 static int show_fvalue(struct expression *expr)
855 int new = new_pseudo();
856 long double value = expr->fvalue;
858 printf("\tmovf.%d\t\tv%d,$%Lf\n", expr->ctype->bit_size, new, value);
859 return new;
862 static int show_string_expr(struct expression *expr)
864 int new = new_pseudo();
866 printf("\tmovi.%d\t\tv%d,&%s\n", bits_in_pointer, new, show_string(expr->string));
867 return new;
870 int show_label_expr(struct expression *expr)
872 int new = new_pseudo();
873 printf("\tmovi.%d\t\tv%d,.L%p\n",bits_in_pointer, new, expr->label_symbol);
874 return new;
877 static int show_conditional_expr(struct expression *expr)
879 int cond = show_expression(expr->conditional);
880 int true = show_expression(expr->cond_true);
881 int false = show_expression(expr->cond_false);
882 int new = new_pseudo();
884 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond, expr->ctype->bit_size, new, true, false);
885 return new;
888 static int show_statement_expr(struct expression *expr)
890 return show_statement(expr->statement);
893 static int show_position_expr(struct expression *expr, struct symbol *base)
895 int new = show_expression(expr->init_expr);
896 struct symbol *ctype = expr->init_expr->ctype;
897 int bit_offset;
899 bit_offset = ctype ? ctype->bit_offset : -1;
901 printf("\tinsert v%d at [%d:%d] of %s\n", new,
902 expr->init_offset, bit_offset,
903 show_ident(base->ident));
904 return 0;
907 static int show_initializer_expr(struct expression *expr, struct symbol *ctype)
909 struct expression *entry;
911 FOR_EACH_PTR(expr->expr_list, entry) {
913 again:
914 // Nested initializers have their positions already
915 // recursively calculated - just output them too
916 if (entry->type == EXPR_INITIALIZER) {
917 show_initializer_expr(entry, ctype);
918 continue;
921 // Initializer indexes and identifiers should
922 // have been evaluated to EXPR_POS
923 if (entry->type == EXPR_IDENTIFIER) {
924 printf(" AT '%s':\n", show_ident(entry->expr_ident));
925 entry = entry->ident_expression;
926 goto again;
929 if (entry->type == EXPR_INDEX) {
930 printf(" AT '%d..%d:\n", entry->idx_from, entry->idx_to);
931 entry = entry->idx_expression;
932 goto again;
934 if (entry->type == EXPR_POS) {
935 show_position_expr(entry, ctype);
936 continue;
938 show_initialization(ctype, entry);
939 } END_FOR_EACH_PTR(entry);
940 return 0;
943 int show_symbol_expr_init(struct symbol *sym)
945 struct expression *expr = sym->initializer;
947 if (expr)
948 show_expression(expr);
949 return show_symbol_expr(sym);
953 * Print out an expression. Return the pseudo that contains the
954 * variable.
956 int show_expression(struct expression *expr)
958 if (!expr)
959 return 0;
961 if (!expr->ctype) {
962 struct position *pos = &expr->pos;
963 printf("\tno type at %s:%d:%d\n",
964 stream_name(pos->stream),
965 pos->line, pos->pos);
966 return 0;
969 switch (expr->type) {
970 case EXPR_CALL:
971 return show_call_expression(expr);
973 case EXPR_ASSIGNMENT:
974 return show_assignment(expr);
976 case EXPR_BINOP:
977 case EXPR_COMMA:
978 case EXPR_COMPARE:
979 case EXPR_LOGICAL:
980 return show_binop(expr);
981 case EXPR_PREOP:
982 return show_preop(expr);
983 case EXPR_POSTOP:
984 return show_postop(expr);
985 case EXPR_SYMBOL:
986 return show_symbol_expr(expr->symbol);
987 case EXPR_DEREF:
988 case EXPR_SIZEOF:
989 case EXPR_PTRSIZEOF:
990 case EXPR_ALIGNOF:
991 warning(expr->pos, "invalid expression after evaluation");
992 return 0;
993 case EXPR_CAST:
994 case EXPR_IMPLIED_CAST:
995 return show_cast_expr(expr);
996 case EXPR_VALUE:
997 return show_value(expr);
998 case EXPR_FVALUE:
999 return show_fvalue(expr);
1000 case EXPR_STRING:
1001 return show_string_expr(expr);
1002 case EXPR_INITIALIZER:
1003 return show_initializer_expr(expr, expr->ctype);
1004 case EXPR_SELECT:
1005 case EXPR_CONDITIONAL:
1006 return show_conditional_expr(expr);
1007 case EXPR_STATEMENT:
1008 return show_statement_expr(expr);
1009 case EXPR_LABEL:
1010 return show_label_expr(expr);
1011 case EXPR_SLICE:
1012 return show_slice(expr);
1014 // None of these should exist as direct expressions: they are only
1015 // valid as sub-expressions of initializers.
1016 case EXPR_POS:
1017 warning(expr->pos, "unable to show plain initializer position expression");
1018 return 0;
1019 case EXPR_IDENTIFIER:
1020 warning(expr->pos, "unable to show identifier expression");
1021 return 0;
1022 case EXPR_INDEX:
1023 warning(expr->pos, "unable to show index expression");
1024 return 0;
1025 case EXPR_TYPE:
1026 warning(expr->pos, "unable to show type expression");
1027 return 0;
1029 return 0;