Introduce expression_error
[smatch.git] / show-parse.c
blobed6ef80ab07d47a0c888c7fdba82f31a054e74ec
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 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 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 int modlen;
232 const char *mod;
233 const char *typename;
234 if (!sym)
235 return;
237 if ((typename = builtin_typename(sym))) {
238 int len = strlen(typename);
239 *--name->start = ' ';
240 name->start -= len;
241 memcpy(name->start, typename, len);
242 return;
245 /* Prepend */
246 switch (sym->type) {
247 case SYM_PTR:
248 prepend(name, "*");
249 break;
250 case SYM_FN:
251 prepend(name, "( ");
252 break;
253 case SYM_STRUCT:
254 prepend(name, "struct %s ", show_ident(sym->ident));
255 return;
257 case SYM_UNION:
258 prepend(name, "union %s ", show_ident(sym->ident));
259 return;
261 case SYM_ENUM:
262 prepend(name, "enum %s ", show_ident(sym->ident));
263 break;
265 case SYM_NODE:
266 append(name, "%s", show_ident(sym->ident));
267 break;
269 case SYM_BITFIELD:
270 append(name, ":%d", sym->bit_size);
271 break;
273 case SYM_LABEL:
274 append(name, "label(%s:%p)", show_ident(sym->ident), sym);
275 break;
277 case SYM_ARRAY:
278 break;
280 case SYM_RESTRICT:
281 break;
283 case SYM_FOULED:
284 break;
286 default:
287 prepend(name, "unknown type %d", sym->type);
288 return;
291 mod = modifier_string(sym->ctype.modifiers);
292 modlen = strlen(mod);
293 name->start -= modlen;
294 memcpy(name->start, mod, modlen);
296 do_show_type(sym->ctype.base_type, name);
298 /* Postpend */
299 if (sym->ctype.as)
300 append(name, "<asn:%d>", sym->ctype.as);
302 switch (sym->type) {
303 case SYM_PTR:
304 return;
306 case SYM_FN:
307 append(name, " )( ... )");
308 return;
310 case SYM_ARRAY:
311 append(name, "[%lld]", get_expression_value(sym->array_size));
312 return;
314 case SYM_RESTRICT:
315 prepend(name, "restricted ");
316 return;
318 case SYM_FOULED:
319 prepend(name, "fouled ");
320 return;
322 default:
323 break;
327 void show_type(struct symbol *sym)
329 char array[200];
330 struct type_name name;
332 name.start = name.end = array+100;
333 do_show_type(sym, &name);
334 *name.end = 0;
335 printf("%s", name.start);
338 const char *show_typename(struct symbol *sym)
340 static char array[200];
341 struct type_name name;
343 name.start = name.end = array+100;
344 do_show_type(sym, &name);
345 *name.end = 0;
346 return name.start;
349 void show_symbol(struct symbol *sym)
351 struct symbol *type;
353 if (!sym)
354 return;
356 if (sym->ctype.alignment)
357 printf(".align %ld\n", sym->ctype.alignment);
359 show_type(sym);
360 type = sym->ctype.base_type;
361 if (!type)
362 return;
365 * Show actual implementation information
367 switch (type->type) {
368 struct symbol *member;
370 case SYM_STRUCT:
371 case SYM_UNION:
372 printf(" {\n");
373 FOR_EACH_PTR(type->symbol_list, member) {
374 show_struct_member(member);
375 } END_FOR_EACH_PTR(member);
376 printf("}\n");
377 break;
379 case SYM_FN: {
380 struct statement *stmt = type->stmt;
381 if (stmt) {
382 int val;
383 printf("\n");
384 val = show_statement(stmt);
385 if (val)
386 printf("\tmov.%d\t\tretval,%d\n", stmt->ret->bit_size, val);
387 printf("\tret\n");
389 break;
392 default:
393 break;
396 if (sym->initializer) {
397 printf(" = \n");
398 show_expression(sym->initializer);
402 static int show_symbol_init(struct symbol *sym);
404 static int new_pseudo(void)
406 static int nr = 0;
407 return ++nr;
410 static int new_label(void)
412 static int label = 0;
413 return ++label;
416 static void show_switch_statement(struct statement *stmt)
418 int val = show_expression(stmt->switch_expression);
419 struct symbol *sym;
420 printf("\tswitch v%d\n", val);
423 * Debugging only: Check that the case list is correct
424 * by printing it out.
426 * This is where a _real_ back-end would go through the
427 * cases to decide whether to use a lookup table or a
428 * series of comparisons etc
430 printf("# case table:\n");
431 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
432 struct statement *case_stmt = sym->stmt;
433 struct expression *expr = case_stmt->case_expression;
434 struct expression *to = case_stmt->case_to;
436 if (!expr) {
437 printf(" default");
438 } else {
439 if (expr->type == EXPR_VALUE) {
440 printf(" case %lld", expr->value);
441 if (to) {
442 if (to->type == EXPR_VALUE) {
443 printf(" .. %lld", to->value);
444 } else {
445 printf(" .. what?");
448 } else
449 printf(" what?");
451 printf(": .L%p\n", sym->bb_target);
452 } END_FOR_EACH_PTR(sym);
453 printf("# end case table\n");
455 show_statement(stmt->switch_statement);
457 if (stmt->switch_break->used)
458 printf(".L%p:\n", stmt->switch_break->bb_target);
461 static void show_symbol_decl(struct symbol_list *syms)
463 struct symbol *sym;
464 FOR_EACH_PTR(syms, sym) {
465 show_symbol_init(sym);
466 } END_FOR_EACH_PTR(sym);
469 static int show_return_stmt(struct statement *stmt);
472 * Print out a statement
474 int show_statement(struct statement *stmt)
476 if (!stmt)
477 return 0;
478 switch (stmt->type) {
479 case STMT_DECLARATION:
480 show_symbol_decl(stmt->declaration);
481 return 0;
482 case STMT_RETURN:
483 return show_return_stmt(stmt);
484 case STMT_COMPOUND: {
485 struct statement *s;
486 int last = 0;
488 FOR_EACH_PTR(stmt->stmts, s) {
489 last = show_statement(s);
490 } END_FOR_EACH_PTR(s);
491 if (stmt->ret) {
492 int addr, bits;
493 printf(".L%p:\n", stmt->ret);
494 addr = show_symbol_expr(stmt->ret);
495 bits = stmt->ret->bit_size;
496 last = new_pseudo();
497 printf("\tld.%d\t\tv%d,[v%d]\n", bits, last, addr);
499 return last;
502 case STMT_EXPRESSION:
503 return show_expression(stmt->expression);
504 case STMT_IF: {
505 int val, target;
506 struct expression *cond = stmt->if_conditional;
508 /* This is only valid if nobody can jump into the "dead" statement */
509 #if 0
510 if (cond->type == EXPR_VALUE) {
511 struct statement *s = stmt->if_true;
512 if (!cond->value)
513 s = stmt->if_false;
514 show_statement(s);
515 break;
517 #endif
518 val = show_expression(cond);
519 target = new_label();
520 printf("\tje\t\tv%d,.L%d\n", val, target);
521 show_statement(stmt->if_true);
522 if (stmt->if_false) {
523 int last = new_label();
524 printf("\tjmp\t\t.L%d\n", last);
525 printf(".L%d:\n", target);
526 target = last;
527 show_statement(stmt->if_false);
529 printf(".L%d:\n", target);
530 break;
532 case STMT_SWITCH:
533 show_switch_statement(stmt);
534 break;
536 case STMT_CASE:
537 printf(".L%p:\n", stmt->case_label);
538 show_statement(stmt->case_statement);
539 break;
541 case STMT_ITERATOR: {
542 struct statement *pre_statement = stmt->iterator_pre_statement;
543 struct expression *pre_condition = stmt->iterator_pre_condition;
544 struct statement *statement = stmt->iterator_statement;
545 struct statement *post_statement = stmt->iterator_post_statement;
546 struct expression *post_condition = stmt->iterator_post_condition;
547 int val, loop_top = 0, loop_bottom = 0;
549 show_symbol_decl(stmt->iterator_syms);
550 show_statement(pre_statement);
551 if (pre_condition) {
552 if (pre_condition->type == EXPR_VALUE) {
553 if (!pre_condition->value) {
554 loop_bottom = new_label();
555 printf("\tjmp\t\t.L%d\n", loop_bottom);
557 } else {
558 loop_bottom = new_label();
559 val = show_expression(pre_condition);
560 printf("\tje\t\tv%d, .L%d\n", val, loop_bottom);
563 if (!post_condition || post_condition->type != EXPR_VALUE || post_condition->value) {
564 loop_top = new_label();
565 printf(".L%d:\n", loop_top);
567 show_statement(statement);
568 if (stmt->iterator_continue->used)
569 printf(".L%p:\n", stmt->iterator_continue);
570 show_statement(post_statement);
571 if (!post_condition) {
572 printf("\tjmp\t\t.L%d\n", loop_top);
573 } else if (post_condition->type == EXPR_VALUE) {
574 if (post_condition->value)
575 printf("\tjmp\t\t.L%d\n", loop_top);
576 } else {
577 val = show_expression(post_condition);
578 printf("\tjne\t\tv%d, .L%d\n", val, loop_top);
580 if (stmt->iterator_break->used)
581 printf(".L%p:\n", stmt->iterator_break);
582 if (loop_bottom)
583 printf(".L%d:\n", loop_bottom);
584 break;
586 case STMT_NONE:
587 break;
589 case STMT_LABEL:
590 printf(".L%p:\n", stmt->label_identifier);
591 show_statement(stmt->label_statement);
592 break;
594 case STMT_GOTO:
595 if (stmt->goto_expression) {
596 int val = show_expression(stmt->goto_expression);
597 printf("\tgoto\t\t*v%d\n", val);
598 } else {
599 printf("\tgoto\t\t.L%p\n", stmt->goto_label->bb_target);
601 break;
602 case STMT_ASM:
603 printf("\tasm( .... )\n");
604 break;
605 case STMT_CONTEXT: {
606 int val = show_expression(stmt->expression);
607 printf("\tcontext( %d )\n", val);
608 break;
610 case STMT_RANGE: {
611 int val = show_expression(stmt->range_expression);
612 int low = show_expression(stmt->range_low);
613 int high = show_expression(stmt->range_high);
614 printf("\trange( %d %d-%d)\n", val, low, high);
615 break;
618 return 0;
621 static int show_call_expression(struct expression *expr)
623 struct symbol *direct;
624 struct expression *arg, *fn;
625 int fncall, retval;
626 int framesize;
628 if (!expr->ctype) {
629 warning(expr->pos, "\tcall with no type!");
630 return 0;
633 framesize = 0;
634 FOR_EACH_PTR_REVERSE(expr->args, arg) {
635 int new = show_expression(arg);
636 int size = arg->ctype->bit_size;
637 printf("\tpush.%d\t\tv%d\n", size, new);
638 framesize += size >> 3;
639 } END_FOR_EACH_PTR_REVERSE(arg);
641 fn = expr->fn;
643 /* Remove dereference, if any */
644 direct = NULL;
645 if (fn->type == EXPR_PREOP) {
646 if (fn->unop->type == EXPR_SYMBOL) {
647 struct symbol *sym = fn->unop->symbol;
648 if (sym->ctype.base_type->type == SYM_FN)
649 direct = sym;
652 if (direct) {
653 printf("\tcall\t\t%s\n", show_ident(direct->ident));
654 } else {
655 fncall = show_expression(fn);
656 printf("\tcall\t\t*v%d\n", fncall);
658 if (framesize)
659 printf("\tadd.%d\t\tvSP,vSP,$%d\n", bits_in_pointer, framesize);
661 retval = new_pseudo();
662 printf("\tmov.%d\t\tv%d,retval\n", expr->ctype->bit_size, retval);
663 return retval;
666 static int show_comma(struct expression *expr)
668 show_expression(expr->left);
669 return show_expression(expr->right);
672 static int show_binop(struct expression *expr)
674 int left = show_expression(expr->left);
675 int right = show_expression(expr->right);
676 int new = new_pseudo();
677 const char *opname;
678 static const char *name[] = {
679 ['+'] = "add", ['-'] = "sub",
680 ['*'] = "mul", ['/'] = "div",
681 ['%'] = "mod", ['&'] = "and",
682 ['|'] = "lor", ['^'] = "xor"
684 unsigned int op = expr->op;
686 opname = show_special(op);
687 if (op < sizeof(name)/sizeof(*name))
688 opname = name[op];
689 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname,
690 expr->ctype->bit_size,
691 new, left, right);
692 return new;
695 static int show_slice(struct expression *expr)
697 int target = show_expression(expr->base);
698 int new = new_pseudo();
699 printf("\tslice.%d\t\tv%d,v%d,%d\n", expr->r_nrbits, target, new, expr->r_bitpos);
700 return new;
703 static int show_regular_preop(struct expression *expr)
705 int target = show_expression(expr->unop);
706 int new = new_pseudo();
707 static const char *name[] = {
708 ['!'] = "nonzero", ['-'] = "neg",
709 ['~'] = "not",
711 unsigned int op = expr->op;
712 const char *opname;
714 opname = show_special(op);
715 if (op < sizeof(name)/sizeof(*name))
716 opname = name[op];
717 printf("\t%s.%d\t\tv%d,v%d\n", opname, expr->ctype->bit_size, new, target);
718 return new;
722 * FIXME! Not all accesses are memory loads. We should
723 * check what kind of symbol is behind the dereference.
725 static int show_address_gen(struct expression *expr)
727 return show_expression(expr->unop);
730 static int show_load_gen(int bits, struct expression *expr, int addr)
732 int new = new_pseudo();
734 printf("\tld.%d\t\tv%d,[v%d]\n", bits, new, addr);
735 return new;
738 static void show_store_gen(int bits, int value, struct expression *expr, int addr)
740 /* FIXME!!! Bitfield store! */
741 printf("\tst.%d\t\tv%d,[v%d]\n", bits, value, addr);
744 static int show_assignment(struct expression *expr)
746 struct expression *target = expr->left;
747 int val, addr, bits;
749 if (!expr->ctype)
750 return 0;
752 bits = expr->ctype->bit_size;
753 val = show_expression(expr->right);
754 addr = show_address_gen(target);
755 show_store_gen(bits, val, target, addr);
756 return val;
759 static int show_return_stmt(struct statement *stmt)
761 struct expression *expr = stmt->ret_value;
762 struct symbol *target = stmt->ret_target;
764 if (expr && expr->ctype) {
765 int val = show_expression(expr);
766 int bits = expr->ctype->bit_size;
767 int addr = show_symbol_expr(target);
768 show_store_gen(bits, val, NULL, addr);
770 printf("\tret\t\t(%p)\n", target);
771 return 0;
774 static int show_initialization(struct symbol *sym, struct expression *expr)
776 int val, addr, bits;
778 if (!expr->ctype)
779 return 0;
781 bits = expr->ctype->bit_size;
782 val = show_expression(expr);
783 addr = show_symbol_expr(sym);
784 // FIXME! The "target" expression is for bitfield store information.
785 // Leave it NULL, which works fine.
786 show_store_gen(bits, val, NULL, addr);
787 return 0;
790 static int show_access(struct expression *expr)
792 int addr = show_address_gen(expr);
793 return show_load_gen(expr->ctype->bit_size, expr, addr);
796 static int show_inc_dec(struct expression *expr, int postop)
798 int addr = show_address_gen(expr->unop);
799 int retval, new;
800 const char *opname = expr->op == SPECIAL_INCREMENT ? "add" : "sub";
801 int bits = expr->ctype->bit_size;
803 retval = show_load_gen(bits, expr->unop, addr);
804 new = retval;
805 if (postop)
806 new = new_pseudo();
807 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname, bits, new, retval);
808 show_store_gen(bits, new, expr->unop, addr);
809 return retval;
812 static int show_preop(struct expression *expr)
815 * '*' is an lvalue access, and is fundamentally different
816 * from an arithmetic operation. Maybe it should have an
817 * expression type of its own..
819 if (expr->op == '*')
820 return show_access(expr);
821 if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
822 return show_inc_dec(expr, 0);
823 return show_regular_preop(expr);
826 static int show_postop(struct expression *expr)
828 return show_inc_dec(expr, 1);
831 static int show_symbol_expr(struct symbol *sym)
833 int new = new_pseudo();
835 if (sym->initializer && sym->initializer->type == EXPR_STRING)
836 return show_string_expr(sym->initializer);
838 if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC)) {
839 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer, new, show_ident(sym->ident));
840 return new;
842 if (sym->ctype.modifiers & MOD_ADDRESSABLE) {
843 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer, new, sym->value);
844 return new;
846 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer, new, show_ident(sym->ident), sym);
847 return new;
850 static int show_symbol_init(struct symbol *sym)
852 struct expression *expr = sym->initializer;
854 if (expr) {
855 int val, addr, bits;
857 bits = expr->ctype->bit_size;
858 val = show_expression(expr);
859 addr = show_symbol_expr(sym);
860 show_store_gen(bits, val, NULL, addr);
862 return 0;
865 static int type_is_signed(struct symbol *sym)
867 if (sym->type == SYM_NODE)
868 sym = sym->ctype.base_type;
869 if (sym->type == SYM_PTR)
870 return 0;
871 return !(sym->ctype.modifiers & MOD_UNSIGNED);
874 static int show_cast_expr(struct expression *expr)
876 struct symbol *old_type, *new_type;
877 int op = show_expression(expr->cast_expression);
878 int oldbits, newbits;
879 int new, is_signed;
881 old_type = expr->cast_expression->ctype;
882 new_type = expr->cast_type;
884 oldbits = old_type->bit_size;
885 newbits = new_type->bit_size;
886 if (oldbits >= newbits)
887 return op;
888 new = new_pseudo();
889 is_signed = type_is_signed(old_type);
890 if (is_signed) {
891 printf("\tsext%d.%d\tv%d,v%d\n", oldbits, newbits, new, op);
892 } else {
893 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits, new, op, (1UL << oldbits)-1);
895 return new;
898 static int show_value(struct expression *expr)
900 int new = new_pseudo();
901 unsigned long long value = expr->value;
903 printf("\tmovi.%d\t\tv%d,$%llu\n", expr->ctype->bit_size, new, value);
904 return new;
907 static int show_fvalue(struct expression *expr)
909 int new = new_pseudo();
910 long double value = expr->fvalue;
912 printf("\tmovf.%d\t\tv%d,$%Lf\n", expr->ctype->bit_size, new, value);
913 return new;
916 static int show_string_expr(struct expression *expr)
918 int new = new_pseudo();
920 printf("\tmovi.%d\t\tv%d,&%s\n", bits_in_pointer, new, show_string(expr->string));
921 return new;
924 static int show_label_expr(struct expression *expr)
926 int new = new_pseudo();
927 printf("\tmovi.%d\t\tv%d,.L%p\n",bits_in_pointer, new, expr->label_symbol);
928 return new;
931 static int show_conditional_expr(struct expression *expr)
933 int cond = show_expression(expr->conditional);
934 int true = show_expression(expr->cond_true);
935 int false = show_expression(expr->cond_false);
936 int new = new_pseudo();
938 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond, expr->ctype->bit_size, new, true, false);
939 return new;
942 static int show_statement_expr(struct expression *expr)
944 return show_statement(expr->statement);
947 static int show_position_expr(struct expression *expr, struct symbol *base)
949 int new = show_expression(expr->init_expr);
950 struct symbol *ctype = expr->init_expr->ctype;
951 int bit_offset;
953 bit_offset = ctype ? ctype->bit_offset : -1;
955 printf("\tinsert v%d at [%d:%d] of %s\n", new,
956 expr->init_offset, bit_offset,
957 show_ident(base->ident));
958 return 0;
961 static int show_initializer_expr(struct expression *expr, struct symbol *ctype)
963 struct expression *entry;
965 FOR_EACH_PTR(expr->expr_list, entry) {
967 again:
968 // Nested initializers have their positions already
969 // recursively calculated - just output them too
970 if (entry->type == EXPR_INITIALIZER) {
971 show_initializer_expr(entry, ctype);
972 continue;
975 // Initializer indexes and identifiers should
976 // have been evaluated to EXPR_POS
977 if (entry->type == EXPR_IDENTIFIER) {
978 printf(" AT '%s':\n", show_ident(entry->expr_ident));
979 entry = entry->ident_expression;
980 goto again;
983 if (entry->type == EXPR_INDEX) {
984 printf(" AT '%d..%d:\n", entry->idx_from, entry->idx_to);
985 entry = entry->idx_expression;
986 goto again;
988 if (entry->type == EXPR_POS) {
989 show_position_expr(entry, ctype);
990 continue;
992 show_initialization(ctype, entry);
993 } END_FOR_EACH_PTR(entry);
994 return 0;
997 int show_symbol_expr_init(struct symbol *sym)
999 struct expression *expr = sym->initializer;
1001 if (expr)
1002 show_expression(expr);
1003 return show_symbol_expr(sym);
1007 * Print out an expression. Return the pseudo that contains the
1008 * variable.
1010 int show_expression(struct expression *expr)
1012 if (!expr)
1013 return 0;
1015 if (!expr->ctype) {
1016 struct position *pos = &expr->pos;
1017 printf("\tno type at %s:%d:%d\n",
1018 stream_name(pos->stream),
1019 pos->line, pos->pos);
1020 return 0;
1023 switch (expr->type) {
1024 case EXPR_CALL:
1025 return show_call_expression(expr);
1027 case EXPR_ASSIGNMENT:
1028 return show_assignment(expr);
1030 case EXPR_COMMA:
1031 return show_comma(expr);
1032 case EXPR_BINOP:
1033 case EXPR_COMPARE:
1034 case EXPR_LOGICAL:
1035 return show_binop(expr);
1036 case EXPR_PREOP:
1037 return show_preop(expr);
1038 case EXPR_POSTOP:
1039 return show_postop(expr);
1040 case EXPR_SYMBOL:
1041 return show_symbol_expr(expr->symbol);
1042 case EXPR_DEREF:
1043 case EXPR_SIZEOF:
1044 case EXPR_PTRSIZEOF:
1045 case EXPR_ALIGNOF:
1046 warning(expr->pos, "invalid expression after evaluation");
1047 return 0;
1048 case EXPR_CAST:
1049 case EXPR_IMPLIED_CAST:
1050 return show_cast_expr(expr);
1051 case EXPR_VALUE:
1052 return show_value(expr);
1053 case EXPR_FVALUE:
1054 return show_fvalue(expr);
1055 case EXPR_STRING:
1056 return show_string_expr(expr);
1057 case EXPR_INITIALIZER:
1058 return show_initializer_expr(expr, expr->ctype);
1059 case EXPR_SELECT:
1060 case EXPR_CONDITIONAL:
1061 return show_conditional_expr(expr);
1062 case EXPR_STATEMENT:
1063 return show_statement_expr(expr);
1064 case EXPR_LABEL:
1065 return show_label_expr(expr);
1066 case EXPR_SLICE:
1067 return show_slice(expr);
1069 // None of these should exist as direct expressions: they are only
1070 // valid as sub-expressions of initializers.
1071 case EXPR_POS:
1072 warning(expr->pos, "unable to show plain initializer position expression");
1073 return 0;
1074 case EXPR_IDENTIFIER:
1075 warning(expr->pos, "unable to show identifier expression");
1076 return 0;
1077 case EXPR_INDEX:
1078 warning(expr->pos, "unable to show index expression");
1079 return 0;
1080 case EXPR_TYPE:
1081 warning(expr->pos, "unable to show type expression");
1082 return 0;
1084 return 0;