1ddb578e7ccb28a63c7e5ad0e83054e5f04330ce
[openpower-isa.git] / src / openpower / insndb / db.py
1 import argparse
2 import contextlib
3 import sys
4
5 from openpower.decoder.power_enums import (
6 find_wiki_dir,
7 )
8 from openpower.insndb.core import (
9 Database,
10 Visitor,
11 )
12
13
14 class BaseVisitor(Visitor):
15 def __init__(self, **_):
16 pass
17
18
19 class ListVisitor(BaseVisitor):
20 @contextlib.contextmanager
21 def record(self, record):
22 print(record.name)
23 yield record
24
25
26 class ConcreteInstructionVisitor(BaseVisitor):
27 def __init__(self, insn, **_):
28 self.__insn = insn
29 return super().__init__()
30
31 def handler(self, record):
32 raise NotImplementedError
33
34 @contextlib.contextmanager
35 def record(self, record):
36 if record.name == self.__insn:
37 self.handler(record=record)
38 yield record
39
40
41 class OpcodesVisitor(ConcreteInstructionVisitor):
42 def handler(self, record):
43 for opcode in record.opcodes:
44 print(opcode)
45
46
47 class OperandsVisitor(ConcreteInstructionVisitor):
48 def handler(self, record):
49 for operand in record.dynamic_operands:
50 print(operand.name)
51 for operand in record.static_operands:
52 if operand.name not in ("PO", "XO"):
53 print(operand.name, operand.value, sep="=")
54
55
56 def main():
57 visitors = {
58 "list": ListVisitor,
59 "opcodes": OpcodesVisitor,
60 "operands": OperandsVisitor,
61 }
62 main_parser = argparse.ArgumentParser()
63 main_subparser = main_parser.add_subparsers(dest="command", required=True)
64 main_subparser.add_parser("list",
65 help="list all instructions")
66
67 for (command, help) in {
68 "opcodes": "print instruction opcodes",
69 "operands": "print instruction operands",
70 }.items():
71 parser = main_subparser.add_parser(command, help=help)
72 parser.add_argument("insn", metavar="INSN", help="instruction")
73
74 args = vars(main_parser.parse_args())
75 command = args.pop("command")
76 visitor = visitors[command](**args)
77
78 db = Database(find_wiki_dir())
79 db.visit(visitor=visitor)