public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: Is this a gcc bug?
@ 2001-01-12 19:14 dewar
  2001-01-12 19:36 ` Alexandre Oliva
  0 siblings, 1 reply; 45+ messages in thread
From: dewar @ 2001-01-12 19:14 UTC (permalink / raw)
  To: aoliva, dewar; +Cc: Anshil, gcc

<<undefined behavior: expression modifies `x' multiple times
>>

Of course it is also improper to write

 x + x++

and it would seem peculiar to say that this modifies x multiple times!

^ permalink raw reply	[flat|nested] 45+ messages in thread
* Is this a GCC bug?
@ 2008-06-16 13:01 Bingfeng Mei
  2008-06-16 13:33 ` Bingfeng Mei
  0 siblings, 1 reply; 45+ messages in thread
From: Bingfeng Mei @ 2008-06-16 13:01 UTC (permalink / raw)
  To: gcc

Hello,
I encountered a problem in porting GCC (4.3.0) when I tried to make
contructor/destructor work. The following is the error message compiling
crtstuff.c.  

../../src/gcc/crtstuff.c: In function 'call___do_global_ctors_aux': 
../../src/gcc/crtstuff.c:562: error: expected string literal before '('
token 

Line 562 is:
...
CRT_CALL_STATIC_FUNCTION (INIT_SECTION_ASM_OP, __do_global_ctors_aux)
...

The following is how the macro CRT_CALL_STATIC_FUNCTION is defined. 

#ifndef CRT_CALL_STATIC_FUNCTION
# define CRT_CALL_STATIC_FUNCTION(SECTION_OP, FUNC) \
static void __attribute__((__used__))   \
call_ ## FUNC (void)     \
{       \
  asm (SECTION_OP);     \
  FUNC ();      \
  FORCE_CODE_SECTION_ALIGN    \
  asm (TEXT_SECTION_ASM_OP);    \
}
#endif

Here is the C code of line 562 after preprocessing in our porting:
static void __attribute__((__used__)) call___do_global_ctors_aux (void)
{ asm ("\t.section\t.init"); __do_global_ctors_aux (); asm
((firepath_fnsc ? "\t.section .textc, \"axC\"" : "\t.section .text,
\"axU\"")); }


The error is because inline asm only accepts string literal, not an
expression.  Our definitiion of TEXT_SECTION_ASM_OP is a string depends
on one of our particular targets. 

According to GCC internal manual, it is clearly stated that
TEXT_SECTION_ASM_OP can be an expression. 

TEXT_SECTION_ASM_OP
A C expression whose value is a string, including spacing, containing
the assembler
operation that should precede instructions and read-only data. Normally
"\t.text"
is right.

So I guess either internal manual or crtstuff.c is wrong. Or I am making
some stupid mistake here. Could someone have a look at this?  Thanks in
advance.

Cheers,
Bingfeng Mei
Broadcom UK

^ permalink raw reply	[flat|nested] 45+ messages in thread
* Re: Is this a gcc bug?
@ 2001-01-17 23:30 Axel Kittenberger
  0 siblings, 0 replies; 45+ messages in thread
From: Axel Kittenberger @ 2001-01-17 23:30 UTC (permalink / raw)
  To: gcc

Falk Hueffner wrote:
> > 
> > Michael Eager <eager@mvista.com> writes:
> > 
> > > My sugguestion:
> > >
> > >       undefined behavior:  value of expression using 'x' is undefined
> > 
> > This would seem to imply that the behaviour is undefined because one
> > uses an undefined value. But that's not the case: the standard says
> > program behaviour is undefined even if you say x = ++x and never look
> > at x again. (At least IIRC.)
> 
> It is the result of the expression (including any side effects) which is 
> undefined.  An expression has a value, even if you never use it.
> 
> There are many constructs with undefined behavior.  Some can be
> recognized at compile time; most cannot.  

How about the compile-error:
"detected undefined behaviour: [some actual explanation]"?

..if gcc is able to see it in this case?

-- 
Sent through GMX FreeMail - http://www.gmx.net

^ permalink raw reply	[flat|nested] 45+ messages in thread
* Re: Is this a gcc bug?
@ 2001-01-16 17:06 dewar
  0 siblings, 0 replies; 45+ messages in thread
From: dewar @ 2001-01-16 17:06 UTC (permalink / raw)
  To: dewar, eager; +Cc: Anshil, aoliva, gcc

<<I don't think that even a brilliant optimizer can make any conclusion
about j's value, based on the assumption that j!=4 is the only situation
where the expression is well defined.
>>

On the contrary. The logic goes like this:

1. If j == 4, then the program does undefined things, it can do anything
it wants.

2. The compiler therefore has complete freedom to generate any code it
likes if j==4.

3. One special case of this freedom is that it can generate code that does
the same thing as if j != 4.

4. This is a good choice since it eliminates a test.

So an optimizer can indeed make this assumption, and it is quite difficult
to word an informal standard to avoid this (it may be even harder to word
a formal standard to prevent this).

undefined is a nasty semantic concept :-)

^ permalink raw reply	[flat|nested] 45+ messages in thread
* Re: Is this a gcc bug?
@ 2001-01-14 16:14 dewar
  2001-01-14 16:53 ` Dave Korn
  0 siblings, 1 reply; 45+ messages in thread
