2 * sparse/compile-i386.c
4 * Copyright (C) 2003 Transmeta Corp.
6 * Copyright 2003 Jeff Garzik
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 * in general, any non-32bit SYM_BASETYPE is unlikely to work.
30 * complex initializers
32 * global struct/union variables
33 * addressing structures, and members of structures (as opposed to
34 * scalars) on the stack. Requires smarter stack frame allocation.
36 * any function argument that isn't 32 bits (or promoted to such)
56 #include "expression.h"
62 unsigned int len
; /* does NOT include terminating null */
71 struct loop_stack
*next
;
76 DECLARE_PTR_LIST(str_list
, struct atom
);
77 DECLARE_PTR_LIST(atom_list
, struct atom
);
78 DECLARE_PTR_LIST(storage_list
, struct storage
);
83 struct storage_list
*pseudo_list
;
84 struct atom_list
*atom_list
;
85 struct str_list
*str_list
;
86 struct loop_stack
*loop_stack
;
93 STOR_PSEUDO
, /* variable stored on the stack */
94 STOR_ARG
, /* function argument */
95 STOR_SYM
, /* a symbol we can directly ref in the asm */
96 STOR_REG
, /* scratch register */
97 STOR_VALUE
, /* integer constant */
98 STOR_LABEL
, /* label / jump target */
99 STOR_LABELSYM
, /* label generated from symbol's pointer value */
104 struct storage
*contains
;
105 const unsigned char aliases
[12];
106 #define own_regno aliases[0]
110 enum storage_type type
;
114 struct reg_info
*reg
;
115 struct symbol
*ctype
;
142 struct symbol
*labelsym
;
148 STOR_LABEL_VAL
= (1 << 0),
149 STOR_WANTS_FREE
= (1 << 1),
152 struct symbol_private
{
153 struct storage
*addr
;
168 unsigned int text_len
; /* w/o terminating null */
171 /* stuff for insns */
179 /* stuff for C strings */
181 struct string
*string
;
188 static struct function
*current_func
= NULL
;
189 static struct textbuf
*unit_post_text
= NULL
;
190 static const char *current_section
;
192 static void emit_comment(const char * fmt
, ...) FORMAT_ATTR(1);
193 static void emit_move(struct storage
*src
, struct storage
*dest
,
194 struct symbol
*ctype
, const char *comment
);
195 static int type_is_signed(struct symbol
*sym
);
196 static struct storage
*x86_address_gen(struct expression
*expr
);
197 static struct storage
*x86_symbol_expr(struct symbol
*sym
);
198 static void x86_symbol(struct symbol
*sym
);
199 static struct storage
*x86_statement(struct statement
*stmt
);
200 static struct storage
*x86_expression(struct expression
*expr
);
204 AL
, DL
, CL
, BL
, AH
, DH
, CH
, BH
, // 8-bit
205 AX
, DX
, CX
, BX
, SI
, DI
, BP
, SP
, // 16-bit
206 EAX
, EDX
, ECX
, EBX
, ESI
, EDI
, EBP
, ESP
, // 32-bit
207 EAX_EDX
, ECX_EBX
, ESI_EDI
, // 64-bit
210 /* This works on regno's, reg_info's and hardreg_storage's */
211 #define byte_reg(reg) ((reg) - 16)
212 #define highbyte_reg(reg) ((reg)-12)
213 #define word_reg(reg) ((reg)-8)
215 #define REGINFO(nr, str, conflicts...) [nr] = { .name = str, .aliases = { nr , conflicts } }
217 static struct reg_info reg_info_table
[] = {
218 REGINFO( AL
, "%al", AX
, EAX
, EAX_EDX
),
219 REGINFO( DL
, "%dl", DX
, EDX
, EAX_EDX
),
220 REGINFO( CL
, "%cl", CX
, ECX
, ECX_EBX
),
221 REGINFO( BL
, "%bl", BX
, EBX
, ECX_EBX
),
222 REGINFO( AH
, "%ah", AX
, EAX
, EAX_EDX
),
223 REGINFO( DH
, "%dh", DX
, EDX
, EAX_EDX
),
224 REGINFO( CH
, "%ch", CX
, ECX
, ECX_EBX
),
225 REGINFO( BH
, "%bh", BX
, EBX
, ECX_EBX
),
226 REGINFO( AX
, "%ax", AL
, AH
, EAX
, EAX_EDX
),
227 REGINFO( DX
, "%dx", DL
, DH
, EDX
, EAX_EDX
),
228 REGINFO( CX
, "%cx", CL
, CH
, ECX
, ECX_EBX
),
229 REGINFO( BX
, "%bx", BL
, BH
, EBX
, ECX_EBX
),
230 REGINFO( SI
, "%si", ESI
, ESI_EDI
),
231 REGINFO( DI
, "%di", EDI
, ESI_EDI
),
232 REGINFO( BP
, "%bp", EBP
),
233 REGINFO( SP
, "%sp", ESP
),
234 REGINFO(EAX
, "%eax", AL
, AH
, AX
, EAX_EDX
),
235 REGINFO(EDX
, "%edx", DL
, DH
, DX
, EAX_EDX
),
236 REGINFO(ECX
, "%ecx", CL
, CH
, CX
, ECX_EBX
),
237 REGINFO(EBX
, "%ebx", BL
, BH
, BX
, ECX_EBX
),
238 REGINFO(ESI
, "%esi", SI
, ESI_EDI
),
239 REGINFO(EDI
, "%edi", DI
, ESI_EDI
),
240 REGINFO(EBP
, "%ebp", BP
),
241 REGINFO(ESP
, "%esp", SP
),
242 REGINFO(EAX_EDX
, "%eax:%edx", AL
, AH
, AX
, EAX
, DL
, DH
, DX
, EDX
),
243 REGINFO(ECX_EBX
, "%ecx:%ebx", CL
, CH
, CX
, ECX
, BL
, BH
, BX
, EBX
),
244 REGINFO(ESI_EDI
, "%esi:%edi", SI
, ESI
, DI
, EDI
),
247 #define REGSTORAGE(nr) [nr] = { .type = STOR_REG, .reg = reg_info_table + (nr) }
249 static struct storage hardreg_storage_table
[] = {
250 REGSTORAGE(AL
), REGSTORAGE(DL
), REGSTORAGE(CL
), REGSTORAGE(BL
),
251 REGSTORAGE(AH
), REGSTORAGE(DH
), REGSTORAGE(CH
), REGSTORAGE(BH
),
252 REGSTORAGE(AX
), REGSTORAGE(DX
), REGSTORAGE(CX
), REGSTORAGE(BX
),
253 REGSTORAGE(SI
), REGSTORAGE(DI
), REGSTORAGE(BP
), REGSTORAGE(SP
),
254 REGSTORAGE(EAX
), REGSTORAGE(EDX
), REGSTORAGE(ECX
), REGSTORAGE(EBX
),
255 REGSTORAGE(ESI
), REGSTORAGE(EDI
), REGSTORAGE(EBP
), REGSTORAGE(ESP
),
256 REGSTORAGE(EAX_EDX
), REGSTORAGE(ECX_EBX
), REGSTORAGE(ESI_EDI
),
259 #define REG_EAX (&hardreg_storage_table[EAX])
260 #define REG_ECX (&hardreg_storage_table[ECX])
261 #define REG_EDX (&hardreg_storage_table[EDX])
262 #define REG_ESP (&hardreg_storage_table[ESP])
263 #define REG_DL (&hardreg_storage_table[DL])
264 #define REG_DX (&hardreg_storage_table[DX])
265 #define REG_AL (&hardreg_storage_table[AL])
266 #define REG_AX (&hardreg_storage_table[AX])
268 static DECLARE_BITMAP(regs_in_use
, 256);
270 static inline struct storage
* reginfo_reg(struct reg_info
*info
)
272 return hardreg_storage_table
+ info
->own_regno
;
275 static struct storage
* get_hardreg(struct storage
*reg
, int clear
)
277 struct reg_info
*info
= reg
->reg
;
278 const unsigned char *aliases
;
281 aliases
= info
->aliases
;
282 while ((regno
= *aliases
++) != NOREG
) {
283 if (test_bit(regno
, regs_in_use
))
286 reg_info_table
[regno
].contains
= NULL
;
288 set_bit(info
->own_regno
, regs_in_use
);
291 fprintf(stderr
, "register %s is busy\n", info
->name
);
292 if (regno
+ reg_info_table
!= info
)
293 fprintf(stderr
, " conflicts with %s\n", reg_info_table
[regno
].name
);
297 static void put_reg(struct storage
*reg
)
299 struct reg_info
*info
= reg
->reg
;
300 int regno
= info
->own_regno
;
302 if (test_and_clear_bit(regno
, regs_in_use
))
304 fprintf(stderr
, "freeing already free'd register %s\n", reg_info_table
[regno
].name
);
309 const unsigned char regs
[30];
312 static struct regclass regclass_8
= { "8-bit", { AL
, DL
, CL
, BL
, AH
, DH
, CH
, BH
}};
313 static struct regclass regclass_16
= { "16-bit", { AX
, DX
, CX
, BX
, SI
, DI
, BP
}};
314 static struct regclass regclass_32
= { "32-bit", { EAX
, EDX
, ECX
, EBX
, ESI
, EDI
, EBP
}};
315 static struct regclass regclass_64
= { "64-bit", { EAX_EDX
, ECX_EBX
, ESI_EDI
}};
317 static struct regclass regclass_32_8
= { "32-bit bytes", { EAX
, EDX
, ECX
, EBX
}};
319 static struct regclass
*get_regclass_bits(int bits
)
322 case 8: return ®class_8
;
323 case 16: return ®class_16
;
324 case 64: return ®class_64
;
325 default: return ®class_32
;
329 static struct regclass
*get_regclass(struct expression
*expr
)
331 return get_regclass_bits(expr
->ctype
->bit_size
);
334 static int register_busy(int regno
)
336 if (!test_bit(regno
, regs_in_use
)) {
337 struct reg_info
*info
= reg_info_table
+ regno
;
338 const unsigned char *regs
= info
->aliases
+1;
340 while ((regno
= *regs
) != NOREG
) {
342 if (test_bit(regno
, regs_in_use
))
351 static struct storage
*get_reg(struct regclass
*class)
353 const unsigned char *regs
= class->regs
;
356 while ((regno
= *regs
) != NOREG
) {
358 if (register_busy(regno
))
360 return get_hardreg(hardreg_storage_table
+ regno
, 1);
362 fprintf(stderr
, "Ran out of %s registers\n", class->name
);
366 static struct storage
*get_reg_value(struct storage
*value
, struct regclass
*class)
368 struct reg_info
*info
;
371 /* Do we already have it somewhere */
373 if (info
&& info
->contains
== value
) {
374 emit_comment("already have register %s", info
->name
);
375 return get_hardreg(hardreg_storage_table
+ info
->own_regno
, 0);
378 reg
= get_reg(class);
379 emit_move(value
, reg
, value
->ctype
, "reload register");
381 info
->contains
= value
;
386 static struct storage
*temp_from_bits(unsigned int bit_size
)
388 return get_reg(get_regclass_bits(bit_size
));
391 static inline unsigned int pseudo_offset(struct storage
*s
)
393 if (s
->type
!= STOR_PSEUDO
)
394 return 123456; /* intentionally bogus value */
399 static inline unsigned int arg_offset(struct storage
*s
)
401 if (s
->type
!= STOR_ARG
)
402 return 123456; /* intentionally bogus value */
404 /* FIXME: this is wrong wrong wrong */
405 return current_func
->stack_size
+ ((1 + s
->idx
) * 4);
408 static const char *pretty_offset(int ofs
)
410 static char esp_buf
[64];
413 sprintf(esp_buf
, "%d(%%esp)", ofs
);
415 strcpy(esp_buf
, "(%esp)");
420 static void stor_sym_init(struct symbol
*sym
)
422 struct storage
*stor
;
423 struct symbol_private
*priv
;
425 priv
= calloc(1, sizeof(*priv
) + sizeof(*stor
));
427 die("OOM in stor_sym_init");
429 stor
= (struct storage
*) (priv
+ 1);
432 stor
->type
= STOR_SYM
;
436 static const char *stor_op_name(struct storage
*s
)
438 static char name
[32];
442 strcpy(name
, pretty_offset((int) pseudo_offset(s
)));
445 strcpy(name
, pretty_offset((int) arg_offset(s
)));
448 strcpy(name
, show_ident(s
->sym
->ident
));
451 strcpy(name
, s
->reg
->name
);
454 sprintf(name
, "$%Ld", s
->value
);
457 sprintf(name
, "%s.L%d", s
->flags
& STOR_LABEL_VAL
? "$" : "",
461 sprintf(name
, "%s.LS%p", s
->flags
& STOR_LABEL_VAL
? "$" : "",
469 static struct atom
*new_atom(enum atom_type type
)
473 atom
= calloc(1, sizeof(*atom
)); /* TODO: chunked alloc */
482 static inline void push_cstring(struct function
*f
, struct string
*str
,
487 atom
= new_atom(ATOM_CSTR
);
491 add_ptr_list(&f
->str_list
, atom
); /* note: _not_ atom_list */
494 static inline void push_atom(struct function
*f
, struct atom
*atom
)
496 add_ptr_list(&f
->atom_list
, atom
);
499 static void push_text_atom(struct function
*f
, const char *text
)
501 struct atom
*atom
= new_atom(ATOM_TEXT
);
503 atom
->text
= strdup(text
);
504 atom
->text_len
= strlen(text
);
509 static struct storage
*new_storage(enum storage_type type
)
511 struct storage
*stor
;
513 stor
= calloc(1, sizeof(*stor
));
515 die("OOM in new_storage");
522 static struct storage
*stack_alloc(int n_bytes
)
524 struct function
*f
= current_func
;
525 struct storage
*stor
;
529 stor
= new_storage(STOR_PSEUDO
);
530 stor
->type
= STOR_PSEUDO
;
531 stor
->pseudo
= f
->pseudo_nr
;
532 stor
->offset
= f
->stack_size
; /* FIXME: stack req. natural align */
533 stor
->size
= n_bytes
;
534 f
->stack_size
+= n_bytes
;
537 add_ptr_list(&f
->pseudo_list
, stor
);
542 static struct storage
*new_labelsym(struct symbol
*sym
)
544 struct storage
*stor
;
546 stor
= new_storage(STOR_LABELSYM
);
549 stor
->flags
|= STOR_WANTS_FREE
;
550 stor
->labelsym
= sym
;
556 static struct storage
*new_val(long long value
)
558 struct storage
*stor
;
560 stor
= new_storage(STOR_VALUE
);
563 stor
->flags
|= STOR_WANTS_FREE
;
570 static int new_label(void)
572 static int label
= 0;
576 static void textbuf_push(struct textbuf
**buf_p
, const char *text
)
578 struct textbuf
*tmp
, *list
= *buf_p
;
579 unsigned int text_len
= strlen(text
);
580 unsigned int alloc_len
= text_len
+ 1 + sizeof(*list
);
582 tmp
= calloc(1, alloc_len
);
584 die("OOM on textbuf alloc");
586 tmp
->text
= ((void *) tmp
) + sizeof(*tmp
);
587 memcpy(tmp
->text
, text
, text_len
+ 1);
590 /* add to end of list */
595 tmp
->prev
= list
->prev
;
596 tmp
->prev
->next
= tmp
;
604 static void textbuf_emit(struct textbuf
**buf_p
)
606 struct textbuf
*tmp
, *list
= *buf_p
;
610 if (tmp
->next
== tmp
)
613 tmp
->prev
->next
= tmp
->next
;
614 tmp
->next
->prev
= tmp
->prev
;
618 fputs(tmp
->text
, stdout
);
626 static void insn(const char *insn
, struct storage
*op1
, struct storage
*op2
,
627 const char *comment_in
)
629 struct function
*f
= current_func
;
630 struct atom
*atom
= new_atom(ATOM_INSN
);
632 assert(insn
!= NULL
);
634 strcpy(atom
->insn
, insn
);
635 if (comment_in
&& (*comment_in
))
636 strncpy(atom
->comment
, comment_in
,
637 sizeof(atom
->comment
) - 1);
645 static void emit_comment(const char *fmt
, ...)
647 struct function
*f
= current_func
;
648 static char tmpbuf
[100] = "\t# ";
653 i
= vsnprintf(tmpbuf
+3, sizeof(tmpbuf
)-4, fmt
, args
);
657 push_text_atom(f
, tmpbuf
);
660 static void emit_label (int label
, const char *comment
)
662 struct function
*f
= current_func
;
666 sprintf(s
, ".L%d:\n", label
);
668 sprintf(s
, ".L%d:\t\t\t\t\t# %s\n", label
, comment
);
670 push_text_atom(f
, s
);
673 static void emit_labelsym (struct symbol
*sym
, const char *comment
)
675 struct function
*f
= current_func
;
679 sprintf(s
, ".LS%p:\n", sym
);
681 sprintf(s
, ".LS%p:\t\t\t\t# %s\n", sym
, comment
);
683 push_text_atom(f
, s
);
686 void emit_unit_begin(const char *basename
)
688 printf("\t.file\t\"%s\"\n", basename
);
691 void emit_unit_end(void)
693 textbuf_emit(&unit_post_text
);
694 printf("\t.ident\t\"sparse silly x86 backend (built %s)\"\n", __DATE__
);
697 /* conditionally switch sections */
698 static void emit_section(const char *s
)
700 if (s
== current_section
)
702 if (current_section
&& (!strcmp(s
, current_section
)))
709 static void emit_insn_atom(struct function
*f
, struct atom
*atom
)
713 struct storage
*op1
= atom
->op1
;
714 struct storage
*op2
= atom
->op2
;
716 if (atom
->comment
[0])
717 sprintf(comment
, "\t\t# %s", atom
->comment
);
723 strcpy(tmp
, stor_op_name(op1
));
724 sprintf(s
, "\t%s\t%s, %s%s\n",
725 atom
->insn
, tmp
, stor_op_name(op2
), comment
);
726 } else if (atom
->op1
)
727 sprintf(s
, "\t%s\t%s%s%s\n",
728 atom
->insn
, stor_op_name(op1
),
729 comment
[0] ? "\t" : "", comment
);
731 sprintf(s
, "\t%s\t%s%s\n",
733 comment
[0] ? "\t\t" : "", comment
);
735 write(STDOUT_FILENO
, s
, strlen(s
));
738 static void emit_atom_list(struct function
*f
)
742 FOR_EACH_PTR(f
->atom_list
, atom
) {
743 switch (atom
->type
) {
745 ssize_t rc
= write(STDOUT_FILENO
, atom
->text
,
747 (void) rc
; /* FIXME */
751 emit_insn_atom(f
, atom
);
757 } END_FOR_EACH_PTR(atom
);
760 static void emit_string_list(struct function
*f
)
764 emit_section(".section\t.rodata");
766 FOR_EACH_PTR(f
->str_list
, atom
) {
767 /* FIXME: escape " in string */
768 printf(".L%d:\n", atom
->label
);
769 printf("\t.string\t%s\n", show_string(atom
->string
));
772 } END_FOR_EACH_PTR(atom
);
775 static void func_cleanup(struct function
*f
)
777 struct storage
*stor
;
780 FOR_EACH_PTR(f
->atom_list
, atom
) {
781 if ((atom
->type
== ATOM_TEXT
) && (atom
->text
))
783 if (atom
->op1
&& (atom
->op1
->flags
& STOR_WANTS_FREE
))
785 if (atom
->op2
&& (atom
->op2
->flags
& STOR_WANTS_FREE
))
788 } END_FOR_EACH_PTR(atom
);
790 FOR_EACH_PTR(f
->pseudo_list
, stor
) {
792 } END_FOR_EACH_PTR(stor
);
794 free_ptr_list(&f
->pseudo_list
);
798 /* function prologue */
799 static void emit_func_pre(struct symbol
*sym
)
803 unsigned int i
, argc
= 0, alloc_len
;
805 struct symbol_private
*privbase
;
806 struct storage
*storage_base
;
807 struct symbol
*base_type
= sym
->ctype
.base_type
;
809 FOR_EACH_PTR(base_type
->arguments
, arg
) {
811 } END_FOR_EACH_PTR(arg
);
815 (argc
* sizeof(struct symbol
*)) +
816 (argc
* sizeof(struct symbol_private
)) +
817 (argc
* sizeof(struct storage
));
818 mem
= calloc(1, alloc_len
);
820 die("OOM on func info");
822 f
= (struct function
*) mem
;
824 f
->argv
= (struct symbol
**) mem
;
825 mem
+= (argc
* sizeof(struct symbol
*));
826 privbase
= (struct symbol_private
*) mem
;
827 mem
+= (argc
* sizeof(struct symbol_private
));
828 storage_base
= (struct storage
*) mem
;
831 f
->ret_target
= new_label();
834 FOR_EACH_PTR(base_type
->arguments
, arg
) {
836 arg
->aux
= &privbase
[i
];
837 storage_base
[i
].type
= STOR_ARG
;
838 storage_base
[i
].idx
= i
;
839 privbase
[i
].addr
= &storage_base
[i
];
841 } END_FOR_EACH_PTR(arg
);
843 assert(current_func
== NULL
);
847 /* function epilogue */
848 static void emit_func_post(struct symbol
*sym
)
850 const char *name
= show_ident(sym
->ident
);
851 struct function
*f
= current_func
;
852 int stack_size
= f
->stack_size
;
857 /* function prologue */
858 emit_section(".text");
859 if ((sym
->ctype
.modifiers
& MOD_STATIC
) == 0)
860 printf(".globl %s\n", name
);
861 printf("\t.type\t%s, @function\n", name
);
862 printf("%s:\n", name
);
865 char pseudo_const
[16];
867 sprintf(pseudo_const
, "$%d", stack_size
);
868 printf("\tsubl\t%s, %%esp\n", pseudo_const
);
871 /* function epilogue */
873 /* jump target for 'return' statements */
874 emit_label(f
->ret_target
, NULL
);
879 val
= new_storage(STOR_VALUE
);
880 val
->value
= (long long) (stack_size
);
881 val
->flags
= STOR_WANTS_FREE
;
883 insn("addl", val
, REG_ESP
, NULL
);
886 insn("ret", NULL
, NULL
, NULL
);
888 /* output everything to stdout */
889 fflush(stdout
); /* paranoia; needed? */
892 /* function footer */
893 name
= show_ident(sym
->ident
);
894 printf("\t.size\t%s, .-%s\n", name
, name
);
900 /* emit object (a.k.a. variable, a.k.a. data) prologue */
901 static void emit_object_pre(const char *name
, unsigned long modifiers
,
902 unsigned long alignment
, unsigned int byte_size
)
904 if ((modifiers
& MOD_STATIC
) == 0)
905 printf(".globl %s\n", name
);
906 emit_section(".data");
908 printf("\t.align %lu\n", alignment
);
909 printf("\t.type\t%s, @object\n", name
);
910 printf("\t.size\t%s, %d\n", name
, byte_size
);
911 printf("%s:\n", name
);
914 /* emit value (only) for an initializer scalar */
915 static void emit_scalar(struct expression
*expr
, unsigned int bit_size
)
920 assert(expr
->type
== EXPR_VALUE
);
922 if (expr
->value
== 0ULL) {
923 printf("\t.zero\t%d\n", bit_size
/ 8);
927 ll
= (long long) expr
->value
;
930 case 8: type
= "byte"; ll
= (char) ll
; break;
931 case 16: type
= "value"; ll
= (short) ll
; break;
932 case 32: type
= "long"; ll
= (int) ll
; break;
933 case 64: type
= "quad"; break;
934 default: type
= NULL
; break;
937 assert(type
!= NULL
);
939 printf("\t.%s\t%Ld\n", type
, ll
);
942 static void emit_global_noinit(const char *name
, unsigned long modifiers
,
943 unsigned long alignment
, unsigned int byte_size
)
947 if (modifiers
& MOD_STATIC
) {
948 sprintf(s
, "\t.local\t%s\n", name
);
949 textbuf_push(&unit_post_text
, s
);
952 sprintf(s
, "\t.comm\t%s,%d,%lu\n", name
, byte_size
, alignment
);
954 sprintf(s
, "\t.comm\t%s,%d\n", name
, byte_size
);
955 textbuf_push(&unit_post_text
, s
);
958 static int ea_current
, ea_last
;
960 static void emit_initializer(struct symbol
*sym
,
961 struct expression
*expr
)
963 int distance
= ea_current
- ea_last
- 1;
966 printf("\t.zero\t%d\n", (sym
->bit_size
/ 8) * distance
);
968 if (expr
->type
== EXPR_VALUE
) {
969 struct symbol
*base_type
= sym
->ctype
.base_type
;
970 assert(base_type
!= NULL
);
972 emit_scalar(expr
, sym
->bit_size
/ get_expression_value(base_type
->array_size
));
975 if (expr
->type
!= EXPR_INITIALIZER
)
978 assert(0); /* FIXME */
981 static int sort_array_cmp(const struct expression
*a
,
982 const struct expression
*b
)
984 int a_ofs
= 0, b_ofs
= 0;
986 if (a
->type
== EXPR_POS
)
987 a_ofs
= (int) a
->init_offset
;
988 if (b
->type
== EXPR_POS
)
989 b_ofs
= (int) b
->init_offset
;
991 return a_ofs
- b_ofs
;
994 /* move to front-end? */
995 static void sort_array(struct expression
*expr
)
997 struct expression
*entry
, **list
;
998 unsigned int elem
, sorted
, i
;
1001 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1003 } END_FOR_EACH_PTR(entry
);
1008 list
= malloc(sizeof(entry
) * elem
);
1010 die("OOM in sort_array");
1012 /* this code is no doubt evil and ignores EXPR_INDEX possibly
1013 * to its detriment and other nasty things. improvements
1018 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1019 if ((entry
->type
== EXPR_POS
) || (entry
->type
== EXPR_VALUE
)) {
1020 /* add entry to list[], in sorted order */
1025 for (i
= 0; i
< sorted
; i
++)
1026 if (sort_array_cmp(entry
, list
[i
]) <= 0)
1029 /* If inserting into the middle of list[]
1030 * instead of appending, we memmove.
1031 * This is ugly, but thankfully
1032 * uncommon. Input data with tons of
1033 * entries very rarely have explicit
1034 * offsets. convert to qsort eventually...
1037 memmove(&list
[i
+ 1], &list
[i
],
1038 (sorted
- i
) * sizeof(entry
));
1043 } END_FOR_EACH_PTR(entry
);
1046 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1047 if ((entry
->type
== EXPR_POS
) || (entry
->type
== EXPR_VALUE
))
1048 *THIS_ADDRESS(entry
) = list
[i
++];
1049 } END_FOR_EACH_PTR(entry
);
1053 static void emit_array(struct symbol
*sym
)
1055 struct symbol
*base_type
= sym
->ctype
.base_type
;
1056 struct expression
*expr
= sym
->initializer
;
1057 struct expression
*entry
;
1059 assert(base_type
!= NULL
);
1065 emit_object_pre(show_ident(sym
->ident
), sym
->ctype
.modifiers
,
1066 sym
->ctype
.alignment
,
1071 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1072 if (entry
->type
== EXPR_VALUE
) {
1074 emit_initializer(sym
, entry
);
1075 ea_last
= ea_current
;
1076 } else if (entry
->type
== EXPR_POS
) {
1078 entry
->init_offset
/ (base_type
->bit_size
/ 8);
1079 emit_initializer(sym
, entry
->init_expr
);
1080 ea_last
= ea_current
;
1082 } END_FOR_EACH_PTR(entry
);
1085 void emit_one_symbol(struct symbol
*sym
)
1090 static void emit_copy(struct storage
*dest
, struct storage
*src
,
1091 struct symbol
*ctype
)
1093 struct storage
*reg
= NULL
;
1094 unsigned int bit_size
;
1096 /* FIXME: Bitfield copy! */
1098 bit_size
= src
->size
* 8;
1101 if ((src
->type
== STOR_ARG
) && (bit_size
< 32))
1104 reg
= temp_from_bits(bit_size
);
1105 emit_move(src
, reg
, ctype
, "begin copy ..");
1107 bit_size
= dest
->size
* 8;
1110 if ((dest
->type
== STOR_ARG
) && (bit_size
< 32))
1113 emit_move(reg
, dest
, ctype
, ".... end copy");
1117 static void emit_store(struct expression
*dest_expr
, struct storage
*dest
,
1118 struct storage
*src
, int bits
)
1120 /* FIXME: Bitfield store! */
1121 printf("\tst.%d\t\tv%d,[v%d]\n", bits
, src
->pseudo
, dest
->pseudo
);
1124 static void emit_scalar_noinit(struct symbol
*sym
)
1126 emit_global_noinit(show_ident(sym
->ident
),
1127 sym
->ctype
.modifiers
, sym
->ctype
.alignment
,
1132 static void emit_array_noinit(struct symbol
*sym
)
1134 emit_global_noinit(show_ident(sym
->ident
),
1135 sym
->ctype
.modifiers
, sym
->ctype
.alignment
,
1136 get_expression_value(sym
->array_size
) * (sym
->bit_size
/ 8));
1140 static const char *opbits(const char *insn
, unsigned int bits
)
1142 static char opbits_str
[32];
1146 case 8: c
= 'b'; break;
1147 case 16: c
= 'w'; break;
1148 case 32: c
= 'l'; break;
1149 case 64: c
= 'q'; break;
1150 default: abort(); break;
1153 sprintf(opbits_str
, "%s%c", insn
, c
);
1158 static void emit_move(struct storage
*src
, struct storage
*dest
,
1159 struct symbol
*ctype
, const char *comment
)
1162 unsigned int is_signed
;
1163 unsigned int is_dest
= (src
->type
== STOR_REG
);
1167 bits
= ctype
->bit_size
;
1168 is_signed
= type_is_signed(ctype
);
1175 * Are we moving from a register to a register?
1176 * Make the new reg to be the "cache".
1178 if ((dest
->type
== STOR_REG
) && (src
->type
== STOR_REG
)) {
1179 struct storage
*backing
;
1185 backing
= src
->reg
->contains
;
1187 /* Is it still valid? */
1188 if (backing
->reg
!= src
->reg
)
1191 backing
->reg
= dest
->reg
;
1193 dest
->reg
->contains
= backing
;
1194 insn("mov", src
, dest
, NULL
);
1199 * Are we moving to a register from a non-reg?
1201 * See if we have the non-reg source already cached
1204 if (dest
->type
== STOR_REG
) {
1206 struct reg_info
*info
= src
->reg
;
1207 if (info
->contains
== src
) {
1208 src
= reginfo_reg(info
);
1212 dest
->reg
->contains
= src
;
1213 src
->reg
= dest
->reg
;
1216 if (src
->type
== STOR_REG
) {
1217 /* We could just mark the register dirty here and do lazy store.. */
1218 src
->reg
->contains
= dest
;
1219 dest
->reg
= src
->reg
;
1222 if ((bits
== 8) || (bits
== 16)) {
1226 opname
= is_signed
? "movsx" : "movzx";
1230 insn(opbits(opname
, bits
), src
, dest
, comment
);
1233 static struct storage
*emit_compare(struct expression
*expr
)
1235 struct storage
*left
= x86_expression(expr
->left
);
1236 struct storage
*right
= x86_expression(expr
->right
);
1237 struct storage
*reg1
, *reg2
;
1238 struct storage
*new, *val
;
1239 const char *opname
= NULL
;
1240 unsigned int right_bits
= expr
->right
->ctype
->bit_size
;
1243 case '<': opname
= "setl"; break;
1244 case '>': opname
= "setg"; break;
1246 opname
= "setle"; break;
1248 opname
= "setge"; break;
1249 case SPECIAL_EQUAL
: opname
= "sete"; break;
1250 case SPECIAL_NOTEQUAL
: opname
= "setne"; break;
1251 case SPECIAL_UNSIGNED_LT
:
1252 opname
= "setb"; break;
1253 case SPECIAL_UNSIGNED_GT
:
1254 opname
= "seta"; break;
1255 case SPECIAL_UNSIGNED_LTE
:
1256 opname
= "setb"; break;
1257 case SPECIAL_UNSIGNED_GTE
:
1258 opname
= "setae"; break;
1265 val
= new_storage(STOR_VALUE
);
1266 val
->flags
= STOR_WANTS_FREE
;
1268 reg1
= get_reg(®class_32_8
);
1269 emit_move(val
, reg1
, NULL
, NULL
);
1271 /* move op1 into EAX */
1272 reg2
= get_reg_value(left
, get_regclass(expr
->left
));
1274 /* perform comparison, RHS (op1, right) and LHS (op2, EAX) */
1275 insn(opbits("cmp", right_bits
), right
, reg2
, NULL
);
1278 /* store result of operation, 0 or 1, in DL using SETcc */
1279 insn(opname
, byte_reg(reg1
), NULL
, NULL
);
1281 /* finally, store the result (DL) in a new pseudo / stack slot */
1282 new = stack_alloc(4);
1283 emit_move(reg1
, new, NULL
, "end EXPR_COMPARE");
1289 static struct storage
*emit_value(struct expression
*expr
)
1291 #if 0 /* old and slow way */
1292 struct storage
*new = stack_alloc(4);
1293 struct storage
*val
;
1295 val
= new_storage(STOR_VALUE
);
1296 val
->value
= (long long) expr
->value
;
1297 val
->flags
= STOR_WANTS_FREE
;
1298 insn("movl", val
, new, NULL
);
1302 struct storage
*val
;
1304 val
= new_storage(STOR_VALUE
);
1305 val
->value
= (long long) expr
->value
;
1307 return val
; /* FIXME: memory leak */
1311 static struct storage
*emit_divide(struct expression
*expr
, struct storage
*left
, struct storage
*right
)
1313 struct storage
*eax_edx
;
1314 struct storage
*reg
, *new;
1315 struct storage
*val
= new_storage(STOR_VALUE
);
1317 emit_comment("begin DIVIDE");
1318 eax_edx
= get_hardreg(hardreg_storage_table
+ EAX_EDX
, 1);
1321 val
->flags
= STOR_WANTS_FREE
;
1322 emit_move(val
, REG_EDX
, NULL
, NULL
);
1324 new = stack_alloc(expr
->ctype
->bit_size
/ 8);
1326 /* EAX is dividend */
1327 emit_move(left
, REG_EAX
, NULL
, NULL
);
1329 reg
= get_reg_value(right
, ®class_32
);
1332 insn("div", reg
, REG_EAX
, NULL
);
1336 if (expr
->op
== '%')
1338 emit_move(reg
, new, NULL
, NULL
);
1341 emit_comment("end DIVIDE");
1345 static struct storage
*emit_binop(struct expression
*expr
)
1347 struct storage
*left
= x86_expression(expr
->left
);
1348 struct storage
*right
= x86_expression(expr
->right
);
1349 struct storage
*new;
1350 struct storage
*dest
, *src
;
1351 const char *opname
= NULL
;
1352 const char *suffix
= NULL
;
1356 /* Divides have special register constraints */
1357 if ((expr
->op
== '/') || (expr
->op
== '%'))
1358 return emit_divide(expr
, left
, right
);
1360 is_signed
= type_is_signed(expr
->ctype
);
1378 case SPECIAL_LEFTSHIFT
:
1381 case SPECIAL_RIGHTSHIFT
:
1393 case SPECIAL_LOGICAL_AND
:
1394 warning(expr
->pos
, "bogus bitwise and for logical op (should use '2*setne + and' or something)");
1397 case SPECIAL_LOGICAL_OR
:
1398 warning(expr
->pos
, "bogus bitwise or for logical op (should use 'or + setne' or something)");
1402 error_die(expr
->pos
, "unhandled binop '%s'\n", show_special(expr
->op
));
1406 dest
= get_reg_value(right
, ®class_32
);
1407 src
= get_reg_value(left
, ®class_32
);
1408 switch (expr
->ctype
->bit_size
) {
1419 suffix
= "q"; /* FIXME */
1426 snprintf(opstr
, sizeof(opstr
), "%s%s", opname
, suffix
);
1429 insn(opstr
, src
, dest
, NULL
);
1432 /* store result in new pseudo / stack slot */
1433 new = stack_alloc(expr
->ctype
->bit_size
/ 8);
1434 emit_move(dest
, new, NULL
, "end EXPR_BINOP");
1441 static int emit_conditional_test(struct storage
*val
)
1443 struct storage
*reg
;
1444 struct storage
*target_val
;
1447 /* load result into EAX */
1448 emit_comment("begin if/conditional");
1449 reg
= get_reg_value(val
, ®class_32
);
1451 /* compare result with zero */
1452 insn("test", reg
, reg
, NULL
);
1455 /* create conditional-failed label to jump to */
1456 target_false
= new_label();
1457 target_val
= new_storage(STOR_LABEL
);
1458 target_val
->label
= target_false
;
1459 target_val
->flags
= STOR_WANTS_FREE
;
1460 insn("jz", target_val
, NULL
, NULL
);
1462 return target_false
;
1465 static int emit_conditional_end(int target_false
)
1467 struct storage
*cond_end_st
;
1470 /* finished generating code for if-true statement.
1471 * add a jump-to-end jump to avoid falling through
1472 * to the if-false statement code.
1474 cond_end
= new_label();
1475 cond_end_st
= new_storage(STOR_LABEL
);
1476 cond_end_st
->label
= cond_end
;
1477 cond_end_st
->flags
= STOR_WANTS_FREE
;
1478 insn("jmp", cond_end_st
, NULL
, NULL
);
1480 /* if we have both if-true and if-false statements,
1481 * the failed-conditional case will fall through to here
1483 emit_label(target_false
, NULL
);
1488 static void emit_if_conditional(struct statement
*stmt
)
1490 struct storage
*val
;
1493 /* emit test portion of conditional */
1494 val
= x86_expression(stmt
->if_conditional
);
1495 cond_end
= emit_conditional_test(val
);
1497 /* emit if-true statement */
1498 x86_statement(stmt
->if_true
);
1500 /* emit if-false statement, if present */
1501 if (stmt
->if_false
) {
1502 cond_end
= emit_conditional_end(cond_end
);
1503 x86_statement(stmt
->if_false
);
1506 /* end of conditional; jump target for if-true branch */
1507 emit_label(cond_end
, "end if");
1510 static struct storage
*emit_inc_dec(struct expression
*expr
, int postop
)
1512 struct storage
*addr
= x86_address_gen(expr
->unop
);
1513 struct storage
*retval
;
1516 strcpy(opname
, opbits(expr
->op
== SPECIAL_INCREMENT
? "inc" : "dec",
1517 expr
->ctype
->bit_size
));
1520 struct storage
*new = stack_alloc(4);
1522 emit_copy(new, addr
, expr
->unop
->ctype
);
1528 insn(opname
, addr
, NULL
, NULL
);
1533 static struct storage
*emit_postop(struct expression
*expr
)
1535 return emit_inc_dec(expr
, 1);
1538 static struct storage
*emit_return_stmt(struct statement
*stmt
)
1540 struct function
*f
= current_func
;
1541 struct expression
*expr
= stmt
->ret_value
;
1542 struct storage
*val
= NULL
, *jmplbl
;
1544 if (expr
&& expr
->ctype
) {
1545 val
= x86_expression(expr
);
1546 assert(val
!= NULL
);
1547 emit_move(val
, REG_EAX
, expr
->ctype
, "return");
1550 jmplbl
= new_storage(STOR_LABEL
);
1551 jmplbl
->flags
|= STOR_WANTS_FREE
;
1552 jmplbl
->label
= f
->ret_target
;
1553 insn("jmp", jmplbl
, NULL
, NULL
);
1558 static struct storage
*emit_conditional_expr(struct expression
*expr
)
1560 struct storage
*cond
, *true = NULL
, *false = NULL
;
1561 struct storage
*new = stack_alloc(expr
->ctype
->bit_size
/ 8);
1562 int target_false
, cond_end
;
1564 /* evaluate conditional */
1565 cond
= x86_expression(expr
->conditional
);
1566 target_false
= emit_conditional_test(cond
);
1568 /* handle if-true part of the expression */
1569 true = x86_expression(expr
->cond_true
);
1571 emit_copy(new, true, expr
->ctype
);
1573 cond_end
= emit_conditional_end(target_false
);
1575 /* handle if-false part of the expression */
1576 false = x86_expression(expr
->cond_false
);
1578 emit_copy(new, false, expr
->ctype
);
1580 /* end of conditional; jump target for if-true branch */
1581 emit_label(cond_end
, "end conditional");
1586 static struct storage
*emit_select_expr(struct expression
*expr
)
1588 struct storage
*cond
= x86_expression(expr
->conditional
);
1589 struct storage
*true = x86_expression(expr
->cond_true
);
1590 struct storage
*false = x86_expression(expr
->cond_false
);
1591 struct storage
*reg_cond
, *reg_true
, *reg_false
;
1592 struct storage
*new = stack_alloc(4);
1594 emit_comment("begin SELECT");
1595 reg_cond
= get_reg_value(cond
, get_regclass(expr
->conditional
));
1596 reg_true
= get_reg_value(true, get_regclass(expr
));
1597 reg_false
= get_reg_value(false, get_regclass(expr
));
1600 * Do the actual select: check the conditional for zero,
1601 * move false over true if zero
1603 insn("test", reg_cond
, reg_cond
, NULL
);
1604 insn("cmovz", reg_false
, reg_true
, NULL
);
1607 emit_move(reg_true
, new, expr
->ctype
, NULL
);
1611 emit_comment("end SELECT");
1615 static struct storage
*emit_symbol_expr_init(struct symbol
*sym
)
1617 struct expression
*expr
= sym
->initializer
;
1618 struct symbol_private
*priv
= sym
->aux
;
1621 priv
= calloc(1, sizeof(*priv
));
1625 struct storage
*new = stack_alloc(4);
1626 fprintf(stderr
, "FIXME! no value for symbol %s. creating pseudo %d (stack offset %d)\n",
1627 show_ident(sym
->ident
),
1628 new->pseudo
, new->pseudo
* 4);
1631 priv
->addr
= x86_expression(expr
);
1638 static struct storage
*emit_string_expr(struct expression
*expr
)
1640 struct function
*f
= current_func
;
1641 int label
= new_label();
1642 struct storage
*new;
1644 push_cstring(f
, expr
->string
, label
);
1646 new = new_storage(STOR_LABEL
);
1648 new->flags
= STOR_LABEL_VAL
| STOR_WANTS_FREE
;
1652 static struct storage
*emit_cast_expr(struct expression
*expr
)
1654 struct symbol
*old_type
, *new_type
;
1655 struct storage
*op
= x86_expression(expr
->cast_expression
);
1656 int oldbits
, newbits
;
1657 struct storage
*new;
1659 old_type
= expr
->cast_expression
->ctype
;
1660 new_type
= expr
->cast_type
;
1662 oldbits
= old_type
->bit_size
;
1663 newbits
= new_type
->bit_size
;
1664 if (oldbits
>= newbits
)
1667 emit_move(op
, REG_EAX
, old_type
, "begin cast ..");
1669 new = stack_alloc(newbits
/ 8);
1670 emit_move(REG_EAX
, new, new_type
, ".... end cast");
1675 static struct storage
*emit_regular_preop(struct expression
*expr
)
1677 struct storage
*target
= x86_expression(expr
->unop
);
1678 struct storage
*val
, *new = stack_alloc(4);
1679 const char *opname
= NULL
;
1683 val
= new_storage(STOR_VALUE
);
1684 val
->flags
= STOR_WANTS_FREE
;
1685 emit_move(val
, REG_EDX
, NULL
, NULL
);
1686 emit_move(target
, REG_EAX
, expr
->unop
->ctype
, NULL
);
1687 insn("test", REG_EAX
, REG_EAX
, NULL
);
1688 insn("setz", REG_DL
, NULL
, NULL
);
1689 emit_move(REG_EDX
, new, expr
->unop
->ctype
, NULL
);
1697 emit_move(target
, REG_EAX
, expr
->unop
->ctype
, NULL
);
1698 insn(opname
, REG_EAX
, NULL
, NULL
);
1699 emit_move(REG_EAX
, new, expr
->unop
->ctype
, NULL
);
1709 static void emit_case_statement(struct statement
*stmt
)
1711 emit_labelsym(stmt
->case_label
, NULL
);
1712 x86_statement(stmt
->case_statement
);
1715 static void emit_switch_statement(struct statement
*stmt
)
1717 struct storage
*val
= x86_expression(stmt
->switch_expression
);
1718 struct symbol
*sym
, *default_sym
= NULL
;
1719 struct storage
*labelsym
, *label
;
1722 emit_move(val
, REG_EAX
, stmt
->switch_expression
->ctype
, "begin case");
1725 * This is where a _real_ back-end would go through the
1726 * cases to decide whether to use a lookup table or a
1727 * series of comparisons etc
1729 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
1730 struct statement
*case_stmt
= sym
->stmt
;
1731 struct expression
*expr
= case_stmt
->case_expression
;
1732 struct expression
*to
= case_stmt
->case_to
;
1740 struct storage
*case_val
= new_val(expr
->value
);
1742 assert (expr
->type
== EXPR_VALUE
);
1744 insn("cmpl", case_val
, REG_EAX
, NULL
);
1747 labelsym
= new_labelsym(sym
);
1748 insn("je", labelsym
, NULL
, NULL
);
1752 label
= new_storage(STOR_LABEL
);
1753 label
->flags
|= STOR_WANTS_FREE
;
1754 label
->label
= next_test
= new_label();
1756 /* FIXME: signed/unsigned */
1757 insn("jl", label
, NULL
, NULL
);
1759 case_val
= new_val(to
->value
);
1760 insn("cmpl", case_val
, REG_EAX
, NULL
);
1762 /* TODO: implement and use refcounting... */
1763 label
= new_storage(STOR_LABEL
);
1764 label
->flags
|= STOR_WANTS_FREE
;
1765 label
->label
= next_test
;
1767 /* FIXME: signed/unsigned */
1768 insn("jg", label
, NULL
, NULL
);
1770 labelsym
= new_labelsym(sym
);
1771 insn("jmp", labelsym
, NULL
, NULL
);
1773 emit_label(next_test
, NULL
);
1776 } END_FOR_EACH_PTR(sym
);
1779 labelsym
= new_labelsym(default_sym
);
1780 insn("jmp", labelsym
, NULL
, "default");
1782 label
= new_storage(STOR_LABEL
);
1783 label
->flags
|= STOR_WANTS_FREE
;
1784 label
->label
= switch_end
= new_label();
1785 insn("jmp", label
, NULL
, "goto end of switch");
1788 x86_statement(stmt
->switch_statement
);
1790 if (stmt
->switch_break
->used
)
1791 emit_labelsym(stmt
->switch_break
, NULL
);
1794 emit_label(switch_end
, NULL
);
1797 static void x86_struct_member(struct symbol
*sym
)
1799 printf("\t%s:%d:%ld at offset %ld.%d", show_ident(sym
->ident
), sym
->bit_size
, sym
->ctype
.alignment
, sym
->offset
, sym
->bit_offset
);
1803 static void x86_symbol(struct symbol
*sym
)
1805 struct symbol
*type
;
1810 type
= sym
->ctype
.base_type
;
1815 * Show actual implementation information
1817 switch (type
->type
) {
1820 if (sym
->initializer
)
1823 emit_array_noinit(sym
);
1827 if (sym
->initializer
) {
1828 emit_object_pre(show_ident(sym
->ident
),
1829 sym
->ctype
.modifiers
,
1830 sym
->ctype
.alignment
,
1832 emit_scalar(sym
->initializer
, sym
->bit_size
);
1835 emit_scalar_noinit(sym
);
1840 struct symbol
*member
;
1843 FOR_EACH_PTR(type
->symbol_list
, member
) {
1844 x86_struct_member(member
);
1845 } END_FOR_EACH_PTR(member
);
1851 struct statement
*stmt
= type
->stmt
;
1854 x86_statement(stmt
);
1855 emit_func_post(sym
);
1864 if (sym
->initializer
&& (type
->type
!= SYM_BASETYPE
) &&
1865 (type
->type
!= SYM_ARRAY
)) {
1867 x86_expression(sym
->initializer
);
1871 static void x86_symbol_init(struct symbol
*sym
);
1873 static void x86_symbol_decl(struct symbol_list
*syms
)
1876 FOR_EACH_PTR(syms
, sym
) {
1877 x86_symbol_init(sym
);
1878 } END_FOR_EACH_PTR(sym
);
1881 static void loopstk_push(int cont_lbl
, int loop_bottom_lbl
)
1883 struct function
*f
= current_func
;
1884 struct loop_stack
*ls
;
1886 ls
= malloc(sizeof(*ls
));
1887 ls
->continue_lbl
= cont_lbl
;
1888 ls
->loop_bottom_lbl
= loop_bottom_lbl
;
1889 ls
->next
= f
->loop_stack
;
1893 static void loopstk_pop(void)
1895 struct function
*f
= current_func
;
1896 struct loop_stack
*ls
;
1898 assert(f
->loop_stack
!= NULL
);
1900 f
->loop_stack
= f
->loop_stack
->next
;
1904 static int loopstk_break(void)
1906 return current_func
->loop_stack
->loop_bottom_lbl
;
1909 static int loopstk_continue(void)
1911 return current_func
->loop_stack
->continue_lbl
;
1914 static void emit_loop(struct statement
*stmt
)
1916 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
1917 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
1918 struct statement
*statement
= stmt
->iterator_statement
;
1919 struct statement
*post_statement
= stmt
->iterator_post_statement
;
1920 struct expression
*post_condition
= stmt
->iterator_post_condition
;
1921 int loop_top
= 0, loop_bottom
, loop_continue
;
1922 int have_bottom
= 0;
1923 struct storage
*val
;
1925 loop_bottom
= new_label();
1926 loop_continue
= new_label();
1927 loopstk_push(loop_continue
, loop_bottom
);
1929 x86_symbol_decl(stmt
->iterator_syms
);
1930 x86_statement(pre_statement
);
1931 if (!post_condition
|| post_condition
->type
!= EXPR_VALUE
|| post_condition
->value
) {
1932 loop_top
= new_label();
1933 emit_label(loop_top
, "loop top");
1935 if (pre_condition
) {
1936 if (pre_condition
->type
== EXPR_VALUE
) {
1937 if (!pre_condition
->value
) {
1938 struct storage
*lbv
;
1939 lbv
= new_storage(STOR_LABEL
);
1940 lbv
->label
= loop_bottom
;
1941 lbv
->flags
= STOR_WANTS_FREE
;
1942 insn("jmp", lbv
, NULL
, "go to loop bottom");
1946 struct storage
*lbv
= new_storage(STOR_LABEL
);
1947 lbv
->label
= loop_bottom
;
1948 lbv
->flags
= STOR_WANTS_FREE
;
1951 val
= x86_expression(pre_condition
);
1953 emit_move(val
, REG_EAX
, NULL
, "loop pre condition");
1954 insn("test", REG_EAX
, REG_EAX
, NULL
);
1955 insn("jz", lbv
, NULL
, NULL
);
1958 x86_statement(statement
);
1959 if (stmt
->iterator_continue
->used
)
1960 emit_label(loop_continue
, "'continue' iterator");
1961 x86_statement(post_statement
);
1962 if (!post_condition
) {
1963 struct storage
*lbv
= new_storage(STOR_LABEL
);
1964 lbv
->label
= loop_top
;
1965 lbv
->flags
= STOR_WANTS_FREE
;
1966 insn("jmp", lbv
, NULL
, "go to loop top");
1967 } else if (post_condition
->type
== EXPR_VALUE
) {
1968 if (post_condition
->value
) {
1969 struct storage
*lbv
= new_storage(STOR_LABEL
);
1970 lbv
->label
= loop_top
;
1971 lbv
->flags
= STOR_WANTS_FREE
;
1972 insn("jmp", lbv
, NULL
, "go to loop top");
1975 struct storage
*lbv
= new_storage(STOR_LABEL
);
1976 lbv
->label
= loop_top
;
1977 lbv
->flags
= STOR_WANTS_FREE
;
1979 val
= x86_expression(post_condition
);
1981 emit_move(val
, REG_EAX
, NULL
, "loop post condition");
1982 insn("test", REG_EAX
, REG_EAX
, NULL
);
1983 insn("jnz", lbv
, NULL
, NULL
);
1985 if (have_bottom
|| stmt
->iterator_break
->used
)
1986 emit_label(loop_bottom
, "loop bottom");
1992 * Print out a statement
1994 static struct storage
*x86_statement(struct statement
*stmt
)
1998 switch (stmt
->type
) {
2002 return emit_return_stmt(stmt
);
2003 case STMT_DECLARATION
:
2004 x86_symbol_decl(stmt
->declaration
);
2006 case STMT_COMPOUND
: {
2007 struct statement
*s
;
2008 struct storage
*last
= NULL
;
2010 FOR_EACH_PTR(stmt
->stmts
, s
) {
2011 last
= x86_statement(s
);
2012 } END_FOR_EACH_PTR(s
);
2017 case STMT_EXPRESSION
:
2018 return x86_expression(stmt
->expression
);
2020 emit_if_conditional(stmt
);
2024 emit_case_statement(stmt
);
2027 emit_switch_statement(stmt
);
2038 printf(".L%p:\n", stmt
->label_identifier
);
2039 x86_statement(stmt
->label_statement
);
2043 if (stmt
->goto_expression
) {
2044 struct storage
*val
= x86_expression(stmt
->goto_expression
);
2045 printf("\tgoto *v%d\n", val
->pseudo
);
2046 } else if (!strcmp("break", show_ident(stmt
->goto_label
->ident
))) {
2047 struct storage
*lbv
= new_storage(STOR_LABEL
);
2048 lbv
->label
= loopstk_break();
2049 lbv
->flags
= STOR_WANTS_FREE
;
2050 insn("jmp", lbv
, NULL
, "'break'; go to loop bottom");
2051 } else if (!strcmp("continue", show_ident(stmt
->goto_label
->ident
))) {
2052 struct storage
*lbv
= new_storage(STOR_LABEL
);
2053 lbv
->label
= loopstk_continue();
2054 lbv
->flags
= STOR_WANTS_FREE
;
2055 insn("jmp", lbv
, NULL
, "'continue'; go to loop top");
2057 struct storage
*labelsym
= new_labelsym(stmt
->goto_label
);
2058 insn("jmp", labelsym
, NULL
, NULL
);
2062 printf("\tasm( .... )\n");
2068 static struct storage
*x86_call_expression(struct expression
*expr
)
2070 struct function
*f
= current_func
;
2071 struct symbol
*direct
;
2072 struct expression
*arg
, *fn
;
2073 struct storage
*retval
, *fncall
;
2078 warning(expr
->pos
, "\tcall with no type!");
2083 FOR_EACH_PTR_REVERSE(expr
->args
, arg
) {
2084 struct storage
*new = x86_expression(arg
);
2085 int size
= arg
->ctype
->bit_size
;
2088 * FIXME: i386 SysV ABI dictates that values
2089 * smaller than 32 bits should be placed onto
2090 * the stack as 32-bit objects. We should not
2091 * blindly do a 32-bit push on objects smaller
2096 insn("pushl", new, NULL
,
2097 !framesize
? "begin function call" : NULL
);
2099 framesize
+= bits_to_bytes(size
);
2100 } END_FOR_EACH_PTR_REVERSE(arg
);
2104 /* Remove dereference, if any */
2106 if (fn
->type
== EXPR_PREOP
) {
2107 if (fn
->unop
->type
== EXPR_SYMBOL
) {
2108 struct symbol
*sym
= fn
->unop
->symbol
;
2109 if (sym
->ctype
.base_type
->type
== SYM_FN
)
2114 struct storage
*direct_stor
= new_storage(STOR_SYM
);
2115 direct_stor
->flags
|= STOR_WANTS_FREE
;
2116 direct_stor
->sym
= direct
;
2117 insn("call", direct_stor
, NULL
, NULL
);
2119 fncall
= x86_expression(fn
);
2120 emit_move(fncall
, REG_EAX
, fn
->ctype
, NULL
);
2122 strcpy(s
, "\tcall\t*%eax\n");
2123 push_text_atom(f
, s
);
2126 /* FIXME: pay attention to BITS_IN_POINTER */
2128 struct storage
*val
= new_storage(STOR_VALUE
);
2129 val
->value
= (long long) framesize
;
2130 val
->flags
= STOR_WANTS_FREE
;
2131 insn("addl", val
, REG_ESP
, NULL
);
2134 retval
= stack_alloc(4);
2135 emit_move(REG_EAX
, retval
, NULL
, "end function call");
2140 static struct storage
*x86_address_gen(struct expression
*expr
)
2142 struct function
*f
= current_func
;
2143 struct storage
*addr
;
2144 struct storage
*new;
2147 addr
= x86_expression(expr
->unop
);
2148 if (expr
->unop
->type
== EXPR_SYMBOL
)
2151 emit_move(addr
, REG_EAX
, NULL
, "begin deref ..");
2153 /* FIXME: operand size */
2154 strcpy(s
, "\tmovl\t(%eax), %ecx\n");
2155 push_text_atom(f
, s
);
2157 new = stack_alloc(4);
2158 emit_move(REG_ECX
, new, NULL
, ".... end deref");
2163 static struct storage
*x86_assignment(struct expression
*expr
)
2165 struct expression
*target
= expr
->left
;
2166 struct storage
*val
, *addr
;
2171 val
= x86_expression(expr
->right
);
2172 addr
= x86_address_gen(target
);
2174 switch (val
->type
) {
2175 /* copy, where both operands are memory */
2178 emit_copy(addr
, val
, expr
->ctype
);
2181 /* copy, one or zero operands are memory */
2186 emit_move(val
, addr
, expr
->left
->ctype
, NULL
);
2196 static int x86_initialization(struct symbol
*sym
, struct expression
*expr
)
2198 struct storage
*val
, *addr
;
2204 bits
= expr
->ctype
->bit_size
;
2205 val
= x86_expression(expr
);
2206 addr
= x86_symbol_expr(sym
);
2207 // FIXME! The "target" expression is for bitfield store information.
2208 // Leave it NULL, which works fine.
2209 emit_store(NULL
, addr
, val
, bits
);
2213 static struct storage
*x86_access(struct expression
*expr
)
2215 return x86_address_gen(expr
);
2218 static struct storage
*x86_preop(struct expression
*expr
)
2221 * '*' is an lvalue access, and is fundamentally different
2222 * from an arithmetic operation. Maybe it should have an
2223 * expression type of its own..
2225 if (expr
->op
== '*')
2226 return x86_access(expr
);
2227 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
2228 return emit_inc_dec(expr
, 0);
2229 return emit_regular_preop(expr
);
2232 static struct storage
*x86_symbol_expr(struct symbol
*sym
)
2234 struct storage
*new = stack_alloc(4);
2236 if (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_EXTERN
| MOD_STATIC
)) {
2237 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer
, new->pseudo
, show_ident(sym
->ident
));
2240 if (sym
->ctype
.modifiers
& MOD_ADDRESSABLE
) {
2241 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer
, new->pseudo
, sym
->value
);
2244 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer
, new->pseudo
, show_ident(sym
->ident
), sym
);
2248 static void x86_symbol_init(struct symbol
*sym
)
2250 struct symbol_private
*priv
= sym
->aux
;
2251 struct expression
*expr
= sym
->initializer
;
2252 struct storage
*new;
2255 new = x86_expression(expr
);
2257 new = stack_alloc(sym
->bit_size
/ 8);
2260 priv
= calloc(1, sizeof(*priv
));
2262 /* FIXME: leak! we don't free... */
2263 /* (well, we don't free symbols either) */
2269 static int type_is_signed(struct symbol
*sym
)
2271 if (sym
->type
== SYM_NODE
)
2272 sym
= sym
->ctype
.base_type
;
2273 if (sym
->type
== SYM_PTR
)
2275 return !(sym
->ctype
.modifiers
& MOD_UNSIGNED
);
2278 static struct storage
*x86_label_expr(struct expression
*expr
)
2280 struct storage
*new = stack_alloc(4);
2281 printf("\tmovi.%d\t\tv%d,.L%p\n", bits_in_pointer
, new->pseudo
, expr
->label_symbol
);
2285 static struct storage
*x86_statement_expr(struct expression
*expr
)
2287 return x86_statement(expr
->statement
);
2290 static int x86_position_expr(struct expression
*expr
, struct symbol
*base
)
2292 struct storage
*new = x86_expression(expr
->init_expr
);
2293 struct symbol
*ctype
= expr
->init_expr
->ctype
;
2295 printf("\tinsert v%d at [%d:%d] of %s\n", new->pseudo
,
2296 expr
->init_offset
, ctype
->bit_offset
,
2297 show_ident(base
->ident
));
2301 static void x86_initializer_expr(struct expression
*expr
, struct symbol
*ctype
)
2303 struct expression
*entry
;
2305 FOR_EACH_PTR(expr
->expr_list
, entry
) {
2306 // Nested initializers have their positions already
2307 // recursively calculated - just output them too
2308 if (entry
->type
== EXPR_INITIALIZER
) {
2309 x86_initializer_expr(entry
, ctype
);
2313 // Ignore initializer indexes and identifiers - the
2314 // evaluator has taken them into account
2315 if (entry
->type
== EXPR_IDENTIFIER
|| entry
->type
== EXPR_INDEX
)
2317 if (entry
->type
== EXPR_POS
) {
2318 x86_position_expr(entry
, ctype
);
2321 x86_initialization(ctype
, entry
);
2322 } END_FOR_EACH_PTR(entry
);
2326 * Print out an expression. Return the pseudo that contains the
2329 static struct storage
*x86_expression(struct expression
*expr
)
2335 struct position
*pos
= &expr
->pos
;
2336 printf("\tno type at %s:%d:%d\n",
2337 stream_name(pos
->stream
),
2338 pos
->line
, pos
->pos
);
2342 switch (expr
->type
) {
2346 return x86_call_expression(expr
);
2348 case EXPR_ASSIGNMENT
:
2349 return x86_assignment(expr
);
2352 return emit_compare(expr
);
2356 return emit_binop(expr
);
2358 return x86_preop(expr
);
2360 return emit_postop(expr
);
2362 return emit_symbol_expr_init(expr
->symbol
);
2366 warning(expr
->pos
, "invalid expression after evaluation");
2369 case EXPR_FORCE_CAST
:
2370 case EXPR_IMPLIED_CAST
:
2371 return emit_cast_expr(expr
);
2373 return emit_value(expr
);
2375 return emit_string_expr(expr
);
2376 case EXPR_INITIALIZER
:
2377 x86_initializer_expr(expr
, expr
->ctype
);
2380 return emit_select_expr(expr
);
2381 case EXPR_CONDITIONAL
:
2382 return emit_conditional_expr(expr
);
2383 case EXPR_STATEMENT
:
2384 return x86_statement_expr(expr
);
2386 return x86_label_expr(expr
);
2388 // None of these should exist as direct expressions: they are only
2389 // valid as sub-expressions of initializers.
2391 warning(expr
->pos
, "unable to show plain initializer position expression");
2393 case EXPR_IDENTIFIER
:
2394 warning(expr
->pos
, "unable to show identifier expression");
2397 warning(expr
->pos
, "unable to show index expression");
2400 warning(expr
->pos
, "unable to show type expression");
2403 warning(expr
->pos
, "floating point support is not implemented");