module DDC.Core.Tetra.Convert.Exp.PrimVector
(convertPrimVector)
where
import DDC.Core.Tetra.Convert.Exp.Base
import DDC.Core.Tetra.Convert.Boxing
import DDC.Core.Tetra.Convert.Type
import DDC.Core.Tetra.Convert.Error
import DDC.Core.Exp.Annot
import DDC.Core.Check (AnTEC(..))
import qualified DDC.Core.Tetra.Prim as E
import qualified DDC.Core.Salt.Runtime as A
import qualified DDC.Core.Salt.Name as A
import qualified DDC.Core.Salt.Compounds as A
convertPrimVector
:: Show a
=> ExpContext
-> Context a
-> Exp (AnTEC a E.Name) E.Name
-> Maybe (ConvertM a (Exp a A.Name))
convertPrimVector _ectx ctx xxExp
= let convertX = contextConvertExp ctx
in case xxExp of
XCast _ CastRun xxApp@(XApp a _ _)
| Just ( E.NameOpVector E.OpVectorAlloc True
, [XType _ _rPrime, XType _ tElem, xLength])
<- takeXPrimApps xxApp
, isNumericType tElem
-> Just $ do
let a' = annotTail a
tElem' <- convertDataPrimitiveT tElem
xLengthElems' <- convertX ExpArg ctx xLength
let xLengthBytes' = A.xShl a' A.tNat xLengthElems'
(A.xStoreSize2 a' tElem')
return $ XLet a' (LLet (BAnon (A.tPtr A.rTop A.tObj))
(A.xAllocRaw a' A.rTop 0 xLengthBytes'))
$ XVar a' (UIx 0)
XApp a _ _
| Just ( E.NameOpVector E.OpVectorLength True
, [XType _ _tPrime, XType _ tElem, xVec])
<- takeXPrimApps xxExp
, isNumericType tElem
-> Just $ do
let a' = annotTail a
tElem' <- convertDataPrimitiveT tElem
xVec' <- convertX ExpArg ctx xVec
let xLengthBytes = xVectorLength a' A.rTop xVec'
return $ A.xShr a' A.tNat xLengthBytes
(A.xStoreSize2 a' tElem')
XCast _ CastRun xxApp@(XApp a _ _)
| Just ( E.NameOpVector E.OpVectorRead True
, [XType _ _rPrime, XType _ tElem, xVec, xIndex])
<- takeXPrimApps xxApp
, isNumericType tElem
-> Just $ do
let a' = annotTail a
tElem' <- convertDataPrimitiveT tElem
xVec' <- convertX ExpArg ctx xVec
xIndex' <- convertX ExpArg ctx xIndex
let xPayload' = A.xCastPtr a' A.rTop tElem' (A.tWord 8)
(A.xPayloadOfRaw a' A.rTop xVec')
let xStart' = A.xShl a' A.tNat xIndex'
(A.xStoreSize2 a' tElem')
let xTop' = xVectorLength a' A.rTop xVec'
return $ A.xPeekBounded a' A.rTop tElem' xPayload' xStart' xTop'
XCast _ CastRun xxApp@(XApp a _ _)
| Just ( E.NameOpVector E.OpVectorWrite True
, [XType _ _rPrime, XType _ tElem, xVec, xIndex, xValue])
<- takeXPrimApps xxApp
, isNumericType tElem
-> Just $ do
let a' = annotTail a
tElem' <- convertDataPrimitiveT tElem
xVec' <- convertX ExpArg ctx xVec
xIndex' <- convertX ExpArg ctx xIndex
xValue' <- convertX ExpArg ctx xValue
let xPayload' = A.xCastPtr a' A.rTop tElem' (A.tWord 8)
(A.xPayloadOfRaw a' A.rTop xVec')
let xStart' = A.xShl a' A.tNat xIndex'
(A.xStoreSize2 a' tElem')
let xTop' = xVectorLength a' A.rTop xVec'
return $ A.xPokeBounded a' A.rTop tElem' xPayload' xStart' xTop' xValue'
_ -> Nothing
xVectorLength
:: a -> Type A.Name
-> Exp a A.Name -> Exp a A.Name
xVectorLength a rVec xVec
= let
xLengthObject
= A.xPromote a A.tNat (A.tWord 32)
$ A.xPeek a rVec (A.tWord 32)
(A.xCastPtr a rVec (A.tWord 32) A.tObj xVec)
(A.xNat a 4)
in A.xSub a A.tNat xLengthObject (A.xNat a 8)