Skip to content

Commit f22d1eb

Browse files
romainbrenguierPetr Bauch
authored and
Petr Bauch
committed
Remove redundant code for nondet strings
The string creation in java_object_factory was allocating a char array, which is already done in make_nondet_char_array. We remove the duplicate allocation and simplify make_nondet_infinite_char_array by using make_allocate_code which was used in the java_object_factory version.
1 parent 42cfa05 commit f22d1eb

File tree

2 files changed

+16
-41
lines changed

2 files changed

+16
-41
lines changed

jbmc/src/java_bytecode/java_object_factory.cpp

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -526,18 +526,6 @@ static mp_integer max_value(const typet &type)
526526
UNREACHABLE;
527527
}
528528

529-
/// Create code allocating object of size `size` and assigning it to `lhs`
530-
/// \param lhs : pointer which will be allocated
531-
/// \param size : size of the object
532-
/// \return code allocation object and assigning `lhs`
533-
static codet make_allocate_code(const symbol_exprt &lhs, const exprt &size)
534-
{
535-
side_effect_exprt alloc(ID_allocate, lhs.type(), lhs.source_location());
536-
alloc.copy_to_operands(size);
537-
alloc.copy_to_operands(false_exprt());
538-
return code_assignt(lhs, alloc);
539-
}
540-
541529
/// Check if a structure is a nondeterministic String structure, and if it is
542530
/// initialize its length and data fields.
543531
/// \param struct_expr [out]: struct that we may initialize
@@ -648,25 +636,8 @@ bool initialize_nondet_string_fields(
648636
code_assumet(binary_relation_exprt(length_expr, ID_le, max_length)));
649637
}
650638

651-
// char (*array_data_init)[INFINITY];
652-
const typet data_ptr_type = pointer_type(
653-
array_typet(java_char_type(), infinity_exprt(java_int_type())));
654-
655-
symbolt &data_pointer_sym = get_fresh_aux_symbol(
656-
data_ptr_type, "", "string_data_pointer", loc, ID_java, symbol_table);
657-
const auto data_pointer = data_pointer_sym.symbol_expr();
658-
code.add(code_declt(data_pointer));
659-
660-
// Dynamic allocation: `data array = allocate char[INFINITY]`
661-
code.add(make_allocate_code(data_pointer, infinity_exprt(java_int_type())));
662-
663-
// `data_expr` is `*data_pointer`
664-
// data_expr = nondet(char[INFINITY]) // we use infinity for variable size
665-
const dereference_exprt data_expr(data_pointer);
666-
const exprt nondet_array =
639+
const exprt data_expr =
667640
make_nondet_infinite_char_array(symbol_table, loc, function_id, code);
668-
code.add(code_assignt(data_expr, nondet_array));
669-
670641
struct_expr.operands()[struct_type.component_number("length")] = length_expr;
671642

672643
const address_of_exprt array_pointer(

jbmc/src/java_bytecode/java_string_library_preprocess.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,18 @@ codet java_string_library_preprocesst::code_return_function_application(
617617
return code_returnt(fun_app);
618618
}
619619

620+
/// Create code allocating object of size `size` and assigning it to `lhs`
621+
/// \param lhs : pointer which will be allocated
622+
/// \param size : size of the object
623+
/// \return code allocation object and assigning `lhs`
624+
static codet make_allocate_code(const symbol_exprt &lhs, const exprt &size)
625+
{
626+
side_effect_exprt alloc(ID_allocate, lhs.type(), lhs.source_location());
627+
alloc.add_to_operands(size);
628+
alloc.add_to_operands(false_exprt());
629+
return code_assignt(lhs, alloc);
630+
}
631+
620632
/// Declare a fresh symbol of type array of character with infinite size.
621633
/// \param symbol_table: the symbol table
622634
/// \param loc: source location
@@ -639,17 +651,9 @@ exprt make_nondet_infinite_char_array(
639651
ID_java,
640652
symbol_table);
641653

642-
const exprt data_pointer = data_sym.symbol_expr();
643-
std::vector<const symbolt *> created_symbols;
644-
allocate_dynamic_object(
645-
data_pointer,
646-
array_type,
647-
symbol_table,
648-
loc,
649-
function_id,
650-
code,
651-
created_symbols,
652-
false);
654+
const symbol_exprt data_pointer = data_sym.symbol_expr();
655+
code.add(code_declt(data_pointer));
656+
code.add(make_allocate_code(data_pointer, array_type.size()));
653657
const exprt nondet_data = side_effect_expr_nondett(array_type, loc);
654658
const exprt data = dereference_exprt(data_pointer, array_type);
655659
code.add(code_assignt(data, nondet_data), loc);

0 commit comments

Comments
 (0)