bugfixes for am_offs changes
[libfirm] / ir / be / ia32 / ia32_emitter.c
index 390bfea..8612528 100644 (file)
@@ -49,10 +49,20 @@ void ia32_switch_section(FILE *F, section_t sec) {
        static section_t curr_sec = NO_SECTION;
        static const char *text[ASM_MAX][SECTION_MAX] = {
                {
-                       ".section\t.text", ".section\t.data", ".section\t.rodata", ".section\t.text"
+                       ".section\t.text",
+                       ".section\t.data",
+                       ".section\t.rodata",
+                       ".section\t.text",
+                       ".section\t.tbss,\"awT\",@nobits",
+                       ".section\t.ctors,\"aw\",@progbits"
                },
                {
-                       ".section\t.text", ".section\t.data", ".section .rdata,\"dr\"", ".section\t.text"
+                       ".section\t.text",
+                       ".section\t.data",
+                       ".section .rdata,\"dr\"",
+                       ".section\t.text",
+                       ".section\t.tbss,\"awT\",@nobits",
+                       ".section\t.ctors,\"aw\",@progbits"
                }
        };
 
@@ -69,6 +79,8 @@ void ia32_switch_section(FILE *F, section_t sec) {
        case SECTION_DATA:
        case SECTION_RODATA:
        case SECTION_COMMON:
+       case SECTION_TLS:
+       case SECTION_CTOR:
                fprintf(F, "\t%s\n", text[asm_flavour][sec]);
                break;
 
@@ -820,57 +832,59 @@ static ir_node *get_proj(const ir_node *irn, long proj) {
  * 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_node *proj_true;
+       const ir_node *proj_false;
+       const ir_node *block;
+       const ir_node *next_block;
        char buf[SNPRINTF_BUF_LEN];
        char cmd_buf[SNPRINTF_BUF_LEN];
        char cmnt_buf[SNPRINTF_BUF_LEN];
        int is_unsigned;
+       int pnc;
+       int flipped = 0;
 
        /* get both Proj's */
-       proj1 = get_proj(irn, pn_Cond_true);
-       assert(proj1 && "CondJmp without true Proj");
+       proj_true = get_proj(irn, pn_Cond_true);
+       assert(proj_true && "CondJmp without true Proj");
 
-       proj2 = get_proj(irn, pn_Cond_false);
-       assert(proj2 && "CondJmp without false Proj");
+       proj_false = get_proj(irn, pn_Cond_false);
+       assert(proj_false && "CondJmp without false Proj");
+
+       pnc = get_ia32_pncode(irn);
 
        /* for now, the code works for scheduled and non-schedules blocks */
        block = get_nodes_block(irn);
 
        /* we have a block schedule */
-       next_bl = next_blk_sched(block);
+       next_block = next_blk_sched(block);
 
-       if (get_cfop_target_block(proj1) == next_bl) {
+       if (get_cfop_target_block(proj_true) == next_block) {
                /* exchange both proj's so the second one can be omitted */
-               const ir_node *t = proj1;
-               proj1 = proj2;
-               proj2 = t;
+               const ir_node *t = proj_true;
+               proj_true = proj_false;
+               proj_false = t;
+
+               flipped = 1;
+               pnc = get_negated_pnc(pnc, mode);
        }
 
        /* the first Proj must always be created */
        is_unsigned = mode_is_float(mode) || ! mode_is_signed(mode);
-       if (get_Proj_proj(proj1) == pn_Cond_true) {
-               snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
-                                       get_cmp_suffix(get_ia32_pncode(irn), is_unsigned),
-                                       get_cfop_target(proj1, buf));
-               snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == TRUE */");
-       }
-       else  {
-               snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
-                                       get_cmp_suffix(get_negated_pnc(get_ia32_pncode(irn), mode), is_unsigned),
-                                       get_cfop_target(proj1, buf));
-               snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == FALSE */");
-       }
+       snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
+                get_cmp_suffix(pnc, is_unsigned),
+                get_cfop_target(proj_true, buf));
+       snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* %s(a, b) %s*/",
+                get_pnc_string(pnc), flipped ? "(was flipped)" : "");
        IA32_DO_EMIT(irn);
 
        /* the second Proj might be a fallthrough */
