2 * sparse/compile-i386.c
4 * Copyright (C) 2003 Transmeta Corp.
6 * Copyright 2003 Jeff Garzik
8 * Licensed under the Open Software License version 1.1
13 * in general, any non-32bit SYM_BASETYPE is unlikely to work.
14 * complex initializers
16 * global struct/union variables
17 * addressing structures, and members of structures (as opposed to
18 * scalars) on the stack. Requires smarter stack frame allocation.
20 * any function argument that isn't 32 bits (or promoted to such)
39 #include "expression.h"
44 unsigned int len
; /* does NOT include terminating null */
53 struct loop_stack
*next
;
59 struct ptr_list
*pseudo_list
;
60 struct ptr_list
*atom_list
;
61 struct ptr_list
*str_list
;
62 struct loop_stack
*loop_stack
;
69 STOR_PSEUDO
, /* variable stored on the stack */
70 STOR_ARG
, /* function argument */
71 STOR_SYM
, /* a symbol we can directly ref in the asm */
72 STOR_REG
, /* scratch register */
73 STOR_VALUE
, /* integer constant */
74 STOR_LABEL
, /* label / jump target */
75 STOR_LABELSYM
, /* label generated from symbol's pointer value */
83 enum storage_type type
;
114 struct symbol
*labelsym
;
120 STOR_LABEL_VAL
= (1 << 0),
121 STOR_WANTS_FREE
= (1 << 1),
124 struct symbol_private
{
125 struct storage
*addr
;
140 unsigned int text_len
; /* w/o terminating null */
143 /* stuff for insns */
151 /* stuff for C strings */
153 struct string
*string
;
160 struct function
*current_func
= NULL
;
161 struct textbuf
*unit_post_text
= NULL
;
162 static const char *current_section
;
164 static struct reg_info reg_info_table
[] = {
175 static struct storage hardreg_storage_table
[] = {
178 .reg
= ®_info_table
[0],
183 .reg
= ®_info_table
[1],
188 .reg
= ®_info_table
[2],
193 .reg
= ®_info_table
[3],
198 .reg
= ®_info_table
[4],
203 .reg
= ®_info_table
[5],
208 .reg
= ®_info_table
[6],
213 .reg
= ®_info_table
[7],
217 #define REG_EAX (&hardreg_storage_table[0])
218 #define REG_ECX (&hardreg_storage_table[1])
219 #define REG_EDX (&hardreg_storage_table[2])
220 #define REG_ESP (&hardreg_storage_table[3])
221 #define REG_DL (&hardreg_storage_table[4])
222 #define REG_DX (&hardreg_storage_table[5])
223 #define REG_AL (&hardreg_storage_table[6])
224 #define REG_AX (&hardreg_storage_table[7])
227 static void emit_move(struct storage
*src
, struct storage
*dest
,
228 struct symbol
*ctype
, const char *comment
);
229 static int type_is_signed(struct symbol
*sym
);
230 static struct storage
*x86_address_gen(struct expression
*expr
);
231 static struct storage
*x86_symbol_expr(struct symbol
*sym
);
232 static void x86_symbol(struct symbol
*sym
);
233 static struct storage
*x86_statement(struct statement
*stmt
);
234 static struct storage
*x86_expression(struct expression
*expr
);
237 static inline unsigned int pseudo_offset(struct storage
*s
)
239 if (s
->type
!= STOR_PSEUDO
)
240 return 123456; /* intentionally bogus value */
245 static inline unsigned int arg_offset(struct storage
*s
)
247 if (s
->type
!= STOR_ARG
)
248 return 123456; /* intentionally bogus value */
250 /* FIXME: this is wrong wrong wrong */
251 return current_func
->stack_size
+ ((1 + s
->idx
) * 4);
254 static const char *pretty_offset(int ofs
)
256 static char esp_buf
[64];
259 sprintf(esp_buf
, "%d(%%esp)", ofs
);
261 strcpy(esp_buf
, "(%esp)");
266 static void stor_sym_init(struct symbol
*sym
)
268 struct storage
*stor
;
269 struct symbol_private
*priv
;
271 priv
= calloc(1, sizeof(*priv
) + sizeof(*stor
));
273 die("OOM in stor_sym_init");
275 stor
= (struct storage
*) (priv
+ 1);
278 stor
->type
= STOR_SYM
;
282 static const char *stor_op_name(struct storage
*s
)
284 static char name
[32];
288 strcpy(name
, pretty_offset((int) pseudo_offset(s
)));
291 strcpy(name
, pretty_offset((int) arg_offset(s
)));
294 strcpy(name
, show_ident(s
->sym
->ident
));
297 strcpy(name
, s
->reg
->name
);
300 sprintf(name
, "$%Ld", s
->value
);
303 sprintf(name
, "%s.L%d", s
->flags
& STOR_LABEL_VAL
? "$" : "",
307 sprintf(name
, "%s.LS%p", s
->flags
& STOR_LABEL_VAL
? "$" : "",
315 static struct atom
*new_atom(enum atom_type type
)
319 atom
= calloc(1, sizeof(*atom
)); /* TODO: chunked alloc */
328 static inline void push_cstring(struct function
*f
, struct string
*str
,
333 atom
= new_atom(ATOM_CSTR
);
337 add_ptr_list(&f
->str_list
, atom
); /* note: _not_ atom_list */
340 static inline void push_atom(struct function
*f
, struct atom
*atom
)
342 add_ptr_list(&f
->atom_list
, atom
);
345 static void push_text_atom(struct function
*f
, const char *text
)
347 struct atom
*atom
= new_atom(ATOM_TEXT
);
349 atom
->text
= strdup(text
);
350 atom
->text_len
= strlen(text
);
355 static struct storage
*new_storage(enum storage_type type
)
357 struct storage
*stor
;
359 stor
= calloc(1, sizeof(*stor
));
361 die("OOM in new_storage");
368 static struct storage
*stack_alloc(int n_bytes
)
370 struct function
*f
= current_func
;
371 struct storage
*stor
;
375 stor
= new_storage(STOR_PSEUDO
);
376 stor
->type
= STOR_PSEUDO
;
377 stor
->pseudo
= f
->pseudo_nr
;
378 stor
->offset
= f
->stack_size
;
379 stor
->size
= n_bytes
;
380 f
->stack_size
+= n_bytes
;
383 add_ptr_list(&f
->pseudo_list
, stor
);
388 static struct storage
*new_labelsym(struct symbol
*sym
)
390 struct storage
*stor
;
392 stor
= new_storage(STOR_LABELSYM
);
395 stor
->flags
|= STOR_WANTS_FREE
;
396 stor
->labelsym
= sym
;
402 static struct storage
*new_val(long long value
)
404 struct storage
*stor
;
406 stor
= new_storage(STOR_VALUE
);
409 stor
->flags
|= STOR_WANTS_FREE
;
416 static int new_label(void)
418 static int label
= 0;
422 static void textbuf_push(struct textbuf
**buf_p
, const char *text
)
424 struct textbuf
*tmp
, *list
= *buf_p
;
425 unsigned int text_len
= strlen(text
);
426 unsigned int alloc_len
= text_len
+ 1 + sizeof(*list
);
428 tmp
= calloc(1, alloc_len
);
430 die("OOM on textbuf alloc");
432 tmp
->text
= ((void *) tmp
) + sizeof(*tmp
);
433 memcpy(tmp
->text
, text
, text_len
+ 1);
436 /* add to end of list */
441 tmp
->prev
= list
->prev
;
442 tmp
->prev
->next
= tmp
;
450 static void textbuf_emit(struct textbuf
**buf_p
)
452 struct textbuf
*tmp
, *list
= *buf_p
;
456 if (tmp
->next
== tmp
)
459 tmp
->prev
->next
= tmp
->next
;
460 tmp
->next
->prev
= tmp
->prev
;
464 fputs(tmp
->text
, stdout
);
472 static void insn(const char *insn
, struct storage
*op1
, struct storage
*op2
,
473 const char *comment_in
)
475 struct function
*f
= current_func
;
476 struct atom
*atom
= new_atom(ATOM_INSN
);
478 assert(insn
!= NULL
);
480 strcpy(atom
->insn
, insn
);
481 if (comment_in
&& (*comment_in
))
482 strncpy(atom
->comment
, comment_in
,
483 sizeof(atom
->comment
) - 1);
491 static void emit_label (int label
, const char *comment
)
493 struct function
*f
= current_func
;
497 sprintf(s
, ".L%d:\n", label
);
499 sprintf(s
, ".L%d:\t\t\t\t\t# %s\n", label
, comment
);
501 push_text_atom(f
, s
);
504 static void emit_labelsym (struct symbol
*sym
, const char *comment
)
506 struct function
*f
= current_func
;
510 sprintf(s
, ".LS%p:\n", sym
);
512 sprintf(s
, ".LS%p:\t\t\t\t# %s\n", sym
, comment
);
514 push_text_atom(f
, s
);
517 static void emit_unit_pre(const char *basename
)
519 printf("\t.file\t\"%s\"\n", basename
);
522 static void emit_unit_post(void)
524 textbuf_emit(&unit_post_text
);
525 printf("\t.ident\t\"sparse silly x86 backend (built %s)\"\n", __DATE__
);
528 /* conditionally switch sections */
529 static void emit_section(const char *s
)
531 if (s
== current_section
)
533 if (current_section
&& (!strcmp(s
, current_section
)))
540 static void emit_insn_atom(struct function
*f
, struct atom
*atom
)
544 struct storage
*op1
= atom
->op1
;
545 struct storage
*op2
= atom
->op2
;
547 if (atom
->comment
[0])
548 sprintf(comment
, "\t\t# %s", atom
->comment
);
554 strcpy(tmp
, stor_op_name(op1
));
555 sprintf(s
, "\t%s\t%s, %s%s\n",
556 atom
->insn
, tmp
, stor_op_name(op2
), comment
);
557 } else if (atom
->op1
)
558 sprintf(s
, "\t%s\t%s%s%s\n",
559 atom
->insn
, stor_op_name(op1
),
560 comment
[0] ? "\t" : "", comment
);
562 sprintf(s
, "\t%s\t%s%s\n",
564 comment
[0] ? "\t\t" : "", comment
);
566 write(STDOUT_FILENO
, s
, strlen(s
));
569 static void emit_atom_list(struct function
*f
)
573 FOR_EACH_PTR(f
->atom_list
, atom
) {
574 switch (atom
->type
) {
576 ssize_t rc
= write(STDOUT_FILENO
, atom
->text
,
578 (void) rc
; /* FIXME */
582 emit_insn_atom(f
, atom
);
591 static void emit_string_list(struct function
*f
)
595 emit_section(".section\t.rodata");
597 FOR_EACH_PTR(f
->str_list
, atom
) {
598 /* FIXME: escape " in string */
599 printf(".L%d:\n", atom
->label
);
600 printf("\t.string\t%s\n", show_string(atom
->string
));
606 static void func_cleanup(struct function
*f
)
608 struct storage
*stor
;
611 FOR_EACH_PTR(f
->pseudo_list
, stor
) {
615 FOR_EACH_PTR(f
->atom_list
, atom
) {
616 if ((atom
->type
== ATOM_TEXT
) && (atom
->text
))
618 if (atom
->op1
&& (atom
->op1
->flags
& STOR_WANTS_FREE
))
620 if (atom
->op2
&& (atom
->op2
->flags
& STOR_WANTS_FREE
))
625 free_ptr_list(&f
->pseudo_list
);
629 /* function prologue */
630 static void emit_func_pre(struct symbol
*sym
)
634 unsigned int i
, argc
= 0, alloc_len
;
636 struct symbol_private
*privbase
;
637 struct storage
*storage_base
;
638 struct symbol
*base_type
= sym
->ctype
.base_type
;
640 FOR_EACH_PTR(base_type
->arguments
, arg
) {
646 (argc
* sizeof(struct symbol
*)) +
647 (argc
* sizeof(struct symbol_private
)) +
648 (argc
* sizeof(struct storage
));
649 mem
= calloc(1, alloc_len
);
651 die("OOM on func info");
653 f
= (struct function
*) mem
;
655 f
->argv
= (struct symbol
**) mem
;
656 mem
+= (argc
* sizeof(struct symbol
*));
657 privbase
= (struct symbol_private
*) mem
;
658 mem
+= (argc
* sizeof(struct symbol_private
));
659 storage_base
= (struct storage
*) mem
;
662 f
->ret_target
= new_label();
665 FOR_EACH_PTR(base_type
->arguments
, arg
) {
667 arg
->aux
= &privbase
[i
];
668 storage_base
[i
].type
= STOR_ARG
;
669 storage_base
[i
].idx
= i
;
670 privbase
[i
].addr
= &storage_base
[i
];
674 assert(current_func
== NULL
);
678 /* function epilogue */
679 static void emit_func_post(struct symbol
*sym
)
681 const char *name
= show_ident(sym
->ident
);
682 struct function
*f
= current_func
;
683 int stack_size
= f
->stack_size
;
688 /* function prologue */
689 emit_section(".text");
690 if ((sym
->ctype
.modifiers
& MOD_STATIC
) == 0)
691 printf(".globl %s\n", name
);
692 printf("\t.type\t%s, @function\n", name
);
693 printf("%s:\n", name
);
696 char pseudo_const
[16];
698 sprintf(pseudo_const
, "$%d", stack_size
);
699 printf("\tsubl\t%s, %%esp\n", pseudo_const
);
702 /* function epilogue */
704 /* jump target for 'return' statements */
705 emit_label(f
->ret_target
, NULL
);
710 val
= new_storage(STOR_VALUE
);
711 val
->value
= (long long) (stack_size
);
712 val
->flags
= STOR_WANTS_FREE
;
714 insn("addl", val
, REG_ESP
, NULL
);
717 insn("ret", NULL
, NULL
, NULL
);
719 /* output everything to stdout */
720 fflush(stdout
); /* paranoia; needed? */
723 /* function footer */
724 name
= show_ident(sym
->ident
);
725 printf("\t.size\t%s, .-%s\n", name
, name
);
731 /* emit object (a.k.a. variable, a.k.a. data) prologue */
732 static void emit_object_pre(const char *name
, unsigned long modifiers
,
733 unsigned long alignment
, unsigned int byte_size
)
735 if ((modifiers
& MOD_STATIC
) == 0)
736 printf(".globl %s\n", name
);
737 emit_section(".data");
739 printf("\t.align %lu\n", alignment
);
740 printf("\t.type\t%s, @object\n", name
);
741 printf("\t.size\t%s, %d\n", name
, byte_size
);
742 printf("%s:\n", name
);
745 /* emit value (only) for an initializer scalar */
746 static void emit_scalar(struct expression
*expr
, unsigned int bit_size
)
751 assert(expr
->type
== EXPR_VALUE
);
753 if (expr
->value
== 0ULL) {
754 printf("\t.zero\t%d\n", bit_size
/ 8);
758 ll
= (long long) expr
->value
;
761 case 8: type
= "byte"; ll
= (char) ll
; break;
762 case 16: type
= "value"; ll
= (short) ll
; break;
763 case 32: type
= "long"; ll
= (int) ll
; break;
764 case 64: type
= "quad"; break;
765 default: type
= NULL
; break;
768 assert(type
!= NULL
);
770 printf("\t.%s\t%Ld\n", type
, ll
);
773 static void emit_global_noinit(const char *name
, unsigned long modifiers
,
774 unsigned long alignment
, unsigned int byte_size
)
778 if (modifiers
& MOD_STATIC
) {
779 sprintf(s
, "\t.local\t%s\n", name
);
780 textbuf_push(&unit_post_text
, s
);
783 sprintf(s
, "\t.comm\t%s,%d,%lu\n", name
, byte_size
, alignment
);
785 sprintf(s
, "\t.comm\t%s,%d\n", name
, byte_size
);
786 textbuf_push(&unit_post_text
, s
);
789 static int ea_current
, ea_last
;
791 static void emit_initializer(struct symbol
*sym
,
792 struct expression
*expr
)
794 int distance
= ea_current
- ea_last
- 1;
797 printf("\t.zero\t%d\n", (sym
->bit_size
/ 8) * distance
);
799 if (expr
->type
== EXPR_VALUE
) {
800 struct symbol
*base_type
= sym
->ctype
.base_type
;
801 assert(base_type
!= NULL
);
803 emit_scalar(expr
, sym
->bit_size
/ get_expression_value(base_type
->array_size
));
806 if (expr
->type
!= EXPR_INITIALIZER
)
809 assert(0); /* FIXME */
812 static int sort_array_cmp(const struct expression
*a
,
813 const struct expression
*b
)
815 int a_ofs
= 0, b_ofs
= 0;
817 if (a
->type
== EXPR_POS
)
818 a_ofs
= (int) a
->init_offset
;
819 if (b
->type
== EXPR_POS
)
820 b_ofs
= (int) b
->init_offset
;
822 return a_ofs
- b_ofs
;
825 /* move to front-end? */
826 static void sort_array(struct expression
*expr
)
828 struct expression
*entry
, **list
;
829 unsigned int elem
, sorted
, i
;
832 FOR_EACH_PTR(expr
->expr_list
, entry
) {
839 list
= malloc(sizeof(entry
) * elem
);
841 die("OOM in sort_array");
843 /* this code is no doubt evil and ignores EXPR_INDEX possibly
844 * to its detriment and other nasty things. improvements
849 FOR_EACH_PTR(expr
->expr_list
, entry
) {
850 if ((entry
->type
== EXPR_POS
) || (entry
->type
== EXPR_VALUE
)) {
851 /* add entry to list[], in sorted order */
858 for (i
= 0; i
< sorted
; i
++)
859 if (sort_array_cmp(entry
, list
[i
]) <= 0)
862 /* If inserting into the middle of list[]
863 * instead of appending, we memmove.
864 * This is ugly, but thankfully
865 * uncommon. Input data with tons of
866 * entries very rarely have explicit
867 * offsets. convert to qsort eventually...
870 memmove(&list
[i
+ 1], &list
[i
],
871 (sorted
- i
) * sizeof(entry
));
879 FOR_EACH_PTR(expr
->expr_list
, entry
) {
880 if ((entry
->type
== EXPR_POS
) || (entry
->type
== EXPR_VALUE
))
881 __list
->list
[__i
] = list
[i
++];
886 static void emit_array(struct symbol
*sym
)
888 struct symbol
*base_type
= sym
->ctype
.base_type
;
889 struct expression
*expr
= sym
->initializer
;
890 struct expression
*entry
;
892 assert(base_type
!= NULL
);
898 emit_object_pre(show_ident(sym
->ident
), sym
->ctype
.modifiers
,
899 sym
->ctype
.alignment
,
904 FOR_EACH_PTR(expr
->expr_list
, entry
) {
905 if (entry
->type
== EXPR_VALUE
) {
907 emit_initializer(sym
, entry
);
908 ea_last
= ea_current
;
909 } else if (entry
->type
== EXPR_POS
) {
911 entry
->init_offset
/ (base_type
->bit_size
/ 8);
912 emit_initializer(sym
, entry
->init_expr
);
913 ea_last
= ea_current
;
918 static void emit_one_symbol(struct symbol
*sym
, void *dummy
, int flags
)
923 void emit_unit(const char *basename
, struct symbol_list
*list
)
925 emit_unit_pre(basename
);
926 symbol_iterate(list
, emit_one_symbol
, NULL
);
930 static void emit_copy(struct storage
*dest
, struct storage
*src
,
931 struct symbol
*ctype
)
933 struct storage
*reg
= NULL
;
935 /* FIXME: Bitfield move! */
937 switch (ctype
->bit_size
) {
948 reg
= REG_EAX
; /* FIXME */
955 emit_move(src
, reg
, ctype
, "begin copy ..");
956 emit_move(reg
, dest
, ctype
, ".... end copy");
959 static void emit_store(struct expression
*dest_expr
, struct storage
*dest
,
960 struct storage
*src
, int bits
)
962 /* FIXME: Bitfield store! */
963 printf("\tst.%d\t\tv%d,[v%d]\n", bits
, src
->pseudo
, dest
->pseudo
);
966 static void emit_scalar_noinit(struct symbol
*sym
)
968 emit_global_noinit(show_ident(sym
->ident
),
969 sym
->ctype
.modifiers
, sym
->ctype
.alignment
,
974 static void emit_array_noinit(struct symbol
*sym
)
976 emit_global_noinit(show_ident(sym
->ident
),
977 sym
->ctype
.modifiers
, sym
->ctype
.alignment
,
978 get_expression_value(sym
->array_size
) * (sym
->bit_size
/ 8));
982 static const char *opbits(const char *insn
, unsigned int bits
)
984 static char opbits_str
[32];
988 case 8: c
= 'b'; break;
989 case 16: c
= 'w'; break;
990 case 32: c
= 'l'; break;
991 case 64: c
= 'q'; break;
992 default: assert(0); break;
995 sprintf(opbits_str
, "%s%c", insn
, bits
);
1000 static void emit_move(struct storage
*src
, struct storage
*dest
,
1001 struct symbol
*ctype
, const char *comment
)
1004 unsigned int is_signed
;
1005 unsigned int is_dest
= (src
->type
== STOR_REG
);
1009 bits
= ctype
->bit_size
;
1010 is_signed
= type_is_signed(ctype
);
1016 if ((dest
->type
== STOR_REG
) && (src
->type
== STOR_REG
)) {
1017 insn("mov", src
, dest
, NULL
);
1026 if (is_signed
) opname
= "movsxb";
1027 else opname
= "movzxb";
1034 if (is_signed
) opname
= "movsxw";
1035 else opname
= "movzxw";
1039 case 32: opname
= "movl"; break;
1040 case 64: opname
= "movq"; break;
1042 default: assert(0); break;
1045 insn(opname
, src
, dest
, comment
);
1048 static struct storage
*emit_compare(struct expression
*expr
)
1050 struct storage
*left
= x86_expression(expr
->left
);
1051 struct storage
*right
= x86_expression(expr
->right
);
1052 struct storage
*new, *val
;
1053 const char *opname
= NULL
;
1054 unsigned int is_signed
= type_is_signed(expr
->left
->ctype
); /* FIXME */
1055 unsigned int right_bits
= expr
->right
->ctype
->bit_size
;
1059 if (is_signed
) opname
= "setl";
1060 else opname
= "setb";
1063 if (is_signed
) opname
= "setg";
1064 else opname
= "seta";
1067 if (is_signed
) opname
= "setle";
1068 else opname
= "setbe";
1071 if (is_signed
) opname
= "setge";
1072 else opname
= "setae";
1075 case SPECIAL_EQUAL
: opname
= "sete"; break;
1076 case SPECIAL_NOTEQUAL
: opname
= "setne"; break;
1084 val
= new_storage(STOR_VALUE
);
1085 val
->flags
= STOR_WANTS_FREE
;
1086 emit_move(val
, REG_EDX
, NULL
, NULL
);
1088 /* move op1 into EAX */
1089 emit_move(left
, REG_EAX
, expr
->left
->ctype
, NULL
);
1091 /* perform comparison, RHS (op1, right) and LHS (op2, EAX) */
1092 insn(opbits("cmp", right_bits
), right
, REG_EAX
, NULL
);
1094 /* store result of operation, 0 or 1, in DL using SETcc */
1095 insn(opname
, REG_DL
, NULL
, NULL
);
1097 /* finally, store the result (DL) in a new pseudo / stack slot */
1098 new = stack_alloc(4);
1099 emit_move(REG_EDX
, new, NULL
, "end EXPR_COMPARE");
1104 static struct storage
*emit_value(struct expression
*expr
)
1106 #if 0 /* old and slow way */
1107 struct storage
*new = stack_alloc(4);
1108 struct storage
*val
;
1110 val
= new_storage(STOR_VALUE
);
1111 val
->value
= (long long) expr
->value
;
1112 val
->flags
= STOR_WANTS_FREE
;
1113 insn("movl", val
, new, NULL
);
1117 struct storage
*val
;
1119 val
= new_storage(STOR_VALUE
);
1120 val
->value
= (long long) expr
->value
;
1122 return val
; /* FIXME: memory leak */
1126 static struct storage
*emit_binop(struct expression
*expr
)
1128 struct storage
*left
= x86_expression(expr
->left
);
1129 struct storage
*right
= x86_expression(expr
->right
);
1130 struct storage
*new;
1131 struct storage
*accum_reg
= NULL
;
1132 struct storage
*result_reg
= NULL
;
1133 const char *opname
= NULL
;
1135 char movstr
[16], opstr
[16];
1136 int is_signed
, doing_divide
= 0;
1138 if ((expr
->op
== '/') || (expr
->op
== '%')) {
1141 struct storage
*val
= new_storage(STOR_VALUE
);
1142 val
->flags
= STOR_WANTS_FREE
;
1143 emit_move(val
, REG_EDX
, NULL
, "begin EXPR_DIVIDE");
1146 is_signed
= type_is_signed(expr
->ctype
);
1164 case SPECIAL_LEFTSHIFT
:
1167 case SPECIAL_RIGHTSHIFT
:
1191 switch (expr
->ctype
->bit_size
) {
1194 result_reg
= accum_reg
= REG_AL
;
1195 if (expr
->op
== '%')
1196 result_reg
= REG_DL
;
1200 result_reg
= accum_reg
= REG_AX
;
1201 if (expr
->op
== '%')
1202 result_reg
= REG_DX
;
1206 result_reg
= accum_reg
= REG_EAX
;
1207 if (expr
->op
== '%')
1208 result_reg
= REG_EDX
;
1211 suffix
= "q"; /* FIXME */
1212 result_reg
= accum_reg
= REG_EAX
; /* FIXME */
1213 if (expr
->op
== '%')
1214 result_reg
= REG_EDX
;
1221 sprintf(movstr
, "mov%s", suffix
);
1222 sprintf(opstr
, "%s%s", opname
, suffix
);
1224 /* load op2 into EAX */
1225 insn(movstr
, right
, accum_reg
,
1226 doing_divide
? NULL
: "EXPR_BINOP/COMMA/LOGICAL");
1229 insn(opstr
, left
, accum_reg
, NULL
);
1231 /* store result (EAX or EDX) in new pseudo / stack slot */
1232 new = stack_alloc(4);
1233 insn(movstr
, result_reg
, new,
1234 doing_divide
? "end EXPR_DIVIDE" : "end EXPR_BINOP");
1239 static void emit_if_conditional(struct statement
*stmt
)
1241 struct storage
*val
, *target_val
;
1243 struct expression
*cond
= stmt
->if_conditional
;
1245 /* This is only valid if nobody can jump into the "dead" statement */
1247 if (cond
->type
== EXPR_VALUE
) {
1248 struct statement
*s
= stmt
->if_true
;
1255 val
= x86_expression(cond
);
1257 /* load 'if' test result into EAX */
1258 insn("movl", val
, REG_EAX
, "begin if conditional");
1260 /* compare 'if' test result */
1261 insn("test", REG_EAX
, REG_EAX
, NULL
);
1263 /* create end-of-if label / if-failed labelto jump to,
1264 * and jump to it if the expression returned zero.
1266 target
= new_label();
1267 target_val
= new_storage(STOR_LABEL
);
1268 target_val
->label
= target
;
1269 target_val
->flags
= STOR_WANTS_FREE
;
1270 insn("jz", target_val
, NULL
, NULL
);
1272 x86_statement(stmt
->if_true
);
1273 if (stmt
->if_false
) {
1274 struct storage
*last_val
;
1277 /* finished generating code for if-true statement.
1278 * add a jump-to-end jump to avoid falling through
1279 * to the if-false statement code.
1282 last_val
= new_storage(STOR_LABEL
);
1283 last_val
->label
= last
;
1284 last_val
->flags
= STOR_WANTS_FREE
;
1285 insn("jmp", last_val
, NULL
, NULL
);
1287 /* if we have both if-true and if-false statements,
1288 * the failed-conditional case will fall through to here
1290 emit_label(target
, NULL
);
1293 x86_statement(stmt
->if_false
);
1296 emit_label(target
, "end if");
1299 static struct storage
*emit_inc_dec(struct expression
*expr
, int postop
)
1301 struct storage
*addr
= x86_address_gen(expr
->unop
);
1302 struct storage
*retval
;
1305 strcpy(opname
, opbits(expr
->op
== SPECIAL_INCREMENT
? "inc" : "dec",
1306 expr
->ctype
->bit_size
));
1309 struct storage
*new = stack_alloc(4);
1311 emit_copy(new, addr
, expr
->unop
->ctype
);
1317 insn(opname
, addr
, NULL
, NULL
);
1322 static struct storage
*emit_postop(struct expression
*expr
)
1324 return emit_inc_dec(expr
, 1);
1327 static struct storage
*emit_return_stmt(struct statement
*stmt
)
1329 struct function
*f
= current_func
;
1330 struct expression
*expr
= stmt
->ret_value
;
1331 struct storage
*val
= NULL
, *jmplbl
;
1333 if (expr
&& expr
->ctype
) {
1334 val
= x86_expression(expr
);
1335 assert(val
!= NULL
);
1336 emit_move(val
, REG_EAX
, expr
->ctype
, "return");
1339 jmplbl
= new_storage(STOR_LABEL
);
1340 jmplbl
->flags
|= STOR_WANTS_FREE
;
1341 jmplbl
->label
= f
->ret_target
;
1342 insn("jmp", jmplbl
, NULL
, NULL
);
1347 static struct storage
*emit_conditional_expr(struct expression
*expr
)
1349 struct storage
*cond
= x86_expression(expr
->conditional
);
1350 struct storage
*true = x86_expression(expr
->cond_true
);
1351 struct storage
*false = x86_expression(expr
->cond_false
);
1352 struct storage
*new = stack_alloc(4);
1357 emit_move(cond
, REG_EAX
, expr
->conditional
->ctype
,
1358 "begin EXPR_CONDITIONAL");
1359 emit_move(true, REG_ECX
, expr
->cond_true
->ctype
, NULL
);
1360 emit_move(false, REG_EDX
, expr
->cond_false
->ctype
, NULL
);
1362 /* test EAX (for zero/non-zero) */
1363 insn("test", REG_EAX
, REG_EAX
, NULL
);
1365 /* if false, move EDX to ECX */
1366 insn("cmovz", REG_EDX
, REG_ECX
, NULL
);
1368 /* finally, store the result (ECX) in a new pseudo / stack slot */
1369 new = stack_alloc(4);
1370 emit_move(REG_ECX
, new, expr
->ctype
, "end EXPR_CONDITIONAL");
1371 /* FIXME: we lose type knowledge of expression result at this point */
1376 static struct storage
*emit_symbol_expr_init(struct symbol
*sym
)
1378 struct expression
*expr
= sym
->initializer
;
1379 struct symbol_private
*priv
= sym
->aux
;
1382 priv
= calloc(1, sizeof(*priv
));
1386 struct storage
*new = stack_alloc(4);
1387 fprintf(stderr
, "FIXME! no value for symbol. creating pseudo %d (stack offset %d)\n",
1388 new->pseudo
, new->pseudo
* 4);
1391 priv
->addr
= x86_expression(expr
);
1398 static struct storage
*emit_string_expr(struct expression
*expr
)
1400 struct function
*f
= current_func
;
1401 int label
= new_label();
1402 struct storage
*new;
1404 push_cstring(f
, expr
->string
, label
);
1406 new = new_storage(STOR_LABEL
);
1408 new->flags
= STOR_LABEL_VAL
| STOR_WANTS_FREE
;
1412 static struct storage
*emit_cast_expr(struct expression
*expr
)
1414 struct symbol
*old_type
, *new_type
;
1415 struct storage
*op
= x86_expression(expr
->cast_expression
);
1416 int oldbits
, newbits
;
1417 struct storage
*new;
1419 old_type
= expr
->cast_expression
->ctype
;
1420 new_type
= expr
->cast_type
;
1422 oldbits
= old_type
->bit_size
;
1423 newbits
= new_type
->bit_size
;
1424 if (oldbits
>= newbits
)
1427 emit_move(op
, REG_EAX
, old_type
, "begin cast ..");
1429 new = stack_alloc(4);
1430 emit_move(REG_EAX
, new, new_type
, ".... end cast");
1435 static struct storage
*emit_regular_preop(struct expression
*expr
)
1437 struct storage
*target
= x86_expression(expr
->unop
);
1438 struct storage
*val
, *new = stack_alloc(4);
1439 const char *opname
= NULL
;
1443 val
= new_storage(STOR_VALUE
);
1444 val
->flags
= STOR_WANTS_FREE
;
1445 emit_move(val
, REG_EDX
, NULL
, NULL
);
1446 emit_move(target
, REG_EAX
, expr
->unop
->ctype
, NULL
);
1447 insn("test", REG_EAX
, REG_EAX
, NULL
);
1448 insn("setz", REG_DL
, NULL
, NULL
);
1449 emit_move(REG_EDX
, new, expr
->unop
->ctype
, NULL
);
1457 emit_move(target
, REG_EAX
, expr
->unop
->ctype
, NULL
);
1458 insn(opname
, REG_EAX
, NULL
, NULL
);
1459 emit_move(REG_EAX
, new, expr
->unop
->ctype
, NULL
);
1469 static void emit_case_statement(struct statement
*stmt
)
1471 emit_labelsym(stmt
->case_label
, NULL
);
1472 x86_statement(stmt
->case_statement
);
1475 static void emit_switch_statement(struct statement
*stmt
)
1477 struct storage
*val
= x86_expression(stmt
->switch_expression
);
1478 struct symbol
*sym
, *default_sym
= NULL
;
1479 struct storage
*labelsym
, *label
;
1482 emit_move(val
, REG_EAX
, stmt
->switch_expression
->ctype
, "begin case");
1485 * This is where a _real_ back-end would go through the
1486 * cases to decide whether to use a lookup table or a
1487 * series of comparisons etc
1489 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
1490 struct statement
*case_stmt
= sym
->stmt
;
1491 struct expression
*expr
= case_stmt
->case_expression
;
1492 struct expression
*to
= case_stmt
->case_to
;
1500 struct storage
*case_val
= new_val(expr
->value
);
1502 assert (expr
->type
== EXPR_VALUE
);
1504 insn("cmpl", case_val
, REG_EAX
, NULL
);
1507 labelsym
= new_labelsym(sym
);
1508 insn("je", labelsym
, NULL
, NULL
);
1512 label
= new_storage(STOR_LABEL
);
1513 label
->flags
|= STOR_WANTS_FREE
;
1514 label
->label
= next_test
= new_label();
1516 /* FIXME: signed/unsigned */
1517 insn("jl", label
, NULL
, NULL
);
1519 case_val
= new_val(to
->value
);
1520 insn("cmpl", case_val
, REG_EAX
, NULL
);
1522 /* TODO: implement and use refcounting... */
1523 label
= new_storage(STOR_LABEL
);
1524 label
->flags
|= STOR_WANTS_FREE
;
1525 label
->label
= next_test
;
1527 /* FIXME: signed/unsigned */
1528 insn("jg", label
, NULL
, NULL
);
1530 labelsym
= new_labelsym(sym
);
1531 insn("jmp", labelsym
, NULL
, NULL
);
1533 emit_label(next_test
, NULL
);
1539 labelsym
= new_labelsym(default_sym
);
1540 insn("jmp", labelsym
, NULL
, "default");
1542 label
= new_storage(STOR_LABEL
);
1543 label
->flags
|= STOR_WANTS_FREE
;
1544 label
->label
= switch_end
= new_label();
1545 insn("jmp", label
, NULL
, "goto end of switch");
1548 x86_statement(stmt
->switch_statement
);
1550 if (stmt
->switch_break
->used
)
1551 emit_labelsym(stmt
->switch_break
, NULL
);
1554 emit_label(switch_end
, NULL
);
1557 static void x86_struct_member(struct symbol
*sym
, void *data
, int flags
)
1559 if (flags
& ITERATE_FIRST
)
1561 printf("%s:%d:%ld at offset %ld", show_ident(sym
->ident
), sym
->bit_size
, sym
->ctype
.alignment
, sym
->offset
);
1562 if (sym
->fieldwidth
)
1563 printf("[%d..%d]", sym
->bit_offset
, sym
->bit_offset
+sym
->fieldwidth
-1);
1564 if (flags
& ITERATE_LAST
)
1570 static void x86_symbol(struct symbol
*sym
)
1572 struct symbol
*type
;
1577 type
= sym
->ctype
.base_type
;
1582 * Show actual implementation information
1584 switch (type
->type
) {
1587 if (sym
->initializer
)
1590 emit_array_noinit(sym
);
1594 if (sym
->initializer
) {
1595 emit_object_pre(show_ident(sym
->ident
),
1596 sym
->ctype
.modifiers
,
1597 sym
->ctype
.alignment
,
1599 emit_scalar(sym
->initializer
, sym
->bit_size
);
1602 emit_scalar_noinit(sym
);
1606 symbol_iterate(type
->symbol_list
, x86_struct_member
, NULL
);
1610 symbol_iterate(type
->symbol_list
, x86_struct_member
, NULL
);
1614 struct statement
*stmt
= type
->stmt
;
1617 x86_statement(stmt
);
1618 emit_func_post(sym
);
1627 if (sym
->initializer
&& (type
->type
!= SYM_BASETYPE
) &&
1628 (type
->type
!= SYM_ARRAY
)) {
1630 x86_expression(sym
->initializer
);
1634 static void x86_symbol_init(struct symbol
*sym
);
1636 static void x86_symbol_decl(struct symbol_list
*syms
)
1639 FOR_EACH_PTR(syms
, sym
) {
1640 x86_symbol_init(sym
);
1644 static void loopstk_push(int cont_lbl
, int loop_bottom_lbl
)
1646 struct function
*f
= current_func
;
1647 struct loop_stack
*ls
;
1649 ls
= malloc(sizeof(*ls
));
1650 ls
->continue_lbl
= cont_lbl
;
1651 ls
->loop_bottom_lbl
= loop_bottom_lbl
;
1652 ls
->next
= f
->loop_stack
;
1656 static void loopstk_pop(void)
1658 struct function
*f
= current_func
;
1659 struct loop_stack
*ls
;
1661 assert(f
->loop_stack
!= NULL
);
1663 f
->loop_stack
= f
->loop_stack
->next
;
1667 static int loopstk_break(void)
1669 return current_func
->loop_stack
->loop_bottom_lbl
;
1672 static int loopstk_continue(void)
1674 return current_func
->loop_stack
->continue_lbl
;
1677 static void emit_loop(struct statement
*stmt
)
1679 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
1680 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
1681 struct statement
*statement
= stmt
->iterator_statement
;
1682 struct statement
*post_statement
= stmt
->iterator_post_statement
;
1683 struct expression
*post_condition
= stmt
->iterator_post_condition
;
1684 int loop_top
= 0, loop_bottom
, loop_continue
;
1685 int have_bottom
= 0;
1686 struct storage
*val
;
1688 loop_bottom
= new_label();
1689 loop_continue
= new_label();
1690 loopstk_push(loop_continue
, loop_bottom
);
1692 x86_symbol_decl(stmt
->iterator_syms
);
1693 x86_statement(pre_statement
);
1694 if (pre_condition
) {
1695 if (pre_condition
->type
== EXPR_VALUE
) {
1696 if (!pre_condition
->value
) {
1697 struct storage
*lbv
;
1698 lbv
= new_storage(STOR_LABEL
);
1699 lbv
->label
= loop_bottom
;
1700 lbv
->flags
= STOR_WANTS_FREE
;
1701 insn("jmp", lbv
, NULL
, "go to loop bottom");
1705 struct storage
*lbv
= new_storage(STOR_LABEL
);
1706 lbv
->label
= loop_bottom
;
1707 lbv
->flags
= STOR_WANTS_FREE
;
1710 val
= x86_expression(pre_condition
);
1712 emit_move(val
, REG_EAX
, NULL
, "loop pre condition");
1713 insn("test", REG_EAX
, REG_EAX
, NULL
);
1714 insn("jz", lbv
, NULL
, NULL
);
1717 if (!post_condition
|| post_condition
->type
!= EXPR_VALUE
|| post_condition
->value
) {
1718 loop_top
= new_label();
1719 emit_label(loop_top
, "loop top");
1721 x86_statement(statement
);
1722 if (stmt
->iterator_continue
->used
)
1723 emit_label(loop_continue
, "'continue' iterator");
1724 x86_statement(post_statement
);
1725 if (!post_condition
) {
1726 struct storage
*lbv
= new_storage(STOR_LABEL
);
1727 lbv
->label
= loop_top
;
1728 lbv
->flags
= STOR_WANTS_FREE
;
1729 insn("jmp", lbv
, NULL
, "go to loop top");
1730 } else if (post_condition
->type
== EXPR_VALUE
) {
1731 if (post_condition
->value
) {
1732 struct storage
*lbv
= new_storage(STOR_LABEL
);
1733 lbv
->label
= loop_top
;
1734 lbv
->flags
= STOR_WANTS_FREE
;
1735 insn("jmp", lbv
, NULL
, "go to loop top");
1738 struct storage
*lbv
= new_storage(STOR_LABEL
);
1739 lbv
->label
= loop_top
;
1740 lbv
->flags
= STOR_WANTS_FREE
;
1742 val
= x86_expression(post_condition
);
1744 emit_move(val
, REG_EAX
, NULL
, "loop post condition");
1745 insn("test", REG_EAX
, REG_EAX
, NULL
);
1746 insn("jnz", lbv
, NULL
, NULL
);
1748 if (have_bottom
|| stmt
->iterator_break
->used
)
1749 emit_label(loop_bottom
, "loop bottom");
1755 * Print out a statement
1757 static struct storage
*x86_statement(struct statement
*stmt
)
1761 switch (stmt
->type
) {
1763 return emit_return_stmt(stmt
);
1764 case STMT_COMPOUND
: {
1765 struct statement
*s
;
1766 struct storage
*last
= NULL
;
1768 x86_symbol_decl(stmt
->syms
);
1769 FOR_EACH_PTR(stmt
->stmts
, s
) {
1770 last
= x86_statement(s
);
1776 case STMT_EXPRESSION
:
1777 return x86_expression(stmt
->expression
);
1779 emit_if_conditional(stmt
);
1783 emit_case_statement(stmt
);
1786 emit_switch_statement(stmt
);
1797 printf(".L%p:\n", stmt
->label_identifier
);
1798 x86_statement(stmt
->label_statement
);
1802 if (stmt
->goto_expression
) {
1803 struct storage
*val
= x86_expression(stmt
->goto_expression
);
1804 printf("\tgoto *v%d\n", val
->pseudo
);
1805 } else if (!strcmp("break", show_ident(stmt
->goto_label
->ident
))) {
1806 struct storage
*lbv
= new_storage(STOR_LABEL
);
1807 lbv
->label
= loopstk_break();
1808 lbv
->flags
= STOR_WANTS_FREE
;
1809 insn("jmp", lbv
, NULL
, "'break'; go to loop bottom");
1810 } else if (!strcmp("continue", show_ident(stmt
->goto_label
->ident
))) {
1811 struct storage
*lbv
= new_storage(STOR_LABEL
);
1812 lbv
->label
= loopstk_continue();
1813 lbv
->flags
= STOR_WANTS_FREE
;
1814 insn("jmp", lbv
, NULL
, "'continue'; go to loop top");
1816 struct storage
*labelsym
= new_labelsym(stmt
->goto_label
);
1817 insn("jmp", labelsym
, NULL
, NULL
);
1821 printf("\tasm( .... )\n");
1825 printf("\tjne xxx*\n");
1828 case STMT_CONDFALSE
:
1829 printf("\tje xxx*\n");
1835 static struct storage
*x86_call_expression(struct expression
*expr
)
1837 struct function
*f
= current_func
;
1838 struct symbol
*direct
;
1839 struct expression
*arg
, *fn
;
1840 struct storage
*retval
, *fncall
;
1845 warn(expr
->pos
, "\tcall with no type!");
1850 FOR_EACH_PTR_REVERSE(expr
->args
, arg
) {
1851 struct storage
*new = x86_expression(arg
);
1852 int size
= arg
->ctype
->bit_size
;
1854 /* FIXME: pay attention to 'size' */
1855 insn("pushl", new, NULL
,
1856 !framesize
? "begin function call" : NULL
);
1858 framesize
+= size
>> 3;
1859 } END_FOR_EACH_PTR_REVERSE
;
1863 /* Remove dereference, if any */
1865 if (fn
->type
== EXPR_PREOP
) {
1866 if (fn
->unop
->type
== EXPR_SYMBOL
) {
1867 struct symbol
*sym
= fn
->unop
->symbol
;
1868 if (sym
->ctype
.base_type
->type
== SYM_FN
)
1873 struct storage
*direct_stor
= new_storage(STOR_SYM
);
1874 direct_stor
->flags
|= STOR_WANTS_FREE
;
1875 direct_stor
->sym
= direct
;
1876 insn("call", direct_stor
, NULL
, NULL
);
1878 fncall
= x86_expression(fn
);
1879 emit_move(fncall
, REG_EAX
, fn
->ctype
, NULL
);
1881 strcpy(s
, "\tcall\t*%eax\n");
1882 push_text_atom(f
, s
);
1885 /* FIXME: pay attention to BITS_IN_POINTER */
1887 struct storage
*val
= new_storage(STOR_VALUE
);
1888 val
->value
= (long long) framesize
;
1889 val
->flags
= STOR_WANTS_FREE
;
1890 insn("addl", val
, REG_ESP
, NULL
);
1893 retval
= stack_alloc(4);
1894 emit_move(REG_EAX
, retval
, NULL
, "end function call");
1899 static struct storage
*x86_address_gen(struct expression
*expr
)
1901 struct function
*f
= current_func
;
1902 struct storage
*addr
;
1903 struct storage
*new;
1906 if ((expr
->type
!= EXPR_PREOP
) || (expr
->op
!= '*'))
1907 return x86_expression(expr
->address
);
1909 addr
= x86_expression(expr
->unop
);
1910 if (expr
->unop
->type
== EXPR_SYMBOL
)
1913 emit_move(addr
, REG_EAX
, NULL
, "begin deref ..");
1915 /* FIXME: operand size */
1916 strcpy(s
, "\tmovl\t(%eax), %ecx\n");
1917 push_text_atom(f
, s
);
1919 new = stack_alloc(4);
1920 emit_move(REG_ECX
, new, NULL
, ".... end deref");
1925 static struct storage
*x86_assignment(struct expression
*expr
)
1927 struct expression
*target
= expr
->left
;
1928 struct storage
*val
, *addr
;
1933 val
= x86_expression(expr
->right
);
1934 addr
= x86_address_gen(target
);
1936 switch (val
->type
) {
1937 /* copy, where both operands are memory */
1940 emit_copy(addr
, val
, expr
->ctype
);
1943 /* copy, one or zero operands are memory */
1948 emit_move(val
, addr
, expr
->left
->ctype
, NULL
);
1958 static int x86_initialization(struct symbol
*sym
, struct expression
*expr
)
1960 struct storage
*val
, *addr
;
1966 bits
= expr
->ctype
->bit_size
;
1967 val
= x86_expression(expr
);
1968 addr
= x86_symbol_expr(sym
);
1969 // FIXME! The "target" expression is for bitfield store information.
1970 // Leave it NULL, which works fine.
1971 emit_store(NULL
, addr
, val
, bits
);
1975 static struct storage
*x86_access(struct expression
*expr
)
1977 return x86_address_gen(expr
);
1980 static struct storage
*x86_preop(struct expression
*expr
)
1983 * '*' is an lvalue access, and is fundamentally different
1984 * from an arithmetic operation. Maybe it should have an
1985 * expression type of its own..
1987 if (expr
->op
== '*')
1988 return x86_access(expr
);
1989 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
1990 return emit_inc_dec(expr
, 0);
1991 return emit_regular_preop(expr
);
1994 static struct storage
*x86_symbol_expr(struct symbol
*sym
)
1996 struct storage
*new = stack_alloc(4);
1998 if (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_EXTERN
| MOD_STATIC
)) {
1999 printf("\tmovi.%d\t\tv%d,$%s\n", BITS_IN_POINTER
, new->pseudo
, show_ident(sym
->ident
));
2002 if (sym
->ctype
.modifiers
& MOD_ADDRESSABLE
) {
2003 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", BITS_IN_POINTER
, new->pseudo
, sym
->value
);
2006 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", BITS_IN_POINTER
, new->pseudo
, show_ident(sym
->ident
), sym
);
2010 static void x86_symbol_init(struct symbol
*sym
)
2012 struct symbol_private
*priv
= sym
->aux
;
2013 struct expression
*expr
= sym
->initializer
;
2014 struct storage
*new;
2017 new = x86_expression(expr
);
2019 new = stack_alloc(sym
->bit_size
/ 8);
2022 priv
= calloc(1, sizeof(*priv
));
2024 /* FIXME: leak! we don't free... */
2025 /* (well, we don't free symbols either) */
2031 static int type_is_signed(struct symbol
*sym
)
2033 if (sym
->type
== SYM_NODE
)
2034 sym
= sym
->ctype
.base_type
;
2035 if (sym
->type
== SYM_PTR
)
2037 return !(sym
->ctype
.modifiers
& MOD_UNSIGNED
);
2040 static struct storage
*x86_bitfield_expr(struct expression
*expr
)
2042 return x86_access(expr
);
2045 static struct storage
*x86_label_expr(struct expression
*expr
)
2047 struct storage
*new = stack_alloc(4);
2048 printf("\tmovi.%d\t\tv%d,.L%p\n",BITS_IN_POINTER
, new->pseudo
, expr
->label_symbol
);
2052 static struct storage
*x86_statement_expr(struct expression
*expr
)
2054 return x86_statement(expr
->statement
);
2057 static int x86_position_expr(struct expression
*expr
, struct symbol
*base
)
2059 struct storage
*new = x86_expression(expr
->init_expr
);
2060 struct symbol
*ctype
= expr
->init_sym
;
2062 printf("\tinsert v%d at [%d:%d] of %s\n", new->pseudo
,
2063 expr
->init_offset
, ctype
->bit_offset
,
2064 show_ident(base
->ident
));
2068 static void x86_initializer_expr(struct expression
*expr
, struct symbol
*ctype
)
2070 struct expression
*entry
;
2072 FOR_EACH_PTR(expr
->expr_list
, entry
) {
2073 // Nested initializers have their positions already
2074 // recursively calculated - just output them too
2075 if (entry
->type
== EXPR_INITIALIZER
) {
2076 x86_initializer_expr(entry
, ctype
);
2080 // Ignore initializer indexes and identifiers - the
2081 // evaluator has taken them into account
2082 if (entry
->type
== EXPR_IDENTIFIER
|| entry
->type
== EXPR_INDEX
)
2084 if (entry
->type
== EXPR_POS
) {
2085 x86_position_expr(entry
, ctype
);
2088 x86_initialization(ctype
, entry
);
2093 * Print out an expression. Return the pseudo that contains the
2096 static struct storage
*x86_expression(struct expression
*expr
)
2102 struct position
*pos
= &expr
->pos
;
2103 printf("\tno type at %s:%d:%d\n",
2104 input_streams
[pos
->stream
].name
,
2105 pos
->line
, pos
->pos
);
2109 switch (expr
->type
) {
2111 return x86_call_expression(expr
);
2113 case EXPR_ASSIGNMENT
:
2114 return x86_assignment(expr
);
2117 return emit_compare(expr
);
2121 return emit_binop(expr
);
2123 return x86_preop(expr
);
2125 return emit_postop(expr
);
2127 return emit_symbol_expr_init(expr
->symbol
);
2130 warn(expr
->pos
, "invalid expression after evaluation");
2133 return emit_cast_expr(expr
);
2135 return emit_value(expr
);
2137 return emit_string_expr(expr
);
2139 return x86_bitfield_expr(expr
);
2140 case EXPR_INITIALIZER
:
2141 x86_initializer_expr(expr
, expr
->ctype
);
2143 case EXPR_CONDITIONAL
:
2144 return emit_conditional_expr(expr
);
2145 case EXPR_STATEMENT
:
2146 return x86_statement_expr(expr
);
2148 return x86_label_expr(expr
);
2150 // None of these should exist as direct expressions: they are only
2151 // valid as sub-expressions of initializers.
2153 warn(expr
->pos
, "unable to show plain initializer position expression");
2155 case EXPR_IDENTIFIER
:
2156 warn(expr
->pos
, "unable to show identifier expression");
2159 warn(expr
->pos
, "unable to show index expression");