bob wrote:
The code is very simple (runs in Port Change interrupt):
if ((PINA & _BV(A2DDATA)) != 0)
result |= 0x80000000;
result >>= 1;
Bob confirms (off-list) that variable 'result' is declared volatile. My
opinion is that this is why the compiler must load and store all 32 bits.
Bob, however, disagrees. So, maybe this is another discussion about the
exact meaning and effect of the volatile qualifier.
In my opinion, the compiler is not permitted to optimize accesses to
volatile variables, either load or store. We typically use the qualifier
because these accesses have side effects (they diddle the hardware) or the
variable is shared with another thread of execution (in this case, interrupt
and non-interrupt code). But the compiler is at liberty to optimize the
modification of the variable's value while it is in temporary store (on the
stack or in registers). So, in this case the compiler figures out that
modifying just the least significant byte will result in the same value as a
full 32-bit OR. It's not until the value gets written back to the variable
that it begins to matter again that the variable is volatile. The *value*
is not volatile, only the variable.
So, I think that the compiler is doing exactly what it should do.
I assume that in the non-interrupt code, interrupts are disabled when
accessing this variable, but that has nothing to do with the supposedly
missed optimization.
Graham.
_______________________________________________
AVR-chat mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/avr-chat