From c5fdf38612109e01e4b1a9748f86ebf7094e5972 Mon Sep 17 00:00:00 2001 From: Zachary Snow Date: Fri, 19 Apr 2019 19:08:52 -0400 Subject: [PATCH] added conversion which adds names to unnamed blocks with decls --- src/Convert.hs | 2 ++ src/Convert/NamedBlock.hs | 52 +++++++++++++++++++++++++++++++++++++ sv2v.cabal | 1 + test/basic/named_block.sv | 14 ++++++++++ test/basic/named_block.v | 12 +++++++++ test/basic/named_block_tb.v | 1 + 6 files changed, 82 insertions(+) create mode 100644 src/Convert/NamedBlock.hs create mode 100644 test/basic/named_block.sv create mode 100644 test/basic/named_block.v create mode 100644 test/basic/named_block_tb.v diff --git a/src/Convert.hs b/src/Convert.hs index 4d3a6dc..6f02419 100644 --- a/src/Convert.hs +++ b/src/Convert.hs @@ -18,6 +18,7 @@ import qualified Convert.FuncRet import qualified Convert.Interface import qualified Convert.KWArgs import qualified Convert.Logic +import qualified Convert.NamedBlock import qualified Convert.PackedArray import qualified Convert.Return import qualified Convert.StarPort @@ -33,6 +34,7 @@ type Phase = AST -> AST phases :: [Job.Exclude] -> [Phase] phases excludes = [ Convert.AsgnOp.convert + , Convert.NamedBlock.convert , Convert.Assertion.convert , Convert.Bits.convert , selectExclude (Job.Logic , Convert.Logic.convert) diff --git a/src/Convert/NamedBlock.hs b/src/Convert/NamedBlock.hs new file mode 100644 index 0000000..73b0a63 --- /dev/null +++ b/src/Convert/NamedBlock.hs @@ -0,0 +1,52 @@ +{- sv2v + - Author: Zachary Snow + - + - Conversion for unnamed blocks with contain data declarations + -} + +module Convert.NamedBlock (convert) where + +import Control.Monad.State +import qualified Data.Set as Set + +import Convert.Traverse +import Language.SystemVerilog.AST + +type Idents = Set.Set Identifier + +convert :: AST -> AST +convert ast = + -- 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 ast >>= runner traverseStmtM) Set.empty + where runner = traverseDescriptionsM . traverseModuleItemsM . traverseStmtsM + +collectStmtM :: Stmt -> State Idents Stmt +collectStmtM (Block (Just x) decls stmts) = do + modify $ Set.insert x + return $ Block (Just x) decls stmts +collectStmtM other = return other + +traverseStmtM :: Stmt -> State Idents Stmt +traverseStmtM (Block Nothing [] stmts) = + return $ Block Nothing [] stmts +traverseStmtM (Block Nothing decls stmts) = do + names <- get + let x = uniqueBlockName names + modify $ Set.insert x + return $ Block (Just x) decls stmts +traverseStmtM other = return other + +uniqueBlockName :: Idents -> Identifier +uniqueBlockName names = + step ("sv2v_autoblock_" ++ (show $ Set.size names)) 0 + where + step :: Identifier -> Int -> Identifier + 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 diff --git a/sv2v.cabal b/sv2v.cabal index 45d82a4..f90c73f 100644 --- a/sv2v.cabal +++ b/sv2v.cabal @@ -62,6 +62,7 @@ executable sv2v Convert.Interface Convert.KWArgs Convert.Logic + Convert.NamedBlock Convert.PackedArray Convert.Return Convert.StarPort diff --git a/test/basic/named_block.sv b/test/basic/named_block.sv new file mode 100644 index 0000000..21346e0 --- /dev/null +++ b/test/basic/named_block.sv @@ -0,0 +1,14 @@ +module top; + // The below blocks must be named when converted to Verilog-2005 because it + // contains a data declaration. + initial begin + integer i; + i = 1; + $display("%08d", i); + end + initial begin + integer i; + i = 1; + $display("%08d", i); + end +endmodule diff --git a/test/basic/named_block.v b/test/basic/named_block.v new file mode 100644 index 0000000..07fb107 --- /dev/null +++ b/test/basic/named_block.v @@ -0,0 +1,12 @@ +module top; + initial begin : block_name1 + integer i; + i = 1; + $display("%08d", i); + end + initial begin : block_name2 + integer i; + i = 1; + $display("%08d", i); + end +endmodule diff --git a/test/basic/named_block_tb.v b/test/basic/named_block_tb.v new file mode 100644 index 0000000..d11c69f --- /dev/null +++ b/test/basic/named_block_tb.v @@ -0,0 +1 @@ +// intentionally empty