1 -----------------------------------------------------------------------------
2 -- $Id: DriverState.hs,v 1.24 2001/01/16 12:41:03 simonmar Exp $
4 -- Settings for the driver
6 -- (c) The University of Glasgow 2000
8 -----------------------------------------------------------------------------
10 module DriverState where
12 #include "HsVersions.h"
21 #ifdef mingw32_TARGET_OS
22 import TmpFiles ( newTempName )
23 import Directory ( removeFile )
31 -----------------------------------------------------------------------------
34 -- certain flags can be specified on a per-file basis, in an OPTIONS
35 -- pragma at the beginning of the source file. This means that when
36 -- compiling mulitple files, we have to restore the global option
37 -- settings before compiling a new file.
39 -- The DriverState record contains the per-file-mutable state.
41 data DriverState = DriverState {
43 -- are we runing cpp on this file?
47 stolen_x86_regs :: Int,
48 cmdline_hc_includes :: [String],
50 -- options for a particular phase
58 initDriverState = DriverState {
61 cmdline_hc_includes = [],
69 -- The driver state is first initialized from the command line options,
70 -- and then reset to this initial state before each compilation.
71 -- v_InitDriverState contains the saved initial state, and v_DriverState
72 -- contains the current state (modified by any OPTIONS pragmas, for example).
74 -- v_InitDriverState may also be modified from the GHCi prompt, using :set.
76 GLOBAL_VAR(v_InitDriverState, initDriverState, DriverState)
77 GLOBAL_VAR(v_Driver_state, initDriverState, DriverState)
79 readState :: (DriverState -> a) -> IO a
80 readState f = readIORef v_Driver_state >>= return . f
82 updateState :: (DriverState -> DriverState) -> IO ()
83 updateState f = readIORef v_Driver_state >>= writeIORef v_Driver_state . f
85 addOpt_L a = updateState (\s -> s{opt_L = a : opt_L s})
86 addOpt_P a = updateState (\s -> s{opt_P = a : opt_P s})
87 addOpt_c a = updateState (\s -> s{opt_c = a : opt_c s})
88 addOpt_a a = updateState (\s -> s{opt_a = a : opt_a s})
89 addOpt_m a = updateState (\s -> s{opt_m = a : opt_m s})
91 addCmdlineHCInclude a =
92 updateState (\s -> s{cmdline_hc_includes = a : cmdline_hc_includes s})
94 -- we add to the options from the front, so we need to reverse the list
95 getOpts :: (DriverState -> [a]) -> IO [a]
96 getOpts opts = readState opts >>= return . reverse
98 -----------------------------------------------------------------------------
99 -- non-configured things
101 cHaskell1Version = "5" -- i.e., Haskell 98
103 -----------------------------------------------------------------------------
104 -- Global compilation flags
106 -- location of compiler-related files
107 GLOBAL_VAR(v_TopDir, clibdir, String)
110 v_Hs_source_cpp_opts = global
111 [ "-D__HASKELL1__="++cHaskell1Version
112 , "-D__GLASGOW_HASKELL__="++cProjectVersionInt
114 , "-D__CONCURRENT_HASKELL__"
116 {-# NOINLINE v_Hs_source_cpp_opts #-}
118 -- Keep output from intermediate phases
119 GLOBAL_VAR(v_Keep_hi_diffs, False, Bool)
120 GLOBAL_VAR(v_Keep_hc_files, False, Bool)
121 GLOBAL_VAR(v_Keep_s_files, False, Bool)
122 GLOBAL_VAR(v_Keep_raw_s_files, False, Bool)
123 GLOBAL_VAR(v_Keep_tmp_files, False, Bool)
126 GLOBAL_VAR(v_Scale_sizes_by, 1.0, Double)
127 GLOBAL_VAR(v_Dry_run, False, Bool)
128 GLOBAL_VAR(v_Static, True, Bool)
129 GLOBAL_VAR(v_NoHsMain, False, Bool)
130 GLOBAL_VAR(v_Recomp, True, Bool)
131 GLOBAL_VAR(v_Collect_ghc_timing, False, Bool)
132 GLOBAL_VAR(v_Do_asm_mangling, True, Bool)
133 GLOBAL_VAR(v_Excess_precision, False, Bool)
135 -----------------------------------------------------------------------------
136 -- Splitting object files (for libraries)
138 GLOBAL_VAR(v_Split_object_files, False, Bool)
139 GLOBAL_VAR(v_Split_prefix, "", String)
140 GLOBAL_VAR(v_N_split_files, 0, Int)
143 can_split = prefixMatch "i386" cTARGETPLATFORM
144 || prefixMatch "alpha" cTARGETPLATFORM
145 || prefixMatch "hppa" cTARGETPLATFORM
146 || prefixMatch "m68k" cTARGETPLATFORM
147 || prefixMatch "mips" cTARGETPLATFORM
148 || prefixMatch "powerpc" cTARGETPLATFORM
149 || prefixMatch "rs6000" cTARGETPLATFORM
150 || prefixMatch "sparc" cTARGETPLATFORM
152 -----------------------------------------------------------------------------
153 -- Compiler output options
156 | cGhcWithNativeCodeGen == "YES" &&
157 (prefixMatch "i386" cTARGETPLATFORM ||
158 prefixMatch "sparc" cTARGETPLATFORM) = HscAsm
161 GLOBAL_VAR(v_Output_dir, Nothing, Maybe String)
162 GLOBAL_VAR(v_Object_suf, Nothing, Maybe String)
163 GLOBAL_VAR(v_Output_file, Nothing, Maybe String)
164 GLOBAL_VAR(v_Output_hi, Nothing, Maybe String)
166 GLOBAL_VAR(v_Ld_inputs, [], [String])
168 odir_ify :: String -> IO String
170 odir_opt <- readIORef v_Output_dir
173 Just d -> return (newdir d f)
175 osuf_ify :: String -> IO String
177 osuf_opt <- readIORef v_Object_suf
180 Just s -> return (newsuf s f)
182 -----------------------------------------------------------------------------
185 GLOBAL_VAR(v_ProduceHi, True, Bool)
186 GLOBAL_VAR(v_Hi_on_stdout, False, Bool)
187 GLOBAL_VAR(v_Hi_suf, "hi", String)
189 -----------------------------------------------------------------------------
190 -- Warnings & sanity checking
192 -- Warning packages that are controlled by -W and -Wall. The 'standard'
193 -- warnings that you get all the time are
195 -- -fwarn-overlapping-patterns
196 -- -fwarn-missing-methods
197 -- -fwarn-missing-fields
198 -- -fwarn-deprecations
199 -- -fwarn-duplicate-exports
201 -- these are turned off by -Wnot.
204 standardWarnings = [ "-fwarn-overlapping-patterns"
205 , "-fwarn-missing-methods"
206 , "-fwarn-missing-fields"
207 , "-fwarn-deprecations"
208 , "-fwarn-duplicate-exports"
210 minusWOpts = standardWarnings ++
211 [ "-fwarn-unused-binds"
212 , "-fwarn-unused-matches"
213 , "-fwarn-incomplete-patterns"
214 , "-fwarn-unused-imports"
216 minusWallOpts = minusWOpts ++
217 [ "-fwarn-type-defaults"
218 , "-fwarn-name-shadowing"
219 , "-fwarn-missing-signatures"
220 , "-fwarn-hi-shadowing"
223 data WarningState = W_default | W_ | W_all | W_not
224 GLOBAL_VAR(v_Warning_opt, W_default, WarningState)
226 -----------------------------------------------------------------------------
227 -- Compiler optimisation options
229 GLOBAL_VAR(v_OptLevel, 0, Int)
231 setOptLevel :: String -> IO ()
232 setOptLevel "" = do { writeIORef v_OptLevel 1 }
233 setOptLevel "not" = writeIORef v_OptLevel 0
234 setOptLevel [c] | isDigit c = do
235 let level = ord c - ord '0'
236 writeIORef v_OptLevel level
237 setOptLevel s = unknownFlagErr ("-O"++s)
239 GLOBAL_VAR(v_minus_o2_for_C, False, Bool)
240 GLOBAL_VAR(v_MaxSimplifierIterations, 4, Int)
241 GLOBAL_VAR(v_StgStats, False, Bool)
242 GLOBAL_VAR(v_UsageSPInf, False, Bool) -- Off by default
243 GLOBAL_VAR(v_Strictness, True, Bool)
244 GLOBAL_VAR(v_CPR, True, Bool)
245 GLOBAL_VAR(v_CSE, True, Bool)
247 -- these are the static flags you get without -O.
250 "-fignore-interface-pragmas",
251 "-fomit-interface-pragmas",
255 -- these are the static flags you get when -O is on.
259 "-fdo-eta-reduction",
260 "-fdo-lambda-eta-expansion",
267 hsc_minusO2_flags = hsc_minusO_flags -- for now
269 getStaticOptimisationFlags 0 = hsc_minusNoO_flags
270 getStaticOptimisationFlags 1 = hsc_minusO_flags
271 getStaticOptimisationFlags n = hsc_minusO2_flags
273 buildCoreToDo :: IO [CoreToDo]
275 opt_level <- readIORef v_OptLevel
276 max_iter <- readIORef v_MaxSimplifierIterations
277 usageSP <- readIORef v_UsageSPInf
278 strictness <- readIORef v_Strictness
279 cpr <- readIORef v_CPR
280 cse <- readIORef v_CSE
282 if opt_level == 0 then return
284 CoreDoSimplify (isAmongSimpl [
285 MaxSimplifierIterations max_iter
289 else {- opt_level >= 1 -} return [
291 -- initial simplify: mk specialiser happy: minimum effort please
292 CoreDoSimplify (isAmongSimpl [
294 -- Don't inline anything till full laziness has bitten
295 -- In particular, inlining wrappers inhibits floating
296 -- e.g. ...(case f x of ...)...
297 -- ==> ...(case (case x of I# x# -> fw x#) of ...)...
298 -- ==> ...(case x of I# x# -> case fw x# of ...)...
299 -- and now the redex (f x) isn't floatable any more
301 -- Similarly, don't apply any rules until after full
302 -- laziness. Notably, list fusion can prevent floating.
304 -- Don't do case-of-case transformations.
305 -- This makes full laziness work better
306 MaxSimplifierIterations max_iter
309 -- Specialisation is best done before full laziness
310 -- so that overloaded functions have all their dictionary lambdas manifest
313 CoreDoFloatOutwards False{-not full-},
316 CoreDoSimplify (isAmongSimpl [
318 -- Want to run with inline phase 1 after the specialiser to give
319 -- maximum chance for fusion to work before we inline build/augment
320 -- in phase 2. This made a difference in 'ansi' where an
321 -- overloaded function wasn't inlined till too late.
322 MaxSimplifierIterations max_iter
325 -- infer usage information here in case we need it later.
326 -- (add more of these where you need them --KSW 1999-04)
327 if usageSP then CoreDoUSPInf else CoreDoNothing,
329 CoreDoSimplify (isAmongSimpl [
330 -- Need inline-phase2 here so that build/augment get
331 -- inlined. I found that spectral/hartel/genfft lost some useful
332 -- strictness in the function sumcode' if augment is not inlined
333 -- before strictness analysis runs
335 MaxSimplifierIterations max_iter
338 CoreDoSimplify (isAmongSimpl [
339 MaxSimplifierIterations 2
340 -- No -finline-phase: allow all Ids to be inlined now
341 -- This gets foldr inlined before strictness analysis
344 if strictness then CoreDoStrictness else CoreDoNothing,
345 if cpr then CoreDoCPResult else CoreDoNothing,
349 CoreDoSimplify (isAmongSimpl [
350 MaxSimplifierIterations max_iter
351 -- No -finline-phase: allow all Ids to be inlined now
354 CoreDoFloatOutwards False{-not full-},
355 -- nofib/spectral/hartel/wang doubles in speed if you
356 -- do full laziness late in the day. It only happens
357 -- after fusion and other stuff, so the early pass doesn't
358 -- catch it. For the record, the redex is
359 -- f_el22 (f_el21 r_midblock)
362 -- Leave out lambda lifting for now
363 -- "-fsimplify", -- Tidy up results of full laziness
365 -- "-fmax-simplifier-iterations2",
367 -- "-ffloat-outwards-full",
369 -- We want CSE to follow the final full-laziness pass, because it may
370 -- succeed in commoning up things floated out by full laziness.
371 -- CSE used to rely on the no-shadowing invariant, but it doesn't any more
373 if cse then CoreCSE else CoreDoNothing,
377 -- Case-liberation for -O2. This should be after
378 -- strictness analysis and the simplification which follows it.
380 if opt_level >= 2 then
385 -- Final clean-up simplification:
386 CoreDoSimplify (isAmongSimpl [
387 MaxSimplifierIterations max_iter
388 -- No -finline-phase: allow all Ids to be inlined now
392 buildStgToDo :: IO [ StgToDo ]
394 stg_stats <- readIORef v_StgStats
395 let flags1 | stg_stats = [ D_stg_stats ]
399 ways_ <- readIORef v_Ways
400 let flags2 | WayProf `elem` ways_ = StgDoMassageForProfiling : flags1
405 -----------------------------------------------------------------------------
408 split_marker = ':' -- not configurable (ToDo)
410 v_Import_paths, v_Include_paths, v_Library_paths :: IORef [String]
411 GLOBAL_VAR(v_Import_paths, ["."], [String])
412 GLOBAL_VAR(v_Include_paths, ["."], [String])
413 GLOBAL_VAR(v_Library_paths, [], [String])
415 GLOBAL_VAR(v_Cmdline_libraries, [], [String])
417 addToDirList :: IORef [String] -> String -> IO ()
418 addToDirList ref path
419 = do paths <- readIORef ref
420 writeIORef ref (paths ++ split split_marker path)
422 -----------------------------------------------------------------------------
425 GLOBAL_VAR(v_Path_package_config, error "path_package_config", String)
427 -- package list is maintained in dependency order
428 GLOBAL_VAR(v_Packages, ("std":"rts":"gmp":[]), [String])
430 addPackage :: String -> IO ()
432 = do pkg_details <- readIORef v_Package_details
433 case lookupPkg package pkg_details of
434 Nothing -> throwDyn (OtherError ("unknown package name: " ++ package))
436 ps <- readIORef v_Packages
437 unless (package `elem` ps) $ do
438 mapM_ addPackage (package_deps details)
439 ps <- readIORef v_Packages
440 writeIORef v_Packages (package:ps)
442 getPackageImportPath :: IO [String]
443 getPackageImportPath = do
445 return (nub (concat (map import_dirs ps)))
447 getPackageIncludePath :: IO [String]
448 getPackageIncludePath = do
450 return (nub (filter (not.null) (concatMap include_dirs ps)))
452 -- includes are in reverse dependency order (i.e. rts first)
453 getPackageCIncludes :: IO [String]
454 getPackageCIncludes = do
456 return (reverse (nub (filter (not.null) (concatMap c_includes ps))))
458 getPackageLibraryPath :: IO [String]
459 getPackageLibraryPath = do
461 return (nub (concat (map library_dirs ps)))
463 getPackageLibraries :: IO [String]
464 getPackageLibraries = do
466 tag <- readIORef v_Build_tag
467 let suffix = if null tag then "" else '_':tag
469 map (\p -> map (++suffix) (hs_libraries p) ++ extra_libraries p) ps
472 getPackageExtraGhcOpts :: IO [String]
473 getPackageExtraGhcOpts = do
475 return (concatMap extra_ghc_opts ps)
477 getPackageExtraCcOpts :: IO [String]
478 getPackageExtraCcOpts = do
480 return (concatMap extra_cc_opts ps)
482 getPackageExtraLdOpts :: IO [String]
483 getPackageExtraLdOpts = do
485 return (concatMap extra_ld_opts ps)
487 getPackageInfo :: IO [Package]
489 ps <- readIORef v_Packages
492 getPackageDetails :: [String] -> IO [Package]
493 getPackageDetails ps = do
494 pkg_details <- readIORef v_Package_details
495 return [ pkg | p <- ps, Just pkg <- [ lookupPkg p pkg_details ] ]
497 GLOBAL_VAR(v_Package_details, (error "package_details"), [Package])
499 lookupPkg :: String -> [Package] -> Maybe Package
501 = case [p | p <- ps, name p == nm] of
504 -----------------------------------------------------------------------------
507 -- The central concept of a "way" is that all objects in a given
508 -- program must be compiled in the same "way". Certain options change
509 -- parameters of the virtual machine, eg. profiling adds an extra word
510 -- to the object header, so profiling objects cannot be linked with
511 -- non-profiling objects.
513 -- After parsing the command-line options, we determine which "way" we
514 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
516 -- We then find the "build-tag" associated with this way, and this
517 -- becomes the suffix used to find .hi files and libraries used in
520 GLOBAL_VAR(v_Build_tag, "", String)
549 GLOBAL_VAR(v_Ways, [] ,[WayName])
551 allowed_combination way = way `elem` combs
552 where -- the sub-lists must be ordered according to WayName,
553 -- because findBuildTag sorts them
554 combs = [ [WayProf,WayUnreg], [WayProf,WaySMP] ]
556 findBuildTag :: IO [String] -- new options
558 way_names <- readIORef v_Ways
559 case sort way_names of
560 [] -> do writeIORef v_Build_tag ""
563 [w] -> do let details = lkupWay w
564 writeIORef v_Build_tag (wayTag details)
565 return (wayOpts details)
567 ws -> if not (allowed_combination ws)
568 then throwDyn (OtherError $
569 "combination not supported: " ++
570 foldr1 (\a b -> a ++ '/':b)
571 (map (wayName . lkupWay) ws))
572 else let stuff = map lkupWay ws
573 tag = concat (map wayTag stuff)
574 flags = map wayOpts stuff
576 writeIORef v_Build_tag tag
577 return (concat flags)
580 case lookup w way_details of
581 Nothing -> error "findBuildTag"
582 Just details -> details
590 way_details :: [ (WayName, Way) ]
592 [ (WayProf, Way "p" "Profiling"
598 (WayTicky, Way "t" "Ticky-ticky Profiling"
601 , "-optc-DTICKY_TICKY"
604 (WayUnreg, Way "u" "Unregisterised"
607 (WayPar, Way "mp" "Parallel"
609 , "-D__PARALLEL_HASKELL__"
611 , "-package concurrent"
614 (WayGran, Way "mg" "Gransim"
618 , "-package concurrent"
621 (WaySMP, Way "s" "SMP"
628 (WayUser_a, Way "a" "User way 'a'" ["$WAY_a_REAL_OPTS"]),
629 (WayUser_b, Way "b" "User way 'b'" ["$WAY_b_REAL_OPTS"]),
630 (WayUser_c, Way "c" "User way 'c'" ["$WAY_c_REAL_OPTS"]),
631 (WayUser_d, Way "d" "User way 'd'" ["$WAY_d_REAL_OPTS"]),
632 (WayUser_e, Way "e" "User way 'e'" ["$WAY_e_REAL_OPTS"]),
633 (WayUser_f, Way "f" "User way 'f'" ["$WAY_f_REAL_OPTS"]),
634 (WayUser_g, Way "g" "User way 'g'" ["$WAY_g_REAL_OPTS"]),
635 (WayUser_h, Way "h" "User way 'h'" ["$WAY_h_REAL_OPTS"]),
636 (WayUser_i, Way "i" "User way 'i'" ["$WAY_i_REAL_OPTS"]),
637 (WayUser_j, Way "j" "User way 'j'" ["$WAY_j_REAL_OPTS"]),
638 (WayUser_k, Way "k" "User way 'k'" ["$WAY_k_REAL_OPTS"]),
639 (WayUser_l, Way "l" "User way 'l'" ["$WAY_l_REAL_OPTS"]),
640 (WayUser_m, Way "m" "User way 'm'" ["$WAY_m_REAL_OPTS"]),
641 (WayUser_n, Way "n" "User way 'n'" ["$WAY_n_REAL_OPTS"]),
642 (WayUser_o, Way "o" "User way 'o'" ["$WAY_o_REAL_OPTS"]),
643 (WayUser_A, Way "A" "User way 'A'" ["$WAY_A_REAL_OPTS"]),
644 (WayUser_B, Way "B" "User way 'B'" ["$WAY_B_REAL_OPTS"])
649 , "-optc-DUSE_MINIINTERPRETER"
650 , "-fno-asm-mangling"
654 -----------------------------------------------------------------------------
655 -- Programs for particular phases
657 GLOBAL_VAR(v_Pgm_L, error "pgm_L", String)
658 GLOBAL_VAR(v_Pgm_P, cRAWCPP, String)
659 GLOBAL_VAR(v_Pgm_c, cGCC, String)
660 GLOBAL_VAR(v_Pgm_m, error "pgm_m", String)
661 GLOBAL_VAR(v_Pgm_s, error "pgm_s", String)
662 GLOBAL_VAR(v_Pgm_a, cGCC, String)
663 GLOBAL_VAR(v_Pgm_l, cGCC, String)
664 GLOBAL_VAR(v_Pgm_dll, cMkDLL, String)
666 GLOBAL_VAR(v_Opt_dep, [], [String])
667 GLOBAL_VAR(v_Anti_opt_C, [], [String])
668 GLOBAL_VAR(v_Opt_C, [], [String])
669 GLOBAL_VAR(v_Opt_l, [], [String])
670 GLOBAL_VAR(v_Opt_dll, [], [String])
672 getStaticOpts :: IORef [String] -> IO [String]
673 getStaticOpts ref = readIORef ref >>= return . reverse
675 -----------------------------------------------------------------------------
676 -- Via-C compilation stuff
678 -- flags returned are: ( all C compilations
679 -- , registerised HC compilations
683 | prefixMatch "alpha" cTARGETPLATFORM
684 = return ( ["-static"], [] )
686 | prefixMatch "hppa" cTARGETPLATFORM
687 -- ___HPUX_SOURCE, not _HPUX_SOURCE, is #defined if -ansi!
688 -- (very nice, but too bad the HP /usr/include files don't agree.)
689 = return ( ["-static", "-D_HPUX_SOURCE"], [] )
691 | prefixMatch "m68k" cTARGETPLATFORM
692 -- -fno-defer-pop : for the .hc files, we want all the pushing/
693 -- popping of args to routines to be explicit; if we let things
694 -- be deferred 'til after an STGJUMP, imminent death is certain!
696 -- -fomit-frame-pointer : *don't*
697 -- It's better to have a6 completely tied up being a frame pointer
698 -- rather than let GCC pick random things to do with it.
699 -- (If we want to steal a6, then we would try to do things
700 -- as on iX86, where we *do* steal the frame pointer [%ebp].)
701 = return ( [], ["-fno-defer-pop", "-fno-omit-frame-pointer"] )
703 | prefixMatch "i386" cTARGETPLATFORM
704 -- -fno-defer-pop : basically the same game as for m68k
706 -- -fomit-frame-pointer : *must* in .hc files; because we're stealing
707 -- the fp (%ebp) for our register maps.
708 = do n_regs <- readState stolen_x86_regs
709 sta <- readIORef v_Static
710 return ( [ if sta then "-DDONT_WANT_WIN32_DLL_SUPPORT" else "",
711 if suffixMatch "mingw32" cTARGETPLATFORM then "-mno-cygwin" else "" ],
712 [ "-fno-defer-pop", "-fomit-frame-pointer",
713 "-DSTOLEN_X86_REGS="++show n_regs ]
716 | prefixMatch "mips" cTARGETPLATFORM
717 = return ( ["static"], [] )
719 | prefixMatch "powerpc" cTARGETPLATFORM || prefixMatch "rs6000" cTARGETPLATFORM
720 = return ( ["static"], ["-finhibit-size-directive"] )