From: dewar @ 2001-01-14 16:14 UTC (permalink / raw)
  To: dewar, geoffk; +Cc: amylaar, gcc

<<It doesn't follow, though, that the printf output is not interleaved
with the mandatory diagnostics (if any).
>>

That's different from my reading, my reading is that the mandatory
diagnostics have to come out even if the section of code is not run.

The printf output comes out only if the section of code is run.

Of course if there are mandatory diagnostics, then exeution is
undefined in any case.

^ permalink raw reply	[flat|nested] 45+ messages in thread
* Re: Is this a gcc bug?
@ 2001-01-14  5:31 dewar
  2001-01-14 15:51 ` Geoff Keating
  0 siblings, 1 reply; 45+ messages in thread
From: dewar @ 2001-01-14  5:31 UTC (permalink / raw)
  To: amylaar, geoffk; +Cc: gcc

<<The standard doesn't distinguish between 'run time' and 'compile
time'.
>>

That's a misunderstanding, yes, of course the standard distinguishes
between dynamic and static semantics. When you write a printf statement,
it does not generate output at compile time, and the standard is pretty
clear on this point :-)

^ permalink raw reply	[flat|nested] 45+ messages in thread
* Re: Is this a gcc bug?
@ 2001-01-12 19:15 dewar
  2001-01-12 19:29 ` Fergus Henderson
  0 siblings, 1 reply; 45+ messages in thread
From: dewar @ 2001-01-12 19:15 UTC (permalink / raw)
  To: dewar, fjh; +Cc: Anshil, aoliva, gcc

<<For the Mercury compiler, many error messages have two different forms,
one of which includes additional information such as hints about possible
causes of the error.  By default, the compiler prints the concise versions,
plus a line at the end of the compiler output saying "For more information,
recompile with `-E'", which is the compiler option to enable the more
verbose messages.  I think this is a good compromise.
>>

But that's a response about verbosity, my question which I repeat:

> So the question is, should an error message like this try to educate,
> or just take the simple minded non-determinstic viewpoint.

is about point of view. These are orthogonal issues.

^ permalink raw reply	[flat|nested] 45+ messages in thread
* Re: Is this a gcc bug?
@ 2001-01-12 18:21 dewar
  2001-01-12 18:29 ` Joseph S. Myers
                   ` (4 more replies)
  0 siblings, 5 replies; 45+ messages in thread
From: dewar @ 2001-01-12 18:21 UTC (permalink / raw)
  To: Anshil, aoliva; +Cc: gcc

<<undefined behavior: expression may modify `x' multiple times
>>

That's probably OK in practice, but it is of course a lie, at least
one of omission, the sentence after the colon does represent a
possible outcome. But we could also write:

undefined behavior: expression may not modify x at all.

Now both are equally correct semantically, though of course the first
one is more likely to represent what the code does EXCEPT that clever
optimizers can make all sorts of assumptions that result in strange
behavior. For example:

    x = y[4]++ + y[j]++;

