Make sparse warn about initializers that initialize the same member twice
[smatch.git] / show-parse.c
blob5437a62f227d816449c6acb9baf9893198d8a7cc
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.",
53 struct context *context;
54 int i;
56 if (!sym)
57 return;
58 fprintf(stderr, "%.*s%s%3d:%lu %lx %s (as: %d) %p (%s:%d:%d)\n",
59 indent, indent_string, typestr[sym->type],
60 sym->bit_size, sym->ctype.alignment,
61 sym->ctype.modifiers, show_ident(sym->ident), sym->ctype.as,
62 sym, stream_name(sym->pos.stream), sym->pos.line, sym->pos.pos);
63 i = 0;
64 FOR_EACH_PTR(sym->ctype.contexts, context) {
65 /* FIXME: should print context expression */
66 fprintf(stderr, "< context%d: in=%d, out=%d\n",
67 i, context->in, context->out);
68 fprintf(stderr, " end context%d >\n", i);
69 i++;
70 } END_FOR_EACH_PTR(context);
71 if (sym->type == SYM_FN) {
72 struct symbol *arg;
73 i = 0;
74 FOR_EACH_PTR(sym->arguments, arg) {
75 fprintf(stderr, "< arg%d:\n", i);
76 do_debug_symbol(arg, 0);
77 fprintf(stderr, " end arg%d >\n", i);
78 i++;
79 } END_FOR_EACH_PTR(arg);
81 do_debug_symbol(sym->ctype.base_type, indent+2);
84 void debug_symbol(struct symbol *sym)
86 do_debug_symbol(sym, 0);
90 * Symbol type printout. The type system is by far the most
91 * complicated part of C - everything else is trivial.
93 const char *modifier_string(unsigned long mod)
95 static char buffer[100];
96 char *p = buffer;
97 const char *res,**ptr, *names[] = {
98 "auto", "register", "static", "extern",
99 "const", "volatile", "[signed]", "[unsigned]",
100 "[char]", "[short]", "[long]", "[long]",
101 "[typdef]", "[structof]", "[unionof]", "[enum]",
102 "[typeof]", "[attribute]", "inline", "[addressable]",
103 "[nocast]", "[noderef]", "[accessed]", "[toplevel]",
104 "[label]", "[assigned]", "[type]", "[safe]",
105 "[usertype]", "[force]", "[explicitly-signed]",
106 NULL
108 ptr = names;
109 while ((res = *ptr++) != NULL) {
110 if (mod & 1) {
111 char c;
112 while ((c = *res++) != '\0')
113 *p++ = c;
114 *p++ = ' ';
116 mod >>= 1;
118 *p = 0;
119 return buffer;
122 static void show_struct_member(struct symbol *sym)
124 printf("\t%s:%d:%ld at offset %ld.%d", show_ident(sym->ident), sym->bit_size, sym->ctype.alignment, sym->offset, sym->bit_offset);
125 printf("\n");
128 void show_symbol_list(struct symbol_list *list, const char *sep)
130 struct symbol *sym;
131 const char *prepend = "";
133 FOR_EACH_PTR(list, sym) {
134 puts(prepend);
135 prepend = ", ";
136 show_symbol(sym);
137 } END_FOR_EACH_PTR(sym);
140 struct type_name {
141 char *start;
142 char *end;
145 static void prepend(struct type_name *name, const char *fmt, ...)
147 static char buffer[512];
148 int n;
150 va_list args;
151 va_start(args, fmt);
152 n = vsprintf(buffer, fmt, args);
153 va_end(args);
155 name->start -= n;
156 memcpy(name->start, buffer, n);
159 static void append(struct type_name *name, const char *fmt, ...)
161 static char buffer[512];
162 int n;
164 va_list args;
165 va_start(args, fmt);
166 n = vsprintf(buffer, fmt, args);
167 va_end(args);
169 memcpy(name->end, buffer, n);
170 name->end += n;
173 static void do_show_type(struct symbol *sym, struct type_name *name)
175 int i, modlen;
176 const char *mod;
177 static struct ctype_name {
178 struct symbol *sym;
179 const char *name;
180 } typenames[] = {
181 { & char_ctype, "char" },
182 { &schar_ctype, "signed char" },
183 { &uchar_ctype, "unsigned char" },
184 { & short_ctype, "short" },
185 { &sshort_ctype, "signed short" },
186 { &ushort_ctype, "unsigned short" },
187 { & int_ctype, "int" },
188 { &sint_ctype, "signed int" },
189 { &uint_ctype, "unsigned int" },
190 { &slong_ctype, "signed long" },
191 { & long_ctype, "long" },
192 { &ulong_ctype, "unsigned long" },
193 { & llong_ctype, "long long" },
194 { &sllong_ctype, "signed long long" },
195 { &ullong_ctype, "unsigned long long" },
197 { &void_ctype, "void" },
198 { &bool_ctype, "bool" },
199 { &string_ctype, "string" },
201 { &float_ctype, "float" },
202 { &double_ctype, "double" },
203 { &ldouble_ctype,"long double" },
204 { &incomplete_ctype, "incomplete type" },
205 { &label_ctype, "label type" },
206 { &bad_ctype, "bad type" },
209 if (!sym)
210 return;
212 for (i = 0; i < sizeof(typenames)/sizeof(typenames[0]); i++) {
213 if (typenames[i].sym == sym) {
214 int len = strlen(typenames[i].name);
215 *--name->start = ' ';
216 name->start -= len;
217 memcpy(name->start, typenames[i].name, len);
218 return;
222 /* Prepend */
223 switch (sym->type) {
224 case SYM_PTR:
225 prepend(name, "*");
226 break;
227 case SYM_FN:
228 prepend(name, "( ");
229 break;
230 case SYM_STRUCT:
231 prepend(name, "struct %s ", show_ident(sym->ident));
232 return;
234 case SYM_UNION:
235 prepend(name, "union %s ", show_ident(sym->ident));
236 return;
238 case SYM_ENUM:
239 prepend(name, "enum %s ", show_ident(sym->ident));
240 break;
242 case SYM_NODE:
243 append(name, "%s", show_ident(sym->ident));
244 break;
246 case SYM_BITFIELD:
247 append(name, ":%d", sym->bit_size);
248 break;
250 case SYM_LABEL:
251 append(name, "label(%s:%p)", show_ident(sym->ident), sym);
252 break;
254 case SYM_ARRAY:
255 break;
257 case SYM_RESTRICT:
258 break;
260 default:
261 prepend(name, "unknown type %d", sym->type);
262 return;
265 mod = modifier_string(sym->ctype.modifiers);
266 modlen = strlen(mod);
267 name->start -= modlen;
268 memcpy(name->start, mod, modlen);
270 do_show_type(sym->ctype.base_type, name);
272 /* Postpend */
273 if (sym->ctype.as)
274 append(name, "<asn:%d>", sym->ctype.as);
276 switch (sym->type) {
277 case SYM_PTR:
278 return;
280 case SYM_FN:
281 append(name, " )( ... )");
282 return;
284 case SYM_ARRAY:
285 append(name, "[%lld]", get_expression_value(sym->array_size));
286 return;
288 case SYM_RESTRICT:
289 prepend(name, "restricted ");
290 return;
292 default:
293 break;
297 void show_type(struct symbol *sym)
299 char array[200];
300 struct type_name name;
302 name.start = name.end = array+100;
303 do_show_type(sym, &name);
304 *name.end = 0;
305 printf("%s", name.start);
308 const char *show_typename(struct symbol *sym)
310 static char array[200];
311 struct type_name name;
313 name.start = name.end = array+100;
314 do_show_type(sym, &name);
315 *name.end = 0;
316 return name.start;
319 void show_symbol(struct symbol *sym)
321 struct symbol *type;
323 if (!sym)
324 return;
326 if (sym->ctype.alignment)
327 printf(".align %ld\n", sym->ctype.alignment);
329 show_type(sym);
330 type = sym->ctype.base_type;
331 if (!type)
332 return;
335 * Show actual implementation information
337 switch (type->type) {
338 struct symbol *member;
340 case SYM_STRUCT:
341 case SYM_UNION:
342 printf(" {\n");
343 FOR_EACH_PTR(type->symbol_list, member) {
344 show_struct_member(member);
345 } END_FOR_EACH_PTR(member);
346 printf("}\n");
347 break;
349 case SYM_FN: {
350 struct statement *stmt = type->stmt;
351 if (stmt) {
352 int val;
353 printf("\n");
354 val = show_statement(stmt);
355 if (val)
356 printf("\tmov.%d\t\tretval,%d\n", stmt->ret->bit_size, val);
357 printf("\tret\n");
359 break;
362 default:
363 break;
366 if (sym->initializer) {
367 printf(" = \n");
368 show_expression(sym->initializer);
372 static int show_symbol_init(struct symbol *sym);
374 static int new_pseudo(void)
376 static int nr = 0;
377 return ++nr;
380 static int new_label(void)
382 static int label = 0;
383 return ++label;
386 static void show_switch_statement(struct statement *stmt)
388 int val = show_expression(stmt->switch_expression);
389 struct symbol *sym;
390 printf("\tswitch v%d\n", val);
393 * Debugging only: Check that the case list is correct
394 * by printing it out.
396 * This is where a _real_ back-end would go through the
397 * cases to decide whether to use a lookup table or a
398 * series of comparisons etc
400 printf("# case table:\n");
401 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
402 struct statement *case_stmt = sym->stmt;
403 struct expression *expr = case_stmt->case_expression;
404 struct expression *to = case_stmt->case_to;
406 if (!expr) {
407 printf(" default");
408 } else {
409 if (expr->type == EXPR_VALUE) {
410 printf(" case %lld", expr->value);
411 if (to) {
412 if (to->type == EXPR_VALUE) {
413 printf(" .. %lld", to->value);
414 } else {
415 printf(" .. what?");
418 } else
419 printf(" what?");
421 printf(": .L%p\n", sym->bb_target);
422 } END_FOR_EACH_PTR(sym);
423 printf("# end case table\n");
425 show_statement(stmt->switch_statement);
427 if (stmt->switch_break->used)
428 printf(".L%p:\n", stmt->switch_break->bb_target);
431 static void show_symbol_decl(struct symbol_list *syms)
433 struct symbol *sym;
434 FOR_EACH_PTR(syms, sym) {
435 show_symbol_init(sym);
436 } END_FOR_EACH_PTR(sym);
439 static int show_return_stmt(struct statement *stmt);
442 * Print out a statement
444 int show_statement(struct statement *stmt)
446 if (!stmt)
447 return 0;
448 switch (stmt->type) {
449 case STMT_DECLARATION:
450 show_symbol_decl(stmt->declaration);
451 return 0;
452 case STMT_RETURN:
453 return show_return_stmt(stmt);
454 case STMT_COMPOUND: {
455 struct statement *s;
456 int last = 0;
458 FOR_EACH_PTR(stmt->stmts, s) {
459 last = show_statement(s);
460 } END_FOR_EACH_PTR(s);
461 if (stmt->ret) {
462 int addr, bits;
463 printf(".L%p:\n", stmt->ret);
464 addr = show_symbol_expr(stmt->ret);
465 bits = stmt->ret->bit_size;
466 last = new_pseudo();
467 printf("\tld.%d\t\tv%d,[v%d]\n", bits, last, addr);
469 return last;
472 case STMT_EXPRESSION:
473 return show_expression(stmt->expression);
474 case STMT_IF: {
475 int val, target;
476 struct expression *cond = stmt->if_conditional;
478 /* This is only valid if nobody can jump into the "dead" statement */
479 #if 0
480 if (cond->type == EXPR_VALUE) {
481 struct statement *s = stmt->if_true;
482 if (!cond->value)
483 s = stmt->if_false;
484 show_statement(s);
485 break;
487 #endif
488 val = show_expression(cond);
489 target = new_label();
490 printf("\tje\t\tv%d,.L%d\n", val, target);
491 show_statement(stmt->if_true);
492 if (stmt->if_false) {
493 int last = new_label();
494 printf("\tjmp\t\t.L%d\n", last);
495 printf(".L%d:\n", target);
496 target = last;
497 show_statement(stmt->if_false);
499 printf(".L%d:\n", target);
500 break;
502 case STMT_SWITCH:
503 show_switch_statement(stmt);
504 break;
506 case STMT_CASE:
507 printf(".L%p:\n", stmt->case_label);
508 show_statement(stmt->case_statement);
509 break;
511 case STMT_ITERATOR: {
512 struct statement *pre_statement = stmt->iterator_pre_statement;
513 struct expression *pre_condition = stmt->iterator_pre_condition;
514 struct statement *statement = stmt->iterator_statement;
515 struct statement *post_statement = stmt->iterator_post_statement;
516 struct expression *post_condition = stmt->iterator_post_condition;
517 int val, loop_top = 0, loop_bottom = 0;
519 show_symbol_decl(stmt->iterator_syms);
520 show_statement(pre_statement);
521 if (pre_condition) {
522 if (pre_condition->type == EXPR_VALUE) {
523 if (!pre_condition->value) {
524 loop_bottom = new_label();
525 printf("\tjmp\t\t.L%d\n", loop_bottom);
527 } else {
528 loop_bottom = new_label();
529 val = show_expression(pre_condition);
530 printf("\tje\t\tv%d, .L%d\n", val, loop_bottom);
533 if (!post_condition || post_condition->type != EXPR_VALUE || post_condition->value) {
534 loop_top = new_label();
535 printf(".L%d:\n", loop_top);
537 show_statement(statement);
538 if (stmt->iterator_continue->used)
539 printf(".L%p:\n", stmt->iterator_continue);
540 show_statement(post_statement);
541 if (!post_condition) {
542 printf("\tjmp\t\t.L%d\n", loop_top);
543 } else if (post_condition->type == EXPR_VALUE) {
544 if (post_condition->value)
545 printf("\tjmp\t\t.L%d\n", loop_top);
546 } else {
547 val = show_expression(post_condition);
548 printf("\tjne\t\tv%d, .L%d\n", val, loop_top);
550 if (stmt->iterator_break->used)
551 printf(".L%p:\n", stmt->iterator_break);
552 if (loop_bottom)
553 printf(".L%d:\n", loop_bottom);
554 break;
556 case STMT_NONE:
557 break;
559 case STMT_LABEL:
560 printf(".L%p:\n", stmt->label_identifier);
561 show_statement(stmt->label_statement);
562 break;
564 case STMT_GOTO:
565 if (stmt->goto_expression) {
566 int val = show_expression(stmt->goto_expression);
567 printf("\tgoto\t\t*v%d\n", val);
568 } else {
569 printf("\tgoto\t\t.L%p\n", stmt->goto_label->bb_target);
571 break;
572 case STMT_ASM:
573 printf("\tasm( .... )\n");
574 break;
575 case STMT_CONTEXT: {
576 int val = show_expression(stmt->expression);
577 printf("\tcontext( %d )\n", val);
578 break;
580 case STMT_RANGE: {
581 int val = show_expression(stmt->range_expression);
582 int low = show_expression(stmt->range_low);
583 int high = show_expression(stmt->range_high);
584 printf("\trange( %d %d-%d)\n", val, low, high);
585 break;
588 return 0;
591 static int show_call_expression(struct expression *expr)
593 struct symbol *direct;
594 struct expression *arg, *fn;
595 int fncall, retval;
596 int framesize;
598 if (!expr->ctype) {
599 warning(expr->pos, "\tcall with no type!");
600 return 0;
603 framesize = 0;
604 FOR_EACH_PTR_REVERSE(expr->args, arg) {
605 int new = show_expression(arg);
606 int size = arg->ctype->bit_size;
607 printf("\tpush.%d\t\tv%d\n", size, new);
608 framesize += size >> 3;
609 } END_FOR_EACH_PTR_REVERSE(arg);
611 fn = expr->fn;
613 /* Remove dereference, if any */
614 direct = NULL;
615 if (fn->type == EXPR_PREOP) {
616 if (fn->unop->type == EXPR_SYMBOL) {
617 struct symbol *sym = fn->unop->symbol;
618 if (sym->ctype.base_type->type == SYM_FN)
619 direct = sym;
622 if (direct) {
623 printf("\tcall\t\t%s\n", show_ident(direct->ident));
624 } else {
625 fncall = show_expression(fn);
626 printf("\tcall\t\t*v%d\n", fncall);
628 if (framesize)
629 printf("\tadd.%d\t\tvSP,vSP,$%d\n", bits_in_pointer, framesize);
631 retval = new_pseudo();
632 printf("\tmov.%d\t\tv%d,retval\n", expr->ctype->bit_size, retval);
633 return retval;
636 static int show_comma(struct expression *expr)
638 show_expression(expr->left);
639 return show_expression(expr->right);
642 static int show_binop(struct expression *expr)
644 int left = show_expression(expr->left);
645 int right = show_expression(expr->right);
646 int new = new_pseudo();
647 const char *opname;
648 static const char *name[] = {
649 ['+'] = "add", ['-'] = "sub",
650 ['*'] = "mul", ['/'] = "div",
651 ['%'] = "mod", ['&'] = "and",
652 ['|'] = "lor", ['^'] = "xor"
654 unsigned int op = expr->op;
656 opname = show_special(op);
657 if (op < sizeof(name)/sizeof(*name))
658 opname = name[op];
659 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname,
660 expr->ctype->bit_size,
661 new, left, right);
662 return new;
665 static int show_slice(struct expression *expr)
667 int target = show_expression(expr->base);
668 int new = new_pseudo();
669 printf("\tslice.%d\t\tv%d,v%d,%d\n", expr->r_nrbits, target, new, expr->r_bitpos);
670 return new;
673 static int show_regular_preop(struct expression *expr)
675 int target = show_expression(expr->unop);
676 int new = new_pseudo();
677 static const char *name[] = {
678 ['!'] = "nonzero", ['-'] = "neg",
679 ['~'] = "not",
681 unsigned int op = expr->op;
682 const char *opname;
684 opname = show_special(op);
685 if (op < sizeof(name)/sizeof(*name))
686 opname = name[op];
687 printf("\t%s.%d\t\tv%d,v%d\n", opname, expr->ctype->bit_size, new, target);
688 return new;
692 * FIXME! Not all accesses are memory loads. We should
693 * check what kind of symbol is behind the dereference.
695 static int show_address_gen(struct expression *expr)
697 return show_expression(expr->unop);
700 static int show_load_gen(int bits, struct expression *expr, int addr)
702 int new = new_pseudo();
704 printf("\tld.%d\t\tv%d,[v%d]\n", bits, new, addr);
705 return new;
708 static void show_store_gen(int bits, int value, struct expression *expr, int addr)
710 /* FIXME!!! Bitfield store! */
711 printf("\tst.%d\t\tv%d,[v%d]\n", bits, value, addr);
714 static int show_assignment(struct expression *expr)
716 struct expression *target = expr->left;
717 int val, addr, bits;
719 if (!expr->ctype)
720 return 0;
722 bits = expr->ctype->bit_size;
723 val = show_expression(expr->right);
724 addr = show_address_gen(target);
725 show_store_gen(bits, val, target, addr);
726 return val;
729 static int show_return_stmt(struct statement *stmt)
731 struct expression *expr = stmt->ret_value;
732 struct symbol *target = stmt->ret_target;
734 if (expr && expr->ctype) {
735 int val = show_expression(expr);
736 int bits = expr->ctype->bit_size;
737 int addr = show_symbol_expr(target);
738 show_store_gen(bits, val, NULL, addr);
740 printf("\tret\t\t(%p)\n", target);
741 return 0;
744 static int show_initialization(struct symbol *sym, struct expression *expr)
746 int val, addr, bits;
748 if (!expr->ctype)
749 return 0;
751 bits = expr->ctype->bit_size;
752 val = show_expression(expr);
753 addr = show_symbol_expr(sym);
754 // FIXME! The "target" expression is for bitfield store information.
755 // Leave it NULL, which works fine.
756 show_store_gen(bits, val, NULL, addr);
757 return 0;
760 static int show_access(struct expression *expr)
762 int addr = show_address_gen(expr);
763 return show_load_gen(expr->ctype->bit_size, expr, addr);
766 static int show_inc_dec(struct expression *expr, int postop)
768 int addr = show_address_gen(expr->unop);
769 int retval, new;
770 const char *opname = expr->op == SPECIAL_INCREMENT ? "add" : "sub";
771 int bits = expr->ctype->bit_size;
773 retval = show_load_gen(bits, expr->unop, addr);
774 new = retval;
775 if (postop)
776 new = new_pseudo();
777 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname, bits, new, retval);
778 show_store_gen(bits, new, expr->unop, addr);
779 return retval;
782 static int show_preop(struct expression *expr)
785 * '*' is an lvalue access, and is fundamentally different
786 * from an arithmetic operation. Maybe it should have an
787 * expression type of its own..
789 if (expr->op == '*')
790 return show_access(expr);
791 if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
792 return show_inc_dec(expr, 0);
793 return show_regular_preop(expr);
796 static int show_postop(struct expression *expr)
798 return show_inc_dec(expr, 1);
801 static int show_symbol_expr(struct symbol *sym)
803 int new = new_pseudo();
805 if (sym->initializer && sym->initializer->type == EXPR_STRING)
806 return show_string_expr(sym->initializer);
808 if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC)) {
809 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer, new, show_ident(sym->ident));
810 return new;
812 if (sym->ctype.modifiers & MOD_ADDRESSABLE) {
813 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer, new, sym->value);
814 return new;
816 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer, new, show_ident(sym->ident), sym);
817 return new;
820 static int show_symbol_init(struct symbol *sym)
822 struct expression *expr = sym->initializer;
824 if (expr) {
825 int val, addr, bits;
827 bits = expr->ctype->bit_size;
828 val = show_expression(expr);
829 addr = show_symbol_expr(sym);
830 show_store_gen(bits, val, NULL, addr);
832 return 0;
835 static int type_is_signed(struct symbol *sym)
837 if (sym->type == SYM_NODE)
838 sym = sym->ctype.base_type;
839 if (sym->type == SYM_PTR)
840 return 0;
841 return !(sym->ctype.modifiers & MOD_UNSIGNED);
844 static int show_cast_expr(struct expression *expr)
846 struct symbol *old_type, *new_type;
847 int op = show_expression(expr->cast_expression);
848 int oldbits, newbits;
849 int new, is_signed;
851 old_type = expr->cast_expression->ctype;
852 new_type = expr->cast_type;
854 oldbits = old_type->bit_size;
855 newbits = new_type->bit_size;
856 if (oldbits >= newbits)
857 return op;
858 new = new_pseudo();
859 is_signed = type_is_signed(old_type);
860 if (is_signed) {
861 printf("\tsext%d.%d\tv%d,v%d\n", oldbits, newbits, new, op);
862 } else {
863 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits, new, op, (1UL << oldbits)-1);
865 return new;
868 static int show_value(struct expression *expr)
870 int new = new_pseudo();
871 unsigned long long value = expr->value;
873 printf("\tmovi.%d\t\tv%d,$%llu\n", expr->ctype->bit_size, new, value);
874 return new;
877 static int show_fvalue(struct expression *expr)
879 int new = new_pseudo();
880 long double value = expr->fvalue;
882 printf("\tmovf.%d\t\tv%d,$%Lf\n", expr->ctype->bit_size, new, value);
883 return new;
886 static int show_string_expr(struct expression *expr)
888 int new = new_pseudo();
890 printf("\tmovi.%d\t\tv%d,&%s\n", bits_in_pointer, new, show_string(expr->string));
891 return new;
894 static int show_label_expr(struct expression *expr)
896 int new = new_pseudo();
897 printf("\tmovi.%d\t\tv%d,.L%p\n",bits_in_pointer, new, expr->label_symbol);
898 return new;
901 static int show_conditional_expr(struct expression *expr)
903 int cond = show_expression(expr->conditional);
904 int true = show_expression(expr->cond_true);
905 int false = show_expression(expr->cond_false);
906 int new = new_pseudo();
908 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond, expr->ctype->bit_size, new, true, false);
909 return new;
912 static int show_statement_expr(struct expression *expr)
914 return show_statement(expr->statement);
917 static int show_position_expr(struct expression *expr, struct symbol *base)
919 int new = show_expression(expr->init_expr);
920 struct symbol *ctype = expr->init_expr->ctype;
921 int bit_offset;
923 bit_offset = ctype ? ctype->bit_offset : -1;
925 printf("\tinsert v%d at [%d:%d] of %s\n", new,
926 expr->init_offset, bit_offset,
927 show_ident(base->ident));
928 return 0;
931 static int show_initializer_expr(struct expression *expr, struct symbol *ctype)
933 struct expression *entry;
935 FOR_EACH_PTR(expr->expr_list, entry) {
937 again:
938 // Nested initializers have their positions already
939 // recursively calculated - just output them too
940 if (entry->type == EXPR_INITIALIZER) {
941 show_initializer_expr(entry, ctype);
942 continue;
945 // Initializer indexes and identifiers should
946 // have been evaluated to EXPR_POS
947 if (entry->type == EXPR_IDENTIFIER) {
948 printf(" AT '%s':\n", show_ident(entry->expr_ident));
949 entry = entry->ident_expression;
950 goto again;
953 if (entry->type == EXPR_INDEX) {
954 printf(" AT '%d..%d:\n", entry->idx_from, entry->idx_to);
955 entry = entry->idx_expression;
956 goto again;
958 if (entry->type == EXPR_POS) {
959 show_position_expr(entry, ctype);
960 continue;
962 show_initialization(ctype, entry);
963 } END_FOR_EACH_PTR(entry);
964 return 0;
967 int show_symbol_expr_init(struct symbol *sym)
969 struct expression *expr = sym->initializer;
971 if (expr)
972 show_expression(expr);
973 return show_symbol_expr(sym);
977 * Print out an expression. Return the pseudo that contains the
978 * variable.
980 int show_expression(struct expression *expr)
982 if (!expr)
983 return 0;
985 if (!expr->ctype) {
986 struct position *pos = &expr->pos;
987 printf("\tno type at %s:%d:%d\n",
988 stream_name(pos->stream),
989 pos->line, pos->pos);
990 return 0;
993 switch (expr->type) {
994 case EXPR_CALL:
995 return show_call_expression(expr);
997 case EXPR_ASSIGNMENT:
998 return show_assignment(expr);
1000 case EXPR_COMMA:
1001 return show_comma(expr);
1002 case EXPR_BINOP:
1003 case EXPR_COMPARE:
1004 case EXPR_LOGICAL:
1005 return show_binop(expr);
1006 case EXPR_PREOP:
1007 return show_preop(expr);
1008 case EXPR_POSTOP:
1009 return show_postop(expr);
1010 case EXPR_SYMBOL:
1011 return show_symbol_expr(expr->symbol);
1012 case EXPR_DEREF:
1013 case EXPR_SIZEOF:
1014 case EXPR_PTRSIZEOF:
1015 case EXPR_ALIGNOF:
1016 warning(expr->pos, "invalid expression after evaluation");
1017 return 0;
1018 case EXPR_CAST:
1019 case EXPR_IMPLIED_CAST:
1020 return show_cast_expr(expr);
1021 case EXPR_VALUE:
1022 return show_value(expr);
1023 case EXPR_FVALUE:
1024 return show_fvalue(expr);
1025 case EXPR_STRING:
1026 return show_string_expr(expr);
1027 case EXPR_INITIALIZER:
1028 return show_initializer_expr(expr, expr->ctype);
1029 case EXPR_SELECT:
1030 case EXPR_CONDITIONAL:
1031 return show_conditional_expr(expr);
1032 case EXPR_STATEMENT:
1033 return show_statement_expr(expr);
1034 case EXPR_LABEL:
1035 return show_label_expr(expr);
1036 case EXPR_SLICE:
1037 return show_slice(expr);
1039 // None of these should exist as direct expressions: they are only
1040 // valid as sub-expressions of initializers.
1041 case EXPR_POS:
1042 warning(expr->pos, "unable to show plain initializer position expression");
1043 return 0;
1044 case EXPR_IDENTIFIER:
1045 warning(expr->pos, "unable to show identifier expression");
1046 return 0;
1047 case EXPR_INDEX:
1048 warning(expr->pos, "unable to show index expression");
1049 return 0;
1050 case EXPR_TYPE:
1051 warning(expr->pos, "unable to show type expression");
1052 return 0;
1054 return 0;