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 void do_debug_symbol(struct symbol
*sym
, int indent
)
28 static const char indent_string
[] = " ";
29 static const char *typestr
[] = {
30 "base", "node", "ptr.", "fn..",
31 "arry", "strt", "unin", "enum",
32 "tdef", "tpof", "memb", "bitf",
38 fprintf(stderr
, "%.*s%s%3d:%lu %lx %s (as: %d, context: %x:%x) %p (%s:%d:%d)\n",
39 indent
, indent_string
, typestr
[sym
->type
],
40 sym
->bit_size
, sym
->ctype
.alignment
,
41 sym
->ctype
.modifiers
, show_ident(sym
->ident
),
42 sym
->ctype
.as
, sym
->ctype
.context
, sym
->ctype
.contextmask
,
43 sym
, input_streams
[sym
->pos
.stream
].name
, sym
->pos
.line
, sym
->pos
.pos
);
44 if (sym
->type
== SYM_FN
) {
47 FOR_EACH_PTR(sym
->arguments
, arg
) {
48 fprintf(stderr
, "< arg%d:\n", i
);
49 do_debug_symbol(arg
, 0);
50 fprintf(stderr
, " end arg%d >\n", i
);
54 do_debug_symbol(sym
->ctype
.base_type
, indent
+2);
57 void debug_symbol(struct symbol
*sym
)
59 do_debug_symbol(sym
, 0);
63 * Symbol type printout. The type system is by far the most
64 * complicated part of C - everything else is trivial.
66 const char *modifier_string(unsigned long mod
)
68 static char buffer
[100];
70 const char *res
,**ptr
, *names
[] = {
71 "auto", "register", "static", "extern",
72 "const", "volatile", "[signed]", "[unsigned]",
73 "[char]", "[short]", "[long]", "[long]",
74 "[typdef]", "[structof]", "[unionof]", "[enum]",
75 "[typeof]", "[attribute]", "inline", "[addressable]",
76 "[nocast]", "[noderef]",
80 while ((res
= *ptr
++) != NULL
) {
83 while ((c
= *res
++) != '\0')
93 void show_struct_member(struct symbol
*sym
, void *data
, int flags
)
95 if (flags
& ITERATE_FIRST
)
97 printf("%s:%d:%ld at offset %ld", show_ident(sym
->ident
), sym
->bit_size
, sym
->ctype
.alignment
, sym
->offset
);
99 printf("[%d..%d]", sym
->bit_offset
, sym
->bit_offset
+sym
->fieldwidth
-1);
100 if (flags
& ITERATE_LAST
)
106 static void show_one_symbol(struct symbol
*sym
, void *sep
, int flags
)
109 if (!(flags
& ITERATE_LAST
))
110 printf("%s", (const char *)sep
);
113 void show_symbol_list(struct symbol_list
*list
, const char *sep
)
115 symbol_iterate(list
, show_one_symbol
, (void *)sep
);
123 static void prepend(struct type_name
*name
, const char *fmt
, ...)
125 static char buffer
[512];
130 n
= vsprintf(buffer
, fmt
, args
);
134 memcpy(name
->start
, buffer
, n
);
137 static void append(struct type_name
*name
, const char *fmt
, ...)
139 static char buffer
[512];
144 n
= vsprintf(buffer
, fmt
, args
);
147 memcpy(name
->end
, buffer
, n
);
151 static void do_show_type(struct symbol
*sym
, struct type_name
*name
)
155 static struct ctype_name
{
159 { & char_ctype
, "char" },
160 { &uchar_ctype
, "unsigned char" },
161 { & short_ctype
, "short" },
162 { &ushort_ctype
, "unsigned short" },
163 { & int_ctype
, "int" },
164 { &uint_ctype
, "unsigned int" },
165 { & long_ctype
, "long" },
166 { &ulong_ctype
, "unsigned long" },
167 { & llong_ctype
, "long long" },
168 { &ullong_ctype
, "unsigned long long" },
170 { &void_ctype
, "void" },
171 { &bool_ctype
, "bool" },
172 { &string_ctype
, "string" },
174 { &float_ctype
, "float" },
175 { &double_ctype
, "double" },
176 { &ldouble_ctype
,"long double" },
182 for (i
= 0; i
< sizeof(typenames
)/sizeof(typenames
[0]); i
++) {
183 if (typenames
[i
].sym
== sym
) {
184 int len
= strlen(typenames
[i
].name
);
185 *--name
->start
= ' ';
187 memcpy(name
->start
, typenames
[i
].name
, len
);
201 prepend(name
, "struct %s ", show_ident(sym
->ident
));
205 prepend(name
, "union %s ", show_ident(sym
->ident
));
209 prepend(name
, "enum %s ", show_ident(sym
->ident
));
213 append(name
, "%s", show_ident(sym
->ident
));
217 append(name
, ":%d", sym
->fieldwidth
);
221 append(name
, "label(%s:%p)", show_ident(sym
->ident
), sym
);
228 prepend(name
, "unknown type %d", sym
->type
);
232 mod
= modifier_string(sym
->ctype
.modifiers
);
233 modlen
= strlen(mod
);
234 name
->start
-= modlen
;
235 memcpy(name
->start
, mod
, modlen
);
237 do_show_type(sym
->ctype
.base_type
, name
);
241 append(name
, "<asn:%d>", sym
->ctype
.as
);
248 append(name
, " )( ... )");
252 append(name
, "[%d]", sym
->array_size
);
259 void show_type(struct symbol
*sym
)
262 struct type_name name
;
264 name
.start
= name
.end
= array
+100;
265 do_show_type(sym
, &name
);
267 printf("%s", name
.start
);
270 const char *show_typename(struct symbol
*sym
)
272 static char array
[200];
273 struct type_name name
;
275 name
.start
= name
.end
= array
+100;
276 do_show_type(sym
, &name
);
281 void show_symbol(struct symbol
*sym
)
288 if (sym
->ctype
.alignment
)
289 printf(".align %ld\n", sym
->ctype
.alignment
);
292 type
= sym
->ctype
.base_type
;
297 * Show actual implementation information
299 switch (type
->type
) {
301 symbol_iterate(type
->symbol_list
, show_struct_member
, NULL
);
305 symbol_iterate(type
->symbol_list
, show_struct_member
, NULL
);
310 show_statement(type
->stmt
);
318 if (sym
->initializer
) {
320 show_expression(sym
->initializer
);
324 static int show_return_stmt(struct statement
*stmt
)
326 struct expression
*expr
= stmt
->ret_value
;
327 struct symbol
*target
= stmt
->ret_target
;
329 if (expr
&& expr
->ctype
) {
330 int val
= show_expression(expr
);
331 printf("\tmov.%d\t\tretval,v%d\n",
332 expr
->ctype
->bit_size
, val
);
334 printf("\tgoto .L%p\n", target
);
338 static int show_symbol_init(struct symbol
*sym
);
340 static int new_label(void)
342 static int label
= 0;
346 static void show_switch_statement(struct statement
*stmt
)
348 int val
= show_expression(stmt
->switch_expression
);
350 printf("\tswitch v%d\n", val
);
353 * Debugging only: Check that the case list is correct
354 * by printing it out.
356 * This is where a _real_ back-end would go through the
357 * cases to decide whether to use a lookup table or a
358 * series of comparisons etc
360 printf("# case table:\n");
361 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
362 struct statement
*case_stmt
= sym
->stmt
;
363 struct expression
*expr
= case_stmt
->case_expression
;
364 struct expression
*to
= case_stmt
->case_to
;
369 if (expr
->type
== EXPR_VALUE
) {
370 printf(" case %lld", expr
->value
);
372 if (to
->type
== EXPR_VALUE
) {
373 printf(" .. %lld", to
->value
);
381 printf(": .L%p\n", sym
);
383 printf("# end case table\n");
385 show_statement(stmt
->switch_statement
);
387 if (stmt
->switch_break
->used
)
388 printf(".L%p:\n", stmt
->switch_break
);
391 static void show_symbol_decl(struct symbol_list
*syms
)
394 FOR_EACH_PTR(syms
, sym
) {
395 show_symbol_init(sym
);
400 * Print out a statement
402 int show_statement(struct statement
*stmt
)
406 switch (stmt
->type
) {
408 return show_return_stmt(stmt
);
409 case STMT_COMPOUND
: {
413 show_symbol_decl(stmt
->syms
);
414 FOR_EACH_PTR(stmt
->stmts
, s
) {
415 last
= show_statement(s
);
418 printf(".L%p:\n", stmt
->ret
);
422 case STMT_EXPRESSION
:
423 return show_expression(stmt
->expression
);
426 struct expression
*cond
= stmt
->if_conditional
;
428 if (cond
->type
== EXPR_VALUE
) {
429 struct statement
*s
= stmt
->if_true
;
435 val
= show_expression(cond
);
436 target
= new_label();
437 printf("\tje\t\tv%d,.L%d\n", val
, target
);
438 show_statement(stmt
->if_true
);
439 if (stmt
->if_false
) {
440 int last
= new_label();
441 printf("\tjmp\t\t.L%d\n", last
);
442 printf(".L%d:\n", target
);
444 show_statement(stmt
->if_false
);
446 printf(".L%d:\n", target
);
450 show_switch_statement(stmt
);
454 printf(".L%p:\n", stmt
->case_label
);
455 show_statement(stmt
->case_statement
);
458 case STMT_ITERATOR
: {
459 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
460 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
461 struct statement
*statement
= stmt
->iterator_statement
;
462 struct statement
*post_statement
= stmt
->iterator_post_statement
;
463 struct expression
*post_condition
= stmt
->iterator_post_condition
;
464 int val
, loop_top
= 0, loop_bottom
= 0;
466 show_symbol_decl(stmt
->iterator_syms
);
467 show_statement(pre_statement
);
469 if (pre_condition
->type
== EXPR_VALUE
) {
470 if (!pre_condition
->value
)
472 pre_condition
= NULL
;
474 loop_bottom
= new_label();
475 val
= show_expression(pre_condition
);
476 printf("\tje\t\tv%d, .L%d\n", val
, loop_bottom
);
479 if (!post_condition
|| post_condition
->type
!= EXPR_VALUE
|| post_condition
->value
) {
480 loop_top
= new_label();
481 printf(".L%d:\n", loop_top
);
483 show_statement(statement
);
484 if (stmt
->iterator_continue
->used
)
485 printf(".L%p:\n", stmt
->iterator_continue
);
486 show_statement(post_statement
);
487 if (!post_condition
) {
488 printf("\tjmp\t\t.L%d\n", loop_top
);
489 } else if (post_condition
->type
== EXPR_VALUE
) {
490 if (post_condition
->value
)
491 printf("\tjmp\t\t.L%d\n", loop_top
);
493 val
= show_expression(post_condition
);
494 printf("\tjne\t\tv%d, .L%d\n", val
, loop_top
);
496 if (stmt
->iterator_break
->used
)
497 printf(".L%p:\n", stmt
->iterator_break
);
499 printf(".L%d:\n", loop_bottom
);
506 printf(".L%p:\n", stmt
->label_identifier
);
507 show_statement(stmt
->label_statement
);
511 if (stmt
->goto_expression
) {
512 int val
= show_expression(stmt
->goto_expression
);
513 printf("\tgoto *v%d\n", val
);
515 printf("\tgoto .L%p\n", stmt
->goto_label
);
519 printf("\tasm( .... )\n");
526 static void show_one_statement(struct statement
*stmt
, void *sep
, int flags
)
528 show_statement(stmt
);
529 if (!(flags
& ITERATE_LAST
))
530 printf("%s", (const char *)sep
);
533 void show_statement_list(struct statement_list
*stmt
, const char *sep
)
535 statement_iterate(stmt
, show_one_statement
, (void *)sep
);
538 static void show_one_expression(struct expression
*expr
, void *sep
, int flags
)
540 show_expression(expr
);
541 if (!(flags
& ITERATE_LAST
))
542 printf("%s", (const char *)sep
);
545 void show_expression_list(struct expression_list
*list
, const char *sep
)
547 expression_iterate(list
, show_one_expression
, (void *)sep
);
550 static int new_pseudo(void)
556 static int show_call_expression(struct expression
*expr
)
558 struct symbol
*direct
;
559 struct expression
*arg
, *fn
;
564 warn(expr
->pos
, "\tcall with no type!");
569 FOR_EACH_PTR_REVERSE(expr
->args
, arg
) {
570 int new = show_expression(arg
);
571 int size
= arg
->ctype
->bit_size
;
572 printf("\tpush.%d\t\tv%d\n", size
, new);
573 framesize
+= size
>> 3;
574 } END_FOR_EACH_PTR_REVERSE
;
578 /* Remove dereference, if any */
580 if (fn
->type
== EXPR_PREOP
) {
581 if (fn
->unop
->type
== EXPR_SYMBOL
) {
582 struct symbol
*sym
= fn
->unop
->symbol
;
583 if (sym
->ctype
.base_type
->type
== SYM_FN
)
588 printf("\tcall\t\t%s\n", show_ident(direct
->ident
));
590 fncall
= show_expression(fn
);
591 printf("\tcall\t\t*v%d\n", fncall
);
594 printf("\tadd.%d\t\tvSP,vSP,$%d\n", BITS_IN_POINTER
, framesize
);
596 retval
= new_pseudo();
597 printf("\tmov.%d\t\tv%d,retval\n", expr
->ctype
->bit_size
, retval
);
601 static int show_binop(struct expression
*expr
)
603 int left
= show_expression(expr
->left
);
604 int right
= show_expression(expr
->right
);
605 int new = new_pseudo();
607 static const char *name
[] = {
608 ['+'] = "add", ['-'] = "sub",
609 ['*'] = "mul", ['/'] = "div",
610 ['%'] = "mod", ['&'] = "and",
611 ['|'] = "lor", ['^'] = "xor"
613 unsigned int op
= expr
->op
;
615 opname
= show_special(op
);
616 if (op
< sizeof(name
)/sizeof(*name
))
618 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname
,
619 expr
->ctype
->bit_size
,
624 static int show_regular_preop(struct expression
*expr
)
626 int target
= show_expression(expr
->unop
);
627 int new = new_pseudo();
628 static const char *name
[] = {
629 ['!'] = "nonzero", ['-'] = "neg",
632 unsigned int op
= expr
->op
;
635 opname
= show_special(op
);
636 if (op
< sizeof(name
)/sizeof(*name
))
638 printf("\t%s.%d\t\tv%d,v%d\n", opname
, expr
->ctype
->bit_size
, new, target
);
643 * FIXME! Not all accesses are memory loads. We should
644 * check what kind of symbol is behind the dereference.
646 static int show_address_gen(struct expression
*expr
)
648 if (expr
->type
== EXPR_PREOP
)
649 return show_expression(expr
->unop
);
650 return show_expression(expr
->address
);
653 static int show_load_gen(int bits
, struct expression
*expr
, int addr
)
655 int new = new_pseudo();
657 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, new, addr
);
658 if (expr
->type
== EXPR_PREOP
)
663 printf("\tshr.%d\t\tv%d,v%d,$%d\n", bits
, new, new, expr
->bitpos
);
664 printf("\tandi.%d\t\tv%d,v%d,$%llu\n", bits
, new, new, (1ULL << expr
->nrbits
)-1);
668 static void show_store_gen(int bits
, int value
, struct expression
*expr
, int addr
)
670 /* FIXME!!! Bitfield store! */
671 printf("\tst.%d\t\tv%d,[v%d]\n", bits
, value
, addr
);
674 static int show_assignment(struct expression
*expr
)
676 struct expression
*target
= expr
->left
;
682 bits
= expr
->ctype
->bit_size
;
683 val
= show_expression(expr
->right
);
684 addr
= show_address_gen(target
);
685 show_store_gen(bits
, val
, target
, addr
);
689 static int show_symbol_expr(struct symbol
*sym
);
691 static int show_initialization(struct symbol
*sym
, struct expression
*expr
)
698 bits
= expr
->ctype
->bit_size
;
699 val
= show_expression(expr
);
700 addr
= show_symbol_expr(sym
);
701 // FIXME! The "target" expression is for bitfield store information.
702 // Leave it NULL, which works fine.
703 show_store_gen(bits
, val
, NULL
, addr
);
707 static int show_access(struct expression
*expr
)
709 int addr
= show_address_gen(expr
);
710 return show_load_gen(expr
->ctype
->bit_size
, expr
, addr
);
713 static int show_inc_dec(struct expression
*expr
, int postop
)
715 int addr
= show_address_gen(expr
->unop
);
717 const char *opname
= expr
->op
== SPECIAL_INCREMENT
? "add" : "sub";
718 int bits
= expr
->ctype
->bit_size
;
720 retval
= show_load_gen(bits
, expr
->unop
, addr
);
724 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname
, bits
, new, retval
);
725 show_store_gen(bits
, new, expr
->unop
, addr
);
729 static int show_preop(struct expression
*expr
)
732 * '*' is an lvalue access, and is fundamentally different
733 * from an arithmetic operation. Maybe it should have an
734 * expression type of its own..
737 return show_access(expr
);
738 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
739 return show_inc_dec(expr
, 0);
740 return show_regular_preop(expr
);
743 static int show_postop(struct expression
*expr
)
745 return show_inc_dec(expr
, 1);
748 static int show_symbol_expr(struct symbol
*sym
)
750 int new = new_pseudo();
752 if (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_EXTERN
| MOD_STATIC
)) {
753 printf("\tmovi.%d\t\tv%d,$%s\n", BITS_IN_POINTER
, new, show_ident(sym
->ident
));
756 if (sym
->ctype
.modifiers
& MOD_ADDRESSABLE
) {
757 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", BITS_IN_POINTER
, new, sym
->value
);
760 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", BITS_IN_POINTER
, new, show_ident(sym
->ident
), sym
);
764 static int show_symbol_init(struct symbol
*sym
)
766 struct expression
*expr
= sym
->initializer
;
771 bits
= expr
->ctype
->bit_size
;
772 val
= show_expression(expr
);
773 addr
= show_symbol_expr(sym
);
774 show_store_gen(bits
, val
, NULL
, addr
);
779 static int type_is_signed(struct symbol
*sym
)
781 if (sym
->type
== SYM_NODE
)
782 sym
= sym
->ctype
.base_type
;
783 if (sym
->type
== SYM_PTR
)
785 return !(sym
->ctype
.modifiers
& MOD_UNSIGNED
);
788 static int show_cast_expr(struct expression
*expr
)
790 struct symbol
*old_type
, *new_type
;
791 int op
= show_expression(expr
->cast_expression
);
792 int oldbits
, newbits
;
795 old_type
= expr
->cast_expression
->ctype
;
796 new_type
= expr
->cast_type
;
798 oldbits
= old_type
->bit_size
;
799 newbits
= new_type
->bit_size
;
800 if (oldbits
>= newbits
)
803 is_signed
= type_is_signed(old_type
);
805 printf("\tsext%d.%d\tv%d,v%d\n", oldbits
, newbits
, new, op
);
807 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits
, new, op
, (1UL << oldbits
)-1);
812 static int show_value(struct expression
*expr
)
814 int new = new_pseudo();
815 unsigned long long value
= expr
->value
;
817 printf("\tmovi.%d\t\tv%d,$%llu\n", expr
->ctype
->bit_size
, new, value
);
821 static int show_string_expr(struct expression
*expr
)
823 int new = new_pseudo();
825 printf("\tmovi.%d\t\tv%d,&%s\n", BITS_IN_POINTER
, new, show_string(expr
->string
));
829 static int show_bitfield_expr(struct expression
*expr
)
831 return show_access(expr
);
834 int show_label_expr(struct expression
*expr
)
836 int new = new_pseudo();
837 printf("\tmovi.%d\t\tv%d,.L%p\n",BITS_IN_POINTER
, new, expr
->label_symbol
);
841 static int show_conditional_expr(struct expression
*expr
)
843 int cond
= show_expression(expr
->conditional
);
844 int true = show_expression(expr
->cond_true
);
845 int false = show_expression(expr
->cond_false
);
846 int new = new_pseudo();
850 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond
, expr
->ctype
->bit_size
, new, true, false);
854 static int show_statement_expr(struct expression
*expr
)
856 return show_statement(expr
->statement
);
859 static int show_position_expr(struct expression
*expr
, struct symbol
*base
)
861 int new = show_expression(expr
->init_expr
);
862 struct symbol
*ctype
= expr
->init_sym
;
864 printf("\tinsert v%d at [%d:%d] of %s\n", new,
865 expr
->init_offset
, ctype
->bit_offset
,
866 show_ident(base
->ident
));
870 static int show_initializer_expr(struct expression
*expr
, struct symbol
*ctype
)
872 struct expression
*entry
;
874 FOR_EACH_PTR(expr
->expr_list
, entry
) {
875 // Nested initializers have their positions already
876 // recursively calculated - just output them too
877 if (entry
->type
== EXPR_INITIALIZER
) {
878 show_initializer_expr(entry
, ctype
);
882 // Ignore initializer indexes and identifiers - the
883 // evaluator has taken them into account
884 if (entry
->type
== EXPR_IDENTIFIER
|| entry
->type
== EXPR_INDEX
)
886 if (entry
->type
== EXPR_POS
) {
887 show_position_expr(entry
, ctype
);
890 show_initialization(ctype
, entry
);
896 * Print out an expression. Return the pseudo that contains the
899 int show_expression(struct expression
*expr
)
905 struct position
*pos
= &expr
->pos
;
906 printf("\tno type at %s:%d:%d\n",
907 input_streams
[pos
->stream
].name
,
908 pos
->line
, pos
->pos
);
912 switch (expr
->type
) {
914 return show_call_expression(expr
);
916 case EXPR_ASSIGNMENT
:
917 return show_assignment(expr
);
923 return show_binop(expr
);
925 return show_preop(expr
);
927 return show_postop(expr
);
929 return show_symbol_expr(expr
->symbol
);
932 warn(expr
->pos
, "invalid expression after evaluation");
935 return show_cast_expr(expr
);
937 return show_value(expr
);
939 return show_string_expr(expr
);
941 return show_bitfield_expr(expr
);
942 case EXPR_INITIALIZER
:
943 return show_initializer_expr(expr
, expr
->ctype
);
944 case EXPR_CONDITIONAL
:
945 return show_conditional_expr(expr
);
947 return show_statement_expr(expr
);
949 return show_label_expr(expr
);
951 // None of these should exist as direct expressions: they are only
952 // valid as sub-expressions of initializers.
954 warn(expr
->pos
, "unable to show plain initializer position expression");
956 case EXPR_IDENTIFIER
:
957 warn(expr
->pos
, "unable to show identifier expression");
960 warn(expr
->pos
, "unable to show index expression");