Skip to content

Commit d498455

Browse files
committed
Add implementation and tests of conversion of left bit shift expression to bvshl.
1 parent d5526a3 commit d498455

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/solvers/smt2_incremental/convert_expr_to_smt.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,14 @@ static smt_termt convert_expr_to_smt(const index_exprt &index)
550550

551551
static smt_termt convert_expr_to_smt(const shift_exprt &shift)
552552
{
553+
// TODO: Dispatch into different types of shifting
554+
if(const auto left_shift = expr_try_dynamic_cast<shl_exprt>(shift))
555+
{
556+
return smt_bit_vector_theoryt::shift_left(
557+
convert_expr_to_smt(left_shift->op0()),
558+
convert_expr_to_smt(left_shift->op1()));
559+
}
560+
553561
// TODO: split into functions for separate types of shift including rotate.
554562
UNIMPLEMENTED_FEATURE(
555563
"Generation of SMT formula for shift expression: " + shift.pretty());

src/util/bitvector_expr.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ class shl_exprt : public shift_exprt
305305
}
306306
};
307307

308+
template <>
309+
inline bool can_cast_expr<shl_exprt>(const exprt &base)
310+
{
311+
return base.id() == ID_shl;
312+
}
313+
308314
/// \brief Cast an exprt to a \ref shl_exprt
309315
///
310316
/// \a expr must be known to be \ref shl_exprt.

unit/solvers/smt2_incremental/convert_expr_to_smt.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,3 +672,45 @@ SCENARIO(
672672
}
673673
}
674674
}
675+
676+
SCENARIO(
677+
"Left-shift expressions are converted to SMT terms",
678+
"[core][smt2_incremental]")
679+
{
680+
GIVEN("An integer bitvector and the number of places we're going to shift")
681+
{
682+
// This is going to act as both the value to be shifted, and a value
683+
// signifying the places to the left we're shifting.
684+
const auto one_bvint = from_integer({1}, signedbv_typet{8});
685+
686+
WHEN("We construct a shl_exprt and convert it to an SMT term")
687+
{
688+
const auto shift_expr = shl_exprt{one_bvint, one_bvint};
689+
const auto constructed_term = convert_expr_to_smt(shift_expr);
690+
691+
THEN("It should be equivalent to a bvshl term")
692+
{
693+
const smt_termt smt_term_one = smt_bit_vector_constant_termt{1, 8};
694+
const auto expected_term = smt_bit_vector_theoryt::shift_left(
695+
/* term */
696+
smt_term_one,
697+
/* distance */
698+
smt_term_one);
699+
}
700+
}
701+
702+
WHEN(
703+
"We construct a malformed shl_exprt and attempt to convert it to an SMT "
704+
"term")
705+
{
706+
const cbmc_invariants_should_throwt invariants_throw;
707+
THEN(
708+
"convert_expr_to_smt should throw an exception because of validation "
709+
"failure")
710+
{
711+
REQUIRE_THROWS(
712+
convert_expr_to_smt(shl_exprt{one_bvint, false_exprt{}}));
713+
}
714+
}
715+
}
716+
}

0 commit comments

Comments
 (0)