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
193 | CoreDoNothing -- useful when building up lists of these things
198 = StgDoMassageForProfiling -- should be (next to) last
199 -- There's also setStgVarInfo, but its absolute "lastness"
200 -- is so critical that it is hardwired in (no flag).
205 data SimplifierSwitch
206 = MaxSimplifierIterations Int
207 | SimplInlinePhase Int
213 %************************************************************************
215 \subsection{Dynamic command-line options}
217 %************************************************************************
230 | Opt_D_dump_inlinings
231 | Opt_D_dump_occur_anal
236 | Opt_D_dump_simpl_iterations
246 | Opt_D_dump_worker_wrapper
247 | Opt_D_dump_rn_trace
248 | Opt_D_dump_rn_stats
250 | Opt_D_dump_simpl_stats
251 | Opt_D_dump_tc_trace
254 | Opt_D_verbose_core2core
255 | Opt_D_verbose_stg2stg
257 | Opt_D_dump_hi_diffs
258 | Opt_D_dump_minimal_imports
263 | Opt_WarnDuplicateExports
265 | Opt_WarnIncompletePatterns
266 | Opt_WarnMissingFields
267 | Opt_WarnMissingMethods
268 | Opt_WarnMissingSigs
269 | Opt_WarnNameShadowing
270 | Opt_WarnOverlappingPatterns
271 | Opt_WarnSimplePatterns
272 | Opt_WarnTypeDefaults
273 | Opt_WarnUnusedBinds
274 | Opt_WarnUnusedImports
275 | Opt_WarnUnusedMatches
276 | Opt_WarnDeprecations
279 | Opt_AllowOverlappingInstances
280 | Opt_AllowUndecidableInstances
283 | Opt_NoImplicitPrelude
287 data DynFlags = DynFlags {
288 coreToDo :: [CoreToDo],
289 stgToDo :: [StgToDo],
291 hscOutName :: String, -- name of the output file
292 verbosity :: Int, -- verbosity level
293 cppFlag :: Bool, -- preprocess with cpp?
294 stolen_x86_regs :: Int,
295 cmdlineHcIncludes :: [String], -- -#includes
297 -- options for particular phases
308 defaultDynFlags = DynFlags {
309 coreToDo = [], stgToDo = [],
315 cmdlineHcIncludes = [],
321 flags = standardWarnings,
327 0 | print errors & warnings only
328 1 | minimal verbosity: print "compiling M ... done." for each module.
329 2 | equivalent to -dshow-passes
330 3 | equivalent to existing "ghc -v"
331 4 | "ghc -v -ddump-most"
332 5 | "ghc -v -ddump-all"
335 dopt :: DynFlag -> DynFlags -> Bool
336 dopt f dflags = f `elem` (flags dflags)
338 dopt_CoreToDo :: DynFlags -> [CoreToDo]
339 dopt_CoreToDo = coreToDo
341 dopt_StgToDo :: DynFlags -> [StgToDo]
342 dopt_StgToDo = stgToDo
344 dopt_OutName :: DynFlags -> String
345 dopt_OutName = hscOutName
354 dopt_HscLang :: DynFlags -> HscLang
355 dopt_HscLang = hscLang
358 %************************************************************************
360 \subsection{Warnings}
362 %************************************************************************
366 = [ Opt_WarnDeprecations,
367 Opt_WarnOverlappingPatterns,
368 Opt_WarnMissingFields,
369 Opt_WarnMissingMethods,
370 Opt_WarnDuplicateExports
374 = standardWarnings ++
375 [ Opt_WarnUnusedBinds,
376 Opt_WarnUnusedMatches,
377 Opt_WarnUnusedImports,
378 Opt_WarnIncompletePatterns
383 [ Opt_WarnTypeDefaults,
384 Opt_WarnNameShadowing,
390 %************************************************************************
392 \subsection{Classifying command-line options}
394 %************************************************************************
397 -- v_Statis_hsc_opts is here to avoid a circular dependency with
399 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
401 lookUp :: FAST_STRING -> Bool
402 lookup_int :: String -> Maybe Int
403 lookup_def_int :: String -> Int -> Int
404 lookup_def_float :: String -> Float -> Float
405 lookup_str :: String -> Maybe String
407 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
408 packed_static_opts = map _PK_ unpacked_static_opts
410 lookUp sw = sw `elem` packed_static_opts
412 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
414 lookup_int sw = case (lookup_str sw) of
416 Just xx -> Just (read xx)
418 lookup_def_int sw def = case (lookup_str sw) of
419 Nothing -> def -- Use default
422 lookup_def_float sw def = case (lookup_str sw) of
423 Nothing -> def -- Use default
428 Putting the compiler options into temporary at-files
429 may turn out to be necessary later on if we turn hsc into
430 a pure Win32 application where I think there's a command-line
431 length limit of 255. unpacked_opts understands the @ option.
433 unpacked_opts :: [String]
437 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
439 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
444 %************************************************************************
446 \subsection{Static options}
448 %************************************************************************
452 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
453 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
454 opt_PprStyle_RawTypes = lookUp SLIT("-dppr-rawtypes")
455 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
458 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
459 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
460 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
461 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
462 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
463 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
466 opt_AllStrict = lookUp SLIT("-fall-strict")
467 opt_NoMonomorphismRestriction = lookUp SLIT("-fno-monomorphism-restriction")
468 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
469 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
470 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
471 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
472 opt_Parallel = lookUp SLIT("-fparallel")
473 opt_SMP = lookUp SLIT("-fsmp")
476 opt_NoMethodSharing = lookUp SLIT("-fno-method-sharing")
477 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
478 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
479 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
480 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
481 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
482 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
483 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
486 The optional '-inpackage=P' flag tells what package
487 we are compiling this module for.
488 The Prelude, for example is compiled with '-inpackage prelude'
490 opt_InPackage = case lookup_str "-inpackage=" of
492 Nothing -> SLIT("Main") -- The package name if none is specified
494 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
495 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
496 opt_GranMacros = lookUp SLIT("-fgransim")
497 opt_HiVersion = read cProjectVersionInt :: Int
498 opt_HistorySize = lookup_def_int "-fhistory-size" 20
499 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
500 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
501 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
502 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
503 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
504 opt_KeepStgTypes = lookUp SLIT("-fkeep-stg-types")
506 -- Simplifier switches
507 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
508 -- NoPreInlining is there just to see how bad things
509 -- get if you don't do it!
510 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
511 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
512 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
513 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
516 opt_UF_HiFileThreshold = lookup_def_int "-funfolding-interface-threshold" (45::Int)
517 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
518 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
519 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
520 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
521 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
523 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
524 opt_UF_DearOp = ( 4 :: Int)
526 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
527 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
528 opt_Static = lookUp SLIT("-static")
529 opt_Unregisterised = lookUp SLIT("-funregisterised")
532 %************************************************************************
534 \subsection{List of static hsc flags}
536 %************************************************************************
541 "fauto-sccs-on-all-toplevs",
542 "fauto-sccs-on-exported-toplevs",
543 "fauto-sccs-on-individual-cafs",
544 "fauto-sccs-on-dicts",
549 "firrefutable-tuples",
556 "funfold-casms-in-hi-file",
558 "funbox-strict-fields",
559 "femit-extern-decls",
560 "fglobalise-toplev-names",
563 "fignore-interface-pragmas",
564 "fno-hi-version-check",
566 "fno-method-sharing",
567 "fno-monomorphism-restriction",
568 "fomit-interface-pragmas",
572 "fdo-lambda-eta-expansion",
575 "funfolding-update-in-place",
581 || any (flip prefixMatch f) [
583 "fliberate-case-threshold",
585 "funfolding-interface-threshold",
586 "funfolding-creation-threshold",
587 "funfolding-use-threshold",
588 "funfolding-fun-discount",
589 "funfolding-keeness-factor"
593 %************************************************************************
595 \subsection{Switch ordering}
597 %************************************************************************
599 These things behave just like enumeration types.
602 instance Eq SimplifierSwitch where
603 a == b = tagOf_SimplSwitch a ==# tagOf_SimplSwitch b
605 instance Ord SimplifierSwitch where
606 a < b = tagOf_SimplSwitch a <# tagOf_SimplSwitch b
607 a <= b = tagOf_SimplSwitch a <=# tagOf_SimplSwitch b
610 tagOf_SimplSwitch (SimplInlinePhase _) = _ILIT(1)
611 tagOf_SimplSwitch (MaxSimplifierIterations _) = _ILIT(2)
612 tagOf_SimplSwitch DontApplyRules = _ILIT(3)
613 tagOf_SimplSwitch SimplLetToCase = _ILIT(4)
614 tagOf_SimplSwitch NoCaseOfCase = _ILIT(5)
616 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
618 lAST_SIMPL_SWITCH_TAG = 5
621 %************************************************************************
623 \subsection{Switch lookup}
625 %************************************************************************
628 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
629 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
630 -- in the list; defaults right at the end.
632 tidied_on_switches = foldl rm_dups [] on_switches
633 -- The fold*l* ensures that we keep the latest switches;
634 -- ie the ones that occur earliest in the list.
636 sw_tbl :: Array Int SwitchResult
637 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
641 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
643 defined_elems = map mk_assoc_elem tidied_on_switches
645 -- (avoid some unboxing, bounds checking, and other horrible things:)
646 #if __GLASGOW_HASKELL__ < 405
647 case sw_tbl of { Array bounds_who_needs_'em stuff ->
649 case sw_tbl of { Array _ _ stuff ->
652 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
653 #if __GLASGOW_HASKELL__ < 400
655 #elif __GLASGOW_HASKELL__ < 403
662 mk_assoc_elem k@(MaxSimplifierIterations lvl)
663 = (iBox (tagOf_SimplSwitch k), SwInt lvl)
664 mk_assoc_elem k@(SimplInlinePhase n)
665 = (iBox (tagOf_SimplSwitch k), SwInt n)
667 = (iBox (tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
669 -- cannot have duplicates if we are going to use the array thing
670 rm_dups switches_so_far switch
671 = if switch `is_elem` switches_so_far
673 else switch : switches_so_far
675 sw `is_elem` [] = False
676 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) ==# (tagOf_SimplSwitch s)
681 %************************************************************************
683 \subsection{Misc functions for command-line options}
685 %************************************************************************
689 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
691 switchIsOn lookup_fn switch
692 = case (lookup_fn switch) of
693 SwBool False -> False
696 intSwitchSet :: (switch -> SwitchResult)
700 intSwitchSet lookup_fn switch
701 = case (lookup_fn (switch (panic "intSwitchSet"))) of
702 SwInt int -> Just int
707 startsWith :: String -> String -> Maybe String
708 -- startsWith pfx (pfx++rest) = Just rest
710 startsWith [] str = Just str
711 startsWith (c:cs) (s:ss)
712 = if c /= s then Nothing else startsWith cs ss
713 startsWith _ [] = Nothing
715 endsWith :: String -> String -> Maybe String
717 = case (startsWith (reverse cs) (reverse ss)) of
719 Just rs -> Just (reverse rs)