CBMC
boolbv_case.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  PRECONDITION(expr.id() == ID_case);
16 
17  const std::vector<exprt> &operands=expr.operands();
18 
19  std::size_t width=boolbv_width(expr.type());
20 
21  // make it free variables
22  bvt bv = prop.new_variables(width);
23 
25  operands.size() >= 3, "case should have at least three operands");
26 
28  operands.size() % 2 == 1, "number of case operands should be odd");
29 
30  enum { FIRST, COMPARE, VALUE } what=FIRST;
31  bvt compare_bv;
32  literalt previous_compare=const_literal(false);
33  literalt compare_literal=const_literal(false);
34 
35  for(const auto &operand : expr.operands())
36  {
37  bvt op = convert_bv(operand);
38 
39  switch(what)
40  {
41  case FIRST:
42  compare_bv.swap(op);
43  what=COMPARE;
44  break;
45 
46  case COMPARE:
48  compare_bv.size() == op.size(),
49  std::string("size of compare operand does not match:\n") +
50  "compare operand: " + std::to_string(compare_bv.size()) +
51  "\noperand: " + std::to_string(op.size()) + '\n' + operand.pretty());
52 
53  compare_literal=bv_utils.equal(compare_bv, op);
54  compare_literal=prop.land(!previous_compare, compare_literal);
55 
56  previous_compare=prop.lor(previous_compare, compare_literal);
57 
58  what=VALUE;
59  break;
60 
61  case VALUE:
63  bv.size() == op.size(),
64  std::string("size of value operand does not match:\n") +
65  "result size: " + std::to_string(bv.size()) +
66  "\noperand: " + std::to_string(op.size()) + '\n' + operand.pretty());
67 
68  {
69  literalt value_literal=bv_utils.equal(bv, op);
70 
72  prop.limplies(compare_literal, value_literal));
73  }
74 
75  what=COMPARE;
76  break;
77 
78  default:
80  }
81  }
82 
83  return bv;
84 }
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 std::size_t boolbv_width(const typet &type) const
Definition: boolbv.h:102
virtual bvt convert_case(const exprt &expr)
Definition: boolbv_case.cpp:13
literalt equal(const bvt &op0, const bvt &op1)
Bit-blasting ID_equal and use in other encodings.
Definition: bv_utils.cpp:1369
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
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 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
std::vector< literalt > bvt
Definition: literal.h:201
literalt const_literal(bool value)
Definition: literal.h:188
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:525
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:534
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.