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
24 opt_PprStyle_RawTypes,
30 -- other dynamic flags
37 opt_AutoSccsOnAllToplevs,
38 opt_AutoSccsOnExportedToplevs,
39 opt_AutoSccsOnIndividualCafs,
47 opt_MaxContextReductionDepth,
48 opt_IrrefutableTuples,
56 opt_LiberateCaseThreshold,
57 opt_StgDoLetNoEscapes,
60 opt_UnboxStrictFields,
61 opt_SimplNoPreInlining,
62 opt_SimplDoEtaReduction,
63 opt_SimplDoLambdaEtaExpansion,
66 opt_SimplPedanticBottoms,
67 opt_SimplExcessPrecision,
70 opt_UF_HiFileThreshold,
71 opt_UF_CreationThreshold,
73 opt_UF_FunAppDiscount,
82 opt_EnsureSplittableC,
87 opt_IgnoreIfacePragmas,
90 opt_OmitInterfacePragmas,
97 #include "HsVersions.h"
99 import Array ( array, (//) )
101 import IOExts ( IORef, readIORef )
102 import Constants -- Default values for some flags
107 import Maybes ( firstJust )
108 import Panic ( panic )
110 #if __GLASGOW_HASKELL__ < 301
111 import ArrBase ( Array(..) )
113 import PrelArr ( Array(..) )
117 %************************************************************************
119 \subsection{Command-line options}
121 %************************************************************************
123 The hsc command-line options are split into two categories:
128 Static flags are represented by top-level values of type Bool or Int,
129 for example. They therefore have the same value throughout the
132 Dynamic flags are represented by an abstract type, DynFlags, which is
133 passed into hsc by the compilation manager for every compilation.
134 Dynamic flags are those that change on a per-compilation basis,
135 perhaps because they may be present in the OPTIONS pragma at the top
138 Other flag-related blurb:
140 A list of {\em ToDo}s is things to be done in a particular part of
141 processing. A (fictitious) example for the Core-to-Core simplifier
142 might be: run the simplifier, then run the strictness analyser, then
143 run the simplifier again (three ``todos'').
145 There are three ``to-do processing centers'' at the moment. In the
146 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
147 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
148 (\tr{simplStg/SimplStg.lhs}).
150 %************************************************************************
152 \subsection{Datatypes associated with command-line options}
154 %************************************************************************
158 = SwBool Bool -- on/off
159 | SwString FAST_STRING -- nothing or a String
160 | SwInt Int -- nothing or an Int
164 data CoreToDo -- These are diff core-to-core passes,
165 -- which may be invoked in any order,
166 -- as many times as you like.
168 = CoreDoSimplify -- The core-to-core simplifier.
169 (SimplifierSwitch -> SwitchResult)
170 -- Each run of the simplifier can take a different
171 -- set of simplifier-specific flags.
173 | CoreDoFloatOutwards Bool -- True <=> float lambdas to top level
178 | CoreDoWorkerWrapper
185 | CoreDoNothing -- useful when building up lists of these things
192 | StgDoMassageForProfiling -- should be (next to) last
193 -- There's also setStgVarInfo, but its absolute "lastness"
194 -- is so critical that it is hardwired in (no flag).
199 data SimplifierSwitch
200 = MaxSimplifierIterations Int
201 | SimplInlinePhase Int
207 %************************************************************************
209 \subsection{Dynamic command-line options}
211 %************************************************************************
226 | Opt_D_dump_inlinings
227 | Opt_D_dump_occur_anal
232 | Opt_D_dump_simpl_iterations
241 | Opt_D_dump_worker_wrapper
243 | Opt_D_dump_rn_trace
244 | Opt_D_dump_rn_stats
246 | Opt_D_dump_simpl_stats
248 | Opt_D_verbose_core2core
249 | 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
277 | Opt_NoImplicitPrelude
283 data DynFlags = DynFlags {
284 coreToDo :: [CoreToDo],
285 stgToDo :: [StgToDo],
287 hscOutName :: String, -- name of the file in which to place output
291 dopt :: DynFlag -> DynFlags -> Bool
292 dopt f dflags = f `elem` (flags dflags)
294 dopt_CoreToDo :: DynFlags -> [CoreToDo]
295 dopt_CoreToDo = coreToDo
297 dopt_StgToDo :: DynFlags -> [StgToDo]
298 dopt_StgToDo = stgToDo
300 dopt_OutName :: DynFlags -> String
301 dopt_OutName = hscOutName
310 dopt_HscLang :: DynFlags -> HscLang
311 dopt_HscLang = hscLang
314 %************************************************************************
316 \subsection{Classifying command-line options}
318 %************************************************************************
321 -- v_Statis_hsc_opts is here to avoid a circular dependency with
323 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
325 lookUp :: FAST_STRING -> Bool
326 lookup_int :: String -> Maybe Int
327 lookup_def_int :: String -> Int -> Int
328 lookup_def_float :: String -> Float -> Float
329 lookup_str :: String -> Maybe String
331 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
332 packed_static_opts = map _PK_ unpacked_static_opts
334 lookUp sw = sw `elem` packed_static_opts
336 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
338 lookup_int sw = case (lookup_str sw) of
340 Just xx -> Just (read xx)
342 lookup_def_int sw def = case (lookup_str sw) of
343 Nothing -> def -- Use default
346 lookup_def_float sw def = case (lookup_str sw) of
347 Nothing -> def -- Use default
352 Putting the compiler options into temporary at-files
353 may turn out to be necessary later on if we turn hsc into
354 a pure Win32 application where I think there's a command-line
355 length limit of 255. unpacked_opts understands the @ option.
357 unpacked_opts :: [String]
361 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
363 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
368 %************************************************************************
370 \subsection{Static options}
372 %************************************************************************
376 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
377 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
378 opt_PprStyle_RawTypes = lookUp SLIT("-dppr-rawtypes")
379 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
382 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
383 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
384 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
385 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
386 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
387 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
390 opt_AllStrict = lookUp SLIT("-fall-strict")
391 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
392 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
393 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
394 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
395 opt_Parallel = lookUp SLIT("-fparallel")
396 opt_SMP = lookUp SLIT("-fsmp")
399 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
400 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
401 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
402 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
403 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
404 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
405 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
408 The optional '-inpackage=P' flag tells what package
409 we are compiling this module for.
410 The Prelude, for example is compiled with '-package prelude'
412 opt_InPackage = case lookup_str "-inpackage=" of
414 Nothing -> SLIT("Main") -- The package name if none is specified
416 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
417 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
418 opt_GranMacros = lookUp SLIT("-fgransim")
419 opt_HiVersion = read cProjectVersionInt :: Int
420 opt_HistorySize = lookup_def_int "-fhistory-size" 20
421 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
422 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
423 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
424 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
425 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
427 -- Simplifier switches
428 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
429 -- NoPreInlining is there just to see how bad things
430 -- get if you don't do it!
431 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
432 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
433 opt_SimplCaseOfCase = lookUp SLIT("-fcase-of-case")
434 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
435 opt_SimplPedanticBottoms = lookUp SLIT("-fpedantic-bottoms")
436 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
439 opt_UF_HiFileThreshold = lookup_def_int "-funfolding-interface-threshold" (45::Int)
440 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
441 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
442 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
443 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
444 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
446 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
447 opt_UF_DearOp = ( 4 :: Int)
449 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
450 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
451 opt_Static = lookUp SLIT("-static")
452 opt_Unregisterised = lookUp SLIT("-funregisterised")
455 %************************************************************************
457 \subsection{List of static hsc flags}
459 %************************************************************************
464 "fauto-sccs-on-all-toplevs",
465 "fauto-sccs-on-exported-toplevs",
466 "fauto-sccs-on-individual-cafs",
467 "fauto-sccs-on-dicts",
472 "firrefutable-tuples",
479 "funfold-casms-in-hi-file",
481 "funbox-strict-fields",
482 "femit-extern-decls",
483 "fglobalise-toplev-names",
486 "fignore-interface-pragmas",
487 "fno-hi-version-check",
488 "fno-implicit-prelude",
490 "fomit-interface-pragmas",
493 "fdo-lambda-eta-expansion",
498 "funfolding-update-in-place",
505 || any (flip prefixMatch f) [
507 "fliberate-case-threshold",
509 "funfolding-interface-threshold",
510 "funfolding-creation-threshold",
511 "funfolding-use-threshold",
512 "funfolding-fun-discount",
513 "funfolding-keeness-factor"
517 %************************************************************************
519 \subsection{Switch ordering}
521 %************************************************************************
523 These things behave just like enumeration types.
526 instance Eq SimplifierSwitch where
527 a == b = tagOf_SimplSwitch a ==# tagOf_SimplSwitch b
529 instance Ord SimplifierSwitch where
530 a < b = tagOf_SimplSwitch a <# tagOf_SimplSwitch b
531 a <= b = tagOf_SimplSwitch a <=# tagOf_SimplSwitch b
534 tagOf_SimplSwitch (SimplInlinePhase _) = _ILIT(1)
535 tagOf_SimplSwitch (MaxSimplifierIterations _) = _ILIT(2)
536 tagOf_SimplSwitch DontApplyRules = _ILIT(3)
537 tagOf_SimplSwitch SimplLetToCase = _ILIT(4)
538 tagOf_SimplSwitch NoCaseOfCase = _ILIT(5)
540 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
542 lAST_SIMPL_SWITCH_TAG = 5
545 %************************************************************************
547 \subsection{Switch lookup}
549 %************************************************************************
552 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
553 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
554 -- in the list; defaults right at the end.
556 tidied_on_switches = foldl rm_dups [] on_switches
557 -- The fold*l* ensures that we keep the latest switches;
558 -- ie the ones that occur earliest in the list.
560 sw_tbl :: Array Int SwitchResult
561 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
565 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
567 defined_elems = map mk_assoc_elem tidied_on_switches
569 -- (avoid some unboxing, bounds checking, and other horrible things:)
570 #if __GLASGOW_HASKELL__ < 405
571 case sw_tbl of { Array bounds_who_needs_'em stuff ->
573 case sw_tbl of { Array _ _ stuff ->
576 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
577 #if __GLASGOW_HASKELL__ < 400
579 #elif __GLASGOW_HASKELL__ < 403
586 mk_assoc_elem k@(MaxSimplifierIterations lvl)
587 = (iBox (tagOf_SimplSwitch k), SwInt lvl)
588 mk_assoc_elem k@(SimplInlinePhase n)
589 = (iBox (tagOf_SimplSwitch k), SwInt n)
591 = (iBox (tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
593 -- cannot have duplicates if we are going to use the array thing
594 rm_dups switches_so_far switch
595 = if switch `is_elem` switches_so_far
597 else switch : switches_so_far
599 sw `is_elem` [] = False
600 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) ==# (tagOf_SimplSwitch s)
605 %************************************************************************
607 \subsection{Misc functions for command-line options}
609 %************************************************************************
613 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
615 switchIsOn lookup_fn switch
616 = case (lookup_fn switch) of
617 SwBool False -> False
620 intSwitchSet :: (switch -> SwitchResult)
624 intSwitchSet lookup_fn switch
625 = case (lookup_fn (switch (panic "intSwitchSet"))) of
626 SwInt int -> Just int
631 startsWith :: String -> String -> Maybe String
632 -- startsWith pfx (pfx++rest) = Just rest
634 startsWith [] str = Just str
635 startsWith (c:cs) (s:ss)
636 = if c /= s then Nothing else startsWith cs ss
637 startsWith _ [] = Nothing
639 endsWith :: String -> String -> Maybe String
641 = case (startsWith (reverse cs) (reverse ss)) of
643 Just rs -> Just (reverse rs)