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
27 -- other dynamic flags
34 opt_AutoSccsOnAllToplevs,
35 opt_AutoSccsOnExportedToplevs,
36 opt_AutoSccsOnIndividualCafs,
44 opt_MaxContextReductionDepth,
45 opt_IrrefutableTuples,
53 opt_LiberateCaseThreshold,
54 opt_StgDoLetNoEscapes,
57 opt_UnboxStrictFields,
58 opt_SimplNoPreInlining,
59 opt_SimplDoEtaReduction,
60 opt_SimplDoLambdaEtaExpansion,
63 opt_SimplPedanticBottoms,
64 opt_SimplExcessPrecision,
67 opt_UF_HiFileThreshold,
68 opt_UF_CreationThreshold,
70 opt_UF_FunAppDiscount,
79 opt_EnsureSplittableC,
84 opt_IgnoreIfacePragmas,
86 opt_NoImplicitPrelude,
88 opt_OmitInterfacePragmas,
96 #include "HsVersions.h"
98 import Array ( array, (//) )
101 import Constants -- Default values for some flags
105 import Maybes ( firstJust )
106 import Panic ( panic )
108 #if __GLASGOW_HASKELL__ < 301
109 import ArrBase ( Array(..) )
111 import PrelArr ( Array(..) )
115 %************************************************************************
117 \subsection{Command-line options}
119 %************************************************************************
121 The hsc command-line options are split into two categories:
126 Static flags are represented by top-level values of type Bool or Int,
127 for example. They therefore have the same value throughout the
130 Dynamic flags are represented by an abstract type, DynFlags, which is
131 passed into hsc by the compilation manager for every compilation.
132 Dynamic flags are those that change on a per-compilation basis,
133 perhaps because they may be present in the OPTIONS pragma at the top
136 Other flag-related blurb:
138 A list of {\em ToDo}s is things to be done in a particular part of
139 processing. A (fictitious) example for the Core-to-Core simplifier
140 might be: run the simplifier, then run the strictness analyser, then
141 run the simplifier again (three ``todos'').
143 There are three ``to-do processing centers'' at the moment. In the
144 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
145 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
146 (\tr{simplStg/SimplStg.lhs}).
148 %************************************************************************
150 \subsection{Datatypes associated with command-line options}
152 %************************************************************************
156 = SwBool Bool -- on/off
157 | SwString FAST_STRING -- nothing or a String
158 | SwInt Int -- nothing or an Int
162 data CoreToDo -- These are diff core-to-core passes,
163 -- which may be invoked in any order,
164 -- as many times as you like.
166 = CoreDoSimplify -- The core-to-core simplifier.
167 (SimplifierSwitch -> SwitchResult)
168 -- Each run of the simplifier can take a different
169 -- set of simplifier-specific flags.
171 | CoreDoFloatOutwards Bool -- True <=> float lambdas to top level
176 | CoreDoWorkerWrapper
183 | CoreDoNothing -- useful when building up lists of these things
190 | StgDoMassageForProfiling -- should be (next to) last
191 -- There's also setStgVarInfo, but its absolute "lastness"
192 -- is so critical that it is hardwired in (no flag).
197 data SimplifierSwitch
198 = MaxSimplifierIterations Int
199 | SimplInlinePhase Int
205 %************************************************************************
207 \subsection{Dynamic command-line options}
209 %************************************************************************
224 | Opt_D_dump_inlinings
225 | Opt_D_dump_occur_anal
230 | Opt_D_dump_simpl_iterations
239 | Opt_D_dump_worker_wrapper
241 | Opt_D_dump_rn_trace
242 | Opt_D_dump_rn_stats
244 | Opt_D_dump_simpl_stats
246 | Opt_D_verbose_core2core
247 | Opt_D_verbose_stg2stg
248 | Opt_D_dump_hi_diffs
249 | Opt_D_dump_minimal_imports
254 | Opt_WarnDuplicateExports
256 | Opt_WarnIncompletePatterns
257 | Opt_WarnMissingFields
258 | Opt_WarnMissingMethods
259 | Opt_WarnMissingSigs
260 | Opt_WarnNameShadowing
261 | Opt_WarnOverlappingPatterns
262 | Opt_WarnSimplePatterns
263 | Opt_WarnTypeDefaults
264 | Opt_WarnUnusedBinds
265 | Opt_WarnUnusedImports
266 | Opt_WarnUnusedMatches
267 | Opt_WarnDeprecations
270 | Opt_AllowOverlappingInstances
271 | Opt_AllowUndecidableInstances
279 data DynFlags = DynFlags {
280 coreToDo :: CoreToDo,
283 hscOutName :: String, -- name of the file in which to place output
287 dopt :: DynFlag -> DynFlags -> Bool
288 dopt f dflags = f `elem` (flags dflags)
290 dopt_CoreToDo :: DynFlags -> CoreToDo
291 dopt_CoreToDo = coreToDo
293 dopt_StgToDo :: DynFlags -> StgToDo
294 dopt_StgToDo = stgToDo
296 dopt_OutName :: DynFlags -> String
297 dopt_OutName = hscOutName
306 dopt_HscLang :: DynFlags -> HscLang
307 dopt_HscLang = hscLang
310 %************************************************************************
312 \subsection{Classifying command-line options}
314 %************************************************************************
317 lookUp :: FAST_STRING -> Bool
318 lookup_int :: String -> Maybe Int
319 lookup_def_int :: String -> Int -> Int
320 lookup_def_float :: String -> Float -> Float
321 lookup_str :: String -> Maybe String
323 lookUp sw = sw `elem` argv
325 lookup_str sw = firstJust (map (startsWith sw) unpacked_opts)
327 lookup_int sw = case (lookup_str sw) of
329 Just xx -> Just (read xx)
331 lookup_def_int sw def = case (lookup_str sw) of
332 Nothing -> def -- Use default
335 lookup_def_char sw def = case (lookup_str sw) of
337 _ -> def -- Use default
339 lookup_def_float sw def = case (lookup_str sw) of
340 Nothing -> def -- Use default
343 unpacked_opts = map _UNPK_ argv
346 Putting the compiler options into temporary at-files
347 may turn out to be necessary later on if we turn hsc into
348 a pure Win32 application where I think there's a command-line
349 length limit of 255. unpacked_opts understands the @ option.
351 unpacked_opts :: [String]
357 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
362 %************************************************************************
364 \subsection{Static options}
366 %************************************************************************
370 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
371 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
372 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
375 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
376 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
377 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
378 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
379 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
380 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
383 opt_AllStrict = lookUp SLIT("-fall-strict")
384 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
385 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
386 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
387 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
388 opt_Parallel = lookUp SLIT("-fparallel")
389 opt_SMP = lookUp SLIT("-fsmp")
392 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
393 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
394 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
395 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
396 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
397 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
398 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
401 The optional '-inpackage=P' flag tells what package
402 we are compiling this module for.
403 The Prelude, for example is compiled with '-package prelude'
405 opt_InPackage = case lookup_str "-inpackage=" of
407 Nothing -> SLIT("Main") -- The package name if none is specified
409 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
410 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
411 opt_GranMacros = lookUp SLIT("-fgransim")
412 opt_HiVersion = lookup_def_int "-fhi-version=" 0 -- what version we're compiling.
413 opt_HistorySize = lookup_def_int "-fhistory-size" 20
414 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
415 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
416 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
417 opt_NoImplicitPrelude = lookUp SLIT("-fno-implicit-prelude")
418 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
419 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
421 -- Simplifier switches
422 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
423 -- NoPreInlining is there just to see how bad things
424 -- get if you don't do it!
425 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
426 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
427 opt_SimplCaseOfCase = lookUp SLIT("-fcase-of-case")
428 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
429 opt_SimplPedanticBottoms = lookUp SLIT("-fpedantic-bottoms")
430 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
433 opt_UF_HiFileThreshold = lookup_def_int "-funfolding-interface-threshold" (45::Int)
434 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
435 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
436 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
437 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
438 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
440 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
441 opt_UF_DearOp = ( 4 :: Int)
443 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
444 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
445 opt_Static = lookUp SLIT("-static")
446 opt_Unregisterised = lookUp SLIT("-funregisterised")
447 opt_Verbose = lookUp SLIT("-v")
450 %************************************************************************
452 \subsection{List of static hsc flags}
454 %************************************************************************
459 "-fauto-sccs-on-all-toplevs",
460 "-fauto-sccs-on-exported-toplevs",
461 "-fauto-sccs-on-individual-cafs",
462 "-fauto-sccs-on-dicts",
468 "-firrefutable-tuples",
475 "-funfold-casms-in-hi-file",
477 "-funbox-strict-fields",
478 "-femit-extern-decls",
479 "-fglobalise-toplev-names",
482 "-fignore-interface-pragmas",
483 "-fno-hi-version-check",
484 "-fno-implicit-prelude",
486 "-fomit-interface-pragmas",
488 "-fdo-eta-reduction",
489 "-fdo-lambda-eta-expansion",
492 "-fpedantic-bottoms",
493 "-fexcess-precision",
494 "-funfolding-update-in-place",
497 "-fno-prune-tydecls",
501 || any (flip prefixMatch f) [
503 "-fliberate-case-threshold",
506 "-funfolding-interface-threshold",
507 "-funfolding-creation-threshold",
508 "-funfolding-use-threshold",
509 "-funfolding-fun-discount",
510 "-funfolding-keeness-factor"
514 %************************************************************************
516 \subsection{Switch ordering}
518 %************************************************************************
520 These things behave just like enumeration types.
523 instance Eq SimplifierSwitch where
524 a == b = tagOf_SimplSwitch a ==# tagOf_SimplSwitch b
526 instance Ord SimplifierSwitch where
527 a < b = tagOf_SimplSwitch a <# tagOf_SimplSwitch b
528 a <= b = tagOf_SimplSwitch a <=# tagOf_SimplSwitch b
531 tagOf_SimplSwitch (SimplInlinePhase _) = _ILIT(1)
532 tagOf_SimplSwitch (MaxSimplifierIterations _) = _ILIT(2)
533 tagOf_SimplSwitch DontApplyRules = _ILIT(3)
534 tagOf_SimplSwitch SimplLetToCase = _ILIT(4)
535 tagOf_SimplSwitch NoCaseOfCase = _ILIT(5)
537 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
539 lAST_SIMPL_SWITCH_TAG = 5
542 %************************************************************************
544 \subsection{Switch lookup}
546 %************************************************************************
549 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
550 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
551 -- in the list; defaults right at the end.
553 tidied_on_switches = foldl rm_dups [] on_switches
554 -- The fold*l* ensures that we keep the latest switches;
555 -- ie the ones that occur earliest in the list.
557 sw_tbl :: Array Int SwitchResult
558 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
562 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
564 defined_elems = map mk_assoc_elem tidied_on_switches
566 -- (avoid some unboxing, bounds checking, and other horrible things:)
567 #if __GLASGOW_HASKELL__ < 405
568 case sw_tbl of { Array bounds_who_needs_'em stuff ->
570 case sw_tbl of { Array _ _ stuff ->
573 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
574 #if __GLASGOW_HASKELL__ < 400
576 #elif __GLASGOW_HASKELL__ < 403
583 mk_assoc_elem k@(MaxSimplifierIterations lvl)
584 = (iBox (tagOf_SimplSwitch k), SwInt lvl)
585 mk_assoc_elem k@(SimplInlinePhase n)
586 = (iBox (tagOf_SimplSwitch k), SwInt n)
588 = (iBox (tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
590 -- cannot have duplicates if we are going to use the array thing
591 rm_dups switches_so_far switch
592 = if switch `is_elem` switches_so_far
594 else switch : switches_so_far
596 sw `is_elem` [] = False
597 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) ==# (tagOf_SimplSwitch s)
601 Default settings for simplifier switches
604 defaultSimplSwitches = [MaxSimplifierIterations 1]
607 %************************************************************************
609 \subsection{Misc functions for command-line options}
611 %************************************************************************
615 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
617 switchIsOn lookup_fn switch
618 = case (lookup_fn switch) of
619 SwBool False -> False
622 intSwitchSet :: (switch -> SwitchResult)
626 intSwitchSet lookup_fn switch
627 = case (lookup_fn (switch (panic "intSwitchSet"))) of
628 SwInt int -> Just int
633 startsWith :: String -> String -> Maybe String
634 -- startsWith pfx (pfx++rest) = Just rest
636 startsWith [] str = Just str
637 startsWith (c:cs) (s:ss)
638 = if c /= s then Nothing else startsWith cs ss
639 startsWith _ [] = Nothing
641 endsWith :: String -> String -> Maybe String
643 = case (startsWith (reverse cs) (reverse ss)) of
645 Just rs -> Just (reverse rs)