2 % (c) The University of Glasgow, 1996-2000
4 \section[CmdLineOpts]{Things to do with command-line options}
10 SimplifierSwitch(..), isAmongSimpl,
14 DynFlag(..), -- needed non-abstractly by DriverFlags
27 -- other dynamic flags
33 opt_AutoSccsOnAllToplevs,
34 opt_AutoSccsOnExportedToplevs,
35 opt_AutoSccsOnIndividualCafs,
43 opt_MaxContextReductionDepth,
44 opt_IrrefutableTuples,
52 opt_LiberateCaseThreshold,
53 opt_StgDoLetNoEscapes,
56 opt_UnboxStrictFields,
57 opt_SimplNoPreInlining,
58 opt_SimplDoEtaReduction,
59 opt_SimplDoLambdaEtaExpansion,
62 opt_SimplPedanticBottoms,
63 opt_SimplExcessPrecision,
66 opt_UF_HiFileThreshold,
67 opt_UF_CreationThreshold,
69 opt_UF_FunAppDiscount,
78 opt_EnsureSplittableC,
83 opt_IgnoreIfacePragmas,
85 opt_NoImplicitPrelude,
87 opt_OmitInterfacePragmas,
95 #include "HsVersions.h"
97 import Array ( array, (//) )
100 import Constants -- Default values for some flags
104 import Maybes ( firstJust )
105 import Panic ( panic )
107 #if __GLASGOW_HASKELL__ < 301
108 import ArrBase ( Array(..) )
110 import PrelArr ( Array(..) )
114 %************************************************************************
116 \subsection{Command-line options}
118 %************************************************************************
120 The hsc command-line options are split into two categories:
125 Static flags are represented by top-level values of type Bool or Int,
126 for example. They therefore have the same value throughout the
129 Dynamic flags are represented by an abstract type, DynFlags, which is
130 passed into hsc by the compilation manager for every compilation.
131 Dynamic flags are those that change on a per-compilation basis,
132 perhaps because they may be present in the OPTIONS pragma at the top
135 Other flag-related blurb:
137 A list of {\em ToDo}s is things to be done in a particular part of
138 processing. A (fictitious) example for the Core-to-Core simplifier
139 might be: run the simplifier, then run the strictness analyser, then
140 run the simplifier again (three ``todos'').
142 There are three ``to-do processing centers'' at the moment. In the
143 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
144 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
145 (\tr{simplStg/SimplStg.lhs}).
147 %************************************************************************
149 \subsection{Datatypes associated with command-line options}
151 %************************************************************************
155 = SwBool Bool -- on/off
156 | SwString FAST_STRING -- nothing or a String
157 | SwInt Int -- nothing or an Int
161 data CoreToDo -- These are diff core-to-core passes,
162 -- which may be invoked in any order,
163 -- as many times as you like.
165 = CoreDoSimplify -- The core-to-core simplifier.
166 (SimplifierSwitch -> SwitchResult)
167 -- Each run of the simplifier can take a different
168 -- set of simplifier-specific flags.
170 | CoreDoFloatOutwards Bool -- True <=> float lambdas to top level
175 | CoreDoWorkerWrapper
182 | CoreDoNothing -- useful when building up lists of these things
189 | StgDoMassageForProfiling -- should be (next to) last
190 -- There's also setStgVarInfo, but its absolute "lastness"
191 -- is so critical that it is hardwired in (no flag).
196 data SimplifierSwitch
197 = MaxSimplifierIterations Int
198 | SimplInlinePhase Int
204 %************************************************************************
206 \subsection{Dynamic command-line options}
208 %************************************************************************
223 | Opt_D_dump_inlinings
224 | Opt_D_dump_occur_anal
229 | Opt_D_dump_simpl_iterations
238 | Opt_D_dump_worker_wrapper
240 | Opt_D_dump_rn_trace
241 | Opt_D_dump_rn_stats
243 | Opt_D_dump_simpl_stats
245 | Opt_D_verbose_core2core
246 | Opt_D_verbose_stg2stg
247 | Opt_D_dump_hi_diffs
248 | Opt_D_dump_minimal_imports
253 | Opt_WarnDuplicateExports
255 | Opt_WarnIncompletePatterns
256 | Opt_WarnMissingFields
257 | Opt_WarnMissingMethods
258 | Opt_WarnMissingSigs
259 | Opt_WarnNameShadowing
260 | Opt_WarnOverlappingPatterns
261 | Opt_WarnSimplePatterns
262 | Opt_WarnTypeDefaults
263 | Opt_WarnUnusedBinds
264 | Opt_WarnUnusedImports
265 | Opt_WarnUnusedMatches
266 | Opt_WarnDeprecations
269 | Opt_AllowOverlappingInstances
270 | Opt_AllowUndecidableInstances
278 data DynFlags = DynFlags {
279 coreToDo :: CoreToDo,
285 dopt :: DynFlag -> DynFlags -> Bool
286 dopt f dflags = f `elem` (flags dflags)
288 dopt_CoreToDo :: DynFlags -> CoreToDo
289 dopt_CoreToDo = coreToDo
291 dopt_StgToDo :: DynFlags -> StgToDo
292 dopt_StgToDo = stgToDo
295 = HscC String -- String is the filename to put output into
296 | HscAsm String -- ditto
297 | HscJava String -- ditto
300 dopt_HscLang :: DynFlags -> HscLang
301 dopt_HscLang = hscLang
304 %************************************************************************
306 \subsection{Classifying command-line options}
308 %************************************************************************
311 lookUp :: FAST_STRING -> Bool
312 lookup_int :: String -> Maybe Int
313 lookup_def_int :: String -> Int -> Int
314 lookup_def_float :: String -> Float -> Float
315 lookup_str :: String -> Maybe String
317 lookUp sw = sw `elem` argv
319 lookup_str sw = firstJust (map (startsWith sw) unpacked_opts)
321 lookup_int sw = case (lookup_str sw) of
323 Just xx -> Just (read xx)
325 lookup_def_int sw def = case (lookup_str sw) of
326 Nothing -> def -- Use default
329 lookup_def_char sw def = case (lookup_str sw) of
331 _ -> def -- Use default
333 lookup_def_float sw def = case (lookup_str sw) of
334 Nothing -> def -- Use default
337 unpacked_opts = map _UNPK_ argv
340 Putting the compiler options into temporary at-files
341 may turn out to be necessary later on if we turn hsc into
342 a pure Win32 application where I think there's a command-line
343 length limit of 255. unpacked_opts understands the @ option.
345 unpacked_opts :: [String]
351 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
356 %************************************************************************
358 \subsection{Static options}
360 %************************************************************************
364 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
365 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
366 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
369 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
370 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
371 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
372 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
373 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
374 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
377 opt_AllStrict = lookUp SLIT("-fall-strict")
378 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
379 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
380 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
381 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
382 opt_Parallel = lookUp SLIT("-fparallel")
383 opt_SMP = lookUp SLIT("-fsmp")
386 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
387 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
388 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
389 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
390 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
391 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
392 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
395 The optional '-inpackage=P' flag tells what package
396 we are compiling this module for.
397 The Prelude, for example is compiled with '-package prelude'
399 opt_InPackage = case lookup_str "-inpackage=" of
401 Nothing -> SLIT("Main") -- The package name if none is specified
403 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
404 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
405 opt_GranMacros = lookUp SLIT("-fgransim")
406 opt_HiVersion = lookup_def_int "-fhi-version=" 0 -- what version we're compiling.
407 opt_HistorySize = lookup_def_int "-fhistory-size" 20
408 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
409 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
410 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
411 opt_NoImplicitPrelude = lookUp SLIT("-fno-implicit-prelude")
412 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
413 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
415 -- Simplifier switches
416 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
417 -- NoPreInlining is there just to see how bad things
418 -- get if you don't do it!
419 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
420 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
421 opt_SimplCaseOfCase = lookUp SLIT("-fcase-of-case")
422 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
423 opt_SimplPedanticBottoms = lookUp SLIT("-fpedantic-bottoms")
424 opt_SimplExcessPrecision = lookUp SLIT("-fexcess-precision")
427 opt_UF_HiFileThreshold = lookup_def_int "-funfolding-interface-threshold" (45::Int)
428 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
429 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
430 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
431 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
432 opt_UF_UpdateInPlace = lookUp SLIT("-funfolding-update-in-place")
434 opt_UF_CheapOp = ( 1 :: Int) -- Only one instruction; and the args are charged for
435 opt_UF_DearOp = ( 4 :: Int)
437 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
438 opt_NoPruneTyDecls = lookUp SLIT("-fno-prune-tydecls")
439 opt_Static = lookUp SLIT("-static")
440 opt_Unregisterised = lookUp SLIT("-funregisterised")
441 opt_Verbose = lookUp SLIT("-v")
444 %************************************************************************
446 \subsection{List of static hsc flags}
448 %************************************************************************
453 "-fauto-sccs-on-all-toplevs",
454 "-fauto-sccs-on-exported-toplevs",
455 "-fauto-sccs-on-individual-cafs",
456 "-fauto-sccs-on-dicts",
462 "-firrefutable-tuples",
469 "-funfold-casms-in-hi-file",
471 "-funbox-strict-fields",
472 "-femit-extern-decls",
473 "-fglobalise-toplev-names",
476 "-fignore-interface-pragmas",
477 "-fno-hi-version-check",
478 "-fno-implicit-prelude",
480 "-fomit-interface-pragmas",
482 "-fdo-eta-reduction",
483 "-fdo-lambda-eta-expansion",
486 "-fpedantic-bottoms",
487 "-fexcess-precision",
488 "-funfolding-update-in-place",
491 "-fno-prune-tydecls",
495 || any (flip prefixMatch f) [
497 "-fliberate-case-threshold",
500 "-funfolding-interface-threshold",
501 "-funfolding-creation-threshold",
502 "-funfolding-use-threshold",
503 "-funfolding-fun-discount",
504 "-funfolding-keeness-factor"
508 %************************************************************************
510 \subsection{Switch ordering}
512 %************************************************************************
514 These things behave just like enumeration types.
517 instance Eq SimplifierSwitch where
518 a == b = tagOf_SimplSwitch a ==# tagOf_SimplSwitch b
520 instance Ord SimplifierSwitch where
521 a < b = tagOf_SimplSwitch a <# tagOf_SimplSwitch b
522 a <= b = tagOf_SimplSwitch a <=# tagOf_SimplSwitch b
525 tagOf_SimplSwitch (SimplInlinePhase _) = _ILIT(1)
526 tagOf_SimplSwitch (MaxSimplifierIterations _) = _ILIT(2)
527 tagOf_SimplSwitch DontApplyRules = _ILIT(3)
528 tagOf_SimplSwitch SimplLetToCase = _ILIT(4)
529 tagOf_SimplSwitch NoCaseOfCase = _ILIT(5)
531 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
533 lAST_SIMPL_SWITCH_TAG = 5
536 %************************************************************************
538 \subsection{Switch lookup}
540 %************************************************************************
543 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
544 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
545 -- in the list; defaults right at the end.
547 tidied_on_switches = foldl rm_dups [] on_switches
548 -- The fold*l* ensures that we keep the latest switches;
549 -- ie the ones that occur earliest in the list.
551 sw_tbl :: Array Int SwitchResult
552 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
556 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
558 defined_elems = map mk_assoc_elem tidied_on_switches
560 -- (avoid some unboxing, bounds checking, and other horrible things:)
561 #if __GLASGOW_HASKELL__ < 405
562 case sw_tbl of { Array bounds_who_needs_'em stuff ->
564 case sw_tbl of { Array _ _ stuff ->
567 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
568 #if __GLASGOW_HASKELL__ < 400
570 #elif __GLASGOW_HASKELL__ < 403
577 mk_assoc_elem k@(MaxSimplifierIterations lvl)
578 = (iBox (tagOf_SimplSwitch k), SwInt lvl)
579 mk_assoc_elem k@(SimplInlinePhase n)
580 = (iBox (tagOf_SimplSwitch k), SwInt n)
582 = (iBox (tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
584 -- cannot have duplicates if we are going to use the array thing
585 rm_dups switches_so_far switch
586 = if switch `is_elem` switches_so_far
588 else switch : switches_so_far
590 sw `is_elem` [] = False
591 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) ==# (tagOf_SimplSwitch s)
595 Default settings for simplifier switches
598 defaultSimplSwitches = [MaxSimplifierIterations 1]
601 %************************************************************************
603 \subsection{Misc functions for command-line options}
605 %************************************************************************
609 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
611 switchIsOn lookup_fn switch
612 = case (lookup_fn switch) of
613 SwBool False -> False
616 intSwitchSet :: (switch -> SwitchResult)
620 intSwitchSet lookup_fn switch
621 = case (lookup_fn (switch (panic "intSwitchSet"))) of
622 SwInt int -> Just int
627 startsWith :: String -> String -> Maybe String
628 -- startsWith pfx (pfx++rest) = Just rest
630 startsWith [] str = Just str
631 startsWith (c:cs) (s:ss)
632 = if c /= s then Nothing else startsWith cs ss
633 startsWith _ [] = Nothing
635 endsWith :: String -> String -> Maybe String
637 = case (startsWith (reverse cs) (reverse ss)) of
639 Just rs -> Just (reverse rs)