Test Trac #9036
[ghc.git] / docs / comm / the-beast / main.html
1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
2 <html>
3 <head>
4 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
5 <title>The GHC Commentary - Compiling and running the Main module</title>
6 </head>
7
8 <body BGCOLOR="FFFFFF">
9 <h1>Compiling and running the Main module</h1>
10
11 GHC allows you to determine which module contains the "main" function, and
12 what that function is called, via the <code>-fmain-is</code> flag. The trouble is
13 that the runtime system is fixed, so what symbol should it link to?
14 <p>
15 The current solution is this. Suppose the main function is <code>Foo.run</code>.
16 <ul>
17 <li>
18 Then, when compiling module <code>Foo</code>, GHC adds an extra definition:
19 <pre>
20 :Main.main = runIO Foo.run
21 </pre>
22 Now the RTS can invoke <code>:Main.main</code> to start the program. (This extra
23 definition is inserted in TcRnDriver.checkMain.)
24 <p><li>
25 Before starting the program, though, the RTS also initialises the module tree
26 by calling <code>init_:Main</code>, so when compiling the main module (Foo in this case),
27 as well as generating <code>init_Foo</code> as usual, GHC also generates
28 <pre>
29 init_zcMain() { init_Foo; }
30 </pre>
31 This extra initialisation code is generated in CodeGen.mkModuleInit.
32 </ul>
33
34 </body>
35 </html>