Reorganisation of the source tree
[ghc-hetmet.git] / docs / comm / the-beast / alien.html
diff --git a/docs/comm/the-beast/alien.html b/docs/comm/the-beast/alien.html
new file mode 100644 (file)
index 0000000..3d4776e
--- /dev/null
@@ -0,0 +1,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>