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.HashSet (HashSet)
import Data.Text.Lazy (Text)
import GHC.Generics (Generic)
import Text.PrettyPrint.Leijen.Text.Monadic (Doc)
import Unbound.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.Primitives.Types (PrimMap)
import CLaSH.Util
newtype NetlistMonad a =
NetlistMonad { runNetlist :: WriterT [(Identifier,HWType)] (StateT NetlistState (FreshMT IO)) a }
deriving (Functor, Monad, Applicative, MonadState NetlistState, MonadWriter [(Identifier,HWType)], Fresh, MonadIO)
type VHDLState = (HashSet HWType,Int,HashMap HWType Doc)
data NetlistState
= NetlistState
{ _bindings :: HashMap TmName (Type,Term)
, _varEnv :: Gamma
, _varCount :: Int
, _cmpCount :: Int
, _components :: HashMap TmName Component
, _primitives :: PrimMap
, _vhdlMState :: VHDLState
, _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
| Bit
| Bool
| Integer
| 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 -> ()
Bit -> ()
Bool -> ()
Integer -> ()
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 Text
| NetDecl Identifier HWType (Maybe Expr)
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 Size) Literal
| DataCon HWType (Maybe Modifier) [Expr]
| Identifier Identifier (Maybe Modifier)
| DataTag HWType (Either Expr Expr)
| BlackBoxE Text (Maybe Modifier)
deriving Show
data Literal
= NumLit Int
| BitLit Bit
| BoolLit Bool
| VecLit [Literal]
deriving Show
data Bit
= H
| L
| U
| Z
deriving Show
makeLenses ''NetlistState