Commit 54ea7d55 by Zachary Snow

simplify automatic procedural block naming

parent 57ef23ef
...@@ -11,47 +11,29 @@ ...@@ -11,47 +11,29 @@
module Convert.NamedBlock (convert) where module Convert.NamedBlock (convert) where
import Control.Monad.State.Strict import Control.Monad.State.Strict
import qualified Data.Set as Set
import Convert.Traverse import Convert.Traverse
import Language.SystemVerilog.AST import Language.SystemVerilog.AST
type Idents = Set.Set Identifier
convert :: [AST] -> [AST] convert :: [AST] -> [AST]
convert asts = convert = map $ traverseDescriptions convertDescription
-- we collect all the existing blocks in the first pass to make sure we
-- don't generate conflicting names on repeated passes of this conversion
evalState (runner collectStmtM asts >>= runner traverseStmtM) Set.empty
where runner = mapM . traverseDescriptionsM . traverseModuleItemsM .
traverseStmtsM . traverseNestedStmtsM
collectStmtM :: Stmt -> State Idents Stmt convertDescription :: Description -> Description
collectStmtM (Block kw x decls stmts) = do convertDescription description =
modify $ Set.insert x evalState (traverseModuleItemsM traverseModuleItem description) 1
return $ Block kw x decls stmts where
collectStmtM other = return other traverseModuleItem = traverseStmtsM $ traverseNestedStmtsM traverseStmtM
traverseStmtM :: Stmt -> State Idents Stmt traverseStmtM :: Stmt -> State Int Stmt
traverseStmtM (Block kw "" [] stmts) = traverseStmtM (Block kw "" [] stmts) =
return $ Block kw "" [] stmts return $ Block kw "" [] stmts
traverseStmtM (Block kw "" decls stmts) = do traverseStmtM (Block kw "" decls stmts) = do
names <- get x <- uniqueBlockName
let x = uniqueBlockName names
modify $ Set.insert x
return $ Block kw x decls stmts return $ Block kw x decls stmts
traverseStmtM other = return other traverseStmtM other = return other
uniqueBlockName :: Idents -> Identifier uniqueBlockName :: State Int String
uniqueBlockName names = uniqueBlockName = do
step ("sv2v_autoblock_" ++ (show $ Set.size names)) 0 cnt <- get
where put $ cnt + 1
step :: Identifier -> Int -> Identifier return $ "sv2v_autoblock_" ++ show cnt
step base n =
if Set.member name names
then step base (n + 1)
else name
where
name = if n == 0
then base
else base ++ "_" ++ show n
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment