@@ -190,17 +190,9 @@ primitives =
190 -- Managed pointer registration
190 -- Managed pointer registration
191 Prim (sUN "prim__registerPtr") (ty [PtrType, AType (ATInt ITNative)] ManagedPtrType) 2 (p_cantreduce)
191 Prim (sUN "prim__registerPtr") (ty [PtrType, AType (ATInt ITNative)] ManagedPtrType) 2 (p_cantreduce)
192 (2, LRegisterPtr) total,
192 (2, LRegisterPtr) total,
194− Prim (sUN "prim__allocate") (ty [AType (ATInt (ITFixed IT64))] BufferType) 1 (p_allocate)
195− (1, LAllocate) total,
196− 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)
197− (6, LAppendBuffer) partial,
198 Prim (sUN "prim__systemInfo") (ty [AType (ATInt ITNative)] StrType) 1 (p_cantreduce)
193 Prim (sUN "prim__systemInfo") (ty [AType (ATInt ITNative)] StrType) 1 (p_cantreduce)
199 (1, LSystemInfo) total
194 (1, LSystemInfo) total
200 ] ++ concatMap intOps [ITFixed IT8, ITFixed IT16, ITFixed IT32, ITFixed IT64, ITBig, ITNative, ITChar]
195 ] ++ concatMap intOps [ITFixed IT8, ITFixed IT16, ITFixed IT32, ITFixed IT64, ITBig, ITNative, ITChar]
201− ++ concatMap vecOps vecTypes
202− ++ concatMap fixedOps [ITFixed IT8, ITFixed IT16, ITFixed IT32, ITFixed IT64] -- ITNative, ITChar, ATFloat ] ++ vecTypes
203− ++ vecBitcasts vecTypes
205vecTypes :: [IntTy]
197vecTypes :: [IntTy]
206vecTypes = [ITVec IT8 16, ITVec IT16 8, ITVec IT32 4, ITVec IT64 2]
198vecTypes = [ITVec IT8 16, ITVec IT16 8, ITVec IT32 4, ITVec IT64 2]
@@ -270,48 +262,11 @@ vecCmps ity =
270 , iCmp ity "gt" True (bCmp ity (>)) LGt total
262 , iCmp ity "gt" True (bCmp ity (>)) LGt total
273−-- The TODOs in this function are documented as Issue #1617 on the issue tracker.
275−-- https://github.com/idris-lang/Idris-dev/issues/1617
276−vecOps :: IntTy -> [Prim]
277−vecOps ity@(ITVec elem count) =
278− [ Prim (sUN $ "prim__mk" ++ intTyName ity)
279− (ty (replicate count . AType . ATInt . ITFixed $ elem) (AType . ATInt $ ity))
280− count (mkVecCon elem count) (count, LMkVec elem count) total
281− , Prim (sUN $ "prim__index" ++ intTyName ity)
282− (ty [AType . ATInt $ ity, AType (ATInt (ITFixed IT32))] (AType . ATInt . ITFixed $ elem))
283− 2 (mkVecIndex count) (2, LIdxVec elem count) partial -- TODO: Ensure this reduces
284− , Prim (sUN $ "prim__update" ++ intTyName ity)
285− (ty [AType . ATInt $ ity, AType (ATInt (ITFixed IT32)), AType . ATInt . ITFixed $ elem]
286− (AType . ATInt $ ity))
287− 3 (mkVecUpdate elem count) (3, LUpdateVec elem count) partial -- TODO: Ensure this reduces
288− ] ++ intArith ity ++ vecCmps ity
290bitcastPrim :: ArithTy -> ArithTy -> (ArithTy -> [Const] -> Maybe Const) -> PrimFn -> Prim
265bitcastPrim :: ArithTy -> ArithTy -> (ArithTy -> [Const] -> Maybe Const) -> PrimFn -> Prim
291bitcastPrim from to impl prim =
266bitcastPrim from to impl prim =
292 Prim (sUN $ "prim__bitcast" ++ aTyName from ++ "_" ++ aTyName to) (ty [AType from] (AType to)) 1 (impl to)
267 Prim (sUN $ "prim__bitcast" ++ aTyName from ++ "_" ++ aTyName to) (ty [AType from] (AType to)) 1 (impl to)
293 (1, prim) total
268 (1, prim) total
295−vecBitcasts :: [IntTy] -> [Prim]
296−vecBitcasts tys = [bitcastPrim from to bitcastVec (LBitCast from to)
297− | from <- map ATInt vecTypes, to <- map ATInt vecTypes, from /= to]
299−fixedOps :: IntTy -> [Prim]
300−fixedOps ity@(ITFixed _) =
301− map appendFun endiannesses ++
302− map peekFun endiannesses
304− endiannesses = [ Native, LE, BE ]
305− tyName = intTyName ity
306− b64 = AType (ATInt (ITFixed IT64))
307− thisTy = AType $ ATInt ity
308− appendFun en = Prim (sUN $ "prim__append" ++ tyName ++ show en)
309− (ty [BufferType, b64, b64, thisTy] BufferType)
310− 4 (p_append en) (4, LAppend ity en) partial
311− peekFun en = Prim (sUN $ "prim__peek" ++ tyName ++ show en)
312− (ty [BufferType, b64] thisTy)
313− 2 (p_peek ity en) (2, LPeek ity en) partial
315mapHalf :: (V.Unbox a, V.Unbox b) => ((a, a) -> b) -> Vector a -> Vector b
270mapHalf :: (V.Unbox a, V.Unbox b) => ((a, a) -> b) -> Vector a -> Vector b
316mapHalf f xs = V.generate (V.length xs `div` 2) (\i -> f (xs V.! (i*2), xs V.! (i*2+1)))
271mapHalf f xs = V.generate (V.length xs `div` 2) (\i -> f (xs V.! (i*2), xs V.! (i*2+1)))
@@ -339,76 +294,6 @@ truncWord64 :: Bool -> Word64 -> Word32
339truncWord64 True x = fromIntegral (x `shiftR` 32)
294truncWord64 True x = fromIntegral (x `shiftR` 32)
340truncWord64 False x = fromIntegral x
295truncWord64 False x = fromIntegral x
342−bitcastVec :: ArithTy -> [Const] -> Maybe Const
343−bitcastVec (ATInt (ITVec IT8 n)) [x@(B8V v)]
344− | V.length v == n = Just x
345−bitcastVec (ATInt (ITVec IT16 n)) [B8V v]
346− | V.length v == n*2 = Just . B16V . mapHalf concatWord8 $ v
347−bitcastVec (ATInt (ITVec IT32 n)) [B8V v]
348− | V.length v == n*4 = Just . B32V . mapHalf concatWord16 . mapHalf concatWord8 $ v
349−bitcastVec (ATInt (ITVec IT64 n)) [B8V v]
350− | V.length v == n*8 = Just . B64V . mapHalf concatWord32 . mapHalf concatWord16 . mapHalf concatWord8 $ v
352−bitcastVec (ATInt (ITVec IT8 n)) [B16V v]
353− | V.length v * 2 == n = Just . B8V . mapDouble truncWord16 $ v
354−bitcastVec (ATInt (ITVec IT16 n)) [x@(B16V v)]
355− | V.length v == n = Just x
356−bitcastVec (ATInt (ITVec IT32 n)) [B16V v]
357− | V.length v == n*2 = Just . B32V . mapHalf concatWord16 $ v
358−bitcastVec (ATInt (ITVec IT64 n)) [B16V v]
359− | V.length v == n*4 = Just . B64V . mapHalf concatWord32 . mapHalf concatWord16 $ v
361−bitcastVec (ATInt (ITVec IT8 n)) [B32V v]
362− | V.length v * 4 == n = Just . B8V . mapDouble truncWord16 . mapDouble truncWord32 $ v
363−bitcastVec (ATInt (ITVec IT16 n)) [B32V v]
364− | V.length v * 2 == n = Just . B16V . mapDouble truncWord32 $ v
365−bitcastVec (ATInt (ITVec IT32 n)) [x@(B32V v)]
366− | V.length v == n = Just x
367−bitcastVec (ATInt (ITVec IT64 n)) [B32V v]
368− | V.length v == n*2 = Just . B64V . mapHalf concatWord32 $ v
370−bitcastVec (ATInt (ITVec IT8 n)) [B64V v]
371− | V.length v * 8 == n = Just . B8V . mapDouble truncWord16 . mapDouble truncWord32 . mapDouble truncWord64 $ v
372−bitcastVec (ATInt (ITVec IT16 n)) [B64V v]
373− | V.length v * 4 == n = Just . B16V . mapDouble truncWord32 . mapDouble truncWord64 $ v
374−bitcastVec (ATInt (ITVec IT32 n)) [B64V v]
375− | V.length v == n*2 = Just . B32V . mapDouble truncWord64 $ v
376−bitcastVec (ATInt (ITVec IT64 n)) [x@(B64V v)]
377− | V.length v == n = Just x
379−bitcastVec _ _ = Nothing
381−mkVecCon :: NativeTy -> Int -> [Const] -> Maybe Const
382−mkVecCon ity count args
383− | length ints == count = Just (mkVec ity count ints)
384− | otherwise = Nothing
387− mkVec :: NativeTy -> Int -> [Integer] -> Const
388− mkVec IT8 len values = B8V $ V.generate len (fromInteger . (values !!))
389− mkVec IT16 len values = B16V $ V.generate len (fromInteger . (values !!))
390− mkVec IT32 len values = B32V $ V.generate len (fromInteger . (values !!))
391− mkVec IT64 len values = B64V $ V.generate len (fromInteger . (values !!))
393−mkVecIndex count [vec, B32 i] = Just (idxVec vec)
395− idxVec :: Const -> Const
396− idxVec (B8V v) = B8 $ V.unsafeIndex v (fromIntegral i)
397− idxVec (B16V v) = B16 $ V.unsafeIndex v (fromIntegral i)
398− idxVec (B32V v) = B32 $ V.unsafeIndex v (fromIntegral i)
399− idxVec (B64V v) = B64 $ V.unsafeIndex v (fromIntegral i)
400−mkVecIndex _ _ = Nothing
402−mkVecUpdate ity count [vec, B32 i, newElem] = updateVec vec newElem
404− updateVec :: Const -> Const -> Maybe Const
405− updateVec (B8V v) (B8 e) = Just . B8V $ V.unsafeUpdate v (V.singleton (fromIntegral i, e))
406− updateVec (B16V v) (B16 e) = Just . B16V $ V.unsafeUpdate v (V.singleton (fromIntegral i, e))
407− updateVec (B32V v) (B32 e) = Just . B32V $ V.unsafeUpdate v (V.singleton (fromIntegral i, e))
408− updateVec (B64V v) (B64 e) = Just . B64V $ V.unsafeUpdate v (V.singleton (fromIntegral i, e))
409− updateVec _ _ = Nothing
410−mkVecUpdate _ _ _ = Nothing
412aTyName :: ArithTy -> String
297aTyName :: ArithTy -> String
413aTyName (ATInt t) = intTyName t
298aTyName (ATInt t) = intTyName t
414aTyName ATFloat = "Float"
299aTyName ATFloat = "Float"
@@ -470,18 +355,6 @@ bsrem (ITFixed IT64) [B64 x, B64 y]
470 = Just $ B64 (fromIntegral (fromIntegral x `rem` fromIntegral y :: Int64))
355 = Just $ B64 (fromIntegral (fromIntegral x `rem` fromIntegral y :: Int64))
471bsrem ITNative [I x, I y] = Just $ I (x `rem` y)
356bsrem ITNative [I x, I y] = Just $ I (x `rem` y)
472bsrem ITChar [Ch x, Ch y] = Just $ Ch (chr $ (ord x) `rem` (ord y))
357bsrem ITChar [Ch x, Ch y] = Just $ Ch (chr $ (ord x) `rem` (ord y))
473−bsrem (ITVec IT8 _) [B8V x, B8V y]
475− V.zipWith (\n d -> (fromIntegral (fromIntegral n `rem` fromIntegral d :: Int8))) x y
476−bsrem (ITVec IT16 _) [B16V x, B16V y]
478− V.zipWith (\n d -> (fromIntegral (fromIntegral n `rem` fromIntegral d :: Int16))) x y
479−bsrem (ITVec IT32 _) [B32V x, B32V y]
481− V.zipWith (\n d -> (fromIntegral (fromIntegral n `rem` fromIntegral d :: Int64))) x y
482−bsrem (ITVec IT64 _) [B64V x, B64V y]
484− V.zipWith (\n d -> (fromIntegral (fromIntegral n `rem` fromIntegral d :: Int64))) x y
485bsrem _ _ = Nothing
358bsrem _ _ = Nothing
487bsdiv :: IntTy -> [Const] -> Maybe Const
360bsdiv :: IntTy -> [Const] -> Maybe Const
@@ -496,18 +369,6 @@ bsdiv (ITFixed IT64) [B64 x, B64 y]
496 = Just $ B64 (fromIntegral (fromIntegral x `div` fromIntegral y :: Int64))
369 = Just $ B64 (fromIntegral (fromIntegral x `div` fromIntegral y :: Int64))
497bsdiv ITNative [I x, I y] = Just $ I (x `div` y)
370bsdiv ITNative [I x, I y] = Just $ I (x `div` y)
498bsdiv ITChar [Ch x, Ch y] = Just $ Ch (chr $ (ord x) `div` (ord y))
371bsdiv ITChar [Ch x, Ch y] = Just $ Ch (chr $ (ord x) `div` (ord y))
499−bsdiv (ITVec IT8 _) [B8V x, B8V y]
501− V.zipWith (\n d -> (fromIntegral (fromIntegral n `div` fromIntegral d :: Int8))) x y
502−bsdiv (ITVec IT16 _) [B16V x, B16V y]
504− V.zipWith (\n d -> (fromIntegral (fromIntegral n `div` fromIntegral d :: Int16))) x y
505−bsdiv (ITVec IT32 _) [B32V x, B32V y]
507− V.zipWith (\n d -> (fromIntegral (fromIntegral n `div` fromIntegral d :: Int64))) x y
508−bsdiv (ITVec IT64 _) [B64V x, B64V y]
510− V.zipWith (\n d -> (fromIntegral (fromIntegral n `div` fromIntegral d :: Int64))) x y
511bsdiv _ _ = Nothing
372bsdiv _ _ = Nothing
513bashr :: IntTy -> [Const] -> Maybe Const
374bashr :: IntTy -> [Const] -> Maybe Const
@@ -522,18 +383,6 @@ bashr (ITFixed IT64) [B64 x, B64 y]
522 = Just $ B64 (fromIntegral (fromIntegral x `shiftR` fromIntegral y :: Int64))
383 = Just $ B64 (fromIntegral (fromIntegral x `shiftR` fromIntegral y :: Int64))
523bashr ITNative [I x, I y] = Just $ I (x `shiftR` y)
384bashr ITNative [I x, I y] = Just $ I (x `shiftR` y)
524bashr ITChar [Ch x, Ch y] = Just $ Ch (chr $ (ord x) `shiftR` (ord y))
385bashr ITChar [Ch x, Ch y] = Just $ Ch (chr $ (ord x) `shiftR` (ord y))
525−bashr (ITVec IT8 _) [B8V x, B8V y]
527− V.zipWith (\n d -> (fromIntegral (fromIntegral n `shiftR` fromIntegral d :: Int8))) x y
528−bashr (ITVec IT16 _) [B16V x, B16V y]
530− V.zipWith (\n d -> (fromIntegral (fromIntegral n `shiftR` fromIntegral d :: Int16))) x y
531−bashr (ITVec IT32 _) [B32V x, B32V y]
533− V.zipWith (\n d -> (fromIntegral (fromIntegral n `shiftR` fromIntegral d :: Int64))) x y
534−bashr (ITVec IT64 _) [B64V x, B64V y]
536− V.zipWith (\n d -> (fromIntegral (fromIntegral n `shiftR` fromIntegral d :: Int64))) x y
537bashr _ _ = Nothing
386bashr _ _ = Nothing
539bUn :: IntTy -> (forall a. Bits a => a -> a) -> [Const] -> Maybe Const
388bUn :: IntTy -> (forall a. Bits a => a -> a) -> [Const] -> Maybe Const
@@ -544,10 +393,6 @@ bUn (ITFixed IT64) op [B64 x] = Just $ B64 (op x)
544bUn ITBig op [BI x] = Just $ BI (op x)
393bUn ITBig op [BI x] = Just $ BI (op x)
545bUn ITNative op [I x] = Just $ I (op x)
394bUn ITNative op [I x] = Just $ I (op x)
546bUn ITChar op [Ch x] = Just $ Ch (chr $ op (ord x))
395bUn ITChar op [Ch x] = Just $ Ch (chr $ op (ord x))
547−bUn (ITVec IT8 _) op [B8V x] = Just . B8V $ V.map op x
548−bUn (ITVec IT16 _) op [B16V x] = Just . B16V $ V.map op x
549−bUn (ITVec IT32 _) op [B32V x] = Just . B32V $ V.map op x
550−bUn (ITVec IT64 _) op [B64V x] = Just . B64V $ V.map op x
551bUn _ _ _ = Nothing
396bUn _ _ _ = Nothing
553bitBin :: IntTy -> (forall a. (Bits a, Integral a) => a -> a -> a) -> [Const] -> Maybe Const
398bitBin :: IntTy -> (forall a. (Bits a, Integral a) => a -> a -> a) -> [Const] -> Maybe Const
@@ -558,10 +403,6 @@ bitBin (ITFixed IT64) op [B64 x, B64 y] = Just $ B64 (op x y)
558bitBin ITBig op [BI x, BI y] = Just $ BI (op x y)
403bitBin ITBig op [BI x, BI y] = Just $ BI (op x y)
559bitBin ITNative op [I x, I y] = Just $ I (op x y)
404bitBin ITNative op [I x, I y] = Just $ I (op x y)
560bitBin ITChar op [Ch x, Ch y] = Just $ Ch (chr $ op (ord x) (ord y))
405bitBin ITChar op [Ch x, Ch y] = Just $ Ch (chr $ op (ord x) (ord y))
561−bitBin (ITVec IT8 _) op [B8V x, B8V y] = Just . B8V $ V.zipWith op x y
562−bitBin (ITVec IT16 _) op [B16V x, B16V y] = Just . B16V $ V.zipWith op x y
563−bitBin (ITVec IT32 _) op [B32V x, B32V y] = Just . B32V $ V.zipWith op x y
564−bitBin (ITVec IT64 _) op [B64V x, B64V y] = Just . B64V $ V.zipWith op x y
565bitBin _ _ _ = Nothing
406bitBin _ _ _ = Nothing
567bCmp :: IntTy -> (forall a. (Integral a, Ord a) => a -> a -> Bool) -> [Const] -> Maybe Const
408bCmp :: IntTy -> (forall a. (Integral a, Ord a) => a -> a -> Bool) -> [Const] -> Maybe Const
@@ -572,14 +413,6 @@ bCmp (ITFixed IT64) op [B64 x, B64 y] = Just $ I (if (op x y) then 1 else 0)
572bCmp ITBig op [BI x, BI y] = Just $ I (if (op x y) then 1 else 0)
413bCmp ITBig op [BI x, BI y] = Just $ I (if (op x y) then 1 else 0)
573bCmp ITNative op [I x, I y] = Just $ I (if (op x y) then 1 else 0)
414bCmp ITNative op [I x, I y] = Just $ I (if (op x y) then 1 else 0)
574bCmp ITChar op [Ch x, Ch y] = Just $ I (if (op (ord x) (ord y)) then 1 else 0)
415bCmp ITChar op [Ch x, Ch y] = Just $ I (if (op (ord x) (ord y)) then 1 else 0)
575−bCmp (ITVec IT8 _) op [B8V x, B8V y]
576− = Just . B8V . V.map (\b -> if b then -1 else 0) $ V.zipWith op x y
577−bCmp (ITVec IT16 _) op [B16V x, B16V y]
578− = Just . B16V . V.map (\b -> if b then -1 else 0) $ V.zipWith op x y
579−bCmp (ITVec IT32 _) op [B32V x, B32V y]
580− = Just . B32V . V.map (\b -> if b then -1 else 0) $ V.zipWith op x y
581−bCmp (ITVec IT64 _) op [B64V x, B64V y]
582− = Just . B64V . V.map (\b -> if b then -1 else 0) $ V.zipWith op x y
583bCmp _ _ _ = Nothing
416bCmp _ _ _ = Nothing
@@ -717,53 +550,5 @@ p_strCons _ = Nothing
717p_strRev [Str xs] = Just $ Str (reverse xs)
550p_strRev [Str xs] = Just $ Str (reverse xs)
718p_strRev _ = Nothing
551p_strRev _ = Nothing
720−p_allocate, p_appendBuffer :: [Const] -> Maybe Const
721−p_allocate [B64 _] = Just $ B8V V.empty
722−p_allocate n = Nothing
723−p_appendBuffer [B8V vect1, B64 size1, B64 count, B64 size2, B64 off, B8V vect2] =
724− Just $ B8V (foldl (V.++)
725− (V.slice 0 (fromIntegral size1) vect1)
726− (replicate (fromIntegral count) (V.slice (fromIntegral off) (fromIntegral size2) vect2)))
727−p_appendBuffer n = Nothing
728−p_append :: Endianness -> [Const] -> Maybe Const
729−p_append en [B8V vect, B64 size, B64 count, val] =
730− let bytes = case val of
732− B16 x -> [fromIntegral x, fromIntegral (shiftR x 8)]
733− B32 x -> map (fromIntegral . shiftR x) [0,8..24]
734− B64 x -> map (fromIntegral . shiftR x) [0,8..56]
735− in Just $ B8V (foldl V.snoc
736− (V.slice 0 (fromIntegral size) vect)
737− (concat (replicate (fromIntegral count)
741−p_append _ _ = Nothing
743−p_peek :: IntTy -> Endianness -> [Const] -> Maybe Const
744−p_peek (ITFixed IT8) _ [B8V vect, B64 offset] = Just $ B8 (vect V.! fromIntegral offset)
745−p_peek (ITFixed IT16) en [B8V vect, B64 offset] =
747− in Just $ B16 (foldl (\full byte -> shiftL full 8 .|. fromIntegral (vect V.! (fromIntegral offset+byte)))
751− _ -> reverse bytes))
752−p_peek (ITFixed IT32) en [B8V vect, B64 offset] =
754− in Just $ B32 (foldl (\full byte -> shiftL full 8 .|. fromIntegral (vect V.! (fromIntegral offset+byte)))
758− _ -> reverse bytes))
759−p_peek (ITFixed IT64) en [B8V vect, B64 offset] =
761− in Just $ B64 (foldl (\full byte -> shiftL full 8 .|. fromIntegral (vect V.! (fromIntegral offset+byte)))
765− _ -> reverse bytes))
766−p_peek _ _ _ = Nothing
768p_cantreduce :: a -> Maybe b
553p_cantreduce :: a -> Maybe b
769p_cantreduce _ = Nothing
554p_cantreduce _ = Nothing