/
formula_mngm.py
331 lines (252 loc) · 10.2 KB
/
formula_mngm.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
# Copyright 2018 Cristian Mattarei
#
# Licensed under the modified BSD (3-clause BSD) License.
#
# 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.
import itertools
import re
from pysmt.walkers.identitydag import IdentityDagWalker
from pysmt.shortcuts import Ite, EqualsOrIff, BV, get_type, simplify, And, Or
from pysmt.typing import BOOL, BVType, ArrayType
from cosa.utils.generic import new_string
def B2BV(f):
if get_type(f).is_bv_type():
return f
return Ite(f, BV(1,1), BV(0,1))
def BV2B(f):
if get_type(f).is_bool_type():
return f
return EqualsOrIff(f, BV(1,1))
class SubstituteWalker(IdentityDagWalker):
def set_substitute_function(self, function):
self.substitute_function = function
def set_substitute_map(self, smap):
self.mapsymbols = smap
def walk_symbol(self, formula, args, **kwargs):
if formula.symbol_name() in self.mapsymbols:
return self.mgr.Symbol(self.mapsymbols[formula.symbol_name()],
formula.symbol_type())
return self.mgr.Symbol(formula.symbol_name(),
formula.symbol_type())
class SymbolsWalker(IdentityDagWalker):
symbols = set([])
def reset_symbols(self):
self.symbols = set([])
def walk_symbol(self, formula, args, **kwargs):
self.symbols.add(formula)
return formula
def substitute(formula, mapsym, reset_walker=False):
subwalker = SubstituteWalker()
subwalker.set_substitute_map(mapsym)
return subwalker.walk(formula)
free_variables_dic = {}
def get_free_variables(formula):
if formula in free_variables_dic:
return set([x for x in free_variables_dic[formula]])
symwalker = SymbolsWalker()
symwalker.reset_symbols()
symwalker.walk(formula)
ret = symwalker.symbols
free_variables_dic[formula] = ret
return ret
############### Values and Helper Functions for quote_names #################
# don't treat these as variables in quote_names
KEYWORDS = ["not","xor",\
"False","True",\
"next","prev",\
"G","F","X","U","R","O","H",\
"ZEXT", "SEXT", "bvcomp",\
"a>>", "s<", "s>",
"u<", "u<=", "u>", "u>="]
# rewrite the first entry in the tuple to the second
OPERATORS = [(" < "," u< "), \
(" > "," u> "), \
(" >= "," u>= "), \
(" <= "," u<= ")]
VAR_PATTERN = re.compile(r"(?<![\w\.])([a-zA-Z_][\w_.]*)")
def __replace_keywords(strformula, offset=0):
'''
INTERNAL USE ONLY
Removes keywords without removing them from other words
'''
# need to use pattern replacement for keywords,
# so that common ones (e.g. "G") are not removed from words
patterns = [re.compile(r"(?<![\w.]){}(?![\w.])".format(k)) for k in KEYWORDS]
removed = []
idx = offset
for mp in patterns:
m = re.search(mp, strformula)
if m:
strformula, num_reps = re.subn(mp, "{%i}"%idx, strformula)
idx += 1
removed.append(m.group())
return strformula, removed, idx
def __replace_matching_strings(strformula, pattern, offset=0):
'''
INTERNAL USE ONLY
Removes all strings matching any of the patterns
'''
to_replace = sorted(set(re.findall(pattern, strformula)), key=len, reverse=True)
idx = offset
removed = []
for r in to_replace:
strformula = strformula.replace(r, "{%i}"%idx)
removed.append(r)
idx += 1
return strformula, removed, idx
def __replace_quoted(strformula, offset=0):
return __replace_matching_strings(strformula, r"'.*?'", offset)
def __replace_escaped(strformula, offset=0):
return __replace_matching_strings(strformula, r"\\\S*", offset)
def __replace_vars(strformula, offset=0):
return __replace_matching_strings(strformula, VAR_PATTERN, offset)
def quote_names(strformula, prefix=None, replace_ops=True):
'''
Quotes variable names with single quotes so the user doesn't need to quote
them in properties, assumptions, etc...
Valid variable names are those from Verilog (including escaped names),
except without '$' as a valid (unescaped) symbol
The user can always supply single quotes themselves to treat the string as
a variable.
For example,
varname[0] will be treated as a bit-extract on the variable 'varname'
by default
but,
'varname[0]' is a variable named 'varname[0]'
'''
strformula, replaced_keywords, last_idx = __replace_keywords(strformula)
strformula, replaced_quoted, last_idx = __replace_quoted(strformula, last_idx)
# remove quotes for now, might need to add a prefix
replaced_quoted = [r.replace("'", "") for r in replaced_quoted]
strformula, replaced_escaped, last_idx = __replace_escaped(strformula, last_idx)
strformula, replaced_vars, _ = __replace_vars(strformula, last_idx)
replaced = itertools.chain(replaced_quoted, replaced_escaped, replaced_vars)
if prefix is not None and prefix != '':
replaced = ["'{}.{}'".format(prefix, r) for r in replaced]
else:
replaced = ["'{}'".format(r) for r in replaced]
# add keywords back in -- don't want to add prefix to keywords
replaced = itertools.chain(replaced_keywords, replaced)
# replace all the removed symbols
strformula = strformula.format(*replaced)
if replace_ops:
for op in OPERATORS:
strformula = strformula.replace(op[0], op[1])
return strformula
def mem_access(addr, locations, width_idx, idx=0):
first_loc = min(2**width_idx, len(locations))-1
ite_chain = locations[first_loc]
for i in reversed(range(0, first_loc)):
location = BV(i, width_idx)
ite_chain = Ite(EqualsOrIff(addr, location), locations[i], ite_chain)
return ite_chain
class SortingNetwork(object):
simplify = False
@staticmethod
def sorting_network(inputs):
if len(inputs) == 0:
return []
return SortingNetwork.sorting_network_int(inputs)
@staticmethod
def sorting_network_int(inputs):
if len(inputs) == 1:
return inputs;
if len(inputs) == 2:
el1 = inputs[0]
el2 = inputs[1]
return SortingNetwork.two_comparator(el1, el2)
pivot = int(len(inputs) / 2)
left_inputs = inputs[:pivot]
right_inputs = inputs[pivot:]
left_outputs = SortingNetwork.sorting_network_int(left_inputs)
right_outputs = SortingNetwork.sorting_network_int(right_inputs)
outputs = SortingNetwork.merge(left_outputs, right_outputs)
return outputs
# Basic comparator for two inputs.
@staticmethod
def two_comparator(input1, input2):
if SortingNetwork.simplify:
return [simplify(Or(input1, input2)), simplify(And(input1, input2))]
else:
return [Or(input1, input2), And(input1, input2)]
# Function that merges two arrays of signals.
@staticmethod
def merge(input1, input2):
output = []
if len(input1) == 0:
return input2
if len(input2) == 0:
return input1
if (len(input1) == 1) and (len(input2) == 1):
el1 = input1[0]
el2 = input2[0];
return SortingNetwork.two_comparator(el1, el2);
is_input1_even = ((len(input1) % 2) == 0)
is_input2_even = ((len(input2) % 2) == 0)
is_input1_odd = not is_input1_even
is_input2_odd = not is_input2_even
if is_input1_odd and is_input2_even:
return SortingNetwork.merge(input2, input1)
size_h1 = (len(input1) / 2);
res_h1 = (len(input1) % 2);
input1_odd = []
input1_even = []
for i in range(len(input1)):
element = input1[i]
if (((i+1)%2) == 0):
input1_even.append(element)
else:
input1_odd.append(element)
size_h2 = (len(input2) / 2);
res_h2 = (len(input2) % 2);
input2_odd = []
input2_even = []
for i in range(len(input2)):
element = input2[i]
if (((i+1)%2) == 0):
input2_even.append(element)
else:
input2_odd.append(element)
output_odd = SortingNetwork.merge(input1_odd, input2_odd);
output_even = SortingNetwork.merge(input1_even, input2_even);
# is_input1_even && is_input2_even
if is_input1_even and is_input2_even:
first_output_odd = output_odd[0]
last_output_even = output_even[-1]
output.append(first_output_odd)
for i in range(len(output_odd) - 1):
el_odd = output_odd[i+1]
el_even = output_even[i]
res = SortingNetwork.two_comparator(el_odd, el_even)
output.append(res[0])
output.append(res[1])
output.append(last_output_even)
# end of is_input1_even && is_input2_even
if is_input1_even and is_input2_odd:
first_output_odd = output_odd[0]
output.append(first_output_odd)
for i in range(len(output_even)):
el_odd = output_odd[i+1]
el_even = output_even[i]
res = SortingNetwork.two_comparator(el_odd, el_even)
output.append(res[0])
output.append(res[1])
# end of is_input1_even && is_input2_odd
if is_input1_odd and is_input2_odd:
first_output_odd = output_odd[0]
last_output_odd = output_odd[-1]
output.append(first_output_odd)
for i in range(len(output_even)):
el_odd = output_odd[i+1]
el_even = output_even[i]
res = SortingNetwork.two_comparator(el_odd, el_even)
output.append(res[0])
output.append(res[1])
output.append(last_output_odd)
# end of is_input1_odd && is_input2_odd
assert((len(input1)+len(input2)) == len(output))
return output