2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief This file implements the ia32 node emitter.
23 * @author Christian Wuerdig, Matthias Braun
41 #include "iredges_t.h"
44 #include "raw_bitset.h"
47 #include "../besched_t.h"
48 #include "../benode_t.h"
50 #include "../be_dbgout.h"
51 #include "../beemitter.h"
52 #include "../begnuas.h"
53 #include "../beirg_t.h"
54 #include "../be_dbgout.h"
56 #include "ia32_emitter.h"
57 #include "gen_ia32_emitter.h"
58 #include "gen_ia32_regalloc_if.h"
59 #include "ia32_nodes_attr.h"
60 #include "ia32_new_nodes.h"
61 #include "ia32_map_regs.h"
62 #include "ia32_architecture.h"
63 #include "bearch_ia32_t.h"
65 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
67 #define BLOCK_PREFIX ".L"
69 #define SNPRINTF_BUF_LEN 128
71 static const arch_env_t *arch_env;
72 static const ia32_isa_t *isa;
73 static ia32_code_gen_t *cg;
75 static char pic_base_label[128];
76 static ir_label_t exc_label_id;
77 static int mark_spill_reload = 0;
79 /** Return the next block in Block schedule */
80 static ir_node *get_prev_block_sched(const ir_node *block)
82 return get_irn_link(block);
85 static int is_fallthrough(const ir_node *cfgpred)
89 if (!is_Proj(cfgpred))
91 pred = get_Proj_pred(cfgpred);
92 if (is_ia32_SwitchJmp(pred))
98 static int block_needs_label(const ir_node *block)
101 int n_cfgpreds = get_Block_n_cfgpreds(block);
103 if (n_cfgpreds == 0) {
105 } else if (n_cfgpreds == 1) {
106 ir_node *cfgpred = get_Block_cfgpred(block, 0);
107 ir_node *cfgpred_block = get_nodes_block(cfgpred);
109 if (get_prev_block_sched(block) == cfgpred_block
110 && is_fallthrough(cfgpred)) {
119 * Returns the register at in position pos.
121 static const arch_register_t *get_in_reg(const ir_node *irn, int pos)
124 const arch_register_t *reg = NULL;
126 assert(get_irn_arity(irn) > pos && "Invalid IN position");
128 /* The out register of the operator at position pos is the
129 in register we need. */
130 op = get_irn_n(irn, pos);
132 reg = arch_get_irn_register(op);
134 assert(reg && "no in register found");
136 if (reg == &ia32_gp_regs[REG_GP_NOREG])
137 panic("trying to emit noreg for %+F input %d", irn, pos);
139 /* in case of unknown register: just return a valid register */
140 if (reg == &ia32_gp_regs[REG_GP_UKNWN]) {
141 const arch_register_req_t *req = arch_get_register_req(irn, pos);
143 if (arch_register_req_is(req, limited)) {
144 /* in case of limited requirements: get the first allowed register */
145 unsigned idx = rbitset_next(req->limited, 0, 1);
146 reg = arch_register_for_index(req->cls, idx);
148 /* otherwise get first register in class */
149 reg = arch_register_for_index(req->cls, 0);
157 * Returns the register at out position pos.
159 static const arch_register_t *get_out_reg(const ir_node *irn, int pos)
162 const arch_register_t *reg = NULL;
164 /* 1st case: irn is not of mode_T, so it has only */
165 /* one OUT register -> good */
166 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
167 /* Proj with the corresponding projnum for the register */
169 if (get_irn_mode(irn) != mode_T) {
171 reg = arch_get_irn_register(irn);
172 } else if (is_ia32_irn(irn)) {
173 reg = get_ia32_out_reg(irn, pos);
175 const ir_edge_t *edge;
177 foreach_out_edge(irn, edge) {
178 proj = get_edge_src_irn(edge);
179 assert(is_Proj(proj) && "non-Proj from mode_T node");
180 if (get_Proj_proj(proj) == pos) {
181 reg = arch_get_irn_register(proj);
187 assert(reg && "no out register found");
192 * Add a number to a prefix. This number will not be used a second time.
194 static char *get_unique_label(char *buf, size_t buflen, const char *prefix)
196 static unsigned long id = 0;
197 snprintf(buf, buflen, "%s%lu", prefix, ++id);
201 /*************************************************************
203 * (_) | | / _| | | | |
204 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
205 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
206 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
207 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
210 *************************************************************/
212 static void emit_8bit_register(const arch_register_t *reg)
214 const char *reg_name = arch_register_get_name(reg);
217 be_emit_char(reg_name[1]);
221 static void emit_16bit_register(const arch_register_t *reg)
223 const char *reg_name = ia32_get_mapped_reg_name(isa->regs_16bit, reg);
226 be_emit_string(reg_name);
229 static void emit_register(const arch_register_t *reg, const ir_mode *mode)
231 const char *reg_name;
234 int size = get_mode_size_bits(mode);
236 case 8: emit_8bit_register(reg); return;
237 case 16: emit_16bit_register(reg); return;
239 assert(mode_is_float(mode) || size == 32);
242 reg_name = arch_register_get_name(reg);
245 be_emit_string(reg_name);
248 void ia32_emit_source_register(const ir_node *node, int pos)
250 const arch_register_t *reg = get_in_reg(node, pos);
252 emit_register(reg, NULL);
255 static void ia32_emit_entity(ir_entity *entity, int no_pic_adjust)
259 set_entity_backend_marked(entity, 1);
260 id = get_entity_ld_ident(entity);
263 if (get_entity_owner(entity) == get_tls_type()) {
264 if (get_entity_visibility(entity) == visibility_external_allocated) {
265 be_emit_cstring("@INDNTPOFF");
267 be_emit_cstring("@NTPOFF");
271 if (!no_pic_adjust && do_pic) {
272 /* TODO: only do this when necessary */
274 be_emit_string(pic_base_label);
278 static void emit_ia32_Immediate_no_prefix(const ir_node *node)
280 const ia32_immediate_attr_t *attr = get_ia32_immediate_attr_const(node);
282 if (attr->symconst != NULL) {
285 ia32_emit_entity(attr->symconst, 0);
287 if (attr->symconst == NULL || attr->offset != 0) {
288 if (attr->symconst != NULL) {
289 be_emit_irprintf("%+d", attr->offset);
291 be_emit_irprintf("0x%X", attr->offset);
296 static void emit_ia32_Immediate(const ir_node *node)
299 emit_ia32_Immediate_no_prefix(node);
302 void ia32_emit_8bit_source_register_or_immediate(const ir_node *node, int pos)
304 const arch_register_t *reg;
305 ir_node *in = get_irn_n(node, pos);
306 if (is_ia32_Immediate(in)) {
307 emit_ia32_Immediate(in);
311 reg = get_in_reg(node, pos);
312 emit_8bit_register(reg);
315 void ia32_emit_dest_register(const ir_node *node, int pos)
317 const arch_register_t *reg = get_out_reg(node, pos);
319 emit_register(reg, NULL);
322 void ia32_emit_8bit_dest_register(const ir_node *node, int pos)
324 const arch_register_t *reg = get_out_reg(node, pos);
326 emit_register(reg, mode_Bu);
329 void ia32_emit_x87_register(const ir_node *node, int pos)
331 const ia32_x87_attr_t *attr = get_ia32_x87_attr_const(node);
335 be_emit_string(attr->x87[pos]->name);
338 static void ia32_emit_mode_suffix_mode(const ir_mode *mode)
340 assert(mode_is_int(mode) || mode_is_reference(mode));
341 switch (get_mode_size_bits(mode)) {
342 case 8: be_emit_char('b'); return;
343 case 16: be_emit_char('w'); return;
344 case 32: be_emit_char('l'); return;
345 /* gas docu says q is the suffix but gcc, objdump and icc use ll
347 case 64: be_emit_cstring("ll"); return;
349 panic("Can't output mode_suffix for %+F", mode);
352 void ia32_emit_mode_suffix(const ir_node *node)
354 ir_mode *mode = get_ia32_ls_mode(node);
358 ia32_emit_mode_suffix_mode(mode);
361 void ia32_emit_x87_mode_suffix(const ir_node *node)
365 /* we only need to emit the mode on address mode */
366 if (get_ia32_op_type(node) == ia32_Normal)
369 mode = get_ia32_ls_mode(node);
370 assert(mode != NULL);
372 if (mode_is_float(mode)) {
373 switch (get_mode_size_bits(mode)) {
374 case 32: be_emit_char('s'); return;
375 case 64: be_emit_char('l'); return;
377 case 96: be_emit_char('t'); return;
380 assert(mode_is_int(mode));
381 switch (get_mode_size_bits(mode)) {
382 case 16: be_emit_char('s'); return;
383 case 32: be_emit_char('l'); return;
384 /* gas docu says q is the suffix but gcc, objdump and icc use ll
386 case 64: be_emit_cstring("ll"); return;
389 panic("Can't output mode_suffix for %+F", mode);
392 static char get_xmm_mode_suffix(ir_mode *mode)
394 assert(mode_is_float(mode));
395 switch(get_mode_size_bits(mode)) {
398 default: panic("Invalid XMM mode");
402 void ia32_emit_xmm_mode_suffix(const ir_node *node)
404 ir_mode *mode = get_ia32_ls_mode(node);
405 assert(mode != NULL);
407 be_emit_char(get_xmm_mode_suffix(mode));
410 void ia32_emit_xmm_mode_suffix_s(const ir_node *node)
412 ir_mode *mode = get_ia32_ls_mode(node);
413 assert(mode != NULL);
414 be_emit_char(get_xmm_mode_suffix(mode));
417 void ia32_emit_extend_suffix(const ir_mode *mode)
419 if (get_mode_size_bits(mode) == 32)
421 be_emit_char(mode_is_signed(mode) ? 's' : 'z');
424 void ia32_emit_source_register_or_immediate(const ir_node *node, int pos)
426 ir_node *in = get_irn_n(node, pos);
427 if (is_ia32_Immediate(in)) {
428 emit_ia32_Immediate(in);
430 const ir_mode *mode = get_ia32_ls_mode(node);
431 const arch_register_t *reg = get_in_reg(node, pos);
432 emit_register(reg, mode);
437 * Returns the target block for a control flow node.
439 static ir_node *get_cfop_target_block(const ir_node *irn)
441 assert(get_irn_mode(irn) == mode_X);
442 return get_irn_link(irn);
446 * Emits a block label for the given block.
448 static void ia32_emit_block_name(const ir_node *block)
450 if (has_Block_label(block)) {
451 be_emit_string(be_gas_block_label_prefix());
452 be_emit_irprintf("%lu", get_Block_label(block));
454 be_emit_cstring(BLOCK_PREFIX);
455 be_emit_irprintf("%ld", get_irn_node_nr(block));
460 * Emits the target label for a control flow node.
462 static void ia32_emit_cfop_target(const ir_node *node)
464 ir_node *block = get_cfop_target_block(node);
465 ia32_emit_block_name(block);
469 * coding of conditions
471 struct cmp2conditon_t {
477 * positive conditions for signed compares
479 static const struct cmp2conditon_t cmp2condition_s[] = {
480 { NULL, pn_Cmp_False }, /* always false */
481 { "e", pn_Cmp_Eq }, /* == */
482 { "l", pn_Cmp_Lt }, /* < */
483 { "le", pn_Cmp_Le }, /* <= */
484 { "g", pn_Cmp_Gt }, /* > */
485 { "ge", pn_Cmp_Ge }, /* >= */
486 { "ne", pn_Cmp_Lg }, /* != */
487 { NULL, pn_Cmp_Leg}, /* always true */
491 * positive conditions for unsigned compares
493 static const struct cmp2conditon_t cmp2condition_u[] = {
494 { NULL, pn_Cmp_False }, /* always false */
495 { "e", pn_Cmp_Eq }, /* == */
496 { "b", pn_Cmp_Lt }, /* < */
497 { "be", pn_Cmp_Le }, /* <= */
498 { "a", pn_Cmp_Gt }, /* > */
499 { "ae", pn_Cmp_Ge }, /* >= */
500 { "ne", pn_Cmp_Lg }, /* != */
501 { NULL, pn_Cmp_Leg }, /* always true */
504 static void ia32_emit_cmp_suffix(int pnc)
508 if ((pnc & ia32_pn_Cmp_float) || (pnc & ia32_pn_Cmp_unsigned)) {
510 assert(cmp2condition_u[pnc].num == pnc);
511 str = cmp2condition_u[pnc].name;
514 assert(cmp2condition_s[pnc].num == pnc);
515 str = cmp2condition_s[pnc].name;
521 typedef enum ia32_emit_mod_t {
522 EMIT_RESPECT_LS = 1U << 0,
523 EMIT_ALTERNATE_AM = 1U << 1
527 * fmt parameter output
528 * ---- ---------------------- ---------------------------------------------
530 * %AM <node> address mode of the node
531 * %AR const arch_register_t* address mode of the node or register
532 * %ASx <node> address mode of the node or source register x
533 * %Dx <node> destination register x
534 * %I <node> immediate of the node
535 * %L <node> control flow target of the node
536 * %M <node> mode suffix of the node
537 * %P int condition code
538 * %R const arch_register_t* register
539 * %Sx <node> source register x
540 * %s const char* string
541 * %u unsigned int unsigned int
544 * # modifier for %ASx, %D and %S uses ls mode of node to alter register width
545 * * modifier does not prefix immediates with $, but AM with *
547 static void ia32_emitf(const ir_node *node, const char *fmt, ...)
553 const char *start = fmt;
554 ia32_emit_mod_t mod = 0;
556 while (*fmt != '%' && *fmt != '\n' && *fmt != '\0')
559 be_emit_string_len(start, fmt - start);
563 be_emit_finish_line_gas(node);
575 mod |= EMIT_ALTERNATE_AM;
580 mod |= EMIT_RESPECT_LS;
592 if (mod & EMIT_ALTERNATE_AM)
598 const arch_register_t *reg = va_arg(ap, const arch_register_t*);
599 if (mod & EMIT_ALTERNATE_AM)
601 if (get_ia32_op_type(node) == ia32_AddrModeS) {
604 emit_register(reg, NULL);
610 if (get_ia32_op_type(node) == ia32_AddrModeS) {
611 if (mod & EMIT_ALTERNATE_AM)
616 assert(get_ia32_op_type(node) == ia32_Normal);
621 default: goto unknown;
628 const arch_register_t *reg;
630 if (*fmt < '0' || '9' <= *fmt)
634 reg = get_out_reg(node, pos);
635 emit_register(reg, mod & EMIT_RESPECT_LS ? get_ia32_ls_mode(node) : NULL);
640 if (!(mod & EMIT_ALTERNATE_AM))
642 emit_ia32_Immediate_no_prefix(node);
646 ia32_emit_cfop_target(node);
650 ia32_emit_mode_suffix_mode(get_ia32_ls_mode(node));
655 int pnc = va_arg(ap, int);
656 ia32_emit_cmp_suffix(pnc);
661 const arch_register_t *reg = va_arg(ap, const arch_register_t*);
662 emit_register(reg, NULL);
671 if (*fmt < '0' || '9' <= *fmt)
675 in = get_irn_n(node, pos);
676 if (is_ia32_Immediate(in)) {
677 if (!(mod & EMIT_ALTERNATE_AM))
679 emit_ia32_Immediate_no_prefix(in);
681 const arch_register_t *reg;
683 if (mod & EMIT_ALTERNATE_AM)
685 reg = get_in_reg(node, pos);
686 emit_register(reg, mod & EMIT_RESPECT_LS ? get_ia32_ls_mode(node) : NULL);
692 const char *str = va_arg(ap, const char*);
698 unsigned num = va_arg(ap, unsigned);
699 be_emit_irprintf("%u", num);
705 panic("unknown conversion");
713 * Emits registers and/or address mode of a binary operation.
715 void ia32_emit_binop(const ir_node *node)
717 if (is_ia32_Immediate(get_irn_n(node, n_ia32_binary_right))) {
718 ia32_emitf(node, "%#S4, %#AS3");
720 ia32_emitf(node, "%#AS4, %#S3");
725 * Emits registers and/or address mode of a binary operation.
727 void ia32_emit_x87_binop(const ir_node *node)
729 switch(get_ia32_op_type(node)) {
732 const ia32_x87_attr_t *x87_attr = get_ia32_x87_attr_const(node);
733 const arch_register_t *in1 = x87_attr->x87[0];
734 const arch_register_t *in = x87_attr->x87[1];
735 const arch_register_t *out = x87_attr->x87[2];
739 } else if (out == in) {
744 be_emit_string(arch_register_get_name(in));
745 be_emit_cstring(", %");
746 be_emit_string(arch_register_get_name(out));
754 assert(0 && "unsupported op type");
759 * Emits registers and/or address mode of a unary operation.
761 void ia32_emit_unop(const ir_node *node, int pos)
765 ia32_emitf(node, fmt);
769 * Emits address mode.
771 void ia32_emit_am(const ir_node *node)
773 ir_entity *ent = get_ia32_am_sc(node);
774 int offs = get_ia32_am_offs_int(node);
775 ir_node *base = get_irn_n(node, n_ia32_base);
776 int has_base = !is_ia32_NoReg_GP(base);
777 ir_node *index = get_irn_n(node, n_ia32_index);
778 int has_index = !is_ia32_NoReg_GP(index);
780 /* just to be sure... */
781 assert(!is_ia32_use_frame(node) || get_ia32_frame_ent(node) != NULL);
785 if (is_ia32_am_sc_sign(node))
787 ia32_emit_entity(ent, 0);
790 /* also handle special case if nothing is set */
791 if (offs != 0 || (ent == NULL && !has_base && !has_index)) {
793 be_emit_irprintf("%+d", offs);
795 be_emit_irprintf("%d", offs);
799 if (has_base || has_index) {
804 const arch_register_t *reg = get_in_reg(node, n_ia32_base);
805 emit_register(reg, NULL);
808 /* emit index + scale */
810 const arch_register_t *reg = get_in_reg(node, n_ia32_index);
813 emit_register(reg, NULL);
815 scale = get_ia32_am_scale(node);
817 be_emit_irprintf(",%d", 1 << scale);
824 static void emit_ia32_IMul(const ir_node *node)
826 ir_node *left = get_irn_n(node, n_ia32_IMul_left);
827 const arch_register_t *out_reg = get_out_reg(node, pn_ia32_IMul_res);
829 /* do we need the 3-address form? */
830 if (is_ia32_NoReg_GP(left) ||
831 get_in_reg(node, n_ia32_IMul_left) != out_reg) {
832 ia32_emitf(node, "\timul%M %#S4, %#AS3, %#D0\n");
834 ia32_emitf(node, "\timul%M %#AS4, %#S3\n");
839 * walks up a tree of copies/perms/spills/reloads to find the original value
840 * that is moved around
842 static ir_node *find_original_value(ir_node *node)
844 if (irn_visited(node))
847 mark_irn_visited(node);
848 if (be_is_Copy(node)) {
849 return find_original_value(be_get_Copy_op(node));
850 } else if (be_is_CopyKeep(node)) {
851 return find_original_value(be_get_CopyKeep_op(node));
852 } else if (is_Proj(node)) {
853 ir_node *pred = get_Proj_pred(node);
854 if (be_is_Perm(pred)) {
855 return find_original_value(get_irn_n(pred, get_Proj_proj(node)));
856 } else if (be_is_MemPerm(pred)) {
857 return find_original_value(get_irn_n(pred, get_Proj_proj(node) + 1));
858 } else if (is_ia32_Load(pred)) {
859 return find_original_value(get_irn_n(pred, n_ia32_Load_mem));
863 } else if (is_ia32_Store(node)) {
864 return find_original_value(get_irn_n(node, n_ia32_Store_val));
865 } else if (is_Phi(node)) {
867 arity = get_irn_arity(node);
868 for (i = 0; i < arity; ++i) {
869 ir_node *in = get_irn_n(node, i);
870 ir_node *res = find_original_value(in);
881 static int determine_final_pnc(const ir_node *node, int flags_pos,
884 ir_node *flags = get_irn_n(node, flags_pos);
885 const ia32_attr_t *flags_attr;
886 flags = skip_Proj(flags);
888 if (is_ia32_Sahf(flags)) {
889 ir_node *cmp = get_irn_n(flags, n_ia32_Sahf_val);
890 if (!(is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
891 || is_ia32_FucomppFnstsw(cmp) || is_ia32_FtstFnstsw(cmp))) {
892 inc_irg_visited(current_ir_graph);
893 cmp = find_original_value(cmp);
895 assert(is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
896 || is_ia32_FucomppFnstsw(cmp) || is_ia32_FtstFnstsw(cmp));
899 flags_attr = get_ia32_attr_const(cmp);
900 if (flags_attr->data.ins_permuted)
901 pnc = get_mirrored_pnc(pnc);
902 pnc |= ia32_pn_Cmp_float;
903 } else if (is_ia32_Ucomi(flags) || is_ia32_Fucomi(flags)
904 || is_ia32_Fucompi(flags)) {
905 flags_attr = get_ia32_attr_const(flags);
907 if (flags_attr->data.ins_permuted)
908 pnc = get_mirrored_pnc(pnc);
909 pnc |= ia32_pn_Cmp_float;
911 flags_attr = get_ia32_attr_const(flags);
913 if (flags_attr->data.ins_permuted)
914 pnc = get_mirrored_pnc(pnc);
915 if (flags_attr->data.cmp_unsigned)
916 pnc |= ia32_pn_Cmp_unsigned;
922 void ia32_emit_cmp_suffix_node(const ir_node *node,
925 const ia32_attr_t *attr = get_ia32_attr_const(node);
927 pn_Cmp pnc = get_ia32_condcode(node);
929 pnc = determine_final_pnc(node, flags_pos, pnc);
930 if (attr->data.ins_permuted) {
931 if (pnc & ia32_pn_Cmp_float) {
932 pnc = get_negated_pnc(pnc, mode_F);
934 pnc = get_negated_pnc(pnc, mode_Iu);
938 ia32_emit_cmp_suffix(pnc);
942 * Emits an exception label for a given node.
944 static void ia32_emit_exc_label(const ir_node *node)
946 be_emit_string(be_gas_insn_label_prefix());
947 be_emit_irprintf("%lu", get_ia32_exc_label_id(node));
951 * Returns the Proj with projection number proj and NOT mode_M
953 static ir_node *get_proj(const ir_node *node, long proj)
955 const ir_edge_t *edge;
958 assert(get_irn_mode(node) == mode_T && "expected mode_T node");
960 foreach_out_edge(node, edge) {
961 src = get_edge_src_irn(edge);
963 assert(is_Proj(src) && "Proj expected");
964 if (get_irn_mode(src) == mode_M)
967 if (get_Proj_proj(src) == proj)
973 static int can_be_fallthrough(const ir_node *node)
975 ir_node *target_block = get_cfop_target_block(node);
976 ir_node *block = get_nodes_block(node);
977 return get_prev_block_sched(target_block) == block;
981 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
983 static void emit_ia32_Jcc(const ir_node *node)
985 int need_parity_label = 0;
986 const ir_node *proj_true;
987 const ir_node *proj_false;
988 const ir_node *block;
989 pn_Cmp pnc = get_ia32_condcode(node);
991 pnc = determine_final_pnc(node, 0, pnc);
994 proj_true = get_proj(node, pn_ia32_Jcc_true);
995 assert(proj_true && "Jcc without true Proj");
997 proj_false = get_proj(node, pn_ia32_Jcc_false);
998 assert(proj_false && "Jcc without false Proj");
1000 block = get_nodes_block(node);
1002 if (can_be_fallthrough(proj_true)) {
1003 /* exchange both proj's so the second one can be omitted */
1004 const ir_node *t = proj_true;
1006 proj_true = proj_false;
1008 if (pnc & ia32_pn_Cmp_float) {
1009 pnc = get_negated_pnc(pnc, mode_F);
1011 pnc = get_negated_pnc(pnc, mode_Iu);
1015 if (pnc & ia32_pn_Cmp_float) {
1016 /* Some floating point comparisons require a test of the parity flag,
1017 * which indicates that the result is unordered */
1020 ia32_emitf(proj_true, "\tjp %L\n");
1025 ia32_emitf(proj_true, "\tjnp %L\n");
1031 /* we need a local label if the false proj is a fallthrough
1032 * as the falseblock might have no label emitted then */
1033 if (can_be_fallthrough(proj_false)) {
1034 need_parity_label = 1;
1035 ia32_emitf(proj_false, "\tjp 1f\n");
1037 ia32_emitf(proj_false, "\tjp %L\n");
1044 ia32_emitf(proj_true, "\tjp %L\n");
1052 ia32_emitf(proj_true, "\tj%P %L\n", pnc);
1055 if (need_parity_label) {
1056 ia32_emitf(NULL, "1:\n");
1059 /* the second Proj might be a fallthrough */
1060 if (can_be_fallthrough(proj_false)) {
1061 ia32_emitf(proj_false, "\t/* fallthrough to %L */\n");
1063 ia32_emitf(proj_false, "\tjmp %L\n");
1067 static void emit_ia32_CMov(const ir_node *node)
1069 const ia32_attr_t *attr = get_ia32_attr_const(node);
1070 int ins_permuted = attr->data.ins_permuted;
1071 const arch_register_t *out = arch_get_irn_register(node);
1072 pn_Cmp pnc = get_ia32_condcode(node);
1073 const arch_register_t *in_true;
1074 const arch_register_t *in_false;
1076 pnc = determine_final_pnc(node, n_ia32_CMov_eflags, pnc);
1078 in_true = arch_get_irn_register(get_irn_n(node, n_ia32_CMov_val_true));
1079 in_false = arch_get_irn_register(get_irn_n(node, n_ia32_CMov_val_false));
1081 /* should be same constraint fullfilled? */
1082 if (out == in_false) {
1083 /* yes -> nothing to do */
1084 } else if (out == in_true) {
1085 const arch_register_t *tmp;
1087 assert(get_ia32_op_type(node) == ia32_Normal);
1089 ins_permuted = !ins_permuted;
1096 ia32_emitf(node, "\tmovl %R, %R\n", in_false, out);
1100 if (pnc & ia32_pn_Cmp_float) {
1101 pnc = get_negated_pnc(pnc, mode_F);
1103 pnc = get_negated_pnc(pnc, mode_Iu);
1107 /* TODO: handling of Nans isn't correct yet */
1109 ia32_emitf(node, "\tcmov%P %AR, %#R\n", pnc, in_true, out);
1112 /*********************************************************
1115 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
1116 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
1117 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
1118 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
1121 *********************************************************/
1123 /* jump table entry (target and corresponding number) */
1124 typedef struct _branch_t {
1129 /* jump table for switch generation */
1130 typedef struct _jmp_tbl_t {
1131 ir_node *defProj; /**< default target */
1132 long min_value; /**< smallest switch case */
1133 long max_value; /**< largest switch case */
1134 long num_branches; /**< number of jumps */
1135 char *label; /**< label of the jump table */
1136 branch_t *branches; /**< jump array */
1140 * Compare two variables of type branch_t. Used to sort all switch cases
1142 static int ia32_cmp_branch_t(const void *a, const void *b)
1144 branch_t *b1 = (branch_t *)a;
1145 branch_t *b2 = (branch_t *)b;
1147 if (b1->value <= b2->value)
1154 * Emits code for a SwitchJmp (creates a jump table if
1155 * possible otherwise a cmp-jmp cascade). Port from
1158 static void emit_ia32_SwitchJmp(const ir_node *node)
1160 unsigned long interval;
1166 const ir_edge_t *edge;
1168 /* fill the table structure */
1169 tbl.label = XMALLOCN(char, SNPRINTF_BUF_LEN);
1170 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, ".TBL_");
1172 tbl.num_branches = get_irn_n_edges(node) - 1;
1173 tbl.branches = XMALLOCNZ(branch_t, tbl.num_branches);
1174 tbl.min_value = INT_MAX;
1175 tbl.max_value = INT_MIN;
1177 default_pn = get_ia32_condcode(node);
1179 /* go over all proj's and collect them */
1180 foreach_out_edge(node, edge) {
1181 proj = get_edge_src_irn(edge);
1182 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
1184 pnc = get_Proj_proj(proj);
1186 /* check for default proj */
1187 if (pnc == default_pn) {
1188 assert(tbl.defProj == NULL && "found two default Projs at SwitchJmp");
1191 tbl.min_value = pnc < tbl.min_value ? pnc : tbl.min_value;
1192 tbl.max_value = pnc > tbl.max_value ? pnc : tbl.max_value;
1194 /* create branch entry */
1195 tbl.branches[i].target = proj;
1196 tbl.branches[i].value = pnc;
1201 assert(i == tbl.num_branches);
1203 /* sort the branches by their number */
1204 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
1206 /* two-complement's magic make this work without overflow */
1207 interval = tbl.max_value - tbl.min_value;
1209 /* emit the table */
1210 ia32_emitf(node, "\tcmpl $%u, %S0\n", interval);
1211 ia32_emitf(tbl.defProj, "\tja %L\n");
1213 if (tbl.num_branches > 1) {
1215 ia32_emitf(node, "\tjmp *%s(,%S0,4)\n", tbl.label);
1217 be_gas_emit_switch_section(GAS_SECTION_RODATA);
1218 ia32_emitf(NULL, "\t.align 4\n");
1219 ia32_emitf(NULL, "%s:\n", tbl.label);
1221 last_value = tbl.branches[0].value;
1222 for (i = 0; i != tbl.num_branches; ++i) {
1223 while (last_value != tbl.branches[i].value) {
1224 ia32_emitf(tbl.defProj, ".long %L\n");
1227 ia32_emitf(tbl.branches[i].target, ".long %L\n");
1230 be_gas_emit_switch_section(GAS_SECTION_TEXT);
1232 /* one jump is enough */
1233 ia32_emitf(tbl.branches[0].target, "\tjmp %L\n");
1243 * Emits code for a unconditional jump.
1245 static void emit_Jmp(const ir_node *node)
1249 /* for now, the code works for scheduled and non-schedules blocks */
1250 block = get_nodes_block(node);
1252 /* we have a block schedule */
1253 if (can_be_fallthrough(node)) {
1254 ia32_emitf(node, "\t/* fallthrough to %L */\n");
1256 ia32_emitf(node, "\tjmp %L\n");
1261 * Emit an inline assembler operand.
1263 * @param node the ia32_ASM node
1264 * @param s points to the operand (a %c)
1266 * @return pointer to the first char in s NOT in the current operand
1268 static const char* emit_asm_operand(const ir_node *node, const char *s)
1270 const ia32_attr_t *ia32_attr = get_ia32_attr_const(node);
1271 const ia32_asm_attr_t *attr = CONST_CAST_IA32_ATTR(ia32_asm_attr_t,
1273 const arch_register_t *reg;
1274 const ia32_asm_reg_t *asm_regs = attr->register_map;
1275 const ia32_asm_reg_t *asm_reg;
1276 const char *reg_name;
1285 /* parse modifiers */
1288 ir_fprintf(stderr, "Warning: asm text (%+F) ends with %\n", node);
1312 ir_fprintf(stderr, "Warning: asm text (%+F) contains unknown modifier "
1313 "'%c' for asm op\n", node, c);
1319 sscanf(s, "%d%n", &num, &p);
1321 ir_fprintf(stderr, "Warning: Couldn't parse assembler operand (%+F)\n",
1328 if (num < 0 || num >= ARR_LEN(asm_regs)) {
1329 ir_fprintf(stderr, "Error: Custom assembler references invalid "
1330 "input/output (%+F)\n", node);
1333 asm_reg = & asm_regs[num];
1334 assert(asm_reg->valid);
1337 if (asm_reg->use_input == 0) {
1338 reg = get_out_reg(node, asm_reg->inout_pos);
1340 ir_node *pred = get_irn_n(node, asm_reg->inout_pos);
1342 /* might be an immediate value */
1343 if (is_ia32_Immediate(pred)) {
1344 emit_ia32_Immediate(pred);
1347 reg = get_in_reg(node, asm_reg->inout_pos);
1350 ir_fprintf(stderr, "Warning: no register assigned for %d asm op "
1351 "(%+F)\n", num, node);
1355 if (asm_reg->memory) {
1360 if (modifier != 0) {
1364 reg_name = ia32_get_mapped_reg_name(isa->regs_8bit, reg);
1367 reg_name = ia32_get_mapped_reg_name(isa->regs_8bit_high, reg);
1370 reg_name = ia32_get_mapped_reg_name(isa->regs_16bit, reg);
1373 panic("Invalid asm op modifier");
1375 be_emit_string(reg_name);
1377 emit_register(reg, asm_reg->mode);
1380 if (asm_reg->memory) {
1388 * Emits code for an ASM pseudo op.
1390 static void emit_ia32_Asm(const ir_node *node)
1392 const void *gen_attr = get_irn_generic_attr_const(node);
1393 const ia32_asm_attr_t *attr
1394 = CONST_CAST_IA32_ATTR(ia32_asm_attr_t, gen_attr);
1395 ident *asm_text = attr->asm_text;
1396 const char *s = get_id_str(asm_text);
1398 ia32_emitf(node, "#APP\t\n");
1405 s = emit_asm_operand(node, s);
1411 ia32_emitf(NULL, "\n#NO_APP\n");
1414 /**********************************
1417 * | | ___ _ __ _ _| |_) |
1418 * | | / _ \| '_ \| | | | _ <
1419 * | |___| (_) | |_) | |_| | |_) |
1420 * \_____\___/| .__/ \__, |____/
1423 **********************************/
1426 * Emit movsb/w instructions to make mov count divideable by 4
1428 static void emit_CopyB_prolog(unsigned size)
1431 ia32_emitf(NULL, "\tmovsb\n");
1433 ia32_emitf(NULL, "\tmovsw\n");
1437 * Emit rep movsd instruction for memcopy.
1439 static void emit_ia32_CopyB(const ir_node *node)
1441 unsigned size = get_ia32_copyb_size(node);
1443 emit_CopyB_prolog(size);
1444 ia32_emitf(node, "\trep movsd\n");
1448 * Emits unrolled memcopy.
1450 static void emit_ia32_CopyB_i(const ir_node *node)
1452 unsigned size = get_ia32_copyb_size(node);
1454 emit_CopyB_prolog(size);
1458 ia32_emitf(NULL, "\tmovsd\n");
1464 /***************************
1468 * | | / _ \| '_ \ \ / /
1469 * | |___| (_) | | | \ V /
1470 * \_____\___/|_| |_|\_/
1472 ***************************/
1475 * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1477 static void emit_ia32_Conv_with_FP(const ir_node *node)
1479 ir_mode *ls_mode = get_ia32_ls_mode(node);
1480 int ls_bits = get_mode_size_bits(ls_mode);
1483 if (is_ia32_Conv_I2FP(node)) {
1484 if (ls_bits == 32) {
1489 } else if (is_ia32_Conv_FP2I(node)) {
1490 if (ls_bits == 32) {
1496 assert(is_ia32_Conv_FP2FP(node));
1497 if (ls_bits == 32) {
1504 ia32_emitf(node, "\tcvt%s %AS3, %D0\n", conv);
1507 static void emit_ia32_Conv_I2FP(const ir_node *node)
1509 emit_ia32_Conv_with_FP(node);
1512 static void emit_ia32_Conv_FP2I(const ir_node *node)
1514 emit_ia32_Conv_with_FP(node);
1517 static void emit_ia32_Conv_FP2FP(const ir_node *node)
1519 emit_ia32_Conv_with_FP(node);
1523 * Emits code for an Int conversion.
1525 static void emit_ia32_Conv_I2I(const ir_node *node)
1527 ir_mode *smaller_mode = get_ia32_ls_mode(node);
1528 int smaller_bits = get_mode_size_bits(smaller_mode);
1529 int signed_mode = mode_is_signed(smaller_mode);
1531 assert(!mode_is_float(smaller_mode));
1532 assert(smaller_bits == 8 || smaller_bits == 16);
1535 smaller_bits == 16 &&
1536 &ia32_gp_regs[REG_EAX] == get_out_reg(node, 0) &&
1537 &ia32_gp_regs[REG_EAX] == arch_get_irn_register(get_irn_n(node, n_ia32_unary_op))) {
1538 /* argument and result are both in EAX and signedness is ok: use the
1539 * smaller cwtl opcode */
1540 ia32_emitf(node, "\tcwtl\n");
1542 const char *sign_suffix = signed_mode ? "s" : "z";
1543 ia32_emitf(node, "\tmov%s%Ml %#AS3, %D0\n", sign_suffix);
1550 static void emit_ia32_Call(const ir_node *node)
1552 /* Special case: Call must not have its immediates prefixed by $, instead
1553 * address mode is prefixed by *. */
1554 ia32_emitf(node, "\tcall %*AS3\n");
1558 /*******************************************
1561 * | |__ ___ _ __ ___ __| | ___ ___
1562 * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1563 * | |_) | __/ | | | (_) | (_| | __/\__ \
1564 * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1566 *******************************************/
1569 * Emits code to increase stack pointer.
1571 static void emit_be_IncSP(const ir_node *node)
1573 int offs = be_get_IncSP_offset(node);
1579 ia32_emitf(node, "\tsubl $%u, %D0\n", offs);
1581 ia32_emitf(node, "\taddl $%u, %D0\n", -offs);
1586 * Emits code for Copy/CopyKeep.
1588 static void Copy_emitter(const ir_node *node, const ir_node *op)
1590 const arch_register_t *in = arch_get_irn_register(op);
1591 const arch_register_t *out = arch_get_irn_register(node);
1596 if (is_unknown_reg(in))
1598 /* copies of vf nodes aren't real... */
1599 if (arch_register_get_class(in) == &ia32_reg_classes[CLASS_ia32_vfp])
1602 if (get_irn_mode(node) == mode_E) {
1603 ia32_emitf(node, "\tmovsd %R, %R\n", in, out);
1605 ia32_emitf(node, "\tmovl %R, %R\n", in, out);
1609 static void emit_be_Copy(const ir_node *node)
1611 Copy_emitter(node, be_get_Copy_op(node));
1614 static void emit_be_CopyKeep(const ir_node *node)
1616 Copy_emitter(node, be_get_CopyKeep_op(node));
1620 * Emits code for exchange.
1622 static void emit_be_Perm(const ir_node *node)
1624 const arch_register_t *in0, *in1;
1625 const arch_register_class_t *cls0, *cls1;
1627 in0 = arch_get_irn_register(get_irn_n(node, 0));
1628 in1 = arch_get_irn_register(get_irn_n(node, 1));
1630 cls0 = arch_register_get_class(in0);
1631 cls1 = arch_register_get_class(in1);
1633 assert(cls0 == cls1 && "Register class mismatch at Perm");
1635 if (cls0 == &ia32_reg_classes[CLASS_ia32_gp]) {
1636 ia32_emitf(node, "\txchg %R, %R\n", in1, in0);
1637 } else if (cls0 == &ia32_reg_classes[CLASS_ia32_xmm]) {
1638 ia32_emitf(NULL, "\txorpd %R, %R\n", in1, in0);
1639 ia32_emitf(NULL, "\txorpd %R, %R\n", in0, in1);
1640 ia32_emitf(node, "\txorpd %R, %R\n", in1, in0);
1641 } else if (cls0 == &ia32_reg_classes[CLASS_ia32_vfp]) {
1643 } else if (cls0 == &ia32_reg_classes[CLASS_ia32_st]) {
1646 panic("unexpected register class in be_Perm (%+F)", node);
1651 * Emits code for Constant loading.
1653 static void emit_ia32_Const(const ir_node *node)
1655 ia32_emitf(node, "\tmovl %I, %D0\n");
1659 * Emits code to load the TLS base
1661 static void emit_ia32_LdTls(const ir_node *node)
1663 ia32_emitf(node, "\tmovl %%gs:0, %D0\n");
1666 /* helper function for emit_ia32_Minus64Bit */
1667 static void emit_mov(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1669 ia32_emitf(node, "\tmovl %R, %R\n", src, dst);
1672 /* helper function for emit_ia32_Minus64Bit */
1673 static void emit_neg(const ir_node* node, const arch_register_t *reg)
1675 ia32_emitf(node, "\tnegl %R\n", reg);
1678 /* helper function for emit_ia32_Minus64Bit */
1679 static void emit_sbb0(const ir_node* node, const arch_register_t *reg)
1681 ia32_emitf(node, "\tsbbl $0, %R\n", reg);
1684 /* helper function for emit_ia32_Minus64Bit */
1685 static void emit_sbb(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1687 ia32_emitf(node, "\tsbbl %R, %R\n", src, dst);
1690 /* helper function for emit_ia32_Minus64Bit */
1691 static void emit_xchg(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1693 ia32_emitf(node, "\txchgl %R, %R\n", src, dst);
1696 /* helper function for emit_ia32_Minus64Bit */
1697 static void emit_zero(const ir_node* node, const arch_register_t *reg)
1699 ia32_emitf(node, "\txorl %R, %R\n", reg, reg);
1702 static void emit_ia32_Minus64Bit(const ir_node *node)
1704 const arch_register_t *in_lo = get_in_reg(node, 0);
1705 const arch_register_t *in_hi = get_in_reg(node, 1);
1706 const arch_register_t *out_lo = get_out_reg(node, 0);
1707 const arch_register_t *out_hi = get_out_reg(node, 1);
1709 if (out_lo == in_lo) {
1710 if (out_hi != in_hi) {
1711 /* a -> a, b -> d */
1714 /* a -> a, b -> b */
1717 } else if (out_lo == in_hi) {
1718 if (out_hi == in_lo) {
1719 /* a -> b, b -> a */
1720 emit_xchg(node, in_lo, in_hi);
1723 /* a -> b, b -> d */
1724 emit_mov(node, in_hi, out_hi);
1725 emit_mov(node, in_lo, out_lo);
1729 if (out_hi == in_lo) {
1730 /* a -> c, b -> a */
1731 emit_mov(node, in_lo, out_lo);
1733 } else if (out_hi == in_hi) {
1734 /* a -> c, b -> b */
1735 emit_mov(node, in_lo, out_lo);
1738 /* a -> c, b -> d */
1739 emit_mov(node, in_lo, out_lo);
1745 emit_neg( node, out_hi);
1746 emit_neg( node, out_lo);
1747 emit_sbb0(node, out_hi);
1751 emit_zero(node, out_hi);
1752 emit_neg( node, out_lo);
1753 emit_sbb( node, in_hi, out_hi);
1756 static void emit_ia32_GetEIP(const ir_node *node)
1758 ia32_emitf(node, "\tcall %s\n", pic_base_label);
1759 ia32_emitf(NULL, "%s:\n", pic_base_label);
1760 ia32_emitf(node, "\tpopl %D0\n");
1763 static void emit_be_Return(const ir_node *node)
1765 unsigned pop = be_Return_get_pop(node);
1767 if (pop > 0 || be_Return_get_emit_pop(node)) {
1768 ia32_emitf(node, "\tret $%u\n", pop);
1770 ia32_emitf(node, "\tret\n");
1774 static void emit_Nothing(const ir_node *node)
1780 /***********************************************************************************
1783 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
1784 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1785 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
1786 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
1788 ***********************************************************************************/
1791 * Enters the emitter functions for handled nodes into the generic
1792 * pointer of an opcode.
1794 static void ia32_register_emitters(void)
1796 #define IA32_EMIT2(a,b) op_ia32_##a->ops.generic = (op_func)emit_ia32_##b
1797 #define IA32_EMIT(a) IA32_EMIT2(a,a)
1798 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
1799 #define IGN(a) op_##a->ops.generic = (op_func)emit_Nothing
1800 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
1801 #define BE_IGN(a) op_be_##a->ops.generic = (op_func)emit_Nothing
1803 /* first clear the generic function pointer for all ops */
1804 clear_irp_opcodes_generic_func();
1806 /* register all emitter functions defined in spec */
1807 ia32_register_spec_emitters();
1809 /* other ia32 emitter functions */
1810 IA32_EMIT2(Conv_I2I8Bit, Conv_I2I);
1815 IA32_EMIT(Conv_FP2FP);
1816 IA32_EMIT(Conv_FP2I);
1817 IA32_EMIT(Conv_I2FP);
1818 IA32_EMIT(Conv_I2I);
1825 IA32_EMIT(Minus64Bit);
1826 IA32_EMIT(SwitchJmp);
1828 /* benode emitter */
1851 typedef void (*emit_func_ptr) (const ir_node *);
1854 * Assign and emit an exception label if the current instruction can fail.
1856 static void ia32_assign_exc_label(ir_node *node)
1858 /* assign a new ID to the instruction */
1859 set_ia32_exc_label_id(node, ++exc_label_id);
1861 ia32_emit_exc_label(node);
1863 be_emit_pad_comment();
1864 be_emit_cstring("/* exception to Block ");
1865 ia32_emit_cfop_target(node);
1866 be_emit_cstring(" */\n");
1867 be_emit_write_line();
1871 * Emits code for a node.
1873 static void ia32_emit_node(ir_node *node)
1875 ir_op *op = get_irn_op(node);
1877 DBG((dbg, LEVEL_1, "emitting code for %+F\n", node));
1879 if (is_ia32_irn(node)) {
1880 if (get_ia32_exc_label(node)) {
1881 /* emit the exception label of this instruction */
1882 ia32_assign_exc_label(node);
1884 if (mark_spill_reload) {
1885 if (is_ia32_is_spill(node)) {
1886 ia32_emitf(NULL, "\txchg %ebx, %ebx /* spill mark */\n");
1888 if (is_ia32_is_reload(node)) {
1889 ia32_emitf(NULL, "\txchg %edx, %edx /* reload mark */\n");
1891 if (is_ia32_is_remat(node)) {
1892 ia32_emitf(NULL, "\txchg %ecx, %ecx /* remat mark */\n");
1896 if (op->ops.generic) {
1897 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
1899 be_dbg_set_dbg_info(get_irn_dbg_info(node));
1904 ir_fprintf(stderr, "Error: No emit handler for node %+F (%+G, graph %+F)\n", node, node, current_ir_graph);
1910 * Emits gas alignment directives
1912 static void ia32_emit_alignment(unsigned align, unsigned skip)
1914 ia32_emitf(NULL, "\t.p2align %u,,%u\n", align, skip);
1918 * Emits gas alignment directives for Labels depended on cpu architecture.
1920 static void ia32_emit_align_label(void)
1922 unsigned align = ia32_cg_config.label_alignment;
1923 unsigned maximum_skip = ia32_cg_config.label_alignment_max_skip;
1924 ia32_emit_alignment(align, maximum_skip);
1928 * Test whether a block should be aligned.
1929 * For cpus in the P4/Athlon class it is useful to align jump labels to
1930 * 16 bytes. However we should only do that if the alignment nops before the
1931 * label aren't executed more often than we have jumps to the label.
1933 static int should_align_block(const ir_node *block)
1935 static const double DELTA = .0001;
1936 ir_exec_freq *exec_freq = cg->birg->exec_freq;
1937 ir_node *prev = get_prev_block_sched(block);
1939 double prev_freq = 0; /**< execfreq of the fallthrough block */
1940 double jmp_freq = 0; /**< execfreq of all non-fallthrough blocks */
1943 if (exec_freq == NULL)
1945 if (ia32_cg_config.label_alignment_factor <= 0)
1948 block_freq = get_block_execfreq(exec_freq, block);
1949 if (block_freq < DELTA)
1952 n_cfgpreds = get_Block_n_cfgpreds(block);
1953 for(i = 0; i < n_cfgpreds; ++i) {
1954 const ir_node *pred = get_Block_cfgpred_block(block, i);
1955 double pred_freq = get_block_execfreq(exec_freq, pred);
1958 prev_freq += pred_freq;
1960 jmp_freq += pred_freq;
1964 if (prev_freq < DELTA && !(jmp_freq < DELTA))
1967 jmp_freq /= prev_freq;
1969 return jmp_freq > ia32_cg_config.label_alignment_factor;
1973 * Emit the block header for a block.
1975 * @param block the block
1976 * @param prev_block the previous block
1978 static void ia32_emit_block_header(ir_node *block)
1980 ir_graph *irg = current_ir_graph;
1981 int need_label = block_needs_label(block);
1983 ir_exec_freq *exec_freq = cg->birg->exec_freq;
1985 if (block == get_irg_end_block(irg) || block == get_irg_start_block(irg))
1988 if (ia32_cg_config.label_alignment > 0) {
1989 /* align the current block if:
1990 * a) if should be aligned due to its execution frequency
1991 * b) there is no fall-through here
1993 if (should_align_block(block)) {
1994 ia32_emit_align_label();
1996 /* if the predecessor block has no fall-through,
1997 we can always align the label. */
1999 int has_fallthrough = 0;
2001 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
2002 ir_node *cfg_pred = get_Block_cfgpred(block, i);
2003 if (can_be_fallthrough(cfg_pred)) {
2004 has_fallthrough = 1;
2009 if (!has_fallthrough)
2010 ia32_emit_align_label();
2014 if (need_label || has_Block_label(block)) {
2015 ia32_emit_block_name(block);
2018 be_emit_pad_comment();
2019 be_emit_cstring(" /* ");
2021 be_emit_cstring("\t/* ");
2022 ia32_emit_block_name(block);
2023 be_emit_cstring(": ");
2026 be_emit_cstring("preds:");
2028 /* emit list of pred blocks in comment */
2029 arity = get_irn_arity(block);
2030 for (i = 0; i < arity; ++i) {
2031 ir_node *predblock = get_Block_cfgpred_block(block, i);
2032 be_emit_irprintf(" %d", get_irn_node_nr(predblock));
2034 if (exec_freq != NULL) {
2035 be_emit_irprintf(" freq: %f",
2036 get_block_execfreq(exec_freq, block));
2038 be_emit_cstring(" */\n");
2039 be_emit_write_line();
2043 * Walks over the nodes in a block connected by scheduling edges
2044 * and emits code for each node.
2046 static void ia32_gen_block(ir_node *block)
2050 ia32_emit_block_header(block);
2052 /* emit the contents of the block */
2053 be_dbg_set_dbg_info(get_irn_dbg_info(block));
2054 sched_foreach(block, node) {
2055 ia32_emit_node(node);
2059 typedef struct exc_entry {
2060 ir_node *exc_instr; /** The instruction that can issue an exception. */
2061 ir_node *block; /** The block to call then. */
2066 * Sets labels for control flow nodes (jump target).
2067 * Links control predecessors to there destination blocks.
2069 static void ia32_gen_labels(ir_node *block, void *data)
2071 exc_entry **exc_list = data;
2075 for (n = get_Block_n_cfgpreds(block) - 1; n >= 0; --n) {
2076 pred = get_Block_cfgpred(block, n);
2077 set_irn_link(pred, block);
2079 pred = skip_Proj(pred);
2080 if (is_ia32_irn(pred) && get_ia32_exc_label(pred)) {
2085 ARR_APP1(exc_entry, *exc_list, e);
2086 set_irn_link(pred, block);
2092 * Compare two exception_entries.
2094 static int cmp_exc_entry(const void *a, const void *b)
2096 const exc_entry *ea = a;
2097 const exc_entry *eb = b;
2099 if (get_ia32_exc_label_id(ea->exc_instr) < get_ia32_exc_label_id(eb->exc_instr))
2105 * Main driver. Emits the code for one routine.
2107 void ia32_gen_routine(ia32_code_gen_t *ia32_cg, ir_graph *irg)
2109 ir_entity *entity = get_irg_entity(irg);
2110 exc_entry *exc_list = NEW_ARR_F(exc_entry, 0);
2114 isa = (const ia32_isa_t*) cg->arch_env;
2115 arch_env = cg->arch_env;
2116 do_pic = cg->birg->main_env->options->pic;
2118 ia32_register_emitters();
2120 get_unique_label(pic_base_label, sizeof(pic_base_label), ".PIC_BASE");
2122 be_dbg_method_begin(entity, be_abi_get_stack_layout(cg->birg->abi));
2123 be_gas_emit_function_prolog(entity, ia32_cg_config.function_alignment);
2125 /* we use links to point to target blocks */
2126 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
2127 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &exc_list);
2129 /* initialize next block links */
2130 n = ARR_LEN(cg->blk_sched);
2131 for (i = 0; i < n; ++i) {
2132 ir_node *block = cg->blk_sched[i];
2133 ir_node *prev = i > 0 ? cg->blk_sched[i-1] : NULL;
2135 set_irn_link(block, prev);
2138 for (i = 0; i < n; ++i) {
2139 ir_node *block = cg->blk_sched[i];
2141 ia32_gen_block(block);
2144 be_gas_emit_function_epilog(entity);
2145 be_dbg_method_end();
2147 be_emit_write_line();
2149 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
2151 /* Sort the exception table using the exception label id's.
2152 Those are ascending with ascending addresses. */
2153 qsort(exc_list, ARR_LEN(exc_list), sizeof(exc_list[0]), cmp_exc_entry);
2157 for (i = 0; i < ARR_LEN(exc_list); ++i) {
2158 be_emit_cstring("\t.long ");
2159 ia32_emit_exc_label(exc_list[i].exc_instr);
2161 be_emit_cstring("\t.long ");
2162 ia32_emit_block_name(exc_list[i].block);
2166 DEL_ARR_F(exc_list);
2169 static const lc_opt_table_entry_t ia32_emitter_options[] = {
2170 LC_OPT_ENT_BOOL("mark_spill_reload", "mark spills and reloads with ud opcodes", &mark_spill_reload),
2174 void ia32_init_emitter(void)
2176 lc_opt_entry_t *be_grp;
2177 lc_opt_entry_t *ia32_grp;
2179 be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
2180 ia32_grp = lc_opt_get_grp(be_grp, "ia32");
2182 lc_opt_add_table(ia32_grp, ia32_emitter_options);
2184 FIRM_DBG_REGISTER(dbg, "firm.be.ia32.emitter");