Skip to content

Commit 2ed72b0

Browse files
committed
format_expr now prints bv-typed constants
This adds formatting for bv-type constants to format_expr. As the bv type does not have a numerical representation, the 0/1 bit pattern is printed, starting with the bit with the highest index.
1 parent 5002f3b commit 2ed72b0

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/util/format_expr.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ static std::ostream &format_rec(std::ostream &os, const constant_exprt &src)
186186
type == ID_unsignedbv || type == ID_signedbv || type == ID_c_bool ||
187187
type == ID_c_bit_field)
188188
return os << *numeric_cast<mp_integer>(src);
189+
else if(type == ID_bv)
190+
{
191+
// These do not have a numerical interpretation.
192+
// We'll print the 0/1 bit pattern, starting with the bit
193+
// that has the highest index.
194+
auto width = to_bv_type(src.type()).get_width();
195+
std::string result;
196+
result.reserve(width);
197+
auto &value = src.get_value();
198+
for(std::size_t i = 0; i < width; i++)
199+
result += get_bvrep_bit(value, width, width - i - 1) ? '1' : '0';
200+
return os << result;
201+
}
189202
else if(type == ID_integer || type == ID_natural || type == ID_range)
190203
return os << src.get_value();
191204
else if(type == ID_string)

unit/util/format_expr.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
77
\*******************************************************************/
88

9-
#include <util/expr.h>
9+
#include <util/arith_tools.h>
10+
#include <util/bitvector_types.h>
1011
#include <util/format_expr.h>
12+
#include <util/std_expr.h>
1113

1214
#include <testing-utils/use_catch.h>
1315

@@ -23,3 +25,10 @@ TEST_CASE(
2325
});
2426
REQUIRE(format_to_string(exprt{custom_id}) == "output");
2527
}
28+
29+
TEST_CASE("Format a bv-typed constant", "[core][util][format_expr]")
30+
{
31+
auto value = make_bvrep(4, [](std::size_t index) { return index != 2; });
32+
auto expr = constant_exprt{value, bv_typet{4}};
33+
REQUIRE(format_to_string(expr) == "1011");
34+
}

0 commit comments

Comments
 (0)