forked from CTSRD-CHERI/cheri-cap-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CHERICapWrapBlarney.py
executable file
·93 lines (86 loc) · 3.35 KB
/
CHERICapWrapBlarney.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
#! /usr/bin/env python3
import argparse
import re
parser = argparse.ArgumentParser(description=
'''Generates a Blarney wrapper for the given Bluespec generated verilog file
containing a module definition of a purely combinational CHERI function.
''')
parser.add_argument('verilog_files', metavar='VERILOG_FILE', type=str, nargs='+',
help='The file(s) to process')
parser.add_argument('--output', '-o', metavar='OUTPUT_FILE', type=str, nargs='?',
default="CHERIBlarneyWrappers",
help='The output Blarney Haskell module to generate')
args = parser.parse_args()
class BlarneyWrapper:
def __init__(self, size, name, ins, out):
self.size = size
self.name = name
self.ins = ins
self.out = out
def verilogModuleName(self):
return "module_wrap{:d}_{:s}".format(self.size, self.name)
def verilogInputNames(self):
return ["wrap{:d}_{:s}_{:s}".format(self.size, self.name, nm)
for nm in [x[0] for x in self.ins]]
def verilogOutputName(self):
return "wrap{:d}_{:s}".format(self.size, self.name)
def emitBlarney(self):
ins_names = [x[0] for x in self.ins]
ins_wdths = [x[1] for x in self.ins]
str_type = "{:s} :: {:s}{:s}{:s}".format(
self.name,
" -> ".join(["Bit {:d}".format(n) for n in ins_wdths]),
" -> " if self.ins else "",
"Bit {:d}".format(self.out[1]))
str_decl = "{:s} {:s} = FromBV $\n makePrim1 (Custom \"{:s}\" [{:s}] [{:s}] [] False Nothing) [{:s}]".format(
self.name, " ".join(ins_names),
self.verilogModuleName(),
", ".join(["(\"{:s}\", {:d})".format(n, w)
for (n, w) in zip(self.verilogInputNames(),
ins_wdths)]),
"(\"{:s}\", {:d})".format(self.verilogOutputName(), self.out[1]),
", ".join(["toBV {:s}".format(nm) for nm in ins_names]))
return "{:s}\n{:s}".format(str_type, str_decl)
def main():
# define module regexp
modDecl = re.compile("^module\s+module_wrap(\d+)_(\w+)\(")
# TODO handle size 1
#
wrappers = []
for fname in args.verilog_files:
size = 0
name = None
ins = []
out = ("",0)
with open(fname, "r") as f:
for ln in f:
modM = modDecl.match(ln)
if modM:
size = int(modM.group(1))
name = modM.group(2)
break
if not name:
print("Couldn't find a valid Verilog module definition")
exit(-1)
# define input/output regexp
inDecl = re.compile("^\s*input(\s+\[(\d+)\s+:\s+0\])?\s+wrap(\d+)_"+name+"_(\w+);")
outDecl = re.compile("^\s*output(\s+\[(\d+)\s+:\s+0\])?\s+wrap(\d+)_"+name+";")
for ln in f:
inM = inDecl.match(ln)
outM = outDecl.match(ln)
if inM:
ins.append((inM.group(4), (int(inM.group(2)) + 1) if inM.group(1) else 1))
elif outM:
out = (name, (int(outM.group(2)) + 1) if outM.group(1) else 1)
#else:
# print("===>> no match for line: {:s}".format(ln))
wrappers.append(BlarneyWrapper(size, name, ins, out))
with open(args.output+".hs", "w") as f:
#print("module CHERI{:d} where\n".format(size))
f.write("module "+args.output+" where\n\n")
f.write("import Blarney\n")
f.write("import Blarney.Core.BV\n")
for w in wrappers:
f.write("\n{:s}\n".format(w.emitBlarney()))
if __name__ == "__main__":
main()