21
21
#include < util/arith_tools.h>
22
22
#include < util/c_types.h>
23
23
#include < util/ieee_float.h>
24
+ #include < util/invariant.h>
24
25
#include < util/namespace.h>
25
26
#include < util/prefix.h>
26
27
#include < util/simplify_expr.h>
@@ -163,9 +164,11 @@ const exprt java_bytecode_convert_methodt::variable(
163
164
size_t address,
164
165
java_bytecode_convert_methodt::variable_cast_argumentt do_cast)
165
166
{
166
- irep_idt number=to_constant_expr (arg).get_value ();
167
+ mp_integer number;
168
+ bool ret=to_integer (to_constant_expr (arg), number);
169
+ CHECK_RETURN (!ret);
167
170
168
- std::size_t number_int=safe_string2size_t ( id2string ( number) );
171
+ std::size_t number_int=integer2size_t ( number);
169
172
typet t=java_type_from_char (type_char);
170
173
variablest &var_list=variables[number_int];
171
174
@@ -176,7 +179,7 @@ const exprt java_bytecode_convert_methodt::variable(
176
179
if (var.symbol_expr .get_identifier ().empty ())
177
180
{
178
181
// an unnamed local variable
179
- irep_idt base_name=" anonlocal::" +id2string (number )+type_char;
182
+ irep_idt base_name=" anonlocal::" +std::to_string (number_int )+type_char;
180
183
irep_idt identifier=id2string (current_method)+" ::" +id2string (base_name);
181
184
182
185
symbol_exprt result (identifier, t);
@@ -848,8 +851,9 @@ codet java_bytecode_convert_methodt::convert_instructions(
848
851
{
849
852
assert (!i_it->args .empty ());
850
853
851
- const unsigned target=safe_string2unsigned (
852
- id2string (to_constant_expr (i_it->args [0 ]).get_value ()));
854
+ unsigned target;
855
+ bool ret=to_unsigned_integer (to_constant_expr (i_it->args [0 ]), target);
856
+ DATA_INVARIANT (!ret, " target expected to be unsigned integer" );
853
857
targets.insert (target);
854
858
855
859
a_entry.first ->second .successors .push_back (target);
@@ -873,8 +877,9 @@ codet java_bytecode_convert_methodt::convert_instructions(
873
877
{
874
878
if (is_label)
875
879
{
876
- const unsigned target=safe_string2unsigned (
877
- id2string (to_constant_expr (arg).get_value ()));
880
+ unsigned target;
881
+ bool ret=to_unsigned_integer (to_constant_expr (arg), target);
882
+ DATA_INVARIANT (!ret, " target expected to be unsigned integer" );
878
883
targets.insert (target);
879
884
a_entry.first ->second .successors .push_back (target);
880
885
}
@@ -955,9 +960,11 @@ codet java_bytecode_convert_methodt::convert_instructions(
955
960
statement[statement.size ()-2 ]==' _' &&
956
961
isdigit (statement[statement.size ()-1 ]))
957
962
{
958
- arg0=constant_exprt (
959
- std::string (id2string (statement), statement.size ()-1 , 1 ),
960
- integer_typet ());
963
+ arg0=
964
+ from_integer (
965
+ string2integer (
966
+ std::string (id2string (statement), statement.size ()-1 , 1 )),
967
+ java_int_type ());
961
968
statement=std::string (id2string (statement), 0 , statement.size ()-2 );
962
969
}
963
970
@@ -1353,16 +1360,20 @@ codet java_bytecode_convert_methodt::convert_instructions(
1353
1360
else if (statement==" goto" || statement==" goto_w" )
1354
1361
{
1355
1362
assert (op.empty () && results.empty ());
1356
- irep_idt number=to_constant_expr (arg0).get_value ();
1357
- code_gotot code_goto (label (number));
1363
+ mp_integer number;
1364
+ bool ret=to_integer (to_constant_expr (arg0), number);
1365
+ INVARIANT (!ret, " goto argument should be an integer" );
1366
+ code_gotot code_goto (label (integer2string (number)));
1358
1367
c=code_goto;
1359
1368
}
1360
1369
else if (statement==" jsr" || statement==" jsr_w" )
1361
1370
{
1362
1371
// As 'goto', except we must also push the subroutine return address:
1363
1372
assert (op.empty () && results.size ()==1 );
1364
- irep_idt number=to_constant_expr (arg0).get_value ();
1365
- code_gotot code_goto (label (number));
1373
+ mp_integer number;
1374
+ bool ret=to_integer (to_constant_expr (arg0), number);
1375
+ INVARIANT (!ret, " jsr argument should be an integer" );
1376
+ code_gotot code_goto (label (integer2string (number)));
1366
1377
c=code_goto;
1367
1378
results[0 ]=
1368
1379
from_integer (
@@ -1422,33 +1433,43 @@ codet java_bytecode_convert_methodt::convert_instructions(
1422
1433
ieee_float_spect::double_precision ());
1423
1434
1424
1435
ieee_floatt value (spec);
1425
- const typet &arg_type (arg0.type ());
1426
- if (ID_integer==arg_type.id ())
1427
- value.from_integer (arg0.get_int (ID_value));
1436
+ if (arg0.type ().id ()!=ID_floatbv)
1437
+ {
1438
+ mp_integer number;
1439
+ bool ret=to_integer (to_constant_expr (arg0), number);
1440
+ DATA_INVARIANT (!ret, " failed to convert constant" );
1441
+ value.from_integer (number);
1442
+ }
1428
1443
else
1429
1444
value.from_expr (to_constant_expr (arg0));
1430
1445
1431
1446
results[0 ]=value.to_expr ();
1432
1447
}
1433
1448
else
1434
1449
{
1435
- const unsigned value (arg0.get_unsigned_int (ID_value));
1450
+ mp_integer value;
1451
+ bool ret=to_integer (to_constant_expr (arg0), value);
1452
+ DATA_INVARIANT (!ret, " failed to convert constant" );
1436
1453
const typet type=java_type_from_char (statement[0 ]);
1437
1454
results[0 ]=from_integer (value, type);
1438
1455
}
1439
1456
}
1440
1457
else if (statement==patternt (" ?ipush" ))
1441
1458
{
1442
- assert (results.size ()==1 );
1443
- mp_integer int_value;
1444
- bool ret=to_integer (to_constant_expr (arg0), int_value);
1445
- INVARIANT (!ret, " ?ipush argument should be an integer" );
1446
- results[0 ]=from_integer (int_value, java_int_type ());
1459
+ PRECONDITION (results.size ()==1 );
1460
+ DATA_INVARIANT (
1461
+ arg0.id ()==ID_constant,
1462
+ " ipush argument expected to be constant" );
1463
+ results[0 ]=arg0;
1464
+ if (arg0.type ()!=java_int_type ())
1465
+ results[0 ].make_typecast (java_int_type ());
1447
1466
}
1448
1467
else if (statement==patternt (" if_?cmp??" ))
1449
1468
{
1450
- irep_idt number=to_constant_expr (arg0).get_value ();
1451
1469
assert (op.size ()==2 && results.empty ());
1470
+ mp_integer number;
1471
+ bool ret=to_integer (to_constant_expr (arg0), number);
1472
+ INVARIANT (!ret, " if_?cmp?? argument should be an integer" );
1452
1473
1453
1474
code_ifthenelset code_branch;
1454
1475
const irep_idt cmp_op=get_if_cmp_operator (statement);
@@ -1463,7 +1484,7 @@ codet java_bytecode_convert_methodt::convert_instructions(
1463
1484
1464
1485
code_branch.cond ()=condition;
1465
1486
code_branch.cond ().add_source_location ()=i_it->source_location ;
1466
- code_branch.then_case ()=code_gotot (label (number));
1487
+ code_branch.then_case ()=code_gotot (label (integer2string ( number) ));
1467
1488
code_branch.then_case ().add_source_location ()=i_it->source_location ;
1468
1489
code_branch.add_source_location ()=i_it->source_location ;
1469
1490
@@ -1480,15 +1501,17 @@ codet java_bytecode_convert_methodt::convert_instructions(
1480
1501
statement==" ifle" ?ID_le:
1481
1502
(assert (false ), " " );
1482
1503
1483
- irep_idt number=to_constant_expr (arg0).get_value ();
1484
1504
assert (op.size ()==1 && results.empty ());
1505
+ mp_integer number;
1506
+ bool ret=to_integer (to_constant_expr (arg0), number);
1507
+ INVARIANT (!ret, " if?? argument should be an integer" );
1485
1508
1486
1509
code_ifthenelset code_branch;
1487
1510
code_branch.cond ()=
1488
1511
binary_relation_exprt (op[0 ], id, from_integer (0 , op[0 ].type ()));
1489
1512
code_branch.cond ().add_source_location ()=i_it->source_location ;
1490
1513
code_branch.cond ().add_source_location ().set_function (method_id);
1491
- code_branch.then_case ()=code_gotot (label (number));
1514
+ code_branch.then_case ()=code_gotot (label (integer2string ( number) ));
1492
1515
code_branch.then_case ().add_source_location ()=i_it->source_location ;
1493
1516
code_branch.then_case ().add_source_location ().set_function (method_id);
1494
1517
code_branch.add_source_location ()=i_it->source_location ;
@@ -1498,13 +1521,15 @@ codet java_bytecode_convert_methodt::convert_instructions(
1498
1521
}
1499
1522
else if (statement==patternt (" ifnonnull" ))
1500
1523
{
1501
- irep_idt number=to_constant_expr (arg0).get_value ();
1502
1524
assert (op.size ()==1 && results.empty ());
1525
+ mp_integer number;
1526
+ bool ret=to_integer (to_constant_expr (arg0), number);
1527
+ INVARIANT (!ret, " ifnonnull argument should be an integer" );
1503
1528
code_ifthenelset code_branch;
1504
1529
const typecast_exprt lhs (op[0 ], java_reference_type (empty_typet ()));
1505
1530
const exprt rhs (null_pointer_exprt (to_pointer_type (lhs.type ())));
1506
1531
code_branch.cond ()=binary_relation_exprt (lhs, ID_notequal, rhs);
1507
- code_branch.then_case ()=code_gotot (label (number));
1532
+ code_branch.then_case ()=code_gotot (label (integer2string ( number) ));
1508
1533
code_branch.then_case ().add_source_location ()=i_it->source_location ;
1509
1534
code_branch.add_source_location ()=i_it->source_location ;
1510
1535
@@ -1513,12 +1538,14 @@ codet java_bytecode_convert_methodt::convert_instructions(
1513
1538
else if (statement==patternt (" ifnull" ))
1514
1539
{
1515
1540
assert (op.size ()==1 && results.empty ());
1516
- irep_idt number=to_constant_expr (arg0).get_value ();
1541
+ mp_integer number;
1542
+ bool ret=to_integer (to_constant_expr (arg0), number);
1543
+ INVARIANT (!ret, " ifnull argument should be an integer" );
1517
1544
code_ifthenelset code_branch;
1518
1545
const typecast_exprt lhs (op[0 ], java_reference_type (empty_typet ()));
1519
1546
const exprt rhs (null_pointer_exprt (to_pointer_type (lhs.type ())));
1520
1547
code_branch.cond ()=binary_relation_exprt (lhs, ID_equal, rhs);
1521
- code_branch.then_case ()=code_gotot (label (number));
1548
+ code_branch.then_case ()=code_gotot (label (integer2string ( number) ));
1522
1549
code_branch.then_case ().add_source_location ()=i_it->source_location ;
1523
1550
code_branch.add_source_location ()=i_it->source_location ;
1524
1551
@@ -1540,9 +1567,12 @@ codet java_bytecode_convert_methodt::convert_instructions(
1540
1567
code_assignt code_assign;
1541
1568
code_assign.lhs ()=
1542
1569
variable (arg0, ' i' , i_it->address , NO_CAST);
1570
+ exprt arg1_int_type=arg1;
1571
+ if (arg1.type ()!=java_int_type ())
1572
+ arg1_int_type.make_typecast (java_int_type ());
1543
1573
code_assign.rhs ()=plus_exprt (
1544
1574
variable (arg0, ' i' , i_it->address , CAST_AS_NEEDED),
1545
- typecast_exprt (arg1, java_int_type ()) );
1575
+ arg1_int_type );
1546
1576
block.copy_to_operands (code_assign);
1547
1577
c=block;
1548
1578
}
@@ -1579,10 +1609,16 @@ codet java_bytecode_convert_methodt::convert_instructions(
1579
1609
const std::size_t width=type.get_size_t (ID_width);
1580
1610
typet target=unsignedbv_typet (width);
1581
1611
1582
- const typecast_exprt lhs (op[0 ], target);
1583
- const typecast_exprt rhs (op[1 ], target);
1612
+ exprt lhs=op[0 ];
1613
+ if (lhs.type ()!=target)
1614
+ lhs.make_typecast (target);
1615
+ exprt rhs=op[1 ];
1616
+ if (rhs.type ()!=target)
1617
+ rhs.make_typecast (target);
1584
1618
1585
- results[0 ]=typecast_exprt (lshr_exprt (lhs, rhs), op[0 ].type ());
1619
+ results[0 ]=lshr_exprt (lhs, rhs);
1620
+ if (results[0 ].type ()!=op[0 ].type ())
1621
+ results[0 ].make_typecast (op[0 ].type ());
1586
1622
}
1587
1623
else if (statement==patternt (" ?add" ))
1588
1624
{
@@ -1815,7 +1851,10 @@ codet java_bytecode_convert_methodt::convert_instructions(
1815
1851
else if (statement==patternt (" ?2?" )) // i2c etc.
1816
1852
{
1817
1853
assert (op.size ()==1 && results.size ()==1 );
1818
- results[0 ]=typecast_exprt (op[0 ], java_type_from_char (statement[2 ]));
1854
+ typet type=java_type_from_char (statement[2 ]);
1855
+ results[0 ]=op[0 ];
1856
+ if (results[0 ].type ()!=type)
1857
+ results[0 ].make_typecast (type);
1819
1858
}
1820
1859
else if (statement==" new" )
1821
1860
{
@@ -1901,8 +1940,10 @@ codet java_bytecode_convert_methodt::convert_instructions(
1901
1940
{
1902
1941
// The first argument is the type, the second argument is the number of
1903
1942
// dimensions. The size of each dimension is on the stack.
1904
- irep_idt number=to_constant_expr (arg1).get_value ();
1905
- std::size_t dimension=safe_string2size_t (id2string (number));
1943
+ mp_integer number;
1944
+ bool ret=to_integer (to_constant_expr (arg1), number);
1945
+ INVARIANT (!ret, " multianewarray argument should be an integer" );
1946
+ std::size_t dimension=integer2size_t (number);
1906
1947
1907
1948
op=pop (dimension);
1908
1949
assert (results.size ()==1 );
@@ -1976,8 +2017,10 @@ codet java_bytecode_convert_methodt::convert_instructions(
1976
2017
code_switch_caset code_case;
1977
2018
code_case.add_source_location ()=i_it->source_location ;
1978
2019
1979
- irep_idt number=to_constant_expr (*a_it).get_value ();
1980
- code_case.code ()=code_gotot (label (number));
2020
+ mp_integer number;
2021
+ bool ret=to_integer (to_constant_expr (*a_it), number);
2022
+ DATA_INVARIANT (!ret, " case label expected to be integer" );
2023
+ code_case.code ()=code_gotot (label (integer2string (number)));
1981
2024
code_case.code ().add_source_location ()=i_it->source_location ;
1982
2025
1983
2026
if (a_it==i_it->args .begin ())
@@ -1986,7 +2029,9 @@ codet java_bytecode_convert_methodt::convert_instructions(
1986
2029
{
1987
2030
instructiont::argst::const_iterator prev=a_it;
1988
2031
prev--;
1989
- code_case.case_op ()=typecast_exprt (*prev, op[0 ].type ());
2032
+ code_case.case_op ()=*prev;
2033
+ if (code_case.case_op ().type ()!=op[0 ].type ())
2034
+ code_case.case_op ().make_typecast (op[0 ].type ());
1990
2035
code_case.case_op ().add_source_location ()=i_it->source_location ;
1991
2036
}
1992
2037
0 commit comments