Skip to content

Commit d8598f8

Browse files
Merge pull request #4735 from romainbrenguier/clean-up/symex-assign
Clean-up symex assignments
2 parents 4c38989 + 63e01d9 commit d8598f8

File tree

8 files changed

+105
-62
lines changed

8 files changed

+105
-62
lines changed

src/goto-symex/goto_symex.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -557,14 +557,14 @@ class goto_symext
557557
const ssa_exprt &lhs,
558558
const exprt &full_lhs,
559559
const struct_exprt &rhs,
560-
exprt::operandst &,
560+
const exprt::operandst &,
561561
assignment_typet);
562562
void symex_assign_symbol(
563563
statet &,
564564
const ssa_exprt &lhs,
565565
const exprt &full_lhs,
566566
const exprt &rhs,
567-
exprt::operandst &,
567+
const exprt::operandst &,
568568
assignment_typet);
569569
void symex_assign_typecast(
570570
statet &,
@@ -602,14 +602,6 @@ class goto_symext
602602
exprt::operandst &,
603603
assignment_typet);
604604

605-
/// Store the \p what expression by recursively descending into the operands
606-
/// of \p lhs until the first operand \c op0 is _nil_: this _nil_ operand
607-
/// is then replaced with \p what.
608-
/// \param lhs: Non-symbol pointed-to expression
609-
/// \param what: The expression to be added to the \p lhs
610-
/// \return The resulting expression
611-
static exprt add_to_lhs(const exprt &lhs, const exprt &what);
612-
613605
virtual void
614606
symex_va_start(statet &, const exprt &lhs, const side_effect_exprt &);
615607

src/goto-symex/ssa_step.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,25 @@ irep_idt SSA_stept::get_property_id() const
213213

214214
return property_id;
215215
}
216+
217+
SSA_assignment_stept::SSA_assignment_stept(
218+
symex_targett::sourcet source,
219+
exprt _guard,
220+
ssa_exprt _ssa_lhs,
221+
exprt _ssa_full_lhs,
222+
exprt _original_full_lhs,
223+
exprt _ssa_rhs,
224+
exprt _cond_expr,
225+
symex_targett::assignment_typet _assignment_type,
226+
bool _hidden)
227+
: SSA_stept(source, goto_trace_stept::typet::ASSIGNMENT)
228+
{
229+
guard = std::move(_guard);
230+
ssa_lhs = std::move(_ssa_lhs);
231+
ssa_full_lhs = std::move(_ssa_full_lhs);
232+
original_full_lhs = std::move(_original_full_lhs);
233+
ssa_rhs = std::move(_ssa_rhs);
234+
assignment_type = _assignment_type;
235+
cond_expr = std::move(_cond_expr);
236+
hidden = _hidden;
237+
}

src/goto-symex/ssa_step.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,19 @@ class SSA_stept
195195
void validate(const namespacet &ns, const validation_modet vm) const;
196196
};
197197

198+
class SSA_assignment_stept : public SSA_stept
199+
{
200+
public:
201+
SSA_assignment_stept(
202+
symex_targett::sourcet source,
203+
exprt guard,
204+
ssa_exprt ssa_lhs,
205+
exprt ssa_full_lhs,
206+
exprt original_full_lhs,
207+
exprt ssa_rhs,
208+
exprt cond_expr,
209+
symex_targett::assignment_typet assignment_type,
210+
bool hidden);
211+
};
212+
198213
#endif // CPROVER_GOTO_SYMEX_SSA_STEP_H

src/goto-symex/symex_assign.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
1515
#include <util/c_types.h>
1616
#include <util/cprover_prefix.h>
1717
#include <util/exception_utils.h>
18+
#include <util/expr_util.h>
1819
#include <util/pointer_offset_size.h>
1920
#include <util/simplify_expr.h>
2021

@@ -79,9 +80,13 @@ void goto_symext::symex_assign(statet &state, const code_assignt &code)
7980
}
8081
}
8182

