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