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