public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* target/10702: incorrect code generated on Sparc for expression combining increment and decrement of same variable
@ 2003-05-09 12:36 Raphael.Quinet
  0 siblings, 0 replies; 2+ messages in thread
From: Raphael.Quinet @ 2003-05-09 12:36 UTC (permalink / raw)
  To: gcc-gnats


>Number:         10702
>Category:       target
>Synopsis:       incorrect code generated on Sparc for expression combining increment and decrement of same variable
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Fri May 09 12:36:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     Raphael Quinet
>Release:        3.2.1
>Organization:
>Environment:
sparc-sun-solaris2.6 and sparc-sun-solaris2.8

Output from gcc -v:
Reading specs from /Local/lib/gcc-lib/sparc-sun-solaris2.6/3.2.1/specs
Configured with: ../gcc-3.2.1/configure --prefix=/Local --with-local-prefix=/Local --with-gnu-ld --with-gnu-as --with-ld=/Local/bin-gnu/ld --with-as=/Local/bin-gnu/as --enable-threads --disable-shared --enable-languages=c,c++,f77,objc --disable-libgcj
Thread model: posix
gcc version 3.2.1

In case this can be useful, "ld" and "as" are from binutils-2.13.1
>Description:
The code included below does not provide the expected
result when compiled on a sparc-sun-solaris platform.  It
does work on Intel platforms.

Expected result: the return value of the program should be 2 (x is incremented once and decremented once).

Observed result with gcc-3.2.1 (and older versions such as
2.95): 1.  Only the first operation is executed (x--).
------------------------
int x = 2;

int
main (int argc,
      char *argv[])
{
  int y;

  y = x-- * x++;
  return x;
}
------------------------

You can also test this by replacing "return x" by some
printf() statements if you want to see the result printed
instead of having to check the return code of the program.

Additional comments:
- Using "char" or "short" instead of "int" produces the
  correct result.  The bug affects (long) integers and
  pointers.
- This can be tested with various expressions including
  more than one operation on the same variable.  For
  example, this problem can be tested with pointers by
  declaring x to be of type "char *" and using the
  expression "y = (x++ == x--);".
- Only the first operation in the expression produces its
  expected side effect on x.  So using "y = x++ * x--" will
  only increment x by 1, while "y = x-- * x++" will only
  decrement x by 1.
- It is necessary to assign the result of the expression to
  some variable or to use it in some way in order to trigger
  this bug.  If you replace "y = x-- * x++" by the simpler
  statement "x-- * x++", then the result will be correct.
- Using the option -Wall or -Wsequence-point produces the
  expected warning "operation on `x' may be undefined", but
  I would have expected the result of the expression to be
  undefined, not the final value of x.  Also, the same
  program returns the correct value on other platforms, such
  as i*86-gnu-linux.
>How-To-Repeat:
gcc -Wall test.c
./a.out
echo $?
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


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

* Re: target/10702: incorrect code generated on Sparc for expression combining increment and decrement of same variable
@ 2003-05-09 12:49 ehrhardt
  0 siblings, 0 replies; 2+ messages in thread
From: ehrhardt @ 2003-05-09 12:49 UTC (permalink / raw)
  To: Raphael.Quinet, gcc-bugs, gcc-prs, nobody

Synopsis: incorrect code generated on Sparc for expression combining increment and decrement of same variable

State-Changed-From-To: open->closed
State-Changed-By: cae
State-Changed-When: Fri May  9 12:49:11 2003
State-Changed-Why:
    Not a bug. You're invoking undefined behaviour. There is no
    sequence point beteween the two postfix operations on x, hence
    there is no guarantee that the result of the first operation is written
    back into x before the second operation.
    
        regards    Christian

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=10702


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

end of thread, other threads:[~2003-05-09 12:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-05-09 12:36 target/10702: incorrect code generated on Sparc for expression combining increment and decrement of same variable Raphael.Quinet
2003-05-09 12:49 ehrhardt

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).