From: Stuart Brady <sdbrady@ntlworld.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH] Compile-time checking for shift operations
Date: Sun, 14 Dec 2008 17:12:46 +0000 [thread overview]
Message-ID: <20081214171246.GA13983@miranda.arrow> (raw)
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
reply other threads:[~2008-12-14 17:11 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20081214171246.GA13983@miranda.arrow \
--to=sdbrady@ntlworld.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).