Accessing hardware registers mapped in address space requires the use of volatile pointer - to leave all accesses in the result and not optimize them away. But writing to volatile pointer in C++ may result in extra read instruction right after write, which may obviously trigger some undesired action in hardware, as memory access actually accesses mmapped hardware register. The program below demonstrates this behaviour, when compiled with bash$ gcc -O2 -S test_volatile_ptr.cpp Here is a short snippet of assembly output, which you should look for: movl $128, (%eax) ... movl (%eax), %eax ; I don't like this one bash$ gcc -v Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs gcc version 2.96 20000731 (Red Hat Linux 7.3 2.96-110) Complete source code: cat >test_volatile_ptr.cpp <<EOF // <test_volatile_ptr.cpp> // Pointer to hardware registers mapped in our address space. // volatile is to prevent read/write optimizations. volatile unsigned long *addr; // Helper function to set memory mapped register. inline void _set (unsigned long _offs, unsigned long _data) { addr[_offs] = _data; } // This one produces correct assembler output. void init_good () { addr[0] = 0x80; } // This one adds extra read after write, // Which in case of memory mapped hardware access // may trigger undesired action in hardware. void init_bad () { _set (0, 0x80); } // </test_volatile_ptr.cpp> EOF
Fixed in at least 3.2.2, time to update your gcc.