1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
4 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
5 <title>The GHC Commentary - The Glorious Driver</title>
8 <body BGCOLOR="FFFFFF">
9 <h1>The GHC Commentary - The Glorious Driver</h1>
11 The Glorious Driver (GD) is the part of GHC that orchestrates the
12 interaction of all the other pieces that make up GHC. It supersedes the
13 <em>Evil Driver (ED),</em> which was a Perl script that served the same
14 purpose and was in use until version 4.08.1 of GHC. Simon Marlow
15 eventually slayed the ED and instated the GD. The GD is usually called
16 the <em>Compilation Manager</em> these days.
19 The GD has been substantially extended for GHCi, i.e., the interactive
20 variant of GHC that integrates the compiler with a (meta-circular)
21 interpreter since version 5.00. Most of the driver is located in the
24 href="http://cvs.haskell.org/cgi-bin/cvsweb.cgi/fptools/ghc/compiler/main/"><code>fptools/ghc/compiler/main/</code></a>.
27 <h2>Command Line Options</h2>
29 GHC's many flavours of command line options make the code interpreting
30 them rather involved. The following provides a brief overview of the
31 processing of these options. Since the addition of the interactive
32 front-end to GHC, there are two kinds of options: <em>static
33 options</em> and <em>dynamic options.</em> The former can only be set
34 when the system is invoked, whereas the latter can be altered in the
35 course of an interactive session. A brief explanation on the difference
36 between these options and related matters is at the start of the module
38 href="http://cvs.haskell.org/cgi-bin/cvsweb.cgi/fptools/ghc/compiler/main/CmdLineOpts.lhs"><code>CmdLineOpts</code></a>.
39 The same module defines the enumeration <code>DynFlag</code>, which
40 contains all dynamic flags. Moreover, there is the labelled record
41 <code>DynFlags</code> that collects all the flag-related information
42 that is passed by the compilation manager to the compiler proper,
43 <code>hsc</code>, whenever a compilation is triggered. If you like to
44 find out whether an option is static, use the predicate
45 <code>isStaticHscFlag</code> in the same module.
47 The second module that contains a lot of code related to the management
49 href="http://cvs.haskell.org/cgi-bin/cvsweb.cgi/fptools/ghc/compiler/main/DriverFlags.hs"><code>DriverFlags.hs</code></a>.
50 In particular, the module contains two association lists that map the
51 textual representation of the various flags to a data structure that
52 tells the driver how to parse the flag (e.g., whether it has any
53 arguments) and provides its internal representation. All static flags
54 are contained in <code>static_flags</code>. A whole range of
55 <code>-f</code> flags can be negated by adding a <code>-f-no-</code>
56 prefix. These flags are contained in the association list
59 The driver uses a nasty hack based on <code>IORef</code>s that permits
60 the rest of the compiler to access static flags as CAFs; i.e., there is
61 a family of toplevel variable definitions in
63 href="http://cvs.haskell.org/cgi-bin/cvsweb.cgi/fptools/ghc/compiler/main/CmdLineOpts.lhs"><code>CmdLineOpts</code></a>,
64 below the literate section heading <i>Static options</i>, each of which
65 contains the value of one static option. This is essentially realised
66 via global variables (in the sense of C-style, updatable, global
67 variables) defined via an evil pre-processor macro named
68 <code>GLOBAL_VAR</code>, which is defined in a particularly ugly corner
69 of GHC, namely the C header file
71 href="http://cvs.haskell.org/cgi-bin/cvsweb.cgi/fptools/ghc/compiler/HsVersions.h"><code>HsVersions.h</code></a>.
73 <h2>Linking the <code>RTS</code> and <code>libHSstd</code></h2>
75 Since the RTS and HSstd refer to each other, there is a Cunning
76 Hack to avoid putting them each on the command-line twice or
77 thrice (aside: try asking for `plaice and chips thrice' in a
78 fish and chip shop; bet you only get two lots). The hack involves
80 the symbols that the RTS needs from libHSstd, such as
81 <code>PrelWeak_runFinalizzerBatch_closure</code> and
82 <code>__init_Prelude</code>, to the link line with the
83 <code>-u</code> flag. The standard library appears before the
84 RTS on the link line, and these options cause the corresponding
85 symbols to be picked up even so the linked might not have seen them
86 being used as the RTS appears later on the link line. As a result,
87 when the RTS is also scanned, these symbols are already resolved. This
88 avoids the linker having to read the standard library and RTS
92 This does, however, leads to a complication. Normal Haskell
93 programs do not have a <code>main()</code> function, so this is
94 supplied by the RTS (in the file
95 <a href="http://cvs.haskell.org/cgi-bin/cvsweb.cgi/fptools/ghc/rts/Main.c"><code>Main.c</code></a>).
96 It calls <code>startupHaskell</code>, which
97 itself calls <code>__init_PrelMain</code>, which is therefore,
98 since it occurs in the standard library, one of the symbols
99 passed to the linker using the <code>-u</code> option. This is fine
100 for standalone Haskell programs, but as soon as the Haskell code is only
101 used as part of a program implemented in a foreign language, the
102 <code>main()</code> function of that foreign language should be used
103 instead of that of the Haskell runtime. In this case, the previously
104 described arrangement unfortunately fails as
105 <code>__init_PrelMain</code> had better not be linked in,
106 because it tries to call <code>__init_Main</code>, which won't
107 exist. In other words, the RTS's <code>main()</code> refers to
108 <code>__init_PrelMain</code> which in turn refers to
109 <code>__init_Main</code>. Although the RTS's <code>main()</code>
110 might not be linked in if the program provides its own, the driver
111 will normally force <code>__init_PrelMain</code> to be linked in anyway,
112 using <code>-u</code>, because it's a back-reference from the
113 RTS to HSstd. This case is coped with by the <code>-no-hs-main</code>
114 flag, which suppresses passing the corresonding <code>-u</code> option
115 to the linker -- although in some versions of the compiler (e.g., 5.00.2)
116 it didn't work. In addition, the driver arranges that the C program
117 providing the <code>main()</code> that we want to use appears in the
118 link line after the RTS. Therefore, the RTS's main is never used and
119 without the <code>-u</code> the label <code>__init_PrelMain</code>
125 Last modified: Fri Aug 24 16:16:10 EST 2001