Skip to content

Commit e8fa1c9

Browse files
author
Daniel Kroening
committed
bswap_exprt now knows how many bits a byte has
1 parent b75fcbc commit e8fa1c9

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2190,7 +2190,8 @@ exprt c_typecheck_baset::do_special_functions(
21902190
throw 0;
21912191
}
21922192

2193-
bswap_exprt bswap_expr(expr.arguments().front(), expr.type());
2193+
// these are hard-wired to 8 bits according to the gcc manual
2194+
bswap_exprt bswap_expr(expr.arguments().front(), 8, expr.type());
21942195
bswap_expr.add_source_location()=source_location;
21952196

21962197
return bswap_expr;

src/solvers/flattening/boolbv_bswap.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ Author: Michael Tautschnig
88

99
#include "boolbv.h"
1010

11-
#include <util/config.h>
1211
#include <util/invariant.h>
1312

1413
bvt boolbvt::convert_bswap(const bswap_exprt &expr)
1514
{
1615
const std::size_t width = boolbv_width(expr.type());
1716

1817
// width must be multiple of bytes
19-
const std::size_t byte_bits = config.ansi_c.char_width;
18+
const std::size_t byte_bits = expr.get_bits_per_byte();
2019
if(width % byte_bits != 0)
2120
return conversion_failed(expr);
2221

src/util/irep_ids.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ IREP_ID_TWO(overlay_class, java::com.diffblue.OverlayClassImplementation)
664664
IREP_ID_TWO(overlay_method, java::com.diffblue.OverlayMethodImplementation)
665665
IREP_ID_TWO(C_annotations, #annotations)
666666
IREP_ID_ONE(final)
667+
IREP_ID_ONE(bits_per_byte)
667668

668669
// Projects depending on this code base that wish to extend the list of
669670
// available ids should provide a file local_irep_ids.h in their source tree and

src/util/simplify_expr_int.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,19 @@ bool simplify_exprt::simplify_bswap(bswap_exprt &expr)
2828
{
2929
if(expr.type().id() == ID_unsignedbv && expr.op().is_constant())
3030
{
31+
auto bits_per_byte = expr.get_bits_per_byte();
3132
std::size_t width=to_bitvector_type(expr.type()).get_width();
3233
mp_integer value;
3334
to_integer(expr.op(), value);
3435
std::vector<mp_integer> bytes;
3536

3637
// take apart
37-
for(std::size_t bit=0; bit<width; bit+=8)
38-
bytes.push_back((value >> bit)%256);
38+
for(std::size_t bit = 0; bit < width; bit += bits_per_byte)
39+
bytes.push_back((value >> bit)%power(2, bits_per_byte));
3940

4041
// put back together, but backwards
4142
mp_integer new_value=0;
42-
for(std::size_t bit=0; bit<width; bit+=8)
43+
for(std::size_t bit = 0; bit < width; bit += bits_per_byte)
4344
{
4445
assert(!bytes.empty());
4546
new_value+=bytes.back()<<bit;

src/util/std_expr.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -506,18 +506,25 @@ inline void validate_expr(const unary_minus_exprt &value)
506506
class bswap_exprt: public unary_exprt
507507
{
508508
public:
509-
bswap_exprt(): unary_exprt(ID_bswap)
509+
bswap_exprt(const exprt &_op, std::size_t bits_per_byte, const typet &_type)
510+
: unary_exprt(ID_bswap, _op, _type)
510511
{
512+
set_bits_per_byte(bits_per_byte);
511513
}
512514

513-
bswap_exprt(const exprt &_op, const typet &_type)
514-
: unary_exprt(ID_bswap, _op, _type)
515+
explicit bswap_exprt(const exprt &_op, std::size_t &_bits_per_byte)
516+
: unary_exprt(ID_bswap, _op, _op.type())
515517
{
516518
}
517519

518-
explicit bswap_exprt(const exprt &_op)
519-
: unary_exprt(ID_bswap, _op, _op.type())
520+
std::size_t get_bits_per_byte() const
521+
{
522+
return get_size_t(ID_bits_per_byte);
523+
}
524+
525+
void set_bits_per_byte(std::size_t bits_per_byte)
520526
{
527+
set(ID_bits_per_byte, bits_per_byte);
521528
}
522529
};
523530

0 commit comments

Comments
 (0)