Is there a safe way in g++ to force a variable to be in a certain register when a function is called? This function contains inline-asm-code that assumes input开发者_StackOverflow中文版s in certain registers.
I tried to declare local variables to be in fixed registers (register int x asm ("$10")
) and pass them to the function, but -O3
messes it up.
I don't want to reserve registers for the whole program by declaring global variables in registers.
I you want the parameters of a function to be passed in registers, you can do something like this:
int __attribute__((fastcall)) foo(register int a, register int b)
{
return a + b;
}
__attribute__((fastcall))
means that the first two parameters of the function are passed in ECX and EDX respectively.- The
register
keyword is used to prevent GCC from copying the parameters to the stack once the function is entered.
I found this to work reliably across different -O
levels.
Use asm volatile
inline assembly blocks, like explained in this page.
You can use Extended Assembly. This is for gcc, it should work: http://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html You can use input register that will be filled by the variable you want. Or you may refer to the C++ variable directly by its name in the inline asm code.
Pass variables declared with explicit registers directly to the inline asm statement; the register must be specified in the function containing the asm statement.
精彩评论