调用函数时,g ++中是否有一种安全的方法可以强制变量位于某个寄存器中?此函数包含假定某些寄存器中的输入的 inline-asm-code。
我试图将局部变量声明在固定寄存器 ( register int x asm ("$10")
) 中并将它们传递给函数,但-O3
搞砸了。
我不想通过在寄存器中声明全局变量来为整个程序保留寄存器。
调用函数时,g ++中是否有一种安全的方法可以强制变量位于某个寄存器中?此函数包含假定某些寄存器中的输入的 inline-asm-code。
我试图将局部变量声明在固定寄存器 ( register int x asm ("$10")
) 中并将它们传递给函数,但-O3
搞砸了。
我不想通过在寄存器中声明全局变量来为整个程序保留寄存器。
我希望函数的参数在寄存器中传递,您可以执行以下操作:
int __attribute__((fastcall)) foo(register int a, register int b)
{
return a + b;
}
__attribute__((fastcall))
表示函数的前两个参数分别传入ECX和EDX。register
关键字用于防止 GCC 在进入函数后将参数复制到堆栈中。我发现这可以在不同-O
级别上可靠地工作。
使用asm volatile
内联汇编块,如本页所述。
Pass variables declared with explicit registers directly to the inline asm statement; the register must be specified in the function containing the asm statement.
您可以使用扩展程序集。这适用于 gcc,它应该可以工作: http: //gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html 您可以使用将由您想要的变量填充的输入寄存器。或者您可以在内联汇编代码中直接通过其名称来引用 C++ 变量。