Skip to content

Commit 5f55c33

Browse files
authored
Merge pull request #3504 from tautschnig/constructors-ifthenelse
Multi-operand constructor for code_ifthenelset
2 parents b280689 + 8427934 commit 5f55c33

13 files changed

+138
-167
lines changed

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2705,15 +2705,19 @@ code_ifthenelset java_bytecode_convert_methodt::convert_ifnull(
27052705
const mp_integer &number,
27062706
const source_locationt &location) const
27072707
{
2708-
code_ifthenelset code_branch;
27092708
const typecast_exprt lhs(op[0], java_reference_type(empty_typet()));
27102709
const exprt rhs(null_pointer_exprt(to_pointer_type(lhs.type())));
2711-
code_branch.cond() = binary_relation_exprt(lhs, ID_equal, rhs);
2710+
27122711
const method_offsett label_number = numeric_cast_v<method_offsett>(number);
2713-
code_branch.then_case() = code_gotot(label(std::to_string(label_number)));
2712+
2713+
code_ifthenelset code_branch(
2714+
binary_relation_exprt(lhs, ID_equal, rhs),
2715+
code_gotot(label(std::to_string(label_number))));
2716+
27142717
code_branch.then_case().add_source_location() =
27152718
address_map.at(label_number).source->source_location;
27162719
code_branch.add_source_location() = location;
2720+
27172721
return code_branch;
27182722
}
27192723

@@ -2723,15 +2727,19 @@ code_ifthenelset java_bytecode_convert_methodt::convert_ifnonull(
27232727
const mp_integer &number,
27242728
const source_locationt &location) const
27252729
{
2726-
code_ifthenelset code_branch;
27272730
const typecast_exprt lhs(op[0], java_reference_type(empty_typet()));
27282731
const exprt rhs(null_pointer_exprt(to_pointer_type(lhs.type())));
2729-
code_branch.cond() = binary_relation_exprt(lhs, ID_notequal, rhs);
2732+
27302733
const method_offsett label_number = numeric_cast_v<method_offsett>(number);
2731-
code_branch.then_case() = code_gotot(label(std::to_string(label_number)));
2734+
2735+
code_ifthenelset code_branch(
2736+
binary_relation_exprt(lhs, ID_notequal, rhs),
2737+
code_gotot(label(std::to_string(label_number))));
2738+
27322739
code_branch.then_case().add_source_location() =
27332740
address_map.at(label_number).source->source_location;
27342741
code_branch.add_source_location() = location;
2742+
27352743
return code_branch;
27362744
}
27372745

@@ -2742,18 +2750,20 @@ code_ifthenelset java_bytecode_convert_methodt::convert_if(
27422750
const mp_integer &number,
27432751
const source_locationt &location) const
27442752
{
2745-
code_ifthenelset code_branch;
2746-
code_branch.cond() =
2747-
binary_relation_exprt(op[0], id, from_integer(0, op[0].type()));
2753+
const method_offsett label_number = numeric_cast_v<method_offsett>(number);
2754+
2755+
code_ifthenelset code_branch(
2756+
binary_relation_exprt(op[0], id, from_integer(0, op[0].type())),
2757+
code_gotot(label(std::to_string(label_number))));
2758+
27482759
code_branch.cond().add_source_location() = location;
27492760
code_branch.cond().add_source_location().set_function(method_id);
2750-
const method_offsett label_number = numeric_cast_v<method_offsett>(number);
2751-
code_branch.then_case() = code_gotot(label(std::to_string(label_number)));
27522761
code_branch.then_case().add_source_location() =
27532762
address_map.at(label_number).source->source_location;
27542763
code_branch.then_case().add_source_location().set_function(method_id);
27552764
code_branch.add_source_location() = location;
27562765
code_branch.add_source_location().set_function(method_id);
2766+
27572767
return code_branch;
27582768
}
27592769

@@ -2764,15 +2774,16 @@ code_ifthenelset java_bytecode_convert_methodt::convert_if_cmp(
27642774
const mp_integer &number,
27652775
const source_locationt &location) const
27662776
{
2767-
code_ifthenelset code_branch;
27682777
const irep_idt cmp_op = get_if_cmp_operator(statement);
27692778
binary_relation_exprt condition(
27702779
op[0], cmp_op, typecast_exprt::conditional_cast(op[1], op[0].type()));
27712780
condition.add_source_location() = location;
27722781

2773-
code_branch.cond() = condition;
27742782
const method_offsett label_number = numeric_cast_v<method_offsett>(number);
2775-
code_branch.then_case() = code_gotot(label(std::to_string(label_number)));
2783+
2784+
code_ifthenelset code_branch(
2785+
std::move(condition), code_gotot(label(std::to_string(label_number))));
2786+
27762787
code_branch.then_case().add_source_location() =
27772788
address_map.at(label_number).source->source_location;
27782789
code_branch.add_source_location() = location;
@@ -2797,15 +2808,16 @@ code_blockt java_bytecode_convert_methodt::convert_ret(
27972808
c.add(g);
27982809
else
27992810
{
2800-
code_ifthenelset branch;
28012811
auto address_ptr =
28022812
from_integer(jsr_ret_targets[idx], unsignedbv_typet(64));
28032813
address_ptr.type() = pointer_type(void_typet());
2804-
branch.cond() = equal_exprt(retvar, address_ptr);
2814+
2815+
code_ifthenelset branch(equal_exprt(retvar, address_ptr), std::move(g));
2816+
28052817
branch.cond().add_source_location() = location;
2806-
branch.then_case() = g;
28072818
branch.add_source_location() = location;
2808-
c.add(branch);
2819+
2820+
c.add(std::move(branch));
28092821
}
28102822
}
28112823
return c;

jbmc/src/java_bytecode/java_bytecode_instrument.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ code_ifthenelset java_bytecode_instrumentt::throw_exception(
127127
side_effect_expr_throwt throw_expr(irept(), typet(), original_loc);
128128
throw_expr.copy_to_operands(new_symbol.symbol_expr());
129129

130-
code_ifthenelset if_code;
130+
code_ifthenelset if_code(
131+
cond, code_blockt({assign_new, code_expressiont(throw_expr)}));
132+
131133
if_code.add_source_location()=original_loc;
132-
if_code.cond()=cond;
133-
if_code.then_case() = code_blockt({assign_new, code_expressiont(throw_expr)});
134134

135135
return if_code;
136136
}
@@ -250,11 +250,9 @@ code_ifthenelset java_bytecode_instrumentt::check_class_cast(
250250
check_code=std::move(assert_class);
251251
}
252252

253-
code_ifthenelset conditional_check;
254-
notequal_exprt op_not_null(null_check_op, null_pointer_exprt(voidptr));
255-
conditional_check.cond()=std::move(op_not_null);
256-
conditional_check.then_case()=std::move(check_code);
257-
return conditional_check;
253+
return code_ifthenelset(
254+
notequal_exprt(std::move(null_check_op), null_pointer_exprt(voidptr)),
255+
std::move(check_code));
258256
}
259257

260258
/// Checks whether \p expr is null and throws NullPointerException/

jbmc/src/java_bytecode/java_object_factory.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -853,12 +853,12 @@ void java_object_factoryt::gen_nondet_pointer_init(
853853
// <code from recursive call to gen_nondet_init() with
854854
// tmp$<temporary_counter>>
855855
// }
856-
code_ifthenelset null_check;
857-
null_check.cond() = side_effect_expr_nondett(bool_typet(), location);
858-
null_check.then_case()=set_null_inst;
859-
null_check.else_case()=non_null_inst;
856+
code_ifthenelset null_check(
857+
side_effect_expr_nondett(bool_typet(), location),
858+
std::move(set_null_inst),
859+
std::move(non_null_inst));
860860

861-
new_object_assignments.add(null_check);
861+
new_object_assignments.add(std::move(null_check));
862862
}
863863

864864
// Similarly to above, maybe use a conditional if both the
@@ -872,13 +872,12 @@ void java_object_factoryt::gen_nondet_pointer_init(
872872
INVARIANT(update_in_place==update_in_placet::MAY_UPDATE_IN_PLACE,
873873
"No-update and must-update should have already been resolved");
874874

875-
code_ifthenelset update_check;
876-
update_check.cond() =
877-
side_effect_expr_nondett(bool_typet(), expr.source_location());
878-
update_check.then_case()=update_in_place_assignments;
879-
update_check.else_case()=new_object_assignments;
875+
code_ifthenelset update_check(
876+
side_effect_expr_nondett(bool_typet(), expr.source_location()),
877+
std::move(update_in_place_assignments),
878+
std::move(new_object_assignments));
880879

881-
assignments.add(update_check);
880+
assignments.add(std::move(update_check));
882881
}
883882
}
884883

