Debian Codegraph @

src-ghc/Clash/GHC/Evaluator/Primitive.hs

raw
1 {-|
2 Copyright : (C) 2013-2016, University of Twente,
3 2016-2017, Myrtle Software Ltd,
4 2017-2022, Google Inc.,
5 2017-2024, QBayLogic B.V.
6 License : BSD2 (see the file LICENSE)
7 Maintainer : QBayLogic B.V. <[email protected]>
8 -}
9
10 {-# LANGUAGE CPP #-}
11 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
12 {-# LANGUAGE MagicHash #-}
13 {-# LANGUAGE NamedFieldPuns #-}
14 {-# LANGUAGE OverloadedStrings #-}
15 {-# LANGUAGE RankNTypes #-}
16 {-# LANGUAGE TemplateHaskell #-}
17 {-# LANGUAGE UnboxedTuples #-}
18
19 {-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
20
21 #include "MachDeps.h"
22
23 module Clash.GHC.Evaluator.Primitive
24 ( ghcPrimStep
25 , ghcPrimUnwind
26 , isUndefinedPrimVal
27 , isUndefinedXPrimVal
28 ) where
29
30 import Control.DeepSeq (force)
31 import Control.Exception (ArithException(..), Exception, tryJust, evaluate)
32 import Control.Monad.State.Strict (State, MonadState)
33 import qualified Control.Monad.State.Strict as State
34 import Control.Monad.Trans.Except (runExcept)
35 import Data.Binary.IEEE754 (doubleToWord, floatToWord, wordToDouble, wordToFloat)
36 import Data.Bits
37 import qualified Data.ByteString.Internal as BS
38 import Data.Char (chr,ord)
39 import qualified Data.Either as Either
40 import Data.Maybe (fromMaybe, mapMaybe)
41 import qualified Data.List as List
42 import qualified Data.Primitive.ByteArray as BA
43 import Data.Proxy (Proxy)
44 import Data.Reflection (reifyNat)
45 import Data.Text (Text)
46 import qualified Data.Text as Text
47 import Data.Text.Extra (showt)
48 import GHC.Exts (IsList(..))
49 import GHC.Float
50 import GHC.Int
51 import GHC.Integer
52 (decodeDoubleInteger,encodeDoubleInteger,compareInteger,orInteger,andInteger,
53 xorInteger,complementInteger,absInteger,signumInteger)
54 #if MIN_VERSION_base(4,16,0)
55 import GHC.Num.Integer (Integer (..), integerEncodeFloat#)
56 #elif MIN_VERSION_base(4,15,0)
57 import GHC.Num.Integer
58 (Integer (..), integerEncodeFloat#, integerToFloat#, integerToDouble#)
59 #else
60 import GHC.Integer.GMP.Internals
61 (Integer (..), BigNat (..))
62 #endif
63 #if MIN_VERSION_base(4,15,0)
64 import GHC.Num.Natural (naturalSubUnsafe)
65 #endif
66 import GHC.Natural
67 import GHC.ForeignPtr
68 import GHC.Prim
69 import GHC.Real (Ratio (..))
70 import GHC.TypeLits (KnownNat)
71 import GHC.Types (IO (..))
72 import GHC.Word
73 import System.IO.Unsafe (unsafeDupablePerformIO)
74 #if MIN_VERSION_ghc(9,4,0)
75 import Data.Bifunctor (first)
76 import qualified Data.Text.Array as Text
77 import qualified Data.Text.Internal as Text
78 #endif
79
80 #if MIN_VERSION_ghc(9,0,0)
81 import GHC.Types.Basic (Boxity (..))
82 import GHC.Types.Name (getSrcSpan, nameOccName, occNameString)
83 import GHC.Builtin.Names (trueDataConKey, falseDataConKey)
84 import qualified GHC.Core.TyCon as TyCon
85 import GHC.Builtin.Types (tupleTyCon)
86 #else
87 import BasicTypes (Boxity (..))
88 import Name (getSrcSpan, nameOccName, occNameString)
89 import PrelNames (trueDataConKey, falseDataConKey)
90 import qualified TyCon
91 import TysWiredIn (tupleTyCon)
92 #endif
93
94 import Clash.Class.BitPack (pack,unpack)
95 import Clash.Core.DataCon (DataCon (..))
96 import Clash.Core.Evaluator.Types
97 import Clash.Core.HasType (piResultTys, applyTypeToArgs)
98 import Clash.Core.Literal (Literal (..))
99 import Clash.Core.Name
100 (Name (..), NameSort (..), mkUnsafeSystemName)
101 import Clash.Core.Pretty (showPpr)
102 import Clash.Core.Term
103 (IsMultiPrim (..), Pat (..), PrimInfo (..), Term (..), WorkInfo (..), mkApps,
104 PrimUnfolding(..), collectArgs)
105 import Clash.Core.Type
106 (Type (..), ConstTy (..), LitTy (..), TypeView (..), mkFunTy, mkTyConApp,
107 splitFunForallTy, tyView)
108 import Clash.Core.TyCon
109 (TyConMap, TyConName, tyConDataCons)
110 import Clash.Core.TysPrim
111 import Clash.Core.Util
112 (mkRTree,mkVec,tyNatSize,dataConInstArgTys,primCo, mkSelectorCase,undefinedPrims,
113 undefinedXPrims)
114 import Clash.Core.Var (mkLocalId, mkTyVar)
115 import qualified Clash.Data.UniqMap as UniqMap
116 import Clash.Debug
117 import Clash.GHC.GHC2Core (modNameM)
118 import Clash.Unique (fromGhcUnique)
119 import Clash.Util
120 (MonadUnique (..), clogBase, flogBase, curLoc, namePat)
121 import Clash.Util.Supply (Supply,freshId)
122 import Clash.Normalize.PrimitiveReductions
123 (typeNatMul, typeNatSub, typeNatAdd, vecLastPrim, vecInitPrim, vecHeadPrim,
124 vecTailPrim, mkVecCons, mkVecNil)
125
126 import qualified Clash.Normalize.Primitives as NP
127 import Clash.Promoted.Nat.Unsafe (unsafeSNat)
128 import qualified Clash.Sized.Internal.BitVector as BitVector
129 import qualified Clash.Sized.Internal.Signed as Signed
130 import qualified Clash.Sized.Internal.Unsigned as Unsigned
131 import Clash.Sized.Internal.BitVector(BitVector(..), Bit(..))
132 import Clash.Sized.Internal.Signed (Signed (..))
133 import Clash.Sized.Internal.Unsigned (Unsigned (..))
134 import Clash.XException (isX)
135
136 import {-# SOURCE #-} Clash.GHC.Evaluator
137
138 import qualified Clash.Annotations.BitRepresentation.Deriving
139 import qualified Clash.Class.BitPack.Internal
140 import qualified Clash.Class.Exp
141 import qualified Clash.Promoted.Nat
142 import qualified Clash.Sized.Internal.BitVector
143 import qualified Clash.Sized.Internal.Index
144 import qualified Clash.Sized.Internal.Signed
145 import qualified Clash.Sized.Internal.Unsigned
146 import qualified Clash.Sized.RTree
147 import qualified Clash.Sized.Vector
148 import qualified GHC.Base
149 import qualified GHC.Classes
150 import qualified GHC.CString
151 import qualified GHC.TypeLits
152 import qualified GHC.TypeNats
153 import qualified GHC.Types
154
155 #if MIN_VERSION_base(4,15,0)
156 import qualified GHC.Num
157 import qualified GHC.Num.Integer
158 #endif
159
160 #if MIN_VERSION_ghc_prim(0,12,0)
161 import qualified GHC.PrimopWrappers
162 #endif
163
164 #if !MIN_VERSION_base(4,15,0)
165 import qualified GHC.Integer.Logarithms
166 #endif
167
168 isUndefinedPrimVal :: Value -> Bool
169 isUndefinedPrimVal (PrimVal (PrimInfo{primName}) _ _) =
170 primName `elem` undefinedPrims
171 isUndefinedPrimVal _ = False
172
173 isUndefinedXPrimVal :: Value -> Bool
174 isUndefinedXPrimVal (PrimVal (PrimInfo{primName}) _ _) =
175 primName `elem` undefinedXPrims
176 isUndefinedXPrimVal _ = False
177
178 -- | Evaluation of primitive operations.
179 ghcPrimUnwind :: PrimUnwind
180 ghcPrimUnwind tcm p tys vs v [] m
181 | primName p `elem` [ showt 'Clash.Sized.Internal.Index.fromInteger#
182 , showt 'GHC.CString.unpackCString#
183 , showt 'NP.removedArg
184 , showt ''MutableByteArray#
185 , showt 'NP.undefined
186 , showt 'NP.undefinedX
187 ]
188 -- The above primitives are actually values, and not operations.
189 = ghcUnwind (PrimVal p tys (vs ++ [v])) m tcm
190 | primName p == showt 'Clash.Sized.Internal.BitVector.fromInteger#
191 = case (vs,v) of
192 ([naturalLiteral -> Just n,mask], integerLiteral -> Just i) ->
193 ghcUnwind (PrimVal p tys [Lit (NaturalLiteral n), mask, Lit (IntegerLiteral (wrapUnsigned n i))]) m tcm
194 _ -> error ($(curLoc) ++ "Internal error" ++ show (vs,v))
195 | primName p == showt 'Clash.Sized.Internal.BitVector.fromInteger##
196 = case (vs,v) of
197 ([mask], integerLiteral -> Just i) ->
198 ghcUnwind (PrimVal p tys [mask, Lit (IntegerLiteral (wrapUnsigned 1 i))]) m tcm
199 _ -> error ($(curLoc) ++ "Internal error" ++ show (vs,v))
200 | primName p == showt 'Clash.Sized.Internal.Signed.fromInteger#
201 = case (vs,v) of
202 ([naturalLiteral -> Just n],integerLiteral -> Just i) ->
203 ghcUnwind (PrimVal p tys [Lit (NaturalLiteral n), Lit (IntegerLiteral (wrapSigned n i))]) m tcm
204 _ -> error ($(curLoc) ++ "Internal error" ++ show (vs,v))
205 | primName p == showt 'Clash.Sized.Internal.Unsigned.fromInteger#
206 = case (vs,v) of
207 ([naturalLiteral -> Just n],integerLiteral -> Just i) ->
208 ghcUnwind (PrimVal p tys [Lit (NaturalLiteral n), Lit (IntegerLiteral (wrapUnsigned n i))]) m tcm
209 _ -> error ($(curLoc) ++ "Internal error" ++ show (vs,v))
210 | isUndefinedPrimVal v
211 = let tyArgs = map Right tys
212 tmArgs = map (Left . valToTerm) (vs ++ [v])
213 in Just $ flip setTerm m $ TyApp (Prim NP.undefined) $
214 applyTypeToArgs (Prim p) tcm (primType p) (tyArgs ++ tmArgs)
215 | isUndefinedXPrimVal v
216 = let tyArgs = map Right tys
217 tmArgs = map (Left . valToTerm) (vs ++ [v])
218 in Just $ flip setTerm m $ TyApp (Prim NP.undefinedX) $
219 applyTypeToArgs (Prim p) tcm (primType p) (tyArgs ++ tmArgs)
220 | otherwise
221 = ghcPrimStep tcm (forcePrims m) p tys (vs ++ [v]) m
222
223 ghcPrimUnwind tcm p tys vs v [e] m0
224 -- Note [Lazy primitives]
225 -- ~~~~~~~~~~~~~~~~~~~~~~
226 --
227 -- Primitives are usually considered undefined when one of their arguments is
228 -- (unless they're unused). _Some_ primitives can still yield a result even
229 -- though one of their arguments is undefined. It turns out that all primitives
230 -- exhibiting this property happen to be "lazy" in their last argument. Thus,
231 -- all the cases can be covered by a match on [e] and their names:
232 | primName p `elem` [ showt 'Clash.Sized.Vector.lazyV
233 , showt 'Clash.Sized.Vector.replicate
234 , "Clash.Sized.Vector.replace_int"
235 , showt '(GHC.Classes.&&)
236 , showt '(GHC.Classes.||)
237 , showt 'BitVector.xToBV
238 , "Clash.Sized.Vector.imap_go"
239 ]
240 = if isUndefinedPrimVal v then
241 let tyArgs = map Right tys
242 tmArgs = map (Left . valToTerm) (vs ++ [v]) ++ [Left e]
243 in Just $ flip setTerm m0 $ TyApp (Prim NP.undefined) $
244 applyTypeToArgs (Prim p) tcm (primType p) (tyArgs ++ tmArgs)
245 else
246 let (m1,i) = newLetBinding tcm m0 e
247 in ghcPrimStep tcm (forcePrims m0) p tys (vs ++ [v,Suspend (Var i)]) m1
248
249 ghcPrimUnwind tcm p tys vs (collectValueTicks -> (v, ts)) (e:es) m
250 | isUndefinedPrimVal v
251 = let tyArgs = map Right tys
252 tmArgs = map (Left . valToTerm) (vs ++ [v]) ++ map Left (e:es)
253 in Just $ flip setTerm m $ TyApp (Prim NP.undefined) $
254 applyTypeToArgs (Prim p) tcm (primType p) (tyArgs ++ tmArgs)
255 | otherwise
256 = Just . setTerm e $ stackPush (PrimApply p tys (vs ++ [foldr TickValue v ts]) es) m
257
258 newtype PrimEvalMonad a = PEM (State Supply a)
259 deriving (Functor, Applicative, Monad, MonadState Supply)
260
261 instance MonadUnique PrimEvalMonad where
262 getUniqueM = PEM $ State.state (\s -> case freshId s of (!i,!s') -> (i,s'))
263
264 runPEM :: PrimEvalMonad a -> Supply -> (a, Supply)
265 runPEM (PEM m) = State.runState m
266
267 ghcPrimStep :: PrimStep
268 ghcPrimStep tcm isSubj pInfo tys args mach = case primName pInfo of
269 -----------------
270 -- GHC.Prim.Char#
271 -----------------
272 $(namePat 'GHC.Prim.gtChar#) | Just (i,j) <- charLiterals args
273 -> reduce (boolToIntLiteral (i > j))
274 $(namePat 'GHC.Prim.geChar#) | Just (i,j) <- charLiterals args
275 -> reduce (boolToIntLiteral (i >= j))
276 $(namePat 'GHC.Prim.eqChar#) | Just (i,j) <- charLiterals args
277 -> reduce (boolToIntLiteral (i == j))
278 $(namePat 'GHC.Prim.neChar#) | Just (i,j) <- charLiterals args
279 -> reduce (boolToIntLiteral (i /= j))
280 $(namePat 'GHC.Prim.ltChar#) | Just (i,j) <- charLiterals args
281 -> reduce (boolToIntLiteral (i < j))
282 $(namePat 'GHC.Prim.leChar#) | Just (i,j) <- charLiterals args
283 -> reduce (boolToIntLiteral (i <= j))
284 $(namePat 'GHC.Prim.ord#) | [i] <- charLiterals' args
285 -> reduce (integerToIntLiteral (toInteger $ ord i))
286
287 ----------------
288 -- GHC.Prim.Int#
289 ----------------
290 $(namePat '(GHC.Prim.+#)) | Just (i,j) <- intLiterals args
291 -> reduce (integerToIntLiteral (i+j))
292 $(namePat '(GHC.Prim.-#)) | Just (i,j) <- intLiterals args
293 -> reduce (integerToIntLiteral (i-j))
294 $(namePat '(GHC.Prim.*#)) | Just (i,j) <- intLiterals args
295 -> reduce (integerToIntLiteral (i*j))
296
297 $(namePat 'GHC.Prim.mulIntMayOflo#) | Just (i,j) <- intLiterals args
298 -> let !(I# a) = fromInteger i
299 !(I# b) = fromInteger j
300 c :: Int#
301 c = mulIntMayOflo# a b
302 in reduce (integerToIntLiteral (toInteger $ I# c))
303
304 $(namePat 'GHC.Prim.quotInt#) | Just (i,j) <- intLiterals args
305 -> reduce $ catchDivByZero (integerToIntLiteral (i `quot` j))
306 $(namePat 'GHC.Prim.remInt#) | Just (i,j) <- intLiterals args
307 -> reduce $ catchDivByZero (integerToIntLiteral (i `rem` j))
308 $(namePat 'GHC.Prim.quotRemInt#) | Just (i,j) <- intLiterals args
309 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
310 (Just tupTc) = UniqMap.lookup tupTcNm tcm
311 [tupDc] = tyConDataCons tupTc
312 (q,r) = quotRem i j
313 ret = mkApps (Data tupDc) (map Right tyArgs ++
314 [Left $ catchDivByZero (integerToIntLiteral q)
315 ,Left $ catchDivByZero (integerToIntLiteral r)])
316 in reduce ret
317
318 $(namePat 'GHC.Prim.andI#) | Just (i,j) <- intLiterals args
319 -> reduce (integerToIntLiteral (i .&. j))
320 $(namePat 'GHC.Prim.orI#) | Just (i,j) <- intLiterals args
321 -> reduce (integerToIntLiteral (i .|. j))
322 $(namePat 'GHC.Prim.xorI#) | Just (i,j) <- intLiterals args
323 -> reduce (integerToIntLiteral (i `xor` j))
324 $(namePat 'GHC.Prim.notI#) | [i] <- intLiterals' args
325 -> reduce (integerToIntLiteral (complement i))
326
327 $(namePat 'GHC.Prim.negateInt#)
328 | [Lit (IntLiteral i)] <- args
329 -> reduce (integerToIntLiteral (negate i))
330
331 $(namePat 'GHC.Prim.addIntC#) | Just (i,j) <- intLiterals args
332 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
333 (Just tupTc) = UniqMap.lookup tupTcNm tcm
334 [tupDc] = tyConDataCons tupTc
335 !(I# a) = fromInteger i
336 !(I# b) = fromInteger j
337 !(# d, c #) = addIntC# a b
338 in reduce $
339 mkApps (Data tupDc) (map Right tyArgs ++
340 [ Left (Literal . IntLiteral . toInteger $ I# d)
341 , Left (Literal . IntLiteral . toInteger $ I# c)])
342 $(namePat 'GHC.Prim.subIntC#) | Just (i,j) <- intLiterals args
343 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
344 (Just tupTc) = UniqMap.lookup tupTcNm tcm
345 [tupDc] = tyConDataCons tupTc
346 !(I# a) = fromInteger i
347 !(I# b) = fromInteger j
348 !(# d, c #) = subIntC# a b
349 in reduce $
350 mkApps (Data tupDc) (map Right tyArgs ++
351 [ Left (Literal . IntLiteral . toInteger $ I# d)
352 , Left (Literal . IntLiteral . toInteger $ I# c)])
353
354 $(namePat '(GHC.Prim.>#)) | Just (i,j) <- intLiterals args
355 -> reduce (boolToIntLiteral (i > j))
356 $(namePat '(GHC.Prim.>=#)) | Just (i,j) <- intLiterals args
357 -> reduce (boolToIntLiteral (i >= j))
358 $(namePat '(GHC.Prim.==#)) | Just (i,j) <- intLiterals args
359 -> reduce (boolToIntLiteral (i == j))
360 $(namePat '(GHC.Prim./=#)) | Just (i,j) <- intLiterals args
361 -> reduce (boolToIntLiteral (i /= j))
362 $(namePat '(GHC.Prim.<#))| Just (i,j) <- intLiterals args
363 -> reduce (boolToIntLiteral (i < j))
364 $(namePat '(GHC.Prim.<=#)) | Just (i,j) <- intLiterals args
365 -> reduce (boolToIntLiteral (i <= j))
366
367 $(namePat 'GHC.Prim.chr#) | [i] <- intLiterals' args
368 -> reduce (charToCharLiteral (chr $ fromInteger i))
369
370 $(namePat 'GHC.Prim.int2Word#)
371 | [Lit (IntLiteral i)] <- args
372 -> reduce . Literal . WordLiteral . toInteger $ (fromInteger :: Integer -> Word) i -- for overflow behavior
373
374 $(namePat 'GHC.Prim.int2Float#)
375 | [Lit (IntLiteral i)] <- args
376 -> reduce . Literal . FloatLiteral . floatToWord $ fromInteger i
377 $(namePat 'GHC.Prim.int2Double#)
378 | [Lit (IntLiteral i)] <- args
379 -> reduce . Literal . DoubleLiteral . doubleToWord $ fromInteger i
380
381 $(namePat 'GHC.Prim.word2Float#)
382 | [Lit (WordLiteral i)] <- args
383 -> reduce . Literal . FloatLiteral . floatToWord $ fromInteger i
384 $(namePat 'GHC.Prim.word2Double#)
385 | [Lit (WordLiteral i)] <- args
386 -> reduce . Literal . DoubleLiteral . doubleToWord $ fromInteger i
387
388 $(namePat 'GHC.Prim.uncheckedIShiftL#)
389 | [ Lit (IntLiteral i)
390 , Lit (IntLiteral s)
391 ] <- args
392 -> reduce (integerToIntLiteral (i `shiftL` fromInteger s))
393 $(namePat 'GHC.Prim.uncheckedIShiftRA#)
394 | [ Lit (IntLiteral i)
395 , Lit (IntLiteral s)
396 ] <- args
397 -> reduce (integerToIntLiteral (i `shiftR` fromInteger s))
398 $(namePat 'GHC.Prim.uncheckedIShiftRL#) | Just (i,j) <- intLiterals args
399 -> let !(I# a) = fromInteger i
400 !(I# b) = fromInteger j
401 c :: Int#
402 c = uncheckedIShiftRL# a b
403 in reduce (integerToIntLiteral (toInteger $ I# c))
404
405 -----------------
406 -- GHC.Prim.Word#
407 -----------------
408 $(namePat 'GHC.Prim.plusWord#) | Just (i,j) <- wordLiterals args
409 -> reduce (integerToWordLiteral (i+j))
410
411 $(namePat 'GHC.Prim.subWordC#) | Just (i,j) <- wordLiterals args
412 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
413 (Just tupTc) = UniqMap.lookup tupTcNm tcm
414 [tupDc] = tyConDataCons tupTc
415 !(W# a) = fromInteger i
416 !(W# b) = fromInteger j
417 !(# d, c #) = subWordC# a b
418 in reduce $
419 mkApps (Data tupDc) (map Right tyArgs ++
420 [ Left (Literal . WordLiteral . toInteger $ W# d)
421 , Left (Literal . IntLiteral . toInteger $ I# c)])
422
423 $(namePat 'GHC.Prim.plusWord2#) | Just (i,j) <- wordLiterals args
424 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
425 (Just tupTc) = UniqMap.lookup tupTcNm tcm
426 [tupDc] = tyConDataCons tupTc
427 !(W# a) = fromInteger i
428 !(W# b) = fromInteger j
429 !(# h', l #) = plusWord2# a b
430 in reduce $
431 mkApps (Data tupDc) (map Right tyArgs ++
432 [ Left (Literal . WordLiteral . toInteger $ W# h')
433 , Left (Literal . WordLiteral . toInteger $ W# l)])
434
435 $(namePat 'GHC.Prim.minusWord#) | Just (i,j) <- wordLiterals args
436 -> reduce (integerToWordLiteral (i-j))
437 $(namePat 'GHC.Prim.timesWord#) | Just (i,j) <- wordLiterals args
438 -> reduce (integerToWordLiteral (i*j))
439
440 $(namePat 'GHC.Prim.timesWord2#) | Just (i,j) <- wordLiterals args
441 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
442 (Just tupTc) = UniqMap.lookup tupTcNm tcm
443 [tupDc] = tyConDataCons tupTc
444 !(W# a) = fromInteger i
445 !(W# b) = fromInteger j
446 !(# h', l #) = timesWord2# a b
447 in reduce $
448 mkApps (Data tupDc) (map Right tyArgs ++
449 [ Left (Literal . WordLiteral . toInteger $ W# h')
450 , Left (Literal . WordLiteral . toInteger $ W# l)])
451
452 $(namePat 'GHC.Prim.quotWord#) | Just (i,j) <- wordLiterals args
453 -> reduce $ catchDivByZero (integerToWordLiteral (i `quot` j))
454 $(namePat 'GHC.Prim.remWord#) | Just (i,j) <- wordLiterals args
455 -> reduce $ catchDivByZero (integerToWordLiteral (i `rem` j))
456 $(namePat 'GHC.Prim.quotRemWord#) | Just (i,j) <- wordLiterals args
457 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
458 (Just tupTc) = UniqMap.lookup tupTcNm tcm
459 [tupDc] = tyConDataCons tupTc
460 (q,r) = quotRem i j
461 ret = mkApps (Data tupDc) (map Right tyArgs ++
462 [Left $ catchDivByZero (integerToWordLiteral q)
463 ,Left $ catchDivByZero (integerToWordLiteral r)])
464 in reduce ret
465 $(namePat 'GHC.Prim.quotRemWord2#) | [i,j,k'] <- wordLiterals' args
466 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
467 (Just tupTc) = UniqMap.lookup tupTcNm tcm
468 [tupDc] = tyConDataCons tupTc
469 !(W# a) = fromInteger i
470 !(W# b) = fromInteger j
471 !(W# c) = fromInteger k'
472 !(# x, y #) = quotRemWord2# a b c
473 in reduce $
474 mkApps (Data tupDc) (map Right tyArgs ++
475 [ Left $ catchDivByZero (Literal . WordLiteral . toInteger $ W# x)
476 , Left $ catchDivByZero (Literal . WordLiteral . toInteger $ W# y)])
477
478 $(namePat 'GHC.Prim.and#) | Just (i,j) <- wordLiterals args
479 -> reduce (integerToWordLiteral (i .&. j))
480 $(namePat 'GHC.Prim.or#) | Just (i,j) <- wordLiterals args
481 -> reduce (integerToWordLiteral (i .|. j))
482 $(namePat 'GHC.Prim.xor#) | Just (i,j) <- wordLiterals args
483 -> reduce (integerToWordLiteral (i `xor` j))
484 $(namePat 'GHC.Prim.not#) | [i] <- wordLiterals' args
485 -> reduce (integerToWordLiteral (complement i))
486
487 $(namePat 'GHC.Prim.uncheckedShiftL#)
488 | [ Lit (WordLiteral w)
489 , Lit (IntLiteral i)
490 ] <- args
491 -> reduce (Literal (WordLiteral (w `shiftL` fromInteger i)))
492 $(namePat 'GHC.Prim.uncheckedShiftRL#)
493 | [ Lit (WordLiteral w)
494 , Lit (IntLiteral i)
495 ] <- args
496 -> reduce (Literal (WordLiteral (w `shiftR` fromInteger i)))
497
498 $(namePat 'GHC.Prim.word2Int#)
499 | [Lit (WordLiteral i)] <- args
500 -> reduce . Literal . IntLiteral . toInteger $ (fromInteger :: Integer -> Int) i -- for overflow behavior
501
502 $(namePat 'GHC.Prim.gtWord#) | Just (i,j) <- wordLiterals args
503 -> reduce (boolToIntLiteral (i > j))
504 $(namePat 'GHC.Prim.geWord#) | Just (i,j) <- wordLiterals args
505 -> reduce (boolToIntLiteral (i >= j))
506 $(namePat 'GHC.Prim.eqWord#) | Just (i,j) <- wordLiterals args
507 -> reduce (boolToIntLiteral (i == j))
508 $(namePat 'GHC.Prim.neWord#) | Just (i,j) <- wordLiterals args
509 -> reduce (boolToIntLiteral (i /= j))
510 $(namePat 'GHC.Prim.ltWord#) | Just (i,j) <- wordLiterals args
511 -> reduce (boolToIntLiteral (i < j))
512 $(namePat 'GHC.Prim.leWord#) | Just (i,j) <- wordLiterals args
513 -> reduce (boolToIntLiteral (i <= j))
514
515 $(namePat 'GHC.Prim.popCnt8#) | [i] <- wordLiterals' args
516 -> reduce . integerToWordLiteral . toInteger . popCount . (fromInteger :: Integer -> Word8) $ i
517 $(namePat 'GHC.Prim.popCnt16#) | [i] <- wordLiterals' args
518 -> reduce . integerToWordLiteral . toInteger . popCount . (fromInteger :: Integer -> Word16) $ i
519 $(namePat 'GHC.Prim.popCnt32#) | [i] <- wordLiterals' args
520 -> reduce . integerToWordLiteral . toInteger . popCount . (fromInteger :: Integer -> Word32) $ i
521 $(namePat 'GHC.Prim.popCnt64#) | [i] <- word64Literals' args
522 -> reduce . integerToWordLiteral . toInteger . popCount . (fromInteger :: Integer -> Word64) $ i
523 $(namePat 'GHC.Prim.popCnt#) | [i] <- wordLiterals' args
524 -> reduce . integerToWordLiteral . toInteger . popCount . (fromInteger :: Integer -> Word) $ i
525
526 $(namePat 'GHC.Prim.clz8#) | [i] <- wordLiterals' args
527 -> reduce . integerToWordLiteral . toInteger . countLeadingZeros . (fromInteger :: Integer -> Word8) $ i
528 $(namePat 'GHC.Prim.clz16#) | [i] <- wordLiterals' args
529 -> reduce . integerToWordLiteral . toInteger . countLeadingZeros . (fromInteger :: Integer -> Word16) $ i
530 $(namePat 'GHC.Prim.clz32#) | [i] <- wordLiterals' args
531 -> reduce . integerToWordLiteral . toInteger . countLeadingZeros . (fromInteger :: Integer -> Word32) $ i
532 $(namePat 'GHC.Prim.clz64#) | [i] <- word64Literals' args
533 -> reduce . integerToWordLiteral . toInteger . countLeadingZeros . (fromInteger :: Integer -> Word64) $ i
534 $(namePat 'GHC.Prim.clz#) | [i] <- wordLiterals' args
535 -> reduce . integerToWordLiteral . toInteger . countLeadingZeros . (fromInteger :: Integer -> Word) $ i
536
537 $(namePat 'GHC.Prim.ctz8#) | [i] <- wordLiterals' args
538 -> reduce . integerToWordLiteral . toInteger . countTrailingZeros . (fromInteger :: Integer -> Word) $ i .&. (bit 8 - 1)
539 $(namePat 'GHC.Prim.ctz16#) | [i] <- wordLiterals' args
540 -> reduce . integerToWordLiteral . toInteger . countTrailingZeros . (fromInteger :: Integer -> Word) $ i .&. (bit 16 - 1)
541 $(namePat 'GHC.Prim.ctz32#) | [i] <- wordLiterals' args
542 -> reduce . integerToWordLiteral . toInteger . countTrailingZeros . (fromInteger :: Integer -> Word) $ i .&. (bit 32 - 1)
543 $(namePat 'GHC.Prim.ctz64#) | [i] <- word64Literals' args
544 -> reduce . integerToWordLiteral . toInteger . countTrailingZeros . (fromInteger :: Integer -> Word64) $ i .&. (bit 64 - 1)
545 $(namePat 'GHC.Prim.ctz#) | [i] <- wordLiterals' args
546 -> reduce . integerToWordLiteral . toInteger . countTrailingZeros . (fromInteger :: Integer -> Word) $ i
547
548 $(namePat 'GHC.Prim.byteSwap16#) | [i] <- wordLiterals' args
549 -> reduce . integerToWordLiteral . toInteger . byteSwap16 . (fromInteger :: Integer -> Word16) $ i
550 $(namePat 'GHC.Prim.byteSwap32#) | [i] <- wordLiterals' args
551 -> reduce . integerToWordLiteral . toInteger . byteSwap32 . (fromInteger :: Integer -> Word32) $ i
552 $(namePat 'GHC.Prim.byteSwap64#) | [i] <- word64Literals' args
553 -> reduce . integerToWordLiteral . toInteger . byteSwap64 . (fromInteger :: Integer -> Word64) $ i
554 $(namePat 'GHC.Prim.byteSwap#) | [i] <- wordLiterals' args -- assume 64bits
555 -> reduce . integerToWordLiteral . toInteger . byteSwap64 . (fromInteger :: Integer -> Word64) $ i
556
557 #if MIN_VERSION_base(4,14,0)
558 $(namePat 'GHC.Prim.bitReverse#) | [i] <- wordLiterals' args
559 -> reduce . integerToWordLiteral . toInteger . bitReverse64 . fromInteger $ i -- assume 64bits
560 $(namePat 'GHC.Prim.bitReverse8#) | [i] <- wordLiterals' args
561 -> reduce . integerToWordLiteral . toInteger . bitReverse8 . fromInteger $ i
562 $(namePat 'GHC.Prim.bitReverse16#) | [i] <- wordLiterals' args
563 -> reduce . integerToWordLiteral . toInteger . bitReverse16 . fromInteger $ i
564 $(namePat 'GHC.Prim.bitReverse32#) | [i] <- wordLiterals' args
565 -> reduce . integerToWordLiteral . toInteger . bitReverse32 . fromInteger $ i
566 $(namePat 'GHC.Prim.bitReverse64#) | [i] <- word64Literals' args
567 -> reduce . integerToWordLiteral . toInteger . bitReverse64 . fromInteger $ i
568 #endif
569 ------------
570 -- Narrowing
571 ------------
572 $(namePat 'GHC.Prim.narrow8Int#) | [i] <- intLiterals' args
573 -> let !(I# a) = fromInteger i
574 b = narrow8Int# a
575 in reduce . Literal . IntLiteral . toInteger $ I# b
576 $(namePat 'GHC.Prim.narrow16Int#) | [i] <- intLiterals' args
577 -> let !(I# a) = fromInteger i
578 b = narrow16Int# a
579 in reduce . Literal . IntLiteral . toInteger $ I# b
580 $(namePat 'GHC.Prim.narrow32Int#) | [i] <- intLiterals' args
581 -> let !(I# a) = fromInteger i
582 b = narrow32Int# a
583 in reduce . Literal . IntLiteral . toInteger $ I# b
584 $(namePat 'GHC.Prim.narrow8Word#) | [i] <- wordLiterals' args
585 -> let !(W# a) = fromInteger i
586 b = narrow8Word# a
587 in reduce . Literal . WordLiteral . toInteger $ W# b
588 $(namePat 'GHC.Prim.narrow16Word#) | [i] <- wordLiterals' args
589 -> let !(W# a) = fromInteger i
590 b = narrow16Word# a
591 in reduce . Literal . WordLiteral . toInteger $ W# b
592 $(namePat 'GHC.Prim.narrow32Word#) | [i] <- wordLiterals' args
593 -> let !(W# a) = fromInteger i
594 b = narrow32Word# a
595 in reduce . Literal . WordLiteral . toInteger $ W# b
596
597 #if MIN_VERSION_base(4,16,0)
598 --------
599 -- Int8#
600 --------
601 $(namePat 'GHC.Prim.intToInt8#) | [i] <- intLiterals' args
602 -> let !(I# a) = fromInteger i
603 b = narrow8Int# a
604 in reduce . Literal . Int8Literal . toInteger $ I# b
605 $(namePat 'GHC.Prim.int8ToInt#) | [i] <- int8Literals' args
606 -> reduce . Literal $ IntLiteral i
607 -- XXX: Primitive does not exist?
608 "GHC.Prim.negateInt8" | [i] <- int8Literals' args
609 -> let !(I8# a) = fromInteger i
610 in reduce (Literal (Int8Literal (toInteger (I8# (negateInt8# a)))))
611 $(namePat 'GHC.Prim.plusInt8#) | Just r <- liftI8 plusInt8# args
612 -> reduce r
613 $(namePat 'GHC.Prim.subInt8#) | Just r <- liftI8 subInt8# args
614 -> reduce r
615 $(namePat 'GHC.Prim.timesInt8#) | Just r <- liftI8 timesInt8# args
616 -> reduce r
617 $(namePat 'GHC.Prim.quotInt8#) | Just r <- liftI8 quotInt8# args
618 -> reduce r
619 $(namePat 'GHC.Prim.remInt8#) | Just r <- liftI8 remInt8# args
620 -> reduce r
621 $(namePat 'GHC.Prim.quotRemInt8#)
622 | [i, j] <- int8Literals' args
623 , (_,tyView -> TyConApp tupTcNm tyArgs) <- splitFunForallTy ty
624 , (Just tupTc) <- UniqMap.lookup tupTcNm tcm
625 , [tupDc] <- tyConDataCons tupTc
626 -> let !(I8# a) = fromInteger i
627 !(I8# b) = fromInteger j
628 !(# q, r #) = quotRemInt8# a b
629 in reduce $
630 mkApps (Data tupDc) (map Right tyArgs ++
631 [ Left (Literal (Int8Literal (toInteger (I8# q))))
632 , Left (Literal (Int8Literal (toInteger (I8# r))))])
633 $(namePat 'GHC.Prim.uncheckedShiftLInt8#) | Just r <- liftI8I uncheckedShiftLInt8# args
634 -> reduce r
635 $(namePat 'GHC.Prim.uncheckedShiftRAInt8#) | Just r <- liftI8I uncheckedShiftRAInt8# args
636 -> reduce r
637 $(namePat 'GHC.Prim.uncheckedShiftRLInt8#) | Just r <- liftI8I uncheckedShiftRLInt8# args
638 -> reduce r
639 $(namePat 'GHC.Prim.int8ToWord8#) | [i] <- int8Literals' args
640 -> let !(I8# a) = fromInteger i
641 in reduce (Literal (Word8Literal (toInteger (W8# (int8ToWord8# a)))))
642 $(namePat 'GHC.Prim.eqInt8#) | Just r <- liftI8RI eqInt8# args
643 -> reduce r
644 $(namePat 'GHC.Prim.geInt8#) | Just r <- liftI8RI geInt8# args
645 -> reduce r
646 $(namePat 'GHC.Prim.gtInt8#) | Just r <- liftI8RI gtInt8# args
647 -> reduce r
648 $(namePat 'GHC.Prim.leInt8#) | Just r <- liftI8RI leInt8# args
649 -> reduce r
650 $(namePat 'GHC.Prim.ltInt8#) | Just r <- liftI8RI ltInt8# args
651 -> reduce r
652 $(namePat 'GHC.Prim.neInt8#) | Just r <- liftI8RI neInt8# args
653 -> reduce r
654
655 ---------
656 -- Int16#
657 ---------
658 $(namePat 'GHC.Prim.intToInt16#) | [i] <- intLiterals' args
659 -> let !(I# a) = fromInteger i
660 b = narrow16Int# a
661 in reduce . Literal . Int16Literal . toInteger $ I# b
662 $(namePat 'GHC.Prim.int16ToInt#) | [i] <- int16Literals' args
663 -> reduce . Literal $ IntLiteral i
664 -- XXX: Primitive does not exist?
665 "GHC.Prim.negateInt16" | [i] <- int16Literals' args
666 -> let !(I16# a) = fromInteger i
667 in reduce (Literal (Int16Literal (toInteger (I16# (negateInt16# a)))))
668 $(namePat 'GHC.Prim.plusInt16#) | Just r <- liftI16 plusInt16# args
669 -> reduce r
670 $(namePat 'GHC.Prim.subInt16#) | Just r <- liftI16 subInt16# args
671 -> reduce r
672 $(namePat 'GHC.Prim.timesInt16#) | Just r <- liftI16 timesInt16# args
673 -> reduce r
674 $(namePat 'GHC.Prim.quotInt16#) | Just r <- liftI16 quotInt16# args
675 -> reduce r
676 $(namePat 'GHC.Prim.remInt16#) | Just r <- liftI16 remInt16# args
677 -> reduce r
678 $(namePat 'GHC.Prim.quotRemInt16#)
679 | [i, j] <- int16Literals' args
680 , (_,tyView -> TyConApp tupTcNm tyArgs) <- splitFunForallTy ty
681 , (Just tupTc) <- UniqMap.lookup tupTcNm tcm
682 , [tupDc] <- tyConDataCons tupTc
683 -> let !(I16# a) = fromInteger i
684 !(I16# b) = fromInteger j
685 !(# q, r #) = quotRemInt16# a b
686 in reduce $
687 mkApps (Data tupDc) (map Right tyArgs ++
688 [ Left (Literal (Int16Literal (toInteger (I16# q))))
689 , Left (Literal (Int16Literal (toInteger (I16# r))))])
690 $(namePat 'GHC.Prim.uncheckedShiftLInt16#) | Just r <- liftI16I uncheckedShiftLInt16# args
691 -> reduce r
692 $(namePat 'GHC.Prim.uncheckedShiftRAInt16#) | Just r <- liftI16I uncheckedShiftRAInt16# args
693 -> reduce r
694 $(namePat 'GHC.Prim.uncheckedShiftRLInt16#) | Just r <- liftI16I uncheckedShiftRLInt16# args
695 -> reduce r
696 $(namePat 'GHC.Prim.int16ToWord16#) | [i] <- int16Literals' args
697 -> let !(I16# a) = fromInteger i
698 in reduce (Literal (Word16Literal (toInteger (W16# (int16ToWord16# a)))))
699 $(namePat 'GHC.Prim.eqInt16#) | Just r <- liftI16RI eqInt16# args
700 -> reduce r
701 $(namePat 'GHC.Prim.geInt16#) | Just r <- liftI16RI geInt16# args
702 -> reduce r
703 $(namePat 'GHC.Prim.gtInt16#) | Just r <- liftI16RI gtInt16# args
704 -> reduce r
705 $(namePat 'GHC.Prim.leInt16#) | Just r <- liftI16RI leInt16# args
706 -> reduce r
707 $(namePat 'GHC.Prim.ltInt16#) | Just r <- liftI16RI ltInt16# args
708 -> reduce r
709 $(namePat 'GHC.Prim.neInt16#) | Just r <- liftI16RI neInt16# args
710 -> reduce r
711
712 ---------
713 -- Int32#
714 ---------
715 $(namePat 'GHC.Prim.intToInt32#) | [i] <- intLiterals' args
716 -> let !(I# a) = fromInteger i
717 b = narrow32Int# a
718 in reduce . Literal . Int32Literal . toInteger $ I# b
719 $(namePat 'GHC.Prim.int32ToInt#) | [i] <- int32Literals' args
720 -> reduce . Literal $ IntLiteral i
721 -- XXX: Primitive does not exist?
722 "GHC.Prim.negateInt32" | [i] <- int32Literals' args
723 -> let !(I32# a) = fromInteger i
724 in reduce (Literal (Int32Literal (toInteger (I32# (negateInt32# a)))))
725 $(namePat 'GHC.Prim.plusInt32#) | Just r <- liftI32 plusInt32# args
726 -> reduce r
727 $(namePat 'GHC.Prim.subInt32#) | Just r <- liftI32 subInt32# args
728 -> reduce r
729 $(namePat 'GHC.Prim.timesInt32#) | Just r <- liftI32 timesInt32# args
730 -> reduce r
731 $(namePat 'GHC.Prim.quotInt32#) | Just r <- liftI32 quotInt32# args
732 -> reduce r
733 $(namePat 'GHC.Prim.remInt32#) | Just r <- liftI32 remInt32# args
734 -> reduce r
735 $(namePat 'GHC.Prim.quotRemInt32#)
736 | [i, j] <- int32Literals' args
737 , (_,tyView -> TyConApp tupTcNm tyArgs) <- splitFunForallTy ty
738 , (Just tupTc) <- UniqMap.lookup tupTcNm tcm
739 , [tupDc] <- tyConDataCons tupTc
740 -> let !(I32# a) = fromInteger i
741 !(I32# b) = fromInteger j
742 !(# q, r #) = quotRemInt32# a b
743 in reduce $
744 mkApps (Data tupDc) (map Right tyArgs ++
745 [ Left (Literal (Int32Literal (toInteger (I32# q))))
746 , Left (Literal (Int32Literal (toInteger (I32# r))))])
747 $(namePat 'GHC.Prim.uncheckedShiftLInt32#) | Just r <- liftI32I uncheckedShiftLInt32# args
748 -> reduce r
749 $(namePat 'GHC.Prim.uncheckedShiftRAInt32#) | Just r <- liftI32I uncheckedShiftRAInt32# args
750 -> reduce r
751 $(namePat 'GHC.Prim.uncheckedShiftRLInt32#) | Just r <- liftI32I uncheckedShiftRLInt32# args
752 -> reduce r
753 $(namePat 'GHC.Prim.int32ToWord32#) | [i] <- int32Literals' args
754 -> let !(I32# a) = fromInteger i
755 in reduce (Literal (Word32Literal (toInteger (W32# (int32ToWord32# a)))))
756 $(namePat 'GHC.Prim.eqInt32#) | Just r <- liftI32RI eqInt32# args
757 -> reduce r
758 $(namePat 'GHC.Prim.geInt32#) | Just r <- liftI32RI geInt32# args
759 -> reduce r
760 $(namePat 'GHC.Prim.gtInt32#) | Just r <- liftI32RI gtInt32# args
761 -> reduce r
762 $(namePat 'GHC.Prim.leInt32#) | Just r <- liftI32RI leInt32# args
763 -> reduce r
764 $(namePat 'GHC.Prim.ltInt32#) | Just r <- liftI32RI ltInt32# args
765 -> reduce r
766 $(namePat 'GHC.Prim.neInt32#) | Just r <- liftI32RI neInt32# args
767 -> reduce r
768
769 ---------
770 -- Int64#
771 ---------
772 #if MIN_VERSION_base(4,17,0)
773 $(namePat 'GHC.Prim.intToInt64#) | [i] <- intLiterals' args
774 -> reduce (Literal (Int64Literal i))
775 $(namePat 'GHC.Prim.int64ToInt#) | [i] <- int64Literals' args
776 -> reduce . Literal $ IntLiteral i
777 -- XXX: Primitive does not exist?
778 "GHC.Prim.negateInt64" | [i] <- int64Literals' args
779 -> let !(I64# a) = fromInteger i
780 in reduce (Literal (Int64Literal (toInteger (I64# (negateInt64# a)))))
781 $(namePat 'GHC.Prim.plusInt64#) | Just r <- liftI64 plusInt64# args
782 -> reduce r
783 $(namePat 'GHC.Prim.subInt64#) | Just r <- liftI64 subInt64# args
784 -> reduce r
785 $(namePat 'GHC.Prim.timesInt64#) | Just r <- liftI64 timesInt64# args
786 -> reduce r
787 $(namePat 'GHC.Prim.quotInt64#) | Just r <- liftI64 quotInt64# args
788 -> reduce r
789 $(namePat 'GHC.Prim.remInt64#) | Just r <- liftI64 remInt64# args
790 -> reduce r
791 $(namePat 'GHC.Prim.uncheckedIShiftL64#) | Just r <- liftI64I uncheckedIShiftL64# args
792 -> reduce r
793 $(namePat 'GHC.Prim.uncheckedIShiftRA64#) | Just r <- liftI64I uncheckedIShiftRA64# args
794 -> reduce r
795 $(namePat 'GHC.Prim.uncheckedIShiftRL64#) | Just r <- liftI64I uncheckedIShiftRL64# args
796 -> reduce r
797 $(namePat 'GHC.Prim.int64ToWord64#) | [i] <- int64Literals' args
798 -> let !(I64# a) = fromInteger i
799 in reduce (Literal (Word64Literal (toInteger (W64# (int64ToWord64# a)))))
800 $(namePat 'GHC.Prim.eqInt64#) | Just r <- liftI64RI eqInt64# args
801 -> reduce r
802 $(namePat 'GHC.Prim.geInt64#) | Just r <- liftI64RI geInt64# args
803 -> reduce r
804 $(namePat 'GHC.Prim.gtInt64#) | Just r <- liftI64RI gtInt64# args
805 -> reduce r
806 $(namePat 'GHC.Prim.leInt64#) | Just r <- liftI64RI leInt64# args
807 -> reduce r
808 $(namePat 'GHC.Prim.ltInt64#) | Just r <- liftI64RI ltInt64# args
809 -> reduce r
810 $(namePat 'GHC.Prim.neInt64#) | Just r <- liftI64RI neInt64# args
811 -> reduce r
812 #endif
813
814 ---------
815 -- Word8#
816 ---------
817 $(namePat 'GHC.Prim.wordToWord8#) | [i] <- wordLiterals' args
818 -> let !(W# a) = fromInteger i
819 b = narrow8Word# a
820 in reduce . Literal . Word8Literal . toInteger $ W# b
821 $(namePat 'GHC.Prim.word8ToWord#) | [i] <- word8Literals' args
822 -> reduce . Literal $ WordLiteral i
823 $(namePat 'GHC.Prim.plusWord8#) | Just r <- liftW8 plusWord8# args
824 -> reduce r
825 $(namePat 'GHC.Prim.subWord8#) | Just r <- liftW8 subWord8# args
826 -> reduce r
827 $(namePat 'GHC.Prim.timesWord8#) | Just r <- liftW8 timesWord8# args
828 -> reduce r
829 $(namePat 'GHC.Prim.quotWord8#) | Just r <- liftW8 quotWord8# args
830 -> reduce r
831 $(namePat 'GHC.Prim.remWord8#) | Just r <- liftW8 remWord8# args
832 -> reduce r
833 $(namePat 'GHC.Prim.quotRemWord8#)
834 | [i, j] <- word8Literals' args
835 , (_,tyView -> TyConApp tupTcNm tyArgs) <- splitFunForallTy ty
836 , (Just tupTc) <- UniqMap.lookup tupTcNm tcm
837 , [tupDc] <- tyConDataCons tupTc
838 -> let !(W8# a) = fromInteger i
839 !(W8# b) = fromInteger j
840 !(# q, r #) = quotRemWord8# a b
841 in reduce $
842 mkApps (Data tupDc) (map Right tyArgs ++
843 [ Left (Literal (Word8Literal (toInteger (W8# q))))
844 , Left (Literal (Word8Literal (toInteger (W8# r))))])
845 $(namePat 'GHC.Prim.andWord8#) | Just r <- liftW8 andWord8# args
846 -> reduce r
847 $(namePat 'GHC.Prim.orWord8#) | Just r <- liftW8 orWord8# args
848 -> reduce r
849 $(namePat 'GHC.Prim.xorWord8#) | Just r <- liftW8 xorWord8# args
850 -> reduce r
851 $(namePat 'GHC.Prim.notWord8#) | [i] <- word8Literals' args
852 -> let !(W8# a) = fromInteger i
853 in reduce (Literal (Word8Literal (toInteger (W8# (notWord8# a)))))
854 $(namePat 'GHC.Prim.uncheckedShiftLWord8#) | Just r <- liftW8I uncheckedShiftLWord8# args
855 -> reduce r
856 $(namePat 'GHC.Prim.uncheckedShiftRLWord8#) | Just r <- liftW8I uncheckedShiftRLWord8# args
857 -> reduce r
858 $(namePat 'GHC.Prim.word8ToInt8#) | [i] <- word8Literals' args
859 -> let !(W8# a) = fromInteger i
860 in reduce (Literal (Int8Literal (toInteger (I8# (word8ToInt8# a)))))
861 $(namePat 'GHC.Prim.eqWord8#) | Just r <- liftW8RI eqWord8# args
862 -> reduce r
863 $(namePat 'GHC.Prim.geWord8#) | Just r <- liftW8RI geWord8# args
864 -> reduce r
865 $(namePat 'GHC.Prim.gtWord8#) | Just r <- liftW8RI gtWord8# args
866 -> reduce r
867 $(namePat 'GHC.Prim.leWord8#) | Just r <- liftW8RI leWord8# args
868 -> reduce r
869 $(namePat 'GHC.Prim.ltWord8#) | Just r <- liftW8RI ltWord8# args
870 -> reduce r
871 $(namePat 'GHC.Prim.neWord8#) | Just r <- liftW8RI neWord8# args
872 -> reduce r
873
874 ----------
875 -- Word16#
876 ----------
877 $(namePat 'GHC.Prim.wordToWord16#) | [i] <- wordLiterals' args
878 -> let !(W# a) = fromInteger i
879 b = narrow16Word# a
880 in reduce . Literal . Word16Literal . toInteger $ W# b
881 $(namePat 'GHC.Prim.word16ToWord#) | [i] <- word16Literals' args
882 -> reduce . Literal $ WordLiteral i
883 $(namePat 'GHC.Prim.plusWord16#) | Just r <- liftW16 plusWord16# args
884 -> reduce r
885 $(namePat 'GHC.Prim.subWord16#) | Just r <- liftW16 subWord16# args
886 -> reduce r
887 $(namePat 'GHC.Prim.timesWord16#) | Just r <- liftW16 timesWord16# args
888 -> reduce r
889 $(namePat 'GHC.Prim.quotWord16#) | Just r <- liftW16 quotWord16# args
890 -> reduce r
891 $(namePat 'GHC.Prim.remWord16#) | Just r <- liftW16 remWord16# args
892 -> reduce r
893 $(namePat 'GHC.Prim.quotRemWord16#)
894 | [i, j] <- word16Literals' args
895 , (_,tyView -> TyConApp tupTcNm tyArgs) <- splitFunForallTy ty
896 , (Just tupTc) <- UniqMap.lookup tupTcNm tcm
897 , [tupDc] <- tyConDataCons tupTc
898 -> let !(W16# a) = fromInteger i
899 !(W16# b) = fromInteger j
900 !(# q, r #) = quotRemWord16# a b
901 in reduce $
902 mkApps (Data tupDc) (map Right tyArgs ++
903 [ Left (Literal (Word16Literal (toInteger (W16# q))))
904 , Left (Literal (Word16Literal (toInteger (W16# r))))])
905 $(namePat 'GHC.Prim.andWord16#) | Just r <- liftW16 andWord16# args
906 -> reduce r
907 $(namePat 'GHC.Prim.orWord16#) | Just r <- liftW16 orWord16# args
908 -> reduce r
909 $(namePat 'GHC.Prim.xorWord16#) | Just r <- liftW16 xorWord16# args
910 -> reduce r
911 $(namePat 'GHC.Prim.notWord16#) | [i] <- word16Literals' args
912 -> let !(W16# a) = fromInteger i
913 in reduce (Literal (Word16Literal (toInteger (W16# (notWord16# a)))))
914 $(namePat 'GHC.Prim.uncheckedShiftLWord16#) | Just r <- liftW16I uncheckedShiftLWord16# args
915 -> reduce r
916 $(namePat 'GHC.Prim.uncheckedShiftRLWord16#) | Just r <- liftW16I uncheckedShiftRLWord16# args
917 -> reduce r
918 $(namePat 'GHC.Prim.word16ToInt16#) | [i] <- word16Literals' args
919 -> let !(W16# a) = fromInteger i
920 in reduce (Literal (Int16Literal (toInteger (I16# (word16ToInt16# a)))))
921 $(namePat 'GHC.Prim.eqWord16#) | Just r <- liftW16RI eqWord16# args
922 -> reduce r
923 $(namePat 'GHC.Prim.geWord16#) | Just r <- liftW16RI geWord16# args
924 -> reduce r
925 $(namePat 'GHC.Prim.gtWord16#) | Just r <- liftW16RI gtWord16# args
926 -> reduce r
927 $(namePat 'GHC.Prim.leWord16#) | Just r <- liftW16RI leWord16# args
928 -> reduce r
929 $(namePat 'GHC.Prim.ltWord16#) | Just r <- liftW16RI ltWord16# args
930 -> reduce r
931 $(namePat 'GHC.Prim.neWord16#) | Just r <- liftW16RI neWord16# args
932 -> reduce r
933
934 ----------
935 -- Word32#
936 ----------
937 $(namePat 'GHC.Prim.wordToWord32#) | [i] <- wordLiterals' args
938 -> let !(W# a) = fromInteger i
939 b = narrow32Word# a
940 in reduce . Literal . Word32Literal . toInteger $ W# b
941 $(namePat 'GHC.Prim.word32ToWord#) | [i] <- word32Literals' args
942 -> reduce . Literal $ WordLiteral i
943 $(namePat 'GHC.Prim.plusWord32#) | Just r <- liftW32 plusWord32# args
944 -> reduce r
945 $(namePat 'GHC.Prim.subWord32#) | Just r <- liftW32 subWord32# args
946 -> reduce r
947 $(namePat 'GHC.Prim.timesWord32#) | Just r <- liftW32 timesWord32# args
948 -> reduce r
949 $(namePat 'GHC.Prim.quotWord32#) | Just r <- liftW32 quotWord32# args
950 -> reduce r
951 $(namePat 'GHC.Prim.remWord32#) | Just r <- liftW32 remWord32# args
952 -> reduce r
953 $(namePat 'GHC.Prim.quotRemWord32#)
954 | [i, j] <- word32Literals' args
955 , (_,tyView -> TyConApp tupTcNm tyArgs) <- splitFunForallTy ty
956 , (Just tupTc) <- UniqMap.lookup tupTcNm tcm
957 , [tupDc] <- tyConDataCons tupTc
958 -> let !(W32# a) = fromInteger i
959 !(W32# b) = fromInteger j
960 !(# q, r #) = quotRemWord32# a b
961 in reduce $
962 mkApps (Data tupDc) (map Right tyArgs ++
963 [ Left (Literal (Word32Literal (toInteger (W32# q))))
964 , Left (Literal (Word32Literal (toInteger (W32# r))))])
965 $(namePat 'GHC.Prim.andWord32#) | Just r <- liftW32 andWord32# args
966 -> reduce r
967 $(namePat 'GHC.Prim.orWord32#) | Just r <- liftW32 orWord32# args
968 -> reduce r
969 $(namePat 'GHC.Prim.xorWord32#) | Just r <- liftW32 xorWord32# args
970 -> reduce r
971 $(namePat 'GHC.Prim.notWord32#) | [i] <- word32Literals' args
972 -> let !(W32# a) = fromInteger i
973 in reduce (Literal (Word32Literal (toInteger (W32# (notWord32# a)))))
974 $(namePat 'GHC.Prim.uncheckedShiftLWord32#) | Just r <- liftW32I uncheckedShiftLWord32# args
975 -> reduce r
976 $(namePat 'GHC.Prim.uncheckedShiftRLWord32#) | Just r <- liftW32I uncheckedShiftRLWord32# args
977 -> reduce r
978 $(namePat 'GHC.Prim.word32ToInt32#) | [i] <- word32Literals' args
979 -> let !(W32# a) = fromInteger i
980 in reduce (Literal (Int32Literal (toInteger (I32# (word32ToInt32# a)))))
981 $(namePat 'GHC.Prim.eqWord32#) | Just r <- liftW32RI eqWord32# args
982 -> reduce r
983 $(namePat 'GHC.Prim.geWord32#) | Just r <- liftW32RI geWord32# args
984 -> reduce r
985 $(namePat 'GHC.Prim.gtWord32#) | Just r <- liftW32RI gtWord32# args
986 -> reduce r
987 $(namePat 'GHC.Prim.leWord32#) | Just r <- liftW32RI leWord32# args
988 -> reduce r
989 $(namePat 'GHC.Prim.ltWord32#) | Just r <- liftW32RI ltWord32# args
990 -> reduce r
991 $(namePat 'GHC.Prim.neWord32#) | Just r <- liftW32RI neWord32# args
992 -> reduce r
993
994 #if MIN_VERSION_base(4,17,0)
995 ----------
996 -- Word64#
997 ----------
998 $(namePat 'GHC.Prim.wordToWord64#) | [i] <- wordLiterals' args
999 -> reduce (Literal (Word64Literal i))
1000 $(namePat 'GHC.Prim.word64ToWord#) | [i] <- word64Literals' args
1001 -> reduce . Literal $ WordLiteral i
1002 $(namePat 'GHC.Prim.plusWord64#) | Just r <- liftW64 plusWord64# args
1003 -> reduce r
1004 $(namePat 'GHC.Prim.subWord64#) | Just r <- liftW64 subWord64# args
1005 -> reduce r
1006 $(namePat 'GHC.Prim.timesWord64#) | Just r <- liftW64 timesWord64# args
1007 -> reduce r
1008 $(namePat 'GHC.Prim.quotWord64#) | Just r <- liftW64 quotWord64# args
1009 -> reduce r
1010 $(namePat 'GHC.Prim.remWord64#) | Just r <- liftW64 remWord64# args
1011 -> reduce r
1012 $(namePat 'GHC.Prim.and64#) | Just r <- liftW64 and64# args
1013 -> reduce r
1014 $(namePat 'GHC.Prim.or64#) | Just r <- liftW64 or64# args
1015 -> reduce r
1016 $(namePat 'GHC.Prim.xor64#) | Just r <- liftW64 xor64# args
1017 -> reduce r
1018 $(namePat 'GHC.Prim.not64#) | [i] <- word64Literals' args
1019 -> let !(W64# a) = fromInteger i
1020 in reduce (Literal (Word64Literal (toInteger (W64# (not64# a)))))
1021 $(namePat 'GHC.Prim.uncheckedShiftL64#) | Just r <- liftW64I uncheckedShiftL64# args
1022 -> reduce r
1023 $(namePat 'GHC.Prim.uncheckedShiftRL64#) | Just r <- liftW64I uncheckedShiftRL64# args
1024 -> reduce r
1025 $(namePat 'GHC.Prim.word64ToInt64#) | [i] <- word64Literals' args
1026 -> let !(W64# a) = fromInteger i
1027 in reduce (Literal (Int64Literal (toInteger (I64# (word64ToInt64# a)))))
1028 $(namePat 'GHC.Prim.eqWord64#) | Just r <- liftW64RI eqWord64# args
1029 -> reduce r
1030 $(namePat 'GHC.Prim.geWord64#) | Just r <- liftW64RI geWord64# args
1031 -> reduce r
1032 $(namePat 'GHC.Prim.gtWord64#) | Just r <- liftW64RI gtWord64# args
1033 -> reduce r
1034 $(namePat 'GHC.Prim.leWord64#) | Just r <- liftW64RI leWord64# args
1035 -> reduce r
1036 $(namePat 'GHC.Prim.ltWord64#) | Just r <- liftW64RI ltWord64# args
1037 -> reduce r
1038 $(namePat 'GHC.Prim.neWord64#) | Just r <- liftW64RI neWord64# args
1039 -> reduce r
1040 #endif
1041 #endif
1042
1043 ----------
1044 -- Double#
1045 ----------
1046 $(namePat '(GHC.Prim.>##)) | Just r <- liftDDI (>##) args
1047 -> reduce r
1048 $(namePat '(GHC.Prim.>=##)) | Just r <- liftDDI (>=##) args
1049 -> reduce r
1050 $(namePat '(GHC.Prim.==##)) | Just r <- liftDDI (==##) args
1051 -> reduce r
1052 $(namePat '(GHC.Prim./=##)) | Just r <- liftDDI (/=##) args
1053 -> reduce r
1054 $(namePat '(GHC.Prim.<##)) | Just r <- liftDDI (<##) args
1055 -> reduce r
1056 $(namePat '(GHC.Prim.<=##)) | Just r <- liftDDI (<=##) args
1057 -> reduce r
1058 $(namePat '(GHC.Prim.+##)) | Just r <- liftDDD (+##) args
1059 -> reduce r
1060 $(namePat '(GHC.Prim.-##)) | Just r <- liftDDD (-##) args
1061 -> reduce r
1062 $(namePat '(GHC.Prim.*##)) | Just r <- liftDDD (*##) args
1063 -> reduce r
1064 $(namePat '(GHC.Prim./##)) | Just r <- liftDDD (/##) args
1065 -> reduce r
1066
1067 $(namePat 'GHC.Prim.negateDouble#) | Just r <- liftDD negateDouble# args
1068 -> reduce r
1069 $(namePat 'GHC.Prim.fabsDouble#) | Just r <- liftDD fabsDouble# args
1070 -> reduce r
1071
1072 $(namePat 'GHC.Prim.double2Int#) | [i] <- doubleLiterals' args
1073 -> let !(D# a) = wordToDouble i
1074 r = double2Int# a
1075 in reduce . Literal . IntLiteral . toInteger $ I# r
1076 $(namePat 'GHC.Prim.double2Float#)
1077 | [Lit (DoubleLiteral d)] <- args
1078 -> let !(D# a) = wordToDouble d
1079 r = double2Float# a
1080 in reduce . Literal . FloatLiteral . floatToWord $ F# r
1081
1082 $(namePat 'GHC.Prim.expDouble#) | Just r <- liftDD expDouble# args
1083 -> reduce r
1084 $(namePat 'GHC.Prim.logDouble#) | Just r <- liftDD logDouble# args
1085 -> reduce r
1086 $(namePat 'GHC.Prim.sqrtDouble#) | Just r <- liftDD sqrtDouble# args
1087 -> reduce r
1088 $(namePat 'GHC.Prim.sinDouble#) | Just r <- liftDD sinDouble# args
1089 -> reduce r
1090 $(namePat 'GHC.Prim.cosDouble#) | Just r <- liftDD cosDouble# args
1091 -> reduce r
1092 $(namePat 'GHC.Prim.tanDouble#) | Just r <- liftDD tanDouble# args
1093 -> reduce r
1094 $(namePat 'GHC.Prim.asinDouble#) | Just r <- liftDD asinDouble# args
1095 -> reduce r
1096 $(namePat 'GHC.Prim.acosDouble#) | Just r <- liftDD acosDouble# args
1097 -> reduce r
1098 $(namePat 'GHC.Prim.atanDouble#) | Just r <- liftDD atanDouble# args
1099 -> reduce r
1100 $(namePat 'GHC.Prim.sinhDouble#) | Just r <- liftDD sinhDouble# args
1101 -> reduce r
1102 $(namePat 'GHC.Prim.coshDouble#) | Just r <- liftDD coshDouble# args
1103 -> reduce r
1104 $(namePat 'GHC.Prim.tanhDouble#) | Just r <- liftDD tanhDouble# args
1105 -> reduce r
1106
1107 #if MIN_VERSION_ghc(8,7,0)
1108 $(namePat 'GHC.Prim.asinhDouble#) | Just r <- liftDD asinhDouble# args
1109 -> reduce r
1110 $(namePat 'GHC.Prim.acoshDouble#) | Just r <- liftDD acoshDouble# args
1111 -> reduce r
1112 $(namePat 'GHC.Prim.atanhDouble#) | Just r <- liftDD atanhDouble# args
1113 -> reduce r
1114 #endif
1115
1116 $(namePat '(GHC.Prim.**##)) | Just r <- liftDDD (**##) args
1117 -> reduce r
1118 -- decodeDouble_2Int# :: Double# -> (#Int#, Word#, Word#, Int##)
1119 $(namePat 'GHC.Prim.decodeDouble_2Int#) | [i] <- doubleLiterals' args
1120 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
1121 (Just tupTc) = UniqMap.lookup tupTcNm tcm
1122 [tupDc] = tyConDataCons tupTc
1123 !(D# a) = wordToDouble i
1124 !(# p, q, r, s #) = decodeDouble_2Int# a
1125 in reduce $
1126 mkApps (Data tupDc) (map Right tyArgs ++
1127 [ Left (Literal . IntLiteral . toInteger $ I# p)
1128 , Left (Literal . WordLiteral . toInteger $ W# q)
1129 , Left (Literal . WordLiteral . toInteger $ W# r)
1130 , Left (Literal . IntLiteral . toInteger $ I# s)])
1131 -- decodeDouble_Int64# :: Double# -> (# Int64#, Int# #)
1132 $(namePat 'GHC.Prim.decodeDouble_Int64#) | [i] <- doubleLiterals' args
1133 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
1134 (Just tupTc) = UniqMap.lookup tupTcNm tcm
1135 [tupDc] = tyConDataCons tupTc
1136 !(D# a) = wordToDouble i
1137 !(# p, q #) = decodeDouble_Int64# a
1138 in reduce $
1139 mkApps (Data tupDc) (map Right tyArgs ++
1140 #if MIN_VERSION_ghc_prim(0,9,0)
1141 [ Left (Literal . Int64Literal . toInteger $ I64# p)
1142 #else
1143 [ Left (Literal . IntLiteral . toInteger $ I64# p)
1144 #endif
1145 , Left (Literal . IntLiteral . toInteger $ I# q)])
1146
1147 --------
1148 -- Float
1149 --------
1150 $(namePat 'GHC.Prim.gtFloat#) | Just r <- liftFFI gtFloat# args
1151 -> reduce r
1152 $(namePat 'GHC.Prim.geFloat#) | Just r <- liftFFI geFloat# args
1153 -> reduce r
1154 $(namePat 'GHC.Prim.eqFloat#) | Just r <- liftFFI eqFloat# args
1155 -> reduce r
1156 $(namePat 'GHC.Prim.neFloat#) | Just r <- liftFFI neFloat# args
1157 -> reduce r
1158 $(namePat 'GHC.Prim.ltFloat#) | Just r <- liftFFI ltFloat# args
1159 -> reduce r
1160 $(namePat 'GHC.Prim.leFloat#) | Just r <- liftFFI leFloat# args
1161 -> reduce r
1162
1163 $(namePat 'GHC.Prim.plusFloat#) | Just r <- liftFFF plusFloat# args
1164 -> reduce r
1165 $(namePat 'GHC.Prim.minusFloat#) | Just r <- liftFFF minusFloat# args
1166 -> reduce r
1167 $(namePat 'GHC.Prim.timesFloat#) | Just r <- liftFFF timesFloat# args
1168 -> reduce r
1169 $(namePat 'GHC.Prim.divideFloat#) | Just r <- liftFFF divideFloat# args
1170 -> reduce r
1171
1172 $(namePat 'GHC.Prim.negateFloat#) | Just r <- liftFF negateFloat# args
1173 -> reduce r
1174 $(namePat 'GHC.Prim.fabsFloat#) | Just r <- liftFF fabsFloat# args
1175 -> reduce r
1176
1177 $(namePat 'GHC.Prim.float2Int#) | [i] <- floatLiterals' args
1178 -> let !(F# a) = wordToFloat i
1179 r = float2Int# a
1180 in reduce . Literal . IntLiteral . toInteger $ I# r
1181
1182 $(namePat 'GHC.Prim.expFloat#) | Just r <- liftFF expFloat# args
1183 -> reduce r
1184 $(namePat 'GHC.Prim.logFloat#) | Just r <- liftFF logFloat# args
1185 -> reduce r
1186 $(namePat 'GHC.Prim.sqrtFloat#) | Just r <- liftFF sqrtFloat# args
1187 -> reduce r
1188 $(namePat 'GHC.Prim.sinFloat#) | Just r <- liftFF sinFloat# args
1189 -> reduce r
1190 $(namePat 'GHC.Prim.cosFloat#) | Just r <- liftFF cosFloat# args
1191 -> reduce r
1192 $(namePat 'GHC.Prim.tanFloat#) | Just r <- liftFF tanFloat# args
1193 -> reduce r
1194 $(namePat 'GHC.Prim.asinFloat#) | Just r <- liftFF asinFloat# args
1195 -> reduce r
1196 $(namePat 'GHC.Prim.acosFloat#) | Just r <- liftFF acosFloat# args
1197 -> reduce r
1198 $(namePat 'GHC.Prim.atanFloat#) | Just r <- liftFF atanFloat# args
1199 -> reduce r
1200 $(namePat 'GHC.Prim.sinhFloat#) | Just r <- liftFF sinhFloat# args
1201 -> reduce r
1202 $(namePat 'GHC.Prim.coshFloat#) | Just r <- liftFF coshFloat# args
1203 -> reduce r
1204 $(namePat 'GHC.Prim.tanhFloat#) | Just r <- liftFF tanhFloat# args
1205 -> reduce r
1206 $(namePat 'GHC.Prim.powerFloat#) | Just r <- liftFFF powerFloat# args
1207 -> reduce r
1208
1209 -- GHC.Float.asinh -- XXX: Very fragile
1210 -- $w$casinh is the Double specialisation of asinh
1211 -- $w$casinh1 is the Float specialisation of asinh
1212 "GHC.Float.$w$casinh" | Just r <- liftDD go args
1213 -> reduce r
1214 where go f = case asinh (D# f) of
1215 D# f' -> f'
1216 "GHC.Float.$w$casinh1" | Just r <- liftFF go args
1217 -> reduce r
1218 where go f = case asinh (F# f) of
1219 F# f' -> f'
1220
1221 #if MIN_VERSION_ghc(8,7,0)
1222 $(namePat 'GHC.Prim.asinhFloat#) | Just r <- liftFF asinhFloat# args
1223 -> reduce r
1224 $(namePat 'GHC.Prim.acoshFloat#) | Just r <- liftFF acoshFloat# args
1225 -> reduce r
1226 $(namePat 'GHC.Prim.atanhFloat#) | Just r <- liftFF atanhFloat# args
1227 -> reduce r
1228 #endif
1229
1230 $(namePat 'GHC.Prim.float2Double#) | [i] <- floatLiterals' args
1231 -> let !(F# a) = wordToFloat i
1232 r = float2Double# a
1233 in reduce . Literal . DoubleLiteral . doubleToWord $ D# r
1234
1235
1236 $(namePat 'GHC.Prim.newByteArray#)
1237 | [iV,PrimVal rwTy _ _] <- args
1238 , [i] <- intLiterals' [iV]
1239 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
1240 (Just tupTc) = UniqMap.lookup tupTcNm tcm
1241 [tupDc] = tyConDataCons tupTc
1242 p = primCount mach
1243 lit = Literal (ByteArrayLiteral (fromList (List.genericReplicate i 0)))
1244 mbaTy = mkFunTy intPrimTy (last tyArgs)
1245 newE = mkApps (Data tupDc) (map Right tyArgs ++
1246 [Left (Prim rwTy)
1247 ,Left (mkApps (Prim (PrimInfo (showt ''MutableByteArray#) mbaTy WorkNever SingleResult NoUnfolding))
1248 [Left (Literal . IntLiteral $ toInteger p)])
1249 ])
1250 in Just . setTerm newE $ primInsert p lit mach
1251
1252 $(namePat 'GHC.Prim.setByteArray#)
1253 | [PrimVal _mbaTy _ [baV]
1254 ,offV,lenV,cV
1255 ,PrimVal rwTy _ _
1256 ] <- args
1257 , [ba,off,len,c] <- intLiterals' [baV,offV,lenV,cV]
1258 -> let Just (Literal (ByteArrayLiteral ba1)) =
1259 primLookup (fromInteger ba) mach
1260 !(I# off') = fromInteger off
1261 !(I# len') = fromInteger len
1262 !(I# c') = fromInteger c
1263 ba2 = unsafeDupablePerformIO $ do
1264 BA.MutableByteArray mba <- BA.unsafeThawByteArray ba1
1265 svoid (setByteArray# mba off' len' c')
1266 BA.unsafeFreezeByteArray (BA.MutableByteArray mba)
1267 ba3 = Literal (ByteArrayLiteral ba2)
1268 in Just . setTerm (Prim rwTy) $ primUpdate (fromInteger ba) ba3 mach
1269
1270 $(namePat 'GHC.Prim.writeWordArray#)
1271 | [PrimVal _mbaTy _ [baV]
1272 ,iV,wV
1273 ,PrimVal rwTy _ _
1274 ] <- args
1275 , [ba,i] <- intLiterals' [baV,iV]
1276 , [w] <- wordLiterals' [wV]
1277 -> let Just (Literal (ByteArrayLiteral ba1)) =
1278 primLookup (fromInteger ba) mach
1279 !(I# i') = fromInteger i
1280 !(W# w') = fromIntegral w
1281 ba2 = unsafeDupablePerformIO $ do
1282 BA.MutableByteArray mba <- BA.unsafeThawByteArray ba1
1283 svoid (writeWordArray# mba i' w')
1284 BA.unsafeFreezeByteArray (BA.MutableByteArray mba)
1285 ba3 = Literal (ByteArrayLiteral ba2)
1286 in Just . setTerm (Prim rwTy) $ primUpdate (fromInteger ba) ba3 mach
1287
1288 $(namePat 'GHC.Prim.unsafeFreezeByteArray#)
1289 | [PrimVal _mbaTy _ [baV]
1290 ,PrimVal rwTy _ _
1291 ] <- args
1292 , [ba] <- intLiterals' [baV]
1293 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
1294 (Just tupTc) = UniqMap.lookup tupTcNm tcm
1295 [tupDc] = tyConDataCons tupTc
1296 Just ba' = primLookup (fromInteger ba) mach
1297 in reduce $ mkApps (Data tupDc) (map Right tyArgs ++
1298 [Left (Prim rwTy)
1299 ,Left ba'])
1300
1301 $(namePat 'GHC.Prim.sizeofByteArray#)
1302 | [Lit (ByteArrayLiteral ba)] <- args
1303 -> reduce (Literal (IntLiteral (toInteger (BA.sizeofByteArray ba))))
1304
1305 $(namePat 'GHC.Prim.indexWordArray#)
1306 | [Lit (ByteArrayLiteral (BA.ByteArray ba)),iV] <- args
1307 , [i] <- intLiterals' [iV]
1308 -> let !(I# i') = fromInteger i
1309 !w = indexWordArray# ba i'
1310 in reduce (Literal (WordLiteral (toInteger (W# w))))
1311
1312 -- XXX: Primitive does not exist?
1313 "GHC.Prim.getSizeofMutBigNat#"
1314 | [PrimVal _mbaTy _ [baV]
1315 ,PrimVal rwTy _ _
1316 ] <- args
1317 , [ba] <- intLiterals' [baV]
1318 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
1319 (Just tupTc) = UniqMap.lookup tupTcNm tcm
1320 [tupDc] = tyConDataCons tupTc
1321 Just (Literal (ByteArrayLiteral ba')) = primLookup (fromInteger ba) mach
1322 lit = Literal (IntLiteral (toInteger (BA.sizeofByteArray ba')))
1323 in reduce $ mkApps (Data tupDc) (map Right tyArgs ++
1324 [Left (Prim rwTy)
1325 ,Left lit])
1326
1327 $(namePat 'GHC.Prim.resizeMutableByteArray#)
1328 | [PrimVal mbaTy _ [baV]
1329 ,iV
1330 ,PrimVal rwTy _ _
1331 ] <- args
1332 , [ba,i] <- intLiterals' [baV,iV]
1333 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
1334 (Just tupTc) = UniqMap.lookup tupTcNm tcm
1335 [tupDc] = tyConDataCons tupTc
1336 p = primCount mach
1337 Just (Literal (ByteArrayLiteral ba1))
1338 = primLookup (fromInteger ba) mach
1339 !(I# i') = fromInteger i
1340 ba2 = unsafeDupablePerformIO $ do
1341 BA.MutableByteArray mba <- BA.unsafeThawByteArray ba1
1342 mba' <- IO (\s -> case resizeMutableByteArray# mba i' s of
1343 (# s', mba' #) -> (# s', BA.MutableByteArray mba' #))
1344 BA.unsafeFreezeByteArray mba'
1345 ba3 = Literal (ByteArrayLiteral ba2)
1346 newE = mkApps (Data tupDc) (map Right tyArgs ++
1347 [Left (Prim rwTy)
1348 ,Left (mkApps (Prim mbaTy)
1349 [Left (Literal . IntLiteral $ toInteger p)])
1350 ])
1351 in Just . setTerm newE $ primInsert p ba3 mach
1352
1353 $(namePat 'GHC.Prim.shrinkMutableByteArray#)
1354 | [PrimVal _mbaTy _ [baV]
1355 ,lenV
1356 ,PrimVal rwTy _ _
1357 ] <- args
1358 , [ba,len] <- intLiterals' [baV,lenV]
1359 -> let Just (Literal (ByteArrayLiteral ba1)) =
1360 primLookup (fromInteger ba) mach
1361 !(I# len') = fromInteger len
1362 ba2 = unsafeDupablePerformIO $ do
1363 BA.MutableByteArray mba <- BA.unsafeThawByteArray ba1
1364 svoid (shrinkMutableByteArray# mba len')
1365 BA.unsafeFreezeByteArray (BA.MutableByteArray mba)
1366 ba3 = Literal (ByteArrayLiteral ba2)
1367 in Just . setTerm (Prim rwTy) $ primUpdate (fromInteger ba) ba3 mach
1368
1369 $(namePat 'GHC.Prim.copyByteArray#)
1370 | [Lit (ByteArrayLiteral (BA.ByteArray src_ba))
1371 ,src_offV
1372 ,PrimVal _mbaTy _ [dst_mbaV]
1373 ,dst_offV, nV
1374 ,PrimVal rwTy _ _
1375 ] <- args
1376 , [src_off,dst_mba,dst_off,n] <- intLiterals' [src_offV,dst_mbaV,dst_offV,nV]
1377 -> let Just (Literal (ByteArrayLiteral dst_ba)) =
1378 primLookup (fromInteger dst_mba) mach
1379 !(I# src_off') = fromInteger src_off
1380 !(I# dst_off') = fromInteger dst_off
1381 !(I# n') = fromInteger n
1382 ba2 = unsafeDupablePerformIO $ do
1383 BA.MutableByteArray dst_mba1 <- BA.unsafeThawByteArray dst_ba
1384 svoid (copyByteArray# src_ba src_off' dst_mba1 dst_off' n')
1385 BA.unsafeFreezeByteArray (BA.MutableByteArray dst_mba1)
1386 ba3 = Literal (ByteArrayLiteral ba2)
1387 in Just . setTerm (Prim rwTy) $ primUpdate (fromInteger dst_mba) ba3 mach
1388
1389 $(namePat 'GHC.Prim.readWordArray#)
1390 | [PrimVal _mbaTy _ [baV]
1391 ,offV
1392 ,PrimVal rwTy _ _
1393 ] <- args
1394 , [ba,off] <- intLiterals' [baV,offV]
1395 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
1396 (Just tupTc) = UniqMap.lookup tupTcNm tcm
1397 [tupDc] = tyConDataCons tupTc
1398 Just (Literal (ByteArrayLiteral ba1)) =
1399 primLookup (fromInteger ba) mach
1400 !(I# off') = fromInteger off
1401 w = unsafeDupablePerformIO $ do
1402 BA.MutableByteArray mba <- BA.unsafeThawByteArray ba1
1403 IO (\s -> case readWordArray# mba off' s of
1404 (# s', w' #) -> (# s', W# w' #))
1405 newE = mkApps (Data tupDc) (map Right tyArgs ++
1406 [Left (Prim rwTy)
1407 ,Left (Literal (WordLiteral (toInteger w)))
1408 ])
1409 in reduce newE
1410
1411 $(namePat 'GHC.Prim.copyAddrToByteArray#)
1412 | [ Lit (StringLiteral addr)
1413 , PrimVal _mbaTy _ [dst_mbaV]
1414 , offV, lenV
1415 , PrimVal rwTy _ _
1416 ] <- args
1417 , [off,len,dst_mba] <- intLiterals' [offV, lenV, dst_mbaV]
1418 -> let Just (Literal (ByteArrayLiteral dst_ba)) =
1419 primLookup (fromInteger dst_mba) mach
1420 !(I# off') = fromInteger off
1421 !(I# len') = fromInteger len
1422 !(BS.PS (ForeignPtr addr' _) _ _) = BS.packChars addr
1423 ba2 = unsafeDupablePerformIO $ do
1424 BA.MutableByteArray dst_mba1 <- BA.unsafeThawByteArray dst_ba
1425 svoid (copyAddrToByteArray# addr' dst_mba1 off' len')
1426 BA.unsafeFreezeByteArray (BA.MutableByteArray dst_mba1)
1427 ba3 = Literal (ByteArrayLiteral ba2)
1428 in Just . setTerm (Prim rwTy) $ primUpdate (fromInteger dst_mba) ba3 mach
1429
1430 -- decodeFloat_Int# :: Float# -> (#Int#, Int##)
1431 $(namePat 'GHC.Prim.decodeFloat_Int#) | [i] <- floatLiterals' args
1432 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
1433 (Just tupTc) = UniqMap.lookup tupTcNm tcm
1434 [tupDc] = tyConDataCons tupTc
1435 !(F# a) = wordToFloat i
1436 !(# p, q #) = decodeFloat_Int# a
1437 in reduce $
1438 mkApps (Data tupDc) (map Right tyArgs ++
1439 [ Left (Literal . IntLiteral . toInteger $ I# p)
1440 , Left (Literal . IntLiteral . toInteger $ I# q)])
1441
1442 $(namePat 'GHC.Prim.tagToEnum#)
1443 | [ConstTy (TyCon tcN)] <- tys
1444 , [Lit (IntLiteral i)] <- args
1445 -> let dc = do { tc <- UniqMap.lookup tcN tcm
1446 ; let dcs = tyConDataCons tc
1447 ; List.find ((== (i+1)) . toInteger . dcTag) dcs
1448 }
1449 in (\e -> setTerm (Data e) mach) <$> dc
1450
1451 #if MIN_VERSION_ghc_prim(0,12,0)
1452 $(namePat 'GHC.PrimopWrappers.dataToTagSmall#)
1453 | [DC dc _] <- args
1454 -> reduce (Literal (IntLiteral (toInteger (dcTag dc - 1))))
1455
1456 $(namePat 'GHC.PrimopWrappers.dataToTagLarge#)
1457 | [DC dc _] <- args
1458 -> reduce (Literal (IntLiteral (toInteger (dcTag dc - 1))))
1459 #endif
1460 #if MIN_VERSION_ghc(9,10,0)
1461 $(namePat 'GHC.Prim.dataToTagSmall#)
1462 | [DC dc _] <- args
1463 -> reduce (Literal (IntLiteral (toInteger (dcTag dc - 1))))
1464 $(namePat 'GHC.Prim.dataToTagLarge#)
1465 | [DC dc _] <- args
1466 -> reduce (Literal (IntLiteral (toInteger (dcTag dc - 1))))
1467 $(namePat 'GHC.Base.dataToTag#)
1468 | [DC dc _] <- args
1469 -> reduce (Literal (IntLiteral (toInteger (dcTag dc - 1))))
1470 #else
1471 $(namePat 'GHC.Prim.dataToTag#)
1472 | [DC dc _] <- args
1473 -> reduce (Literal (IntLiteral (toInteger (dcTag dc - 1))))
1474 #endif
1475
1476 $(namePat 'GHC.Classes.eqInt) | Just (i,j) <- intCLiterals args
1477 -> reduce (boolToBoolLiteral tcm ty (i == j))
1478
1479 $(namePat 'GHC.Classes.neInt) | Just (i,j) <- intCLiterals args
1480 -> reduce (boolToBoolLiteral tcm ty (i /= j))
1481
1482 $(namePat 'GHC.Classes.leInt) | Just (i,j) <- intCLiterals args
1483 -> reduce (boolToBoolLiteral tcm ty (i <= j))
1484
1485 $(namePat 'GHC.Classes.ltInt) | Just (i,j) <- intCLiterals args
1486 -> reduce (boolToBoolLiteral tcm ty (i < j))
1487
1488 $(namePat 'GHC.Classes.geInt) | Just (i,j) <- intCLiterals args
1489 -> reduce (boolToBoolLiteral tcm ty (i >= j))
1490
1491 $(namePat 'GHC.Classes.gtInt) | Just (i,j) <- intCLiterals args
1492 -> reduce (boolToBoolLiteral tcm ty (i > j))
1493
1494 $(namePat '(GHC.Classes.&&))
1495 | [ lArg , rArg ] <- args
1496 , eval <- Evaluator ghcStep ghcUnwind ghcPrimStep ghcPrimUnwind
1497 -- evaluation of the arguments is deferred until the evaluation of the ghcPrimUnwindWith
1498 -- to make `&&` lazy in both arguments
1499 , mach1@Machine{mStack=[],mTerm=lArgWHNF} <- whnf eval tcm True (setTerm (valToTerm lArg) $ stackClear mach)
1500 , mach2@Machine{mStack=[],mTerm=rArgWHNF} <- whnf eval tcm True (setTerm (valToTerm rArg) $ stackClear mach1)
1501 -> case [ lArgWHNF, rArgWHNF ] of
1502 [ Data lCon, Data rCon ] ->
1503 Just $ mach2
1504 { mStack = mStack mach
1505 , mTerm = boolToBoolLiteral tcm ty (isTrueDC lCon && isTrueDC rCon)
1506 }
1507
1508 [ Data lCon, _ ]
1509 | isTrueDC lCon -> reduce rArgWHNF
1510 | otherwise -> reduce (boolToBoolLiteral tcm ty False)
1511
1512 [ _, Data rCon ]
1513 | isTrueDC rCon -> reduce lArgWHNF
1514 | otherwise -> reduce (boolToBoolLiteral tcm ty False)
1515
1516 _ -> Nothing
1517
1518 $(namePat '(GHC.Classes.||))
1519 | [ lArg , rArg ] <- args
1520 , eval <- Evaluator ghcStep ghcUnwind ghcPrimStep ghcPrimUnwind
1521 -- evaluation of the arguments is deferred until the evaluation of the ghcPrimUnwindWith
1522 -- to make `||` lazy in both arguments
1523 , mach1@Machine{mStack=[],mTerm=lArgWHNF} <- whnf eval tcm True (setTerm (valToTerm lArg) $ stackClear mach)
1524 , mach2@Machine{mStack=[],mTerm=rArgWHNF} <- whnf eval tcm True (setTerm (valToTerm rArg) $ stackClear mach1)
1525 -> case [ lArgWHNF, rArgWHNF ] of
1526 [ Data lCon, Data rCon ] ->
1527 Just $ mach2
1528 { mStack = mStack mach
1529 , mTerm = boolToBoolLiteral tcm ty (isTrueDC lCon || isTrueDC rCon)
1530 }
1531
1532 [ Data lCon, _ ]
1533 | isFalseDC lCon -> reduce rArgWHNF
1534 | otherwise -> reduce (boolToBoolLiteral tcm ty True)
1535
1536 [ _, Data rCon ]
1537 | isFalseDC rCon -> reduce lArgWHNF
1538 | otherwise -> reduce (boolToBoolLiteral tcm ty True)
1539
1540 _ -> Nothing
1541
1542 $(namePat 'GHC.Classes.divInt#) | Just (i,j) <- intLiterals args
1543 -> reduce (integerToIntLiteral (i `div` j))
1544
1545 -- modInt# :: Int# -> Int# -> Int#
1546 $(namePat 'GHC.Classes.modInt#)
1547 | [dividend, divisor] <- intLiterals' args
1548 ->
1549 if divisor == 0 then
1550 let iTy = snd (splitFunForallTy ty) in
1551 reduce (TyApp (Prim NP.undefined) iTy)
1552 else
1553 reduce (Literal (IntLiteral (dividend `mod` divisor)))
1554
1555 $(namePat 'GHC.Classes.not)
1556 | [DC bCon _] <- args
1557 -> reduce (boolToBoolLiteral tcm ty (nameOcc (dcName bCon) == showt 'False))
1558
1559 #if MIN_VERSION_base(4,15,0)
1560 $(namePat 'GHC.Num.Integer.integerLogBase#)
1561 | Just (a,b) <- integerLiterals args
1562 , Just c <- flogBase a b
1563 -> (reduce . Literal . WordLiteral . toInteger) c
1564
1565 #if MIN_VERSION_base(4,16,0)
1566 $(namePat 'GHC.Float.integerToFloat#)
1567 #else
1568 $(namePat 'GHC.Num.integerToFloat#)
1569 #endif
1570 | [v] <- args
1571 , Just i <- integerLiteral v
1572 -> reduce . Literal . FloatLiteral . floatToWord $ F# (integerToFloat# i)
1573
1574 #if MIN_VERSION_base(4,16,0)
1575 $(namePat 'GHC.Float.integerToDouble#)
1576 #else
1577 $(namePat 'GHC.Num.integerToDouble#)
1578 #endif
1579 | [v] <- args
1580 , Just i <- integerLiteral v
1581 -> reduce . Literal . DoubleLiteral . doubleToWord $ D# (integerToDouble# i)
1582
1583 $(namePat 'GHC.Num.naturalLogBase#)
1584 | Just (a,b) <- naturalLiterals args
1585 , Just c <- flogBase a b
1586 -> (reduce . Literal . WordLiteral . toInteger) c
1587 #else
1588 $(namePat 'GHC.Integer.Logarithms.integerLogBase#)
1589 | Just (a,b) <- integerLiterals args
1590 , Just c <- flogBase a b
1591 -> (reduce . Literal . IntLiteral . toInteger) c
1592 #endif
1593
1594 #if !MIN_VERSION_base(4,15,0)
1595 "GHC.Integer.Type.smallInteger"
1596 | [Lit (IntLiteral i)] <- args
1597 -> reduce (Literal (IntegerLiteral i))
1598 #endif
1599
1600 #if MIN_VERSION_base(4,15,0)
1601 $(namePat 'GHC.Num.Integer.integerToInt#)
1602 #else
1603 "GHC.Integer.Type.integerToInt"
1604 #endif
1605 | [i] <- integerLiterals' args
1606 -> reduce (integerToIntLiteral i)
1607
1608 #if MIN_VERSION_base(4,15,0)
1609 $(namePat 'GHC.Num.Integer.integerDecodeDouble#) -- :: Double# -> (#Integer, Int##)
1610 #else
1611 "GHC.Integer.Type.decodeDoubleInteger" -- :: Double# -> (#Integer, Int##)
1612 #endif
1613 | [Lit (DoubleLiteral i)] <- args
1614 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
1615 (Just tupTc) = UniqMap.lookup tupTcNm tcm
1616 [tupDc] = tyConDataCons tupTc
1617 !(D# a) = wordToDouble i
1618 !(# b, c #) = decodeDoubleInteger a
1619 in reduce $
1620 mkApps (Data tupDc) (map Right tyArgs ++
1621 [ Left (integerToIntegerLiteral b)
1622 , Left (integerToIntLiteral . toInteger $ I# c)])
1623
1624 #if MIN_VERSION_base(4,15,0)
1625 $(namePat 'GHC.Num.Integer.integerEncodeDouble#) -- :: Integer -> Int# -> Double
1626 #else
1627 "GHC.Integer.Type.encodeDoubleInteger" -- :: Integer -> Int# -> Double
1628 #endif
1629 | [iV, Lit (IntLiteral j)] <- args
1630 , [i] <- integerLiterals' [iV]
1631 -> let !(I# k') = fromInteger j
1632 r = encodeDoubleInteger i k'
1633 in reduce . Literal . DoubleLiteral . doubleToWord $ D# r
1634
1635 #if MIN_VERSION_base(4,15,0)
1636 $(namePat 'GHC.Num.Integer.integerEncodeFloat#)
1637 | [iV, Lit (IntLiteral j)] <- args
1638 , [i] <- integerLiterals' [iV]
1639 -> let !(I# k') = fromInteger j
1640 r = integerEncodeFloat# i k'
1641 in reduce . Literal . FloatLiteral . floatToWord $ F# r
1642 #endif
1643
1644 #if MIN_VERSION_base(4,15,0)
1645 $(namePat 'GHC.Num.Integer.integerQuotRem#) -- :: Integer -> Integer -> (#Integer, Integer#)
1646 #else
1647 "GHC.Integer.Type.quotRemInteger" -- :: Integer -> Integer -> (#Integer, Integer#)
1648 #endif
1649 | [i, j] <- integerLiterals' args
1650 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
1651 (Just tupTc) = UniqMap.lookup tupTcNm tcm
1652 [tupDc] = tyConDataCons tupTc
1653 (q,r) = quotRem i j
1654 in reduce $
1655 mkApps (Data tupDc) (map Right tyArgs ++
1656 [ Left $ catchDivByZero (integerToIntegerLiteral q)
1657 , Left $ catchDivByZero (integerToIntegerLiteral r)])
1658
1659 #if MIN_VERSION_base(4,15,0)
1660 $(namePat 'GHC.Num.Integer.integerAdd)
1661 #else
1662 "GHC.Integer.Type.plusInteger"
1663 #endif
1664 | Just (i,j) <- integerLiterals args
1665 -> reduce (integerToIntegerLiteral (i+j))
1666
1667 #if MIN_VERSION_base(4,15,0)
1668 $(namePat 'GHC.Num.Integer.integerSub)
1669 #else
1670 "GHC.Integer.Type.minusInteger"
1671 #endif
1672 | Just (i,j) <- integerLiterals args
1673 -> reduce (integerToIntegerLiteral (i-j))
1674
1675 #if MIN_VERSION_base(4,15,0)
1676 $(namePat 'GHC.Num.Integer.integerMul)
1677 #else
1678 "GHC.Integer.Type.timesInteger"
1679 #endif
1680 | Just (i,j) <- integerLiterals args
1681 -> reduce (integerToIntegerLiteral (i*j))
1682
1683 #if MIN_VERSION_base(4,15,0)
1684 $(namePat 'GHC.Num.Integer.integerNegate)
1685 #else
1686 "GHC.Integer.Type.negateInteger"
1687 #endif
1688 | [i] <- integerLiterals' args
1689 -> reduce (integerToIntegerLiteral (negate i))
1690
1691 #if MIN_VERSION_base(4,15,0)
1692 $(namePat 'GHC.Num.Integer.integerDiv)
1693 #else
1694 "GHC.Integer.Type.divInteger"
1695 #endif
1696 | Just (i,j) <- integerLiterals args
1697 -> reduce $ catchDivByZero (integerToIntegerLiteral (i `div` j))
1698
1699 #if MIN_VERSION_base(4,15,0)
1700 $(namePat 'GHC.Num.Integer.integerMod)
1701 #else
1702 "GHC.Integer.Type.modInteger"
1703 #endif
1704 | Just (i,j) <- integerLiterals args
1705 -> reduce $ catchDivByZero (integerToIntegerLiteral (i `mod` j))
1706
1707 #if MIN_VERSION_base(4,15,0)
1708 $(namePat 'GHC.Num.Integer.integerQuot)
1709 #else
1710 "GHC.Integer.Type.quotInteger"
1711 #endif
1712 | Just (i,j) <- integerLiterals args
1713 -> reduce $ catchDivByZero (integerToIntegerLiteral (i `quot` j))
1714
1715 #if MIN_VERSION_base(4,15,0)
1716 $(namePat 'GHC.Num.Integer.integerRem)
1717 #else
1718 "GHC.Integer.Type.remInteger"
1719 #endif
1720 | Just (i,j) <- integerLiterals args
1721 -> reduce $ catchDivByZero (integerToIntegerLiteral (i `rem` j))
1722
1723 #if MIN_VERSION_base(4,15,0)
1724 $(namePat 'GHC.Num.Integer.integerDivMod#)
1725 #else
1726 "GHC.Integer.Type.divModInteger"
1727 #endif
1728 | Just (i,j) <- integerLiterals args
1729 -> let (_,tyView -> TyConApp ubTupTcNm [liftedKi,_,intTy,_]) = splitFunForallTy ty
1730 (Just ubTupTc) = UniqMap.lookup ubTupTcNm tcm
1731 [ubTupDc] = tyConDataCons ubTupTc
1732 (d,m) = divMod i j
1733 in reduce $
1734 mkApps (Data ubTupDc) [ Right liftedKi, Right liftedKi
1735 , Right intTy, Right intTy
1736 , Left $ catchDivByZero (Literal (IntegerLiteral d))
1737 , Left $ catchDivByZero (Literal (IntegerLiteral m))
1738 ]
1739
1740 #if MIN_VERSION_base(4,15,0)
1741 $(namePat 'GHC.Num.Integer.integerGt)
1742 #else
1743 "GHC.Integer.Type.gtInteger"
1744 #endif
1745 | Just (i,j) <- integerLiterals args
1746 -> reduce (boolToBoolLiteral tcm ty (i > j))
1747
1748 #if MIN_VERSION_base(4,15,0)
1749 $(namePat 'GHC.Num.Integer.integerGe)
1750 #else
1751 "GHC.Integer.Type.geInteger"
1752 #endif
1753 | Just (i,j) <- integerLiterals args
1754 -> reduce (boolToBoolLiteral tcm ty (i >= j))
1755
1756 #if MIN_VERSION_base(4,15,0)
1757 $(namePat 'GHC.Num.Integer.integerEq)
1758 #else
1759 "GHC.Integer.Type.eqInteger"
1760 #endif
1761 | Just (i,j) <- integerLiterals args
1762 -> reduce (boolToBoolLiteral tcm ty (i == j))
1763
1764 #if MIN_VERSION_base(4,15,0)
1765 $(namePat 'GHC.Num.Integer.integerNe)
1766 #else
1767 "GHC.Integer.Type.neqInteger"
1768 #endif
1769 | Just (i,j) <- integerLiterals args
1770 -> reduce (boolToBoolLiteral tcm ty (i /= j))
1771
1772 #if MIN_VERSION_base(4,15,0)
1773 $(namePat 'GHC.Num.Integer.integerLt)
1774 #else
1775 "GHC.Integer.Type.ltInteger"
1776 #endif
1777 | Just (i,j) <- integerLiterals args
1778 -> reduce (boolToBoolLiteral tcm ty (i < j))
1779
1780 #if MIN_VERSION_base(4,15,0)
1781 $(namePat 'GHC.Num.Integer.integerLe)
1782 #else
1783 "GHC.Integer.Type.leInteger"
1784 #endif
1785 | Just (i,j) <- integerLiterals args
1786 -> reduce (boolToBoolLiteral tcm ty (i <= j))
1787
1788 #if MIN_VERSION_base(4,15,0)
1789 $(namePat 'GHC.Num.Integer.integerGt#)
1790 #else
1791 "GHC.Integer.Type.gtInteger#"
1792 #endif
1793 | Just (i,j) <- integerLiterals args
1794 -> reduce (boolToIntLiteral (i > j))
1795
1796 #if MIN_VERSION_base(4,15,0)
1797 $(namePat 'GHC.Num.Integer.integerGe#)
1798 #else
1799 "GHC.Integer.Type.geInteger#"
1800 #endif
1801 | Just (i,j) <- integerLiterals args
1802 -> reduce (boolToIntLiteral (i >= j))
1803
1804 #if MIN_VERSION_base(4,15,0)
1805 $(namePat 'GHC.Num.Integer.integerEq#)
1806 #else
1807 "GHC.Integer.Type.eqInteger#"
1808 #endif
1809 | Just (i,j) <- integerLiterals args
1810 -> reduce (boolToIntLiteral (i == j))
1811
1812 #if MIN_VERSION_base(4,15,0)
1813 $(namePat 'GHC.Num.Integer.integerNe#)
1814 #else
1815 "GHC.Integer.Type.neqInteger#"
1816 #endif
1817 | Just (i,j) <- integerLiterals args
1818 -> reduce (boolToIntLiteral (i /= j))
1819
1820 #if MIN_VERSION_base(4,15,0)
1821 $(namePat 'GHC.Num.Integer.integerLt#)
1822 #else
1823 "GHC.Integer.Type.ltInteger#"
1824 #endif
1825 | Just (i,j) <- integerLiterals args
1826 -> reduce (boolToIntLiteral (i < j))
1827
1828 #if MIN_VERSION_base(4,15,0)
1829 $(namePat 'GHC.Num.Integer.integerLe#)
1830 #else
1831 "GHC.Integer.Type.leInteger#"
1832 #endif
1833 | Just (i,j) <- integerLiterals args
1834 -> reduce (boolToIntLiteral (i <= j))
1835
1836 #if MIN_VERSION_base(4,15,0)
1837 $(namePat 'GHC.Num.Integer.integerCompare)
1838 #else
1839 "GHC.Integer.Type.compareInteger" -- :: Integer -> Integer -> Ordering
1840 #endif
1841 | [i, j] <- integerLiterals' args
1842 -> let -- Get the required result type (viewed as an applied type constructor name)
1843 (_,tyView -> TyConApp tupTcNm []) = splitFunForallTy ty
1844 -- Find the type constructor from the name
1845 (Just tupTc) = UniqMap.lookup tupTcNm tcm
1846 -- Get the data constructors of that type
1847 -- The type is 'Ordering', so they are: 'LT', 'EQ', 'GT'
1848 [ltDc, eqDc, gtDc] = tyConDataCons tupTc
1849 -- Do the actual compile-time evaluation
1850 ordVal = compareInteger i j
1851 in reduce $ case ordVal of
1852 LT -> Data ltDc
1853 EQ -> Data eqDc
1854 GT -> Data gtDc
1855
1856 #if MIN_VERSION_base(4,15,0)
1857 $(namePat 'GHC.Num.Integer.integerShiftR#)
1858 | [iV, Lit (WordLiteral j)] <- args
1859 #else
1860 "GHC.Integer.Type.shiftRInteger"
1861 | [iV, Lit (IntLiteral j)] <- args
1862 #endif
1863 , [i] <- integerLiterals' [iV]
1864 -> reduce (integerToIntegerLiteral (i `shiftR` fromInteger j))
1865
1866 #if MIN_VERSION_base(4,15,0)
1867 $(namePat 'GHC.Num.Integer.integerShiftL#)
1868 | [iV, Lit (WordLiteral j)] <- args
1869 #else
1870 "GHC.Integer.Type.shiftLInteger"
1871 | [iV, Lit (IntLiteral j)] <- args
1872 #endif
1873 , [i] <- integerLiterals' [iV]
1874 -> reduce (integerToIntegerLiteral (i `shiftL` fromInteger j))
1875
1876 #if MIN_VERSION_base(4,15,0)
1877 $(namePat 'GHC.Num.Integer.integerFromWord#)
1878 #else
1879 "GHC.Integer.Type.wordToInteger"
1880 #endif
1881 | [Lit (WordLiteral w)] <- args
1882 -> reduce (Literal (IntegerLiteral w))
1883
1884 #if MIN_VERSION_base(4,15,0)
1885 $(namePat 'GHC.Num.Integer.integerToWord#)
1886 #else
1887 "GHC.Integer.Type.integerToWord"
1888 #endif
1889 | [i] <- integerLiterals' args
1890 -> reduce (integerToWordLiteral i)
1891
1892 #if MIN_VERSION_base(4,15,0)
1893 $(namePat 'GHC.Num.Integer.integerTestBit#) -- :: Integer -> Int# -> Int#
1894 | [Lit (IntegerLiteral i), Lit (WordLiteral j)] <- args
1895 -> reduce (boolToIntLiteral (testBit i (fromInteger j)))
1896 #else
1897 "GHC.Integer.Type.testBitInteger" -- :: Integer -> Int# -> Bool
1898 | [Lit (IntegerLiteral i), Lit (IntLiteral j)] <- args
1899 -> reduce (boolToBoolLiteral tcm ty (testBit i (fromInteger j)))
1900 #endif
1901
1902 #if MIN_VERSION_base(4,15,0)
1903 $(namePat 'GHC.Num.NS)
1904 | [Lit (WordLiteral w)] <- args
1905 -> reduce (Literal (NaturalLiteral w))
1906 $(namePat 'GHC.Num.NB)
1907 | [Lit (ByteArrayLiteral (BA.ByteArray ba))] <- args
1908 -> reduce (Literal (NaturalLiteral (IP ba)))
1909 | [Lit l] <- args
1910 -> error ("NB: " <> show l)
1911 $(namePat 'GHC.Num.Integer.IS)
1912 | [Lit (IntLiteral i)] <- args
1913 -> reduce (Literal (IntegerLiteral i))
1914 $(namePat 'GHC.Num.Integer.IP)
1915 | [Lit (ByteArrayLiteral (BA.ByteArray ba))] <- args
1916 -> reduce (Literal (IntegerLiteral (IP ba)))
1917 | [Lit l] <- args
1918 -> error ("IP: " <> show l)
1919 $(namePat 'GHC.Num.Integer.IN)
1920 | [Lit (ByteArrayLiteral (BA.ByteArray ba))] <- args
1921 -> reduce (Literal (IntegerLiteral (IN ba)))
1922 | [Lit l] <- args
1923 -> error ("IN: " <> show l)
1924 #else
1925 $(namePat 'GHC.Natural.NatS#)
1926 | [Lit (WordLiteral w)] <- args
1927 -> reduce (Literal (NaturalLiteral w))
1928 #endif
1929
1930 #if MIN_VERSION_base(4,15,0)
1931 $(namePat 'GHC.Num.Integer.integerFromNatural)
1932 #else
1933 $(namePat 'GHC.Natural.naturalToInteger)
1934 #endif
1935 | [i] <- naturalLiterals' args
1936 -> reduce (Literal (IntegerLiteral (toInteger i)))
1937
1938 #if MIN_VERSION_base(4,15,0)
1939 $(namePat 'GHC.Num.Integer.integerToNatural)
1940 #else
1941 $(namePat 'GHC.Natural.naturalFromInteger)
1942 #endif
1943 | [i] <- integerLiterals' args
1944 ->
1945 let nTy = snd (splitFunForallTy ty) in
1946 reduce (checkNaturalRange1 nTy i id)
1947
1948 #if MIN_VERSION_base(4,15,0)
1949 $(namePat 'GHC.Num.Integer.integerToNaturalClamp)
1950 | [i] <- integerLiterals' args
1951 -> if i < 0 then
1952 reduce (naturalToNaturalLiteral 0)
1953 else
1954 reduce (naturalToNaturalLiteral (fromInteger i))
1955
1956 $(namePat 'GHC.Num.Integer.integerToNaturalThrow)
1957 | [i] <- integerLiterals' args
1958 -> let nTy = snd (splitFunForallTy ty) in
1959 reduce (checkNaturalRange1 nTy i id)
1960 #endif
1961
1962 #if MIN_VERSION_base(4,17,0) || WORD_SIZE_IN_BITS < 64
1963 $(namePat 'GHC.Num.Integer.integerToInt64#)
1964 | [i] <- integerLiterals' args
1965 -> reduce (integerToInt64Literal i)
1966
1967 $(namePat 'GHC.Num.Integer.integerToWord64#)
1968 | [i] <- integerLiterals' args
1969 -> reduce (integerToWord64Literal i)
1970
1971 #if MIN_VERSION_base(4,17,0)
1972 $(namePat 'GHC.Num.Integer.integerFromWord64#)
1973 | [w] <- word64Literals' args
1974 -> reduce (Literal (IntegerLiteral w))
1975 #endif
1976 #endif
1977
1978 #if !MIN_VERSION_base(4,15,0)
1979 -- GHC.shiftLNatural --- XXX: Fragile worker of GHC.shiflLNatural
1980 "GHC.Natural.$wshiftLNatural"
1981 | [nV,iV] <- args
1982 , [n] <- naturalLiterals' [nV]
1983 , [i] <- fromInteger <$> intLiterals' [iV]
1984 ->
1985 let nTy = snd (splitFunForallTy ty) in
1986 reduce (checkNaturalRange1 nTy n ((flip shiftL) i))
1987 #endif
1988
1989 #if MIN_VERSION_base(4,15,0)
1990 $(namePat 'GHC.Num.naturalAdd)
1991 #else
1992 $(namePat 'GHC.Natural.plusNatural)
1993 #endif
1994 | Just (i,j) <- naturalLiterals args
1995 ->
1996 let nTy = snd (splitFunForallTy ty) in
1997 reduce (checkNaturalRange2 nTy i j (+))
1998
1999 #if MIN_VERSION_base(4,15,0)
2000 $(namePat 'GHC.Num.naturalMul)
2001 #else
2002 $(namePat 'GHC.Natural.timesNatural)
2003 #endif
2004 | Just (i,j) <- naturalLiterals args
2005 ->
2006 let nTy = snd (splitFunForallTy ty) in
2007 reduce (checkNaturalRange2 nTy i j (*))
2008
2009 #if MIN_VERSION_base(4,15,0)
2010 $(namePat 'GHC.Num.Natural.naturalSubUnsafe)
2011 | Just (i,j) <- naturalLiterals args
2012 ->
2013 let nTy = snd (splitFunForallTy ty) in
2014 reduce (checkNaturalRange nTy [i, j] (\[i', j'] ->
2015 naturalToNaturalLiteral (naturalSubUnsafe i' j')))
2016
2017 $(namePat 'GHC.Num.naturalSubThrow)
2018 | Just (i,j) <- naturalLiterals args
2019 ->
2020 let nTy = snd (splitFunForallTy ty) in
2021 reduce (checkNaturalRange nTy [i, j] (\[i', j'] ->
2022 case minusNaturalMaybe i' j' of
2023 Nothing -> checkNaturalRange1 nTy (-1) id
2024 Just n -> naturalToNaturalLiteral n))
2025 #else
2026 $(namePat 'GHC.Natural.minusNatural)
2027 | Just (i,j) <- naturalLiterals args
2028 ->
2029 let nTy = snd (splitFunForallTy ty) in
2030 reduce (checkNaturalRange nTy [i, j] (\[i', j'] ->
2031 case minusNaturalMaybe i' j' of
2032 Nothing -> checkNaturalRange1 nTy (-1) id
2033 Just n -> naturalToNaturalLiteral n))
2034 #endif
2035
2036 #if MIN_VERSION_base(4,15,0)
2037 $(namePat 'GHC.Num.naturalFromWord#)
2038 #else
2039 $(namePat 'GHC.Natural.wordToNatural#)
2040 #endif
2041 | [Lit (WordLiteral w)] <- args
2042 ->
2043 let nTy = snd (splitFunForallTy ty) in
2044 reduce (checkNaturalRange1 nTy w id)
2045
2046 #if MIN_VERSION_base(4,15,0)
2047 $(namePat 'GHC.Num.naturalToWord#)
2048 | [i] <- naturalLiterals' args
2049 -> reduce (integerToWordLiteral i)
2050
2051 $(namePat 'GHC.Num.naturalQuot)
2052 | Just (i,j) <- naturalLiterals args
2053 ->
2054 let nTy = snd (splitFunForallTy ty) in
2055 reduce (checkNaturalRange2 nTy i j quot)
2056
2057 $(namePat 'GHC.Num.naturalRem)
2058 | Just (i,j) <- naturalLiterals args
2059 ->
2060 let nTy = snd (splitFunForallTy ty) in
2061 reduce (checkNaturalRange2 nTy i j rem)
2062 #endif
2063
2064 #if MIN_VERSION_base(4,15,0)
2065 $(namePat 'GHC.Num.naturalQuotRem#) -- :: Natural -> Natural -> (#Natural, Natural#)
2066 | [i, j] <- naturalLiterals' args
2067 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
2068 (Just tupTc) = UniqMap.lookup tupTcNm tcm
2069 [tupDc] = tyConDataCons tupTc
2070 (q,r) = quotRem (fromInteger i) (fromInteger j)
2071 in reduce $
2072 mkApps (Data tupDc) (map Right tyArgs ++
2073 [ Left $ catchDivByZero (naturalToNaturalLiteral q)
2074 , Left $ catchDivByZero (naturalToNaturalLiteral r)])
2075 #endif
2076
2077 #if MIN_VERSION_base(4,15,0)
2078 $(namePat 'GHC.Num.naturalGcd)
2079 #else
2080 $(namePat 'GHC.Natural.gcdNatural)
2081 #endif
2082 | Just (i,j) <- naturalLiterals args
2083 ->
2084 let nTy = snd (splitFunForallTy ty) in
2085 reduce (checkNaturalRange2 nTy i j gcd)
2086
2087 #if MIN_VERSION_base(4,15,0)
2088 $(namePat 'GHC.Num.naturalLcm)
2089 | Just (i,j) <- naturalLiterals args
2090 ->
2091 let nTy = snd (splitFunForallTy ty) in
2092 reduce (checkNaturalRange2 nTy i j lcm)
2093 #endif
2094
2095 #if MIN_VERSION_base(4,15,0)
2096 $(namePat 'GHC.Num.naturalGt#)
2097 | Just (i,j) <- naturalLiterals args
2098 -> reduce (boolToIntLiteral (i > j))
2099
2100 $(namePat 'GHC.Num.naturalGe#)
2101 | Just (i,j) <- naturalLiterals args
2102 -> reduce (boolToIntLiteral (i >= j))
2103
2104 $(namePat 'GHC.Num.naturalEq#)
2105 | Just (i,j) <- naturalLiterals args
2106 -> reduce (boolToIntLiteral (i == j))
2107
2108 $(namePat 'GHC.Num.naturalNe#)
2109 | Just (i,j) <- naturalLiterals args
2110 -> reduce (boolToIntLiteral (i /= j))
2111
2112 $(namePat 'GHC.Num.naturalLt#)
2113 | Just (i,j) <- naturalLiterals args
2114 -> reduce (boolToIntLiteral (i < j))
2115
2116 $(namePat 'GHC.Num.naturalLe#)
2117 | Just (i,j) <- naturalLiterals args
2118 -> reduce (boolToIntLiteral (i <= j))
2119
2120 $(namePat 'GHC.Num.naturalShiftL#)
2121 | [iV, Lit (WordLiteral j)] <- args
2122 , [i] <- naturalLiterals' [iV]
2123 -> reduce (naturalToNaturalLiteral (fromInteger (i `shiftL` fromInteger j)))
2124
2125 $(namePat 'GHC.Num.naturalShiftR#)
2126 | [iV, Lit (WordLiteral j)] <- args
2127 , [i] <- naturalLiterals' [iV]
2128 -> reduce (naturalToNaturalLiteral (fromInteger (i `shiftR` fromInteger j)))
2129
2130 $(namePat 'GHC.Num.naturalCompare)
2131 | [i, j] <- naturalLiterals' args
2132 -> let -- Get the required result type (viewed as an applied type constructor name)
2133 (_,tyView -> TyConApp tupTcNm []) = splitFunForallTy ty
2134 -- Find the type constructor from the name
2135 (Just tupTc) = UniqMap.lookup tupTcNm tcm
2136 -- Get the data constructors of that type
2137 -- The type is 'Ordering', so they are: 'LT', 'EQ', 'GT'
2138 [ltDc, eqDc, gtDc] = tyConDataCons tupTc
2139 -- Do the actual compile-time evaluation
2140 ordVal = compareInteger i j
2141 in reduce $ case ordVal of
2142 LT -> Data ltDc
2143 EQ -> Data eqDc
2144 GT -> Data gtDc
2145
2146 $(namePat 'GHC.Num.naturalSignum)
2147 | [i] <- naturalLiterals' args
2148 -> reduce (Literal (NaturalLiteral (signum i)))
2149
2150 "GHC.Num.Natural.$wnaturalSignum"
2151 | [i] <- naturalLiterals' args
2152 -> reduce (Literal (WordLiteral (signum i)))
2153 #endif
2154
2155 -- GHC.Real.^ -- XXX: Very fragile
2156 -- ^_f, $wf, $wf1 are specialisations of the internal function f in the implementation of (^) in GHC.Real
2157 "GHC.Real.^_f" -- :: Integer -> Integer -> Integer
2158 | [i,j] <- integerLiterals' args
2159 -> reduce (integerToIntegerLiteral $ i ^ j)
2160 "GHC.Real.$wf" -- :: Integer -> Int# -> Integer
2161 | [iV, Lit (IntLiteral j)] <- args
2162 , [i] <- integerLiterals' [iV]
2163 -> reduce (integerToIntegerLiteral $ i ^ j)
2164 "GHC.Real.$wf1" -- :: Int# -> Int# -> Int#
2165 | [Lit (IntLiteral i), Lit (IntLiteral j)] <- args
2166 -> reduce (integerToIntLiteral $ i ^ j)
2167 "GHC.Internal.Real.^_$s$spowImpl2" -- :: Int# -> Integer -> Integer
2168 | [intLiteral -> Just j, integerLiteral -> Just i] <- args
2169 -> reduce (integerToIntLiteral $ i ^ j)
2170 "GHC.Internal.Real.^_$s$spowImpl" -- :: Int -> Integer -> Integer
2171 | [intLiteral -> Just j, integerLiteral -> Just i] <- args
2172 -> reduce (integerToIntLiteral $ i ^ j)
2173 "GHC.Internal.Real.$w$spowImpl" -- :: Integer -> Int# -> Integer
2174 | [integerLiteral -> Just i, intLiteral -> Just j] <- args
2175 -> reduce (integerToIntLiteral $ i ^ j)
2176 "GHC.Internal.Real.$w$spowImpl1" -- :: Int# -> Int# -> Integer
2177 | [intLiteral -> Just i, intLiteral -> Just j] <- args
2178 -> reduce (integerToIntLiteral $ i ^ j)
2179 "GHC.Real.^_$s$spowImpl2" -- :: Int# -> Integer -> Integer
2180 | [intLiteral -> Just j, integerLiteral -> Just i] <- args
2181 -> reduce (integerToIntLiteral $ i ^ j)
2182 "GHC.Real.$w$spowImpl" -- :: Integer -> Int# -> Integer
2183 | [integerLiteral -> Just i, intLiteral -> Just j] <- args
2184 -> reduce (integerToIntLiteral $ i ^ j)
2185 "GHC.Real.$w$spowImpl1" -- :: Int# -> Int# -> Integer
2186 | [intLiteral -> Just i, intLiteral -> Just j] <- args
2187 -> reduce (integerToIntLiteral $ i ^ j)
2188 "GHC.Real.^_$sf2" -- :: Int# -> Integer -> Integer
2189 | [intLiteral -> Just j, integerLiteral -> Just i] <- args
2190 -> reduce (integerToIntLiteral $ i ^ j)
2191
2192 -- Type level ^ -- XXX: Very fragile
2193 -- These is are specialized versions of ^_f, named by some combination of ghc and singletons.
2194 "Data.Singletons.TypeLits.Internal.$fSingI->^@#@$_f" -- ghc-8.6.5, singletons-2.5.1
2195 | [i,j] <- naturalLiterals' args
2196 -> reduce (Literal (NaturalLiteral (i ^ j)))
2197 "Data.Singletons.TypeLits.Internal.%^_f" -- ghc-8.8.1, singletons-2.6
2198 | [i,j] <- naturalLiterals' args
2199 -> reduce (Literal (NaturalLiteral (i ^ j)))
2200
2201 -- XXX: Does it make sense to match on a @NaturalLiteral@ here?
2202 $(namePat 'GHC.TypeLits.natVal)
2203 | [Lit (NaturalLiteral n), _] <- args
2204 -> reduce (integerToIntegerLiteral n)
2205
2206 $(namePat 'GHC.TypeNats.natVal)
2207 | [Lit (NaturalLiteral n), _] <- args
2208 -> reduce (Literal (NaturalLiteral n))
2209
2210 $(namePat 'GHC.TypeNats.someNatVal)
2211 | [Lit (NaturalLiteral n)] <- args
2212 -> let resTy = getResultTy tcm ty tys
2213 in reduce (mkSomeNat tcm n resTy)
2214
2215 -- XXX: Does it make sense to match on a @NaturalLiteral@ here?
2216 $(namePat 'GHC.TypeLits.someNatVal)
2217 | [Lit (NaturalLiteral n)] <- args
2218 -> let resTy = getResultTy tcm ty tys
2219 in reduce (mkSomeNat tcm n resTy)
2220
2221 $(namePat 'GHC.Types.I#)
2222 | isSubj
2223 , [Lit (IntLiteral i)] <- args
2224 -> let (_,tyView -> TyConApp intTcNm []) = splitFunForallTy ty
2225 (Just intTc) = UniqMap.lookup intTcNm tcm
2226 [intDc] = tyConDataCons intTc
2227 in reduce (mkApps (Data intDc) [Left (Literal (IntLiteral i))])
2228
2229 $(namePat 'GHC.Int.I8#)
2230 | isSubj
2231 #if MIN_VERSION_base(4,16,0)
2232 , [Lit (Int8Literal i)] <- args
2233 #else
2234 , [Lit (IntLiteral i)] <- args
2235 #endif
2236 -> let (_,tyView -> TyConApp intTcNm []) = splitFunForallTy ty
2237 (Just intTc) = UniqMap.lookup intTcNm tcm
2238 [intDc] = tyConDataCons intTc
2239 #if MIN_VERSION_base(4,16,0)
2240 in reduce (mkApps (Data intDc) [Left (Literal (Int8Literal i))])
2241 #else
2242 in reduce (mkApps (Data intDc) [Left (Literal (IntLiteral i))])
2243 #endif
2244 $(namePat 'GHC.Int.I16#)
2245 | isSubj
2246 #if MIN_VERSION_base(4,16,0)
2247 , [Lit (Int16Literal i)] <- args
2248 #else
2249 , [Lit (IntLiteral i)] <- args
2250 #endif
2251 -> let (_,tyView -> TyConApp intTcNm []) = splitFunForallTy ty
2252 (Just intTc) = UniqMap.lookup intTcNm tcm
2253 [intDc] = tyConDataCons intTc
2254 #if MIN_VERSION_base(4,16,0)
2255 in reduce (mkApps (Data intDc) [Left (Literal (Int16Literal i))])
2256 #else
2257 in reduce (mkApps (Data intDc) [Left (Literal (IntLiteral i))])
2258 #endif
2259 $(namePat 'GHC.Int.I32#)
2260 | isSubj
2261 #if MIN_VERSION_base(4,16,0)
2262 , [Lit (Int32Literal i)] <- args
2263 #else
2264 , [Lit (IntLiteral i)] <- args
2265 #endif
2266 -> let (_,tyView -> TyConApp intTcNm []) = splitFunForallTy ty
2267 (Just intTc) = UniqMap.lookup intTcNm tcm
2268 [intDc] = tyConDataCons intTc
2269 #if MIN_VERSION_base(4,16,0)
2270 in reduce (mkApps (Data intDc) [Left (Literal (Int32Literal i))])
2271 #else
2272 in reduce (mkApps (Data intDc) [Left (Literal (IntLiteral i))])
2273 #endif
2274 $(namePat 'GHC.Int.I64#)
2275 | isSubj
2276 #if MIN_VERSION_base(4,16,0)
2277 , [Lit (Int64Literal i)] <- args
2278 #else
2279 , [Lit (IntLiteral i)] <- args
2280 #endif
2281 -> let (_,tyView -> TyConApp intTcNm []) = splitFunForallTy ty
2282 (Just intTc) = UniqMap.lookup intTcNm tcm
2283 [intDc] = tyConDataCons intTc
2284 #if MIN_VERSION_base(4,16,0)
2285 in reduce (mkApps (Data intDc) [Left (Literal (Int64Literal i))])
2286 #else
2287 in reduce (mkApps (Data intDc) [Left (Literal (IntLiteral i))])
2288 #endif
2289
2290 $(namePat 'GHC.Word.W8#)
2291 | isSubj
2292 #if MIN_VERSION_base(4,16,0)
2293 , [Lit (Word8Literal c)] <- args
2294 #else
2295 , [Lit (WordLiteral c)] <- args
2296 #endif
2297 -> let (_,tyView -> TyConApp wordTcNm []) = splitFunForallTy ty
2298 (Just wordTc) = UniqMap.lookup wordTcNm tcm
2299 [wordDc] = tyConDataCons wordTc
2300 #if MIN_VERSION_base(4,16,0)
2301 in reduce (mkApps (Data wordDc) [Left (Literal (Word8Literal c))])
2302 #else
2303 in reduce (mkApps (Data wordDc) [Left (Literal (WordLiteral c))])
2304 #endif
2305 $(namePat 'GHC.Word.W16#)
2306 | isSubj
2307 #if MIN_VERSION_base(4,16,0)
2308 , [Lit (Word16Literal c)] <- args
2309 #else
2310 , [Lit (WordLiteral c)] <- args
2311 #endif
2312 -> let (_,tyView -> TyConApp wordTcNm []) = splitFunForallTy ty
2313 (Just wordTc) = UniqMap.lookup wordTcNm tcm
2314 [wordDc] = tyConDataCons wordTc
2315 #if MIN_VERSION_base(4,16,0)
2316 in reduce (mkApps (Data wordDc) [Left (Literal (Word16Literal c))])
2317 #else
2318 in reduce (mkApps (Data wordDc) [Left (Literal (WordLiteral c))])
2319 #endif
2320 $(namePat 'GHC.Word.W32#)
2321 | isSubj
2322 #if MIN_VERSION_base(4,16,0)
2323 , [Lit (Word32Literal c)] <- args
2324 #else
2325 , [Lit (WordLiteral c)] <- args
2326 #endif
2327 -> let (_,tyView -> TyConApp wordTcNm []) = splitFunForallTy ty
2328 (Just wordTc) = UniqMap.lookup wordTcNm tcm
2329 [wordDc] = tyConDataCons wordTc
2330 #if MIN_VERSION_base(4,16,0)
2331 in reduce (mkApps (Data wordDc) [Left (Literal (Word32Literal c))])
2332 #else
2333 in reduce (mkApps (Data wordDc) [Left (Literal (WordLiteral c))])
2334 #endif
2335 $(namePat 'GHC.Word.W64#)
2336 | isSubj
2337 #if MIN_VERSION_base(4,16,0)
2338 , [Lit (Word64Literal c)] <- args
2339 #else
2340 , [Lit (WordLiteral c)] <- args
2341 #endif
2342 -> let (_,tyView -> TyConApp wordTcNm []) = splitFunForallTy ty
2343 (Just wordTc) = UniqMap.lookup wordTcNm tcm
2344 [wordDc] = tyConDataCons wordTc
2345 #if MIN_VERSION_base(4,16,0)
2346 in reduce (mkApps (Data wordDc) [Left (Literal (Word64Literal c))])
2347 #else
2348 in reduce (mkApps (Data wordDc) [Left (Literal (WordLiteral c))])
2349 #endif
2350
2351 $(namePat 'GHC.Types.W#)
2352 | isSubj
2353 , [Lit (WordLiteral i)] <- args
2354 -> let (_,tyView -> TyConApp intTcNm []) = splitFunForallTy ty
2355 (Just intTc) = UniqMap.lookup intTcNm tcm
2356 [intDc] = tyConDataCons intTc
2357 in reduce (mkApps (Data intDc) [Left (Literal (WordLiteral i))])
2358
2359 "GHC.Float.$w$sfromRat''" -- XXX: Very fragile
2360 | [Lit (IntLiteral _minEx)
2361 ,Lit (IntLiteral matDigs)
2362 ,nV
2363 ,dV] <- args
2364 , [n,d] <- integerLiterals' [nV,dV]
2365 -> case fromInteger matDigs of
2366 matDigs'
2367 | matDigs' == floatDigits (undefined :: Float)
2368 -> reduce (Literal (FloatLiteral (floatToWord (fromRational (n :% d)))))
2369 | matDigs' == floatDigits (undefined :: Double)
2370 -> reduce (Literal (DoubleLiteral (doubleToWord (fromRational (n :% d)))))
2371 _ -> error $ $(curLoc) ++ "GHC.Float.$w$sfromRat'': Not a Float or Double"
2372
2373 "GHC.Float.$w$sfromRat''1" -- XXX: Very fragile
2374 | [Lit (IntLiteral _minEx)
2375 ,Lit (IntLiteral matDigs)
2376 ,nV
2377 ,dV] <- args
2378 , [n,d] <- integerLiterals' [nV,dV]
2379 -> case fromInteger matDigs of
2380 matDigs'
2381 | matDigs' == floatDigits (undefined :: Float)
2382 -> reduce (Literal (FloatLiteral (floatToWord (fromRational (n :% d)))))
2383 | matDigs' == floatDigits (undefined :: Double)
2384 -> reduce (Literal (DoubleLiteral (doubleToWord (fromRational (n :% d)))))
2385 _ -> error $ $(curLoc) ++ "GHC.Float.$w$sfromRat'': Not a Float or Double"
2386
2387 #if MIN_VERSION_base(4,15,0)
2388 $(namePat 'GHC.Num.Integer.integerSignum#)
2389 #else
2390 "GHC.Integer.Type.$wsignumInteger" -- XXX: Not super-fragile, but still..
2391 #endif
2392 | [i] <- integerLiterals' args
2393 -> reduce (Literal (IntLiteral (signum i)))
2394
2395 #if MIN_VERSION_base(4,15,0)
2396 $(namePat 'GHC.Num.Integer.integerSignum)
2397 #else
2398 "GHC.Integer.Type.signumInteger"
2399 #endif
2400 | [i] <- integerLiterals' args
2401 -> reduce (Literal (IntegerLiteral (signumInteger i)))
2402
2403 #if MIN_VERSION_base(4,15,0)
2404 "GHC.Num.Integer.$wintegerSignum"
2405 | [i] <- integerLiterals' args
2406 -> reduce (Literal (IntLiteral (signum i)))
2407 #endif
2408
2409 #if MIN_VERSION_base(4,15,0)
2410 $(namePat 'GHC.Num.Integer.integerAbs)
2411 #else
2412 "GHC.Integer.Type.absInteger"
2413 #endif
2414 | [i] <- integerLiterals' args
2415 -> reduce (Literal (IntegerLiteral (absInteger i)))
2416
2417 #if MIN_VERSION_base(4,15,0)
2418 $(namePat 'GHC.Num.Integer.integerBit#)
2419 | [i] <- wordLiterals' args
2420 #else
2421 "GHC.Integer.Type.bitInteger"
2422 | [i] <- intLiterals' args
2423 #endif
2424 -> reduce (Literal (IntegerLiteral (bit (fromInteger i))))
2425
2426 #if MIN_VERSION_base(4,15,0)
2427 $(namePat 'GHC.Num.Integer.integerComplement)
2428 #else
2429 "GHC.Integer.Type.complementInteger"
2430 #endif
2431 | [i] <- integerLiterals' args
2432 -> reduce (Literal (IntegerLiteral (complementInteger i)))
2433
2434 #if MIN_VERSION_base(4,15,0)
2435 $(namePat 'GHC.Num.Integer.integerOr)
2436 #else
2437 "GHC.Integer.Type.orInteger"
2438 #endif
2439 | [i, j] <- integerLiterals' args
2440 -> reduce (Literal (IntegerLiteral (orInteger i j)))
2441
2442 #if MIN_VERSION_base(4,15,0)
2443 $(namePat 'GHC.Num.Integer.integerXor)
2444 #else
2445 "GHC.Integer.Type.xorInteger"
2446 #endif
2447 | [i, j] <- integerLiterals' args
2448 -> reduce (Literal (IntegerLiteral (xorInteger i j)))
2449
2450 #if MIN_VERSION_base(4,15,0)
2451 $(namePat 'GHC.Num.Integer.integerAnd)
2452 #else
2453 "GHC.Integer.Type.andInteger"
2454 #endif
2455 | [i, j] <- integerLiterals' args
2456 -> reduce (Literal (IntegerLiteral (andInteger i j)))
2457
2458 #if !MIN_VERSION_base(4,15,0)
2459 "GHC.Integer.Type.doubleFromInteger"
2460 | [i] <- integerLiterals' args
2461 -> reduce (Literal (DoubleLiteral (doubleToWord (fromInteger i))))
2462 #endif
2463
2464 #if MIN_VERSION_base(4,17,0)
2465 "GHC.Num.Integer.$wintegerFromInt64#"
2466 | [i] <- int64Literals' args
2467 -> reduce . Literal $ IntLiteral i
2468 #endif
2469
2470 $(namePat 'GHC.Base.eqString)
2471 | [PrimVal _ _ [Lit (StringLiteral s1)]
2472 ,PrimVal _ _ [Lit (StringLiteral s2)]
2473 ] <- args
2474 -> reduce (boolToBoolLiteral tcm ty (s1 == s2))
2475 | otherwise -> error (show args)
2476
2477 $(namePat 'GHC.Base.quotInt)
2478 | [ DC intDc [Left (Literal (IntLiteral i))]
2479 , DC _ [Left (Literal (IntLiteral j))]
2480 ] <- args
2481 -> reduce (App (Data intDc) (Literal (IntLiteral (i `quot` j))))
2482
2483 $(namePat 'GHC.Base.remInt)
2484 | [ DC intDc [Left (Literal (IntLiteral i))]
2485 , DC _ [Left (Literal (IntLiteral j))]
2486 ] <- args
2487 -> reduce (App (Data intDc) (Literal (IntLiteral (i `rem` j))))
2488
2489 $(namePat 'GHC.Base.divInt)
2490 | [ DC intDc [Left (Literal (IntLiteral i))]
2491 , DC _ [Left (Literal (IntLiteral j))]
2492 ] <- args
2493 -> reduce (App (Data intDc) (Literal (IntLiteral (i `div` j))))
2494
2495
2496 $(namePat 'GHC.Base.modInt)
2497 | [ DC intDc [Left (Literal (IntLiteral i))]
2498 , DC _ [Left (Literal (IntLiteral j))]
2499 ] <- args
2500 -> reduce (App (Data intDc) (Literal (IntLiteral (i `mod` j))))
2501
2502 $(namePat 'Clash.Class.BitPack.Internal.packDouble#) -- :: Double -> BitVector 64
2503 | [DC _ [Left arg]] <- args
2504 , eval <- Evaluator ghcStep ghcUnwind ghcPrimStep ghcPrimUnwind
2505 , mach2@Machine{mStack=[],mTerm=Literal (DoubleLiteral i)} <- whnf eval tcm True (setTerm arg $ stackClear mach)
2506 -> let resTyInfo = extractTySizeInfo tcm ty tys
2507 in Just $ mach2
2508 { mStack = mStack mach
2509 , mTerm = mkBitVectorLit' resTyInfo 0 (toInteger $ (pack :: Word64 -> BitVector 64) i)
2510 }
2511
2512 $(namePat 'Clash.Class.BitPack.Internal.packFloat#) -- :: Float -> BitVector 32
2513 | [DC _ [Left arg]] <- args
2514 , eval <- Evaluator ghcStep ghcUnwind ghcPrimStep ghcPrimUnwind
2515 , mach2@Machine{mStack=[],mTerm=Literal (FloatLiteral i)} <- whnf eval tcm True (setTerm arg $ stackClear mach)
2516 -> let resTyInfo = extractTySizeInfo tcm ty tys
2517 in Just $ mach2
2518 { mStack = mStack mach
2519 , mTerm = mkBitVectorLit' resTyInfo 0 (toInteger $ (pack :: Word32 -> BitVector 32) i)
2520 }
2521
2522 $(namePat 'Clash.Class.BitPack.Internal.unpackFloat#)
2523 | [i] <- bitVectorLiterals' args
2524 -> let resTy = getResultTy tcm ty tys
2525 val = unpack (toBV i :: BitVector 32)
2526 in reduce (mkFloatCLit tcm val resTy)
2527
2528 $(namePat 'Clash.Class.BitPack.Internal.unpackDouble#)
2529 | [i] <- bitVectorLiterals' args
2530 -> let resTy = getResultTy tcm ty tys
2531 val = unpack (toBV i :: BitVector 64)
2532 in reduce (mkDoubleCLit tcm val resTy)
2533
2534 $(namePat 'Clash.Sized.Internal.BitVector.xToBV)
2535 | isSubj
2536 , Just (nTy, kn) <- extractKnownNat tcm tys
2537 -- The second argument to `xToBV` is always going to be suspended.
2538 -- See Note [Lazy primitives]
2539 , [ _, (Suspend arg) ] <- args
2540 , eval <- Evaluator ghcStep ghcUnwind ghcPrimStep ghcPrimUnwind
2541 , mach1@Machine{mStack=[],mTerm=argWHNF} <-
2542 whnf eval tcm True (setTerm arg (stackClear mach))
2543 , let undefBitVector =
2544 Just $ mach1
2545 { mStack = mStack mach
2546 , mTerm = mkBitVectorLit ty nTy kn (bit (fromInteger kn)-1) 0
2547 }
2548 -> case isX argWHNF of
2549 Left _ -> undefBitVector
2550 _ -> case collectArgs argWHNF of
2551 (Prim p,_) | primName p `elem` undefinedXPrims -> undefBitVector
2552 _ -> Just $ mach1
2553 { mStack = mStack mach
2554 , mTerm = argWHNF
2555 }
2556
2557 -- expIndex#
2558 -- :: KnownNat m
2559 -- => Index m
2560 -- -> SNat n
2561 -- -> Index (n^m)
2562 $(namePat 'Clash.Class.Exp.expIndex#)
2563 | [b] <- indexLiterals' args
2564 , [(_mTy, km), (_, e)] <- extractKnownNats tcm tys
2565 -> reduce (mkIndexLit ty (LitTy (NumTy (km^e))) (km^e) (b^e))
2566
2567 -- expSigned#
2568 -- :: KnownNat m
2569 -- => Signed m
2570 -- -> SNat n
2571 -- -> Signed (n*m)
2572 $(namePat 'Clash.Class.Exp.expSigned#)
2573 | [b] <- signedLiterals' args
2574 , [(_mTy, km), (_, e)] <- extractKnownNats tcm tys
2575 -> reduce (mkSignedLit ty (LitTy (NumTy (km*e))) (km*e) (b^e))
2576
2577 -- expUnsigned#
2578 -- :: KnownNat m
2579 -- => Unsigned m
2580 -- -> SNat n
2581 -- -> Unsigned m
2582 $(namePat 'Clash.Class.Exp.expUnsigned#)
2583 | [b] <- unsignedLiterals' args
2584 , [(_mTy, km), (_, e)] <- extractKnownNats tcm tys
2585 -> reduce (mkUnsignedLit ty (LitTy (NumTy (km*e))) (km*e) (b^e))
2586
2587 $(namePat 'Clash.Promoted.Nat.powSNat)
2588 | [Right a, Right b] <- map (runExcept . tyNatSize tcm) tys
2589 -> let c = case a of
2590 2 -> 1 `shiftL` (fromInteger b)
2591 _ -> a ^ b
2592 (_,tyView -> TyConApp snatTcNm _) = splitFunForallTy ty
2593 (Just snatTc) = UniqMap.lookup snatTcNm tcm
2594 [snatDc] = tyConDataCons snatTc
2595 in reduce $
2596 mkApps (Data snatDc) [ Right (LitTy (NumTy c))
2597 , Left (Literal (NaturalLiteral c))]
2598
2599 $(namePat 'Clash.Promoted.Nat.flogBaseSNat)
2600 | [Right a, Right b] <- map (runExcept . tyNatSize tcm) tys
2601 , Just c <- flogBase a b
2602 , let c' = toInteger c
2603 -> let (_,tyView -> TyConApp snatTcNm _) = splitFunForallTy ty
2604 (Just snatTc) = UniqMap.lookup snatTcNm tcm
2605 [snatDc] = tyConDataCons snatTc
2606 in reduce $
2607 mkApps (Data snatDc) [ Right (LitTy (NumTy c'))
2608 , Left (Literal (NaturalLiteral c'))]
2609
2610 $(namePat 'Clash.Promoted.Nat.clogBaseSNat)
2611 | [Right a, Right b] <- map (runExcept . tyNatSize tcm) tys
2612 , Just c <- clogBase a b
2613 , let c' = toInteger c
2614 -> let (_,tyView -> TyConApp snatTcNm _) = splitFunForallTy ty
2615 (Just snatTc) = UniqMap.lookup snatTcNm tcm
2616 [snatDc] = tyConDataCons snatTc
2617 in reduce $
2618 mkApps (Data snatDc) [ Right (LitTy (NumTy c'))
2619 , Left (Literal (NaturalLiteral c'))]
2620 | otherwise
2621 -> error ("clogBaseSNat: args = " <> show args <> ", tys = " <> show tys)
2622
2623 $(namePat 'Clash.Promoted.Nat.logBaseSNat)
2624 | [Right a, Right b] <- map (runExcept . tyNatSize tcm) tys
2625 , Just c <- flogBase a b
2626 , let c' = toInteger c
2627 -> let (_,tyView -> TyConApp snatTcNm _) = splitFunForallTy ty
2628 (Just snatTc) = UniqMap.lookup snatTcNm tcm
2629 [snatDc] = tyConDataCons snatTc
2630 in reduce $
2631 mkApps (Data snatDc) [ Right (LitTy (NumTy c'))
2632 , Left (Literal (NaturalLiteral c'))]
2633
2634 ------------
2635 -- BitVector
2636 ------------
2637 -- Constructor
2638 $(namePat 'Clash.Sized.Internal.BitVector.BV)
2639 | [Right _] <- map (runExcept . tyNatSize tcm) tys
2640 , Just (m,i) <- integerLiterals args
2641 -> let resTyInfo = extractTySizeInfo tcm ty tys
2642 in reduce (mkBitVectorLit' resTyInfo m i)
2643
2644 $(namePat 'Clash.Sized.Internal.BitVector.Bit)
2645 | Just (m,i) <- integerLiterals args
2646 -> reduce (mkBitLit ty m i)
2647
2648 -- Initialization
2649 $(namePat 'Clash.Sized.Internal.BitVector.size#)
2650 | Just (_, kn) <- extractKnownNat tcm tys
2651 -> let (_,tyView -> TyConApp intTcNm _) = splitFunForallTy ty
2652 (Just intTc) = UniqMap.lookup intTcNm tcm
2653 [intCon] = tyConDataCons intTc
2654 in reduce (mkApps (Data intCon) [Left (Literal (IntLiteral kn))])
2655 $(namePat 'Clash.Sized.Internal.BitVector.maxIndex#)
2656 | Just (_, kn) <- extractKnownNat tcm tys
2657 -> let (_,tyView -> TyConApp intTcNm _) = splitFunForallTy ty
2658 (Just intTc) = UniqMap.lookup intTcNm tcm
2659 [intCon] = tyConDataCons intTc
2660 in reduce (mkApps (Data intCon) [Left (Literal (IntLiteral (kn-1)))])
2661
2662 -- Construction
2663 $(namePat 'Clash.Sized.Internal.BitVector.high)
2664 -> reduce (mkBitLit ty 0 1)
2665 $(namePat 'Clash.Sized.Internal.BitVector.low)
2666 -> reduce (mkBitLit ty 0 0)
2667
2668 $(namePat 'Clash.Sized.Internal.BitVector.undefined#)
2669 | Just (_, kn) <- extractKnownNat tcm tys
2670 -> let resTyInfo = extractTySizeInfo tcm ty tys
2671 mask = bit (fromInteger kn) - 1
2672 in reduce (mkBitVectorLit' resTyInfo mask 0)
2673
2674 -- Eq
2675 $(namePat 'Clash.Sized.Internal.BitVector.eq##) | [(0,i),(0,j)] <- bitLiterals args
2676 -> reduce (boolToBoolLiteral tcm ty (i == j))
2677 $(namePat 'Clash.Sized.Internal.BitVector.neq##) | [(0,i),(0,j)] <- bitLiterals args
2678 -> reduce (boolToBoolLiteral tcm ty (i /= j))
2679
2680 -- Ord
2681 $(namePat 'Clash.Sized.Internal.BitVector.lt##) | [(0,i),(0,j)] <- bitLiterals args
2682 -> reduce (boolToBoolLiteral tcm ty (i < j))
2683 $(namePat 'Clash.Sized.Internal.BitVector.ge##) | [(0,i),(0,j)] <- bitLiterals args
2684 -> reduce (boolToBoolLiteral tcm ty (i >= j))
2685 $(namePat 'Clash.Sized.Internal.BitVector.gt##) | [(0,i),(0,j)] <- bitLiterals args
2686 -> reduce (boolToBoolLiteral tcm ty (i > j))
2687 $(namePat 'Clash.Sized.Internal.BitVector.le##) | [(0,i),(0,j)] <- bitLiterals args
2688 -> reduce (boolToBoolLiteral tcm ty (i <= j))
2689
2690 -- Enum
2691 $(namePat 'Clash.Sized.Internal.BitVector.toEnum##)
2692 | [i] <- intCLiterals' args
2693 -> let Bit msk val = BitVector.toEnum## (fromInteger i)
2694 in reduce (mkBitLit ty (toInteger msk) (toInteger val))
2695
2696 -- Bits
2697 $(namePat 'Clash.Sized.Internal.BitVector.and##)
2698 | [i,j] <- bitLiterals args
2699 -> let Bit msk val = BitVector.and## (toBit i) (toBit j)
2700 in reduce (mkBitLit ty (toInteger msk) (toInteger val))
2701 $(namePat 'Clash.Sized.Internal.BitVector.or##)
2702 | [i,j] <- bitLiterals args
2703 -> let Bit msk val = BitVector.or## (toBit i) (toBit j)
2704 in reduce (mkBitLit ty (toInteger msk) (toInteger val))
2705 $(namePat 'Clash.Sized.Internal.BitVector.xor##)
2706 | [i,j] <- bitLiterals args
2707 -> let Bit msk val = BitVector.xor## (toBit i) (toBit j)
2708 in reduce (mkBitLit ty (toInteger msk) (toInteger val))
2709
2710 $(namePat 'Clash.Sized.Internal.BitVector.complement##)
2711 | [i] <- bitLiterals args
2712 -> let Bit msk val = BitVector.complement## (toBit i)
2713 in reduce (mkBitLit ty (toInteger msk) (toInteger val))
2714
2715 -- Pack
2716 $(namePat 'Clash.Sized.Internal.BitVector.pack#)
2717 | [(msk,i)] <- bitLiterals args
2718 -> let resTyInfo = extractTySizeInfo tcm ty tys
2719 in reduce (mkBitVectorLit' resTyInfo msk i)
2720
2721 $(namePat 'Clash.Sized.Internal.BitVector.unpack#)
2722 | [(msk,i)] <- bitVectorLiterals' args
2723 -> reduce (mkBitLit ty msk i)
2724
2725 -- Concatenation
2726 $(namePat '(Clash.Sized.Internal.BitVector.++#)) -- :: KnownNat m => BitVector n -> BitVector m -> BitVector (n + m)
2727 | Just (_,m) <- extractKnownNat tcm tys
2728 , [(mski,i),(mskj,j)] <- bitVectorLiterals' args
2729 -> let val = i `shiftL` fromInteger m .|. j
2730 msk = mski `shiftL` fromInteger m .|. mskj
2731 resTyInfo = extractTySizeInfo tcm ty tys
2732 in reduce (mkBitVectorLit' resTyInfo msk val)
2733
2734 -- Reduction
2735 $(namePat 'Clash.Sized.Internal.BitVector.reduceAnd#) -- :: KnownNat n => BitVector n -> Bit
2736 | [i] <- bitVectorLiterals' args
2737 , Just (_, kn) <- extractKnownNat tcm tys
2738 -> let resTy = getResultTy tcm ty tys
2739 val = reifyNat kn (op (toBV i))
2740 in reduce (mkBitLit resTy 0 val)
2741 where
2742 op :: KnownNat n => BitVector n -> Proxy n -> Integer
2743 op u _ = toInteger (BitVector.reduceAnd# u)
2744 $(namePat 'Clash.Sized.Internal.BitVector.reduceOr#) -- :: KnownNat n => BitVector n -> Bit
2745 | [i] <- bitVectorLiterals' args
2746 , Just (_, kn) <- extractKnownNat tcm tys
2747 -> let resTy = getResultTy tcm ty tys
2748 val = reifyNat kn (op (toBV i))
2749 in reduce (mkBitLit resTy 0 val)
2750 where
2751 op :: KnownNat n => BitVector n -> Proxy n -> Integer
2752 op u _ = toInteger (BitVector.reduceOr# u)
2753 $(namePat 'Clash.Sized.Internal.BitVector.reduceXor#) -- :: KnownNat n => BitVector n -> Bit
2754 | [i] <- bitVectorLiterals' args
2755 , Just (_, kn) <- extractKnownNat tcm tys
2756 -> let resTy = getResultTy tcm ty tys
2757 val = reifyNat kn (op (toBV i))
2758 in reduce (mkBitLit resTy 0 val)
2759 where
2760 op :: KnownNat n => BitVector n -> Proxy n -> Integer
2761 op u _ = toInteger (BitVector.reduceXor# u)
2762
2763
2764 -- Indexing
2765 $(namePat 'Clash.Sized.Internal.BitVector.index#) -- :: KnownNat n => BitVector n -> Int -> Bit
2766 | Just (_,kn,i,j) <- bitVectorLitIntLit tcm tys args
2767 -> let resTy = getResultTy tcm ty tys
2768 (msk,val) = reifyNat kn (op (toBV i) (fromInteger j))
2769 in reduce (mkBitLit resTy msk val)
2770 where
2771 op :: KnownNat n => BitVector n -> Int -> Proxy n -> (Integer,Integer)
2772 op u i _ = (toInteger m, toInteger v)
2773 where Bit m v = (BitVector.index# u i)
2774 $(namePat 'Clash.Sized.Internal.BitVector.replaceBit#) -- :: :: KnownNat n => BitVector n -> Int -> Bit -> BitVector n
2775 | Just (_, n) <- extractKnownNat tcm tys
2776 , [ _
2777 , PrimVal bvP _ [_, Lit (NaturalLiteral mskBv), Lit (IntegerLiteral bv)]
2778 , valArgs -> Just [Literal (IntLiteral i)]
2779 , PrimVal bP _ [Lit (WordLiteral mskB), Lit (IntegerLiteral b)]
2780 ] <- args
2781 , primName bvP == showt 'Clash.Sized.Internal.BitVector.fromInteger#
2782 , primName bP == showt 'Clash.Sized.Internal.BitVector.fromInteger##
2783 -> let resTyInfo = extractTySizeInfo tcm ty tys
2784 (mskVal,val) = reifyNat n (op (BV (fromInteger mskBv) (fromInteger bv))
2785 (fromInteger i)
2786 (Bit (fromInteger mskB) (fromInteger b)))
2787 in reduce (mkBitVectorLit' resTyInfo mskVal val)
2788 where
2789 op :: KnownNat n => BitVector n -> Int -> Bit -> Proxy n -> (Integer,Integer)
2790 -- op bv i b _ = (BitVector.unsafeMask res, BitVector.unsafeToInteger res)
2791 op bv i b _ = splitBV (BitVector.replaceBit# bv i b)
2792 $(namePat 'Clash.Sized.Internal.BitVector.setSlice#)
2793 -- :: SNat (m+1+i) -> BitVector (m + 1 + i) -> SNat m -> SNat n -> BitVector (m + 1 - n) -> BitVector (m + 1 + i)
2794 | mTy : iTy : nTy : _ <- tys
2795 , Right m <- runExcept (tyNatSize tcm mTy)
2796 , Right iN <- runExcept (tyNatSize tcm iTy)
2797 , Right n <- runExcept (tyNatSize tcm nTy)
2798 , [i,j] <- bitVectorLiterals' args
2799 -> let BV msk val = BitVector.setSlice# (unsafeSNat (m+1+iN)) (toBV i) (unsafeSNat m) (unsafeSNat n) (toBV j)
2800 resTyInfo = extractTySizeInfo tcm ty tys
2801 in reduce (mkBitVectorLit' resTyInfo (toInteger msk) (toInteger val))
2802 $(namePat 'Clash.Sized.Internal.BitVector.slice#)
2803 -- :: BitVector (m + 1 + i) -> SNat m -> SNat n -> BitVector (m + 1 - n)
2804 | mTy : _ : nTy : _ <- tys
2805 , Right m <- runExcept (tyNatSize tcm mTy)
2806 , Right n <- runExcept (tyNatSize tcm nTy)
2807 , [i] <- bitVectorLiterals' args
2808 -> let BV msk val = BitVector.slice# (toBV i) (unsafeSNat m) (unsafeSNat n)
2809 resTyInfo = extractTySizeInfo tcm ty tys
2810 in reduce (mkBitVectorLit' resTyInfo (toInteger msk) (toInteger val))
2811 $(namePat 'Clash.Sized.Internal.BitVector.split#) -- :: forall n m. KnownNat n => BitVector (m + n) -> (BitVector m, BitVector n)
2812 | nTy : mTy : _ <- tys
2813 , Right n <- runExcept (tyNatSize tcm nTy)
2814 , Right m <- runExcept (tyNatSize tcm mTy)
2815 , [(mski,i)] <- bitVectorLiterals' args
2816 -> let ty' = piResultTys tcm ty tys
2817 (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty'
2818 (Just tupTc) = UniqMap.lookup tupTcNm tcm
2819 [tupDc] = tyConDataCons tupTc
2820 bvTy : _ = tyArgs
2821 valM = i `shiftR` fromInteger n
2822 mskM = mski `shiftR` fromInteger n
2823 valN = i .&. mask
2824 mskN = mski .&. mask
2825 mask = bit (fromInteger n) - 1
2826 in reduce $
2827 mkApps (Data tupDc) (map Right tyArgs ++
2828 [ Left (mkBitVectorLit bvTy mTy m mskM valM)
2829 , Left (mkBitVectorLit bvTy nTy n mskN valN)])
2830
2831 $(namePat 'Clash.Sized.Internal.BitVector.msb#) -- :: forall n. KnownNat n => BitVector n -> Bit
2832 | [i] <- bitVectorLiterals' args
2833 , Just (_, kn) <- extractKnownNat tcm tys
2834 -> let resTy = getResultTy tcm ty tys
2835 (msk,val) = reifyNat kn (op (toBV i))
2836 in reduce (mkBitLit resTy (toInteger msk) (toInteger val))
2837 where
2838 op :: KnownNat n => BitVector n -> Proxy n -> (Word,Word)
2839 op u _ = (unsafeMask# res, BitVector.unsafeToInteger# res)
2840 where
2841 res = BitVector.msb# u
2842 $(namePat 'Clash.Sized.Internal.BitVector.lsb#) -- :: BitVector n -> Bit
2843 | [i] <- bitVectorLiterals' args
2844 -> let resTy = getResultTy tcm ty tys
2845 Bit msk val = BitVector.lsb# (toBV i)
2846 in reduce (mkBitLit resTy (toInteger msk) (toInteger val))
2847
2848
2849 -- Eq
2850 -- eq#, neq# :: KnownNat n => BitVector n -> BitVector n -> Bool
2851 $(namePat 'Clash.Sized.Internal.BitVector.eq#)
2852 | nTy : _ <- tys
2853 , Right 0 <- runExcept (tyNatSize tcm nTy)
2854 -> reduce (boolToBoolLiteral tcm ty True)
2855 | Just (_, kn) <- extractKnownNat tcm tys
2856 , Just val <- reifyNat kn (liftBitVector2Bool BitVector.eq# ty tcm args)
2857 -> reduce val
2858
2859 $(namePat 'Clash.Sized.Internal.BitVector.neq#)
2860 | nTy : _ <- tys
2861 , Right 0 <- runExcept (tyNatSize tcm nTy)
2862 -> reduce (boolToBoolLiteral tcm ty False)
2863 | Just (_, kn) <- extractKnownNat tcm tys
2864 , Just val <- reifyNat kn (liftBitVector2Bool BitVector.neq# ty tcm args)
2865 -> reduce val
2866
2867 -- Ord
2868 -- lt#,ge#,gt#,le# :: KnownNat n => BitVector n -> BitVector n -> Bool
2869 $(namePat 'Clash.Sized.Internal.BitVector.lt#)
2870 | nTy : _ <- tys
2871 , Right 0 <- runExcept (tyNatSize tcm nTy)
2872 -> reduce (boolToBoolLiteral tcm ty False)
2873 | Just (_, kn) <- extractKnownNat tcm tys
2874 , Just val <- reifyNat kn (liftBitVector2Bool BitVector.lt# ty tcm args)
2875 -> reduce val
2876 $(namePat 'Clash.Sized.Internal.BitVector.ge#)
2877 | nTy : _ <- tys
2878 , Right 0 <- runExcept (tyNatSize tcm nTy)
2879 -> reduce (boolToBoolLiteral tcm ty True)
2880 | Just (_, kn) <- extractKnownNat tcm tys
2881 , Just val <- reifyNat kn (liftBitVector2Bool BitVector.ge# ty tcm args)
2882 -> reduce val
2883 $(namePat 'Clash.Sized.Internal.BitVector.gt#)
2884 | nTy : _ <- tys
2885 , Right 0 <- runExcept (tyNatSize tcm nTy)
2886 -> reduce (boolToBoolLiteral tcm ty False)
2887 | Just (_, kn) <- extractKnownNat tcm tys
2888 , Just val <- reifyNat kn (liftBitVector2Bool BitVector.gt# ty tcm args)
2889 -> reduce val
2890 $(namePat 'Clash.Sized.Internal.BitVector.le#)
2891 | nTy : _ <- tys
2892 , Right 0 <- runExcept (tyNatSize tcm nTy)
2893 -> reduce (boolToBoolLiteral tcm ty True)
2894 | Just (_, kn) <- extractKnownNat tcm tys
2895 , Just val <- reifyNat kn (liftBitVector2Bool BitVector.le# ty tcm args)
2896 -> reduce val
2897
2898 -- Enum
2899
2900 $(namePat 'Clash.Sized.Internal.BitVector.toEnum#)
2901 | let resTyInfo@(_,_,kn) = extractTySizeInfo tcm ty tys
2902 , Just val <- reifyNat kn (liftInteger2BitVector (BitVector.toEnum# . fromInteger) resTyInfo args)
2903 -> reduce val
2904
2905 $(namePat 'Clash.Sized.Internal.BitVector.fromEnum#)
2906 | Just (_, kn) <- extractKnownNat tcm tys
2907 , let resTy = getResultTy tcm ty tys
2908 , Just val <- reifyNat kn (liftBitVector2CInt tcm resTy (toInteger . BitVector.fromEnum#) args)
2909 -> reduce val
2910
2911 -- Bounded
2912 $(namePat 'Clash.Sized.Internal.BitVector.minBound#)
2913 | Just (nTy,len) <- extractKnownNat tcm tys
2914 -> reduce (mkBitVectorLit ty nTy len 0 0)
2915 $(namePat 'Clash.Sized.Internal.BitVector.maxBound#)
2916 | Just (litTy,mb) <- extractKnownNat tcm tys
2917 -> let maxB = (2 ^ mb) - 1
2918 in reduce (mkBitVectorLit ty litTy mb 0 maxB)
2919
2920 -- Num
2921 $(namePat '(Clash.Sized.Internal.BitVector.+#))
2922 | Just (_, kn) <- extractKnownNat tcm tys
2923 , Just val <- reifyNat kn (liftBitVector2 (BitVector.+#) ty tcm tys args)
2924 -> reduce val
2925 $(namePat '(Clash.Sized.Internal.BitVector.-#))
2926 | Just (_, kn) <- extractKnownNat tcm tys
2927 , Just val <- reifyNat kn (liftBitVector2 (BitVector.-#) ty tcm tys args)
2928 -> reduce val
2929 $(namePat '(Clash.Sized.Internal.BitVector.*#))
2930 | Just (_, kn) <- extractKnownNat tcm tys
2931 , Just val <- reifyNat kn (liftBitVector2 (BitVector.*#) ty tcm tys args)
2932 -> reduce val
2933 $(namePat 'Clash.Sized.Internal.BitVector.negate#)
2934 | Just (nTy, kn) <- extractKnownNat tcm tys
2935 , [i] <- bitVectorLiterals' args
2936 -> let (msk,val) = reifyNat kn (op (toBV i))
2937 in reduce (mkBitVectorLit ty nTy kn msk val)
2938 where
2939 op :: KnownNat n => BitVector n -> Proxy n -> (Integer,Integer)
2940 op u _ = splitBV (BitVector.negate# u)
2941
2942 -- ExtendingNum
2943 $(namePat 'Clash.Sized.Internal.BitVector.plus#) -- :: (KnownNat n, KnownNat m) => BitVector m -> BitVector n -> BitVector (Max m n + 1)
2944 | [(0,i),(0,j)] <- bitVectorLiterals' args
2945 -> let ty' = piResultTys tcm ty tys
2946 (_,resTy) = splitFunForallTy ty'
2947 (TyConApp _ [resSizeTy]) = tyView resTy
2948 Right resSizeInt = runExcept (tyNatSize tcm resSizeTy)
2949 in reduce (mkBitVectorLit resTy resSizeTy resSizeInt 0 (i+j))
2950
2951 $(namePat 'Clash.Sized.Internal.BitVector.minus#)
2952 | [(0,i),(0,j)] <- bitVectorLiterals' args
2953 -> let ty' = piResultTys tcm ty tys
2954 (_,resTy) = splitFunForallTy ty'
2955 (TyConApp _ [resSizeTy]) = tyView resTy
2956 Right resSizeInt = runExcept (tyNatSize tcm resSizeTy)
2957 val = reifyNat resSizeInt (runSizedF (BitVector.-#) i j)
2958 in reduce (mkBitVectorLit resTy resSizeTy resSizeInt 0 val)
2959
2960 $(namePat 'Clash.Sized.Internal.BitVector.times#)
2961 | [(0,i),(0,j)] <- bitVectorLiterals' args
2962 -> let ty' = piResultTys tcm ty tys
2963 (_,resTy) = splitFunForallTy ty'
2964 (TyConApp _ [resSizeTy]) = tyView resTy
2965 Right resSizeInt = runExcept (tyNatSize tcm resSizeTy)
2966 in reduce (mkBitVectorLit resTy resSizeTy resSizeInt 0 (i*j))
2967
2968 -- Integral
2969 $(namePat 'Clash.Sized.Internal.BitVector.quot#)
2970 | Just (_, kn) <- extractKnownNat tcm tys
2971 , Just val <- reifyNat kn (liftBitVector2 (BitVector.quot#) ty tcm tys args)
2972 -> reduce $ catchDivByZero val
2973 $(namePat 'Clash.Sized.Internal.BitVector.rem#)
2974 | Just (_, kn) <- extractKnownNat tcm tys
2975 , Just val <- reifyNat kn (liftBitVector2 (BitVector.rem#) ty tcm tys args)
2976 -> reduce $ catchDivByZero val
2977 $(namePat 'Clash.Sized.Internal.BitVector.toInteger#)
2978 | Just (_, kn) <- extractKnownNat tcm tys
2979 , [i] <- bitVectorLiterals' args
2980 -> let val = reifyNat kn (op (toBV i))
2981 in reduce (integerToIntegerLiteral val)
2982 where
2983 op :: KnownNat n => BitVector n -> Proxy n -> Integer
2984 op u _ = BitVector.toInteger# u
2985
2986 -- Bits
2987 $(namePat 'Clash.Sized.Internal.BitVector.and#)
2988 | Just (_, kn) <- extractKnownNat tcm tys
2989 , Just val <- reifyNat kn (liftBitVector2 (BitVector.and#) ty tcm tys args)
2990 -> reduce val
2991 $(namePat 'Clash.Sized.Internal.BitVector.or#)
2992 | Just (_, kn) <- extractKnownNat tcm tys
2993 , Just val <- reifyNat kn (liftBitVector2 (BitVector.or#) ty tcm tys args)
2994 -> reduce val
2995 $(namePat 'Clash.Sized.Internal.BitVector.xor#)
2996 | Just (_, kn) <- extractKnownNat tcm tys
2997 , Just val <- reifyNat kn (liftBitVector2 (BitVector.xor#) ty tcm tys args)
2998 -> reduce val
2999
3000 $(namePat 'Clash.Sized.Internal.BitVector.complement#)
3001 | [i] <- bitVectorLiterals' args
3002 , Just (nTy, kn) <- extractKnownNat tcm tys
3003 -> let (msk,val) = reifyNat kn (op (toBV i))
3004 in reduce (mkBitVectorLit ty nTy kn msk val)
3005 where
3006 op :: KnownNat n => BitVector n -> Proxy n -> (Integer,Integer)
3007 op u _ = splitBV $ BitVector.complement# u
3008
3009 $(namePat 'Clash.Sized.Internal.BitVector.shiftL#)
3010 | Just (nTy,kn,i,j) <- bitVectorLitIntLit tcm tys args
3011 -> let (msk,val) = reifyNat kn (op (toBV i) (fromInteger j))
3012 in reduce (mkBitVectorLit ty nTy kn msk val)
3013 where
3014 op :: KnownNat n => BitVector n -> Int -> Proxy n -> (Integer,Integer)
3015 op u i _ = splitBV (BitVector.shiftL# u i)
3016 $(namePat 'Clash.Sized.Internal.BitVector.shiftR#)
3017 | Just (nTy,kn,i,j) <- bitVectorLitIntLit tcm tys args
3018 -> let (msk,val) = reifyNat kn (op (toBV i) (fromInteger j))
3019 in reduce (mkBitVectorLit ty nTy kn msk val)
3020 where
3021 op :: KnownNat n => BitVector n -> Int -> Proxy n -> (Integer,Integer)
3022 op u i _ = splitBV (BitVector.shiftR# u i)
3023 $(namePat 'Clash.Sized.Internal.BitVector.rotateL#)
3024 | Just (nTy,kn,i,j) <- bitVectorLitIntLit tcm tys args
3025 -> let (msk,val) = reifyNat kn (op (toBV i) (fromInteger j))
3026 in reduce (mkBitVectorLit ty nTy kn msk val)
3027 where
3028 op :: KnownNat n => BitVector n -> Int -> Proxy n -> (Integer,Integer)
3029 op u i _ = splitBV (BitVector.rotateL# u i)
3030 $(namePat 'Clash.Sized.Internal.BitVector.rotateR#)
3031 | Just (nTy,kn,i,j) <- bitVectorLitIntLit tcm tys args
3032 -> let (msk,val) = reifyNat kn (op (toBV i) (fromInteger j))
3033 in reduce (mkBitVectorLit ty nTy kn msk val)
3034 where
3035 op :: KnownNat n => BitVector n -> Int -> Proxy n -> (Integer,Integer)
3036 op u i _ = splitBV (BitVector.rotateR# u i)
3037
3038 -- truncateB
3039 $(namePat 'Clash.Sized.Internal.BitVector.truncateB#) -- forall a b . KnownNat a => BitVector (a + b) -> BitVector a
3040 | aTy : _ <- tys
3041 , Right ka <- runExcept (tyNatSize tcm aTy)
3042 , [(mski,i)] <- bitVectorLiterals' args
3043 -> let bitsKeep = (bit (fromInteger ka)) - 1
3044 val = i .&. bitsKeep
3045 msk = mski .&. bitsKeep
3046 in reduce (mkBitVectorLit ty aTy ka msk val)
3047
3048 --------
3049 -- Index
3050 --------
3051 -- BitPack
3052 $(namePat 'Clash.Sized.Internal.Index.pack#)
3053 | nTy : _ <- tys
3054 , Right _ <- runExcept (tyNatSize tcm nTy)
3055 , [i] <- indexLiterals' args
3056 -> let resTyInfo = extractTySizeInfo tcm ty tys
3057 in reduce (mkBitVectorLit' resTyInfo 0 i)
3058 $(namePat 'Clash.Sized.Internal.Index.unpack#)
3059 | Just (nTy,kn) <- extractKnownNat tcm tys
3060 , [(0,i)] <- bitVectorLiterals' args
3061 -> reduce (mkIndexLit ty nTy kn i)
3062
3063 -- Eq
3064 $(namePat 'Clash.Sized.Internal.Index.eq#) | Just (i,j) <- indexLiterals args
3065 -> reduce (boolToBoolLiteral tcm ty (i == j))
3066 $(namePat 'Clash.Sized.Internal.Index.neq#) | Just (i,j) <- indexLiterals args
3067 -> reduce (boolToBoolLiteral tcm ty (i /= j))
3068
3069 -- Ord
3070 $(namePat 'Clash.Sized.Internal.Index.lt#)
3071 | Just (i,j) <- indexLiterals args
3072 -> reduce (boolToBoolLiteral tcm ty (i < j))
3073 $(namePat 'Clash.Sized.Internal.Index.ge#)
3074 | Just (i,j) <- indexLiterals args
3075 -> reduce (boolToBoolLiteral tcm ty (i >= j))
3076 $(namePat 'Clash.Sized.Internal.Index.gt#)
3077 | Just (i,j) <- indexLiterals args
3078 -> reduce (boolToBoolLiteral tcm ty (i > j))
3079 $(namePat 'Clash.Sized.Internal.Index.le#)
3080 | Just (i,j) <- indexLiterals args
3081 -> reduce (boolToBoolLiteral tcm ty (i <= j))
3082
3083 -- Enum
3084 $(namePat 'Clash.Sized.Internal.Index.toEnum#)
3085 | [i] <- intCLiterals' args
3086 , Just (nTy, mb) <- extractKnownNat tcm tys
3087 -> reduce (mkIndexLit ty nTy mb i)
3088
3089 $(namePat 'Clash.Sized.Internal.Index.fromEnum#)
3090 | [i] <- indexLiterals' args
3091 -> let resTy = getResultTy tcm ty tys
3092 in reduce (mkIntCLit tcm i resTy)
3093
3094 -- Bounded
3095 $(namePat 'Clash.Sized.Internal.Index.maxBound#)
3096 | Just (nTy,mb) <- extractKnownNat tcm tys
3097 -> reduce (mkIndexLit ty nTy mb (mb - 1))
3098
3099 -- Num
3100 $(namePat '(Clash.Sized.Internal.Index.+#))
3101 | Just (nTy,kn) <- extractKnownNat tcm tys
3102 , [i,j] <- indexLiterals' args
3103 -> reduce (mkIndexLit ty nTy kn (i + j))
3104 $(namePat '(Clash.Sized.Internal.Index.-#))
3105 | Just (nTy,kn) <- extractKnownNat tcm tys
3106 , [i,j] <- indexLiterals' args
3107 -> reduce (mkIndexLit ty nTy kn (i - j))
3108 $(namePat '(Clash.Sized.Internal.Index.*#))
3109 | Just (nTy,kn) <- extractKnownNat tcm tys
3110 , [i,j] <- indexLiterals' args
3111 -> reduce (mkIndexLit ty nTy kn (i * j))
3112
3113 -- ExtendingNum
3114 $(namePat 'Clash.Sized.Internal.Index.plus#)
3115 | mTy : nTy : _ <- tys
3116 , Right _ <- runExcept (tyNatSize tcm mTy)
3117 , Right _ <- runExcept (tyNatSize tcm nTy)
3118 , Just (i,j) <- indexLiterals args
3119 -> let resTyInfo = extractTySizeInfo tcm ty tys
3120 in reduce (mkIndexLit' resTyInfo (i + j))
3121 $(namePat 'Clash.Sized.Internal.Index.minus#)
3122 | mTy : nTy : _ <- tys
3123 , Right _ <- runExcept (tyNatSize tcm mTy)
3124 , Right _ <- runExcept (tyNatSize tcm nTy)
3125 , Just (i,j) <- indexLiterals args
3126 -> let resTyInfo = extractTySizeInfo tcm ty tys
3127 in reduce (mkIndexLit' resTyInfo (i - j))
3128 $(namePat 'Clash.Sized.Internal.Index.times#)
3129 | mTy : nTy : _ <- tys
3130 , Right _ <- runExcept (tyNatSize tcm mTy)
3131 , Right _ <- runExcept (tyNatSize tcm nTy)
3132 , Just (i,j) <- indexLiterals args
3133 -> let resTyInfo = extractTySizeInfo tcm ty tys
3134 in reduce (mkIndexLit' resTyInfo (i * j))
3135
3136 -- Integral
3137 $(namePat 'Clash.Sized.Internal.Index.quot#)
3138 | Just (nTy,kn) <- extractKnownNat tcm tys
3139 , Just (i,j) <- indexLiterals args
3140 -> reduce $ catchDivByZero (mkIndexLit ty nTy kn (i `quot` j))
3141 $(namePat 'Clash.Sized.Internal.Index.rem#)
3142 | Just (nTy,kn) <- extractKnownNat tcm tys
3143 , Just (i,j) <- indexLiterals args
3144 -> reduce $ catchDivByZero (mkIndexLit ty nTy kn (i `rem` j))
3145 $(namePat 'Clash.Sized.Internal.Index.toInteger#)
3146 | [PrimVal p _ [_, Lit (IntegerLiteral i)]] <- args
3147 , primName p == showt 'Clash.Sized.Internal.Index.fromInteger#
3148 -> reduce (integerToIntegerLiteral i)
3149
3150 -- Resize
3151 $(namePat 'Clash.Sized.Internal.Index.resize#)
3152 | Just (mTy,m) <- extractKnownNat tcm tys
3153 , [i] <- indexLiterals' args
3154 -> reduce (mkIndexLit ty mTy m i)
3155
3156 ---------
3157 -- Signed
3158 ---------
3159 $(namePat 'Clash.Sized.Internal.Signed.size#)
3160 | Just (_, kn) <- extractKnownNat tcm tys
3161 -> let (_,tyView -> TyConApp intTcNm _) = splitFunForallTy ty
3162 (Just intTc) = UniqMap.lookup intTcNm tcm
3163 [intCon] = tyConDataCons intTc
3164 in reduce (mkApps (Data intCon) [Left (Literal (IntLiteral kn))])
3165
3166 -- BitPack
3167 $(namePat 'Clash.Sized.Internal.Signed.pack#)
3168 | Just (nTy, kn) <- extractKnownNat tcm tys
3169 , [i] <- signedLiterals' args
3170 -> let val = reifyNat kn (op (fromInteger i))
3171 in reduce (mkBitVectorLit ty nTy kn 0 val)
3172 where
3173 op :: KnownNat n => Signed n -> Proxy n -> Integer
3174 op s _ = toInteger (Signed.pack# s)
3175 $(namePat 'Clash.Sized.Internal.Signed.unpack#)
3176 | Just (nTy, kn) <- extractKnownNat tcm tys
3177 , [(0,i)] <- bitVectorLiterals' args
3178 -> let val = reifyNat kn (op (fromInteger i))
3179 in reduce (mkSignedLit ty nTy kn val)
3180 where
3181 op :: KnownNat n => BitVector n -> Proxy n -> Integer
3182 op s _ = toInteger (Signed.unpack# s)
3183
3184 -- Eq
3185 $(namePat 'Clash.Sized.Internal.Signed.eq#) | Just (i,j) <- signedLiterals args
3186 -> reduce (boolToBoolLiteral tcm ty (i == j))
3187 $(namePat 'Clash.Sized.Internal.Signed.neq#) | Just (i,j) <- signedLiterals args
3188 -> reduce (boolToBoolLiteral tcm ty (i /= j))
3189
3190 -- Ord
3191 $(namePat 'Clash.Sized.Internal.Signed.lt#) | Just (i,j) <- signedLiterals args
3192 -> reduce (boolToBoolLiteral tcm ty (i < j))
3193 $(namePat 'Clash.Sized.Internal.Signed.ge#) | Just (i,j) <- signedLiterals args
3194 -> reduce (boolToBoolLiteral tcm ty (i >= j))
3195 $(namePat 'Clash.Sized.Internal.Signed.gt#) | Just (i,j) <- signedLiterals args
3196 -> reduce (boolToBoolLiteral tcm ty (i > j))
3197 $(namePat 'Clash.Sized.Internal.Signed.le#) | Just (i,j) <- signedLiterals args
3198 -> reduce (boolToBoolLiteral tcm ty (i <= j))
3199
3200 -- Enum
3201 $(namePat 'Clash.Sized.Internal.Signed.toEnum#)
3202 | [i] <- intCLiterals' args
3203 , Just (litTy, mb) <- extractKnownNat tcm tys
3204 -> reduce (mkSignedLit ty litTy mb i)
3205
3206 $(namePat 'Clash.Sized.Internal.Signed.fromEnum#)
3207 | [i] <- signedLiterals' args
3208 -> let resTy = getResultTy tcm ty tys
3209 in reduce (mkIntCLit tcm i resTy)
3210
3211 -- Bounded
3212 $(namePat 'Clash.Sized.Internal.Signed.minBound#)
3213 | Just (litTy,mb) <- extractKnownNat tcm tys
3214 -> let minB = negate (2 ^ (mb - 1))
3215 in reduce (mkSignedLit ty litTy mb minB)
3216 $(namePat 'Clash.Sized.Internal.Signed.maxBound#)
3217 | Just (litTy,mb) <- extractKnownNat tcm tys
3218 -> let maxB = (2 ^ (mb - 1)) - 1
3219 in reduce (mkSignedLit ty litTy mb maxB)
3220
3221 -- Num
3222 $(namePat '(Clash.Sized.Internal.Signed.+#))
3223 | Just (_, kn) <- extractKnownNat tcm tys
3224 , Just val <- reifyNat kn (liftSigned2 (Signed.+#) ty tcm tys args)
3225 -> reduce (val)
3226 $(namePat '(Clash.Sized.Internal.Signed.-#))
3227 | Just (_, kn) <- extractKnownNat tcm tys
3228 , Just val <- reifyNat kn (liftSigned2 (Signed.-#) ty tcm tys args)
3229 -> reduce (val)
3230 $(namePat '(Clash.Sized.Internal.Signed.*#))
3231 | Just (_, kn) <- extractKnownNat tcm tys
3232 , Just val <- reifyNat kn (liftSigned2 (Signed.*#) ty tcm tys args)
3233 -> reduce (val)
3234 $(namePat 'Clash.Sized.Internal.Signed.negate#)
3235 | Just (nTy, kn) <- extractKnownNat tcm tys
3236 , [i] <- signedLiterals' args
3237 -> let val = reifyNat kn (op (fromInteger i))
3238 in reduce (mkSignedLit ty nTy kn val)
3239 where
3240 op :: KnownNat n => Signed n -> Proxy n -> Integer
3241 op s _ = toInteger (Signed.negate# s)
3242 $(namePat 'Clash.Sized.Internal.Signed.abs#)
3243 | Just (nTy, kn) <- extractKnownNat tcm tys
3244 , [i] <- signedLiterals' args
3245 -> let val = reifyNat kn (op (fromInteger i))
3246 in reduce (mkSignedLit ty nTy kn val)
3247 where
3248 op :: KnownNat n => Signed n -> Proxy n -> Integer
3249 op s _ = toInteger (Signed.abs# s)
3250
3251 -- ExtendingNum
3252 $(namePat 'Clash.Sized.Internal.Signed.plus#)
3253 | Just (i,j) <- signedLiterals args
3254 -> let ty' = piResultTys tcm ty tys
3255 (_,resTy) = splitFunForallTy ty'
3256 (TyConApp _ [resSizeTy]) = tyView resTy
3257 Right resSizeInt = runExcept (tyNatSize tcm resSizeTy)
3258 in reduce (mkSignedLit resTy resSizeTy resSizeInt (i+j))
3259
3260 $(namePat 'Clash.Sized.Internal.Signed.minus#)
3261 | Just (i,j) <- signedLiterals args
3262 -> let ty' = piResultTys tcm ty tys
3263 (_,resTy) = splitFunForallTy ty'
3264 (TyConApp _ [resSizeTy]) = tyView resTy
3265 Right resSizeInt = runExcept (tyNatSize tcm resSizeTy)
3266 in reduce (mkSignedLit resTy resSizeTy resSizeInt (i-j))
3267
3268 $(namePat 'Clash.Sized.Internal.Signed.times#)
3269 | Just (i,j) <- signedLiterals args
3270 -> let ty' = piResultTys tcm ty tys
3271 (_,resTy) = splitFunForallTy ty'
3272 (TyConApp _ [resSizeTy]) = tyView resTy
3273 Right resSizeInt = runExcept (tyNatSize tcm resSizeTy)
3274 in reduce (mkSignedLit resTy resSizeTy resSizeInt (i*j))
3275
3276 -- Integral
3277 $(namePat 'Clash.Sized.Internal.Signed.quot#)
3278 | Just (_, kn) <- extractKnownNat tcm tys
3279 , Just val <- reifyNat kn (liftSigned2 (Signed.quot#) ty tcm tys args)
3280 -> reduce $ catchDivByZero val
3281 $(namePat 'Clash.Sized.Internal.Signed.rem#)
3282 | Just (_, kn) <- extractKnownNat tcm tys
3283 , Just val <- reifyNat kn (liftSigned2 (Signed.rem#) ty tcm tys args)
3284 -> reduce $ catchDivByZero val
3285 $(namePat 'Clash.Sized.Internal.Signed.div#)
3286 | Just (_, kn) <- extractKnownNat tcm tys
3287 , Just val <- reifyNat kn (liftSigned2 (Signed.div#) ty tcm tys args)
3288 -> reduce $ catchDivByZero val
3289 $(namePat 'Clash.Sized.Internal.Signed.mod#)
3290 | Just (_, kn) <- extractKnownNat tcm tys
3291 , Just val <- reifyNat kn (liftSigned2 (Signed.mod#) ty tcm tys args)
3292 -> reduce $ catchDivByZero val
3293 $(namePat 'Clash.Sized.Internal.Signed.toInteger#)
3294 | [PrimVal p _ [_, Lit (IntegerLiteral i)]] <- args
3295 , primName p == showt 'Clash.Sized.Internal.Signed.fromInteger#
3296 -> reduce (integerToIntegerLiteral i)
3297
3298 -- Bits
3299 $(namePat 'Clash.Sized.Internal.Signed.and#)
3300 | [i,j] <- signedLiterals' args
3301 , Just (nTy, kn) <- extractKnownNat tcm tys
3302 -> reduce (mkSignedLit ty nTy kn (i .&. j))
3303 $(namePat 'Clash.Sized.Internal.Signed.or#)
3304 | [i,j] <- signedLiterals' args
3305 , Just (nTy, kn) <- extractKnownNat tcm tys
3306 -> reduce (mkSignedLit ty nTy kn (i .|. j))
3307 $(namePat 'Clash.Sized.Internal.Signed.xor#)
3308 | [i,j] <- signedLiterals' args
3309 , Just (nTy, kn) <- extractKnownNat tcm tys
3310 -> reduce (mkSignedLit ty nTy kn (i `xor` j))
3311
3312 $(namePat 'Clash.Sized.Internal.Signed.complement#)
3313 | [i] <- signedLiterals' args
3314 , Just (nTy, kn) <- extractKnownNat tcm tys
3315 -> let val = reifyNat kn (op (fromInteger i))
3316 in reduce (mkSignedLit ty nTy kn val)
3317 where
3318 op :: KnownNat n => Signed n -> Proxy n -> Integer
3319 op u _ = toInteger (Signed.complement# u)
3320
3321 $(namePat 'Clash.Sized.Internal.Signed.shiftL#)
3322 | Just (nTy,kn,i,j) <- signedLitIntLit tcm tys args
3323 -> let val = reifyNat kn (op (fromInteger i) (fromInteger j))
3324 in reduce (mkSignedLit ty nTy kn val)
3325 where
3326 op :: KnownNat n => Signed n -> Int -> Proxy n -> Integer
3327 op u i _ = toInteger (Signed.shiftL# u i)
3328 $(namePat 'Clash.Sized.Internal.Signed.shiftR#)
3329 | Just (nTy,kn,i,j) <- signedLitIntLit tcm tys args
3330 -> let val = reifyNat kn (op (fromInteger i) (fromInteger j))
3331 in reduce (mkSignedLit ty nTy kn val)
3332 where
3333 op :: KnownNat n => Signed n -> Int -> Proxy n -> Integer
3334 op u i _ = toInteger (Signed.shiftR# u i)
3335 $(namePat 'Clash.Sized.Internal.Signed.rotateL#)
3336 | Just (nTy,kn,i,j) <- signedLitIntLit tcm tys args
3337 -> let val = reifyNat kn (op (fromInteger i) (fromInteger j))
3338 in reduce (mkSignedLit ty nTy kn val)
3339 where
3340 op :: KnownNat n => Signed n -> Int -> Proxy n -> Integer
3341 op u i _ = toInteger (Signed.rotateL# u i)
3342 $(namePat 'Clash.Sized.Internal.Signed.rotateR#)
3343 | Just (nTy,kn,i,j) <- signedLitIntLit tcm tys args
3344 -> let val = reifyNat kn (op (fromInteger i) (fromInteger j))
3345 in reduce (mkSignedLit ty nTy kn val)
3346 where
3347 op :: KnownNat n => Signed n -> Int -> Proxy n -> Integer
3348 op u i _ = toInteger (Signed.rotateR# u i)
3349
3350 -- Resize
3351 $(namePat 'Clash.Sized.Internal.Signed.resize#) -- forall m n. (KnownNat n, KnownNat m) => Signed n -> Signed m
3352 | mTy : nTy : _ <- tys
3353 , Right mInt <- runExcept (tyNatSize tcm mTy)
3354 , Right nInt <- runExcept (tyNatSize tcm nTy)
3355 , [i] <- signedLiterals' args
3356 -> let val | nInt <= mInt = extended
3357 | otherwise = truncated
3358 extended = i
3359 mask = 1 `shiftL` fromInteger (mInt - 1)
3360 i' = i `mod` mask
3361 truncated = if testBit i (fromInteger nInt - 1)
3362 then (i' - mask)
3363 else i'
3364 in reduce (mkSignedLit ty mTy mInt val)
3365 $(namePat 'Clash.Sized.Internal.Signed.truncateB#) -- KnownNat m => Signed (m + n) -> Signed m
3366 | Just (mTy, km) <- extractKnownNat tcm tys
3367 , [i] <- signedLiterals' args
3368 -> let bitsKeep = (bit (fromInteger km)) - 1
3369 val = i .&. bitsKeep
3370 in reduce (mkSignedLit ty mTy km val)
3371
3372 -- SaturatingNum
3373 -- No need to manually evaluate Clash.Sized.Internal.Signed.minBoundSym#
3374 -- It is just implemented in terms of other primitives.
3375
3376
3377 -----------
3378 -- Unsigned
3379 -----------
3380 $(namePat 'Clash.Sized.Internal.Unsigned.size#)
3381 | Just (_, kn) <- extractKnownNat tcm tys
3382 -> let (_,ty') = splitFunForallTy ty
3383 (TyConApp intTcNm _) = tyView ty'
3384 (Just intTc) = UniqMap.lookup intTcNm tcm
3385 [intCon] = tyConDataCons intTc
3386 in reduce (mkApps (Data intCon) [Left (Literal (IntLiteral kn))])
3387
3388 -- BitPack
3389 $(namePat 'Clash.Sized.Internal.Unsigned.pack#)
3390 | Just (nTy, kn) <- extractKnownNat tcm tys
3391 , [i] <- unsignedLiterals' args
3392 -> reduce (mkBitVectorLit ty nTy kn 0 i)
3393 $(namePat 'Clash.Sized.Internal.Unsigned.unpack#)
3394 | Just (nTy, kn) <- extractKnownNat tcm tys
3395 , [i] <- bitVectorLiterals' args
3396 -> let val = reifyNat kn (op (toBV i))
3397 in reduce (mkUnsignedLit ty nTy kn val)
3398 where
3399 op :: KnownNat n => BitVector n -> Proxy n -> Integer
3400 op u _ = toInteger (Unsigned.unpack# u)
3401
3402 -- Eq
3403 $(namePat 'Clash.Sized.Internal.Unsigned.eq#) | Just (i,j) <- unsignedLiterals args
3404 -> reduce (boolToBoolLiteral tcm ty (i == j))
3405 $(namePat 'Clash.Sized.Internal.Unsigned.neq#) | Just (i,j) <- unsignedLiterals args
3406 -> reduce (boolToBoolLiteral tcm ty (i /= j))
3407
3408 -- Ord
3409 $(namePat 'Clash.Sized.Internal.Unsigned.lt#) | Just (i,j) <- unsignedLiterals args
3410 -> reduce (boolToBoolLiteral tcm ty (i < j))
3411 $(namePat 'Clash.Sized.Internal.Unsigned.ge#) | Just (i,j) <- unsignedLiterals args
3412 -> reduce (boolToBoolLiteral tcm ty (i >= j))
3413 $(namePat 'Clash.Sized.Internal.Unsigned.gt#) | Just (i,j) <- unsignedLiterals args
3414 -> reduce (boolToBoolLiteral tcm ty (i > j))
3415 $(namePat 'Clash.Sized.Internal.Unsigned.le#) | Just (i,j) <- unsignedLiterals args
3416 -> reduce (boolToBoolLiteral tcm ty (i <= j))
3417
3418 -- Enum
3419 $(namePat 'Clash.Sized.Internal.Unsigned.toEnum#)
3420 | [i] <- intCLiterals' args
3421 , Just (litTy, mb) <- extractKnownNat tcm tys
3422 -> reduce (mkUnsignedLit ty litTy mb i)
3423
3424 $(namePat 'Clash.Sized.Internal.Unsigned.fromEnum#)
3425 | [i] <- unsignedLiterals' args
3426 -> let resTy = getResultTy tcm ty tys
3427 in reduce (mkIntCLit tcm i resTy)
3428
3429 -- Bounded
3430 $(namePat 'Clash.Sized.Internal.Unsigned.minBound#)
3431 | Just (nTy,len) <- extractKnownNat tcm tys
3432 -> reduce (mkUnsignedLit ty nTy len 0)
3433 $(namePat 'Clash.Sized.Internal.Unsigned.maxBound#)
3434 | Just (litTy,mb) <- extractKnownNat tcm tys
3435 -> let maxB = (2 ^ mb) - 1
3436 in reduce (mkUnsignedLit ty litTy mb maxB)
3437
3438 -- Num
3439 $(namePat '(Clash.Sized.Internal.Unsigned.+#))
3440 | Just (_, kn) <- extractKnownNat tcm tys
3441 , Just val <- reifyNat kn (liftUnsigned2 (Unsigned.+#) ty tcm tys args)
3442 -> reduce val
3443 $(namePat '(Clash.Sized.Internal.Unsigned.-#))
3444 | Just (_, kn) <- extractKnownNat tcm tys
3445 , Just val <- reifyNat kn (liftUnsigned2 (Unsigned.-#) ty tcm tys args)
3446 -> reduce val
3447 $(namePat '(Clash.Sized.Internal.Unsigned.*#))
3448 | Just (_, kn) <- extractKnownNat tcm tys
3449 , Just val <- reifyNat kn (liftUnsigned2 (Unsigned.*#) ty tcm tys args)
3450 -> reduce val
3451 $(namePat 'Clash.Sized.Internal.Unsigned.negate#)
3452 | Just (nTy, kn) <- extractKnownNat tcm tys
3453 , [i] <- unsignedLiterals' args
3454 -> let val = reifyNat kn (op (fromInteger i))
3455 in reduce (mkUnsignedLit ty nTy kn val)
3456 where
3457 op :: KnownNat n => Unsigned n -> Proxy n -> Integer
3458 op u _ = toInteger (Unsigned.negate# u)
3459
3460 -- ExtendingNum
3461 $(namePat 'Clash.Sized.Internal.Unsigned.plus#) -- :: Unsigned m -> Unsigned n -> Unsigned (Max m n + 1)
3462 | Just (i,j) <- unsignedLiterals args
3463 -> let ty' = piResultTys tcm ty tys
3464 (_,resTy) = splitFunForallTy ty'
3465 (TyConApp _ [resSizeTy]) = tyView resTy
3466 Right resSizeInt = runExcept (tyNatSize tcm resSizeTy)
3467 in reduce (mkUnsignedLit resTy resSizeTy resSizeInt (i+j))
3468
3469 $(namePat 'Clash.Sized.Internal.Unsigned.minus#)
3470 | [i,j] <- unsignedLiterals' args
3471 -> let ty' = piResultTys tcm ty tys
3472 (_,resTy) = splitFunForallTy ty'
3473 (TyConApp _ [resSizeTy]) = tyView resTy
3474 Right resSizeInt = runExcept (tyNatSize tcm resSizeTy)
3475 val = reifyNat resSizeInt (runSizedF (Unsigned.-#) i j)
3476 in reduce (mkUnsignedLit resTy resSizeTy resSizeInt val)
3477
3478 $(namePat 'Clash.Sized.Internal.Unsigned.times#)
3479 | Just (i,j) <- unsignedLiterals args
3480 -> let ty' = piResultTys tcm ty tys
3481 (_,resTy) = splitFunForallTy ty'
3482 (TyConApp _ [resSizeTy]) = tyView resTy
3483 Right resSizeInt = runExcept (tyNatSize tcm resSizeTy)
3484 in reduce (mkUnsignedLit resTy resSizeTy resSizeInt (i*j))
3485
3486 -- Integral
3487 $(namePat 'Clash.Sized.Internal.Unsigned.quot#)
3488 | Just (_, kn) <- extractKnownNat tcm tys
3489 , Just val <- reifyNat kn (liftUnsigned2 (Unsigned.quot#) ty tcm tys args)
3490 -> reduce $ catchDivByZero val
3491 $(namePat 'Clash.Sized.Internal.Unsigned.rem#)
3492 | Just (_, kn) <- extractKnownNat tcm tys
3493 , Just val <- reifyNat kn (liftUnsigned2 (Unsigned.rem#) ty tcm tys args)
3494 -> reduce $ catchDivByZero val
3495 $(namePat 'Clash.Sized.Internal.Unsigned.toInteger#)
3496 | [PrimVal p _ [_, Lit (IntegerLiteral i)]] <- args
3497 , primName p == showt 'Clash.Sized.Internal.Unsigned.fromInteger#
3498 -> reduce (integerToIntegerLiteral i)
3499
3500 -- Bits
3501 $(namePat 'Clash.Sized.Internal.Unsigned.and#)
3502 | Just (i,j) <- unsignedLiterals args
3503 , Just (nTy, kn) <- extractKnownNat tcm tys
3504 -> reduce (mkUnsignedLit ty nTy kn (i .&. j))
3505 $(namePat 'Clash.Sized.Internal.Unsigned.or#)
3506 | Just (i,j) <- unsignedLiterals args
3507 , Just (nTy, kn) <- extractKnownNat tcm tys
3508 -> reduce (mkUnsignedLit ty nTy kn (i .|. j))
3509 $(namePat 'Clash.Sized.Internal.Unsigned.xor#)
3510 | Just (i,j) <- unsignedLiterals args
3511 , Just (nTy, kn) <- extractKnownNat tcm tys
3512 -> reduce (mkUnsignedLit ty nTy kn (i `xor` j))
3513
3514 $(namePat 'Clash.Sized.Internal.Unsigned.complement#)
3515 | [i] <- unsignedLiterals' args
3516 , Just (nTy, kn) <- extractKnownNat tcm tys
3517 -> let val = reifyNat kn (op (fromInteger i))
3518 in reduce (mkUnsignedLit ty nTy kn val)
3519 where
3520 op :: KnownNat n => Unsigned n -> Proxy n -> Integer
3521 op u _ = toInteger (Unsigned.complement# u)
3522
3523 $(namePat 'Clash.Sized.Internal.Unsigned.shiftL#) -- :: forall n. KnownNat n => Unsigned n -> Int -> Unsigned n
3524 | Just (nTy,kn,i,j) <- unsignedLitIntLit tcm tys args
3525 -> let val = reifyNat kn (op (fromInteger i) (fromInteger j))
3526 in reduce (mkUnsignedLit ty nTy kn val)
3527 where
3528 op :: KnownNat n => Unsigned n -> Int -> Proxy n -> Integer
3529 op u i _ = toInteger (Unsigned.shiftL# u i)
3530 $(namePat 'Clash.Sized.Internal.Unsigned.shiftR#) -- :: forall n. KnownNat n => Unsigned n -> Int -> Unsigned n
3531 | Just (nTy,kn,i,j) <- unsignedLitIntLit tcm tys args
3532 -> let val = reifyNat kn (op (fromInteger i) (fromInteger j))
3533 in reduce (mkUnsignedLit ty nTy kn val)
3534 where
3535 op :: KnownNat n => Unsigned n -> Int -> Proxy n -> Integer
3536 op u i _ = toInteger (Unsigned.shiftR# u i)
3537 $(namePat 'Clash.Sized.Internal.Unsigned.rotateL#) -- :: forall n. KnownNat n => Unsigned n -> Int -> Unsigned n
3538 | Just (nTy,kn,i,j) <- unsignedLitIntLit tcm tys args
3539 -> let val = reifyNat kn (op (fromInteger i) (fromInteger j))
3540 in reduce (mkUnsignedLit ty nTy kn val)
3541 where
3542 op :: KnownNat n => Unsigned n -> Int -> Proxy n -> Integer
3543 op u i _ = toInteger (Unsigned.rotateL# u i)
3544 $(namePat 'Clash.Sized.Internal.Unsigned.rotateR#) -- :: forall n. KnownNat n => Unsigned n -> Int -> Unsigned n
3545 | Just (nTy,kn,i,j) <- unsignedLitIntLit tcm tys args
3546 -> let val = reifyNat kn (op (fromInteger i) (fromInteger j))
3547 in reduce (mkUnsignedLit ty nTy kn val)
3548 where
3549 op :: KnownNat n => Unsigned n -> Int -> Proxy n -> Integer
3550 op u i _ = toInteger (Unsigned.rotateR# u i)
3551
3552 -- Resize
3553 $(namePat 'Clash.Sized.Internal.Unsigned.resize#) -- forall n m . KnownNat m => Unsigned n -> Unsigned m
3554 | _ : mTy : _ <- tys
3555 , Right km <- runExcept (tyNatSize tcm mTy)
3556 , [i] <- unsignedLiterals' args
3557 -> let bitsKeep = (bit (fromInteger km)) - 1
3558 val = i .&. bitsKeep
3559 in reduce (mkUnsignedLit ty mTy km val)
3560
3561 -- Conversions
3562 $(namePat 'Clash.Sized.Internal.Unsigned.unsignedToWord)
3563 | isSubj
3564 , [a] <- unsignedLiterals' args
3565 -> let b = Unsigned.unsignedToWord (U (fromInteger a))
3566 (_,tyView -> TyConApp wordTcNm []) = splitFunForallTy ty
3567 (Just wordTc) = UniqMap.lookup wordTcNm tcm
3568 [wordDc] = tyConDataCons wordTc
3569 in reduce (mkApps (Data wordDc) [Left (Literal (WordLiteral (toInteger b)))])
3570
3571 $(namePat 'Clash.Sized.Internal.Unsigned.unsigned8toWord8)
3572 | isSubj
3573 , [a] <- unsignedLiterals' args
3574 -> let b = Unsigned.unsigned8toWord8 (U (fromInteger a))
3575 (_,tyView -> TyConApp wordTcNm []) = splitFunForallTy ty
3576 (Just wordTc) = UniqMap.lookup wordTcNm tcm
3577 [wordDc] = tyConDataCons wordTc
3578 in reduce (mkApps (Data wordDc) [Left (Literal (WordLiteral (toInteger b)))])
3579
3580 $(namePat 'Clash.Sized.Internal.Unsigned.unsigned16toWord16)
3581 | isSubj
3582 , [a] <- unsignedLiterals' args
3583 -> let b = Unsigned.unsigned16toWord16 (U (fromInteger a))
3584 (_,tyView -> TyConApp wordTcNm []) = splitFunForallTy ty
3585 (Just wordTc) = UniqMap.lookup wordTcNm tcm
3586 [wordDc] = tyConDataCons wordTc
3587 in reduce (mkApps (Data wordDc) [Left (Literal (WordLiteral (toInteger b)))])
3588
3589 $(namePat 'Clash.Sized.Internal.Unsigned.unsigned32toWord32)
3590 | isSubj
3591 , [a] <- unsignedLiterals' args
3592 -> let b = Unsigned.unsigned32toWord32 (U (fromInteger a))
3593 (_,tyView -> TyConApp wordTcNm []) = splitFunForallTy ty
3594 (Just wordTc) = UniqMap.lookup wordTcNm tcm
3595 [wordDc] = tyConDataCons wordTc
3596 in reduce (mkApps (Data wordDc) [Left (Literal (WordLiteral (toInteger b)))])
3597
3598 $(namePat 'Clash.Annotations.BitRepresentation.Deriving.dontApplyInHDL)
3599 | isSubj
3600 , f : a : _ <- args
3601 -> reduceWHNF (mkApps (valToTerm f) [Left (valToTerm a)])
3602
3603 --------
3604 -- RTree
3605 --------
3606 $(namePat 'Clash.Sized.RTree.textract)
3607 | isSubj
3608 , [DC _ tArgs] <- args
3609 -> reduceWHNF (Either.lefts tArgs !! 1)
3610
3611 $(namePat 'Clash.Sized.RTree.tsplit)
3612 | isSubj
3613 , dTy : aTy : _ <- tys
3614 , [DC _ tArgs] <- args
3615 , (tyArgs,tyView -> TyConApp tupTcNm _) <- splitFunForallTy ty
3616 , TyConApp treeTcNm _ <- tyView (Either.rights tyArgs !! 0)
3617 -> let (Just tupTc) = UniqMap.lookup tupTcNm tcm
3618 [tupDc] = tyConDataCons tupTc
3619 in reduce $
3620 mkApps (Data tupDc)
3621 [Right (mkTyConApp treeTcNm [dTy,aTy])
3622 ,Right (mkTyConApp treeTcNm [dTy,aTy])
3623 ,Left (Either.lefts tArgs !! 1)
3624 ,Left (Either.lefts tArgs !! 2)
3625 ]
3626
3627 $(namePat 'Clash.Sized.RTree.tdfold)
3628 | isSubj
3629 , pTy : kTy : aTy : _ <- tys
3630 , _ : p : f : g : ts : _ <- args
3631 , DC _ tArgs <- ts
3632 , Right k' <- runExcept (tyNatSize tcm kTy)
3633 -> case k' of
3634 0 -> reduceWHNF (mkApps (valToTerm f) [Left (Either.lefts tArgs !! 1)])
3635 _ -> let k'ty = LitTy (NumTy (k'-1))
3636 (tyArgs,_) = splitFunForallTy ty
3637 (tyArgs',_) = splitFunForallTy (Either.rights tyArgs !! 3)
3638 TyConApp snatTcNm _ = tyView (Either.rights tyArgs' !! 0)
3639 Just snatTc = UniqMap.lookup snatTcNm tcm
3640 [snatDc] = tyConDataCons snatTc
3641 in reduceWHNF $
3642 mkApps (valToTerm g)
3643 [Right k'ty
3644 ,Left (mkApps (Data snatDc)
3645 [Right k'ty
3646 ,Left (Literal (NaturalLiteral (k'-1)))])
3647 ,Left (mkApps (Prim pInfo)
3648 [Right pTy
3649 ,Right k'ty
3650 ,Right aTy
3651 ,Left (Literal (NaturalLiteral (k'-1)))
3652 ,Left (valToTerm p)
3653 ,Left (valToTerm f)
3654 ,Left (valToTerm g)
3655 ,Left (Either.lefts tArgs !! 1)
3656 ])
3657 ,Left (mkApps (Prim pInfo)
3658 [Right pTy
3659 ,Right k'ty
3660 ,Right aTy
3661 ,Left (Literal (NaturalLiteral (k'-1)))
3662 ,Left (valToTerm p)
3663 ,Left (valToTerm f)
3664 ,Left (valToTerm g)
3665 ,Left (Either.lefts tArgs !! 2)
3666 ])
3667 ]
3668
3669 $(namePat 'Clash.Sized.RTree.treplicate)
3670 | isSubj
3671 , let ty' = piResultTys tcm ty tys
3672 , (_,tyView -> TyConApp treeTcNm [lenTy,argTy]) <- splitFunForallTy ty'
3673 , Right len <- runExcept (tyNatSize tcm lenTy)
3674 -> let (Just treeTc) = UniqMap.lookup treeTcNm tcm
3675 [lrCon,brCon] = tyConDataCons treeTc
3676 in reduce (mkRTree lrCon brCon argTy len (replicate (2^len) (valToTerm (last args))))
3677
3678 ---------
3679 -- Vector
3680 ---------
3681 $(namePat 'Clash.Sized.Vector.length) -- :: KnownNat n => Vec n a -> Int
3682 | isSubj
3683 , [nTy, _] <- tys
3684 , Right n <-runExcept (tyNatSize tcm nTy)
3685 -> let (_, tyView -> TyConApp intTcNm _) = splitFunForallTy ty
3686 (Just intTc) = UniqMap.lookup intTcNm tcm
3687 [intCon] = tyConDataCons intTc
3688 in reduce (mkApps (Data intCon) [Left (Literal (IntLiteral (toInteger n)))])
3689
3690 -- XXX: Not a thing anymore?
3691 "Clash.Sized.Vector.maxIndex"
3692 | isSubj
3693 , [nTy, _] <- tys
3694 , Right n <- runExcept (tyNatSize tcm nTy)
3695 -> let (_, tyView -> TyConApp intTcNm _) = splitFunForallTy ty
3696 (Just intTc) = UniqMap.lookup intTcNm tcm
3697 [intCon] = tyConDataCons intTc
3698 in reduce (mkApps (Data intCon) [Left (Literal (IntLiteral (toInteger (n - 1))))])
3699
3700 -- Indexing
3701 -- XXX: Not exported
3702 "Clash.Sized.Vector.index_int" -- :: KnownNat n => Vec n a -> Int
3703 | nTy : aTy : _ <- tys
3704 , _ : xs : i : _ <- args
3705 , DC intDc [Left (Literal (IntLiteral i'))] <- i
3706 -> if i' < 0
3707 then Nothing
3708 else case xs of
3709 DC _ vArgs -> case runExcept (tyNatSize tcm nTy) of
3710 Right 0 -> Nothing
3711 Right n' ->
3712 if i' == 0
3713 then reduceWHNF (Either.lefts vArgs !! 1)
3714 else reduceWHNF $
3715 mkApps (Prim pInfo)
3716 [Right (LitTy (NumTy (n'-1)))
3717 ,Right aTy
3718 ,Left (Literal (NaturalLiteral (n'-1)))
3719 ,Left (Either.lefts vArgs !! 2)
3720 ,Left (mkApps (Data intDc)
3721 [Left (Literal (IntLiteral (i'-1)))])
3722 ]
3723 _ -> Nothing
3724 _ -> Nothing
3725 $(namePat 'Clash.Sized.Vector.head) -- :: Vec (n+1) a -> a
3726 | isSubj
3727 , [DC _ vArgs] <- args
3728 -> reduceWHNF (Either.lefts vArgs !! 1)
3729 $(namePat 'Clash.Sized.Vector.last) -- :: Vec (n+1) a -> a
3730 | isSubj
3731 , [DC _ vArgs] <- args
3732 , (Right _ : Right aTy : Right nTy : _) <- vArgs
3733 , Right n <- runExcept (tyNatSize tcm nTy)
3734 -> if n == 0
3735 then reduceWHNF (Either.lefts vArgs !! 1)
3736 else reduceWHNF
3737 (mkApps (Prim pInfo)
3738 [Right (LitTy (NumTy (n-1)))
3739 ,Right aTy
3740 ,Left (Either.lefts vArgs !! 2)
3741 ])
3742 -- - Sub-vectors
3743 $(namePat 'Clash.Sized.Vector.tail) -- :: Vec (n+1) a -> Vec n a
3744 | isSubj
3745 , [DC _ vArgs] <- args
3746 -> reduceWHNF (Either.lefts vArgs !! 2)
3747 $(namePat 'Clash.Sized.Vector.init) -- :: Vec (n+1) a -> Vec n a
3748 | isSubj
3749 , [DC consCon vArgs] <- args
3750 , (Right _ : Right aTy : Right nTy : _) <- vArgs
3751 , Right n <- runExcept (tyNatSize tcm nTy)
3752 -> if n == 0
3753 then reduceWHNF (Either.lefts vArgs !! 2)
3754 else reduce $
3755 mkVecCons consCon aTy n
3756 (Either.lefts vArgs !! 1)
3757 (mkApps (Prim pInfo)
3758 [Right (LitTy (NumTy (n-1)))
3759 ,Right aTy
3760 ,Left (Either.lefts vArgs !! 2)])
3761 $(namePat 'Clash.Sized.Vector.select) -- :: (CmpNat (i+s) (s*n) ~ GT) => SNat f -> SNat s -> SNat n -> Vec (f + i) a -> Vec n a
3762 | isSubj
3763 , iTy : sTy : nTy : fTy : aTy : _ <- tys
3764 , eq : f : s : n : xs : _ <- args
3765 , Right n' <- runExcept (tyNatSize tcm nTy)
3766 , Right f' <- runExcept (tyNatSize tcm fTy)
3767 , Right i' <- runExcept (tyNatSize tcm iTy)
3768 , Right s' <- runExcept (tyNatSize tcm sTy)
3769 , DC _ vArgs <- xs
3770 -> case n' of
3771 0 -> reduce (mkVecNil nilCon aTy)
3772 _ -> case f' of
3773 0 -> let splitAtCall =
3774 mkApps (splitAtPrim snatTcNm vecTcNm)
3775 [Right sTy
3776 ,Right (LitTy (NumTy (i'-s')))
3777 ,Right aTy
3778 ,Left (valToTerm s)
3779 ,Left (valToTerm xs)
3780 ]
3781 fVecTy = mkTyConApp vecTcNm [sTy,aTy]
3782 iVecTy = mkTyConApp vecTcNm [LitTy (NumTy (i'-s')),aTy]
3783 -- Guaranteed no capture, so okay to use unsafe name generation
3784 fNm = mkUnsafeSystemName "fxs" 0
3785 iNm = mkUnsafeSystemName "ixs" 1
3786 fId = mkLocalId fVecTy fNm
3787 iId = mkLocalId iVecTy iNm
3788 tupPat = DataPat tupDc [] [fId,iId]
3789 iAlt = (tupPat, (Var iId))
3790 in reduce $
3791 mkVecCons consCon aTy n' (Either.lefts vArgs !! 1) $
3792 mkApps (Prim pInfo)
3793 [Right (LitTy (NumTy (i'-s')))
3794 ,Right sTy
3795 ,Right (LitTy (NumTy (n'-1)))
3796 ,Right (LitTy (NumTy 0))
3797 ,Right aTy
3798 ,Left (valToTerm eq)
3799 ,Left (Literal (NaturalLiteral 0))
3800 ,Left (valToTerm s)
3801 ,Left (Literal (NaturalLiteral (n'-1)))
3802 ,Left (Case splitAtCall iVecTy [iAlt])
3803 ]
3804 _ -> let splitAtCall =
3805 mkApps (splitAtPrim snatTcNm vecTcNm)
3806 [Right fTy
3807 ,Right iTy
3808 ,Right aTy
3809 ,Left (valToTerm f)
3810 ,Left (valToTerm xs)
3811 ]
3812 fVecTy = mkTyConApp vecTcNm [fTy,aTy]
3813 iVecTy = mkTyConApp vecTcNm [iTy,aTy]
3814 -- Guaranteed no capture, so okay to use unsafe name generation
3815 fNm = mkUnsafeSystemName "fxs" 0
3816 iNm = mkUnsafeSystemName "ixs" 1
3817 fId = mkLocalId fVecTy fNm
3818 iId = mkLocalId iVecTy iNm
3819 tupPat = DataPat tupDc [] [fId,iId]
3820 iAlt = (tupPat, (Var iId))
3821 in reduceWHNF $
3822 mkApps (Prim pInfo)
3823 [Right iTy
3824 ,Right sTy
3825 ,Right nTy
3826 ,Right (LitTy (NumTy 0))
3827 ,Right aTy
3828 ,Left (valToTerm eq)
3829 ,Left (Literal (NaturalLiteral 0))
3830 ,Left (valToTerm s)
3831 ,Left (valToTerm n)
3832 ,Left (Case splitAtCall iVecTy [iAlt])
3833 ]
3834 where
3835 (tyArgs,tyView -> TyConApp vecTcNm _) = splitFunForallTy ty
3836 Just vecTc = UniqMap.lookup vecTcNm tcm
3837 [nilCon,consCon] = tyConDataCons vecTc
3838 TyConApp snatTcNm _ = tyView (Either.rights tyArgs !! 1)
3839 tupTcNm = ghcTyconToTyConName (tupleTyCon Boxed 2)
3840 (Just tupTc) = UniqMap.lookup tupTcNm tcm
3841 [tupDc] = tyConDataCons tupTc
3842 -- - Splitting
3843 $(namePat 'Clash.Sized.Vector.splitAt) -- :: SNat m -> Vec (m + n) a -> (Vec m a, Vec n a)
3844 | isSubj
3845 , (DC snatDc (Right mTy:_)):_ <- args
3846 , Right m <- runExcept (tyNatSize tcm mTy)
3847 -> let _:nTy:aTy:_ = tys
3848 -- Get the tuple data-constructor
3849 ty1 = piResultTys tcm ty tys
3850 (_,tyView -> TyConApp tupTcNm tyArgs@(tyArg:_)) = splitFunForallTy ty1
3851 (Just tupTc) = UniqMap.lookup tupTcNm tcm
3852 [tupDc] = tyConDataCons tupTc
3853 -- Get the vector data-constructors
3854 TyConApp vecTcNm _ = tyView tyArg
3855 Just vecTc = UniqMap.lookup vecTcNm tcm
3856 [nilCon,consCon] = tyConDataCons vecTc
3857 -- Recursive call to @splitAt@
3858 splitAtRec v =
3859 mkApps (Prim pInfo)
3860 [Right (LitTy (NumTy (m-1)))
3861 ,Right nTy
3862 ,Right aTy
3863 ,Left (mkApps (Data snatDc)
3864 [ Right (LitTy (NumTy (m-1)))
3865 , Left (Literal (NaturalLiteral (m-1)))])
3866 ,Left v
3867 ]
3868 -- Projection either the first or second field of the recursive
3869 -- call to @splitAt@
3870 splitAtSelR v = Case (splitAtRec v)
3871 m1VecTy = mkTyConApp vecTcNm [LitTy (NumTy (m-1)),aTy]
3872 nVecTy = mkTyConApp vecTcNm [nTy,aTy]
3873 -- Guaranteed no capture, so okay to use unsafe name generation
3874 lNm = mkUnsafeSystemName "l" 0
3875 rNm = mkUnsafeSystemName "r" 1
3876 lId = mkLocalId m1VecTy lNm
3877 rId = mkLocalId nVecTy rNm
3878 tupPat = DataPat tupDc [] [lId,rId]
3879 lAlt = (tupPat, (Var lId))
3880 rAlt = (tupPat, (Var rId))
3881
3882 in case m of
3883 -- (Nil,v)
3884 0 -> reduce $
3885 mkApps (Data tupDc) $ (map Right tyArgs) ++
3886 [ Left (mkVecNil nilCon aTy)
3887 , Left (valToTerm (last args))
3888 ]
3889 -- (x:xs) <- v
3890 m' | DC _ vArgs <- last args
3891 -- (x:fst (splitAt (m-1) xs),snd (splitAt (m-1) xs))
3892 -> case Either.lefts vArgs of
3893 (_ : x : xs : _) ->
3894 reduce $
3895 mkApps (Data tupDc) $ (map Right tyArgs) ++
3896 [ Left (mkVecCons consCon aTy m' x
3897 (splitAtSelR xs m1VecTy [lAlt]))
3898 , Left (splitAtSelR xs nVecTy [rAlt])
3899 ]
3900 _ ->
3901 -- v actually reduces to Nil and not Cons, this only happens
3902 -- when 'n' would reduce to a negative number; the complement
3903 -- of 'm'.
3904 --
3905 -- See Clash issue: https://github.com/clash-lang/clash-compiler/issues/2831
3906 let resTy = getResultTy tcm ty tys
3907 in reduce (TyApp (Prim NP.undefined) resTy)
3908
3909 -- v doesn't reduce to a data-constructor
3910 _ -> Nothing
3911
3912 $(namePat 'Clash.Sized.Vector.unconcat) -- :: KnownNat n => SNamt m -> Vec (n * m) a -> Vec n (Vec m a)
3913 | isSubj
3914 , kn : snat : v : _ <- args
3915 , nTy : mTy : aTy :_ <- tys
3916 , Lit (NaturalLiteral n) <- kn
3917 -> let ( Either.rights -> argTys, tyView -> TyConApp vecTcNm _) =
3918 splitFunForallTy ty
3919 Just vecTc = UniqMap.lookup vecTcNm tcm
3920 [nilCon,consCon] = tyConDataCons vecTc
3921 tupTcNm = ghcTyconToTyConName (tupleTyCon Boxed 2)
3922 (Just tupTc) = UniqMap.lookup tupTcNm tcm
3923 [tupDc] = tyConDataCons tupTc
3924 TyConApp snatTcNm _ = tyView (argTys !! 1)
3925 n1mTy = mkTyConApp typeNatMul
3926 [mkTyConApp typeNatSub [nTy,LitTy (NumTy 1)]
3927 ,mTy]
3928 splitAtCall =
3929 mkApps (splitAtPrim snatTcNm vecTcNm)
3930 [Right mTy
3931 ,Right n1mTy
3932 ,Right aTy
3933 ,Left (valToTerm snat)
3934 ,Left (valToTerm v)
3935 ]
3936 mVecTy = mkTyConApp vecTcNm [mTy,aTy]
3937 n1mVecTy = mkTyConApp vecTcNm [n1mTy,aTy]
3938 -- Guaranteed no capture, so okay to use unsafe name generation
3939 asNm = mkUnsafeSystemName "as" 0
3940 bsNm = mkUnsafeSystemName "bs" 1
3941 asId = mkLocalId mVecTy asNm
3942 bsId = mkLocalId n1mVecTy bsNm
3943 tupPat = DataPat tupDc [] [asId,bsId]
3944 asAlt = (tupPat, (Var asId))
3945 bsAlt = (tupPat, (Var bsId))
3946
3947 in case n of
3948 0 -> reduce (mkVecNil nilCon mVecTy)
3949 _ -> reduce $
3950 mkVecCons consCon mVecTy n
3951 (Case splitAtCall mVecTy [asAlt])
3952 (mkApps (Prim pInfo)
3953 [Right (LitTy (NumTy (n-1)))
3954 ,Right mTy
3955 ,Right aTy
3956 ,Left (Literal (NaturalLiteral (n-1)))
3957 ,Left (valToTerm snat)
3958 ,Left (Case splitAtCall n1mVecTy [bsAlt])])
3959 -- Construction
3960 -- - initialisation
3961 $(namePat 'Clash.Sized.Vector.replicate) -- :: SNat n -> a -> Vec n a
3962 | isSubj
3963 , let ty' = piResultTys tcm ty tys
3964 , let (_,resTy) = splitFunForallTy ty'
3965 , (TyConApp vecTcNm [lenTy,argTy]) <- tyView resTy
3966 , Right len <- runExcept (tyNatSize tcm lenTy)
3967 -> let (Just vecTc) = UniqMap.lookup vecTcNm tcm
3968 [nilCon,consCon] = tyConDataCons vecTc
3969 in reduce $
3970 mkVec nilCon consCon argTy len
3971 (replicate (fromInteger len) (valToTerm (last args)))
3972 -- - Concatenation
3973 $(namePat '(Clash.Sized.Vector.++)) -- :: Vec n a -> Vec m a -> Vec (n + m) a
3974 | isSubj
3975 , (DC dc vArgs):_ <- args
3976 , Right nTy : Right aTy : _ <- vArgs
3977 , Right n <- runExcept (tyNatSize tcm nTy)
3978 -> case n of
3979 0 -> reduce (valToTerm (last args))
3980 n' | (_ : _ : mTy : _) <- tys
3981 , Right m <- runExcept (tyNatSize tcm mTy)
3982 -> -- x : (xs ++ ys)
3983 reduce $
3984 mkVecCons dc aTy (n' + m) (Either.lefts vArgs !! 1)
3985 (mkApps (Prim pInfo)
3986 [Right (LitTy (NumTy (n'-1)))
3987 ,Right aTy
3988 ,Right mTy
3989 ,Left (Either.lefts vArgs !! 2)
3990 ,Left (valToTerm (last args))
3991 ])
3992 _ -> Nothing
3993 $(namePat 'Clash.Sized.Vector.concat) -- :: Vec n (Vec m a) -> Vec (n * m) a
3994 | isSubj
3995 , (nTy : mTy : aTy : _) <- tys
3996 , (xs : _) <- args
3997 , DC dc vArgs <- xs
3998 , Right n <- runExcept (tyNatSize tcm nTy)
3999 -> case n of
4000 0 -> reduce (mkVecNil dc aTy)
4001 _ | _ : h' : t : _ <- Either.lefts vArgs
4002 , (_,tyView -> TyConApp vecTcNm _) <- splitFunForallTy ty
4003 -> reduceWHNF $
4004 mkApps (vecAppendPrim vecTcNm)
4005 [Right mTy
4006 ,Right aTy
4007 ,Right $ mkTyConApp typeNatMul
4008 [mkTyConApp typeNatSub [nTy,LitTy (NumTy 1)], mTy]
4009 ,Left h'
4010 ,Left $ mkApps (Prim pInfo)
4011 [ Right (LitTy (NumTy (n-1)))
4012 , Right mTy
4013 , Right aTy
4014 , Left t
4015 ]
4016 ]
4017 _ -> Nothing
4018
4019 -- Modifying vectors
4020 "Clash.Sized.Vector.replace_int" -- :: KnownNat n => Vec n a -> Int -> a -> Vec n a
4021 | nTy : aTy : _ <- tys
4022 , _ : xs : i : a : _ <- args
4023 , DC intDc [Left (Literal (IntLiteral i'))] <- i
4024 -> if i' < 0
4025 then Nothing
4026 else case xs of
4027 DC vecTcNm vArgs -> case runExcept (tyNatSize tcm nTy) of
4028 Right 0 -> Nothing
4029 Right n' ->
4030 if i' == 0
4031 then reduce (mkVecCons vecTcNm aTy n' (valToTerm a) (Either.lefts vArgs !! 2))
4032 else reduce $
4033 mkVecCons vecTcNm aTy n' (Either.lefts vArgs !! 1)
4034 (mkApps (Prim pInfo)
4035 [Right (LitTy (NumTy (n'-1)))
4036 ,Right aTy
4037 ,Left (Literal (NaturalLiteral (n'-1)))
4038 ,Left (Either.lefts vArgs !! 2)
4039 ,Left (mkApps (Data intDc)
4040 [Left (Literal (IntLiteral (i'-1)))])
4041 ,Left (valToTerm a)
4042 ])
4043 _ -> Nothing
4044 _ -> Nothing
4045
4046 -- - specialized permutations
4047 $(namePat 'Clash.Sized.Vector.reverse) -- :: Vec n a -> Vec n a
4048 | isSubj
4049 , nTy : aTy : _ <- tys
4050 , [DC vecDc vArgs] <- args
4051 -> case runExcept (tyNatSize tcm nTy) of
4052 Right 0 -> reduce (mkVecNil vecDc aTy)
4053 Right n
4054 | (_,tyView -> TyConApp vecTcNm _) <- splitFunForallTy ty
4055 , let (Just vecTc) = UniqMap.lookup vecTcNm tcm
4056 , let [nilCon,consCon] = tyConDataCons vecTc
4057 -> reduceWHNF $
4058 mkApps (vecAppendPrim vecTcNm)
4059 [Right (LitTy (NumTy (n-1)))
4060 ,Right aTy
4061 ,Right (LitTy (NumTy 1))
4062 ,Left (mkApps (Prim pInfo)
4063 [Right (LitTy (NumTy (n-1)))
4064 ,Right aTy
4065 ,Left (Either.lefts vArgs !! 2)
4066 ])
4067 ,Left (mkVec nilCon consCon aTy 1 [Either.lefts vArgs !! 1])
4068 ]
4069 _ -> Nothing
4070 $(namePat 'Clash.Sized.Vector.transpose) -- :: KnownNat n => Vec m (Vec n a) -> Vec n (Vec m a)
4071 | isSubj
4072 , nTy : mTy : aTy : _ <- tys
4073 , kn : xss : _ <- args
4074 , (_,tyView -> TyConApp vecTcNm _) <- splitFunForallTy ty
4075 , DC _ vArgs <- xss
4076 , Right n <- runExcept (tyNatSize tcm nTy)
4077 , Right m <- runExcept (tyNatSize tcm mTy)
4078 -> case m of
4079 0 -> let (Just vecTc) = UniqMap.lookup vecTcNm tcm
4080 [nilCon,consCon] = tyConDataCons vecTc
4081 in reduce $
4082 mkVec nilCon consCon (mkTyConApp vecTcNm [mTy,aTy]) n
4083 (replicate (fromInteger n) (mkVec nilCon consCon aTy 0 []))
4084 m' -> let (Just vecTc) = UniqMap.lookup vecTcNm tcm
4085 [_,consCon] = tyConDataCons vecTc
4086 Just (consCoTy : _) = dataConInstArgTys consCon
4087 [mTy,aTy,LitTy (NumTy (m'-1))]
4088 in reduceWHNF $
4089 mkApps (vecZipWithPrim vecTcNm)
4090 [ Right aTy
4091 , Right (mkTyConApp vecTcNm [LitTy (NumTy (m'-1)),aTy])
4092 , Right (mkTyConApp vecTcNm [mTy,aTy])
4093 , Right nTy
4094 , Left (mkApps (Data consCon)
4095 [Right mTy
4096 ,Right aTy
4097 ,Right (LitTy (NumTy (m'-1)))
4098 ,Left (primCo consCoTy)
4099 ])
4100 , Left (Either.lefts vArgs !! 1)
4101 , Left (mkApps (Prim pInfo)
4102 [ Right nTy
4103 , Right (LitTy (NumTy (m'-1)))
4104 , Right aTy
4105 , Left (valToTerm kn)
4106 , Left (Either.lefts vArgs !! 2)
4107 ])
4108 ]
4109
4110 $(namePat 'Clash.Sized.Vector.rotateLeftS) -- :: KnownNat n => Vec n a -> SNat d -> Vec n a
4111 | nTy : aTy : _ : _ <- tys
4112 , kn : xs : d : _ <- args
4113 , DC dc vArgs <- xs
4114 , Right n <- runExcept (tyNatSize tcm nTy)
4115 -> case n of
4116 0 -> reduce (mkVecNil dc aTy)
4117 n' | DC snatDc [_,Left d'] <- d
4118 , eval <- Evaluator ghcStep ghcUnwind ghcPrimStep ghcPrimUnwind
4119 , mach2@Machine{mStack=[],mTerm=Literal (NaturalLiteral d2)} <- whnf eval tcm isSubj (setTerm d' $ stackClear mach)
4120 -> case (d2 `mod` n) of
4121 0 -> reduce (valToTerm xs)
4122 d3 -> let (_,tyView -> TyConApp vecTcNm _) = splitFunForallTy ty
4123 (Just vecTc) = UniqMap.lookup vecTcNm tcm
4124 [nilCon,consCon] = tyConDataCons vecTc
4125 in reduceWHNF' mach2 $
4126 mkApps (Prim pInfo)
4127 [Right nTy
4128 ,Right aTy
4129 ,Right (LitTy (NumTy (d3-1)))
4130 ,Left (valToTerm kn)
4131 ,Left (mkApps (vecAppendPrim vecTcNm)
4132 [Right (LitTy (NumTy (n'-1)))
4133 ,Right aTy
4134 ,Right (LitTy (NumTy 1))
4135 ,Left (Either.lefts vArgs !! 2)
4136 ,Left (mkVec nilCon consCon aTy 1 [Either.lefts vArgs !! 1])])
4137 ,Left (mkApps (Data snatDc)
4138 [Right (LitTy (NumTy (d3-1)))
4139 ,Left (Literal (NaturalLiteral (d3-1)))])
4140 ]
4141 _ -> Nothing
4142
4143 $(namePat 'Clash.Sized.Vector.rotateRightS) -- :: KnownNat n => Vec n a -> SNat d -> Vec n a
4144 | isSubj
4145 , nTy : aTy : _ : _ <- tys
4146 , kn : xs : d : _ <- args
4147 , DC dc _ <- xs
4148 , Right n <- runExcept (tyNatSize tcm nTy)
4149 -> case n of
4150 0 -> reduce (mkVecNil dc aTy)
4151 n' | DC snatDc [_,Left d'] <- d
4152 , eval <- Evaluator ghcStep ghcUnwind ghcPrimStep ghcPrimUnwind
4153 , mach2@Machine{mStack=[],mTerm=Literal (NaturalLiteral d2)} <- whnf eval tcm isSubj (setTerm d' $ stackClear mach)
4154 -> case (d2 `mod` n) of
4155 0 -> reduce (valToTerm xs)
4156 d3 -> let (_,tyView -> TyConApp vecTcNm _) = splitFunForallTy ty
4157 in reduceWHNF' mach2 $
4158 mkApps (Prim pInfo)
4159 [Right nTy
4160 ,Right aTy
4161 ,Right (LitTy (NumTy (d3-1)))
4162 ,Left (valToTerm kn)
4163 ,Left (mkVecCons dc aTy n
4164 (mkApps (vecLastPrim vecTcNm)
4165 [Right (LitTy (NumTy (n'-1)))
4166 ,Right aTy
4167 ,Left (valToTerm xs)])
4168 (mkApps (vecInitPrim vecTcNm)
4169 [Right (LitTy (NumTy (n'-1)))
4170 ,Right aTy
4171 ,Left (valToTerm xs)]))
4172 ,Left (mkApps (Data snatDc)
4173 [Right (LitTy (NumTy (d3-1)))
4174 ,Left (Literal (NaturalLiteral (d3-1)))])
4175 ]
4176 _ -> Nothing
4177 -- Element-wise operations
4178 -- - mapping
4179 $(namePat 'Clash.Sized.Vector.map) -- :: (a -> b) -> Vec n a -> Vec n b
4180 | isSubj
4181 , DC dc vArgs <- args !! 1
4182 , aTy : bTy : nTy : _ <- tys
4183 , Right n <- runExcept (tyNatSize tcm nTy)
4184 -> case n of
4185 0 -> reduce (mkVecNil dc bTy)
4186 n' -> reduce $
4187 mkVecCons dc bTy n'
4188 (mkApps (valToTerm (args !! 0)) [Left (Either.lefts vArgs !! 1)])
4189 (mkApps (Prim pInfo)
4190 [Right aTy
4191 ,Right bTy
4192 ,Right (LitTy (NumTy (n' - 1)))
4193 ,Left (valToTerm (args !! 0))
4194 ,Left (Either.lefts vArgs !! 2)])
4195 $(namePat 'Clash.Sized.Vector.imap) -- :: forall n a b . KnownNat n => (Index n -> a -> b) -> Vec n a -> Vec n b
4196 | isSubj
4197 , nTy : aTy : bTy : _ <- tys
4198 , (tyArgs,tyView -> TyConApp vecTcNm _) <- splitFunForallTy ty
4199 , let (tyArgs',_) = splitFunForallTy (Either.rights tyArgs !! 1)
4200 , TyConApp indexTcNm _ <- tyView (Either.rights tyArgs' !! 0)
4201 , Right n <- runExcept (tyNatSize tcm nTy)
4202 , let iLit = mkIndexLit (Either.rights tyArgs' !! 0) nTy n 0
4203 -> reduceWHNF $
4204 mkApps (Prim (PrimInfo "Clash.Sized.Vector.imap_go" (vecImapGoTy vecTcNm indexTcNm) WorkNever SingleResult NoUnfolding))
4205 [Right nTy
4206 ,Right nTy
4207 ,Right aTy
4208 ,Right bTy
4209 ,Left (valToTerm (args !! 1))
4210 ,Left (valToTerm (args !! 2))
4211 ,Left iLit
4212 ]
4213
4214 "Clash.Sized.Vector.imap_go"
4215 | isSubj
4216 , nTy : mTy : aTy : bTy : _ <- tys
4217 , f : xs : (Suspend nArg) : _ <- args
4218 , DC dc vArgs <- xs
4219 , Right n' <- runExcept (tyNatSize tcm nTy)
4220 , Right m <- runExcept (tyNatSize tcm mTy)
4221 -> case m of
4222 0 -> reduce (mkVecNil dc bTy)
4223 m'
4224 | eval <- Evaluator ghcStep ghcUnwind ghcPrimStep ghcPrimUnwind
4225 , mach1@Machine{mStack=[],mTerm=n} <-
4226 whnf eval tcm True (setTerm nArg (stackClear mach))
4227 -> let (tyArgs,_) = splitFunForallTy ty
4228 TyConApp indexTcNm _ = tyView (Either.rights tyArgs !! 2)
4229 iLit = mkIndexLit (Either.rights tyArgs !! 2) nTy n' 1
4230 in Just $ flip setTerm (mach1 {mStack = mStack mach}) $ mkVecCons dc bTy m'
4231 (mkApps (valToTerm f) [Left n,Left (Either.lefts vArgs !! 1)])
4232 (mkApps (Prim pInfo)
4233 [Right nTy
4234 ,Right (LitTy (NumTy (m'-1)))
4235 ,Right aTy
4236 ,Right bTy
4237 ,Left (valToTerm f)
4238 ,Left (Either.lefts vArgs !! 2)
4239 ,Left (mkApps (Prim (PrimInfo (showt '(Clash.Sized.Internal.Index.+#)) (indexAddTy indexTcNm) WorkVariable SingleResult NoUnfolding))
4240 [Right nTy
4241 ,Left (Literal (NaturalLiteral n'))
4242 ,Left n
4243 ,Left iLit
4244 ])
4245 ])
4246 | otherwise
4247 -> Nothing
4248
4249 -- :: forall n a. KnownNat n => (a -> a) -> a -> Vec n a
4250 $(namePat 'Clash.Sized.Vector.iterateI)
4251 | isSubj
4252 , [nTy, aTy] <- tys
4253 , [_n, f, a] <- args
4254 , Right n <- runExcept (tyNatSize tcm nTy)
4255 ->
4256 let
4257 TyConApp vecTcNm _ = tyView (getResultTy tcm ty tys)
4258 Just vecTc = UniqMap.lookup vecTcNm tcm
4259 [nilCon, consCon] = tyConDataCons vecTc
4260 in case n of
4261 0 -> reduce (mkVecNil nilCon aTy)
4262 _ -> reduce $
4263 mkVecCons consCon aTy n
4264 (valToTerm a)
4265 (mkApps
4266 (Prim pInfo)
4267 [ Right (LitTy (NumTy (n - 1)))
4268 , Right aTy
4269 , Left (valToTerm (Lit (NaturalLiteral (n - 1))))
4270 , Left (valToTerm f)
4271 , Left (mkApps (valToTerm f) [Left (valToTerm a)])
4272 ])
4273
4274 -- - Zipping
4275 $(namePat 'Clash.Sized.Vector.zipWith) -- :: (a -> b -> c) -> Vec n a -> Vec n b -> Vec n c
4276 | isSubj
4277 , aTy : bTy : cTy : nTy : _ <- tys
4278 , f : xs : ys : _ <- args
4279 , DC dc vArgs <- xs
4280 , (_,tyView -> TyConApp vecTcNm _) <- splitFunForallTy ty
4281 , Right n <- runExcept (tyNatSize tcm nTy)
4282 -> case n of
4283 0 -> reduce (mkVecNil dc cTy)
4284 n' -> reduce $ mkVecCons dc cTy n'
4285 (mkApps (valToTerm f)
4286 [Left (Either.lefts vArgs !! 1)
4287 ,Left (mkApps (vecHeadPrim vecTcNm)
4288 [Right (LitTy (NumTy (n'-1)))
4289 ,Right bTy
4290 ,Left (valToTerm ys)
4291 ])
4292 ])
4293 (mkApps (Prim pInfo)
4294 [Right aTy
4295 ,Right bTy
4296 ,Right cTy
4297 ,Right (LitTy (NumTy (n' - 1)))
4298 ,Left (valToTerm f)
4299 ,Left (Either.lefts vArgs !! 2)
4300 ,Left (mkApps (vecTailPrim vecTcNm)
4301 [Right (LitTy (NumTy (n'-1)))
4302 ,Right bTy
4303 ,Left (valToTerm ys)
4304 ])])
4305
4306 -- Folding
4307 $(namePat 'Clash.Sized.Vector.foldr) -- :: (a -> b -> b) -> b -> Vec n a -> b
4308 | isSubj
4309 , aTy : bTy : nTy : _ <- tys
4310 , f : z : xs : _ <- args
4311 , DC _ vArgs <- xs
4312 , Right n <- runExcept (tyNatSize tcm nTy)
4313 -> case n of
4314 0 -> reduce (valToTerm z)
4315 _ -> reduceWHNF $
4316 mkApps (valToTerm f)
4317 [Left (Either.lefts vArgs !! 1)
4318 ,Left (mkApps (Prim pInfo)
4319 [Right aTy
4320 ,Right bTy
4321 ,Right (LitTy (NumTy (n-1)))
4322 ,Left (valToTerm f)
4323 ,Left (valToTerm z)
4324 ,Left (Either.lefts vArgs !! 2)
4325 ])
4326 ]
4327 $(namePat 'Clash.Sized.Vector.fold) -- :: (a -> a -> a) -> Vec (n + 1) a -> a
4328 | isSubj
4329 , nTy : aTy : _ <- tys
4330 , f : vs : _ <- args
4331 , DC _ vArgs <- vs
4332 , Right n <- runExcept (tyNatSize tcm nTy)
4333 -> case n of
4334 0 -> reduceWHNF (Either.lefts vArgs !! 1)
4335 _ -> let (tyArgs,_) = splitFunForallTy ty
4336 TyConApp vecTcNm _ = tyView (Either.rights tyArgs !! 1)
4337 tupTcNm = ghcTyconToTyConName (tupleTyCon Boxed 2)
4338 (Just tupTc) = UniqMap.lookup tupTcNm tcm
4339 [tupDc] = tyConDataCons tupTc
4340 n' = n+1
4341 m = n' `div` 2
4342 n1 = n' - m
4343 mTy = LitTy (NumTy m)
4344 m'ty = LitTy (NumTy (m-1))
4345 n1mTy = LitTy (NumTy n1)
4346 n1m'ty = LitTy (NumTy (n1-1))
4347 splitAtCall =
4348 mkApps (Prim (PrimInfo "Clash.Sized.Vector.fold_split" (foldSplitAtTy vecTcNm) WorkNever SingleResult NoUnfolding))
4349 [Right mTy
4350 ,Right n1mTy
4351 ,Right aTy
4352 ,Left (Literal (NaturalLiteral m))
4353 ,Left (valToTerm vs)
4354 ]
4355 mVecTy = mkTyConApp vecTcNm [mTy,aTy]
4356 n1mVecTy = mkTyConApp vecTcNm [n1mTy,aTy]
4357 -- Guaranteed no capture, so okay to use unsafe name generation
4358 asNm = mkUnsafeSystemName "as" 0
4359 bsNm = mkUnsafeSystemName "bs" 1
4360 asId = mkLocalId mVecTy asNm
4361 bsId = mkLocalId n1mVecTy bsNm
4362 tupPat = DataPat tupDc [] [asId,bsId]
4363 asAlt = (tupPat, (Var asId))
4364 bsAlt = (tupPat, (Var bsId))
4365 in reduceWHNF $
4366 mkApps (valToTerm f)
4367 [Left (mkApps (Prim pInfo)
4368 [Right m'ty
4369 ,Right aTy
4370 ,Left (valToTerm f)
4371 ,Left (Case splitAtCall mVecTy [asAlt])
4372 ])
4373 ,Left (mkApps (Prim pInfo)
4374 [Right n1m'ty
4375 ,Right aTy
4376 ,Left (valToTerm f)
4377 ,Left (Case splitAtCall n1mVecTy [bsAlt])
4378 ])
4379 ]
4380
4381
4382 "Clash.Sized.Vector.fold_split" -- :: Natural -> Vec (m + n) a -> (Vec m a, Vec n a)
4383 | isSubj
4384 , mTy : nTy : aTy : _ <- tys
4385 , Right m <- runExcept (tyNatSize tcm mTy)
4386 -> let -- Get the tuple data-constructor
4387 ty1 = piResultTys tcm ty tys
4388 (_,tyView -> TyConApp tupTcNm tyArgs@(tyArg:_)) = splitFunForallTy ty1
4389 (Just tupTc) = UniqMap.lookup tupTcNm tcm
4390 [tupDc] = tyConDataCons tupTc
4391 -- Get the vector data-constructors
4392 TyConApp vecTcNm _ = tyView tyArg
4393 Just vecTc = UniqMap.lookup vecTcNm tcm
4394 [nilCon,consCon] = tyConDataCons vecTc
4395 -- Recursive call to @splitAt@
4396 splitAtRec v =
4397 mkApps (Prim pInfo)
4398 [Right (LitTy (NumTy (m-1)))
4399 ,Right nTy
4400 ,Right aTy
4401 ,Left (Literal (NaturalLiteral (m-1)))
4402 ,Left v
4403 ]
4404 -- Projection either the first or second field of the recursive
4405 -- call to @splitAt@
4406 splitAtSelR v = Case (splitAtRec v)
4407 m1VecTy = mkTyConApp vecTcNm [LitTy (NumTy (m-1)),aTy]
4408 nVecTy = mkTyConApp vecTcNm [nTy,aTy]
4409 -- Guaranteed no capture, so okay to use unsafe name generation
4410 lNm = mkUnsafeSystemName "l" 0
4411 rNm = mkUnsafeSystemName "r" 1
4412 lId = mkLocalId m1VecTy lNm
4413 rId = mkLocalId nVecTy rNm
4414 tupPat = DataPat tupDc [] [lId,rId]
4415 lAlt = (tupPat, (Var lId))
4416 rAlt = (tupPat, (Var rId))
4417 in case m of
4418 -- (Nil,v)
4419 0 -> reduce $
4420 mkApps (Data tupDc) $ (map Right tyArgs) ++
4421 [ Left (mkVecNil nilCon aTy)
4422 , Left (valToTerm (last args))
4423 ]
4424 -- (x:xs) <- v
4425 m' | DC _ vArgs <- last args
4426 -- (x:fst (splitAt (m-1) xs),snd (splitAt (m-1) xs))
4427 -> reduce $
4428 mkApps (Data tupDc) $ (map Right tyArgs) ++
4429 [ Left (mkVecCons consCon aTy m' (Either.lefts vArgs !! 1)
4430 (splitAtSelR (Either.lefts vArgs !! 2) m1VecTy [lAlt]))
4431 , Left (splitAtSelR (Either.lefts vArgs !! 2) nVecTy [rAlt])
4432 ]
4433 -- v doesn't reduce to a data-constructor
4434 _ -> Nothing
4435 -- - Specialised folds
4436 $(namePat 'Clash.Sized.Vector.dfold)
4437 | isSubj
4438 , pTy : kTy : aTy : _ <- tys
4439 , _ : p : f : z : xs : _ <- args
4440 , DC _ vArgs <- xs
4441 , Right k' <- runExcept (tyNatSize tcm kTy)
4442 -> case k' of
4443 0 -> reduce (valToTerm z)
4444 _ -> let (tyArgs,_) = splitFunForallTy ty
4445 (tyArgs',_) = splitFunForallTy (Either.rights tyArgs !! 2)
4446 TyConApp snatTcNm _ = tyView (Either.rights tyArgs' !! 0)
4447 Just snatTc = UniqMap.lookup snatTcNm tcm
4448 [snatDc] = tyConDataCons snatTc
4449 k'ty = LitTy (NumTy (k'-1))
4450 in reduceWHNF $
4451 mkApps (valToTerm f)
4452 [Right k'ty
4453 ,Left (mkApps (Data snatDc)
4454 [Right k'ty
4455 ,Left (Literal (NaturalLiteral (k'-1)))])
4456 ,Left (Either.lefts vArgs !! 1)
4457 ,Left (mkApps (Prim pInfo)
4458 [Right pTy
4459 ,Right k'ty
4460 ,Right aTy
4461 ,Left (Literal (NaturalLiteral (k'-1)))
4462 ,Left (valToTerm p)
4463 ,Left (valToTerm f)
4464 ,Left (valToTerm z)
4465 ,Left (Either.lefts vArgs !! 2)
4466 ])
4467 ]
4468 $(namePat 'Clash.Sized.Vector.dtfold)
4469 | isSubj
4470 , pTy : kTy : aTy : _ <- tys
4471 , _ : p : f : g : xs : _ <- args
4472 , DC _ vArgs <- xs
4473 , Right k' <- runExcept (tyNatSize tcm kTy)
4474 -> case k' of
4475 0 -> reduceWHNF (mkApps (valToTerm f) [Left (Either.lefts vArgs !! 1)])
4476 _ -> let (tyArgs,_) = splitFunForallTy ty
4477 TyConApp vecTcNm _ = tyView (Either.rights tyArgs !! 4)
4478 (tyArgs',_) = splitFunForallTy (Either.rights tyArgs !! 3)
4479 TyConApp snatTcNm _ = tyView (Either.rights tyArgs' !! 0)
4480 Just snatTc = UniqMap.lookup snatTcNm tcm
4481 [snatDc] = tyConDataCons snatTc
4482 tupTcNm = ghcTyconToTyConName (tupleTyCon Boxed 2)
4483 (Just tupTc) = UniqMap.lookup tupTcNm tcm
4484 [tupDc] = tyConDataCons tupTc
4485 k'ty = LitTy (NumTy (k'-1))
4486 k2ty = LitTy (NumTy (2^(k'-1)))
4487 splitAtCall =
4488 mkApps (splitAtPrim snatTcNm vecTcNm)
4489 [Right k2ty
4490 ,Right k2ty
4491 ,Right aTy
4492 ,Left (mkApps (Data snatDc)
4493 [Right k2ty
4494 ,Left (Literal (NaturalLiteral (2^(k'-1))))])
4495 ,Left (valToTerm xs)
4496 ]
4497 xsSVecTy = mkTyConApp vecTcNm [k2ty,aTy]
4498 -- Guaranteed no capture, so okay to use unsafe name generation
4499 xsLNm = mkUnsafeSystemName "xsL" 0
4500 xsRNm = mkUnsafeSystemName "xsR" 1
4501 xsLId = mkLocalId k2ty xsLNm
4502 xsRId = mkLocalId k2ty xsRNm
4503 tupPat = DataPat tupDc [] [xsLId,xsRId]
4504 asAlt = (tupPat, (Var xsLId))
4505 bsAlt = (tupPat, (Var xsRId))
4506 in reduceWHNF $
4507 mkApps (valToTerm g)
4508 [Right k'ty
4509 ,Left (mkApps (Data snatDc)
4510 [Right k'ty
4511 ,Left (Literal (NaturalLiteral (k'-1)))])
4512 ,Left (mkApps (Prim pInfo)
4513 [Right pTy
4514 ,Right k'ty
4515 ,Right aTy
4516 ,Left (Literal (NaturalLiteral (k'-1)))
4517 ,Left (valToTerm p)
4518 ,Left (valToTerm f)
4519 ,Left (valToTerm g)
4520 ,Left (Case splitAtCall xsSVecTy [asAlt])])
4521 ,Left (mkApps (Prim pInfo)
4522 [Right pTy
4523 ,Right k'ty
4524 ,Right aTy
4525 ,Left (Literal (NaturalLiteral (k'-1)))
4526 ,Left (valToTerm p)
4527 ,Left (valToTerm f)
4528 ,Left (valToTerm g)
4529 ,Left (Case splitAtCall xsSVecTy [bsAlt])])
4530 ]
4531 -- Misc
4532 $(namePat 'Clash.Sized.Vector.lazyV)
4533 | isSubj
4534 , nTy : aTy : _ <- tys
4535 , _ : xs : _ <- args
4536 , (_,tyView -> TyConApp vecTcNm _) <- splitFunForallTy ty
4537 , Right n <- runExcept (tyNatSize tcm nTy)
4538 -> case n of
4539 0 -> let (Just vecTc) = UniqMap.lookup vecTcNm tcm
4540 [nilCon,_] = tyConDataCons vecTc
4541 in reduce (mkVecNil nilCon aTy)
4542 n' -> let (Just vecTc) = UniqMap.lookup vecTcNm tcm
4543 [_,consCon] = tyConDataCons vecTc
4544 in reduce $ mkVecCons consCon aTy n'
4545 (mkApps (vecHeadPrim vecTcNm)
4546 [ Right (LitTy (NumTy (n' - 1)))
4547 , Right aTy
4548 , Left (valToTerm xs)
4549 ])
4550 (mkApps (Prim pInfo)
4551 [ Right (LitTy (NumTy (n' - 1)))
4552 , Right aTy
4553 , Left (Literal (NaturalLiteral (n'-1)))
4554 , Left (mkApps (vecTailPrim vecTcNm)
4555 [ Right (LitTy (NumTy (n'-1)))
4556 , Right aTy
4557 , Left (valToTerm xs)
4558 ])
4559 ])
4560 -- Traversable
4561 $(namePat 'Clash.Sized.Vector.traverse#)
4562 | isSubj
4563 , aTy : fTy : bTy : nTy : _ <- tys
4564 , apDict : f : xs : _ <- args
4565 , DC dc vArgs <- xs
4566 , Right n <- runExcept (tyNatSize tcm nTy)
4567 -> case n of
4568 0 -> let (pureF,ids') = runPEM (mkSelectorCase $(curLoc) is0 tcm (valToTerm apDict) 1 1) ids
4569 in reduceWHNF' (mach { mSupply = ids' }) $
4570 mkApps pureF
4571 [Right (mkTyConApp (vecTcNm) [nTy,bTy])
4572 ,Left (mkVecNil dc bTy)]
4573 _ -> let ((fmapF,apF),ids') = flip runPEM ids $ do
4574 fDict <- mkSelectorCase $(curLoc) is0 tcm (valToTerm apDict) 1 0
4575 fmapF' <- mkSelectorCase $(curLoc) is0 tcm fDict 1 0
4576 apF' <- mkSelectorCase $(curLoc) is0 tcm (valToTerm apDict) 1 2
4577 return (fmapF',apF')
4578 n'ty = LitTy (NumTy (n-1))
4579 Just (consCoTy : _) = dataConInstArgTys dc [nTy,bTy,n'ty]
4580 in reduceWHNF' (mach { mSupply = ids' }) $
4581 mkApps apF
4582 [Right (mkTyConApp vecTcNm [n'ty,bTy])
4583 ,Right (mkTyConApp vecTcNm [nTy,bTy])
4584 ,Left (mkApps fmapF
4585 [Right bTy
4586 ,Right (mkFunTy (mkTyConApp vecTcNm [n'ty,bTy])
4587 (mkTyConApp vecTcNm [nTy,bTy]))
4588 ,Left (mkApps (Data dc)
4589 [Right nTy
4590 ,Right bTy
4591 ,Right n'ty
4592 ,Left (primCo consCoTy)])
4593 ,Left (mkApps (valToTerm f)
4594 [Left (Either.lefts vArgs !! 1)])
4595 ])
4596 ,Left (mkApps (Prim pInfo)
4597 [Right aTy
4598 ,Right fTy
4599 ,Right bTy
4600 ,Right n'ty
4601 ,Left (valToTerm apDict)
4602 ,Left (valToTerm f)
4603 ,Left (Either.lefts vArgs !! 2)
4604 ])
4605 ]
4606 where
4607 (tyArgs,_) = splitFunForallTy ty
4608 TyConApp vecTcNm _ = tyView (Either.rights tyArgs !! 2)
4609 (ids, is0) = (mSupply mach, mScopeNames mach)
4610
4611 -- BitPack
4612 $(namePat 'Clash.Sized.Vector.concatBitVector#)
4613 | isSubj
4614 , nTy : mTy : _ <- tys
4615 , _ : km : v : _ <- args
4616 , DC _ vArgs <- v
4617 , Right n <- runExcept (tyNatSize tcm nTy)
4618 -> case n of
4619 0 -> let resTyInfo = extractTySizeInfo tcm ty tys
4620 in reduce (mkBitVectorLit' resTyInfo 0 0)
4621 n' | Right m <- runExcept (tyNatSize tcm mTy)
4622 , (_,tyView -> TyConApp bvTcNm _) <- splitFunForallTy ty
4623 -> reduceWHNF $
4624 mkApps (bvAppendPrim bvTcNm)
4625 [ Right (mkTyConApp typeNatMul [LitTy (NumTy (n'-1)),mTy])
4626 , Right mTy
4627 , Left (Literal (NaturalLiteral ((n'-1)*m)))
4628 , Left (Either.lefts vArgs !! 1)
4629 , Left (mkApps (Prim pInfo)
4630 [ Right (LitTy (NumTy (n'-1)))
4631 , Right mTy
4632 , Left (Literal (NaturalLiteral (n'-1)))
4633 , Left (valToTerm km)
4634 , Left (Either.lefts vArgs !! 2)
4635 ])
4636 ]
4637 _ -> Nothing
4638 $(namePat 'Clash.Sized.Vector.unconcatBitVector#)
4639 | isSubj
4640 , nTy : mTy : _ <- tys
4641 , _ : km : bv : _ <- args
4642 , (_,tyView -> TyConApp vecTcNm [_,bvMTy]) <- splitFunForallTy ty
4643 , TyConApp bvTcNm _ <- tyView bvMTy
4644 , Right n <- runExcept (tyNatSize tcm nTy)
4645 -> case n of
4646 0 ->
4647 let (Just vecTc) = UniqMap.lookup vecTcNm tcm
4648 [nilCon,_] = tyConDataCons vecTc
4649 in reduce (mkVecNil nilCon (mkTyConApp bvTcNm [mTy]))
4650 n' | Right m <- runExcept (tyNatSize tcm mTy) ->
4651 let Just vecTc = UniqMap.lookup vecTcNm tcm
4652 [_,consCon] = tyConDataCons vecTc
4653 tupTcNm = ghcTyconToTyConName (tupleTyCon Boxed 2)
4654 Just tupTc = UniqMap.lookup tupTcNm tcm
4655 [tupDc] = tyConDataCons tupTc
4656 splitCall =
4657 mkApps (bvSplitPrim bvTcNm)
4658 [ Right (mkTyConApp typeNatMul [LitTy (NumTy (n'-1)),mTy])
4659 , Right mTy
4660 , Left (Literal (NaturalLiteral ((n'-1)*m)))
4661 , Left (valToTerm bv)
4662 ]
4663 mBVTy = mkTyConApp bvTcNm [mTy]
4664 n1BVTy = mkTyConApp bvTcNm
4665 [mkTyConApp typeNatMul
4666 [LitTy (NumTy (n'-1))
4667 ,mTy]]
4668 -- Guaranteed no capture, so okay to use unsafe name generation
4669 xNm = mkUnsafeSystemName "x" 0
4670 bvNm = mkUnsafeSystemName "bv'" 1
4671 xId = mkLocalId mBVTy xNm
4672 bvId = mkLocalId n1BVTy bvNm
4673 tupPat = DataPat tupDc [] [xId,bvId]
4674 xAlt = (tupPat, (Var xId))
4675 bvAlt = (tupPat, (Var bvId))
4676
4677 in reduce $ mkVecCons consCon (mkTyConApp bvTcNm [mTy]) n'
4678 (Case splitCall mBVTy [xAlt])
4679 (mkApps (Prim pInfo)
4680 [ Right (LitTy (NumTy (n'-1)))
4681 , Right mTy
4682 , Left (Literal (NaturalLiteral (n'-1)))
4683 , Left (valToTerm km)
4684 , Left (Case splitCall n1BVTy [bvAlt])
4685 ])
4686 _ -> Nothing
4687 #if MIN_VERSION_ghc(9,4,0)
4688 "Data.Text.Show.$wunpackCStringAscii#"
4689 | [Lit (StringLiteral addr)] <- args
4690 , Text.Text (Text.ByteArray ba) _off len <- Text.pack addr
4691 -> let (_,tyView -> TyConApp tupTcNm tyArgs) = splitFunForallTy ty
4692 (Just tupTc) = UniqMap.lookup tupTcNm tcm
4693 [tupDc] = tyConDataCons tupTc
4694 ret = mkApps (Data tupDc) (map Right tyArgs ++
4695 [ Left (Literal (ByteArrayLiteral (BA.ByteArray ba)))
4696 , Left (Literal (IntLiteral 0))
4697 , Left (Literal (IntLiteral (toInteger len)))])
4698 in reduce ret
4699 -- XXX: Does not seem to exist?
4700 "GHC.Magic.noinlineConstraint"
4701 | [arg] <- args
4702 -> reduce (valToTerm arg)
4703 #if MIN_VERSION_base(4,18,0)
4704 $(namePat 'GHC.TypeNats.withSomeSNat)
4705 | Lit (NaturalLiteral n) : fun : _ <- args
4706 , _ : funTy : _ <- Either.rights (fst (splitFunForallTy ty))
4707 , (tyView -> TyConApp snatTcNm _) : _ <- Either.rights (fst (splitFunForallTy funTy))
4708 , Just snatTc <- UniqMap.lookup snatTcNm tcm
4709 , [snatDc] <- tyConDataCons snatTc
4710 -> let nTy = LitTy (NumTy n)
4711 snat = mkApps (Data snatDc) [Right nTy, Left (Literal (NaturalLiteral n))]
4712 ret = mkApps (valToTerm fun) [Right nTy, Left snat]
4713 in reduce ret
4714 #endif
4715 -- XXX: Does not seem to exist?
4716 "GHC.Magic.nospec"
4717 | [arg] <- args
4718 -> reduce (valToTerm arg)
4719 "GHC.Float.$wproperFractionDouble"
4720 | _ : Lit (DoubleLiteral d) : _ <- args
4721 , [sty@(tyView -> TyConApp signedTcNm [nTy@(LitTy (NumTy kn))])] <- tys
4722 , nameOcc signedTcNm == showt ''Signed
4723 , (_, tyView -> TyConApp tupTcNm tyArgs) <- splitFunForallTy ty
4724 , Just tupTc <- UniqMap.lookup tupTcNm tcm
4725 , [tupDc] <- tyConDataCons tupTc
4726 -> let (sn, d1) = reifyNat kn (\p -> first toInteger (op p (wordToDouble d)))
4727 ret = mkApps (Data tupDc) (map Right tyArgs ++
4728 [ Left (mkSignedLit sty nTy kn sn)
4729 , Left (mkDoubleCLit tcm (doubleToWord d1) (last tyArgs))
4730 ])
4731 in reduce ret
4732 where
4733 op :: KnownNat n => Proxy n -> Double -> (Signed n, Double)
4734 op _ = properFraction
4735 "GHC.Internal.Float.$wproperFractionDouble"
4736 | _ : Lit (DoubleLiteral d) : _ <- args
4737 , [sty@(tyView -> TyConApp signedTcNm [nTy@(LitTy (NumTy kn))])] <- tys
4738 , nameOcc signedTcNm == showt ''Clash.Sized.Internal.Signed.Signed
4739 , (_, tyView -> TyConApp tupTcNm tyArgs) <- splitFunForallTy ty
4740 , Just tupTc <- UniqMap.lookup tupTcNm tcm
4741 , [tupDc] <- tyConDataCons tupTc
4742 -> let (sn, d1) = reifyNat kn (\p -> first toInteger (op p (wordToDouble d)))
4743 ret = mkApps (Data tupDc) (map Right tyArgs ++
4744 [ Left (mkSignedLit sty nTy kn sn)
4745 , Left (mkDoubleCLit tcm (doubleToWord d1) (last tyArgs))
4746 ])
4747 in reduce ret
4748 where
4749 op :: KnownNat n => Proxy n -> Double -> (Signed n, Double)
4750 op _ = properFraction
4751 #endif
4752 _ -> Nothing
4753 where
4754 ty = primType pInfo
4755
4756 checkNaturalRange1 nTy i f =
4757 checkNaturalRange nTy [i]
4758 (\[i'] -> naturalToNaturalLiteral (f i'))
4759
4760 checkNaturalRange2 nTy i j f =
4761 checkNaturalRange nTy [i, j]
4762 (\[i', j'] -> naturalToNaturalLiteral (f i' j'))
4763
4764 -- Check given integer's range. If any of them are less than zero, give up
4765 -- and return an undefined type.
4766 checkNaturalRange
4767 :: Type
4768 -- Type of GHC.Natural.Natural ^
4769 -> [Integer]
4770 -> ([Natural] -> Term)
4771 -> Term
4772 checkNaturalRange nTy natsAsInts f =
4773 if any (<0) natsAsInts then
4774 TyApp (Prim NP.undefined) nTy
4775 else
4776 f (map fromInteger natsAsInts)
4777
4778 reduce :: Term -> Maybe Machine
4779 reduce e = case isX e of
4780 Left msg -> trace (unlines ["Warning: Not evaluating constant expression:", show (primName pInfo), "Because doing so generates an XException:", msg]) Nothing
4781 Right e' -> Just (setTerm e' mach)
4782
4783 reduceWHNF e =
4784 let eval = Evaluator ghcStep ghcUnwind ghcPrimStep ghcPrimUnwind
4785 mach1@Machine{mStack=[]} = whnf eval tcm isSubj (setTerm e $ stackClear mach)
4786 in Just $ mach1 { mStack = mStack mach }
4787
4788 reduceWHNF' mach1 e =
4789 let eval = Evaluator ghcStep ghcUnwind ghcPrimStep ghcPrimUnwind
4790 mach2@Machine{mStack=[]} = whnf eval tcm isSubj (setTerm e mach1)
4791 in Just $ mach2 { mStack = mStack mach }
4792
4793 makeUndefinedIf :: Exception e => (e -> Bool) -> Term -> Term
4794 makeUndefinedIf wantToHandle tm =
4795 case unsafeDupablePerformIO $ tryJust selectException (evaluate $ force tm) of
4796 Right b -> b
4797 Left e -> trace (msg e) (TyApp (Prim NP.undefined) resTy)
4798 where
4799 resTy = getResultTy tcm ty tys
4800 selectException e | wantToHandle e = Just e
4801 | otherwise = Nothing
4802 msg e = unlines ["Warning: caught exception: \"" ++ show e ++ "\" while trying to evaluate: "
4803 , showPpr (mkApps (Prim pInfo) (map (Left . valToTerm) args))
4804 ]
4805
4806 catchDivByZero = makeUndefinedIf (==DivideByZero)
4807
4808 -- Helper functions for literals
4809
4810 pairOf :: (Value -> Maybe a) -> [Value] -> Maybe (a, a)
4811 pairOf f [x, y] = (,) <$> f x <*> f y
4812 pairOf _ _ = Nothing
4813
4814 listOf :: (Value -> Maybe a) -> [Value] -> [a]
4815 listOf = mapMaybe
4816
4817 wrapUnsigned :: Integer -> Integer -> Integer
4818 wrapUnsigned n i = i `mod` sz
4819 where
4820 sz = 1 `shiftL` fromInteger n
4821
4822 wrapSigned :: Integer -> Integer -> Integer
4823 wrapSigned n i = if n == 0 then 0 else res
4824 where
4825 mask = 1 `shiftL` fromInteger (n - 1)
4826 res = case divMod i mask of
4827 (s,i1) | even s -> i1
4828 | otherwise -> i1 - mask
4829
4830 doubleLiterals' :: [Value] -> [Word64]
4831 doubleLiterals' = listOf doubleLiteral
4832
4833 doubleLiteral :: Value -> Maybe Word64
4834 doubleLiteral v = case v of
4835 Lit (DoubleLiteral i) -> Just i
4836 _ -> Nothing
4837
4838 floatLiterals' :: [Value] -> [Word32]
4839 floatLiterals' = listOf floatLiteral
4840
4841 floatLiteral :: Value -> Maybe Word32
4842 floatLiteral v = case v of
4843 Lit (FloatLiteral i) -> Just i
4844 _ -> Nothing
4845
4846 integerLiterals :: [Value] -> Maybe (Integer, Integer)
4847 integerLiterals = pairOf integerLiteral
4848
4849 integerLiteral :: Value -> Maybe Integer
4850 integerLiteral v =
4851 case v of
4852 Lit (IntegerLiteral i) -> Just i
4853 DC dc [Left (Literal (IntLiteral i))]
4854 | dcTag dc == 1
4855 -> Just i
4856 DC dc [Left (Literal (ByteArrayLiteral (BA.ByteArray ba)))]
4857 | dcTag dc == 2
4858 #if MIN_VERSION_base(4,15,0)
4859 -> Just (IP ba)
4860 #else
4861 -> Just (Jp# (BN# ba))
4862 #endif
4863 | dcTag dc == 3
4864 #if MIN_VERSION_base(4,15,0)
4865 -> Just (IN ba)
4866 #else
4867 -> Just (Jn# (BN# ba))
4868 #endif
4869 _ -> Nothing
4870
4871 naturalLiterals :: [Value] -> Maybe (Integer, Integer)
4872 naturalLiterals = pairOf naturalLiteral
4873
4874 naturalLiteral :: Value -> Maybe Integer
4875 naturalLiteral v =
4876 case v of
4877 Lit (NaturalLiteral i) -> Just i
4878 DC dc [Left (Literal (WordLiteral i))]
4879 | dcTag dc == 1
4880 -> Just i
4881 DC dc [Left (Literal (ByteArrayLiteral (BA.ByteArray ba)))]
4882 | dcTag dc == 2
4883 #if MIN_VERSION_base(4,15,0)
4884 -> Just (IP ba)
4885 #else
4886 -> Just (Jp# (BN# ba))
4887 #endif
4888 _ -> Nothing
4889
4890 integerLiterals' :: [Value] -> [Integer]
4891 integerLiterals' = listOf integerLiteral
4892
4893 naturalLiterals' :: [Value] -> [Integer]
4894 naturalLiterals' = listOf naturalLiteral
4895
4896 intLiterals :: [Value] -> Maybe (Integer,Integer)
4897 intLiterals = pairOf intLiteral
4898
4899 intLiterals' :: [Value] -> [Integer]
4900 intLiterals' = listOf intLiteral
4901
4902 intCLiterals' :: [Value] -> [Integer]
4903 intCLiterals' = listOf intCLiteral
4904
4905 intLiteral :: Value -> Maybe Integer
4906 intLiteral x = case x of
4907 Lit (IntLiteral i) -> Just i
4908 _ -> Nothing
4909
4910 #if MIN_VERSION_base(4,16,0)
4911 int8Literals' :: [Value] -> [Integer]
4912 int8Literals' = listOf int8Literal
4913
4914 int8Literal :: Value -> Maybe Integer
4915 int8Literal x = case x of
4916 Lit (Int8Literal i) -> Just i
4917 _ -> Nothing
4918
4919 int16Literals' :: [Value] -> [Integer]
4920 int16Literals' = listOf int16Literal
4921
4922 int16Literal :: Value -> Maybe Integer
4923 int16Literal x = case x of
4924 Lit (Int16Literal i) -> Just i
4925 _ -> Nothing
4926
4927 int32Literals' :: [Value] -> [Integer]
4928 int32Literals' = listOf int32Literal
4929
4930 int32Literal :: Value -> Maybe Integer
4931 int32Literal x = case x of
4932 Lit (Int32Literal i) -> Just i
4933 _ -> Nothing
4934
4935 #if MIN_VERSION_base(4,17,0)
4936 int64Literals' :: [Value] -> [Integer]
4937 int64Literals' = listOf int64Literal
4938
4939 int64Literal :: Value -> Maybe Integer
4940 int64Literal x = case x of
4941 Lit (Int64Literal i) -> Just i
4942 _ -> Nothing
4943 #endif
4944 #endif
4945
4946 intCLiteral :: Value -> Maybe Integer
4947 intCLiteral v = case v of
4948 (DC _ [Left (Literal (IntLiteral i))]) -> Just i
4949 _ -> Nothing
4950
4951 intCLiterals :: [Value] -> Maybe (Integer, Integer)
4952 intCLiterals = pairOf intCLiteral
4953
4954 wordLiterals :: [Value] -> Maybe (Integer,Integer)
4955 wordLiterals = pairOf wordLiteral
4956
4957 wordLiterals' :: [Value] -> [Integer]
4958 wordLiterals' = listOf wordLiteral
4959
4960 wordLiteral :: Value -> Maybe Integer
4961 wordLiteral x = case x of
4962 Lit (WordLiteral i) -> Just i
4963 _ -> Nothing
4964
4965 #if MIN_VERSION_base(4,16,0)
4966 word8Literals' :: [Value] -> [Integer]
4967 word8Literals' = listOf word8Literal
4968
4969 word8Literal :: Value -> Maybe Integer
4970 word8Literal x = case x of
4971 Lit (Word8Literal i) -> Just i
4972 _ -> Nothing
4973
4974 word16Literals' :: [Value] -> [Integer]
4975 word16Literals' = listOf word16Literal
4976
4977 word16Literal :: Value -> Maybe Integer
4978 word16Literal x = case x of
4979 Lit (Word16Literal i) -> Just i
4980 _ -> Nothing
4981
4982 word32Literals' :: [Value] -> [Integer]
4983 word32Literals' = listOf word32Literal
4984
4985 word32Literal :: Value -> Maybe Integer
4986 word32Literal x = case x of
4987 Lit (Word32Literal i) -> Just i
4988 _ -> Nothing
4989 #endif
4990
4991 word64Literals' :: [Value] -> [Integer]
4992 word64Literals' = listOf word64Literal
4993
4994 #if MIN_VERSION_base(4,17,0)
4995 word64Literal :: Value -> Maybe Integer
4996 word64Literal x = case x of
4997 Lit (Word64Literal i) -> Just i
4998 _ -> Nothing
4999 #else
5000 -- Prior to GHC 9.4 Word64# didn't exist, 64 bit primitives took Word# instead
5001 word64Literal :: Value -> Maybe Integer
5002 word64Literal= wordLiteral
5003 #endif
5004
5005 charLiterals :: [Value] -> Maybe (Char,Char)
5006 charLiterals = pairOf charLiteral
5007
5008 charLiterals' :: [Value] -> [Char]
5009 charLiterals' = listOf charLiteral
5010
5011 charLiteral :: Value -> Maybe Char
5012 charLiteral x = case x of
5013 Lit (CharLiteral c) -> Just c
5014 _ -> Nothing
5015
5016 sizedLiterals :: Text -> [Value] -> Maybe (Integer,Integer)
5017 sizedLiterals szCon = pairOf (sizedLiteral szCon)
5018
5019 sizedLiterals' :: Text -> [Value] -> [Integer]
5020 sizedLiterals' szCon = listOf (sizedLiteral szCon)
5021
5022 sizedLiteral :: Text -> Value -> Maybe Integer
5023 sizedLiteral szCon val = case val of
5024 PrimVal p _ [_, Lit (IntegerLiteral i)]
5025 | primName p == szCon -> Just i
5026 _ -> Nothing
5027
5028 bitLiterals
5029 :: [Value]
5030 -> [(Integer,Integer)]
5031 bitLiterals = map normalizeBit . mapMaybe go
5032 where
5033 normalizeBit (msk,v) = (msk .&. 1, v .&. 1)
5034 go val = case val of
5035 PrimVal p _ [Lit (WordLiteral m), Lit (IntegerLiteral i)]
5036 | primName p == showt 'Clash.Sized.Internal.BitVector.fromInteger##
5037 -> Just (m,i)
5038 _ -> Nothing
5039
5040 indexLiterals, signedLiterals, unsignedLiterals
5041 :: [Value] -> Maybe (Integer,Integer)
5042 indexLiterals = sizedLiterals (showt 'Clash.Sized.Internal.Index.fromInteger#)
5043 signedLiterals = sizedLiterals (showt 'Clash.Sized.Internal.Signed.fromInteger#)
5044 unsignedLiterals = sizedLiterals (showt 'Clash.Sized.Internal.Unsigned.fromInteger#)
5045
5046 indexLiterals', signedLiterals', unsignedLiterals'
5047 :: [Value] -> [Integer]
5048 indexLiterals' = sizedLiterals' (showt 'Clash.Sized.Internal.Index.fromInteger#)
5049 signedLiterals' = sizedLiterals' (showt 'Clash.Sized.Internal.Signed.fromInteger#)
5050 unsignedLiterals' = sizedLiterals' (showt 'Clash.Sized.Internal.Unsigned.fromInteger#)
5051
5052 bitVectorLiterals'
5053 :: [Value] -> [(Integer,Integer)]
5054 bitVectorLiterals' = listOf bitVectorLiteral
5055
5056 bitVectorLiteral :: Value -> Maybe (Integer, Integer)
5057 bitVectorLiteral val = case val of
5058 (PrimVal p _ [_, Lit (NaturalLiteral m), Lit (IntegerLiteral i)])
5059 | primName p == showt 'Clash.Sized.Internal.BitVector.fromInteger# -> Just (m, i)
5060 _ -> Nothing
5061
5062 toBV :: (Integer,Integer) -> BitVector n
5063 toBV (mask,val) = BV (fromInteger mask) (fromInteger val)
5064
5065 splitBV :: BitVector n -> (Integer,Integer)
5066 splitBV (BV msk val) = (toInteger msk, toInteger val)
5067
5068 toBit :: (Integer,Integer) -> Bit
5069 toBit (mask,val) = Bit (fromInteger mask) (fromInteger val)
5070
5071 valArgs
5072 :: Value
5073 -> Maybe [Term]
5074 valArgs v =
5075 case v of
5076 PrimVal _ _ vs -> Just (fmap valToTerm vs)
5077 DC _ args -> Just (Either.lefts args)
5078 _ -> Nothing
5079
5080 -- Tries to match literal arguments to a function like
5081 -- (Unsigned.shiftL# :: forall n. KnownNat n => Unsigned n -> Int -> Unsigned n)
5082 sizedLitIntLit
5083 :: Text -> TyConMap -> [Type] -> [Value]
5084 -> Maybe (Type,Integer,Integer,Integer)
5085 sizedLitIntLit szCon tcm tys args
5086 | Just (nTy,kn) <- extractKnownNat tcm tys
5087 , [_
5088 ,PrimVal p _ [_,Lit (IntegerLiteral i)]
5089 ,valArgs -> Just [Literal (IntLiteral j)]
5090 ] <- args
5091 , primName p == szCon
5092 = Just (nTy,kn,i,j)
5093 | otherwise
5094 = Nothing
5095
5096 signedLitIntLit, unsignedLitIntLit
5097 :: TyConMap -> [Type] -> [Value]
5098 -> Maybe (Type,Integer,Integer,Integer)
5099 signedLitIntLit = sizedLitIntLit (showt 'Clash.Sized.Internal.Signed.fromInteger#)
5100 unsignedLitIntLit = sizedLitIntLit (showt 'Clash.Sized.Internal.Unsigned.fromInteger#)
5101
5102 bitVectorLitIntLit
5103 :: TyConMap -> [Type] -> [Value]
5104 -> Maybe (Type,Integer,(Integer,Integer),Integer)
5105 bitVectorLitIntLit tcm tys args
5106 | Just (nTy,kn) <- extractKnownNat tcm tys
5107 , [_
5108 ,PrimVal p _ [_,Lit (NaturalLiteral m),Lit (IntegerLiteral i)]
5109 ,valArgs -> Just [Literal (IntLiteral j)]
5110 ] <- args
5111 , primName p == showt 'Clash.Sized.Internal.BitVector.fromInteger#
5112 = Just (nTy,kn,(m,i),j)
5113 | otherwise
5114 = Nothing
5115
5116 mkIntCLit :: TyConMap -> Integer -> Type -> Term
5117 mkIntCLit tcm lit resTy =
5118 App (Data intDc) (Literal (IntLiteral lit))
5119 where
5120 (_, tyView -> TyConApp intTcNm []) = splitFunForallTy resTy
5121 Just intTc = UniqMap.lookup intTcNm tcm
5122 [intDc] = tyConDataCons intTc
5123
5124 mkFloatCLit :: TyConMap -> Word32 -> Type -> Term
5125 mkFloatCLit tcm lit resTy =
5126 App (Data floatDc) (Literal (FloatLiteral lit))
5127 where
5128 (_, tyView -> TyConApp floatTcNm []) = splitFunForallTy resTy
5129 (Just floatTc) = UniqMap.lookup floatTcNm tcm
5130 [floatDc] = tyConDataCons floatTc
5131
5132 mkDoubleCLit :: TyConMap -> Word64 -> Type -> Term
5133 mkDoubleCLit tcm lit resTy =
5134 App (Data doubleDc) (Literal (DoubleLiteral lit))
5135 where
5136 (_, tyView -> TyConApp doubleTcNm []) = splitFunForallTy resTy
5137 (Just doubleTc) = UniqMap.lookup doubleTcNm tcm
5138 [doubleDc] = tyConDataCons doubleTc
5139
5140 mkSomeNat :: TyConMap -> Integer -> Type -> Term
5141 mkSomeNat tcm lit resTy =
5142 mkApps (Data someNatDc)
5143 [ Right (LitTy (NumTy lit))
5144 , Left (Literal (NaturalLiteral lit))
5145 , Left proxy
5146 ]
5147 where
5148 -- Get the SomeNat data constructor
5149 TyConApp someNatTcNm [] = tyView resTy
5150 (Just someNatTc) = UniqMap.lookup someNatTcNm tcm
5151 [someNatDc] = tyConDataCons someNatTc
5152
5153 -- Get the Proxy data constructor
5154 (_:_:Right (tyView -> TyConApp proxyTcNm [natTy,_]):_,_) =
5155 splitFunForallTy (dcType someNatDc)
5156 (Just proxyTc) = UniqMap.lookup proxyTcNm tcm
5157 [proxyDc] = tyConDataCons proxyTc
5158
5159 -- Build the Proxy argument
5160 proxy = mkApps (Data proxyDc)
5161 [ Right natTy
5162 , Right (LitTy (NumTy lit))
5163 ]
5164
5165 -- From an argument list to function of type
5166 -- forall n. KnownNat n => ...
5167 -- extract (nTy,nInt)
5168 -- where nTy is the Type of n
5169 -- and nInt is its value as an Integer
5170 extractKnownNat :: TyConMap -> [Type] -> Maybe (Type, Integer)
5171 extractKnownNat tcm tys = case tys of
5172 nTy : _ | Right nInt <- runExcept (tyNatSize tcm nTy)
5173 -> Just (nTy, nInt)
5174 _ -> Nothing
5175
5176 -- From an argument list to function of type
5177 -- forall n m o .. . (KnownNat n, KnownNat m, KnownNat o, ..) => ...
5178 -- extract [(nTy,nInt), (mTy,mInt), (oTy,oInt)]
5179 -- where nTy is the Type of n
5180 -- and nInt is its value as an Integer
5181 extractKnownNats :: TyConMap -> [Type] -> [(Type, Integer)]
5182 extractKnownNats tcm =
5183 mapMaybe (extractKnownNat tcm . pure)
5184
5185 -- Construct a constant term of a sized type
5186 mkSizedLit
5187 :: (Type -> Term)
5188 -- ^ Type constructor?
5189 -> Type
5190 -- ^ Result type
5191 -> Type
5192 -- ^ forall n.
5193 -> Integer
5194 -- ^ KnownNat n
5195 -> Integer
5196 -- ^ Value to construct
5197 -> Term
5198 mkSizedLit conPrim ty nTy kn val =
5199 mkApps
5200 (conPrim sTy)
5201 [ Right nTy
5202 , Left (Literal (NaturalLiteral kn))
5203 , Left (Literal (IntegerLiteral val)) ]
5204 where
5205 (_,sTy) = splitFunForallTy ty
5206
5207 mkBitLit
5208 :: Type
5209 -- ^ Result type
5210 -> Integer
5211 -- ^ Mask
5212 -> Integer
5213 -- ^ Value
5214 -> Term
5215 mkBitLit ty msk val =
5216 mkApps (bConPrim sTy) [ Left (Literal (WordLiteral (msk .&. 1)))
5217 , Left (Literal (IntegerLiteral (val .&. 1)))]
5218 where
5219 (_,sTy) = splitFunForallTy ty
5220
5221 mkSignedLit, mkUnsignedLit
5222 :: Type
5223 -- Result type
5224 -> Type
5225 -- forall n.
5226 -> Integer
5227 -- KnownNat n
5228 -> Integer
5229 -- Value
5230 -> Term
5231 mkSignedLit = mkSizedLit signedConPrim
5232 mkUnsignedLit = mkSizedLit unsignedConPrim
5233
5234 mkBitVectorLit
5235 :: Type
5236 -- ^ Result type
5237 -> Type
5238 -- ^ forall n.
5239 -> Integer
5240 -- ^ KnownNat n
5241 -> Integer
5242 -- ^ mask
5243 -> Integer
5244 -- ^ Value to construct
5245 -> Term
5246 mkBitVectorLit ty nTy kn mask val
5247 = mkApps (bvConPrim sTy)
5248 [Right nTy
5249 ,Left (Literal (NaturalLiteral kn))
5250 ,Left (Literal (NaturalLiteral mask))
5251 ,Left (Literal (IntegerLiteral val))]
5252 where
5253 (_,sTy) = splitFunForallTy ty
5254
5255 mkIndexLitE
5256 :: Type
5257 -- ^ Result type
5258 -> Type
5259 -- ^ forall n.
5260 -> Integer
5261 -- ^ KnownNat n
5262 -> Integer
5263 -- ^ Value to construct
5264 -> Either Term Term
5265 -- ^ Either undefined (if given value is out of bounds of given type) or term
5266 -- representing literal
5267 mkIndexLitE rTy nTy kn val
5268 | val >= 0
5269 , val < kn
5270 = Right (mkSizedLit indexConPrim rTy nTy kn val)
5271 | otherwise
5272 = Left (TyApp (Prim NP.undefined) (mkTyConApp indexTcNm [nTy]))
5273 where
5274 TyConApp indexTcNm _ = tyView (snd (splitFunForallTy rTy))
5275
5276 mkIndexLit
5277 :: Type
5278 -- ^ Result type
5279 -> Type
5280 -- ^ forall n.
5281 -> Integer
5282 -- ^ KnownNat n
5283 -> Integer
5284 -- ^ Value to construct
5285 -> Term
5286 mkIndexLit rTy nTy kn val =
5287 either id id (mkIndexLitE rTy nTy kn val)
5288
5289 mkBitVectorLit'
5290 :: (Type, Type, Integer)
5291 -- ^ (result type, forall n., KnownNat n)
5292 -> Integer
5293 -- ^ Mask
5294 -> Integer
5295 -- ^ Value
5296 -> Term
5297 mkBitVectorLit' (ty,nTy,kn) = mkBitVectorLit ty nTy kn
5298
5299 mkIndexLit'
5300 :: (Type, Type, Integer)
5301 -- ^ (result type, forall n., KnownNat n)
5302 -> Integer
5303 -- ^ value
5304 -> Term
5305 mkIndexLit' (rTy,nTy,kn) = mkIndexLit rTy nTy kn
5306
5307 boolToIntLiteral :: Bool -> Term
5308 boolToIntLiteral b = if b then Literal (IntLiteral 1) else Literal (IntLiteral 0)
5309
5310 boolToBoolLiteral :: TyConMap -> Type -> Bool -> Term
5311 boolToBoolLiteral tcm ty b =
5312 let (_,tyView -> TyConApp boolTcNm []) = splitFunForallTy ty
5313 (Just boolTc) = UniqMap.lookup boolTcNm tcm
5314 [falseDc,trueDc] = tyConDataCons boolTc
5315 retDc = if b then trueDc else falseDc
5316 in Data retDc
5317
5318 charToCharLiteral :: Char -> Term
5319 charToCharLiteral = Literal . CharLiteral
5320
5321 integerToIntLiteral :: Integer -> Term
5322 integerToIntLiteral = Literal . IntLiteral . toInteger . (fromInteger :: Integer -> Int) -- for overflow behavior
5323
5324 integerToWordLiteral :: Integer -> Term
5325 integerToWordLiteral = Literal . WordLiteral . toInteger . (fromInteger :: Integer -> Word) -- for overflow behavior
5326
5327 #if MIN_VERSION_base(4,17,0) || WORD_SIZE_IN_BITS < 64
5328 integerToInt64Literal :: Integer -> Term
5329 integerToInt64Literal = Literal . Int64Literal . toInteger . (fromInteger :: Integer -> Int64) -- for overflow behavior
5330
5331 integerToWord64Literal :: Integer -> Term
5332 integerToWord64Literal = Literal . Word64Literal . toInteger . (fromInteger :: Integer -> Word64) -- for overflow behavior
5333 #endif
5334
5335 integerToIntegerLiteral :: Integer -> Term
5336 integerToIntegerLiteral = Literal . IntegerLiteral
5337
5338 naturalToNaturalLiteral :: Natural -> Term
5339 naturalToNaturalLiteral = Literal . NaturalLiteral . toInteger
5340
5341 bConPrim :: Type -> Term
5342 bConPrim (tyView -> TyConApp bTcNm _)
5343 = Prim (PrimInfo (showt 'Clash.Sized.Internal.BitVector.fromInteger##) funTy WorkNever SingleResult NoUnfolding)
5344 where
5345 funTy = foldr1 mkFunTy [wordPrimTy,integerPrimTy,mkTyConApp bTcNm []]
5346 bConPrim _ = error $ $(curLoc) ++ "called with incorrect type"
5347
5348 bvConPrim :: Type -> Term
5349 bvConPrim (tyView -> TyConApp bvTcNm _)
5350 = Prim (PrimInfo (showt 'Clash.Sized.Internal.BitVector.fromInteger#) (ForAllTy nTV funTy) WorkNever SingleResult NoUnfolding)
5351 where
5352 funTy = foldr1 mkFunTy [naturalPrimTy,naturalPrimTy,integerPrimTy,mkTyConApp bvTcNm [nVar]]
5353 nName = mkUnsafeSystemName "n" 0
5354 nVar = VarTy nTV
5355 nTV = mkTyVar typeNatKind nName
5356 bvConPrim _ = error $ $(curLoc) ++ "called with incorrect type"
5357
5358 indexConPrim :: Type -> Term
5359 indexConPrim (tyView -> TyConApp indexTcNm _)
5360 = Prim (PrimInfo (showt 'Clash.Sized.Internal.Index.fromInteger#) (ForAllTy nTV funTy) WorkNever SingleResult NoUnfolding)
5361 where
5362 funTy = foldr1 mkFunTy [naturalPrimTy,integerPrimTy,mkTyConApp indexTcNm [nVar]]
5363 nName = mkUnsafeSystemName "n" 0
5364 nVar = VarTy nTV
5365 nTV = mkTyVar typeNatKind nName
5366 indexConPrim _ = error $ $(curLoc) ++ "called with incorrect type"
5367
5368 signedConPrim :: Type -> Term
5369 signedConPrim (tyView -> TyConApp signedTcNm _)
5370 = Prim (PrimInfo (showt 'Clash.Sized.Internal.Signed.fromInteger#) (ForAllTy nTV funTy) WorkNever SingleResult NoUnfolding)
5371 where
5372 funTy = foldr1 mkFunTy [naturalPrimTy,integerPrimTy,mkTyConApp signedTcNm [nVar]]
5373 nName = mkUnsafeSystemName "n" 0
5374 nVar = VarTy nTV
5375 nTV = mkTyVar typeNatKind nName
5376 signedConPrim _ = error $ $(curLoc) ++ "called with incorrect type"
5377
5378 unsignedConPrim :: Type -> Term
5379 unsignedConPrim (tyView -> TyConApp unsignedTcNm _)
5380 = Prim (PrimInfo (showt 'Clash.Sized.Internal.Unsigned.fromInteger#) (ForAllTy nTV funTy) WorkNever SingleResult NoUnfolding)
5381 where
5382 funTy = foldr1 mkFunTy [naturalPrimTy,integerPrimTy,mkTyConApp unsignedTcNm [nVar]]
5383 nName = mkUnsafeSystemName "n" 0
5384 nVar = VarTy nTV
5385 nTV = mkTyVar typeNatKind nName
5386 unsignedConPrim _ = error $ $(curLoc) ++ "called with incorrect type"
5387
5388
5389 -- | Lift a binary function over 'Unsigned' values to be used as literal Evaluator
5390 --
5391 --
5392 liftUnsigned2 :: KnownNat n
5393 => (Unsigned n -> Unsigned n -> Unsigned n)
5394 -> Type
5395 -> TyConMap
5396 -> [Type]
5397 -> [Value]
5398 -> (Proxy n -> Maybe Term)
5399 liftUnsigned2 = liftSized2 unsignedLiterals' mkUnsignedLit
5400
5401 liftSigned2 :: KnownNat n
5402 => (Signed n -> Signed n -> Signed n)
5403 -> Type
5404 -> TyConMap
5405 -> [Type]
5406 -> [Value]
5407 -> (Proxy n -> Maybe Term)
5408 liftSigned2 = liftSized2 signedLiterals' mkSignedLit
5409
5410 liftBitVector2 :: KnownNat n
5411 => (BitVector n -> BitVector n -> BitVector n)
5412 -> Type
5413 -> TyConMap
5414 -> [Type]
5415 -> [Value]
5416 -> (Proxy n -> Maybe Term)
5417 liftBitVector2 f ty tcm tys args _p
5418 | Just (nTy, kn) <- extractKnownNat tcm tys
5419 , [i,j] <- bitVectorLiterals' args
5420 = let BV mask val = f (toBV i) (toBV j)
5421 in Just $ mkBitVectorLit ty nTy kn (toInteger mask) (toInteger val)
5422 | otherwise = Nothing
5423
5424 liftBitVector2Bool :: KnownNat n
5425 => (BitVector n -> BitVector n -> Bool)
5426 -> Type
5427 -> TyConMap
5428 -> [Value]
5429 -> (Proxy n -> Maybe Term)
5430 liftBitVector2Bool f ty tcm args _p
5431 | [i,j] <- bitVectorLiterals' args
5432 = let val = f (toBV i) (toBV j)
5433 in Just $ boolToBoolLiteral tcm ty val
5434 | otherwise = Nothing
5435
5436 liftInteger2BitVector
5437 :: KnownNat n
5438 => (Integer -> BitVector n)
5439 -> (Type, Type, Integer)
5440 -> [Value]
5441 -> (Proxy n -> Maybe Term)
5442 liftInteger2BitVector f resTyInfo args _p
5443 | [i] <- intCLiterals' args
5444 = let BV msk val = f i
5445 in Just (mkBitVectorLit' resTyInfo (toInteger msk) (toInteger val))
5446
5447 | otherwise
5448 = Nothing
5449
5450 liftBitVector2CInt
5451 :: KnownNat n
5452 => TyConMap
5453 -> Type
5454 -> (BitVector n -> Integer)
5455 -> [Value]
5456 -> (Proxy n -> Maybe Term)
5457 liftBitVector2CInt tcm resTy f args _p
5458 | [i] <- bitVectorLiterals' args
5459 = let val = f (toBV i)
5460 in Just $ mkIntCLit tcm val resTy
5461 | otherwise
5462 = Nothing
5463
5464 liftSized2 :: (KnownNat n, Integral (sized n))
5465 => ([Value] -> [Integer])
5466 -- ^ literal argument extraction function
5467 -> (Type -> Type -> Integer -> Integer -> Term)
5468 -- ^ literal contruction function
5469 -> (sized n -> sized n -> sized n)
5470 -> Type
5471 -> TyConMap
5472 -> [Type]
5473 -> [Value]
5474 -> (Proxy n -> Maybe Term)
5475 liftSized2 extractLitArgs mkLit f ty tcm tys args p
5476 | Just (nTy, kn) <- extractKnownNat tcm tys
5477 , [i,j] <- extractLitArgs args
5478 = let val = runSizedF f i j p
5479 in Just $ mkLit ty nTy kn val
5480 | otherwise = Nothing
5481
5482 -- | Helper to run a function over sized types on integers
5483 --
5484 -- This only works on function of type (sized n -> sized n -> sized n)
5485 -- The resulting function must be executed with reifyNat
5486 runSizedF
5487 :: (KnownNat n, Integral (sized n))
5488 => (sized n -> sized n -> sized n)
5489 -- ^ function to run
5490 -> Integer
5491 -- ^ first argument
5492 -> Integer
5493 -- ^ second argument
5494 -> (Proxy n -> Integer)
5495 runSizedF f i j _ = toInteger $ f (fromInteger i) (fromInteger j)
5496
5497 extractTySizeInfo :: TyConMap -> Type -> [Type] -> (Type, Type, Integer)
5498 extractTySizeInfo tcm ty tys = (resTy,resSizeTy,resSize)
5499 where
5500 ty' = piResultTys tcm ty tys
5501 (_,resTy) = splitFunForallTy ty'
5502 TyConApp _ [resSizeTy] = tyView resTy
5503 Right resSize = runExcept (tyNatSize tcm resSizeTy)
5504
5505 getResultTy
5506 :: TyConMap
5507 -> Type
5508 -> [Type]
5509 -> Type
5510 getResultTy tcm ty tys = resTy
5511 where
5512 ty' = piResultTys tcm ty tys
5513 (_,resTy) = splitFunForallTy ty'
5514
5515 liftDDI :: (Double# -> Double# -> Int#) -> [Value] -> Maybe Term
5516 liftDDI f args = case doubleLiterals' args of
5517 [i,j] -> Just $ runDDI f i j
5518 _ -> Nothing
5519 liftDDD :: (Double# -> Double# -> Double#) -> [Value] -> Maybe Term
5520 liftDDD f args = case doubleLiterals' args of
5521 [i,j] -> Just $ runDDD f i j
5522 _ -> Nothing
5523 liftDD :: (Double# -> Double#) -> [Value] -> Maybe Term
5524 liftDD f args = case doubleLiterals' args of
5525 [i] -> Just $ runDD f i
5526 _ -> Nothing
5527 runDDI :: (Double# -> Double# -> Int#) -> Word64 -> Word64 -> Term
5528 runDDI f i j
5529 = let !(D# a) = wordToDouble i
5530 !(D# b) = wordToDouble j
5531 r = f a b
5532 in Literal . IntLiteral . toInteger $ I# r
5533 runDDD :: (Double# -> Double# -> Double#) -> Word64 -> Word64 -> Term
5534 runDDD f i j
5535 = let !(D# a) = wordToDouble i
5536 !(D# b) = wordToDouble j
5537 r = f a b
5538 in Literal . DoubleLiteral . doubleToWord $ D# r
5539 runDD :: (Double# -> Double#) -> Word64 -> Term
5540 runDD f i
5541 = let !(D# a) = wordToDouble i
5542 r = f a
5543 in Literal . DoubleLiteral . doubleToWord $ D# r
5544
5545 liftFFI :: (Float# -> Float# -> Int#) -> [Value] -> Maybe Term
5546 liftFFI f args = case floatLiterals' args of
5547 [i,j] -> Just $ runFFI f i j
5548 _ -> Nothing
5549 liftFFF :: (Float# -> Float# -> Float#) -> [Value] -> Maybe Term
5550 liftFFF f args = case floatLiterals' args of
5551 [i,j] -> Just $ runFFF f i j
5552 _ -> Nothing
5553 liftFF :: (Float# -> Float#) -> [Value] -> Maybe Term
5554 liftFF f args = case floatLiterals' args of
5555 [i] -> Just $ runFF f i
5556 _ -> Nothing
5557 runFFI :: (Float# -> Float# -> Int#) -> Word32 -> Word32 -> Term
5558 runFFI f i j
5559 = let !(F# a) = wordToFloat i
5560 !(F# b) = wordToFloat j
5561 r = f a b
5562 in Literal . IntLiteral . toInteger $ I# r
5563 runFFF :: (Float# -> Float# -> Float#) -> Word32 -> Word32 -> Term
5564 runFFF f i j
5565 = let !(F# a) = wordToFloat i
5566 !(F# b) = wordToFloat j
5567 r = f a b
5568 in Literal . FloatLiteral . floatToWord $ F# r
5569 runFF :: (Float# -> Float#) -> Word32 -> Term
5570 runFF f i
5571 = let !(F# a) = wordToFloat i
5572 r = f a
5573 in Literal . FloatLiteral . floatToWord $ F# r
5574
5575 #if MIN_VERSION_base(4,16,0)
5576 liftI8 :: (Int8# -> Int8# -> Int8#) -> [Value] -> Maybe Term
5577 liftI8 f args = case int8Literals' args of
5578 [i,j] ->
5579 let !(I8# a) = fromInteger i
5580 !(I8# b) = fromInteger j
5581 in Just (Literal (Int8Literal (toInteger (I8# (f a b)))))
5582 _ -> Nothing
5583
5584 liftI8I :: (Int8# -> Int# -> Int8#) -> [Value] -> Maybe Term
5585 liftI8I f args = case args of
5586 [Lit (Int8Literal i),Lit (IntLiteral j)] ->
5587 let !(I8# a) = fromInteger i
5588 !(I# b) = fromInteger j
5589 in Just (Literal (Int8Literal (toInteger (I8# (f a b)))))
5590 _ -> Nothing
5591
5592 liftI8RI :: (Int8# -> Int8# -> Int#) -> [Value] -> Maybe Term
5593 liftI8RI f args = case int8Literals' args of
5594 [i,j] ->
5595 let !(I8# a) = fromInteger i
5596 !(I8# b) = fromInteger j
5597 in Just (Literal (IntLiteral (toInteger (I# (f a b)))))
5598 _ -> Nothing
5599
5600 liftI16 :: (Int16# -> Int16# -> Int16#) -> [Value] -> Maybe Term
5601 liftI16 f args = case int16Literals' args of
5602 [i,j] ->
5603 let !(I16# a) = fromInteger i
5604 !(I16# b) = fromInteger j
5605 in Just (Literal (Int16Literal (toInteger (I16# (f a b)))))
5606 _ -> Nothing
5607
5608 liftI16I :: (Int16# -> Int# -> Int16#) -> [Value] -> Maybe Term
5609 liftI16I f args = case args of
5610 [Lit (Int16Literal i),Lit (IntLiteral j)] ->
5611 let !(I16# a) = fromInteger i
5612 !(I# b) = fromInteger j
5613 in Just (Literal (Int16Literal (toInteger (I16# (f a b)))))
5614 _ -> Nothing
5615
5616 liftI16RI :: (Int16# -> Int16# -> Int#) -> [Value] -> Maybe Term
5617 liftI16RI f args = case int16Literals' args of
5618 [i,j] ->
5619 let !(I16# a) = fromInteger i
5620 !(I16# b) = fromInteger j
5621 in Just (Literal (IntLiteral (toInteger (I# (f a b)))))
5622 _ -> Nothing
5623
5624 liftI32 :: (Int32# -> Int32# -> Int32#) -> [Value] -> Maybe Term
5625 liftI32 f args = case int32Literals' args of
5626 [i,j] ->
5627 let !(I32# a) = fromInteger i
5628 !(I32# b) = fromInteger j
5629 in Just (Literal (Int32Literal (toInteger (I32# (f a b)))))
5630 _ -> Nothing
5631
5632 liftI32I :: (Int32# -> Int# -> Int32#) -> [Value] -> Maybe Term
5633 liftI32I f args = case args of
5634 [Lit (Int32Literal i),Lit (IntLiteral j)] ->
5635 let !(I32# a) = fromInteger i
5636 !(I# b) = fromInteger j
5637 in Just (Literal (Int32Literal (toInteger (I32# (f a b)))))
5638 _ -> Nothing
5639
5640 liftI32RI :: (Int32# -> Int32# -> Int#) -> [Value] -> Maybe Term
5641 liftI32RI f args = case int32Literals' args of
5642 [i,j] ->
5643 let !(I32# a) = fromInteger i
5644 !(I32# b) = fromInteger j
5645 in Just (Literal (IntLiteral (toInteger (I# (f a b)))))
5646 _ -> Nothing
5647
5648 #if MIN_VERSION_base(4,17,0)
5649 liftI64 :: (Int64# -> Int64# -> Int64#) -> [Value] -> Maybe Term
5650 liftI64 f args = case int64Literals' args of
5651 [i,j] ->
5652 let !(I64# a) = fromInteger i
5653 !(I64# b) = fromInteger j
5654 in Just (Literal (Int64Literal (toInteger (I64# (f a b)))))
5655 _ -> Nothing
5656
5657 liftI64I :: (Int64# -> Int# -> Int64#) -> [Value] -> Maybe Term
5658 liftI64I f args = case args of
5659 [Lit (Int64Literal i),Lit (IntLiteral j)] ->
5660 let !(I64# a) = fromInteger i
5661 !(I# b) = fromInteger j
5662 in Just (Literal (Int64Literal (toInteger (I64# (f a b)))))
5663 _ -> Nothing
5664
5665 liftI64RI :: (Int64# -> Int64# -> Int#) -> [Value] -> Maybe Term
5666 liftI64RI f args = case int64Literals' args of
5667 [i,j] ->
5668 let !(I64# a) = fromInteger i
5669 !(I64# b) = fromInteger j
5670 in Just (Literal (IntLiteral (toInteger (I# (f a b)))))
5671 _ -> Nothing
5672 #endif
5673
5674 liftW8 :: (Word8# -> Word8# -> Word8#) -> [Value] -> Maybe Term
5675 liftW8 f args = case word8Literals' args of
5676 [i,j] ->
5677 let !(W8# a) = fromInteger i
5678 !(W8# b) = fromInteger j
5679 in Just (Literal (Word8Literal (toInteger (W8# (f a b)))))
5680 _ -> Nothing
5681
5682 liftW8I :: (Word8# -> Int# -> Word8#) -> [Value] -> Maybe Term
5683 liftW8I f args = case args of
5684 [Lit (Word8Literal i),Lit (IntLiteral j)] ->
5685 let !(W8# a) = fromInteger i
5686 !(I# b) = fromInteger j
5687 in Just (Literal (Word8Literal (toInteger (W8# (f a b)))))
5688 _ -> Nothing
5689
5690 liftW8RI :: (Word8# -> Word8# -> Int#) -> [Value] -> Maybe Term
5691 liftW8RI f args = case word8Literals' args of
5692 [i,j] ->
5693 let !(W8# a) = fromInteger i
5694 !(W8# b) = fromInteger j
5695 in Just (Literal (IntLiteral (toInteger (I# (f a b)))))
5696 _ -> Nothing
5697
5698 liftW16 :: (Word16# -> Word16# -> Word16#) -> [Value] -> Maybe Term
5699 liftW16 f args = case word16Literals' args of
5700 [i,j] -> let !(W16# a) = fromInteger i
5701 !(W16# b) = fromInteger j
5702 in Just (Literal (Word16Literal (toInteger (W16# (f a b)))))
5703 _ -> Nothing
5704
5705 liftW16I :: (Word16# -> Int# -> Word16#) -> [Value] -> Maybe Term
5706 liftW16I f args = case args of
5707 [Lit (Word16Literal i),Lit (IntLiteral j)] ->
5708 let !(W16# a) = fromInteger i
5709 !(I# b) = fromInteger j
5710 in Just (Literal (Word16Literal (toInteger (W16# (f a b)))))
5711 _ -> Nothing
5712
5713 liftW16RI :: (Word16# -> Word16# -> Int#) -> [Value] -> Maybe Term
5714 liftW16RI f args = case word16Literals' args of
5715 [i,j] ->
5716 let !(W16# a) = fromInteger i
5717 !(W16# b) = fromInteger j
5718 in Just (Literal (IntLiteral (toInteger (I# (f a b)))))
5719 _ -> Nothing
5720
5721 liftW32 :: (Word32# -> Word32# -> Word32#) -> [Value] -> Maybe Term
5722 liftW32 f args = case word32Literals' args of
5723 [i,j] -> let !(W32# a) = fromInteger i
5724 !(W32# b) = fromInteger j
5725 in Just (Literal (Word32Literal (toInteger (W32# (f a b)))))
5726 _ -> Nothing
5727
5728 liftW32I :: (Word32# -> Int# -> Word32#) -> [Value] -> Maybe Term
5729 liftW32I f args = case args of
5730 [Lit (Word32Literal i),Lit (IntLiteral j)] ->
5731 let !(W32# a) = fromInteger i
5732 !(I# b) = fromInteger j
5733 in Just (Literal (Word32Literal (toInteger (W32# (f a b)))))
5734 _ -> Nothing
5735
5736 liftW32RI :: (Word32# -> Word32# -> Int#) -> [Value] -> Maybe Term
5737 liftW32RI f args = case word32Literals' args of
5738 [i,j] ->
5739 let !(W32# a) = fromInteger i
5740 !(W32# b) = fromInteger j
5741 in Just (Literal (IntLiteral (toInteger (I# (f a b)))))
5742 _ -> Nothing
5743
5744 #if MIN_VERSION_base(4,17,0)
5745 liftW64 :: (Word64# -> Word64# -> Word64#) -> [Value] -> Maybe Term
5746 liftW64 f args = case word64Literals' args of
5747 [i,j] -> let !(W64# a) = fromInteger i
5748 !(W64# b) = fromInteger j
5749 in Just (Literal (Word64Literal (toInteger (W64# (f a b)))))
5750 _ -> Nothing
5751
5752 liftW64I :: (Word64# -> Int# -> Word64#) -> [Value] -> Maybe Term
5753 liftW64I f args = case args of
5754 [Lit (Word64Literal i),Lit (IntLiteral j)] ->
5755 let !(W64# a) = fromInteger i
5756 !(I# b) = fromInteger j
5757 in Just (Literal (Word64Literal (toInteger (W64# (f a b)))))
5758 _ -> Nothing
5759
5760 liftW64RI :: (Word64# -> Word64# -> Int#) -> [Value] -> Maybe Term
5761 liftW64RI f args = case word64Literals' args of
5762 [i,j] ->
5763 let !(W64# a) = fromInteger i
5764 !(W64# b) = fromInteger j
5765 in Just (Literal (IntLiteral (toInteger (I# (f a b)))))
5766 _ -> Nothing
5767 #endif
5768 #endif
5769
5770 splitAtPrim
5771 :: TyConName
5772 -- ^ SNat TyCon name
5773 -> TyConName
5774 -- ^ Vec TyCon name
5775 -> Term
5776 splitAtPrim snatTcNm vecTcNm =
5777 Prim (PrimInfo (showt 'Clash.Sized.Vector.splitAt) (splitAtTy snatTcNm vecTcNm) WorkNever SingleResult NoUnfolding)
5778
5779 splitAtTy
5780 :: TyConName
5781 -- ^ SNat TyCon name
5782 -> TyConName
5783 -- ^ Vec TyCon name
5784 -> Type
5785 splitAtTy snatNm vecNm =
5786 ForAllTy mTV (
5787 ForAllTy nTV (
5788 ForAllTy aTV (
5789 mkFunTy
5790 (mkTyConApp snatNm [VarTy mTV])
5791 (mkFunTy
5792 (mkTyConApp vecNm
5793 [mkTyConApp typeNatAdd
5794 [VarTy mTV
5795 ,VarTy nTV]
5796 ,VarTy aTV])
5797 (mkTyConApp tupNm
5798 [mkTyConApp vecNm
5799 [VarTy mTV
5800 ,VarTy aTV]
5801 ,mkTyConApp vecNm
5802 [VarTy nTV
5803 ,VarTy aTV]])))))
5804 where
5805 mTV = mkTyVar typeNatKind (mkUnsafeSystemName "m" 0)
5806 nTV = mkTyVar typeNatKind (mkUnsafeSystemName "n" 1)
5807 aTV = mkTyVar liftedTypeKind (mkUnsafeSystemName "a" 2)
5808 tupNm = ghcTyconToTyConName (tupleTyCon Boxed 2)
5809
5810 foldSplitAtTy
5811 :: TyConName
5812 -- ^ Vec TyCon name
5813 -> Type
5814 foldSplitAtTy vecNm =
5815 ForAllTy mTV (
5816 ForAllTy nTV (
5817 ForAllTy aTV (
5818 mkFunTy
5819 naturalPrimTy
5820 (mkFunTy
5821 (mkTyConApp vecNm
5822 [mkTyConApp typeNatAdd
5823 [VarTy mTV
5824 ,VarTy nTV]
5825 ,VarTy aTV])
5826 (mkTyConApp tupNm
5827 [mkTyConApp vecNm
5828 [VarTy mTV
5829 ,VarTy aTV]
5830 ,mkTyConApp vecNm
5831 [VarTy nTV
5832 ,VarTy aTV]])))))
5833 where
5834 mTV = mkTyVar typeNatKind (mkUnsafeSystemName "m" 0)
5835 nTV = mkTyVar typeNatKind (mkUnsafeSystemName "n" 1)
5836 aTV = mkTyVar liftedTypeKind (mkUnsafeSystemName "a" 2)
5837 tupNm = ghcTyconToTyConName (tupleTyCon Boxed 2)
5838
5839 vecAppendPrim
5840 :: TyConName
5841 -- ^ Vec TyCon name
5842 -> Term
5843 vecAppendPrim vecNm =
5844 Prim (PrimInfo (showt '(Clash.Sized.Vector.++)) (vecAppendTy vecNm) WorkNever SingleResult NoUnfolding)
5845
5846 vecAppendTy
5847 :: TyConName
5848 -- ^ Vec TyCon name
5849 -> Type
5850 vecAppendTy vecNm =
5851 ForAllTy nTV (
5852 ForAllTy aTV (
5853 ForAllTy mTV (
5854 mkFunTy
5855 (mkTyConApp vecNm [VarTy nTV
5856 ,VarTy aTV
5857 ])
5858 (mkFunTy
5859 (mkTyConApp vecNm [VarTy mTV
5860 ,VarTy aTV
5861 ])
5862 (mkTyConApp vecNm [mkTyConApp typeNatAdd
5863 [VarTy nTV
5864 ,VarTy mTV]
5865 ,VarTy aTV
5866 ])))))
5867 where
5868 nTV = mkTyVar typeNatKind (mkUnsafeSystemName "n" 0)
5869 aTV = mkTyVar liftedTypeKind (mkUnsafeSystemName "a" 1)
5870 mTV = mkTyVar typeNatKind (mkUnsafeSystemName "m" 2)
5871
5872 vecZipWithPrim
5873 :: TyConName
5874 -- ^ Vec TyCon name
5875 -> Term
5876 vecZipWithPrim vecNm =
5877 Prim (PrimInfo (showt 'Clash.Sized.Vector.zipWith) (vecZipWithTy vecNm) WorkNever SingleResult NoUnfolding)
5878
5879 vecZipWithTy
5880 :: TyConName
5881 -- ^ Vec TyCon name
5882 -> Type
5883 vecZipWithTy vecNm =
5884 ForAllTy aTV (
5885 ForAllTy bTV (
5886 ForAllTy cTV (
5887 ForAllTy nTV (
5888 mkFunTy
5889 (mkFunTy aTy (mkFunTy bTy cTy))
5890 (mkFunTy
5891 (mkTyConApp vecNm [nTy,aTy])
5892 (mkFunTy
5893 (mkTyConApp vecNm [nTy,bTy])
5894 (mkTyConApp vecNm [nTy,cTy])))))))
5895 where
5896 aTV = mkTyVar liftedTypeKind (mkUnsafeSystemName "a" 0)
5897 bTV = mkTyVar liftedTypeKind (mkUnsafeSystemName "b" 1)
5898 cTV = mkTyVar liftedTypeKind (mkUnsafeSystemName "c" 2)
5899 nTV = mkTyVar typeNatKind (mkUnsafeSystemName "n" 3)
5900 aTy = VarTy aTV
5901 bTy = VarTy bTV
5902 cTy = VarTy cTV
5903 nTy = VarTy nTV
5904
5905 vecImapGoTy
5906 :: TyConName
5907 -- ^ Vec TyCon name
5908 -> TyConName
5909 -- ^ Index TyCon name
5910 -> Type
5911 vecImapGoTy vecTcNm indexTcNm =
5912 ForAllTy nTV (
5913 ForAllTy mTV (
5914 ForAllTy aTV (
5915 ForAllTy bTV (
5916 mkFunTy fTy
5917 (mkFunTy vecATy (mkFunTy indexTy vecBTy))))))
5918 where
5919 nTV = mkTyVar typeNatKind (mkUnsafeSystemName "n" 0)
5920 mTV = mkTyVar typeNatKind (mkUnsafeSystemName "m" 1)
5921 aTV = mkTyVar liftedTypeKind (mkUnsafeSystemName "a" 2)
5922 bTV = mkTyVar liftedTypeKind (mkUnsafeSystemName "b" 3)
5923 indexTy = mkTyConApp indexTcNm [nTy]
5924 nTy = VarTy nTV
5925 mTy = VarTy mTV
5926 fTy = mkFunTy indexTy (mkFunTy aTy bTy)
5927 aTy = VarTy aTV
5928 bTy = VarTy bTV
5929 vecATy = mkTyConApp vecTcNm [mTy,aTy]
5930 vecBTy = mkTyConApp vecTcNm [mTy,bTy]
5931
5932 indexAddTy
5933 :: TyConName
5934 -- ^ Index TyCon name
5935 -> Type
5936 indexAddTy indexTcNm =
5937 ForAllTy nTV (
5938 mkFunTy naturalPrimTy (mkFunTy indexTy (mkFunTy indexTy indexTy)))
5939 where
5940 nTV = mkTyVar typeNatKind (mkUnsafeSystemName "n" 0)
5941 indexTy = mkTyConApp indexTcNm [VarTy nTV]
5942
5943 bvAppendPrim
5944 :: TyConName
5945 -- ^ BitVector TyCon Name
5946 -> Term
5947 bvAppendPrim bvTcNm =
5948 Prim (PrimInfo (showt '(Clash.Sized.Internal.BitVector.++#)) (bvAppendTy bvTcNm) WorkNever SingleResult NoUnfolding)
5949
5950 bvAppendTy
5951 :: TyConName
5952 -- ^ BitVector TyCon Name
5953 -> Type
5954 bvAppendTy bvNm =
5955 ForAllTy mTV (
5956 ForAllTy nTV (
5957 mkFunTy naturalPrimTy (mkFunTy
5958 (mkTyConApp bvNm [VarTy nTV])
5959 (mkFunTy
5960 (mkTyConApp bvNm [VarTy mTV])
5961 (mkTyConApp bvNm [mkTyConApp typeNatAdd
5962 [VarTy nTV
5963 ,VarTy mTV]])))))
5964 where
5965 mTV = mkTyVar typeNatKind (mkUnsafeSystemName "m" 0)
5966 nTV = mkTyVar typeNatKind (mkUnsafeSystemName "n" 1)
5967
5968 bvSplitPrim
5969 :: TyConName
5970 -- ^ BitVector TyCon Name
5971 -> Term
5972 bvSplitPrim bvTcNm =
5973 Prim (PrimInfo (showt 'Clash.Sized.Internal.BitVector.split#) (bvSplitTy bvTcNm) WorkNever SingleResult NoUnfolding)
5974
5975 bvSplitTy
5976 :: TyConName
5977 -- ^ BitVector TyCon Name
5978 -> Type
5979 bvSplitTy bvNm =
5980 ForAllTy nTV (
5981 ForAllTy mTV (
5982 mkFunTy naturalPrimTy (mkFunTy
5983 (mkTyConApp bvNm [mkTyConApp typeNatAdd
5984 [VarTy mTV
5985 ,VarTy nTV]])
5986 (mkTyConApp tupNm [mkTyConApp bvNm [VarTy mTV]
5987 ,mkTyConApp bvNm [VarTy nTV]]))))
5988 where
5989 nTV = mkTyVar typeNatKind (mkUnsafeSystemName "n" 0)
5990 mTV = mkTyVar typeNatKind (mkUnsafeSystemName "m" 1)
5991 tupNm = ghcTyconToTyConName (tupleTyCon Boxed 2)
5992
5993 ghcTyconToTyConName
5994 :: TyCon.TyCon
5995 -> TyConName
5996 ghcTyconToTyConName tc =
5997 Name User n' (fromGhcUnique (TyCon.tyConUnique tc)) (getSrcSpan n)
5998 where
5999 n' = fromMaybe "_INTERNAL_" (modNameM n) `Text.append`
6000 ('.' `Text.cons` Text.pack occName)
6001 occName = occNameString $ nameOccName n
6002 n = TyCon.tyConName tc
6003
6004 svoid :: (State# RealWorld -> State# RealWorld) -> IO ()
6005 svoid m0 = IO (\s -> case m0 s of s' -> (# s', () #))
6006
6007 isTrueDC,isFalseDC :: DataCon -> Bool
6008 isTrueDC dc = dcUniq dc == fromGhcUnique trueDataConKey
6009 isFalseDC dc = dcUniq dc == fromGhcUnique falseDataConKey