680x0 mul and div instructions
v2 cover 680x0 mul and div instructions
This series is another subset of the series I sent in May:
https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg00501.html
It must be applied on top of series:
"target-m68k: 680x0 instruction set, part 2"
This subset contains reworked patches of mul and div instructions:
- "add 64bit mull": correctly set QREG_CC_V,
- "inline divu/divs": don't inline divu/divs, but update existing functions to manage 680x0 div instructions
I've checked it doesn't break coldfire support:
http://wiki.qemu.org/download/coldfire-test-0.1.tar.bz2
but it can't boot a 680x0 processor kernel.
v2:
- Free "rem" and "quot" in "divl".
Laurent Vivier (2):
target-m68k: add 64bit mull
target-m68k: add 680x0 divu/divs variants
linux-user/main.c | 7 ++ target-m68k/cpu.h | 4 - target-m68k/helper.h | 6 +- target-m68k/op_helper.c | 102 +++++++++++++++++------ target-m68k/qregs.def | 2 - target-m68k/translate.c | 214 ++++++++++++++++++++++++++++++++++++++---------- 6 files changed, 259 insertions(+), 76 deletions(-)
Signature · 1 line
2.7.4
PATCH v2 1/2 target-m68k: add 64bit mull
--- target-m68k/translate.c | 60 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 12 deletions(-)
target-m68k/translate.c +48 -12
@@ -1812,24 +1812,60 @@ DISAS_INSN(tas)DISAS_INSN (mull) {uint16_t ext; -TCGv reg;TCGv src1; -TCGv dest; +int sign; -/* The upper 32 bits of the product are discarded, so -muls.l and mulu.l are functionally equivalent. */ ext =read_im16 (env, s); -if (ext &0x87ff ) { -gen_exception (s, s->pc -4 ,EXCP_UNSUPPORTED ); + + sign = ext &0x800 ; + +if (ext &0x400 ) { +if (!m68k_feature (s->env,M68K_FEATURE_QUAD_MULDIV )) { +gen_exception (s, s->pc -4 ,EXCP_UNSUPPORTED ); +return ; + } + +SRC_EA (env, src1,OS_LONG ,0 ,NULL ); + +if (sign) { +tcg_gen_muls2_i32 (DREG (ext,12 ),DREG (ext,0 ), src1,DREG (ext,12 )); + }else { +tcg_gen_mulu2_i32 (DREG (ext,12 ),DREG (ext,0 ), src1,DREG (ext,12 )); + } + +tcg_gen_movi_i32 (QREG_CC_V ,0 ); +tcg_gen_mov_i32 (QREG_CC_C ,QREG_CC_V ); +tcg_gen_mov_i32 (QREG_CC_N ,DREG (ext,0 )); +tcg_gen_or_i32 (QREG_CC_Z ,DREG (ext,12 ),DREG (ext,0 )); + +set_cc_op (s,CC_OP_FLAGS );return ; } - reg =DREG (ext,12 );SRC_EA (env, src1,OS_LONG ,0 ,NULL ); - dest =tcg_temp_new (); -tcg_gen_mul_i32 (dest, src1, reg); -tcg_gen_mov_i32 (reg, dest); -/* Unlike m68k, coldfire always clears the overflow bit. */ -gen_logic_cc (s, dest,OS_LONG ); +if (m68k_feature (s->env,M68K_FEATURE_M68000 )) { +tcg_gen_movi_i32 (QREG_CC_C ,0 ); +if (sign) { +tcg_gen_muls2_i32 (QREG_CC_N ,QREG_CC_V , src1,DREG (ext,12 )); +/* QREG_CC_V is -(QREG_CC_V != (QREG_CC_N >> 31)) */ +tcg_gen_sari_i32 (QREG_CC_Z ,QREG_CC_N ,31 ); +tcg_gen_setcond_i32 (TCG_COND_NE ,QREG_CC_V ,QREG_CC_V ,QREG_CC_Z ); + }else { +tcg_gen_mulu2_i32 (QREG_CC_N ,QREG_CC_V , src1,DREG (ext,12 )); +/* QREG_CC_V is -(QREG_CC_V != 0), use QREG_CC_C as 0 */ +tcg_gen_setcond_i32 (TCG_COND_NE ,QREG_CC_V ,QREG_CC_V ,QREG_CC_C ); + } +tcg_gen_neg_i32 (QREG_CC_V ,QREG_CC_V ); +tcg_gen_mov_i32 (DREG (ext,12 ),QREG_CC_N ); + +tcg_gen_mov_i32 (QREG_CC_Z ,QREG_CC_N ); + +set_cc_op (s,CC_OP_FLAGS ); + }else { +/* The upper 32 bits of the product are discarded, so +muls.l and mulu.l are functionally equivalent. */ +tcg_gen_mul_i32 (DREG (ext,12 ), src1,DREG (ext,12 )); +gen_logic_cc (s,DREG (ext,12 ),OS_LONG ); + } }static void gen_link (DisasContext *s,uint16_t insn,int32_t offset)
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH v2 1/2] target-m68k: add 64bit mull
target-m68k/translate.c DISAS_INSN(tas) · line 1834
Quoted 13 lines · Laurent Vivier
target-m68k/translate.c +13
-0+if (ext &0x400 ) { +if (!m68k_feature (s->env,M68K_FEATURE_QUAD_MULDIV )) { +gen_exception (s, s->pc -4 ,EXCP_UNSUPPORTED ); +return ; + } + +SRC_EA (env, src1,OS_LONG ,0 ,NULL ); + +if (sign) { +tcg_gen_muls2_i32 (DREG (ext,12 ),DREG (ext,0 ), src1,DREG (ext,12 )); + }else { +tcg_gen_mulu2_i32 (DREG (ext,12 ),DREG (ext,0 ), src1,DREG (ext,12 )); + }
TCG will react badly if Dl == Dh, and thus the two outputs are the same.
This is undefined in the spec, but guests violating the spec shouldn't cause qemu to crash.
I think you should compute this into N and Z and move the results (perhaps in an order matching real hw?) to the data registers afterward.
r~
Re: [Qemu-devel] [PATCH v2 1/2] target-m68k: add 64bit mull
Quoted 19 lines · Richard Henderson
Quoted 13 lines · Laurent Vivier
target-m68k/translate.c +10
-0+if (ext &0x400 ) { +if (!m68k_feature (s->env,M68K_FEATURE_QUAD_MULDIV )) { +gen_exception (s, s->pc -4 ,EXCP_UNSUPPORTED ); +return ; + } + +SRC_EA (env, src1,OS_LONG ,0 ,NULL ); + +if (sign) { + tcg_gen_muls2_i32(DREG(ext, 12), DREG(ext, 0), src1,
DREG(ext, 12));(quoted diff) +2
-0+ } else { + tcg_gen_mulu2_i32(DREG(ext, 12), DREG(ext, 0), src1,Quoted 2 lines
DREG(ext, 12));
+ }TCG will react badly if Dl == Dh, and thus the two outputs are the same.
Quoted 5 lines
This is undefined in the spec, but guests violating the spec shouldn't cause qemu to crash.
I think you should compute this into N and Z and move the results (perhaps in an order matching real hw?) to the data registers afterward.
I'm going to update as you say, and I have checked on a real 68040: it returns the low word of the result.
Thanks,
Laurent
PATCH v2 2/2 target-m68k: add 680x0 divu/divs variants
In reply to Laurent VivierUpdate helper to set the throwing location in case of div-by-0. Cleanup divX.w and add quad word variants of divX.l.
--- linux-user/main.c | 7 +++ target-m68k/cpu.h | 4 -- target-m68k/helper.h | 6 +- target-m68k/op_helper.c | 102 ++++++++++++++++++++++++-------- target-m68k/qregs.def | 2 - target-m68k/translate.c | 154 ++++++++++++++++++++++++++++++++++++++---------- 6 files changed, 211 insertions(+), 64 deletions(-)
linux-user/main.c +7 -0
@@ -2864,6 +2864,13 @@ void cpu_loop(CPUM68KState *env) info._sifields._sigfault._addr = env->pc;queue_signal (env, info.si_signo,QEMU_SI_FAULT , &info);break ; +case EXCP_DIV0 : + info.si_signo =TARGET_SIGFPE ; + info.si_errno =0 ; + info.si_code =TARGET_FPE_INTDIV ; + info._sifields._sigfault._addr = env->pc; +queue_signal (env, info.si_signo,QEMU_SI_FAULT , &info); +break ;case EXCP_TRAP0 : {abi_long ret;
target-m68k/cpu.h +0 -4
@@ -95,10 +95,6 @@ typedef struct CPUM68KState {uint32_t macsr;uint32_t mac_mask; -/* Temporary storage for DIV helpers. */ -uint32_t div1; -uint32_t div2; -/* MMU status. */ struct {uint32_t ar;
target-m68k/helper.h +4 -2
@@ -1,8 +1,10 @@ DEF_HELPER_1(bitrev, i32, i32) 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_4(divu, i64, env, i32, i32, i32) +DEF_HELPER_4(divs, i64, env, i32, i32, i32) +DEF_HELPER_3(divu64, i64, env, i64, i32) +DEF_HELPER_3(divs64, i64, env, i64, i32) DEF_HELPER_2(set_sr, void, env, i32)DEF_HELPER_3 (movec, void, env, i32, i32)
target-m68k/op_helper.c +78 -24
141 lines not on this page · raw message
target-m68k/qregs.def +0 -2
@@ -7,7 +7,5 @@ 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) DEFO32(MAC_MASK, mac_mask)
target-m68k/translate.c +122 -32
186 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH v2 2/2] target-m68k: add 680x0 divu/divs variants
Quoted 10 lines · Laurent Vivier
target-m68k/op_helper.c +2
-4-void HELPER (divu )(CPUM68KState *env,uint32_t word) +uint64_t HELPER (divu )(CPUM68KState *env,uint32_t num,uint32_t den, +uint32_t word) { -uint32_t num; -uint32_t den;uint32_t quot;uint32_t rem; - num = env->div1;
target-m68k/op_helper.c void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt) · line 199
Quoted 10 lines
target-m68k/op_helper.c +1
-3- den = env->div2; -/* ??? This needs to make sure the throwing location is accurate. */ if (den ==0 ) { -raise_exception (env,EXCP_DIV0 ); +raise_exception_ra (env,EXCP_DIV0 ,GETPC ()); } quot = num / den; rem = num % den; env->cc_v = (word && quot >0xffff ?-1 :0 );
It would be better to have separate routines for word and !word.
Quoted 4 lines
target-m68k/op_helper.c +2
-2- env->cc_z = quot; - env->cc_n = quot; +/* real 68040 keep Z and N on overflow, +* whereas documentation says "undefined"
target-m68k/op_helper.c void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt) · line 209
Quoted 10 lines
target-m68k/op_helper.c +6
-2+*/ +if (!env->cc_v) { + env->cc_z = quot; + env->cc_n = quot; + } env->cc_c =0 ; - env->div1 = quot; - env->div2 = rem; +return quot | ((uint64_t )rem <<32 );
You ought to pack the data into 32 bits here,
target-m68k/translate.c DISAS_INSN(mulw) · line 1224
Quoted 8 lines
target-m68k/translate.c +8
-0+/* result rem.w:quot.w */ + + quot =tcg_temp_new_i32 (); + rem =tcg_temp_new_i32 (); +tcg_gen_extr_i64_i32 (quot, rem, t64); +tcg_temp_free_i64 (t64); +tcg_gen_deposit_i32 (quot, quot, rem,16 ,16 ); +tcg_temp_free_i32 (rem);
instead of here.
target-m68k/translate.c DISAS_INSN(mulw) · line 1234
Quoted 10 lines
target-m68k/translate.c +10
-0+ +/* on overflow, operands are unaffected, +* Z and N flags are undefined, C is always 0 +*/ + +tcg_gen_movcond_i32 (TCG_COND_EQ ,DREG (insn,9 ), +QREG_CC_V ,QREG_CC_C /* zero */ , + quot,DREG (insn,9 )); +tcg_temp_free_i32 (quot); +
However, given how overflow conditionally sets the register(s), I also wonder if it wouldn't be cleaner to pass the output register numbers to the helper.
E.g.
t = tcg_const_i32(REG(insn, 9));
gen_helper_divsw(cpu_env, t, src);
tcg_temp_free_i32(t);HELPER_3(divsw, env, int, s32)
void HELPER(divsw)(CPUM68KState *env, int destr, int32_t den)
{
int32_t num = env->dregs[destr];
int32_t quot, rem;
if (den == 0) {
....
}
quot = num / den;
rem = num % den;
env->cc_c = 0; /* always cleared, even if overflow */
if (quot != (int16_t)quot) {
env->cc_v = -1;
/* nothing else is modified */
return;
}
env->dregs[destr] = deposit32(quot, 16, 16, rem);
env->cc_n = quot;
env->cc_z = quot;
env->cc_v = 0;
}target-m68k/translate.c DISAS_INSN(mulw) · line 1337
Quoted 8 lines
target-m68k/translate.c +8
-0+/* divs.l <EA>, Dq 32/32 -> 32q */ +/* divsl.l <EA>, Dr:Dq 32/32 -> 32r:32q */ +tcg_gen_movcond_i32 (TCG_COND_EQ ,DREG (ext,0 ), +QREG_CC_V ,QREG_CC_C /* zero */ , + rem,DREG (ext,0 ));/* rem */ +tcg_gen_movcond_i32 (TCG_COND_EQ ,DREG (ext,12 ), +QREG_CC_V ,QREG_CC_C /* zero */ , + quot,DREG (ext,12 ));/* quot */
This doubly applies to the 64-bit output variants.
r~