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 static const char *show_modifiers(unsigned long mod
, int term
)
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
)
158 if (len
&& !term
) // strip the trailing space
165 // show the modifiers, terminated by a space if not empty
166 const char *modifier_string(unsigned long mod
)
168 return show_modifiers(mod
, 1);
172 // show the modifiers, without an ending space
173 const char *modifier_name(unsigned long mod
)
175 return show_modifiers(mod
, 0);
178 static void show_struct_member(struct symbol
*sym
)
180 printf("\t%s:%d:%ld at offset %ld.%d", show_ident(sym
->ident
), sym
->bit_size
, sym
->ctype
.alignment
, sym
->offset
, sym
->bit_offset
);
184 void show_symbol_list(struct symbol_list
*list
)
187 const char *prepend
= "";
189 FOR_EACH_PTR(list
, sym
) {
193 } END_FOR_EACH_PTR(sym
);
196 const char *show_as(struct ident
*as
)
200 return show_ident(as
);
208 static void FORMAT_ATTR(2) prepend(struct type_name
*name
, const char *fmt
, ...)
210 static char buffer
[512];
215 n
= vsprintf(buffer
, fmt
, args
);
219 memcpy(name
->start
, buffer
, n
);
222 static void FORMAT_ATTR(2) append(struct type_name
*name
, const char *fmt
, ...)
224 static char buffer
[512];
229 n
= vsprintf(buffer
, fmt
, args
);
232 memcpy(name
->end
, buffer
, n
);
236 static struct ctype_name
{
241 { & char_ctype
, "char", "" },
242 { &schar_ctype
, "signed char", "" },
243 { &uchar_ctype
, "unsigned char", "" },
244 { & short_ctype
, "short", "" },
245 { &sshort_ctype
, "signed short", "" },
246 { &ushort_ctype
, "unsigned short", "" },
247 { & int_ctype
, "int", "" },
248 { &sint_ctype
, "signed int", "" },
249 { &uint_ctype
, "unsigned int", "U" },
250 { & long_ctype
, "long", "L" },
251 { &slong_ctype
, "signed long", "L" },
252 { &ulong_ctype
, "unsigned long", "UL" },
253 { & llong_ctype
, "long long", "LL" },
254 { &sllong_ctype
, "signed long long", "LL" },
255 { &ullong_ctype
, "unsigned long long", "ULL" },
256 { & int128_ctype
, "__int128", "" },
257 { &sint128_ctype
, "signed __int128", "" },
258 { &uint128_ctype
, "unsigned __int128", "" },
260 { &void_ctype
, "void", "" },
261 { &bool_ctype
, "bool", "" },
263 { &float_ctype
, "float", "F" },
264 { &double_ctype
, "double", "" },
265 { &ldouble_ctype
,"long double", "L" },
266 { &incomplete_ctype
, "incomplete type", "" },
267 { &int_type
, "abstract int", "" },
268 { &fp_type
, "abstract fp", "" },
269 { &label_ctype
, "label type", "" },
270 { &bad_ctype
, "bad type", "" },
273 const char *builtin_typename(struct symbol
*sym
)
277 for (i
= 0; i
< ARRAY_SIZE(typenames
); i
++)
278 if (typenames
[i
].sym
== sym
)
279 return typenames
[i
].name
;
283 const char *builtin_type_suffix(struct symbol
*sym
)
287 for (i
= 0; i
< ARRAY_SIZE(typenames
); i
++)
288 if (typenames
[i
].sym
== sym
)
289 return typenames
[i
].suffix
;
293 const char *builtin_ctypename(struct ctype
*ctype
)
297 for (i
= 0; i
< ARRAY_SIZE(typenames
); i
++)
298 if (&typenames
[i
].sym
->ctype
== ctype
)
299 return typenames
[i
].name
;
303 static void do_show_type(struct symbol
*sym
, struct type_name
*name
)
305 const char *typename
;
306 unsigned long mod
= 0;
307 struct ident
*as
= NULL
;
313 if (sym
&& (sym
->type
!= SYM_NODE
&& sym
->type
!= SYM_ARRAY
&&
314 sym
->type
!= SYM_BITFIELD
)) {
319 prepend(name
, "%s ", show_as(as
));
321 if (sym
&& (sym
->type
== SYM_BASETYPE
|| sym
->type
== SYM_ENUM
))
322 mod
&= ~MOD_SPECIFIER
;
323 s
= modifier_string(mod
);
326 memcpy(name
->start
, s
, len
);
334 if ((typename
= builtin_typename(sym
))) {
335 int len
= strlen(typename
);
336 if (name
->start
!= name
->end
)
337 *--name
->start
= ' ';
339 memcpy(name
->start
, typename
, len
);
347 mod
= sym
->ctype
.modifiers
;
350 examine_pointer_target(sym
);
359 append(name
, "( ... )");
363 if (name
->start
!= name
->end
)
364 *--name
->start
= ' ';
365 prepend(name
, "struct %s", show_ident(sym
->ident
));
369 if (name
->start
!= name
->end
)
370 *--name
->start
= ' ';
371 prepend(name
, "union %s", show_ident(sym
->ident
));
375 prepend(name
, "enum %s ", show_ident(sym
->ident
));
380 append(name
, "%s", show_ident(sym
->ident
));
381 mod
|= sym
->ctype
.modifiers
;
382 combine_address_space(sym
->pos
, &as
, sym
->ctype
.as
);
386 mod
|= sym
->ctype
.modifiers
;
387 combine_address_space(sym
->pos
, &as
, sym
->ctype
.as
);
388 append(name
, ":%d", sym
->bit_size
);
392 append(name
, "label(%s:%p)", show_ident(sym
->ident
), sym
);
396 mod
|= sym
->ctype
.modifiers
;
397 combine_address_space(sym
->pos
, &as
, sym
->ctype
.as
);
403 append(name
, "[%lld]", get_expression_value(sym
->array_size
));
411 if (name
->start
!= name
->end
)
412 *--name
->start
= ' ';
413 prepend(name
, "restricted %s", show_ident(sym
->ident
));
421 if (name
->start
!= name
->end
)
422 *--name
->start
= ' ';
423 prepend(name
, "unknown type %d", sym
->type
);
427 sym
= sym
->ctype
.base_type
;
432 prepend(name
, "restricted ");
434 prepend(name
, "fouled ");
436 // strip trailing space
437 if (name
->end
> name
->start
&& name
->end
[-1] == ' ')
441 void show_type(struct symbol
*sym
)
444 struct type_name name
;
446 name
.start
= name
.end
= array
+100;
447 do_show_type(sym
, &name
);
449 printf("%s", name
.start
);
452 const char *show_typename(struct symbol
*sym
)
454 static char array
[200];
455 struct type_name name
;
457 name
.start
= name
.end
= array
+100;
458 do_show_type(sym
, &name
);
463 void show_symbol(struct symbol
*sym
)
470 if (sym
->ctype
.alignment
)
471 printf(".align %ld\n", sym
->ctype
.alignment
);
474 type
= sym
->ctype
.base_type
;
481 * Show actual implementation information
483 switch (type
->type
) {
484 struct symbol
*member
;
489 FOR_EACH_PTR(type
->symbol_list
, member
) {
490 show_struct_member(member
);
491 } END_FOR_EACH_PTR(member
);
496 struct statement
*stmt
= type
->stmt
;
500 val
= show_statement(stmt
);
502 printf("\tmov.%d\t\tretval,%d\n", stmt
->ret
->bit_size
, val
);
513 if (sym
->initializer
) {
515 show_expression(sym
->initializer
);
519 static int show_symbol_init(struct symbol
*sym
);
521 static int new_pseudo(void)
527 static int new_label(void)
529 static int label
= 0;
533 static void show_switch_statement(struct statement
*stmt
)
535 int val
= show_expression(stmt
->switch_expression
);
537 printf("\tswitch v%d\n", val
);
540 * Debugging only: Check that the case list is correct
541 * by printing it out.
543 * This is where a _real_ back-end would go through the
544 * cases to decide whether to use a lookup table or a
545 * series of comparisons etc
547 printf("# case table:\n");
548 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
549 struct statement
*case_stmt
= sym
->stmt
;
550 struct expression
*expr
= case_stmt
->case_expression
;
551 struct expression
*to
= case_stmt
->case_to
;
556 if (expr
->type
== EXPR_VALUE
) {
557 printf(" case %lld", expr
->value
);
559 if (to
->type
== EXPR_VALUE
) {
560 printf(" .. %lld", to
->value
);
568 printf(": .L%p\n", sym
);
569 } END_FOR_EACH_PTR(sym
);
570 printf("# end case table\n");
572 show_statement(stmt
->switch_statement
);
574 if (stmt
->switch_break
->used
)
575 printf(".L%p:\n", stmt
->switch_break
);
578 static void show_symbol_decl(struct symbol_list
*syms
)
581 FOR_EACH_PTR(syms
, sym
) {
582 show_symbol_init(sym
);
583 } END_FOR_EACH_PTR(sym
);
586 static int show_return_stmt(struct statement
*stmt
);
589 * Print out a statement
591 int show_statement(struct statement
*stmt
)
595 switch (stmt
->type
) {
596 case STMT_DECLARATION
:
597 show_symbol_decl(stmt
->declaration
);
600 return show_return_stmt(stmt
);
601 case STMT_COMPOUND
: {
605 if (stmt
->inline_fn
) {
606 show_statement(stmt
->args
);
607 printf("\tbegin_inline \t%s\n", show_ident(stmt
->inline_fn
->ident
));
609 FOR_EACH_PTR(stmt
->stmts
, s
) {
610 last
= show_statement(s
);
611 } END_FOR_EACH_PTR(s
);
614 printf(".L%p:\n", stmt
->ret
);
615 addr
= show_symbol_expr(stmt
->ret
);
616 bits
= stmt
->ret
->bit_size
;
618 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, last
, addr
);
621 printf("\tend_inlined\t%s\n", show_ident(stmt
->inline_fn
->ident
));
625 case STMT_EXPRESSION
:
626 return show_expression(stmt
->expression
);
629 struct expression
*cond
= stmt
->if_conditional
;
631 /* This is only valid if nobody can jump into the "dead" statement */
633 if (cond
->type
== EXPR_VALUE
) {
634 struct statement
*s
= stmt
->if_true
;
641 val
= show_expression(cond
);
642 target
= new_label();
643 printf("\tje\t\tv%d,.L%d\n", val
, target
);
644 show_statement(stmt
->if_true
);
645 if (stmt
->if_false
) {
646 int last
= new_label();
647 printf("\tjmp\t\t.L%d\n", last
);
648 printf(".L%d:\n", target
);
650 show_statement(stmt
->if_false
);
652 printf(".L%d:\n", target
);
656 show_switch_statement(stmt
);
660 printf(".L%p:\n", stmt
->case_label
);
661 show_statement(stmt
->case_statement
);
664 case STMT_ITERATOR
: {
665 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
666 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
667 struct statement
*statement
= stmt
->iterator_statement
;
668 struct statement
*post_statement
= stmt
->iterator_post_statement
;
669 struct expression
*post_condition
= stmt
->iterator_post_condition
;
670 int val
, loop_top
= 0, loop_bottom
= 0;
672 show_symbol_decl(stmt
->iterator_syms
);
673 show_statement(pre_statement
);
675 if (pre_condition
->type
== EXPR_VALUE
) {
676 if (!pre_condition
->value
) {
677 loop_bottom
= new_label();
678 printf("\tjmp\t\t.L%d\n", loop_bottom
);
681 loop_bottom
= new_label();
682 val
= show_expression(pre_condition
);
683 printf("\tje\t\tv%d, .L%d\n", val
, loop_bottom
);
686 if (!post_condition
|| post_condition
->type
!= EXPR_VALUE
|| post_condition
->value
) {
687 loop_top
= new_label();
688 printf(".L%d:\n", loop_top
);
690 show_statement(statement
);
691 if (stmt
->iterator_continue
->used
)
692 printf(".L%p:\n", stmt
->iterator_continue
);
693 show_statement(post_statement
);
694 if (!post_condition
) {
695 printf("\tjmp\t\t.L%d\n", loop_top
);
696 } else if (post_condition
->type
== EXPR_VALUE
) {
697 if (post_condition
->value
)
698 printf("\tjmp\t\t.L%d\n", loop_top
);
700 val
= show_expression(post_condition
);
701 printf("\tjne\t\tv%d, .L%d\n", val
, loop_top
);
703 if (stmt
->iterator_break
->used
)
704 printf(".L%p:\n", stmt
->iterator_break
);
706 printf(".L%d:\n", loop_bottom
);
713 printf(".L%p:\n", stmt
->label_identifier
);
714 show_statement(stmt
->label_statement
);
718 if (stmt
->goto_expression
) {
719 int val
= show_expression(stmt
->goto_expression
);
720 printf("\tgoto\t\t*v%d\n", val
);
722 printf("\tgoto\t\t.L%p\n", stmt
->goto_label
);
726 printf("\tasm( .... )\n");
729 int val
= show_expression(stmt
->expression
);
730 printf("\tcontext( %d )\n", val
);
734 int val
= show_expression(stmt
->range_expression
);
735 int low
= show_expression(stmt
->range_low
);
736 int high
= show_expression(stmt
->range_high
);
737 printf("\trange( %d %d-%d)\n", val
, low
, high
);
744 static int show_call_expression(struct expression
*expr
)
746 struct symbol
*direct
;
747 struct expression
*arg
, *fn
;
752 warning(expr
->pos
, "\tcall with no type!");
757 FOR_EACH_PTR_REVERSE(expr
->args
, arg
) {
758 int new = show_expression(arg
);
759 int size
= arg
->ctype
->bit_size
;
760 printf("\tpush.%d\t\tv%d\n", size
, new);
761 framesize
+= bits_to_bytes(size
);
762 } END_FOR_EACH_PTR_REVERSE(arg
);
766 /* Remove dereference, if any */
768 if (fn
->type
== EXPR_PREOP
) {
769 if (fn
->unop
->type
== EXPR_SYMBOL
) {
770 struct symbol
*sym
= fn
->unop
->symbol
;
771 if (sym
->ctype
.base_type
->type
== SYM_FN
)
776 printf("\tcall\t\t%s\n", show_ident(direct
->ident
));
778 fncall
= show_expression(fn
);
779 printf("\tcall\t\t*v%d\n", fncall
);
782 printf("\tadd.%d\t\tvSP,vSP,$%d\n", bits_in_pointer
, framesize
);
784 retval
= new_pseudo();
785 printf("\tmov.%d\t\tv%d,retval\n", expr
->ctype
->bit_size
, retval
);
789 static int show_comma(struct expression
*expr
)
791 show_expression(expr
->left
);
792 return show_expression(expr
->right
);
795 static int show_binop(struct expression
*expr
)
797 int left
= show_expression(expr
->left
);
798 int right
= show_expression(expr
->right
);
799 int new = new_pseudo();
801 static const char *name
[] = {
802 ['+'] = "add", ['-'] = "sub",
803 ['*'] = "mul", ['/'] = "div",
804 ['%'] = "mod", ['&'] = "and",
805 ['|'] = "lor", ['^'] = "xor"
807 unsigned int op
= expr
->op
;
809 opname
= show_special(op
);
810 if (op
< ARRAY_SIZE(name
))
812 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname
,
813 expr
->ctype
->bit_size
,
818 static int show_slice(struct expression
*expr
)
820 int target
= show_expression(expr
->base
);
821 int new = new_pseudo();
822 printf("\tslice.%d\t\tv%d,v%d,%d\n", expr
->ctype
->bit_size
, target
, new, expr
->r_bitpos
);
826 static int show_regular_preop(struct expression
*expr
)
828 int target
= show_expression(expr
->unop
);
829 int new = new_pseudo();
830 static const char *name
[] = {
831 ['!'] = "nonzero", ['-'] = "neg",
834 unsigned int op
= expr
->op
;
837 opname
= show_special(op
);
838 if (op
< ARRAY_SIZE(name
))
840 printf("\t%s.%d\t\tv%d,v%d\n", opname
, expr
->ctype
->bit_size
, new, target
);
845 * FIXME! Not all accesses are memory loads. We should
846 * check what kind of symbol is behind the dereference.
848 static int show_address_gen(struct expression
*expr
)
850 return show_expression(expr
->unop
);
853 static int show_load_gen(int bits
, struct expression
*expr
, int addr
)
855 int new = new_pseudo();
857 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, new, addr
);
861 static void show_store_gen(int bits
, int value
, struct expression
*expr
, int addr
)
863 /* FIXME!!! Bitfield store! */
864 printf("\tst.%d\t\tv%d,[v%d]\n", bits
, value
, addr
);
867 static int show_assignment(struct expression
*expr
)
869 struct expression
*target
= expr
->left
;
875 bits
= expr
->ctype
->bit_size
;
876 val
= show_expression(expr
->right
);
877 addr
= show_address_gen(target
);
878 show_store_gen(bits
, val
, target
, addr
);
882 static int show_return_stmt(struct statement
*stmt
)
884 struct expression
*expr
= stmt
->ret_value
;
885 struct symbol
*target
= stmt
->ret_target
;
887 if (expr
&& expr
->ctype
) {
888 int val
= show_expression(expr
);
889 int bits
= expr
->ctype
->bit_size
;
890 int addr
= show_symbol_expr(target
);
891 show_store_gen(bits
, val
, NULL
, addr
);
893 printf("\tret\t\t(%p)\n", target
);
897 static int show_initialization(struct symbol
*sym
, struct expression
*expr
)
904 bits
= expr
->ctype
->bit_size
;
905 val
= show_expression(expr
);
906 addr
= show_symbol_expr(sym
);
907 // FIXME! The "target" expression is for bitfield store information.
908 // Leave it NULL, which works fine.
909 show_store_gen(bits
, val
, NULL
, addr
);
913 static int show_access(struct expression
*expr
)
915 int addr
= show_address_gen(expr
);
916 return show_load_gen(expr
->ctype
->bit_size
, expr
, addr
);
919 static int show_inc_dec(struct expression
*expr
, int postop
)
921 int addr
= show_address_gen(expr
->unop
);
923 const char *opname
= expr
->op
== SPECIAL_INCREMENT
? "add" : "sub";
924 int bits
= expr
->ctype
->bit_size
;
926 retval
= show_load_gen(bits
, expr
->unop
, addr
);
930 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname
, bits
, new, retval
);
931 show_store_gen(bits
, new, expr
->unop
, addr
);
935 static int show_preop(struct expression
*expr
)
938 * '*' is an lvalue access, and is fundamentally different
939 * from an arithmetic operation. Maybe it should have an
940 * expression type of its own..
943 return show_access(expr
);
944 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
945 return show_inc_dec(expr
, 0);
946 return show_regular_preop(expr
);
949 static int show_postop(struct expression
*expr
)
951 return show_inc_dec(expr
, 1);
954 static int show_symbol_expr(struct symbol
*sym
)
956 int new = new_pseudo();
958 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_STRING
)
959 return show_string_expr(sym
->initializer
);
961 if (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_EXTERN
| MOD_STATIC
)) {
962 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer
, new, show_ident(sym
->ident
));
965 if (sym
->ctype
.modifiers
& MOD_ADDRESSABLE
) {
966 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer
, new, 0LL);
969 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer
, new, show_ident(sym
->ident
), sym
);
973 static int show_symbol_init(struct symbol
*sym
)
975 struct expression
*expr
= sym
->initializer
;
980 bits
= expr
->ctype
->bit_size
;
981 val
= show_expression(expr
);
982 addr
= show_symbol_expr(sym
);
983 show_store_gen(bits
, val
, NULL
, addr
);
988 static int show_cast_expr(struct expression
*expr
)
990 struct symbol
*old_type
, *new_type
;
991 int op
= show_expression(expr
->cast_expression
);
992 int oldbits
, newbits
;
995 old_type
= expr
->cast_expression
->ctype
;
996 new_type
= expr
->cast_type
;
998 oldbits
= old_type
->bit_size
;
999 newbits
= new_type
->bit_size
;
1000 if (oldbits
>= newbits
)
1003 is_signed
= is_signed_type(old_type
);
1005 printf("\tsext%d.%d\tv%d,v%d\n", oldbits
, newbits
, new, op
);
1007 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits
, new, op
, (1UL << oldbits
)-1);
1012 static int show_value(struct expression
*expr
)
1014 int new = new_pseudo();
1015 unsigned long long value
= expr
->value
;
1017 printf("\tmovi.%d\t\tv%d,$%llu\n", expr
->ctype
->bit_size
, new, value
);
1021 static int show_fvalue(struct expression
*expr
)
1023 int new = new_pseudo();
1024 long double value
= expr
->fvalue
;
1026 printf("\tmovf.%d\t\tv%d,$%Le\n", expr
->ctype
->bit_size
, new, value
);
1030 static int show_string_expr(struct expression
*expr
)
1032 int new = new_pseudo();
1034 printf("\tmovi.%d\t\tv%d,&%s\n", bits_in_pointer
, new, show_string(expr
->string
));
1038 static int show_label_expr(struct expression
*expr
)
1040 int new = new_pseudo();
1041 printf("\tmovi.%d\t\tv%d,.L%p\n",bits_in_pointer
, new, expr
->label_symbol
);
1045 static int show_conditional_expr(struct expression
*expr
)
1047 int cond
= show_expression(expr
->conditional
);
1048 int valt
= show_expression(expr
->cond_true
);
1049 int valf
= show_expression(expr
->cond_false
);
1050 int new = new_pseudo();
1052 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond
, expr
->ctype
->bit_size
, new, valt
, valf
);
1056 static int show_statement_expr(struct expression
*expr
)
1058 return show_statement(expr
->statement
);
1061 static int show_position_expr(struct expression
*expr
, struct symbol
*base
)
1063 int new = show_expression(expr
->init_expr
);
1064 struct symbol
*ctype
= expr
->init_expr
->ctype
;
1067 bit_offset
= ctype
? ctype
->bit_offset
: -1;
1069 printf("\tinsert v%d at [%d:%d] of %s\n", new,
1070 expr
->init_offset
, bit_offset
,
1071 show_ident(base
->ident
));
1075 static int show_initializer_expr(struct expression
*expr
, struct symbol
*ctype
)
1077 struct expression
*entry
;
1079 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1082 // Nested initializers have their positions already
1083 // recursively calculated - just output them too
1084 if (entry
->type
== EXPR_INITIALIZER
) {
1085 show_initializer_expr(entry
, ctype
);
1089 // Initializer indexes and identifiers should
1090 // have been evaluated to EXPR_POS
1091 if (entry
->type
== EXPR_IDENTIFIER
) {
1092 printf(" AT '%s':\n", show_ident(entry
->expr_ident
));
1093 entry
= entry
->ident_expression
;
1097 if (entry
->type
== EXPR_INDEX
) {
1098 printf(" AT '%d..%d:\n", entry
->idx_from
, entry
->idx_to
);
1099 entry
= entry
->idx_expression
;
1102 if (entry
->type
== EXPR_POS
) {
1103 show_position_expr(entry
, ctype
);
1106 show_initialization(ctype
, entry
);
1107 } END_FOR_EACH_PTR(entry
);
1111 int show_symbol_expr_init(struct symbol
*sym
)
1113 struct expression
*expr
= sym
->initializer
;
1116 show_expression(expr
);
1117 return show_symbol_expr(sym
);
1121 * Print out an expression. Return the pseudo that contains the
1124 int show_expression(struct expression
*expr
)
1130 struct position
*pos
= &expr
->pos
;
1131 printf("\tno type at %s:%d:%d\n",
1132 stream_name(pos
->stream
),
1133 pos
->line
, pos
->pos
);
1137 switch (expr
->type
) {
1139 return show_call_expression(expr
);
1141 case EXPR_ASSIGNMENT
:
1142 return show_assignment(expr
);
1145 return show_comma(expr
);
1149 return show_binop(expr
);
1151 return show_preop(expr
);
1153 return show_postop(expr
);
1155 return show_symbol_expr(expr
->symbol
);
1158 case EXPR_PTRSIZEOF
:
1161 warning(expr
->pos
, "invalid expression after evaluation");
1164 case EXPR_FORCE_CAST
:
1165 case EXPR_IMPLIED_CAST
:
1166 return show_cast_expr(expr
);
1168 return show_value(expr
);
1170 return show_fvalue(expr
);
1172 return show_string_expr(expr
);
1173 case EXPR_INITIALIZER
:
1174 return show_initializer_expr(expr
, expr
->ctype
);
1176 case EXPR_CONDITIONAL
:
1177 return show_conditional_expr(expr
);
1178 case EXPR_STATEMENT
:
1179 return show_statement_expr(expr
);
1181 return show_label_expr(expr
);
1183 return show_slice(expr
);
1185 // None of these should exist as direct expressions: they are only
1186 // valid as sub-expressions of initializers.
1188 warning(expr
->pos
, "unable to show plain initializer position expression");
1190 case EXPR_IDENTIFIER
:
1191 warning(expr
->pos
, "unable to show identifier expression");
1194 warning(expr
->pos
, "unable to show index expression");
1197 warning(expr
->pos
, "unable to show type expression");
1200 warning(expr
->pos
, "unable to show generic expression");