Remove NoGenerics pragma, as the ghc bug 3391 is now fixed
[matthijs/master-project/cλash.git] / cλash / CLasH / Normalize / NormalizeTypes.hs
1 {-# LANGUAGE TemplateHaskell #-}
2 module CLasH.Normalize.NormalizeTypes where
3
4
5 -- Standard modules
6 import qualified Control.Monad.Trans.Writer as Writer
7 import qualified Control.Monad.Trans.State as State
8 import qualified Data.Monoid as Monoid
9 import qualified Data.Accessor.Template
10 import Data.Accessor
11 import qualified Data.Map as Map
12 import Debug.Trace
13
14 -- GHC API
15 import CoreSyn
16 import qualified UniqSupply
17 import qualified VarSet
18 import Outputable ( Outputable, showSDoc, ppr )
19
20 -- Local imports
21 import CLasH.Utils.Core.CoreShow
22 import CLasH.Utils.Pretty
23 import CLasH.VHDL.VHDLTypes -- For TypeState
24
25 data TransformState = TransformState {
26     tsUniqSupply_ :: UniqSupply.UniqSupply
27   , tsBindings_ :: Map.Map CoreBndr CoreExpr
28   , tsNormalized_ :: VarSet.VarSet -- ^ The binders that have been normalized
29   , tsType_ :: TypeState
30 }
31
32 $( Data.Accessor.Template.deriveAccessors ''TransformState )
33
34 -- A session of multiple transformations over multiple expressions
35 type TransformSession = (State.State TransformState)
36 -- Wrap a writer around a TransformSession, to run a single transformation
37 -- over a single expression and track if the expression was changed.
38 type TransformMonad = Writer.WriterT Monoid.Any TransformSession
39
40 -- | Transforms a CoreExpr and keeps track if it has changed.
41 type Transform = CoreExpr -> TransformMonad CoreExpr
42
43 -- Finds the value of a global binding, if available
44 getGlobalBind :: CoreBndr -> TransformSession (Maybe CoreExpr)
45 getGlobalBind bndr = do
46   bindings <- getA tsBindings
47   return $ Map.lookup bndr bindings 
48
49 -- Adds a new global binding with the given value
50 addGlobalBind :: CoreBndr -> CoreExpr -> TransformSession ()
51 addGlobalBind bndr expr = modA tsBindings (Map.insert bndr expr)
52
53 -- Returns a list of all global binders
54 getGlobalBinders :: TransformSession [CoreBndr]
55 getGlobalBinders = do
56   bindings <- getA tsBindings
57   return $ Map.keys bindings