{-# LANGUAGE CPP #-}
module GHC.StgToCmm.DataCon (
cgTopRhsCon, buildDynCon, bindConArgs
) where
#include "HsVersions.h"
import GHC.Prelude
import GHC.Stg.Syntax
import GHC.Core ( AltCon(..) )
import GHC.StgToCmm.Monad
import GHC.StgToCmm.Env
import GHC.StgToCmm.Heap
import GHC.StgToCmm.Layout
import GHC.StgToCmm.Utils
import GHC.StgToCmm.Closure
import GHC.Cmm.Expr
import GHC.Cmm.Utils
import GHC.Cmm.CLabel
import GHC.Cmm.Graph
import GHC.Runtime.Heap.Layout
import GHC.Types.CostCentre
import GHC.Unit
import GHC.Core.DataCon
import GHC.Driver.Session
import GHC.Data.FastString
import GHC.Types.Id
import GHC.Types.Id.Info( CafInfo( NoCafRefs ) )
import GHC.Types.Name (isInternalName)
import GHC.Types.RepType (countConRepArgs)
import GHC.Types.Literal
import GHC.Builtin.Utils
import GHC.Utils.Outputable
import GHC.Platform
import GHC.Utils.Misc
import GHC.Utils.Monad (mapMaybeM)
import Control.Monad
import Data.Char
cgTopRhsCon :: DynFlags
-> Id
-> DataCon
-> [NonVoid StgArg]
-> (CgIdInfo, FCode ())
cgTopRhsCon :: DynFlags
-> Id -> DataCon -> [NonVoid StgArg] -> (CgIdInfo, FCode ())
cgTopRhsCon DynFlags
dflags Id
id DataCon
con [NonVoid StgArg]
args
| Just CgIdInfo
static_info <- DynFlags -> Id -> DataCon -> [NonVoid StgArg] -> Maybe CgIdInfo
precomputedStaticConInfo_maybe DynFlags
dflags Id
id DataCon
con [NonVoid StgArg]
args
, let static_code :: FCode ()
static_code | Name -> Bool
isInternalName Name
name = () -> FCode ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Bool
otherwise = FCode ()
gen_code
=
(CgIdInfo
static_info, FCode ()
static_code)
| Bool
otherwise
= (CgIdInfo
id_Info, FCode ()
gen_code)
where
id_Info :: CgIdInfo
id_Info = DynFlags -> Id -> LambdaFormInfo -> CmmLit -> CgIdInfo
litIdInfo DynFlags
dflags Id
id (DataCon -> LambdaFormInfo
mkConLFInfo DataCon
con) (CLabel -> CmmLit
CmmLabel CLabel
closure_label)
name :: Name
name = Id -> Name
idName Id
id
caffy :: CafInfo
caffy = Id -> CafInfo
idCafInfo Id
id
closure_label :: CLabel
closure_label = Name -> CafInfo -> CLabel
mkClosureLabel Name
name CafInfo
caffy
gen_code :: FCode ()
gen_code =
do { Module
this_mod <- FCode Module
getModuleName
; Bool -> FCode () -> FCode ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Platform -> OS
platformOS (DynFlags -> Platform
targetPlatform DynFlags
dflags) OS -> OS -> Bool
forall a. Eq a => a -> a -> Bool
== OS
OSMinGW32) (FCode () -> FCode ()) -> FCode () -> FCode ()
forall a b. (a -> b) -> a -> b
$
MASSERT( not (isDllConApp dflags this_mod con (map fromNonVoid args)) )
; ASSERT( args `lengthIs` countConRepArgs con ) return ()
; let
(Int
tot_wds,
Int
ptr_wds,
[FieldOffOrPadding StgArg]
nv_args_w_offsets) =
DynFlags
-> ClosureHeader
-> [NonVoid (PrimRep, StgArg)]
-> (Int, Int, [FieldOffOrPadding StgArg])
forall a.
DynFlags
-> ClosureHeader
-> [NonVoid (PrimRep, a)]
-> (Int, Int, [FieldOffOrPadding a])
mkVirtHeapOffsetsWithPadding DynFlags
dflags ClosureHeader
StdHeader ([NonVoid StgArg] -> [NonVoid (PrimRep, StgArg)]
addArgReps [NonVoid StgArg]
args)
mk_payload :: FieldOffOrPadding StgArg -> FCode CmmLit
mk_payload (Padding Int
len Int
_) = CmmLit -> FCode CmmLit
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> Width -> CmmLit
CmmInt Integer
0 (Int -> Width
widthFromBytes Int
len))
mk_payload (FieldOff NonVoid StgArg
arg Int
_) = do
CmmExpr
amode <- NonVoid StgArg -> FCode CmmExpr
getArgAmode NonVoid StgArg
arg
case CmmExpr
amode of
CmmLit CmmLit
lit -> CmmLit -> FCode CmmLit
forall (m :: * -> *) a. Monad m => a -> m a
return CmmLit
lit
CmmExpr
_ -> String -> FCode CmmLit
forall a. String -> a
panic String
"GHC.StgToCmm.DataCon.cgTopRhsCon"
nonptr_wds :: Int
nonptr_wds = Int
tot_wds Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
ptr_wds
info_tbl :: CmmInfoTable
info_tbl = DynFlags -> DataCon -> Bool -> Int -> Int -> CmmInfoTable
mkDataConInfoTable DynFlags
dflags DataCon
con Bool
True Int
ptr_wds Int
nonptr_wds
; [CmmLit]
payload <- (FieldOffOrPadding StgArg -> FCode CmmLit)
-> [FieldOffOrPadding StgArg] -> FCode [CmmLit]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM FieldOffOrPadding StgArg -> FCode CmmLit
mk_payload [FieldOffOrPadding StgArg]
nv_args_w_offsets
; CLabel -> CmmInfoTable -> CostCentreStack -> [CmmLit] -> FCode ()
emitDataCon CLabel
closure_label CmmInfoTable
info_tbl CostCentreStack
dontCareCCS [CmmLit]
payload }
buildDynCon :: Id
-> Bool
-> CostCentreStack
-> DataCon
-> [NonVoid StgArg]
-> FCode (CgIdInfo, FCode CmmAGraph)
buildDynCon :: Id
-> Bool
-> CostCentreStack
-> DataCon
-> [NonVoid StgArg]
-> FCode (CgIdInfo, FCode CmmAGraph)
buildDynCon Id
binder Bool
actually_bound CostCentreStack
cc DataCon
con [NonVoid StgArg]
args
= do DynFlags
dflags <- FCode DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
DynFlags
-> Id
-> Bool
-> CostCentreStack
-> DataCon
-> [NonVoid StgArg]
-> FCode (CgIdInfo, FCode CmmAGraph)
buildDynCon' DynFlags
dflags Id
binder Bool
actually_bound CostCentreStack
cc DataCon
con [NonVoid StgArg]
args
buildDynCon' :: DynFlags
-> Id -> Bool
-> CostCentreStack
-> DataCon
-> [NonVoid StgArg]
-> FCode (CgIdInfo, FCode CmmAGraph)
buildDynCon' :: DynFlags
-> Id
-> Bool
-> CostCentreStack
-> DataCon
-> [NonVoid StgArg]
-> FCode (CgIdInfo, FCode CmmAGraph)
buildDynCon' DynFlags
dflags Id
binder Bool
_ CostCentreStack
_cc DataCon
con [NonVoid StgArg]
args
| Just CgIdInfo
cgInfo <- DynFlags -> Id -> DataCon -> [NonVoid StgArg] -> Maybe CgIdInfo
precomputedStaticConInfo_maybe DynFlags
dflags Id
binder DataCon
con [NonVoid StgArg]
args
= (CgIdInfo, FCode CmmAGraph) -> FCode (CgIdInfo, FCode CmmAGraph)
forall (m :: * -> *) a. Monad m => a -> m a
return (CgIdInfo
cgInfo, CmmAGraph -> FCode CmmAGraph
forall (m :: * -> *) a. Monad m => a -> m a
return CmmAGraph
mkNop)
buildDynCon' DynFlags
dflags Id
binder Bool
actually_bound CostCentreStack
ccs DataCon
con [NonVoid StgArg]
args
= do { (CgIdInfo
id_info, LocalReg
reg) <- Id -> LambdaFormInfo -> FCode (CgIdInfo, LocalReg)
rhsIdInfo Id
binder LambdaFormInfo
lf_info
; (CgIdInfo, FCode CmmAGraph) -> FCode (CgIdInfo, FCode CmmAGraph)
forall (m :: * -> *) a. Monad m => a -> m a
return (CgIdInfo
id_info, LocalReg -> FCode CmmAGraph
gen_code LocalReg
reg)
}
where
lf_info :: LambdaFormInfo
lf_info = DataCon -> LambdaFormInfo
mkConLFInfo DataCon
con
gen_code :: LocalReg -> FCode CmmAGraph
gen_code LocalReg
reg
= do { let (Int
tot_wds, Int
ptr_wds, [(NonVoid StgArg, Int)]
args_w_offsets)
= DynFlags
-> [NonVoid (PrimRep, StgArg)]
-> (Int, Int, [(NonVoid StgArg, Int)])
forall a.
DynFlags
-> [NonVoid (PrimRep, a)] -> (Int, Int, [(NonVoid a, Int)])
mkVirtConstrOffsets DynFlags
dflags ([NonVoid StgArg] -> [NonVoid (PrimRep, StgArg)]
addArgReps [NonVoid StgArg]
args)
nonptr_wds :: Int
nonptr_wds = Int
tot_wds Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
ptr_wds
info_tbl :: CmmInfoTable
info_tbl = DynFlags -> DataCon -> Bool -> Int -> Int -> CmmInfoTable
mkDataConInfoTable DynFlags
dflags DataCon
con Bool
False
Int
ptr_wds Int
nonptr_wds
; let ticky_name :: Maybe Id
ticky_name | Bool
actually_bound = Id -> Maybe Id
forall a. a -> Maybe a
Just Id
binder
| Bool
otherwise = Maybe Id
forall a. Maybe a
Nothing
; CmmExpr
hp_plus_n <- Maybe Id
-> CmmInfoTable
-> LambdaFormInfo
-> CmmExpr
-> CmmExpr
-> [(NonVoid StgArg, Int)]
-> FCode CmmExpr
allocDynClosure Maybe Id
ticky_name CmmInfoTable
info_tbl LambdaFormInfo
lf_info
CmmExpr
use_cc CmmExpr
blame_cc [(NonVoid StgArg, Int)]
args_w_offsets
; CmmAGraph -> FCode CmmAGraph
forall (m :: * -> *) a. Monad m => a -> m a
return (DynFlags -> LocalReg -> LambdaFormInfo -> CmmExpr -> CmmAGraph
mkRhsInit DynFlags
dflags LocalReg
reg LambdaFormInfo
lf_info CmmExpr
hp_plus_n) }
where
use_cc :: CmmExpr
use_cc
| CostCentreStack -> Bool
isCurrentCCS CostCentreStack
ccs = CmmExpr
cccsExpr
| Bool
otherwise = String -> CmmExpr
forall a. String -> a
panic String
"buildDynCon: non-current CCS not implemented"
blame_cc :: CmmExpr
blame_cc = CmmExpr
use_cc
precomputedStaticConInfo_maybe :: DynFlags -> Id -> DataCon -> [NonVoid StgArg] -> Maybe CgIdInfo
precomputedStaticConInfo_maybe :: DynFlags -> Id -> DataCon -> [NonVoid StgArg] -> Maybe CgIdInfo
precomputedStaticConInfo_maybe DynFlags
dflags Id
binder DataCon
con []
| DataCon -> Bool
isNullaryRepDataCon DataCon
con
= CgIdInfo -> Maybe CgIdInfo
forall a. a -> Maybe a
Just (CgIdInfo -> Maybe CgIdInfo) -> CgIdInfo -> Maybe CgIdInfo
forall a b. (a -> b) -> a -> b
$ DynFlags -> Id -> LambdaFormInfo -> CmmLit -> CgIdInfo
litIdInfo DynFlags
dflags Id
binder (DataCon -> LambdaFormInfo
mkConLFInfo DataCon
con)
(CLabel -> CmmLit
CmmLabel (Name -> CafInfo -> CLabel
mkClosureLabel (DataCon -> Name
dataConName DataCon
con) CafInfo
NoCafRefs))
precomputedStaticConInfo_maybe DynFlags
dflags Id
binder DataCon
con [NonVoid StgArg
arg]
| Bool
intClosure Bool -> Bool -> Bool
|| Bool
charClosure
, Platform -> OS
platformOS Platform
platform OS -> OS -> Bool
forall a. Eq a => a -> a -> Bool
/= OS
OSMinGW32 Bool -> Bool -> Bool
|| Bool -> Bool
not (DynFlags -> Bool
positionIndependent DynFlags
dflags)
, Just Integer
val <- NonVoid StgArg -> Maybe Integer
getClosurePayload NonVoid StgArg
arg
, Integer -> Bool
inRange Integer
val
= let intlike_lbl :: CLabel
intlike_lbl = UnitId -> FastString -> CLabel
mkCmmClosureLabel UnitId
rtsUnitId (String -> FastString
fsLit String
label)
val_int :: Int
val_int = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
val :: Int
offsetW :: Int
offsetW = (Int
val_int Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
min_static_range)) Int -> Int -> Int
forall a. Num a => a -> a -> a
* (DynFlags -> Int
fixedHdrSizeW DynFlags
dflags Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
static_amode :: CmmLit
static_amode = Platform -> CLabel -> Int -> CmmLit
cmmLabelOffW Platform
platform CLabel
intlike_lbl Int
offsetW
in CgIdInfo -> Maybe CgIdInfo
forall a. a -> Maybe a
Just (CgIdInfo -> Maybe CgIdInfo) -> CgIdInfo -> Maybe CgIdInfo
forall a b. (a -> b) -> a -> b
$ DynFlags -> Id -> LambdaFormInfo -> CmmLit -> CgIdInfo
litIdInfo DynFlags
dflags Id
binder (DataCon -> LambdaFormInfo
mkConLFInfo DataCon
con) CmmLit
static_amode
where
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
intClosure :: Bool
intClosure = DataCon -> Bool
maybeIntLikeCon DataCon
con
charClosure :: Bool
charClosure = DataCon -> Bool
maybeCharLikeCon DataCon
con
getClosurePayload :: NonVoid StgArg -> Maybe Integer
getClosurePayload (NonVoid (StgLitArg (LitNumber LitNumType
LitNumInt Integer
val))) = Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
val
getClosurePayload (NonVoid (StgLitArg (LitChar Char
val))) = Integer -> Maybe Integer
forall a. a -> Maybe a
Just (Integer -> Maybe Integer) -> Integer -> Maybe Integer
forall a b. (a -> b) -> a -> b
$ (Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Integer) -> (Char -> Int) -> Char -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
ord (Char -> Integer) -> Char -> Integer
forall a b. (a -> b) -> a -> b
$ Char
val)
getClosurePayload NonVoid StgArg
_ = Maybe Integer
forall a. Maybe a
Nothing
inRange :: Integer -> Bool
inRange :: Integer -> Bool
inRange Integer
val
= Integer
val Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
min_static_range Bool -> Bool -> Bool
&& Integer
val Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
max_static_range
min_static_range :: Integer
min_static_range :: Integer
min_static_range
| Bool
intClosure = Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (DynFlags -> Int
mIN_INTLIKE DynFlags
dflags)
| Bool
charClosure = Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (DynFlags -> Int
mIN_CHARLIKE DynFlags
dflags)
| Bool
otherwise = String -> Integer
forall a. String -> a
panic String
"precomputedStaticConInfo_maybe: Unknown closure type"
max_static_range :: Integer
max_static_range
| Bool
intClosure = Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (DynFlags -> Int
mAX_INTLIKE DynFlags
dflags)
| Bool
charClosure = Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (DynFlags -> Int
mAX_CHARLIKE DynFlags
dflags)
| Bool
otherwise = String -> Integer
forall a. String -> a
panic String
"precomputedStaticConInfo_maybe: Unknown closure type"
label :: String
label
| Bool
intClosure = String
"stg_INTLIKE"
| Bool
charClosure = String
"stg_CHARLIKE"
| Bool
otherwise = String -> String
forall a. String -> a
panic String
"precomputedStaticConInfo_maybe: Unknown closure type"
precomputedStaticConInfo_maybe DynFlags
_ Id
_ DataCon
_ [NonVoid StgArg]
_ = Maybe CgIdInfo
forall a. Maybe a
Nothing
bindConArgs :: AltCon -> LocalReg -> [NonVoid Id] -> FCode [LocalReg]
bindConArgs :: AltCon -> LocalReg -> [NonVoid Id] -> FCode [LocalReg]
bindConArgs (DataAlt DataCon
con) LocalReg
base [NonVoid Id]
args
= ASSERT(not (isUnboxedTupleCon con))
do DynFlags
dflags <- FCode DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
Platform
platform <- FCode Platform
getPlatform
let (Int
_, Int
_, [(NonVoid Id, Int)]
args_w_offsets) = DynFlags
-> [NonVoid (PrimRep, Id)] -> (Int, Int, [(NonVoid Id, Int)])
forall a.
DynFlags
-> [NonVoid (PrimRep, a)] -> (Int, Int, [(NonVoid a, Int)])
mkVirtConstrOffsets DynFlags
dflags ([NonVoid Id] -> [NonVoid (PrimRep, Id)]
addIdReps [NonVoid Id]
args)
tag :: Int
tag = DynFlags -> DataCon -> Int
tagForCon DynFlags
dflags DataCon
con
bind_arg :: (NonVoid Id, ByteOff) -> FCode (Maybe LocalReg)
bind_arg :: (NonVoid Id, Int) -> FCode (Maybe LocalReg)
bind_arg (arg :: NonVoid Id
arg@(NonVoid Id
b), Int
offset)
| Id -> Bool
isDeadBinder Id
b
= Maybe LocalReg -> FCode (Maybe LocalReg)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe LocalReg
forall a. Maybe a
Nothing
| Bool
otherwise
= do { CmmAGraph -> FCode ()
emit (CmmAGraph -> FCode ()) -> CmmAGraph -> FCode ()
forall a b. (a -> b) -> a -> b
$ Platform -> LocalReg -> LocalReg -> Int -> Int -> CmmAGraph
mkTaggedObjectLoad Platform
platform (Platform -> NonVoid Id -> LocalReg
idToReg Platform
platform NonVoid Id
arg)
LocalReg
base Int
offset Int
tag
; LocalReg -> Maybe LocalReg
forall a. a -> Maybe a
Just (LocalReg -> Maybe LocalReg)
-> FCode LocalReg -> FCode (Maybe LocalReg)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NonVoid Id -> FCode LocalReg
bindArgToReg NonVoid Id
arg }
((NonVoid Id, Int) -> FCode (Maybe LocalReg))
-> [(NonVoid Id, Int)] -> FCode [LocalReg]
forall (m :: * -> *) a b.
Applicative m =>
(a -> m (Maybe b)) -> [a] -> m [b]
mapMaybeM (NonVoid Id, Int) -> FCode (Maybe LocalReg)
bind_arg [(NonVoid Id, Int)]
args_w_offsets
bindConArgs AltCon
_other_con LocalReg
_base [NonVoid Id]
args
= ASSERT( null args ) return []