Skip to content

Commit 6a79aa2

Browse files
Extract convert_switch function
1 parent abfb887 commit 6a79aa2

File tree

2 files changed

+60
-44
lines changed

2 files changed

+60
-44
lines changed

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,50 +1616,11 @@ codet java_bytecode_convert_methodt::convert_instructions(
16161616
statement=="lookupswitch")
16171617
{
16181618
PRECONDITION(op.size() == 1 && results.empty());
1619-
1620-
// we turn into switch-case
1621-
code_switcht code_switch;
1622-
code_switch.add_source_location()=i_it->source_location;
1623-
code_switch.value()=op[0];
1624-
code_blockt code_block;
1625-
code_block.add_source_location()=i_it->source_location;
1626-
1627-
bool is_label=true;
1628-
for(instructiont::argst::const_iterator
1629-
a_it=i_it->args.begin();
1630-
a_it!=i_it->args.end();
1631-
a_it++, is_label=!is_label)
1632-
{
1633-
if(is_label)
1634-
{
1635-
code_switch_caset code_case;
1636-
code_case.add_source_location()=i_it->source_location;
1637-
1638-
mp_integer number;
1639-
bool ret=to_integer(to_constant_expr(*a_it), number);
1640-
DATA_INVARIANT(!ret, "case label expected to be integer");
1641-
code_case.code()=code_gotot(label(integer2string(number)));
1642-
code_case.code().add_source_location()=
1643-
address_map.at(integer2unsigned(number)).source->source_location;
1644-
1645-
if(a_it==i_it->args.begin())
1646-
code_case.set_default();
1647-
else
1648-
{
1649-
instructiont::argst::const_iterator prev=a_it;
1650-
prev--;
1651-
code_case.case_op()=*prev;
1652-
if(code_case.case_op().type()!=op[0].type())
1653-
code_case.case_op().make_typecast(op[0].type());
1654-
code_case.case_op().add_source_location()=i_it->source_location;
1655-
}
1656-
1657-
code_block.add(code_case);
1658-
}
1659-
}
1660-
1661-
code_switch.body()=code_block;
1662-
c=code_switch;
1619+
c = convert_switch(
1620+
address_map,
1621+
op,
1622+
i_it->args,
1623+
i_it->source_location);
16631624
}
16641625
else if(statement=="pop" || statement=="pop2")
16651626
{
@@ -1942,6 +1903,55 @@ codet java_bytecode_convert_methodt::convert_instructions(
19421903
return code;
19431904
}
19441905

1906+
codet java_bytecode_convert_methodt::convert_switch(
1907+
java_bytecode_convert_methodt::address_mapt &address_map,
1908+
const exprt::operandst &op,
1909+
const java_bytecode_parse_treet::instructiont::argst &args,
1910+
const source_locationt &location)
1911+
{
1912+
// we turn into switch-case
1913+
code_switcht code_switch;
1914+
code_switch.add_source_location() = location;
1915+
code_switch.value() = op[0];
1916+
code_blockt code_block;
1917+
code_block.add_source_location() = location;
1918+
1919+
bool is_label = true;
1920+
for(auto a_it = args.begin(); a_it != args.end();
1921+
a_it++, is_label = !is_label)
1922+
{
1923+
if(is_label)
1924+
{
1925+
code_switch_caset code_case;
1926+
code_case.add_source_location() = location;
1927+
1928+
mp_integer number;
1929+
bool ret = to_integer(to_constant_expr(*a_it), number);
1930+
DATA_INVARIANT(!ret, "case label expected to be integer");
1931+
code_case.code() = code_gotot(label(integer2string(number)));
1932+
code_case.code().add_source_location() =
1933+
address_map.at(integer2unsigned(number)).source->source_location;
1934+
1935+
if(a_it == args.begin())
1936+
code_case.set_default();
1937+
else
1938+
{
1939+
auto prev = a_it;
1940+
prev--;
1941+
code_case.case_op() = *prev;
1942+
if(code_case.case_op().type() != op[0].type())
1943+
code_case.case_op().make_typecast(op[0].type());
1944+
code_case.case_op().add_source_location() = location;
1945+
}
1946+
1947+
code_block.add(code_case);
1948+
}
1949+
}
1950+
1951+
code_switch.body() = code_block;
1952+
return code_switch;
1953+
}
1954+
19451955
void java_bytecode_convert_methodt::convert_dup2(
19461956
exprt::operandst &op,
19471957
exprt::operandst &results)

jbmc/src/java_bytecode/java_bytecode_convert_method_class.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,5 +465,11 @@ class java_bytecode_convert_methodt:public messaget
465465
convert_dup2_x1(exprt::operandst &op, exprt::operandst &results);
466466

467467
void convert_dup2(exprt::operandst &op, exprt::operandst &results);
468+
469+
codet convert_switch(
470+
java_bytecode_convert_methodt::address_mapt &address_map,
471+
const exprt::operandst &op,
472+
const java_bytecode_parse_treet::instructiont::argst &args,
473+
const source_locationt &location);
468474
};
469475
#endif

0 commit comments

Comments
 (0)