2 -- Simple module providing some types used by Translator. These are in a
3 -- separate module to prevent circular dependencies in Pretty for example.
5 {-# LANGUAGE TemplateHaskell #-}
6 module CLasH.Translator.TranslatorTypes where
9 import qualified Control.Monad.Trans.State as State
10 import qualified Data.Map as Map
11 import qualified Data.Accessor.Template
16 import qualified CoreSyn
18 import qualified HscTypes
19 import qualified UniqSupply
22 import qualified Language.VHDL.AST as AST
25 import CLasH.VHDL.VHDLTypes
26 import CLasH.Translator.Annotations
28 -- | A specification of an entity we can generate VHDL for. Consists of the
29 -- binder of the top level entity, an optional initial state and an optional
31 type EntitySpec = (Maybe CoreSyn.CoreBndr, Maybe [(CoreSyn.CoreBndr, CoreSyn.CoreBndr)], Maybe CoreSyn.CoreExpr)
33 -- | A function that knows which parts of a module to compile
35 HscTypes.CoreModule -- ^ The module to look at
36 -> GHC.Ghc [EntitySpec]
38 -----------------------------------------------------------------------------
39 -- The TranslatorSession
40 -----------------------------------------------------------------------------
42 -- A orderable equivalent of CoreSyn's Type for use as a map key
43 newtype OrdType = OrdType Type.Type
44 instance Eq OrdType where
45 (OrdType a) == (OrdType b) = Type.tcEqType a b
46 instance Ord OrdType where
47 compare (OrdType a) (OrdType b) = Type.tcCmpType a b
49 data HType = AggrType String [HType] |
50 EnumType String [String] |
58 deriving (Eq, Ord, Show)
60 -- A map of a Core type to the corresponding type name, or Nothing when the
61 -- type would be empty.
62 type TypeMapRec = Maybe (AST.VHDLId, Maybe (Either AST.TypeDef AST.SubtypeIn))
63 type TypeMap = Map.Map HType TypeMapRec
65 -- A map of a vector Core element type and function name to the coressponding
66 -- VHDLId of the function and the function body.
67 type TypeFunMap = Map.Map (HType, String) (AST.VHDLId, AST.SubProgBody)
69 type TfpIntMap = Map.Map OrdType Int
70 -- A substate that deals with type generation
71 data TypeState = TypeState {
72 -- | A map of Core type -> VHDL Type
74 -- | A list of type declarations
75 tsTypeDecls_ :: [Maybe AST.PackageDecItem],
76 -- | A map of vector Core type -> VHDL type function
77 tsTypeFuns_ :: TypeFunMap,
78 tsTfpInts_ :: TfpIntMap,
79 tsHscEnv_ :: HscTypes.HscEnv
83 $( Data.Accessor.Template.deriveAccessors ''TypeState )
86 type TypeSession = State.State TypeState
87 -- A global state for the translator
88 data TranslatorState = TranslatorState {
89 tsUniqSupply_ :: UniqSupply.UniqSupply
90 , tsType_ :: TypeState
91 , tsBindings_ :: Map.Map CoreSyn.CoreBndr CoreSyn.CoreExpr
92 , tsNormalized_ :: Map.Map CoreSyn.CoreBndr CoreSyn.CoreExpr
93 , tsEntityCounter_ :: Integer
94 , tsEntities_ :: Map.Map CoreSyn.CoreBndr Entity
95 , tsArchitectures_ :: Map.Map CoreSyn.CoreBndr (Architecture, [CoreSyn.CoreBndr])
96 , tsInitStates_ :: Map.Map CoreSyn.CoreBndr CoreSyn.CoreBndr
100 $( Data.Accessor.Template.deriveAccessors ''TranslatorState )
102 type TranslatorSession = State.State TranslatorState
104 -----------------------------------------------------------------------------
106 -----------------------------------------------------------------------------
108 -- Does the given binder reference a top level binder in the current
110 isTopLevelBinder :: CoreSyn.CoreBndr -> TranslatorSession Bool
111 isTopLevelBinder bndr = do
112 bindings <- getA tsBindings
113 return $ Map.member bndr bindings
115 -- Finds the value of a global binding, if available
116 getGlobalBind :: CoreSyn.CoreBndr -> TranslatorSession (Maybe CoreSyn.CoreExpr)
117 getGlobalBind bndr = do
118 bindings <- getA tsBindings
119 return $ Map.lookup bndr bindings
121 -- Adds a new global binding with the given value
122 addGlobalBind :: CoreSyn.CoreBndr -> CoreSyn.CoreExpr -> TranslatorSession ()
123 addGlobalBind bndr expr = modA tsBindings (Map.insert bndr expr)
125 -- Returns a list of all global binders
126 getGlobalBinders :: TranslatorSession [CoreSyn.CoreBndr]
127 getGlobalBinders = do
128 bindings <- getA tsBindings
129 return $ Map.keys bindings
131 -- vim: set ts=8 sw=2 sts=2 expandtab: