Skip to contentSkip to conversation

Proposed format for m68k flags

PATCH In review Richard Henderson 3 participants 16 messages Aug 14, 2015 – Aug 15, 2015

lore
Share & tools
lore
#0 lore raw

cover Proposed format for m68k flags

As promised a couple of days ago, with the addition of CC_OP_CMP, which wasn't in the text of my proposal the other day. From the looks of the generated code, I believe this is ideal.

The following is based on Laurent's 8/30 Update cpu flags management.

FWIW, there's something in the last patch here that breaks the coldfire kernel I've been testing (it may even be a bug in tcg; the problem only appears well into the boot process). But I'm about to go away for the weekend and still wanted to include it to show what can be done.

For convenience, the complete tree pushed to

git://github.com/rth7680/qemu.git tgt-m68k

r~

Richard Henderson (11):
target-m68k: Print flags properly
target-m68k: Some fixes to SR and flags management
target-m68k: Remove incorrect clearing of cc_x
target-m68k: Replace helper_xflag_lt with setcond
target-m68k: Reorg flags handling
target-m68k: Introduce DisasCompare
target-m68k: Use setcond for scc
target-m68k: Optimize some comparisons
target-m68k: Optimize gen_flush_flags
target-m68k: Inline shifts
target-m68k: Inline addx, subx, negx

 target-m68k/cpu.c       |   2 +-
 target-m68k/cpu.h       |  48 +--
 target-m68k/helper.c    | 399 +++++++++----------------
 target-m68k/helper.h    |  12 +-
 target-m68k/op_helper.c |  35 +--
 target-m68k/qregs.def   |   6 +-
 target-m68k/translate.c | 769 +++++++++++++++++++++++++++++++-----------------
 7 files changed, 674 insertions(+), 597 deletions(-)
Signature · 1 line
2.4.3
#1 lore raw

PATCH 1/11 target-m68k: Print flags properly

---
 target-m68k/cpu.h       | 1 +
 target-m68k/helper.c    | 2 +-
 target-m68k/translate.c | 4 ++--
 3 files changed, 4 insertions(+), 3 deletions(-)
target-m68k/cpu.h +1 -0
index 6d1a140..9708615 100644
@@ -123,6 +123,7 @@ int cpu_m68k_exec(CPUState *cpu);
    is returned if the signal was handled by the virtual CPU.  */
 int cpu_m68k_signal_handler(int host_signum, void *pinfo,
                            void *puc);
+uint32_t cpu_m68k_flush_flags(CPUM68KState *env, int op);
 
 typedef enum {
     CC_OP_DYNAMIC, /* Use env->cc_op  */
target-m68k/helper.c +1 -1
index c7bccf7..2185041 100644
@@ -130,7 +130,7 @@ void m68k_cpu_init_gdb(M68kCPU *cpu)
     /* TODO: Add [E]MAC registers.  */
 }
 
-static uint32_t cpu_m68k_flush_flags(CPUM68KState *env, int op)
+uint32_t cpu_m68k_flush_flags(CPUM68KState *env, int op)
 {
     int flags;
     uint32_t src;
target-m68k/translate.c +2 -2
index 80ac63a..c000d0a 100644
@@ -3236,8 +3236,8 @@ void m68k_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
                      i, u.l.upper, u.l.lower, *(double *)&u.d);
       }
     cpu_fprintf (f, "PC = %08x   ", env->pc);
