diff mbox

fix casting to bool

Message ID 1368737766-6517-1-git-send-email-xi.wang@gmail.com (mailing list archive)
State Rejected, archived
Headers show

Commit Message

Xi Wang May 16, 2013, 8:56 p.m. UTC
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(+)

Comments

Pekka Enberg May 19, 2013, 7:47 a.m. UTC | #1
On Thu, May 16, 2013 at 11:56 PM, Xi Wang <xi.wang@gmail.com> wrote:
> 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>

Acked-by: Pekka Enberg <penberg@kernel.org>

> ---
>  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
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

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.
 	 */