Joe Groff
2012-Jan-18 23:50 UTC
[LLVMdev] Best way to interface with MSVC _ftol2 runtime function for fptoui?
Hi everyone. On i386--win32 targets, LLVM tries to use the MSVCRT routine _ftol2 for floating-point to unsigned conversions, but this function has a nonstandard calling convention LLVM doesn't understand. It takes its input operand on the x87 stack as ST0, which it pops off of the stack before returning. The return value is given in EDX:EAX. In effect, I need to call it like this: %1 = call i64 asm "call __ftol2", "=A,{st},~{dirflag},~{fpsr},~{flags}" (double %x) nounwind but with the added consideration that the input operand is popped by the call, so the callee can't emit its own fstp instruction afterward. LLVM inline asm doesn't appear to be capable of communicating this. In #llvm it was suggested to write a custom instruction for the call, but it looks like there are a few layers of abstraction in the X86 target for dealing with x87, and I can't quite grasp exactly how instruction stack effects are communicated. What would be the best approach to implement proper support for this runtime call? -Joe
Jakob Stoklund Olesen
2012-Jan-19 02:04 UTC
[LLVMdev] Best way to interface with MSVC _ftol2 runtime function for fptoui?
On Jan 18, 2012, at 3:50 PM, Joe Groff wrote:> Hi everyone. On i386--win32 targets, LLVM tries to use the MSVCRT > routine _ftol2 for floating-point to unsigned conversions, but this > function has a nonstandard calling convention LLVM doesn't understand. > It takes its input operand on the x87 stack as ST0, which it pops off > of the stack before returning. The return value is given in EDX:EAX. > In effect, I need to call it like this: > > %1 = call i64 asm "call __ftol2", > "=A,{st},~{dirflag},~{fpsr},~{flags}" (double %x) nounwind > > but with the added consideration that the input operand is popped by > the call, so the callee can't emit its own fstp instruction afterward. > LLVM inline asm doesn't appear to be capable of communicating this.This should work: %1 = call i64 asm "call __ftol2", "=A,{st},~{dirflag},~{fpsr},~{flags},~{st}" (double %x) nounwind See http://llvm.org/viewvc/llvm-project/llvm-gcc-4.2/trunk/gcc/reg-stack.c?view=markup And the INLINEASM handling in http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86FloatingPoint.cpp?view=markup> In > #llvm it was suggested to write a custom instruction for the call, but > it looks like there are a few layers of abstraction in the X86 target > for dealing with x87, and I can't quite grasp exactly how instruction > stack effects are communicated. What would be the best approach to > implement proper support for this runtime call?If the inline asm works for you, use it. It is currently the only way of supporting caller-popped arguments. Normal instructions can't do it because it depends on treating the inline asm clobber list differently from otherwise clobbered registers. /jakob
Joe Groff
2012-Jan-19 02:37 UTC
[LLVMdev] Best way to interface with MSVC _ftol2 runtime function for fptoui?
2012/1/18 Jakob Stoklund Olesen <stoklund at 2pi.dk>:> This should work: > %1 = call i64 asm "call __ftol2", "=A,{st},~{dirflag},~{fpsr},~{flags},~{st}" (double %x) nounwindThanks Jakob, the ~{st} constraint does the trick. It wasn't clear to me that "clobbers" means "pops" for x87 registers. -Joe
Apparently Analagous Threads
- [LLVMdev] Best way to interface with MSVC _ftol2 runtime function for fptoui?
- [LLVMdev] Best way to interface with MSVC _ftol2 runtime function for fptoui?
- [LLVMdev] Best way to interface with MSVC _ftol2 runtime function for fptoui?
- [LLVMdev] Best way to interface with MSVC _ftol2 runtime function for fptoui?
- [LLVMdev] Best way to interface with MSVC _ftol2 runtime function for fptoui?