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,
82 opt_UnboxStrictFields,
83 opt_SimplNoPreInlining,
84 opt_SimplDoEtaReduction,
85 opt_SimplDoLambdaEtaExpansion,
87 opt_SimplExcessPrecision,
91 opt_UF_CreationThreshold,
93 opt_UF_FunAppDiscount,
101 opt_EmitCExternDecls,
102 opt_EnsureSplittableC,
107 opt_IgnoreIfacePragmas,
110 opt_OmitInterfacePragmas,
118 #include "HsVersions.h"
120 import Constants -- Default values for some flags
122 import FastString ( FastString, mkFastString )
124 import Maybes ( firstJust )
127 import DATA_IOREF ( IORef, readIORef, writeIORef )
128 import UNSAFE_IO ( unsafePerformIO )
131 %************************************************************************
133 \subsection{Command-line options}
135 %************************************************************************
137 The hsc command-line options are split into two categories:
142 Static flags are represented by top-level values of type Bool or Int,
143 for example. They therefore have the same value throughout the
146 Dynamic flags are represented by an abstract type, DynFlags, which is
147 passed into hsc by the compilation manager for every compilation.
148 Dynamic flags are those that change on a per-compilation basis,
149 perhaps because they may be present in the OPTIONS pragma at the top
152 Other flag-related blurb:
154 A list of {\em ToDo}s is things to be done in a particular part of
155 processing. A (fictitious) example for the Core-to-Core simplifier
156 might be: run the simplifier, then run the strictness analyser, then
157 run the simplifier again (three ``todos'').
159 There are three ``to-do processing centers'' at the moment. In the
160 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
161 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
162 (\tr{simplStg/SimplStg.lhs}).
164 %************************************************************************
166 \subsection{Datatypes associated with command-line options}
168 %************************************************************************
171 data CoreToDo -- These are diff core-to-core passes,
172 -- which may be invoked in any order,
173 -- as many times as you like.
175 = CoreDoSimplify -- The core-to-core simplifier.
178 -- Each run of the simplifier can take a different
179 -- set of simplifier-specific flags.
181 | CoreDoFloatOutwards FloatOutSwitches
186 | CoreDoWorkerWrapper
190 | CoreDoOldStrictness
193 | CoreDoRuleCheck Int{-CompilerPhase-} String -- Check for non-application of rules
194 -- matching this string
196 | CoreDoNothing -- useful when building up lists of these things
201 = StgDoMassageForProfiling -- should be (next to) last
202 -- There's also setStgVarInfo, but its absolute "lastness"
203 -- is so critical that it is hardwired in (no flag).
208 data SimplifierMode -- See comments in SimplMonad
212 data SimplifierSwitch
213 = MaxSimplifierIterations Int
216 data FloatOutSwitches
217 = FloatOutSw Bool -- True <=> float lambdas to top level
218 Bool -- True <=> float constants to top level,
219 -- even if they do not escape a lambda
222 %************************************************************************
224 \subsection{Dynamic command-line options}
226 %************************************************************************
239 | Opt_D_dump_inlinings
240 | Opt_D_dump_occur_anal
245 | Opt_D_dump_simpl_iterations
255 | Opt_D_dump_worker_wrapper
256 | Opt_D_dump_rn_trace
257 | Opt_D_dump_rn_stats
259 | Opt_D_dump_simpl_stats
260 | Opt_D_dump_tc_trace
265 | Opt_D_verbose_core2core
266 | Opt_D_verbose_stg2stg
268 | Opt_D_dump_hi_diffs
269 | Opt_D_dump_minimal_imports
274 | Opt_WarnDuplicateExports
276 | Opt_WarnIncompletePatterns
277 | Opt_WarnMissingFields
278 | Opt_WarnMissingMethods
279 | Opt_WarnMissingSigs
280 | Opt_WarnNameShadowing
281 | Opt_WarnOverlappingPatterns
282 | Opt_WarnSimplePatterns
283 | Opt_WarnTypeDefaults
284 | Opt_WarnUnusedBinds
285 | Opt_WarnUnusedImports
286 | Opt_WarnUnusedMatches
287 | Opt_WarnDeprecations
291 | Opt_AllowOverlappingInstances
292 | Opt_AllowUndecidableInstances
293 | Opt_AllowIncoherentInstances
294 | Opt_NoMonomorphismRestriction
297 | Opt_PArr -- syntactic support for parallel arrays
298 | Opt_With -- deprecated keyword for implicit parms
300 | Opt_NoImplicitPrelude
304 data DynFlags = DynFlags {
305 coreToDo :: [CoreToDo],
306 stgToDo :: [StgToDo],
308 hscOutName :: String, -- name of the output file
309 hscStubHOutName :: String, -- name of the .stub_h output file
310 hscStubCOutName :: String, -- name of the .stub_c output file
311 extCoreName :: String, -- name of the .core output file
312 verbosity :: Int, -- verbosity level
313 cppFlag :: Bool, -- preprocess with cpp?
314 ppFlag :: Bool, -- preprocess with a Haskell Pp?
315 stolen_x86_regs :: Int,
316 cmdlineHcIncludes :: [String], -- -#includes
318 -- options for particular phases
344 | cGhcWithNativeCodeGen == "YES" &&
345 (prefixMatch "i386" cTARGETPLATFORM ||
346 prefixMatch "sparc" cTARGETPLATFORM) = HscAsm
349 defaultDynFlags = DynFlags {
350 coreToDo = [], stgToDo = [],
351 hscLang = defaultHscLang,
353 hscStubHOutName = "", hscStubCOutName = "",
359 cmdlineHcIncludes = [],
370 flags = [Opt_Generics] ++ standardWarnings,
371 -- Generating the helper-functions for
372 -- generics is now on by default
378 0 | print errors & warnings only
379 1 | minimal verbosity: print "compiling M ... done." for each module.
380 2 | equivalent to -dshow-passes
381 3 | equivalent to existing "ghc -v"
382 4 | "ghc -v -ddump-most"
383 5 | "ghc -v -ddump-all"
386 dopt :: DynFlag -> DynFlags -> Bool
387 dopt f dflags = f `elem` (flags dflags)
389 dopt_CoreToDo :: DynFlags -> [CoreToDo]
390 dopt_CoreToDo = coreToDo
392 dopt_StgToDo :: DynFlags -> [StgToDo]
393 dopt_StgToDo = stgToDo
395 dopt_OutName :: DynFlags -> String
396 dopt_OutName = hscOutName
398 dopt_HscLang :: DynFlags -> HscLang
399 dopt_HscLang = hscLang
401 dopt_set :: DynFlags -> DynFlag -> DynFlags
402 dopt_set dfs f = dfs{ flags = f : flags dfs }
404 dopt_unset :: DynFlags -> DynFlag -> DynFlags
405 dopt_unset dfs f = dfs{ flags = filter (/= f) (flags dfs) }
407 getOpts :: (DynFlags -> [a]) -> IO [a]
408 -- We add to the options from the front, so we need to reverse the list
409 getOpts opts = dynFlag opts >>= return . reverse
411 -- we can only switch between HscC, HscAsmm, and HscILX with dynamic flags
412 -- (-fvia-C, -fasm, -filx respectively).
413 setLang l = updDynFlags (\ dfs -> case hscLang dfs of
414 HscC -> dfs{ hscLang = l }
415 HscAsm -> dfs{ hscLang = l }
416 HscILX -> dfs{ hscLang = l }
420 verb <- dynFlag verbosity
421 if verb >= 3 then return "-v" else return ""
424 -----------------------------------------------------------------------------
425 -- Mess about with the mutable variables holding the dynamic arguments
428 -- is the "baseline" dynamic flags, initialised from
429 -- the defaults and command line options, and updated by the
430 -- ':s' command in GHCi.
433 -- is the dynamic flags for the current compilation. It is reset
434 -- to the value of v_InitDynFlags before each compilation, then
435 -- updated by reading any OPTIONS pragma in the current module.
438 GLOBAL_VAR(v_InitDynFlags, defaultDynFlags, DynFlags)
439 GLOBAL_VAR(v_DynFlags, defaultDynFlags, DynFlags)
441 setDynFlags :: DynFlags -> IO ()
442 setDynFlags dfs = writeIORef v_DynFlags dfs
444 saveDynFlags :: IO ()
445 saveDynFlags = do dfs <- readIORef v_DynFlags
446 writeIORef v_InitDynFlags dfs
448 restoreDynFlags :: IO DynFlags
449 restoreDynFlags = do dfs <- readIORef v_InitDynFlags
450 writeIORef v_DynFlags dfs
453 getDynFlags :: IO DynFlags
454 getDynFlags = readIORef v_DynFlags
456 updDynFlags :: (DynFlags -> DynFlags) -> IO ()
457 updDynFlags f = do dfs <- readIORef v_DynFlags
458 writeIORef v_DynFlags (f dfs)
460 dynFlag :: (DynFlags -> a) -> IO a
461 dynFlag f = do dflags <- readIORef v_DynFlags; return (f dflags)
463 setDynFlag, unSetDynFlag :: DynFlag -> IO ()
464 setDynFlag f = updDynFlags (\dfs -> dopt_set dfs f)
465 unSetDynFlag f = updDynFlags (\dfs -> dopt_unset dfs f)
469 %************************************************************************
471 \subsection{Warnings}
473 %************************************************************************
477 = [ Opt_WarnDeprecations,
478 Opt_WarnOverlappingPatterns,
479 Opt_WarnMissingFields,
480 Opt_WarnMissingMethods,
481 Opt_WarnDuplicateExports
485 = standardWarnings ++
486 [ Opt_WarnUnusedBinds,
487 Opt_WarnUnusedMatches,
488 Opt_WarnUnusedImports,
489 Opt_WarnIncompletePatterns,
495 [ Opt_WarnTypeDefaults,
496 Opt_WarnNameShadowing,
502 %************************************************************************
504 \subsection{Classifying command-line options}
506 %************************************************************************
509 -- v_Statis_hsc_opts is here to avoid a circular dependency with
511 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
513 lookUp :: FastString -> Bool
514 lookup_int :: String -> Maybe Int
515 lookup_def_int :: String -> Int -> Int
516 lookup_def_float :: String -> Float -> Float
517 lookup_str :: String -> Maybe String
519 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
520 packed_static_opts = map mkFastString unpacked_static_opts
522 lookUp sw = sw `elem` packed_static_opts
524 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
526 lookup_int sw = case (lookup_str sw) of
528 Just xx -> Just (read xx)
530 lookup_def_int sw def = case (lookup_str sw) of
531 Nothing -> def -- Use default
534 lookup_def_float sw def = case (lookup_str sw) of
535 Nothing -> def -- Use default
540 Putting the compiler options into temporary at-files
541 may turn out to be necessary later on if we turn hsc into
542 a pure Win32 application where I think there's a command-line
543 length limit of 255. unpacked_opts understands the @ option.
545 unpacked_opts :: [String]
549 map unpackFS argv -- NOT ARGV any more: v_Static_hsc_opts
551 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
556 %************************************************************************
558 \subsection{Static options}
560 %************************************************************************
564 opt_PprStyle_NoPrags = lookUp FSLIT("-dppr-noprags")
565 opt_PprStyle_Debug = lookUp FSLIT("-dppr-debug")
566 opt_PprStyle_RawTypes = lookUp FSLIT("-dppr-rawtypes")
567 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
570 opt_AutoSccsOnAllToplevs = lookUp FSLIT("-fauto-sccs-on-all-toplevs")
571 opt_AutoSccsOnExportedToplevs = lookUp FSLIT("-fauto-sccs-on-exported-toplevs")
572 opt_AutoSccsOnIndividualCafs = lookUp FSLIT("-fauto-sccs-on-individual-cafs")
573 opt_AutoSccsOnDicts = lookUp FSLIT("-fauto-sccs-on-dicts")
574 opt_SccProfilingOn = lookUp FSLIT("-fscc-profiling")
575 opt_DoTickyProfiling = lookUp FSLIT("-fticky-ticky")
578 opt_AllStrict = lookUp FSLIT("-fall-strict")
579 opt_DictsStrict = lookUp FSLIT("-fdicts-strict")
580 opt_IrrefutableTuples = lookUp FSLIT("-firrefutable-tuples")
581 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
582 opt_NumbersStrict = lookUp FSLIT("-fnumbers-strict")
583 opt_Parallel = lookUp FSLIT("-fparallel")
584 opt_SMP = lookUp FSLIT("-fsmp")
585 opt_Flatten = lookUp FSLIT("-fflatten")
588 opt_NoMethodSharing = lookUp FSLIT("-fno-method-sharing")
589 opt_DoSemiTagging = lookUp FSLIT("-fsemi-tagging")
590 opt_FoldrBuildOn = lookUp FSLIT("-ffoldr-build-on")
591 opt_CprOff = lookUp FSLIT("-fcpr-off")
592 -- Switch off CPR analysis in the new demand analyser
593 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
594 opt_StgDoLetNoEscapes = lookUp FSLIT("-flet-no-escape")
595 opt_UnfoldCasms = lookUp FSLIT("-funfold-casms-in-hi-file")
596 opt_UsageSPOn = lookUp FSLIT("-fusagesp-on")
597 opt_UnboxStrictFields = lookUp FSLIT("-funbox-strict-fields")
598 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
601 The optional '-inpackage=P' flag tells what package
602 we are compiling this module for.
603 The Prelude, for example is compiled with '-inpackage std'
605 opt_InPackage = case lookup_str "-inpackage=" of
606 Just p -> mkFastString p
607 Nothing -> FSLIT("Main") -- The package name if none is specified
609 opt_EmitCExternDecls = lookUp FSLIT("-femit-extern-decls")
610 opt_EnsureSplittableC = lookUp FSLIT("-fglobalise-toplev-names")
611 opt_GranMacros = lookUp FSLIT("-fgransim")
612 opt_HiVersion = read (cProjectVersionInt ++ cProjectPatchLevel) :: Int
613 opt_HistorySize = lookup_def_int "-fhistory-size" 20
614 opt_IgnoreAsserts = lookUp FSLIT("-fignore-asserts")
615 opt_IgnoreIfacePragmas = lookUp FSLIT("-fignore-interface-pragmas")
616 opt_NoHiCheck = lookUp FSLIT("-fno-hi-version-check")
617 opt_OmitBlackHoling = lookUp FSLIT("-dno-black-holing")
618 opt_OmitInterfacePragmas = lookUp FSLIT("-fomit-interface-pragmas")
619 opt_RuntimeTypes = lookUp FSLIT("-fruntime-types")
621 -- Simplifier switches
622 opt_SimplNoPreInlining = lookUp FSLIT("-fno-pre-inlining")
623 -- NoPreInlining is there just to see how bad things
624 -- get if you don't do it!
625 opt_SimplDoEtaReduction = lookUp FSLIT("-fdo-eta-reduction")
626 opt_SimplDoLambdaEtaExpansion = lookUp FSLIT("-fdo-lambda-eta-expansion")
627 opt_SimplCaseMerge = lookUp FSLIT("-fcase-merge")
628 opt_SimplExcessPrecision = lookUp FSLIT("-fexcess-precision")
631 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
632 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
633 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
634 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
635 opt_UF_UpdateInPlace = lookUp FSLIT("-funfolding-update-in-place")
637 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
638 opt_UF_DearOp = ( 4 :: Int)
640 opt_NoPruneDecls = lookUp FSLIT("-fno-prune-decls")
641 opt_NoPruneTyDecls = lookUp FSLIT("-fno-prune-tydecls")
642 opt_Static = lookUp FSLIT("-static")
643 opt_Unregisterised = lookUp FSLIT("-funregisterised")
644 opt_EmitExternalCore = lookUp FSLIT("-fext-core")
647 %************************************************************************
649 \subsection{List of static hsc flags}
651 %************************************************************************
656 "fauto-sccs-on-all-toplevs",
657 "fauto-sccs-on-exported-toplevs",
658 "fauto-sccs-on-individual-cafs",
659 "fauto-sccs-on-dicts",
664 "firrefutable-tuples",
672 "funfold-casms-in-hi-file",
674 "funbox-strict-fields",
675 "femit-extern-decls",
676 "fglobalise-toplev-names",
679 "fignore-interface-pragmas",
680 "fno-hi-version-check",
682 "fno-method-sharing",
683 "fomit-interface-pragmas",
687 "fdo-lambda-eta-expansion",
690 "funfolding-update-in-place",
699 || any (flip prefixMatch f) [
701 "fliberate-case-threshold",
704 "funfolding-creation-threshold",
705 "funfolding-use-threshold",
706 "funfolding-fun-discount",
707 "funfolding-keeness-factor"
711 %************************************************************************
713 \subsection{Misc functions for command-line options}
715 %************************************************************************
720 startsWith :: String -> String -> Maybe String
721 -- startsWith pfx (pfx++rest) = Just rest
723 startsWith [] str = Just str
724 startsWith (c:cs) (s:ss)
725 = if c /= s then Nothing else startsWith cs ss
726 startsWith _ [] = Nothing
728 endsWith :: String -> String -> Maybe String
730 = case (startsWith (reverse cs) (reverse ss)) of
732 Just rs -> Just (reverse rs)