a5ef6c44df83ef5cb8df3308695719dd70cf2637
[ghc-hetmet.git] / ghc / compiler / main / CmdLineOpts.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1996
3 %
4 \section[CmdLineOpts]{Things to do with command-line options}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module CmdLineOpts (
10         CoreToDo(..),
11         SimplifierSwitch(..),
12         StgToDo(..),
13         SwitchResult(..),
14         classifyOpts,
15
16         intSwitchSet,
17         switchIsOn,
18
19         maybe_CompilingGhcInternals,
20         opt_AllDemanded,
21         opt_AllStrict,
22         opt_AutoSccsOnAllToplevs,
23         opt_AutoSccsOnExportedToplevs,
24         opt_AutoSccsOnIndividualCafs,
25         opt_CompilingGhcInternals,
26         opt_D_dump_absC,
27         opt_D_dump_asm,
28         opt_D_dump_deforest,
29         opt_D_dump_deriv,
30         opt_D_dump_ds,
31         opt_D_dump_flatC,
32         opt_D_dump_occur_anal,
33         opt_D_dump_rdr,
34         opt_D_dump_realC,
35         opt_D_dump_rn,
36         opt_D_dump_simpl,
37         opt_D_dump_simpl_iterations,
38         opt_D_dump_spec,
39         opt_D_dump_stg,
40         opt_D_dump_stranal,
41         opt_D_dump_tc,
42         opt_D_show_passes,
43         opt_D_show_rn_trace,
44         opt_D_simplifier_stats,
45         opt_D_source_stats,
46         opt_D_verbose_core2core,
47         opt_D_verbose_stg2stg,
48         opt_DoCoreLinting,
49         opt_DoStgLinting,
50         opt_DoSemiTagging,
51         opt_DoEtaReduction,
52         opt_DoTickyProfiling,
53         opt_EnsureSplittableC,
54         opt_FoldrBuildOn,
55         opt_FoldrBuildTrace,
56         opt_ForConcurrent,
57         opt_GlasgowExts,
58         opt_GranMacros,
59         opt_HiMap,
60         opt_IgnoreIfacePragmas,
61         opt_IgnoreStrictnessPragmas,
62         opt_IrrefutableEverything,
63         opt_IrrefutableTuples,
64         opt_LiberateCaseThreshold,
65         opt_NoImplicitPrelude,
66         opt_NumbersStrict,
67         opt_OmitBlackHoling,
68         opt_OmitDefaultInstanceMethods,
69         opt_OmitInterfacePragmas,
70         opt_PprStyle_All,
71         opt_PprStyle_Debug,
72         opt_PprStyle_User,
73         opt_PprUserLength,
74         opt_ProduceC,
75         opt_ProduceHi,
76         opt_ProduceS,
77         opt_ReportWhyUnfoldingsDisallowed,
78         opt_ReturnInRegsThreshold,
79         opt_SccGroup,
80         opt_SccProfilingOn,
81         opt_ShowImportSpecs,
82         opt_ShowPragmaNameErrs,
83         opt_SigsRequired,
84         opt_SourceUnchanged,
85         opt_SpecialiseAll,
86         opt_SpecialiseImports,
87         opt_SpecialiseOverloaded,
88         opt_SpecialiseTrace,
89         opt_SpecialiseUnboxed,
90         opt_StgDoLetNoEscapes,
91
92         opt_InterfaceUnfoldThreshold,
93         opt_UnfoldingCreationThreshold,
94         opt_UnfoldingConDiscount,
95         opt_UnfoldingUseThreshold,
96
97         opt_Verbose,
98         opt_WarnNameShadowing,
99         opt_WarnUnusedNames,
100         opt_WarnIncompletePatterns, opt_WarnOverlappedPatterns,
101         opt_PruneTyDecls, opt_PruneInstDecls,
102         opt_D_show_unused_imports,
103         opt_D_show_rn_stats,
104         
105         all_toplev_ids_visible
106     ) where
107
108 IMPORT_1_3(Array(array, (//)))
109 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
110 import PreludeGlaST     -- bad bad bad boy, Will (_Array internals)
111 #else
112 import GlaExts
113 import ArrBase
114 -- 2.04 and later exports Lift from GlaExts
115 #if __GLASGOW_HASKELL__ < 204
116 import PrelBase (Lift(..))
117 #endif
118 #endif
119 import Argv
120
121 CHK_Ubiq() -- debugging consistency check
122
123 import Constants        -- Default values for some flags
124
125 import Maybes           ( assocMaybe, firstJust, maybeToBool )
126 import Util             ( startsWith, panic, panic#, assertPanic )
127 \end{code}
128
129 A command-line {\em switch} is (generally) either on or off; e.g., the
130 ``verbose'' (-v) switch is either on or off.  (The \tr{-G<group>}
131 switch is an exception; it's set to a string, or nothing.)
132
133 A list of {\em ToDo}s is things to be done in a particular part of
134 processing.  A (fictitious) example for the Core-to-Core simplifier
135 might be: run the simplifier, then run the strictness analyser, then
136 run the simplifier again (three ``todos'').
137
138 There are three ``to-do processing centers'' at the moment.  In the
139 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
140 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
141 (\tr{simplStg/SimplStg.lhs}).
142
143 %************************************************************************
144 %*                                                                      *
145 \subsection{Datatypes associated with command-line options}
146 %*                                                                      *
147 %************************************************************************
148
149 \begin{code}
150 data SwitchResult
151   = SwBool      Bool            -- on/off
152   | SwString    FAST_STRING     -- nothing or a String
153   | SwInt       Int             -- nothing or an Int
154 \end{code}
155
156 \begin{code}
157 data CoreToDo           -- These are diff core-to-core passes,
158                         -- which may be invoked in any order,
159                         -- as many times as you like.
160
161   = CoreDoSimplify      -- The core-to-core simplifier.
162         (SimplifierSwitch -> SwitchResult)
163                         -- Each run of the simplifier can take a different
164                         -- set of simplifier-specific flags.
165   | CoreDoCalcInlinings1
166   | CoreDoCalcInlinings2
167   | CoreDoFloatInwards
168   | CoreDoFullLaziness
169   | CoreLiberateCase
170   | CoreDoPrintCore
171   | CoreDoStaticArgs
172   | CoreDoStrictness
173   | CoreDoSpecialising
174   | CoreDoDeforest
175   | CoreDoFoldrBuildWorkerWrapper
176   | CoreDoFoldrBuildWWAnal
177 \end{code}
178
179 \begin{code}
180 data StgToDo
181   = StgDoStaticArgs
182   | StgDoUpdateAnalysis
183   | StgDoLambdaLift
184   | StgDoMassageForProfiling  -- should be (next to) last
185   -- There's also setStgVarInfo, but its absolute "lastness"
186   -- is so critical that it is hardwired in (no flag).
187   | D_stg_stats
188 \end{code}
189
190 \begin{code}
191 data SimplifierSwitch
192   = SimplOkToDupCode
193   | SimplFloatLetsExposingWHNF
194   | SimplOkToFloatPrimOps
195   | SimplAlwaysFloatLetsFromLets
196   | SimplDoCaseElim
197   | SimplReuseCon
198   | SimplCaseOfCase
199   | SimplLetToCase
200   | SimplMayDeleteConjurableIds
201   | SimplPedanticBottoms -- see Simplifier for an explanation
202   | SimplDoArityExpand   -- expand arity of bindings
203   | SimplDoFoldrBuild    -- This is the per-simplification flag;
204                          -- see also FoldrBuildOn, used elsewhere
205                          -- in the compiler.
206   | SimplDoInlineFoldrBuild
207                          -- inline foldr/build (*after* f/b rule is used)
208
209   | IgnoreINLINEPragma
210   | SimplDoLambdaEtaExpansion
211
212   | EssentialUnfoldingsOnly -- never mind the thresholds, only
213                             -- do unfoldings that *must* be done
214                             -- (to saturate constructors and primitives)
215
216   | ShowSimplifierProgress  -- report counts on every interation
217
218   | MaxSimplifierIterations Int
219
220   | KeepSpecPragmaIds       -- We normally *toss* Ids we can do without
221   | KeepUnusedBindings
222
223   | SimplNoLetFromCase      -- used when turning off floating entirely
224   | SimplNoLetFromApp       -- (for experimentation only) WDP 95/10
225   | SimplNoLetFromStrictLet
226
227   | SimplDontFoldBackAppend
228                         -- we fold `foldr (:)' back into flip (++),
229                         -- but we *don't* want to do it when compiling
230                         -- List.hs, otherwise
231                         -- xs ++ ys = foldr (:) ys xs
232                         -- {- via our loopback -}
233                         -- xs ++ ys = xs ++ ys
234                         -- Oops!
235                         -- So only use this flag inside List.hs
236                         -- (Sigh, what a HACK, Andy.  WDP 96/01)
237
238   | SimplCaseMerge
239   | SimplCaseScrutinee  -- This flag tells that the expression being simplified is
240                         -- the scrutinee of a case expression, so we should
241                         -- apply the scrutinee discount when considering inlinings.
242                         -- See SimplVar.lhs
243 \end{code}
244
245 %************************************************************************
246 %*                                                                      *
247 \subsection{Classifying command-line options}
248 %*                                                                      *
249 %************************************************************************
250
251 \begin{code}
252 lookUp         :: FAST_STRING -> Bool
253 lookup_int     :: String -> Maybe Int
254 lookup_def_int :: String -> Int -> Int
255 lookup_str     :: String -> Maybe String
256
257 lookUp     sw = maybeToBool (assoc_opts sw)
258         
259 lookup_str sw = firstJust (map (startsWith sw) unpacked_opts)
260
261 lookup_int sw = case (lookup_str sw) of
262                   Nothing -> Nothing
263                   Just xx -> Just (read xx)
264
265 lookup_def_int sw def = case (lookup_str sw) of
266                             Nothing -> def              -- Use default
267                             Just xx -> read xx
268
269 assoc_opts    = assocMaybe [ (a, True) | a <- argv ]
270 unpacked_opts = map _UNPK_ argv
271 \end{code}
272
273 \begin{code}
274 opt_AllDemanded                 = lookUp  SLIT("-fall-demanded")
275 opt_AllStrict                   = lookUp  SLIT("-fall-strict")
276 opt_AutoSccsOnAllToplevs        = lookUp  SLIT("-fauto-sccs-on-all-toplevs")
277 opt_AutoSccsOnExportedToplevs   = lookUp  SLIT("-fauto-sccs-on-exported-toplevs")
278 opt_AutoSccsOnIndividualCafs    = lookUp  SLIT("-fauto-sccs-on-individual-cafs")
279 opt_CompilingGhcInternals       = maybeToBool maybe_CompilingGhcInternals
280 maybe_CompilingGhcInternals     = lookup_str "-fcompiling-ghc-internals="
281 opt_D_dump_absC                 = lookUp  SLIT("-ddump-absC")
282 opt_D_dump_asm                  = lookUp  SLIT("-ddump-asm")
283 opt_D_dump_deforest             = lookUp  SLIT("-ddump-deforest")
284 opt_D_dump_deriv                = lookUp  SLIT("-ddump-deriv")
285 opt_D_dump_ds                   = lookUp  SLIT("-ddump-ds")
286 opt_D_dump_flatC                = lookUp  SLIT("-ddump-flatC")
287 opt_D_dump_occur_anal           = lookUp  SLIT("-ddump-occur-anal")
288 opt_D_dump_rdr                  = lookUp  SLIT("-ddump-rdr")
289 opt_D_dump_realC                = lookUp  SLIT("-ddump-realC")
290 opt_D_dump_rn                   = lookUp  SLIT("-ddump-rn")
291 opt_D_dump_simpl                = lookUp  SLIT("-ddump-simpl")
292 opt_D_dump_simpl_iterations     = lookUp  SLIT("-ddump-simpl-iterations")
293 opt_D_dump_spec                 = lookUp  SLIT("-ddump-spec")
294 opt_D_dump_stg                  = lookUp  SLIT("-ddump-stg")
295 opt_D_dump_stranal              = lookUp  SLIT("-ddump-stranal")
296 opt_D_dump_tc                   = lookUp  SLIT("-ddump-tc")
297 opt_D_show_passes               = lookUp  SLIT("-dshow-passes")
298 opt_D_show_rn_trace             = lookUp  SLIT("-dshow-rn-trace")
299 opt_D_simplifier_stats          = lookUp  SLIT("-dsimplifier-stats")
300 opt_D_source_stats              = lookUp  SLIT("-dsource-stats")
301 opt_D_verbose_core2core         = lookUp  SLIT("-dverbose-simpl")
302 opt_D_verbose_stg2stg           = lookUp  SLIT("-dverbose-stg")
303 opt_DoCoreLinting               = lookUp  SLIT("-dcore-lint")
304 opt_DoStgLinting                = lookUp  SLIT("-dstg-lint")
305 opt_DoSemiTagging               = lookUp  SLIT("-fsemi-tagging")
306 opt_DoTickyProfiling            = lookUp  SLIT("-fticky-ticky")
307 opt_DoEtaReduction              = lookUp  SLIT("-fdo-eta-reduction")
308 opt_EnsureSplittableC           = lookUp  SLIT("-fglobalise-toplev-names")
309 opt_FoldrBuildOn                = lookUp  SLIT("-ffoldr-build-on")
310 opt_FoldrBuildTrace             = lookUp  SLIT("-ffoldr-build-trace")
311 opt_ForConcurrent               = lookUp  SLIT("-fconcurrent")
312 opt_GranMacros                  = lookUp  SLIT("-fgransim")
313 opt_GlasgowExts                 = lookUp  SLIT("-fglasgow-exts")
314 --UNUSED:opt_Haskell_1_3        = lookUp  SLIT("-fhaskell-1.3")
315 opt_HiMap                       = lookup_str "-himap="  -- file saying where to look for .hi files
316 opt_IgnoreIfacePragmas          = lookUp  SLIT("-fignore-interface-pragmas")
317 opt_IgnoreStrictnessPragmas     = lookUp  SLIT("-fignore-strictness-pragmas")
318 opt_IrrefutableEverything       = lookUp  SLIT("-firrefutable-everything")
319 opt_IrrefutableTuples           = lookUp  SLIT("-firrefutable-tuples")
320 opt_NoImplicitPrelude           = lookUp  SLIT("-fno-implicit-prelude")
321 opt_NumbersStrict               = lookUp  SLIT("-fnumbers-strict")
322 opt_OmitBlackHoling             = lookUp  SLIT("-dno-black-holing")
323 opt_OmitDefaultInstanceMethods  = lookUp  SLIT("-fomit-default-instance-methods")
324 opt_OmitInterfacePragmas        = lookUp  SLIT("-fomit-interface-pragmas")
325 opt_PprStyle_All                = lookUp  SLIT("-dppr-all")
326 opt_PprStyle_Debug              = lookUp  SLIT("-dppr-debug")
327 opt_PprStyle_User               = lookUp  SLIT("-dppr-user")
328 opt_PprUserLength               = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
329 opt_ProduceC                    = lookup_str "-C="
330 opt_ProduceS                    = lookup_str "-S="
331 opt_ProduceHi                   = lookup_str "-hifile=" -- the one to produce this time 
332 opt_ReportWhyUnfoldingsDisallowed= lookUp SLIT("-freport-disallowed-unfoldings")
333 opt_SccProfilingOn              = lookUp  SLIT("-fscc-profiling")
334 opt_ShowImportSpecs             = lookUp  SLIT("-fshow-import-specs")
335 opt_ShowPragmaNameErrs          = lookUp  SLIT("-fshow-pragma-name-errs")
336 opt_SigsRequired                = lookUp  SLIT("-fsignatures-required")
337 opt_SourceUnchanged             = lookUp  SLIT("-fsource-unchanged")
338 opt_SpecialiseAll               = lookUp  SLIT("-fspecialise-all")
339 opt_SpecialiseImports           = lookUp  SLIT("-fspecialise-imports")
340 opt_SpecialiseOverloaded        = lookUp  SLIT("-fspecialise-overloaded")
341 opt_SpecialiseTrace             = lookUp  SLIT("-ftrace-specialisation")
342 opt_SpecialiseUnboxed           = lookUp  SLIT("-fspecialise-unboxed")
343 opt_StgDoLetNoEscapes           = lookUp  SLIT("-flet-no-escape")
344 opt_ReturnInRegsThreshold       = lookup_int "-freturn-in-regs-threshold"
345 opt_SccGroup                    = lookup_str "-G="
346 opt_Verbose                     = lookUp  SLIT("-v")
347
348 opt_InterfaceUnfoldThreshold    = lookup_def_int "-funfolding-interface-threshold" iNTERFACE_UNFOLD_THRESHOLD
349 opt_UnfoldingCreationThreshold  = lookup_def_int "-funfolding-creation-threshold"  uNFOLDING_CREATION_THRESHOLD
350 opt_UnfoldingUseThreshold       = lookup_def_int "-funfolding-use-threshold"       uNFOLDING_USE_THRESHOLD
351 opt_UnfoldingConDiscount        = lookup_def_int "-funfolding-con-discount"        uNFOLDING_CON_DISCOUNT_WEIGHT
352                         
353 opt_LiberateCaseThreshold       = lookup_def_int "-fliberate-case-threshold"       lIBERATE_CASE_THRESHOLD
354 opt_WarnNameShadowing           = lookUp  SLIT("-fwarn-name-shadowing")
355 opt_WarnIncompletePatterns      = not (lookUp  SLIT("-fno-warn-incomplete-patterns"))
356 opt_WarnOverlappedPatterns      = not (lookUp  SLIT("-fno-warn-overlapped-patterns"))
357 opt_WarnUnusedNames             = lookUp  SLIT("-fwarn-unused-names")
358 opt_PruneTyDecls                = not (lookUp SLIT("-fno-prune-tydecls"))
359 opt_PruneInstDecls              = not (lookUp SLIT("-fno-prune-instdecls"))
360 opt_D_show_unused_imports       = lookUp SLIT("-dshow-unused-imports")
361 opt_D_show_rn_stats             = lookUp SLIT("-dshow-rn-stats")
362
363 -- opt_UnfoldingOverrideThreshold       = lookup_int "-funfolding-override-threshold"
364 \end{code}
365
366
367 \begin{code}
368 all_toplev_ids_visible :: Bool
369 all_toplev_ids_visible = 
370   not opt_OmitInterfacePragmas ||  -- Pragmas can make them visible
371   opt_EnsureSplittableC        ||  -- Splitting requires visiblilty
372   opt_AutoSccsOnAllToplevs         -- ditto for profiling 
373                                    -- (ToDo: fix up the auto-annotation
374                                    -- pass in the desugarer to avoid having
375                                    -- to do this)
376
377 \end{code}
378
379
380
381 \begin{code}
382 classifyOpts :: ([CoreToDo],    -- Core-to-Core processing spec
383                  [StgToDo])     -- STG-to-STG   processing spec
384
385 classifyOpts = sep argv [] [] -- accumulators...
386   where
387     sep :: [FAST_STRING]                         -- cmd-line opts (input)
388         -> [CoreToDo] -> [StgToDo]       -- to_do accumulators
389         -> ([CoreToDo], [StgToDo])       -- result
390
391     sep [] core_td stg_td -- all done!
392       = (reverse core_td, reverse stg_td)
393
394 #       define CORE_TD(to_do) sep opts (to_do:core_td) stg_td
395 #       define STG_TD(to_do)  sep opts core_td (to_do:stg_td)
396 #       define IGNORE_ARG()   sep opts core_td stg_td
397
398     sep (opt1:opts) core_td stg_td
399       =
400         case (_UNPK_ opt1) of -- the non-"just match a string" options are at the end...
401
402           ',' : _       -> IGNORE_ARG() -- it is for the parser
403
404           "-fsimplify"  -> -- gather up SimplifierSwitches specially...
405                            simpl_sep opts defaultSimplSwitches core_td stg_td
406
407           "-fcalc-inlinings1"-> CORE_TD(CoreDoCalcInlinings1)
408           "-fcalc-inlinings2"-> CORE_TD(CoreDoCalcInlinings2)
409           "-ffloat-inwards"  -> CORE_TD(CoreDoFloatInwards)
410           "-ffull-laziness"  -> CORE_TD(CoreDoFullLaziness)
411           "-fliberate-case"  -> CORE_TD(CoreLiberateCase)
412           "-fprint-core"     -> CORE_TD(CoreDoPrintCore)
413           "-fstatic-args"    -> CORE_TD(CoreDoStaticArgs)
414           "-fstrictness"     -> CORE_TD(CoreDoStrictness)
415           "-fspecialise"     -> CORE_TD(CoreDoSpecialising)
416           "-fdeforest"       -> CORE_TD(CoreDoDeforest)
417           "-ffoldr-build-worker-wrapper"  -> CORE_TD(CoreDoFoldrBuildWorkerWrapper)
418           "-ffoldr-build-ww-anal"  -> CORE_TD(CoreDoFoldrBuildWWAnal)
419
420           "-fstg-static-args" -> STG_TD(StgDoStaticArgs)
421           "-fupdate-analysis" -> STG_TD(StgDoUpdateAnalysis)
422           "-dstg-stats"       -> STG_TD(D_stg_stats)
423           "-flambda-lift"     -> STG_TD(StgDoLambdaLift)
424           "-fmassage-stg-for-profiling" -> STG_TD(StgDoMassageForProfiling)
425
426           _ -> -- NB: the driver is really supposed to handle bad options
427                IGNORE_ARG()
428
429     ----------------
430
431     simpl_sep :: [FAST_STRING]      -- cmd-line opts (input)
432         -> [SimplifierSwitch]       -- simplifier-switch accumulator
433         -> [CoreToDo] -> [StgToDo]  -- to_do accumulators
434         -> ([CoreToDo], [StgToDo])  -- result
435
436         -- "simpl_sep" tailcalls "sep" once it's seen one set
437         -- of SimplifierSwitches for a CoreDoSimplify.
438
439 #ifdef DEBUG
440     simpl_sep input@[] simpl_sw core_td stg_td
441       = panic "simpl_sep []"
442 #endif
443
444         -- The SimplifierSwitches should be delimited by "[" and "]".
445
446     simpl_sep (opt1:opts) simpl_sw core_td stg_td
447       = case (_UNPK_ opt1) of
448           "[" -> simpl_sep opts simpl_sw core_td stg_td
449           "]" -> let
450                     this_simpl = CoreDoSimplify (isAmongSimpl simpl_sw)
451                  in
452                  sep opts (this_simpl : core_td) stg_td
453
454 #         define SIMPL_SW(sw) simpl_sep opts (sw:simpl_sw) core_td stg_td
455
456           -- the non-"just match a string" options are at the end...
457           "-fshow-simplifier-progress"      -> SIMPL_SW(ShowSimplifierProgress)
458           "-fcode-duplication-ok"           -> SIMPL_SW(SimplOkToDupCode)
459           "-ffloat-lets-exposing-whnf"      -> SIMPL_SW(SimplFloatLetsExposingWHNF)
460           "-ffloat-primops-ok"              -> SIMPL_SW(SimplOkToFloatPrimOps)
461           "-falways-float-lets-from-lets"   -> SIMPL_SW(SimplAlwaysFloatLetsFromLets)
462           "-fdo-case-elim"                  -> SIMPL_SW(SimplDoCaseElim)
463           "-fdo-lambda-eta-expansion"       -> SIMPL_SW(SimplDoLambdaEtaExpansion)
464           "-fdo-foldr-build"                -> SIMPL_SW(SimplDoFoldrBuild)
465           "-fdo-not-fold-back-append"       -> SIMPL_SW(SimplDontFoldBackAppend)
466           "-fdo-arity-expand"               -> SIMPL_SW(SimplDoArityExpand)
467           "-fdo-inline-foldr-build"         -> SIMPL_SW(SimplDoInlineFoldrBuild)
468           "-freuse-con"                     -> SIMPL_SW(SimplReuseCon)
469           "-fcase-of-case"                  -> SIMPL_SW(SimplCaseOfCase)
470           "-fcase-merge"                    -> SIMPL_SW(SimplCaseMerge)
471           "-flet-to-case"                   -> SIMPL_SW(SimplLetToCase)
472           "-fpedantic-bottoms"              -> SIMPL_SW(SimplPedanticBottoms)
473           "-fkeep-spec-pragma-ids"          -> SIMPL_SW(KeepSpecPragmaIds)
474           "-fkeep-unused-bindings"          -> SIMPL_SW(KeepUnusedBindings)
475           "-fmay-delete-conjurable-ids"     -> SIMPL_SW(SimplMayDeleteConjurableIds)
476           "-fessential-unfoldings-only"     -> SIMPL_SW(EssentialUnfoldingsOnly)
477           "-fignore-inline-pragma"          -> SIMPL_SW(IgnoreINLINEPragma)
478           "-fno-let-from-case"              -> SIMPL_SW(SimplNoLetFromCase)
479           "-fno-let-from-app"               -> SIMPL_SW(SimplNoLetFromApp)
480           "-fno-let-from-strict-let"        -> SIMPL_SW(SimplNoLetFromStrictLet)
481
482           o | starts_with_msi  -> SIMPL_SW(MaxSimplifierIterations (read after_msi))
483            where
484             maybe_msi           = startsWith "-fmax-simplifier-iterations"   o
485             starts_with_msi     = maybeToBool maybe_msi
486             (Just after_msi)    = maybe_msi
487
488           _ -> -- NB: the driver is really supposed to handle bad options
489                simpl_sep opts simpl_sw core_td stg_td
490 \end{code}
491
492 %************************************************************************
493 %*                                                                      *
494 \subsection{Switch ordering}
495 %*                                                                      *
496 %************************************************************************
497
498 In spite of the @Produce*@ and @SccGroup@ constructors, these things
499 behave just like enumeration types.
500
501 \begin{code}
502 instance Eq SimplifierSwitch where
503     a == b = tagOf_SimplSwitch a _EQ_ tagOf_SimplSwitch b
504
505 instance Ord SimplifierSwitch where
506     a <  b  = tagOf_SimplSwitch a _LT_ tagOf_SimplSwitch b
507     a <= b  = tagOf_SimplSwitch a _LE_ tagOf_SimplSwitch b
508
509 tagOf_SimplSwitch SimplOkToDupCode              =(ILIT(0) :: FAST_INT)
510 tagOf_SimplSwitch SimplFloatLetsExposingWHNF    = ILIT(1)
511 tagOf_SimplSwitch SimplOkToFloatPrimOps         = ILIT(2)
512 tagOf_SimplSwitch SimplAlwaysFloatLetsFromLets  = ILIT(3)
513 tagOf_SimplSwitch SimplDoCaseElim               = ILIT(4)
514 tagOf_SimplSwitch SimplReuseCon                 = ILIT(5)
515 tagOf_SimplSwitch SimplCaseOfCase               = ILIT(6)
516 tagOf_SimplSwitch SimplLetToCase                = ILIT(7)
517 tagOf_SimplSwitch SimplMayDeleteConjurableIds   = ILIT(9)
518 tagOf_SimplSwitch SimplPedanticBottoms          = ILIT(10)
519 tagOf_SimplSwitch SimplDoArityExpand            = ILIT(11)
520 tagOf_SimplSwitch SimplDoFoldrBuild             = ILIT(12)
521 tagOf_SimplSwitch SimplDoInlineFoldrBuild       = ILIT(14)
522 tagOf_SimplSwitch IgnoreINLINEPragma            = ILIT(15)
523 tagOf_SimplSwitch SimplDoLambdaEtaExpansion     = ILIT(16)
524 tagOf_SimplSwitch EssentialUnfoldingsOnly       = ILIT(19)
525 tagOf_SimplSwitch ShowSimplifierProgress        = ILIT(20)
526 tagOf_SimplSwitch (MaxSimplifierIterations _)   = ILIT(21)
527 tagOf_SimplSwitch KeepSpecPragmaIds             = ILIT(25)
528 tagOf_SimplSwitch KeepUnusedBindings            = ILIT(26)
529 tagOf_SimplSwitch SimplNoLetFromCase            = ILIT(27)
530 tagOf_SimplSwitch SimplNoLetFromApp             = ILIT(28)
531 tagOf_SimplSwitch SimplNoLetFromStrictLet       = ILIT(29)
532 tagOf_SimplSwitch SimplDontFoldBackAppend       = ILIT(30)
533 tagOf_SimplSwitch SimplCaseMerge                = ILIT(31)
534 tagOf_SimplSwitch SimplCaseScrutinee            = ILIT(32)
535
536 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
537
538 tagOf_SimplSwitch _ = panic# "tagOf_SimplSwitch"
539
540 lAST_SIMPL_SWITCH_TAG = IBOX(tagOf_SimplSwitch SimplCaseScrutinee)
541 \end{code}
542
543 %************************************************************************
544 %*                                                                      *
545 \subsection{Switch lookup}
546 %*                                                                      *
547 %************************************************************************
548
549 \begin{code}
550 #if __GLASGOW_HASKELL__ == 201
551 # define ARRAY      Array
552 # define LIFT       GHCbase.Lift
553 # define SET_TO     =:
554 (=:) a b = (a,b)
555 #elif __GLASGOW_HASKELL__ >= 202
556 # define ARRAY      Array
557 # define LIFT       Lift
558 # define SET_TO     =:
559 (=:) a b = (a,b)
560 #else
561 # define ARRAY      _Array
562 # define LIFT       _Lift
563 # define SET_TO     :=
564 #endif
565
566 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
567
568 isAmongSimpl on_switches                -- Switches mentioned later occur *earlier*
569                                         -- in the list; defaults right at the end.
570   = let
571         tidied_on_switches = foldl rm_dups [] on_switches
572                 -- The fold*l* ensures that we keep the latest switches;
573                 -- ie the ones that occur earliest in the list.
574
575         sw_tbl :: Array Int SwitchResult
576
577         sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
578                         all_undefined)
579                  // defined_elems
580
581         all_undefined = [ i SET_TO SwBool False | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
582
583         defined_elems = map mk_assoc_elem tidied_on_switches
584     in
585     -- (avoid some unboxing, bounds checking, and other horrible things:)
586     case sw_tbl of { ARRAY bounds_who_needs_'em stuff ->
587     \ switch ->
588         case (indexArray# stuff (tagOf_SimplSwitch switch)) of
589           LIFT v -> v
590     }
591   where
592     mk_assoc_elem k@(MaxSimplifierIterations lvl)       = IBOX(tagOf_SimplSwitch k) SET_TO SwInt lvl
593
594     mk_assoc_elem k = IBOX(tagOf_SimplSwitch k) SET_TO SwBool   True -- I'm here, Mom!
595
596     -- cannot have duplicates if we are going to use the array thing
597     rm_dups switches_so_far switch
598       = if switch `is_elem` switches_so_far
599         then switches_so_far
600         else switch : switches_so_far
601       where
602         sw `is_elem` []     = False
603         sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) _EQ_ (tagOf_SimplSwitch s)
604                             || sw `is_elem` ss
605 \end{code}
606
607 Default settings for simplifier switches
608
609 \begin{code}
610 defaultSimplSwitches = [MaxSimplifierIterations         1
611                        ]
612 \end{code}
613
614 %************************************************************************
615 %*                                                                      *
616 \subsection{Misc functions for command-line options}
617 %*                                                                      *
618 %************************************************************************
619
620
621 \begin{code}
622 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
623
624 switchIsOn lookup_fn switch
625   = case (lookup_fn switch) of
626       SwBool False -> False
627       _            -> True
628
629 stringSwitchSet :: (switch -> SwitchResult)
630                 -> (FAST_STRING -> switch)
631                 -> Maybe FAST_STRING
632
633 stringSwitchSet lookup_fn switch
634   = case (lookup_fn (switch (panic "stringSwitchSet"))) of
635       SwString str -> Just str
636       _            -> Nothing
637
638 intSwitchSet :: (switch -> SwitchResult)
639              -> (Int -> switch)
640              -> Maybe Int
641
642 intSwitchSet lookup_fn switch
643   = case (lookup_fn (switch (panic "intSwitchSet"))) of
644       SwInt int -> Just int
645       _         -> Nothing
646 \end{code}