Skip to content

Commit edefb93

Browse files
Split assign_array_from_json into two versions
Depending on whether the array has deterministic length or not. This will make it easier to allocate the array directly to the right size in the deterministic case.
1 parent 0640789 commit edefb93

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

jbmc/src/java_bytecode/assignments_from_json.cpp

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ static symbol_exprt nondet_length(
468468
/// For the assignment of the array elements, see
469469
/// \ref assign_array_data_component_from_json.
470470
/// For the overall algorithm, see \ref assign_from_json_rec.
471-
static void assign_array_from_json(
471+
static void assign_det_length_array_from_json(
472472
const exprt &expr,
473473
const jsont &json,
474474
const exprt &given_length_expr,
@@ -481,18 +481,30 @@ static void assign_array_from_json(
481481
const json_arrayt json_array = get_untyped_array(json, element_type);
482482
const auto number_of_elements =
483483
from_integer(json_array.size(), java_int_type());
484-
info.block.add(code_assumet{[&]() -> exprt {
485-
if(has_nondet_length(json))
486-
{
487-
return and_exprt{
488-
binary_predicate_exprt{given_length_expr, ID_ge, number_of_elements},
489-
binary_predicate_exprt{
490-
given_length_expr,
491-
ID_le,
492-
from_integer(info.max_user_array_length, java_int_type())}};
493-
}
494-
return equal_exprt{given_length_expr, number_of_elements};
495-
}()});
484+
info.block.add(
485+
code_assumet{equal_exprt{given_length_expr, number_of_elements}});
486+
assign_array_data_component_from_json(expr, json, type_from_array, info);
487+
}
488+
489+
static void assign_nondet_length_array_from_json(
490+
const exprt &expr,
491+
const jsont &json,
492+
const exprt &given_length_expr,
493+
const optionalt<std::string> &type_from_array,
494+
object_creation_infot &info)
495+
{
496+
PRECONDITION(is_java_array_type(expr.type()));
497+
const auto &element_type =
498+
java_array_element_type(to_struct_tag_type(expr.type().subtype()));
499+
const json_arrayt json_array = get_untyped_array(json, element_type);
500+
const auto number_of_elements =
501+
from_integer(json_array.size(), java_int_type());
502+
info.block.add(code_assumet{and_exprt{
503+
binary_predicate_exprt{given_length_expr, ID_ge, number_of_elements},
504+
binary_predicate_exprt{
505+
given_length_expr,
506+
ID_le,
507+
from_integer(info.max_user_array_length, java_int_type())}}});
496508
assign_array_data_component_from_json(expr, json, type_from_array, info);
497509
}
498510

@@ -771,8 +783,16 @@ static void assign_reference_from_json(
771783
{
772784
INVARIANT(
773785
reference.array_length, "an array reference should stores its length");
774-
assign_array_from_json(
775-
reference.expr, json, *reference.array_length, type_from_array, info);
786+
if(has_nondet_length(json))
787+
{
788+
assign_nondet_length_array_from_json(
789+
reference.expr, json, *reference.array_length, type_from_array, info);
790+
}
791+
else
792+
{
793+
assign_det_length_array_from_json(
794+
reference.expr, json, *reference.array_length, type_from_array, info);
795+
}
776796
}
777797
else
778798
assign_struct_from_json(dereference_exprt(reference.expr), json, info);
@@ -814,7 +834,16 @@ void assign_from_json_rec(
814834
const exprt length =
815835
nondet_length(info.allocate_objects, info.block, info.loc);
816836
allocate_array(expr, length, info);
817-
assign_array_from_json(expr, json, length, type_from_array, info);
837+
if(has_nondet_length(json))
838+
{
839+
assign_nondet_length_array_from_json(
840+
expr, json, length, type_from_array, info);
841+
}
842+
else
843+
{
844+
assign_det_length_array_from_json(
845+
expr, json, length, type_from_array, info);
846+
}
818847
}
819848
else if(
820849
const auto runtime_type =

0 commit comments

Comments
 (0)