ARM Cortex M0+ __set_MSP C++17 编译器警告

ARM Cortex M0+ __set_MSP C++17 compiler warning

我确实有一个从引导加载程序跳转到应用程序的工作代码。 但是现在从 C++17 开始,设置主堆栈指针的实现 __set_MSP 会引发编译器警告。

代码:

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

警告:

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

我的问题是,如果省去只是省略 clobbing 条目:

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

代码仍然有效,进入申请。 但我想确定这不是偶然的。

非常感谢!

clobber 通知编译器一些寄存器的值被汇编语句改变了,除了输入和输出寄存器,因此编译器可以创建指令来保存和恢复这些寄存器的值,通过压入和恢复这些寄存器的值。从堆栈中弹出。

重要的是,GCC 期望 SP 通过内联汇编语句保持不变。如此处 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.

这里

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

为了回答你的问题,

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

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

是的,因为这里说即使包含 SP 在逻辑上是正确的,但它没有任何用处(编译器即使接受它也不会使用此信息)所以它可以安全地从列表中删除。