2019-03-07 02:30:47 +01:00
|
|
|
{- sv2v
|
|
|
|
|
- Author: Zachary Snow <zach@zachjs.com>
|
|
|
|
|
-
|
|
|
|
|
- Conversion for interfaces
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
module Convert.Interface (convert) where
|
|
|
|
|
|
2019-04-23 21:46:08 +02:00
|
|
|
import Data.Maybe (fromJust, mapMaybe)
|
2019-03-07 02:30:47 +01:00
|
|
|
import Control.Monad.Writer
|
|
|
|
|
import qualified Data.Map.Strict as Map
|
2019-04-02 19:33:18 +02:00
|
|
|
import qualified Data.Set as Set
|
2019-03-07 02:30:47 +01:00
|
|
|
|
|
|
|
|
import Convert.Traverse
|
|
|
|
|
import Language.SystemVerilog.AST
|
|
|
|
|
|
|
|
|
|
type Instances = Map.Map Identifier Identifier
|
|
|
|
|
type Interface = ([Identifier], [ModuleItem])
|
|
|
|
|
type Interfaces = Map.Map Identifier Interface
|
|
|
|
|
type Modports = Map.Map Identifier [ModportDecl]
|
2020-02-09 19:42:45 +01:00
|
|
|
type Modules = Map.Map Identifier [(Identifier, Type)]
|
2019-03-07 02:30:47 +01:00
|
|
|
|
2019-04-24 00:44:45 +02:00
|
|
|
convert :: [AST] -> [AST]
|
2019-04-30 21:44:52 +02:00
|
|
|
convert =
|
2020-04-14 04:23:03 +02:00
|
|
|
map (filter $ not . isInterface) .
|
|
|
|
|
repeatedConverter
|
2019-03-07 02:30:47 +01:00
|
|
|
where
|
2020-04-14 04:23:03 +02:00
|
|
|
repeatedConverter :: [AST] -> [AST]
|
|
|
|
|
repeatedConverter files =
|
|
|
|
|
if files == files'
|
|
|
|
|
then files
|
|
|
|
|
else repeatedConverter files'
|
|
|
|
|
where
|
|
|
|
|
files' =
|
|
|
|
|
traverseFiles (collectDescriptionsM collectDesc)
|
|
|
|
|
(map . uncurry convertDescription)
|
|
|
|
|
files
|
2019-03-26 20:10:16 +01:00
|
|
|
-- we can only collect/map non-extern interfaces
|
2019-04-01 07:23:44 +02:00
|
|
|
collectDesc :: Description -> Writer (Interfaces, Modules) ()
|
2019-09-16 05:17:14 +02:00
|
|
|
collectDesc (orig @ (Part _ False kw _ name ports items)) = do
|
2019-04-01 07:23:44 +02:00
|
|
|
if kw == Interface
|
|
|
|
|
then tell (Map.singleton name (ports, items), Map.empty)
|
2020-02-09 19:42:45 +01:00
|
|
|
else tell (Map.empty, Map.singleton name decls)
|
2019-10-21 04:06:10 +02:00
|
|
|
where
|
2020-02-09 19:42:45 +01:00
|
|
|
decls = execWriter $
|
|
|
|
|
collectModuleItemsM (collectDeclsM collectDecl) orig
|
|
|
|
|
collectDecl :: Decl -> Writer [(Identifier, Type)] ()
|
2019-10-21 04:06:10 +02:00
|
|
|
collectDecl (Variable _ t ident _ _) =
|
2020-02-09 19:42:45 +01:00
|
|
|
tell [(ident, t)]
|
2019-10-21 04:06:10 +02:00
|
|
|
collectDecl _ = return ()
|
2019-03-07 02:30:47 +01:00
|
|
|
collectDesc _ = return ()
|
|
|
|
|
isInterface :: Description -> Bool
|
2019-09-16 05:17:14 +02:00
|
|
|
isInterface (Part _ False Interface _ _ _ _) = True
|
2019-03-07 02:30:47 +01:00
|
|
|
isInterface _ = False
|
|
|
|
|
|
2019-04-01 07:23:44 +02:00
|
|
|
convertDescription :: Interfaces -> Modules -> Description -> Description
|
2019-09-16 05:17:14 +02:00
|
|
|
convertDescription interfaces modules (Part attrs extern Module lifetime name ports items) =
|
|
|
|
|
Part attrs extern Module lifetime name ports' items'
|
2019-03-07 02:30:47 +01:00
|
|
|
where
|
2019-03-07 03:55:27 +01:00
|
|
|
items' =
|
2019-04-02 19:33:18 +02:00
|
|
|
map (traverseNestedModuleItems $ traverseExprs' ExcludeTFs (traverseNestedExprs $ convertExpr instances modports)) $
|
|
|
|
|
map (traverseNestedModuleItems $ traverseLHSs' ExcludeTFs (traverseNestedLHSs $ convertLHS instances modports)) $
|
2019-03-07 03:55:27 +01:00
|
|
|
map (traverseNestedModuleItems mapInterface) $
|
|
|
|
|
items
|
|
|
|
|
ports' = concatMap convertPort ports
|
2019-03-07 02:30:47 +01:00
|
|
|
|
|
|
|
|
-- collect the interface type of all interface instances in this module
|
2019-03-07 03:55:27 +01:00
|
|
|
(instances, modports) = execWriter $ mapM
|
|
|
|
|
(collectNestedModuleItemsM collectInterface) items
|
|
|
|
|
collectInterface :: ModuleItem -> Writer (Instances, Modports) ()
|
2019-04-23 23:12:56 +02:00
|
|
|
collectInterface (MIPackageItem (Decl (Variable _ t ident _ _))) =
|
2019-03-07 03:55:27 +01:00
|
|
|
case t of
|
|
|
|
|
InterfaceT interfaceName (Just modportName) [] ->
|
|
|
|
|
tell (Map.empty, Map.singleton ident modportDecls)
|
2019-10-21 04:06:10 +02:00
|
|
|
where Just modportDecls = lookupModport interfaceName modportName
|
2020-06-04 02:18:14 +02:00
|
|
|
Alias Nothing interfaceName [] ->
|
|
|
|
|
case impliedModport interfaceName of
|
|
|
|
|
Just modportDecls ->
|
|
|
|
|
tell (Map.empty, Map.singleton ident modportDecls)
|
|
|
|
|
Nothing -> return ()
|
2019-03-07 03:55:27 +01:00
|
|
|
_ -> return ()
|
2019-03-25 23:53:55 +01:00
|
|
|
collectInterface (Instance part _ ident Nothing _) =
|
2019-03-07 02:30:47 +01:00
|
|
|
if Map.member part interfaces
|
2019-03-07 03:55:27 +01:00
|
|
|
then tell (Map.singleton ident part, Map.empty)
|
2019-03-07 02:30:47 +01:00
|
|
|
else return ()
|
2019-03-07 03:55:27 +01:00
|
|
|
collectInterface _ = return ()
|
2019-03-07 02:30:47 +01:00
|
|
|
|
2019-03-07 03:55:27 +01:00
|
|
|
mapInterface :: ModuleItem -> ModuleItem
|
2020-06-04 02:18:14 +02:00
|
|
|
mapInterface (orig @ (MIPackageItem (Decl (Variable _ t ident _ _)))) =
|
2019-10-21 04:06:10 +02:00
|
|
|
-- expand instantiation of a modport
|
2019-03-07 03:55:27 +01:00
|
|
|
case Map.lookup ident modports of
|
|
|
|
|
Just modportDecls -> Generate $
|
2019-04-23 23:12:56 +02:00
|
|
|
map (GenModuleItem . MIPackageItem . Decl . mapper)
|
|
|
|
|
modportDecls
|
2019-03-07 03:55:27 +01:00
|
|
|
Nothing -> orig
|
|
|
|
|
where
|
2020-06-04 02:18:14 +02:00
|
|
|
interfaceName = case t of
|
|
|
|
|
InterfaceT x (Just _) [] -> x
|
|
|
|
|
Alias Nothing x [] -> x
|
|
|
|
|
_ -> error $ "unexpected modport type " ++ show t
|
2020-02-08 01:10:21 +01:00
|
|
|
interfaceItems =
|
|
|
|
|
case Map.lookup interfaceName interfaces of
|
|
|
|
|
Just res -> snd res
|
|
|
|
|
Nothing -> error $ "could not find interface " ++ show interfaceName
|
2019-04-23 21:46:08 +02:00
|
|
|
mapper (dir, port, expr) =
|
|
|
|
|
Variable dir mpt (ident ++ "_" ++ port) mprs Nothing
|
|
|
|
|
where (mpt, mprs) = lookupType interfaceItems (fromJust expr)
|
2019-03-25 23:53:55 +01:00
|
|
|
mapInterface (Instance part params ident Nothing instancePorts) =
|
2019-10-21 04:06:10 +02:00
|
|
|
-- expand modport port bindings
|
2019-03-07 02:30:47 +01:00
|
|
|
case Map.lookup part interfaces of
|
|
|
|
|
Just interface ->
|
2019-10-21 04:06:10 +02:00
|
|
|
-- inline instantiation of an interface
|
2019-10-20 21:58:37 +02:00
|
|
|
Generate $ map GenModuleItem $
|
|
|
|
|
inlineInterface interface (ident, params, expandedPorts)
|
2019-03-25 23:53:55 +01:00
|
|
|
Nothing -> Instance part params ident Nothing expandedPorts
|
2020-02-09 19:42:45 +01:00
|
|
|
where expandedPorts = concatMap (uncurry $ expandPortBinding part) (zip instancePorts [0..])
|
2019-04-02 19:33:18 +02:00
|
|
|
mapInterface (orig @ (MIPackageItem (Function _ _ _ decls _))) =
|
|
|
|
|
convertTF decls orig
|
|
|
|
|
mapInterface (orig @ (MIPackageItem (Task _ _ decls _))) =
|
|
|
|
|
convertTF decls orig
|
2019-03-07 03:55:27 +01:00
|
|
|
mapInterface other = other
|
2019-03-07 02:30:47 +01:00
|
|
|
|
2019-04-02 19:33:18 +02:00
|
|
|
convertTF :: [Decl] -> ModuleItem -> ModuleItem
|
2019-10-21 04:06:10 +02:00
|
|
|
convertTF decls =
|
|
|
|
|
traverseExprs (traverseNestedExprs $ convertExpr its mps) .
|
|
|
|
|
traverseLHSs (traverseNestedLHSs $ convertLHS its mps)
|
2019-04-02 19:33:18 +02:00
|
|
|
where
|
|
|
|
|
locals = Set.fromList $ mapMaybe declVarIdent decls
|
|
|
|
|
its = Map.withoutKeys instances locals
|
|
|
|
|
mps = Map.withoutKeys modports locals
|
2019-10-21 04:06:10 +02:00
|
|
|
declVarIdent :: Decl -> Maybe Identifier
|
|
|
|
|
declVarIdent (Variable _ _ x _ _) = Just x
|
|
|
|
|
declVarIdent _ = Nothing
|
2019-04-02 19:33:18 +02:00
|
|
|
|
2020-02-09 19:42:45 +01:00
|
|
|
expandPortBinding :: Identifier -> PortBinding -> Int -> [PortBinding]
|
|
|
|
|
expandPortBinding _ (origBinding @ (portName, Just (Dot (Ident instanceName) modportName))) _ =
|
2019-10-21 04:06:10 +02:00
|
|
|
-- expand instance modport bound to a modport
|
|
|
|
|
if Map.member instanceName instances && modportDecls /= Nothing
|
2020-06-04 02:18:14 +02:00
|
|
|
then expandPortBinding' portName instanceName $ fromJust modportDecls
|
2019-10-21 04:06:10 +02:00
|
|
|
else [origBinding]
|
|
|
|
|
where
|
|
|
|
|
interfaceName = instances Map.! instanceName
|
|
|
|
|
modportDecls = lookupModport interfaceName modportName
|
2020-02-09 19:42:45 +01:00
|
|
|
expandPortBinding moduleName (origBinding @ (portName, Just (Ident ident))) idx =
|
2019-10-21 04:06:10 +02:00
|
|
|
case (instances Map.!? ident, modports Map.!? ident) of
|
2019-04-01 07:23:44 +02:00
|
|
|
(Nothing, Nothing) -> [origBinding]
|
2020-06-04 02:18:14 +02:00
|
|
|
(Just interfaceName, _) ->
|
2019-10-21 04:06:10 +02:00
|
|
|
-- given entire interface, but just bound to a modport
|
2020-06-04 02:18:14 +02:00
|
|
|
if Map.notMember moduleName modules then
|
|
|
|
|
error $ "could not find module " ++ show moduleName
|
|
|
|
|
else if modportDecls == Nothing then
|
|
|
|
|
[origBinding]
|
|
|
|
|
else
|
|
|
|
|
expandPortBinding' portName ident $ fromJust modportDecls
|
2019-04-01 07:23:44 +02:00
|
|
|
where
|
2020-06-04 02:18:14 +02:00
|
|
|
Just decls = Map.lookup moduleName modules
|
|
|
|
|
portType =
|
|
|
|
|
if null portName
|
|
|
|
|
then if idx < length decls
|
|
|
|
|
then snd $ decls !! idx
|
|
|
|
|
else error $ "could not infer port "
|
|
|
|
|
++ show origBinding ++ " in module "
|
2020-02-08 01:10:21 +01:00
|
|
|
++ show moduleName
|
2020-06-04 02:18:14 +02:00
|
|
|
else case lookup portName decls of
|
|
|
|
|
Nothing -> error $ "could not find port "
|
|
|
|
|
++ show portName ++ " in module "
|
|
|
|
|
++ show moduleName
|
|
|
|
|
Just t -> t
|
|
|
|
|
modportDecls =
|
|
|
|
|
case portType of
|
|
|
|
|
InterfaceT _ (Just modportName) [] ->
|
|
|
|
|
lookupModport interfaceName modportName
|
|
|
|
|
Alias Nothing _ [] ->
|
|
|
|
|
impliedModport interfaceName
|
|
|
|
|
_ -> Nothing
|
|
|
|
|
(_, Just modportDecls) ->
|
2019-10-21 04:06:10 +02:00
|
|
|
-- modport directly bound to a modport
|
2020-06-04 02:18:14 +02:00
|
|
|
expandPortBinding' portName ident $ map redirect modportDecls
|
|
|
|
|
where redirect (d, x, _) = (d, x, Just $ Ident x)
|
2020-02-09 19:42:45 +01:00
|
|
|
expandPortBinding _ other _ = [other]
|
2019-03-07 02:30:47 +01:00
|
|
|
|
2020-06-04 02:18:14 +02:00
|
|
|
expandPortBinding' :: Identifier -> Identifier -> [ModportDecl] -> [PortBinding]
|
|
|
|
|
expandPortBinding' portName instanceName modportDecls =
|
|
|
|
|
map mapper modportDecls
|
|
|
|
|
where
|
|
|
|
|
mapper (_, x, me) = (x', me')
|
|
|
|
|
where
|
|
|
|
|
x' = if null portName then "" else portName ++ '_' : x
|
|
|
|
|
me' = fmap (traverseNestedExprs prefixExpr) me
|
|
|
|
|
prefixExpr :: Expr -> Expr
|
|
|
|
|
prefixExpr (Ident x) = Ident (instanceName ++ '_' : x)
|
|
|
|
|
prefixExpr other = other
|
|
|
|
|
|
2019-10-21 04:06:10 +02:00
|
|
|
lookupModport :: Identifier -> Identifier -> Maybe [ModportDecl]
|
|
|
|
|
lookupModport interfaceName =
|
2019-10-20 20:53:37 +02:00
|
|
|
if Map.member interfaceName interfaces
|
|
|
|
|
then (Map.!?) modportMap
|
|
|
|
|
else error $ "could not find interface " ++ show interfaceName
|
2019-03-07 02:30:47 +01:00
|
|
|
where
|
2019-10-21 04:06:10 +02:00
|
|
|
interfaceItems = snd $ interfaces Map.! interfaceName
|
2019-03-07 02:30:47 +01:00
|
|
|
modportMap = execWriter $
|
|
|
|
|
mapM (collectNestedModuleItemsM collectModport) $
|
|
|
|
|
interfaceItems
|
|
|
|
|
collectModport :: ModuleItem -> Writer Modports ()
|
2019-03-07 03:55:27 +01:00
|
|
|
collectModport (Modport ident l) = tell $ Map.singleton ident l
|
2019-03-07 02:30:47 +01:00
|
|
|
collectModport _ = return ()
|
|
|
|
|
|
2020-06-04 02:18:14 +02:00
|
|
|
impliedModport :: Identifier -> Maybe [ModportDecl]
|
|
|
|
|
impliedModport interfaceName =
|
|
|
|
|
if Map.member interfaceName interfaces
|
|
|
|
|
then Just modport
|
|
|
|
|
else Nothing
|
|
|
|
|
where
|
|
|
|
|
interfaceItems = snd $ interfaces Map.! interfaceName
|
|
|
|
|
modport = execWriter $
|
|
|
|
|
mapM (collectNestedModuleItemsM collectModportDecls) $
|
|
|
|
|
interfaceItems
|
|
|
|
|
collectModportDecls :: ModuleItem -> Writer [ModportDecl] ()
|
|
|
|
|
collectModportDecls (MIPackageItem (Decl (Variable d _ x _ _))) =
|
|
|
|
|
tell [(d', x, Just $ Ident x)]
|
|
|
|
|
where d' = if d == Local then Inout else d
|
|
|
|
|
collectModportDecls _ = return ()
|
|
|
|
|
|
2019-04-02 19:33:18 +02:00
|
|
|
convertExpr :: Instances -> Modports -> Expr -> Expr
|
|
|
|
|
convertExpr its mps (orig @ (Dot (Ident x) y)) =
|
|
|
|
|
if Map.member x mps || Map.member x its
|
2019-03-07 03:55:27 +01:00
|
|
|
then Ident (x ++ "_" ++ y)
|
|
|
|
|
else orig
|
2019-04-02 19:33:18 +02:00
|
|
|
convertExpr _ _ other = other
|
|
|
|
|
convertLHS :: Instances -> Modports -> LHS -> LHS
|
|
|
|
|
convertLHS its mps (orig @ (LHSDot (LHSIdent x) y)) =
|
|
|
|
|
if Map.member x mps || Map.member x its
|
2019-03-07 03:55:27 +01:00
|
|
|
then LHSIdent (x ++ "_" ++ y)
|
|
|
|
|
else orig
|
2019-04-02 19:33:18 +02:00
|
|
|
convertLHS _ _ other = other
|
2019-03-07 03:55:27 +01:00
|
|
|
convertPort :: Identifier -> [Identifier]
|
|
|
|
|
convertPort ident =
|
|
|
|
|
case Map.lookup ident modports of
|
|
|
|
|
Nothing -> [ident]
|
|
|
|
|
Just decls -> map (\(_, x, _) -> ident ++ "_" ++ x) decls
|
|
|
|
|
|
2019-04-01 07:23:44 +02:00
|
|
|
convertDescription _ _ other = other
|
2019-03-07 02:30:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
-- add a prefix to all standard identifiers in a module item
|
2020-02-10 03:57:09 +01:00
|
|
|
prefixModuleItems :: (Identifier -> Identifier) -> ModuleItem -> ModuleItem
|
2019-03-07 02:30:47 +01:00
|
|
|
prefixModuleItems prefix =
|
2020-02-10 03:57:09 +01:00
|
|
|
prefixOtherItem .
|
2019-03-07 02:30:47 +01:00
|
|
|
traverseDecls prefixDecl .
|
2019-03-18 19:27:14 +01:00
|
|
|
traverseExprs (traverseNestedExprs prefixExpr) .
|
|
|
|
|
traverseLHSs (traverseNestedLHSs prefixLHS )
|
2019-03-07 02:30:47 +01:00
|
|
|
where
|
|
|
|
|
prefixDecl :: Decl -> Decl
|
2020-02-10 03:57:09 +01:00
|
|
|
prefixDecl (Variable d t x a me) = Variable d t (prefix x) a me
|
|
|
|
|
prefixDecl (Param s t x e) = Param s t (prefix x) e
|
|
|
|
|
prefixDecl (ParamType s x mt) = ParamType s (prefix x) mt
|
|
|
|
|
prefixDecl (CommentDecl c) = CommentDecl c
|
2019-03-07 02:30:47 +01:00
|
|
|
prefixExpr :: Expr -> Expr
|
2020-02-10 03:57:09 +01:00
|
|
|
prefixExpr (Ident x) = Ident (prefix x)
|
2019-03-07 02:30:47 +01:00
|
|
|
prefixExpr other = other
|
|
|
|
|
prefixLHS :: LHS -> LHS
|
2020-02-10 03:57:09 +01:00
|
|
|
prefixLHS (LHSIdent x) = LHSIdent (prefix x)
|
2019-03-07 02:30:47 +01:00
|
|
|
prefixLHS other = other
|
2020-04-14 04:23:03 +02:00
|
|
|
prefixOtherItem :: ModuleItem -> ModuleItem
|
2020-02-10 03:57:09 +01:00
|
|
|
prefixOtherItem (MIPackageItem item) =
|
2019-10-19 22:22:39 +02:00
|
|
|
MIPackageItem $ prefixPackageItem prefix item
|
2020-04-14 04:23:03 +02:00
|
|
|
prefixOtherItem (Instance m params name rs ports) =
|
|
|
|
|
Instance m params (prefix name) rs ports
|
2020-02-10 03:57:09 +01:00
|
|
|
prefixOtherItem (Genvar x) = Genvar $ prefix x
|
|
|
|
|
prefixOtherItem other = other
|
2019-10-19 22:22:39 +02:00
|
|
|
|
|
|
|
|
-- add a prefix to all standard identifiers in a package item
|
2020-02-10 03:57:09 +01:00
|
|
|
prefixPackageItem :: (Identifier -> Identifier) -> PackageItem -> PackageItem
|
2019-10-19 22:22:39 +02:00
|
|
|
prefixPackageItem prefix (Function lifetime t x decls stmts) =
|
|
|
|
|
Function lifetime t x' decls stmts
|
2020-02-10 03:57:09 +01:00
|
|
|
where x' = prefix x
|
2019-10-19 22:22:39 +02:00
|
|
|
prefixPackageItem prefix (Task lifetime x decls stmts) =
|
|
|
|
|
Task lifetime x' decls stmts
|
2020-02-10 03:57:09 +01:00
|
|
|
where x' = prefix x
|
2019-10-19 22:22:39 +02:00
|
|
|
prefixPackageItem _ other = other
|
2019-03-07 02:30:47 +01:00
|
|
|
|
2020-02-10 03:57:09 +01:00
|
|
|
-- collect all identifiers defined within a module item
|
|
|
|
|
collectIdentsM :: ModuleItem -> Writer (Set.Set Identifier) ()
|
|
|
|
|
collectIdentsM (MIPackageItem (Function _ _ x _ _)) = tell $ Set.singleton x
|
|
|
|
|
collectIdentsM (MIPackageItem (Task _ x _ _)) = tell $ Set.singleton x
|
2020-04-14 04:23:03 +02:00
|
|
|
collectIdentsM (Instance _ _ x _ _) = tell $ Set.singleton x
|
2020-02-10 03:57:09 +01:00
|
|
|
collectIdentsM (Genvar x) = tell $ Set.singleton x
|
|
|
|
|
collectIdentsM item = collectDeclsM collectDecl item
|
|
|
|
|
where
|
|
|
|
|
collectDecl :: Decl -> Writer (Set.Set Identifier) ()
|
|
|
|
|
collectDecl (Variable _ _ x _ _) = tell $ Set.singleton x
|
|
|
|
|
collectDecl (Param _ _ x _) = tell $ Set.singleton x
|
|
|
|
|
collectDecl (ParamType _ x _) = tell $ Set.singleton x
|
|
|
|
|
collectDecl (CommentDecl _) = return ()
|
|
|
|
|
|
2019-04-23 21:46:08 +02:00
|
|
|
lookupType :: [ModuleItem] -> Expr -> (Type, [Range])
|
2019-03-07 03:55:27 +01:00
|
|
|
lookupType items (Ident ident) =
|
2019-04-23 21:46:08 +02:00
|
|
|
case mapMaybe findType items of
|
|
|
|
|
[] -> error $ "unable to locate type of " ++ ident
|
|
|
|
|
ts -> head ts
|
2019-03-07 03:55:27 +01:00
|
|
|
where
|
2019-04-23 21:46:08 +02:00
|
|
|
findType :: ModuleItem -> Maybe (Type, [Range])
|
2020-06-04 02:18:14 +02:00
|
|
|
findType (MIPackageItem (Decl (Variable _ t x rs _))) =
|
2019-04-23 21:46:08 +02:00
|
|
|
if x == ident then Just (t, rs) else Nothing
|
2019-03-07 03:55:27 +01:00
|
|
|
findType _ = Nothing
|
2019-04-17 07:44:03 +02:00
|
|
|
lookupType _ expr =
|
|
|
|
|
-- TODO: Add support for non-Ident modport expressions.
|
|
|
|
|
error $ "interface conversion does not support modport expressions that "
|
|
|
|
|
++ " are not identifiers: " ++ show expr
|
2019-03-07 03:55:27 +01:00
|
|
|
|
2019-03-07 02:30:47 +01:00
|
|
|
-- convert an interface instantiation into a series of equivalent module items
|
2019-10-20 21:58:37 +02:00
|
|
|
inlineInterface :: Interface -> (Identifier, [ParamBinding], [PortBinding]) -> [ModuleItem]
|
|
|
|
|
inlineInterface (ports, items) (instanceName, instanceParams, instancePorts) =
|
2020-01-31 04:17:17 +01:00
|
|
|
(:) comment $
|
2019-03-09 00:23:38 +01:00
|
|
|
flip (++) portBindings $
|
2019-03-07 02:30:47 +01:00
|
|
|
map (traverseNestedModuleItems removeModport) $
|
2019-04-23 23:12:56 +02:00
|
|
|
map (traverseNestedModuleItems removeDeclDir) $
|
2019-04-11 20:08:50 +02:00
|
|
|
itemsPrefixed
|
2019-03-07 02:30:47 +01:00
|
|
|
where
|
2020-01-31 04:17:17 +01:00
|
|
|
comment = MIPackageItem $ Decl $ CommentDecl $
|
|
|
|
|
"expanded instance: " ++ instanceName
|
2019-03-07 02:30:47 +01:00
|
|
|
prefix = instanceName ++ "_"
|
2020-02-10 03:57:09 +01:00
|
|
|
idents = execWriter $
|
|
|
|
|
mapM (collectNestedModuleItemsM collectIdentsM) items
|
|
|
|
|
prefixIfNecessary :: Identifier -> Identifier
|
|
|
|
|
prefixIfNecessary x =
|
|
|
|
|
if Set.member x idents
|
|
|
|
|
then prefix ++ x
|
|
|
|
|
else x
|
2019-10-20 21:58:37 +02:00
|
|
|
itemsPrefixed =
|
2020-02-10 03:57:09 +01:00
|
|
|
map (traverseNestedModuleItems $ prefixModuleItems prefixIfNecessary) $
|
2019-10-20 21:58:37 +02:00
|
|
|
map (traverseDecls overrideParam) $
|
|
|
|
|
items
|
2019-03-07 02:30:47 +01:00
|
|
|
origInstancePortNames = map fst instancePorts
|
|
|
|
|
instancePortExprs = map snd instancePorts
|
|
|
|
|
instancePortNames =
|
|
|
|
|
map (prefix ++) $
|
|
|
|
|
if all ("" ==) origInstancePortNames
|
|
|
|
|
then ports
|
|
|
|
|
else origInstancePortNames
|
|
|
|
|
portBindings =
|
2019-04-11 20:08:50 +02:00
|
|
|
mapMaybe portBindingItem $
|
2019-03-07 02:30:47 +01:00
|
|
|
zip instancePortNames instancePortExprs
|
|
|
|
|
|
2019-04-23 23:12:56 +02:00
|
|
|
removeDeclDir :: ModuleItem -> ModuleItem
|
|
|
|
|
removeDeclDir (MIPackageItem (Decl (Variable _ t x a me))) =
|
|
|
|
|
MIPackageItem $ Decl $ Variable Local t x a me
|
|
|
|
|
removeDeclDir other = other
|
2019-03-07 02:30:47 +01:00
|
|
|
removeModport :: ModuleItem -> ModuleItem
|
2019-03-07 19:19:31 +01:00
|
|
|
removeModport (Modport x _) =
|
2020-01-31 04:17:17 +01:00
|
|
|
MIPackageItem $ Decl $ CommentDecl $ "removed modport " ++ x
|
2019-03-07 02:30:47 +01:00
|
|
|
removeModport other = other
|
2019-04-11 20:08:50 +02:00
|
|
|
|
2019-10-20 21:58:37 +02:00
|
|
|
instanceParamMap = Map.fromList instanceParams
|
|
|
|
|
overrideParam :: Decl -> Decl
|
|
|
|
|
overrideParam (Param Parameter t x e) =
|
|
|
|
|
case Map.lookup x instanceParamMap of
|
|
|
|
|
Nothing -> Param Parameter t x e
|
|
|
|
|
Just (Right e') -> Param Parameter t x e'
|
|
|
|
|
Just (Left t') ->
|
|
|
|
|
error $ "interface param expected expression, found type: "
|
|
|
|
|
++ show t'
|
|
|
|
|
overrideParam (ParamType Parameter x mt) =
|
|
|
|
|
case Map.lookup x instanceParamMap of
|
|
|
|
|
Nothing -> ParamType Parameter x mt
|
|
|
|
|
Just (Left t') -> ParamType Parameter x (Just t')
|
|
|
|
|
Just (Right e') ->
|
|
|
|
|
error $ "interface param expected type, found expression: "
|
|
|
|
|
++ show e'
|
|
|
|
|
overrideParam other = other
|
|
|
|
|
|
2019-04-11 20:08:50 +02:00
|
|
|
portBindingItem :: PortBinding -> Maybe ModuleItem
|
|
|
|
|
portBindingItem (ident, Just expr) =
|
|
|
|
|
Just $ if declDirs Map.! ident == Input
|
2020-03-21 02:13:57 +01:00
|
|
|
then Assign AssignOptionNone (LHSIdent ident) expr
|
|
|
|
|
else Assign AssignOptionNone (toLHS expr) (Ident ident)
|
2019-04-11 20:08:50 +02:00
|
|
|
portBindingItem (_, Nothing) = Nothing
|
|
|
|
|
|
|
|
|
|
declDirs = execWriter $
|
|
|
|
|
mapM (collectDeclsM collectDeclDir) itemsPrefixed
|
|
|
|
|
collectDeclDir :: Decl -> Writer (Map.Map Identifier Direction) ()
|
|
|
|
|
collectDeclDir (Variable dir _ ident _ _) =
|
|
|
|
|
if dir /= Local
|
|
|
|
|
then tell $ Map.singleton ident dir
|
|
|
|
|
else return ()
|
|
|
|
|
collectDeclDir _ = return ()
|
|
|
|
|
|
2019-04-23 02:44:35 +02:00
|
|
|
toLHS :: Expr -> LHS
|
|
|
|
|
toLHS expr =
|
|
|
|
|
case exprToLHS expr of
|
|
|
|
|
Just lhs -> lhs
|
|
|
|
|
Nothing -> error $ "trying to bind an interface output to " ++
|
|
|
|
|
show expr ++ " but that can't be an LHS"
|