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
39 opt_AutoSccsOnAllToplevs,
40 opt_AutoSccsOnExportedToplevs,
41 opt_AutoSccsOnIndividualCafs,
49 opt_MaxContextReductionDepth,
50 opt_IrrefutableTuples,
54 opt_NoMonomorphismRestriction,
60 opt_LiberateCaseThreshold,
61 opt_StgDoLetNoEscapes,
64 opt_UnboxStrictFields,
65 opt_SimplNoPreInlining,
66 opt_SimplDoEtaReduction,
67 opt_SimplDoLambdaEtaExpansion,
70 opt_SimplPedanticBottoms,
71 opt_SimplExcessPrecision,
74 opt_UF_HiFileThreshold,
75 opt_UF_CreationThreshold,
77 opt_UF_FunAppDiscount,
86 opt_EnsureSplittableC,
91 opt_IgnoreIfacePragmas,
94 opt_OmitInterfacePragmas,
101 #include "HsVersions.h"
103 import Array ( array, (//) )
105 import IOExts ( IORef, readIORef )
106 import Constants -- Default values for some flags
111 import Maybes ( firstJust )
112 import Panic ( panic )
114 #if __GLASGOW_HASKELL__ < 301
115 import ArrBase ( Array(..) )
117 import PrelArr ( Array(..) )
121 %************************************************************************
123 \subsection{Command-line options}
125 %************************************************************************
127 The hsc command-line options are split into two categories:
132 Static flags are represented by top-level values of type Bool or Int,
133 for example. They therefore have the same value throughout the
136 Dynamic flags are represented by an abstract type, DynFlags, which is
137 passed into hsc by the compilation manager for every compilation.
138 Dynamic flags are those that change on a per-compilation basis,
139 perhaps because they may be present in the OPTIONS pragma at the top
142 Other flag-related blurb:
144 A list of {\em ToDo}s is things to be done in a particular part of
145 processing. A (fictitious) example for the Core-to-Core simplifier
146 might be: run the simplifier, then run the strictness analyser, then
147 run the simplifier again (three ``todos'').
149 There are three ``to-do processing centers'' at the moment. In the
150 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
151 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
152 (\tr{simplStg/SimplStg.lhs}).
154 %************************************************************************
156 \subsection{Datatypes associated with command-line options}
158 %************************************************************************
162 = SwBool Bool -- on/off
163 | SwString FAST_STRING -- nothing or a String
164 | SwInt Int -- nothing or an Int
168 data CoreToDo -- These are diff core-to-core passes,
169 -- which may be invoked in any order,
170 -- as many times as you like.
172 = CoreDoSimplify -- The core-to-core simplifier.
173 (SimplifierSwitch -> SwitchResult)
174 -- Each run of the simplifier can take a different
175 -- set of simplifier-specific flags.
177 | CoreDoFloatOutwards Bool -- True <=> float lambdas to top level
182 | CoreDoWorkerWrapper
189 | CoreDoNothing -- useful when building up lists of these things
194 = StgDoMassageForProfiling -- should be (next to) last
195 -- There's also setStgVarInfo, but its absolute "lastness"
196 -- is so critical that it is hardwired in (no flag).
201 data SimplifierSwitch
202 = MaxSimplifierIterations Int
203 | SimplInlinePhase Int
209 %************************************************************************
211 \subsection{Dynamic command-line options}
213 %************************************************************************
226 | Opt_D_dump_inlinings
227 | Opt_D_dump_occur_anal
232 | Opt_D_dump_simpl_iterations
242 | Opt_D_dump_worker_wrapper
243 | Opt_D_dump_rn_trace
244 | Opt_D_dump_rn_stats
246 | Opt_D_dump_simpl_stats
249 | Opt_D_verbose_core2core
250 | Opt_D_verbose_stg2stg
252 | Opt_D_dump_hi_diffs
253 | Opt_D_dump_minimal_imports
258 | Opt_WarnDuplicateExports
260 | Opt_WarnIncompletePatterns
261 | Opt_WarnMissingFields
262 | Opt_WarnMissingMethods
263 | Opt_WarnMissingSigs
264 | Opt_WarnNameShadowing
265 | Opt_WarnOverlappingPatterns
266 | Opt_WarnSimplePatterns
267 | Opt_WarnTypeDefaults
268 | Opt_WarnUnusedBinds
269 | Opt_WarnUnusedImports
270 | Opt_WarnUnusedMatches
271 | Opt_WarnDeprecations
274 | Opt_AllowOverlappingInstances
275 | Opt_AllowUndecidableInstances
278 | Opt_NoImplicitPrelude
284 data DynFlags = DynFlags {
285 coreToDo :: [CoreToDo],
286 stgToDo :: [StgToDo],
288 hscOutName :: String, -- name of the output file
289 verbosity :: Int, -- verbosity level
290 cppFlag :: Bool, -- preprocess with cpp?
291 stolen_x86_regs :: Int,
292 cmdlineHcIncludes :: [String], -- -#includes
294 -- options for particular phases
305 defaultDynFlags = DynFlags {
306 coreToDo = [], stgToDo = [],
312 cmdlineHcIncludes = [],
324 0 | print errors & warnings only
325 1 | minimal verbosity: print "compiling M ... done." for each module.
326 2 | equivalent to -dshow-passes
327 3 | equivalent to existing "ghc -v"
328 4 | "ghc -v -ddump-most"
329 5 | "ghc -v -ddump-all"
332 dopt :: DynFlag -> DynFlags -> Bool
333 dopt f dflags = f `elem` (flags dflags)
335 dopt_CoreToDo :: DynFlags -> [CoreToDo]
336 dopt_CoreToDo = coreToDo
338 dopt_StgToDo :: DynFlags -> [StgToDo]
339 dopt_StgToDo = stgToDo
341 dopt_OutName :: DynFlags -> String
342 dopt_OutName = hscOutName
351 dopt_HscLang :: DynFlags -> HscLang
352 dopt_HscLang = hscLang
355 %************************************************************************
357 \subsection{Classifying command-line options}
359 %************************************************************************
362 -- v_Statis_hsc_opts is here to avoid a circular dependency with
364 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
366 lookUp :: FAST_STRING -> Bool
367 lookup_int :: String -> Maybe Int
368 lookup_def_int :: String -> Int -> Int
369 lookup_def_float :: String -> Float -> Float
370 lookup_str :: String -> Maybe String
372 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
373 packed_static_opts = map _PK_ unpacked_static_opts
375 lookUp sw = sw `elem` packed_static_opts
377 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
379 lookup_int sw = case (lookup_str sw) of
381 Just xx -> Just (read xx)
383 lookup_def_int sw def = case (lookup_str sw) of
384 Nothing -> def -- Use default
387 lookup_def_float sw def = case (lookup_str sw) of
388 Nothing -> def -- Use default
393 Putting the compiler options into temporary at-files
394 may turn out to be necessary later on if we turn hsc into
395 a pure Win32 application where I think there's a command-line
396 length limit of 255. unpacked_opts understands the @ option.
398 unpacked_opts :: [String]
402 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
404 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
409 %************************************************************************
411 \subsection{Static options}
413 %************************************************************************
417 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
418 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
419 opt_PprStyle_RawTypes = lookUp SLIT("-dppr-rawtypes")
420 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
423 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
424 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
425 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
426 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
427 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
428 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
431 opt_AllStrict = lookUp SLIT("-fall-strict")
432 opt_NoMonomorphismRestriction = lookUp SLIT("-fno-monomorphism-restriction")
433 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
434 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
435 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
436 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
437 opt_Parallel = lookUp SLIT("-fparallel")
438 opt_SMP = lookUp SLIT("-fsmp")
441 opt_NoMethodSharing = lookUp SLIT("-fno-method-sharing")
442 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
443 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
444 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
445 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
446 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
447 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
448 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
451 The optional '-inpackage=P' flag tells what package
452 we are compiling this module for.
453 The Prelude, for example is compiled with '-package prelude'
455 opt_InPackage = case lookup_str "-inpackage=" of
457 Nothing -> SLIT("Main") -- The package name if none is specified
459 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
460 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
461 opt_GranMacros = lookUp SLIT("-fgransim")
462 opt_HiVersion = read cProjectVersionInt :: Int
463 opt_HistorySize = lookup_def_int "-fhistory-size" 20
464 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
465 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
466 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
467 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
468 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
470 -- Simplifier switches
471 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
472 -- NoPreInlining is there just to see how bad things
473 -- get if you don't do it!
474 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
475 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
476 opt_SimplCaseOfCase = lookUp SLIT("-fcase-of-case")
477 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
478 opt_SimplPedanticBottoms = lookUp SLIT("-fpedantic-bottoms")
479 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
482 opt_UF_HiFileThreshold = lookup_def_int "-funfolding-interface-threshold" (45::Int)
483 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
484 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
485 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
486 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
487 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
489 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
490 opt_UF_DearOp = ( 4 :: Int)
492 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
493 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
494 opt_Static = lookUp SLIT("-static")
495 opt_Unregisterised = lookUp SLIT("-funregisterised")
498 %************************************************************************
500 \subsection{List of static hsc flags}
502 %************************************************************************
507 "fauto-sccs-on-all-toplevs",
508 "fauto-sccs-on-exported-toplevs",
509 "fauto-sccs-on-individual-cafs",
510 "fauto-sccs-on-dicts",
515 "firrefutable-tuples",
522 "funfold-casms-in-hi-file",
524 "funbox-strict-fields",
525 "femit-extern-decls",
526 "fglobalise-toplev-names",
529 "fignore-interface-pragmas",
530 "fno-hi-version-check",
532 "fno-method-sharing",
533 "fno-monomorphism-restriction",
534 "fomit-interface-pragmas",
537 "fdo-lambda-eta-expansion",
542 "funfolding-update-in-place",
549 || any (flip prefixMatch f) [
551 "fliberate-case-threshold",
553 "funfolding-interface-threshold",
554 "funfolding-creation-threshold",
555 "funfolding-use-threshold",
556 "funfolding-fun-discount",
557 "funfolding-keeness-factor"
561 %************************************************************************
563 \subsection{Switch ordering}
565 %************************************************************************
567 These things behave just like enumeration types.
570 instance Eq SimplifierSwitch where
571 a == b = tagOf_SimplSwitch a ==# tagOf_SimplSwitch b
573 instance Ord SimplifierSwitch where
574 a < b = tagOf_SimplSwitch a <# tagOf_SimplSwitch b
575 a <= b = tagOf_SimplSwitch a <=# tagOf_SimplSwitch b
578 tagOf_SimplSwitch (SimplInlinePhase _) = _ILIT(1)
579 tagOf_SimplSwitch (MaxSimplifierIterations _) = _ILIT(2)
580 tagOf_SimplSwitch DontApplyRules = _ILIT(3)
581 tagOf_SimplSwitch SimplLetToCase = _ILIT(4)
582 tagOf_SimplSwitch NoCaseOfCase = _ILIT(5)
584 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
586 lAST_SIMPL_SWITCH_TAG = 5
589 %************************************************************************
591 \subsection{Switch lookup}
593 %************************************************************************
596 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
597 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
598 -- in the list; defaults right at the end.
600 tidied_on_switches = foldl rm_dups [] on_switches
601 -- The fold*l* ensures that we keep the latest switches;
602 -- ie the ones that occur earliest in the list.
604 sw_tbl :: Array Int SwitchResult
605 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
609 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
611 defined_elems = map mk_assoc_elem tidied_on_switches
613 -- (avoid some unboxing, bounds checking, and other horrible things:)
614 #if __GLASGOW_HASKELL__ < 405
615 case sw_tbl of { Array bounds_who_needs_'em stuff ->
617 case sw_tbl of { Array _ _ stuff ->
620 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
621 #if __GLASGOW_HASKELL__ < 400
623 #elif __GLASGOW_HASKELL__ < 403
630 mk_assoc_elem k@(MaxSimplifierIterations lvl)
631 = (iBox (tagOf_SimplSwitch k), SwInt lvl)
632 mk_assoc_elem k@(SimplInlinePhase n)
633 = (iBox (tagOf_SimplSwitch k), SwInt n)
635 = (iBox (tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
637 -- cannot have duplicates if we are going to use the array thing
638 rm_dups switches_so_far switch
639 = if switch `is_elem` switches_so_far
641 else switch : switches_so_far
643 sw `is_elem` [] = False
644 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) ==# (tagOf_SimplSwitch s)
649 %************************************************************************
651 \subsection{Misc functions for command-line options}
653 %************************************************************************
657 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
659 switchIsOn lookup_fn switch
660 = case (lookup_fn switch) of
661 SwBool False -> False
664 intSwitchSet :: (switch -> SwitchResult)
668 intSwitchSet lookup_fn switch
669 = case (lookup_fn (switch (panic "intSwitchSet"))) of
670 SwInt int -> Just int
675 startsWith :: String -> String -> Maybe String
676 -- startsWith pfx (pfx++rest) = Just rest
678 startsWith [] str = Just str
679 startsWith (c:cs) (s:ss)
680 = if c /= s then Nothing else startsWith cs ss
681 startsWith _ [] = Nothing
683 endsWith :: String -> String -> Maybe String
685 = case (startsWith (reverse cs) (reverse ss)) of
687 Just rs -> Just (reverse rs)