Skip to content

Use endianness map for both big and little endian #5859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions scripts/expected_doxygen_warnings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
warning: Include graph for 'goto_instrument_parse_options.cpp' not generated, too many nodes (97), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
warning: Included by graph for 'goto_functions.h' not generated, too many nodes (66), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
warning: Included by graph for 'goto_model.h' not generated, too many nodes (111), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
warning: Included by graph for 'arith_tools.h' not generated, too many nodes (183), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
warning: Included by graph for 'c_types.h' not generated, too many nodes (143), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
warning: Included by graph for 'config.h' not generated, too many nodes (78), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
warning: Included by graph for 'arith_tools.h' not generated, too many nodes (182), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
warning: Included by graph for 'c_types.h' not generated, too many nodes (144), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
warning: Included by graph for 'config.h' not generated, too many nodes (77), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
warning: Included by graph for 'exception_utils.h' not generated, too many nodes (61), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
warning: Included by graph for 'expr.h' not generated, too many nodes (87), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
warning: Included by graph for 'expr_util.h' not generated, too many nodes (61), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
Expand Down
8 changes: 8 additions & 0 deletions src/solvers/flattening/boolbv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
#include <util/arith_tools.h>
#include <util/bitvector_expr.h>
#include <util/bitvector_types.h>
#include <util/config.h>
#include <util/floatbv_expr.h>
#include <util/magic.h>
#include <util/mp_arith.h>
Expand All @@ -32,6 +33,13 @@ Author: Daniel Kroening, [email protected]
#include <solvers/floatbv/float_utils.h>
#include <solvers/lowering/expr_lowering.h>

endianness_mapt boolbvt::endianness_map(const typet &type) const
{
const bool little_endian =
config.ansi_c.endianness == configt::ansi_ct::endiannesst::IS_LITTLE_ENDIAN;
return endianness_map(type, little_endian);
}

/// Convert expression to vector of literalts, using an internal
/// cache to speed up conversion if available. Also assert the resultant
/// vector is of a specific size, and freeze any elements if appropriate.
Expand Down
2 changes: 2 additions & 0 deletions src/solvers/flattening/boolbv.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class boolbvt:public arrayst
return endianness_mapt{type, little_endian, ns};
}

virtual endianness_mapt endianness_map(const typet &type) const;

protected:
boolbv_widtht bv_width;
bv_utilst bv_utils;
Expand Down
34 changes: 13 additions & 21 deletions src/solvers/flattening/boolbv_byte_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,23 @@ bvt boolbvt::convert_byte_update(const byte_update_exprt &expr)
}
else
{
if(little_endian)
{
for(std::size_t i=0; i<update_width; i++)
bv[numeric_cast_v<std::size_t>(offset + i)] = value_bv[i];
}
else
{
endianness_mapt map_op = endianness_map(op.type(), false);
endianness_mapt map_value = endianness_map(value.type(), false);
endianness_mapt map_op = endianness_map(op.type(), little_endian);
endianness_mapt map_value = endianness_map(value.type(), little_endian);

const std::size_t offset_i = numeric_cast_v<std::size_t>(offset);
const std::size_t offset_i = numeric_cast_v<std::size_t>(offset);

for(std::size_t i=0; i<update_width; i++)
{
size_t index_op=map_op.map_bit(offset_i+i);
size_t index_value=map_value.map_bit(i);
for(std::size_t i = 0; i < update_width; i++)
{
size_t index_op = map_op.map_bit(offset_i + i);
size_t index_value = map_value.map_bit(i);

INVARIANT(
index_op < bv.size(), "bit vector index shall be within bounds");
INVARIANT(
index_value < value_bv.size(),
"bit vector index shall be within bounds");
INVARIANT(
index_op < bv.size(), "bit vector index shall be within bounds");
INVARIANT(
index_value < value_bv.size(),
"bit vector index shall be within bounds");

bv[index_op]=value_bv[index_value];
}
bv[index_op] = value_bv[index_value];
}
}

Expand Down
66 changes: 59 additions & 7 deletions src/solvers/flattening/boolbv_member.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ Author: Daniel Kroening, [email protected]

#include "boolbv.h"

bvt boolbvt::convert_member(const member_exprt &expr)
#include <util/c_types.h>

static bvt convert_member_struct(
const member_exprt &expr,
const bvt &struct_bv,
const std::function<std::size_t(const typet &)> boolbv_width,
const namespacet &ns)
{
const exprt &struct_op=expr.struct_op();
const typet &struct_op_type=ns.follow(struct_op.type());

const bvt &struct_bv=convert_bv(struct_op);

const irep_idt &component_name = expr.get_component_name();
const struct_union_typet::componentst &components =
to_struct_union_type(struct_op_type).components();
const struct_typet::componentst &components =
to_struct_type(struct_op_type).components();

std::size_t offset = 0;

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

if(struct_op_type.id() != ID_union)
offset += sub_width;
offset += sub_width;
}

INVARIANT_WITH_DIAGNOSTICS(
Expand All @@ -52,3 +55,52 @@ bvt boolbvt::convert_member(const member_exprt &expr)
expr.find_source_location(),
id2string(component_name));
}

