This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Attribute const and inline functions
- From: David Brown <david at westcontrol dot com>
- To: Sebastian Huber <sebastian dot huber at embedded-brains dot de>
- Cc: GCC Help <gcc-help at gcc dot gnu dot org>
- Date: Mon, 24 Feb 2014 16:40:45 +0100
- Subject: Re: Attribute const and inline functions
- Authentication-results: sourceware.org; auth=none
- References: <5308EC44 dot 4010708 at embedded-brains dot de> <530B178F dot 9080704 at westcontrol dot com> <530B36AB dot 1010003 at embedded-brains dot de> <530B602B dot 1060704 at westcontrol dot com> <530B66CB dot 6090604 at embedded-brains dot de>
On 24/02/14 16:35, Sebastian Huber wrote:
> On 2014-02-24 16:07, David Brown wrote:
>>> extern const int const_var;
>>> >
>>> >GCC will read the value of const_var again after a compiler memory
>>> >barrier (e.g. __asm__ volatile("" ::: "memory")).
>> That's the rules - a "memory clobber" says that/any/ memory may change,
>> and things read from memory could change and must therefore be re-read.
>> Specifying the extern var as "const" does not tell the compiler that
>> the value is constant - it simply tells the compiler that/you/ promise
>> not to change it. (It would be nice if C, or at least gcc, had a way to
>> say that the value is never changed, but it does not.)
>
> These variables go into the .rodata section. It seems a bit over
> paranoid to assume that they change. In my case the .rodata section is
> a read-only region covering a NOR flash, so its unlikely to change.
>
C does not have any way to express this - so there is no way for the
compiler to know that the value cannot change. (It might be able to do
so if you use LTO or whole-program optimisation, or if the constant were
static rather than extern.)
As an embedded programmer, I would like some way to say "this value will
/never/ change", as that would suit many common uses - but there is no
way (AFAIK) to do so.