Skip to content

Commit 3f3a702

Browse files
committed
Refactor array access into a separate function
This does not save many lines of code, but the way we access Java arrays using pointer arithmetic can be a bit confusing and it makes sense to abstract it away and keep the documentation of why we do things this way in one place.
1 parent 0672aa1 commit 3f3a702

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

jbmc/src/java_bytecode/java_object_factory.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,10 +1200,10 @@ void java_object_factoryt::array_loop_init_code(
12001200
assignments.add(std::move(max_test));
12011201
}
12021202

1203-
const dereference_exprt arraycellref{
1204-
plus_exprt{array_init_symexpr, counter_expr}};
1203+
const dereference_exprt element_at_counter =
1204+
element_at_pointer_array_index(array_init_symexpr, counter_expr);
12051205

1206-
bool new_item_is_primitive = arraycellref.type().id() != ID_pointer;
1206+
bool new_item_is_primitive = element_at_counter.type().id() != ID_pointer;
12071207

12081208
// Use a temporary to initialise a new, or update an existing, non-primitive.
12091209
// This makes it clearer that in a sequence like
@@ -1213,17 +1213,17 @@ void java_object_factoryt::array_loop_init_code(
12131213
exprt init_expr;
12141214
if(new_item_is_primitive)
12151215
{
1216-
init_expr = arraycellref;
1216+
init_expr = element_at_counter;
12171217
}
12181218
else
12191219
{
12201220
init_expr = allocate_objects.allocate_automatic_local_object(
1221-
arraycellref.type(), "new_array_item");
1221+
element_at_counter.type(), "new_array_item");
12221222

12231223
// If we're updating an existing array item, read the existing object that
12241224
// we (may) alter:
12251225
if(update_in_place != update_in_placet::NO_UPDATE_IN_PLACE)
1226-
assignments.add(code_assignt(init_expr, arraycellref));
1226+
assignments.add(code_assignt(init_expr, element_at_counter));
12271227
}
12281228

12291229
// MUST_UPDATE_IN_PLACE only applies to this object.
@@ -1249,7 +1249,7 @@ void java_object_factoryt::array_loop_init_code(
12491249
{
12501250
// We used a temporary variable to update or initialise an array item;
12511251
// now write it into the array:
1252-
assignments.add(code_assignt(arraycellref, init_expr));
1252+
assignments.add(code_assignt(element_at_counter, init_expr));
12531253
}
12541254

12551255
exprt java_one=from_integer(1, java_int_type());
@@ -1389,11 +1389,9 @@ bool java_object_factoryt::gen_nondet_enum_init(
13891389
allocate_objects,
13901390
assignments);
13911391

1392-
// Generate statement using pointer arithmetic to access array element:
1393-
// expr = (expr.type())*(enum_array_expr + index_expr);
1394-
plus_exprt plus(enum_array_expr, index_expr);
1395-
const dereference_exprt arraycellref(plus);
1396-
code_assignt enum_assign(expr, typecast_exprt(arraycellref, expr.type()));
1392+
const dereference_exprt element_at_index =
1393+
element_at_pointer_array_index(enum_array_expr, index_expr);
1394+
code_assignt enum_assign(expr, typecast_exprt(element_at_index, expr.type()));
13971395
assignments.add(enum_assign);
13981396

13991397
return true;

jbmc/src/java_bytecode/java_utils.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ dereference_exprt checked_dereference(const exprt &expr)
191191
return result;
192192
}
193193

194+
dereference_exprt
195+
element_at_pointer_array_index(const exprt &pointer, const exprt &index)
196+
{
197+
return dereference_exprt{plus_exprt{pointer, index}};
198+
}
199+
194200
/// Finds the corresponding closing delimiter to the given opening delimiter. It
195201
/// is assumed that \p open_pos points to the opening delimiter \p open_char in
196202
/// the \p src string. The depth of nesting is counted to identify the correct

jbmc/src/java_bytecode/java_utils.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ irep_idt resolve_friendly_method_name(
8181
/// \param expr: expression to dereference and check
8282
dereference_exprt checked_dereference(const exprt &expr);
8383

84+
/// Generate statement using pointer arithmetic to access the element at the
85+
/// given index of a pointer array:
86+
/// `*(pointer + index)`
87+
/// In JBMC, an array is generally represented as a pointer to its first
88+
/// element. This is because we cannot know the type of an array of
89+
/// nondeterministic length, as the length is part of any array type. But we do
90+
/// know the type of the first element of the array, so we work with that
91+
/// instead.
92+
/// \param pointer: pointer to the first element of an array
93+
/// \param index: index of the element to access
94+
/// \return expression representing the (index)'th element of the array
95+
dereference_exprt
96+
element_at_pointer_array_index(const exprt &pointer, const exprt &index);
97+
8498
/// Add the components in components_to_add to the class denoted
8599
/// by class symbol.
86100
/// \param class_symbol: The symbol representing the class we want to modify.

0 commit comments

Comments
 (0)