Keep (and show) a count of applied transformations.
[matthijs/master-project/cλash.git] / cλash / CLasH / Translator / TranslatorTypes.hs
1 {-# LANGUAGE TemplateHaskell #-}
2 --
3 -- Simple module providing some types used by Translator. These are in a
4 -- separate module to prevent circular dependencies in Pretty for example.
5 --
6 module CLasH.Translator.TranslatorTypes where
7
8 -- Standard modules
9 import qualified Control.Monad.Trans.State as State
10 import qualified Data.Map as Map
11 import qualified Data.Accessor.Template
12 import qualified Data.Accessor.Monad.Trans.State as MonadState
13
14 -- GHC API
15 import qualified GHC
16 import qualified CoreSyn
17 import qualified Type
18 import qualified HscTypes
19 import qualified UniqSupply
20
21 -- VHDL Imports
22 import qualified Language.VHDL.AST as AST
23
24 -- Local imports
25 import CLasH.VHDL.VHDLTypes
26
27 -- | A specification of an entity we can generate VHDL for. Consists of the
28 --   binder of the top level entity, an optional initial state and an optional
29 --   test input.
30 type EntitySpec = (Maybe CoreSyn.CoreBndr, Maybe [(CoreSyn.CoreBndr, CoreSyn.CoreBndr)], Maybe CoreSyn.CoreExpr)
31
32 -- | A function that knows which parts of a module to compile
33 type Finder =
34   HscTypes.CoreModule -- ^ The module to look at
35   -> GHC.Ghc [EntitySpec]
36
37 -----------------------------------------------------------------------------
38 -- The TranslatorSession
39 -----------------------------------------------------------------------------
40
41 -- A orderable equivalent of CoreSyn's Type for use as a map key
42 newtype OrdType = OrdType Type.Type
43 instance Eq OrdType where
44   (OrdType a) == (OrdType b) = Type.tcEqType a b
45 instance Ord OrdType where
46   compare (OrdType a) (OrdType b) = Type.tcCmpType a b
47
48 data HType = AggrType String [HType] |
49              EnumType String [String] |
50              VecType Int HType |
51              UVecType HType |
52              SizedWType Int |
53              RangedWType Int |
54              SizedIType Int |
55              BuiltinType String |
56              StateType
57   deriving (Eq, Ord, Show)
58
59 -- A map of a Core type to the corresponding type name, or Nothing when the
60 -- type would be empty.
61 type TypeMapRec   = Maybe (AST.VHDLId, Maybe (Either AST.TypeDef AST.SubtypeIn))
62 type TypeMap      = Map.Map HType TypeMapRec
63
64 -- A map of a vector Core element type and function name to the coressponding
65 -- VHDLId of the function and the function body.
66 type TypeFunMap = Map.Map (HType, String) (AST.VHDLId, AST.SubProgBody)
67
68 type TfpIntMap = Map.Map OrdType Int
69 -- A substate that deals with type generation
70 data TypeState = TypeState {
71   -- | A map of Core type -> VHDL Type
72   tsTypes_      :: TypeMap,
73   -- | A list of type declarations
74   tsTypeDecls_  :: [Maybe AST.PackageDecItem],
75   -- | A map of vector Core type -> VHDL type function
76   tsTypeFuns_   :: TypeFunMap,
77   tsTfpInts_    :: TfpIntMap,
78   tsHscEnv_     :: HscTypes.HscEnv
79 }
80
81 -- Derive accessors
82 Data.Accessor.Template.deriveAccessors ''TypeState
83
84 -- Define a session
85 type TypeSession = State.State TypeState
86 -- A global state for the translator
87 data TranslatorState = TranslatorState {
88     tsUniqSupply_ :: UniqSupply.UniqSupply
89   , tsType_ :: TypeState
90   , tsBindings_ :: Map.Map CoreSyn.CoreBndr CoreSyn.CoreExpr
91   , tsNormalized_ :: Map.Map CoreSyn.CoreBndr CoreSyn.CoreExpr
92   , tsEntityCounter_ :: Integer
93   , tsEntities_ :: Map.Map CoreSyn.CoreBndr Entity
94   , tsArchitectures_ :: Map.Map CoreSyn.CoreBndr (Architecture, [CoreSyn.CoreBndr])
95   , tsInitStates_ :: Map.Map CoreSyn.CoreBndr CoreSyn.CoreBndr
96   , tsTransformCounter_ :: Int -- ^ How many transformations were applied?
97 }
98
99 -- Derive accessors
100 Data.Accessor.Template.deriveAccessors ''TranslatorState
101
102 type TranslatorSession = State.State TranslatorState
103
104 -----------------------------------------------------------------------------
105 -- Some accessors
106 -----------------------------------------------------------------------------
107
108 -- Does the given binder reference a top level binder in the current
109 -- module(s)?
110 isTopLevelBinder :: CoreSyn.CoreBndr -> TranslatorSession Bool
111 isTopLevelBinder bndr = do
112   bindings <- MonadState.get tsBindings
113   return $ Map.member bndr bindings
114
115 -- Finds the value of a global binding, if available
116 getGlobalBind :: CoreSyn.CoreBndr -> TranslatorSession (Maybe CoreSyn.CoreExpr)
117 getGlobalBind bndr = do
118   bindings <- MonadState.get tsBindings
119   return $ Map.lookup bndr bindings 
120
121 -- Adds a new global binding with the given value
122 addGlobalBind :: CoreSyn.CoreBndr -> CoreSyn.CoreExpr -> TranslatorSession ()
123 addGlobalBind bndr expr = MonadState.modify tsBindings (Map.insert bndr expr)
124
125 -- Returns a list of all global binders
126 getGlobalBinders :: TranslatorSession [CoreSyn.CoreBndr]
127 getGlobalBinders = do
128   bindings <- MonadState.get tsBindings
129   return $ Map.keys bindings
130
131 -- vim: set ts=8 sw=2 sts=2 expandtab: