[Top][All Lists]

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

Re: [avr-gcc-list] A Question Of Optmization

From: David Brown
Subject: Re: [avr-gcc-list] A Question Of Optmization
Date: Wed, 16 Apr 2008 22:25:58 +0200
User-agent: Thunderbird (Windows/20080213)

Graham Davies wrote:
David Brown wrote:
In fact, the compiler will *always* do *exactly*
what your program code asks ...

Although the general thrust of your argument is correct, this statement may not be true, depending on how the reader interprets "what your program code asks". The language definition requires the compiler to produce code which *has the same final effect* as what you have written, but in the interests of optimization, it is permitted to deviate from doing each and every thing you have asked it to do. It is thinking that the compiler must do exactly what you have asked that leads people to omit the volatile keyword in the first place.

That's really what I meant - as far as it is possible to observe your system, the code generated by the compiler does exactly what your program asks. But I think you've explained it better than I did - my wording was open to misinterpretation.

The volatile keyword tells the compiler that things happen to or are caused by a variable that would otherwise be unknown to the compiler when it translates a unit of code. This is an indication that the compiler cannot evaluate the final effect of access to that variable during the translation. Therefore, it translates each access exactly as it is written. In fact, then, you can only be sure that "the compiler will *always* do *exactly* what your program code asks" if your variables are volatile.

My point was that the compiler is actually doing exactly what you ask it even if it "optimises out" code or data. Using "volatile" does not change whether the compiler generates code that "does exactly what your program asks for" (since the compiler will always generate such code) - rather, it changes the meaning of your source code. You get different generated code because you asked for different code, not because you asked the compiler to view it differently or optimise differently.

As I said above, I'm not sure I was very clear in my post - your explanation here should help others.

You are right that fiddling with optimization flags is not required to have the same effect and is not the way to fix the problem. I think the important thing here is to read the manual for your compiler as this won't be specified by the language standard.

Absolutely - you can't know your compiler and target too well in embedded programming.

Someone pointed out to me that this sort of discussion is somewhat off-topic for this mailing list. I hope it is helpful to some people, however - newcomers to avr-gcc often have trouble with things like "volatile", because avr-gcc has much more advanced optimisations than most other avr compilers.

reply via email to

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