CBMC
boolbv_cond.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/invariant.h>
12 
14 {
15  const exprt::operandst &operands=expr.operands();
16 
17  std::size_t width=boolbv_width(expr.type());
18 
19  bvt bv;
20 
21  DATA_INVARIANT(operands.size() >= 2, "cond must have at least two operands");
22 
24  operands.size() % 2 == 0, "number of cond operands must be even");
25 
26  if(prop.has_set_to())
27  {
28  bool condition=true;
29  literalt previous_cond=const_literal(false);
30  literalt cond_literal=const_literal(false);
31 
32  // make it free variables
33  bv = prop.new_variables(width);
34 
35  for(const auto &operand : expr.operands())
36  {
37  if(condition)
38  {
39  cond_literal = convert(operand);
40  cond_literal=prop.land(!previous_cond, cond_literal);
41 
42  previous_cond=prop.lor(previous_cond, cond_literal);
43  }
44  else
45  {
46  const bvt &op = convert_bv(operand, bv.size());
47 
48  literalt value_literal=bv_utils.equal(bv, op);
49 
50  prop.l_set_to_true(prop.limplies(cond_literal, value_literal));
51  }
52 
53  condition=!condition;
54  }
55  }
56  else
57  {
58  bv.resize(width);
59 
60  // functional version -- go backwards
61  for(std::size_t i=expr.operands().size(); i!=0; i-=2)
62  {
63  INVARIANT(
64  i >= 2,
65  "since the number of operands is even if i is nonzero it must be "
66  "greater than two");
67  const exprt &cond=expr.operands()[i-2];
68  const exprt &value=expr.operands()[i-1];
69 
70  literalt cond_literal=convert(cond);
71 
72  const bvt &op = convert_bv(value, bv.size());
73 
74  for(std::size_t j = 0; j < bv.size(); j++)
75  bv[j] = prop.lselect(cond_literal, op[j], bv[j]);
76  }
77  }
78 
79  return bv;
80 }
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
virtual bvt convert_cond(const cond_exprt &)
Definition: boolbv_cond.cpp:13
bv_utilst bv_utils
Definition: boolbv.h:117
virtual std::size_t boolbv_width(const typet &type) const
Definition: boolbv.h:102
literalt equal(const bvt &op0, const bvt &op1)
Bit-blasting ID_equal and use in other encodings.
Definition: bv_utils.cpp:1369
this is a parametric version of an if-expression: it returns the value of the first case (using the o...
Definition: std_expr.h:3360
Base class for all expressions.
Definition: expr.h:56
std::vector< exprt > operandst
Definition: expr.h:58
typet & type()
Return the type of the expression.
Definition: expr.h:84
operandst & operands()
Definition: expr.h:94
literalt convert(const exprt &expr) override
Convert a Boolean expression and return the corresponding literal.
void l_set_to_true(literalt a)
Definition: prop.h:52
virtual literalt land(literalt a, literalt b)=0
virtual literalt limplies(literalt a, literalt b)=0
virtual literalt lselect(literalt a, literalt b, literalt c)=0
virtual bvt new_variables(std::size_t width)
generates a bitvector of given width with new variables
Definition: prop.cpp:30
virtual literalt lor(literalt a, literalt b)=0
virtual bool has_set_to() const
Definition: prop.h:81
std::vector< literalt > bvt
Definition: literal.h:201
literalt const_literal(bool value)
Definition: literal.h:188
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:534