Skip to contentSkip to conversation

Fix mac.w / mac.l insn

PATCH In review Oleg Endo 3 participants 6 messages Aug 26 – Sep 1

lore
Share & tools
lore

Oleg Endo

author
#0 lore raw

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
index 53b0921..efbc8ab 100644
@@ -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);
#1 lore raw

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
#2 lore raw
In reply to Oleg Endo
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 += 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
[2 qemu-sh4-mac-same-reg.patch <text/x-patch; UTF-8 (base64)>]

target/sh4/translate.c +14 -4
index 53b0921..efbc8ab 100644
@@ -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
index e503a97b22..64fe2cd31d 100644
@@ -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
author
#3 lore raw
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
index e503a97b22..64fe2cd31d 100644
@@ -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 -0
             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;

Thanks, I think you're right. I'll have another look and report back in a few days.

Best regards,
Oleg Endo

author
#4 lore raw
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
index e503a97b22..64fe2cd31d 100644
@@ -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 -0
             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.

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
index 53b0921..4ab439b 100644
@@ -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 */
Reviewed-by
#5 lore raw
Quoted 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
index e503a97b22..64fe2cd31d 100644
@@ -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 -0
             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.

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
index 53b0921..4ab439b 100644
@@ -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 -1
                                 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 */
Signature · 1 line
Yosinori Sato