GCC Inline Assembler "memory" Clobber don't prevent from re-arrange the code in ARM .

stephen lu lumotuwe@gmail.com
Fri Nov 3 03:11:00 GMT 2017


I read article about GCC Inline Assembler
(http://www.ethernut.de/en/documents/arm-inline-asm.html).

In this article,  "memory" Clobber forces the compiler to store all
cached values before and reload them after executing the assembler
instructions. And it must retain the sequence.

this is the example.
The following code intends to multiply c with b, of which one or both
may be modified by an interrupt routine. Disabling interrupts before
accessing the variables and re-enable them afterwards looks like a
good idea.


This may fail. Because the optimizer may decide to do the
multiplication first and then execute both inline assembler
instructions or vice versa.  :
asm volatile("mrs r12, cpsr\n\t"
    "orr r12, r12, #0xC0\n\t"
    "msr cpsr_c, r12\n\t" ::: "r12", "cc");
c *= b; /* This may fail. */
asm volatile("mrs r12, cpsr\n"
    "bic r12, r12, #0xC0\n"
    "msr cpsr_c, r12" ::: "r12", "cc");


This is safe by adding "memory" Clobber .
asm volatile("mrs r12, cpsr\n\t"
    "orr r12, r12, #0xC0\n\t"
    "msr cpsr_c, r12\n\t" :: : "r12", "cc", "memory");
c *= b; /* This is safe. */
asm volatile("mrs r12, cpsr\n"
    "bic r12, r12, #0xC0\n"
    "msr cpsr_c, r12" ::: "r12", "cc", "memory");


But I disassemble code by objdump -d .  "memory" Clobber  don't works,
the code is to do execute both inline assembler instructions, and then
do the multiplication.

mrs     ip, CPSR
orr     ip, ip, #192    ; 0xc0
msr     CPSR_c, ip
mrs     ip, CPSR
bic     ip, ip, #192    ; 0xc0
msr     CPSR_c, ip
mul     r0, r1, r0
mov     pc, lr


Can anyone help me?


Remarks:
 @ubuntu:~$ arm-linux-gcc -v
Using built-in specs.
Target: arm-unknown-linux-gnu
Configured with:
/home/lihua/toolchain/cross/bin/targets/src/gcc-4.3.4/configure
--build=i386-build_redhat-linux-gnu --host=i386-build_redhat-linux-gnu
--target=arm-unknown-linux-gnu
--prefix=/opt/compiler/glibc-oabi-toolchain-arm-generic
--with-sysroot=/opt/compiler/glibc-oabi-toolchain-arm-generic/arm-unknown-linux-gnu//sys-root
--enable-languages=c,c++,java,objc,obj-c++ --disable-multilib
--with-float=soft --with-pkgversion='<lihua_338@163.com>'
--enable-__cxa_atexit
--with-gmp=/opt/compiler/glibc-oabi-toolchain-arm-generic
--with-mpfr=/opt/compiler/glibc-oabi-toolchain-arm-generic
--with-ppl=/opt/compiler/glibc-oabi-toolchain-arm-generic
--with-cloog=/opt/compiler/glibc-oabi-toolchain-arm-generic
--with-mpc=/opt/compiler/glibc-oabi-toolchain-arm-generic
--with-local-prefix=/opt/compiler/glibc-oabi-toolchain-arm-generic/arm-unknown-linux-gnu//sys-root
--disable-nls --enable-threads=posix --enable-symvers=gnu --enable-c99
--enable-long-long --enable-target-optspace
Thread model: posix



More information about the Gcc-help mailing list