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) %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
,
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];
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]",
68 while ((res
= *ptr
++) != NULL
) {
71 while ((c
= *res
++) != '\0')
81 void show_struct_member(struct symbol
*sym
, void *data
, int flags
)
83 if (flags
& ITERATE_FIRST
)
85 printf("%s:%d:%ld at offset %ld", show_ident(sym
->ident
), sym
->bit_size
, sym
->ctype
.alignment
, sym
->offset
);
87 printf("[%d..%d]", sym
->bit_offset
, sym
->bit_offset
+sym
->fieldwidth
-1);
88 if (flags
& ITERATE_LAST
)
94 static void show_one_symbol(struct symbol
*sym
, void *sep
, int flags
)
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
);
111 static void prepend(struct type_name
*name
, const char *fmt
, ...)
113 static char buffer
[512];
118 n
= vsprintf(buffer
, fmt
, args
);
122 memcpy(name
->start
, buffer
, n
);
125 static void append(struct type_name
*name
, const char *fmt
, ...)
127 static char buffer
[512];
132 n
= vsprintf(buffer
, fmt
, args
);
135 memcpy(name
->end
, buffer
, n
);
139 static void do_show_type(struct symbol
*sym
, struct type_name
*name
)
143 static struct ctype_name
{
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" },
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
= ' ';
175 memcpy(name
->start
, typenames
[i
].name
, len
);
189 prepend(name
, "struct %s ", show_ident(sym
->ident
));
193 prepend(name
, "union %s ", show_ident(sym
->ident
));
197 prepend(name
, "enum %s ", show_ident(sym
->ident
));
201 append(name
, "%s", show_ident(sym
->ident
));
205 append(name
, ":%d", sym
->fieldwidth
);
209 append(name
, "label(%s:%p)", show_ident(sym
->ident
), sym
);
216 prepend(name
, "unknown type %d", sym
->type
);
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
);
229 append(name
, "<asn:%d>", sym
->ctype
.as
);
236 append(name
, " )( ... )");
240 append(name
, "[%d]", sym
->array_size
);
247 void show_type(struct symbol
*sym
)
250 struct type_name name
;
252 name
.start
= name
.end
= array
+100;
253 do_show_type(sym
, &name
);
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
);
269 void show_symbol(struct symbol
*sym
)
277 type
= sym
->ctype
.base_type
;
282 * Show actual implementation information
284 switch (type
->type
) {
286 symbol_iterate(type
->symbol_list
, show_struct_member
, NULL
);
290 symbol_iterate(type
->symbol_list
, show_struct_member
, NULL
);
295 show_statement(type
->stmt
);
302 if (sym
->initializer
) {
304 show_expression(sym
->initializer
);
308 static int show_return_stmt(struct statement
*stmt
)
310 struct expression
*expr
= stmt
->expression
;
313 int val
= show_expression(expr
);
314 printf("\tmov.%d\t\tretval,v%d\n",
315 expr
->ctype
->bit_size
, val
);
321 static int show_symbol_init(struct symbol
*sym
);
323 static int new_label(void)
325 static int label
= 0;
330 * Print out a statement
332 int show_statement(struct statement
*stmt
)
336 switch (stmt
->type
) {
338 return show_return_stmt(stmt
);
339 case STMT_COMPOUND
: {
344 FOR_EACH_PTR(stmt
->syms
, sym
) {
345 show_symbol_init(sym
);
348 FOR_EACH_PTR(stmt
->stmts
, s
) {
349 last
= show_statement(s
);
354 case STMT_EXPRESSION
:
355 return show_expression(stmt
->expression
);
358 struct expression
*cond
= stmt
->if_conditional
;
360 if (cond
->type
== EXPR_VALUE
) {
361 struct statement
*s
= stmt
->if_true
;
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
);
376 show_statement(stmt
->if_false
);
378 printf(".L%d:\n", target
);
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
);
391 if (!stmt
->case_expression
)
394 printf("case %lld", stmt
->case_expression
->value
);
396 printf("...%lld", stmt
->case_to
->value
);
401 show_statement(stmt
->case_statement
);
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
);
414 if (pre_condition
->type
== EXPR_VALUE
) {
415 if (!pre_condition
->value
)
417 pre_condition
= NULL
;
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
);
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
);
443 printf(".L%d:\n", loop_bottom
);
447 printf("\t!NONE!\n");
451 printf(".L%p:\n", stmt
->label_identifier
);
452 show_statement(stmt
->label_statement
);
456 if (stmt
->goto_expression
) {
457 int val
= show_expression(stmt
->goto_expression
);
458 printf("\tgoto *v%d\n", val
);
460 printf("\tgoto .L%p\n", stmt
->goto_label
);
464 printf("\tasm( .... )\n");
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)
501 static int show_call_expression(struct expression
*expr
)
503 struct symbol
*direct
;
504 struct expression
*arg
, *fn
;
509 warn(expr
->pos
, "\tcall with no type!");
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;
523 /* Remove dereference, if any */
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
)
533 printf("\tcall\t\t%s\n", show_ident(direct
->ident
));
535 fncall
= show_expression(fn
);
536 printf("\tcall\t\t*v%d\n", fncall
);
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
);
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();
552 static const char *name
[] = {
553 ['+'] = "add", ['-'] = "sub",
554 ['*'] = "mul", ['/'] = "div",
557 unsigned int op
= expr
->op
;
559 opname
= show_special(op
);
560 if (op
< sizeof(name
)/sizeof(*name
))
562 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname
,
563 expr
->ctype
->bit_size
,
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",
576 unsigned int op
= expr
->op
;
579 opname
= show_special(op
);
580 if (op
< sizeof(name
)/sizeof(*name
))
582 printf("\t%s.%d\t\tv%d,v%d\n", opname
, expr
->ctype
->bit_size
, new, target
);
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
)
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);
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
;
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
);
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
);
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
);
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
);
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..
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
));
682 if (sym
->ctype
.modifiers
& MOD_ADDRESSABLE
) {
683 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", BITS_IN_POINTER
, new, sym
->value
);
686 printf("\taddi.%d\t\tv%d,vFP,$xxx\n", BITS_IN_POINTER
, new);
690 static int show_symbol_init(struct symbol
*sym
)
692 struct expression
*expr
= sym
->initializer
;
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
);
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
)
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
;
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
)
729 is_signed
= type_is_signed(old_type
);
731 printf("\tsext%d.%d\tv%d,v%d\n", oldbits
, newbits
, new, op
);
733 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits
, new, op
, (1UL << oldbits
)-1);
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
);
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
));
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();
769 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond
, expr
->ctype
->bit_size
, new, true, false);
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");
785 * Print out an expression. Return the pseudo that contains the
788 int show_expression(struct expression
*expr
)
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
);
801 switch (expr
->type
) {
803 return show_call_expression(expr
);
805 case EXPR_ASSIGNMENT
:
806 return show_assignment(expr
);
812 return show_binop(expr
);
814 return show_preop(expr
);
816 return show_postop(expr
);
818 return show_symbol_expr(expr
->symbol
);
821 warn(expr
->pos
, "invalid expression after evaluation");
824 return show_cast_expr(expr
);
826 return show_value(expr
);
828 return show_string_expr(expr
);
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");
837 warn(expr
->pos
, "unable to show index expression");
839 case EXPR_CONDITIONAL
:
840 return show_conditional_expr(expr
);
842 return show_statement_expr(expr
);