Skip to content

Commit e2ed0df

Browse files
committed
Avoid using integer_typet where unsuitable
The solver back end cannot necessarily handle unbounded integers. Do not use them unless explicitly requested/supported.
1 parent 8e70eaa commit e2ed0df

File tree

8 files changed

+131
-80
lines changed

8 files changed

+131
-80
lines changed

src/java_bytecode/java_bytecode_convert_method.cpp

+86-41
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Author: Daniel Kroening, [email protected]
2121
#include <util/arith_tools.h>
2222
#include <util/c_types.h>
2323
#include <util/ieee_float.h>
24+
#include <util/invariant.h>
2425
#include <util/namespace.h>
2526
#include <util/prefix.h>
2627
#include <util/simplify_expr.h>
@@ -163,9 +164,11 @@ const exprt java_bytecode_convert_methodt::variable(
163164
size_t address,
164165
java_bytecode_convert_methodt::variable_cast_argumentt do_cast)
165166
{
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);
167170

168-
std::size_t number_int=safe_string2size_t(id2string(number));
171+
std::size_t number_int=integer2size_t(number);
169172
typet t=java_type_from_char(type_char);
170173
variablest &var_list=variables[number_int];
171174

@@ -176,7 +179,7 @@ const exprt java_bytecode_convert_methodt::variable(
176179
if(var.symbol_expr.get_identifier().empty())
177180
{
178181
// 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;
180183
irep_idt identifier=id2string(current_method)+"::"+id2string(base_name);
181184

182185
symbol_exprt result(identifier, t);
@@ -848,8 +851,9 @@ codet java_bytecode_convert_methodt::convert_instructions(
848851
{
849852
assert(!i_it->args.empty());
850853

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");
853857
targets.insert(target);
854858

855859
a_entry.first->second.successors.push_back(target);
@@ -873,8 +877,9 @@ codet java_bytecode_convert_methodt::convert_instructions(
873877
{
874878
if(is_label)
875879
{
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");
878883
targets.insert(target);
879884
a_entry.first->second.successors.push_back(target);
880885
}
@@ -955,9 +960,11 @@ codet java_bytecode_convert_methodt::convert_instructions(
955960
statement[statement.size()-2]=='_' &&
956961
isdigit(statement[statement.size()-1]))
957962
{
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());
961968
statement=std::string(id2string(statement), 0, statement.size()-2);
962969
}
963970

@@ -1353,16 +1360,20 @@ codet java_bytecode_convert_methodt::convert_instructions(
13531360
else if(statement=="goto" || statement=="goto_w")
13541361
{
13551362
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)));
13581367
c=code_goto;
13591368
}
13601369
else if(statement=="jsr" || statement=="jsr_w")
13611370
{
13621371
// As 'goto', except we must also push the subroutine return address:
13631372
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)));
13661377
c=code_goto;
13671378
results[0]=
13681379
from_integer(
@@ -1422,33 +1433,43 @@ codet java_bytecode_convert_methodt::convert_instructions(
14221433
ieee_float_spect::double_precision());
14231434

14241435
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+
}
14281443
else
14291444
value.from_expr(to_constant_expr(arg0));
14301445

14311446
results[0]=value.to_expr();
14321447
}
14331448
else
14341449
{
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");
14361453
const typet type=java_type_from_char(statement[0]);
14371454
results[0]=from_integer(value, type);
14381455
}
14391456
}
14401457
else if(statement==patternt("?ipush"))
14411458
{
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());
14471466
}
14481467
else if(statement==patternt("if_?cmp??"))
14491468
{
1450-
irep_idt number=to_constant_expr(arg0).get_value();
14511469
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");
14521473

14531474
code_ifthenelset code_branch;
14541475
const irep_idt cmp_op=get_if_cmp_operator(statement);
@@ -1463,7 +1484,7 @@ codet java_bytecode_convert_methodt::convert_instructions(
14631484

14641485
code_branch.cond()=condition;
14651486
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)));
14671488
code_branch.then_case().add_source_location()=i_it->source_location;
14681489
code_branch.add_source_location()=i_it->source_location;
14691490

@@ -1480,15 +1501,17 @@ codet java_bytecode_convert_methodt::convert_instructions(
14801501
statement=="ifle"?ID_le:
14811502
(assert(false), "");
14821503

1483-
irep_idt number=to_constant_expr(arg0).get_value();
14841504
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");
14851508

14861509
code_ifthenelset code_branch;
14871510
code_branch.cond()=
14881511
binary_relation_exprt(op[0], id, from_integer(0, op[0].type()));
14891512
code_branch.cond().add_source_location()=i_it->source_location;
14901513
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)));
14921515
code_branch.then_case().add_source_location()=i_it->source_location;
14931516
code_branch.then_case().add_source_location().set_function(method_id);
14941517
code_branch.add_source_location()=i_it->source_location;
@@ -1498,13 +1521,15 @@ codet java_bytecode_convert_methodt::convert_instructions(
14981521
}
14991522
else if(statement==patternt("ifnonnull"))
15001523
{
1501-
irep_idt number=to_constant_expr(arg0).get_value();
15021524
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");
15031528
code_ifthenelset code_branch;
15041529
const typecast_exprt lhs(op[0], java_reference_type(empty_typet()));
15051530
const exprt rhs(null_pointer_exprt(to_pointer_type(lhs.type())));
15061531
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)));
15081533
code_branch.then_case().add_source_location()=i_it->source_location;
15091534
code_branch.add_source_location()=i_it->source_location;
15101535

@@ -1513,12 +1538,14 @@ codet java_bytecode_convert_methodt::convert_instructions(
15131538
else if(statement==patternt("ifnull"))
15141539
{
15151540
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");
15171544
code_ifthenelset code_branch;
15181545
const typecast_exprt lhs(op[0], java_reference_type(empty_typet()));
15191546
const exprt rhs(null_pointer_exprt(to_pointer_type(lhs.type())));
15201547
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)));
15221549
code_branch.then_case().add_source_location()=i_it->source_location;
15231550
code_branch.add_source_location()=i_it->source_location;
15241551

@@ -1540,9 +1567,12 @@ codet java_bytecode_convert_methodt::convert_instructions(
15401567
code_assignt code_assign;
15411568
code_assign.lhs()=
15421569
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());
15431573
code_assign.rhs()=plus_exprt(
15441574
variable(arg0, 'i', i_it->address, CAST_AS_NEEDED),
1545-
typecast_exprt(arg1, java_int_type()));
1575+
arg1_int_type);
15461576
block.copy_to_operands(code_assign);
15471577
c=block;
15481578
}
@@ -1579,10 +1609,16 @@ codet java_bytecode_convert_methodt::convert_instructions(
15791609
const std::size_t width=type.get_size_t(ID_width);
15801610
typet target=unsignedbv_typet(width);
15811611

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);
15841618

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());
15861622
}
15871623
else if(statement==patternt("?add"))
15881624
{
@@ -1815,7 +1851,10 @@ codet java_bytecode_convert_methodt::convert_instructions(
18151851
else if(statement==patternt("?2?")) // i2c etc.
18161852
{
18171853
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);
18191858
}
18201859
else if(statement=="new")
18211860
{
@@ -1901,8 +1940,10 @@ codet java_bytecode_convert_methodt::convert_instructions(
19011940
{
19021941
// The first argument is the type, the second argument is the number of
19031942
// 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);
19061947

19071948
op=pop(dimension);
19081949
assert(results.size()==1);
@@ -1976,8 +2017,10 @@ codet java_bytecode_convert_methodt::convert_instructions(
19762017
code_switch_caset code_case;
19772018
code_case.add_source_location()=i_it->source_location;
19782019

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)));
19812024
code_case.code().add_source_location()=i_it->source_location;
19822025

19832026
if(a_it==i_it->args.begin())
@@ -1986,7 +2029,9 @@ codet java_bytecode_convert_methodt::convert_instructions(
19862029
{
19872030
instructiont::argst::const_iterator prev=a_it;
19882031
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());
19902035
code_case.case_op().add_source_location()=i_it->source_location;
19912036
}
19922037

0 commit comments

Comments
 (0)