math: array parameters can be NULL
[smatch.git] / show-parse.c
blob51a151911e3ba1db461e2aba44fa6d0c8b27b29d
1 /*
2 * sparse/show-parse.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
25 * Print out results of parsing for debugging and testing.
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <unistd.h>
33 #include <fcntl.h>
35 #include "lib.h"
36 #include "allocate.h"
37 #include "token.h"
38 #include "parse.h"
39 #include "symbol.h"
40 #include "scope.h"
41 #include "expression.h"
42 #include "target.h"
44 static int show_symbol_expr(struct symbol *sym);
45 static int show_string_expr(struct expression *expr);
47 static void do_debug_symbol(struct symbol *sym, int indent)
49 static const char indent_string[] = " ";
50 static const char *typestr[] = {
51 [SYM_UNINITIALIZED] = "none",
52 [SYM_PREPROCESSOR] = "cpp.",
53 [SYM_BASETYPE] = "base",
54 [SYM_NODE] = "node",
55 [SYM_PTR] = "ptr.",
56 [SYM_FN] = "fn..",
57 [SYM_ARRAY] = "arry",
58 [SYM_STRUCT] = "strt",
59 [SYM_UNION] = "unin",
60 [SYM_ENUM] = "enum",
61 [SYM_TYPEOF] = "tpof",
62 [SYM_BITFIELD] = "bitf",
63 [SYM_LABEL] = "labl",
64 [SYM_RESTRICT] = "rstr",
65 [SYM_FOULED] = "foul",
66 [SYM_BAD] = "bad.",
68 struct context *context;
69 int i;
71 if (!sym)
72 return;
73 fprintf(stderr, "%.*s%s%3d:%lu %s %s (as: %s) %p (%s:%d:%d) %s\n",
74 indent, indent_string, typestr[sym->type],
75 sym->bit_size, sym->ctype.alignment,
76 modifier_string(sym->ctype.modifiers), show_ident(sym->ident),
77 show_as(sym->ctype.as),
78 sym, stream_name(sym->pos.stream), sym->pos.line, sym->pos.pos,
79 builtin_typename(sym) ?: "");
80 i = 0;
81 FOR_EACH_PTR(sym->ctype.contexts, context) {
82 /* FIXME: should print context expression */
83 fprintf(stderr, "< context%d: in=%d, out=%d\n",
84 i, context->in, context->out);
85 fprintf(stderr, " end context%d >\n", i);
86 i++;
87 } END_FOR_EACH_PTR(context);
88 if (sym->type == SYM_FN) {
89 struct symbol *arg;
90 i = 0;
91 FOR_EACH_PTR(sym->arguments, arg) {
92 fprintf(stderr, "< arg%d:\n", i);
93 do_debug_symbol(arg, 0);
94 fprintf(stderr, " end arg%d >\n", i);
95 i++;
96 } END_FOR_EACH_PTR(arg);
98 do_debug_symbol(sym->ctype.base_type, indent+2);
101 void debug_symbol(struct symbol *sym)
103 do_debug_symbol(sym, 0);
107 * Symbol type printout. The type system is by far the most
108 * complicated part of C - everything else is trivial.
110 const char *modifier_string(unsigned long mod)
112 static char buffer[100];
113 int len = 0;
114 int i;
115 struct mod_name {
116 unsigned long mod;
117 const char *name;
118 } *m;
120 static struct mod_name mod_names[] = {
121 {MOD_AUTO, "auto"},
122 {MOD_EXTERN, "extern"},
123 {MOD_REGISTER, "register"},
124 {MOD_STATIC, "static"},
125 {MOD_INLINE, "inline"},
126 {MOD_CONST, "const"},
127 {MOD_RESTRICT, "restrict"},
128 {MOD_VOLATILE, "volatile"},
129 {MOD_ADDRESSABLE, "[addressable]"},
130 {MOD_ASSIGNED, "[assigned]"},
131 {MOD_ATOMIC, "[atomic]"},
132 {MOD_BITWISE, "[bitwise]"},
133 {MOD_EXPLICITLY_SIGNED, "[explicitly-signed]"},
134 {MOD_GNU_INLINE, "[gnu_inline]"},
135 {MOD_NOCAST, "[nocast]"},
136 {MOD_NODEREF, "[noderef]"},
137 {MOD_NORETURN, "[noreturn]"},
138 {MOD_PURE, "[pure]"},
139 {MOD_SAFE, "[safe]"},
140 {MOD_SIGNED, "[signed]"},
141 {MOD_TLS, "[tls]"},
142 {MOD_TOPLEVEL, "[toplevel]"},
143 {MOD_UNSIGNED, "[unsigned]"},
144 {MOD_UNUSED, "[unused]"},
145 {MOD_USERTYPE, "[usertype]"},
148 for (i = 0; i < ARRAY_SIZE(mod_names); i++) {
149 m = mod_names + i;
150 if (mod & m->mod) {
151 char c;
152 const char *name = m->name;
153 while ((c = *name++) != '\0' && len + 2 < sizeof buffer)
154 buffer[len++] = c;
155 buffer[len++] = ' ';
158 buffer[len] = 0;
159 return buffer;
162 static void show_struct_member(struct symbol *sym)
164 printf("\t%s:%d:%ld at offset %ld.%d", show_ident(sym->ident), sym->bit_size, sym->ctype.alignment, sym->offset, sym->bit_offset);
165 printf("\n");
168 void show_symbol_list(struct symbol_list *list, const char *sep)
170 struct symbol *sym;
171 const char *prepend = "";
173 FOR_EACH_PTR(list, sym) {
174 puts(prepend);
175 prepend = ", ";
176 show_symbol(sym);
177 } END_FOR_EACH_PTR(sym);
180 const char *show_as(struct ident *as)
182 if (!as)
183 return "";
184 return show_ident(as);
187 struct type_name {
188 char *start;
189 char *end;
192 static void FORMAT_ATTR(2) prepend(struct type_name *name, const char *fmt, ...)
194 static char buffer[512];
195 int n;
197 va_list args;
198 va_start(args, fmt);
199 n = vsprintf(buffer, fmt, args);
200 va_end(args);
202 name->start -= n;
203 memcpy(name->start, buffer, n);
206 static void FORMAT_ATTR(2) append(struct type_name *name, const char *fmt, ...)
208 static char buffer[512];
209 int n;
211 va_list args;
212 va_start(args, fmt);
213 n = vsprintf(buffer, fmt, args);
214 va_end(args);
216 memcpy(name->end, buffer, n);
217 name->end += n;
220 static struct ctype_name {
221 struct symbol *sym;
222 const char *name;
223 const char *suffix;
224 } typenames[] = {
225 { & char_ctype, "char", "" },
226 { &schar_ctype, "signed char", "" },
227 { &uchar_ctype, "unsigned char", "" },
228 { & short_ctype, "short", "" },
229 { &sshort_ctype, "signed short", "" },
230 { &ushort_ctype, "unsigned short", "" },
231 { & int_ctype, "int", "" },
232 { &sint_ctype, "signed int", "" },
233 { &uint_ctype, "unsigned int", "U" },
234 { & long_ctype, "long", "L" },
235 { &slong_ctype, "signed long", "L" },
236 { &ulong_ctype, "unsigned long", "UL" },
237 { & llong_ctype, "long long", "LL" },
238 { &sllong_ctype, "signed long long", "LL" },
239 { &ullong_ctype, "unsigned long long", "ULL" },
240 { & int128_ctype, "__int128", "" },
241 { &sint128_ctype, "signed __int128", "" },
242 { &uint128_ctype, "unsigned __int128", "" },
244 { &void_ctype, "void", "" },
245 { &bool_ctype, "bool", "" },
247 { &float_ctype, "float", "F" },
248 { &double_ctype, "double", "" },
249 { &ldouble_ctype,"long double", "L" },
250 { &incomplete_ctype, "incomplete type", "" },
251 { &int_type, "abstract int", "" },
252 { &fp_type, "abstract fp", "" },
253 { &label_ctype, "label type", "" },
254 { &bad_ctype, "bad type", "" },
257 const char *builtin_typename(struct symbol *sym)
259 int i;
261 for (i = 0; i < ARRAY_SIZE(typenames); i++)
262 if (typenames[i].sym == sym)
263 return typenames[i].name;
264 return NULL;
267 const char *builtin_type_suffix(struct symbol *sym)
269 int i;
271 for (i = 0; i < ARRAY_SIZE(typenames); i++)
272 if (typenames[i].sym == sym)
273 return typenames[i].suffix;
274 return NULL;
277 const char *builtin_ctypename(struct ctype *ctype)
279 int i;
281 for (i = 0; i < ARRAY_SIZE(typenames); i++)
282 if (&typenames[i].sym->ctype == ctype)
283 return typenames[i].name;
284 return NULL;
287 static void do_show_type(struct symbol *sym, struct type_name *name)
289 const char *typename;
290 unsigned long mod = 0;
291 struct ident *as = NULL;
292 int was_ptr = 0;
293 int restr = 0;
294 int fouled = 0;
296 deeper:
297 if (sym && (sym->type != SYM_NODE && sym->type != SYM_ARRAY &&
298 sym->type != SYM_BITFIELD)) {
299 const char *s;
300 size_t len;
302 if (as)
303 prepend(name, "%s ", show_as(as));
305 if (sym && (sym->type == SYM_BASETYPE || sym->type == SYM_ENUM))
306 mod &= ~MOD_SPECIFIER;
307 s = modifier_string(mod);
308 len = strlen(s);
309 name->start -= len;
310 memcpy(name->start, s, len);
311 mod = 0;
312 as = NULL;
315 if (!sym)
316 goto out;
318 if ((typename = builtin_typename(sym))) {
319 int len = strlen(typename);
320 if (name->start != name->end)
321 *--name->start = ' ';
322 name->start -= len;
323 memcpy(name->start, typename, len);
324 goto out;
327 /* Prepend */
328 switch (sym->type) {
329 case SYM_PTR:
330 prepend(name, "*");
331 mod = sym->ctype.modifiers;
332 as = sym->ctype.as;
333 was_ptr = 1;
334 examine_pointer_target(sym);
335 break;
337 case SYM_FN:
338 if (was_ptr) {
339 prepend(name, "( ");
340 append(name, " )");
341 was_ptr = 0;
343 append(name, "( ... )");
344 break;
346 case SYM_STRUCT:
347 if (name->start != name->end)
348 *--name->start = ' ';
349 prepend(name, "struct %s", show_ident(sym->ident));
350 goto out;
352 case SYM_UNION:
353 if (name->start != name->end)
354 *--name->start = ' ';
355 prepend(name, "union %s", show_ident(sym->ident));
356 goto out;
358 case SYM_ENUM:
359 prepend(name, "enum %s ", show_ident(sym->ident));
360 break;
362 case SYM_NODE:
363 if (sym->ident)
364 append(name, "%s", show_ident(sym->ident));
365 mod |= sym->ctype.modifiers;
366 combine_address_space(sym->pos, &as, sym->ctype.as);
367 break;
369 case SYM_BITFIELD:
370 mod |= sym->ctype.modifiers;
371 combine_address_space(sym->pos, &as, sym->ctype.as);
372 append(name, ":%d", sym->bit_size);
373 break;
375 case SYM_LABEL:
376 append(name, "label(%s:%p)", show_ident(sym->ident), sym);
377 return;
379 case SYM_ARRAY:
380 mod |= sym->ctype.modifiers;
381 combine_address_space(sym->pos, &as, sym->ctype.as);
382 if (was_ptr) {
383 prepend(name, "( ");
384 append(name, " )");
385 was_ptr = 0;
387 append(name, "[%lld]", get_expression_value(sym->array_size));
388 break;
390 case SYM_RESTRICT:
391 if (!sym->ident) {
392 restr = 1;
393 break;
395 if (name->start != name->end)
396 *--name->start = ' ';
397 prepend(name, "restricted %s", show_ident(sym->ident));
398 goto out;
400 case SYM_FOULED:
401 fouled = 1;
402 break;
404 default:
405 if (name->start != name->end)
406 *--name->start = ' ';
407 prepend(name, "unknown type %d", sym->type);
408 goto out;
411 sym = sym->ctype.base_type;
412 goto deeper;
414 out:
415 if (restr)
416 prepend(name, "restricted ");
417 if (fouled)
418 prepend(name, "fouled ");
420 // strip trailing space
421 if (name->end > name->start && name->end[-1] == ' ')
422 name->end--;
425 void show_type(struct symbol *sym)
427 char array[200];
428 struct type_name name;
430 name.start = name.end = array+100;
431 do_show_type(sym, &name);
432 *name.end = 0;
433 printf("%s", name.start);
436 const char *show_typename(struct symbol *sym)
438 static char array[200];
439 struct type_name name;
441 name.start = name.end = array+100;
442 do_show_type(sym, &name);
443 *name.end = 0;
444 return name.start;
447 void show_symbol(struct symbol *sym)
449 struct symbol *type;
451 if (!sym)
452 return;
454 if (sym->ctype.alignment)
455 printf(".align %ld\n", sym->ctype.alignment);
457 show_type(sym);
458 type = sym->ctype.base_type;
459 if (!type) {
460 printf("\n");
461 return;
465 * Show actual implementation information
467 switch (type->type) {
468 struct symbol *member;
470 case SYM_STRUCT:
471 case SYM_UNION:
472 printf(" {\n");
473 FOR_EACH_PTR(type->symbol_list, member) {
474 show_struct_member(member);
475 } END_FOR_EACH_PTR(member);
476 printf("}\n");
477 break;
479 case SYM_FN: {
480 struct statement *stmt = type->stmt;
481 printf("\n");
482 if (stmt) {
483 int val;
484 val = show_statement(stmt);
485 if (val)
486 printf("\tmov.%d\t\tretval,%d\n", stmt->ret->bit_size, val);
487 printf("\tret\n");
489 break;
492 default:
493 printf("\n");
494 break;
497 if (sym->initializer) {
498 printf(" = \n");
499 show_expression(sym->initializer);
503 static int show_symbol_init(struct symbol *sym);
505 static int new_pseudo(void)
507 static int nr = 0;
508 return ++nr;
511 static int new_label(void)
513 static int label = 0;
514 return ++label;
517 static void show_switch_statement(struct statement *stmt)
519 int val = show_expression(stmt->switch_expression);
520 struct symbol *sym;
521 printf("\tswitch v%d\n", val);
524 * Debugging only: Check that the case list is correct
525 * by printing it out.
527 * This is where a _real_ back-end would go through the
528 * cases to decide whether to use a lookup table or a
529 * series of comparisons etc
531 printf("# case table:\n");
532 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
533 struct statement *case_stmt = sym->stmt;
534 struct expression *expr = case_stmt->case_expression;
535 struct expression *to = case_stmt->case_to;
537 if (!expr) {
538 printf(" default");
539 } else {
540 if (expr->type == EXPR_VALUE) {
541 printf(" case %lld", expr->value);
542 if (to) {
543 if (to->type == EXPR_VALUE) {
544 printf(" .. %lld", to->value);
545 } else {
546 printf(" .. what?");
549 } else
550 printf(" what?");
552 printf(": .L%p\n", sym);
553 } END_FOR_EACH_PTR(sym);
554 printf("# end case table\n");
556 show_statement(stmt->switch_statement);
558 if (stmt->switch_break->used)
559 printf(".L%p:\n", stmt->switch_break);
562 static void show_symbol_decl(struct symbol_list *syms)
564 struct symbol *sym;
565 FOR_EACH_PTR(syms, sym) {
566 show_symbol_init(sym);
567 } END_FOR_EACH_PTR(sym);
570 static int show_return_stmt(struct statement *stmt);
573 * Print out a statement
575 int show_statement(struct statement *stmt)
577 if (!stmt)
578 return 0;
579 switch (stmt->type) {
580 case STMT_DECLARATION:
581 show_symbol_decl(stmt->declaration);
582 return 0;
583 case STMT_RETURN:
584 return show_return_stmt(stmt);
585 case STMT_COMPOUND: {
586 struct statement *s;
587 int last = 0;
589 if (stmt->inline_fn) {
590 show_statement(stmt->args);
591 printf("\tbegin_inline \t%s\n", show_ident(stmt->inline_fn->ident));
593 FOR_EACH_PTR(stmt->stmts, s) {
594 last = show_statement(s);
595 } END_FOR_EACH_PTR(s);
596 if (stmt->ret) {
597 int addr, bits;
598 printf(".L%p:\n", stmt->ret);
599 addr = show_symbol_expr(stmt->ret);
600 bits = stmt->ret->bit_size;
601 last = new_pseudo();
602 printf("\tld.%d\t\tv%d,[v%d]\n", bits, last, addr);
604 if (stmt->inline_fn)
605 printf("\tend_inlined\t%s\n", show_ident(stmt->inline_fn->ident));
606 return last;
609 case STMT_EXPRESSION:
610 return show_expression(stmt->expression);
611 case STMT_IF: {
612 int val, target;
613 struct expression *cond = stmt->if_conditional;
615 /* This is only valid if nobody can jump into the "dead" statement */
616 #if 0
617 if (cond->type == EXPR_VALUE) {
618 struct statement *s = stmt->if_true;
619 if (!cond->value)
620 s = stmt->if_false;
621 show_statement(s);
622 break;
624 #endif
625 val = show_expression(cond);
626 target = new_label();
627 printf("\tje\t\tv%d,.L%d\n", val, target);
628 show_statement(stmt->if_true);
629 if (stmt->if_false) {
630 int last = new_label();
631 printf("\tjmp\t\t.L%d\n", last);
632 printf(".L%d:\n", target);
633 target = last;
634 show_statement(stmt->if_false);
636 printf(".L%d:\n", target);
637 break;
639 case STMT_SWITCH:
640 show_switch_statement(stmt);
641 break;
643 case STMT_CASE:
644 printf(".L%p:\n", stmt->case_label);
645 show_statement(stmt->case_statement);
646 break;
648 case STMT_ITERATOR: {
649 struct statement *pre_statement = stmt->iterator_pre_statement;
650 struct expression *pre_condition = stmt->iterator_pre_condition;
651 struct statement *statement = stmt->iterator_statement;
652 struct statement *post_statement = stmt->iterator_post_statement;
653 struct expression *post_condition = stmt->iterator_post_condition;
654 int val, loop_top = 0, loop_bottom = 0;
656 show_symbol_decl(stmt->iterator_syms);
657 show_statement(pre_statement);
658 if (pre_condition) {
659 if (pre_condition->type == EXPR_VALUE) {
660 if (!pre_condition->value) {
661 loop_bottom = new_label();
662 printf("\tjmp\t\t.L%d\n", loop_bottom);
664 } else {
665 loop_bottom = new_label();
666 val = show_expression(pre_condition);
667 printf("\tje\t\tv%d, .L%d\n", val, loop_bottom);
670 if (!post_condition || post_condition->type != EXPR_VALUE || post_condition->value) {
671 loop_top = new_label();
672 printf(".L%d:\n", loop_top);
674 show_statement(statement);
675 if (stmt->iterator_continue->used)
676 printf(".L%p:\n", stmt->iterator_continue);
677 show_statement(post_statement);
678 if (!post_condition) {
679 printf("\tjmp\t\t.L%d\n", loop_top);
680 } else if (post_condition->type == EXPR_VALUE) {
681 if (post_condition->value)
682 printf("\tjmp\t\t.L%d\n", loop_top);
683 } else {
684 val = show_expression(post_condition);
685 printf("\tjne\t\tv%d, .L%d\n", val, loop_top);
687 if (stmt->iterator_break->used)
688 printf(".L%p:\n", stmt->iterator_break);
689 if (loop_bottom)
690 printf(".L%d:\n", loop_bottom);
691 break;
693 case STMT_NONE:
694 break;
696 case STMT_LABEL:
697 printf(".L%p:\n", stmt->label_identifier);
698 show_statement(stmt->label_statement);
699 break;
701 case STMT_GOTO:
702 if (stmt->goto_expression) {
703 int val = show_expression(stmt->goto_expression);
704 printf("\tgoto\t\t*v%d\n", val);
705 } else {
706 printf("\tgoto\t\t.L%p\n", stmt->goto_label);
708 break;
709 case STMT_ASM:
710 printf("\tasm( .... )\n");
711 break;
712 case STMT_CONTEXT: {
713 int val = show_expression(stmt->expression);
714 printf("\tcontext( %d )\n", val);
715 break;
717 case STMT_RANGE: {
718 int val = show_expression(stmt->range_expression);
719 int low = show_expression(stmt->range_low);
720 int high = show_expression(stmt->range_high);
721 printf("\trange( %d %d-%d)\n", val, low, high);
722 break;
725 return 0;
728 static int show_call_expression(struct expression *expr)
730 struct symbol *direct;
731 struct expression *arg, *fn;
732 int fncall, retval;
733 int framesize;
735 if (!expr->ctype) {
736 warning(expr->pos, "\tcall with no type!");
737 return 0;
740 framesize = 0;
741 FOR_EACH_PTR_REVERSE(expr->args, arg) {
742 int new = show_expression(arg);
743 int size = arg->ctype->bit_size;
744 printf("\tpush.%d\t\tv%d\n", size, new);
745 framesize += bits_to_bytes(size);
746 } END_FOR_EACH_PTR_REVERSE(arg);
748 fn = expr->fn;
750 /* Remove dereference, if any */
751 direct = NULL;
752 if (fn->type == EXPR_PREOP) {
753 if (fn->unop->type == EXPR_SYMBOL) {
754 struct symbol *sym = fn->unop->symbol;
755 if (sym->ctype.base_type->type == SYM_FN)
756 direct = sym;
759 if (direct) {
760 printf("\tcall\t\t%s\n", show_ident(direct->ident));
761 } else {
762 fncall = show_expression(fn);
763 printf("\tcall\t\t*v%d\n", fncall);
765 if (framesize)
766 printf("\tadd.%d\t\tvSP,vSP,$%d\n", bits_in_pointer, framesize);
768 retval = new_pseudo();
769 printf("\tmov.%d\t\tv%d,retval\n", expr->ctype->bit_size, retval);
770 return retval;
773 static int show_comma(struct expression *expr)
775 show_expression(expr->left);
776 return show_expression(expr->right);
779 static int show_binop(struct expression *expr)
781 int left = show_expression(expr->left);
782 int right = show_expression(expr->right);
783 int new = new_pseudo();
784 const char *opname;
785 static const char *name[] = {
786 ['+'] = "add", ['-'] = "sub",
787 ['*'] = "mul", ['/'] = "div",
788 ['%'] = "mod", ['&'] = "and",
789 ['|'] = "lor", ['^'] = "xor"
791 unsigned int op = expr->op;
793 opname = show_special(op);
794 if (op < ARRAY_SIZE(name))
795 opname = name[op];
796 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname,
797 expr->ctype->bit_size,
798 new, left, right);
799 return new;
802 static int show_slice(struct expression *expr)
804 int target = show_expression(expr->base);
805 int new = new_pseudo();
806 printf("\tslice.%d\t\tv%d,v%d,%d\n", expr->r_nrbits, target, new, expr->r_bitpos);
807 return new;
810 static int show_regular_preop(struct expression *expr)
812 int target = show_expression(expr->unop);
813 int new = new_pseudo();
814 static const char *name[] = {
815 ['!'] = "nonzero", ['-'] = "neg",
816 ['~'] = "not",
818 unsigned int op = expr->op;
819 const char *opname;
821 opname = show_special(op);
822 if (op < ARRAY_SIZE(name))
823 opname = name[op];
824 printf("\t%s.%d\t\tv%d,v%d\n", opname, expr->ctype->bit_size, new, target);
825 return new;
829 * FIXME! Not all accesses are memory loads. We should
830 * check what kind of symbol is behind the dereference.
832 static int show_address_gen(struct expression *expr)
834 return show_expression(expr->unop);
837 static int show_load_gen(int bits, struct expression *expr, int addr)
839 int new = new_pseudo();
841 printf("\tld.%d\t\tv%d,[v%d]\n", bits, new, addr);
842 return new;
845 static void show_store_gen(int bits, int value, struct expression *expr, int addr)
847 /* FIXME!!! Bitfield store! */
848 printf("\tst.%d\t\tv%d,[v%d]\n", bits, value, addr);
851 static int show_assignment(struct expression *expr)
853 struct expression *target = expr->left;
854 int val, addr, bits;
856 if (!expr->ctype)
857 return 0;
859 bits = expr->ctype->bit_size;
860 val = show_expression(expr->right);
861 addr = show_address_gen(target);
862 show_store_gen(bits, val, target, addr);
863 return val;
866 static int show_return_stmt(struct statement *stmt)
868 struct expression *expr = stmt->ret_value;
869 struct symbol *target = stmt->ret_target;
871 if (expr && expr->ctype) {
872 int val = show_expression(expr);
873 int bits = expr->ctype->bit_size;
874 int addr = show_symbol_expr(target);
875 show_store_gen(bits, val, NULL, addr);
877 printf("\tret\t\t(%p)\n", target);
878 return 0;
881 static int show_initialization(struct symbol *sym, struct expression *expr)
883 int val, addr, bits;
885 if (!expr->ctype)
886 return 0;
888 bits = expr->ctype->bit_size;
889 val = show_expression(expr);
890 addr = show_symbol_expr(sym);
891 // FIXME! The "target" expression is for bitfield store information.
892 // Leave it NULL, which works fine.
893 show_store_gen(bits, val, NULL, addr);
894 return 0;
897 static int show_access(struct expression *expr)
899 int addr = show_address_gen(expr);
900 return show_load_gen(expr->ctype->bit_size, expr, addr);
903 static int show_inc_dec(struct expression *expr, int postop)
905 int addr = show_address_gen(expr->unop);
906 int retval, new;
907 const char *opname = expr->op == SPECIAL_INCREMENT ? "add" : "sub";
908 int bits = expr->ctype->bit_size;
910 retval = show_load_gen(bits, expr->unop, addr);
911 new = retval;
912 if (postop)
913 new = new_pseudo();
914 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname, bits, new, retval);
915 show_store_gen(bits, new, expr->unop, addr);
916 return retval;
919 static int show_preop(struct expression *expr)
922 * '*' is an lvalue access, and is fundamentally different
923 * from an arithmetic operation. Maybe it should have an
924 * expression type of its own..
926 if (expr->op == '*')
927 return show_access(expr);
928 if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
929 return show_inc_dec(expr, 0);
930 return show_regular_preop(expr);
933 static int show_postop(struct expression *expr)
935 return show_inc_dec(expr, 1);
938 static int show_symbol_expr(struct symbol *sym)
940 int new = new_pseudo();
942 if (sym->initializer && sym->initializer->type == EXPR_STRING)
943 return show_string_expr(sym->initializer);
945 if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC)) {
946 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer, new, show_ident(sym->ident));
947 return new;
949 if (sym->ctype.modifiers & MOD_ADDRESSABLE) {
950 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer, new, 0LL);
951 return new;
953 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer, new, show_ident(sym->ident), sym);
954 return new;
957 static int show_symbol_init(struct symbol *sym)
959 struct expression *expr = sym->initializer;
961 if (expr) {
962 int val, addr, bits;
964 bits = expr->ctype->bit_size;
965 val = show_expression(expr);
966 addr = show_symbol_expr(sym);
967 show_store_gen(bits, val, NULL, addr);
969 return 0;
972 static int show_cast_expr(struct expression *expr)
974 struct symbol *old_type, *new_type;
975 int op = show_expression(expr->cast_expression);
976 int oldbits, newbits;
977 int new, is_signed;
979 old_type = expr->cast_expression->ctype;
980 new_type = expr->cast_type;
982 oldbits = old_type->bit_size;
983 newbits = new_type->bit_size;
984 if (oldbits >= newbits)
985 return op;
986 new = new_pseudo();
987 is_signed = is_signed_type(old_type);
988 if (is_signed) {
989 printf("\tsext%d.%d\tv%d,v%d\n", oldbits, newbits, new, op);
990 } else {
991 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits, new, op, (1UL << oldbits)-1);
993 return new;
996 static int show_value(struct expression *expr)
998 int new = new_pseudo();
999 unsigned long long value = expr->value;
1001 printf("\tmovi.%d\t\tv%d,$%llu\n", expr->ctype->bit_size, new, value);
1002 return new;
1005 static int show_fvalue(struct expression *expr)
1007 int new = new_pseudo();
1008 long double value = expr->fvalue;
1010 printf("\tmovf.%d\t\tv%d,$%Le\n", expr->ctype->bit_size, new, value);
1011 return new;
1014 static int show_string_expr(struct expression *expr)
1016 int new = new_pseudo();
1018 printf("\tmovi.%d\t\tv%d,&%s\n", bits_in_pointer, new, show_string(expr->string));
1019 return new;
1022 static int show_label_expr(struct expression *expr)
1024 int new = new_pseudo();
1025 printf("\tmovi.%d\t\tv%d,.L%p\n",bits_in_pointer, new, expr->label_symbol);
1026 return new;
1029 static int show_conditional_expr(struct expression *expr)
1031 int cond = show_expression(expr->conditional);
1032 int valt = show_expression(expr->cond_true);
1033 int valf = show_expression(expr->cond_false);
1034 int new = new_pseudo();
1036 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond, expr->ctype->bit_size, new, valt, valf);
1037 return new;
1040 static int show_statement_expr(struct expression *expr)
1042 return show_statement(expr->statement);
1045 static int show_position_expr(struct expression *expr, struct symbol *base)
1047 int new = show_expression(expr->init_expr);
1048 struct symbol *ctype = expr->init_expr->ctype;
1049 int bit_offset;
1051 bit_offset = ctype ? ctype->bit_offset : -1;
1053 printf("\tinsert v%d at [%d:%d] of %s\n", new,
1054 expr->init_offset, bit_offset,
1055 show_ident(base->ident));
1056 return 0;
1059 static int show_initializer_expr(struct expression *expr, struct symbol *ctype)
1061 struct expression *entry;
1063 FOR_EACH_PTR(expr->expr_list, entry) {
1065 again:
1066 // Nested initializers have their positions already
1067 // recursively calculated - just output them too
1068 if (entry->type == EXPR_INITIALIZER) {
1069 show_initializer_expr(entry, ctype);
1070 continue;
1073 // Initializer indexes and identifiers should
1074 // have been evaluated to EXPR_POS
1075 if (entry->type == EXPR_IDENTIFIER) {
1076 printf(" AT '%s':\n", show_ident(entry->expr_ident));
1077 entry = entry->ident_expression;
1078 goto again;
1081 if (entry->type == EXPR_INDEX) {
1082 printf(" AT '%d..%d:\n", entry->idx_from, entry->idx_to);
1083 entry = entry->idx_expression;
1084 goto again;
1086 if (entry->type == EXPR_POS) {
1087 show_position_expr(entry, ctype);
1088 continue;
1090 show_initialization(ctype, entry);
1091 } END_FOR_EACH_PTR(entry);
1092 return 0;
1095 int show_symbol_expr_init(struct symbol *sym)
1097 struct expression *expr = sym->initializer;
1099 if (expr)
1100 show_expression(expr);
1101 return show_symbol_expr(sym);
1105 * Print out an expression. Return the pseudo that contains the
1106 * variable.
1108 int show_expression(struct expression *expr)
1110 if (!expr)
1111 return 0;
1113 if (!expr->ctype) {
1114 struct position *pos = &expr->pos;
1115 printf("\tno type at %s:%d:%d\n",
1116 stream_name(pos->stream),
1117 pos->line, pos->pos);
1118 return 0;
1121 switch (expr->type) {
1122 case EXPR_CALL:
1123 return show_call_expression(expr);
1125 case EXPR_ASSIGNMENT:
1126 return show_assignment(expr);
1128 case EXPR_COMMA:
1129 return show_comma(expr);
1130 case EXPR_BINOP:
1131 case EXPR_COMPARE:
1132 case EXPR_LOGICAL:
1133 return show_binop(expr);
1134 case EXPR_PREOP:
1135 return show_preop(expr);
1136 case EXPR_POSTOP:
1137 return show_postop(expr);
1138 case EXPR_SYMBOL:
1139 return show_symbol_expr(expr->symbol);
1140 case EXPR_DEREF:
1141 case EXPR_SIZEOF:
1142 case EXPR_PTRSIZEOF:
1143 case EXPR_ALIGNOF:
1144 case EXPR_OFFSETOF:
1145 warning(expr->pos, "invalid expression after evaluation");
1146 return 0;
1147 case EXPR_CAST:
1148 case EXPR_FORCE_CAST:
1149 case EXPR_IMPLIED_CAST:
1150 return show_cast_expr(expr);
1151 case EXPR_VALUE:
1152 return show_value(expr);
1153 case EXPR_FVALUE:
1154 return show_fvalue(expr);
1155 case EXPR_STRING:
1156 return show_string_expr(expr);
1157 case EXPR_INITIALIZER:
1158 return show_initializer_expr(expr, expr->ctype);
1159 case EXPR_SELECT:
1160 case EXPR_CONDITIONAL:
1161 return show_conditional_expr(expr);
1162 case EXPR_STATEMENT:
1163 return show_statement_expr(expr);
1164 case EXPR_LABEL:
1165 return show_label_expr(expr);
1166 case EXPR_SLICE:
1167 return show_slice(expr);
1169 // None of these should exist as direct expressions: they are only
1170 // valid as sub-expressions of initializers.
1171 case EXPR_POS:
1172 warning(expr->pos, "unable to show plain initializer position expression");
1173 return 0;
1174 case EXPR_IDENTIFIER:
1175 warning(expr->pos, "unable to show identifier expression");
1176 return 0;
1177 case EXPR_INDEX:
1178 warning(expr->pos, "unable to show index expression");
1179 return 0;
1180 case EXPR_TYPE:
1181 warning(expr->pos, "unable to show type expression");
1182 return 0;
1183 case EXPR_GENERIC:
1184 warning(expr->pos, "unable to show generic expression");
1185 return 0;
1187 return 0;