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