qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] Compile-time checking for shift operations
@ 2008-12-14 17:12 Stuart Brady
  0 siblings, 0 replies; only message in thread
From: Stuart Brady @ 2008-12-14 17:12 UTC (permalink / raw)
  To: qemu-devel

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 <stuart.brady@gmail.com>

Index: tcg/tcg-op.h
===================================================================
--- tcg/tcg-op.h	(revision 6028)
+++ tcg/tcg-op.h	(working copy)
@@ -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);
-- 
Stuart Brady

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2008-12-14 17:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-12-14 17:12 [Qemu-devel] [PATCH] Compile-time checking for shift operations Stuart Brady

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).