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
27 opt_AutoSccsOnAllToplevs,
28 opt_AutoSccsOnExportedToplevs,
29 opt_AutoSccsOnIndividualCafs,
38 opt_IrrefutableTuples,
44 opt_SpecInlineJoinPoints,
46 opt_SimplNoPreInlining,
47 opt_SimplExcessPrecision,
51 opt_UF_CreationThreshold,
53 opt_UF_FunAppDiscount,
57 -- Optimization fuel controls
77 #include "HsVersions.h"
83 import Maybes ( firstJust )
86 import Control.Exception ( throwDyn )
88 import System.IO.Unsafe ( unsafePerformIO )
89 import Control.Monad ( when )
90 import Data.Char ( isDigit )
93 -----------------------------------------------------------------------------
96 parseStaticFlags :: [String] -> IO [String]
97 parseStaticFlags args = do
98 ready <- readIORef v_opt_C_ready
99 when ready $ throwDyn (ProgramError "Too late for parseStaticFlags: call it before newSession")
101 (leftover, errs) <- processArgs static_flags args
102 when (not (null errs)) $ throwDyn (UsageError (unlines errs))
104 -- deal with the way flags: the way (eg. prof) gives rise to
105 -- further flags, some of which might be static.
106 way_flags <- findBuildTag
108 -- if we're unregisterised, add some more flags
109 let unreg_flags | cGhcUnregisterised == "YES" = unregFlags
112 (more_leftover, errs) <- processArgs static_flags (unreg_flags ++ way_flags)
114 -- see sanity code in staticOpts
115 writeIORef v_opt_C_ready True
117 -- TABLES_NEXT_TO_CODE affects the info table layout.
118 -- Be careful to do this *after* all processArgs,
119 -- because evaluating tablesNextToCode involves looking at the global
120 -- static flags. Those pesky global variables...
121 let cg_flags | tablesNextToCode = ["-optc-DTABLES_NEXT_TO_CODE"]
124 -- HACK: -fexcess-precision is both a static and a dynamic flag. If
125 -- the static flag parser has slurped it, we must return it as a
126 -- leftover too. ToDo: make -fexcess-precision dynamic only.
127 let excess_prec | opt_SimplExcessPrecision = ["-fexcess-precision"]
130 when (not (null errs)) $ ghcError (UsageError (unlines errs))
131 return (excess_prec++cg_flags++more_leftover++leftover)
133 initStaticOpts :: IO ()
134 initStaticOpts = writeIORef v_opt_C_ready True
136 static_flags :: [(String, OptKind IO)]
137 -- All the static flags should appear in this list. It describes how each
138 -- static flag should be processed. Two main purposes:
139 -- (a) if a command-line flag doesn't appear in the list, GHC can complain
140 -- (b) a command-line flag may remove, or add, other flags; e.g. the "-fno-X" things
142 -- The common (PassFlag addOpt) action puts the static flag into the bunch of
143 -- things that are searched up by the top-level definitions like
144 -- opt_foo = lookUp (fsLit "-dfoo")
146 -- Note that ordering is important in the following list: any flag which
147 -- is a prefix flag (i.e. HasArg, Prefix, OptPrefix, AnySuffix) will override
148 -- flags further down the list with the same prefix.
151 ------- GHCi -------------------------------------------------------
152 ( "ignore-dot-ghci", PassFlag addOpt )
153 , ( "read-dot-ghci" , NoArg (removeOpt "-ignore-dot-ghci") )
155 ------- ways --------------------------------------------------------
156 , ( "prof" , NoArg (addWay WayProf) )
157 , ( "ticky" , NoArg (addWay WayTicky) )
158 , ( "parallel" , NoArg (addWay WayPar) )
159 , ( "gransim" , NoArg (addWay WayGran) )
160 , ( "smp" , NoArg (addWay WayThreaded) ) -- backwards compat.
161 , ( "debug" , NoArg (addWay WayDebug) )
162 , ( "ndp" , NoArg (addWay WayNDP) )
163 , ( "threaded" , NoArg (addWay WayThreaded) )
166 ------ Debugging ----------------------------------------------------
167 , ( "dppr-debug", PassFlag addOpt )
168 , ( "dsuppress-uniques", PassFlag addOpt )
169 , ( "dppr-user-length", AnySuffix addOpt )
170 , ( "dopt-fuel", AnySuffix addOpt )
171 , ( "dno-debug-output", PassFlag addOpt )
172 -- rest of the debugging flags are dynamic
174 --------- Profiling --------------------------------------------------
175 , ( "auto-all" , NoArg (addOpt "-fauto-sccs-on-all-toplevs") )
176 , ( "auto" , NoArg (addOpt "-fauto-sccs-on-exported-toplevs") )
177 , ( "caf-all" , NoArg (addOpt "-fauto-sccs-on-individual-cafs") )
178 -- "ignore-sccs" doesn't work (ToDo)
180 , ( "no-auto-all" , NoArg (removeOpt "-fauto-sccs-on-all-toplevs") )
181 , ( "no-auto" , NoArg (removeOpt "-fauto-sccs-on-exported-toplevs") )
182 , ( "no-caf-all" , NoArg (removeOpt "-fauto-sccs-on-individual-cafs") )
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
295 opt_NoDebugOutput :: Bool
296 opt_NoDebugOutput = lookUp (fsLit "-dno-debug-output")
300 opt_AutoSccsOnAllToplevs :: Bool
301 opt_AutoSccsOnAllToplevs = lookUp (fsLit "-fauto-sccs-on-all-toplevs")
302 opt_AutoSccsOnExportedToplevs :: Bool
303 opt_AutoSccsOnExportedToplevs = lookUp (fsLit "-fauto-sccs-on-exported-toplevs")
304 opt_AutoSccsOnIndividualCafs :: Bool
305 opt_AutoSccsOnIndividualCafs = lookUp (fsLit "-fauto-sccs-on-individual-cafs")
306 opt_SccProfilingOn :: Bool
307 opt_SccProfilingOn = lookUp (fsLit "-fscc-profiling")
308 opt_DoTickyProfiling :: Bool
309 opt_DoTickyProfiling = WayTicky `elem` (unsafePerformIO $ readIORef v_Ways)
313 opt_Hpc = lookUp (fsLit "-fhpc")
316 opt_DictsStrict :: Bool
317 opt_DictsStrict = lookUp (fsLit "-fdicts-strict")
318 opt_IrrefutableTuples :: Bool
319 opt_IrrefutableTuples = lookUp (fsLit "-firrefutable-tuples")
321 opt_Parallel = lookUp (fsLit "-fparallel")
324 opt_DsMultiTyVar :: Bool
325 opt_DsMultiTyVar = not (lookUp (fsLit "-fno-ds-multi-tyvar"))
328 opt_SpecInlineJoinPoints :: Bool
329 opt_SpecInlineJoinPoints = lookUp (fsLit "-fspec-inline-join-points")
331 opt_NoStateHack :: Bool
332 opt_NoStateHack = lookUp (fsLit "-fno-state-hack")
334 opt_CprOff = lookUp (fsLit "-fcpr-off")
335 -- Switch off CPR analysis in the new demand analyser
336 opt_MaxWorkerArgs :: Int
337 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
339 opt_GranMacros :: Bool
340 opt_GranMacros = lookUp (fsLit "-fgransim")
341 opt_HiVersion :: Integer
342 opt_HiVersion = read (cProjectVersionInt ++ cProjectPatchLevel) :: Integer
343 opt_HistorySize :: Int
344 opt_HistorySize = lookup_def_int "-fhistory-size" 20
345 opt_OmitBlackHoling :: Bool
346 opt_OmitBlackHoling = lookUp (fsLit "-dno-black-holing")
348 -- Simplifier switches
349 opt_SimplNoPreInlining :: Bool
350 opt_SimplNoPreInlining = lookUp (fsLit "-fno-pre-inlining")
351 -- NoPreInlining is there just to see how bad things
352 -- get if you don't do it!
353 opt_SimplExcessPrecision :: Bool
354 opt_SimplExcessPrecision = lookUp (fsLit "-fexcess-precision")
357 opt_UF_CreationThreshold :: Int
358 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
359 opt_UF_UseThreshold :: Int
360 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
361 opt_UF_FunAppDiscount :: Int
362 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
363 opt_UF_KeenessFactor :: Float
364 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
367 opt_UF_DearOp = ( 4 :: Int)
370 -- Related to linking
372 #if darwin_TARGET_OS && x86_64_TARGET_ARCH
375 opt_PIC = lookUp (fsLit "-fPIC")
378 opt_Static = lookUp (fsLit "-static")
379 opt_Unregisterised :: Bool
380 opt_Unregisterised = lookUp (fsLit "-funregisterised")
382 -- Derived, not a real option. Determines whether we will be compiling
383 -- info tables that reside just before the entry code, or with an
384 -- indirection to the entry code. See TABLES_NEXT_TO_CODE in
385 -- includes/InfoTables.h.
386 tablesNextToCode :: Bool
387 tablesNextToCode = not opt_Unregisterised
388 && cGhcEnableTablesNextToCode == "YES"
390 opt_EmitExternalCore :: Bool
391 opt_EmitExternalCore = lookUp (fsLit "-fext-core")
393 -- Include full span info in error messages, instead of just the start position.
394 opt_ErrorSpans :: Bool
395 opt_ErrorSpans = lookUp (fsLit "-ferror-spans")
398 -- object files and libraries to be linked in are collected here.
399 -- ToDo: perhaps this could be done without a global, it wasn't obvious
400 -- how to do it though --SDM.
401 GLOBAL_VAR(v_Ld_inputs, [], [String])
403 isStaticFlag :: String -> Bool
406 "fauto-sccs-on-all-toplevs",
407 "fauto-sccs-on-exported-toplevs",
408 "fauto-sccs-on-individual-cafs",
411 "fspec-inline-join-points",
412 "firrefutable-tuples",
415 "fno-hi-version-check",
417 "fno-method-sharing",
419 "fno-ds-multi-tyvar",
424 "fhardwire-lib-paths",
432 || any (`isPrefixOf` f) [
433 "fliberate-case-threshold",
436 "funfolding-creation-threshold",
437 "funfolding-use-threshold",
438 "funfolding-fun-discount",
439 "funfolding-keeness-factor"
444 -- Misc functions for command-line options
446 startsWith :: String -> String -> Maybe String
447 -- startsWith pfx (pfx++rest) = Just rest
449 startsWith [] str = Just str
450 startsWith (c:cs) (s:ss)
451 = if c /= s then Nothing else startsWith cs ss
452 startsWith _ [] = Nothing
455 -----------------------------------------------------------------------------
456 -- convert sizes like "3.5M" into integers
458 decodeSize :: String -> Integer
460 | c == "" = truncate n
461 | c == "K" || c == "k" = truncate (n * 1000)
462 | c == "M" || c == "m" = truncate (n * 1000 * 1000)
463 | c == "G" || c == "g" = truncate (n * 1000 * 1000 * 1000)
464 | otherwise = throwDyn (CmdLineError ("can't decode size: " ++ str))
465 where (m, c) = span pred str
467 pred c = isDigit c || c == '.'
470 -----------------------------------------------------------------------------
473 foreign import ccall unsafe "setHeapSize" setHeapSize :: Int -> IO ()
474 foreign import ccall unsafe "enableTimingStats" enableTimingStats :: IO ()
476 -----------------------------------------------------------------------------
479 -- The central concept of a "way" is that all objects in a given
480 -- program must be compiled in the same "way". Certain options change
481 -- parameters of the virtual machine, eg. profiling adds an extra word
482 -- to the object header, so profiling objects cannot be linked with
483 -- non-profiling objects.
485 -- After parsing the command-line options, we determine which "way" we
486 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
488 -- We then find the "build-tag" associated with this way, and this
489 -- becomes the suffix used to find .hi files and libraries used in
492 GLOBAL_VAR(v_Build_tag, "", String)
494 -- The RTS has its own build tag, because there are some ways that
495 -- affect the RTS only.
496 GLOBAL_VAR(v_RTS_Build_tag, "", String)
525 GLOBAL_VAR(v_Ways, [] ,[WayName])
527 allowed_combination :: [WayName] -> Bool
528 allowed_combination way = and [ x `allowedWith` y
529 | x <- way, y <- way, x < y ]
531 -- Note ordering in these tests: the left argument is
532 -- <= the right argument, according to the Ord instance
535 -- debug is allowed with everything
536 _ `allowedWith` WayDebug = True
537 WayDebug `allowedWith` _ = True
539 WayProf `allowedWith` WayNDP = True
540 WayThreaded `allowedWith` WayProf = True
541 _ `allowedWith` _ = False
544 findBuildTag :: IO [String] -- new options
546 way_names <- readIORef v_Ways
547 let ws = sort (nub way_names)
549 if not (allowed_combination ws)
550 then throwDyn (CmdLineError $
551 "combination not supported: " ++
552 foldr1 (\a b -> a ++ '/':b)
553 (map (wayName . lkupWay) ws))
554 else let ways = map lkupWay ws
555 tag = mkBuildTag (filter (not.wayRTSOnly) ways)
556 rts_tag = mkBuildTag ways
557 flags = map wayOpts ways
559 writeIORef v_Build_tag tag
560 writeIORef v_RTS_Build_tag rts_tag
561 return (concat flags)
565 mkBuildTag :: [Way] -> String
566 mkBuildTag ways = concat (intersperse "_" (map wayTag ways))
568 lkupWay :: WayName -> Way
570 case lookup w way_details of
571 Nothing -> error "findBuildTag"
572 Just details -> details
574 isRTSWay :: WayName -> Bool
575 isRTSWay = wayRTSOnly . lkupWay
584 way_details :: [ (WayName, Way) ]
586 [ (WayThreaded, Way "thr" True "Threaded" [
587 #if defined(freebsd_TARGET_OS)
590 -- FreeBSD's default threading library is the KSE-based M:N libpthread,
591 -- which GHC has some problems with. It's currently not clear whether
592 -- the problems are our fault or theirs, but it seems that using the
593 -- alternative 1:1 threading library libthr works around it:
595 #elif defined(solaris2_TARGET_OS)
600 (WayDebug, Way "debug" True "Debug" [] ),
602 (WayProf, Way "p" False "Profiling"
605 , "-optc-DPROFILING" ]),
607 (WayTicky, Way "t" True "Ticky-ticky Profiling"
609 , "-optc-DTICKY_TICKY" ]),
611 -- optl's below to tell linker where to find the PVM library -- HWL
612 (WayPar, Way "mp" False "Parallel"
614 , "-D__PARALLEL_HASKELL__"
616 , "-package concurrent"
618 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
622 -- at the moment we only change the RTS and could share compiler and libs!
623 (WayPar, Way "mt" False "Parallel ticky profiling"
625 , "-D__PARALLEL_HASKELL__"
628 , "-package concurrent"
630 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
634 (WayPar, Way "md" False "Distributed"
636 , "-D__PARALLEL_HASKELL__"
637 , "-D__DISTRIBUTED_HASKELL__"
640 , "-package concurrent"
642 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
646 (WayGran, Way "mg" False "GranSim"
650 , "-package concurrent" ]),
652 (WayNDP, Way "ndp" False "Nested data parallelism"
656 (WayUser_a, Way "a" False "User way 'a'" ["$WAY_a_REAL_OPTS"]),
657 (WayUser_b, Way "b" False "User way 'b'" ["$WAY_b_REAL_OPTS"]),
658 (WayUser_c, Way "c" False "User way 'c'" ["$WAY_c_REAL_OPTS"]),
659 (WayUser_d, Way "d" False "User way 'd'" ["$WAY_d_REAL_OPTS"]),
660 (WayUser_e, Way "e" False "User way 'e'" ["$WAY_e_REAL_OPTS"]),
661 (WayUser_f, Way "f" False "User way 'f'" ["$WAY_f_REAL_OPTS"]),
662 (WayUser_g, Way "g" False "User way 'g'" ["$WAY_g_REAL_OPTS"]),
663 (WayUser_h, Way "h" False "User way 'h'" ["$WAY_h_REAL_OPTS"]),
664 (WayUser_i, Way "i" False "User way 'i'" ["$WAY_i_REAL_OPTS"]),
665 (WayUser_j, Way "j" False "User way 'j'" ["$WAY_j_REAL_OPTS"]),
666 (WayUser_k, Way "k" False "User way 'k'" ["$WAY_k_REAL_OPTS"]),
667 (WayUser_l, Way "l" False "User way 'l'" ["$WAY_l_REAL_OPTS"]),
668 (WayUser_m, Way "m" False "User way 'm'" ["$WAY_m_REAL_OPTS"]),
669 (WayUser_n, Way "n" False "User way 'n'" ["$WAY_n_REAL_OPTS"]),
670 (WayUser_o, Way "o" False "User way 'o'" ["$WAY_o_REAL_OPTS"]),
671 (WayUser_A, Way "A" False "User way 'A'" ["$WAY_A_REAL_OPTS"]),
672 (WayUser_B, Way "B" False "User way 'B'" ["$WAY_B_REAL_OPTS"])
675 unregFlags :: [String]
678 , "-optc-DUSE_MINIINTERPRETER"
679 , "-fno-asm-mangling"