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>, GCC Help <gcc-help at gcc dot gnu dot org>
- Date: Mon, 24 Feb 2014 10:57:35 +0100
- Subject: Re: Attribute const and inline functions
- Authentication-results: sourceware.org; auth=none
- References: <5308EC44 dot 4010708 at embedded-brains dot de>
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.