Skip to content

Commit dac0f59

Browse files
committed
Avoid default code_switch(_case)t's default constructor
Construct them bottom-up, which is more efficient and type safe.
1 parent ab81ffc commit dac0f59

File tree

3 files changed

+26
-32
lines changed

3 files changed

+26
-32
lines changed

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,9 +1930,6 @@ code_switcht java_bytecode_convert_methodt::convert_switch(
19301930
const source_locationt &location)
19311931
{
19321932
// we turn into switch-case
1933-
code_switcht code_switch;
1934-
code_switch.add_source_location() = location;
1935-
code_switch.value() = op[0];
19361933
code_blockt code_block;
19371934
code_block.add_source_location() = location;
19381935

@@ -1942,36 +1939,40 @@ code_switcht java_bytecode_convert_methodt::convert_switch(
19421939
{
19431940
if(is_label)
19441941
{
1945-
code_switch_caset code_case;
1946-
code_case.add_source_location() = location;
1947-
19481942
const mp_integer number = numeric_cast_v<mp_integer>(*a_it);
19491943
// The switch case does not contain any code, it just branches via a GOTO
19501944
// to the jump target of the tableswitch/lookupswitch case at
19511945
// hand. Therefore we consider this code to belong to the source bytecode
19521946
// instruction and not the target instruction.
19531947
const method_offsett label_number =
19541948
numeric_cast_v<method_offsett>(number);
1955-
code_case.code() = code_gotot(label(std::to_string(label_number)));
1956-
code_case.code().add_source_location() = location;
1949+
code_gotot code(label(std::to_string(label_number)));
1950+
code.add_source_location() = location;
19571951

19581952
if(a_it == args.begin())
1953+
{
1954+
code_switch_caset code_case(nil_exprt(), std::move(code));
1955+
code_case.add_source_location() = location;
19591956
code_case.set_default();
1957+
1958+
code_block.add(std::move(code_case));
1959+
}
19601960
else
19611961
{
1962-
auto prev = a_it;
1963-
prev--;
1964-
code_case.case_op() = *prev;
1965-
if(code_case.case_op().type() != op[0].type())
1966-
code_case.case_op().make_typecast(op[0].type());
1967-
code_case.case_op().add_source_location() = location;
1968-
}
1962+
exprt case_op =
1963+
typecast_exprt::conditional_cast(*std::prev(a_it), op[0].type());
1964+
case_op.add_source_location() = location;
1965+
1966+
code_switch_caset code_case(std::move(case_op), std::move(code));
1967+
code_case.add_source_location() = location;
19691968

1970-
code_block.add(code_case);
1969+
code_block.add(std::move(code_case));
1970+
}
19711971
}
19721972
}
19731973

1974-
code_switch.body() = code_block;
1974+
code_switcht code_switch(op[0], std::move(code_block));
1975+
code_switch.add_source_location() = location;
19751976
return code_switch;
19761977
}
19771978

jbmc/src/java_bytecode/java_entry_point.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,6 @@ exprt::operandst java_build_arguments(
358358
INVARIANT(!is_this, "We cannot have different types for `this` here");
359359
// create a non-deterministic switch between all possible values for the
360360
// type of the parameter.
361-
code_switcht code_switch;
362361

363362
// the idea is to get a new symbol for the parameter value `tmp`
364363

@@ -385,8 +384,10 @@ exprt::operandst java_build_arguments(
385384
symbol_table);
386385
main_arguments[param_number] = result_symbol.symbol_expr();
387386

388-
std::vector<codet> cases(alternatives.size());
389-
const auto initialize_parameter = [&](const struct_tag_typet &type) {
387+
std::vector<codet> cases;
388+
cases.reserve(alternatives.size());
389+
for(const auto &type : alternatives)
390+
{
390391
code_blockt init_code_for_type;
391392
exprt init_expr_for_parameter = object_factory(
392393
java_reference_type(type),
@@ -402,14 +403,8 @@ exprt::operandst java_build_arguments(
402403
code_assignt(
403404
result_symbol.symbol_expr(),
404405
typecast_exprt(init_expr_for_parameter, p.type())));
405-
return init_code_for_type;
406-
};
407-
408-
std::transform(
409-
alternatives.begin(),
410-
alternatives.end(),
411-
cases.begin(),
412-
initialize_parameter);
406+
cases.push_back(init_code_for_type);
407+
}
413408

414409
init_code.add(
415410
generate_nondet_switch(

src/goto-instrument/goto_program2code.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -904,9 +904,7 @@ goto_programt::const_targett goto_program2codet::convert_goto_switch(
904904
return convert_goto_if(target, upper_bound, dest);
905905

906906
// maybe, let's try some more
907-
code_switcht s;
908-
s.value()=to_equal_expr(eq_cand).lhs();
909-
s.body()=code_blockt();
907+
code_switcht s(to_equal_expr(eq_cand).lhs(), code_blockt());
910908

911909
copy_source_location(target, s);
912910

@@ -972,7 +970,7 @@ goto_programt::const_targett goto_program2codet::convert_goto_switch(
972970
it!=cases.end();
973971
++it)
974972
{
975-
code_switch_caset csc;
973+
code_switch_caset csc(nil_exprt{}, code_blockt{});
976974
// branch condition is nil_exprt for default case;
977975
if(it->value.is_nil())
978976
csc.set_default();

0 commit comments

Comments
 (0)