da1407a78044bdcf8153deb3a06ddb6ac6174e44
[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   entity   :: Maybe Entity,
27   funcArch :: Maybe AST.ArchBody
28 }
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 = do
39   fs <- State.gets funcs -- Get the funcs element from the session
40   let fs' = Map.insert hsfunc (FuncData Nothing Nothing Nothing) fs -- Insert function
41   State.modify (\x -> x {funcs = fs' })
42
43 -- | Find the given function in the current session
44 getFunc :: HsFunction -> VHDLState (Maybe FuncData)
45 getFunc hsfunc = do
46   fs <- State.gets funcs -- Get the funcs element from the session
47   return $ Map.lookup hsfunc fs
48
49 -- | Gets all functions from the current session
50 getFuncs :: VHDLState [(HsFunction, FuncData)]
51 getFuncs = do
52   fs <- State.gets funcs -- Get the funcs element from the session
53   return $ Map.toList fs
54
55 -- | Sets the FlatFunction for the given HsFunction in the given setting.
56 setFlatFunc :: HsFunction -> FlatFunction -> VHDLState ()
57 setFlatFunc hsfunc flatfunc = do
58   fs <- State.gets funcs -- Get the funcs element from the session
59   let fs'= Map.adjust (\d -> d { flatFunc = Just flatfunc }) hsfunc fs
60   State.modify (\x -> x {funcs = fs' })
61
62 -- | Modify all functions in the map using the given function
63 modFuncs :: (HsFunction -> FuncData -> FuncData) -> VHDLState ()
64 modFuncs f = do
65   fs <- State.gets funcs -- Get the funcs element from the session
66   let fs' = Map.mapWithKey f fs
67   State.modify (\x -> x {funcs = fs' })
68
69 getModule :: VHDLState HscTypes.CoreModule
70 getModule = State.gets coreMod -- Get the coreMod element from the session
71
72 type VHDLState = State.State VHDLSession
73
74 -- Makes the given name unique by appending a unique number.
75 -- This does not do any checking against existing names, so it only guarantees
76 -- uniqueness with other names generated by uniqueName.
77 uniqueName :: String -> VHDLState String
78 uniqueName name = do
79   count <- State.gets nameCount -- Get the funcs element from the session
80   State.modify (\s -> s {nameCount = count + 1})
81   return $ name ++ "_" ++ (show count)
82
83 -- vim: set ts=8 sw=2 sts=2 expandtab: