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