CBMC
boolbv_equality.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 <util/byte_operators.h>
10 #include <util/invariant.h>
11 #include <util/std_expr.h>
12 
13 #include "boolbv.h"
14 
16 {
17  const bool equality_types_match = expr.lhs().type() == expr.rhs().type();
19  equality_types_match,
20  "types of expressions on each side of equality should match",
23 
24  // see if it is an unbounded array
25  if(is_unbounded_array(expr.lhs().type()))
26  {
27  // flatten byte_update/byte_extract operators if needed
28 
29  if(has_byte_operator(expr))
30  {
31  return record_array_equality(
33  }
34 
35  return record_array_equality(expr);
36  }
37 
38  const bvt &lhs_bv = convert_bv(expr.lhs());
39  const bvt &rhs_bv = convert_bv(expr.rhs());
40 
42  lhs_bv.size() == rhs_bv.size(),
43  "sizes of lhs and rhs bitvectors should match",
44  irep_pretty_diagnosticst{expr.lhs()},
45  "lhs size: " + std::to_string(lhs_bv.size()),
46  irep_pretty_diagnosticst{expr.rhs()},
47  "rhs size: " + std::to_string(rhs_bv.size()));
48 
49  if(lhs_bv.empty())
50  {
51  // An empty bit-vector comparison. It's not clear
52  // what this is meant to say.
53  return prop.new_variable();
54  }
55 
56  return bv_utils.equal(lhs_bv, rhs_bv);
57 }
58 
60  const binary_relation_exprt &expr)
61 {
62  // This is 4-valued comparison, i.e., z===z, x===x etc.
63  // The result is always Boolean.
64 
66  expr.lhs().type() == expr.rhs().type(),
67  "lhs and rhs types should match in verilog_case_equality",
68  irep_pretty_diagnosticst{expr.lhs()},
69  irep_pretty_diagnosticst{expr.rhs()});
70 
71  const bvt &lhs_bv = convert_bv(expr.lhs());
72  const bvt &rhs_bv = convert_bv(expr.rhs());
73 
75  lhs_bv.size() == rhs_bv.size(),
76  "bitvector arguments to verilog_case_equality should have the same size",
77  irep_pretty_diagnosticst{expr.lhs()},
78  "lhs size: " + std::to_string(lhs_bv.size()),
79  irep_pretty_diagnosticst{expr.rhs()},
80  "rhs size: " + std::to_string(rhs_bv.size()));
81 
82  if(expr.id()==ID_verilog_case_inequality)
83  return !bv_utils.equal(lhs_bv, rhs_bv);
84  else
85  return bv_utils.equal(lhs_bv, rhs_bv);
86 }
bool has_byte_operator(const exprt &src)
Return true iff src or one of its operands contain a byte extract or byte update expression.
Expression classes for byte-level operators.
exprt lower_byte_operators(const exprt &src, const namespacet &ns)
Rewrite an expression possibly containing byte-extract or -update expressions to more fundamental ope...
const namespacet & ns
Definition: arrays.h:56
literalt record_array_equality(const equal_exprt &expr)
Definition: arrays.cpp:55
exprt & lhs()
Definition: std_expr.h:668
exprt & rhs()
Definition: std_expr.h:678
A base class for relations, i.e., binary predicates whose two operands have the same type.
Definition: std_expr.h:762
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
bool is_unbounded_array(const typet &type) const override
Definition: boolbv.cpp:533
virtual literalt convert_verilog_case_equality(const binary_relation_exprt &expr)
bv_utilst bv_utils
Definition: boolbv.h:117
virtual literalt convert_equality(const equal_exprt &expr)
literalt equal(const bvt &op0, const bvt &op1)
Bit-blasting ID_equal and use in other encodings.
Definition: bv_utils.cpp:1369
Equality.
Definition: std_expr.h:1361
typet & type()
Return the type of the expression.
Definition: expr.h:84
const irep_idt & id() const
Definition: irep.h:388
virtual literalt new_variable()=0
std::vector< literalt > bvt
Definition: literal.h:201
#define DATA_INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON,...)
Definition: invariant.h:535
API to expression classes.
const equal_exprt & to_equal_expr(const exprt &expr)
Cast an exprt to an equal_exprt.
Definition: std_expr.h:1402
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.