4 * Copyright (C) 2003 Transmeta Corp.
7 * Licensed under the Open Software License version 1.1
9 * Print out results of parsing for debugging and testing.
24 #include "expression.h"
27 static int show_symbol_expr(struct symbol
*sym
);
29 static void do_debug_symbol(struct symbol
*sym
, int indent
)
31 static const char indent_string
[] = " ";
32 static const char *typestr
[] = {
33 "base", "node", "ptr.", "fn..",
34 "arry", "strt", "unin", "enum",
35 "tdef", "tpof", "memb", "bitf",
41 fprintf(stderr
, "%.*s%s%3d:%lu %lx %s (as: %d, context: %x:%x) %p (%s:%d:%d)\n",
42 indent
, indent_string
, typestr
[sym
->type
],
43 sym
->bit_size
, sym
->ctype
.alignment
,
44 sym
->ctype
.modifiers
, show_ident(sym
->ident
),
45 sym
->ctype
.as
, sym
->ctype
.context
, sym
->ctype
.contextmask
,
46 sym
, input_streams
[sym
->pos
.stream
].name
, sym
->pos
.line
, sym
->pos
.pos
);
47 if (sym
->type
== SYM_FN
) {
50 FOR_EACH_PTR(sym
->arguments
, arg
) {
51 fprintf(stderr
, "< arg%d:\n", i
);
52 do_debug_symbol(arg
, 0);
53 fprintf(stderr
, " end arg%d >\n", i
);
57 do_debug_symbol(sym
->ctype
.base_type
, indent
+2);
60 void debug_symbol(struct symbol
*sym
)
62 do_debug_symbol(sym
, 0);
66 * Symbol type printout. The type system is by far the most
67 * complicated part of C - everything else is trivial.
69 const char *modifier_string(unsigned long mod
)
71 static char buffer
[100];
73 const char *res
,**ptr
, *names
[] = {
74 "auto", "register", "static", "extern",
75 "const", "volatile", "[signed]", "[unsigned]",
76 "[char]", "[short]", "[long]", "[long]",
77 "[typdef]", "[structof]", "[unionof]", "[enum]",
78 "[typeof]", "[attribute]", "inline", "[addressable]",
79 "[nocast]", "[noderef]", "[accessed]", "[toplevel]",
80 "[label]", "[assigned]", "[type]", "[safe]",
81 "[usertype]", "[force]",
85 while ((res
= *ptr
++) != NULL
) {
88 while ((c
= *res
++) != '\0')
98 void show_struct_member(struct symbol
*sym
, void *data
, int flags
)
100 if (flags
& ITERATE_FIRST
)
102 printf("%s:%d:%ld at offset %ld", show_ident(sym
->ident
), sym
->bit_size
, sym
->ctype
.alignment
, sym
->offset
);
104 printf("[%d..%d]", sym
->bit_offset
, sym
->bit_offset
+sym
->fieldwidth
-1);
105 if (flags
& ITERATE_LAST
)
111 static void show_one_symbol(struct symbol
*sym
, void *sep
, int flags
)
114 if (!(flags
& ITERATE_LAST
))
115 printf("%s", (const char *)sep
);
118 void show_symbol_list(struct symbol_list
*list
, const char *sep
)
120 symbol_iterate(list
, show_one_symbol
, (void *)sep
);
128 static void prepend(struct type_name
*name
, const char *fmt
, ...)
130 static char buffer
[512];
135 n
= vsprintf(buffer
, fmt
, args
);
139 memcpy(name
->start
, buffer
, n
);
142 static void append(struct type_name
*name
, const char *fmt
, ...)
144 static char buffer
[512];
149 n
= vsprintf(buffer
, fmt
, args
);
152 memcpy(name
->end
, buffer
, n
);
156 static void do_show_type(struct symbol
*sym
, struct type_name
*name
)
160 static struct ctype_name
{
164 { & char_ctype
, "char" },
165 { &uchar_ctype
, "unsigned char" },
166 { & short_ctype
, "short" },
167 { &ushort_ctype
, "unsigned short" },
168 { & int_ctype
, "int" },
169 { &uint_ctype
, "unsigned int" },
170 { & long_ctype
, "long" },
171 { &ulong_ctype
, "unsigned long" },
172 { & llong_ctype
, "long long" },
173 { &ullong_ctype
, "unsigned long long" },
175 { &void_ctype
, "void" },
176 { &bool_ctype
, "bool" },
177 { &string_ctype
, "string" },
179 { &float_ctype
, "float" },
180 { &double_ctype
, "double" },
181 { &ldouble_ctype
,"long double" },
187 for (i
= 0; i
< sizeof(typenames
)/sizeof(typenames
[0]); i
++) {
188 if (typenames
[i
].sym
== sym
) {
189 int len
= strlen(typenames
[i
].name
);
190 *--name
->start
= ' ';
192 memcpy(name
->start
, typenames
[i
].name
, len
);
206 prepend(name
, "struct %s ", show_ident(sym
->ident
));
210 prepend(name
, "union %s ", show_ident(sym
->ident
));
214 prepend(name
, "enum %s ", show_ident(sym
->ident
));
218 append(name
, "%s", show_ident(sym
->ident
));
222 append(name
, ":%d", sym
->fieldwidth
);
226 append(name
, "label(%s:%p)", show_ident(sym
->ident
), sym
);
233 prepend(name
, "unknown type %d", sym
->type
);
237 mod
= modifier_string(sym
->ctype
.modifiers
);
238 modlen
= strlen(mod
);
239 name
->start
-= modlen
;
240 memcpy(name
->start
, mod
, modlen
);
242 do_show_type(sym
->ctype
.base_type
, name
);
246 append(name
, "<asn:%d>", sym
->ctype
.as
);
253 append(name
, " )( ... )");
257 append(name
, "[%lld]", get_expression_value(sym
->array_size
));
264 void show_type(struct symbol
*sym
)
267 struct type_name name
;
269 name
.start
= name
.end
= array
+100;
270 do_show_type(sym
, &name
);
272 printf("%s", name
.start
);
275 const char *show_typename(struct symbol
*sym
)
277 static char array
[200];
278 struct type_name name
;
280 name
.start
= name
.end
= array
+100;
281 do_show_type(sym
, &name
);
286 void show_symbol(struct symbol
*sym
)
293 if (sym
->ctype
.alignment
)
294 printf(".align %ld\n", sym
->ctype
.alignment
);
297 type
= sym
->ctype
.base_type
;
302 * Show actual implementation information
304 switch (type
->type
) {
306 symbol_iterate(type
->symbol_list
, show_struct_member
, NULL
);
310 symbol_iterate(type
->symbol_list
, show_struct_member
, NULL
);
314 struct statement
*stmt
= type
->stmt
;
318 val
= show_statement(stmt
);
320 printf("\tmov.%d\t\tretval,%d\n", stmt
->ret
->bit_size
, val
);
330 if (sym
->initializer
) {
332 show_expression(sym
->initializer
);
336 static int show_symbol_init(struct symbol
*sym
);
338 static int new_pseudo(void)
344 static int new_label(void)
346 static int label
= 0;
350 static void show_switch_statement(struct statement
*stmt
)
352 int val
= show_expression(stmt
->switch_expression
);
354 printf("\tswitch v%d\n", val
);
357 * Debugging only: Check that the case list is correct
358 * by printing it out.
360 * This is where a _real_ back-end would go through the
361 * cases to decide whether to use a lookup table or a
362 * series of comparisons etc
364 printf("# case table:\n");
365 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
366 struct statement
*case_stmt
= sym
->stmt
;
367 struct expression
*expr
= case_stmt
->case_expression
;
368 struct expression
*to
= case_stmt
->case_to
;
373 if (expr
->type
== EXPR_VALUE
) {
374 printf(" case %lld", expr
->value
);
376 if (to
->type
== EXPR_VALUE
) {
377 printf(" .. %lld", to
->value
);
385 printf(": .L%p\n", sym
->bb_target
);
387 printf("# end case table\n");
389 show_statement(stmt
->switch_statement
);
391 if (stmt
->switch_break
->used
)
392 printf(".L%p:\n", stmt
->switch_break
->bb_target
);
395 static void show_symbol_decl(struct symbol_list
*syms
)
398 FOR_EACH_PTR(syms
, sym
) {
399 show_symbol_init(sym
);
403 static int show_return_stmt(struct statement
*stmt
);
406 * Print out a statement
408 int show_statement(struct statement
*stmt
)
412 switch (stmt
->type
) {
414 return show_return_stmt(stmt
);
415 case STMT_COMPOUND
: {
419 show_symbol_decl(stmt
->syms
);
420 FOR_EACH_PTR(stmt
->stmts
, s
) {
421 last
= show_statement(s
);
425 printf(".L%p:\n", stmt
->ret
);
426 addr
= show_symbol_expr(stmt
->ret
);
427 bits
= stmt
->ret
->bit_size
;
429 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, last
, addr
);
434 case STMT_EXPRESSION
:
435 return show_expression(stmt
->expression
);
438 struct expression
*cond
= stmt
->if_conditional
;
440 /* This is only valid if nobody can jump into the "dead" statement */
442 if (cond
->type
== EXPR_VALUE
) {
443 struct statement
*s
= stmt
->if_true
;
450 val
= show_expression(cond
);
451 target
= new_label();
452 printf("\tje\t\tv%d,.L%d\n", val
, target
);
453 show_statement(stmt
->if_true
);
454 if (stmt
->if_false
) {
455 int last
= new_label();
456 printf("\tjmp\t\t.L%d\n", last
);
457 printf(".L%d:\n", target
);
459 show_statement(stmt
->if_false
);
461 printf(".L%d:\n", target
);
465 show_switch_statement(stmt
);
469 printf(".L%p:\n", stmt
->case_label
);
470 show_statement(stmt
->case_statement
);
473 case STMT_ITERATOR
: {
474 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
475 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
476 struct statement
*statement
= stmt
->iterator_statement
;
477 struct statement
*post_statement
= stmt
->iterator_post_statement
;
478 struct expression
*post_condition
= stmt
->iterator_post_condition
;
479 int val
, loop_top
= 0, loop_bottom
= 0;
481 show_symbol_decl(stmt
->iterator_syms
);
482 show_statement(pre_statement
);
484 if (pre_condition
->type
== EXPR_VALUE
) {
485 if (!pre_condition
->value
) {
486 loop_bottom
= new_label();
487 printf("\tjmp\t\t.L%d\n", loop_bottom
);
490 loop_bottom
= new_label();
491 val
= show_expression(pre_condition
);
492 printf("\tje\t\tv%d, .L%d\n", val
, loop_bottom
);
495 if (!post_condition
|| post_condition
->type
!= EXPR_VALUE
|| post_condition
->value
) {
496 loop_top
= new_label();
497 printf(".L%d:\n", loop_top
);
499 show_statement(statement
);
500 if (stmt
->iterator_continue
->used
)
501 printf(".L%p:\n", stmt
->iterator_continue
);
502 show_statement(post_statement
);
503 if (!post_condition
) {
504 printf("\tjmp\t\t.L%d\n", loop_top
);
505 } else if (post_condition
->type
== EXPR_VALUE
) {
506 if (post_condition
->value
)
507 printf("\tjmp\t\t.L%d\n", loop_top
);
509 val
= show_expression(post_condition
);
510 printf("\tjne\t\tv%d, .L%d\n", val
, loop_top
);
512 if (stmt
->iterator_break
->used
)
513 printf(".L%p:\n", stmt
->iterator_break
);
515 printf(".L%d:\n", loop_bottom
);
522 printf(".L%p:\n", stmt
->label_identifier
);
523 show_statement(stmt
->label_statement
);
527 if (stmt
->goto_expression
) {
528 int val
= show_expression(stmt
->goto_expression
);
529 printf("\tgoto\t\t*v%d\n", val
);
531 printf("\tgoto\t\t.L%p\n", stmt
->goto_label
->bb_target
);
535 printf("\tasm( .... )\n");
542 static void show_one_statement(struct statement
*stmt
, void *sep
, int flags
)
544 show_statement(stmt
);
545 if (!(flags
& ITERATE_LAST
))
546 printf("%s", (const char *)sep
);
549 void show_statement_list(struct statement_list
*stmt
, const char *sep
)
551 statement_iterate(stmt
, show_one_statement
, (void *)sep
);
554 static void show_one_expression(struct expression
*expr
, void *sep
, int flags
)
556 show_expression(expr
);
557 if (!(flags
& ITERATE_LAST
))
558 printf("%s", (const char *)sep
);
561 void show_expression_list(struct expression_list
*list
, const char *sep
)
563 expression_iterate(list
, show_one_expression
, (void *)sep
);
566 static int show_call_expression(struct expression
*expr
)
568 struct symbol
*direct
;
569 struct expression
*arg
, *fn
;
574 warn(expr
->pos
, "\tcall with no type!");
579 FOR_EACH_PTR_REVERSE(expr
->args
, arg
) {
580 int new = show_expression(arg
);
581 int size
= arg
->ctype
->bit_size
;
582 printf("\tpush.%d\t\tv%d\n", size
, new);
583 framesize
+= size
>> 3;
584 } END_FOR_EACH_PTR_REVERSE
;
588 /* Remove dereference, if any */
590 if (fn
->type
== EXPR_PREOP
) {
591 if (fn
->unop
->type
== EXPR_SYMBOL
) {
592 struct symbol
*sym
= fn
->unop
->symbol
;
593 if (sym
->ctype
.base_type
->type
== SYM_FN
)
598 printf("\tcall\t\t%s\n", show_ident(direct
->ident
));
600 fncall
= show_expression(fn
);
601 printf("\tcall\t\t*v%d\n", fncall
);
604 printf("\tadd.%d\t\tvSP,vSP,$%d\n", bits_in_pointer
, framesize
);
606 retval
= new_pseudo();
607 printf("\tmov.%d\t\tv%d,retval\n", expr
->ctype
->bit_size
, retval
);
611 static int show_binop(struct expression
*expr
)
613 int left
= show_expression(expr
->left
);
614 int right
= show_expression(expr
->right
);
615 int new = new_pseudo();
617 static const char *name
[] = {
618 ['+'] = "add", ['-'] = "sub",
619 ['*'] = "mul", ['/'] = "div",
620 ['%'] = "mod", ['&'] = "and",
621 ['|'] = "lor", ['^'] = "xor"
623 unsigned int op
= expr
->op
;
625 opname
= show_special(op
);
626 if (op
< sizeof(name
)/sizeof(*name
))
628 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname
,
629 expr
->ctype
->bit_size
,
634 static int show_regular_preop(struct expression
*expr
)
636 int target
= show_expression(expr
->unop
);
637 int new = new_pseudo();
638 static const char *name
[] = {
639 ['!'] = "nonzero", ['-'] = "neg",
642 unsigned int op
= expr
->op
;
645 opname
= show_special(op
);
646 if (op
< sizeof(name
)/sizeof(*name
))
648 printf("\t%s.%d\t\tv%d,v%d\n", opname
, expr
->ctype
->bit_size
, new, target
);
653 * FIXME! Not all accesses are memory loads. We should
654 * check what kind of symbol is behind the dereference.
656 static int show_address_gen(struct expression
*expr
)
658 if (expr
->type
== EXPR_PREOP
)
659 return show_expression(expr
->unop
);
660 return show_expression(expr
->address
);
663 static int show_load_gen(int bits
, struct expression
*expr
, int addr
)
665 int new = new_pseudo();
667 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, new, addr
);
668 if (expr
->type
== EXPR_PREOP
)
673 printf("\tshr.%d\t\tv%d,v%d,$%d\n", bits
, new, new, expr
->bitpos
);
674 printf("\tandi.%d\t\tv%d,v%d,$%llu\n", bits
, new, new, (1ULL << expr
->nrbits
)-1);
678 static void show_store_gen(int bits
, int value
, struct expression
*expr
, int addr
)
680 /* FIXME!!! Bitfield store! */
681 printf("\tst.%d\t\tv%d,[v%d]\n", bits
, value
, addr
);
684 static int show_assignment(struct expression
*expr
)
686 struct expression
*target
= expr
->left
;
692 bits
= expr
->ctype
->bit_size
;
693 val
= show_expression(expr
->right
);
694 addr
= show_address_gen(target
);
695 show_store_gen(bits
, val
, target
, addr
);
699 static int show_return_stmt(struct statement
*stmt
)
701 struct expression
*expr
= stmt
->ret_value
;
702 struct symbol
*target
= stmt
->ret_target
;
704 if (expr
&& expr
->ctype
) {
705 int val
= show_expression(expr
);
706 int bits
= expr
->ctype
->bit_size
;
707 int addr
= show_symbol_expr(target
);
708 show_store_gen(bits
, val
, NULL
, addr
);
710 printf("\tret\t\t(%p)\n", target
);
714 static int show_initialization(struct symbol
*sym
, struct expression
*expr
)
721 bits
= expr
->ctype
->bit_size
;
722 val
= show_expression(expr
);
723 addr
= show_symbol_expr(sym
);
724 // FIXME! The "target" expression is for bitfield store information.
725 // Leave it NULL, which works fine.
726 show_store_gen(bits
, val
, NULL
, addr
);
730 static int show_access(struct expression
*expr
)
732 int addr
= show_address_gen(expr
);
733 return show_load_gen(expr
->ctype
->bit_size
, expr
, addr
);
736 static int show_inc_dec(struct expression
*expr
, int postop
)
738 int addr
= show_address_gen(expr
->unop
);
740 const char *opname
= expr
->op
== SPECIAL_INCREMENT
? "add" : "sub";
741 int bits
= expr
->ctype
->bit_size
;
743 retval
= show_load_gen(bits
, expr
->unop
, addr
);
747 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname
, bits
, new, retval
);
748 show_store_gen(bits
, new, expr
->unop
, addr
);
752 static int show_preop(struct expression
*expr
)
755 * '*' is an lvalue access, and is fundamentally different
756 * from an arithmetic operation. Maybe it should have an
757 * expression type of its own..
760 return show_access(expr
);
761 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
762 return show_inc_dec(expr
, 0);
763 return show_regular_preop(expr
);
766 static int show_postop(struct expression
*expr
)
768 return show_inc_dec(expr
, 1);
771 static int show_symbol_expr(struct symbol
*sym
)
773 int new = new_pseudo();
775 if (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_EXTERN
| MOD_STATIC
)) {
776 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer
, new, show_ident(sym
->ident
));
779 if (sym
->ctype
.modifiers
& MOD_ADDRESSABLE
) {
780 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer
, new, sym
->value
);
783 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer
, new, show_ident(sym
->ident
), sym
);
787 static int show_symbol_init(struct symbol
*sym
)
789 struct expression
*expr
= sym
->initializer
;
794 bits
= expr
->ctype
->bit_size
;
795 val
= show_expression(expr
);
796 addr
= show_symbol_expr(sym
);
797 show_store_gen(bits
, val
, NULL
, addr
);
802 static int type_is_signed(struct symbol
*sym
)
804 if (sym
->type
== SYM_NODE
)
805 sym
= sym
->ctype
.base_type
;
806 if (sym
->type
== SYM_PTR
)
808 return !(sym
->ctype
.modifiers
& MOD_UNSIGNED
);
811 static int show_cast_expr(struct expression
*expr
)
813 struct symbol
*old_type
, *new_type
;
814 int op
= show_expression(expr
->cast_expression
);
815 int oldbits
, newbits
;
818 old_type
= expr
->cast_expression
->ctype
;
819 new_type
= expr
->cast_type
;
821 oldbits
= old_type
->bit_size
;
822 newbits
= new_type
->bit_size
;
823 if (oldbits
>= newbits
)
826 is_signed
= type_is_signed(old_type
);
828 printf("\tsext%d.%d\tv%d,v%d\n", oldbits
, newbits
, new, op
);
830 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits
, new, op
, (1UL << oldbits
)-1);
835 static int show_value(struct expression
*expr
)
837 int new = new_pseudo();
838 unsigned long long value
= expr
->value
;
840 printf("\tmovi.%d\t\tv%d,$%llu\n", expr
->ctype
->bit_size
, new, value
);
844 static int show_string_expr(struct expression
*expr
)
846 int new = new_pseudo();
848 printf("\tmovi.%d\t\tv%d,&%s\n", bits_in_pointer
, new, show_string(expr
->string
));
852 static int show_bitfield_expr(struct expression
*expr
)
854 return show_access(expr
);
857 int show_label_expr(struct expression
*expr
)
859 int new = new_pseudo();
860 printf("\tmovi.%d\t\tv%d,.L%p\n",bits_in_pointer
, new, expr
->label_symbol
);
864 static int show_conditional_expr(struct expression
*expr
)
866 int cond
= show_expression(expr
->conditional
);
867 int true = show_expression(expr
->cond_true
);
868 int false = show_expression(expr
->cond_false
);
869 int new = new_pseudo();
873 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond
, expr
->ctype
->bit_size
, new, true, false);
877 static int show_statement_expr(struct expression
*expr
)
879 return show_statement(expr
->statement
);
882 static int show_position_expr(struct expression
*expr
, struct symbol
*base
)
884 int new = show_expression(expr
->init_expr
);
885 struct symbol
*ctype
= expr
->init_sym
;
887 printf("\tinsert v%d at [%d:%d] of %s\n", new,
888 expr
->init_offset
, ctype
->bit_offset
,
889 show_ident(base
->ident
));
893 static int show_initializer_expr(struct expression
*expr
, struct symbol
*ctype
)
895 struct expression
*entry
;
897 FOR_EACH_PTR(expr
->expr_list
, entry
) {
898 // Nested initializers have their positions already
899 // recursively calculated - just output them too
900 if (entry
->type
== EXPR_INITIALIZER
) {
901 show_initializer_expr(entry
, ctype
);
905 // Ignore initializer indexes and identifiers - the
906 // evaluator has taken them into account
907 if (entry
->type
== EXPR_IDENTIFIER
|| entry
->type
== EXPR_INDEX
)
909 if (entry
->type
== EXPR_POS
) {
910 show_position_expr(entry
, ctype
);
913 show_initialization(ctype
, entry
);
918 int show_symbol_expr_init(struct symbol
*sym
)
920 struct expression
*expr
= sym
->initializer
;
923 show_expression(expr
);
924 return show_symbol_expr(sym
);
928 * Print out an expression. Return the pseudo that contains the
931 int show_expression(struct expression
*expr
)
937 struct position
*pos
= &expr
->pos
;
938 printf("\tno type at %s:%d:%d\n",
939 input_streams
[pos
->stream
].name
,
940 pos
->line
, pos
->pos
);
944 switch (expr
->type
) {
946 return show_call_expression(expr
);
948 case EXPR_ASSIGNMENT
:
949 return show_assignment(expr
);
955 return show_binop(expr
);
957 return show_preop(expr
);
959 return show_postop(expr
);
961 return show_symbol_expr(expr
->symbol
);
964 warn(expr
->pos
, "invalid expression after evaluation");
967 return show_cast_expr(expr
);
969 return show_value(expr
);
971 return show_string_expr(expr
);
973 return show_bitfield_expr(expr
);
974 case EXPR_INITIALIZER
:
975 return show_initializer_expr(expr
, expr
->ctype
);
976 case EXPR_CONDITIONAL
:
977 return show_conditional_expr(expr
);
979 return show_statement_expr(expr
);
981 return show_label_expr(expr
);
983 // None of these should exist as direct expressions: they are only
984 // valid as sub-expressions of initializers.
986 warn(expr
->pos
, "unable to show plain initializer position expression");
988 case EXPR_IDENTIFIER
:
989 warn(expr
->pos
, "unable to show identifier expression");
992 warn(expr
->pos
, "unable to show index expression");
995 warn(expr
->pos
, "unable to show type expression");