-    sr = env->sr;
-    cpu_fprintf (f, "SR = %04x %c%c%c%c%c ", sr, (sr & 0x10) ? 'X' : '-',
+    sr = env->sr | cpu_m68k_flush_flags(env, env->cc_op) | env->cc_x * CCF_X;
+    cpu_fprintf (f, "SR = %04x %c%c%c%c%c ", sr, (sr & CCF_X) ? 'X' : '-',
                  (sr & CCF_N) ? 'N' : '-', (sr & CCF_Z) ? 'Z' : '-',
                  (sr & CCF_V) ? 'V' : '-', (sr & CCF_C) ? 'C' : '-');
     cpu_fprintf (f, "FPRESULT = %12g\n", *(double *)&env->fp_result);
Signature · 1 line
2.4.3
#2 lore raw

PATCH 2/11 target-m68k: Some fixes to SR and flags management

In reply to Richard Henderson
---
 target-m68k/cpu.h       |  3 ++-
 target-m68k/helper.c    | 17 +++++++++++++++--
 target-m68k/op_helper.c |  5 +++--
 target-m68k/translate.c |  2 +-
 4 files changed, 21 insertions(+), 6 deletions(-)
target-m68k/cpu.h +2 -1
index 9708615..4f0d053 100644
@@ -123,7 +123,8 @@ int cpu_m68k_exec(CPUState *cpu);
    is returned if the signal was handled by the virtual CPU.  */
 int cpu_m68k_signal_handler(int host_signum, void *pinfo,
                            void *puc);
-uint32_t cpu_m68k_flush_flags(CPUM68KState *env, int op);
+uint32_t cpu_m68k_get_ccr(CPUM68KState *env);
+void cpu_m68k_set_ccr(CPUM68KState *env, uint32_t);
 
 typedef enum {
     CC_OP_DYNAMIC, /* Use env->cc_op  */
target-m68k/helper.c +15 -2
index 2185041..c7b0fbe 100644
@@ -130,7 +130,7 @@ void m68k_cpu_init_gdb(M68kCPU *cpu)
     /* TODO: Add [E]MAC registers.  */
 }
 
-uint32_t cpu_m68k_flush_flags(CPUM68KState *env, int op)
+static uint32_t cpu_m68k_flush_flags(CPUM68KState *env, int op)
 {
     int flags;
     uint32_t src;
@@ -270,6 +270,18 @@ set_x:
     return flags;
 }
 
+uint32_t cpu_m68k_get_ccr(CPUM68KState *env)
+{
+    return cpu_m68k_flush_flags(env, env->cc_op) | env->cc_x * CCF_X;
+}
+
+void cpu_m68k_set_ccr(CPUM68KState *env, uint32_t val)
+{
+    env->cc_op = CC_OP_FLAGS;
+    env->cc_dest = val & 0xf;
+    env->cc_x = (val & CCF_X ? 1 : 0);
+}
+
 void HELPER(movec)(CPUM68KState *env, uint32_t reg, uint32_t val)
 {
     M68kCPU *cpu = m68k_env_get_cpu(env);
@@ -467,7 +479,8 @@ uint32_t HELPER(xflag_lt)(uint32_t a, uint32_t b)
 
 void HELPER(set_sr)(CPUM68KState *env, uint32_t val)
 {
-    env->sr = val & 0xffff;
+    env->sr = val & 0xffe0;
+    cpu_m68k_set_ccr(env, val);
     m68k_switch_sp(env);
 }
 
target-m68k/op_helper.c +3 -2
index 1af0ca6..29032c6 100644
@@ -61,9 +61,9 @@ static void do_rte(CPUM68KState *env)
     fmt = cpu_ldl_kernel(env, sp);
     env->pc = cpu_ldl_kernel(env, sp + 4);
     sp |= (fmt >> 28) & 3;
-    env->sr = fmt & 0xffff;
     env->aregs[7] = sp + 8;
-    m68k_switch_sp(env);
+
+    helper_set_sr(env, fmt);
 }
 
 static void do_interrupt_all(CPUM68KState *env, int is_hw)
@@ -110,6 +110,7 @@ static void do_interrupt_all(CPUM68KState *env, int is_hw)
     fmt |= 0x40000000;
     fmt |= vector << 16;
     fmt |= env->sr;
+    fmt |= cpu_m68k_get_ccr(env);
 
     env->sr |= SR_S;
     if (is_hw) {
target-m68k/translate.c +1 -1
index c000d0a..2c720a2 100644
@@ -3236,7 +3236,7 @@ void m68k_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
                      i, u.l.upper, u.l.lower, *(double *)&u.d);
       }
     cpu_fprintf (f, "PC = %08x   ", env->pc);
-    sr = env->sr | cpu_m68k_flush_flags(env, env->cc_op) | env->cc_x * CCF_X;
+    sr = env->sr | cpu_m68k_get_ccr(env);
     cpu_fprintf (f, "SR = %04x %c%c%c%c%c ", sr, (sr & CCF_X) ? 'X' : '-',
                  (sr & CCF_N) ? 'N' : '-', (sr & CCF_Z) ? 'Z' : '-',
                  (sr & CCF_V) ? 'V' : '-', (sr & CCF_C) ? 'C' : '-');
Signature · 1 line
2.4.3
#3 lore raw

PATCH 3/11 target-m68k: Remove incorrect clearing of cc_x

In reply to Richard Henderson

The CF docs certainly doesnt suggest this is true.

---
 target-m68k/helper.c | 7 -------
 1 file changed, 7 deletions(-)
target-m68k/helper.c +0 -7
index c7b0fbe..6feb4bf 100644
@@ -205,19 +205,12 @@ static uint32_t cpu_m68k_flush_flags(CPUM68KState *env, int op)
         break;
     case CC_OP_LOGICB:
         SET_NZ(dest, int8_t);
-        goto set_x;
         break;
     case CC_OP_LOGICW:
         SET_NZ(dest, int16_t);
-        goto set_x;
         break;
     case CC_OP_LOGIC:
         SET_NZ(dest, int32_t);
-set_x:
-        if (!m68k_feature(env, M68K_FEATURE_M68000)) {
-            /* Unlike m68k, coldfire always clears the overflow bit.  */
-            env->cc_x = 0;
-        }
         break;
     case CC_OP_ADDB:
         SET_FLAGS_ADD(int8_t, uint8_t);
Signature · 1 line
2.4.3
#7 lore raw

PATCH 4/11 target-m68k: Replace helper_xflag_lt with setcond

In reply to Richard Henderson
---
 target-m68k/helper.c    |  5 -----
 target-m68k/helper.h    |  1 -
 target-m68k/translate.c | 14 +++++++-------
 3 files changed, 7 insertions(+), 13 deletions(-)
target-m68k/helper.c +0 -5
index 6feb4bf..a032947 100644
@@ -465,11 +465,6 @@ uint32_t HELPER(addx_cc)(CPUM68KState *env, uint32_t op1, uint32_t op2)
     return res;
 }
 
-uint32_t HELPER(xflag_lt)(uint32_t a, uint32_t b)
-{
-    return a < b;
-}
-
 void HELPER(set_sr)(CPUM68KState *env, uint32_t val)
 {
     env->sr = val & 0xffe0;
target-m68k/helper.h +0 -1
index 81c8e79..0f5a7cf 100644
@@ -8,7 +8,6 @@ DEF_HELPER_3(subx_cc, i32, env, i32, i32)
 DEF_HELPER_3(shl_cc, i32, env, i32, i32)
 DEF_HELPER_3(shr_cc, i32, env, i32, i32)
 DEF_HELPER_3(sar_cc, i32, env, i32, i32)
-DEF_HELPER_2(xflag_lt, i32, i32, i32)
 DEF_HELPER_2(set_sr, void, env, i32)
 DEF_HELPER_3(movec, void, env, i32, i32)
 
target-m68k/translate.c +7 -7
index 2c720a2..cf6a54c 100644
@@ -1102,10 +1102,10 @@ DISAS_INSN(addsub)
     }
     if (add) {
         tcg_gen_add_i32(dest, tmp, src);
-        gen_helper_xflag_lt(QREG_CC_X, dest, src);
+        tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, dest, src);
         set_cc_op(s, CC_OP_ADD);
     } else {
-        gen_helper_xflag_lt(QREG_CC_X, tmp, src);
+        tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, tmp, src);
         tcg_gen_sub_i32(dest, tmp, src);
         set_cc_op(s, CC_OP_SUB);
     }
