4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * Print out results of parsing for debugging and testing.
41 #include "expression.h"
44 static int show_symbol_expr(struct symbol
*sym
);
45 static int show_string_expr(struct expression
*expr
);
47 static void do_debug_symbol(struct symbol
*sym
, int indent
)
49 static const char indent_string
[] = " ";
50 static const char *typestr
[] = {
51 [SYM_UNINITIALIZED
] = "none",
52 [SYM_PREPROCESSOR
] = "cpp.",
53 [SYM_BASETYPE
] = "base",
58 [SYM_STRUCT
] = "strt",
61 [SYM_TYPEOF
] = "tpof",
62 [SYM_BITFIELD
] = "bitf",
64 [SYM_RESTRICT
] = "rstr",
65 [SYM_FOULED
] = "foul",
68 struct context
*context
;
73 fprintf(stderr
, "%.*s%s%3d:%lu %s %s (as: %s) %p (%s:%d:%d) %s\n",
74 indent
, indent_string
, typestr
[sym
->type
],
75 sym
->bit_size
, sym
->ctype
.alignment
,
76 modifier_string(sym
->ctype
.modifiers
), show_ident(sym
->ident
),
77 show_as(sym
->ctype
.as
),
78 sym
, stream_name(sym
->pos
.stream
), sym
->pos
.line
, sym
->pos
.pos
,
79 builtin_typename(sym
) ?: "");
81 FOR_EACH_PTR(sym
->ctype
.contexts
, context
) {
82 /* FIXME: should print context expression */
83 fprintf(stderr
, "< context%d: in=%d, out=%d\n",
84 i
, context
->in
, context
->out
);
85 fprintf(stderr
, " end context%d >\n", i
);
87 } END_FOR_EACH_PTR(context
);
88 if (sym
->type
== SYM_FN
) {
91 FOR_EACH_PTR(sym
->arguments
, arg
) {
92 fprintf(stderr
, "< arg%d:\n", i
);
93 do_debug_symbol(arg
, 0);
94 fprintf(stderr
, " end arg%d >\n", i
);
96 } END_FOR_EACH_PTR(arg
);
98 do_debug_symbol(sym
->ctype
.base_type
, indent
+2);
101 void debug_symbol(struct symbol
*sym
)
103 do_debug_symbol(sym
, 0);
107 * Symbol type printout. The type system is by far the most
108 * complicated part of C - everything else is trivial.
110 const char *modifier_string(unsigned long mod
)
112 static char buffer
[100];
120 static struct mod_name mod_names
[] = {
122 {MOD_EXTERN
, "extern"},
123 {MOD_REGISTER
, "register"},
124 {MOD_STATIC
, "static"},
125 {MOD_INLINE
, "inline"},
126 {MOD_CONST
, "const"},
127 {MOD_RESTRICT
, "restrict"},
128 {MOD_VOLATILE
, "volatile"},
129 {MOD_ADDRESSABLE
, "[addressable]"},
130 {MOD_ASSIGNED
, "[assigned]"},
131 {MOD_ATOMIC
, "[atomic]"},
132 {MOD_BITWISE
, "[bitwise]"},
133 {MOD_EXPLICITLY_SIGNED
, "[explicitly-signed]"},
134 {MOD_GNU_INLINE
, "[gnu_inline]"},
135 {MOD_NOCAST
, "[nocast]"},
136 {MOD_NODEREF
, "[noderef]"},
137 {MOD_NORETURN
, "[noreturn]"},
138 {MOD_PURE
, "[pure]"},
139 {MOD_SAFE
, "[safe]"},
140 {MOD_SIGNED
, "[signed]"},
142 {MOD_TOPLEVEL
, "[toplevel]"},
143 {MOD_UNSIGNED
, "[unsigned]"},
144 {MOD_UNUSED
, "[unused]"},
145 {MOD_USERTYPE
, "[usertype]"},
148 for (i
= 0; i
< ARRAY_SIZE(mod_names
); i
++) {
152 const char *name
= m
->name
;
153 while ((c
= *name
++) != '\0' && len
+ 2 < sizeof buffer
)
162 static void show_struct_member(struct symbol
*sym
)
164 printf("\t%s:%d:%ld at offset %ld.%d", show_ident(sym
->ident
), sym
->bit_size
, sym
->ctype
.alignment
, sym
->offset
, sym
->bit_offset
);
168 void show_symbol_list(struct symbol_list
*list
, const char *sep
)
171 const char *prepend
= "";
173 FOR_EACH_PTR(list
, sym
) {
177 } END_FOR_EACH_PTR(sym
);
180 const char *show_as(struct ident
*as
)
184 return show_ident(as
);
192 static void FORMAT_ATTR(2) prepend(struct type_name
*name
, const char *fmt
, ...)
194 static char buffer
[512];
199 n
= vsprintf(buffer
, fmt
, args
);
203 memcpy(name
->start
, buffer
, n
);
206 static void FORMAT_ATTR(2) append(struct type_name
*name
, const char *fmt
, ...)
208 static char buffer
[512];
213 n
= vsprintf(buffer
, fmt
, args
);
216 memcpy(name
->end
, buffer
, n
);
220 static struct ctype_name
{
225 { & char_ctype
, "char", "" },
226 { &schar_ctype
, "signed char", "" },
227 { &uchar_ctype
, "unsigned char", "" },
228 { & short_ctype
, "short", "" },
229 { &sshort_ctype
, "signed short", "" },
230 { &ushort_ctype
, "unsigned short", "" },
231 { & int_ctype
, "int", "" },
232 { &sint_ctype
, "signed int", "" },
233 { &uint_ctype
, "unsigned int", "U" },
234 { & long_ctype
, "long", "L" },
235 { &slong_ctype
, "signed long", "L" },
236 { &ulong_ctype
, "unsigned long", "UL" },
237 { & llong_ctype
, "long long", "LL" },
238 { &sllong_ctype
, "signed long long", "LL" },
239 { &ullong_ctype
, "unsigned long long", "ULL" },
240 { & int128_ctype
, "__int128", "" },
241 { &sint128_ctype
, "signed __int128", "" },
242 { &uint128_ctype
, "unsigned __int128", "" },
244 { &void_ctype
, "void", "" },
245 { &bool_ctype
, "bool", "" },
247 { &float_ctype
, "float", "F" },
248 { &double_ctype
, "double", "" },
249 { &ldouble_ctype
,"long double", "L" },
250 { &incomplete_ctype
, "incomplete type", "" },
251 { &int_type
, "abstract int", "" },
252 { &fp_type
, "abstract fp", "" },
253 { &label_ctype
, "label type", "" },
254 { &bad_ctype
, "bad type", "" },
257 const char *builtin_typename(struct symbol
*sym
)
261 for (i
= 0; i
< ARRAY_SIZE(typenames
); i
++)
262 if (typenames
[i
].sym
== sym
)
263 return typenames
[i
].name
;
267 const char *builtin_type_suffix(struct symbol
*sym
)
271 for (i
= 0; i
< ARRAY_SIZE(typenames
); i
++)
272 if (typenames
[i
].sym
== sym
)
273 return typenames
[i
].suffix
;
277 const char *builtin_ctypename(struct ctype
*ctype
)
281 for (i
= 0; i
< ARRAY_SIZE(typenames
); i
++)
282 if (&typenames
[i
].sym
->ctype
== ctype
)
283 return typenames
[i
].name
;
287 static void do_show_type(struct symbol
*sym
, struct type_name
*name
)
289 const char *typename
;
290 unsigned long mod
= 0;
291 struct ident
*as
= NULL
;
297 if (sym
&& (sym
->type
!= SYM_NODE
&& sym
->type
!= SYM_ARRAY
&&
298 sym
->type
!= SYM_BITFIELD
)) {
303 prepend(name
, "%s ", show_as(as
));
305 if (sym
&& (sym
->type
== SYM_BASETYPE
|| sym
->type
== SYM_ENUM
))
306 mod
&= ~MOD_SPECIFIER
;
307 s
= modifier_string(mod
);
310 memcpy(name
->start
, s
, len
);
318 if ((typename
= builtin_typename(sym
))) {
319 int len
= strlen(typename
);
320 if (name
->start
!= name
->end
)
321 *--name
->start
= ' ';
323 memcpy(name
->start
, typename
, len
);
331 mod
= sym
->ctype
.modifiers
;
334 examine_pointer_target(sym
);
343 append(name
, "( ... )");
347 if (name
->start
!= name
->end
)
348 *--name
->start
= ' ';
349 prepend(name
, "struct %s", show_ident(sym
->ident
));
353 if (name
->start
!= name
->end
)
354 *--name
->start
= ' ';
355 prepend(name
, "union %s", show_ident(sym
->ident
));
359 prepend(name
, "enum %s ", show_ident(sym
->ident
));
364 append(name
, "%s", show_ident(sym
->ident
));
365 mod
|= sym
->ctype
.modifiers
;
366 combine_address_space(sym
->pos
, &as
, sym
->ctype
.as
);
370 mod
|= sym
->ctype
.modifiers
;
371 combine_address_space(sym
->pos
, &as
, sym
->ctype
.as
);
372 append(name
, ":%d", sym
->bit_size
);
376 append(name
, "label(%s:%p)", show_ident(sym
->ident
), sym
);
380 mod
|= sym
->ctype
.modifiers
;
381 combine_address_space(sym
->pos
, &as
, sym
->ctype
.as
);
387 append(name
, "[%lld]", get_expression_value(sym
->array_size
));
395 if (name
->start
!= name
->end
)
396 *--name
->start
= ' ';
397 prepend(name
, "restricted %s", show_ident(sym
->ident
));
405 if (name
->start
!= name
->end
)
406 *--name
->start
= ' ';
407 prepend(name
, "unknown type %d", sym
->type
);
411 sym
= sym
->ctype
.base_type
;
416 prepend(name
, "restricted ");
418 prepend(name
, "fouled ");
420 // strip trailing space
421 if (name
->end
> name
->start
&& name
->end
[-1] == ' ')
425 void show_type(struct symbol
*sym
)
428 struct type_name name
;
430 name
.start
= name
.end
= array
+100;
431 do_show_type(sym
, &name
);
433 printf("%s", name
.start
);
436 const char *show_typename(struct symbol
*sym
)
438 static char array
[200];
439 struct type_name name
;
441 name
.start
= name
.end
= array
+100;
442 do_show_type(sym
, &name
);
447 void show_symbol(struct symbol
*sym
)
454 if (sym
->ctype
.alignment
)
455 printf(".align %ld\n", sym
->ctype
.alignment
);
458 type
= sym
->ctype
.base_type
;
465 * Show actual implementation information
467 switch (type
->type
) {
468 struct symbol
*member
;
473 FOR_EACH_PTR(type
->symbol_list
, member
) {
474 show_struct_member(member
);
475 } END_FOR_EACH_PTR(member
);
480 struct statement
*stmt
= type
->stmt
;
484 val
= show_statement(stmt
);
486 printf("\tmov.%d\t\tretval,%d\n", stmt
->ret
->bit_size
, val
);
497 if (sym
->initializer
) {
499 show_expression(sym
->initializer
);
503 static int show_symbol_init(struct symbol
*sym
);
505 static int new_pseudo(void)
511 static int new_label(void)
513 static int label
= 0;
517 static void show_switch_statement(struct statement
*stmt
)
519 int val
= show_expression(stmt
->switch_expression
);
521 printf("\tswitch v%d\n", val
);
524 * Debugging only: Check that the case list is correct
525 * by printing it out.
527 * This is where a _real_ back-end would go through the
528 * cases to decide whether to use a lookup table or a
529 * series of comparisons etc
531 printf("# case table:\n");
532 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
533 struct statement
*case_stmt
= sym
->stmt
;
534 struct expression
*expr
= case_stmt
->case_expression
;
535 struct expression
*to
= case_stmt
->case_to
;
540 if (expr
->type
== EXPR_VALUE
) {
541 printf(" case %lld", expr
->value
);
543 if (to
->type
== EXPR_VALUE
) {
544 printf(" .. %lld", to
->value
);
552 printf(": .L%p\n", sym
);
553 } END_FOR_EACH_PTR(sym
);
554 printf("# end case table\n");
556 show_statement(stmt
->switch_statement
);
558 if (stmt
->switch_break
->used
)
559 printf(".L%p:\n", stmt
->switch_break
);
562 static void show_symbol_decl(struct symbol_list
*syms
)
565 FOR_EACH_PTR(syms
, sym
) {
566 show_symbol_init(sym
);
567 } END_FOR_EACH_PTR(sym
);
570 static int show_return_stmt(struct statement
*stmt
);
573 * Print out a statement
575 int show_statement(struct statement
*stmt
)
579 switch (stmt
->type
) {
580 case STMT_DECLARATION
:
581 show_symbol_decl(stmt
->declaration
);
584 return show_return_stmt(stmt
);
585 case STMT_COMPOUND
: {
589 if (stmt
->inline_fn
) {
590 show_statement(stmt
->args
);
591 printf("\tbegin_inline \t%s\n", show_ident(stmt
->inline_fn
->ident
));
593 FOR_EACH_PTR(stmt
->stmts
, s
) {
594 last
= show_statement(s
);
595 } END_FOR_EACH_PTR(s
);
598 printf(".L%p:\n", stmt
->ret
);
599 addr
= show_symbol_expr(stmt
->ret
);
600 bits
= stmt
->ret
->bit_size
;
602 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, last
, addr
);
605 printf("\tend_inlined\t%s\n", show_ident(stmt
->inline_fn
->ident
));
609 case STMT_EXPRESSION
:
610 return show_expression(stmt
->expression
);
613 struct expression
*cond
= stmt
->if_conditional
;
615 /* This is only valid if nobody can jump into the "dead" statement */
617 if (cond
->type
== EXPR_VALUE
) {
618 struct statement
*s
= stmt
->if_true
;
625 val
= show_expression(cond
);
626 target
= new_label();
627 printf("\tje\t\tv%d,.L%d\n", val
, target
);
628 show_statement(stmt
->if_true
);
629 if (stmt
->if_false
) {
630 int last
= new_label();
631 printf("\tjmp\t\t.L%d\n", last
);
632 printf(".L%d:\n", target
);
634 show_statement(stmt
->if_false
);
636 printf(".L%d:\n", target
);
640 show_switch_statement(stmt
);
644 printf(".L%p:\n", stmt
->case_label
);
645 show_statement(stmt
->case_statement
);
648 case STMT_ITERATOR
: {
649 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
650 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
651 struct statement
*statement
= stmt
->iterator_statement
;
652 struct statement
*post_statement
= stmt
->iterator_post_statement
;
653 struct expression
*post_condition
= stmt
->iterator_post_condition
;
654 int val
, loop_top
= 0, loop_bottom
= 0;
656 show_symbol_decl(stmt
->iterator_syms
);
657 show_statement(pre_statement
);
659 if (pre_condition
->type
== EXPR_VALUE
) {
660 if (!pre_condition
->value
) {
661 loop_bottom
= new_label();
662 printf("\tjmp\t\t.L%d\n", loop_bottom
);
665 loop_bottom
= new_label();
666 val
= show_expression(pre_condition
);
667 printf("\tje\t\tv%d, .L%d\n", val
, loop_bottom
);
670 if (!post_condition
|| post_condition
->type
!= EXPR_VALUE
|| post_condition
->value
) {
671 loop_top
= new_label();
672 printf(".L%d:\n", loop_top
);
674 show_statement(statement
);
675 if (stmt
->iterator_continue
->used
)
676 printf(".L%p:\n", stmt
->iterator_continue
);
677 show_statement(post_statement
);
678 if (!post_condition
) {
679 printf("\tjmp\t\t.L%d\n", loop_top
);
680 } else if (post_condition
->type
== EXPR_VALUE
) {
681 if (post_condition
->value
)
682 printf("\tjmp\t\t.L%d\n", loop_top
);
684 val
= show_expression(post_condition
);
685 printf("\tjne\t\tv%d, .L%d\n", val
, loop_top
);
687 if (stmt
->iterator_break
->used
)
688 printf(".L%p:\n", stmt
->iterator_break
);
690 printf(".L%d:\n", loop_bottom
);
697 printf(".L%p:\n", stmt
->label_identifier
);
698 show_statement(stmt
->label_statement
);
702 if (stmt
->goto_expression
) {
703 int val
= show_expression(stmt
->goto_expression
);
704 printf("\tgoto\t\t*v%d\n", val
);
706 printf("\tgoto\t\t.L%p\n", stmt
->goto_label
);
710 printf("\tasm( .... )\n");
713 int val
= show_expression(stmt
->expression
);
714 printf("\tcontext( %d )\n", val
);
718 int val
= show_expression(stmt
->range_expression
);
719 int low
= show_expression(stmt
->range_low
);
720 int high
= show_expression(stmt
->range_high
);
721 printf("\trange( %d %d-%d)\n", val
, low
, high
);
728 static int show_call_expression(struct expression
*expr
)
730 struct symbol
*direct
;
731 struct expression
*arg
, *fn
;
736 warning(expr
->pos
, "\tcall with no type!");
741 FOR_EACH_PTR_REVERSE(expr
->args
, arg
) {
742 int new = show_expression(arg
);
743 int size
= arg
->ctype
->bit_size
;
744 printf("\tpush.%d\t\tv%d\n", size
, new);
745 framesize
+= bits_to_bytes(size
);
746 } END_FOR_EACH_PTR_REVERSE(arg
);
750 /* Remove dereference, if any */
752 if (fn
->type
== EXPR_PREOP
) {
753 if (fn
->unop
->type
== EXPR_SYMBOL
) {
754 struct symbol
*sym
= fn
->unop
->symbol
;
755 if (sym
->ctype
.base_type
->type
== SYM_FN
)
760 printf("\tcall\t\t%s\n", show_ident(direct
->ident
));
762 fncall
= show_expression(fn
);
763 printf("\tcall\t\t*v%d\n", fncall
);
766 printf("\tadd.%d\t\tvSP,vSP,$%d\n", bits_in_pointer
, framesize
);
768 retval
= new_pseudo();
769 printf("\tmov.%d\t\tv%d,retval\n", expr
->ctype
->bit_size
, retval
);
773 static int show_comma(struct expression
*expr
)
775 show_expression(expr
->left
);
776 return show_expression(expr
->right
);
779 static int show_binop(struct expression
*expr
)
781 int left
= show_expression(expr
->left
);
782 int right
= show_expression(expr
->right
);
783 int new = new_pseudo();
785 static const char *name
[] = {
786 ['+'] = "add", ['-'] = "sub",
787 ['*'] = "mul", ['/'] = "div",
788 ['%'] = "mod", ['&'] = "and",
789 ['|'] = "lor", ['^'] = "xor"
791 unsigned int op
= expr
->op
;
793 opname
= show_special(op
);
794 if (op
< ARRAY_SIZE(name
))
796 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname
,
797 expr
->ctype
->bit_size
,
802 static int show_slice(struct expression
*expr
)
804 int target
= show_expression(expr
->base
);
805 int new = new_pseudo();
806 printf("\tslice.%d\t\tv%d,v%d,%d\n", expr
->r_nrbits
, target
, new, expr
->r_bitpos
);
810 static int show_regular_preop(struct expression
*expr
)
812 int target
= show_expression(expr
->unop
);
813 int new = new_pseudo();
814 static const char *name
[] = {
815 ['!'] = "nonzero", ['-'] = "neg",
818 unsigned int op
= expr
->op
;
821 opname
= show_special(op
);
822 if (op
< ARRAY_SIZE(name
))
824 printf("\t%s.%d\t\tv%d,v%d\n", opname
, expr
->ctype
->bit_size
, new, target
);
829 * FIXME! Not all accesses are memory loads. We should
830 * check what kind of symbol is behind the dereference.
832 static int show_address_gen(struct expression
*expr
)
834 return show_expression(expr
->unop
);
837 static int show_load_gen(int bits
, struct expression
*expr
, int addr
)
839 int new = new_pseudo();
841 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, new, addr
);
845 static void show_store_gen(int bits
, int value
, struct expression
*expr
, int addr
)
847 /* FIXME!!! Bitfield store! */
848 printf("\tst.%d\t\tv%d,[v%d]\n", bits
, value
, addr
);
851 static int show_assignment(struct expression
*expr
)
853 struct expression
*target
= expr
->left
;
859 bits
= expr
->ctype
->bit_size
;
860 val
= show_expression(expr
->right
);
861 addr
= show_address_gen(target
);
862 show_store_gen(bits
, val
, target
, addr
);
866 static int show_return_stmt(struct statement
*stmt
)
868 struct expression
*expr
= stmt
->ret_value
;
869 struct symbol
*target
= stmt
->ret_target
;
871 if (expr
&& expr
->ctype
) {
872 int val
= show_expression(expr
);
873 int bits
= expr
->ctype
->bit_size
;
874 int addr
= show_symbol_expr(target
);
875 show_store_gen(bits
, val
, NULL
, addr
);
877 printf("\tret\t\t(%p)\n", target
);
881 static int show_initialization(struct symbol
*sym
, struct expression
*expr
)
888 bits
= expr
->ctype
->bit_size
;
889 val
= show_expression(expr
);
890 addr
= show_symbol_expr(sym
);
891 // FIXME! The "target" expression is for bitfield store information.
892 // Leave it NULL, which works fine.
893 show_store_gen(bits
, val
, NULL
, addr
);
897 static int show_access(struct expression
*expr
)
899 int addr
= show_address_gen(expr
);
900 return show_load_gen(expr
->ctype
->bit_size
, expr
, addr
);
903 static int show_inc_dec(struct expression
*expr
, int postop
)
905 int addr
= show_address_gen(expr
->unop
);
907 const char *opname
= expr
->op
== SPECIAL_INCREMENT
? "add" : "sub";
908 int bits
= expr
->ctype
->bit_size
;
910 retval
= show_load_gen(bits
, expr
->unop
, addr
);
914 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname
, bits
, new, retval
);
915 show_store_gen(bits
, new, expr
->unop
, addr
);
919 static int show_preop(struct expression
*expr
)
922 * '*' is an lvalue access, and is fundamentally different
923 * from an arithmetic operation. Maybe it should have an
924 * expression type of its own..
927 return show_access(expr
);
928 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
929 return show_inc_dec(expr
, 0);
930 return show_regular_preop(expr
);
933 static int show_postop(struct expression
*expr
)
935 return show_inc_dec(expr
, 1);
938 static int show_symbol_expr(struct symbol
*sym
)
940 int new = new_pseudo();
942 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_STRING
)
943 return show_string_expr(sym
->initializer
);
945 if (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_EXTERN
| MOD_STATIC
)) {
946 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer
, new, show_ident(sym
->ident
));
949 if (sym
->ctype
.modifiers
& MOD_ADDRESSABLE
) {
950 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer
, new, 0LL);
953 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer
, new, show_ident(sym
->ident
), sym
);
957 static int show_symbol_init(struct symbol
*sym
)
959 struct expression
*expr
= sym
->initializer
;
964 bits
= expr
->ctype
->bit_size
;
965 val
= show_expression(expr
);
966 addr
= show_symbol_expr(sym
);
967 show_store_gen(bits
, val
, NULL
, addr
);
972 static int show_cast_expr(struct expression
*expr
)
974 struct symbol
*old_type
, *new_type
;
975 int op
= show_expression(expr
->cast_expression
);
976 int oldbits
, newbits
;
979 old_type
= expr
->cast_expression
->ctype
;
980 new_type
= expr
->cast_type
;
982 oldbits
= old_type
->bit_size
;
983 newbits
= new_type
->bit_size
;
984 if (oldbits
>= newbits
)
987 is_signed
= is_signed_type(old_type
);
989 printf("\tsext%d.%d\tv%d,v%d\n", oldbits
, newbits
, new, op
);
991 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits
, new, op
, (1UL << oldbits
)-1);
996 static int show_value(struct expression
*expr
)
998 int new = new_pseudo();
999 unsigned long long value
= expr
->value
;
1001 printf("\tmovi.%d\t\tv%d,$%llu\n", expr
->ctype
->bit_size
, new, value
);
1005 static int show_fvalue(struct expression
*expr
)
1007 int new = new_pseudo();
1008 long double value
= expr
->fvalue
;
1010 printf("\tmovf.%d\t\tv%d,$%Le\n", expr
->ctype
->bit_size
, new, value
);
1014 static int show_string_expr(struct expression
*expr
)
1016 int new = new_pseudo();
1018 printf("\tmovi.%d\t\tv%d,&%s\n", bits_in_pointer
, new, show_string(expr
->string
));
1022 static int show_label_expr(struct expression
*expr
)
1024 int new = new_pseudo();
1025 printf("\tmovi.%d\t\tv%d,.L%p\n",bits_in_pointer
, new, expr
->label_symbol
);
1029 static int show_conditional_expr(struct expression
*expr
)
1031 int cond
= show_expression(expr
->conditional
);
1032 int valt
= show_expression(expr
->cond_true
);
1033 int valf
= show_expression(expr
->cond_false
);
1034 int new = new_pseudo();
1036 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond
, expr
->ctype
->bit_size
, new, valt
, valf
);
1040 static int show_statement_expr(struct expression
*expr
)
1042 return show_statement(expr
->statement
);
1045 static int show_position_expr(struct expression
*expr
, struct symbol
*base
)
1047 int new = show_expression(expr
->init_expr
);
1048 struct symbol
*ctype
= expr
->init_expr
->ctype
;
1051 bit_offset
= ctype
? ctype
->bit_offset
: -1;
1053 printf("\tinsert v%d at [%d:%d] of %s\n", new,
1054 expr
->init_offset
, bit_offset
,
1055 show_ident(base
->ident
));
1059 static int show_initializer_expr(struct expression
*expr
, struct symbol
*ctype
)
1061 struct expression
*entry
;
1063 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1066 // Nested initializers have their positions already
1067 // recursively calculated - just output them too
1068 if (entry
->type
== EXPR_INITIALIZER
) {
1069 show_initializer_expr(entry
, ctype
);
1073 // Initializer indexes and identifiers should
1074 // have been evaluated to EXPR_POS
1075 if (entry
->type
== EXPR_IDENTIFIER
) {
1076 printf(" AT '%s':\n", show_ident(entry
->expr_ident
));
1077 entry
= entry
->ident_expression
;
1081 if (entry
->type
== EXPR_INDEX
) {
1082 printf(" AT '%d..%d:\n", entry
->idx_from
, entry
->idx_to
);
1083 entry
= entry
->idx_expression
;
1086 if (entry
->type
== EXPR_POS
) {
1087 show_position_expr(entry
, ctype
);
1090 show_initialization(ctype
, entry
);
1091 } END_FOR_EACH_PTR(entry
);
1095 int show_symbol_expr_init(struct symbol
*sym
)
1097 struct expression
*expr
= sym
->initializer
;
1100 show_expression(expr
);
1101 return show_symbol_expr(sym
);
1105 * Print out an expression. Return the pseudo that contains the
1108 int show_expression(struct expression
*expr
)
1114 struct position
*pos
= &expr
->pos
;
1115 printf("\tno type at %s:%d:%d\n",
1116 stream_name(pos
->stream
),
1117 pos
->line
, pos
->pos
);
1121 switch (expr
->type
) {
1123 return show_call_expression(expr
);
1125 case EXPR_ASSIGNMENT
:
1126 return show_assignment(expr
);
1129 return show_comma(expr
);
1133 return show_binop(expr
);
1135 return show_preop(expr
);
1137 return show_postop(expr
);
1139 return show_symbol_expr(expr
->symbol
);
1142 case EXPR_PTRSIZEOF
:
1145 warning(expr
->pos
, "invalid expression after evaluation");
1148 case EXPR_FORCE_CAST
:
1149 case EXPR_IMPLIED_CAST
:
1150 return show_cast_expr(expr
);
1152 return show_value(expr
);
1154 return show_fvalue(expr
);
1156 return show_string_expr(expr
);
1157 case EXPR_INITIALIZER
:
1158 return show_initializer_expr(expr
, expr
->ctype
);
1160 case EXPR_CONDITIONAL
:
1161 return show_conditional_expr(expr
);
1162 case EXPR_STATEMENT
:
1163 return show_statement_expr(expr
);
1165 return show_label_expr(expr
);
1167 return show_slice(expr
);
1169 // None of these should exist as direct expressions: they are only
1170 // valid as sub-expressions of initializers.
1172 warning(expr
->pos
, "unable to show plain initializer position expression");
1174 case EXPR_IDENTIFIER
:
1175 warning(expr
->pos
, "unable to show identifier expression");
1178 warning(expr
->pos
, "unable to show index expression");
1181 warning(expr
->pos
, "unable to show type expression");
1184 warning(expr
->pos
, "unable to show generic expression");