This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Attribute const and inline functions


On 24/02/14 19:03, Oleg Endo wrote:
> On Mon, 2014-02-24 at 16:40 +0100, David Brown wrote:
>> 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.
> 
> On some targets you can specify variable attributes to accomplish
> exactly this.  See also
> http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html#AVR-Variable-Attributes
> and
> http://gcc.gnu.org/onlinedocs/gcc/Named-Address-Spaces.html#AVR%20Named%20Address%20Spaces
> 

As the saying goes, close but no cigar.

These attributes tell the compiler that the data in question is put in a
particular section (so the linker can put it in the right flash
section), and that a particular type of instruction is used to access
the data (on the AVR, "read from flash" is a different instruction from
"read from ram").  But they don't say anything about the read/write
characteristics of the data.  The best you can do is the same as for
ordinary data - by adding "const" you promise that /you/ will not change
the data.  (See the "Limitations and caveats" section of the named
address spaces page linked above.)

> Of course it would be great if there was a target independent way of
> doing these kind of things...

Yes, we could have something like an "constant" attribute that you could
put on a declaration to say it never gets changed.  You would still need
to be able to cast away that attribute in order to set the initial
value, but the "constant" attribute would be a promise to the compiler
that neither "you" nor "anyone else" would change it after it is first read.

If this is something that would really be useful, then if we can
formulate a clear specification, it could be filed as a gcc feature request.

> 
> Cheers,
> Oleg
> 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]