Skip to content

Commit db3345f

Browse files
Extract convert_switch function
1 parent 9165feb commit db3345f

File tree

2 files changed

+56
-44
lines changed

2 files changed

+56
-44
lines changed

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,50 +1597,7 @@ codet java_bytecode_convert_methodt::convert_instructions(
15971597
statement=="lookupswitch")
15981598
{
15991599
PRECONDITION(op.size() == 1 && results.empty());
1600-
1601-
// we turn into switch-case
1602-
code_switcht code_switch;
1603-
code_switch.add_source_location()=i_it->source_location;
1604-
code_switch.value()=op[0];
1605-
code_blockt code_block;
1606-
code_block.add_source_location()=i_it->source_location;
1607-
1608-
bool is_label=true;
1609-
for(instructiont::argst::const_iterator
1610-
a_it=i_it->args.begin();
1611-
a_it!=i_it->args.end();
1612-
a_it++, is_label=!is_label)
1613-
{
1614-
if(is_label)
1615-
{
1616-
code_switch_caset code_case;
1617-
code_case.add_source_location()=i_it->source_location;
1618-
1619-
mp_integer number;
1620-
bool ret=to_integer(to_constant_expr(*a_it), number);
1621-
DATA_INVARIANT(!ret, "case label expected to be integer");
1622-
code_case.code()=code_gotot(label(integer2string(number)));
1623-
code_case.code().add_source_location()=
1624-
address_map.at(integer2unsigned(number)).source->source_location;
1625-
1626-
if(a_it==i_it->args.begin())
1627-
code_case.set_default();
1628-
else
1629-
{
1630-
instructiont::argst::const_iterator prev=a_it;
1631-
prev--;
1632-
code_case.case_op()=*prev;
1633-
if(code_case.case_op().type()!=op[0].type())
1634-
code_case.case_op().make_typecast(op[0].type());
1635-
code_case.case_op().add_source_location()=i_it->source_location;
1636-
}
1637-
1638-
code_block.add(code_case);
1639-
}
1640-
}
1641-
1642-
code_switch.body()=code_block;
1643-
c=code_switch;
1600+
c = convert_switch(address_map, i_it, op);
16441601
}
16451602
else if(statement=="pop" || statement=="pop2")
16461603
{
@@ -1923,6 +1880,55 @@ codet java_bytecode_convert_methodt::convert_instructions(
19231880
return code;
19241881
}
19251882

1883+
codet java_bytecode_convert_methodt::convert_switch(
1884+
java_bytecode_convert_methodt::address_mapt &address_map,
1885+
const std::vector<java_bytecode_parse_treet::instructiont>::const_iterator
1886+
&i_it,
1887+
const exprt::operandst &op)
1888+
{
1889+
// we turn into switch-case
1890+
code_switcht code_switch;
1891+
code_switch.add_source_location() = i_it->source_location;
1892+
code_switch.value() = op[0];
1893+
code_blockt code_block;
1894+
code_block.add_source_location() = i_it->source_location;
1895+
1896+
bool is_label = true;
1897+
for(auto a_it = i_it->args.begin(); a_it != i_it->args.end();
1898+
a_it++, is_label = !is_label)
1899+
{
1900+
if(is_label)
1901+
{
1902+
code_switch_caset code_case;
1903+
code_case.add_source_location() = i_it->source_location;
1904+
1905+
mp_integer number;
1906+
bool ret = to_integer(to_constant_expr(*a_it), number);
1907+
DATA_INVARIANT(!ret, "case label expected to be integer");
1908+
code_case.code() = code_gotot(label(integer2string(number)));
1909+
code_case.code().add_source_location() =
1910+
address_map.at(integer2unsigned(number)).source->source_location;
1911+
1912+
if(a_it == i_it->args.begin())
1913+
code_case.set_default();
1914+
else
1915+
{
1916+
auto prev = a_it;
1917+
prev--;
1918+
code_case.case_op() = *prev;
1919+
if(code_case.case_op().type() != op[0].type())
1920+
code_case.case_op().make_typecast(op[0].type());
1921+
code_case.case_op().add_source_location() = i_it->source_location;
1922+
}
1923+
1924+
code_block.add(code_case);
1925+
}
1926+
}
1927+
1928+
code_switch.body() = code_block;
1929+
return code_switch;
1930+
}
1931+
19261932
void java_bytecode_convert_methodt::convert_dup2(
19271933
exprt::operandst &op,
19281934
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
@@ -475,5 +475,11 @@ class java_bytecode_convert_methodt:public messaget
475475
void convert_dup2_x1(exprt::operandst &op, exprt::operandst &results);
476476

477477
void convert_dup2(exprt::operandst &op, exprt::operandst &results);
478+
479+
codet convert_switch(
480+
address_mapt &address_map,
481+
const std::vector<java_bytecode_parse_treet::instructiont>::const_iterator
482+
&i_it,
483+
const exprt::operandst &op);
478484
};
479485
#endif

0 commit comments

Comments
 (0)