2 % (c) The AQUA Project, Glasgow University, 1996-98
4 \section[CmdLineOpts]{Things to do with command-line options}
29 opt_D_dump_occur_anal,
35 opt_D_dump_simpl_iterations,
36 opt_D_dump_simpl_stats,
42 opt_D_dump_worker_wrapper,
47 opt_D_verbose_core2core,
48 opt_D_verbose_stg2stg,
57 opt_WarnDuplicateExports,
59 opt_WarnIncompletePatterns,
60 opt_WarnMissingMethods,
62 opt_WarnNameShadowing,
63 opt_WarnOverlappingPatterns,
64 opt_WarnSimplePatterns,
67 opt_WarnUnusedImports,
68 opt_WarnUnusedMatches,
71 opt_AutoSccsOnAllToplevs,
72 opt_AutoSccsOnExportedToplevs,
73 opt_AutoSccsOnIndividualCafs,
82 opt_MaxContextReductionDepth,
83 opt_AllowOverlappingInstances,
84 opt_AllowUndecidableInstances,
86 opt_IrrefutableTuples,
94 opt_LiberateCaseThreshold,
96 opt_StgDoLetNoEscapes,
99 opt_UnboxStrictFields,
100 opt_SimplNoPreInlining,
101 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
224 | StgDoUpdateAnalysis
226 | StgDoMassageForProfiling -- should be (next to) last
227 -- There's also setStgVarInfo, but its absolute "lastness"
228 -- is so critical that it is hardwired in (no flag).
233 data SimplifierSwitch
234 = MaxSimplifierIterations Int
235 | SimplInlinePhase Int
238 %************************************************************************
240 \subsection{Classifying command-line options}
242 %************************************************************************
245 lookUp :: FAST_STRING -> Bool
246 lookup_int :: String -> Maybe Int
247 lookup_def_int :: String -> Int -> Int
248 lookup_def_float :: String -> Float -> Float
249 lookup_str :: String -> Maybe String
251 lookUp sw = maybeToBool (assoc_opts sw)
253 lookup_str sw = firstJust (map (startsWith sw) unpacked_opts)
255 lookup_int sw = case (lookup_str sw) of
257 Just xx -> Just (read xx)
259 lookup_def_int sw def = case (lookup_str sw) of
260 Nothing -> def -- Use default
263 lookup_def_float sw def = case (lookup_str sw) of
264 Nothing -> def -- Use default
267 assoc_opts = assocMaybe [ (a, True) | a <- argv ]
268 unpacked_opts = map _UNPK_ argv
271 Putting the compiler options into temporary at-files
272 may turn out to be necessary later on if we turn hsc into
273 a pure Win32 application where I think there's a command-line
274 length limit of 255. unpacked_opts understands the @ option.
276 assoc_opts = assocMaybe [ (_PK_ a, True) | a <- unpacked_opts ]
278 unpacked_opts :: [String]
284 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
290 src_filename :: FAST_STRING
291 src_filename = case argv of
292 filename : rest | headFS filename /= '-' -> filename
293 otherwise -> panic "no filename"
298 opt_D_dump_absC = lookUp SLIT("-ddump-absC")
299 opt_D_dump_asm = lookUp SLIT("-ddump-asm")
300 opt_D_dump_cpranal = lookUp SLIT("-ddump-cpranal")
301 opt_D_dump_deriv = lookUp SLIT("-ddump-deriv")
302 opt_D_dump_ds = lookUp SLIT("-ddump-ds")
303 opt_D_dump_flatC = lookUp SLIT("-ddump-flatC")
304 opt_D_dump_foreign = lookUp SLIT("-ddump-foreign-stubs")
305 opt_D_dump_inlinings = lookUp SLIT("-ddump-inlinings")
306 opt_D_dump_occur_anal = lookUp SLIT("-ddump-occur-anal")
307 opt_D_dump_parsed = lookUp SLIT("-ddump-parsed")
308 opt_D_dump_realC = lookUp SLIT("-ddump-realC")
309 opt_D_dump_rn = lookUp SLIT("-ddump-rn")
310 opt_D_dump_simpl = lookUp SLIT("-ddump-simpl")
311 opt_D_dump_simpl_iterations = lookUp SLIT("-ddump-simpl-iterations")
312 opt_D_dump_spec = lookUp SLIT("-ddump-spec")
313 opt_D_dump_stg = lookUp SLIT("-ddump-stg")
314 opt_D_dump_stranal = lookUp SLIT("-ddump-stranal")
315 opt_D_dump_tc = lookUp SLIT("-ddump-tc")
316 opt_D_dump_rules = lookUp SLIT("-ddump-rules")
317 opt_D_dump_usagesp = lookUp SLIT("-ddump-usagesp")
318 opt_D_dump_cse = lookUp SLIT("-ddump-cse")
319 opt_D_dump_worker_wrapper = lookUp SLIT("-ddump-workwrap")
320 opt_D_show_passes = lookUp SLIT("-dshow-passes")
321 opt_D_dump_rn_trace = lookUp SLIT("-ddump-rn-trace")
322 opt_D_dump_rn_stats = lookUp SLIT("-ddump-rn-stats")
323 opt_D_dump_simpl_stats = lookUp SLIT("-ddump-simpl-stats")
324 opt_D_source_stats = lookUp SLIT("-dsource-stats")
325 opt_D_verbose_core2core = lookUp SLIT("-dverbose-simpl")
326 opt_D_verbose_stg2stg = lookUp SLIT("-dverbose-stg")
327 opt_DoCoreLinting = lookUp SLIT("-dcore-lint")
328 opt_DoStgLinting = lookUp SLIT("-dstg-lint")
329 opt_DoUSPLinting = lookUp SLIT("-dusagesp-lint")
330 opt_PprStyle_NoPrags = lookUp SLIT("-dppr-noprags")
331 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
332 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
335 opt_WarnDuplicateExports = lookUp SLIT("-fwarn-duplicate-exports")
336 opt_WarnHiShadows = lookUp SLIT("-fwarn-hi-shadowing")
337 opt_WarnIncompletePatterns = lookUp SLIT("-fwarn-incomplete-patterns")
338 opt_WarnMissingMethods = lookUp SLIT("-fwarn-missing-methods")
339 opt_WarnMissingSigs = lookUp SLIT("-fwarn-missing-signatures")
340 opt_WarnNameShadowing = lookUp SLIT("-fwarn-name-shadowing")
341 opt_WarnOverlappingPatterns = lookUp SLIT("-fwarn-overlapping-patterns")
342 opt_WarnSimplePatterns = lookUp SLIT("-fwarn-simple-patterns")
343 opt_WarnTypeDefaults = lookUp SLIT("-fwarn-type-defaults")
344 opt_WarnUnusedBinds = lookUp SLIT("-fwarn-unused-binds")
345 opt_WarnUnusedImports = lookUp SLIT("-fwarn-unused-imports")
346 opt_WarnUnusedMatches = lookUp SLIT("-fwarn-unused-matches")
349 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
350 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
351 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
352 opt_AutoSccsOnDicts = lookUp SLIT("-fauto-sccs-on-dicts")
353 opt_SccGroup = lookup_str "-G="
354 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
355 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
358 opt_AllStrict = lookUp SLIT("-fall-strict")
359 opt_DictsStrict = lookUp SLIT("-fdicts-strict")
360 opt_AllowOverlappingInstances = lookUp SLIT("-fallow-overlapping-instances")
361 opt_AllowUndecidableInstances = lookUp SLIT("-fallow-undecidable-instances")
362 opt_GlasgowExts = lookUp SLIT("-fglasgow-exts")
363 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
364 opt_MaxContextReductionDepth = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
365 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
366 opt_Parallel = lookUp SLIT("-fparallel")
369 opt_DoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
370 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
371 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
372 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" (10::Int)
373 opt_NoPreInlining = lookUp SLIT("-fno-pre-inlining")
374 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
375 opt_UnfoldCasms = lookUp SLIT("-funfold-casms-in-hi-file")
376 opt_UsageSPOn = lookUp SLIT("-fusagesp-on")
377 opt_UnboxStrictFields = lookUp SLIT("-funbox-strict-fields")
380 It's a bit unfortunate to have to re-introduce this chap, but on Win32
381 platforms we do need a way of distinguishing between the case when we're
382 compiling a static version of the Prelude and one that's going to be
383 put into a DLL. Why? Because the compiler's wired in modules need to
384 be attributed as either coming from a DLL or not.
386 opt_CompilingPrelude = lookUp SLIT("-fcompiling-prelude")
387 opt_EmitCExternDecls = lookUp SLIT("-femit-extern-decls")
388 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
389 opt_GranMacros = lookUp SLIT("-fgransim")
390 opt_HiMap = lookup_str "-himap=" -- file saying where to look for .hi files
391 opt_HiVersion = lookup_def_int "-fhi-version=" 0 -- what version we're compiling.
392 opt_HistorySize = lookup_def_int "-fhistory-size" 20
393 opt_IgnoreAsserts = lookUp SLIT("-fignore-asserts")
394 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
395 opt_NoHiCheck = lookUp SLIT("-fno-hi-version-check")
396 opt_NoImplicitPrelude = lookUp SLIT("-fno-implicit-prelude")
397 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
398 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
399 opt_ProduceC = lookup_str "-C="
400 opt_ProduceExportCStubs = lookup_str "-F="
401 opt_ProduceExportHStubs = lookup_str "-FH="
402 opt_ProduceHi = lookup_str "-hifile=" -- the one to produce this time
404 -- Simplifier switches
405 opt_SimplNoPreInlining = lookUp SLIT("-fno-pre-inlining")
406 -- NoPreInlining is there just to see how bad things
407 -- get if you don't do it!
408 opt_SimplDoEtaReduction = lookUp SLIT("-fdo-eta-reduction")
409 opt_SimplDoLambdaEtaExpansion = lookUp SLIT("-fdo-lambda-eta-expansion")
410 opt_SimplCaseOfCase = lookUp SLIT("-fcase-of-case")
411 opt_SimplCaseMerge = lookUp SLIT("-fcase-merge")
412 opt_SimplLetToCase = lookUp SLIT("-flet-to-case")
413 opt_SimplPedanticBottoms = lookUp SLIT("-fpedantic-bottoms")
416 opt_UF_HiFileThreshold = lookup_def_int "-funfolding-interface-threshold" (30::Int)
417 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (30::Int)
418 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
419 opt_UF_ScrutConDiscount = lookup_def_int "-funfolding-con-discount" (3::Int)
420 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
421 opt_UF_PrimArgDiscount = lookup_def_int "-funfolding-prim-discount" (1::Int)
422 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (2.0::Float)
424 opt_UF_CheapOp = ( 0 :: Int) -- Only one instruction; and the args are charged for
425 opt_UF_DearOp = ( 4 :: Int)
426 opt_UF_NoRepLit = ( 20 :: Int) -- Strings can be pretty big
428 opt_ProduceS = lookup_str "-S="
429 opt_ReportCompile = lookUp SLIT("-freport-compile")
430 opt_NoPruneDecls = lookUp SLIT("-fno-prune-decls")
431 opt_SourceUnchanged = lookUp SLIT("-fsource-unchanged")
432 opt_Static = lookUp SLIT("-static")
433 opt_Unregisterised = lookUp SLIT("-funregisterised")
434 opt_Verbose = lookUp SLIT("-v")
436 opt_UseVanillaRegs | opt_Unregisterised = 0
437 | otherwise = mAX_Real_Vanilla_REG
438 opt_UseFloatRegs | opt_Unregisterised = 0
439 | otherwise = mAX_Real_Float_REG
440 opt_UseDoubleRegs | opt_Unregisterised = 0
441 | otherwise = mAX_Real_Double_REG
442 opt_UseLongRegs | opt_Unregisterised = 0
443 | otherwise = mAX_Real_Long_REG
447 classifyOpts :: ([CoreToDo], -- Core-to-Core processing spec
448 [StgToDo]) -- STG-to-STG processing spec
450 classifyOpts = sep argv [] [] -- accumulators...
452 sep :: [FAST_STRING] -- cmd-line opts (input)
453 -> [CoreToDo] -> [StgToDo] -- to_do accumulators
454 -> ([CoreToDo], [StgToDo]) -- result
456 sep [] core_td stg_td -- all done!
457 = (reverse core_td, reverse stg_td)
459 # define CORE_TD(to_do) sep opts (to_do:core_td) stg_td
460 # define STG_TD(to_do) sep opts core_td (to_do:stg_td)
462 sep (opt1:opts) core_td stg_td
463 = case (_UNPK_ opt1) of -- the non-"just match a string" options are at the end...
464 ',' : _ -> sep opts core_td stg_td -- it is for the parser
466 "-fsimplify" -> -- gather up SimplifierSwitches specially...
467 simpl_sep opts defaultSimplSwitches core_td stg_td
469 "-ffloat-inwards" -> CORE_TD(CoreDoFloatInwards)
470 "-ffull-laziness" -> CORE_TD(CoreDoFullLaziness)
471 "-fliberate-case" -> CORE_TD(CoreLiberateCase)
472 "-fcse" -> CORE_TD(CoreCSE)
473 "-fprint-core" -> CORE_TD(CoreDoPrintCore)
474 "-fstatic-args" -> CORE_TD(CoreDoStaticArgs)
475 "-fstrictness" -> CORE_TD(CoreDoStrictness)
476 "-fworker-wrapper" -> CORE_TD(CoreDoWorkerWrapper)
477 "-fspecialise" -> CORE_TD(CoreDoSpecialising)
478 "-fusagesp" -> CORE_TD(CoreDoUSPInf)
479 "-fcpr-analyse" -> CORE_TD(CoreDoCPResult)
481 "-fstg-static-args" -> STG_TD(StgDoStaticArgs)
482 "-fupdate-analysis" -> STG_TD(StgDoUpdateAnalysis)
483 "-dstg-stats" -> STG_TD(D_stg_stats)
484 "-flambda-lift" -> STG_TD(StgDoLambdaLift)
485 "-fmassage-stg-for-profiling" -> STG_TD(StgDoMassageForProfiling)
487 _ -> -- NB: the driver is really supposed to handle bad options
488 sep opts core_td stg_td
492 simpl_sep :: [FAST_STRING] -- cmd-line opts (input)
493 -> [SimplifierSwitch] -- simplifier-switch accumulator
494 -> [CoreToDo] -> [StgToDo] -- to_do accumulators
495 -> ([CoreToDo], [StgToDo]) -- result
497 -- "simpl_sep" tailcalls "sep" once it's seen one set
498 -- of SimplifierSwitches for a CoreDoSimplify.
501 simpl_sep input@[] simpl_sw core_td stg_td
502 = panic "simpl_sep []"
505 -- The SimplifierSwitches should be delimited by "[" and "]".
507 simpl_sep (opt1:opts) simpl_sw core_td stg_td
508 = case (_UNPK_ opt1) of
509 "[" -> simpl_sep opts simpl_sw core_td stg_td
511 this_simpl = CoreDoSimplify (isAmongSimpl simpl_sw)
513 sep opts (this_simpl : core_td) stg_td
515 opt -> case matchSimplSw opt of
516 Just sw -> simpl_sep opts (sw:simpl_sw) core_td stg_td
517 Nothing -> simpl_sep opts simpl_sw core_td stg_td
520 = firstJust [ matchSwInt opt "-fmax-simplifier-iterations" MaxSimplifierIterations
521 , matchSwInt opt "-finline-phase" SimplInlinePhase
524 matchSwBool :: String -> String -> a -> Maybe a
525 matchSwBool opt str sw | opt == str = Just sw
526 | otherwise = Nothing
528 matchSwInt :: String -> String -> (Int -> a) -> Maybe a
529 matchSwInt opt str sw = case startsWith str opt of
530 Just opt_left -> Just (sw (read opt_left))
534 %************************************************************************
536 \subsection{Switch ordering}
538 %************************************************************************
540 In spite of the @Produce*@ and @SccGroup@ constructors, these things
541 behave just like enumeration types.
544 instance Eq SimplifierSwitch where
545 a == b = tagOf_SimplSwitch a _EQ_ tagOf_SimplSwitch b
547 instance Ord SimplifierSwitch where
548 a < b = tagOf_SimplSwitch a _LT_ tagOf_SimplSwitch b
549 a <= b = tagOf_SimplSwitch a _LE_ tagOf_SimplSwitch b
552 tagOf_SimplSwitch (SimplInlinePhase _) = ILIT(1)
553 tagOf_SimplSwitch (MaxSimplifierIterations _) = ILIT(2)
555 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
557 lAST_SIMPL_SWITCH_TAG = 2
560 %************************************************************************
562 \subsection{Switch lookup}
564 %************************************************************************
567 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
569 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
570 -- in the list; defaults right at the end.
572 tidied_on_switches = foldl rm_dups [] on_switches
573 -- The fold*l* ensures that we keep the latest switches;
574 -- ie the ones that occur earliest in the list.
576 sw_tbl :: Array Int SwitchResult
577 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
581 all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
583 defined_elems = map mk_assoc_elem tidied_on_switches
585 -- (avoid some unboxing, bounds checking, and other horrible things:)
586 case sw_tbl of { Array bounds_who_needs_'em stuff ->
588 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
589 #if __GLASGOW_HASKELL__ < 400
591 #elif __GLASGOW_HASKELL__ < 403
598 mk_assoc_elem k@(MaxSimplifierIterations lvl) = (IBOX(tagOf_SimplSwitch k), SwInt lvl)
599 mk_assoc_elem k@(SimplInlinePhase n) = (IBOX(tagOf_SimplSwitch k), SwInt n)
600 mk_assoc_elem k = (IBOX(tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
602 -- cannot have duplicates if we are going to use the array thing
603 rm_dups switches_so_far switch
604 = if switch `is_elem` switches_so_far
606 else switch : switches_so_far
608 sw `is_elem` [] = False
609 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) _EQ_ (tagOf_SimplSwitch s)
613 Default settings for simplifier switches
616 defaultSimplSwitches = [MaxSimplifierIterations 1]
619 %************************************************************************
621 \subsection{Misc functions for command-line options}
623 %************************************************************************
627 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
629 switchIsOn lookup_fn switch
630 = case (lookup_fn switch) of
631 SwBool False -> False
634 intSwitchSet :: (switch -> SwitchResult)
638 intSwitchSet lookup_fn switch
639 = case (lookup_fn (switch (panic "intSwitchSet"))) of
640 SwInt int -> Just int
645 startsWith :: String -> String -> Maybe String
646 -- startsWith pfx (pfx++rest) = Just rest
648 startsWith [] str = Just str
649 startsWith (c:cs) (s:ss)
650 = if c /= s then Nothing else startsWith cs ss
651 startsWith _ [] = Nothing
653 endsWith :: String -> String -> Maybe String
655 = case (startsWith (reverse cs) (reverse ss)) of
657 Just rs -> Just (reverse rs)