[project @ 1996-07-19 18:36:04 by partain]
[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
219   | SimplCaseMerge
220 \end{code}
221
222 %************************************************************************
223 %*                                                                      *
224 \subsection{Classifying command-line options}
225 %*                                                                      *
226 %************************************************************************
227
228 \begin{code}
229 lookUp     :: FAST_STRING -> Bool
230 lookup_int :: String -> Maybe Int
231 lookup_str :: String -> Maybe String
232
233 lookUp     sw = maybeToBool (assoc_opts sw)
234         
235 lookup_str sw = firstJust (map (startsWith sw) unpacked_opts)
236
237 lookup_int sw = case (lookup_str sw) of
238                   Nothing -> Nothing
239                   Just xx -> Just (read xx)
240
241 assoc_opts    = assocMaybe [ (a, True) | a <- argv ]
242 unpacked_opts = map _UNPK_ argv
243 \end{code}
244
245 \begin{code}
246 opt_AllDemanded                 = lookUp  SLIT("-fall-demanded")
247 opt_AllStrict                   = lookUp  SLIT("-fall-strict")
248 opt_AutoSccsOnAllToplevs        = lookUp  SLIT("-fauto-sccs-on-all-toplevs")
249 opt_AutoSccsOnExportedToplevs   = lookUp  SLIT("-fauto-sccs-on-exported-toplevs")
250 opt_AutoSccsOnIndividualCafs    = lookUp  SLIT("-fauto-sccs-on-individual-cafs")
251 opt_D_dump_absC                 = lookUp  SLIT("-ddump-absC")
252 opt_D_dump_asm                  = lookUp  SLIT("-ddump-asm")
253 opt_D_dump_deforest             = lookUp  SLIT("-ddump-deforest")
254 opt_D_dump_deriv                = lookUp  SLIT("-ddump-deriv")
255 opt_D_dump_ds                   = lookUp  SLIT("-ddump-ds")
256 opt_D_dump_flatC                = lookUp  SLIT("-ddump-flatC")
257 opt_D_dump_occur_anal           = lookUp  SLIT("-ddump-occur-anal")
258 opt_D_dump_rdr                  = lookUp  SLIT("-ddump-rdr")
259 opt_D_dump_realC                = lookUp  SLIT("-ddump-realC")
260 opt_D_dump_rn                   = lookUp  SLIT("-ddump-rn")
261 opt_D_dump_simpl                = lookUp  SLIT("-ddump-simpl")
262 opt_D_dump_spec                 = lookUp  SLIT("-ddump-spec")
263 opt_D_dump_stg                  = lookUp  SLIT("-ddump-stg")
264 opt_D_dump_stranal              = lookUp  SLIT("-ddump-stranal")
265 opt_D_dump_tc                   = lookUp  SLIT("-ddump-tc")
266 opt_D_show_passes               = lookUp  SLIT("-dshow-passes")
267 opt_D_simplifier_stats          = lookUp  SLIT("-dsimplifier-stats")
268 opt_D_source_stats              = lookUp  SLIT("-dsource-stats")
269 opt_D_verbose_core2core         = lookUp  SLIT("-dverbose-simpl")
270 opt_D_verbose_stg2stg           = lookUp  SLIT("-dverbose-stg")
271 opt_DoCoreLinting               = lookUp  SLIT("-dcore-lint")
272 opt_DoSemiTagging               = lookUp  SLIT("-fsemi-tagging")
273 opt_DoTickyProfiling            = lookUp  SLIT("-fticky-ticky")
274 opt_FoldrBuildOn                = lookUp  SLIT("-ffoldr-build-on")
275 opt_FoldrBuildTrace             = lookUp  SLIT("-ffoldr-build-trace")
276 opt_ForConcurrent               = lookUp  SLIT("-fconcurrent")
277 opt_GranMacros                  = lookUp  SLIT("-fgransim")
278 opt_GlasgowExts                 = lookUp  SLIT("-fglasgow-exts")
279 opt_Haskell_1_3                 = lookUp  SLIT("-fhaskell-1.3")
280 opt_IgnoreStrictnessPragmas     = lookUp  SLIT("-fignore-strictness-pragmas")
281 opt_IrrefutableEverything       = lookUp  SLIT("-firrefutable-everything")
282 opt_IrrefutableTuples           = lookUp  SLIT("-firrefutable-tuples")
283 opt_WarnNameShadowing           = lookUp  SLIT("-fwarn-name-shadowing")
284 opt_NumbersStrict               = lookUp  SLIT("-fnumbers-strict")
285 opt_OmitBlackHoling             = lookUp  SLIT("-dno-black-holing")
286 opt_OmitDefaultInstanceMethods  = lookUp  SLIT("-fomit-default-instance-methods")
287 opt_OmitInterfacePragmas        = lookUp  SLIT("-fomit-interface-pragmas")
288 opt_PprStyle_All                = lookUp  SLIT("-dppr-all")
289 opt_PprStyle_Debug              = lookUp  SLIT("-dppr-debug")
290 opt_PprStyle_User               = lookUp  SLIT("-dppr-user")
291 opt_ReportWhyUnfoldingsDisallowed= lookUp SLIT("-freport-disallowed-unfoldings")
292 opt_SccProfilingOn              = lookUp  SLIT("-fscc-profiling")
293 opt_ShowImportSpecs             = lookUp  SLIT("-fshow-import-specs")
294 opt_ShowPragmaNameErrs          = lookUp  SLIT("-fshow-pragma-name-errs")
295 opt_SigsRequired                = lookUp  SLIT("-fsignatures-required")
296 opt_SpecialiseAll               = lookUp  SLIT("-fspecialise-all")
297 opt_SpecialiseImports           = lookUp  SLIT("-fspecialise-imports")
298 opt_SpecialiseOverloaded        = lookUp  SLIT("-fspecialise-overloaded")
299 opt_SpecialiseTrace             = lookUp  SLIT("-ftrace-specialisation")
300 opt_SpecialiseUnboxed           = lookUp  SLIT("-fspecialise-unboxed")
301 opt_StgDoLetNoEscapes           = lookUp  SLIT("-flet-no-escape")
302 opt_Verbose                     = lookUp  SLIT("-v")
303 opt_UsingGhcInternals           = lookUp  SLIT("-fusing-ghc-internals")
304 opt_CompilingGhcInternals       = maybeToBool maybe_CompilingGhcInternals
305 maybe_CompilingGhcInternals     = lookup_str "-fcompiling-ghc-internals="
306 opt_SccGroup                    = lookup_str "-G="
307 opt_ProduceC                    = lookup_str "-C="
308 opt_ProduceS                    = lookup_str "-S="
309 opt_ProduceHi                   = lookup_str "-hifile=" -- the one to produce this time 
310 opt_HiMap                       = lookup_str "-himap="  -- file saying where to look for .hi files
311 opt_EnsureSplittableC           = lookup_str "-fglobalise-toplev-names="
312 opt_UnfoldingUseThreshold       = lookup_int "-funfolding-use-threshold"
313 opt_UnfoldingCreationThreshold  = lookup_int "-funfolding-creation-threshold"
314 opt_UnfoldingOverrideThreshold  = lookup_int "-funfolding-override-threshold"
315 opt_ReturnInRegsThreshold       = lookup_int "-freturn-in-regs-threshold"
316
317 opt_NoImplicitPrelude           = lookUp  SLIT("-fno-implicit-prelude")
318 opt_IgnoreIfacePragmas          = lookUp  SLIT("-fignore-interface-pragmas")
319 \end{code}
320
321 \begin{code}
322 classifyOpts :: ([CoreToDo],    -- Core-to-Core processing spec
323                  [StgToDo])     -- STG-to-STG   processing spec
324
325 classifyOpts = sep argv [] [] -- accumulators...
326   where
327     sep :: [FAST_STRING]                         -- cmd-line opts (input)
328         -> [CoreToDo] -> [StgToDo]       -- to_do accumulators
329         -> ([CoreToDo], [StgToDo])       -- result
330
331     sep [] core_td stg_td -- all done!
332       = (reverse core_td, reverse stg_td)
333
334 #       define CORE_TD(to_do) sep opts (to_do:core_td) stg_td
335 #       define STG_TD(to_do)  sep opts core_td (to_do:stg_td)
336 #       define IGNORE_ARG()   sep opts core_td stg_td
337
338     sep (opt1:opts) core_td stg_td
339       =
340         case (_UNPK_ opt1) of -- the non-"just match a string" options are at the end...
341
342           ',' : _       -> IGNORE_ARG() -- it is for the parser
343
344           "-fsimplify"  -> -- gather up SimplifierSwitches specially...
345                            simpl_sep opts defaultSimplSwitches core_td stg_td
346
347           "-fcalc-inlinings1"-> CORE_TD(CoreDoCalcInlinings1)
348           "-fcalc-inlinings2"-> CORE_TD(CoreDoCalcInlinings2)
349           "-ffloat-inwards"  -> CORE_TD(CoreDoFloatInwards)
350           "-ffull-laziness"  -> CORE_TD(CoreDoFullLaziness)
351           "-fliberate-case"  -> CORE_TD(CoreLiberateCase)
352           "-fprint-core"     -> CORE_TD(CoreDoPrintCore)
353           "-fstatic-args"    -> CORE_TD(CoreDoStaticArgs)
354           "-fstrictness"     -> CORE_TD(CoreDoStrictness)
355           "-fspecialise"     -> CORE_TD(CoreDoSpecialising)
356           "-fdeforest"       -> CORE_TD(CoreDoDeforest)
357           "-ffoldr-build-worker-wrapper"  -> CORE_TD(CoreDoFoldrBuildWorkerWrapper)
358           "-ffoldr-build-ww-anal"  -> CORE_TD(CoreDoFoldrBuildWWAnal)
359
360           "-fstg-static-args" -> STG_TD(StgDoStaticArgs)
361           "-fupdate-analysis" -> STG_TD(StgDoUpdateAnalysis)
362           "-dstg-stats"       -> STG_TD(D_stg_stats)
363           "-flambda-lift"     -> STG_TD(StgDoLambdaLift)
364           "-fmassage-stg-for-profiling" -> STG_TD(StgDoMassageForProfiling)
365
366           _ -> -- NB: the driver is really supposed to handle bad options
367                IGNORE_ARG()
368
369     ----------------
370
371     simpl_sep :: [FAST_STRING]      -- cmd-line opts (input)
372         -> [SimplifierSwitch]       -- simplifier-switch accumulator
373         -> [CoreToDo] -> [StgToDo]  -- to_do accumulators
374         -> ([CoreToDo], [StgToDo])  -- result
375
376         -- "simpl_sep" tailcalls "sep" once it's seen one set
377         -- of SimplifierSwitches for a CoreDoSimplify.
378
379 #ifdef DEBUG
380     simpl_sep input@[] simpl_sw core_td stg_td
381       = panic "simpl_sep []"
382 #endif
383
384         -- The SimplifierSwitches should be delimited by "(" and ")".
385
386     simpl_sep (opt1:opts) simpl_sw core_td stg_td
387       = case (_UNPK_ opt1) of
388           "(" -> simpl_sep opts simpl_sw core_td stg_td
389           ")" -> let
390                     this_simpl = CoreDoSimplify (isAmongSimpl simpl_sw)
391                  in
392                  sep opts (this_simpl : core_td) stg_td
393
394 #         define SIMPL_SW(sw) simpl_sep opts (sw:simpl_sw) core_td stg_td
395
396           -- the non-"just match a string" options are at the end...
397           "-fshow-simplifier-progress"      -> SIMPL_SW(ShowSimplifierProgress)
398           "-fcode-duplication-ok"           -> SIMPL_SW(SimplOkToDupCode)
399           "-ffloat-lets-exposing-whnf"      -> SIMPL_SW(SimplFloatLetsExposingWHNF)
400           "-ffloat-primops-ok"              -> SIMPL_SW(SimplOkToFloatPrimOps)
401           "-falways-float-lets-from-lets"   -> SIMPL_SW(SimplAlwaysFloatLetsFromLets)
402           "-fdo-case-elim"                  -> SIMPL_SW(SimplDoCaseElim)
403           "-fdo-eta-reduction"              -> SIMPL_SW(SimplDoEtaReduction)
404           "-fdo-lambda-eta-expansion"       -> SIMPL_SW(SimplDoLambdaEtaExpansion)
405           "-fdo-foldr-build"                -> SIMPL_SW(SimplDoFoldrBuild)
406           "-fdo-not-fold-back-append"       -> SIMPL_SW(SimplDontFoldBackAppend)
407           "-fdo-arity-expand"               -> SIMPL_SW(SimplDoArityExpand)
408           "-fdo-inline-foldr-build"         -> SIMPL_SW(SimplDoInlineFoldrBuild)
409           "-freuse-con"                     -> SIMPL_SW(SimplReuseCon)
410           "-fcase-of-case"                  -> SIMPL_SW(SimplCaseOfCase)
411           "-fcase-merge"                    -> SIMPL_SW(SimplCaseMerge)
412           "-flet-to-case"                   -> SIMPL_SW(SimplLetToCase)
413           "-fpedantic-bottoms"              -> SIMPL_SW(SimplPedanticBottoms)
414           "-fkeep-spec-pragma-ids"          -> SIMPL_SW(KeepSpecPragmaIds)
415           "-fkeep-unused-bindings"          -> SIMPL_SW(KeepUnusedBindings)
416           "-fmay-delete-conjurable-ids"     -> SIMPL_SW(SimplMayDeleteConjurableIds)
417           "-fessential-unfoldings-only"     -> SIMPL_SW(EssentialUnfoldingsOnly)
418           "-fignore-inline-pragma"          -> SIMPL_SW(IgnoreINLINEPragma)
419           "-fno-let-from-case"              -> SIMPL_SW(SimplNoLetFromCase)
420           "-fno-let-from-app"               -> SIMPL_SW(SimplNoLetFromApp)
421           "-fno-let-from-strict-let"        -> SIMPL_SW(SimplNoLetFromStrictLet)
422
423           o | starts_with_msi  -> SIMPL_SW(MaxSimplifierIterations (read after_msi))
424             | starts_with_suut -> SIMPL_SW(SimplUnfoldingUseThreshold (read after_suut))
425             | starts_with_suct -> SIMPL_SW(SimplUnfoldingCreationThreshold (read after_suct))
426             | starts_with_sucd -> SIMPL_SW(SimplUnfoldingConDiscount (read after_sucd))
427            where
428             maybe_suut          = startsWith "-fsimpl-uf-use-threshold"      o
429             maybe_suct          = startsWith "-fsimpl-uf-creation-threshold" o
430             maybe_sucd          = startsWith "-fsimpl-uf-con-discount" o
431             maybe_msi           = startsWith "-fmax-simplifier-iterations"   o
432             starts_with_suut    = maybeToBool maybe_suut
433             starts_with_suct    = maybeToBool maybe_suct
434             starts_with_sucd    = maybeToBool maybe_sucd
435             starts_with_msi     = maybeToBool maybe_msi
436             (Just after_suut)   = maybe_suut
437             (Just after_suct)   = maybe_suct
438             (Just after_sucd)   = maybe_sucd
439             (Just after_msi)    = maybe_msi
440
441           _ -> -- NB: the driver is really supposed to handle bad options
442                simpl_sep opts simpl_sw core_td stg_td
443 \end{code}
444
445 %************************************************************************
446 %*                                                                      *
447 \subsection{Switch ordering}
448 %*                                                                      *
449 %************************************************************************
450
451 In spite of the @Produce*@ and @SccGroup@ constructors, these things
452 behave just like enumeration types.
453
454 \begin{code}
455 instance Eq SimplifierSwitch where
456     a == b = tagOf_SimplSwitch a _EQ_ tagOf_SimplSwitch b
457
458 instance Ord SimplifierSwitch where
459     a <  b  = tagOf_SimplSwitch a _LT_ tagOf_SimplSwitch b
460     a <= b  = tagOf_SimplSwitch a _LE_ tagOf_SimplSwitch b
461
462 tagOf_SimplSwitch SimplOkToDupCode              =(ILIT(0) :: FAST_INT)
463 tagOf_SimplSwitch SimplFloatLetsExposingWHNF    = ILIT(1)
464 tagOf_SimplSwitch SimplOkToFloatPrimOps         = ILIT(2)
465 tagOf_SimplSwitch SimplAlwaysFloatLetsFromLets  = ILIT(3)
466 tagOf_SimplSwitch SimplDoCaseElim               = ILIT(4)
467 tagOf_SimplSwitch SimplReuseCon                 = ILIT(5)
468 tagOf_SimplSwitch SimplCaseOfCase               = ILIT(6)
469 tagOf_SimplSwitch SimplLetToCase                = ILIT(7)
470 tagOf_SimplSwitch SimplMayDeleteConjurableIds   = ILIT(9)
471 tagOf_SimplSwitch SimplPedanticBottoms          = ILIT(10)
472 tagOf_SimplSwitch SimplDoArityExpand            = ILIT(11)
473 tagOf_SimplSwitch SimplDoFoldrBuild             = ILIT(12)
474 tagOf_SimplSwitch SimplDoInlineFoldrBuild       = ILIT(14)
475 tagOf_SimplSwitch IgnoreINLINEPragma            = ILIT(15)
476 tagOf_SimplSwitch SimplDoLambdaEtaExpansion     = ILIT(16)
477 tagOf_SimplSwitch SimplDoEtaReduction           = ILIT(18)
478 tagOf_SimplSwitch EssentialUnfoldingsOnly       = ILIT(19)
479 tagOf_SimplSwitch ShowSimplifierProgress        = ILIT(20)
480 tagOf_SimplSwitch (MaxSimplifierIterations _)   = ILIT(21)
481 tagOf_SimplSwitch (SimplUnfoldingUseThreshold _)      = ILIT(22)
482 tagOf_SimplSwitch (SimplUnfoldingConDiscount _)       = ILIT(23)
483 tagOf_SimplSwitch (SimplUnfoldingCreationThreshold _) = ILIT(24)
484 tagOf_SimplSwitch KeepSpecPragmaIds             = ILIT(25)
485 tagOf_SimplSwitch KeepUnusedBindings            = ILIT(26)
486 tagOf_SimplSwitch SimplNoLetFromCase            = ILIT(27)
487 tagOf_SimplSwitch SimplNoLetFromApp             = ILIT(28)
488 tagOf_SimplSwitch SimplNoLetFromStrictLet       = ILIT(29)
489 tagOf_SimplSwitch SimplDontFoldBackAppend       = ILIT(30)
490 tagOf_SimplSwitch SimplCaseMerge                = ILIT(31)
491 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
492
493 tagOf_SimplSwitch _ = panic# "tagOf_SimplSwitch"
494
495 lAST_SIMPL_SWITCH_TAG = IBOX(tagOf_SimplSwitch SimplCaseMerge)
496 \end{code}
497
498 %************************************************************************
499 %*                                                                      *
500 \subsection{Switch lookup}
501 %*                                                                      *
502 %************************************************************************
503
504 \begin{code}
505 #if __GLASGOW_HASKELL__ >= 200
506 # define ARRAY      Array
507 # define LIFT       GHCbase.Lift
508 # define SET_TO     =:
509 (=:) a b = (a,b)
510 #else
511 # define ARRAY      _Array
512 # define LIFT       _Lift
513 # define SET_TO     :=
514 #endif
515
516 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
517
518 isAmongSimpl on_switches                -- Switches mentioned later occur *earlier*
519                                         -- in the list; defaults right at the end.
520   = let
521         tidied_on_switches = foldl rm_dups [] on_switches
522                 -- The fold*l* ensures that we keep the latest switches;
523                 -- ie the ones that occur earliest in the list.
524
525         sw_tbl :: Array Int SwitchResult
526
527         sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
528                         all_undefined)
529                  // defined_elems
530
531         all_undefined = [ i SET_TO SwBool False | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
532
533         defined_elems = map mk_assoc_elem tidied_on_switches
534     in
535     -- (avoid some unboxing, bounds checking, and other horrible things:)
536     case sw_tbl of { ARRAY bounds_who_needs_'em stuff ->
537     \ switch ->
538         case (indexArray# stuff (tagOf_SimplSwitch switch)) of
539           LIFT v -> v
540     }
541   where
542     mk_assoc_elem k@(MaxSimplifierIterations lvl)       = IBOX(tagOf_SimplSwitch k) SET_TO SwInt lvl
543     mk_assoc_elem k@(SimplUnfoldingUseThreshold      i) = IBOX(tagOf_SimplSwitch k) SET_TO SwInt i
544     mk_assoc_elem k@(SimplUnfoldingConDiscount       i) = IBOX(tagOf_SimplSwitch k) SET_TO SwInt i
545     mk_assoc_elem k@(SimplUnfoldingCreationThreshold i) = IBOX(tagOf_SimplSwitch k) SET_TO SwInt i
546
547     mk_assoc_elem k = IBOX(tagOf_SimplSwitch k) SET_TO SwBool   True -- I'm here, Mom!
548
549     -- cannot have duplicates if we are going to use the array thing
550     rm_dups switches_so_far switch
551       = if switch `is_elem` switches_so_far
552         then switches_so_far
553         else switch : switches_so_far
554       where
555         sw `is_elem` []     = False
556         sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) _EQ_ (tagOf_SimplSwitch s)
557                             || sw `is_elem` ss
558 \end{code}
559
560 Default settings for simplifier switches
561
562 \begin{code}
563 defaultSimplSwitches = [SimplUnfoldingCreationThreshold uNFOLDING_CREATION_THRESHOLD,
564                         SimplUnfoldingUseThreshold      uNFOLDING_USE_THRESHOLD,
565                         SimplUnfoldingConDiscount       uNFOLDING_CON_DISCOUNT_WEIGHT,
566                         MaxSimplifierIterations         1
567                        ]
568 \end{code}
569
570 %************************************************************************
571 %*                                                                      *
572 \subsection{Misc functions for command-line options}
573 %*                                                                      *
574 %************************************************************************
575
576
577 \begin{code}
578 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
579
580 switchIsOn lookup_fn switch
581   = case (lookup_fn switch) of
582       SwBool False -> False
583       _            -> True
584
585 stringSwitchSet :: (switch -> SwitchResult)
586                 -> (FAST_STRING -> switch)
587                 -> Maybe FAST_STRING
588
589 stringSwitchSet lookup_fn switch
590   = case (lookup_fn (switch (panic "stringSwitchSet"))) of
591       SwString str -> Just str
592       _            -> Nothing
593
594 intSwitchSet :: (switch -> SwitchResult)
595              -> (Int -> switch)
596              -> Maybe Int
597
598 intSwitchSet lookup_fn switch
599   = case (lookup_fn (switch (panic "intSwitchSet"))) of
600       SwInt int -> Just int
601       _         -> Nothing
602 \end{code}