On 17/11/11 19:40, David Matthews wrote:
On 16/11/2011 20:27, Phil Clayton wrote:
On 16/11/11 18:27, David Matthews wrote:
On 16/11/2011 18:07, Phil Clayton wrote: Right. Those preprocessor symbols are wrong. I think I changed them elsewhere in the code and somehow those got missed. I think the issue is that on X86/32 all arguments are passed on the stack but on the X86/64 integer-like arguments are passed in the general registers but floating point values are passed in floating-point registers. The code in apply_rec casts everything to (void*) which means that the foreign function will always be called with the arguments in the general registers even if it is expecting them in FP registers. This is frankly horrible and really this should be changed. Instead of interpreting the type information when the foreign function is called it should compile a stub which will convert the arguments into the correct format. I did find a project that would do something like that but it didn't deal with call-backs at all.
Thanks - knowing that everything is cast to (void *) makes the issue much clearer.
Well, having decided that the existing code is horrible I bit the bullet and investigated libffi more closely. It turns out that it does everything we need including callbacks. I've now modified the foreign-function interface so that it will use libffi if it is available. As with GMP there is a configure option to control whether it is used and the default is to use it if it is there. It may need the development version of the libffi package with all the headers in order for configure to detect it.
Fantastic news! It makes a huge difference to me so I am very grateful. libffi seems like a neat idea - I hadn't heard of it.
libffi-devel is required for the headers on Fedora. (The rpm was already present on my system due to the presence of some GHC devel rpm.)
With Fedora, there is a minor configuration issue: like many packages, libffi headers are not installed on a standard path, so relies on pkg-config to supply cflags/libs arguments. I was able to build by simply adding symbolic links in /usr/include. After some investigation, it seems a more general solution can be achieved with the PKG_CHECK_MODULES macro in configure but I don't know whether Fedora is just an unusual case regarding libffi setup.
So far I've tested callbacks on X86/32 and X86/64 and it all works. I haven't yet tested floating point on X86/64 but I'd expect it to work as well.
In my mini-suite of FFI tests, all now work without issue on x86_64. This includes doubles as arguments. Great to see structs can now be used too!
Phil