Skip to content

Commit 11c61e5

Browse files
authored
Merge pull request #5859 from tautschnig/always-endianness-map
Use endianness map for both big and little endian
2 parents 4ecf01d + e7bae74 commit 11c61e5

File tree

7 files changed

+98
-75
lines changed

7 files changed

+98
-75
lines changed

scripts/expected_doxygen_warnings.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
warning: Include graph for 'goto_instrument_parse_options.cpp' not generated, too many nodes (97), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
2222
warning: Included by graph for 'goto_functions.h' not generated, too many nodes (66), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
2323
warning: Included by graph for 'goto_model.h' not generated, too many nodes (111), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
24-
warning: Included by graph for 'arith_tools.h' not generated, too many nodes (183), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
25-
warning: Included by graph for 'c_types.h' not generated, too many nodes (143), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
26-
warning: Included by graph for 'config.h' not generated, too many nodes (78), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
24+
warning: Included by graph for 'arith_tools.h' not generated, too many nodes (182), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
25+
warning: Included by graph for 'c_types.h' not generated, too many nodes (144), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
26+
warning: Included by graph for 'config.h' not generated, too many nodes (77), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
2727
warning: Included by graph for 'exception_utils.h' not generated, too many nodes (61), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
2828
warning: Included by graph for 'expr.h' not generated, too many nodes (87), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
2929
warning: Included by graph for 'expr_util.h' not generated, too many nodes (61), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.

src/solvers/flattening/boolbv.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
1515
#include <util/arith_tools.h>
1616
#include <util/bitvector_expr.h>
1717
#include <util/bitvector_types.h>
18+
#include <util/config.h>
1819
#include <util/floatbv_expr.h>
1920
#include <util/magic.h>
2021
#include <util/mp_arith.h>
@@ -32,6 +33,13 @@ Author: Daniel Kroening, [email protected]
3233
#include <solvers/floatbv/float_utils.h>
3334
#include <solvers/lowering/expr_lowering.h>
3435

36+
endianness_mapt boolbvt::endianness_map(const typet &type) const
37+
{
38+
const bool little_endian =
39+
config.ansi_c.endianness == configt::ansi_ct::endiannesst::IS_LITTLE_ENDIAN;
40+
return endianness_map(type, little_endian);
41+
}
42+
3543
/// Convert expression to vector of literalts, using an internal
3644
/// cache to speed up conversion if available. Also assert the resultant
3745
/// vector is of a specific size, and freeze any elements if appropriate.

src/solvers/flattening/boolbv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ class boolbvt:public arrayst
104104
return endianness_mapt{type, little_endian, ns};
105105
}
106106

107+
virtual endianness_mapt endianness_map(const typet &type) const;
108+
107109
protected:
108110
boolbv_widtht bv_width;
109111
bv_utilst bv_utils;

src/solvers/flattening/boolbv_byte_update.cpp

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,23 @@ bvt boolbvt::convert_byte_update(const byte_update_exprt &expr)
5858
}
5959
else
6060
{
61-
if(little_endian)
62-
{
63-
for(std::size_t i=0; i<update_width; i++)
64-
bv[numeric_cast_v<std::size_t>(offset + i)] = value_bv[i];
65-
}
66-
else
67-
{
68-
endianness_mapt map_op = endianness_map(op.type(), false);
69-
endianness_mapt map_value = endianness_map(value.type(), false);
61+
endianness_mapt map_op = endianness_map(op.type(), little_endian);
62+
endianness_mapt map_value = endianness_map(value.type(), little_endian);
7063

71-
const std::size_t offset_i = numeric_cast_v<std::size_t>(offset);
64+
const std::size_t offset_i = numeric_cast_v<std::size_t>(offset);
7265

73-
for(std::size_t i=0; i<update_width; i++)
74-
{
75-
size_t index_op=map_op.map_bit(offset_i+i);
76-
size_t index_value=map_value.map_bit(i);
66+
for(std::size_t i = 0; i < update_width; i++)
67+
{
68+
size_t index_op = map_op.map_bit(offset_i + i);
69+
size_t index_value = map_value.map_bit(i);
7770

78-
INVARIANT(
79-
index_op < bv.size(), "bit vector index shall be within bounds");
80-
INVARIANT(
81-
index_value < value_bv.size(),
82-
"bit vector index shall be within bounds");
71+
INVARIANT(
72+
index_op < bv.size(), "bit vector index shall be within bounds");
73+
INVARIANT(
74+
index_value < value_bv.size(),
75+
"bit vector index shall be within bounds");
8376

84-
bv[index_op]=value_bv[index_value];
85-
}
77+
bv[index_op] = value_bv[index_value];
8678
}
8779
}
8880

src/solvers/flattening/boolbv_member.cpp

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ Author: Daniel Kroening, [email protected]
88

99
#include "boolbv.h"
1010

11-
bvt boolbvt::convert_member(const member_exprt &expr)
11+
#include <util/c_types.h>
12+
13+
static bvt convert_member_struct(
14+
const member_exprt &expr,
15+
const bvt &struct_bv,
16+
const std::function<std::size_t(const typet &)> boolbv_width,
17+
const namespacet &ns)
1218
{
1319
const exprt &struct_op=expr.struct_op();
1420
const typet &struct_op_type=ns.follow(struct_op.type());
1521

16-
const bvt &struct_bv=convert_bv(struct_op);
17-
1822
const irep_idt &component_name = expr.get_component_name();
19-
const struct_union_typet::componentst &components =
20-
to_struct_union_type(struct_op_type).components();
23+
const struct_typet::componentst &components =
24+
to_struct_type(struct_op_type).components();
2125

2226
std::size_t offset = 0;
2327

@@ -42,8 +46,7 @@ bvt boolbvt::convert_member(const member_exprt &expr)
4246
struct_bv.begin() + offset, struct_bv.begin() + offset + sub_width);
4347
}
4448

45-
if(struct_op_type.id() != ID_union)
46-
offset += sub_width;
49+
offset += sub_width;
4750
}
4851

4952
INVARIANT_WITH_DIAGNOSTICS(
@@ -52,3 +55,52 @@ bvt boolbvt::convert_member(const member_exprt &expr)
5255
expr.find_source_location(),
5356
id2string(component_name));
5457
}
58+
59+
static bvt convert_member_union(
60+
const member_exprt &expr,
61+
const bvt &union_bv,
62+
const boolbvt &boolbv,
63+
const namespacet &ns)
64+
{
65+
const exprt &union_op = expr.compound();
66+
const union_typet &union_op_type =
67+
ns.follow_tag(to_union_tag_type(union_op.type()));
68+
69+
const irep_idt &component_name = expr.get_component_name();
70+
const union_typet::componentt &component =
71+
union_op_type.get_component(component_name);
72+
DATA_INVARIANT_WITH_DIAGNOSTICS(
73+
component.is_not_nil(),
74+
"union type shall contain component accessed by member expression",
75+
expr.find_source_location(),
76+
id2string(component_name));
77+
78+
const typet &subtype = component.type();
79+
const std::size_t sub_width = boolbv.boolbv_width(subtype);
80+
81+
endianness_mapt map_u = boolbv.endianness_map(union_op_type);
82+
endianness_mapt map_component = boolbv.endianness_map(subtype);
83+
84+
bvt result(sub_width, literalt{});
85+
for(std::size_t i = 0; i < sub_width; ++i)
86+
result[map_u.map_bit(i)] = union_bv[map_component.map_bit(i)];
87+
88+
return result;
89+
}
90+
91+
bvt boolbvt::convert_member(const member_exprt &expr)
92+
{
93+
const bvt &compound_bv = convert_bv(expr.compound());
94+
95+
if(expr.compound().type().id() == ID_struct_tag)
96+
return convert_member_struct(
97+
expr,
98+
compound_bv,
99+
[this](const typet &t) { return boolbv_width(t); },
100+
ns);
101+
else
102+
{
103+
PRECONDITION(expr.compound().type().id() == ID_union_tag);
104+
return convert_member_union(expr, compound_bv, *this, ns);
105+
}
106+
}

