a9fd349038534fa18c6cf0ac4f930ebbc551d63e
[openpower-isa.git] / src / openpower / insndb / disasm.py
1 import argparse
2 import enum
3 import sys
4 import os
5
6 from openpower.decoder.power_enums import (
7 find_wiki_dir,
8 )
9 from openpower.insndb.core import (
10 Style,
11 Database,
12 WordInstruction,
13 PrefixedInstruction,
14 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, style, **_):
55 db = Database(find_wiki_dir())
56 for insn in insns:
57 record = db[insn]
58 yield from insn.disassemble(record=record, style=style)
59
60
61 # this is the entry-point for the console-script pysvp64dis
62 def main():
63 parser = argparse.ArgumentParser()
64 parser.add_argument("ifile", nargs="?",
65 type=argparse.FileType("rb"), default=sys.stdin.buffer)
66 parser.add_argument("ofile", nargs="?",
67 type=argparse.FileType("w"), default=sys.stdout)
68 parser.add_argument("-b", "--byteorder",
69 type=ByteOrder, default=ByteOrder.LITTLE)
70 parser.add_argument("-s", "--short",
71 dest="style", default=Style.NORMAL,
72 action="store_const", const=Style.SHORT)
73 parser.add_argument("-v", "--verbose",
74 dest="style", default=Style.NORMAL,
75 action="store_const", const=Style.VERBOSE)
76 parser.add_argument("-l", "--legacy",
77 dest="style", default=Style.NORMAL,
78 action="store_const", const=Style.LEGACY)
79 parser.add_argument("-L", "--log",
80 action="store_true", default=False)
81
82 args = dict(vars(parser.parse_args()))
83
84 # if logging requested do not disable it.
85 if not args['log']:
86 os.environ['SILENCELOG'] = '1'
87
88 # load instructions and dump them
89 insns = load(**args)
90 for line in dump(insns, **args):
91 print(line, file=args["ofile"])
92
93
94 # still here but use "python3 setup.py develop" then run the
95 # command "pysvp64dis" instead of "python3 src/openpower/sv/trans/pysvp64dis.py"
96 if __name__ == "__main__":
97 main()