Skip to contentSkip to conversation

Run time TCGv size check for debugging

RFC PATCH In review Blue Swirl 3 participants 12 messages Oct 29, 2008 – Nov 1, 2008

lore
Share & tools
lore

Blue Swirl

author
#0 lore raw

RFC PATCH 1/1 Run time TCGv size check for debugging

Hi,

When emulating a mixed 32/64 bit Qemu target CPUs it's easy to confuse the TCGv size, passing 32 bit TCGv to a function expecting a 64 bit one and vice versa. This patch adds a run time sanity check for TCGv sizes.

Because a 32 bit Qemu host does not really use 64 bit TCGvs, the patch is only functional on a 64 bit host. Of course also a pure 32 bit Qemu target is not likely to suffer from TCGv size confusion.

Some use cases are not covered yet. Comments?

tcg/tcg-op.h +144 -5
Index: qemu/tcg/tcg-op.h
===================================================================
@@ -169,14 +169,22 @@
     tcg_gen_op1i(INDEX_op_br, label);
 }
 
-static inline void tcg_gen_mov_i32(TCGv ret, TCGv arg)
+static inline void tcg_gen_mov_i32_nocheck(TCGv ret, TCGv arg)
 {
     if (GET_TCGV(ret) != GET_TCGV(arg))
         tcg_gen_op2(INDEX_op_mov_i32, ret, arg);
 }
 
+static inline void tcg_gen_mov_i32(TCGv ret, TCGv arg)
+{
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg);
+    tcg_gen_mov_i32_nocheck(ret, arg);
+}
+
 static inline void tcg_gen_movi_i32(TCGv ret, int32_t arg)
 {
+    CHECK_TCGV_32(ret);
     tcg_gen_op2i(INDEX_op_movi_i32, ret, arg);
 }
 
