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
)
289 type
= sym
->ctype
.base_type
;
294 * Show actual implementation information
296 switch (type
->type
) {
298 symbol_iterate(type
->symbol_list
, show_struct_member
, NULL
);
302 symbol_iterate(type
->symbol_list
, show_struct_member
, NULL
);
307 show_statement(type
->stmt
);
314 if (sym
->initializer
) {
316 show_expression(sym
->initializer
);
320 static int show_return_stmt(struct statement
*stmt
)
322 struct expression
*expr
= stmt
->expression
;
324 if (expr
&& expr
->ctype
) {
325 int val
= show_expression(expr
);
326 printf("\tmov.%d\t\tretval,v%d\n",
327 expr
->ctype
->bit_size
, val
);
333 static int show_symbol_init(struct symbol
*sym
);
335 static int new_label(void)
337 static int label
= 0;
342 * Print out a statement
344 int show_statement(struct statement
*stmt
)
348 switch (stmt
->type
) {
350 return show_return_stmt(stmt
);
351 case STMT_COMPOUND
: {
356 FOR_EACH_PTR(stmt
->syms
, sym
) {
357 show_symbol_init(sym
);
360 FOR_EACH_PTR(stmt
->stmts
, s
) {
361 last
= show_statement(s
);
366 case STMT_EXPRESSION
:
367 return show_expression(stmt
->expression
);
370 struct expression
*cond
= stmt
->if_conditional
;
372 if (cond
->type
== EXPR_VALUE
) {
373 struct statement
*s
= stmt
->if_true
;
379 val
= show_expression(cond
);
380 target
= new_label();
381 printf("\tje\t\tv%d,.L%d\n", val
, target
);
382 show_statement(stmt
->if_true
);
383 if (stmt
->if_false
) {
384 int last
= new_label();
385 printf("\tjmp\t\t.L%d\n", last
);
386 printf(".L%d:\n", target
);
388 show_statement(stmt
->if_false
);
390 printf(".L%d:\n", target
);
394 int val
= show_expression(stmt
->switch_expression
);
395 printf("\tswitch v%d\n", val
);
396 show_statement(stmt
->switch_statement
);
397 if (stmt
->switch_break
->used
)
398 printf(".L%p:\n", stmt
->switch_break
);
403 if (!stmt
->case_expression
)
406 printf("case %lld", stmt
->case_expression
->value
);
408 printf("...%lld", stmt
->case_to
->value
);
413 show_statement(stmt
->case_statement
);
416 case STMT_ITERATOR
: {
417 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
418 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
419 struct statement
*statement
= stmt
->iterator_statement
;
420 struct statement
*post_statement
= stmt
->iterator_post_statement
;
421 struct expression
*post_condition
= stmt
->iterator_post_condition
;
422 int val
, loop_top
= 0, loop_bottom
= 0;
424 show_statement(pre_statement
);
426 if (pre_condition
->type
== EXPR_VALUE
) {
427 if (!pre_condition
->value
)
429 pre_condition
= NULL
;
431 loop_bottom
= new_label();
432 val
= show_expression(pre_condition
);
433 printf("\tje\t\tv%d, .L%d\n", val
, loop_bottom
);
436 if (!post_condition
|| post_condition
->type
!= EXPR_VALUE
|| post_condition
->value
)
437 loop_top
= new_label();
438 printf(".L%d:\n", loop_top
);
439 show_statement(statement
);
440 if (stmt
->iterator_continue
->used
)
441 printf(".L%p:\n", stmt
->iterator_continue
);
442 show_statement(post_statement
);
443 if (!post_condition
) {
444 printf("\tjmp\t\t.L%d\n", loop_top
);
445 } else if (post_condition
->type
== EXPR_VALUE
) {
446 if (post_condition
->value
)
447 printf("\tjmp\t\t.L%d\n", loop_top
);
449 val
= show_expression(post_condition
);
450 printf("\tjne\t\tv%d, .L%d\n", val
, loop_top
);
452 if (stmt
->iterator_break
->used
)
453 printf(".L%p:\n", stmt
->iterator_break
);
455 printf(".L%d:\n", loop_bottom
);
459 printf("\t!NONE!\n");
463 printf(".L%p:\n", stmt
->label_identifier
);
464 show_statement(stmt
->label_statement
);
468 if (stmt
->goto_expression
) {
469 int val
= show_expression(stmt
->goto_expression
);
470 printf("\tgoto *v%d\n", val
);
472 printf("\tgoto .L%p\n", stmt
->goto_label
);
476 printf("\tasm( .... )\n");
483 static void show_one_statement(struct statement
*stmt
, void *sep
, int flags
)
485 show_statement(stmt
);
486 if (!(flags
& ITERATE_LAST
))
487 printf("%s", (const char *)sep
);
490 void show_statement_list(struct statement_list
*stmt
, const char *sep
)
492 statement_iterate(stmt
, show_one_statement
, (void *)sep
);
495 static void show_one_expression(struct expression
*expr
, void *sep
, int flags
)
497 show_expression(expr
);
498 if (!(flags
& ITERATE_LAST
))
499 printf("%s", (const char *)sep
);
502 void show_expression_list(struct expression_list
*list
, const char *sep
)
504 expression_iterate(list
, show_one_expression
, (void *)sep
);
507 static int new_pseudo(void)
513 static int show_call_expression(struct expression
*expr
)
515 struct symbol
*direct
;
516 struct expression
*arg
, *fn
;
521 warn(expr
->pos
, "\tcall with no type!");
526 FOR_EACH_PTR_REVERSE(expr
->args
, arg
) {
527 int new = show_expression(arg
);
528 int size
= arg
->ctype
->bit_size
;
529 printf("\tpush.%d\t\tv%d\n", size
, new);
530 framesize
+= size
>> 3;
535 /* Remove dereference, if any */
537 if (fn
->type
== EXPR_PREOP
) {
538 if (fn
->unop
->type
== EXPR_SYMBOL
) {
539 struct symbol
*sym
= fn
->unop
->symbol
;
540 if (sym
->ctype
.base_type
->type
== SYM_FN
)
545 printf("\tcall\t\t%s\n", show_ident(direct
->ident
));
547 fncall
= show_expression(fn
);
548 printf("\tcall\t\t*v%d\n", fncall
);
551 printf("\tadd.%d\t\tvSP,vSP,$%d\n", BITS_IN_POINTER
, framesize
);
553 retval
= new_pseudo();
554 printf("\tmov.%d\t\tv%d,retval\n", expr
->ctype
->bit_size
, retval
);
558 static int show_binop(struct expression
*expr
)
560 int left
= show_expression(expr
->left
);
561 int right
= show_expression(expr
->right
);
562 int new = new_pseudo();
564 static const char *name
[] = {
565 ['+'] = "add", ['-'] = "sub",
566 ['*'] = "mul", ['/'] = "div",
569 unsigned int op
= expr
->op
;
571 opname
= show_special(op
);
572 if (op
< sizeof(name
)/sizeof(*name
))
574 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname
,
575 expr
->ctype
->bit_size
,
580 static int show_regular_preop(struct expression
*expr
)
582 int target
= show_expression(expr
->unop
);
583 int new = new_pseudo();
584 static const char *name
[] = {
585 ['!'] = "nonzero", ['-'] = "neg",
588 unsigned int op
= expr
->op
;
591 opname
= show_special(op
);
592 if (op
< sizeof(name
)/sizeof(*name
))
594 printf("\t%s.%d\t\tv%d,v%d\n", opname
, expr
->ctype
->bit_size
, new, target
);
599 * FIXME! Not all accesses are memory loads. We should
600 * check what kind of symbol is behind the dereference.
602 static int show_address_gen(struct expression
*expr
)
604 if (expr
->type
== EXPR_PREOP
)
605 return show_expression(expr
->unop
);
606 return show_expression(expr
->address
);
609 static int show_load_gen(int bits
, struct expression
*expr
, int addr
)
611 int new = new_pseudo();
613 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, new, addr
);
614 if (expr
->type
== EXPR_PREOP
)
619 printf("\tshr.%d\t\tv%d,v%d,$%d\n", bits
, new, new, expr
->bitpos
);
620 printf("\tandi.%d\t\tv%d,v%d,$%llu\n", bits
, new, new, (1ULL << expr
->nrbits
)-1);
624 static void show_store_gen(int bits
, int value
, struct expression
*expr
, int addr
)
626 /* FIXME!!! Bitfield store! */
627 printf("\tst.%d\t\tv%d,[v%d]\n", bits
, value
, addr
);
630 static int show_assignment(struct expression
*expr
)
632 struct expression
*target
= expr
->left
;
638 bits
= expr
->ctype
->bit_size
;
639 val
= show_expression(expr
->right
);
640 addr
= show_address_gen(target
);
641 show_store_gen(bits
, val
, target
, addr
);
645 static int show_symbol_expr(struct symbol
*sym
);
647 static int show_initialization(struct symbol
*sym
, struct expression
*expr
)
654 bits
= expr
->ctype
->bit_size
;
655 val
= show_expression(expr
);
656 addr
= show_symbol_expr(sym
);
657 // FIXME! The "target" expression is for bitfield store information.
658 // Leave it NULL, which works fine.
659 show_store_gen(bits
, val
, NULL
, addr
);
663 static int show_access(struct expression
*expr
)
665 int addr
= show_address_gen(expr
);
666 return show_load_gen(expr
->ctype
->bit_size
, expr
, addr
);
669 static int show_inc_dec(struct expression
*expr
, int postop
)
671 int addr
= show_address_gen(expr
->unop
);
673 const char *opname
= expr
->op
== SPECIAL_INCREMENT
? "add" : "sub";
674 int bits
= expr
->ctype
->bit_size
;
676 retval
= show_load_gen(bits
, expr
->unop
, addr
);
680 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname
, bits
, new, retval
);
681 show_store_gen(bits
, new, expr
->unop
, addr
);
685 static int show_preop(struct expression
*expr
)
688 * '*' is an lvalue access, and is fundamentally different
689 * from an arithmetic operation. Maybe it should have an
690 * expression type of its own..
693 return show_access(expr
);
694 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
695 return show_inc_dec(expr
, 0);
696 return show_regular_preop(expr
);
699 static int show_postop(struct expression
*expr
)
701 return show_inc_dec(expr
, 1);
704 static int show_symbol_expr(struct symbol
*sym
)
706 int new = new_pseudo();
708 if (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_EXTERN
| MOD_STATIC
)) {
709 printf("\tmovi.%d\t\tv%d,$%s\n", BITS_IN_POINTER
, new, show_ident(sym
->ident
));
712 if (sym
->ctype
.modifiers
& MOD_ADDRESSABLE
) {
713 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", BITS_IN_POINTER
, new, sym
->value
);
716 printf("\taddi.%d\t\tv%d,vFP,$xxx\n", BITS_IN_POINTER
, new);
720 static int show_symbol_init(struct symbol
*sym
)
722 struct expression
*expr
= sym
->initializer
;
727 bits
= expr
->ctype
->bit_size
;
728 val
= show_expression(expr
);
729 addr
= show_symbol_expr(sym
);
730 show_store_gen(bits
, val
, NULL
, addr
);
735 static int type_is_signed(struct symbol
*sym
)
737 if (sym
->type
== SYM_NODE
)
738 sym
= sym
->ctype
.base_type
;
739 if (sym
->type
== SYM_PTR
)
741 return !(sym
->ctype
.modifiers
& MOD_UNSIGNED
);
744 static int show_cast_expr(struct expression
*expr
)
746 struct symbol
*old_type
, *new_type
;
747 int op
= show_expression(expr
->cast_expression
);
748 int oldbits
, newbits
;
751 old_type
= expr
->cast_expression
->ctype
;
752 new_type
= expr
->cast_type
;
754 oldbits
= old_type
->bit_size
;
755 newbits
= new_type
->bit_size
;
756 if (oldbits
>= newbits
)
759 is_signed
= type_is_signed(old_type
);
761 printf("\tsext%d.%d\tv%d,v%d\n", oldbits
, newbits
, new, op
);
763 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits
, new, op
, (1UL << oldbits
)-1);
768 static int show_value(struct expression
*expr
)
770 int new = new_pseudo();
771 unsigned long long value
= expr
->value
;
773 printf("\tmovi.%d\t\tv%d,$%llu\n", expr
->ctype
->bit_size
, new, value
);
777 static int show_string_expr(struct expression
*expr
)
779 int new = new_pseudo();
781 printf("\tmovi.%d\t\tv%d,&%s\n", BITS_IN_POINTER
, new, show_string(expr
->string
));
785 static int show_bitfield_expr(struct expression
*expr
)
787 return show_access(expr
);
790 static int show_conditional_expr(struct expression
*expr
)
792 int cond
= show_expression(expr
->conditional
);
793 int true = show_expression(expr
->cond_true
);
794 int false = show_expression(expr
->cond_false
);
795 int new = new_pseudo();
799 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond
, expr
->ctype
->bit_size
, new, true, false);
803 static int show_statement_expr(struct expression
*expr
)
805 return show_statement(expr
->statement
);
808 static int show_position_expr(struct expression
*expr
, struct symbol
*base
)
810 int new = show_expression(expr
->init_expr
);
811 struct symbol
*ctype
= expr
->init_sym
;
813 printf("\tinsert v%d at [%d:%d] of %s\n", new,
814 expr
->init_offset
, ctype
->bit_offset
,
815 show_ident(base
->ident
));
819 static int show_initializer_expr(struct expression
*expr
, struct symbol
*ctype
)
821 struct expression
*entry
;
823 FOR_EACH_PTR(expr
->expr_list
, entry
) {
824 // Nested initializers have their positions already
825 // recursively calculated - just output them too
826 if (entry
->type
== EXPR_INITIALIZER
) {
827 show_initializer_expr(entry
, ctype
);
831 // Ignore initializer indexes and identifiers - the
832 // evaluator has taken them into account
833 if (entry
->type
== EXPR_IDENTIFIER
|| entry
->type
== EXPR_INDEX
)
835 if (entry
->type
== EXPR_POS
) {
836 show_position_expr(entry
, ctype
);
839 show_initialization(ctype
, entry
);
845 * Print out an expression. Return the pseudo that contains the
848 int show_expression(struct expression
*expr
)
854 struct position
*pos
= &expr
->pos
;
855 printf("\tno type at %s:%d:%d\n",
856 input_streams
[pos
->stream
].name
,
857 pos
->line
, pos
->pos
);
861 switch (expr
->type
) {
863 return show_call_expression(expr
);
865 case EXPR_ASSIGNMENT
:
866 return show_assignment(expr
);
872 return show_binop(expr
);
874 return show_preop(expr
);
876 return show_postop(expr
);
878 return show_symbol_expr(expr
->symbol
);
881 warn(expr
->pos
, "invalid expression after evaluation");
884 return show_cast_expr(expr
);
886 return show_value(expr
);
888 return show_string_expr(expr
);
890 return show_bitfield_expr(expr
);
891 case EXPR_INITIALIZER
:
892 return show_initializer_expr(expr
, expr
->ctype
);
893 case EXPR_CONDITIONAL
:
894 return show_conditional_expr(expr
);
896 return show_statement_expr(expr
);
898 // None of these should exist as direct expressions: they are only
899 // valid as sub-expressions of initializers.
901 warn(expr
->pos
, "unable to show plain initializer position expression");
903 case EXPR_IDENTIFIER
:
904 warn(expr
->pos
, "unable to show identifier expression");
907 warn(expr
->pos
, "unable to show index expression");