the optimizer is allowed to conclude that j!=4, and to propagate this
information both forwards and backwards. The backwards propagation
can be especially surprising. Suppose that just before is the statement:

   if (j != 4) delete_system_disk();

then the compiler could in theory call delete system disk without 
testing the value of j at all :-)

So the question is, should an error message like this try to educate,
or just take the simple minded non-determinstic viewpoint.

One of the things that happens as C compilers optimize more, is that people
who make improper assumptions can run into serious trouble, so I think there
is some argument for education here.

^ permalink raw reply	[flat|nested] 45+ messages in thread
* Re: Is this a gcc bug?
@ 2001-01-12  1:18 Axel Kittenberger
  2001-01-12 18:14 ` Alexandre Oliva
  0 siblings, 1 reply; 45+ messages in thread
From: Axel Kittenberger @ 2001-01-12  1:18 UTC (permalink / raw)
  To: gcc

yuppi, a warning brainstorming :-)

how about: 
-----------
bug.i:8: unpredictable behaviour due to undefined evaluation sequence of
'x'
-----------
it's not the shortest, but I think it's both correct and understanding.

I donnot know how gcc warning polices are, but wouldn't it be desirable
having this warning enabled by default? since the original syntax is never
correct c.

-- 
Sent through GMX FreeMail - http://www.gmx.net

^ permalink raw reply	[flat|nested] 45+ messages in thread
* Re: Is this a gcc bug?
@ 2001-01-11 10:38 dewar
  0 siblings, 0 replies; 45+ messages in thread
From: dewar @ 2001-01-11 10:38 UTC (permalink / raw)
  To: bernds, rearnsha; +Cc: gcc, jbuck, per

<<Expression containing 'x' has undefined meaning (more than one
interpretation possible).
>>

I find that wrong enough to be confusing, the problem is not that
there are multiple interpretations, it is that the result is
undefined. You do NOT want to teach people, incorrectly, that
this has non-deterministic (but otherwise defined) semantics,
since that's not what the standard says. The current error
message is better than the above suggestion.

^ permalink raw reply	[flat|nested] 45+ messages in thread
* Re: Is this a gcc bug?
@ 2001-01-11 10:30 dewar
  0 siblings, 0 replies; 45+ messages in thread
From: dewar @ 2001-01-11 10:30 UTC (permalink / raw)
  To: per, rearnsha; +Cc: bernds, gcc

<<How about, multiple side-effects on 'x' between sequence points?
>>

That seems at the wrong level to me, someone who knows what a sequence
point is will understand the original message, someone who does not
will be mystefied. I would say something like "in a single statement".
Yes, it's not accurate, but trying to be too accurate can confused
the uninitiated :-)

^ permalink raw reply	[flat|nested] 45+ messages in thread
* Re: Is this a gcc bug?
@ 2001-01-11 10:05 dewar
  0 siblings, 0 replies; 45+ messages in thread
From: dewar @ 2001-01-11 10:05 UTC (permalink / raw)
  To: bernds, per; +Cc: gcc

<<I must say this is a very confusing error message, especally to
someone who does is not aware of the problem.  I'm not sure I could
even begin to guess what it meant!
>>

Well it is hard to make error messages meaningful in a case where someone
does not know the language. At the very least this warning will warn someone
that this is the case (that there is something they don't know about). For
anyone who knows C, this is certainly an OK message.

I am all in favor of trying to make the message a little clearer if possible,
but the important thing is to have SOME warning come out that will warn
someone that they either are doing something wrong that they will understand
immediately, or that they need help because there is something they don't
understand.

it is always tricky to know whether to aim messages at someone who knows
the language or someone who does not, or somewhere in between.

(reminds me of when my son worked at Tower Records, in the classical
dept, and when someone would ask "what's playing", he had to make a
judgment on whether to say

Beethoven's Fifth

or

This is the remastered Berlin Philharmonic performance with Karjan conducting

and if he got it wrong, he got either a blank stare, or annoyance :-)

^ permalink raw reply	[flat|nested] 45+ messages in thread
* RE: Is this a gcc bug?
@ 2001-01-11 10:00 David Korn
  2001-01-11 21:36 ` Andy Walker
  0 siblings, 1 reply; 45+ messages in thread
