1 -----------------------------------------------------------------------------
2 -- $Id: DriverState.hs,v 1.44 2001/06/14 12:50:06 simonpj Exp $
4 -- Settings for the driver
6 -- (c) The University of Glasgow 2000
8 -----------------------------------------------------------------------------
10 module DriverState where
12 #include "../includes/config.h"
13 #include "HsVersions.h"
28 -----------------------------------------------------------------------------
29 -- non-configured things
31 cHaskell1Version = "5" -- i.e., Haskell 98
33 -----------------------------------------------------------------------------
34 -- Global compilation flags
37 v_Hs_source_cpp_opts = global
38 [ "-D__HASKELL1__="++cHaskell1Version
39 , "-D__GLASGOW_HASKELL__="++cProjectVersionInt
41 , "-D__CONCURRENT_HASKELL__"
43 {-# NOINLINE v_Hs_source_cpp_opts #-}
45 -- Keep output from intermediate phases
46 GLOBAL_VAR(v_Keep_hi_diffs, False, Bool)
47 GLOBAL_VAR(v_Keep_hc_files, False, Bool)
48 GLOBAL_VAR(v_Keep_s_files, False, Bool)
49 GLOBAL_VAR(v_Keep_raw_s_files, False, Bool)
50 GLOBAL_VAR(v_Keep_tmp_files, False, Bool)
53 GLOBAL_VAR(v_Scale_sizes_by, 1.0, Double)
54 GLOBAL_VAR(v_Static, True, Bool)
55 GLOBAL_VAR(v_NoHsMain, False, Bool)
56 GLOBAL_VAR(v_Recomp, True, Bool)
57 GLOBAL_VAR(v_Collect_ghc_timing, False, Bool)
58 GLOBAL_VAR(v_Do_asm_mangling, True, Bool)
59 GLOBAL_VAR(v_Excess_precision, False, Bool)
61 -----------------------------------------------------------------------------
62 -- Splitting object files (for libraries)
64 GLOBAL_VAR(v_Split_object_files, False, Bool)
65 GLOBAL_VAR(v_Split_info, ("",0), (String,Int))
66 -- The split prefix and number of files
70 can_split = prefixMatch "i386" cTARGETPLATFORM
71 || prefixMatch "alpha" cTARGETPLATFORM
72 || prefixMatch "hppa" cTARGETPLATFORM
73 || prefixMatch "m68k" cTARGETPLATFORM
74 || prefixMatch "mips" cTARGETPLATFORM
75 || prefixMatch "powerpc" cTARGETPLATFORM
76 || prefixMatch "rs6000" cTARGETPLATFORM
77 || prefixMatch "sparc" cTARGETPLATFORM
79 -----------------------------------------------------------------------------
80 -- Compiler output options
83 | cGhcWithNativeCodeGen == "YES" &&
84 (prefixMatch "i386" cTARGETPLATFORM ||
85 prefixMatch "sparc" cTARGETPLATFORM) = HscAsm
88 GLOBAL_VAR(v_Output_dir, Nothing, Maybe String)
89 GLOBAL_VAR(v_Output_file, Nothing, Maybe String)
90 GLOBAL_VAR(v_Output_hi, Nothing, Maybe String)
92 GLOBAL_VAR(v_Object_suf, Nothing, Maybe String)
93 GLOBAL_VAR(v_HC_suf, Nothing, Maybe String)
94 GLOBAL_VAR(v_Hi_dir, Nothing, Maybe String)
95 GLOBAL_VAR(v_Hi_suf, "hi", String)
97 GLOBAL_VAR(v_Ld_inputs, [], [String])
99 odir_ify :: String -> IO String
101 odir_opt <- readIORef v_Output_dir
104 Just d -> return (newdir d f)
106 osuf_ify :: String -> IO String
108 osuf_opt <- readIORef v_Object_suf
111 Just s -> return (newsuf s f)
113 -----------------------------------------------------------------------------
114 -- Compiler optimisation options
116 GLOBAL_VAR(v_OptLevel, 0, Int)
118 setOptLevel :: String -> IO ()
119 setOptLevel "" = do { writeIORef v_OptLevel 1 }
120 setOptLevel "not" = writeIORef v_OptLevel 0
121 setOptLevel [c] | isDigit c = do
122 let level = ord c - ord '0'
123 writeIORef v_OptLevel level
124 setOptLevel s = unknownFlagErr ("-O"++s)
126 GLOBAL_VAR(v_minus_o2_for_C, False, Bool)
127 GLOBAL_VAR(v_MaxSimplifierIterations, 4, Int)
128 GLOBAL_VAR(v_StgStats, False, Bool)
129 GLOBAL_VAR(v_UsageSPInf, False, Bool) -- Off by default
130 GLOBAL_VAR(v_Strictness, True, Bool)
131 GLOBAL_VAR(v_CPR, True, Bool)
132 GLOBAL_VAR(v_CSE, True, Bool)
134 -- these are the static flags you get without -O.
137 "-fignore-interface-pragmas",
138 "-fomit-interface-pragmas",
139 "-fdo-lambda-eta-expansion", -- This one is important for a tiresome reason:
140 -- we want to make sure that the bindings for data
141 -- constructors are eta-expanded. This is probably
142 -- a good thing anyway, but it seems fragile.
146 -- these are the static flags you get when -O is on.
151 "-fdo-eta-reduction",
152 "-fdo-lambda-eta-expansion",
158 hsc_minusO2_flags = hsc_minusO_flags -- for now
160 getStaticOptimisationFlags 0 = hsc_minusNoO_flags
161 getStaticOptimisationFlags 1 = hsc_minusO_flags
162 getStaticOptimisationFlags n = hsc_minusO2_flags
164 buildCoreToDo :: IO [CoreToDo]
166 opt_level <- readIORef v_OptLevel
167 max_iter <- readIORef v_MaxSimplifierIterations
168 usageSP <- readIORef v_UsageSPInf
169 strictness <- readIORef v_Strictness
170 cpr <- readIORef v_CPR
171 cse <- readIORef v_CSE
173 if opt_level == 0 then return
175 CoreDoSimplify (isAmongSimpl [
176 MaxSimplifierIterations max_iter
180 else {- opt_level >= 1 -} return [
182 -- initial simplify: mk specialiser happy: minimum effort please
183 CoreDoSimplify (isAmongSimpl [
185 -- Don't inline anything till full laziness has bitten
186 -- In particular, inlining wrappers inhibits floating
187 -- e.g. ...(case f x of ...)...
188 -- ==> ...(case (case x of I# x# -> fw x#) of ...)...
189 -- ==> ...(case x of I# x# -> case fw x# of ...)...
190 -- and now the redex (f x) isn't floatable any more
192 -- Similarly, don't apply any rules until after full
193 -- laziness. Notably, list fusion can prevent floating.
195 -- Don't do case-of-case transformations.
196 -- This makes full laziness work better
197 MaxSimplifierIterations max_iter
200 -- Specialisation is best done before full laziness
201 -- so that overloaded functions have all their dictionary lambdas manifest
204 CoreDoFloatOutwards False{-not full-},
207 CoreDoSimplify (isAmongSimpl [
209 -- Want to run with inline phase 1 after the specialiser to give
210 -- maximum chance for fusion to work before we inline build/augment
211 -- in phase 2. This made a difference in 'ansi' where an
212 -- overloaded function wasn't inlined till too late.
213 MaxSimplifierIterations max_iter
216 -- infer usage information here in case we need it later.
217 -- (add more of these where you need them --KSW 1999-04)
218 if usageSP then CoreDoUSPInf else CoreDoNothing,
220 CoreDoSimplify (isAmongSimpl [
221 -- Need inline-phase2 here so that build/augment get
222 -- inlined. I found that spectral/hartel/genfft lost some useful
223 -- strictness in the function sumcode' if augment is not inlined
224 -- before strictness analysis runs
226 MaxSimplifierIterations max_iter
229 CoreDoSimplify (isAmongSimpl [
230 MaxSimplifierIterations 2
231 -- No -finline-phase: allow all Ids to be inlined now
232 -- This gets foldr inlined before strictness analysis
235 if strictness then CoreDoStrictness else CoreDoNothing,
236 if cpr then CoreDoCPResult else CoreDoNothing,
240 CoreDoSimplify (isAmongSimpl [
241 MaxSimplifierIterations max_iter
242 -- No -finline-phase: allow all Ids to be inlined now
245 CoreDoFloatOutwards False{-not full-},
246 -- nofib/spectral/hartel/wang doubles in speed if you
247 -- do full laziness late in the day. It only happens
248 -- after fusion and other stuff, so the early pass doesn't
249 -- catch it. For the record, the redex is
250 -- f_el22 (f_el21 r_midblock)
253 -- Leave out lambda lifting for now
254 -- "-fsimplify", -- Tidy up results of full laziness
256 -- "-fmax-simplifier-iterations2",
258 -- "-ffloat-outwards-full",
260 -- We want CSE to follow the final full-laziness pass, because it may
261 -- succeed in commoning up things floated out by full laziness.
262 -- CSE used to rely on the no-shadowing invariant, but it doesn't any more
264 if cse then CoreCSE else CoreDoNothing,
268 -- Case-liberation for -O2. This should be after
269 -- strictness analysis and the simplification which follows it.
271 if opt_level >= 2 then
275 if opt_level >= 2 then
280 -- Final clean-up simplification:
281 CoreDoSimplify (isAmongSimpl [
282 MaxSimplifierIterations max_iter
283 -- No -finline-phase: allow all Ids to be inlined now
287 buildStgToDo :: IO [ StgToDo ]
289 stg_stats <- readIORef v_StgStats
290 let flags1 | stg_stats = [ D_stg_stats ]
294 ways_ <- readIORef v_Ways
295 let flags2 | WayProf `elem` ways_ = StgDoMassageForProfiling : flags1
300 -----------------------------------------------------------------------------
303 split_marker = ':' -- not configurable (ToDo)
305 v_Import_paths, v_Include_paths, v_Library_paths :: IORef [String]
306 GLOBAL_VAR(v_Import_paths, ["."], [String])
307 GLOBAL_VAR(v_Include_paths, ["."], [String])
308 GLOBAL_VAR(v_Library_paths, [], [String])
310 GLOBAL_VAR(v_Cmdline_libraries, [], [String])
312 addToDirList :: IORef [String] -> String -> IO ()
313 addToDirList ref path
314 = do paths <- readIORef ref
315 writeIORef ref (paths ++ split split_marker path)
317 GLOBAL_VAR(v_HCHeader, "", String)
319 -----------------------------------------------------------------------------
322 -- package list is maintained in dependency order
323 GLOBAL_VAR(v_Packages, ("std":"rts":"gmp":[]), [String])
325 addPackage :: String -> IO ()
327 = do pkg_details <- readIORef v_Package_details
328 case lookupPkg package pkg_details of
329 Nothing -> throwDyn (CmdLineError ("unknown package name: " ++ package))
331 ps <- readIORef v_Packages
332 unless (package `elem` ps) $ do
333 mapM_ addPackage (package_deps details)
334 ps <- readIORef v_Packages
335 writeIORef v_Packages (package:ps)
337 getPackageImportPath :: IO [String]
338 getPackageImportPath = do
340 return (nub (filter (not.null) (concatMap import_dirs ps)))
342 getPackageIncludePath :: IO [String]
343 getPackageIncludePath = do
345 return (nub (filter (not.null) (concatMap include_dirs ps)))
347 -- includes are in reverse dependency order (i.e. rts first)
348 getPackageCIncludes :: IO [String]
349 getPackageCIncludes = do
351 return (reverse (nub (filter (not.null) (concatMap c_includes ps))))
353 getPackageLibraryPath :: IO [String]
354 getPackageLibraryPath = do
356 return (nub (filter (not.null) (concatMap library_dirs ps)))
358 getPackageLibraries :: IO [String]
359 getPackageLibraries = do
361 tag <- readIORef v_Build_tag
362 let suffix = if null tag then "" else '_':tag
364 map (\p -> map (++suffix) (hs_libraries p) ++ extra_libraries p) ps
367 getPackageExtraGhcOpts :: IO [String]
368 getPackageExtraGhcOpts = do
370 return (concatMap extra_ghc_opts ps)
372 getPackageExtraCcOpts :: IO [String]
373 getPackageExtraCcOpts = do
375 return (concatMap extra_cc_opts ps)
377 getPackageExtraLdOpts :: IO [String]
378 getPackageExtraLdOpts = do
380 return (concatMap extra_ld_opts ps)
382 getPackageInfo :: IO [PackageConfig]
384 ps <- readIORef v_Packages
387 getPackageDetails :: [String] -> IO [PackageConfig]
388 getPackageDetails ps = do
389 pkg_details <- readIORef v_Package_details
390 return [ pkg | p <- ps, Just pkg <- [ lookupPkg p pkg_details ] ]
392 GLOBAL_VAR(v_Package_details, (error "package_details"), [PackageConfig])
394 lookupPkg :: String -> [PackageConfig] -> Maybe PackageConfig
396 = case [p | p <- ps, name p == nm] of
400 -----------------------------------------------------------------------------
403 -- The central concept of a "way" is that all objects in a given
404 -- program must be compiled in the same "way". Certain options change
405 -- parameters of the virtual machine, eg. profiling adds an extra word
406 -- to the object header, so profiling objects cannot be linked with
407 -- non-profiling objects.
409 -- After parsing the command-line options, we determine which "way" we
410 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
412 -- We then find the "build-tag" associated with this way, and this
413 -- becomes the suffix used to find .hi files and libraries used in
416 GLOBAL_VAR(v_Build_tag, "", String)
445 GLOBAL_VAR(v_Ways, [] ,[WayName])
447 allowed_combination way = way `elem` combs
448 where -- the sub-lists must be ordered according to WayName,
449 -- because findBuildTag sorts them
450 combs = [ [WayProf,WayUnreg], [WayProf,WaySMP] ]
452 findBuildTag :: IO [String] -- new options
454 way_names <- readIORef v_Ways
455 case sort way_names of
456 [] -> do -- writeIORef v_Build_tag ""
459 [w] -> do let details = lkupWay w
460 writeIORef v_Build_tag (wayTag details)
461 return (wayOpts details)
463 ws -> if not (allowed_combination ws)
464 then throwDyn (CmdLineError $
465 "combination not supported: " ++
466 foldr1 (\a b -> a ++ '/':b)
467 (map (wayName . lkupWay) ws))
468 else let stuff = map lkupWay ws
469 tag = concat (map wayTag stuff)
470 flags = map wayOpts stuff
472 writeIORef v_Build_tag tag
473 return (concat flags)
476 case lookup w way_details of
477 Nothing -> error "findBuildTag"
478 Just details -> details
486 way_details :: [ (WayName, Way) ]
488 [ (WayProf, Way "p" "Profiling"
494 (WayTicky, Way "t" "Ticky-ticky Profiling"
497 , "-optc-DTICKY_TICKY"
500 (WayUnreg, Way "u" "Unregisterised"
503 -- optl's below to tell linker where to find the PVM library -- HWL
504 (WayPar, Way "mp" "Parallel"
506 , "-D__PARALLEL_HASKELL__"
508 , "-package concurrent"
510 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
515 -- at the moment we only change the RTS and could share compiler and libs!
516 (WayPar, Way "mt" "Parallel ticky profiling"
518 , "-D__PARALLEL_HASKELL__"
521 , "-package concurrent"
523 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
528 (WayPar, Way "md" "Distributed"
530 , "-D__PARALLEL_HASKELL__"
531 , "-D__DISTRIBUTED_HASKELL__"
534 , "-package concurrent"
536 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
541 (WayGran, Way "mg" "GranSim"
545 , "-package concurrent"
548 (WaySMP, Way "s" "SMP"
555 (WayUser_a, Way "a" "User way 'a'" ["$WAY_a_REAL_OPTS"]),
556 (WayUser_b, Way "b" "User way 'b'" ["$WAY_b_REAL_OPTS"]),
557 (WayUser_c, Way "c" "User way 'c'" ["$WAY_c_REAL_OPTS"]),
558 (WayUser_d, Way "d" "User way 'd'" ["$WAY_d_REAL_OPTS"]),
559 (WayUser_e, Way "e" "User way 'e'" ["$WAY_e_REAL_OPTS"]),
560 (WayUser_f, Way "f" "User way 'f'" ["$WAY_f_REAL_OPTS"]),
561 (WayUser_g, Way "g" "User way 'g'" ["$WAY_g_REAL_OPTS"]),
562 (WayUser_h, Way "h" "User way 'h'" ["$WAY_h_REAL_OPTS"]),
563 (WayUser_i, Way "i" "User way 'i'" ["$WAY_i_REAL_OPTS"]),
564 (WayUser_j, Way "j" "User way 'j'" ["$WAY_j_REAL_OPTS"]),
565 (WayUser_k, Way "k" "User way 'k'" ["$WAY_k_REAL_OPTS"]),
566 (WayUser_l, Way "l" "User way 'l'" ["$WAY_l_REAL_OPTS"]),
567 (WayUser_m, Way "m" "User way 'm'" ["$WAY_m_REAL_OPTS"]),
568 (WayUser_n, Way "n" "User way 'n'" ["$WAY_n_REAL_OPTS"]),
569 (WayUser_o, Way "o" "User way 'o'" ["$WAY_o_REAL_OPTS"]),
570 (WayUser_A, Way "A" "User way 'A'" ["$WAY_A_REAL_OPTS"]),
571 (WayUser_B, Way "B" "User way 'B'" ["$WAY_B_REAL_OPTS"])
576 , "-optc-DUSE_MINIINTERPRETER"
577 , "-fno-asm-mangling"
581 -----------------------------------------------------------------------------
582 -- Programs for particular phases
584 GLOBAL_VAR(v_Opt_dep, [], [String])
585 GLOBAL_VAR(v_Anti_opt_C, [], [String])
586 GLOBAL_VAR(v_Opt_C, [], [String])
587 GLOBAL_VAR(v_Opt_l, [], [String])
588 GLOBAL_VAR(v_Opt_dll, [], [String])
590 getStaticOpts :: IORef [String] -> IO [String]
591 getStaticOpts ref = readIORef ref >>= return . reverse