Skip to content

Fix multinewarray #686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified regression/cbmc-java/multinewarray/multinewarray.class
Binary file not shown.
4 changes: 2 additions & 2 deletions regression/cbmc-java/multinewarray/multinewarray.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public static void main(String[] args)
assert some_a[0].length==3;
assert some_a[0][0].length==2;

int x=10;
int y=20;
int x=3;
int y=5;
int[][] int_array = new int[x][y];

for(int i=0; i<x; ++i)
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-java/multinewarray/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
CORE
multinewarray.class

^EXIT=0$
Expand Down
22 changes: 19 additions & 3 deletions src/goto-programs/builtin_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -851,13 +851,19 @@ void goto_convertt::do_java_new_array(
goto_programt tmp;

symbol_exprt tmp_i=
new_tmp_symbol(index_type(), "index", tmp, location).symbol_expr();
new_tmp_symbol(length.type(), "index", tmp, location).symbol_expr();

code_fort for_loop;

side_effect_exprt sub_java_new=rhs;
sub_java_new.operands().erase(sub_java_new.operands().begin());

assert(rhs.type().id()==ID_pointer);
typet sub_type=
static_cast<const typet &>(rhs.type().subtype().find("#element_type"));
assert(sub_type.id()==ID_pointer);
sub_java_new.type()=sub_type;

side_effect_exprt inc(ID_assign);
inc.operands().resize(2);
inc.op0()=tmp_i;
Expand All @@ -866,11 +872,21 @@ void goto_convertt::do_java_new_array(
dereference_exprt deref_expr(
plus_exprt(data, tmp_i), data.type().subtype());

code_blockt for_body;
symbol_exprt init_sym=
new_tmp_symbol(sub_type, "subarray_init", tmp, location).symbol_expr();

code_assignt init_subarray(init_sym, sub_java_new);
code_assignt assign_subarray(
deref_expr,
typecast_exprt(init_sym, deref_expr.type()));
for_body.move_to_operands(init_subarray);
for_body.move_to_operands(assign_subarray);

for_loop.init()=code_assignt(tmp_i, from_integer(0, tmp_i.type()));
for_loop.cond()=binary_relation_exprt(tmp_i, ID_lt, rhs.op0());
for_loop.iter()=inc;
for_loop.body()=code_skipt();
for_loop.body()=code_assignt(deref_expr, sub_java_new);
for_loop.body()=for_body;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does the recursion happen that one would (seemingly) need for, e.g., a three-dimensional array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The java-new-array expression created is one operand shorter, so if it's still 2 or longer we'll end up here again when we convert it


convert(for_loop, tmp);
dest.destructive_append(tmp);
Expand Down