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
26 opt_AutoSccsOnAllToplevs,
27 opt_AutoSccsOnExportedToplevs,
28 opt_AutoSccsOnIndividualCafs,
37 opt_IrrefutableTuples,
42 opt_SpecInlineJoinPoints,
44 opt_SimplNoPreInlining,
45 opt_SimplExcessPrecision,
49 opt_UF_CreationThreshold,
51 opt_UF_FunAppDiscount,
55 -- Optimization fuel controls
75 #include "HsVersions.h"
81 import Maybes ( firstJust )
84 import Control.Exception ( throwDyn )
86 import System.IO.Unsafe ( unsafePerformIO )
87 import Control.Monad ( when )
88 import Data.Char ( isDigit )
91 -----------------------------------------------------------------------------
94 parseStaticFlags :: [String] -> IO [String]
95 parseStaticFlags args = do
96 ready <- readIORef v_opt_C_ready
97 when ready $ throwDyn (ProgramError "Too late for parseStaticFlags: call it before newSession")
99 (leftover, errs) <- processArgs static_flags args
100 when (not (null errs)) $ throwDyn (UsageError (unlines errs))
102 -- deal with the way flags: the way (eg. prof) gives rise to
103 -- further flags, some of which might be static.
104 way_flags <- findBuildTag
106 -- if we're unregisterised, add some more flags
107 let unreg_flags | cGhcUnregisterised == "YES" = unregFlags
110 (more_leftover, errs) <- processArgs static_flags (unreg_flags ++ way_flags)
112 -- see sanity code in staticOpts
113 writeIORef v_opt_C_ready True
115 -- TABLES_NEXT_TO_CODE affects the info table layout.
116 -- Be careful to do this *after* all processArgs,
117 -- because evaluating tablesNextToCode involves looking at the global
118 -- static flags. Those pesky global variables...
119 let cg_flags | tablesNextToCode = ["-optc-DTABLES_NEXT_TO_CODE"]
122 -- HACK: -fexcess-precision is both a static and a dynamic flag. If
123 -- the static flag parser has slurped it, we must return it as a
124 -- leftover too. ToDo: make -fexcess-precision dynamic only.
125 let excess_prec | opt_SimplExcessPrecision = ["-fexcess-precision"]
128 when (not (null errs)) $ ghcError (UsageError (unlines errs))
129 return (excess_prec++cg_flags++more_leftover++leftover)
131 initStaticOpts :: IO ()
132 initStaticOpts = writeIORef v_opt_C_ready True
134 static_flags :: [(String, OptKind IO)]
135 -- All the static flags should appear in this list. It describes how each
136 -- static flag should be processed. Two main purposes:
137 -- (a) if a command-line flag doesn't appear in the list, GHC can complain
138 -- (b) a command-line flag may remove, or add, other flags; e.g. the "-fno-X" things
140 -- The common (PassFlag addOpt) action puts the static flag into the bunch of
141 -- things that are searched up by the top-level definitions like
142 -- opt_foo = lookUp (fsLit "-dfoo")
144 -- Note that ordering is important in the following list: any flag which
145 -- is a prefix flag (i.e. HasArg, Prefix, OptPrefix, AnySuffix) will override
146 -- flags further down the list with the same prefix.
149 ------- GHCi -------------------------------------------------------
150 ( "ignore-dot-ghci", PassFlag addOpt )
151 , ( "read-dot-ghci" , NoArg (removeOpt "-ignore-dot-ghci") )
153 ------- ways --------------------------------------------------------
154 , ( "prof" , NoArg (addWay WayProf) )
155 , ( "ticky" , NoArg (addWay WayTicky) )
156 , ( "parallel" , NoArg (addWay WayPar) )
157 , ( "gransim" , NoArg (addWay WayGran) )
158 , ( "smp" , NoArg (addWay WayThreaded) ) -- backwards compat.
159 , ( "debug" , NoArg (addWay WayDebug) )
160 , ( "ndp" , NoArg (addWay WayNDP) )
161 , ( "threaded" , NoArg (addWay WayThreaded) )
164 ------ Debugging ----------------------------------------------------
165 , ( "dppr-debug", PassFlag addOpt )
166 , ( "dsuppress-uniques", PassFlag addOpt )
167 , ( "dppr-user-length", AnySuffix addOpt )
168 , ( "dopt-fuel", AnySuffix addOpt )
169 -- rest of the debugging flags are dynamic
171 --------- Profiling --------------------------------------------------
172 , ( "auto-all" , NoArg (addOpt "-fauto-sccs-on-all-toplevs") )
173 , ( "auto" , NoArg (addOpt "-fauto-sccs-on-exported-toplevs") )
174 , ( "caf-all" , NoArg (addOpt "-fauto-sccs-on-individual-cafs") )
175 -- "ignore-sccs" doesn't work (ToDo)
177 , ( "no-auto-all" , NoArg (removeOpt "-fauto-sccs-on-all-toplevs") )
178 , ( "no-auto" , NoArg (removeOpt "-fauto-sccs-on-exported-toplevs") )
179 , ( "no-caf-all" , NoArg (removeOpt "-fauto-sccs-on-individual-cafs") )
181 ----- Linker --------------------------------------------------------
182 , ( "static" , PassFlag addOpt )
183 , ( "dynamic" , NoArg (removeOpt "-static") )
184 , ( "rdynamic" , NoArg (return ()) ) -- ignored for compat w/ gcc
186 ----- RTS opts ------------------------------------------------------
187 , ( "H" , HasArg (setHeapSize . fromIntegral . decodeSize) )
188 , ( "Rghc-timing" , NoArg (enableTimingStats) )
190 ------ Compiler flags -----------------------------------------------
191 -- All other "-fno-<blah>" options cancel out "-f<blah>" on the hsc cmdline
192 , ( "fno-", PrefixPred (\s -> isStaticFlag ("f"++s))
193 (\s -> removeOpt ("-f"++s)) )
195 -- Pass all remaining "-f<blah>" options to hsc
196 , ( "f", AnySuffixPred (isStaticFlag) addOpt )
199 addOpt :: String -> IO ()
200 addOpt = consIORef v_opt_C
202 addWay :: WayName -> IO ()
203 addWay = consIORef v_Ways
205 removeOpt :: String -> IO ()
207 fs <- readIORef v_opt_C
208 writeIORef v_opt_C $! filter (/= f) fs
210 lookUp :: FastString -> Bool
211 lookup_def_int :: String -> Int -> Int
212 lookup_def_float :: String -> Float -> Float
213 lookup_str :: String -> Maybe String
215 -- holds the static opts while they're being collected, before
216 -- being unsafely read by unpacked_static_opts below.
217 GLOBAL_VAR(v_opt_C, defaultStaticOpts, [String])
218 GLOBAL_VAR(v_opt_C_ready, False, Bool)
220 staticFlags :: [String]
221 staticFlags = unsafePerformIO $ do
222 ready <- readIORef v_opt_C_ready
224 then panic "Static flags have not been initialised!\n Please call GHC.newSession or GHC.parseStaticFlags early enough."
225 else readIORef v_opt_C
227 -- -static is the default
228 defaultStaticOpts :: [String]
229 defaultStaticOpts = ["-static"]
231 packed_static_opts :: [FastString]
232 packed_static_opts = map mkFastString staticFlags
234 lookUp sw = sw `elem` packed_static_opts
236 -- (lookup_str "foo") looks for the flag -foo=X or -fooX,
237 -- and returns the string X
239 = case firstJust (map (startsWith sw) staticFlags) of
240 Just ('=' : str) -> Just str
244 lookup_def_int sw def = case (lookup_str sw) of
245 Nothing -> def -- Use default
246 Just xx -> try_read sw xx
248 lookup_def_float sw def = case (lookup_str sw) of
249 Nothing -> def -- Use default
250 Just xx -> try_read sw xx
253 try_read :: Read a => String -> String -> a
254 -- (try_read sw str) tries to read s; if it fails, it
255 -- bleats about flag sw
258 ((x,_):_) -> x -- Be forgiving: ignore trailing goop, and alternative parses
259 [] -> ghcError (UsageError ("Malformed argument " ++ str ++ " for flag " ++ sw))
260 -- ToDo: hack alert. We should really parse the arugments
261 -- and announce errors in a more civilised way.
265 Putting the compiler options into temporary at-files
266 may turn out to be necessary later on if we turn hsc into
267 a pure Win32 application where I think there's a command-line
268 length limit of 255. unpacked_opts understands the @ option.
270 unpacked_opts :: [String]
274 map unpackFS argv -- NOT ARGV any more: v_Static_hsc_opts
276 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
280 opt_IgnoreDotGhci :: Bool
281 opt_IgnoreDotGhci = lookUp (fsLit "-ignore-dot-ghci")
284 opt_SuppressUniques :: Bool
285 opt_SuppressUniques = lookUp (fsLit "-dsuppress-uniques")
286 opt_PprStyle_Debug :: Bool
287 opt_PprStyle_Debug = lookUp (fsLit "-dppr-debug")
288 opt_PprUserLength :: Int
289 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
291 opt_Fuel = lookup_def_int "-dopt-fuel" maxBound
294 opt_AutoSccsOnAllToplevs :: Bool
295 opt_AutoSccsOnAllToplevs = lookUp (fsLit "-fauto-sccs-on-all-toplevs")
296 opt_AutoSccsOnExportedToplevs :: Bool
297 opt_AutoSccsOnExportedToplevs = lookUp (fsLit "-fauto-sccs-on-exported-toplevs")
298 opt_AutoSccsOnIndividualCafs :: Bool
299 opt_AutoSccsOnIndividualCafs = lookUp (fsLit "-fauto-sccs-on-individual-cafs")
300 opt_SccProfilingOn :: Bool
301 opt_SccProfilingOn = lookUp (fsLit "-fscc-profiling")
302 opt_DoTickyProfiling :: Bool
303 opt_DoTickyProfiling = WayTicky `elem` (unsafePerformIO $ readIORef v_Ways)
307 opt_Hpc = lookUp (fsLit "-fhpc")
310 opt_DictsStrict :: Bool
311 opt_DictsStrict = lookUp (fsLit "-fdicts-strict")
312 opt_IrrefutableTuples :: Bool
313 opt_IrrefutableTuples = lookUp (fsLit "-firrefutable-tuples")
315 opt_Parallel = lookUp (fsLit "-fparallel")
318 opt_SpecInlineJoinPoints :: Bool
319 opt_SpecInlineJoinPoints = lookUp (fsLit "-fspec-inline-join-points")
320 opt_NoStateHack :: Bool
321 opt_NoStateHack = lookUp (fsLit "-fno-state-hack")
323 opt_CprOff = lookUp (fsLit "-fcpr-off")
324 -- Switch off CPR analysis in the new demand analyser
325 opt_MaxWorkerArgs :: Int
326 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
328 opt_GranMacros :: Bool
329 opt_GranMacros = lookUp (fsLit "-fgransim")
330 opt_HiVersion :: Integer
331 opt_HiVersion = read (cProjectVersionInt ++ cProjectPatchLevel) :: Integer
332 opt_HistorySize :: Int
333 opt_HistorySize = lookup_def_int "-fhistory-size" 20
334 opt_OmitBlackHoling :: Bool
335 opt_OmitBlackHoling = lookUp (fsLit "-dno-black-holing")
337 -- Simplifier switches
338 opt_SimplNoPreInlining :: Bool
339 opt_SimplNoPreInlining = lookUp (fsLit "-fno-pre-inlining")
340 -- NoPreInlining is there just to see how bad things
341 -- get if you don't do it!
342 opt_SimplExcessPrecision :: Bool
343 opt_SimplExcessPrecision = lookUp (fsLit "-fexcess-precision")
346 opt_UF_CreationThreshold :: Int
347 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
348 opt_UF_UseThreshold :: Int
349 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
350 opt_UF_FunAppDiscount :: Int
351 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
352 opt_UF_KeenessFactor :: Float
353 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
356 opt_UF_DearOp = ( 4 :: Int)
359 -- Related to linking
361 #if darwin_TARGET_OS && x86_64_TARGET_ARCH
364 opt_PIC = lookUp (fsLit "-fPIC")
367 opt_Static = lookUp (fsLit "-static")
368 opt_Unregisterised :: Bool
369 opt_Unregisterised = lookUp (fsLit "-funregisterised")
371 -- Derived, not a real option. Determines whether we will be compiling
372 -- info tables that reside just before the entry code, or with an
373 -- indirection to the entry code. See TABLES_NEXT_TO_CODE in
374 -- includes/InfoTables.h.
375 tablesNextToCode :: Bool
376 tablesNextToCode = not opt_Unregisterised
377 && cGhcEnableTablesNextToCode == "YES"
379 opt_EmitExternalCore :: Bool
380 opt_EmitExternalCore = lookUp (fsLit "-fext-core")
382 -- Include full span info in error messages, instead of just the start position.
383 opt_ErrorSpans :: Bool
384 opt_ErrorSpans = lookUp (fsLit "-ferror-spans")
387 -- object files and libraries to be linked in are collected here.
388 -- ToDo: perhaps this could be done without a global, it wasn't obvious
389 -- how to do it though --SDM.
390 GLOBAL_VAR(v_Ld_inputs, [], [String])
392 isStaticFlag :: String -> Bool
395 "fauto-sccs-on-all-toplevs",
396 "fauto-sccs-on-exported-toplevs",
397 "fauto-sccs-on-individual-cafs",
400 "fspec-inline-join-points",
401 "firrefutable-tuples",
404 "fno-hi-version-check",
406 "fno-method-sharing",
412 "fhardwire-lib-paths",
420 || any (`isPrefixOf` f) [
421 "fliberate-case-threshold",
424 "funfolding-creation-threshold",
425 "funfolding-use-threshold",
426 "funfolding-fun-discount",
427 "funfolding-keeness-factor"
432 -- Misc functions for command-line options
434 startsWith :: String -> String -> Maybe String
435 -- startsWith pfx (pfx++rest) = Just rest
437 startsWith [] str = Just str
438 startsWith (c:cs) (s:ss)
439 = if c /= s then Nothing else startsWith cs ss
440 startsWith _ [] = Nothing
443 -----------------------------------------------------------------------------
444 -- convert sizes like "3.5M" into integers
446 decodeSize :: String -> Integer
448 | c == "" = truncate n
449 | c == "K" || c == "k" = truncate (n * 1000)
450 | c == "M" || c == "m" = truncate (n * 1000 * 1000)
451 | c == "G" || c == "g" = truncate (n * 1000 * 1000 * 1000)
452 | otherwise = throwDyn (CmdLineError ("can't decode size: " ++ str))
453 where (m, c) = span pred str
455 pred c = isDigit c || c == '.'
458 -----------------------------------------------------------------------------
461 foreign import ccall unsafe "setHeapSize" setHeapSize :: Int -> IO ()
462 foreign import ccall unsafe "enableTimingStats" enableTimingStats :: IO ()
464 -----------------------------------------------------------------------------
467 -- The central concept of a "way" is that all objects in a given
468 -- program must be compiled in the same "way". Certain options change
469 -- parameters of the virtual machine, eg. profiling adds an extra word
470 -- to the object header, so profiling objects cannot be linked with
471 -- non-profiling objects.
473 -- After parsing the command-line options, we determine which "way" we
474 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
476 -- We then find the "build-tag" associated with this way, and this
477 -- becomes the suffix used to find .hi files and libraries used in
480 GLOBAL_VAR(v_Build_tag, "", String)
482 -- The RTS has its own build tag, because there are some ways that
483 -- affect the RTS only.
484 GLOBAL_VAR(v_RTS_Build_tag, "", String)
513 GLOBAL_VAR(v_Ways, [] ,[WayName])
515 allowed_combination :: [WayName] -> Bool
516 allowed_combination way = and [ x `allowedWith` y
517 | x <- way, y <- way, x < y ]
519 -- Note ordering in these tests: the left argument is
520 -- <= the right argument, according to the Ord instance
523 -- debug is allowed with everything
524 _ `allowedWith` WayDebug = True
525 WayDebug `allowedWith` _ = True
527 WayProf `allowedWith` WayNDP = True
528 WayThreaded `allowedWith` WayProf = True
529 _ `allowedWith` _ = False
532 findBuildTag :: IO [String] -- new options
534 way_names <- readIORef v_Ways
535 let ws = sort (nub way_names)
537 if not (allowed_combination ws)
538 then throwDyn (CmdLineError $
539 "combination not supported: " ++
540 foldr1 (\a b -> a ++ '/':b)
541 (map (wayName . lkupWay) ws))
542 else let ways = map lkupWay ws
543 tag = mkBuildTag (filter (not.wayRTSOnly) ways)
544 rts_tag = mkBuildTag ways
545 flags = map wayOpts ways
547 writeIORef v_Build_tag tag
548 writeIORef v_RTS_Build_tag rts_tag
549 return (concat flags)
553 mkBuildTag :: [Way] -> String
554 mkBuildTag ways = concat (intersperse "_" (map wayTag ways))
556 lkupWay :: WayName -> Way
558 case lookup w way_details of
559 Nothing -> error "findBuildTag"
560 Just details -> details
562 isRTSWay :: WayName -> Bool
563 isRTSWay = wayRTSOnly . lkupWay
572 way_details :: [ (WayName, Way) ]
574 [ (WayThreaded, Way "thr" True "Threaded" [
575 #if defined(freebsd_TARGET_OS)
578 -- FreeBSD's default threading library is the KSE-based M:N libpthread,
579 -- which GHC has some problems with. It's currently not clear whether
580 -- the problems are our fault or theirs, but it seems that using the
581 -- alternative 1:1 threading library libthr works around it:
583 #elif defined(solaris2_TARGET_OS)
588 (WayDebug, Way "debug" True "Debug" [] ),
590 (WayProf, Way "p" False "Profiling"
593 , "-optc-DPROFILING" ]),
595 (WayTicky, Way "t" True "Ticky-ticky Profiling"
597 , "-optc-DTICKY_TICKY" ]),
599 -- optl's below to tell linker where to find the PVM library -- HWL
600 (WayPar, Way "mp" False "Parallel"
602 , "-D__PARALLEL_HASKELL__"
604 , "-package concurrent"
606 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
610 -- at the moment we only change the RTS and could share compiler and libs!
611 (WayPar, Way "mt" False "Parallel ticky profiling"
613 , "-D__PARALLEL_HASKELL__"
616 , "-package concurrent"
618 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
622 (WayPar, Way "md" False "Distributed"
624 , "-D__PARALLEL_HASKELL__"
625 , "-D__DISTRIBUTED_HASKELL__"
628 , "-package concurrent"
630 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
634 (WayGran, Way "mg" False "GranSim"
638 , "-package concurrent" ]),
640 (WayNDP, Way "ndp" False "Nested data parallelism"
644 (WayUser_a, Way "a" False "User way 'a'" ["$WAY_a_REAL_OPTS"]),
645 (WayUser_b, Way "b" False "User way 'b'" ["$WAY_b_REAL_OPTS"]),
646 (WayUser_c, Way "c" False "User way 'c'" ["$WAY_c_REAL_OPTS"]),
647 (WayUser_d, Way "d" False "User way 'd'" ["$WAY_d_REAL_OPTS"]),
648 (WayUser_e, Way "e" False "User way 'e'" ["$WAY_e_REAL_OPTS"]),
649 (WayUser_f, Way "f" False "User way 'f'" ["$WAY_f_REAL_OPTS"]),
650 (WayUser_g, Way "g" False "User way 'g'" ["$WAY_g_REAL_OPTS"]),
651 (WayUser_h, Way "h" False "User way 'h'" ["$WAY_h_REAL_OPTS"]),
652 (WayUser_i, Way "i" False "User way 'i'" ["$WAY_i_REAL_OPTS"]),
653 (WayUser_j, Way "j" False "User way 'j'" ["$WAY_j_REAL_OPTS"]),
654 (WayUser_k, Way "k" False "User way 'k'" ["$WAY_k_REAL_OPTS"]),
655 (WayUser_l, Way "l" False "User way 'l'" ["$WAY_l_REAL_OPTS"]),
656 (WayUser_m, Way "m" False "User way 'm'" ["$WAY_m_REAL_OPTS"]),
657 (WayUser_n, Way "n" False "User way 'n'" ["$WAY_n_REAL_OPTS"]),
658 (WayUser_o, Way "o" False "User way 'o'" ["$WAY_o_REAL_OPTS"]),
659 (WayUser_A, Way "A" False "User way 'A'" ["$WAY_A_REAL_OPTS"]),
660 (WayUser_B, Way "B" False "User way 'B'" ["$WAY_B_REAL_OPTS"])
663 unregFlags :: [String]
666 , "-optc-DUSE_MINIINTERPRETER"
667 , "-fno-asm-mangling"