src/solvers/flattening/boolbv_union.cpp

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ Author: Daniel Kroening, [email protected]
88

99
#include "boolbv.h"
1010

11-
#include <util/arith_tools.h>
12-
#include <util/config.h>
13-
1411
bvt boolbvt::convert_union(const union_exprt &expr)
1512
{
1613
std::size_t width=boolbv_width(expr.type());
@@ -28,31 +25,15 @@ bvt boolbvt::convert_union(const union_exprt &expr)
2825
bvt bv;
2926
bv.resize(width);
3027

31-
if(config.ansi_c.endianness==configt::ansi_ct::endiannesst::IS_LITTLE_ENDIAN)
32-
{
33-
for(std::size_t i=0; i<op_bv.size(); i++)
34-
bv[i]=op_bv[i];
35-
36-
// pad with nondets
37-
for(std::size_t i=op_bv.size(); i<bv.size(); i++)
38-
bv[i]=prop.new_variable();
39-
}
40-
else
41-
{
42-
INVARIANT(
43-
config.ansi_c.endianness == configt::ansi_ct::endiannesst::IS_BIG_ENDIAN,
44-
"endianness should be set to either little endian or big endian");
45-
46-
endianness_mapt map_u = endianness_map(expr.type(), false);
47-
endianness_mapt map_op = endianness_map(expr.op().type(), false);
48-
49-
for(std::size_t i=0; i<op_bv.size(); i++)
50-
bv[map_u.map_bit(i)]=op_bv[map_op.map_bit(i)];
51-
52-
// pad with nondets
53-
for(std::size_t i=op_bv.size(); i<bv.size(); i++)
54-
bv[map_u.map_bit(i)]=prop.new_variable();
55-
}
28+
endianness_mapt map_u = endianness_map(expr.type());
29+
endianness_mapt map_op = endianness_map(expr.op().type());
30+
31+
for(std::size_t i = 0; i < op_bv.size(); i++)
32+
bv[map_u.map_bit(i)] = op_bv[map_op.map_bit(i)];
33+
34+
// pad with nondets
35+
for(std::size_t i = op_bv.size(); i < bv.size(); i++)
36+
bv[map_u.map_bit(i)] = prop.new_variable();
5637

5738
return bv;
5839
}

src/solvers/flattening/boolbv_with.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Author: Daniel Kroening, [email protected]
1010

1111
#include <util/arith_tools.h>
1212
#include <util/c_types.h>
13-
#include <util/config.h>
1413
#include <util/std_expr.h>
1514
#include <util/std_types.h>
1615

@@ -237,20 +236,9 @@ void boolbvt::convert_with_union(
237236
"convert_with_union: unexpected operand op2 width",
238237
irep_pretty_diagnosticst{type});
239238

240-
if(config.ansi_c.endianness==configt::ansi_ct::endiannesst::IS_LITTLE_ENDIAN)
241-
{
242-
for(std::size_t i=0; i<op2_bv.size(); i++)
243-
next_bv[i]=op2_bv[i];
244-
}
245-
else
246-
{
247-
assert(
248-
config.ansi_c.endianness==configt::ansi_ct::endiannesst::IS_BIG_ENDIAN);
239+
endianness_mapt map_u = endianness_map(type);
240+
endianness_mapt map_op2 = endianness_map(op2.type());
249241

250-
endianness_mapt map_u = endianness_map(type, false);
251-
endianness_mapt map_op2 = endianness_map(op2.type(), false);
252-
253-
for(std::size_t i=0; i<op2_bv.size(); i++)
254-
next_bv[map_u.map_bit(i)]=op2_bv[map_op2.map_bit(i)];
255-
}
242+
for(std::size_t i = 0; i < op2_bv.size(); i++)
243+
next_bv[map_u.map_bit(i)] = op2_bv[map_op2.map_bit(i)];
256244
}

0 commit comments

Comments
 (0)