cprover
Loading...
Searching...
No Matches
boolbv_case.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
9#include <util/invariant.h>
10#include <util/std_expr.h>
11
12#include "boolbv.h"
13
15{
16 const std::vector<exprt> &operands=expr.operands();
17
18 std::size_t width=boolbv_width(expr.type());
19
20 // make it free variables
21 bvt bv = prop.new_variables(width);
22
24 operands.size() >= 3, "case should have at least three operands");
25
27 operands.size() % 2 == 1, "number of case operands should be odd");
28
29 enum { FIRST, COMPARE, VALUE } what=FIRST;
30 bvt compare_bv;
31 literalt previous_compare=const_literal(false);
32 literalt compare_literal=const_literal(false);
33
34 for(const auto &operand : expr.operands())
35 {
36 bvt op = convert_bv(operand);
37
38 switch(what)
39 {
40 case FIRST:
41 compare_bv.swap(op);
42 what=COMPARE;
43 break;
44
45 case COMPARE:
47 compare_bv.size() == op.size(),
48 std::string("size of compare operand does not match:\n") +
49 "compare operand: " + std::to_string(compare_bv.size()) +
50 "\noperand: " + std::to_string(op.size()) + '\n' + operand.pretty());
51
52 compare_literal=bv_utils.equal(compare_bv, op);
53 compare_literal=prop.land(!previous_compare, compare_literal);
54
55 previous_compare=prop.lor(previous_compare, compare_literal);
56
57 what=VALUE;
58 break;
59
60 case VALUE:
62 bv.size() == op.size(),
63 std::string("size of value operand does not match:\n") +
64 "result size: " + std::to_string(bv.size()) +
65 "\noperand: " + std::to_string(op.size()) + '\n' + operand.pretty());
66
67 {
68 literalt value_literal=bv_utils.equal(bv, op);
69
70 prop.l_set_to_true(
71 prop.limplies(compare_literal, value_literal));
72 }
73
74 what=COMPARE;
75 break;
76
77 default:
79 }
80 }
81
82 return bv;
83}
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:40
virtual bvt convert_case(const case_exprt &)
bv_utilst bv_utils
Definition boolbv.h:121
virtual std::size_t boolbv_width(const typet &type) const
Definition boolbv.h:106
Case expression: evaluates to the value corresponding to the first matching case.
Definition std_expr.h:3456
typet & type()
Return the type of the expression.
Definition expr.h:85
operandst & operands()
Definition expr.h:95
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
API to expression classes.