Skip to content

Commit c78f239

Browse files
author
Daniel Kroening
committed
Fix binary represenation in XML generated from constant_exprt
We no longer use binary as bitvector representation, and thus, conversion must happen.
1 parent 4dc7725 commit c78f239

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/goto-programs/xml_expr.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ xmlt xml(const exprt &expr, const namespacet &ns)
136136
if(expr.id() == ID_constant)
137137
{
138138
const auto &constant_expr = to_constant_expr(expr);
139+
const auto &value = constant_expr.get_value();
139140

140141
const typet &type = expr.type();
141142

@@ -175,10 +176,14 @@ xmlt xml(const exprt &expr, const namespacet &ns)
175176
}
176177
else if(type.id() == ID_c_enum)
177178
{
179+
const auto width =
180+
to_bitvector_type(to_c_enum_type(type).subtype()).get_width();
181+
178182
result.name = "integer";
179-
result.set_attribute("binary", constant_expr.get_string(ID_value));
180183
result.set_attribute(
181-
"width", to_bitvector_type(to_c_enum_type(type).subtype()).get_width());
184+
"binary",
185+
integer2binary(numeric_cast_v<mp_integer>(constant_expr), width));
186+
result.set_attribute("width", width);
182187
result.set_attribute("c_type", "enum");
183188

184189
mp_integer i;
@@ -198,22 +203,28 @@ xmlt xml(const exprt &expr, const namespacet &ns)
198203
}
199204
else if(type.id() == ID_fixedbv)
200205
{
206+
const auto width = to_fixedbv_type(type).get_width();
201207
result.name = "fixed";
202-
result.set_attribute("width", to_bitvector_type(type).get_width());
203-
result.set_attribute("binary", constant_expr.get_string(ID_value));
208+
result.set_attribute("width", width);
209+
result.set_attribute(
210+
"binary", integer2binary(bvrep2integer(value, width, false), width));
204211
result.data = fixedbvt(constant_expr).to_ansi_c_string();
205212
}
206213
else if(type.id() == ID_floatbv)
207214
{
215+
const auto width = to_floatbv_type(type).get_width();
208216
result.name = "float";
209-
result.set_attribute("width", to_bitvector_type(type).get_width());
210-
result.set_attribute("binary", constant_expr.get_string(ID_value));
217+
result.set_attribute("width", width);
218+
result.set_attribute(
219+
"binary", integer2binary(bvrep2integer(value, width, false), width));
211220
result.data = ieee_floatt(constant_expr).to_ansi_c_string();
212221
}
213222
else if(type.id() == ID_pointer)
214223
{
224+
const auto width = to_pointer_type(type).get_width();
215225
result.name = "pointer";
216-
result.set_attribute("binary", constant_expr.get_string(ID_value));
226+
result.set_attribute(
227+
"binary", integer2binary(bvrep2integer(value, width, false), width));
217228
if(constant_expr.get(ID_value) == ID_NULL)
218229
result.data = "NULL";
219230
}

unit/goto-programs/xml_expr.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Author: Michael Tautschnig
1212
#include <util/config.h>
1313
#include <util/namespace.h>
1414
#include <util/std_expr.h>
15+
#include <util/std_types.h>
1516
#include <util/symbol_table.h>
1617

1718
#include <goto-programs/xml_expr.h>
@@ -23,8 +24,17 @@ TEST_CASE("Constant expression to XML")
2324
const symbol_tablet symbol_table;
2425
const namespacet ns(symbol_table);
2526

26-
const constant_exprt number = from_integer(0xFF, unsignedbv_typet(8));
27-
const xmlt x = xml(number, ns);
27+
const constant_exprt number_ubv = from_integer(0xFF, unsignedbv_typet(8));
28+
const xmlt x_ubv = xml(number_ubv, ns);
2829

29-
REQUIRE(x.get_attribute("binary") == "11111111");
30+
REQUIRE(x_ubv.get_attribute("binary") == "11111111");
31+
32+
fixedbv_typet fixedbv_type;
33+
fixedbv_type.set_width(8);
34+
fixedbv_type.set_integer_bits(6);
35+
36+
const constant_exprt number_fbv = from_integer(0x3, fixedbv_type);
37+
const xmlt x_fbv = xml(number_fbv, ns);
38+
39+
REQUIRE(x_fbv.get_attribute("binary") == "00001100");
3040
}

0 commit comments

Comments
 (0)