This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Wierd bug with volatile pointer expressions
- To: gcc-bugs at gcc dot gnu dot org
- Subject: Wierd bug with volatile pointer expressions
- From: Eric Doenges <doenges at lpr dot e-technik dot tu-muenchen dot de>
- Date: Wed, 13 Sep 2000 10:04:01 +0200 (MET DST)
Hi all,
I found the following wierd behaviour (which I think is a bug) of
gcc-2.95.2. Consider the following code:
-----------------------------------------------------------------
#define WIERD ({\
*((volatile short *)1000) |= 5;\
while (*((volatile short *)1000) != 5);\
})
#define OK {\
*((volatile short *)1000) |= 5;\
while (*((volatile short *)1000) != 5);\
}
void test1(void)
{
WIERD;
}
void test2(void)
{
OK;
}
'test1' is compiled (m68k-coff-gcc -S -mcpu32 -O3 -Wall -fno-builtin
-gcoff test.c) to:
[ ... ]
move.w #1000,%a0
move.w (%a0),%d0
or.w #5,%d0
move.w %d0,(%a0)
move.w (%a0),%d0
.even
.L5:
move.w (%a0),%d0
cmp.w #5,%d0
jbne .L5
[ ... ]
Note the superfluous move.w (%a0),%d0 before the .even - d0 is loaded with
the exact same value in the next statement.
'test2' looks like this:
[ ... ]
move.w #1000,%a0
move.w (%a0),%d0
or.w #5,%d0
move.w %d0,(%a0)
.even
.L9:
move.w (%a0),%d0
cmp.w #5,%d0
jbne .L9
[ ... ]
Here, there is no unecessary move.
-----------------------------------------------------------------
The only difference in the code is that 'WIERD' is defined with round
brackets. So why is this a bug ? Suppose that reading memory address 1000
triggers some hardware function. In this case, having unwanted references
to memory is clearly a problem.
Without the 'volatile' keyword, the superflous move is not emitted.
The error occurs on all Version of gcc-2.95.2 availible to me, i.e. native
Compilers on i386-linux, alpha-dec-osf4.0f and sparc-sun-solaris2.8, as
well as the m68k-coff cross compiler on i386-linux.
--
________________________________________________________________________
Dipl.-Ing. Eric Doenges http://www.rcs.ei.tum.de
Institute for Real-Time Computer Systems (RCS) fon +49-89-289-23590
Technische Universitaet Muenchen, D-80290 Muenchen fax +49-89-289-23555