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 22/02/14 19:28, Sebastian Huber wrote:
> Hello,
> 
> first some background information.  I would like to read from a memory
> mapped register (some sort of hardware counter).  The problem is that
> the address of this register is unknown at compile and link time.  So
> some low-level initialization code determines the register address and
> later its value never changes.
> 
> I hoped that the const function attribute is of some help here:
> 
> http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#Function-Attributes
> 
> 
> Here it explicitly says "function is not allowed to read global
> memory".  Unfortunately the compiler takes this serious.
> 
> I have the following test code:
> 
> extern volatile const int *ip;
> 
> __attribute__((const)) volatile int *get_ip(void);
> 
> /* This function reads from global memory and is declared as const. GCC
> ignores the const attribute in this case. */
> __attribute__((const)) static inline volatile int *get_ip_inline(void)
> {
>     return ip;
> }

Try the "pure" attribute rather than the "const" one.  "pure" functions
are allowed to read global memory, but not write it (or cause other
side-effects), and the compiler can safely call them fewer times than
the program says.

Also, you don't say anything about optimisation options - "pure" and
"const" attributes only help if optimisation is enabled.


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