[project @ 2000-12-12 14:35:08 by simonmar]
[ghc-hetmet.git] / ghc / compiler / main / DriverState.hs
index f3299af..5723788 100644 (file)
@@ -1,5 +1,5 @@
 -----------------------------------------------------------------------------
--- $Id: DriverState.hs,v 1.8 2000/10/26 16:51:44 sewardj Exp $
+-- $Id: DriverState.hs,v 1.20 2000/12/12 14:35:08 simonmar Exp $
 --
 -- Settings for the driver
 --
@@ -16,12 +16,14 @@ import CmdLineOpts
 import DriverUtil
 import Util
 import Config
-
 import Exception
 import IOExts
+#ifdef mingw32_TARGET_OS
+import TmpFiles        ( newTempName )
+import Directory ( removeFile )
+#endif
+import Panic
 
-import System
-import IO
 import List
 import Char  
 import Monad
@@ -64,7 +66,15 @@ initDriverState = DriverState {
        opt_m                   = [],
    }
        
-GLOBAL_VAR(v_Driver_state, initDriverState, DriverState)
+-- The driver state is first initialized from the command line options,
+-- and then reset to this initial state before each compilation.
+-- v_InitDriverState contains the saved initial state, and v_DriverState
+-- contains the current state (modified by any OPTIONS pragmas, for example).
+--
+-- v_InitDriverState may also be modified from the GHCi prompt, using :set.
+--
+GLOBAL_VAR(v_InitDriverState, initDriverState, DriverState)
+GLOBAL_VAR(v_Driver_state,    initDriverState, DriverState)
 
 readState :: (DriverState -> a) -> IO a
 readState f = readIORef v_Driver_state >>= return . f
@@ -72,11 +82,11 @@ readState f = readIORef v_Driver_state >>= return . f
 updateState :: (DriverState -> DriverState) -> IO ()
 updateState f = readIORef v_Driver_state >>= writeIORef v_Driver_state . f
 
-addOpt_L     a = updateState (\s -> s{opt_L      =  a : opt_L      s})
-addOpt_P     a = updateState (\s -> s{opt_P      =  a : opt_P      s})
-addOpt_c     a = updateState (\s -> s{opt_c      =  a : opt_c      s})
-addOpt_a     a = updateState (\s -> s{opt_a      =  a : opt_a      s})
-addOpt_m     a = updateState (\s -> s{opt_m      =  a : opt_m      s})
+addOpt_L     a = updateState (\s -> s{opt_L =  a : opt_L s})
+addOpt_P     a = updateState (\s -> s{opt_P =  a : opt_P s})
+addOpt_c     a = updateState (\s -> s{opt_c =  a : opt_c s})
+addOpt_a     a = updateState (\s -> s{opt_a =  a : opt_a s})
+addOpt_m     a = updateState (\s -> s{opt_m =  a : opt_m s})
 
 addCmdlineHCInclude a = 
    updateState (\s -> s{cmdline_hc_includes =  a : cmdline_hc_includes s})
@@ -95,7 +105,6 @@ cHaskell1Version = "5" -- i.e., Haskell 98
 
 -- location of compiler-related files
 GLOBAL_VAR(v_TopDir,  clibdir, String)
-GLOBAL_VAR(v_Inplace, False,   Bool)
 
 -- Cpp-related flags
 v_Hs_source_cpp_opts = global
@@ -106,13 +115,6 @@ v_Hs_source_cpp_opts = global
        ]
 {-# NOINLINE v_Hs_source_cpp_opts #-}
 
--- Verbose
-GLOBAL_VAR(v_Verbose, False, Bool)
-is_verbose = do v <- readIORef v_Verbose; if v then return "-v" else return ""
-
--- where to keep temporary files
-GLOBAL_VAR(v_TmpDir,       cDEFAULT_TMPDIR,  String   )
-
 -- Keep output from intermediate phases
 GLOBAL_VAR(v_Keep_hi_diffs,            False,          Bool)
 GLOBAL_VAR(v_Keep_hc_files,            False,          Bool)
@@ -128,6 +130,7 @@ GLOBAL_VAR(v_Static,                        True,           Bool)
 #else
 GLOBAL_VAR(v_Static,                   False,          Bool)
 #endif
+GLOBAL_VAR(v_NoHsMain,                         False,          Bool)
 GLOBAL_VAR(v_Recomp,                   True,           Bool)
 GLOBAL_VAR(v_Collect_ghc_timing,       False,          Bool)
 GLOBAL_VAR(v_Do_asm_mangling,          True,           Bool)
@@ -141,27 +144,26 @@ GLOBAL_VAR(v_Split_prefix,                "",             String)
 GLOBAL_VAR(v_N_split_files,            0,              Int)
        
 can_split :: Bool
-can_split =  prefixMatch "i386" cTARGETPLATFORM
-         || prefixMatch "alpha" cTARGETPLATFORM
-         || prefixMatch "hppa" cTARGETPLATFORM
-         || prefixMatch "m68k" cTARGETPLATFORM
-         || prefixMatch "mips" cTARGETPLATFORM
+can_split =  prefixMatch "i386"    cTARGETPLATFORM
+         || prefixMatch "alpha"   cTARGETPLATFORM
+         || prefixMatch "hppa"    cTARGETPLATFORM
+         || prefixMatch "m68k"    cTARGETPLATFORM
+         || prefixMatch "mips"    cTARGETPLATFORM
          || prefixMatch "powerpc" cTARGETPLATFORM
-         || prefixMatch "rs6000" cTARGETPLATFORM
-         || prefixMatch "sparc" cTARGETPLATFORM
+         || prefixMatch "rs6000"  cTARGETPLATFORM
+         || prefixMatch "sparc"   cTARGETPLATFORM
 
 -----------------------------------------------------------------------------
 -- Compiler output options
 
-GLOBAL_VAR(v_Hsc_Lang, if cGhcWithNativeCodeGen == "YES" && 
-                        (prefixMatch "i386" cTARGETPLATFORM ||
-                         prefixMatch "sparc" cTARGETPLATFORM)
-                       then  HscAsm
-                       else  HscC, 
-          HscLang)
+defaultHscLang
+  | cGhcWithNativeCodeGen == "YES" && 
+       (prefixMatch "i386" cTARGETPLATFORM ||
+        prefixMatch "sparc" cTARGETPLATFORM)   =  HscAsm
+  | otherwise                                  =  HscC
 
 GLOBAL_VAR(v_Output_dir,  Nothing, Maybe String)
-GLOBAL_VAR(v_Output_suf,  Nothing, Maybe String)
+GLOBAL_VAR(v_Object_suf,  Nothing, Maybe String)
 GLOBAL_VAR(v_Output_file, Nothing, Maybe String)
 GLOBAL_VAR(v_Output_hi,   Nothing, Maybe String)
 
@@ -176,7 +178,7 @@ odir_ify f = do
 
 osuf_ify :: String -> IO String
 osuf_ify f = do
-  osuf_opt <- readIORef v_Output_suf
+  osuf_opt <- readIORef v_Object_suf
   case osuf_opt of
        Nothing -> return f
        Just s  -> return (newsuf s f)
@@ -231,23 +233,16 @@ GLOBAL_VAR(v_Warning_opt, W_default, WarningState)
 GLOBAL_VAR(v_OptLevel, 0, Int)
 
 setOptLevel :: String -> IO ()
-setOptLevel ""             = do { writeIORef v_OptLevel 1; go_via_C }
+setOptLevel ""             = do { writeIORef v_OptLevel 1 }
 setOptLevel "not"          = writeIORef v_OptLevel 0
 setOptLevel [c] | isDigit c = do
    let level = ord c - ord '0'
    writeIORef v_OptLevel level
-   when (level >= 1) go_via_C
 setOptLevel s = unknownFlagErr ("-O"++s)
 
-go_via_C = do
-   l <- readIORef v_Hsc_Lang
-   case l of { HscAsm -> writeIORef v_Hsc_Lang HscC; 
-              _other -> return () }
-
-GLOBAL_VAR(v_minus_o2_for_C, False, Bool)
-
-GLOBAL_VAR(v_MaxSimplifierIterations, 4,     Int)
-GLOBAL_VAR(v_StgStats,                False, Bool)
+GLOBAL_VAR(v_minus_o2_for_C,            False, Bool)
+GLOBAL_VAR(v_MaxSimplifierIterations,   4,     Int)
+GLOBAL_VAR(v_StgStats,                  False, Bool)
 GLOBAL_VAR(v_UsageSPInf,               False, Bool)  -- Off by default
 GLOBAL_VAR(v_Strictness,               True,  Bool)
 GLOBAL_VAR(v_CPR,                      True,  Bool)
@@ -271,6 +266,10 @@ hsc_minusO_flags =
        "-flet-to-case"
    ]
 
+getStaticOptimisationFlags 0 = hsc_minusNoO_flags
+getStaticOptimisationFlags 1 = hsc_minusO_flags
+getStaticOptimisationFlags n = hsc_minusO2_flags
+
 buildCoreToDo :: IO [CoreToDo]
 buildCoreToDo = do
    opt_level  <- readIORef v_OptLevel
@@ -287,7 +286,7 @@ buildCoreToDo = do
        ])
       ]
 
-    else {- level >= 1 -} return [ 
+    else {- opt_level >= 1 -} return [ 
 
        -- initial simplify: mk specialiser happy: minimum effort please
        CoreDoSimplify (isAmongSimpl [
@@ -359,6 +358,7 @@ buildCoreToDo = do
                -- catch it.  For the record, the redex is 
                --        f_el22 (f_el21 r_midblock)
 
+
 -- Leave out lambda lifting for now
 --       "-fsimplify", -- Tidy up results of full laziness
 --         "[", 
@@ -368,12 +368,8 @@ buildCoreToDo = do
 
        -- We want CSE to follow the final full-laziness pass, because it may
        -- succeed in commoning up things floated out by full laziness.
-       --
-       -- CSE must immediately follow a simplification pass, because it relies
-       -- on the no-shadowing invariant.  See comments at the top of CSE.lhs
-       -- So it must NOT follow float-inwards, which can give rise to shadowing,
-       -- even if its input doesn't have shadows.  Hence putting it between
-       -- the two passes.
+       -- CSE used to rely on the no-shadowing invariant, but it doesn't any more
+
        if cse then CoreCSE else CoreDoNothing,
 
        CoreDoFloatInwards,
@@ -381,11 +377,10 @@ buildCoreToDo = do
 -- Case-liberation for -O2.  This should be after
 -- strictness analysis and the simplification which follows it.
 
---       ( ($OptLevel != 2)
---       ? ""
---       : "-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 ]" ),
---
---       "-fliberate-case",
+       if opt_level >= 2 then
+          CoreLiberateCase
+       else
+          CoreDoNothing,
 
        -- Final clean-up simplification:
        CoreDoSimplify (isAmongSimpl [
@@ -446,56 +441,53 @@ addPackage package
 
 getPackageImportPath   :: IO [String]
 getPackageImportPath = do
-  ps <- readIORef v_Packages
-  ps' <- getPackageDetails ps
-  return (nub (concat (map import_dirs ps')))
+  ps <- getPackageInfo
+  return (nub (concat (map import_dirs ps)))
 
 getPackageIncludePath   :: IO [String]
 getPackageIncludePath = do
-  ps <- readIORef v_Packages 
-  ps' <- getPackageDetails ps
-  return (nub (filter (not.null) (concatMap include_dirs ps')))
+  ps <- getPackageInfo
+  return (nub (filter (not.null) (concatMap include_dirs ps)))
 
        -- includes are in reverse dependency order (i.e. rts first)
 getPackageCIncludes   :: IO [String]
 getPackageCIncludes = do
-  ps <- readIORef v_Packages
-  ps' <- getPackageDetails ps
-  return (reverse (nub (filter (not.null) (concatMap c_includes ps'))))
+  ps <- getPackageInfo
+  return (reverse (nub (filter (not.null) (concatMap c_includes ps))))
 
 getPackageLibraryPath  :: IO [String]
 getPackageLibraryPath = do
-  ps <- readIORef v_Packages
-  ps' <- getPackageDetails ps
-  return (nub (concat (map library_dirs ps')))
+  ps <- getPackageInfo
+  return (nub (concat (map library_dirs ps)))
 
 getPackageLibraries    :: IO [String]
 getPackageLibraries = do
-  ps <- readIORef v_Packages
-  ps' <- getPackageDetails ps
+  ps <- getPackageInfo
   tag <- readIORef v_Build_tag
   let suffix = if null tag then "" else '_':tag
   return (concat (
-       map (\p -> map (++suffix) (hs_libraries p) ++ extra_libraries p) ps'
+       map (\p -> map (++suffix) (hs_libraries p) ++ extra_libraries p) ps
      ))
 
 getPackageExtraGhcOpts :: IO [String]
 getPackageExtraGhcOpts = do
-  ps <- readIORef v_Packages
-  ps' <- getPackageDetails ps
-  return (concatMap extra_ghc_opts ps')
+  ps <- getPackageInfo
+  return (concatMap extra_ghc_opts ps)
 
 getPackageExtraCcOpts  :: IO [String]
 getPackageExtraCcOpts = do
-  ps <- readIORef v_Packages
-  ps' <- getPackageDetails ps
-  return (concatMap extra_cc_opts ps')
+  ps <- getPackageInfo
+  return (concatMap extra_cc_opts ps)
 
 getPackageExtraLdOpts  :: IO [String]
 getPackageExtraLdOpts = do
+  ps <- getPackageInfo
+  return (concatMap extra_ld_opts ps)
+
+getPackageInfo :: IO [Package]
+getPackageInfo = do
   ps <- readIORef v_Packages
-  ps' <- getPackageDetails ps
-  return (concatMap extra_ld_opts ps')
+  getPackageDetails ps
 
 getPackageDetails :: [String] -> IO [Package]
 getPackageDetails ps = do
@@ -530,7 +522,6 @@ GLOBAL_VAR(v_Build_tag, "", String)
 data WayName
   = WayProf
   | WayUnreg
-  | WayDll
   | WayTicky
   | WayPar
   | WayGran
@@ -557,12 +548,10 @@ data WayName
 
 GLOBAL_VAR(v_Ways, [] ,[WayName])
 
--- ToDo: allow WayDll with any other allowed combination
-
-allowed_combinations = 
-   [  [WayProf,WayUnreg],
-      [WayProf,WaySMP]    -- works???
-   ]
+allowed_combination way = way `elem` combs
+  where  -- the sub-lists must be ordered according to WayName, 
+         -- because findBuildTag sorts them
+    combs                = [ [WayProf,WayUnreg], [WayProf,WaySMP] ]
 
 findBuildTag :: IO [String]  -- new options
 findBuildTag = do
@@ -575,7 +564,7 @@ findBuildTag = do
               writeIORef v_Build_tag (wayTag details)
               return (wayOpts details)
 
-     ws  -> if  ws `notElem` allowed_combinations
+     ws  -> if not (allowed_combination ws)
                then throwDyn (OtherError $
                                "combination not supported: "  ++
                                foldr1 (\a b -> a ++ '/':b) 
@@ -619,9 +608,6 @@ way_details =
        , "-funregisterised"
        , "-fvia-C" ]),
 
-    (WayDll, Way  "dll" "DLLized"
-        [ ]),
-
     (WayPar, Way  "mp" "Parallel" 
        [ "-fparallel"
        , "-D__PARALLEL_HASKELL__"
@@ -717,7 +703,8 @@ machdepCCOpts
       --   the fp (%ebp) for our register maps.
        = do n_regs <- readState stolen_x86_regs
             sta    <- readIORef v_Static
-            return ( [ if sta then "-DDONT_WANT_WIN32_DLL_SUPPORT" else "" ],
+            return ( [ if sta then "-DDONT_WANT_WIN32_DLL_SUPPORT" else "",
+                        if suffixMatch "mingw32" cTARGETPLATFORM then "-mno-cygwin" else "" ],
                      [ "-fno-defer-pop", "-fomit-frame-pointer",
                        "-DSTOLEN_X86_REGS="++show n_regs ]
                    )
@@ -730,40 +717,3 @@ machdepCCOpts
 
    | otherwise
        = return ( [], [] )
-
-
------------------------------------------------------------------------------
--- Running an external program
-
-run_something phase_name cmd
- = do
-   verb <- readIORef v_Verbose
-   when verb $ do
-       putStr phase_name
-       putStrLn ":"
-       putStrLn cmd
-       hFlush stdout
-
-   -- test for -n flag
-   n <- readIORef v_Dry_run
-   unless n $ do 
-
-   -- and run it!
-#ifndef mingw32_TARGET_OS
-   exit_code <- system cmd `catchAllIO` 
-                  (\_ -> throwDyn (PhaseFailed phase_name (ExitFailure 1)))
-#else
-   tmp <- newTempName "sh"
-   h <- openFile tmp WriteMode
-   hPutStrLn h cmd
-   hClose h
-   exit_code <- system ("sh - " ++ tmp) `catchAllIO` 
-                  (\e -> throwDyn (PhaseFailed phase_name (ExitFailure 1)))
-   removeFile tmp
-#endif
-
-   if exit_code /= ExitSuccess
-       then throwDyn (PhaseFailed phase_name exit_code)
-       else do when verb (putStr "\n")
-               return ()
-