Oops. Bad scoping for iterators and switch() statements. We didn't
[smatch.git] / show-parse.c
blobe12c57115c2c10f795822ea1342a568e48f65b88
1 /*
2 * sparse/show-parse.c
4 * Copyright (C) 2003 Transmeta Corp, all rights reserved.
6 * Print out results of parsing for debugging and testing.
7 */
8 #include <stdarg.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <unistd.h>
14 #include <fcntl.h>
16 #include "lib.h"
17 #include "token.h"
18 #include "parse.h"
19 #include "symbol.h"
20 #include "scope.h"
21 #include "expression.h"
22 #include "target.h"
24 static void do_debug_symbol(struct symbol *sym, int indent)
26 static const char indent_string[] = " ";
27 static const char *typestr[] = {
28 "base", "node", "ptr.", "fn..",
29 "arry", "strt", "unin", "enum",
30 "tdef", "tpof", "memb", "bitf",
31 "labl"
34 if (!sym)
35 return;
36 fprintf(stderr, "%.*s%s%3d:%lu %lx %s (as: %d, context: %x:%x) %p\n",
37 indent, indent_string, typestr[sym->type],
38 sym->bit_size, sym->ctype.alignment,
39 sym->ctype.modifiers, show_ident(sym->ident),
40 sym->ctype.as, sym->ctype.context, sym->ctype.contextmask,
41 sym);
42 do_debug_symbol(sym->ctype.base_type, indent+2);
45 void debug_symbol(struct symbol *sym)
47 do_debug_symbol(sym, 0);
51 * Symbol type printout. The type system is by far the most
52 * complicated part of C - everything else is trivial.
54 const char *modifier_string(unsigned long mod)
56 static char buffer[100];
57 char *p = buffer;
58 const char *res,**ptr, *names[] = {
59 "auto", "register", "static", "extern",
60 "const", "volatile", "[signed]", "[unsigned]",
61 "[char]", "[short]", "[long]", "[long]",
62 "[typdef]", "[structof]", "[unionof]", "[enum]",
63 "[typeof]", "[attribute]", "inline", "[addressable]",
64 "[nocast]", "[noderef]",
65 NULL
67 ptr = names;
68 while ((res = *ptr++) != NULL) {
69 if (mod & 1) {
70 char c;
71 while ((c = *res++) != '\0')
72 *p++ = c;
73 *p++ = ' ';
75 mod >>= 1;
77 *p = 0;
78 return buffer;
81 void show_struct_member(struct symbol *sym, void *data, int flags)
83 if (flags & ITERATE_FIRST)
84 printf(" {\n\t");
85 printf("%s:%d:%ld at offset %ld", show_ident(sym->ident), sym->bit_size, sym->ctype.alignment, sym->offset);
86 if (sym->fieldwidth)
87 printf("[%d..%d]", sym->bit_offset, sym->bit_offset+sym->fieldwidth-1);
88 if (flags & ITERATE_LAST)
89 printf("\n} ");
90 else
91 printf(", ");
94 static void show_one_symbol(struct symbol *sym, void *sep, int flags)
96 show_symbol(sym);
97 if (!(flags & ITERATE_LAST))
98 printf("%s", (const char *)sep);
101 void show_symbol_list(struct symbol_list *list, const char *sep)
103 symbol_iterate(list, show_one_symbol, (void *)sep);
106 struct type_name {
107 char *start;
108 char *end;
111 static void prepend(struct type_name *name, const char *fmt, ...)
113 static char buffer[512];
114 int n;
116 va_list args;
117 va_start(args, fmt);
118 n = vsprintf(buffer, fmt, args);
119 va_end(args);
121 name->start -= n;
122 memcpy(name->start, buffer, n);
125 static void append(struct type_name *name, const char *fmt, ...)
127 static char buffer[512];
128 int n;
130 va_list args;
131 va_start(args, fmt);
132 n = vsprintf(buffer, fmt, args);
133 va_end(args);
135 memcpy(name->end, buffer, n);
136 name->end += n;
139 static void do_show_type(struct symbol *sym, struct type_name *name)
141 int i, modlen;
142 const char *mod;
143 static struct ctype_name {
144 struct symbol *sym;
145 char *name;
146 } typenames[] = {
147 { & char_ctype, "char" },
148 { &uchar_ctype, "unsigned char" },
149 { & short_ctype, "short" },
150 { &ushort_ctype, "unsigned short" },
151 { & int_ctype, "int" },
152 { &uint_ctype, "unsigned int" },
153 { & long_ctype, "long" },
154 { &ulong_ctype, "unsigned long" },
155 { & llong_ctype, "long long" },
156 { &ullong_ctype, "unsigned long long" },
158 { &void_ctype, "void" },
159 { &bool_ctype, "bool" },
160 { &string_ctype, "string" },
162 { &float_ctype, "float" },
163 { &double_ctype, "double" },
164 { &ldouble_ctype,"long double" },
167 if (!sym)
168 return;
170 for (i = 0; i < sizeof(typenames)/sizeof(typenames[0]); i++) {
171 if (typenames[i].sym == sym) {
172 int len = strlen(typenames[i].name);
173 *--name->start = ' ';
174 name->start -= len;
175 memcpy(name->start, typenames[i].name, len);
176 return;
180 /* Prepend */
181 switch (sym->type) {
182 case SYM_PTR:
183 prepend(name, "*");
184 break;
185 case SYM_FN:
186 prepend(name, "( ");
187 break;
188 case SYM_STRUCT:
189 prepend(name, "struct %s ", show_ident(sym->ident));
190 return;
192 case SYM_UNION:
193 prepend(name, "union %s ", show_ident(sym->ident));
194 return;
196 case SYM_ENUM:
197 prepend(name, "enum %s ", show_ident(sym->ident));
198 return;
200 case SYM_NODE:
201 append(name, "%s", show_ident(sym->ident));
202 break;
204 case SYM_BITFIELD:
205 append(name, ":%d", sym->fieldwidth);
206 break;
208 case SYM_LABEL:
209 append(name, "label(%s:%p)", show_ident(sym->ident), sym);
210 break;
212 case SYM_ARRAY:
213 break;
215 default:
216 prepend(name, "unknown type %d", sym->type);
217 return;
220 mod = modifier_string(sym->ctype.modifiers);
221 modlen = strlen(mod);
222 name->start -= modlen;
223 memcpy(name->start, mod, modlen);
225 do_show_type(sym->ctype.base_type, name);
227 /* Postpend */
228 if (sym->ctype.as)
229 append(name, "<asn:%d>", sym->ctype.as);
231 switch (sym->type) {
232 case SYM_PTR:
233 return;
235 case SYM_FN:
236 append(name, " )( ... )");
237 return;
239 case SYM_ARRAY:
240 append(name, "[%d]", sym->array_size);
241 return;
242 default:
243 break;
247 void show_type(struct symbol *sym)
249 char array[200];
250 struct type_name name;
252 name.start = name.end = array+100;
253 do_show_type(sym, &name);
254 *name.end = 0;
255 printf("%s", name.start);
258 const char *show_typename(struct symbol *sym)
260 static char array[200];
261 struct type_name name;
263 name.start = name.end = array+100;
264 do_show_type(sym, &name);
265 *name.end = 0;
266 return name.start;
269 void show_symbol(struct symbol *sym)
271 struct symbol *type;
273 if (!sym)
274 return;
276 show_type(sym);
277 type = sym->ctype.base_type;
278 if (!type)
279 return;
282 * Show actual implementation information
284 switch (type->type) {
285 case SYM_STRUCT:
286 symbol_iterate(type->symbol_list, show_struct_member, NULL);
287 break;
289 case SYM_UNION:
290 symbol_iterate(type->symbol_list, show_struct_member, NULL);
291 break;
293 case SYM_FN:
294 printf("\n");
295 show_statement(type->stmt);
296 break;
298 default:
299 break;
302 if (sym->initializer) {
303 printf(" = ");
304 show_expression(sym->initializer);
308 static int show_return_stmt(struct statement *stmt)
310 struct expression *expr = stmt->expression;
312 if (expr) {
313 int val = show_expression(expr);
314 printf("\tmov.%d\t\tretval,v%d\n",
315 expr->ctype->bit_size, val);
317 printf("\tret\n");
318 return 0;
321 static int show_symbol_init(struct symbol *sym);
323 static int new_label(void)
325 static int label = 0;
326 return ++label;
330 * Print out a statement
332 int show_statement(struct statement *stmt)
334 if (!stmt)
335 return 0;
336 switch (stmt->type) {
337 case STMT_RETURN:
338 return show_return_stmt(stmt);
339 case STMT_COMPOUND: {
340 struct symbol *sym;
341 struct statement *s;
342 int last;
344 FOR_EACH_PTR(stmt->syms, sym) {
345 show_symbol_init(sym);
346 } END_FOR_EACH_PTR;
348 FOR_EACH_PTR(stmt->stmts, s) {
349 last = show_statement(s);
350 } END_FOR_EACH_PTR;
351 return last;
354 case STMT_EXPRESSION:
355 return show_expression(stmt->expression);
356 case STMT_IF: {
357 int val, target;
358 struct expression *cond = stmt->if_conditional;
360 if (cond->type == EXPR_VALUE) {
361 struct statement *s = stmt->if_true;
362 if (!cond->value)
363 s = stmt->if_false;
364 show_statement(s);
365 break;
367 val = show_expression(cond);
368 target = new_label();
369 printf("\tje\t\tv%d,.L%d\n", val, target);
370 show_statement(stmt->if_true);
371 if (stmt->if_false) {
372 int last = new_label();
373 printf("\tjmp\t\t.L%d\n", last);
374 printf(".L%d:\n", target);
375 target = last;
376 show_statement(stmt->if_false);
378 printf(".L%d:\n", target);
379 break;
381 case STMT_SWITCH: {
382 int val = show_expression(stmt->switch_expression);
383 printf("\tswitch v%d\n", val);
384 show_statement(stmt->switch_statement);
385 if (stmt->switch_break->used)
386 printf(".L%p:\n", stmt->switch_break);
387 break;
390 case STMT_CASE:
391 if (!stmt->case_expression)
392 printf("default");
393 else {
394 printf("case %lld", stmt->case_expression->value);
395 if (stmt->case_to) {
396 printf("...%lld", stmt->case_to->value);
400 printf(":\n");
401 show_statement(stmt->case_statement);
402 break;
404 case STMT_ITERATOR: {
405 struct statement *pre_statement = stmt->iterator_pre_statement;
406 struct expression *pre_condition = stmt->iterator_pre_condition;
407 struct statement *statement = stmt->iterator_statement;
408 struct statement *post_statement = stmt->iterator_post_statement;
409 struct expression *post_condition = stmt->iterator_post_condition;
410 int val, loop_top = 0, loop_bottom = 0;
412 show_statement(pre_statement);
413 if (pre_condition) {
414 if (pre_condition->type == EXPR_VALUE) {
415 if (!pre_condition->value)
416 break;
417 pre_condition = NULL;
418 } else {
419 loop_bottom = new_label();
420 val = show_expression(pre_condition);
421 printf("\tje\t\tv%d, .L%d\n", val, loop_bottom);
424 if (!post_condition || post_condition->type != EXPR_VALUE || post_condition->value)
425 loop_top = new_label();
426 printf(".L%d:\n", loop_top);
427 show_statement(statement);
428 if (stmt->iterator_continue->used)
429 printf(".L%p:\n", stmt->iterator_continue);
430 show_statement(post_statement);
431 if (!post_condition) {
432 printf("\tjmp\t\t.L%d\n", loop_top);
433 } else if (post_condition->type == EXPR_VALUE) {
434 if (post_condition->value)
435 printf("\tjmp\t\t.L%d\n", loop_top);
436 } else {
437 val = show_expression(post_condition);
438 printf("\tjne\t\tv%d, .L%d\n", val, loop_top);
440 if (stmt->iterator_break->used)
441 printf(".L%p:\n", stmt->iterator_break);
442 if (pre_condition)
443 printf(".L%d:\n", loop_bottom);
444 break;
446 case STMT_NONE:
447 printf("\t!NONE!\n");
448 break;
450 case STMT_LABEL:
451 printf(".L%p:\n", stmt->label_identifier);
452 show_statement(stmt->label_statement);
453 break;
455 case STMT_GOTO:
456 if (stmt->goto_expression) {
457 int val = show_expression(stmt->goto_expression);
458 printf("\tgoto *v%d\n", val);
459 } else {
460 printf("\tgoto .L%p\n", stmt->goto_label);
462 break;
463 case STMT_ASM:
464 printf("\tasm( .... )\n");
465 break;
468 return 0;
471 static void show_one_statement(struct statement *stmt, void *sep, int flags)
473 show_statement(stmt);
474 if (!(flags & ITERATE_LAST))
475 printf("%s", (const char *)sep);
478 void show_statement_list(struct statement_list *stmt, const char *sep)
480 statement_iterate(stmt, show_one_statement, (void *)sep);
483 static void show_one_expression(struct expression *expr, void *sep, int flags)
485 show_expression(expr);
486 if (!(flags & ITERATE_LAST))
487 printf("%s", (const char *)sep);
490 void show_expression_list(struct expression_list *list, const char *sep)
492 expression_iterate(list, show_one_expression, (void *)sep);
495 static int new_pseudo(void)
497 static int nr = 0;
498 return ++nr;
501 static int show_call_expression(struct expression *expr)
503 struct symbol *direct;
504 struct expression *arg, *fn;
505 int fncall, retval;
506 int framesize;
508 if (!expr->ctype) {
509 warn(expr->pos, "\tcall with no type!");
510 return 0;
513 framesize = 0;
514 FOR_EACH_PTR_REVERSE(expr->args, arg) {
515 int new = show_expression(arg);
516 int size = arg->ctype->bit_size;
517 printf("\tpush.%d\t\tv%d\n", size, new);
518 framesize += size >> 3;
519 } END_FOR_EACH_PTR;
521 fn = expr->fn;
523 /* Remove dereference, if any */
524 direct = NULL;
525 if (fn->type == EXPR_PREOP) {
526 if (fn->unop->type == EXPR_SYMBOL) {
527 struct symbol *sym = fn->unop->symbol;
528 if (sym->ctype.base_type->type == SYM_FN)
529 direct = sym;
532 if (direct) {
533 printf("\tcall\t\t%s\n", show_ident(direct->ident));
534 } else {
535 fncall = show_expression(fn);
536 printf("\tcall\t\t*v%d\n", fncall);
538 if (framesize)
539 printf("\tadd.%d\t\tvSP,vSP,$%d\n", BITS_IN_POINTER, framesize);
541 retval = new_pseudo();
542 printf("\tmov.%d\t\tv%d,retval\n", expr->ctype->bit_size, retval);
543 return retval;
546 static int show_binop(struct expression *expr)
548 int left = show_expression(expr->left);
549 int right = show_expression(expr->right);
550 int new = new_pseudo();
551 const char *opname;
552 static const char *name[] = {
553 ['+'] = "add", ['-'] = "sub",
554 ['*'] = "mul", ['/'] = "div",
555 ['%'] = "mod"
557 unsigned int op = expr->op;
559 opname = show_special(op);
560 if (op < sizeof(name)/sizeof(*name))
561 opname = name[op];
562 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname,
563 expr->ctype->bit_size,
564 new, left, right);
565 return new;
568 static int show_regular_preop(struct expression *expr)
570 int target = show_expression(expr->unop);
571 int new = new_pseudo();
572 static const char *name[] = {
573 ['!'] = "nonzero", ['-'] = "neg",
574 ['~'] = "not",
576 unsigned int op = expr->op;
577 const char *opname;
579 opname = show_special(op);
580 if (op < sizeof(name)/sizeof(*name))
581 opname = name[op];
582 printf("\t%s.%d\t\tv%d,v%d\n", opname, expr->ctype->bit_size, new, target);
583 return new;
587 * FIXME! Not all accesses are memory loads. We should
588 * check what kind of symbol is behind the dereference.
590 static int show_address_gen(struct expression *expr)
592 if (expr->type == EXPR_PREOP)
593 return show_expression(expr->unop);
594 return show_expression(expr->address);
597 static int show_load_gen(int bits, struct expression *expr, int addr)
599 int new = new_pseudo();
601 printf("\tld.%d\t\tv%d,[v%d]\n", bits, new, addr);
602 if (expr->type == EXPR_PREOP)
603 return new;
605 /* bitfield load! */
606 if (expr->bitpos)
607 printf("\tshr.%d\t\tv%d,v%d,$%d\n", bits, new, new, expr->bitpos);
608 printf("\tandi.%d\t\tv%d,v%d,$%llu\n", bits, new, new, (1ULL << expr->nrbits)-1);
609 return new;
612 static void show_store_gen(int bits, int value, struct expression *expr, int addr)
614 /* FIXME!!! Bitfield store! */
615 printf("\tst.%d\t\tv%d,[v%d]\n", bits, value, addr);
618 static int show_assignment(struct expression *expr)
620 struct expression *target = expr->left;
621 int val, addr, bits;
623 if (!expr->ctype)
624 return 0;
626 bits = expr->ctype->bit_size;
627 val = show_expression(expr->right);
628 addr = show_address_gen(target);
629 show_store_gen(bits, val, target, addr);
630 return val;
633 static int show_access(struct expression *expr)
635 int addr = show_address_gen(expr);
636 return show_load_gen(expr->ctype->bit_size, expr, addr);
639 static int show_inc_dec(struct expression *expr, int postop)
641 int addr = show_address_gen(expr->unop);
642 int retval, new;
643 const char *opname = expr->op == SPECIAL_INCREMENT ? "add" : "sub";
644 int bits = expr->ctype->bit_size;
646 retval = show_load_gen(bits, expr->unop, addr);
647 new = retval;
648 if (postop)
649 new = new_pseudo();
650 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname, bits, new, retval);
651 show_store_gen(bits, new, expr->unop, addr);
652 return retval;
655 static int show_preop(struct expression *expr)
658 * '*' is an lvalue access, and is fundamentally different
659 * from an arithmetic operation. Maybe it should have an
660 * expression type of its own..
662 if (expr->op == '*')
663 return show_access(expr);
664 if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
665 return show_inc_dec(expr, 0);
666 return show_regular_preop(expr);
669 static int show_postop(struct expression *expr)
671 return show_inc_dec(expr, 1);
674 static int show_symbol_expr(struct symbol *sym)
676 int new = new_pseudo();
678 if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC)) {
679 printf("\tmovi.%d\t\tv%d,$%s\n", BITS_IN_POINTER, new, show_ident(sym->ident));
680 return new;
682 if (sym->ctype.modifiers & MOD_ADDRESSABLE) {
683 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", BITS_IN_POINTER, new, sym->value);
684 return new;
686 printf("\taddi.%d\t\tv%d,vFP,$xxx\n", BITS_IN_POINTER, new);
687 return new;
690 static int show_symbol_init(struct symbol *sym)
692 struct expression *expr = sym->initializer;
694 if (expr) {
695 int val, addr, bits;
697 bits = expr->ctype->bit_size;
698 val = show_expression(expr);
699 addr = show_symbol_expr(sym);
700 show_store_gen(bits, val, NULL, addr);
702 return 0;
705 static int type_is_signed(struct symbol *sym)
707 if (sym->type == SYM_NODE)
708 sym = sym->ctype.base_type;
709 if (sym->type == SYM_PTR)
710 return 0;
711 return !(sym->ctype.modifiers & MOD_UNSIGNED);
714 static int show_cast_expr(struct expression *expr)
716 struct symbol *old_type, *new_type;
717 int op = show_expression(expr->cast_expression);
718 int oldbits, newbits;
719 int new, is_signed;
721 old_type = expr->cast_expression->ctype;
722 new_type = expr->cast_type;
724 oldbits = old_type->bit_size;
725 newbits = new_type->bit_size;
726 if (oldbits >= newbits)
727 return op;
728 new = new_pseudo();
729 is_signed = type_is_signed(old_type);
730 if (is_signed) {
731 printf("\tsext%d.%d\tv%d,v%d\n", oldbits, newbits, new, op);
732 } else {
733 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits, new, op, (1UL << oldbits)-1);
735 return new;
738 static int show_value(struct expression *expr)
740 int new = new_pseudo();
741 unsigned long long value = expr->value;
743 printf("\tmovi.%d\t\tv%d,$%llu\n", expr->ctype->bit_size, new, value);
744 return new;
747 static int show_string_expr(struct expression *expr)
749 int new = new_pseudo();
751 printf("\tmovi.%d\t\tv%d,&%s\n", BITS_IN_POINTER, new, show_string(expr->string));
752 return new;
755 static int show_bitfield_expr(struct expression *expr)
757 return show_access(expr);
760 static int show_conditional_expr(struct expression *expr)
762 int cond = show_expression(expr->conditional);
763 int true = show_expression(expr->cond_true);
764 int false = show_expression(expr->cond_false);
765 int new = new_pseudo();
767 if (!true)
768 true = cond;
769 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond, expr->ctype->bit_size, new, true, false);
770 return new;
773 static int show_statement_expr(struct expression *expr)
775 return show_statement(expr->statement);
778 static int show_initializer_expr(struct expression *expr)
780 printf("\t// initializer goes here\n");
781 return 0;
785 * Print out an expression. Return the pseudo that contains the
786 * variable.
788 int show_expression(struct expression *expr)
790 if (!expr)
791 return 0;
793 if (!expr->ctype) {
794 struct position *pos = &expr->pos;
795 printf("\tno type at %s:%d:%d\n",
796 input_streams[pos->stream].name,
797 pos->line, pos->pos);
798 return 0;
801 switch (expr->type) {
802 case EXPR_CALL:
803 return show_call_expression(expr);
805 case EXPR_ASSIGNMENT:
806 return show_assignment(expr);
808 case EXPR_BINOP:
809 case EXPR_COMMA:
810 case EXPR_COMPARE:
811 case EXPR_LOGICAL:
812 return show_binop(expr);
813 case EXPR_PREOP:
814 return show_preop(expr);
815 case EXPR_POSTOP:
816 return show_postop(expr);
817 case EXPR_SYMBOL:
818 return show_symbol_expr(expr->symbol);
819 case EXPR_DEREF:
820 case EXPR_SIZEOF:
821 warn(expr->pos, "invalid expression after evaluation");
822 return 0;
823 case EXPR_CAST:
824 return show_cast_expr(expr);
825 case EXPR_VALUE:
826 return show_value(expr);
827 case EXPR_STRING:
828 return show_string_expr(expr);
829 case EXPR_BITFIELD:
830 return show_bitfield_expr(expr);
831 case EXPR_INITIALIZER:
832 return show_initializer_expr(expr);
833 case EXPR_IDENTIFIER:
834 warn(expr->pos, "unable to show identifier expression");
835 return 0;
836 case EXPR_INDEX:
837 warn(expr->pos, "unable to show index expression");
838 return 0;
839 case EXPR_CONDITIONAL:
840 return show_conditional_expr(expr);
841 case EXPR_STATEMENT:
842 return show_statement_expr(expr);
844 return 0;