33 const typet &src_type,
35 const typet &dest_type,
55 std::size_t src_width = src.size();
58 if(dest_width == 0 || src_width == 0)
62 dest.reserve(dest_width);
64 if(dest_type.
id() == ID_complex)
68 dest.insert(dest.end(), src.begin(), src.end());
71 for(std::size_t i = src.size(); i < dest_width; i++)
76 else if(src_type.
id() == ID_complex)
79 bvt lower, upper, lower_res, upper_res;
80 lower.assign(src.begin(), src.begin() + src.size() / 2);
81 upper.assign(src.begin() + src.size() / 2, src.end());
93 lower_res.size() + upper_res.size() == dest_width,
94 "lower result bitvector size plus upper result bitvector size shall "
95 "equal the destination bitvector size");
97 dest.insert(dest.end(), upper_res.begin(), upper_res.end());
102 if(src_type.
id() == ID_complex)
105 dest_type.
id() == ID_complex,
106 "destination type shall be of complex type when source type is of "
109 dest_type.
id() == ID_signedbv || dest_type.
id() == ID_unsignedbv ||
110 dest_type.
id() == ID_floatbv || dest_type.
id() == ID_fixedbv ||
111 dest_type.
id() == ID_c_enum || dest_type.
id() == ID_c_enum_tag ||
112 dest_type.
id() == ID_bool)
117 tmp_src.resize(src.size() / 2);
161 else if(src_type.
id() == ID_bool)
197 INVARIANT(src_width >= dest_width,
"cannot extend bv-typed bitvector");
199 if(dest_width < src_width)
200 dest.resize(dest_width);
209 if(src_type.
id() == ID_bool)
218 src_width == 1,
"bitvector of type boolean shall have width one");
220 for(
auto &literal : dest)
221 literal =
prop.
land(literal, src[0]);
234 std::size_t dest_fraction_bits =
236 std::size_t dest_int_bits = dest_width - dest_fraction_bits;
237 std::size_t op_fraction_bits =
239 std::size_t op_int_bits = src_width - op_fraction_bits;
241 dest.resize(dest_width);
246 for(std::size_t i = 0; i < dest_fraction_bits; i++)
249 std::size_t p = dest_fraction_bits - i - 1;
251 if(i < op_fraction_bits)
252 dest[p] = src[op_fraction_bits - i - 1];
257 for(std::size_t i = 0; i < dest_int_bits; i++)
260 std::size_t p = dest_fraction_bits + i;
261 INVARIANT(p < dest_width,
"bit index shall be within bounds");
264 dest[p] = src[i + op_fraction_bits];
266 dest[p] = src[src_width - 1];
273 INVARIANT(src_width >= dest_width,
"cannot extend bv-typed bitvector");
275 if(dest_width < src_width)
276 dest.resize(dest_width);
285 std::size_t dest_fraction_bits =
288 for(std::size_t i = 0; i < dest_fraction_bits; i++)
291 for(std::size_t i = 0; i < dest_width - dest_fraction_bits; i++)
302 l = src[src_width - 1];
312 else if(src_type.
id() == ID_bool)
315 std::size_t fraction_bits =
319 src_width == 1,
"bitvector of type boolean shall have width one");
321 for(std::size_t i = 0; i < dest_width; i++)
323 if(i == fraction_bits)
324 dest.push_back(src[0]);
346 std::size_t op_fraction_bits =
349 for(std::size_t i = 0; i < dest_width; i++)
351 if(i < src_width - op_fraction_bits)
352 dest.push_back(src[i + op_fraction_bits]);
356 dest.push_back(src[src_width - 1]);
365 bvt fraction_bits_bv = src;
366 fraction_bits_bv.resize(op_fraction_bits);
383 bool sign_extension =
386 for(std::size_t i = 0; i < dest_width; i++)
389 dest.push_back(src[i]);
390 else if(sign_extension)
391 dest.push_back(src[src_width - 1]);
401 for(std::size_t i = 0; i < dest_width; i++)
403 std::size_t src_index = i * 2;
405 if(src_index < src_width)
406 dest.push_back(src[src_index]);
417 for(std::size_t i = 0; i < dest_width; i++)
419 std::size_t src_index = i * 2;
421 if(src_index < src_width)
422 dest.push_back(src[src_index]);
424 dest.push_back(src.back());
432 INVARIANT(src_width >= dest_width,
"cannot extend bv-typed bitvector");
434 if(dest_width < src_width)
435 dest.resize(dest_width);
441 if(src_type.
id() == ID_bool)
446 src_width == 1,
"bitvector of type boolean shall have width one");
448 for(std::size_t i = 0; i < dest_width; i++)
451 dest.push_back(src[0]);
465 src_type.
id() == ID_bool)
467 for(std::size_t i = 0, j = 0; i < dest_width; i += 2, j++)
470 dest.push_back(src[j]);
481 for(std::size_t i = 0, j = 0; i < dest_width; i += 2, j++)
484 dest.push_back(src[j]);
486 dest.push_back(src.back());
498 if(dest_width < src_width)
499 dest.resize(dest_width);
503 while(dest.size() < dest_width)
514 INVARIANT(src_width >= dest_width,
"cannot extend bv-typed bitvector");
516 if(dest_width < src_width)
517 dest.resize(dest_width);
526 dest[0] = !float_utils.
is_zero(src);
538 if(dest_type.
id() == ID_array)
540 if(src_width == dest_width)
546 else if(dest_type.
id() == ID_struct || dest_type.
id() == ID_struct_tag)
549 dest_type.
id() == ID_struct_tag
553 if(src_type.
id() == ID_struct || src_type.
id() == ID_struct_tag)
560 src_type.
id() == ID_struct_tag
573 typedef std::map<irep_idt, std::size_t> op_mapt;
576 for(std::size_t i = 0; i < op_comp.size(); i++)
577 op_map[op_comp[i].get_name()] = i;
580 for(std::size_t i = 0; i < dest_comp.size(); i++)
582 std::size_t offset = dest_offsets[i];
583 std::size_t comp_width =
boolbv_width(dest_comp[i].type());
587 op_mapt::const_iterator it = op_map.find(dest_comp[i].get_name());
589 if(it == op_map.end())
594 for(std::size_t j = 0; j < comp_width; j++)
600 if(dest_comp[i].type() != dest_comp[it->second].type())
603 for(std::size_t j = 0; j < comp_width; j++)
608 std::size_t op_offset = op_offsets[it->second];
609 for(std::size_t j = 0; j < comp_width; j++)
610 dest[offset + j] = src[op_offset + j];
626 if(expr.
op().
type().
id() == ID_range)
631 if(from == 1 && to == 1)
633 else if(from == 0 && to == 0)
645 if(expr.
op().
type().
id() == ID_floatbv)
648 return !float_utils.
is_zero(bv);
Pre-defined bitvector types.
const fixedbv_typet & to_fixedbv_type(const typet &type)
Cast a typet to a fixedbv_typet.
const floatbv_typet & to_floatbv_type(const typet &type)
Cast a typet to a floatbv_typet.
bvtypet get_bvtype(const typet &type)
typet c_bit_field_replacement_type(const c_bit_field_typet &src, const namespacet &ns)
const c_bit_field_typet & to_c_bit_field_type(const typet &type)
Cast a typet to a c_bit_field_typet.
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...
offset_mapt build_offset_map(const struct_typet &src)
virtual literalt convert_typecast(const typecast_exprt &expr)
conversion from bitvector types to boolean
bvt conversion_failed(const exprt &expr)
Print that the expression of x has failed conversion, then return a vector of x's width.
virtual std::size_t boolbv_width(const typet &type) const
virtual bvt convert_bv_typecast(const typecast_exprt &expr)
bool type_conversion(const typet &src_type, const bvt &src, const typet &dest_type, bvt &dest)
std::vector< std::size_t > offset_mapt
bvt add(const bvt &op0, const bvt &op1)
static bvt zero_extension(const bvt &bv, std::size_t new_size)
static bvt build_constant(const mp_integer &i, std::size_t width)
literalt is_zero(const bvt &op)
bvt incrementer(const bvt &op, literalt carry_in)
static bvt sign_extension(const bvt &bv, std::size_t new_size)
Base class for all expressions.
typet & type()
Return the type of the expression.
std::size_t get_fraction_bits() const
bvt from_unsigned_integer(const bvt &)
bvt from_signed_integer(const bvt &)
literalt is_zero(const bvt &)
static ieee_float_valuet one(const floatbv_typet &)
const irep_idt & id() const
const std::string & get_string(const irep_idt &name) const
const union_typet & follow_tag(const union_tag_typet &) const
Follow type tag of union type.
virtual literalt convert_rest(const exprt &expr)
virtual literalt land(literalt a, literalt b)=0
virtual literalt new_variable()=0
virtual literalt lor(literalt a, literalt b)=0
Structure type, corresponds to C style structs.
const componentst & components() const
std::vector< componentt > componentst
Semantic type conversion.
The type of an expression, extends irept.
std::vector< literalt > bvt
literalt const_literal(bool value)
const integer_range_typet & to_integer_range_type(const typet &type)
Cast a typet to a integer_range_typet.
const mp_integer string2integer(const std::string &n, unsigned base)
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
const complex_typet & to_complex_type(const typet &type)
Cast a typet to a complex_typet.