@@ -149,6 +149,13 @@ class java_object_factoryt
149
149
size_t depth,
150
150
const source_locationt &location);
151
151
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
+
152
159
void gen_method_call_if_present (
153
160
code_blockt &assignments,
154
161
const exprt &instance_expr,
@@ -1218,6 +1225,60 @@ void java_object_factoryt::gen_nondet_init(
1218
1225
}
1219
1226
}
1220
1227
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
+
1221
1282
// / Allocates a fresh array and emits an assignment writing to \p lhs the
1222
1283
// / address of the new array. Single-use at the moment, but useful to keep as a
1223
1284
// / separate function for downstream branches.
@@ -1238,46 +1299,19 @@ void java_object_factoryt::allocate_nondet_length_array(
1238
1299
const typet &element_type,
1239
1300
const source_locationt &location)
1240
1301
{
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 (
1253
1303
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,
1263
1307
location);
1264
1308
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
-
1275
1309
side_effect_exprt java_new_array (ID_java_new_array, lhs.type (), loc);
1276
1310
java_new_array.copy_to_operands (length_sym_expr);
1277
1311
java_new_array.set (ID_length_upper_bound, max_length_expr);
1278
1312
java_new_array.type ().subtype ().set (ID_element_type, element_type);
1279
1313
code_assignt assign (lhs, java_new_array);
1280
- assign.add_source_location ()= loc;
1314
+ assign.add_source_location () = loc;
1281
1315
assignments.copy_to_operands (assign);
1282
1316
}
1283
1317
0 commit comments