Skip to content

Commit 6c84caa

Browse files
committed
Refactor logic for generating a nondet int
1 parent 07d1e44 commit 6c84caa

File tree

1 file changed

+66
-32
lines changed

1 file changed

+66
-32
lines changed

jbmc/src/java_bytecode/java_object_factory.cpp

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ class java_object_factoryt
149149
size_t depth,
150150
const source_locationt &location);
151151

152+
const symbol_exprt gen_nondet_int_init(
153+
code_blockt &assignments,
154+
const std::string &basename_prefix,
155+
const exprt &min_length_expr,
156+
const exprt &max_length_expr,
157+
const source_locationt &location);
158+
152159
void gen_method_call_if_present(
153160
code_blockt &assignments,
154161
const exprt &instance_expr,
@@ -1218,6 +1225,60 @@ void java_object_factoryt::gen_nondet_init(
12181225
}
12191226
}
12201227

1228+
/// Nondeterministically initializes an int i in the range min <= i <= max,
1229+
/// where min is the integer represented by `min_value_expr` and max is the
1230+
/// integer represented by `max_value_expr`.
1231+
/// \param [out] assignments: A code block that the initializing assignments
1232+
/// will be appended to.
1233+
/// \param basename_prefix: Used for naming the newly created symbol.
1234+
/// \param min_value_expr: Represents the minimum value for the integer.
1235+
/// \param max_value_expr: Represents the maximum value for the integer.
1236+
/// \param location: Source location associated with nondet-initialization.
1237+
/// \return A symbol expression for the resulting integer.
1238+
const symbol_exprt java_object_factoryt::gen_nondet_int_init(
1239+
code_blockt &assignments,
1240+
const std::string &basename_prefix,
1241+
const exprt &min_value_expr,
1242+
const exprt &max_value_expr,
1243+
const source_locationt &location)
1244+
{
1245+
PRECONDITION(min_value_expr.type() == max_value_expr.type());
1246+
// Allocate a new symbol for the int
1247+
const symbolt &int_symbol = get_fresh_aux_symbol(
1248+
min_value_expr.type(),
1249+
id2string(object_factory_parameters.function_id),
1250+
basename_prefix,
1251+
loc,
1252+
ID_java,
1253+
symbol_table);
1254+
symbols_created.push_back(&int_symbol);
1255+
const auto &int_symbol_expr = int_symbol.symbol_expr();
1256+
1257+
// Nondet-initialize it
1258+
gen_nondet_init(
1259+
assignments,
1260+
int_symbol_expr,
1261+
false, // is_sub
1262+
irep_idt(),
1263+
false, // skip_classid
1264+
allocation_typet::LOCAL, // immaterial, type is primitive
1265+
false, // override
1266+
typet(), // override type is immaterial
1267+
0, // depth is immaterial, always non-null
1268+
update_in_placet::NO_UPDATE_IN_PLACE,
1269+
location);
1270+
1271+
// Insert assumptions to bound its value
1272+
const auto min_assume_expr =
1273+
binary_relation_exprt(int_symbol_expr, ID_ge, min_value_expr);
1274+
const auto max_assume_expr =
1275+
binary_relation_exprt(int_symbol_expr, ID_le, max_value_expr);
1276+
assignments.add(code_assumet(min_assume_expr));
1277+
assignments.add(code_assumet(max_assume_expr));
1278+
1279+
return int_symbol_expr;
1280+
}
1281+
12211282
/// Allocates a fresh array and emits an assignment writing to \p lhs the
12221283
/// address of the new array. Single-use at the moment, but useful to keep as a
12231284
/// separate function for downstream branches.
@@ -1238,46 +1299,19 @@ void java_object_factoryt::allocate_nondet_length_array(
12381299
const typet &element_type,
12391300
const source_locationt &location)
12401301
{
1241-
symbolt &length_sym = get_fresh_aux_symbol(
1242-
java_int_type(),
1243-
id2string(object_factory_parameters.function_id),
1244-
"nondet_array_length",
1245-
loc,
1246-
ID_java,
1247-
symbol_table);
1248-
symbols_created.push_back(&length_sym);
1249-
const auto &length_sym_expr=length_sym.symbol_expr();
1250-
1251-
// Initialize array with some undetermined length:
1252-
gen_nondet_init(
1302+
const auto &length_sym_expr = gen_nondet_int_init(
12531303
assignments,
1254-
length_sym_expr,
1255-
false, // is_sub
1256-
irep_idt(),
1257-
false, // skip_classid
1258-
allocation_typet::LOCAL, // immaterial, type is primitive
1259-
false, // override
1260-
typet(), // override type is immaterial
1261-
0, // depth is immaterial, always non-null
1262-
update_in_placet::NO_UPDATE_IN_PLACE,
1304+
"nondet_array_length",
1305+
from_integer(0, java_int_type()),
1306+
max_length_expr,
12631307
location);
12641308

1265-
// Insert assumptions to bound its length:
1266-
binary_relation_exprt
1267-
assume1(length_sym_expr, ID_ge, from_integer(0, java_int_type()));
1268-
binary_relation_exprt
1269-
assume2(length_sym_expr, ID_le, max_length_expr);
1270-
code_assumet assume_inst1(assume1);
1271-
code_assumet assume_inst2(assume2);
1272-
assignments.move_to_operands(assume_inst1);
1273-
assignments.move_to_operands(assume_inst2);
1274-
12751309
side_effect_exprt java_new_array(ID_java_new_array, lhs.type(), loc);
12761310
java_new_array.copy_to_operands(length_sym_expr);
12771311
java_new_array.set(ID_length_upper_bound, max_length_expr);
12781312
java_new_array.type().subtype().set(ID_element_type, element_type);
12791313
code_assignt assign(lhs, java_new_array);
1280-
assign.add_source_location()=loc;
1314+
assign.add_source_location() = loc;
12811315
assignments.copy_to_operands(assign);
12821316
}
12831317

0 commit comments

Comments
 (0)