Skip to content

Use goto_programt::make_X factories in goto_convert #4000

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

Merged
merged 2 commits into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
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
97 changes: 31 additions & 66 deletions src/goto-programs/goto_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,7 @@ void goto_convertt::convert(
// make sure dest is never empty
if(dest.instructions.empty())
{
dest.add_instruction(SKIP);
dest.instructions.back().code.make_nil();
dest.instructions.back().source_location=code.source_location();
dest.add(goto_programt::make_skip(code.source_location()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the use of these, couldn't we change the approach a bit and make goto_programt::add be a templated function taking a variadic number of arguments, with specialisations for the various instruction types?

template <goto_program_instruction_typet T, typename ... Params>
add(Params...);
template <> add<SKIP, source_locationt> (source_locationt loc) { ... };

My rationale is that we might eventually forbid to construct an instructiont other than via .add, and it also saves us typing goto_programt::. The above would then be dest.add<SKIP>(code.source_location());

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've been tempted by that idea. Will explore.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be ok to merge all the make_X work as that does the hard work of collecting individual assignments into a single function call. Possibly moving to add<T>(...) is then just text transformation.

}
}

Expand Down Expand Up @@ -1033,9 +1031,8 @@ void goto_convertt::convert_while(

// do the z label
goto_programt tmp_z;
goto_programt::targett z=tmp_z.add_instruction();
z->make_skip();
z->source_location=source_location;
goto_programt::targett z =
tmp_z.add(goto_programt::make_skip(source_location));

goto_programt tmp_branch;
generate_conditional_branch(
Expand Down Expand Up @@ -1107,9 +1104,8 @@ void goto_convertt::convert_dowhile(

// do the z label
goto_programt tmp_z;
goto_programt::targett z=tmp_z.add_instruction();
z->make_skip();
z->source_location=code.source_location();
goto_programt::targett z =
tmp_z.add(goto_programt::make_skip(code.source_location()));

// do the x label
goto_programt::targett x;
Expand Down Expand Up @@ -1187,8 +1183,7 @@ void goto_convertt::convert_switch(

// we first add a 'location' node for the switch statement,
// which would otherwise not be recorded
dest.add_instruction()->make_location(
code.source_location());
dest.add(goto_programt::make_location(code.source_location()));

// get the location of the end of the body, but
// default to location of switch, if none
Expand All @@ -1208,9 +1203,8 @@ void goto_convertt::convert_switch(

// do the z label
goto_programt tmp_z;
goto_programt::targett z=tmp_z.add_instruction();
z->make_skip();
z->source_location=body_end_location;
goto_programt::targett z =
tmp_z.add(goto_programt::make_skip(body_end_location));

// set the new targets -- continue stays as is
targets.set_break(z);
Expand Down Expand Up @@ -1250,17 +1244,12 @@ void goto_convertt::convert_switch(
case_number++;
guard_expr.add_source_location()=source_location;

goto_programt::targett x=tmp_cases.add_instruction();
x->make_goto(case_pair.first);
x->guard.swap(guard_expr);
x->source_location=source_location;
tmp_cases.add(goto_programt::make_goto(
case_pair.first, std::move(guard_expr), source_location));
}

{
goto_programt::targett d_jump=tmp_cases.add_instruction();
d_jump->make_goto(targets.default_target);
d_jump->source_location=targets.default_target->source_location;
}
tmp_cases.add(goto_programt::make_goto(
targets.default_target, targets.default_target->source_location));

dest.destructive_append(sideeffects);
dest.destructive_append(tmp_cases);
Expand All @@ -1284,9 +1273,8 @@ void goto_convertt::convert_break(
code.source_location(), targets.break_stack_size, dest, mode);

// add goto
goto_programt::targett t=dest.add_instruction();
t->make_goto(targets.break_target);
t->source_location=code.source_location();
dest.add(
goto_programt::make_goto(targets.break_target, code.source_location()));
}

void goto_convertt::convert_return(
Expand Down Expand Up @@ -1329,10 +1317,7 @@ void goto_convertt::convert_return(
new_code.find_source_location());

// Now add a return node to set the return value.
goto_programt::targett t=dest.add_instruction();
t->make_return();
t->code=new_code;
t->source_location=new_code.source_location();
dest.add(goto_programt::make_return(new_code, new_code.source_location()));
}
else
{
Expand All @@ -1347,9 +1332,8 @@ void goto_convertt::convert_return(
unwind_destructor_stack(code.source_location(), 0, dest, mode);

// add goto to end-of-function
goto_programt::targett t=dest.add_instruction();
t->make_goto(targets.return_target, true_exprt());
t->source_location=new_code.source_location();
dest.add(goto_programt::make_goto(
targets.return_target, new_code.source_location()));
}

void goto_convertt::convert_continue(
Expand All @@ -1367,9 +1351,8 @@ void goto_convertt::convert_continue(
code.source_location(), targets.continue_stack_size, dest, mode);

// add goto
goto_programt::targett t=dest.add_instruction();
t->make_goto(targets.continue_target);
t->source_location=code.source_location();
dest.add(
goto_programt::make_goto(targets.continue_target, code.source_location()));
}

void goto_convertt::convert_goto(const code_gotot &code, goto_programt &dest)
Expand Down Expand Up @@ -1529,11 +1512,8 @@ void goto_convertt::generate_ifthenelse(
{
// hmpf. Useless branch.
goto_programt tmp_z;
goto_programt::targett z=tmp_z.add_instruction();
z->make_skip();
goto_programt::targett v=dest.add_instruction();
v->make_goto(z, guard);
v->source_location=source_location;
goto_programt::targett z = tmp_z.add(goto_programt::make_skip());
dest.add(goto_programt::make_goto(z, guard, source_location));
dest.destructive_append(tmp_z);
return;
}
Expand Down Expand Up @@ -1706,12 +1686,7 @@ void goto_convertt::generate_conditional_branch(
exprt cond=guard;
clean_expr(cond, dest, mode);

goto_programt tmp;
goto_programt::targett g=tmp.add_instruction();
g->make_goto(target_true);
g->guard=cond;
g->source_location=source_location;
dest.destructive_append(tmp);
dest.add(goto_programt::make_goto(target_true, cond, source_location));
}
}

Expand Down Expand Up @@ -1753,10 +1728,7 @@ void goto_convertt::generate_conditional_branch(
generate_conditional_branch(
boolean_negate(expr), target_false, source_location, dest, mode);

goto_programt::targett t_true=dest.add_instruction();
t_true->make_goto(target_true);
t_true->guard=true_exprt();
t_true->source_location=source_location;
dest.add(goto_programt::make_goto(target_true, source_location));

return;
}
Expand All @@ -1772,30 +1744,25 @@ void goto_convertt::generate_conditional_branch(
std::list<exprt> op;
collect_operands(guard, guard.id(), op);

// true branch(es)
for(const auto &expr : op)
generate_conditional_branch(
expr, target_true, source_location, dest, mode);

goto_programt::targett t_false=dest.add_instruction();
t_false->make_goto(target_false);
t_false->guard=true_exprt();
t_false->source_location=guard.source_location();
// false branch
dest.add(goto_programt::make_goto(target_false, guard.source_location()));

return;
}

exprt cond=guard;
clean_expr(cond, dest, mode);

goto_programt::targett t_true=dest.add_instruction();
t_true->make_goto(target_true);
t_true->guard=cond;
t_true->source_location=source_location;
// true branch
dest.add(goto_programt::make_goto(target_true, cond, source_location));

goto_programt::targett t_false=dest.add_instruction();
t_false->make_goto(target_false);
t_false->guard=true_exprt();
t_false->source_location=source_location;
// false branch
dest.add(goto_programt::make_goto(target_false, source_location));
}

bool goto_convertt::get_string_constant(
Expand Down Expand Up @@ -1994,9 +1961,7 @@ void goto_convertt::generate_thread_block(
goto_programt::targett a=preamble.add_instruction(START_THREAD);
a->source_location=thread_body.source_location();
a->targets.push_back(c);
goto_programt::targett b=preamble.add_instruction();
b->source_location=thread_body.source_location();
b->make_goto(z);
preamble.add(goto_programt::make_goto(z, thread_body.source_location()));

dest.destructive_append(preamble);
dest.destructive_append(body);
Expand Down
7 changes: 7 additions & 0 deletions src/goto-programs/goto_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,13 @@ class goto_programt
return instructiont(code_returnt(), irep_idt(), l, RETURN, nil_exprt(), {});
}

static instructiont make_return(
code_returnt c,
const source_locationt &l = source_locationt::nil())
{
return instructiont(std::move(c), irep_idt(), l, RETURN, nil_exprt(), {});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to make progress on #3126, which removes all these irep_idt() in the make_* functions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed!

}

static instructiont
make_skip(const source_locationt &l = source_locationt::nil())
{
Expand Down