{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
module GHC.StgToCmm ( codeGen ) where
#include "HsVersions.h"
import GHC.Prelude as Prelude
import GHC.StgToCmm.Prof (initCostCentres, ldvEnter)
import GHC.StgToCmm.Monad
import GHC.StgToCmm.Env
import GHC.StgToCmm.Bind
import GHC.StgToCmm.DataCon
import GHC.StgToCmm.Layout
import GHC.StgToCmm.Utils
import GHC.StgToCmm.Closure
import GHC.StgToCmm.Hpc
import GHC.StgToCmm.Ticky
import GHC.Cmm
import GHC.Cmm.Utils
import GHC.Cmm.CLabel
import GHC.Stg.Syntax
import GHC.Driver.Session
import GHC.Utils.Error
import GHC.Driver.Types
import GHC.Types.CostCentre
import GHC.Types.Id
import GHC.Types.Id.Info
import GHC.Types.RepType
import GHC.Core.DataCon
import GHC.Core.TyCon
import GHC.Unit.Module
import GHC.Utils.Outputable
import GHC.Data.Stream
import GHC.Types.Basic
import GHC.Types.Var.Set ( isEmptyDVarSet )
import GHC.SysTools.FileCleanup
import GHC.Data.OrdList
import GHC.Cmm.Graph
import Data.IORef
import Control.Monad (when,void)
import GHC.Utils.Misc
import System.IO.Unsafe
import qualified Data.ByteString as BS
codeGen :: DynFlags
-> Module
-> [TyCon]
-> CollectedCCs
-> [CgStgTopBinding]
-> HpcInfo
-> Stream IO CmmGroup ()
codeGen :: DynFlags
-> Module
-> [TyCon]
-> CollectedCCs
-> [CgStgTopBinding]
-> HpcInfo
-> Stream IO CmmGroup ()
codeGen DynFlags
dflags Module
this_mod [TyCon]
data_tycons
CollectedCCs
cost_centre_info [CgStgTopBinding]
stg_binds HpcInfo
hpc_info
= do {
; IORef CgState
cgref <- IO (IORef CgState) -> Stream IO CmmGroup (IORef CgState)
forall a b. IO a -> Stream IO b a
liftIO (IO (IORef CgState) -> Stream IO CmmGroup (IORef CgState))
-> IO (IORef CgState) -> Stream IO CmmGroup (IORef CgState)
forall a b. (a -> b) -> a -> b
$ CgState -> IO (IORef CgState)
forall a. a -> IO (IORef a)
newIORef (CgState -> IO (IORef CgState)) -> IO CgState -> IO (IORef CgState)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
External instance of the constraint type Monad IO
=<< IO CgState
initC
; let cg :: FCode () -> Stream IO CmmGroup ()
cg :: FCode () -> Stream IO CmmGroup ()
cg FCode ()
fcode = do
CmmGroup
cmm <- IO CmmGroup -> Stream IO CmmGroup CmmGroup
forall a b. IO a -> Stream IO b a
liftIO (IO CmmGroup -> Stream IO CmmGroup CmmGroup)
-> (IO CmmGroup -> IO CmmGroup)
-> IO CmmGroup
-> Stream IO CmmGroup CmmGroup
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynFlags -> SDoc -> (CmmGroup -> ()) -> IO CmmGroup -> IO CmmGroup
forall (m :: * -> *) a.
MonadIO m =>
DynFlags -> SDoc -> (a -> ()) -> m a -> m a
External instance of the constraint type MonadIO IO
withTimingSilent DynFlags
dflags (String -> SDoc
text String
"STG -> Cmm") (CmmGroup -> () -> ()
`seq` ()) (IO CmmGroup -> Stream IO CmmGroup CmmGroup)
-> IO CmmGroup -> Stream IO CmmGroup CmmGroup
forall a b. (a -> b) -> a -> b
$ do
CgState
st <- IORef CgState -> IO CgState
forall a. IORef a -> IO a
readIORef IORef CgState
cgref
let (CmmGroup
a,CgState
st') = DynFlags
-> Module -> CgState -> FCode CmmGroup -> (CmmGroup, CgState)
forall a. DynFlags -> Module -> CgState -> FCode a -> (a, CgState)
runC DynFlags
dflags Module
this_mod CgState
st (FCode () -> FCode CmmGroup
getCmm FCode ()
fcode)
IORef CgState -> CgState -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef CgState
cgref (CgState -> IO ()) -> CgState -> IO ()
forall a b. (a -> b) -> a -> b
$! CgState
st'{ cgs_tops :: OrdList CmmDecl
cgs_tops = OrdList CmmDecl
forall a. OrdList a
nilOL,
cgs_stmts :: CmmAGraph
cgs_stmts = CmmAGraph
mkNop }
CmmGroup -> IO CmmGroup
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad IO
return CmmGroup
a
CmmGroup -> Stream IO CmmGroup ()
forall (m :: * -> *) a. Monad m => a -> Stream m a ()
External instance of the constraint type Monad IO
yield CmmGroup
cmm
; FCode () -> Stream IO CmmGroup ()
cg (CollectedCCs -> Module -> HpcInfo -> FCode ()
mkModuleInit CollectedCCs
cost_centre_info Module
this_mod HpcInfo
hpc_info)
; (CgStgTopBinding -> Stream IO CmmGroup ())
-> [CgStgTopBinding] -> Stream IO CmmGroup ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
External instance of the constraint type forall (m :: * -> *) a. Monad m => Monad (Stream m a)
External instance of the constraint type Monad IO
External instance of the constraint type Foldable []
mapM_ (FCode () -> Stream IO CmmGroup ()
cg (FCode () -> Stream IO CmmGroup ())
-> (CgStgTopBinding -> FCode ())
-> CgStgTopBinding
-> Stream IO CmmGroup ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynFlags -> CgStgTopBinding -> FCode ()
cgTopBinding DynFlags
dflags) [CgStgTopBinding]
stg_binds
; let do_tycon :: TyCon -> Stream IO CmmGroup ()
do_tycon TyCon
tycon = do
Bool -> Stream IO CmmGroup () -> Stream IO CmmGroup ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
External instance of the constraint type forall (m :: * -> *) a. Monad m => Applicative (Stream m a)
External instance of the constraint type Monad IO
when (TyCon -> Bool
isEnumerationTyCon TyCon
tycon) (Stream IO CmmGroup () -> Stream IO CmmGroup ())
-> Stream IO CmmGroup () -> Stream IO CmmGroup ()
forall a b. (a -> b) -> a -> b
$ FCode () -> Stream IO CmmGroup ()
cg (TyCon -> FCode ()
cgEnumerationTyCon TyCon
tycon)
(DataCon -> Stream IO CmmGroup ())
-> [DataCon] -> Stream IO CmmGroup ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
External instance of the constraint type forall (m :: * -> *) a. Monad m => Monad (Stream m a)
External instance of the constraint type Monad IO
External instance of the constraint type Foldable []
mapM_ (FCode () -> Stream IO CmmGroup ()
cg (FCode () -> Stream IO CmmGroup ())
-> (DataCon -> FCode ()) -> DataCon -> Stream IO CmmGroup ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DataCon -> FCode ()
cgDataCon) (TyCon -> [DataCon]
tyConDataCons TyCon
tycon)
; (TyCon -> Stream IO CmmGroup ())
-> [TyCon] -> Stream IO CmmGroup ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
External instance of the constraint type forall (m :: * -> *) a. Monad m => Monad (Stream m a)
External instance of the constraint type Monad IO
External instance of the constraint type Foldable []
mapM_ TyCon -> Stream IO CmmGroup ()
do_tycon [TyCon]
data_tycons
}
cgTopBinding :: DynFlags -> CgStgTopBinding -> FCode ()
cgTopBinding :: DynFlags -> CgStgTopBinding -> FCode ()
cgTopBinding DynFlags
dflags (StgTopLifted (StgNonRec BinderP 'CodeGen
id GenStgRhs 'CodeGen
rhs))
= do { let (CgIdInfo
info, FCode ()
fcode) = DynFlags
-> RecFlag -> Id -> GenStgRhs 'CodeGen -> (CgIdInfo, FCode ())
cgTopRhs DynFlags
dflags RecFlag
NonRecursive Id
BinderP 'CodeGen
id GenStgRhs 'CodeGen
rhs
; FCode ()
fcode
; CgIdInfo -> FCode ()
addBindC CgIdInfo
info
}
cgTopBinding DynFlags
dflags (StgTopLifted (StgRec [(BinderP 'CodeGen, GenStgRhs 'CodeGen)]
pairs))
= do { let ([Id]
bndrs, [GenStgRhs 'CodeGen]
rhss) = [(Id, GenStgRhs 'CodeGen)] -> ([Id], [GenStgRhs 'CodeGen])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Id, GenStgRhs 'CodeGen)]
[(BinderP 'CodeGen, GenStgRhs 'CodeGen)]
pairs
; let pairs' :: [(Id, GenStgRhs 'CodeGen)]
pairs' = [Id] -> [GenStgRhs 'CodeGen] -> [(Id, GenStgRhs 'CodeGen)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Id]
bndrs [GenStgRhs 'CodeGen]
rhss
r :: [(CgIdInfo, FCode ())]
r = (Id -> GenStgRhs 'CodeGen -> (CgIdInfo, FCode ()))
-> [(Id, GenStgRhs 'CodeGen)] -> [(CgIdInfo, FCode ())]
forall a b c. (a -> b -> c) -> [(a, b)] -> [c]
unzipWith (DynFlags
-> RecFlag -> Id -> GenStgRhs 'CodeGen -> (CgIdInfo, FCode ())
cgTopRhs DynFlags
dflags RecFlag
Recursive) [(Id, GenStgRhs 'CodeGen)]
pairs'
([CgIdInfo]
infos, [FCode ()]
fcodes) = [(CgIdInfo, FCode ())] -> ([CgIdInfo], [FCode ()])
forall a b. [(a, b)] -> ([a], [b])
unzip [(CgIdInfo, FCode ())]
r
; [CgIdInfo] -> FCode ()
addBindsC [CgIdInfo]
infos
; [FCode ()] -> FCode ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
External instance of the constraint type Monad FCode
External instance of the constraint type Foldable []
sequence_ [FCode ()]
fcodes
}
cgTopBinding DynFlags
dflags (StgTopStringLit Id
id ByteString
str) = do
let label :: CLabel
label = Name -> CLabel
mkBytesLabel (Id -> Name
idName Id
id)
let isNCG :: Bool
isNCG = PlatformMisc -> Bool
platformMisc_ghcWithNativeCodeGen (PlatformMisc -> Bool) -> PlatformMisc -> Bool
forall a b. (a -> b) -> a -> b
$ DynFlags -> PlatformMisc
platformMisc DynFlags
dflags
isSmall :: Bool
isSmall = Int -> Word
forall a b. (Integral a, Num b) => a -> b
External instance of the constraint type Num Word
External instance of the constraint type Integral Int
fromIntegral (ByteString -> Int
BS.length ByteString
str) Word -> Word -> Bool
forall a. Ord a => a -> a -> Bool
External instance of the constraint type Ord Word
<= DynFlags -> Word
binBlobThreshold DynFlags
dflags
asString :: Bool
asString = DynFlags -> Word
binBlobThreshold DynFlags
dflags Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Word
== Word
0 Bool -> Bool -> Bool
|| Bool
isSmall
(CmmLit
lit,GenCmmDecl (GenCmmStatics raw) info stmt
decl) = if Bool -> Bool
not Bool
isNCG Bool -> Bool -> Bool
|| Bool
asString
then CLabel
-> ByteString -> (CmmLit, GenCmmDecl (GenCmmStatics raw) info stmt)
forall (raw :: Bool) info stmt.
CLabel
-> ByteString -> (CmmLit, GenCmmDecl (GenCmmStatics raw) info stmt)
mkByteStringCLit CLabel
label ByteString
str
else CLabel
-> String -> (CmmLit, GenCmmDecl (GenCmmStatics raw) info stmt)
forall (raw :: Bool) info stmt.
CLabel
-> String -> (CmmLit, GenCmmDecl (GenCmmStatics raw) info stmt)
mkFileEmbedLit CLabel
label (String -> (CmmLit, GenCmmDecl (GenCmmStatics raw) info stmt))
-> String -> (CmmLit, GenCmmDecl (GenCmmStatics raw) info stmt)
forall a b. (a -> b) -> a -> b
$ IO String -> String
forall a. IO a -> a
unsafePerformIO (IO String -> String) -> IO String -> String
forall a b. (a -> b) -> a -> b
$ do
String
bFile <- DynFlags -> TempFileLifetime -> String -> IO String
newTempName DynFlags
dflags TempFileLifetime
TFL_CurrentModule String
".dat"
String -> ByteString -> IO ()
BS.writeFile String
bFile ByteString
str
String -> IO String
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad IO
return String
bFile
CmmDecl -> FCode ()
emitDecl CmmDecl
forall {raw :: Bool} {info} {stmt}.
GenCmmDecl (GenCmmStatics raw) info stmt
decl
CgIdInfo -> FCode ()
addBindC (DynFlags -> Id -> LambdaFormInfo -> CmmLit -> CgIdInfo
litIdInfo DynFlags
dflags Id
id LambdaFormInfo
mkLFStringLit CmmLit
lit)
cgTopRhs :: DynFlags -> RecFlag -> Id -> CgStgRhs -> (CgIdInfo, FCode ())
cgTopRhs :: DynFlags
-> RecFlag -> Id -> GenStgRhs 'CodeGen -> (CgIdInfo, FCode ())
cgTopRhs DynFlags
dflags RecFlag
_rec Id
bndr (StgRhsCon CostCentreStack
_cc DataCon
con [StgArg]
args)
= DynFlags
-> Id -> DataCon -> [NonVoid StgArg] -> (CgIdInfo, FCode ())
cgTopRhsCon DynFlags
dflags Id
bndr DataCon
con ([StgArg] -> [NonVoid StgArg]
assertNonVoidStgArgs [StgArg]
args)
cgTopRhs DynFlags
dflags RecFlag
rec Id
bndr (StgRhsClosure XRhsClosure 'CodeGen
fvs CostCentreStack
cc UpdateFlag
upd_flag [BinderP 'CodeGen]
args GenStgExpr 'CodeGen
body)
= ASSERT(isEmptyDVarSet fvs)
DynFlags
-> RecFlag
-> Id
-> CostCentreStack
-> UpdateFlag
-> [Id]
-> GenStgExpr 'CodeGen
-> (CgIdInfo, FCode ())
cgTopRhsClosure DynFlags
dflags RecFlag
rec Id
bndr CostCentreStack
cc UpdateFlag
upd_flag [Id]
[BinderP 'CodeGen]
args GenStgExpr 'CodeGen
body
mkModuleInit
:: CollectedCCs
-> Module
-> HpcInfo
-> FCode ()
mkModuleInit :: CollectedCCs -> Module -> HpcInfo -> FCode ()
mkModuleInit CollectedCCs
cost_centre_info Module
this_mod HpcInfo
hpc_info
= do { Module -> HpcInfo -> FCode ()
initHpc Module
this_mod HpcInfo
hpc_info
; CollectedCCs -> FCode ()
initCostCentres CollectedCCs
cost_centre_info
}
cgEnumerationTyCon :: TyCon -> FCode ()
cgEnumerationTyCon :: TyCon -> FCode ()
cgEnumerationTyCon TyCon
tycon
= do DynFlags
dflags <- FCode DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
External instance of the constraint type HasDynFlags FCode
getDynFlags
CLabel -> [CmmLit] -> FCode ()
emitRODataLits (Name -> CafInfo -> CLabel
mkLocalClosureTableLabel (TyCon -> Name
tyConName TyCon
tycon) CafInfo
NoCafRefs)
[ CLabel -> Int -> CmmLit
CmmLabelOff (Name -> CafInfo -> CLabel
mkLocalClosureLabel (DataCon -> Name
dataConName DataCon
con) CafInfo
NoCafRefs)
(DynFlags -> DataCon -> Int
tagForCon DynFlags
dflags DataCon
con)
| DataCon
con <- TyCon -> [DataCon]
tyConDataCons TyCon
tycon]
cgDataCon :: DataCon -> FCode ()
cgDataCon :: DataCon -> FCode ()
cgDataCon DataCon
data_con
= do { DynFlags
dflags <- FCode DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
External instance of the constraint type HasDynFlags FCode
getDynFlags
; Platform
platform <- FCode Platform
getPlatform
; let
(Int
tot_wds,
Int
ptr_wds)
= DynFlags -> [NonVoid PrimRep] -> (Int, Int)
mkVirtConstrSizes DynFlags
dflags [NonVoid PrimRep]
arg_reps
nonptr_wds :: Int
nonptr_wds = Int
tot_wds Int -> Int -> Int
forall a. Num a => a -> a -> a
External instance of the constraint type Num Int
- Int
ptr_wds
dyn_info_tbl :: CmmInfoTable
dyn_info_tbl =
DynFlags -> DataCon -> Bool -> Int -> Int -> CmmInfoTable
mkDataConInfoTable DynFlags
dflags DataCon
data_con Bool
False Int
ptr_wds Int
nonptr_wds
arg_reps :: [NonVoid PrimRep]
arg_reps :: [NonVoid PrimRep]
arg_reps = [ PrimRep -> NonVoid PrimRep
forall a. a -> NonVoid a
NonVoid PrimRep
rep_ty
| Type
ty <- DataCon -> [Type]
dataConRepArgTys DataCon
data_con
, PrimRep
rep_ty <- HasDebugCallStack => Type -> [PrimRep]
Type -> [PrimRep]
External instance of the constraint type HasDebugCallStack
typePrimRep Type
ty
, Bool -> Bool
not (PrimRep -> Bool
isVoidRep PrimRep
rep_ty) ]
; CmmInfoTable -> Convention -> [LocalReg] -> FCode () -> FCode ()
emitClosureAndInfoTable CmmInfoTable
dyn_info_tbl Convention
NativeDirectCall [] (FCode () -> FCode ()) -> FCode () -> FCode ()
forall a b. (a -> b) -> a -> b
$
do { FCode ()
tickyEnterDynCon
; CmmExpr -> FCode ()
ldvEnter (CmmReg -> CmmExpr
CmmReg CmmReg
nodeReg)
; Int -> FCode ()
tickyReturnOldCon ([NonVoid PrimRep] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
External instance of the constraint type Foldable []
length [NonVoid PrimRep]
arg_reps)
; FCode ReturnKind -> FCode ()
forall (f :: * -> *) a. Functor f => f a -> f ()
External instance of the constraint type Functor FCode
void (FCode ReturnKind -> FCode ()) -> FCode ReturnKind -> FCode ()
forall a b. (a -> b) -> a -> b
$ [CmmExpr] -> FCode ReturnKind
emitReturn [Platform -> CmmExpr -> Int -> CmmExpr
cmmOffsetB Platform
platform (CmmReg -> CmmExpr
CmmReg CmmReg
nodeReg) (DynFlags -> DataCon -> Int
tagForCon DynFlags
dflags DataCon
data_con)]
}
}