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