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,
34 -- other dynamic flags
40 -- sets of warning opts
46 opt_AutoSccsOnAllToplevs,
47 opt_AutoSccsOnExportedToplevs,
48 opt_AutoSccsOnIndividualCafs,
56 opt_MaxContextReductionDepth,
57 opt_IrrefutableTuples,
61 opt_NoMonomorphismRestriction,
68 opt_LiberateCaseThreshold,
69 opt_StgDoLetNoEscapes,
72 opt_UnboxStrictFields,
73 opt_SimplNoPreInlining,
74 opt_SimplDoEtaReduction,
75 opt_SimplDoLambdaEtaExpansion,
77 opt_SimplExcessPrecision,
80 opt_UF_CreationThreshold,
82 opt_UF_FunAppDiscount,
91 opt_EnsureSplittableC,
96 opt_IgnoreIfacePragmas,
99 opt_OmitInterfacePragmas,
106 #include "HsVersions.h"
108 import Array ( array, (//) )
110 import IOExts ( IORef, readIORef )
111 import Constants -- Default values for some flags
116 import Maybes ( firstJust )
117 import Panic ( panic )
119 #if __GLASGOW_HASKELL__ < 301
120 import ArrBase ( Array(..) )
122 import PrelArr ( Array(..) )
126 %************************************************************************
128 \subsection{Command-line options}
130 %************************************************************************
132 The hsc command-line options are split into two categories:
137 Static flags are represented by top-level values of type Bool or Int,
138 for example. They therefore have the same value throughout the
141 Dynamic flags are represented by an abstract type, DynFlags, which is
142 passed into hsc by the compilation manager for every compilation.
143 Dynamic flags are those that change on a per-compilation basis,
144 perhaps because they may be present in the OPTIONS pragma at the top
147 Other flag-related blurb:
149 A list of {\em ToDo}s is things to be done in a particular part of
150 processing. A (fictitious) example for the Core-to-Core simplifier
151 might be: run the simplifier, then run the strictness analyser, then
152 run the simplifier again (three ``todos'').
154 There are three ``to-do processing centers'' at the moment. In the
155 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
156 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
157 (\tr{simplStg/SimplStg.lhs}).
159 %************************************************************************
161 \subsection{Datatypes associated with command-line options}
163 %************************************************************************
167 = SwBool Bool -- on/off
168 | SwString FAST_STRING -- nothing or a String
169 | SwInt Int -- nothing or an Int
173 data CoreToDo -- These are diff core-to-core passes,
174 -- which may be invoked in any order,
175 -- as many times as you like.
177 = CoreDoSimplify -- The core-to-core simplifier.
178 (SimplifierSwitch -> SwitchResult)
179 -- Each run of the simplifier can take a different
180 -- set of simplifier-specific flags.
182 | CoreDoFloatOutwards Bool -- True <=> float lambdas to top level
187 | CoreDoWorkerWrapper
195 | CoreDoNothing -- useful when building up lists of these things
200 = StgDoMassageForProfiling -- should be (next to) last
201 -- There's also setStgVarInfo, but its absolute "lastness"
202 -- is so critical that it is hardwired in (no flag).
207 data SimplifierSwitch
208 = MaxSimplifierIterations Int
209 | SimplInlinePhase Int
215 %************************************************************************
217 \subsection{Dynamic command-line options}
219 %************************************************************************
232 | Opt_D_dump_inlinings
233 | Opt_D_dump_occur_anal
238 | Opt_D_dump_simpl_iterations
248 | Opt_D_dump_worker_wrapper
249 | Opt_D_dump_rn_trace
250 | Opt_D_dump_rn_stats
252 | Opt_D_dump_simpl_stats
253 | Opt_D_dump_tc_trace
256 | Opt_D_verbose_core2core
257 | Opt_D_verbose_stg2stg
259 | Opt_D_dump_hi_diffs
260 | Opt_D_dump_minimal_imports
265 | Opt_WarnDuplicateExports
267 | Opt_WarnIncompletePatterns
268 | Opt_WarnMissingFields
269 | Opt_WarnMissingMethods
270 | Opt_WarnMissingSigs
271 | Opt_WarnNameShadowing
272 | Opt_WarnOverlappingPatterns
273 | Opt_WarnSimplePatterns
274 | Opt_WarnTypeDefaults
275 | Opt_WarnUnusedBinds
276 | Opt_WarnUnusedImports
277 | Opt_WarnUnusedMatches
278 | Opt_WarnDeprecations
281 | Opt_AllowOverlappingInstances
282 | Opt_AllowUndecidableInstances
285 | Opt_NoImplicitPrelude
289 data DynFlags = DynFlags {
290 coreToDo :: [CoreToDo],
291 stgToDo :: [StgToDo],
293 hscOutName :: String, -- name of the output file
294 hscStubHOutName :: String, -- name of the .stub_h output file
295 hscStubCOutName :: String, -- name of the .stub_c output file
296 verbosity :: Int, -- verbosity level
297 cppFlag :: Bool, -- preprocess with cpp?
298 stolen_x86_regs :: Int,
299 cmdlineHcIncludes :: [String], -- -#includes
301 -- options for particular phases
312 defaultDynFlags = DynFlags {
313 coreToDo = [], stgToDo = [],
316 hscStubHOutName = "", hscStubCOutName = "",
320 cmdlineHcIncludes = [],
326 flags = standardWarnings,
332 0 | print errors & warnings only
333 1 | minimal verbosity: print "compiling M ... done." for each module.
334 2 | equivalent to -dshow-passes
335 3 | equivalent to existing "ghc -v"
336 4 | "ghc -v -ddump-most"
337 5 | "ghc -v -ddump-all"
340 dopt :: DynFlag -> DynFlags -> Bool
341 dopt f dflags = f `elem` (flags dflags)
343 dopt_CoreToDo :: DynFlags -> [CoreToDo]
344 dopt_CoreToDo = coreToDo
346 dopt_StgToDo :: DynFlags -> [StgToDo]
347 dopt_StgToDo = stgToDo
349 dopt_OutName :: DynFlags -> String
350 dopt_OutName = hscOutName
352 dopt_set :: DynFlags -> DynFlag -> DynFlags
353 dopt_set dfs f = dfs{ flags = f : flags dfs }
355 dopt_unset :: DynFlags -> DynFlag -> DynFlags
356 dopt_unset dfs f = dfs{ flags = filter (/= f) (flags dfs) }
368 dopt_HscLang :: DynFlags -> HscLang
369 dopt_HscLang = hscLang
372 %************************************************************************
374 \subsection{Warnings}
376 %************************************************************************
380 = [ Opt_WarnDeprecations,
381 Opt_WarnOverlappingPatterns,
382 Opt_WarnMissingFields,
383 Opt_WarnMissingMethods,
384 Opt_WarnDuplicateExports
388 = standardWarnings ++
389 [ Opt_WarnUnusedBinds,
390 Opt_WarnUnusedMatches,
391 Opt_WarnUnusedImports,
392 Opt_WarnIncompletePatterns
397 [ Opt_WarnTypeDefaults,
398 Opt_WarnNameShadowing,
404 %************************************************************************
406 \subsection{Classifying command-line options}
408 %************************************************************************
411 -- v_Statis_hsc_opts is here to avoid a circular dependency with
413 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
415 lookUp :: FAST_STRING -> Bool
416 lookup_int :: String -> Maybe Int
417 lookup_def_int :: String -> Int -> Int
418 lookup_def_float :: String -> Float -> Float
419 lookup_str :: String -> Maybe String
421 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
422 packed_static_opts = map _PK_ unpacked_static_opts
424 lookUp sw = sw `elem` packed_static_opts
426 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
428 lookup_int sw = case (lookup_str sw) of
430 Just xx -> Just (read xx)
432 lookup_def_int sw def = case (lookup_str sw) of
433 Nothing -> def -- Use default
436 lookup_def_float sw def = case (lookup_str sw) of
437 Nothing -> def -- Use default
442 Putting the compiler options into temporary at-files
443 may turn out to be necessary later on if we turn hsc into
444 a pure Win32 application where I think there's a command-line
445 length limit of 255. unpacked_opts understands the @ option.
447 unpacked_opts :: [String]
451 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
453 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
458 %************************************************************************
460 \subsection{Static options}
462 %************************************************************************
466 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
467 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
468 opt_PprStyle_RawTypes = lookUp SLIT("-dppr-rawtypes")
469 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
472 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
473 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
474 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
475 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
476 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
477 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
480 opt_AllStrict = lookUp SLIT("-fall-strict")
481 opt_NoMonomorphismRestriction = lookUp SLIT("-fno-monomorphism-restriction")
482 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
483 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
484 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
485 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
486 opt_Parallel = lookUp SLIT("-fparallel")
487 opt_SMP = lookUp SLIT("-fsmp")
490 opt_NoMethodSharing = lookUp SLIT("-fno-method-sharing")
491 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
492 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
493 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
494 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
495 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
496 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
497 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
500 The optional '-inpackage=P' flag tells what package
501 we are compiling this module for.
502 The Prelude, for example is compiled with '-inpackage std'
504 opt_InPackage = case lookup_str "-inpackage=" of
506 Nothing -> SLIT("Main") -- The package name if none is specified
508 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
509 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
510 opt_GranMacros = lookUp SLIT("-fgransim")
511 opt_HiVersion = read cProjectVersionInt :: Int
512 opt_HistorySize = lookup_def_int "-fhistory-size" 20
513 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
514 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
515 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
516 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
517 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
518 opt_KeepStgTypes = lookUp SLIT("-fkeep-stg-types")
520 -- Simplifier switches
521 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
522 -- NoPreInlining is there just to see how bad things
523 -- get if you don't do it!
524 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
525 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
526 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
527 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
530 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
531 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
532 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
533 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
534 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
536 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
537 opt_UF_DearOp = ( 4 :: Int)
539 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
540 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
541 opt_Static = lookUp SLIT("-static")
542 opt_Unregisterised = lookUp SLIT("-funregisterised")
545 %************************************************************************
547 \subsection{List of static hsc flags}
549 %************************************************************************
554 "fauto-sccs-on-all-toplevs",
555 "fauto-sccs-on-exported-toplevs",
556 "fauto-sccs-on-individual-cafs",
557 "fauto-sccs-on-dicts",
562 "firrefutable-tuples",
569 "funfold-casms-in-hi-file",
571 "funbox-strict-fields",
572 "femit-extern-decls",
573 "fglobalise-toplev-names",
576 "fignore-interface-pragmas",
577 "fno-hi-version-check",
579 "fno-method-sharing",
580 "fno-monomorphism-restriction",
581 "fomit-interface-pragmas",
585 "fdo-lambda-eta-expansion",
588 "funfolding-update-in-place",
594 || any (flip prefixMatch f) [
596 "fliberate-case-threshold",
598 "funfolding-creation-threshold",
599 "funfolding-use-threshold",
600 "funfolding-fun-discount",
601 "funfolding-keeness-factor"
605 %************************************************************************
607 \subsection{Switch ordering}
609 %************************************************************************
611 These things behave just like enumeration types.
614 instance Eq SimplifierSwitch where
615 a == b = tagOf_SimplSwitch a ==# tagOf_SimplSwitch b
617 instance Ord SimplifierSwitch where
618 a < b = tagOf_SimplSwitch a <# tagOf_SimplSwitch b
619 a <= b = tagOf_SimplSwitch a <=# tagOf_SimplSwitch b
622 tagOf_SimplSwitch (SimplInlinePhase _) = _ILIT(1)
623 tagOf_SimplSwitch (MaxSimplifierIterations _) = _ILIT(2)
624 tagOf_SimplSwitch DontApplyRules = _ILIT(3)
625 tagOf_SimplSwitch SimplLetToCase = _ILIT(4)
626 tagOf_SimplSwitch NoCaseOfCase = _ILIT(5)
628 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
630 lAST_SIMPL_SWITCH_TAG = 5
633 %************************************************************************
635 \subsection{Switch lookup}
637 %************************************************************************
640 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
641 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
642 -- in the list; defaults right at the end.
644 tidied_on_switches = foldl rm_dups [] on_switches
645 -- The fold*l* ensures that we keep the latest switches;
646 -- ie the ones that occur earliest in the list.
648 sw_tbl :: Array Int SwitchResult
649 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
653 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
655 defined_elems = map mk_assoc_elem tidied_on_switches
657 -- (avoid some unboxing, bounds checking, and other horrible things:)
658 #if __GLASGOW_HASKELL__ < 405
659 case sw_tbl of { Array bounds_who_needs_'em stuff ->
661 case sw_tbl of { Array _ _ stuff ->
664 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
665 #if __GLASGOW_HASKELL__ < 400
667 #elif __GLASGOW_HASKELL__ < 403
674 mk_assoc_elem k@(MaxSimplifierIterations lvl)
675 = (iBox (tagOf_SimplSwitch k), SwInt lvl)
676 mk_assoc_elem k@(SimplInlinePhase n)
677 = (iBox (tagOf_SimplSwitch k), SwInt n)
679 = (iBox (tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
681 -- cannot have duplicates if we are going to use the array thing
682 rm_dups switches_so_far switch
683 = if switch `is_elem` switches_so_far
685 else switch : switches_so_far
687 sw `is_elem` [] = False
688 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) ==# (tagOf_SimplSwitch s)
693 %************************************************************************
695 \subsection{Misc functions for command-line options}
697 %************************************************************************
701 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
703 switchIsOn lookup_fn switch
704 = case (lookup_fn switch) of
705 SwBool False -> False
708 intSwitchSet :: (switch -> SwitchResult)
712 intSwitchSet lookup_fn switch
713 = case (lookup_fn (switch (panic "intSwitchSet"))) of
714 SwInt int -> Just int
719 startsWith :: String -> String -> Maybe String
720 -- startsWith pfx (pfx++rest) = Just rest
722 startsWith [] str = Just str
723 startsWith (c:cs) (s:ss)
724 = if c /= s then Nothing else startsWith cs ss
725 startsWith _ [] = Nothing
727 endsWith :: String -> String -> Maybe String
729 = case (startsWith (reverse cs) (reverse ss)) of
731 Just rs -> Just (reverse rs)