</head>
<body BGCOLOR="FFFFFF">
- <h1>The Glasgow Haskell Compiler (GHC) Commentary [v0.1]</h1>
+ <h1>The Glasgow Haskell Compiler (GHC) Commentary [v0.2]</h1>
<p>
<!-- Contributors: Whoever makes substantial additions or changes to the
document, please add your name and keep the order alphabetic. Moreover,
<li><a href="the-beast/typecheck.html">Checking Types</a>
<li><a href="the-beast/simplifier.html">The Mighty Simplifier</a>
<li><a href="the-beast/mangler.html">The Evil Mangler</a>
+ <li><a href="the-beast/alien.html">Alien Functions</a>
</ul>
<h2>RTS & Libraries</h2>
<p><small>
<!-- hhmts start -->
-Last modified: Wed Aug 8 00:11:49 EST 2001
+Last modified: Fri Aug 10 11:48:22 EST 2001
<!-- hhmts end -->
</small>
</body>
--- /dev/null
+<!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. 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>