Skip to content

Commit ef59569

Browse files
committed
SMV: bit selection operator
This adds the [ : ] bit selection operator to the SMV frontend.
1 parent 3f0410b commit ef59569

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
@@ -184,6 +184,32 @@ expr2smvt::resultt expr2smvt::convert_binary_associative(
184184

185185
/*******************************************************************\
186186
187+
Function: expr2smvt::convert_extractbits
188+
189+
Inputs:
190+
191+
Outputs:
192+
193+
Purpose:
194+
195+
\*******************************************************************/
196+
197+
expr2smvt::resultt expr2smvt::convert_extractbits(const extractbits_exprt &expr)
198+
{
199+
// we can do constant indices only
200+
if(!expr.index().is_constant())
201+
return convert_norep(expr);
202+
203+
auto op_rec = convert_rec(expr.src());
204+
auto high_s = integer2string(
205+
numeric_cast_v<mp_integer>(to_constant_expr(expr.index())) +
206+
to_bitvector_type(expr.type()).width() - 1);
207+
auto low_s = convert_rec(expr.index()).s;
208+
return {precedencet::INDEX, op_rec.s + '[' + high_s + ':' + low_s + ']'};
209+
}
210+
211+
/*******************************************************************\
212+
187213
Function: expr2smvt::convert_function_application
188214
189215
Inputs:
@@ -712,6 +738,9 @@ expr2smvt::resultt expr2smvt::convert_rec(const exprt &src)
712738
else if(src.id() == ID_smv_unsigned_cast)
713739
return convert_function_application("unsigned", src);
714740

741+
else if(src.id() == ID_extractbits)
742+
return convert_extractbits(to_extractbits_expr(src));
743+
715744
else if(src.id() == ID_typecast)
716745
{
717746
return convert_rec(to_typecast_expr(src).op());

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_name
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)