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