@@ -1317,7 +1317,7 @@ DISAS_INSN(arith_im)
         break;
     case 2: /* subi */
         tcg_gen_mov_i32(dest, src1);
-        gen_helper_xflag_lt(QREG_CC_X, dest, tcg_const_i32(im));
+        tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, dest, tcg_const_i32(im));
         tcg_gen_subi_i32(dest, dest, im);
         gen_update_cc_add(dest, tcg_const_i32(im));
         set_cc_op(s, CC_OP_SUB);
@@ -1326,7 +1326,7 @@ DISAS_INSN(arith_im)
         tcg_gen_mov_i32(dest, src1);
         tcg_gen_addi_i32(dest, dest, im);
         gen_update_cc_add(dest, tcg_const_i32(im));
-        gen_helper_xflag_lt(QREG_CC_X, dest, tcg_const_i32(im));
+        tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, dest, tcg_const_i32(im));
         set_cc_op(s, CC_OP_ADD);
         break;
     case 5: /* eori */
@@ -1456,7 +1456,7 @@ DISAS_INSN(neg)
     tcg_gen_neg_i32(reg, src1);
     set_cc_op(s, CC_OP_SUB);
     gen_update_cc_add(reg, src1);
-    gen_helper_xflag_lt(QREG_CC_X, tcg_const_i32(0), src1);
+    tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, tcg_const_i32(0), src1);
     set_cc_op(s, CC_OP_SUB);
 }
 
@@ -1696,12 +1696,12 @@ DISAS_INSN(addsubq)
     } else {
         src2 = tcg_const_i32(val);
         if (insn & 0x0100) {
-            gen_helper_xflag_lt(QREG_CC_X, dest, src2);
+            tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, dest, src2);
             tcg_gen_subi_i32(dest, dest, val);
             set_cc_op(s, CC_OP_SUB);
         } else {
             tcg_gen_addi_i32(dest, dest, val);
-            gen_helper_xflag_lt(QREG_CC_X, dest, src2);
+            tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, dest, src2);
             set_cc_op(s, CC_OP_ADD);
         }
         gen_update_cc_add(dest, src2);
Signature · 1 line
2.4.3
#10 lore raw

PATCH 7/11 target-m68k: Use setcond for scc

In reply to Richard Henderson
---
 target-m68k/translate.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
target-m68k/translate.c +11 -9
index ce48e2a..28c3e1e 100644
@@ -886,19 +886,21 @@ static void gen_jmpcc(DisasContext *s, int cond, TCGLabel *l1)
 
 DISAS_INSN(scc)
 {
-    TCGLabel *l1;
+    DisasCompare c;
     int cond;
-    TCGv reg;
+    TCGv reg, tmp;
 
-    l1 = gen_new_label();
     cond = (insn >> 8) & 0xf;
+    gen_cc_cond(&c, s, cond);
+
+    tmp = tcg_temp_new();
+    tcg_gen_setcond_i32(c.tcond, tmp, c.v1, c.v2);
+    free_cond(&c);
+
     reg = DREG(insn, 0);
-    tcg_gen_andi_i32(reg, reg, 0xffffff00);
-    /* This is safe because we modify the reg directly, with no other values
-       live.  */
-    gen_jmpcc(s, cond ^ 1, l1);
-    tcg_gen_ori_i32(reg, reg, 0xff);
-    gen_set_label(l1);
+    tcg_gen_neg_i32(tmp, tmp);
+    tcg_gen_deposit_i32(reg, reg, tmp, 0, 8);
+    tcg_temp_free(tmp);
 }
 
 /* Force a TB lookup after an instruction that changes the CPU state.  */
Signature · 1 line
2.4.3
#11 lore raw

PATCH 8/11 target-m68k: Optimize some comparisons

In reply to Richard Henderson
---
 target-m68k/translate.c | 108 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 102 insertions(+), 6 deletions(-)
target-m68k/translate.c +102 -6
index 28c3e1e..6f60c6f 100644
@@ -793,10 +793,43 @@ static void gen_cc_cond(DisasCompare *c, DisasContext *s, int cond)
 {
     TCGv tmp, tmp2;
     TCGCond tcond;
+    CCOp op = s->cc_op;
 
-    /* TODO: Optimize compare/branch pairs rather than always flushing
-       flag state to CC_OP_FLAGS.  */
-    gen_flush_flags(s);
+    /* The CC_OP_CMP form can handle most normal comparisons directly.  */
+    if (op == CC_OP_CMP) {
+        c->g1 = c->g2 = 1;
+        c->v1 = QREG_CC_N;
+        c->v2 = QREG_CC_V;
+        switch (cond) {
+        case 2: /* HI */
+        case 3: /* LS */
+            tcond = TCG_COND_LEU;
+            goto done;
+        case 4: /* CC */
+        case 5: /* CS */
+            tcond = TCG_COND_LTU;
+            goto done;
+        case 6: /* NE */
+        case 7: /* EQ */
+            tcond = TCG_COND_EQ;
+            goto done;
+        case 10: /* PL */
+        case 11: /* MI */
+            c->g1 = c->g2 = 0;
+            c->v2 = tcg_const_i32(0);
+            c->v1 = tmp = tcg_temp_new();
+            tcg_gen_sub_i32(tmp, QREG_CC_N, QREG_CC_V);
+            /* fallthru */
+        case 12: /* GE */
+        case 13: /* LT */
+            tcond = TCG_COND_LT;
+            goto done;
+        case 14: /* GT */
+        case 15: /* LE */
+            tcond = TCG_COND_LE;
+            goto done;
+        }
+    }
 
     c->g1 = 1;
     c->g2 = 0;
@@ -807,7 +840,71 @@ static void gen_cc_cond(DisasCompare *c, DisasContext *s, int cond)
     case 1: /* F */
         c->v1 = c->v2;
         tcond = TCG_COND_NEVER;
+        goto done;
+    case 14: /* GT (!(Z || (N ^ V))) */
+    case 15: /* LE (Z || (N ^ V)) */
+        /* Logic operations clear V, which simplifies LE to (Z || N),
+           and since Z and N are co-located, this becomes a normal
+           comparison vs N.  */
+        if (op == CC_OP_LOGIC) {
+            c->v1 = QREG_CC_N;
+            tcond = TCG_COND_LE;
+            goto done;
+        }
+        break;
+    case 12: /* GE (!(N ^ V)) */
+    case 13: /* LT (N ^ V) */
+        /* Logic operations clear V, which simplifies this to N.  */
+        if (op != CC_OP_LOGIC) {
+            break;
+        }
+        /* fallthru */
+    case 10: /* PL (!N) */
+    case 11: /* MI (N) */
+        /* Several cases represent N normally.  */
+        if (op == CC_OP_ADD || op == CC_OP_SUB || op == CC_OP_LOGIC) {
+            c->v1 = QREG_CC_N;
+            tcond = TCG_COND_LT;
+            goto done;
+        }
+        break;
+    case 6: /* NE (!Z) */
+    case 7: /* EQ (Z) */
+        /* Some cases fold Z into N.  */
+        if (op == CC_OP_ADD || op == CC_OP_SUB || op == CC_OP_LOGIC) {
+            tcond = TCG_COND_EQ;
+            c->v1 = QREG_CC_N;
+            goto done;
+        }
         break;
+    case 4: /* CC (!C) */
+    case 5: /* CS (C) */
+        /* Some cases fold C into X.  */
+        if (op == CC_OP_ADD || op == CC_OP_SUB) {
+            tcond = TCG_COND_NE;
+            c->v1 = QREG_CC_X;
+            goto done;
+        }
+        /* fallthru */
+    case 8: /* VC (!V) */
+    case 9: /* VS (V) */
+        /* Logic operations clear V and C.  */
+        if (op == CC_OP_LOGIC) {
+            tcond = TCG_COND_NEVER;
+            c->v2 = c->v1;
+        }
+        break;
+    }
+
+    /* Otherwise, flush flag state to CC_OP_FLAGS.  */
+    gen_flush_flags(s);
+
+    switch (cond) {
+    case 0: /* T */
+    case 1: /* F */
+    default:
+        /* Invalid, or handled above.  */
+        abort();
     case 2: /* HI (!C && !Z) -> !(C || Z)*/
     case 3: /* LS (C || Z) */
         c->v1 = tmp = tcg_temp_new();
@@ -855,10 +952,9 @@ static void gen_cc_cond(DisasCompare *c, DisasContext *s, int cond)
         tcg_temp_free(tmp2);
         tcond = TCG_COND_LT;
         break;
-    default:
-        /* Should ever happen.  */
-        abort();
     }
+
+ done:
     if ((cond & 1) == 0) {
         tcond = tcg_invert_cond(tcond);
     }
Signature · 1 line
2.4.3
#12 lore raw

PATCH 9/11 target-m68k: Optimize gen_flush_flags

In reply to Richard Henderson
---
 target-m68k/translate.c | 56 +++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)
