ARM Cortex M0+ __set_MSP C++17 compiler warning

Viewed 1002

I do have a working code for jumping from bootloader to application. But now since C++17 the implementation for setting the Master Stack Pointer __set_MSP raises a compiler warning.

Code:

__attribute__( ( always_inline ) ) __STATIC_INLINE void __set_MSP(uint32_t topOfMainStack)
{
  __ASM volatile ("MSR msp, %0\n" : : "r" (topOfMainStack) : "sp");
}

Warning:

warning: listing the stack pointer register 'sp' in a clobber list is deprecated [-Wdeprecated]

My Question is, if it's save to just omit the clobbing entry:

__ASM volatile ("MSR msp, %0\n" : : "r" (topOfMainStack));

The code still works and the application is entered. But I want to be sure that this is not just working by chance.

Thanks a lot!

1 Answers

The clobber informs the compiler that the values of some registers are changed by the assembly statement, in addition to the input and output registers, so the compiler can create instructions to save and recover the values of those registers by pushing and popping from the stack.

Importantly GCC expects the SP to remain unchanged by an inline assembly statement. As in here https://github.com/checkpoint-restore/criu/issues/705

Another restriction is that the clobber list should not contain the stack pointer register. This is because the compiler requires the value of the stack pointer to be the same after an asm statement as it was on entry to the statement. However, previous versions of GCC did not enforce this rule and allowed the stack pointer to appear in the list, with unclear semantics. This behavior is deprecated and listing the stack pointer may become an error in future versions of GCC.

and here

https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Clobbers-and-Scratch-Registers

To answer your question,

My Question is, if it's save to just omit the clobbing entry:

https://github.com/checkpoint-restore/criu/commit/901f5d48ec6ef485ea889a4a97011a12f4c3808e#

Yes, because here is says even though including the SP is logically correct, it serves no purpose (the compiler does not use this information even if it accepts it) so it can safely dropped from the list.

Related