Reputation: 59
I was now using a stackful co-routines for network programming. But I was punished by the invalidation of return stack buffer (see http://www.agner.org/optimize/microarchitecture.pdf p.36), during the context switch (because we manually change the SP register)
I found out that the jmp
instruction is better than ret
after assembly language test. However, I have some more functions that indirectly call the context switch function that was written in C++ language (compiled by GCC). How can we force these function return using jmp
instead of ret
in the GCC assembly result?
Some common but not perfect methods:
__builtin_frame_address+2*sizeof(void*)
and jmp
to the return address, before ret
?This is an unsafe solution. In C++, local variables or right values are destructed before ret
instruction. We will omit these instruction if we jmp
. What's worse, even if we are in C, callee-saved registers need to be restored before ret
instruction and we will also omit these instruction, too.
So what can we do to force GCC use jmp instead of ret to avoid the problems listing above?
Upvotes: 3
Views: 1405
Reputation: 3675
Use an assembler macro:
.macro ret
pop %ecx
jmp *%ecx
.endm
Put that in inline assembler at the top of the file or elsewhere.
Upvotes: 3