@@ -1461,19 +1460,17 @@ void java_object_factoryt::gen_nondet_array_init(
14611460
code_labelt init_done_label(done_name, code_skipt());
14621461
code_gotot goto_done(done_name);
14631462

1464-
code_ifthenelset done_test;
1465-
done_test.cond()=equal_exprt(counter_expr, length_expr);
1466-
done_test.then_case()=goto_done;
1463+
const code_ifthenelset done_test(
1464+
equal_exprt(counter_expr, length_expr), goto_done);
14671465

14681466
assignments.add(std::move(done_test));
14691467

14701468
if(update_in_place!=update_in_placet::MUST_UPDATE_IN_PLACE)
14711469
{
14721470
// Add a redundant if(counter == max_length) break
14731471
// that is easier for the unwinder to understand.
1474-
code_ifthenelset max_test;
1475-
max_test.cond()=equal_exprt(counter_expr, max_length_expr);
1476-
max_test.then_case()=goto_done;
1472+
code_ifthenelset max_test(
1473+
equal_exprt(counter_expr, max_length_expr), std::move(goto_done));
14771474

14781475
assignments.add(std::move(max_test));
14791476
}

jbmc/src/java_bytecode/java_static_initializers.cpp

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,12 @@ code_blockt get_thread_safe_clinit_wrapper_body(
510510

511511
// if(C::__CPROVER_PREFIX_clinit_thread_local_state == INIT_COMPLETE) return;
512512
{
513-
code_ifthenelset conditional;
514-
conditional.cond() = gen_clinit_eqexpr(
515-
clinit_thread_local_state_sym.symbol_expr(),
516-
clinit_statest::INIT_COMPLETE);
517-
conditional.then_case() = code_returnt();
518-
function_body.add(conditional);
513+
code_ifthenelset conditional(
514+
gen_clinit_eqexpr(
515+
clinit_thread_local_state_sym.symbol_expr(),
516+
clinit_statest::INIT_COMPLETE),
517+
code_returnt());
518+
function_body.add(std::move(conditional));
519519
}
520520

521521
// C::__CPROVER_PREFIX_clinit_thread_local_state = INIT_COMPLETE;
@@ -550,25 +550,21 @@ code_blockt get_thread_safe_clinit_wrapper_body(
550550
// init_complete = true;
551551
// }
552552
{
553-
code_ifthenelset not_init_conditional;
554-
code_blockt then_block;
555-
not_init_conditional.cond() = gen_clinit_eqexpr(
556-
clinit_state_sym.symbol_expr(), clinit_statest::NOT_INIT);
557-
then_block.add(
558-
gen_clinit_assign(
559-
clinit_state_sym.symbol_expr(), clinit_statest::IN_PROGRESS));
560-
then_block.add(code_assignt(init_complete.symbol_expr(), false_exprt()));
561-
not_init_conditional.then_case() = then_block;
562-
563-
code_ifthenelset init_conditional;
564-
code_blockt init_conditional_body;
565-
init_conditional.cond() = gen_clinit_eqexpr(
566-
clinit_state_sym.symbol_expr(), clinit_statest::INIT_COMPLETE);
567-
init_conditional_body.add(
568-
code_assignt(init_complete.symbol_expr(), true_exprt()));
569-
init_conditional.then_case() = init_conditional_body;
570-
not_init_conditional.else_case() = init_conditional;
571-
function_body.add(not_init_conditional);
553+
code_ifthenelset init_conditional(
554+
gen_clinit_eqexpr(
555+
clinit_state_sym.symbol_expr(), clinit_statest::INIT_COMPLETE),
556+
code_blockt({code_assignt(init_complete.symbol_expr(), true_exprt())}));
557+
558+
code_ifthenelset not_init_conditional(
559+
gen_clinit_eqexpr(
560+
clinit_state_sym.symbol_expr(), clinit_statest::NOT_INIT),
561+
code_blockt(
562+
{gen_clinit_assign(
563+
clinit_state_sym.symbol_expr(), clinit_statest::IN_PROGRESS),
564+
code_assignt(init_complete.symbol_expr(), false_exprt())}),
565+
std::move(init_conditional));
566+
567+
function_body.add(std::move(not_init_conditional));
572568
}
573569

574570
// ATOMIC_END
@@ -578,10 +574,8 @@ code_blockt get_thread_safe_clinit_wrapper_body(
578574

579575
// if(init_complete) return;
580576
{
581-
code_ifthenelset conditional;
582-
conditional.cond() = init_complete.symbol_expr();
583-
conditional.then_case() = code_returnt();
584-
function_body.add(conditional);
577+
code_ifthenelset conditional(init_complete.symbol_expr(), code_returnt());
578+
function_body.add(std::move(conditional));
585579
}
586580

587581
// Initialize the super-class C' and
@@ -680,12 +674,6 @@ code_ifthenelset get_clinit_wrapper_body(
680674
already_run_symbol.symbol_expr(),
681675
false_exprt());
682676

683-
// the entire body of the function is an if-then-else
684-
code_ifthenelset wrapper_body;
685-
686-
// add the condition to the if
687-
wrapper_body.cond() = check_already_run;
688-
689677
// add the "already-run = false" statement
690678
code_assignt set_already_run(already_run_symbol.symbol_expr(), true_exprt());
691679
code_blockt init_body({set_already_run});
@@ -698,9 +686,8 @@ code_ifthenelset get_clinit_wrapper_body(
698686
object_factory_parameters,
699687
pointer_type_selector);
700688

701-
wrapper_body.then_case() = init_body;
702-
703-
return wrapper_body;
689+
// the entire body of the function is an if-then-else
690+
return code_ifthenelset(std::move(check_already_run), std::move(init_body));
704691
}
705692

706693

jbmc/src/java_bytecode/java_string_library_preprocess.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,12 +1065,11 @@ code_blockt java_string_library_preprocesst::make_float_to_string_code(
10651065
str, string_expr_list[0], symbol_table, true);
10661066
for(std::size_t i=1; i<condition_list.size(); i++)
10671067
{
1068-
code_ifthenelset ife;
1069-
ife.cond()=condition_list[i];
1070-
ife.then_case() = code_assign_string_expr_to_java_string(
1071-
str, string_expr_list[i], symbol_table, true);
1072-
ife.else_case()=tmp_code;
1073-
tmp_code=ife;
1068+
tmp_code = code_ifthenelset(
1069+
condition_list[i],
1070+
code_assign_string_expr_to_java_string(
1071+
str, string_expr_list[i], symbol_table, true),
1072+
tmp_code);
10741073
}
10751074
code.add(tmp_code, loc);
10761075

jbmc/src/java_bytecode/remove_instanceof.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,6 @@ bool remove_instanceoft::lower_instanceof(
134134
// possible values of T.
135135
// (http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.20.2)
136136

137-
code_ifthenelset is_null_branch;
138-
is_null_branch.cond() =
139-
equal_exprt(
140-
check_ptr, null_pointer_exprt(to_pointer_type(check_ptr.type())));
141-
is_null_branch.then_case() =
142-
code_assignt(instanceof_result_sym.symbol_expr(), false_exprt());
143-
144137
code_blockt else_block;
145138
else_block.add(code_declt(clsid_tmp_sym.symbol_expr()));
146139
else_block.add(code_assignt(clsid_tmp_sym.symbol_expr(), object_clsid));
@@ -155,7 +148,11 @@ bool remove_instanceoft::lower_instanceof(
155148
else_block.add(
156149
code_assignt(instanceof_result_sym.symbol_expr(), disjunction(or_ops)));
157150

158-
is_null_branch.else_case() = std::move(else_block);
151+
const code_ifthenelset is_null_branch(
152+
equal_exprt(
153+
check_ptr, null_pointer_exprt(to_pointer_type(check_ptr.type()))),
154+
code_assignt(instanceof_result_sym.symbol_expr(), false_exprt()),
155+
std::move(else_block));
159156

160157
// Replace the instanceof construct with instanceof_result:
161158
expr = instanceof_result_sym.symbol_expr();

src/ansi-c/c_nondet_symbol_factory.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ void symbol_factoryt::gen_nondet_init(
187187
auto set_null_inst=code_assignt(expr, null_pointer_exprt(pointer_type));
188188
set_null_inst.add_source_location()=loc;
189189

190-
code_ifthenelset null_check;
191-
null_check.cond() = side_effect_expr_nondett(bool_typet(), loc);
192-
null_check.then_case()=set_null_inst;
193-
null_check.else_case()=non_null_inst;
190+
code_ifthenelset null_check(
191+
side_effect_expr_nondett(bool_typet(), loc),
192+
std::move(set_null_inst),
193+
std::move(non_null_inst));
194194

195195
assignments.add(std::move(null_check));
196196
}

0 commit comments

Comments
 (0)