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,
61 opt_LiberateCaseThreshold,
62 opt_StgDoLetNoEscapes,
65 opt_UnboxStrictFields,
66 opt_SimplNoPreInlining,
67 opt_SimplDoEtaReduction,
68 opt_SimplDoLambdaEtaExpansion,
71 opt_SimplPedanticBottoms,
72 opt_SimplExcessPrecision,
75 opt_UF_HiFileThreshold,
76 opt_UF_CreationThreshold,
78 opt_UF_FunAppDiscount,
87 opt_EnsureSplittableC,
92 opt_IgnoreIfacePragmas,
95 opt_OmitInterfacePragmas,
102 #include "HsVersions.h"
104 import Array ( array, (//) )
106 import IOExts ( IORef, readIORef )
107 import Constants -- Default values for some flags
112 import Maybes ( firstJust )
113 import Panic ( panic )
115 #if __GLASGOW_HASKELL__ < 301
116 import ArrBase ( Array(..) )
118 import PrelArr ( Array(..) )
122 %************************************************************************
124 \subsection{Command-line options}
126 %************************************************************************
128 The hsc command-line options are split into two categories:
133 Static flags are represented by top-level values of type Bool or Int,
134 for example. They therefore have the same value throughout the
137 Dynamic flags are represented by an abstract type, DynFlags, which is
138 passed into hsc by the compilation manager for every compilation.
139 Dynamic flags are those that change on a per-compilation basis,
140 perhaps because they may be present in the OPTIONS pragma at the top
143 Other flag-related blurb:
145 A list of {\em ToDo}s is things to be done in a particular part of
146 processing. A (fictitious) example for the Core-to-Core simplifier
147 might be: run the simplifier, then run the strictness analyser, then
148 run the simplifier again (three ``todos'').
150 There are three ``to-do processing centers'' at the moment. In the
151 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
152 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
153 (\tr{simplStg/SimplStg.lhs}).
155 %************************************************************************
157 \subsection{Datatypes associated with command-line options}
159 %************************************************************************
163 = SwBool Bool -- on/off
164 | SwString FAST_STRING -- nothing or a String
165 | SwInt Int -- nothing or an Int
169 data CoreToDo -- These are diff core-to-core passes,
170 -- which may be invoked in any order,
171 -- as many times as you like.
173 = CoreDoSimplify -- The core-to-core simplifier.
174 (SimplifierSwitch -> SwitchResult)
175 -- Each run of the simplifier can take a different
176 -- set of simplifier-specific flags.
178 | CoreDoFloatOutwards Bool -- True <=> float lambdas to top level
183 | CoreDoWorkerWrapper
190 | CoreDoNothing -- useful when building up lists of these things
195 = StgDoMassageForProfiling -- should be (next to) last
196 -- There's also setStgVarInfo, but its absolute "lastness"
197 -- is so critical that it is hardwired in (no flag).
202 data SimplifierSwitch
203 = MaxSimplifierIterations Int
204 | SimplInlinePhase Int
210 %************************************************************************
212 \subsection{Dynamic command-line options}
214 %************************************************************************
227 | Opt_D_dump_inlinings
228 | Opt_D_dump_occur_anal
233 | Opt_D_dump_simpl_iterations
240 | Opt_D_dump_tc_trace
244 | Opt_D_dump_worker_wrapper
245 | Opt_D_dump_rn_trace
246 | Opt_D_dump_rn_stats
248 | Opt_D_dump_simpl_stats
251 | Opt_D_verbose_core2core
252 | Opt_D_verbose_stg2stg
254 | Opt_D_dump_hi_diffs
255 | Opt_D_dump_minimal_imports
260 | Opt_WarnDuplicateExports
262 | Opt_WarnIncompletePatterns
263 | Opt_WarnMissingFields
264 | Opt_WarnMissingMethods
265 | Opt_WarnMissingSigs
266 | Opt_WarnNameShadowing
267 | Opt_WarnOverlappingPatterns
268 | Opt_WarnSimplePatterns
269 | Opt_WarnTypeDefaults
270 | Opt_WarnUnusedBinds
271 | Opt_WarnUnusedImports
272 | Opt_WarnUnusedMatches
273 | Opt_WarnDeprecations
276 | Opt_AllowOverlappingInstances
277 | Opt_AllowUndecidableInstances
280 | Opt_NoImplicitPrelude
286 data DynFlags = DynFlags {
287 coreToDo :: [CoreToDo],
288 stgToDo :: [StgToDo],
290 hscOutName :: String, -- name of the output file
291 verbosity :: Int, -- verbosity level
292 cppFlag :: Bool, -- preprocess with cpp?
293 stolen_x86_regs :: Int,
294 cmdlineHcIncludes :: [String], -- -#includes
296 -- options for particular phases
307 defaultDynFlags = DynFlags {
308 coreToDo = [], stgToDo = [],
314 cmdlineHcIncludes = [],
326 0 | print errors & warnings only
327 1 | minimal verbosity: print "compiling M ... done." for each module.
328 2 | equivalent to -dshow-passes
329 3 | equivalent to existing "ghc -v"
330 4 | "ghc -v -ddump-most"
331 5 | "ghc -v -ddump-all"
334 dopt :: DynFlag -> DynFlags -> Bool
335 dopt f dflags = f `elem` (flags dflags)
337 dopt_CoreToDo :: DynFlags -> [CoreToDo]
338 dopt_CoreToDo = coreToDo
340 dopt_StgToDo :: DynFlags -> [StgToDo]
341 dopt_StgToDo = stgToDo
343 dopt_OutName :: DynFlags -> String
344 dopt_OutName = hscOutName
353 dopt_HscLang :: DynFlags -> HscLang
354 dopt_HscLang = hscLang
357 %************************************************************************
359 \subsection{Classifying command-line options}
361 %************************************************************************
364 -- v_Statis_hsc_opts is here to avoid a circular dependency with
366 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
368 lookUp :: FAST_STRING -> Bool
369 lookup_int :: String -> Maybe Int
370 lookup_def_int :: String -> Int -> Int
371 lookup_def_float :: String -> Float -> Float
372 lookup_str :: String -> Maybe String
374 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
375 packed_static_opts = map _PK_ unpacked_static_opts
377 lookUp sw = sw `elem` packed_static_opts
379 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
381 lookup_int sw = case (lookup_str sw) of
383 Just xx -> Just (read xx)
385 lookup_def_int sw def = case (lookup_str sw) of
386 Nothing -> def -- Use default
389 lookup_def_float sw def = case (lookup_str sw) of
390 Nothing -> def -- Use default
395 Putting the compiler options into temporary at-files
396 may turn out to be necessary later on if we turn hsc into
397 a pure Win32 application where I think there's a command-line
398 length limit of 255. unpacked_opts understands the @ option.
400 unpacked_opts :: [String]
404 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
406 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
411 %************************************************************************
413 \subsection{Static options}
415 %************************************************************************
419 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
420 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
421 opt_PprStyle_RawTypes = lookUp SLIT("-dppr-rawtypes")
422 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
425 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
426 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
427 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
428 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
429 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
430 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
433 opt_AllStrict = lookUp SLIT("-fall-strict")
434 opt_NoMonomorphismRestriction = lookUp SLIT("-fno-monomorphism-restriction")
435 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
436 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
437 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
438 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
439 opt_Parallel = lookUp SLIT("-fparallel")
440 opt_SMP = lookUp SLIT("-fsmp")
443 opt_NoMethodSharing = lookUp SLIT("-fno-method-sharing")
444 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
445 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
446 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
447 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
448 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
449 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
450 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
453 The optional '-inpackage=P' flag tells what package
454 we are compiling this module for.
455 The Prelude, for example is compiled with '-inpackage prelude'
457 opt_InPackage = case lookup_str "-inpackage=" of
459 Nothing -> SLIT("Main") -- The package name if none is specified
461 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
462 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
463 opt_GranMacros = lookUp SLIT("-fgransim")
464 opt_HiVersion = read cProjectVersionInt :: Int
465 opt_HistorySize = lookup_def_int "-fhistory-size" 20
466 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
467 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
468 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
469 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
470 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
471 opt_KeepStgTypes = lookUp SLIT("-fkeep-stg-types")
473 -- Simplifier switches
474 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
475 -- NoPreInlining is there just to see how bad things
476 -- get if you don't do it!
477 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
478 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
479 opt_SimplCaseOfCase = lookUp SLIT("-fcase-of-case")
480 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
481 opt_SimplPedanticBottoms = lookUp SLIT("-fpedantic-bottoms")
482 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
485 opt_UF_HiFileThreshold = lookup_def_int "-funfolding-interface-threshold" (45::Int)
486 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
487 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
488 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
489 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
490 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
492 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
493 opt_UF_DearOp = ( 4 :: Int)
495 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
496 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
497 opt_Static = lookUp SLIT("-static")
498 opt_Unregisterised = lookUp SLIT("-funregisterised")
501 %************************************************************************
503 \subsection{List of static hsc flags}
505 %************************************************************************
510 "fauto-sccs-on-all-toplevs",
511 "fauto-sccs-on-exported-toplevs",
512 "fauto-sccs-on-individual-cafs",
513 "fauto-sccs-on-dicts",
518 "firrefutable-tuples",
525 "funfold-casms-in-hi-file",
527 "funbox-strict-fields",
528 "femit-extern-decls",
529 "fglobalise-toplev-names",
532 "fignore-interface-pragmas",
533 "fno-hi-version-check",
535 "fno-method-sharing",
536 "fno-monomorphism-restriction",
537 "fomit-interface-pragmas",
541 "fdo-lambda-eta-expansion",
546 "funfolding-update-in-place",
553 || any (flip prefixMatch f) [
555 "fliberate-case-threshold",
557 "funfolding-interface-threshold",
558 "funfolding-creation-threshold",
559 "funfolding-use-threshold",
560 "funfolding-fun-discount",
561 "funfolding-keeness-factor"
565 %************************************************************************
567 \subsection{Switch ordering}
569 %************************************************************************
571 These things behave just like enumeration types.
574 instance Eq SimplifierSwitch where
575 a == b = tagOf_SimplSwitch a ==# tagOf_SimplSwitch b
577 instance Ord SimplifierSwitch where
578 a < b = tagOf_SimplSwitch a <# tagOf_SimplSwitch b
579 a <= b = tagOf_SimplSwitch a <=# tagOf_SimplSwitch b
582 tagOf_SimplSwitch (SimplInlinePhase _) = _ILIT(1)
583 tagOf_SimplSwitch (MaxSimplifierIterations _) = _ILIT(2)
584 tagOf_SimplSwitch DontApplyRules = _ILIT(3)
585 tagOf_SimplSwitch SimplLetToCase = _ILIT(4)
586 tagOf_SimplSwitch NoCaseOfCase = _ILIT(5)
588 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
590 lAST_SIMPL_SWITCH_TAG = 5
593 %************************************************************************
595 \subsection{Switch lookup}
597 %************************************************************************
600 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
601 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
602 -- in the list; defaults right at the end.
604 tidied_on_switches = foldl rm_dups [] on_switches
605 -- The fold*l* ensures that we keep the latest switches;
606 -- ie the ones that occur earliest in the list.
608 sw_tbl :: Array Int SwitchResult
609 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
613 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
615 defined_elems = map mk_assoc_elem tidied_on_switches
617 -- (avoid some unboxing, bounds checking, and other horrible things:)
618 #if __GLASGOW_HASKELL__ < 405
619 case sw_tbl of { Array bounds_who_needs_'em stuff ->
621 case sw_tbl of { Array _ _ stuff ->
624 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
625 #if __GLASGOW_HASKELL__ < 400
627 #elif __GLASGOW_HASKELL__ < 403
634 mk_assoc_elem k@(MaxSimplifierIterations lvl)
635 = (iBox (tagOf_SimplSwitch k), SwInt lvl)
636 mk_assoc_elem k@(SimplInlinePhase n)
637 = (iBox (tagOf_SimplSwitch k), SwInt n)
639 = (iBox (tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
641 -- cannot have duplicates if we are going to use the array thing
642 rm_dups switches_so_far switch
643 = if switch `is_elem` switches_so_far
645 else switch : switches_so_far
647 sw `is_elem` [] = False
648 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) ==# (tagOf_SimplSwitch s)
653 %************************************************************************
655 \subsection{Misc functions for command-line options}
657 %************************************************************************
661 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
663 switchIsOn lookup_fn switch
664 = case (lookup_fn switch) of
665 SwBool False -> False
668 intSwitchSet :: (switch -> SwitchResult)
672 intSwitchSet lookup_fn switch
673 = case (lookup_fn (switch (panic "intSwitchSet"))) of
674 SwInt int -> Just int
679 startsWith :: String -> String -> Maybe String
680 -- startsWith pfx (pfx++rest) = Just rest
682 startsWith [] str = Just str
683 startsWith (c:cs) (s:ss)
684 = if c /= s then Nothing else startsWith cs ss
685 startsWith _ [] = Nothing
687 endsWith :: String -> String -> Maybe String
689 = case (startsWith (reverse cs) (reverse ss)) of
691 Just rs -> Just (reverse rs)