Allow localparams in constant functions
authorZachary Snow <zach@zachjs.com>
Fri, 21 Aug 2020 00:09:54 +0000 (20:09 -0400)
committerZachary Snow <zach@zachjs.com>
Fri, 21 Aug 2020 00:10:24 +0000 (20:10 -0400)
frontends/ast/simplify.cc
tests/various/const_func_block_var.v

index 7f9795d297ff371f3baa9e80f97f459453d9c2f3..b8e4f941c4ccddf7a3fa89813459e8d7bb338c5c 100644 (file)
@@ -4488,6 +4488,18 @@ AstNode *AstNode::eval_const_function(AstNode *fcall)
 
                log_assert(variables.count(str) != 0);
 
+               if (stmt->type == AST_LOCALPARAM)
+               {
+                       while (stmt->simplify(true, false, false, 1, -1, false, true)) { }
+
+                       if (!backup_scope.count(stmt->str))
+                               backup_scope[stmt->str] = current_scope[stmt->str];
+                       current_scope[stmt->str] = stmt;
+
+                       block->children.erase(block->children.begin());
+                       continue;
+               }
+
                if (stmt->type == AST_ASSIGN_EQ)
                {
                        if (stmt->children.at(0)->type == AST_IDENTIFIER && stmt->children.at(0)->children.size() != 0 &&
index 98e83aa5b7acd4a846fe8b07a852b4e39fac19f5..cb60844ab241ff5b9f9b381abfee25bc939ac602 100644 (file)
@@ -1,15 +1,18 @@
 module top(out);
        function integer operation;
                input integer num;
+               localparam incr = 1;
+               localparam mult = 1;
                begin
                        operation = 0;
                        begin : op_i
                                integer i;
-                               for (i = 0; i < 2; i = i + 1)
+                               for (i = 0; i * mult < 2; i = i + incr)
                                begin : op_j
                                        integer j;
-                                       for (j = i; j < i * 2; j = j + 1)
-                                               num = num + 1;
+                                       localparam other_mult = 2;
+                                       for (j = i; j < i * other_mult; j = j + incr)
+                                               num = num + incr;
                                end
                                num = num * 2;
                        end