Skip to content

Commit 102885f

Browse files
committed
Java object factory: allocate fixed length arrays
This replaces the variable-length arrays produced by the Java object factory with fixed-length ones annotated with a variable length member. This allows the backend to use the much cheaper fixed-length array lowering rather than the full variable-length array theory, while retaining the ability for the effective length to vary. In effect this creates a bounded- length array construct. Fixes diffblue#746
1 parent 4cfcb87 commit 102885f

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

src/goto-programs/builtin_functions.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,17 @@ void goto_convertt::do_java_new_array(
824824
struct_type.components()[2].get_name(),
825825
struct_type.components()[2].type());
826826
side_effect_exprt data_cpp_new_expr(ID_cpp_new_array, data.type());
827-
data_cpp_new_expr.set(ID_size, rhs.op0());
827+
828+
// The instruction may specify a (hopefully small) upper bound on the
829+
// array size, in which case we allocate a fixed-length array that may
830+
// be larger than the `length` member rather than use a true variable-
831+
// length array, which produces a more complex formula in the current
832+
// backend.
833+
const irept size_bound=rhs.find(ID_length_upper_bound);
834+
if(size_bound.is_nil())
835+
data_cpp_new_expr.set(ID_size, rhs.op0());
836+
else
837+
data_cpp_new_expr.set(ID_size, size_bound);
828838
goto_programt::targett t_p=dest.add_instruction(ASSIGN);
829839
t_p->code=code_assignt(data, data_cpp_new_expr);
830840
t_p->source_location=location;

src/java_bytecode/java_object_factory.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ void java_object_factoryt::gen_nondet_array_init(
386386

387387
side_effect_exprt java_new_array(ID_java_new_array, expr.type());
388388
java_new_array.copy_to_operands(length_sym_expr);
389+
java_new_array.set(ID_length_upper_bound, max_length_expr);
389390
java_new_array.type().subtype().set(ID_C_element_type, element_type);
390391
codet assign=code_assignt(expr, java_new_array);
391392
assign.add_source_location()=loc;

src/util/irep_ids.def

+1
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,7 @@ IREP_ID_ONE(java_instanceof)
741741
IREP_ID_ONE(java_super_method_call)
742742
IREP_ID_ONE(java_enum_static_unwind)
743743
IREP_ID_ONE(push_catch)
744+
IREP_ID_ONE(length_upper_bound)
744745
IREP_ID_ONE(string_constraint)
745746
IREP_ID_ONE(string_not_contains_constraint)
746747
IREP_ID_ONE(cprover_char_literal_func)

0 commit comments

Comments
 (0)