d23daea033d77b38710b5fe6c09fcbfaae2be62f
[matthijs/master-project/cλash.git] / VHDLTypes.hs
1 --
2 -- Some types used by the VHDL module.
3 --
4 {-# LANGUAGE TemplateHaskell #-}
5 module VHDLTypes where
6
7 -- Standard imports
8 import qualified Control.Monad.Trans.State as State
9 import qualified Data.Map as Map
10 import Data.Accessor
11 import qualified Data.Accessor.Template
12
13 -- GHC API imports
14 import qualified Type
15 import qualified CoreSyn
16
17 -- ForSyDe imports
18 import qualified ForSyDe.Backend.VHDL.AST as AST
19
20 -- Local imports
21 import FlattenTypes
22 import HsValueMap
23
24 type VHDLSignalMapElement = (Maybe (AST.VHDLId, AST.TypeMark))
25 -- | A mapping from a haskell structure to the corresponding VHDL port
26 --   signature, or Nothing for values that do not translate to a port.
27 type VHDLSignalMap = HsValueMap VHDLSignalMapElement
28
29 -- A description of a VHDL entity. Contains both the entity itself as well as
30 -- info on how to map a haskell value (argument / result) on to the entity's
31 -- ports.
32 data Entity = Entity { 
33   ent_id     :: AST.VHDLId,           -- The id of the entity
34   ent_args   :: [VHDLSignalMapElement],      -- A mapping of each function argument to port names
35   ent_res    :: VHDLSignalMapElement         -- A mapping of the function result to port names
36 } deriving (Show);
37
38 -- A orderable equivalent of CoreSyn's Type for use as a map key
39 newtype OrdType = OrdType { getType :: Type.Type }
40 instance Eq OrdType where
41   (OrdType a) == (OrdType b) = Type.tcEqType a b
42 instance Ord OrdType where
43   compare (OrdType a) (OrdType b) = Type.tcCmpType a b
44
45 -- A map of a Core type to the corresponding type name
46 type TypeMap = Map.Map OrdType (AST.VHDLId, Either AST.TypeDef AST.SubtypeIn)
47
48 -- A map of Elem types to the corresponding VHDL Id for the Vector
49 type ElemTypeMap = Map.Map OrdType (AST.VHDLId, AST.TypeDef)
50
51 -- A map of a vector Core element type and function name to the coressponding
52 -- VHDLId of the function and the function body.
53 type TypeFunMap = Map.Map (OrdType, String) (AST.VHDLId, AST.SubProgBody)
54
55 -- A map of a Haskell function to a hardware signature
56 type SignatureMap = Map.Map CoreSyn.CoreBndr Entity
57
58 data VHDLState = VHDLState {
59   -- | A map of Core type -> VHDL Type
60   vsTypes_      :: TypeMap,
61   -- | A map of Elem types -> VHDL Vector Id
62   vsElemTypes_   :: ElemTypeMap,
63   -- | A map of vector Core type -> VHDL type function
64   vsTypeFuns_   :: TypeFunMap,
65   -- | A map of HsFunction -> hardware signature (entity name, port names,
66   --   etc.)
67   vsSignatures_ :: SignatureMap
68 }
69
70 -- Derive accessors
71 $( Data.Accessor.Template.deriveAccessors ''VHDLState )
72
73 -- | The state containing a VHDL Session
74 type VHDLSession = State.State VHDLState
75
76 -- | A substate containing just the types
77 type TypeState = State.State TypeMap
78
79 type Builder = Either (CoreSyn.CoreBndr -> [AST.Expr] -> VHDLSession AST.Expr) (Entity -> [CoreSyn.CoreBndr] -> VHDLSession AST.GenerateSm)
80
81 -- A map of a builtin function to VHDL function builder 
82 type NameTable = Map.Map String (Int, Builder )
83
84 -- vim: set ts=8 sw=2 sts=2 expandtab: