[Haskell-cafe] Re: Could FFI support pass-by-value of structs?

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Mon Jun 29 16:03:15 EDT 2009


On Wed, 2009-06-24 at 18:48 -0700, John Meacham wrote:
> On Wed, Jun 24, 2009 at 10:23:29AM -0300, Maurí­cio wrote:
> > However, isn't just knowing the size and alignment enough to
> > write a generic struct handler that, by using the appropriate
> > calling convention, is going to work with any struct? If not,
> > I agree with you it's really not worth it (as we can use pointers
> > as Felipe sugested.
> 
> No, unfortunately it is not. Depending on the actual data types inside
> the struct, values may be passed in registers, the stack, or split
> between the two. for instance, the floating point values in a given
> struct may be passed in SSE registers on x86-64, while the integral
> parts are passed in normal registers. 

A better approach might be to not use a custom user-defined type with
its Storable instance and just use a tuple of FFI-types.

Using Maurí­cio's example:

/*****/
struct ex {
     int x;
     int y;
     int z;
};

ex example_functions (ex p)
{
   (...)
}
/*****/

foreign import ccall "example_functions" exampleFunction
   :: (CInt, CInt, CInt) -> IO (CInt, CInt, CInt)


So no generic user mechanism, but extend the FFI-marshalable types to
include tuples of FFI-types and to pass them using the C struct ABI.
I've no idea what happens when using C pack pragmas etc.

Duncan



More information about the Haskell-Cafe mailing list