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,
59 opt_LiberateCaseThreshold,
60 opt_StgDoLetNoEscapes,
63 opt_UnboxStrictFields,
64 opt_SimplNoPreInlining,
65 opt_SimplDoEtaReduction,
66 opt_SimplDoLambdaEtaExpansion,
69 opt_SimplPedanticBottoms,
70 opt_SimplExcessPrecision,
73 opt_UF_HiFileThreshold,
74 opt_UF_CreationThreshold,
76 opt_UF_FunAppDiscount,
85 opt_EnsureSplittableC,
90 opt_IgnoreIfacePragmas,
93 opt_OmitInterfacePragmas,
100 #include "HsVersions.h"
102 import Array ( array, (//) )
104 import IOExts ( IORef, readIORef )
105 import Constants -- Default values for some flags
110 import Maybes ( firstJust )
111 import Panic ( panic )
113 #if __GLASGOW_HASKELL__ < 301
114 import ArrBase ( Array(..) )
116 import PrelArr ( Array(..) )
120 %************************************************************************
122 \subsection{Command-line options}
124 %************************************************************************
126 The hsc command-line options are split into two categories:
131 Static flags are represented by top-level values of type Bool or Int,
132 for example. They therefore have the same value throughout the
135 Dynamic flags are represented by an abstract type, DynFlags, which is
136 passed into hsc by the compilation manager for every compilation.
137 Dynamic flags are those that change on a per-compilation basis,
138 perhaps because they may be present in the OPTIONS pragma at the top
141 Other flag-related blurb:
143 A list of {\em ToDo}s is things to be done in a particular part of
144 processing. A (fictitious) example for the Core-to-Core simplifier
145 might be: run the simplifier, then run the strictness analyser, then
146 run the simplifier again (three ``todos'').
148 There are three ``to-do processing centers'' at the moment. In the
149 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
150 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
151 (\tr{simplStg/SimplStg.lhs}).
153 %************************************************************************
155 \subsection{Datatypes associated with command-line options}
157 %************************************************************************
161 = SwBool Bool -- on/off
162 | SwString FAST_STRING -- nothing or a String
163 | SwInt Int -- nothing or an Int
167 data CoreToDo -- These are diff core-to-core passes,
168 -- which may be invoked in any order,
169 -- as many times as you like.
171 = CoreDoSimplify -- The core-to-core simplifier.
172 (SimplifierSwitch -> SwitchResult)
173 -- Each run of the simplifier can take a different
174 -- set of simplifier-specific flags.
176 | CoreDoFloatOutwards Bool -- True <=> float lambdas to top level
181 | CoreDoWorkerWrapper
188 | CoreDoNothing -- useful when building up lists of these things
193 = StgDoMassageForProfiling -- should be (next to) last
194 -- There's also setStgVarInfo, but its absolute "lastness"
195 -- is so critical that it is hardwired in (no flag).
200 data SimplifierSwitch
201 = MaxSimplifierIterations Int
202 | SimplInlinePhase Int
208 %************************************************************************
210 \subsection{Dynamic command-line options}
212 %************************************************************************
225 | Opt_D_dump_inlinings
226 | Opt_D_dump_occur_anal
231 | Opt_D_dump_simpl_iterations
241 | Opt_D_dump_worker_wrapper
242 | Opt_D_dump_rn_trace
243 | Opt_D_dump_rn_stats
245 | 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
288 verbosity :: Int, -- verbosity level
292 defaultDynFlags = DynFlags {
293 coreToDo = [], stgToDo = [],
294 hscLang = HscC, hscOutName = "",
295 verbosity = 0, flags = []
301 0 | print errors & warnings only
302 1 | minimal verbosity: print "compiling M ... done." for each module.
303 2 | equivalent to -dshow-passes
304 3 | equivalent to existing "ghc -v"
305 4 | "ghc -v -ddump-most"
306 5 | "ghc -v -ddump-all"
309 dopt :: DynFlag -> DynFlags -> Bool
310 dopt f dflags = f `elem` (flags dflags)
312 dopt_CoreToDo :: DynFlags -> [CoreToDo]
313 dopt_CoreToDo = coreToDo
315 dopt_StgToDo :: DynFlags -> [StgToDo]
316 dopt_StgToDo = stgToDo
318 dopt_OutName :: DynFlags -> String
319 dopt_OutName = hscOutName
328 dopt_HscLang :: DynFlags -> HscLang
329 dopt_HscLang = hscLang
332 %************************************************************************
334 \subsection{Classifying command-line options}
336 %************************************************************************
339 -- v_Statis_hsc_opts is here to avoid a circular dependency with
341 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
343 lookUp :: FAST_STRING -> Bool
344 lookup_int :: String -> Maybe Int
345 lookup_def_int :: String -> Int -> Int
346 lookup_def_float :: String -> Float -> Float
347 lookup_str :: String -> Maybe String
349 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
350 packed_static_opts = map _PK_ unpacked_static_opts
352 lookUp sw = sw `elem` packed_static_opts
354 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
356 lookup_int sw = case (lookup_str sw) of
358 Just xx -> Just (read xx)
360 lookup_def_int sw def = case (lookup_str sw) of
361 Nothing -> def -- Use default
364 lookup_def_float sw def = case (lookup_str sw) of
365 Nothing -> def -- Use default
370 Putting the compiler options into temporary at-files
371 may turn out to be necessary later on if we turn hsc into
372 a pure Win32 application where I think there's a command-line
373 length limit of 255. unpacked_opts understands the @ option.
375 unpacked_opts :: [String]
379 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
381 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
386 %************************************************************************
388 \subsection{Static options}
390 %************************************************************************
394 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
395 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
396 opt_PprStyle_RawTypes = lookUp SLIT("-dppr-rawtypes")
397 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
400 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
401 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
402 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
403 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
404 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
405 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
408 opt_AllStrict = lookUp SLIT("-fall-strict")
409 opt_NoMonomorphismRestriction = lookUp SLIT("-fno-monomorphism-restriction")
410 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
411 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
412 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
413 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
414 opt_Parallel = lookUp SLIT("-fparallel")
415 opt_SMP = lookUp SLIT("-fsmp")
418 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
419 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
420 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
421 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
422 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
423 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
424 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
427 The optional '-inpackage=P' flag tells what package
428 we are compiling this module for.
429 The Prelude, for example is compiled with '-package prelude'
431 opt_InPackage = case lookup_str "-inpackage=" of
433 Nothing -> SLIT("Main") -- The package name if none is specified
435 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
436 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
437 opt_GranMacros = lookUp SLIT("-fgransim")
438 opt_HiVersion = read cProjectVersionInt :: Int
439 opt_HistorySize = lookup_def_int "-fhistory-size" 20
440 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
441 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
442 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
443 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
444 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
446 -- Simplifier switches
447 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
448 -- NoPreInlining is there just to see how bad things
449 -- get if you don't do it!
450 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
451 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
452 opt_SimplCaseOfCase = lookUp SLIT("-fcase-of-case")
453 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
454 opt_SimplPedanticBottoms = lookUp SLIT("-fpedantic-bottoms")
455 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
458 opt_UF_HiFileThreshold = lookup_def_int "-funfolding-interface-threshold" (45::Int)
459 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
460 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
461 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
462 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
463 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
465 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
466 opt_UF_DearOp = ( 4 :: Int)
468 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
469 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
470 opt_Static = lookUp SLIT("-static")
471 opt_Unregisterised = lookUp SLIT("-funregisterised")
474 %************************************************************************
476 \subsection{List of static hsc flags}
478 %************************************************************************
483 "fauto-sccs-on-all-toplevs",
484 "fauto-sccs-on-exported-toplevs",
485 "fauto-sccs-on-individual-cafs",
486 "fauto-sccs-on-dicts",
491 "firrefutable-tuples",
498 "funfold-casms-in-hi-file",
500 "funbox-strict-fields",
501 "femit-extern-decls",
502 "fglobalise-toplev-names",
505 "fignore-interface-pragmas",
506 "fno-hi-version-check",
508 "fomit-interface-pragmas",
511 "fdo-lambda-eta-expansion",
516 "funfolding-update-in-place",
523 || any (flip prefixMatch f) [
525 "fliberate-case-threshold",
527 "funfolding-interface-threshold",
528 "funfolding-creation-threshold",
529 "funfolding-use-threshold",
530 "funfolding-fun-discount",
531 "funfolding-keeness-factor"
535 %************************************************************************
537 \subsection{Switch ordering}
539 %************************************************************************
541 These things behave just like enumeration types.
544 instance Eq SimplifierSwitch where
545 a == b = tagOf_SimplSwitch a ==# tagOf_SimplSwitch b
547 instance Ord SimplifierSwitch where
548 a < b = tagOf_SimplSwitch a <# tagOf_SimplSwitch b
549 a <= b = tagOf_SimplSwitch a <=# tagOf_SimplSwitch b
552 tagOf_SimplSwitch (SimplInlinePhase _) = _ILIT(1)
553 tagOf_SimplSwitch (MaxSimplifierIterations _) = _ILIT(2)
554 tagOf_SimplSwitch DontApplyRules = _ILIT(3)
555 tagOf_SimplSwitch SimplLetToCase = _ILIT(4)
556 tagOf_SimplSwitch NoCaseOfCase = _ILIT(5)
558 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
560 lAST_SIMPL_SWITCH_TAG = 5
563 %************************************************************************
565 \subsection{Switch lookup}
567 %************************************************************************
570 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
571 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
572 -- in the list; defaults right at the end.
574 tidied_on_switches = foldl rm_dups [] on_switches
575 -- The fold*l* ensures that we keep the latest switches;
576 -- ie the ones that occur earliest in the list.
578 sw_tbl :: Array Int SwitchResult
579 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
583 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
585 defined_elems = map mk_assoc_elem tidied_on_switches
587 -- (avoid some unboxing, bounds checking, and other horrible things:)
588 #if __GLASGOW_HASKELL__ < 405
589 case sw_tbl of { Array bounds_who_needs_'em stuff ->
591 case sw_tbl of { Array _ _ stuff ->
594 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
595 #if __GLASGOW_HASKELL__ < 400
597 #elif __GLASGOW_HASKELL__ < 403
604 mk_assoc_elem k@(MaxSimplifierIterations lvl)
605 = (iBox (tagOf_SimplSwitch k), SwInt lvl)
606 mk_assoc_elem k@(SimplInlinePhase n)
607 = (iBox (tagOf_SimplSwitch k), SwInt n)
609 = (iBox (tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
611 -- cannot have duplicates if we are going to use the array thing
612 rm_dups switches_so_far switch
613 = if switch `is_elem` switches_so_far
615 else switch : switches_so_far
617 sw `is_elem` [] = False
618 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) ==# (tagOf_SimplSwitch s)
623 %************************************************************************
625 \subsection{Misc functions for command-line options}
627 %************************************************************************
631 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
633 switchIsOn lookup_fn switch
634 = case (lookup_fn switch) of
635 SwBool False -> False
638 intSwitchSet :: (switch -> SwitchResult)
642 intSwitchSet lookup_fn switch
643 = case (lookup_fn (switch (panic "intSwitchSet"))) of
644 SwInt int -> Just int
649 startsWith :: String -> String -> Maybe String
650 -- startsWith pfx (pfx++rest) = Just rest
652 startsWith [] str = Just str
653 startsWith (c:cs) (s:ss)
654 = if c /= s then Nothing else startsWith cs ss
655 startsWith _ [] = Nothing
657 endsWith :: String -> String -> Maybe String
659 = case (startsWith (reverse cs) (reverse ss)) of
661 Just rs -> Just (reverse rs)