flow: tweak the ->pos setting for fake assignments
[smatch.git] / show-parse.c
blobc97debed6ca486c6750599d133eeec438ca0a74f
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 int len = 0;
99 int i;
100 struct mod_name {
101 unsigned long mod;
102 const char *name;
103 } *m;
105 static struct mod_name mod_names[] = {
106 {MOD_AUTO, "auto"},
107 {MOD_REGISTER, "register"},
108 {MOD_STATIC, "static"},
109 {MOD_EXTERN, "extern"},
110 {MOD_CONST, "const"},
111 {MOD_VOLATILE, "volatile"},
112 {MOD_SIGNED, "[signed]"},
113 {MOD_UNSIGNED, "[unsigned]"},
114 {MOD_CHAR, "[char]"},
115 {MOD_SHORT, "[short]"},
116 {MOD_LONG, "[long]"},
117 {MOD_LONGLONG, "[long long]"},
118 {MOD_LONGLONGLONG, "[long long long]"},
119 {MOD_TYPEDEF, "[typedef]"},
120 {MOD_TLS, "[tls]"},
121 {MOD_INLINE, "inline"},
122 {MOD_ADDRESSABLE, "[addressable]"},
123 {MOD_NOCAST, "[nocast]"},
124 {MOD_NODEREF, "[noderef]"},
125 {MOD_ACCESSED, "[accessed]"},
126 {MOD_TOPLEVEL, "[toplevel]"},
127 {MOD_ASSIGNED, "[assigned]"},
128 {MOD_TYPE, "[type]"},
129 {MOD_SAFE, "[safe]"},
130 {MOD_USERTYPE, "[usertype]"},
131 {MOD_NORETURN, "[noreturn]"},
132 {MOD_EXPLICITLY_SIGNED, "[explicitly-signed]"},
133 {MOD_BITWISE, "[bitwise]"},
136 for (i = 0; i < ARRAY_SIZE(mod_names); i++) {
137 m = mod_names + i;
138 if (mod & m->mod) {
139 char c;
140 const char *name = m->name;
141 while ((c = *name++) != '\0' && len + 2 < sizeof buffer)
142 buffer[len++] = c;
143 buffer[len++] = ' ';
146 buffer[len] = 0;
147 return buffer;
150 static void show_struct_member(struct symbol *sym)
152 printf("\t%s:%d:%ld at offset %ld.%d", show_ident(sym->ident), sym->bit_size, sym->ctype.alignment, sym->offset, sym->bit_offset);
153 printf("\n");
156 void show_symbol_list(struct symbol_list *list, const char *sep)
158 struct symbol *sym;
159 const char *prepend = "";
161 FOR_EACH_PTR(list, sym) {
162 puts(prepend);
163 prepend = ", ";
164 show_symbol(sym);
165 } END_FOR_EACH_PTR(sym);
168 struct type_name {
169 char *start;
170 char *end;
173 static void FORMAT_ATTR(2) prepend(struct type_name *name, const char *fmt, ...)
175 static char buffer[512];
176 int n;
178 va_list args;
179 va_start(args, fmt);
180 n = vsprintf(buffer, fmt, args);
181 va_end(args);
183 name->start -= n;
184 memcpy(name->start, buffer, n);
187 static void FORMAT_ATTR(2) append(struct type_name *name, const char *fmt, ...)
189 static char buffer[512];
190 int n;
192 va_list args;
193 va_start(args, fmt);
194 n = vsprintf(buffer, fmt, args);
195 va_end(args);
197 memcpy(name->end, buffer, n);
198 name->end += n;
201 static struct ctype_name {
202 struct symbol *sym;
203 const char *name;
204 } typenames[] = {
205 { & char_ctype, "char" },
206 { &schar_ctype, "signed char" },
207 { &uchar_ctype, "unsigned char" },
208 { & short_ctype, "short" },
209 { &sshort_ctype, "signed short" },
210 { &ushort_ctype, "unsigned short" },
211 { & int_ctype, "int" },
212 { &sint_ctype, "signed int" },
213 { &uint_ctype, "unsigned int" },
214 { &slong_ctype, "signed long" },
215 { & long_ctype, "long" },
216 { &ulong_ctype, "unsigned long" },
217 { & llong_ctype, "long long" },
218 { &sllong_ctype, "signed long long" },
219 { &ullong_ctype, "unsigned long long" },
220 { & lllong_ctype, "long long long" },
221 { &slllong_ctype, "signed long long long" },
222 { &ulllong_ctype, "unsigned long long long" },
224 { &void_ctype, "void" },
225 { &bool_ctype, "bool" },
226 { &string_ctype, "string" },
228 { &float_ctype, "float" },
229 { &double_ctype, "double" },
230 { &ldouble_ctype,"long double" },
231 { &incomplete_ctype, "incomplete type" },
232 { &int_type, "abstract int" },
233 { &fp_type, "abstract fp" },
234 { &label_ctype, "label type" },
235 { &bad_ctype, "bad type" },
238 const char *builtin_typename(struct symbol *sym)
240 int i;
242 for (i = 0; i < sizeof(typenames)/sizeof(typenames[0]); i++)
243 if (typenames[i].sym == sym)
244 return typenames[i].name;
245 return NULL;
248 const char *builtin_ctypename(struct ctype *ctype)
250 int i;
252 for (i = 0; i < sizeof(typenames)/sizeof(typenames[0]); i++)
253 if (&typenames[i].sym->ctype == ctype)
254 return typenames[i].name;
255 return NULL;
258 static void do_show_type(struct symbol *sym, struct type_name *name)
260 const char *typename;
261 unsigned long mod = 0;
262 int as = 0;
263 int was_ptr = 0;
264 int restr = 0;
265 int fouled = 0;
267 deeper:
268 if (!sym || (sym->type != SYM_NODE && sym->type != SYM_ARRAY &&
269 sym->type != SYM_BITFIELD)) {
270 const char *s;
271 size_t len;
273 if (as)
274 prepend(name, "<asn:%d>", as);
276 s = modifier_string(mod);
277 len = strlen(s);
278 name->start -= len;
279 memcpy(name->start, s, len);
280 mod = 0;
281 as = 0;
284 if (!sym)
285 goto out;
287 if ((typename = builtin_typename(sym))) {
288 int len = strlen(typename);
289 if (name->start != name->end)
290 *--name->start = ' ';
291 name->start -= len;
292 memcpy(name->start, typename, len);
293 goto out;
296 /* Prepend */
297 switch (sym->type) {
298 case SYM_PTR:
299 prepend(name, "*");
300 mod = sym->ctype.modifiers;
301 as = sym->ctype.as;
302 was_ptr = 1;
303 break;
305 case SYM_FN:
306 if (was_ptr) {
307 prepend(name, "( ");
308 append(name, " )");
309 was_ptr = 0;
311 append(name, "( ... )");
312 break;
314 case SYM_STRUCT:
315 if (name->start != name->end)
316 *--name->start = ' ';
317 prepend(name, "struct %s", show_ident(sym->ident));
318 goto out;
320 case SYM_UNION:
321 if (name->start != name->end)
322 *--name->start = ' ';
323 prepend(name, "union %s", show_ident(sym->ident));
324 goto out;
326 case SYM_ENUM:
327 prepend(name, "enum %s ", show_ident(sym->ident));
328 break;
330 case SYM_NODE:
331 append(name, "%s", show_ident(sym->ident));
332 mod |= sym->ctype.modifiers;
333 as |= sym->ctype.as;
334 break;
336 case SYM_BITFIELD:
337 mod |= sym->ctype.modifiers;
338 as |= sym->ctype.as;
339 append(name, ":%d", sym->bit_size);
340 break;
342 case SYM_LABEL:
343 append(name, "label(%s:%p)", show_ident(sym->ident), sym);
344 return;
346 case SYM_ARRAY:
347 mod |= sym->ctype.modifiers;
348 as |= sym->ctype.as;
349 if (was_ptr) {
350 prepend(name, "( ");
351 append(name, " )");
352 was_ptr = 0;
354 append(name, "[%lld]", get_expression_value(sym->array_size));
355 break;
357 case SYM_RESTRICT:
358 if (!sym->ident) {
359 restr = 1;
360 break;
362 if (name->start != name->end)
363 *--name->start = ' ';
364 prepend(name, "restricted %s", show_ident(sym->ident));
365 goto out;
367 case SYM_FOULED:
368 fouled = 1;
369 break;
371 default:
372 if (name->start != name->end)
373 *--name->start = ' ';
374 prepend(name, "unknown type %d", sym->type);
375 goto out;
378 sym = sym->ctype.base_type;
379 goto deeper;
381 out:
382 if (restr)
383 prepend(name, "restricted ");
384 if (fouled)
385 prepend(name, "fouled ");
388 void show_type(struct symbol *sym)
390 char array[200];
391 struct type_name name;
393 name.start = name.end = array+100;
394 do_show_type(sym, &name);
395 *name.end = 0;
396 printf("%s", name.start);
399 const char *show_typename(struct symbol *sym)
401 static char array[200];
402 struct type_name name;
404 name.start = name.end = array+100;
405 do_show_type(sym, &name);
406 *name.end = 0;
407 return name.start;
410 void show_symbol(struct symbol *sym)
412 struct symbol *type;
414 if (!sym)
415 return;
417 if (sym->ctype.alignment)
418 printf(".align %ld\n", sym->ctype.alignment);
420 show_type(sym);
421 type = sym->ctype.base_type;
422 if (!type) {
423 printf("\n");
424 return;
428 * Show actual implementation information
430 switch (type->type) {
431 struct symbol *member;
433 case SYM_STRUCT:
434 case SYM_UNION:
435 printf(" {\n");
436 FOR_EACH_PTR(type->symbol_list, member) {
437 show_struct_member(member);
438 } END_FOR_EACH_PTR(member);
439 printf("}\n");
440 break;
442 case SYM_FN: {
443 struct statement *stmt = type->stmt;
444 printf("\n");
445 if (stmt) {
446 int val;
447 val = show_statement(stmt);
448 if (val)
449 printf("\tmov.%d\t\tretval,%d\n", stmt->ret->bit_size, val);
450 printf("\tret\n");
452 break;
455 default:
456 printf("\n");
457 break;
460 if (sym->initializer) {
461 printf(" = \n");
462 show_expression(sym->initializer);
466 static int show_symbol_init(struct symbol *sym);
468 static int new_pseudo(void)
470 static int nr = 0;
471 return ++nr;
474 static int new_label(void)
476 static int label = 0;
477 return ++label;
480 static void show_switch_statement(struct statement *stmt)
482 int val = show_expression(stmt->switch_expression);
483 struct symbol *sym;
484 printf("\tswitch v%d\n", val);
487 * Debugging only: Check that the case list is correct
488 * by printing it out.
490 * This is where a _real_ back-end would go through the
491 * cases to decide whether to use a lookup table or a
492 * series of comparisons etc
494 printf("# case table:\n");
495 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
496 struct statement *case_stmt = sym->stmt;
497 struct expression *expr = case_stmt->case_expression;
498 struct expression *to = case_stmt->case_to;
500 if (!expr) {
501 printf(" default");
502 } else {
503 if (expr->type == EXPR_VALUE) {
504 printf(" case %lld", expr->value);
505 if (to) {
506 if (to->type == EXPR_VALUE) {
507 printf(" .. %lld", to->value);
508 } else {
509 printf(" .. what?");
512 } else
513 printf(" what?");
515 printf(": .L%p\n", sym->bb_target);
516 } END_FOR_EACH_PTR(sym);
517 printf("# end case table\n");
519 show_statement(stmt->switch_statement);
521 if (stmt->switch_break->used)
522 printf(".L%p:\n", stmt->switch_break->bb_target);
525 static void show_symbol_decl(struct symbol_list *syms)
527 struct symbol *sym;
528 FOR_EACH_PTR(syms, sym) {
529 show_symbol_init(sym);
530 } END_FOR_EACH_PTR(sym);
533 static int show_return_stmt(struct statement *stmt);
536 * Print out a statement
538 int show_statement(struct statement *stmt)
540 if (!stmt)
541 return 0;
542 switch (stmt->type) {
543 case STMT_DECLARATION:
544 show_symbol_decl(stmt->declaration);
545 return 0;
546 case STMT_RETURN:
547 return show_return_stmt(stmt);
548 case STMT_COMPOUND: {
549 struct statement *s;
550 int last = 0;
552 if (stmt->inline_fn) {
553 show_statement(stmt->args);
554 printf("\tbegin_inline \t%s\n", show_ident(stmt->inline_fn->ident));
556 FOR_EACH_PTR(stmt->stmts, s) {
557 last = show_statement(s);
558 } END_FOR_EACH_PTR(s);
559 if (stmt->ret) {
560 int addr, bits;
561 printf(".L%p:\n", stmt->ret);
562 addr = show_symbol_expr(stmt->ret);
563 bits = stmt->ret->bit_size;
564 last = new_pseudo();
565 printf("\tld.%d\t\tv%d,[v%d]\n", bits, last, addr);
567 if (stmt->inline_fn)
568 printf("\tend_inlined\t%s\n", show_ident(stmt->inline_fn->ident));
569 return last;
572 case STMT_EXPRESSION:
573 return show_expression(stmt->expression);
574 case STMT_IF: {
575 int val, target;
576 struct expression *cond = stmt->if_conditional;
578 /* This is only valid if nobody can jump into the "dead" statement */
579 #if 0
580 if (cond->type == EXPR_VALUE) {
581 struct statement *s = stmt->if_true;
582 if (!cond->value)
583 s = stmt->if_false;
584 show_statement(s);
585 break;
587 #endif
588 val = show_expression(cond);
589 target = new_label();
590 printf("\tje\t\tv%d,.L%d\n", val, target);
591 show_statement(stmt->if_true);
592 if (stmt->if_false) {
593 int last = new_label();
594 printf("\tjmp\t\t.L%d\n", last);
595 printf(".L%d:\n", target);
596 target = last;
597 show_statement(stmt->if_false);
599 printf(".L%d:\n", target);
600 break;
602 case STMT_SWITCH:
603 show_switch_statement(stmt);
604 break;
606 case STMT_CASE:
607 printf(".L%p:\n", stmt->case_label);
608 show_statement(stmt->case_statement);
609 break;
611 case STMT_ITERATOR: {
612 struct statement *pre_statement = stmt->iterator_pre_statement;
613 struct expression *pre_condition = stmt->iterator_pre_condition;
614 struct statement *statement = stmt->iterator_statement;
615 struct statement *post_statement = stmt->iterator_post_statement;
616 struct expression *post_condition = stmt->iterator_post_condition;
617 int val, loop_top = 0, loop_bottom = 0;
619 show_symbol_decl(stmt->iterator_syms);
620 show_statement(pre_statement);
621 if (pre_condition) {
622 if (pre_condition->type == EXPR_VALUE) {
623 if (!pre_condition->value) {
624 loop_bottom = new_label();
625 printf("\tjmp\t\t.L%d\n", loop_bottom);
627 } else {
628 loop_bottom = new_label();
629 val = show_expression(pre_condition);
630 printf("\tje\t\tv%d, .L%d\n", val, loop_bottom);
633 if (!post_condition || post_condition->type != EXPR_VALUE || post_condition->value) {
634 loop_top = new_label();
635 printf(".L%d:\n", loop_top);
637 show_statement(statement);
638 if (stmt->iterator_continue->used)
639 printf(".L%p:\n", stmt->iterator_continue);
640 show_statement(post_statement);
641 if (!post_condition) {
642 printf("\tjmp\t\t.L%d\n", loop_top);
643 } else if (post_condition->type == EXPR_VALUE) {
644 if (post_condition->value)
645 printf("\tjmp\t\t.L%d\n", loop_top);
646 } else {
647 val = show_expression(post_condition);
648 printf("\tjne\t\tv%d, .L%d\n", val, loop_top);
650 if (stmt->iterator_break->used)
651 printf(".L%p:\n", stmt->iterator_break);
652 if (loop_bottom)
653 printf(".L%d:\n", loop_bottom);
654 break;
656 case STMT_NONE:
657 break;
659 case STMT_LABEL:
660 printf(".L%p:\n", stmt->label_identifier);
661 show_statement(stmt->label_statement);
662 break;
664 case STMT_GOTO:
665 if (stmt->goto_expression) {
666 int val = show_expression(stmt->goto_expression);
667 printf("\tgoto\t\t*v%d\n", val);
668 } else {
669 printf("\tgoto\t\t.L%p\n", stmt->goto_label->bb_target);
671 break;
672 case STMT_ASM:
673 printf("\tasm( .... )\n");
674 break;
675 case STMT_CONTEXT: {
676 int val = show_expression(stmt->expression);
677 printf("\tcontext( %d )\n", val);
678 break;
680 case STMT_RANGE: {
681 int val = show_expression(stmt->range_expression);
682 int low = show_expression(stmt->range_low);
683 int high = show_expression(stmt->range_high);
684 printf("\trange( %d %d-%d)\n", val, low, high);
685 break;
688 return 0;
691 static int show_call_expression(struct expression *expr)
693 struct symbol *direct;
694 struct expression *arg, *fn;
695 int fncall, retval;
696 int framesize;
698 if (!expr->ctype) {
699 warning(expr->pos, "\tcall with no type!");
700 return 0;
703 framesize = 0;
704 FOR_EACH_PTR_REVERSE(expr->args, arg) {
705 int new = show_expression(arg);
706 int size = arg->ctype->bit_size;
707 printf("\tpush.%d\t\tv%d\n", size, new);
708 framesize += bits_to_bytes(size);
709 } END_FOR_EACH_PTR_REVERSE(arg);
711 fn = expr->fn;
713 /* Remove dereference, if any */
714 direct = NULL;
715 if (fn->type == EXPR_PREOP) {
716 if (fn->unop->type == EXPR_SYMBOL) {
717 struct symbol *sym = fn->unop->symbol;
718 if (sym->ctype.base_type->type == SYM_FN)
719 direct = sym;
722 if (direct) {
723 printf("\tcall\t\t%s\n", show_ident(direct->ident));
724 } else {
725 fncall = show_expression(fn);
726 printf("\tcall\t\t*v%d\n", fncall);
728 if (framesize)
729 printf("\tadd.%d\t\tvSP,vSP,$%d\n", bits_in_pointer, framesize);
731 retval = new_pseudo();
732 printf("\tmov.%d\t\tv%d,retval\n", expr->ctype->bit_size, retval);
733 return retval;
736 static int show_comma(struct expression *expr)
738 show_expression(expr->left);
739 return show_expression(expr->right);
742 static int show_binop(struct expression *expr)
744 int left = show_expression(expr->left);
745 int right = show_expression(expr->right);
746 int new = new_pseudo();
747 const char *opname;
748 static const char *name[] = {
749 ['+'] = "add", ['-'] = "sub",
750 ['*'] = "mul", ['/'] = "div",
751 ['%'] = "mod", ['&'] = "and",
752 ['|'] = "lor", ['^'] = "xor"
754 unsigned int op = expr->op;
756 opname = show_special(op);
757 if (op < sizeof(name)/sizeof(*name))
758 opname = name[op];
759 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname,
760 expr->ctype->bit_size,
761 new, left, right);
762 return new;
765 static int show_slice(struct expression *expr)
767 int target = show_expression(expr->base);
768 int new = new_pseudo();
769 printf("\tslice.%d\t\tv%d,v%d,%d\n", expr->r_nrbits, target, new, expr->r_bitpos);
770 return new;
773 static int show_regular_preop(struct expression *expr)
775 int target = show_expression(expr->unop);
776 int new = new_pseudo();
777 static const char *name[] = {
778 ['!'] = "nonzero", ['-'] = "neg",
779 ['~'] = "not",
781 unsigned int op = expr->op;
782 const char *opname;
784 opname = show_special(op);
785 if (op < sizeof(name)/sizeof(*name))
786 opname = name[op];
787 printf("\t%s.%d\t\tv%d,v%d\n", opname, expr->ctype->bit_size, new, target);
788 return new;
792 * FIXME! Not all accesses are memory loads. We should
793 * check what kind of symbol is behind the dereference.
795 static int show_address_gen(struct expression *expr)
797 return show_expression(expr->unop);
800 static int show_load_gen(int bits, struct expression *expr, int addr)
802 int new = new_pseudo();
804 printf("\tld.%d\t\tv%d,[v%d]\n", bits, new, addr);
805 return new;
808 static void show_store_gen(int bits, int value, struct expression *expr, int addr)
810 /* FIXME!!! Bitfield store! */
811 printf("\tst.%d\t\tv%d,[v%d]\n", bits, value, addr);
814 static int show_assignment(struct expression *expr)
816 struct expression *target = expr->left;
817 int val, addr, bits;
819 if (!expr->ctype)
820 return 0;
822 bits = expr->ctype->bit_size;
823 val = show_expression(expr->right);
824 addr = show_address_gen(target);
825 show_store_gen(bits, val, target, addr);
826 return val;
829 static int show_return_stmt(struct statement *stmt)
831 struct expression *expr = stmt->ret_value;
832 struct symbol *target = stmt->ret_target;
834 if (expr && expr->ctype) {
835 int val = show_expression(expr);
836 int bits = expr->ctype->bit_size;
837 int addr = show_symbol_expr(target);
838 show_store_gen(bits, val, NULL, addr);
840 printf("\tret\t\t(%p)\n", target);
841 return 0;
844 static int show_initialization(struct symbol *sym, struct expression *expr)
846 int val, addr, bits;
848 if (!expr->ctype)
849 return 0;
851 bits = expr->ctype->bit_size;
852 val = show_expression(expr);
853 addr = show_symbol_expr(sym);
854 // FIXME! The "target" expression is for bitfield store information.
855 // Leave it NULL, which works fine.
856 show_store_gen(bits, val, NULL, addr);
857 return 0;
860 static int show_access(struct expression *expr)
862 int addr = show_address_gen(expr);
863 return show_load_gen(expr->ctype->bit_size, expr, addr);
866 static int show_inc_dec(struct expression *expr, int postop)
868 int addr = show_address_gen(expr->unop);
869 int retval, new;
870 const char *opname = expr->op == SPECIAL_INCREMENT ? "add" : "sub";
871 int bits = expr->ctype->bit_size;
873 retval = show_load_gen(bits, expr->unop, addr);
874 new = retval;
875 if (postop)
876 new = new_pseudo();
877 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname, bits, new, retval);
878 show_store_gen(bits, new, expr->unop, addr);
879 return retval;
882 static int show_preop(struct expression *expr)
885 * '*' is an lvalue access, and is fundamentally different
886 * from an arithmetic operation. Maybe it should have an
887 * expression type of its own..
889 if (expr->op == '*')
890 return show_access(expr);
891 if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
892 return show_inc_dec(expr, 0);
893 return show_regular_preop(expr);
896 static int show_postop(struct expression *expr)
898 return show_inc_dec(expr, 1);
901 static int show_symbol_expr(struct symbol *sym)
903 int new = new_pseudo();
905 if (sym->initializer && sym->initializer->type == EXPR_STRING)
906 return show_string_expr(sym->initializer);
908 if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC)) {
909 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer, new, show_ident(sym->ident));
910 return new;
912 if (sym->ctype.modifiers & MOD_ADDRESSABLE) {
913 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer, new, sym->value);
914 return new;
916 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer, new, show_ident(sym->ident), sym);
917 return new;
920 static int show_symbol_init(struct symbol *sym)
922 struct expression *expr = sym->initializer;
924 if (expr) {
925 int val, addr, bits;
927 bits = expr->ctype->bit_size;
928 val = show_expression(expr);
929 addr = show_symbol_expr(sym);
930 show_store_gen(bits, val, NULL, addr);
932 return 0;
935 static int type_is_signed(struct symbol *sym)
937 if (sym->type == SYM_NODE)
938 sym = sym->ctype.base_type;
939 if (sym->type == SYM_PTR)
940 return 0;
941 return !(sym->ctype.modifiers & MOD_UNSIGNED);
944 static int show_cast_expr(struct expression *expr)
946 struct symbol *old_type, *new_type;
947 int op = show_expression(expr->cast_expression);
948 int oldbits, newbits;
949 int new, is_signed;
951 old_type = expr->cast_expression->ctype;
952 new_type = expr->cast_type;
954 oldbits = old_type->bit_size;
955 newbits = new_type->bit_size;
956 if (oldbits >= newbits)
957 return op;
958 new = new_pseudo();
959 is_signed = type_is_signed(old_type);
960 if (is_signed) {
961 printf("\tsext%d.%d\tv%d,v%d\n", oldbits, newbits, new, op);
962 } else {
963 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits, new, op, (1UL << oldbits)-1);
965 return new;
968 static int show_value(struct expression *expr)
970 int new = new_pseudo();
971 unsigned long long value = expr->value;
973 printf("\tmovi.%d\t\tv%d,$%llu\n", expr->ctype->bit_size, new, value);
974 return new;
977 static int show_fvalue(struct expression *expr)
979 int new = new_pseudo();
980 long double value = expr->fvalue;
982 printf("\tmovf.%d\t\tv%d,$%Lf\n", expr->ctype->bit_size, new, value);
983 return new;
986 static int show_string_expr(struct expression *expr)
988 int new = new_pseudo();
990 printf("\tmovi.%d\t\tv%d,&%s\n", bits_in_pointer, new, show_string(expr->string));
991 return new;
994 static int show_label_expr(struct expression *expr)
996 int new = new_pseudo();
997 printf("\tmovi.%d\t\tv%d,.L%p\n",bits_in_pointer, new, expr->label_symbol);
998 return new;
1001 static int show_conditional_expr(struct expression *expr)
1003 int cond = show_expression(expr->conditional);
1004 int true = show_expression(expr->cond_true);
1005 int false = show_expression(expr->cond_false);
1006 int new = new_pseudo();
1008 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond, expr->ctype->bit_size, new, true, false);
1009 return new;
1012 static int show_statement_expr(struct expression *expr)
1014 return show_statement(expr->statement);
1017 static int show_position_expr(struct expression *expr, struct symbol *base)
1019 int new = show_expression(expr->init_expr);
1020 struct symbol *ctype = expr->init_expr->ctype;
1021 int bit_offset;
1023 bit_offset = ctype ? ctype->bit_offset : -1;
1025 printf("\tinsert v%d at [%d:%d] of %s\n", new,
1026 expr->init_offset, bit_offset,
1027 show_ident(base->ident));
1028 return 0;
1031 static int show_initializer_expr(struct expression *expr, struct symbol *ctype)
1033 struct expression *entry;
1035 FOR_EACH_PTR(expr->expr_list, entry) {
1037 again:
1038 // Nested initializers have their positions already
1039 // recursively calculated - just output them too
1040 if (entry->type == EXPR_INITIALIZER) {
1041 show_initializer_expr(entry, ctype);
1042 continue;
1045 // Initializer indexes and identifiers should
1046 // have been evaluated to EXPR_POS
1047 if (entry->type == EXPR_IDENTIFIER) {
1048 printf(" AT '%s':\n", show_ident(entry->expr_ident));
1049 entry = entry->ident_expression;
1050 goto again;
1053 if (entry->type == EXPR_INDEX) {
1054 printf(" AT '%d..%d:\n", entry->idx_from, entry->idx_to);
1055 entry = entry->idx_expression;
1056 goto again;
1058 if (entry->type == EXPR_POS) {
1059 show_position_expr(entry, ctype);
1060 continue;
1062 show_initialization(ctype, entry);
1063 } END_FOR_EACH_PTR(entry);
1064 return 0;
1067 int show_symbol_expr_init(struct symbol *sym)
1069 struct expression *expr = sym->initializer;
1071 if (expr)
1072 show_expression(expr);
1073 return show_symbol_expr(sym);
1077 * Print out an expression. Return the pseudo that contains the
1078 * variable.
1080 int show_expression(struct expression *expr)
1082 if (!expr)
1083 return 0;
1085 if (!expr->ctype) {
1086 struct position *pos = &expr->pos;
1087 printf("\tno type at %s:%d:%d\n",
1088 stream_name(pos->stream),
1089 pos->line, pos->pos);
1090 return 0;
1093 switch (expr->type) {
1094 case EXPR_CALL:
1095 return show_call_expression(expr);
1097 case EXPR_ASSIGNMENT:
1098 return show_assignment(expr);
1100 case EXPR_COMMA:
1101 return show_comma(expr);
1102 case EXPR_BINOP:
1103 case EXPR_COMPARE:
1104 case EXPR_LOGICAL:
1105 return show_binop(expr);
1106 case EXPR_PREOP:
1107 return show_preop(expr);
1108 case EXPR_POSTOP:
1109 return show_postop(expr);
1110 case EXPR_SYMBOL:
1111 return show_symbol_expr(expr->symbol);
1112 case EXPR_DEREF:
1113 case EXPR_SIZEOF:
1114 case EXPR_PTRSIZEOF:
1115 case EXPR_ALIGNOF:
1116 case EXPR_OFFSETOF:
1117 warning(expr->pos, "invalid expression after evaluation");
1118 return 0;
1119 case EXPR_CAST:
1120 case EXPR_FORCE_CAST:
1121 case EXPR_IMPLIED_CAST:
1122 return show_cast_expr(expr);
1123 case EXPR_VALUE:
1124 return show_value(expr);
1125 case EXPR_FVALUE:
1126 return show_fvalue(expr);
1127 case EXPR_STRING:
1128 return show_string_expr(expr);
1129 case EXPR_INITIALIZER:
1130 return show_initializer_expr(expr, expr->ctype);
1131 case EXPR_SELECT:
1132 case EXPR_CONDITIONAL:
1133 return show_conditional_expr(expr);
1134 case EXPR_STATEMENT:
1135 return show_statement_expr(expr);
1136 case EXPR_LABEL:
1137 return show_label_expr(expr);
1138 case EXPR_SLICE:
1139 return show_slice(expr);
1141 // None of these should exist as direct expressions: they are only
1142 // valid as sub-expressions of initializers.
1143 case EXPR_POS:
1144 warning(expr->pos, "unable to show plain initializer position expression");
1145 return 0;
1146 case EXPR_IDENTIFIER:
1147 warning(expr->pos, "unable to show identifier expression");
1148 return 0;
1149 case EXPR_INDEX:
1150 warning(expr->pos, "unable to show index expression");
1151 return 0;
1152 case EXPR_TYPE:
1153 warning(expr->pos, "unable to show type expression");
1154 return 0;
1156 return 0;