CBMC
boolbv_bitwise.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 <util/bitvector_expr.h>
12 
14 {
15  const std::size_t width = boolbv_width(expr.type());
16 
17  if(expr.id()==ID_bitnot)
18  {
19  const exprt &op = to_bitnot_expr(expr).op();
20  const bvt &op_bv = convert_bv(op, width);
21  return bv_utils.inverted(op_bv);
22  }
23  else if(expr.id()==ID_bitand || expr.id()==ID_bitor ||
24  expr.id()==ID_bitxor ||
25  expr.id()==ID_bitnand || expr.id()==ID_bitnor ||
26  expr.id()==ID_bitxnor)
27  {
28  bvt bv;
29  bv.resize(width);
30 
31  forall_operands(it, expr)
32  {
33  const bvt &op = convert_bv(*it, width);
34 
35  if(it==expr.operands().begin())
36  bv=op;
37  else
38  {
39  for(std::size_t i=0; i<width; i++)
40  {
41  if(expr.id()==ID_bitand)
42  bv[i]=prop.land(bv[i], op[i]);
43  else if(expr.id()==ID_bitor)
44  bv[i]=prop.lor(bv[i], op[i]);
45  else if(expr.id()==ID_bitxor)
46  bv[i]=prop.lxor(bv[i], op[i]);
47  else if(expr.id()==ID_bitnand)
48  bv[i]=prop.lnand(bv[i], op[i]);
49  else if(expr.id()==ID_bitnor)
50  bv[i]=prop.lnor(bv[i], op[i]);
51  else if(expr.id()==ID_bitxnor)
52  bv[i]=prop.lequal(bv[i], op[i]);
53  else
55  }
56  }
57  }
58 
59  return bv;
60  }
61 
63 }
API to expression classes for bitvectors.
const bitnot_exprt & to_bitnot_expr(const exprt &expr)
Cast an exprt to a bitnot_exprt.
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:39
bv_utilst bv_utils
Definition: boolbv.h:117
virtual bvt convert_bitwise(const exprt &expr)
virtual std::size_t boolbv_width(const typet &type) const
Definition: boolbv.h:102
static bvt inverted(const bvt &op)
Definition: bv_utils.cpp:637
Base class for all expressions.
Definition: expr.h:56
typet & type()
Return the type of the expression.
Definition: expr.h:84
operandst & operands()
Definition: expr.h:94
const irep_idt & id() const
Definition: irep.h:388
virtual literalt land(literalt a, literalt b)=0
virtual literalt lnand(literalt a, literalt b)=0
virtual literalt lnor(literalt a, literalt b)=0
virtual literalt lxor(literalt a, literalt b)=0
virtual literalt lequal(literalt a, literalt b)=0
virtual literalt lor(literalt a, literalt b)=0
const exprt & op() const
Definition: std_expr.h:391
#define forall_operands(it, expr)
Definition: expr.h:20
std::vector< literalt > bvt
Definition: literal.h:201
#define UNIMPLEMENTED
Definition: invariant.h:558