[LLVMdev] Best way to interface with MSVC _ftol2 runtime function for fptoui?

Joe Groff arcata at gmail.com
Wed Jan 18 15:50:15 PST 2012


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



More information about the llvm-dev mailing list