Skip to content

Commit 096e83e

Browse files
Extract convert_switch function
1 parent 0249464 commit 096e83e

File tree

2 files changed

+56
-44
lines changed

2 files changed

+56
-44
lines changed

src/java_bytecode/java_bytecode_convert_method.cpp

+50-44
Original file line numberDiff line numberDiff line change
@@ -1588,50 +1588,7 @@ codet java_bytecode_convert_methodt::convert_instructions(
15881588
statement=="lookupswitch")
15891589
{
15901590
PRECONDITION(op.size() == 1 && results.empty());
1591-
1592-
// we turn into switch-case
1593-
code_switcht code_switch;
1594-
code_switch.add_source_location()=i_it->source_location;
1595-
code_switch.value()=op[0];
1596-
code_blockt code_block;
1597-
code_block.add_source_location()=i_it->source_location;
1598-
1599-
bool is_label=true;
1600-
for(instructiont::argst::const_iterator
1601-
a_it=i_it->args.begin();
1602-
a_it!=i_it->args.end();
1603-
a_it++, is_label=!is_label)
1604-
{
1605-
if(is_label)
1606-
{
1607-
code_switch_caset code_case;
1608-
code_case.add_source_location()=i_it->source_location;
1609-
1610-
mp_integer number;
1611-
bool ret=to_integer(to_constant_expr(*a_it), number);
1612-
DATA_INVARIANT(!ret, "case label expected to be integer");
1613-
code_case.code()=code_gotot(label(integer2string(number)));
1614-
code_case.code().add_source_location()=
1615-
address_map.at(integer2unsigned(number)).source->source_location;
1616-
1617-
if(a_it==i_it->args.begin())
1618-
code_case.set_default();
1619-
else
1620-
{
1621-
instructiont::argst::const_iterator prev=a_it;
1622-
prev--;
1623-
code_case.case_op()=*prev;
1624-
if(code_case.case_op().type()!=op[0].type())
1625-
code_case.case_op().make_typecast(op[0].type());
1626-
code_case.case_op().add_source_location()=i_it->source_location;
1627-
}
1628-
1629-
code_block.add(code_case);
1630-
}
1631-
}
1632-
1633-
code_switch.body()=code_block;
1634-
c=code_switch;
1591+
c = convert_switch(address_map, i_it, op);
16351592
}
16361593
else if(statement=="pop" || statement=="pop2")
16371594
{
@@ -1914,6 +1871,55 @@ codet java_bytecode_convert_methodt::convert_instructions(
19141871
return code;
19151872
}
19161873

1874+
codet java_bytecode_convert_methodt::convert_switch(
1875+
java_bytecode_convert_methodt::address_mapt &address_map,
1876+
const std::vector<java_bytecode_parse_treet::instructiont>::const_iterator
1877+
&i_it,
1878+
const exprt::operandst &op)
1879+
{
1880+
// we turn into switch-case
1881+
code_switcht code_switch;
1882+
code_switch.add_source_location() = i_it->source_location;
1883+
code_switch.value() = op[0];
1884+
code_blockt code_block;
1885+
code_block.add_source_location() = i_it->source_location;
1886+
1887+
bool is_label = true;
1888+
for(auto a_it = i_it->args.begin(); a_it != i_it->args.end();
1889+
a_it++, is_label = !is_label)
1890+
{
1891+
if(is_label)
1892+
{
1893+
code_switch_caset code_case;
1894+
code_case.add_source_location() = i_it->source_location;
1895+
1896+
mp_integer number;
1897+
bool ret = to_integer(to_constant_expr(*a_it), number);
1898+
DATA_INVARIANT(!ret, "case label expected to be integer");
1899+
code_case.code() = code_gotot(label(integer2string(number)));
1900+
code_case.code().add_source_location() =
1901+
address_map.at(integer2unsigned(number)).source->source_location;
1902+
1903+
if(a_it == i_it->args.begin())
1904+
code_case.set_default();
1905+
else
1906+
{
1907+
auto prev = a_it;
1908+
prev--;
1909+
code_case.case_op() = *prev;
1910+
if(code_case.case_op().type() != op[0].type())
1911+
code_case.case_op().make_typecast(op[0].type());
1912+
code_case.case_op().add_source_location() = i_it->source_location;
1913+
}
1914+
1915+
code_block.add(code_case);
1916+
}
1917+
}
1918+
1919+
code_switch.body() = code_block;
1920+
return code_switch;
1921+
}
1922+
19171923
void java_bytecode_convert_methodt::convert_dup2(
19181924
exprt::operandst &op,
19191925
exprt::operandst &results)

src/java_bytecode/java_bytecode_convert_method_class.h

+6
Original file line numberDiff line numberDiff line change
@@ -460,5 +460,11 @@ class java_bytecode_convert_methodt:public messaget
460460
void convert_dup2_x1(exprt::operandst &op, exprt::operandst &results);
461461

462462
void convert_dup2(exprt::operandst &op, exprt::operandst &results);
463+
464+
codet convert_switch(
465+
address_mapt &address_map,
466+
const std::vector<java_bytecode_parse_treet::instructiont>::const_iterator
467+
&i_it,
468+
const exprt::operandst &op);
463469
};
464470
#endif

0 commit comments

Comments
 (0)