4 * Copyright (C) 2003 Transmeta Corp, all rights reserved.
6 * Print out results of parsing for debugging and testing.
21 #include "expression.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",
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];
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]",
67 while ((res
= *ptr
++) != NULL
) {
70 while ((c
= *res
++) != '\0')
80 void show_struct_member(struct symbol
*sym
, void *data
, int flags
)
82 if (flags
& ITERATE_FIRST
)
84 printf("%s:%d:%ld at offset %ld", show_ident(sym
->ident
), sym
->bit_size
, sym
->ctype
.alignment
, sym
->offset
);
86 printf("[%d..%d]", sym
->bit_offset
, sym
->bit_offset
+sym
->fieldwidth
-1);
87 if (flags
& ITERATE_LAST
)
93 static void show_one_symbol(struct symbol
*sym
, void *sep
, int flags
)
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
)
118 static void prepend(struct type_name
*name
, const char *fmt
, ...)
120 static char buffer
[512];
125 n
= vsprintf(buffer
, fmt
, args
);
129 memcpy(name
->start
, buffer
, n
);
132 static void append(struct type_name
*name
, const char *fmt
, ...)
134 static char buffer
[512];
139 n
= vsprintf(buffer
, fmt
, args
);
142 memcpy(name
->end
, buffer
, n
);
146 static void do_show_type(struct symbol
*sym
, struct type_name
*name
)
150 static struct ctype_name
{
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" },
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
= ' ';
182 memcpy(name
->start
, typenames
[i
].name
, len
);
196 prepend(name
, "struct %s ", show_ident(sym
->ident
));
200 prepend(name
, "union %s ", show_ident(sym
->ident
));
204 prepend(name
, "enum %s ", show_ident(sym
->ident
));
208 append(name
, "%s", show_ident(sym
->ident
));
212 append(name
, ":%d", sym
->fieldwidth
);
216 append(name
, "label(%s:%p)", show_ident(sym
->ident
), sym
);
223 prepend(name
, "unknown type %d", sym
->type
);
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
);
236 append(name
, "<asn:%d>", sym
->ctype
.as
);
243 append(name
, " )( ... )");
247 append(name
, "[%d]", sym
->array_size
);
254 void show_type(struct symbol
*sym
)
257 struct type_name name
;
259 name
.start
= name
.end
= array
+100;
260 do_show_type(sym
, &name
);
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
);
276 void show_symbol(struct symbol
*sym
)
284 type
= sym
->ctype
.base_type
;
289 * Show actual implementation information
291 switch (type
->type
) {
293 symbol_iterate(type
->symbol_list
, show_struct_member
, NULL
);
297 symbol_iterate(type
->symbol_list
, show_struct_member
, NULL
);
302 show_statement(type
->stmt
);
309 if (sym
->initializer
) {
311 show_expression(sym
->initializer
);
315 static int show_return_stmt(struct statement
*stmt
)
317 struct expression
*expr
= stmt
->expression
;
320 int val
= show_expression(expr
);
321 printf("\tmov.%d\t\tretval,v%d\n",
322 expr
->ctype
->bit_size
, val
);
328 static int show_symbol_init(struct symbol
*sym
);
331 * Print out a statement
333 int show_statement(struct statement
*stmt
)
337 switch (stmt
->type
) {
339 return show_return_stmt(stmt
);
340 case STMT_COMPOUND
: {
345 FOR_EACH_PTR(stmt
->syms
, sym
) {
346 show_symbol_init(sym
);
349 FOR_EACH_PTR(stmt
->stmts
, s
) {
350 last
= show_statement(s
);
355 case STMT_EXPRESSION
:
356 return show_expression(stmt
->expression
);
359 show_expression(stmt
->if_conditional
);
361 show_statement(stmt
->if_true
);
362 if (stmt
->if_false
) {
364 show_statement(stmt
->if_false
);
368 printf("\tswitch (");
369 show_expression(stmt
->switch_expression
);
371 show_statement(stmt
->switch_statement
);
375 if (!stmt
->case_expression
)
379 show_expression(stmt
->case_expression
);
382 show_expression(stmt
->case_to
);
386 show_statement(stmt
->case_statement
);
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
) {
409 show_statement(pre_statement
);
411 show_expression(pre_condition
);
413 show_statement(post_statement
);
415 show_statement(statement
);
416 } else if (pre_condition
) {
418 show_statement(pre_statement
);
422 show_expression(pre_condition
);
424 show_statement(statement
);
427 show_statement(pre_statement
);
431 show_statement(statement
);
433 show_expression(post_condition
);
443 printf("\tcontinue");
447 show_symbol(stmt
->label_identifier
);
449 show_statement(stmt
->label_statement
);
453 if (stmt
->goto_expression
) {
455 show_expression(stmt
->goto_expression
);
458 show_symbol(stmt
->goto_label
);
462 printf("\tasm( .... )");
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)
499 static int show_call_expression(struct expression
*expr
)
501 struct symbol
*direct
;
502 struct expression
*arg
, *fn
;
507 warn(expr
->pos
, "\tcall with no type!");
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;
521 /* Remove dereference, if any */
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
)
531 printf("\tcall\t\t%s\n", show_ident(direct
->ident
));
533 fncall
= show_expression(fn
);
534 printf("\tcall\t\t*v%d\n", fncall
);
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
);
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();
550 static const char *name
[] = {
551 ['+'] = "add", ['-'] = "sub",
552 ['*'] = "mul", ['/'] = "div",
555 unsigned int op
= expr
->op
;
557 opname
= show_special(op
);
558 if (op
< sizeof(name
)/sizeof(*name
))
560 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname
,
561 expr
->ctype
->bit_size
,
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",
574 unsigned int op
= expr
->op
;
577 opname
= show_special(op
);
578 if (op
< sizeof(name
)/sizeof(*name
))
580 printf("\t%s.%d\t\tv%d,v%d\n", opname
, expr
->ctype
->bit_size
, new, target
);
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
)
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);
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
);
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
);
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
);
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
);
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..
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
));
675 static int show_symbol_init(struct symbol
*sym
)
677 struct expression
*expr
= sym
->initializer
;
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
);
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
)
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
;
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
)
714 is_signed
= type_is_signed(old_type
);
716 printf("\tsext%d.%d\tv%d,v%d\n", oldbits
, newbits
, new, op
);
718 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits
, new, op
, (1UL << oldbits
)-1);
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
);
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
));
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();
754 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond
, expr
->ctype
->bit_size
, new, true, false);
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");
770 * Print out an expression. Return the pseudo that contains the
773 int show_expression(struct expression
*expr
)
778 switch (expr
->type
) {
780 return show_call_expression(expr
);
782 case EXPR_ASSIGNMENT
:
783 return show_assignment(expr
);
789 return show_binop(expr
);
791 return show_preop(expr
);
793 return show_postop(expr
);
795 return show_symbol_expr(expr
->symbol
);
798 warn(expr
->pos
, "invalid expression after evaluation");
801 return show_cast_expr(expr
);
803 return show_value(expr
);
805 return show_string_expr(expr
);
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");
814 warn(expr
->pos
, "unable to show index expression");
816 case EXPR_CONDITIONAL
:
817 return show_conditional_expr(expr
);
819 return show_statement_expr(expr
);