Reorganisation of the source tree
[ghc-hetmet.git] / docs / comm / the-beast / main.html
diff --git a/docs/comm/the-beast/main.html b/docs/comm/the-beast/main.html
new file mode 100644 (file)
index 0000000..332ffaa
--- /dev/null
@@ -0,0 +1,35 @@
+<!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 - Compiling and running the Main module</title>
+  </head>
+
+  <body BGCOLOR="FFFFFF">
+    <h1>Compiling and running the Main module</h1>
+
+GHC allows you to determine which module contains the "main" function, and
+what that function is called, via the <code>-fmain-is</code> flag.   The trouble is
+that the runtime system is fixed, so what symbol should it link to?
+<p>
+The current solution is this.  Suppose the main function is <code>Foo.run</code>.
+<ul>
+<li>
+Then, when compiling module <code>Foo</code>, GHC adds an extra definition:
+<pre>
+  :Main.main = runIO Foo.run
+</pre>
+Now the RTS can invoke <code>:Main.main</code> to start the program.  (This extra
+definition is inserted in TcRnDriver.checkMain.)
+<p><li>
+Before starting the program, though, the RTS also initialises the module tree
+by calling <code>init_:Main</code>, so when compiling the main module (Foo in this case),
+as well as generating <code>init_Foo</code> as usual, GHC also generates
+<pre>
+  init_zcMain() { init_Foo; }
+</pre>
+This extra initialisation code is generated in CodeGen.mkModuleInit.
+</ul>
+
+  </body>
+</html>