Extract entities from the session and return them in the design file.
[matthijs/master-project/cλash.git] / TranslatorTypes.hs
1 --
2 -- Simple module providing some types used by Translator. These are in a
3 -- separate module to prevent circular dependencies in Pretty for example.
4 --
5 module TranslatorTypes where
6
7 import qualified Control.Monad.State as State
8 import qualified Data.Map as Map
9
10 import qualified HscTypes
11
12 import FlattenTypes
13 import VHDLTypes
14 import HsValueMap
15
16
17 -- | A map from a HsFunction identifier to various stuff we collect about a
18 --   function along the way.
19 type FuncMap  = Map.Map HsFunction FuncData
20
21 -- | Some stuff we collect about a function along the way.
22 data FuncData = FuncData {
23   flatFunc :: Maybe FlatFunction,
24   entity   :: Maybe Entity
25 }
26
27 data VHDLSession = VHDLSession {
28   coreMod   :: HscTypes.CoreModule, -- The current module
29   nameCount :: Int,             -- A counter that can be used to generate unique names
30   funcs     :: FuncMap          -- A map from HsFunction to FlatFunction, HWFunction, VHDL Entity and Architecture
31 }
32
33 -- | Add the function to the session
34 addFunc :: HsFunction -> VHDLState ()
35 addFunc hsfunc = do
36   fs <- State.gets funcs -- Get the funcs element from the session
37   let fs' = Map.insert hsfunc (FuncData Nothing Nothing) fs -- Insert function
38   State.modify (\x -> x {funcs = fs' })
39
40 -- | Find the given function in the current session
41 getFunc :: HsFunction -> VHDLState (Maybe FuncData)
42 getFunc hsfunc = do
43   fs <- State.gets funcs -- Get the funcs element from the session
44   return $ Map.lookup hsfunc fs
45
46 -- | Gets all functions from the current session
47 getFuncs :: VHDLState [(HsFunction, FuncData)]
48 getFuncs = do
49   fs <- State.gets funcs -- Get the funcs element from the session
50   return $ Map.toList fs
51
52 -- | Sets the FlatFunction for the given HsFunction in the given setting.
53 setFlatFunc :: HsFunction -> FlatFunction -> VHDLState ()
54 setFlatFunc hsfunc flatfunc = do
55   fs <- State.gets funcs -- Get the funcs element from the session
56   let fs'= Map.adjust (\d -> d { flatFunc = Just flatfunc }) hsfunc fs
57   State.modify (\x -> x {funcs = fs' })
58
59 -- | Modify all functions in the map using the given function
60 modFuncs :: (HsFunction -> FuncData -> FuncData) -> VHDLState ()
61 modFuncs f = do
62   fs <- State.gets funcs -- Get the funcs element from the session
63   let fs' = Map.mapWithKey f fs
64   State.modify (\x -> x {funcs = fs' })
65
66 getModule :: VHDLState HscTypes.CoreModule
67 getModule = State.gets coreMod -- Get the coreMod element from the session
68
69 type VHDLState = State.State VHDLSession
70
71 -- Makes the given name unique by appending a unique number.
72 -- This does not do any checking against existing names, so it only guarantees
73 -- uniqueness with other names generated by uniqueName.
74 uniqueName :: String -> VHDLState String
75 uniqueName name = do
76   count <- State.gets nameCount -- Get the funcs element from the session
77   State.modify (\s -> s {nameCount = count + 1})
78   return $ name ++ "_" ++ (show count)
79
80 -- vim: set ts=8 sw=2 sts=2 expandtab: