module CLaSH.Netlist.Types where
import Control.DeepSeq
import Control.Monad.State (MonadIO, MonadState, StateT)
import Control.Monad.Writer (MonadWriter, WriterT)
import Data.Hashable
import Data.HashMap.Lazy (HashMap)
import Data.IntMap.Lazy (IntMap, empty)
import qualified Data.Text as S
import Data.Text.Lazy (Text, pack)
import GHC.Generics (Generic)
import Unbound.Generics.LocallyNameless (Fresh, FreshMT)
import CLaSH.Core.Term (Term, TmName)
import CLaSH.Core.Type (Type)
import CLaSH.Core.TyCon (TyCon, TyConName)
import CLaSH.Core.Util (Gamma)
import CLaSH.Netlist.BlackBox.Types
import CLaSH.Primitives.Types (PrimMap)
import CLaSH.Util
newtype NetlistMonad a =
NetlistMonad { runNetlist :: WriterT
[(Identifier,HWType)]
(StateT NetlistState (FreshMT IO))
a
}
deriving (Functor, Monad, Applicative, MonadWriter [(Identifier,HWType)],
MonadState NetlistState, Fresh, MonadIO)
data NetlistState
= NetlistState
{ _bindings :: HashMap TmName (Type,Term)
, _varEnv :: Gamma
, _varCount :: Int
, _cmpCount :: Int
, _components :: HashMap TmName Component
, _primitives :: PrimMap
, _typeTranslator :: HashMap TyConName TyCon -> Type -> Maybe (Either String HWType)
, _tcCache :: HashMap TyConName TyCon
}
type Identifier = Text
data Component
= Component
{ componentName :: Identifier
, hiddenPorts :: [(Identifier,HWType)]
, inputs :: [(Identifier,HWType)]
, output :: (Identifier,HWType)
, declarations :: [Declaration]
}
deriving Show
instance NFData Component where
rnf c = case c of
Component nm hi inps outps decls -> rnf nm `seq` rnf hi `seq` rnf inps `seq`
rnf outps `seq` rnf decls
type Size = Int
data HWType
= Void
| Bool
| Integer
| BitVector Size
| Index Size
| Signed Size
| Unsigned Size
| Vector Size HWType
| Sum Identifier [Identifier]
| Product Identifier [HWType]
| SP Identifier [(Identifier,[HWType])]
| Clock Int
| Reset Int
deriving (Eq,Show,Generic)
instance Hashable HWType
instance NFData HWType where
rnf hwty = case hwty of
Void -> ()
Bool -> ()
Integer -> ()
BitVector s -> rnf s
Index u -> rnf u
Signed s -> rnf s
Unsigned s -> rnf s
Vector s el -> rnf s `seq` rnf el
Sum i ids -> rnf i `seq` rnf ids
Product i ids -> rnf i `seq` rnf ids
SP i ids -> rnf i `seq` rnf ids
Clock i -> rnf i
Reset i -> rnf i
data Declaration
= Assignment Identifier Expr
| CondAssignment Identifier Expr [(Maybe Expr,Expr)]
| InstDecl Identifier Identifier [(Identifier,Expr)]
| BlackBoxD S.Text BlackBoxTemplate BlackBoxContext
| NetDecl Identifier HWType
deriving Show
instance NFData Declaration where
rnf a = a `seq` ()
data Modifier
= Indexed (HWType,Int,Int)
| DC (HWType,Int)
| VecAppend
deriving Show
data Expr
= Literal (Maybe (HWType,Size)) Literal
| DataCon HWType Modifier [Expr]
| Identifier Identifier (Maybe Modifier)
| DataTag HWType (Either Identifier Identifier)
| BlackBoxE S.Text BlackBoxTemplate BlackBoxContext Bool
deriving Show
data Literal
= NumLit Integer
| BitLit Bit
| BoolLit Bool
| VecLit [Literal]
deriving Show
data Bit
= H
| L
| U
| Z
deriving Show
data BlackBoxContext
= Context
{ bbResult :: (SyncExpr,HWType)
, bbInputs :: [(SyncExpr,HWType,Bool)]
, bbFunctions :: IntMap (Either BlackBoxTemplate Declaration,BlackBoxContext)
}
deriving Show
emptyBBContext :: BlackBoxContext
emptyBBContext = Context (Left $ Identifier (pack "__EMPTY__") Nothing, Void) [] empty
type SyncIdentifier = Either Identifier (Identifier,(Identifier,Int))
type SyncExpr = Either Expr (Expr,(Identifier,Int))
makeLenses ''NetlistState