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,
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
123 import FastString ( FastString, mkFastString )
126 import Maybes ( firstJust )
129 %************************************************************************
131 \subsection{Command-line options}
133 %************************************************************************
135 The hsc command-line options are split into two categories:
140 Static flags are represented by top-level values of type Bool or Int,
141 for example. They therefore have the same value throughout the
144 Dynamic flags are represented by an abstract type, DynFlags, which is
145 passed into hsc by the compilation manager for every compilation.
146 Dynamic flags are those that change on a per-compilation basis,
147 perhaps because they may be present in the OPTIONS pragma at the top
150 Other flag-related blurb:
152 A list of {\em ToDo}s is things to be done in a particular part of
153 processing. A (fictitious) example for the Core-to-Core simplifier
154 might be: run the simplifier, then run the strictness analyser, then
155 run the simplifier again (three ``todos'').
157 There are three ``to-do processing centers'' at the moment. In the
158 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
159 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
160 (\tr{simplStg/SimplStg.lhs}).
162 %************************************************************************
164 \subsection{Datatypes associated with command-line options}
166 %************************************************************************
169 data CoreToDo -- These are diff core-to-core passes,
170 -- which may be invoked in any order,
171 -- as many times as you like.
173 = CoreDoSimplify -- The core-to-core simplifier.
176 -- Each run of the simplifier can take a different
177 -- set of simplifier-specific flags.
179 | CoreDoFloatOutwards FloatOutSwitches
184 | CoreDoWorkerWrapper
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
253 | Opt_D_dump_worker_wrapper
254 | Opt_D_dump_rn_trace
255 | Opt_D_dump_rn_stats
257 | Opt_D_dump_simpl_stats
258 | 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
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
293 | Opt_PArr -- syntactic support for parallel arrays
295 | Opt_NoImplicitPrelude
299 data DynFlags = DynFlags {
300 coreToDo :: [CoreToDo],
301 stgToDo :: [StgToDo],
303 hscOutName :: String, -- name of the output file
304 hscStubHOutName :: String, -- name of the .stub_h output file
305 hscStubCOutName :: String, -- name of the .stub_c output file
306 extCoreName :: String, -- name of the .core output file
307 verbosity :: Int, -- verbosity level
308 cppFlag :: Bool, -- preprocess with cpp?
309 ppFlag :: Bool, -- preprocess with a Haskell Pp?
310 stolen_x86_regs :: Int,
311 cmdlineHcIncludes :: [String], -- -#includes
313 -- options for particular phases
338 defaultDynFlags = DynFlags {
339 coreToDo = [], stgToDo = [],
342 hscStubHOutName = "", hscStubCOutName = "",
348 cmdlineHcIncludes = [],
359 flags = standardWarnings,
365 0 | print errors & warnings only
366 1 | minimal verbosity: print "compiling M ... done." for each module.
367 2 | equivalent to -dshow-passes
368 3 | equivalent to existing "ghc -v"
369 4 | "ghc -v -ddump-most"
370 5 | "ghc -v -ddump-all"
373 dopt :: DynFlag -> DynFlags -> Bool
374 dopt f dflags = f `elem` (flags dflags)
376 dopt_CoreToDo :: DynFlags -> [CoreToDo]
377 dopt_CoreToDo = coreToDo
379 dopt_StgToDo :: DynFlags -> [StgToDo]
380 dopt_StgToDo = stgToDo
382 dopt_OutName :: DynFlags -> String
383 dopt_OutName = hscOutName
385 dopt_HscLang :: DynFlags -> HscLang
386 dopt_HscLang = hscLang
388 dopt_set :: DynFlags -> DynFlag -> DynFlags
389 dopt_set dfs f = dfs{ flags = f : flags dfs }
391 dopt_unset :: DynFlags -> DynFlag -> DynFlags
392 dopt_unset dfs f = dfs{ flags = filter (/= f) (flags dfs) }
394 getOpts :: (DynFlags -> [a]) -> IO [a]
395 -- We add to the options from the front, so we need to reverse the list
396 getOpts opts = dynFlag opts >>= return . reverse
398 -- we can only switch between HscC, HscAsmm, and HscILX with dynamic flags
399 -- (-fvia-C, -fasm, -filx respectively).
400 setLang l = updDynFlags (\ dfs -> case hscLang dfs of
401 HscC -> dfs{ hscLang = l }
402 HscAsm -> dfs{ hscLang = l }
403 HscILX -> dfs{ hscLang = l }
407 verb <- dynFlag verbosity
408 if verb >= 3 then return "-v" else return ""
411 -----------------------------------------------------------------------------
412 -- Mess about with the mutable variables holding the dynamic arguments
415 -- is the "baseline" dynamic flags, initialised from
416 -- the defaults and command line options, and updated by the
417 -- ':s' command in GHCi.
420 -- is the dynamic flags for the current compilation. It is reset
421 -- to the value of v_InitDynFlags before each compilation, then
422 -- updated by reading any OPTIONS pragma in the current module.
425 GLOBAL_VAR(v_InitDynFlags, defaultDynFlags, DynFlags)
426 GLOBAL_VAR(v_DynFlags, defaultDynFlags, DynFlags)
428 setDynFlags :: DynFlags -> IO ()
429 setDynFlags dfs = writeIORef v_DynFlags dfs
431 saveDynFlags :: IO ()
432 saveDynFlags = do dfs <- readIORef v_DynFlags
433 writeIORef v_InitDynFlags dfs
435 restoreDynFlags :: IO DynFlags
436 restoreDynFlags = do dfs <- readIORef v_InitDynFlags
437 writeIORef v_DynFlags dfs
440 getDynFlags :: IO DynFlags
441 getDynFlags = readIORef v_DynFlags
443 updDynFlags :: (DynFlags -> DynFlags) -> IO ()
444 updDynFlags f = do dfs <- readIORef v_DynFlags
445 writeIORef v_DynFlags (f dfs)
447 dynFlag :: (DynFlags -> a) -> IO a
448 dynFlag f = do dflags <- readIORef v_DynFlags; return (f dflags)
450 setDynFlag, unSetDynFlag :: DynFlag -> IO ()
451 setDynFlag f = updDynFlags (\dfs -> dopt_set dfs f)
452 unSetDynFlag f = updDynFlags (\dfs -> dopt_unset dfs f)
456 %************************************************************************
458 \subsection{Warnings}
460 %************************************************************************
464 = [ Opt_WarnDeprecations,
465 Opt_WarnOverlappingPatterns,
466 Opt_WarnMissingFields,
467 Opt_WarnMissingMethods,
468 Opt_WarnDuplicateExports
472 = standardWarnings ++
473 [ Opt_WarnUnusedBinds,
474 Opt_WarnUnusedMatches,
475 Opt_WarnUnusedImports,
476 Opt_WarnIncompletePatterns,
482 [ Opt_WarnTypeDefaults,
483 Opt_WarnNameShadowing,
489 %************************************************************************
491 \subsection{Classifying command-line options}
493 %************************************************************************
496 -- v_Statis_hsc_opts is here to avoid a circular dependency with
498 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
500 lookUp :: FastString -> Bool
501 lookup_int :: String -> Maybe Int
502 lookup_def_int :: String -> Int -> Int
503 lookup_def_float :: String -> Float -> Float
504 lookup_str :: String -> Maybe String
506 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
507 packed_static_opts = map mkFastString unpacked_static_opts
509 lookUp sw = sw `elem` packed_static_opts
511 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
513 lookup_int sw = case (lookup_str sw) of
515 Just xx -> Just (read xx)
517 lookup_def_int sw def = case (lookup_str sw) of
518 Nothing -> def -- Use default
521 lookup_def_float sw def = case (lookup_str sw) of
522 Nothing -> def -- Use default
527 Putting the compiler options into temporary at-files
528 may turn out to be necessary later on if we turn hsc into
529 a pure Win32 application where I think there's a command-line
530 length limit of 255. unpacked_opts understands the @ option.
532 unpacked_opts :: [String]
536 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
538 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
543 %************************************************************************
545 \subsection{Static options}
547 %************************************************************************
551 opt_PprStyle_NoPrags = lookUp FSLIT("-dppr-noprags")
552 opt_PprStyle_Debug = lookUp FSLIT("-dppr-debug")
553 opt_PprStyle_RawTypes = lookUp FSLIT("-dppr-rawtypes")
554 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
557 opt_AutoSccsOnAllToplevs = lookUp FSLIT("-fauto-sccs-on-all-toplevs")
558 opt_AutoSccsOnExportedToplevs = lookUp FSLIT("-fauto-sccs-on-exported-toplevs")
559 opt_AutoSccsOnIndividualCafs = lookUp FSLIT("-fauto-sccs-on-individual-cafs")
560 opt_AutoSccsOnDicts = lookUp FSLIT("-fauto-sccs-on-dicts")
561 opt_SccProfilingOn = lookUp FSLIT("-fscc-profiling")
562 opt_DoTickyProfiling = lookUp FSLIT("-fticky-ticky")
565 opt_AllStrict = lookUp FSLIT("-fall-strict")
566 opt_DictsStrict = lookUp FSLIT("-fdicts-strict")
567 opt_IrrefutableTuples = lookUp FSLIT("-firrefutable-tuples")
568 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
569 opt_NumbersStrict = lookUp FSLIT("-fnumbers-strict")
570 opt_Parallel = lookUp FSLIT("-fparallel")
571 opt_SMP = lookUp FSLIT("-fsmp")
572 opt_Flatten = lookUp FSLIT("-fflatten")
575 opt_NoMethodSharing = lookUp FSLIT("-fno-method-sharing")
576 opt_DoSemiTagging = lookUp FSLIT("-fsemi-tagging")
577 opt_FoldrBuildOn = lookUp FSLIT("-ffoldr-build-on")
578 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
579 opt_StgDoLetNoEscapes = lookUp FSLIT("-flet-no-escape")
580 opt_UnfoldCasms = lookUp FSLIT("-funfold-casms-in-hi-file")
581 opt_UsageSPOn = lookUp FSLIT("-fusagesp-on")
582 opt_UnboxStrictFields = lookUp FSLIT("-funbox-strict-fields")
583 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
586 The optional '-inpackage=P' flag tells what package
587 we are compiling this module for.
588 The Prelude, for example is compiled with '-inpackage std'
590 opt_InPackage = case lookup_str "-inpackage=" of
592 Nothing -> FSLIT("Main") -- The package name if none is specified
594 opt_EmitCExternDecls = lookUp FSLIT("-femit-extern-decls")
595 opt_EnsureSplittableC = lookUp FSLIT("-fglobalise-toplev-names")
596 opt_GranMacros = lookUp FSLIT("-fgransim")
597 opt_HiVersion = read (cProjectVersionInt ++ cProjectPatchLevel) :: Int
598 opt_HistorySize = lookup_def_int "-fhistory-size" 20
599 opt_IgnoreAsserts = lookUp FSLIT("-fignore-asserts")
600 opt_IgnoreIfacePragmas = lookUp FSLIT("-fignore-interface-pragmas")
601 opt_NoHiCheck = lookUp FSLIT("-fno-hi-version-check")
602 opt_OmitBlackHoling = lookUp FSLIT("-dno-black-holing")
603 opt_OmitInterfacePragmas = lookUp FSLIT("-fomit-interface-pragmas")
604 opt_RuntimeTypes = lookUp FSLIT("-fruntime-types")
606 -- Simplifier switches
607 opt_SimplNoPreInlining = lookUp FSLIT("-fno-pre-inlining")
608 -- NoPreInlining is there just to see how bad things
609 -- get if you don't do it!
610 opt_SimplDoEtaReduction = lookUp FSLIT("-fdo-eta-reduction")
611 opt_SimplDoLambdaEtaExpansion = lookUp FSLIT("-fdo-lambda-eta-expansion")
612 opt_SimplCaseMerge = lookUp FSLIT("-fcase-merge")
613 opt_SimplExcessPrecision = lookUp FSLIT("-fexcess-precision")
616 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
617 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
618 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
619 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
620 opt_UF_UpdateInPlace = lookUp FSLIT("-funfolding-update-in-place")
622 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
623 opt_UF_DearOp = ( 4 :: Int)
625 opt_NoPruneDecls = lookUp FSLIT("-fno-prune-decls")
626 opt_NoPruneTyDecls = lookUp FSLIT("-fno-prune-tydecls")
627 opt_Static = lookUp FSLIT("-static")
628 opt_Unregisterised = lookUp FSLIT("-funregisterised")
629 opt_EmitExternalCore = lookUp FSLIT("-fext-core")
632 %************************************************************************
634 \subsection{List of static hsc flags}
636 %************************************************************************
641 "fauto-sccs-on-all-toplevs",
642 "fauto-sccs-on-exported-toplevs",
643 "fauto-sccs-on-individual-cafs",
644 "fauto-sccs-on-dicts",
649 "firrefutable-tuples",
657 "funfold-casms-in-hi-file",
659 "funbox-strict-fields",
660 "femit-extern-decls",
661 "fglobalise-toplev-names",
664 "fignore-interface-pragmas",
665 "fno-hi-version-check",
667 "fno-method-sharing",
668 "fomit-interface-pragmas",
672 "fdo-lambda-eta-expansion",
675 "funfolding-update-in-place",
683 || any (flip prefixMatch f) [
685 "fliberate-case-threshold",
688 "funfolding-creation-threshold",
689 "funfolding-use-threshold",
690 "funfolding-fun-discount",
691 "funfolding-keeness-factor"
695 %************************************************************************
697 \subsection{Misc functions for command-line options}
699 %************************************************************************
704 startsWith :: String -> String -> Maybe String
705 -- startsWith pfx (pfx++rest) = Just rest
707 startsWith [] str = Just str
708 startsWith (c:cs) (s:ss)
709 = if c /= s then Nothing else startsWith cs ss
710 startsWith _ [] = Nothing
712 endsWith :: String -> String -> Maybe String
714 = case (startsWith (reverse cs) (reverse ss)) of
716 Just rs -> Just (reverse rs)