Attribute const and inline functions

Sebastian Huber sebastian.huber@embedded-brains.de
Sat Feb 22 18:28:00 GMT 2014


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;
}

static inline int i(void)
{
     return *get_ip();
}

static inline int i_inline(void)
{
     return *get_ip_inline();
}

void f(void);

void g(void)
{
     i();
     f();
     i();
}

void g_inline(void)
{
     i_inline();
     f();
     i_inline();
}

On SPARC this compiles to:

         .file   "test.c"
         .section        ".text"
         .align 4
         .global g
         .type   g, #function
         .proc   020
g:
         save    %sp, -96, %sp
         call    get_ip, 0
          nop
         ld      [%o0], %g1
         call    f, 0
          mov    %o0, %i5
         ld      [%i5], %g1
         jmp     %i7+8
          restore
         .size   g, .-g
         .align 4
         .global g_inline
         .type   g_inline, #function
         .proc   020
g_inline:
         save    %sp, -96, %sp
         sethi   %hi(ip), %i5
         ld      [%i5+%lo(ip)], %g1
         ld      [%g1], %g1
         call    f, 0
          nop
         ld      [%i5+%lo(ip)], %g1
         ld      [%g1], %g1
         jmp     %i7+8
          restore
         .size   g_inline, .-g_inline
         .ident  "GCC: (GNU) 4.9.0 20140103 (experimental)"

In the g_inline variant we have a second "ld      [%i5+%lo(ip)], %g1", 
which is what I want to avoid.  Is there some way to tell GCC that I 
would like to do dangerous things?

-- 
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