This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
AL vs EAX optimizer bug in egcs-1.1.2 on x86
- To: gcc-bugs at gcc dot gnu dot org
- Subject: AL vs EAX optimizer bug in egcs-1.1.2 on x86
- From: "Scott McPeak" <smcpeak at cs dot berkeley dot edu>
- Date: Fri, 25 Aug 2000 21:33:19 -0700 (PDT)
(I don't know if anyone cares about tracking egcs bugs anymore. But I
still use it, and I couldn't find a list of known bugs, so ...)
NOTE: The bug I'm reporting is *not* present in gcc-2.95.2.
Version:
gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
System:
Linux leetch 2.2.13 #2 Wed May 24 18:36:44 PDT 2000 i686 unknown
Source code:
/* optimbug.c */
int main()
{
int x = 0; /* start with all bits 0 */
/* little endian: sets x to 1 */
/* big endian: sets x to a large number */
*((char*)(&x)) = 1;
if (x == 1) {
exit(0); /* success will mean little endian */
}
else {
exit(1); /* big endian */
}
}
If this code is compiled by
gcc optimbug.c
then a.out will exit with status code 0 on my little-endian machine.
But if the code is compiled by
gcc -O2 optimbug.c
then a.out will exit with status code 1!
here is assembly dump of egcs-1.1.2 optimized version:
Dump of assembler code for function main:
0x80483b4 <main>: push %ebp
0x80483b5 <main+1>: mov %esp,%ebp
0x80483b7 <main+3>: mov $0x1,%al <-- the bug is 'al'
0x80483b9 <main+5>: cmp $0x1,%eax
0x80483bc <main+8>: jne 0x80483c8 <main+20>
0x80483be <main+10>: push $0x0
0x80483c0 <main+12>: call 0x8048308 <exit>
0x80483c5 <main+17>: lea 0x0(%esi),%esi
0x80483c8 <main+20>: push $0x1
0x80483ca <main+22>: call 0x8048308 <exit>
End of assembler dump.
and here is an assembly dump of gcc-2.95.2 optimized (where
the problem does *not* occur):
Dump of assembler code for function main:
0x80483f0 <main>: push %ebp
0x80483f1 <main+1>: mov %esp,%ebp
0x80483f3 <main+3>: sub $0x8,%esp
0x80483f6 <main+6>: mov $0x1,%eax <-- correctly uses 'eax'
0x80483fb <main+11>: cmp $0x1,%eax
0x80483fe <main+14>: jne 0x8048410 <main+32>
0x8048400 <main+16>: add $0xfffffff4,%esp
0x8048403 <main+19>: push $0x0
0x8048405 <main+21>: call 0x8048308 <exit>
0x804840a <main+26>: lea 0x0(%esi),%esi
0x8048410 <main+32>: add $0xfffffff4,%esp
0x8048413 <main+35>: push $0x1
0x8048415 <main+37>: call 0x8048308 <exit>
End of assembler dump.
Do what you will with this report. I've already got a workaround
(make 'x' global).
-Scott