static bvt convert_member_union(
const member_exprt &expr,
const bvt &union_bv,
const boolbvt &boolbv,
const namespacet &ns)
{
const exprt &union_op = expr.compound();
const union_typet &union_op_type =
ns.follow_tag(to_union_tag_type(union_op.type()));

const irep_idt &component_name = expr.get_component_name();
const union_typet::componentt &component =
union_op_type.get_component(component_name);
DATA_INVARIANT_WITH_DIAGNOSTICS(
component.is_not_nil(),
"union type shall contain component accessed by member expression",
expr.find_source_location(),
id2string(component_name));

const typet &subtype = component.type();
const std::size_t sub_width = boolbv.boolbv_width(subtype);

endianness_mapt map_u = boolbv.endianness_map(union_op_type);
endianness_mapt map_component = boolbv.endianness_map(subtype);

bvt result(sub_width, literalt{});
for(std::size_t i = 0; i < sub_width; ++i)
result[map_u.map_bit(i)] = union_bv[map_component.map_bit(i)];

return result;
}

bvt boolbvt::convert_member(const member_exprt &expr)
{
const bvt &compound_bv = convert_bv(expr.compound());

if(expr.compound().type().id() == ID_struct_tag)
return convert_member_struct(
expr,
compound_bv,
[this](const typet &t) { return boolbv_width(t); },
ns);
else
{
PRECONDITION(expr.compound().type().id() == ID_union_tag);
return convert_member_union(expr, compound_bv, *this, ns);
}
}
37 changes: 9 additions & 28 deletions src/solvers/flattening/boolbv_union.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ Author: Daniel Kroening, [email protected]

#include "boolbv.h"

#include <util/arith_tools.h>
#include <util/config.h>

bvt boolbvt::convert_union(const union_exprt &expr)
{
std::size_t width=boolbv_width(expr.type());
Expand All @@ -28,31 +25,15 @@ bvt boolbvt::convert_union(const union_exprt &expr)
bvt bv;
bv.resize(width);

if(config.ansi_c.endianness==configt::ansi_ct::endiannesst::IS_LITTLE_ENDIAN)
{
for(std::size_t i=0; i<op_bv.size(); i++)
bv[i]=op_bv[i];

// pad with nondets
for(std::size_t i=op_bv.size(); i<bv.size(); i++)
bv[i]=prop.new_variable();
}
else
{
INVARIANT(
config.ansi_c.endianness == configt::ansi_ct::endiannesst::IS_BIG_ENDIAN,
"endianness should be set to either little endian or big endian");

endianness_mapt map_u = endianness_map(expr.type(), false);
endianness_mapt map_op = endianness_map(expr.op().type(), false);

for(std::size_t i=0; i<op_bv.size(); i++)
bv[map_u.map_bit(i)]=op_bv[map_op.map_bit(i)];

// pad with nondets
for(std::size_t i=op_bv.size(); i<bv.size(); i++)
bv[map_u.map_bit(i)]=prop.new_variable();
}
endianness_mapt map_u = endianness_map(expr.type());
endianness_mapt map_op = endianness_map(expr.op().type());

for(std::size_t i = 0; i < op_bv.size(); i++)
bv[map_u.map_bit(i)] = op_bv[map_op.map_bit(i)];

// pad with nondets
for(std::size_t i = op_bv.size(); i < bv.size(); i++)
bv[map_u.map_bit(i)] = prop.new_variable();

return bv;
}
20 changes: 4 additions & 16 deletions src/solvers/flattening/boolbv_with.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Author: Daniel Kroening, [email protected]

#include <util/arith_tools.h>
#include <util/c_types.h>
#include <util/config.h>
#include <util/std_expr.h>
#include <util/std_types.h>

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

if(config.ansi_c.endianness==configt::ansi_ct::endiannesst::IS_LITTLE_ENDIAN)
{
for(std::size_t i=0; i<op2_bv.size(); i++)
next_bv[i]=op2_bv[i];
}
else
{
assert(
config.ansi_c.endianness==configt::ansi_ct::endiannesst::IS_BIG_ENDIAN);
endianness_mapt map_u = endianness_map(type);
endianness_mapt map_op2 = endianness_map(op2.type());

endianness_mapt map_u = endianness_map(type, false);
endianness_mapt map_op2 = endianness_map(op2.type(), false);

for(std::size_t i=0; i<op2_bv.size(); i++)
next_bv[map_u.map_bit(i)]=op2_bv[map_op2.map_bit(i)];
}
for(std::size_t i = 0; i < op2_bv.size(); i++)
next_bv[map_u.map_bit(i)] = op2_bv[map_op2.map_bit(i)];
}