Skip to content

Commit b33daaa

Browse files
committed
SMV: bit selection operator
This adds the [ : ] bit selection operator to the SMV frontend.
1 parent 6732c86 commit b33daaa

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/smvlang/expr2smv.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,32 @@ expr2smvt::resultt expr2smvt::convert_binary_associative(
188188

189189
/*******************************************************************\
190190
191+
Function: expr2smvt::convert_extractbits
192+
193+
Inputs:
194+
195+
Outputs:
196+
197+
Purpose:
198+
199+
\*******************************************************************/
200+
201+
expr2smvt::resultt expr2smvt::convert_extractbits(const extractbits_exprt &expr)
202+
{
203+
// we can do constant indices only
204+
if(!expr.index().is_constant())
205+
return convert_norep(expr);
206+
207+
auto op_rec = convert_rec(expr.src());
208+
auto high_s = integer2string(
209+
numeric_cast_v<mp_integer>(to_constant_expr(expr.index())) +
210+
to_bitvector_type(expr.type()).width() - 1);
211+
auto low_s = convert_rec(expr.index()).s;
212+
return {precedencet::INDEX, op_rec.s + '[' + high_s + ':' + low_s + ']'};
213+
}
214+
215+
/*******************************************************************\
216+
191217
Function: expr2smvt::convert_function_application
192218
193219
Inputs:
@@ -834,6 +860,9 @@ expr2smvt::resultt expr2smvt::convert_rec(const exprt &src)
834860
else if(src.id() == ID_smv_unsigned_cast)
835861
return convert_function_application("unsigned", src);
836862

863+
else if(src.id() == ID_extractbits)
864+
return convert_extractbits(to_extractbits_expr(src));
865+
837866
else if(src.id() == ID_typecast)
838867
{
839868
return convert_typecast(to_typecast_expr(src));

src/smvlang/expr2smv_class.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Author: Daniel Kroening, [email protected]
1010
#define CPROVER_SMV_EXPR2SMV_CLASS_H
1111

1212
#include <util/arith_tools.h>
13+
#include <util/bitvector_expr.h>
1314
#include <util/bitvector_types.h>
1415
#include <util/namespace.h>
1516
#include <util/std_expr.h>
@@ -123,6 +124,8 @@ class expr2smvt
123124

124125
resultt convert_cond(const exprt &);
125126

127+
resultt convert_extractbits(const extractbits_exprt &);
128+
126129
resultt
127130
convert_function_application(const std::string &symbol, const exprt &);
128131

src/smvlang/parser.y

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ static void new_module(YYSTYPE &module)
297297
%left COLONCOLON_Token
298298
%left UMINUS /* supplies precedence for unary minus */
299299
%left DOT_Token
300+
%nonassoc '['
300301

301302
%%
302303

@@ -690,6 +691,8 @@ term : variable_identifier
690691
| term mod_Token term { binary($$, $1, ID_mod, $3); }
691692
| term GTGT_Token term { binary($$, $1, ID_shr, $3); }
692693
| term LTLT_Token term { binary($$, $1, ID_shl, $3); }
694+
| term '[' NUMBER_Token ',' NUMBER_Token ']' // bit selection
695+
{ init($$, ID_extractbits); mto($$, $1); mto($$, $3); mto($$, $5); }
693696
| term COLONCOLON_Token term { binary($$, $1, ID_concatenation, $3); }
694697
| term TIMES_Token term { binary($$, $1, ID_mult, $3); }
695698
| term DIVIDE_Token term { binary($$, $1, ID_div, $3); }

0 commit comments

Comments
 (0)