CBMC
boolbv_extractbits.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/arith_tools.h>
12 #include <util/bitvector_expr.h>
13 
15 {
16  const std::size_t bv_width = boolbv_width(expr.type());
17 
18  auto const &src_bv = convert_bv(expr.src());
19 
20  auto const maybe_upper_as_int = numeric_cast<mp_integer>(expr.upper());
21  auto const maybe_lower_as_int = numeric_cast<mp_integer>(expr.lower());
22 
23  // We only do constants for now.
24  // Should implement a shift here.
25  if(!maybe_upper_as_int.has_value() || !maybe_lower_as_int.has_value())
26  return conversion_failed(expr);
27 
28  auto upper_as_int = maybe_upper_as_int.value();
29  auto lower_as_int = maybe_lower_as_int.value();
30 
32  upper_as_int >= 0 && upper_as_int < src_bv.size(),
33  "upper end of extracted bits must be within the bitvector",
34  expr.find_source_location(),
36 
38  lower_as_int >= 0 && lower_as_int < src_bv.size(),
39  "lower end of extracted bits must be within the bitvector",
40  expr.find_source_location(),
42 
44  lower_as_int <= upper_as_int,
45  "upper bound must be greater or equal to lower bound");
46 
47  // now lower_as_int <= upper_as_int
48 
50  (upper_as_int - lower_as_int + 1) == bv_width,
51  "the difference between upper and lower end of the range must have the "
52  "same width as the resulting bitvector type",
53  expr.find_source_location(),
55 
56  const std::size_t offset = numeric_cast_v<std::size_t>(lower_as_int);
57 
58  bvt result_bv(src_bv.begin() + offset, src_bv.begin() + offset + bv_width);
59 
60  return result_bv;
61 }
API to expression classes for bitvectors.
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:37
boolbv_widtht bv_width
Definition: boolbv.h:115
virtual bvt convert_extractbits(const extractbits_exprt &expr)
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
virtual std::size_t boolbv_width(const typet &type) const
Definition: boolbv.h:101
const source_locationt & find_source_location() const
Get a source_locationt from the expression or from its operands (non-recursively).
Definition: expr.cpp:147
typet & type()
Return the type of the expression.
Definition: expr.h:84
Extracts a sub-range of a bit-vector operand.
std::vector< literalt > bvt
Definition: literal.h:201
#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 DATA_INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON,...)
Definition: invariant.h:535