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