[Haskell-cafe] Anonymous FFI calls

Francesco Mazzoli f at mazzo.li
Wed Feb 11 20:01:41 UTC 2015


On 11 February 2015 at 18:26, Sylvain Henry <hsyl20 at gmail.com> wrote:
> You can use FunPtr and wrapper imports to convert a FunPtr into a Haskell
> function.
> https://hackage.haskell.org/package/base-4.7.0.2/docs/Foreign-Ptr.html#g:2

The problem with that scheme is that you have to define a FFI import
per type.  TH solutions are not viable (and cumbersome) for our use
case -- the biggest problem being related to the TH staging
restriction.

> Code using http://hackage.haskell.org/package/libffi looks pretty
> similar, though the specification needs terms (such as argCInt)
> instead of a mandatory type signature. GHC itself includes libffi so
> maybe the foreign import "dynamic" calls end up doing exactly the same
> thing as the hackage libffi does.

Thanks!  That seems like a good workaround for the time being -- we
had actually considered for a split second re-implementing the C
calling convention, I should have suspected that such a library
existed already.

However, while that library works for pointers, it does not (and
can't) have the facilities to refer to symbols.

In any case, there still is the need for such a facility built-in GHC,
since such solutions are always going to incur in some overhead,
compared to calling C functions directly.  For example in the linked
bindings to `libffi' the arguments are passed as a list, which is
certainly a big difference from storing them into registers.

And again, it seems like 95% of the work is already done, since
`foreign import's are already desugared to Haskell functions with a
primitive call to the C function.

Francesco


More information about the ghc-devs mailing list