2 % (c) The AQUA Project, Glasgow University, 1996-98
4 \section[CmdLineOpts]{Things to do with command-line options}
26 opt_D_dump_occur_anal,
32 opt_D_dump_simpl_iterations,
33 opt_D_dump_simpl_stats,
39 opt_D_dump_worker_wrapper,
44 opt_D_verbose_core2core,
45 opt_D_verbose_stg2stg,
54 opt_WarnDuplicateExports,
56 opt_WarnIncompletePatterns,
57 opt_WarnMissingMethods,
59 opt_WarnNameShadowing,
60 opt_WarnOverlappingPatterns,
61 opt_WarnSimplePatterns,
64 opt_WarnUnusedImports,
65 opt_WarnUnusedMatches,
68 opt_AutoSccsOnAllToplevs,
69 opt_AutoSccsOnExportedToplevs,
70 opt_AutoSccsOnIndividualCafs,
79 opt_MaxContextReductionDepth,
80 opt_AllowOverlappingInstances,
81 opt_AllowUndecidableInstances,
83 opt_IrrefutableTuples,
91 opt_LiberateCaseThreshold,
93 opt_StgDoLetNoEscapes,
96 opt_UnboxStrictFields,
97 opt_SimplNoPreInlining,
98 opt_SimplDoEtaReduction,
100 opt_SimplDoLambdaEtaExpansion,
104 opt_SimplPedanticBottoms,
107 opt_UF_HiFileThreshold,
108 opt_UF_CreationThreshold,
110 opt_UF_ScrutConDiscount,
111 opt_UF_FunAppDiscount,
112 opt_UF_PrimArgDiscount,
113 opt_UF_KeenessFactor,
119 opt_CompilingPrelude,
120 opt_EmitCExternDecls,
121 opt_EnsureSplittableC,
127 opt_IgnoreIfacePragmas,
129 opt_NoImplicitPrelude,
131 opt_OmitInterfacePragmas,
133 opt_ProduceExportCStubs,
134 opt_ProduceExportHStubs,
151 #include "HsVersions.h"
153 import Array ( array, (//) )
156 import Constants -- Default values for some flags
158 import Maybes ( assocMaybe, firstJust, maybeToBool )
159 import Panic ( panic, panic# )
161 #if __GLASGOW_HASKELL__ < 301
162 import ArrBase ( Array(..) )
164 import PrelArr ( Array(..) )
168 A command-line {\em switch} is (generally) either on or off; e.g., the
169 ``verbose'' (-v) switch is either on or off. (The \tr{-G<group>}
170 switch is an exception; it's set to a string, or nothing.)
172 A list of {\em ToDo}s is things to be done in a particular part of
173 processing. A (fictitious) example for the Core-to-Core simplifier
174 might be: run the simplifier, then run the strictness analyser, then
175 run the simplifier again (three ``todos'').
177 There are three ``to-do processing centers'' at the moment. In the
178 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
179 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
180 (\tr{simplStg/SimplStg.lhs}).
183 %************************************************************************
185 \subsection{Datatypes associated with command-line options}
187 %************************************************************************
191 = SwBool Bool -- on/off
192 | SwString FAST_STRING -- nothing or a String
193 | SwInt Int -- nothing or an Int
197 data CoreToDo -- These are diff core-to-core passes,
198 -- which may be invoked in any order,
199 -- as many times as you like.
201 = CoreDoSimplify -- The core-to-core simplifier.
202 (SimplifierSwitch -> SwitchResult)
203 -- Each run of the simplifier can take a different
204 -- set of simplifier-specific flags.
211 | CoreDoWorkerWrapper
220 | StgDoUpdateAnalysis
222 | StgDoMassageForProfiling -- should be (next to) last
223 -- There's also setStgVarInfo, but its absolute "lastness"
224 -- is so critical that it is hardwired in (no flag).
229 data SimplifierSwitch
230 = MaxSimplifierIterations Int
231 | SimplInlinePhase Int
234 %************************************************************************
236 \subsection{Classifying command-line options}
238 %************************************************************************
241 lookUp :: FAST_STRING -> Bool
242 lookup_int :: String -> Maybe Int
243 lookup_def_int :: String -> Int -> Int
244 lookup_def_float :: String -> Float -> Float
245 lookup_str :: String -> Maybe String
247 lookUp sw = maybeToBool (assoc_opts sw)
249 lookup_str sw = firstJust (map (startsWith sw) unpacked_opts)
251 lookup_int sw = case (lookup_str sw) of
253 Just xx -> Just (read xx)
255 lookup_def_int sw def = case (lookup_str sw) of
256 Nothing -> def -- Use default
259 lookup_def_float sw def = case (lookup_str sw) of
260 Nothing -> def -- Use default
263 assoc_opts = assocMaybe [ (a, True) | a <- argv ]
264 unpacked_opts = map _UNPK_ argv
267 Putting the compiler options into temporary at-files
268 may turn out to be necessary later on if we turn hsc into
269 a pure Win32 application where I think there's a command-line
270 length limit of 255. unpacked_opts understands the @ option.
272 assoc_opts = assocMaybe [ (_PK_ a, True) | a <- unpacked_opts ]
274 unpacked_opts :: [String]
280 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
287 opt_D_dump_absC = lookUp SLIT("-ddump-absC")
288 opt_D_dump_asm = lookUp SLIT("-ddump-asm")
289 opt_D_dump_cpranal = lookUp SLIT("-ddump-cpranalyse")
290 opt_D_dump_deriv = lookUp SLIT("-ddump-deriv")
291 opt_D_dump_ds = lookUp SLIT("-ddump-ds")
292 opt_D_dump_flatC = lookUp SLIT("-ddump-flatC")
293 opt_D_dump_foreign = lookUp SLIT("-ddump-foreign-stubs")
294 opt_D_dump_inlinings = lookUp SLIT("-ddump-inlinings")
295 opt_D_dump_occur_anal = lookUp SLIT("-ddump-occur-anal")
296 opt_D_dump_rdr = lookUp SLIT("-ddump-rdr")
297 opt_D_dump_realC = lookUp SLIT("-ddump-realC")
298 opt_D_dump_rn = lookUp SLIT("-ddump-rn")
299 opt_D_dump_simpl = lookUp SLIT("-ddump-simpl")
300 opt_D_dump_simpl_iterations = lookUp SLIT("-ddump-simpl-iterations")
301 opt_D_dump_spec = lookUp SLIT("-ddump-spec")
302 opt_D_dump_stg = lookUp SLIT("-ddump-stg")
303 opt_D_dump_stranal = lookUp SLIT("-ddump-stranal")
304 opt_D_dump_tc = lookUp SLIT("-ddump-tc")
305 opt_D_dump_rules = lookUp SLIT("-ddump-rules")
306 opt_D_dump_usagesp = lookUp SLIT("-ddump-usagesp")
307 opt_D_dump_worker_wrapper = lookUp SLIT("-ddump-workwrap")
308 opt_D_show_passes = lookUp SLIT("-dshow-passes")
309 opt_D_dump_rn_trace = lookUp SLIT("-ddump-rn-trace")
310 opt_D_dump_rn_stats = lookUp SLIT("-ddump-rn-stats")
311 opt_D_dump_simpl_stats = lookUp SLIT("-ddump-simpl-stats")
312 opt_D_source_stats = lookUp SLIT("-dsource-stats")
313 opt_D_verbose_core2core = lookUp SLIT("-dverbose-simpl")
314 opt_D_verbose_stg2stg = lookUp SLIT("-dverbose-stg")
315 opt_DoCoreLinting = lookUp SLIT("-dcore-lint")
316 opt_DoStgLinting = lookUp SLIT("-dstg-lint")
317 opt_DoUSPLinting = lookUp SLIT("-dusagesp-lint")
318 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
319 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
320 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
323 opt_WarnDuplicateExports = lookUp SLIT("-fwarn-duplicate-exports")
324 opt_WarnHiShadows = lookUp SLIT("-fwarn-hi-shadowing")
325 opt_WarnIncompletePatterns = lookUp SLIT("-fwarn-incomplete-patterns")
326 opt_WarnMissingMethods = lookUp SLIT("-fwarn-missing-methods")
327 opt_WarnMissingSigs = lookUp SLIT("-fwarn-missing-signatures")
328 opt_WarnNameShadowing = lookUp SLIT("-fwarn-name-shadowing")
329 opt_WarnOverlappingPatterns = lookUp SLIT("-fwarn-overlapping-patterns")
330 opt_WarnSimplePatterns = lookUp SLIT("-fwarn-simple-patterns")
331 opt_WarnTypeDefaults = lookUp SLIT("-fwarn-type-defaults")
332 opt_WarnUnusedBinds = lookUp SLIT("-fwarn-unused-binds")
333 opt_WarnUnusedImports = lookUp SLIT("-fwarn-unused-imports")
334 opt_WarnUnusedMatches = lookUp SLIT("-fwarn-unused-matches")
337 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
338 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
339 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
340 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
341 opt_SccGroup = lookup_str "-G="
342 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
343 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
346 opt_AllStrict = lookUp SLIT("-fall-strict")
347 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
348 opt_AllowOverlappingInstances = lookUp SLIT("-fallow-overlapping-instances")
349 opt_AllowUndecidableInstances = lookUp SLIT("-fallow-undecidable-instances")
350 opt_GlasgowExts = lookUp SLIT("-fglasgow-exts")
351 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
352 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
353 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
354 opt_Parallel = lookUp SLIT("-fparallel")
357 opt_DoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
358 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
359 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
360 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
361 opt_NoPreInlining = lookUp SLIT("-fno-pre-inlining")
362 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
363 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
364 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
365 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
368 It's a bit unfortunate to have to re-introduce this chap, but on Win32
369 platforms we do need a way of distinguishing between the case when we're
370 compiling a static version of the Prelude and one that's going to be
371 put into a DLL. Why? Because the compiler's wired in modules need to
372 be attributed as either coming from a DLL or not.
374 opt_CompilingPrelude = lookUp SLIT("-fcompiling-prelude")
375 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
376 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
377 opt_GranMacros = lookUp SLIT("-fgransim")
378 opt_HiMap = lookup_str "-himap=" -- file saying where to look for .hi files
379 opt_HiVersion = lookup_def_int "-fhi-version=" 0 -- what version we're compiling.
380 opt_HistorySize = lookup_def_int "-fhistory-size" 20
381 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
382 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
383 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
384 opt_NoImplicitPrelude = lookUp SLIT("-fno-implicit-prelude")
385 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
386 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
387 opt_ProduceC = lookup_str "-C="
388 opt_ProduceExportCStubs = lookup_str "-F="
389 opt_ProduceExportHStubs = lookup_str "-FH="
390 opt_ProduceHi = lookup_str "-hifile=" -- the one to produce this time
392 -- Simplifier switches
393 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
394 -- NoPreInlining is there just to see how bad things
395 -- get if you don't do it!
396 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
397 opt_SimplDoCaseElim = lookUp SLIT("-fdo-case-elim")
398 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
399 opt_SimplCaseOfCase = lookUp SLIT("-fcase-of-case")
400 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
401 opt_SimplLetToCase = lookUp SLIT("-flet-to-case")
402 opt_SimplPedanticBottoms = lookUp SLIT("-fpedantic-bottoms")
405 opt_UF_HiFileThreshold = lookup_def_int "-funfolding-interface-threshold" (30::Int)
406 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (30::Int)
407 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
408 opt_UF_ScrutConDiscount = lookup_def_int "-funfolding-con-discount" (3::Int)
409 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
410 opt_UF_PrimArgDiscount = lookup_def_int "-funfolding-prim-discount" (1::Int)
411 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (2.0::Float)
413 opt_UF_CheapOp = ( 1 :: Int)
414 opt_UF_DearOp = ( 8 :: Int)
415 opt_UF_NoRepLit = ( 20 :: Int) -- Strings can be pretty big
417 opt_ProduceS = lookup_str "-S="
418 opt_ReportCompile = lookUp SLIT("-freport-compile")
419 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
420 opt_SourceUnchanged = lookUp SLIT("-fsource-unchanged")
421 opt_Static = lookUp SLIT("-static")
422 opt_Unregisterised = lookUp SLIT("-funregisterised")
423 opt_Verbose = lookUp SLIT("-v")
425 opt_UseVanillaRegs | opt_Unregisterised = 0
426 | otherwise = mAX_Real_Vanilla_REG
427 opt_UseFloatRegs | opt_Unregisterised = 0
428 | otherwise = mAX_Real_Float_REG
429 opt_UseDoubleRegs | opt_Unregisterised = 0
430 | otherwise = mAX_Real_Double_REG
431 opt_UseLongRegs | opt_Unregisterised = 0
432 | otherwise = mAX_Real_Long_REG
436 classifyOpts :: ([CoreToDo], -- Core-to-Core processing spec
437 [StgToDo]) -- STG-to-STG processing spec
439 classifyOpts = sep argv [] [] -- accumulators...
441 sep :: [FAST_STRING] -- cmd-line opts (input)
442 -> [CoreToDo] -> [StgToDo] -- to_do accumulators
443 -> ([CoreToDo], [StgToDo]) -- result
445 sep [] core_td stg_td -- all done!
446 = (reverse core_td, reverse stg_td)
448 # define CORE_TD(to_do) sep opts (to_do:core_td) stg_td
449 # define STG_TD(to_do) sep opts core_td (to_do:stg_td)
451 sep (opt1:opts) core_td stg_td
452 = case (_UNPK_ opt1) of -- the non-"just match a string" options are at the end...
453 ',' : _ -> sep opts core_td stg_td -- it is for the parser
455 "-fsimplify" -> -- gather up SimplifierSwitches specially...
456 simpl_sep opts defaultSimplSwitches core_td stg_td
458 "-ffloat-inwards" -> CORE_TD(CoreDoFloatInwards)
459 "-ffull-laziness" -> CORE_TD(CoreDoFullLaziness)
460 "-fliberate-case" -> CORE_TD(CoreLiberateCase)
461 "-fprint-core" -> CORE_TD(CoreDoPrintCore)
462 "-fstatic-args" -> CORE_TD(CoreDoStaticArgs)
463 "-fstrictness" -> CORE_TD(CoreDoStrictness)
464 "-fworker-wrapper" -> CORE_TD(CoreDoWorkerWrapper)
465 "-fspecialise" -> CORE_TD(CoreDoSpecialising)
466 "-fusagesp" -> CORE_TD(CoreDoUSPInf)
467 "-fcpr-analyse" -> CORE_TD(CoreDoCPResult)
469 "-fstg-static-args" -> STG_TD(StgDoStaticArgs)
470 "-fupdate-analysis" -> STG_TD(StgDoUpdateAnalysis)
471 "-dstg-stats" -> STG_TD(D_stg_stats)
472 "-flambda-lift" -> STG_TD(StgDoLambdaLift)
473 "-fmassage-stg-for-profiling" -> STG_TD(StgDoMassageForProfiling)
475 _ -> -- NB: the driver is really supposed to handle bad options
476 sep opts core_td stg_td
480 simpl_sep :: [FAST_STRING] -- cmd-line opts (input)
481 -> [SimplifierSwitch] -- simplifier-switch accumulator
482 -> [CoreToDo] -> [StgToDo] -- to_do accumulators
483 -> ([CoreToDo], [StgToDo]) -- result
485 -- "simpl_sep" tailcalls "sep" once it's seen one set
486 -- of SimplifierSwitches for a CoreDoSimplify.
489 simpl_sep input@[] simpl_sw core_td stg_td
490 = panic "simpl_sep []"
493 -- The SimplifierSwitches should be delimited by "[" and "]".
495 simpl_sep (opt1:opts) simpl_sw core_td stg_td
496 = case (_UNPK_ opt1) of
497 "[" -> simpl_sep opts simpl_sw core_td stg_td
499 this_simpl = CoreDoSimplify (isAmongSimpl simpl_sw)
501 sep opts (this_simpl : core_td) stg_td
503 opt -> case matchSimplSw opt of
504 Just sw -> simpl_sep opts (sw:simpl_sw) core_td stg_td
505 Nothing -> simpl_sep opts simpl_sw core_td stg_td
508 = firstJust [ matchSwInt opt "-fmax-simplifier-iterations" MaxSimplifierIterations
509 , matchSwInt opt "-finline-phase" SimplInlinePhase
512 matchSwBool :: String -> String -> a -> Maybe a
513 matchSwBool opt str sw | opt == str = Just sw
514 | otherwise = Nothing
516 matchSwInt :: String -> String -> (Int -> a) -> Maybe a
517 matchSwInt opt str sw = case startsWith str opt of
518 Just opt_left -> Just (sw (read opt_left))
522 %************************************************************************
524 \subsection{Switch ordering}
526 %************************************************************************
528 In spite of the @Produce*@ and @SccGroup@ constructors, these things
529 behave just like enumeration types.
532 instance Eq SimplifierSwitch where
533 a == b = tagOf_SimplSwitch a _EQ_ tagOf_SimplSwitch b
535 instance Ord SimplifierSwitch where
536 a < b = tagOf_SimplSwitch a _LT_ tagOf_SimplSwitch b
537 a <= b = tagOf_SimplSwitch a _LE_ tagOf_SimplSwitch b
540 tagOf_SimplSwitch (SimplInlinePhase _) = ILIT(1)
541 tagOf_SimplSwitch (MaxSimplifierIterations _) = ILIT(2)
543 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
545 lAST_SIMPL_SWITCH_TAG = 2
548 %************************************************************************
550 \subsection{Switch lookup}
552 %************************************************************************
555 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
557 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
558 -- in the list; defaults right at the end.
560 tidied_on_switches = foldl rm_dups [] on_switches
561 -- The fold*l* ensures that we keep the latest switches;
562 -- ie the ones that occur earliest in the list.
564 sw_tbl :: Array Int SwitchResult
565 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
569 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
571 defined_elems = map mk_assoc_elem tidied_on_switches
573 -- (avoid some unboxing, bounds checking, and other horrible things:)
574 case sw_tbl of { Array bounds_who_needs_'em stuff ->
576 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
577 #if __GLASGOW_HASKELL__ < 400
579 #elif __GLASGOW_HASKELL__ < 403
586 mk_assoc_elem k@(MaxSimplifierIterations lvl) = (IBOX(tagOf_SimplSwitch k), SwInt lvl)
587 mk_assoc_elem k@(SimplInlinePhase n) = (IBOX(tagOf_SimplSwitch k), SwInt n)
588 mk_assoc_elem k = (IBOX(tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
590 -- cannot have duplicates if we are going to use the array thing
591 rm_dups switches_so_far switch
592 = if switch `is_elem` switches_so_far
594 else switch : switches_so_far
596 sw `is_elem` [] = False
597 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) _EQ_ (tagOf_SimplSwitch s)
601 Default settings for simplifier switches
604 defaultSimplSwitches = [MaxSimplifierIterations 1]
607 %************************************************************************
609 \subsection{Misc functions for command-line options}
611 %************************************************************************
615 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
617 switchIsOn lookup_fn switch
618 = case (lookup_fn switch) of
619 SwBool False -> False
622 intSwitchSet :: (switch -> SwitchResult)
626 intSwitchSet lookup_fn switch
627 = case (lookup_fn (switch (panic "intSwitchSet"))) of
628 SwInt int -> Just int
633 startsWith :: String -> String -> Maybe String
634 -- startsWith pfx (pfx++rest) = Just rest
636 startsWith [] str = Just str
637 startsWith (c:cs) (s:ss)
638 = if c /= s then Nothing else startsWith cs ss
639 startsWith _ [] = Nothing
641 endsWith :: String -> String -> Maybe String
643 = case (startsWith (reverse cs) (reverse ss)) of
645 Just rs -> Just (reverse rs)