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
282 | Opt_AllowOverlappingInstances
283 | Opt_AllowUndecidableInstances
286 | Opt_NoImplicitPrelude
290 data DynFlags = DynFlags {
291 coreToDo :: [CoreToDo],
292 stgToDo :: [StgToDo],
294 hscOutName :: String, -- name of the output file
295 hscStubHOutName :: String, -- name of the .stub_h output file
296 hscStubCOutName :: String, -- name of the .stub_c output file
297 verbosity :: Int, -- verbosity level
298 cppFlag :: Bool, -- preprocess with cpp?
299 stolen_x86_regs :: Int,
300 cmdlineHcIncludes :: [String], -- -#includes
302 -- options for particular phases
313 defaultDynFlags = DynFlags {
314 coreToDo = [], stgToDo = [],
317 hscStubHOutName = "", hscStubCOutName = "",
321 cmdlineHcIncludes = [],
327 flags = standardWarnings,
333 0 | print errors & warnings only
334 1 | minimal verbosity: print "compiling M ... done." for each module.
335 2 | equivalent to -dshow-passes
336 3 | equivalent to existing "ghc -v"
337 4 | "ghc -v -ddump-most"
338 5 | "ghc -v -ddump-all"
341 dopt :: DynFlag -> DynFlags -> Bool
342 dopt f dflags = f `elem` (flags dflags)
344 dopt_CoreToDo :: DynFlags -> [CoreToDo]
345 dopt_CoreToDo = coreToDo
347 dopt_StgToDo :: DynFlags -> [StgToDo]
348 dopt_StgToDo = stgToDo
350 dopt_OutName :: DynFlags -> String
351 dopt_OutName = hscOutName
353 dopt_set :: DynFlags -> DynFlag -> DynFlags
354 dopt_set dfs f = dfs{ flags = f : flags dfs }
356 dopt_unset :: DynFlags -> DynFlag -> DynFlags
357 dopt_unset dfs f = dfs{ flags = filter (/= f) (flags dfs) }
369 dopt_HscLang :: DynFlags -> HscLang
370 dopt_HscLang = hscLang
373 %************************************************************************
375 \subsection{Warnings}
377 %************************************************************************
381 = [ Opt_WarnDeprecations,
382 Opt_WarnOverlappingPatterns,
383 Opt_WarnMissingFields,
384 Opt_WarnMissingMethods,
385 Opt_WarnDuplicateExports,
389 = standardWarnings ++
390 [ Opt_WarnUnusedBinds,
391 Opt_WarnUnusedMatches,
392 Opt_WarnUnusedImports,
393 Opt_WarnIncompletePatterns,
399 [ Opt_WarnTypeDefaults,
400 Opt_WarnNameShadowing,
406 %************************************************************************
408 \subsection{Classifying command-line options}
410 %************************************************************************
413 -- v_Statis_hsc_opts is here to avoid a circular dependency with
415 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
417 lookUp :: FAST_STRING -> Bool
418 lookup_int :: String -> Maybe Int
419 lookup_def_int :: String -> Int -> Int
420 lookup_def_float :: String -> Float -> Float
421 lookup_str :: String -> Maybe String
423 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
424 packed_static_opts = map _PK_ unpacked_static_opts
426 lookUp sw = sw `elem` packed_static_opts
428 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
430 lookup_int sw = case (lookup_str sw) of
432 Just xx -> Just (read xx)
434 lookup_def_int sw def = case (lookup_str sw) of
435 Nothing -> def -- Use default
438 lookup_def_float sw def = case (lookup_str sw) of
439 Nothing -> def -- Use default
444 Putting the compiler options into temporary at-files
445 may turn out to be necessary later on if we turn hsc into
446 a pure Win32 application where I think there's a command-line
447 length limit of 255. unpacked_opts understands the @ option.
449 unpacked_opts :: [String]
453 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
455 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
460 %************************************************************************
462 \subsection{Static options}
464 %************************************************************************
468 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
469 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
470 opt_PprStyle_RawTypes = lookUp SLIT("-dppr-rawtypes")
471 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
474 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
475 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
476 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
477 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
478 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
479 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
482 opt_AllStrict = lookUp SLIT("-fall-strict")
483 opt_NoMonomorphismRestriction = lookUp SLIT("-fno-monomorphism-restriction")
484 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
485 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
486 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
487 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
488 opt_Parallel = lookUp SLIT("-fparallel")
489 opt_SMP = lookUp SLIT("-fsmp")
492 opt_NoMethodSharing = lookUp SLIT("-fno-method-sharing")
493 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
494 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
495 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
496 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
497 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
498 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
499 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
502 The optional '-inpackage=P' flag tells what package
503 we are compiling this module for.
504 The Prelude, for example is compiled with '-inpackage std'
506 opt_InPackage = case lookup_str "-inpackage=" of
508 Nothing -> SLIT("Main") -- The package name if none is specified
510 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
511 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
512 opt_GranMacros = lookUp SLIT("-fgransim")
513 opt_HiVersion = read cProjectVersionInt :: Int
514 opt_HistorySize = lookup_def_int "-fhistory-size" 20
515 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
516 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
517 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
518 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
519 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
520 opt_KeepStgTypes = lookUp SLIT("-fkeep-stg-types")
522 -- Simplifier switches
523 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
524 -- NoPreInlining is there just to see how bad things
525 -- get if you don't do it!
526 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
527 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
528 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
529 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
532 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
533 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
534 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
535 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
536 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
538 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
539 opt_UF_DearOp = ( 4 :: Int)
541 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
542 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
543 opt_Static = lookUp SLIT("-static")
544 opt_Unregisterised = lookUp SLIT("-funregisterised")
547 %************************************************************************
549 \subsection{List of static hsc flags}
551 %************************************************************************
556 "fauto-sccs-on-all-toplevs",
557 "fauto-sccs-on-exported-toplevs",
558 "fauto-sccs-on-individual-cafs",
559 "fauto-sccs-on-dicts",
564 "firrefutable-tuples",
571 "funfold-casms-in-hi-file",
573 "funbox-strict-fields",
574 "femit-extern-decls",
575 "fglobalise-toplev-names",
578 "fignore-interface-pragmas",
579 "fno-hi-version-check",
581 "fno-method-sharing",
582 "fno-monomorphism-restriction",
583 "fomit-interface-pragmas",
587 "fdo-lambda-eta-expansion",
590 "funfolding-update-in-place",
596 || any (flip prefixMatch f) [
598 "fliberate-case-threshold",
600 "funfolding-creation-threshold",
601 "funfolding-use-threshold",
602 "funfolding-fun-discount",
603 "funfolding-keeness-factor"
607 %************************************************************************
609 \subsection{Switch ordering}
611 %************************************************************************
613 These things behave just like enumeration types.
616 instance Eq SimplifierSwitch where
617 a == b = tagOf_SimplSwitch a ==# tagOf_SimplSwitch b
619 instance Ord SimplifierSwitch where
620 a < b = tagOf_SimplSwitch a <# tagOf_SimplSwitch b
621 a <= b = tagOf_SimplSwitch a <=# tagOf_SimplSwitch b
624 tagOf_SimplSwitch (SimplInlinePhase _) = _ILIT(1)
625 tagOf_SimplSwitch (MaxSimplifierIterations _) = _ILIT(2)
626 tagOf_SimplSwitch DontApplyRules = _ILIT(3)
627 tagOf_SimplSwitch SimplLetToCase = _ILIT(4)
628 tagOf_SimplSwitch NoCaseOfCase = _ILIT(5)
630 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
632 lAST_SIMPL_SWITCH_TAG = 5
635 %************************************************************************
637 \subsection{Switch lookup}
639 %************************************************************************
642 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
643 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
644 -- in the list; defaults right at the end.
646 tidied_on_switches = foldl rm_dups [] on_switches
647 -- The fold*l* ensures that we keep the latest switches;
648 -- ie the ones that occur earliest in the list.
650 sw_tbl :: Array Int SwitchResult
651 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
655 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
657 defined_elems = map mk_assoc_elem tidied_on_switches
659 -- (avoid some unboxing, bounds checking, and other horrible things:)
660 #if __GLASGOW_HASKELL__ < 405
661 case sw_tbl of { Array bounds_who_needs_'em stuff ->
663 case sw_tbl of { Array _ _ stuff ->
666 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
667 #if __GLASGOW_HASKELL__ < 400
669 #elif __GLASGOW_HASKELL__ < 403
676 mk_assoc_elem k@(MaxSimplifierIterations lvl)
677 = (iBox (tagOf_SimplSwitch k), SwInt lvl)
678 mk_assoc_elem k@(SimplInlinePhase n)
679 = (iBox (tagOf_SimplSwitch k), SwInt n)
681 = (iBox (tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
683 -- cannot have duplicates if we are going to use the array thing
684 rm_dups switches_so_far switch
685 = if switch `is_elem` switches_so_far
687 else switch : switches_so_far
689 sw `is_elem` [] = False
690 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) ==# (tagOf_SimplSwitch s)
695 %************************************************************************
697 \subsection{Misc functions for command-line options}
699 %************************************************************************
703 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
705 switchIsOn lookup_fn switch
706 = case (lookup_fn switch) of
707 SwBool False -> False
710 intSwitchSet :: (switch -> SwitchResult)
714 intSwitchSet lookup_fn switch
715 = case (lookup_fn (switch (panic "intSwitchSet"))) of
716 SwInt int -> Just int
721 startsWith :: String -> String -> Maybe String
722 -- startsWith pfx (pfx++rest) = Just rest
724 startsWith [] str = Just str
725 startsWith (c:cs) (s:ss)
726 = if c /= s then Nothing else startsWith cs ss
727 startsWith _ [] = Nothing
729 endsWith :: String -> String -> Maybe String
731 = case (startsWith (reverse cs) (reverse ss)) of
733 Just rs -> Just (reverse rs)