avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [avr-gcc-list] Endless loop: uchar c; for (c=1; c; c++)


From: Eric Weddington
Subject: RE: [avr-gcc-list] Endless loop: uchar c; for (c=1; c; c++)
Date: Tue, 06 Mar 2007 11:10:38 -0700

 

> -----Original Message-----
> From: 
> address@hidden 
> [mailto:address@hidden
> org] On Behalf Of Dmitry K.
> Sent: Tuesday, March 06, 2007 12:26 AM
> To: address@hidden
> Subject: [avr-gcc-list] Endless loop: uchar c; for (c=1; c; c++)
> 
> Hi,
> 
> wrong code by avr-gcc 4.1.1:
> 
> Program:
> ~~~~~~~
> 
> /* This program is compiled as endless loop: condition `c1 != 0'
>    is not tested.  In original (real) program there was an another
>    error: in conditional expression based on c1 and c2 inside the
>    loops (here this is removed).
> 
>    Compiler: avr-gcc 4.1.1
>    Options: -W -Wall -Os -S   */
> 
> void foo (const char *s1, const char *s2);
> 
> int main ()
> {
>     unsigned char c1, c2;
>     char s1[2] = ".";
>     char s2[2] = ".";
> 
>     for (c1 = 1; c1; c1++) {
>       for (c2 = 1; c2; c2++) {
>           s1[0] = c1;
>           s2[0] = c2;
>           foo (s1, s2);
>       }
>     }
>     return 0;
> }
> 
> Result:
> ~~~~~~
>       ...
> main:
> /* prologue: frame size=4 */
>       ldi r28,lo8(__stack - 4)
>       ldi r29,hi8(__stack - 4)
>       out __SP_H__,r29
>       out __SP_L__,r28
> /* prologue end (size=4) */
>       lds r24,.LC0
>       lds r25,(.LC0)+1
>       std Y+2,r25
>       std Y+1,r24
>       std Y+4,r25
>       std Y+3,r24
>       ldi r16,lo8(1)
>       rjmp .L2
> .L3:
>       std Y+1,r16
>       std Y+3,r17
>       mov r22,r28
>       mov r23,r29
>       subi r22,lo8(-(3))
>       sbci r23,hi8(-(3))
>       mov r24,r28
>       mov r25,r29
>       adiw r24,1
>       rcall foo
>       subi r17,lo8(-(1))
>       brne .L3
>       subi r16,lo8(-(1))
> .L2:
>       ldi r17,lo8(1)
>       rjmp .L3
> /* epilogue: frame size=4 */
> /* epilogue: noreturn */

Hmm. This is a bit strange.

It seems that when compiling to assembler (-S as Dmitry recommends above),
then you'll note that the epilogue is set to noreturn, which also ends up
taking out the final breq of the outer loop, as there is nothing to branch
to anymore.

However, when I compile and assemble, and I look at a requested listing from
the assembler (basically using the WinAVR Makefile Template), the code looks
fine. An epilog exists, and the correct end condition for the loop is in the
code. This is also the case when I do a disassembly on the object code
(avr-objdump -d <object>); the code looks fine, an epilog exists, and the
branch is in the code.

Does anyone have an idea why using avr-gcc -S ... would be so different then
the actual code generated (as compared to the disassembly)?

Eric





reply via email to

[Prev in Thread] Current Thread [Next in Thread]