CBMC
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 
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)
36  else if(expr.id()==ID_ashr)
38  else if(expr.id()==ID_lshr)
40  else if(expr.id()==ID_rol)
42  else if(expr.id()==ID_ror)
44  else
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 }
A base class for binary expressions.
Definition: std_expr.h:638
exprt & op1()
Definition: expr.h:136
exprt & op0()
Definition: expr.h:133
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:37
virtual bvt convert_shift(const binary_exprt &expr)
bv_utilst bv_utils
Definition: boolbv.h:116
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:83
virtual std::size_t boolbv_width(const typet &type) const
Definition: boolbv.h:101
static bvt shift(const bvt &op, const shiftt shift, std::size_t distance)
Definition: bv_utils.cpp:537
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
typet & type()
Return the type of the expression.
Definition: expr.h:84
bool is_constant() const
Return whether the expression is a constant.
Definition: expr.h:212
const irep_idt & id() const
Definition: irep.h:384
std::vector< literalt > bvt
Definition: literal.h:201
BigInt mp_integer
Definition: smt_terms.h:17
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:525
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition: std_expr.h:3037