Add the current CoreModule to the session.
[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 HscTypes
9 import Flatten
10
11 type FuncMap = [(HsFunction, 
12     (FlatFunction))]
13
14 data VHDLSession = VHDLSession {
15   coreMod   :: HscTypes.CoreModule, -- The current module
16   nameCount :: Int,             -- A counter that can be used to generate unique names
17   funcs     :: FuncMap          -- A map from HsFunction to FlatFunction, HWFunction, VHDL Entity and Architecture
18 }
19
20 -- Add the function to the session
21 addFunc :: HsFunction -> FlatFunction -> VHDLState ()
22 addFunc hsfunc flatfunc = do
23   fs <- State.gets funcs -- Get the funcs element from the session
24   State.modify (\x -> x {funcs = (hsfunc, flatfunc) : fs }) -- Prepend name and f
25
26 getModule :: VHDLState HscTypes.CoreModule
27 getModule = State.gets coreMod -- Get the coreMod element from the session
28
29 type VHDLState = State.State VHDLSession
30
31 -- Makes the given name unique by appending a unique number.
32 -- This does not do any checking against existing names, so it only guarantees
33 -- uniqueness with other names generated by uniqueName.
34 uniqueName :: String -> VHDLState String
35 uniqueName name = do
36   count <- State.gets nameCount -- Get the funcs element from the session
37   State.modify (\s -> s {nameCount = count + 1})
38   return $ name ++ "_" ++ (show count)
39