From b09fdaf76a75292d7be7183aad040e2831d2b370 Mon Sep 17 00:00:00 2001 From: Zachary Snow Date: Tue, 8 Aug 2023 22:30:36 -0400 Subject: [PATCH] simplify shadowing notes non-trivial localparams too --- CHANGELOG.md | 1 + src/Convert/Simplify.hs | 11 +++++------ test/basic/simplify_localparam_shadow.sv | 10 ++++++++++ 3 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 test/basic/simplify_localparam_shadow.sv diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ea7d50..b09695a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ ### Bug Fixes * Fixed an issue that prevented parsing tasks and functions with `inout` ports +* Fixed errant constant folding of shadowed non-trivial localparams * Fixed certain non-ANSI style port declarations being incorrectly reported as incompatible diff --git a/src/Convert/Simplify.hs b/src/Convert/Simplify.hs index 2734b84..d1aad2c 100644 --- a/src/Convert/Simplify.hs +++ b/src/Convert/Simplify.hs @@ -17,8 +17,6 @@ module Convert.Simplify (convert) where -import Control.Monad (when) - import Convert.ExprUtils import Convert.Scoper import Convert.Traverse @@ -53,13 +51,14 @@ pattern SimpleVector sg l r <- IntegerVector _ sg [(RawNum l, RawNum r)] insertExpr :: Identifier -> Expr -> Scoper Expr () insertExpr ident expr = do expr' <- substituteExprM expr - case expr' of + insertElem ident $ case expr' of Cast (Left (SimpleVector sg l r)) (Number n) -> - insertElem ident $ Number $ numberCast signed size n + Number $ numberCast signed size n where signed = sg == Signed size = fromIntegral $ abs $ l - r + 1 - _ -> when (isSimpleExpr expr') $ insertElem ident expr' + _ | isSimpleExpr expr' -> expr' + _ -> Nil isSimpleExpr :: Expr -> Bool isSimpleExpr Number{} = True @@ -84,7 +83,7 @@ traverseGenItemM :: GenItem -> Scoper Expr GenItem traverseGenItemM = traverseGenItemExprsM traverseExprM traverseStmtM :: Stmt -> Scoper Expr Stmt -traverseStmtM stmt = traverseStmtExprsM traverseExprM stmt +traverseStmtM = traverseStmtExprsM traverseExprM traverseExprM :: Expr -> Scoper Expr Expr traverseExprM = embedScopes convertExpr diff --git a/test/basic/simplify_localparam_shadow.sv b/test/basic/simplify_localparam_shadow.sv new file mode 100644 index 0000000..0146d80 --- /dev/null +++ b/test/basic/simplify_localparam_shadow.sv @@ -0,0 +1,10 @@ +module top; + localparam i = 1234; + if (1) begin + genvar j; + for (j = 0; j < 10; j = j + 1) begin + localparam i = j; + initial $display(i > 5 ? i + 100 : i - 100); + end + end +endmodule