diff --git a/src/solvers/flattening/boolbv_typecast.cpp b/src/solvers/flattening/boolbv_typecast.cpp index d5f8c69bf8a..6f14f1c6d1a 100644 --- a/src/solvers/flattening/boolbv_typecast.cpp +++ b/src/solvers/flattening/boolbv_typecast.cpp @@ -6,21 +6,20 @@ Author: Daniel Kroening, kroening@kroening.com \*******************************************************************/ -#include "boolbv.h" - -#include "boolbv_type.h" -#include "c_bit_field_replacement_type.h" - #include #include #include #include +#include "boolbv.h" +#include "boolbv_type.h" +#include "c_bit_field_replacement_type.h" + bvt boolbvt::convert_bv_typecast(const typecast_exprt &expr) { - const exprt &op=expr.op(); - const bvt &op_bv=convert_bv(op); + const exprt &op = expr.op(); + const bvt &op_bv = convert_bv(op); bvt bv; @@ -31,55 +30,55 @@ bvt boolbvt::convert_bv_typecast(const typecast_exprt &expr) } bool boolbvt::type_conversion( - const typet &src_type, const bvt &src, - const typet &dest_type, bvt &dest) + const typet &src_type, + const bvt &src, + const typet &dest_type, + bvt &dest) { - bvtypet dest_bvtype=get_bvtype(dest_type); - bvtypet src_bvtype=get_bvtype(src_type); - - if(src_bvtype==bvtypet::IS_C_BIT_FIELD) - return - type_conversion( - c_bit_field_replacement_type(to_c_bit_field_type(src_type), ns), - src, - dest_type, - dest); - - if(dest_bvtype==bvtypet::IS_C_BIT_FIELD) - return - type_conversion( - src_type, - src, - c_bit_field_replacement_type(to_c_bit_field_type(dest_type), ns), - dest); - - std::size_t src_width=src.size(); - std::size_t dest_width=boolbv_width(dest_type); - - if(dest_width==0 || src_width==0) + bvtypet dest_bvtype = get_bvtype(dest_type); + bvtypet src_bvtype = get_bvtype(src_type); + + if(src_bvtype == bvtypet::IS_C_BIT_FIELD) + return type_conversion( + c_bit_field_replacement_type(to_c_bit_field_type(src_type), ns), + src, + dest_type, + dest); + + if(dest_bvtype == bvtypet::IS_C_BIT_FIELD) + return type_conversion( + src_type, + src, + c_bit_field_replacement_type(to_c_bit_field_type(dest_type), ns), + dest); + + std::size_t src_width = src.size(); + std::size_t dest_width = boolbv_width(dest_type); + + if(dest_width == 0 || src_width == 0) return true; dest.clear(); dest.reserve(dest_width); - if(dest_type.id()==ID_complex) + if(dest_type.id() == ID_complex) { if(src_type == to_complex_type(dest_type).subtype()) { dest.insert(dest.end(), src.begin(), src.end()); // pad with zeros - for(std::size_t i=src.size(); i= dest_width, "cannot extend bv-typed bitvector"); + dest = src; + if(dest_width < src_width) + dest.resize(dest_width); + return false; - case bvtypet::IS_C_BIT_FIELD: - case bvtypet::IS_UNKNOWN: - case bvtypet::IS_RANGE: - case bvtypet::IS_VERILOG_UNSIGNED: - case bvtypet::IS_VERILOG_SIGNED: - case bvtypet::IS_FIXED: - if(src_type.id()==ID_bool) - { - // bool to float + case bvtypet::IS_C_BIT_FIELD: + case bvtypet::IS_UNKNOWN: + case bvtypet::IS_RANGE: + case bvtypet::IS_VERILOG_UNSIGNED: + case bvtypet::IS_VERILOG_SIGNED: + case bvtypet::IS_FIXED: + if(src_type.id() == ID_bool) + { + // bool to float - // build a one - ieee_floatt f(to_floatbv_type(dest_type)); - f.from_integer(1); + // build a one + ieee_floatt f(to_floatbv_type(dest_type)); + f.from_integer(1); - dest=convert_bv(f.to_expr()); + dest = convert_bv(f.to_expr()); - INVARIANT( - src_width == 1, "bitvector of type boolean shall have width one"); + INVARIANT( + src_width == 1, "bitvector of type boolean shall have width one"); - for(auto &literal : dest) - literal = prop.land(literal, src[0]); + for(auto &literal : dest) + literal = prop.land(literal, src[0]); - return false; - } + return false; } } - break; + } + break; case bvtypet::IS_FIXED: - if(src_bvtype==bvtypet::IS_FIXED) + if(src_bvtype == bvtypet::IS_FIXED) { // fixed to fixed - std::size_t dest_fraction_bits= + std::size_t dest_fraction_bits = to_fixedbv_type(dest_type).get_fraction_bits(); - std::size_t dest_int_bits=dest_width-dest_fraction_bits; - std::size_t op_fraction_bits= + std::size_t dest_int_bits = dest_width - dest_fraction_bits; + std::size_t op_fraction_bits = to_fixedbv_type(src_type).get_fraction_bits(); - std::size_t op_int_bits=src_width-op_fraction_bits; + std::size_t op_int_bits = src_width - op_fraction_bits; dest.resize(dest_width); // i == position after dot // i == 0: first position after dot - for(std::size_t i=0; i= dest_width, "cannot extend bv-typed bitvector"); + dest = src; + if(dest_width < src_width) + dest.resize(dest_width); return false; } - else if(src_bvtype==bvtypet::IS_UNSIGNED || - src_bvtype==bvtypet::IS_SIGNED || - src_bvtype==bvtypet::IS_C_BOOL || - src_bvtype==bvtypet::IS_C_ENUM) + else if( + src_bvtype == bvtypet::IS_UNSIGNED || src_bvtype == bvtypet::IS_SIGNED || + src_bvtype == bvtypet::IS_C_BOOL || src_bvtype == bvtypet::IS_C_ENUM) { // integer to fixed - std::size_t dest_fraction_bits= + std::size_t dest_fraction_bits = to_fixedbv_type(dest_type).get_fraction_bits(); - for(std::size_t i=0; i= dest_width, "cannot extend bv-typed bitvector"); + dest = src; + if(dest_width < src_width) + dest.resize(dest_width); + return false; - INVARIANT( - src_width == 1, "bitvector of type boolean shall have width one"); + case bvtypet::IS_RANGE: + case bvtypet::IS_C_BIT_FIELD: + case bvtypet::IS_UNKNOWN: + if(src_type.id() == ID_bool) + { + // bool to integer - for(std::size_t i = 0; i < dest_width; i++) - { - if(i == 0) - dest.push_back(src[0]); - else - dest.push_back(const_literal(false)); - } + INVARIANT( + src_width == 1, "bitvector of type boolean shall have width one"); - return false; + for(std::size_t i = 0; i < dest_width; i++) + { + if(i == 0) + dest.push_back(src[0]); + else + dest.push_back(const_literal(false)); } + + return false; + } } break; + } case bvtypet::IS_VERILOG_UNSIGNED: - if(src_bvtype==bvtypet::IS_UNSIGNED || - src_bvtype==bvtypet::IS_C_BOOL || - src_type.id()==ID_bool) + if( + src_bvtype == bvtypet::IS_UNSIGNED || src_bvtype == bvtypet::IS_C_BOOL || + src_type.id() == ID_bool) { - for(std::size_t i=0, j=0; i= dest_width, "cannot extend bv-typed bitvector"); + dest = src; + if(dest_width < src_width) + dest.resize(dest_width); return false; case bvtypet::IS_C_BOOL: dest.resize(dest_width, const_literal(false)); - if(src_bvtype==bvtypet::IS_FLOAT) + if(src_bvtype == bvtypet::IS_FLOAT) { float_utilst float_utils(prop, to_floatbv_type(src_type)); - dest[0]=!float_utils.is_zero(src); + dest[0] = !float_utils.is_zero(src); } - else if(src_bvtype==bvtypet::IS_C_BOOL) - dest[0]=src[0]; + else if(src_bvtype == bvtypet::IS_C_BOOL) + dest[0] = src[0]; else - dest[0]=!bv_utils.is_zero(src); + dest[0] = !bv_utils.is_zero(src); return false; case bvtypet::IS_C_BIT_FIELD: case bvtypet::IS_UNKNOWN: case bvtypet::IS_VERILOG_SIGNED: - if(dest_type.id()==ID_array) + if(dest_type.id() == ID_array) { - if(src_width==dest_width) + if(src_width == dest_width) { - dest=src; + dest = src; return false; } } @@ -552,11 +550,9 @@ bool boolbvt::type_conversion( const struct_typet &op_struct = to_struct_type(ns.follow(src_type)); - const struct_typet::componentst &dest_comp= - dest_struct.components(); + const struct_typet::componentst &dest_comp = dest_struct.components(); - const struct_typet::componentst &op_comp= - op_struct.components(); + const struct_typet::componentst &op_comp = op_struct.components(); // build offset maps const offset_mapt op_offsets = build_offset_map(op_struct); @@ -566,44 +562,41 @@ bool boolbvt::type_conversion( typedef std::map op_mapt; op_mapt op_map; - for(std::size_t i=0; isecond].type()) + if(dest_comp[i].type() != dest_comp[it->second].type()) { // filling with free variables - for(std::size_t j=0; jsecond]; - for(std::size_t j=0; jsecond]; + for(std::size_t j = 0; j < comp_width; j++) + dest[offset + j] = src[op_offset + j]; } } } @@ -624,9 +617,9 @@ literalt boolbvt::convert_typecast(const typecast_exprt &expr) mp_integer from = string2integer(expr.op().type().get_string(ID_from)); mp_integer to = string2integer(expr.op().type().get_string(ID_to)); - if(from==1 && to==1) + if(from == 1 && to == 1) return const_literal(true); - else if(from==0 && to==0) + else if(from == 0 && to == 0) return const_literal(false); }