Skip to content

Commit 27fd210

Browse files
committed
Use generic simplify_expr instead of local simplification rules
1 parent 4d35274 commit 27fd210

File tree

1 file changed

+2
-78
lines changed

1 file changed

+2
-78
lines changed

src/goto-programs/json_goto_trace.cpp

Lines changed: 2 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -21,82 +21,6 @@ Author: Daniel Kroening
2121

2222
#include <langapi/language_util.h>
2323

24-
/// Replaces in src, expressions of the form pointer_offset(constant) by that
25-
/// constant.
26-
/// \param src: an expression
27-
void remove_pointer_offsets(exprt &src)
28-
{
29-
if(src.id()==ID_pointer_offset &&
30-
src.op0().id()==ID_constant &&
31-
src.op0().type().id()==ID_pointer)
32-
{
33-
std::string binary_str=id2string(to_constant_expr(src.op0()).get_value());
34-
// The constant address consists of OBJECT-ID || OFFSET.
35-
// Shift out the object-identifier bits, leaving only the offset:
36-
mp_integer offset=binary2integer(
37-
binary_str.substr(config.bv_encoding.object_bits), false);
38-
src=from_integer(offset, src.type());
39-
}
40-
else
41-
for(auto &op : src.operands())
42-
remove_pointer_offsets(op);
43-
}
44-
45-
/// Replaces in src, expressions of the form pointer_offset(array_symbol) by a
46-
/// constant value of 0. This is meant to simplify array expressions.
47-
/// \param src: an expression
48-
/// \param array_symbol: a symbol expression representing an array
49-
void remove_pointer_offsets(exprt &src, const symbol_exprt &array_symbol)
50-
{
51-
if(src.id()==ID_pointer_offset &&
52-
src.op0().id()==ID_constant &&
53-
src.op0().op0().id()==ID_address_of &&
54-
src.op0().op0().op0().id()==ID_index)
55-
{
56-
const index_exprt &idx=to_index_expr(src.op0().op0().op0());
57-
const irep_idt &array_id=to_symbol_expr(idx.array()).get_identifier();
58-
if(idx.array().id()==ID_symbol &&
59-
array_id==array_symbol.get_identifier() &&
60-
to_constant_expr(idx.index()).value_is_zero_string())
61-
src=from_integer(0, src.type());
62-
}
63-
else
64-
for(auto &op : src.operands())
65-
remove_pointer_offsets(op, array_symbol);
66-
}
67-
68-
/// Simplify an expression before putting it in the json format
69-
/// \param src: an expression potentialy containing array accesses (index_expr)
70-
/// \return an expression similar in meaning to src but where array accesses
71-
/// have been simplified
72-
exprt simplify_array_access(const exprt &src, const namespacet &ns)
73-
{
74-
if(src.id()==ID_index && to_index_expr(src).array().id()==ID_symbol)
75-
{
76-
// Case where the array is a symbol.
77-
const symbol_exprt &array_symbol=to_symbol_expr(to_index_expr(src).array());
78-
exprt simplified_index=to_index_expr(src).index();
79-
// We remove potential appearances of `pointer_offset(array_symbol)`
80-
remove_pointer_offsets(simplified_index, array_symbol);
81-
simplified_index=simplify_expr(simplified_index, ns);
82-
return index_exprt(array_symbol, simplified_index);
83-
}
84-
else if(src.id()==ID_index && to_index_expr(src).array().id()==ID_array)
85-
{
86-
// Case where the array is given by an array of expressions
87-
exprt index=to_index_expr(src).index();
88-
remove_pointer_offsets(index);
89-
90-
// We look for an actual integer value for the index
91-
index=simplify_expr(index, ns);
92-
unsigned i;
93-
if(index.id()==ID_constant &&
94-
!to_unsigned_integer(to_constant_expr(index), i))
95-
return to_index_expr(src).array().operands()[i];
96-
}
97-
return src;
98-
}
99-
10024
/// Produce a json representation of a trace.
10125
/// \param ns: a namespace
10226
/// \param goto_trace: a trace in a goto program
@@ -169,7 +93,7 @@ void convert(
16993
DATA_INVARIANT(
17094
step.full_lhs.is_not_nil(),
17195
"full_lhs in assignment must not be nil");
172-
exprt simplified=simplify_array_access(step.full_lhs, ns);
96+
exprt simplified=simplify_expr(step.full_lhs, ns);
17397
full_lhs_string=from_expr(ns, identifier, simplified);
17498

17599
const symbolt *symbol;
@@ -183,7 +107,7 @@ void convert(
183107
type_string=from_type(ns, identifier, symbol->type);
184108

185109
json_assignment["mode"]=json_stringt(id2string(symbol->mode));
186-
exprt simplified=simplify_array_access(step.full_lhs_value, ns);
110+
exprt simplified=simplify_expr(step.full_lhs_value, ns);
187111

188112
full_lhs_value=json(simplified, ns, symbol->mode);
189113
}

0 commit comments

Comments
 (0)