2 % (c) The AQUA Project, Glasgow University, 1996-98
4 \section[CmdLineOpts]{Things to do with command-line options}
28 opt_D_dump_occur_anal,
34 opt_D_dump_simpl_iterations,
35 opt_D_dump_simpl_stats,
41 opt_D_dump_worker_wrapper,
46 opt_D_verbose_core2core,
47 opt_D_verbose_stg2stg,
56 opt_WarnDuplicateExports,
58 opt_WarnIncompletePatterns,
59 opt_WarnMissingMethods,
61 opt_WarnNameShadowing,
62 opt_WarnOverlappingPatterns,
63 opt_WarnSimplePatterns,
66 opt_WarnUnusedImports,
67 opt_WarnUnusedMatches,
70 opt_AutoSccsOnAllToplevs,
71 opt_AutoSccsOnExportedToplevs,
72 opt_AutoSccsOnIndividualCafs,
81 opt_MaxContextReductionDepth,
82 opt_AllowOverlappingInstances,
83 opt_AllowUndecidableInstances,
85 opt_IrrefutableTuples,
93 opt_LiberateCaseThreshold,
95 opt_StgDoLetNoEscapes,
98 opt_UnboxStrictFields,
99 opt_SimplNoPreInlining,
100 opt_SimplDoEtaReduction,
102 opt_SimplDoLambdaEtaExpansion,
106 opt_SimplPedanticBottoms,
109 opt_UF_HiFileThreshold,
110 opt_UF_CreationThreshold,
112 opt_UF_ScrutConDiscount,
113 opt_UF_FunAppDiscount,
114 opt_UF_PrimArgDiscount,
115 opt_UF_KeenessFactor,
121 opt_CompilingPrelude,
122 opt_EmitCExternDecls,
123 opt_EnsureSplittableC,
129 opt_IgnoreIfacePragmas,
131 opt_NoImplicitPrelude,
133 opt_OmitInterfacePragmas,
135 opt_ProduceExportCStubs,
136 opt_ProduceExportHStubs,
153 #include "HsVersions.h"
155 import Array ( array, (//) )
158 import Constants -- Default values for some flags
160 import FastString ( headFS )
161 import Maybes ( assocMaybe, firstJust, maybeToBool )
162 import Panic ( panic, panic# )
164 #if __GLASGOW_HASKELL__ < 301
165 import ArrBase ( Array(..) )
167 import PrelArr ( Array(..) )
171 A command-line {\em switch} is (generally) either on or off; e.g., the
172 ``verbose'' (-v) switch is either on or off. (The \tr{-G<group>}
173 switch is an exception; it's set to a string, or nothing.)
175 A list of {\em ToDo}s is things to be done in a particular part of
176 processing. A (fictitious) example for the Core-to-Core simplifier
177 might be: run the simplifier, then run the strictness analyser, then
178 run the simplifier again (three ``todos'').
180 There are three ``to-do processing centers'' at the moment. In the
181 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
182 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
183 (\tr{simplStg/SimplStg.lhs}).
186 %************************************************************************
188 \subsection{Datatypes associated with command-line options}
190 %************************************************************************
194 = SwBool Bool -- on/off
195 | SwString FAST_STRING -- nothing or a String
196 | SwInt Int -- nothing or an Int
200 data CoreToDo -- These are diff core-to-core passes,
201 -- which may be invoked in any order,
202 -- as many times as you like.
204 = CoreDoSimplify -- The core-to-core simplifier.
205 (SimplifierSwitch -> SwitchResult)
206 -- Each run of the simplifier can take a different
207 -- set of simplifier-specific flags.
214 | CoreDoWorkerWrapper
223 | StgDoUpdateAnalysis
225 | StgDoMassageForProfiling -- should be (next to) last
226 -- There's also setStgVarInfo, but its absolute "lastness"
227 -- is so critical that it is hardwired in (no flag).
232 data SimplifierSwitch
233 = MaxSimplifierIterations Int
234 | SimplInlinePhase Int
237 %************************************************************************
239 \subsection{Classifying command-line options}
241 %************************************************************************
244 lookUp :: FAST_STRING -> Bool
245 lookup_int :: String -> Maybe Int
246 lookup_def_int :: String -> Int -> Int
247 lookup_def_float :: String -> Float -> Float
248 lookup_str :: String -> Maybe String
250 lookUp sw = maybeToBool (assoc_opts sw)
252 lookup_str sw = firstJust (map (startsWith sw) unpacked_opts)
254 lookup_int sw = case (lookup_str sw) of
256 Just xx -> Just (read xx)
258 lookup_def_int sw def = case (lookup_str sw) of
259 Nothing -> def -- Use default
262 lookup_def_float sw def = case (lookup_str sw) of
263 Nothing -> def -- Use default
266 assoc_opts = assocMaybe [ (a, True) | a <- argv ]
267 unpacked_opts = map _UNPK_ argv
270 Putting the compiler options into temporary at-files
271 may turn out to be necessary later on if we turn hsc into
272 a pure Win32 application where I think there's a command-line
273 length limit of 255. unpacked_opts understands the @ option.
275 assoc_opts = assocMaybe [ (_PK_ a, True) | a <- unpacked_opts ]
277 unpacked_opts :: [String]
283 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
289 src_filename :: FAST_STRING
290 src_filename = case argv of
291 filename : rest | headFS filename /= '-' -> filename
292 otherwise -> panic "no filename"
297 opt_D_dump_absC = lookUp SLIT("-ddump-absC")
298 opt_D_dump_asm = lookUp SLIT("-ddump-asm")
299 opt_D_dump_cpranal = lookUp SLIT("-ddump-cpranalyse")
300 opt_D_dump_deriv = lookUp SLIT("-ddump-deriv")
301 opt_D_dump_ds = lookUp SLIT("-ddump-ds")
302 opt_D_dump_flatC = lookUp SLIT("-ddump-flatC")
303 opt_D_dump_foreign = lookUp SLIT("-ddump-foreign-stubs")
304 opt_D_dump_inlinings = lookUp SLIT("-ddump-inlinings")
305 opt_D_dump_occur_anal = lookUp SLIT("-ddump-occur-anal")
306 opt_D_dump_rdr = lookUp SLIT("-ddump-rdr")
307 opt_D_dump_realC = lookUp SLIT("-ddump-realC")
308 opt_D_dump_rn = lookUp SLIT("-ddump-rn")
309 opt_D_dump_simpl = lookUp SLIT("-ddump-simpl")
310 opt_D_dump_simpl_iterations = lookUp SLIT("-ddump-simpl-iterations")
311 opt_D_dump_spec = lookUp SLIT("-ddump-spec")
312 opt_D_dump_stg = lookUp SLIT("-ddump-stg")
313 opt_D_dump_stranal = lookUp SLIT("-ddump-stranal")
314 opt_D_dump_tc = lookUp SLIT("-ddump-tc")
315 opt_D_dump_rules = lookUp SLIT("-ddump-rules")
316 opt_D_dump_usagesp = lookUp SLIT("-ddump-usagesp")
317 opt_D_dump_worker_wrapper = lookUp SLIT("-ddump-workwrap")
318 opt_D_show_passes = lookUp SLIT("-dshow-passes")
319 opt_D_dump_rn_trace = lookUp SLIT("-ddump-rn-trace")
320 opt_D_dump_rn_stats = lookUp SLIT("-ddump-rn-stats")
321 opt_D_dump_simpl_stats = lookUp SLIT("-ddump-simpl-stats")
322 opt_D_source_stats = lookUp SLIT("-dsource-stats")
323 opt_D_verbose_core2core = lookUp SLIT("-dverbose-simpl")
324 opt_D_verbose_stg2stg = lookUp SLIT("-dverbose-stg")
325 opt_DoCoreLinting = lookUp SLIT("-dcore-lint")
326 opt_DoStgLinting = lookUp SLIT("-dstg-lint")
327 opt_DoUSPLinting = lookUp SLIT("-dusagesp-lint")
328 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
329 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
330 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
333 opt_WarnDuplicateExports = lookUp SLIT("-fwarn-duplicate-exports")
334 opt_WarnHiShadows = lookUp SLIT("-fwarn-hi-shadowing")
335 opt_WarnIncompletePatterns = lookUp SLIT("-fwarn-incomplete-patterns")
336 opt_WarnMissingMethods = lookUp SLIT("-fwarn-missing-methods")
337 opt_WarnMissingSigs = lookUp SLIT("-fwarn-missing-signatures")
338 opt_WarnNameShadowing = lookUp SLIT("-fwarn-name-shadowing")
339 opt_WarnOverlappingPatterns = lookUp SLIT("-fwarn-overlapping-patterns")
340 opt_WarnSimplePatterns = lookUp SLIT("-fwarn-simple-patterns")
341 opt_WarnTypeDefaults = lookUp SLIT("-fwarn-type-defaults")
342 opt_WarnUnusedBinds = lookUp SLIT("-fwarn-unused-binds")
343 opt_WarnUnusedImports = lookUp SLIT("-fwarn-unused-imports")
344 opt_WarnUnusedMatches = lookUp SLIT("-fwarn-unused-matches")
347 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
348 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
349 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
350 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
351 opt_SccGroup = lookup_str "-G="
352 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
353 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
356 opt_AllStrict = lookUp SLIT("-fall-strict")
357 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
358 opt_AllowOverlappingInstances = lookUp SLIT("-fallow-overlapping-instances")
359 opt_AllowUndecidableInstances = lookUp SLIT("-fallow-undecidable-instances")
360 opt_GlasgowExts = lookUp SLIT("-fglasgow-exts")
361 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
362 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
363 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
364 opt_Parallel = lookUp SLIT("-fparallel")
367 opt_DoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
368 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
369 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
370 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
371 opt_NoPreInlining = lookUp SLIT("-fno-pre-inlining")
372 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
373 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
374 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
375 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
378 It's a bit unfortunate to have to re-introduce this chap, but on Win32
379 platforms we do need a way of distinguishing between the case when we're
380 compiling a static version of the Prelude and one that's going to be
381 put into a DLL. Why? Because the compiler's wired in modules need to
382 be attributed as either coming from a DLL or not.
384 opt_CompilingPrelude = lookUp SLIT("-fcompiling-prelude")
385 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
386 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
387 opt_GranMacros = lookUp SLIT("-fgransim")
388 opt_HiMap = lookup_str "-himap=" -- file saying where to look for .hi files
389 opt_HiVersion = lookup_def_int "-fhi-version=" 0 -- what version we're compiling.
390 opt_HistorySize = lookup_def_int "-fhistory-size" 20
391 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
392 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
393 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
394 opt_NoImplicitPrelude = lookUp SLIT("-fno-implicit-prelude")
395 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
396 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
397 opt_ProduceC = lookup_str "-C="
398 opt_ProduceExportCStubs = lookup_str "-F="
399 opt_ProduceExportHStubs = lookup_str "-FH="
400 opt_ProduceHi = lookup_str "-hifile=" -- the one to produce this time
402 -- Simplifier switches
403 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
404 -- NoPreInlining is there just to see how bad things
405 -- get if you don't do it!
406 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
407 opt_SimplDoCaseElim = lookUp SLIT("-fdo-case-elim")
408 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
409 opt_SimplCaseOfCase = lookUp SLIT("-fcase-of-case")
410 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
411 opt_SimplLetToCase = lookUp SLIT("-flet-to-case")
412 opt_SimplPedanticBottoms = lookUp SLIT("-fpedantic-bottoms")
415 opt_UF_HiFileThreshold = lookup_def_int "-funfolding-interface-threshold" (30::Int)
416 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (30::Int)
417 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
418 opt_UF_ScrutConDiscount = lookup_def_int "-funfolding-con-discount" (3::Int)
419 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
420 opt_UF_PrimArgDiscount = lookup_def_int "-funfolding-prim-discount" (1::Int)
421 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (2.0::Float)
423 opt_UF_CheapOp = ( 1 :: Int)
424 opt_UF_DearOp = ( 8 :: Int)
425 opt_UF_NoRepLit = ( 20 :: Int) -- Strings can be pretty big
427 opt_ProduceS = lookup_str "-S="
428 opt_ReportCompile = lookUp SLIT("-freport-compile")
429 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
430 opt_SourceUnchanged = lookUp SLIT("-fsource-unchanged")
431 opt_Static = lookUp SLIT("-static")
432 opt_Unregisterised = lookUp SLIT("-funregisterised")
433 opt_Verbose = lookUp SLIT("-v")
435 opt_UseVanillaRegs | opt_Unregisterised = 0
436 | otherwise = mAX_Real_Vanilla_REG
437 opt_UseFloatRegs | opt_Unregisterised = 0
438 | otherwise = mAX_Real_Float_REG
439 opt_UseDoubleRegs | opt_Unregisterised = 0
440 | otherwise = mAX_Real_Double_REG
441 opt_UseLongRegs | opt_Unregisterised = 0
442 | otherwise = mAX_Real_Long_REG
446 classifyOpts :: ([CoreToDo], -- Core-to-Core processing spec
447 [StgToDo]) -- STG-to-STG processing spec
449 classifyOpts = sep argv [] [] -- accumulators...
451 sep :: [FAST_STRING] -- cmd-line opts (input)
452 -> [CoreToDo] -> [StgToDo] -- to_do accumulators
453 -> ([CoreToDo], [StgToDo]) -- result
455 sep [] core_td stg_td -- all done!
456 = (reverse core_td, reverse stg_td)
458 # define CORE_TD(to_do) sep opts (to_do:core_td) stg_td
459 # define STG_TD(to_do) sep opts core_td (to_do:stg_td)
461 sep (opt1:opts) core_td stg_td
462 = case (_UNPK_ opt1) of -- the non-"just match a string" options are at the end...
463 ',' : _ -> sep opts core_td stg_td -- it is for the parser
465 "-fsimplify" -> -- gather up SimplifierSwitches specially...
466 simpl_sep opts defaultSimplSwitches core_td stg_td
468 "-ffloat-inwards" -> CORE_TD(CoreDoFloatInwards)
469 "-ffull-laziness" -> CORE_TD(CoreDoFullLaziness)
470 "-fliberate-case" -> CORE_TD(CoreLiberateCase)
471 "-fprint-core" -> CORE_TD(CoreDoPrintCore)
472 "-fstatic-args" -> CORE_TD(CoreDoStaticArgs)
473 "-fstrictness" -> CORE_TD(CoreDoStrictness)
474 "-fworker-wrapper" -> CORE_TD(CoreDoWorkerWrapper)
475 "-fspecialise" -> CORE_TD(CoreDoSpecialising)
476 "-fusagesp" -> CORE_TD(CoreDoUSPInf)
477 "-fcpr-analyse" -> CORE_TD(CoreDoCPResult)
479 "-fstg-static-args" -> STG_TD(StgDoStaticArgs)
480 "-fupdate-analysis" -> STG_TD(StgDoUpdateAnalysis)
481 "-dstg-stats" -> STG_TD(D_stg_stats)
482 "-flambda-lift" -> STG_TD(StgDoLambdaLift)
483 "-fmassage-stg-for-profiling" -> STG_TD(StgDoMassageForProfiling)
485 _ -> -- NB: the driver is really supposed to handle bad options
486 sep opts core_td stg_td
490 simpl_sep :: [FAST_STRING] -- cmd-line opts (input)
491 -> [SimplifierSwitch] -- simplifier-switch accumulator
492 -> [CoreToDo] -> [StgToDo] -- to_do accumulators
493 -> ([CoreToDo], [StgToDo]) -- result
495 -- "simpl_sep" tailcalls "sep" once it's seen one set
496 -- of SimplifierSwitches for a CoreDoSimplify.
499 simpl_sep input@[] simpl_sw core_td stg_td
500 = panic "simpl_sep []"
503 -- The SimplifierSwitches should be delimited by "[" and "]".
505 simpl_sep (opt1:opts) simpl_sw core_td stg_td
506 = case (_UNPK_ opt1) of
507 "[" -> simpl_sep opts simpl_sw core_td stg_td
509 this_simpl = CoreDoSimplify (isAmongSimpl simpl_sw)
511 sep opts (this_simpl : core_td) stg_td
513 opt -> case matchSimplSw opt of
514 Just sw -> simpl_sep opts (sw:simpl_sw) core_td stg_td
515 Nothing -> simpl_sep opts simpl_sw core_td stg_td
518 = firstJust [ matchSwInt opt "-fmax-simplifier-iterations" MaxSimplifierIterations
519 , matchSwInt opt "-finline-phase" SimplInlinePhase
522 matchSwBool :: String -> String -> a -> Maybe a
523 matchSwBool opt str sw | opt == str = Just sw
524 | otherwise = Nothing
526 matchSwInt :: String -> String -> (Int -> a) -> Maybe a
527 matchSwInt opt str sw = case startsWith str opt of
528 Just opt_left -> Just (sw (read opt_left))
532 %************************************************************************
534 \subsection{Switch ordering}
536 %************************************************************************
538 In spite of the @Produce*@ and @SccGroup@ constructors, these things
539 behave just like enumeration types.
542 instance Eq SimplifierSwitch where
543 a == b = tagOf_SimplSwitch a _EQ_ tagOf_SimplSwitch b
545 instance Ord SimplifierSwitch where
546 a < b = tagOf_SimplSwitch a _LT_ tagOf_SimplSwitch b
547 a <= b = tagOf_SimplSwitch a _LE_ tagOf_SimplSwitch b
550 tagOf_SimplSwitch (SimplInlinePhase _) = ILIT(1)
551 tagOf_SimplSwitch (MaxSimplifierIterations _) = ILIT(2)
553 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
555 lAST_SIMPL_SWITCH_TAG = 2
558 %************************************************************************
560 \subsection{Switch lookup}
562 %************************************************************************
565 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
567 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
568 -- in the list; defaults right at the end.
570 tidied_on_switches = foldl rm_dups [] on_switches
571 -- The fold*l* ensures that we keep the latest switches;
572 -- ie the ones that occur earliest in the list.
574 sw_tbl :: Array Int SwitchResult
575 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
579 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
581 defined_elems = map mk_assoc_elem tidied_on_switches
583 -- (avoid some unboxing, bounds checking, and other horrible things:)
584 case sw_tbl of { Array bounds_who_needs_'em stuff ->
586 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
587 #if __GLASGOW_HASKELL__ < 400
589 #elif __GLASGOW_HASKELL__ < 403
596 mk_assoc_elem k@(MaxSimplifierIterations lvl) = (IBOX(tagOf_SimplSwitch k), SwInt lvl)
597 mk_assoc_elem k@(SimplInlinePhase n) = (IBOX(tagOf_SimplSwitch k), SwInt n)
598 mk_assoc_elem k = (IBOX(tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
600 -- cannot have duplicates if we are going to use the array thing
601 rm_dups switches_so_far switch
602 = if switch `is_elem` switches_so_far
604 else switch : switches_so_far
606 sw `is_elem` [] = False
607 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) _EQ_ (tagOf_SimplSwitch s)
611 Default settings for simplifier switches
614 defaultSimplSwitches = [MaxSimplifierIterations 1]
617 %************************************************************************
619 \subsection{Misc functions for command-line options}
621 %************************************************************************
625 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
627 switchIsOn lookup_fn switch
628 = case (lookup_fn switch) of
629 SwBool False -> False
632 intSwitchSet :: (switch -> SwitchResult)
636 intSwitchSet lookup_fn switch
637 = case (lookup_fn (switch (panic "intSwitchSet"))) of
638 SwInt int -> Just int
643 startsWith :: String -> String -> Maybe String
644 -- startsWith pfx (pfx++rest) = Just rest
646 startsWith [] str = Just str
647 startsWith (c:cs) (s:ss)
648 = if c /= s then Nothing else startsWith cs ss
649 startsWith _ [] = Nothing
651 endsWith :: String -> String -> Maybe String
653 = case (startsWith (reverse cs) (reverse ss)) of
655 Just rs -> Just (reverse rs)