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