New flag -dsuppress-coercions to avoid printing coercions in Core dumps
[ghc-hetmet.git] / compiler / main / StaticFlags.hs
1 {-# OPTIONS -fno-cse #-}
2 -- -fno-cse is needed for GLOBAL_VAR's to behave properly
3
4 -----------------------------------------------------------------------------
5 --
6 -- Static flags
7 --
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.
10 --
11 -- (c) The University of Glasgow 2005
12 --
13 -----------------------------------------------------------------------------
14
15 module StaticFlags (
16         staticFlags,
17         initStaticOpts,
18
19         -- Ways
20         WayName(..), Way(..), v_Ways, isRTSWay, mkBuildTag,
21
22         -- Output style options
23         opt_PprUserLength,
24         opt_SuppressUniques,
25         opt_SuppressCoercions,
26         opt_PprStyle_Debug,
27         opt_NoDebugOutput,
28
29         -- profiling opts
30         opt_SccProfilingOn,
31
32         -- Hpc opts
33         opt_Hpc,
34
35         -- language opts
36         opt_DictsStrict,
37         opt_IrrefutableTuples,
38         opt_Parallel,
39
40         -- optimisation opts
41         opt_DsMultiTyVar,
42         opt_NoStateHack,
43         opt_SimpleListLiterals,
44         opt_SpecInlineJoinPoints,
45         opt_CprOff,
46         opt_SimplNoPreInlining,
47         opt_SimplExcessPrecision,
48         opt_MaxWorkerArgs,
49
50         -- Unfolding control
51         opt_UF_CreationThreshold,
52         opt_UF_UseThreshold,
53         opt_UF_FunAppDiscount,
54         opt_UF_KeenessFactor,
55         opt_UF_DearOp,
56
57         -- Optimization fuel controls
58         opt_Fuel,
59
60         -- Related to linking
61         opt_PIC,
62         opt_Static,
63
64         -- misc opts
65         opt_IgnoreDotGhci,
66         opt_ErrorSpans,
67         opt_GranMacros,
68         opt_HiVersion,
69         opt_HistorySize,
70         opt_OmitBlackHoling,
71         opt_Unregisterised,
72         v_Ld_inputs,
73         tablesNextToCode,
74         opt_StubDeadValues,
75
76     -- For the parser
77     addOpt, removeOpt, addWay, getWayFlags, v_opt_C_ready
78   ) where
79
80 #include "HsVersions.h"
81
82 import Config
83 import FastString
84 import Util
85 import Maybes           ( firstJust )
86 import Panic
87
88 import Data.Maybe       ( listToMaybe )
89 import Data.IORef
90 import System.IO.Unsafe ( unsafePerformIO )
91 import Data.List
92
93 -----------------------------------------------------------------------------
94 -- Static flags
95
96 initStaticOpts :: IO ()
97 initStaticOpts = writeIORef v_opt_C_ready True
98
99 addOpt :: String -> IO ()
100 addOpt = consIORef v_opt_C
101
102 addWay :: WayName -> IO ()
103 addWay = consIORef v_Ways . lkupWay
104
105 removeOpt :: String -> IO ()
106 removeOpt f = do
107   fs <- readIORef v_opt_C
108   writeIORef v_opt_C $! filter (/= f) fs    
109
110 lookUp           :: FastString -> Bool
111 lookup_def_int   :: String -> Int -> Int
112 lookup_def_float :: String -> Float -> Float
113 lookup_str       :: String -> Maybe String
114
115 -- holds the static opts while they're being collected, before
116 -- being unsafely read by unpacked_static_opts below.
117 GLOBAL_VAR(v_opt_C, defaultStaticOpts, [String])
118 GLOBAL_VAR(v_opt_C_ready, False, Bool)
119
120 staticFlags :: [String]
121 staticFlags = unsafePerformIO $ do
122   ready <- readIORef v_opt_C_ready
123   if (not ready)
124         then panic "Static flags have not been initialised!\n        Please call GHC.newSession or GHC.parseStaticFlags early enough."
125         else readIORef v_opt_C
126
127 -- -static is the default
128 defaultStaticOpts :: [String]
129 defaultStaticOpts = ["-static"]
130
131 packed_static_opts :: [FastString]
132 packed_static_opts   = map mkFastString staticFlags
133
134 lookUp     sw = sw `elem` packed_static_opts
135         
136 -- (lookup_str "foo") looks for the flag -foo=X or -fooX, 
137 -- and returns the string X
138 lookup_str sw 
139    = case firstJust (map (stripPrefix sw) staticFlags) of
140         Just ('=' : str) -> Just str
141         Just str         -> Just str
142         Nothing          -> Nothing     
143
144 lookup_def_int sw def = case (lookup_str sw) of
145                             Nothing -> def              -- Use default
146                             Just xx -> try_read sw xx
147
148 lookup_def_float sw def = case (lookup_str sw) of
149                             Nothing -> def              -- Use default
150                             Just xx -> try_read sw xx
151
152
153 try_read :: Read a => String -> String -> a
154 -- (try_read sw str) tries to read s; if it fails, it
155 -- bleats about flag sw
156 try_read sw str
157   = case reads str of
158         ((x,_):_) -> x  -- Be forgiving: ignore trailing goop, and alternative parses
159         []        -> ghcError (UsageError ("Malformed argument " ++ str ++ " for flag " ++ sw))
160                         -- ToDo: hack alert. We should really parse the arugments
161                         --       and announce errors in a more civilised way.
162
163
164 {-
165  Putting the compiler options into temporary at-files
166  may turn out to be necessary later on if we turn hsc into
167  a pure Win32 application where I think there's a command-line
168  length limit of 255. unpacked_opts understands the @ option.
169
170 unpacked_opts :: [String]
171 unpacked_opts =
172   concat $
173   map (expandAts) $
174   map unpackFS argv  -- NOT ARGV any more: v_Static_hsc_opts
175   where
176    expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
177    expandAts l = [l]
178 -}
179
180 opt_IgnoreDotGhci :: Bool
181 opt_IgnoreDotGhci               = lookUp (fsLit "-ignore-dot-ghci")
182
183 -- debugging opts
184 opt_SuppressUniques :: Bool
185 opt_SuppressUniques             = lookUp  (fsLit "-dsuppress-uniques")
186 opt_SuppressCoercions :: Bool
187 opt_SuppressCoercions           = lookUp  (fsLit "-dsuppress-coercions")
188 opt_PprStyle_Debug  :: Bool
189 opt_PprStyle_Debug              = lookUp  (fsLit "-dppr-debug")
190 opt_PprUserLength   :: Int
191 opt_PprUserLength               = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
192 opt_Fuel            :: Int
193 opt_Fuel                        = lookup_def_int "-dopt-fuel" maxBound
194 opt_NoDebugOutput   :: Bool
195 opt_NoDebugOutput               = lookUp  (fsLit "-dno-debug-output")
196
197
198 -- profiling opts
199 opt_SccProfilingOn :: Bool
200 opt_SccProfilingOn              = lookUp  (fsLit "-fscc-profiling")
201
202 -- Hpc opts
203 opt_Hpc :: Bool
204 opt_Hpc                         = lookUp (fsLit "-fhpc")  
205
206 -- language opts
207 opt_DictsStrict :: Bool
208 opt_DictsStrict                 = lookUp  (fsLit "-fdicts-strict")
209 opt_IrrefutableTuples :: Bool
210 opt_IrrefutableTuples           = lookUp  (fsLit "-firrefutable-tuples")
211 opt_Parallel :: Bool
212 opt_Parallel                    = lookUp  (fsLit "-fparallel")
213
214 -- optimisation opts
215 opt_DsMultiTyVar :: Bool
216 opt_DsMultiTyVar                = not (lookUp (fsLit "-fno-ds-multi-tyvar"))
217         -- On by default
218
219 opt_SpecInlineJoinPoints :: Bool
220 opt_SpecInlineJoinPoints        = lookUp  (fsLit "-fspec-inline-join-points")
221
222 opt_SimpleListLiterals :: Bool
223 opt_SimpleListLiterals          = lookUp  (fsLit "-fsimple-list-literals")
224
225 opt_NoStateHack :: Bool
226 opt_NoStateHack                 = lookUp  (fsLit "-fno-state-hack")
227
228 opt_CprOff :: Bool
229 opt_CprOff                      = lookUp  (fsLit "-fcpr-off")
230         -- Switch off CPR analysis in the new demand analyser
231 opt_MaxWorkerArgs :: Int
232 opt_MaxWorkerArgs               = lookup_def_int "-fmax-worker-args" (10::Int)
233
234 opt_GranMacros :: Bool
235 opt_GranMacros                  = lookUp  (fsLit "-fgransim")
236 opt_HiVersion :: Integer
237 opt_HiVersion                   = read (cProjectVersionInt ++ cProjectPatchLevel) :: Integer
238 opt_HistorySize :: Int
239 opt_HistorySize                 = lookup_def_int "-fhistory-size" 20
240 opt_OmitBlackHoling :: Bool
241 opt_OmitBlackHoling             = lookUp  (fsLit "-dno-black-holing")
242 opt_StubDeadValues  :: Bool
243 opt_StubDeadValues              = lookUp  (fsLit "-dstub-dead-values")
244
245 -- Simplifier switches
246 opt_SimplNoPreInlining :: Bool
247 opt_SimplNoPreInlining          = lookUp  (fsLit "-fno-pre-inlining")
248         -- NoPreInlining is there just to see how bad things
249         -- get if you don't do it!
250 opt_SimplExcessPrecision :: Bool
251 opt_SimplExcessPrecision        = lookUp  (fsLit "-fexcess-precision")
252
253 -- Unfolding control
254 opt_UF_CreationThreshold :: Int
255 opt_UF_CreationThreshold        = lookup_def_int "-funfolding-creation-threshold"  (45::Int)
256 opt_UF_UseThreshold :: Int
257 opt_UF_UseThreshold             = lookup_def_int "-funfolding-use-threshold"       (6::Int)     -- Discounts can be big
258 opt_UF_FunAppDiscount :: Int
259 opt_UF_FunAppDiscount           = lookup_def_int "-funfolding-fun-discount"        (6::Int)     -- It's great to inline a fn
260 opt_UF_KeenessFactor :: Float
261 opt_UF_KeenessFactor            = lookup_def_float "-funfolding-keeness-factor"    (1.5::Float)
262
263 opt_UF_DearOp :: Int
264 opt_UF_DearOp   = ( 4 :: Int)
265
266
267 -- Related to linking
268 opt_PIC :: Bool
269 #if darwin_TARGET_OS && x86_64_TARGET_ARCH
270 opt_PIC                         = True
271 #else
272 opt_PIC                         = lookUp (fsLit "-fPIC")
273 #endif
274 opt_Static :: Bool
275 opt_Static                      = lookUp  (fsLit "-static")
276 opt_Unregisterised :: Bool
277 opt_Unregisterised              = lookUp  (fsLit "-funregisterised")
278
279 -- Derived, not a real option.  Determines whether we will be compiling
280 -- info tables that reside just before the entry code, or with an
281 -- indirection to the entry code.  See TABLES_NEXT_TO_CODE in 
282 -- includes/rts/storage/InfoTables.h.
283 tablesNextToCode :: Bool
284 tablesNextToCode                = not opt_Unregisterised
285                                   && cGhcEnableTablesNextToCode == "YES"
286
287 -- Include full span info in error messages, instead of just the start position.
288 opt_ErrorSpans :: Bool
289 opt_ErrorSpans                  = lookUp (fsLit "-ferror-spans")
290
291
292 -- object files and libraries to be linked in are collected here.
293 -- ToDo: perhaps this could be done without a global, it wasn't obvious
294 -- how to do it though --SDM.
295 GLOBAL_VAR(v_Ld_inputs, [],      [String])
296
297 -----------------------------------------------------------------------------
298 -- Ways
299
300 -- The central concept of a "way" is that all objects in a given
301 -- program must be compiled in the same "way".  Certain options change
302 -- parameters of the virtual machine, eg. profiling adds an extra word
303 -- to the object header, so profiling objects cannot be linked with
304 -- non-profiling objects.
305
306 -- After parsing the command-line options, we determine which "way" we
307 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
308
309 -- We then find the "build-tag" associated with this way, and this
310 -- becomes the suffix used to find .hi files and libraries used in
311 -- this compilation.
312
313 data WayName
314   = WayThreaded
315   | WayDebug
316   | WayProf
317   | WayEventLog
318   | WayTicky
319   | WayPar
320   | WayGran
321   | WayNDP
322   | WayDyn
323   deriving (Eq,Ord)
324
325 GLOBAL_VAR(v_Ways, [] ,[Way])
326
327 allowed_combination :: [WayName] -> Bool
328 allowed_combination way = and [ x `allowedWith` y 
329                               | x <- way, y <- way, x < y ]
330   where
331         -- Note ordering in these tests: the left argument is
332         -- <= the right argument, according to the Ord instance
333         -- on Way above.
334
335         -- dyn is allowed with everything
336         _ `allowedWith` WayDyn                  = True
337         WayDyn `allowedWith` _                  = True
338
339         -- debug is allowed with everything
340         _ `allowedWith` WayDebug                = True
341         WayDebug `allowedWith` _                = True
342
343         WayProf `allowedWith` WayNDP            = True
344         WayThreaded `allowedWith` WayProf       = True
345         WayThreaded `allowedWith` WayEventLog   = True
346         _ `allowedWith` _                       = False
347
348
349 getWayFlags :: IO [String]  -- new options
350 getWayFlags = do
351   unsorted <- readIORef v_Ways
352   let ways = sortBy (compare `on` wayName) $
353              nubBy  ((==) `on` wayName) $ unsorted
354   writeIORef v_Ways ways
355
356   if not (allowed_combination (map wayName ways))
357       then ghcError (CmdLineError $
358                     "combination not supported: "  ++
359                     foldr1 (\a b -> a ++ '/':b) 
360                     (map wayDesc ways))
361       else
362            return (concatMap wayOpts ways)
363
364 mkBuildTag :: [Way] -> String
365 mkBuildTag ways = concat (intersperse "_" (map wayTag ways))
366
367 lkupWay :: WayName -> Way
368 lkupWay w = 
369    case listToMaybe (filter ((==) w . wayName) way_details) of
370         Nothing -> error "findBuildTag"
371         Just details -> details
372
373 isRTSWay :: WayName -> Bool
374 isRTSWay = wayRTSOnly . lkupWay 
375
376 data Way = Way {
377   wayName    :: WayName,
378   wayTag     :: String,
379   wayRTSOnly :: Bool,
380   wayDesc    :: String,
381   wayOpts    :: [String]
382   }
383
384 way_details :: [ Way ]
385 way_details =
386   [ Way WayThreaded "thr" True "Threaded" [
387 #if defined(freebsd_TARGET_OS)
388 --        "-optc-pthread"
389 --      , "-optl-pthread"
390         -- FreeBSD's default threading library is the KSE-based M:N libpthread,
391         -- which GHC has some problems with.  It's currently not clear whether
392         -- the problems are our fault or theirs, but it seems that using the
393         -- alternative 1:1 threading library libthr works around it:
394           "-optl-lthr"
395 #elif defined(solaris2_TARGET_OS)
396           "-optl-lrt"
397 #endif
398         ],
399
400     Way WayDebug "debug" True "Debug" [],
401
402     Way WayDyn "dyn" False "Dynamic"
403         [ "-DDYNAMIC"
404         , "-optc-DDYNAMIC" ],
405
406     Way WayProf "p" False "Profiling"
407         [ "-fscc-profiling"
408         , "-DPROFILING"
409         , "-optc-DPROFILING" ],
410
411     Way WayEventLog "l" True "RTS Event Logging"
412         [ "-DTRACING"
413         , "-optc-DTRACING" ],
414
415     Way WayTicky "t" True "Ticky-ticky Profiling"  
416         [ "-DTICKY_TICKY"
417         , "-optc-DTICKY_TICKY" ],
418
419     Way WayPar "mp" False "Parallel" 
420         [ "-fparallel"
421         , "-D__PARALLEL_HASKELL__"
422         , "-optc-DPAR"
423         , "-package concurrent"
424         , "-optc-w"
425         , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
426         , "-optl-lpvm3"
427         , "-optl-lgpvm3" ],
428
429     -- at the moment we only change the RTS and could share compiler and libs!
430     Way WayPar "mt" False "Parallel ticky profiling" 
431         [ "-fparallel"
432         , "-D__PARALLEL_HASKELL__"
433         , "-optc-DPAR"
434         , "-optc-DPAR_TICKY"
435         , "-package concurrent"
436         , "-optc-w"
437         , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
438         , "-optl-lpvm3"
439         , "-optl-lgpvm3" ],
440
441     Way WayPar "md" False "Distributed" 
442         [ "-fparallel"
443         , "-D__PARALLEL_HASKELL__"
444         , "-D__DISTRIBUTED_HASKELL__"
445         , "-optc-DPAR"
446         , "-optc-DDIST"
447         , "-package concurrent"
448         , "-optc-w"
449         , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
450         , "-optl-lpvm3"
451         , "-optl-lgpvm3" ],
452
453     Way WayGran "mg" False "GranSim"
454         [ "-fgransim"
455         , "-D__GRANSIM__"
456         , "-optc-DGRAN"
457         , "-package concurrent" ],
458
459     Way WayNDP "ndp" False "Nested data parallelism"
460         [ "-XParr"
461         , "-fvectorise"]
462   ]