1 {-# OPTIONS -fno-cse #-}
2 -- -fno-cse is needed for GLOBAL_VAR's to behave properly
4 -----------------------------------------------------------------------------
8 -- Static flags can only be set once, on the command-line. Inside GHC,
9 -- each static flag corresponds to a top-level value, usually of type Bool.
11 -- (c) The University of Glasgow 2005
13 -----------------------------------------------------------------------------
21 WayName(..), v_Ways, v_Build_tag, v_RTS_Build_tag, isRTSWay,
23 -- Output style options
30 opt_AutoSccsOnAllToplevs,
31 opt_AutoSccsOnExportedToplevs,
32 opt_AutoSccsOnIndividualCafs,
41 opt_IrrefutableTuples,
47 opt_SpecInlineJoinPoints,
49 opt_SimplNoPreInlining,
50 opt_SimplExcessPrecision,
54 opt_UF_CreationThreshold,
56 opt_UF_FunAppDiscount,
60 -- Optimization fuel controls
80 #include "HsVersions.h"
86 import Maybes ( firstJust )
89 import Control.Exception ( throwDyn )
91 import System.IO.Unsafe ( unsafePerformIO )
92 import Control.Monad ( when )
93 import Data.Char ( isDigit )
96 -----------------------------------------------------------------------------
99 parseStaticFlags :: [String] -> IO ([String], [String])
100 parseStaticFlags args = do
101 ready <- readIORef v_opt_C_ready
102 when ready $ throwDyn (ProgramError "Too late for parseStaticFlags: call it before newSession")
104 (leftover, errs, warns1) <- processArgs static_flags args
105 when (not (null errs)) $ throwDyn (UsageError (unlines errs))
107 -- deal with the way flags: the way (eg. prof) gives rise to
108 -- further flags, some of which might be static.
109 way_flags <- findBuildTag
111 -- if we're unregisterised, add some more flags
112 let unreg_flags | cGhcUnregisterised == "YES" = unregFlags
115 (more_leftover, errs, warns2) <- processArgs static_flags (unreg_flags ++ way_flags)
117 -- see sanity code in staticOpts
118 writeIORef v_opt_C_ready True
120 -- TABLES_NEXT_TO_CODE affects the info table layout.
121 -- Be careful to do this *after* all processArgs,
122 -- because evaluating tablesNextToCode involves looking at the global
123 -- static flags. Those pesky global variables...
124 let cg_flags | tablesNextToCode = ["-optc-DTABLES_NEXT_TO_CODE"]
127 -- HACK: -fexcess-precision is both a static and a dynamic flag. If
128 -- the static flag parser has slurped it, we must return it as a
129 -- leftover too. ToDo: make -fexcess-precision dynamic only.
130 let excess_prec | opt_SimplExcessPrecision = ["-fexcess-precision"]
133 when (not (null errs)) $ ghcError (UsageError (unlines errs))
134 return (excess_prec ++ cg_flags ++ more_leftover ++ leftover,
137 initStaticOpts :: IO ()
138 initStaticOpts = writeIORef v_opt_C_ready True
140 static_flags :: [Flag IO]
141 -- All the static flags should appear in this list. It describes how each
142 -- static flag should be processed. Two main purposes:
143 -- (a) if a command-line flag doesn't appear in the list, GHC can complain
144 -- (b) a command-line flag may remove, or add, other flags; e.g. the "-fno-X" things
146 -- The common (PassFlag addOpt) action puts the static flag into the bunch of
147 -- things that are searched up by the top-level definitions like
148 -- opt_foo = lookUp (fsLit "-dfoo")
150 -- Note that ordering is important in the following list: any flag which
151 -- is a prefix flag (i.e. HasArg, Prefix, OptPrefix, AnySuffix) will override
152 -- flags further down the list with the same prefix.
155 ------- GHCi -------------------------------------------------------
156 Flag "ignore-dot-ghci" (PassFlag addOpt) Supported
157 , Flag "read-dot-ghci" (NoArg (removeOpt "-ignore-dot-ghci")) Supported
159 ------- ways --------------------------------------------------------
160 , Flag "prof" (NoArg (addWay WayProf)) Supported
161 , Flag "ticky" (NoArg (addWay WayTicky)) Supported
162 , Flag "parallel" (NoArg (addWay WayPar)) Supported
163 , Flag "gransim" (NoArg (addWay WayGran)) Supported
164 , Flag "smp" (NoArg (addWay WayThreaded))
165 (Deprecated "Use -threaded instead")
166 , Flag "debug" (NoArg (addWay WayDebug)) Supported
167 , Flag "ndp" (NoArg (addWay WayNDP)) Supported
168 , Flag "threaded" (NoArg (addWay WayThreaded)) Supported
171 ------ Debugging ----------------------------------------------------
172 , Flag "dppr-debug" (PassFlag addOpt) Supported
173 , Flag "dsuppress-uniques" (PassFlag addOpt) Supported
174 , Flag "dppr-user-length" (AnySuffix addOpt) Supported
175 , Flag "dopt-fuel" (AnySuffix addOpt) Supported
176 , Flag "dno-debug-output" (PassFlag addOpt) Supported
177 -- rest of the debugging flags are dynamic
179 --------- Profiling --------------------------------------------------
180 , Flag "auto-all" (NoArg (addOpt "-fauto-sccs-on-all-toplevs"))
182 , Flag "auto" (NoArg (addOpt "-fauto-sccs-on-exported-toplevs"))
184 , Flag "caf-all" (NoArg (addOpt "-fauto-sccs-on-individual-cafs"))
186 -- "ignore-sccs" doesn't work (ToDo)
188 , Flag "no-auto-all" (NoArg (removeOpt "-fauto-sccs-on-all-toplevs"))
190 , Flag "no-auto" (NoArg (removeOpt "-fauto-sccs-on-exported-toplevs"))
192 , Flag "no-caf-all" (NoArg (removeOpt "-fauto-sccs-on-individual-cafs"))
195 ----- Linker --------------------------------------------------------
196 , Flag "static" (PassFlag addOpt) Supported
197 , Flag "dynamic" (NoArg (removeOpt "-static")) Supported
198 -- ignored for compat w/ gcc:
199 , Flag "rdynamic" (NoArg (return ())) Supported
201 ----- RTS opts ------------------------------------------------------
202 , Flag "H" (HasArg (setHeapSize . fromIntegral . decodeSize))
204 , Flag "Rghc-timing" (NoArg (enableTimingStats)) Supported
206 ------ Compiler flags -----------------------------------------------
207 -- All other "-fno-<blah>" options cancel out "-f<blah>" on the hsc cmdline
209 (PrefixPred (\s -> isStaticFlag ("f"++s)) (\s -> removeOpt ("-f"++s)))
212 -- Pass all remaining "-f<blah>" options to hsc
213 , Flag "f" (AnySuffixPred (isStaticFlag) addOpt)
217 addOpt :: String -> IO ()
218 addOpt = consIORef v_opt_C
220 addWay :: WayName -> IO ()
221 addWay = consIORef v_Ways
223 removeOpt :: String -> IO ()
225 fs <- readIORef v_opt_C
226 writeIORef v_opt_C $! filter (/= f) fs
228 lookUp :: FastString -> Bool
229 lookup_def_int :: String -> Int -> Int
230 lookup_def_float :: String -> Float -> Float
231 lookup_str :: String -> Maybe String
233 -- holds the static opts while they're being collected, before
234 -- being unsafely read by unpacked_static_opts below.
235 GLOBAL_VAR(v_opt_C, defaultStaticOpts, [String])
236 GLOBAL_VAR(v_opt_C_ready, False, Bool)
238 staticFlags :: [String]
239 staticFlags = unsafePerformIO $ do
240 ready <- readIORef v_opt_C_ready
242 then panic "Static flags have not been initialised!\n Please call GHC.newSession or GHC.parseStaticFlags early enough."
243 else readIORef v_opt_C
245 -- -static is the default
246 defaultStaticOpts :: [String]
247 defaultStaticOpts = ["-static"]
249 packed_static_opts :: [FastString]
250 packed_static_opts = map mkFastString staticFlags
252 lookUp sw = sw `elem` packed_static_opts
254 -- (lookup_str "foo") looks for the flag -foo=X or -fooX,
255 -- and returns the string X
257 = case firstJust (map (maybePrefixMatch sw) staticFlags) of
258 Just ('=' : str) -> Just str
262 lookup_def_int sw def = case (lookup_str sw) of
263 Nothing -> def -- Use default
264 Just xx -> try_read sw xx
266 lookup_def_float sw def = case (lookup_str sw) of
267 Nothing -> def -- Use default
268 Just xx -> try_read sw xx
271 try_read :: Read a => String -> String -> a
272 -- (try_read sw str) tries to read s; if it fails, it
273 -- bleats about flag sw
276 ((x,_):_) -> x -- Be forgiving: ignore trailing goop, and alternative parses
277 [] -> ghcError (UsageError ("Malformed argument " ++ str ++ " for flag " ++ sw))
278 -- ToDo: hack alert. We should really parse the arugments
279 -- and announce errors in a more civilised way.
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.
288 unpacked_opts :: [String]
292 map unpackFS argv -- NOT ARGV any more: v_Static_hsc_opts
294 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
298 opt_IgnoreDotGhci :: Bool
299 opt_IgnoreDotGhci = lookUp (fsLit "-ignore-dot-ghci")
302 opt_SuppressUniques :: Bool
303 opt_SuppressUniques = lookUp (fsLit "-dsuppress-uniques")
304 opt_PprStyle_Debug :: Bool
305 opt_PprStyle_Debug = lookUp (fsLit "-dppr-debug")
306 opt_PprUserLength :: Int
307 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
309 opt_Fuel = lookup_def_int "-dopt-fuel" maxBound
310 opt_NoDebugOutput :: Bool
311 opt_NoDebugOutput = lookUp (fsLit "-dno-debug-output")
315 opt_AutoSccsOnAllToplevs :: Bool
316 opt_AutoSccsOnAllToplevs = lookUp (fsLit "-fauto-sccs-on-all-toplevs")
317 opt_AutoSccsOnExportedToplevs :: Bool
318 opt_AutoSccsOnExportedToplevs = lookUp (fsLit "-fauto-sccs-on-exported-toplevs")
319 opt_AutoSccsOnIndividualCafs :: Bool
320 opt_AutoSccsOnIndividualCafs = lookUp (fsLit "-fauto-sccs-on-individual-cafs")
321 opt_SccProfilingOn :: Bool
322 opt_SccProfilingOn = lookUp (fsLit "-fscc-profiling")
323 opt_DoTickyProfiling :: Bool
324 opt_DoTickyProfiling = WayTicky `elem` (unsafePerformIO $ readIORef v_Ways)
328 opt_Hpc = lookUp (fsLit "-fhpc")
331 opt_DictsStrict :: Bool
332 opt_DictsStrict = lookUp (fsLit "-fdicts-strict")
333 opt_IrrefutableTuples :: Bool
334 opt_IrrefutableTuples = lookUp (fsLit "-firrefutable-tuples")
336 opt_Parallel = lookUp (fsLit "-fparallel")
339 opt_DsMultiTyVar :: Bool
340 opt_DsMultiTyVar = not (lookUp (fsLit "-fno-ds-multi-tyvar"))
343 opt_SpecInlineJoinPoints :: Bool
344 opt_SpecInlineJoinPoints = lookUp (fsLit "-fspec-inline-join-points")
346 opt_NoStateHack :: Bool
347 opt_NoStateHack = lookUp (fsLit "-fno-state-hack")
349 opt_CprOff = lookUp (fsLit "-fcpr-off")
350 -- Switch off CPR analysis in the new demand analyser
351 opt_MaxWorkerArgs :: Int
352 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
354 opt_GranMacros :: Bool
355 opt_GranMacros = lookUp (fsLit "-fgransim")
356 opt_HiVersion :: Integer
357 opt_HiVersion = read (cProjectVersionInt ++ cProjectPatchLevel) :: Integer
358 opt_HistorySize :: Int
359 opt_HistorySize = lookup_def_int "-fhistory-size" 20
360 opt_OmitBlackHoling :: Bool
361 opt_OmitBlackHoling = lookUp (fsLit "-dno-black-holing")
363 -- Simplifier switches
364 opt_SimplNoPreInlining :: Bool
365 opt_SimplNoPreInlining = lookUp (fsLit "-fno-pre-inlining")
366 -- NoPreInlining is there just to see how bad things
367 -- get if you don't do it!
368 opt_SimplExcessPrecision :: Bool
369 opt_SimplExcessPrecision = lookUp (fsLit "-fexcess-precision")
372 opt_UF_CreationThreshold :: Int
373 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
374 opt_UF_UseThreshold :: Int
375 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
376 opt_UF_FunAppDiscount :: Int
377 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
378 opt_UF_KeenessFactor :: Float
379 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
382 opt_UF_DearOp = ( 4 :: Int)
385 -- Related to linking
387 #if darwin_TARGET_OS && x86_64_TARGET_ARCH
390 opt_PIC = lookUp (fsLit "-fPIC")
393 opt_Static = lookUp (fsLit "-static")
394 opt_Unregisterised :: Bool
395 opt_Unregisterised = lookUp (fsLit "-funregisterised")
397 -- Derived, not a real option. Determines whether we will be compiling
398 -- info tables that reside just before the entry code, or with an
399 -- indirection to the entry code. See TABLES_NEXT_TO_CODE in
400 -- includes/InfoTables.h.
401 tablesNextToCode :: Bool
402 tablesNextToCode = not opt_Unregisterised
403 && cGhcEnableTablesNextToCode == "YES"
405 opt_EmitExternalCore :: Bool
406 opt_EmitExternalCore = lookUp (fsLit "-fext-core")
408 -- Include full span info in error messages, instead of just the start position.
409 opt_ErrorSpans :: Bool
410 opt_ErrorSpans = lookUp (fsLit "-ferror-spans")
413 -- object files and libraries to be linked in are collected here.
414 -- ToDo: perhaps this could be done without a global, it wasn't obvious
415 -- how to do it though --SDM.
416 GLOBAL_VAR(v_Ld_inputs, [], [String])
418 isStaticFlag :: String -> Bool
421 "fauto-sccs-on-all-toplevs",
422 "fauto-sccs-on-exported-toplevs",
423 "fauto-sccs-on-individual-cafs",
426 "fspec-inline-join-points",
427 "firrefutable-tuples",
430 "fno-hi-version-check",
432 "fno-method-sharing",
434 "fno-ds-multi-tyvar",
439 "fhardwire-lib-paths",
447 || any (`isPrefixOf` f) [
448 "fliberate-case-threshold",
451 "funfolding-creation-threshold",
452 "funfolding-use-threshold",
453 "funfolding-fun-discount",
454 "funfolding-keeness-factor"
457 -----------------------------------------------------------------------------
458 -- convert sizes like "3.5M" into integers
460 decodeSize :: String -> Integer
462 | c == "" = truncate n
463 | c == "K" || c == "k" = truncate (n * 1000)
464 | c == "M" || c == "m" = truncate (n * 1000 * 1000)
465 | c == "G" || c == "g" = truncate (n * 1000 * 1000 * 1000)
466 | otherwise = throwDyn (CmdLineError ("can't decode size: " ++ str))
467 where (m, c) = span pred str
469 pred c = isDigit c || c == '.'
472 -----------------------------------------------------------------------------
475 foreign import ccall unsafe "setHeapSize" setHeapSize :: Int -> IO ()
476 foreign import ccall unsafe "enableTimingStats" enableTimingStats :: IO ()
478 -----------------------------------------------------------------------------
481 -- The central concept of a "way" is that all objects in a given
482 -- program must be compiled in the same "way". Certain options change
483 -- parameters of the virtual machine, eg. profiling adds an extra word
484 -- to the object header, so profiling objects cannot be linked with
485 -- non-profiling objects.
487 -- After parsing the command-line options, we determine which "way" we
488 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
490 -- We then find the "build-tag" associated with this way, and this
491 -- becomes the suffix used to find .hi files and libraries used in
494 GLOBAL_VAR(v_Build_tag, "", String)
496 -- The RTS has its own build tag, because there are some ways that
497 -- affect the RTS only.
498 GLOBAL_VAR(v_RTS_Build_tag, "", String)
527 GLOBAL_VAR(v_Ways, [] ,[WayName])
529 allowed_combination :: [WayName] -> Bool
530 allowed_combination way = and [ x `allowedWith` y
531 | x <- way, y <- way, x < y ]
533 -- Note ordering in these tests: the left argument is
534 -- <= the right argument, according to the Ord instance
537 -- debug is allowed with everything
538 _ `allowedWith` WayDebug = True
539 WayDebug `allowedWith` _ = True
541 WayProf `allowedWith` WayNDP = True
542 WayThreaded `allowedWith` WayProf = True
543 _ `allowedWith` _ = False
546 findBuildTag :: IO [String] -- new options
548 way_names <- readIORef v_Ways
549 let ws = sort (nub way_names)
551 if not (allowed_combination ws)
552 then throwDyn (CmdLineError $
553 "combination not supported: " ++
554 foldr1 (\a b -> a ++ '/':b)
555 (map (wayName . lkupWay) ws))
556 else let ways = map lkupWay ws
557 tag = mkBuildTag (filter (not.wayRTSOnly) ways)
558 rts_tag = mkBuildTag ways
559 flags = map wayOpts ways
561 writeIORef v_Build_tag tag
562 writeIORef v_RTS_Build_tag rts_tag
563 return (concat flags)
567 mkBuildTag :: [Way] -> String
568 mkBuildTag ways = concat (intersperse "_" (map wayTag ways))
570 lkupWay :: WayName -> Way
572 case lookup w way_details of
573 Nothing -> error "findBuildTag"
574 Just details -> details
576 isRTSWay :: WayName -> Bool
577 isRTSWay = wayRTSOnly . lkupWay
586 way_details :: [ (WayName, Way) ]
588 [ (WayThreaded, Way "thr" True "Threaded" [
589 #if defined(freebsd_TARGET_OS)
592 -- FreeBSD's default threading library is the KSE-based M:N libpthread,
593 -- which GHC has some problems with. It's currently not clear whether
594 -- the problems are our fault or theirs, but it seems that using the
595 -- alternative 1:1 threading library libthr works around it:
597 #elif defined(solaris2_TARGET_OS)
602 (WayDebug, Way "debug" True "Debug" [] ),
604 (WayProf, Way "p" False "Profiling"
607 , "-optc-DPROFILING" ]),
609 (WayTicky, Way "t" True "Ticky-ticky Profiling"
611 , "-optc-DTICKY_TICKY" ]),
613 -- optl's below to tell linker where to find the PVM library -- HWL
614 (WayPar, Way "mp" False "Parallel"
616 , "-D__PARALLEL_HASKELL__"
618 , "-package concurrent"
620 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
624 -- at the moment we only change the RTS and could share compiler and libs!
625 (WayPar, Way "mt" False "Parallel ticky profiling"
627 , "-D__PARALLEL_HASKELL__"
630 , "-package concurrent"
632 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
636 (WayPar, Way "md" False "Distributed"
638 , "-D__PARALLEL_HASKELL__"
639 , "-D__DISTRIBUTED_HASKELL__"
642 , "-package concurrent"
644 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
648 (WayGran, Way "mg" False "GranSim"
652 , "-package concurrent" ]),
654 (WayNDP, Way "ndp" False "Nested data parallelism"
658 (WayUser_a, Way "a" False "User way 'a'" ["$WAY_a_REAL_OPTS"]),
659 (WayUser_b, Way "b" False "User way 'b'" ["$WAY_b_REAL_OPTS"]),
660 (WayUser_c, Way "c" False "User way 'c'" ["$WAY_c_REAL_OPTS"]),
661 (WayUser_d, Way "d" False "User way 'd'" ["$WAY_d_REAL_OPTS"]),
662 (WayUser_e, Way "e" False "User way 'e'" ["$WAY_e_REAL_OPTS"]),
663 (WayUser_f, Way "f" False "User way 'f'" ["$WAY_f_REAL_OPTS"]),
664 (WayUser_g, Way "g" False "User way 'g'" ["$WAY_g_REAL_OPTS"]),
665 (WayUser_h, Way "h" False "User way 'h'" ["$WAY_h_REAL_OPTS"]),
666 (WayUser_i, Way "i" False "User way 'i'" ["$WAY_i_REAL_OPTS"]),
667 (WayUser_j, Way "j" False "User way 'j'" ["$WAY_j_REAL_OPTS"]),
668 (WayUser_k, Way "k" False "User way 'k'" ["$WAY_k_REAL_OPTS"]),
669 (WayUser_l, Way "l" False "User way 'l'" ["$WAY_l_REAL_OPTS"]),
670 (WayUser_m, Way "m" False "User way 'm'" ["$WAY_m_REAL_OPTS"]),
671 (WayUser_n, Way "n" False "User way 'n'" ["$WAY_n_REAL_OPTS"]),
672 (WayUser_o, Way "o" False "User way 'o'" ["$WAY_o_REAL_OPTS"]),
673 (WayUser_A, Way "A" False "User way 'A'" ["$WAY_A_REAL_OPTS"]),
674 (WayUser_B, Way "B" False "User way 'B'" ["$WAY_B_REAL_OPTS"])
677 unregFlags :: [String]
680 , "-optc-DUSE_MINIINTERPRETER"
681 , "-fno-asm-mangling"