Static Value-Flow Analysis
Loading...
Searching...
No Matches
Z3Expr.h
Go to the documentation of this file.
1//===- Z3Expr.h -- Z3 conditions----------------------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-2017> <Yulei Sui>
6//
7
8// This program is free software: you can redistribute it and/or modify
9// it under the terms of the GNU Affero General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU Affero General Public License for more details.
17
18// You should have received a copy of the GNU Affero General Public License
19// along with this program. If not, see <http://www.gnu.org/licenses/>.
20//
21//===----------------------------------------------------------------------===//
22/*
23 * Z3Expr.h
24 *
25 * Created on: April 29, 2022
26 * Author: Xiao
27 */
28
29#ifndef Z3_EXAMPLE_Z3EXPR_H
30#define Z3_EXAMPLE_Z3EXPR_H
31
32#include "z3++.h"
33#include "SVFIR/SVFType.h"
34
35namespace SVF
36{
37
38class Z3Expr
39{
40public:
41 static z3::context *ctx;
42 static z3::solver* solver;
43
44private:
45 z3::expr e;
46
47
48
49public:
50
52 {
53 }
54
55 Z3Expr(const z3::expr &_e) : e(_e)
56 {
57 }
58
60 {
61 }
62
64 {
65 }
66
67 Z3Expr(float f) : Z3Expr((double) f)
68 {
69 }
70
72 {
73 }
74
75 virtual ~Z3Expr() = default;
76
77 inline Z3Expr &operator=(const Z3Expr &rhs)
78 {
79 if (this->id() != rhs.id())
80 {
81 e = rhs.getExpr();
82 }
83 return *this;
84 }
85
86 const z3::expr &getExpr() const
87 {
88 return e;
89 }
90
92 static z3::solver &getSolver();
93
95 static z3::context &getContext();
96
98 static void releaseContext();
99
101 static void releaseSolver();
102
103
105 static z3::expr nullExpr()
106 {
107 return getContext().int_const("null");
108 }
109
111 inline u32_t id() const
112 {
113 return e.id();
114 }
115
117 inline u32_t hash() const
118 {
119 return e.hash();
120 }
121
122 inline const std::string to_string() const
123 {
124 return e.to_string();
125 }
126
127 inline bool is_numeral() const
128 {
129 return e.is_numeral();
130 }
131
132 inline bool is_bool() const
133 {
134 return e.is_bool();
135 }
136
137 inline Z3Expr simplify() const
138 {
139 return e.simplify();
140 }
141
143 {
144 return e.get_numeral_int64();
145 }
146
147 inline int get_numeral_int() const
148 {
149 return e.get_numeral_int();
150 }
151
152 //{% reload operator
153 friend Z3Expr operator==(const Z3Expr &lhs, const Z3Expr &rhs)
154 {
155 return lhs.getExpr() == rhs.getExpr();
156 }
157
158 friend Z3Expr operator!=(const Z3Expr &lhs, const Z3Expr &rhs)
159 {
160 return lhs.getExpr() != rhs.getExpr();
161 }
162
163 friend Z3Expr operator>(const Z3Expr &lhs, const Z3Expr &rhs)
164 {
165 return lhs.getExpr() > rhs.getExpr();
166 }
167
168 friend Z3Expr operator<(const Z3Expr &lhs, const Z3Expr &rhs)
169 {
170 return lhs.getExpr() < rhs.getExpr();
171 }
172
173 friend Z3Expr operator<=(const Z3Expr &lhs, const Z3Expr &rhs)
174 {
175 return lhs.getExpr() <= rhs.getExpr();
176 }
177
178 friend Z3Expr operator>=(const Z3Expr &lhs, const Z3Expr &rhs)
179 {
180 return lhs.getExpr() >= rhs.getExpr();
181 }
182
183 friend Z3Expr operator+(const Z3Expr &lhs, const Z3Expr &rhs)
184 {
185 return lhs.getExpr() + rhs.getExpr();
186 }
187
188 friend Z3Expr operator-(const Z3Expr &lhs, const Z3Expr &rhs)
189 {
190 return lhs.getExpr() - rhs.getExpr();
191 }
192
193 friend Z3Expr operator*(const Z3Expr &lhs, const Z3Expr &rhs)
194 {
195 return lhs.getExpr() * rhs.getExpr();
196 }
197
198 friend Z3Expr operator/(const Z3Expr &lhs, const Z3Expr &rhs)
199 {
200 return lhs.getExpr() / rhs.getExpr();
201 }
202
203 friend Z3Expr operator%(const Z3Expr &lhs, const Z3Expr &rhs)
204 {
205 return lhs.getExpr() % rhs.getExpr();
206 }
207
208 friend Z3Expr operator^(const Z3Expr &lhs, const Z3Expr &rhs)
209 {
210 return lhs.getExpr() ^ rhs.getExpr();
211 }
212
213 friend Z3Expr operator&(const Z3Expr &lhs, const Z3Expr &rhs)
214 {
215 return lhs.getExpr() & rhs.getExpr();
216 }
217
218 friend Z3Expr operator|(const Z3Expr &lhs, const Z3Expr &rhs)
219 {
220 return lhs.getExpr() | rhs.getExpr();
221 }
222
223 friend Z3Expr ashr(const Z3Expr &lhs, const Z3Expr &rhs)
224 {
225 return ashr(lhs.getExpr(), rhs.getExpr());
226 }
227
228 friend Z3Expr shl(const Z3Expr &lhs, const Z3Expr &rhs)
229 {
230 return shl(lhs.getExpr(), rhs.getExpr());
231 }
232
233 friend Z3Expr int2bv(u32_t n, const Z3Expr &e)
234 {
235 return int2bv(n, e.getExpr());
236 }
237
238 friend Z3Expr bv2int(const Z3Expr &e, bool isSigned)
239 {
240 return z3::bv2int(e.getExpr(), isSigned);
241 }
242
243 friend Z3Expr operator&&(const Z3Expr &lhs, const Z3Expr &rhs)
244 {
245 return lhs.getExpr() && rhs.getExpr();
246 }
247
248 friend Z3Expr operator||(const Z3Expr &lhs, const Z3Expr &rhs)
249 {
250 return lhs.getExpr() || rhs.getExpr();
251 }
252
253 friend Z3Expr operator!(const Z3Expr &lhs)
254 {
255 return !lhs.getExpr();
256 }
257
258 friend Z3Expr ite(const Z3Expr &cond, const Z3Expr &lhs, const Z3Expr &rhs)
259 {
260 return ite(cond.getExpr(), lhs.getExpr(), rhs.getExpr());
261 }
262
263 friend std::ostream &operator<<(std::ostream &out, const Z3Expr &expr)
264 {
265 out << expr.getExpr();
266 return out;
267 }
268
269 friend bool eq(const Z3Expr &lhs, const Z3Expr &rhs)
270 {
271 return eq(lhs.getExpr().simplify(), rhs.getExpr().simplify());
272 }
273
274 z3::sort get_sort() const
275 {
276 return getExpr().get_sort();
277 }
278 //%}
279
281 static std::string dumpStr(const Z3Expr &z3Expr);
282
283
285 static u32_t getExprSize(const Z3Expr &z3Expr);
286
288 static inline Z3Expr getTrueCond()
289 {
290 return getContext().bool_val(true);
291 }
292
293
295 static inline Z3Expr getFalseCond()
296 {
297 return getContext().bool_val(false);
298 }
299
301 static inline Z3Expr NEG(const Z3Expr &z3Expr)
302 {
303 return (!z3Expr).simplify();
304 }
305
307 static Z3Expr AND(const Z3Expr &lhs, const Z3Expr &rhs);
308
310 static Z3Expr OR(const Z3Expr &lhs, const Z3Expr &rhs);
311};
312} // End namespace SVF
313
315template<>
316struct std::hash<SVF::Z3Expr>
317{
318 size_t operator()(const SVF::Z3Expr &z3Expr) const
319 {
320 return z3Expr.id();
321 }
322};
323
324#endif //Z3_EXAMPLE_Z3EXPR_H
325
cJSON * n
Definition cJSON.cpp:2558
Z3Expr(const Z3Expr &z3Expr)
Definition Z3Expr.h:63
const z3::expr & getExpr() const
Definition Z3Expr.h:86
static z3::solver & getSolver()
Get z3 solver, singleton design here to make sure we only have one context.
Definition Z3Expr.cpp:56
friend Z3Expr operator!=(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:158
const std::string to_string() const
Definition Z3Expr.h:122
friend Z3Expr operator|(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:218
friend Z3Expr operator>(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:163
friend Z3Expr operator%(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:203
static z3::solver * solver
Definition Z3Expr.h:42
friend Z3Expr operator||(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:248
friend Z3Expr bv2int(const Z3Expr &e, bool isSigned)
Definition Z3Expr.h:238
static Z3Expr AND(const Z3Expr &lhs, const Z3Expr &rhs)
compute AND, used for branch condition
Definition Z3Expr.cpp:92
Z3Expr(double f)
Definition Z3Expr.h:71
static std::string dumpStr(const Z3Expr &z3Expr)
output Z3 expression as a string
Definition Z3Expr.cpp:168
friend Z3Expr ashr(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:223
u32_t hash() const
get hash id
Definition Z3Expr.h:117
friend Z3Expr operator&(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:213
friend Z3Expr operator*(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:193
Z3Expr(const z3::expr &_e)
Definition Z3Expr.h:55
friend Z3Expr operator>=(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:178
Z3Expr(float f)
Definition Z3Expr.h:67
friend Z3Expr shl(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:228
static u32_t getExprSize(const Z3Expr &z3Expr)
get the number of subexpression of a Z3 expression
Definition Z3Expr.cpp:76
Z3Expr(int i)
Definition Z3Expr.h:59
z3::sort get_sort() const
Definition Z3Expr.h:274
friend Z3Expr operator==(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:153
friend Z3Expr ite(const Z3Expr &cond, const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:258
friend Z3Expr operator/(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:198
friend Z3Expr operator<=(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:173
static Z3Expr NEG(const Z3Expr &z3Expr)
compute NEG
Definition Z3Expr.h:301
static z3::expr nullExpr()
null expression
Definition Z3Expr.h:105
friend Z3Expr operator<(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:168
static void releaseSolver()
release z3 solver
Definition Z3Expr.cpp:49
static void releaseContext()
release z3 context
Definition Z3Expr.cpp:40
bool is_bool() const
Definition Z3Expr.h:132
int get_numeral_int() const
Definition Z3Expr.h:147
static Z3Expr getTrueCond()
Return the unique true condition.
Definition Z3Expr.h:288
virtual ~Z3Expr()=default
static Z3Expr OR(const Z3Expr &lhs, const Z3Expr &rhs)
compute OR, used for branch condition
Definition Z3Expr.cpp:130
Z3Expr & operator=(const Z3Expr &rhs)
Definition Z3Expr.h:77
friend Z3Expr operator!(const Z3Expr &lhs)
Definition Z3Expr.h:253
friend bool eq(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:269
friend Z3Expr int2bv(u32_t n, const Z3Expr &e)
Definition Z3Expr.h:233
u32_t id() const
get id
Definition Z3Expr.h:111
static z3::context & getContext()
Get z3 context, singleton design here to make sure we only have one context.
Definition Z3Expr.cpp:66
bool is_numeral() const
Definition Z3Expr.h:127
int64_t get_numeral_int64() const
Definition Z3Expr.h:142
friend Z3Expr operator&&(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:243
z3::expr e
Definition Z3Expr.h:45
friend Z3Expr operator^(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:208
friend Z3Expr operator-(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:188
static z3::context * ctx
Definition Z3Expr.h:41
static Z3Expr getFalseCond()
Return the unique false condition.
Definition Z3Expr.h:295
friend Z3Expr operator+(const Z3Expr &lhs, const Z3Expr &rhs)
Definition Z3Expr.h:183
friend std::ostream & operator<<(std::ostream &out, const Z3Expr &expr)
Definition Z3Expr.h:263
Z3Expr simplify() const
Definition Z3Expr.h:137
for isBitcode
Definition BasicTypes.h:68
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
unsigned u32_t
Definition GeneralType.h:46
size_t operator()(const SVF::Z3Expr &z3Expr) const
Definition Z3Expr.h:318