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