Skip to contentSkip to conversation

target/i386/tcg: more fixes

PATCH In review Paolo Bonzini 3 participants 15 messages Jan 7 – Jan 8

lore
Share & tools
lore
#0 lore raw

cover target/i386/tcg: more fixes

A bunch of fixes, coming either from earlier reviews or from code inspection.

Paolo

Paolo Bonzini (5):
target/i386/tcg: remove dead code
target/i386/tcg: do not mark all SSE instructions as unaligned target/i386/tcg: mask addresses for VSIB target/i386/tcg: allow VEX in 16-bit protected mode target/i386/tcg: remove register case from decode_modrm_address

 target/i386/ops_sse.h                | 16 ++++++++--------
 target/i386/tcg/decode-new.h         |  2 ++
 target/i386/tcg/ops_sse_header.h.inc |  8 ++++----
 target/i386/tcg/translate.c          |  3 ---
 target/i386/tcg/decode-new.c.inc     | 20 ++++++++------------
 target/i386/tcg/emit.c.inc           | 17 +++++++++--------
 6 files changed, 31 insertions(+), 35 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH 1/5 target/i386/tcg: remove dead code

Remove dead code; it arose when I noticed that, because 0x3? opcodes do have a pop, case 0x32 works just fine as fcomp (even though 0x?2 is fcom):
there is no need to hack the op to 0x03.

Reported by Coverity as CID 1643922.

---
 target/i386/tcg/translate.c | 3 ---
 1 file changed, 3 deletions(-)
target/i386/tcg/translate.c +0 -3
index 7c444d5006d..460848e4223 100644
@@ -2543,9 +2543,6 @@ static void gen_x87(DisasContext *s, X86DecodedInsn *decode)
                 break;
             }
             break;
-            /* map to fcomp; op & 7 == 2 would not pop  */
-            op = 0x03;
-            /* fallthrough */
         case 0x00 ... 0x07: /* fxxx st, sti */
         case 0x22 ... 0x23: /* fcom2 and fcomp3, undocumented ops */
         case 0x32: /* fcomp5, undocumented op */
Signature · 1 line
2.52.0
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] target/i386/tcg: remove dead code

Quoted 16 lines · Paolo Bonzini

Remove dead code; it arose when I noticed that, because 0x3? opcodes do have a pop, case 0x32 works just fine as fcomp (even though 0x?2 is fcom):
there is no need to hack the op to 0x03.

Reported by Coverity as CID 1643922.

---
  target/i386/tcg/translate.c | 3 ---
  1 file changed, 3 deletions(-)
target/i386/tcg/translate.c +0 -0
index 7c444d5006d..460848e4223 100644

target/i386/tcg/translate.c static void gen_x87(DisasContext *s, X86DecodedInsn *decode) · line 2548

Quoted 9 lines
target/i386/tcg/translate.c +0 -3
@@ -2543,9 +2543,6 @@ static void gen_x87(DisasContext *s, X86DecodedInsn *decode)
                  break;
              }
              break;
-            /* map to fcomp; op & 7 == 2 would not pop  */
-            op = 0x03;
-            /* fallthrough */
          case 0x00 ... 0x07: /* fxxx st, sti */
          case 0x22 ... 0x23: /* fcom2 and fcomp3, undocumented ops */
          case 0x32: /* fcomp5, undocumented op */

r~

#3 lore raw

PATCH 2/5 target/i386/tcg: do not mark all SSE instructions as unaligned

In reply to Paolo Bonzini

If the vex_special field was not initialized, it was considered to be X86_VEX_SSEUnaligned (whose value was zero). Add a new value to fix that.

---
 target/i386/tcg/decode-new.h     |  2 ++
 target/i386/tcg/decode-new.c.inc | 11 +++++++----
 2 files changed, 9 insertions(+), 4 deletions(-)
