3722af0961a8043e6c10cd49078b89883586e2e9
[openpower-isa.git] / src / openpower / sv / trans / pysvp64dis.py
1 import argparse as _argparse
2 import enum as _enum
3 import sys as _sys
4 import os as _os
5 from io import BytesIO
6
7 from openpower.decoder.power_enums import (
8 find_wiki_dir as _find_wiki_dir,
9 )
10 from openpower.decoder.power_insn import (
11 Verbosity as _Verbosity,
12 Database as _Database,
13 WordInstruction as _WordInstruction,
14 PrefixedInstruction as _PrefixedInstruction,
15 SVP64Instruction as _SVP64Instruction,
16 )
17
18
19 class ByteOrder(_enum.Enum):
20 LITTLE = "little"
21 BIG = "big"
22
23 def __str__(self):
24 return self.name.lower()
25
26
27 def load(ifile, byteorder=ByteOrder.LITTLE, **_):
28 byteorder = str(byteorder)
29
30 # copy over to persistent binfile (BytesIO)
31 cpfile = BytesIO()
32 cpfile.write(ifile.read())
33 cpfile.seek(0)
34
35 while True:
36 insn = cpfile.read(4)
37 length = len(insn)
38 if length == 0:
39 return
40 elif length < 4:
41 raise IOError(insn)
42 insn = _WordInstruction.integer(value=insn, byteorder=byteorder)
43 if insn.PO == 0x1:
44 suffix = cpfile.read(4)
45 length = len(suffix)
46 if length == 0:
47 yield insn
48 return
49 elif length < 4:
50 raise IOError(suffix)
51
52 prefix = insn
53 suffix = _WordInstruction.integer(value=suffix, byteorder=byteorder)
54 insn = _SVP64Instruction.pair(prefix=prefix, suffix=suffix)
55 if insn.prefix.id != 0b11:
56 insn = _PrefixedInstruction.pair(prefix=prefix, suffix=suffix)
57 yield insn
58
59 cpfile.seek(0) # restore position so that generator can be reused
60
61
62 def dump(insns, verbosity, **_):
63 db = _Database(_find_wiki_dir())
64 for insn in insns:
65 yield from insn.disassemble(db=db, verbosity=verbosity)
66
67
68 # this is the entry-point for the console-script pysvp64dis
69 def main():
70 parser = _argparse.ArgumentParser()
71 parser.add_argument("ifile", nargs="?",
72 type=_argparse.FileType("rb"), default=_sys.stdin.buffer)
73 parser.add_argument("ofile", nargs="?",
74 type=_argparse.FileType("w"), default=_sys.stdout)
75 parser.add_argument("-b", "--byteorder",
76 type=ByteOrder, default=ByteOrder.LITTLE)
77 parser.add_argument("-s", "--short",
78 dest="verbosity", default=_Verbosity.NORMAL,
79 action="store_const", const=_Verbosity.SHORT)
80 parser.add_argument("-v", "--verbose",
81 dest="verbosity", default=_Verbosity.NORMAL,
82 action="store_const", const=_Verbosity.VERBOSE)
83 parser.add_argument("-l", "--log",
84 action="store_true", default=False)
85
86 args = dict(vars(parser.parse_args()))
87
88 # if logging requested do not disable it.
89 if not args['log']:
90 _os.environ['SILENCELOG'] = '1'
91
92 # load instructions and dump them
93 insns = load(**args)
94 for line in dump(insns, **args):
95 print(line, file=args["ofile"])
96
97
98 # still here but use "python3 setup.py develop" then run the
99 # command "pysvp64dis" instead of "python3 src/openpower/sv/trans/pysvp64dis.py"
100 if __name__ == "__main__":
101 main()