2 % (c) The AQUA Project, Glasgow University, 1996
4 \section[CmdLineOpts]{Things to do with command-line options}
7 #include "HsVersions.h"
9 module CmdLineOpts where
11 import PreludeGlaST -- bad bad bad boy, Will (_Array internals)
14 CHK_Ubiq() -- debugging consistency check
16 import Maybes ( assocMaybe, firstJust, maybeToBool, Maybe(..) )
17 import Util ( startsWith, panic, panic#, assertPanic )
20 A command-line {\em switch} is (generally) either on or off; e.g., the
21 ``verbose'' (-v) switch is either on or off. (The \tr{-G<group>}
22 switch is an exception; it's set to a string, or nothing.)
24 A list of {\em ToDo}s is things to be done in a particular part of
25 processing. A (fictitious) example for the Core-to-Core simplifier
26 might be: run the simplifier, then run the strictness analyser, then
27 run the simplifier again (three ``todos'').
29 There are three ``to-do processing centers'' at the moment. In the
30 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
31 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
32 (\tr{simplStg/SimplStg.lhs}).
34 %************************************************************************
36 \subsection{Datatypes associated with command-line options}
38 %************************************************************************
42 = SwBool Bool -- on/off
43 | SwString FAST_STRING -- nothing or a String
44 | SwInt Int -- nothing or an Int
48 data CoreToDo -- These are diff core-to-core passes,
49 -- which may be invoked in any order,
50 -- as many times as you like.
52 = CoreDoSimplify -- The core-to-core simplifier.
53 (SimplifierSwitch -> SwitchResult)
54 -- Each run of the simplifier can take a different
55 -- set of simplifier-specific flags.
56 | CoreDoCalcInlinings1
57 | CoreDoCalcInlinings2
66 | CoreDoAutoCostCentres
67 | CoreDoFoldrBuildWorkerWrapper
68 | CoreDoFoldrBuildWWAnal
76 | StgDoMassageForProfiling -- should be (next to) last
77 -- There's also setStgVarInfo, but its absolute "lastness"
78 -- is so critical that it is hardwired in (no flag).
85 | SimplFloatLetsExposingWHNF
86 | SimplOkToFloatPrimOps
87 | SimplAlwaysFloatLetsFromLets
92 | SimplMayDeleteConjurableIds
93 | SimplPedanticBottoms -- see Simplifier for an explanation
94 | SimplDoArityExpand -- expand arity of bindings
95 | SimplDoFoldrBuild -- This is the per-simplification flag;
96 -- see also FoldrBuildOn, used elsewhere
98 | SimplDoInlineFoldrBuild
99 -- inline foldr/build (*after* f/b rule is used)
102 | SimplDoLambdaEtaExpansion
103 | SimplDoEtaReduction
105 | EssentialUnfoldingsOnly -- never mind the thresholds, only
106 -- do unfoldings that *must* be done
107 -- (to saturate constructors and primitives)
109 | ShowSimplifierProgress -- report counts on every interation
111 | MaxSimplifierIterations Int
113 | SimplUnfoldingUseThreshold Int -- per-simplification variants
114 | SimplUnfoldingCreationThreshold Int
116 | KeepSpecPragmaIds -- We normally *toss* Ids we can do without
119 | SimplNoLetFromCase -- used when turning off floating entirely
120 | SimplNoLetFromApp -- (for experimentation only) WDP 95/10
121 | SimplNoLetFromStrictLet
123 | SimplDontFoldBackAppend
124 -- we fold `foldr (:)' back into flip (++),
125 -- but we *don't* want to do it when compiling
126 -- List.hs, otherwise
127 -- xs ++ ys = foldr (:) ys xs
128 -- {- via our loopback -}
129 -- xs ++ ys = xs ++ ys
131 -- So only use this flag inside List.hs
132 -- (Sigh, what a HACK, Andy. WDP 96/01)
135 %************************************************************************
137 \subsection{Classifying command-line options}
139 %************************************************************************
142 lookup :: FAST_STRING -> Bool
143 lookup_int :: String -> Maybe Int
144 lookup_str :: String -> Maybe String
146 lookup sw = maybeToBool (assoc_opts sw)
148 lookup_str sw = firstJust (map (startsWith sw) unpacked_opts)
150 lookup_int sw = case (lookup_str sw) of
152 Just xx -> Just (read xx)
154 assoc_opts = assocMaybe [ (a, True) | a <- argv ]
155 unpacked_opts = map _UNPK_ argv
159 opt_AllDemanded = lookup SLIT("-fall-demanded")
160 opt_AllStrict = lookup SLIT("-fall-strict")
161 opt_AutoSccsOnAllToplevs = lookup SLIT("-fauto-sccs-on-all-toplevs")
162 opt_AutoSccsOnExportedToplevs = lookup SLIT("-fauto-sccs-on-exported-toplevs")
163 opt_AutoSccsOnIndividualCafs = lookup SLIT("-fauto-sccs-on-individual-cafs")
164 opt_CompilingPrelude = lookup SLIT("-prelude")
165 opt_D_dump_absC = lookup SLIT("-ddump-absC")
166 opt_D_dump_asm = lookup SLIT("-ddump-asm")
167 opt_D_dump_deforest = lookup SLIT("-ddump-deforest")
168 opt_D_dump_deriv = lookup SLIT("-ddump-deriv")
169 opt_D_dump_ds = lookup SLIT("-ddump-ds")
170 opt_D_dump_flatC = lookup SLIT("-ddump-flatC")
171 opt_D_dump_occur_anal = lookup SLIT("-ddump-occur-anal")
172 opt_D_dump_rdr = lookup SLIT("-ddump-rdr")
173 opt_D_dump_realC = lookup SLIT("-ddump-realC")
174 opt_D_dump_rn = lookup SLIT("-ddump-rn")
175 opt_D_dump_simpl = lookup SLIT("-ddump-simpl")
176 opt_D_dump_spec = lookup SLIT("-ddump-spec")
177 opt_D_dump_stg = lookup SLIT("-ddump-stg")
178 opt_D_dump_stranal = lookup SLIT("-ddump-stranal")
179 opt_D_dump_tc = lookup SLIT("-ddump-tc")
180 opt_D_show_passes = lookup SLIT("-dshow-passes")
181 opt_D_simplifier_stats = lookup SLIT("-dsimplifier-stats")
182 opt_D_source_stats = lookup SLIT("-dsource-stats")
183 opt_D_verbose_core2core = lookup SLIT("-dverbose-simpl")
184 opt_D_verbose_stg2stg = lookup SLIT("-dverbose-stg")
185 opt_DoCoreLinting = lookup SLIT("-dcore-lint")
186 opt_DoSemiTagging = lookup SLIT("-fsemi-tagging")
187 opt_DoTickyProfiling = lookup SLIT("-fticky-ticky")
188 opt_EmitArityChecks = lookup SLIT("-darity-checks")
189 opt_FoldrBuildOn = lookup SLIT("-ffoldr-build-on")
190 opt_FoldrBuildTrace = lookup SLIT("-ffoldr-build-trace")
191 opt_ForConcurrent = lookup SLIT("-fconcurrent")
192 opt_GlasgowExts = lookup SLIT("-fglasgow-exts")
193 opt_Haskell_1_3 = lookup SLIT("-fhaskell-1.3")
194 opt_HideBuiltinNames = lookup SLIT("-fhide-builtin-names")
195 opt_HideMostBuiltinNames = lookup SLIT("-fmin-builtin-names")
196 opt_IgnoreStrictnessPragmas = lookup SLIT("-fignore-strictness-pragmas")
197 opt_IrrefutableEverything = lookup SLIT("-firrefutable-everything")
198 opt_IrrefutableTuples = lookup SLIT("-firrefutable-tuples")
199 opt_WarnNameShadowing = lookup SLIT("-fwarn-name-shadowing")
200 opt_NumbersStrict = lookup SLIT("-fnumbers-strict")
201 opt_OmitBlackHoling = lookup SLIT("-dno-black-holing")
202 opt_OmitDefaultInstanceMethods = lookup SLIT("-fomit-default-instance-methods")
203 opt_OmitInterfacePragmas = lookup SLIT("-fomit-interface-pragmas")
204 opt_OmitReexportedInstances = lookup SLIT("-fomit-reexported-instances")
205 opt_PprStyle_All = lookup SLIT("-dppr-all")
206 opt_PprStyle_Debug = lookup SLIT("-dppr-debug")
207 opt_PprStyle_User = lookup SLIT("-dppr-user")
208 opt_ReportWhyUnfoldingsDisallowed= lookup SLIT("-freport-disallowed-unfoldings")
209 opt_SccProfilingOn = lookup SLIT("-fscc-profiling")
210 opt_ShowImportSpecs = lookup SLIT("-fshow-import-specs")
211 opt_ShowPragmaNameErrs = lookup SLIT("-fshow-pragma-name-errs")
212 opt_SigsRequired = lookup SLIT("-fsignatures-required")
213 opt_SpecialiseAll = lookup SLIT("-fspecialise-all")
214 opt_SpecialiseImports = lookup SLIT("-fspecialise-imports")
215 opt_SpecialiseOverloaded = lookup SLIT("-fspecialise-overloaded")
216 opt_SpecialiseTrace = lookup SLIT("-ftrace-specialisation")
217 opt_SpecialiseUnboxed = lookup SLIT("-fspecialise-unboxed")
218 opt_StgDoLetNoEscapes = lookup SLIT("-flet-no-escape")
219 opt_Verbose = lookup SLIT("-v")
220 opt_AsmTarget = lookup_str "-fasm="
221 opt_SccGroup = lookup_str "-G="
222 opt_ProduceC = lookup_str "-C="
223 opt_ProduceS = lookup_str "-S="
224 opt_ProduceHi = lookup_str "-hifile="
225 opt_ProduceHu = lookup_str "-hufile="
226 opt_MyHi = lookup_str "-myhifile=" -- the ones produced last time
227 opt_MyHu = lookup_str "-myhufile=" -- for this module
228 opt_EnsureSplittableC = lookup_str "-fglobalise-toplev-names="
229 opt_UnfoldingUseThreshold = lookup_int "-funfolding-use-threshold"
230 opt_UnfoldingCreationThreshold = lookup_int "-funfolding-creation-threshold"
231 opt_UnfoldingOverrideThreshold = lookup_int "-funfolding-override-threshold"
232 opt_ReturnInRegsThreshold = lookup_int "-freturn-in-regs-threshold"
234 opt_NoImplicitPrelude = lookup SLIT("-fno-implicit-prelude")
235 opt_IgnoreIfacePragmas = lookup SLIT("-fignore-interface-pragmas")
237 opt_HuSuffix = case (lookup_str "-husuffix=") of { Nothing -> ".hu" ; Just x -> x }
238 opt_HiSuffix = case (lookup_str "-hisuffix=") of { Nothing -> ".hi" ; Just x -> x }
239 opt_SysHiSuffix = case (lookup_str "-syshisuffix=") of { Nothing -> ".hi" ; Just x -> x }
241 opt_HiDirList = get_dir_list "-i="
242 opt_SysHiDirList = get_dir_list "-j="
245 = case (lookup_str tag) of
246 Nothing -> [{-no dirs to search???-}]
247 Just xs -> colon_split xs "" [] -- character and dir accumulators, both reversed...
249 colon_split [] cacc dacc = reverse (reverse cacc : dacc)
250 colon_split (':' : xs) cacc dacc = colon_split xs "" (reverse cacc : dacc)
251 colon_split ( x : xs) cacc dacc = colon_split xs (x : cacc) dacc
253 -- -hisuf, -hisuf-prelude
254 -- -fno-implicit-prelude
255 -- -fignore-interface-pragmas
256 -- importdirs and sysimport dirs
260 classifyOpts :: ([CoreToDo], -- Core-to-Core processing spec
261 [StgToDo]) -- STG-to-STG processing spec
263 classifyOpts = sep argv [] [] -- accumulators...
265 sep :: [FAST_STRING] -- cmd-line opts (input)
266 -> [CoreToDo] -> [StgToDo] -- to_do accumulators
267 -> ([CoreToDo], [StgToDo]) -- result
269 sep [] core_td stg_td -- all done!
270 = (reverse core_td, reverse stg_td)
272 # define CORE_TD(to_do) sep opts (to_do:core_td) stg_td
273 # define STG_TD(to_do) sep opts core_td (to_do:stg_td)
274 # define IGNORE_ARG() sep opts core_td stg_td
276 sep (opt1:opts) core_td stg_td
278 case (_UNPK_ opt1) of -- the non-"just match a string" options are at the end...
280 ',' : _ -> IGNORE_ARG() -- it is for the parser
282 "-fsimplify" -> -- gather up SimplifierSwitches specially...
283 simpl_sep opts [] core_td stg_td
285 "-fcalc-inlinings1"-> CORE_TD(CoreDoCalcInlinings1)
286 "-fcalc-inlinings2"-> CORE_TD(CoreDoCalcInlinings2)
287 "-ffloat-inwards" -> CORE_TD(CoreDoFloatInwards)
288 "-ffull-laziness" -> CORE_TD(CoreDoFullLaziness)
289 "-fliberate-case" -> CORE_TD(CoreLiberateCase)
290 "-fprint-core" -> CORE_TD(CoreDoPrintCore)
291 "-fstatic-args" -> CORE_TD(CoreDoStaticArgs)
292 "-fstrictness" -> CORE_TD(CoreDoStrictness)
293 "-fspecialise" -> CORE_TD(CoreDoSpecialising)
294 "-fdeforest" -> CORE_TD(CoreDoDeforest)
295 "-fadd-auto-sccs" -> CORE_TD(CoreDoAutoCostCentres)
296 "-ffoldr-build-worker-wrapper" -> CORE_TD(CoreDoFoldrBuildWorkerWrapper)
297 "-ffoldr-build-ww-anal" -> CORE_TD(CoreDoFoldrBuildWWAnal)
299 "-fstg-static-args" -> STG_TD(StgDoStaticArgs)
300 "-fupdate-analysis" -> STG_TD(StgDoUpdateAnalysis)
301 "-dstg-stats" -> STG_TD(D_stg_stats)
302 "-flambda-lift" -> STG_TD(StgDoLambdaLift)
303 "-fmassage-stg-for-profiling" -> STG_TD(StgDoMassageForProfiling)
305 _ -> -- NB: the driver is really supposed to handle bad options
310 simpl_sep :: [FAST_STRING] -- cmd-line opts (input)
311 -> [SimplifierSwitch] -- simplifier-switch accumulator
312 -> [CoreToDo] -> [StgToDo] -- to_do accumulators
313 -> ([CoreToDo], [StgToDo]) -- result
315 -- "simpl_sep" tailcalls "sep" once it's seen one set
316 -- of SimplifierSwitches for a CoreDoSimplify.
319 simpl_sep input@[] simpl_sw core_td stg_td
320 = panic "simpl_sep []"
323 -- The SimplifierSwitches should be delimited by "(" and ")".
325 simpl_sep (opt1:opts) simpl_sw core_td stg_td
326 = case (_UNPK_ opt1) of
327 "(" -> ASSERT (null simpl_sw)
328 simpl_sep opts [] core_td stg_td
330 this_simpl = CoreDoSimplify (isAmongSimpl simpl_sw)
332 sep opts (this_simpl : core_td) stg_td
334 # define SIMPL_SW(sw) simpl_sep opts (sw:simpl_sw) core_td stg_td
336 -- the non-"just match a string" options are at the end...
337 "-fshow-simplifier-progress" -> SIMPL_SW(ShowSimplifierProgress)
338 "-fcode-duplication-ok" -> SIMPL_SW(SimplOkToDupCode)
339 "-ffloat-lets-exposing-whnf" -> SIMPL_SW(SimplFloatLetsExposingWHNF)
340 "-ffloat-primops-ok" -> SIMPL_SW(SimplOkToFloatPrimOps)
341 "-falways-float-lets-from-lets" -> SIMPL_SW(SimplAlwaysFloatLetsFromLets)
342 "-fdo-case-elim" -> SIMPL_SW(SimplDoCaseElim)
343 "-fdo-eta-reduction" -> SIMPL_SW(SimplDoEtaReduction)
344 "-fdo-lambda-eta-expansion" -> SIMPL_SW(SimplDoLambdaEtaExpansion)
345 "-fdo-foldr-build" -> SIMPL_SW(SimplDoFoldrBuild)
346 "-fdo-not-fold-back-append" -> SIMPL_SW(SimplDontFoldBackAppend)
347 "-fdo-arity-expand" -> SIMPL_SW(SimplDoArityExpand)
348 "-fdo-inline-foldr-build" -> SIMPL_SW(SimplDoInlineFoldrBuild)
349 "-freuse-con" -> SIMPL_SW(SimplReuseCon)
350 "-fcase-of-case" -> SIMPL_SW(SimplCaseOfCase)
351 "-flet-to-case" -> SIMPL_SW(SimplLetToCase)
352 "-fpedantic-bottoms" -> SIMPL_SW(SimplPedanticBottoms)
353 "-fkeep-spec-pragma-ids" -> SIMPL_SW(KeepSpecPragmaIds)
354 "-fkeep-unused-bindings" -> SIMPL_SW(KeepUnusedBindings)
355 "-fmay-delete-conjurable-ids" -> SIMPL_SW(SimplMayDeleteConjurableIds)
356 "-fessential-unfoldings-only" -> SIMPL_SW(EssentialUnfoldingsOnly)
357 "-fignore-inline-pragma" -> SIMPL_SW(IgnoreINLINEPragma)
358 "-fno-let-from-case" -> SIMPL_SW(SimplNoLetFromCase)
359 "-fno-let-from-app" -> SIMPL_SW(SimplNoLetFromApp)
360 "-fno-let-from-strict-let" -> SIMPL_SW(SimplNoLetFromStrictLet)
362 o | starts_with_msi -> SIMPL_SW(MaxSimplifierIterations (read after_msi))
363 | starts_with_suut -> SIMPL_SW(SimplUnfoldingUseThreshold (read after_suut))
364 | starts_with_suct -> SIMPL_SW(SimplUnfoldingCreationThreshold (read after_suct))
366 maybe_suut = startsWith "-fsimpl-uf-use-threshold" o
367 maybe_suct = startsWith "-fsimpl-uf-creation-threshold" o
368 maybe_msi = startsWith "-fmax-simplifier-iterations" o
369 starts_with_suut = maybeToBool maybe_suut
370 starts_with_suct = maybeToBool maybe_suct
371 starts_with_msi = maybeToBool maybe_msi
372 (Just after_suut) = maybe_suut
373 (Just after_suct) = maybe_suct
374 (Just after_msi) = maybe_msi
376 _ -> -- NB: the driver is really supposed to handle bad options
377 simpl_sep opts simpl_sw core_td stg_td
380 %************************************************************************
382 \subsection{Switch ordering}
384 %************************************************************************
386 In spite of the @Produce*@ and @SccGroup@ constructors, these things
387 behave just like enumeration types.
390 instance Eq SimplifierSwitch where
391 a == b = tagOf_SimplSwitch a _EQ_ tagOf_SimplSwitch b
393 instance Ord SimplifierSwitch where
394 a < b = tagOf_SimplSwitch a _LT_ tagOf_SimplSwitch b
395 a <= b = tagOf_SimplSwitch a _LE_ tagOf_SimplSwitch b
397 tagOf_SimplSwitch SimplOkToDupCode =(ILIT(0) :: FAST_INT)
398 tagOf_SimplSwitch SimplFloatLetsExposingWHNF = ILIT(1)
399 tagOf_SimplSwitch SimplOkToFloatPrimOps = ILIT(2)
400 tagOf_SimplSwitch SimplAlwaysFloatLetsFromLets = ILIT(3)
401 tagOf_SimplSwitch SimplDoCaseElim = ILIT(4)
402 tagOf_SimplSwitch SimplReuseCon = ILIT(5)
403 tagOf_SimplSwitch SimplCaseOfCase = ILIT(6)
404 tagOf_SimplSwitch SimplLetToCase = ILIT(7)
405 tagOf_SimplSwitch SimplMayDeleteConjurableIds = ILIT(9)
406 tagOf_SimplSwitch SimplPedanticBottoms = ILIT(10)
407 tagOf_SimplSwitch SimplDoArityExpand = ILIT(11)
408 tagOf_SimplSwitch SimplDoFoldrBuild = ILIT(12)
409 tagOf_SimplSwitch SimplDoInlineFoldrBuild = ILIT(14)
410 tagOf_SimplSwitch IgnoreINLINEPragma = ILIT(15)
411 tagOf_SimplSwitch SimplDoLambdaEtaExpansion = ILIT(16)
412 tagOf_SimplSwitch SimplDoEtaReduction = ILIT(18)
413 tagOf_SimplSwitch EssentialUnfoldingsOnly = ILIT(19)
414 tagOf_SimplSwitch ShowSimplifierProgress = ILIT(20)
415 tagOf_SimplSwitch (MaxSimplifierIterations _) = ILIT(21)
416 tagOf_SimplSwitch (SimplUnfoldingUseThreshold _) = ILIT(22)
417 tagOf_SimplSwitch (SimplUnfoldingCreationThreshold _) = ILIT(23)
418 tagOf_SimplSwitch KeepSpecPragmaIds = ILIT(24)
419 tagOf_SimplSwitch KeepUnusedBindings = ILIT(25)
420 tagOf_SimplSwitch SimplNoLetFromCase = ILIT(26)
421 tagOf_SimplSwitch SimplNoLetFromApp = ILIT(27)
422 tagOf_SimplSwitch SimplNoLetFromStrictLet = ILIT(28)
423 tagOf_SimplSwitch SimplDontFoldBackAppend = ILIT(29)
424 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
426 tagOf_SimplSwitch _ = panic# "tagOf_SimplSwitch"
428 lAST_SIMPL_SWITCH_TAG = IBOX(tagOf_SimplSwitch SimplDontFoldBackAppend)
431 %************************************************************************
433 \subsection{Switch lookup}
435 %************************************************************************
438 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
440 isAmongSimpl on_switches
442 tidied_on_switches = foldl rm_dups [] on_switches
444 sw_tbl :: Array Int SwitchResult
446 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
450 all_undefined = [ i := SwBool False | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
452 defined_elems = map mk_assoc_elem tidied_on_switches
454 -- (avoid some unboxing, bounds checking, and other horrible things:)
455 case sw_tbl of { _Array bounds_who_needs_'em stuff ->
457 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
461 mk_assoc_elem k@(MaxSimplifierIterations lvl) = IBOX(tagOf_SimplSwitch k) := SwInt lvl
462 mk_assoc_elem k@(SimplUnfoldingUseThreshold i) = IBOX(tagOf_SimplSwitch k) := SwInt i
463 mk_assoc_elem k@(SimplUnfoldingCreationThreshold i) = IBOX(tagOf_SimplSwitch k) := SwInt i
465 mk_assoc_elem k = IBOX(tagOf_SimplSwitch k) := SwBool True -- I'm here, Mom!
467 -- cannot have duplicates if we are going to use the array thing
469 rm_dups switches_so_far switch
470 = if switch `is_elem` switches_so_far
472 else switch : switches_so_far
474 sw `is_elem` [] = False
475 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) _EQ_ (tagOf_SimplSwitch s)
479 %************************************************************************
481 \subsection{Misc functions for command-line options}
483 %************************************************************************
487 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
489 switchIsOn lookup_fn switch
490 = case (lookup_fn switch) of
491 SwBool False -> False
494 stringSwitchSet :: (switch -> SwitchResult)
495 -> (FAST_STRING -> switch)
498 stringSwitchSet lookup_fn switch
499 = case (lookup_fn (switch (panic "stringSwitchSet"))) of
500 SwString str -> Just str
503 intSwitchSet :: (switch -> SwitchResult)
507 intSwitchSet lookup_fn switch
508 = case (lookup_fn (switch (panic "intSwitchSet"))) of
509 SwInt int -> Just int