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