Another day, another avr-gcc bug. I found this while working on ArduinoShrink, testing with the Arduino IDE 1.8.13/avr-gcc 7.3.0.
The bug is that avr-gcc does not read from flash (use lpm) when indexing an extern __flash array. When using the same array as a pointer, it works correctly. What's surprising to me is not the bug but the workaround. One of the first things I learned about C over 30 years ago is that array indexing and pointer arithmetic are identical, and that compilers internally change one into the other. i.e.
char arr[] = {1, 2, 3}; arr[1]; *(arr + 1); // same
Since the plus '+' operator is commutative, something that surprises the typical Arduino user (but is obvious to us old C programmers), is that this even works:
int a1[] = {1, 2, 3, 4, 5}; int main() { return 2[a1]; }
Here's the Arduino code for those that wish to test it:
https://gist.github.com/nerdralp...
To see the generated code, go to your system temp directory, look for arduino_build, and run avr-objdump -d on the ELF.
p.s. I chose to post this in the AVR subform, because the compilers and general programming subforum is for, " topics not specifically related to a particular microcontroller". This bug is specific to avr-gcc, and therefore the topic is specifically related to classic 8-bit AVR MCUs.