Skip to content

Commit f6c4867

Browse files
authored
Merge pull request #4642 from romainbrenguier/refactor/symex_assign
Refactoring of symex_assign_symbol
2 parents 517c783 + fdc03e0 commit f6c4867

13 files changed

+131
-113
lines changed

src/goto-symex/field_sensitivity.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,15 @@ void field_sensitivityt::field_assignments_rec(
222222
exprt ssa_rhs = state.rename(lhs, ns).get();
223223
simplify(ssa_rhs, ns);
224224

225-
ssa_exprt ssa_lhs = to_ssa_expr(lhs_fs);
226-
state.assignment(
227-
ssa_lhs, ssa_rhs, ns, true, true, allow_pointer_unsoundness);
225+
const ssa_exprt ssa_lhs = state
226+
.assignment(
227+
to_ssa_expr(lhs_fs),
228+
ssa_rhs,
229+
ns,
230+
true,
231+
true,
232+
allow_pointer_unsoundness)
233+
.get();
228234

229235
// do the assignment
230236
target.assignment(

src/goto-symex/goto_symex_state.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ goto_symex_statet::goto_symex_statet(
3838
source(_source),
3939
guard_manager(manager),
4040
symex_target(nullptr),
41-
record_events(true),
41+
record_events({true}),
4242
fresh_l2_name_provider(fresh_l2_name_provider)
4343
{
4444
threads.emplace_back(guard_manager);
@@ -47,7 +47,8 @@ goto_symex_statet::goto_symex_statet(
4747

4848
goto_symex_statet::~goto_symex_statet()=default;
4949

50-
/// write to a variable
50+
/// Check that \p expr is correctly renamed to level 2 and return true in case
51+
/// an error is detected.
5152
static bool check_renaming(const exprt &expr);
5253

5354
static bool check_renaming(const typet &type)
@@ -151,9 +152,9 @@ goto_symex_statet::set_indices<L2>(ssa_exprt ssa_expr, const namespacet &ns)
151152
return level2(level1(level0(std::move(ssa_expr), ns, source.thread_nr)));
152153
}
153154

154-
void goto_symex_statet::assignment(
155-
ssa_exprt &lhs, // L0/L1
156-
const exprt &rhs, // L2
155+
renamedt<ssa_exprt, L2> goto_symex_statet::assignment(
156+
ssa_exprt lhs, // L0/L1
157+
const exprt &rhs, // L2
157158
const namespacet &ns,
158159
bool rhs_is_simplified,
159160
bool record_value,
@@ -182,7 +183,8 @@ void goto_symex_statet::assignment(
182183

183184
// do the l2 renaming
184185
increase_generation(l1_identifier, lhs);
185-
lhs = set_indices<L2>(std::move(lhs), ns).get();
186+
renamedt<ssa_exprt, L2> l2_lhs = set_indices<L2>(std::move(lhs), ns);
187+
lhs = l2_lhs.get();
186188

187189
// in case we happen to be multi-threaded, record the memory access
188190
bool is_shared=l2_thread_write_encoding(lhs, ns);
@@ -233,6 +235,8 @@ void goto_symex_statet::assignment(
233235
value_set.output(ns, std::cout);
234236
std::cout << "**********************\n";
235237
#endif
238+
239+
return l2_lhs;
236240
}
237241

238242
template <levelt level>
@@ -442,21 +446,21 @@ bool goto_symex_statet::l2_thread_read_encoding(
442446
else
443447
tmp = if_exprt{cond.as_expr(), l2_true_case.get(), l2_false_case.get()};
444448

445-
const bool record_events_bak=record_events;
446-
record_events=false;
447-
assignment(ssa_l1, tmp, ns, true, true);
448-
record_events=record_events_bak;
449+
record_events.push(false);
450+
ssa_exprt ssa_l2 = assignment(std::move(ssa_l1), tmp, ns, true, true).get();
451+
record_events.pop();
449452

450453
symex_target->assignment(
451454
guard_as_expr,
452-
ssa_l1,
453-
ssa_l1,
454-
ssa_l1.get_original_expr(),
455+
ssa_l2,
456+
ssa_l2,
457+
ssa_l2.get_original_expr(),
455458
tmp,
456459
source,
457460
symex_targett::assignment_typet::PHI);
458461

459-
expr = set_indices<L2>(std::move(ssa_l1), ns).get();
462+
INVARIANT(!check_renaming(ssa_l2), "expr should be renamed to L2");
463+
expr = std::move(ssa_l2);
460464

461465
a_s_read.second.push_back(guard);
462466
if(!no_write.op().is_false())
@@ -466,7 +470,7 @@ bool goto_symex_statet::l2_thread_read_encoding(
466470
}
467471

468472
// No event and no fresh index, but avoid constant propagation
469-
if(!record_events)
473+
if(!record_events.top())
470474
{
471475
expr = set_indices<L2>(std::move(ssa_l1), ns).get();
472476
return true;
@@ -488,7 +492,7 @@ goto_symex_statet::write_is_shared_resultt goto_symex_statet::write_is_shared(
488492
const ssa_exprt &expr,
489493
const namespacet &ns) const
490494
{
491-
if(!record_events)
495+
if(!record_events.top())
492496
return write_is_shared_resultt::NOT_SHARED;
493497

494498
PRECONDITION(dirty != nullptr);

src/goto-symex/goto_symex_state.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,14 @@ class goto_symex_statet final : public goto_statet
108108
template <levelt level = L2>
109109
void rename(typet &type, const irep_idt &l1_identifier, const namespacet &ns);
110110

111-
void assignment(
112-
ssa_exprt &lhs, // L0/L1
113-
const exprt &rhs, // L2
111+
/// \return lhs renamed to level 2
112+
renamedt<ssa_exprt, L2> assignment(
113+
ssa_exprt lhs, // L0/L1
114+
const exprt &rhs, // L2
114115
const namespacet &ns,
115116
bool rhs_is_simplified,
116117
bool record_value,
117-
bool allow_pointer_unsoundness=false);
118+
bool allow_pointer_unsoundness = false);
118119

119120
field_sensitivityt field_sensitivity;
120121

@@ -197,7 +198,7 @@ class goto_symex_statet final : public goto_statet
197198
write_is_shared_resultt
198199
write_is_shared(const ssa_exprt &expr, const namespacet &ns) const;
199200

200-
bool record_events;
201+
std::stack<bool> record_events;
201202

202203
const incremental_dirtyt *dirty = nullptr;
203204

src/goto-symex/postcondition.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ void postconditiont::strengthen(exprt &dest)
130130
SSA_step.ssa_lhs.type().id()==ID_struct)
131131
return;
132132

133-
equal_exprt equality(SSA_step.ssa_lhs, SSA_step.ssa_rhs);
134-
get_original_name(equality);
133+
exprt equality =
134+
get_original_name(equal_exprt{SSA_step.ssa_lhs, SSA_step.ssa_rhs});
135135

136136
if(dest.is_true())
137137
dest.swap(equality);
@@ -169,8 +169,7 @@ bool postconditiont::is_used(
169169
it!=expr_set.end();
170170
it++)
171171
{
172-
exprt tmp(*it);
173-
get_original_name(tmp);
172+
const exprt tmp = get_original_name(*it);
174173
find_symbols(tmp, symbols);
175174
}
176175

src/goto-symex/precondition.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ void preconditiont::compute_rec(exprt &dest)
140140
}
141141
else if(dest==SSA_step.ssa_lhs.get_original_expr())
142142
{
143-
dest=SSA_step.ssa_rhs;
144-
get_original_name(dest);
143+
dest = get_original_name(SSA_step.ssa_rhs);
145144
}
146145
else
147146
Forall_operands(it, dest)

src/goto-symex/renaming_level.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,37 +97,42 @@ void symex_level1t::restore_from(const current_namest &other)
9797
}
9898
}
9999

100-
void get_original_name(exprt &expr)
100+
exprt get_original_name(exprt expr)
101101
{
102-
get_original_name(expr.type());
102+
expr.type() = get_original_name(std::move(expr.type()));
103103

104104
if(expr.id() == ID_symbol && expr.get_bool(ID_C_SSA_symbol))
105-
expr = to_ssa_expr(expr).get_original_expr();
105+
return to_ssa_expr(expr).get_original_expr();
106106
else
107+
{
107108
Forall_operands(it, expr)
108-
get_original_name(*it);
109+
*it = get_original_name(std::move(*it));
110+
return expr;
111+
}
109112
}
110113

111-
void get_original_name(typet &type)
114+
typet get_original_name(typet type)
112115
{
113116
// rename all the symbols with their last known value
114117

115118
if(type.id() == ID_array)
116119
{
117120
auto &array_type = to_array_type(type);
118-
get_original_name(array_type.subtype());
119-
get_original_name(array_type.size());
121+
array_type.subtype() = get_original_name(std::move(array_type.subtype()));
122+
array_type.size() = get_original_name(std::move(array_type.size()));
120123
}
121124
else if(type.id() == ID_struct || type.id() == ID_union)
122125
{
123126
struct_union_typet &s_u_type = to_struct_union_type(type);
124127
struct_union_typet::componentst &components = s_u_type.components();
125128

126129
for(auto &component : components)
127-
get_original_name(component.type());
130+
component.type() = get_original_name(std::move(component.type()));
128131
}
129132
else if(type.id() == ID_pointer)
130133
{
131-
get_original_name(to_pointer_type(type).subtype());
134+
type.subtype() =
135+
get_original_name(std::move(to_pointer_type(type).subtype()));
132136
}
137+
return type;
133138
}

src/goto-symex/renaming_level.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ struct symex_level2t : public symex_renaming_levelt
148148
};
149149

150150
/// Undo all levels of renaming
151-
void get_original_name(exprt &expr);
151+
exprt get_original_name(exprt expr);
152152

153153
/// Undo all levels of renaming
154-
void get_original_name(typet &type);
154+
typet get_original_name(typet type);
155155

156156
#endif // CPROVER_GOTO_SYMEX_RENAMING_LEVEL_H

0 commit comments

Comments
 (0)