Skip to content

Commit c55aff0

Browse files
committed
Verilog: fix upper bound when printing extractbits expressions
The Verilog part-select rendered for an extractbits expression selects the bits from index to index + width - 1, but the printer emitted index + width as the upper bound. E.g., bits 4 to 7 of a vector were printed as a[8:4] instead of a[7:4].
1 parent 8aff64b commit c55aff0

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

src/verilog/expr2verilog.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,13 +1122,13 @@ expr2verilogt::resultt expr2verilogt::convert_extractbits(
11221122
if(src.index().is_constant())
11231123
{
11241124
auto index_int = numeric_cast_v<mp_integer>(to_constant_expr(src.index()));
1125-
dest += integer2string(index_int + width);
1125+
dest += integer2string(index_int + width - 1);
11261126
}
11271127
else
11281128
{
11291129
dest += convert_rec(src.index()).s;
11301130
dest += " + ";
1131-
dest += std::to_string(width);
1131+
dest += std::to_string(width - 1);
11321132
}
11331133

11341134
dest+=':';

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ SRC += ebmc/bdd_model_checker.cpp \
1818
trans-netlist/aig.cpp \
1919
trans-netlist/id2smv.cpp \
2020
verilog/convert_literals.cpp \
21+
verilog/expr2verilog.cpp \
2122
verilog/indexed_part_select.cpp \
2223
verilog/typename.cpp \
2324
verilog/verilog_expr.cpp \

unit/verilog/expr2verilog.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*******************************************************************\
2+
3+
Module: expr2verilog Unit Tests
4+
5+
Author: Daniel Kroening, kroening@kroening.com
6+
7+
\*******************************************************************/
8+
9+
#include <util/arith_tools.h>
10+
#include <util/bitvector_expr.h>
11+
#include <util/bitvector_types.h>
12+
#include <util/mathematical_types.h>
13+
#include <util/namespace.h>
14+
#include <util/symbol_table.h>
15+
16+
#include <testing-utils/use_catch.h>
17+
#include <verilog/expr2verilog.h>
18+
19+
SCENARIO("Output of extractbits expressions")
20+
{
21+
symbol_tablet symbol_table;
22+
namespacet ns{symbol_table};
23+
24+
GIVEN("an extractbits expression with constant index")
25+
{
26+
// bits 4 to 7 of a
27+
auto src = symbol_exprt{"a", unsignedbv_typet{32}};
28+
auto extractbits = extractbits_exprt{
29+
src, from_integer(4, integer_typet{}), unsignedbv_typet{4}};
30+
31+
THEN("the part select has upper bound index + width - 1")
32+
{
33+
REQUIRE(expr2verilog(extractbits, ns) == "a[7:4]");
34+
}
35+
}
36+
37+
GIVEN("an extractbits expression with non-constant index")
38+
{
39+
// bits i to i + 3 of a
40+
auto src = symbol_exprt{"a", unsignedbv_typet{32}};
41+
auto index = symbol_exprt{"i", integer_typet{}};
42+
auto extractbits = extractbits_exprt{src, index, unsignedbv_typet{4}};
43+
44+
THEN("the part select has upper bound index + width - 1")
45+
{
46+
REQUIRE(expr2verilog(extractbits, ns) == "a[i + 3:i]");
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)