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]"},
134 {MOD_PURE
, "[pure]"},
137 for (i
= 0; i
< ARRAY_SIZE(mod_names
); i
++) {
141 const char *name
= m
->name
;
142 while ((c
= *name
++) != '\0' && len
+ 2 < sizeof buffer
)
151 static void show_struct_member(struct symbol
*sym
)
153 printf("\t%s:%d:%ld at offset %ld.%d", show_ident(sym
->ident
), sym
->bit_size
, sym
->ctype
.alignment
, sym
->offset
, sym
->bit_offset
);
157 void show_symbol_list(struct symbol_list
*list
, const char *sep
)
160 const char *prepend
= "";
162 FOR_EACH_PTR(list
, sym
) {
166 } END_FOR_EACH_PTR(sym
);
174 static void FORMAT_ATTR(2) prepend(struct type_name
*name
, const char *fmt
, ...)
176 static char buffer
[512];
181 n
= vsprintf(buffer
, fmt
, args
);
185 memcpy(name
->start
, buffer
, n
);
188 static void FORMAT_ATTR(2) append(struct type_name
*name
, const char *fmt
, ...)
190 static char buffer
[512];
195 n
= vsprintf(buffer
, fmt
, args
);
198 memcpy(name
->end
, buffer
, n
);
202 static struct ctype_name
{
206 { & char_ctype
, "char" },
207 { &schar_ctype
, "signed char" },
208 { &uchar_ctype
, "unsigned char" },
209 { & short_ctype
, "short" },
210 { &sshort_ctype
, "signed short" },
211 { &ushort_ctype
, "unsigned short" },
212 { & int_ctype
, "int" },
213 { &sint_ctype
, "signed int" },
214 { &uint_ctype
, "unsigned int" },
215 { &slong_ctype
, "signed long" },
216 { & long_ctype
, "long" },
217 { &ulong_ctype
, "unsigned long" },
218 { & llong_ctype
, "long long" },
219 { &sllong_ctype
, "signed long long" },
220 { &ullong_ctype
, "unsigned long long" },
221 { & lllong_ctype
, "long long long" },
222 { &slllong_ctype
, "signed long long long" },
223 { &ulllong_ctype
, "unsigned long long long" },
225 { &void_ctype
, "void" },
226 { &bool_ctype
, "bool" },
227 { &string_ctype
, "string" },
229 { &float_ctype
, "float" },
230 { &double_ctype
, "double" },
231 { &ldouble_ctype
,"long double" },
232 { &incomplete_ctype
, "incomplete type" },
233 { &int_type
, "abstract int" },
234 { &fp_type
, "abstract fp" },
235 { &label_ctype
, "label type" },
236 { &bad_ctype
, "bad type" },
239 const char *builtin_typename(struct symbol
*sym
)
243 for (i
= 0; i
< ARRAY_SIZE(typenames
); i
++)
244 if (typenames
[i
].sym
== sym
)
245 return typenames
[i
].name
;
249 const char *builtin_ctypename(struct ctype
*ctype
)
253 for (i
= 0; i
< ARRAY_SIZE(typenames
); i
++)
254 if (&typenames
[i
].sym
->ctype
== ctype
)
255 return typenames
[i
].name
;
259 static void do_show_type(struct symbol
*sym
, struct type_name
*name
)
261 const char *typename
;
262 unsigned long mod
= 0;
269 if (!sym
|| (sym
->type
!= SYM_NODE
&& sym
->type
!= SYM_ARRAY
&&
270 sym
->type
!= SYM_BITFIELD
)) {
275 prepend(name
, "<asn:%d>", as
);
277 s
= modifier_string(mod
);
280 memcpy(name
->start
, s
, len
);
288 if ((typename
= builtin_typename(sym
))) {
289 int len
= strlen(typename
);
290 if (name
->start
!= name
->end
)
291 *--name
->start
= ' ';
293 memcpy(name
->start
, typename
, len
);
301 mod
= sym
->ctype
.modifiers
;
312 append(name
, "( ... )");
316 if (name
->start
!= name
->end
)
317 *--name
->start
= ' ';
318 prepend(name
, "struct %s", show_ident(sym
->ident
));
322 if (name
->start
!= name
->end
)
323 *--name
->start
= ' ';
324 prepend(name
, "union %s", show_ident(sym
->ident
));
328 prepend(name
, "enum %s ", show_ident(sym
->ident
));
332 append(name
, "%s", show_ident(sym
->ident
));
333 mod
|= sym
->ctype
.modifiers
;
338 mod
|= sym
->ctype
.modifiers
;
340 append(name
, ":%d", sym
->bit_size
);
344 append(name
, "label(%s:%p)", show_ident(sym
->ident
), sym
);
348 mod
|= sym
->ctype
.modifiers
;
355 append(name
, "[%lld]", get_expression_value(sym
->array_size
));
363 if (name
->start
!= name
->end
)
364 *--name
->start
= ' ';
365 prepend(name
, "restricted %s", show_ident(sym
->ident
));
373 if (name
->start
!= name
->end
)
374 *--name
->start
= ' ';
375 prepend(name
, "unknown type %d", sym
->type
);
379 sym
= sym
->ctype
.base_type
;
384 prepend(name
, "restricted ");
386 prepend(name
, "fouled ");
389 void show_type(struct symbol
*sym
)
392 struct type_name name
;
394 name
.start
= name
.end
= array
+100;
395 do_show_type(sym
, &name
);
397 printf("%s", name
.start
);
400 const char *show_typename(struct symbol
*sym
)
402 static char array
[200];
403 struct type_name name
;
405 name
.start
= name
.end
= array
+100;
406 do_show_type(sym
, &name
);
411 void show_symbol(struct symbol
*sym
)
418 if (sym
->ctype
.alignment
)
419 printf(".align %ld\n", sym
->ctype
.alignment
);
422 type
= sym
->ctype
.base_type
;
429 * Show actual implementation information
431 switch (type
->type
) {
432 struct symbol
*member
;
437 FOR_EACH_PTR(type
->symbol_list
, member
) {
438 show_struct_member(member
);
439 } END_FOR_EACH_PTR(member
);
444 struct statement
*stmt
= type
->stmt
;
448 val
= show_statement(stmt
);
450 printf("\tmov.%d\t\tretval,%d\n", stmt
->ret
->bit_size
, val
);
461 if (sym
->initializer
) {
463 show_expression(sym
->initializer
);
467 static int show_symbol_init(struct symbol
*sym
);
469 static int new_pseudo(void)
475 static int new_label(void)
477 static int label
= 0;
481 static void show_switch_statement(struct statement
*stmt
)
483 int val
= show_expression(stmt
->switch_expression
);
485 printf("\tswitch v%d\n", val
);
488 * Debugging only: Check that the case list is correct
489 * by printing it out.
491 * This is where a _real_ back-end would go through the
492 * cases to decide whether to use a lookup table or a
493 * series of comparisons etc
495 printf("# case table:\n");
496 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
497 struct statement
*case_stmt
= sym
->stmt
;
498 struct expression
*expr
= case_stmt
->case_expression
;
499 struct expression
*to
= case_stmt
->case_to
;
504 if (expr
->type
== EXPR_VALUE
) {
505 printf(" case %lld", expr
->value
);
507 if (to
->type
== EXPR_VALUE
) {
508 printf(" .. %lld", to
->value
);
516 printf(": .L%p\n", sym
->bb_target
);
517 } END_FOR_EACH_PTR(sym
);
518 printf("# end case table\n");
520 show_statement(stmt
->switch_statement
);
522 if (stmt
->switch_break
->used
)
523 printf(".L%p:\n", stmt
->switch_break
->bb_target
);
526 static void show_symbol_decl(struct symbol_list
*syms
)
529 FOR_EACH_PTR(syms
, sym
) {
530 show_symbol_init(sym
);
531 } END_FOR_EACH_PTR(sym
);
534 static int show_return_stmt(struct statement
*stmt
);
537 * Print out a statement
539 int show_statement(struct statement
*stmt
)
543 switch (stmt
->type
) {
544 case STMT_DECLARATION
:
545 show_symbol_decl(stmt
->declaration
);
548 return show_return_stmt(stmt
);
549 case STMT_COMPOUND
: {
553 if (stmt
->inline_fn
) {
554 show_statement(stmt
->args
);
555 printf("\tbegin_inline \t%s\n", show_ident(stmt
->inline_fn
->ident
));
557 FOR_EACH_PTR(stmt
->stmts
, s
) {
558 last
= show_statement(s
);
559 } END_FOR_EACH_PTR(s
);
562 printf(".L%p:\n", stmt
->ret
);
563 addr
= show_symbol_expr(stmt
->ret
);
564 bits
= stmt
->ret
->bit_size
;
566 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, last
, addr
);
569 printf("\tend_inlined\t%s\n", show_ident(stmt
->inline_fn
->ident
));
573 case STMT_EXPRESSION
:
574 return show_expression(stmt
->expression
);
577 struct expression
*cond
= stmt
->if_conditional
;
579 /* This is only valid if nobody can jump into the "dead" statement */
581 if (cond
->type
== EXPR_VALUE
) {
582 struct statement
*s
= stmt
->if_true
;
589 val
= show_expression(cond
);
590 target
= new_label();
591 printf("\tje\t\tv%d,.L%d\n", val
, target
);
592 show_statement(stmt
->if_true
);
593 if (stmt
->if_false
) {
594 int last
= new_label();
595 printf("\tjmp\t\t.L%d\n", last
);
596 printf(".L%d:\n", target
);
598 show_statement(stmt
->if_false
);
600 printf(".L%d:\n", target
);
604 show_switch_statement(stmt
);
608 printf(".L%p:\n", stmt
->case_label
);
609 show_statement(stmt
->case_statement
);
612 case STMT_ITERATOR
: {
613 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
614 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
615 struct statement
*statement
= stmt
->iterator_statement
;
616 struct statement
*post_statement
= stmt
->iterator_post_statement
;
617 struct expression
*post_condition
= stmt
->iterator_post_condition
;
618 int val
, loop_top
= 0, loop_bottom
= 0;
620 show_symbol_decl(stmt
->iterator_syms
);
621 show_statement(pre_statement
);
623 if (pre_condition
->type
== EXPR_VALUE
) {
624 if (!pre_condition
->value
) {
625 loop_bottom
= new_label();
626 printf("\tjmp\t\t.L%d\n", loop_bottom
);
629 loop_bottom
= new_label();
630 val
= show_expression(pre_condition
);
631 printf("\tje\t\tv%d, .L%d\n", val
, loop_bottom
);
634 if (!post_condition
|| post_condition
->type
!= EXPR_VALUE
|| post_condition
->value
) {
635 loop_top
= new_label();
636 printf(".L%d:\n", loop_top
);
638 show_statement(statement
);
639 if (stmt
->iterator_continue
->used
)
640 printf(".L%p:\n", stmt
->iterator_continue
);
641 show_statement(post_statement
);
642 if (!post_condition
) {
643 printf("\tjmp\t\t.L%d\n", loop_top
);
644 } else if (post_condition
->type
== EXPR_VALUE
) {
645 if (post_condition
->value
)
646 printf("\tjmp\t\t.L%d\n", loop_top
);
648 val
= show_expression(post_condition
);
649 printf("\tjne\t\tv%d, .L%d\n", val
, loop_top
);
651 if (stmt
->iterator_break
->used
)
652 printf(".L%p:\n", stmt
->iterator_break
);
654 printf(".L%d:\n", loop_bottom
);
661 printf(".L%p:\n", stmt
->label_identifier
);
662 show_statement(stmt
->label_statement
);
666 if (stmt
->goto_expression
) {
667 int val
= show_expression(stmt
->goto_expression
);
668 printf("\tgoto\t\t*v%d\n", val
);
670 printf("\tgoto\t\t.L%p\n", stmt
->goto_label
->bb_target
);
674 printf("\tasm( .... )\n");
677 int val
= show_expression(stmt
->expression
);
678 printf("\tcontext( %d )\n", val
);
682 int val
= show_expression(stmt
->range_expression
);
683 int low
= show_expression(stmt
->range_low
);
684 int high
= show_expression(stmt
->range_high
);
685 printf("\trange( %d %d-%d)\n", val
, low
, high
);
692 static int show_call_expression(struct expression
*expr
)
694 struct symbol
*direct
;
695 struct expression
*arg
, *fn
;
700 warning(expr
->pos
, "\tcall with no type!");
705 FOR_EACH_PTR_REVERSE(expr
->args
, arg
) {
706 int new = show_expression(arg
);
707 int size
= arg
->ctype
->bit_size
;
708 printf("\tpush.%d\t\tv%d\n", size
, new);
709 framesize
+= bits_to_bytes(size
);
710 } END_FOR_EACH_PTR_REVERSE(arg
);
714 /* Remove dereference, if any */
716 if (fn
->type
== EXPR_PREOP
) {
717 if (fn
->unop
->type
== EXPR_SYMBOL
) {
718 struct symbol
*sym
= fn
->unop
->symbol
;
719 if (sym
->ctype
.base_type
->type
== SYM_FN
)
724 printf("\tcall\t\t%s\n", show_ident(direct
->ident
));
726 fncall
= show_expression(fn
);
727 printf("\tcall\t\t*v%d\n", fncall
);
730 printf("\tadd.%d\t\tvSP,vSP,$%d\n", bits_in_pointer
, framesize
);
732 retval
= new_pseudo();
733 printf("\tmov.%d\t\tv%d,retval\n", expr
->ctype
->bit_size
, retval
);
737 static int show_comma(struct expression
*expr
)
739 show_expression(expr
->left
);
740 return show_expression(expr
->right
);
743 static int show_binop(struct expression
*expr
)
745 int left
= show_expression(expr
->left
);
746 int right
= show_expression(expr
->right
);
747 int new = new_pseudo();
749 static const char *name
[] = {
750 ['+'] = "add", ['-'] = "sub",
751 ['*'] = "mul", ['/'] = "div",
752 ['%'] = "mod", ['&'] = "and",
753 ['|'] = "lor", ['^'] = "xor"
755 unsigned int op
= expr
->op
;
757 opname
= show_special(op
);
758 if (op
< ARRAY_SIZE(name
))
760 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname
,
761 expr
->ctype
->bit_size
,
766 static int show_slice(struct expression
*expr
)
768 int target
= show_expression(expr
->base
);
769 int new = new_pseudo();
770 printf("\tslice.%d\t\tv%d,v%d,%d\n", expr
->r_nrbits
, target
, new, expr
->r_bitpos
);
774 static int show_regular_preop(struct expression
*expr
)
776 int target
= show_expression(expr
->unop
);
777 int new = new_pseudo();
778 static const char *name
[] = {
779 ['!'] = "nonzero", ['-'] = "neg",
782 unsigned int op
= expr
->op
;
785 opname
= show_special(op
);
786 if (op
< ARRAY_SIZE(name
))
788 printf("\t%s.%d\t\tv%d,v%d\n", opname
, expr
->ctype
->bit_size
, new, target
);
793 * FIXME! Not all accesses are memory loads. We should
794 * check what kind of symbol is behind the dereference.
796 static int show_address_gen(struct expression
*expr
)
798 return show_expression(expr
->unop
);
801 static int show_load_gen(int bits
, struct expression
*expr
, int addr
)
803 int new = new_pseudo();
805 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, new, addr
);
809 static void show_store_gen(int bits
, int value
, struct expression
*expr
, int addr
)
811 /* FIXME!!! Bitfield store! */
812 printf("\tst.%d\t\tv%d,[v%d]\n", bits
, value
, addr
);
815 static int show_assignment(struct expression
*expr
)
817 struct expression
*target
= expr
->left
;
823 bits
= expr
->ctype
->bit_size
;
824 val
= show_expression(expr
->right
);
825 addr
= show_address_gen(target
);
826 show_store_gen(bits
, val
, target
, addr
);
830 static int show_return_stmt(struct statement
*stmt
)
832 struct expression
*expr
= stmt
->ret_value
;
833 struct symbol
*target
= stmt
->ret_target
;
835 if (expr
&& expr
->ctype
) {
836 int val
= show_expression(expr
);
837 int bits
= expr
->ctype
->bit_size
;
838 int addr
= show_symbol_expr(target
);
839 show_store_gen(bits
, val
, NULL
, addr
);
841 printf("\tret\t\t(%p)\n", target
);
845 static int show_initialization(struct symbol
*sym
, struct expression
*expr
)
852 bits
= expr
->ctype
->bit_size
;
853 val
= show_expression(expr
);
854 addr
= show_symbol_expr(sym
);
855 // FIXME! The "target" expression is for bitfield store information.
856 // Leave it NULL, which works fine.
857 show_store_gen(bits
, val
, NULL
, addr
);
861 static int show_access(struct expression
*expr
)
863 int addr
= show_address_gen(expr
);
864 return show_load_gen(expr
->ctype
->bit_size
, expr
, addr
);
867 static int show_inc_dec(struct expression
*expr
, int postop
)
869 int addr
= show_address_gen(expr
->unop
);
871 const char *opname
= expr
->op
== SPECIAL_INCREMENT
? "add" : "sub";
872 int bits
= expr
->ctype
->bit_size
;
874 retval
= show_load_gen(bits
, expr
->unop
, addr
);
878 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname
, bits
, new, retval
);
879 show_store_gen(bits
, new, expr
->unop
, addr
);
883 static int show_preop(struct expression
*expr
)
886 * '*' is an lvalue access, and is fundamentally different
887 * from an arithmetic operation. Maybe it should have an
888 * expression type of its own..
891 return show_access(expr
);
892 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
893 return show_inc_dec(expr
, 0);
894 return show_regular_preop(expr
);
897 static int show_postop(struct expression
*expr
)
899 return show_inc_dec(expr
, 1);
902 static int show_symbol_expr(struct symbol
*sym
)
904 int new = new_pseudo();
906 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_STRING
)
907 return show_string_expr(sym
->initializer
);
909 if (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_EXTERN
| MOD_STATIC
)) {
910 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer
, new, show_ident(sym
->ident
));
913 if (sym
->ctype
.modifiers
& MOD_ADDRESSABLE
) {
914 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer
, new, sym
->value
);
917 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer
, new, show_ident(sym
->ident
), sym
);
921 static int show_symbol_init(struct symbol
*sym
)
923 struct expression
*expr
= sym
->initializer
;
928 bits
= expr
->ctype
->bit_size
;
929 val
= show_expression(expr
);
930 addr
= show_symbol_expr(sym
);
931 show_store_gen(bits
, val
, NULL
, addr
);
936 static int type_is_signed(struct symbol
*sym
)
938 if (sym
->type
== SYM_NODE
)
939 sym
= sym
->ctype
.base_type
;
940 if (sym
->type
== SYM_PTR
)
942 return !(sym
->ctype
.modifiers
& MOD_UNSIGNED
);
945 static int show_cast_expr(struct expression
*expr
)
947 struct symbol
*old_type
, *new_type
;
948 int op
= show_expression(expr
->cast_expression
);
949 int oldbits
, newbits
;
952 old_type
= expr
->cast_expression
->ctype
;
953 new_type
= expr
->cast_type
;
955 oldbits
= old_type
->bit_size
;
956 newbits
= new_type
->bit_size
;
957 if (oldbits
>= newbits
)
960 is_signed
= type_is_signed(old_type
);
962 printf("\tsext%d.%d\tv%d,v%d\n", oldbits
, newbits
, new, op
);
964 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits
, new, op
, (1UL << oldbits
)-1);
969 static int show_value(struct expression
*expr
)
971 int new = new_pseudo();
972 unsigned long long value
= expr
->value
;
974 printf("\tmovi.%d\t\tv%d,$%llu\n", expr
->ctype
->bit_size
, new, value
);
978 static int show_fvalue(struct expression
*expr
)
980 int new = new_pseudo();
981 long double value
= expr
->fvalue
;
983 printf("\tmovf.%d\t\tv%d,$%Lf\n", expr
->ctype
->bit_size
, new, value
);
987 static int show_string_expr(struct expression
*expr
)
989 int new = new_pseudo();
991 printf("\tmovi.%d\t\tv%d,&%s\n", bits_in_pointer
, new, show_string(expr
->string
));
995 static int show_label_expr(struct expression
*expr
)
997 int new = new_pseudo();
998 printf("\tmovi.%d\t\tv%d,.L%p\n",bits_in_pointer
, new, expr
->label_symbol
);
1002 static int show_conditional_expr(struct expression
*expr
)
1004 int cond
= show_expression(expr
->conditional
);
1005 int true = show_expression(expr
->cond_true
);
1006 int false = show_expression(expr
->cond_false
);
1007 int new = new_pseudo();
1009 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond
, expr
->ctype
->bit_size
, new, true, false);
1013 static int show_statement_expr(struct expression
*expr
)
1015 return show_statement(expr
->statement
);
1018 static int show_position_expr(struct expression
*expr
, struct symbol
*base
)
1020 int new = show_expression(expr
->init_expr
);
1021 struct symbol
*ctype
= expr
->init_expr
->ctype
;
1024 bit_offset
= ctype
? ctype
->bit_offset
: -1;
1026 printf("\tinsert v%d at [%d:%d] of %s\n", new,
1027 expr
->init_offset
, bit_offset
,
1028 show_ident(base
->ident
));
1032 static int show_initializer_expr(struct expression
*expr
, struct symbol
*ctype
)
1034 struct expression
*entry
;
1036 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1039 // Nested initializers have their positions already
1040 // recursively calculated - just output them too
1041 if (entry
->type
== EXPR_INITIALIZER
) {
1042 show_initializer_expr(entry
, ctype
);
1046 // Initializer indexes and identifiers should
1047 // have been evaluated to EXPR_POS
1048 if (entry
->type
== EXPR_IDENTIFIER
) {
1049 printf(" AT '%s':\n", show_ident(entry
->expr_ident
));
1050 entry
= entry
->ident_expression
;
1054 if (entry
->type
== EXPR_INDEX
) {
1055 printf(" AT '%d..%d:\n", entry
->idx_from
, entry
->idx_to
);
1056 entry
= entry
->idx_expression
;
1059 if (entry
->type
== EXPR_POS
) {
1060 show_position_expr(entry
, ctype
);
1063 show_initialization(ctype
, entry
);
1064 } END_FOR_EACH_PTR(entry
);
1068 int show_symbol_expr_init(struct symbol
*sym
)
1070 struct expression
*expr
= sym
->initializer
;
1073 show_expression(expr
);
1074 return show_symbol_expr(sym
);
1078 * Print out an expression. Return the pseudo that contains the
1081 int show_expression(struct expression
*expr
)
1087 struct position
*pos
= &expr
->pos
;
1088 printf("\tno type at %s:%d:%d\n",
1089 stream_name(pos
->stream
),
1090 pos
->line
, pos
->pos
);
1094 switch (expr
->type
) {
1096 return show_call_expression(expr
);
1098 case EXPR_ASSIGNMENT
:
1099 return show_assignment(expr
);
1102 return show_comma(expr
);
1106 return show_binop(expr
);
1108 return show_preop(expr
);
1110 return show_postop(expr
);
1112 return show_symbol_expr(expr
->symbol
);
1115 case EXPR_PTRSIZEOF
:
1118 warning(expr
->pos
, "invalid expression after evaluation");
1121 case EXPR_FORCE_CAST
:
1122 case EXPR_IMPLIED_CAST
:
1123 return show_cast_expr(expr
);
1125 return show_value(expr
);
1127 return show_fvalue(expr
);
1129 return show_string_expr(expr
);
1130 case EXPR_INITIALIZER
:
1131 return show_initializer_expr(expr
, expr
->ctype
);
1133 case EXPR_CONDITIONAL
:
1134 return show_conditional_expr(expr
);
1135 case EXPR_STATEMENT
:
1136 return show_statement_expr(expr
);
1138 return show_label_expr(expr
);
1140 return show_slice(expr
);
1142 // None of these should exist as direct expressions: they are only
1143 // valid as sub-expressions of initializers.
1145 warning(expr
->pos
, "unable to show plain initializer position expression");
1147 case EXPR_IDENTIFIER
:
1148 warning(expr
->pos
, "unable to show identifier expression");
1151 warning(expr
->pos
, "unable to show index expression");
1154 warning(expr
->pos
, "unable to show type expression");