diff --git a/src/Convert/SizeCast.hs b/src/Convert/SizeCast.hs index 4e09ea1..d006c67 100644 --- a/src/Convert/SizeCast.hs +++ b/src/Convert/SizeCast.hs @@ -8,6 +8,7 @@ module Convert.SizeCast (convert) where import Control.Monad.Writer +import Data.List (isPrefixOf) import Convert.ExprUtils import Convert.Scoper @@ -18,17 +19,30 @@ convert :: [AST] -> [AST] convert = map $ traverseDescriptions convertDescription convertDescription :: Description -> Description -convertDescription = partScoper +convertDescription = + traverseModuleItems dropDuplicateCaster . partScoper traverseDeclM traverseModuleItemM traverseGenItemM traverseStmtM traverseDeclM :: Decl -> Scoper Type Decl traverseDeclM decl = do - case decl of - Variable _ t x _ _ -> insertElem x t - Param _ t x _ -> insertElem x t - ParamType _ _ _ -> return () - CommentDecl _ -> return () - traverseDeclExprsM traverseExprM decl + decl' <- case decl of + Variable _ t x _ _ -> do + details <- lookupElemM x + if isPrefixOf "sv2v_cast_" x && details /= Nothing + then return $ Variable Local DuplicateTag x [] Nil + else insertElem x t >> return decl + Param _ t x _ -> insertElem x t >> return decl + ParamType _ _ _ -> return decl + CommentDecl _ -> return decl + traverseDeclExprsM traverseExprM decl' + +pattern DuplicateTag :: Type +pattern DuplicateTag = Alias ":duplicate_cast_to_be_removed:" [] + +dropDuplicateCaster :: ModuleItem -> ModuleItem +dropDuplicateCaster (MIPackageItem (Function _ DuplicateTag _ _ _)) = + Generate [] +dropDuplicateCaster other = other traverseModuleItemM :: ModuleItem -> Scoper Type ModuleItem traverseModuleItemM (Genvar x) = diff --git a/test/basic/duplicate_cast.sv b/test/basic/duplicate_cast.sv new file mode 100644 index 0000000..9d2485e --- /dev/null +++ b/test/basic/duplicate_cast.sv @@ -0,0 +1,14 @@ +package PKG; + localparam P = 1'b1; + typedef struct packed { + logic f; + } foo_t; +endpackage + +module top; + PKG::foo_t b; + logic [1:0] a; + assign b = '{default: PKG::P}; + assign a = '{default: PKG::P}; + initial #1 $display("%b %b", a, b); +endmodule diff --git a/test/basic/duplicate_cast.v b/test/basic/duplicate_cast.v new file mode 100644 index 0000000..7b6ccaf --- /dev/null +++ b/test/basic/duplicate_cast.v @@ -0,0 +1,11 @@ +module top; + wire b; + wire [1:0] a; + function automatic val; + input inp; + val = inp; + endfunction + assign b = val(1); + assign a = {2 {val(1)}}; + initial #1 $display("%b %b", a, b); +endmodule