Merge git://github.com/darchon/clash into cλash
[matthijs/master-project/cλash.git] / NormalizeTypes.hs
1 {-# LANGUAGE TemplateHaskell #-}
2 module 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 CoreShow
22 import Pretty
23
24 data TransformState = TransformState {
25     tsUniqSupply_ :: UniqSupply.UniqSupply
26   , tsBindings_ :: Map.Map CoreBndr CoreExpr
27   , tsNormalized_ :: VarSet.VarSet -- ^ The binders that have been normalized
28 }
29
30 $( Data.Accessor.Template.deriveAccessors ''TransformState )
31
32 -- A session of multiple transformations over multiple expressions
33 type TransformSession = (State.State TransformState)
34 -- Wrap a writer around a TransformSession, to run a single transformation
35 -- over a single expression and track if the expression was changed.
36 type TransformMonad = Writer.WriterT Monoid.Any TransformSession
37
38 -- | Transforms a CoreExpr and keeps track if it has changed.
39 type Transform = CoreExpr -> TransformMonad CoreExpr
40
41 -- Finds the value of a global binding, if available
42 getGlobalBind :: CoreBndr -> TransformSession (Maybe CoreExpr)
43 getGlobalBind bndr = do
44   bindings <- getA tsBindings
45   return $ Map.lookup bndr bindings 
46
47 -- Adds a new global binding with the given value
48 addGlobalBind :: CoreBndr -> CoreExpr -> TransformSession ()
49 addGlobalBind bndr expr = modA tsBindings (Map.insert bndr expr)
50
51 -- Returns a list of all global binders
52 getGlobalBinders :: TransformSession [CoreBndr]
53 getGlobalBinders = do
54   bindings <- getA tsBindings
55   return $ Map.keys bindings