Skip to content

Commit 159bd43

Browse files
committed
Add implementation and tests for conversion of logical right shift expressions to SMT.
1 parent 2377d02 commit 159bd43

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/solvers/smt2_incremental/convert_expr_to_smt.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,13 @@ static smt_termt convert_expr_to_smt(const shift_exprt &shift)
557557
convert_expr_to_smt(left_shift->op0()),
558558
convert_expr_to_smt(left_shift->op1()));
559559
}
560+
else if(
561+
const auto right_logical_shift = expr_try_dynamic_cast<lshr_exprt>(shift))
562+
{
563+
return smt_bit_vector_theoryt::logical_shift_right(
564+
convert_expr_to_smt(right_logical_shift->op0()),
565+
convert_expr_to_smt(right_logical_shift->op1()));
566+
}
560567

561568
// TODO: split into functions for separate types of shift including rotate.
562569
UNIMPLEMENTED_FEATURE(

src/util/bitvector_expr.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,12 @@ class lshr_exprt : public shift_exprt
364364
}
365365
};
366366

367+
template <>
368+
inline bool can_cast_expr<lshr_exprt>(const exprt &base)
369+
{
370+
return base.id() == ID_lshr;
371+
}
372+
367373
/// \brief Extracts a single bit of a bit-vector operand
368374
class extractbit_exprt : public binary_predicate_exprt
369375
{

unit/solvers/smt2_incremental/convert_expr_to_smt.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,3 +714,44 @@ SCENARIO(
714714
}
715715
}
716716
}
717+
718+
SCENARIO(
719+
"Logical Right-shift expressions are converted to SMT terms",
720+
"[core][smt2_incremental]")
721+
{
722+
GIVEN("An integer bitvector")
723+
{
724+
const auto one_bvint = from_integer({1}, signedbv_typet{8});
725+
726+
WHEN("We construct a lshr_exprt and convert it to an SMT term")
727+
{
728+
const auto shift_expr = lshr_exprt{one_bvint, one_bvint};
729+
const auto constructed_term = convert_expr_to_smt(shift_expr);
730+
731+
THEN("We should get an logical shift left SMT term")
732+
{
733+
const smt_termt smt_term_one = smt_bit_vector_constant_termt{1, 8};
734+
const auto expected_term = smt_bit_vector_theoryt::logical_shift_right(
735+
/* Value to be shifted */
736+
smt_term_one,
737+
/* Places */
738+
smt_term_one);
739+
REQUIRE(constructed_term == expected_term);
740+
}
741+
}
742+
743+
WHEN(
744+
"We construct a malformed lshr_exprt and attempt to convert it to an SMT"
745+
" term")
746+
{
747+
const cbmc_invariants_should_throwt invariants_throw;
748+
THEN(
749+
"convert_expr_to_smt should throw an exception because of validation "
750+
"failure")
751+
{
752+
REQUIRE_THROWS(
753+
convert_expr_to_smt(lshr_exprt{one_bvint, false_exprt{}}));
754+
}
755+
}
756+
}
757+
}

0 commit comments

Comments
 (0)