-       if (get_cfop_target_block(proj2) != next_bl) {
-               snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj2, buf));
+       if (get_cfop_target_block(proj_false) != next_block) {
+               snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj_false, 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));
+               snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough %s */", get_cfop_target(proj_false, buf));
        }
        IA32_DO_EMIT(irn);
 }
@@ -977,7 +991,7 @@ static void emit_ia32_x87CondJmp(ir_node *irn, ia32_emit_env_t *env) {
        const char *instr = "fcom";
        int reverse = 0;
 
-       switch (get_ia32_pncode(irn)) {
+       switch (get_ia32_irn_opcode(irn)) {
        case iro_ia32_fcomrJmp:
                reverse = 1;
        case iro_ia32_fcomJmp:
@@ -998,9 +1012,9 @@ static void emit_ia32_x87CondJmp(ir_node *irn, ia32_emit_env_t *env) {
        }
 
        if (reverse)
-               set_ia32_pncode(irn, (long)get_negated_pnc(get_ia32_pncode(irn), mode_Is));
+               set_ia32_pncode(irn, (long)get_inversed_pnc(get_ia32_pncode(irn)));
 
-       snprintf(cmd_buf, SNPRINTF_BUF_LEN, "%s %%%s", instr, reg);
+       snprintf(cmd_buf, SNPRINTF_BUF_LEN, "%s %s%s", instr, reg[0] == '\0' ? "" : "%", reg);
        lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
        IA32_DO_EMIT(irn);
        lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fnstsw %%ax", irn);
@@ -1010,7 +1024,8 @@ static void emit_ia32_x87CondJmp(ir_node *irn, ia32_emit_env_t *env) {
        snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* Store ah into flags */");
        IA32_DO_EMIT(irn);
 
-       finish_CondJmp(F, irn, mode_Is);
+       /* the compare flags must be evaluated using carry , ie unsigned */
+       finish_CondJmp(F, irn, mode_Iu);
 }
 
 static void CMov_emitter(ir_node *irn, ia32_emit_env_t *env) {
@@ -1020,14 +1035,16 @@ static void CMov_emitter(ir_node *irn, ia32_emit_env_t *env) {
        int        is_unsigned = mode_is_float(mode) || ! mode_is_signed(mode);
        const char *cmp_suffix = get_cmp_suffix(get_ia32_pncode(irn), is_unsigned);
        int is_PsiCondCMov     = is_ia32_PsiCondCMov(irn);
+       int idx_left  = 2 - is_PsiCondCMov;
+       int idx_right = 3 - is_PsiCondCMov;
 
        char cmd_buf[SNPRINTF_BUF_LEN];
        char cmnt_buf[SNPRINTF_BUF_LEN];
        const arch_register_t *in1, *in2, *out;
 
        out = arch_get_irn_register(env->arch_env, irn);
-       in1 = arch_get_irn_register(env->arch_env, get_irn_n(irn, 2 - is_PsiCondCMov));
-       in2 = arch_get_irn_register(env->arch_env, get_irn_n(irn, 3 - is_PsiCondCMov));
+       in1 = arch_get_irn_register(env->arch_env, get_irn_n(irn, idx_left));
+       in2 = arch_get_irn_register(env->arch_env, get_irn_n(irn, idx_right));
 
        /* we have to emit the cmp first, because the destination register */
        /* could be one of the compare registers                           */
@@ -1039,7 +1056,7 @@ static void CMov_emitter(ir_node *irn, ia32_emit_env_t *env) {
        }
        else if (is_PsiCondCMov) {
                /* omit compare because flags are already set by And/Or */
-               snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
+               lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "test %1S, %1S", irn, irn);
        }
        else {
                assert(0 && "unsupported CMov");
@@ -1052,21 +1069,29 @@ static void CMov_emitter(ir_node *irn, ia32_emit_env_t *env) {
        }
        else if (REGS_ARE_EQUAL(out, in1)) {
                /* true in == out -> need complement compare and exchange true and default in */
-               ir_node *t = get_irn_n(irn, 2);
-               set_irn_n(irn, 2, get_irn_n(irn, 3));
-               set_irn_n(irn, 3, t);
+               ir_node *t = get_irn_n(irn, idx_left);
+               set_irn_n(irn, idx_left, get_irn_n(irn, idx_right));
+               set_irn_n(irn, idx_right, t);
 
-               cmp_suffix  = get_cmp_suffix(get_inversed_pnc(get_ia32_pncode(irn)), is_unsigned);
+               cmp_suffix  = get_cmp_suffix(get_negated_pnc(get_ia32_pncode(irn), get_irn_mode(irn)), is_unsigned);
 
        }
        else {
                /* out is different from in: need copy default -> out */
-               lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %4S", irn, irn);
+               if (is_PsiCondCMov)
+                       lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
+               else
+                       lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %4S", irn, irn);
+
                lc_esnprintf(arg_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* copy default -> out */" );
                IA32_DO_EMIT(irn);
        }
 
-       lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "cmov%s %1D, %3S", cmp_suffix, irn, irn);
+       if (is_PsiCondCMov)
+               lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "cmov%s %1D, %2S", cmp_suffix, irn, irn);
+       else
+               lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "cmov%s %1D, %3S", cmp_suffix, irn, irn);
+
        lc_esnprintf(arg_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* condition is true case */" );
        IA32_DO_EMIT(irn);
 }
@@ -1139,41 +1164,80 @@ static void emit_ia32_xCmp(ir_node *irn, ia32_emit_env_t *env) {
        FILE               *F       = env->out;
        const lc_arg_env_t *arg_env = ia32_get_arg_env();
        int                sse_pnc  = -1;
+       long               pnc      = get_ia32_pncode(irn);
+       long               unord    = pnc & pn_Cmp_Uo;
        char cmd_buf[SNPRINTF_BUF_LEN];
        char cmnt_buf[SNPRINTF_BUF_LEN];
 
-       switch (get_ia32_pncode(irn)) {
+       switch (pnc) {
                case pn_Cmp_Leg: /* odered */
                        sse_pnc = 7;
                        break;
                case pn_Cmp_Uo:  /* unordered */
                        sse_pnc = 3;
                        break;
-               case pn_Cmp_Ue:  /* == */
+               case pn_Cmp_Ue:
+               case pn_Cmp_Eq:  /* == */
                        sse_pnc = 0;
                        break;
-               case pn_Cmp_Ul:  /* < */
+               case pn_Cmp_Ul:
+               case pn_Cmp_Lt:  /* < */
                        sse_pnc = 1;
                        break;
-               case pn_Cmp_Ule: /* <= */
+               case pn_Cmp_Ule:
+               case pn_Cmp_Le: /* <= */
                        sse_pnc = 2;
                        break;
-               case pn_Cmp_Ug:  /* > */
+               case pn_Cmp_Ug:
+               case pn_Cmp_Gt:  /* > */
                        sse_pnc = 6;
                        break;
-               case pn_Cmp_Uge: /* >= */
+               case pn_Cmp_Uge:
+               case pn_Cmp_Ge: /* >= */
                        sse_pnc = 5;
                        break;
-               case pn_Cmp_Ne:  /* != */
+               case pn_Cmp_Ne:
+               case pn_Cmp_Lg:  /* != */
                        sse_pnc = 4;
                        break;
        }
 
-       assert(sse_pnc >= 0 && "unsupported floating point compare");
-
-       lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "cmps%M %s, %d", irn, ia32_emit_binop(irn, env), sse_pnc);
-       lc_esnprintf(arg_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* SSE compare with result in %1D */", irn);
+       assert(sse_pnc >= 0 && "unsupported compare");
+
+       if (unord && sse_pnc != 3) {
+               /*
+                       We need a separate compare against unordered.
+                       Quick and Dirty solution:
+                       - get some memory on stack
+                       - compare
+                       - store result
+                       - compare
+                       - and result and stored result
+                   - cleanup stack
+               */
+               snprintf(cmd_buf, SNPRINTF_BUF_LEN, "sub %%esp, 8");
+               snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* reserve some space for unordered compare result */");
+               IA32_DO_EMIT(NULL);
+               snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmpsd %s, 3", ia32_emit_binop(irn, env));
+               snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* SSE compare: unordered */");
+               IA32_DO_EMIT(NULL);
+               lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "movsd [%%esp], %1D", irn);
+               snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* store compare result */");
+               IA32_DO_EMIT(NULL);
+       }
+
+       snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmpsd %s, %d", ia32_emit_binop(irn, env), sse_pnc);
+       lc_esnprintf(arg_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* SSE compare (%+F) with result in %1D */", irn, irn);
        IA32_DO_EMIT(irn);
+
+       if (unord && sse_pnc != 3) {
+               lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "andpd %1D, [%%esp]", irn);
+               snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* build the final result */");
+               IA32_DO_EMIT(NULL);
+               snprintf(cmd_buf, SNPRINTF_BUF_LEN, "add %%esp, 8");
+               snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* free allocated space */");
+               IA32_DO_EMIT(NULL);
+       }
 }
 
 /*********************************************************
@@ -1532,8 +1596,10 @@ static void emit_ia32_Conv_I2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
                        conv_cmd = "cbw";
                else if (n == 16 || m == 16)
                        conv_cmd = "cwde";
-               else
+               else {
+                       printf("%d -> %d unsupported\n", n, m);
                        assert(0 && "unsupported Conv_I2I");
+               }
        }
 
         switch(get_ia32_op_type(irn)) {
@@ -1621,15 +1687,14 @@ static void emit_be_Call(const ir_node *irn, ia32_emit_env_t *emit_env) {
  */
 static void emit_be_IncSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
        FILE          *F    = emit_env->out;
-       unsigned       offs = be_get_IncSP_offset(irn);
-       be_stack_dir_t dir  = be_get_IncSP_direction(irn);
+       int offs = be_get_IncSP_offset(irn);
        char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
 
        if (offs) {
-               if (dir == be_stack_dir_expand)
+               if (offs > 0)
                        lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "sub %1S, %u", irn, offs);
                else
-                       lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S, %u", irn, offs);
+                       lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S, %u", irn, -offs);
                lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (IncSP) */", irn);
        }
        else {
@@ -1705,10 +1770,12 @@ static void emit_be_Perm(const ir_node *irn, ia32_emit_env_t *emit_env) {
                        "pxor %1S, %2S\n\tpxor %2S, %1S\n\tpxor %1S, %2S", irn, irn, irn, irn, irn, irn);
        }
        else if (cls1 == &ia32_reg_classes[CLASS_ia32_vfp]) {
-               assert(0 && "Perm with vfp should not happen");
+               /* is a NOP */
+               cmd_buf[0] = '\0';
        }
        else if (cls1 == &ia32_reg_classes[CLASS_ia32_st]) {
-               assert(0 && "Perm with st(X) should not happen");
+               /* is a NOP */
+               cmd_buf[0] = '\0';
        }
 
        lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%1A, %2A) */", irn, irn, irn);
@@ -1749,7 +1816,7 @@ static void emit_ia32_Const(const ir_node *n, ia32_emit_env_t *env) {
  * Emits code to increase stack pointer.
  */
 static void emit_ia32_AddSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
-       FILE          *F = emit_env->out;
+       FILE *F = emit_env->out;
        char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
 
        if (is_ia32_ImmConst(irn)) {
@@ -1769,6 +1836,29 @@ static void emit_ia32_AddSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
        IA32_DO_EMIT(irn);
 }
 
+/**
+ * Emits code to load the TLS base
+ */
+static void emit_ia32_LdTls(const ir_node *irn, ia32_emit_env_t *emit_env) {
+       FILE *F = emit_env->out;
+       char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
+
+       switch (asm_flavour) {
+       case ASM_LINUX_GAS:
+               lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, DWORD PTR %%gs:0", irn);
+               break;
+       case ASM_MINGW_GAS:
+               lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, DWORD PTR %%gs:0", irn);
+               break;
+       default:
+               assert(0 && "unsupported TLS");
+               break;
+       }
+       snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* get thread local storage base */");
+
+       IA32_DO_EMIT(irn);
+}
+
 static void emit_be_Return(const ir_node *n, ia32_emit_env_t *env) {
        FILE *F = env->out;
        const lc_arg_env_t *arg_env = ia32_get_arg_env();
@@ -1831,6 +1921,7 @@ static void ia32_register_emitters(void) {
        IA32_EMIT(Conv_I2I8Bit);
        IA32_EMIT(Const);
        IA32_EMIT(AddSP);
+       IA32_EMIT(LdTls);
        IA32_EMIT(xCmp);
        IA32_EMIT(xCmpSet);
        IA32_EMIT(xCmpCMov);
@@ -1901,23 +1992,20 @@ static void ia32_emit_alignment(FILE *F, unsigned align, unsigned skip) {
 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;
+                       align = 2;
                        break;
                case arch_i486:
-                       align = 4; maximum_skip = 15;
+                       align = 4;
                        break;
                case arch_k6:
-                       align = 5; maximum_skip = 31;
+                       align = 5;
                        break;
                default:
-                       align = 4; maximum_skip = 15;
+                       align = 4;
        }
+       maximum_skip = (1 << align) - 1;
        ia32_emit_alignment(F, align, maximum_skip);
 }
 
@@ -1927,23 +2015,20 @@ static void ia32_emit_align_func(FILE *F, cpu_support cpu) {
 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;
+                       align = 2;
                        break;
                case arch_i486:
-                       align = 4; maximum_skip = 15;
+                       align = 4;
                        break;
                case arch_k6:
-                       align = 5; maximum_skip = 7;
+                       align = 5;
                        break;
                default:
-                       align = 4; maximum_skip = 7;
+                       align = 4;
        }
+       maximum_skip = (1 << align) - 1;
        ia32_emit_alignment(F, align, maximum_skip);
 }
 
@@ -1955,6 +2040,7 @@ static void ia32_gen_block(ir_node *block, void *env) {
        ia32_emit_env_t *emit_env = env;
        const ir_node *irn;
        int need_label = block != get_irg_start_block(get_irn_irg(block));
+       FILE *F = emit_env->out;
 
        if (! is_Block(block))
                return;
@@ -1966,10 +2052,27 @@ static void ia32_gen_block(ir_node *block, void *env) {
        }
 
        if (need_label) {
+               char cmd_buf[SNPRINTF_BUF_LEN];
+               int i, arity;
+
                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));
+
+               ir_snprintf(cmd_buf, sizeof(cmd_buf), BLOCK_PREFIX("%d:"),
+                           get_irn_node_nr(block));
+               fprintf(F, "%-43s ", cmd_buf);
+
+               /* emit list of pred blocks in comment */
+               fprintf(F, "/* preds:");
+
+               arity = get_irn_arity(block);
+               for(i = 0; i < arity; ++i) {
+                       ir_node *predblock = get_Block_cfgpred_block(block, i);
+                       fprintf(F, " %ld", get_irn_node_nr(predblock));
+               }
+               fprintf(F, " */\n");
        }
 
+       /* emit the contents of the block */
        sched_foreach(block, irn) {
                ia32_emit_node(irn, env);
        }