This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

c++/787: g++ calls function multiple times for multiple pre-increment



>Number:         787
>Category:       c++
>Synopsis:       g++ calls function multiple times for multiple pre-increment
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Mon Nov 13 08:56:00 PST 2000
>Closed-Date:
>Last-Modified:
>Originator:     Christopher Eltschka
>Release:        gcc version 2.97 20001112 (experimental)
>Organization:
>Environment:
www.codesourcery.org online compiler

The same error occurs if compiling with egcs-1.0.3 on
Linux (i486-linux), and also if compiling and linking
with 2.95.2 on another Linux machine (i486-suse-linux),
and also with gcc-2.8.1 on OSF4 (alphaev5-dec-osf4.0d).

According to the CodeSourcery web info, the configure line of
the compiler is
../configure --prefix=/tmp/gcc-20001112-root/usr/local --enable-shared --enable-threads --disable-checking i386-pc-linux-gnu

For the other versions tested, I don't know the configure line.
>Description:
The attached source code should print

in foo
4
4

Instead it prints

in foo
in foo
in foo
in foo
2
2

Note that calling identity inserts sequence points between
the increments, and therefore causes the behaviour of the
program to be well defined (without the calls to identity
the behaviour would be undefined due to modifying the same
value without intervening sequence point)

The bug occurs without any compiler options (just
g++ bug.cc); however, the behaviour seems to be the
same if optimizing (I've only tested with optimizing on
egcs-1.0.3/Linux with levels -O1, -O2, -O3).

Looking at the assembly code shows that identity is
also called too often. Unless I'm misreading the
assembly code, the execution sequence in the problematic
statement is

foo, ++, foo, identity, ++, foo, ++, foo, identity,
 identity, ++, printf

The correct execution sequence would be

foo, ++, identity, ++, identity, ++, printf

It seems to me that after each increment except the last
one, the whole previous sequence is repeated before
going on. That would produce exactly the observed
sequence, as the following shows (repeated code
is added in parentheses to the code that should have
been generated)

foo, ++, (foo,) identity, ++, (foo, ++, foo, identity,)
 identity, ++, printf

As can be seen, the sequences in parentheses are exactly
the corresponding sequencea before the preceding ++
(including previous additional sequences).
>How-To-Repeat:

>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/octet-stream; name="bug.cc"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="bug.cc"

Ly8gI2luY2x1ZGUgPHN0ZGlvLmg+CmV4dGVybiAiQyIgaW50IHByaW50ZihjaGFyIGNvbnN0Kiwg
Li4uKTsKCmludCAmZm9vKGludCAmYXJnKQp7CiAgcHJpbnRmKCJpbiBmb29cbiIpOwogIGFyZz0x
OwogIHJldHVybiBhcmc7Cn0KCmludCYgaWRlbnRpdHkoaW50JiB4KSAKewogIHJldHVybiB4Owp9
CgppbnQgbWFpbigpewogIGludCBhOwovLy0tLS0tLS0tLS0gcHJvYmxlbSAtLS0tLS0tLS0tLS0t
LQogIHByaW50ZigiJWlcbiIsICsraWRlbnRpdHkoKytpZGVudGl0eSgrK2ZvbyhhKSkpICk7Ci8v
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tCiAgcHJpbnRmKCIlaVxuIiwgYSk7CiAg
cmV0dXJuIDA7Cn0K

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]