bugfixes for am_offs changes
[libfirm] / ir / be / ia32 / ia32_emitter.c
index 579e990..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);
 }
@@ -1000,7 +1014,7 @@ static void emit_ia32_x87CondJmp(ir_node *irn, ia32_emit_env_t *env) {
        if (reverse)
                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);
@@ -1042,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");
@@ -1064,12 +1078,20 @@ static void CMov_emitter(ir_node *irn, ia32_emit_env_t *env) {
        }
        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);
 }
@@ -1574,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)) {
@@ -1792,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)) {
@@ -1812,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();
@@ -1874,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);
@@ -1944,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);
 }
 
@@ -1970,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);
 }