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_index_as_int = numeric_cast<mp_integer>(expr.index());
21 
22  // We only do constants for now.
23  // Should implement a shift here.
24  if(!maybe_index_as_int.has_value())
25  return conversion_failed(expr);
26 
27  auto index_as_int = maybe_index_as_int.value();
28 
30  index_as_int >= 0 && index_as_int < src_bv.size(),
31  "index of extractbits must be within the bitvector",
32  expr.find_source_location(),
34 
36  index_as_int + bv_width - 1 < src_bv.size(),
37  "index+width-1 of extractbits must be within the bitvector",
38  expr.find_source_location(),
40 
41  const std::size_t offset = numeric_cast_v<std::size_t>(index_as_int);
42 
43  bvt result_bv(src_bv.begin() + offset, src_bv.begin() + offset + bv_width);
44 
45  return result_bv;
46 }
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:39
boolbv_widtht bv_width
Definition: boolbv.h:116
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:94
virtual std::size_t boolbv_width(const typet &type) const
Definition: boolbv.h:102
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_WITH_DIAGNOSTICS(CONDITION, REASON,...)
Definition: invariant.h:535