From: David Korn @ 2001-01-11 10:00 UTC (permalink / raw)
  To: gcc

>> > How about something like:
>> >         warning: 'x' may have unexpected value because of 
>side-effects
>> > or
>> >         warning: side-effects make value of 'x' undefined
>> > or
>> >         warning: undefined order of side-effects on 'x'
>> > One might replace "side-effects" by "updates" or "changes to".
>> 
>> How about, multiple side-effects on 'x' between sequence points?
>
>"sequence points" is standardese and doesn't mean anything to 
>most users.
>Even "side-effects" may be surprising -- I don't think a user thinks
>of the increment of x in x++ as a "side effect".
>
>How about:
>
>warning: the order of the modifications to 'x' is undefined

  How about:

warning: extra-nasal demons invoked on line #xxx, please report to 
comp.lang.c to be flamed.

       DaveK  <g>

ps. Of course, we'd be within the language spec to emit code to format the
user's hard drive at that point, but let's be merciful. Maybe it should
just kick off nethack, or compile a quick version of wumpus or somesuch....
-- 
The Boulder Pledge: "Under no circumstances will I ever purchase anything 
offered to me as the result of an unsolicited email message. Nor will I 
forward chain letters, petitions, mass mailings, or virus warnings to large 
numbers of others. This is my contribution to the survival of the online
community." 


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************

^ permalink raw reply	[flat|nested] 45+ messages in thread
* RE: Is this a gcc bug?
@ 2001-01-11  7:07 dewar
  0 siblings, 0 replies; 45+ messages in thread
From: dewar @ 2001-01-11  7:07 UTC (permalink / raw)
  To: bernds, dewar; +Cc: dkorn, gcc, uros

<<Already there.  You just have to turn on warnings.
>>

Fair enough, I did not realize this particular warning was there (since
I have never written any code that did that myself :-)

^ permalink raw reply	[flat|nested] 45+ messages in thread
* RE: Is this a gcc bug?
@ 2001-01-11  5:53 dewar
  2001-01-11  6:06 ` Bernd Schmidt
  0 siblings, 1 reply; 45+ messages in thread
From: dewar @ 2001-01-11  5:53 UTC (permalink / raw)
  To: dkorn, gcc, uros

it seems like it would not be hard to implement a warning for the case where
a variable is incremented or decremented somewhere in a statement, and
referenced elsewhere (this is not all the difficult cases, but is a very
high fraction of them). This would also catch macros where this happens
less visibly.

^ permalink raw reply	[flat|nested] 45+ messages in thread
* Re: Is this a gcc bug?
@ 2001-01-11  5:49 dewar
  0 siblings, 0 replies; 45+ messages in thread
From: dewar @ 2001-01-11  5:49 UTC (permalink / raw)
  To: gcc, uros

  I would expect the result to be "0, 5, 7, 22".

That's the only strange thing here, you have no right to expect this. You
are assuming left to right semantics, but there is no such requirement in
C. 

^ permalink raw reply	[flat|nested] 45+ messages in thread
* RE: Is this a gcc bug?
@ 2001-01-11  3:04 David Korn
  0 siblings, 0 replies; 45+ messages in thread
From: David Korn @ 2001-01-11  3:04 UTC (permalink / raw)
  To: 'Uros Bizjak', gcc

>From: Uros Bizjak [ mailto:uros@kss-loka.si ]
>Sent: 11 January 2001 10:11

>  This code produces a strange result on Solaris 2.5.1 with gcc 2.8.1:
>
>--cut here--
>
>#include <stdio.h>
>int main()
>{
>        int x = 0;
>        int y = 5;
>        int z = 15;
>
>        printf ("%i, %i, %i, %i\n", x,  x += y, x += 2, x += z); 
>        return 0;
>}
>
>--cut here--
>
>  It displays "22, 5, 7, 22".
>  I would expect the result to be "0, 5, 7, 22".

  The code isn't valid.  It is part of the language specification that you
cannot rely on the order of evaluation of arguments to a function call, nor
can you rely on when the side effects take place.  There's a technical
thing about these things called sequence points and how you're only allowed
to change the value of a variable once between them, but that's quite 
in-depth.  If you called 

   printf("%d %d %d\n", x++, x++, x++);

the compiler is free to pass the same value for all three occurrences of x
and add three to it after the function call, or to evaluate the arguments
from left to right and pass 0, 1, 2, or from right to left and pass 2, 1, 0,
or indeed (since the language spec says this is undefined behaviour) to 
output a program that plays space invaders when you run it.  Steer clear
of such ambiguous constructs and you'll be ok.

      DaveK
-- 
The Boulder Pledge: "Under no circumstances will I ever purchase anything 
offered to me as the result of an unsolicited email message. Nor will I 
forward chain letters, petitions, mass mailings, or virus warnings to large 
numbers of others. This is my contribution to the survival of the online
community." 


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************

^ permalink raw reply	[flat|nested] 45+ messages in thread
* Is this a gcc bug?
@ 2001-01-11  2:11 Uros Bizjak
  2001-01-11  3:04 ` Alexandre Oliva
  0 siblings, 1 reply; 45+ messages in thread
