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,
88 opt_NoImplicitPrelude,
90 opt_OmitInterfacePragmas,
98 #include "HsVersions.h"
100 import Array ( array, (//) )
102 import IOExts ( IORef, readIORef )
104 import Constants -- Default values for some flags
108 import Maybes ( firstJust )
109 import Panic ( panic )
111 #if __GLASGOW_HASKELL__ < 301
112 import ArrBase ( Array(..) )
114 import PrelArr ( Array(..) )
118 %************************************************************************
120 \subsection{Command-line options}
122 %************************************************************************
124 The hsc command-line options are split into two categories:
129 Static flags are represented by top-level values of type Bool or Int,
130 for example. They therefore have the same value throughout the
133 Dynamic flags are represented by an abstract type, DynFlags, which is
134 passed into hsc by the compilation manager for every compilation.
135 Dynamic flags are those that change on a per-compilation basis,
136 perhaps because they may be present in the OPTIONS pragma at the top
139 Other flag-related blurb:
141 A list of {\em ToDo}s is things to be done in a particular part of
142 processing. A (fictitious) example for the Core-to-Core simplifier
143 might be: run the simplifier, then run the strictness analyser, then
144 run the simplifier again (three ``todos'').
146 There are three ``to-do processing centers'' at the moment. In the
147 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
148 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
149 (\tr{simplStg/SimplStg.lhs}).
151 %************************************************************************
153 \subsection{Datatypes associated with command-line options}
155 %************************************************************************
159 = SwBool Bool -- on/off
160 | SwString FAST_STRING -- nothing or a String
161 | SwInt Int -- nothing or an Int
165 data CoreToDo -- These are diff core-to-core passes,
166 -- which may be invoked in any order,
167 -- as many times as you like.
169 = CoreDoSimplify -- The core-to-core simplifier.
170 (SimplifierSwitch -> SwitchResult)
171 -- Each run of the simplifier can take a different
172 -- set of simplifier-specific flags.
174 | CoreDoFloatOutwards Bool -- True <=> float lambdas to top level
179 | CoreDoWorkerWrapper
186 | CoreDoNothing -- useful when building up lists of these things
193 | StgDoMassageForProfiling -- should be (next to) last
194 -- There's also setStgVarInfo, but its absolute "lastness"
195 -- is so critical that it is hardwired in (no flag).
200 data SimplifierSwitch
201 = MaxSimplifierIterations Int
202 | SimplInlinePhase Int
208 %************************************************************************
210 \subsection{Dynamic command-line options}
212 %************************************************************************
227 | Opt_D_dump_inlinings
228 | Opt_D_dump_occur_anal
233 | Opt_D_dump_simpl_iterations
242 | Opt_D_dump_worker_wrapper
244 | Opt_D_dump_rn_trace
245 | Opt_D_dump_rn_stats
247 | Opt_D_dump_simpl_stats
249 | Opt_D_verbose_core2core
250 | Opt_D_verbose_stg2stg
251 | Opt_D_dump_hi_diffs
252 | Opt_D_dump_minimal_imports
257 | Opt_WarnDuplicateExports
259 | Opt_WarnIncompletePatterns
260 | Opt_WarnMissingFields
261 | Opt_WarnMissingMethods
262 | Opt_WarnMissingSigs
263 | Opt_WarnNameShadowing
264 | Opt_WarnOverlappingPatterns
265 | Opt_WarnSimplePatterns
266 | Opt_WarnTypeDefaults
267 | Opt_WarnUnusedBinds
268 | Opt_WarnUnusedImports
269 | Opt_WarnUnusedMatches
270 | Opt_WarnDeprecations
273 | Opt_AllowOverlappingInstances
274 | Opt_AllowUndecidableInstances
282 data DynFlags = DynFlags {
283 coreToDo :: [CoreToDo],
284 stgToDo :: [StgToDo],
286 hscOutName :: String, -- name of the file in which to place output
290 dopt :: DynFlag -> DynFlags -> Bool
291 dopt f dflags = f `elem` (flags dflags)
293 dopt_CoreToDo :: DynFlags -> [CoreToDo]
294 dopt_CoreToDo = coreToDo
296 dopt_StgToDo :: DynFlags -> [StgToDo]
297 dopt_StgToDo = stgToDo
299 dopt_OutName :: DynFlags -> String
300 dopt_OutName = hscOutName
309 dopt_HscLang :: DynFlags -> HscLang
310 dopt_HscLang = hscLang
313 %************************************************************************
315 \subsection{Classifying command-line options}
317 %************************************************************************
320 -- v_Statis_hsc_opts is here to avoid a circular dependency with
322 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
324 lookUp :: FAST_STRING -> Bool
325 lookup_int :: String -> Maybe Int
326 lookup_def_int :: String -> Int -> Int
327 lookup_def_float :: String -> Float -> Float
328 lookup_str :: String -> Maybe String
330 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
331 packed_static_opts = map _PK_ unpacked_static_opts
333 lookUp sw = sw `elem` packed_static_opts
335 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
337 lookup_int sw = case (lookup_str sw) of
339 Just xx -> Just (read xx)
341 lookup_def_int sw def = case (lookup_str sw) of
342 Nothing -> def -- Use default
345 lookup_def_char sw def = case (lookup_str sw) of
347 _ -> def -- Use default
349 lookup_def_float sw def = case (lookup_str sw) of
350 Nothing -> def -- Use default
355 Putting the compiler options into temporary at-files
356 may turn out to be necessary later on if we turn hsc into
357 a pure Win32 application where I think there's a command-line
358 length limit of 255. unpacked_opts understands the @ option.
360 unpacked_opts :: [String]
364 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
366 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
371 %************************************************************************
373 \subsection{Static options}
375 %************************************************************************
379 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
380 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
381 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
384 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
385 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
386 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
387 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
388 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
389 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
392 opt_AllStrict = lookUp SLIT("-fall-strict")
393 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
394 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
395 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
396 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
397 opt_Parallel = lookUp SLIT("-fparallel")
398 opt_SMP = lookUp SLIT("-fsmp")
401 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
402 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
403 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
404 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
405 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
406 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
407 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
410 The optional '-inpackage=P' flag tells what package
411 we are compiling this module for.
412 The Prelude, for example is compiled with '-package prelude'
414 opt_InPackage = case lookup_str "-inpackage=" of
416 Nothing -> SLIT("Main") -- The package name if none is specified
418 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
419 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
420 opt_GranMacros = lookUp SLIT("-fgransim")
421 opt_HiVersion = lookup_def_int "-fhi-version=" 0 -- what version we're compiling.
422 opt_HistorySize = lookup_def_int "-fhistory-size" 20
423 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
424 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
425 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
426 opt_NoImplicitPrelude = lookUp SLIT("-fno-implicit-prelude")
427 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
428 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
430 -- Simplifier switches
431 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
432 -- NoPreInlining is there just to see how bad things
433 -- get if you don't do it!
434 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
435 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
436 opt_SimplCaseOfCase = lookUp SLIT("-fcase-of-case")
437 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
438 opt_SimplPedanticBottoms = lookUp SLIT("-fpedantic-bottoms")
439 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
442 opt_UF_HiFileThreshold = lookup_def_int "-funfolding-interface-threshold" (45::Int)
443 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
444 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
445 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
446 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
447 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
449 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
450 opt_UF_DearOp = ( 4 :: Int)
452 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
453 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
454 opt_Static = lookUp SLIT("-static")
455 opt_Unregisterised = lookUp SLIT("-funregisterised")
456 opt_Verbose = lookUp SLIT("-v")
459 %************************************************************************
461 \subsection{List of static hsc flags}
463 %************************************************************************
468 "-fauto-sccs-on-all-toplevs",
469 "-fauto-sccs-on-exported-toplevs",
470 "-fauto-sccs-on-individual-cafs",
471 "-fauto-sccs-on-dicts",
477 "-firrefutable-tuples",
484 "-funfold-casms-in-hi-file",
486 "-funbox-strict-fields",
487 "-femit-extern-decls",
488 "-fglobalise-toplev-names",
491 "-fignore-interface-pragmas",
492 "-fno-hi-version-check",
493 "-fno-implicit-prelude",
495 "-fomit-interface-pragmas",
497 "-fdo-eta-reduction",
498 "-fdo-lambda-eta-expansion",
501 "-fpedantic-bottoms",
502 "-fexcess-precision",
503 "-funfolding-update-in-place",
506 "-fno-prune-tydecls",
510 || any (flip prefixMatch f) [
512 "-fliberate-case-threshold",
515 "-funfolding-interface-threshold",
516 "-funfolding-creation-threshold",
517 "-funfolding-use-threshold",
518 "-funfolding-fun-discount",
519 "-funfolding-keeness-factor"
523 %************************************************************************
525 \subsection{Switch ordering}
527 %************************************************************************
529 These things behave just like enumeration types.
532 instance Eq SimplifierSwitch where
533 a == b = tagOf_SimplSwitch a ==# tagOf_SimplSwitch b
535 instance Ord SimplifierSwitch where
536 a < b = tagOf_SimplSwitch a <# tagOf_SimplSwitch b
537 a <= b = tagOf_SimplSwitch a <=# tagOf_SimplSwitch b
540 tagOf_SimplSwitch (SimplInlinePhase _) = _ILIT(1)
541 tagOf_SimplSwitch (MaxSimplifierIterations _) = _ILIT(2)
542 tagOf_SimplSwitch DontApplyRules = _ILIT(3)
543 tagOf_SimplSwitch SimplLetToCase = _ILIT(4)
544 tagOf_SimplSwitch NoCaseOfCase = _ILIT(5)
546 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
548 lAST_SIMPL_SWITCH_TAG = 5
551 %************************************************************************
553 \subsection{Switch lookup}
555 %************************************************************************
558 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
559 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
560 -- in the list; defaults right at the end.
562 tidied_on_switches = foldl rm_dups [] on_switches
563 -- The fold*l* ensures that we keep the latest switches;
564 -- ie the ones that occur earliest in the list.
566 sw_tbl :: Array Int SwitchResult
567 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
571 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
573 defined_elems = map mk_assoc_elem tidied_on_switches
575 -- (avoid some unboxing, bounds checking, and other horrible things:)
576 #if __GLASGOW_HASKELL__ < 405
577 case sw_tbl of { Array bounds_who_needs_'em stuff ->
579 case sw_tbl of { Array _ _ stuff ->
582 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
583 #if __GLASGOW_HASKELL__ < 400
585 #elif __GLASGOW_HASKELL__ < 403
592 mk_assoc_elem k@(MaxSimplifierIterations lvl)
593 = (iBox (tagOf_SimplSwitch k), SwInt lvl)
594 mk_assoc_elem k@(SimplInlinePhase n)
595 = (iBox (tagOf_SimplSwitch k), SwInt n)
597 = (iBox (tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
599 -- cannot have duplicates if we are going to use the array thing
600 rm_dups switches_so_far switch
601 = if switch `is_elem` switches_so_far
603 else switch : switches_so_far
605 sw `is_elem` [] = False
606 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) ==# (tagOf_SimplSwitch s)
610 Default settings for simplifier switches
613 defaultSimplSwitches = [MaxSimplifierIterations 1]
616 %************************************************************************
618 \subsection{Misc functions for command-line options}
620 %************************************************************************
624 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
626 switchIsOn lookup_fn switch
627 = case (lookup_fn switch) of
628 SwBool False -> False
631 intSwitchSet :: (switch -> SwitchResult)
635 intSwitchSet lookup_fn switch
636 = case (lookup_fn (switch (panic "intSwitchSet"))) of
637 SwInt int -> Just int
642 startsWith :: String -> String -> Maybe String
643 -- startsWith pfx (pfx++rest) = Just rest
645 startsWith [] str = Just str
646 startsWith (c:cs) (s:ss)
647 = if c /= s then Nothing else startsWith cs ss
648 startsWith _ [] = Nothing
650 endsWith :: String -> String -> Maybe String
652 = case (startsWith (reverse cs) (reverse ss)) of
654 Just rs -> Just (reverse rs)