Mark port signals as such during flattening.
[matthijs/master-project/cλash.git] / Flatten.hs
index f7ab86ce107ed770e6bbc46e3d67186e83eccdb9..2e66e90d1dbdb40b70324223423bbfedc07c4857 100644 (file)
@@ -7,6 +7,8 @@ import qualified Name
 import qualified Maybe
 import qualified DataCon
 import qualified CoreUtils
+import qualified Data.Traversable as Traversable
+import qualified Data.Foldable as Foldable
 import Control.Applicative
 import Outputable ( showSDoc, ppr )
 import qualified Control.Monad.State as State
@@ -27,24 +29,18 @@ genSignals ::
   Type.Type
   -> FlattenState (SignalMap UnnamedSignal)
 
-genSignals ty = do
-  typeMapToUseMap SigInternal tymap
-  where
-    -- First generate a map with the right structure containing the types
-    tymap = mkHsValueMap ty
-
-typeMapToUseMap ::
-  SigUse
-  -> HsValueMap Type.Type
-  -> FlattenState (SignalMap UnnamedSignal)
+genSignals ty =
+  -- First generate a map with the right structure containing the types, and
+  -- generate signals for each of them.
+  Traversable.mapM (\ty -> genSignalId SigInternal ty) (mkHsValueMap ty)
 
-typeMapToUseMap use (Single ty) = do
-  id <- genSignalId use ty
-  return $ Single id
-
-typeMapToUseMap use (Tuple tymaps) = do
-  usemaps <- State.mapM (typeMapToUseMap use) tymaps
-  return $ Tuple usemaps
+-- | Marks a signal as the given SigUse, if its id is in the list of id's
+--   given.
+markSignal :: SigUse -> [UnnamedSignal] -> (UnnamedSignal, SignalInfo) -> (UnnamedSignal, SignalInfo)
+markSignal use ids (id, info) =
+  (id, info')
+  where
+    info' = if id `elem` ids then info { sigUse = use} else info
 
 -- | Flatten a haskell function
 flattenFunction ::
@@ -54,12 +50,14 @@ flattenFunction ::
 
 flattenFunction _ (Rec _) = error "Recursive binders not supported"
 flattenFunction hsfunc bind@(NonRec var expr) =
-  FlatFunction args res apps conds sigs
+  FlatFunction args res apps conds sigs'
   where
     init_state        = ([], [], [], 0)
     (fres, end_state) = State.runState (flattenExpr [] expr) init_state
     (args, res)       = fres
+    portlist          = concat (map Foldable.toList (res:args))
     (apps, conds, sigs, _)  = end_state
+    sigs'             = fmap (markSignal SigPort portlist) sigs
 
 flattenExpr ::
   BindMap