cprover
Loading...
Searching...
No Matches
boolbv_extractbits.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: 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
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",
36
38 lower_as_int >= 0 && lower_as_int < src_bv.size(),
39 "lower end of extracted bits must be within the bitvector",
42
45 "upper bound must be greater or equal to lower bound");
46
47 // now lower_as_int <= upper_as_int
48
51 "the difference between upper and lower end of the range must have the "
52 "same width as the resulting bitvector type",
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.
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:563
boolbv_widtht bv_width
Definition boolbv.h:113
virtual bvt convert_extractbits(const extractbits_exprt &expr)
virtual const bvt & convert_bv(const exprt &expr, const optionalt< std::size_t > expected_width={})
Convert expression to vector of literalts, using an internal cache to speed up conversion if availabl...
Definition boolbv.cpp:38
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:82
virtual std::size_t boolbv_width(const typet &type) const
Definition boolbv.h:99
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