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(arch_env, 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;
143 /* ask for the requirements */
144 req = arch_get_register_req(arch_env, irn, pos);
146 if (arch_register_req_is(req, limited)) {
147 /* in case of limited requirements: get the first allowed register */
148 unsigned idx = rbitset_next(req->limited, 0, 1);
149 reg = arch_register_for_index(req->cls, idx);
151 /* otherwise get first register in class */
152 reg = arch_register_for_index(req->cls, 0);
160 * Returns the register at out position pos.
162 static const arch_register_t *get_out_reg(const ir_node *irn, int pos)
165 const arch_register_t *reg = NULL;
167 /* 1st case: irn is not of mode_T, so it has only */
168 /* one OUT register -> good */
169 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
170 /* Proj with the corresponding projnum for the register */
172 if (get_irn_mode(irn) != mode_T) {
174 reg = arch_get_irn_register(arch_env, irn);
175 } else if (is_ia32_irn(irn)) {
176 reg = get_ia32_out_reg(irn, pos);
178 const ir_edge_t *edge;
180 foreach_out_edge(irn, edge) {
181 proj = get_edge_src_irn(edge);
182 assert(is_Proj(proj) && "non-Proj from mode_T node");
183 if (get_Proj_proj(proj) == pos) {
184 reg = arch_get_irn_register(arch_env, proj);
190 assert(reg && "no out register found");
195 * Add a number to a prefix. This number will not be used a second time.
197 static char *get_unique_label(char *buf, size_t buflen, const char *prefix)
199 static unsigned long id = 0;
200 snprintf(buf, buflen, "%s%lu", prefix, ++id);
204 /*************************************************************
206 * (_) | | / _| | | | |
207 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
208 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
209 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
210 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
213 *************************************************************/
215 static void emit_8bit_register(const arch_register_t *reg)
217 const char *reg_name = arch_register_get_name(reg);
220 be_emit_char(reg_name[1]);
224 static void emit_16bit_register(const arch_register_t *reg)
226 const char *reg_name = ia32_get_mapped_reg_name(isa->regs_16bit, reg);
229 be_emit_string(reg_name);
232 static void emit_register(const arch_register_t *reg, const ir_mode *mode)
234 const char *reg_name;
237 int size = get_mode_size_bits(mode);
239 case 8: emit_8bit_register(reg); return;
240 case 16: emit_16bit_register(reg); return;
242 assert(mode_is_float(mode) || size == 32);
245 reg_name = arch_register_get_name(reg);
248 be_emit_string(reg_name);
251 void ia32_emit_source_register(const ir_node *node, int pos)
253 const arch_register_t *reg = get_in_reg(node, pos);
255 emit_register(reg, NULL);
258 static void ia32_emit_entity(ir_entity *entity, int no_pic_adjust)
262 set_entity_backend_marked(entity, 1);
263 id = get_entity_ld_ident(entity);
266 if (get_entity_owner(entity) == get_tls_type()) {
267 if (get_entity_visibility(entity) == visibility_external_allocated) {
268 be_emit_cstring("@INDNTPOFF");
270 be_emit_cstring("@NTPOFF");
274 if (!no_pic_adjust && do_pic) {
275 /* TODO: only do this when necessary */
277 be_emit_string(pic_base_label);
281 static void emit_ia32_Immediate_no_prefix(const ir_node *node)
283 const ia32_immediate_attr_t *attr = get_ia32_immediate_attr_const(node);
285 if (attr->symconst != NULL) {
288 ia32_emit_entity(attr->symconst, 0);
290 if (attr->symconst == NULL || attr->offset != 0) {
291 if (attr->symconst != NULL) {
292 be_emit_irprintf("%+d", attr->offset);
294 be_emit_irprintf("0x%X", attr->offset);
299 static void emit_ia32_Immediate(const ir_node *node)
302 emit_ia32_Immediate_no_prefix(node);
305 void ia32_emit_8bit_source_register_or_immediate(const ir_node *node, int pos)
307 const arch_register_t *reg;
308 ir_node *in = get_irn_n(node, pos);
309 if (is_ia32_Immediate(in)) {
310 emit_ia32_Immediate(in);
314 reg = get_in_reg(node, pos);
315 emit_8bit_register(reg);
318 void ia32_emit_dest_register(const ir_node *node, int pos)
320 const arch_register_t *reg = get_out_reg(node, pos);
322 emit_register(reg, NULL);
325 void ia32_emit_8bit_dest_register(const ir_node *node, int pos)
327 const arch_register_t *reg = get_out_reg(node, pos);
329 emit_register(reg, mode_Bu);
332 void ia32_emit_x87_register(const ir_node *node, int pos)
334 const ia32_x87_attr_t *attr = get_ia32_x87_attr_const(node);
338 be_emit_string(attr->x87[pos]->name);
341 static void ia32_emit_mode_suffix_mode(const ir_mode *mode)
343 assert(mode_is_int(mode) || mode_is_reference(mode));
344 switch (get_mode_size_bits(mode)) {
345 case 8: be_emit_char('b'); return;
346 case 16: be_emit_char('w'); return;
347 case 32: be_emit_char('l'); return;
348 /* gas docu says q is the suffix but gcc, objdump and icc use ll
350 case 64: be_emit_cstring("ll"); return;
352 panic("Can't output mode_suffix for %+F", mode);
355 void ia32_emit_mode_suffix(const ir_node *node)
357 ir_mode *mode = get_ia32_ls_mode(node);
361 ia32_emit_mode_suffix_mode(mode);
364 void ia32_emit_x87_mode_suffix(const ir_node *node)
368 /* we only need to emit the mode on address mode */
369 if (get_ia32_op_type(node) == ia32_Normal)
372 mode = get_ia32_ls_mode(node);
373 assert(mode != NULL);
375 if (mode_is_float(mode)) {
376 switch (get_mode_size_bits(mode)) {
377 case 32: be_emit_char('s'); return;
378 case 64: be_emit_char('l'); return;
380 case 96: be_emit_char('t'); return;
383 assert(mode_is_int(mode));
384 switch (get_mode_size_bits(mode)) {
385 case 16: be_emit_char('s'); return;
386 case 32: be_emit_char('l'); return;
387 /* gas docu says q is the suffix but gcc, objdump and icc use ll
389 case 64: be_emit_cstring("ll"); return;
392 panic("Can't output mode_suffix for %+F", mode);
395 static char get_xmm_mode_suffix(ir_mode *mode)
397 assert(mode_is_float(mode));
398 switch(get_mode_size_bits(mode)) {
401 default: panic("Invalid XMM mode");
405 void ia32_emit_xmm_mode_suffix(const ir_node *node)
407 ir_mode *mode = get_ia32_ls_mode(node);
408 assert(mode != NULL);
410 be_emit_char(get_xmm_mode_suffix(mode));
413 void ia32_emit_xmm_mode_suffix_s(const ir_node *node)
415 ir_mode *mode = get_ia32_ls_mode(node);
416 assert(mode != NULL);
417 be_emit_char(get_xmm_mode_suffix(mode));
420 void ia32_emit_extend_suffix(const ir_mode *mode)
422 if (get_mode_size_bits(mode) == 32)
424 be_emit_char(mode_is_signed(mode) ? 's' : 'z');
427 void ia32_emit_source_register_or_immediate(const ir_node *node, int pos)
429 ir_node *in = get_irn_n(node, pos);
430 if (is_ia32_Immediate(in)) {
431 emit_ia32_Immediate(in);
433 const ir_mode *mode = get_ia32_ls_mode(node);
434 const arch_register_t *reg = get_in_reg(node, pos);
435 emit_register(reg, mode);
440 * Returns the target block for a control flow node.
442 static ir_node *get_cfop_target_block(const ir_node *irn)
444 assert(get_irn_mode(irn) == mode_X);
445 return get_irn_link(irn);
449 * Emits a block label for the given block.
451 static void ia32_emit_block_name(const ir_node *block)
453 if (has_Block_label(block)) {
454 be_emit_string(be_gas_block_label_prefix());
455 be_emit_irprintf("%lu", get_Block_label(block));
457 be_emit_cstring(BLOCK_PREFIX);
458 be_emit_irprintf("%ld", get_irn_node_nr(block));
463 * Emits the target label for a control flow node.
465 static void ia32_emit_cfop_target(const ir_node *node)
467 ir_node *block = get_cfop_target_block(node);
468 ia32_emit_block_name(block);
472 * coding of conditions
474 struct cmp2conditon_t {
480 * positive conditions for signed compares
482 static const struct cmp2conditon_t cmp2condition_s[] = {
483 { NULL, pn_Cmp_False }, /* always false */
484 { "e", pn_Cmp_Eq }, /* == */
485 { "l", pn_Cmp_Lt }, /* < */
486 { "le", pn_Cmp_Le }, /* <= */
487 { "g", pn_Cmp_Gt }, /* > */
488 { "ge", pn_Cmp_Ge }, /* >= */
489 { "ne", pn_Cmp_Lg }, /* != */
490 { NULL, pn_Cmp_Leg}, /* always true */
494 * positive conditions for unsigned compares
496 static const struct cmp2conditon_t cmp2condition_u[] = {
497 { NULL, pn_Cmp_False }, /* always false */
498 { "e", pn_Cmp_Eq }, /* == */
499 { "b", pn_Cmp_Lt }, /* < */
500 { "be", pn_Cmp_Le }, /* <= */
501 { "a", pn_Cmp_Gt }, /* > */
502 { "ae", pn_Cmp_Ge }, /* >= */
503 { "ne", pn_Cmp_Lg }, /* != */
504 { NULL, pn_Cmp_Leg }, /* always true */
507 static void ia32_emit_cmp_suffix(int pnc)
511 if ((pnc & ia32_pn_Cmp_float) || (pnc & ia32_pn_Cmp_unsigned)) {
513 assert(cmp2condition_u[pnc].num == pnc);
514 str = cmp2condition_u[pnc].name;
517 assert(cmp2condition_s[pnc].num == pnc);
518 str = cmp2condition_s[pnc].name;
524 typedef enum ia32_emit_mod_t {
525 EMIT_RESPECT_LS = 1U << 0,
526 EMIT_ALTERNATE_AM = 1U << 1
530 * fmt parameter output
531 * ---- ---------------------- ---------------------------------------------
533 * %AM <node> address mode of the node
534 * %AR const arch_register_t* address mode of the node or register
535 * %ASx <node> address mode of the node or source register x
536 * %Dx <node> destination register x
537 * %I <node> immediate of the node
538 * %L <node> control flow target of the node
539 * %M <node> mode suffix of the node
540 * %P int condition code
541 * %R const arch_register_t* register
542 * %Sx <node> source register x
543 * %s const char* string
544 * %u unsigned int unsigned int
547 * # modifier for %ASx, %D and %S uses ls mode of node to alter register width
548 * * modifier does not prefix immediates with $, but AM with *
550 static void ia32_emitf(const ir_node *node, const char *fmt, ...)
556 const char *start = fmt;
557 ia32_emit_mod_t mod = 0;
559 while (*fmt != '%' && *fmt != '\n' && *fmt != '\0')
562 be_emit_string_len(start, fmt - start);
566 be_emit_finish_line_gas(node);
578 mod |= EMIT_ALTERNATE_AM;
583 mod |= EMIT_RESPECT_LS;
595 if (mod & EMIT_ALTERNATE_AM)
601 const arch_register_t *reg = va_arg(ap, const arch_register_t*);
602 if (get_ia32_op_type(node) == ia32_AddrModeS) {
603 if (mod & EMIT_ALTERNATE_AM)
607 emit_register(reg, NULL);
613 if (get_ia32_op_type(node) == ia32_AddrModeS) {
614 if (mod & EMIT_ALTERNATE_AM)
619 assert(get_ia32_op_type(node) == ia32_Normal);
624 default: goto unknown;
631 const arch_register_t *reg;
633 if (*fmt < '0' || '9' <= *fmt)
637 reg = get_out_reg(node, pos);
638 emit_register(reg, mod & EMIT_RESPECT_LS ? get_ia32_ls_mode(node) : NULL);
643 if (!(mod & EMIT_ALTERNATE_AM))
645 emit_ia32_Immediate_no_prefix(node);
649 ia32_emit_cfop_target(node);
653 ia32_emit_mode_suffix_mode(get_ia32_ls_mode(node));
658 int pnc = va_arg(ap, int);
659 ia32_emit_cmp_suffix(pnc);
664 const arch_register_t *reg = va_arg(ap, const arch_register_t*);
665 emit_register(reg, NULL);
674 if (*fmt < '0' || '9' <= *fmt)
678 in = get_irn_n(node, pos);
679 if (is_ia32_Immediate(in)) {
680 if (!(mod & EMIT_ALTERNATE_AM))
682 emit_ia32_Immediate_no_prefix(in);
684 const arch_register_t *reg = get_in_reg(node, pos);
685 emit_register(reg, mod & EMIT_RESPECT_LS ? get_ia32_ls_mode(node) : NULL);
691 const char *str = va_arg(ap, const char*);
697 unsigned num = va_arg(ap, unsigned);
698 be_emit_irprintf("%u", num);
704 panic("unknown conversion");
712 * Emits registers and/or address mode of a binary operation.
714 void ia32_emit_binop(const ir_node *node)
716 if (is_ia32_Immediate(get_irn_n(node, n_ia32_binary_right))) {
717 ia32_emitf(node, "%#S4, %#AS3");
719 ia32_emitf(node, "%#AS4, %#S3");
724 * Emits registers and/or address mode of a binary operation.
726 void ia32_emit_x87_binop(const ir_node *node)
728 switch(get_ia32_op_type(node)) {
731 const ia32_x87_attr_t *x87_attr = get_ia32_x87_attr_const(node);
732 const arch_register_t *in1 = x87_attr->x87[0];
733 const arch_register_t *in = x87_attr->x87[1];
734 const arch_register_t *out = x87_attr->x87[2];
738 } else if (out == in) {
743 be_emit_string(arch_register_get_name(in));
744 be_emit_cstring(", %");
745 be_emit_string(arch_register_get_name(out));
753 assert(0 && "unsupported op type");
758 * Emits registers and/or address mode of a unary operation.
760 void ia32_emit_unop(const ir_node *node, int pos)
764 ia32_emitf(node, fmt);
768 * Emits address mode.
770 void ia32_emit_am(const ir_node *node)
772 ir_entity *ent = get_ia32_am_sc(node);
773 int offs = get_ia32_am_offs_int(node);
774 ir_node *base = get_irn_n(node, n_ia32_base);
775 int has_base = !is_ia32_NoReg_GP(base);
776 ir_node *index = get_irn_n(node, n_ia32_index);
777 int has_index = !is_ia32_NoReg_GP(index);
779 /* just to be sure... */
780 assert(!is_ia32_use_frame(node) || get_ia32_frame_ent(node) != NULL);
784 if (is_ia32_am_sc_sign(node))
786 ia32_emit_entity(ent, 0);
789 /* also handle special case if nothing is set */
790 if (offs != 0 || (ent == NULL && !has_base && !has_index)) {
792 be_emit_irprintf("%+d", offs);
794 be_emit_irprintf("%d", offs);
798 if (has_base || has_index) {
803 const arch_register_t *reg = get_in_reg(node, n_ia32_base);
804 emit_register(reg, NULL);
807 /* emit index + scale */
809 const arch_register_t *reg = get_in_reg(node, n_ia32_index);
812 emit_register(reg, NULL);
814 scale = get_ia32_am_scale(node);
816 be_emit_irprintf(",%d", 1 << scale);
823 static void emit_ia32_IMul(const ir_node *node)
825 ir_node *left = get_irn_n(node, n_ia32_IMul_left);
826 const arch_register_t *out_reg = get_out_reg(node, pn_ia32_IMul_res);
828 /* do we need the 3-address form? */
829 if (is_ia32_NoReg_GP(left) ||
830 get_in_reg(node, n_ia32_IMul_left) != out_reg) {
831 ia32_emitf(node, "\timul%M %#S4, %#AS3, %#D0\n");
833 ia32_emitf(node, "\timul%M %#AS4, %#S3\n");
838 * walks up a tree of copies/perms/spills/reloads to find the original value
839 * that is moved around
841 static ir_node *find_original_value(ir_node *node)
843 if (irn_visited(node))
846 mark_irn_visited(node);
847 if (be_is_Copy(node)) {
848 return find_original_value(be_get_Copy_op(node));
849 } else if (be_is_CopyKeep(node)) {
850 return find_original_value(be_get_CopyKeep_op(node));
851 } else if (is_Proj(node)) {
852 ir_node *pred = get_Proj_pred(node);
853 if (be_is_Perm(pred)) {
854 return find_original_value(get_irn_n(pred, get_Proj_proj(node)));
855 } else if (be_is_MemPerm(pred)) {
856 return find_original_value(get_irn_n(pred, get_Proj_proj(node) + 1));
857 } else if (is_ia32_Load(pred)) {
858 return find_original_value(get_irn_n(pred, n_ia32_Load_mem));
862 } else if (is_ia32_Store(node)) {
863 return find_original_value(get_irn_n(node, n_ia32_Store_val));
864 } else if (is_Phi(node)) {
866 arity = get_irn_arity(node);
867 for (i = 0; i < arity; ++i) {
868 ir_node *in = get_irn_n(node, i);
869 ir_node *res = find_original_value(in);
880 static int determine_final_pnc(const ir_node *node, int flags_pos,
883 ir_node *flags = get_irn_n(node, flags_pos);
884 const ia32_attr_t *flags_attr;
885 flags = skip_Proj(flags);
887 if (is_ia32_Sahf(flags)) {
888 ir_node *cmp = get_irn_n(flags, n_ia32_Sahf_val);
889 if (!(is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
890 || is_ia32_FucomppFnstsw(cmp) || is_ia32_FtstFnstsw(cmp))) {
891 inc_irg_visited(current_ir_graph);
892 cmp = find_original_value(cmp);
894 assert(is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
895 || is_ia32_FucomppFnstsw(cmp) || is_ia32_FtstFnstsw(cmp));
898 flags_attr = get_ia32_attr_const(cmp);
899 if (flags_attr->data.ins_permuted)
900 pnc = get_mirrored_pnc(pnc);
901 pnc |= ia32_pn_Cmp_float;
902 } else if (is_ia32_Ucomi(flags) || is_ia32_Fucomi(flags)
903 || is_ia32_Fucompi(flags)) {
904 flags_attr = get_ia32_attr_const(flags);
906 if (flags_attr->data.ins_permuted)
907 pnc = get_mirrored_pnc(pnc);
908 pnc |= ia32_pn_Cmp_float;
910 flags_attr = get_ia32_attr_const(flags);
912 if (flags_attr->data.ins_permuted)
913 pnc = get_mirrored_pnc(pnc);
914 if (flags_attr->data.cmp_unsigned)
915 pnc |= ia32_pn_Cmp_unsigned;
921 void ia32_emit_cmp_suffix_node(const ir_node *node,
924 const ia32_attr_t *attr = get_ia32_attr_const(node);
926 pn_Cmp pnc = get_ia32_condcode(node);
928 pnc = determine_final_pnc(node, flags_pos, pnc);
929 if (attr->data.ins_permuted) {
930 if (pnc & ia32_pn_Cmp_float) {
931 pnc = get_negated_pnc(pnc, mode_F);
933 pnc = get_negated_pnc(pnc, mode_Iu);
937 ia32_emit_cmp_suffix(pnc);
941 * Emits an exception label for a given node.
943 static void ia32_emit_exc_label(const ir_node *node)
945 be_emit_string(be_gas_insn_label_prefix());
946 be_emit_irprintf("%lu", get_ia32_exc_label_id(node));
950 * Returns the Proj with projection number proj and NOT mode_M
952 static ir_node *get_proj(const ir_node *node, long proj)
954 const ir_edge_t *edge;
957 assert(get_irn_mode(node) == mode_T && "expected mode_T node");
959 foreach_out_edge(node, edge) {
960 src = get_edge_src_irn(edge);
962 assert(is_Proj(src) && "Proj expected");
963 if (get_irn_mode(src) == mode_M)
966 if (get_Proj_proj(src) == proj)
972 static int can_be_fallthrough(const ir_node *node)
974 ir_node *target_block = get_cfop_target_block(node);
975 ir_node *block = get_nodes_block(node);
976 return get_prev_block_sched(target_block) == block;
980 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
982 static void emit_ia32_Jcc(const ir_node *node)
984 int need_parity_label = 0;
985 const ir_node *proj_true;
986 const ir_node *proj_false;
987 const ir_node *block;
988 pn_Cmp pnc = get_ia32_condcode(node);
990 pnc = determine_final_pnc(node, 0, pnc);
993 proj_true = get_proj(node, pn_ia32_Jcc_true);
994 assert(proj_true && "Jcc without true Proj");
996 proj_false = get_proj(node, pn_ia32_Jcc_false);
997 assert(proj_false && "Jcc without false Proj");
999 block = get_nodes_block(node);
1001 if (can_be_fallthrough(proj_true)) {
1002 /* exchange both proj's so the second one can be omitted */
1003 const ir_node *t = proj_true;
1005 proj_true = proj_false;
1007 if (pnc & ia32_pn_Cmp_float) {
1008 pnc = get_negated_pnc(pnc, mode_F);
1010 pnc = get_negated_pnc(pnc, mode_Iu);
1014 if (pnc & ia32_pn_Cmp_float) {
1015 /* Some floating point comparisons require a test of the parity flag,
1016 * which indicates that the result is unordered */
1019 ia32_emitf(proj_true, "\tjp %L\n");
1024 ia32_emitf(proj_true, "\tjnp %L\n");
1030 /* we need a local label if the false proj is a fallthrough
1031 * as the falseblock might have no label emitted then */
1032 if (can_be_fallthrough(proj_false)) {
1033 need_parity_label = 1;
1034 ia32_emitf(proj_false, "\tjp 1f\n");
1036 ia32_emitf(proj_false, "\tjp %L\n");
1043 ia32_emitf(proj_true, "\tjp %L\n");
1051 ia32_emitf(proj_true, "\tj%P %L\n", pnc);
1054 if (need_parity_label) {
1055 ia32_emitf(NULL, "1:\n");
1058 /* the second Proj might be a fallthrough */
1059 if (can_be_fallthrough(proj_false)) {
1060 ia32_emitf(proj_false, "\t/* fallthrough to %L */\n");
1062 ia32_emitf(proj_false, "\tjmp %L\n");
1066 static void emit_ia32_CMov(const ir_node *node)
1068 const ia32_attr_t *attr = get_ia32_attr_const(node);
1069 int ins_permuted = attr->data.ins_permuted;
1070 const arch_register_t *out = arch_get_irn_register(arch_env, node);
1071 pn_Cmp pnc = get_ia32_condcode(node);
1072 const arch_register_t *in_true;
1073 const arch_register_t *in_false;
1075 pnc = determine_final_pnc(node, n_ia32_CMov_eflags, pnc);
1077 in_true = arch_get_irn_register(arch_env,
1078 get_irn_n(node, n_ia32_CMov_val_true));
1079 in_false = arch_get_irn_register(arch_env,
1080 get_irn_n(node, n_ia32_CMov_val_false));
1082 /* should be same constraint fullfilled? */
1083 if (out == in_false) {
1084 /* yes -> nothing to do */
1085 } else if (out == in_true) {
1086 const arch_register_t *tmp;
1088 assert(get_ia32_op_type(node) == ia32_Normal);
1090 ins_permuted = !ins_permuted;
1097 ia32_emitf(node, "\tmovl %R, %R\n", in_false, out);
1101 if (pnc & ia32_pn_Cmp_float) {
1102 pnc = get_negated_pnc(pnc, mode_F);
1104 pnc = get_negated_pnc(pnc, mode_Iu);
1108 /* TODO: handling of Nans isn't correct yet */
1110 ia32_emitf(node, "\tcmov%P %AR, %#R\n", pnc, in_true, out);
1113 /*********************************************************
1116 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
1117 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
1118 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
1119 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
1122 *********************************************************/
1124 /* jump table entry (target and corresponding number) */
1125 typedef struct _branch_t {
1130 /* jump table for switch generation */
1131 typedef struct _jmp_tbl_t {
1132 ir_node *defProj; /**< default target */
1133 long min_value; /**< smallest switch case */
1134 long max_value; /**< largest switch case */
1135 long num_branches; /**< number of jumps */
1136 char *label; /**< label of the jump table */
1137 branch_t *branches; /**< jump array */
1141 * Compare two variables of type branch_t. Used to sort all switch cases
1143 static int ia32_cmp_branch_t(const void *a, const void *b)
1145 branch_t *b1 = (branch_t *)a;
1146 branch_t *b2 = (branch_t *)b;
1148 if (b1->value <= b2->value)
1155 * Emits code for a SwitchJmp (creates a jump table if
1156 * possible otherwise a cmp-jmp cascade). Port from
1159 static void emit_ia32_SwitchJmp(const ir_node *node)
1161 unsigned long interval;
1167 const ir_edge_t *edge;
1169 /* fill the table structure */
1170 tbl.label = XMALLOCN(char, SNPRINTF_BUF_LEN);
1171 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, ".TBL_");
1173 tbl.num_branches = get_irn_n_edges(node) - 1;
1174 tbl.branches = XMALLOCNZ(branch_t, tbl.num_branches);
1175 tbl.min_value = INT_MAX;
1176 tbl.max_value = INT_MIN;
1178 default_pn = get_ia32_condcode(node);
1180 /* go over all proj's and collect them */
1181 foreach_out_edge(node, edge) {
1182 proj = get_edge_src_irn(edge);
1183 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
1185 pnc = get_Proj_proj(proj);
1187 /* check for default proj */
1188 if (pnc == default_pn) {
1189 assert(tbl.defProj == NULL && "found two default Projs at SwitchJmp");
1192 tbl.min_value = pnc < tbl.min_value ? pnc : tbl.min_value;
1193 tbl.max_value = pnc > tbl.max_value ? pnc : tbl.max_value;
1195 /* create branch entry */
1196 tbl.branches[i].target = proj;
1197 tbl.branches[i].value = pnc;
1202 assert(i == tbl.num_branches);
1204 /* sort the branches by their number */
1205 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
1207 /* two-complement's magic make this work without overflow */
1208 interval = tbl.max_value - tbl.min_value;
1210 /* emit the table */
1211 ia32_emitf(node, "\tcmpl $%u, %S0\n", interval);
1212 ia32_emitf(tbl.defProj, "\tja %L\n");
1214 if (tbl.num_branches > 1) {
1216 ia32_emitf(node, "\tjmp *%s(,%S0,4)\n", tbl.label);
1218 be_gas_emit_switch_section(GAS_SECTION_RODATA);
1219 ia32_emitf(NULL, "\t.align 4\n");
1220 ia32_emitf(NULL, "%s:\n", tbl.label);
1222 last_value = tbl.branches[0].value;
1223 for (i = 0; i != tbl.num_branches; ++i) {
1224 while (last_value != tbl.branches[i].value) {
1225 ia32_emitf(tbl.defProj, ".long %L\n");
1228 ia32_emitf(tbl.branches[i].target, ".long %L\n");
1231 be_gas_emit_switch_section(GAS_SECTION_TEXT);
1233 /* one jump is enough */
1234 ia32_emitf(tbl.branches[0].target, "\tjmp %L\n");
1244 * Emits code for a unconditional jump.
1246 static void emit_Jmp(const ir_node *node)
1250 /* for now, the code works for scheduled and non-schedules blocks */
1251 block = get_nodes_block(node);
1253 /* we have a block schedule */
1254 if (can_be_fallthrough(node)) {
1255 ia32_emitf(node, "\t/* fallthrough to %L */\n");
1257 ia32_emitf(node, "\tjmp %L\n");
1262 * Emit an inline assembler operand.
1264 * @param node the ia32_ASM node
1265 * @param s points to the operand (a %c)
1267 * @return pointer to the first char in s NOT in the current operand
1269 static const char* emit_asm_operand(const ir_node *node, const char *s)
1271 const ia32_attr_t *ia32_attr = get_ia32_attr_const(node);
1272 const ia32_asm_attr_t *attr = CONST_CAST_IA32_ATTR(ia32_asm_attr_t,
1274 const arch_register_t *reg;
1275 const ia32_asm_reg_t *asm_regs = attr->register_map;
1276 const ia32_asm_reg_t *asm_reg;
1277 const char *reg_name;
1286 /* parse modifiers */
1289 ir_fprintf(stderr, "Warning: asm text (%+F) ends with %\n", node);
1313 ir_fprintf(stderr, "Warning: asm text (%+F) contains unknown modifier "
1314 "'%c' for asm op\n", node, c);
1320 sscanf(s, "%d%n", &num, &p);
1322 ir_fprintf(stderr, "Warning: Couldn't parse assembler operand (%+F)\n",
1329 if (num < 0 || num >= ARR_LEN(asm_regs)) {
1330 ir_fprintf(stderr, "Error: Custom assembler references invalid "
1331 "input/output (%+F)\n", node);
1334 asm_reg = & asm_regs[num];
1335 assert(asm_reg->valid);
1338 if (asm_reg->use_input == 0) {
1339 reg = get_out_reg(node, asm_reg->inout_pos);
1341 ir_node *pred = get_irn_n(node, asm_reg->inout_pos);
1343 /* might be an immediate value */
1344 if (is_ia32_Immediate(pred)) {
1345 emit_ia32_Immediate(pred);
1348 reg = get_in_reg(node, asm_reg->inout_pos);
1351 ir_fprintf(stderr, "Warning: no register assigned for %d asm op "
1352 "(%+F)\n", num, node);
1356 if (asm_reg->memory) {
1361 if (modifier != 0) {
1365 reg_name = ia32_get_mapped_reg_name(isa->regs_8bit, reg);
1368 reg_name = ia32_get_mapped_reg_name(isa->regs_8bit_high, reg);
1371 reg_name = ia32_get_mapped_reg_name(isa->regs_16bit, reg);
1374 panic("Invalid asm op modifier");
1376 be_emit_string(reg_name);
1378 emit_register(reg, asm_reg->mode);
1381 if (asm_reg->memory) {
1389 * Emits code for an ASM pseudo op.
1391 static void emit_ia32_Asm(const ir_node *node)
1393 const void *gen_attr = get_irn_generic_attr_const(node);
1394 const ia32_asm_attr_t *attr
1395 = CONST_CAST_IA32_ATTR(ia32_asm_attr_t, gen_attr);
1396 ident *asm_text = attr->asm_text;
1397 const char *s = get_id_str(asm_text);
1399 ia32_emitf(node, "#APP\t\n");
1406 s = emit_asm_operand(node, s);
1412 ia32_emitf(NULL, "\n#NO_APP\n");
1415 /**********************************
1418 * | | ___ _ __ _ _| |_) |
1419 * | | / _ \| '_ \| | | | _ <
1420 * | |___| (_) | |_) | |_| | |_) |
1421 * \_____\___/| .__/ \__, |____/
1424 **********************************/
1427 * Emit movsb/w instructions to make mov count divideable by 4
1429 static void emit_CopyB_prolog(unsigned size)
1432 ia32_emitf(NULL, "\tmovsb\n");
1434 ia32_emitf(NULL, "\tmovsw\n");
1438 * Emit rep movsd instruction for memcopy.
1440 static void emit_ia32_CopyB(const ir_node *node)
1442 unsigned size = get_ia32_copyb_size(node);
1444 emit_CopyB_prolog(size);
1445 ia32_emitf(node, "\trep movsd\n");
1449 * Emits unrolled memcopy.
1451 static void emit_ia32_CopyB_i(const ir_node *node)
1453 unsigned size = get_ia32_copyb_size(node);
1455 emit_CopyB_prolog(size);
1459 ia32_emitf(NULL, "\tmovsd\n");
1465 /***************************
1469 * | | / _ \| '_ \ \ / /
1470 * | |___| (_) | | | \ V /
1471 * \_____\___/|_| |_|\_/
1473 ***************************/
1476 * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1478 static void emit_ia32_Conv_with_FP(const ir_node *node)
1480 ir_mode *ls_mode = get_ia32_ls_mode(node);
1481 int ls_bits = get_mode_size_bits(ls_mode);
1484 if (is_ia32_Conv_I2FP(node)) {
1485 if (ls_bits == 32) {
1490 } else if (is_ia32_Conv_FP2I(node)) {
1491 if (ls_bits == 32) {
1497 assert(is_ia32_Conv_FP2FP(node));
1498 if (ls_bits == 32) {
1505 ia32_emitf(node, "\tcvt%s %AS3, %D0\n", conv);
1508 static void emit_ia32_Conv_I2FP(const ir_node *node)
1510 emit_ia32_Conv_with_FP(node);
1513 static void emit_ia32_Conv_FP2I(const ir_node *node)
1515 emit_ia32_Conv_with_FP(node);
1518 static void emit_ia32_Conv_FP2FP(const ir_node *node)
1520 emit_ia32_Conv_with_FP(node);
1524 * Emits code for an Int conversion.
1526 static void emit_ia32_Conv_I2I(const ir_node *node)
1528 ir_mode *smaller_mode = get_ia32_ls_mode(node);
1529 int smaller_bits = get_mode_size_bits(smaller_mode);
1530 int signed_mode = mode_is_signed(smaller_mode);
1532 assert(!mode_is_float(smaller_mode));
1533 assert(smaller_bits == 8 || smaller_bits == 16);
1536 smaller_bits == 16 &&
1537 &ia32_gp_regs[REG_EAX] == get_out_reg(node, 0) &&
1538 &ia32_gp_regs[REG_EAX] == arch_get_irn_register(arch_env, get_irn_n(node, n_ia32_unary_op))) {
1539 /* argument and result are both in EAX and signedness is ok: use the
1540 * smaller cwtl opcode */
1541 ia32_emitf(node, "\tcwtl\n");
1543 const char *sign_suffix = signed_mode ? "s" : "z";
1544 ia32_emitf(node, "\tmov%s%Ml %#AS3, %D0\n", sign_suffix);
1551 static void emit_ia32_Call(const ir_node *node)
1553 /* Special case: Call must not have its immediates prefixed by $, instead
1554 * address mode is prefixed by *. */
1555 ia32_emitf(node, "\tcall %*AS3\n");
1559 /*******************************************
1562 * | |__ ___ _ __ ___ __| | ___ ___
1563 * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1564 * | |_) | __/ | | | (_) | (_| | __/\__ \
1565 * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1567 *******************************************/
1570 * Emits code to increase stack pointer.
1572 static void emit_be_IncSP(const ir_node *node)
1574 int offs = be_get_IncSP_offset(node);
1580 ia32_emitf(node, "\tsubl $%u, %D0\n", offs);
1582 ia32_emitf(node, "\taddl $%u, %D0\n", -offs);
1587 * Emits code for Copy/CopyKeep.
1589 static void Copy_emitter(const ir_node *node, const ir_node *op)
1591 const arch_register_t *in = arch_get_irn_register(arch_env, op);
1592 const arch_register_t *out = arch_get_irn_register(arch_env, node);
1597 if (is_unknown_reg(in))
1599 /* copies of vf nodes aren't real... */
1600 if (arch_register_get_class(in) == &ia32_reg_classes[CLASS_ia32_vfp])
1603 if (get_irn_mode(node) == mode_E) {
1604 ia32_emitf(node, "\tmovsd %R, %R\n", in, out);
1606 ia32_emitf(node, "\tmovl %R, %R\n", in, out);
1610 static void emit_be_Copy(const ir_node *node)
1612 Copy_emitter(node, be_get_Copy_op(node));
1615 static void emit_be_CopyKeep(const ir_node *node)
1617 Copy_emitter(node, be_get_CopyKeep_op(node));
1621 * Emits code for exchange.
1623 static void emit_be_Perm(const ir_node *node)
1625 const arch_register_t *in0, *in1;
1626 const arch_register_class_t *cls0, *cls1;
1628 in0 = arch_get_irn_register(arch_env, get_irn_n(node, 0));
1629 in1 = arch_get_irn_register(arch_env, get_irn_n(node, 1));
1631 cls0 = arch_register_get_class(in0);
1632 cls1 = arch_register_get_class(in1);
1634 assert(cls0 == cls1 && "Register class mismatch at Perm");
1636 if (cls0 == &ia32_reg_classes[CLASS_ia32_gp]) {
1637 ia32_emitf(node, "\txchg %R, %R\n", in1, in0);
1638 } else if (cls0 == &ia32_reg_classes[CLASS_ia32_xmm]) {
1639 ia32_emitf(NULL, "\txorpd %R, %R\n", in1, in0);
1640 ia32_emitf(NULL, "\txorpd %R, %R\n", in0, in1);
1641 ia32_emitf(node, "\txorpd %R, %R\n", in1, in0);
1642 } else if (cls0 == &ia32_reg_classes[CLASS_ia32_vfp]) {
1644 } else if (cls0 == &ia32_reg_classes[CLASS_ia32_st]) {
1647 panic("unexpected register class in be_Perm (%+F)", node);
1652 * Emits code for Constant loading.
1654 static void emit_ia32_Const(const ir_node *node)
1656 ia32_emitf(node, "\tmovl %I, %D0\n");
1660 * Emits code to load the TLS base
1662 static void emit_ia32_LdTls(const ir_node *node)
1664 ia32_emitf(node, "\tmovl %%gs:0, %D0\n");
1667 /* helper function for emit_ia32_Minus64Bit */
1668 static void emit_mov(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1670 ia32_emitf(node, "\tmovl %R, %R\n", src, dst);
1673 /* helper function for emit_ia32_Minus64Bit */
1674 static void emit_neg(const ir_node* node, const arch_register_t *reg)
1676 ia32_emitf(node, "\tnegl %R\n", reg);
1679 /* helper function for emit_ia32_Minus64Bit */
1680 static void emit_sbb0(const ir_node* node, const arch_register_t *reg)
1682 ia32_emitf(node, "\tsbbl $0, %R\n", reg);
1685 /* helper function for emit_ia32_Minus64Bit */
1686 static void emit_sbb(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1688 ia32_emitf(node, "\tsbbl %R, %R\n", src, dst);
1691 /* helper function for emit_ia32_Minus64Bit */
1692 static void emit_xchg(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1694 ia32_emitf(node, "\txchgl %R, %R\n", src, dst);
1697 /* helper function for emit_ia32_Minus64Bit */
1698 static void emit_zero(const ir_node* node, const arch_register_t *reg)
1700 ia32_emitf(node, "\txorl %R, %R\n", reg, reg);
1703 static void emit_ia32_Minus64Bit(const ir_node *node)
1705 const arch_register_t *in_lo = get_in_reg(node, 0);
1706 const arch_register_t *in_hi = get_in_reg(node, 1);
1707 const arch_register_t *out_lo = get_out_reg(node, 0);
1708 const arch_register_t *out_hi = get_out_reg(node, 1);
1710 if (out_lo == in_lo) {
1711 if (out_hi != in_hi) {
1712 /* a -> a, b -> d */
1715 /* a -> a, b -> b */
1718 } else if (out_lo == in_hi) {
1719 if (out_hi == in_lo) {
1720 /* a -> b, b -> a */
1721 emit_xchg(node, in_lo, in_hi);
1724 /* a -> b, b -> d */
1725 emit_mov(node, in_hi, out_hi);
1726 emit_mov(node, in_lo, out_lo);
1730 if (out_hi == in_lo) {
1731 /* a -> c, b -> a */
1732 emit_mov(node, in_lo, out_lo);
1734 } else if (out_hi == in_hi) {
1735 /* a -> c, b -> b */
1736 emit_mov(node, in_lo, out_lo);
1739 /* a -> c, b -> d */
1740 emit_mov(node, in_lo, out_lo);
1746 emit_neg( node, out_hi);
1747 emit_neg( node, out_lo);
1748 emit_sbb0(node, out_hi);
1752 emit_zero(node, out_hi);
1753 emit_neg( node, out_lo);
1754 emit_sbb( node, in_hi, out_hi);
1757 static void emit_ia32_GetEIP(const ir_node *node)
1759 ia32_emitf(node, "\tcall %s\n", pic_base_label);
1760 ia32_emitf(NULL, "%s:\n", pic_base_label);
1761 ia32_emitf(node, "\tpopl %D0\n");
1764 static void emit_be_Return(const ir_node *node)
1766 unsigned pop = be_Return_get_pop(node);
1768 if (pop > 0 || be_Return_get_emit_pop(node)) {
1769 ia32_emitf(node, "\tret $%u\n", pop);
1771 ia32_emitf(node, "\tret\n");
1775 static void emit_Nothing(const ir_node *node)
1781 /***********************************************************************************
1784 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
1785 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1786 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
1787 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
1789 ***********************************************************************************/
1792 * Enters the emitter functions for handled nodes into the generic
1793 * pointer of an opcode.
1795 static void ia32_register_emitters(void)
1797 #define IA32_EMIT2(a,b) op_ia32_##a->ops.generic = (op_func)emit_ia32_##b
1798 #define IA32_EMIT(a) IA32_EMIT2(a,a)
1799 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
1800 #define IGN(a) op_##a->ops.generic = (op_func)emit_Nothing
1801 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
1802 #define BE_IGN(a) op_be_##a->ops.generic = (op_func)emit_Nothing
1804 /* first clear the generic function pointer for all ops */
1805 clear_irp_opcodes_generic_func();
1807 /* register all emitter functions defined in spec */
1808 ia32_register_spec_emitters();
1810 /* other ia32 emitter functions */
1811 IA32_EMIT2(Conv_I2I8Bit, Conv_I2I);
1816 IA32_EMIT(Conv_FP2FP);
1817 IA32_EMIT(Conv_FP2I);
1818 IA32_EMIT(Conv_I2FP);
1819 IA32_EMIT(Conv_I2I);
1826 IA32_EMIT(Minus64Bit);
1827 IA32_EMIT(SwitchJmp);
1829 /* benode emitter */
1852 typedef void (*emit_func_ptr) (const ir_node *);
1855 * Assign and emit an exception label if the current instruction can fail.
1857 static void ia32_assign_exc_label(ir_node *node)
1859 /* assign a new ID to the instruction */
1860 set_ia32_exc_label_id(node, ++exc_label_id);
1862 ia32_emit_exc_label(node);
1864 be_emit_pad_comment();
1865 be_emit_cstring("/* exception to Block ");
1866 ia32_emit_cfop_target(node);
1867 be_emit_cstring(" */\n");
1868 be_emit_write_line();
1872 * Emits code for a node.
1874 static void ia32_emit_node(ir_node *node)
1876 ir_op *op = get_irn_op(node);
1878 DBG((dbg, LEVEL_1, "emitting code for %+F\n", node));
1880 if (is_ia32_irn(node)) {
1881 if (get_ia32_exc_label(node)) {
1882 /* emit the exception label of this instruction */
1883 ia32_assign_exc_label(node);
1885 if (mark_spill_reload) {
1886 if (is_ia32_is_spill(node)) {
1887 ia32_emitf(NULL, "\txchg %ebx, %ebx /* spill mark */\n");
1889 if (is_ia32_is_reload(node)) {
1890 ia32_emitf(NULL, "\txchg %edx, %edx /* reload mark */\n");
1892 if (is_ia32_is_remat(node)) {
1893 ia32_emitf(NULL, "\txchg %ecx, %ecx /* remat mark */\n");
1897 if (op->ops.generic) {
1898 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
1900 be_dbg_set_dbg_info(get_irn_dbg_info(node));
1905 ir_fprintf(stderr, "Error: No emit handler for node %+F (%+G, graph %+F)\n", node, node, current_ir_graph);
1911 * Emits gas alignment directives
1913 static void ia32_emit_alignment(unsigned align, unsigned skip)
1915 ia32_emitf(NULL, "\t.p2align %u,,%u\n", align, skip);
1919 * Emits gas alignment directives for Labels depended on cpu architecture.
1921 static void ia32_emit_align_label(void)
1923 unsigned align = ia32_cg_config.label_alignment;
1924 unsigned maximum_skip = ia32_cg_config.label_alignment_max_skip;
1925 ia32_emit_alignment(align, maximum_skip);
1929 * Test whether a block should be aligned.
1930 * For cpus in the P4/Athlon class it is useful to align jump labels to
1931 * 16 bytes. However we should only do that if the alignment nops before the
1932 * label aren't executed more often than we have jumps to the label.
1934 static int should_align_block(const ir_node *block)
1936 static const double DELTA = .0001;
1937 ir_exec_freq *exec_freq = cg->birg->exec_freq;
1938 ir_node *prev = get_prev_block_sched(block);
1940 double prev_freq = 0; /**< execfreq of the fallthrough block */
1941 double jmp_freq = 0; /**< execfreq of all non-fallthrough blocks */
1944 if (exec_freq == NULL)
1946 if (ia32_cg_config.label_alignment_factor <= 0)
1949 block_freq = get_block_execfreq(exec_freq, block);
1950 if (block_freq < DELTA)
1953 n_cfgpreds = get_Block_n_cfgpreds(block);
1954 for(i = 0; i < n_cfgpreds; ++i) {
1955 const ir_node *pred = get_Block_cfgpred_block(block, i);
1956 double pred_freq = get_block_execfreq(exec_freq, pred);
1959 prev_freq += pred_freq;
1961 jmp_freq += pred_freq;
1965 if (prev_freq < DELTA && !(jmp_freq < DELTA))
1968 jmp_freq /= prev_freq;
1970 return jmp_freq > ia32_cg_config.label_alignment_factor;
1974 * Emit the block header for a block.
1976 * @param block the block
1977 * @param prev_block the previous block
1979 static void ia32_emit_block_header(ir_node *block)
1981 ir_graph *irg = current_ir_graph;
1982 int need_label = block_needs_label(block);
1984 ir_exec_freq *exec_freq = cg->birg->exec_freq;
1986 if (block == get_irg_end_block(irg) || block == get_irg_start_block(irg))
1989 if (ia32_cg_config.label_alignment > 0) {
1990 /* align the current block if:
1991 * a) if should be aligned due to its execution frequency
1992 * b) there is no fall-through here
1994 if (should_align_block(block)) {
1995 ia32_emit_align_label();
1997 /* if the predecessor block has no fall-through,
1998 we can always align the label. */
2000 int has_fallthrough = 0;
2002 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
2003 ir_node *cfg_pred = get_Block_cfgpred(block, i);
2004 if (can_be_fallthrough(cfg_pred)) {
2005 has_fallthrough = 1;
2010 if (!has_fallthrough)
2011 ia32_emit_align_label();
2015 if (need_label || has_Block_label(block)) {
2016 ia32_emit_block_name(block);
2019 be_emit_pad_comment();
2020 be_emit_cstring(" /* ");
2022 be_emit_cstring("\t/* ");
2023 ia32_emit_block_name(block);
2024 be_emit_cstring(": ");
2027 be_emit_cstring("preds:");
2029 /* emit list of pred blocks in comment */
2030 arity = get_irn_arity(block);
2031 for (i = 0; i < arity; ++i) {
2032 ir_node *predblock = get_Block_cfgpred_block(block, i);
2033 be_emit_irprintf(" %d", get_irn_node_nr(predblock));
2035 if (exec_freq != NULL) {
2036 be_emit_irprintf(" freq: %f",
2037 get_block_execfreq(exec_freq, block));
2039 be_emit_cstring(" */\n");
2040 be_emit_write_line();
2044 * Walks over the nodes in a block connected by scheduling edges
2045 * and emits code for each node.
2047 static void ia32_gen_block(ir_node *block)
2051 ia32_emit_block_header(block);
2053 /* emit the contents of the block */
2054 be_dbg_set_dbg_info(get_irn_dbg_info(block));
2055 sched_foreach(block, node) {
2056 ia32_emit_node(node);
2060 typedef struct exc_entry {
2061 ir_node *exc_instr; /** The instruction that can issue an exception. */
2062 ir_node *block; /** The block to call then. */
2067 * Sets labels for control flow nodes (jump target).
2068 * Links control predecessors to there destination blocks.
2070 static void ia32_gen_labels(ir_node *block, void *data)
2072 exc_entry **exc_list = data;
2076 for (n = get_Block_n_cfgpreds(block) - 1; n >= 0; --n) {
2077 pred = get_Block_cfgpred(block, n);
2078 set_irn_link(pred, block);
2080 pred = skip_Proj(pred);
2081 if (is_ia32_irn(pred) && get_ia32_exc_label(pred)) {
2086 ARR_APP1(exc_entry, *exc_list, e);
2087 set_irn_link(pred, block);
2093 * Compare two exception_entries.
2095 static int cmp_exc_entry(const void *a, const void *b)
2097 const exc_entry *ea = a;
2098 const exc_entry *eb = b;
2100 if (get_ia32_exc_label_id(ea->exc_instr) < get_ia32_exc_label_id(eb->exc_instr))
2106 * Main driver. Emits the code for one routine.
2108 void ia32_gen_routine(ia32_code_gen_t *ia32_cg, ir_graph *irg)
2110 ir_entity *entity = get_irg_entity(irg);
2111 exc_entry *exc_list = NEW_ARR_F(exc_entry, 0);
2115 isa = (const ia32_isa_t*) cg->arch_env;
2116 arch_env = cg->arch_env;
2117 do_pic = cg->birg->main_env->options->pic;
2119 ia32_register_emitters();
2121 get_unique_label(pic_base_label, sizeof(pic_base_label), ".PIC_BASE");
2123 be_dbg_method_begin(entity, be_abi_get_stack_layout(cg->birg->abi));
2124 be_gas_emit_function_prolog(entity, ia32_cg_config.function_alignment);
2126 /* we use links to point to target blocks */
2127 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
2128 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &exc_list);
2130 /* initialize next block links */
2131 n = ARR_LEN(cg->blk_sched);
2132 for (i = 0; i < n; ++i) {
2133 ir_node *block = cg->blk_sched[i];
2134 ir_node *prev = i > 0 ? cg->blk_sched[i-1] : NULL;
2136 set_irn_link(block, prev);
2139 for (i = 0; i < n; ++i) {
2140 ir_node *block = cg->blk_sched[i];
2142 ia32_gen_block(block);
2145 be_gas_emit_function_epilog(entity);
2146 be_dbg_method_end();
2148 be_emit_write_line();
2150 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
2152 /* Sort the exception table using the exception label id's.
2153 Those are ascending with ascending addresses. */
2154 qsort(exc_list, ARR_LEN(exc_list), sizeof(exc_list[0]), cmp_exc_entry);
2158 for (i = 0; i < ARR_LEN(exc_list); ++i) {
2159 be_emit_cstring("\t.long ");
2160 ia32_emit_exc_label(exc_list[i].exc_instr);
2162 be_emit_cstring("\t.long ");
2163 ia32_emit_block_name(exc_list[i].block);
2167 DEL_ARR_F(exc_list);
2170 static const lc_opt_table_entry_t ia32_emitter_options[] = {
2171 LC_OPT_ENT_BOOL("mark_spill_reload", "mark spills and reloads with ud opcodes", &mark_spill_reload),
2175 void ia32_init_emitter(void)
2177 lc_opt_entry_t *be_grp;
2178 lc_opt_entry_t *ia32_grp;
2180 be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
2181 ia32_grp = lc_opt_get_grp(be_grp, "ia32");
2183 lc_opt_add_table(ia32_grp, ia32_emitter_options);
2185 FIRM_DBG_REGISTER(dbg, "firm.be.ia32.emitter");