Why Register Accesses Must Not Be Reordered or Cached
Reading a normal memory location twice gives the same answer, so compilers happily delete the second read, reorder accesses, or keep values in a processor register. Hardware does not work that way: reading a status register can clear it, and the order of two writes can be the difference between starting a transfer and corrupting one. Code that touches devices therefore has to use accessors that force every access to really happen, in the order written, or the driver will work at one optimisation level and fail at another.
Questions this Concept answers
- Why may a compiler legally delete a second read of a normal memory location but not a second read of a device register?
volatile (computer programming)
A variable is "volatile" when something outside your program's normal flow can change it, such as a hardware register mapped into memory. Compilers normally assume nothing else touches memory, so the…