pysvp64dis: do not create temporary bytes upon load
[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
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
29 while True:
30 insn = ifile.read(4)
31 length = len(insn)
32 if length == 0:
33 return
34 elif length < 4:
35 raise IOError(insn)
36 insn = _WordInstruction.integer(value=insn, byteorder=byteorder)
37 if insn.PO == 0x1:
38 suffix = ifile.read(4)
39 length = len(suffix)
40 if length == 0:
41 yield insn
42 return
43 elif length < 4:
44 raise IOError(suffix)
45
46 prefix = insn
47 suffix = _WordInstruction.integer(value=suffix, byteorder=byteorder)
48 insn = _SVP64Instruction.pair(prefix=prefix, suffix=suffix)
49 if insn.prefix.id != 0b11:
50 insn = _PrefixedInstruction.pair(prefix=prefix, suffix=suffix)
51 yield insn
52
53
54 def dump(insns, verbosity, **_):
55 db = _Database(_find_wiki_dir())
56 for insn in insns:
57 yield from insn.disassemble(db=db, verbosity=verbosity)
58
59
60 # this is the entry-point for the console-script pysvp64dis
61 def main():
62 parser = _argparse.ArgumentParser()
63 parser.add_argument("ifile", nargs="?",
64 type=_argparse.FileType("rb"), default=_sys.stdin.buffer)
65 parser.add_argument("ofile", nargs="?",
66 type=_argparse.FileType("w"), default=_sys.stdout)
67 parser.add_argument("-b", "--byteorder",
68 type=ByteOrder, default=ByteOrder.LITTLE)
69 parser.add_argument("-s", "--short",
70 dest="verbosity", default=_Verbosity.NORMAL,
71 action="store_const", const=_Verbosity.SHORT)
72 parser.add_argument("-v", "--verbose",
73 dest="verbosity", default=_Verbosity.NORMAL,
74 action="store_const", const=_Verbosity.VERBOSE)
75 parser.add_argument("-l", "--log",
76 action="store_true", default=False)
77
78 args = dict(vars(parser.parse_args()))
79
80 # if logging requested do not disable it.
81 if not args['log']:
82 _os.environ['SILENCELOG'] = '1'
83
84 # load instructions and dump them
85 insns = load(**args)
86 for line in dump(insns, **args):
87 print(line, file=args["ofile"])
88
89
90 # still here but use "python3 setup.py develop" then run the
91 # command "pysvp64dis" instead of "python3 src/openpower/sv/trans/pysvp64dis.py"
92 if __name__ == "__main__":
93 main()