CBMC
boolbv_constraint_select_one.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 
10 #include "boolbv.h"
11 
13 {
14  const exprt::operandst &operands=expr.operands();
15 
16  if(expr.id()!=ID_constraint_select_one)
17  throw "expected constraint_select_one expression";
18 
19  if(operands.empty())
20  throw "constraint_select_one takes at least one operand";
21 
22  if(expr.type() != to_multi_ary_expr(expr).op0().type())
23  throw "constraint_select_one expects matching types";
24 
25  bvt bv;
26 
27  if(prop.has_set_to())
28  {
29  std::size_t width=boolbv_width(expr.type());
30  bv=prop.new_variables(width);
31 
32  bvt b;
33  b.reserve(expr.operands().size());
34 
35  // add constraints
36  forall_operands(it, expr)
37  {
38  bvt it_bv=convert_bv(*it);
39 
40  if(it_bv.size()!=bv.size())
41  throw "constraint_select_one expects matching width";
42 
43  b.push_back(bv_utils.equal(bv, it_bv));
44  }
45 
46  prop.lcnf(b);
47  }
48  else
49  {
50  std::size_t op_nr=0;
51  forall_operands(it, expr)
52  {
53  const bvt &op_bv=convert_bv(*it);
54 
55  if(op_nr==0)
56  bv=op_bv;
57  else
58  {
59  if(op_bv.size()!=bv.size())
60  return conversion_failed(expr);
61 
62  for(std::size_t i=0; i<op_bv.size(); i++)
63  bv[i]=prop.lselect(prop.new_variable(), bv[i], op_bv[i]);
64  }
65 
66  op_nr++;
67  }
68  }
69 
70  return bv;
71 }
propt::new_variables
virtual bvt new_variables(std::size_t width)
generates a bitvector of given width with new variables
Definition: prop.cpp:20
bvt
std::vector< literalt > bvt
Definition: literal.h:201
propt::new_variable
virtual literalt new_variable()=0
exprt
Base class for all expressions.
Definition: expr.h:55
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:84
boolbvt::boolbv_width
virtual std::size_t boolbv_width(const typet &type) const
Definition: boolbv.h:102
boolbvt::convert_bv
virtual const bvt & convert_bv(const exprt &expr, const optionalt< 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
forall_operands
#define forall_operands(it, expr)
Definition: expr.h:20
irept::id
const irep_idt & id() const
Definition: irep.h:396
exprt::operandst
std::vector< exprt > operandst
Definition: expr.h:58
boolbvt::convert_constraint_select_one
virtual bvt convert_constraint_select_one(const exprt &expr)
Definition: boolbv_constraint_select_one.cpp:12
boolbvt::bv_utils
bv_utilst bv_utils
Definition: boolbv.h:117
propt::has_set_to
virtual bool has_set_to() const
Definition: prop.h:79
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:83
boolbv.h
exprt::operands
operandst & operands()
Definition: expr.h:94
bv_utilst::equal
literalt equal(const bvt &op0, const bvt &op1)
Bit-blasting ID_equal and use in other encodings.
Definition: bv_utils.cpp:1165
to_multi_ary_expr
const multi_ary_exprt & to_multi_ary_expr(const exprt &expr)
Cast an exprt to a multi_ary_exprt.
Definition: std_expr.h:932
propt::lselect
virtual literalt lselect(literalt a, literalt b, literalt c)=0
propt::lcnf
void lcnf(literalt l0, literalt l1)
Definition: prop.h:56
prop_conv_solvert::prop
propt & prop
Definition: prop_conv_solver.h:130