From 92b4d655eca0cc756d8fe7c7ce74552962ed66c4 Mon Sep 17 00:00:00 2001 From: Michael Tautschnig Date: Sat, 4 Feb 2023 19:29:00 +0000 Subject: [PATCH 1/2] clang-format boolbv_typecast.cpp The nested `switch` statements had indentation mixed up, making it very difficult to understand which `switch` statement a `case` actually belonged to. --- src/solvers/flattening/boolbv_typecast.cpp | 535 ++++++++++----------- 1 file changed, 264 insertions(+), 271 deletions(-) diff --git a/src/solvers/flattening/boolbv_typecast.cpp b/src/solvers/flattening/boolbv_typecast.cpp index d5f8c69bf8a..a42576f9b68 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 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); } From b4def866c3484958391cac61d23074e8af7f927f Mon Sep 17 00:00:00 2001 From: Michael Tautschnig Date: Mon, 12 Dec 2022 16:16:08 +0000 Subject: [PATCH 2/2] Support (T)bv-typed conversion when T has smaller bit width We can safely reduce the number of bits of a bv (and not just signed/unsigned bv) typed expression by just cutting off more-significant bits. Fixes: #7426 --- src/solvers/flattening/boolbv_typecast.cpp | 24 +++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/solvers/flattening/boolbv_typecast.cpp b/src/solvers/flattening/boolbv_typecast.cpp index a42576f9b68..6f14f1c6d1a 100644 --- a/src/solvers/flattening/boolbv_typecast.cpp +++ b/src/solvers/flattening/boolbv_typecast.cpp @@ -187,10 +187,10 @@ bool boolbvt::type_conversion( return false; case bvtypet::IS_BV: - INVARIANT( - src_width == dest_width, - "source bitvector size shall equal the destination bitvector size"); + INVARIANT(src_width >= 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: @@ -265,10 +265,10 @@ bool boolbvt::type_conversion( } else if(src_bvtype == bvtypet::IS_BV) { - INVARIANT( - src_width == dest_width, - "source bitvector width shall equal the destination bitvector width"); + INVARIANT(src_width >= dest_width, "cannot extend bv-typed bitvector"); dest = src; + if(dest_width < src_width) + dest.resize(dest_width); return false; } else if( @@ -424,10 +424,10 @@ bool boolbvt::type_conversion( break; case bvtypet::IS_BV: - INVARIANT( - src_width == dest_width, - "source bitvector width shall equal the destination bitvector width"); + INVARIANT(src_width >= dest_width, "cannot extend bv-typed bitvector"); dest = src; + if(dest_width < src_width) + dest.resize(dest_width); return false; case bvtypet::IS_RANGE: @@ -506,10 +506,10 @@ bool boolbvt::type_conversion( break; case bvtypet::IS_BV: - INVARIANT( - src_width == dest_width, - "source bitvector width shall equal the destination bitvector width"); + INVARIANT(src_width >= 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: