UNDO: Add -fpass-case-bndr-to-join-points
[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(..), v_Ways, v_Build_tag, v_RTS_Build_tag, isRTSWay,
21
22         -- Output style options
23         opt_PprUserLength,
24         opt_SuppressUniques,
25         opt_PprStyle_Debug,
26         opt_NoDebugOutput,
27
28         -- profiling opts
29         opt_SccProfilingOn,
30         opt_DoTickyProfiling,
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         opt_EmitExternalCore,
73         v_Ld_inputs,
74         tablesNextToCode,
75         opt_StubDeadValues,
76
77     -- For the parser
78     addOpt, removeOpt, addWay, findBuildTag, v_opt_C_ready
79   ) where
80
81 #include "HsVersions.h"
82
83 import Config
84 import FastString
85 import Util
86 import Maybes           ( firstJust )
87 import Panic
88
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
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 (maybePrefixMatch 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_PprStyle_Debug  :: Bool
187 opt_PprStyle_Debug              = lookUp  (fsLit "-dppr-debug")
188 opt_PprUserLength   :: Int
189 opt_PprUserLength               = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
190 opt_Fuel            :: Int
191 opt_Fuel                        = lookup_def_int "-dopt-fuel" maxBound
192 opt_NoDebugOutput   :: Bool
193 opt_NoDebugOutput               = lookUp  (fsLit "-dno-debug-output")
194
195
196 -- profiling opts
197 opt_SccProfilingOn :: Bool
198 opt_SccProfilingOn              = lookUp  (fsLit "-fscc-profiling")
199 opt_DoTickyProfiling :: Bool
200 opt_DoTickyProfiling            = WayTicky `elem` (unsafePerformIO $ readIORef v_Ways)
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"       (8::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/InfoTables.h.
283 tablesNextToCode :: Bool
284 tablesNextToCode                = not opt_Unregisterised
285                                   && cGhcEnableTablesNextToCode == "YES"
286
287 opt_EmitExternalCore :: Bool
288 opt_EmitExternalCore            = lookUp  (fsLit "-fext-core")
289
290 -- Include full span info in error messages, instead of just the start position.
291 opt_ErrorSpans :: Bool
292 opt_ErrorSpans                  = lookUp (fsLit "-ferror-spans")
293
294
295 -- object files and libraries to be linked in are collected here.
296 -- ToDo: perhaps this could be done without a global, it wasn't obvious
297 -- how to do it though --SDM.
298 GLOBAL_VAR(v_Ld_inputs, [],      [String])
299
300 -----------------------------------------------------------------------------
301 -- Ways
302
303 -- The central concept of a "way" is that all objects in a given
304 -- program must be compiled in the same "way".  Certain options change
305 -- parameters of the virtual machine, eg. profiling adds an extra word
306 -- to the object header, so profiling objects cannot be linked with
307 -- non-profiling objects.
308
309 -- After parsing the command-line options, we determine which "way" we
310 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
311
312 -- We then find the "build-tag" associated with this way, and this
313 -- becomes the suffix used to find .hi files and libraries used in
314 -- this compilation.
315
316 GLOBAL_VAR(v_Build_tag, "", String)
317
318 -- The RTS has its own build tag, because there are some ways that
319 -- affect the RTS only.
320 GLOBAL_VAR(v_RTS_Build_tag, "", String)
321
322 data WayName
323   = WayThreaded
324   | WayDebug
325   | WayProf
326   | WayTicky
327   | WayPar
328   | WayGran
329   | WayNDP
330   | WayUser_a
331   | WayUser_b
332   | WayUser_c
333   | WayUser_d
334   | WayUser_e
335   | WayUser_f
336   | WayUser_g
337   | WayUser_h
338   | WayUser_i
339   | WayUser_j
340   | WayUser_k
341   | WayUser_l
342   | WayUser_m
343   | WayUser_n
344   | WayUser_o
345   | WayUser_A
346   | WayUser_B
347   deriving (Eq,Ord)
348
349 GLOBAL_VAR(v_Ways, [] ,[WayName])
350
351 allowed_combination :: [WayName] -> Bool
352 allowed_combination way = and [ x `allowedWith` y 
353                               | x <- way, y <- way, x < y ]
354   where
355         -- Note ordering in these tests: the left argument is
356         -- <= the right argument, according to the Ord instance
357         -- on Way above.
358
359         -- debug is allowed with everything
360         _ `allowedWith` WayDebug                = True
361         WayDebug `allowedWith` _                = True
362
363         WayProf `allowedWith` WayNDP            = True
364         WayThreaded `allowedWith` WayProf       = True
365         _ `allowedWith` _                       = False
366
367
368 findBuildTag :: IO [String]  -- new options
369 findBuildTag = do
370   way_names <- readIORef v_Ways
371   let ws = sort (nub way_names)
372
373   if not (allowed_combination ws)
374       then ghcError (CmdLineError $
375                     "combination not supported: "  ++
376                     foldr1 (\a b -> a ++ '/':b) 
377                     (map (wayName . lkupWay) ws))
378       else let ways    = map lkupWay ws
379                tag     = mkBuildTag (filter (not.wayRTSOnly) ways)
380                rts_tag = mkBuildTag ways
381                flags   = map wayOpts ways
382            in do
383            writeIORef v_Build_tag tag
384            writeIORef v_RTS_Build_tag rts_tag
385            return (concat flags)
386
387
388
389 mkBuildTag :: [Way] -> String
390 mkBuildTag ways = concat (intersperse "_" (map wayTag ways))
391
392 lkupWay :: WayName -> Way
393 lkupWay w = 
394    case lookup w way_details of
395         Nothing -> error "findBuildTag"
396         Just details -> details
397
398 isRTSWay :: WayName -> Bool
399 isRTSWay = wayRTSOnly . lkupWay 
400
401 data Way = Way {
402   wayTag     :: String,
403   wayRTSOnly :: Bool,
404   wayName    :: String,
405   wayOpts    :: [String]
406   }
407
408 way_details :: [ (WayName, Way) ]
409 way_details =
410   [ (WayThreaded, Way "thr" True "Threaded" [
411 #if defined(freebsd_TARGET_OS)
412 --        "-optc-pthread"
413 --      , "-optl-pthread"
414         -- FreeBSD's default threading library is the KSE-based M:N libpthread,
415         -- which GHC has some problems with.  It's currently not clear whether
416         -- the problems are our fault or theirs, but it seems that using the
417         -- alternative 1:1 threading library libthr works around it:
418           "-optl-lthr"
419 #elif defined(solaris2_TARGET_OS)
420           "-optl-lrt"
421 #endif
422         ] ),
423
424     (WayDebug, Way "debug" True "Debug" [] ),
425
426     (WayProf, Way  "p" False "Profiling"
427         [ "-fscc-profiling"
428         , "-DPROFILING"
429         , "-optc-DPROFILING" ]),
430
431     (WayTicky, Way  "t" True "Ticky-ticky Profiling"  
432         [ "-DTICKY_TICKY"
433         , "-optc-DTICKY_TICKY" ]),
434
435     -- optl's below to tell linker where to find the PVM library -- HWL
436     (WayPar, Way  "mp" False "Parallel" 
437         [ "-fparallel"
438         , "-D__PARALLEL_HASKELL__"
439         , "-optc-DPAR"
440         , "-package concurrent"
441         , "-optc-w"
442         , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
443         , "-optl-lpvm3"
444         , "-optl-lgpvm3" ]),
445
446     -- at the moment we only change the RTS and could share compiler and libs!
447     (WayPar, Way  "mt" False "Parallel ticky profiling" 
448         [ "-fparallel"
449         , "-D__PARALLEL_HASKELL__"
450         , "-optc-DPAR"
451         , "-optc-DPAR_TICKY"
452         , "-package concurrent"
453         , "-optc-w"
454         , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
455         , "-optl-lpvm3"
456         , "-optl-lgpvm3" ]),
457
458     (WayPar, Way  "md" False "Distributed" 
459         [ "-fparallel"
460         , "-D__PARALLEL_HASKELL__"
461         , "-D__DISTRIBUTED_HASKELL__"
462         , "-optc-DPAR"
463         , "-optc-DDIST"
464         , "-package concurrent"
465         , "-optc-w"
466         , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
467         , "-optl-lpvm3"
468         , "-optl-lgpvm3" ]),
469
470     (WayGran, Way  "mg" False "GranSim"
471         [ "-fgransim"
472         , "-D__GRANSIM__"
473         , "-optc-DGRAN"
474         , "-package concurrent" ]),
475
476     (WayNDP, Way  "ndp" False "Nested data parallelism"
477         [ "-XParr"
478         , "-fvectorise"]),
479
480     (WayUser_a,  Way  "a"  False "User way 'a'"  ["$WAY_a_REAL_OPTS"]), 
481     (WayUser_b,  Way  "b"  False "User way 'b'"  ["$WAY_b_REAL_OPTS"]), 
482     (WayUser_c,  Way  "c"  False "User way 'c'"  ["$WAY_c_REAL_OPTS"]), 
483     (WayUser_d,  Way  "d"  False "User way 'd'"  ["$WAY_d_REAL_OPTS"]), 
484     (WayUser_e,  Way  "e"  False "User way 'e'"  ["$WAY_e_REAL_OPTS"]), 
485     (WayUser_f,  Way  "f"  False "User way 'f'"  ["$WAY_f_REAL_OPTS"]), 
486     (WayUser_g,  Way  "g"  False "User way 'g'"  ["$WAY_g_REAL_OPTS"]), 
487     (WayUser_h,  Way  "h"  False "User way 'h'"  ["$WAY_h_REAL_OPTS"]), 
488     (WayUser_i,  Way  "i"  False "User way 'i'"  ["$WAY_i_REAL_OPTS"]), 
489     (WayUser_j,  Way  "j"  False "User way 'j'"  ["$WAY_j_REAL_OPTS"]), 
490     (WayUser_k,  Way  "k"  False "User way 'k'"  ["$WAY_k_REAL_OPTS"]), 
491     (WayUser_l,  Way  "l"  False "User way 'l'"  ["$WAY_l_REAL_OPTS"]), 
492     (WayUser_m,  Way  "m"  False "User way 'm'"  ["$WAY_m_REAL_OPTS"]), 
493     (WayUser_n,  Way  "n"  False "User way 'n'"  ["$WAY_n_REAL_OPTS"]), 
494     (WayUser_o,  Way  "o"  False "User way 'o'"  ["$WAY_o_REAL_OPTS"]), 
495     (WayUser_A,  Way  "A"  False "User way 'A'"  ["$WAY_A_REAL_OPTS"]), 
496     (WayUser_B,  Way  "B"  False "User way 'B'"  ["$WAY_B_REAL_OPTS"]) 
497   ]
498