Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove_java_new can now follow one typecast #4747

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions jbmc/src/java_bytecode/remove_java_new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,17 @@ goto_programt::targett remove_java_newt::lower_java_new(
malloc_expr.copy_to_operands(*object_size);
// could use true and get rid of the code below
malloc_expr.copy_to_operands(false_exprt());
*target =
goto_programt::make_assignment(code_assignt(lhs, malloc_expr), location);

auto malloc_expr_casted =
typecast_exprt::conditional_cast(malloc_expr, lhs.type());

*target = goto_programt::make_assignment(
code_assignt(lhs, malloc_expr_casted), location);

// zero-initialize the object
dereference_exprt deref(lhs, object_type);
auto lhs_casted = typecast_exprt::conditional_cast(lhs, rhs.type());

dereference_exprt deref(lhs_casted, object_type);
auto zero_object = zero_initializer(object_type, location, ns);
CHECK_RETURN(zero_object.has_value());
set_class_identifier(
Expand Down Expand Up @@ -147,8 +153,11 @@ goto_programt::targett remove_java_newt::lower_java_new_array(
// code use true and get rid of the code below
malloc_expr.copy_to_operands(false_exprt());

*target =
goto_programt::make_assignment(code_assignt(lhs, malloc_expr), location);
auto lhs_casted = typecast_exprt::conditional_cast(lhs, malloc_expr.type());

*target = goto_programt::make_assignment(
code_assignt(lhs_casted, malloc_expr), location);

goto_programt::targett next = std::next(target);

const struct_typet &struct_type = to_struct_type(ns.follow(object_type));
Expand All @@ -159,7 +168,7 @@ goto_programt::targett remove_java_newt::lower_java_new_array(
PRECONDITION(struct_type.components().size() == 3);

// Init base class:
dereference_exprt deref(lhs, object_type);
dereference_exprt deref(lhs_casted, object_type);
auto zero_object = zero_initializer(object_type, location, ns);
CHECK_RETURN(zero_object.has_value());
set_class_identifier(
Expand Down Expand Up @@ -328,15 +337,17 @@ goto_programt::targett remove_java_newt::lower_java_new(
if(!target->is_assign())
return target;

if(as_const(*target).get_assign().rhs().id() == ID_side_effect)
{
// we make a copy, as we intend to destroy the assignment
// inside lower_java_new and lower_java_new_array
const auto code_assign = target->get_assign();
// we make a copy of lhs/rhs, as we intend to destroy the assignment
// inside lower_java_new and lower_java_new_array
const auto &code_assign = target->get_assign();
auto rhs = code_assign.rhs();
auto lhs = code_assign.lhs();

const exprt &lhs = code_assign.lhs();
const exprt &rhs = code_assign.rhs();
if(rhs.id() == ID_typecast) // follow one typecast
rhs = to_typecast_expr(rhs).op();

if(rhs.id() == ID_side_effect)
{
const auto &side_effect_expr = to_side_effect_expr(rhs);
const auto &statement = side_effect_expr.get_statement();

Expand Down