@@ -180,9 +180,9 @@ primitives =
180 Prim (sUN "prim__registerPtr") (ty [PtrType, AType (ATInt ITNative)] ManagedPtrType) 2 (p_cantreduce)
180 Prim (sUN "prim__registerPtr") (ty [PtrType, AType (ATInt ITNative)] ManagedPtrType) 2 (p_cantreduce)
181 (2, LRegisterPtr) total,
181 (2, LRegisterPtr) total,
182 -- Buffers
182 -- Buffers
183− Prim (sUN "prim__allocate") (ty [AType (ATInt (ITFixed IT64))] BufferType) 1 (p_cantreduce)
183+ Prim (sUN "prim__allocate") (ty [AType (ATInt (ITFixed IT64))] BufferType) 1 (p_allocate)
184 (1, LAllocate) total,
184 (1, LAllocate) total,
185− Prim (sUN "prim__appendBuffer") (ty [BufferType, AType (ATInt (ITFixed IT64)), AType (ATInt (ITFixed IT64)), AType (ATInt (ITFixed IT64)), AType (ATInt (ITFixed IT64)), BufferType] BufferType) 6 (p_cantreduce)
185+ Prim (sUN "prim__appendBuffer") (ty [BufferType, AType (ATInt (ITFixed IT64)), AType (ATInt (ITFixed IT64)), AType (ATInt (ITFixed IT64)), AType (ATInt (ITFixed IT64)), BufferType] BufferType) 6 (p_appendBuffer)
186 (6, LAppendBuffer) partial
186 (6, LAppendBuffer) partial
187 ] ++ concatMap intOps [ITFixed IT8, ITFixed IT16, ITFixed IT32, ITFixed IT64, ITBig, ITNative, ITChar]
187 ] ++ concatMap intOps [ITFixed IT8, ITFixed IT16, ITFixed IT32, ITFixed IT64, ITBig, ITNative, ITChar]
188 ++ concatMap vecOps vecTypes
188 ++ concatMap vecOps vecTypes
@@ -291,10 +291,10 @@ fixedOps ity@(ITFixed _) =
291 thisTy = AType $ ATInt ity
291 thisTy = AType $ ATInt ity
292 appendFun en = Prim (sUN $ "prim__append" ++ tyName ++ show en)
292 appendFun en = Prim (sUN $ "prim__append" ++ tyName ++ show en)
293 (ty [BufferType, b64, b64, thisTy] BufferType)
293 (ty [BufferType, b64, b64, thisTy] BufferType)
294− 4 (p_cantreduce) (4, LAppend ity en) partial
294+ 4 (p_append en) (4, LAppend ity en) partial
295 peekFun en = Prim (sUN $ "prim__peek" ++ tyName ++ show en)
295 peekFun en = Prim (sUN $ "prim__peek" ++ tyName ++ show en)
296 (ty [BufferType, b64] thisTy)
296 (ty [BufferType, b64] thisTy)
297− 2 (p_cantreduce) (2, LPeek ity en) partial
297+ 2 (p_peek ity en) (2, LPeek ity en) partial
299mapHalf :: (V.Unbox a, V.Unbox b) => ((a, a) -> b) -> Vector a -> Vector b
299mapHalf :: (V.Unbox a, V.Unbox b) => ((a, a) -> b) -> Vector a -> Vector b
300mapHalf f xs = V.generate (V.length xs `div` 2) (\i -> f (xs V.! (i*2), xs V.! (i*2+1)))
300mapHalf f xs = V.generate (V.length xs `div` 2) (\i -> f (xs V.! (i*2), xs V.! (i*2+1)))
@@ -697,6 +697,53 @@ p_strCons _ = Nothing
697p_strRev [Str xs] = Just $ Str (reverse xs)
697p_strRev [Str xs] = Just $ Str (reverse xs)
698p_strRev _ = Nothing
698p_strRev _ = Nothing
700+p_allocate, p_appendBuffer :: [Const] -> Maybe Const
701+p_allocate [B64 _] = Just $ B8V V.empty
702+p_allocate n = Nothing
703+p_appendBuffer [B8V vect1, B64 size1, B64 count, B64 size2, B64 off, B8V vect2] =
704+ Just $ B8V (foldl (V.++)
705+ (V.slice 0 (fromIntegral size1) vect1)
706+ (replicate (fromIntegral count) (V.slice (fromIntegral off) (fromIntegral size2) vect2)))
707+p_appendBuffer n = Nothing
708+p_append :: Endianness -> [Const] -> Maybe Const
709+p_append en [B8V vect, B64 size, B64 count, val] =
710+ let bytes = case val of
712+ B16 x -> [fromIntegral x, fromIntegral (shiftR x 8)]
713+ B32 x -> map (fromIntegral . shiftR x) [0,8..24]
714+ B64 x -> map (fromIntegral . shiftR x) [0,8..56]
715+ in Just $ B8V (foldl V.snoc
716+ (V.slice 0 (fromIntegral size) vect)
717+ (concat (replicate (fromIntegral count)
721+p_append _ _ = Nothing
723+p_peek :: IntTy -> Endianness -> [Const] -> Maybe Const
724+p_peek (ITFixed IT8) _ [B8V vect, B64 offset] = Just $ B8 (vect V.! fromIntegral offset)
725+p_peek (ITFixed IT16) en [B8V vect, B64 offset] =
727+ in Just $ B16 (foldl (\full byte -> shiftL full 8 .|. fromIntegral (vect V.! (fromIntegral offset+byte)))
731+ _ -> reverse bytes))
732+p_peek (ITFixed IT32) en [B8V vect, B64 offset] =
734+ in Just $ B32 (foldl (\full byte -> shiftL full 8 .|. fromIntegral (vect V.! (fromIntegral offset+byte)))
738+ _ -> reverse bytes))
739+p_peek (ITFixed IT64) en [B8V vect, B64 offset] =
741+ in Just $ B64 (foldl (\full byte -> shiftL full 8 .|. fromIntegral (vect V.! (fromIntegral offset+byte)))
745+ _ -> reverse bytes))
746+p_peek _ _ _ = Nothing
700p_cantreduce :: a -> Maybe b
748p_cantreduce :: a -> Maybe b
701p_cantreduce _ = Nothing
749p_cantreduce _ = Nothing