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 -----------------------------------------------------------------------------
20 WayName(..), Way(..), v_Ways, isRTSWay, mkBuildTag,
22 -- Output style options
26 opt_PprStyle_Debug, opt_TraceLevel,
29 -- Suppressing boring aspects of core dumps
32 opt_SuppressCoercions,
33 opt_SuppressModulePrefixes,
34 opt_SuppressTypeApplications,
36 opt_SuppressTypeSignatures,
46 opt_IrrefutableTuples,
51 opt_SimpleListLiterals,
53 opt_SimplNoPreInlining,
54 opt_SimplExcessPrecision,
59 opt_UF_CreationThreshold,
61 opt_UF_FunAppDiscount,
66 -- Optimization fuel controls
87 addOpt, removeOpt, addWay, getWayFlags, v_opt_C_ready
90 #include "HsVersions.h"
95 import Maybes ( firstJusts )
98 import Data.Maybe ( listToMaybe )
100 import System.IO.Unsafe ( unsafePerformIO )
103 -----------------------------------------------------------------------------
106 initStaticOpts :: IO ()
107 initStaticOpts = writeIORef v_opt_C_ready True
109 addOpt :: String -> IO ()
110 addOpt = consIORef v_opt_C
112 addWay :: WayName -> IO ()
113 addWay = consIORef v_Ways . lkupWay
115 removeOpt :: String -> IO ()
117 fs <- readIORef v_opt_C
118 writeIORef v_opt_C $! filter (/= f) fs
120 lookUp :: FastString -> Bool
121 lookup_def_int :: String -> Int -> Int
122 lookup_def_float :: String -> Float -> Float
123 lookup_str :: String -> Maybe String
125 -- holds the static opts while they're being collected, before
126 -- being unsafely read by unpacked_static_opts below.
127 GLOBAL_VAR(v_opt_C, defaultStaticOpts, [String])
128 GLOBAL_VAR(v_opt_C_ready, False, Bool)
130 staticFlags :: [String]
131 staticFlags = unsafePerformIO $ do
132 ready <- readIORef v_opt_C_ready
134 then panic "Static flags have not been initialised!\n Please call GHC.newSession or GHC.parseStaticFlags early enough."
135 else readIORef v_opt_C
137 -- -static is the default
138 defaultStaticOpts :: [String]
139 defaultStaticOpts = ["-static"]
141 packed_static_opts :: [FastString]
142 packed_static_opts = map mkFastString staticFlags
144 lookUp sw = sw `elem` packed_static_opts
146 -- (lookup_str "foo") looks for the flag -foo=X or -fooX,
147 -- and returns the string X
149 = case firstJusts (map (stripPrefix sw) staticFlags) of
150 Just ('=' : str) -> Just str
154 lookup_def_int sw def = case (lookup_str sw) of
155 Nothing -> def -- Use default
156 Just xx -> try_read sw xx
158 lookup_def_float sw def = case (lookup_str sw) of
159 Nothing -> def -- Use default
160 Just xx -> try_read sw xx
163 try_read :: Read a => String -> String -> a
164 -- (try_read sw str) tries to read s; if it fails, it
165 -- bleats about flag sw
168 ((x,_):_) -> x -- Be forgiving: ignore trailing goop, and alternative parses
169 [] -> ghcError (UsageError ("Malformed argument " ++ str ++ " for flag " ++ sw))
170 -- ToDo: hack alert. We should really parse the arugments
171 -- and announce errors in a more civilised way.
175 Putting the compiler options into temporary at-files
176 may turn out to be necessary later on if we turn hsc into
177 a pure Win32 application where I think there's a command-line
178 length limit of 255. unpacked_opts understands the @ option.
180 unpacked_opts :: [String]
184 map unpackFS argv -- NOT ARGV any more: v_Static_hsc_opts
186 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
190 opt_IgnoreDotGhci :: Bool
191 opt_IgnoreDotGhci = lookUp (fsLit "-ignore-dot-ghci")
194 -- | Suppress all that is suppressable in core dumps.
195 opt_SuppressAll :: Bool
197 = lookUp (fsLit "-dsuppress-all")
199 -- | Suppress unique ids on variables.
200 opt_SuppressUniques :: Bool
202 = lookUp (fsLit "-dsuppress-all")
203 || lookUp (fsLit "-dsuppress-uniques")
205 -- | Suppress all coercions, them replacing with '...'
206 opt_SuppressCoercions :: Bool
207 opt_SuppressCoercions
208 = lookUp (fsLit "-dsuppress-all")
209 || lookUp (fsLit "-dsuppress-coercions")
211 -- | Suppress module id prefixes on variables.
212 opt_SuppressModulePrefixes :: Bool
213 opt_SuppressModulePrefixes
214 = lookUp (fsLit "-dsuppress-all")
215 || lookUp (fsLit "-dsuppress-module-prefixes")
217 -- | Suppress type applications.
218 opt_SuppressTypeApplications :: Bool
219 opt_SuppressTypeApplications
220 = lookUp (fsLit "-dsuppress-all")
221 || lookUp (fsLit "-dsuppress-type-applications")
223 -- | Suppress info such as arity and unfoldings on identifiers.
224 opt_SuppressIdInfo :: Bool
226 = lookUp (fsLit "-dsuppress-all")
227 || lookUp (fsLit "-dsuppress-idinfo")
229 -- | Suppress seprate type signatures in core, but leave types on lambda bound vars
230 opt_SuppressTypeSignatures :: Bool
231 opt_SuppressTypeSignatures
232 = lookUp (fsLit "-dsuppress-all")
233 || lookUp (fsLit "-dsuppress-type-signatures")
236 -- | Display case expressions with a single alternative as strict let bindings
237 opt_PprCaseAsLet :: Bool
238 opt_PprCaseAsLet = lookUp (fsLit "-dppr-case-as-let")
240 -- | Set the maximum width of the dumps
241 -- If GHC's command line options are bad then the options parser uses the
242 -- pretty printer display the error message. In this case the staticFlags
243 -- won't be initialized yet, so we must check for this case explicitly
244 -- and return the default value.
248 $ do ready <- readIORef v_opt_C_ready
251 else return $ lookup_def_int "-dppr-cols" 100
254 opt_PprStyle_Debug :: Bool
255 opt_PprStyle_Debug = lookUp (fsLit "-dppr-debug")
257 opt_TraceLevel :: Int
258 opt_TraceLevel = lookup_def_int "-dtrace-level" 1 -- Standard level is 1
261 opt_PprUserLength :: Int
262 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
265 opt_Fuel = lookup_def_int "-dopt-fuel" maxBound
267 opt_NoDebugOutput :: Bool
268 opt_NoDebugOutput = lookUp (fsLit "-dno-debug-output")
271 opt_SccProfilingOn :: Bool
272 opt_SccProfilingOn = lookUp (fsLit "-fscc-profiling")
276 opt_Hpc = lookUp (fsLit "-fhpc")
279 opt_DictsStrict :: Bool
280 opt_DictsStrict = lookUp (fsLit "-fdicts-strict")
282 opt_IrrefutableTuples :: Bool
283 opt_IrrefutableTuples = lookUp (fsLit "-firrefutable-tuples")
286 opt_Parallel = lookUp (fsLit "-fparallel")
288 opt_SimpleListLiterals :: Bool
289 opt_SimpleListLiterals = lookUp (fsLit "-fsimple-list-literals")
291 opt_NoStateHack :: Bool
292 opt_NoStateHack = lookUp (fsLit "-fno-state-hack")
295 opt_CprOff = lookUp (fsLit "-fcpr-off")
296 -- Switch off CPR analysis in the new demand analyser
297 opt_MaxWorkerArgs :: Int
298 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
300 opt_GranMacros :: Bool
301 opt_GranMacros = lookUp (fsLit "-fgransim")
303 opt_HiVersion :: Integer
304 opt_HiVersion = read (cProjectVersionInt ++ cProjectPatchLevel) :: Integer
306 opt_HistorySize :: Int
307 opt_HistorySize = lookup_def_int "-fhistory-size" 20
309 opt_OmitBlackHoling :: Bool
310 opt_OmitBlackHoling = lookUp (fsLit "-dno-black-holing")
312 opt_StubDeadValues :: Bool
313 opt_StubDeadValues = lookUp (fsLit "-dstub-dead-values")
315 -- Simplifier switches
316 opt_SimplNoPreInlining :: Bool
317 opt_SimplNoPreInlining = lookUp (fsLit "-fno-pre-inlining")
318 -- NoPreInlining is there just to see how bad things
319 -- get if you don't do it!
320 opt_SimplExcessPrecision :: Bool
321 opt_SimplExcessPrecision = lookUp (fsLit "-fexcess-precision")
323 opt_NoOptCoercion :: Bool
324 opt_NoOptCoercion = lookUp (fsLit "-fno-opt-coercion")
327 -- See Note [Discounts and thresholds] in CoreUnfold
329 opt_UF_CreationThreshold, opt_UF_UseThreshold :: Int
330 opt_UF_DearOp, opt_UF_FunAppDiscount, opt_UF_DictDiscount :: Int
331 opt_UF_KeenessFactor :: Float
333 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
334 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (6::Int)
335 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int)
337 opt_UF_DictDiscount = lookup_def_int "-funfolding-dict-discount" (3::Int)
338 -- Be fairly keen to inline a fuction if that means
339 -- we'll be able to pick the right method from a dictionary
341 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
342 opt_UF_DearOp = ( 4 :: Int)
345 -- Related to linking
347 #if darwin_TARGET_OS && x86_64_TARGET_ARCH
349 #elif darwin_TARGET_OS
350 opt_PIC = lookUp (fsLit "-fPIC") || not opt_Static
352 opt_PIC = lookUp (fsLit "-fPIC")
355 opt_Static = lookUp (fsLit "-static")
356 opt_Unregisterised :: Bool
357 opt_Unregisterised = lookUp (fsLit "-funregisterised")
359 -- Derived, not a real option. Determines whether we will be compiling
360 -- info tables that reside just before the entry code, or with an
361 -- indirection to the entry code. See TABLES_NEXT_TO_CODE in
362 -- includes/rts/storage/InfoTables.h.
363 tablesNextToCode :: Bool
364 tablesNextToCode = not opt_Unregisterised
365 && cGhcEnableTablesNextToCode == "YES"
367 -- Include full span info in error messages, instead of just the start position.
368 opt_ErrorSpans :: Bool
369 opt_ErrorSpans = lookUp (fsLit "-ferror-spans")
372 opt_Ticky = lookUp (fsLit "-ticky")
374 -- object files and libraries to be linked in are collected here.
375 -- ToDo: perhaps this could be done without a global, it wasn't obvious
376 -- how to do it though --SDM.
377 GLOBAL_VAR(v_Ld_inputs, [], [String])
379 -----------------------------------------------------------------------------
382 -- The central concept of a "way" is that all objects in a given
383 -- program must be compiled in the same "way". Certain options change
384 -- parameters of the virtual machine, eg. profiling adds an extra word
385 -- to the object header, so profiling objects cannot be linked with
386 -- non-profiling objects.
388 -- After parsing the command-line options, we determine which "way" we
389 -- are building - this might be a combination way, eg. profiling+threaded.
391 -- We then find the "build-tag" associated with this way, and this
392 -- becomes the suffix used to find .hi files and libraries used in
406 GLOBAL_VAR(v_Ways, [] ,[Way])
408 allowed_combination :: [WayName] -> Bool
409 allowed_combination way = and [ x `allowedWith` y
410 | x <- way, y <- way, x < y ]
412 -- Note ordering in these tests: the left argument is
413 -- <= the right argument, according to the Ord instance
416 -- dyn is allowed with everything
417 _ `allowedWith` WayDyn = True
418 WayDyn `allowedWith` _ = True
420 -- debug is allowed with everything
421 _ `allowedWith` WayDebug = True
422 WayDebug `allowedWith` _ = True
424 WayProf `allowedWith` WayNDP = True
425 WayThreaded `allowedWith` WayProf = True
426 WayThreaded `allowedWith` WayEventLog = True
427 _ `allowedWith` _ = False
430 getWayFlags :: IO [String] -- new options
432 unsorted <- readIORef v_Ways
433 let ways = sortBy (compare `on` wayName) $
434 nubBy ((==) `on` wayName) $ unsorted
435 writeIORef v_Ways ways
437 if not (allowed_combination (map wayName ways))
438 then ghcError (CmdLineError $
439 "combination not supported: " ++
440 foldr1 (\a b -> a ++ '/':b)
443 return (concatMap wayOpts ways)
445 mkBuildTag :: [Way] -> String
446 mkBuildTag ways = concat (intersperse "_" (map wayTag ways))
448 lkupWay :: WayName -> Way
450 case listToMaybe (filter ((==) w . wayName) way_details) of
451 Nothing -> error "findBuildTag"
452 Just details -> details
454 isRTSWay :: WayName -> Bool
455 isRTSWay = wayRTSOnly . lkupWay
465 way_details :: [ Way ]
467 [ Way WayThreaded "thr" True "Threaded" [
468 #if defined(freebsd_TARGET_OS)
471 -- FreeBSD's default threading library is the KSE-based M:N libpthread,
472 -- which GHC has some problems with. It's currently not clear whether
473 -- the problems are our fault or theirs, but it seems that using the
474 -- alternative 1:1 threading library libthr works around it:
476 #elif defined(openbsd_TARGET_OS)
479 #elif defined(solaris2_TARGET_OS)
484 Way WayDebug "debug" True "Debug" [],
486 Way WayDyn "dyn" False "Dynamic"
489 #if defined(mingw32_TARGET_OS)
490 -- On Windows, code that is to be linked into a dynamic library must be compiled
491 -- with -fPIC. Labels not in the current package are assumed to be in a DLL
492 -- different from the current one.
494 #elif defined(openbsd_TARGET_OS)
495 -- Without this, linking the shared libHSffi fails because
496 -- it uses pthread mutexes.
501 Way WayProf "p" False "Profiling"
504 , "-optc-DPROFILING" ],
506 Way WayEventLog "l" True "RTS Event Logging"
508 , "-optc-DTRACING" ],
510 Way WayPar "mp" False "Parallel"
512 , "-D__PARALLEL_HASKELL__"
514 , "-package concurrent"
516 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
520 -- at the moment we only change the RTS and could share compiler and libs!
521 Way WayPar "mt" False "Parallel ticky profiling"
523 , "-D__PARALLEL_HASKELL__"
526 , "-package concurrent"
528 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
532 Way WayPar "md" False "Distributed"
534 , "-D__PARALLEL_HASKELL__"
535 , "-D__DISTRIBUTED_HASKELL__"
538 , "-package concurrent"
540 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
544 Way WayGran "mg" False "GranSim"
548 , "-package concurrent" ],
550 Way WayNDP "ndp" False "Nested data parallelism"