Inline assembler: register clobber

Asked by marco

Hi, first of all: sorry for my bad english.

I have a arm1136 based SOC, and I'm playing with the inline assembler.
Please, look at the following code:

void function() {
  asm volatile("mov r8, r0\n"
               : : : "r8");
}

Due to ARM calling convention r8 must be saved before the asm statment and after it must be restored, right?
As expected, if I "objdump" this, r8 is saved/restored:

00000000 <function>:
   0: e92d4100 push {r8, lr}
   4: e1a08000 mov r8, r0
   8: e8bd8100 pop {r8, pc}

But if I compile this code in thumb mode (-mthumb), r8 doesn't get saved/restored:

00000000 <function>:
   0: 4680 mov r8, r0
   2: 4770 bx lr

I can't undestand this, r8 should be saved/restored in thumb mode too, where am I wrong?

This is the output of "arm-none-eabi-gcc -v":
Using built-in specs.
COLLECT_GCC=arm-none-eabi-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-none-eabi/4.8/lto-wrapper
Target: arm-none-eabi
Configured with: ../gcc-4.8.3/configure --build=x86_64-linux-gnu --prefix=/usr --includedir='/usr/lib/include' --mandir='/usr/lib/share/man' --infodir='/usr/lib/share/info' --sysconfdir=/etc --localstatedir=/var --disable-silent-rules --libexecdir='/usr/lib/lib/gcc-arm-none-eabi' --disable-maintainer-mode --disable-dependency-tracking --enable-languages=c,c++ --prefix=/usr/lib --infodir=/usr/share/doc/gcc-arm-none-eabi/info --mandir=/usr/share/man --htmldir=/usr/share/doc/gcc-arm-none-eabi/html --pdfdir=/usr/share/doc/gcc-arm-none-eabi/pdf --bindir=/usr/bin --libexecdir=/usr/lib --libdir=/usr/lib --with-system-zlib --enable-multilib --disable-decimal-float --disable-libffi --disable-libgomp --disable-libmudflap --disable-libquadmath --disable-libssp --disable-libstdcxx-pch --disable-libstdc++-v3 --disable-nls --disable-shared --disable-threads --disable-tls --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=arm-none-eabi --with-gnu-as --with-gnu-ld --with-headers=no --without-newlib --with-pkgversion=4.8.3-9+11 --without-included-gettext --with-multilib-list=armv6-m,armv7-m,armv7e-m,armv7-r INHIBIT_LIBC_CFLAGS=-DUSE_TM_CLONE_REGISTRY=0 AR_FOR_TARGET=arm-none-eabi-ar AS_FOR_TARGET=arm-none-eabi-as LD_FOR_TARGET=arm-none-eabi-ld NM_FOR_TARGET=arm-none-eabi-nm OBJDUMP_FOR_TARGET=arm-none-eabi-objdump RANLIB_FOR_TARGET=arm-none-eabi-ranlib READELF_FOR_TARGET=arm-none-eabi-readelf STRIP_FOR_TARGET=arm-none-eabi-strip
Thread model: single
gcc version 4.8.3 20140820 (release) (4.8.3-9+11)

I'm in debian testing (jessie)

Thanks in advance for your help.

Question information

Language:
English Edit question
Status:
Solved
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Solved by:
Thomas Preud'homme
Solved:
Last query:
Last reply:
Revision history for this message
Terry Guo (terry.guo) said :
#1

Looks to me you are using different toolchain from the one in this webiste. I just tried toolchain in this website and everything looks fine to me. For thumb mode, the r8 is saved and restored.

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#2

Hi Marco,

I checked the toolchain in Jessie (which is our 2014Q2 update, you can get the 2014Q3 update via experimental) and like Terry I find that r8 is correctly saved when compiling with -mthumb:

push {r7, lr}
mov r7, r8
push {r7}

Revision history for this message
marco (marcomilanese7) said :
#3

Hi,

Ok, I tried toolchain in the website:

Command line: ./arm-none-eabi-gcc -mcpu=arm1136jf-s test.c -c -Os
00000000 <function>:
   0: e92d4100 push {r8, lr}
   4: e1a08000 mov r8, r0
   8: e8bd8100 pop {r8, pc}

OK, r8 is saved and restored

Command line: ./arm-none-eabi-gcc -mcpu=arm1136jf-s test.c -c -mthumb -Os
00000000 <function>:
   0: 4680 mov r8, r0
   2: 4770 bx lr

r8 isn't saved/restored

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#4

-Os with -mthumb is what causes this bug. I haven't looked at the code yet but I'm fairly confident that this is related to https://bugs.launchpad.net/gcc-arm-embedded/+bug/1313130

I'll need to rework my patch to fix that.

Revision history for this message
marco (marcomilanese7) said :
#5

Ok, thank you.
Do you know if this is a arm-none-eabi specific issue? I'm getting the same problem with the android (cyangenmod 11) toolchain

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#6

If this is the same issue it's specific to Thumb-1, -Os and the ARM backend so it would hit arm-none-linux-gnu as well.

Revision history for this message
marco (marcomilanese7) said :
#7

Ok, do you me to create a bug report?

Revision history for this message
Best Thomas Preud'homme (thomas-preudhomme) said :
#8

I just gave a link to this question in the bug report I mentionned above, that ought to be enough. Thanks.

Revision history for this message
marco (marcomilanese7) said :
#9

Thanks Thomas Preud'homme, that solved my question.