Skip to content

Commit 5542c54

Browse files
committed
Move nondet enum initialization to new function
1 parent 6ecf4f9 commit 5542c54

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

jbmc/src/java_bytecode/java_object_factory.cpp

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ class java_object_factoryt
107107
update_in_placet,
108108
const source_locationt &location);
109109

110+
void gen_nondet_enum_init(
111+
code_blockt &assignments,
112+
const exprt &expr,
113+
const java_class_typet &java_class_type,
114+
const source_locationt &location);
115+
110116
void gen_nondet_init(
111117
code_blockt &assignments,
112118
const exprt &expr,
@@ -408,46 +414,7 @@ void java_object_factoryt::gen_pointer_target_init(
408414
}
409415
if(target_class_type.get_base("java::java.lang.Enum"))
410416
{
411-
// We nondet-initialize enums to be equal to one of the constants defined
412-
// for their type:
413-
// int i = nondet(int)
414-
// assume(0 < = i < $VALUES.length)
415-
// expr = $VALUES[i]
416-
// where $VALUES is a variable generated by the Java compiler that stores
417-
// the array that is returned by Enum.values().
418-
419-
const irep_idt &class_name = target_class_type.get_name();
420-
const irep_idt values_name = id2string(class_name) + ".$VALUES";
421-
INVARIANT(
422-
ns.get_symbol_table().has_symbol(values_name),
423-
"The $VALUES array (populated by clinit_wrapper) should be in the "
424-
"symbol table");
425-
const symbolt &values = ns.lookup(values_name);
426-
427-
// Access members (length and data) of $VALUES array
428-
dereference_exprt deref_expr(values.symbol_expr());
429-
const auto &deref_struct_type =
430-
to_struct_type(ns.follow(deref_expr.type()));
431-
PRECONDITION(is_valid_java_array(deref_struct_type));
432-
const auto &comps = deref_struct_type.components();
433-
const member_exprt length_expr(deref_expr, "length", comps[1].type());
434-
const member_exprt enum_array_expr =
435-
member_exprt(deref_expr, "data", comps[2].type());
436-
437-
const symbol_exprt &index_expr = gen_nondet_int_init(
438-
assignments,
439-
"enum_index_init",
440-
from_integer(0, java_int_type()),
441-
minus_exprt(length_expr, from_integer(1, java_int_type())),
442-
location);
443-
444-
// Generate statement using pointer arithmetic to access array element:
445-
// expr = (expr.type())*(enum_array_expr + index_expr);
446-
plus_exprt plus(enum_array_expr, index_expr);
447-
const dereference_exprt arraycellref(
448-
plus, enum_array_expr.type().subtype());
449-
code_assignt enum_assign(expr, typecast_exprt(arraycellref, expr.type()));
450-
assignments.add(enum_assign);
417+
gen_nondet_enum_init(assignments, expr, target_class_type, location);
451418
return;
452419
}
453420
}
@@ -1492,6 +1459,51 @@ void java_object_factoryt::gen_nondet_array_init(
14921459
assignments.move_to_operands(init_done_label);
14931460
}
14941461

1462+
/// We nondet-initialize enums to be equal to one of the constants defined
1463+
/// for their type:
1464+
/// int i = nondet(int);
1465+
/// assume(0 < = i < $VALUES.length);
1466+
/// expr = $VALUES[i];
1467+
/// where $VALUES is a variable generated by the Java compiler that stores
1468+
/// the array that is returned by Enum.values().
1469+
void java_object_factoryt::gen_nondet_enum_init(
1470+
code_blockt &assignments,
1471+
const exprt &expr,
1472+
const java_class_typet &java_class_type,
1473+
const source_locationt &location)
1474+
{
1475+
const irep_idt &class_name = java_class_type.get_name();
1476+
const irep_idt values_name = id2string(class_name) + ".$VALUES";
1477+
INVARIANT(
1478+
ns.get_symbol_table().has_symbol(values_name),
1479+
"The $VALUES array (populated by clinit_wrapper) should be in the "
1480+
"symbol table");
1481+
const symbolt &values = ns.lookup(values_name);
1482+
1483+
// Access members (length and data) of $VALUES array
1484+
dereference_exprt deref_expr(values.symbol_expr());
1485+
const auto &deref_struct_type = to_struct_type(ns.follow(deref_expr.type()));
1486+
PRECONDITION(is_valid_java_array(deref_struct_type));
1487+
const auto &comps = deref_struct_type.components();
1488+
const member_exprt length_expr(deref_expr, "length", comps[1].type());
1489+
const member_exprt enum_array_expr =
1490+
member_exprt(deref_expr, "data", comps[2].type());
1491+
1492+
const symbol_exprt &index_expr = gen_nondet_int_init(
1493+
assignments,
1494+
"enum_index_init",
1495+
from_integer(0, java_int_type()),
1496+
minus_exprt(length_expr, from_integer(1, java_int_type())),
1497+
location);
1498+
1499+
// Generate statement using pointer arithmetic to access array element:
1500+
// expr = (expr.type())*(enum_array_expr + index_expr);
1501+
plus_exprt plus(enum_array_expr, index_expr);
1502+
const dereference_exprt arraycellref(plus);
1503+
code_assignt enum_assign(expr, typecast_exprt(arraycellref, expr.type()));
1504+
assignments.add(enum_assign);
1505+
}
1506+
14951507
/// Add code_declt instructions to `init_code` for every non-static symbol
14961508
/// in `symbols_created`
14971509
/// \param symbols_created: list of symbols

0 commit comments

Comments
 (0)