gcc bug?

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello!

I have following simple c-program for ATTiny13:

http://pastebin.com/f513f62a3

For some reason if I use some optimizing (something else than -O0), gcc thinks that

				if((t100ms - t_pulsestart100ms) > PULSEHIGH)

will never come true and optimizes away all code below that. With -O0 everything works.

Is it bug or what?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm guessing that the compiler assumes that t_pulsestart100ms will never change. Try declaring it as volatile?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Search for the many threads on volatile.

t100ms is changed in the ISR, but main() does not know that t100ms may change out of main()'s control.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And read the avr-libc FAQ. The volatile keyword is explained in item #1.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ahh.. damn volatile. Usually gcc has handled globals as volatile, but seems that gcc-avr doesn't...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Usually gcc has handled globals as volatile

Only if you were building without optimisation. FAQ#1 applies to all variants of GCC as far as I know:

http://www.nongnu.org/avr-libc/u...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think in a multithreaded program, volatile is still needed to tell the compiler that the variable's value may change outside of the current code flow.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

An alternative to using volatile is to use inline assember statements with "memory" in the clobber list or a call to an external function.

kmr wrote:
I think in a multithreaded program, volatile is still needed to tell the compiler that the variable's value may change outside of the current code flow.

In a multi-processor system (eg. typical new PC) volatile is not sufficient unless you are using a Microsoft compiler or don't care about the order the variables change.

Also beware of the possibility of an interrupt during an access of a variable.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

All good points, Timothy. But, I was responding directly to the point

Quote:
Usually gcc has handled globals as volatile
I should have put that quote in my post so the context would be clear.