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]",
83 while ((res
= *ptr
++) != NULL
) {
86 while ((c
= *res
++) != '\0')
96 void show_struct_member(struct symbol
*sym
, void *data
, int flags
)
98 if (flags
& ITERATE_FIRST
)
100 printf("%s:%d:%ld at offset %ld", show_ident(sym
->ident
), sym
->bit_size
, sym
->ctype
.alignment
, sym
->offset
);
102 printf("[%d..%d]", sym
->bit_offset
, sym
->bit_offset
+sym
->fieldwidth
-1);
103 if (flags
& ITERATE_LAST
)
109 static void show_one_symbol(struct symbol
*sym
, void *sep
, int flags
)
112 if (!(flags
& ITERATE_LAST
))
113 printf("%s", (const char *)sep
);
116 void show_symbol_list(struct symbol_list
*list
, const char *sep
)
118 symbol_iterate(list
, show_one_symbol
, (void *)sep
);
126 static void prepend(struct type_name
*name
, const char *fmt
, ...)
128 static char buffer
[512];
133 n
= vsprintf(buffer
, fmt
, args
);
137 memcpy(name
->start
, buffer
, n
);
140 static void append(struct type_name
*name
, const char *fmt
, ...)
142 static char buffer
[512];
147 n
= vsprintf(buffer
, fmt
, args
);
150 memcpy(name
->end
, buffer
, n
);
154 static void do_show_type(struct symbol
*sym
, struct type_name
*name
)
158 static struct ctype_name
{
162 { & char_ctype
, "char" },
163 { &uchar_ctype
, "unsigned char" },
164 { & short_ctype
, "short" },
165 { &ushort_ctype
, "unsigned short" },
166 { & int_ctype
, "int" },
167 { &uint_ctype
, "unsigned int" },
168 { & long_ctype
, "long" },
169 { &ulong_ctype
, "unsigned long" },
170 { & llong_ctype
, "long long" },
171 { &ullong_ctype
, "unsigned long long" },
173 { &void_ctype
, "void" },
174 { &bool_ctype
, "bool" },
175 { &string_ctype
, "string" },
177 { &float_ctype
, "float" },
178 { &double_ctype
, "double" },
179 { &ldouble_ctype
,"long double" },
185 for (i
= 0; i
< sizeof(typenames
)/sizeof(typenames
[0]); i
++) {
186 if (typenames
[i
].sym
== sym
) {
187 int len
= strlen(typenames
[i
].name
);
188 *--name
->start
= ' ';
190 memcpy(name
->start
, typenames
[i
].name
, len
);
204 prepend(name
, "struct %s ", show_ident(sym
->ident
));
208 prepend(name
, "union %s ", show_ident(sym
->ident
));
212 prepend(name
, "enum %s ", show_ident(sym
->ident
));
216 append(name
, "%s", show_ident(sym
->ident
));
220 append(name
, ":%d", sym
->fieldwidth
);
224 append(name
, "label(%s:%p)", show_ident(sym
->ident
), sym
);
231 prepend(name
, "unknown type %d", sym
->type
);
235 mod
= modifier_string(sym
->ctype
.modifiers
);
236 modlen
= strlen(mod
);
237 name
->start
-= modlen
;
238 memcpy(name
->start
, mod
, modlen
);
240 do_show_type(sym
->ctype
.base_type
, name
);
244 append(name
, "<asn:%d>", sym
->ctype
.as
);
251 append(name
, " )( ... )");
255 append(name
, "[%lld]", get_expression_value(sym
->array_size
));
262 void show_type(struct symbol
*sym
)
265 struct type_name name
;
267 name
.start
= name
.end
= array
+100;
268 do_show_type(sym
, &name
);
270 printf("%s", name
.start
);
273 const char *show_typename(struct symbol
*sym
)
275 static char array
[200];
276 struct type_name name
;
278 name
.start
= name
.end
= array
+100;
279 do_show_type(sym
, &name
);
284 void show_symbol(struct symbol
*sym
)
291 if (sym
->ctype
.alignment
)
292 printf(".align %ld\n", sym
->ctype
.alignment
);
295 type
= sym
->ctype
.base_type
;
300 * Show actual implementation information
302 switch (type
->type
) {
304 symbol_iterate(type
->symbol_list
, show_struct_member
, NULL
);
308 symbol_iterate(type
->symbol_list
, show_struct_member
, NULL
);
312 struct statement
*stmt
= type
->stmt
;
316 val
= show_statement(stmt
);
318 printf("\tmov.%d\t\tretval,%d\n", stmt
->ret
->bit_size
, val
);
328 if (sym
->initializer
) {
330 show_expression(sym
->initializer
);
334 static int show_symbol_init(struct symbol
*sym
);
336 static int new_pseudo(void)
342 static int new_label(void)
344 static int label
= 0;
348 static void show_switch_statement(struct statement
*stmt
)
350 int val
= show_expression(stmt
->switch_expression
);
352 printf("\tswitch v%d\n", val
);
355 * Debugging only: Check that the case list is correct
356 * by printing it out.
358 * This is where a _real_ back-end would go through the
359 * cases to decide whether to use a lookup table or a
360 * series of comparisons etc
362 printf("# case table:\n");
363 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
364 struct statement
*case_stmt
= sym
->stmt
;
365 struct expression
*expr
= case_stmt
->case_expression
;
366 struct expression
*to
= case_stmt
->case_to
;
371 if (expr
->type
== EXPR_VALUE
) {
372 printf(" case %lld", expr
->value
);
374 if (to
->type
== EXPR_VALUE
) {
375 printf(" .. %lld", to
->value
);
383 printf(": .L%p\n", sym
->bb_target
);
385 printf("# end case table\n");
387 show_statement(stmt
->switch_statement
);
389 if (stmt
->switch_break
->used
)
390 printf(".L%p:\n", stmt
->switch_break
->bb_target
);
393 static void show_symbol_decl(struct symbol_list
*syms
)
396 FOR_EACH_PTR(syms
, sym
) {
397 show_symbol_init(sym
);
401 static int show_return_stmt(struct statement
*stmt
);
404 * Print out a statement
406 int show_statement(struct statement
*stmt
)
410 switch (stmt
->type
) {
412 return show_return_stmt(stmt
);
413 case STMT_COMPOUND
: {
417 show_symbol_decl(stmt
->syms
);
418 FOR_EACH_PTR(stmt
->stmts
, s
) {
419 last
= show_statement(s
);
423 printf(".L%p:\n", stmt
->ret
);
424 addr
= show_symbol_expr(stmt
->ret
);
425 bits
= stmt
->ret
->bit_size
;
427 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, last
, addr
);
432 case STMT_EXPRESSION
:
433 return show_expression(stmt
->expression
);
436 struct expression
*cond
= stmt
->if_conditional
;
438 /* This is only valid if nobody can jump into the "dead" statement */
440 if (cond
->type
== EXPR_VALUE
) {
441 struct statement
*s
= stmt
->if_true
;
448 val
= show_expression(cond
);
449 target
= new_label();
450 printf("\tje\t\tv%d,.L%d\n", val
, target
);
451 show_statement(stmt
->if_true
);
452 if (stmt
->if_false
) {
453 int last
= new_label();
454 printf("\tjmp\t\t.L%d\n", last
);
455 printf(".L%d:\n", target
);
457 show_statement(stmt
->if_false
);
459 printf(".L%d:\n", target
);
463 show_switch_statement(stmt
);
467 printf(".L%p:\n", stmt
->case_label
);
468 show_statement(stmt
->case_statement
);
471 case STMT_ITERATOR
: {
472 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
473 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
474 struct statement
*statement
= stmt
->iterator_statement
;
475 struct statement
*post_statement
= stmt
->iterator_post_statement
;
476 struct expression
*post_condition
= stmt
->iterator_post_condition
;
477 int val
, loop_top
= 0, loop_bottom
= 0;
479 show_symbol_decl(stmt
->iterator_syms
);
480 show_statement(pre_statement
);
482 if (pre_condition
->type
== EXPR_VALUE
) {
483 if (!pre_condition
->value
) {
484 loop_bottom
= new_label();
485 printf("\tjmp\t\t.L%d\n", loop_bottom
);
488 loop_bottom
= new_label();
489 val
= show_expression(pre_condition
);
490 printf("\tje\t\tv%d, .L%d\n", val
, loop_bottom
);
493 if (!post_condition
|| post_condition
->type
!= EXPR_VALUE
|| post_condition
->value
) {
494 loop_top
= new_label();
495 printf(".L%d:\n", loop_top
);
497 show_statement(statement
);
498 if (stmt
->iterator_continue
->used
)
499 printf(".L%p:\n", stmt
->iterator_continue
);
500 show_statement(post_statement
);
501 if (!post_condition
) {
502 printf("\tjmp\t\t.L%d\n", loop_top
);
503 } else if (post_condition
->type
== EXPR_VALUE
) {
504 if (post_condition
->value
)
505 printf("\tjmp\t\t.L%d\n", loop_top
);
507 val
= show_expression(post_condition
);
508 printf("\tjne\t\tv%d, .L%d\n", val
, loop_top
);
510 if (stmt
->iterator_break
->used
)
511 printf(".L%p:\n", stmt
->iterator_break
);
513 printf(".L%d:\n", loop_bottom
);
520 printf(".L%p:\n", stmt
->label_identifier
);
521 show_statement(stmt
->label_statement
);
525 if (stmt
->goto_expression
) {
526 int val
= show_expression(stmt
->goto_expression
);
527 printf("\tgoto\t\t*v%d\n", val
);
529 printf("\tgoto\t\t.L%p\n", stmt
->goto_label
->bb_target
);
533 printf("\tasm( .... )\n");
540 static void show_one_statement(struct statement
*stmt
, void *sep
, int flags
)
542 show_statement(stmt
);
543 if (!(flags
& ITERATE_LAST
))
544 printf("%s", (const char *)sep
);
547 void show_statement_list(struct statement_list
*stmt
, const char *sep
)
549 statement_iterate(stmt
, show_one_statement
, (void *)sep
);
552 static void show_one_expression(struct expression
*expr
, void *sep
, int flags
)
554 show_expression(expr
);
555 if (!(flags
& ITERATE_LAST
))
556 printf("%s", (const char *)sep
);
559 void show_expression_list(struct expression_list
*list
, const char *sep
)
561 expression_iterate(list
, show_one_expression
, (void *)sep
);
564 static int show_call_expression(struct expression
*expr
)
566 struct symbol
*direct
;
567 struct expression
*arg
, *fn
;
572 warn(expr
->pos
, "\tcall with no type!");
577 FOR_EACH_PTR_REVERSE(expr
->args
, arg
) {
578 int new = show_expression(arg
);
579 int size
= arg
->ctype
->bit_size
;
580 printf("\tpush.%d\t\tv%d\n", size
, new);
581 framesize
+= size
>> 3;
582 } END_FOR_EACH_PTR_REVERSE
;
586 /* Remove dereference, if any */
588 if (fn
->type
== EXPR_PREOP
) {
589 if (fn
->unop
->type
== EXPR_SYMBOL
) {
590 struct symbol
*sym
= fn
->unop
->symbol
;
591 if (sym
->ctype
.base_type
->type
== SYM_FN
)
596 printf("\tcall\t\t%s\n", show_ident(direct
->ident
));
598 fncall
= show_expression(fn
);
599 printf("\tcall\t\t*v%d\n", fncall
);
602 printf("\tadd.%d\t\tvSP,vSP,$%d\n", BITS_IN_POINTER
, framesize
);
604 retval
= new_pseudo();
605 printf("\tmov.%d\t\tv%d,retval\n", expr
->ctype
->bit_size
, retval
);
609 static int show_binop(struct expression
*expr
)
611 int left
= show_expression(expr
->left
);
612 int right
= show_expression(expr
->right
);
613 int new = new_pseudo();
615 static const char *name
[] = {
616 ['+'] = "add", ['-'] = "sub",
617 ['*'] = "mul", ['/'] = "div",
618 ['%'] = "mod", ['&'] = "and",
619 ['|'] = "lor", ['^'] = "xor"
621 unsigned int op
= expr
->op
;
623 opname
= show_special(op
);
624 if (op
< sizeof(name
)/sizeof(*name
))
626 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname
,
627 expr
->ctype
->bit_size
,
632 static int show_regular_preop(struct expression
*expr
)
634 int target
= show_expression(expr
->unop
);
635 int new = new_pseudo();
636 static const char *name
[] = {
637 ['!'] = "nonzero", ['-'] = "neg",
640 unsigned int op
= expr
->op
;
643 opname
= show_special(op
);
644 if (op
< sizeof(name
)/sizeof(*name
))
646 printf("\t%s.%d\t\tv%d,v%d\n", opname
, expr
->ctype
->bit_size
, new, target
);
651 * FIXME! Not all accesses are memory loads. We should
652 * check what kind of symbol is behind the dereference.
654 static int show_address_gen(struct expression
*expr
)
656 if (expr
->type
== EXPR_PREOP
)
657 return show_expression(expr
->unop
);
658 return show_expression(expr
->address
);
661 static int show_load_gen(int bits
, struct expression
*expr
, int addr
)
663 int new = new_pseudo();
665 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, new, addr
);
666 if (expr
->type
== EXPR_PREOP
)
671 printf("\tshr.%d\t\tv%d,v%d,$%d\n", bits
, new, new, expr
->bitpos
);
672 printf("\tandi.%d\t\tv%d,v%d,$%llu\n", bits
, new, new, (1ULL << expr
->nrbits
)-1);
676 static void show_store_gen(int bits
, int value
, struct expression
*expr
, int addr
)
678 /* FIXME!!! Bitfield store! */
679 printf("\tst.%d\t\tv%d,[v%d]\n", bits
, value
, addr
);
682 static int show_assignment(struct expression
*expr
)
684 struct expression
*target
= expr
->left
;
690 bits
= expr
->ctype
->bit_size
;
691 val
= show_expression(expr
->right
);
692 addr
= show_address_gen(target
);
693 show_store_gen(bits
, val
, target
, addr
);
697 static int show_return_stmt(struct statement
*stmt
)
699 struct expression
*expr
= stmt
->ret_value
;
700 struct symbol
*target
= stmt
->ret_target
;
702 if (expr
&& expr
->ctype
) {
703 int val
= show_expression(expr
);
704 int bits
= expr
->ctype
->bit_size
;
705 int addr
= show_symbol_expr(target
);
706 show_store_gen(bits
, val
, NULL
, addr
);
708 printf("\tret\t\t(%p)\n", target
);
712 static int show_initialization(struct symbol
*sym
, struct expression
*expr
)
719 bits
= expr
->ctype
->bit_size
;
720 val
= show_expression(expr
);
721 addr
= show_symbol_expr(sym
);
722 // FIXME! The "target" expression is for bitfield store information.
723 // Leave it NULL, which works fine.
724 show_store_gen(bits
, val
, NULL
, addr
);
728 static int show_access(struct expression
*expr
)
730 int addr
= show_address_gen(expr
);
731 return show_load_gen(expr
->ctype
->bit_size
, expr
, addr
);
734 static int show_inc_dec(struct expression
*expr
, int postop
)
736 int addr
= show_address_gen(expr
->unop
);
738 const char *opname
= expr
->op
== SPECIAL_INCREMENT
? "add" : "sub";
739 int bits
= expr
->ctype
->bit_size
;
741 retval
= show_load_gen(bits
, expr
->unop
, addr
);
745 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname
, bits
, new, retval
);
746 show_store_gen(bits
, new, expr
->unop
, addr
);
750 static int show_preop(struct expression
*expr
)
753 * '*' is an lvalue access, and is fundamentally different
754 * from an arithmetic operation. Maybe it should have an
755 * expression type of its own..
758 return show_access(expr
);
759 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
760 return show_inc_dec(expr
, 0);
761 return show_regular_preop(expr
);
764 static int show_postop(struct expression
*expr
)
766 return show_inc_dec(expr
, 1);
769 static int show_symbol_expr(struct symbol
*sym
)
771 int new = new_pseudo();
773 if (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_EXTERN
| MOD_STATIC
)) {
774 printf("\tmovi.%d\t\tv%d,$%s\n", BITS_IN_POINTER
, new, show_ident(sym
->ident
));
777 if (sym
->ctype
.modifiers
& MOD_ADDRESSABLE
) {
778 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", BITS_IN_POINTER
, new, sym
->value
);
781 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", BITS_IN_POINTER
, new, show_ident(sym
->ident
), sym
);
785 static int show_symbol_init(struct symbol
*sym
)
787 struct expression
*expr
= sym
->initializer
;
792 bits
= expr
->ctype
->bit_size
;
793 val
= show_expression(expr
);
794 addr
= show_symbol_expr(sym
);
795 show_store_gen(bits
, val
, NULL
, addr
);
800 static int type_is_signed(struct symbol
*sym
)
802 if (sym
->type
== SYM_NODE
)
803 sym
= sym
->ctype
.base_type
;
804 if (sym
->type
== SYM_PTR
)
806 return !(sym
->ctype
.modifiers
& MOD_UNSIGNED
);
809 static int show_cast_expr(struct expression
*expr
)
811 struct symbol
*old_type
, *new_type
;
812 int op
= show_expression(expr
->cast_expression
);
813 int oldbits
, newbits
;
816 old_type
= expr
->cast_expression
->ctype
;
817 new_type
= expr
->cast_type
;
819 oldbits
= old_type
->bit_size
;
820 newbits
= new_type
->bit_size
;
821 if (oldbits
>= newbits
)
824 is_signed
= type_is_signed(old_type
);
826 printf("\tsext%d.%d\tv%d,v%d\n", oldbits
, newbits
, new, op
);
828 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits
, new, op
, (1UL << oldbits
)-1);
833 static int show_value(struct expression
*expr
)
835 int new = new_pseudo();
836 unsigned long long value
= expr
->value
;
838 printf("\tmovi.%d\t\tv%d,$%llu\n", expr
->ctype
->bit_size
, new, value
);
842 static int show_string_expr(struct expression
*expr
)
844 int new = new_pseudo();
846 printf("\tmovi.%d\t\tv%d,&%s\n", BITS_IN_POINTER
, new, show_string(expr
->string
));
850 static int show_bitfield_expr(struct expression
*expr
)
852 return show_access(expr
);
855 int show_label_expr(struct expression
*expr
)
857 int new = new_pseudo();
858 printf("\tmovi.%d\t\tv%d,.L%p\n",BITS_IN_POINTER
, new, expr
->label_symbol
);
862 static int show_conditional_expr(struct expression
*expr
)
864 int cond
= show_expression(expr
->conditional
);
865 int true = show_expression(expr
->cond_true
);
866 int false = show_expression(expr
->cond_false
);
867 int new = new_pseudo();
871 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond
, expr
->ctype
->bit_size
, new, true, false);
875 static int show_statement_expr(struct expression
*expr
)
877 return show_statement(expr
->statement
);
880 static int show_position_expr(struct expression
*expr
, struct symbol
*base
)
882 int new = show_expression(expr
->init_expr
);
883 struct symbol
*ctype
= expr
->init_sym
;
885 printf("\tinsert v%d at [%d:%d] of %s\n", new,
886 expr
->init_offset
, ctype
->bit_offset
,
887 show_ident(base
->ident
));
891 static int show_initializer_expr(struct expression
*expr
, struct symbol
*ctype
)
893 struct expression
*entry
;
895 FOR_EACH_PTR(expr
->expr_list
, entry
) {
896 // Nested initializers have their positions already
897 // recursively calculated - just output them too
898 if (entry
->type
== EXPR_INITIALIZER
) {
899 show_initializer_expr(entry
, ctype
);
903 // Ignore initializer indexes and identifiers - the
904 // evaluator has taken them into account
905 if (entry
->type
== EXPR_IDENTIFIER
|| entry
->type
== EXPR_INDEX
)
907 if (entry
->type
== EXPR_POS
) {
908 show_position_expr(entry
, ctype
);
911 show_initialization(ctype
, entry
);
916 int show_symbol_expr_init(struct symbol
*sym
)
918 struct expression
*expr
= sym
->initializer
;
921 show_expression(expr
);
922 return show_symbol_expr(sym
);
926 * Print out an expression. Return the pseudo that contains the
929 int show_expression(struct expression
*expr
)
935 struct position
*pos
= &expr
->pos
;
936 printf("\tno type at %s:%d:%d\n",
937 input_streams
[pos
->stream
].name
,
938 pos
->line
, pos
->pos
);
942 switch (expr
->type
) {
944 return show_call_expression(expr
);
946 case EXPR_ASSIGNMENT
:
947 return show_assignment(expr
);
953 return show_binop(expr
);
955 return show_preop(expr
);
957 return show_postop(expr
);
959 return show_symbol_expr(expr
->symbol
);
962 warn(expr
->pos
, "invalid expression after evaluation");
965 return show_cast_expr(expr
);
967 return show_value(expr
);
969 return show_string_expr(expr
);
971 return show_bitfield_expr(expr
);
972 case EXPR_INITIALIZER
:
973 return show_initializer_expr(expr
, expr
->ctype
);
974 case EXPR_CONDITIONAL
:
975 return show_conditional_expr(expr
);
977 return show_statement_expr(expr
);
979 return show_label_expr(expr
);
981 // None of these should exist as direct expressions: they are only
982 // valid as sub-expressions of initializers.
984 warn(expr
->pos
, "unable to show plain initializer position expression");
986 case EXPR_IDENTIFIER
:
987 warn(expr
->pos
, "unable to show identifier expression");
990 warn(expr
->pos
, "unable to show index expression");
993 warn(expr
->pos
, "unable to show type expression");