Skip to content

Commit d451ec0

Browse files
committed
Add conversion of minus_exprt with both operands being pointers
1 parent 83454a1 commit d451ec0

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/solvers/smt2_incremental/convert_expr_to_smt.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,11 +644,44 @@ static smt_termt convert_expr_to_smt(
644644
can_cast_type<integer_bitvector_typet>(minus.lhs().type()) &&
645645
can_cast_type<integer_bitvector_typet>(minus.rhs().type());
646646

647+
const bool lhs_is_pointer = can_cast_type<pointer_typet>(minus.lhs().type());
648+
const bool rhs_is_pointer = can_cast_type<pointer_typet>(minus.rhs().type());
649+
650+
const bool both_operands_pointers = lhs_is_pointer && rhs_is_pointer;
651+
652+
// We don't really handle this - we just compute this to fall
653+
// into an if-else branch that gives proper error handling information.
654+
const bool one_operand_pointer = lhs_is_pointer || rhs_is_pointer;
655+
647656
if(both_operands_bitvector)
648657
{
649658
return smt_bit_vector_theoryt::subtract(
650659
converted.at(minus.lhs()), converted.at(minus.rhs()));
651660
}
661+
else if(both_operands_pointers)
662+
{
663+
const auto lhs_base_type = to_pointer_type(minus.lhs().type()).base_type();
664+
const auto rhs_base_type = to_pointer_type(minus.rhs().type()).base_type();
665+
INVARIANT(
666+
lhs_base_type == rhs_base_type,
667+
"only pointers of the same object type can be subtracted.");
668+
return smt_bit_vector_theoryt::unsigned_divide(
669+
smt_bit_vector_theoryt::subtract(
670+
converted.at(minus.lhs()), converted.at(minus.rhs())),
671+
pointer_sizes.at(lhs_base_type));
672+
}
673+
else if(one_operand_pointer)
674+
{
675+
UNIMPLEMENTED_FEATURE(
676+
"convert_expr_to_smt::minus_exprt doesn't handle expressions where"
677+
"only one operand is a pointer - this is because these expressions"
678+
"are normally handled by convert_expr_to_smt::plus_exprt due to"
679+
"transformations of the expressions by previous passes bringing"
680+
"them into a form more suitably handled by that version of the function."
681+
"If you are here, this is a mistake or something went wrong before."
682+
"The expression that caused the problem is: " +
683+
minus.pretty());
684+
}
652685
else
653686
{
654687
UNIMPLEMENTED_FEATURE(
@@ -1452,7 +1485,7 @@ static smt_termt dispatch_expr_to_smt_conversion(
14521485
}
14531486
if(const auto minus = expr_try_dynamic_cast<minus_exprt>(expr))
14541487
{
1455-
return convert_expr_to_smt(*minus, converted);
1488+
return convert_expr_to_smt(*minus, converted, pointer_sizes);
14561489
}
14571490
if(const auto divide = expr_try_dynamic_cast<div_exprt>(expr))
14581491
{

0 commit comments

Comments
 (0)