Fix up some address space evaluations. This is now already
[smatch.git] / show-parse.c
blobc78ba97fce1d338e5c671fcc9e4cfff91bae4036
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)\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 do_debug_symbol(sym->ctype.base_type, indent+2);
44 void debug_symbol(struct symbol *sym)
46 do_debug_symbol(sym, 0);
50 * Symbol type printout. The type system is by far the most
51 * complicated part of C - everything else is trivial.
53 const char *modifier_string(unsigned long mod)
55 static char buffer[100];
56 char *p = buffer;
57 const char *res,**ptr, *names[] = {
58 "auto", "register", "static", "extern",
59 "const", "volatile", "[signed]", "[unsigned]",
60 "[char]", "[short]", "[long]", "[long]",
61 "[typdef]", "[structof]", "[unionof]", "[enum]",
62 "[typeof]", "[attribute]", "inline", "[addressable]",
63 "[nocast]", "[noderef]",
64 NULL
66 ptr = names;
67 while ((res = *ptr++) != NULL) {
68 if (mod & 1) {
69 char c;
70 while ((c = *res++) != '\0')
71 *p++ = c;
72 *p++ = ' ';
74 mod >>= 1;
76 *p = 0;
77 return buffer;
80 void show_struct_member(struct symbol *sym, void *data, int flags)
82 if (flags & ITERATE_FIRST)
83 printf(" {\n\t");
84 printf("%s:%d:%ld at offset %ld", show_ident(sym->ident), sym->bit_size, sym->ctype.alignment, sym->offset);
85 if (sym->fieldwidth)
86 printf("[%d..%d]", sym->bit_offset, sym->bit_offset+sym->fieldwidth-1);
87 if (flags & ITERATE_LAST)
88 printf("\n} ");
89 else
90 printf(", ");
93 static void show_one_symbol(struct symbol *sym, void *sep, int flags)
95 show_symbol(sym);
96 if (!(flags & ITERATE_LAST))
97 printf("%s", (const char *)sep);
100 void show_symbol_list(struct symbol_list *list, const char *sep)
102 symbol_iterate(list, show_one_symbol, (void *)sep);
105 void show_type_list(struct symbol *sym)
107 while (sym) {
108 show_symbol(sym);
109 sym = sym->next;
113 struct type_name {
114 char *start;
115 char *end;
118 static void prepend(struct type_name *name, const char *fmt, ...)
120 static char buffer[512];
121 int n;
123 va_list args;
124 va_start(args, fmt);
125 n = vsprintf(buffer, fmt, args);
126 va_end(args);
128 name->start -= n;
129 memcpy(name->start, buffer, n);
132 static void append(struct type_name *name, const char *fmt, ...)
134 static char buffer[512];
135 int n;
137 va_list args;
138 va_start(args, fmt);
139 n = vsprintf(buffer, fmt, args);
140 va_end(args);
142 memcpy(name->end, buffer, n);
143 name->end += n;
146 static void do_show_type(struct symbol *sym, struct type_name *name)
148 int i, modlen;
149 const char *mod;
150 static struct ctype_name {
151 struct symbol *sym;
152 char *name;
153 } typenames[] = {
154 { & char_ctype, "char" },
155 { &uchar_ctype, "unsigned char" },
156 { & short_ctype, "short" },
157 { &ushort_ctype, "unsigned short" },
158 { & int_ctype, "int" },
159 { &uint_ctype, "unsigned int" },
160 { & long_ctype, "long" },
161 { &ulong_ctype, "unsigned long" },
162 { & llong_ctype, "long long" },
163 { &ullong_ctype, "unsigned long long" },
165 { &void_ctype, "void" },
166 { &bool_ctype, "bool" },
167 { &string_ctype, "string" },
169 { &float_ctype, "float" },
170 { &double_ctype, "double" },
171 { &ldouble_ctype,"long double" },
174 if (!sym)
175 return;
177 for (i = 0; i < sizeof(typenames)/sizeof(typenames[0]); i++) {
178 if (typenames[i].sym == sym) {
179 int len = strlen(typenames[i].name);
180 *--name->start = ' ';
181 name->start -= len;
182 memcpy(name->start, typenames[i].name, len);
183 return;
187 /* Prepend */
188 switch (sym->type) {
189 case SYM_PTR:
190 prepend(name, "*");
191 break;
192 case SYM_FN:
193 prepend(name, "( ");
194 break;
195 case SYM_STRUCT:
196 prepend(name, "struct %s ", show_ident(sym->ident));
197 return;
199 case SYM_UNION:
200 prepend(name, "union %s ", show_ident(sym->ident));
201 return;
203 case SYM_ENUM:
204 prepend(name, "enum %s ", show_ident(sym->ident));
205 return;
207 case SYM_NODE:
208 append(name, "%s", show_ident(sym->ident));
209 break;
211 case SYM_BITFIELD:
212 append(name, ":%d", sym->fieldwidth);
213 break;
215 case SYM_LABEL:
216 append(name, "label(%s:%p)", show_ident(sym->ident), sym);
217 break;
219 case SYM_ARRAY:
220 break;
222 default:
223 prepend(name, "unknown type %d", sym->type);
224 return;
227 mod = modifier_string(sym->ctype.modifiers);
228 modlen = strlen(mod);
229 name->start -= modlen;
230 memcpy(name->start, mod, modlen);
232 do_show_type(sym->ctype.base_type, name);
234 /* Postpend */
235 if (sym->ctype.as)
236 append(name, "<asn:%d>", sym->ctype.as);
238 switch (sym->type) {
239 case SYM_PTR:
240 return;
242 case SYM_FN:
243 append(name, " )( ... )");
244 return;
246 case SYM_ARRAY:
247 append(name, "[%d]", sym->array_size);
248 return;
249 default:
250 break;
254 void show_type(struct symbol *sym)
256 char array[200];
257 struct type_name name;
259 name.start = name.end = array+100;
260 do_show_type(sym, &name);
261 *name.end = 0;
262 printf("%s", name.start);
265 const char *show_typename(struct symbol *sym)
267 static char array[200];
268 struct type_name name;
270 name.start = name.end = array+100;
271 do_show_type(sym, &name);
272 *name.end = 0;
273 return name.start;
276 void show_symbol(struct symbol *sym)
278 struct symbol *type;
280 if (!sym)
281 return;
283 show_type(sym);
284 type = sym->ctype.base_type;
285 if (!type)
286 return;
289 * Show actual implementation information
291 switch (type->type) {
292 case SYM_STRUCT:
293 symbol_iterate(type->symbol_list, show_struct_member, NULL);
294 break;
296 case SYM_UNION:
297 symbol_iterate(type->symbol_list, show_struct_member, NULL);
298 break;
300 case SYM_FN:
301 printf("\n");
302 show_statement(type->stmt);
303 break;
305 default:
306 break;
309 if (sym->initializer) {
310 printf(" = ");
311 show_expression(sym->initializer);
315 static int show_return_stmt(struct statement *stmt)
317 struct expression *expr = stmt->expression;
319 if (expr) {
320 int val = show_expression(expr);
321 printf("\tmov.%d\t\tretval,v%d\n",
322 expr->ctype->bit_size, val);
324 printf("\tret\n");
325 return 0;
328 static int show_symbol_init(struct symbol *sym);
331 * Print out a statement
333 int show_statement(struct statement *stmt)
335 if (!stmt)
336 return 0;
337 switch (stmt->type) {
338 case STMT_RETURN:
339 return show_return_stmt(stmt);
340 case STMT_COMPOUND: {
341 struct symbol *sym;
342 struct statement *s;
343 int last;
345 FOR_EACH_PTR(stmt->syms, sym) {
346 show_symbol_init(sym);
347 } END_FOR_EACH_PTR;
349 FOR_EACH_PTR(stmt->stmts, s) {
350 last = show_statement(s);
351 } END_FOR_EACH_PTR;
352 return last;
355 case STMT_EXPRESSION:
356 return show_expression(stmt->expression);
357 case STMT_IF:
358 printf("\tif (");
359 show_expression(stmt->if_conditional);
360 printf(")\n");
361 show_statement(stmt->if_true);
362 if (stmt->if_false) {
363 printf("\nelse\n");
364 show_statement(stmt->if_false);
366 break;
367 case STMT_SWITCH:
368 printf("\tswitch (");
369 show_expression(stmt->switch_expression);
370 printf(")\n");
371 show_statement(stmt->switch_statement);
372 break;
374 case STMT_CASE:
375 if (!stmt->case_expression)
376 printf("default");
377 else {
378 printf("case ");
379 show_expression(stmt->case_expression);
380 if (stmt->case_to) {
381 printf(" ... ");
382 show_expression(stmt->case_to);
385 printf(":");
386 show_statement(stmt->case_statement);
387 break;
389 case STMT_BREAK:
390 printf("\tbreak");
391 break;
393 case STMT_ITERATOR: {
394 struct statement *pre_statement = stmt->iterator_pre_statement;
395 struct expression *pre_condition = stmt->iterator_pre_condition;
396 struct statement *statement = stmt->iterator_statement;
397 struct statement *post_statement = stmt->iterator_post_statement;
398 struct expression *post_condition = stmt->iterator_post_condition;
401 * THIS IS ONLY APPROXIMATE!
403 * Real iterators are more generic than
404 * any of for/while/do-while, and can't
405 * be printed out as C without goto's
407 if (post_statement || !post_condition) {
408 printf("\tfor ( ");
409 show_statement(pre_statement);
410 printf(" ; ");
411 show_expression(pre_condition);
412 printf(" ; ");
413 show_statement(post_statement);
414 printf(" )\n");
415 show_statement(statement);
416 } else if (pre_condition) {
417 if (pre_statement) {
418 show_statement(pre_statement);
419 printf(";\n");
421 printf("\twhile (");
422 show_expression(pre_condition);
423 printf(")\n");
424 show_statement(statement);
425 } else {
426 if (pre_statement) {
427 show_statement(pre_statement);
428 printf(";\n");
430 printf("\tdo\n");
431 show_statement(statement);
432 printf("\twhile (");
433 show_expression(post_condition);
434 printf(")");
436 break;
438 case STMT_NONE:
439 printf("\tNONE");
440 break;
442 case STMT_CONTINUE:
443 printf("\tcontinue");
444 break;
446 case STMT_LABEL:
447 show_symbol(stmt->label_identifier);
448 printf(":\n");
449 show_statement(stmt->label_statement);
450 break;
452 case STMT_GOTO:
453 if (stmt->goto_expression) {
454 printf("\tgoto *");
455 show_expression(stmt->goto_expression);
456 } else {
457 printf("\tgoto ");
458 show_symbol(stmt->goto_label);
460 break;
461 case STMT_ASM:
462 printf("\tasm( .... )");
463 break;
466 return 0;
469 static void show_one_statement(struct statement *stmt, void *sep, int flags)
471 show_statement(stmt);
472 if (!(flags & ITERATE_LAST))
473 printf("%s", (const char *)sep);
476 void show_statement_list(struct statement_list *stmt, const char *sep)
478 statement_iterate(stmt, show_one_statement, (void *)sep);
481 static void show_one_expression(struct expression *expr, void *sep, int flags)
483 show_expression(expr);
484 if (!(flags & ITERATE_LAST))
485 printf("%s", (const char *)sep);
488 void show_expression_list(struct expression_list *list, const char *sep)
490 expression_iterate(list, show_one_expression, (void *)sep);
493 static int new_pseudo(void)
495 static int nr = 0;
496 return ++nr;
499 static int show_call_expression(struct expression *expr)
501 struct symbol *direct;
502 struct expression *arg, *fn;
503 int fncall, retval;
504 int framesize;
506 if (!expr->ctype) {
507 warn(expr->pos, "\tcall with no type!");
508 return 0;
511 framesize = 0;
512 FOR_EACH_PTR_REVERSE(expr->args, arg) {
513 int new = show_expression(arg);
514 int size = arg->ctype->bit_size;
515 printf("\tpush.%d\t\tv%d\n", size, new);
516 framesize += size >> 3;
517 } END_FOR_EACH_PTR;
519 fn = expr->fn;
521 /* Remove dereference, if any */
522 direct = NULL;
523 if (fn->type == EXPR_PREOP) {
524 if (fn->unop->type == EXPR_SYMBOL) {
525 struct symbol *sym = fn->unop->symbol;
526 if (sym->ctype.base_type->type == SYM_FN)
527 direct = sym;
530 if (direct) {
531 printf("\tcall\t\t%s\n", show_ident(direct->ident));
532 } else {
533 fncall = show_expression(fn);
534 printf("\tcall\t\t*v%d\n", fncall);
536 if (framesize)
537 printf("\tadd.%d\t\tvSP,vSP,$%d\n", BITS_IN_POINTER, framesize);
539 retval = new_pseudo();
540 printf("\tmov.%d\t\tv%d,retval\n", expr->ctype->bit_size, retval);
541 return retval;
544 static int show_binop(struct expression *expr)
546 int left = show_expression(expr->left);
547 int right = show_expression(expr->right);
548 int new = new_pseudo();
549 const char *opname;
550 static const char *name[] = {
551 ['+'] = "add", ['-'] = "sub",
552 ['*'] = "mul", ['/'] = "div",
553 ['%'] = "mod"
555 unsigned int op = expr->op;
557 opname = show_special(op);
558 if (op < sizeof(name)/sizeof(*name))
559 opname = name[op];
560 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname,
561 expr->ctype->bit_size,
562 new, left, right);
563 return new;
566 static int show_regular_preop(struct expression *expr)
568 int target = show_expression(expr->unop);
569 int new = new_pseudo();
570 static const char *name[] = {
571 ['!'] = "nonzero", ['-'] = "neg",
572 ['~'] = "not",
574 unsigned int op = expr->op;
575 const char *opname;
577 opname = show_special(op);
578 if (op < sizeof(name)/sizeof(*name))
579 opname = name[op];
580 printf("\t%s.%d\t\tv%d,v%d\n", opname, expr->ctype->bit_size, new, target);
581 return new;
585 * FIXME! Not all accesses are memory loads. We should
586 * check what kind of symbol is behind the dereference.
588 static int show_address_gen(struct expression *expr)
590 if (expr->type == EXPR_PREOP)
591 return show_expression(expr->unop);
592 return show_expression(expr->address);
595 static int show_load_gen(int bits, struct expression *expr, int addr)
597 int new = new_pseudo();
599 printf("\tld.%d\t\tv%d,[v%d]\n", bits, new, addr);
600 if (expr->type == EXPR_PREOP)
601 return new;
603 /* bitfield load! */
604 if (expr->bitpos)
605 printf("\tshr.%d\t\tv%d,v%d,$%d\n", bits, new, new, expr->bitpos);
606 printf("\tandi.%d\t\tv%d,v%d,$%llu\n", bits, new, new, (1ULL << expr->nrbits)-1);
607 return new;
610 static void show_store_gen(int bits, int value, struct expression *expr, int addr)
612 /* FIXME!!! Bitfield store! */
613 printf("\tst.%d\t\tv%d,[v%d]\n", bits, value, addr);
616 static int show_assignment(struct expression *expr)
618 struct expression *target = expr->left;
619 int val, addr, bits = expr->ctype->bit_size;
621 val = show_expression(expr->right);
622 addr = show_address_gen(target);
623 show_store_gen(bits, val, target, addr);
624 return val;
627 static int show_access(struct expression *expr)
629 int addr = show_address_gen(expr);
630 return show_load_gen(expr->ctype->bit_size, expr, addr);
633 static int show_inc_dec(struct expression *expr, int postop)
635 int addr = show_address_gen(expr->unop);
636 int retval, new;
637 const char *opname = expr->op == SPECIAL_INCREMENT ? "add" : "sub";
638 int bits = expr->ctype->bit_size;
640 retval = show_load_gen(bits, expr->unop, addr);
641 new = retval;
642 if (postop)
643 new = new_pseudo();
644 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname, bits, new, retval);
645 show_store_gen(bits, new, expr->unop, addr);
646 return retval;
649 static int show_preop(struct expression *expr)
652 * '*' is an lvalue access, and is fundamentally different
653 * from an arithmetic operation. Maybe it should have an
654 * expression type of its own..
656 if (expr->op == '*')
657 return show_access(expr);
658 if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
659 return show_inc_dec(expr, 0);
660 return show_regular_preop(expr);
663 static int show_postop(struct expression *expr)
665 return show_inc_dec(expr, 1);
668 static int show_symbol_expr(struct symbol *sym)
670 int new = new_pseudo();
671 printf("\tmovi.%d\t\tv%d,$%s\n", BITS_IN_POINTER, new, show_ident(sym->ident));
672 return new;
675 static int show_symbol_init(struct symbol *sym)
677 struct expression *expr = sym->initializer;
679 if (expr) {
680 int val, addr, bits;
682 bits = expr->ctype->bit_size;
683 val = show_expression(expr);
684 addr = show_symbol_expr(sym);
685 show_store_gen(bits, val, NULL, addr);
687 return 0;
690 static int type_is_signed(struct symbol *sym)
692 if (sym->type == SYM_NODE)
693 sym = sym->ctype.base_type;
694 if (sym->type == SYM_PTR)
695 return 0;
696 return !(sym->ctype.modifiers & MOD_UNSIGNED);
699 static int show_cast_expr(struct expression *expr)
701 struct symbol *old_type, *new_type;
702 int op = show_expression(expr->cast_expression);
703 int oldbits, newbits;
704 int new, is_signed;
706 old_type = expr->cast_expression->ctype;
707 new_type = expr->cast_type;
709 oldbits = old_type->bit_size;
710 newbits = new_type->bit_size;
711 if (oldbits >= newbits)
712 return op;
713 new = new_pseudo();
714 is_signed = type_is_signed(old_type);
715 if (is_signed) {
716 printf("\tsext%d.%d\tv%d,v%d\n", oldbits, newbits, new, op);
717 } else {
718 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits, new, op, (1UL << oldbits)-1);
720 return new;
723 static int show_value(struct expression *expr)
725 int new = new_pseudo();
726 unsigned long long value = expr->value;
728 printf("\tmovi.%d\t\tv%d,$%llu\n", expr->ctype->bit_size, new, value);
729 return new;
732 static int show_string_expr(struct expression *expr)
734 int new = new_pseudo();
736 printf("\tmovi.%d\t\tv%d,&%s\n", BITS_IN_POINTER, new, show_string(expr->string));
737 return new;
740 static int show_bitfield_expr(struct expression *expr)
742 return show_access(expr);
745 static int show_conditional_expr(struct expression *expr)
747 int cond = show_expression(expr->conditional);
748 int true = show_expression(expr->cond_true);
749 int false = show_expression(expr->cond_false);
750 int new = new_pseudo();
752 if (!true)
753 true = cond;
754 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond, expr->ctype->bit_size, new, true, false);
755 return new;
758 static int show_statement_expr(struct expression *expr)
760 return show_statement(expr->statement);
763 static int show_initializer_expr(struct expression *expr)
765 printf("\t// initializer goes here\n");
766 return 0;
770 * Print out an expression. Return the pseudo that contains the
771 * variable.
773 int show_expression(struct expression *expr)
775 if (!expr)
776 return 0;
778 switch (expr->type) {
779 case EXPR_CALL:
780 return show_call_expression(expr);
782 case EXPR_ASSIGNMENT:
783 return show_assignment(expr);
785 case EXPR_BINOP:
786 case EXPR_COMMA:
787 case EXPR_COMPARE:
788 case EXPR_LOGICAL:
789 return show_binop(expr);
790 case EXPR_PREOP:
791 return show_preop(expr);
792 case EXPR_POSTOP:
793 return show_postop(expr);
794 case EXPR_SYMBOL:
795 return show_symbol_expr(expr->symbol);
796 case EXPR_DEREF:
797 case EXPR_SIZEOF:
798 warn(expr->pos, "invalid expression after evaluation");
799 return 0;
800 case EXPR_CAST:
801 return show_cast_expr(expr);
802 case EXPR_VALUE:
803 return show_value(expr);
804 case EXPR_STRING:
805 return show_string_expr(expr);
806 case EXPR_BITFIELD:
807 return show_bitfield_expr(expr);
808 case EXPR_INITIALIZER:
809 return show_initializer_expr(expr);
810 case EXPR_IDENTIFIER:
811 warn(expr->pos, "unable to show identifier expression");
812 return 0;
813 case EXPR_INDEX:
814 warn(expr->pos, "unable to show index expression");
815 return 0;
816 case EXPR_CONDITIONAL:
817 return show_conditional_expr(expr);
818 case EXPR_STATEMENT:
819 return show_statement_expr(expr);
821 return 0;