How do I tell GCC that a global variable is immutable after initialization?

Sebastian Huber sebastian.huber@embedded-brains.de
Wed Jun 22 12:16:00 GMT 2016


I solved the problem using something like this

extern const struct s s;

extern struct s s_mutable;

Define s and s_mutable via a basic asm statement in the .data section.

For example:

https://git.rtems.org/rtems/tree/cpukit/score/cpu/sparc/rtems/score/cpu.h#n1327

https://git.rtems.org/rtems/tree/cpukit/score/cpu/sparc/rtems/score/sparcimpl.h#n71

On 22/06/16 09:00, Sebastian Huber wrote:
> Hello,
>
> is there a way to tell GCC that a global variable is immutable after 
> initialization? For example
>
> struct {
>     int (*f)(void);
> } s;
>
> int f(void)
> {
>     int a;
>     int b;
>
>     a = (*s.f)();
>     b = (*s.f)();
>
>     return a + b;
> }
>
> yields on ARMv8 for example
>
> f:
>         push    {r4, r5, r6, lr}
>         movw    r4, #:lower16:s
>         movt    r4, #:upper16:s
>         ldr     r3, [r4]
>         blx     r3
>         ldr     r3, [r4] <- I would like to get rid of this load here
>         mov     r5, r0
>         blx     r3
>         add     r0, r5, r0
>         pop     {r4, r5, r6, pc}
>
> The
>
> a = (*s.f)();
>
> is a call to a global function, so GCC must assume that s might have 
> changed afterwards. I would like to get rid of the second load of s.f. 
> Is there a special attribute to tell GCC that s is essentially 
> immutable? I cannot use the const qualifier, since the structure is 
> initialized during system start.
>

-- 
Sebastian Huber, embedded brains GmbH

Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone   : +49 89 189 47 41-16
Fax     : +49 89 189 47 41-09
E-Mail  : sebastian.huber@embedded-brains.de
PGP     : Public key available on request.

Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.



More information about the Gcc-help mailing list