{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StrictData #-}
{-# OPTIONS_GHC -Wno-pattern-namespace-specifier #-}

-- |
-- Module      : Cek.Pykinos.Store
-- Description : 4096-cell chunked Structure-of-Arrays Store arena for CESK machines.
--
-- Separates unboxed cell phase tags, memoized normal-form DAG pointers ('NfAddr'),
-- and auxiliary 32-bit integers from boxed payload pointers to minimize GC scanning.
--
-- === Structure-of-Arrays (SoA) Memory Layout
--
-- In traditional pointer-heavy graph reduction engines, every lazy evaluation cell
-- (thunk or indirection) is a heap closure containing an info pointer, mutable fields,
-- and payload pointers. As the heap grows, garbage collection pause times degrade
-- proportionally with the total number of cells.
--
-- 'SoAStore' eliminates per-cell heap headers by grouping cells into 4096-cell
-- contiguous memory chunks ('StoreChunk'):
--
--   * 'scUnboxed': A 36,864-byte unboxed 'MutableByteArray' holding raw bytes and
--     integers in three segregated contiguous lanes:
--       * Lane 0 (Bytes 0..4095): 4096 x 1-byte 'CellTag' values.
--       * Lane 1 (Bytes 4096..20479): 4096 x 4-byte 'Int32' memoized normal-form DAG addresses.
--       * Lane 2 (Bytes 20480..36863): 4096 x 4-byte 'Int32' auxiliary integers (literals, symbols).
--   * 'scPtrA': A 4096-slot 'SmallMutableArray' holding primary boxed payload pointers.
--   * 'scPtrB': A 4096-slot 'SmallMutableArray' holding secondary boxed payload pointers.
--
-- === Cell Lifecycle Transitions
--
-- Cells progress through strict evaluation phases tracked in Lane 0:
--
--   1. 'CT_Thunk': Unevaluated closure @(term, env)@ stored across 'scPtrA' and 'scPtrB'.
--   2. 'CT_Blackhole': Currently under active evaluation. Cycles trigger runtime errors.
--   3. 'CT_WLit': Integer / word literal stored unboxed in Lane 2 with zero heap allocations.
--   4. 'CT_WBinder': Evaluated lambda or function abstraction.
--   5. 'CT_WRecord': Evaluated record with interned 'Pykinos.Schema.HiddenClass.SchemaId'.
--   6. 'CT_WNeu': Neutral application or variable spine pending normalization.
module Cek.Pykinos.Store
  ( -- * Cell Phase Tags
    CellTag(..)
  , pattern CT_Thunk
  , pattern CT_Blackhole
  , pattern CT_WLit
  , pattern CT_WBinder
  , pattern CT_WRecord
  , pattern CT_WNeu
  , pattern CT_Unbound

    -- * Store Arena Structures
  , StoreChunk(..)
  , SoAStore(..)

    -- * Store Allocation and Initialization
  , newSoAStore
  , acquireSlot
  , allocThunkSoA
  , allocWLitSoA
  , allocWNeuSoA

    -- * Cell Phase and Memoization Accessors
  , 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#)

-- | 1-byte cell phase opcode stored in Lane 0 of 'StoreChunk'.
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)

-- | Unevaluated lazy thunk closure @(term, env)@.
pattern CT_Thunk     :: CellTag
pattern $bCT_Thunk :: CellTag
$mCT_Thunk :: forall {r}. CellTag -> ((# #) -> r) -> ((# #) -> r) -> r
CT_Thunk     = CellTag 0

-- | In-progress evaluation sentinel; detects infinite recursion loops.
pattern CT_Blackhole :: CellTag
pattern $bCT_Blackhole :: CellTag
$mCT_Blackhole :: forall {r}. CellTag -> ((# #) -> r) -> ((# #) -> r) -> r
CT_Blackhole = CellTag 1

-- | Evaluated unboxed integer/boolean literal.
pattern CT_WLit      :: CellTag
pattern $bCT_WLit :: CellTag
$mCT_WLit :: forall {r}. CellTag -> ((# #) -> r) -> ((# #) -> r) -> r
CT_WLit      = CellTag 2

-- | Evaluated lambda abstraction or binder closure.
pattern CT_WBinder   :: CellTag
pattern $bCT_WBinder :: CellTag
$mCT_WBinder :: forall {r}. CellTag -> ((# #) -> r) -> ((# #) -> r) -> r
CT_WBinder   = CellTag 3

-- | Evaluated flat record mapped by a 'SchemaId'.
pattern CT_WRecord   :: CellTag
pattern $bCT_WRecord :: CellTag
$mCT_WRecord :: forall {r}. CellTag -> ((# #) -> r) -> ((# #) -> r) -> r
CT_WRecord   = CellTag 4

-- | Neutral term spine waiting on an unbound variable or argument.
pattern CT_WNeu      :: CellTag
pattern $bCT_WNeu :: CellTag
$mCT_WNeu :: forall {r}. CellTag -> ((# #) -> r) -> ((# #) -> r) -> r
CT_WNeu      = CellTag 5

-- | Unbound / unallocated cell slot marker.
pattern CT_Unbound   :: CellTag
pattern $bCT_Unbound :: CellTag
$mCT_Unbound :: forall {r}. CellTag -> ((# #) -> r) -> ((# #) -> r) -> r
CT_Unbound   = CellTag 6

-- | A 4096-cell Structure-of-Arrays Store chunk.
--
-- Layout of the 36,864-byte 'scUnboxed' 'MutableByteArray':
--   * Bytes [    0 ..  4095]: Lane 0 — 4096 x Word8  CellTag (1 byte per cell)
--   * Bytes [ 4096 .. 20479]: Lane 1 — 4096 x Int32  Memoized NfAddr (4 bytes per cell)
--   * Bytes [20480 .. 36863]: Lane 2 — 4096 x Int32  AuxInt (4 bytes per cell)
data StoreChunk s = StoreChunk
  { forall s. StoreChunk s -> MutableByteArray s
scUnboxed :: !(MutableByteArray s)
    -- ^ Unified contiguous unboxed memory holding tags, memoized NF addresses, and literals.
  , forall s. StoreChunk s -> SmallMutableArray s Any
scPtrA    :: !(SmallMutableArray s Any)
    -- ^ Lane 3: Primary pointer lane for AST terms, records, or closures.
  , forall s. StoreChunk s -> SmallMutableArray s Any
scPtrB    :: !(SmallMutableArray s Any)
    -- ^ Lane 4: Secondary pointer lane for environment lists or capture frames.
  }

-- | Chunked SoA Store arena supporting up to 4,194,304 cells across 1024 chunks.
data SoAStore s = SoAStore
  { forall s. SoAStore s -> SmallMutableArray s (StoreChunk s)
ssChunks :: !(SmallMutableArray s (StoreChunk s))
    -- ^ Array of chunk pointers, dynamically allocated as monotonic address space expands.
  , forall s. SoAStore s -> MutableByteArray s
ssState  :: !(MutableByteArray s)
    -- ^ Store control state: Int32[0] = nextAddr counter, Int32[1] = count of allocated chunks.
  }

-- | Allocate and initialize a fresh 4096-cell 'StoreChunk' with Lane 1 set to -1.
--
-- Initializing Lane 1 with -1 ensures that 'readMemoNfSoA' can detect unmemoized cells
-- with a single negative integer check.
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)

-- | Create a new 'SoAStore' with chunk 0 pre-allocated.
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)

-- | Acquire the next monotonic cell 'Int32' address, allocating a new chunk on boundary crossings.
--
-- Returns the global address, the enclosing 'StoreChunk', and the local intra-chunk slot index (0..4095).
{-# 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)

-- | Allocate a lazy thunk @(term, env)@ cell in the Store.
--
-- Initializes the cell phase to 'CT_Thunk' and saves the term and environment pointers.
{-# 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

-- | Allocate an unboxed integer literal cell with zero pointer writes.
--
-- Stores the value directly in Lane 2 of 'scUnboxed', completely bypassing the GC write barrier.
{-# 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

-- | Allocate a fresh neutral binder symbol @WNeu sym []@ in the Store.
--
-- Records the symbol ID in Lane 2 and initializes an empty elimination spine.
{-# 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

-- | Check in O(1) whether 'addr' already has a memoized normal-form DAG pointer ('NfAddr' >= 0).
{-# 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))

-- | Record a memoized normal-form DAG pointer 'nfAddr' for cell 'addr'.
{-# 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

-- | Read the unboxed 'CellTag', 'AuxInt', 'StoreChunk', and slot offset for 'addr'.
{-# 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)