target/i386/tcg/decode-new.h +2 -0
index 7f23d373ea7..92822969003 100644
@@ -242,6 +242,8 @@ typedef enum X86InsnSpecial {
  * section of the manual.
  */
 typedef enum X86VEXSpecial {
+    X86_VEX_None,
+
     /* Legacy SSE instructions that allow unaligned operands */
     X86_VEX_SSEUnaligned,
 
target/i386/tcg/decode-new.c.inc +7 -4
index 9d3a7c0d426..f662364c600 100644
@@ -1068,15 +1068,15 @@ static void decode_0F10(DisasContext *s, CPUX86State *env, X86OpEntry *entry, ui
 static void decode_0F11(DisasContext *s, CPUX86State *env, X86OpEntry *entry, uint8_t *b)
 {
     static const X86OpEntry opcodes_0F11_reg[4] = {
-        X86_OP_ENTRY3(MOVDQ,   W,x,  None,None, V,x, vex4), /* MOVUPS */
-        X86_OP_ENTRY3(MOVDQ,   W,x,  None,None, V,x, vex4), /* MOVUPD */
+        X86_OP_ENTRY3(MOVDQ,   W,x,  None,None, V,x, vex4_unal), /* MOVUPS */
+        X86_OP_ENTRY3(MOVDQ,   W,x,  None,None, V,x, vex4_unal), /* MOVUPD */
         X86_OP_ENTRY3(VMOVSS,  W,x,  H,x,       V,x, vex5),
         X86_OP_ENTRY3(VMOVLPx, W,x,  H,x,       V,q, vex5), /* MOVSD */
     };
 
     static const X86OpEntry opcodes_0F11_mem[4] = {
-        X86_OP_ENTRY3(MOVDQ,      W,x,  None,None, V,x, vex4), /* MOVUPS */
-        X86_OP_ENTRY3(MOVDQ,      W,x,  None,None, V,x, vex4), /* MOVUPD */
+        X86_OP_ENTRY3(MOVDQ,      W,x,  None,None, V,x, vex4_unal), /* MOVUPS */
+        X86_OP_ENTRY3(MOVDQ,      W,x,  None,None, V,x, vex4_unal), /* MOVUPD */
         X86_OP_ENTRY3(VMOVSS_st,  M,ss, None,None, V,x, vex5),
         X86_OP_ENTRY3(VMOVLPx_st, M,sd, None,None, V,x, vex5), /* MOVSD */
     };
@@ -2624,6 +2624,9 @@ static bool validate_vex(DisasContext *s, X86DecodedInsn *decode)
     X86OpEntry *e = &decode->e;
 
     switch (e->vex_special) {
+    case X86_VEX_None:
+        break;
+
     case X86_VEX_REPScalar:
         /*
          * Instructions which differ between 00/66 and F2/F3 in the
Signature · 1 line
2.52.0
Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] target/i386/tcg: do not mark all SSE instructions as unaligned

Quoted 10 lines · Paolo Bonzini

If the vex_special field was not initialized, it was considered to be X86_VEX_SSEUnaligned (whose value was zero). Add a new value to fix that.

---
  target/i386/tcg/decode-new.h     |  2 ++
  target/i386/tcg/decode-new.c.inc | 11 +++++++----
  2 files changed, 9 insertions(+), 4 deletions(-)

r~

#5 lore raw

PATCH 3/5 target/i386/tcg: mask addresses for VSIB

In reply to Paolo Bonzini

VSIB can have either 32-bit or 64-bit addresses, pass a constant mask to the helper and apply it before the load.

---
 target/i386/ops_sse.h                | 16 ++++++++--------
 target/i386/tcg/ops_sse_header.h.inc |  8 ++++----
 target/i386/tcg/emit.c.inc           | 17 +++++++++--------
 3 files changed, 21 insertions(+), 20 deletions(-)
target/i386/ops_sse.h +8 -8
index a2e4d480399..853196b2bbd 100644
@@ -2362,42 +2362,42 @@ void glue(helper_vpmaskmovq, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
 }
 
 void glue(helper_vpgatherdd, SUFFIX)(CPUX86State *env,
-        Reg *d, Reg *v, Reg *s, target_ulong a0, unsigned scale)
+        Reg *d, Reg *v, Reg *s, target_ulong a0, unsigned scale, target_ulong amask)
 {
     int i;
     for (i = 0; i < (2 << SHIFT); i++) {
         if (v->L(i) >> 31) {
             target_ulong addr = a0
                 + ((target_ulong)(int32_t)s->L(i) << scale);
-            d->L(i) = cpu_ldl_data_ra(env, addr, GETPC());
+            d->L(i) = cpu_ldl_data_ra(env, addr & amask, GETPC());
         }
         v->L(i) = 0;
     }
 }
 
 void glue(helper_vpgatherdq, SUFFIX)(CPUX86State *env,
-        Reg *d, Reg *v, Reg *s, target_ulong a0, unsigned scale)
+        Reg *d, Reg *v, Reg *s, target_ulong a0, unsigned scale, target_ulong amask)
 {
     int i;
     for (i = 0; i < (1 << SHIFT); i++) {
         if (v->Q(i) >> 63) {
             target_ulong addr = a0
                 + ((target_ulong)(int32_t)s->L(i) << scale);
-            d->Q(i) = cpu_ldq_data_ra(env, addr, GETPC());
+            d->Q(i) = cpu_ldq_data_ra(env, addr & amask, GETPC());
         }
         v->Q(i) = 0;
     }
 }
 
 void glue(helper_vpgatherqd, SUFFIX)(CPUX86State *env,
-        Reg *d, Reg *v, Reg *s, target_ulong a0, unsigned scale)
+        Reg *d, Reg *v, Reg *s, target_ulong a0, unsigned scale, target_ulong amask)
 {
     int i;
     for (i = 0; i < (1 << SHIFT); i++) {
         if (v->L(i) >> 31) {
             target_ulong addr = a0
                 + ((target_ulong)(int64_t)s->Q(i) << scale);
-            d->L(i) = cpu_ldl_data_ra(env, addr, GETPC());
+            d->L(i) = cpu_ldl_data_ra(env, addr & amask, GETPC());
         }
         v->L(i) = 0;
     }
@@ -2408,14 +2408,14 @@ void glue(helper_vpgatherqd, SUFFIX)(CPUX86State *env,
 }
 
 void glue(helper_vpgatherqq, SUFFIX)(CPUX86State *env,
-        Reg *d, Reg *v, Reg *s, target_ulong a0, unsigned scale)
+        Reg *d, Reg *v, Reg *s, target_ulong a0, unsigned scale, target_ulong amask)
 {
     int i;
     for (i = 0; i < (1 << SHIFT); i++) {
         if (v->Q(i) >> 63) {
             target_ulong addr = a0
                 + ((target_ulong)(int64_t)s->Q(i) << scale);
-            d->Q(i) = cpu_ldq_data_ra(env, addr, GETPC());
+            d->Q(i) = cpu_ldq_data_ra(env, addr & amask, GETPC());
         }
         v->Q(i) = 0;
     }
target/i386/tcg/ops_sse_header.h.inc +4 -4
index d92c6faf6d6..bbeb7301c33 100644
@@ -388,10 +388,10 @@ DEF_HELPER_4(glue(vpmaskmovd_st, SUFFIX), void, env, Reg, Reg, tl)
 DEF_HELPER_4(glue(vpmaskmovq_st, SUFFIX), void, env, Reg, Reg, tl)
 DEF_HELPER_4(glue(vpmaskmovd, SUFFIX), void, env, Reg, Reg, Reg)
 DEF_HELPER_4(glue(vpmaskmovq, SUFFIX), void, env, Reg, Reg, Reg)
-DEF_HELPER_6(glue(vpgatherdd, SUFFIX), void, env, Reg, Reg, Reg, tl, i32)
-DEF_HELPER_6(glue(vpgatherdq, SUFFIX), void, env, Reg, Reg, Reg, tl, i32)
-DEF_HELPER_6(glue(vpgatherqd, SUFFIX), void, env, Reg, Reg, Reg, tl, i32)
-DEF_HELPER_6(glue(vpgatherqq, SUFFIX), void, env, Reg, Reg, Reg, tl, i32)
+DEF_HELPER_7(glue(vpgatherdd, SUFFIX), void, env, Reg, Reg, Reg, tl, i32, tl)
+DEF_HELPER_7(glue(vpgatherdq, SUFFIX), void, env, Reg, Reg, Reg, tl, i32, tl)
+DEF_HELPER_7(glue(vpgatherqd, SUFFIX), void, env, Reg, Reg, Reg, tl, i32, tl)
+DEF_HELPER_7(glue(vpgatherqq, SUFFIX), void, env, Reg, Reg, Reg, tl, i32, tl)
 #if SHIFT == 2
 DEF_HELPER_3(vpermd_ymm, void, Reg, Reg, Reg)
 DEF_HELPER_4(vpermdq_ymm, void, Reg, Reg, Reg, i32)
target/i386/tcg/emit.c.inc +9 -8
index e55b65176fc..bc3a07f972c 100644
@@ -43,8 +43,8 @@ typedef void (*SSEFunc_0_pppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_ptr reg_c,
                                TCGv_i32 val);
 typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
                                TCGv val);
-typedef void (*SSEFunc_0_epppti)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
-                                 TCGv_ptr reg_c, TCGv a0, TCGv_i32 scale);
+typedef void (*SSEFunc_0_eppptit)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
+                                 TCGv_ptr reg_c, TCGv a0, TCGv_i32 scale, TCGv amask);
 typedef void (*SSEFunc_0_eppppi)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
                                   TCGv_ptr reg_c, TCGv_ptr reg_d, TCGv_i32 flags);
 typedef void (*SSEFunc_0_eppppii)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
@@ -1100,18 +1100,19 @@ VEXW_AVX(VPMASKMOV, vpmaskmov)
 
 /* Same as above, but with extra arguments to the helper.  */
 static inline void gen_vsib_avx(DisasContext *s, X86DecodedInsn *decode,
-                                SSEFunc_0_epppti d_xmm, SSEFunc_0_epppti q_xmm,
-                                SSEFunc_0_epppti d_ymm, SSEFunc_0_epppti q_ymm)
+                                SSEFunc_0_eppptit d_xmm, SSEFunc_0_eppptit q_xmm,
+                                SSEFunc_0_eppptit d_ymm, SSEFunc_0_eppptit q_ymm)
 {
-    SSEFunc_0_epppti d = s->vex_l ? d_ymm : d_xmm;
-    SSEFunc_0_epppti q = s->vex_l ? q_ymm : q_xmm;
-    SSEFunc_0_epppti fn = s->vex_w ? q : d;
+    SSEFunc_0_eppptit d = s->vex_l ? d_ymm : d_xmm;
+    SSEFunc_0_eppptit q = s->vex_l ? q_ymm : q_xmm;
+    SSEFunc_0_eppptit fn = s->vex_w ? q : d;
     TCGv_i32 scale = tcg_constant_i32(decode->mem.scale);
     TCGv_ptr index = tcg_temp_new_ptr();
+    TCGv mask = tcg_constant_tl(MAKE_64BIT_MASK(0, 8 << s->aflag));
 
     /* Pass third input as (index, base, scale) */
     tcg_gen_addi_ptr(index, tcg_env, ZMM_OFFSET(decode->mem.index));
-    fn(tcg_env, OP_PTR0, OP_PTR1, index, s->A0, scale);
+    fn(tcg_env, OP_PTR0, OP_PTR1, index, s->A0, scale, mask);
 
     /*
      * There are two output operands, so zero OP1's high 128 bits
Signature · 1 line
2.52.0
Reviewed-by on v1 3/5
#6 lore raw

Re: [PATCH 3/5] target/i386/tcg: mask addresses for VSIB

Quoted 10 lines · Paolo Bonzini

VSIB can have either 32-bit or 64-bit addresses, pass a constant mask to the helper and apply it before the load.

---
  target/i386/ops_sse.h                | 16 ++++++++--------
  target/i386/tcg/ops_sse_header.h.inc |  8 ++++----
  target/i386/tcg/emit.c.inc           | 17 +++++++++--------
  3 files changed, 21 insertions(+), 20 deletions(-)

r~

#7 lore raw

PATCH 4/5 target/i386/tcg: allow VEX in 16-bit protected mode

In reply to Paolo Bonzini

VEX is only forbidden in real and vm86 mode; 16-bit protected mode supports it for some unfathomable reason.

---
 target/i386/tcg/decode-new.c.inc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/tcg/decode-new.c.inc +1 -1
index f662364c600..243df7e3735 100644
@@ -2875,7 +2875,7 @@ static void disas_insn(DisasContext *s, CPUState *cpu)
          * VEX prefixes cannot be used except in 32-bit mode.
          * Otherwise the instruction is LES or LDS.
          */
-        if (CODE32(s) && !VM86(s)) {
+        if (PE(s) && !VM86(s)) {
             static const int pp_prefix[4] = {
                 0, PREFIX_DATA, PREFIX_REPZ, PREFIX_REPNZ
             };
Signature · 1 line
2.52.0
#8 lore raw

Re: [PATCH 4/5] target/i386/tcg: allow VEX in 16-bit protected mode

Quoted 14 lines · Paolo Bonzini

VEX is only forbidden in real and vm86 mode; 16-bit protected mode supports it for some unfathomable reason.

---
target/i386/tcg/decode-new.c.inc | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/tcg/decode-new.c.inc +0 -0
index f662364c600..243df7e3735 100644

target/i386/tcg/decode-new.c.inc static void disas_insn(DisasContext *s, CPUState *cpu) · line 2875

Quoted 1 line
target/i386/tcg/decode-new.c.inc +0 -0
@@ -2875,7 +2875,7 @@ static void disas_insn(DisasContext *s, CPUState *cpu)
         * VEX prefixes cannot be used except in 32-bit mode.

Does this comment need updating too?

Regards,
BALATON Zoltan

Quoted 8 lines
target/i386/tcg/decode-new.c.inc +1 -1
         * Otherwise the instruction is LES or LDS.
         */
-        if (CODE32(s) && !VM86(s)) {
+        if (PE(s) && !VM86(s)) {
            static const int pp_prefix[4] = {
                0, PREFIX_DATA, PREFIX_REPZ, PREFIX_REPNZ
            };
#9 lore raw

Re: [PATCH 4/5] target/i386/tcg: allow VEX in 16-bit protected mode

Quoted 10 lines · BALATON Zoltan
Quoted 9 lines · Paolo Bonzini

VEX is only forbidden in real and vm86 mode; 16-bit protected mode supports it for some unfathomable reason.

---
target/i386/tcg/decode-new.c.inc | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 9 lines

target/i386/tcg/decode-new.c.inc static void disas_insn(DisasContext *s, CPUState · line 2876

Quoted 7 lines
target/i386/tcg/decode-new.c.inc +0 -0
index f662364c600..243df7e3735 100644
@@ -2875,7 +2875,7 @@ static void disas_insn(DisasContext *s, CPUState 
 *cpu)
          * VEX prefixes cannot be used except in 32-bit mode.

Does this comment need updating too?

Good catch, it should be "Bits 6-7 of the first byte must be set except in 64-bit mode".

Paolo

Quoted 14 lines

Regards,
BALATON Zoltan

Quoted 8 lines
target/i386/tcg/decode-new.c.inc +1 -1
          * Otherwise the instruction is LES or LDS.
          */
-        if (CODE32(s) && !VM86(s)) {
+        if (PE(s) && !VM86(s)) {
             static const int pp_prefix[4] = {

                0, PREFIX_DATA, PREFIX_REPZ, PREFIX_REPNZ };

Reviewed-by on v1 4/5
#10 lore raw

Re: [PATCH 4/5] target/i386/tcg: allow VEX in 16-bit protected mode

In reply to Paolo Bonzini
Quoted 14 lines · Paolo Bonzini

VEX is only forbidden in real and vm86 mode; 16-bit protected mode supports it for some unfathomable reason.

---
  target/i386/tcg/decode-new.c.inc | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/tcg/decode-new.c.inc +0 -0
index f662364c600..243df7e3735 100644

target/i386/tcg/decode-new.c.inc static void disas_insn(DisasContext *s, CPUState *cpu) · line 2881

Quoted 8 lines
target/i386/tcg/decode-new.c.inc +1 -1
@@ -2875,7 +2875,7 @@ static void disas_insn(DisasContext *s, CPUState *cpu)
           * VEX prefixes cannot be used except in 32-bit mode.
           * Otherwise the instruction is LES or LDS.
           */
-        if (CODE32(s) && !VM86(s)) {
+        if (PE(s) && !VM86(s)) {
              static const int pp_prefix[4] = {
                  0, PREFIX_DATA, PREFIX_REPZ, PREFIX_REPNZ
              };

With the comment updated as you say,

r~

#11 lore raw

PATCH 5/5 target/i386/tcg: remove register case from decode_modrm_address

In reply to Paolo Bonzini

Unlike the older code in translate.c, mod=11b *is* filtered out earlier by decode_modrm.

---
 target/i386/tcg/decode-new.c.inc | 7 -------
 1 file changed, 7 deletions(-)
target/i386/tcg/decode-new.c.inc +0 -7
index 243df7e3735..7b595607fa7 100644
@@ -2024,12 +2024,6 @@ static AddressParts decode_modrm_address(CPUX86State *env, DisasContext *s,
     rm = modrm & 7;
     base = rm | REX_B(s);
 
-    if (mod == 3) {
-        /* Normally filtered out earlier, but including this path
-           simplifies multi-byte nop, as well as bndcl, bndcu, bndcn.  */
-        goto done;
-    }
-
     switch (s->aflag) {
     case MO_64:
     case MO_32:
@@ -2127,7 +2121,6 @@ static AddressParts decode_modrm_address(CPUX86State *env, DisasContext *s,
         g_assert_not_reached();
     }
 
- done:
     return (AddressParts){ def_seg, base, index, scale, disp };
 }
 
Signature · 1 line
2.52.0
#12 lore raw

Re: [PATCH 5/5] target/i386/tcg: remove register case from decode_modrm_address

Quoted 13 lines · Paolo Bonzini

Unlike the older code in translate.c, mod=11b *is* filtered out earlier by decode_modrm.

---
  target/i386/tcg/decode-new.c.inc | 7 -------
  1 file changed, 7 deletions(-)
target/i386/tcg/decode-new.c.inc +0 -0
index 243df7e3735..7b595607fa7 100644

target/i386/tcg/decode-new.c.inc static AddressParts decode_modrm_address(CPUX86State *env, DisasContext *s, · line 2032

Quoted 9 lines
target/i386/tcg/decode-new.c.inc +0 -6
@@ -2024,12 +2024,6 @@ static AddressParts decode_modrm_address(CPUX86State *env, DisasContext *s,
      rm = modrm & 7;
      base = rm | REX_B(s);
 
-    if (mod == 3) {
-        /* Normally filtered out earlier, but including this path
-           simplifies multi-byte nop, as well as bndcl, bndcu, bndcn.  */
-        goto done;
-    }
-

I can see that this is true, but one has to dig around to see that it's so.

There's enough prep code duplicated between decode_modrm and decode_modrm_address that I think it would be worthwhile to merge the two functions and simplify.

Also, not do things like

        switch (mod) {
...
        default:
        case 2:

where the default isn't reachable.

r~

#13 lore raw

Re: [PATCH 5/5] target/i386/tcg: remove register case from decode_modrm_address

Quoted 27 lines · Richard Henderson
Quoted 19 lines · Paolo Bonzini

Unlike the older code in translate.c, mod=11b *is* filtered out earlier by decode_modrm.

---
  target/i386/tcg/decode-new.c.inc | 7 -------

  1 file changed, 7 deletions(-)
target/i386/tcg/decode-new.c.inc +0 -3
index 243df7e3735..7b595607fa7 100644
@@ -2024,12 +2024,6 @@ static AddressParts 
 decode_modrm_address(CPUX86State *env, DisasContext *s,
       rm = modrm & 7;
       base = rm | REX_B(s);
-    if (mod == 3) {
-        /* Normally filtered out earlier, but including this path
-           simplifies multi-byte nop, as well as bndcl, bndcu, 
Quoted 4 lines

bndcn.  */
-        goto done;
-    }
-

I can see that this is true, but one has to dig around to see that it's so.

Quoted 1 line

There's enough prep code duplicated between decode_modrm and decode_modrm_address that I think it would be worthwhile to merge the two functions and simplify.

Is there really so much? I guess you could write the combined function like so:

    int modrm = get_modrm(s, env);
    int mod = (s->modrm >> 6) & 3;
    int rm = s->modrm & 7;
    if (mod == 3) {
        op->n = (rm | REX_B(s)) & reg_nb_mask(s, op->unit);
        return;
    }

    int def_seg = R_DS;
    int base = rm | REX_B(s);
    int index = -1;
    int scale = 0;
    targe_long disp = 0;
    bool is_vsib = decode->e.vex_class == 12;
    switch(s->aflag) {
        ...
    }

    op->has_ea = true;
    op->n = -1;
    decode->mem = (AddressParts){ def_seg, base, index, scale, disp };

but it does not seem so much better... I guess I could do the modrm split just once:

target/i386/tcg/decode-new.c.inc +10 -10
index 41012659104..23cae451eb7 100644
@@ -2247,21 +2247,19 @@
 
 /* Decompose an address.  */
 static AddressParts decode_modrm_address(CPUX86State *env, DisasContext *s,
-                                         int modrm, bool is_vsib)
+                                         int mod, int rm, bool is_vsib)
 {
-    int def_seg, base, index, scale, mod, rm;
+    int def_seg, base, index, scale;
     target_long disp;
     bool havesib;
 
+    assert(mod == 3);
     def_seg = R_DS;
+    base = rm | REX_B(s);
     index = -1;
     scale = 0;
     disp = 0;
 
-    mod = (modrm >> 6) & 3;
-    rm = modrm & 7;
-    base = rm | REX_B(s);
-
     switch (s->aflag) {
     case MO_64:
     case MO_32:
@@ -2379,16 +2376,18 @@
 static int decode_modrm(DisasContext *s, CPUX86State *env,
                         X86DecodedInsn *decode, X86DecodedOp *op)
 {
-    int modrm = s->modrm;
-    if ((modrm >> 6) == 3) {
-        op->n = (modrm & 7);
+    int modrm = get_modrm(s, env);
+    int mod = (s->modrm >> 6) & 3;
+    int rm = s->modrm & 7;
+    if (mod == 3) {
+        op->n = rm;
         if (op->unit != X86_OP_MMX) {
             op->n |= REX_B(s);
         }
     } else {
         op->has_ea = true;
         op->n = -1;
-        decode->mem = decode_modrm_address(env, s, get_modrm(s, env),
+        decode->mem = decode_modrm_address(env, s, mod, rm,
                                            decode->e.vex_class == 12);
     }
     return modrm;
Quoted 9 lines

Also, not do things like

        switch (mod) {
...
        default:
        case 2:

where the default isn't reachable.

I agree and was curious to when it dated... and it's March 2003 :) (commit 4b74fe1f001, "many fixes", which brought in the very first implementation of 16-bit addressing modes):

+ default:
+ case 7:
+ gen_op_movl_A0_reg[R_EBX]();
+ break;

Paolo

#14 lore raw

Re: [PATCH 5/5] target/i386/tcg: remove register case from decode_modrm_address

Quoted 49 lines · Paolo Bonzini
Quoted 26 lines · Richard Henderson
Quoted 13 lines · Paolo Bonzini

Unlike the older code in translate.c, mod=11b *is* filtered out earlier by decode_modrm.

---
  target/i386/tcg/decode-new.c.inc | 7 -------

  1 file changed, 7 deletions(-)
target/i386/tcg/decode-new.c.inc +0 -0
index 243df7e3735..7b595607fa7 100644

target/i386/tcg/decode-new.c.inc static AddressParts decode_modrm_address(CPUX86State *env, · line 2032

Quoted 9 lines
target/i386/tcg/decode-new.c.inc +0 -6
@@ -2024,12 +2024,6 @@ static AddressParts decode_modrm_address(CPUX86State *env, 
 DisasContext *s,
       rm = modrm & 7;
       base = rm | REX_B(s);
-    if (mod == 3) {
-        /* Normally filtered out earlier, but including this path
-           simplifies multi-byte nop, as well as bndcl, bndcu, bndcn.  */
-        goto done;
-    }
-

I can see that this is true, but one has to dig around to see that it's so.

Quoted 1 line

There's enough prep code duplicated between decode_modrm and decode_modrm_address that I think it would be worthwhile to merge the two functions and simplify.

Is there really so much?  I guess you could write the combined function like so:

    int modrm = get_modrm(s, env);
    int mod = (s->modrm >> 6) & 3;
    int rm = s->modrm & 7;
    if (mod == 3) {
        op->n = (rm | REX_B(s)) & reg_nb_mask(s, op->unit); return;
    }

    int def_seg = R_DS;
    int base = rm | REX_B(s);
    int index = -1;
    int scale = 0;
    targe_long disp = 0;
    bool is_vsib = decode->e.vex_class == 12;
    switch(s->aflag) {
        ...
    }

Quoted 3 lines

    op->has_ea = true;
    op->n = -1;
    decode->mem = (AddressParts){ def_seg, base, index, scale, disp };

Yes, this is the sort of thing that I was thinking.

Where, in particular, the compiler gets to see that bounds of mod were [0-3], and that mod == 3 had been eliminated, so that later in the function the bounds of mod are [0-2] for those later switch statements.

Quoted 2 lines

but it does not seem so much better...  I guess I could do the modrm split just once:

I suppose the compiler would be able to do the same bounds discovery with that, since it'll definitely inline the single-use function. So at least that's an improvement over the current situation, where I think the two shift + mask extractions from a memory operand probably aren't CSE'd.

But the fact that you felt the need to add an assert there at the beginning suggests to me that it really is clearer as a single function.

r~