2 % (c) The University of Glasgow, 1996-2000
4 \section[CmdLineOpts]{Things to do with command-line options}
14 DynFlag(..), -- needed non-abstractly by Main
29 dopt_D_dump_inlinings,
30 dopt_D_dump_occur_anal,
36 dopt_D_dump_simpl_iterations,
37 dopt_D_dump_simpl_stats,
44 dopt_D_dump_worker_wrapper,
49 dopt_D_dump_minimal_imports,
51 dopt_D_verbose_core2core,
52 dopt_D_verbose_stg2stg,
61 -- other dynamic flags
66 opt_WarnDuplicateExports,
68 opt_WarnIncompletePatterns,
69 opt_WarnMissingFields,
70 opt_WarnMissingMethods,
72 opt_WarnNameShadowing,
73 opt_WarnOverlappingPatterns,
74 opt_WarnSimplePatterns,
77 opt_WarnUnusedImports,
78 opt_WarnUnusedMatches,
82 opt_AutoSccsOnAllToplevs,
83 opt_AutoSccsOnExportedToplevs,
84 opt_AutoSccsOnIndividualCafs,
92 opt_MaxContextReductionDepth,
93 dopt_AllowOverlappingInstances,
94 dopt_AllowUndecidableInstances,
97 opt_IrrefutableTuples,
105 opt_LiberateCaseThreshold,
106 opt_StgDoLetNoEscapes,
109 opt_UnboxStrictFields,
110 opt_SimplNoPreInlining,
111 opt_SimplDoEtaReduction,
112 opt_SimplDoLambdaEtaExpansion,
115 opt_SimplPedanticBottoms,
116 opt_SimplExcessPrecision,
119 opt_UF_HiFileThreshold,
120 opt_UF_CreationThreshold,
122 opt_UF_FunAppDiscount,
123 opt_UF_KeenessFactor,
124 opt_UF_UpdateInPlace,
130 opt_EmitCExternDecls,
131 opt_EnsureSplittableC,
136 opt_IgnoreIfacePragmas,
138 opt_NoImplicitPrelude,
140 opt_OmitInterfacePragmas,
155 #include "HsVersions.h"
157 import Array ( array, (//) )
160 import Constants -- Default values for some flags
162 import Maybes ( firstJust )
163 import Panic ( panic )
165 #if __GLASGOW_HASKELL__ < 301
166 import ArrBase ( Array(..) )
168 import PrelArr ( Array(..) )
172 %************************************************************************
174 \subsection{Command-line options}
176 %************************************************************************
178 The hsc command-line options are split into two categories:
183 Static flags are represented by top-level values of type Bool or Int,
184 for example. They therefore have the same value throughout the
187 Dynamic flags are represented by an abstract type, DynFlags, which is
188 passed into hsc by the compilation manager for every compilation.
189 Dynamic flags are those that change on a per-compilation basis,
190 perhaps because they may be present in the OPTIONS pragma at the top
193 Other flag-related blurb:
195 A list of {\em ToDo}s is things to be done in a particular part of
196 processing. A (fictitious) example for the Core-to-Core simplifier
197 might be: run the simplifier, then run the strictness analyser, then
198 run the simplifier again (three ``todos'').
200 There are three ``to-do processing centers'' at the moment. In the
201 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
202 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
203 (\tr{simplStg/SimplStg.lhs}).
205 %************************************************************************
207 \subsection{Datatypes associated with command-line options}
209 %************************************************************************
213 = SwBool Bool -- on/off
214 | SwString FAST_STRING -- nothing or a String
215 | SwInt Int -- nothing or an Int
219 data CoreToDo -- These are diff core-to-core passes,
220 -- which may be invoked in any order,
221 -- as many times as you like.
223 = CoreDoSimplify -- The core-to-core simplifier.
224 (SimplifierSwitch -> SwitchResult)
225 -- Each run of the simplifier can take a different
226 -- set of simplifier-specific flags.
228 | CoreDoFloatOutwards Bool -- True <=> float lambdas to top level
233 | CoreDoWorkerWrapper
245 | StgDoMassageForProfiling -- should be (next to) last
246 -- There's also setStgVarInfo, but its absolute "lastness"
247 -- is so critical that it is hardwired in (no flag).
252 data SimplifierSwitch
253 = MaxSimplifierIterations Int
254 | SimplInlinePhase Int
260 %************************************************************************
262 \subsection{Dynamic command-line options}
264 %************************************************************************
279 | Opt_D_dump_inlinings
280 | Opt_D_dump_occur_anal
285 | Opt_D_dump_simpl_iterations
294 | Opt_D_dump_worker_wrapper
296 | Opt_D_dump_rn_trace
297 | Opt_D_dump_rn_stats
299 | Opt_D_dump_simpl_stats
301 | Opt_D_verbose_core2core
302 | Opt_D_verbose_stg2stg
303 | Opt_D_dump_hi_diffs
304 | Opt_D_dump_minimal_imports
310 | Opt_AllowOverlappingInstances
311 | Opt_AllowUndecidableInstances
315 data DynFlags = DynFlags {
316 coreToDo :: CoreToDo,
322 boolOpt :: DynFlag -> DynFlags -> Bool
323 boolOpt f dflags = f `elem` (flags dflags)
325 dopt_D_dump_all = boolOpt Opt_D_dump_all
326 dopt_D_dump_most = boolOpt Opt_D_dump_most
327 dopt_D_dump_absC = boolOpt Opt_D_dump_absC
328 dopt_D_dump_asm = boolOpt Opt_D_dump_asm
329 dopt_D_dump_cpranal = boolOpt Opt_D_dump_cpranal
330 dopt_D_dump_deriv = boolOpt Opt_D_dump_deriv
331 dopt_D_dump_ds = boolOpt Opt_D_dump_ds
332 dopt_D_dump_flatC = boolOpt Opt_D_dump_flatC
333 dopt_D_dump_foreign = boolOpt Opt_D_dump_foreign
334 dopt_D_dump_inlinings = boolOpt Opt_D_dump_inlinings
335 dopt_D_dump_occur_anal = boolOpt Opt_D_dump_occur_anal
336 dopt_D_dump_parsed = boolOpt Opt_D_dump_parsed
337 dopt_D_dump_realC = boolOpt Opt_D_dump_realC
338 dopt_D_dump_rn = boolOpt Opt_D_dump_rn
339 dopt_D_dump_simpl = boolOpt Opt_D_dump_simpl
340 dopt_D_dump_simpl_iterations = boolOpt Opt_D_dump_simpl_iterations
341 dopt_D_dump_spec = boolOpt Opt_D_dump_spec
342 dopt_D_dump_stg = boolOpt Opt_D_dump_stg
343 dopt_D_dump_stranal = boolOpt Opt_D_dump_stranal
344 dopt_D_dump_tc = boolOpt Opt_D_dump_tc
345 dopt_D_dump_types = boolOpt Opt_D_dump_types
346 dopt_D_dump_rules = boolOpt Opt_D_dump_rules
347 dopt_D_dump_usagesp = boolOpt Opt_D_dump_usagesp
348 dopt_D_dump_cse = boolOpt Opt_D_dump_cse
349 dopt_D_dump_worker_wrapper = boolOpt Opt_D_dump_worker_wrapper
350 dopt_D_show_passes = boolOpt Opt_D_show_passes
351 dopt_D_dump_rn_trace = boolOpt Opt_D_dump_rn_trace
352 dopt_D_dump_rn_stats = boolOpt Opt_D_dump_rn_stats
353 dopt_D_dump_stix = boolOpt Opt_D_dump_stix
354 dopt_D_dump_simpl_stats = boolOpt Opt_D_dump_simpl_stats
355 dopt_D_source_stats = boolOpt Opt_D_source_stats
356 dopt_D_verbose_core2core = boolOpt Opt_D_verbose_core2core
357 dopt_D_verbose_stg2stg = boolOpt Opt_D_verbose_stg2stg
358 dopt_D_dump_hi_diffs = boolOpt Opt_D_dump_hi_diffs
359 dopt_D_dump_minimal_imports = boolOpt Opt_D_dump_minimal_imports
360 dopt_DoCoreLinting = boolOpt Opt_DoCoreLinting
361 dopt_DoStgLinting = boolOpt Opt_DoStgLinting
362 dopt_DoUSPLinting = boolOpt Opt_DoUSPLinting
364 dopt_AllowOverlappingInstances = boolOpt Opt_AllowOverlappingInstances
365 dopt_AllowUndecidableInstances = boolOpt Opt_AllowUndecidableInstances
366 dopt_GlasgowExts = boolOpt Opt_GlasgowExts
368 dopt_CoreToDo :: DynFlags -> CoreToDo
369 dopt_CoreToDo = coreToDo
371 dopt_StgToDo :: DynFlags -> StgToDo
372 dopt_StgToDo = stgToDo
380 dopt_HscLang :: DynFlags -> HscLang
381 dopt_HscLang = hscLang
384 %************************************************************************
386 \subsection{Classifying command-line options}
388 %************************************************************************
391 lookUp :: FAST_STRING -> Bool
392 lookup_int :: String -> Maybe Int
393 lookup_def_int :: String -> Int -> Int
394 lookup_def_float :: String -> Float -> Float
395 lookup_str :: String -> Maybe String
397 lookUp sw = sw `elem` argv
399 lookup_str sw = firstJust (map (startsWith sw) unpacked_opts)
401 lookup_int sw = case (lookup_str sw) of
403 Just xx -> Just (read xx)
405 lookup_def_int sw def = case (lookup_str sw) of
406 Nothing -> def -- Use default
409 lookup_def_char sw def = case (lookup_str sw) of
411 _ -> def -- Use default
413 lookup_def_float sw def = case (lookup_str sw) of
414 Nothing -> def -- Use default
417 unpacked_opts = map _UNPK_ argv
420 Putting the compiler options into temporary at-files
421 may turn out to be necessary later on if we turn hsc into
422 a pure Win32 application where I think there's a command-line
423 length limit of 255. unpacked_opts understands the @ option.
425 unpacked_opts :: [String]
431 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
436 %************************************************************************
438 \subsection{Static options}
440 %************************************************************************
444 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
445 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
446 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
449 opt_WarnDuplicateExports = lookUp SLIT("-fwarn-duplicate-exports")
450 opt_WarnHiShadows = lookUp SLIT("-fwarn-hi-shadowing")
451 opt_WarnIncompletePatterns = lookUp SLIT("-fwarn-incomplete-patterns")
452 opt_WarnMissingFields = lookUp SLIT("-fwarn-missing-fields")
453 opt_WarnMissingMethods = lookUp SLIT("-fwarn-missing-methods")
454 opt_WarnMissingSigs = lookUp SLIT("-fwarn-missing-signatures")
455 opt_WarnNameShadowing = lookUp SLIT("-fwarn-name-shadowing")
456 opt_WarnOverlappingPatterns = lookUp SLIT("-fwarn-overlapping-patterns")
457 opt_WarnSimplePatterns = lookUp SLIT("-fwarn-simple-patterns")
458 opt_WarnTypeDefaults = lookUp SLIT("-fwarn-type-defaults")
459 opt_WarnUnusedBinds = lookUp SLIT("-fwarn-unused-binds")
460 opt_WarnUnusedImports = lookUp SLIT("-fwarn-unused-imports")
461 opt_WarnUnusedMatches = lookUp SLIT("-fwarn-unused-matches")
462 opt_WarnDeprecations = lookUp SLIT("-fwarn-deprecations")
465 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
466 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
467 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
468 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
469 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
470 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
473 opt_AllStrict = lookUp SLIT("-fall-strict")
474 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
475 opt_Generics = lookUp SLIT("-fgenerics")
476 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
477 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
478 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
479 opt_Parallel = lookUp SLIT("-fparallel")
480 opt_SMP = lookUp SLIT("-fsmp")
483 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
484 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
485 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
486 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
487 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
488 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
489 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
492 The optional '-inpackage=P' flag tells what package
493 we are compiling this module for.
494 The Prelude, for example is compiled with '-package prelude'
496 opt_InPackage = case lookup_str "-inpackage=" of
498 Nothing -> SLIT("Main") -- The package name if none is specified
500 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
501 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
502 opt_GranMacros = lookUp SLIT("-fgransim")
503 opt_HiVersion = lookup_def_int "-fhi-version=" 0 -- what version we're compiling.
504 opt_HistorySize = lookup_def_int "-fhistory-size" 20
505 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
506 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
507 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
508 opt_NoImplicitPrelude = lookUp SLIT("-fno-implicit-prelude")
509 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
510 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
512 -- Simplifier switches
513 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
514 -- NoPreInlining is there just to see how bad things
515 -- get if you don't do it!
516 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
517 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
518 opt_SimplCaseOfCase = lookUp SLIT("-fcase-of-case")
519 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
520 opt_SimplPedanticBottoms = lookUp SLIT("-fpedantic-bottoms")
521 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
524 opt_UF_HiFileThreshold = lookup_def_int "-funfolding-interface-threshold" (45::Int)
525 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
526 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
527 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
528 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
529 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
531 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
532 opt_UF_DearOp = ( 4 :: Int)
534 opt_ReportCompile = lookUp SLIT("-freport-compile")
535 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
536 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
537 opt_Static = lookUp SLIT("-static")
538 opt_Unregisterised = lookUp SLIT("-funregisterised")
539 opt_Verbose = lookUp SLIT("-v")
541 opt_UseVanillaRegs | opt_Unregisterised = 0
542 | otherwise = mAX_Real_Vanilla_REG
543 opt_UseFloatRegs | opt_Unregisterised = 0
544 | otherwise = mAX_Real_Float_REG
545 opt_UseDoubleRegs | opt_Unregisterised = 0
546 | otherwise = mAX_Real_Double_REG
547 opt_UseLongRegs | opt_Unregisterised = 0
548 | otherwise = mAX_Real_Long_REG
551 %************************************************************************
553 \subsection{Switch ordering}
555 %************************************************************************
557 In spite of the @Produce*@ constructor, these things behave just like
561 instance Eq SimplifierSwitch where
562 a == b = tagOf_SimplSwitch a _EQ_ tagOf_SimplSwitch b
564 instance Ord SimplifierSwitch where
565 a < b = tagOf_SimplSwitch a _LT_ tagOf_SimplSwitch b
566 a <= b = tagOf_SimplSwitch a _LE_ tagOf_SimplSwitch b
569 tagOf_SimplSwitch (SimplInlinePhase _) = ILIT(1)
570 tagOf_SimplSwitch (MaxSimplifierIterations _) = ILIT(2)
571 tagOf_SimplSwitch DontApplyRules = ILIT(3)
572 tagOf_SimplSwitch SimplLetToCase = ILIT(4)
573 tagOf_SimplSwitch NoCaseOfCase = ILIT(5)
575 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
577 lAST_SIMPL_SWITCH_TAG = 5
580 %************************************************************************
582 \subsection{Switch lookup}
584 %************************************************************************
587 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
589 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
590 -- in the list; defaults right at the end.
592 tidied_on_switches = foldl rm_dups [] on_switches
593 -- The fold*l* ensures that we keep the latest switches;
594 -- ie the ones that occur earliest in the list.
596 sw_tbl :: Array Int SwitchResult
597 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
601 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
603 defined_elems = map mk_assoc_elem tidied_on_switches
605 -- (avoid some unboxing, bounds checking, and other horrible things:)
606 #if __GLASGOW_HASKELL__ < 405
607 case sw_tbl of { Array bounds_who_needs_'em stuff ->
609 case sw_tbl of { Array _ _ stuff ->
612 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
613 #if __GLASGOW_HASKELL__ < 400
615 #elif __GLASGOW_HASKELL__ < 403
622 mk_assoc_elem k@(MaxSimplifierIterations lvl) = (IBOX(tagOf_SimplSwitch k), SwInt lvl)
623 mk_assoc_elem k@(SimplInlinePhase n) = (IBOX(tagOf_SimplSwitch k), SwInt n)
624 mk_assoc_elem k = (IBOX(tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
626 -- cannot have duplicates if we are going to use the array thing
627 rm_dups switches_so_far switch
628 = if switch `is_elem` switches_so_far
630 else switch : switches_so_far
632 sw `is_elem` [] = False
633 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) _EQ_ (tagOf_SimplSwitch s)
637 Default settings for simplifier switches
640 defaultSimplSwitches = [MaxSimplifierIterations 1]
643 %************************************************************************
645 \subsection{Misc functions for command-line options}
647 %************************************************************************
651 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
653 switchIsOn lookup_fn switch
654 = case (lookup_fn switch) of
655 SwBool False -> False
658 intSwitchSet :: (switch -> SwitchResult)
662 intSwitchSet lookup_fn switch
663 = case (lookup_fn (switch (panic "intSwitchSet"))) of
664 SwInt int -> Just int
669 startsWith :: String -> String -> Maybe String
670 -- startsWith pfx (pfx++rest) = Just rest
672 startsWith [] str = Just str
673 startsWith (c:cs) (s:ss)
674 = if c /= s then Nothing else startsWith cs ss
675 startsWith _ [] = Nothing
677 endsWith :: String -> String -> Maybe String
679 = case (startsWith (reverse cs) (reverse ss)) of
681 Just rs -> Just (reverse rs)