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 dopt, -- DynFlag -> DynFlags -> Bool
24 dopt_set, dopt_unset, -- DynFlags -> DynFlag -> DynFlags
25 dopt_CoreToDo, -- DynFlags -> [CoreToDo]
26 dopt_StgToDo, -- DynFlags -> [StgToDo]
27 dopt_HscLang, -- DynFlags -> HscLang
28 dopt_OutName, -- DynFlags -> String
29 getOpts, -- (DynFlags -> [a]) -> IO [a]
33 -- Manipulating the DynFlags state
34 getDynFlags, -- IO DynFlags
35 setDynFlags, -- DynFlags -> IO ()
36 updDynFlags, -- (DynFlags -> DynFlags) -> IO ()
37 dynFlag, -- (DynFlags -> a) -> IO a
38 setDynFlag, unSetDynFlag, -- DynFlag -> IO ()
39 saveDynFlags, -- IO ()
40 restoreDynFlags, -- IO DynFlags
42 -- sets of warning opts
47 -- Output style options
49 opt_PprStyle_RawTypes,
54 opt_AutoSccsOnAllToplevs,
55 opt_AutoSccsOnExportedToplevs,
56 opt_AutoSccsOnIndividualCafs,
64 opt_MaxContextReductionDepth,
65 opt_IrrefutableTuples,
69 opt_NoMonomorphismRestriction,
76 opt_LiberateCaseThreshold,
77 opt_StgDoLetNoEscapes,
80 opt_UnboxStrictFields,
81 opt_SimplNoPreInlining,
82 opt_SimplDoEtaReduction,
83 opt_SimplDoLambdaEtaExpansion,
85 opt_SimplExcessPrecision,
89 opt_UF_CreationThreshold,
91 opt_UF_FunAppDiscount,
100 opt_EnsureSplittableC,
105 opt_IgnoreIfacePragmas,
108 opt_OmitInterfacePragmas,
116 #include "HsVersions.h"
119 import IOExts ( IORef, readIORef, writeIORef )
120 import Constants -- Default values for some flags
125 import Maybes ( firstJust )
128 %************************************************************************
130 \subsection{Command-line options}
132 %************************************************************************
134 The hsc command-line options are split into two categories:
139 Static flags are represented by top-level values of type Bool or Int,
140 for example. They therefore have the same value throughout the
143 Dynamic flags are represented by an abstract type, DynFlags, which is
144 passed into hsc by the compilation manager for every compilation.
145 Dynamic flags are those that change on a per-compilation basis,
146 perhaps because they may be present in the OPTIONS pragma at the top
149 Other flag-related blurb:
151 A list of {\em ToDo}s is things to be done in a particular part of
152 processing. A (fictitious) example for the Core-to-Core simplifier
153 might be: run the simplifier, then run the strictness analyser, then
154 run the simplifier again (three ``todos'').
156 There are three ``to-do processing centers'' at the moment. In the
157 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
158 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
159 (\tr{simplStg/SimplStg.lhs}).
161 %************************************************************************
163 \subsection{Datatypes associated with command-line options}
165 %************************************************************************
168 data CoreToDo -- These are diff core-to-core passes,
169 -- which may be invoked in any order,
170 -- as many times as you like.
172 = CoreDoSimplify -- The core-to-core simplifier.
175 -- Each run of the simplifier can take a different
176 -- set of simplifier-specific flags.
178 | CoreDoFloatOutwards FloatOutSwitches
183 | CoreDoWorkerWrapper
190 | CoreDoRuleCheck Int{-CompilerPhase-} String -- Check for non-application of rules
191 -- matching this string
193 | CoreDoNothing -- useful when building up lists of these things
198 = StgDoMassageForProfiling -- should be (next to) last
199 -- There's also setStgVarInfo, but its absolute "lastness"
200 -- is so critical that it is hardwired in (no flag).
205 data SimplifierMode -- See comments in SimplMonad
209 data SimplifierSwitch
210 = MaxSimplifierIterations Int
213 data FloatOutSwitches
214 = FloatOutSw Bool -- True <=> float lambdas to top level
215 Bool -- True <=> float constants to top level,
216 -- even if they do not escape a lambda
219 %************************************************************************
221 \subsection{Dynamic command-line options}
223 %************************************************************************
236 | Opt_D_dump_inlinings
237 | Opt_D_dump_occur_anal
242 | 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
260 | Opt_D_verbose_core2core
261 | Opt_D_verbose_stg2stg
263 | Opt_D_dump_hi_diffs
264 | Opt_D_dump_minimal_imports
269 | Opt_WarnDuplicateExports
271 | Opt_WarnIncompletePatterns
272 | Opt_WarnMissingFields
273 | Opt_WarnMissingMethods
274 | Opt_WarnMissingSigs
275 | Opt_WarnNameShadowing
276 | Opt_WarnOverlappingPatterns
277 | Opt_WarnSimplePatterns
278 | Opt_WarnTypeDefaults
279 | Opt_WarnUnusedBinds
280 | Opt_WarnUnusedImports
281 | Opt_WarnUnusedMatches
282 | Opt_WarnDeprecations
286 | Opt_AllowOverlappingInstances
287 | Opt_AllowUndecidableInstances
288 | Opt_AllowIncoherentInstances
291 | Opt_NoImplicitPrelude
295 data DynFlags = DynFlags {
296 coreToDo :: [CoreToDo],
297 stgToDo :: [StgToDo],
299 hscOutName :: String, -- name of the output file
300 hscStubHOutName :: String, -- name of the .stub_h output file
301 hscStubCOutName :: String, -- name of the .stub_c output file
302 extCoreName :: String, -- name of the .core output file
303 verbosity :: Int, -- verbosity level
304 cppFlag :: Bool, -- preprocess with cpp?
305 ppFlag :: Bool, -- preprocess with a Haskell Pp?
306 stolen_x86_regs :: Int,
307 cmdlineHcIncludes :: [String], -- -#includes
309 -- options for particular phases
334 defaultDynFlags = DynFlags {
335 coreToDo = [], stgToDo = [],
338 hscStubHOutName = "", hscStubCOutName = "",
344 cmdlineHcIncludes = [],
355 flags = standardWarnings,
361 0 | print errors & warnings only
362 1 | minimal verbosity: print "compiling M ... done." for each module.
363 2 | equivalent to -dshow-passes
364 3 | equivalent to existing "ghc -v"
365 4 | "ghc -v -ddump-most"
366 5 | "ghc -v -ddump-all"
369 dopt :: DynFlag -> DynFlags -> Bool
370 dopt f dflags = f `elem` (flags dflags)
372 dopt_CoreToDo :: DynFlags -> [CoreToDo]
373 dopt_CoreToDo = coreToDo
375 dopt_StgToDo :: DynFlags -> [StgToDo]
376 dopt_StgToDo = stgToDo
378 dopt_OutName :: DynFlags -> String
379 dopt_OutName = hscOutName
381 dopt_HscLang :: DynFlags -> HscLang
382 dopt_HscLang = hscLang
384 dopt_set :: DynFlags -> DynFlag -> DynFlags
385 dopt_set dfs f = dfs{ flags = f : flags dfs }
387 dopt_unset :: DynFlags -> DynFlag -> DynFlags
388 dopt_unset dfs f = dfs{ flags = filter (/= f) (flags dfs) }
390 getOpts :: (DynFlags -> [a]) -> IO [a]
391 -- We add to the options from the front, so we need to reverse the list
392 getOpts opts = dynFlag opts >>= return . reverse
394 -- we can only switch between HscC, HscAsmm, and HscILX with dynamic flags
395 -- (-fvia-C, -fasm, -filx respectively).
396 setLang l = updDynFlags (\ dfs -> case hscLang dfs of
397 HscC -> dfs{ hscLang = l }
398 HscAsm -> dfs{ hscLang = l }
399 HscILX -> dfs{ hscLang = l }
403 verb <- dynFlag verbosity
404 if verb >= 3 then return "-v" else return ""
407 -----------------------------------------------------------------------------
408 -- Mess about with the mutable variables holding the dynamic arguments
411 -- is the "baseline" dynamic flags, initialised from
412 -- the defaults and command line options, and updated by the
413 -- ':s' command in GHCi.
416 -- is the dynamic flags for the current compilation. It is reset
417 -- to the value of v_InitDynFlags before each compilation, then
418 -- updated by reading any OPTIONS pragma in the current module.
421 GLOBAL_VAR(v_InitDynFlags, defaultDynFlags, DynFlags)
422 GLOBAL_VAR(v_DynFlags, defaultDynFlags, DynFlags)
424 setDynFlags :: DynFlags -> IO ()
425 setDynFlags dfs = writeIORef v_DynFlags dfs
427 saveDynFlags :: IO ()
428 saveDynFlags = do dfs <- readIORef v_DynFlags
429 writeIORef v_InitDynFlags dfs
431 restoreDynFlags :: IO DynFlags
432 restoreDynFlags = do dfs <- readIORef v_InitDynFlags
433 writeIORef v_DynFlags dfs
436 getDynFlags :: IO DynFlags
437 getDynFlags = readIORef v_DynFlags
439 updDynFlags :: (DynFlags -> DynFlags) -> IO ()
440 updDynFlags f = do dfs <- readIORef v_DynFlags
441 writeIORef v_DynFlags (f dfs)
443 dynFlag :: (DynFlags -> a) -> IO a
444 dynFlag f = do dflags <- readIORef v_DynFlags; return (f dflags)
446 setDynFlag, unSetDynFlag :: DynFlag -> IO ()
447 setDynFlag f = updDynFlags (\dfs -> dopt_set dfs f)
448 unSetDynFlag f = updDynFlags (\dfs -> dopt_unset dfs f)
452 %************************************************************************
454 \subsection{Warnings}
456 %************************************************************************
460 = [ Opt_WarnDeprecations,
461 Opt_WarnOverlappingPatterns,
462 Opt_WarnMissingFields,
463 Opt_WarnMissingMethods,
464 Opt_WarnDuplicateExports
468 = standardWarnings ++
469 [ Opt_WarnUnusedBinds,
470 Opt_WarnUnusedMatches,
471 Opt_WarnUnusedImports,
472 Opt_WarnIncompletePatterns,
478 [ Opt_WarnTypeDefaults,
479 Opt_WarnNameShadowing,
485 %************************************************************************
487 \subsection{Classifying command-line options}
489 %************************************************************************
492 -- v_Statis_hsc_opts is here to avoid a circular dependency with
494 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
496 lookUp :: FAST_STRING -> Bool
497 lookup_int :: String -> Maybe Int
498 lookup_def_int :: String -> Int -> Int
499 lookup_def_float :: String -> Float -> Float
500 lookup_str :: String -> Maybe String
502 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
503 packed_static_opts = map _PK_ unpacked_static_opts
505 lookUp sw = sw `elem` packed_static_opts
507 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
509 lookup_int sw = case (lookup_str sw) of
511 Just xx -> Just (read xx)
513 lookup_def_int sw def = case (lookup_str sw) of
514 Nothing -> def -- Use default
517 lookup_def_float sw def = case (lookup_str sw) of
518 Nothing -> def -- Use default
523 Putting the compiler options into temporary at-files
524 may turn out to be necessary later on if we turn hsc into
525 a pure Win32 application where I think there's a command-line
526 length limit of 255. unpacked_opts understands the @ option.
528 unpacked_opts :: [String]
532 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
534 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
539 %************************************************************************
541 \subsection{Static options}
543 %************************************************************************
547 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
548 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
549 opt_PprStyle_RawTypes = lookUp SLIT("-dppr-rawtypes")
550 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
553 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
554 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
555 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
556 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
557 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
558 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
561 opt_AllStrict = lookUp SLIT("-fall-strict")
562 opt_NoMonomorphismRestriction = lookUp SLIT("-fno-monomorphism-restriction")
563 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
564 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
565 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
566 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
567 opt_Parallel = lookUp SLIT("-fparallel")
568 opt_SMP = lookUp SLIT("-fsmp")
571 opt_NoMethodSharing = lookUp SLIT("-fno-method-sharing")
572 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
573 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
574 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
575 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
576 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
577 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
578 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
579 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
582 The optional '-inpackage=P' flag tells what package
583 we are compiling this module for.
584 The Prelude, for example is compiled with '-inpackage std'
586 opt_InPackage = case lookup_str "-inpackage=" of
588 Nothing -> SLIT("Main") -- The package name if none is specified
590 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
591 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
592 opt_GranMacros = lookUp SLIT("-fgransim")
593 opt_HiVersion = read cProjectVersionInt :: Int
594 opt_HistorySize = lookup_def_int "-fhistory-size" 20
595 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
596 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
597 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
598 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
599 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
600 opt_RuntimeTypes = lookUp SLIT("-fruntime-types")
602 -- Simplifier switches
603 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
604 -- NoPreInlining is there just to see how bad things
605 -- get if you don't do it!
606 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
607 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
608 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
609 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
612 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
613 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
614 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
615 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
616 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
618 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
619 opt_UF_DearOp = ( 4 :: Int)
621 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
622 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
623 opt_Static = lookUp SLIT("-static")
624 opt_Unregisterised = lookUp SLIT("-funregisterised")
625 opt_EmitExternalCore = lookUp SLIT("-fext-core")
628 %************************************************************************
630 \subsection{List of static hsc flags}
632 %************************************************************************
637 "fauto-sccs-on-all-toplevs",
638 "fauto-sccs-on-exported-toplevs",
639 "fauto-sccs-on-individual-cafs",
640 "fauto-sccs-on-dicts",
645 "firrefutable-tuples",
652 "funfold-casms-in-hi-file",
654 "funbox-strict-fields",
655 "femit-extern-decls",
656 "fglobalise-toplev-names",
659 "fignore-interface-pragmas",
660 "fno-hi-version-check",
662 "fno-method-sharing",
663 "fno-monomorphism-restriction",
664 "fomit-interface-pragmas",
668 "fdo-lambda-eta-expansion",
671 "funfolding-update-in-place",
679 || any (flip prefixMatch f) [
681 "fliberate-case-threshold",
684 "funfolding-creation-threshold",
685 "funfolding-use-threshold",
686 "funfolding-fun-discount",
687 "funfolding-keeness-factor"
691 %************************************************************************
693 \subsection{Misc functions for command-line options}
695 %************************************************************************
700 startsWith :: String -> String -> Maybe String
701 -- startsWith pfx (pfx++rest) = Just rest
703 startsWith [] str = Just str
704 startsWith (c:cs) (s:ss)
705 = if c /= s then Nothing else startsWith cs ss
706 startsWith _ [] = Nothing
708 endsWith :: String -> String -> Maybe String
710 = case (startsWith (reverse cs) (reverse ss)) of
712 Just rs -> Just (reverse rs)