X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fdocs%2Fusers_guide%2Fffi-chap.sgml;h=4534b07000a0e1d7f8b1ef3421559101ad35cf75;hb=7bcdfdba58be3bd3159a87ca8413c28e3c64d147;hp=ce24313a847caf7059009e83dfbf8d4f0efc0fb5;hpb=ec655d31e7a73d0e2b9eb160faa7ebd7c9fe3577;p=ghc-hetmet.git diff --git a/ghc/docs/users_guide/ffi-chap.sgml b/ghc/docs/users_guide/ffi-chap.sgml index ce24313..4534b07 100644 --- a/ghc/docs/users_guide/ffi-chap.sgml +++ b/ghc/docs/users_guide/ffi-chap.sgml @@ -1,40 +1,78 @@ -Foreign function interface +Foreign function interface (FFI) - The foreign function interface consists of the following - components: + GHC (mostly) conforms to the Haskell 98 Foreign Function Interface + Addendum 1.0, whose definition is available from http://haskell.org/. + The FFI support in GHC diverges from the Addendum in the following ways: + - The Foreign Function Interface language specification - (which constitutes most of this Chapter, beginning with ). You must use the - command-line option to make GHC - understand the foreign declarations defined - by the FFI. + The routines hs_init(), + hs_exit(), and hs_set_argv() from + Chapter 6.1 of the Addendum are not supported yet. - Several library modules which provide access to types used - by foreign languages and utilties for marshalling values to and - from foreign functions, and for converting errors in the foreign - language into Haskell IO errors. See for - more details. + Syntactic forms and library functions proposed in earlier versions + of the FFI are still supported for backwards compatibility. + + + + GHC implements a number of GHC-specific extensions to the FFI + Addendum. These extensions are described in , but please note that programs using + these features are not portable. Hence, these features should be + avoided where possible. -&ffi-body; + The FFI libraries are documented in the accompanying library + documentation; see for example the Foreign + module. + + + GHC extensions to the FFI Addendum + + The FFI features that are described in this section are specific to + GHC. Avoid them where possible to not compromise the portability of the + resulting code. + + + Arrays + + The types ByteArray and + MutableByteArray may be used as basic foreign types + (see FFI Addendum, Section 3.2). In C land, they map to + (char *). + + + + Unboxed types + + The following unboxed types may be used as basic foreign types + (see FFI Addendum, Section 3.2): Int#, + Word#, Char#, + Float#, Double#, + Addr#, StablePtr# a, + MutableByteArray#, ForeignObj#, + and ByteArray#. + + + - + Using the FFI with GHC The following sections also give some hints and tips on the use of the foreign function interface in GHC. - Using <literal>foreign export</literal> with GHC + Using <literal>foreign export</literal> and <literal>foreign + import ccall "wrapper"</literal> with GHC foreign export with GHC @@ -42,7 +80,7 @@ When GHC compiles a module (say M.hs) which uses foreign export or foreign - export dynamic, it generates two + import "wrapper", it generates two additional files, M_stub.c and M_stub.h. GHC will automatically compile M_stub.c to generate @@ -57,7 +95,7 @@ module Foo where -foreign export foo :: Int -> IO Int +foreign export ccall foo :: Int -> IO Int foo :: Int -> IO Int foo n = return (length (f n)) @@ -162,24 +200,23 @@ int main(int argc, char *argv[]) - Using <literal>foreign export dynamic</literal> with + <title>Using <literal>foreign import ccall "wrapper"</literal> with GHC - foreign export - dynamicwith GHC + foreign import + ccall "wrapper"with GHC - When foreign export dynamic is used - in a Haskell module, The C stub file - M_stub.c generated by GHC contains small - helper functions used by the code generated for the - foreign export dynamic, so it must be - linked in to the final program. When linking the program, - remember to include M_stub.o in the final - link command line, or you'll get link errors for the missing - function(s) (this isn't necessary when building your program - with ghc ––make, as GHC will automatically - link in the correct bits). + When foreign import ccall "wrapper" is used + in a Haskell module, The C stub file M_stub.c + generated by GHC contains small helper functions used by the code + generated for the imported wrapper, so it must be linked in to the + final program. When linking the program, remember to include + M_stub.o in the final link command line, or + you'll get link errors for the missing function(s) (this isn't + necessary when building your program with ghc + ––make, as GHC will automatically link in the + correct bits). @@ -209,8 +246,8 @@ HsInt lookupEFS (HsForeignObj a, HsInt i); The types HsInt, - HsForeignObj etc. are described in . + HsForeignObj etc. are described in the H98 FFI + Addendum. Note that this approach is only essential for returning