1 -----------------------------------------------------------------------------
2 -- $Id: DriverState.hs,v 1.17 2000/12/04 16:42:14 rrt 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 )
32 -----------------------------------------------------------------------------
35 -- certain flags can be specified on a per-file basis, in an OPTIONS
36 -- pragma at the beginning of the source file. This means that when
37 -- compiling mulitple files, we have to restore the global option
38 -- settings before compiling a new file.
40 -- The DriverState record contains the per-file-mutable state.
42 data DriverState = DriverState {
44 -- are we runing cpp on this file?
48 stolen_x86_regs :: Int,
49 cmdline_hc_includes :: [String],
51 -- options for a particular phase
59 initDriverState = DriverState {
62 cmdline_hc_includes = [],
70 -- The driver state is first initialized from the command line options,
71 -- and then reset to this initial state before each compilation.
72 -- v_InitDriverState contains the saved initial state, and v_DriverState
73 -- contains the current state (modified by any OPTIONS pragmas, for example).
75 -- v_InitDriverState may also be modified from the GHCi prompt, using :set.
77 GLOBAL_VAR(v_InitDriverState, initDriverState, DriverState)
78 GLOBAL_VAR(v_Driver_state, initDriverState, DriverState)
80 readState :: (DriverState -> a) -> IO a
81 readState f = readIORef v_Driver_state >>= return . f
83 updateState :: (DriverState -> DriverState) -> IO ()
84 updateState f = readIORef v_Driver_state >>= writeIORef v_Driver_state . f
86 addOpt_L a = updateState (\s -> s{opt_L = a : opt_L s})
87 addOpt_P a = updateState (\s -> s{opt_P = a : opt_P s})
88 addOpt_c a = updateState (\s -> s{opt_c = a : opt_c s})
89 addOpt_a a = updateState (\s -> s{opt_a = a : opt_a s})
90 addOpt_m a = updateState (\s -> s{opt_m = a : opt_m s})
92 addCmdlineHCInclude a =
93 updateState (\s -> s{cmdline_hc_includes = a : cmdline_hc_includes s})
95 -- we add to the options from the front, so we need to reverse the list
96 getOpts :: (DriverState -> [a]) -> IO [a]
97 getOpts opts = readState opts >>= return . reverse
99 -----------------------------------------------------------------------------
100 -- non-configured things
102 cHaskell1Version = "5" -- i.e., Haskell 98
104 -----------------------------------------------------------------------------
105 -- Global compilation flags
107 -- location of compiler-related files
108 GLOBAL_VAR(v_TopDir, clibdir, String)
111 v_Hs_source_cpp_opts = global
112 [ "-D__HASKELL1__="++cHaskell1Version
113 , "-D__GLASGOW_HASKELL__="++cProjectVersionInt
115 , "-D__CONCURRENT_HASKELL__"
117 {-# NOINLINE v_Hs_source_cpp_opts #-}
119 -- Keep output from intermediate phases
120 GLOBAL_VAR(v_Keep_hi_diffs, False, Bool)
121 GLOBAL_VAR(v_Keep_hc_files, False, Bool)
122 GLOBAL_VAR(v_Keep_s_files, False, Bool)
123 GLOBAL_VAR(v_Keep_raw_s_files, False, Bool)
124 GLOBAL_VAR(v_Keep_tmp_files, False, Bool)
127 GLOBAL_VAR(v_Scale_sizes_by, 1.0, Double)
128 GLOBAL_VAR(v_Dry_run, False, Bool)
129 #if !defined(HAVE_WIN32_DLL_SUPPORT) || defined(DONT_WANT_WIN32_DLL_SUPPORT)
130 GLOBAL_VAR(v_Static, True, Bool)
132 GLOBAL_VAR(v_Static, False, Bool)
134 GLOBAL_VAR(v_NoHsMain, False, Bool)
135 GLOBAL_VAR(v_Recomp, True, Bool)
136 GLOBAL_VAR(v_Collect_ghc_timing, False, Bool)
137 GLOBAL_VAR(v_Do_asm_mangling, True, Bool)
138 GLOBAL_VAR(v_Excess_precision, False, Bool)
140 -----------------------------------------------------------------------------
141 -- Splitting object files (for libraries)
143 GLOBAL_VAR(v_Split_object_files, False, Bool)
144 GLOBAL_VAR(v_Split_prefix, "", String)
145 GLOBAL_VAR(v_N_split_files, 0, Int)
148 can_split = prefixMatch "i386" cTARGETPLATFORM
149 || prefixMatch "alpha" cTARGETPLATFORM
150 || prefixMatch "hppa" cTARGETPLATFORM
151 || prefixMatch "m68k" cTARGETPLATFORM
152 || prefixMatch "mips" cTARGETPLATFORM
153 || prefixMatch "powerpc" cTARGETPLATFORM
154 || prefixMatch "rs6000" cTARGETPLATFORM
155 || prefixMatch "sparc" cTARGETPLATFORM
157 -----------------------------------------------------------------------------
158 -- Compiler output options
161 | cGhcWithNativeCodeGen == "YES" &&
162 (prefixMatch "i386" cTARGETPLATFORM ||
163 prefixMatch "sparc" cTARGETPLATFORM) = HscAsm
166 GLOBAL_VAR(v_Output_dir, Nothing, Maybe String)
167 GLOBAL_VAR(v_Object_suf, Nothing, Maybe String)
168 GLOBAL_VAR(v_Output_file, Nothing, Maybe String)
169 GLOBAL_VAR(v_Output_hi, Nothing, Maybe String)
171 GLOBAL_VAR(v_Ld_inputs, [], [String])
173 odir_ify :: String -> IO String
175 odir_opt <- readIORef v_Output_dir
178 Just d -> return (newdir d f)
180 osuf_ify :: String -> IO String
182 osuf_opt <- readIORef v_Object_suf
185 Just s -> return (newsuf s f)
187 -----------------------------------------------------------------------------
190 GLOBAL_VAR(v_ProduceHi, True, Bool)
191 GLOBAL_VAR(v_Hi_on_stdout, False, Bool)
192 GLOBAL_VAR(v_Hi_suf, "hi", String)
194 -----------------------------------------------------------------------------
195 -- Warnings & sanity checking
197 -- Warning packages that are controlled by -W and -Wall. The 'standard'
198 -- warnings that you get all the time are
200 -- -fwarn-overlapping-patterns
201 -- -fwarn-missing-methods
202 -- -fwarn-missing-fields
203 -- -fwarn-deprecations
204 -- -fwarn-duplicate-exports
206 -- these are turned off by -Wnot.
209 standardWarnings = [ "-fwarn-overlapping-patterns"
210 , "-fwarn-missing-methods"
211 , "-fwarn-missing-fields"
212 , "-fwarn-deprecations"
213 , "-fwarn-duplicate-exports"
215 minusWOpts = standardWarnings ++
216 [ "-fwarn-unused-binds"
217 , "-fwarn-unused-matches"
218 , "-fwarn-incomplete-patterns"
219 , "-fwarn-unused-imports"
221 minusWallOpts = minusWOpts ++
222 [ "-fwarn-type-defaults"
223 , "-fwarn-name-shadowing"
224 , "-fwarn-missing-signatures"
225 , "-fwarn-hi-shadowing"
228 data WarningState = W_default | W_ | W_all | W_not
229 GLOBAL_VAR(v_Warning_opt, W_default, WarningState)
231 -----------------------------------------------------------------------------
232 -- Compiler optimisation options
234 GLOBAL_VAR(v_OptLevel, 0, Int)
236 setOptLevel :: String -> IO ()
237 setOptLevel "" = do { writeIORef v_OptLevel 1 }
238 setOptLevel "not" = writeIORef v_OptLevel 0
239 setOptLevel [c] | isDigit c = do
240 let level = ord c - ord '0'
241 writeIORef v_OptLevel level
242 setOptLevel s = unknownFlagErr ("-O"++s)
244 GLOBAL_VAR(v_minus_o2_for_C, False, Bool)
245 GLOBAL_VAR(v_MaxSimplifierIterations, 4, Int)
246 GLOBAL_VAR(v_StgStats, False, Bool)
247 GLOBAL_VAR(v_UsageSPInf, False, Bool) -- Off by default
248 GLOBAL_VAR(v_Strictness, True, Bool)
249 GLOBAL_VAR(v_CPR, True, Bool)
250 GLOBAL_VAR(v_CSE, True, Bool)
252 hsc_minusO2_flags = hsc_minusO_flags -- for now
256 "-fignore-interface-pragmas",
257 "-fomit-interface-pragmas"
263 "-fdo-eta-reduction",
264 "-fdo-lambda-eta-expansion",
270 getStaticOptimisationFlags 0 = hsc_minusNoO_flags
271 getStaticOptimisationFlags 1 = hsc_minusO_flags
272 getStaticOptimisationFlags n = hsc_minusO2_flags
274 buildCoreToDo :: IO [CoreToDo]
276 opt_level <- readIORef v_OptLevel
277 max_iter <- readIORef v_MaxSimplifierIterations
278 usageSP <- readIORef v_UsageSPInf
279 strictness <- readIORef v_Strictness
280 cpr <- readIORef v_CPR
281 cse <- readIORef v_CSE
283 if opt_level == 0 then return
285 CoreDoSimplify (isAmongSimpl [
286 MaxSimplifierIterations max_iter
290 else {- level >= 1 -} return [
292 -- initial simplify: mk specialiser happy: minimum effort please
293 CoreDoSimplify (isAmongSimpl [
295 -- Don't inline anything till full laziness has bitten
296 -- In particular, inlining wrappers inhibits floating
297 -- e.g. ...(case f x of ...)...
298 -- ==> ...(case (case x of I# x# -> fw x#) of ...)...
299 -- ==> ...(case x of I# x# -> case fw x# of ...)...
300 -- and now the redex (f x) isn't floatable any more
302 -- Similarly, don't apply any rules until after full
303 -- laziness. Notably, list fusion can prevent floating.
305 -- Don't do case-of-case transformations.
306 -- This makes full laziness work better
307 MaxSimplifierIterations max_iter
310 -- Specialisation is best done before full laziness
311 -- so that overloaded functions have all their dictionary lambdas manifest
314 CoreDoFloatOutwards False{-not full-},
317 CoreDoSimplify (isAmongSimpl [
319 -- Want to run with inline phase 1 after the specialiser to give
320 -- maximum chance for fusion to work before we inline build/augment
321 -- in phase 2. This made a difference in 'ansi' where an
322 -- overloaded function wasn't inlined till too late.
323 MaxSimplifierIterations max_iter
326 -- infer usage information here in case we need it later.
327 -- (add more of these where you need them --KSW 1999-04)
328 if usageSP then CoreDoUSPInf else CoreDoNothing,
330 CoreDoSimplify (isAmongSimpl [
331 -- Need inline-phase2 here so that build/augment get
332 -- inlined. I found that spectral/hartel/genfft lost some useful
333 -- strictness in the function sumcode' if augment is not inlined
334 -- before strictness analysis runs
336 MaxSimplifierIterations max_iter
339 CoreDoSimplify (isAmongSimpl [
340 MaxSimplifierIterations 2
341 -- No -finline-phase: allow all Ids to be inlined now
342 -- This gets foldr inlined before strictness analysis
345 if strictness then CoreDoStrictness else CoreDoNothing,
346 if cpr then CoreDoCPResult else CoreDoNothing,
350 CoreDoSimplify (isAmongSimpl [
351 MaxSimplifierIterations max_iter
352 -- No -finline-phase: allow all Ids to be inlined now
355 CoreDoFloatOutwards False{-not full-},
356 -- nofib/spectral/hartel/wang doubles in speed if you
357 -- do full laziness late in the day. It only happens
358 -- after fusion and other stuff, so the early pass doesn't
359 -- catch it. For the record, the redex is
360 -- 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.
372 -- CSE must immediately follow a simplification pass, because it relies
373 -- on the no-shadowing invariant. See comments at the top of CSE.lhs
374 -- So it must NOT follow float-inwards, which can give rise to shadowing,
375 -- even if its input doesn't have shadows. Hence putting it between
377 if cse then CoreCSE else CoreDoNothing,
381 -- Case-liberation for -O2. This should be after
382 -- strictness analysis and the simplification which follows it.
384 -- ( ($OptLevel != 2)
386 -- : "-fliberate-case -fsimplify [ $Oopt_FB_Support -ffloat-lets-exposing-whnf -ffloat-primops-ok -fcase-of-case -fdo-case-elim -fcase-merge -fdo-lambda-eta-expansion -freuse-con -flet-to-case $Oopt_PedanticBottoms $Oopt_MaxSimplifierIterations $Oopt_ShowSimplifierProgress ]" ),
388 -- "-fliberate-case",
390 -- Final clean-up simplification:
391 CoreDoSimplify (isAmongSimpl [
392 MaxSimplifierIterations max_iter
393 -- No -finline-phase: allow all Ids to be inlined now
397 buildStgToDo :: IO [ StgToDo ]
399 stg_stats <- readIORef v_StgStats
400 let flags1 | stg_stats = [ D_stg_stats ]
404 ways_ <- readIORef v_Ways
405 let flags2 | WayProf `elem` ways_ = StgDoMassageForProfiling : flags1
410 -----------------------------------------------------------------------------
413 split_marker = ':' -- not configurable (ToDo)
415 v_Import_paths, v_Include_paths, v_Library_paths :: IORef [String]
416 GLOBAL_VAR(v_Import_paths, ["."], [String])
417 GLOBAL_VAR(v_Include_paths, ["."], [String])
418 GLOBAL_VAR(v_Library_paths, [], [String])
420 GLOBAL_VAR(v_Cmdline_libraries, [], [String])
422 addToDirList :: IORef [String] -> String -> IO ()
423 addToDirList ref path
424 = do paths <- readIORef ref
425 writeIORef ref (paths ++ split split_marker path)
427 -----------------------------------------------------------------------------
430 GLOBAL_VAR(v_Path_package_config, error "path_package_config", String)
432 -- package list is maintained in dependency order
433 GLOBAL_VAR(v_Packages, ("std":"rts":"gmp":[]), [String])
435 addPackage :: String -> IO ()
437 = do pkg_details <- readIORef v_Package_details
438 case lookupPkg package pkg_details of
439 Nothing -> throwDyn (OtherError ("unknown package name: " ++ package))
441 ps <- readIORef v_Packages
442 unless (package `elem` ps) $ do
443 mapM_ addPackage (package_deps details)
444 ps <- readIORef v_Packages
445 writeIORef v_Packages (package:ps)
447 getPackageImportPath :: IO [String]
448 getPackageImportPath = do
450 return (nub (concat (map import_dirs ps)))
452 getPackageIncludePath :: IO [String]
453 getPackageIncludePath = do
455 return (nub (filter (not.null) (concatMap include_dirs ps)))
457 -- includes are in reverse dependency order (i.e. rts first)
458 getPackageCIncludes :: IO [String]
459 getPackageCIncludes = do
461 return (reverse (nub (filter (not.null) (concatMap c_includes ps))))
463 getPackageLibraryPath :: IO [String]
464 getPackageLibraryPath = do
466 return (nub (concat (map library_dirs ps)))
468 getPackageLibraries :: IO [String]
469 getPackageLibraries = do
471 tag <- readIORef v_Build_tag
472 let suffix = if null tag then "" else '_':tag
474 map (\p -> map (++suffix) (hs_libraries p) ++ extra_libraries p) ps
477 getPackageExtraGhcOpts :: IO [String]
478 getPackageExtraGhcOpts = do
480 return (concatMap extra_ghc_opts ps)
482 getPackageExtraCcOpts :: IO [String]
483 getPackageExtraCcOpts = do
485 return (concatMap extra_cc_opts ps)
487 getPackageExtraLdOpts :: IO [String]
488 getPackageExtraLdOpts = do
490 return (concatMap extra_ld_opts ps)
492 getPackageInfo :: IO [Package]
494 ps <- readIORef v_Packages
497 getPackageDetails :: [String] -> IO [Package]
498 getPackageDetails ps = do
499 pkg_details <- readIORef v_Package_details
500 return [ pkg | p <- ps, Just pkg <- [ lookupPkg p pkg_details ] ]
502 GLOBAL_VAR(v_Package_details, (error "package_details"), [Package])
504 lookupPkg :: String -> [Package] -> Maybe Package
506 = case [p | p <- ps, name p == nm] of
509 -----------------------------------------------------------------------------
512 -- The central concept of a "way" is that all objects in a given
513 -- program must be compiled in the same "way". Certain options change
514 -- parameters of the virtual machine, eg. profiling adds an extra word
515 -- to the object header, so profiling objects cannot be linked with
516 -- non-profiling objects.
518 -- After parsing the command-line options, we determine which "way" we
519 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
521 -- We then find the "build-tag" associated with this way, and this
522 -- becomes the suffix used to find .hi files and libraries used in
525 GLOBAL_VAR(v_Build_tag, "", String)
554 GLOBAL_VAR(v_Ways, [] ,[WayName])
556 allowed_combinations way = ways `elem` combs
557 where -- the sub-lists must be ordered according to WayName, because findBuildTag sorts them
558 combs = [ [WayProf,WayUnreg], [WayProf,WaySMP] ]
560 findBuildTag :: IO [String] -- new options
562 way_names <- readIORef v_Ways
563 case sort way_names of
564 [] -> do writeIORef v_Build_tag ""
567 [w] -> do let details = lkupWay w
568 writeIORef v_Build_tag (wayTag details)
569 return (wayOpts details)
571 ws -> if not allowed_combination ws
572 then throwDyn (OtherError $
573 "combination not supported: " ++
574 foldr1 (\a b -> a ++ '/':b)
575 (map (wayName . lkupWay) ws))
576 else let stuff = map lkupWay ws
577 tag = concat (map wayTag stuff)
578 flags = map wayOpts stuff
580 writeIORef v_Build_tag tag
581 return (concat flags)
584 case lookup w way_details of
585 Nothing -> error "findBuildTag"
586 Just details -> details
594 way_details :: [ (WayName, Way) ]
596 [ (WayProf, Way "p" "Profiling"
602 (WayTicky, Way "t" "Ticky-ticky Profiling"
605 , "-optc-DTICKY_TICKY"
608 (WayUnreg, Way "u" "Unregisterised"
610 , "-optc-DUSE_MINIINTERPRETER"
611 , "-fno-asm-mangling"
615 (WayPar, Way "mp" "Parallel"
617 , "-D__PARALLEL_HASKELL__"
619 , "-package concurrent"
622 (WayGran, Way "mg" "Gransim"
626 , "-package concurrent"
629 (WaySMP, Way "s" "SMP"
636 (WayUser_a, Way "a" "User way 'a'" ["$WAY_a_REAL_OPTS"]),
637 (WayUser_b, Way "b" "User way 'b'" ["$WAY_b_REAL_OPTS"]),
638 (WayUser_c, Way "c" "User way 'c'" ["$WAY_c_REAL_OPTS"]),
639 (WayUser_d, Way "d" "User way 'd'" ["$WAY_d_REAL_OPTS"]),
640 (WayUser_e, Way "e" "User way 'e'" ["$WAY_e_REAL_OPTS"]),
641 (WayUser_f, Way "f" "User way 'f'" ["$WAY_f_REAL_OPTS"]),
642 (WayUser_g, Way "g" "User way 'g'" ["$WAY_g_REAL_OPTS"]),
643 (WayUser_h, Way "h" "User way 'h'" ["$WAY_h_REAL_OPTS"]),
644 (WayUser_i, Way "i" "User way 'i'" ["$WAY_i_REAL_OPTS"]),
645 (WayUser_j, Way "j" "User way 'j'" ["$WAY_j_REAL_OPTS"]),
646 (WayUser_k, Way "k" "User way 'k'" ["$WAY_k_REAL_OPTS"]),
647 (WayUser_l, Way "l" "User way 'l'" ["$WAY_l_REAL_OPTS"]),
648 (WayUser_m, Way "m" "User way 'm'" ["$WAY_m_REAL_OPTS"]),
649 (WayUser_n, Way "n" "User way 'n'" ["$WAY_n_REAL_OPTS"]),
650 (WayUser_o, Way "o" "User way 'o'" ["$WAY_o_REAL_OPTS"]),
651 (WayUser_A, Way "A" "User way 'A'" ["$WAY_A_REAL_OPTS"]),
652 (WayUser_B, Way "B" "User way 'B'" ["$WAY_B_REAL_OPTS"])
655 -----------------------------------------------------------------------------
656 -- Programs for particular phases
658 GLOBAL_VAR(v_Pgm_L, error "pgm_L", String)
659 GLOBAL_VAR(v_Pgm_P, cRAWCPP, String)
660 GLOBAL_VAR(v_Pgm_c, cGCC, String)
661 GLOBAL_VAR(v_Pgm_m, error "pgm_m", String)
662 GLOBAL_VAR(v_Pgm_s, error "pgm_s", String)
663 GLOBAL_VAR(v_Pgm_a, cGCC, String)
664 GLOBAL_VAR(v_Pgm_l, cGCC, 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"] )