added nolea option to switch of LEA optimization
[libfirm] / ir / be / ia32 / ia32_emitter.c
index e7c0cd1..fa52f20 100644 (file)
@@ -30,6 +30,7 @@
 #include "ia32_nodes_attr.h"
 #include "ia32_new_nodes.h"
 #include "ia32_map_regs.h"
+#include "bearch_ia32_t.h"
 
 #define BLOCK_PREFIX(x) ".L" x
 
@@ -46,7 +47,7 @@ asm_flavour_t asm_flavour = ASM_LINUX_GAS;
  */
 void ia32_switch_section(FILE *F, section_t sec) {
        static section_t curr_sec = NO_SECTION;
-  static const char *text[ASM_MAX][SECTION_MAX] = {
+       static const char *text[ASM_MAX][SECTION_MAX] = {
                {
                        ".section\t.text", ".section\t.data", ".section\t.rodata", ".section\t.text"
                },
@@ -141,8 +142,6 @@ static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
                reg = &ia32_xmm_regs[REG_XMM0];
        else if (REGS_ARE_EQUAL(reg, &ia32_vfp_regs[REG_VFP_UKNWN]))
                reg = &ia32_vfp_regs[REG_VF0];
-       else if (REGS_ARE_EQUAL(reg, &ia32_st_regs[REG_ST_UKNWN]))
-               reg = &ia32_st_regs[REG_ST0];
 
        return reg;
 }
@@ -287,7 +286,7 @@ static int ia32_get_mode_suffix(lc_appendable_t *app,
        ir_mode *mode = get_irn_mode(X);
 
        if (mode == mode_T) {
-               mode = is_ia32_AddrModeS(X) || is_ia32_AddrModeD(X) ? get_ia32_ls_mode(X) : get_ia32_res_mode(X);
+               mode = (is_ia32_Ld(X) || is_ia32_St(X)) ? get_ia32_ls_mode(X) : get_ia32_res_mode(X);
        }
 
        if (!X)
@@ -396,7 +395,7 @@ char *ia32_emit_binop(const ir_node *n, ia32_emit_env_t *env) {
                                        lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, ia32_emit_am(n, env));
                                }
                                else {
-                                       lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%4S, %s", n, ia32_emit_am(n, env));
+                                       lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, ia32_emit_am(n, env));
                                }
                        }
                        break;
@@ -503,8 +502,8 @@ char *ia32_emit_unop(const ir_node *n, ia32_emit_env_t *env) {
                                lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
                        }
                        break;
-               case ia32_am_Dest:
-                       snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n, env));
+               case ia32_AddrModeD:
+                       snprintf(buf, SNPRINTF_BUF_LEN, "%s", ia32_emit_am(n, env));
                        break;
                default:
                        assert(0 && "unsupported op type");
@@ -744,40 +743,56 @@ static ir_node *next_blk_sched(const ir_node *block) {
        return get_irn_link(block);
 }
 
+/**
+ * Returns the Proj with projection number proj and NOT mode_M
+ */
+static ir_node *get_proj(const ir_node *irn, long proj) {
+       const ir_edge_t *edge;
+       ir_node         *src;
+
+       assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
+
+       foreach_out_edge(irn, edge) {
+               src = get_edge_src_irn(edge);
+
+               assert(is_Proj(src) && "Proj expected");
+               if (get_irn_mode(src) == mode_M)
+                       continue;
+
+               if (get_Proj_proj(src) == proj)
+                       return src;
+       }
+       return NULL;
+}
+
 /**
  * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
  */
 static void finish_CondJmp(FILE *F, const ir_node *irn, ir_mode *mode) {
        const ir_node   *proj1, *proj2 = NULL;
        const ir_node   *block, *next_bl = NULL;
-       const ir_edge_t *edge;
        char buf[SNPRINTF_BUF_LEN];
        char cmd_buf[SNPRINTF_BUF_LEN];
        char cmnt_buf[SNPRINTF_BUF_LEN];
 
        /* get both Proj's */
-       edge = get_irn_out_edge_first(irn);
-       proj1 = get_edge_src_irn(edge);
-       assert(is_Proj(proj1) && "CondJmp with a non-Proj");
+       proj1 = get_proj(irn, pn_Cond_true);
+       assert(proj1 && "CondJmp without true Proj");
 
-       edge = get_irn_out_edge_next(irn, edge);
-       if (edge) {
-               proj2 = get_edge_src_irn(edge);
-               assert(is_Proj(proj2) && "CondJmp with a non-Proj");
-       }
+       proj2 = get_proj(irn, pn_Cond_false);
+       assert(proj2 && "CondJmp without false Proj");
 
        /* for now, the code works for scheduled and non-schedules blocks */
        block = get_nodes_block(irn);
-       if (proj2) {
-               /* we have a block schedule */
-               next_bl = next_blk_sched(block);
-
-               if (get_cfop_target_block(proj1) == next_bl) {
-                       /* exchange both proj's so the second one can be omitted */
-                       const ir_node *t = proj1;
-                       proj1 = proj2;
-                       proj2 = t;
-               }
+
+       /* we have a block schedule */
+       next_bl = next_blk_sched(block);
+
+       if (get_cfop_target_block(proj1) == next_bl) {
+               /* exchange both proj's so the second one can be omitted */
+               const ir_node *t = proj1;
+               proj1 = proj2;
+               proj2 = t;
        }
 
        /* the first Proj must always be created */
@@ -797,17 +812,15 @@ static void finish_CondJmp(FILE *F, const ir_node *irn, ir_mode *mode) {
        IA32_DO_EMIT(irn);
 
        /* the second Proj might be a fallthrough */
-       if (proj2) {
-               if (get_cfop_target_block(proj2) != next_bl) {
-                       snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj2, buf));
-                       snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* otherwise */");
-               }
-               else {
-                       cmd_buf[0] = '\0';
-                       snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrogh %s */", get_cfop_target(proj2, buf));
-               }
-               IA32_DO_EMIT(irn);
+       if (get_cfop_target_block(proj2) != next_bl) {
+               snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj2, buf));
+               snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* otherwise */");
        }
