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 )
90 import System.IO.Unsafe ( unsafePerformIO )
91 import Control.Monad ( when )
92 import Data.Char ( isDigit )
95 -----------------------------------------------------------------------------
98 parseStaticFlags :: [String] -> IO ([String], [String])
99 parseStaticFlags args = do
100 ready <- readIORef v_opt_C_ready
101 when ready $ ghcError (ProgramError "Too late for parseStaticFlags: call it before newSession")
103 (leftover, errs, warns1) <- processArgs static_flags args
104 when (not (null errs)) $ ghcError (UsageError (unlines errs))
106 -- deal with the way flags: the way (eg. prof) gives rise to
107 -- further flags, some of which might be static.
108 way_flags <- findBuildTag
110 -- if we're unregisterised, add some more flags
111 let unreg_flags | cGhcUnregisterised == "YES" = unregFlags
114 (more_leftover, errs, warns2) <- processArgs static_flags (unreg_flags ++ way_flags)
116 -- see sanity code in staticOpts
117 writeIORef v_opt_C_ready True
119 -- TABLES_NEXT_TO_CODE affects the info table layout.
120 -- Be careful to do this *after* all processArgs,
121 -- because evaluating tablesNextToCode involves looking at the global
122 -- static flags. Those pesky global variables...
123 let cg_flags | tablesNextToCode = ["-optc-DTABLES_NEXT_TO_CODE"]
126 -- HACK: -fexcess-precision is both a static and a dynamic flag. If
127 -- the static flag parser has slurped it, we must return it as a
128 -- leftover too. ToDo: make -fexcess-precision dynamic only.
129 let excess_prec | opt_SimplExcessPrecision = ["-fexcess-precision"]
132 when (not (null errs)) $ ghcError (UsageError (unlines errs))
133 return (excess_prec ++ cg_flags ++ more_leftover ++ leftover,
136 initStaticOpts :: IO ()
137 initStaticOpts = writeIORef v_opt_C_ready True
139 static_flags :: [Flag IO]
140 -- All the static flags should appear in this list. It describes how each
141 -- static flag should be processed. Two main purposes:
142 -- (a) if a command-line flag doesn't appear in the list, GHC can complain
143 -- (b) a command-line flag may remove, or add, other flags; e.g. the "-fno-X" things
145 -- The common (PassFlag addOpt) action puts the static flag into the bunch of
146 -- things that are searched up by the top-level definitions like
147 -- opt_foo = lookUp (fsLit "-dfoo")
149 -- Note that ordering is important in the following list: any flag which
150 -- is a prefix flag (i.e. HasArg, Prefix, OptPrefix, AnySuffix) will override
151 -- flags further down the list with the same prefix.
154 ------- GHCi -------------------------------------------------------
155 Flag "ignore-dot-ghci" (PassFlag addOpt) Supported
156 , Flag "read-dot-ghci" (NoArg (removeOpt "-ignore-dot-ghci")) Supported
158 ------- ways --------------------------------------------------------
159 , Flag "prof" (NoArg (addWay WayProf)) Supported
160 , Flag "ticky" (NoArg (addWay WayTicky)) Supported
161 , Flag "parallel" (NoArg (addWay WayPar)) Supported
162 , Flag "gransim" (NoArg (addWay WayGran)) Supported
163 , Flag "smp" (NoArg (addWay WayThreaded))
164 (Deprecated "Use -threaded instead")
165 , Flag "debug" (NoArg (addWay WayDebug)) Supported
166 , Flag "ndp" (NoArg (addWay WayNDP)) Supported
167 , Flag "threaded" (NoArg (addWay WayThreaded)) Supported
170 ------ Debugging ----------------------------------------------------
171 , Flag "dppr-debug" (PassFlag addOpt) Supported
172 , Flag "dsuppress-uniques" (PassFlag addOpt) Supported
173 , Flag "dppr-user-length" (AnySuffix addOpt) Supported
174 , Flag "dopt-fuel" (AnySuffix addOpt) Supported
175 , Flag "dno-debug-output" (PassFlag addOpt) Supported
176 -- rest of the debugging flags are dynamic
178 --------- Profiling --------------------------------------------------
179 , Flag "auto-all" (NoArg (addOpt "-fauto-sccs-on-all-toplevs"))
181 , Flag "auto" (NoArg (addOpt "-fauto-sccs-on-exported-toplevs"))
183 , Flag "caf-all" (NoArg (addOpt "-fauto-sccs-on-individual-cafs"))
185 -- "ignore-sccs" doesn't work (ToDo)
187 , Flag "no-auto-all" (NoArg (removeOpt "-fauto-sccs-on-all-toplevs"))
189 , Flag "no-auto" (NoArg (removeOpt "-fauto-sccs-on-exported-toplevs"))
191 , Flag "no-caf-all" (NoArg (removeOpt "-fauto-sccs-on-individual-cafs"))
194 ----- Linker --------------------------------------------------------
195 , Flag "static" (PassFlag addOpt) Supported
196 , Flag "dynamic" (NoArg (removeOpt "-static")) Supported
197 -- ignored for compat w/ gcc:
198 , Flag "rdynamic" (NoArg (return ())) Supported
200 ----- RTS opts ------------------------------------------------------
201 , Flag "H" (HasArg (setHeapSize . fromIntegral . decodeSize))
203 , Flag "Rghc-timing" (NoArg (enableTimingStats)) Supported
205 ------ Compiler flags -----------------------------------------------
206 -- All other "-fno-<blah>" options cancel out "-f<blah>" on the hsc cmdline
208 (PrefixPred (\s -> isStaticFlag ("f"++s)) (\s -> removeOpt ("-f"++s)))
211 -- Pass all remaining "-f<blah>" options to hsc
212 , Flag "f" (AnySuffixPred (isStaticFlag) addOpt)
216 addOpt :: String -> IO ()
217 addOpt = consIORef v_opt_C
219 addWay :: WayName -> IO ()
220 addWay = consIORef v_Ways
222 removeOpt :: String -> IO ()
224 fs <- readIORef v_opt_C
225 writeIORef v_opt_C $! filter (/= f) fs
227 lookUp :: FastString -> Bool
228 lookup_def_int :: String -> Int -> Int
229 lookup_def_float :: String -> Float -> Float
230 lookup_str :: String -> Maybe String
232 -- holds the static opts while they're being collected, before
233 -- being unsafely read by unpacked_static_opts below.
234 GLOBAL_VAR(v_opt_C, defaultStaticOpts, [String])
235 GLOBAL_VAR(v_opt_C_ready, False, Bool)
237 staticFlags :: [String]
238 staticFlags = unsafePerformIO $ do
239 ready <- readIORef v_opt_C_ready
241 then panic "Static flags have not been initialised!\n Please call GHC.newSession or GHC.parseStaticFlags early enough."
242 else readIORef v_opt_C
244 -- -static is the default
245 defaultStaticOpts :: [String]
246 defaultStaticOpts = ["-static"]
248 packed_static_opts :: [FastString]
249 packed_static_opts = map mkFastString staticFlags
251 lookUp sw = sw `elem` packed_static_opts
253 -- (lookup_str "foo") looks for the flag -foo=X or -fooX,
254 -- and returns the string X
256 = case firstJust (map (maybePrefixMatch sw) staticFlags) of
257 Just ('=' : str) -> Just str
261 lookup_def_int sw def = case (lookup_str sw) of
262 Nothing -> def -- Use default
263 Just xx -> try_read sw xx
265 lookup_def_float sw def = case (lookup_str sw) of
266 Nothing -> def -- Use default
267 Just xx -> try_read sw xx
270 try_read :: Read a => String -> String -> a
271 -- (try_read sw str) tries to read s; if it fails, it
272 -- bleats about flag sw
275 ((x,_):_) -> x -- Be forgiving: ignore trailing goop, and alternative parses
276 [] -> ghcError (UsageError ("Malformed argument " ++ str ++ " for flag " ++ sw))
277 -- ToDo: hack alert. We should really parse the arugments
278 -- and announce errors in a more civilised way.
282 Putting the compiler options into temporary at-files
283 may turn out to be necessary later on if we turn hsc into
284 a pure Win32 application where I think there's a command-line
285 length limit of 255. unpacked_opts understands the @ option.
287 unpacked_opts :: [String]
291 map unpackFS argv -- NOT ARGV any more: v_Static_hsc_opts
293 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
297 opt_IgnoreDotGhci :: Bool
298 opt_IgnoreDotGhci = lookUp (fsLit "-ignore-dot-ghci")
301 opt_SuppressUniques :: Bool
302 opt_SuppressUniques = lookUp (fsLit "-dsuppress-uniques")
303 opt_PprStyle_Debug :: Bool
304 opt_PprStyle_Debug = lookUp (fsLit "-dppr-debug")
305 opt_PprUserLength :: Int
306 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
308 opt_Fuel = lookup_def_int "-dopt-fuel" maxBound
309 opt_NoDebugOutput :: Bool
310 opt_NoDebugOutput = lookUp (fsLit "-dno-debug-output")
314 opt_AutoSccsOnAllToplevs :: Bool
315 opt_AutoSccsOnAllToplevs = lookUp (fsLit "-fauto-sccs-on-all-toplevs")
316 opt_AutoSccsOnExportedToplevs :: Bool
317 opt_AutoSccsOnExportedToplevs = lookUp (fsLit "-fauto-sccs-on-exported-toplevs")
318 opt_AutoSccsOnIndividualCafs :: Bool
319 opt_AutoSccsOnIndividualCafs = lookUp (fsLit "-fauto-sccs-on-individual-cafs")
320 opt_SccProfilingOn :: Bool
321 opt_SccProfilingOn = lookUp (fsLit "-fscc-profiling")
322 opt_DoTickyProfiling :: Bool
323 opt_DoTickyProfiling = WayTicky `elem` (unsafePerformIO $ readIORef v_Ways)
327 opt_Hpc = lookUp (fsLit "-fhpc")
330 opt_DictsStrict :: Bool
331 opt_DictsStrict = lookUp (fsLit "-fdicts-strict")
332 opt_IrrefutableTuples :: Bool
333 opt_IrrefutableTuples = lookUp (fsLit "-firrefutable-tuples")
335 opt_Parallel = lookUp (fsLit "-fparallel")
338 opt_DsMultiTyVar :: Bool
339 opt_DsMultiTyVar = not (lookUp (fsLit "-fno-ds-multi-tyvar"))
342 opt_SpecInlineJoinPoints :: Bool
343 opt_SpecInlineJoinPoints = lookUp (fsLit "-fspec-inline-join-points")
345 opt_NoStateHack :: Bool
346 opt_NoStateHack = lookUp (fsLit "-fno-state-hack")
348 opt_CprOff = lookUp (fsLit "-fcpr-off")
349 -- Switch off CPR analysis in the new demand analyser
350 opt_MaxWorkerArgs :: Int
351 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
353 opt_GranMacros :: Bool
354 opt_GranMacros = lookUp (fsLit "-fgransim")
355 opt_HiVersion :: Integer
356 opt_HiVersion = read (cProjectVersionInt ++ cProjectPatchLevel) :: Integer
357 opt_HistorySize :: Int
358 opt_HistorySize = lookup_def_int "-fhistory-size" 20
359 opt_OmitBlackHoling :: Bool
360 opt_OmitBlackHoling = lookUp (fsLit "-dno-black-holing")
362 -- Simplifier switches
363 opt_SimplNoPreInlining :: Bool
364 opt_SimplNoPreInlining = lookUp (fsLit "-fno-pre-inlining")
365 -- NoPreInlining is there just to see how bad things
366 -- get if you don't do it!
367 opt_SimplExcessPrecision :: Bool
368 opt_SimplExcessPrecision = lookUp (fsLit "-fexcess-precision")
371 opt_UF_CreationThreshold :: Int
372 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
373 opt_UF_UseThreshold :: Int
374 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
375 opt_UF_FunAppDiscount :: Int
376 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
377 opt_UF_KeenessFactor :: Float
378 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
381 opt_UF_DearOp = ( 4 :: Int)
384 -- Related to linking
386 #if darwin_TARGET_OS && x86_64_TARGET_ARCH
389 opt_PIC = lookUp (fsLit "-fPIC")
392 opt_Static = lookUp (fsLit "-static")
393 opt_Unregisterised :: Bool
394 opt_Unregisterised = lookUp (fsLit "-funregisterised")
396 -- Derived, not a real option. Determines whether we will be compiling
397 -- info tables that reside just before the entry code, or with an
398 -- indirection to the entry code. See TABLES_NEXT_TO_CODE in
399 -- includes/InfoTables.h.
400 tablesNextToCode :: Bool
401 tablesNextToCode = not opt_Unregisterised
402 && cGhcEnableTablesNextToCode == "YES"
404 opt_EmitExternalCore :: Bool
405 opt_EmitExternalCore = lookUp (fsLit "-fext-core")
407 -- Include full span info in error messages, instead of just the start position.
408 opt_ErrorSpans :: Bool
409 opt_ErrorSpans = lookUp (fsLit "-ferror-spans")
412 -- object files and libraries to be linked in are collected here.
413 -- ToDo: perhaps this could be done without a global, it wasn't obvious
414 -- how to do it though --SDM.
415 GLOBAL_VAR(v_Ld_inputs, [], [String])
417 isStaticFlag :: String -> Bool
420 "fauto-sccs-on-all-toplevs",
421 "fauto-sccs-on-exported-toplevs",
422 "fauto-sccs-on-individual-cafs",
425 "fspec-inline-join-points",
426 "firrefutable-tuples",
429 "fno-hi-version-check",
431 "fno-method-sharing",
433 "fno-ds-multi-tyvar",
438 "fhardwire-lib-paths",
446 || any (`isPrefixOf` f) [
447 "fliberate-case-threshold",
450 "funfolding-creation-threshold",
451 "funfolding-use-threshold",
452 "funfolding-fun-discount",
453 "funfolding-keeness-factor"
456 -----------------------------------------------------------------------------
457 -- convert sizes like "3.5M" into integers
459 decodeSize :: String -> Integer
461 | c == "" = truncate n
462 | c == "K" || c == "k" = truncate (n * 1000)
463 | c == "M" || c == "m" = truncate (n * 1000 * 1000)
464 | c == "G" || c == "g" = truncate (n * 1000 * 1000 * 1000)
465 | otherwise = ghcError (CmdLineError ("can't decode size: " ++ str))
466 where (m, c) = span pred str
468 pred c = isDigit c || c == '.'
471 -----------------------------------------------------------------------------
474 foreign import ccall unsafe "setHeapSize" setHeapSize :: Int -> IO ()
475 foreign import ccall unsafe "enableTimingStats" enableTimingStats :: IO ()
477 -----------------------------------------------------------------------------
480 -- The central concept of a "way" is that all objects in a given
481 -- program must be compiled in the same "way". Certain options change
482 -- parameters of the virtual machine, eg. profiling adds an extra word
483 -- to the object header, so profiling objects cannot be linked with
484 -- non-profiling objects.
486 -- After parsing the command-line options, we determine which "way" we
487 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
489 -- We then find the "build-tag" associated with this way, and this
490 -- becomes the suffix used to find .hi files and libraries used in
493 GLOBAL_VAR(v_Build_tag, "", String)
495 -- The RTS has its own build tag, because there are some ways that
496 -- affect the RTS only.
497 GLOBAL_VAR(v_RTS_Build_tag, "", String)
526 GLOBAL_VAR(v_Ways, [] ,[WayName])
528 allowed_combination :: [WayName] -> Bool
529 allowed_combination way = and [ x `allowedWith` y
530 | x <- way, y <- way, x < y ]
532 -- Note ordering in these tests: the left argument is
533 -- <= the right argument, according to the Ord instance
536 -- debug is allowed with everything
537 _ `allowedWith` WayDebug = True
538 WayDebug `allowedWith` _ = True
540 WayProf `allowedWith` WayNDP = True
541 WayThreaded `allowedWith` WayProf = True
542 _ `allowedWith` _ = False
545 findBuildTag :: IO [String] -- new options
547 way_names <- readIORef v_Ways
548 let ws = sort (nub way_names)
550 if not (allowed_combination ws)
551 then ghcError (CmdLineError $
552 "combination not supported: " ++
553 foldr1 (\a b -> a ++ '/':b)
554 (map (wayName . lkupWay) ws))
555 else let ways = map lkupWay ws
556 tag = mkBuildTag (filter (not.wayRTSOnly) ways)
557 rts_tag = mkBuildTag ways
558 flags = map wayOpts ways
560 writeIORef v_Build_tag tag
561 writeIORef v_RTS_Build_tag rts_tag
562 return (concat flags)
566 mkBuildTag :: [Way] -> String
567 mkBuildTag ways = concat (intersperse "_" (map wayTag ways))
569 lkupWay :: WayName -> Way
571 case lookup w way_details of
572 Nothing -> error "findBuildTag"
573 Just details -> details
575 isRTSWay :: WayName -> Bool
576 isRTSWay = wayRTSOnly . lkupWay
585 way_details :: [ (WayName, Way) ]
587 [ (WayThreaded, Way "thr" True "Threaded" [
588 #if defined(freebsd_TARGET_OS)
591 -- FreeBSD's default threading library is the KSE-based M:N libpthread,
592 -- which GHC has some problems with. It's currently not clear whether
593 -- the problems are our fault or theirs, but it seems that using the
594 -- alternative 1:1 threading library libthr works around it:
596 #elif defined(solaris2_TARGET_OS)
601 (WayDebug, Way "debug" True "Debug" [] ),
603 (WayProf, Way "p" False "Profiling"
606 , "-optc-DPROFILING" ]),
608 (WayTicky, Way "t" True "Ticky-ticky Profiling"
610 , "-optc-DTICKY_TICKY" ]),
612 -- optl's below to tell linker where to find the PVM library -- HWL
613 (WayPar, Way "mp" False "Parallel"
615 , "-D__PARALLEL_HASKELL__"
617 , "-package concurrent"
619 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
623 -- at the moment we only change the RTS and could share compiler and libs!
624 (WayPar, Way "mt" False "Parallel ticky profiling"
626 , "-D__PARALLEL_HASKELL__"
629 , "-package concurrent"
631 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
635 (WayPar, Way "md" False "Distributed"
637 , "-D__PARALLEL_HASKELL__"
638 , "-D__DISTRIBUTED_HASKELL__"
641 , "-package concurrent"
643 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
647 (WayGran, Way "mg" False "GranSim"
651 , "-package concurrent" ]),
653 (WayNDP, Way "ndp" False "Nested data parallelism"
657 (WayUser_a, Way "a" False "User way 'a'" ["$WAY_a_REAL_OPTS"]),
658 (WayUser_b, Way "b" False "User way 'b'" ["$WAY_b_REAL_OPTS"]),
659 (WayUser_c, Way "c" False "User way 'c'" ["$WAY_c_REAL_OPTS"]),
660 (WayUser_d, Way "d" False "User way 'd'" ["$WAY_d_REAL_OPTS"]),
661 (WayUser_e, Way "e" False "User way 'e'" ["$WAY_e_REAL_OPTS"]),
662 (WayUser_f, Way "f" False "User way 'f'" ["$WAY_f_REAL_OPTS"]),
663 (WayUser_g, Way "g" False "User way 'g'" ["$WAY_g_REAL_OPTS"]),
664 (WayUser_h, Way "h" False "User way 'h'" ["$WAY_h_REAL_OPTS"]),
665 (WayUser_i, Way "i" False "User way 'i'" ["$WAY_i_REAL_OPTS"]),
666 (WayUser_j, Way "j" False "User way 'j'" ["$WAY_j_REAL_OPTS"]),
667 (WayUser_k, Way "k" False "User way 'k'" ["$WAY_k_REAL_OPTS"]),
668 (WayUser_l, Way "l" False "User way 'l'" ["$WAY_l_REAL_OPTS"]),
669 (WayUser_m, Way "m" False "User way 'm'" ["$WAY_m_REAL_OPTS"]),
670 (WayUser_n, Way "n" False "User way 'n'" ["$WAY_n_REAL_OPTS"]),
671 (WayUser_o, Way "o" False "User way 'o'" ["$WAY_o_REAL_OPTS"]),
672 (WayUser_A, Way "A" False "User way 'A'" ["$WAY_A_REAL_OPTS"]),
673 (WayUser_B, Way "B" False "User way 'B'" ["$WAY_B_REAL_OPTS"])
676 unregFlags :: [String]
679 , "-optc-DUSE_MINIINTERPRETER"
680 , "-fno-asm-mangling"