2 % (c) The University of Glasgow, 1996-2000
4 \section[CmdLineOpts]{Things to do with command-line options}
10 SimplifierSwitch(..), isAmongSimpl,
14 DynFlag(..), -- needed non-abstractly by DriverFlags
31 dopt_D_dump_inlinings,
32 dopt_D_dump_occur_anal,
38 dopt_D_dump_simpl_iterations,
39 dopt_D_dump_simpl_stats,
46 dopt_D_dump_worker_wrapper,
51 dopt_D_dump_minimal_imports,
53 dopt_D_verbose_core2core,
54 dopt_D_verbose_stg2stg,
63 -- other dynamic flags
68 opt_WarnDuplicateExports,
70 opt_WarnIncompletePatterns,
71 opt_WarnMissingFields,
72 opt_WarnMissingMethods,
74 opt_WarnNameShadowing,
75 opt_WarnOverlappingPatterns,
76 opt_WarnSimplePatterns,
79 opt_WarnUnusedImports,
80 opt_WarnUnusedMatches,
84 opt_AutoSccsOnAllToplevs,
85 opt_AutoSccsOnExportedToplevs,
86 opt_AutoSccsOnIndividualCafs,
94 opt_MaxContextReductionDepth,
95 dopt_AllowOverlappingInstances,
96 dopt_AllowUndecidableInstances,
99 opt_IrrefutableTuples,
107 opt_LiberateCaseThreshold,
108 opt_StgDoLetNoEscapes,
111 opt_UnboxStrictFields,
112 opt_SimplNoPreInlining,
113 opt_SimplDoEtaReduction,
114 opt_SimplDoLambdaEtaExpansion,
117 opt_SimplPedanticBottoms,
118 opt_SimplExcessPrecision,
121 opt_UF_HiFileThreshold,
122 opt_UF_CreationThreshold,
124 opt_UF_FunAppDiscount,
125 opt_UF_KeenessFactor,
126 opt_UF_UpdateInPlace,
132 opt_EmitCExternDecls,
133 opt_EnsureSplittableC,
138 opt_IgnoreIfacePragmas,
140 opt_NoImplicitPrelude,
142 opt_OmitInterfacePragmas,
157 #include "HsVersions.h"
159 import Array ( array, (//) )
162 import Constants -- Default values for some flags
165 import Maybes ( firstJust )
166 import Panic ( panic )
168 #if __GLASGOW_HASKELL__ < 301
169 import ArrBase ( Array(..) )
171 import PrelArr ( Array(..) )
175 %************************************************************************
177 \subsection{Command-line options}
179 %************************************************************************
181 The hsc command-line options are split into two categories:
186 Static flags are represented by top-level values of type Bool or Int,
187 for example. They therefore have the same value throughout the
190 Dynamic flags are represented by an abstract type, DynFlags, which is
191 passed into hsc by the compilation manager for every compilation.
192 Dynamic flags are those that change on a per-compilation basis,
193 perhaps because they may be present in the OPTIONS pragma at the top
196 Other flag-related blurb:
198 A list of {\em ToDo}s is things to be done in a particular part of
199 processing. A (fictitious) example for the Core-to-Core simplifier
200 might be: run the simplifier, then run the strictness analyser, then
201 run the simplifier again (three ``todos'').
203 There are three ``to-do processing centers'' at the moment. In the
204 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
205 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
206 (\tr{simplStg/SimplStg.lhs}).
208 %************************************************************************
210 \subsection{Datatypes associated with command-line options}
212 %************************************************************************
216 = SwBool Bool -- on/off
217 | SwString FAST_STRING -- nothing or a String
218 | SwInt Int -- nothing or an Int
222 data CoreToDo -- These are diff core-to-core passes,
223 -- which may be invoked in any order,
224 -- as many times as you like.
226 = CoreDoSimplify -- The core-to-core simplifier.
227 (SimplifierSwitch -> SwitchResult)
228 -- Each run of the simplifier can take a different
229 -- set of simplifier-specific flags.
231 | CoreDoFloatOutwards Bool -- True <=> float lambdas to top level
236 | CoreDoWorkerWrapper
243 | CoreDoNothing -- useful when building up lists of these things
250 | StgDoMassageForProfiling -- should be (next to) last
251 -- There's also setStgVarInfo, but its absolute "lastness"
252 -- is so critical that it is hardwired in (no flag).
257 data SimplifierSwitch
258 = MaxSimplifierIterations Int
259 | SimplInlinePhase Int
265 %************************************************************************
267 \subsection{Dynamic command-line options}
269 %************************************************************************
284 | Opt_D_dump_inlinings
285 | Opt_D_dump_occur_anal
290 | Opt_D_dump_simpl_iterations
299 | Opt_D_dump_worker_wrapper
301 | Opt_D_dump_rn_trace
302 | Opt_D_dump_rn_stats
304 | Opt_D_dump_simpl_stats
306 | Opt_D_verbose_core2core
307 | Opt_D_verbose_stg2stg
308 | Opt_D_dump_hi_diffs
309 | Opt_D_dump_minimal_imports
315 | Opt_AllowOverlappingInstances
316 | Opt_AllowUndecidableInstances
320 data DynFlags = DynFlags {
321 coreToDo :: CoreToDo,
327 boolOpt :: DynFlag -> DynFlags -> Bool
328 boolOpt f dflags = f `elem` (flags dflags)
330 dopt_D_dump_all = boolOpt Opt_D_dump_all
331 dopt_D_dump_most = boolOpt Opt_D_dump_most
332 dopt_D_dump_absC = boolOpt Opt_D_dump_absC
333 dopt_D_dump_asm = boolOpt Opt_D_dump_asm
334 dopt_D_dump_cpranal = boolOpt Opt_D_dump_cpranal
335 dopt_D_dump_deriv = boolOpt Opt_D_dump_deriv
336 dopt_D_dump_ds = boolOpt Opt_D_dump_ds
337 dopt_D_dump_flatC = boolOpt Opt_D_dump_flatC
338 dopt_D_dump_foreign = boolOpt Opt_D_dump_foreign
339 dopt_D_dump_inlinings = boolOpt Opt_D_dump_inlinings
340 dopt_D_dump_occur_anal = boolOpt Opt_D_dump_occur_anal
341 dopt_D_dump_parsed = boolOpt Opt_D_dump_parsed
342 dopt_D_dump_realC = boolOpt Opt_D_dump_realC
343 dopt_D_dump_rn = boolOpt Opt_D_dump_rn
344 dopt_D_dump_simpl = boolOpt Opt_D_dump_simpl
345 dopt_D_dump_simpl_iterations = boolOpt Opt_D_dump_simpl_iterations
346 dopt_D_dump_spec = boolOpt Opt_D_dump_spec
347 dopt_D_dump_stg = boolOpt Opt_D_dump_stg
348 dopt_D_dump_stranal = boolOpt Opt_D_dump_stranal
349 dopt_D_dump_tc = boolOpt Opt_D_dump_tc
350 dopt_D_dump_types = boolOpt Opt_D_dump_types
351 dopt_D_dump_rules = boolOpt Opt_D_dump_rules
352 dopt_D_dump_usagesp = boolOpt Opt_D_dump_usagesp
353 dopt_D_dump_cse = boolOpt Opt_D_dump_cse
354 dopt_D_dump_worker_wrapper = boolOpt Opt_D_dump_worker_wrapper
355 dopt_D_show_passes = boolOpt Opt_D_show_passes
356 dopt_D_dump_rn_trace = boolOpt Opt_D_dump_rn_trace
357 dopt_D_dump_rn_stats = boolOpt Opt_D_dump_rn_stats
358 dopt_D_dump_stix = boolOpt Opt_D_dump_stix
359 dopt_D_dump_simpl_stats = boolOpt Opt_D_dump_simpl_stats
360 dopt_D_source_stats = boolOpt Opt_D_source_stats
361 dopt_D_verbose_core2core = boolOpt Opt_D_verbose_core2core
362 dopt_D_verbose_stg2stg = boolOpt Opt_D_verbose_stg2stg
363 dopt_D_dump_hi_diffs = boolOpt Opt_D_dump_hi_diffs
364 dopt_D_dump_minimal_imports = boolOpt Opt_D_dump_minimal_imports
365 dopt_DoCoreLinting = boolOpt Opt_DoCoreLinting
366 dopt_DoStgLinting = boolOpt Opt_DoStgLinting
367 dopt_DoUSPLinting = boolOpt Opt_DoUSPLinting
369 dopt_AllowOverlappingInstances = boolOpt Opt_AllowOverlappingInstances
370 dopt_AllowUndecidableInstances = boolOpt Opt_AllowUndecidableInstances
371 dopt_GlasgowExts = boolOpt Opt_GlasgowExts
373 dopt_CoreToDo :: DynFlags -> CoreToDo
374 dopt_CoreToDo = coreToDo
376 dopt_StgToDo :: DynFlags -> StgToDo
377 dopt_StgToDo = stgToDo
385 dopt_HscLang :: DynFlags -> HscLang
386 dopt_HscLang = hscLang
389 %************************************************************************
391 \subsection{Classifying command-line options}
393 %************************************************************************
396 lookUp :: FAST_STRING -> Bool
397 lookup_int :: String -> Maybe Int
398 lookup_def_int :: String -> Int -> Int
399 lookup_def_float :: String -> Float -> Float
400 lookup_str :: String -> Maybe String
402 lookUp sw = sw `elem` argv
404 lookup_str sw = firstJust (map (startsWith sw) unpacked_opts)
406 lookup_int sw = case (lookup_str sw) of
408 Just xx -> Just (read xx)
410 lookup_def_int sw def = case (lookup_str sw) of
411 Nothing -> def -- Use default
414 lookup_def_char sw def = case (lookup_str sw) of
416 _ -> def -- Use default
418 lookup_def_float sw def = case (lookup_str sw) of
419 Nothing -> def -- Use default
422 unpacked_opts = map _UNPK_ argv
425 Putting the compiler options into temporary at-files
426 may turn out to be necessary later on if we turn hsc into
427 a pure Win32 application where I think there's a command-line
428 length limit of 255. unpacked_opts understands the @ option.
430 unpacked_opts :: [String]
436 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
441 %************************************************************************
443 \subsection{Static options}
445 %************************************************************************
449 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
450 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
451 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
454 opt_WarnDuplicateExports = lookUp SLIT("-fwarn-duplicate-exports")
455 opt_WarnHiShadows = lookUp SLIT("-fwarn-hi-shadowing")
456 opt_WarnIncompletePatterns = lookUp SLIT("-fwarn-incomplete-patterns")
457 opt_WarnMissingFields = lookUp SLIT("-fwarn-missing-fields")
458 opt_WarnMissingMethods = lookUp SLIT("-fwarn-missing-methods")
459 opt_WarnMissingSigs = lookUp SLIT("-fwarn-missing-signatures")
460 opt_WarnNameShadowing = lookUp SLIT("-fwarn-name-shadowing")
461 opt_WarnOverlappingPatterns = lookUp SLIT("-fwarn-overlapping-patterns")
462 opt_WarnSimplePatterns = lookUp SLIT("-fwarn-simple-patterns")
463 opt_WarnTypeDefaults = lookUp SLIT("-fwarn-type-defaults")
464 opt_WarnUnusedBinds = lookUp SLIT("-fwarn-unused-binds")
465 opt_WarnUnusedImports = lookUp SLIT("-fwarn-unused-imports")
466 opt_WarnUnusedMatches = lookUp SLIT("-fwarn-unused-matches")
467 opt_WarnDeprecations = lookUp SLIT("-fwarn-deprecations")
470 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
471 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
472 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
473 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
474 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
475 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
478 opt_AllStrict = lookUp SLIT("-fall-strict")
479 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
480 opt_Generics = lookUp SLIT("-fgenerics")
481 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
482 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
483 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
484 opt_Parallel = lookUp SLIT("-fparallel")
485 opt_SMP = lookUp SLIT("-fsmp")
488 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
489 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
490 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
491 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
492 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
493 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
494 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
497 The optional '-inpackage=P' flag tells what package
498 we are compiling this module for.
499 The Prelude, for example is compiled with '-package prelude'
501 opt_InPackage = case lookup_str "-inpackage=" of
503 Nothing -> SLIT("Main") -- The package name if none is specified
505 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
506 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
507 opt_GranMacros = lookUp SLIT("-fgransim")
508 opt_HiVersion = lookup_def_int "-fhi-version=" 0 -- what version we're compiling.
509 opt_HistorySize = lookup_def_int "-fhistory-size" 20
510 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
511 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
512 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
513 opt_NoImplicitPrelude = lookUp SLIT("-fno-implicit-prelude")
514 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
515 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
517 -- Simplifier switches
518 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
519 -- NoPreInlining is there just to see how bad things
520 -- get if you don't do it!
521 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
522 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
523 opt_SimplCaseOfCase = lookUp SLIT("-fcase-of-case")
524 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
525 opt_SimplPedanticBottoms = lookUp SLIT("-fpedantic-bottoms")
526 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
529 opt_UF_HiFileThreshold = lookup_def_int "-funfolding-interface-threshold" (45::Int)
530 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
531 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
532 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
533 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
534 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
536 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
537 opt_UF_DearOp = ( 4 :: Int)
539 opt_ReportCompile = lookUp SLIT("-freport-compile")
540 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
541 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
542 opt_Static = lookUp SLIT("-static")
543 opt_Unregisterised = lookUp SLIT("-funregisterised")
544 opt_Verbose = lookUp SLIT("-v")
546 opt_UseVanillaRegs | opt_Unregisterised = 0
547 | otherwise = mAX_Real_Vanilla_REG
548 opt_UseFloatRegs | opt_Unregisterised = 0
549 | otherwise = mAX_Real_Float_REG
550 opt_UseDoubleRegs | opt_Unregisterised = 0
551 | otherwise = mAX_Real_Double_REG
552 opt_UseLongRegs | opt_Unregisterised = 0
553 | otherwise = mAX_Real_Long_REG
556 %************************************************************************
558 \subsection{List of static hsc flags}
560 %************************************************************************
565 "-fwarn-duplicate-exports",
566 "-fwarn-hi-shadowing",
567 "-fwarn-incomplete-patterns",
568 "-fwarn-missing-fields",
569 "-fwarn-missing-methods",
570 "-fwarn-missing-signatures",
571 "-fwarn-name-shadowing",
572 "-fwarn-overlapping-patterns",
573 "-fwarn-simple-patterns",
574 "-fwarn-type-defaults",
575 "-fwarn-unused-binds",
576 "-fwarn-unused-imports",
577 "-fwarn-unused-matches",
578 "-fwarn-deprecations",
579 "-fauto-sccs-on-all-toplevs",
580 "-fauto-sccs-on-exported-toplevs",
581 "-fauto-sccs-on-individual-cafs",
582 "-fauto-sccs-on-dicts",
588 "-firrefutable-tuples",
595 "-funfold-casms-in-hi-file",
597 "-funbox-strict-fields",
598 "-femit-extern-decls",
599 "-fglobalise-toplev-names",
602 "-fignore-interface-pragmas",
603 "-fno-hi-version-check",
604 "-fno-implicit-prelude",
606 "-fomit-interface-pragmas",
608 "-fdo-eta-reduction",
609 "-fdo-lambda-eta-expansion",
612 "-fpedantic-bottoms",
613 "-fexcess-precision",
614 "-funfolding-update-in-place",
617 "-fno-prune-tydecls",
621 || any (flip prefixMatch f) [
623 "-fliberate-case-threshold",
626 "-funfolding-interface-threshold",
627 "-funfolding-creation-threshold",
628 "-funfolding-use-threshold",
629 "-funfolding-fun-discount",
630 "-funfolding-keeness-factor"
634 %************************************************************************
636 \subsection{Switch ordering}
638 %************************************************************************
640 These things behave just like enumeration types.
643 instance Eq SimplifierSwitch where
644 a == b = tagOf_SimplSwitch a _EQ_ tagOf_SimplSwitch b
646 instance Ord SimplifierSwitch where
647 a < b = tagOf_SimplSwitch a _LT_ tagOf_SimplSwitch b
648 a <= b = tagOf_SimplSwitch a _LE_ tagOf_SimplSwitch b
651 tagOf_SimplSwitch (SimplInlinePhase _) = ILIT(1)
652 tagOf_SimplSwitch (MaxSimplifierIterations _) = ILIT(2)
653 tagOf_SimplSwitch DontApplyRules = ILIT(3)
654 tagOf_SimplSwitch SimplLetToCase = ILIT(4)
655 tagOf_SimplSwitch NoCaseOfCase = ILIT(5)
657 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
659 lAST_SIMPL_SWITCH_TAG = 5
662 %************************************************************************
664 \subsection{Switch lookup}
666 %************************************************************************
669 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
670 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
671 -- in the list; defaults right at the end.
673 tidied_on_switches = foldl rm_dups [] on_switches
674 -- The fold*l* ensures that we keep the latest switches;
675 -- ie the ones that occur earliest in the list.
677 sw_tbl :: Array Int SwitchResult
678 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
682 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
684 defined_elems = map mk_assoc_elem tidied_on_switches
686 -- (avoid some unboxing, bounds checking, and other horrible things:)
687 #if __GLASGOW_HASKELL__ < 405
688 case sw_tbl of { Array bounds_who_needs_'em stuff ->
690 case sw_tbl of { Array _ _ stuff ->
693 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
694 #if __GLASGOW_HASKELL__ < 400
696 #elif __GLASGOW_HASKELL__ < 403
703 mk_assoc_elem k@(MaxSimplifierIterations lvl) = (IBOX(tagOf_SimplSwitch k), SwInt lvl)
704 mk_assoc_elem k@(SimplInlinePhase n) = (IBOX(tagOf_SimplSwitch k), SwInt n)
705 mk_assoc_elem k = (IBOX(tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
707 -- cannot have duplicates if we are going to use the array thing
708 rm_dups switches_so_far switch
709 = if switch `is_elem` switches_so_far
711 else switch : switches_so_far
713 sw `is_elem` [] = False
714 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) _EQ_ (tagOf_SimplSwitch s)
718 Default settings for simplifier switches
721 defaultSimplSwitches = [MaxSimplifierIterations 1]
724 %************************************************************************
726 \subsection{Misc functions for command-line options}
728 %************************************************************************
732 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
734 switchIsOn lookup_fn switch
735 = case (lookup_fn switch) of
736 SwBool False -> False
739 intSwitchSet :: (switch -> SwitchResult)
743 intSwitchSet lookup_fn switch
744 = case (lookup_fn (switch (panic "intSwitchSet"))) of
745 SwInt int -> Just int
750 startsWith :: String -> String -> Maybe String
751 -- startsWith pfx (pfx++rest) = Just rest
753 startsWith [] str = Just str
754 startsWith (c:cs) (s:ss)
755 = if c /= s then Nothing else startsWith cs ss
756 startsWith _ [] = Nothing
758 endsWith :: String -> String -> Maybe String
760 = case (startsWith (reverse cs) (reverse ss)) of
762 Just rs -> Just (reverse rs)