X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=FlattenTypes.hs;h=092baff4911fdcb6789399ec613c09293fe30f02;hb=9b7d00ad53acfc821840051ef693d87470b4462b;hp=409c2ac249970f3e1f12593bdfb7431977a40a67;hpb=ed9f3e71c71db41d85a9dabb3a676f4c342b8266;p=matthijs%2Fmaster-project%2Fc%CE%BBash.git diff --git a/FlattenTypes.hs b/FlattenTypes.hs index 409c2ac..092baff 100644 --- a/FlattenTypes.hs +++ b/FlattenTypes.hs @@ -20,7 +20,7 @@ type SignalMap = HsValueMap SignalId type StateId = Int -- | How is a given (single) value in a function's type (ie, argument or --- return value) used? +-- return value) used? data HsValueUse = Port -- ^ Use it as a port (input or output) | State StateId -- ^ Use it as state (input or output). The int is used to @@ -75,20 +75,38 @@ hasState hsfunc = any (Foldable.any isStateUse) (hsFuncArgs hsfunc) || Foldable.any isStateUse (hsFuncRes hsfunc) --- | A flattened function application -data FApp = FApp { - appFunc :: HsFunction, - appArgs :: [SignalMap], - appRes :: SignalMap -} deriving (Show, Eq) - --- | A conditional signal definition -data CondDef = CondDef { - cond :: SignalId, - high :: SignalId, - low :: SignalId, - condRes :: SignalId -} deriving (Show, Eq) +-- | Something that defines a signal +data SigDef = + -- | A flattened function application + FApp { + appFunc :: HsFunction, + appArgs :: [SignalMap], + appRes :: SignalMap + } + -- | A conditional signal definition + | CondDef { + cond :: SignalId, + high :: SignalId, + low :: SignalId, + condRes :: SignalId + } + -- | Unconditional signal definition + | UncondDef { + defSrc :: Either SignalId SignalExpr, + defDst :: SignalId + } deriving (Show, Eq) + +-- | An expression on signals +data SignalExpr = + EqLit SignalId String -- ^ Is the given signal equal to the given (VHDL) literal + | Literal String -- ^ A literal value + | Eq SignalId SignalId -- ^ A comparison between to signals + deriving (Show, Eq) + +-- Returns the function used by the given SigDef, if any +usedHsFunc :: SigDef -> Maybe HsFunction +usedHsFunc (FApp hsfunc _ _) = Just hsfunc +usedHsFunc _ = Nothing -- | How is a given signal used in the resulting VHDL? data SigUse = @@ -127,8 +145,7 @@ data SignalInfo = SignalInfo { data FlatFunction = FlatFunction { flat_args :: [SignalMap], flat_res :: SignalMap, - flat_apps :: [FApp], - flat_conds :: [CondDef], + flat_defs :: [SigDef], flat_sigs :: [(SignalId, SignalInfo)] } @@ -140,6 +157,10 @@ signalInfo sigs id = Maybe.fromJust $ lookup id sigs -- | A list of binds in effect at a particular point of evaluation type BindMap = [( CoreBndr, -- ^ The bind name + BindValue -- ^ The value bound to it + )] + +type BindValue = Either -- ^ The bind value which is either (SignalMap) -- ^ a signal @@ -147,28 +168,34 @@ type BindMap = [( HsValueUse, -- ^ or a HighOrder function [SignalId] -- ^ With these signals already applied to it ) - )] -- | The state during the flattening of a single function -type FlattenState = State.State ([FApp], [CondDef], [(SignalId, SignalInfo)], SignalId) +type FlattenState = State.State ([SigDef], [(SignalId, SignalInfo)], SignalId) -- | Add an application to the current FlattenState -addApp :: (FApp) -> FlattenState () -addApp a = do - (apps, conds, sigs, n) <- State.get - State.put (a:apps, conds, sigs, n) - --- | Add a conditional definition to the current FlattenState -addCondDef :: (CondDef) -> FlattenState () -addCondDef c = do - (apps, conds, sigs, n) <- State.get - State.put (apps, c:conds, sigs, n) +addDef :: SigDef -> FlattenState () +addDef d = do + (defs, sigs, n) <- State.get + State.put (d:defs, sigs, n) -- | Generates a new signal id, which is unique within the current flattening. genSignalId :: SigUse -> Type.Type -> FlattenState SignalId genSignalId use ty = do - (apps, conds, sigs, n) <- State.get + (defs, sigs, n) <- State.get -- Generate a new numbered but unnamed signal let s = (n, SignalInfo Nothing use ty) - State.put (apps, conds, s:sigs, n+1) + State.put (defs, s:sigs, n+1) return n + +-- | Returns the SignalInfo for the given signal. Errors if the signal is not +-- known in the session. +getSignalInfo :: SignalId -> FlattenState SignalInfo +getSignalInfo id = do + (defs, sigs, n) <- State.get + return $ signalInfo sigs id + +setSignalInfo :: SignalId -> SignalInfo -> FlattenState () +setSignalInfo id' info' = do + (defs, sigs, n) <- State.get + let sigs' = map (\(id, info) -> (id, if id == id' then info' else info)) sigs + State.put (defs, sigs', n)