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