cprover
Toggle main menu visibility
Loading...
Searching...
No Matches
boolbv_shift.cpp
Go to the documentation of this file.
1
/*******************************************************************\
2
3
Module:
4
5
Author: Daniel Kroening, kroening@kroening.com
6
7
\*******************************************************************/
8
9
#include "
boolbv.h
"
10
11
#include <limits>
12
13
#include <
util/arith_tools.h
>
14
15
bvt
boolbvt::convert_shift
(
const
binary_exprt
&expr)
16
{
17
const
irep_idt
&type_id=expr.
type
().
id
();
18
19
if
(type_id!=ID_unsignedbv &&
20
type_id!=ID_signedbv &&
21
type_id!=ID_floatbv &&
22
type_id!=ID_pointer &&
23
type_id!=ID_bv &&
24
type_id!=ID_verilog_signedbv &&
25
type_id!=ID_verilog_unsignedbv)
26
return
conversion_failed
(expr);
27
28
std::size_t width=
boolbv_width
(expr.
type
());
29
30
const
bvt
&op =
convert_bv
(expr.
op0
(), width);
31
32
bv_utilst::shiftt
shift;
33
34
if
(expr.
id
()==ID_shl)
35
shift=
bv_utilst::shiftt::SHIFT_LEFT
;
36
else
if
(expr.
id
()==ID_ashr)
37
shift=
bv_utilst::shiftt::SHIFT_ARIGHT
;
38
else
if
(expr.
id
()==ID_lshr)
39
shift=
bv_utilst::shiftt::SHIFT_LRIGHT
;
40
else
if
(expr.
id
()==ID_rol)
41
shift=
bv_utilst::shiftt::ROTATE_LEFT
;
42
else
if
(expr.
id
()==ID_ror)
43
shift=
bv_utilst::shiftt::ROTATE_RIGHT
;
44
else
45
UNREACHABLE
;
46
47
// we optimise for the special case where the shift distance
48
// is a constant
49
50
if
(expr.
op1
().
is_constant
())
51
{
52
mp_integer
i =
numeric_cast_v<mp_integer>
(
to_constant_expr
(expr.
op1
()));
53
54
std::size_t distance;
55
56
if
(i<0 || i>std::numeric_limits<signed>::max())
57
distance=0;
58
else
59
distance =
numeric_cast_v<std::size_t>
(i);
60
61
if
(type_id==ID_verilog_signedbv ||
62
type_id==ID_verilog_unsignedbv)
63
distance*=2;
64
65
return
bv_utils
.shift(op, shift, distance);
66
}
67
else
68
{
69
const
bvt
&distance=
convert_bv
(expr.
op1
());
70
return
bv_utils
.shift(op, shift, distance);
71
}
72
}
arith_tools.h
numeric_cast_v
Target numeric_cast_v(const mp_integer &arg)
Convert an mp_integer to integral type Target An invariant will fail if the conversion is not possibl...
Definition
arith_tools.h:135
boolbv.h
binary_exprt
A base class for binary expressions.
Definition
std_expr.h:639
binary_exprt::op0
exprt & op0()
Definition
expr.h:134
binary_exprt::op1
exprt & op1()
Definition
expr.h:137
boolbvt::convert_bv
virtual const bvt & convert_bv(const exprt &expr, const std::optional< std::size_t > expected_width={})
Convert expression to vector of literalts, using an internal cache to speed up conversion if availabl...
Definition
boolbv.cpp:40
boolbvt::convert_shift
virtual bvt convert_shift(const binary_exprt &expr)
Definition
boolbv_shift.cpp:15
boolbvt::bv_utils
bv_utilst bv_utils
Definition
boolbv.h:121
boolbvt::conversion_failed
bvt conversion_failed(const exprt &expr)
Print that the expression of x has failed conversion, then return a vector of x's width.
Definition
boolbv.cpp:95
boolbvt::boolbv_width
virtual std::size_t boolbv_width(const typet &type) const
Definition
boolbv.h:106
bv_utilst::shiftt
shiftt
Definition
bv_utils.h:74
bv_utilst::shiftt::ROTATE_RIGHT
@ ROTATE_RIGHT
Definition
bv_utils.h:75
bv_utilst::shiftt::ROTATE_LEFT
@ ROTATE_LEFT
Definition
bv_utils.h:75
bv_utilst::shiftt::SHIFT_LEFT
@ SHIFT_LEFT
Definition
bv_utils.h:75
bv_utilst::shiftt::SHIFT_ARIGHT
@ SHIFT_ARIGHT
Definition
bv_utils.h:75
bv_utilst::shiftt::SHIFT_LRIGHT
@ SHIFT_LRIGHT
Definition
bv_utils.h:75
exprt::is_constant
bool is_constant() const
Return whether the expression is a constant.
Definition
expr.h:213
exprt::type
typet & type()
Return the type of the expression.
Definition
expr.h:85
irept::id
const irep_idt & id() const
Definition
irep.h:388
bvt
std::vector< literalt > bvt
Definition
literal.h:201
mp_integer
BigInt mp_integer
Definition
smt_terms.h:17
UNREACHABLE
#define UNREACHABLE
This should be used to mark dead code.
Definition
invariant.h:525
to_constant_expr
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition
std_expr.h:3068
irep_idt
dstringt irep_idt
Definition
verification_result.h:16
solvers
flattening
boolbv_shift.cpp
Generated by
1.17.0