Some surprising compiler stuff with a weird solution:
Here the "disassembly" of an intialization routine. It just assigns values to elements of two arrays.
It's not obvious but the first variables, enable_moon, reverse_modes etc are actually macro aliases to elements in a single array, OPT_ARRAYs. You can see the first LDI to R30 and R31 (Z) loads the array address. the R24 loads the value to set. Then the value is saved to Z+N, where N is different for each array element. That's basically ok... (for anyone looking carefully, R1 always contains 0, which is what most of those are being set to).
00000095 e2.e6 LDI R30,0x62 Load immediate
00000096 f0.e0 LDI R31,0x00 Load immediate
00000097 85.e5 LDI R24,0x55 Load immediate
00000098 80.87 STD Z+8,R24 Store indirect with displacement
567: enable_moon = INIT_ENABLE_MOON;
00000099 13.82 STD Z+3,R1 Store indirect with displacement
574: reverse_modes = INIT_REVERSE_MODES;
0000009A 14.82 STD Z+4,R1 Store indirect with displacement
579: memory = INIT_MEMORY;
0000009B 12.82 STD Z+2,R1 Store indirect with displacement
582: offtim3 = INIT_OFFTIM3;
0000009C 16.82 STD Z+6,R1 Store indirect with displacement
589: muggle_mode = INIT_MUGGLE_MODE;
0000009D 11.82 STD Z+1,R1 Store indirect with displacement
603: overrides[5]=GROUP_SELECT_MODE;
0000009E ad.e6 LDI R26,0x6D Load immediate
0000009F b0.e0 LDI R27,0x00 Load immediate
000000A0 8e.ef LDI R24,0xFE Load immediate
000000A1 15.96 ADIW R26,0x05 Add immediate to word
000000A2 8c.93 ST X,R24 Store indirect
000000A3 15.97 SBIW R26,0x05 Subtract immediate from word
605: overrides[7]=TEMP_CAL_MODE;
000000A4 8d.ef LDI R24,0xFD Load immediate
000000A5 17.96 ADIW R26,0x07 Add immediate to word
000000A6 8c.93 ST X,R24 Store indirect
614: modegroup = INIT_MODEGROUP;
000000A7 11.86 STD Z+9,R1 Store indirect with displacement
616: maxtemp = INIT_MAXTEMP; // temperature step-down threshold, each number is roughly equal to 4c degrees
000000A8 88.e5 LDI R24,0x58 Load immediate
000000A9 82.87 STD Z+10,R24 Store indirect with displacement
618: mode_idx=0; // initial mode
000000AA 61.2c MOV R6,R1 Copy register
1542: eepos=0;
000000AB 71.2c MOV R7,R1 Copy register
The Z register (R30 and R31) can store to an address (array pointer) plus a displacement (array offset), those instructions like Z+8.
.
Z is in use already and will be used again, so isn't used for the overrides array, which uses X instead. X cannot do store with displacement. So it loads the 0 position, and then it loads the offset for each element, and then it adds them together. And then it stores. But the zero and offsets are compile time constants, so why doesn't the compiler just compute the address of each element and load that instead of loading two different offsets and performing a sum? This is pretty surprising.
Worse, after adding 5 to write the 5th (ok 6th) element it subtracts 5 back off to reset the array pointer, and then adds 7. Really? The compiler couldn't at least figure out to just add 2 instead? I guess after the first error there's no reason to expect better.
But, assuming there's some good reason for all this, why doesn't it at least just move the overrrides initialization to the end where it can be done with Z? I mean these optimizing compilers re-arrange our code whenever we don't want them to. It's amazing how many discussions there are about getting interrupts to turn on and off when you want them to, and basically no guarantees they will, but it doesn't reorder this?
So I did exactly that. I moved those lines to the end. And then it did use Z, and saved 10 bytes! Whoohoo. (but this is just one thing I happened to notice). This makes the code organization a little uglier though.
Sometimes I'm amazed at what the optimizations can do. Sometimes I'm irritated, and sometimes like this I'm just very unimpressed.