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)
40 #include "expression.h"
46 unsigned int len
; /* does NOT include terminating null */
55 struct loop_stack
*next
;
60 DECLARE_PTR_LIST(str_list
, struct atom
);
61 DECLARE_PTR_LIST(atom_list
, struct atom
);
62 DECLARE_PTR_LIST(storage_list
, struct storage
);
67 struct storage_list
*pseudo_list
;
68 struct atom_list
*atom_list
;
69 struct str_list
*str_list
;
70 struct loop_stack
*loop_stack
;
77 STOR_PSEUDO
, /* variable stored on the stack */
78 STOR_ARG
, /* function argument */
79 STOR_SYM
, /* a symbol we can directly ref in the asm */
80 STOR_REG
, /* scratch register */
81 STOR_VALUE
, /* integer constant */
82 STOR_LABEL
, /* label / jump target */
83 STOR_LABELSYM
, /* label generated from symbol's pointer value */
88 struct storage
*contains
;
89 const unsigned char aliases
[12];
90 #define own_regno aliases[0]
94 enum storage_type type
;
126 struct symbol
*labelsym
;
132 STOR_LABEL_VAL
= (1 << 0),
133 STOR_WANTS_FREE
= (1 << 1),
136 struct symbol_private
{
137 struct storage
*addr
;
152 unsigned int text_len
; /* w/o terminating null */
155 /* stuff for insns */
163 /* stuff for C strings */
165 struct string
*string
;
172 static struct function
*current_func
= NULL
;
173 static struct textbuf
*unit_post_text
= NULL
;
174 static const char *current_section
;
176 static void emit_comment(const char * fmt
, ...);
177 static void emit_move(struct storage
*src
, struct storage
*dest
,
178 struct symbol
*ctype
, const char *comment
);
179 static int type_is_signed(struct symbol
*sym
);
180 static struct storage
*x86_address_gen(struct expression
*expr
);
181 static struct storage
*x86_symbol_expr(struct symbol
*sym
);
182 static void x86_symbol(struct symbol
*sym
);
183 static struct storage
*x86_statement(struct statement
*stmt
);
184 static struct storage
*x86_expression(struct expression
*expr
);
188 AL
, DL
, CL
, BL
, AH
, DH
, CH
, BH
, // 8-bit
189 AX
, DX
, CX
, BX
, SI
, DI
, BP
, SP
, // 16-bit
190 EAX
, EDX
, ECX
, EBX
, ESI
, EDI
, EBP
, ESP
, // 32-bit
191 EAX_EDX
, ECX_EBX
, ESI_EDI
, // 64-bit
194 /* This works on regno's, reg_info's and hardreg_storage's */
195 #define byte_reg(reg) ((reg) - 16)
196 #define highbyte_reg(reg) ((reg)-12)
197 #define word_reg(reg) ((reg)-8)
199 #define REGINFO(nr, str, conflicts...) [nr] = { .name = str, .aliases = { nr , conflicts } }
201 static struct reg_info reg_info_table
[] = {
202 REGINFO( AL
, "%al", AX
, EAX
, EAX_EDX
),
203 REGINFO( DL
, "%dl", DX
, EDX
, EAX_EDX
),
204 REGINFO( CL
, "%cl", CX
, ECX
, ECX_EBX
),
205 REGINFO( BL
, "%bl", BX
, EBX
, ECX_EBX
),
206 REGINFO( AH
, "%ah", AX
, EAX
, EAX_EDX
),
207 REGINFO( DH
, "%dh", DX
, EDX
, EAX_EDX
),
208 REGINFO( CH
, "%ch", CX
, ECX
, ECX_EBX
),
209 REGINFO( BH
, "%bh", BX
, EBX
, ECX_EBX
),
210 REGINFO( AX
, "%ax", AL
, AH
, EAX
, EAX_EDX
),
211 REGINFO( DX
, "%dx", DL
, DH
, EDX
, EAX_EDX
),
212 REGINFO( CX
, "%cx", CL
, CH
, ECX
, ECX_EBX
),
213 REGINFO( BX
, "%bx", BL
, BH
, EBX
, ECX_EBX
),
214 REGINFO( SI
, "%si", ESI
, ESI_EDI
),
215 REGINFO( DI
, "%di", EDI
, ESI_EDI
),
216 REGINFO( BP
, "%bp", EBP
),
217 REGINFO( SP
, "%sp", ESP
),
218 REGINFO(EAX
, "%eax", AL
, AH
, AX
, EAX_EDX
),
219 REGINFO(EDX
, "%edx", DL
, DH
, DX
, EAX_EDX
),
220 REGINFO(ECX
, "%ecx", CL
, CH
, CX
, ECX_EBX
),
221 REGINFO(EBX
, "%ebx", BL
, BH
, BX
, ECX_EBX
),
222 REGINFO(ESI
, "%esi", SI
, ESI_EDI
),
223 REGINFO(EDI
, "%edi", DI
, ESI_EDI
),
224 REGINFO(EBP
, "%ebp", BP
),
225 REGINFO(ESP
, "%esp", SP
),
226 REGINFO(EAX_EDX
, "%eax:%edx", AL
, AH
, AX
, EAX
, DL
, DH
, DX
, EDX
),
227 REGINFO(ECX_EBX
, "%ecx:%ebx", CL
, CH
, CX
, ECX
, BL
, BH
, BX
, EBX
),
228 REGINFO(ESI_EDI
, "%esi:%edi", SI
, ESI
, DI
, EDI
),
231 #define REGSTORAGE(nr) [nr] = { .type = STOR_REG, .reg = reg_info_table + (nr) }
233 static struct storage hardreg_storage_table
[] = {
234 REGSTORAGE(AL
), REGSTORAGE(DL
), REGSTORAGE(CL
), REGSTORAGE(BL
),
235 REGSTORAGE(AH
), REGSTORAGE(DH
), REGSTORAGE(CH
), REGSTORAGE(BH
),
236 REGSTORAGE(AX
), REGSTORAGE(DX
), REGSTORAGE(CX
), REGSTORAGE(BX
),
237 REGSTORAGE(SI
), REGSTORAGE(DI
), REGSTORAGE(BP
), REGSTORAGE(SP
),
238 REGSTORAGE(EAX
), REGSTORAGE(EDX
), REGSTORAGE(ECX
), REGSTORAGE(EBX
),
239 REGSTORAGE(ESI
), REGSTORAGE(EDI
), REGSTORAGE(EBP
), REGSTORAGE(ESP
),
240 REGSTORAGE(EAX_EDX
), REGSTORAGE(ECX_EBX
), REGSTORAGE(ESI_EDI
),
243 #define REG_EAX (&hardreg_storage_table[EAX])
244 #define REG_ECX (&hardreg_storage_table[ECX])
245 #define REG_EDX (&hardreg_storage_table[EDX])
246 #define REG_ESP (&hardreg_storage_table[ESP])
247 #define REG_DL (&hardreg_storage_table[DL])
248 #define REG_DX (&hardreg_storage_table[DX])
249 #define REG_AL (&hardreg_storage_table[AL])
250 #define REG_AX (&hardreg_storage_table[AX])
252 DECLARE_BITMAP(regs_in_use
, 256);
254 static inline struct storage
* reginfo_reg(struct reg_info
*info
)
256 return hardreg_storage_table
+ info
->own_regno
;
259 static struct storage
* get_hardreg(struct storage
*reg
, int clear
)
261 struct reg_info
*info
= reg
->reg
;
262 const unsigned char *aliases
;
265 aliases
= info
->aliases
;
266 while ((regno
= *aliases
++) != NOREG
) {
267 if (test_bit(regno
, regs_in_use
))
270 reg_info_table
[regno
].contains
= NULL
;
272 set_bit(info
->own_regno
, regs_in_use
);
275 fprintf(stderr
, "register %s is busy\n", info
->name
);
276 if (regno
+ reg_info_table
!= info
)
277 fprintf(stderr
, " conflicts with %s\n", reg_info_table
[regno
].name
);
281 static void put_reg(struct storage
*reg
)
283 struct reg_info
*info
= reg
->reg
;
284 int regno
= info
->own_regno
;
286 if (test_and_clear_bit(regno
, regs_in_use
))
288 fprintf(stderr
, "freeing already free'd register %s\n", reg_info_table
[regno
].name
);
293 const unsigned char regs
[30];
296 static struct regclass regclass_8
= { "8-bit", { AL
, DL
, CL
, BL
, AH
, DH
, CH
, BH
}};
297 static struct regclass regclass_16
= { "16-bit", { AX
, DX
, CX
, BX
, SI
, DI
, BP
}};
298 static struct regclass regclass_32
= { "32-bit", { EAX
, EDX
, ECX
, EBX
, ESI
, EDI
, EBP
}};
299 static struct regclass regclass_64
= { "64-bit", { EAX_EDX
, ECX_EBX
, ESI_EDI
}};
301 static struct regclass regclass_32_8
= { "32-bit bytes", { EAX
, EDX
, ECX
, EBX
}};
303 static struct regclass
*get_regclass_bits(int bits
)
306 case 8: return ®class_8
;
307 case 16: return ®class_16
;
308 case 64: return ®class_64
;
309 default: return ®class_32
;
313 static struct regclass
*get_regclass(struct expression
*expr
)
315 return get_regclass_bits(expr
->ctype
->bit_size
);
318 static int register_busy(int regno
)
320 if (!test_bit(regno
, regs_in_use
)) {
321 struct reg_info
*info
= reg_info_table
+ regno
;
322 const unsigned char *regs
= info
->aliases
+1;
324 while ((regno
= *regs
) != NOREG
) {
326 if (test_bit(regno
, regs_in_use
))
335 static struct storage
*get_reg(struct regclass
*class)
337 const unsigned char *regs
= class->regs
;
340 while ((regno
= *regs
) != NOREG
) {
342 if (register_busy(regno
))
344 return get_hardreg(hardreg_storage_table
+ regno
, 1);
346 fprintf(stderr
, "Ran out of %s registers\n", class->name
);
350 static struct storage
*get_reg_value(struct storage
*value
, struct regclass
*class)
352 struct reg_info
*info
;
355 /* Do we already have it somewhere */
357 if (info
&& info
->contains
== value
) {
358 emit_comment("already have register %s", info
->name
);
359 return get_hardreg(hardreg_storage_table
+ info
->own_regno
, 0);
362 reg
= get_reg(class);
363 emit_move(value
, reg
, value
->ctype
, "reload register");
365 info
->contains
= value
;
370 static struct storage
*temp_from_bits(unsigned int bit_size
)
372 return get_reg(get_regclass_bits(bit_size
));
375 static inline unsigned int pseudo_offset(struct storage
*s
)
377 if (s
->type
!= STOR_PSEUDO
)
378 return 123456; /* intentionally bogus value */
383 static inline unsigned int arg_offset(struct storage
*s
)
385 if (s
->type
!= STOR_ARG
)
386 return 123456; /* intentionally bogus value */
388 /* FIXME: this is wrong wrong wrong */
389 return current_func
->stack_size
+ ((1 + s
->idx
) * 4);
392 static const char *pretty_offset(int ofs
)
394 static char esp_buf
[64];
397 sprintf(esp_buf
, "%d(%%esp)", ofs
);
399 strcpy(esp_buf
, "(%esp)");
404 static void stor_sym_init(struct symbol
*sym
)
406 struct storage
*stor
;
407 struct symbol_private
*priv
;
409 priv
= calloc(1, sizeof(*priv
) + sizeof(*stor
));
411 die("OOM in stor_sym_init");
413 stor
= (struct storage
*) (priv
+ 1);
416 stor
->type
= STOR_SYM
;
420 static const char *stor_op_name(struct storage
*s
)
422 static char name
[32];
426 strcpy(name
, pretty_offset((int) pseudo_offset(s
)));
429 strcpy(name
, pretty_offset((int) arg_offset(s
)));
432 strcpy(name
, show_ident(s
->sym
->ident
));
435 strcpy(name
, s
->reg
->name
);
438 sprintf(name
, "$%Ld", s
->value
);
441 sprintf(name
, "%s.L%d", s
->flags
& STOR_LABEL_VAL
? "$" : "",
445 sprintf(name
, "%s.LS%p", s
->flags
& STOR_LABEL_VAL
? "$" : "",
453 static struct atom
*new_atom(enum atom_type type
)
457 atom
= calloc(1, sizeof(*atom
)); /* TODO: chunked alloc */
466 static inline void push_cstring(struct function
*f
, struct string
*str
,
471 atom
= new_atom(ATOM_CSTR
);
475 add_ptr_list(&f
->str_list
, atom
); /* note: _not_ atom_list */
478 static inline void push_atom(struct function
*f
, struct atom
*atom
)
480 add_ptr_list(&f
->atom_list
, atom
);
483 static void push_text_atom(struct function
*f
, const char *text
)
485 struct atom
*atom
= new_atom(ATOM_TEXT
);
487 atom
->text
= strdup(text
);
488 atom
->text_len
= strlen(text
);
493 static struct storage
*new_storage(enum storage_type type
)
495 struct storage
*stor
;
497 stor
= calloc(1, sizeof(*stor
));
499 die("OOM in new_storage");
506 static struct storage
*stack_alloc(int n_bytes
)
508 struct function
*f
= current_func
;
509 struct storage
*stor
;
513 stor
= new_storage(STOR_PSEUDO
);
514 stor
->type
= STOR_PSEUDO
;
515 stor
->pseudo
= f
->pseudo_nr
;
516 stor
->offset
= f
->stack_size
; /* FIXME: stack req. natural align */
517 stor
->size
= n_bytes
;
518 f
->stack_size
+= n_bytes
;
521 add_ptr_list(&f
->pseudo_list
, stor
);
526 static struct storage
*new_labelsym(struct symbol
*sym
)
528 struct storage
*stor
;
530 stor
= new_storage(STOR_LABELSYM
);
533 stor
->flags
|= STOR_WANTS_FREE
;
534 stor
->labelsym
= sym
;
540 static struct storage
*new_val(long long value
)
542 struct storage
*stor
;
544 stor
= new_storage(STOR_VALUE
);
547 stor
->flags
|= STOR_WANTS_FREE
;
554 static int new_label(void)
556 static int label
= 0;
560 static void textbuf_push(struct textbuf
**buf_p
, const char *text
)
562 struct textbuf
*tmp
, *list
= *buf_p
;
563 unsigned int text_len
= strlen(text
);
564 unsigned int alloc_len
= text_len
+ 1 + sizeof(*list
);
566 tmp
= calloc(1, alloc_len
);
568 die("OOM on textbuf alloc");
570 tmp
->text
= ((void *) tmp
) + sizeof(*tmp
);
571 memcpy(tmp
->text
, text
, text_len
+ 1);
574 /* add to end of list */
579 tmp
->prev
= list
->prev
;
580 tmp
->prev
->next
= tmp
;
588 static void textbuf_emit(struct textbuf
**buf_p
)
590 struct textbuf
*tmp
, *list
= *buf_p
;
594 if (tmp
->next
== tmp
)
597 tmp
->prev
->next
= tmp
->next
;
598 tmp
->next
->prev
= tmp
->prev
;
602 fputs(tmp
->text
, stdout
);
610 static void insn(const char *insn
, struct storage
*op1
, struct storage
*op2
,
611 const char *comment_in
)
613 struct function
*f
= current_func
;
614 struct atom
*atom
= new_atom(ATOM_INSN
);
616 assert(insn
!= NULL
);
618 strcpy(atom
->insn
, insn
);
619 if (comment_in
&& (*comment_in
))
620 strncpy(atom
->comment
, comment_in
,
621 sizeof(atom
->comment
) - 1);
629 static void emit_comment(const char *fmt
, ...)
631 struct function
*f
= current_func
;
632 static char tmpbuf
[100] = "\t# ";
637 i
= vsnprintf(tmpbuf
+3, sizeof(tmpbuf
)-4, fmt
, args
);
641 push_text_atom(f
, tmpbuf
);
644 static void emit_label (int label
, const char *comment
)
646 struct function
*f
= current_func
;
650 sprintf(s
, ".L%d:\n", label
);
652 sprintf(s
, ".L%d:\t\t\t\t\t# %s\n", label
, comment
);
654 push_text_atom(f
, s
);
657 static void emit_labelsym (struct symbol
*sym
, const char *comment
)
659 struct function
*f
= current_func
;
663 sprintf(s
, ".LS%p:\n", sym
);
665 sprintf(s
, ".LS%p:\t\t\t\t# %s\n", sym
, comment
);
667 push_text_atom(f
, s
);
670 void emit_unit_begin(const char *basename
)
672 printf("\t.file\t\"%s\"\n", basename
);
675 void emit_unit_end(void)
677 textbuf_emit(&unit_post_text
);
678 printf("\t.ident\t\"sparse silly x86 backend (built %s)\"\n", __DATE__
);
681 /* conditionally switch sections */
682 static void emit_section(const char *s
)
684 if (s
== current_section
)
686 if (current_section
&& (!strcmp(s
, current_section
)))
693 static void emit_insn_atom(struct function
*f
, struct atom
*atom
)
697 struct storage
*op1
= atom
->op1
;
698 struct storage
*op2
= atom
->op2
;
700 if (atom
->comment
[0])
701 sprintf(comment
, "\t\t# %s", atom
->comment
);
707 strcpy(tmp
, stor_op_name(op1
));
708 sprintf(s
, "\t%s\t%s, %s%s\n",
709 atom
->insn
, tmp
, stor_op_name(op2
), comment
);
710 } else if (atom
->op1
)
711 sprintf(s
, "\t%s\t%s%s%s\n",
712 atom
->insn
, stor_op_name(op1
),
713 comment
[0] ? "\t" : "", comment
);
715 sprintf(s
, "\t%s\t%s%s\n",
717 comment
[0] ? "\t\t" : "", comment
);
719 write(STDOUT_FILENO
, s
, strlen(s
));
722 static void emit_atom_list(struct function
*f
)
726 FOR_EACH_PTR(f
->atom_list
, atom
) {
727 switch (atom
->type
) {
729 ssize_t rc
= write(STDOUT_FILENO
, atom
->text
,
731 (void) rc
; /* FIXME */
735 emit_insn_atom(f
, atom
);
741 } END_FOR_EACH_PTR(atom
);
744 static void emit_string_list(struct function
*f
)
748 emit_section(".section\t.rodata");
750 FOR_EACH_PTR(f
->str_list
, atom
) {
751 /* FIXME: escape " in string */
752 printf(".L%d:\n", atom
->label
);
753 printf("\t.string\t%s\n", show_string(atom
->string
));
756 } END_FOR_EACH_PTR(atom
);
759 static void func_cleanup(struct function
*f
)
761 struct storage
*stor
;
764 FOR_EACH_PTR(f
->pseudo_list
, stor
) {
766 } END_FOR_EACH_PTR(stor
);
768 FOR_EACH_PTR(f
->atom_list
, atom
) {
769 if ((atom
->type
== ATOM_TEXT
) && (atom
->text
))
771 if (atom
->op1
&& (atom
->op1
->flags
& STOR_WANTS_FREE
))
773 if (atom
->op2
&& (atom
->op2
->flags
& STOR_WANTS_FREE
))
776 } END_FOR_EACH_PTR(atom
);
778 free_ptr_list(&f
->pseudo_list
);
782 /* function prologue */
783 static void emit_func_pre(struct symbol
*sym
)
787 unsigned int i
, argc
= 0, alloc_len
;
789 struct symbol_private
*privbase
;
790 struct storage
*storage_base
;
791 struct symbol
*base_type
= sym
->ctype
.base_type
;
793 FOR_EACH_PTR(base_type
->arguments
, arg
) {
795 } END_FOR_EACH_PTR(arg
);
799 (argc
* sizeof(struct symbol
*)) +
800 (argc
* sizeof(struct symbol_private
)) +
801 (argc
* sizeof(struct storage
));
802 mem
= calloc(1, alloc_len
);
804 die("OOM on func info");
806 f
= (struct function
*) mem
;
808 f
->argv
= (struct symbol
**) mem
;
809 mem
+= (argc
* sizeof(struct symbol
*));
810 privbase
= (struct symbol_private
*) mem
;
811 mem
+= (argc
* sizeof(struct symbol_private
));
812 storage_base
= (struct storage
*) mem
;
815 f
->ret_target
= new_label();
818 FOR_EACH_PTR(base_type
->arguments
, arg
) {
820 arg
->aux
= &privbase
[i
];
821 storage_base
[i
].type
= STOR_ARG
;
822 storage_base
[i
].idx
= i
;
823 privbase
[i
].addr
= &storage_base
[i
];
825 } END_FOR_EACH_PTR(arg
);
827 assert(current_func
== NULL
);
831 /* function epilogue */
832 static void emit_func_post(struct symbol
*sym
)
834 const char *name
= show_ident(sym
->ident
);
835 struct function
*f
= current_func
;
836 int stack_size
= f
->stack_size
;
841 /* function prologue */
842 emit_section(".text");
843 if ((sym
->ctype
.modifiers
& MOD_STATIC
) == 0)
844 printf(".globl %s\n", name
);
845 printf("\t.type\t%s, @function\n", name
);
846 printf("%s:\n", name
);
849 char pseudo_const
[16];
851 sprintf(pseudo_const
, "$%d", stack_size
);
852 printf("\tsubl\t%s, %%esp\n", pseudo_const
);
855 /* function epilogue */
857 /* jump target for 'return' statements */
858 emit_label(f
->ret_target
, NULL
);
863 val
= new_storage(STOR_VALUE
);
864 val
->value
= (long long) (stack_size
);
865 val
->flags
= STOR_WANTS_FREE
;
867 insn("addl", val
, REG_ESP
, NULL
);
870 insn("ret", NULL
, NULL
, NULL
);
872 /* output everything to stdout */
873 fflush(stdout
); /* paranoia; needed? */
876 /* function footer */
877 name
= show_ident(sym
->ident
);
878 printf("\t.size\t%s, .-%s\n", name
, name
);
884 /* emit object (a.k.a. variable, a.k.a. data) prologue */
885 static void emit_object_pre(const char *name
, unsigned long modifiers
,
886 unsigned long alignment
, unsigned int byte_size
)
888 if ((modifiers
& MOD_STATIC
) == 0)
889 printf(".globl %s\n", name
);
890 emit_section(".data");
892 printf("\t.align %lu\n", alignment
);
893 printf("\t.type\t%s, @object\n", name
);
894 printf("\t.size\t%s, %d\n", name
, byte_size
);
895 printf("%s:\n", name
);
898 /* emit value (only) for an initializer scalar */
899 static void emit_scalar(struct expression
*expr
, unsigned int bit_size
)
904 assert(expr
->type
== EXPR_VALUE
);
906 if (expr
->value
== 0ULL) {
907 printf("\t.zero\t%d\n", bit_size
/ 8);
911 ll
= (long long) expr
->value
;
914 case 8: type
= "byte"; ll
= (char) ll
; break;
915 case 16: type
= "value"; ll
= (short) ll
; break;
916 case 32: type
= "long"; ll
= (int) ll
; break;
917 case 64: type
= "quad"; break;
918 default: type
= NULL
; break;
921 assert(type
!= NULL
);
923 printf("\t.%s\t%Ld\n", type
, ll
);
926 static void emit_global_noinit(const char *name
, unsigned long modifiers
,
927 unsigned long alignment
, unsigned int byte_size
)
931 if (modifiers
& MOD_STATIC
) {
932 sprintf(s
, "\t.local\t%s\n", name
);
933 textbuf_push(&unit_post_text
, s
);
936 sprintf(s
, "\t.comm\t%s,%d,%lu\n", name
, byte_size
, alignment
);
938 sprintf(s
, "\t.comm\t%s,%d\n", name
, byte_size
);
939 textbuf_push(&unit_post_text
, s
);
942 static int ea_current
, ea_last
;
944 static void emit_initializer(struct symbol
*sym
,
945 struct expression
*expr
)
947 int distance
= ea_current
- ea_last
- 1;
950 printf("\t.zero\t%d\n", (sym
->bit_size
/ 8) * distance
);
952 if (expr
->type
== EXPR_VALUE
) {
953 struct symbol
*base_type
= sym
->ctype
.base_type
;
954 assert(base_type
!= NULL
);
956 emit_scalar(expr
, sym
->bit_size
/ get_expression_value(base_type
->array_size
));
959 if (expr
->type
!= EXPR_INITIALIZER
)
962 assert(0); /* FIXME */
965 static int sort_array_cmp(const struct expression
*a
,
966 const struct expression
*b
)
968 int a_ofs
= 0, b_ofs
= 0;
970 if (a
->type
== EXPR_POS
)
971 a_ofs
= (int) a
->init_offset
;
972 if (b
->type
== EXPR_POS
)
973 b_ofs
= (int) b
->init_offset
;
975 return a_ofs
- b_ofs
;
978 /* move to front-end? */
979 static void sort_array(struct expression
*expr
)
981 struct expression
*entry
, **list
;
982 unsigned int elem
, sorted
, i
;
985 FOR_EACH_PTR(expr
->expr_list
, entry
) {
987 } END_FOR_EACH_PTR(entry
);
992 list
= malloc(sizeof(entry
) * elem
);
994 die("OOM in sort_array");
996 /* this code is no doubt evil and ignores EXPR_INDEX possibly
997 * to its detriment and other nasty things. improvements
1002 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1003 if ((entry
->type
== EXPR_POS
) || (entry
->type
== EXPR_VALUE
)) {
1004 /* add entry to list[], in sorted order */
1011 for (i
= 0; i
< sorted
; i
++)
1012 if (sort_array_cmp(entry
, list
[i
]) <= 0)
1015 /* If inserting into the middle of list[]
1016 * instead of appending, we memmove.
1017 * This is ugly, but thankfully
1018 * uncommon. Input data with tons of
1019 * entries very rarely have explicit
1020 * offsets. convert to qsort eventually...
1023 memmove(&list
[i
+ 1], &list
[i
],
1024 (sorted
- i
) * sizeof(entry
));
1029 } END_FOR_EACH_PTR(entry
);
1032 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1033 if ((entry
->type
== EXPR_POS
) || (entry
->type
== EXPR_VALUE
))
1034 *THIS_ADDRESS(entry
) = list
[i
++];
1035 } END_FOR_EACH_PTR(entry
);
1039 static void emit_array(struct symbol
*sym
)
1041 struct symbol
*base_type
= sym
->ctype
.base_type
;
1042 struct expression
*expr
= sym
->initializer
;
1043 struct expression
*entry
;
1045 assert(base_type
!= NULL
);
1051 emit_object_pre(show_ident(sym
->ident
), sym
->ctype
.modifiers
,
1052 sym
->ctype
.alignment
,
1057 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1058 if (entry
->type
== EXPR_VALUE
) {
1060 emit_initializer(sym
, entry
);
1061 ea_last
= ea_current
;
1062 } else if (entry
->type
== EXPR_POS
) {
1064 entry
->init_offset
/ (base_type
->bit_size
/ 8);
1065 emit_initializer(sym
, entry
->init_expr
);
1066 ea_last
= ea_current
;
1068 } END_FOR_EACH_PTR(entry
);
1071 void emit_one_symbol(struct symbol
*sym
)
1076 static void emit_copy(struct storage
*dest
, struct storage
*src
,
1077 struct symbol
*ctype
)
1079 struct storage
*reg
= NULL
;
1080 unsigned int bit_size
;
1082 /* FIXME: Bitfield copy! */
1084 bit_size
= src
->size
* 8;
1087 if ((src
->type
== STOR_ARG
) && (bit_size
< 32))
1090 reg
= temp_from_bits(bit_size
);
1091 emit_move(src
, reg
, ctype
, "begin copy ..");
1093 bit_size
= dest
->size
* 8;
1096 if ((dest
->type
== STOR_ARG
) && (bit_size
< 32))
1099 emit_move(reg
, dest
, ctype
, ".... end copy");
1103 static void emit_store(struct expression
*dest_expr
, struct storage
*dest
,
1104 struct storage
*src
, int bits
)
1106 /* FIXME: Bitfield store! */
1107 printf("\tst.%d\t\tv%d,[v%d]\n", bits
, src
->pseudo
, dest
->pseudo
);
1110 static void emit_scalar_noinit(struct symbol
*sym
)
1112 emit_global_noinit(show_ident(sym
->ident
),
1113 sym
->ctype
.modifiers
, sym
->ctype
.alignment
,
1118 static void emit_array_noinit(struct symbol
*sym
)
1120 emit_global_noinit(show_ident(sym
->ident
),
1121 sym
->ctype
.modifiers
, sym
->ctype
.alignment
,
1122 get_expression_value(sym
->array_size
) * (sym
->bit_size
/ 8));
1126 static const char *opbits(const char *insn
, unsigned int bits
)
1128 static char opbits_str
[32];
1132 case 8: c
= 'b'; break;
1133 case 16: c
= 'w'; break;
1134 case 32: c
= 'l'; break;
1135 case 64: c
= 'q'; break;
1136 default: abort(); break;
1139 sprintf(opbits_str
, "%s%c", insn
, c
);
1144 static void emit_move(struct storage
*src
, struct storage
*dest
,
1145 struct symbol
*ctype
, const char *comment
)
1148 unsigned int is_signed
;
1149 unsigned int is_dest
= (src
->type
== STOR_REG
);
1153 bits
= ctype
->bit_size
;
1154 is_signed
= type_is_signed(ctype
);
1161 * Are we moving from a register to a register?
1162 * Make the new reg to be the "cache".
1164 if ((dest
->type
== STOR_REG
) && (src
->type
== STOR_REG
)) {
1165 struct storage
*backing
;
1171 backing
= src
->reg
->contains
;
1173 /* Is it still valid? */
1174 if (backing
->reg
!= src
->reg
)
1177 backing
->reg
= dest
->reg
;
1179 dest
->reg
->contains
= backing
;
1180 insn("mov", src
, dest
, NULL
);
1185 * Are we moving to a register from a non-reg?
1187 * See if we have the non-reg source already cached
1190 if (dest
->type
== STOR_REG
) {
1192 struct reg_info
*info
= src
->reg
;
1193 if (info
->contains
== src
) {
1194 src
= reginfo_reg(info
);
1198 dest
->reg
->contains
= src
;
1199 src
->reg
= dest
->reg
;
1202 if (src
->type
== STOR_REG
) {
1203 /* We could just mark the register dirty here and do lazy store.. */
1204 src
->reg
->contains
= dest
;
1205 dest
->reg
= src
->reg
;
1208 if ((bits
== 8) || (bits
== 16)) {
1212 opname
= is_signed
? "movsx" : "movzx";
1216 insn(opbits(opname
, bits
), src
, dest
, comment
);
1219 static struct storage
*emit_compare(struct expression
*expr
)
1221 struct storage
*left
= x86_expression(expr
->left
);
1222 struct storage
*right
= x86_expression(expr
->right
);
1223 struct storage
*reg1
, *reg2
;
1224 struct storage
*new, *val
;
1225 const char *opname
= NULL
;
1226 unsigned int right_bits
= expr
->right
->ctype
->bit_size
;
1229 case '<': opname
= "setl"; break;
1230 case '>': opname
= "setg"; break;
1232 opname
= "setle"; break;
1234 opname
= "setge"; break;
1235 case SPECIAL_EQUAL
: opname
= "sete"; break;
1236 case SPECIAL_NOTEQUAL
: opname
= "setne"; break;
1237 case SPECIAL_UNSIGNED_LT
:
1238 opname
= "setb"; break;
1239 case SPECIAL_UNSIGNED_GT
:
1240 opname
= "seta"; break;
1241 case SPECIAL_UNSIGNED_LTE
:
1242 opname
= "setb"; break;
1243 case SPECIAL_UNSIGNED_GTE
:
1244 opname
= "setae"; break;
1251 val
= new_storage(STOR_VALUE
);
1252 val
->flags
= STOR_WANTS_FREE
;
1254 reg1
= get_reg(®class_32_8
);
1255 emit_move(val
, reg1
, NULL
, NULL
);
1257 /* move op1 into EAX */
1258 reg2
= get_reg_value(left
, get_regclass(expr
->left
));
1260 /* perform comparison, RHS (op1, right) and LHS (op2, EAX) */
1261 insn(opbits("cmp", right_bits
), right
, reg2
, NULL
);
1264 /* store result of operation, 0 or 1, in DL using SETcc */
1265 insn(opname
, byte_reg(reg1
), NULL
, NULL
);
1267 /* finally, store the result (DL) in a new pseudo / stack slot */
1268 new = stack_alloc(4);
1269 emit_move(reg1
, new, NULL
, "end EXPR_COMPARE");
1275 static struct storage
*emit_value(struct expression
*expr
)
1277 #if 0 /* old and slow way */
1278 struct storage
*new = stack_alloc(4);
1279 struct storage
*val
;
1281 val
= new_storage(STOR_VALUE
);
1282 val
->value
= (long long) expr
->value
;
1283 val
->flags
= STOR_WANTS_FREE
;
1284 insn("movl", val
, new, NULL
);
1288 struct storage
*val
;
1290 val
= new_storage(STOR_VALUE
);
1291 val
->value
= (long long) expr
->value
;
1293 return val
; /* FIXME: memory leak */
1297 static struct storage
*emit_divide(struct expression
*expr
, struct storage
*left
, struct storage
*right
)
1299 struct storage
*eax_edx
;
1300 struct storage
*reg
, *new;
1301 struct storage
*val
= new_storage(STOR_VALUE
);
1303 emit_comment("begin DIVIDE");
1304 eax_edx
= get_hardreg(hardreg_storage_table
+ EAX_EDX
, 1);
1307 val
= new_storage(STOR_VALUE
);
1308 val
->flags
= STOR_WANTS_FREE
;
1309 emit_move(val
, REG_EDX
, NULL
, NULL
);
1311 new = stack_alloc(expr
->ctype
->bit_size
/ 8);
1313 /* EAX is dividend */
1314 emit_move(left
, REG_EAX
, NULL
, NULL
);
1316 reg
= get_reg_value(right
, ®class_32
);
1319 insn("div", reg
, REG_EAX
, NULL
);
1323 if (expr
->op
== '%')
1325 emit_move(reg
, new, NULL
, NULL
);
1328 emit_comment("end DIVIDE");
1332 static struct storage
*emit_binop(struct expression
*expr
)
1334 struct storage
*left
= x86_expression(expr
->left
);
1335 struct storage
*right
= x86_expression(expr
->right
);
1336 struct storage
*new;
1337 struct storage
*dest
, *src
;
1338 const char *opname
= NULL
;
1339 const char *suffix
= NULL
;
1343 /* Divides have special register constraints */
1344 if ((expr
->op
== '/') || (expr
->op
== '%'))
1345 return emit_divide(expr
, left
, right
);
1347 is_signed
= type_is_signed(expr
->ctype
);
1365 case SPECIAL_LEFTSHIFT
:
1368 case SPECIAL_RIGHTSHIFT
:
1380 case SPECIAL_LOGICAL_AND
:
1381 warning(expr
->pos
, "bogus bitwise and for logical op (should use '2*setne + and' or something)");
1384 case SPECIAL_LOGICAL_OR
:
1385 warning(expr
->pos
, "bogus bitwise or for logical op (should use 'or + setne' or something)");
1389 error_die(expr
->pos
, "unhandled binop '%s'\n", show_special(expr
->op
));
1393 dest
= get_reg_value(right
, ®class_32
);
1394 src
= get_reg_value(left
, ®class_32
);
1395 switch (expr
->ctype
->bit_size
) {
1406 suffix
= "q"; /* FIXME */
1413 snprintf(opstr
, sizeof(opstr
), "%s%s", opname
, suffix
);
1416 insn(opstr
, src
, dest
, NULL
);
1419 /* store result in new pseudo / stack slot */
1420 new = stack_alloc(expr
->ctype
->bit_size
/ 8);
1421 emit_move(dest
, new, NULL
, "end EXPR_BINOP");
1428 static int emit_conditional_test(struct storage
*val
)
1430 struct storage
*reg
;
1431 struct storage
*target_val
;
1434 /* load result into EAX */
1435 emit_comment("begin if/conditional");
1436 reg
= get_reg_value(val
, ®class_32
);
1438 /* compare result with zero */
1439 insn("test", reg
, reg
, NULL
);
1442 /* create conditional-failed label to jump to */
1443 target_false
= new_label();
1444 target_val
= new_storage(STOR_LABEL
);
1445 target_val
->label
= target_false
;
1446 target_val
->flags
= STOR_WANTS_FREE
;
1447 insn("jz", target_val
, NULL
, NULL
);
1449 return target_false
;
1452 static int emit_conditional_end(int target_false
)
1454 struct storage
*cond_end_st
;
1457 /* finished generating code for if-true statement.
1458 * add a jump-to-end jump to avoid falling through
1459 * to the if-false statement code.
1461 cond_end
= new_label();
1462 cond_end_st
= new_storage(STOR_LABEL
);
1463 cond_end_st
->label
= cond_end
;
1464 cond_end_st
->flags
= STOR_WANTS_FREE
;
1465 insn("jmp", cond_end_st
, NULL
, NULL
);
1467 /* if we have both if-true and if-false statements,
1468 * the failed-conditional case will fall through to here
1470 emit_label(target_false
, NULL
);
1475 static void emit_if_conditional(struct statement
*stmt
)
1477 struct storage
*val
;
1480 /* emit test portion of conditional */
1481 val
= x86_expression(stmt
->if_conditional
);
1482 cond_end
= emit_conditional_test(val
);
1484 /* emit if-true statement */
1485 x86_statement(stmt
->if_true
);
1487 /* emit if-false statement, if present */
1488 if (stmt
->if_false
) {
1489 cond_end
= emit_conditional_end(cond_end
);
1490 x86_statement(stmt
->if_false
);
1493 /* end of conditional; jump target for if-true branch */
1494 emit_label(cond_end
, "end if");
1497 static struct storage
*emit_inc_dec(struct expression
*expr
, int postop
)
1499 struct storage
*addr
= x86_address_gen(expr
->unop
);
1500 struct storage
*retval
;
1503 strcpy(opname
, opbits(expr
->op
== SPECIAL_INCREMENT
? "inc" : "dec",
1504 expr
->ctype
->bit_size
));
1507 struct storage
*new = stack_alloc(4);
1509 emit_copy(new, addr
, expr
->unop
->ctype
);
1515 insn(opname
, addr
, NULL
, NULL
);
1520 static struct storage
*emit_postop(struct expression
*expr
)
1522 return emit_inc_dec(expr
, 1);
1525 static struct storage
*emit_return_stmt(struct statement
*stmt
)
1527 struct function
*f
= current_func
;
1528 struct expression
*expr
= stmt
->ret_value
;
1529 struct storage
*val
= NULL
, *jmplbl
;
1531 if (expr
&& expr
->ctype
) {
1532 val
= x86_expression(expr
);
1533 assert(val
!= NULL
);
1534 emit_move(val
, REG_EAX
, expr
->ctype
, "return");
1537 jmplbl
= new_storage(STOR_LABEL
);
1538 jmplbl
->flags
|= STOR_WANTS_FREE
;
1539 jmplbl
->label
= f
->ret_target
;
1540 insn("jmp", jmplbl
, NULL
, NULL
);
1545 static struct storage
*emit_conditional_expr(struct expression
*expr
)
1547 struct storage
*cond
, *true = NULL
, *false = NULL
;
1548 struct storage
*new = stack_alloc(expr
->ctype
->bit_size
/ 8);
1549 int target_false
, cond_end
;
1551 /* evaluate conditional */
1552 cond
= x86_expression(expr
->conditional
);
1553 target_false
= emit_conditional_test(cond
);
1555 /* handle if-true part of the expression */
1556 true = x86_expression(expr
->cond_true
);
1558 emit_copy(new, true, expr
->ctype
);
1560 cond_end
= emit_conditional_end(target_false
);
1562 /* handle if-false part of the expression */
1563 false = x86_expression(expr
->cond_false
);
1565 emit_copy(new, false, expr
->ctype
);
1567 /* end of conditional; jump target for if-true branch */
1568 emit_label(cond_end
, "end conditional");
1573 static struct storage
*emit_select_expr(struct expression
*expr
)
1575 struct storage
*cond
= x86_expression(expr
->conditional
);
1576 struct storage
*true = x86_expression(expr
->cond_true
);
1577 struct storage
*false = x86_expression(expr
->cond_false
);
1578 struct storage
*reg_cond
, *reg_true
, *reg_false
;
1579 struct storage
*new = stack_alloc(4);
1581 emit_comment("begin SELECT");
1582 reg_cond
= get_reg_value(cond
, get_regclass(expr
->conditional
));
1583 reg_true
= get_reg_value(true, get_regclass(expr
));
1584 reg_false
= get_reg_value(false, get_regclass(expr
));
1587 * Do the actual select: check the conditional for zero,
1588 * move false over true if zero
1590 insn("test", reg_cond
, reg_cond
, NULL
);
1591 insn("cmovz", reg_false
, reg_true
, NULL
);
1594 emit_move(reg_true
, new, expr
->ctype
, NULL
);
1598 emit_comment("end SELECT");
1602 static struct storage
*emit_symbol_expr_init(struct symbol
*sym
)
1604 struct expression
*expr
= sym
->initializer
;
1605 struct symbol_private
*priv
= sym
->aux
;
1608 priv
= calloc(1, sizeof(*priv
));
1612 struct storage
*new = stack_alloc(4);
1613 fprintf(stderr
, "FIXME! no value for symbol %s. creating pseudo %d (stack offset %d)\n",
1614 show_ident(sym
->ident
),
1615 new->pseudo
, new->pseudo
* 4);
1618 priv
->addr
= x86_expression(expr
);
1625 static struct storage
*emit_string_expr(struct expression
*expr
)
1627 struct function
*f
= current_func
;
1628 int label
= new_label();
1629 struct storage
*new;
1631 push_cstring(f
, expr
->string
, label
);
1633 new = new_storage(STOR_LABEL
);
1635 new->flags
= STOR_LABEL_VAL
| STOR_WANTS_FREE
;
1639 static struct storage
*emit_cast_expr(struct expression
*expr
)
1641 struct symbol
*old_type
, *new_type
;
1642 struct storage
*op
= x86_expression(expr
->cast_expression
);
1643 int oldbits
, newbits
;
1644 struct storage
*new;
1646 old_type
= expr
->cast_expression
->ctype
;
1647 new_type
= expr
->cast_type
;
1649 oldbits
= old_type
->bit_size
;
1650 newbits
= new_type
->bit_size
;
1651 if (oldbits
>= newbits
)
1654 emit_move(op
, REG_EAX
, old_type
, "begin cast ..");
1656 new = stack_alloc(newbits
/ 8);
1657 emit_move(REG_EAX
, new, new_type
, ".... end cast");
1662 static struct storage
*emit_regular_preop(struct expression
*expr
)
1664 struct storage
*target
= x86_expression(expr
->unop
);
1665 struct storage
*val
, *new = stack_alloc(4);
1666 const char *opname
= NULL
;
1670 val
= new_storage(STOR_VALUE
);
1671 val
->flags
= STOR_WANTS_FREE
;
1672 emit_move(val
, REG_EDX
, NULL
, NULL
);
1673 emit_move(target
, REG_EAX
, expr
->unop
->ctype
, NULL
);
1674 insn("test", REG_EAX
, REG_EAX
, NULL
);
1675 insn("setz", REG_DL
, NULL
, NULL
);
1676 emit_move(REG_EDX
, new, expr
->unop
->ctype
, NULL
);
1684 emit_move(target
, REG_EAX
, expr
->unop
->ctype
, NULL
);
1685 insn(opname
, REG_EAX
, NULL
, NULL
);
1686 emit_move(REG_EAX
, new, expr
->unop
->ctype
, NULL
);
1696 static void emit_case_statement(struct statement
*stmt
)
1698 emit_labelsym(stmt
->case_label
, NULL
);
1699 x86_statement(stmt
->case_statement
);
1702 static void emit_switch_statement(struct statement
*stmt
)
1704 struct storage
*val
= x86_expression(stmt
->switch_expression
);
1705 struct symbol
*sym
, *default_sym
= NULL
;
1706 struct storage
*labelsym
, *label
;
1709 emit_move(val
, REG_EAX
, stmt
->switch_expression
->ctype
, "begin case");
1712 * This is where a _real_ back-end would go through the
1713 * cases to decide whether to use a lookup table or a
1714 * series of comparisons etc
1716 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
1717 struct statement
*case_stmt
= sym
->stmt
;
1718 struct expression
*expr
= case_stmt
->case_expression
;
1719 struct expression
*to
= case_stmt
->case_to
;
1727 struct storage
*case_val
= new_val(expr
->value
);
1729 assert (expr
->type
== EXPR_VALUE
);
1731 insn("cmpl", case_val
, REG_EAX
, NULL
);
1734 labelsym
= new_labelsym(sym
);
1735 insn("je", labelsym
, NULL
, NULL
);
1739 label
= new_storage(STOR_LABEL
);
1740 label
->flags
|= STOR_WANTS_FREE
;
1741 label
->label
= next_test
= new_label();
1743 /* FIXME: signed/unsigned */
1744 insn("jl", label
, NULL
, NULL
);
1746 case_val
= new_val(to
->value
);
1747 insn("cmpl", case_val
, REG_EAX
, NULL
);
1749 /* TODO: implement and use refcounting... */
1750 label
= new_storage(STOR_LABEL
);
1751 label
->flags
|= STOR_WANTS_FREE
;
1752 label
->label
= next_test
;
1754 /* FIXME: signed/unsigned */
1755 insn("jg", label
, NULL
, NULL
);
1757 labelsym
= new_labelsym(sym
);
1758 insn("jmp", labelsym
, NULL
, NULL
);
1760 emit_label(next_test
, NULL
);
1763 } END_FOR_EACH_PTR(sym
);
1766 labelsym
= new_labelsym(default_sym
);
1767 insn("jmp", labelsym
, NULL
, "default");
1769 label
= new_storage(STOR_LABEL
);
1770 label
->flags
|= STOR_WANTS_FREE
;
1771 label
->label
= switch_end
= new_label();
1772 insn("jmp", label
, NULL
, "goto end of switch");
1775 x86_statement(stmt
->switch_statement
);
1777 if (stmt
->switch_break
->used
)
1778 emit_labelsym(stmt
->switch_break
, NULL
);
1781 emit_label(switch_end
, NULL
);
1784 static void x86_struct_member(struct symbol
*sym
)
1786 printf("\t%s:%d:%ld at offset %ld.%d", show_ident(sym
->ident
), sym
->bit_size
, sym
->ctype
.alignment
, sym
->offset
, sym
->bit_offset
);
1790 static void x86_symbol(struct symbol
*sym
)
1792 struct symbol
*type
;
1797 type
= sym
->ctype
.base_type
;
1802 * Show actual implementation information
1804 switch (type
->type
) {
1807 if (sym
->initializer
)
1810 emit_array_noinit(sym
);
1814 if (sym
->initializer
) {
1815 emit_object_pre(show_ident(sym
->ident
),
1816 sym
->ctype
.modifiers
,
1817 sym
->ctype
.alignment
,
1819 emit_scalar(sym
->initializer
, sym
->bit_size
);
1822 emit_scalar_noinit(sym
);
1827 struct symbol
*member
;
1830 FOR_EACH_PTR(type
->symbol_list
, member
) {
1831 x86_struct_member(member
);
1832 } END_FOR_EACH_PTR(member
);
1838 struct statement
*stmt
= type
->stmt
;
1841 x86_statement(stmt
);
1842 emit_func_post(sym
);
1851 if (sym
->initializer
&& (type
->type
!= SYM_BASETYPE
) &&
1852 (type
->type
!= SYM_ARRAY
)) {
1854 x86_expression(sym
->initializer
);
1858 static void x86_symbol_init(struct symbol
*sym
);
1860 static void x86_symbol_decl(struct symbol_list
*syms
)
1863 FOR_EACH_PTR(syms
, sym
) {
1864 x86_symbol_init(sym
);
1865 } END_FOR_EACH_PTR(sym
);
1868 static void loopstk_push(int cont_lbl
, int loop_bottom_lbl
)
1870 struct function
*f
= current_func
;
1871 struct loop_stack
*ls
;
1873 ls
= malloc(sizeof(*ls
));
1874 ls
->continue_lbl
= cont_lbl
;
1875 ls
->loop_bottom_lbl
= loop_bottom_lbl
;
1876 ls
->next
= f
->loop_stack
;
1880 static void loopstk_pop(void)
1882 struct function
*f
= current_func
;
1883 struct loop_stack
*ls
;
1885 assert(f
->loop_stack
!= NULL
);
1887 f
->loop_stack
= f
->loop_stack
->next
;
1891 static int loopstk_break(void)
1893 return current_func
->loop_stack
->loop_bottom_lbl
;
1896 static int loopstk_continue(void)
1898 return current_func
->loop_stack
->continue_lbl
;
1901 static void emit_loop(struct statement
*stmt
)
1903 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
1904 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
1905 struct statement
*statement
= stmt
->iterator_statement
;
1906 struct statement
*post_statement
= stmt
->iterator_post_statement
;
1907 struct expression
*post_condition
= stmt
->iterator_post_condition
;
1908 int loop_top
= 0, loop_bottom
, loop_continue
;
1909 int have_bottom
= 0;
1910 struct storage
*val
;
1912 loop_bottom
= new_label();
1913 loop_continue
= new_label();
1914 loopstk_push(loop_continue
, loop_bottom
);
1916 x86_symbol_decl(stmt
->iterator_syms
);
1917 x86_statement(pre_statement
);
1918 if (pre_condition
) {
1919 if (pre_condition
->type
== EXPR_VALUE
) {
1920 if (!pre_condition
->value
) {
1921 struct storage
*lbv
;
1922 lbv
= new_storage(STOR_LABEL
);
1923 lbv
->label
= loop_bottom
;
1924 lbv
->flags
= STOR_WANTS_FREE
;
1925 insn("jmp", lbv
, NULL
, "go to loop bottom");
1929 struct storage
*lbv
= new_storage(STOR_LABEL
);
1930 lbv
->label
= loop_bottom
;
1931 lbv
->flags
= STOR_WANTS_FREE
;
1934 val
= x86_expression(pre_condition
);
1936 emit_move(val
, REG_EAX
, NULL
, "loop pre condition");
1937 insn("test", REG_EAX
, REG_EAX
, NULL
);
1938 insn("jz", lbv
, NULL
, NULL
);
1941 if (!post_condition
|| post_condition
->type
!= EXPR_VALUE
|| post_condition
->value
) {
1942 loop_top
= new_label();
1943 emit_label(loop_top
, "loop top");
1945 x86_statement(statement
);
1946 if (stmt
->iterator_continue
->used
)
1947 emit_label(loop_continue
, "'continue' iterator");
1948 x86_statement(post_statement
);
1949 if (!post_condition
) {
1950 struct storage
*lbv
= new_storage(STOR_LABEL
);
1951 lbv
->label
= loop_top
;
1952 lbv
->flags
= STOR_WANTS_FREE
;
1953 insn("jmp", lbv
, NULL
, "go to loop top");
1954 } else if (post_condition
->type
== EXPR_VALUE
) {
1955 if (post_condition
->value
) {
1956 struct storage
*lbv
= new_storage(STOR_LABEL
);
1957 lbv
->label
= loop_top
;
1958 lbv
->flags
= STOR_WANTS_FREE
;
1959 insn("jmp", lbv
, NULL
, "go to loop top");
1962 struct storage
*lbv
= new_storage(STOR_LABEL
);
1963 lbv
->label
= loop_top
;
1964 lbv
->flags
= STOR_WANTS_FREE
;
1966 val
= x86_expression(post_condition
);
1968 emit_move(val
, REG_EAX
, NULL
, "loop post condition");
1969 insn("test", REG_EAX
, REG_EAX
, NULL
);
1970 insn("jnz", lbv
, NULL
, NULL
);
1972 if (have_bottom
|| stmt
->iterator_break
->used
)
1973 emit_label(loop_bottom
, "loop bottom");
1979 * Print out a statement
1981 static struct storage
*x86_statement(struct statement
*stmt
)
1985 switch (stmt
->type
) {
1989 return emit_return_stmt(stmt
);
1990 case STMT_COMPOUND
: {
1991 struct statement
*s
;
1992 struct storage
*last
= NULL
;
1994 x86_symbol_decl(stmt
->syms
);
1995 FOR_EACH_PTR(stmt
->stmts
, s
) {
1996 last
= x86_statement(s
);
1997 } END_FOR_EACH_PTR(s
);
2002 case STMT_EXPRESSION
:
2003 return x86_expression(stmt
->expression
);
2005 emit_if_conditional(stmt
);
2009 emit_case_statement(stmt
);
2012 emit_switch_statement(stmt
);
2023 printf(".L%p:\n", stmt
->label_identifier
);
2024 x86_statement(stmt
->label_statement
);
2028 if (stmt
->goto_expression
) {
2029 struct storage
*val
= x86_expression(stmt
->goto_expression
);
2030 printf("\tgoto *v%d\n", val
->pseudo
);
2031 } else if (!strcmp("break", show_ident(stmt
->goto_label
->ident
))) {
2032 struct storage
*lbv
= new_storage(STOR_LABEL
);
2033 lbv
->label
= loopstk_break();
2034 lbv
->flags
= STOR_WANTS_FREE
;
2035 insn("jmp", lbv
, NULL
, "'break'; go to loop bottom");
2036 } else if (!strcmp("continue", show_ident(stmt
->goto_label
->ident
))) {
2037 struct storage
*lbv
= new_storage(STOR_LABEL
);
2038 lbv
->label
= loopstk_continue();
2039 lbv
->flags
= STOR_WANTS_FREE
;
2040 insn("jmp", lbv
, NULL
, "'continue'; go to loop top");
2042 struct storage
*labelsym
= new_labelsym(stmt
->goto_label
);
2043 insn("jmp", labelsym
, NULL
, NULL
);
2047 printf("\tasm( .... )\n");
2053 static struct storage
*x86_call_expression(struct expression
*expr
)
2055 struct function
*f
= current_func
;
2056 struct symbol
*direct
;
2057 struct expression
*arg
, *fn
;
2058 struct storage
*retval
, *fncall
;
2063 warning(expr
->pos
, "\tcall with no type!");
2068 FOR_EACH_PTR_REVERSE(expr
->args
, arg
) {
2069 struct storage
*new = x86_expression(arg
);
2070 int size
= arg
->ctype
->bit_size
;
2073 * FIXME: i386 SysV ABI dictates that values
2074 * smaller than 32 bits should be placed onto
2075 * the stack as 32-bit objects. We should not
2076 * blindly do a 32-bit push on objects smaller
2081 insn("pushl", new, NULL
,
2082 !framesize
? "begin function call" : NULL
);
2084 framesize
+= size
>> 3;
2085 } END_FOR_EACH_PTR_REVERSE(arg
);
2089 /* Remove dereference, if any */
2091 if (fn
->type
== EXPR_PREOP
) {
2092 if (fn
->unop
->type
== EXPR_SYMBOL
) {
2093 struct symbol
*sym
= fn
->unop
->symbol
;
2094 if (sym
->ctype
.base_type
->type
== SYM_FN
)
2099 struct storage
*direct_stor
= new_storage(STOR_SYM
);
2100 direct_stor
->flags
|= STOR_WANTS_FREE
;
2101 direct_stor
->sym
= direct
;
2102 insn("call", direct_stor
, NULL
, NULL
);
2104 fncall
= x86_expression(fn
);
2105 emit_move(fncall
, REG_EAX
, fn
->ctype
, NULL
);
2107 strcpy(s
, "\tcall\t*%eax\n");
2108 push_text_atom(f
, s
);
2111 /* FIXME: pay attention to BITS_IN_POINTER */
2113 struct storage
*val
= new_storage(STOR_VALUE
);
2114 val
->value
= (long long) framesize
;
2115 val
->flags
= STOR_WANTS_FREE
;
2116 insn("addl", val
, REG_ESP
, NULL
);
2119 retval
= stack_alloc(4);
2120 emit_move(REG_EAX
, retval
, NULL
, "end function call");
2125 static struct storage
*x86_address_gen(struct expression
*expr
)
2127 struct function
*f
= current_func
;
2128 struct storage
*addr
;
2129 struct storage
*new;
2132 addr
= x86_expression(expr
->unop
);
2133 if (expr
->unop
->type
== EXPR_SYMBOL
)
2136 emit_move(addr
, REG_EAX
, NULL
, "begin deref ..");
2138 /* FIXME: operand size */
2139 strcpy(s
, "\tmovl\t(%eax), %ecx\n");
2140 push_text_atom(f
, s
);
2142 new = stack_alloc(4);
2143 emit_move(REG_ECX
, new, NULL
, ".... end deref");
2148 static struct storage
*x86_assignment(struct expression
*expr
)
2150 struct expression
*target
= expr
->left
;
2151 struct storage
*val
, *addr
;
2156 val
= x86_expression(expr
->right
);
2157 addr
= x86_address_gen(target
);
2159 switch (val
->type
) {
2160 /* copy, where both operands are memory */
2163 emit_copy(addr
, val
, expr
->ctype
);
2166 /* copy, one or zero operands are memory */
2171 emit_move(val
, addr
, expr
->left
->ctype
, NULL
);
2181 static int x86_initialization(struct symbol
*sym
, struct expression
*expr
)
2183 struct storage
*val
, *addr
;
2189 bits
= expr
->ctype
->bit_size
;
2190 val
= x86_expression(expr
);
2191 addr
= x86_symbol_expr(sym
);
2192 // FIXME! The "target" expression is for bitfield store information.
2193 // Leave it NULL, which works fine.
2194 emit_store(NULL
, addr
, val
, bits
);
2198 static struct storage
*x86_access(struct expression
*expr
)
2200 return x86_address_gen(expr
);
2203 static struct storage
*x86_preop(struct expression
*expr
)
2206 * '*' is an lvalue access, and is fundamentally different
2207 * from an arithmetic operation. Maybe it should have an
2208 * expression type of its own..
2210 if (expr
->op
== '*')
2211 return x86_access(expr
);
2212 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
2213 return emit_inc_dec(expr
, 0);
2214 return emit_regular_preop(expr
);
2217 static struct storage
*x86_symbol_expr(struct symbol
*sym
)
2219 struct storage
*new = stack_alloc(4);
2221 if (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_EXTERN
| MOD_STATIC
)) {
2222 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer
, new->pseudo
, show_ident(sym
->ident
));
2225 if (sym
->ctype
.modifiers
& MOD_ADDRESSABLE
) {
2226 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer
, new->pseudo
, sym
->value
);
2229 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer
, new->pseudo
, show_ident(sym
->ident
), sym
);
2233 static void x86_symbol_init(struct symbol
*sym
)
2235 struct symbol_private
*priv
= sym
->aux
;
2236 struct expression
*expr
= sym
->initializer
;
2237 struct storage
*new;
2240 new = x86_expression(expr
);
2242 new = stack_alloc(sym
->bit_size
/ 8);
2245 priv
= calloc(1, sizeof(*priv
));
2247 /* FIXME: leak! we don't free... */
2248 /* (well, we don't free symbols either) */
2254 static int type_is_signed(struct symbol
*sym
)
2256 if (sym
->type
== SYM_NODE
)
2257 sym
= sym
->ctype
.base_type
;
2258 if (sym
->type
== SYM_PTR
)
2260 return !(sym
->ctype
.modifiers
& MOD_UNSIGNED
);
2263 static struct storage
*x86_label_expr(struct expression
*expr
)
2265 struct storage
*new = stack_alloc(4);
2266 printf("\tmovi.%d\t\tv%d,.L%p\n", bits_in_pointer
, new->pseudo
, expr
->label_symbol
);
2270 static struct storage
*x86_statement_expr(struct expression
*expr
)
2272 return x86_statement(expr
->statement
);
2275 static int x86_position_expr(struct expression
*expr
, struct symbol
*base
)
2277 struct storage
*new = x86_expression(expr
->init_expr
);
2278 struct symbol
*ctype
= expr
->init_expr
->ctype
;
2280 printf("\tinsert v%d at [%d:%d] of %s\n", new->pseudo
,
2281 expr
->init_offset
, ctype
->bit_offset
,
2282 show_ident(base
->ident
));
2286 static void x86_initializer_expr(struct expression
*expr
, struct symbol
*ctype
)
2288 struct expression
*entry
;
2290 FOR_EACH_PTR(expr
->expr_list
, entry
) {
2291 // Nested initializers have their positions already
2292 // recursively calculated - just output them too
2293 if (entry
->type
== EXPR_INITIALIZER
) {
2294 x86_initializer_expr(entry
, ctype
);
2298 // Ignore initializer indexes and identifiers - the
2299 // evaluator has taken them into account
2300 if (entry
->type
== EXPR_IDENTIFIER
|| entry
->type
== EXPR_INDEX
)
2302 if (entry
->type
== EXPR_POS
) {
2303 x86_position_expr(entry
, ctype
);
2306 x86_initialization(ctype
, entry
);
2307 } END_FOR_EACH_PTR(entry
);
2311 * Print out an expression. Return the pseudo that contains the
2314 static struct storage
*x86_expression(struct expression
*expr
)
2320 struct position
*pos
= &expr
->pos
;
2321 printf("\tno type at %s:%d:%d\n",
2322 stream_name(pos
->stream
),
2323 pos
->line
, pos
->pos
);
2327 switch (expr
->type
) {
2331 return x86_call_expression(expr
);
2333 case EXPR_ASSIGNMENT
:
2334 return x86_assignment(expr
);
2337 return emit_compare(expr
);
2341 return emit_binop(expr
);
2343 return x86_preop(expr
);
2345 return emit_postop(expr
);
2347 return emit_symbol_expr_init(expr
->symbol
);
2351 warning(expr
->pos
, "invalid expression after evaluation");
2354 case EXPR_IMPLIED_CAST
:
2355 return emit_cast_expr(expr
);
2357 return emit_value(expr
);
2359 return emit_string_expr(expr
);
2360 case EXPR_INITIALIZER
:
2361 x86_initializer_expr(expr
, expr
->ctype
);
2364 return emit_select_expr(expr
);
2365 case EXPR_CONDITIONAL
:
2366 return emit_conditional_expr(expr
);
2367 case EXPR_STATEMENT
:
2368 return x86_statement_expr(expr
);
2370 return x86_label_expr(expr
);
2372 // None of these should exist as direct expressions: they are only
2373 // valid as sub-expressions of initializers.
2375 warning(expr
->pos
, "unable to show plain initializer position expression");
2377 case EXPR_IDENTIFIER
:
2378 warning(expr
->pos
, "unable to show identifier expression");
2381 warning(expr
->pos
, "unable to show index expression");
2384 warning(expr
->pos
, "unable to show type expression");
2387 warning(expr
->pos
, "floating point support is not implemented");