2 % (c) The University of Glasgow, 1996-2000
4 \section[CmdLineOpts]{Things to do with command-line options}
9 CoreToDo(..), StgToDo(..),
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
30 -- Manipulating the DynFlags state
31 getDynFlags, -- IO DynFlags
32 setDynFlags, -- DynFlags -> IO ()
33 updDynFlags, -- (DynFlags -> DynFlags) -> IO ()
34 dynFlag, -- (DynFlags -> a) -> IO a
35 setDynFlag, unSetDynFlag, -- DynFlag -> IO ()
36 saveDynFlags, -- IO ()
37 restoreDynFlags, -- IO DynFlags
39 -- sets of warning opts
44 -- Output style options
46 opt_PprStyle_RawTypes,
51 opt_AutoSccsOnAllToplevs,
52 opt_AutoSccsOnExportedToplevs,
53 opt_AutoSccsOnIndividualCafs,
61 opt_MaxContextReductionDepth,
62 opt_IrrefutableTuples,
66 opt_NoMonomorphismRestriction,
73 opt_LiberateCaseThreshold,
74 opt_StgDoLetNoEscapes,
77 opt_UnboxStrictFields,
78 opt_SimplNoPreInlining,
79 opt_SimplDoEtaReduction,
80 opt_SimplDoLambdaEtaExpansion,
82 opt_SimplExcessPrecision,
86 opt_UF_CreationThreshold,
88 opt_UF_FunAppDiscount,
97 opt_EnsureSplittableC,
102 opt_IgnoreIfacePragmas,
105 opt_OmitInterfacePragmas,
113 #include "HsVersions.h"
116 import IOExts ( IORef, readIORef, writeIORef )
117 import BasicTypes ( CompilerPhase )
118 import Constants -- Default values for some flags
123 import Maybes ( firstJust )
126 %************************************************************************
128 \subsection{Command-line options}
130 %************************************************************************
132 The hsc command-line options are split into two categories:
137 Static flags are represented by top-level values of type Bool or Int,
138 for example. They therefore have the same value throughout the
141 Dynamic flags are represented by an abstract type, DynFlags, which is
142 passed into hsc by the compilation manager for every compilation.
143 Dynamic flags are those that change on a per-compilation basis,
144 perhaps because they may be present in the OPTIONS pragma at the top
147 Other flag-related blurb:
149 A list of {\em ToDo}s is things to be done in a particular part of
150 processing. A (fictitious) example for the Core-to-Core simplifier
151 might be: run the simplifier, then run the strictness analyser, then
152 run the simplifier again (three ``todos'').
154 There are three ``to-do processing centers'' at the moment. In the
155 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
156 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
157 (\tr{simplStg/SimplStg.lhs}).
159 %************************************************************************
161 \subsection{Datatypes associated with command-line options}
163 %************************************************************************
166 data CoreToDo -- These are diff core-to-core passes,
167 -- which may be invoked in any order,
168 -- as many times as you like.
170 = CoreDoSimplify -- The core-to-core simplifier.
173 -- Each run of the simplifier can take a different
174 -- set of simplifier-specific flags.
176 | CoreDoFloatOutwards Bool -- True <=> float lambdas to top level
181 | CoreDoWorkerWrapper
188 | CoreDoRuleCheck CompilerPhase String -- Check for non-application of rules
189 -- matching this string
191 | CoreDoNothing -- useful when building up lists of these things
196 = StgDoMassageForProfiling -- should be (next to) last
197 -- There's also setStgVarInfo, but its absolute "lastness"
198 -- is so critical that it is hardwired in (no flag).
203 data SimplifierMode -- See comments in SimplMonad
207 data SimplifierSwitch
208 = MaxSimplifierIterations Int
212 %************************************************************************
214 \subsection{Dynamic command-line options}
216 %************************************************************************
229 | Opt_D_dump_inlinings
230 | Opt_D_dump_occur_anal
235 | Opt_D_dump_simpl_iterations
245 | Opt_D_dump_worker_wrapper
246 | Opt_D_dump_rn_trace
247 | Opt_D_dump_rn_stats
249 | Opt_D_dump_simpl_stats
250 | Opt_D_dump_tc_trace
253 | Opt_D_verbose_core2core
254 | Opt_D_verbose_stg2stg
256 | Opt_D_dump_hi_diffs
257 | Opt_D_dump_minimal_imports
262 | Opt_WarnDuplicateExports
264 | Opt_WarnIncompletePatterns
265 | Opt_WarnMissingFields
266 | Opt_WarnMissingMethods
267 | Opt_WarnMissingSigs
268 | Opt_WarnNameShadowing
269 | Opt_WarnOverlappingPatterns
270 | Opt_WarnSimplePatterns
271 | Opt_WarnTypeDefaults
272 | Opt_WarnUnusedBinds
273 | Opt_WarnUnusedImports
274 | Opt_WarnUnusedMatches
275 | Opt_WarnDeprecations
279 | Opt_AllowOverlappingInstances
280 | Opt_AllowUndecidableInstances
283 | Opt_NoImplicitPrelude
287 data DynFlags = DynFlags {
288 coreToDo :: [CoreToDo],
289 stgToDo :: [StgToDo],
291 hscOutName :: String, -- name of the output file
292 hscStubHOutName :: String, -- name of the .stub_h output file
293 hscStubCOutName :: String, -- name of the .stub_c output file
294 extCoreName :: String, -- name of the .core output file
295 verbosity :: Int, -- verbosity level
296 cppFlag :: Bool, -- preprocess with cpp?
297 stolen_x86_regs :: Int,
298 cmdlineHcIncludes :: [String], -- -#includes
300 -- options for particular phases
324 defaultDynFlags = DynFlags {
325 coreToDo = [], stgToDo = [],
328 hscStubHOutName = "", hscStubCOutName = "",
333 cmdlineHcIncludes = [],
343 flags = standardWarnings,
349 0 | print errors & warnings only
350 1 | minimal verbosity: print "compiling M ... done." for each module.
351 2 | equivalent to -dshow-passes
352 3 | equivalent to existing "ghc -v"
353 4 | "ghc -v -ddump-most"
354 5 | "ghc -v -ddump-all"
357 dopt :: DynFlag -> DynFlags -> Bool
358 dopt f dflags = f `elem` (flags dflags)
360 dopt_CoreToDo :: DynFlags -> [CoreToDo]
361 dopt_CoreToDo = coreToDo
363 dopt_StgToDo :: DynFlags -> [StgToDo]
364 dopt_StgToDo = stgToDo
366 dopt_OutName :: DynFlags -> String
367 dopt_OutName = hscOutName
369 dopt_HscLang :: DynFlags -> HscLang
370 dopt_HscLang = hscLang
372 dopt_set :: DynFlags -> DynFlag -> DynFlags
373 dopt_set dfs f = dfs{ flags = f : flags dfs }
375 dopt_unset :: DynFlags -> DynFlag -> DynFlags
376 dopt_unset dfs f = dfs{ flags = filter (/= f) (flags dfs) }
379 -----------------------------------------------------------------------------
380 -- Mess about with the mutable variables holding the dynamic arguments
383 -- is the "baseline" dynamic flags, initialised from
384 -- the defaults and command line options, and updated by the
385 -- ':s' command in GHCi.
388 -- is the dynamic flags for the current compilation. It is reset
389 -- to the value of v_InitDynFlags before each compilation, then
390 -- updated by reading any OPTIONS pragma in the current module.
393 GLOBAL_VAR(v_InitDynFlags, defaultDynFlags, DynFlags)
394 GLOBAL_VAR(v_DynFlags, defaultDynFlags, DynFlags)
396 setDynFlags :: DynFlags -> IO ()
397 setDynFlags dfs = writeIORef v_DynFlags dfs
399 saveDynFlags :: IO ()
400 saveDynFlags = do dfs <- readIORef v_DynFlags
401 writeIORef v_InitDynFlags dfs
403 restoreDynFlags :: IO DynFlags
404 restoreDynFlags = do dfs <- readIORef v_InitDynFlags
405 writeIORef v_DynFlags dfs
408 getDynFlags :: IO DynFlags
409 getDynFlags = readIORef v_DynFlags
411 updDynFlags :: (DynFlags -> DynFlags) -> IO ()
412 updDynFlags f = do dfs <- readIORef v_DynFlags
413 writeIORef v_DynFlags (f dfs)
415 dynFlag :: (DynFlags -> a) -> IO a
416 dynFlag f = do dflags <- readIORef v_DynFlags; return (f dflags)
418 setDynFlag, unSetDynFlag :: DynFlag -> IO ()
419 setDynFlag f = updDynFlags (\dfs -> dopt_set dfs f)
420 unSetDynFlag f = updDynFlags (\dfs -> dopt_unset dfs f)
424 %************************************************************************
426 \subsection{Warnings}
428 %************************************************************************
432 = [ Opt_WarnDeprecations,
433 Opt_WarnOverlappingPatterns,
434 Opt_WarnMissingFields,
435 Opt_WarnMissingMethods,
436 Opt_WarnDuplicateExports
440 = standardWarnings ++
441 [ Opt_WarnUnusedBinds,
442 Opt_WarnUnusedMatches,
443 Opt_WarnUnusedImports,
444 Opt_WarnIncompletePatterns,
450 [ Opt_WarnTypeDefaults,
451 Opt_WarnNameShadowing,
457 %************************************************************************
459 \subsection{Classifying command-line options}
461 %************************************************************************
464 -- v_Statis_hsc_opts is here to avoid a circular dependency with
466 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
468 lookUp :: FAST_STRING -> Bool
469 lookup_int :: String -> Maybe Int
470 lookup_def_int :: String -> Int -> Int
471 lookup_def_float :: String -> Float -> Float
472 lookup_str :: String -> Maybe String
474 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
475 packed_static_opts = map _PK_ unpacked_static_opts
477 lookUp sw = sw `elem` packed_static_opts
479 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
481 lookup_int sw = case (lookup_str sw) of
483 Just xx -> Just (read xx)
485 lookup_def_int sw def = case (lookup_str sw) of
486 Nothing -> def -- Use default
489 lookup_def_float sw def = case (lookup_str sw) of
490 Nothing -> def -- Use default
495 Putting the compiler options into temporary at-files
496 may turn out to be necessary later on if we turn hsc into
497 a pure Win32 application where I think there's a command-line
498 length limit of 255. unpacked_opts understands the @ option.
500 unpacked_opts :: [String]
504 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
506 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
511 %************************************************************************
513 \subsection{Static options}
515 %************************************************************************
519 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
520 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
521 opt_PprStyle_RawTypes = lookUp SLIT("-dppr-rawtypes")
522 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
525 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
526 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
527 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
528 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
529 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
530 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
533 opt_AllStrict = lookUp SLIT("-fall-strict")
534 opt_NoMonomorphismRestriction = lookUp SLIT("-fno-monomorphism-restriction")
535 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
536 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
537 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
538 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
539 opt_Parallel = lookUp SLIT("-fparallel")
540 opt_SMP = lookUp SLIT("-fsmp")
543 opt_NoMethodSharing = lookUp SLIT("-fno-method-sharing")
544 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
545 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
546 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
547 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
548 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
549 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
550 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
551 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
554 The optional '-inpackage=P' flag tells what package
555 we are compiling this module for.
556 The Prelude, for example is compiled with '-inpackage std'
558 opt_InPackage = case lookup_str "-inpackage=" of
560 Nothing -> SLIT("Main") -- The package name if none is specified
562 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
563 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
564 opt_GranMacros = lookUp SLIT("-fgransim")
565 opt_HiVersion = read cProjectVersionInt :: Int
566 opt_HistorySize = lookup_def_int "-fhistory-size" 20
567 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
568 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
569 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
570 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
571 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
572 opt_RuntimeTypes = lookUp SLIT("-fruntime-types")
574 -- Simplifier switches
575 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
576 -- NoPreInlining is there just to see how bad things
577 -- get if you don't do it!
578 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
579 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
580 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
581 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
584 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
585 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
586 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
587 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
588 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
590 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
591 opt_UF_DearOp = ( 4 :: Int)
593 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
594 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
595 opt_Static = lookUp SLIT("-static")
596 opt_Unregisterised = lookUp SLIT("-funregisterised")
597 opt_EmitExternalCore = lookUp SLIT("-fext-core")
600 %************************************************************************
602 \subsection{List of static hsc flags}
604 %************************************************************************
609 "fauto-sccs-on-all-toplevs",
610 "fauto-sccs-on-exported-toplevs",
611 "fauto-sccs-on-individual-cafs",
612 "fauto-sccs-on-dicts",
617 "firrefutable-tuples",
624 "funfold-casms-in-hi-file",
626 "funbox-strict-fields",
627 "femit-extern-decls",
628 "fglobalise-toplev-names",
631 "fignore-interface-pragmas",
632 "fno-hi-version-check",
634 "fno-method-sharing",
635 "fno-monomorphism-restriction",
636 "fomit-interface-pragmas",
640 "fdo-lambda-eta-expansion",
643 "funfolding-update-in-place",
651 || any (flip prefixMatch f) [
653 "fliberate-case-threshold",
656 "funfolding-creation-threshold",
657 "funfolding-use-threshold",
658 "funfolding-fun-discount",
659 "funfolding-keeness-factor"
663 %************************************************************************
665 \subsection{Misc functions for command-line options}
667 %************************************************************************
672 startsWith :: String -> String -> Maybe String
673 -- startsWith pfx (pfx++rest) = Just rest
675 startsWith [] str = Just str
676 startsWith (c:cs) (s:ss)
677 = if c /= s then Nothing else startsWith cs ss
678 startsWith _ [] = Nothing
680 endsWith :: String -> String -> Maybe String
682 = case (startsWith (reverse cs) (reverse ss)) of
684 Just rs -> Just (reverse rs)