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,
43 opt_SpecInlineJoinPoints,
45 opt_SimplNoPreInlining,
46 opt_SimplExcessPrecision,
50 opt_UF_CreationThreshold,
52 opt_UF_FunAppDiscount,
56 -- Optimization fuel controls
76 #include "HsVersions.h"
82 import Maybes ( firstJust )
85 import Control.Exception ( throwDyn )
87 import System.IO.Unsafe ( unsafePerformIO )
88 import Control.Monad ( when )
89 import Data.Char ( isDigit )
92 -----------------------------------------------------------------------------
95 parseStaticFlags :: [String] -> IO [String]
96 parseStaticFlags args = do
97 ready <- readIORef v_opt_C_ready
98 when ready $ throwDyn (ProgramError "Too late for parseStaticFlags: call it before newSession")
100 (leftover, errs) <- processArgs static_flags args
101 when (not (null errs)) $ throwDyn (UsageError (unlines errs))
103 -- deal with the way flags: the way (eg. prof) gives rise to
104 -- further flags, some of which might be static.
105 way_flags <- findBuildTag
107 -- if we're unregisterised, add some more flags
108 let unreg_flags | cGhcUnregisterised == "YES" = unregFlags
111 (more_leftover, errs) <- processArgs static_flags (unreg_flags ++ way_flags)
113 -- see sanity code in staticOpts
114 writeIORef v_opt_C_ready True
116 -- TABLES_NEXT_TO_CODE affects the info table layout.
117 -- Be careful to do this *after* all processArgs,
118 -- because evaluating tablesNextToCode involves looking at the global
119 -- static flags. Those pesky global variables...
120 let cg_flags | tablesNextToCode = ["-optc-DTABLES_NEXT_TO_CODE"]
123 -- HACK: -fexcess-precision is both a static and a dynamic flag. If
124 -- the static flag parser has slurped it, we must return it as a
125 -- leftover too. ToDo: make -fexcess-precision dynamic only.
126 let excess_prec | opt_SimplExcessPrecision = ["-fexcess-precision"]
129 when (not (null errs)) $ ghcError (UsageError (unlines errs))
130 return (excess_prec++cg_flags++more_leftover++leftover)
132 initStaticOpts :: IO ()
133 initStaticOpts = writeIORef v_opt_C_ready True
135 static_flags :: [(String, OptKind IO)]
136 -- All the static flags should appear in this list. It describes how each
137 -- static flag should be processed. Two main purposes:
138 -- (a) if a command-line flag doesn't appear in the list, GHC can complain
139 -- (b) a command-line flag may remove, or add, other flags; e.g. the "-fno-X" things
141 -- The common (PassFlag addOpt) action puts the static flag into the bunch of
142 -- things that are searched up by the top-level definitions like
143 -- opt_foo = lookUp (fsLit "-dfoo")
145 -- Note that ordering is important in the following list: any flag which
146 -- is a prefix flag (i.e. HasArg, Prefix, OptPrefix, AnySuffix) will override
147 -- flags further down the list with the same prefix.
150 ------- GHCi -------------------------------------------------------
151 ( "ignore-dot-ghci", PassFlag addOpt )
152 , ( "read-dot-ghci" , NoArg (removeOpt "-ignore-dot-ghci") )
154 ------- ways --------------------------------------------------------
155 , ( "prof" , NoArg (addWay WayProf) )
156 , ( "ticky" , NoArg (addWay WayTicky) )
157 , ( "parallel" , NoArg (addWay WayPar) )
158 , ( "gransim" , NoArg (addWay WayGran) )
159 , ( "smp" , NoArg (addWay WayThreaded) ) -- backwards compat.
160 , ( "debug" , NoArg (addWay WayDebug) )
161 , ( "ndp" , NoArg (addWay WayNDP) )
162 , ( "threaded" , NoArg (addWay WayThreaded) )
165 ------ Debugging ----------------------------------------------------
166 , ( "dppr-debug", PassFlag addOpt )
167 , ( "dsuppress-uniques", PassFlag addOpt )
168 , ( "dppr-user-length", AnySuffix addOpt )
169 , ( "dopt-fuel", AnySuffix addOpt )
170 , ( "dno-debug-output", PassFlag addOpt )
171 -- rest of the debugging flags are dynamic
173 --------- Profiling --------------------------------------------------
174 , ( "auto-all" , NoArg (addOpt "-fauto-sccs-on-all-toplevs") )
175 , ( "auto" , NoArg (addOpt "-fauto-sccs-on-exported-toplevs") )
176 , ( "caf-all" , NoArg (addOpt "-fauto-sccs-on-individual-cafs") )
177 -- "ignore-sccs" doesn't work (ToDo)
179 , ( "no-auto-all" , NoArg (removeOpt "-fauto-sccs-on-all-toplevs") )
180 , ( "no-auto" , NoArg (removeOpt "-fauto-sccs-on-exported-toplevs") )
181 , ( "no-caf-all" , NoArg (removeOpt "-fauto-sccs-on-individual-cafs") )
183 ----- Linker --------------------------------------------------------
184 , ( "static" , PassFlag addOpt )
185 , ( "dynamic" , NoArg (removeOpt "-static") )
186 , ( "rdynamic" , NoArg (return ()) ) -- ignored for compat w/ gcc
188 ----- RTS opts ------------------------------------------------------
189 , ( "H" , HasArg (setHeapSize . fromIntegral . decodeSize) )
190 , ( "Rghc-timing" , NoArg (enableTimingStats) )
192 ------ Compiler flags -----------------------------------------------
193 -- All other "-fno-<blah>" options cancel out "-f<blah>" on the hsc cmdline
194 , ( "fno-", PrefixPred (\s -> isStaticFlag ("f"++s))
195 (\s -> removeOpt ("-f"++s)) )
197 -- Pass all remaining "-f<blah>" options to hsc
198 , ( "f", AnySuffixPred (isStaticFlag) addOpt )
201 addOpt :: String -> IO ()
202 addOpt = consIORef v_opt_C
204 addWay :: WayName -> IO ()
205 addWay = consIORef v_Ways
207 removeOpt :: String -> IO ()
209 fs <- readIORef v_opt_C
210 writeIORef v_opt_C $! filter (/= f) fs
212 lookUp :: FastString -> Bool
213 lookup_def_int :: String -> Int -> Int
214 lookup_def_float :: String -> Float -> Float
215 lookup_str :: String -> Maybe String
217 -- holds the static opts while they're being collected, before
218 -- being unsafely read by unpacked_static_opts below.
219 GLOBAL_VAR(v_opt_C, defaultStaticOpts, [String])
220 GLOBAL_VAR(v_opt_C_ready, False, Bool)
222 staticFlags :: [String]
223 staticFlags = unsafePerformIO $ do
224 ready <- readIORef v_opt_C_ready
226 then panic "Static flags have not been initialised!\n Please call GHC.newSession or GHC.parseStaticFlags early enough."
227 else readIORef v_opt_C
229 -- -static is the default
230 defaultStaticOpts :: [String]
231 defaultStaticOpts = ["-static"]
233 packed_static_opts :: [FastString]
234 packed_static_opts = map mkFastString staticFlags
236 lookUp sw = sw `elem` packed_static_opts
238 -- (lookup_str "foo") looks for the flag -foo=X or -fooX,
239 -- and returns the string X
241 = case firstJust (map (startsWith sw) staticFlags) of
242 Just ('=' : str) -> Just str
246 lookup_def_int sw def = case (lookup_str sw) of
247 Nothing -> def -- Use default
248 Just xx -> try_read sw xx
250 lookup_def_float sw def = case (lookup_str sw) of
251 Nothing -> def -- Use default
252 Just xx -> try_read sw xx
255 try_read :: Read a => String -> String -> a
256 -- (try_read sw str) tries to read s; if it fails, it
257 -- bleats about flag sw
260 ((x,_):_) -> x -- Be forgiving: ignore trailing goop, and alternative parses
261 [] -> ghcError (UsageError ("Malformed argument " ++ str ++ " for flag " ++ sw))
262 -- ToDo: hack alert. We should really parse the arugments
263 -- and announce errors in a more civilised way.
267 Putting the compiler options into temporary at-files
268 may turn out to be necessary later on if we turn hsc into
269 a pure Win32 application where I think there's a command-line
270 length limit of 255. unpacked_opts understands the @ option.
272 unpacked_opts :: [String]
276 map unpackFS argv -- NOT ARGV any more: v_Static_hsc_opts
278 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
282 opt_IgnoreDotGhci :: Bool
283 opt_IgnoreDotGhci = lookUp (fsLit "-ignore-dot-ghci")
286 opt_SuppressUniques :: Bool
287 opt_SuppressUniques = lookUp (fsLit "-dsuppress-uniques")
288 opt_PprStyle_Debug :: Bool
289 opt_PprStyle_Debug = lookUp (fsLit "-dppr-debug")
290 opt_PprUserLength :: Int
291 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
293 opt_Fuel = lookup_def_int "-dopt-fuel" maxBound
294 opt_NoDebugOutput :: Bool
295 opt_NoDebugOutput = lookUp (fsLit "-dno-debug-output")
299 opt_AutoSccsOnAllToplevs :: Bool
300 opt_AutoSccsOnAllToplevs = lookUp (fsLit "-fauto-sccs-on-all-toplevs")
301 opt_AutoSccsOnExportedToplevs :: Bool
302 opt_AutoSccsOnExportedToplevs = lookUp (fsLit "-fauto-sccs-on-exported-toplevs")
303 opt_AutoSccsOnIndividualCafs :: Bool
304 opt_AutoSccsOnIndividualCafs = lookUp (fsLit "-fauto-sccs-on-individual-cafs")
305 opt_SccProfilingOn :: Bool
306 opt_SccProfilingOn = lookUp (fsLit "-fscc-profiling")
307 opt_DoTickyProfiling :: Bool
308 opt_DoTickyProfiling = WayTicky `elem` (unsafePerformIO $ readIORef v_Ways)
312 opt_Hpc = lookUp (fsLit "-fhpc")
315 opt_DictsStrict :: Bool
316 opt_DictsStrict = lookUp (fsLit "-fdicts-strict")
317 opt_IrrefutableTuples :: Bool
318 opt_IrrefutableTuples = lookUp (fsLit "-firrefutable-tuples")
320 opt_Parallel = lookUp (fsLit "-fparallel")
323 opt_SpecInlineJoinPoints :: Bool
324 opt_SpecInlineJoinPoints = lookUp (fsLit "-fspec-inline-join-points")
325 opt_NoStateHack :: Bool
326 opt_NoStateHack = lookUp (fsLit "-fno-state-hack")
328 opt_CprOff = lookUp (fsLit "-fcpr-off")
329 -- Switch off CPR analysis in the new demand analyser
330 opt_MaxWorkerArgs :: Int
331 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
333 opt_GranMacros :: Bool
334 opt_GranMacros = lookUp (fsLit "-fgransim")
335 opt_HiVersion :: Integer
336 opt_HiVersion = read (cProjectVersionInt ++ cProjectPatchLevel) :: Integer
337 opt_HistorySize :: Int
338 opt_HistorySize = lookup_def_int "-fhistory-size" 20
339 opt_OmitBlackHoling :: Bool
340 opt_OmitBlackHoling = lookUp (fsLit "-dno-black-holing")
342 -- Simplifier switches
343 opt_SimplNoPreInlining :: Bool
344 opt_SimplNoPreInlining = lookUp (fsLit "-fno-pre-inlining")
345 -- NoPreInlining is there just to see how bad things
346 -- get if you don't do it!
347 opt_SimplExcessPrecision :: Bool
348 opt_SimplExcessPrecision = lookUp (fsLit "-fexcess-precision")
351 opt_UF_CreationThreshold :: Int
352 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
353 opt_UF_UseThreshold :: Int
354 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
355 opt_UF_FunAppDiscount :: Int
356 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
357 opt_UF_KeenessFactor :: Float
358 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
361 opt_UF_DearOp = ( 4 :: Int)
364 -- Related to linking
366 #if darwin_TARGET_OS && x86_64_TARGET_ARCH
369 opt_PIC = lookUp (fsLit "-fPIC")
372 opt_Static = lookUp (fsLit "-static")
373 opt_Unregisterised :: Bool
374 opt_Unregisterised = lookUp (fsLit "-funregisterised")
376 -- Derived, not a real option. Determines whether we will be compiling
377 -- info tables that reside just before the entry code, or with an
378 -- indirection to the entry code. See TABLES_NEXT_TO_CODE in
379 -- includes/InfoTables.h.
380 tablesNextToCode :: Bool
381 tablesNextToCode = not opt_Unregisterised
382 && cGhcEnableTablesNextToCode == "YES"
384 opt_EmitExternalCore :: Bool
385 opt_EmitExternalCore = lookUp (fsLit "-fext-core")
387 -- Include full span info in error messages, instead of just the start position.
388 opt_ErrorSpans :: Bool
389 opt_ErrorSpans = lookUp (fsLit "-ferror-spans")
392 -- object files and libraries to be linked in are collected here.
393 -- ToDo: perhaps this could be done without a global, it wasn't obvious
394 -- how to do it though --SDM.
395 GLOBAL_VAR(v_Ld_inputs, [], [String])
397 isStaticFlag :: String -> Bool
400 "fauto-sccs-on-all-toplevs",
401 "fauto-sccs-on-exported-toplevs",
402 "fauto-sccs-on-individual-cafs",
405 "fspec-inline-join-points",
406 "firrefutable-tuples",
409 "fno-hi-version-check",
411 "fno-method-sharing",
417 "fhardwire-lib-paths",
425 || any (`isPrefixOf` f) [
426 "fliberate-case-threshold",
429 "funfolding-creation-threshold",
430 "funfolding-use-threshold",
431 "funfolding-fun-discount",
432 "funfolding-keeness-factor"
437 -- Misc functions for command-line options
439 startsWith :: String -> String -> Maybe String
440 -- startsWith pfx (pfx++rest) = Just rest
442 startsWith [] str = Just str
443 startsWith (c:cs) (s:ss)
444 = if c /= s then Nothing else startsWith cs ss
445 startsWith _ [] = Nothing
448 -----------------------------------------------------------------------------
449 -- convert sizes like "3.5M" into integers
451 decodeSize :: String -> Integer
453 | c == "" = truncate n
454 | c == "K" || c == "k" = truncate (n * 1000)
455 | c == "M" || c == "m" = truncate (n * 1000 * 1000)
456 | c == "G" || c == "g" = truncate (n * 1000 * 1000 * 1000)
457 | otherwise = throwDyn (CmdLineError ("can't decode size: " ++ str))
458 where (m, c) = span pred str
460 pred c = isDigit c || c == '.'
463 -----------------------------------------------------------------------------
466 foreign import ccall unsafe "setHeapSize" setHeapSize :: Int -> IO ()
467 foreign import ccall unsafe "enableTimingStats" enableTimingStats :: IO ()
469 -----------------------------------------------------------------------------
472 -- The central concept of a "way" is that all objects in a given
473 -- program must be compiled in the same "way". Certain options change
474 -- parameters of the virtual machine, eg. profiling adds an extra word
475 -- to the object header, so profiling objects cannot be linked with
476 -- non-profiling objects.
478 -- After parsing the command-line options, we determine which "way" we
479 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
481 -- We then find the "build-tag" associated with this way, and this
482 -- becomes the suffix used to find .hi files and libraries used in
485 GLOBAL_VAR(v_Build_tag, "", String)
487 -- The RTS has its own build tag, because there are some ways that
488 -- affect the RTS only.
489 GLOBAL_VAR(v_RTS_Build_tag, "", String)
518 GLOBAL_VAR(v_Ways, [] ,[WayName])
520 allowed_combination :: [WayName] -> Bool
521 allowed_combination way = and [ x `allowedWith` y
522 | x <- way, y <- way, x < y ]
524 -- Note ordering in these tests: the left argument is
525 -- <= the right argument, according to the Ord instance
528 -- debug is allowed with everything
529 _ `allowedWith` WayDebug = True
530 WayDebug `allowedWith` _ = True
532 WayProf `allowedWith` WayNDP = True
533 WayThreaded `allowedWith` WayProf = True
534 _ `allowedWith` _ = False
537 findBuildTag :: IO [String] -- new options
539 way_names <- readIORef v_Ways
540 let ws = sort (nub way_names)
542 if not (allowed_combination ws)
543 then throwDyn (CmdLineError $
544 "combination not supported: " ++
545 foldr1 (\a b -> a ++ '/':b)
546 (map (wayName . lkupWay) ws))
547 else let ways = map lkupWay ws
548 tag = mkBuildTag (filter (not.wayRTSOnly) ways)
549 rts_tag = mkBuildTag ways
550 flags = map wayOpts ways
552 writeIORef v_Build_tag tag
553 writeIORef v_RTS_Build_tag rts_tag
554 return (concat flags)
558 mkBuildTag :: [Way] -> String
559 mkBuildTag ways = concat (intersperse "_" (map wayTag ways))
561 lkupWay :: WayName -> Way
563 case lookup w way_details of
564 Nothing -> error "findBuildTag"
565 Just details -> details
567 isRTSWay :: WayName -> Bool
568 isRTSWay = wayRTSOnly . lkupWay
577 way_details :: [ (WayName, Way) ]
579 [ (WayThreaded, Way "thr" True "Threaded" [
580 #if defined(freebsd_TARGET_OS)
583 -- FreeBSD's default threading library is the KSE-based M:N libpthread,
584 -- which GHC has some problems with. It's currently not clear whether
585 -- the problems are our fault or theirs, but it seems that using the
586 -- alternative 1:1 threading library libthr works around it:
588 #elif defined(solaris2_TARGET_OS)
593 (WayDebug, Way "debug" True "Debug" [] ),
595 (WayProf, Way "p" False "Profiling"
598 , "-optc-DPROFILING" ]),
600 (WayTicky, Way "t" True "Ticky-ticky Profiling"
602 , "-optc-DTICKY_TICKY" ]),
604 -- optl's below to tell linker where to find the PVM library -- HWL
605 (WayPar, Way "mp" False "Parallel"
607 , "-D__PARALLEL_HASKELL__"
609 , "-package concurrent"
611 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
615 -- at the moment we only change the RTS and could share compiler and libs!
616 (WayPar, Way "mt" False "Parallel ticky profiling"
618 , "-D__PARALLEL_HASKELL__"
621 , "-package concurrent"
623 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
627 (WayPar, Way "md" False "Distributed"
629 , "-D__PARALLEL_HASKELL__"
630 , "-D__DISTRIBUTED_HASKELL__"
633 , "-package concurrent"
635 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
639 (WayGran, Way "mg" False "GranSim"
643 , "-package concurrent" ]),
645 (WayNDP, Way "ndp" False "Nested data parallelism"
649 (WayUser_a, Way "a" False "User way 'a'" ["$WAY_a_REAL_OPTS"]),
650 (WayUser_b, Way "b" False "User way 'b'" ["$WAY_b_REAL_OPTS"]),
651 (WayUser_c, Way "c" False "User way 'c'" ["$WAY_c_REAL_OPTS"]),
652 (WayUser_d, Way "d" False "User way 'd'" ["$WAY_d_REAL_OPTS"]),
653 (WayUser_e, Way "e" False "User way 'e'" ["$WAY_e_REAL_OPTS"]),
654 (WayUser_f, Way "f" False "User way 'f'" ["$WAY_f_REAL_OPTS"]),
655 (WayUser_g, Way "g" False "User way 'g'" ["$WAY_g_REAL_OPTS"]),
656 (WayUser_h, Way "h" False "User way 'h'" ["$WAY_h_REAL_OPTS"]),
657 (WayUser_i, Way "i" False "User way 'i'" ["$WAY_i_REAL_OPTS"]),
658 (WayUser_j, Way "j" False "User way 'j'" ["$WAY_j_REAL_OPTS"]),
659 (WayUser_k, Way "k" False "User way 'k'" ["$WAY_k_REAL_OPTS"]),
660 (WayUser_l, Way "l" False "User way 'l'" ["$WAY_l_REAL_OPTS"]),
661 (WayUser_m, Way "m" False "User way 'm'" ["$WAY_m_REAL_OPTS"]),
662 (WayUser_n, Way "n" False "User way 'n'" ["$WAY_n_REAL_OPTS"]),
663 (WayUser_o, Way "o" False "User way 'o'" ["$WAY_o_REAL_OPTS"]),
664 (WayUser_A, Way "A" False "User way 'A'" ["$WAY_A_REAL_OPTS"]),
665 (WayUser_B, Way "B" False "User way 'B'" ["$WAY_B_REAL_OPTS"])
668 unregFlags :: [String]
671 , "-optc-DUSE_MINIINTERPRETER"
672 , "-fno-asm-mangling"