{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StrictData #-}
{-# OPTIONS_GHC -Wno-pattern-namespace-specifier #-}
module Cek.Pykinos.Store
(
CellTag(..)
, pattern CT_Thunk
, pattern CT_Blackhole
, pattern CT_WLit
, pattern CT_WBinder
, pattern CT_WRecord
, pattern CT_WNeu
, pattern CT_Unbound
, StoreChunk(..)
, SoAStore(..)
, newSoAStore
, acquireSlot
, allocThunkSoA
, allocWLitSoA
, allocWNeuSoA
, readCellPhaseSoA
, readMemoNfSoA
, writeMemoNfSoA
) where
import Control.Monad.ST (ST)
import Data.Bits (shiftR, (.&.))
import Data.Int (Int32)
import Data.Primitive.ByteArray
( MutableByteArray
, newByteArray
, readByteArray
, setByteArray
, writeByteArray
)
import Data.Primitive.SmallArray
( SmallMutableArray
, newSmallArray
, readSmallArray
, writeSmallArray
)
import Data.Word (Word8)
import GHC.Exts (Any, unsafeCoerce#)
newtype CellTag = CellTag { CellTag -> Word8
unCellTag :: Word8 }
deriving (CellTag -> CellTag -> Bool
(CellTag -> CellTag -> Bool)
-> (CellTag -> CellTag -> Bool) -> Eq CellTag
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CellTag -> CellTag -> Bool
== :: CellTag -> CellTag -> Bool
$c/= :: CellTag -> CellTag -> Bool
/= :: CellTag -> CellTag -> Bool
Eq, Eq CellTag
Eq CellTag =>
(CellTag -> CellTag -> Ordering)
-> (CellTag -> CellTag -> Bool)
-> (CellTag -> CellTag -> Bool)
-> (CellTag -> CellTag -> Bool)
-> (CellTag -> CellTag -> Bool)
-> (CellTag -> CellTag -> CellTag)
-> (CellTag -> CellTag -> CellTag)
-> Ord CellTag
CellTag -> CellTag -> Bool
CellTag -> CellTag -> Ordering
CellTag -> CellTag -> CellTag
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: CellTag -> CellTag -> Ordering
compare :: CellTag -> CellTag -> Ordering
$c< :: CellTag -> CellTag -> Bool
< :: CellTag -> CellTag -> Bool
$c<= :: CellTag -> CellTag -> Bool
<= :: CellTag -> CellTag -> Bool
$c> :: CellTag -> CellTag -> Bool
> :: CellTag -> CellTag -> Bool
$c>= :: CellTag -> CellTag -> Bool
>= :: CellTag -> CellTag -> Bool
$cmax :: CellTag -> CellTag -> CellTag
max :: CellTag -> CellTag -> CellTag
$cmin :: CellTag -> CellTag -> CellTag
min :: CellTag -> CellTag -> CellTag
Ord, Int -> CellTag -> ShowS
[CellTag] -> ShowS
CellTag -> String
(Int -> CellTag -> ShowS)
-> (CellTag -> String) -> ([CellTag] -> ShowS) -> Show CellTag
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CellTag -> ShowS
showsPrec :: Int -> CellTag -> ShowS
$cshow :: CellTag -> String
show :: CellTag -> String
$cshowList :: [CellTag] -> ShowS
showList :: [CellTag] -> ShowS
Show)
pattern CT_Thunk :: CellTag
pattern $bCT_Thunk :: CellTag
$mCT_Thunk :: forall {r}. CellTag -> ((# #) -> r) -> ((# #) -> r) -> r
CT_Thunk = CellTag 0
pattern CT_Blackhole :: CellTag
pattern $bCT_Blackhole :: CellTag
$mCT_Blackhole :: forall {r}. CellTag -> ((# #) -> r) -> ((# #) -> r) -> r
CT_Blackhole = CellTag 1
pattern CT_WLit :: CellTag
pattern $bCT_WLit :: CellTag
$mCT_WLit :: forall {r}. CellTag -> ((# #) -> r) -> ((# #) -> r) -> r
CT_WLit = CellTag 2
pattern CT_WBinder :: CellTag
pattern $bCT_WBinder :: CellTag
$mCT_WBinder :: forall {r}. CellTag -> ((# #) -> r) -> ((# #) -> r) -> r
CT_WBinder = CellTag 3
pattern CT_WRecord :: CellTag
pattern $bCT_WRecord :: CellTag
$mCT_WRecord :: forall {r}. CellTag -> ((# #) -> r) -> ((# #) -> r) -> r
CT_WRecord = CellTag 4
pattern CT_WNeu :: CellTag
pattern $bCT_WNeu :: CellTag
$mCT_WNeu :: forall {r}. CellTag -> ((# #) -> r) -> ((# #) -> r) -> r
CT_WNeu = CellTag 5
pattern CT_Unbound :: CellTag
pattern $bCT_Unbound :: CellTag
$mCT_Unbound :: forall {r}. CellTag -> ((# #) -> r) -> ((# #) -> r) -> r
CT_Unbound = CellTag 6
data StoreChunk s = StoreChunk
{ forall s. StoreChunk s -> MutableByteArray s
scUnboxed :: !(MutableByteArray s)
, forall s. StoreChunk s -> SmallMutableArray s Any
scPtrA :: !(SmallMutableArray s Any)
, forall s. StoreChunk s -> SmallMutableArray s Any
scPtrB :: !(SmallMutableArray s Any)
}
data SoAStore s = SoAStore
{ forall s. SoAStore s -> SmallMutableArray s (StoreChunk s)
ssChunks :: !(SmallMutableArray s (StoreChunk s))
, forall s. SoAStore s -> MutableByteArray s
ssState :: !(MutableByteArray s)
}
allocStoreChunk :: ST s (StoreChunk s)
allocStoreChunk :: forall s. ST s (StoreChunk s)
allocStoreChunk = do
mba <- Int -> ST s (MutableByteArray (PrimState (ST s)))
forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
newByteArray Int
36864
setByteArray mba 4096 16384 (0xFF :: Word8)
ptrA <- newSmallArray 4096 (unsafeCoerce# ())
ptrB <- newSmallArray 4096 (unsafeCoerce# ())
pure (StoreChunk mba ptrA ptrB)
newSoAStore :: ST s (SoAStore s)
newSoAStore :: forall s. ST s (SoAStore s)
newSoAStore = do
c0 <- ST s (StoreChunk s)
forall s. ST s (StoreChunk s)
allocStoreChunk
chunks <- newSmallArray 1024 (unsafeCoerce# ())
writeSmallArray chunks 0 c0
st <- newByteArray 8
writeByteArray st 0 (0 :: Int32)
writeByteArray st 1 (1 :: Int32)
pure (SoAStore chunks st)
{-# INLINE acquireSlot #-}
acquireSlot :: SoAStore s -> ST s (Int32, StoreChunk s, Int)
acquireSlot :: forall s. SoAStore s -> ST s (Int32, StoreChunk s, Int)
acquireSlot SoAStore{MutableByteArray s
SmallMutableArray s (StoreChunk s)
ssChunks :: forall s. SoAStore s -> SmallMutableArray s (StoreChunk s)
ssState :: forall s. SoAStore s -> MutableByteArray s
ssChunks :: SmallMutableArray s (StoreChunk s)
ssState :: MutableByteArray s
..} = do
addr :: Int32 <- MutableByteArray (PrimState (ST s)) -> Int -> ST s Int32
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> m a
readByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
ssState Int
0
numC :: Int32 <- readByteArray ssState 1
writeByteArray ssState 0 (addr + 1)
let !ix = Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
addr :: Int
!chunkIx = Int
ix Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
12
!slotIx = Int
ix Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0xFFF
chunk <- if chunkIx < fromIntegral numC
then readSmallArray ssChunks chunkIx
else do
c <- allocStoreChunk
writeSmallArray ssChunks chunkIx c
writeByteArray ssState 1 (numC + 1)
pure c
pure (addr, chunk, slotIx)
{-# INLINE allocThunkSoA #-}
allocThunkSoA :: SoAStore s -> a -> b -> ST s Int32
allocThunkSoA :: forall s a b. SoAStore s -> a -> b -> ST s Int32
allocThunkSoA SoAStore s
store !a
term !b
env = do
(!addr, StoreChunk{..}, !slot) <- SoAStore s -> ST s (Int32, StoreChunk s, Int)
forall s. SoAStore s -> ST s (Int32, StoreChunk s, Int)
acquireSlot SoAStore s
store
writeByteArray scUnboxed slot (unCellTag CT_Thunk)
writeSmallArray scPtrA slot (unsafeCoerce# term)
writeSmallArray scPtrB slot (unsafeCoerce# env)
pure addr
{-# INLINE allocWLitSoA #-}
allocWLitSoA :: SoAStore s -> Int32 -> ST s Int32
allocWLitSoA :: forall s. SoAStore s -> Int32 -> ST s Int32
allocWLitSoA SoAStore s
store !Int32
litVal = do
(!addr, StoreChunk{..}, !slot) <- SoAStore s -> ST s (Int32, StoreChunk s, Int)
forall s. SoAStore s -> ST s (Int32, StoreChunk s, Int)
acquireSlot SoAStore s
store
writeByteArray scUnboxed slot (unCellTag CT_WLit)
writeByteArray scUnboxed (5120 + slot) litVal
pure addr
{-# INLINE allocWNeuSoA #-}
allocWNeuSoA :: SoAStore s -> Int32 -> ST s Int32
allocWNeuSoA :: forall s. SoAStore s -> Int32 -> ST s Int32
allocWNeuSoA SoAStore s
store !Int32
symId = do
(!addr, StoreChunk{..}, !slot) <- SoAStore s -> ST s (Int32, StoreChunk s, Int)
forall s. SoAStore s -> ST s (Int32, StoreChunk s, Int)
acquireSlot SoAStore s
store
writeByteArray scUnboxed slot (unCellTag CT_WNeu)
writeByteArray scUnboxed (5120 + slot) symId
writeSmallArray scPtrA slot (unsafeCoerce# ([] :: [Any]))
pure addr
{-# INLINE readMemoNfSoA #-}
readMemoNfSoA :: SoAStore s -> Int32 -> ST s Int32
readMemoNfSoA :: forall s. SoAStore s -> Int32 -> ST s Int32
readMemoNfSoA SoAStore{MutableByteArray s
SmallMutableArray s (StoreChunk s)
ssChunks :: forall s. SoAStore s -> SmallMutableArray s (StoreChunk s)
ssState :: forall s. SoAStore s -> MutableByteArray s
ssChunks :: SmallMutableArray s (StoreChunk s)
ssState :: MutableByteArray s
..} !Int32
addr = do
let !ix :: Int
ix = Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
addr :: Int
StoreChunk{..} <- SmallMutableArray (PrimState (ST s)) (StoreChunk s)
-> Int -> ST s (StoreChunk s)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> m a
readSmallArray SmallMutableArray s (StoreChunk s)
SmallMutableArray (PrimState (ST s)) (StoreChunk s)
ssChunks (Int
ix Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
12)
readByteArray scUnboxed (1024 + (ix .&. 0xFFF))
{-# INLINE writeMemoNfSoA #-}
writeMemoNfSoA :: SoAStore s -> Int32 -> Int32 -> ST s ()
writeMemoNfSoA :: forall s. SoAStore s -> Int32 -> Int32 -> ST s ()
writeMemoNfSoA SoAStore{MutableByteArray s
SmallMutableArray s (StoreChunk s)
ssChunks :: forall s. SoAStore s -> SmallMutableArray s (StoreChunk s)
ssState :: forall s. SoAStore s -> MutableByteArray s
ssChunks :: SmallMutableArray s (StoreChunk s)
ssState :: MutableByteArray s
..} !Int32
addr !Int32
nfAddr = do
let !ix :: Int
ix = Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
addr :: Int
StoreChunk{..} <- SmallMutableArray (PrimState (ST s)) (StoreChunk s)
-> Int -> ST s (StoreChunk s)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> m a
readSmallArray SmallMutableArray s (StoreChunk s)
SmallMutableArray (PrimState (ST s)) (StoreChunk s)
ssChunks (Int
ix Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
12)
writeByteArray scUnboxed (1024 + (ix .&. 0xFFF)) nfAddr
{-# INLINE readCellPhaseSoA #-}
readCellPhaseSoA :: SoAStore s -> Int32 -> ST s (CellTag, Int32, StoreChunk s, Int)
readCellPhaseSoA :: forall s.
SoAStore s -> Int32 -> ST s (CellTag, Int32, StoreChunk s, Int)
readCellPhaseSoA SoAStore{MutableByteArray s
SmallMutableArray s (StoreChunk s)
ssChunks :: forall s. SoAStore s -> SmallMutableArray s (StoreChunk s)
ssState :: forall s. SoAStore s -> MutableByteArray s
ssChunks :: SmallMutableArray s (StoreChunk s)
ssState :: MutableByteArray s
..} !Int32
addr = do
let !ix :: Int
ix = Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
addr :: Int
!slot :: Int
slot = Int
ix Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0xFFF
chunk@StoreChunk{..} <- SmallMutableArray (PrimState (ST s)) (StoreChunk s)
-> Int -> ST s (StoreChunk s)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> m a
readSmallArray SmallMutableArray s (StoreChunk s)
SmallMutableArray (PrimState (ST s)) (StoreChunk s)
ssChunks (Int
ix Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
12)
!rawTag <- readByteArray scUnboxed slot
!aux <- readByteArray scUnboxed (5120 + slot)
pure (CellTag rawTag, aux, chunk, slot)