2 % (c) The University of Glasgow, 1996-2000
4 \section[CmdLineOpts]{Things to do with command-line options}
10 SimplifierSwitch(..), isAmongSimpl,
15 DynFlag(..), -- needed non-abstractly by DriverFlags
26 opt_PprStyle_RawTypes,
32 -- other dynamic flags
38 -- sets of warning opts
44 opt_AutoSccsOnAllToplevs,
45 opt_AutoSccsOnExportedToplevs,
46 opt_AutoSccsOnIndividualCafs,
54 opt_MaxContextReductionDepth,
55 opt_IrrefutableTuples,
59 opt_NoMonomorphismRestriction,
66 opt_LiberateCaseThreshold,
67 opt_StgDoLetNoEscapes,
70 opt_UnboxStrictFields,
71 opt_SimplNoPreInlining,
72 opt_SimplDoEtaReduction,
73 opt_SimplDoLambdaEtaExpansion,
75 opt_SimplExcessPrecision,
78 opt_UF_HiFileThreshold,
79 opt_UF_CreationThreshold,
81 opt_UF_FunAppDiscount,
90 opt_EnsureSplittableC,
95 opt_IgnoreIfacePragmas,
98 opt_OmitInterfacePragmas,
105 #include "HsVersions.h"
107 import Array ( array, (//) )
109 import IOExts ( IORef, readIORef )
110 import Constants -- Default values for some flags
115 import Maybes ( firstJust )
116 import Panic ( panic )
118 #if __GLASGOW_HASKELL__ < 301
119 import ArrBase ( Array(..) )
121 import PrelArr ( Array(..) )
125 %************************************************************************
127 \subsection{Command-line options}
129 %************************************************************************
131 The hsc command-line options are split into two categories:
136 Static flags are represented by top-level values of type Bool or Int,
137 for example. They therefore have the same value throughout the
140 Dynamic flags are represented by an abstract type, DynFlags, which is
141 passed into hsc by the compilation manager for every compilation.
142 Dynamic flags are those that change on a per-compilation basis,
143 perhaps because they may be present in the OPTIONS pragma at the top
146 Other flag-related blurb:
148 A list of {\em ToDo}s is things to be done in a particular part of
149 processing. A (fictitious) example for the Core-to-Core simplifier
150 might be: run the simplifier, then run the strictness analyser, then
151 run the simplifier again (three ``todos'').
153 There are three ``to-do processing centers'' at the moment. In the
154 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
155 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
156 (\tr{simplStg/SimplStg.lhs}).
158 %************************************************************************
160 \subsection{Datatypes associated with command-line options}
162 %************************************************************************
166 = SwBool Bool -- on/off
167 | SwString FAST_STRING -- nothing or a String
168 | SwInt Int -- nothing or an Int
172 data CoreToDo -- These are diff core-to-core passes,
173 -- which may be invoked in any order,
174 -- as many times as you like.
176 = CoreDoSimplify -- The core-to-core simplifier.
177 (SimplifierSwitch -> SwitchResult)
178 -- Each run of the simplifier can take a different
179 -- set of simplifier-specific flags.
181 | CoreDoFloatOutwards Bool -- True <=> float lambdas to top level
186 | CoreDoWorkerWrapper
194 | CoreDoNothing -- useful when building up lists of these things
199 = StgDoMassageForProfiling -- should be (next to) last
200 -- There's also setStgVarInfo, but its absolute "lastness"
201 -- is so critical that it is hardwired in (no flag).
206 data SimplifierSwitch
207 = MaxSimplifierIterations Int
208 | SimplInlinePhase Int
214 %************************************************************************
216 \subsection{Dynamic command-line options}
218 %************************************************************************
231 | Opt_D_dump_inlinings
232 | Opt_D_dump_occur_anal
237 | Opt_D_dump_simpl_iterations
247 | Opt_D_dump_worker_wrapper
248 | Opt_D_dump_rn_trace
249 | Opt_D_dump_rn_stats
251 | Opt_D_dump_simpl_stats
252 | Opt_D_dump_tc_trace
255 | Opt_D_verbose_core2core
256 | Opt_D_verbose_stg2stg
258 | Opt_D_dump_hi_diffs
259 | Opt_D_dump_minimal_imports
264 | Opt_WarnDuplicateExports
266 | Opt_WarnIncompletePatterns
267 | Opt_WarnMissingFields
268 | Opt_WarnMissingMethods
269 | Opt_WarnMissingSigs
270 | Opt_WarnNameShadowing
271 | Opt_WarnOverlappingPatterns
272 | Opt_WarnSimplePatterns
273 | Opt_WarnTypeDefaults
274 | Opt_WarnUnusedBinds
275 | Opt_WarnUnusedImports
276 | Opt_WarnUnusedMatches
277 | Opt_WarnDeprecations
280 | Opt_AllowOverlappingInstances
281 | Opt_AllowUndecidableInstances
284 | Opt_NoImplicitPrelude
288 data DynFlags = DynFlags {
289 coreToDo :: [CoreToDo],
290 stgToDo :: [StgToDo],
292 hscOutName :: String, -- name of the output file
293 verbosity :: Int, -- verbosity level
294 cppFlag :: Bool, -- preprocess with cpp?
295 stolen_x86_regs :: Int,
296 cmdlineHcIncludes :: [String], -- -#includes
298 -- options for particular phases
309 defaultDynFlags = DynFlags {
310 coreToDo = [], stgToDo = [],
316 cmdlineHcIncludes = [],
322 flags = standardWarnings,
328 0 | print errors & warnings only
329 1 | minimal verbosity: print "compiling M ... done." for each module.
330 2 | equivalent to -dshow-passes
331 3 | equivalent to existing "ghc -v"
332 4 | "ghc -v -ddump-most"
333 5 | "ghc -v -ddump-all"
336 dopt :: DynFlag -> DynFlags -> Bool
337 dopt f dflags = f `elem` (flags dflags)
339 dopt_CoreToDo :: DynFlags -> [CoreToDo]
340 dopt_CoreToDo = coreToDo
342 dopt_StgToDo :: DynFlags -> [StgToDo]
343 dopt_StgToDo = stgToDo
345 dopt_OutName :: DynFlags -> String
346 dopt_OutName = hscOutName
358 dopt_HscLang :: DynFlags -> HscLang
359 dopt_HscLang = hscLang
362 %************************************************************************
364 \subsection{Warnings}
366 %************************************************************************
370 = [ Opt_WarnDeprecations,
371 Opt_WarnOverlappingPatterns,
372 Opt_WarnMissingFields,
373 Opt_WarnMissingMethods,
374 Opt_WarnDuplicateExports
378 = standardWarnings ++
379 [ Opt_WarnUnusedBinds,
380 Opt_WarnUnusedMatches,
381 Opt_WarnUnusedImports,
382 Opt_WarnIncompletePatterns
387 [ Opt_WarnTypeDefaults,
388 Opt_WarnNameShadowing,
394 %************************************************************************
396 \subsection{Classifying command-line options}
398 %************************************************************************
401 -- v_Statis_hsc_opts is here to avoid a circular dependency with
403 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
405 lookUp :: FAST_STRING -> Bool
406 lookup_int :: String -> Maybe Int
407 lookup_def_int :: String -> Int -> Int
408 lookup_def_float :: String -> Float -> Float
409 lookup_str :: String -> Maybe String
411 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
412 packed_static_opts = map _PK_ unpacked_static_opts
414 lookUp sw = sw `elem` packed_static_opts
416 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
418 lookup_int sw = case (lookup_str sw) of
420 Just xx -> Just (read xx)
422 lookup_def_int sw def = case (lookup_str sw) of
423 Nothing -> def -- Use default
426 lookup_def_float sw def = case (lookup_str sw) of
427 Nothing -> def -- Use default
432 Putting the compiler options into temporary at-files
433 may turn out to be necessary later on if we turn hsc into
434 a pure Win32 application where I think there's a command-line
435 length limit of 255. unpacked_opts understands the @ option.
437 unpacked_opts :: [String]
441 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
443 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
448 %************************************************************************
450 \subsection{Static options}
452 %************************************************************************
456 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
457 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
458 opt_PprStyle_RawTypes = lookUp SLIT("-dppr-rawtypes")
459 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
462 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
463 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
464 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
465 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
466 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
467 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
470 opt_AllStrict = lookUp SLIT("-fall-strict")
471 opt_NoMonomorphismRestriction = lookUp SLIT("-fno-monomorphism-restriction")
472 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
473 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
474 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
475 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
476 opt_Parallel = lookUp SLIT("-fparallel")
477 opt_SMP = lookUp SLIT("-fsmp")
480 opt_NoMethodSharing = lookUp SLIT("-fno-method-sharing")
481 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
482 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
483 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
484 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
485 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
486 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
487 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
490 The optional '-inpackage=P' flag tells what package
491 we are compiling this module for.
492 The Prelude, for example is compiled with '-inpackage prelude'
494 opt_InPackage = case lookup_str "-inpackage=" of
496 Nothing -> SLIT("Main") -- The package name if none is specified
498 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
499 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
500 opt_GranMacros = lookUp SLIT("-fgransim")
501 opt_HiVersion = read cProjectVersionInt :: Int
502 opt_HistorySize = lookup_def_int "-fhistory-size" 20
503 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
504 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
505 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
506 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
507 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
508 opt_KeepStgTypes = lookUp SLIT("-fkeep-stg-types")
510 -- Simplifier switches
511 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
512 -- NoPreInlining is there just to see how bad things
513 -- get if you don't do it!
514 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
515 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
516 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
517 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
520 opt_UF_HiFileThreshold = lookup_def_int "-funfolding-interface-threshold" (45::Int)
521 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
522 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
523 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
524 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
525 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
527 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
528 opt_UF_DearOp = ( 4 :: Int)
530 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
531 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
532 opt_Static = lookUp SLIT("-static")
533 opt_Unregisterised = lookUp SLIT("-funregisterised")
536 %************************************************************************
538 \subsection{List of static hsc flags}
540 %************************************************************************
545 "fauto-sccs-on-all-toplevs",
546 "fauto-sccs-on-exported-toplevs",
547 "fauto-sccs-on-individual-cafs",
548 "fauto-sccs-on-dicts",
553 "firrefutable-tuples",
560 "funfold-casms-in-hi-file",
562 "funbox-strict-fields",
563 "femit-extern-decls",
564 "fglobalise-toplev-names",
567 "fignore-interface-pragmas",
568 "fno-hi-version-check",
570 "fno-method-sharing",
571 "fno-monomorphism-restriction",
572 "fomit-interface-pragmas",
576 "fdo-lambda-eta-expansion",
579 "funfolding-update-in-place",
585 || any (flip prefixMatch f) [
587 "fliberate-case-threshold",
589 "funfolding-interface-threshold",
590 "funfolding-creation-threshold",
591 "funfolding-use-threshold",
592 "funfolding-fun-discount",
593 "funfolding-keeness-factor"
597 %************************************************************************
599 \subsection{Switch ordering}
601 %************************************************************************
603 These things behave just like enumeration types.
606 instance Eq SimplifierSwitch where
607 a == b = tagOf_SimplSwitch a ==# tagOf_SimplSwitch b
609 instance Ord SimplifierSwitch where
610 a < b = tagOf_SimplSwitch a <# tagOf_SimplSwitch b
611 a <= b = tagOf_SimplSwitch a <=# tagOf_SimplSwitch b
614 tagOf_SimplSwitch (SimplInlinePhase _) = _ILIT(1)
615 tagOf_SimplSwitch (MaxSimplifierIterations _) = _ILIT(2)
616 tagOf_SimplSwitch DontApplyRules = _ILIT(3)
617 tagOf_SimplSwitch SimplLetToCase = _ILIT(4)
618 tagOf_SimplSwitch NoCaseOfCase = _ILIT(5)
620 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
622 lAST_SIMPL_SWITCH_TAG = 5
625 %************************************************************************
627 \subsection{Switch lookup}
629 %************************************************************************
632 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
633 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
634 -- in the list; defaults right at the end.
636 tidied_on_switches = foldl rm_dups [] on_switches
637 -- The fold*l* ensures that we keep the latest switches;
638 -- ie the ones that occur earliest in the list.
640 sw_tbl :: Array Int SwitchResult
641 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
645 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
647 defined_elems = map mk_assoc_elem tidied_on_switches
649 -- (avoid some unboxing, bounds checking, and other horrible things:)
650 #if __GLASGOW_HASKELL__ < 405
651 case sw_tbl of { Array bounds_who_needs_'em stuff ->
653 case sw_tbl of { Array _ _ stuff ->
656 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
657 #if __GLASGOW_HASKELL__ < 400
659 #elif __GLASGOW_HASKELL__ < 403
666 mk_assoc_elem k@(MaxSimplifierIterations lvl)
667 = (iBox (tagOf_SimplSwitch k), SwInt lvl)
668 mk_assoc_elem k@(SimplInlinePhase n)
669 = (iBox (tagOf_SimplSwitch k), SwInt n)
671 = (iBox (tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
673 -- cannot have duplicates if we are going to use the array thing
674 rm_dups switches_so_far switch
675 = if switch `is_elem` switches_so_far
677 else switch : switches_so_far
679 sw `is_elem` [] = False
680 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) ==# (tagOf_SimplSwitch s)
685 %************************************************************************
687 \subsection{Misc functions for command-line options}
689 %************************************************************************
693 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
695 switchIsOn lookup_fn switch
696 = case (lookup_fn switch) of
697 SwBool False -> False
700 intSwitchSet :: (switch -> SwitchResult)
704 intSwitchSet lookup_fn switch
705 = case (lookup_fn (switch (panic "intSwitchSet"))) of
706 SwInt int -> Just int
711 startsWith :: String -> String -> Maybe String
712 -- startsWith pfx (pfx++rest) = Just rest
714 startsWith [] str = Just str
715 startsWith (c:cs) (s:ss)
716 = if c /= s then Nothing else startsWith cs ss
717 startsWith _ [] = Nothing
719 endsWith :: String -> String -> Maybe String
721 = case (startsWith (reverse cs) (reverse ss)) of
723 Just rs -> Just (reverse rs)