target-m68k/translate.c +52 -4
index 6f60c6f..19097c2 100644
@@ -461,18 +461,66 @@ static TCGv gen_lea_indexed(CPUM68KState *env, DisasContext *s, TCGv base)
 
 static void gen_flush_flags(DisasContext *s)
 {
-    TCGv tmp;
+    TCGv t0, t1;
 
     switch (s->cc_op) {
     case CC_OP_FLAGS:
         return;
+
+    case CC_OP_ADD:
+        tcg_gen_mov_i32(QREG_CC_C, QREG_CC_X);
+        tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
+        /* Compute signed overflow for addition.  */
+        t0 = tcg_temp_new();
+        t1 = tcg_temp_new();
+        tcg_gen_sub_i32(t0, QREG_CC_N, QREG_CC_V);
+        tcg_gen_xor_i32(t1, QREG_CC_N, QREG_CC_V);
+        tcg_gen_xor_i32(QREG_CC_V, QREG_CC_V, t0);
+        tcg_temp_free(t0);
+        tcg_gen_andc_i32(QREG_CC_V, t1, QREG_CC_V);
+        tcg_temp_free(t1);
+        break;
+
+    case CC_OP_SUB:
+        tcg_gen_mov_i32(QREG_CC_C, QREG_CC_X);
+        tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
+        /* Compute signed overflow for subtraction.  */
+        t0 = tcg_temp_new();
+        t1 = tcg_temp_new();
+        tcg_gen_add_i32(t0, QREG_CC_N, QREG_CC_V);
+        tcg_gen_xor_i32(t1, QREG_CC_N, QREG_CC_V);
+        tcg_gen_xor_i32(QREG_CC_V, QREG_CC_V, t0);
+        tcg_temp_free(t0);
+        tcg_gen_and_i32(QREG_CC_V, QREG_CC_V, t1);
+        tcg_temp_free(t1);
+        break;
+
+    case CC_OP_CMP:
+        tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_C, QREG_CC_N, QREG_CC_V);
+        tcg_gen_sub_i32(QREG_CC_Z, QREG_CC_N, QREG_CC_V);
+        /* Compute signed overflow for subtraction.  */
+        t0 = tcg_temp_new();
+        tcg_gen_xor_i32(t0, QREG_CC_Z, QREG_CC_N);
+        tcg_gen_xor_i32(QREG_CC_V, QREG_CC_V, QREG_CC_N);
+        tcg_gen_and_i32(QREG_CC_V, QREG_CC_V, t0);
+        tcg_temp_free(t0);
+        tcg_gen_mov_i32(QREG_CC_N, QREG_CC_Z);
+        break;
+
+    case CC_OP_LOGIC:
+        tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
+        tcg_gen_movi_i32(QREG_CC_C, 0);
+        tcg_gen_movi_i32(QREG_CC_V, 0);
+        break;
+
     case CC_OP_DYNAMIC:
         gen_helper_flush_flags(cpu_env, QREG_CC_OP);
         break;
+
     default:
-        tmp = tcg_const_i32(s->cc_op);
-        gen_helper_flush_flags(cpu_env, tmp);
-        tcg_temp_free(tmp);
+        t0 = tcg_const_i32(s->cc_op);
+        gen_helper_flush_flags(cpu_env, t0);
+        tcg_temp_free(t0);
         break;
     }
 
Signature · 1 line
2.4.3
#13 lore raw

PATCH 10/11 target-m68k: Inline shifts

In reply to Richard Henderson
---
 target-m68k/helper.c    | 52 ---------------------------
 target-m68k/helper.h    |  3 --
 target-m68k/translate.c | 94 +++++++++++++++++++++++++++++++++++++------------
 3 files changed, 72 insertions(+), 77 deletions(-)
target-m68k/helper.c +0 -52
index ff7e481..6bd80a5 100644
@@ -322,58 +322,6 @@ void HELPER(set_sr)(CPUM68KState *env, uint32_t val)
     m68k_switch_sp(env);
 }
 
