4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Print out results of parsing for debugging and testing.
25 #include "expression.h"
28 static int show_symbol_expr(struct symbol
*sym
);
29 static int show_string_expr(struct expression
*expr
);
31 static void do_debug_symbol(struct symbol
*sym
, int indent
)
33 static const char indent_string
[] = " ";
34 static const char *typestr
[] = {
35 [SYM_UNINITIALIZED
] = "none",
36 [SYM_PREPROCESSOR
] = "cpp.",
37 [SYM_BASETYPE
] = "base",
42 [SYM_STRUCT
] = "strt",
45 [SYM_TYPEDEF
] = "tdef",
46 [SYM_TYPEOF
] = "tpof",
47 [SYM_MEMBER
] = "memb",
48 [SYM_BITFIELD
] = "bitf",
50 [SYM_RESTRICT
] = "rstr",
51 [SYM_FOULED
] = "foul",
54 struct context
*context
;
59 fprintf(stderr
, "%.*s%s%3d:%lu %s %s (as: %d) %p (%s:%d:%d) %s\n",
60 indent
, indent_string
, typestr
[sym
->type
],
61 sym
->bit_size
, sym
->ctype
.alignment
,
62 modifier_string(sym
->ctype
.modifiers
), show_ident(sym
->ident
), sym
->ctype
.as
,
63 sym
, stream_name(sym
->pos
.stream
), sym
->pos
.line
, sym
->pos
.pos
,
64 builtin_typename(sym
) ?: "");
66 FOR_EACH_PTR(sym
->ctype
.contexts
, context
) {
67 /* FIXME: should print context expression */
68 fprintf(stderr
, "< context%d: in=%d, out=%d\n",
69 i
, context
->in
, context
->out
);
70 fprintf(stderr
, " end context%d >\n", i
);
72 } END_FOR_EACH_PTR(context
);
73 if (sym
->type
== SYM_FN
) {
76 FOR_EACH_PTR(sym
->arguments
, arg
) {
77 fprintf(stderr
, "< arg%d:\n", i
);
78 do_debug_symbol(arg
, 0);
79 fprintf(stderr
, " end arg%d >\n", i
);
81 } END_FOR_EACH_PTR(arg
);
83 do_debug_symbol(sym
->ctype
.base_type
, indent
+2);
86 void debug_symbol(struct symbol
*sym
)
88 do_debug_symbol(sym
, 0);
92 * Symbol type printout. The type system is by far the most
93 * complicated part of C - everything else is trivial.
95 const char *modifier_string(unsigned long mod
)
97 static char buffer
[100];
105 static struct mod_name mod_names
[] = {
107 {MOD_REGISTER
, "register"},
108 {MOD_STATIC
, "static"},
109 {MOD_EXTERN
, "extern"},
110 {MOD_CONST
, "const"},
111 {MOD_VOLATILE
, "volatile"},
112 {MOD_SIGNED
, "[signed]"},
113 {MOD_UNSIGNED
, "[unsigned]"},
114 {MOD_CHAR
, "[char]"},
115 {MOD_SHORT
, "[short]"},
116 {MOD_LONG
, "[long]"},
117 {MOD_LONGLONG
, "[long long]"},
118 {MOD_LONGLONGLONG
, "[long long long]"},
119 {MOD_TYPEDEF
, "[typedef]"},
121 {MOD_INLINE
, "inline"},
122 {MOD_ADDRESSABLE
, "[addressable]"},
123 {MOD_NOCAST
, "[nocast]"},
124 {MOD_NODEREF
, "[noderef]"},
125 {MOD_ACCESSED
, "[accessed]"},
126 {MOD_TOPLEVEL
, "[toplevel]"},
127 {MOD_ASSIGNED
, "[assigned]"},
128 {MOD_TYPE
, "[type]"},
129 {MOD_SAFE
, "[safe]"},
130 {MOD_USERTYPE
, "[usertype]"},
131 {MOD_NORETURN
, "[noreturn]"},
132 {MOD_EXPLICITLY_SIGNED
, "[explicitly-signed]"},
133 {MOD_BITWISE
, "[bitwise]"},
136 for (i
= 0; i
< ARRAY_SIZE(mod_names
); i
++) {
140 const char *name
= m
->name
;
141 while ((c
= *name
++) != '\0' && len
+ 2 < sizeof buffer
)
150 static void show_struct_member(struct symbol
*sym
)
152 printf("\t%s:%d:%ld at offset %ld.%d", show_ident(sym
->ident
), sym
->bit_size
, sym
->ctype
.alignment
, sym
->offset
, sym
->bit_offset
);
156 void show_symbol_list(struct symbol_list
*list
, const char *sep
)
159 const char *prepend
= "";
161 FOR_EACH_PTR(list
, sym
) {
165 } END_FOR_EACH_PTR(sym
);
173 static void FORMAT_ATTR(2) prepend(struct type_name
*name
, const char *fmt
, ...)
175 static char buffer
[512];
180 n
= vsprintf(buffer
, fmt
, args
);
184 memcpy(name
->start
, buffer
, n
);
187 static void FORMAT_ATTR(2) append(struct type_name
*name
, const char *fmt
, ...)
189 static char buffer
[512];
194 n
= vsprintf(buffer
, fmt
, args
);
197 memcpy(name
->end
, buffer
, n
);
201 static struct ctype_name
{
205 { & char_ctype
, "char" },
206 { &schar_ctype
, "signed char" },
207 { &uchar_ctype
, "unsigned char" },
208 { & short_ctype
, "short" },
209 { &sshort_ctype
, "signed short" },
210 { &ushort_ctype
, "unsigned short" },
211 { & int_ctype
, "int" },
212 { &sint_ctype
, "signed int" },
213 { &uint_ctype
, "unsigned int" },
214 { &slong_ctype
, "signed long" },
215 { & long_ctype
, "long" },
216 { &ulong_ctype
, "unsigned long" },
217 { & llong_ctype
, "long long" },
218 { &sllong_ctype
, "signed long long" },
219 { &ullong_ctype
, "unsigned long long" },
220 { & lllong_ctype
, "long long long" },
221 { &slllong_ctype
, "signed long long long" },
222 { &ulllong_ctype
, "unsigned long long long" },
224 { &void_ctype
, "void" },
225 { &bool_ctype
, "bool" },
226 { &string_ctype
, "string" },
228 { &float_ctype
, "float" },
229 { &double_ctype
, "double" },
230 { &ldouble_ctype
,"long double" },
231 { &incomplete_ctype
, "incomplete type" },
232 { &int_type
, "abstract int" },
233 { &fp_type
, "abstract fp" },
234 { &label_ctype
, "label type" },
235 { &bad_ctype
, "bad type" },
238 const char *builtin_typename(struct symbol
*sym
)
242 for (i
= 0; i
< sizeof(typenames
)/sizeof(typenames
[0]); i
++)
243 if (typenames
[i
].sym
== sym
)
244 return typenames
[i
].name
;
248 const char *builtin_ctypename(struct ctype
*ctype
)
252 for (i
= 0; i
< sizeof(typenames
)/sizeof(typenames
[0]); i
++)
253 if (&typenames
[i
].sym
->ctype
== ctype
)
254 return typenames
[i
].name
;
258 static void do_show_type(struct symbol
*sym
, struct type_name
*name
)
260 const char *typename
;
261 unsigned long mod
= 0;
268 if (!sym
|| (sym
->type
!= SYM_NODE
&& sym
->type
!= SYM_ARRAY
&&
269 sym
->type
!= SYM_BITFIELD
)) {
274 prepend(name
, "<asn:%d>", as
);
276 s
= modifier_string(mod
);
279 memcpy(name
->start
, s
, len
);
287 if ((typename
= builtin_typename(sym
))) {
288 int len
= strlen(typename
);
289 if (name
->start
!= name
->end
)
290 *--name
->start
= ' ';
292 memcpy(name
->start
, typename
, len
);
300 mod
= sym
->ctype
.modifiers
;
311 append(name
, "( ... )");
315 if (name
->start
!= name
->end
)
316 *--name
->start
= ' ';
317 prepend(name
, "struct %s", show_ident(sym
->ident
));
321 if (name
->start
!= name
->end
)
322 *--name
->start
= ' ';
323 prepend(name
, "union %s", show_ident(sym
->ident
));
327 prepend(name
, "enum %s ", show_ident(sym
->ident
));
331 append(name
, "%s", show_ident(sym
->ident
));
332 mod
|= sym
->ctype
.modifiers
;
337 mod
|= sym
->ctype
.modifiers
;
339 append(name
, ":%d", sym
->bit_size
);
343 append(name
, "label(%s:%p)", show_ident(sym
->ident
), sym
);
347 mod
|= sym
->ctype
.modifiers
;
354 append(name
, "[%lld]", get_expression_value(sym
->array_size
));
362 if (name
->start
!= name
->end
)
363 *--name
->start
= ' ';
364 prepend(name
, "restricted %s", show_ident(sym
->ident
));
372 if (name
->start
!= name
->end
)
373 *--name
->start
= ' ';
374 prepend(name
, "unknown type %d", sym
->type
);
378 sym
= sym
->ctype
.base_type
;
383 prepend(name
, "restricted ");
385 prepend(name
, "fouled ");
388 void show_type(struct symbol
*sym
)
391 struct type_name name
;
393 name
.start
= name
.end
= array
+100;
394 do_show_type(sym
, &name
);
396 printf("%s", name
.start
);
399 const char *show_typename(struct symbol
*sym
)
401 static char array
[200];
402 struct type_name name
;
404 name
.start
= name
.end
= array
+100;
405 do_show_type(sym
, &name
);
410 void show_symbol(struct symbol
*sym
)
417 if (sym
->ctype
.alignment
)
418 printf(".align %ld\n", sym
->ctype
.alignment
);
421 type
= sym
->ctype
.base_type
;
428 * Show actual implementation information
430 switch (type
->type
) {
431 struct symbol
*member
;
436 FOR_EACH_PTR(type
->symbol_list
, member
) {
437 show_struct_member(member
);
438 } END_FOR_EACH_PTR(member
);
443 struct statement
*stmt
= type
->stmt
;
447 val
= show_statement(stmt
);
449 printf("\tmov.%d\t\tretval,%d\n", stmt
->ret
->bit_size
, val
);
460 if (sym
->initializer
) {
462 show_expression(sym
->initializer
);
466 static int show_symbol_init(struct symbol
*sym
);
468 static int new_pseudo(void)
474 static int new_label(void)
476 static int label
= 0;
480 static void show_switch_statement(struct statement
*stmt
)
482 int val
= show_expression(stmt
->switch_expression
);
484 printf("\tswitch v%d\n", val
);
487 * Debugging only: Check that the case list is correct
488 * by printing it out.
490 * This is where a _real_ back-end would go through the
491 * cases to decide whether to use a lookup table or a
492 * series of comparisons etc
494 printf("# case table:\n");
495 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
496 struct statement
*case_stmt
= sym
->stmt
;
497 struct expression
*expr
= case_stmt
->case_expression
;
498 struct expression
*to
= case_stmt
->case_to
;
503 if (expr
->type
== EXPR_VALUE
) {
504 printf(" case %lld", expr
->value
);
506 if (to
->type
== EXPR_VALUE
) {
507 printf(" .. %lld", to
->value
);
515 printf(": .L%p\n", sym
->bb_target
);
516 } END_FOR_EACH_PTR(sym
);
517 printf("# end case table\n");
519 show_statement(stmt
->switch_statement
);
521 if (stmt
->switch_break
->used
)
522 printf(".L%p:\n", stmt
->switch_break
->bb_target
);
525 static void show_symbol_decl(struct symbol_list
*syms
)
528 FOR_EACH_PTR(syms
, sym
) {
529 show_symbol_init(sym
);
530 } END_FOR_EACH_PTR(sym
);
533 static int show_return_stmt(struct statement
*stmt
);
536 * Print out a statement
538 int show_statement(struct statement
*stmt
)
542 switch (stmt
->type
) {
543 case STMT_DECLARATION
:
544 show_symbol_decl(stmt
->declaration
);
547 return show_return_stmt(stmt
);
548 case STMT_COMPOUND
: {
552 if (stmt
->inline_fn
) {
553 show_statement(stmt
->args
);
554 printf("\tbegin_inline \t%s\n", show_ident(stmt
->inline_fn
->ident
));
556 FOR_EACH_PTR(stmt
->stmts
, s
) {
557 last
= show_statement(s
);
558 } END_FOR_EACH_PTR(s
);
561 printf(".L%p:\n", stmt
->ret
);
562 addr
= show_symbol_expr(stmt
->ret
);
563 bits
= stmt
->ret
->bit_size
;
565 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, last
, addr
);
568 printf("\tend_inlined\t%s\n", show_ident(stmt
->inline_fn
->ident
));
572 case STMT_EXPRESSION
:
573 return show_expression(stmt
->expression
);
576 struct expression
*cond
= stmt
->if_conditional
;
578 /* This is only valid if nobody can jump into the "dead" statement */
580 if (cond
->type
== EXPR_VALUE
) {
581 struct statement
*s
= stmt
->if_true
;
588 val
= show_expression(cond
);
589 target
= new_label();
590 printf("\tje\t\tv%d,.L%d\n", val
, target
);
591 show_statement(stmt
->if_true
);
592 if (stmt
->if_false
) {
593 int last
= new_label();
594 printf("\tjmp\t\t.L%d\n", last
);
595 printf(".L%d:\n", target
);
597 show_statement(stmt
->if_false
);
599 printf(".L%d:\n", target
);
603 show_switch_statement(stmt
);
607 printf(".L%p:\n", stmt
->case_label
);
608 show_statement(stmt
->case_statement
);
611 case STMT_ITERATOR
: {
612 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
613 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
614 struct statement
*statement
= stmt
->iterator_statement
;
615 struct statement
*post_statement
= stmt
->iterator_post_statement
;
616 struct expression
*post_condition
= stmt
->iterator_post_condition
;
617 int val
, loop_top
= 0, loop_bottom
= 0;
619 show_symbol_decl(stmt
->iterator_syms
);
620 show_statement(pre_statement
);
622 if (pre_condition
->type
== EXPR_VALUE
) {
623 if (!pre_condition
->value
) {
624 loop_bottom
= new_label();
625 printf("\tjmp\t\t.L%d\n", loop_bottom
);
628 loop_bottom
= new_label();
629 val
= show_expression(pre_condition
);
630 printf("\tje\t\tv%d, .L%d\n", val
, loop_bottom
);
633 if (!post_condition
|| post_condition
->type
!= EXPR_VALUE
|| post_condition
->value
) {
634 loop_top
= new_label();
635 printf(".L%d:\n", loop_top
);
637 show_statement(statement
);
638 if (stmt
->iterator_continue
->used
)
639 printf(".L%p:\n", stmt
->iterator_continue
);
640 show_statement(post_statement
);
641 if (!post_condition
) {
642 printf("\tjmp\t\t.L%d\n", loop_top
);
643 } else if (post_condition
->type
== EXPR_VALUE
) {
644 if (post_condition
->value
)
645 printf("\tjmp\t\t.L%d\n", loop_top
);
647 val
= show_expression(post_condition
);
648 printf("\tjne\t\tv%d, .L%d\n", val
, loop_top
);
650 if (stmt
->iterator_break
->used
)
651 printf(".L%p:\n", stmt
->iterator_break
);
653 printf(".L%d:\n", loop_bottom
);
660 printf(".L%p:\n", stmt
->label_identifier
);
661 show_statement(stmt
->label_statement
);
665 if (stmt
->goto_expression
) {
666 int val
= show_expression(stmt
->goto_expression
);
667 printf("\tgoto\t\t*v%d\n", val
);
669 printf("\tgoto\t\t.L%p\n", stmt
->goto_label
->bb_target
);
673 printf("\tasm( .... )\n");
676 int val
= show_expression(stmt
->expression
);
677 printf("\tcontext( %d )\n", val
);
681 int val
= show_expression(stmt
->range_expression
);
682 int low
= show_expression(stmt
->range_low
);
683 int high
= show_expression(stmt
->range_high
);
684 printf("\trange( %d %d-%d)\n", val
, low
, high
);
691 static int show_call_expression(struct expression
*expr
)
693 struct symbol
*direct
;
694 struct expression
*arg
, *fn
;
699 warning(expr
->pos
, "\tcall with no type!");
704 FOR_EACH_PTR_REVERSE(expr
->args
, arg
) {
705 int new = show_expression(arg
);
706 int size
= arg
->ctype
->bit_size
;
707 printf("\tpush.%d\t\tv%d\n", size
, new);
708 framesize
+= bits_to_bytes(size
);
709 } END_FOR_EACH_PTR_REVERSE(arg
);
713 /* Remove dereference, if any */
715 if (fn
->type
== EXPR_PREOP
) {
716 if (fn
->unop
->type
== EXPR_SYMBOL
) {
717 struct symbol
*sym
= fn
->unop
->symbol
;
718 if (sym
->ctype
.base_type
->type
== SYM_FN
)
723 printf("\tcall\t\t%s\n", show_ident(direct
->ident
));
725 fncall
= show_expression(fn
);
726 printf("\tcall\t\t*v%d\n", fncall
);
729 printf("\tadd.%d\t\tvSP,vSP,$%d\n", bits_in_pointer
, framesize
);
731 retval
= new_pseudo();
732 printf("\tmov.%d\t\tv%d,retval\n", expr
->ctype
->bit_size
, retval
);
736 static int show_comma(struct expression
*expr
)
738 show_expression(expr
->left
);
739 return show_expression(expr
->right
);
742 static int show_binop(struct expression
*expr
)
744 int left
= show_expression(expr
->left
);
745 int right
= show_expression(expr
->right
);
746 int new = new_pseudo();
748 static const char *name
[] = {
749 ['+'] = "add", ['-'] = "sub",
750 ['*'] = "mul", ['/'] = "div",
751 ['%'] = "mod", ['&'] = "and",
752 ['|'] = "lor", ['^'] = "xor"
754 unsigned int op
= expr
->op
;
756 opname
= show_special(op
);
757 if (op
< sizeof(name
)/sizeof(*name
))
759 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname
,
760 expr
->ctype
->bit_size
,
765 static int show_slice(struct expression
*expr
)
767 int target
= show_expression(expr
->base
);
768 int new = new_pseudo();
769 printf("\tslice.%d\t\tv%d,v%d,%d\n", expr
->r_nrbits
, target
, new, expr
->r_bitpos
);
773 static int show_regular_preop(struct expression
*expr
)
775 int target
= show_expression(expr
->unop
);
776 int new = new_pseudo();
777 static const char *name
[] = {
778 ['!'] = "nonzero", ['-'] = "neg",
781 unsigned int op
= expr
->op
;
784 opname
= show_special(op
);
785 if (op
< sizeof(name
)/sizeof(*name
))
787 printf("\t%s.%d\t\tv%d,v%d\n", opname
, expr
->ctype
->bit_size
, new, target
);
792 * FIXME! Not all accesses are memory loads. We should
793 * check what kind of symbol is behind the dereference.
795 static int show_address_gen(struct expression
*expr
)
797 return show_expression(expr
->unop
);
800 static int show_load_gen(int bits
, struct expression
*expr
, int addr
)
802 int new = new_pseudo();
804 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, new, addr
);
808 static void show_store_gen(int bits
, int value
, struct expression
*expr
, int addr
)
810 /* FIXME!!! Bitfield store! */
811 printf("\tst.%d\t\tv%d,[v%d]\n", bits
, value
, addr
);
814 static int show_assignment(struct expression
*expr
)
816 struct expression
*target
= expr
->left
;
822 bits
= expr
->ctype
->bit_size
;
823 val
= show_expression(expr
->right
);
824 addr
= show_address_gen(target
);
825 show_store_gen(bits
, val
, target
, addr
);
829 static int show_return_stmt(struct statement
*stmt
)
831 struct expression
*expr
= stmt
->ret_value
;
832 struct symbol
*target
= stmt
->ret_target
;
834 if (expr
&& expr
->ctype
) {
835 int val
= show_expression(expr
);
836 int bits
= expr
->ctype
->bit_size
;
837 int addr
= show_symbol_expr(target
);
838 show_store_gen(bits
, val
, NULL
, addr
);
840 printf("\tret\t\t(%p)\n", target
);
844 static int show_initialization(struct symbol
*sym
, struct expression
*expr
)
851 bits
= expr
->ctype
->bit_size
;
852 val
= show_expression(expr
);
853 addr
= show_symbol_expr(sym
);
854 // FIXME! The "target" expression is for bitfield store information.
855 // Leave it NULL, which works fine.
856 show_store_gen(bits
, val
, NULL
, addr
);
860 static int show_access(struct expression
*expr
)
862 int addr
= show_address_gen(expr
);
863 return show_load_gen(expr
->ctype
->bit_size
, expr
, addr
);
866 static int show_inc_dec(struct expression
*expr
, int postop
)
868 int addr
= show_address_gen(expr
->unop
);
870 const char *opname
= expr
->op
== SPECIAL_INCREMENT
? "add" : "sub";
871 int bits
= expr
->ctype
->bit_size
;
873 retval
= show_load_gen(bits
, expr
->unop
, addr
);
877 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname
, bits
, new, retval
);
878 show_store_gen(bits
, new, expr
->unop
, addr
);
882 static int show_preop(struct expression
*expr
)
885 * '*' is an lvalue access, and is fundamentally different
886 * from an arithmetic operation. Maybe it should have an
887 * expression type of its own..
890 return show_access(expr
);
891 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
892 return show_inc_dec(expr
, 0);
893 return show_regular_preop(expr
);
896 static int show_postop(struct expression
*expr
)
898 return show_inc_dec(expr
, 1);
901 static int show_symbol_expr(struct symbol
*sym
)
903 int new = new_pseudo();
905 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_STRING
)
906 return show_string_expr(sym
->initializer
);
908 if (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_EXTERN
| MOD_STATIC
)) {
909 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer
, new, show_ident(sym
->ident
));
912 if (sym
->ctype
.modifiers
& MOD_ADDRESSABLE
) {
913 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer
, new, sym
->value
);
916 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer
, new, show_ident(sym
->ident
), sym
);
920 static int show_symbol_init(struct symbol
*sym
)
922 struct expression
*expr
= sym
->initializer
;
927 bits
= expr
->ctype
->bit_size
;
928 val
= show_expression(expr
);
929 addr
= show_symbol_expr(sym
);
930 show_store_gen(bits
, val
, NULL
, addr
);
935 static int type_is_signed(struct symbol
*sym
)
937 if (sym
->type
== SYM_NODE
)
938 sym
= sym
->ctype
.base_type
;
939 if (sym
->type
== SYM_PTR
)
941 return !(sym
->ctype
.modifiers
& MOD_UNSIGNED
);
944 static int show_cast_expr(struct expression
*expr
)
946 struct symbol
*old_type
, *new_type
;
947 int op
= show_expression(expr
->cast_expression
);
948 int oldbits
, newbits
;
951 old_type
= expr
->cast_expression
->ctype
;
952 new_type
= expr
->cast_type
;
954 oldbits
= old_type
->bit_size
;
955 newbits
= new_type
->bit_size
;
956 if (oldbits
>= newbits
)
959 is_signed
= type_is_signed(old_type
);
961 printf("\tsext%d.%d\tv%d,v%d\n", oldbits
, newbits
, new, op
);
963 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits
, new, op
, (1UL << oldbits
)-1);
968 static int show_value(struct expression
*expr
)
970 int new = new_pseudo();
971 unsigned long long value
= expr
->value
;
973 printf("\tmovi.%d\t\tv%d,$%llu\n", expr
->ctype
->bit_size
, new, value
);
977 static int show_fvalue(struct expression
*expr
)
979 int new = new_pseudo();
980 long double value
= expr
->fvalue
;
982 printf("\tmovf.%d\t\tv%d,$%Lf\n", expr
->ctype
->bit_size
, new, value
);
986 static int show_string_expr(struct expression
*expr
)
988 int new = new_pseudo();
990 printf("\tmovi.%d\t\tv%d,&%s\n", bits_in_pointer
, new, show_string(expr
->string
));
994 static int show_label_expr(struct expression
*expr
)
996 int new = new_pseudo();
997 printf("\tmovi.%d\t\tv%d,.L%p\n",bits_in_pointer
, new, expr
->label_symbol
);
1001 static int show_conditional_expr(struct expression
*expr
)
1003 int cond
= show_expression(expr
->conditional
);
1004 int true = show_expression(expr
->cond_true
);
1005 int false = show_expression(expr
->cond_false
);
1006 int new = new_pseudo();
1008 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond
, expr
->ctype
->bit_size
, new, true, false);
1012 static int show_statement_expr(struct expression
*expr
)
1014 return show_statement(expr
->statement
);
1017 static int show_position_expr(struct expression
*expr
, struct symbol
*base
)
1019 int new = show_expression(expr
->init_expr
);
1020 struct symbol
*ctype
= expr
->init_expr
->ctype
;
1023 bit_offset
= ctype
? ctype
->bit_offset
: -1;
1025 printf("\tinsert v%d at [%d:%d] of %s\n", new,
1026 expr
->init_offset
, bit_offset
,
1027 show_ident(base
->ident
));
1031 static int show_initializer_expr(struct expression
*expr
, struct symbol
*ctype
)
1033 struct expression
*entry
;
1035 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1038 // Nested initializers have their positions already
1039 // recursively calculated - just output them too
1040 if (entry
->type
== EXPR_INITIALIZER
) {
1041 show_initializer_expr(entry
, ctype
);
1045 // Initializer indexes and identifiers should
1046 // have been evaluated to EXPR_POS
1047 if (entry
->type
== EXPR_IDENTIFIER
) {
1048 printf(" AT '%s':\n", show_ident(entry
->expr_ident
));
1049 entry
= entry
->ident_expression
;
1053 if (entry
->type
== EXPR_INDEX
) {
1054 printf(" AT '%d..%d:\n", entry
->idx_from
, entry
->idx_to
);
1055 entry
= entry
->idx_expression
;
1058 if (entry
->type
== EXPR_POS
) {
1059 show_position_expr(entry
, ctype
);
1062 show_initialization(ctype
, entry
);
1063 } END_FOR_EACH_PTR(entry
);
1067 int show_symbol_expr_init(struct symbol
*sym
)
1069 struct expression
*expr
= sym
->initializer
;
1072 show_expression(expr
);
1073 return show_symbol_expr(sym
);
1077 * Print out an expression. Return the pseudo that contains the
1080 int show_expression(struct expression
*expr
)
1086 struct position
*pos
= &expr
->pos
;
1087 printf("\tno type at %s:%d:%d\n",
1088 stream_name(pos
->stream
),
1089 pos
->line
, pos
->pos
);
1093 switch (expr
->type
) {
1095 return show_call_expression(expr
);
1097 case EXPR_ASSIGNMENT
:
1098 return show_assignment(expr
);
1101 return show_comma(expr
);
1105 return show_binop(expr
);
1107 return show_preop(expr
);
1109 return show_postop(expr
);
1111 return show_symbol_expr(expr
->symbol
);
1114 case EXPR_PTRSIZEOF
:
1117 warning(expr
->pos
, "invalid expression after evaluation");
1120 case EXPR_FORCE_CAST
:
1121 case EXPR_IMPLIED_CAST
:
1122 return show_cast_expr(expr
);
1124 return show_value(expr
);
1126 return show_fvalue(expr
);
1128 return show_string_expr(expr
);
1129 case EXPR_INITIALIZER
:
1130 return show_initializer_expr(expr
, expr
->ctype
);
1132 case EXPR_CONDITIONAL
:
1133 return show_conditional_expr(expr
);
1134 case EXPR_STATEMENT
:
1135 return show_statement_expr(expr
);
1137 return show_label_expr(expr
);
1139 return show_slice(expr
);
1141 // None of these should exist as direct expressions: they are only
1142 // valid as sub-expressions of initializers.
1144 warning(expr
->pos
, "unable to show plain initializer position expression");
1146 case EXPR_IDENTIFIER
:
1147 warning(expr
->pos
, "unable to show identifier expression");
1150 warning(expr
->pos
, "unable to show index expression");
1153 warning(expr
->pos
, "unable to show type expression");