b9db66a485220276f060c18edcb9c1419efa1fa3
[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
22 -- A description of a port of an entity
23 type Port = (AST.VHDLId, AST.TypeMark)
24
25 -- A description of a VHDL entity. Contains both the entity itself as well as
26 -- info on how to map a haskell value (argument / result) on to the entity's
27 -- ports.
28 data Entity = Entity { 
29   ent_id     :: AST.VHDLId,           -- The id of the entity
30   ent_args   :: [Port],      -- A mapping of each function argument to port names
31   ent_res    :: Port         -- A mapping of the function result to port names
32 } deriving (Show);
33
34 -- A orderable equivalent of CoreSyn's Type for use as a map key
35 newtype OrdType = OrdType { getType :: Type.Type }
36 instance Eq OrdType where
37   (OrdType a) == (OrdType b) = Type.tcEqType a b
38 instance Ord OrdType where
39   compare (OrdType a) (OrdType b) = Type.tcCmpType a b
40
41 data HType = StdType OrdType |
42              ADTType String [HType] |
43              VecType Int HType |
44              SizedWType Int |
45              RangedWType Int |
46              SizedIType Int |
47              BuiltinType String
48   deriving (Eq, Ord)
49
50 -- A map of a Core type to the corresponding type name
51 type TypeMap = Map.Map HType (AST.VHDLId, Either AST.TypeDef AST.SubtypeIn)
52
53 -- A map of a vector Core element type and function name to the coressponding
54 -- VHDLId of the function and the function body.
55 type TypeFunMap = Map.Map (OrdType, String) (AST.VHDLId, AST.SubProgBody)
56
57 -- A map of a Haskell function to a hardware signature
58 type SignatureMap = Map.Map CoreSyn.CoreBndr Entity
59
60 type TfpIntMap = Map.Map OrdType Int
61
62 data TypeState = TypeState {
63   -- | A map of Core type -> VHDL Type
64   vsTypes_      :: TypeMap,
65   -- | A list of type declarations
66   vsTypeDecls_  :: [AST.PackageDecItem],
67   -- | A map of vector Core type -> VHDL type function
68   vsTypeFuns_   :: TypeFunMap,
69   vsTfpInts_    :: TfpIntMap
70 }
71 -- Derive accessors
72 $( Data.Accessor.Template.deriveAccessors ''TypeState )
73 -- Define an empty TypeState
74 emptyTypeState = TypeState Map.empty [] Map.empty Map.empty
75 -- Define a session
76 type TypeSession = State.State TypeState
77
78 data VHDLState = VHDLState {
79   -- | A subtype with typing info
80   vsType_       :: TypeState,
81   -- | A map of HsFunction -> hardware signature (entity name, port names,
82   --   etc.)
83   vsSignatures_ :: SignatureMap
84 }
85
86 -- Derive accessors
87 $( Data.Accessor.Template.deriveAccessors ''VHDLState )
88
89 -- | The state containing a VHDL Session
90 type VHDLSession = State.State VHDLState
91
92 -- A function that generates VHDL for a builtin function
93 type BuiltinBuilder = 
94   (Either CoreSyn.CoreBndr AST.VHDLName) -- ^ The destination signal and it's original type
95   -> CoreSyn.CoreBndr -- ^ The function called
96   -> [Either CoreSyn.CoreExpr AST.Expr] -- ^ The value arguments passed (excluding type and
97                     --   dictionary arguments).
98   -> VHDLSession [AST.ConcSm] -- ^ The resulting concurrent statements.
99
100 -- A map of a builtin function to VHDL function builder 
101 type NameTable = Map.Map String (Int, BuiltinBuilder )
102
103 -- vim: set ts=8 sw=2 sts=2 expandtab: