linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pekka Enberg <penberg@kernel.org>
To: Xi Wang <xi.wang@gmail.com>
Cc: linux-sparse@vger.kernel.org, Christopher Li <sparse@chrisli.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Jeff Garzik <jeff@garzik.org>
Subject: Re: [RFC][PATCH] fix casting constant to _Bool
Date: Sat, 16 Jun 2012 10:10:18 +0300	[thread overview]
Message-ID: <CAOJsxLGAQ2Dn-NiJ1FkD+JxL2Bp7ni-jwptgT99QrW21KrdFzg@mail.gmail.com> (raw)
In-Reply-To: <1339829720-2069-1-git-send-email-xi.wang@gmail.com>

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

  reply	other threads:[~2012-06-16  7:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-16  6:55 [RFC][PATCH] fix casting constant to _Bool Xi Wang
2012-06-16  7:10 ` Pekka Enberg [this message]
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

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=CAOJsxLGAQ2Dn-NiJ1FkD+JxL2Bp7ni-jwptgT99QrW21KrdFzg@mail.gmail.com \
    --to=penberg@kernel.org \
    --cc=jeff@garzik.org \
    --cc=linux-sparse@vger.kernel.org \
    --cc=sparse@chrisli.org \
    --cc=torvalds@linux-foundation.org \
    --cc=xi.wang@gmail.com \
    /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).