Mark all signals as ports or states when appropriate.
[matthijs/master-project/cλash.git] / FlattenTypes.hs
index 5c95632859d22fc30a8752a28661ad6083eaf224..ce1f22dc00bbece034db0108d6ae6b6f52217cc0 100644 (file)
@@ -1,6 +1,8 @@
 module FlattenTypes where
 
+import qualified Maybe
 import Data.Traversable
+import qualified Data.Foldable as Foldable
 import qualified Control.Monad.State as State
 
 import CoreSyn
@@ -28,6 +30,11 @@ data HsValueUse =
   }
   deriving (Show, Eq, Ord)
 
+-- | Is this HsValueUse a state use?
+isStateUse :: HsValueUse -> Bool
+isStateUse (State _) = True
+isStateUse _         = False
+
 -- | A map from a Haskell value to the use of each single value
 type HsUseMap = HsValueMap HsValueUse
 
@@ -60,6 +67,11 @@ data HsFunction = HsFunction {
   hsFuncRes  :: HsUseMap
 } deriving (Show, Eq, Ord)
 
+hasState :: HsFunction -> Bool
+hasState hsfunc = 
+  any (Foldable.any isStateUse) (hsFuncArgs hsfunc)
+  || Foldable.any isStateUse (hsFuncRes hsfunc)
+
 -- | A flattened function application
 data FApp sigid = FApp {
   appFunc :: HsFunction,
@@ -77,10 +89,12 @@ data CondDef sigid = CondDef {
 
 -- | How is a given signal used in the resulting VHDL?
 data SigUse = 
-  SigPort         -- | Use as a port
-  | SigInternal   -- | Use as an internal signal
-  | SigState      -- | Use as an internal state
-  | SigSubState   -- | Do not use, state variable is used in a subcircuit
+  SigPortIn          -- | Use as an input port
+  | SigPortOut       -- | Use as an input port
+  | SigInternal      -- | Use as an internal signal
+  | SigStateOld Int  -- | Use as the current internal state
+  | SigStateNew Int  -- | Use as the new internal state
+  | SigSubState      -- | Do not use, state variable is used in a subcircuit
 
 -- | Information on a signal definition
 data SignalInfo = SignalInfo {
@@ -98,6 +112,11 @@ data FlatFunction' sigid = FlatFunction {
   flat_sigs   :: [(sigid, SignalInfo)]
 }
 
+-- | Lookup a given signal id in a signal map, and return the associated
+--   SignalInfo. Errors out if the signal was not found.
+signalInfo :: Eq sigid => [(sigid, SignalInfo)] -> sigid -> SignalInfo
+signalInfo sigs id = Maybe.fromJust $ lookup id sigs
+
 -- | A flat function that does not have its signals named
 type FlatFunction = FlatFunction' UnnamedSignal