2 -- Simple module providing some types used by Translator. These are in a
3 -- separate module to prevent circular dependencies in Pretty for example.
5 module TranslatorTypes where
7 import qualified Control.Monad.State as State
8 import qualified Data.Map as Map
10 import qualified HscTypes
12 import qualified ForSyDe.Backend.VHDL.AST as AST
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
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
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
36 -- | Add the function to the session
37 addFunc :: HsFunction -> VHDLState ()
39 modFuncMap (Map.insert hsfunc (FuncData Nothing Nothing Nothing))
41 -- | Find the given function in the current session
42 getFunc :: HsFunction -> VHDLState (Maybe FuncData)
44 fs <- State.gets funcs -- Get the funcs element from the session
45 return $ Map.lookup hsfunc fs
47 -- | Gets all functions from the current session
48 getFuncs :: VHDLState [(HsFunction, FuncData)]
50 fs <- State.gets funcs -- Get the funcs element from the session
51 return $ Map.toList fs
53 -- | Sets the FlatFunction for the given HsFunction in the given setting.
54 setFlatFunc :: HsFunction -> FlatFunction -> VHDLState ()
55 setFlatFunc hsfunc flatfunc =
56 modFunc (\d -> d { flatFunc = Just flatfunc }) hsfunc
58 -- | Modify a function in the map using the given function
59 modFunc :: (FuncData -> FuncData) -> HsFunction -> VHDLState ()
61 modFuncMap (Map.adjust f hsfunc)
63 -- | Modify the function map in the session using the given function
64 modFuncMap :: (FuncMap -> FuncMap) -> VHDLState ()
66 fs <- State.gets funcs -- Get the funcs element from the session
68 State.modify (\x -> x {funcs = fs' })
70 -- | Modify all functions in the map using the given function
71 modFuncs :: (HsFunction -> FuncData -> FuncData) -> VHDLState ()
73 modFuncMap (Map.mapWithKey f)
75 getModule :: VHDLState HscTypes.CoreModule
76 getModule = State.gets coreMod -- Get the coreMod element from the session
78 type VHDLState = State.State VHDLSession
80 -- Makes the given name unique by appending a unique number.
81 -- This does not do any checking against existing names, so it only guarantees
82 -- uniqueness with other names generated by uniqueName.
83 uniqueName :: String -> VHDLState String
85 count <- State.gets nameCount -- Get the funcs element from the session
86 State.modify (\s -> s {nameCount = count + 1})
87 return $ name ++ "_" ++ (show count)
89 -- vim: set ts=8 sw=2 sts=2 expandtab: