Skip to content

Commit 7389a8d

Browse files
author
Daniel Kroening
committed
patternt now works on bytecodes
1 parent 25a1271 commit 7389a8d

File tree

3 files changed

+55
-73
lines changed

3 files changed

+55
-73
lines changed

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -590,19 +590,19 @@ void java_bytecode_convert_methodt::convert(
590590
to_java_class_type(class_symbol.type).lambda_method_handles());
591591
}
592592

593-
static irep_idt get_if_cmp_operator(const irep_idt &stmt)
593+
static irep_idt get_if_cmp_operator(const u1 bytecode)
594594
{
595-
if(stmt==patternt("if_?cmplt"))
595+
if(bytecode == patternt("if_?cmplt"))
596596
return ID_lt;
597-
if(stmt==patternt("if_?cmple"))
597+
if(bytecode == patternt("if_?cmple"))
598598
return ID_le;
599-
if(stmt==patternt("if_?cmpgt"))
599+
if(bytecode == patternt("if_?cmpgt"))
600600
return ID_gt;
601-
if(stmt==patternt("if_?cmpge"))
601+
if(bytecode == patternt("if_?cmpge"))
602602
return ID_ge;
603-
if(stmt==patternt("if_?cmpeq"))
603+
if(bytecode == patternt("if_?cmpeq"))
604604
return ID_equal;
605-
if(stmt==patternt("if_?cmpne"))
605+
if(bytecode == patternt("if_?cmpne"))
606606
return ID_notequal;
607607

608608
throw "unhandled java comparison instruction";
@@ -1001,7 +1001,7 @@ code_blockt java_bytecode_convert_methodt::convert_instructions(
10011001
// clang-format off
10021002
if(bytecode != BC_goto &&
10031003
bytecode != BC_return &&
1004-
statement != patternt("?return") &&
1004+
bytecode != patternt("?return") &&
10051005
bytecode != BC_athrow &&
10061006
bytecode != BC_jsr &&
10071007
bytecode != BC_jsr_w &&
@@ -1024,8 +1024,8 @@ code_blockt java_bytecode_convert_methodt::convert_instructions(
10241024
bytecode == BC_ldiv ||
10251025
bytecode == BC_irem ||
10261026
bytecode == BC_lrem ||
1027-
statement == patternt("?astore") ||
1028-
statement == patternt("?aload") ||
1027+
bytecode == patternt("?astore") ||
1028+
bytecode == patternt("?aload") ||
10291029
bytecode == BC_invokestatic ||
10301030
bytecode == BC_invokevirtual ||
10311031
bytecode == BC_invokespecial ||
@@ -1043,8 +1043,8 @@ code_blockt java_bytecode_convert_methodt::convert_instructions(
10431043

10441044
// clang-format off
10451045
if(bytecode == BC_goto ||
1046-
statement == patternt("if_?cmp??") ||
1047-
statement == patternt("if??") ||
1046+
bytecode == patternt("if_?cmp??") ||
1047+
bytecode == patternt("if??") ||
10481048
bytecode == BC_ifnonnull ||
10491049
bytecode == BC_ifnull ||
10501050
bytecode == BC_jsr ||
@@ -1266,7 +1266,7 @@ code_blockt java_bytecode_convert_methodt::convert_instructions(
12661266
PRECONDITION(op.empty() && results.empty());
12671267
c=code_returnt();
12681268
}
1269-
else if(statement==patternt("?return"))
1269+
else if(bytecode == patternt("?return"))
12701270
{
12711271
// Return types are promoted in java, so this might need
12721272
// conversion.
@@ -1275,25 +1275,24 @@ code_blockt java_bytecode_convert_methodt::convert_instructions(
12751275
typecast_exprt::conditional_cast(op[0], method_return_type);
12761276
c=code_returnt(r);
12771277
}
1278-
else if(statement==patternt("?astore"))
1278+
else if(bytecode == patternt("?astore"))
12791279
{
12801280
assert(op.size()==3 && results.empty());
12811281
c = convert_astore(statement, op, i_it->source_location);
12821282
}
1283-
else if(
1284-
statement == patternt("?store") || statement == patternt("?store_?"))
1283+
else if(bytecode == patternt("?store") || bytecode == patternt("?store_?"))
12851284
{
12861285
// store value into some local variable
12871286
PRECONDITION(op.size() == 1 && results.empty());
12881287
c = convert_store(
12891288
statement, arg0, op, i_it->address, i_it->source_location);
12901289
}
1291-
else if(statement==patternt("?aload"))
1290+
else if(bytecode == patternt("?aload"))
12921291
{
12931292
PRECONDITION(op.size() == 2 && results.size() == 1);
12941293
results[0] = convert_aload(statement, op);
12951294
}
1296-
else if(statement == patternt("?load") || statement == patternt("?load_?"))
1295+
else if(bytecode == patternt("?load") || bytecode == patternt("?load_?"))
12971296
{
12981297
// load a value from a local variable
12991298
results[0]=
@@ -1347,28 +1346,28 @@ code_blockt java_bytecode_convert_methodt::convert_instructions(
13471346
assert(results.size()==1);
13481347
results[0]=from_integer(-1, java_int_type());
13491348
}
1350-
else if(statement == patternt("?const_?"))
1349+
else if(bytecode == patternt("?const_?"))
13511350
{
13521351
assert(results.size()==1);
13531352
results = convert_const(statement, to_constant_expr(arg0), results);
13541353
}
1355-
else if(statement==patternt("?ipush"))
1354+
else if(bytecode == patternt("?ipush"))
13561355
{
13571356
PRECONDITION(results.size()==1);
13581357
DATA_INVARIANT(
13591358
arg0.id()==ID_constant,
13601359
"ipush argument expected to be constant");
13611360
results[0] = typecast_exprt::conditional_cast(arg0, java_int_type());
13621361
}
1363-
else if(statement==patternt("if_?cmp??"))
1362+
else if(bytecode == patternt("if_?cmp??"))
13641363
{
13651364
PRECONDITION(op.size() == 2 && results.empty());
13661365
const mp_integer number =
13671366
numeric_cast_v<mp_integer>(to_constant_expr(arg0));
13681367
c = convert_if_cmp(
1369-
address_map, statement, op, number, i_it->source_location);
1368+
address_map, bytecode, op, number, i_it->source_location);
13701369
}
1371-
else if(statement==patternt("if??"))
1370+
else if(bytecode == patternt("if??"))
13721371
{
13731372
// clang-format off
13741373
const irep_idt id=
@@ -1387,14 +1386,14 @@ code_blockt java_bytecode_convert_methodt::convert_instructions(
13871386
numeric_cast_v<mp_integer>(to_constant_expr(arg0));
13881387
c = convert_if(address_map, op, id, number, i_it->source_location);
13891388
}
1390-
else if(statement==patternt("ifnonnull"))
1389+
else if(bytecode == patternt("ifnonnull"))
13911390
{
13921391
PRECONDITION(op.size() == 1 && results.empty());
13931392
const mp_integer number =
13941393
numeric_cast_v<mp_integer>(to_constant_expr(arg0));
13951394
c = convert_ifnonull(address_map, op, number, i_it->source_location);
13961395
}
1397-
else if(statement==patternt("ifnull"))
1396+
else if(bytecode == patternt("ifnull"))
13981397
{
13991398
PRECONDITION(op.size() == 1 && results.empty());
14001399
const mp_integer number =
@@ -1405,80 +1404,80 @@ code_blockt java_bytecode_convert_methodt::convert_instructions(
14051404
{
14061405
c = convert_iinc(arg0, arg1, i_it->source_location, i_it->address);
14071406
}
1408-
else if(statement==patternt("?xor"))
1407+
else if(bytecode == patternt("?xor"))
14091408
{
14101409
PRECONDITION(op.size() == 2 && results.size() == 1);
14111410
results[0]=bitxor_exprt(op[0], op[1]);
14121411
}
1413-
else if(statement==patternt("?or"))
1412+
else if(bytecode == patternt("?or"))
14141413
{
14151414
PRECONDITION(op.size() == 2 && results.size() == 1);
14161415
results[0]=bitor_exprt(op[0], op[1]);
14171416
}
1418-
else if(statement==patternt("?and"))
1417+
else if(bytecode == patternt("?and"))
14191418
{
14201419
PRECONDITION(op.size() == 2 && results.size() == 1);
14211420
results[0]=bitand_exprt(op[0], op[1]);
14221421
}
1423-
else if(statement==patternt("?shl"))
1422+
else if(bytecode == patternt("?shl"))
14241423
{
14251424
PRECONDITION(op.size() == 2 && results.size() == 1);
14261425
results[0]=shl_exprt(op[0], op[1]);
14271426
}
1428-
else if(statement==patternt("?shr"))
1427+
else if(bytecode == patternt("?shr"))
14291428
{
14301429
PRECONDITION(op.size() == 2 && results.size() == 1);
14311430
results[0]=ashr_exprt(op[0], op[1]);
14321431
}
1433-
else if(statement==patternt("?ushr"))
1432+
else if(bytecode == patternt("?ushr"))
14341433
{
14351434
PRECONDITION(op.size() == 2 && results.size() == 1);
14361435
results = convert_ushr(statement, op, results);
14371436
}
1438-
else if(statement==patternt("?add"))
1437+
else if(bytecode == patternt("?add"))
14391438
{
14401439
PRECONDITION(op.size() == 2 && results.size() == 1);
14411440
results[0]=plus_exprt(op[0], op[1]);
14421441
}
1443-
else if(statement==patternt("?sub"))
1442+
else if(bytecode == patternt("?sub"))
14441443
{
14451444
PRECONDITION(op.size() == 2 && results.size() == 1);
14461445
results[0]=minus_exprt(op[0], op[1]);
14471446
}
1448-
else if(statement==patternt("?div"))
1447+
else if(bytecode == patternt("?div"))
14491448
{
14501449
PRECONDITION(op.size() == 2 && results.size() == 1);
14511450
results[0]=div_exprt(op[0], op[1]);
14521451
}
1453-
else if(statement==patternt("?mul"))
1452+
else if(bytecode == patternt("?mul"))
14541453
{
14551454
PRECONDITION(op.size() == 2 && results.size() == 1);
14561455
results[0]=mult_exprt(op[0], op[1]);
14571456
}
1458-
else if(statement==patternt("?neg"))
1457+
else if(bytecode == patternt("?neg"))
14591458
{
14601459
PRECONDITION(op.size() == 1 && results.size() == 1);
14611460
results[0]=unary_minus_exprt(op[0], op[0].type());
14621461
}
1463-
else if(statement==patternt("?rem"))
1462+
else if(bytecode == patternt("?rem"))
14641463
{
14651464
PRECONDITION(op.size() == 2 && results.size() == 1);
14661465
if(bytecode == BC_frem || bytecode == BC_drem)
14671466
results[0]=rem_exprt(op[0], op[1]);
14681467
else
14691468
results[0]=mod_exprt(op[0], op[1]);
14701469
}
1471-
else if(statement==patternt("?cmp"))
1470+
else if(bytecode == patternt("?cmp"))
14721471
{
14731472
PRECONDITION(op.size() == 2 && results.size() == 1);
14741473
results = convert_cmp(op, results);
14751474
}
1476-
else if(statement==patternt("?cmp?"))
1475+
else if(bytecode == patternt("?cmp?"))
14771476
{
14781477
PRECONDITION(op.size() == 2 && results.size() == 1);
14791478
results = convert_cmp2(statement, op, results);
14801479
}
1481-
else if(statement==patternt("?cmpl"))
1480+
else if(bytecode == patternt("?cmpl"))
14821481
{
14831482
PRECONDITION(op.size() == 2 && results.size() == 1);
14841483
results[0]=binary_relation_exprt(op[0], ID_lt, op[1]);
@@ -1562,7 +1561,7 @@ code_blockt java_bytecode_convert_methodt::convert_instructions(
15621561

15631562
c = convert_putstatic(i_it->source_location, arg0, op, symbol_expr);
15641563
}
1565-
else if(statement==patternt("?2?")) // i2c etc.
1564+
else if(bytecode == patternt("?2?")) // i2c etc.
15661565
{
15671566
PRECONDITION(op.size() == 1 && results.size() == 1);
15681567
typet type=java_type_from_char(statement[2]);
@@ -2747,12 +2746,12 @@ code_ifthenelset java_bytecode_convert_methodt::convert_if(
27472746

27482747
code_ifthenelset java_bytecode_convert_methodt::convert_if_cmp(
27492748
const java_bytecode_convert_methodt::address_mapt &address_map,
2750-
const irep_idt &statement,
2749+
const u1 bytecode,
27512750
const exprt::operandst &op,
27522751
const mp_integer &number,
27532752
const source_locationt &location) const
27542753
{
2755-
const irep_idt cmp_op = get_if_cmp_operator(statement);
2754+
const irep_idt cmp_op = get_if_cmp_operator(bytecode);
27562755
binary_relation_exprt condition(
27572756
op[0], cmp_op, typecast_exprt::conditional_cast(op[1], op[0].type()));
27582757
condition.add_source_location() = location;

jbmc/src/java_bytecode/java_bytecode_convert_method_class.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ class java_bytecode_convert_methodt:public messaget
373373

374374
code_ifthenelset convert_if_cmp(
375375
const java_bytecode_convert_methodt::address_mapt &address_map,
376-
const irep_idt &statement,
376+
const u1 bytecode,
377377
const exprt::operandst &op,
378378
const mp_integer &number,
379379
const source_locationt &location) const;

jbmc/src/java_bytecode/pattern.h

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Author: Daniel Kroening, [email protected]
1212
#ifndef CPROVER_JAVA_BYTECODE_PATTERN_H
1313
#define CPROVER_JAVA_BYTECODE_PATTERN_H
1414

15-
#include <util/irep.h>
15+
#include "bytecode_info.h"
1616

1717
/// Given a string of the format '?blah?', will return true when compared
1818
/// against a string that matches appart from any characters that are '?'
@@ -25,47 +25,30 @@ class patternt
2525
}
2626

2727
// match with '?'
28-
bool operator==(const std::string &what) const
28+
bool operator==(const u1 bytecode) const
2929
{
30-
for(std::size_t i = 0; i < what.size(); i++)
30+
const char *what = bytecode_info[bytecode].mnemonic;
31+
32+
std::size_t i;
33+
34+
for(i = 0; what[i] != 0; i++)
3135
if(p[i] == 0)
3236
return false;
3337
else if(p[i] != '?' && p[i] != what[i])
3438
return false;
3539

36-
return p[what.size()] == 0;
37-
}
38-
39-
#ifndef USE_STD_STRING
40-
bool operator==(const irep_idt &what) const
41-
{
42-
return (*this) == id2string(what);
43-
}
44-
#endif
45-
46-
friend bool operator==(const std::string &what, const patternt &p)
47-
{
48-
return p == what;
49-
}
50-
51-
#ifndef USE_STD_STRING
52-
friend bool operator==(const irep_idt &what, const patternt &p)
53-
{
54-
return p == what;
40+
return p[i] == 0;
5541
}
56-
#endif
5742

58-
friend bool operator!=(const std::string &what, const patternt &p)
43+
friend bool operator==(const u1 bytecode, const patternt &p)
5944
{
60-
return !(p == what);
45+
return p == bytecode;
6146
}
6247

63-
#ifndef USE_STD_STRING
64-
friend bool operator!=(const irep_idt &what, const patternt &p)
48+
friend bool operator!=(const u1 bytecode, const patternt &p)
6549
{
66-
return !(p == what);
50+
return !(p == bytecode);
6751
}
68-
#endif
6952

7053
protected:
7154
const char *p;

0 commit comments

Comments
 (0)