-uint32_t HELPER(shl_cc)(CPUM68KState *env, uint32_t val, uint32_t shift)
-{
-    uint64_t result;
-
-    shift &= 63;
-    result = (uint64_t)val << shift;
-
-    env->cc_c = (result >> 32) & 1;
-    env->cc_n = result;
-    env->cc_z = result;
-    env->cc_v = 0;
-    env->cc_x = shift ? env->cc_c : env->cc_x;
-
-    return result;
-}
-
-uint32_t HELPER(shr_cc)(CPUM68KState *env, uint32_t val, uint32_t shift)
-{
-    uint64_t temp;
-    uint32_t result;
-
-    shift &= 63;
-    temp = (uint64_t)val << 32 >> shift;
-    result = temp >> 32;
-
-    env->cc_c = (temp >> 31) & 1;
-    env->cc_n = result;
-    env->cc_z = result;
-    env->cc_v = 0;
-    env->cc_x = shift ? env->cc_c : env->cc_x;
-
-    return result;
-}
-
-uint32_t HELPER(sar_cc)(CPUM68KState *env, uint32_t val, uint32_t shift)
-{
-    uint64_t temp;
-    uint32_t result;
-
-    shift &= 63;
-    temp = (int64_t)val << 32 >> shift;
-    result = temp >> 32;
-
-    env->cc_c = (temp >> 31) & 1;
-    env->cc_n = result;
-    env->cc_z = result;
-    env->cc_v = result ^ val;
-    env->cc_x = shift ? env->cc_c : env->cc_x;
-
-    return result;
-}
-
 /* FPU helpers.  */
 uint32_t HELPER(f64_to_i32)(CPUM68KState *env, float64 val)
 {
target-m68k/helper.h +0 -3
index c868148..9985f9b 100644
@@ -5,9 +5,6 @@ DEF_HELPER_2(divu, void, env, i32)
 DEF_HELPER_2(divs, void, env, i32)
 DEF_HELPER_3(addx_cc, i32, env, i32, i32)
 DEF_HELPER_3(subx_cc, i32, env, i32, i32)
-DEF_HELPER_3(shl_cc, i32, env, i32, i32)
-DEF_HELPER_3(shr_cc, i32, env, i32, i32)
-DEF_HELPER_3(sar_cc, i32, env, i32, i32)
 DEF_HELPER_2(set_sr, void, env, i32)
 DEF_HELPER_3(movec, void, env, i32, i32)
 
target-m68k/translate.c +72 -22

120 lines not on this page · raw message

Signature · 1 line
2.4.3
#9 lore raw

PATCH 6/11 target-m68k: Introduce DisasCompare

In reply to Richard Henderson
---
 target-m68k/translate.c | 82 ++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 60 insertions(+), 22 deletions(-)
target-m68k/translate.c +60 -22
index b0cb39d..ce48e2a 100644
@@ -781,8 +781,15 @@ static TCGv gen_ea(CPUM68KState *env, DisasContext *s, uint16_t insn,
     return NULL_QREG;
 }
 
-/* This generates a conditional branch, clobbering all temporaries.  */
-static void gen_jmpcc(DisasContext *s, int cond, TCGLabel *l1)
+typedef struct {
+    TCGCond tcond;
+    bool g1;
+    bool g2;
+    TCGv v1;
+    TCGv v2;
+} DisasCompare;
+
+static void gen_cc_cond(DisasCompare *c, DisasContext *s, int cond)
 {
     TCGv tmp, tmp2;
     TCGCond tcond;
@@ -790,60 +797,91 @@ static void gen_jmpcc(DisasContext *s, int cond, TCGLabel *l1)
     /* TODO: Optimize compare/branch pairs rather than always flushing
        flag state to CC_OP_FLAGS.  */
     gen_flush_flags(s);
+
+    c->g1 = 1;
+    c->g2 = 0;
+    c->v2 = tcg_const_i32(0);
+
     switch (cond) {
     case 0: /* T */
-        tcg_gen_br(l1);
-        return;
     case 1: /* F */
-        return;
+        c->v1 = c->v2;
+        tcond = TCG_COND_NEVER;
+        break;
     case 2: /* HI (!C && !Z) -> !(C || Z)*/
     case 3: /* LS (C || Z) */
-        tmp = tcg_temp_new();
-        tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, QREG_CC_Z, 0);
+        c->v1 = tmp = tcg_temp_new();
+        c->g1 = 0;
+        tcg_gen_setcond_i32(TCG_COND_EQ, tmp, QREG_CC_Z, c->v2);
         tcg_gen_or_i32(tmp, tmp, QREG_CC_C);
-        tcond = (cond & 1 ? TCG_COND_NE: TCG_COND_EQ);
+        tcond = TCG_COND_NE;
         break;
     case 4: /* CC (!C) */
     case 5: /* CS (C) */
-        tmp = QREG_CC_C;
-        tcond = (cond & 1 ? TCG_COND_NE : TCG_COND_EQ);
+        c->v1 = QREG_CC_C;
+        tcond = TCG_COND_NE;
         break;
     case 6: /* NE (!Z) */
     case 7: /* EQ (Z) */
-        tmp = QREG_CC_Z;
-        tcond = (cond & 1 ? TCG_COND_EQ : TCG_COND_NE);
+        c->v1 = QREG_CC_Z;
+        tcond = TCG_COND_EQ;
         break;
     case 8: /* VC (!V) */
     case 9: /* VS (V) */
-        tmp = QREG_CC_V;
-        tcond = (cond & 1 ? TCG_COND_LT : TCG_COND_GE);
+        c->v1 = QREG_CC_V;
+        tcond = TCG_COND_LT;
         break;
     case 10: /* PL (!N) */
     case 11: /* MI (N) */
-        tmp = QREG_CC_N;
-        tcond = (cond & 1 ? TCG_COND_LT : TCG_COND_GE);
+        c->v1 = QREG_CC_N;
+        tcond = TCG_COND_LT;
         break;
     case 12: /* GE (!(N ^ V)) */
     case 13: /* LT (N ^ V) */
-        tmp = tcg_temp_new();
+        c->v1 = tmp = tcg_temp_new();
+        c->g1 = 0;
         tcg_gen_xor_i32(tmp, QREG_CC_N, QREG_CC_V);
-        tcond = (cond & 1 ? TCG_COND_LT : TCG_COND_GE);
+        tcond = TCG_COND_LT;
         break;
     case 14: /* GT (!(Z || (N ^ V))) */
     case 15: /* LE (Z || (N ^ V)) */
-        tmp = tcg_temp_new();
-        tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, QREG_CC_Z, 0);
+        c->v1 = tmp = tcg_temp_new();
+        c->g1 = 0;
+        tcg_gen_setcond_i32(TCG_COND_EQ, tmp, QREG_CC_Z, c->v2);
         tcg_gen_neg_i32(tmp, tmp);
         tmp2 = tcg_temp_new();
         tcg_gen_xor_i32(tmp2, QREG_CC_N, QREG_CC_V);
         tcg_gen_or_i32(tmp, tmp, tmp2);
-        tcond = (cond & 1 ? TCG_COND_LT : TCG_COND_GE);
+        tcg_temp_free(tmp2);
+        tcond = TCG_COND_LT;
         break;
     default:
         /* Should ever happen.  */
         abort();
     }
-  tcg_gen_brcondi_i32(tcond, tmp, 0, l1);
+    if ((cond & 1) == 0) {
+        tcond = tcg_invert_cond(tcond);
+    }
+    c->tcond = tcond;
+}
+
+static void free_cond(DisasCompare *c)
+{
+    if (!c->g1) {
+        tcg_temp_free(c->v1);
+    }
+    if (!c->g2) {
+        tcg_temp_free(c->v2);
+    }
+}
+
+static void gen_jmpcc(DisasContext *s, int cond, TCGLabel *l1)
+{
+  DisasCompare c;
+
+  gen_cc_cond(&c, s, cond);
+  tcg_gen_brcond_i32(c.tcond, c.v1, c.v2, l1);
+  free_cond(&c);
 }
 
 DISAS_INSN(scc)
Signature · 1 line
2.4.3
#14 lore raw

PATCH 11/11 target-m68k: Inline addx, subx, negx

In reply to Richard Henderson
---
 target-m68k/helper.c    | 40 -----------------------------
 target-m68k/helper.h    |  2 --
 target-m68k/translate.c | 67 +++++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 56 insertions(+), 53 deletions(-)
target-m68k/helper.c +0 -40
index 6bd80a5..519eef3 100644
@@ -275,46 +275,6 @@ uint32_t HELPER(sats)(uint32_t val, uint32_t v)
     return val;
 }
 
