[PATCH] fix of compound literals on inlining
[smatch.git] / show-parse.c
blob72c408b8aa2502c62a6e5af273ec9395da74c715
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 static 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_CONTEXT: {
565 int val = show_expression(stmt->expression);
566 printf("\tcontext( %d )\n", val);
567 break;
569 case STMT_RANGE: {
570 int val = show_expression(stmt->range_expression);
571 int low = show_expression(stmt->range_low);
572 int high = show_expression(stmt->range_high);
573 printf("\trange( %d %d-%d)\n", val, low, high);
574 break;
577 return 0;
580 static int show_call_expression(struct expression *expr)
582 struct symbol *direct;
583 struct expression *arg, *fn;
584 int fncall, retval;
585 int framesize;
587 if (!expr->ctype) {
588 warning(expr->pos, "\tcall with no type!");
589 return 0;
592 framesize = 0;
593 FOR_EACH_PTR_REVERSE(expr->args, arg) {
594 int new = show_expression(arg);
595 int size = arg->ctype->bit_size;
596 printf("\tpush.%d\t\tv%d\n", size, new);
597 framesize += size >> 3;
598 } END_FOR_EACH_PTR_REVERSE(arg);
600 fn = expr->fn;
602 /* Remove dereference, if any */
603 direct = NULL;
604 if (fn->type == EXPR_PREOP) {
605 if (fn->unop->type == EXPR_SYMBOL) {
606 struct symbol *sym = fn->unop->symbol;
607 if (sym->ctype.base_type->type == SYM_FN)
608 direct = sym;
611 if (direct) {
612 printf("\tcall\t\t%s\n", show_ident(direct->ident));
613 } else {
614 fncall = show_expression(fn);
615 printf("\tcall\t\t*v%d\n", fncall);
617 if (framesize)
618 printf("\tadd.%d\t\tvSP,vSP,$%d\n", bits_in_pointer, framesize);
620 retval = new_pseudo();
621 printf("\tmov.%d\t\tv%d,retval\n", expr->ctype->bit_size, retval);
622 return retval;
625 static int show_comma(struct expression *expr)
627 show_expression(expr->left);
628 return show_expression(expr->right);
631 static int show_binop(struct expression *expr)
633 int left = show_expression(expr->left);
634 int right = show_expression(expr->right);
635 int new = new_pseudo();
636 const char *opname;
637 static const char *name[] = {
638 ['+'] = "add", ['-'] = "sub",
639 ['*'] = "mul", ['/'] = "div",
640 ['%'] = "mod", ['&'] = "and",
641 ['|'] = "lor", ['^'] = "xor"
643 unsigned int op = expr->op;
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,v%d\n", opname,
649 expr->ctype->bit_size,
650 new, left, right);
651 return new;
654 static int show_slice(struct expression *expr)
656 int target = show_expression(expr->base);
657 int new = new_pseudo();
658 printf("\tslice.%d\t\tv%d,v%d,%d\n", expr->r_nrbits, target, new, expr->r_bitpos);
659 return new;
662 static int show_regular_preop(struct expression *expr)
664 int target = show_expression(expr->unop);
665 int new = new_pseudo();
666 static const char *name[] = {
667 ['!'] = "nonzero", ['-'] = "neg",
668 ['~'] = "not",
670 unsigned int op = expr->op;
671 const char *opname;
673 opname = show_special(op);
674 if (op < sizeof(name)/sizeof(*name))
675 opname = name[op];
676 printf("\t%s.%d\t\tv%d,v%d\n", opname, expr->ctype->bit_size, new, target);
677 return new;
681 * FIXME! Not all accesses are memory loads. We should
682 * check what kind of symbol is behind the dereference.
684 static int show_address_gen(struct expression *expr)
686 return show_expression(expr->unop);
689 static int show_load_gen(int bits, struct expression *expr, int addr)
691 int new = new_pseudo();
693 printf("\tld.%d\t\tv%d,[v%d]\n", bits, new, addr);
694 return new;
697 static void show_store_gen(int bits, int value, struct expression *expr, int addr)
699 /* FIXME!!! Bitfield store! */
700 printf("\tst.%d\t\tv%d,[v%d]\n", bits, value, addr);
703 static int show_assignment(struct expression *expr)
705 struct expression *target = expr->left;
706 int val, addr, bits;
708 if (!expr->ctype)
709 return 0;
711 bits = expr->ctype->bit_size;
712 val = show_expression(expr->right);
713 addr = show_address_gen(target);
714 show_store_gen(bits, val, target, addr);
715 return val;
718 static int show_return_stmt(struct statement *stmt)
720 struct expression *expr = stmt->ret_value;
721 struct symbol *target = stmt->ret_target;
723 if (expr && expr->ctype) {
724 int val = show_expression(expr);
725 int bits = expr->ctype->bit_size;
726 int addr = show_symbol_expr(target);
727 show_store_gen(bits, val, NULL, addr);
729 printf("\tret\t\t(%p)\n", target);
730 return 0;
733 static int show_initialization(struct symbol *sym, struct expression *expr)
735 int val, addr, bits;
737 if (!expr->ctype)
738 return 0;
740 bits = expr->ctype->bit_size;
741 val = show_expression(expr);
742 addr = show_symbol_expr(sym);
743 // FIXME! The "target" expression is for bitfield store information.
744 // Leave it NULL, which works fine.
745 show_store_gen(bits, val, NULL, addr);
746 return 0;
749 static int show_access(struct expression *expr)
751 int addr = show_address_gen(expr);
752 return show_load_gen(expr->ctype->bit_size, expr, addr);
755 static int show_inc_dec(struct expression *expr, int postop)
757 int addr = show_address_gen(expr->unop);
758 int retval, new;
759 const char *opname = expr->op == SPECIAL_INCREMENT ? "add" : "sub";
760 int bits = expr->ctype->bit_size;
762 retval = show_load_gen(bits, expr->unop, addr);
763 new = retval;
764 if (postop)
765 new = new_pseudo();
766 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname, bits, new, retval);
767 show_store_gen(bits, new, expr->unop, addr);
768 return retval;
771 static int show_preop(struct expression *expr)
774 * '*' is an lvalue access, and is fundamentally different
775 * from an arithmetic operation. Maybe it should have an
776 * expression type of its own..
778 if (expr->op == '*')
779 return show_access(expr);
780 if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
781 return show_inc_dec(expr, 0);
782 return show_regular_preop(expr);
785 static int show_postop(struct expression *expr)
787 return show_inc_dec(expr, 1);
790 static int show_symbol_expr(struct symbol *sym)
792 int new = new_pseudo();
794 if (sym->initializer && sym->initializer->type == EXPR_STRING)
795 return show_string_expr(sym->initializer);
797 if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC)) {
798 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer, new, show_ident(sym->ident));
799 return new;
801 if (sym->ctype.modifiers & MOD_ADDRESSABLE) {
802 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer, new, sym->value);
803 return new;
805 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer, new, show_ident(sym->ident), sym);
806 return new;
809 static int show_symbol_init(struct symbol *sym)
811 struct expression *expr = sym->initializer;
813 if (expr) {
814 int val, addr, bits;
816 bits = expr->ctype->bit_size;
817 val = show_expression(expr);
818 addr = show_symbol_expr(sym);
819 show_store_gen(bits, val, NULL, addr);
821 return 0;
824 static int type_is_signed(struct symbol *sym)
826 if (sym->type == SYM_NODE)
827 sym = sym->ctype.base_type;
828 if (sym->type == SYM_PTR)
829 return 0;
830 return !(sym->ctype.modifiers & MOD_UNSIGNED);
833 static int show_cast_expr(struct expression *expr)
835 struct symbol *old_type, *new_type;
836 int op = show_expression(expr->cast_expression);
837 int oldbits, newbits;
838 int new, is_signed;
840 old_type = expr->cast_expression->ctype;
841 new_type = expr->cast_type;
843 oldbits = old_type->bit_size;
844 newbits = new_type->bit_size;
845 if (oldbits >= newbits)
846 return op;
847 new = new_pseudo();
848 is_signed = type_is_signed(old_type);
849 if (is_signed) {
850 printf("\tsext%d.%d\tv%d,v%d\n", oldbits, newbits, new, op);
851 } else {
852 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits, new, op, (1UL << oldbits)-1);
854 return new;
857 static int show_value(struct expression *expr)
859 int new = new_pseudo();
860 unsigned long long value = expr->value;
862 printf("\tmovi.%d\t\tv%d,$%llu\n", expr->ctype->bit_size, new, value);
863 return new;
866 static int show_fvalue(struct expression *expr)
868 int new = new_pseudo();
869 long double value = expr->fvalue;
871 printf("\tmovf.%d\t\tv%d,$%Lf\n", expr->ctype->bit_size, new, value);
872 return new;
875 static int show_string_expr(struct expression *expr)
877 int new = new_pseudo();
879 printf("\tmovi.%d\t\tv%d,&%s\n", bits_in_pointer, new, show_string(expr->string));
880 return new;
883 static int show_label_expr(struct expression *expr)
885 int new = new_pseudo();
886 printf("\tmovi.%d\t\tv%d,.L%p\n",bits_in_pointer, new, expr->label_symbol);
887 return new;
890 static int show_conditional_expr(struct expression *expr)
892 int cond = show_expression(expr->conditional);
893 int true = show_expression(expr->cond_true);
894 int false = show_expression(expr->cond_false);
895 int new = new_pseudo();
897 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond, expr->ctype->bit_size, new, true, false);
898 return new;
901 static int show_statement_expr(struct expression *expr)
903 return show_statement(expr->statement);
906 static int show_position_expr(struct expression *expr, struct symbol *base)
908 int new = show_expression(expr->init_expr);
909 struct symbol *ctype = expr->init_expr->ctype;
910 int bit_offset;
912 bit_offset = ctype ? ctype->bit_offset : -1;
914 printf("\tinsert v%d at [%d:%d] of %s\n", new,
915 expr->init_offset, bit_offset,
916 show_ident(base->ident));
917 return 0;
920 static int show_initializer_expr(struct expression *expr, struct symbol *ctype)
922 struct expression *entry;
924 FOR_EACH_PTR(expr->expr_list, entry) {
926 again:
927 // Nested initializers have their positions already
928 // recursively calculated - just output them too
929 if (entry->type == EXPR_INITIALIZER) {
930 show_initializer_expr(entry, ctype);
931 continue;
934 // Initializer indexes and identifiers should
935 // have been evaluated to EXPR_POS
936 if (entry->type == EXPR_IDENTIFIER) {
937 printf(" AT '%s':\n", show_ident(entry->expr_ident));
938 entry = entry->ident_expression;
939 goto again;
942 if (entry->type == EXPR_INDEX) {
943 printf(" AT '%d..%d:\n", entry->idx_from, entry->idx_to);
944 entry = entry->idx_expression;
945 goto again;
947 if (entry->type == EXPR_POS) {
948 show_position_expr(entry, ctype);
949 continue;
951 show_initialization(ctype, entry);
952 } END_FOR_EACH_PTR(entry);
953 return 0;
956 int show_symbol_expr_init(struct symbol *sym)
958 struct expression *expr = sym->initializer;
960 if (expr)
961 show_expression(expr);
962 return show_symbol_expr(sym);
966 * Print out an expression. Return the pseudo that contains the
967 * variable.
969 int show_expression(struct expression *expr)
971 if (!expr)
972 return 0;
974 if (!expr->ctype) {
975 struct position *pos = &expr->pos;
976 printf("\tno type at %s:%d:%d\n",
977 stream_name(pos->stream),
978 pos->line, pos->pos);
979 return 0;
982 switch (expr->type) {
983 case EXPR_CALL:
984 return show_call_expression(expr);
986 case EXPR_ASSIGNMENT:
987 return show_assignment(expr);
989 case EXPR_COMMA:
990 return show_comma(expr);
991 case EXPR_BINOP:
992 case EXPR_COMPARE:
993 case EXPR_LOGICAL:
994 return show_binop(expr);
995 case EXPR_PREOP:
996 return show_preop(expr);
997 case EXPR_POSTOP:
998 return show_postop(expr);
999 case EXPR_SYMBOL:
1000 return show_symbol_expr(expr->symbol);
1001 case EXPR_DEREF:
1002 case EXPR_SIZEOF:
1003 case EXPR_PTRSIZEOF:
1004 case EXPR_ALIGNOF:
1005 warning(expr->pos, "invalid expression after evaluation");
1006 return 0;
1007 case EXPR_CAST:
1008 case EXPR_IMPLIED_CAST:
1009 return show_cast_expr(expr);
1010 case EXPR_VALUE:
1011 return show_value(expr);
1012 case EXPR_FVALUE:
1013 return show_fvalue(expr);
1014 case EXPR_STRING:
1015 return show_string_expr(expr);
1016 case EXPR_INITIALIZER:
1017 return show_initializer_expr(expr, expr->ctype);
1018 case EXPR_SELECT:
1019 case EXPR_CONDITIONAL:
1020 return show_conditional_expr(expr);
1021 case EXPR_STATEMENT:
1022 return show_statement_expr(expr);
1023 case EXPR_LABEL:
1024 return show_label_expr(expr);
1025 case EXPR_SLICE:
1026 return show_slice(expr);
1028 // None of these should exist as direct expressions: they are only
1029 // valid as sub-expressions of initializers.
1030 case EXPR_POS:
1031 warning(expr->pos, "unable to show plain initializer position expression");
1032 return 0;
1033 case EXPR_IDENTIFIER:
1034 warning(expr->pos, "unable to show identifier expression");
1035 return 0;
1036 case EXPR_INDEX:
1037 warning(expr->pos, "unable to show index expression");
1038 return 0;
1039 case EXPR_TYPE:
1040 warning(expr->pos, "unable to show type expression");
1041 return 0;
1043 return 0;