-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenCCounter.py
executable file
·282 lines (237 loc) · 7.3 KB
/
genCCounter.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
#!/usr/bin/env python3
import sys
from z3 import *
from collections import defaultdict
TEMPLATE="""
#include <stdio.h>
int mod (int a, int b)
{{
if(b < 0) //you can check for b == 0 separately and do what you want
return mod(a, -b);
int ret = a % b;
if(ret < 0)
ret+=b;
return ret;
}}
int main() {{
unsigned long long SOLUTION_COUNT = 0;
//count
{count}
printf("%llu \\n",SOLUTION_COUNT);
return 0;
}}
"""
def is_literal(e):
return is_const(e) and e.decl().kind() == Z3_OP_UNINTERPRETED
def is_bvconst(e):
return is_const(e) and e.decl().kind() == Z3_OP_BNUM
def is_ite(e):
return e.decl().kind() == Z3_OP_ITE
def is_iff(e):
return e.decl().kind() == Z3_OP_IFF
def is_sge(c):
return c.decl().kind() == Z3_OP_SGEQ
def is_sgt(c):
return c.decl().kind() == Z3_OP_SGT
def is_sle(c):
return c.decl().kind() == Z3_OP_SLEQ
def is_slt(c):
return c.decl().kind() == Z3_OP_SLT
def is_badd(c):
return c.decl().kind() == Z3_OP_BADD
def is_bsub(c):
return c.decl().kind() == Z3_OP_BSUB
def is_bmul(c):
return c.decl().kind() == Z3_OP_BMUL
def is_bsdiv(c):
return c.decl().kind() == Z3_OP_BSDIV
def is_bsmod(c):
return c.decl().kind() == Z3_OP_BSMOD
def is_band(c):
return c.decl().kind() == Z3_OP_BAND
def is_bor(c):
return c.decl().kind() == Z3_OP_BOR
def is_blshift_r(c):
return c.decl().kind() == Z3_OP_BLSHR
def is_bshift_l(c):
return c.decl().kind() == Z3_OP_BSHL
def find_var_bounds(clauses):
# name => (lo,hi)
input_vars = defaultdict(lambda: [0,0])
to_process = []
for clause in clauses:
children = clause.children()
if is_sge(clause):
input_vars[children[0]][0] = children[1].as_long()
elif is_sgt(clause):
input_vars[children[0]][0] = children[1].as_long() + 1
elif is_sle(clause):
input_vars[children[0]][1] = children[1].as_long()
elif is_slt(clause):
input_vars[children[0]][1] = children[1].as_long() - 1
else:
to_process.append(clause)
return (input_vars,to_process)
def compile_to_c(formula):
#don't go deep now, just find out the bounds
input_vars, to_process = find_var_bounds(formula.children())
loop = []
domain_size = 1
for var,bounds in input_vars.items():
assert var.size() == 32
line = "for (int {name} = {lo}; {name} <= {hi}; {name}++) {{"
loop.append(line.format(name=var.sexpr(), lo=bounds[0], hi=bounds[1]))
domain_size *= bounds[1] - bounds[0]
filters = []
n_ops = 0
# print(formula, file=sys.stderr)
# print(formula.children(), file=sys.stderr)
for clause in formula.children():
for boolean_expr in c_visitor(clause):
line = "if (!({})) {{\n continue;\n }} \n".format(boolean_expr)
filters.append(line)
for oprt in ['+','-','*','/','%', ' & ', ' | ', ' ^ ', ' ~ ', '>>', '<<']:
n_ops += line.count(oprt)
full_loop = []
full_loop.extend(loop)
full_loop.extend(filters)
full_loop.append("SOLUTION_COUNT++;")
full_loop.extend(["}"] * len(loop))
count_code = "\n".join(full_loop)
return (TEMPLATE.format(count=count_code), domain_size, n_ops)
def c_visitor(e):
if is_literal(e):
yield e.decl().name()
return
elif is_bvconst(e):
yield str(e.as_long())
return
elif is_not(e):
assert len(e.children()) == 1
ch = e.children()[0]
for expr in c_visitor(ch):
yield "!(" + expr + ")"
return
elif is_or(e):
or_clauses = []
for ch in e.children():
for expr in c_visitor(ch):
or_clauses.append(expr)
yield "(" + " || ".join(or_clauses) + ")"
return
elif is_and(e):
or_clauses = []
for ch in e.children():
for expr in c_visitor(ch):
or_clauses.append(expr)
yield "(" + " && ".join(or_clauses) + ")"
elif is_eq(e) or is_iff(e):
eq_clauses = []
for ch in e.children():
for expr in c_visitor(ch):
eq_clauses.append(expr)
yield "(" + " == ".join(eq_clauses) + ")"
return
elif is_sge(e):
clauses = []
for ch in e.children():
for expr in c_visitor(ch):
clauses.append(expr)
yield "(" + " >= ".join(clauses) + ")"
return
elif is_sgt(e):
clauses = []
for ch in e.children():
for expr in c_visitor(ch):
clauses.append(expr)
yield "(" + " > ".join(clauses) + ")"
return
elif is_sle(e):
clauses = []
for ch in e.children():
for expr in c_visitor(ch):
clauses.append(expr)
yield "(" + " <= ".join(clauses) + ")"
return
elif is_slt(e):
clauses = []
for ch in e.children():
for expr in c_visitor(ch):
clauses.append(expr)
yield "(" + " < ".join(clauses) + ")"
return
elif is_badd(e):
clauses = []
for ch in e.children():
for expr in c_visitor(ch):
clauses.append(expr)
yield "(" + " + ".join(clauses) + ")"
return
elif is_bsub(e):
clauses = []
for ch in e.children():
for expr in c_visitor(ch):
clauses.append(expr)
yield "(" + " - ".join(clauses) + ")"
return
elif is_bmul(e):
clauses = []
for ch in e.children():
for expr in c_visitor(ch):
clauses.append(expr)
yield "(" + " * ".join(clauses) + ")"
return
elif is_bsdiv(e):
clauses = []
for ch in e.children():
for expr in c_visitor(ch):
clauses.append(expr)
yield "(" + " / ".join(clauses) + ")"
return
elif is_bsmod(e):
clauses = []
for ch in e.children():
for expr in c_visitor(ch):
clauses.append(expr)
yield "(mod(" + ",".join(clauses) + "))"
return
elif is_bor(e):
clauses = []
for ch in e.children():
for expr in c_visitor(ch):
clauses.append(expr)
yield "(" + " | ".join(clauses) + ")"
return
elif is_band(e):
clauses = []
for ch in e.children():
for expr in c_visitor(ch):
clauses.append(expr)
yield "(" + " & ".join(clauses) + ")"
return
elif is_blshift_r(e):
clauses = []
for ch in e.children():
for expr in c_visitor(ch):
clauses.append(expr)
yield "((unsigned int)" + " >> ".join(clauses) + ")"
return
elif is_bshift_l(e):
clauses = []
for ch in e.children():
for expr in c_visitor(ch):
clauses.append(expr)
yield "(" + " << ".join(clauses) + ")"
return
else:
raise Exception("Unhandled type: " + e.sexpr(), e.decl())
def main():
inputfile = sys.argv[1]
print("Reading formula...", file=sys.stderr)
formula = parse_smt2_file(inputfile)
print("Generating C source...", file=sys.stderr)
code, domain, nopts = compile_to_c(formula)
print("{},{},{}".format(inputfile,domain,nopts), file=sys.stderr)
print(code)
if __name__ == '__main__':
main()