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