Gcc 如何抑制;警告:控件达到非无效函数的末尾“;
我使用gcc交叉编译器翻译了一些PowerPC汇编代码,该编译器具有以下功能:Gcc 如何抑制;警告:控件达到非无效函数的末尾“;,gcc,warnings,powerpc,eabi,Gcc,Warnings,Powerpc,Eabi,我使用gcc交叉编译器翻译了一些PowerPC汇编代码,该编译器具有以下功能: uint32_t fill_cache(void) { __asm__ ("addi 3, 0, 0\n"); /* R3 = 0 */ /* More asm here modifying R3 and filling the cache lines. */ } 在PowerPC EABI下,返回以R3计算的值。当我得到 foo.c:105: warning: control reaches e
uint32_t fill_cache(void)
{
__asm__ ("addi 3, 0, 0\n"); /* R3 = 0 */
/* More asm here modifying R3 and filling the cache lines. */
}
在PowerPC EABI下,返回以R3计算的值。当我得到
foo.c:105: warning: control reaches end of non-void function
有没有一种方法教gcc实际返回值?或者有没有方法抑制警告(不删除-Wall或添加-Wno-*)?我希望仅针对此功能有选择地抑制此警告,以便使一般警告级别尽可能高
由于调用者需要计算的值,因此不能使此函数返回void。解决方案1:使用此选项,您可以在本地抑制某些诊断检查。在非void函数中抱怨不返回的特定选项(也由-Wall暗示)是-Wreturn类型。因此,抑制警告的特定代码为:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-type"
/* Your code here */
#pragma GCC diagnostic pop
通过使用-fddiagnostics show选项进行编译,可以找出导致警告的选项。它只会将该选项附加到警告消息中
解决方案2:定义一个寄存器并将其放入所需寄存器中。请参考中的变量,以及生成的代码:
uint32_t fill_cache(void)
{
register uint32_t cacheVal __asm__ ("r3");
__asm__ __volatile__ ("addi %0, 0, 0" : "=r" (cacheVal));
/* More code here */
return cacheVal;
}
volatile修饰符用于确保优化策略不会删除指令或以其他方式影响指令
出于至少两个原因,首选解决方案2:
函数可以声明为裸,在这种情况下,编译器不会生成prolog和epilog,并假定程序员保留所有必要的寄存器,并在返回前将输出值放入正确的寄存器
uint32_t fill_cache(void) __attribute__((naked)); // Declaration
// attribute should be specified in declaration not in implementation
uint32_t fill_cache(void)
{
__asm__ ("addi 3, 0, 0\n"); /* R3 = 0 */
/* More asm here modifying R3 and filling the cache lines. */
}
有点晚了,但也许有人也会插手:)
PS:就我所知,\uu asm\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
以及\uuuuuuuuuuuuuuuuuuuuuuuuuuu。实际上,GNU GCC中的\uu asm\uu
和asm
之间没有区别。但现代方法是无下划线的:asm volatile