selectable_int: refactor fields mappings
[openpower-isa.git] / src / openpower / sv / trans / pysvp64dis.py
1 import argparse as _argparse
2 import enum as _enum
3 import functools as _functools
4 import sys as _sys
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 Instruction as _Instruction,
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, **_):
26 db = _Database(_find_wiki_dir())
27
28 def load(ifile):
29 prefix = ifile.read(4)
30 length = len(prefix)
31 if length == 0:
32 return None
33 elif length < 4:
34 raise IOError(prefix)
35 prefix = _Instruction(value=prefix, byteorder=byteorder, db=db)
36 if prefix.major != 0x1:
37 return prefix
38
39 suffix = ifile.read(4)
40 length = len(suffix)
41 if length == 0:
42 return prefix
43 elif length < 4:
44 raise IOError(suffix)
45 try:
46 return _SVP64Instruction(prefix=prefix, suffix=suffix,
47 byteorder=byteorder, db=db)
48 except _SVP64Instruction.PrefixError:
49 return _PrefixedInstruction(prefix=prefix, suffix=suffix,
50 byteorder=byteorder, db=db)
51
52 while True:
53 insn = load(ifile)
54 if insn is None:
55 break
56 yield insn
57
58
59 def dump(insns, **_):
60 for insn in insns:
61 yield from insn.disassemble()
62
63
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
73 args = dict(vars(parser.parse_args()))
74 ifile = args["ifile"]
75 ofile = args["ofile"]
76 byteorder = args["byteorder"]
77
78 insns = load(ifile, byteorder)
79 for line in dump(insns):
80 print(line, file=ofile)
81
82
83 if __name__ == "__main__":
84 main()