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
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 Constants -- Default values for some flags
122 import Maybes ( firstJust )
125 %************************************************************************
127 \subsection{Command-line options}
129 %************************************************************************
131 The hsc command-line options are split into two categories:
136 Static flags are represented by top-level values of type Bool or Int,
137 for example. They therefore have the same value throughout the
140 Dynamic flags are represented by an abstract type, DynFlags, which is
141 passed into hsc by the compilation manager for every compilation.
142 Dynamic flags are those that change on a per-compilation basis,
143 perhaps because they may be present in the OPTIONS pragma at the top
146 Other flag-related blurb:
148 A list of {\em ToDo}s is things to be done in a particular part of
149 processing. A (fictitious) example for the Core-to-Core simplifier
150 might be: run the simplifier, then run the strictness analyser, then
151 run the simplifier again (three ``todos'').
153 There are three ``to-do processing centers'' at the moment. In the
154 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
155 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
156 (\tr{simplStg/SimplStg.lhs}).
158 %************************************************************************
160 \subsection{Datatypes associated with command-line options}
162 %************************************************************************
165 data CoreToDo -- These are diff core-to-core passes,
166 -- which may be invoked in any order,
167 -- as many times as you like.
169 = CoreDoSimplify -- The core-to-core simplifier.
172 -- Each run of the simplifier can take a different
173 -- set of simplifier-specific flags.
175 | CoreDoFloatOutwards FloatOutSwitches
180 | CoreDoWorkerWrapper
187 | CoreDoRuleCheck Int{-CompilerPhase-} String -- Check for non-application of rules
188 -- matching this string
190 | CoreDoNothing -- useful when building up lists of these things
195 = StgDoMassageForProfiling -- should be (next to) last
196 -- There's also setStgVarInfo, but its absolute "lastness"
197 -- is so critical that it is hardwired in (no flag).
202 data SimplifierMode -- See comments in SimplMonad
206 data SimplifierSwitch
207 = MaxSimplifierIterations Int
210 data FloatOutSwitches
211 = FloatOutSw Bool -- True <=> float lambdas to top level
212 Bool -- True <=> float constants to top level,
213 -- even if they do not escape a lambda
216 %************************************************************************
218 \subsection{Dynamic command-line options}
220 %************************************************************************
233 | Opt_D_dump_inlinings
234 | Opt_D_dump_occur_anal
239 | Opt_D_dump_simpl_iterations
249 | Opt_D_dump_worker_wrapper
250 | Opt_D_dump_rn_trace
251 | Opt_D_dump_rn_stats
253 | Opt_D_dump_simpl_stats
254 | Opt_D_dump_tc_trace
257 | Opt_D_verbose_core2core
258 | Opt_D_verbose_stg2stg
260 | Opt_D_dump_hi_diffs
261 | Opt_D_dump_minimal_imports
266 | Opt_WarnDuplicateExports
268 | Opt_WarnIncompletePatterns
269 | Opt_WarnMissingFields
270 | Opt_WarnMissingMethods
271 | Opt_WarnMissingSigs
272 | Opt_WarnNameShadowing
273 | Opt_WarnOverlappingPatterns
274 | Opt_WarnSimplePatterns
275 | Opt_WarnTypeDefaults
276 | Opt_WarnUnusedBinds
277 | Opt_WarnUnusedImports
278 | Opt_WarnUnusedMatches
279 | Opt_WarnDeprecations
283 | Opt_AllowOverlappingInstances
284 | Opt_AllowUndecidableInstances
287 | Opt_NoImplicitPrelude
291 data DynFlags = DynFlags {
292 coreToDo :: [CoreToDo],
293 stgToDo :: [StgToDo],
295 hscOutName :: String, -- name of the output file
296 hscStubHOutName :: String, -- name of the .stub_h output file
297 hscStubCOutName :: String, -- name of the .stub_c output file
298 extCoreName :: String, -- name of the .core output file
299 verbosity :: Int, -- verbosity level
300 cppFlag :: Bool, -- preprocess with cpp?
301 ppFlag :: Bool, -- preprocess with a Haskell Pp?
302 stolen_x86_regs :: Int,
303 cmdlineHcIncludes :: [String], -- -#includes
305 -- options for particular phases
330 defaultDynFlags = DynFlags {
331 coreToDo = [], stgToDo = [],
334 hscStubHOutName = "", hscStubCOutName = "",
340 cmdlineHcIncludes = [],
351 flags = standardWarnings,
357 0 | print errors & warnings only
358 1 | minimal verbosity: print "compiling M ... done." for each module.
359 2 | equivalent to -dshow-passes
360 3 | equivalent to existing "ghc -v"
361 4 | "ghc -v -ddump-most"
362 5 | "ghc -v -ddump-all"
365 dopt :: DynFlag -> DynFlags -> Bool
366 dopt f dflags = f `elem` (flags dflags)
368 dopt_CoreToDo :: DynFlags -> [CoreToDo]
369 dopt_CoreToDo = coreToDo
371 dopt_StgToDo :: DynFlags -> [StgToDo]
372 dopt_StgToDo = stgToDo
374 dopt_OutName :: DynFlags -> String
375 dopt_OutName = hscOutName
377 dopt_HscLang :: DynFlags -> HscLang
378 dopt_HscLang = hscLang
380 dopt_set :: DynFlags -> DynFlag -> DynFlags
381 dopt_set dfs f = dfs{ flags = f : flags dfs }
383 dopt_unset :: DynFlags -> DynFlag -> DynFlags
384 dopt_unset dfs f = dfs{ flags = filter (/= f) (flags dfs) }
387 -----------------------------------------------------------------------------
388 -- Mess about with the mutable variables holding the dynamic arguments
391 -- is the "baseline" dynamic flags, initialised from
392 -- the defaults and command line options, and updated by the
393 -- ':s' command in GHCi.
396 -- is the dynamic flags for the current compilation. It is reset
397 -- to the value of v_InitDynFlags before each compilation, then
398 -- updated by reading any OPTIONS pragma in the current module.
401 GLOBAL_VAR(v_InitDynFlags, defaultDynFlags, DynFlags)
402 GLOBAL_VAR(v_DynFlags, defaultDynFlags, DynFlags)
404 setDynFlags :: DynFlags -> IO ()
405 setDynFlags dfs = writeIORef v_DynFlags dfs
407 saveDynFlags :: IO ()
408 saveDynFlags = do dfs <- readIORef v_DynFlags
409 writeIORef v_InitDynFlags dfs
411 restoreDynFlags :: IO DynFlags
412 restoreDynFlags = do dfs <- readIORef v_InitDynFlags
413 writeIORef v_DynFlags dfs
416 getDynFlags :: IO DynFlags
417 getDynFlags = readIORef v_DynFlags
419 updDynFlags :: (DynFlags -> DynFlags) -> IO ()
420 updDynFlags f = do dfs <- readIORef v_DynFlags
421 writeIORef v_DynFlags (f dfs)
423 dynFlag :: (DynFlags -> a) -> IO a
424 dynFlag f = do dflags <- readIORef v_DynFlags; return (f dflags)
426 setDynFlag, unSetDynFlag :: DynFlag -> IO ()
427 setDynFlag f = updDynFlags (\dfs -> dopt_set dfs f)
428 unSetDynFlag f = updDynFlags (\dfs -> dopt_unset dfs f)
432 %************************************************************************
434 \subsection{Warnings}
436 %************************************************************************
440 = [ Opt_WarnDeprecations,
441 Opt_WarnOverlappingPatterns,
442 Opt_WarnMissingFields,
443 Opt_WarnMissingMethods,
444 Opt_WarnDuplicateExports
448 = standardWarnings ++
449 [ Opt_WarnUnusedBinds,
450 Opt_WarnUnusedMatches,
451 Opt_WarnUnusedImports,
452 Opt_WarnIncompletePatterns,
458 [ Opt_WarnTypeDefaults,
459 Opt_WarnNameShadowing,
465 %************************************************************************
467 \subsection{Classifying command-line options}
469 %************************************************************************
472 -- v_Statis_hsc_opts is here to avoid a circular dependency with
474 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
476 lookUp :: FAST_STRING -> Bool
477 lookup_int :: String -> Maybe Int
478 lookup_def_int :: String -> Int -> Int
479 lookup_def_float :: String -> Float -> Float
480 lookup_str :: String -> Maybe String
482 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
483 packed_static_opts = map _PK_ unpacked_static_opts
485 lookUp sw = sw `elem` packed_static_opts
487 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
489 lookup_int sw = case (lookup_str sw) of
491 Just xx -> Just (read xx)
493 lookup_def_int sw def = case (lookup_str sw) of
494 Nothing -> def -- Use default
497 lookup_def_float sw def = case (lookup_str sw) of
498 Nothing -> def -- Use default
503 Putting the compiler options into temporary at-files
504 may turn out to be necessary later on if we turn hsc into
505 a pure Win32 application where I think there's a command-line
506 length limit of 255. unpacked_opts understands the @ option.
508 unpacked_opts :: [String]
512 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
514 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
519 %************************************************************************
521 \subsection{Static options}
523 %************************************************************************
527 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
528 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
529 opt_PprStyle_RawTypes = lookUp SLIT("-dppr-rawtypes")
530 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
533 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
534 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
535 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
536 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
537 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
538 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
541 opt_AllStrict = lookUp SLIT("-fall-strict")
542 opt_NoMonomorphismRestriction = lookUp SLIT("-fno-monomorphism-restriction")
543 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
544 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
545 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
546 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
547 opt_Parallel = lookUp SLIT("-fparallel")
548 opt_SMP = lookUp SLIT("-fsmp")
551 opt_NoMethodSharing = lookUp SLIT("-fno-method-sharing")
552 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
553 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
554 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
555 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
556 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
557 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
558 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
559 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
562 The optional '-inpackage=P' flag tells what package
563 we are compiling this module for.
564 The Prelude, for example is compiled with '-inpackage std'
566 opt_InPackage = case lookup_str "-inpackage=" of
568 Nothing -> SLIT("Main") -- The package name if none is specified
570 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
571 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
572 opt_GranMacros = lookUp SLIT("-fgransim")
573 opt_HiVersion = read cProjectVersionInt :: Int
574 opt_HistorySize = lookup_def_int "-fhistory-size" 20
575 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
576 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
577 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
578 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
579 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
580 opt_RuntimeTypes = lookUp SLIT("-fruntime-types")
582 -- Simplifier switches
583 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
584 -- NoPreInlining is there just to see how bad things
585 -- get if you don't do it!
586 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
587 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
588 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
589 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
592 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
593 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
594 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
595 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
596 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
598 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
599 opt_UF_DearOp = ( 4 :: Int)
601 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
602 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
603 opt_Static = lookUp SLIT("-static")
604 opt_Unregisterised = lookUp SLIT("-funregisterised")
605 opt_EmitExternalCore = lookUp SLIT("-fext-core")
608 %************************************************************************
610 \subsection{List of static hsc flags}
612 %************************************************************************
617 "fauto-sccs-on-all-toplevs",
618 "fauto-sccs-on-exported-toplevs",
619 "fauto-sccs-on-individual-cafs",
620 "fauto-sccs-on-dicts",
625 "firrefutable-tuples",
632 "funfold-casms-in-hi-file",
634 "funbox-strict-fields",
635 "femit-extern-decls",
636 "fglobalise-toplev-names",
639 "fignore-interface-pragmas",
640 "fno-hi-version-check",
642 "fno-method-sharing",
643 "fno-monomorphism-restriction",
644 "fomit-interface-pragmas",
648 "fdo-lambda-eta-expansion",
651 "funfolding-update-in-place",
659 || any (flip prefixMatch f) [
661 "fliberate-case-threshold",
664 "funfolding-creation-threshold",
665 "funfolding-use-threshold",
666 "funfolding-fun-discount",
667 "funfolding-keeness-factor"
671 %************************************************************************
673 \subsection{Misc functions for command-line options}
675 %************************************************************************
680 startsWith :: String -> String -> Maybe String
681 -- startsWith pfx (pfx++rest) = Just rest
683 startsWith [] str = Just str
684 startsWith (c:cs) (s:ss)
685 = if c /= s then Nothing else startsWith cs ss
686 startsWith _ [] = Nothing
688 endsWith :: String -> String -> Maybe String
690 = case (startsWith (reverse cs) (reverse ss)) of
692 Just rs -> Just (reverse rs)