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