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 -----------------------------------------------------------------------------
18 WayName(..), v_Ways, v_Build_tag, v_RTS_Build_tag, isRTSWay,
20 -- Output style options
26 opt_AutoSccsOnAllToplevs,
27 opt_AutoSccsOnExportedToplevs,
28 opt_AutoSccsOnIndividualCafs,
37 opt_IrrefutableTuples,
42 opt_SpecInlineJoinPoints,
44 opt_SimplNoPreInlining,
45 opt_SimplExcessPrecision,
49 opt_UF_CreationThreshold,
51 opt_UF_FunAppDiscount,
55 -- Optimization fuel controls
75 #include "HsVersions.h"
81 import Maybes ( firstJust )
84 import Control.Exception ( throwDyn )
86 import System.IO.Unsafe ( unsafePerformIO )
87 import Control.Monad ( when )
88 import Data.Char ( isDigit )
91 -----------------------------------------------------------------------------
94 parseStaticFlags :: [String] -> IO [String]
95 parseStaticFlags args = do
96 ready <- readIORef v_opt_C_ready
97 when ready $ throwDyn (ProgramError "Too late for parseStaticFlags: call it before newSession")
99 (leftover, errs) <- processArgs static_flags args
100 when (not (null errs)) $ throwDyn (UsageError (unlines errs))
102 -- deal with the way flags: the way (eg. prof) gives rise to
103 -- further flags, some of which might be static.
104 way_flags <- findBuildTag
106 -- if we're unregisterised, add some more flags
107 let unreg_flags | cGhcUnregisterised == "YES" = unregFlags
110 (more_leftover, errs) <- processArgs static_flags (unreg_flags ++ way_flags)
112 -- see sanity code in staticOpts
113 writeIORef v_opt_C_ready True
115 -- TABLES_NEXT_TO_CODE affects the info table layout.
116 -- Be careful to do this *after* all processArgs,
117 -- because evaluating tablesNextToCode involves looking at the global
118 -- static flags. Those pesky global variables...
119 let cg_flags | tablesNextToCode = ["-optc-DTABLES_NEXT_TO_CODE"]
122 -- HACK: -fexcess-precision is both a static and a dynamic flag. If
123 -- the static flag parser has slurped it, we must return it as a
124 -- leftover too. ToDo: make -fexcess-precision dynamic only.
125 let excess_prec | opt_SimplExcessPrecision = ["-fexcess-precision"]
128 when (not (null errs)) $ ghcError (UsageError (unlines errs))
129 return (excess_prec++cg_flags++more_leftover++leftover)
131 initStaticOpts :: IO ()
132 initStaticOpts = writeIORef v_opt_C_ready True
134 static_flags :: [(String, OptKind IO)]
135 -- All the static flags should appear in this list. It describes how each
136 -- static flag should be processed. Two main purposes:
137 -- (a) if a command-line flag doesn't appear in the list, GHC can complain
138 -- (b) a command-line flag may remove, or add, other flags; e.g. the "-fno-X" things
140 -- The common (PassFlag addOpt) action puts the static flag into the bunch of
141 -- things that are searched up by the top-level definitions like
142 -- opt_foo = lookUp (fsLit "-dfoo")
144 -- Note that ordering is important in the following list: any flag which
145 -- is a prefix flag (i.e. HasArg, Prefix, OptPrefix, AnySuffix) will override
146 -- flags further down the list with the same prefix.
149 ------- GHCi -------------------------------------------------------
150 ( "ignore-dot-ghci", PassFlag addOpt )
151 , ( "read-dot-ghci" , NoArg (removeOpt "-ignore-dot-ghci") )
153 ------- ways --------------------------------------------------------
154 , ( "prof" , NoArg (addWay WayProf) )
155 , ( "ticky" , NoArg (addWay WayTicky) )
156 , ( "parallel" , NoArg (addWay WayPar) )
157 , ( "gransim" , NoArg (addWay WayGran) )
158 , ( "smp" , NoArg (addWay WayThreaded) ) -- backwards compat.
159 , ( "debug" , NoArg (addWay WayDebug) )
160 , ( "ndp" , NoArg (addWay WayNDP) )
161 , ( "threaded" , NoArg (addWay WayThreaded) )
164 ------ Debugging ----------------------------------------------------
165 , ( "dppr-debug", PassFlag addOpt )
166 , ( "dsuppress-uniques", PassFlag addOpt )
167 , ( "dppr-user-length", AnySuffix addOpt )
168 , ( "dopt-fuel", AnySuffix addOpt )
169 -- rest of the debugging flags are dynamic
171 --------- Profiling --------------------------------------------------
172 , ( "auto-all" , NoArg (addOpt "-fauto-sccs-on-all-toplevs") )
173 , ( "auto" , NoArg (addOpt "-fauto-sccs-on-exported-toplevs") )
174 , ( "caf-all" , NoArg (addOpt "-fauto-sccs-on-individual-cafs") )
175 -- "ignore-sccs" doesn't work (ToDo)
177 , ( "no-auto-all" , NoArg (removeOpt "-fauto-sccs-on-all-toplevs") )
178 , ( "no-auto" , NoArg (removeOpt "-fauto-sccs-on-exported-toplevs") )
179 , ( "no-caf-all" , NoArg (removeOpt "-fauto-sccs-on-individual-cafs") )
181 ------- Miscellaneous -----------------------------------------------
182 , ( "no-link-chk" , NoArg (return ()) ) -- ignored for backwards compat
184 ----- Linker --------------------------------------------------------
185 , ( "static" , PassFlag addOpt )
186 , ( "dynamic" , NoArg (removeOpt "-static") )
187 , ( "rdynamic" , NoArg (return ()) ) -- ignored for compat w/ gcc
189 ----- RTS opts ------------------------------------------------------
190 , ( "H" , HasArg (setHeapSize . fromIntegral . decodeSize) )
191 , ( "Rghc-timing" , NoArg (enableTimingStats) )
193 ------ Compiler flags -----------------------------------------------
194 -- All other "-fno-<blah>" options cancel out "-f<blah>" on the hsc cmdline
195 , ( "fno-", PrefixPred (\s -> isStaticFlag ("f"++s))
196 (\s -> removeOpt ("-f"++s)) )
198 -- Pass all remaining "-f<blah>" options to hsc
199 , ( "f", AnySuffixPred (isStaticFlag) addOpt )
202 addOpt :: String -> IO ()
203 addOpt = consIORef v_opt_C
205 addWay :: WayName -> IO ()
206 addWay = consIORef v_Ways
208 removeOpt :: String -> IO ()
210 fs <- readIORef v_opt_C
211 writeIORef v_opt_C $! filter (/= f) fs
213 lookUp :: FastString -> Bool
214 lookup_def_int :: String -> Int -> Int
215 lookup_def_float :: String -> Float -> Float
216 lookup_str :: String -> Maybe String
218 -- holds the static opts while they're being collected, before
219 -- being unsafely read by unpacked_static_opts below.
220 GLOBAL_VAR(v_opt_C, defaultStaticOpts, [String])
221 GLOBAL_VAR(v_opt_C_ready, False, Bool)
223 staticFlags :: [String]
224 staticFlags = unsafePerformIO $ do
225 ready <- readIORef v_opt_C_ready
227 then panic "Static flags have not been initialised!\n Please call GHC.newSession or GHC.parseStaticFlags early enough."
228 else readIORef v_opt_C
230 -- -static is the default
231 defaultStaticOpts :: [String]
232 defaultStaticOpts = ["-static"]
234 packed_static_opts :: [FastString]
235 packed_static_opts = map mkFastString staticFlags
237 lookUp sw = sw `elem` packed_static_opts
239 -- (lookup_str "foo") looks for the flag -foo=X or -fooX,
240 -- and returns the string X
242 = case firstJust (map (startsWith sw) staticFlags) of
243 Just ('=' : str) -> Just str
247 lookup_def_int sw def = case (lookup_str sw) of
248 Nothing -> def -- Use default
249 Just xx -> try_read sw xx
251 lookup_def_float sw def = case (lookup_str sw) of
252 Nothing -> def -- Use default
253 Just xx -> try_read sw xx
256 try_read :: Read a => String -> String -> a
257 -- (try_read sw str) tries to read s; if it fails, it
258 -- bleats about flag sw
261 ((x,_):_) -> x -- Be forgiving: ignore trailing goop, and alternative parses
262 [] -> ghcError (UsageError ("Malformed argument " ++ str ++ " for flag " ++ sw))
263 -- ToDo: hack alert. We should really parse the arugments
264 -- and announce errors in a more civilised way.
268 Putting the compiler options into temporary at-files
269 may turn out to be necessary later on if we turn hsc into
270 a pure Win32 application where I think there's a command-line
271 length limit of 255. unpacked_opts understands the @ option.
273 unpacked_opts :: [String]
277 map unpackFS argv -- NOT ARGV any more: v_Static_hsc_opts
279 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
283 opt_IgnoreDotGhci :: Bool
284 opt_IgnoreDotGhci = lookUp (fsLit "-ignore-dot-ghci")
287 opt_SuppressUniques :: Bool
288 opt_SuppressUniques = lookUp (fsLit "-dsuppress-uniques")
289 opt_PprStyle_Debug :: Bool
290 opt_PprStyle_Debug = lookUp (fsLit "-dppr-debug")
291 opt_PprUserLength :: Int
292 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
294 opt_Fuel = lookup_def_int "-dopt-fuel" maxBound
297 opt_AutoSccsOnAllToplevs :: Bool
298 opt_AutoSccsOnAllToplevs = lookUp (fsLit "-fauto-sccs-on-all-toplevs")
299 opt_AutoSccsOnExportedToplevs :: Bool
300 opt_AutoSccsOnExportedToplevs = lookUp (fsLit "-fauto-sccs-on-exported-toplevs")
301 opt_AutoSccsOnIndividualCafs :: Bool
302 opt_AutoSccsOnIndividualCafs = lookUp (fsLit "-fauto-sccs-on-individual-cafs")
303 opt_SccProfilingOn :: Bool
304 opt_SccProfilingOn = lookUp (fsLit "-fscc-profiling")
305 opt_DoTickyProfiling :: Bool
306 opt_DoTickyProfiling = WayTicky `elem` (unsafePerformIO $ readIORef v_Ways)
310 opt_Hpc = lookUp (fsLit "-fhpc")
313 opt_DictsStrict :: Bool
314 opt_DictsStrict = lookUp (fsLit "-fdicts-strict")
315 opt_IrrefutableTuples :: Bool
316 opt_IrrefutableTuples = lookUp (fsLit "-firrefutable-tuples")
318 opt_Parallel = lookUp (fsLit "-fparallel")
321 opt_SpecInlineJoinPoints :: Bool
322 opt_SpecInlineJoinPoints = lookUp (fsLit "-fspec-inline-join-points")
323 opt_NoStateHack :: Bool
324 opt_NoStateHack = lookUp (fsLit "-fno-state-hack")
326 opt_CprOff = lookUp (fsLit "-fcpr-off")
327 -- Switch off CPR analysis in the new demand analyser
328 opt_MaxWorkerArgs :: Int
329 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
331 opt_GranMacros :: Bool
332 opt_GranMacros = lookUp (fsLit "-fgransim")
333 opt_HiVersion :: Integer
334 opt_HiVersion = read (cProjectVersionInt ++ cProjectPatchLevel) :: Integer
335 opt_HistorySize :: Int
336 opt_HistorySize = lookup_def_int "-fhistory-size" 20
337 opt_OmitBlackHoling :: Bool
338 opt_OmitBlackHoling = lookUp (fsLit "-dno-black-holing")
340 -- Simplifier switches
341 opt_SimplNoPreInlining :: Bool
342 opt_SimplNoPreInlining = lookUp (fsLit "-fno-pre-inlining")
343 -- NoPreInlining is there just to see how bad things
344 -- get if you don't do it!
345 opt_SimplExcessPrecision :: Bool
346 opt_SimplExcessPrecision = lookUp (fsLit "-fexcess-precision")
349 opt_UF_CreationThreshold :: Int
350 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
351 opt_UF_UseThreshold :: Int
352 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
353 opt_UF_FunAppDiscount :: Int
354 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
355 opt_UF_KeenessFactor :: Float
356 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
359 opt_UF_DearOp = ( 4 :: Int)
362 -- Related to linking
364 #if darwin_TARGET_OS && x86_64_TARGET_ARCH
367 opt_PIC = lookUp (fsLit "-fPIC")
370 opt_Static = lookUp (fsLit "-static")
371 opt_Unregisterised :: Bool
372 opt_Unregisterised = lookUp (fsLit "-funregisterised")
374 -- Derived, not a real option. Determines whether we will be compiling
375 -- info tables that reside just before the entry code, or with an
376 -- indirection to the entry code. See TABLES_NEXT_TO_CODE in
377 -- includes/InfoTables.h.
378 tablesNextToCode :: Bool
379 tablesNextToCode = not opt_Unregisterised
380 && cGhcEnableTablesNextToCode == "YES"
382 opt_EmitExternalCore :: Bool
383 opt_EmitExternalCore = lookUp (fsLit "-fext-core")
385 -- Include full span info in error messages, instead of just the start position.
386 opt_ErrorSpans :: Bool
387 opt_ErrorSpans = lookUp (fsLit "-ferror-spans")
390 -- object files and libraries to be linked in are collected here.
391 -- ToDo: perhaps this could be done without a global, it wasn't obvious
392 -- how to do it though --SDM.
393 GLOBAL_VAR(v_Ld_inputs, [], [String])
395 isStaticFlag :: String -> Bool
398 "fauto-sccs-on-all-toplevs",
399 "fauto-sccs-on-exported-toplevs",
400 "fauto-sccs-on-individual-cafs",
403 "fspec-inline-join-points",
404 "firrefutable-tuples",
407 "fno-hi-version-check",
409 "fno-method-sharing",
415 "fhardwire-lib-paths",
423 || any (`isPrefixOf` f) [
424 "fliberate-case-threshold",
427 "funfolding-creation-threshold",
428 "funfolding-use-threshold",
429 "funfolding-fun-discount",
430 "funfolding-keeness-factor"
435 -- Misc functions for command-line options
437 startsWith :: String -> String -> Maybe String
438 -- startsWith pfx (pfx++rest) = Just rest
440 startsWith [] str = Just str
441 startsWith (c:cs) (s:ss)
442 = if c /= s then Nothing else startsWith cs ss
443 startsWith _ [] = Nothing
446 -----------------------------------------------------------------------------
447 -- convert sizes like "3.5M" into integers
449 decodeSize :: String -> Integer
451 | c == "" = truncate n
452 | c == "K" || c == "k" = truncate (n * 1000)
453 | c == "M" || c == "m" = truncate (n * 1000 * 1000)
454 | c == "G" || c == "g" = truncate (n * 1000 * 1000 * 1000)
455 | otherwise = throwDyn (CmdLineError ("can't decode size: " ++ str))
456 where (m, c) = span pred str
458 pred c = isDigit c || c == '.'
461 -----------------------------------------------------------------------------
464 foreign import ccall unsafe "setHeapSize" setHeapSize :: Int -> IO ()
465 foreign import ccall unsafe "enableTimingStats" enableTimingStats :: IO ()
467 -----------------------------------------------------------------------------
470 -- The central concept of a "way" is that all objects in a given
471 -- program must be compiled in the same "way". Certain options change
472 -- parameters of the virtual machine, eg. profiling adds an extra word
473 -- to the object header, so profiling objects cannot be linked with
474 -- non-profiling objects.
476 -- After parsing the command-line options, we determine which "way" we
477 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
479 -- We then find the "build-tag" associated with this way, and this
480 -- becomes the suffix used to find .hi files and libraries used in
483 GLOBAL_VAR(v_Build_tag, "", String)
485 -- The RTS has its own build tag, because there are some ways that
486 -- affect the RTS only.
487 GLOBAL_VAR(v_RTS_Build_tag, "", String)
516 GLOBAL_VAR(v_Ways, [] ,[WayName])
518 allowed_combination :: [WayName] -> Bool
519 allowed_combination way = and [ x `allowedWith` y
520 | x <- way, y <- way, x < y ]
522 -- Note ordering in these tests: the left argument is
523 -- <= the right argument, according to the Ord instance
526 -- debug is allowed with everything
527 _ `allowedWith` WayDebug = True
528 WayDebug `allowedWith` _ = True
530 WayProf `allowedWith` WayNDP = True
531 WayThreaded `allowedWith` WayProf = True
532 _ `allowedWith` _ = False
535 findBuildTag :: IO [String] -- new options
537 way_names <- readIORef v_Ways
538 let ws = sort (nub way_names)
540 if not (allowed_combination ws)
541 then throwDyn (CmdLineError $
542 "combination not supported: " ++
543 foldr1 (\a b -> a ++ '/':b)
544 (map (wayName . lkupWay) ws))
545 else let ways = map lkupWay ws
546 tag = mkBuildTag (filter (not.wayRTSOnly) ways)
547 rts_tag = mkBuildTag ways
548 flags = map wayOpts ways
550 writeIORef v_Build_tag tag
551 writeIORef v_RTS_Build_tag rts_tag
552 return (concat flags)
556 mkBuildTag :: [Way] -> String
557 mkBuildTag ways = concat (intersperse "_" (map wayTag ways))
559 lkupWay :: WayName -> Way
561 case lookup w way_details of
562 Nothing -> error "findBuildTag"
563 Just details -> details
565 isRTSWay :: WayName -> Bool
566 isRTSWay = wayRTSOnly . lkupWay
575 way_details :: [ (WayName, Way) ]
577 [ (WayThreaded, Way "thr" True "Threaded" [
578 #if defined(freebsd_TARGET_OS)
581 -- FreeBSD's default threading library is the KSE-based M:N libpthread,
582 -- which GHC has some problems with. It's currently not clear whether
583 -- the problems are our fault or theirs, but it seems that using the
584 -- alternative 1:1 threading library libthr works around it:
586 #elif defined(solaris2_TARGET_OS)
591 (WayDebug, Way "debug" True "Debug" [] ),
593 (WayProf, Way "p" False "Profiling"
596 , "-optc-DPROFILING" ]),
598 (WayTicky, Way "t" True "Ticky-ticky Profiling"
600 , "-optc-DTICKY_TICKY" ]),
602 -- optl's below to tell linker where to find the PVM library -- HWL
603 (WayPar, Way "mp" False "Parallel"
605 , "-D__PARALLEL_HASKELL__"
607 , "-package concurrent"
609 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
613 -- at the moment we only change the RTS and could share compiler and libs!
614 (WayPar, Way "mt" False "Parallel ticky profiling"
616 , "-D__PARALLEL_HASKELL__"
619 , "-package concurrent"
621 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
625 (WayPar, Way "md" False "Distributed"
627 , "-D__PARALLEL_HASKELL__"
628 , "-D__DISTRIBUTED_HASKELL__"
631 , "-package concurrent"
633 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
637 (WayGran, Way "mg" False "GranSim"
641 , "-package concurrent" ]),
643 (WayNDP, Way "ndp" False "Nested data parallelism"
647 (WayUser_a, Way "a" False "User way 'a'" ["$WAY_a_REAL_OPTS"]),
648 (WayUser_b, Way "b" False "User way 'b'" ["$WAY_b_REAL_OPTS"]),
649 (WayUser_c, Way "c" False "User way 'c'" ["$WAY_c_REAL_OPTS"]),
650 (WayUser_d, Way "d" False "User way 'd'" ["$WAY_d_REAL_OPTS"]),
651 (WayUser_e, Way "e" False "User way 'e'" ["$WAY_e_REAL_OPTS"]),
652 (WayUser_f, Way "f" False "User way 'f'" ["$WAY_f_REAL_OPTS"]),
653 (WayUser_g, Way "g" False "User way 'g'" ["$WAY_g_REAL_OPTS"]),
654 (WayUser_h, Way "h" False "User way 'h'" ["$WAY_h_REAL_OPTS"]),
655 (WayUser_i, Way "i" False "User way 'i'" ["$WAY_i_REAL_OPTS"]),
656 (WayUser_j, Way "j" False "User way 'j'" ["$WAY_j_REAL_OPTS"]),
657 (WayUser_k, Way "k" False "User way 'k'" ["$WAY_k_REAL_OPTS"]),
658 (WayUser_l, Way "l" False "User way 'l'" ["$WAY_l_REAL_OPTS"]),
659 (WayUser_m, Way "m" False "User way 'm'" ["$WAY_m_REAL_OPTS"]),
660 (WayUser_n, Way "n" False "User way 'n'" ["$WAY_n_REAL_OPTS"]),
661 (WayUser_o, Way "o" False "User way 'o'" ["$WAY_o_REAL_OPTS"]),
662 (WayUser_A, Way "A" False "User way 'A'" ["$WAY_A_REAL_OPTS"]),
663 (WayUser_B, Way "B" False "User way 'B'" ["$WAY_B_REAL_OPTS"])
666 unregFlags :: [String]
669 , "-optc-DUSE_MINIINTERPRETER"
670 , "-fno-asm-mangling"