I've spent many hours the last week or two debugging a problem which is apparently the result of incorrect code generated with optimization set to -Os. The total code is fairly large (13,134 bytes). I've tried to reduce it to a minimum size but everything I've tried which is smaller works. The code works with no optimization and when I set the toolchain flavour to WinAVR-20100101.
The code is built for an ATmega644.
Here's the Native Studio 6 (Version 6.0.1703 - beta) -Os generated code:
ptr = IINCHIP_READ( Sn_TX_WR0( s ) ); 27b8: ed b7 in r30, 0x3d ; 61 27ba: fe b7 in r31, 0x3e ; 62 27bc: 36 96 adiw r30, 0x06 ; 6 27be: 0f b6 in r0, 0x3f ; 63 27c0: f8 94 cli 27c2: fe bf out 0x3e, r31 ; 62 27c4: 0f be out 0x3f, r0 ; 63 27c6: ed bf out 0x3d, r30 ; 61 27c8: c8 01 movw r24, r16 27ca: 0e 94 62 0e call 0x1cc4 ; 0x1cc4ptr = ( ( ptr & 0x00ff ) << 8 ) + IINCHIP_READ( Sn_TX_WR0( s ) + 1 ); 27ce: 20 e0 ldi r18, 0x00 ; 0 27d0: e9 01 movw r28, r18 27d2: c6 01 movw r24, r12 27d4: 0e 94 62 0e call 0x1cc4 ; 0x1cc4 27d8: c8 0f add r28, r24 27da: d1 1d adc r29, r1
And here's the WinAVR-20100101 -Os generated code:
ptr = IINCHIP_READ( Sn_TX_WR0( s ) ); 292e: ed b7 in r30, 0x3d ; 61 2930: fe b7 in r31, 0x3e ; 62 2932: 36 96 adiw r30, 0x06 ; 6 2934: 0f b6 in r0, 0x3f ; 63 2936: f8 94 cli 2938: fe bf out 0x3e, r31 ; 62 293a: 0f be out 0x3f, r0 ; 63 293c: ed bf out 0x3d, r30 ; 61 293e: c6 01 movw r24, r12 2940: 0e 94 58 0e call 0x1cb0 ; 0x1cb0ptr = ( ( ptr & 0x00ff ) << 8 ) + IINCHIP_READ( Sn_TX_WR0( s ) + 1 ); 2944: 18 2f mov r17, r24 2946: 00 e0 ldi r16, 0x00 ; 0 2948: c7 01 movw r24, r14 294a: 0e 94 58 0e call 0x1cb0 ; 0x1cb0 294e: 08 0f add r16, r24 2950: 11 1d adc r17, r1
The code makes 2 calls to IINCHIP_READ (which returns a uint8_t in r24) and combines the results in ptr, a uint16_t.
Note that the WinAVR-20100101 generated code saves the result from the first IINCHIP_READ call in r17 (mov 17, r24). The Native generated code doesn't use the result from the first IINCHIP_READ call at all.
The WinAVR-20100101 result is 0xD240 and the Native result is 0x0040.
I'd like to make a bug report but like I mentioned, I can only create this with a significant amount of code. Any ideas? Atmel support - should I submit a bug report and provide all my code?
Don