8e24541eb6317efa38d5756466bf6b393c3465bf
[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 qualified Data.Map as Map
10 import Flatten
11
12
13 -- | A map from a HsFunction identifier to various stuff we collect about a
14 --   function along the way.
15 type FuncMap  = Map.Map HsFunction FuncData
16
17 -- | Some stuff we collect about a function along the way.
18 data FuncData = FuncData {
19   flatFunc :: Maybe FlatFunction
20 }
21
22 data VHDLSession = VHDLSession {
23   coreMod   :: HscTypes.CoreModule, -- The current module
24   nameCount :: Int,             -- A counter that can be used to generate unique names
25   funcs     :: FuncMap          -- A map from HsFunction to FlatFunction, HWFunction, VHDL Entity and Architecture
26 }
27
28 -- | Add the function to the session
29 addFunc :: HsFunction -> VHDLState ()
30 addFunc hsfunc = do
31   fs <- State.gets funcs -- Get the funcs element from the session
32   let fs' = Map.insert hsfunc (FuncData Nothing) fs -- Insert function
33   State.modify (\x -> x {funcs = fs' })
34
35 -- | Find the given function in the current session
36 getFunc :: HsFunction -> VHDLState (Maybe FuncData)
37 getFunc hsfunc = do
38   fs <- State.gets funcs -- Get the funcs element from the session
39   return $ Map.lookup hsfunc fs
40
41 -- | Sets the FlatFunction for the given HsFunction in the given setting.
42 setFlatFunc :: HsFunction -> FlatFunction -> VHDLState ()
43 setFlatFunc hsfunc flatfunc = do
44   fs <- State.gets funcs -- Get the funcs element from the session
45   let fs'= Map.adjust (\d -> d { flatFunc = Just flatfunc }) hsfunc fs
46   State.modify (\x -> x {funcs = fs' })
47
48 getModule :: VHDLState HscTypes.CoreModule
49 getModule = State.gets coreMod -- Get the coreMod element from the session
50
51 type VHDLState = State.State VHDLSession
52
53 -- Makes the given name unique by appending a unique number.
54 -- This does not do any checking against existing names, so it only guarantees
55 -- uniqueness with other names generated by uniqueName.
56 uniqueName :: String -> VHDLState String
57 uniqueName name = do
58   count <- State.gets nameCount -- Get the funcs element from the session
59   State.modify (\s -> s {nameCount = count + 1})
60   return $ name ++ "_" ++ (show count)
61
62 -- vim: set ts=8 sw=2 sts=2 expandtab: