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
340 | cGhcWithNativeCodeGen == "YES" &&
341 (prefixMatch "i386" cTARGETPLATFORM ||
342 prefixMatch "sparc" cTARGETPLATFORM) = HscAsm
345 defaultDynFlags = DynFlags {
346 coreToDo = [], stgToDo = [],
347 hscLang = defaultHscLang,
349 hscStubHOutName = "", hscStubCOutName = "",
355 cmdlineHcIncludes = [],
366 flags = standardWarnings,
372 0 | print errors & warnings only
373 1 | minimal verbosity: print "compiling M ... done." for each module.
374 2 | equivalent to -dshow-passes
375 3 | equivalent to existing "ghc -v"
376 4 | "ghc -v -ddump-most"
377 5 | "ghc -v -ddump-all"
380 dopt :: DynFlag -> DynFlags -> Bool
381 dopt f dflags = f `elem` (flags dflags)
383 dopt_CoreToDo :: DynFlags -> [CoreToDo]
384 dopt_CoreToDo = coreToDo
386 dopt_StgToDo :: DynFlags -> [StgToDo]
387 dopt_StgToDo = stgToDo
389 dopt_OutName :: DynFlags -> String
390 dopt_OutName = hscOutName
392 dopt_HscLang :: DynFlags -> HscLang
393 dopt_HscLang = hscLang
395 dopt_set :: DynFlags -> DynFlag -> DynFlags
396 dopt_set dfs f = dfs{ flags = f : flags dfs }
398 dopt_unset :: DynFlags -> DynFlag -> DynFlags
399 dopt_unset dfs f = dfs{ flags = filter (/= f) (flags dfs) }
401 getOpts :: (DynFlags -> [a]) -> IO [a]
402 -- We add to the options from the front, so we need to reverse the list
403 getOpts opts = dynFlag opts >>= return . reverse
405 -- we can only switch between HscC, HscAsmm, and HscILX with dynamic flags
406 -- (-fvia-C, -fasm, -filx respectively).
407 setLang l = updDynFlags (\ dfs -> case hscLang dfs of
408 HscC -> dfs{ hscLang = l }
409 HscAsm -> dfs{ hscLang = l }
410 HscILX -> dfs{ hscLang = l }
414 verb <- dynFlag verbosity
415 if verb >= 3 then return "-v" else return ""
418 -----------------------------------------------------------------------------
419 -- Mess about with the mutable variables holding the dynamic arguments
422 -- is the "baseline" dynamic flags, initialised from
423 -- the defaults and command line options, and updated by the
424 -- ':s' command in GHCi.
427 -- is the dynamic flags for the current compilation. It is reset
428 -- to the value of v_InitDynFlags before each compilation, then
429 -- updated by reading any OPTIONS pragma in the current module.
432 GLOBAL_VAR(v_InitDynFlags, defaultDynFlags, DynFlags)
433 GLOBAL_VAR(v_DynFlags, defaultDynFlags, DynFlags)
435 setDynFlags :: DynFlags -> IO ()
436 setDynFlags dfs = writeIORef v_DynFlags dfs
438 saveDynFlags :: IO ()
439 saveDynFlags = do dfs <- readIORef v_DynFlags
440 writeIORef v_InitDynFlags dfs
442 restoreDynFlags :: IO DynFlags
443 restoreDynFlags = do dfs <- readIORef v_InitDynFlags
444 writeIORef v_DynFlags dfs
447 getDynFlags :: IO DynFlags
448 getDynFlags = readIORef v_DynFlags
450 updDynFlags :: (DynFlags -> DynFlags) -> IO ()
451 updDynFlags f = do dfs <- readIORef v_DynFlags
452 writeIORef v_DynFlags (f dfs)
454 dynFlag :: (DynFlags -> a) -> IO a
455 dynFlag f = do dflags <- readIORef v_DynFlags; return (f dflags)
457 setDynFlag, unSetDynFlag :: DynFlag -> IO ()
458 setDynFlag f = updDynFlags (\dfs -> dopt_set dfs f)
459 unSetDynFlag f = updDynFlags (\dfs -> dopt_unset dfs f)
463 %************************************************************************
465 \subsection{Warnings}
467 %************************************************************************
471 = [ Opt_WarnDeprecations,
472 Opt_WarnOverlappingPatterns,
473 Opt_WarnMissingFields,
474 Opt_WarnMissingMethods,
475 Opt_WarnDuplicateExports
479 = standardWarnings ++
480 [ Opt_WarnUnusedBinds,
481 Opt_WarnUnusedMatches,
482 Opt_WarnUnusedImports,
483 Opt_WarnIncompletePatterns,
489 [ Opt_WarnTypeDefaults,
490 Opt_WarnNameShadowing,
496 %************************************************************************
498 \subsection{Classifying command-line options}
500 %************************************************************************
503 -- v_Statis_hsc_opts is here to avoid a circular dependency with
505 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
507 lookUp :: FastString -> Bool
508 lookup_int :: String -> Maybe Int
509 lookup_def_int :: String -> Int -> Int
510 lookup_def_float :: String -> Float -> Float
511 lookup_str :: String -> Maybe String
513 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
514 packed_static_opts = map mkFastString unpacked_static_opts
516 lookUp sw = sw `elem` packed_static_opts
518 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
520 lookup_int sw = case (lookup_str sw) of
522 Just xx -> Just (read xx)
524 lookup_def_int sw def = case (lookup_str sw) of
525 Nothing -> def -- Use default
528 lookup_def_float sw def = case (lookup_str sw) of
529 Nothing -> def -- Use default
534 Putting the compiler options into temporary at-files
535 may turn out to be necessary later on if we turn hsc into
536 a pure Win32 application where I think there's a command-line
537 length limit of 255. unpacked_opts understands the @ option.
539 unpacked_opts :: [String]
543 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
545 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
550 %************************************************************************
552 \subsection{Static options}
554 %************************************************************************
558 opt_PprStyle_NoPrags = lookUp FSLIT("-dppr-noprags")
559 opt_PprStyle_Debug = lookUp FSLIT("-dppr-debug")
560 opt_PprStyle_RawTypes = lookUp FSLIT("-dppr-rawtypes")
561 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
564 opt_AutoSccsOnAllToplevs = lookUp FSLIT("-fauto-sccs-on-all-toplevs")
565 opt_AutoSccsOnExportedToplevs = lookUp FSLIT("-fauto-sccs-on-exported-toplevs")
566 opt_AutoSccsOnIndividualCafs = lookUp FSLIT("-fauto-sccs-on-individual-cafs")
567 opt_AutoSccsOnDicts = lookUp FSLIT("-fauto-sccs-on-dicts")
568 opt_SccProfilingOn = lookUp FSLIT("-fscc-profiling")
569 opt_DoTickyProfiling = lookUp FSLIT("-fticky-ticky")
572 opt_AllStrict = lookUp FSLIT("-fall-strict")
573 opt_DictsStrict = lookUp FSLIT("-fdicts-strict")
574 opt_IrrefutableTuples = lookUp FSLIT("-firrefutable-tuples")
575 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
576 opt_NumbersStrict = lookUp FSLIT("-fnumbers-strict")
577 opt_Parallel = lookUp FSLIT("-fparallel")
578 opt_SMP = lookUp FSLIT("-fsmp")
579 opt_Flatten = lookUp FSLIT("-fflatten")
582 opt_NoMethodSharing = lookUp FSLIT("-fno-method-sharing")
583 opt_DoSemiTagging = lookUp FSLIT("-fsemi-tagging")
584 opt_FoldrBuildOn = lookUp FSLIT("-ffoldr-build-on")
585 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
586 opt_StgDoLetNoEscapes = lookUp FSLIT("-flet-no-escape")
587 opt_UnfoldCasms = lookUp FSLIT("-funfold-casms-in-hi-file")
588 opt_UsageSPOn = lookUp FSLIT("-fusagesp-on")
589 opt_UnboxStrictFields = lookUp FSLIT("-funbox-strict-fields")
590 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
593 The optional '-inpackage=P' flag tells what package
594 we are compiling this module for.
595 The Prelude, for example is compiled with '-inpackage std'
597 opt_InPackage = case lookup_str "-inpackage=" of
599 Nothing -> FSLIT("Main") -- The package name if none is specified
601 opt_EmitCExternDecls = lookUp FSLIT("-femit-extern-decls")
602 opt_EnsureSplittableC = lookUp FSLIT("-fglobalise-toplev-names")
603 opt_GranMacros = lookUp FSLIT("-fgransim")
604 opt_HiVersion = read (cProjectVersionInt ++ cProjectPatchLevel) :: Int
605 opt_HistorySize = lookup_def_int "-fhistory-size" 20
606 opt_IgnoreAsserts = lookUp FSLIT("-fignore-asserts")
607 opt_IgnoreIfacePragmas = lookUp FSLIT("-fignore-interface-pragmas")
608 opt_NoHiCheck = lookUp FSLIT("-fno-hi-version-check")
609 opt_OmitBlackHoling = lookUp FSLIT("-dno-black-holing")
610 opt_OmitInterfacePragmas = lookUp FSLIT("-fomit-interface-pragmas")
611 opt_RuntimeTypes = lookUp FSLIT("-fruntime-types")
613 -- Simplifier switches
614 opt_SimplNoPreInlining = lookUp FSLIT("-fno-pre-inlining")
615 -- NoPreInlining is there just to see how bad things
616 -- get if you don't do it!
617 opt_SimplDoEtaReduction = lookUp FSLIT("-fdo-eta-reduction")
618 opt_SimplDoLambdaEtaExpansion = lookUp FSLIT("-fdo-lambda-eta-expansion")
619 opt_SimplCaseMerge = lookUp FSLIT("-fcase-merge")
620 opt_SimplExcessPrecision = lookUp FSLIT("-fexcess-precision")
623 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
624 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
625 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
626 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
627 opt_UF_UpdateInPlace = lookUp FSLIT("-funfolding-update-in-place")
629 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
630 opt_UF_DearOp = ( 4 :: Int)
632 opt_NoPruneDecls = lookUp FSLIT("-fno-prune-decls")
633 opt_NoPruneTyDecls = lookUp FSLIT("-fno-prune-tydecls")
634 opt_Static = lookUp FSLIT("-static")
635 opt_Unregisterised = lookUp FSLIT("-funregisterised")
636 opt_EmitExternalCore = lookUp FSLIT("-fext-core")
639 %************************************************************************
641 \subsection{List of static hsc flags}
643 %************************************************************************
648 "fauto-sccs-on-all-toplevs",
649 "fauto-sccs-on-exported-toplevs",
650 "fauto-sccs-on-individual-cafs",
651 "fauto-sccs-on-dicts",
656 "firrefutable-tuples",
664 "funfold-casms-in-hi-file",
666 "funbox-strict-fields",
667 "femit-extern-decls",
668 "fglobalise-toplev-names",
671 "fignore-interface-pragmas",
672 "fno-hi-version-check",
674 "fno-method-sharing",
675 "fomit-interface-pragmas",
679 "fdo-lambda-eta-expansion",
682 "funfolding-update-in-place",
690 || any (flip prefixMatch f) [
692 "fliberate-case-threshold",
695 "funfolding-creation-threshold",
696 "funfolding-use-threshold",
697 "funfolding-fun-discount",
698 "funfolding-keeness-factor"
702 %************************************************************************
704 \subsection{Misc functions for command-line options}
706 %************************************************************************
711 startsWith :: String -> String -> Maybe String
712 -- startsWith pfx (pfx++rest) = Just rest
714 startsWith [] str = Just str
715 startsWith (c:cs) (s:ss)
716 = if c /= s then Nothing else startsWith cs ss
717 startsWith _ [] = Nothing
719 endsWith :: String -> String -> Maybe String
721 = case (startsWith (reverse cs) (reverse ss)) of
723 Just rs -> Just (reverse rs)