2 * Copyright (C) 1995-2007 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"
46 #include "../besched_t.h"
47 #include "../benode_t.h"
49 #include "../be_dbgout.h"
50 #include "../beemitter.h"
51 #include "../begnuas.h"
52 #include "../beirg_t.h"
54 #include "ia32_emitter.h"
55 #include "gen_ia32_emitter.h"
56 #include "gen_ia32_regalloc_if.h"
57 #include "ia32_nodes_attr.h"
58 #include "ia32_new_nodes.h"
59 #include "ia32_map_regs.h"
60 #include "bearch_ia32_t.h"
62 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
64 #define BLOCK_PREFIX ".L"
66 #define SNPRINTF_BUF_LEN 128
68 static const arch_env_t *arch_env;
69 static const ia32_isa_t *isa;
70 static ia32_code_gen_t *cg;
73 * Returns the register at in position pos.
75 static const arch_register_t *get_in_reg(const ir_node *irn, int pos)
78 const arch_register_t *reg = NULL;
80 assert(get_irn_arity(irn) > pos && "Invalid IN position");
82 /* The out register of the operator at position pos is the
83 in register we need. */
84 op = get_irn_n(irn, pos);
86 reg = arch_get_irn_register(arch_env, op);
88 assert(reg && "no in register found");
90 if(reg == &ia32_gp_regs[REG_GP_NOREG])
91 panic("trying to emit noreg for %+F input %d", irn, pos);
93 /* in case of unknown register: just return a valid register */
94 if (reg == &ia32_gp_regs[REG_GP_UKNWN]) {
95 const arch_register_req_t *req;
97 /* ask for the requirements */
98 req = arch_get_register_req(arch_env, irn, pos);
100 if (arch_register_req_is(req, limited)) {
101 /* in case of limited requirements: get the first allowed register */
102 unsigned idx = rbitset_next(req->limited, 0, 1);
103 reg = arch_register_for_index(req->cls, idx);
105 /* otherwise get first register in class */
106 reg = arch_register_for_index(req->cls, 0);
114 * Returns the register at out position pos.
116 static const arch_register_t *get_out_reg(const ir_node *irn, int pos)
119 const arch_register_t *reg = NULL;
121 /* 1st case: irn is not of mode_T, so it has only */
122 /* one OUT register -> good */
123 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
124 /* Proj with the corresponding projnum for the register */
126 if (get_irn_mode(irn) != mode_T) {
128 reg = arch_get_irn_register(arch_env, irn);
129 } else if (is_ia32_irn(irn)) {
130 reg = get_ia32_out_reg(irn, pos);
132 const ir_edge_t *edge;
134 foreach_out_edge(irn, edge) {
135 proj = get_edge_src_irn(edge);
136 assert(is_Proj(proj) && "non-Proj from mode_T node");
137 if (get_Proj_proj(proj) == pos) {
138 reg = arch_get_irn_register(arch_env, proj);
144 assert(reg && "no out register found");
149 * Add a number to a prefix. This number will not be used a second time.
151 static char *get_unique_label(char *buf, size_t buflen, const char *prefix)
153 static unsigned long id = 0;
154 snprintf(buf, buflen, "%s%lu", prefix, ++id);
158 /*************************************************************
160 * (_) | | / _| | | | |
161 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
162 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
163 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
164 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
167 *************************************************************/
169 static void emit_8bit_register(const arch_register_t *reg)
171 const char *reg_name = arch_register_get_name(reg);
174 be_emit_char(reg_name[1]);
178 static void emit_16bit_register(const arch_register_t *reg)
180 const char *reg_name = ia32_get_mapped_reg_name(isa->regs_16bit, reg);
183 be_emit_string(reg_name);
186 static void emit_register(const arch_register_t *reg, const ir_mode *mode)
188 const char *reg_name;
191 int size = get_mode_size_bits(mode);
193 emit_8bit_register(reg);
195 } else if(size == 16) {
196 emit_16bit_register(reg);
203 reg_name = arch_register_get_name(reg);
206 be_emit_string(reg_name);
209 void ia32_emit_source_register(const ir_node *node, int pos)
211 const arch_register_t *reg = get_in_reg(node, pos);
213 emit_register(reg, NULL);
216 static void emit_ia32_Immediate(const ir_node *node);
218 void ia32_emit_8bit_source_register_or_immediate(const ir_node *node, int pos)
220 const arch_register_t *reg;
221 ir_node *in = get_irn_n(node, pos);
222 if(is_ia32_Immediate(in)) {
223 emit_ia32_Immediate(in);
227 reg = get_in_reg(node, pos);
228 emit_8bit_register(reg);
231 void ia32_emit_dest_register(const ir_node *node, int pos)
233 const arch_register_t *reg = get_out_reg(node, pos);
235 emit_register(reg, NULL);
238 void ia32_emit_8bit_dest_register(const ir_node *node, int pos)
240 const arch_register_t *reg = get_out_reg(node, pos);
242 emit_register(reg, mode_Bu);
245 void ia32_emit_x87_register(const ir_node *node, int pos)
247 const ia32_x87_attr_t *attr = get_ia32_x87_attr_const(node);
251 be_emit_string(attr->x87[pos]->name);
254 static void ia32_emit_mode_suffix_mode(const ir_mode *mode)
256 if(mode_is_float(mode)) {
257 switch(get_mode_size_bits(mode)) {
258 case 32: be_emit_char('s'); return;
259 case 64: be_emit_char('l'); return;
260 case 80: be_emit_char('t'); return;
263 assert(mode_is_int(mode) || mode_is_reference(mode));
264 switch(get_mode_size_bits(mode)) {
265 case 64: be_emit_cstring("ll"); return;
266 /* gas docu says q is the suffix but gcc, objdump and icc use
268 case 32: be_emit_char('l'); return;
269 case 16: be_emit_char('w'); return;
270 case 8: be_emit_char('b'); return;
273 panic("Can't output mode_suffix for %+F\n", mode);
276 void ia32_emit_mode_suffix(const ir_node *node)
278 ir_mode *mode = get_ia32_ls_mode(node);
282 ia32_emit_mode_suffix_mode(mode);
285 void ia32_emit_x87_mode_suffix(const ir_node *node)
287 ir_mode *mode = get_ia32_ls_mode(node);
289 ia32_emit_mode_suffix_mode(mode);
293 char get_xmm_mode_suffix(ir_mode *mode)
295 assert(mode_is_float(mode));
296 switch(get_mode_size_bits(mode)) {
307 void ia32_emit_xmm_mode_suffix(const ir_node *node)
309 ir_mode *mode = get_ia32_ls_mode(node);
310 assert(mode != NULL);
312 be_emit_char(get_xmm_mode_suffix(mode));
315 void ia32_emit_xmm_mode_suffix_s(const ir_node *node)
317 ir_mode *mode = get_ia32_ls_mode(node);
318 assert(mode != NULL);
319 be_emit_char(get_xmm_mode_suffix(mode));
322 void ia32_emit_extend_suffix(const ir_mode *mode)
324 if(get_mode_size_bits(mode) == 32)
326 if(mode_is_signed(mode)) {
334 void ia32_emit_function_object(const char *name)
336 switch (be_gas_flavour) {
337 case GAS_FLAVOUR_NORMAL:
338 be_emit_cstring("\t.type\t");
339 be_emit_string(name);
340 be_emit_cstring(", @function\n");
341 be_emit_write_line();
343 case GAS_FLAVOUR_MINGW:
344 be_emit_cstring("\t.def\t");
345 be_emit_string(name);
346 be_emit_cstring(";\t.scl\t2;\t.type\t32;\t.endef\n");
347 be_emit_write_line();
355 void ia32_emit_function_size(const char *name)
357 switch (be_gas_flavour) {
358 case GAS_FLAVOUR_NORMAL:
359 be_emit_cstring("\t.size\t");
360 be_emit_string(name);
361 be_emit_cstring(", .-");
362 be_emit_string(name);
364 be_emit_write_line();
372 void ia32_emit_source_register_or_immediate(const ir_node *node, int pos)
374 ir_node *in = get_irn_n(node, pos);
375 if(is_ia32_Immediate(in)) {
376 emit_ia32_Immediate(in);
378 const ir_mode *mode = get_ia32_ls_mode(node);
379 const arch_register_t *reg = get_in_reg(node, pos);
380 emit_register(reg, mode);
385 * Emits registers and/or address mode of a binary operation.
387 void ia32_emit_binop(const ir_node *node) {
388 const ir_node *right_op = get_irn_n(node, n_ia32_binary_right);
389 const ir_mode *mode = get_ia32_ls_mode(node);
390 const arch_register_t *reg_left;
392 switch(get_ia32_op_type(node)) {
394 reg_left = get_in_reg(node, n_ia32_binary_left);
395 if(is_ia32_Immediate(right_op)) {
396 emit_ia32_Immediate(right_op);
397 be_emit_cstring(", ");
398 emit_register(reg_left, mode);
401 const arch_register_t *reg_right
402 = get_in_reg(node, n_ia32_binary_right);
403 emit_register(reg_right, mode);
404 be_emit_cstring(", ");
405 emit_register(reg_left, mode);
409 if(is_ia32_Immediate(right_op)) {
410 emit_ia32_Immediate(right_op);
411 be_emit_cstring(", ");
414 reg_left = get_in_reg(node, n_ia32_binary_left);
416 be_emit_cstring(", ");
417 emit_register(reg_left, mode);
421 panic("DestMode can't be output by %%binop anymore");
424 assert(0 && "unsupported op type");
429 * Emits registers and/or address mode of a binary operation.
431 void ia32_emit_x87_binop(const ir_node *node) {
432 switch(get_ia32_op_type(node)) {
435 const ia32_x87_attr_t *x87_attr = get_ia32_x87_attr_const(node);
436 const arch_register_t *in1 = x87_attr->x87[0];
437 const arch_register_t *in2 = x87_attr->x87[1];
438 const arch_register_t *out = x87_attr->x87[2];
439 const arch_register_t *in;
441 in = out ? ((out == in2) ? in1 : in2) : in2;
442 out = out ? out : in1;
445 be_emit_string(arch_register_get_name(in));
446 be_emit_cstring(", %");
447 be_emit_string(arch_register_get_name(out));
455 assert(0 && "unsupported op type");
459 void ia32_emit_am_or_dest_register(const ir_node *node,
461 if(get_ia32_op_type(node) == ia32_Normal) {
462 ia32_emit_dest_register(node, pos);
464 assert(get_ia32_op_type(node) == ia32_AddrModeD);
470 * Emits registers and/or address mode of a unary operation.
472 void ia32_emit_unop(const ir_node *node, int pos) {
475 switch(get_ia32_op_type(node)) {
477 op = get_irn_n(node, pos);
478 if (is_ia32_Immediate(op)) {
479 emit_ia32_Immediate(op);
481 ia32_emit_source_register(node, pos);
489 assert(0 && "unsupported op type");
494 * Emits address mode.
496 void ia32_emit_am(const ir_node *node) {
497 ir_entity *ent = get_ia32_am_sc(node);
498 int offs = get_ia32_am_offs_int(node);
499 ir_node *base = get_irn_n(node, 0);
500 int has_base = !is_ia32_NoReg_GP(base);
501 ir_node *index = get_irn_n(node, 1);
502 int has_index = !is_ia32_NoReg_GP(index);
504 /* just to be sure... */
505 assert(!is_ia32_use_frame(node) || get_ia32_frame_ent(node) != NULL);
511 set_entity_backend_marked(ent, 1);
512 id = get_entity_ld_ident(ent);
513 if (is_ia32_am_sc_sign(node))
517 if(get_entity_owner(ent) == get_tls_type()) {
518 if (get_entity_visibility(ent) == visibility_external_allocated) {
519 be_emit_cstring("@INDNTPOFF");
521 be_emit_cstring("@NTPOFF");
528 be_emit_irprintf("%+d", offs);
530 be_emit_irprintf("%d", offs);
534 if (has_base || has_index) {
539 const arch_register_t *reg = get_in_reg(node, n_ia32_base);
540 emit_register(reg, NULL);
543 /* emit index + scale */
545 const arch_register_t *reg = get_in_reg(node, n_ia32_index);
548 emit_register(reg, NULL);
550 scale = get_ia32_am_scale(node);
552 be_emit_irprintf(",%d", 1 << get_ia32_am_scale(node));
558 /* special case if nothing is set */
559 if(ent == NULL && offs == 0 && !has_base && !has_index) {
564 /*************************************************
567 * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
568 * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
569 * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
570 * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
572 *************************************************/
575 #define IA32_DO_EMIT(irn) ia32_fprintf_format(F, irn, cmd_buf, cmnt_buf)
578 * coding of conditions
580 struct cmp2conditon_t {
586 * positive conditions for signed compares
588 static const struct cmp2conditon_t cmp2condition_s[] = {
589 { NULL, pn_Cmp_False }, /* always false */
590 { "e", pn_Cmp_Eq }, /* == */
591 { "l", pn_Cmp_Lt }, /* < */
592 { "le", pn_Cmp_Le }, /* <= */
593 { "g", pn_Cmp_Gt }, /* > */
594 { "ge", pn_Cmp_Ge }, /* >= */
595 { "ne", pn_Cmp_Lg }, /* != */
596 { NULL, pn_Cmp_Leg}, /* always true */
600 * positive conditions for unsigned compares
602 static const struct cmp2conditon_t cmp2condition_u[] = {
603 { NULL, pn_Cmp_False }, /* always false */
604 { "e", pn_Cmp_Eq }, /* == */
605 { "b", pn_Cmp_Lt }, /* < */
606 { "be", pn_Cmp_Le }, /* <= */
607 { "a", pn_Cmp_Gt }, /* > */
608 { "ae", pn_Cmp_Ge }, /* >= */
609 { "ne", pn_Cmp_Lg }, /* != */
610 { NULL, pn_Cmp_Leg }, /* always true */
614 ia32_pn_Cmp_unsigned = 0x1000,
615 ia32_pn_Cmp_float = 0x2000,
619 * walks up a tree of copies/perms/spills/reloads to find the original value
620 * that is moved around
622 static ir_node *find_original_value(ir_node *node)
624 inc_irg_visited(current_ir_graph);
626 mark_irn_visited(node);
627 if(be_is_Copy(node)) {
628 node = be_get_Copy_op(node);
629 } else if(be_is_CopyKeep(node)) {
630 node = be_get_CopyKeep_op(node);
631 } else if(is_Proj(node)) {
632 ir_node *pred = get_Proj_pred(node);
633 if(be_is_Perm(pred)) {
634 node = get_irn_n(pred, get_Proj_proj(node));
635 } else if(be_is_MemPerm(pred)) {
636 node = get_irn_n(pred, get_Proj_proj(node) + 1);
637 } else if(is_ia32_Load(pred)) {
638 node = get_irn_n(pred, n_ia32_Load_mem);
642 } else if(is_ia32_Store(node)) {
643 node = get_irn_n(node, n_ia32_Store_val);
644 } else if(is_Phi(node)) {
646 arity = get_irn_arity(node);
647 for(i = 0; i < arity; ++i) {
648 ir_node *in = get_irn_n(node, i);
661 static int determine_final_pnc(const ir_node *node, int flags_pos,
664 ir_node *flags = get_irn_n(node, flags_pos);
665 const ia32_attr_t *flags_attr;
666 flags = skip_Proj(flags);
668 if(is_ia32_Sahf(flags)) {
669 ir_node *cmp = get_irn_n(flags, n_ia32_Sahf_val);
670 if(!(is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
671 || is_ia32_FucomppFnstsw(cmp) || is_ia32_FtstFnstsw(cmp))) {
672 cmp = find_original_value(cmp);
673 assert(is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
674 || is_ia32_FucomppFnstsw(cmp) || is_ia32_FtstFnstsw(cmp));
677 flags_attr = get_ia32_attr_const(cmp);
678 if(flags_attr->data.cmp_flipped)
679 pnc = get_mirrored_pnc(pnc);
680 pnc |= ia32_pn_Cmp_float;
681 } else if(is_ia32_Ucomi(flags) || is_ia32_Fucomi(flags)
682 || is_ia32_Fucompi(flags)) {
683 flags_attr = get_ia32_attr_const(flags);
685 if(flags_attr->data.cmp_flipped)
686 pnc = get_mirrored_pnc(pnc);
687 pnc |= ia32_pn_Cmp_float;
689 assert(is_ia32_Cmp(flags) || is_ia32_Test(flags)
690 || is_ia32_Cmp8Bit(flags) || is_ia32_Test8Bit(flags));
691 flags_attr = get_ia32_attr_const(flags);
693 if(flags_attr->data.cmp_flipped)
694 pnc = get_mirrored_pnc(pnc);
695 if(flags_attr->data.cmp_unsigned)
696 pnc |= ia32_pn_Cmp_unsigned;
702 static void ia32_emit_cmp_suffix(int pnc)
706 if((pnc & ia32_pn_Cmp_float) || (pnc & ia32_pn_Cmp_unsigned)) {
708 assert(cmp2condition_u[pnc].num == pnc);
709 str = cmp2condition_u[pnc].name;
712 assert(cmp2condition_s[pnc].num == pnc);
713 str = cmp2condition_s[pnc].name;
719 void ia32_emit_cmp_suffix_node(const ir_node *node,
722 pn_Cmp pnc = get_ia32_pncode(node);
724 pnc = determine_final_pnc(node, flags_pos, pnc);
725 ia32_emit_cmp_suffix(pnc);
729 * Returns the target block for a control flow node.
732 ir_node *get_cfop_target_block(const ir_node *irn) {
733 return get_irn_link(irn);
737 * Emits a block label for the given block.
740 void ia32_emit_block_name(const ir_node *block)
742 if (has_Block_label(block)) {
743 be_emit_string(be_gas_label_prefix());
744 be_emit_irprintf("%u", (unsigned)get_Block_label(block));
746 be_emit_cstring(BLOCK_PREFIX);
747 be_emit_irprintf("%d", get_irn_node_nr(block));
752 * Emits the target label for a control flow node.
754 static void ia32_emit_cfop_target(const ir_node *node)
756 ir_node *block = get_cfop_target_block(node);
758 ia32_emit_block_name(block);
761 /** Return the next block in Block schedule */
762 static ir_node *next_blk_sched(const ir_node *block)
764 return get_irn_link(block);
768 * Returns the Proj with projection number proj and NOT mode_M
770 static ir_node *get_proj(const ir_node *node, long proj) {
771 const ir_edge_t *edge;
774 assert(get_irn_mode(node) == mode_T && "expected mode_T node");
776 foreach_out_edge(node, edge) {
777 src = get_edge_src_irn(edge);
779 assert(is_Proj(src) && "Proj expected");
780 if (get_irn_mode(src) == mode_M)
783 if (get_Proj_proj(src) == proj)
790 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
792 static void emit_ia32_Jcc(const ir_node *node)
794 const ir_node *proj_true;
795 const ir_node *proj_false;
796 const ir_node *block;
797 const ir_node *next_block;
798 pn_Cmp pnc = get_ia32_pncode(node);
800 pnc = determine_final_pnc(node, 0, pnc);
803 proj_true = get_proj(node, pn_ia32_Jcc_true);
804 assert(proj_true && "Jcc without true Proj");
806 proj_false = get_proj(node, pn_ia32_Jcc_false);
807 assert(proj_false && "Jcc without false Proj");
809 block = get_nodes_block(node);
810 next_block = next_blk_sched(block);
812 if (get_cfop_target_block(proj_true) == next_block) {
813 /* exchange both proj's so the second one can be omitted */
814 const ir_node *t = proj_true;
816 proj_true = proj_false;
818 if(pnc & ia32_pn_Cmp_float) {
819 pnc = get_negated_pnc(pnc, mode_F);
821 pnc = get_negated_pnc(pnc, mode_Iu);
825 if (pnc & ia32_pn_Cmp_float) {
826 /* Some floating point comparisons require a test of the parity flag,
827 * which indicates that the result is unordered */
830 be_emit_cstring("\tjp ");
831 ia32_emit_cfop_target(proj_true);
832 be_emit_finish_line_gas(proj_true);
836 be_emit_cstring("\tjnp ");
837 ia32_emit_cfop_target(proj_true);
838 be_emit_finish_line_gas(proj_true);
844 be_emit_cstring("\tjp ");
845 ia32_emit_cfop_target(proj_false);
846 be_emit_finish_line_gas(proj_false);
852 be_emit_cstring("\tjp ");
853 ia32_emit_cfop_target(proj_true);
854 be_emit_finish_line_gas(proj_true);
862 be_emit_cstring("\tj");
863 ia32_emit_cmp_suffix(pnc);
865 ia32_emit_cfop_target(proj_true);
866 be_emit_finish_line_gas(proj_true);
869 /* the second Proj might be a fallthrough */
870 if (get_cfop_target_block(proj_false) != next_block) {
871 be_emit_cstring("\tjmp ");
872 ia32_emit_cfop_target(proj_false);
873 be_emit_finish_line_gas(proj_false);
875 be_emit_cstring("\t/* fallthrough to ");
876 ia32_emit_cfop_target(proj_false);
877 be_emit_cstring(" */");
878 be_emit_finish_line_gas(proj_false);
882 static void emit_ia32_CMov(const ir_node *node)
884 const arch_register_t *out = arch_get_irn_register(arch_env, node);
885 const arch_register_t *in_true;
886 const arch_register_t *in_false;
887 pn_Cmp pnc = get_ia32_pncode(node);
889 pnc = determine_final_pnc(node, n_ia32_CMov_eflags, pnc);
891 in_true = arch_get_irn_register(arch_env,
892 get_irn_n(node, n_ia32_CMov_val_true));
893 in_false = arch_get_irn_register(arch_env,
894 get_irn_n(node, n_ia32_CMov_val_false));
896 /* should be same constraint fullfilled? */
897 if(out == in_false) {
898 /* yes -> nothing to do */
899 } else if(out == in_true) {
900 const arch_register_t *tmp;
902 /* swap left/right and negate pnc */
903 pnc = get_negated_pnc(pnc, mode_Iu);
910 be_emit_cstring("\tmovl ");
911 emit_register(in_false, NULL);
912 be_emit_cstring(", ");
913 emit_register(out, NULL);
914 be_emit_finish_line_gas(node);
917 be_emit_cstring("\tcmov");
918 ia32_emit_cmp_suffix(pnc);
920 if(get_ia32_op_type(node) == ia32_AddrModeS) {
923 emit_register(in_true, get_ia32_ls_mode(node));
925 be_emit_cstring(", ");
926 emit_register(out, get_ia32_ls_mode(node));
927 be_emit_finish_line_gas(node);
930 /*********************************************************
933 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
934 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
935 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
936 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
939 *********************************************************/
941 /* jump table entry (target and corresponding number) */
942 typedef struct _branch_t {
947 /* jump table for switch generation */
948 typedef struct _jmp_tbl_t {
949 ir_node *defProj; /**< default target */
950 long min_value; /**< smallest switch case */
951 long max_value; /**< largest switch case */
952 long num_branches; /**< number of jumps */
953 char *label; /**< label of the jump table */
954 branch_t *branches; /**< jump array */
958 * Compare two variables of type branch_t. Used to sort all switch cases
961 int ia32_cmp_branch_t(const void *a, const void *b) {
962 branch_t *b1 = (branch_t *)a;
963 branch_t *b2 = (branch_t *)b;
965 if (b1->value <= b2->value)
972 * Emits code for a SwitchJmp (creates a jump table if
973 * possible otherwise a cmp-jmp cascade). Port from
977 void emit_ia32_SwitchJmp(const ir_node *node) {
978 unsigned long interval;
983 const ir_edge_t *edge;
985 /* fill the table structure */
986 tbl.label = xmalloc(SNPRINTF_BUF_LEN);
987 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, ".TBL_");
989 tbl.num_branches = get_irn_n_edges(node);
990 tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
991 tbl.min_value = INT_MAX;
992 tbl.max_value = INT_MIN;
995 /* go over all proj's and collect them */
996 foreach_out_edge(node, edge) {
997 proj = get_edge_src_irn(edge);
998 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
1000 pnc = get_Proj_proj(proj);
1002 /* create branch entry */
1003 tbl.branches[i].target = proj;
1004 tbl.branches[i].value = pnc;
1006 tbl.min_value = pnc < tbl.min_value ? pnc : tbl.min_value;
1007 tbl.max_value = pnc > tbl.max_value ? pnc : tbl.max_value;
1009 /* check for default proj */
1010 if (pnc == get_ia32_pncode(node)) {
1011 assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
1018 /* sort the branches by their number */
1019 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
1021 /* two-complement's magic make this work without overflow */
1022 interval = tbl.max_value - tbl.min_value;
1024 /* emit the table */
1025 be_emit_cstring("\tcmpl $");
1026 be_emit_irprintf("%u, ", interval);
1027 ia32_emit_source_register(node, 0);
1028 be_emit_finish_line_gas(node);
1030 be_emit_cstring("\tja ");
1031 ia32_emit_cfop_target(tbl.defProj);
1032 be_emit_finish_line_gas(node);
1034 if (tbl.num_branches > 1) {
1036 be_emit_cstring("\tjmp *");
1037 be_emit_string(tbl.label);
1038 be_emit_cstring("(,");
1039 ia32_emit_source_register(node, 0);
1040 be_emit_cstring(",4)");
1041 be_emit_finish_line_gas(node);
1043 be_gas_emit_switch_section(GAS_SECTION_RODATA);
1044 be_emit_cstring("\t.align 4\n");
1045 be_emit_write_line();
1047 be_emit_string(tbl.label);
1048 be_emit_cstring(":\n");
1049 be_emit_write_line();
1051 be_emit_cstring(".long ");
1052 ia32_emit_cfop_target(tbl.branches[0].target);
1053 be_emit_finish_line_gas(NULL);
1055 last_value = tbl.branches[0].value;
1056 for (i = 1; i < tbl.num_branches; ++i) {
1057 while (++last_value < tbl.branches[i].value) {
1058 be_emit_cstring(".long ");
1059 ia32_emit_cfop_target(tbl.defProj);
1060 be_emit_finish_line_gas(NULL);
1062 be_emit_cstring(".long ");
1063 ia32_emit_cfop_target(tbl.branches[i].target);
1064 be_emit_finish_line_gas(NULL);
1066 be_gas_emit_switch_section(GAS_SECTION_TEXT);
1068 /* one jump is enough */
1069 be_emit_cstring("\tjmp ");
1070 ia32_emit_cfop_target(tbl.branches[0].target);
1071 be_emit_finish_line_gas(node);
1081 * Emits code for a unconditional jump.
1083 static void emit_Jmp(const ir_node *node)
1085 ir_node *block, *next_block;
1087 /* for now, the code works for scheduled and non-schedules blocks */
1088 block = get_nodes_block(node);
1090 /* we have a block schedule */
1091 next_block = next_blk_sched(block);
1092 if (get_cfop_target_block(node) != next_block) {
1093 be_emit_cstring("\tjmp ");
1094 ia32_emit_cfop_target(node);
1096 be_emit_cstring("\t/* fallthrough to ");
1097 ia32_emit_cfop_target(node);
1098 be_emit_cstring(" */");
1100 be_emit_finish_line_gas(node);
1103 static void emit_ia32_Immediate(const ir_node *node)
1105 const ia32_immediate_attr_t *attr = get_ia32_immediate_attr_const(node);
1108 if(attr->symconst != NULL) {
1109 ident *id = get_entity_ld_ident(attr->symconst);
1111 if(attr->attr.data.am_sc_sign)
1115 if(attr->symconst == NULL || attr->offset != 0) {
1116 if(attr->symconst != NULL) {
1117 be_emit_irprintf("%+d", attr->offset);
1119 be_emit_irprintf("0x%X", attr->offset);
1124 static const char* emit_asm_operand(const ir_node *node, const char *s)
1126 const arch_register_t *reg;
1127 const char *reg_name;
1131 const ia32_attr_t *attr;
1138 /* parse modifiers */
1141 ir_fprintf(stderr, "Warning: asm text (%+F) ends with %\n", node);
1165 ir_fprintf(stderr, "Warning: asm text (%+F) contains unknown modifier "
1166 "'%c' for asm op\n", node, c);
1172 sscanf(s, "%d%n", &num, &p);
1174 ir_fprintf(stderr, "Warning: Couldn't parse assembler operand (%+F)\n",
1182 attr = get_ia32_attr_const(node);
1183 n_outs = ARR_LEN(attr->slots);
1185 reg = get_out_reg(node, num);
1188 int in = num - n_outs;
1189 if(in >= get_irn_arity(node)) {
1190 ir_fprintf(stderr, "Warning: Invalid input %d specified in asm "
1191 "op (%+F)\n", num, node);
1194 pred = get_irn_n(node, in);
1195 /* might be an immediate value */
1196 if(is_ia32_Immediate(pred)) {
1197 emit_ia32_Immediate(pred);
1200 reg = get_in_reg(node, in);
1203 ir_fprintf(stderr, "Warning: no register assigned for %d asm op "
1204 "(%+F)\n", num, node);
1212 reg_name = arch_register_get_name(reg);
1215 reg_name = ia32_get_mapped_reg_name(isa->regs_8bit, reg);
1218 reg_name = ia32_get_mapped_reg_name(isa->regs_8bit_high, reg);
1221 reg_name = ia32_get_mapped_reg_name(isa->regs_16bit, reg);
1224 panic("Invalid asm op modifier");
1226 be_emit_string(reg_name);
1232 * Emits code for an ASM pseudo op.
1234 static void emit_ia32_Asm(const ir_node *node)
1236 const void *gen_attr = get_irn_generic_attr_const(node);
1237 const ia32_asm_attr_t *attr
1238 = CONST_CAST_IA32_ATTR(ia32_asm_attr_t, gen_attr);
1239 ident *asm_text = attr->asm_text;
1240 const char *s = get_id_str(asm_text);
1242 be_emit_cstring("# Begin ASM \t");
1243 be_emit_finish_line_gas(node);
1250 s = emit_asm_operand(node, s);
1259 be_emit_write_line();
1261 be_emit_cstring("# End ASM\n");
1262 be_emit_write_line();
1265 /**********************************
1268 * | | ___ _ __ _ _| |_) |
1269 * | | / _ \| '_ \| | | | _ <
1270 * | |___| (_) | |_) | |_| | |_) |
1271 * \_____\___/| .__/ \__, |____/
1274 **********************************/
1277 * Emit movsb/w instructions to make mov count divideable by 4
1279 static void emit_CopyB_prolog(int rem) {
1280 be_emit_cstring("\tcld");
1281 be_emit_finish_line_gas(NULL);
1285 be_emit_cstring("\tmovsb");
1286 be_emit_finish_line_gas(NULL);
1289 be_emit_cstring("\tmovsw");
1290 be_emit_finish_line_gas(NULL);
1293 be_emit_cstring("\tmovsb");
1294 be_emit_finish_line_gas(NULL);
1295 be_emit_cstring("\tmovsw");
1296 be_emit_finish_line_gas(NULL);
1302 * Emit rep movsd instruction for memcopy.
1304 static void emit_ia32_CopyB(const ir_node *node)
1306 int rem = get_ia32_pncode(node);
1308 emit_CopyB_prolog(rem);
1310 be_emit_cstring("\trep movsd");
1311 be_emit_finish_line_gas(node);
1315 * Emits unrolled memcopy.
1317 static void emit_ia32_CopyB_i(const ir_node *node)
1319 int size = get_ia32_pncode(node);
1321 emit_CopyB_prolog(size & 0x3);
1325 be_emit_cstring("\tmovsd");
1326 be_emit_finish_line_gas(NULL);
1332 /***************************
1336 * | | / _ \| '_ \ \ / /
1337 * | |___| (_) | | | \ V /
1338 * \_____\___/|_| |_|\_/
1340 ***************************/
1343 * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1345 static void emit_ia32_Conv_with_FP(const ir_node *node)
1347 ir_mode *ls_mode = get_ia32_ls_mode(node);
1348 int ls_bits = get_mode_size_bits(ls_mode);
1350 be_emit_cstring("\tcvt");
1352 if(is_ia32_Conv_I2FP(node)) {
1354 be_emit_cstring("si2ss");
1356 be_emit_cstring("si2sd");
1358 } else if(is_ia32_Conv_FP2I(node)) {
1360 be_emit_cstring("ss2si");
1362 be_emit_cstring("sd2si");
1365 assert(is_ia32_Conv_FP2FP(node));
1367 be_emit_cstring("sd2ss");
1369 be_emit_cstring("ss2sd");
1374 switch(get_ia32_op_type(node)) {
1376 ia32_emit_source_register(node, n_ia32_unary_op);
1378 case ia32_AddrModeS:
1382 assert(0 && "unsupported op type for Conv");
1384 be_emit_cstring(", ");
1385 ia32_emit_dest_register(node, 0);
1386 be_emit_finish_line_gas(node);
1389 static void emit_ia32_Conv_I2FP(const ir_node *node)
1391 emit_ia32_Conv_with_FP(node);
1394 static void emit_ia32_Conv_FP2I(const ir_node *node)
1396 emit_ia32_Conv_with_FP(node);
1399 static void emit_ia32_Conv_FP2FP(const ir_node *node)
1401 emit_ia32_Conv_with_FP(node);
1405 * Emits code for an Int conversion.
1407 static void emit_ia32_Conv_I2I(const ir_node *node)
1409 const char *sign_suffix;
1410 ir_mode *smaller_mode = get_ia32_ls_mode(node);
1411 int smaller_bits = get_mode_size_bits(smaller_mode);
1413 const arch_register_t *in_reg, *out_reg;
1415 assert(!mode_is_float(smaller_mode));
1416 assert(smaller_bits == 8 || smaller_bits == 16 || smaller_bits == 32);
1418 signed_mode = mode_is_signed(smaller_mode);
1419 if(smaller_bits == 32) {
1420 // this should not happen as it's no convert
1424 sign_suffix = signed_mode ? "s" : "z";
1427 out_reg = get_out_reg(node, 0);
1429 switch(get_ia32_op_type(node)) {
1431 in_reg = get_in_reg(node, n_ia32_unary_op);
1433 if (in_reg == &ia32_gp_regs[REG_EAX] &&
1434 out_reg == &ia32_gp_regs[REG_EAX] &&
1438 /* argument and result are both in EAX and */
1439 /* signedness is ok: -> use the smaller cwtl opcode */
1440 be_emit_cstring("\tcwtl");
1442 be_emit_cstring("\tmov");
1443 be_emit_string(sign_suffix);
1444 ia32_emit_mode_suffix_mode(smaller_mode);
1445 be_emit_cstring("l ");
1446 emit_register(in_reg, smaller_mode);
1447 be_emit_cstring(", ");
1448 emit_register(out_reg, NULL);
1451 case ia32_AddrModeS: {
1452 be_emit_cstring("\tmov");
1453 be_emit_string(sign_suffix);
1454 ia32_emit_mode_suffix_mode(smaller_mode);
1455 be_emit_cstring("l ");
1457 be_emit_cstring(", ");
1458 emit_register(out_reg, NULL);
1462 assert(0 && "unsupported op type for Conv");
1464 be_emit_finish_line_gas(node);
1468 * Emits code for an 8Bit Int conversion.
1470 static void emit_ia32_Conv_I2I8Bit(const ir_node *node)
1472 emit_ia32_Conv_I2I(node);
1476 /*******************************************
1479 * | |__ ___ _ __ ___ __| | ___ ___
1480 * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1481 * | |_) | __/ | | | (_) | (_| | __/\__ \
1482 * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1484 *******************************************/
1487 * Emits a backend call
1489 static void emit_be_Call(const ir_node *node)
1491 ir_entity *ent = be_Call_get_entity(node);
1493 be_emit_cstring("\tcall ");
1495 set_entity_backend_marked(ent, 1);
1496 be_emit_string(get_entity_ld_name(ent));
1498 const arch_register_t *reg = get_in_reg(node, be_pos_Call_ptr);
1500 emit_register(reg, NULL);
1502 be_emit_finish_line_gas(node);
1506 * Emits code to increase stack pointer.
1508 static void emit_be_IncSP(const ir_node *node)
1510 int offs = be_get_IncSP_offset(node);
1511 const arch_register_t *reg = arch_get_irn_register(arch_env, node);
1517 be_emit_cstring("\tsubl $");
1518 be_emit_irprintf("%u, ", offs);
1519 emit_register(reg, NULL);
1521 be_emit_cstring("\taddl $");
1522 be_emit_irprintf("%u, ", -offs);
1523 emit_register(reg, NULL);
1525 be_emit_finish_line_gas(node);
1529 * Emits code for Copy/CopyKeep.
1531 static void Copy_emitter(const ir_node *node, const ir_node *op)
1533 const arch_register_t *in = arch_get_irn_register(arch_env, op);
1534 const arch_register_t *out = arch_get_irn_register(arch_env, node);
1540 if(is_unknown_reg(in))
1542 /* copies of vf nodes aren't real... */
1543 if(arch_register_get_class(in) == &ia32_reg_classes[CLASS_ia32_vfp])
1546 mode = get_irn_mode(node);
1547 if (mode == mode_E) {
1548 be_emit_cstring("\tmovsd ");
1549 emit_register(in, NULL);
1550 be_emit_cstring(", ");
1551 emit_register(out, NULL);
1553 be_emit_cstring("\tmovl ");
1554 emit_register(in, NULL);
1555 be_emit_cstring(", ");
1556 emit_register(out, NULL);
1558 be_emit_finish_line_gas(node);
1561 static void emit_be_Copy(const ir_node *node)
1563 Copy_emitter(node, be_get_Copy_op(node));
1566 static void emit_be_CopyKeep(const ir_node *node)
1568 Copy_emitter(node, be_get_CopyKeep_op(node));
1572 * Emits code for exchange.
1574 static void emit_be_Perm(const ir_node *node)
1576 const arch_register_t *in0, *in1;
1577 const arch_register_class_t *cls0, *cls1;
1579 in0 = arch_get_irn_register(arch_env, get_irn_n(node, 0));
1580 in1 = arch_get_irn_register(arch_env, get_irn_n(node, 1));
1582 cls0 = arch_register_get_class(in0);
1583 cls1 = arch_register_get_class(in1);
1585 assert(cls0 == cls1 && "Register class mismatch at Perm");
1587 if (cls0 == &ia32_reg_classes[CLASS_ia32_gp]) {
1588 be_emit_cstring("\txchg ");
1589 emit_register(in1, NULL);
1590 be_emit_cstring(", ");
1591 emit_register(in0, NULL);
1592 be_emit_finish_line_gas(node);
1593 } else if (cls0 == &ia32_reg_classes[CLASS_ia32_xmm]) {
1594 be_emit_cstring("\txorpd ");
1595 emit_register(in1, NULL);
1596 be_emit_cstring(", ");
1597 emit_register(in0, NULL);
1598 be_emit_finish_line_gas(NULL);
1600 be_emit_cstring("\txorpd ");
1601 emit_register(in0, NULL);
1602 be_emit_cstring(", ");
1603 emit_register(in1, NULL);
1604 be_emit_finish_line_gas(NULL);
1606 be_emit_cstring("\txorpd ");
1607 emit_register(in1, NULL);
1608 be_emit_cstring(", ");
1609 emit_register(in0, NULL);
1610 be_emit_finish_line_gas(node);
1611 } else if (cls0 == &ia32_reg_classes[CLASS_ia32_vfp]) {
1613 } else if (cls0 == &ia32_reg_classes[CLASS_ia32_st]) {
1616 panic("unexpected register class in be_Perm (%+F)\n", node);
1621 * Emits code for Constant loading.
1623 static void emit_ia32_Const(const ir_node *node)
1625 be_emit_cstring("\tmovl ");
1626 emit_ia32_Immediate(node);
1627 be_emit_cstring(", ");
1628 ia32_emit_dest_register(node, 0);
1630 be_emit_finish_line_gas(node);
1634 * Emits code to load the TLS base
1636 static void emit_ia32_LdTls(const ir_node *node)
1638 be_emit_cstring("\tmovl %gs:0, ");
1639 ia32_emit_dest_register(node, 0);
1640 be_emit_finish_line_gas(node);
1643 /* helper function for emit_ia32_Minus64Bit */
1644 static void emit_mov(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1646 be_emit_cstring("\tmovl ");
1647 emit_register(src, NULL);
1648 be_emit_cstring(", ");
1649 emit_register(dst, NULL);
1650 be_emit_finish_line_gas(node);
1653 /* helper function for emit_ia32_Minus64Bit */
1654 static void emit_neg(const ir_node* node, const arch_register_t *reg)
1656 be_emit_cstring("\tnegl ");
1657 emit_register(reg, NULL);
1658 be_emit_finish_line_gas(node);
1661 /* helper function for emit_ia32_Minus64Bit */
1662 static void emit_sbb0(const ir_node* node, const arch_register_t *reg)
1664 be_emit_cstring("\tsbbl $0, ");
1665 emit_register(reg, NULL);
1666 be_emit_finish_line_gas(node);
1669 /* helper function for emit_ia32_Minus64Bit */
1670 static void emit_sbb(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1672 be_emit_cstring("\tsbbl ");
1673 emit_register(src, NULL);
1674 be_emit_cstring(", ");
1675 emit_register(dst, NULL);
1676 be_emit_finish_line_gas(node);
1679 /* helper function for emit_ia32_Minus64Bit */
1680 static void emit_xchg(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1682 be_emit_cstring("\txchgl ");
1683 emit_register(src, NULL);
1684 be_emit_cstring(", ");
1685 emit_register(dst, NULL);
1686 be_emit_finish_line_gas(node);
1689 /* helper function for emit_ia32_Minus64Bit */
1690 static void emit_zero(const ir_node* node, const arch_register_t *reg)
1692 be_emit_cstring("\txorl ");
1693 emit_register(reg, NULL);
1694 be_emit_cstring(", ");
1695 emit_register(reg, NULL);
1696 be_emit_finish_line_gas(node);
1699 static void emit_ia32_Minus64Bit(const ir_node *node)
1701 const arch_register_t *in_lo = get_in_reg(node, 0);
1702 const arch_register_t *in_hi = get_in_reg(node, 1);
1703 const arch_register_t *out_lo = get_out_reg(node, 0);
1704 const arch_register_t *out_hi = get_out_reg(node, 1);
1706 if (out_lo == in_lo) {
1707 if (out_hi != in_hi) {
1708 /* a -> a, b -> d */
1711 /* a -> a, b -> b */
1714 } else if (out_lo == in_hi) {
1715 if (out_hi == in_lo) {
1716 /* a -> b, b -> a */
1717 emit_xchg(node, in_lo, in_hi);
1720 /* a -> b, b -> d */
1721 emit_mov(node, in_hi, out_hi);
1722 emit_mov(node, in_lo, out_lo);
1726 if (out_hi == in_lo) {
1727 /* a -> c, b -> a */
1728 emit_mov(node, in_lo, out_lo);
1730 } else if (out_hi == in_hi) {
1731 /* a -> c, b -> b */
1732 emit_mov(node, in_lo, out_lo);
1735 /* a -> c, b -> d */
1736 emit_mov(node, in_lo, out_lo);
1742 emit_neg( node, out_hi);
1743 emit_neg( node, out_lo);
1744 emit_sbb0(node, out_hi);
1748 emit_zero(node, out_hi);
1749 emit_neg( node, out_lo);
1750 emit_sbb( node, in_hi, out_hi);
1753 static void emit_be_Return(const ir_node *node)
1756 be_emit_cstring("\tret");
1758 pop = be_Return_get_pop(node);
1760 be_emit_irprintf(" $%d", pop);
1762 be_emit_finish_line_gas(node);
1765 static void emit_Nothing(const ir_node *node)
1771 /***********************************************************************************
1774 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
1775 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1776 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
1777 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
1779 ***********************************************************************************/
1782 * Enters the emitter functions for handled nodes into the generic
1783 * pointer of an opcode.
1786 void ia32_register_emitters(void) {
1788 #define IA32_EMIT2(a,b) op_ia32_##a->ops.generic = (op_func)emit_ia32_##b
1789 #define IA32_EMIT(a) IA32_EMIT2(a,a)
1790 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
1791 #define IGN(a) op_##a->ops.generic = (op_func)emit_Nothing
1792 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
1793 #define BE_IGN(a) op_be_##a->ops.generic = (op_func)emit_Nothing
1795 /* first clear the generic function pointer for all ops */
1796 clear_irp_opcodes_generic_func();
1798 /* register all emitter functions defined in spec */
1799 ia32_register_spec_emitters();
1801 /* other ia32 emitter functions */
1804 IA32_EMIT(SwitchJmp);
1807 IA32_EMIT(Conv_I2FP);
1808 IA32_EMIT(Conv_FP2I);
1809 IA32_EMIT(Conv_FP2FP);
1810 IA32_EMIT(Conv_I2I);
1811 IA32_EMIT(Conv_I2I8Bit);
1814 IA32_EMIT(Minus64Bit);
1817 /* benode emitter */
1842 static const char *last_name = NULL;
1843 static unsigned last_line = -1;
1844 static unsigned num = -1;
1847 * Emit the debug support for node node.
1849 static void ia32_emit_dbg(const ir_node *node)
1851 dbg_info *db = get_irn_dbg_info(node);
1853 const char *fname = be_retrieve_dbg_info(db, &lineno);
1855 if (! cg->birg->main_env->options->stabs_debug_support)
1859 if (last_name != fname) {
1861 be_dbg_include_begin(cg->birg->main_env->db_handle, fname);
1864 if (last_line != lineno) {
1867 snprintf(name, sizeof(name), ".LM%u", ++num);
1869 be_dbg_line(cg->birg->main_env->db_handle, lineno, name);
1870 be_emit_string(name);
1871 be_emit_cstring(":\n");
1872 be_emit_write_line();
1877 typedef void (*emit_func_ptr) (const ir_node *);
1880 * Emits code for a node.
1882 static void ia32_emit_node(const ir_node *node)
1884 ir_op *op = get_irn_op(node);
1886 DBG((dbg, LEVEL_1, "emitting code for %+F\n", node));
1888 if (op->ops.generic) {
1889 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
1890 ia32_emit_dbg(node);
1894 ir_fprintf(stderr, "Error: No emit handler for node %+F (%+G, graph %+F)\n", node, node, current_ir_graph);
1900 * Emits gas alignment directives
1902 static void ia32_emit_alignment(unsigned align, unsigned skip)
1904 be_emit_cstring("\t.p2align ");
1905 be_emit_irprintf("%u,,%u\n", align, skip);
1906 be_emit_write_line();
1910 * Emits gas alignment directives for Functions depended on cpu architecture.
1912 static void ia32_emit_align_func(cpu_support cpu)
1915 unsigned maximum_skip;
1930 maximum_skip = (1 << align) - 1;
1931 ia32_emit_alignment(align, maximum_skip);
1935 * Emits gas alignment directives for Labels depended on cpu architecture.
1937 static void ia32_emit_align_label(cpu_support cpu)
1939 unsigned align; unsigned maximum_skip;
1954 maximum_skip = (1 << align) - 1;
1955 ia32_emit_alignment(align, maximum_skip);
1959 * Test wether a block should be aligned.
1960 * For cpus in the P4/Athlon class it is usefull to align jump labels to
1961 * 16 bytes. However we should only do that if the alignment nops before the
1962 * label aren't executed more often than we have jumps to the label.
1964 static int should_align_block(ir_node *block, ir_node *prev)
1966 static const double DELTA = .0001;
1967 ir_exec_freq *exec_freq = cg->birg->exec_freq;
1969 double prev_freq = 0; /**< execfreq of the fallthrough block */
1970 double jmp_freq = 0; /**< execfreq of all non-fallthrough blocks */
1971 cpu_support cpu = isa->opt_arch;
1974 if(exec_freq == NULL)
1976 if(cpu == arch_i386 || cpu == arch_i486)
1979 block_freq = get_block_execfreq(exec_freq, block);
1980 if(block_freq < DELTA)
1983 n_cfgpreds = get_Block_n_cfgpreds(block);
1984 for(i = 0; i < n_cfgpreds; ++i) {
1985 ir_node *pred = get_Block_cfgpred_block(block, i);
1986 double pred_freq = get_block_execfreq(exec_freq, pred);
1989 prev_freq += pred_freq;
1991 jmp_freq += pred_freq;
1995 if(prev_freq < DELTA && !(jmp_freq < DELTA))
1998 jmp_freq /= prev_freq;
2002 case arch_athlon_64:
2004 return jmp_freq > 3;
2006 return jmp_freq > 2;
2010 static void ia32_emit_block_header(ir_node *block, ir_node *prev)
2015 ir_exec_freq *exec_freq = cg->birg->exec_freq;
2017 n_cfgpreds = get_Block_n_cfgpreds(block);
2018 need_label = (n_cfgpreds != 0);
2020 if (should_align_block(block, prev)) {
2022 ia32_emit_align_label(isa->opt_arch);
2026 ia32_emit_block_name(block);
2029 be_emit_pad_comment();
2030 be_emit_cstring(" /* preds:");
2032 /* emit list of pred blocks in comment */
2033 arity = get_irn_arity(block);
2034 for (i = 0; i < arity; ++i) {
2035 ir_node *predblock = get_Block_cfgpred_block(block, i);
2036 be_emit_irprintf(" %d", get_irn_node_nr(predblock));
2039 be_emit_cstring("\t/* ");
2040 ia32_emit_block_name(block);
2041 be_emit_cstring(": ");
2043 if (exec_freq != NULL) {
2044 be_emit_irprintf(" freq: %f",
2045 get_block_execfreq(exec_freq, block));
2047 be_emit_cstring(" */\n");
2048 be_emit_write_line();
2052 * Walks over the nodes in a block connected by scheduling edges
2053 * and emits code for each node.
2055 static void ia32_gen_block(ir_node *block, ir_node *last_block)
2057 const ir_node *node;
2059 ia32_emit_block_header(block, last_block);
2061 /* emit the contents of the block */
2062 ia32_emit_dbg(block);
2063 sched_foreach(block, node) {
2064 ia32_emit_node(node);
2069 * Emits code for function start.
2071 static void ia32_emit_func_prolog(ir_graph *irg)
2073 ir_entity *irg_ent = get_irg_entity(irg);
2074 const char *irg_name = get_entity_ld_name(irg_ent);
2075 cpu_support cpu = isa->opt_arch;
2076 const be_irg_t *birg = cg->birg;
2078 be_emit_write_line();
2079 be_gas_emit_switch_section(GAS_SECTION_TEXT);
2080 be_dbg_method_begin(birg->main_env->db_handle, irg_ent, be_abi_get_stack_layout(birg->abi));
2081 ia32_emit_align_func(cpu);
2082 if (get_entity_visibility(irg_ent) == visibility_external_visible) {
2083 be_emit_cstring(".global ");
2084 be_emit_string(irg_name);
2086 be_emit_write_line();
2088 ia32_emit_function_object(irg_name);
2089 be_emit_string(irg_name);
2090 be_emit_cstring(":\n");
2091 be_emit_write_line();
2095 * Emits code for function end
2097 static void ia32_emit_func_epilog(ir_graph *irg)
2099 const char *irg_name = get_entity_ld_name(get_irg_entity(irg));
2100 const be_irg_t *birg = cg->birg;
2102 ia32_emit_function_size(irg_name);
2103 be_dbg_method_end(birg->main_env->db_handle);
2105 be_emit_write_line();
2110 * Sets labels for control flow nodes (jump target)
2112 static void ia32_gen_labels(ir_node *block, void *data)
2115 int n = get_Block_n_cfgpreds(block);
2118 for (n--; n >= 0; n--) {
2119 pred = get_Block_cfgpred(block, n);
2120 set_irn_link(pred, block);
2125 * Emit an exception label if the current instruction can fail.
2127 void ia32_emit_exc_label(const ir_node *node)
2129 if (get_ia32_exc_label(node)) {
2130 be_emit_irprintf(".EXL%u\n", 0);
2131 be_emit_write_line();
2136 * Main driver. Emits the code for one routine.
2138 void ia32_gen_routine(ia32_code_gen_t *ia32_cg, ir_graph *irg)
2141 ir_node *last_block = NULL;
2145 isa = (const ia32_isa_t*) cg->arch_env->isa;
2146 arch_env = cg->arch_env;
2148 ia32_register_emitters();
2150 ia32_emit_func_prolog(irg);
2151 irg_block_walk_graph(irg, ia32_gen_labels, NULL, NULL);
2153 n = ARR_LEN(cg->blk_sched);
2154 for (i = 0; i < n;) {
2157 block = cg->blk_sched[i];
2159 next_bl = i < n ? cg->blk_sched[i] : NULL;
2161 /* set here the link. the emitter expects to find the next block here */
2162 set_irn_link(block, next_bl);
2163 ia32_gen_block(block, last_block);
2167 ia32_emit_func_epilog(irg);
2170 void ia32_init_emitter(void)
2172 FIRM_DBG_REGISTER(dbg, "firm.be.ia32.emitter");