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 {-# LANGUAGE TemplateHaskell #-}
6 module TranslatorTypes where
8 import qualified Control.Monad.State as State
9 import qualified Data.Map as Map
10 import qualified Data.Accessor.Template
13 import qualified HscTypes
15 import qualified ForSyDe.Backend.VHDL.AST as AST
22 -- | A map from a HsFunction identifier to various stuff we collect about a
23 -- function along the way.
24 type FuncMap = Map.Map HsFunction FuncData
26 -- | Some stuff we collect about a function along the way.
27 data FuncData = FuncData {
28 fdFlatFunc_ :: Maybe FlatFunction,
29 fdEntity_ :: Maybe Entity,
30 fdArch_ :: Maybe AST.ArchBody
34 $( Data.Accessor.Template.deriveAccessors ''FuncData )
36 data TranslatorSession = VHDLSession {
37 coreMod :: HscTypes.CoreModule, -- The current module
38 nameCount :: Int, -- A counter that can be used to generate unique names
39 funcs :: FuncMap -- A map from HsFunction to FlatFunction, HWFunction, VHDL Entity and Architecture
42 type TranslatorState = State.State TranslatorSession
44 -- | Add the function to the session
45 addFunc :: HsFunction -> TranslatorState ()
47 modFuncMap (Map.insert hsfunc (FuncData Nothing Nothing Nothing))
49 -- | Find the given function in the current session
50 getFunc :: HsFunction -> TranslatorState (Maybe FuncData)
52 fs <- State.gets funcs -- Get the funcs element from the session
53 return $ Map.lookup hsfunc fs
55 -- | Gets all functions from the current session
56 getFuncs :: TranslatorState [(HsFunction, FuncData)]
58 fs <- State.gets funcs -- Get the funcs element from the session
59 return $ Map.toList fs
61 -- | Gets all the functions from the current session
62 getHsFuncs :: TranslatorState [HsFunction]
64 fs <- State.gets funcs -- Get the funcs element from the session
67 -- | Sets the FlatFunction for the given HsFunction in the current session.
68 setFlatFunc :: HsFunction -> FlatFunction -> TranslatorState ()
69 setFlatFunc hsfunc flatfunc =
70 modFunc (fdFlatFunc ^= Just flatfunc) hsfunc
72 -- | Sets the Entity for the given HsFunction in the current session.
73 setEntity :: HsFunction -> Entity -> TranslatorState ()
74 setEntity hsfunc entity =
75 modFunc (fdEntity ^= Just entity) hsfunc
77 -- | Sets the Entity for the given HsFunction in the current session.
78 setArchitecture :: HsFunction -> AST.ArchBody -> TranslatorState ()
79 setArchitecture hsfunc arch =
80 modFunc (fdArch ^= Just arch) hsfunc
82 -- | Modify a function in the map using the given function
83 modFunc :: (FuncData -> FuncData) -> HsFunction -> TranslatorState ()
85 modFuncMap (Map.adjust f hsfunc)
87 -- | Get the map of functions in the session
88 getFuncMap :: TranslatorState FuncMap
89 getFuncMap = State.gets funcs
91 -- | Modify the function map in the session using the given function
92 modFuncMap :: (FuncMap -> FuncMap) -> TranslatorState ()
94 fs <- State.gets funcs -- Get the funcs element from the session
96 State.modify (\x -> x {funcs = fs' })
98 -- | Apply the given function to all functions in the map, and collect the
99 -- results. The function is allowed to change the function map in the
100 -- session, but any new functions added will not be mapped.
101 modFuncs :: (HsFunction -> FuncData -> TranslatorState ()) -> TranslatorState ()
103 hsfuncs <- getHsFuncs
108 fdata_maybe <- getFunc hsfunc
110 Nothing -> do return ()
111 Just fdata -> f hsfunc fdata
113 getModule :: TranslatorState HscTypes.CoreModule
114 getModule = State.gets coreMod -- Get the coreMod element from the session
116 -- Makes the given name unique by appending a unique number.
117 -- This does not do any checking against existing names, so it only guarantees
118 -- uniqueness with other names generated by uniqueName.
119 uniqueName :: String -> TranslatorState String
121 count <- State.gets nameCount -- Get the funcs element from the session
122 State.modify (\s -> s {nameCount = count + 1})
123 return $ name ++ "_" ++ (show count)
125 -- vim: set ts=8 sw=2 sts=2 expandtab: