tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] More fun with comparison between pointer and integer.


From: Dave Dodge
Subject: Re: [Tinycc-devel] More fun with comparison between pointer and integer...
Date: Wed, 5 Sep 2007 04:25:59 -0400
User-agent: Mutt/1.5.13 (2006-08-11)

On Wed, Sep 05, 2007 at 02:33:58AM -0500, Rob Landley wrote:
> On Wednesday 05 September 2007 1:02:03 am Dave Dodge wrote:
> > > I fixed the "ptr || ptr" bit not working (check hg), and I just made it
> > > stop warning me about "comparison between pointer and int" for && and ||,
> > > but now it's saying "initializer element not constant".
> >
> > Well looking at 6.6, I'm having a hard time figuring out how to fit
> > the above into one of the described forms of "constant expression".

> It's a pointer to a constant string.  It's in a read-only section of
> memory.  More to the point, the or is testing whether or not it's
> nonzero, so the actual _value_ of the pointer goes away and all we
> need to retain is that it wasn't NULL.

I agree with all that; just saying the Standard doesn't explicitly
permit it.  Pretty much the only way it seems to allow using a
pointer/address to an object _anywhere_ in a constant expression is in
sizeof context, or when the final expression is an address (and then
there are restrictions that are pretty obviously designed to make the
address suitable for relocation).  Even simply comparing a
constant-valued pointer to null seems to put you in
implementation-defined territory.

> *shrug*  GCC handles this just fine, that's why I was doing it.  The 
> alternative is ugly and brittle #ifdefs instead of letting the optimizer 
> eliminate dead code naturally.

Have not tried this, but maybe you could add another layer to trick
tcc into taking it:

  (ptr != NULL) || (ptr != NULL) || ... || 0

Or even more hackish, _if_ you know that in this expression your
strings are going to be given as string literals (not pointers) and
have more or less characters than there are in the type produced by
NULL, then:

  ((sizeof ptr) != (sizeof NULL))

                                                  -Dave Dodge




reply via email to

[Prev in Thread] Current Thread [Next in Thread]