1 -----------------------------------------------------------------------------
2 -- $Id: DriverState.hs,v 1.46 2001/06/27 16:38:17 simonmar 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"
15 import Packages ( PackageConfig(..) )
29 -----------------------------------------------------------------------------
30 -- non-configured things
32 cHaskell1Version = "5" -- i.e., Haskell 98
34 -----------------------------------------------------------------------------
35 -- GHC modes of operation
38 = DoMkDependHS -- ghc -M
39 | DoMkDLL -- ghc --mk-dll
40 | StopBefore Phase -- ghc -E | -C | -S | -c
41 | DoMake -- ghc --make
42 | DoInteractive -- ghc --interactive
43 | DoLink -- [ the default ]
46 GLOBAL_VAR(v_GhcMode, error "mode not set", GhcMode)
48 -----------------------------------------------------------------------------
49 -- Global compilation flags
52 v_Hs_source_cpp_opts = global
53 [ "-D__HASKELL1__="++cHaskell1Version
54 , "-D__GLASGOW_HASKELL__="++cProjectVersionInt
56 , "-D__CONCURRENT_HASKELL__"
58 {-# NOINLINE v_Hs_source_cpp_opts #-}
60 -- Keep output from intermediate phases
61 GLOBAL_VAR(v_Keep_hi_diffs, False, Bool)
62 GLOBAL_VAR(v_Keep_hc_files, False, Bool)
63 GLOBAL_VAR(v_Keep_s_files, False, Bool)
64 GLOBAL_VAR(v_Keep_raw_s_files, False, Bool)
65 GLOBAL_VAR(v_Keep_tmp_files, False, Bool)
68 GLOBAL_VAR(v_Scale_sizes_by, 1.0, Double)
69 GLOBAL_VAR(v_Static, True, Bool)
70 GLOBAL_VAR(v_NoHsMain, False, Bool)
71 GLOBAL_VAR(v_Recomp, True, Bool)
72 GLOBAL_VAR(v_Collect_ghc_timing, False, Bool)
73 GLOBAL_VAR(v_Do_asm_mangling, True, Bool)
74 GLOBAL_VAR(v_Excess_precision, False, Bool)
76 -----------------------------------------------------------------------------
77 -- Splitting object files (for libraries)
79 GLOBAL_VAR(v_Split_object_files, False, Bool)
80 GLOBAL_VAR(v_Split_info, ("",0), (String,Int))
81 -- The split prefix and number of files
85 can_split = prefixMatch "i386" cTARGETPLATFORM
86 || prefixMatch "alpha" cTARGETPLATFORM
87 || prefixMatch "hppa" cTARGETPLATFORM
88 || prefixMatch "m68k" cTARGETPLATFORM
89 || prefixMatch "mips" cTARGETPLATFORM
90 || prefixMatch "powerpc" cTARGETPLATFORM
91 || prefixMatch "rs6000" cTARGETPLATFORM
92 || prefixMatch "sparc" cTARGETPLATFORM
94 -----------------------------------------------------------------------------
95 -- Compiler output options
98 | cGhcWithNativeCodeGen == "YES" &&
99 (prefixMatch "i386" cTARGETPLATFORM ||
100 prefixMatch "sparc" cTARGETPLATFORM) = HscAsm
103 GLOBAL_VAR(v_Output_dir, Nothing, Maybe String)
104 GLOBAL_VAR(v_Output_file, Nothing, Maybe String)
105 GLOBAL_VAR(v_Output_hi, Nothing, Maybe String)
107 GLOBAL_VAR(v_Object_suf, Nothing, Maybe String)
108 GLOBAL_VAR(v_HC_suf, Nothing, Maybe String)
109 GLOBAL_VAR(v_Hi_dir, Nothing, Maybe String)
110 GLOBAL_VAR(v_Hi_suf, "hi", String)
112 GLOBAL_VAR(v_Ld_inputs, [], [String])
114 odir_ify :: String -> IO String
116 odir_opt <- readIORef v_Output_dir
119 Just d -> return (newdir d f)
121 osuf_ify :: String -> IO String
123 osuf_opt <- readIORef v_Object_suf
126 Just s -> return (newsuf s f)
128 -----------------------------------------------------------------------------
129 -- Compiler optimisation options
131 GLOBAL_VAR(v_OptLevel, 0, Int)
133 setOptLevel :: String -> IO ()
134 setOptLevel "" = do { writeIORef v_OptLevel 1 }
135 setOptLevel "not" = writeIORef v_OptLevel 0
136 setOptLevel [c] | isDigit c = do
137 let level = ord c - ord '0'
138 writeIORef v_OptLevel level
139 setOptLevel s = unknownFlagErr ("-O"++s)
141 GLOBAL_VAR(v_minus_o2_for_C, False, Bool)
142 GLOBAL_VAR(v_MaxSimplifierIterations, 4, Int)
143 GLOBAL_VAR(v_StgStats, False, Bool)
144 GLOBAL_VAR(v_UsageSPInf, False, Bool) -- Off by default
145 GLOBAL_VAR(v_Strictness, True, Bool)
146 GLOBAL_VAR(v_CPR, True, Bool)
147 GLOBAL_VAR(v_CSE, True, Bool)
149 -- these are the static flags you get without -O.
152 "-fignore-interface-pragmas",
153 "-fomit-interface-pragmas",
154 "-fdo-lambda-eta-expansion", -- This one is important for a tiresome reason:
155 -- we want to make sure that the bindings for data
156 -- constructors are eta-expanded. This is probably
157 -- a good thing anyway, but it seems fragile.
161 -- these are the static flags you get when -O is on.
166 "-fdo-eta-reduction",
167 "-fdo-lambda-eta-expansion",
173 hsc_minusO2_flags = hsc_minusO_flags -- for now
175 getStaticOptimisationFlags 0 = hsc_minusNoO_flags
176 getStaticOptimisationFlags 1 = hsc_minusO_flags
177 getStaticOptimisationFlags n = hsc_minusO2_flags
179 buildCoreToDo :: IO [CoreToDo]
181 opt_level <- readIORef v_OptLevel
182 max_iter <- readIORef v_MaxSimplifierIterations
183 usageSP <- readIORef v_UsageSPInf
184 strictness <- readIORef v_Strictness
185 cpr <- readIORef v_CPR
186 cse <- readIORef v_CSE
188 if opt_level == 0 then return
190 CoreDoSimplify (isAmongSimpl [
191 MaxSimplifierIterations max_iter
195 else {- opt_level >= 1 -} return [
197 -- initial simplify: mk specialiser happy: minimum effort please
198 CoreDoSimplify (isAmongSimpl [
200 -- Don't inline anything till full laziness has bitten
201 -- In particular, inlining wrappers inhibits floating
202 -- e.g. ...(case f x of ...)...
203 -- ==> ...(case (case x of I# x# -> fw x#) of ...)...
204 -- ==> ...(case x of I# x# -> case fw x# of ...)...
205 -- and now the redex (f x) isn't floatable any more
207 -- Similarly, don't apply any rules until after full
208 -- laziness. Notably, list fusion can prevent floating.
210 -- Don't do case-of-case transformations.
211 -- This makes full laziness work better
212 MaxSimplifierIterations max_iter
215 -- Specialisation is best done before full laziness
216 -- so that overloaded functions have all their dictionary lambdas manifest
219 CoreDoFloatOutwards False{-not full-},
222 CoreDoSimplify (isAmongSimpl [
224 -- Want to run with inline phase 1 after the specialiser to give
225 -- maximum chance for fusion to work before we inline build/augment
226 -- in phase 2. This made a difference in 'ansi' where an
227 -- overloaded function wasn't inlined till too late.
228 MaxSimplifierIterations max_iter
231 -- infer usage information here in case we need it later.
232 -- (add more of these where you need them --KSW 1999-04)
233 if usageSP then CoreDoUSPInf else CoreDoNothing,
235 CoreDoSimplify (isAmongSimpl [
236 -- Need inline-phase2 here so that build/augment get
237 -- inlined. I found that spectral/hartel/genfft lost some useful
238 -- strictness in the function sumcode' if augment is not inlined
239 -- before strictness analysis runs
241 MaxSimplifierIterations max_iter
244 CoreDoSimplify (isAmongSimpl [
245 MaxSimplifierIterations 2
246 -- No -finline-phase: allow all Ids to be inlined now
247 -- This gets foldr inlined before strictness analysis
250 if strictness then CoreDoStrictness else CoreDoNothing,
251 if cpr then CoreDoCPResult else CoreDoNothing,
255 CoreDoSimplify (isAmongSimpl [
256 MaxSimplifierIterations max_iter
257 -- No -finline-phase: allow all Ids to be inlined now
260 CoreDoFloatOutwards False{-not full-},
261 -- nofib/spectral/hartel/wang doubles in speed if you
262 -- do full laziness late in the day. It only happens
263 -- after fusion and other stuff, so the early pass doesn't
264 -- catch it. For the record, the redex is
265 -- f_el22 (f_el21 r_midblock)
268 -- Leave out lambda lifting for now
269 -- "-fsimplify", -- Tidy up results of full laziness
271 -- "-fmax-simplifier-iterations2",
273 -- "-ffloat-outwards-full",
275 -- We want CSE to follow the final full-laziness pass, because it may
276 -- succeed in commoning up things floated out by full laziness.
277 -- CSE used to rely on the no-shadowing invariant, but it doesn't any more
279 if cse then CoreCSE else CoreDoNothing,
283 -- Case-liberation for -O2. This should be after
284 -- strictness analysis and the simplification which follows it.
286 if opt_level >= 2 then
290 if opt_level >= 2 then
295 -- Final clean-up simplification:
296 CoreDoSimplify (isAmongSimpl [
297 MaxSimplifierIterations max_iter
298 -- No -finline-phase: allow all Ids to be inlined now
302 buildStgToDo :: IO [ StgToDo ]
304 stg_stats <- readIORef v_StgStats
305 let flags1 | stg_stats = [ D_stg_stats ]
309 ways_ <- readIORef v_Ways
310 let flags2 | WayProf `elem` ways_ = StgDoMassageForProfiling : flags1
315 -----------------------------------------------------------------------------
318 split_marker = ':' -- not configurable (ToDo)
320 v_Import_paths, v_Include_paths, v_Library_paths :: IORef [String]
321 GLOBAL_VAR(v_Import_paths, ["."], [String])
322 GLOBAL_VAR(v_Include_paths, ["."], [String])
323 GLOBAL_VAR(v_Library_paths, [], [String])
325 GLOBAL_VAR(v_Cmdline_libraries, [], [String])
327 addToDirList :: IORef [String] -> String -> IO ()
328 addToDirList ref path
329 = do paths <- readIORef ref
330 writeIORef ref (paths ++ split split_marker path)
332 GLOBAL_VAR(v_HCHeader, "", String)
334 -----------------------------------------------------------------------------
337 -- package list is maintained in dependency order
338 GLOBAL_VAR(v_Packages, ("std":"rts":"gmp":[]), [String])
340 addPackage :: String -> IO ()
342 = do pkg_details <- readIORef v_Package_details
343 case lookupPkg package pkg_details of
344 Nothing -> throwDyn (CmdLineError ("unknown package name: " ++ package))
346 ps <- readIORef v_Packages
347 unless (package `elem` ps) $ do
348 mapM_ addPackage (package_deps details)
349 ps <- readIORef v_Packages
350 writeIORef v_Packages (package:ps)
352 getPackageImportPath :: IO [String]
353 getPackageImportPath = do
355 return (nub (filter (not.null) (concatMap import_dirs ps)))
357 getPackageIncludePath :: IO [String]
358 getPackageIncludePath = do
360 return (nub (filter (not.null) (concatMap include_dirs ps)))
362 -- includes are in reverse dependency order (i.e. rts first)
363 getPackageCIncludes :: IO [String]
364 getPackageCIncludes = do
366 return (reverse (nub (filter (not.null) (concatMap c_includes ps))))
368 getPackageLibraryPath :: IO [String]
369 getPackageLibraryPath = do
371 return (nub (filter (not.null) (concatMap library_dirs ps)))
373 getPackageLibraries :: IO [String]
374 getPackageLibraries = do
376 tag <- readIORef v_Build_tag
377 let suffix = if null tag then "" else '_':tag
379 map (\p -> map (++suffix) (hs_libraries p) ++ extra_libraries p) ps
382 getPackageExtraGhcOpts :: IO [String]
383 getPackageExtraGhcOpts = do
385 return (concatMap extra_ghc_opts ps)
387 getPackageExtraCcOpts :: IO [String]
388 getPackageExtraCcOpts = do
390 return (concatMap extra_cc_opts ps)
392 getPackageExtraLdOpts :: IO [String]
393 getPackageExtraLdOpts = do
395 return (concatMap extra_ld_opts ps)
397 getPackageInfo :: IO [PackageConfig]
399 ps <- readIORef v_Packages
402 getPackageDetails :: [String] -> IO [PackageConfig]
403 getPackageDetails ps = do
404 pkg_details <- readIORef v_Package_details
405 return [ pkg | p <- ps, Just pkg <- [ lookupPkg p pkg_details ] ]
407 GLOBAL_VAR(v_Package_details, (error "package_details"), [PackageConfig])
409 lookupPkg :: String -> [PackageConfig] -> Maybe PackageConfig
411 = case [p | p <- ps, name p == nm] of
415 -----------------------------------------------------------------------------
418 -- The central concept of a "way" is that all objects in a given
419 -- program must be compiled in the same "way". Certain options change
420 -- parameters of the virtual machine, eg. profiling adds an extra word
421 -- to the object header, so profiling objects cannot be linked with
422 -- non-profiling objects.
424 -- After parsing the command-line options, we determine which "way" we
425 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
427 -- We then find the "build-tag" associated with this way, and this
428 -- becomes the suffix used to find .hi files and libraries used in
431 GLOBAL_VAR(v_Build_tag, "", String)
460 GLOBAL_VAR(v_Ways, [] ,[WayName])
462 allowed_combination way = way `elem` combs
463 where -- the sub-lists must be ordered according to WayName,
464 -- because findBuildTag sorts them
465 combs = [ [WayProf,WayUnreg], [WayProf,WaySMP] ]
467 findBuildTag :: IO [String] -- new options
469 way_names <- readIORef v_Ways
470 case sort way_names of
471 [] -> do -- writeIORef v_Build_tag ""
474 [w] -> do let details = lkupWay w
475 writeIORef v_Build_tag (wayTag details)
476 return (wayOpts details)
478 ws -> if not (allowed_combination ws)
479 then throwDyn (CmdLineError $
480 "combination not supported: " ++
481 foldr1 (\a b -> a ++ '/':b)
482 (map (wayName . lkupWay) ws))
483 else let stuff = map lkupWay ws
484 tag = concat (map wayTag stuff)
485 flags = map wayOpts stuff
487 writeIORef v_Build_tag tag
488 return (concat flags)
491 case lookup w way_details of
492 Nothing -> error "findBuildTag"
493 Just details -> details
501 way_details :: [ (WayName, Way) ]
503 [ (WayProf, Way "p" "Profiling"
509 (WayTicky, Way "t" "Ticky-ticky Profiling"
512 , "-optc-DTICKY_TICKY"
515 (WayUnreg, Way "u" "Unregisterised"
518 -- optl's below to tell linker where to find the PVM library -- HWL
519 (WayPar, Way "mp" "Parallel"
521 , "-D__PARALLEL_HASKELL__"
523 , "-package concurrent"
525 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
530 -- at the moment we only change the RTS and could share compiler and libs!
531 (WayPar, Way "mt" "Parallel ticky profiling"
533 , "-D__PARALLEL_HASKELL__"
536 , "-package concurrent"
538 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
543 (WayPar, Way "md" "Distributed"
545 , "-D__PARALLEL_HASKELL__"
546 , "-D__DISTRIBUTED_HASKELL__"
549 , "-package concurrent"
551 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
556 (WayGran, Way "mg" "GranSim"
560 , "-package concurrent"
563 (WaySMP, Way "s" "SMP"
570 (WayUser_a, Way "a" "User way 'a'" ["$WAY_a_REAL_OPTS"]),
571 (WayUser_b, Way "b" "User way 'b'" ["$WAY_b_REAL_OPTS"]),
572 (WayUser_c, Way "c" "User way 'c'" ["$WAY_c_REAL_OPTS"]),
573 (WayUser_d, Way "d" "User way 'd'" ["$WAY_d_REAL_OPTS"]),
574 (WayUser_e, Way "e" "User way 'e'" ["$WAY_e_REAL_OPTS"]),
575 (WayUser_f, Way "f" "User way 'f'" ["$WAY_f_REAL_OPTS"]),
576 (WayUser_g, Way "g" "User way 'g'" ["$WAY_g_REAL_OPTS"]),
577 (WayUser_h, Way "h" "User way 'h'" ["$WAY_h_REAL_OPTS"]),
578 (WayUser_i, Way "i" "User way 'i'" ["$WAY_i_REAL_OPTS"]),
579 (WayUser_j, Way "j" "User way 'j'" ["$WAY_j_REAL_OPTS"]),
580 (WayUser_k, Way "k" "User way 'k'" ["$WAY_k_REAL_OPTS"]),
581 (WayUser_l, Way "l" "User way 'l'" ["$WAY_l_REAL_OPTS"]),
582 (WayUser_m, Way "m" "User way 'm'" ["$WAY_m_REAL_OPTS"]),
583 (WayUser_n, Way "n" "User way 'n'" ["$WAY_n_REAL_OPTS"]),
584 (WayUser_o, Way "o" "User way 'o'" ["$WAY_o_REAL_OPTS"]),
585 (WayUser_A, Way "A" "User way 'A'" ["$WAY_A_REAL_OPTS"]),
586 (WayUser_B, Way "B" "User way 'B'" ["$WAY_B_REAL_OPTS"])
591 , "-optc-DUSE_MINIINTERPRETER"
592 , "-fno-asm-mangling"
596 -----------------------------------------------------------------------------
597 -- Programs for particular phases
599 GLOBAL_VAR(v_Opt_dep, [], [String])
600 GLOBAL_VAR(v_Anti_opt_C, [], [String])
601 GLOBAL_VAR(v_Opt_C, [], [String])
602 GLOBAL_VAR(v_Opt_l, [], [String])
603 GLOBAL_VAR(v_Opt_dll, [], [String])
605 getStaticOpts :: IORef [String] -> IO [String]
606 getStaticOpts ref = readIORef ref >>= return . reverse