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_TYPEDEF
] = "tdef",
62 [SYM_TYPEOF
] = "tpof",
63 [SYM_MEMBER
] = "memb",
64 [SYM_BITFIELD
] = "bitf",
66 [SYM_RESTRICT
] = "rstr",
67 [SYM_FOULED
] = "foul",
70 struct context
*context
;
75 fprintf(stderr
, "%.*s%s%3d:%lu %s %s (as: %d) %p (%s:%d:%d) %s\n",
76 indent
, indent_string
, typestr
[sym
->type
],
77 sym
->bit_size
, sym
->ctype
.alignment
,
78 modifier_string(sym
->ctype
.modifiers
), show_ident(sym
->ident
), sym
->ctype
.attribute
->as
,
79 sym
, stream_name(sym
->pos
.stream
), sym
->pos
.line
, sym
->pos
.pos
,
80 builtin_typename(sym
) ?: "");
82 FOR_EACH_PTR(sym
->ctype
.attribute
->contexts
, context
) {
83 /* FIXME: should print context expression */
84 fprintf(stderr
, "< context%d: in=%d, out=%d\n",
85 i
, context
->in
, context
->out
);
86 fprintf(stderr
, " end context%d >\n", i
);
88 } END_FOR_EACH_PTR(context
);
89 if (sym
->type
== SYM_FN
) {
92 FOR_EACH_PTR(sym
->arguments
, arg
) {
93 fprintf(stderr
, "< arg%d:\n", i
);
94 do_debug_symbol(arg
, 0);
95 fprintf(stderr
, " end arg%d >\n", i
);
97 } END_FOR_EACH_PTR(arg
);
99 do_debug_symbol(sym
->ctype
.base_type
, indent
+2);
102 void debug_symbol(struct symbol
*sym
)
104 do_debug_symbol(sym
, 0);
108 * Symbol type printout. The type system is by far the most
109 * complicated part of C - everything else is trivial.
111 const char *modifier_string(unsigned long mod
)
113 static char buffer
[100];
121 static struct mod_name mod_names
[] = {
123 {MOD_REGISTER
, "register"},
124 {MOD_STATIC
, "static"},
125 {MOD_EXTERN
, "extern"},
126 {MOD_CONST
, "const"},
127 {MOD_VOLATILE
, "volatile"},
128 {MOD_SIGNED
, "[signed]"},
129 {MOD_UNSIGNED
, "[unsigned]"},
130 {MOD_CHAR
, "[char]"},
131 {MOD_SHORT
, "[short]"},
132 {MOD_LONG
, "[long]"},
133 {MOD_LONGLONG
, "[long long]"},
134 {MOD_LONGLONGLONG
, "[long long long]"},
135 {MOD_TYPEDEF
, "[typedef]"},
137 {MOD_INLINE
, "inline"},
138 {MOD_ADDRESSABLE
, "[addressable]"},
139 {MOD_NOCAST
, "[nocast]"},
140 {MOD_NODEREF
, "[noderef]"},
141 {MOD_ACCESSED
, "[accessed]"},
142 {MOD_TOPLEVEL
, "[toplevel]"},
143 {MOD_ASSIGNED
, "[assigned]"},
144 {MOD_TYPE
, "[type]"},
145 {MOD_SAFE
, "[safe]"},
146 {MOD_USERTYPE
, "[usertype]"},
147 {MOD_NORETURN
, "[noreturn]"},
148 {MOD_EXPLICITLY_SIGNED
, "[explicitly-signed]"},
149 {MOD_BITWISE
, "[bitwise]"},
150 {MOD_PURE
, "[pure]"},
153 for (i
= 0; i
< ARRAY_SIZE(mod_names
); i
++) {
157 const char *name
= m
->name
;
158 while ((c
= *name
++) != '\0' && len
+ 2 < sizeof buffer
)
167 static void show_struct_member(struct symbol
*sym
)
169 printf("\t%s:%d:%ld at offset %ld.%d", show_ident(sym
->ident
), sym
->bit_size
, sym
->ctype
.alignment
, sym
->offset
, sym
->bit_offset
);
173 void show_symbol_list(struct symbol_list
*list
, const char *sep
)
176 const char *prepend
= "";
178 FOR_EACH_PTR(list
, sym
) {
182 } END_FOR_EACH_PTR(sym
);
190 static void FORMAT_ATTR(2) prepend(struct type_name
*name
, const char *fmt
, ...)
192 static char buffer
[512];
197 n
= vsprintf(buffer
, fmt
, args
);
201 memcpy(name
->start
, buffer
, n
);
204 static void FORMAT_ATTR(2) append(struct type_name
*name
, const char *fmt
, ...)
206 static char buffer
[512];
211 n
= vsprintf(buffer
, fmt
, args
);
214 memcpy(name
->end
, buffer
, n
);
218 static struct ctype_name
{
222 { & char_ctype
, "char" },
223 { &schar_ctype
, "signed char" },
224 { &uchar_ctype
, "unsigned char" },
225 { & short_ctype
, "short" },
226 { &sshort_ctype
, "signed short" },
227 { &ushort_ctype
, "unsigned short" },
228 { & int_ctype
, "int" },
229 { &sint_ctype
, "signed int" },
230 { &uint_ctype
, "unsigned int" },
231 { &slong_ctype
, "signed long" },
232 { & long_ctype
, "long" },
233 { &ulong_ctype
, "unsigned long" },
234 { & llong_ctype
, "long long" },
235 { &sllong_ctype
, "signed long long" },
236 { &ullong_ctype
, "unsigned long long" },
237 { & lllong_ctype
, "long long long" },
238 { &slllong_ctype
, "signed long long long" },
239 { &ulllong_ctype
, "unsigned long long long" },
241 { &void_ctype
, "void" },
242 { &bool_ctype
, "bool" },
243 { &string_ctype
, "string" },
245 { &float_ctype
, "float" },
246 { &double_ctype
, "double" },
247 { &ldouble_ctype
,"long double" },
248 { &incomplete_ctype
, "incomplete type" },
249 { &int_type
, "abstract int" },
250 { &fp_type
, "abstract fp" },
251 { &label_ctype
, "label type" },
252 { &bad_ctype
, "bad type" },
255 const char *builtin_typename(struct symbol
*sym
)
259 for (i
= 0; i
< ARRAY_SIZE(typenames
); i
++)
260 if (typenames
[i
].sym
== sym
)
261 return typenames
[i
].name
;
265 const char *builtin_ctypename(struct ctype
*ctype
)
269 for (i
= 0; i
< ARRAY_SIZE(typenames
); i
++)
270 if (&typenames
[i
].sym
->ctype
== ctype
)
271 return typenames
[i
].name
;
275 static void do_show_type(struct symbol
*sym
, struct type_name
*name
)
277 const char *typename
;
278 unsigned long mod
= 0;
285 if (!sym
|| (sym
->type
!= SYM_NODE
&& sym
->type
!= SYM_ARRAY
&&
286 sym
->type
!= SYM_BITFIELD
)) {
291 prepend(name
, "<asn:%d>", as
);
293 s
= modifier_string(mod
);
296 memcpy(name
->start
, s
, len
);
304 if ((typename
= builtin_typename(sym
))) {
305 int len
= strlen(typename
);
306 if (name
->start
!= name
->end
)
307 *--name
->start
= ' ';
309 memcpy(name
->start
, typename
, len
);
317 mod
= sym
->ctype
.modifiers
;
318 as
= sym
->ctype
.attribute
->as
;
328 append(name
, "( ... )");
332 if (name
->start
!= name
->end
)
333 *--name
->start
= ' ';
334 prepend(name
, "struct %s", show_ident(sym
->ident
));
338 if (name
->start
!= name
->end
)
339 *--name
->start
= ' ';
340 prepend(name
, "union %s", show_ident(sym
->ident
));
344 prepend(name
, "enum %s ", show_ident(sym
->ident
));
348 append(name
, "%s", show_ident(sym
->ident
));
349 mod
|= sym
->ctype
.modifiers
;
350 as
|= sym
->ctype
.attribute
->as
;
354 mod
|= sym
->ctype
.modifiers
;
355 as
|= sym
->ctype
.attribute
->as
;
356 append(name
, ":%d", sym
->bit_size
);
360 append(name
, "label(%s:%p)", show_ident(sym
->ident
), sym
);
364 mod
|= sym
->ctype
.modifiers
;
365 as
|= sym
->ctype
.attribute
->as
;
371 append(name
, "[%lld]", get_expression_value(sym
->array_size
));
379 if (name
->start
!= name
->end
)
380 *--name
->start
= ' ';
381 prepend(name
, "restricted %s", show_ident(sym
->ident
));
389 if (name
->start
!= name
->end
)
390 *--name
->start
= ' ';
391 prepend(name
, "unknown type %d", sym
->type
);
395 sym
= sym
->ctype
.base_type
;
400 prepend(name
, "restricted ");
402 prepend(name
, "fouled ");
405 void show_type(struct symbol
*sym
)
408 struct type_name name
;
410 name
.start
= name
.end
= array
+100;
411 do_show_type(sym
, &name
);
413 printf("%s", name
.start
);
416 const char *show_typename(struct symbol
*sym
)
418 static char array
[200];
419 struct type_name name
;
421 name
.start
= name
.end
= array
+100;
422 do_show_type(sym
, &name
);
427 void show_symbol(struct symbol
*sym
)
434 if (sym
->ctype
.alignment
)
435 printf(".align %ld\n", sym
->ctype
.alignment
);
438 type
= sym
->ctype
.base_type
;
445 * Show actual implementation information
447 switch (type
->type
) {
448 struct symbol
*member
;
453 FOR_EACH_PTR(type
->symbol_list
, member
) {
454 show_struct_member(member
);
455 } END_FOR_EACH_PTR(member
);
460 struct statement
*stmt
= type
->stmt
;
464 val
= show_statement(stmt
);
466 printf("\tmov.%d\t\tretval,%d\n", stmt
->ret
->bit_size
, val
);
477 if (sym
->initializer
) {
479 show_expression(sym
->initializer
);
483 static int show_symbol_init(struct symbol
*sym
);
485 static int new_pseudo(void)
491 static int new_label(void)
493 static int label
= 0;
497 static void show_switch_statement(struct statement
*stmt
)
499 int val
= show_expression(stmt
->switch_expression
);
501 printf("\tswitch v%d\n", val
);
504 * Debugging only: Check that the case list is correct
505 * by printing it out.
507 * This is where a _real_ back-end would go through the
508 * cases to decide whether to use a lookup table or a
509 * series of comparisons etc
511 printf("# case table:\n");
512 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
513 struct statement
*case_stmt
= sym
->stmt
;
514 struct expression
*expr
= case_stmt
->case_expression
;
515 struct expression
*to
= case_stmt
->case_to
;
520 if (expr
->type
== EXPR_VALUE
) {
521 printf(" case %lld", expr
->value
);
523 if (to
->type
== EXPR_VALUE
) {
524 printf(" .. %lld", to
->value
);
532 printf(": .L%p\n", sym
->bb_target
);
533 } END_FOR_EACH_PTR(sym
);
534 printf("# end case table\n");
536 show_statement(stmt
->switch_statement
);
538 if (stmt
->switch_break
->used
)
539 printf(".L%p:\n", stmt
->switch_break
->bb_target
);
542 static void show_symbol_decl(struct symbol_list
*syms
)
545 FOR_EACH_PTR(syms
, sym
) {
546 show_symbol_init(sym
);
547 } END_FOR_EACH_PTR(sym
);
550 static int show_return_stmt(struct statement
*stmt
);
553 * Print out a statement
555 int show_statement(struct statement
*stmt
)
559 switch (stmt
->type
) {
560 case STMT_DECLARATION
:
561 show_symbol_decl(stmt
->declaration
);
564 return show_return_stmt(stmt
);
565 case STMT_COMPOUND
: {
569 if (stmt
->inline_fn
) {
570 show_statement(stmt
->args
);
571 printf("\tbegin_inline \t%s\n", show_ident(stmt
->inline_fn
->ident
));
573 FOR_EACH_PTR(stmt
->stmts
, s
) {
574 last
= show_statement(s
);
575 } END_FOR_EACH_PTR(s
);
578 printf(".L%p:\n", stmt
->ret
);
579 addr
= show_symbol_expr(stmt
->ret
);
580 bits
= stmt
->ret
->bit_size
;
582 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, last
, addr
);
585 printf("\tend_inlined\t%s\n", show_ident(stmt
->inline_fn
->ident
));
589 case STMT_EXPRESSION
:
590 return show_expression(stmt
->expression
);
593 struct expression
*cond
= stmt
->if_conditional
;
595 /* This is only valid if nobody can jump into the "dead" statement */
597 if (cond
->type
== EXPR_VALUE
) {
598 struct statement
*s
= stmt
->if_true
;
605 val
= show_expression(cond
);
606 target
= new_label();
607 printf("\tje\t\tv%d,.L%d\n", val
, target
);
608 show_statement(stmt
->if_true
);
609 if (stmt
->if_false
) {
610 int last
= new_label();
611 printf("\tjmp\t\t.L%d\n", last
);
612 printf(".L%d:\n", target
);
614 show_statement(stmt
->if_false
);
616 printf(".L%d:\n", target
);
620 show_switch_statement(stmt
);
624 printf(".L%p:\n", stmt
->case_label
);
625 show_statement(stmt
->case_statement
);
628 case STMT_ITERATOR
: {
629 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
630 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
631 struct statement
*statement
= stmt
->iterator_statement
;
632 struct statement
*post_statement
= stmt
->iterator_post_statement
;
633 struct expression
*post_condition
= stmt
->iterator_post_condition
;
634 int val
, loop_top
= 0, loop_bottom
= 0;
636 show_symbol_decl(stmt
->iterator_syms
);
637 show_statement(pre_statement
);
639 if (pre_condition
->type
== EXPR_VALUE
) {
640 if (!pre_condition
->value
) {
641 loop_bottom
= new_label();
642 printf("\tjmp\t\t.L%d\n", loop_bottom
);
645 loop_bottom
= new_label();
646 val
= show_expression(pre_condition
);
647 printf("\tje\t\tv%d, .L%d\n", val
, loop_bottom
);
650 if (!post_condition
|| post_condition
->type
!= EXPR_VALUE
|| post_condition
->value
) {
651 loop_top
= new_label();
652 printf(".L%d:\n", loop_top
);
654 show_statement(statement
);
655 if (stmt
->iterator_continue
->used
)
656 printf(".L%p:\n", stmt
->iterator_continue
);
657 show_statement(post_statement
);
658 if (!post_condition
) {
659 printf("\tjmp\t\t.L%d\n", loop_top
);
660 } else if (post_condition
->type
== EXPR_VALUE
) {
661 if (post_condition
->value
)
662 printf("\tjmp\t\t.L%d\n", loop_top
);
664 val
= show_expression(post_condition
);
665 printf("\tjne\t\tv%d, .L%d\n", val
, loop_top
);
667 if (stmt
->iterator_break
->used
)
668 printf(".L%p:\n", stmt
->iterator_break
);
670 printf(".L%d:\n", loop_bottom
);
677 printf(".L%p:\n", stmt
->label_identifier
);
678 show_statement(stmt
->label_statement
);
682 if (stmt
->goto_expression
) {
683 int val
= show_expression(stmt
->goto_expression
);
684 printf("\tgoto\t\t*v%d\n", val
);
686 printf("\tgoto\t\t.L%p\n", stmt
->goto_label
->bb_target
);
690 printf("\tasm( .... )\n");
693 int val
= show_expression(stmt
->expression
);
694 printf("\tcontext( %d )\n", val
);
698 int val
= show_expression(stmt
->range_expression
);
699 int low
= show_expression(stmt
->range_low
);
700 int high
= show_expression(stmt
->range_high
);
701 printf("\trange( %d %d-%d)\n", val
, low
, high
);
708 static int show_call_expression(struct expression
*expr
)
710 struct symbol
*direct
;
711 struct expression
*arg
, *fn
;
716 warning(expr
->pos
, "\tcall with no type!");
721 FOR_EACH_PTR_REVERSE(expr
->args
, arg
) {
722 int new = show_expression(arg
);
723 int size
= arg
->ctype
->bit_size
;
724 printf("\tpush.%d\t\tv%d\n", size
, new);
725 framesize
+= bits_to_bytes(size
);
726 } END_FOR_EACH_PTR_REVERSE(arg
);
730 /* Remove dereference, if any */
732 if (fn
->type
== EXPR_PREOP
) {
733 if (fn
->unop
->type
== EXPR_SYMBOL
) {
734 struct symbol
*sym
= fn
->unop
->symbol
;
735 if (sym
->ctype
.base_type
->type
== SYM_FN
)
740 printf("\tcall\t\t%s\n", show_ident(direct
->ident
));
742 fncall
= show_expression(fn
);
743 printf("\tcall\t\t*v%d\n", fncall
);
746 printf("\tadd.%d\t\tvSP,vSP,$%d\n", bits_in_pointer
, framesize
);
748 retval
= new_pseudo();
749 printf("\tmov.%d\t\tv%d,retval\n", expr
->ctype
->bit_size
, retval
);
753 static int show_comma(struct expression
*expr
)
755 show_expression(expr
->left
);
756 return show_expression(expr
->right
);
759 static int show_binop(struct expression
*expr
)
761 int left
= show_expression(expr
->left
);
762 int right
= show_expression(expr
->right
);
763 int new = new_pseudo();
765 static const char *name
[] = {
766 ['+'] = "add", ['-'] = "sub",
767 ['*'] = "mul", ['/'] = "div",
768 ['%'] = "mod", ['&'] = "and",
769 ['|'] = "lor", ['^'] = "xor"
771 unsigned int op
= expr
->op
;
773 opname
= show_special(op
);
774 if (op
< ARRAY_SIZE(name
))
776 printf("\t%s.%d\t\tv%d,v%d,v%d\n", opname
,
777 expr
->ctype
->bit_size
,
782 static int show_slice(struct expression
*expr
)
784 int target
= show_expression(expr
->base
);
785 int new = new_pseudo();
786 printf("\tslice.%d\t\tv%d,v%d,%d\n", expr
->r_nrbits
, target
, new, expr
->r_bitpos
);
790 static int show_regular_preop(struct expression
*expr
)
792 int target
= show_expression(expr
->unop
);
793 int new = new_pseudo();
794 static const char *name
[] = {
795 ['!'] = "nonzero", ['-'] = "neg",
798 unsigned int op
= expr
->op
;
801 opname
= show_special(op
);
802 if (op
< ARRAY_SIZE(name
))
804 printf("\t%s.%d\t\tv%d,v%d\n", opname
, expr
->ctype
->bit_size
, new, target
);
809 * FIXME! Not all accesses are memory loads. We should
810 * check what kind of symbol is behind the dereference.
812 static int show_address_gen(struct expression
*expr
)
814 return show_expression(expr
->unop
);
817 static int show_load_gen(int bits
, struct expression
*expr
, int addr
)
819 int new = new_pseudo();
821 printf("\tld.%d\t\tv%d,[v%d]\n", bits
, new, addr
);
825 static void show_store_gen(int bits
, int value
, struct expression
*expr
, int addr
)
827 /* FIXME!!! Bitfield store! */
828 printf("\tst.%d\t\tv%d,[v%d]\n", bits
, value
, addr
);
831 static int show_assignment(struct expression
*expr
)
833 struct expression
*target
= expr
->left
;
839 bits
= expr
->ctype
->bit_size
;
840 val
= show_expression(expr
->right
);
841 addr
= show_address_gen(target
);
842 show_store_gen(bits
, val
, target
, addr
);
846 static int show_return_stmt(struct statement
*stmt
)
848 struct expression
*expr
= stmt
->ret_value
;
849 struct symbol
*target
= stmt
->ret_target
;
851 if (expr
&& expr
->ctype
) {
852 int val
= show_expression(expr
);
853 int bits
= expr
->ctype
->bit_size
;
854 int addr
= show_symbol_expr(target
);
855 show_store_gen(bits
, val
, NULL
, addr
);
857 printf("\tret\t\t(%p)\n", target
);
861 static int show_initialization(struct symbol
*sym
, struct expression
*expr
)
868 bits
= expr
->ctype
->bit_size
;
869 val
= show_expression(expr
);
870 addr
= show_symbol_expr(sym
);
871 // FIXME! The "target" expression is for bitfield store information.
872 // Leave it NULL, which works fine.
873 show_store_gen(bits
, val
, NULL
, addr
);
877 static int show_access(struct expression
*expr
)
879 int addr
= show_address_gen(expr
);
880 return show_load_gen(expr
->ctype
->bit_size
, expr
, addr
);
883 static int show_inc_dec(struct expression
*expr
, int postop
)
885 int addr
= show_address_gen(expr
->unop
);
887 const char *opname
= expr
->op
== SPECIAL_INCREMENT
? "add" : "sub";
888 int bits
= expr
->ctype
->bit_size
;
890 retval
= show_load_gen(bits
, expr
->unop
, addr
);
894 printf("\t%s.%d\t\tv%d,v%d,$1\n", opname
, bits
, new, retval
);
895 show_store_gen(bits
, new, expr
->unop
, addr
);
899 static int show_preop(struct expression
*expr
)
902 * '*' is an lvalue access, and is fundamentally different
903 * from an arithmetic operation. Maybe it should have an
904 * expression type of its own..
907 return show_access(expr
);
908 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
909 return show_inc_dec(expr
, 0);
910 return show_regular_preop(expr
);
913 static int show_postop(struct expression
*expr
)
915 return show_inc_dec(expr
, 1);
918 static int show_symbol_expr(struct symbol
*sym
)
920 int new = new_pseudo();
922 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_STRING
)
923 return show_string_expr(sym
->initializer
);
925 if (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_EXTERN
| MOD_STATIC
)) {
926 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer
, new, show_ident(sym
->ident
));
929 if (sym
->ctype
.modifiers
& MOD_ADDRESSABLE
) {
930 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer
, new, sym
->value
);
933 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer
, new, show_ident(sym
->ident
), sym
);
937 static int show_symbol_init(struct symbol
*sym
)
939 struct expression
*expr
= sym
->initializer
;
944 bits
= expr
->ctype
->bit_size
;
945 val
= show_expression(expr
);
946 addr
= show_symbol_expr(sym
);
947 show_store_gen(bits
, val
, NULL
, addr
);
952 static int type_is_signed(struct symbol
*sym
)
954 if (sym
->type
== SYM_NODE
)
955 sym
= sym
->ctype
.base_type
;
956 if (sym
->type
== SYM_PTR
)
958 return !(sym
->ctype
.modifiers
& MOD_UNSIGNED
);
961 static int show_cast_expr(struct expression
*expr
)
963 struct symbol
*old_type
, *new_type
;
964 int op
= show_expression(expr
->cast_expression
);
965 int oldbits
, newbits
;
968 old_type
= expr
->cast_expression
->ctype
;
969 new_type
= expr
->cast_type
;
971 oldbits
= old_type
->bit_size
;
972 newbits
= new_type
->bit_size
;
973 if (oldbits
>= newbits
)
976 is_signed
= type_is_signed(old_type
);
978 printf("\tsext%d.%d\tv%d,v%d\n", oldbits
, newbits
, new, op
);
980 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits
, new, op
, (1UL << oldbits
)-1);
985 static int show_value(struct expression
*expr
)
987 int new = new_pseudo();
988 unsigned long long value
= expr
->value
;
990 printf("\tmovi.%d\t\tv%d,$%llu\n", expr
->ctype
->bit_size
, new, value
);
994 static int show_fvalue(struct expression
*expr
)
996 int new = new_pseudo();
997 long double value
= expr
->fvalue
;
999 printf("\tmovf.%d\t\tv%d,$%Lf\n", expr
->ctype
->bit_size
, new, value
);
1003 static int show_string_expr(struct expression
*expr
)
1005 int new = new_pseudo();
1007 printf("\tmovi.%d\t\tv%d,&%s\n", bits_in_pointer
, new, show_string(expr
->string
));
1011 static int show_label_expr(struct expression
*expr
)
1013 int new = new_pseudo();
1014 printf("\tmovi.%d\t\tv%d,.L%p\n",bits_in_pointer
, new, expr
->label_symbol
);
1018 static int show_conditional_expr(struct expression
*expr
)
1020 int cond
= show_expression(expr
->conditional
);
1021 int true = show_expression(expr
->cond_true
);
1022 int false = show_expression(expr
->cond_false
);
1023 int new = new_pseudo();
1025 printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond
, expr
->ctype
->bit_size
, new, true, false);
1029 static int show_statement_expr(struct expression
*expr
)
1031 return show_statement(expr
->statement
);
1034 static int show_position_expr(struct expression
*expr
, struct symbol
*base
)
1036 int new = show_expression(expr
->init_expr
);
1037 struct symbol
*ctype
= expr
->init_expr
->ctype
;
1040 bit_offset
= ctype
? ctype
->bit_offset
: -1;
1042 printf("\tinsert v%d at [%d:%d] of %s\n", new,
1043 expr
->init_offset
, bit_offset
,
1044 show_ident(base
->ident
));
1048 static int show_initializer_expr(struct expression
*expr
, struct symbol
*ctype
)
1050 struct expression
*entry
;
1052 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1055 // Nested initializers have their positions already
1056 // recursively calculated - just output them too
1057 if (entry
->type
== EXPR_INITIALIZER
) {
1058 show_initializer_expr(entry
, ctype
);
1062 // Initializer indexes and identifiers should
1063 // have been evaluated to EXPR_POS
1064 if (entry
->type
== EXPR_IDENTIFIER
) {
1065 printf(" AT '%s':\n", show_ident(entry
->expr_ident
));
1066 entry
= entry
->ident_expression
;
1070 if (entry
->type
== EXPR_INDEX
) {
1071 printf(" AT '%d..%d:\n", entry
->idx_from
, entry
->idx_to
);
1072 entry
= entry
->idx_expression
;
1075 if (entry
->type
== EXPR_POS
) {
1076 show_position_expr(entry
, ctype
);
1079 show_initialization(ctype
, entry
);
1080 } END_FOR_EACH_PTR(entry
);
1084 int show_symbol_expr_init(struct symbol
*sym
)
1086 struct expression
*expr
= sym
->initializer
;
1089 show_expression(expr
);
1090 return show_symbol_expr(sym
);
1094 * Print out an expression. Return the pseudo that contains the
1097 int show_expression(struct expression
*expr
)
1103 struct position
*pos
= &expr
->pos
;
1104 printf("\tno type at %s:%d:%d\n",
1105 stream_name(pos
->stream
),
1106 pos
->line
, pos
->pos
);
1110 switch (expr
->type
) {
1112 return show_call_expression(expr
);
1114 case EXPR_ASSIGNMENT
:
1115 return show_assignment(expr
);
1118 return show_comma(expr
);
1122 return show_binop(expr
);
1124 return show_preop(expr
);
1126 return show_postop(expr
);
1128 return show_symbol_expr(expr
->symbol
);
1131 case EXPR_PTRSIZEOF
:
1134 warning(expr
->pos
, "invalid expression after evaluation");
1137 case EXPR_FORCE_CAST
:
1138 case EXPR_IMPLIED_CAST
:
1139 return show_cast_expr(expr
);
1141 return show_value(expr
);
1143 return show_fvalue(expr
);
1145 return show_string_expr(expr
);
1146 case EXPR_INITIALIZER
:
1147 return show_initializer_expr(expr
, expr
->ctype
);
1149 case EXPR_CONDITIONAL
:
1150 return show_conditional_expr(expr
);
1151 case EXPR_STATEMENT
:
1152 return show_statement_expr(expr
);
1154 return show_label_expr(expr
);
1156 return show_slice(expr
);
1158 // None of these should exist as direct expressions: they are only
1159 // valid as sub-expressions of initializers.
1161 warning(expr
->pos
, "unable to show plain initializer position expression");
1163 case EXPR_IDENTIFIER
:
1164 warning(expr
->pos
, "unable to show identifier expression");
1167 warning(expr
->pos
, "unable to show index expression");
1170 warning(expr
->pos
, "unable to show type expression");