1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <!-- FFI docs as a chapter -->
6 Foreign function interface (FFI)
9 <para>GHC (mostly) conforms to the Haskell 98 Foreign Function Interface
10 Addendum 1.0, whose definition is available from <ulink url="http://www.haskell.org/"><literal>http://www.haskell.org/</literal></ulink>.</para>
12 <para>To enable FFI support in GHC, give the <option>-XForeignFunctionInterface</option><indexterm><primary><option>-XForeignFunctionInterface</option></primary>
13 </indexterm> flag.</para>
15 <para>GHC implements a number of GHC-specific extensions to the FFI
16 Addendum. These extensions are described in <xref linkend="ffi-ghcexts" />, but please note that programs using
17 these features are not portable. Hence, these features should be
18 avoided where possible.</para>
20 <para>The FFI libraries are documented in the accompanying library
21 documentation; see for example the
22 <ulink url="../libraries/base/Control-Concurrent.html"><literal>Foreign</literal></ulink> module.</para>
24 <sect1 id="ffi-ghcexts">
25 <title>GHC extensions to the FFI Addendum</title>
27 <para>The FFI features that are described in this section are specific to
28 GHC. Your code will not be portable to other compilers if you use them.</para>
31 <title>Unboxed types</title>
33 <para>The following unboxed types may be used as basic foreign types
34 (see FFI Addendum, Section 3.2): <literal>Int#</literal>,
35 <literal>Word#</literal>, <literal>Char#</literal>,
36 <literal>Float#</literal>, <literal>Double#</literal>,
37 <literal>Addr#</literal>, <literal>StablePtr# a</literal>,
38 <literal>MutableByteArray#</literal>, <literal>ForeignObj#</literal>,
39 and <literal>ByteArray#</literal>.</para>
42 <sect2 id="ffi-newtype-io">
43 <title>Newtype wrapping of the IO monad</title>
44 <para>The FFI spec requires the IO monad to appear in various places,
45 but it can sometimes be convenient to wrap the IO monad in a
46 <literal>newtype</literal>, thus:
48 newtype MyIO a = MIO (IO a)
50 (A reason for doing so might be to prevent the programmer from
51 calling arbitrary IO procedures in some part of the program.)
53 <para>The Haskell FFI already specifies that arguments and results of
54 foreign imports and exports will be automatically unwrapped if they are
55 newtypes (Section 3.2 of the FFI addendum). GHC extends the FFI by automatically unwrapping any newtypes that
56 wrap the IO monad itself.
57 More precisely, wherever the FFI specification requires an IO type, GHC will
58 accept any newtype-wrapping of an IO type. For example, these declarations are
61 foreign import foo :: Int -> MyIO Int
62 foreign import "dynamic" baz :: (Int -> MyIO Int) -> CInt -> MyIO Int
68 <title>Primitive imports</title>
70 GHC extends the FFI with an additional calling convention
71 <literal>prim</literal>, e.g.:
73 foreign import prim "foo" foo :: ByteArray# -> (# Int#, Int# #)
75 This is used to import functions written in Cmm code that follow an
76 internal GHC calling convention. This feature is not intended for
77 use outside of the core libraries that come with GHC. For more
78 details see the GHC developer wiki.
84 <title>Using the FFI with GHC</title>
86 <para>The following sections also give some hints and tips on the
87 use of the foreign function interface in GHC.</para>
89 <sect2 id="foreign-export-ghc">
90 <title>Using <literal>foreign export</literal> and <literal>foreign import ccall "wrapper"</literal> with GHC</title>
92 <indexterm><primary><literal>foreign export
93 </literal></primary><secondary>with GHC</secondary>
96 <para>When GHC compiles a module (say <filename>M.hs</filename>)
97 which uses <literal>foreign export</literal> or
98 <literal>foreign import "wrapper"</literal>, it generates two
99 additional files, <filename>M_stub.c</filename> and
100 <filename>M_stub.h</filename>. GHC will automatically compile
101 <filename>M_stub.c</filename> to generate
102 <filename>M_stub.o</filename> at the same time.</para>
104 <para>For a plain <literal>foreign export</literal>, the file
105 <filename>M_stub.h</filename> contains a C prototype for the
106 foreign exported function, and <filename>M_stub.c</filename>
107 contains its definition. For example, if we compile the
108 following module:</para>
113 foreign export ccall foo :: Int -> IO Int
116 foo n = return (length (f n))
120 f n = n:(f (n-1))</programlisting>
122 <para>Then <filename>Foo_stub.h</filename> will contain
123 something like this:</para>
127 extern HsInt foo(HsInt a0);</programlisting>
129 <para>and <filename>Foo_stub.c</filename> contains the
130 compiler-generated definition of <literal>foo()</literal>. To
131 invoke <literal>foo()</literal> from C, just <literal>#include
132 "Foo_stub.h"</literal> and call <literal>foo()</literal>.</para>
134 <para>The <filename>foo_stub.c</filename> and
135 <filename>foo_stub.h</filename> files can be redirected using the
136 <option>-stubdir</option> option; see <xref linkend="options-output"
139 <para>When linking the program, remember to include
140 <filename>M_stub.o</filename> in the final link command line, or
141 you'll get link errors for the missing function(s) (this isn't
142 necessary when building your program with <literal>ghc
143 ––make</literal>, as GHC will automatically link in the
144 correct bits).</para>
146 <sect3 id="using-own-main">
147 <title>Using your own <literal>main()</literal></title>
149 <para>Normally, GHC's runtime system provides a
150 <literal>main()</literal>, which arranges to invoke
151 <literal>Main.main</literal> in the Haskell program. However,
152 you might want to link some Haskell code into a program which
153 has a main function written in another language, say C. In
154 order to do this, you have to initialize the Haskell runtime
155 system explicitly.</para>
157 <para>Let's take the example from above, and invoke it from a
158 standalone C program. Here's the C code:</para>
161 #include <stdio.h>
164 #ifdef __GLASGOW_HASKELL__
165 #include "foo_stub.h"
168 #ifdef __GLASGOW_HASKELL__
169 extern void __stginit_Foo ( void );
172 int main(int argc, char *argv[])
176 hs_init(&argc, &argv);
177 #ifdef __GLASGOW_HASKELL__
178 hs_add_root(__stginit_Foo);
181 for (i = 0; i < 5; i++) {
182 printf("%d\n", foo(2500));
189 <para>We've surrounded the GHC-specific bits with
190 <literal>#ifdef __GLASGOW_HASKELL__</literal>; the rest of the
191 code should be portable across Haskell implementations that
192 support the FFI standard.</para>
194 <para>The call to <literal>hs_init()</literal>
195 initializes GHC's runtime system. Do NOT try to invoke any
196 Haskell functions before calling
197 <literal>hs_init()</literal>: bad things will
198 undoubtedly happen.</para>
200 <para>We pass references to <literal>argc</literal> and
201 <literal>argv</literal> to <literal>hs_init()</literal>
202 so that it can separate out any arguments for the RTS
203 (i.e. those arguments between
204 <literal>+RTS...-RTS</literal>).</para>
207 <function>hs_add_root</function><indexterm><primary><function>hs_add_root</function></primary>
208 </indexterm>, a GHC-specific interface which is required to
209 initialise the Haskell modules in the program. The argument
210 to <function>hs_add_root</function> should be the name of the
211 initialization function for the "root" module in your program
212 - in other words, the module which directly or indirectly
213 imports all the other Haskell modules in the program. In a
214 standalone Haskell program the root module is normally
215 <literal>Main</literal>, but when you are using Haskell code
216 from a library it may not be. If your program has multiple
217 root modules, then you can call
218 <function>hs_add_root</function> multiple times, one for each
219 root. The name of the initialization function for module
220 <replaceable>M</replaceable> is
221 <literal>__stginit_<replaceable>M</replaceable></literal>, and
222 it may be declared as an external function symbol as in the
223 code above. Note that the symbol name should be transformed
224 according to the Z-encoding:</para>
227 <tgroup cols="2" align="left" colsep="1" rowsep="1">
230 <entry>Character</entry>
231 <entry>Replacement</entry>
236 <entry><literal>.</literal></entry>
237 <entry><literal>zd</literal></entry>
240 <entry><literal>_</literal></entry>
241 <entry><literal>zu</literal></entry>
244 <entry><literal>`</literal></entry>
245 <entry><literal>zq</literal></entry>
248 <entry><literal>Z</literal></entry>
249 <entry><literal>ZZ</literal></entry>
252 <entry><literal>z</literal></entry>
253 <entry><literal>zz</literal></entry>
259 <para>After we've finished invoking our Haskell functions, we
260 can call <literal>hs_exit()</literal>, which terminates the
263 <para>There can be multiple calls to
264 <literal>hs_init()</literal>, but each one should be matched
265 by one (and only one) call to
266 <literal>hs_exit()</literal><footnote><para>The outermost
267 <literal>hs_exit()</literal> will actually de-initialise the
268 system. NOTE that currently GHC's runtime cannot reliably
269 re-initialise after this has happened,
270 see <xref linkend="ffi-divergence" />.</para>
273 <para>NOTE: when linking the final program, it is normally
274 easiest to do the link using GHC, although this isn't
275 essential. If you do use GHC, then don't forget the flag
276 <option>-no-hs-main</option><indexterm><primary><option>-no-hs-main</option></primary>
277 </indexterm>, otherwise GHC will try to link
278 to the <literal>Main</literal> Haskell module.</para>
281 <sect3 id="ffi-library">
282 <title>Making a Haskell library that can be called from foreign
285 <para>The scenario here is much like in <xref linkend="using-own-main"
286 />, except that the aim is not to link a complete program, but to
287 make a library from Haskell code that can be deployed in the same
288 way that you would deploy a library of C code.</para>
290 <para>The main requirement here is that the runtime needs to be
291 initialized before any Haskell code can be called, so your library
292 should provide initialisation and deinitialisation entry points,
293 implemented in C or C++. For example:</para>
296 HsBool mylib_init(void){
300 // Initialize Haskell runtime
301 hs_init(&argc, &argv);
303 // Tell Haskell about all root modules
304 hs_add_root(__stginit_Foo);
306 // do any other initialization here and
307 // return false if there was a problem
311 void mylib_end(void){
316 <para>The initialisation routine, <literal>mylib_init</literal>, calls
317 <literal>hs_init()</literal> and <literal>hs_add_root()</literal> as
318 normal to initialise the Haskell runtime, and the corresponding
319 deinitialisation function <literal>mylib_end()</literal> calls
320 <literal>hs_exit()</literal> to shut down the runtime.</para>
325 <sect2 id="glasgow-foreign-headers">
326 <title>Using header files</title>
328 <indexterm><primary>C calls, function headers</primary></indexterm>
330 <para>C functions are normally declared using prototypes in a C
331 header file. Earlier versions of GHC (6.8.3 and
332 earlier) <literal>#include</literal>d the header file in
333 the C source file generated from the Haskell code, and the C
334 compiler could therefore check that the C function being
335 called via the FFI was being called at the right type.</para>
337 <para>GHC no longer includes external header files when
338 compiling via C, so this checking is not performed. The
339 change was made for compatibility with the native code backend
340 (<literal>-fasm</literal>) and to comply strictly with the FFI
341 specification, which requires that FFI calls are not subject
342 to macro expansion and other CPP conversions that may be
343 applied when using C header files. This approach also
344 simplifies the inlining of foreign calls across module and
345 package boundaries: there's no need for the header file to be
346 available when compiling an inlined version of a foreign call,
347 so the compiler is free to inline foreign calls in any
350 <para>The <literal>-#include</literal> option is now
351 deprecated, and the <literal>include-files</literal> field
352 in a Cabal package specification is ignored.</para>
357 <title>Memory Allocation</title>
359 <para>The FFI libraries provide several ways to allocate memory
360 for use with the FFI, and it isn't always clear which way is the
361 best. This decision may be affected by how efficient a
362 particular kind of allocation is on a given compiler/platform,
363 so this section aims to shed some light on how the different
364 kinds of allocation perform with GHC.</para>
368 <term><literal>alloca</literal> and friends</term>
370 <para>Useful for short-term allocation when the allocation
371 is intended to scope over a given <literal>IO</literal>
372 computation. This kind of allocation is commonly used
373 when marshalling data to and from FFI functions.</para>
375 <para>In GHC, <literal>alloca</literal> is implemented
376 using <literal>MutableByteArray#</literal>, so allocation
377 and deallocation are fast: much faster than C's
378 <literal>malloc/free</literal>, but not quite as fast as
379 stack allocation in C. Use <literal>alloca</literal>
380 whenever you can.</para>
385 <term><literal>mallocForeignPtr</literal></term>
387 <para>Useful for longer-term allocation which requires
388 garbage collection. If you intend to store the pointer to
389 the memory in a foreign data structure, then
390 <literal>mallocForeignPtr</literal> is
391 <emphasis>not</emphasis> a good choice, however.</para>
393 <para>In GHC, <literal>mallocForeignPtr</literal> is also
394 implemented using <literal>MutableByteArray#</literal>.
395 Although the memory is pointed to by a
396 <literal>ForeignPtr</literal>, there are no actual
397 finalizers involved (unless you add one with
398 <literal>addForeignPtrFinalizer</literal>), and the
399 deallocation is done using GC, so
400 <literal>mallocForeignPtr</literal> is normally very
406 <term><literal>malloc/free</literal></term>
408 <para>If all else fails, then you need to resort to
409 <literal>Foreign.malloc</literal> and
410 <literal>Foreign.free</literal>. These are just wrappers
411 around the C functions of the same name, and their
412 efficiency will depend ultimately on the implementations
413 of these functions in your platform's C library. We
414 usually find <literal>malloc</literal> and
415 <literal>free</literal> to be significantly slower than
416 the other forms of allocation above.</para>
421 <term><literal>Foreign.Marshal.Pool</literal></term>
423 <para>Pools are currently implemented using
424 <literal>malloc/free</literal>, so while they might be a
425 more convenient way to structure your memory allocation
426 than using one of the other forms of allocation, they
427 won't be any more efficient. We do plan to provide an
428 improved-performance implementation of Pools in the
429 future, however.</para>
435 <sect2 id="ffi-threads">
436 <title>Multi-threading and the FFI</title>
438 <para>In order to use the FFI in a multi-threaded setting, you must
439 use the <option>-threaded</option> option
440 (see <xref linkend="options-linker" />).</para>
443 <title>Foreign imports and multi-threading</title>
445 <para>When you call a <literal>foreign import</literal>ed
446 function that is annotated as <literal>safe</literal> (the
447 default), and the program was linked
448 using <option>-threaded</option>, then the call will run
449 concurrently with other running Haskell threads. If the
450 program was linked without <option>-threaded</option>,
451 then the other Haskell threads will be blocked until the
454 <para>This means that if you need to make a foreign call to
455 a function that takes a long time or blocks indefinitely,
456 then you should mark it <literal>safe</literal> and
457 use <option>-threaded</option>. Some library functions
458 make such calls internally; their documentation should
459 indicate when this is the case.</para>
461 <para>If you are making foreign calls from multiple Haskell
462 threads and using <option>-threaded</option>, make sure that
463 the foreign code you are calling is thread-safe. In
464 particularly, some GUI libraries are not thread-safe and
465 require that the caller only invokes GUI methods from a
466 single thread. If this is the case, you may need to
467 restrict your GUI operations to a single Haskell thread,
468 and possibly also use a bound thread (see
469 <xref linkend="haskell-threads-and-os-threads" />).</para>
471 <para>Note that foreign calls made by different Haskell
472 threads may execute in <emphasis>parallel</emphasis>, even
473 when the <literal>+RTS -N</literal> flag is not being used
474 (<xref linkend="parallel-options" />). The <literal>+RTS
475 -N</literal> flag controls parallel execution of Haskell
476 threads, but there may be an arbitrary number of foreign
477 calls in progress at any one time, regardless of
478 the <literal>+RTS -N</literal> value.</para>
481 <sect3 id="haskell-threads-and-os-threads">
482 <title>The relationship between Haskell threads and OS
485 <para>Normally there is no fixed relationship between Haskell
486 threads and OS threads. This means that when you make a
487 foreign call, that call may take place in an unspecified OS
488 thread. Furthermore, there is no guarantee that multiple
489 calls made by one Haskell thread will be made by the same OS
492 <para>This usually isn't a problem, and it allows the GHC
493 runtime system to make efficient use of OS thread resources.
494 However, there are cases where it is useful to have more
495 control over which OS thread is used, for example when
496 calling foreign code that makes use of thread-local state.
497 For cases like this, we provide <emphasis>bound
498 threads</emphasis>, which are Haskell threads tied to a
499 particular OS thread. For information on bound threads, see
501 for the <ulink url="../libraries/base/Control-Concurrent.html"><literal>Control.Concurrent</literal></ulink>
506 <title>Foreign exports and multi-threading</title>
508 <para>When the program is linked
509 with <option>-threaded</option>, then you may
510 invoke <literal>foreign export</literal>ed functions from
511 multiple OS threads concurrently. The runtime system must
512 be initialised as usual by
513 calling <literal>hs_init()</literal>
514 and <literal>hs_add_root</literal>, and these calls must
515 complete before invoking any <literal>foreign
516 export</literal>ed functions.</para>
520 <title>On the use of <literal>hs_exit()</literal></title>
522 <para><literal>hs_exit()</literal> normally causes the termination of
523 any running Haskell threads in the system, and when
524 <literal>hs_exit()</literal> returns, there will be no more Haskell
525 threads running. The runtime will then shut down the system in an
526 orderly way, generating profiling
527 output and statistics if necessary, and freeing all the memory it
530 <para>It isn't always possible to terminate a Haskell thread forcibly:
531 for example, the thread might be currently executing a foreign call,
532 and we have no way to force the foreign call to complete. What's
533 more, the runtime must
534 assume that in the worst case the Haskell code and runtime are about
535 to be removed from memory (e.g. if this is a <link linkend="win32-dlls">Windows DLL</link>,
536 <literal>hs_exit()</literal> is normally called before unloading the
537 DLL). So <literal>hs_exit()</literal> <emphasis>must</emphasis> wait
538 until all outstanding foreign calls return before it can return
541 <para>The upshot of this is that if you have Haskell threads that are
542 blocked in foreign calls, then <literal>hs_exit()</literal> may hang
543 (or possibly busy-wait) until the calls return. Therefore it's a
544 good idea to make sure you don't have any such threads in the system
545 when calling <literal>hs_exit()</literal>. This includes any threads
546 doing I/O, because I/O may (or may not, depending on the
547 type of I/O and the platform) be implemented using blocking foreign
550 <para>The GHC runtime treats program exit as a special case, to avoid
551 the need to wait for blocked threads when a standalone
552 executable exits. Since the program and all its threads are about to
553 terminate at the same time that the code is removed from memory, it
554 isn't necessary to ensure that the threads have exited first.
555 (Unofficially, if you want to use this fast and loose version of
556 <literal>hs_exit()</literal>, then call
557 <literal>shutdownHaskellAndExit()</literal> instead).</para>
565 ;;; Local Variables: ***
567 ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***