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
305 dopt_HscLang :: DynFlags -> HscLang
306 dopt_HscLang = hscLang
309 %************************************************************************
311 \subsection{Classifying command-line options}
313 %************************************************************************
316 lookUp :: FAST_STRING -> Bool
317 lookup_int :: String -> Maybe Int
318 lookup_def_int :: String -> Int -> Int
319 lookup_def_float :: String -> Float -> Float
320 lookup_str :: String -> Maybe String
322 lookUp sw = sw `elem` argv
324 lookup_str sw = firstJust (map (startsWith sw) unpacked_opts)
326 lookup_int sw = case (lookup_str sw) of
328 Just xx -> Just (read xx)
330 lookup_def_int sw def = case (lookup_str sw) of
331 Nothing -> def -- Use default
334 lookup_def_char sw def = case (lookup_str sw) of
336 _ -> def -- Use default
338 lookup_def_float sw def = case (lookup_str sw) of
339 Nothing -> def -- Use default
342 unpacked_opts = map _UNPK_ argv
345 Putting the compiler options into temporary at-files
346 may turn out to be necessary later on if we turn hsc into
347 a pure Win32 application where I think there's a command-line
348 length limit of 255. unpacked_opts understands the @ option.
350 unpacked_opts :: [String]
356 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
361 %************************************************************************
363 \subsection{Static options}
365 %************************************************************************
369 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
370 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
371 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
374 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
375 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
376 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
377 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
378 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
379 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
382 opt_AllStrict = lookUp SLIT("-fall-strict")
383 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
384 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
385 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
386 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
387 opt_Parallel = lookUp SLIT("-fparallel")
388 opt_SMP = lookUp SLIT("-fsmp")
391 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
392 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
393 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
394 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
395 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
396 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
397 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
400 The optional '-inpackage=P' flag tells what package
401 we are compiling this module for.
402 The Prelude, for example is compiled with '-package prelude'
404 opt_InPackage = case lookup_str "-inpackage=" of
406 Nothing -> SLIT("Main") -- The package name if none is specified
408 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
409 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
410 opt_GranMacros = lookUp SLIT("-fgransim")
411 opt_HiVersion = lookup_def_int "-fhi-version=" 0 -- what version we're compiling.
412 opt_HistorySize = lookup_def_int "-fhistory-size" 20
413 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
414 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
415 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
416 opt_NoImplicitPrelude = lookUp SLIT("-fno-implicit-prelude")
417 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
418 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
420 -- Simplifier switches
421 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
422 -- NoPreInlining is there just to see how bad things
423 -- get if you don't do it!
424 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
425 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
426 opt_SimplCaseOfCase = lookUp SLIT("-fcase-of-case")
427 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
428 opt_SimplPedanticBottoms = lookUp SLIT("-fpedantic-bottoms")
429 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
432 opt_UF_HiFileThreshold = lookup_def_int "-funfolding-interface-threshold" (45::Int)
433 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
434 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
435 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
436 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
437 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
439 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
440 opt_UF_DearOp = ( 4 :: Int)
442 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
443 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
444 opt_Static = lookUp SLIT("-static")
445 opt_Unregisterised = lookUp SLIT("-funregisterised")
446 opt_Verbose = lookUp SLIT("-v")
449 %************************************************************************
451 \subsection{List of static hsc flags}
453 %************************************************************************
458 "-fauto-sccs-on-all-toplevs",
459 "-fauto-sccs-on-exported-toplevs",
460 "-fauto-sccs-on-individual-cafs",
461 "-fauto-sccs-on-dicts",
467 "-firrefutable-tuples",
474 "-funfold-casms-in-hi-file",
476 "-funbox-strict-fields",
477 "-femit-extern-decls",
478 "-fglobalise-toplev-names",
481 "-fignore-interface-pragmas",
482 "-fno-hi-version-check",
483 "-fno-implicit-prelude",
485 "-fomit-interface-pragmas",
487 "-fdo-eta-reduction",
488 "-fdo-lambda-eta-expansion",
491 "-fpedantic-bottoms",
492 "-fexcess-precision",
493 "-funfolding-update-in-place",
496 "-fno-prune-tydecls",
500 || any (flip prefixMatch f) [
502 "-fliberate-case-threshold",
505 "-funfolding-interface-threshold",
506 "-funfolding-creation-threshold",
507 "-funfolding-use-threshold",
508 "-funfolding-fun-discount",
509 "-funfolding-keeness-factor"
513 %************************************************************************
515 \subsection{Switch ordering}
517 %************************************************************************
519 These things behave just like enumeration types.
522 instance Eq SimplifierSwitch where
523 a == b = tagOf_SimplSwitch a ==# tagOf_SimplSwitch b
525 instance Ord SimplifierSwitch where
526 a < b = tagOf_SimplSwitch a <# tagOf_SimplSwitch b
527 a <= b = tagOf_SimplSwitch a <=# tagOf_SimplSwitch b
530 tagOf_SimplSwitch (SimplInlinePhase _) = _ILIT(1)
531 tagOf_SimplSwitch (MaxSimplifierIterations _) = _ILIT(2)
532 tagOf_SimplSwitch DontApplyRules = _ILIT(3)
533 tagOf_SimplSwitch SimplLetToCase = _ILIT(4)
534 tagOf_SimplSwitch NoCaseOfCase = _ILIT(5)
536 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
538 lAST_SIMPL_SWITCH_TAG = 5
541 %************************************************************************
543 \subsection{Switch lookup}
545 %************************************************************************
548 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
549 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
550 -- in the list; defaults right at the end.
552 tidied_on_switches = foldl rm_dups [] on_switches
553 -- The fold*l* ensures that we keep the latest switches;
554 -- ie the ones that occur earliest in the list.
556 sw_tbl :: Array Int SwitchResult
557 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
561 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
563 defined_elems = map mk_assoc_elem tidied_on_switches
565 -- (avoid some unboxing, bounds checking, and other horrible things:)
566 #if __GLASGOW_HASKELL__ < 405
567 case sw_tbl of { Array bounds_who_needs_'em stuff ->
569 case sw_tbl of { Array _ _ stuff ->
572 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
573 #if __GLASGOW_HASKELL__ < 400
575 #elif __GLASGOW_HASKELL__ < 403
582 mk_assoc_elem k@(MaxSimplifierIterations lvl)
583 = (iBox (tagOf_SimplSwitch k), SwInt lvl)
584 mk_assoc_elem k@(SimplInlinePhase n)
585 = (iBox (tagOf_SimplSwitch k), SwInt n)
587 = (iBox (tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
589 -- cannot have duplicates if we are going to use the array thing
590 rm_dups switches_so_far switch
591 = if switch `is_elem` switches_so_far
593 else switch : switches_so_far
595 sw `is_elem` [] = False
596 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) ==# (tagOf_SimplSwitch s)
600 Default settings for simplifier switches
603 defaultSimplSwitches = [MaxSimplifierIterations 1]
606 %************************************************************************
608 \subsection{Misc functions for command-line options}
610 %************************************************************************
614 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
616 switchIsOn lookup_fn switch
617 = case (lookup_fn switch) of
618 SwBool False -> False
621 intSwitchSet :: (switch -> SwitchResult)
625 intSwitchSet lookup_fn switch
626 = case (lookup_fn (switch (panic "intSwitchSet"))) of
627 SwInt int -> Just int
632 startsWith :: String -> String -> Maybe String
633 -- startsWith pfx (pfx++rest) = Just rest
635 startsWith [] str = Just str
636 startsWith (c:cs) (s:ss)
637 = if c /= s then Nothing else startsWith cs ss
638 startsWith _ [] = Nothing
640 endsWith :: String -> String -> Maybe String
642 = case (startsWith (reverse cs) (reverse ss)) of
644 Just rs -> Just (reverse rs)