6
6
7
7
\*******************************************************************/
8
8
9
+ #include < iostream>
9
10
#include < cassert>
10
11
11
12
#include < util/i2string.h>
@@ -675,7 +676,7 @@ void goto_convertt::do_java_new_array(
675
676
{
676
677
if (lhs.is_nil ())
677
678
throw " do_java_new_array without lhs is yet to be implemented" ;
678
-
679
+
679
680
source_locationt location=rhs.source_location ();
680
681
681
682
assert (rhs.operands ().size ()>=1 ); // one per dimension
@@ -718,7 +719,7 @@ void goto_convertt::do_java_new_array(
718
719
719
720
// Allocate a (struct realtype**) instead of a (void**) if possible,
720
721
// to avoid confusing the trace due to dynamic type aliasing.
721
- const irept& given_element_type=rhs .find (ID_C_element_type);
722
+ const irept& given_element_type=object_type .find (ID_C_element_type);
722
723
typet allocate_data_type;
723
724
exprt cast_data_member;
724
725
if (given_element_type!=get_nil_irep ())
@@ -745,22 +746,16 @@ void goto_convertt::do_java_new_array(
745
746
t_p->code =code_assignt (data,cast_cpp_new);
746
747
t_p->source_location =location;
747
748
748
- // zero-initialize the data
749
+ // zero-initialize the data, or create subarrays if specified:
749
750
if (!rhs.get_bool (" skip_initialise" ))
750
- {
751
- exprt zero_element=gen_zero (data.type ().subtype ());
752
- codet array_set (ID_array_set);
753
- array_set.copy_to_operands (data, zero_element);
754
- goto_programt::targett t_d=dest.add_instruction (OTHER);
755
- t_d->code =array_set;
756
- t_d->source_location =location;
757
- }
758
-
759
- if (rhs.operands ().size ()>=2 )
760
751
{
761
752
// produce
762
- // for(int i=0; i<size; i++) tmp[i]=java_new(dim-1);
753
+ // for(int i=0; i<size; i++) { init=java_new(dim-1); tmp[i]=init; }
754
+ // or init=0 if this is the last dimension.
763
755
// This will be converted recursively.
756
+
757
+ // Since reference arrays carry void*, init using a local variable
758
+ // before assigning to the array.
764
759
765
760
goto_programt tmp;
766
761
@@ -769,21 +764,38 @@ void goto_convertt::do_java_new_array(
769
764
770
765
code_fort for_loop;
771
766
772
- side_effect_exprt sub_java_new=rhs;
773
- sub_java_new.operands ().erase (sub_java_new.operands ().begin ());
774
-
775
767
side_effect_exprt inc (ID_assign);
776
768
inc.operands ().resize (2 );
777
769
inc.op0 ()=tmp_i;
778
770
inc.op1 ()=plus_exprt (tmp_i, gen_one (tmp_i.type ()));
779
771
780
- dereference_exprt deref_expr (plus_exprt (data, tmp_i), data.type ().subtype ());
781
-
782
772
for_loop.init ()=code_assignt (tmp_i, gen_zero (tmp_i.type ()));
783
773
for_loop.cond ()=binary_relation_exprt (tmp_i, ID_lt, rhs.op0 ());
784
774
for_loop.iter ()=inc;
785
- for_loop.body ()=code_skipt ();
786
- for_loop.body ()=code_assignt (deref_expr, sub_java_new);
775
+
776
+ code_blockt for_body;
777
+ dereference_exprt deref_expr (plus_exprt (data, tmp_i), data.type ().subtype ());
778
+
779
+ if (rhs.operands ().size () >= 2 )
780
+ {
781
+ side_effect_exprt sub_java_new=rhs;
782
+ sub_java_new.operands ().erase (sub_java_new.operands ().begin ());
783
+ sub_java_new.type ()=static_cast <const typet &>(given_element_type);
784
+ exprt next_array_init=
785
+ new_tmp_symbol (sub_java_new.type (), " multiarray_init" , tmp, location).symbol_expr ();
786
+ for_body.copy_to_operands (code_assignt (next_array_init, sub_java_new));
787
+
788
+ if (deref_expr.type ()!=next_array_init.type ())
789
+ next_array_init=typecast_exprt (next_array_init, deref_expr.type ());
790
+ for_body.copy_to_operands (code_assignt (deref_expr, next_array_init));
791
+ }
792
+ else
793
+ {
794
+ exprt zero_element=gen_zero (deref_expr.type ());
795
+ for_body.copy_to_operands (code_assignt (deref_expr,zero_element));
796
+ }
797
+
798
+ for_loop.body ()=for_body;
787
799
788
800
convert (for_loop, tmp);
789
801
dest.destructive_append (tmp);
0 commit comments