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);
135 dest.resize(dest_width);
136 for(std::size_t i = 0; i < dest.size(); i++)
147 if(dest_from == src_from)
190 INVARIANT(src_width >= dest_width,
"cannot extend bv-typed bitvector");
192 if(dest_width < src_width)
193 dest.resize(dest_width);
202 if(src_type.
id() == ID_bool)
213 src_width == 1,
"bitvector of type boolean shall have width one");
215 for(
auto &literal : dest)
216 literal =
prop.
land(literal, src[0]);
229 std::size_t dest_fraction_bits =
231 std::size_t dest_int_bits = dest_width - dest_fraction_bits;
232 std::size_t op_fraction_bits =
234 std::size_t op_int_bits = src_width - op_fraction_bits;
236 dest.resize(dest_width);
241 for(std::size_t i = 0; i < dest_fraction_bits; i++)
244 std::size_t p = dest_fraction_bits - i - 1;
246 if(i < op_fraction_bits)
247 dest[p] = src[op_fraction_bits - i - 1];
252 for(std::size_t i = 0; i < dest_int_bits; i++)
255 std::size_t p = dest_fraction_bits + i;
256 INVARIANT(p < dest_width,
"bit index shall be within bounds");
259 dest[p] = src[i + op_fraction_bits];
261 dest[p] = src[src_width - 1];
268 INVARIANT(src_width >= dest_width,
"cannot extend bv-typed bitvector");
270 if(dest_width < src_width)
271 dest.resize(dest_width);
280 std::size_t dest_fraction_bits =
283 for(std::size_t i = 0; i < dest_fraction_bits; i++)
286 for(std::size_t i = 0; i < dest_width - dest_fraction_bits; i++)
297 l = src[src_width - 1];
307 else if(src_type.
id() == ID_bool)
310 std::size_t fraction_bits =
314 src_width == 1,
"bitvector of type boolean shall have width one");
316 for(std::size_t i = 0; i < dest_width; i++)
318 if(i == fraction_bits)
319 dest.push_back(src[0]);
341 std::size_t op_fraction_bits =
344 for(std::size_t i = 0; i < dest_width; i++)
346 if(i < src_width - op_fraction_bits)
347 dest.push_back(src[i + op_fraction_bits]);
351 dest.push_back(src[src_width - 1]);
360 bvt fraction_bits_bv = src;
361 fraction_bits_bv.resize(op_fraction_bits);
378 bool sign_extension =
381 for(std::size_t i = 0; i < dest_width; i++)
384 dest.push_back(src[i]);
385 else if(sign_extension)
386 dest.push_back(src[src_width - 1]);
396 for(std::size_t i = 0; i < dest_width; i++)
398 std::size_t src_index = i * 2;
400 if(src_index < src_width)
401 dest.push_back(src[src_index]);
412 for(std::size_t i = 0; i < dest_width; i++)
414 std::size_t src_index = i * 2;
416 if(src_index < src_width)
417 dest.push_back(src[src_index]);
419 dest.push_back(src.back());
427 INVARIANT(src_width >= dest_width,
"cannot extend bv-typed bitvector");
429 if(dest_width < src_width)
430 dest.resize(dest_width);
436 if(src_type.
id() == ID_bool)
441 src_width == 1,
"bitvector of type boolean shall have width one");
443 for(std::size_t i = 0; i < dest_width; i++)
446 dest.push_back(src[0]);
460 src_type.
id() == ID_bool)
462 for(std::size_t i = 0, j = 0; i < dest_width; i += 2, j++)
465 dest.push_back(src[j]);
476 for(std::size_t i = 0, j = 0; i < dest_width; i += 2, j++)
479 dest.push_back(src[j]);
481 dest.push_back(src.back());
493 if(dest_width < src_width)
494 dest.resize(dest_width);
498 while(dest.size() < dest_width)
509 INVARIANT(src_width >= dest_width,
"cannot extend bv-typed bitvector");
511 if(dest_width < src_width)
512 dest.resize(dest_width);
521 dest[0] = !float_utils.
is_zero(src);
533 if(dest_type.
id() == ID_array)
535 if(src_width == dest_width)
541 else if(dest_type.
id() == ID_struct || dest_type.
id() == ID_struct_tag)
544 dest_type.
id() == ID_struct_tag
548 if(src_type.
id() == ID_struct || src_type.
id() == ID_struct_tag)
555 src_type.
id() == ID_struct_tag
568 typedef std::map<irep_idt, std::size_t> op_mapt;
571 for(std::size_t i = 0; i < op_comp.size(); i++)
572 op_map[op_comp[i].get_name()] = i;
575 for(std::size_t i = 0; i < dest_comp.size(); i++)
577 std::size_t offset = dest_offsets[i];
578 std::size_t comp_width =
boolbv_width(dest_comp[i].type());
582 op_mapt::const_iterator it = op_map.find(dest_comp[i].get_name());
584 if(it == op_map.end())
589 for(std::size_t j = 0; j < comp_width; j++)
595 if(dest_comp[i].type() != dest_comp[it->second].type())
598 for(std::size_t j = 0; j < comp_width; j++)
603 std::size_t op_offset = op_offsets[it->second];
604 for(std::size_t j = 0; j < comp_width; j++)
605 dest[offset + j] = src[op_offset + j];
621 if(expr.
op().
type().
id() == ID_range)
626 if(from == 1 && to == 1)
628 else if(from == 0 && to == 0)
Pre-defined bitvector types.
const floatbv_typet & to_floatbv_type(const typet &type)
Cast a typet to a floatbv_typet.
const fixedbv_typet & to_fixedbv_type(const typet &type)
Cast a typet to a fixedbv_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)
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 &)
constant_exprt to_expr() const
void from_integer(const mp_integer &i)
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
mp_integer get_from() const
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 mp_integer string2integer(const std::string &n, unsigned base)
const range_typet & to_range_type(const typet &type)
Cast a typet to a range_typet.
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
const complex_typet & to_complex_type(const typet &type)
Cast a typet to a complex_typet.
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.