-
Notifications
You must be signed in to change notification settings - Fork 22
/
common.py
346 lines (266 loc) · 7.07 KB
/
common.py
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# Copyright 2014-2015 The Alive authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from z3 import *
gbl_unique_id = 0
def mk_unique_id():
global gbl_unique_id
id = str(gbl_unique_id)
gbl_unique_id += 1
return id
def fold_ite_list(l):
if len(l) == 0:
return None
cond, val = l[0]
if len(l) == 1:
return val
return If(cond, val, fold_ite_list(l[1:]))
def freshBV(prefix, size):
return BitVec('%s_%s' % (prefix, mk_unique_id()), size)
def mk_and(l):
l = [e for e in l if not is_true(e)]
if len(l) == 0:
return BoolVal(True)
if len(l) == 1:
return l[0]
return And(l)
def mk_or(l):
l = [e for e in l if not is_false(e)]
if len(l) == 0:
return BoolVal(False)
if len(l) == 1:
return l[0]
return Or(l)
def mk_not(e):
if is_false(e):
return BoolVal(True)
if is_true(e):
return BoolVal(False)
return Not(e)
def mk_distinct(l):
if len(l) < 2:
return BoolVal(True)
return Distinct(l)
def mk_if(c, a, b):
if is_true(c):
return a
if is_false(c):
return b
return If(c, a, b)
def mk_implies(a, b):
if is_true(a):
return b
if is_false(a) or is_true(b):
return BoolVal(True)
if is_false(b):
return Not(a)
return Implies(a, b)
def mk_concat(l):
if len(l) == 1:
return l[0]
return Concat(l)
def mk_forall(l, f):
if l == []:
return f
return ForAll(l, f)
def mk_exists(l, f):
if l == []:
return f
return Exists(l, f)
def toBV(b):
return If(b, BitVecVal(1, 1), BitVecVal(0, 1))
def truncateOrZExt(src, tgt):
srcb = src.size()
if isinstance(tgt, int):
tgtb = tgt
else:
tgtb = tgt.size()
if srcb == tgtb:
return src
if srcb > tgtb:
return Extract(tgtb - 1, 0, src)
return ZeroExt(tgtb - srcb, src)
def truncateOrSExt(src, tgt):
srcb = src.size()
tgtb = tgt.size()
if srcb == tgtb:
return src
if srcb > tgtb:
return Extract(tgtb - 1, 0, src)
return SignExt(tgtb - srcb, src)
def truncateOrPad(src, tgt):
srcb = src.size()
tgtb = tgt.size()
if srcb == tgtb:
return src
if srcb > tgtb:
return Extract(srcb - 1, srcb - tgtb, src)
return Concat(src, BitVecVal(0, tgtb - srcb))
"""
def no_overflow_smul(a, b):
size = a.size()
assert b.size() == size
m = SignExt(size, a) * SignExt(size, b)
min = BitVecVal(-(1 << (size-1)), 2*size)
max = BitVecVal((1 << (size-1)) -1, 2*size)
return And(m >= min, m <= max)
"""
def no_overflow_smul(a, b):
size = a.size()
assert b.size() == size
m = SignExt(size, a) * SignExt(size, b)
return m == SignExt(size, a * b)
def no_overflow_umul(a, b):
size = a.size()
assert b.size() == size
m = Extract(2*size-1, size, ZeroExt(size, a) * ZeroExt(size, b))
return m == BitVecVal(0, size)
def isShiftedMask(a):
v = (a - 1) | a
return [v != 0, ((v + 1) & v) == 0]
def bv_log2(v, bitwidth):
def rec(h, l):
if h <= l:
return BitVecVal(l, bitwidth)
mid = l+int((h-l)/2)
return If(Extract(h,mid+1,v) != 0, rec(h, mid+1), rec(mid, l))
return rec(v.size()-1, 0)
"""
linear version of log2
def bv_log2(v, bitwidth):
def rec(i):
if i == 0:
return BitVecVal(0, bitwidth)
return If(Extract(i,i,v) == BitVecVal(1,1), BitVecVal(i,bitwidth), rec(i-1))
return rec(v.size()-1)
"""
def ctlz(v, output_width):
size = v.size()
def rec(i):
if i < 0:
return BitVecVal(size, output_width)
return If(Extract(i,i,v) == BitVecVal(1, 1),
BitVecVal(size-1-i, output_width),
rec(i-1))
return rec(size-1)
def cttz(v, output_width):
size = v.size()
def rec(i):
if i == size:
return BitVecVal(size, output_width)
return If(Extract(i,i,v) == BitVecVal(1, 1),
BitVecVal(i, output_width),
rec(i+1))
return rec(0)
def ComputeNumSignBits(v, bitwidth):
size = v.size()
size1 = size - 1
sign = Extract(size1, size1, v)
def rec(i):
if i < 0:
return BitVecVal(size, bitwidth)
return If(Extract(i,i,v) == sign,
rec(i-1),
BitVecVal(size1-i, bitwidth))
return rec(size - 2)
##########################
# Type inference utilities
def register_pick_one_type(v):
global gbl_one_type_only
gbl_one_type_only.add(str(v))
def unregister_pick_one_type(vs):
global gbl_one_type_only
for v in vs.keys():
gbl_one_type_only.discard(v)
def reset_pick_one_type():
global gbl_one_type_only
gbl_one_type_only = set([])
def get_pick_one_type():
return gbl_one_type_only
##########################
# number of users of an instruction
def get_users_var(name):
return BitVec('u_' + name, 8)
def get_flag_var(flag, inst):
dst = 'src' if gbl_is_source else 'tgt'
return BitVec('f_%s_%s_%s' % (flag, inst, dst), 1)
def set_smt_is_source(s):
global gbl_is_source
gbl_is_source = s
gbl_infer_flags = False
def set_infer_flags(f):
global gbl_infer_flags
gbl_infer_flags = f
def do_infer_flags():
return gbl_infer_flags
gbl_use_array_theory = False
def set_use_array_theory(f):
global gbl_use_array_theory
gbl_use_array_theory = f
def use_array_theory():
return gbl_use_array_theory
gbl_new_semantics = False
def set_use_new_semantics(f):
global gbl_new_semantics
gbl_new_semantics = f
def use_new_semantics():
return gbl_new_semantics
gbl_ptr_size = 32
def set_ptr_size(m):
global gbl_ptr_size
sz = m.get_interp(Int('ptrsize'))
if sz is not None:
gbl_ptr_size = sz.as_long()
def get_ptr_size():
return gbl_ptr_size
##########################
# Error handling
class AliveError(Exception):
pass
class ParseError(BaseException):
def __init__(self, msgs, token = None):
if isinstance(msgs, list):
self.msgs = msgs
else:
self.msgs = [msgs]
self.token = token
def __repr__(self):
lineno = get_lineno()
line = get_line(lineno)
col = get_column(line, self.token)
return exception2str("\n".join(self.msgs), line, lineno, col)
gbl_line_offset = 0
def exception2str(msg, line, lineno, col, line_offset = None):
if line_offset is None:
line_offset = gbl_line_offset
s = "ERROR: %s (line: %d)\n" % (msg, line_offset + lineno)
s += line + '\n'
s += ' ' * col + '^'
return s
def get_lineno():
return gbl_parse_str.count('\n', 0, gbl_parse_loc) + 1
def get_line(lineno):
return gbl_parse_str.split('\n')[lineno-1]
def get_column(s, tok):
col = gbl_parse_loc - (gbl_parse_str.rfind('\n', 0, gbl_parse_loc)+1)
if not tok:
return col
token_col = s.find(tok, col)
return token_col if token_col >= 0 else col
def save_parse_str(s, line):
global gbl_parse_str, gbl_line_offset
gbl_parse_str = s
gbl_line_offset = line-1
def save_loc(loc):
global gbl_parse_loc
gbl_parse_loc = loc