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