Skip to content

Commit edc4a28

Browse files
Extract convert_newarray function
1 parent f8d00f6 commit edc4a28

File tree

2 files changed

+68
-52
lines changed

2 files changed

+68
-52
lines changed

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

+60-52
Original file line numberDiff line numberDiff line change
@@ -1822,64 +1822,14 @@ codet java_bytecode_convert_methodt::convert_instructions(
18221822
{
18231823
// use temporary since the stack symbol might get duplicated
18241824
PRECONDITION(op.empty() && results.size() == 1);
1825-
convert_new(i_it, arg0, c, results);
1825+
convert_new(i_it->source_location, arg0, c, results);
18261826
}
18271827
else if(statement=="newarray" ||
18281828
statement=="anewarray")
18291829
{
18301830
// the op is the array size
18311831
PRECONDITION(op.size() == 1 && results.size() == 1);
1832-
1833-
char element_type;
1834-
1835-
if(statement=="newarray")
1836-
{
1837-
irep_idt id=arg0.type().id();
1838-
1839-
if(id==ID_bool)
1840-
element_type='z';
1841-
else if(id==ID_char)
1842-
element_type='c';
1843-
else if(id==ID_float)
1844-
element_type='f';
1845-
else if(id==ID_double)
1846-
element_type='d';
1847-
else if(id==ID_byte)
1848-
element_type='b';
1849-
else if(id==ID_short)
1850-
element_type='s';
1851-
else if(id==ID_int)
1852-
element_type='i';
1853-
else if(id==ID_long)
1854-
element_type='j';
1855-
else
1856-
element_type='?';
1857-
}
1858-
else
1859-
element_type='a';
1860-
1861-
const reference_typet ref_type=
1862-
java_array_type(element_type);
1863-
1864-
side_effect_exprt java_new_array(ID_java_new_array, ref_type);
1865-
java_new_array.copy_to_operands(op[0]);
1866-
1867-
if(!i_it->source_location.get_line().empty())
1868-
java_new_array.add_source_location()=i_it->source_location;
1869-
1870-
c=code_blockt();
1871-
1872-
if(max_array_length!=0)
1873-
{
1874-
constant_exprt size_limit=
1875-
from_integer(max_array_length, java_int_type());
1876-
binary_relation_exprt le_max_size(op[0], ID_le, size_limit);
1877-
code_assumet assume_le_max_size(le_max_size);
1878-
c.move_to_operands(assume_le_max_size);
1879-
}
1880-
const exprt tmp=tmp_variable("newarray", ref_type);
1881-
c.copy_to_operands(code_assignt(tmp, java_new_array));
1882-
results[0]=tmp;
1832+
convert_newarray(i_it->source_location, statement, arg0, op, c, results);
18831833
}
18841834
else if(statement=="multianewarray")
18851835
{
@@ -2384,6 +2334,64 @@ codet java_bytecode_convert_methodt::convert_instructions(
23842334
return code;
23852335
}
23862336

2337+
void java_bytecode_convert_methodt::convert_newarray(
2338+
const source_locationt &location,
2339+
const irep_idt &statement,
2340+
const exprt &arg0,
2341+
const exprt::operandst &op,
2342+
codet &c,
2343+
exprt::operandst &results)
2344+
{
2345+
char element_type;
2346+
2347+
if(statement == "newarray")
2348+
{
2349+
irep_idt id = arg0.type().id();
2350+
2351+
if(id == ID_bool)
2352+
element_type = 'z';
2353+
else if(id == ID_char)
2354+
element_type = 'c';
2355+
else if(id == ID_float)
2356+
element_type = 'f';
2357+
else if(id == ID_double)
2358+
element_type = 'd';
2359+
else if(id == ID_byte)
2360+
element_type = 'b';
2361+
else if(id == ID_short)
2362+
element_type = 's';
2363+
else if(id == ID_int)
2364+
element_type = 'i';
2365+
else if(id == ID_long)
2366+
element_type = 'j';
2367+
else
2368+
element_type = '?';
2369+
}
2370+
else
2371+
element_type = 'a';
2372+
2373+
const reference_typet ref_type = java_array_type(element_type);
2374+
2375+
side_effect_exprt java_new_array(ID_java_new_array, ref_type);
2376+
java_new_array.copy_to_operands(op[0]);
2377+
2378+
if(!location.get_line().empty())
2379+
java_new_array.add_source_location() = location;
2380+
2381+
c = code_blockt();
2382+
2383+
if(max_array_length != 0)
2384+
{
2385+
constant_exprt size_limit = from_integer(max_array_length, java_int_type());
2386+
binary_relation_exprt le_max_size(op[0], ID_le, size_limit);
2387+
code_assumet assume_le_max_size(le_max_size);
2388+
c.move_to_operands(assume_le_max_size);
2389+
}
2390+
const exprt tmp = tmp_variable("newarray", ref_type);
2391+
c.copy_to_operands(code_assignt(tmp, java_new_array));
2392+
results[0] = tmp;
2393+
}
2394+
23872395
void java_bytecode_convert_methodt::convert_new(
23882396
const source_locationt &location,
23892397
const exprt &arg0,

jbmc/src/java_bytecode/java_bytecode_convert_method_class.h

+8
Original file line numberDiff line numberDiff line change
@@ -401,5 +401,13 @@ class java_bytecode_convert_methodt:public messaget
401401
const exprt &arg0,
402402
codet &c,
403403
exprt::operandst &results);
404+
405+
void convert_newarray(
406+
const source_locationt &location,
407+
const irep_idt &statement,
408+
const exprt &arg0,
409+
const exprt::operandst &op,
410+
codet &c,
411+
exprt::operandst &results);
404412
};
405413
#endif

0 commit comments

Comments
 (0)