4 * Copyright (C) 2003 Transmeta Corp.
6 * Licensed under the Open Software License version 1.1
8 * Print out results of parsing for debugging and testing.
23 #include "expression.h"
26 static int show_symbol_expr(struct symbol
*sym
);
28 static void do_debug_symbol(struct symbol
*sym
, int indent
)
30 static const char indent_string
[] = " ";
31 static const char *typestr
[] = {
32 "base", "node", "ptr.", "fn..",
33 "arry", "strt", "unin", "enum",
34 "tdef", "tpof", "memb", "bitf",
40 fprintf(stderr
, "%.*s%s%3d:%lu %lx %s (as: %d, context: %x:%x) %p (%s:%d:%d)\n",
41 indent
, indent_string
, typestr
[sym
->type
],
42 sym
->bit_size
, sym
->ctype
.alignment
,
43 sym
->ctype
.modifiers
, show_ident(sym
->ident
),
44 sym
->ctype
.as
, sym
->ctype
.context
, sym
->ctype
.contextmask
,
45 sym
, input_streams
[sym
->pos
.stream
].name
, sym
->pos
.line
, sym
->pos
.pos
);
46 if (sym
->type
== SYM_FN
) {
49 FOR_EACH_PTR(sym
->arguments
, arg
) {
50 fprintf(stderr
, "< arg%d:\n", i
);
51 do_debug_symbol(arg
, 0);
52 fprintf(stderr
, " end arg%d >\n", i
);
56 do_debug_symbol(sym
->ctype
.base_type
, indent
+2);
59 void debug_symbol(struct symbol
*sym
)
61 do_debug_symbol(sym
, 0);
65 * Symbol type printout. The type system is by far the most
66 * complicated part of C - everything else is trivial.
68 const char *modifier_string(unsigned long mod
)
70 static char buffer
[100];
72 const char *res
,**ptr
, *names
[] = {
73 "auto", "register", "static", "extern",
74 "const", "volatile", "[signed]", "[unsigned]",
75 "[char]", "[short]", "[long]", "[long]",
76 "[typdef]", "[structof]", "[unionof]", "[enum]",
77 "[typeof]", "[attribute]", "inline", "[addressable]",
78 "[nocast]", "[noderef]",
82 while ((res
= *ptr
++) != NULL
) {
85 while ((c
= *res
++) != '\0')
95 void show_struct_member(struct symbol
*sym
, void *data
, int flags
)
97 if (flags
& ITERATE_FIRST
)
99 printf("%s:%d:%ld at offset %ld", show_ident(sym
->ident
), sym
->bit_size
, sym
->ctype
.alignment
, sym
->offset
);
101 printf("[%d..%d]", sym
->bit_offset
, sym
->bit_offset
+sym
->fieldwidth
-1);
102 if (flags
& ITERATE_LAST
)
108 static void show_one_symbol(struct symbol
*sym
, void *sep
, int flags
)
111 if (!(flags
& ITERATE_LAST
))
112 printf("%s", (const char *)sep
);
115 void show_symbol_list(struct symbol_list
*list
, const char *sep
)
117 symbol_iterate(list
, show_one_symbol
, (void *)sep
);
125 static void prepend(struct type_name
*name
, const char *fmt
, ...)
127 static char buffer
[512];
132 n
= vsprintf(buffer
, fmt
, args
);
136 memcpy(name
->start
, buffer
, n
);
139 static void append(struct type_name
*name
, const char *fmt
, ...)
141 static char buffer
[512];
146 n
= vsprintf(buffer
, fmt
, args
);
149 memcpy(name
->end
, buffer
, n
);
153 static void do_show_type(struct symbol
*sym
, struct type_name
*name
)
157 static struct ctype_name
{
161 { & char_ctype
, "char" },
162 { &uchar_ctype
, "unsigned char" },
163 { & short_ctype
, "short" },
164 { &ushort_ctype
, "unsigned short" },
165 { & int_ctype
, "int" },
166 { &uint_ctype
, "unsigned int" },
167 { & long_ctype
, "long" },
168 { &ulong_ctype
, "unsigned long" },
169 { & llong_ctype
, "long long" },
170 { &ullong_ctype
, "unsigned long long" },
172 { &void_ctype
, "void" },
173 { &bool_ctype
, "bool" },
174 { &string_ctype
, "string" },
176 { &float_ctype
, "float" },
177 { &double_ctype
, "double" },
178 { &ldouble_ctype
,"long double" },
184 for (i
= 0; i
< sizeof(typenames
)/sizeof(typenames
[0]); i
++) {
185 if (typenames
[i
].sym
== sym
) {
186 int len
= strlen(typenames
[i
].name
);
187 *--name
->start
= ' ';
189 memcpy(name
->start
, typenames
[i
].name
, len
);
203 prepend(name
, "struct %s ", show_ident(sym
->ident
));
207 prepend(name
, "union %s ", show_ident(sym
->ident
));
211 prepend(name
, "enum %s ", show_ident(sym
->ident
));
215 append(name
, "%s", show_ident(sym
->ident
));
219 append(name
, ":%d", sym
->fieldwidth
);
223 append(name
, "label(%s:%p)", show_ident(sym
->ident
), sym
);
230 prepend(name
, "unknown type %d", sym
->type
);
234 mod
= modifier_string(sym
->ctype
.modifiers
);
235 modlen
= strlen(mod
);
236 name
->start
-= modlen
;
237 memcpy(name
->start
, mod
, modlen
);
239 do_show_type(sym
->ctype
.base_type
, name
);
243 append(name
, "<asn:%d>", sym
->ctype
.as
);
250 append(name
, " )( ... )");
254 append(name
, "[%d]", sym
->array_size
);
261 void show_type(struct symbol
*sym
)
264 struct type_name name
;
266 name
.start
= name
.end
= array
+100;
267 do_show_type(sym
, &name
);
269 printf("%s", name
.start
);
272 const char *show_typename(struct symbol
*sym
)
274 static char array
[200];
275 struct type_name name
;
277 name
.start
= name
.end
= array
+100;
278 do_show_type(sym
, &name
);
283 void show_symbol(struct symbol
*sym
)
290 if (sym
->ctype
.alignment
)
291 printf(".align %ld\n", sym
->ctype
.alignment
);
294 type
= sym
->ctype
.base_type
;
299 * Show actual implementation information
301 switch (type
->type
) {
303 symbol_iterate(type
->symbol_list
, show_struct_member
, NULL
);
307 symbol_iterate(type
->symbol_list
, show_struct_member
, NULL
);
311 struct statement
*stmt
= type
->stmt
;
315 val
= show_statement(stmt
);
317 printf("\tmov.%d\t\tretval,%d\n", stmt
->ret
->bit_size
, val
);
327 if (sym
->initializer
) {
329 show_expression(sym
->initializer
);
333 static int show_symbol_init(struct symbol
*sym
);
335 static int new_pseudo(void)
341 static int new_label(void)
343 static int label
= 0;
347 static void show_switch_statement(struct statement
*stmt
)
349 int val
= show_expression(stmt
->switch_expression
);
351 printf("\tswitch v%d\n", val
);
354 * Debugging only: Check that the case list is correct
355 * by printing it out.
357 * This is where a _real_ back-end would go through the
358 * cases to decide whether to use a lookup table or a
359 * series of comparisons etc
361 printf("# case table:\n");
362 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
363 struct statement
*case_stmt
= sym
->stmt
;
364 struct expression
*expr
= case_stmt
->case_expression
;
365 struct expression
*to
= case_stmt
->case_to
;
370 if (expr
->type
== EXPR_VALUE
) {
371 printf(" case %lld", expr
->value
);
373 if (to
->type
== EXPR_VALUE
) {
374 printf(" .. %lld", to
->value
);
382 printf(": .L%p\n", sym
);
384 printf("# end case table\n");
386 show_statement(stmt
->switch_statement
);
388 if (stmt
->switch_break
->used
)
389 printf(".L%p:\n", stmt
->switch_break
);
392 static void show_symbol_decl(struct symbol_list
*syms
)
395 FOR_EACH_PTR(syms
, sym
) {
396 show_symbol_init(sym
);
400 static int show_return_stmt(struct statement
*stmt
);
403 * Print out a statement
405 int show_statement(struct statement
*stmt
)
409 switch (stmt
->type
) {
411 return show_return_stmt(stmt
);
412 case STMT_COMPOUND
: {
416 show_symbol_decl(stmt
->syms
);
417 FOR_EACH_PTR(stmt
->stmts
, s
) {
418 last
= show_statement(s
);
422 printf(".L%p:\n", stmt
->ret
);
423 addr
= show_symbol_expr(stmt
->ret
);
424 bits
= stmt
->ret
->bit_size
;
426 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, last
, addr
);
431 case STMT_EXPRESSION
:
432 return show_expression(stmt
->expression
);
435 struct expression
*cond
= stmt
->if_conditional
;
437 if (cond
->type
== EXPR_VALUE
) {
438 struct statement
*s
= stmt
->if_true
;
444 val
= show_expression(cond
);
445 target
= new_label();
446 printf("\tje\t\tv%d,.L%d\n", val
, target
);
447 show_statement(stmt
->if_true
);
448 if (stmt
->if_false
) {
449 int last
= new_label();
450 printf("\tjmp\t\t.L%d\n", last
);
451 printf(".L%d:\n", target
);
453 show_statement(stmt
->if_false
);
455 printf(".L%d:\n", target
);
459 show_switch_statement(stmt
);
463 printf(".L%p:\n", stmt
->case_label
);
464 show_statement(stmt
->case_statement
);
467 case STMT_ITERATOR
: {
468 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
469 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
470 struct statement
*statement
= stmt
->iterator_statement
;
471 struct statement
*post_statement
= stmt
->iterator_post_statement
;
472 struct expression
*post_condition
= stmt
->iterator_post_condition
;
473 int val
, loop_top
= 0, loop_bottom
= 0;
475 show_symbol_decl(stmt
->iterator_syms
);
476 show_statement(pre_statement
);
478 if (pre_condition
->type
== EXPR_VALUE
) {
479 if (!pre_condition
->value
)
481 pre_condition
= NULL
;
483 loop_bottom
= new_label();
484 val
= show_expression(pre_condition
);
485 printf("\tje\t\tv%d, .L%d\n", val
, loop_bottom
);
488 if (!post_condition
|| post_condition
->type
!= EXPR_VALUE
|| post_condition
->value
) {
489 loop_top
= new_label();
490 printf(".L%d:\n", loop_top
);
492 show_statement(statement
);
493 if (stmt
->iterator_continue
->used
)
494 printf(".L%p:\n", stmt
->iterator_continue
);
495 show_statement(post_statement
);
496 if (!post_condition
) {
497 printf("\tjmp\t\t.L%d\n", loop_top
);
498 } else if (post_condition
->type
== EXPR_VALUE
) {
499 if (post_condition
->value
)
500 printf("\tjmp\t\t.L%d\n", loop_top
);
502 val
= show_expression(post_condition
);
503 printf("\tjne\t\tv%d, .L%d\n", val
, loop_top
);
505 if (stmt
->iterator_break
->used
)
506 printf(".L%p:\n", stmt
->iterator_break
);
508 printf(".L%d:\n", loop_bottom
);
515 printf(".L%p:\n", stmt
->label_identifier
);
516 show_statement(stmt
->label_statement
);
520 if (stmt
->goto_expression
) {
521 int val
= show_expression(stmt
->goto_expression
);
522 printf("\tgoto *v%d\n", val
);
524 printf("\tgoto .L%p\n", stmt
->goto_label
);
528 printf("\tasm( .... )\n");
535 static void show_one_statement(struct statement
*stmt
, void *sep
, int flags
)
537 show_statement(stmt
);
538 if (!(flags
& ITERATE_LAST
))
539 printf("%s", (const char *)sep
);
542 void show_statement_list(struct statement_list
*stmt
, const char *sep
)
544 statement_iterate(stmt
, show_one_statement
, (void *)sep
);
547 static void show_one_expression(struct expression
*expr
, void *sep
, int flags
)
549 show_expression(expr
);
550 if (!(flags
& ITERATE_LAST
))
551 printf("%s", (const char *)sep
);
554 void show_expression_list(struct expression_list
*list
, const char *sep
)
556 expression_iterate(list
, show_one_expression
, (void *)sep
);
559 static int show_call_expression(struct expression
*expr
)
561 struct symbol
*direct
;
562 struct expression
*arg
, *fn
;
567 warn(expr
->pos
, "\tcall with no type!");
572 FOR_EACH_PTR_REVERSE(expr
->args
, arg
) {
573 int new = show_expression(arg
);
574 int size
= arg
->ctype
->bit_size
;
575 printf("\tpush.%d\t\tv%d\n", size
, new);
576 framesize
+= size
>> 3;
577 } END_FOR_EACH_PTR_REVERSE
;
581 /* Remove dereference, if any */
583 if (fn
->type
== EXPR_PREOP
) {
584 if (fn
->unop
->type
== EXPR_SYMBOL
) {
585 struct symbol
*sym
= fn
->unop
->symbol
;
586 if (sym
->ctype
.base_type
->type
== SYM_FN
)
591 printf("\tcall\t\t%s\n", show_ident(direct
->ident
));
593 fncall
= show_expression(fn
);
594 printf("\tcall\t\t*v%d\n", fncall
);
597 printf("\tadd.%d\t\tvSP,vSP,$%d\n", BITS_IN_POINTER
, framesize
);
599 retval
= new_pseudo();
600 printf("\tmov.%d\t\tv%d,retval\n", expr
->ctype
->bit_size
, retval
);
604 static int show_binop(struct expression
*expr
)
606 int left
= show_expression(expr
->left
);
607 int right
= show_expression(expr
->right
);
608 int new = new_pseudo();
610 static const char *name
[] = {
611 ['+'] = "add", ['-'] = "sub",
612 ['*'] = "mul", ['/'] = "div",
613 ['%'] = "mod", ['&'] = "and",
614 ['|'] = "lor", ['^'] = "xor"
616 unsigned int op
= expr
->op
;
618 opname
= show_special(op
);
619 if (op
< sizeof(name
)/sizeof(*name
))
621 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname
,
622 expr
->ctype
->bit_size
,
627 static int show_regular_preop(struct expression
*expr
)
629 int target
= show_expression(expr
->unop
);
630 int new = new_pseudo();
631 static const char *name
[] = {
632 ['!'] = "nonzero", ['-'] = "neg",
635 unsigned int op
= expr
->op
;
638 opname
= show_special(op
);
639 if (op
< sizeof(name
)/sizeof(*name
))
641 printf("\t%s.%d\t\tv%d,v%d\n", opname
, expr
->ctype
->bit_size
, new, target
);
646 * FIXME! Not all accesses are memory loads. We should
647 * check what kind of symbol is behind the dereference.
649 static int show_address_gen(struct expression
*expr
)
651 if (expr
->type
== EXPR_PREOP
)
652 return show_expression(expr
->unop
);
653 return show_expression(expr
->address
);
656 static int show_load_gen(int bits
, struct expression
*expr
, int addr
)
658 int new = new_pseudo();
660 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, new, addr
);
661 if (expr
->type
== EXPR_PREOP
)
666 printf("\tshr.%d\t\tv%d,v%d,$%d\n", bits
, new, new, expr
->bitpos
);
667 printf("\tandi.%d\t\tv%d,v%d,$%llu\n", bits
, new, new, (1ULL << expr
->nrbits
)-1);
671 static void show_store_gen(int bits
, int value
, struct expression
*expr
, int addr
)
673 /* FIXME!!! Bitfield store! */
674 printf("\tst.%d\t\tv%d,[v%d]\n", bits
, value
, addr
);
677 static int show_assignment(struct expression
*expr
)
679 struct expression
*target
= expr
->left
;
685 bits
= expr
->ctype
->bit_size
;
686 val
= show_expression(expr
->right
);
687 addr
= show_address_gen(target
);
688 show_store_gen(bits
, val
, target
, addr
);
692 static int show_return_stmt(struct statement
*stmt
)
694 struct expression
*expr
= stmt
->ret_value
;
695 struct symbol
*target
= stmt
->ret_target
;
697 if (expr
&& expr
->ctype
) {
698 int val
= show_expression(expr
);
699 int bits
= expr
->ctype
->bit_size
;
700 int addr
= show_symbol_expr(target
);
701 show_store_gen(bits
, val
, NULL
, addr
);
703 printf("\tgoto .L%p\n", target
);
707 static int show_initialization(struct symbol
*sym
, struct expression
*expr
)
714 bits
= expr
->ctype
->bit_size
;
715 val
= show_expression(expr
);
716 addr
= show_symbol_expr(sym
);
717 // FIXME! The "target" expression is for bitfield store information.
718 // Leave it NULL, which works fine.
719 show_store_gen(bits
, val
, NULL
, addr
);
723 static int show_access(struct expression
*expr
)
725 int addr
= show_address_gen(expr
);
726 return show_load_gen(expr
->ctype
->bit_size
, expr
, addr
);
729 static int show_inc_dec(struct expression
*expr
, int postop
)
731 int addr
= show_address_gen(expr
->unop
);
733 const char *opname
= expr
->op
== SPECIAL_INCREMENT
? "add" : "sub";
734 int bits
= expr
->ctype
->bit_size
;
736 retval
= show_load_gen(bits
, expr
->unop
, addr
);
740 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname
, bits
, new, retval
);
741 show_store_gen(bits
, new, expr
->unop
, addr
);
745 static int show_preop(struct expression
*expr
)
748 * '*' is an lvalue access, and is fundamentally different
749 * from an arithmetic operation. Maybe it should have an
750 * expression type of its own..
753 return show_access(expr
);
754 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
755 return show_inc_dec(expr
, 0);
756 return show_regular_preop(expr
);
759 static int show_postop(struct expression
*expr
)
761 return show_inc_dec(expr
, 1);
764 static int show_symbol_expr(struct symbol
*sym
)
766 int new = new_pseudo();
768 if (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_EXTERN
| MOD_STATIC
)) {
769 printf("\tmovi.%d\t\tv%d,$%s\n", BITS_IN_POINTER
, new, show_ident(sym
->ident
));
772 if (sym
->ctype
.modifiers
& MOD_ADDRESSABLE
) {
773 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", BITS_IN_POINTER
, new, sym
->value
);
776 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", BITS_IN_POINTER
, new, show_ident(sym
->ident
), sym
);
780 static int show_symbol_init(struct symbol
*sym
)
782 struct expression
*expr
= sym
->initializer
;
787 bits
= expr
->ctype
->bit_size
;
788 val
= show_expression(expr
);
789 addr
= show_symbol_expr(sym
);
790 show_store_gen(bits
, val
, NULL
, addr
);
795 static int type_is_signed(struct symbol
*sym
)
797 if (sym
->type
== SYM_NODE
)
798 sym
= sym
->ctype
.base_type
;
799 if (sym
->type
== SYM_PTR
)
801 return !(sym
->ctype
.modifiers
& MOD_UNSIGNED
);
804 static int show_cast_expr(struct expression
*expr
)
806 struct symbol
*old_type
, *new_type
;
807 int op
= show_expression(expr
->cast_expression
);
808 int oldbits
, newbits
;
811 old_type
= expr
->cast_expression
->ctype
;
812 new_type
= expr
->cast_type
;
814 oldbits
= old_type
->bit_size
;
815 newbits
= new_type
->bit_size
;
816 if (oldbits
>= newbits
)
819 is_signed
= type_is_signed(old_type
);
821 printf("\tsext%d.%d\tv%d,v%d\n", oldbits
, newbits
, new, op
);
823 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits
, new, op
, (1UL << oldbits
)-1);
828 static int show_value(struct expression
*expr
)
830 int new = new_pseudo();
831 unsigned long long value
= expr
->value
;
833 printf("\tmovi.%d\t\tv%d,$%llu\n", expr
->ctype
->bit_size
, new, value
);
837 static int show_string_expr(struct expression
*expr
)
839 int new = new_pseudo();
841 printf("\tmovi.%d\t\tv%d,&%s\n", BITS_IN_POINTER
, new, show_string(expr
->string
));
845 static int show_bitfield_expr(struct expression
*expr
)
847 return show_access(expr
);
850 int show_label_expr(struct expression
*expr
)
852 int new = new_pseudo();
853 printf("\tmovi.%d\t\tv%d,.L%p\n",BITS_IN_POINTER
, new, expr
->label_symbol
);
857 static int show_conditional_expr(struct expression
*expr
)
859 int cond
= show_expression(expr
->conditional
);
860 int true = show_expression(expr
->cond_true
);
861 int false = show_expression(expr
->cond_false
);
862 int new = new_pseudo();
866 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond
, expr
->ctype
->bit_size
, new, true, false);
870 static int show_statement_expr(struct expression
*expr
)
872 return show_statement(expr
->statement
);
875 static int show_position_expr(struct expression
*expr
, struct symbol
*base
)
877 int new = show_expression(expr
->init_expr
);
878 struct symbol
*ctype
= expr
->init_sym
;
880 printf("\tinsert v%d at [%d:%d] of %s\n", new,
881 expr
->init_offset
, ctype
->bit_offset
,
882 show_ident(base
->ident
));
886 static int show_initializer_expr(struct expression
*expr
, struct symbol
*ctype
)
888 struct expression
*entry
;
890 FOR_EACH_PTR(expr
->expr_list
, entry
) {
891 // Nested initializers have their positions already
892 // recursively calculated - just output them too
893 if (entry
->type
== EXPR_INITIALIZER
) {
894 show_initializer_expr(entry
, ctype
);
898 // Ignore initializer indexes and identifiers - the
899 // evaluator has taken them into account
900 if (entry
->type
== EXPR_IDENTIFIER
|| entry
->type
== EXPR_INDEX
)
902 if (entry
->type
== EXPR_POS
) {
903 show_position_expr(entry
, ctype
);
906 show_initialization(ctype
, entry
);
912 * Print out an expression. Return the pseudo that contains the
915 int show_expression(struct expression
*expr
)
921 struct position
*pos
= &expr
->pos
;
922 printf("\tno type at %s:%d:%d\n",
923 input_streams
[pos
->stream
].name
,
924 pos
->line
, pos
->pos
);
928 switch (expr
->type
) {
930 return show_call_expression(expr
);
932 case EXPR_ASSIGNMENT
:
933 return show_assignment(expr
);
939 return show_binop(expr
);
941 return show_preop(expr
);
943 return show_postop(expr
);
945 return show_symbol_expr(expr
->symbol
);
948 warn(expr
->pos
, "invalid expression after evaluation");
951 return show_cast_expr(expr
);
953 return show_value(expr
);
955 return show_string_expr(expr
);
957 return show_bitfield_expr(expr
);
958 case EXPR_INITIALIZER
:
959 return show_initializer_expr(expr
, expr
->ctype
);
960 case EXPR_CONDITIONAL
:
961 return show_conditional_expr(expr
);
963 return show_statement_expr(expr
);
965 return show_label_expr(expr
);
967 // None of these should exist as direct expressions: they are only
968 // valid as sub-expressions of initializers.
970 warn(expr
->pos
, "unable to show plain initializer position expression");
972 case EXPR_IDENTIFIER
:
973 warn(expr
->pos
, "unable to show identifier expression");
976 warn(expr
->pos
, "unable to show index expression");