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

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

[avr-gcc-list] Bad code generated when using volatile keyword/-O0 opt?


From: Rick Mann
Subject: [avr-gcc-list] Bad code generated when using volatile keyword/-O0 opt?
Date: Sat, 11 Feb 2006 22:43:33 -0800

Generating code for an ATmega128.

I've boiled this down to a pretty simple case, I think. In the following code, if compiled with -Os, if "d" is made volatile, then it never seems to get past execution in the first delay loop. If it's not volatile, it works correctly. If I compile with -O0, neither volatile nor non-volatile works (same failure mode).

I started experimenting with -O0 (and the volatile keyword) to try to make sure gdb was in sync between source and assembly.


---
#include <avr/io.h>

int
main()
{
        //      Set LED output pins…
        
        DDRC = _BV(PC5) | _BV(PC4);
        PORTC = 0;
        
        volatile unsigned char d;
        const unsigned char kD = 255;
        
        while (1)
        {
                PORTC = _BV(PC5);
                d = kD;
                while (d--);    //      first delay loop
                
                PORTC = _BV(PC4);
                d = kD;
                while (d--);    //      second delay loop
        }

        return 0;
}
---


With the volatile keyword, the .lst files shows this for the first loop:

---
  17:main.c        ****                 d = kD;
100                             .stabn  68,0,17,.LM4-main
101                     .LM4:
102 0016 2983                   std Y+1,r18
103                     .L3:
  18:main.c        ****                 while (d--);
104                             .stabn  68,0,18,.LM5-main
105                     .LM5:
106 0018 8981                   ldd r24,Y+1
107 001a 8150                   subi r24,lo8(-(-1))
108 001c 8983                   std Y+1,r24
109 001e 8981                   ldd r24,Y+1
110 0020 8F3F                   cpi r24,lo8(-1)
111 0022 D1F7                   brne .L3
  19:main.c        ****                 
---

And without volatile:

---
  99 0014 8FEF                  ldi r24,lo8(-1)
100                     .L3:
  17:main.c        ****                 d = kD;
  18:main.c        ****                 while (d--);
101                             .stabn  68,0,18,.LM4-main
102                     .LM4:
103 0016 8150                   subi r24,1
104 0018 F0F7                   brcc .L3
---

Not sure if this is a bug, but it sure seems like it to me. Let me know what other info you might need.

--
Rick


Attachment: smime.p7s
Description: S/MIME cryptographic signature


reply via email to

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