Compile-time checking for shift operations
PATCH 1/1 Compile-time checking for shift operations
This patch implements compile-time checking for the immediate versions of TCG's shift operations, ensuring that number of places shifted is within the correct range (i.e. greater than or equal to zero, but less than the TCGv's width), provided that the number of places shifted is a constant expression.
Signed-off-by Stuart Brady <[email protected]>
tcg-op.h +36 -0
@@ -25,6 +25,22 @@int gen_new_label (void ); +#if QEMU_GNUC_PREREQ (4 ,3 ) +extern void __attribute__((error ("Invalid shift" ))) +shift_error (void ); +#endif + +static inline void tcg_check_shift (int shift,int limit) +{ +#if QEMU_GNUC_PREREQ (4 ,3 ) +if (__builtin_constant_p (shift)) { +if (shift <0 || shift >= limit) { +shift_error (); + } + } +#endif +} +static inline void tcg_gen_op1_i32 (int opc,TCGv_i32 arg1) { *gen_opc_ptr++ = opc;
@@ -496,6 +512,8 @@static inline void tcg_gen_shli_i32 (TCGv_i32 ret,TCGv_i32 arg1,int32_t arg2) { +tcg_check_shift (arg2,32 ); +if (arg2 ==0 ) {tcg_gen_mov_i32 (ret, arg1); }else {
@@ -512,6 +530,8 @@static inline void tcg_gen_shri_i32 (TCGv_i32 ret,TCGv_i32 arg1,int32_t arg2) { +tcg_check_shift (arg2,32 ); +if (arg2 ==0 ) {tcg_gen_mov_i32 (ret, arg1); }else {
@@ -528,6 +548,8 @@static inline void tcg_gen_sari_i32 (TCGv_i32 ret,TCGv_i32 arg1,int32_t arg2) { +tcg_check_shift (arg2,32 ); +if (arg2 ==0 ) {tcg_gen_mov_i32 (ret, arg1); }else {
@@ -782,6 +804,8 @@static inline void tcg_gen_shli_i64 (TCGv_i64 ret,TCGv_i64 arg1,int64_t arg2) { +tcg_check_shift (arg2,64 ); +tcg_gen_shifti_i64 (ret, arg1, arg2,0 ,0 ); }
@@ -792,6 +816,8 @@static inline void tcg_gen_shri_i64 (TCGv_i64 ret,TCGv_i64 arg1,int64_t arg2) { +tcg_check_shift (arg2,64 ); +tcg_gen_shifti_i64 (ret, arg1, arg2,1 ,0 ); }
@@ -802,6 +828,8 @@static inline void tcg_gen_sari_i64 (TCGv_i64 ret,TCGv_i64 arg1,int64_t arg2) { +tcg_check_shift (arg2,64 ); +tcg_gen_shifti_i64 (ret, arg1, arg2,1 ,1 ); }
@@ -1601,6 +1629,8 @@static inline void tcg_gen_rotli_i32 (TCGv_i32 ret,TCGv_i32 arg1,int32_t arg2) { +tcg_check_shift (arg2,32 ); +/* some cases can be optimized here */ if (arg2 ==0 ) {tcg_gen_mov_i32 (ret, arg1);
@@ -1618,6 +1648,8 @@static inline void tcg_gen_rotli_i64 (TCGv_i64 ret,TCGv_i64 arg1,int64_t arg2) { +tcg_check_shift (arg2,64 ); +/* some cases can be optimized here */ if (arg2 ==0 ) {tcg_gen_mov_i64 (ret, arg1);
@@ -1663,6 +1695,8 @@static inline void tcg_gen_rotri_i32 (TCGv_i32 ret,TCGv_i32 arg1,int32_t arg2) { +tcg_check_shift (arg2,32 ); +/* some cases can be optimized here */ if (arg2 ==0 ) {tcg_gen_mov_i32 (ret, arg1);
@@ -1673,6 +1707,8 @@static inline void tcg_gen_rotri_i64 (TCGv_i64 ret,TCGv_i64 arg1,int64_t arg2) { +tcg_check_shift (arg2,64 ); +/* some cases can be optimized here */ if (arg2 ==0 ) {tcg_gen_mov_i64 (ret, arg1);
Signature · 1 line
Stuart Brady