main.html 1.24 KB
Newer Older
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
<!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>