@@ -357,11 +365,16 @@
 
 static inline void tcg_gen_add_i32(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     tcg_gen_op3(INDEX_op_add_i32, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_addi_i32(TCGv ret, TCGv arg1, int32_t arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
     /* some cases can be optimized here */
     if (arg2 == 0) {
         tcg_gen_mov_i32(ret, arg1);
@@ -374,11 +387,16 @@
 
 static inline void tcg_gen_sub_i32(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     tcg_gen_op3(INDEX_op_sub_i32, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_subi_i32(TCGv ret, TCGv arg1, int32_t arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
     /* some cases can be optimized here */
     if (arg2 == 0) {
         tcg_gen_mov_i32(ret, arg1);
@@ -391,11 +409,16 @@
 
 static inline void tcg_gen_and_i32(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     tcg_gen_op3(INDEX_op_and_i32, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_andi_i32(TCGv ret, TCGv arg1, int32_t arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
     /* some cases can be optimized here */
     if (arg2 == 0) {
         tcg_gen_movi_i32(ret, 0);
@@ -410,11 +433,16 @@
 
 static inline void tcg_gen_or_i32(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     tcg_gen_op3(INDEX_op_or_i32, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_ori_i32(TCGv ret, TCGv arg1, int32_t arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
     /* some cases can be optimized here */
     if (arg2 == 0xffffffff) {
         tcg_gen_movi_i32(ret, 0xffffffff);
@@ -429,11 +457,16 @@
 
 static inline void tcg_gen_xor_i32(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     tcg_gen_op3(INDEX_op_xor_i32, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_xori_i32(TCGv ret, TCGv arg1, int32_t arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
     /* some cases can be optimized here */
     if (arg2 == 0) {
         tcg_gen_mov_i32(ret, arg1);
@@ -446,11 +479,16 @@
 
 static inline void tcg_gen_shl_i32(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     tcg_gen_op3(INDEX_op_shl_i32, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_shli_i32(TCGv ret, TCGv arg1, int32_t arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
     if (arg2 == 0) {
         tcg_gen_mov_i32(ret, arg1);
     } else {
@@ -462,11 +500,16 @@
 
 static inline void tcg_gen_shr_i32(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     tcg_gen_op3(INDEX_op_shr_i32, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_shri_i32(TCGv ret, TCGv arg1, int32_t arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
     if (arg2 == 0) {
         tcg_gen_mov_i32(ret, arg1);
     } else {
@@ -478,11 +521,16 @@
 
 static inline void tcg_gen_sar_i32(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     tcg_gen_op3(INDEX_op_sar_i32, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_sari_i32(TCGv ret, TCGv arg1, int32_t arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
     if (arg2 == 0) {
         tcg_gen_mov_i32(ret, arg1);
     } else {
@@ -495,6 +543,8 @@
 static inline void tcg_gen_brcond_i32(int cond, TCGv arg1, TCGv arg2, 
                                       int label_index)
 {
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     tcg_gen_op4ii(INDEX_op_brcond_i32, arg1, arg2, cond, label_index);
 }
 
@@ -502,18 +552,24 @@
                                        int label_index)
 {
     TCGv t0 = tcg_const_i32(arg2);
+    CHECK_TCGV_32(arg1);
     tcg_gen_brcond_i32(cond, arg1, t0, label_index);
     tcg_temp_free(t0);
 }
 
 static inline void tcg_gen_mul_i32(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     tcg_gen_op3(INDEX_op_mul_i32, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_muli_i32(TCGv ret, TCGv arg1, int32_t arg2)
 {
     TCGv t0 = tcg_const_i32(arg2);
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
     tcg_gen_mul_i32(ret, arg1, t0);
     tcg_temp_free(t0);
 }
@@ -521,27 +577,42 @@
 #ifdef TCG_TARGET_HAS_div_i32
 static inline void tcg_gen_div_i32(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     tcg_gen_op3(INDEX_op_div_i32, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_rem_i32(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     tcg_gen_op3(INDEX_op_rem_i32, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_divu_i32(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     tcg_gen_op3(INDEX_op_divu_i32, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_remu_i32(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     tcg_gen_op3(INDEX_op_remu_i32, ret, arg1, arg2);
 }
 #else
 static inline void tcg_gen_div_i32(TCGv ret, TCGv arg1, TCGv arg2)
 {
     TCGv t0;
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     t0 = tcg_temp_new(TCG_TYPE_I32);
     tcg_gen_sari_i32(t0, arg1, 31);
     tcg_gen_op5(INDEX_op_div2_i32, ret, t0, arg1, t0, arg2);
@@ -551,6 +622,9 @@
 static inline void tcg_gen_rem_i32(TCGv ret, TCGv arg1, TCGv arg2)
 {
     TCGv t0;
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     t0 = tcg_temp_new(TCG_TYPE_I32);
     tcg_gen_sari_i32(t0, arg1, 31);
     tcg_gen_op5(INDEX_op_div2_i32, t0, ret, arg1, t0, arg2);
@@ -560,6 +634,9 @@
 static inline void tcg_gen_divu_i32(TCGv ret, TCGv arg1, TCGv arg2)
 {
     TCGv t0;
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     t0 = tcg_temp_new(TCG_TYPE_I32);
     tcg_gen_movi_i32(t0, 0);
     tcg_gen_op5(INDEX_op_divu2_i32, ret, t0, arg1, t0, arg2);
@@ -569,6 +646,9 @@
 static inline void tcg_gen_remu_i32(TCGv ret, TCGv arg1, TCGv arg2)
 {
     TCGv t0;
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_32(arg1);
+    CHECK_TCGV_32(arg2);
     t0 = tcg_temp_new(TCG_TYPE_I32);
     tcg_gen_movi_i32(t0, 0);
     tcg_gen_op5(INDEX_op_divu2_i32, t0, ret, arg1, t0, arg2);
@@ -699,12 +779,17 @@
     tcg_gen_and_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), TCGV_HIGH(arg2));
 }
 
-static inline void tcg_gen_andi_i64(TCGv ret, TCGv arg1, int64_t arg2)
+static inline void tcg_gen_andi_i64_nocheck(TCGv ret, TCGv arg1, int64_t arg2)
 {
     tcg_gen_andi_i32(ret, arg1, arg2);
     tcg_gen_andi_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), arg2 >> 32);
 }
 
+static inline void tcg_gen_andi_i64(TCGv ret, TCGv arg1, int64_t arg2)
+{
+    tcg_gen_andi_i64_nocheck(TCGv ret, TCGv arg1, int64_t arg2)
+}
+
 static inline void tcg_gen_or_i64(TCGv ret, TCGv arg1, TCGv arg2)
 {
     tcg_gen_or_i32(ret, arg1, arg2);
@@ -819,12 +904,15 @@
 
 static inline void tcg_gen_mov_i64(TCGv ret, TCGv arg)
 {
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_64(arg);
     if (GET_TCGV(ret) != GET_TCGV(arg))
         tcg_gen_op2(INDEX_op_mov_i64, ret, arg);
 }
 
 static inline void tcg_gen_movi_i64(TCGv ret, int64_t arg)
 {
+    CHECK_TCGV_64(ret);
     tcg_gen_op2i(INDEX_op_movi_i64, ret, arg);
 }
 
@@ -894,71 +982,106 @@
 
 static inline void tcg_gen_add_i64(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_64(arg1);
+    CHECK_TCGV_64(arg2);
     tcg_gen_op3(INDEX_op_add_i64, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_addi_i64(TCGv ret, TCGv arg1, int64_t arg2)
 {
     TCGv t0 = tcg_const_i64(arg2);
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_64(arg1);
     tcg_gen_add_i64(ret, arg1, t0);
     tcg_temp_free(t0);
 }
 
 static inline void tcg_gen_sub_i64(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_64(arg1);
+    CHECK_TCGV_64(arg2);
     tcg_gen_op3(INDEX_op_sub_i64, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_subi_i64(TCGv ret, TCGv arg1, int64_t arg2)
 {
     TCGv t0 = tcg_const_i64(arg2);
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_64(arg1);
     tcg_gen_sub_i64(ret, arg1, t0);
     tcg_temp_free(t0);
 }
 
 static inline void tcg_gen_and_i64(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_64(arg1);
+    CHECK_TCGV_64(arg2);
     tcg_gen_op3(INDEX_op_and_i64, ret, arg1, arg2);
 }
 
-static inline void tcg_gen_andi_i64(TCGv ret, TCGv arg1, int64_t arg2)
+static inline void tcg_gen_andi_i64_nocheck(TCGv ret, TCGv arg1, int64_t arg2)
 {
     TCGv t0 = tcg_const_i64(arg2);
     tcg_gen_and_i64(ret, arg1, t0);
     tcg_temp_free(t0);
 }
 
+static inline void tcg_gen_andi_i64(TCGv ret, TCGv arg1, int64_t arg2)
+{
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_64(arg1);
+    tcg_gen_andi_i64_nocheck(ret, arg1, arg2);
+}
+
 static inline void tcg_gen_or_i64(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_64(arg1);
+    CHECK_TCGV_64(arg2);
     tcg_gen_op3(INDEX_op_or_i64, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_ori_i64(TCGv ret, TCGv arg1, int64_t arg2)
 {
     TCGv t0 = tcg_const_i64(arg2);
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_64(arg1);
     tcg_gen_or_i64(ret, arg1, t0);
     tcg_temp_free(t0);
 }
 
 static inline void tcg_gen_xor_i64(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_64(arg1);
+    CHECK_TCGV_64(arg2);
     tcg_gen_op3(INDEX_op_xor_i64, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_xori_i64(TCGv ret, TCGv arg1, int64_t arg2)
 {
     TCGv t0 = tcg_const_i64(arg2);
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_64(arg1);
     tcg_gen_xor_i64(ret, arg1, t0);
     tcg_temp_free(t0);
 }
 
 static inline void tcg_gen_shl_i64(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_64(arg1);
+    CHECK_TCGV_64(arg2);
     tcg_gen_op3(INDEX_op_shl_i64, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_shli_i64(TCGv ret, TCGv arg1, int64_t arg2)
 {
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_64(arg1);
     if (arg2 == 0) {
         tcg_gen_mov_i64(ret, arg1);
     } else {
@@ -970,11 +1093,16 @@
 
 static inline void tcg_gen_shr_i64(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_64(arg1);
+    CHECK_TCGV_64(arg2);
     tcg_gen_op3(INDEX_op_shr_i64, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_shri_i64(TCGv ret, TCGv arg1, int64_t arg2)
 {
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_64(arg1);
     if (arg2 == 0) {
         tcg_gen_mov_i64(ret, arg1);
     } else {
@@ -986,11 +1114,16 @@
 
 static inline void tcg_gen_sar_i64(TCGv ret, TCGv arg1, TCGv arg2)
 {
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_64(arg1);
+    CHECK_TCGV_64(arg2);
     tcg_gen_op3(INDEX_op_sar_i64, ret, arg1, arg2);
 }
 
 static inline void tcg_gen_sari_i64(TCGv ret, TCGv arg1, int64_t arg2)
 {
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_64(arg1);
     if (arg2 == 0) {
         tcg_gen_mov_i64(ret, arg1);
     } else {
@@ -1284,20 +1417,26 @@
    registers.  This will probably break MIPS64 targets.  */
 static inline void tcg_gen_trunc_i64_i32(TCGv ret, TCGv arg)
 {
-    tcg_gen_mov_i32(ret, arg);
+    CHECK_TCGV_32(ret);
+    CHECK_TCGV_64(arg);
+    tcg_gen_mov_i32_nocheck(ret, arg);
 }
 
 /* Note: we assume the target supports move between 32 and 64 bit
    registers */
 static inline void tcg_gen_extu_i32_i64(TCGv ret, TCGv arg)
 {
-    tcg_gen_andi_i64(ret, arg, 0xffffffffu);
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_32(arg);
+    tcg_gen_andi_i64_nocheck(ret, arg, 0xffffffffu);
 }
 
 /* Note: we assume the target supports move between 32 and 64 bit
    registers */
 static inline void tcg_gen_ext_i32_i64(TCGv ret, TCGv arg)
 {
+    CHECK_TCGV_64(ret);
+    CHECK_TCGV_32(arg);
     tcg_gen_ext32s_i64(ret, arg);
 }
 
tcg/tcg.h +22 -1
Index: qemu/tcg/tcg.h
===================================================================
@@ -116,20 +116,39 @@
    Users of tcg_gen_* don't need to know about any of this, and should
    treat TCGv as an opaque type.  */
 
-//#define DEBUG_TCGV 1
+#define DEBUG_TCGV 1
 
 #ifdef DEBUG_TCGV
 
 typedef struct
 {
     int n;
+#if TCG_TARGET_REG_BITS == 64
+    int size;
+#endif
 } TCGv;
 
 #define MAKE_TCGV(i) __extension__ \
   ({ TCGv make_tcgv_tmp = {i}; make_tcgv_tmp;})
+#define MAKE_TCGV32(i) __extension__ \
+    ({ TCGv make_tcgv_tmp = {i, 32}; make_tcgv_tmp;})
+#define MAKE_TCGV64(i) __extension__ \
+    ({ TCGv make_tcgv_tmp = {i, 64}; make_tcgv_tmp;})
 #define GET_TCGV(t) ((t).n)
+#define tcg_check_abort(want, got)                                      \
+    do {                                                                \
+        fprintf(stderr, "%s:%d: tcg fatal error: bad TCGV size "        \
+                "(wanted %d != got %d)\n",                              \
+                __FILE__, __LINE__, want, got);                         \
+        abort();                                                        \
+    } while (0)
 #if TCG_TARGET_REG_BITS == 32
 #define TCGV_HIGH(t) MAKE_TCGV(GET_TCGV(t) + 1)
+#define CHECK_TCGV_32(t) do {} while(0)
+#define CHECK_TCGV_64(t) do {} while(0)
+#else
+#define CHECK_TCGV_32(t) do { if (t.size != 32) tcg_check_abort(32, t.size);} while(0)
+#define CHECK_TCGV_64(t) do { if (t.size != 64) tcg_check_abort(64, t.size);} while(0)
 #endif
 
 #else /* !DEBUG_TCGV */
@@ -140,6 +159,8 @@
 #if TCG_TARGET_REG_BITS == 32
 #define TCGV_HIGH(t) ((t) + 1)
 #endif
+#define CHECK_TCGV_32(t) do {} while(0)
+#define CHECK_TCGV_64(t) do {} while(0)
 
 #endif /* DEBUG_TCGV */
 
tcg/tcg.c +12 -3
Index: qemu/tcg/tcg.c
===================================================================
@@ -289,7 +289,10 @@
     ts->name = name;
     s->nb_globals++;
     tcg_regset_set_reg(s->reserved_regs, reg);
-    return MAKE_TCGV(idx);
+    if (type == TCG_TYPE_I32)
+        return MAKE_TCGV32(idx);
+    else
+        return MAKE_TCGV64(idx);
 }
 
 #if TCG_TARGET_REG_BITS == 32
@@ -386,7 +389,10 @@
         ts->name = name;
         s->nb_globals++;
     }
-    return MAKE_TCGV(idx);
+    if (type == TCG_TYPE_I32)
+        return MAKE_TCGV32(idx);
+    else
+        return MAKE_TCGV64(idx);
 }
 
 TCGv tcg_temp_new_internal(TCGType type, int temp_local)
@@ -437,7 +443,10 @@
             s->nb_temps++;
         }
     }
-    return MAKE_TCGV(idx);
+    if (type == TCG_TYPE_I32)
+        return MAKE_TCGV32(idx);
+    else
+        return MAKE_TCGV64(idx);
 }
 
 void tcg_temp_free(TCGv arg)
#1 lore raw
Quoted 6 lines · Blue Swirl

Hi,

When emulating a mixed 32/64 bit Qemu target CPUs it's easy to confuse the TCGv size, passing 32 bit TCGv to a function expecting a 64 bit one and vice versa. This patch adds a run time sanity check for TCGv sizes.

Would it make more sense to push these down into tcg_gen_op* ? tcg-op.h is already fairly unwieldy.

I wonder if it's worth adding TCG_LOW to enable checking on 32-bit hosts.

For futureproofing I'd name things FOO_I32 rather than FOO_32.

Paul

author
#2 lore raw
Quoted 10 lines · Paul Brook
Quoted 6 lines · Blue Swirl

Hi,

When emulating a mixed 32/64 bit Qemu target CPUs it's easy to confuse the TCGv size, passing 32 bit TCGv to a function expecting a 64 bit one and vice versa. This patch adds a run time sanity check for TCGv sizes.

Would it make more sense to push these down into tcg_gen_op* ?

How? At that point we don't know what was the correct size.

Quoted 1 line

tcg-op.h is already fairly unwieldy.

True, and as debugging TCGv will not be common, I'm not sure whether the patch is worth committing.

Quoted 3 lines

I wonder if it's worth adding TCG_LOW to enable checking on 32-bit hosts.

For futureproofing I'd name things FOO_I32 rather than FOO_32.

Good point, will fix.

#3 lore raw
Quoted 4 lines · Blue Swirl
Quoted 3 lines · Paul Brook
Quoted 2 lines · Blue Swirl

Hi,

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

When emulating a mixed 32/64 bit Qemu target CPUs it's easy to confuse the TCGv size, passing 32 bit TCGv to a function expecting a 64 bit one and vice versa. This patch adds a run time sanity check for TCGv sizes.

Would it make more sense to push these down into tcg_gen_op* ?

How? At that point we don't know what was the correct size.

I figure there's only a handful of different cases, so it'll be cleaner to introduce tcg_gen_op_i32_i32 etc. which trivially reduce to tcg_gen_op2 when debugging is disabled.

Paul

author
#4 lore raw
Quoted 10 lines · Paul Brook
Quoted 9 lines · Blue Swirl
Quoted 8 lines · Paul Brook
Quoted 6 lines · Blue Swirl

Hi,

When emulating a mixed 32/64 bit Qemu target CPUs it's easy to confuse the TCGv size, passing 32 bit TCGv to a function expecting a 64 bit one and vice versa. This patch adds a run time sanity check for TCGv sizes.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Would it make more sense to push these down into tcg_gen_op* ?

How? At that point we don't know what was the correct size.

I figure there's only a handful of different cases, so it'll be cleaner to
introduce tcg_gen_op_i32_i32 etc. which trivially reduce to tcg_gen_op2 when debugging is disabled.

Now I see. I'll try that next.

#5 lore raw
In reply to Blue Swirl
Quoted 7 lines · Blue Swirl

Hi,

When emulating a mixed 32/64 bit Qemu target CPUs it's easy to confuse the TCGv size, passing 32 bit TCGv to a function expecting a 64 bit one and vice versa. This patch adds a run time sanity check for TCGv sizes.

Quoted 5 lines

Because a 32 bit Qemu host does not really use 64 bit TCGvs, the patch is only functional on a 64 bit host. Of course also a pure 32 bit Qemu target is not likely to suffer from TCGv size confusion.

Some use cases are not covered yet. Comments?

Theses tests can be done at compile time by introducing the TCGv_i32 and TCGv_i64 types. The same can be done with the helpers by using a few macros to declare them.

A optional runtime check would be still useful as an additional pass using the OP definitions to ensure that the TCG optimizations pass(es) are OK. IMHO, doing it only in tcg_gen_xxx is not enough.

Fabrice.

#6 lore raw
Quoted 12 lines · Fabrice Bellard
Quoted 11 lines · Blue Swirl

Hi,

When emulating a mixed 32/64 bit Qemu target CPUs it's easy to confuse the TCGv size, passing 32 bit TCGv to a function expecting a 64 bit one and vice versa. This patch adds a run time sanity check for TCGv sizes.

Because a 32 bit Qemu host does not really use 64 bit TCGvs, the patch is only functional on a 64 bit host. Of course also a pure 32 bit Qemu target is not likely to suffer from TCGv size confusion.

Quoted 5 lines
Quoted 1 line

Some use cases are not covered yet. Comments?

Theses tests can be done at compile time by introducing the TCGv_i32 and TCGv_i64 types. The same can be done with the helpers by using a few macros to declare them.

That would also require updating all the target code in translate.c to use these types. In principle there's no reason why this couldn't be done, but it'd be a much more invasive change.
AFAIK there's no way of doing compile time inheritance checking in C.

Paul

#7 lore raw
Quoted 13 lines · Paul Brook
Quoted 12 lines · Fabrice Bellard
Quoted 11 lines · Blue Swirl

Hi,

When emulating a mixed 32/64 bit Qemu target CPUs it's easy to confuse the TCGv size, passing 32 bit TCGv to a function expecting a 64 bit one and vice versa. This patch adds a run time sanity check for TCGv sizes.

Because a 32 bit Qemu host does not really use 64 bit TCGvs, the patch is only functional on a 64 bit host. Of course also a pure 32 bit Qemu target is not likely to suffer from TCGv size confusion.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Some use cases are not covered yet. Comments?

Theses tests can be done at compile time by introducing the TCGv_i32 and TCGv_i64 types. The same can be done with the helpers by using a few macros to declare them.

That would also require updating all the target code in translate.c to use these types. In principle there's no reason why this couldn't be done, but it'd be a much more invasive change.

If you define TCGv as the word size of the emulated CPU, it will eliminates most of the changes.

Quoted 1 line

AFAIK there's no way of doing compile time inheritance checking in C.

Fabrice.

author
#8 lore raw
Quoted 19 lines · Fabrice Bellard
Quoted 18 lines · Paul Brook
Quoted 16 lines · Fabrice Bellard
Quoted 7 lines · Blue Swirl

Hi,

When emulating a mixed 32/64 bit Qemu target CPUs it's easy to confuse the TCGv size, passing 32 bit TCGv to a function expecting a 64 bit one and vice versa. This patch adds a run time sanity check for TCGv sizes.

Quoted 5 lines

Because a 32 bit Qemu host does not really use 64 bit TCGvs, the patch is only functional on a 64 bit host. Of course also a pure 32 bit Qemu target is not likely to suffer from TCGv size confusion.

Some use cases are not covered yet. Comments?

Theses tests can be done at compile time by introducing the TCGv_i32 and TCGv_i64 types. The same can be done with the helpers by using a few macros to declare them.

Quoted 7 lines
Quoted 3 lines

That would also require updating all the target code in translate.c to use these types. In principle there's no reason why this couldn't be done, but it'd be a much more invasive change.

If you define TCGv as the word size of the emulated CPU, it will
eliminates most of the changes.

This version introduces TCGv_i32 and TCGv_i64. TCGv_ptr and TCGv (TL sized) are based on them.

For Sparc, the patch is very invasive (I just commented out the helpers to avoid that part) but I think i386 would need much smaller changes.

With the patch, I found some bugs in Sparc translation. I'm not sure what to do with helpers, there should be a way to declare the size of the arguments somehow and then the calling should be easier than:
tcg_gen_helper_1_4_i64_tl_i32_i32_i32(helper_ld_asi, dst, addr, r_asi, r_size, r_sign);

Otherwise, I think only some variant of the TCGV_LOW parts are worth committing, they make the code slightly more easy to understand.

target-sparc/translate.c +21 -19
Index: qemu/target-sparc/translate.c
===================================================================
@@ -302,7 +302,7 @@
     tcg_gen_brcondi_tl(TCG_COND_NE, r_temp, 0, l1);
     tcg_gen_ori_i32(cpu_psr, cpu_psr, PSR_ZERO);
     gen_set_label(l1);
-    tcg_gen_ext_i32_tl(r_temp, dst);
+    tcg_gen_ext32s_tl(r_temp, dst);
     tcg_gen_brcondi_tl(TCG_COND_GE, r_temp, 0, l2);
     tcg_gen_ori_i32(cpu_psr, cpu_psr, PSR_NEG);
     gen_set_label(l2);
@@ -752,8 +752,8 @@
     r_temp = tcg_temp_new_i64(TCG_TYPE_I64);
     r_temp2 = tcg_temp_new_i64(TCG_TYPE_I64);
 
-    tcg_gen_extu_i32_i64(r_temp, src2);
-    tcg_gen_extu_i32_i64(r_temp2, src1);
+    tcg_gen_extu_tl_i64(r_temp, src2);
+    tcg_gen_extu_tl_i64(r_temp2, src1);
     tcg_gen_mul_i64(r_temp2, r_temp, r_temp2);
 
     tcg_gen_shri_i64(r_temp, r_temp2, 32);
@@ -775,8 +775,8 @@
     r_temp = tcg_temp_new_i64(TCG_TYPE_I64);
     r_temp2 = tcg_temp_new_i64(TCG_TYPE_I64);
 
-    tcg_gen_ext_i32_i64(r_temp, src2);
-    tcg_gen_ext_i32_i64(r_temp2, src1);
+    tcg_gen_ext_tl_i64(r_temp, src2);
+    tcg_gen_ext_tl_i64(r_temp2, src1);
     tcg_gen_mul_i64(r_temp2, r_temp, r_temp2);
 
     tcg_gen_shri_i64(r_temp, r_temp2, 32);
@@ -2249,9 +2249,10 @@
                         r_tsptr = tcg_temp_new_ptr(TCG_TYPE_PTR);
                         tcg_gen_ld_ptr(r_tsptr, cpu_env,
                                        offsetof(CPUState, tsptr));
-                        tcg_gen_ld_i32(cpu_tmp0, r_tsptr,
+                        tcg_gen_ld_i32(cpu_tmp32, r_tsptr,
                                        offsetof(trap_state, tt));
                         tcg_temp_free_ptr(r_tsptr);
+                        tcg_gen_ext_i32_tl(cpu_tmp0, cpu_tmp32);
                     }
                     break;
                 case 4: // tick
@@ -2323,7 +2324,7 @@
                     CHECK_IU_FEATURE(dc, HYPV);
                     if (!hypervisor(dc))
                         goto priv_insn;
-                    tcg_gen_ext_i32_tl(cpu_tmp0, cpu_ssr);
+                    tcg_gen_mov_tl(cpu_tmp0, cpu_ssr);
                     break;
                 case 31: // ver
                     tcg_gen_mov_tl(cpu_tmp0, cpu_ver);
@@ -3036,7 +3037,7 @@
                         tcg_gen_sari_i64(cpu_dst, cpu_src1, rs2 & 0x3f);
                     } else {
                         tcg_gen_andi_i64(cpu_dst, cpu_src1, 0xffffffffULL);
-                        tcg_gen_ext_i32_i64(cpu_dst, cpu_dst);
+                        tcg_gen_ext32s_i64(cpu_dst, cpu_dst);
                         tcg_gen_sari_i64(cpu_dst, cpu_dst, rs2 & 0x1f);
                     }
                 } else {                /* register */
@@ -3048,7 +3049,7 @@
                     } else {
                         tcg_gen_andi_i64(cpu_tmp0, cpu_src2, 0x1f);
                         tcg_gen_andi_i64(cpu_dst, cpu_src1, 0xffffffffULL);
-                        tcg_gen_ext_i32_i64(cpu_dst, cpu_dst);
+                        tcg_gen_ext32s_i64(cpu_dst, cpu_dst);
                         tcg_gen_sar_i64(cpu_dst, cpu_dst, cpu_tmp0);
                     }
                 }
@@ -3434,7 +3435,8 @@
                                     r_tsptr = tcg_temp_new_ptr(TCG_TYPE_PTR);
                                     tcg_gen_ld_ptr(r_tsptr, cpu_env,
                                                    offsetof(CPUState, tsptr));
-                                    tcg_gen_st_i32(cpu_tmp0, r_tsptr,
+                                    tcg_gen_trunc_tl_i32(cpu_tmp32, cpu_tmp0);
+                                    tcg_gen_st_i32(cpu_tmp32, r_tsptr,
                                                    offsetof(trap_state, tt));
                                     tcg_temp_free_ptr(r_tsptr);
                                 }
@@ -3515,7 +3517,7 @@
                                 CHECK_IU_FEATURE(dc, HYPV);
                                 if (!hypervisor(dc))
                                     goto priv_insn;
-                                tcg_gen_trunc_tl_i32(cpu_ssr, cpu_tmp0);
+                                tcg_gen_mov_tl(cpu_ssr, cpu_tmp0);
                                 break;
                             default:
                                 goto illegal_insn;
@@ -4314,9 +4316,9 @@
                     CHECK_IU_FEATURE(dc, SWAP);
                     gen_movl_reg_TN(rd, cpu_val);
                     gen_address_mask(dc, cpu_addr);
-                    tcg_gen_qemu_ld32u(cpu_tmp32, cpu_addr, dc->mem_idx);
+                    tcg_gen_qemu_ld32u(cpu_tmp0, cpu_addr, dc->mem_idx);
                     tcg_gen_qemu_st32(cpu_val, cpu_addr, dc->mem_idx);
-                    tcg_gen_extu_i32_tl(cpu_val, cpu_tmp32);
+                    tcg_gen_mov_tl(cpu_val, cpu_tmp0);
                     break;
 #if !defined(CONFIG_USER_ONLY) || defined(TARGET_SPARC64)
                 case 0x10:      /* load word alternate */
@@ -4461,7 +4463,8 @@
                 switch (xop) {
                 case 0x20:      /* load fpreg */
                     gen_address_mask(dc, cpu_addr);
-                    tcg_gen_qemu_ld32u(cpu_fpr[rd], cpu_addr, dc->mem_idx);
+                    tcg_gen_qemu_ld32u(cpu_tmp0, cpu_addr, dc->mem_idx);
+                    tcg_gen_trunc_tl_i32(cpu_fpr[rd], cpu_tmp0);
                     break;
                 case 0x21:      /* ldfsr, V9 ldxfsr */
 #ifdef TARGET_SPARC64
@@ -4600,7 +4603,8 @@
                 switch (xop) {
                 case 0x24: /* store fpreg */
                     gen_address_mask(dc, cpu_addr);
-                    tcg_gen_qemu_st32(cpu_fpr[rd], cpu_addr, dc->mem_idx);
+                    tcg_gen_ext_i32_tl(cpu_tmp0, cpu_fpr[rd]);
+                    tcg_gen_qemu_st32(cpu_tmp0, cpu_addr, dc->mem_idx);
                     break;
                 case 0x25: /* stfsr, V9 stxfsr */
 #ifdef TARGET_SPARC64
@@ -4608,10 +4612,8 @@
                     tcg_gen_ld_i64(cpu_tmp64, cpu_env, offsetof(CPUState, fsr));
                     if (rd == 1)
                         tcg_gen_qemu_st64(cpu_tmp64, cpu_addr, dc->mem_idx);
-                    else {
-                        tcg_gen_trunc_tl_i32(cpu_tmp32, cpu_tmp64);
-                        tcg_gen_qemu_st32(cpu_tmp32, cpu_addr, dc->mem_idx);
-                    }
+                    else
+                        tcg_gen_qemu_st32(cpu_tmp64, cpu_addr, dc->mem_idx);
 #else
                     tcg_gen_ld_i32(cpu_tmp32, cpu_env, offsetof(CPUState, fsr));
                     tcg_gen_qemu_st32(cpu_tmp32, cpu_addr, dc->mem_idx);
#9 lore raw
Quoted 2 lines

This version introduces TCGv_i32 and TCGv_i64. TCGv_ptr and TCGv (TL sized) are based on them.

This is looking good to me.

Quoted 9 lines

For Sparc, the patch is very invasive (I just commented out the helpers to avoid that part) but I think i386 would need much smaller changes.

With the patch, I found some bugs in Sparc translation. I'm not sure what to do with helpers, there should be a way to declare the size of the arguments somehow and then the calling should be easier than:
tcg_gen_helper_1_4_i64_tl_i32_i32_i32(helper_ld_asi, dst, addr, r_asi, r_size, r_sign);

I think we can build some infrastructure to make helpers easier to handle. It should be relatively mechanical changes to the existing helper[s].h. ARM and m68k already do this, though need a bit more work to get static typechecking. All the information it there's it's just not quite in the right form. I'll see if I can dig out the CPP magic required for this.

Paul

#10 lore raw
Quoted 11 lines
Quoted 5 lines

With the patch, I found some bugs in Sparc translation. I'm not sure what to do with helpers, there should be a way to declare the size of the arguments somehow and then the calling should be easier than:
tcg_gen_helper_1_4_i64_tl_i32_i32_i32(helper_ld_asi, dst, addr, r_asi, r_size, r_sign);

I think we can build some infrastructure to make helpers easier to handle. It should be relatively mechanical changes to the existing helper[s].h. ARM and m68k already do this, though need a bit more work to get static typechecking. All the information it there's it's just not quite in the right form. I'll see if I can dig out the CPP magic required for this.

I've got something that works. Any chance you could resend you patch with a signed-off-by line so I can do the integration?

Paul

author
#11 lore raw
Quoted 13 lines · Paul Brook
Quoted 11 lines
Quoted 5 lines

With the patch, I found some bugs in Sparc translation. I'm not sure what to do with helpers, there should be a way to declare the size of the arguments somehow and then the calling should be easier than:
tcg_gen_helper_1_4_i64_tl_i32_i32_i32(helper_ld_asi, dst, addr, r_asi, r_size, r_sign);

I think we can build some infrastructure to make helpers easier to handle. It should be relatively mechanical changes to the existing helper[s].h. ARM and m68k already do this, though need a bit more work to get static typechecking. All the information it there's it's just not quite in the right form. I'll see if I can dig out the CPP magic required for this.

Quoted 2 lines

I've got something that works. Any chance you could resend you patch with a
signed-off-by line so I can do the integration?

I don't trust these much, but anyway: