2 % (c) The University of Glasgow, 1996-2000
4 \section[CmdLineOpts]{Things to do with command-line options}
9 CoreToDo(..), StgToDo(..),
11 SimplifierMode(..), FloatOutSwitches(..),
14 DynFlag(..), -- needed non-abstractly by DriverFlags
21 -- Manipulating DynFlags
22 defaultDynFlags, -- DynFlags
23 defaultHscLang, -- HscLang
24 dopt, -- DynFlag -> DynFlags -> Bool
25 dopt_set, dopt_unset, -- DynFlags -> DynFlag -> DynFlags
26 dopt_CoreToDo, -- DynFlags -> [CoreToDo]
27 dopt_StgToDo, -- DynFlags -> [StgToDo]
28 dopt_HscLang, -- DynFlags -> HscLang
29 dopt_OutName, -- DynFlags -> String
30 getOpts, -- (DynFlags -> [a]) -> IO [a]
34 -- Manipulating the DynFlags state
35 getDynFlags, -- IO DynFlags
36 setDynFlags, -- DynFlags -> IO ()
37 updDynFlags, -- (DynFlags -> DynFlags) -> IO ()
38 dynFlag, -- (DynFlags -> a) -> IO a
39 setDynFlag, unSetDynFlag, -- DynFlag -> IO ()
40 saveDynFlags, -- IO ()
41 restoreDynFlags, -- IO DynFlags
43 -- sets of warning opts
48 -- Output style options
50 opt_PprStyle_RawTypes,
55 opt_AutoSccsOnAllToplevs,
56 opt_AutoSccsOnExportedToplevs,
57 opt_AutoSccsOnIndividualCafs,
65 opt_MaxContextReductionDepth,
66 opt_IrrefutableTuples,
77 opt_LiberateCaseThreshold,
78 opt_StgDoLetNoEscapes,
81 opt_UnboxStrictFields,
82 opt_SimplNoPreInlining,
83 opt_SimplDoEtaReduction,
84 opt_SimplDoLambdaEtaExpansion,
86 opt_SimplExcessPrecision,
90 opt_UF_CreationThreshold,
92 opt_UF_FunAppDiscount,
100 opt_EmitCExternDecls,
101 opt_EnsureSplittableC,
106 opt_IgnoreIfacePragmas,
109 opt_OmitInterfacePragmas,
117 #include "HsVersions.h"
119 import Constants -- Default values for some flags
121 import FastString ( FastString, mkFastString )
123 import Maybes ( firstJust )
126 import DATA_IOREF ( IORef, readIORef, writeIORef )
127 import UNSAFE_IO ( unsafePerformIO )
130 %************************************************************************
132 \subsection{Command-line options}
134 %************************************************************************
136 The hsc command-line options are split into two categories:
141 Static flags are represented by top-level values of type Bool or Int,
142 for example. They therefore have the same value throughout the
145 Dynamic flags are represented by an abstract type, DynFlags, which is
146 passed into hsc by the compilation manager for every compilation.
147 Dynamic flags are those that change on a per-compilation basis,
148 perhaps because they may be present in the OPTIONS pragma at the top
151 Other flag-related blurb:
153 A list of {\em ToDo}s is things to be done in a particular part of
154 processing. A (fictitious) example for the Core-to-Core simplifier
155 might be: run the simplifier, then run the strictness analyser, then
156 run the simplifier again (three ``todos'').
158 There are three ``to-do processing centers'' at the moment. In the
159 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
160 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
161 (\tr{simplStg/SimplStg.lhs}).
163 %************************************************************************
165 \subsection{Datatypes associated with command-line options}
167 %************************************************************************
170 data CoreToDo -- These are diff core-to-core passes,
171 -- which may be invoked in any order,
172 -- as many times as you like.
174 = CoreDoSimplify -- The core-to-core simplifier.
177 -- Each run of the simplifier can take a different
178 -- set of simplifier-specific flags.
180 | CoreDoFloatOutwards FloatOutSwitches
185 | CoreDoWorkerWrapper
188 | CoreDoOldStrictness
191 | CoreDoRuleCheck Int{-CompilerPhase-} String -- Check for non-application of rules
192 -- matching this string
194 | CoreDoNothing -- useful when building up lists of these things
199 = StgDoMassageForProfiling -- should be (next to) last
200 -- There's also setStgVarInfo, but its absolute "lastness"
201 -- is so critical that it is hardwired in (no flag).
206 data SimplifierMode -- See comments in SimplMonad
210 data SimplifierSwitch
211 = MaxSimplifierIterations Int
214 data FloatOutSwitches
215 = FloatOutSw Bool -- True <=> float lambdas to top level
216 Bool -- True <=> float constants to top level,
217 -- even if they do not escape a lambda
220 %************************************************************************
222 \subsection{Dynamic command-line options}
224 %************************************************************************
237 | Opt_D_dump_inlinings
238 | Opt_D_dump_occur_anal
243 | Opt_D_dump_simpl_iterations
252 | Opt_D_dump_worker_wrapper
253 | Opt_D_dump_rn_trace
254 | Opt_D_dump_rn_stats
256 | Opt_D_dump_simpl_stats
257 | Opt_D_dump_tc_trace
262 | Opt_D_verbose_core2core
263 | Opt_D_verbose_stg2stg
265 | Opt_D_dump_hi_diffs
266 | Opt_D_dump_minimal_imports
270 | Opt_WarnIsError -- -Werror; makes warnings fatal
271 | Opt_WarnDuplicateExports
273 | Opt_WarnIncompletePatterns
274 | Opt_WarnMissingFields
275 | Opt_WarnMissingMethods
276 | Opt_WarnMissingSigs
277 | Opt_WarnNameShadowing
278 | Opt_WarnOverlappingPatterns
279 | Opt_WarnSimplePatterns
280 | Opt_WarnTypeDefaults
281 | Opt_WarnUnusedBinds
282 | Opt_WarnUnusedImports
283 | Opt_WarnUnusedMatches
284 | Opt_WarnDeprecations
288 | Opt_AllowOverlappingInstances
289 | Opt_AllowUndecidableInstances
290 | Opt_AllowIncoherentInstances
291 | Opt_NoMonomorphismRestriction
294 | Opt_PArr -- syntactic support for parallel arrays
295 | Opt_With -- deprecated keyword for implicit parms
297 | Opt_NoImplicitPrelude
301 data DynFlags = DynFlags {
302 coreToDo :: [CoreToDo],
303 stgToDo :: [StgToDo],
305 hscOutName :: String, -- name of the output file
306 hscStubHOutName :: String, -- name of the .stub_h output file
307 hscStubCOutName :: String, -- name of the .stub_c output file
308 extCoreName :: String, -- name of the .core output file
309 verbosity :: Int, -- verbosity level
310 cppFlag :: Bool, -- preprocess with cpp?
311 ppFlag :: Bool, -- preprocess with a Haskell Pp?
312 stolen_x86_regs :: Int,
313 cmdlineHcIncludes :: [String], -- -#includes
315 -- options for particular phases
341 | cGhcWithNativeCodeGen == "YES" &&
342 (prefixMatch "i386" cTARGETPLATFORM ||
343 prefixMatch "sparc" cTARGETPLATFORM) = HscAsm
346 defaultDynFlags = DynFlags {
347 coreToDo = [], stgToDo = [],
348 hscLang = defaultHscLang,
350 hscStubHOutName = "", hscStubCOutName = "",
356 cmdlineHcIncludes = [],
367 flags = [Opt_Generics] ++ standardWarnings,
368 -- Generating the helper-functions for
369 -- generics is now on by default
375 0 | print errors & warnings only
376 1 | minimal verbosity: print "compiling M ... done." for each module.
377 2 | equivalent to -dshow-passes
378 3 | equivalent to existing "ghc -v"
379 4 | "ghc -v -ddump-most"
380 5 | "ghc -v -ddump-all"
383 dopt :: DynFlag -> DynFlags -> Bool
384 dopt f dflags = f `elem` (flags dflags)
386 dopt_CoreToDo :: DynFlags -> [CoreToDo]
387 dopt_CoreToDo = coreToDo
389 dopt_StgToDo :: DynFlags -> [StgToDo]
390 dopt_StgToDo = stgToDo
392 dopt_OutName :: DynFlags -> String
393 dopt_OutName = hscOutName
395 dopt_HscLang :: DynFlags -> HscLang
396 dopt_HscLang = hscLang
398 dopt_set :: DynFlags -> DynFlag -> DynFlags
399 dopt_set dfs f = dfs{ flags = f : flags dfs }
401 dopt_unset :: DynFlags -> DynFlag -> DynFlags
402 dopt_unset dfs f = dfs{ flags = filter (/= f) (flags dfs) }
404 getOpts :: (DynFlags -> [a]) -> IO [a]
405 -- We add to the options from the front, so we need to reverse the list
406 getOpts opts = dynFlag opts >>= return . reverse
408 -- we can only switch between HscC, HscAsmm, and HscILX with dynamic flags
409 -- (-fvia-C, -fasm, -filx respectively).
410 setLang l = updDynFlags (\ dfs -> case hscLang dfs of
411 HscC -> dfs{ hscLang = l }
412 HscAsm -> dfs{ hscLang = l }
413 HscILX -> dfs{ hscLang = l }
417 verb <- dynFlag verbosity
418 if verb >= 3 then return "-v" else return ""
421 -----------------------------------------------------------------------------
422 -- Mess about with the mutable variables holding the dynamic arguments
425 -- is the "baseline" dynamic flags, initialised from
426 -- the defaults and command line options, and updated by the
427 -- ':s' command in GHCi.
430 -- is the dynamic flags for the current compilation. It is reset
431 -- to the value of v_InitDynFlags before each compilation, then
432 -- updated by reading any OPTIONS pragma in the current module.
435 GLOBAL_VAR(v_InitDynFlags, defaultDynFlags, DynFlags)
436 GLOBAL_VAR(v_DynFlags, defaultDynFlags, DynFlags)
438 setDynFlags :: DynFlags -> IO ()
439 setDynFlags dfs = writeIORef v_DynFlags dfs
441 saveDynFlags :: IO ()
442 saveDynFlags = do dfs <- readIORef v_DynFlags
443 writeIORef v_InitDynFlags dfs
445 restoreDynFlags :: IO DynFlags
446 restoreDynFlags = do dfs <- readIORef v_InitDynFlags
447 writeIORef v_DynFlags dfs
450 getDynFlags :: IO DynFlags
451 getDynFlags = readIORef v_DynFlags
453 updDynFlags :: (DynFlags -> DynFlags) -> IO ()
454 updDynFlags f = do dfs <- readIORef v_DynFlags
455 writeIORef v_DynFlags (f dfs)
457 dynFlag :: (DynFlags -> a) -> IO a
458 dynFlag f = do dflags <- readIORef v_DynFlags; return (f dflags)
460 setDynFlag, unSetDynFlag :: DynFlag -> IO ()
461 setDynFlag f = updDynFlags (\dfs -> dopt_set dfs f)
462 unSetDynFlag f = updDynFlags (\dfs -> dopt_unset dfs f)
466 %************************************************************************
468 \subsection{Warnings}
470 %************************************************************************
474 = [ Opt_WarnDeprecations,
475 Opt_WarnOverlappingPatterns,
476 Opt_WarnMissingFields,
477 Opt_WarnMissingMethods,
478 Opt_WarnDuplicateExports
482 = standardWarnings ++
483 [ Opt_WarnUnusedBinds,
484 Opt_WarnUnusedMatches,
485 Opt_WarnUnusedImports,
486 Opt_WarnIncompletePatterns,
492 [ Opt_WarnTypeDefaults,
493 Opt_WarnNameShadowing,
499 %************************************************************************
501 \subsection{Classifying command-line options}
503 %************************************************************************
506 -- v_Statis_hsc_opts is here to avoid a circular dependency with
508 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
510 lookUp :: FastString -> Bool
511 lookup_int :: String -> Maybe Int
512 lookup_def_int :: String -> Int -> Int
513 lookup_def_float :: String -> Float -> Float
514 lookup_str :: String -> Maybe String
516 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
517 packed_static_opts = map mkFastString unpacked_static_opts
519 lookUp sw = sw `elem` packed_static_opts
521 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
523 lookup_int sw = case (lookup_str sw) of
525 Just xx -> Just (read xx)
527 lookup_def_int sw def = case (lookup_str sw) of
528 Nothing -> def -- Use default
531 lookup_def_float sw def = case (lookup_str sw) of
532 Nothing -> def -- Use default
537 Putting the compiler options into temporary at-files
538 may turn out to be necessary later on if we turn hsc into
539 a pure Win32 application where I think there's a command-line
540 length limit of 255. unpacked_opts understands the @ option.
542 unpacked_opts :: [String]
546 map unpackFS argv -- NOT ARGV any more: v_Static_hsc_opts
548 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
553 %************************************************************************
555 \subsection{Static options}
557 %************************************************************************
561 opt_PprStyle_NoPrags = lookUp FSLIT("-dppr-noprags")
562 opt_PprStyle_Debug = lookUp FSLIT("-dppr-debug")
563 opt_PprStyle_RawTypes = lookUp FSLIT("-dppr-rawtypes")
564 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
567 opt_AutoSccsOnAllToplevs = lookUp FSLIT("-fauto-sccs-on-all-toplevs")
568 opt_AutoSccsOnExportedToplevs = lookUp FSLIT("-fauto-sccs-on-exported-toplevs")
569 opt_AutoSccsOnIndividualCafs = lookUp FSLIT("-fauto-sccs-on-individual-cafs")
570 opt_AutoSccsOnDicts = lookUp FSLIT("-fauto-sccs-on-dicts")
571 opt_SccProfilingOn = lookUp FSLIT("-fscc-profiling")
572 opt_DoTickyProfiling = lookUp FSLIT("-fticky-ticky")
575 opt_AllStrict = lookUp FSLIT("-fall-strict")
576 opt_DictsStrict = lookUp FSLIT("-fdicts-strict")
577 opt_IrrefutableTuples = lookUp FSLIT("-firrefutable-tuples")
578 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
579 opt_NumbersStrict = lookUp FSLIT("-fnumbers-strict")
580 opt_Parallel = lookUp FSLIT("-fparallel")
581 opt_SMP = lookUp FSLIT("-fsmp")
582 opt_Flatten = lookUp FSLIT("-fflatten")
585 opt_NoMethodSharing = lookUp FSLIT("-fno-method-sharing")
586 opt_DoSemiTagging = lookUp FSLIT("-fsemi-tagging")
587 opt_FoldrBuildOn = lookUp FSLIT("-ffoldr-build-on")
588 opt_CprOff = lookUp FSLIT("-fcpr-off")
589 -- Switch off CPR analysis in the new demand analyser
590 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
591 opt_StgDoLetNoEscapes = lookUp FSLIT("-flet-no-escape")
592 opt_UnfoldCasms = lookUp FSLIT("-funfold-casms-in-hi-file")
593 opt_UnboxStrictFields = lookUp FSLIT("-funbox-strict-fields")
594 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
597 The optional '-inpackage=P' flag tells what package
598 we are compiling this module for.
599 The Prelude, for example is compiled with '-inpackage std'
601 opt_InPackage = case lookup_str "-inpackage=" of
602 Just p -> mkFastString p
603 Nothing -> FSLIT("Main") -- The package name if none is specified
605 opt_EmitCExternDecls = lookUp FSLIT("-femit-extern-decls")
606 opt_EnsureSplittableC = lookUp FSLIT("-fglobalise-toplev-names")
607 opt_GranMacros = lookUp FSLIT("-fgransim")
608 opt_HiVersion = read (cProjectVersionInt ++ cProjectPatchLevel) :: Int
609 opt_HistorySize = lookup_def_int "-fhistory-size" 20
610 opt_IgnoreAsserts = lookUp FSLIT("-fignore-asserts")
611 opt_IgnoreIfacePragmas = lookUp FSLIT("-fignore-interface-pragmas")
612 opt_NoHiCheck = lookUp FSLIT("-fno-hi-version-check")
613 opt_OmitBlackHoling = lookUp FSLIT("-dno-black-holing")
614 opt_OmitInterfacePragmas = lookUp FSLIT("-fomit-interface-pragmas")
615 opt_RuntimeTypes = lookUp FSLIT("-fruntime-types")
617 -- Simplifier switches
618 opt_SimplNoPreInlining = lookUp FSLIT("-fno-pre-inlining")
619 -- NoPreInlining is there just to see how bad things
620 -- get if you don't do it!
621 opt_SimplDoEtaReduction = lookUp FSLIT("-fdo-eta-reduction")
622 opt_SimplDoLambdaEtaExpansion = lookUp FSLIT("-fdo-lambda-eta-expansion")
623 opt_SimplCaseMerge = lookUp FSLIT("-fcase-merge")
624 opt_SimplExcessPrecision = lookUp FSLIT("-fexcess-precision")
627 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
628 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
629 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
630 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
631 opt_UF_UpdateInPlace = lookUp FSLIT("-funfolding-update-in-place")
633 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
634 opt_UF_DearOp = ( 4 :: Int)
636 opt_NoPruneDecls = lookUp FSLIT("-fno-prune-decls")
637 opt_NoPruneTyDecls = lookUp FSLIT("-fno-prune-tydecls")
638 opt_Static = lookUp FSLIT("-static")
639 opt_Unregisterised = lookUp FSLIT("-funregisterised")
640 opt_EmitExternalCore = lookUp FSLIT("-fext-core")
643 %************************************************************************
645 \subsection{List of static hsc flags}
647 %************************************************************************
652 "fauto-sccs-on-all-toplevs",
653 "fauto-sccs-on-exported-toplevs",
654 "fauto-sccs-on-individual-cafs",
655 "fauto-sccs-on-dicts",
660 "firrefutable-tuples",
668 "funfold-casms-in-hi-file",
669 "funbox-strict-fields",
670 "femit-extern-decls",
671 "fglobalise-toplev-names",
674 "fignore-interface-pragmas",
675 "fno-hi-version-check",
677 "fno-method-sharing",
678 "fomit-interface-pragmas",
682 "fdo-lambda-eta-expansion",
685 "funfolding-update-in-place",
694 || any (flip prefixMatch f) [
696 "fliberate-case-threshold",
699 "funfolding-creation-threshold",
700 "funfolding-use-threshold",
701 "funfolding-fun-discount",
702 "funfolding-keeness-factor"
706 %************************************************************************
708 \subsection{Misc functions for command-line options}
710 %************************************************************************
715 startsWith :: String -> String -> Maybe String
716 -- startsWith pfx (pfx++rest) = Just rest
718 startsWith [] str = Just str
719 startsWith (c:cs) (s:ss)
720 = if c /= s then Nothing else startsWith cs ss
721 startsWith _ [] = Nothing
723 endsWith :: String -> String -> Maybe String
725 = case (startsWith (reverse cs) (reverse ss)) of
727 Just rs -> Just (reverse rs)