| 1 |
{-| |
| 2 |
Copyright : (C) 2020, QBayLogic B.V. |
| 3 |
License : BSD2 (see the file LICENSE) |
| 4 |
Maintainer : QBayLogic B.V. <[email protected]> |
| 5 |
|
| 6 |
Evaluation of primitive operations in the partial evaluator. This is used |
| 7 |
by the Clash.GHC.PartialEval.Eval module to implement fully applied primitives. |
| 8 |
-} |
| 9 |
|
| 10 |
module Clash.GHC.PartialEval.Primitive |
| 11 |
( evalPrimitive |
| 12 |
) where |
| 13 |
|
| 14 |
import Clash.Core.PartialEval.Monad |
| 15 |
import Clash.Core.PartialEval.NormalForm |
| 16 |
import Clash.Core.Term (Term, PrimInfo) |
| 17 |
|
| 18 |
-- | Evaluate a primitive with the given arguments. |
| 19 |
-- See NOTE [Evaluating primitives] for more information. |
| 20 |
-- |
| 21 |
evalPrimitive |
| 22 |
:: (Term -> Eval Value) |
| 23 |
-- ^ Evaluation function for forcing arguments |
| 24 |
-> PrimInfo |
| 25 |
-- ^ The primitive to evaluate |
| 26 |
-> Args Value |
| 27 |
-- ^ The arguments supplied to the primitive |
| 28 |
-> Eval Value |
| 29 |
-- ^ The result of evaluating the primitive |
| 30 |
evalPrimitive _eval pr args = |
| 31 |
-- TODO Implement evaluation of primitives. |
| 32 |
pure (VNeutral (NePrim pr args)) |
| 33 |
|
| 34 |
{- |
| 35 |
NOTE [Evaluating primitives] |
| 36 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 37 |
When the evaluator encounters a primitive operation with all arguments applied, |
| 38 |
it will attempt to evaluate it. If this is possible, the call to the primitive |
| 39 |
will be replaced with the result. However, it may not be possible to evaluate |
| 40 |
a primitive if not all arguments are statically known (i.e. if an argument is |
| 41 |
a variable with an unknown value). In this case, a neutral primitive is |
| 42 |
returned instead. |
| 43 |
|
| 44 |
Some primitives do not evaluate, and are deliberately preserved in the result |
| 45 |
of the evaluator as neutral primitives. Notable examples of this are |
| 46 |
|
| 47 |
* GHC.CString.unpackCString# |
| 48 |
* Clash.Sized.Internal.BitVector.fromInteger## |
| 49 |
* Clash.Sized.Internal.BitVector.fromInteger# |
| 50 |
* Clash.Sized.Internal.Index.fromInteger# |
| 51 |
* Clash.Sized.Internal.Signed.fromInteger# |
| 52 |
* Clash.Sized.Internal.Unsigned.fromInteger# |
| 53 |
|
| 54 |
Some primitives may throw exceptions (such as division by zero) or need to |
| 55 |
perform IO (e.g. primitives on ByteArray#). These effects are supported by the |
| 56 |
Eval monad, see Clash.Core.PartialEval.Monad. |
| 57 |
-} |