module GHC.StgToCmm.Foreign (
cgForeignCall,
emitPrimCall,
emitCCall,
emitCCallNeverReturns,
emitForeignCall,
emitSaveThreadState,
saveThreadState,
emitLoadThreadState,
emitSaveRegs,
emitRestoreRegs,
emitPushArgRegs,
emitPopArgRegs,
loadThreadState,
emitOpenNursery,
emitCloseNursery,
) where
import GHC.Prelude hiding( succ, (<*>) )
import GHC.Platform
import GHC.Platform.Profile
import GHC.Stg.Syntax
import GHC.StgToCmm.Prof (storeCurCCS, ccsType)
import GHC.StgToCmm.Monad
import GHC.StgToCmm.Utils
import GHC.StgToCmm.Closure
import GHC.StgToCmm.Layout
import GHC.Cmm.BlockId (newBlockId)
import GHC.Cmm
import GHC.Cmm.Utils
import GHC.Cmm.Graph
import GHC.Cmm.CallConv
import GHC.Core.Type
import GHC.Types.RepType
import GHC.Cmm.CLabel
import GHC.Runtime.Heap.Layout
import GHC.Types.ForeignCall
import GHC.Data.Maybe
import GHC.Utils.Panic
import GHC.Types.Unique.Supply
import GHC.Types.Basic
import GHC.Unit.Types
import GHC.Core.TyCo.Rep
import GHC.Builtin.Types.Prim
import GHC.Utils.Misc (zipEqual)
import Control.Monad
cgForeignCall :: ForeignCall
-> Type
-> [StgArg]
-> Type
-> FCode ReturnKind
cgForeignCall :: ForeignCall -> Type -> [StgArg] -> Type -> FCode ReturnKind
cgForeignCall (CCall (CCallSpec CCallTarget
target CCallConv
cconv Safety
safety)) Type
typ [StgArg]
stg_args Type
res_ty
= do { Platform
platform <- FCode Platform
getPlatform
; let
call_size :: [(CmmExpr, ForeignHint)] -> Maybe ByteOff
call_size [(CmmExpr, ForeignHint)]
args
| CCallConv
StdCallConv <- CCallConv
cconv = ByteOff -> Maybe ByteOff
forall a. a -> Maybe a
Just ([ByteOff] -> ByteOff
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (((CmmExpr, ForeignHint) -> ByteOff)
-> [(CmmExpr, ForeignHint)] -> [ByteOff]
forall a b. (a -> b) -> [a] -> [b]
map (CmmExpr, ForeignHint) -> ByteOff
arg_size [(CmmExpr, ForeignHint)]
args))
| Bool
otherwise = Maybe ByteOff
forall a. Maybe a
Nothing
arg_size :: (CmmExpr, ForeignHint) -> ByteOff
arg_size (CmmExpr
arg, ForeignHint
_) = ByteOff -> ByteOff -> ByteOff
forall a. Ord a => a -> a -> a
max (Width -> ByteOff
widthInBytes (Width -> ByteOff) -> Width -> ByteOff
forall a b. (a -> b) -> a -> b
$ CmmType -> Width
typeWidth (CmmType -> Width) -> CmmType -> Width
forall a b. (a -> b) -> a -> b
$ Platform -> CmmExpr -> CmmType
cmmExprType Platform
platform CmmExpr
arg)
(Platform -> ByteOff
platformWordSizeInBytes Platform
platform)
; [(CmmExpr, ForeignHint)]
cmm_args <- [StgArg] -> Type -> FCode [(CmmExpr, ForeignHint)]
getFCallArgs [StgArg]
stg_args Type
typ
; ([LocalReg]
res_regs, [ForeignHint]
res_hints) <- Type -> FCode ([LocalReg], [ForeignHint])
newUnboxedTupleRegs Type
res_ty
; let (([CmmExpr]
call_args, [ForeignHint]
arg_hints), CmmExpr
cmm_target)
= case CCallTarget
target of
StaticTarget SourceText
_ CLabelString
_ Maybe Unit
_ Bool
False ->
String -> (([CmmExpr], [ForeignHint]), CmmExpr)
forall a. HasCallStack => String -> a
panic String
"cgForeignCall: unexpected FFI value import"
StaticTarget SourceText
_ CLabelString
lbl Maybe Unit
mPkgId Bool
True
-> let labelSource :: ForeignLabelSource
labelSource
= case Maybe Unit
mPkgId of
Maybe Unit
Nothing -> ForeignLabelSource
ForeignLabelInThisPackage
Just Unit
pkgId -> UnitId -> ForeignLabelSource
ForeignLabelInPackage (Unit -> UnitId
toUnitId Unit
pkgId)
size :: Maybe ByteOff
size = [(CmmExpr, ForeignHint)] -> Maybe ByteOff
call_size [(CmmExpr, ForeignHint)]
cmm_args
in ( [(CmmExpr, ForeignHint)] -> ([CmmExpr], [ForeignHint])
forall a b. [(a, b)] -> ([a], [b])
unzip [(CmmExpr, ForeignHint)]
cmm_args
, CmmLit -> CmmExpr
CmmLit (CLabel -> CmmLit
CmmLabel
(CLabelString
-> Maybe ByteOff -> ForeignLabelSource -> FunctionOrData -> CLabel
mkForeignLabel CLabelString
lbl Maybe ByteOff
size ForeignLabelSource
labelSource FunctionOrData
IsFunction)))
CCallTarget
DynamicTarget -> case [(CmmExpr, ForeignHint)]
cmm_args of
(CmmExpr
fn,ForeignHint
_):[(CmmExpr, ForeignHint)]
rest -> ([(CmmExpr, ForeignHint)] -> ([CmmExpr], [ForeignHint])
forall a b. [(a, b)] -> ([a], [b])
unzip [(CmmExpr, ForeignHint)]
rest, CmmExpr
fn)
[] -> String -> (([CmmExpr], [ForeignHint]), CmmExpr)
forall a. HasCallStack => String -> a
panic String
"cgForeignCall []"
fc :: ForeignConvention
fc = CCallConv
-> [ForeignHint]
-> [ForeignHint]
-> CmmReturnInfo
-> ForeignConvention
ForeignConvention CCallConv
cconv [ForeignHint]
arg_hints [ForeignHint]
res_hints CmmReturnInfo
CmmMayReturn
call_target :: ForeignTarget
call_target = CmmExpr -> ForeignConvention -> ForeignTarget
ForeignTarget CmmExpr
cmm_target ForeignConvention
fc
; Sequel
sequel <- FCode Sequel
getSequel
; case Sequel
sequel of
AssignTo [LocalReg]
assign_to_these Bool
_ ->
Safety
-> [LocalReg] -> ForeignTarget -> [CmmExpr] -> FCode ReturnKind
emitForeignCall Safety
safety [LocalReg]
assign_to_these ForeignTarget
call_target [CmmExpr]
call_args
Sequel
_something_else ->
do { ReturnKind
_ <- Safety
-> [LocalReg] -> ForeignTarget -> [CmmExpr] -> FCode ReturnKind
emitForeignCall Safety
safety [LocalReg]
res_regs ForeignTarget
call_target [CmmExpr]
call_args
; [CmmExpr] -> FCode ReturnKind
emitReturn ((LocalReg -> CmmExpr) -> [LocalReg] -> [CmmExpr]
forall a b. (a -> b) -> [a] -> [b]
map (CmmReg -> CmmExpr
CmmReg (CmmReg -> CmmExpr) -> (LocalReg -> CmmReg) -> LocalReg -> CmmExpr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LocalReg -> CmmReg
CmmLocal) [LocalReg]
res_regs)
}
}
emitCCall' :: CmmReturnInfo
-> [(CmmFormal,ForeignHint)]
-> CmmExpr
-> [(CmmActual,ForeignHint)]
-> FCode ()
emitCCall' :: CmmReturnInfo
-> [(LocalReg, ForeignHint)]
-> CmmExpr
-> [(CmmExpr, ForeignHint)]
-> FCode ()
emitCCall' CmmReturnInfo
ret_info [(LocalReg, ForeignHint)]
hinted_results CmmExpr
fn [(CmmExpr, ForeignHint)]
hinted_args
= FCode ReturnKind -> FCode ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (FCode ReturnKind -> FCode ()) -> FCode ReturnKind -> FCode ()
forall a b. (a -> b) -> a -> b
$ Safety
-> [LocalReg] -> ForeignTarget -> [CmmExpr] -> FCode ReturnKind
emitForeignCall Safety
PlayRisky [LocalReg]
results ForeignTarget
target [CmmExpr]
args
where
([CmmExpr]
args, [ForeignHint]
arg_hints) = [(CmmExpr, ForeignHint)] -> ([CmmExpr], [ForeignHint])
forall a b. [(a, b)] -> ([a], [b])
unzip [(CmmExpr, ForeignHint)]
hinted_args
([LocalReg]
results, [ForeignHint]
result_hints) = [(LocalReg, ForeignHint)] -> ([LocalReg], [ForeignHint])
forall a b. [(a, b)] -> ([a], [b])
unzip [(LocalReg, ForeignHint)]
hinted_results
target :: ForeignTarget
target = CmmExpr -> ForeignConvention -> ForeignTarget
ForeignTarget CmmExpr
fn ForeignConvention
fc
fc :: ForeignConvention
fc = CCallConv
-> [ForeignHint]
-> [ForeignHint]
-> CmmReturnInfo
-> ForeignConvention
ForeignConvention CCallConv
CCallConv [ForeignHint]
arg_hints [ForeignHint]
result_hints CmmReturnInfo
ret_info
emitCCall :: [(CmmFormal,ForeignHint)]
-> CmmExpr
-> [(CmmActual,ForeignHint)]
-> FCode ()
emitCCall :: [(LocalReg, ForeignHint)]
-> CmmExpr -> [(CmmExpr, ForeignHint)] -> FCode ()
emitCCall = CmmReturnInfo
-> [(LocalReg, ForeignHint)]
-> CmmExpr
-> [(CmmExpr, ForeignHint)]
-> FCode ()
emitCCall' CmmReturnInfo
CmmMayReturn
emitCCallNeverReturns
:: [(CmmFormal,ForeignHint)]
-> CmmExpr
-> [(CmmActual,ForeignHint)]
-> FCode ()
emitCCallNeverReturns :: [(LocalReg, ForeignHint)]
-> CmmExpr -> [(CmmExpr, ForeignHint)] -> FCode ()
emitCCallNeverReturns = CmmReturnInfo
-> [(LocalReg, ForeignHint)]
-> CmmExpr
-> [(CmmExpr, ForeignHint)]
-> FCode ()
emitCCall' CmmReturnInfo
CmmNeverReturns
emitPrimCall :: [CmmFormal] -> CallishMachOp -> [CmmActual] -> FCode ()
emitPrimCall :: [LocalReg] -> CallishMachOp -> [CmmExpr] -> FCode ()
emitPrimCall [LocalReg]
res CallishMachOp
op [CmmExpr]
args
= FCode ReturnKind -> FCode ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (FCode ReturnKind -> FCode ()) -> FCode ReturnKind -> FCode ()
forall a b. (a -> b) -> a -> b
$ Safety
-> [LocalReg] -> ForeignTarget -> [CmmExpr] -> FCode ReturnKind
emitForeignCall Safety
PlayRisky [LocalReg]
res (CallishMachOp -> ForeignTarget
PrimTarget CallishMachOp
op) [CmmExpr]
args
emitForeignCall
:: Safety
-> [CmmFormal]
-> ForeignTarget
-> [CmmActual]
-> FCode ReturnKind
emitForeignCall :: Safety
-> [LocalReg] -> ForeignTarget -> [CmmExpr] -> FCode ReturnKind
emitForeignCall Safety
safety [LocalReg]
results ForeignTarget
target [CmmExpr]
args
| Bool -> Bool
not (Safety -> Bool
playSafe Safety
safety) = do
Platform
platform <- FCode Platform
getPlatform
let (CmmAGraph
caller_save, CmmAGraph
caller_load) = Platform -> (CmmAGraph, CmmAGraph)
callerSaveVolatileRegs Platform
platform
CmmAGraph -> FCode ()
emit CmmAGraph
caller_save
ForeignTarget
target' <- ForeignTarget -> FCode ForeignTarget
load_target_into_temp ForeignTarget
target
[CmmExpr]
args' <- (CmmExpr -> FCode CmmExpr) -> [CmmExpr] -> FCode [CmmExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM CmmExpr -> FCode CmmExpr
maybe_assign_temp [CmmExpr]
args
CmmAGraph -> FCode ()
emit (CmmAGraph -> FCode ()) -> CmmAGraph -> FCode ()
forall a b. (a -> b) -> a -> b
$ ForeignTarget -> [LocalReg] -> [CmmExpr] -> CmmAGraph
mkUnsafeCall ForeignTarget
target' [LocalReg]
results [CmmExpr]
args'
CmmAGraph -> FCode ()
emit CmmAGraph
caller_load
ReturnKind -> FCode ReturnKind
forall a. a -> FCode a
forall (m :: * -> *) a. Monad m => a -> m a
return ReturnKind
AssignedDirectly
| Bool
otherwise = do
Profile
profile <- FCode Profile
getProfile
Platform
platform <- FCode Platform
getPlatform
ByteOff
updfr_off <- FCode ByteOff
getUpdFrameOff
ForeignTarget
target' <- ForeignTarget -> FCode ForeignTarget
load_target_into_temp ForeignTarget
target
[CmmExpr]
args' <- (CmmExpr -> FCode CmmExpr) -> [CmmExpr] -> FCode [CmmExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM CmmExpr -> FCode CmmExpr
maybe_assign_temp [CmmExpr]
args
BlockId
k <- FCode BlockId
forall (m :: * -> *). MonadUnique m => m BlockId
newBlockId
let (ByteOff
off, [GlobalReg]
_, CmmAGraph
copyout) = Profile
-> Convention
-> Area
-> [LocalReg]
-> [LocalReg]
-> (ByteOff, [GlobalReg], CmmAGraph)
copyInOflow Profile
profile Convention
NativeReturn (BlockId -> Area
Young BlockId
k) [LocalReg]
results []
CmmTickScope
tscope <- FCode CmmTickScope
getTickScope
CmmAGraph -> FCode ()
emit (CmmAGraph -> FCode ()) -> CmmAGraph -> FCode ()
forall a b. (a -> b) -> a -> b
$
( CmmExpr -> CmmExpr -> CmmAGraph
mkStore (Area -> ByteOff -> CmmExpr
CmmStackSlot (BlockId -> Area
Young BlockId
k) (Width -> ByteOff
widthInBytes (Platform -> Width
wordWidth Platform
platform)))
(CmmLit -> CmmExpr
CmmLit (BlockId -> CmmLit
CmmBlock BlockId
k))
CmmAGraph -> CmmAGraph -> CmmAGraph
<*> CmmNode O C -> CmmAGraph
mkLast (CmmForeignCall { tgt :: ForeignTarget
tgt = ForeignTarget
target'
, res :: [LocalReg]
res = [LocalReg]
results
, args :: [CmmExpr]
args = [CmmExpr]
args'
, succ :: BlockId
succ = BlockId
k
, ret_args :: ByteOff
ret_args = ByteOff
off
, ret_off :: ByteOff
ret_off = ByteOff
updfr_off
, intrbl :: Bool
intrbl = Safety -> Bool
playInterruptible Safety
safety })
CmmAGraph -> CmmAGraph -> CmmAGraph
<*> BlockId -> CmmTickScope -> CmmAGraph
mkLabel BlockId
k CmmTickScope
tscope
CmmAGraph -> CmmAGraph -> CmmAGraph
<*> CmmAGraph
copyout
)
ReturnKind -> FCode ReturnKind
forall a. a -> FCode a
forall (m :: * -> *) a. Monad m => a -> m a
return (BlockId -> ByteOff -> ReturnKind
ReturnedTo BlockId
k ByteOff
off)
load_target_into_temp :: ForeignTarget -> FCode ForeignTarget
load_target_into_temp :: ForeignTarget -> FCode ForeignTarget
load_target_into_temp (ForeignTarget CmmExpr
expr ForeignConvention
conv) = do
CmmExpr
tmp <- CmmExpr -> FCode CmmExpr
maybe_assign_temp CmmExpr
expr
ForeignTarget -> FCode ForeignTarget
forall a. a -> FCode a
forall (m :: * -> *) a. Monad m => a -> m a
return (CmmExpr -> ForeignConvention -> ForeignTarget
ForeignTarget CmmExpr
tmp ForeignConvention
conv)
load_target_into_temp other_target :: ForeignTarget
other_target@(PrimTarget CallishMachOp
_) =
ForeignTarget -> FCode ForeignTarget
forall a. a -> FCode a
forall (m :: * -> *) a. Monad m => a -> m a
return ForeignTarget
other_target
maybe_assign_temp :: CmmExpr -> FCode CmmExpr
maybe_assign_temp :: CmmExpr -> FCode CmmExpr
maybe_assign_temp CmmExpr
e = do
Platform
platform <- FCode Platform
getPlatform
LocalReg
reg <- CmmType -> FCode LocalReg
forall (m :: * -> *). MonadUnique m => CmmType -> m LocalReg
newTemp (Platform -> CmmExpr -> CmmType
cmmExprType Platform
platform CmmExpr
e)
CmmReg -> CmmExpr -> FCode ()
emitAssign (LocalReg -> CmmReg
CmmLocal LocalReg
reg) CmmExpr
e
CmmExpr -> FCode CmmExpr
forall a. a -> FCode a
forall (m :: * -> *) a. Monad m => a -> m a
return (CmmReg -> CmmExpr
CmmReg (LocalReg -> CmmReg
CmmLocal LocalReg
reg))
emitSaveThreadState :: FCode ()
emitSaveThreadState :: FCode ()
emitSaveThreadState = do
Profile
profile <- FCode Profile
getProfile
CmmAGraph
code <- Profile -> FCode CmmAGraph
forall (m :: * -> *). MonadUnique m => Profile -> m CmmAGraph
saveThreadState Profile
profile
CmmAGraph -> FCode ()
emit CmmAGraph
code
saveThreadState :: MonadUnique m => Profile -> m CmmAGraph
saveThreadState :: forall (m :: * -> *). MonadUnique m => Profile -> m CmmAGraph
saveThreadState Profile
profile = do
let platform :: Platform
platform = Profile -> Platform
profilePlatform Profile
profile
LocalReg
tso <- CmmType -> m LocalReg
forall (m :: * -> *). MonadUnique m => CmmType -> m LocalReg
newTemp (Platform -> CmmType
gcWord Platform
platform)
CmmAGraph
close_nursery <- Profile -> LocalReg -> m CmmAGraph
forall (m :: * -> *).
MonadUnique m =>
Profile -> LocalReg -> m CmmAGraph
closeNursery Profile
profile LocalReg
tso
CmmAGraph -> m CmmAGraph
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CmmAGraph -> m CmmAGraph) -> CmmAGraph -> m CmmAGraph
forall a b. (a -> b) -> a -> b
$ [CmmAGraph] -> CmmAGraph
catAGraphs
[
CmmReg -> CmmExpr -> CmmAGraph
mkAssign (LocalReg -> CmmReg
CmmLocal LocalReg
tso) CmmExpr
currentTSOExpr
,
CmmExpr -> CmmExpr -> CmmAGraph
mkStore (Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffset Platform
platform
(Platform -> CmmExpr -> CmmExpr
cmmLoadBWord Platform
platform (Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffset Platform
platform
(CmmReg -> CmmExpr
CmmReg (LocalReg -> CmmReg
CmmLocal LocalReg
tso))
(Profile -> ByteOff
tso_stackobj Profile
profile)))
(Profile -> ByteOff
stack_SP Profile
profile))
CmmExpr
spExpr
, CmmAGraph
close_nursery
,
if Profile -> Bool
profileIsProfiling Profile
profile
then CmmExpr -> CmmExpr -> CmmAGraph
mkStore (Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffset Platform
platform (CmmReg -> CmmExpr
CmmReg (LocalReg -> CmmReg
CmmLocal LocalReg
tso)) (Profile -> ByteOff
tso_CCCS Profile
profile)) CmmExpr
cccsExpr
else CmmAGraph
mkNop
]
emitSaveRegs :: FCode ()
emitSaveRegs :: FCode ()
emitSaveRegs = do
Platform
platform <- FCode Platform
getPlatform
let regs :: [GlobalReg]
regs = Platform -> [GlobalReg]
realArgRegsCover Platform
platform
save :: CmmAGraph
save = [CmmAGraph] -> CmmAGraph
catAGraphs ((GlobalReg -> CmmAGraph) -> [GlobalReg] -> [CmmAGraph]
forall a b. (a -> b) -> [a] -> [b]
map (Platform -> GlobalReg -> CmmAGraph
callerSaveGlobalReg Platform
platform) [GlobalReg]
regs)
CmmAGraph -> FCode ()
emit CmmAGraph
save
emitRestoreRegs :: FCode ()
emitRestoreRegs :: FCode ()
emitRestoreRegs = do
Platform
platform <- FCode Platform
getPlatform
let regs :: [GlobalReg]
regs = Platform -> [GlobalReg]
realArgRegsCover Platform
platform
restore :: CmmAGraph
restore = [CmmAGraph] -> CmmAGraph
catAGraphs ((GlobalReg -> CmmAGraph) -> [GlobalReg] -> [CmmAGraph]
forall a b. (a -> b) -> [a] -> [b]
map (Platform -> GlobalReg -> CmmAGraph
callerRestoreGlobalReg Platform
platform) [GlobalReg]
regs)
CmmAGraph -> FCode ()
emit CmmAGraph
restore
emitPushArgRegs :: CmmExpr -> FCode ()
emitPushArgRegs :: CmmExpr -> FCode ()
emitPushArgRegs CmmExpr
regs_live = do
Platform
platform <- FCode Platform
getPlatform
let regs :: [(GlobalReg, ByteOff)]
regs = [GlobalReg] -> [ByteOff] -> [(GlobalReg, ByteOff)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Platform -> [GlobalReg]
allArgRegsCover Platform
platform) [ByteOff
0..]
save_arg :: (GlobalReg, ByteOff) -> FCode CmmAGraph
save_arg (GlobalReg
reg, ByteOff
n) =
let mask :: CmmExpr
mask = CmmLit -> CmmExpr
CmmLit (Integer -> Width -> CmmLit
CmmInt (Integer
1 Integer -> ByteOff -> Integer
forall a. Bits a => a -> ByteOff -> a
`shiftL` ByteOff
n) (Platform -> Width
wordWidth Platform
platform))
live :: CmmExpr
live = Platform -> CmmExpr -> CmmExpr -> CmmExpr
cmmAndWord Platform
platform CmmExpr
regs_live CmmExpr
mask
cond :: CmmExpr
cond = Platform -> CmmExpr -> CmmExpr -> CmmExpr
cmmNeWord Platform
platform CmmExpr
live (Platform -> CmmExpr
zeroExpr Platform
platform)
reg_ty :: CmmType
reg_ty = Platform -> CmmReg -> CmmType
cmmRegType Platform
platform (GlobalReg -> CmmReg
CmmGlobal GlobalReg
reg)
width :: ByteOff
width = Platform -> ByteOff -> ByteOff
roundUpToWords Platform
platform
(Width -> ByteOff
widthInBytes (Width -> ByteOff) -> Width -> ByteOff
forall a b. (a -> b) -> a -> b
$ CmmType -> Width
typeWidth CmmType
reg_ty)
adj_sp :: CmmAGraph
adj_sp = CmmReg -> CmmExpr -> CmmAGraph
mkAssign CmmReg
spReg
(Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffset Platform
platform CmmExpr
spExpr (ByteOff -> ByteOff
forall a. Num a => a -> a
negate ByteOff
width))
save_reg :: CmmAGraph
save_reg = CmmExpr -> CmmExpr -> CmmAGraph
mkStore CmmExpr
spExpr (CmmReg -> CmmExpr
CmmReg (CmmReg -> CmmExpr) -> CmmReg -> CmmExpr
forall a b. (a -> b) -> a -> b
$ GlobalReg -> CmmReg
CmmGlobal GlobalReg
reg)
in CmmExpr -> CmmAGraph -> FCode CmmAGraph
mkCmmIfThen CmmExpr
cond (CmmAGraph -> FCode CmmAGraph) -> CmmAGraph -> FCode CmmAGraph
forall a b. (a -> b) -> a -> b
$ [CmmAGraph] -> CmmAGraph
catAGraphs [CmmAGraph
adj_sp, CmmAGraph
save_reg]
CmmAGraph -> FCode ()
emit (CmmAGraph -> FCode ())
-> ([CmmAGraph] -> CmmAGraph) -> [CmmAGraph] -> FCode ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CmmAGraph] -> CmmAGraph
catAGraphs ([CmmAGraph] -> FCode ()) -> FCode [CmmAGraph] -> FCode ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ((GlobalReg, ByteOff) -> FCode CmmAGraph)
-> [(GlobalReg, ByteOff)] -> FCode [CmmAGraph]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (GlobalReg, ByteOff) -> FCode CmmAGraph
save_arg ([(GlobalReg, ByteOff)] -> [(GlobalReg, ByteOff)]
forall a. [a] -> [a]
reverse [(GlobalReg, ByteOff)]
regs)
emitPopArgRegs :: CmmExpr -> FCode ()
emitPopArgRegs :: CmmExpr -> FCode ()
emitPopArgRegs CmmExpr
regs_live = do
Platform
platform <- FCode Platform
getPlatform
let regs :: [(GlobalReg, ByteOff)]
regs = [GlobalReg] -> [ByteOff] -> [(GlobalReg, ByteOff)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Platform -> [GlobalReg]
allArgRegsCover Platform
platform) [ByteOff
0..]
save_arg :: (GlobalReg, ByteOff) -> FCode CmmAGraph
save_arg (GlobalReg
reg, ByteOff
n) =
let mask :: CmmExpr
mask = CmmLit -> CmmExpr
CmmLit (Integer -> Width -> CmmLit
CmmInt (Integer
1 Integer -> ByteOff -> Integer
forall a. Bits a => a -> ByteOff -> a
`shiftL` ByteOff
n) (Platform -> Width
wordWidth Platform
platform))
live :: CmmExpr
live = Platform -> CmmExpr -> CmmExpr -> CmmExpr
cmmAndWord Platform
platform CmmExpr
regs_live CmmExpr
mask
cond :: CmmExpr
cond = Platform -> CmmExpr -> CmmExpr -> CmmExpr
cmmNeWord Platform
platform CmmExpr
live (Platform -> CmmExpr
zeroExpr Platform
platform)
reg_ty :: CmmType
reg_ty = Platform -> CmmReg -> CmmType
cmmRegType Platform
platform (GlobalReg -> CmmReg
CmmGlobal GlobalReg
reg)
width :: ByteOff
width = Platform -> ByteOff -> ByteOff
roundUpToWords Platform
platform
(Width -> ByteOff
widthInBytes (Width -> ByteOff) -> Width -> ByteOff
forall a b. (a -> b) -> a -> b
$ CmmType -> Width
typeWidth CmmType
reg_ty)
adj_sp :: CmmAGraph
adj_sp = CmmReg -> CmmExpr -> CmmAGraph
mkAssign CmmReg
spReg
(Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffset Platform
platform CmmExpr
spExpr ByteOff
width)
restore_reg :: CmmAGraph
restore_reg = CmmReg -> CmmExpr -> CmmAGraph
mkAssign (GlobalReg -> CmmReg
CmmGlobal GlobalReg
reg) (CmmExpr -> CmmType -> AlignmentSpec -> CmmExpr
CmmLoad CmmExpr
spExpr CmmType
reg_ty AlignmentSpec
NaturallyAligned)
in CmmExpr -> CmmAGraph -> FCode CmmAGraph
mkCmmIfThen CmmExpr
cond (CmmAGraph -> FCode CmmAGraph) -> CmmAGraph -> FCode CmmAGraph
forall a b. (a -> b) -> a -> b
$ [CmmAGraph] -> CmmAGraph
catAGraphs [CmmAGraph
restore_reg, CmmAGraph
adj_sp]
CmmAGraph -> FCode ()
emit (CmmAGraph -> FCode ())
-> ([CmmAGraph] -> CmmAGraph) -> [CmmAGraph] -> FCode ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CmmAGraph] -> CmmAGraph
catAGraphs ([CmmAGraph] -> FCode ()) -> FCode [CmmAGraph] -> FCode ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ((GlobalReg, ByteOff) -> FCode CmmAGraph)
-> [(GlobalReg, ByteOff)] -> FCode [CmmAGraph]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (GlobalReg, ByteOff) -> FCode CmmAGraph
save_arg [(GlobalReg, ByteOff)]
regs
emitCloseNursery :: FCode ()
emitCloseNursery :: FCode ()
emitCloseNursery = do
Profile
profile <- FCode Profile
getProfile
let platform :: Platform
platform = Profile -> Platform
profilePlatform Profile
profile
LocalReg
tso <- CmmType -> FCode LocalReg
forall (m :: * -> *). MonadUnique m => CmmType -> m LocalReg
newTemp (Platform -> CmmType
bWord Platform
platform)
CmmAGraph
code <- Profile -> LocalReg -> FCode CmmAGraph
forall (m :: * -> *).
MonadUnique m =>
Profile -> LocalReg -> m CmmAGraph
closeNursery Profile
profile LocalReg
tso
CmmAGraph -> FCode ()
emit (CmmAGraph -> FCode ()) -> CmmAGraph -> FCode ()
forall a b. (a -> b) -> a -> b
$ CmmReg -> CmmExpr -> CmmAGraph
mkAssign (LocalReg -> CmmReg
CmmLocal LocalReg
tso) CmmExpr
currentTSOExpr CmmAGraph -> CmmAGraph -> CmmAGraph
<*> CmmAGraph
code
closeNursery :: MonadUnique m => Profile -> LocalReg -> m CmmAGraph
closeNursery :: forall (m :: * -> *).
MonadUnique m =>
Profile -> LocalReg -> m CmmAGraph
closeNursery Profile
profile LocalReg
tso = do
let tsoreg :: CmmReg
tsoreg = LocalReg -> CmmReg
CmmLocal LocalReg
tso
platform :: Platform
platform = Profile -> Platform
profilePlatform Profile
profile
CmmReg
cnreg <- LocalReg -> CmmReg
CmmLocal (LocalReg -> CmmReg) -> m LocalReg -> m CmmReg
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CmmType -> m LocalReg
forall (m :: * -> *). MonadUnique m => CmmType -> m LocalReg
newTemp (Platform -> CmmType
bWord Platform
platform)
CmmAGraph -> m CmmAGraph
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CmmAGraph -> m CmmAGraph) -> CmmAGraph -> m CmmAGraph
forall a b. (a -> b) -> a -> b
$ [CmmAGraph] -> CmmAGraph
catAGraphs [
CmmReg -> CmmExpr -> CmmAGraph
mkAssign CmmReg
cnreg CmmExpr
currentNurseryExpr,
CmmExpr -> CmmExpr -> CmmAGraph
mkStore (Platform -> CmmReg -> CmmExpr
nursery_bdescr_free Platform
platform CmmReg
cnreg) (Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffsetW Platform
platform CmmExpr
hpExpr ByteOff
1),
let alloc :: CmmExpr
alloc =
MachOp -> [CmmExpr] -> CmmExpr
CmmMachOp (Platform -> MachOp
mo_wordSub Platform
platform)
[ Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffsetW Platform
platform CmmExpr
hpExpr ByteOff
1
, Platform -> CmmExpr -> CmmExpr
cmmLoadBWord Platform
platform (Platform -> CmmReg -> CmmExpr
nursery_bdescr_start Platform
platform CmmReg
cnreg)
]
alloc_limit :: CmmExpr
alloc_limit = Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffset Platform
platform (CmmReg -> CmmExpr
CmmReg CmmReg
tsoreg) (Profile -> ByteOff
tso_alloc_limit Profile
profile)
in
CmmExpr -> CmmExpr -> CmmAGraph
mkStore CmmExpr
alloc_limit (MachOp -> [CmmExpr] -> CmmExpr
CmmMachOp (Width -> MachOp
MO_Sub Width
W64)
[ CmmExpr -> CmmType -> AlignmentSpec -> CmmExpr
CmmLoad CmmExpr
alloc_limit CmmType
b64 AlignmentSpec
NaturallyAligned
, MachOp -> [CmmExpr] -> CmmExpr
CmmMachOp (Platform -> MachOp
mo_WordTo64 Platform
platform) [CmmExpr
alloc] ])
]
emitLoadThreadState :: FCode ()
emitLoadThreadState :: FCode ()
emitLoadThreadState = do
Profile
profile <- FCode Profile
getProfile
CmmAGraph
code <- Profile -> FCode CmmAGraph
forall (m :: * -> *). MonadUnique m => Profile -> m CmmAGraph
loadThreadState Profile
profile
CmmAGraph -> FCode ()
emit CmmAGraph
code
loadThreadState :: MonadUnique m => Profile -> m CmmAGraph
loadThreadState :: forall (m :: * -> *). MonadUnique m => Profile -> m CmmAGraph
loadThreadState Profile
profile = do
let platform :: Platform
platform = Profile -> Platform
profilePlatform Profile
profile
LocalReg
tso <- CmmType -> m LocalReg
forall (m :: * -> *). MonadUnique m => CmmType -> m LocalReg
newTemp (Platform -> CmmType
gcWord Platform
platform)
LocalReg
stack <- CmmType -> m LocalReg
forall (m :: * -> *). MonadUnique m => CmmType -> m LocalReg
newTemp (Platform -> CmmType
gcWord Platform
platform)
CmmAGraph
open_nursery <- Profile -> LocalReg -> m CmmAGraph
forall (m :: * -> *).
MonadUnique m =>
Profile -> LocalReg -> m CmmAGraph
openNursery Profile
profile LocalReg
tso
CmmAGraph -> m CmmAGraph
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CmmAGraph -> m CmmAGraph) -> CmmAGraph -> m CmmAGraph
forall a b. (a -> b) -> a -> b
$ [CmmAGraph] -> CmmAGraph
catAGraphs [
CmmReg -> CmmExpr -> CmmAGraph
mkAssign (LocalReg -> CmmReg
CmmLocal LocalReg
tso) CmmExpr
currentTSOExpr,
CmmReg -> CmmExpr -> CmmAGraph
mkAssign (LocalReg -> CmmReg
CmmLocal LocalReg
stack) (Platform -> CmmExpr -> CmmExpr
cmmLoadBWord Platform
platform (Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffset Platform
platform (CmmReg -> CmmExpr
CmmReg (LocalReg -> CmmReg
CmmLocal LocalReg
tso)) (Profile -> ByteOff
tso_stackobj Profile
profile))),
CmmReg -> CmmExpr -> CmmAGraph
mkAssign CmmReg
spReg (Platform -> CmmExpr -> CmmExpr
cmmLoadBWord Platform
platform (Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffset Platform
platform (CmmReg -> CmmExpr
CmmReg (LocalReg -> CmmReg
CmmLocal LocalReg
stack)) (Profile -> ByteOff
stack_SP Profile
profile))),
CmmReg -> CmmExpr -> CmmAGraph
mkAssign CmmReg
spLimReg (Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffsetW Platform
platform (Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffset Platform
platform (CmmReg -> CmmExpr
CmmReg (LocalReg -> CmmReg
CmmLocal LocalReg
stack)) (Profile -> ByteOff
stack_STACK Profile
profile))
(PlatformConstants -> ByteOff
pc_RESERVED_STACK_WORDS (Platform -> PlatformConstants
platformConstants Platform
platform))),
CmmReg -> CmmExpr -> CmmAGraph
mkAssign CmmReg
hpAllocReg (Platform -> CmmExpr
zeroExpr Platform
platform),
CmmAGraph
open_nursery,
if Profile -> Bool
profileIsProfiling Profile
profile
then let ccs_ptr :: CmmExpr
ccs_ptr = Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffset Platform
platform (CmmReg -> CmmExpr
CmmReg (LocalReg -> CmmReg
CmmLocal LocalReg
tso)) (Profile -> ByteOff
tso_CCCS Profile
profile)
in CmmExpr -> CmmAGraph
storeCurCCS (CmmExpr -> CmmType -> AlignmentSpec -> CmmExpr
CmmLoad CmmExpr
ccs_ptr (Platform -> CmmType
ccsType Platform
platform) AlignmentSpec
NaturallyAligned)
else CmmAGraph
mkNop
]
emitOpenNursery :: FCode ()
emitOpenNursery :: FCode ()
emitOpenNursery = do
Profile
profile <- FCode Profile
getProfile
let platform :: Platform
platform = Profile -> Platform
profilePlatform Profile
profile
LocalReg
tso <- CmmType -> FCode LocalReg
forall (m :: * -> *). MonadUnique m => CmmType -> m LocalReg
newTemp (Platform -> CmmType
bWord Platform
platform)
CmmAGraph
code <- Profile -> LocalReg -> FCode CmmAGraph
forall (m :: * -> *).
MonadUnique m =>
Profile -> LocalReg -> m CmmAGraph
openNursery Profile
profile LocalReg
tso
CmmAGraph -> FCode ()
emit (CmmAGraph -> FCode ()) -> CmmAGraph -> FCode ()
forall a b. (a -> b) -> a -> b
$ CmmReg -> CmmExpr -> CmmAGraph
mkAssign (LocalReg -> CmmReg
CmmLocal LocalReg
tso) CmmExpr
currentTSOExpr CmmAGraph -> CmmAGraph -> CmmAGraph
<*> CmmAGraph
code
openNursery :: MonadUnique m => Profile -> LocalReg -> m CmmAGraph
openNursery :: forall (m :: * -> *).
MonadUnique m =>
Profile -> LocalReg -> m CmmAGraph
openNursery Profile
profile LocalReg
tso = do
let tsoreg :: CmmReg
tsoreg = LocalReg -> CmmReg
CmmLocal LocalReg
tso
platform :: Platform
platform = Profile -> Platform
profilePlatform Profile
profile
CmmReg
cnreg <- LocalReg -> CmmReg
CmmLocal (LocalReg -> CmmReg) -> m LocalReg -> m CmmReg
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CmmType -> m LocalReg
forall (m :: * -> *). MonadUnique m => CmmType -> m LocalReg
newTemp (Platform -> CmmType
bWord Platform
platform)
CmmReg
bdfreereg <- LocalReg -> CmmReg
CmmLocal (LocalReg -> CmmReg) -> m LocalReg -> m CmmReg
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CmmType -> m LocalReg
forall (m :: * -> *). MonadUnique m => CmmType -> m LocalReg
newTemp (Platform -> CmmType
bWord Platform
platform)
CmmReg
bdstartreg <- LocalReg -> CmmReg
CmmLocal (LocalReg -> CmmReg) -> m LocalReg -> m CmmReg
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CmmType -> m LocalReg
forall (m :: * -> *). MonadUnique m => CmmType -> m LocalReg
newTemp (Platform -> CmmType
bWord Platform
platform)
CmmAGraph -> m CmmAGraph
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CmmAGraph -> m CmmAGraph) -> CmmAGraph -> m CmmAGraph
forall a b. (a -> b) -> a -> b
$ [CmmAGraph] -> CmmAGraph
catAGraphs [
CmmReg -> CmmExpr -> CmmAGraph
mkAssign CmmReg
cnreg CmmExpr
currentNurseryExpr,
CmmReg -> CmmExpr -> CmmAGraph
mkAssign CmmReg
bdfreereg (Platform -> CmmExpr -> CmmExpr
cmmLoadBWord Platform
platform (Platform -> CmmReg -> CmmExpr
nursery_bdescr_free Platform
platform CmmReg
cnreg)),
CmmReg -> CmmExpr -> CmmAGraph
mkAssign CmmReg
hpReg (Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffsetW Platform
platform (CmmReg -> CmmExpr
CmmReg CmmReg
bdfreereg) (-ByteOff
1)),
CmmReg -> CmmExpr -> CmmAGraph
mkAssign CmmReg
bdstartreg (Platform -> CmmExpr -> CmmExpr
cmmLoadBWord Platform
platform (Platform -> CmmReg -> CmmExpr
nursery_bdescr_start Platform
platform CmmReg
cnreg)),
CmmReg -> CmmExpr -> CmmAGraph
mkAssign CmmReg
hpLimReg
(Platform -> CmmExpr -> CmmExpr -> CmmExpr
cmmOffsetExpr Platform
platform
(CmmReg -> CmmExpr
CmmReg CmmReg
bdstartreg)
(Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffset Platform
platform
(MachOp -> [CmmExpr] -> CmmExpr
CmmMachOp (Platform -> MachOp
mo_wordMul Platform
platform)
[ MachOp -> [CmmExpr] -> CmmExpr
CmmMachOp (Width -> Width -> MachOp
MO_SS_Conv Width
W32 (Platform -> Width
wordWidth Platform
platform))
[CmmExpr -> CmmType -> AlignmentSpec -> CmmExpr
CmmLoad (Platform -> CmmReg -> CmmExpr
nursery_bdescr_blocks Platform
platform CmmReg
cnreg) CmmType
b32 AlignmentSpec
NaturallyAligned]
, Platform -> ByteOff -> CmmExpr
mkIntExpr Platform
platform (PlatformConstants -> ByteOff
pc_BLOCK_SIZE (Platform -> PlatformConstants
platformConstants Platform
platform))
])
(-ByteOff
1)
)
),
let alloc :: CmmExpr
alloc =
MachOp -> [CmmExpr] -> CmmExpr
CmmMachOp (Platform -> MachOp
mo_wordSub Platform
platform) [CmmReg -> CmmExpr
CmmReg CmmReg
bdfreereg, CmmReg -> CmmExpr
CmmReg CmmReg
bdstartreg]
alloc_limit :: CmmExpr
alloc_limit = Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffset Platform
platform (CmmReg -> CmmExpr
CmmReg CmmReg
tsoreg) (Profile -> ByteOff
tso_alloc_limit Profile
profile)
in
CmmExpr -> CmmExpr -> CmmAGraph
mkStore CmmExpr
alloc_limit (MachOp -> [CmmExpr] -> CmmExpr
CmmMachOp (Width -> MachOp
MO_Add Width
W64)
[ CmmExpr -> CmmType -> AlignmentSpec -> CmmExpr
CmmLoad CmmExpr
alloc_limit CmmType
b64 AlignmentSpec
NaturallyAligned
, MachOp -> [CmmExpr] -> CmmExpr
CmmMachOp (Platform -> MachOp
mo_WordTo64 Platform
platform) [CmmExpr
alloc] ])
]
nursery_bdescr_free, nursery_bdescr_start, nursery_bdescr_blocks
:: Platform -> CmmReg -> CmmExpr
nursery_bdescr_free :: Platform -> CmmReg -> CmmExpr
nursery_bdescr_free Platform
platform CmmReg
cn =
Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffset Platform
platform (CmmReg -> CmmExpr
CmmReg CmmReg
cn) (PlatformConstants -> ByteOff
pc_OFFSET_bdescr_free (Platform -> PlatformConstants
platformConstants Platform
platform))
nursery_bdescr_start :: Platform -> CmmReg -> CmmExpr
nursery_bdescr_start Platform
platform CmmReg
cn =
Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffset Platform
platform (CmmReg -> CmmExpr
CmmReg CmmReg
cn) (PlatformConstants -> ByteOff
pc_OFFSET_bdescr_start (Platform -> PlatformConstants
platformConstants Platform
platform))
nursery_bdescr_blocks :: Platform -> CmmReg -> CmmExpr
nursery_bdescr_blocks Platform
platform CmmReg
cn =
Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffset Platform
platform (CmmReg -> CmmExpr
CmmReg CmmReg
cn) (PlatformConstants -> ByteOff
pc_OFFSET_bdescr_blocks (Platform -> PlatformConstants
platformConstants Platform
platform))
tso_stackobj, tso_CCCS, tso_alloc_limit, stack_STACK, stack_SP :: Profile -> ByteOff
tso_stackobj :: Profile -> ByteOff
tso_stackobj Profile
profile = Profile -> ByteOff -> ByteOff
closureField Profile
profile (PlatformConstants -> ByteOff
pc_OFFSET_StgTSO_stackobj (Profile -> PlatformConstants
profileConstants Profile
profile))
tso_alloc_limit :: Profile -> ByteOff
tso_alloc_limit Profile
profile = Profile -> ByteOff -> ByteOff
closureField Profile
profile (PlatformConstants -> ByteOff
pc_OFFSET_StgTSO_alloc_limit (Profile -> PlatformConstants
profileConstants Profile
profile))
tso_CCCS :: Profile -> ByteOff
tso_CCCS Profile
profile = Profile -> ByteOff -> ByteOff
closureField Profile
profile (PlatformConstants -> ByteOff
pc_OFFSET_StgTSO_cccs (Profile -> PlatformConstants
profileConstants Profile
profile))
stack_STACK :: Profile -> ByteOff
stack_STACK Profile
profile = Profile -> ByteOff -> ByteOff
closureField Profile
profile (PlatformConstants -> ByteOff
pc_OFFSET_StgStack_stack (Profile -> PlatformConstants
profileConstants Profile
profile))
stack_SP :: Profile -> ByteOff
stack_SP Profile
profile = Profile -> ByteOff -> ByteOff
closureField Profile
profile (PlatformConstants -> ByteOff
pc_OFFSET_StgStack_sp (Profile -> PlatformConstants
profileConstants Profile
profile))
closureField :: Profile -> ByteOff -> ByteOff
closureField :: Profile -> ByteOff -> ByteOff
closureField Profile
profile ByteOff
off = ByteOff
off ByteOff -> ByteOff -> ByteOff
forall a. Num a => a -> a -> a
+ Profile -> ByteOff
fixedHdrSize Profile
profile
getFCallArgs ::
[StgArg]
-> Type
-> FCode [(CmmExpr, ForeignHint)]
getFCallArgs :: [StgArg] -> Type -> FCode [(CmmExpr, ForeignHint)]
getFCallArgs [StgArg]
args Type
typ
= do { [Maybe (CmmExpr, ForeignHint)]
mb_cmms <- ((StgArg, StgFArgType) -> FCode (Maybe (CmmExpr, ForeignHint)))
-> [(StgArg, StgFArgType)] -> FCode [Maybe (CmmExpr, ForeignHint)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (StgArg, StgFArgType) -> FCode (Maybe (CmmExpr, ForeignHint))
get (String -> [StgArg] -> [StgFArgType] -> [(StgArg, StgFArgType)]
forall a b. (() :: Constraint) => String -> [a] -> [b] -> [(a, b)]
zipEqual String
"getFCallArgs" [StgArg]
args (Type -> [StgFArgType]
collectStgFArgTypes Type
typ))
; [(CmmExpr, ForeignHint)] -> FCode [(CmmExpr, ForeignHint)]
forall a. a -> FCode a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Maybe (CmmExpr, ForeignHint)] -> [(CmmExpr, ForeignHint)]
forall a. [Maybe a] -> [a]
catMaybes [Maybe (CmmExpr, ForeignHint)]
mb_cmms) }
where
get :: (StgArg, StgFArgType) -> FCode (Maybe (CmmExpr, ForeignHint))
get (StgArg
arg,StgFArgType
typ)
| [PrimRep] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [PrimRep]
arg_reps
= Maybe (CmmExpr, ForeignHint)
-> FCode (Maybe (CmmExpr, ForeignHint))
forall a. a -> FCode a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (CmmExpr, ForeignHint)
forall a. Maybe a
Nothing
| Bool
otherwise
= do { CmmExpr
cmm <- NonVoid StgArg -> FCode CmmExpr
getArgAmode (StgArg -> NonVoid StgArg
forall a. a -> NonVoid a
NonVoid StgArg
arg)
; Profile
profile <- FCode Profile
getProfile
; Maybe (CmmExpr, ForeignHint)
-> FCode (Maybe (CmmExpr, ForeignHint))
forall a. a -> FCode a
forall (m :: * -> *) a. Monad m => a -> m a
return ((CmmExpr, ForeignHint) -> Maybe (CmmExpr, ForeignHint)
forall a. a -> Maybe a
Just (Profile -> StgFArgType -> CmmExpr -> CmmExpr
add_shim Profile
profile StgFArgType
typ CmmExpr
cmm, ForeignHint
hint)) }
where
arg_ty :: Type
arg_ty = StgArg -> Type
stgArgType StgArg
arg
arg_reps :: [PrimRep]
arg_reps = (() :: Constraint) => Type -> [PrimRep]
Type -> [PrimRep]
typePrimRep Type
arg_ty
hint :: ForeignHint
hint = Type -> ForeignHint
typeForeignHint Type
arg_ty
data StgFArgType
= StgPlainType
| StgArrayType
| StgSmallArrayType
| StgByteArrayType
add_shim :: Profile -> StgFArgType -> CmmExpr -> CmmExpr
add_shim :: Profile -> StgFArgType -> CmmExpr -> CmmExpr
add_shim Profile
profile StgFArgType
ty CmmExpr
expr = case StgFArgType
ty of
StgFArgType
StgPlainType -> CmmExpr
expr
StgFArgType
StgArrayType -> Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffsetB Platform
platform CmmExpr
expr (Profile -> ByteOff
arrPtrsHdrSize Profile
profile)
StgFArgType
StgSmallArrayType -> Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffsetB Platform
platform CmmExpr
expr (Profile -> ByteOff
smallArrPtrsHdrSize Profile
profile)
StgFArgType
StgByteArrayType -> Platform -> CmmExpr -> ByteOff -> CmmExpr
cmmOffsetB Platform
platform CmmExpr
expr (Profile -> ByteOff
arrWordsHdrSize Profile
profile)
where
platform :: Platform
platform = Profile -> Platform
profilePlatform Profile
profile
collectStgFArgTypes :: Type -> [StgFArgType]
collectStgFArgTypes :: Type -> [StgFArgType]
collectStgFArgTypes = [StgFArgType] -> Type -> [StgFArgType]
go []
where
go :: [StgFArgType] -> Type -> [StgFArgType]
go [StgFArgType]
bs (ForAllTy ForAllTyBinder
_ Type
res) = [StgFArgType] -> Type -> [StgFArgType]
go [StgFArgType]
bs Type
res
go [StgFArgType]
bs (AppTy{}) = [StgFArgType] -> [StgFArgType]
forall a. [a] -> [a]
reverse [StgFArgType]
bs
go [StgFArgType]
bs (TyConApp{}) = [StgFArgType] -> [StgFArgType]
forall a. [a] -> [a]
reverse [StgFArgType]
bs
go [StgFArgType]
bs (LitTy{}) = [StgFArgType] -> [StgFArgType]
forall a. [a] -> [a]
reverse [StgFArgType]
bs
go [StgFArgType]
bs (TyVarTy{}) = [StgFArgType] -> [StgFArgType]
forall a. [a] -> [a]
reverse [StgFArgType]
bs
go [StgFArgType]
_ (CastTy{}) = String -> [StgFArgType]
forall a. HasCallStack => String -> a
panic String
"myCollectTypeArgs: CastTy"
go [StgFArgType]
_ (CoercionTy{}) = String -> [StgFArgType]
forall a. HasCallStack => String -> a
panic String
"myCollectTypeArgs: CoercionTy"
go [StgFArgType]
bs (FunTy {ft_arg :: Type -> Type
ft_arg = Type
arg, ft_res :: Type -> Type
ft_res=Type
res}) =
[StgFArgType] -> Type -> [StgFArgType]
go (Type -> StgFArgType
typeToStgFArgType Type
argStgFArgType -> [StgFArgType] -> [StgFArgType]
forall a. a -> [a] -> [a]
:[StgFArgType]
bs) Type
res
typeToStgFArgType :: Type -> StgFArgType
typeToStgFArgType :: Type -> StgFArgType
typeToStgFArgType Type
typ
| TyCon
tycon TyCon -> TyCon -> Bool
forall a. Eq a => a -> a -> Bool
== TyCon
arrayPrimTyCon = StgFArgType
StgArrayType
| TyCon
tycon TyCon -> TyCon -> Bool
forall a. Eq a => a -> a -> Bool
== TyCon
mutableArrayPrimTyCon = StgFArgType
StgArrayType
| TyCon
tycon TyCon -> TyCon -> Bool
forall a. Eq a => a -> a -> Bool
== TyCon
smallArrayPrimTyCon = StgFArgType
StgSmallArrayType
| TyCon
tycon TyCon -> TyCon -> Bool
forall a. Eq a => a -> a -> Bool
== TyCon
smallMutableArrayPrimTyCon = StgFArgType
StgSmallArrayType
| TyCon
tycon TyCon -> TyCon -> Bool
forall a. Eq a => a -> a -> Bool
== TyCon
byteArrayPrimTyCon = StgFArgType
StgByteArrayType
| TyCon
tycon TyCon -> TyCon -> Bool
forall a. Eq a => a -> a -> Bool
== TyCon
mutableByteArrayPrimTyCon = StgFArgType
StgByteArrayType
| Bool
otherwise = StgFArgType
StgPlainType
where
tycon :: TyCon
tycon = (() :: Constraint) => Type -> TyCon
Type -> TyCon
tyConAppTyCon (Type -> Type
unwrapType Type
typ)