summaryrefslogtreecommitdiff
path: root/ghc/docs/comm/the-beast/alien.html
blob: 3d4776ebc9a079d6936ea5df3264f4880afa5739 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
  <head>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
    <title>The GHC Commentary - Alien Functions</title>
  </head>

  <body BGCOLOR="FFFFFF">
    <h1>The GHC Commentary - Alien Functions</h1>
    <p>
      GHC implements experimental (by now it is actually quite well tested)
      support for access to foreign functions and generally the interaction
      between Haskell code and code written in other languages.  Code
      generation in this context can get quite tricky.  This section attempts
      to cast some light on this aspect of the compiler.

    <h4>FFI Stub Files</h4>
    <p>
      For each Haskell module that contains a <code>foreign export
      dynamic</code> declaration, GHC generates a <code>_stub.c</code> file
      that needs to be linked with any program that imports the Haskell
      module.  When asked about it <a
      href="mailto:simonmar@microsoft.com">Simon Marlow</a> justified the
      existence of these files as follows:
    <blockquote>
      The stub files contain the helper function which invokes the Haskell
      code when called from C.
      <p>
	Each time the foreign export dynamic is invoked to create a new
	callback function, a small piece of code has to be dynamically
	generated (by code in <a
	href="http://cvs.haskell.org/cgi-bin/cvsweb.cgi/fptools/ghc/rts/Adjustor.c"><code>Adjustor.c</code></a>).  It is the address of this dynamically generated bit of
	code that is returned as the <code>Addr</code> (or <a
	href="http://cvs.haskell.org/cgi-bin/cvsweb.cgi/fptools/hslibs/lang/Ptr.lhs"><code>Ptr</code></a>).
	When called from C, the dynamically generated code must somehow invoke
	the Haskell function which was originally passed to the
	f.e.d. function -- it does this by invoking the helper function,
	passing it a <a
	href="http://cvs.haskell.org/cgi-bin/cvsweb.cgi/fptools/hslibs/lang/StablePtr.lhs"><code>StablePtr</code></a>
	to the Haskell function.  It's split this way for two reasons: the
	same helper function can be used each time the f.e.d. function is
	called, and to keep the amount of dynamically generated code to a
	minimum.
    </blockquote>
    <p>
      The stub code is generated by <a
      href="http://cvs.haskell.org/cgi-bin/cvsweb.cgi/fptools/ghc/compiler/deSugar/DsForeign.lhs"><code>DSForeign</code></a><code>.fexportEntry</code>.


    <p><small>
<!-- hhmts start -->
Last modified: Fri Aug 10 11:47:41 EST 2001
<!-- hhmts end -->
    </small>
  </body>
</html>