2 % (c) The University of Glasgow, 1996-2000
4 \section[CmdLineOpts]{Things to do with command-line options}
10 SimplifierSwitch(..), isAmongSimpl,
15 DynFlag(..), -- needed non-abstractly by DriverFlags
26 opt_PprStyle_RawTypes,
34 -- other dynamic flags
40 -- sets of warning opts
46 opt_AutoSccsOnAllToplevs,
47 opt_AutoSccsOnExportedToplevs,
48 opt_AutoSccsOnIndividualCafs,
56 opt_MaxContextReductionDepth,
57 opt_IrrefutableTuples,
61 opt_NoMonomorphismRestriction,
68 opt_LiberateCaseThreshold,
69 opt_StgDoLetNoEscapes,
72 opt_UnboxStrictFields,
73 opt_SimplNoPreInlining,
74 opt_SimplDoEtaReduction,
75 opt_SimplDoLambdaEtaExpansion,
77 opt_SimplExcessPrecision,
80 opt_UF_CreationThreshold,
82 opt_UF_FunAppDiscount,
91 opt_EnsureSplittableC,
96 opt_IgnoreIfacePragmas,
99 opt_OmitInterfacePragmas,
107 #include "HsVersions.h"
109 import Array ( array, (//) )
111 import IOExts ( IORef, readIORef )
112 import Constants -- Default values for some flags
117 import Maybes ( firstJust )
118 import Panic ( panic )
120 #if __GLASGOW_HASKELL__ < 301
121 import ArrBase ( Array(..) )
123 import PrelArr ( Array(..) )
127 %************************************************************************
129 \subsection{Command-line options}
131 %************************************************************************
133 The hsc command-line options are split into two categories:
138 Static flags are represented by top-level values of type Bool or Int,
139 for example. They therefore have the same value throughout the
142 Dynamic flags are represented by an abstract type, DynFlags, which is
143 passed into hsc by the compilation manager for every compilation.
144 Dynamic flags are those that change on a per-compilation basis,
145 perhaps because they may be present in the OPTIONS pragma at the top
148 Other flag-related blurb:
150 A list of {\em ToDo}s is things to be done in a particular part of
151 processing. A (fictitious) example for the Core-to-Core simplifier
152 might be: run the simplifier, then run the strictness analyser, then
153 run the simplifier again (three ``todos'').
155 There are three ``to-do processing centers'' at the moment. In the
156 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
157 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
158 (\tr{simplStg/SimplStg.lhs}).
160 %************************************************************************
162 \subsection{Datatypes associated with command-line options}
164 %************************************************************************
168 = SwBool Bool -- on/off
169 | SwString FAST_STRING -- nothing or a String
170 | SwInt Int -- nothing or an Int
174 data CoreToDo -- These are diff core-to-core passes,
175 -- which may be invoked in any order,
176 -- as many times as you like.
178 = CoreDoSimplify -- The core-to-core simplifier.
179 (SimplifierSwitch -> SwitchResult)
180 -- Each run of the simplifier can take a different
181 -- set of simplifier-specific flags.
183 | CoreDoFloatOutwards Bool -- True <=> float lambdas to top level
188 | CoreDoWorkerWrapper
196 | CoreDoNothing -- useful when building up lists of these things
201 = StgDoMassageForProfiling -- should be (next to) last
202 -- There's also setStgVarInfo, but its absolute "lastness"
203 -- is so critical that it is hardwired in (no flag).
208 data SimplifierSwitch
209 = MaxSimplifierIterations Int
210 | SimplInlinePhase Int
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 stolen_x86_regs :: Int,
302 cmdlineHcIncludes :: [String], -- -#includes
304 -- options for particular phases
315 defaultDynFlags = DynFlags {
316 coreToDo = [], stgToDo = [],
319 hscStubHOutName = "", hscStubCOutName = "",
324 cmdlineHcIncludes = [],
330 flags = standardWarnings,
336 0 | print errors & warnings only
337 1 | minimal verbosity: print "compiling M ... done." for each module.
338 2 | equivalent to -dshow-passes
339 3 | equivalent to existing "ghc -v"
340 4 | "ghc -v -ddump-most"
341 5 | "ghc -v -ddump-all"
344 dopt :: DynFlag -> DynFlags -> Bool
345 dopt f dflags = f `elem` (flags dflags)
347 dopt_CoreToDo :: DynFlags -> [CoreToDo]
348 dopt_CoreToDo = coreToDo
350 dopt_StgToDo :: DynFlags -> [StgToDo]
351 dopt_StgToDo = stgToDo
353 dopt_OutName :: DynFlags -> String
354 dopt_OutName = hscOutName
356 dopt_set :: DynFlags -> DynFlag -> DynFlags
357 dopt_set dfs f = dfs{ flags = f : flags dfs }
359 dopt_unset :: DynFlags -> DynFlag -> DynFlags
360 dopt_unset dfs f = dfs{ flags = filter (/= f) (flags dfs) }
370 dopt_HscLang :: DynFlags -> HscLang
371 dopt_HscLang = hscLang
374 %************************************************************************
376 \subsection{Warnings}
378 %************************************************************************
382 = [ Opt_WarnDeprecations,
383 Opt_WarnOverlappingPatterns,
384 Opt_WarnMissingFields,
385 Opt_WarnMissingMethods,
386 Opt_WarnDuplicateExports
390 = standardWarnings ++
391 [ Opt_WarnUnusedBinds,
392 Opt_WarnUnusedMatches,
393 Opt_WarnUnusedImports,
394 Opt_WarnIncompletePatterns,
400 [ Opt_WarnTypeDefaults,
401 Opt_WarnNameShadowing,
407 %************************************************************************
409 \subsection{Classifying command-line options}
411 %************************************************************************
414 -- v_Statis_hsc_opts is here to avoid a circular dependency with
416 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
418 lookUp :: FAST_STRING -> Bool
419 lookup_int :: String -> Maybe Int
420 lookup_def_int :: String -> Int -> Int
421 lookup_def_float :: String -> Float -> Float
422 lookup_str :: String -> Maybe String
424 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
425 packed_static_opts = map _PK_ unpacked_static_opts
427 lookUp sw = sw `elem` packed_static_opts
429 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
431 lookup_int sw = case (lookup_str sw) of
433 Just xx -> Just (read xx)
435 lookup_def_int sw def = case (lookup_str sw) of
436 Nothing -> def -- Use default
439 lookup_def_float sw def = case (lookup_str sw) of
440 Nothing -> def -- Use default
445 Putting the compiler options into temporary at-files
446 may turn out to be necessary later on if we turn hsc into
447 a pure Win32 application where I think there's a command-line
448 length limit of 255. unpacked_opts understands the @ option.
450 unpacked_opts :: [String]
454 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
456 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
461 %************************************************************************
463 \subsection{Static options}
465 %************************************************************************
469 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
470 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
471 opt_PprStyle_RawTypes = lookUp SLIT("-dppr-rawtypes")
472 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
475 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
476 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
477 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
478 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
479 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
480 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
483 opt_AllStrict = lookUp SLIT("-fall-strict")
484 opt_NoMonomorphismRestriction = lookUp SLIT("-fno-monomorphism-restriction")
485 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
486 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
487 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
488 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
489 opt_Parallel = lookUp SLIT("-fparallel")
490 opt_SMP = lookUp SLIT("-fsmp")
493 opt_NoMethodSharing = lookUp SLIT("-fno-method-sharing")
494 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
495 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
496 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
497 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
498 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
499 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
500 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
503 The optional '-inpackage=P' flag tells what package
504 we are compiling this module for.
505 The Prelude, for example is compiled with '-inpackage std'
507 opt_InPackage = case lookup_str "-inpackage=" of
509 Nothing -> SLIT("Main") -- The package name if none is specified
511 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
512 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
513 opt_GranMacros = lookUp SLIT("-fgransim")
514 opt_HiVersion = read cProjectVersionInt :: Int
515 opt_HistorySize = lookup_def_int "-fhistory-size" 20
516 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
517 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
518 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
519 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
520 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
521 opt_RuntimeTypes = lookUp SLIT("-fruntime-types")
523 -- Simplifier switches
524 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
525 -- NoPreInlining is there just to see how bad things
526 -- get if you don't do it!
527 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
528 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
529 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
530 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
533 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
534 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
535 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
536 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
537 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
539 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
540 opt_UF_DearOp = ( 4 :: Int)
542 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
543 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
544 opt_Static = lookUp SLIT("-static")
545 opt_Unregisterised = lookUp SLIT("-funregisterised")
546 opt_EmitExternalCore = lookUp SLIT("-fext-core")
549 %************************************************************************
551 \subsection{List of static hsc flags}
553 %************************************************************************
558 "fauto-sccs-on-all-toplevs",
559 "fauto-sccs-on-exported-toplevs",
560 "fauto-sccs-on-individual-cafs",
561 "fauto-sccs-on-dicts",
566 "firrefutable-tuples",
573 "funfold-casms-in-hi-file",
575 "funbox-strict-fields",
576 "femit-extern-decls",
577 "fglobalise-toplev-names",
580 "fignore-interface-pragmas",
581 "fno-hi-version-check",
583 "fno-method-sharing",
584 "fno-monomorphism-restriction",
585 "fomit-interface-pragmas",
589 "fdo-lambda-eta-expansion",
592 "funfolding-update-in-place",
599 || any (flip prefixMatch f) [
601 "fliberate-case-threshold",
603 "funfolding-creation-threshold",
604 "funfolding-use-threshold",
605 "funfolding-fun-discount",
606 "funfolding-keeness-factor"
610 %************************************************************************
612 \subsection{Switch ordering}
614 %************************************************************************
616 These things behave just like enumeration types.
619 instance Eq SimplifierSwitch where
620 a == b = tagOf_SimplSwitch a ==# tagOf_SimplSwitch b
622 instance Ord SimplifierSwitch where
623 a < b = tagOf_SimplSwitch a <# tagOf_SimplSwitch b
624 a <= b = tagOf_SimplSwitch a <=# tagOf_SimplSwitch b
627 tagOf_SimplSwitch (SimplInlinePhase _) = _ILIT(1)
628 tagOf_SimplSwitch (MaxSimplifierIterations _) = _ILIT(2)
629 tagOf_SimplSwitch DontApplyRules = _ILIT(3)
630 tagOf_SimplSwitch SimplLetToCase = _ILIT(4)
631 tagOf_SimplSwitch NoCaseOfCase = _ILIT(5)
633 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
635 lAST_SIMPL_SWITCH_TAG = 5
638 %************************************************************************
640 \subsection{Switch lookup}
642 %************************************************************************
645 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
646 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
647 -- in the list; defaults right at the end.
649 tidied_on_switches = foldl rm_dups [] on_switches
650 -- The fold*l* ensures that we keep the latest switches;
651 -- ie the ones that occur earliest in the list.
653 sw_tbl :: Array Int SwitchResult
654 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
658 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
660 defined_elems = map mk_assoc_elem tidied_on_switches
662 -- (avoid some unboxing, bounds checking, and other horrible things:)
663 #if __GLASGOW_HASKELL__ < 405
664 case sw_tbl of { Array bounds_who_needs_'em stuff ->
666 case sw_tbl of { Array _ _ stuff ->
669 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
670 #if __GLASGOW_HASKELL__ < 400
672 #elif __GLASGOW_HASKELL__ < 403
679 mk_assoc_elem k@(MaxSimplifierIterations lvl)
680 = (iBox (tagOf_SimplSwitch k), SwInt lvl)
681 mk_assoc_elem k@(SimplInlinePhase n)
682 = (iBox (tagOf_SimplSwitch k), SwInt n)
684 = (iBox (tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
686 -- cannot have duplicates if we are going to use the array thing
687 rm_dups switches_so_far switch
688 = if switch `is_elem` switches_so_far
690 else switch : switches_so_far
692 sw `is_elem` [] = False
693 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) ==# (tagOf_SimplSwitch s)
698 %************************************************************************
700 \subsection{Misc functions for command-line options}
702 %************************************************************************
706 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
708 switchIsOn lookup_fn switch
709 = case (lookup_fn switch) of
710 SwBool False -> False
713 intSwitchSet :: (switch -> SwitchResult)
717 intSwitchSet lookup_fn switch
718 = case (lookup_fn (switch (panic "intSwitchSet"))) of
719 SwInt int -> Just int
724 startsWith :: String -> String -> Maybe String
725 -- startsWith pfx (pfx++rest) = Just rest
727 startsWith [] str = Just str
728 startsWith (c:cs) (s:ss)
729 = if c /= s then Nothing else startsWith cs ss
730 startsWith _ [] = Nothing
732 endsWith :: String -> String -> Maybe String
734 = case (startsWith (reverse cs) (reverse ss)) of
736 Just rs -> Just (reverse rs)