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