[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / compiler / main / CmdLineOpts.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1996-98
3 %
4 \section[CmdLineOpts]{Things to do with command-line options}
5
6 \begin{code}
7 module CmdLineOpts (
8         CoreToDo(..),
9         SimplifierSwitch(..),
10         StgToDo(..),
11         SwitchResult(..),
12         classifyOpts,
13
14         intSwitchSet,
15         switchIsOn,
16
17         opt_AllStrict,
18         opt_AllowOverlappingInstances,
19         opt_AllowUndecidableInstances,
20         opt_AutoSccsOnAllToplevs,
21         opt_AutoSccsOnExportedToplevs,
22         opt_AutoSccsOnIndividualCafs,
23         opt_D_dump_absC,
24         opt_D_dump_asm,
25         opt_D_dump_deriv,
26         opt_D_dump_ds,
27         opt_D_dump_flatC,
28         opt_D_dump_inlinings,
29         opt_D_dump_foreign,
30         opt_D_dump_occur_anal,
31         opt_D_dump_rdr,
32         opt_D_dump_realC,
33         opt_D_dump_rn,
34         opt_D_dump_simpl,
35         opt_D_dump_simpl_iterations,
36         opt_D_dump_spec,
37         opt_D_dump_stg,
38         opt_D_dump_stranal,
39         opt_D_dump_tc,
40         opt_D_show_passes,
41         opt_D_show_rn_trace,
42         opt_D_show_rn_imports,
43         opt_D_simplifier_stats,
44         opt_D_source_stats,
45         opt_D_verbose_core2core,
46         opt_D_verbose_stg2stg,
47         opt_DictsStrict,
48         opt_DoCoreLinting,
49         opt_DoStgLinting,
50         opt_DoSemiTagging,
51         opt_DoEtaReduction,
52         opt_DoTickyProfiling,
53         opt_EmitCExternDecls,
54         opt_EnsureSplittableC,
55         opt_FoldrBuildOn,
56         opt_GlasgowExts,
57         opt_GranMacros,
58         opt_HiMap,
59         opt_HiVersion,
60         opt_IgnoreIfacePragmas,
61         opt_IrrefutableTuples,
62         opt_LiberateCaseThreshold,
63         opt_MaxContextReductionDepth,
64         opt_MultiParamClasses,
65         opt_NoHiCheck,
66         opt_NoImplicitPrelude,
67         opt_NoPreInlining,
68         opt_NumbersStrict,
69         opt_OmitBlackHoling,
70         opt_OmitInterfacePragmas,
71         opt_PprStyle_NoPrags,
72         opt_PprStyle_Debug,
73         opt_PprUserLength,
74         opt_ProduceC,
75         opt_ProduceHi,
76         opt_ProduceS,
77         opt_ProduceExportCStubs,
78         opt_ProduceExportHStubs,
79         opt_ReportCompile,
80         opt_SccGroup,
81         opt_SccProfilingOn,
82         opt_SourceUnchanged,
83         opt_StgDoLetNoEscapes,
84         opt_Parallel,
85
86         opt_InterfaceUnfoldThreshold,
87         opt_UnfoldCasms,
88         opt_UnfoldingCreationThreshold,
89         opt_UnfoldingConDiscount,
90         opt_UnfoldingUseThreshold,
91         opt_UnfoldingKeenessFactor,
92
93         opt_Verbose,
94         opt_WarnNameShadowing,
95         opt_WarnUnusedMatches,
96         opt_WarnUnusedBinds,
97         opt_WarnUnusedImports,
98         opt_WarnIncompletePatterns,
99         opt_WarnOverlappingPatterns,
100         opt_WarnSimplePatterns,
101         opt_WarnMissingMethods,
102         opt_WarnDuplicateExports,
103         opt_WarnHiShadows,
104         opt_WarnMissingSigs,
105         opt_PruneTyDecls, opt_PruneInstDecls,
106         opt_D_show_rn_stats
107     ) where
108
109 #include "HsVersions.h"
110
111 import Array    ( array, (//) )
112 import GlaExts
113 import Argv
114 import Constants        -- Default values for some flags
115
116 import Maybes           ( assocMaybe, firstJust, maybeToBool )
117 import Util             ( startsWith, panic, panic# )
118
119 #if __GLASGOW_HASKELL__ < 301
120 import ArrBase  ( Array(..) )
121 #else
122 import PrelArr  ( Array(..) )
123 #endif
124 \end{code}
125
126 A command-line {\em switch} is (generally) either on or off; e.g., the
127 ``verbose'' (-v) switch is either on or off.  (The \tr{-G<group>}
128 switch is an exception; it's set to a string, or nothing.)
129
130 A list of {\em ToDo}s is things to be done in a particular part of
131 processing.  A (fictitious) example for the Core-to-Core simplifier
132 might be: run the simplifier, then run the strictness analyser, then
133 run the simplifier again (three ``todos'').
134
135 There are three ``to-do processing centers'' at the moment.  In the
136 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
137 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
138 (\tr{simplStg/SimplStg.lhs}).
139
140 %************************************************************************
141 %*                                                                      *
142 \subsection{Datatypes associated with command-line options}
143 %*                                                                      *
144 %************************************************************************
145
146 \begin{code}
147 data SwitchResult
148   = SwBool      Bool            -- on/off
149   | SwString    FAST_STRING     -- nothing or a String
150   | SwInt       Int             -- nothing or an Int
151 \end{code}
152
153 \begin{code}
154 data CoreToDo           -- These are diff core-to-core passes,
155                         -- which may be invoked in any order,
156                         -- as many times as you like.
157
158   = CoreDoSimplify      -- The core-to-core simplifier.
159         (SimplifierSwitch -> SwitchResult)
160                         -- Each run of the simplifier can take a different
161                         -- set of simplifier-specific flags.
162   | CoreDoCalcInlinings1
163   | CoreDoCalcInlinings2
164   | CoreDoFloatInwards
165   | CoreDoFullLaziness
166   | CoreLiberateCase
167   | CoreDoPrintCore
168   | CoreDoStaticArgs
169   | CoreDoStrictness
170   | CoreDoSpecialising
171   | CoreDoFoldrBuildWorkerWrapper
172   | CoreDoFoldrBuildWWAnal
173 \end{code}
174
175 \begin{code}
176 data StgToDo
177   = StgDoStaticArgs
178   | StgDoUpdateAnalysis
179   | StgDoLambdaLift
180   | StgDoMassageForProfiling  -- should be (next to) last
181   -- There's also setStgVarInfo, but its absolute "lastness"
182   -- is so critical that it is hardwired in (no flag).
183   | D_stg_stats
184 \end{code}
185
186 \begin{code}
187 data SimplifierSwitch
188   = SimplOkToDupCode
189   | SimplFloatLetsExposingWHNF
190   | SimplOkToFloatPrimOps
191   | SimplAlwaysFloatLetsFromLets
192   | SimplDoCaseElim
193   | SimplCaseOfCase
194   | SimplLetToCase
195   | SimplMayDeleteConjurableIds
196   | SimplPedanticBottoms -- see Simplifier for an explanation
197   | SimplDoArityExpand   -- expand arity of bindings
198   | SimplDoFoldrBuild    -- This is the per-simplification flag;
199                          -- see also FoldrBuildOn, used elsewhere
200                          -- in the compiler.
201   | SimplDoInlineFoldrBuild
202                          -- inline foldr/build (*after* f/b rule is used)
203
204   | IgnoreINLINEPragma
205   | SimplDoLambdaEtaExpansion
206
207   | EssentialUnfoldingsOnly -- never mind the thresholds, only
208                             -- do unfoldings that *must* be done
209                             -- (to saturate constructors and primitives)
210
211   | MaxSimplifierIterations Int
212
213   | SimplNoLetFromCase      -- used when turning off floating entirely
214   | SimplNoLetFromApp       -- (for experimentation only) WDP 95/10
215   | SimplNoLetFromStrictLet
216
217   | SimplCaseMerge
218   | SimplPleaseClone
219 \end{code}
220
221 %************************************************************************
222 %*                                                                      *
223 \subsection{Classifying command-line options}
224 %*                                                                      *
225 %************************************************************************
226
227 \begin{code}
228 lookUp           :: FAST_STRING -> Bool
229 lookup_int       :: String -> Maybe Int
230 lookup_def_int   :: String -> Int -> Int
231 lookup_def_float :: String -> Float -> Float
232 lookup_str       :: String -> Maybe String
233
234 lookUp     sw = maybeToBool (assoc_opts sw)
235         
236 lookup_str sw = firstJust (map (startsWith sw) unpacked_opts)
237
238 lookup_int sw = case (lookup_str sw) of
239                   Nothing -> Nothing
240                   Just xx -> Just (read xx)
241
242 lookup_def_int sw def = case (lookup_str sw) of
243                             Nothing -> def              -- Use default
244                             Just xx -> read xx
245
246 lookup_def_float sw def = case (lookup_str sw) of
247                             Nothing -> def              -- Use default
248                             Just xx -> read xx
249
250 assoc_opts    = assocMaybe [ (a, True) | a <- argv ]
251 unpacked_opts = map _UNPK_ argv
252
253 {-
254  Putting the compiler options into temporary at-files
255  may turn out to be necessary later on if we turn hsc into
256  a pure Win32 application where I think there's a command-line
257  length limit of 255. unpacked_opts understands the @ option.
258
259 assoc_opts    = assocMaybe [ (_PK_ a, True) | a <- unpacked_opts ]
260
261 unpacked_opts :: [String]
262 unpacked_opts =
263   concat $
264   map (expandAts) $
265   map _UNPK_ argv
266   where
267    expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
268    expandAts l = [l]
269 -}
270 \end{code}
271
272 \begin{code}
273 opt_AllStrict                   = lookUp  SLIT("-fall-strict")
274 opt_AllowOverlappingInstances   = lookUp  SLIT("-fallow-overlapping-instances")
275 opt_AllowUndecidableInstances   = lookUp  SLIT("-fallow-undecidable-instances")
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_D_dump_absC                 = lookUp  SLIT("-ddump-absC")
280 opt_D_dump_asm                  = lookUp  SLIT("-ddump-asm")
281 opt_D_dump_deriv                = lookUp  SLIT("-ddump-deriv")
282 opt_D_dump_ds                   = lookUp  SLIT("-ddump-ds")
283 opt_D_dump_flatC                = lookUp  SLIT("-ddump-flatC")
284 opt_D_dump_inlinings            = lookUp  SLIT("-ddump-inlinings")
285 opt_D_dump_foreign              = lookUp  SLIT("-ddump-foreign-stubs")
286 opt_D_dump_occur_anal           = lookUp  SLIT("-ddump-occur-anal")
287 opt_D_dump_rdr                  = lookUp  SLIT("-ddump-rdr")
288 opt_D_dump_realC                = lookUp  SLIT("-ddump-realC")
289 opt_D_dump_rn                   = lookUp  SLIT("-ddump-rn")
290 opt_D_dump_simpl                = lookUp  SLIT("-ddump-simpl")
291 opt_D_dump_simpl_iterations     = lookUp  SLIT("-ddump-simpl-iterations")
292 opt_D_dump_spec                 = lookUp  SLIT("-ddump-spec")
293 opt_D_dump_stg                  = lookUp  SLIT("-ddump-stg")
294 opt_D_dump_stranal              = lookUp  SLIT("-ddump-stranal")
295 opt_D_dump_tc                   = lookUp  SLIT("-ddump-tc")
296 opt_D_show_passes               = lookUp  SLIT("-dshow-passes")
297 opt_D_show_rn_trace             = lookUp  SLIT("-dshow-rn-trace")
298 opt_D_show_rn_imports           = lookUp  SLIT("-dshow-rn-imports")
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_DictsStrict                 = lookUp  SLIT("-fdicts-strict")
304 opt_DoCoreLinting               = lookUp  SLIT("-dcore-lint")
305 opt_DoStgLinting                = lookUp  SLIT("-dstg-lint")
306 opt_DoEtaReduction              = lookUp  SLIT("-fdo-eta-reduction")
307 opt_DoSemiTagging               = lookUp  SLIT("-fsemi-tagging")
308 opt_DoTickyProfiling            = lookUp  SLIT("-fticky-ticky")
309 opt_EmitCExternDecls            = lookUp  SLIT("-femit-extern-decls")
310 opt_EnsureSplittableC           = lookUp  SLIT("-fglobalise-toplev-names")
311 opt_FoldrBuildOn                = lookUp  SLIT("-ffoldr-build-on")
312 opt_GranMacros                  = lookUp  SLIT("-fgransim")
313 opt_GlasgowExts                 = lookUp  SLIT("-fglasgow-exts")
314 opt_HiMap                       = lookup_str "-himap="       -- file saying where to look for .hi files
315 opt_HiVersion                   = lookup_def_int "-fhi-version=" 0 -- what version we're compiling.
316 opt_IgnoreIfacePragmas          = lookUp  SLIT("-fignore-interface-pragmas")
317 opt_IrrefutableTuples           = lookUp  SLIT("-firrefutable-tuples")
318 opt_MaxContextReductionDepth    = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
319 opt_MultiParamClasses           = opt_GlasgowExts
320 opt_NoHiCheck                   = lookUp  SLIT("-fno-hi-version-check")
321 opt_NoImplicitPrelude           = lookUp  SLIT("-fno-implicit-prelude")
322 opt_NoPreInlining               = lookUp  SLIT("-fno-pre-inlining")
323 opt_NumbersStrict               = lookUp  SLIT("-fnumbers-strict")
324 opt_OmitBlackHoling             = lookUp  SLIT("-dno-black-holing")
325 opt_OmitInterfacePragmas        = lookUp  SLIT("-fomit-interface-pragmas")
326 opt_PprStyle_NoPrags            = lookUp  SLIT("-dppr-noprags")
327 opt_PprStyle_Debug              = lookUp  SLIT("-dppr-debug")
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_ProduceExportCStubs         = lookup_str "-F="
332 opt_ProduceExportHStubs         = lookup_str "-FH="
333 opt_ProduceHi                   = lookup_str "-hifile=" -- the one to produce this time 
334 opt_ReportCompile                = lookUp SLIT("-freport-compile")
335 opt_SccProfilingOn              = lookUp  SLIT("-fscc-profiling")
336 opt_SourceUnchanged             = lookUp  SLIT("-fsource-unchanged")
337 opt_StgDoLetNoEscapes           = lookUp  SLIT("-flet-no-escape")
338 opt_Parallel                    = lookUp  SLIT("-fparallel")
339 opt_SccGroup                    = lookup_str "-G="
340 opt_Verbose                     = lookUp  SLIT("-v")
341
342 opt_UnfoldCasms                 = lookUp SLIT("-funfold-casms-in-hi-file")
343 opt_InterfaceUnfoldThreshold    = lookup_def_int "-funfolding-interface-threshold" iNTERFACE_UNFOLD_THRESHOLD
344 opt_UnfoldingCreationThreshold  = lookup_def_int "-funfolding-creation-threshold"  uNFOLDING_CREATION_THRESHOLD
345 opt_UnfoldingUseThreshold       = lookup_def_int "-funfolding-use-threshold"       uNFOLDING_USE_THRESHOLD
346 opt_UnfoldingConDiscount        = lookup_def_int "-funfolding-con-discount"        uNFOLDING_CON_DISCOUNT_WEIGHT
347                         
348 opt_LiberateCaseThreshold       = lookup_def_int "-fliberate-case-threshold"       lIBERATE_CASE_THRESHOLD
349 opt_UnfoldingKeenessFactor      = lookup_def_float "-funfolding-keeness-factor"    uNFOLDING_KEENESS_FACTOR
350 opt_WarnNameShadowing           = lookUp  SLIT("-fwarn-name-shadowing")
351 opt_WarnHiShadows               = lookUp  SLIT("-fwarn-hi-shadowing")
352 opt_WarnIncompletePatterns      = lookUp  SLIT("-fwarn-incomplete-patterns")
353 opt_WarnOverlappingPatterns     = lookUp  SLIT("-fwarn-overlapping-patterns")
354 opt_WarnSimplePatterns          = lookUp  SLIT("-fwarn-simple-patterns")
355 opt_WarnUnusedMatches           = lookUp  SLIT("-fwarn-unused-matches")
356 opt_WarnUnusedBinds             = lookUp  SLIT("-fwarn-unused-binds")
357 opt_WarnUnusedImports           = lookUp  SLIT("-fwarn-unused-imports")
358 opt_WarnMissingMethods          = lookUp  SLIT("-fwarn-missing-methods")
359 opt_WarnDuplicateExports        = lookUp  SLIT("-fwarn-duplicate-exports")
360 opt_WarnMissingSigs             = lookUp  SLIT("-fwarn-missing-signatures")
361 opt_PruneTyDecls                = not (lookUp SLIT("-fno-prune-tydecls"))
362 opt_PruneInstDecls              = not (lookUp SLIT("-fno-prune-instdecls"))
363 opt_D_show_rn_stats             = lookUp SLIT("-dshow-rn-stats")
364
365 -- opt_UnfoldingOverrideThreshold       = lookup_int "-funfolding-override-threshold"
366 \end{code}
367
368 \begin{code}
369 classifyOpts :: ([CoreToDo],    -- Core-to-Core processing spec
370                  [StgToDo])     -- STG-to-STG   processing spec
371
372 classifyOpts = sep argv [] [] -- accumulators...
373   where
374     sep :: [FAST_STRING]                 -- cmd-line opts (input)
375         -> [CoreToDo] -> [StgToDo]       -- to_do accumulators
376         -> ([CoreToDo], [StgToDo])       -- result
377
378     sep [] core_td stg_td -- all done!
379       = (reverse core_td, reverse stg_td)
380
381 #       define CORE_TD(to_do) sep opts (to_do:core_td) stg_td
382 #       define STG_TD(to_do)  sep opts core_td (to_do:stg_td)
383
384     sep (opt1:opts) core_td stg_td
385       = case (_UNPK_ opt1) of -- the non-"just match a string" options are at the end...
386           ',' : _       -> sep opts core_td stg_td -- it is for the parser
387
388           "-fsimplify"  -> -- gather up SimplifierSwitches specially...
389                            simpl_sep opts defaultSimplSwitches core_td stg_td
390
391           "-fcalc-inlinings1"-> CORE_TD(CoreDoCalcInlinings1)
392           "-fcalc-inlinings2"-> CORE_TD(CoreDoCalcInlinings2)
393           "-ffloat-inwards"  -> CORE_TD(CoreDoFloatInwards)
394           "-ffull-laziness"  -> CORE_TD(CoreDoFullLaziness)
395           "-fliberate-case"  -> CORE_TD(CoreLiberateCase)
396           "-fprint-core"     -> CORE_TD(CoreDoPrintCore)
397           "-fstatic-args"    -> CORE_TD(CoreDoStaticArgs)
398           "-fstrictness"     -> CORE_TD(CoreDoStrictness)
399           "-fspecialise"     -> CORE_TD(CoreDoSpecialising)
400           "-ffoldr-build-worker-wrapper"  -> CORE_TD(CoreDoFoldrBuildWorkerWrapper)
401           "-ffoldr-build-ww-anal"  -> CORE_TD(CoreDoFoldrBuildWWAnal)
402
403           "-fstg-static-args" -> STG_TD(StgDoStaticArgs)
404           "-fupdate-analysis" -> STG_TD(StgDoUpdateAnalysis)
405           "-dstg-stats"       -> STG_TD(D_stg_stats)
406           "-flambda-lift"     -> STG_TD(StgDoLambdaLift)
407           "-fmassage-stg-for-profiling" -> STG_TD(StgDoMassageForProfiling)
408
409           _ -> -- NB: the driver is really supposed to handle bad options
410                sep opts core_td stg_td
411
412     ----------------
413
414     simpl_sep :: [FAST_STRING]            -- cmd-line opts (input)
415               -> [SimplifierSwitch]       -- simplifier-switch accumulator
416               -> [CoreToDo] -> [StgToDo]  -- to_do accumulators
417               -> ([CoreToDo], [StgToDo])  -- result
418
419         -- "simpl_sep" tailcalls "sep" once it's seen one set
420         -- of SimplifierSwitches for a CoreDoSimplify.
421
422 #ifdef DEBUG
423     simpl_sep input@[] simpl_sw core_td stg_td
424       = panic "simpl_sep []"
425 #endif
426
427         -- The SimplifierSwitches should be delimited by "[" and "]".
428
429     simpl_sep (opt1:opts) simpl_sw core_td stg_td
430       = case (_UNPK_ opt1) of
431           "[" -> simpl_sep opts simpl_sw core_td stg_td
432           "]" -> let
433                     this_simpl = CoreDoSimplify (isAmongSimpl simpl_sw)
434                  in
435                  sep opts (this_simpl : core_td) stg_td
436
437 #         define SIMPL_SW(sw) simpl_sep opts (sw:simpl_sw) core_td stg_td
438
439           -- the non-"just match a string" options are at the end...
440           "-fcode-duplication-ok"           -> SIMPL_SW(SimplOkToDupCode)
441           "-ffloat-lets-exposing-whnf"      -> SIMPL_SW(SimplFloatLetsExposingWHNF)
442           "-ffloat-primops-ok"              -> SIMPL_SW(SimplOkToFloatPrimOps)
443           "-falways-float-lets-from-lets"   -> SIMPL_SW(SimplAlwaysFloatLetsFromLets)
444           "-fdo-case-elim"                  -> SIMPL_SW(SimplDoCaseElim)
445           "-fdo-lambda-eta-expansion"       -> SIMPL_SW(SimplDoLambdaEtaExpansion)
446           "-fdo-foldr-build"                -> SIMPL_SW(SimplDoFoldrBuild)
447           "-fdo-arity-expand"               -> SIMPL_SW(SimplDoArityExpand)
448           "-fdo-inline-foldr-build"         -> SIMPL_SW(SimplDoInlineFoldrBuild)
449           "-fcase-of-case"                  -> SIMPL_SW(SimplCaseOfCase)
450           "-fcase-merge"                    -> SIMPL_SW(SimplCaseMerge)
451           "-flet-to-case"                   -> SIMPL_SW(SimplLetToCase)
452           "-fpedantic-bottoms"              -> SIMPL_SW(SimplPedanticBottoms)
453           "-fmay-delete-conjurable-ids"     -> SIMPL_SW(SimplMayDeleteConjurableIds)
454           "-fessential-unfoldings-only"     -> SIMPL_SW(EssentialUnfoldingsOnly)
455           "-fignore-inline-pragma"          -> SIMPL_SW(IgnoreINLINEPragma)
456           "-fno-let-from-case"              -> SIMPL_SW(SimplNoLetFromCase)
457           "-fno-let-from-app"               -> SIMPL_SW(SimplNoLetFromApp)
458           "-fno-let-from-strict-let"        -> SIMPL_SW(SimplNoLetFromStrictLet)
459           "-fclone-binds"                   -> SIMPL_SW(SimplPleaseClone)
460
461           o | starts_with_msi  -> SIMPL_SW(MaxSimplifierIterations (read after_msi))
462            where
463             maybe_msi           = startsWith "-fmax-simplifier-iterations"   o
464             starts_with_msi     = maybeToBool maybe_msi
465             (Just after_msi)    = maybe_msi
466
467           _ -> -- NB: the driver is really supposed to handle bad options
468                simpl_sep opts simpl_sw core_td stg_td
469 \end{code}
470
471 %************************************************************************
472 %*                                                                      *
473 \subsection{Switch ordering}
474 %*                                                                      *
475 %************************************************************************
476
477 In spite of the @Produce*@ and @SccGroup@ constructors, these things
478 behave just like enumeration types.
479
480 \begin{code}
481 instance Eq SimplifierSwitch where
482     a == b = tagOf_SimplSwitch a _EQ_ tagOf_SimplSwitch b
483
484 instance Ord SimplifierSwitch where
485     a <  b  = tagOf_SimplSwitch a _LT_ tagOf_SimplSwitch b
486     a <= b  = tagOf_SimplSwitch a _LE_ tagOf_SimplSwitch b
487
488 tagOf_SimplSwitch SimplOkToDupCode              =(ILIT(0) :: FAST_INT)
489 tagOf_SimplSwitch SimplFloatLetsExposingWHNF    = ILIT(1)
490 tagOf_SimplSwitch SimplOkToFloatPrimOps         = ILIT(2)
491 tagOf_SimplSwitch SimplAlwaysFloatLetsFromLets  = ILIT(3)
492 tagOf_SimplSwitch SimplDoCaseElim               = ILIT(4)
493 tagOf_SimplSwitch SimplCaseOfCase               = ILIT(6)
494 tagOf_SimplSwitch SimplLetToCase                = ILIT(7)
495 tagOf_SimplSwitch SimplMayDeleteConjurableIds   = ILIT(9)
496 tagOf_SimplSwitch SimplPedanticBottoms          = ILIT(10)
497 tagOf_SimplSwitch SimplDoArityExpand            = ILIT(11)
498 tagOf_SimplSwitch SimplDoFoldrBuild             = ILIT(12)
499 tagOf_SimplSwitch SimplDoInlineFoldrBuild       = ILIT(14)
500 tagOf_SimplSwitch IgnoreINLINEPragma            = ILIT(15)
501 tagOf_SimplSwitch SimplDoLambdaEtaExpansion     = ILIT(16)
502 tagOf_SimplSwitch EssentialUnfoldingsOnly       = ILIT(19)
503 tagOf_SimplSwitch (MaxSimplifierIterations _)   = ILIT(21)
504 tagOf_SimplSwitch SimplNoLetFromCase            = ILIT(27)
505 tagOf_SimplSwitch SimplNoLetFromApp             = ILIT(28)
506 tagOf_SimplSwitch SimplNoLetFromStrictLet       = ILIT(29)
507 tagOf_SimplSwitch SimplCaseMerge                = ILIT(31)
508 tagOf_SimplSwitch SimplPleaseClone              = 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 SimplPleaseClone)
515 \end{code}
516
517 %************************************************************************
518 %*                                                                      *
519 \subsection{Switch lookup}
520 %*                                                                      *
521 %************************************************************************
522
523 \begin{code}
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, 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 #if __GLASGOW_HASKELL__ < 400
548           Lift v -> v
549 #else
550           (# _, v #) -> v
551 #endif
552     }
553   where
554     mk_assoc_elem k@(MaxSimplifierIterations lvl)       = (IBOX(tagOf_SimplSwitch k), SwInt lvl)
555
556     mk_assoc_elem k = (IBOX(tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
557
558     -- cannot have duplicates if we are going to use the array thing
559     rm_dups switches_so_far switch
560       = if switch `is_elem` switches_so_far
561         then switches_so_far
562         else switch : switches_so_far
563       where
564         sw `is_elem` []     = False
565         sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) _EQ_ (tagOf_SimplSwitch s)
566                             || sw `is_elem` ss
567 \end{code}
568
569 Default settings for simplifier switches
570
571 \begin{code}
572 defaultSimplSwitches = [MaxSimplifierIterations         1
573                        ]
574 \end{code}
575
576 %************************************************************************
577 %*                                                                      *
578 \subsection{Misc functions for command-line options}
579 %*                                                                      *
580 %************************************************************************
581
582
583 \begin{code}
584 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
585
586 switchIsOn lookup_fn switch
587   = case (lookup_fn switch) of
588       SwBool False -> False
589       _            -> True
590
591 intSwitchSet :: (switch -> SwitchResult)
592              -> (Int -> switch)
593              -> Maybe Int
594
595 intSwitchSet lookup_fn switch
596   = case (lookup_fn (switch (panic "intSwitchSet"))) of
597       SwInt int -> Just int
598       _         -> Nothing
599 \end{code}