+       else {
+               cmd_buf[0] = '\0';
+               snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough %s */", get_cfop_target(proj2, buf));
+       }
+       IA32_DO_EMIT(irn);
 }
 
 /**
@@ -1562,6 +1575,65 @@ static void ia32_emit_node(const ir_node *irn, void *env) {
        }
 }
 
+/**
+ * Emits gas alignment directives
+ */
+static void ia32_emit_alignment(FILE *F, unsigned align, unsigned skip) {
+       fprintf(F, "\t.p2align %u,,%u\n", align, skip);
+}
+
+/**
+ * Emits gas alignment directives for Functions depended on cpu architecture.
+ */
+static void ia32_emit_align_func(FILE *F, cpu_support cpu) {
+       unsigned align; unsigned maximum_skip;
+
+       /* gcc doesn't emit alignment for p4 ?*/
+    if (cpu == arch_pentium_4)
+               return;
+
+       switch (cpu) {
+               case arch_i386:
+                       align = 2; maximum_skip = 3;
+                       break;
+               case arch_i486:
+                       align = 4; maximum_skip = 15;
+                       break;
+               case arch_k6:
+                       align = 5; maximum_skip = 31;
+                       break;
+               default:
+                       align = 4; maximum_skip = 15;
+       }
+       ia32_emit_alignment(F, align, maximum_skip);
+}
+
+/**
+ * Emits gas alignment directives for Labels depended on cpu architecture.
+ */
+static void ia32_emit_align_label(FILE *F, cpu_support cpu) {
+       unsigned align; unsigned maximum_skip;
+
+       /* gcc doesn't emit alignment for p4 ?*/
+    if (cpu == arch_pentium_4)
+               return;
+
+       switch (cpu) {
+               case arch_i386:
+                       align = 2; maximum_skip = 3;
+                       break;
+               case arch_i486:
+                       align = 4; maximum_skip = 15;
+                       break;
+               case arch_k6:
+                       align = 5; maximum_skip = 7;
+                       break;
+               default:
+                       align = 4; maximum_skip = 7;
+       }
+       ia32_emit_alignment(F, align, maximum_skip);
+}
+
 /**
  * Walks over the nodes in a block connected by scheduling edges
  * and emits code for each node.
@@ -1580,8 +1652,10 @@ static void ia32_gen_block(ir_node *block, void *env) {
                need_label = (block == get_extbb_leader(get_nodes_extbb(block)));
        }
 
-       if (need_label)
+       if (need_label) {
+               ia32_emit_align_label(emit_env->out, emit_env->isa->opt_arch);
                fprintf(emit_env->out, BLOCK_PREFIX("%ld:\n"), get_irn_node_nr(block));
+       }
 
        sched_foreach(block, irn) {
                ia32_emit_node(irn, env);
@@ -1591,12 +1665,13 @@ static void ia32_gen_block(ir_node *block, void *env) {
 /**
  * Emits code for function start.
  */
-static void ia32_emit_func_prolog(FILE *F, ir_graph *irg) {
+static void ia32_emit_func_prolog(FILE *F, ir_graph *irg, cpu_support cpu) {
        entity     *irg_ent  = get_irg_entity(irg);
        const char *irg_name = get_entity_ld_name(irg_ent);
 
        fprintf(F, "\n");
        ia32_switch_section(F, SECTION_TEXT);
+       ia32_emit_align_func(F, cpu);
        if (get_entity_visibility(irg_ent) == visibility_external_visible) {
                fprintf(F, ".globl %s\n", irg_name);
        }
@@ -1648,7 +1723,7 @@ void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
 
        ia32_register_emitters();
 
-       ia32_emit_func_prolog(F, irg);
+       ia32_emit_func_prolog(F, irg, emit_env.isa->opt_arch);
        irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
 
        if ((cg->opt & IA32_OPT_EXTBB) && cg->blk_sched) {