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
29 -- other dynamic flags
36 opt_AutoSccsOnAllToplevs,
37 opt_AutoSccsOnExportedToplevs,
38 opt_AutoSccsOnIndividualCafs,
46 opt_MaxContextReductionDepth,
47 opt_IrrefutableTuples,
55 opt_LiberateCaseThreshold,
56 opt_StgDoLetNoEscapes,
59 opt_UnboxStrictFields,
60 opt_SimplNoPreInlining,
61 opt_SimplDoEtaReduction,
62 opt_SimplDoLambdaEtaExpansion,
65 opt_SimplPedanticBottoms,
66 opt_SimplExcessPrecision,
69 opt_UF_HiFileThreshold,
70 opt_UF_CreationThreshold,
72 opt_UF_FunAppDiscount,
81 opt_EnsureSplittableC,
86 opt_IgnoreIfacePragmas,
89 opt_OmitInterfacePragmas,
97 #include "HsVersions.h"
99 import Array ( array, (//) )
101 import IOExts ( IORef, readIORef )
102 import Constants -- Default values for some flags
106 import Maybes ( firstJust )
107 import Panic ( panic )
109 #if __GLASGOW_HASKELL__ < 301
110 import ArrBase ( Array(..) )
112 import PrelArr ( Array(..) )
116 %************************************************************************
118 \subsection{Command-line options}
120 %************************************************************************
122 The hsc command-line options are split into two categories:
127 Static flags are represented by top-level values of type Bool or Int,
128 for example. They therefore have the same value throughout the
131 Dynamic flags are represented by an abstract type, DynFlags, which is
132 passed into hsc by the compilation manager for every compilation.
133 Dynamic flags are those that change on a per-compilation basis,
134 perhaps because they may be present in the OPTIONS pragma at the top
137 Other flag-related blurb:
139 A list of {\em ToDo}s is things to be done in a particular part of
140 processing. A (fictitious) example for the Core-to-Core simplifier
141 might be: run the simplifier, then run the strictness analyser, then
142 run the simplifier again (three ``todos'').
144 There are three ``to-do processing centers'' at the moment. In the
145 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
146 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
147 (\tr{simplStg/SimplStg.lhs}).
149 %************************************************************************
151 \subsection{Datatypes associated with command-line options}
153 %************************************************************************
157 = SwBool Bool -- on/off
158 | SwString FAST_STRING -- nothing or a String
159 | SwInt Int -- nothing or an Int
163 data CoreToDo -- These are diff core-to-core passes,
164 -- which may be invoked in any order,
165 -- as many times as you like.
167 = CoreDoSimplify -- The core-to-core simplifier.
168 (SimplifierSwitch -> SwitchResult)
169 -- Each run of the simplifier can take a different
170 -- set of simplifier-specific flags.
172 | CoreDoFloatOutwards Bool -- True <=> float lambdas to top level
177 | CoreDoWorkerWrapper
184 | CoreDoNothing -- useful when building up lists of these things
191 | StgDoMassageForProfiling -- should be (next to) last
192 -- There's also setStgVarInfo, but its absolute "lastness"
193 -- is so critical that it is hardwired in (no flag).
198 data SimplifierSwitch
199 = MaxSimplifierIterations Int
200 | SimplInlinePhase Int
206 %************************************************************************
208 \subsection{Dynamic command-line options}
210 %************************************************************************
225 | Opt_D_dump_inlinings
226 | Opt_D_dump_occur_anal
231 | Opt_D_dump_simpl_iterations
240 | Opt_D_dump_worker_wrapper
242 | Opt_D_dump_rn_trace
243 | Opt_D_dump_rn_stats
245 | Opt_D_dump_simpl_stats
247 | Opt_D_verbose_core2core
248 | Opt_D_verbose_stg2stg
249 | Opt_D_dump_hi_diffs
250 | Opt_D_dump_minimal_imports
255 | Opt_WarnDuplicateExports
257 | Opt_WarnIncompletePatterns
258 | Opt_WarnMissingFields
259 | Opt_WarnMissingMethods
260 | Opt_WarnMissingSigs
261 | Opt_WarnNameShadowing
262 | Opt_WarnOverlappingPatterns
263 | Opt_WarnSimplePatterns
264 | Opt_WarnTypeDefaults
265 | Opt_WarnUnusedBinds
266 | Opt_WarnUnusedImports
267 | Opt_WarnUnusedMatches
268 | Opt_WarnDeprecations
271 | Opt_AllowOverlappingInstances
272 | Opt_AllowUndecidableInstances
275 | Opt_NoImplicitPrelude
281 data DynFlags = DynFlags {
282 coreToDo :: [CoreToDo],
283 stgToDo :: [StgToDo],
285 hscOutName :: String, -- name of the file in which to place output
289 dopt :: DynFlag -> DynFlags -> Bool
290 dopt f dflags = f `elem` (flags dflags)
292 dopt_CoreToDo :: DynFlags -> [CoreToDo]
293 dopt_CoreToDo = coreToDo
295 dopt_StgToDo :: DynFlags -> [StgToDo]
296 dopt_StgToDo = stgToDo
298 dopt_OutName :: DynFlags -> String
299 dopt_OutName = hscOutName
308 dopt_HscLang :: DynFlags -> HscLang
309 dopt_HscLang = hscLang
312 %************************************************************************
314 \subsection{Classifying command-line options}
316 %************************************************************************
319 -- v_Statis_hsc_opts is here to avoid a circular dependency with
321 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
323 lookUp :: FAST_STRING -> Bool
324 lookup_int :: String -> Maybe Int
325 lookup_def_int :: String -> Int -> Int
326 lookup_def_float :: String -> Float -> Float
327 lookup_str :: String -> Maybe String
329 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
330 packed_static_opts = map _PK_ unpacked_static_opts
332 lookUp sw = sw `elem` packed_static_opts
334 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
336 lookup_int sw = case (lookup_str sw) of
338 Just xx -> Just (read xx)
340 lookup_def_int sw def = case (lookup_str sw) of
341 Nothing -> def -- Use default
344 lookup_def_char sw def = case (lookup_str sw) of
346 _ -> def -- Use default
348 lookup_def_float sw def = case (lookup_str sw) of
349 Nothing -> def -- Use default
354 Putting the compiler options into temporary at-files
355 may turn out to be necessary later on if we turn hsc into
356 a pure Win32 application where I think there's a command-line
357 length limit of 255. unpacked_opts understands the @ option.
359 unpacked_opts :: [String]
363 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
365 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
370 %************************************************************************
372 \subsection{Static options}
374 %************************************************************************
378 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
379 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
380 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
383 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
384 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
385 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
386 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
387 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
388 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
391 opt_AllStrict = lookUp SLIT("-fall-strict")
392 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
393 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
394 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
395 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
396 opt_Parallel = lookUp SLIT("-fparallel")
397 opt_SMP = lookUp SLIT("-fsmp")
400 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
401 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
402 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
403 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
404 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
405 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
406 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
409 The optional '-inpackage=P' flag tells what package
410 we are compiling this module for.
411 The Prelude, for example is compiled with '-package prelude'
413 opt_InPackage = case lookup_str "-inpackage=" of
415 Nothing -> SLIT("Main") -- The package name if none is specified
417 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
418 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
419 opt_GranMacros = lookUp SLIT("-fgransim")
420 opt_HiVersion = lookup_def_int "-fhi-version=" 0 -- what version we're compiling.
421 opt_HistorySize = lookup_def_int "-fhistory-size" 20
422 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
423 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
424 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
425 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
426 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
428 -- Simplifier switches
429 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
430 -- NoPreInlining is there just to see how bad things
431 -- get if you don't do it!
432 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
433 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
434 opt_SimplCaseOfCase = lookUp SLIT("-fcase-of-case")
435 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
436 opt_SimplPedanticBottoms = lookUp SLIT("-fpedantic-bottoms")
437 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
440 opt_UF_HiFileThreshold = lookup_def_int "-funfolding-interface-threshold" (45::Int)
441 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
442 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
443 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
444 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
445 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
447 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
448 opt_UF_DearOp = ( 4 :: Int)
450 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
451 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
452 opt_Static = lookUp SLIT("-static")
453 opt_Unregisterised = lookUp SLIT("-funregisterised")
454 opt_Verbose = lookUp SLIT("-v")
457 %************************************************************************
459 \subsection{List of static hsc flags}
461 %************************************************************************
466 "fauto-sccs-on-all-toplevs",
467 "fauto-sccs-on-exported-toplevs",
468 "fauto-sccs-on-individual-cafs",
469 "fauto-sccs-on-dicts",
474 "firrefutable-tuples",
481 "funfold-casms-in-hi-file",
483 "funbox-strict-fields",
484 "femit-extern-decls",
485 "fglobalise-toplev-names",
488 "fignore-interface-pragmas",
489 "fno-hi-version-check",
490 "fno-implicit-prelude",
492 "fomit-interface-pragmas",
495 "fdo-lambda-eta-expansion",
500 "funfolding-update-in-place",
507 || any (flip prefixMatch f) [
509 "fliberate-case-threshold",
512 "funfolding-interface-threshold",
513 "funfolding-creation-threshold",
514 "funfolding-use-threshold",
515 "funfolding-fun-discount",
516 "funfolding-keeness-factor"
520 %************************************************************************
522 \subsection{Switch ordering}
524 %************************************************************************
526 These things behave just like enumeration types.
529 instance Eq SimplifierSwitch where
530 a == b = tagOf_SimplSwitch a ==# tagOf_SimplSwitch b
532 instance Ord SimplifierSwitch where
533 a < b = tagOf_SimplSwitch a <# tagOf_SimplSwitch b
534 a <= b = tagOf_SimplSwitch a <=# tagOf_SimplSwitch b
537 tagOf_SimplSwitch (SimplInlinePhase _) = _ILIT(1)
538 tagOf_SimplSwitch (MaxSimplifierIterations _) = _ILIT(2)
539 tagOf_SimplSwitch DontApplyRules = _ILIT(3)
540 tagOf_SimplSwitch SimplLetToCase = _ILIT(4)
541 tagOf_SimplSwitch NoCaseOfCase = _ILIT(5)
543 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
545 lAST_SIMPL_SWITCH_TAG = 5
548 %************************************************************************
550 \subsection{Switch lookup}
552 %************************************************************************
555 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
556 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
557 -- in the list; defaults right at the end.
559 tidied_on_switches = foldl rm_dups [] on_switches
560 -- The fold*l* ensures that we keep the latest switches;
561 -- ie the ones that occur earliest in the list.
563 sw_tbl :: Array Int SwitchResult
564 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
568 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
570 defined_elems = map mk_assoc_elem tidied_on_switches
572 -- (avoid some unboxing, bounds checking, and other horrible things:)
573 #if __GLASGOW_HASKELL__ < 405
574 case sw_tbl of { Array bounds_who_needs_'em stuff ->
576 case sw_tbl of { Array _ _ stuff ->
579 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
580 #if __GLASGOW_HASKELL__ < 400
582 #elif __GLASGOW_HASKELL__ < 403
589 mk_assoc_elem k@(MaxSimplifierIterations lvl)
590 = (iBox (tagOf_SimplSwitch k), SwInt lvl)
591 mk_assoc_elem k@(SimplInlinePhase n)
592 = (iBox (tagOf_SimplSwitch k), SwInt n)
594 = (iBox (tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
596 -- cannot have duplicates if we are going to use the array thing
597 rm_dups switches_so_far switch
598 = if switch `is_elem` switches_so_far
600 else switch : switches_so_far
602 sw `is_elem` [] = False
603 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) ==# (tagOf_SimplSwitch s)
607 Default settings for simplifier switches
610 defaultSimplSwitches = [MaxSimplifierIterations 1]
613 %************************************************************************
615 \subsection{Misc functions for command-line options}
617 %************************************************************************
621 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
623 switchIsOn lookup_fn switch
624 = case (lookup_fn switch) of
625 SwBool False -> False
628 intSwitchSet :: (switch -> SwitchResult)
632 intSwitchSet lookup_fn switch
633 = case (lookup_fn (switch (panic "intSwitchSet"))) of
634 SwInt int -> Just int
639 startsWith :: String -> String -> Maybe String
640 -- startsWith pfx (pfx++rest) = Just rest
642 startsWith [] str = Just str
643 startsWith (c:cs) (s:ss)
644 = if c /= s then Nothing else startsWith cs ss
645 startsWith _ [] = Nothing
647 endsWith :: String -> String -> Maybe String
649 = case (startsWith (reverse cs) (reverse ss)) of
651 Just rs -> Just (reverse rs)