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,
81 opt_UnboxStrictFields,
82 opt_SimplNoPreInlining,
83 opt_SimplDoEtaReduction,
84 opt_SimplDoLambdaEtaExpansion,
86 opt_SimplExcessPrecision,
90 opt_UF_CreationThreshold,
92 opt_UF_FunAppDiscount,
100 opt_EmitCExternDecls,
101 opt_EnsureSplittableC,
106 opt_IgnoreIfacePragmas,
109 opt_OmitInterfacePragmas,
117 #include "HsVersions.h"
120 import IOExts ( IORef, readIORef, writeIORef )
121 import Constants -- Default values for some flags
124 import FastString ( FastString, mkFastString )
127 import Maybes ( firstJust )
130 %************************************************************************
132 \subsection{Command-line options}
134 %************************************************************************
136 The hsc command-line options are split into two categories:
141 Static flags are represented by top-level values of type Bool or Int,
142 for example. They therefore have the same value throughout the
145 Dynamic flags are represented by an abstract type, DynFlags, which is
146 passed into hsc by the compilation manager for every compilation.
147 Dynamic flags are those that change on a per-compilation basis,
148 perhaps because they may be present in the OPTIONS pragma at the top
151 Other flag-related blurb:
153 A list of {\em ToDo}s is things to be done in a particular part of
154 processing. A (fictitious) example for the Core-to-Core simplifier
155 might be: run the simplifier, then run the strictness analyser, then
156 run the simplifier again (three ``todos'').
158 There are three ``to-do processing centers'' at the moment. In the
159 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
160 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
161 (\tr{simplStg/SimplStg.lhs}).
163 %************************************************************************
165 \subsection{Datatypes associated with command-line options}
167 %************************************************************************
170 data CoreToDo -- These are diff core-to-core passes,
171 -- which may be invoked in any order,
172 -- as many times as you like.
174 = CoreDoSimplify -- The core-to-core simplifier.
177 -- Each run of the simplifier can take a different
178 -- set of simplifier-specific flags.
180 | CoreDoFloatOutwards FloatOutSwitches
185 | CoreDoWorkerWrapper
192 | CoreDoRuleCheck Int{-CompilerPhase-} String -- Check for non-application of rules
193 -- matching this string
195 | CoreDoNothing -- useful when building up lists of these things
200 = StgDoMassageForProfiling -- should be (next to) last
201 -- There's also setStgVarInfo, but its absolute "lastness"
202 -- is so critical that it is hardwired in (no flag).
207 data SimplifierMode -- See comments in SimplMonad
211 data SimplifierSwitch
212 = MaxSimplifierIterations Int
215 data FloatOutSwitches
216 = FloatOutSw Bool -- True <=> float lambdas to top level
217 Bool -- True <=> float constants to top level,
218 -- even if they do not escape a lambda
221 %************************************************************************
223 \subsection{Dynamic command-line options}
225 %************************************************************************
238 | Opt_D_dump_inlinings
239 | Opt_D_dump_occur_anal
244 | Opt_D_dump_simpl_iterations
254 | Opt_D_dump_worker_wrapper
255 | Opt_D_dump_rn_trace
256 | Opt_D_dump_rn_stats
258 | Opt_D_dump_simpl_stats
259 | Opt_D_dump_tc_trace
263 | Opt_D_verbose_core2core
264 | Opt_D_verbose_stg2stg
266 | Opt_D_dump_hi_diffs
267 | Opt_D_dump_minimal_imports
272 | Opt_WarnDuplicateExports
274 | Opt_WarnIncompletePatterns
275 | Opt_WarnMissingFields
276 | Opt_WarnMissingMethods
277 | Opt_WarnMissingSigs
278 | Opt_WarnNameShadowing
279 | Opt_WarnOverlappingPatterns
280 | Opt_WarnSimplePatterns
281 | Opt_WarnTypeDefaults
282 | Opt_WarnUnusedBinds
283 | Opt_WarnUnusedImports
284 | Opt_WarnUnusedMatches
285 | Opt_WarnDeprecations
289 | Opt_AllowOverlappingInstances
290 | Opt_AllowUndecidableInstances
291 | Opt_AllowIncoherentInstances
292 | Opt_NoMonomorphismRestriction
294 | Opt_PArr -- syntactic support for parallel arrays
296 | Opt_NoImplicitPrelude
300 data DynFlags = DynFlags {
301 coreToDo :: [CoreToDo],
302 stgToDo :: [StgToDo],
304 hscOutName :: String, -- name of the output file
305 hscStubHOutName :: String, -- name of the .stub_h output file
306 hscStubCOutName :: String, -- name of the .stub_c output file
307 extCoreName :: String, -- name of the .core output file
308 verbosity :: Int, -- verbosity level
309 cppFlag :: Bool, -- preprocess with cpp?
310 ppFlag :: Bool, -- preprocess with a Haskell Pp?
311 stolen_x86_regs :: Int,
312 cmdlineHcIncludes :: [String], -- -#includes
314 -- options for particular phases
341 | cGhcWithNativeCodeGen == "YES" &&
342 (prefixMatch "i386" cTARGETPLATFORM ||
343 prefixMatch "sparc" cTARGETPLATFORM) = HscAsm
346 defaultDynFlags = DynFlags {
347 coreToDo = [], stgToDo = [],
348 hscLang = defaultHscLang,
350 hscStubHOutName = "", hscStubCOutName = "",
356 cmdlineHcIncludes = [],
367 flags = standardWarnings,
373 0 | print errors & warnings only
374 1 | minimal verbosity: print "compiling M ... done." for each module.
375 2 | equivalent to -dshow-passes
376 3 | equivalent to existing "ghc -v"
377 4 | "ghc -v -ddump-most"
378 5 | "ghc -v -ddump-all"
381 dopt :: DynFlag -> DynFlags -> Bool
382 dopt f dflags = f `elem` (flags dflags)
384 dopt_CoreToDo :: DynFlags -> [CoreToDo]
385 dopt_CoreToDo = coreToDo
387 dopt_StgToDo :: DynFlags -> [StgToDo]
388 dopt_StgToDo = stgToDo
390 dopt_OutName :: DynFlags -> String
391 dopt_OutName = hscOutName
393 dopt_HscLang :: DynFlags -> HscLang
394 dopt_HscLang = hscLang
396 dopt_set :: DynFlags -> DynFlag -> DynFlags
397 dopt_set dfs f = dfs{ flags = f : flags dfs }
399 dopt_unset :: DynFlags -> DynFlag -> DynFlags
400 dopt_unset dfs f = dfs{ flags = filter (/= f) (flags dfs) }
402 getOpts :: (DynFlags -> [a]) -> IO [a]
403 -- We add to the options from the front, so we need to reverse the list
404 getOpts opts = dynFlag opts >>= return . reverse
406 -- we can only switch between HscC, HscAsmm, and HscILX with dynamic flags
407 -- (-fvia-C, -fasm, -filx respectively).
408 setLang l = updDynFlags (\ dfs -> case hscLang dfs of
409 HscC -> dfs{ hscLang = l }
410 HscAsm -> dfs{ hscLang = l }
411 HscILX -> dfs{ hscLang = l }
415 verb <- dynFlag verbosity
416 if verb >= 3 then return "-v" else return ""
419 -----------------------------------------------------------------------------
420 -- Mess about with the mutable variables holding the dynamic arguments
423 -- is the "baseline" dynamic flags, initialised from
424 -- the defaults and command line options, and updated by the
425 -- ':s' command in GHCi.
428 -- is the dynamic flags for the current compilation. It is reset
429 -- to the value of v_InitDynFlags before each compilation, then
430 -- updated by reading any OPTIONS pragma in the current module.
433 GLOBAL_VAR(v_InitDynFlags, defaultDynFlags, DynFlags)
434 GLOBAL_VAR(v_DynFlags, defaultDynFlags, DynFlags)
436 setDynFlags :: DynFlags -> IO ()
437 setDynFlags dfs = writeIORef v_DynFlags dfs
439 saveDynFlags :: IO ()
440 saveDynFlags = do dfs <- readIORef v_DynFlags
441 writeIORef v_InitDynFlags dfs
443 restoreDynFlags :: IO DynFlags
444 restoreDynFlags = do dfs <- readIORef v_InitDynFlags
445 writeIORef v_DynFlags dfs
448 getDynFlags :: IO DynFlags
449 getDynFlags = readIORef v_DynFlags
451 updDynFlags :: (DynFlags -> DynFlags) -> IO ()
452 updDynFlags f = do dfs <- readIORef v_DynFlags
453 writeIORef v_DynFlags (f dfs)
455 dynFlag :: (DynFlags -> a) -> IO a
456 dynFlag f = do dflags <- readIORef v_DynFlags; return (f dflags)
458 setDynFlag, unSetDynFlag :: DynFlag -> IO ()
459 setDynFlag f = updDynFlags (\dfs -> dopt_set dfs f)
460 unSetDynFlag f = updDynFlags (\dfs -> dopt_unset dfs f)
464 %************************************************************************
466 \subsection{Warnings}
468 %************************************************************************
472 = [ Opt_WarnDeprecations,
473 Opt_WarnOverlappingPatterns,
474 Opt_WarnMissingFields,
475 Opt_WarnMissingMethods,
476 Opt_WarnDuplicateExports
480 = standardWarnings ++
481 [ Opt_WarnUnusedBinds,
482 Opt_WarnUnusedMatches,
483 Opt_WarnUnusedImports,
484 Opt_WarnIncompletePatterns,
490 [ Opt_WarnTypeDefaults,
491 Opt_WarnNameShadowing,
497 %************************************************************************
499 \subsection{Classifying command-line options}
501 %************************************************************************
504 -- v_Statis_hsc_opts is here to avoid a circular dependency with
506 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
508 lookUp :: FastString -> Bool
509 lookup_int :: String -> Maybe Int
510 lookup_def_int :: String -> Int -> Int
511 lookup_def_float :: String -> Float -> Float
512 lookup_str :: String -> Maybe String
514 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
515 packed_static_opts = map mkFastString unpacked_static_opts
517 lookUp sw = sw `elem` packed_static_opts
519 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
521 lookup_int sw = case (lookup_str sw) of
523 Just xx -> Just (read xx)
525 lookup_def_int sw def = case (lookup_str sw) of
526 Nothing -> def -- Use default
529 lookup_def_float sw def = case (lookup_str sw) of
530 Nothing -> def -- Use default
535 Putting the compiler options into temporary at-files
536 may turn out to be necessary later on if we turn hsc into
537 a pure Win32 application where I think there's a command-line
538 length limit of 255. unpacked_opts understands the @ option.
540 unpacked_opts :: [String]
544 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
546 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
551 %************************************************************************
553 \subsection{Static options}
555 %************************************************************************
559 opt_PprStyle_NoPrags = lookUp FSLIT("-dppr-noprags")
560 opt_PprStyle_Debug = lookUp FSLIT("-dppr-debug")
561 opt_PprStyle_RawTypes = lookUp FSLIT("-dppr-rawtypes")
562 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
565 opt_AutoSccsOnAllToplevs = lookUp FSLIT("-fauto-sccs-on-all-toplevs")
566 opt_AutoSccsOnExportedToplevs = lookUp FSLIT("-fauto-sccs-on-exported-toplevs")
567 opt_AutoSccsOnIndividualCafs = lookUp FSLIT("-fauto-sccs-on-individual-cafs")
568 opt_AutoSccsOnDicts = lookUp FSLIT("-fauto-sccs-on-dicts")
569 opt_SccProfilingOn = lookUp FSLIT("-fscc-profiling")
570 opt_DoTickyProfiling = lookUp FSLIT("-fticky-ticky")
573 opt_AllStrict = lookUp FSLIT("-fall-strict")
574 opt_DictsStrict = lookUp FSLIT("-fdicts-strict")
575 opt_IrrefutableTuples = lookUp FSLIT("-firrefutable-tuples")
576 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
577 opt_NumbersStrict = lookUp FSLIT("-fnumbers-strict")
578 opt_Parallel = lookUp FSLIT("-fparallel")
579 opt_SMP = lookUp FSLIT("-fsmp")
580 opt_Flatten = lookUp FSLIT("-fflatten")
583 opt_NoMethodSharing = lookUp FSLIT("-fno-method-sharing")
584 opt_DoSemiTagging = lookUp FSLIT("-fsemi-tagging")
585 opt_FoldrBuildOn = lookUp FSLIT("-ffoldr-build-on")
586 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
587 opt_StgDoLetNoEscapes = lookUp FSLIT("-flet-no-escape")
588 opt_UnfoldCasms = lookUp FSLIT("-funfold-casms-in-hi-file")
589 opt_UsageSPOn = lookUp FSLIT("-fusagesp-on")
590 opt_UnboxStrictFields = lookUp FSLIT("-funbox-strict-fields")
591 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
594 The optional '-inpackage=P' flag tells what package
595 we are compiling this module for.
596 The Prelude, for example is compiled with '-inpackage std'
598 opt_InPackage = case lookup_str "-inpackage=" of
600 Nothing -> FSLIT("Main") -- The package name if none is specified
602 opt_EmitCExternDecls = lookUp FSLIT("-femit-extern-decls")
603 opt_EnsureSplittableC = lookUp FSLIT("-fglobalise-toplev-names")
604 opt_GranMacros = lookUp FSLIT("-fgransim")
605 opt_HiVersion = read (cProjectVersionInt ++ cProjectPatchLevel) :: Int
606 opt_HistorySize = lookup_def_int "-fhistory-size" 20
607 opt_IgnoreAsserts = lookUp FSLIT("-fignore-asserts")
608 opt_IgnoreIfacePragmas = lookUp FSLIT("-fignore-interface-pragmas")
609 opt_NoHiCheck = lookUp FSLIT("-fno-hi-version-check")
610 opt_OmitBlackHoling = lookUp FSLIT("-dno-black-holing")
611 opt_OmitInterfacePragmas = lookUp FSLIT("-fomit-interface-pragmas")
612 opt_RuntimeTypes = lookUp FSLIT("-fruntime-types")
614 -- Simplifier switches
615 opt_SimplNoPreInlining = lookUp FSLIT("-fno-pre-inlining")
616 -- NoPreInlining is there just to see how bad things
617 -- get if you don't do it!
618 opt_SimplDoEtaReduction = lookUp FSLIT("-fdo-eta-reduction")
619 opt_SimplDoLambdaEtaExpansion = lookUp FSLIT("-fdo-lambda-eta-expansion")
620 opt_SimplCaseMerge = lookUp FSLIT("-fcase-merge")
621 opt_SimplExcessPrecision = lookUp FSLIT("-fexcess-precision")
624 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
625 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
626 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
627 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
628 opt_UF_UpdateInPlace = lookUp FSLIT("-funfolding-update-in-place")
630 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
631 opt_UF_DearOp = ( 4 :: Int)
633 opt_NoPruneDecls = lookUp FSLIT("-fno-prune-decls")
634 opt_NoPruneTyDecls = lookUp FSLIT("-fno-prune-tydecls")
635 opt_Static = lookUp FSLIT("-static")
636 opt_Unregisterised = lookUp FSLIT("-funregisterised")
637 opt_EmitExternalCore = lookUp FSLIT("-fext-core")
640 %************************************************************************
642 \subsection{List of static hsc flags}
644 %************************************************************************
649 "fauto-sccs-on-all-toplevs",
650 "fauto-sccs-on-exported-toplevs",
651 "fauto-sccs-on-individual-cafs",
652 "fauto-sccs-on-dicts",
657 "firrefutable-tuples",
665 "funfold-casms-in-hi-file",
667 "funbox-strict-fields",
668 "femit-extern-decls",
669 "fglobalise-toplev-names",
672 "fignore-interface-pragmas",
673 "fno-hi-version-check",
675 "fno-method-sharing",
676 "fomit-interface-pragmas",
680 "fdo-lambda-eta-expansion",
683 "funfolding-update-in-place",
691 || any (flip prefixMatch f) [
693 "fliberate-case-threshold",
696 "funfolding-creation-threshold",
697 "funfolding-use-threshold",
698 "funfolding-fun-discount",
699 "funfolding-keeness-factor"
703 %************************************************************************
705 \subsection{Misc functions for command-line options}
707 %************************************************************************
712 startsWith :: String -> String -> Maybe String
713 -- startsWith pfx (pfx++rest) = Just rest
715 startsWith [] str = Just str
716 startsWith (c:cs) (s:ss)
717 = if c /= s then Nothing else startsWith cs ss
718 startsWith _ [] = Nothing
720 endsWith :: String -> String -> Maybe String
722 = case (startsWith (reverse cs) (reverse ss)) of
724 Just rs -> Just (reverse rs)