* [RFC][PATCH] fix casting constant to _Bool
@ 2012-06-16 6:55 Xi Wang
2012-06-16 7:10 ` Pekka Enberg
2012-06-16 16:32 ` Linus Torvalds
0 siblings, 2 replies; 6+ messages in thread
From: Xi Wang @ 2012-06-16 6:55 UTC (permalink / raw)
To: linux-sparse; +Cc: Christopher Li, Pekka Enberg, Xi Wang
Casting to _Bool requires a zero test rather than truncation.
Signed-off-by: Xi Wang <xi.wang@gmail.com>
---
A simple example is:
_Bool x = 0x200;
x should have been true rather than false.
BTW, this patch also disables the warning "cast truncates bits from
constant value". Does that sound good?
A more serious problem is something like:
_Bool foo(int x) { return x; }
sparse emits:
scast.1 %r2 <- (32) %arg1
which makes sparse-llvm generate:
%R2 = trunc i32 %0 to i1
My experimental backend "splay" has to treat _Bool specifically to
generate:
%0 = icmp ne i32 %x, 0
Should we leave the conversion job to backends, or should we just fix
the sparse IR (e.g., emitting setne rather than scast for casts to _Bool)?
---
expand.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/expand.c b/expand.c
index 63a9075..ee818a4 100644
--- a/expand.c
+++ b/expand.c
@@ -92,6 +92,12 @@ void cast_value(struct expression *expr, struct symbol *newtype,
value = get_longlong(old);
Int:
+ // _Bool requires a zero test rather than truncation.
+ if (is_bool_type(newtype)) {
+ expr->value = value ? 1 : 0;
+ return;
+ }
+
// Truncate it to the new size
signmask = 1ULL << (new_size-1);
mask = signmask | (signmask-1);
--
1.7.9.5
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [RFC][PATCH] fix casting constant to _Bool
2012-06-16 6:55 [RFC][PATCH] fix casting constant to _Bool Xi Wang
@ 2012-06-16 7:10 ` Pekka Enberg
2012-06-16 16:32 ` Linus Torvalds
1 sibling, 0 replies; 6+ messages in thread
From: Pekka Enberg @ 2012-06-16 7:10 UTC (permalink / raw)
To: Xi Wang; +Cc: linux-sparse, Christopher Li, Linus Torvalds, Jeff Garzik
On Sat, Jun 16, 2012 at 9:55 AM, Xi Wang <xi.wang@gmail.com> wrote:
> Casting to _Bool requires a zero test rather than truncation.
>
> Signed-off-by: Xi Wang <xi.wang@gmail.com>
> ---
> A simple example is:
>
> _Bool x = 0x200;
>
> x should have been true rather than false.
>
> BTW, this patch also disables the warning "cast truncates bits from
> constant value". Does that sound good?
That's unfortunate.
> A more serious problem is something like:
>
> _Bool foo(int x) { return x; }
>
> sparse emits:
>
> scast.1 %r2 <- (32) %arg1
>
> which makes sparse-llvm generate:
>
> %R2 = trunc i32 %0 to i1
>
> My experimental backend "splay" has to treat _Bool specifically to
> generate:
>
> %0 = icmp ne i32 %x, 0
>
> Should we leave the conversion job to backends, or should we just fix
> the sparse IR (e.g., emitting setne rather than scast for casts to _Bool)?
It'd be best if the backend didn't have to do anything special here.
Can we have it both ways? Not disable the warning but still deal with
it in the frontend?
> ---
> expand.c | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/expand.c b/expand.c
> index 63a9075..ee818a4 100644
> --- a/expand.c
> +++ b/expand.c
> @@ -92,6 +92,12 @@ void cast_value(struct expression *expr, struct symbol *newtype,
> value = get_longlong(old);
>
> Int:
> + // _Bool requires a zero test rather than truncation.
> + if (is_bool_type(newtype)) {
> + expr->value = value ? 1 : 0;
> + return;
> + }
> +
> // Truncate it to the new size
> signmask = 1ULL << (new_size-1);
> mask = signmask | (signmask-1);
> --
> 1.7.9.5
>
> --
> 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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [RFC][PATCH] fix casting constant to _Bool
2012-06-16 6:55 [RFC][PATCH] fix casting constant to _Bool Xi Wang
2012-06-16 7:10 ` Pekka Enberg
@ 2012-06-16 16:32 ` Linus Torvalds
2012-06-16 16:59 ` Dan Carpenter
1 sibling, 1 reply; 6+ messages in thread
From: Linus Torvalds @ 2012-06-16 16:32 UTC (permalink / raw)
To: Xi Wang; +Cc: linux-sparse, Christopher Li, Pekka Enberg
On Fri, Jun 15, 2012 at 11:55 PM, Xi Wang <xi.wang@gmail.com> wrote:
> Casting to _Bool requires a zero test rather than truncation.
Ack. _Bool is very special (and rather non-C-like).
> BTW, this patch also disables the warning "cast truncates bits from
> constant value". Does that sound good?
For cast to _Bool, that is correct. Since it doesn't truncate the
value, the upper bits are not ignored as they are with other casts.
Linus
--
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [RFC][PATCH] fix casting constant to _Bool
2012-06-16 16:32 ` Linus Torvalds
@ 2012-06-16 16:59 ` Dan Carpenter
2012-06-16 17:20 ` Linus Torvalds
0 siblings, 1 reply; 6+ messages in thread
From: Dan Carpenter @ 2012-06-16 16:59 UTC (permalink / raw)
To: Linus Torvalds; +Cc: Xi Wang, linux-sparse, Christopher Li, Pekka Enberg
On Sat, Jun 16, 2012 at 09:32:50AM -0700, Linus Torvalds wrote:
> On Fri, Jun 15, 2012 at 11:55 PM, Xi Wang <xi.wang@gmail.com> wrote:
> > Casting to _Bool requires a zero test rather than truncation.
>
> Ack. _Bool is very special (and rather non-C-like).
>
> > BTW, this patch also disables the warning "cast truncates bits from
> > constant value". Does that sound good?
>
> For cast to _Bool, that is correct. Since it doesn't truncate the
> value, the upper bits are not ignored as they are with other casts.
>
It's pretty rare to see a cast to bool that truncates the value.
My feeling is that most times they are bugs. I just saw one of
these a couple weeks ago:
http://www.spinics.net/lists/linux-iio/msg05573.html
I wish the error message could be fixed so it says 1 instead of 0.
old: warning: cast truncates bits from constant value (2 becomes 0)
new: warning: cast truncates bits from constant value (2 becomes 1)
regards,
dan carpenter
--
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [RFC][PATCH] fix casting constant to _Bool
2012-06-16 16:59 ` Dan Carpenter
@ 2012-06-16 17:20 ` Linus Torvalds
2012-06-17 18:44 ` Xi Wang
0 siblings, 1 reply; 6+ messages in thread
From: Linus Torvalds @ 2012-06-16 17:20 UTC (permalink / raw)
To: Dan Carpenter; +Cc: Xi Wang, linux-sparse, Christopher Li, Pekka Enberg
On Sat, Jun 16, 2012 at 9:59 AM, Dan Carpenter <dan.carpenter@oracle.com> wrote:
>
> It's pretty rare to see a cast to bool that truncates the value.
> My feeling is that most times they are bugs. I just saw one of
> these a couple weeks ago:
>
> http://www.spinics.net/lists/linux-iio/msg05573.html
>
> I wish the error message could be fixed so it says 1 instead of 0.
Hmm - I could imagine havinbg a warning, but it shouldn't be about truncation.
Maybe something like "Odd constant _Bool cast (%lld -> 1)"? It will
always result in 1, since the only value that results in 0 is 0 (and
isn't odd).
Linus
--
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [RFC][PATCH] fix casting constant to _Bool
2012-06-16 17:20 ` Linus Torvalds
@ 2012-06-17 18:44 ` Xi Wang
0 siblings, 0 replies; 6+ messages in thread
From: Xi Wang @ 2012-06-17 18:44 UTC (permalink / raw)
To: Linus Torvalds; +Cc: Dan Carpenter, linux-sparse, Christopher Li, Pekka Enberg
On Jun 16, 2012, at 1:20 PM, Linus Torvalds wrote:
> Maybe something like "Odd constant _Bool cast (%lld -> 1)"? It will
> always result in 1, since the only value that results in 0 is 0 (and
> isn't odd).
Sounds good. I will send a v2.
- xi
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2012-06-17 18:44 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-06-16 6:55 [RFC][PATCH] fix casting constant to _Bool Xi Wang
2012-06-16 7:10 ` Pekka Enberg
2012-06-16 16:32 ` Linus Torvalds
2012-06-16 16:59 ` Dan Carpenter
2012-06-16 17:20 ` Linus Torvalds
2012-06-17 18:44 ` Xi Wang
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).