1 -----------------------------------------------------------------------------
5 -- Static flags can only be set once, on the command-line. Inside GHC,
6 -- each static flag corresponds to a top-level value, usually of type Bool.
8 -- (c) The University of Glasgow 2005
10 -----------------------------------------------------------------------------
18 WayName(..), v_Ways, v_Build_tag, v_RTS_Build_tag, isRTSWay,
20 -- Output style options
27 opt_AutoSccsOnAllToplevs,
28 opt_AutoSccsOnExportedToplevs,
29 opt_AutoSccsOnIndividualCafs,
38 opt_IrrefutableTuples,
44 opt_SpecInlineJoinPoints,
46 opt_SimplNoPreInlining,
47 opt_SimplExcessPrecision,
51 opt_UF_CreationThreshold,
53 opt_UF_FunAppDiscount,
57 -- Optimization fuel controls
77 #include "HsVersions.h"
83 import Maybes ( firstJust )
86 import Control.Exception ( throwDyn )
88 import System.IO.Unsafe ( unsafePerformIO )
89 import Control.Monad ( when )
90 import Data.Char ( isDigit )
93 -----------------------------------------------------------------------------
96 parseStaticFlags :: [String] -> IO ([String], [String])
97 parseStaticFlags args = do
98 ready <- readIORef v_opt_C_ready
99 when ready $ throwDyn (ProgramError "Too late for parseStaticFlags: call it before newSession")
101 (leftover, errs, warns1) <- processArgs static_flags args
102 when (not (null errs)) $ throwDyn (UsageError (unlines errs))
104 -- deal with the way flags: the way (eg. prof) gives rise to
105 -- further flags, some of which might be static.
106 way_flags <- findBuildTag
108 -- if we're unregisterised, add some more flags
109 let unreg_flags | cGhcUnregisterised == "YES" = unregFlags
112 (more_leftover, errs, warns2) <- processArgs static_flags (unreg_flags ++ way_flags)
114 -- see sanity code in staticOpts
115 writeIORef v_opt_C_ready True
117 -- TABLES_NEXT_TO_CODE affects the info table layout.
118 -- Be careful to do this *after* all processArgs,
119 -- because evaluating tablesNextToCode involves looking at the global
120 -- static flags. Those pesky global variables...
121 let cg_flags | tablesNextToCode = ["-optc-DTABLES_NEXT_TO_CODE"]
124 -- HACK: -fexcess-precision is both a static and a dynamic flag. If
125 -- the static flag parser has slurped it, we must return it as a
126 -- leftover too. ToDo: make -fexcess-precision dynamic only.
127 let excess_prec | opt_SimplExcessPrecision = ["-fexcess-precision"]
130 when (not (null errs)) $ ghcError (UsageError (unlines errs))
131 return (excess_prec ++ cg_flags ++ more_leftover ++ leftover,
134 initStaticOpts :: IO ()
135 initStaticOpts = writeIORef v_opt_C_ready True
137 static_flags :: [Flag IO]
138 -- All the static flags should appear in this list. It describes how each
139 -- static flag should be processed. Two main purposes:
140 -- (a) if a command-line flag doesn't appear in the list, GHC can complain
141 -- (b) a command-line flag may remove, or add, other flags; e.g. the "-fno-X" things
143 -- The common (PassFlag addOpt) action puts the static flag into the bunch of
144 -- things that are searched up by the top-level definitions like
145 -- opt_foo = lookUp (fsLit "-dfoo")
147 -- Note that ordering is important in the following list: any flag which
148 -- is a prefix flag (i.e. HasArg, Prefix, OptPrefix, AnySuffix) will override
149 -- flags further down the list with the same prefix.
152 ------- GHCi -------------------------------------------------------
153 Flag "ignore-dot-ghci" (PassFlag addOpt) Supported
154 , Flag "read-dot-ghci" (NoArg (removeOpt "-ignore-dot-ghci")) Supported
156 ------- ways --------------------------------------------------------
157 , Flag "prof" (NoArg (addWay WayProf)) Supported
158 , Flag "ticky" (NoArg (addWay WayTicky)) Supported
159 , Flag "parallel" (NoArg (addWay WayPar)) Supported
160 , Flag "gransim" (NoArg (addWay WayGran)) Supported
161 , Flag "smp" (NoArg (addWay WayThreaded))
162 (Deprecated "Use -threaded instead")
163 , Flag "debug" (NoArg (addWay WayDebug)) Supported
164 , Flag "ndp" (NoArg (addWay WayNDP)) Supported
165 , Flag "threaded" (NoArg (addWay WayThreaded)) Supported
168 ------ Debugging ----------------------------------------------------
169 , Flag "dppr-debug" (PassFlag addOpt) Supported
170 , Flag "dsuppress-uniques" (PassFlag addOpt) Supported
171 , Flag "dppr-user-length" (AnySuffix addOpt) Supported
172 , Flag "dopt-fuel" (AnySuffix addOpt) Supported
173 , Flag "dno-debug-output" (PassFlag addOpt) Supported
174 -- rest of the debugging flags are dynamic
176 --------- Profiling --------------------------------------------------
177 , Flag "auto-all" (NoArg (addOpt "-fauto-sccs-on-all-toplevs"))
179 , Flag "auto" (NoArg (addOpt "-fauto-sccs-on-exported-toplevs"))
181 , Flag "caf-all" (NoArg (addOpt "-fauto-sccs-on-individual-cafs"))
183 -- "ignore-sccs" doesn't work (ToDo)
185 , Flag "no-auto-all" (NoArg (removeOpt "-fauto-sccs-on-all-toplevs"))
187 , Flag "no-auto" (NoArg (removeOpt "-fauto-sccs-on-exported-toplevs"))
189 , Flag "no-caf-all" (NoArg (removeOpt "-fauto-sccs-on-individual-cafs"))
192 ----- Linker --------------------------------------------------------
193 , Flag "static" (PassFlag addOpt) Supported
194 , Flag "dynamic" (NoArg (removeOpt "-static")) Supported
195 -- ignored for compat w/ gcc:
196 , Flag "rdynamic" (NoArg (return ())) Supported
198 ----- RTS opts ------------------------------------------------------
199 , Flag "H" (HasArg (setHeapSize . fromIntegral . decodeSize))
201 , Flag "Rghc-timing" (NoArg (enableTimingStats)) Supported
203 ------ Compiler flags -----------------------------------------------
204 -- All other "-fno-<blah>" options cancel out "-f<blah>" on the hsc cmdline
206 (PrefixPred (\s -> isStaticFlag ("f"++s)) (\s -> removeOpt ("-f"++s)))
209 -- Pass all remaining "-f<blah>" options to hsc
210 , Flag "f" (AnySuffixPred (isStaticFlag) addOpt)
214 addOpt :: String -> IO ()
215 addOpt = consIORef v_opt_C
217 addWay :: WayName -> IO ()
218 addWay = consIORef v_Ways
220 removeOpt :: String -> IO ()
222 fs <- readIORef v_opt_C
223 writeIORef v_opt_C $! filter (/= f) fs
225 lookUp :: FastString -> Bool
226 lookup_def_int :: String -> Int -> Int
227 lookup_def_float :: String -> Float -> Float
228 lookup_str :: String -> Maybe String
230 -- holds the static opts while they're being collected, before
231 -- being unsafely read by unpacked_static_opts below.
232 GLOBAL_VAR(v_opt_C, defaultStaticOpts, [String])
233 GLOBAL_VAR(v_opt_C_ready, False, Bool)
235 staticFlags :: [String]
236 staticFlags = unsafePerformIO $ do
237 ready <- readIORef v_opt_C_ready
239 then panic "Static flags have not been initialised!\n Please call GHC.newSession or GHC.parseStaticFlags early enough."
240 else readIORef v_opt_C
242 -- -static is the default
243 defaultStaticOpts :: [String]
244 defaultStaticOpts = ["-static"]
246 packed_static_opts :: [FastString]
247 packed_static_opts = map mkFastString staticFlags
249 lookUp sw = sw `elem` packed_static_opts
251 -- (lookup_str "foo") looks for the flag -foo=X or -fooX,
252 -- and returns the string X
254 = case firstJust (map (maybePrefixMatch sw) staticFlags) of
255 Just ('=' : str) -> Just str
259 lookup_def_int sw def = case (lookup_str sw) of
260 Nothing -> def -- Use default
261 Just xx -> try_read sw xx
263 lookup_def_float sw def = case (lookup_str sw) of
264 Nothing -> def -- Use default
265 Just xx -> try_read sw xx
268 try_read :: Read a => String -> String -> a
269 -- (try_read sw str) tries to read s; if it fails, it
270 -- bleats about flag sw
273 ((x,_):_) -> x -- Be forgiving: ignore trailing goop, and alternative parses
274 [] -> ghcError (UsageError ("Malformed argument " ++ str ++ " for flag " ++ sw))
275 -- ToDo: hack alert. We should really parse the arugments
276 -- and announce errors in a more civilised way.
280 Putting the compiler options into temporary at-files
281 may turn out to be necessary later on if we turn hsc into
282 a pure Win32 application where I think there's a command-line
283 length limit of 255. unpacked_opts understands the @ option.
285 unpacked_opts :: [String]
289 map unpackFS argv -- NOT ARGV any more: v_Static_hsc_opts
291 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
295 opt_IgnoreDotGhci :: Bool
296 opt_IgnoreDotGhci = lookUp (fsLit "-ignore-dot-ghci")
299 opt_SuppressUniques :: Bool
300 opt_SuppressUniques = lookUp (fsLit "-dsuppress-uniques")
301 opt_PprStyle_Debug :: Bool
302 opt_PprStyle_Debug = lookUp (fsLit "-dppr-debug")
303 opt_PprUserLength :: Int
304 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
306 opt_Fuel = lookup_def_int "-dopt-fuel" maxBound
307 opt_NoDebugOutput :: Bool
308 opt_NoDebugOutput = lookUp (fsLit "-dno-debug-output")
312 opt_AutoSccsOnAllToplevs :: Bool
313 opt_AutoSccsOnAllToplevs = lookUp (fsLit "-fauto-sccs-on-all-toplevs")
314 opt_AutoSccsOnExportedToplevs :: Bool
315 opt_AutoSccsOnExportedToplevs = lookUp (fsLit "-fauto-sccs-on-exported-toplevs")
316 opt_AutoSccsOnIndividualCafs :: Bool
317 opt_AutoSccsOnIndividualCafs = lookUp (fsLit "-fauto-sccs-on-individual-cafs")
318 opt_SccProfilingOn :: Bool
319 opt_SccProfilingOn = lookUp (fsLit "-fscc-profiling")
320 opt_DoTickyProfiling :: Bool
321 opt_DoTickyProfiling = WayTicky `elem` (unsafePerformIO $ readIORef v_Ways)
325 opt_Hpc = lookUp (fsLit "-fhpc")
328 opt_DictsStrict :: Bool
329 opt_DictsStrict = lookUp (fsLit "-fdicts-strict")
330 opt_IrrefutableTuples :: Bool
331 opt_IrrefutableTuples = lookUp (fsLit "-firrefutable-tuples")
333 opt_Parallel = lookUp (fsLit "-fparallel")
336 opt_DsMultiTyVar :: Bool
337 opt_DsMultiTyVar = not (lookUp (fsLit "-fno-ds-multi-tyvar"))
340 opt_SpecInlineJoinPoints :: Bool
341 opt_SpecInlineJoinPoints = lookUp (fsLit "-fspec-inline-join-points")
343 opt_NoStateHack :: Bool
344 opt_NoStateHack = lookUp (fsLit "-fno-state-hack")
346 opt_CprOff = lookUp (fsLit "-fcpr-off")
347 -- Switch off CPR analysis in the new demand analyser
348 opt_MaxWorkerArgs :: Int
349 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
351 opt_GranMacros :: Bool
352 opt_GranMacros = lookUp (fsLit "-fgransim")
353 opt_HiVersion :: Integer
354 opt_HiVersion = read (cProjectVersionInt ++ cProjectPatchLevel) :: Integer
355 opt_HistorySize :: Int
356 opt_HistorySize = lookup_def_int "-fhistory-size" 20
357 opt_OmitBlackHoling :: Bool
358 opt_OmitBlackHoling = lookUp (fsLit "-dno-black-holing")
360 -- Simplifier switches
361 opt_SimplNoPreInlining :: Bool
362 opt_SimplNoPreInlining = lookUp (fsLit "-fno-pre-inlining")
363 -- NoPreInlining is there just to see how bad things
364 -- get if you don't do it!
365 opt_SimplExcessPrecision :: Bool
366 opt_SimplExcessPrecision = lookUp (fsLit "-fexcess-precision")
369 opt_UF_CreationThreshold :: Int
370 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
371 opt_UF_UseThreshold :: Int
372 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
373 opt_UF_FunAppDiscount :: Int
374 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
375 opt_UF_KeenessFactor :: Float
376 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
379 opt_UF_DearOp = ( 4 :: Int)
382 -- Related to linking
384 #if darwin_TARGET_OS && x86_64_TARGET_ARCH
387 opt_PIC = lookUp (fsLit "-fPIC")
390 opt_Static = lookUp (fsLit "-static")
391 opt_Unregisterised :: Bool
392 opt_Unregisterised = lookUp (fsLit "-funregisterised")
394 -- Derived, not a real option. Determines whether we will be compiling
395 -- info tables that reside just before the entry code, or with an
396 -- indirection to the entry code. See TABLES_NEXT_TO_CODE in
397 -- includes/InfoTables.h.
398 tablesNextToCode :: Bool
399 tablesNextToCode = not opt_Unregisterised
400 && cGhcEnableTablesNextToCode == "YES"
402 opt_EmitExternalCore :: Bool
403 opt_EmitExternalCore = lookUp (fsLit "-fext-core")
405 -- Include full span info in error messages, instead of just the start position.
406 opt_ErrorSpans :: Bool
407 opt_ErrorSpans = lookUp (fsLit "-ferror-spans")
410 -- object files and libraries to be linked in are collected here.
411 -- ToDo: perhaps this could be done without a global, it wasn't obvious
412 -- how to do it though --SDM.
413 GLOBAL_VAR(v_Ld_inputs, [], [String])
415 isStaticFlag :: String -> Bool
418 "fauto-sccs-on-all-toplevs",
419 "fauto-sccs-on-exported-toplevs",
420 "fauto-sccs-on-individual-cafs",
423 "fspec-inline-join-points",
424 "firrefutable-tuples",
427 "fno-hi-version-check",
429 "fno-method-sharing",
431 "fno-ds-multi-tyvar",
436 "fhardwire-lib-paths",
444 || any (`isPrefixOf` f) [
445 "fliberate-case-threshold",
448 "funfolding-creation-threshold",
449 "funfolding-use-threshold",
450 "funfolding-fun-discount",
451 "funfolding-keeness-factor"
454 -----------------------------------------------------------------------------
455 -- convert sizes like "3.5M" into integers
457 decodeSize :: String -> Integer
459 | c == "" = truncate n
460 | c == "K" || c == "k" = truncate (n * 1000)
461 | c == "M" || c == "m" = truncate (n * 1000 * 1000)
462 | c == "G" || c == "g" = truncate (n * 1000 * 1000 * 1000)
463 | otherwise = throwDyn (CmdLineError ("can't decode size: " ++ str))
464 where (m, c) = span pred str
466 pred c = isDigit c || c == '.'
469 -----------------------------------------------------------------------------
472 foreign import ccall unsafe "setHeapSize" setHeapSize :: Int -> IO ()
473 foreign import ccall unsafe "enableTimingStats" enableTimingStats :: IO ()
475 -----------------------------------------------------------------------------
478 -- The central concept of a "way" is that all objects in a given
479 -- program must be compiled in the same "way". Certain options change
480 -- parameters of the virtual machine, eg. profiling adds an extra word
481 -- to the object header, so profiling objects cannot be linked with
482 -- non-profiling objects.
484 -- After parsing the command-line options, we determine which "way" we
485 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
487 -- We then find the "build-tag" associated with this way, and this
488 -- becomes the suffix used to find .hi files and libraries used in
491 GLOBAL_VAR(v_Build_tag, "", String)
493 -- The RTS has its own build tag, because there are some ways that
494 -- affect the RTS only.
495 GLOBAL_VAR(v_RTS_Build_tag, "", String)
524 GLOBAL_VAR(v_Ways, [] ,[WayName])
526 allowed_combination :: [WayName] -> Bool
527 allowed_combination way = and [ x `allowedWith` y
528 | x <- way, y <- way, x < y ]
530 -- Note ordering in these tests: the left argument is
531 -- <= the right argument, according to the Ord instance
534 -- debug is allowed with everything
535 _ `allowedWith` WayDebug = True
536 WayDebug `allowedWith` _ = True
538 WayProf `allowedWith` WayNDP = True
539 WayThreaded `allowedWith` WayProf = True
540 _ `allowedWith` _ = False
543 findBuildTag :: IO [String] -- new options
545 way_names <- readIORef v_Ways
546 let ws = sort (nub way_names)
548 if not (allowed_combination ws)
549 then throwDyn (CmdLineError $
550 "combination not supported: " ++
551 foldr1 (\a b -> a ++ '/':b)
552 (map (wayName . lkupWay) ws))
553 else let ways = map lkupWay ws
554 tag = mkBuildTag (filter (not.wayRTSOnly) ways)
555 rts_tag = mkBuildTag ways
556 flags = map wayOpts ways
558 writeIORef v_Build_tag tag
559 writeIORef v_RTS_Build_tag rts_tag
560 return (concat flags)
564 mkBuildTag :: [Way] -> String
565 mkBuildTag ways = concat (intersperse "_" (map wayTag ways))
567 lkupWay :: WayName -> Way
569 case lookup w way_details of
570 Nothing -> error "findBuildTag"
571 Just details -> details
573 isRTSWay :: WayName -> Bool
574 isRTSWay = wayRTSOnly . lkupWay
583 way_details :: [ (WayName, Way) ]
585 [ (WayThreaded, Way "thr" True "Threaded" [
586 #if defined(freebsd_TARGET_OS)
589 -- FreeBSD's default threading library is the KSE-based M:N libpthread,
590 -- which GHC has some problems with. It's currently not clear whether
591 -- the problems are our fault or theirs, but it seems that using the
592 -- alternative 1:1 threading library libthr works around it:
594 #elif defined(solaris2_TARGET_OS)
599 (WayDebug, Way "debug" True "Debug" [] ),
601 (WayProf, Way "p" False "Profiling"
604 , "-optc-DPROFILING" ]),
606 (WayTicky, Way "t" True "Ticky-ticky Profiling"
608 , "-optc-DTICKY_TICKY" ]),
610 -- optl's below to tell linker where to find the PVM library -- HWL
611 (WayPar, Way "mp" False "Parallel"
613 , "-D__PARALLEL_HASKELL__"
615 , "-package concurrent"
617 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
621 -- at the moment we only change the RTS and could share compiler and libs!
622 (WayPar, Way "mt" False "Parallel ticky profiling"
624 , "-D__PARALLEL_HASKELL__"
627 , "-package concurrent"
629 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
633 (WayPar, Way "md" False "Distributed"
635 , "-D__PARALLEL_HASKELL__"
636 , "-D__DISTRIBUTED_HASKELL__"
639 , "-package concurrent"
641 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
645 (WayGran, Way "mg" False "GranSim"
649 , "-package concurrent" ]),
651 (WayNDP, Way "ndp" False "Nested data parallelism"
655 (WayUser_a, Way "a" False "User way 'a'" ["$WAY_a_REAL_OPTS"]),
656 (WayUser_b, Way "b" False "User way 'b'" ["$WAY_b_REAL_OPTS"]),
657 (WayUser_c, Way "c" False "User way 'c'" ["$WAY_c_REAL_OPTS"]),
658 (WayUser_d, Way "d" False "User way 'd'" ["$WAY_d_REAL_OPTS"]),
659 (WayUser_e, Way "e" False "User way 'e'" ["$WAY_e_REAL_OPTS"]),
660 (WayUser_f, Way "f" False "User way 'f'" ["$WAY_f_REAL_OPTS"]),
661 (WayUser_g, Way "g" False "User way 'g'" ["$WAY_g_REAL_OPTS"]),
662 (WayUser_h, Way "h" False "User way 'h'" ["$WAY_h_REAL_OPTS"]),
663 (WayUser_i, Way "i" False "User way 'i'" ["$WAY_i_REAL_OPTS"]),
664 (WayUser_j, Way "j" False "User way 'j'" ["$WAY_j_REAL_OPTS"]),
665 (WayUser_k, Way "k" False "User way 'k'" ["$WAY_k_REAL_OPTS"]),
666 (WayUser_l, Way "l" False "User way 'l'" ["$WAY_l_REAL_OPTS"]),
667 (WayUser_m, Way "m" False "User way 'm'" ["$WAY_m_REAL_OPTS"]),
668 (WayUser_n, Way "n" False "User way 'n'" ["$WAY_n_REAL_OPTS"]),
669 (WayUser_o, Way "o" False "User way 'o'" ["$WAY_o_REAL_OPTS"]),
670 (WayUser_A, Way "A" False "User way 'A'" ["$WAY_A_REAL_OPTS"]),
671 (WayUser_B, Way "B" False "User way 'B'" ["$WAY_B_REAL_OPTS"])
674 unregFlags :: [String]
677 , "-optc-DUSE_MINIINTERPRETER"
678 , "-fno-asm-mangling"