-uint32_t HELPER(subx_cc)(CPUM68KState *env, uint32_t op1, uint32_t op2)
-{
-    uint32_t res, new_x;
-
-    if (env->cc_x) {
-        new_x = (op1 <= op2);
-        res = op1 - (op2 + 1);
-    } else {
-        new_x = (op1 < op2);
-        res = op1 - op2;
-    }
-    env->cc_x = new_x;
-    env->cc_c = new_x;
-    env->cc_n = res;
-    env->cc_z |= res; /* !Z is sticky */
-    env->cc_v = (res ^ op1) & (op1 ^ op2);
-
-    return res;
-}
-
-uint32_t HELPER(addx_cc)(CPUM68KState *env, uint32_t op1, uint32_t op2)
-{
-    uint32_t res, new_x;
-
-    if (env->cc_x) {
-        res = op1 + op2 + 1;
-        new_x = (res <= op2);
-    } else {
-        res = op1 + op2;
-        new_x = (res < op2);
-    }
-    env->cc_x = new_x;
-    env->cc_c = new_x;
-    env->cc_n = res;
-    env->cc_z |= res; /* !Z is sticky.  */
-    env->cc_v = (res ^ op1) & ~(op1 ^ op2);
-
-    return res;
-}
-
 void HELPER(set_sr)(CPUM68KState *env, uint32_t val)
 {
     env->sr = val & 0xffe0;
target-m68k/helper.h +0 -2
index 9985f9b..aae01f9 100644
@@ -3,8 +3,6 @@ DEF_HELPER_1(ff1, i32, i32)
 DEF_HELPER_FLAGS_2(sats, TCG_CALL_NO_RWG_SE, i32, i32, i32)
 DEF_HELPER_2(divu, void, env, i32)
 DEF_HELPER_2(divs, void, env, i32)
-DEF_HELPER_3(addx_cc, i32, env, i32, i32)
-DEF_HELPER_3(subx_cc, i32, env, i32, i32)
 DEF_HELPER_2(set_sr, void, env, i32)
 DEF_HELPER_3(movec, void, env, i32, i32)
 
target-m68k/translate.c +56 -11

93 lines not on this page · raw message

Signature · 1 line
2.4.3
#8 lore raw

PATCH 5/11 target-m68k: Reorg flags handling

In reply to Richard Henderson

Separate all ccr bits. Continue to batch updates via cc_op.

---
 target-m68k/cpu.c       |   2 +-
 target-m68k/cpu.h       |  46 +++---
 target-m68k/helper.c    | 400 +++++++++++++++++++++---------------------------
 target-m68k/helper.h    |   6 +-
 target-m68k/op_helper.c |  30 ++--
 target-m68k/qregs.def   |   6 +-
 target-m68k/translate.c | 376 +++++++++++++++++++--------------------------
 7 files changed, 379 insertions(+), 487 deletions(-)
target-m68k/cpu.c +1 -1
index d4d8c12..5bdee93 100644
@@ -55,7 +55,7 @@ static void m68k_cpu_reset(CPUState *s)
 #endif
     m68k_switch_sp(env);
     /* ??? FP regs should be initialized to NaN.  */
-    env->cc_op = CC_OP_FLAGS;
+    cpu_m68k_set_ccr(env, 0);
     /* TODO: We should set PC from the interrupt vector.  */
     env->pc = 0;
     tlb_flush(s, 1);
target-m68k/cpu.h +22 -24

58 lines not on this page · raw message

target-m68k/helper.c +177 -223

467 lines not on this page · raw message

target-m68k/helper.h +4 -2
index 0f5a7cf..c868148 100644
@@ -1,6 +1,6 @@
 DEF_HELPER_1(bitrev, i32, i32)
 DEF_HELPER_1(ff1, i32, i32)
-DEF_HELPER_2(sats, i32, i32, i32)
+DEF_HELPER_FLAGS_2(sats, TCG_CALL_NO_RWG_SE, i32, i32, i32)
 DEF_HELPER_2(divu, void, env, i32)
 DEF_HELPER_2(divs, void, env, i32)
 DEF_HELPER_3(addx_cc, i32, env, i32, i32)
@@ -45,5 +45,7 @@ DEF_HELPER_3(set_mac_extf, void, env, i32, i32)
 DEF_HELPER_3(set_mac_exts, void, env, i32, i32)
 DEF_HELPER_3(set_mac_extu, void, env, i32, i32)
 
-DEF_HELPER_2(flush_flags, i32, env, i32)
+DEF_HELPER_2(flush_flags, void, env, i32)
+DEF_HELPER_2(set_ccr, void, env, i32)
+DEF_HELPER_FLAGS_1(get_ccr, TCG_CALL_NO_WG_SE, i32, env)
 DEF_HELPER_2(raise_exception, void, env, i32)
target-m68k/op_helper.c +12 -18
index 29032c6..6203ae9 100644
@@ -183,7 +183,6 @@ void HELPER(divu)(CPUM68KState *env, uint32_t word)
     uint32_t den;
     uint32_t quot;
     uint32_t rem;
-    uint32_t flags;
 
     num = env->div1;
     den = env->div2;
@@ -193,16 +192,14 @@ void HELPER(divu)(CPUM68KState *env, uint32_t word)
     }
     quot = num / den;
     rem = num % den;
-    flags = 0;
-    if (word && quot > 0xffff)
-        flags |= CCF_V;
-    if (quot == 0)
-        flags |= CCF_Z;
-    else if ((int32_t)quot < 0)
-        flags |= CCF_N;
+
+    env->cc_v = (word && quot > 0xffff ? -1 : 0);
+    env->cc_z = quot;
+    env->cc_n = quot;
+    env->cc_c = 0;
+
     env->div1 = quot;
     env->div2 = rem;
-    env->cc_dest = flags;
 }
 
 void HELPER(divs)(CPUM68KState *env, uint32_t word)
@@ -211,7 +208,6 @@ void HELPER(divs)(CPUM68KState *env, uint32_t word)
     int32_t den;
     int32_t quot;
     int32_t rem;
-    int32_t flags;
 
     num = env->div1;
     den = env->div2;
@@ -220,14 +216,12 @@ void HELPER(divs)(CPUM68KState *env, uint32_t word)
     }
     quot = num / den;
     rem = num % den;
-    flags = 0;
-    if (word && quot != (int16_t)quot)
-        flags |= CCF_V;
-    if (quot == 0)
-        flags |= CCF_Z;
-    else if (quot < 0)
-        flags |= CCF_N;
+
+    env->cc_v = (word && quot != (int16_t)quot ? -1 : 0);
+    env->cc_z = quot;
+    env->cc_n = quot;
+    env->cc_c = 0;
+
     env->div1 = quot;
     env->div2 = rem;
-    env->cc_dest = flags;
 }
target-m68k/qregs.def +4 -2
index 204663e..156c0f5 100644
@@ -2,9 +2,11 @@ DEFF64(FP_RESULT, fp_result)
 DEFO32(PC, pc)
 DEFO32(SR, sr)
 DEFO32(CC_OP, cc_op)
-DEFO32(CC_DEST, cc_dest)
-DEFO32(CC_SRC, cc_src)
 DEFO32(CC_X, cc_x)
+DEFO32(CC_C, cc_c)
+DEFO32(CC_N, cc_n)
+DEFO32(CC_V, cc_v)
+DEFO32(CC_Z, cc_z)
 DEFO32(DIV1, div1)
 DEFO32(DIV2, div2)
 DEFO32(MACSR, macsr)
target-m68k/translate.c +159 -217

669 lines not on this page · raw message

Signature · 1 line
2.4.3
#15 lore raw
In reply to Richard Henderson
Quoted 12 lines · Richard Henderson

As promised a couple of days ago, with the addition of CC_OP_CMP, which wasn't in the text of my proposal the other day. From the looks of the generated code, I believe this is ideal.

The following is based on Laurent's 8/30 Update cpu flags management.

FWIW, there's something in the last patch here that breaks the coldfire kernel I've been testing (it may even be a bug in tcg; the problem only appears well into the boot process). But I'm about to go away for the weekend and still wanted to include it to show what can be done.

Quoted 4 lines

For convenience, the complete tree pushed to

git://github.com/rth7680/qemu.git tgt-m68k

Thanks, I'll try to review and test this this week-end.

Laurent

#4 lore raw

Re: [Qemu-devel] [PATCH 03/11] target-m68k: Remove incorrect clearing of cc_x

In reply to Richard Henderson
Quoted 16 lines · Richard Henderson

The CF docs certainly doesnt suggest this is true.

---
 target-m68k/helper.c | 7 -------
 1 file changed, 7 deletions(-)
target-m68k/helper.c +0 -1
index c7b0fbe..6feb4bf 100644
@@ -205,19 +205,12 @@ static uint32_t cpu_m68k_flush_flags(CPUM68KState *env, int op)
         break;
     case CC_OP_LOGICB:
         SET_NZ(dest, int8_t);
-        goto set_x;

target-m68k/helper.c static uint32_t cpu_m68k_flush_flags(CPUM68KState *env, int op) · line 218

Quoted 10 lines
target-m68k/helper.c +0 -4
         break;
     case CC_OP_LOGICW:
         SET_NZ(dest, int16_t);
-        goto set_x;
         break;
     case CC_OP_LOGIC:
         SET_NZ(dest, int32_t);
-set_x:
-        if (!m68k_feature(env, M68K_FEATURE_M68000)) {
-            /* Unlike m68k, coldfire always clears the overflow bit.  */

I think this refers to the muls/mulu insn, where the CF always clears V.

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#5 lore raw

Re: [Qemu-devel] [PATCH 03/11] target-m68k: Remove incorrect clearing of cc_x

Quoted 23 lines · Andreas Schwab
Quoted 20 lines · Richard Henderson

The CF docs certainly doesnt suggest this is true.

---
  target-m68k/helper.c | 7 -------

  1 file changed, 7 deletions(-) 
target-m68k/helper.c +0 -0
index c7b0fbe..6feb4bf 100644 
@@ -205,19 +205,12 @@ static uint32_t cpu_m68k_flush_flags(CPUM68KState *env, int op) 
           break; 

      case CC_OP_LOGICB:
          SET_NZ(dest, int8_t);
-        goto set_x;
          break;
      case CC_OP_LOGICW:
          SET_NZ(dest, int16_t);
-        goto set_x;

Quoted 8 lines
Quoted 6 lines

          break;
      case CC_OP_LOGIC:
          SET_NZ(dest, int32_t);
-set_x:
-        if (!m68k_feature(env, M68K_FEATURE_M68000)) {
-            /* Unlike m68k, coldfire always clears the overflow bit.  */

I think this refers to the muls/mulu insn, where the CF always clears V.

Ah. Except placed here it also applies to and/or/move too.

r~

#6 lore raw

Re: [Qemu-devel] [PATCH 03/11] target-m68k: Remove incorrect clearing of cc_x

Quoted 1 line · Richard Henderson

Ah. Except placed here it also applies to and/or/move too.

Well, none of those ever overflow in the first place.

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."