-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathguard_expr.cpp
213 lines (176 loc) · 4.31 KB
/
guard_expr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*******************************************************************\
Module: Symbolic Execution
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// Symbolic Execution
#include "guard_expr.h"
#include <util/expr_util.h>
#include <util/invariant.h>
#include <util/std_expr.h>
exprt guard_exprt::guard_expr(exprt expr) const
{
if(is_true())
{
// do nothing
return expr;
}
else
{
if(expr.is_constant() && to_constant_expr(expr).is_false())
{
return boolean_negate(as_expr());
}
else
{
return implies_exprt{as_expr(), expr};
}
}
}
void guard_exprt::add(const exprt &expr)
{
PRECONDITION(expr.is_boolean());
if(is_false() || (expr.is_constant() && to_constant_expr(expr).is_true()))
return;
else if(
is_true() || (expr.is_constant() && to_constant_expr(expr).is_false()))
{
this->expr = expr;
return;
}
else if(this->expr.id() != ID_and)
{
and_exprt a({this->expr});
this->expr = a;
}
exprt::operandst &op = this->expr.operands();
if(expr.id() == ID_and)
op.insert(op.end(), expr.operands().begin(), expr.operands().end());
else
op.push_back(expr);
}
guard_exprt &operator-=(guard_exprt &g1, const guard_exprt &g2)
{
if(g1.expr.id() != ID_and)
{
if(g2.expr.id() != ID_and)
{
if(g1.expr == g2.expr)
g1.expr = true_exprt{};
}
else
{
for(const auto &op : g2.expr.operands())
{
if(g1.expr == op)
{
g1.expr = true_exprt{};
break;
}
}
}
return g1;
}
if(g2.expr.id() != ID_and)
{
exprt::operandst &op1 = g1.expr.operands();
for(exprt::operandst::iterator it = op1.begin(); it != op1.end(); ++it)
{
if(g1.expr == g2.expr)
{
op1.erase(it);
break;
}
}
return g1;
}
exprt g2_sorted = g2.as_expr();
exprt::operandst &op1 = g1.expr.operands();
const exprt::operandst &op2 = g2_sorted.operands();
exprt::operandst::iterator it1 = op1.begin();
for(exprt::operandst::const_iterator it2 = op2.begin(); it2 != op2.end();
++it2)
{
if(it1 != op1.end() && *it1 == *it2)
it1 = op1.erase(it1);
else
break;
}
g1.expr = conjunction(op1);
return g1;
}
bool guard_exprt::disjunction_may_simplify(const guard_exprt &other_guard)
{
if(is_true() || is_false() || other_guard.is_true() || other_guard.is_false())
return true;
if(expr.id() == ID_and && other_guard.expr.id() == ID_and)
return true;
if(other_guard.expr == boolean_negate(expr))
return true;
return false;
}
guard_exprt &operator|=(guard_exprt &g1, const guard_exprt &g2)
{
if(g2.is_false() || g1.is_true())
return g1;
if(g1.is_false() || g2.is_true())
{
g1.expr = g2.expr;
return g1;
}
if(g1.expr.id() != ID_and || g2.expr.id() != ID_and)
{
exprt tmp(boolean_negate(g2.as_expr()));
if(tmp == g1.as_expr())
g1.expr = true_exprt();
else
g1.expr = or_exprt(g1.as_expr(), g2.as_expr());
// TODO: make simplify more capable and apply here
return g1;
}
// find common prefix
exprt g2_sorted = g2.as_expr();
exprt::operandst &op1 = g1.expr.operands();
const exprt::operandst &op2 = g2_sorted.operands();
exprt::operandst n_op1, n_op2;
n_op1.reserve(op1.size());
n_op2.reserve(op2.size());
exprt::operandst::iterator it1 = op1.begin();
for(exprt::operandst::const_iterator it2 = op2.begin(); it2 != op2.end();
++it2)
{
while(it1 != op1.end() && *it1 < *it2)
{
n_op1.push_back(*it1);
it1 = op1.erase(it1);
}
if(it1 != op1.end() && *it1 == *it2)
++it1;
else
n_op2.push_back(*it2);
}
while(it1 != op1.end())
{
n_op1.push_back(*it1);
it1 = op1.erase(it1);
}
if(n_op2.empty())
return g1;
// end of common prefix
exprt and_expr1 = conjunction(n_op1);
exprt and_expr2 = conjunction(n_op2);
g1.expr = conjunction(op1);
exprt tmp(boolean_negate(and_expr2));
if(tmp != and_expr1)
{
if(
(and_expr1.is_constant() && to_constant_expr(and_expr1).is_true()) ||
(and_expr2.is_constant() && to_constant_expr(and_expr2).is_true()))
{
}
else
// TODO: make simplify more capable and apply here
g1.add(or_exprt(and_expr1, and_expr2));
}
return g1;
}