-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.py
More file actions
336 lines (297 loc) · 13.6 KB
/
generator.py
File metadata and controls
336 lines (297 loc) · 13.6 KB
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
from math import ceil, log2
import sys
import subprocess
import os.path
from random import randint
sys.path.insert(1, str('./firrtl-operations'))
import uint
import sint
def getbitsize(var):
if var == 0:
return 1
return ceil(log2(var+1))
# return len(bin(var)[2:])
bins = ["+", "-", "*", "/", "%"]
abu = ["^", "|", "&"]
uns = ["<", "<=", ">", ">=", "==", "!="]
bits = ["pad", "shl", "shr", "head", "tail"]
dyn = ["dshl", "dshr"]
sins = ["~"]
binbit = ["andr", "orr", "xorr"]
vlv = ["cat"]
threeparm = ["bits"]
opConvert = {
"dshl": "<<",
"dshr": ">>"
}
class u_operation:
u_ops = {}#27
u_ops["+"] = uint.model_uint.uint_add
u_ops["-"] = uint.model_uint.uint_sub
u_ops["*"] = uint.model_uint.uint_mul
u_ops["/"] = uint.model_uint.uint_div
u_ops["%"] = uint.model_uint.uint_rem
u_ops["<"] = uint.model_uint.uint_lt
u_ops["<="] = uint.model_uint.uint_leq
u_ops[">"] = uint.model_uint.uint_gt
u_ops[">="] = uint.model_uint.uint_geq
u_ops["=="] = uint.model_uint.uint_eq
u_ops["!="] = uint.model_uint.uint_neq
u_ops["pad"] = uint.model_uint.uint_pad
u_ops["shl"] = uint.model_uint.uint_shl
u_ops["shr"] = uint.model_uint.uint_shr
u_ops["dshl"] = uint.model_uint.uint_dshl
u_ops["dshr"] = uint.model_uint.uint_dshr
u_ops["~"] = uint.model_uint.uint_not
u_ops["&"] = uint.model_uint.uint_and
u_ops["|"] = uint.model_uint.uint_or
u_ops["^"] = uint.model_uint.uint_xor
u_ops["andr"] = uint.model_uint.uint_andr
u_ops["orr"] = uint.model_uint.uint_orr
u_ops["xorr"] = uint.model_uint.uint_xorr
u_ops["cat"] = uint.model_uint.uint_cat
u_ops["bits"] = uint.model_uint.uint_bits
u_ops["tail"] = uint.model_uint.uint_tail
u_ops["head"] = uint.model_uint.uint_head
def randcreate(digitlength: int) -> int:
return randint(2**(digitlength-1), (2**digitlength)-1)
def declareVariable(f, num: uint.model_uint):
f.write("\tUInt<" + str(num.bitsize) + "> u" + str(num.value) + "(\"" + str(hex(num.value)) + "\");\n")
def declareOneVariable(f, num: uint.model_uint, name: str):
f.write("\tUInt<" + str(num.bitsize) + "> " + name + "(\"" + str(hex(num.value)) + "\");\n")
# assert(u0+u0 == UInt<1>("0x0"));
def binary(file, func, op: str, a, b):
# func = operation.u_ops[op]
uint_a = uint.model_uint(a)
uint_b = uint.model_uint(b)
result = func(uint_a, uint_b)
if result != None: #only bc divsion by zero is impossible
file.f.write("\tassert((a"+op+"b"+ ") == UInt<"+str(result.bitsize)+">(\"" + str(hex(result.value)) + "\"));\n")
# assert(0 == (u59221<u58024));
def unary(file, func, op: str, a: int, b: int):
# func = operation.u_ops[op]
uint_a = uint.model_uint(a)
uint_b = uint.model_uint(b)
result = func(uint_a, uint_b)
file.f.write("\tassert("+str(result.value) +" == (a"+op+"b"+"));\n")
# assert(u59221.pad<3>() == UInt<16>("0xe755"));
def bitwise(file, func, op: str, a: int, n):
# func = operation.u_ops[op]
uint_a = uint.model_uint(a)
result = func(uint_a, n)
file.f.write("\tassert(a"+"."+op+"<"+str(n)+">() == UInt<"+str(result.bitsize)+">(\""+str(hex(result.value))+"\"));\n")
# assert((u15 >> UInt<3>("0x4")) == UInt<4>("0x0"));
def dynamic(file, func, op: str, a: int, b):
# func = operation.u_ops[op]
uint_a = uint.model_uint(a)
b_size = getbitsize(b)
uint_b = uint.model_uint(b, b_size)
result = func(uint_a, uint_b)
file.f.write("\tassert((a"+" "+op+" UInt<"+str(b_size)+">(\""+str(hex(uint_b.value))+"\")) == UInt<"+str(result.bitsize)+">(\""+str(hex(result.value))+"\"));\n")
# assert(~u0 == UInt<1>("0x0"));
def singular(file, func, op: str, a: int):
# func = operation.u_ops[op]
uint_a = uint.model_uint(a)
result = func(uint_a)
file.f.write("\tassert("+op+"a"+" == UInt<"+str(result.bitsize)+">(\""+str(hex(result.value))+"\"));\n")
# assert(u15%u15 == UInt<4>("0x0"));
def comp(file, func, op: str, a: int, b: int):
# func = operation.u_ops[op]
uint_a = uint.model_uint(a)
uint_b = uint.model_uint(b)
result = func(uint_a, uint_b)
file.f.write("\tassert((a"+op+"b"+ ") == UInt<"+str(result.bitsize)+">(\"" + str(hex(result.value)) + "\"));\n")
# assert((u4059599200.andr()) == UInt<1>("0x0"));
def binarybitwise(file, func, op: str, a: int):
# func = operation.u_ops[op]
uint_a = uint.model_uint(a)
result = func(uint_a)
file.f.write("\tassert((a"+"."+op+"()) == UInt<"+str(result.bitsize)+">(\""+str(hex(result.value))+"\"));\n")
# assert((u4.cat(u5)) == UInt<1>("0x1"));
def vlv(file, func, op: str, a: int, b: int):
# func = operation.u_ops[op]
uint_a = uint.model_uint(a)
uint_b = uint.model_uint(b)
result = func(uint_a, uint_b)
file.f.write("\tassert(a"+"."+op+"(b"+") == UInt<"+str(result.bitsize)+">(\""+str(hex(result.value))+"\"));\n")
# assert((u4.bits<0,1>) == UInt<1>("0x1"));
def threeparm(file, func, op: str, a: int, b: int, c: int):
# func = operation.u_ops[op]
uint_a = uint.model_uint(a)
result = func(uint_a, b, c)
file.f.write("\tassert((a"+"."+op+"<"+str(b)+","+str(c)+">()) == UInt<"+str(result.bitsize)+">(\""+str(hex(result.value))+"\"));\n")
class s_operation:
s_ops = {}
s_ops["+"] = sint.model_sint.sint_add
s_ops["-"] = sint.model_sint.sint_sub
s_ops["*"] = sint.model_sint.sint_mul
s_ops["/"] = sint.model_sint.sint_div
s_ops["%"] = sint.model_sint.sint_mod
s_ops["<"] = sint.model_sint.sint_lt
s_ops["<="] = sint.model_sint.sint_leq
s_ops[">"] = sint.model_sint.sint_gt
s_ops[">="] = sint.model_sint.sint_geq
s_ops["=="] = sint.model_sint.sint_eq
s_ops["!="] = sint.model_sint.sint_neq
s_ops["pad"] = sint.model_sint.sint_pad
s_ops["shl"] = sint.model_sint.sint_shl
s_ops["shr"] = sint.model_sint.sint_shr
s_ops["dshl"] = sint.model_sint.sint_dshl
s_ops["dshr"] = sint.model_sint.sint_dshr
s_ops["~"] = sint.model_sint.sint_not
s_ops["&"] = sint.model_sint.sint_and
s_ops["|"] = sint.model_sint.sint_or
s_ops["^"] = sint.model_sint.sint_xor
s_ops["andr"] = sint.model_sint.sint_andr
s_ops["orr"] = sint.model_sint.sint_orr
s_ops["xorr"] = sint.model_sint.sint_xorr
s_ops["cat"] = sint.model_sint.sint_cat
s_ops["bits"] = sint.model_sint.sint_bits
s_ops["tail"] = sint.model_sint.sint_tail
s_ops["head"] = sint.model_sint.sint_head
def randcreate(digitlength: int) -> int:
return randint(2**(digitlength-1), (2**digitlength)-1)
def declareVariable(f, num: sint.model_sint):
f.write("\tSInt<" + str(num.bitsize) + "> u" + str(num.value) + "(\"" + str(hex(num.value)) + "\");\n")
def declareOneVariable(f, num: sint.model_sint, name: str):
f.write("\tSInt<" + str(num.bitsize) + "> " + name + "(\"" + str(hex(num.realval)) + "\");\n")
# assert(u0+u0 == SInt<1>("0x0"));
def binary(file, func, op: str, a, b):
sint_a = sint.model_sint(a)
sint_b = sint.model_sint(b)
result = func(sint_a, sint_b)
if result != None: #only bc divsion by zero is impossible
file.f.write("\tassert(a"+op+"b"+ " == SInt<"+str(result.bitsize)+">(\"" + str(hex(result.realval)) + "\"));\n")
def abu(file, func, op: str, a, b):
sint_a = sint.model_sint(a)
sint_b = sint.model_sint(b)
result = func(sint_a, sint_b)
if result != None: #only bc divsion by zero is impossible
file.f.write("\tassert((a"+op+"b)"+ " == UInt<"+str(result.bitsize)+">(\"" + str(hex(result.realval)) + "\"));\n")
# assert(0 == (false<(a<b))));
def unary(file, func, op: str, a: int, b: int):
sint_a = sint.model_sint(a)
sint_b = sint.model_sint(b)
result = func(sint_a, sint_b)
file.f.write("\tassert("+str(result.value) +" == (a"+op+"b"+"));\n")
# assert(u59221.pad<3>() == UInt<16>("0xe755"));
def bitwise(file, func, op: str, a: int, n):
sint_a = sint.model_sint(a)
result = func(sint_a, n)
if result.type == "uint":
file.f.write("\tassert(a"+"."+op+"<"+str(n)+">() == UInt<"+str(result.bitsize)+">(\""+str(hex(result.value))+"\"));\n")
else:
file.f.write("\tassert(a"+"."+op+"<"+str(n)+">() == SInt<"+str(result.bitsize)+">(\""+str(hex(result.realval))+"\"));\n")
# assert((s15 >> UInt<3>("0x4")) == UInt<4>("0x0"));
def dynamic(file, func, op: str, a: int, b):
sint_a = sint.model_sint(a)
b_size = getbitsize(b)
uint_b = uint.model_uint(b, b_size)
result = func(sint_a, uint_b)
file.f.write("\tassert((a"+" "+op+" UInt<"+str(b_size)+">(\""+str(hex(uint_b.value))+"\")) == SInt<"+str(result.bitsize)+">(\""+str(hex(result.realval))+"\"));\n")
# assert((u4.cat(u5)) == UInt<1>("0x1"));
def vlv(file, func, op: str, a: int, b: int):
# func = operation.u_ops[op]
sint_a = sint.model_sint(a)
sint_b = sint.model_sint(b)
result = func(sint_a, sint_b)
file.f.write("\tassert(a"+"."+op+"(b) == SInt<"+str(result.bitsize)+">(\""+str(hex(result.realval))+"\"));\n")
class file:
def __init__(self, folder, name): #initalize file
self.name = "testcases/"+folder+"/"+name
f = open(self.name+".cpp", "w")
self.f = f
self.testcase = None
self.completed = 0
def settestcase(self, testcase):
self.testcase = testcase
def new_uint(self, bitsize: int, value: int) -> uint.model_uint:# create uint.model_uint and declare in cpp
obj = uint.model_uint(value, bitsize)
u_operation.declareVariable(self.f, obj)
return obj
def new_one_uint(self, bitsize: int, value: int, name) -> uint.model_uint:# create uint.model_uint and declare in cpp
obj = uint.model_uint(value, bitsize)
u_operation.declareOneVariable(self.f, obj, name)
return obj
def new_one_sint(self, bitsize: int, value: int, name) -> sint.model_sint:# create uint.model_uint and declare in cpp
obj = sint.model_sint(value, bitsize)
s_operation.declareOneVariable(self.f, obj, name)
return obj
def docalculate(self, op: str, a, b = 0, c = 0): #call correct operation
func = u_operation.u_ops[op]
if op in bins:
u_operation.binary(self, func, op, a, b)
elif op in abu:
u_operation.binary(self, func, op, a, b)
elif op in uns:
u_operation.unary(self, func, op, a, b)
elif op in bits:
u_operation.bitwise(self, func, op, a, b)
elif op in dyn:
u_operation.dynamic(self, func, opConvert[op], a, b)
elif op in sins:
u_operation.singular(self, func, op, a)
elif op in binbit:
u_operation.binarybitwise(self, func, op, a)
elif op in vlv:
u_operation.vlv(self, func, op, a, b)
elif op in threeparm:
u_operation.threeparm(self, func, op, a, b, c)
def s_docalculate(self, op: str, a, b = 0, c = 0): #call correct operation
func = s_operation.s_ops[op]
if op in bins:
s_operation.binary(self, func, op, a, b)
elif op in abu:
s_operation.abu(self, func, op, a, b)
elif op in uns:
s_operation.unary(self, func, op, a, b)
elif op in bits:
s_operation.bitwise(self, func, op, a, b)
elif op in dyn:
s_operation.dynamic(self, func, opConvert[op], a, b)
# elif op in sins:
# u_operation.singular(self, func, op, a)
elif op in binbit:#now working on
u_operation.binarybitwise(self, func, op, a)
elif op in vlv:
s_operation.vlv(self, func, op, a, b)
# elif op in threeparm:
# u_operation.threeparm(self, func, op, a, b, c)
def top(self, type):#header and main
if os.getenv("GITHUB_ACTIONS"):
if type == "uint":
self.f.write("#include \"../../../../firrtl-sig/uint.h\"\n")
else:
self.f.write("#include \"../../../../firrtl-sig/sint.h\"\n")
else:
if type == "uint":
self.f.write("#include \"../../../firrtl-sig/uint.h\"\n")
else:
self.f.write("#include \"../../../firrtl-sig/sint.h\"\n")
self.f.write("#include <assert.h>\n")
self.f.write("int main() {\n\n")
def bottom(self): #return and closing
self.f.write("\n")
self.f.write("\treturn 0;\n")
self.f.write("}")
def runmanual(self, type, op, a, b = 0, c = 0):
self.top(type)
if type == "uint":
self.new_one_uint(getbitsize(a),a, "a")
if op not in bits and op not in binbit and op not in sins and op not in threeparm and op not in dyn: #no need to declare b
self.new_one_uint(getbitsize(b),b, "b")
self.docalculate(op, a, b, c)
else:#sint
self.new_one_sint(getbitsize(a),a, "a")
if op not in bits and op not in binbit and op not in sins and op not in threeparm and op not in dyn: #no need to declare b
self.new_one_sint(getbitsize(b),b, "b")
self.s_docalculate(op, a, b, c)
self.bottom()
self.f.close()
subprocess.call(["g++", self.name+".cpp", "-o", self.name]) #test cpp program
if os.path.exists(self.name):
subprocess.call(["./"+self.name])
subprocess.call(["rm", self.name])
self.completed = 1
def getcompletedcount(self):
return self.completed