Skip to content

Commit f6890b3

Browse files
author
Daniel Kroening
committed
modernisation of sum_expr and mul_expr
1 parent 4d87ba1 commit f6890b3

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

src/util/simplify_expr_int.cpp

+34-33
Original file line numberDiff line numberDiff line change
@@ -54,47 +54,47 @@ bool simplify_exprt::simplify_bswap(bswap_exprt &expr)
5454
return true;
5555
}
5656

57-
static bool sum_expr(exprt &dest, const exprt &expr)
57+
//! produce a sum of two constant expressions of the same type
58+
//! \return 'false' iff this was successful
59+
static bool sum_expr(
60+
constant_exprt &dest,
61+
const constant_exprt &expr)
5862
{
59-
if(!dest.is_constant() || !expr.is_constant())
60-
return true;
61-
6263
if(dest.type()!=expr.type())
6364
return true;
6465

6566
const irep_idt &type_id=dest.type().id();
6667

6768
if(type_id==ID_integer || type_id==ID_natural)
6869
{
69-
dest.set(ID_value, integer2string(
70-
string2integer(dest.get_string(ID_value))+
71-
string2integer(expr.get_string(ID_value))));
70+
dest.set_value(integer2string(
71+
string2integer(id2string(dest.get_value()))+
72+
string2integer(id2string(expr.get_value()))));
7273
return false;
7374
}
7475
else if(type_id==ID_rational)
7576
{
7677
rationalt a, b;
7778
if(!to_rational(dest, a) && !to_rational(expr, b))
7879
{
79-
exprt a_plus_b=from_rational(a+b);
80-
dest.set(ID_value, a_plus_b.get_string(ID_value));
80+
dest=from_rational(a+b);
8181
return false;
8282
}
8383
}
8484
else if(type_id==ID_unsignedbv || type_id==ID_signedbv)
8585
{
86-
dest.set(ID_value, integer2binary(
87-
binary2integer(dest.get_string(ID_value), false)+
88-
binary2integer(expr.get_string(ID_value), false),
89-
unsafe_string2unsigned(dest.type().get_string(ID_width))));
86+
dest.set_value(integer2binary(
87+
binary2integer(id2string(dest.get_value()), false)+
88+
binary2integer(id2string(expr.get_value()), false),
89+
to_bitvector_type(dest.type()).get_width()));
9090
return false;
9191
}
9292
else if(type_id==ID_fixedbv)
9393
{
94-
dest.set(ID_value, integer2binary(
95-
binary2integer(dest.get_string(ID_value), false)+
96-
binary2integer(expr.get_string(ID_value), false),
97-
unsafe_string2unsigned(dest.type().get_string(ID_width))));
94+
dest.set_value(integer2binary(
95+
binary2integer(id2string(dest.get_value()), false)+
96+
binary2integer(id2string(expr.get_value()), false),
97+
to_bitvector_type(dest.type()).get_width()));
9898
return false;
9999
}
100100
else if(type_id==ID_floatbv)
@@ -108,40 +108,40 @@ static bool sum_expr(exprt &dest, const exprt &expr)
108108
return true;
109109
}
110110

111-
static bool mul_expr(exprt &dest, const exprt &expr)
111+
//! produce a product of two expressions of the same type
112+
//! \return 'false' iff this was successful
113+
static bool mul_expr(
114+
constant_exprt &dest,
115+
const constant_exprt &expr)
112116
{
113-
if(!dest.is_constant() || !expr.is_constant())
114-
return true;
115-
116117
if(dest.type()!=expr.type())
117118
return true;
118119

119120
const irep_idt &type_id=dest.type().id();
120121

121122
if(type_id==ID_integer || type_id==ID_natural)
122123
{
123-
dest.set(ID_value, integer2string(
124-
string2integer(dest.get_string(ID_value))*
125-
string2integer(expr.get_string(ID_value))));
124+
dest.set_value(integer2string(
125+
string2integer(id2string(dest.get_value()))*
126+
string2integer(id2string(expr.get_value()))));
126127
return false;
127128
}
128129
else if(type_id==ID_rational)
129130
{
130131
rationalt a, b;
131132
if(!to_rational(dest, a) && !to_rational(expr, b))
132133
{
133-
exprt a_mul_b=from_rational(a*b);
134-
dest.set(ID_value, a_mul_b.get_string(ID_value));
134+
dest=from_rational(a*b);
135135
return false;
136136
}
137137
}
138138
else if(type_id==ID_unsignedbv || type_id==ID_signedbv)
139139
{
140140
// the following works for signed and unsigned integers
141-
dest.set(ID_value, integer2binary(
142-
binary2integer(dest.get_string(ID_value), false)*
143-
binary2integer(expr.get_string(ID_value), false),
144-
unsafe_string2unsigned(dest.type().get_string(ID_width))));
141+
dest.set_value(integer2binary(
142+
binary2integer(id2string(dest.get_value()), false)*
143+
binary2integer(id2string(expr.get_value()), false),
144+
to_bitvector_type(dest.type()).get_width()));
145145
return false;
146146
}
147147
else if(type_id==ID_fixedbv)
@@ -213,7 +213,7 @@ bool simplify_exprt::simplify_mult(exprt &expr)
213213
if(found)
214214
{
215215
// update the constant factor
216-
if(!mul_expr(*constant, *it))
216+
if(!mul_expr(to_constant_expr(*constant), to_constant_expr(*it)))
217217
do_erase=true;
218218
}
219219
else
@@ -465,7 +465,7 @@ bool simplify_exprt::simplify_plus(exprt &expr)
465465
it->is_constant() &&
466466
next->is_constant())
467467
{
468-
sum_expr(*it, *next);
468+
sum_expr(to_constant_expr(*it), to_constant_expr(*next));
469469
operands.erase(next);
470470
}
471471
}
@@ -517,7 +517,8 @@ bool simplify_exprt::simplify_plus(exprt &expr)
517517
}
518518
else
519519
{
520-
if(!sum_expr(*const_sum, *it))
520+
if(!sum_expr(to_constant_expr(*const_sum),
521+
to_constant_expr(*it)))
521522
{
522523
*it=from_integer(0, it->type());
523524
assert(it->is_not_nil());

0 commit comments

Comments
 (0)