Skip to content

Commit 9cecd57

Browse files
committed
Verilog: constant folding for replication
This adds constant folding for replication operators to the Verilog typechecker.
1 parent a4526fc commit 9cecd57

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

regression/verilog/expressions/replication1.v

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,12 @@ module main(in);
2121
always assert property4:
2222
{{ 1 { 1'b0 }}, in } == in;
2323

24+
// constant folding
25+
parameter P = { 2 { 2'b01 } };
26+
27+
wire [P:0] some_wire;
28+
29+
always assert property5:
30+
P == 'b0101;
31+
2432
endmodule

src/verilog/verilog_typecheck_expr.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,17 @@ exprt verilog_typecheck_exprt::elaborate_constant_expression(exprt expr)
15521552
expr = notequal_exprt(
15531553
reduction_or.op(), from_integer(0, reduction_or.op().type()));
15541554
}
1555+
else if(expr.id() == ID_replication)
1556+
{
1557+
auto &replication = to_replication_expr(expr);
1558+
auto times = numeric_cast_v<std::size_t>(replication.times());
1559+
// lower to a concatenation
1560+
exprt::operandst ops;
1561+
ops.reserve(times);
1562+
for(std::size_t i = 0; i < times; i++)
1563+
ops.push_back(replication.op());
1564+
expr = concatenation_exprt{ops, expr.type()};
1565+
}
15551566

15561567
// We fall back to the simplifier to approximate
15571568
// the standard's definition of 'constant expression'.

0 commit comments

Comments
 (0)