Add comment. get_value returns UNDEFINED on error.
[smatch.git] / show-parse.c
blob064af32728017f10541a9aa2389e4e1f48fa172b
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_FOULED] = "foul",
52 [SYM_BAD] = "bad.",
54 struct context *context;
55 int i;
57 if (!sym)
58 return;
59 fprintf(stderr, "%.*s%s%3d:%lu %s %s (as: %d) %p (%s:%d:%d) %s\n",
60 indent, indent_string, typestr[sym->type],
61 sym->bit_size, sym->ctype.alignment,
62 modifier_string(sym->ctype.modifiers), show_ident(sym->ident), sym->ctype.as,
63 sym, stream_name(sym->pos.stream), sym->pos.line, sym->pos.pos,
64 builtin_typename(sym) ?: "");
65 i = 0;
66 FOR_EACH_PTR(sym->ctype.contexts, context) {
67 /* FIXME: should print context expression */
68 fprintf(stderr, "< context%d: in=%d, out=%d\n",
69 i, context->in, context->out);
70 fprintf(stderr, " end context%d >\n", i);
71 i++;
72 } END_FOR_EACH_PTR(context);
73 if (sym->type == SYM_FN) {
74 struct symbol *arg;
75 i = 0;
76 FOR_EACH_PTR(sym->arguments, arg) {
77 fprintf(stderr, "< arg%d:\n", i);
78 do_debug_symbol(arg, 0);
79 fprintf(stderr, " end arg%d >\n", i);
80 i++;
81 } END_FOR_EACH_PTR(arg);
83 do_debug_symbol(sym->ctype.base_type, indent+2);
86 void debug_symbol(struct symbol *sym)
88 do_debug_symbol(sym, 0);
92 * Symbol type printout. The type system is by far the most
93 * complicated part of C - everything else is trivial.
95 const char *modifier_string(unsigned long mod)
97 static char buffer[100];
98 char *p = buffer;
99 const char *res,**ptr, *names[] = {
100 "auto", "register", "static", "extern",
101 "const", "volatile", "[signed]", "[unsigned]",
102 "[char]", "[short]", "[long]", "[long long]",
103 "[typdef]", "[structof]", "[unionof]", "[enum]",
104 "[typeof]", "[attribute]", "inline", "[addressable]",
105 "[nocast]", "[noderef]", "[accessed]", "[toplevel]",
106 "[label]", "[assigned]", "[type]", "[safe]",
107 "[usertype]", "[force]", "[explicitly-signed]",
108 NULL
110 ptr = names;
111 while ((res = *ptr++) != NULL) {
112 if (mod & 1) {
113 char c;
114 while ((c = *res++) != '\0')
115 *p++ = c;
116 *p++ = ' ';
118 mod >>= 1;
120 *p = 0;
121 return buffer;
124 static void show_struct_member(struct symbol *sym)
126 printf("\t%s:%d:%ld at offset %ld.%d", show_ident(sym->ident), sym->bit_size, sym->ctype.alignment, sym->offset, sym->bit_offset);
127 printf("\n");
130 void show_symbol_list(struct symbol_list *list, const char *sep)
132 struct symbol *sym;
133 const char *prepend = "";
135 FOR_EACH_PTR(list, sym) {
136 puts(prepend);
137 prepend = ", ";
138 show_symbol(sym);
139 } END_FOR_EACH_PTR(sym);
142 struct type_name {
143 char *start;
144 char *end;
147 static void FORMAT_ATTR(2) prepend(struct type_name *name, const char *fmt, ...)
149 static char buffer[512];
150 int n;
152 va_list args;
153 va_start(args, fmt);
154 n = vsprintf(buffer, fmt, args);
155 va_end(args);
157 name->start -= n;
158 memcpy(name->start, buffer, n);
161 static void FORMAT_ATTR(2) append(struct type_name *name, const char *fmt, ...)
163 static char buffer[512];
164 int n;
166 va_list args;
167 va_start(args, fmt);
168 n = vsprintf(buffer, fmt, args);
169 va_end(args);
171 memcpy(name->end, buffer, n);
172 name->end += n;
175 static struct ctype_name {
176 struct symbol *sym;
177 const char *name;
178 } typenames[] = {
179 { & char_ctype, "char" },
180 { &schar_ctype, "signed char" },
181 { &uchar_ctype, "unsigned char" },
182 { & short_ctype, "short" },
183 { &sshort_ctype, "signed short" },
184 { &ushort_ctype, "unsigned short" },
185 { & int_ctype, "int" },
186 { &sint_ctype, "signed int" },
187 { &uint_ctype, "unsigned int" },
188 { &slong_ctype, "signed long" },
189 { & long_ctype, "long" },
190 { &ulong_ctype, "unsigned long" },
191 { & llong_ctype, "long long" },
192 { &sllong_ctype, "signed long long" },
193 { &ullong_ctype, "unsigned long long" },
195 { &void_ctype, "void" },
196 { &bool_ctype, "bool" },
197 { &string_ctype, "string" },
199 { &float_ctype, "float" },
200 { &double_ctype, "double" },
201 { &ldouble_ctype,"long double" },
202 { &incomplete_ctype, "incomplete type" },
203 { &int_type, "abstract int" },
204 { &fp_type, "abstract fp" },
205 { &label_ctype, "label type" },
206 { &bad_ctype, "bad type" },
209 const char *builtin_typename(struct symbol *sym)
211 int i;
213 for (i = 0; i < sizeof(typenames)/sizeof(typenames[0]); i++)
214 if (typenames[i].sym == sym)
215 return typenames[i].name;
216 return NULL;
219 const char *builtin_ctypename(struct ctype *ctype)
221 int i;
223 for (i = 0; i < sizeof(typenames)/sizeof(typenames[0]); i++)
224 if (&typenames[i].sym->ctype == ctype)
225 return typenames[i].name;
226 return NULL;
229 static void do_show_type(struct symbol *sym, struct type_name *name)
231 const char *typename;
232 unsigned long mod = 0;
233 int as = 0;
234 int was_ptr = 0;
235 int restr = 0;
236 int fouled = 0;
238 deeper:
239 if (!sym || (sym->type != SYM_NODE && sym->type != SYM_ARRAY &&
240 sym->type != SYM_BITFIELD)) {
241 const char *s;
242 size_t len;
244 if (as)
245 prepend(name, "<asn:%d>", as);
247 s = modifier_string(mod);
248 len = strlen(s);
249 name->start -= len;
250 memcpy(name->start, s, len);
251 mod = 0;
252 as = 0;
255 if (!sym)
256 goto out;
258 if ((typename = builtin_typename(sym))) {
259 int len = strlen(typename);
260 if (name->start != name->end)
261 *--name->start = ' ';
262 name->start -= len;
263 memcpy(name->start, typename, len);
264 goto out;
267 /* Prepend */
268 switch (sym->type) {
269 case SYM_PTR:
270 prepend(name, "*");
271 mod = sym->ctype.modifiers;
272 as = sym->ctype.as;
273 was_ptr = 1;
274 break;
276 case SYM_FN:
277 if (was_ptr) {
278 prepend(name, "( ");
279 append(name, " )");
280 was_ptr = 0;
282 append(name, "( ... )");
283 break;
285 case SYM_STRUCT:
286 if (name->start != name->end)
287 *--name->start = ' ';
288 prepend(name, "struct %s", show_ident(sym->ident));
289 goto out;
291 case SYM_UNION:
292 if (name->start != name->end)
293 *--name->start = ' ';
294 prepend(name, "union %s", show_ident(sym->ident));
295 goto out;
297 case SYM_ENUM:
298 prepend(name, "enum %s ", show_ident(sym->ident));
299 break;
301 case SYM_NODE:
302 append(name, "%s", show_ident(sym->ident));
303 mod |= sym->ctype.modifiers;
304 as |= sym->ctype.as;
305 break;
307 case SYM_BITFIELD:
308 mod |= sym->ctype.modifiers;
309 as |= sym->ctype.as;
310 append(name, ":%d", sym->bit_size);
311 break;
313 case SYM_LABEL:
314 append(name, "label(%s:%p)", show_ident(sym->ident), sym);
315 return;
317 case SYM_ARRAY:
318 mod |= sym->ctype.modifiers;
319 as |= sym->ctype.as;
320 if (was_ptr) {
321 prepend(name, "( ");
322 append(name, " )");
323 was_ptr = 0;
325 append(name, "[%lld]", get_expression_value(sym->array_size));
326 break;
328 case SYM_RESTRICT:
329 if (!sym->ident) {
330 restr = 1;
331 break;
333 if (name->start != name->end)
334 *--name->start = ' ';
335 prepend(name, "restricted %s", show_ident(sym->ident));
336 goto out;
338 case SYM_FOULED:
339 fouled = 1;
340 break;
342 default:
343 if (name->start != name->end)
344 *--name->start = ' ';
345 prepend(name, "unknown type %d", sym->type);
346 goto out;
349 sym = sym->ctype.base_type;
350 goto deeper;
352 out:
353 if (restr)
354 prepend(name, "restricted ");
355 if (fouled)
356 prepend(name, "fouled ");
359 void show_type(struct symbol *sym)
361 char array[200];
362 struct type_name name;
364 name.start = name.end = array+100;
365 do_show_type(sym, &name);
366 *name.end = 0;
367 printf("%s", name.start);
370 const char *show_typename(struct symbol *sym)
372 static char array[200];
373 struct type_name name;
375 name.start = name.end = array+100;
376 do_show_type(sym, &name);
377 *name.end = 0;
378 return name.start;
381 void show_symbol(struct symbol *sym)
383 struct symbol *type;
385 if (!sym)
386 return;
388 if (sym->ctype.alignment)
389 printf(".align %ld\n", sym->ctype.alignment);
391 show_type(sym);
392 type = sym->ctype.base_type;
393 if (!type)
394 return;
397 * Show actual implementation information
399 switch (type->type) {
400 struct symbol *member;
402 case SYM_STRUCT:
403 case SYM_UNION:
404 printf(" {\n");
405 FOR_EACH_PTR(type->symbol_list, member) {
406 show_struct_member(member);
407 } END_FOR_EACH_PTR(member);
408 printf("}\n");
409 break;
411 case SYM_FN: {
412 struct statement *stmt = type->stmt;
413 if (stmt) {
414 int val;
415 printf("\n");
416 val = show_statement(stmt);
417 if (val)
418 printf("\tmov.%d\t\tretval,%d\n", stmt->ret->bit_size, val);
419 printf("\tret\n");
421 break;
424 default:
425 break;
428 if (sym->initializer) {
429 printf(" = \n");
430 show_expression(sym->initializer);
434 static int show_symbol_init(struct symbol *sym);
436 static int new_pseudo(void)
438 static int nr = 0;
439 return ++nr;
442 static int new_label(void)
444 static int label = 0;
445 return ++label;
448 static void show_switch_statement(struct statement *stmt)
450 int val = show_expression(stmt->switch_expression);
451 struct symbol *sym;
452 printf("\tswitch v%d\n", val);
455 * Debugging only: Check that the case list is correct
456 * by printing it out.
458 * This is where a _real_ back-end would go through the
459 * cases to decide whether to use a lookup table or a
460 * series of comparisons etc
462 printf("# case table:\n");
463 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
464 struct statement *case_stmt = sym->stmt;
465 struct expression *expr = case_stmt->case_expression;
466 struct expression *to = case_stmt->case_to;
468 if (!expr) {
469 printf(" default");
470 } else {
471 if (expr->type == EXPR_VALUE) {
472 printf(" case %lld", expr->value);
473 if (to) {
474 if (to->type == EXPR_VALUE) {
475 printf(" .. %lld", to->value);
476 } else {
477 printf(" .. what?");
480 } else
481 printf(" what?");
483 printf(": .L%p\n", sym->bb_target);
484 } END_FOR_EACH_PTR(sym);
485 printf("# end case table\n");
487 show_statement(stmt->switch_statement);
489 if (stmt->switch_break->used)
490 printf(".L%p:\n", stmt->switch_break->bb_target);
493 static void show_symbol_decl(struct symbol_list *syms)
495 struct symbol *sym;
496 FOR_EACH_PTR(syms, sym) {
497 show_symbol_init(sym);
498 } END_FOR_EACH_PTR(sym);
501 static int show_return_stmt(struct statement *stmt);
504 * Print out a statement
506 int show_statement(struct statement *stmt)
508 if (!stmt)
509 return 0;
510 switch (stmt->type) {
511 case STMT_DECLARATION:
512 show_symbol_decl(stmt->declaration);
513 return 0;
514 case STMT_RETURN:
515 return show_return_stmt(stmt);
516 case STMT_COMPOUND: {
517 struct statement *s;
518 int last = 0;
520 if (stmt->inline_fn) {
521 show_statement(stmt->args);
522 printf("\tbegin_inline \t%s\n", show_ident(stmt->inline_fn->ident));
524 FOR_EACH_PTR(stmt->stmts, s) {
525 last = show_statement(s);
526 } END_FOR_EACH_PTR(s);
527 if (stmt->ret) {
528 int addr, bits;
529 printf(".L%p:\n", stmt->ret);
530 addr = show_symbol_expr(stmt->ret);
531 bits = stmt->ret->bit_size;
532 last = new_pseudo();
533 printf("\tld.%d\t\tv%d,[v%d]\n", bits, last, addr);
535 if (stmt->inline_fn)
536 printf("\tend_inlined\t%s\n", show_ident(stmt->inline_fn->ident));
537 return last;
540 case STMT_EXPRESSION:
541 return show_expression(stmt->expression);
542 case STMT_IF: {
543 int val, target;
544 struct expression *cond = stmt->if_conditional;
546 /* This is only valid if nobody can jump into the "dead" statement */
547 #if 0
548 if (cond->type == EXPR_VALUE) {
549 struct statement *s = stmt->if_true;
550 if (!cond->value)
551 s = stmt->if_false;
552 show_statement(s);
553 break;
555 #endif
556 val = show_expression(cond);
557 target = new_label();
558 printf("\tje\t\tv%d,.L%d\n", val, target);
559 show_statement(stmt->if_true);
560 if (stmt->if_false) {
561 int last = new_label();
562 printf("\tjmp\t\t.L%d\n", last);
563 printf(".L%d:\n", target);
564 target = last;
565 show_statement(stmt->if_false);
567 printf(".L%d:\n", target);
568 break;
570 case STMT_SWITCH:
571 show_switch_statement(stmt);
572 break;
574 case STMT_CASE:
575 printf(".L%p:\n", stmt->case_label);
576 show_statement(stmt->case_statement);
577 break;
579 case STMT_ITERATOR: {
580 struct statement *pre_statement = stmt->iterator_pre_statement;
581 struct expression *pre_condition = stmt->iterator_pre_condition;
582 struct statement *statement = stmt->iterator_statement;
583 struct statement *post_statement = stmt->iterator_post_statement;
584 struct expression *post_condition = stmt->iterator_post_condition;
585 int val, loop_top = 0, loop_bottom = 0;
587 show_symbol_decl(stmt->iterator_syms);
588 show_statement(pre_statement);
589 if (pre_condition) {
590 if (pre_condition->type == EXPR_VALUE) {
591 if (!pre_condition->value) {
592 loop_bottom = new_label();
593 printf("\tjmp\t\t.L%d\n", loop_bottom);
595 } else {
596 loop_bottom = new_label();
597 val = show_expression(pre_condition);
598 printf("\tje\t\tv%d, .L%d\n", val, loop_bottom);
601 if (!post_condition || post_condition->type != EXPR_VALUE || post_condition->value) {
602 loop_top = new_label();
603 printf(".L%d:\n", loop_top);
605 show_statement(statement);
606 if (stmt->iterator_continue->used)
607 printf(".L%p:\n", stmt->iterator_continue);
608 show_statement(post_statement);
609 if (!post_condition) {
610 printf("\tjmp\t\t.L%d\n", loop_top);
611 } else if (post_condition->type == EXPR_VALUE) {
612 if (post_condition->value)
613 printf("\tjmp\t\t.L%d\n", loop_top);
614 } else {
615 val = show_expression(post_condition);
616 printf("\tjne\t\tv%d, .L%d\n", val, loop_top);
618 if (stmt->iterator_break->used)
619 printf(".L%p:\n", stmt->iterator_break);
620 if (loop_bottom)
621 printf(".L%d:\n", loop_bottom);
622 break;
624 case STMT_NONE:
625 break;
627 case STMT_LABEL:
628 printf(".L%p:\n", stmt->label_identifier);
629 show_statement(stmt->label_statement);
630 break;
632 case STMT_GOTO:
633 if (stmt->goto_expression) {
634 int val = show_expression(stmt->goto_expression);
635 printf("\tgoto\t\t*v%d\n", val);
636 } else {
637 printf("\tgoto\t\t.L%p\n", stmt->goto_label->bb_target);
639 break;
640 case STMT_ASM:
641 printf("\tasm( .... )\n");
642 break;
643 case STMT_CONTEXT: {
644 int val = show_expression(stmt->expression);
645 printf("\tcontext( %d )\n", val);
646 break;
648 case STMT_RANGE: {
649 int val = show_expression(stmt->range_expression);
650 int low = show_expression(stmt->range_low);
651 int high = show_expression(stmt->range_high);
652 printf("\trange( %d %d-%d)\n", val, low, high);
653 break;
656 return 0;
659 static int show_call_expression(struct expression *expr)
661 struct symbol *direct;
662 struct expression *arg, *fn;
663 int fncall, retval;
664 int framesize;
666 if (!expr->ctype) {
667 warning(expr->pos, "\tcall with no type!");
668 return 0;
671 framesize = 0;
672 FOR_EACH_PTR_REVERSE(expr->args, arg) {
673 int new = show_expression(arg);
674 int size = arg->ctype->bit_size;
675 printf("\tpush.%d\t\tv%d\n", size, new);
676 framesize += size >> 3;
677 } END_FOR_EACH_PTR_REVERSE(arg);
679 fn = expr->fn;
681 /* Remove dereference, if any */
682 direct = NULL;
683 if (fn->type == EXPR_PREOP) {
684 if (fn->unop->type == EXPR_SYMBOL) {
685 struct symbol *sym = fn->unop->symbol;
686 if (sym->ctype.base_type->type == SYM_FN)
687 direct = sym;
690 if (direct) {
691 printf("\tcall\t\t%s\n", show_ident(direct->ident));
692 } else {
693 fncall = show_expression(fn);
694 printf("\tcall\t\t*v%d\n", fncall);
696 if (framesize)
697 printf("\tadd.%d\t\tvSP,vSP,$%d\n", bits_in_pointer, framesize);
699 retval = new_pseudo();
700 printf("\tmov.%d\t\tv%d,retval\n", expr->ctype->bit_size, retval);
701 return retval;
704 static int show_comma(struct expression *expr)
706 show_expression(expr->left);
707 return show_expression(expr->right);
710 static int show_binop(struct expression *expr)
712 int left = show_expression(expr->left);
713 int right = show_expression(expr->right);
714 int new = new_pseudo();
715 const char *opname;
716 static const char *name[] = {
717 ['+'] = "add", ['-'] = "sub",
718 ['*'] = "mul", ['/'] = "div",
719 ['%'] = "mod", ['&'] = "and",
720 ['|'] = "lor", ['^'] = "xor"
722 unsigned int op = expr->op;
724 opname = show_special(op);
725 if (op < sizeof(name)/sizeof(*name))
726 opname = name[op];
727 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname,
728 expr->ctype->bit_size,
729 new, left, right);
730 return new;
733 static int show_slice(struct expression *expr)
735 int target = show_expression(expr->base);
736 int new = new_pseudo();
737 printf("\tslice.%d\t\tv%d,v%d,%d\n", expr->r_nrbits, target, new, expr->r_bitpos);
738 return new;
741 static int show_regular_preop(struct expression *expr)
743 int target = show_expression(expr->unop);
744 int new = new_pseudo();
745 static const char *name[] = {
746 ['!'] = "nonzero", ['-'] = "neg",
747 ['~'] = "not",
749 unsigned int op = expr->op;
750 const char *opname;
752 opname = show_special(op);
753 if (op < sizeof(name)/sizeof(*name))
754 opname = name[op];
755 printf("\t%s.%d\t\tv%d,v%d\n", opname, expr->ctype->bit_size, new, target);
756 return new;
760 * FIXME! Not all accesses are memory loads. We should
761 * check what kind of symbol is behind the dereference.
763 static int show_address_gen(struct expression *expr)
765 return show_expression(expr->unop);
768 static int show_load_gen(int bits, struct expression *expr, int addr)
770 int new = new_pseudo();
772 printf("\tld.%d\t\tv%d,[v%d]\n", bits, new, addr);
773 return new;
776 static void show_store_gen(int bits, int value, struct expression *expr, int addr)
778 /* FIXME!!! Bitfield store! */
779 printf("\tst.%d\t\tv%d,[v%d]\n", bits, value, addr);
782 static int show_assignment(struct expression *expr)
784 struct expression *target = expr->left;
785 int val, addr, bits;
787 if (!expr->ctype)
788 return 0;
790 bits = expr->ctype->bit_size;
791 val = show_expression(expr->right);
792 addr = show_address_gen(target);
793 show_store_gen(bits, val, target, addr);
794 return val;
797 static int show_return_stmt(struct statement *stmt)
799 struct expression *expr = stmt->ret_value;
800 struct symbol *target = stmt->ret_target;
802 if (expr && expr->ctype) {
803 int val = show_expression(expr);
804 int bits = expr->ctype->bit_size;
805 int addr = show_symbol_expr(target);
806 show_store_gen(bits, val, NULL, addr);
808 printf("\tret\t\t(%p)\n", target);
809 return 0;
812 static int show_initialization(struct symbol *sym, struct expression *expr)
814 int val, addr, bits;
816 if (!expr->ctype)
817 return 0;
819 bits = expr->ctype->bit_size;
820 val = show_expression(expr);
821 addr = show_symbol_expr(sym);
822 // FIXME! The "target" expression is for bitfield store information.
823 // Leave it NULL, which works fine.
824 show_store_gen(bits, val, NULL, addr);
825 return 0;
828 static int show_access(struct expression *expr)
830 int addr = show_address_gen(expr);
831 return show_load_gen(expr->ctype->bit_size, expr, addr);
834 static int show_inc_dec(struct expression *expr, int postop)
836 int addr = show_address_gen(expr->unop);
837 int retval, new;
838 const char *opname = expr->op == SPECIAL_INCREMENT ? "add" : "sub";
839 int bits = expr->ctype->bit_size;
841 retval = show_load_gen(bits, expr->unop, addr);
842 new = retval;
843 if (postop)
844 new = new_pseudo();
845 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname, bits, new, retval);
846 show_store_gen(bits, new, expr->unop, addr);
847 return retval;
850 static int show_preop(struct expression *expr)
853 * '*' is an lvalue access, and is fundamentally different
854 * from an arithmetic operation. Maybe it should have an
855 * expression type of its own..
857 if (expr->op == '*')
858 return show_access(expr);
859 if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
860 return show_inc_dec(expr, 0);
861 return show_regular_preop(expr);
864 static int show_postop(struct expression *expr)
866 return show_inc_dec(expr, 1);
869 static int show_symbol_expr(struct symbol *sym)
871 int new = new_pseudo();
873 if (sym->initializer && sym->initializer->type == EXPR_STRING)
874 return show_string_expr(sym->initializer);
876 if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC)) {
877 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer, new, show_ident(sym->ident));
878 return new;
880 if (sym->ctype.modifiers & MOD_ADDRESSABLE) {
881 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer, new, sym->value);
882 return new;
884 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer, new, show_ident(sym->ident), sym);
885 return new;
888 static int show_symbol_init(struct symbol *sym)
890 struct expression *expr = sym->initializer;
892 if (expr) {
893 int val, addr, bits;
895 bits = expr->ctype->bit_size;
896 val = show_expression(expr);
897 addr = show_symbol_expr(sym);
898 show_store_gen(bits, val, NULL, addr);
900 return 0;
903 static int type_is_signed(struct symbol *sym)
905 if (sym->type == SYM_NODE)
906 sym = sym->ctype.base_type;
907 if (sym->type == SYM_PTR)
908 return 0;
909 return !(sym->ctype.modifiers & MOD_UNSIGNED);
912 static int show_cast_expr(struct expression *expr)
914 struct symbol *old_type, *new_type;
915 int op = show_expression(expr->cast_expression);
916 int oldbits, newbits;
917 int new, is_signed;
919 old_type = expr->cast_expression->ctype;
920 new_type = expr->cast_type;
922 oldbits = old_type->bit_size;
923 newbits = new_type->bit_size;
924 if (oldbits >= newbits)
925 return op;
926 new = new_pseudo();
927 is_signed = type_is_signed(old_type);
928 if (is_signed) {
929 printf("\tsext%d.%d\tv%d,v%d\n", oldbits, newbits, new, op);
930 } else {
931 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits, new, op, (1UL << oldbits)-1);
933 return new;
936 static int show_value(struct expression *expr)
938 int new = new_pseudo();
939 unsigned long long value = expr->value;
941 printf("\tmovi.%d\t\tv%d,$%llu\n", expr->ctype->bit_size, new, value);
942 return new;
945 static int show_fvalue(struct expression *expr)
947 int new = new_pseudo();
948 long double value = expr->fvalue;
950 printf("\tmovf.%d\t\tv%d,$%Lf\n", expr->ctype->bit_size, new, value);
951 return new;
954 static int show_string_expr(struct expression *expr)
956 int new = new_pseudo();
958 printf("\tmovi.%d\t\tv%d,&%s\n", bits_in_pointer, new, show_string(expr->string));
959 return new;
962 static int show_label_expr(struct expression *expr)
964 int new = new_pseudo();
965 printf("\tmovi.%d\t\tv%d,.L%p\n",bits_in_pointer, new, expr->label_symbol);
966 return new;
969 static int show_conditional_expr(struct expression *expr)
971 int cond = show_expression(expr->conditional);
972 int true = show_expression(expr->cond_true);
973 int false = show_expression(expr->cond_false);
974 int new = new_pseudo();
976 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond, expr->ctype->bit_size, new, true, false);
977 return new;
980 static int show_statement_expr(struct expression *expr)
982 return show_statement(expr->statement);
985 static int show_position_expr(struct expression *expr, struct symbol *base)
987 int new = show_expression(expr->init_expr);
988 struct symbol *ctype = expr->init_expr->ctype;
989 int bit_offset;
991 bit_offset = ctype ? ctype->bit_offset : -1;
993 printf("\tinsert v%d at [%d:%d] of %s\n", new,
994 expr->init_offset, bit_offset,
995 show_ident(base->ident));
996 return 0;
999 static int show_initializer_expr(struct expression *expr, struct symbol *ctype)
1001 struct expression *entry;
1003 FOR_EACH_PTR(expr->expr_list, entry) {
1005 again:
1006 // Nested initializers have their positions already
1007 // recursively calculated - just output them too
1008 if (entry->type == EXPR_INITIALIZER) {
1009 show_initializer_expr(entry, ctype);
1010 continue;
1013 // Initializer indexes and identifiers should
1014 // have been evaluated to EXPR_POS
1015 if (entry->type == EXPR_IDENTIFIER) {
1016 printf(" AT '%s':\n", show_ident(entry->expr_ident));
1017 entry = entry->ident_expression;
1018 goto again;
1021 if (entry->type == EXPR_INDEX) {
1022 printf(" AT '%d..%d:\n", entry->idx_from, entry->idx_to);
1023 entry = entry->idx_expression;
1024 goto again;
1026 if (entry->type == EXPR_POS) {
1027 show_position_expr(entry, ctype);
1028 continue;
1030 show_initialization(ctype, entry);
1031 } END_FOR_EACH_PTR(entry);
1032 return 0;
1035 int show_symbol_expr_init(struct symbol *sym)
1037 struct expression *expr = sym->initializer;
1039 if (expr)
1040 show_expression(expr);
1041 return show_symbol_expr(sym);
1045 * Print out an expression. Return the pseudo that contains the
1046 * variable.
1048 int show_expression(struct expression *expr)
1050 if (!expr)
1051 return 0;
1053 if (!expr->ctype) {
1054 struct position *pos = &expr->pos;
1055 printf("\tno type at %s:%d:%d\n",
1056 stream_name(pos->stream),
1057 pos->line, pos->pos);
1058 return 0;
1061 switch (expr->type) {
1062 case EXPR_CALL:
1063 return show_call_expression(expr);
1065 case EXPR_ASSIGNMENT:
1066 return show_assignment(expr);
1068 case EXPR_COMMA:
1069 return show_comma(expr);
1070 case EXPR_BINOP:
1071 case EXPR_COMPARE:
1072 case EXPR_LOGICAL:
1073 return show_binop(expr);
1074 case EXPR_PREOP:
1075 return show_preop(expr);
1076 case EXPR_POSTOP:
1077 return show_postop(expr);
1078 case EXPR_SYMBOL:
1079 return show_symbol_expr(expr->symbol);
1080 case EXPR_DEREF:
1081 case EXPR_SIZEOF:
1082 case EXPR_PTRSIZEOF:
1083 case EXPR_ALIGNOF:
1084 case EXPR_OFFSETOF:
1085 warning(expr->pos, "invalid expression after evaluation");
1086 return 0;
1087 case EXPR_CAST:
1088 case EXPR_FORCE_CAST:
1089 case EXPR_IMPLIED_CAST:
1090 return show_cast_expr(expr);
1091 case EXPR_VALUE:
1092 return show_value(expr);
1093 case EXPR_FVALUE:
1094 return show_fvalue(expr);
1095 case EXPR_STRING:
1096 return show_string_expr(expr);
1097 case EXPR_INITIALIZER:
1098 return show_initializer_expr(expr, expr->ctype);
1099 case EXPR_SELECT:
1100 case EXPR_CONDITIONAL:
1101 return show_conditional_expr(expr);
1102 case EXPR_STATEMENT:
1103 return show_statement_expr(expr);
1104 case EXPR_LABEL:
1105 return show_label_expr(expr);
1106 case EXPR_SLICE:
1107 return show_slice(expr);
1109 // None of these should exist as direct expressions: they are only
1110 // valid as sub-expressions of initializers.
1111 case EXPR_POS:
1112 warning(expr->pos, "unable to show plain initializer position expression");
1113 return 0;
1114 case EXPR_IDENTIFIER:
1115 warning(expr->pos, "unable to show identifier expression");
1116 return 0;
1117 case EXPR_INDEX:
1118 warning(expr->pos, "unable to show index expression");
1119 return 0;
1120 case EXPR_TYPE:
1121 warning(expr->pos, "unable to show type expression");
1122 return 0;
1124 return 0;