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