Derive Show for a bunch of types.
[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 qualified ForSyDe.Backend.VHDL.AST as AST
13
14 import FlattenTypes
15 import VHDLTypes
16 import HsValueMap
17
18
19 -- | A map from a HsFunction identifier to various stuff we collect about a
20 --   function along the way.
21 type FuncMap  = Map.Map HsFunction FuncData
22
23 -- | Some stuff we collect about a function along the way.
24 data FuncData = FuncData {
25   flatFunc     :: Maybe FlatFunction,
26   funcEntity   :: Maybe Entity,
27   funcArch     :: Maybe AST.ArchBody
28 } deriving (Show)
29
30 data VHDLSession = VHDLSession {
31   coreMod   :: HscTypes.CoreModule, -- The current module
32   nameCount :: Int,             -- A counter that can be used to generate unique names
33   funcs     :: FuncMap          -- A map from HsFunction to FlatFunction, HWFunction, VHDL Entity and Architecture
34 }
35
36 -- | Add the function to the session
37 addFunc :: HsFunction -> VHDLState ()
38 addFunc hsfunc =
39   modFuncMap (Map.insert hsfunc (FuncData Nothing Nothing Nothing))
40
41 -- | Find the given function in the current session
42 getFunc :: HsFunction -> VHDLState (Maybe FuncData)
43 getFunc hsfunc = do
44   fs <- State.gets funcs -- Get the funcs element from the session
45   return $ Map.lookup hsfunc fs
46
47 -- | Gets all functions from the current session
48 getFuncs :: VHDLState [(HsFunction, FuncData)]
49 getFuncs = do
50   fs <- State.gets funcs -- Get the funcs element from the session
51   return $ Map.toList fs
52
53 -- | Gets all the functions from the current session
54 getHsFuncs :: VHDLState [HsFunction]
55 getHsFuncs = do
56   fs <- State.gets funcs -- Get the funcs element from the session
57   return $ Map.keys fs
58   
59 -- | Sets the FlatFunction for the given HsFunction in the current session.
60 setFlatFunc :: HsFunction -> FlatFunction -> VHDLState ()
61 setFlatFunc hsfunc flatfunc =
62   modFunc (\d -> d { flatFunc = Just flatfunc }) hsfunc
63
64 -- | Sets the Entity for the given HsFunction in the current session.
65 setEntity :: HsFunction -> Entity -> VHDLState ()
66 setEntity hsfunc entity =
67   modFunc (\d -> d { funcEntity = Just entity }) hsfunc
68
69 -- | Sets the Entity for the given HsFunction in the current session.
70 setArchitecture :: HsFunction -> AST.ArchBody -> VHDLState ()
71 setArchitecture hsfunc arch =
72   modFunc (\d -> d { funcArch = Just arch }) hsfunc
73
74 -- | Modify a function in the map using the given function
75 modFunc :: (FuncData -> FuncData) -> HsFunction -> VHDLState ()
76 modFunc f hsfunc =
77   modFuncMap (Map.adjust f hsfunc)
78
79 -- | Modify the function map in the session using the given function
80 modFuncMap :: (FuncMap -> FuncMap) -> VHDLState ()
81 modFuncMap f = do
82   fs <- State.gets funcs -- Get the funcs element from the session
83   let fs' = f fs
84   State.modify (\x -> x {funcs = fs' })
85
86 -- | Apply the given function to all functions in the map, and collect the
87 --   results. The function is allowed to change the function map in the
88 --   session, but any new functions added will not be mapped.
89 modFuncs :: (HsFunction -> FuncData -> VHDLState ()) -> VHDLState ()
90 modFuncs f = do
91   hsfuncs <- getHsFuncs
92   mapM doFunc hsfuncs
93   return ()
94   where
95     doFunc hsfunc = do
96       fdata_maybe <- getFunc hsfunc
97       case fdata_maybe of
98         Nothing -> do return ()
99         Just fdata -> f hsfunc fdata
100
101 getModule :: VHDLState HscTypes.CoreModule
102 getModule = State.gets coreMod -- Get the coreMod element from the session
103
104 type VHDLState = State.State VHDLSession
105
106 -- Makes the given name unique by appending a unique number.
107 -- This does not do any checking against existing names, so it only guarantees
108 -- uniqueness with other names generated by uniqueName.
109 uniqueName :: String -> VHDLState String
110 uniqueName name = do
111   count <- State.gets nameCount -- Get the funcs element from the session
112   State.modify (\s -> s {nameCount = count + 1})
113   return $ name ++ "_" ++ (show count)
114
115 -- vim: set ts=8 sw=2 sts=2 expandtab: