Fix mac.w / mac.l insn
PATCH 1/1 Fix mac.w / mac.l insn
Hi!
It seems the implementation of the mac.w / mac.l insn is wrong:
mac.w @Rm+,@Rn+
mac.l @Rm+,@Rn+
Qemu's implementation first reads both mems, then applies the post-inc. However,
mac.w @R0+,@R0+
should actually do:
read @(R0 + 0)
R0 += 2
read @(R0 + 2)
R0 += 2
That's also how GDB sh-sim implements it.
The attached patch fixes it.
I ran into this issue recently when trying to use qemu for sh4. I'm not on the list, so please add me in CC in the reply.
Best regards,
Oleg Endo
target/sh4/translate.c +21 -4
@@ -809,8 +809,17 @@ static void _decode_opc(DisasContext * ctx)tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx,MO_TESL |MO_ALIGN ); arg1 =tcg_temp_new (); -tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx, -MO_TESL |MO_ALIGN ); +if (B7_4 ==B11_8 ) { +/* When Rm and Rn are the same register, the second +operand is read after the first post-increment. */ +TCGv addr =tcg_temp_new (); +tcg_gen_addi_i32 (addr,REG (B11_8 ),4 ); +tcg_gen_qemu_ld_i32 (arg1, addr, ctx->memidx, +MO_TESL |MO_ALIGN ); + }else { +tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx, +MO_TESL |MO_ALIGN ); + }gen_helper_macl (tcg_env, arg0, arg1);tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),4 );tcg_gen_addi_i32 (REG (B11_8 ),REG (B11_8 ),4 );
@@ -823,8 +832,16 @@ static void _decode_opc(DisasContext * ctx)tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx,MO_TESW |MO_ALIGN ); arg1 =tcg_temp_new (); -tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx, -MO_TESW |MO_ALIGN ); +if (B7_4 ==B11_8 ) { +/* See mac.l above. */ +TCGv addr =tcg_temp_new (); +tcg_gen_addi_i32 (addr,REG (B11_8 ),2 ); +tcg_gen_qemu_ld_i32 (arg1, addr, ctx->memidx, +MO_TESW |MO_ALIGN ); + }else { +tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx, +MO_TESW |MO_ALIGN ); + }gen_helper_macw (tcg_env, arg0, arg1);tcg_gen_addi_i32 (REG (B11_8 ),REG (B11_8 ),2 );tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),2 );
Hi Oleg,
Quoted 16 lines · Oleg Endo
It seems the implementation of the mac.w / mac.l insn is wrong:
mac.w @Rm+,@Rn+ mac.l @Rm+,@Rn+Qemu's implementation first reads both mems, then applies the post-inc. However,
mac.w @R0+,@R0+
should actually do:
read @(R0 + 0)
R0 += 2
read @(R0 + 2)
R0 += 2
Quoted 5 lines
That's also how GDB sh-sim implements it.
The attached patch fixes it.I ran into this issue recently when trying to use qemu for sh4. I'm not on the list, so please add me in CC in the reply.
I think the patch should be posted as inline, i.e. using git send-email.
Adrian
Signature · 4 lines
.''`. John Paul Adrian Glaubitz : :' : Debian Developer `. `' Physicist `- GPG: 62FF 8A75 84E0 2956 9546 0006 7426 3B37 F5B5 F913
Quoted 62 lines · Oleg Endo
[1 <text/plain; UTF-8 (quoted-printable)>]
Hi!It seems the implementation of the mac.w / mac.l insn is wrong:
mac.w @Rm+,@Rn+ mac.l @Rm+,@Rn+Qemu's implementation first reads both mems, then applies the post-inc. However,
mac.w @R0+,@R0+
should actually do:
read @(R0 + 0)
R0 += 2
read @(R0 + 2)
R0 += 2That's also how GDB sh-sim implements it.
The attached patch fixes it.I ran into this issue recently when trying to use qemu for sh4. I'm not on the list, so please add me in CC in the reply.
Best regards,
Oleg Endo
[2 qemu-sh4-mac-same-reg.patch <text/x-patch; UTF-8 (base64)>]target/sh4/translate.c +14
-4@@ -809,8 +809,17 @@ static void _decode_opc(DisasContext * ctx)tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx,MO_TESL |MO_ALIGN ); arg1 =tcg_temp_new (); -tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx, -MO_TESL |MO_ALIGN ); +if (B7_4 ==B11_8 ) { +/* When Rm and Rn are the same register, the second +operand is read after the first post-increment. */ +TCGv addr =tcg_temp_new (); +tcg_gen_addi_i32 (addr,REG (B11_8 ),4 ); +tcg_gen_qemu_ld_i32 (arg1, addr, ctx->memidx, +MO_TESL |MO_ALIGN ); + }else { +tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx, +MO_TESL |MO_ALIGN ); + }gen_helper_macl (tcg_env, arg0, arg1);tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),4 );tcg_gen_addi_i32 (REG (B11_8 ),REG (B11_8 ),4 );@@ -823,8 +832,16 @@ static void _decode_opc(DisasContext * ctx)tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx,MO_TESW |MO_ALIGN ); arg1 =tcg_temp_new (); -tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx, -MO_TESW |MO_ALIGN ); +if (B7_4 ==B11_8 ) { +/* See mac.l above. */ +TCGv addr =tcg_temp_new ();
target/sh4/translate.c static void _decode_opc(DisasContext * ctx) · line 847
Quoted 10 lines
target/sh4/translate.c +7
-0+tcg_gen_addi_i32 (addr,REG (B11_8 ),2 ); +tcg_gen_qemu_ld_i32 (arg1, addr, ctx->memidx, +MO_TESW |MO_ALIGN ); + }else { +tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx, +MO_TESW |MO_ALIGN ); + }gen_helper_macw (tcg_env, arg0, arg1);tcg_gen_addi_i32 (REG (B11_8 ),REG (B11_8 ),2 );tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),2 );
It is true that the current behavior differs from the pseudocode in the manual. Wouldn't it be simpler to just generate a TCG that matches the pseudocode? I'm sorry, I haven't actually tested this change yet.
target/sh4/translate.c +6 -6
@@ -801,13 +801,13 @@ static void _decode_opc(DisasContext * ctx) {TCGv arg0, arg1; arg0 =tcg_temp_new (); -tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, -MO_TESL |MO_ALIGN ); -tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),4 ); arg1 =tcg_temp_new ();tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx,MO_TESL |MO_ALIGN );tcg_gen_addi_i32 (REG (B11_8 ),REG (B11_8 ),4 ); +tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, +MO_TESL |MO_ALIGN ); +tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),4 );gen_helper_macl (tcg_env, arg0, arg1); }return ;
@@ -815,13 +815,13 @@ static void _decode_opc(DisasContext * ctx) {TCGv arg0, arg1; arg0 =tcg_temp_new (); -tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, -MO_TESW |MO_ALIGN ); -tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),2 ); arg1 =tcg_temp_new ();tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx,MO_TESW |MO_ALIGN );tcg_gen_addi_i32 (REG (B11_8 ),REG (B11_8 ),2 ); +tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, +MO_TESW |MO_ALIGN ); +tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),2 );gen_helper_macw (tcg_env, arg0, arg1); }return ;
Signature · 1 line
Yosinori Sato
Quoted 35 lines · On Wed, 2026-08-26 at 20:44 +0900, [email protected] wrote:
It is true that the current behavior differs from the pseudocode in the manual. Wouldn't it be simpler to just generate a TCG that matches the pseudocode? I'm sorry, I haven't actually tested this change yet.
target/sh4/translate.c +3
-6@@ -801,13 +801,13 @@ static void _decode_opc(DisasContext * ctx) {TCGv arg0, arg1; arg0 =tcg_temp_new (); -tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, -MO_TESL |MO_ALIGN ); -tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),4 ); arg1 =tcg_temp_new ();tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx,MO_TESL |MO_ALIGN );tcg_gen_addi_i32 (REG (B11_8 ),REG (B11_8 ),4 ); +tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, +MO_TESL |MO_ALIGN ); +tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),4 );gen_helper_macl (tcg_env, arg0, arg1); }return ;@@ -815,13 +815,13 @@ static void _decode_opc(DisasContext * ctx) {TCGv arg0, arg1; arg0 =tcg_temp_new (); -tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, -MO_TESW |MO_ALIGN ); -tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),2 );
target/sh4/translate.c static void _decode_opc(DisasContext * ctx) · line 827
Quoted 10 lines
target/sh4/translate.c +3
-0arg1 =tcg_temp_new ();tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx,MO_TESW |MO_ALIGN );tcg_gen_addi_i32 (REG (B11_8 ),REG (B11_8 ),2 ); +tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, +MO_TESW |MO_ALIGN ); +tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),2 );gen_helper_macw (tcg_env, arg0, arg1); }return ;
Thanks, I think you're right. I'll have another look and report back in a few days.
Best regards,
Oleg Endo
Quoted 41 lines · Oleg Endo
Quoted 40 lines · On Wed, 2026-08-26 at 20:44 +0900, [email protected] wrote:
It is true that the current behavior differs from the pseudocode in the manual. Wouldn't it be simpler to just generate a TCG that matches the pseudocode? I'm sorry, I haven't actually tested this change yet.
target/sh4/translate.c +6
-6@@ -801,13 +801,13 @@ static void _decode_opc(DisasContext * ctx) {TCGv arg0, arg1; arg0 =tcg_temp_new (); -tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, -MO_TESL |MO_ALIGN ); -tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),4 ); arg1 =tcg_temp_new ();tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx,MO_TESL |MO_ALIGN );tcg_gen_addi_i32 (REG (B11_8 ),REG (B11_8 ),4 ); +tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, +MO_TESL |MO_ALIGN ); +tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),4 );gen_helper_macl (tcg_env, arg0, arg1); }return ;@@ -815,13 +815,13 @@ static void _decode_opc(DisasContext * ctx) {TCGv arg0, arg1; arg0 =tcg_temp_new (); -tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, -MO_TESW |MO_ALIGN ); -tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),2 ); arg1 =tcg_temp_new ();tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx,MO_TESW |MO_ALIGN );tcg_gen_addi_i32 (REG (B11_8 ),REG (B11_8 ),2 ); +tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, +MO_TESW |MO_ALIGN ); +tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),2 );
Quoted 9 lines
target/sh4/translate.c static void _decode_opc(DisasContext * ctx) · line 827
Quoted 5 lines
target/sh4/translate.c +0
-0gen_helper_macw (tcg_env, arg0, arg1); }return ;Thanks, I think you're right. I'll have another look and report back in a few days.
I've checked it, the attached patch has less clutter, as per your suggestion and it works.
Best regards,
Oleg Endo
target/sh4/translate.c +7 -7
@@ -806,28 +806,28 @@ static void _decode_opc(DisasContext * ctx) {TCGv arg0, arg1; arg0 =tcg_temp_new (); -tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, -MO_TESL |MO_ALIGN ); arg1 =tcg_temp_new ();tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx,MO_TESL |MO_ALIGN ); -gen_helper_macl (tcg_env, arg0, arg1); -tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),4 );tcg_gen_addi_i32 (REG (B11_8 ),REG (B11_8 ),4 ); +tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, +MO_TESL |MO_ALIGN ); +tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),4 ); +gen_helper_macl (tcg_env, arg0, arg1); }return ;case 0x400f :/* mac.w @Rm+,@Rn+ */ {TCGv arg0, arg1; arg0 =tcg_temp_new (); -tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, -MO_TESW |MO_ALIGN ); arg1 =tcg_temp_new ();tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx,MO_TESW |MO_ALIGN ); -gen_helper_macw (tcg_env, arg0, arg1);tcg_gen_addi_i32 (REG (B11_8 ),REG (B11_8 ),2 ); +tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, +MO_TESW |MO_ALIGN );tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),2 ); +gen_helper_macw (tcg_env, arg0, arg1); }return ;case 0x0007 :/* mul.l Rm,Rn */
[email protected]
Reviewed-byQuoted 90 lines · Oleg Endo
[1 <text/plain; UTF-8 (quoted-printable)>]
Quoted 41 lines · Oleg Endo
Quoted 40 lines · On Wed, 2026-08-26 at 20:44 +0900, [email protected] wrote:
It is true that the current behavior differs from the pseudocode in the manual. Wouldn't it be simpler to just generate a TCG that matches the pseudocode? I'm sorry, I haven't actually tested this change yet.
target/sh4/translate.c +6
-6@@ -801,13 +801,13 @@ static void _decode_opc(DisasContext * ctx) {TCGv arg0, arg1; arg0 =tcg_temp_new (); -tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, -MO_TESL |MO_ALIGN ); -tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),4 ); arg1 =tcg_temp_new ();tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx,MO_TESL |MO_ALIGN );tcg_gen_addi_i32 (REG (B11_8 ),REG (B11_8 ),4 ); +tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, +MO_TESL |MO_ALIGN ); +tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),4 );gen_helper_macl (tcg_env, arg0, arg1); }return ;@@ -815,13 +815,13 @@ static void _decode_opc(DisasContext * ctx) {TCGv arg0, arg1; arg0 =tcg_temp_new (); -tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, -MO_TESW |MO_ALIGN ); -tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),2 ); arg1 =tcg_temp_new ();tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx,MO_TESW |MO_ALIGN );tcg_gen_addi_i32 (REG (B11_8 ),REG (B11_8 ),2 ); +tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, +MO_TESW |MO_ALIGN ); +tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),2 );Quoted 9 lines
target/sh4/translate.c static void _decode_opc(DisasContext * ctx) · line 827
Quoted 5 lines
target/sh4/translate.c +0
-0gen_helper_macw (tcg_env, arg0, arg1); }return ;Thanks, I think you're right. I'll have another look and report back in a few days.
I've checked it, the attached patch has less clutter, as per your suggestion and it works.
Best regards,
Oleg Endo
[2 qemu-sh4-mac-order-v2.patch <text/x-patch; UTF-8 (base64)>]target/sh4/translate.c +4
-6@@ -806,28 +806,28 @@ static void _decode_opc(DisasContext * ctx) {TCGv arg0, arg1; arg0 =tcg_temp_new (); -tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, -MO_TESL |MO_ALIGN ); arg1 =tcg_temp_new ();tcg_gen_qemu_ld_i32 (arg1,REG (B11_8 ), ctx->memidx,MO_TESL |MO_ALIGN ); -gen_helper_macl (tcg_env, arg0, arg1); -tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),4 );tcg_gen_addi_i32 (REG (B11_8 ),REG (B11_8 ),4 ); +tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, +MO_TESL |MO_ALIGN ); +tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),4 ); +gen_helper_macl (tcg_env, arg0, arg1); }return ;case 0x400f :/* mac.w @Rm+,@Rn+ */ {TCGv arg0, arg1; arg0 =tcg_temp_new (); -tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, -MO_TESW |MO_ALIGN ); arg1 =tcg_temp_new (); tcg_gen_qemu_ld_i32(arg1, REG(B11_8), ctx->memidx,
target/sh4/translate.c static void _decode_opc(DisasContext * ctx) · line 833
Quoted 10 lines
target/sh4/translate.c +3
-1MO_TESW | MO_ALIGN); -gen_helper_macw (tcg_env, arg0, arg1);tcg_gen_addi_i32 (REG (B11_8 ),REG (B11_8 ),2 ); +tcg_gen_qemu_ld_i32 (arg0,REG (B7_4 ), ctx->memidx, +MO_TESW |MO_ALIGN );tcg_gen_addi_i32 (REG (B7_4 ),REG (B7_4 ),2 ); +gen_helper_macw (tcg_env, arg0, arg1); }return ;case 0x0007 :/* mul.l Rm,Rn */
Signature · 1 line
Yosinori Sato