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 *v%d\n", val
);
529 printf("\tgoto .L%p\n", stmt
->goto_label
->bb_target
);
533 printf("\tasm( .... )\n");
537 case STMT_CONDFALSE
: {
538 int val
= show_expression(stmt
->bb_conditional
);
539 printf("\t%s v%d,.L%p\n",
540 stmt
->type
== STMT_CONDTRUE
? "jnz" : "jz",
541 val
, stmt
->bb_target
->bb_target
);
549 static void show_one_statement(struct statement
*stmt
, void *sep
, int flags
)
551 show_statement(stmt
);
552 if (!(flags
& ITERATE_LAST
))
553 printf("%s", (const char *)sep
);
556 void show_statement_list(struct statement_list
*stmt
, const char *sep
)
558 statement_iterate(stmt
, show_one_statement
, (void *)sep
);
561 static void show_one_expression(struct expression
*expr
, void *sep
, int flags
)
563 show_expression(expr
);
564 if (!(flags
& ITERATE_LAST
))
565 printf("%s", (const char *)sep
);
568 void show_expression_list(struct expression_list
*list
, const char *sep
)
570 expression_iterate(list
, show_one_expression
, (void *)sep
);
573 static int show_call_expression(struct expression
*expr
)
575 struct symbol
*direct
;
576 struct expression
*arg
, *fn
;
581 warn(expr
->pos
, "\tcall with no type!");
586 FOR_EACH_PTR_REVERSE(expr
->args
, arg
) {
587 int new = show_expression(arg
);
588 int size
= arg
->ctype
->bit_size
;
589 printf("\tpush.%d\t\tv%d\n", size
, new);
590 framesize
+= size
>> 3;
591 } END_FOR_EACH_PTR_REVERSE
;
595 /* Remove dereference, if any */
597 if (fn
->type
== EXPR_PREOP
) {
598 if (fn
->unop
->type
== EXPR_SYMBOL
) {
599 struct symbol
*sym
= fn
->unop
->symbol
;
600 if (sym
->ctype
.base_type
->type
== SYM_FN
)
605 printf("\tcall\t\t%s\n", show_ident(direct
->ident
));
607 fncall
= show_expression(fn
);
608 printf("\tcall\t\t*v%d\n", fncall
);
611 printf("\tadd.%d\t\tvSP,vSP,$%d\n", BITS_IN_POINTER
, framesize
);
613 retval
= new_pseudo();
614 printf("\tmov.%d\t\tv%d,retval\n", expr
->ctype
->bit_size
, retval
);
618 static int show_binop(struct expression
*expr
)
620 int left
= show_expression(expr
->left
);
621 int right
= show_expression(expr
->right
);
622 int new = new_pseudo();
624 static const char *name
[] = {
625 ['+'] = "add", ['-'] = "sub",
626 ['*'] = "mul", ['/'] = "div",
627 ['%'] = "mod", ['&'] = "and",
628 ['|'] = "lor", ['^'] = "xor"
630 unsigned int op
= expr
->op
;
632 opname
= show_special(op
);
633 if (op
< sizeof(name
)/sizeof(*name
))
635 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname
,
636 expr
->ctype
->bit_size
,
641 static int show_regular_preop(struct expression
*expr
)
643 int target
= show_expression(expr
->unop
);
644 int new = new_pseudo();
645 static const char *name
[] = {
646 ['!'] = "nonzero", ['-'] = "neg",
649 unsigned int op
= expr
->op
;
652 opname
= show_special(op
);
653 if (op
< sizeof(name
)/sizeof(*name
))
655 printf("\t%s.%d\t\tv%d,v%d\n", opname
, expr
->ctype
->bit_size
, new, target
);
660 * FIXME! Not all accesses are memory loads. We should
661 * check what kind of symbol is behind the dereference.
663 static int show_address_gen(struct expression
*expr
)
665 if (expr
->type
== EXPR_PREOP
)
666 return show_expression(expr
->unop
);
667 return show_expression(expr
->address
);
670 static int show_load_gen(int bits
, struct expression
*expr
, int addr
)
672 int new = new_pseudo();
674 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, new, addr
);
675 if (expr
->type
== EXPR_PREOP
)
680 printf("\tshr.%d\t\tv%d,v%d,$%d\n", bits
, new, new, expr
->bitpos
);
681 printf("\tandi.%d\t\tv%d,v%d,$%llu\n", bits
, new, new, (1ULL << expr
->nrbits
)-1);
685 static void show_store_gen(int bits
, int value
, struct expression
*expr
, int addr
)
687 /* FIXME!!! Bitfield store! */
688 printf("\tst.%d\t\tv%d,[v%d]\n", bits
, value
, addr
);
691 static int show_assignment(struct expression
*expr
)
693 struct expression
*target
= expr
->left
;
699 bits
= expr
->ctype
->bit_size
;
700 val
= show_expression(expr
->right
);
701 addr
= show_address_gen(target
);
702 show_store_gen(bits
, val
, target
, addr
);
706 static int show_return_stmt(struct statement
*stmt
)
708 struct expression
*expr
= stmt
->ret_value
;
709 struct symbol
*target
= stmt
->ret_target
;
711 if (expr
&& expr
->ctype
) {
712 int val
= show_expression(expr
);
713 int bits
= expr
->ctype
->bit_size
;
714 int addr
= show_symbol_expr(target
);
715 show_store_gen(bits
, val
, NULL
, addr
);
717 printf("\tgoto .L%p\n", target
);
721 static int show_initialization(struct symbol
*sym
, struct expression
*expr
)
728 bits
= expr
->ctype
->bit_size
;
729 val
= show_expression(expr
);
730 addr
= show_symbol_expr(sym
);
731 // FIXME! The "target" expression is for bitfield store information.
732 // Leave it NULL, which works fine.
733 show_store_gen(bits
, val
, NULL
, addr
);
737 static int show_access(struct expression
*expr
)
739 int addr
= show_address_gen(expr
);
740 return show_load_gen(expr
->ctype
->bit_size
, expr
, addr
);
743 static int show_inc_dec(struct expression
*expr
, int postop
)
745 int addr
= show_address_gen(expr
->unop
);
747 const char *opname
= expr
->op
== SPECIAL_INCREMENT
? "add" : "sub";
748 int bits
= expr
->ctype
->bit_size
;
750 retval
= show_load_gen(bits
, expr
->unop
, addr
);
754 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname
, bits
, new, retval
);
755 show_store_gen(bits
, new, expr
->unop
, addr
);
759 static int show_preop(struct expression
*expr
)
762 * '*' is an lvalue access, and is fundamentally different
763 * from an arithmetic operation. Maybe it should have an
764 * expression type of its own..
767 return show_access(expr
);
768 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
769 return show_inc_dec(expr
, 0);
770 return show_regular_preop(expr
);
773 static int show_postop(struct expression
*expr
)
775 return show_inc_dec(expr
, 1);
778 static int show_symbol_expr(struct symbol
*sym
)
780 int new = new_pseudo();
782 if (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_EXTERN
| MOD_STATIC
)) {
783 printf("\tmovi.%d\t\tv%d,$%s\n", BITS_IN_POINTER
, new, show_ident(sym
->ident
));
786 if (sym
->ctype
.modifiers
& MOD_ADDRESSABLE
) {
787 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", BITS_IN_POINTER
, new, sym
->value
);
790 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", BITS_IN_POINTER
, new, show_ident(sym
->ident
), sym
);
794 static int show_symbol_init(struct symbol
*sym
)
796 struct expression
*expr
= sym
->initializer
;
801 bits
= expr
->ctype
->bit_size
;
802 val
= show_expression(expr
);
803 addr
= show_symbol_expr(sym
);
804 show_store_gen(bits
, val
, NULL
, addr
);
809 static int type_is_signed(struct symbol
*sym
)
811 if (sym
->type
== SYM_NODE
)
812 sym
= sym
->ctype
.base_type
;
813 if (sym
->type
== SYM_PTR
)
815 return !(sym
->ctype
.modifiers
& MOD_UNSIGNED
);
818 static int show_cast_expr(struct expression
*expr
)
820 struct symbol
*old_type
, *new_type
;
821 int op
= show_expression(expr
->cast_expression
);
822 int oldbits
, newbits
;
825 old_type
= expr
->cast_expression
->ctype
;
826 new_type
= expr
->cast_type
;
828 oldbits
= old_type
->bit_size
;
829 newbits
= new_type
->bit_size
;
830 if (oldbits
>= newbits
)
833 is_signed
= type_is_signed(old_type
);
835 printf("\tsext%d.%d\tv%d,v%d\n", oldbits
, newbits
, new, op
);
837 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits
, new, op
, (1UL << oldbits
)-1);
842 static int show_value(struct expression
*expr
)
844 int new = new_pseudo();
845 unsigned long long value
= expr
->value
;
847 printf("\tmovi.%d\t\tv%d,$%llu\n", expr
->ctype
->bit_size
, new, value
);
851 static int show_string_expr(struct expression
*expr
)
853 int new = new_pseudo();
855 printf("\tmovi.%d\t\tv%d,&%s\n", BITS_IN_POINTER
, new, show_string(expr
->string
));
859 static int show_bitfield_expr(struct expression
*expr
)
861 return show_access(expr
);
864 int show_label_expr(struct expression
*expr
)
866 int new = new_pseudo();
867 printf("\tmovi.%d\t\tv%d,.L%p\n",BITS_IN_POINTER
, new, expr
->label_symbol
);
871 static int show_conditional_expr(struct expression
*expr
)
873 int cond
= show_expression(expr
->conditional
);
874 int true = show_expression(expr
->cond_true
);
875 int false = show_expression(expr
->cond_false
);
876 int new = new_pseudo();
880 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond
, expr
->ctype
->bit_size
, new, true, false);
884 static int show_statement_expr(struct expression
*expr
)
886 return show_statement(expr
->statement
);
889 static int show_position_expr(struct expression
*expr
, struct symbol
*base
)
891 int new = show_expression(expr
->init_expr
);
892 struct symbol
*ctype
= expr
->init_sym
;
894 printf("\tinsert v%d at [%d:%d] of %s\n", new,
895 expr
->init_offset
, ctype
->bit_offset
,
896 show_ident(base
->ident
));
900 static int show_initializer_expr(struct expression
*expr
, struct symbol
*ctype
)
902 struct expression
*entry
;
904 FOR_EACH_PTR(expr
->expr_list
, entry
) {
905 // Nested initializers have their positions already
906 // recursively calculated - just output them too
907 if (entry
->type
== EXPR_INITIALIZER
) {
908 show_initializer_expr(entry
, ctype
);
912 // Ignore initializer indexes and identifiers - the
913 // evaluator has taken them into account
914 if (entry
->type
== EXPR_IDENTIFIER
|| entry
->type
== EXPR_INDEX
)
916 if (entry
->type
== EXPR_POS
) {
917 show_position_expr(entry
, ctype
);
920 show_initialization(ctype
, entry
);
925 int show_symbol_expr_init(struct symbol
*sym
)
927 struct expression
*expr
= sym
->initializer
;
930 show_initializer_expr(expr
, expr
->ctype
);
931 return show_symbol_expr(sym
);
935 * Print out an expression. Return the pseudo that contains the
938 int show_expression(struct expression
*expr
)
944 struct position
*pos
= &expr
->pos
;
945 printf("\tno type at %s:%d:%d\n",
946 input_streams
[pos
->stream
].name
,
947 pos
->line
, pos
->pos
);
951 switch (expr
->type
) {
953 return show_call_expression(expr
);
955 case EXPR_ASSIGNMENT
:
956 return show_assignment(expr
);
962 return show_binop(expr
);
964 return show_preop(expr
);
966 return show_postop(expr
);
968 return show_symbol_expr_init(expr
->symbol
);
971 warn(expr
->pos
, "invalid expression after evaluation");
974 return show_cast_expr(expr
);
976 return show_value(expr
);
978 return show_string_expr(expr
);
980 return show_bitfield_expr(expr
);
981 case EXPR_INITIALIZER
:
982 return show_initializer_expr(expr
, expr
->ctype
);
983 case EXPR_CONDITIONAL
:
984 return show_conditional_expr(expr
);
986 return show_statement_expr(expr
);
988 return show_label_expr(expr
);
990 // None of these should exist as direct expressions: they are only
991 // valid as sub-expressions of initializers.
993 warn(expr
->pos
, "unable to show plain initializer position expression");
995 case EXPR_IDENTIFIER
:
996 warn(expr
->pos
, "unable to show identifier expression");
999 warn(expr
->pos
, "unable to show index expression");