82-
exprt goto_symext::add_to_lhs(
83-
const exprt &lhs,
84-
const exprt &what)
83+
/// Store the \p what expression by recursively descending into the operands
84+
/// of \p lhs until the first operand \c op0 is _nil_: this _nil_ operand
85+
/// is then replaced with \p what.
86+
/// \param lhs: Non-symbol pointed-to expression
87+
/// \param what: The expression to be added to the \p lhs
88+
/// \return The resulting expression
89+
static exprt add_to_lhs(const exprt &lhs, const exprt &what)
8590
{
8691
PRECONDITION(lhs.id() != ID_symbol);
8792
exprt tmp_what=what;
@@ -387,7 +392,7 @@ void goto_symext::symex_assign_from_struct(
387392
const ssa_exprt &lhs, // L1
388393
const exprt &full_lhs,
389394
const struct_exprt &rhs,
390-
exprt::operandst &guard,
395+
const exprt::operandst &guard,
391396
assignment_typet assignment_type)
392397
{
393398
const struct_typet &type = to_struct_type(ns.follow(lhs.type()));
@@ -419,7 +424,7 @@ void goto_symext::symex_assign_symbol(
419424
const ssa_exprt &lhs, // L1
420425
const exprt &full_lhs,
421426
const exprt &rhs,
422-
exprt::operandst &guard,
427+
const exprt::operandst &guard,
423428
assignment_typet assignment_type)
424429
{
425430
// Shortcut the common case of a whole-struct initializer:
@@ -452,16 +457,16 @@ void goto_symext::symex_assign_symbol(
452457

453458
do_simplify(assignment.rhs);
454459

455-
ssa_exprt &l1_lhs = assignment.lhs;
456-
ssa_exprt l2_lhs = state
457-
.assignment(
458-
assignment.lhs,
459-
assignment.rhs,
460-
ns,
461-
symex_config.simplify_opt,
462-
symex_config.constant_propagation,
463-
symex_config.allow_pointer_unsoundness)
464-
.get();
460+
const ssa_exprt &l1_lhs = assignment.lhs;
461+
const ssa_exprt l2_lhs = state
462+
.assignment(
463+
assignment.lhs,
464+
assignment.rhs,
465+
ns,
466+
symex_config.simplify_opt,
467+
symex_config.constant_propagation,
468+
symex_config.allow_pointer_unsoundness)
469+
.get();
465470

466471
exprt ssa_full_lhs = add_to_lhs(full_lhs, l2_lhs);
467472
state.record_events.push(false);
@@ -481,12 +486,12 @@ void goto_symext::symex_assign_symbol(
481486
<< messaget::eom;
482487
});
483488

484-
// Temporarily add the state guard
485-
guard.emplace_back(state.guard.as_expr());
489+
const exprt assignment_guard =
490+
make_and(state.guard.as_expr(), conjunction(guard));
486491

487492
const exprt original_lhs = get_original_name(l2_full_lhs);
488493
target.assignment(
489-
conjunction(guard),
494+
assignment_guard,
490495
l2_lhs,
491496
l2_full_lhs,
492497
original_lhs,
@@ -505,9 +510,6 @@ void goto_symext::symex_assign_symbol(
505510
state.propagation.erase_if_exists(l1_lhs.get_identifier());
506511
state.value_set.erase_symbol(l1_lhs, ns);
507512
}
508-
509-
// Restore the guard
510-
guard.pop_back();
511513
}
512514

513515
void goto_symext::symex_assign_typecast(
@@ -564,10 +566,8 @@ void goto_symext::symex_assign_array(
564566
// into
565567
// a'==a WITH [i:=e]
566568

567-
with_exprt new_rhs(lhs_array, lhs_index, rhs);
568-
new_rhs.type() = lhs_index_type;
569-
570-
exprt new_full_lhs=add_to_lhs(full_lhs, lhs);
569+
const with_exprt new_rhs{lhs_array, lhs_index, rhs};
570+
const exprt new_full_lhs = add_to_lhs(full_lhs, lhs);
571571

572572
symex_assign_rec(
573573
state, lhs_array, new_full_lhs, new_rhs, guard, assignment_type);

src/goto-symex/symex_target_equation.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,19 @@ void symex_target_equationt::assignment(
114114
{
115115
PRECONDITION(ssa_lhs.is_not_nil());
116116

117-
SSA_steps.emplace_back(source, goto_trace_stept::typet::ASSIGNMENT);
118-
SSA_stept &SSA_step=SSA_steps.back();
119-
120-
SSA_step.guard=guard;
121-
SSA_step.ssa_lhs=ssa_lhs;
122-
SSA_step.ssa_full_lhs=ssa_full_lhs;
123-
SSA_step.original_full_lhs=original_full_lhs;
124-
SSA_step.ssa_rhs=ssa_rhs;
125-
SSA_step.assignment_type=assignment_type;
126-
127-
SSA_step.cond_expr=equal_exprt(SSA_step.ssa_lhs, SSA_step.ssa_rhs);
128-
SSA_step.hidden=(assignment_type!=assignment_typet::STATE &&
129-
assignment_type!=assignment_typet::VISIBLE_ACTUAL_PARAMETER);
130-
131-
merge_ireps(SSA_step);
117+
SSA_steps.emplace_back(SSA_assignment_stept{
118+
source,
119+
guard,
120+
ssa_lhs,
121+
ssa_full_lhs,
122+
original_full_lhs,
123+
ssa_rhs,
124+
equal_exprt(ssa_lhs, ssa_rhs),
125+
assignment_type,
126+
assignment_type != assignment_typet::STATE &&
127+
assignment_type != assignment_typet::VISIBLE_ACTUAL_PARAMETER});
128+
129+
merge_ireps(SSA_steps.back());
132130
}
133131

134132
void symex_target_equationt::decl(

src/solvers/prop/bdd_expr.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,6 @@ bddt bdd_exprt::from_expr(const exprt &expr)
9191
return from_expr_rec(expr);
9292
}
9393

94-
/// Conjunction of two expressions. If the second is already an `and_exprt`
95-
/// add to its operands instead of creating a new expression.
96-
static exprt make_and(exprt a, exprt b)
97-
{
98-
if(b.id() == ID_and)
99-
{
100-
b.add_to_operands(std::move(a));
101-
return b;
102-
}
103-
return and_exprt{std::move(a), std::move(b)};
104-
}
105-
10694
/// Disjunction of two expressions. If the second is already an `or_exprt`
10795
/// add to its operands instead of creating a new expression.
10896
static exprt make_or(exprt a, exprt b)

src/util/expr_util.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,26 @@ constant_exprt make_boolean_expr(bool value)
289289
else
290290
return false_exprt();
291291
}
292+
293+
exprt make_and(exprt a, exprt b)
294+
{
295+
PRECONDITION(a.type().id() == ID_bool && b.type().id() == ID_bool);
296+
if(b.is_constant())
297+
{
298+
if(b.get(ID_value) == ID_false)
299+
return false_exprt{};
300+
return a;
301+
}
302+
if(a.is_constant())
303+
{
304+
if(a.get(ID_value) == ID_false)
305+
return false_exprt{};
306+
return b;
307+
}
308+
if(b.id() == ID_and)
309+
{
310+
b.add_to_operands(std::move(a));
311+
return b;
312+
}
313+
return and_exprt{std::move(a), std::move(b)};
314+
}

src/util/expr_util.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,9 @@ class is_constantt
100100
/// returns true_exprt if given true and false_exprt otherwise
101101
constant_exprt make_boolean_expr(bool);
102102

103+
/// Conjunction of two expressions. If the second is already an `and_exprt`
104+
/// add to its operands instead of creating a new expression. If one is `true`,
105+
/// return the other expression. If one is `false` returns `false`.
106+
exprt make_and(exprt a, exprt b);
107+
103108
#endif // CPROVER_UTIL_EXPR_UTIL_H

0 commit comments

Comments
 (0)