1 -----------------------------------------------------------------------------
2 -- $Id: DriverState.hs,v 1.48 2001/07/09 17:44:08 sof 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(..) )
28 import Directory ( doesDirectoryExist )
30 -----------------------------------------------------------------------------
31 -- non-configured things
33 cHaskell1Version = "5" -- i.e., Haskell 98
35 -----------------------------------------------------------------------------
36 -- GHC modes of operation
39 = DoMkDependHS -- ghc -M
40 | DoMkDLL -- ghc --mk-dll
41 | StopBefore Phase -- ghc -E | -C | -S | -c
42 | DoMake -- ghc --make
43 | DoInteractive -- ghc --interactive
44 | DoLink -- [ the default ]
47 GLOBAL_VAR(v_GhcMode, error "mode not set", GhcMode)
49 -----------------------------------------------------------------------------
50 -- Global compilation flags
53 v_Hs_source_cpp_opts = global
54 [ "-D__HASKELL1__="++cHaskell1Version
55 , "-D__GLASGOW_HASKELL__="++cProjectVersionInt
57 , "-D__CONCURRENT_HASKELL__"
59 {-# NOINLINE v_Hs_source_cpp_opts #-}
61 -- Keep output from intermediate phases
62 GLOBAL_VAR(v_Keep_hi_diffs, False, Bool)
63 GLOBAL_VAR(v_Keep_hc_files, False, Bool)
64 GLOBAL_VAR(v_Keep_s_files, False, Bool)
65 GLOBAL_VAR(v_Keep_raw_s_files, False, Bool)
66 GLOBAL_VAR(v_Keep_tmp_files, False, Bool)
69 GLOBAL_VAR(v_Scale_sizes_by, 1.0, Double)
70 GLOBAL_VAR(v_Static, True, Bool)
71 GLOBAL_VAR(v_NoHsMain, False, Bool)
72 GLOBAL_VAR(v_Recomp, True, Bool)
73 GLOBAL_VAR(v_Collect_ghc_timing, False, Bool)
74 GLOBAL_VAR(v_Do_asm_mangling, True, Bool)
75 GLOBAL_VAR(v_Excess_precision, False, Bool)
77 -----------------------------------------------------------------------------
78 -- Splitting object files (for libraries)
80 GLOBAL_VAR(v_Split_object_files, False, Bool)
81 GLOBAL_VAR(v_Split_info, ("",0), (String,Int))
82 -- The split prefix and number of files
86 can_split = prefixMatch "i386" cTARGETPLATFORM
87 || prefixMatch "alpha" cTARGETPLATFORM
88 || prefixMatch "hppa" cTARGETPLATFORM
89 || prefixMatch "m68k" cTARGETPLATFORM
90 || prefixMatch "mips" cTARGETPLATFORM
91 || prefixMatch "powerpc" cTARGETPLATFORM
92 || prefixMatch "rs6000" cTARGETPLATFORM
93 || prefixMatch "sparc" cTARGETPLATFORM
95 -----------------------------------------------------------------------------
96 -- Compiler output options
99 | cGhcWithNativeCodeGen == "YES" &&
100 (prefixMatch "i386" cTARGETPLATFORM ||
101 prefixMatch "sparc" cTARGETPLATFORM) = HscAsm
104 GLOBAL_VAR(v_Output_dir, Nothing, Maybe String)
105 GLOBAL_VAR(v_Output_file, Nothing, Maybe String)
106 GLOBAL_VAR(v_Output_hi, Nothing, Maybe String)
108 GLOBAL_VAR(v_Object_suf, Nothing, Maybe String)
109 GLOBAL_VAR(v_HC_suf, Nothing, Maybe String)
110 GLOBAL_VAR(v_Hi_dir, Nothing, Maybe String)
111 GLOBAL_VAR(v_Hi_suf, "hi", String)
113 GLOBAL_VAR(v_Ld_inputs, [], [String])
115 odir_ify :: String -> IO String
117 odir_opt <- readIORef v_Output_dir
120 Just d -> return (newdir d f)
122 osuf_ify :: String -> IO String
124 osuf_opt <- readIORef v_Object_suf
127 Just s -> return (newsuf s f)
129 -----------------------------------------------------------------------------
130 -- Compiler optimisation options
132 GLOBAL_VAR(v_OptLevel, 0, Int)
134 setOptLevel :: String -> IO ()
135 setOptLevel "" = do { writeIORef v_OptLevel 1 }
136 setOptLevel "not" = writeIORef v_OptLevel 0
137 setOptLevel [c] | isDigit c = do
138 let level = ord c - ord '0'
139 writeIORef v_OptLevel level
140 setOptLevel s = unknownFlagErr ("-O"++s)
142 GLOBAL_VAR(v_minus_o2_for_C, False, Bool)
143 GLOBAL_VAR(v_MaxSimplifierIterations, 4, Int)
144 GLOBAL_VAR(v_StgStats, False, Bool)
145 GLOBAL_VAR(v_UsageSPInf, False, Bool) -- Off by default
146 GLOBAL_VAR(v_Strictness, True, Bool)
147 GLOBAL_VAR(v_CPR, True, Bool)
148 GLOBAL_VAR(v_CSE, True, Bool)
150 -- these are the static flags you get without -O.
153 "-fignore-interface-pragmas",
154 "-fomit-interface-pragmas",
155 "-fdo-lambda-eta-expansion", -- This one is important for a tiresome reason:
156 -- we want to make sure that the bindings for data
157 -- constructors are eta-expanded. This is probably
158 -- a good thing anyway, but it seems fragile.
162 -- these are the static flags you get when -O is on.
167 "-fdo-eta-reduction",
168 "-fdo-lambda-eta-expansion",
174 hsc_minusO2_flags = hsc_minusO_flags -- for now
176 getStaticOptimisationFlags 0 = hsc_minusNoO_flags
177 getStaticOptimisationFlags 1 = hsc_minusO_flags
178 getStaticOptimisationFlags n = hsc_minusO2_flags
180 buildCoreToDo :: IO [CoreToDo]
182 opt_level <- readIORef v_OptLevel
183 max_iter <- readIORef v_MaxSimplifierIterations
184 usageSP <- readIORef v_UsageSPInf
185 strictness <- readIORef v_Strictness
186 cpr <- readIORef v_CPR
187 cse <- readIORef v_CSE
189 if opt_level == 0 then return
191 CoreDoSimplify (isAmongSimpl [
192 MaxSimplifierIterations max_iter
196 else {- opt_level >= 1 -} return [
198 -- initial simplify: mk specialiser happy: minimum effort please
199 CoreDoSimplify (isAmongSimpl [
201 -- Don't inline anything till full laziness has bitten
202 -- In particular, inlining wrappers inhibits floating
203 -- e.g. ...(case f x of ...)...
204 -- ==> ...(case (case x of I# x# -> fw x#) of ...)...
205 -- ==> ...(case x of I# x# -> case fw x# of ...)...
206 -- and now the redex (f x) isn't floatable any more
208 -- Similarly, don't apply any rules until after full
209 -- laziness. Notably, list fusion can prevent floating.
211 -- Don't do case-of-case transformations.
212 -- This makes full laziness work better
213 MaxSimplifierIterations max_iter
216 -- Specialisation is best done before full laziness
217 -- so that overloaded functions have all their dictionary lambdas manifest
220 CoreDoFloatOutwards False{-not full-},
223 CoreDoSimplify (isAmongSimpl [
225 -- Want to run with inline phase 1 after the specialiser to give
226 -- maximum chance for fusion to work before we inline build/augment
227 -- in phase 2. This made a difference in 'ansi' where an
228 -- overloaded function wasn't inlined till too late.
229 MaxSimplifierIterations max_iter
232 -- infer usage information here in case we need it later.
233 -- (add more of these where you need them --KSW 1999-04)
234 if usageSP then CoreDoUSPInf else CoreDoNothing,
236 CoreDoSimplify (isAmongSimpl [
237 -- Need inline-phase2 here so that build/augment get
238 -- inlined. I found that spectral/hartel/genfft lost some useful
239 -- strictness in the function sumcode' if augment is not inlined
240 -- before strictness analysis runs
242 MaxSimplifierIterations max_iter
245 CoreDoSimplify (isAmongSimpl [
246 MaxSimplifierIterations 2
247 -- No -finline-phase: allow all Ids to be inlined now
248 -- This gets foldr inlined before strictness analysis
251 if strictness then CoreDoStrictness else CoreDoNothing,
252 if cpr then CoreDoCPResult else CoreDoNothing,
256 CoreDoSimplify (isAmongSimpl [
257 MaxSimplifierIterations max_iter
258 -- No -finline-phase: allow all Ids to be inlined now
261 CoreDoFloatOutwards False{-not full-},
262 -- nofib/spectral/hartel/wang doubles in speed if you
263 -- do full laziness late in the day. It only happens
264 -- after fusion and other stuff, so the early pass doesn't
265 -- catch it. For the record, the redex is
266 -- f_el22 (f_el21 r_midblock)
269 -- Leave out lambda lifting for now
270 -- "-fsimplify", -- Tidy up results of full laziness
272 -- "-fmax-simplifier-iterations2",
274 -- "-ffloat-outwards-full",
276 -- We want CSE to follow the final full-laziness pass, because it may
277 -- succeed in commoning up things floated out by full laziness.
278 -- CSE used to rely on the no-shadowing invariant, but it doesn't any more
280 if cse then CoreCSE else CoreDoNothing,
284 -- Case-liberation for -O2. This should be after
285 -- strictness analysis and the simplification which follows it.
287 if opt_level >= 2 then
291 if opt_level >= 2 then
296 -- Final clean-up simplification:
297 CoreDoSimplify (isAmongSimpl [
298 MaxSimplifierIterations max_iter
299 -- No -finline-phase: allow all Ids to be inlined now
303 buildStgToDo :: IO [ StgToDo ]
305 stg_stats <- readIORef v_StgStats
306 let flags1 | stg_stats = [ D_stg_stats ]
310 ways_ <- readIORef v_Ways
311 let flags2 | WayProf `elem` ways_ = StgDoMassageForProfiling : flags1
316 -----------------------------------------------------------------------------
319 split_marker = ':' -- not configurable (ToDo)
321 v_Import_paths, v_Include_paths, v_Library_paths :: IORef [String]
322 GLOBAL_VAR(v_Import_paths, ["."], [String])
323 GLOBAL_VAR(v_Include_paths, ["."], [String])
324 GLOBAL_VAR(v_Library_paths, [], [String])
326 GLOBAL_VAR(v_Cmdline_libraries, [], [String])
328 addToDirList :: IORef [String] -> String -> IO ()
329 addToDirList ref path
330 = do paths <- readIORef ref
331 shiny_new_ones <- splitUp path
332 writeIORef ref (paths ++ shiny_new_ones)
335 splitUp ::String -> IO [String]
336 #ifdef mingw32_TARGET_OS
337 -- 'hybrid' support for DOS-style paths in directory lists.
339 -- That is, if "foo:bar:baz" is used, this interpreted as
340 -- consisting of three entries, 'foo', 'bar', 'baz'.
341 -- However, with "c:/foo:c:\\foo;x:/bar", this is interpreted
342 -- as four elts, "c:/foo", "c:\\foo", "x", and "/bar" --
343 -- *provided* c:/foo exists and x:/bar doesn't.
345 -- Notice that no attempt is made to fully replace the 'standard'
346 -- split marker ':' with the Windows / DOS one, ';'. The reason being
347 -- that this will cause too much breakage for users & ':' will
348 -- work fine even with DOS paths, if you're not insisting on being silly.
350 splitUp [] = return []
351 splitUp (x:':':div:xs)
352 | div `elem` dir_markers = do
353 let (p,rs) = findNextPath xs
356 Consult the file system to check the interpretation
357 of (x:':':div:p) -- this is arguably excessive, we
358 could skip this test & just say that it is a valid
361 flg <- doesDirectoryExist (x:':':div:p)
363 return ((x:':':div:p):ps)
365 return ([x]:(div:p):ps)
367 let (p,rs) = findNextPath xs
374 -- will be called either when we've consumed nought or the "<Drive>:/" part of
375 -- a DOS path, so splitting is just a Q of finding the next split marker.
377 case break (`elem` split_markers) xs of
381 split_markers :: [Char]
382 split_markers = [':', ';']
384 dir_markers :: [Char]
385 dir_markers = ['/', '\\']
388 splitUp xs = return (split split_marker xs)
391 GLOBAL_VAR(v_HCHeader, "", String)
393 -----------------------------------------------------------------------------
396 -- package list is maintained in dependency order
397 GLOBAL_VAR(v_Packages, ("std":"rts":"gmp":[]), [String])
399 addPackage :: String -> IO ()
401 = do pkg_details <- readIORef v_Package_details
402 case lookupPkg package pkg_details of
403 Nothing -> throwDyn (CmdLineError ("unknown package name: " ++ package))
405 ps <- readIORef v_Packages
406 unless (package `elem` ps) $ do
407 mapM_ addPackage (package_deps details)
408 ps <- readIORef v_Packages
409 writeIORef v_Packages (package:ps)
411 getPackageImportPath :: IO [String]
412 getPackageImportPath = do
414 return (nub (filter (not.null) (concatMap import_dirs ps)))
416 getPackageIncludePath :: IO [String]
417 getPackageIncludePath = do
419 return (nub (filter (not.null) (concatMap include_dirs ps)))
421 -- includes are in reverse dependency order (i.e. rts first)
422 getPackageCIncludes :: IO [String]
423 getPackageCIncludes = do
425 return (reverse (nub (filter (not.null) (concatMap c_includes ps))))
427 getPackageLibraryPath :: IO [String]
428 getPackageLibraryPath = do
430 return (nub (filter (not.null) (concatMap library_dirs ps)))
432 getPackageLibraries :: IO [String]
433 getPackageLibraries = do
435 tag <- readIORef v_Build_tag
436 let suffix = if null tag then "" else '_':tag
438 map (\p -> map (++suffix) (hACK (hs_libraries p)) ++ extra_libraries p) ps
441 -- This is a totally horrible (temporary) hack, for Win32. Problem is
442 -- that package.conf for Win32 says that the main prelude lib is
443 -- split into HSstd1 and HSstd2, which is needed due to limitations in
444 -- the PEi386 file format, to make GHCi work. However, we still only
445 -- have HSstd.a for static linking, not HSstd1.a and HSstd2.a.
446 -- getPackageLibraries is called to find the .a's to add to the static
447 -- link line. On Win32, this hACK detects HSstd1 and HSstd2 and
448 -- replaces them with HSstd, so static linking still works.
449 -- Libraries needed for dynamic (GHCi) linking are discovered via
450 -- different route (in InteractiveUI.linkPackage).
451 -- See driver/PackageSrc.hs for the HSstd1/HSstd2 split definition.
452 -- THIS IS A STRICTLY TEMPORARY HACK (famous last words ...)
454 # ifndef mingw32_TARGET_OS
457 = if "HSstd1" `elem` libs && "HSstd2" `elem` libs
458 then "HSstd" : filter ((/= "HSstd").(take 5)) libs
462 getPackageExtraGhcOpts :: IO [String]
463 getPackageExtraGhcOpts = do
465 return (concatMap extra_ghc_opts ps)
467 getPackageExtraCcOpts :: IO [String]
468 getPackageExtraCcOpts = do
470 return (concatMap extra_cc_opts ps)
472 getPackageExtraLdOpts :: IO [String]
473 getPackageExtraLdOpts = do
475 return (concatMap extra_ld_opts ps)
477 getPackageInfo :: IO [PackageConfig]
479 ps <- readIORef v_Packages
482 getPackageDetails :: [String] -> IO [PackageConfig]
483 getPackageDetails ps = do
484 pkg_details <- readIORef v_Package_details
485 return [ pkg | p <- ps, Just pkg <- [ lookupPkg p pkg_details ] ]
487 GLOBAL_VAR(v_Package_details, (error "package_details"), [PackageConfig])
489 lookupPkg :: String -> [PackageConfig] -> Maybe PackageConfig
491 = case [p | p <- ps, name p == nm] of
495 -----------------------------------------------------------------------------
498 -- The central concept of a "way" is that all objects in a given
499 -- program must be compiled in the same "way". Certain options change
500 -- parameters of the virtual machine, eg. profiling adds an extra word
501 -- to the object header, so profiling objects cannot be linked with
502 -- non-profiling objects.
504 -- After parsing the command-line options, we determine which "way" we
505 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
507 -- We then find the "build-tag" associated with this way, and this
508 -- becomes the suffix used to find .hi files and libraries used in
511 GLOBAL_VAR(v_Build_tag, "", String)
540 GLOBAL_VAR(v_Ways, [] ,[WayName])
542 allowed_combination way = way `elem` combs
543 where -- the sub-lists must be ordered according to WayName,
544 -- because findBuildTag sorts them
545 combs = [ [WayProf,WayUnreg], [WayProf,WaySMP] ]
547 findBuildTag :: IO [String] -- new options
549 way_names <- readIORef v_Ways
550 case sort way_names of
551 [] -> do -- writeIORef v_Build_tag ""
554 [w] -> do let details = lkupWay w
555 writeIORef v_Build_tag (wayTag details)
556 return (wayOpts details)
558 ws -> if not (allowed_combination ws)
559 then throwDyn (CmdLineError $
560 "combination not supported: " ++
561 foldr1 (\a b -> a ++ '/':b)
562 (map (wayName . lkupWay) ws))
563 else let stuff = map lkupWay ws
564 tag = concat (map wayTag stuff)
565 flags = map wayOpts stuff
567 writeIORef v_Build_tag tag
568 return (concat flags)
571 case lookup w way_details of
572 Nothing -> error "findBuildTag"
573 Just details -> details
581 way_details :: [ (WayName, Way) ]
583 [ (WayProf, Way "p" "Profiling"
589 (WayTicky, Way "t" "Ticky-ticky Profiling"
592 , "-optc-DTICKY_TICKY"
595 (WayUnreg, Way "u" "Unregisterised"
598 -- optl's below to tell linker where to find the PVM library -- HWL
599 (WayPar, Way "mp" "Parallel"
601 , "-D__PARALLEL_HASKELL__"
603 , "-package concurrent"
605 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
610 -- at the moment we only change the RTS and could share compiler and libs!
611 (WayPar, Way "mt" "Parallel ticky profiling"
613 , "-D__PARALLEL_HASKELL__"
616 , "-package concurrent"
618 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
623 (WayPar, Way "md" "Distributed"
625 , "-D__PARALLEL_HASKELL__"
626 , "-D__DISTRIBUTED_HASKELL__"
629 , "-package concurrent"
631 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
636 (WayGran, Way "mg" "GranSim"
640 , "-package concurrent"
643 (WaySMP, Way "s" "SMP"
650 (WayUser_a, Way "a" "User way 'a'" ["$WAY_a_REAL_OPTS"]),
651 (WayUser_b, Way "b" "User way 'b'" ["$WAY_b_REAL_OPTS"]),
652 (WayUser_c, Way "c" "User way 'c'" ["$WAY_c_REAL_OPTS"]),
653 (WayUser_d, Way "d" "User way 'd'" ["$WAY_d_REAL_OPTS"]),
654 (WayUser_e, Way "e" "User way 'e'" ["$WAY_e_REAL_OPTS"]),
655 (WayUser_f, Way "f" "User way 'f'" ["$WAY_f_REAL_OPTS"]),
656 (WayUser_g, Way "g" "User way 'g'" ["$WAY_g_REAL_OPTS"]),
657 (WayUser_h, Way "h" "User way 'h'" ["$WAY_h_REAL_OPTS"]),
658 (WayUser_i, Way "i" "User way 'i'" ["$WAY_i_REAL_OPTS"]),
659 (WayUser_j, Way "j" "User way 'j'" ["$WAY_j_REAL_OPTS"]),
660 (WayUser_k, Way "k" "User way 'k'" ["$WAY_k_REAL_OPTS"]),
661 (WayUser_l, Way "l" "User way 'l'" ["$WAY_l_REAL_OPTS"]),
662 (WayUser_m, Way "m" "User way 'm'" ["$WAY_m_REAL_OPTS"]),
663 (WayUser_n, Way "n" "User way 'n'" ["$WAY_n_REAL_OPTS"]),
664 (WayUser_o, Way "o" "User way 'o'" ["$WAY_o_REAL_OPTS"]),
665 (WayUser_A, Way "A" "User way 'A'" ["$WAY_A_REAL_OPTS"]),
666 (WayUser_B, Way "B" "User way 'B'" ["$WAY_B_REAL_OPTS"])
671 , "-optc-DUSE_MINIINTERPRETER"
672 , "-fno-asm-mangling"
676 -----------------------------------------------------------------------------
677 -- Programs for particular phases
679 GLOBAL_VAR(v_Opt_dep, [], [String])
680 GLOBAL_VAR(v_Anti_opt_C, [], [String])
681 GLOBAL_VAR(v_Opt_C, [], [String])
682 GLOBAL_VAR(v_Opt_l, [], [String])
683 GLOBAL_VAR(v_Opt_dll, [], [String])
685 getStaticOpts :: IORef [String] -> IO [String]
686 getStaticOpts ref = readIORef ref >>= return . reverse