Inline assembly without inputs considered const/pure?

Matthias Pfaller leo@marco.de
Tue Feb 23 15:04:00 GMT 2016


On 02/23/2016 09:20 AM, Matthias Pfaller wrote:
> Hi,
> 
> I'm using __asm on a cortex-m4 to enable/disable/restore interrupts.
> 
> I have defined a couple of macros to manipulate registers only available
> by using the msr/mrs opcodes:
> 
>> #define mrs(x)  ({                                                          \
>>         volatile unsigned int __r = 0;                                      \
>>         __asm__ __volatile__("" : : : "memory");                            \
>>         __asm__("mrs %[R]," __STRING(x) : [R] "=r" (__r) : : "cc", "memory");\
>>         __r;                                                                \
>>     })

After some tries I found an actually quite simple solution for my
problem. The compiler deletes the mrs because it has no input. So we
have to provide input that is constant, but different on each invocation
of the macro. Using the "J" constraints I can supply an integer constant
between -4095 and 4095 without forcing the compiler to produce any load
requests. __LINE__ is a source for unique integers:

> #define mrs(x)  ({                                                          \
>         unsigned int __r;                                                   \
>         __asm__ __volatile__("" : : : "memory");                            \
>         __asm__("mrs %[R]," __STRING(x) "/* %[D] avoid constant folding */" \
>             : [R] "=r" (__r)                                                \
>             : [D] "J" ((__LINE__ & 0x1fff) - 0xfff)                         \
>             : "cc", "memory"                                                \
>         );                                                                  \   
>         __r;                                                                \   
>     })

Matthias
-- 
Matthias Pfaller                          Software Entwicklung
marco Systemanalyse und Entwicklung GmbH  Tel   +49 8131 5161 41
Hans-Böckler-Str. 2, D 85221 Dachau       Fax   +49 8131 5161 66
http://www.marco.de/                      Email leo@marco.de
Geschäftsführer Martin Reuter             HRB 171775 Amtsgericht München



More information about the Gcc-help mailing list