Move the DesignFile creation to VHDL.
[matthijs/master-project/cλash.git] / VHDL.hs
1 --
2 -- Functions to generate VHDL from FlatFunctions
3 --
4 module VHDL where
5
6 import qualified Data.Foldable as Foldable
7 import qualified Maybe
8 import qualified Control.Monad as Monad
9
10 import qualified Type
11 import qualified Name
12 import qualified TyCon
13 import Outputable ( showSDoc, ppr )
14
15 import qualified ForSyDe.Backend.VHDL.AST as AST
16
17 import VHDLTypes
18 import Flatten
19 import FlattenTypes
20 import TranslatorTypes
21 import Pretty
22
23 getDesignFile :: VHDLState AST.DesignFile
24 getDesignFile = do
25   -- Extract the library units generated from all the functions in the
26   -- session.
27   funcs <- getFuncs
28   let units = concat $ map getLibraryUnits funcs
29   return $ AST.DesignFile 
30     []
31     units
32   
33 -- | Create an entity for a given function
34 createEntity ::
35   HsFunction        -- | The function signature
36   -> FuncData       -- | The function data collected so far
37   -> VHDLState ()
38
39 createEntity hsfunc fdata = 
40   let func = flatFunc fdata in
41   case func of
42     -- Skip (builtin) functions without a FlatFunction
43     Nothing -> do return ()
44     -- Create an entity for all other functions
45     Just flatfunc ->
46       
47       let 
48         sigs    = flat_sigs flatfunc
49         args    = flat_args flatfunc
50         res     = flat_res  flatfunc
51         args'   = map (fmap (mkMap sigs)) args
52         res'    = fmap (mkMap sigs) res
53         ent_decl' = createEntityAST hsfunc args' res'
54         AST.EntityDec entity_id _ = ent_decl' 
55         entity' = Entity entity_id args' res' (Just ent_decl')
56       in
57         setEntity hsfunc entity'
58   where
59     mkMap :: Eq id => [(id, SignalInfo)] -> id -> (AST.VHDLId, AST.TypeMark)
60     mkMap sigmap id =
61       (mkVHDLId nm, vhdl_ty ty)
62       where
63         info = Maybe.fromMaybe
64           (error $ "Signal not found in the name map? This should not happen!")
65           (lookup id sigmap)
66         nm = Maybe.fromMaybe
67           (error $ "Signal not named? This should not happen!")
68           (sigName info)
69         ty = sigTy info
70
71   -- | Create the VHDL AST for an entity
72 createEntityAST ::
73   HsFunction            -- | The signature of the function we're working with
74   -> [VHDLSignalMap]    -- | The entity's arguments
75   -> VHDLSignalMap      -- | The entity's result
76   -> AST.EntityDec      -- | The entity with the ent_decl filled in as well
77
78 createEntityAST hsfunc args res =
79   AST.EntityDec vhdl_id ports
80   where
81     vhdl_id = mkEntityId hsfunc
82     ports = concatMap (mapToPorts AST.In) args
83             ++ mapToPorts AST.Out res
84             ++ clk_port
85     mapToPorts :: AST.Mode -> VHDLSignalMap -> [AST.IfaceSigDec] 
86     mapToPorts mode m =
87       map (mkIfaceSigDec mode) (Foldable.toList m)
88     -- Add a clk port if we have state
89     clk_port = if hasState hsfunc
90       then
91         [AST.IfaceSigDec (mkVHDLId "clk") AST.In VHDL.std_logic_ty]
92       else
93         []
94
95 -- | Create a port declaration
96 mkIfaceSigDec ::
97   AST.Mode                         -- | The mode for the port (In / Out)
98   -> (AST.VHDLId, AST.TypeMark)    -- | The id and type for the port
99   -> AST.IfaceSigDec               -- | The resulting port declaration
100
101 mkIfaceSigDec mode (id, ty) = AST.IfaceSigDec id mode ty
102
103 -- | Generate a VHDL entity name for the given hsfunc
104 mkEntityId hsfunc =
105   -- TODO: This doesn't work for functions with multiple signatures!
106   mkVHDLId $ hsFuncName hsfunc
107
108 -- | Create an architecture for a given function
109 createArchitecture ::
110   HsFunction        -- | The function signature
111   -> FuncData       -- | The function data collected so far
112   -> VHDLState ()
113
114 createArchitecture hsfunc fdata = 
115   let func = flatFunc fdata in
116   case func of
117     -- Skip (builtin) functions without a FlatFunction
118     Nothing -> do return ()
119     -- Create an architecture for all other functions
120     Just flatfunc -> do
121       let sigs = flat_sigs flatfunc
122       let args = flat_args flatfunc
123       let res  = flat_res  flatfunc
124       let apps = flat_apps flatfunc
125       let entity_id = Maybe.fromMaybe
126                       (error $ "Building architecture without an entity? This should not happen!")
127                       (getEntityId fdata)
128       -- Create signal declarations for all signals that are not in args and
129       -- res
130       let sig_decs = [mkSigDec info | (id, info) <- sigs, (all (id `Foldable.notElem`) (res:args)) ]
131       -- Create component instantiations for all function applications
132       insts <- mapM (mkCompInsSm sigs) apps
133       let procs = map mkStateProcSm (getOwnStates hsfunc flatfunc)
134       let insts' = map AST.CSISm insts
135       let procs' = map AST.CSPSm procs
136       let arch = AST.ArchBody (mkVHDLId "structural") (AST.NSimple entity_id) (map AST.BDISD sig_decs) (insts' ++ procs')
137       setArchitecture hsfunc arch
138
139 mkStateProcSm :: (Int, SignalInfo, SignalInfo) -> AST.ProcSm
140 mkStateProcSm (num, old, new) =
141   AST.ProcSm label [clk] [statement]
142   where
143     label       = mkVHDLId $ "state_" ++ (show num)
144     clk         = mkVHDLId "clk"
145     rising_edge = AST.NSimple $ mkVHDLId "rising_edge"
146     wform       = AST.Wform [AST.WformElem (AST.PrimName $ AST.NSimple $ getSignalId new) Nothing]
147     assign      = AST.SigAssign (AST.NSimple $ getSignalId old) wform
148     rising_edge_clk = AST.PrimFCall $ AST.FCall rising_edge [Nothing AST.:=>: (AST.ADName $ AST.NSimple clk)]
149     statement   = AST.IfSm rising_edge_clk [assign] [] Nothing
150
151 mkSigDec :: SignalInfo -> AST.SigDec
152 mkSigDec info =
153     AST.SigDec (getSignalId info) (vhdl_ty ty) Nothing
154   where
155     ty = sigTy info
156
157 -- | Creates a VHDL Id from a named SignalInfo. Errors out if the SignalInfo
158 --   is not named.
159 getSignalId :: SignalInfo -> AST.VHDLId
160 getSignalId info =
161     mkVHDLId $ Maybe.fromMaybe
162       (error $ "Unnamed signal? This should not happen!")
163       (sigName info)
164
165 -- | Transforms a flat function application to a VHDL component instantiation.
166 mkCompInsSm ::
167   [(UnnamedSignal, SignalInfo)] -- | The signals in the current architecture
168   -> FApp UnnamedSignal         -- | The application to look at.
169   -> VHDLState AST.CompInsSm    -- | The corresponding VHDL component instantiation.
170
171 mkCompInsSm sigs app = do
172   let hsfunc = appFunc app
173   fdata_maybe <- getFunc hsfunc
174   let fdata = Maybe.fromMaybe
175         (error $ "Using function '" ++ (prettyShow hsfunc) ++ "' that is not in the session? This should not happen!")
176         fdata_maybe
177   let entity = Maybe.fromMaybe
178         (error $ "Using function '" ++ (prettyShow hsfunc) ++ "' without entity declaration? This should not happen!")
179         (funcEntity fdata)
180   let entity_id = ent_id entity
181   label <- uniqueName (AST.fromVHDLId entity_id)
182   let portmaps = mkAssocElems sigs app entity
183   return $ AST.CompInsSm (mkVHDLId label) (AST.IUEntity (AST.NSimple entity_id)) (AST.PMapAspect portmaps)
184
185 mkAssocElems :: 
186   [(UnnamedSignal, SignalInfo)] -- | The signals in the current architecture
187   -> FApp UnnamedSignal         -- | The application to look at.
188   -> Entity                     -- | The entity to map against.
189   -> [AST.AssocElem]            -- | The resulting port maps
190
191 mkAssocElems sigmap app entity =
192     -- Create the actual AssocElems
193     zipWith mkAssocElem ports sigs
194   where
195     -- Turn the ports and signals from a map into a flat list. This works,
196     -- since the maps must have an identical form by definition. TODO: Check
197     -- the similar form?
198     arg_ports = concat (map Foldable.toList (ent_args entity))
199     res_ports = Foldable.toList (ent_res entity)
200     arg_sigs  = (concat (map Foldable.toList (appArgs app)))
201     res_sigs  = Foldable.toList (appRes app)
202     -- Extract the id part from the (id, type) tuple
203     ports     = (map fst (arg_ports ++ res_ports)) 
204     -- Translate signal numbers into names
205     sigs      = (map (lookupSigName sigmap) (arg_sigs ++ res_sigs))
206
207 -- | Look up a signal in the signal name map
208 lookupSigName :: [(UnnamedSignal, SignalInfo)] -> UnnamedSignal -> String
209 lookupSigName sigs sig = name
210   where
211     info = Maybe.fromMaybe
212       (error $ "Unknown signal " ++ (show sig) ++ " used? This should not happen!")
213       (lookup sig sigs)
214     name = Maybe.fromMaybe
215       (error $ "Unnamed signal " ++ (show sig) ++ " used? This should not happen!")
216       (sigName info)
217
218 -- | Create an VHDL port -> signal association
219 mkAssocElem :: AST.VHDLId -> String -> AST.AssocElem
220 mkAssocElem port signal = Just port AST.:=>: (AST.ADName (AST.NSimple (mkVHDLId signal))) 
221
222 -- | Extracts the generated entity id from the given funcdata
223 getEntityId :: FuncData -> Maybe AST.VHDLId
224 getEntityId fdata =
225   case funcEntity fdata of
226     Nothing -> Nothing
227     Just e  -> case ent_decl e of
228       Nothing -> Nothing
229       Just (AST.EntityDec id _) -> Just id
230
231 getLibraryUnits ::
232   (HsFunction, FuncData)      -- | A function from the session
233   -> [AST.LibraryUnit]        -- | The library units it generates
234
235 getLibraryUnits (hsfunc, fdata) =
236   case funcEntity fdata of 
237     Nothing -> []
238     Just ent -> case ent_decl ent of
239       Nothing -> []
240       Just decl -> [AST.LUEntity decl]
241   ++
242   case funcArch fdata of
243     Nothing -> []
244     Just arch -> [AST.LUArch arch]
245
246 -- | The VHDL Bit type
247 bit_ty :: AST.TypeMark
248 bit_ty = AST.unsafeVHDLBasicId "Bit"
249
250 -- | The VHDL std_logic
251 std_logic_ty :: AST.TypeMark
252 std_logic_ty = AST.unsafeVHDLBasicId "std_logic"
253
254 -- Translate a Haskell type to a VHDL type
255 vhdl_ty :: Type.Type -> AST.TypeMark
256 vhdl_ty ty = Maybe.fromMaybe
257   (error $ "Unsupported Haskell type: " ++ (showSDoc $ ppr ty))
258   (vhdl_ty_maybe ty)
259
260 -- Translate a Haskell type to a VHDL type
261 vhdl_ty_maybe :: Type.Type -> Maybe AST.TypeMark
262 vhdl_ty_maybe ty =
263   case Type.splitTyConApp_maybe ty of
264     Just (tycon, args) ->
265       let name = TyCon.tyConName tycon in
266         -- TODO: Do something more robust than string matching
267         case Name.getOccString name of
268           "Bit"      -> Just bit_ty
269           otherwise  -> Nothing
270     otherwise -> Nothing
271
272 -- Shortcut
273 mkVHDLId :: String -> AST.VHDLId
274 mkVHDLId = AST.unsafeVHDLBasicId