-- | Subsystem configuration
module GHC.Driver.Config
   ( initOptCoercionOpts
   , initSimpleOpts
   , initBCOOpts
   , initEvalOpts
   )
where

import GHC.Prelude

import GHC.Driver.Session
import GHC.Core.SimpleOpt
import GHC.Core.Coercion.Opt
import GHC.Runtime.Interpreter (BCOOpts(..))
import GHCi.Message (EvalOpts(..))

import GHC.Conc (getNumProcessors)
import Control.Monad.IO.Class

-- | Initialise coercion optimiser configuration from DynFlags
initOptCoercionOpts :: DynFlags -> OptCoercionOpts
initOptCoercionOpts :: DynFlags -> OptCoercionOpts
initOptCoercionOpts DynFlags
dflags = OptCoercionOpts
   { optCoercionEnabled :: Bool
optCoercionEnabled = Bool -> Bool
not (DynFlags -> Bool
hasNoOptCoercion DynFlags
dflags)
   }

-- | Initialise Simple optimiser configuration from DynFlags
initSimpleOpts :: DynFlags -> SimpleOpts
initSimpleOpts :: DynFlags -> SimpleOpts
initSimpleOpts DynFlags
dflags = SimpleOpts
   { so_uf_opts :: UnfoldingOpts
so_uf_opts = DynFlags -> UnfoldingOpts
unfoldingOpts DynFlags
dflags
   , so_co_opts :: OptCoercionOpts
so_co_opts = DynFlags -> OptCoercionOpts
initOptCoercionOpts DynFlags
dflags
   }

-- | Extract BCO options from DynFlags
initBCOOpts :: DynFlags -> IO BCOOpts
initBCOOpts :: DynFlags -> IO BCOOpts
initBCOOpts DynFlags
dflags = do
  -- Serializing ResolvedBCO is expensive, so if we're in parallel mode
  -- (-j<n>) parallelise the serialization.
  Int
n_jobs <- case DynFlags -> Maybe Int
parMakeCount DynFlags
dflags of
              Maybe Int
Nothing -> IO Int -> IO Int
forall a. IO a -> IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO Int
getNumProcessors
              Just Int
n  -> Int -> IO Int
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Int
n
  BCOOpts -> IO BCOOpts
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (BCOOpts -> IO BCOOpts) -> BCOOpts -> IO BCOOpts
forall a b. (a -> b) -> a -> b
$ Int -> BCOOpts
BCOOpts Int
n_jobs

-- | Extract GHCi options from DynFlags and step
initEvalOpts :: DynFlags -> Bool -> EvalOpts
initEvalOpts :: DynFlags -> Bool -> EvalOpts
initEvalOpts DynFlags
dflags Bool
step =
  EvalOpts
    { useSandboxThread :: Bool
useSandboxThread = GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_GhciSandbox DynFlags
dflags
    , singleStep :: Bool
singleStep       = Bool
step
    , breakOnException :: Bool
breakOnException = GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_BreakOnException DynFlags
dflags
    , breakOnError :: Bool
breakOnError     = GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_BreakOnError DynFlags
dflags
    }