sv/binutils.py: provide sketch sv_decode.vhdl converter
[openpower-isa.git] / src / openpower / sv / sv_binutils.py
1 import argparse as _argparse
2 import codecs as _codecs
3 import pathlib as _pathlib
4 import re as _re
5
6
7 from openpower.decoder.power_enums import (
8 SVPtype as _SVPtype,
9 SVEtype as _SVEtype,
10 In1Sel as _In1Sel,
11 In2Sel as _In2Sel,
12 In3Sel as _In3Sel,
13 OutSel as _OutSel,
14 CRInSel as _CRInSel,
15 CROutSel as _CROutSel,
16 SVEXTRA as _SVEXTRA,
17 )
18
19
20 PATTERN = "".join((
21 r"^\s*",
22 r"(?P<opcode>(?:2#[01]+#)|(?:[0-9]+)|(?:[01-]+))",
23 r"\s?=>\s?",
24 r"\(",
25 r",\s".join((
26 rf"(?P<ptype>{'|'.join(item.name for item in _SVPtype)})",
27 rf"(?P<etype>{'|'.join(item.name for item in _SVEtype)})",
28 rf"(?P<in1>{'|'.join(item.name for item in _In1Sel)})",
29 rf"(?P<in2>{'|'.join(item.name for item in _In2Sel)})",
30 rf"(?P<in3>{'|'.join(item.name for item in _In3Sel)})",
31 rf"(?P<out>{'|'.join(item.name for item in _OutSel)})",
32 rf"(?P<out2>{'|'.join(item.name for item in _OutSel)})",
33 rf"(?P<cr_in>{'|'.join(item.name for item in _CRInSel)})",
34 rf"(?P<cr_out>{'|'.join(item.name for item in _CROutSel)})",
35 rf"(?P<sv_in1>{'|'.join(item.name for item in _SVEXTRA)})",
36 rf"(?P<sv_in2>{'|'.join(item.name for item in _SVEXTRA)})",
37 rf"(?P<sv_in3>{'|'.join(item.name for item in _SVEXTRA)})",
38 rf"(?P<sv_out>{'|'.join(item.name for item in _SVEXTRA)})",
39 rf"(?P<sv_out2>{'|'.join(item.name for item in _SVEXTRA)})",
40 rf"(?P<sv_cr_in>{'|'.join(item.name for item in _SVEXTRA)})",
41 rf"(?P<sv_cr_out>{'|'.join(item.name for item in _SVEXTRA)})",
42 )),
43 r"\)",
44 r",",
45 r"\s?--\s?",
46 r"(?P<insn>[A-Za-z0-9_\./]+)",
47 r"\s*$",
48 ))
49 REGEX = _re.compile(PATTERN)
50
51
52 def parse(stream):
53 for line in stream:
54 match = REGEX.match(line)
55 if match is not None:
56 yield match.groupdict()
57
58
59 def main(vhdl):
60 insns = []
61 with _codecs.open(vhdl, "rb", "UTF-8") as stream:
62 for insn in parse(stream):
63 insns.append(insn)
64
65 print(f"{len(insns)} instructions found")
66
67
68 if __name__ == "__main__":
69 parser = _argparse.ArgumentParser()
70 parser.add_argument("vhdl", type=_pathlib.Path, help="sv_decode.vhdl path")
71
72 args = vars(parser.parse_args())
73 main(**args)