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
15 import qualified CoreSyn
17 import qualified HscTypes
18 import qualified UniqSupply
21 import qualified Language.VHDL.AST as AST
24 import CLasH.VHDL.VHDLTypes
26 -- A orderable equivalent of CoreSyn's Type for use as a map key
27 newtype OrdType = OrdType { getType :: Type.Type }
28 instance Eq OrdType where
29 (OrdType a) == (OrdType b) = Type.tcEqType a b
30 instance Ord OrdType where
31 compare (OrdType a) (OrdType b) = Type.tcCmpType a b
33 data HType = StdType OrdType |
34 ADTType String [HType] |
42 -- A map of a Core type to the corresponding type name
43 type TypeMap = Map.Map HType (AST.VHDLId, Either AST.TypeDef AST.SubtypeIn)
45 -- A map of a vector Core element type and function name to the coressponding
46 -- VHDLId of the function and the function body.
47 type TypeFunMap = Map.Map (OrdType, String) (AST.VHDLId, AST.SubProgBody)
49 type TfpIntMap = Map.Map OrdType Int
50 -- A substate that deals with type generation
51 data TypeState = TypeState {
52 -- | A map of Core type -> VHDL Type
54 -- | A list of type declarations
55 tsTypeDecls_ :: [AST.PackageDecItem],
56 -- | A map of vector Core type -> VHDL type function
57 tsTypeFuns_ :: TypeFunMap,
58 tsTfpInts_ :: TfpIntMap,
59 tsHscEnv_ :: HscTypes.HscEnv
63 $( Data.Accessor.Template.deriveAccessors ''TypeState )
66 type TypeSession = State.State TypeState
67 -- A global state for the translator
68 data TranslatorState = TranslatorState {
69 tsUniqSupply_ :: UniqSupply.UniqSupply
70 , tsType_ :: TypeState
71 , tsBindings_ :: Map.Map CoreSyn.CoreBndr CoreSyn.CoreExpr
72 , tsNormalized_ :: Map.Map CoreSyn.CoreBndr CoreSyn.CoreExpr
73 , tsEntities_ :: Map.Map CoreSyn.CoreBndr Entity
74 , tsArchitectures_ :: Map.Map CoreSyn.CoreBndr (Architecture, [CoreSyn.CoreBndr])
78 $( Data.Accessor.Template.deriveAccessors ''TranslatorState )
80 type TranslatorSession = State.State TranslatorState
82 -----------------------------------------------------------------------------
84 -----------------------------------------------------------------------------
86 -- Does the given binder reference a top level binder in the current
88 isTopLevelBinder :: CoreSyn.CoreBndr -> TranslatorSession Bool
89 isTopLevelBinder bndr = do
90 bindings <- getA tsBindings
91 return $ Map.member bndr bindings
93 -- Finds the value of a global binding, if available
94 getGlobalBind :: CoreSyn.CoreBndr -> TranslatorSession (Maybe CoreSyn.CoreExpr)
95 getGlobalBind bndr = do
96 bindings <- getA tsBindings
97 return $ Map.lookup bndr bindings
99 -- Adds a new global binding with the given value
100 addGlobalBind :: CoreSyn.CoreBndr -> CoreSyn.CoreExpr -> TranslatorSession ()
101 addGlobalBind bndr expr = modA tsBindings (Map.insert bndr expr)
103 -- Returns a list of all global binders
104 getGlobalBinders :: TranslatorSession [CoreSyn.CoreBndr]
105 getGlobalBinders = do
106 bindings <- getA tsBindings
107 return $ Map.keys bindings
109 -- vim: set ts=8 sw=2 sts=2 expandtab: