linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Xi Wang <xi.wang@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: sparse@chrisli.org, Xi Wang <xi.wang@gmail.com>
Subject: [PATCH] fix casting to bool
Date: Thu, 16 May 2013 16:56:06 -0400	[thread overview]
Message-ID: <1368737766-6517-1-git-send-email-xi.wang@gmail.com> (raw)

Consider the following function.

	static _Bool foo(int x) { return x; }

Currently sparse emits:

	scast.1     %r2 <- (32) %arg1
	ret.1       %r2

This is incorrect since bool requires a zero test.

	setne.32    %r2 <- %arg1, $0
	ret.1       %r2

This patch adds zero testing for bool in cast_to().

Signed-off-by: Xi Wang <xi.wang@gmail.com>
---
 evaluate.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/evaluate.c b/evaluate.c
index d9c767f..a090028 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -272,6 +272,18 @@ static struct expression * cast_to(struct expression *old, struct symbol *type)
 	if (old->ctype != &null_ctype && is_same_type(old, type))
 		return old;
 
+	/* bool requires a zero test */
+	if (is_bool_type(type)) {
+		expr = alloc_expression(old->pos, EXPR_COMPARE);
+		expr->op = SPECIAL_NOTEQUAL;
+		expr->ctype = type;
+		expr->left = old;
+		expr->right = alloc_expression(old->pos, EXPR_VALUE);
+		expr->right->ctype = old->ctype;
+		expr->right->value = 0;
+		return expr;
+	}
+
 	/*
 	 * See if we can simplify the op. Move the cast down.
 	 */
-- 
1.8.1.2


             reply	other threads:[~2013-05-16 21:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-16 20:56 Xi Wang [this message]
2013-05-19  7:47 ` [PATCH] fix casting to bool Pekka Enberg

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=1368737766-6517-1-git-send-email-xi.wang@gmail.com \
    --to=xi.wang@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    --cc=sparse@chrisli.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).