Skip to content

Commit 5e37b9e

Browse files
Extract convert_switch function
1 parent ea3399a commit 5e37b9e

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
@@ -1601,50 +1601,7 @@ codet java_bytecode_convert_methodt::convert_instructions(
16011601
statement=="lookupswitch")
16021602
{
16031603
PRECONDITION(op.size() == 1 && results.empty());
1604-
1605-
// we turn into switch-case
1606-
code_switcht code_switch;
1607-
code_switch.add_source_location()=i_it->source_location;
1608-
code_switch.value()=op[0];
1609-
code_blockt code_block;
1610-
code_block.add_source_location()=i_it->source_location;
1611-
1612-
bool is_label=true;
1613-
for(instructiont::argst::const_iterator
1614-
a_it=i_it->args.begin();
1615-
a_it!=i_it->args.end();
1616-
a_it++, is_label=!is_label)
1617-
{
1618-
if(is_label)
1619-
{
1620-
code_switch_caset code_case;
1621-
code_case.add_source_location()=i_it->source_location;
1622-
1623-
mp_integer number;
1624-
bool ret=to_integer(to_constant_expr(*a_it), number);
1625-
DATA_INVARIANT(!ret, "case label expected to be integer");
1626-
code_case.code()=code_gotot(label(integer2string(number)));
1627-
code_case.code().add_source_location()=
1628-
address_map.at(integer2unsigned(number)).source->source_location;
1629-
1630-
if(a_it==i_it->args.begin())
1631-
code_case.set_default();
1632-
else
1633-
{
1634-
instructiont::argst::const_iterator prev=a_it;
1635-
prev--;
1636-
code_case.case_op()=*prev;
1637-
if(code_case.case_op().type()!=op[0].type())
1638-
code_case.case_op().make_typecast(op[0].type());
1639-
code_case.case_op().add_source_location()=i_it->source_location;
1640-
}
1641-
1642-
code_block.add(code_case);
1643-
}
1644-
}
1645-
1646-
code_switch.body()=code_block;
1647-
c=code_switch;
1604+
c = convert_switch(address_map, i_it, op);
16481605
}
16491606
else if(statement=="pop" || statement=="pop2")
16501607
{
@@ -1927,6 +1884,55 @@ codet java_bytecode_convert_methodt::convert_instructions(
19271884
return code;
19281885
}
19291886

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

464464
void convert_dup2(exprt::operandst &op, exprt::operandst &results);
465+
466+
codet convert_switch(
467+
address_mapt &address_map,
468+
const std::vector<java_bytecode_parse_treet::instructiont>::const_iterator
469+
&i_it,
470+
const exprt::operandst &op);
465471
};
466472
#endif

0 commit comments

Comments
 (0)