module GHC.Driver.Config.CmmToLlvm
  ( initLlvmCgConfig
  )
where

import GHC.Prelude
import GHC.Driver.Session
import GHC.Driver.LlvmConfigCache
import GHC.Platform
import GHC.CmmToLlvm.Config
import GHC.SysTools.Tasks

import GHC.Utils.Outputable
import GHC.Utils.Logger

-- | Initialize the Llvm code generator configuration from DynFlags
initLlvmCgConfig :: Logger -> LlvmConfigCache -> DynFlags -> IO LlvmCgConfig
initLlvmCgConfig :: Logger -> LlvmConfigCache -> DynFlags -> IO LlvmCgConfig
initLlvmCgConfig Logger
logger LlvmConfigCache
config_cache DynFlags
dflags = do
  Maybe LlvmVersion
version <- Logger -> DynFlags -> IO (Maybe LlvmVersion)
figureLlvmVersion Logger
logger DynFlags
dflags
  LlvmConfig
llvm_config <- LlvmConfigCache -> IO LlvmConfig
readLlvmConfigCache LlvmConfigCache
config_cache
  LlvmCgConfig -> IO LlvmCgConfig
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LlvmCgConfig -> IO LlvmCgConfig)
-> LlvmCgConfig -> IO LlvmCgConfig
forall a b. (a -> b) -> a -> b
$! LlvmCgConfig {
    llvmCgPlatform :: Platform
llvmCgPlatform               = DynFlags -> Platform
targetPlatform DynFlags
dflags
    , llvmCgContext :: SDocContext
llvmCgContext              = DynFlags -> PprStyle -> SDocContext
initSDocContext DynFlags
dflags PprStyle
PprCode
    , llvmCgFillUndefWithGarbage :: Bool
llvmCgFillUndefWithGarbage = GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_LlvmFillUndefWithGarbage DynFlags
dflags
    , llvmCgSplitSection :: Bool
llvmCgSplitSection         = GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_SplitSections DynFlags
dflags
    , llvmCgBmiVersion :: Maybe BmiVersion
llvmCgBmiVersion           = case Platform -> Arch
platformArch (DynFlags -> Platform
targetPlatform DynFlags
dflags) of
                                      Arch
ArchX86_64 -> DynFlags -> Maybe BmiVersion
bmiVersion DynFlags
dflags
                                      Arch
ArchX86    -> DynFlags -> Maybe BmiVersion
bmiVersion DynFlags
dflags
                                      Arch
_          -> Maybe BmiVersion
forall a. Maybe a
Nothing
    , llvmCgLlvmVersion :: Maybe LlvmVersion
llvmCgLlvmVersion          = Maybe LlvmVersion
version
    , llvmCgDoWarn :: Bool
llvmCgDoWarn               = WarningFlag -> DynFlags -> Bool
wopt WarningFlag
Opt_WarnUnsupportedLlvmVersion DynFlags
dflags
    , llvmCgLlvmTarget :: String
llvmCgLlvmTarget           = PlatformMisc -> String
platformMisc_llvmTarget (PlatformMisc -> String) -> PlatformMisc -> String
forall a b. (a -> b) -> a -> b
$! DynFlags -> PlatformMisc
platformMisc DynFlags
dflags
    , llvmCgLlvmConfig :: LlvmConfig
llvmCgLlvmConfig           = LlvmConfig
llvm_config
    }