1 -----------------------------------------------------------------------------
5 -- Static flags can only be set once, on the command-line. Inside GHC,
6 -- each static flag corresponds to a top-level value, usually of type Bool.
8 -- (c) The University of Glasgow 2005
10 -----------------------------------------------------------------------------
12 module StaticFlagParser (parseStaticFlags) where
14 #include "HsVersions.h"
16 import qualified StaticFlags as SF
17 import StaticFlags ( v_opt_C_ready, getWayFlags, tablesNextToCode, WayName(..)
18 , opt_SimplExcessPrecision )
30 -----------------------------------------------------------------------------
33 -- | Parses GHC's static flags from a list of command line arguments.
35 -- These flags are static in the sense that they can be set only once and they
36 -- are global, meaning that they affect every instance of GHC running;
37 -- multiple GHC threads will use the same flags.
39 -- This function must be called before any session is started, i.e., before
40 -- the first call to 'GHC.withGhc'.
42 -- Static flags are more of a hack and are static for more or less historical
43 -- reasons. In the long run, most static flags should eventually become
46 -- XXX: can we add an auto-generated list of static flags here?
48 parseStaticFlags :: [Located String] -> IO ([Located String], [Located String])
49 parseStaticFlags args = do
50 ready <- readIORef v_opt_C_ready
51 when ready $ ghcError (ProgramError "Too late for parseStaticFlags: call it before newSession")
53 (leftover, errs, warns1) <- processArgs static_flags args
54 when (not (null errs)) $ ghcError $ errorsToGhcException errs
56 -- deal with the way flags: the way (eg. prof) gives rise to
57 -- further flags, some of which might be static.
58 way_flags <- getWayFlags
59 let way_flags' = map (mkGeneralLocated "in way flags") way_flags
61 -- if we're unregisterised, add some more flags
62 let unreg_flags | cGhcUnregisterised == "YES" = unregFlags
65 (more_leftover, errs, warns2) <- processArgs static_flags (unreg_flags ++ way_flags')
67 -- see sanity code in staticOpts
68 writeIORef v_opt_C_ready True
70 -- TABLES_NEXT_TO_CODE affects the info table layout.
71 -- Be careful to do this *after* all processArgs,
72 -- because evaluating tablesNextToCode involves looking at the global
73 -- static flags. Those pesky global variables...
74 let cg_flags | tablesNextToCode = map (mkGeneralLocated "in cg_flags")
75 ["-optc-DTABLES_NEXT_TO_CODE"]
78 -- HACK: -fexcess-precision is both a static and a dynamic flag. If
79 -- the static flag parser has slurped it, we must return it as a
80 -- leftover too. ToDo: make -fexcess-precision dynamic only.
82 | opt_SimplExcessPrecision = map (mkGeneralLocated "in excess_prec")
83 ["-fexcess-precision"]
86 when (not (null errs)) $ ghcError $ errorsToGhcException errs
87 return (excess_prec ++ cg_flags ++ more_leftover ++ leftover,
90 static_flags :: [Flag IO]
91 -- All the static flags should appear in this list. It describes how each
92 -- static flag should be processed. Two main purposes:
93 -- (a) if a command-line flag doesn't appear in the list, GHC can complain
94 -- (b) a command-line flag may remove, or add, other flags; e.g. the "-fno-X" things
96 -- The common (PassFlag addOpt) action puts the static flag into the bunch of
97 -- things that are searched up by the top-level definitions like
98 -- opt_foo = lookUp (fsLit "-dfoo")
100 -- Note that ordering is important in the following list: any flag which
101 -- is a prefix flag (i.e. HasArg, Prefix, OptPrefix, AnySuffix) will override
102 -- flags further down the list with the same prefix.
105 ------- GHCi -------------------------------------------------------
106 Flag "ignore-dot-ghci" (PassFlag addOpt)
107 , Flag "read-dot-ghci" (NoArg (removeOpt "-ignore-dot-ghci"))
109 ------- ways --------------------------------------------------------
110 , Flag "prof" (NoArg (addWay WayProf))
111 , Flag "eventlog" (NoArg (addWay WayEventLog))
112 , Flag "parallel" (NoArg (addWay WayPar))
113 , Flag "gransim" (NoArg (addWay WayGran))
114 , Flag "smp" (NoArg (addWay WayThreaded >> deprecate "Use -threaded instead"))
115 , Flag "debug" (NoArg (addWay WayDebug))
116 , Flag "ndp" (NoArg (addWay WayNDP))
117 , Flag "threaded" (NoArg (addWay WayThreaded))
119 , Flag "ticky" (PassFlag (\f -> do addOpt f; addWay WayDebug))
120 -- -ticky enables ticky-ticky code generation, and also implies -debug which
121 -- is required to get the RTS ticky support.
123 ------ Debugging ----------------------------------------------------
124 , Flag "dppr-debug" (PassFlag addOpt)
125 , Flag "dppr-cols" (AnySuffix addOpt)
126 , Flag "dppr-user-length" (AnySuffix addOpt)
127 , Flag "dppr-case-as-let" (PassFlag addOpt)
128 , Flag "dsuppress-all" (PassFlag addOpt)
129 , Flag "dsuppress-uniques" (PassFlag addOpt)
130 , Flag "dsuppress-coercions" (PassFlag addOpt)
131 , Flag "dsuppress-module-prefixes" (PassFlag addOpt)
132 , Flag "dsuppress-type-applications" (PassFlag addOpt)
133 , Flag "dsuppress-idinfo" (PassFlag addOpt)
134 , Flag "dsuppress-type-signatures" (PassFlag addOpt)
135 , Flag "dopt-fuel" (AnySuffix addOpt)
136 , Flag "dtrace-level" (AnySuffix addOpt)
137 , Flag "dno-debug-output" (PassFlag addOpt)
138 , Flag "dstub-dead-values" (PassFlag addOpt)
139 -- rest of the debugging flags are dynamic
141 ----- Linker --------------------------------------------------------
142 , Flag "static" (PassFlag addOpt)
143 , Flag "dynamic" (NoArg (removeOpt "-static" >> addWay WayDyn))
144 -- ignored for compat w/ gcc:
145 , Flag "rdynamic" (NoArg (return ()))
147 ----- RTS opts ------------------------------------------------------
148 , Flag "H" (HasArg (\s -> liftEwM (setHeapSize (fromIntegral (decodeSize s)))))
150 , Flag "Rghc-timing" (NoArg (liftEwM enableTimingStats))
152 ------ Compiler flags -----------------------------------------------
154 -- -fPIC requires extra checking: only the NCG supports it.
155 -- See also DynFlags.parseDynamicFlags.
156 , Flag "fPIC" (PassFlag setPIC)
158 -- All other "-fno-<blah>" options cancel out "-f<blah>" on the hsc cmdline
160 (PrefixPred (\s -> isStaticFlag ("f"++s)) (\s -> removeOpt ("-f"++s)))
163 -- Pass all remaining "-f<blah>" options to hsc
164 , Flag "f" (AnySuffixPred isStaticFlag addOpt)
167 setPIC :: String -> StaticP ()
168 setPIC | cGhcWithNativeCodeGen == "YES" || cGhcUnregisterised == "YES"
171 = ghcError $ CmdLineError "-fPIC is not supported on this platform"
173 isStaticFlag :: String -> Bool
178 "fspec-inline-join-points",
179 "firrefutable-tuples",
182 "fno-hi-version-check",
185 "fsimple-list-literals",
191 "fhardwire-lib-paths",
198 || any (`isPrefixOf` f) [
199 "fliberate-case-threshold",
202 "funfolding-creation-threshold",
203 "funfolding-dict-threshold",
204 "funfolding-use-threshold",
205 "funfolding-fun-discount",
206 "funfolding-keeness-factor"
209 unregFlags :: [Located String]
210 unregFlags = map (mkGeneralLocated "in unregFlags")
212 , "-optc-DUSE_MINIINTERPRETER"
213 , "-funregisterised" ]
215 -----------------------------------------------------------------------------
216 -- convert sizes like "3.5M" into integers
218 decodeSize :: String -> Integer
220 | c == "" = truncate n
221 | c == "K" || c == "k" = truncate (n * 1000)
222 | c == "M" || c == "m" = truncate (n * 1000 * 1000)
223 | c == "G" || c == "g" = truncate (n * 1000 * 1000 * 1000)
224 | otherwise = ghcError (CmdLineError ("can't decode size: " ++ str))
225 where (m, c) = span pred str
227 pred c = isDigit c || c == '.'
230 type StaticP = EwM IO
232 addOpt :: String -> StaticP ()
233 addOpt = liftEwM . SF.addOpt
235 addWay :: WayName -> StaticP ()
236 addWay = liftEwM . SF.addWay
238 removeOpt :: String -> StaticP ()
239 removeOpt = liftEwM . SF.removeOpt
241 -----------------------------------------------------------------------------
244 foreign import ccall unsafe "setHeapSize" setHeapSize :: Int -> IO ()
245 foreign import ccall unsafe "enableTimingStats" enableTimingStats :: IO ()