1 -----------------------------------------------------------------------------
2 -- $Id: DriverState.hs,v 1.19 2000/12/08 12:32:15 simonpj 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 )
30 -----------------------------------------------------------------------------
33 -- certain flags can be specified on a per-file basis, in an OPTIONS
34 -- pragma at the beginning of the source file. This means that when
35 -- compiling mulitple files, we have to restore the global option
36 -- settings before compiling a new file.
38 -- The DriverState record contains the per-file-mutable state.
40 data DriverState = DriverState {
42 -- are we runing cpp on this file?
46 stolen_x86_regs :: Int,
47 cmdline_hc_includes :: [String],
49 -- options for a particular phase
57 initDriverState = DriverState {
60 cmdline_hc_includes = [],
68 -- The driver state is first initialized from the command line options,
69 -- and then reset to this initial state before each compilation.
70 -- v_InitDriverState contains the saved initial state, and v_DriverState
71 -- contains the current state (modified by any OPTIONS pragmas, for example).
73 -- v_InitDriverState may also be modified from the GHCi prompt, using :set.
75 GLOBAL_VAR(v_InitDriverState, initDriverState, DriverState)
76 GLOBAL_VAR(v_Driver_state, initDriverState, DriverState)
78 readState :: (DriverState -> a) -> IO a
79 readState f = readIORef v_Driver_state >>= return . f
81 updateState :: (DriverState -> DriverState) -> IO ()
82 updateState f = readIORef v_Driver_state >>= writeIORef v_Driver_state . f
84 addOpt_L a = updateState (\s -> s{opt_L = a : opt_L s})
85 addOpt_P a = updateState (\s -> s{opt_P = a : opt_P s})
86 addOpt_c a = updateState (\s -> s{opt_c = a : opt_c s})
87 addOpt_a a = updateState (\s -> s{opt_a = a : opt_a s})
88 addOpt_m a = updateState (\s -> s{opt_m = a : opt_m s})
90 addCmdlineHCInclude a =
91 updateState (\s -> s{cmdline_hc_includes = a : cmdline_hc_includes s})
93 -- we add to the options from the front, so we need to reverse the list
94 getOpts :: (DriverState -> [a]) -> IO [a]
95 getOpts opts = readState opts >>= return . reverse
97 -----------------------------------------------------------------------------
98 -- non-configured things
100 cHaskell1Version = "5" -- i.e., Haskell 98
102 -----------------------------------------------------------------------------
103 -- Global compilation flags
105 -- location of compiler-related files
106 GLOBAL_VAR(v_TopDir, clibdir, String)
109 v_Hs_source_cpp_opts = global
110 [ "-D__HASKELL1__="++cHaskell1Version
111 , "-D__GLASGOW_HASKELL__="++cProjectVersionInt
113 , "-D__CONCURRENT_HASKELL__"
115 {-# NOINLINE v_Hs_source_cpp_opts #-}
117 -- Keep output from intermediate phases
118 GLOBAL_VAR(v_Keep_hi_diffs, False, Bool)
119 GLOBAL_VAR(v_Keep_hc_files, False, Bool)
120 GLOBAL_VAR(v_Keep_s_files, False, Bool)
121 GLOBAL_VAR(v_Keep_raw_s_files, False, Bool)
122 GLOBAL_VAR(v_Keep_tmp_files, False, Bool)
125 GLOBAL_VAR(v_Scale_sizes_by, 1.0, Double)
126 GLOBAL_VAR(v_Dry_run, False, Bool)
127 #if !defined(HAVE_WIN32_DLL_SUPPORT) || defined(DONT_WANT_WIN32_DLL_SUPPORT)
128 GLOBAL_VAR(v_Static, True, Bool)
130 GLOBAL_VAR(v_Static, False, Bool)
132 GLOBAL_VAR(v_NoHsMain, False, Bool)
133 GLOBAL_VAR(v_Recomp, True, Bool)
134 GLOBAL_VAR(v_Collect_ghc_timing, False, Bool)
135 GLOBAL_VAR(v_Do_asm_mangling, True, Bool)
136 GLOBAL_VAR(v_Excess_precision, False, Bool)
138 -----------------------------------------------------------------------------
139 -- Splitting object files (for libraries)
141 GLOBAL_VAR(v_Split_object_files, False, Bool)
142 GLOBAL_VAR(v_Split_prefix, "", String)
143 GLOBAL_VAR(v_N_split_files, 0, Int)
146 can_split = prefixMatch "i386" cTARGETPLATFORM
147 || prefixMatch "alpha" cTARGETPLATFORM
148 || prefixMatch "hppa" cTARGETPLATFORM
149 || prefixMatch "m68k" cTARGETPLATFORM
150 || prefixMatch "mips" cTARGETPLATFORM
151 || prefixMatch "powerpc" cTARGETPLATFORM
152 || prefixMatch "rs6000" cTARGETPLATFORM
153 || prefixMatch "sparc" cTARGETPLATFORM
155 -----------------------------------------------------------------------------
156 -- Compiler output options
159 | cGhcWithNativeCodeGen == "YES" &&
160 (prefixMatch "i386" cTARGETPLATFORM ||
161 prefixMatch "sparc" cTARGETPLATFORM) = HscAsm
164 GLOBAL_VAR(v_Output_dir, Nothing, Maybe String)
165 GLOBAL_VAR(v_Object_suf, Nothing, Maybe String)
166 GLOBAL_VAR(v_Output_file, Nothing, Maybe String)
167 GLOBAL_VAR(v_Output_hi, Nothing, Maybe String)
169 GLOBAL_VAR(v_Ld_inputs, [], [String])
171 odir_ify :: String -> IO String
173 odir_opt <- readIORef v_Output_dir
176 Just d -> return (newdir d f)
178 osuf_ify :: String -> IO String
180 osuf_opt <- readIORef v_Object_suf
183 Just s -> return (newsuf s f)
185 -----------------------------------------------------------------------------
188 GLOBAL_VAR(v_ProduceHi, True, Bool)
189 GLOBAL_VAR(v_Hi_on_stdout, False, Bool)
190 GLOBAL_VAR(v_Hi_suf, "hi", String)
192 -----------------------------------------------------------------------------
193 -- Warnings & sanity checking
195 -- Warning packages that are controlled by -W and -Wall. The 'standard'
196 -- warnings that you get all the time are
198 -- -fwarn-overlapping-patterns
199 -- -fwarn-missing-methods
200 -- -fwarn-missing-fields
201 -- -fwarn-deprecations
202 -- -fwarn-duplicate-exports
204 -- these are turned off by -Wnot.
207 standardWarnings = [ "-fwarn-overlapping-patterns"
208 , "-fwarn-missing-methods"
209 , "-fwarn-missing-fields"
210 , "-fwarn-deprecations"
211 , "-fwarn-duplicate-exports"
213 minusWOpts = standardWarnings ++
214 [ "-fwarn-unused-binds"
215 , "-fwarn-unused-matches"
216 , "-fwarn-incomplete-patterns"
217 , "-fwarn-unused-imports"
219 minusWallOpts = minusWOpts ++
220 [ "-fwarn-type-defaults"
221 , "-fwarn-name-shadowing"
222 , "-fwarn-missing-signatures"
223 , "-fwarn-hi-shadowing"
226 data WarningState = W_default | W_ | W_all | W_not
227 GLOBAL_VAR(v_Warning_opt, W_default, WarningState)
229 -----------------------------------------------------------------------------
230 -- Compiler optimisation options
232 GLOBAL_VAR(v_OptLevel, 0, Int)
234 setOptLevel :: String -> IO ()
235 setOptLevel "" = do { writeIORef v_OptLevel 1 }
236 setOptLevel "not" = writeIORef v_OptLevel 0
237 setOptLevel [c] | isDigit c = do
238 let level = ord c - ord '0'
239 writeIORef v_OptLevel level
240 setOptLevel s = unknownFlagErr ("-O"++s)
242 GLOBAL_VAR(v_minus_o2_for_C, False, Bool)
243 GLOBAL_VAR(v_MaxSimplifierIterations, 4, Int)
244 GLOBAL_VAR(v_StgStats, False, Bool)
245 GLOBAL_VAR(v_UsageSPInf, False, Bool) -- Off by default
246 GLOBAL_VAR(v_Strictness, True, Bool)
247 GLOBAL_VAR(v_CPR, True, Bool)
248 GLOBAL_VAR(v_CSE, True, Bool)
250 hsc_minusO2_flags = hsc_minusO_flags -- for now
254 "-fignore-interface-pragmas",
255 "-fomit-interface-pragmas"
261 "-fdo-eta-reduction",
262 "-fdo-lambda-eta-expansion",
268 getStaticOptimisationFlags 0 = hsc_minusNoO_flags
269 getStaticOptimisationFlags 1 = hsc_minusO_flags
270 getStaticOptimisationFlags n = hsc_minusO2_flags
272 buildCoreToDo :: IO [CoreToDo]
274 opt_level <- readIORef v_OptLevel
275 max_iter <- readIORef v_MaxSimplifierIterations
276 usageSP <- readIORef v_UsageSPInf
277 strictness <- readIORef v_Strictness
278 cpr <- readIORef v_CPR
279 cse <- readIORef v_CSE
281 if opt_level == 0 then return
283 CoreDoSimplify (isAmongSimpl [
284 MaxSimplifierIterations max_iter
288 else {- opt_level >= 1 -} return [
290 -- initial simplify: mk specialiser happy: minimum effort please
291 CoreDoSimplify (isAmongSimpl [
293 -- Don't inline anything till full laziness has bitten
294 -- In particular, inlining wrappers inhibits floating
295 -- e.g. ...(case f x of ...)...
296 -- ==> ...(case (case x of I# x# -> fw x#) of ...)...
297 -- ==> ...(case x of I# x# -> case fw x# of ...)...
298 -- and now the redex (f x) isn't floatable any more
300 -- Similarly, don't apply any rules until after full
301 -- laziness. Notably, list fusion can prevent floating.
303 -- Don't do case-of-case transformations.
304 -- This makes full laziness work better
305 MaxSimplifierIterations max_iter
308 -- Specialisation is best done before full laziness
309 -- so that overloaded functions have all their dictionary lambdas manifest
312 CoreDoFloatOutwards False{-not full-},
315 CoreDoSimplify (isAmongSimpl [
317 -- Want to run with inline phase 1 after the specialiser to give
318 -- maximum chance for fusion to work before we inline build/augment
319 -- in phase 2. This made a difference in 'ansi' where an
320 -- overloaded function wasn't inlined till too late.
321 MaxSimplifierIterations max_iter
324 -- infer usage information here in case we need it later.
325 -- (add more of these where you need them --KSW 1999-04)
326 if usageSP then CoreDoUSPInf else CoreDoNothing,
328 CoreDoSimplify (isAmongSimpl [
329 -- Need inline-phase2 here so that build/augment get
330 -- inlined. I found that spectral/hartel/genfft lost some useful
331 -- strictness in the function sumcode' if augment is not inlined
332 -- before strictness analysis runs
334 MaxSimplifierIterations max_iter
337 CoreDoSimplify (isAmongSimpl [
338 MaxSimplifierIterations 2
339 -- No -finline-phase: allow all Ids to be inlined now
340 -- This gets foldr inlined before strictness analysis
343 if strictness then CoreDoStrictness else CoreDoNothing,
344 if cpr then CoreDoCPResult else CoreDoNothing,
348 CoreDoSimplify (isAmongSimpl [
349 MaxSimplifierIterations max_iter
350 -- No -finline-phase: allow all Ids to be inlined now
353 CoreDoFloatOutwards False{-not full-},
354 -- nofib/spectral/hartel/wang doubles in speed if you
355 -- do full laziness late in the day. It only happens
356 -- after fusion and other stuff, so the early pass doesn't
357 -- catch it. For the record, the redex is
358 -- f_el22 (f_el21 r_midblock)
361 -- Leave out lambda lifting for now
362 -- "-fsimplify", -- Tidy up results of full laziness
364 -- "-fmax-simplifier-iterations2",
366 -- "-ffloat-outwards-full",
368 -- We want CSE to follow the final full-laziness pass, because it may
369 -- succeed in commoning up things floated out by full laziness.
370 -- CSE used to rely on the no-shadowing invariant, but it doesn't any more
372 if cse then CoreCSE else CoreDoNothing,
376 -- Case-liberation for -O2. This should be after
377 -- strictness analysis and the simplification which follows it.
379 if opt_level >= 2 then
384 -- Final clean-up simplification:
385 CoreDoSimplify (isAmongSimpl [
386 MaxSimplifierIterations max_iter
387 -- No -finline-phase: allow all Ids to be inlined now
391 buildStgToDo :: IO [ StgToDo ]
393 stg_stats <- readIORef v_StgStats
394 let flags1 | stg_stats = [ D_stg_stats ]
398 ways_ <- readIORef v_Ways
399 let flags2 | WayProf `elem` ways_ = StgDoMassageForProfiling : flags1
404 -----------------------------------------------------------------------------
407 split_marker = ':' -- not configurable (ToDo)
409 v_Import_paths, v_Include_paths, v_Library_paths :: IORef [String]
410 GLOBAL_VAR(v_Import_paths, ["."], [String])
411 GLOBAL_VAR(v_Include_paths, ["."], [String])
412 GLOBAL_VAR(v_Library_paths, [], [String])
414 GLOBAL_VAR(v_Cmdline_libraries, [], [String])
416 addToDirList :: IORef [String] -> String -> IO ()
417 addToDirList ref path
418 = do paths <- readIORef ref
419 writeIORef ref (paths ++ split split_marker path)
421 -----------------------------------------------------------------------------
424 GLOBAL_VAR(v_Path_package_config, error "path_package_config", String)
426 -- package list is maintained in dependency order
427 GLOBAL_VAR(v_Packages, ("std":"rts":"gmp":[]), [String])
429 addPackage :: String -> IO ()
431 = do pkg_details <- readIORef v_Package_details
432 case lookupPkg package pkg_details of
433 Nothing -> throwDyn (OtherError ("unknown package name: " ++ package))
435 ps <- readIORef v_Packages
436 unless (package `elem` ps) $ do
437 mapM_ addPackage (package_deps details)
438 ps <- readIORef v_Packages
439 writeIORef v_Packages (package:ps)
441 getPackageImportPath :: IO [String]
442 getPackageImportPath = do
444 return (nub (concat (map import_dirs ps)))
446 getPackageIncludePath :: IO [String]
447 getPackageIncludePath = do
449 return (nub (filter (not.null) (concatMap include_dirs ps)))
451 -- includes are in reverse dependency order (i.e. rts first)
452 getPackageCIncludes :: IO [String]
453 getPackageCIncludes = do
455 return (reverse (nub (filter (not.null) (concatMap c_includes ps))))
457 getPackageLibraryPath :: IO [String]
458 getPackageLibraryPath = do
460 return (nub (concat (map library_dirs ps)))
462 getPackageLibraries :: IO [String]
463 getPackageLibraries = do
465 tag <- readIORef v_Build_tag
466 let suffix = if null tag then "" else '_':tag
468 map (\p -> map (++suffix) (hs_libraries p) ++ extra_libraries p) ps
471 getPackageExtraGhcOpts :: IO [String]
472 getPackageExtraGhcOpts = do
474 return (concatMap extra_ghc_opts ps)
476 getPackageExtraCcOpts :: IO [String]
477 getPackageExtraCcOpts = do
479 return (concatMap extra_cc_opts ps)
481 getPackageExtraLdOpts :: IO [String]
482 getPackageExtraLdOpts = do
484 return (concatMap extra_ld_opts ps)
486 getPackageInfo :: IO [Package]
488 ps <- readIORef v_Packages
491 getPackageDetails :: [String] -> IO [Package]
492 getPackageDetails ps = do
493 pkg_details <- readIORef v_Package_details
494 return [ pkg | p <- ps, Just pkg <- [ lookupPkg p pkg_details ] ]
496 GLOBAL_VAR(v_Package_details, (error "package_details"), [Package])
498 lookupPkg :: String -> [Package] -> Maybe Package
500 = case [p | p <- ps, name p == nm] of
503 -----------------------------------------------------------------------------
506 -- The central concept of a "way" is that all objects in a given
507 -- program must be compiled in the same "way". Certain options change
508 -- parameters of the virtual machine, eg. profiling adds an extra word
509 -- to the object header, so profiling objects cannot be linked with
510 -- non-profiling objects.
512 -- After parsing the command-line options, we determine which "way" we
513 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
515 -- We then find the "build-tag" associated with this way, and this
516 -- becomes the suffix used to find .hi files and libraries used in
519 GLOBAL_VAR(v_Build_tag, "", String)
548 GLOBAL_VAR(v_Ways, [] ,[WayName])
550 allowed_combination way = way `elem` combs
551 where -- the sub-lists must be ordered according to WayName,
552 -- because findBuildTag sorts them
553 combs = [ [WayProf,WayUnreg], [WayProf,WaySMP] ]
555 findBuildTag :: IO [String] -- new options
557 way_names <- readIORef v_Ways
558 case sort way_names of
559 [] -> do writeIORef v_Build_tag ""
562 [w] -> do let details = lkupWay w
563 writeIORef v_Build_tag (wayTag details)
564 return (wayOpts details)
566 ws -> if not (allowed_combination ws)
567 then throwDyn (OtherError $
568 "combination not supported: " ++
569 foldr1 (\a b -> a ++ '/':b)
570 (map (wayName . lkupWay) ws))
571 else let stuff = map lkupWay ws
572 tag = concat (map wayTag stuff)
573 flags = map wayOpts stuff
575 writeIORef v_Build_tag tag
576 return (concat flags)
579 case lookup w way_details of
580 Nothing -> error "findBuildTag"
581 Just details -> details
589 way_details :: [ (WayName, Way) ]
591 [ (WayProf, Way "p" "Profiling"
597 (WayTicky, Way "t" "Ticky-ticky Profiling"
600 , "-optc-DTICKY_TICKY"
603 (WayUnreg, Way "u" "Unregisterised"
605 , "-optc-DUSE_MINIINTERPRETER"
606 , "-fno-asm-mangling"
610 (WayPar, Way "mp" "Parallel"
612 , "-D__PARALLEL_HASKELL__"
614 , "-package concurrent"
617 (WayGran, Way "mg" "Gransim"
621 , "-package concurrent"
624 (WaySMP, Way "s" "SMP"
631 (WayUser_a, Way "a" "User way 'a'" ["$WAY_a_REAL_OPTS"]),
632 (WayUser_b, Way "b" "User way 'b'" ["$WAY_b_REAL_OPTS"]),
633 (WayUser_c, Way "c" "User way 'c'" ["$WAY_c_REAL_OPTS"]),
634 (WayUser_d, Way "d" "User way 'd'" ["$WAY_d_REAL_OPTS"]),
635 (WayUser_e, Way "e" "User way 'e'" ["$WAY_e_REAL_OPTS"]),
636 (WayUser_f, Way "f" "User way 'f'" ["$WAY_f_REAL_OPTS"]),
637 (WayUser_g, Way "g" "User way 'g'" ["$WAY_g_REAL_OPTS"]),
638 (WayUser_h, Way "h" "User way 'h'" ["$WAY_h_REAL_OPTS"]),
639 (WayUser_i, Way "i" "User way 'i'" ["$WAY_i_REAL_OPTS"]),
640 (WayUser_j, Way "j" "User way 'j'" ["$WAY_j_REAL_OPTS"]),
641 (WayUser_k, Way "k" "User way 'k'" ["$WAY_k_REAL_OPTS"]),
642 (WayUser_l, Way "l" "User way 'l'" ["$WAY_l_REAL_OPTS"]),
643 (WayUser_m, Way "m" "User way 'm'" ["$WAY_m_REAL_OPTS"]),
644 (WayUser_n, Way "n" "User way 'n'" ["$WAY_n_REAL_OPTS"]),
645 (WayUser_o, Way "o" "User way 'o'" ["$WAY_o_REAL_OPTS"]),
646 (WayUser_A, Way "A" "User way 'A'" ["$WAY_A_REAL_OPTS"]),
647 (WayUser_B, Way "B" "User way 'B'" ["$WAY_B_REAL_OPTS"])
650 -----------------------------------------------------------------------------
651 -- Programs for particular phases
653 GLOBAL_VAR(v_Pgm_L, error "pgm_L", String)
654 GLOBAL_VAR(v_Pgm_P, cRAWCPP, String)
655 GLOBAL_VAR(v_Pgm_c, cGCC, String)
656 GLOBAL_VAR(v_Pgm_m, error "pgm_m", String)
657 GLOBAL_VAR(v_Pgm_s, error "pgm_s", String)
658 GLOBAL_VAR(v_Pgm_a, cGCC, String)
659 GLOBAL_VAR(v_Pgm_l, cGCC, String)
661 GLOBAL_VAR(v_Opt_dep, [], [String])
662 GLOBAL_VAR(v_Anti_opt_C, [], [String])
663 GLOBAL_VAR(v_Opt_C, [], [String])
664 GLOBAL_VAR(v_Opt_l, [], [String])
665 GLOBAL_VAR(v_Opt_dll, [], [String])
667 getStaticOpts :: IORef [String] -> IO [String]
668 getStaticOpts ref = readIORef ref >>= return . reverse
670 -----------------------------------------------------------------------------
671 -- Via-C compilation stuff
673 -- flags returned are: ( all C compilations
674 -- , registerised HC compilations
678 | prefixMatch "alpha" cTARGETPLATFORM
679 = return ( ["-static"], [] )
681 | prefixMatch "hppa" cTARGETPLATFORM
682 -- ___HPUX_SOURCE, not _HPUX_SOURCE, is #defined if -ansi!
683 -- (very nice, but too bad the HP /usr/include files don't agree.)
684 = return ( ["-static", "-D_HPUX_SOURCE"], [] )
686 | prefixMatch "m68k" cTARGETPLATFORM
687 -- -fno-defer-pop : for the .hc files, we want all the pushing/
688 -- popping of args to routines to be explicit; if we let things
689 -- be deferred 'til after an STGJUMP, imminent death is certain!
691 -- -fomit-frame-pointer : *don't*
692 -- It's better to have a6 completely tied up being a frame pointer
693 -- rather than let GCC pick random things to do with it.
694 -- (If we want to steal a6, then we would try to do things
695 -- as on iX86, where we *do* steal the frame pointer [%ebp].)
696 = return ( [], ["-fno-defer-pop", "-fno-omit-frame-pointer"] )
698 | prefixMatch "i386" cTARGETPLATFORM
699 -- -fno-defer-pop : basically the same game as for m68k
701 -- -fomit-frame-pointer : *must* in .hc files; because we're stealing
702 -- the fp (%ebp) for our register maps.
703 = do n_regs <- readState stolen_x86_regs
704 sta <- readIORef v_Static
705 return ( [ if sta then "-DDONT_WANT_WIN32_DLL_SUPPORT" else "",
706 if suffixMatch "mingw32" cTARGETPLATFORM then "-mno-cygwin" else "" ],
707 [ "-fno-defer-pop", "-fomit-frame-pointer",
708 "-DSTOLEN_X86_REGS="++show n_regs ]
711 | prefixMatch "mips" cTARGETPLATFORM
712 = return ( ["static"], [] )
714 | prefixMatch "powerpc" cTARGETPLATFORM || prefixMatch "rs6000" cTARGETPLATFORM
715 = return ( ["static"], ["-finhibit-size-directive"] )