From: Uros Bizjak @ 2001-01-11  2:11 UTC (permalink / raw)
  To: gcc

Hello!

  This code produces a strange result on Solaris 2.5.1 with gcc 2.8.1:

--cut here--

#include <stdio.h>
int main()
{
        int x = 0;
        int y = 5;
        int z = 15;

        printf ("%i, %i, %i, %i\n", x,  x += y, x += 2, x += z); 
        return 0;
}

--cut here--

  It displays "22, 5, 7, 22".
  I would expect the result to be "0, 5, 7, 22".

	Uros.

^ permalink raw reply	[flat|nested] 45+ messages in thread

end of thread, other threads:[~2008-06-16 13:33 UTC | newest]

Thread overview: 45+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-01-12 19:14 Is this a gcc bug? dewar
2001-01-12 19:36 ` Alexandre Oliva
  -- strict thread matches above, loose matches on Subject: below --
2008-06-16 13:01 Is this a GCC bug? Bingfeng Mei
2008-06-16 13:33 ` Bingfeng Mei
2001-01-17 23:30 Is this a gcc bug? Axel Kittenberger
2001-01-16 17:06 dewar
2001-01-14 16:14 dewar
2001-01-14 16:53 ` Dave Korn
2001-01-14  5:31 dewar
2001-01-14 15:51 ` Geoff Keating
2001-01-12 19:15 dewar
2001-01-12 19:29 ` Fergus Henderson
2001-01-12 18:21 dewar
2001-01-12 18:29 ` Joseph S. Myers
2001-01-12 18:49 ` Alexandre Oliva
2001-01-12 18:58 ` Fergus Henderson
2001-01-13 10:34 ` James Dennett
2001-01-16 17:01 ` Michael Eager
2001-01-17  3:21   ` Falk Hueffner
2001-01-17 14:57     ` Michael Eager
2001-01-12  1:18 Axel Kittenberger
2001-01-12 18:14 ` Alexandre Oliva
2001-01-12 19:11   ` Fergus Henderson
2001-01-13  3:21   ` Richard Earnshaw
2001-01-11 10:38 dewar
2001-01-11 10:30 dewar
2001-01-11 10:05 dewar
2001-01-11 10:00 David Korn
2001-01-11 21:36 ` Andy Walker
2001-01-13 18:45   ` Joern Rennecke
2001-01-14  2:21     ` Geoff Keating
2001-01-11  7:07 dewar
2001-01-11  5:53 dewar
2001-01-11  6:06 ` Bernd Schmidt
2001-01-11  8:40   ` Per Bothner
2001-01-11  9:03     ` Richard Earnshaw
2001-01-11  9:27       ` Alexandre Oliva
2001-01-11  9:33       ` Joe Buck
2001-01-11  9:38         ` Bernd Schmidt
2001-01-11  9:44           ` Richard Earnshaw
2001-01-11 10:57     ` Neil Booth
2001-01-11  5:49 dewar
2001-01-11  3:04 David Korn
2001-01-11  2:11 Uros Bizjak
2001-01-11  3:04 ` Alexandre Oliva

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 read-only IMAP folder(s) and NNTP newsgroup(s).