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
246 | Opt_D_dump_InterpSyn
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 file in which to place output
289 verbosity :: Int, -- verbosity level
293 defaultDynFlags = DynFlags {
294 coreToDo = [], stgToDo = [],
295 hscLang = HscC, hscOutName = "",
296 verbosity = 0, flags = []
302 0 | print errors & warnings only
303 1 | minimal verbosity: print "compiling M ... done." for each module.
304 2 | equivalent to -dshow-passes
305 3 | equivalent to existing "ghc -v"
306 4 | "ghc -v -ddump-most"
307 5 | "ghc -v -ddump-all"
310 dopt :: DynFlag -> DynFlags -> Bool
311 dopt f dflags = f `elem` (flags dflags)
313 dopt_CoreToDo :: DynFlags -> [CoreToDo]
314 dopt_CoreToDo = coreToDo
316 dopt_StgToDo :: DynFlags -> [StgToDo]
317 dopt_StgToDo = stgToDo
319 dopt_OutName :: DynFlags -> String
320 dopt_OutName = hscOutName
329 dopt_HscLang :: DynFlags -> HscLang
330 dopt_HscLang = hscLang
333 %************************************************************************
335 \subsection{Classifying command-line options}
337 %************************************************************************
340 -- v_Statis_hsc_opts is here to avoid a circular dependency with
342 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
344 lookUp :: FAST_STRING -> Bool
345 lookup_int :: String -> Maybe Int
346 lookup_def_int :: String -> Int -> Int
347 lookup_def_float :: String -> Float -> Float
348 lookup_str :: String -> Maybe String
350 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
351 packed_static_opts = map _PK_ unpacked_static_opts
353 lookUp sw = sw `elem` packed_static_opts
355 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
357 lookup_int sw = case (lookup_str sw) of
359 Just xx -> Just (read xx)
361 lookup_def_int sw def = case (lookup_str sw) of
362 Nothing -> def -- Use default
365 lookup_def_float sw def = case (lookup_str sw) of
366 Nothing -> def -- Use default
371 Putting the compiler options into temporary at-files
372 may turn out to be necessary later on if we turn hsc into
373 a pure Win32 application where I think there's a command-line
374 length limit of 255. unpacked_opts understands the @ option.
376 unpacked_opts :: [String]
380 map _UNPK_ argv -- NOT ARGV any more: v_Static_hsc_opts
382 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
387 %************************************************************************
389 \subsection{Static options}
391 %************************************************************************
395 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
396 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
397 opt_PprStyle_RawTypes = lookUp SLIT("-dppr-rawtypes")
398 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
401 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
402 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
403 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
404 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
405 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
406 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
409 opt_AllStrict = lookUp SLIT("-fall-strict")
410 opt_NoMonomorphismRestriction = lookUp SLIT("-fno-monomorphism-restriction")
411 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
412 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
413 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
414 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
415 opt_Parallel = lookUp SLIT("-fparallel")
416 opt_SMP = lookUp SLIT("-fsmp")
419 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
420 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
421 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
422 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
423 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
424 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
425 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
428 The optional '-inpackage=P' flag tells what package
429 we are compiling this module for.
430 The Prelude, for example is compiled with '-package prelude'
432 opt_InPackage = case lookup_str "-inpackage=" of
434 Nothing -> SLIT("Main") -- The package name if none is specified
436 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
437 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
438 opt_GranMacros = lookUp SLIT("-fgransim")
439 opt_HiVersion = read cProjectVersionInt :: Int
440 opt_HistorySize = lookup_def_int "-fhistory-size" 20
441 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
442 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
443 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
444 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
445 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
447 -- Simplifier switches
448 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
449 -- NoPreInlining is there just to see how bad things
450 -- get if you don't do it!
451 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
452 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
453 opt_SimplCaseOfCase = lookUp SLIT("-fcase-of-case")
454 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
455 opt_SimplPedanticBottoms = lookUp SLIT("-fpedantic-bottoms")
456 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
459 opt_UF_HiFileThreshold = lookup_def_int "-funfolding-interface-threshold" (45::Int)
460 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
461 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
462 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
463 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
464 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
466 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
467 opt_UF_DearOp = ( 4 :: Int)
469 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
470 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
471 opt_Static = lookUp SLIT("-static")
472 opt_Unregisterised = lookUp SLIT("-funregisterised")
475 %************************************************************************
477 \subsection{List of static hsc flags}
479 %************************************************************************
484 "fauto-sccs-on-all-toplevs",
485 "fauto-sccs-on-exported-toplevs",
486 "fauto-sccs-on-individual-cafs",
487 "fauto-sccs-on-dicts",
492 "firrefutable-tuples",
499 "funfold-casms-in-hi-file",
501 "funbox-strict-fields",
502 "femit-extern-decls",
503 "fglobalise-toplev-names",
506 "fignore-interface-pragmas",
507 "fno-hi-version-check",
509 "fomit-interface-pragmas",
512 "fdo-lambda-eta-expansion",
517 "funfolding-update-in-place",
524 || any (flip prefixMatch f) [
526 "fliberate-case-threshold",
528 "funfolding-interface-threshold",
529 "funfolding-creation-threshold",
530 "funfolding-use-threshold",
531 "funfolding-fun-discount",
532 "funfolding-keeness-factor"
536 %************************************************************************
538 \subsection{Switch ordering}
540 %************************************************************************
542 These things behave just like enumeration types.
545 instance Eq SimplifierSwitch where
546 a == b = tagOf_SimplSwitch a ==# tagOf_SimplSwitch b
548 instance Ord SimplifierSwitch where
549 a < b = tagOf_SimplSwitch a <# tagOf_SimplSwitch b
550 a <= b = tagOf_SimplSwitch a <=# tagOf_SimplSwitch b
553 tagOf_SimplSwitch (SimplInlinePhase _) = _ILIT(1)
554 tagOf_SimplSwitch (MaxSimplifierIterations _) = _ILIT(2)
555 tagOf_SimplSwitch DontApplyRules = _ILIT(3)
556 tagOf_SimplSwitch SimplLetToCase = _ILIT(4)
557 tagOf_SimplSwitch NoCaseOfCase = _ILIT(5)
559 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
561 lAST_SIMPL_SWITCH_TAG = 5
564 %************************************************************************
566 \subsection{Switch lookup}
568 %************************************************************************
571 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
572 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
573 -- in the list; defaults right at the end.
575 tidied_on_switches = foldl rm_dups [] on_switches
576 -- The fold*l* ensures that we keep the latest switches;
577 -- ie the ones that occur earliest in the list.
579 sw_tbl :: Array Int SwitchResult
580 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
584 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
586 defined_elems = map mk_assoc_elem tidied_on_switches
588 -- (avoid some unboxing, bounds checking, and other horrible things:)
589 #if __GLASGOW_HASKELL__ < 405
590 case sw_tbl of { Array bounds_who_needs_'em stuff ->
592 case sw_tbl of { Array _ _ stuff ->
595 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
596 #if __GLASGOW_HASKELL__ < 400
598 #elif __GLASGOW_HASKELL__ < 403
605 mk_assoc_elem k@(MaxSimplifierIterations lvl)
606 = (iBox (tagOf_SimplSwitch k), SwInt lvl)
607 mk_assoc_elem k@(SimplInlinePhase n)
608 = (iBox (tagOf_SimplSwitch k), SwInt n)
610 = (iBox (tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
612 -- cannot have duplicates if we are going to use the array thing
613 rm_dups switches_so_far switch
614 = if switch `is_elem` switches_so_far
616 else switch : switches_so_far
618 sw `is_elem` [] = False
619 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) ==# (tagOf_SimplSwitch s)
624 %************************************************************************
626 \subsection{Misc functions for command-line options}
628 %************************************************************************
632 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
634 switchIsOn lookup_fn switch
635 = case (lookup_fn switch) of
636 SwBool False -> False
639 intSwitchSet :: (switch -> SwitchResult)
643 intSwitchSet lookup_fn switch
644 = case (lookup_fn (switch (panic "intSwitchSet"))) of
645 SwInt int -> Just int
650 startsWith :: String -> String -> Maybe String
651 -- startsWith pfx (pfx++rest) = Just rest
653 startsWith [] str = Just str
654 startsWith (c:cs) (s:ss)
655 = if c /= s then Nothing else startsWith cs ss
656 startsWith _ [] = Nothing
658 endsWith :: String -> String -> Maybe String
660 = case (startsWith (reverse cs) (reverse ss)) of
662 Just rs -> Just (reverse rs)