From d403ecea8eca44b2a2a8df36ce4c5ffae3ac3b84 Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Sat, 3 Jun 2023 12:59:09 +0300 Subject: [PATCH] insndb/db: support opcodes command --- src/openpower/insndb/db.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/openpower/insndb/db.py b/src/openpower/insndb/db.py index 96d881c5..753ab626 100644 --- a/src/openpower/insndb/db.py +++ b/src/openpower/insndb/db.py @@ -12,21 +12,45 @@ from openpower.insndb.types import ( def main(): - class ListVisitor(Visitor): + class GenericVisitor(Visitor): + def __init__(self, **_): + pass + + class ListVisitor(GenericVisitor): @contextlib.contextmanager def record(self, record): print(record.name) yield record + class OpcodesVisitor(GenericVisitor): + def __init__(self, insn, **_): + self.__insn = insn + return super().__init__() + + @contextlib.contextmanager + def record(self, record): + if record.name == self.__insn: + for opcode in record.opcodes: + print(opcode) + yield record + visitors = { "list": ListVisitor, + "opcodes": OpcodesVisitor, } parser = argparse.ArgumentParser() subparser = parser.add_subparsers(dest="command", required=True) - parser_list = subparser.add_parser("list") + parser_list = subparser.add_parser("list", + help="list all instructions") + parser_opcodes = subparser.add_parser("opcodes", + help="print instruction opcodes") + parser_opcodes.add_argument("insn", + metavar="INSN", + help="instruction") + args = vars(parser.parse_args()) command = args.pop("command") - visitor = visitors[command]() + visitor = visitors[command](**args) db = Database(find_wiki_dir()) db.visit(visitor=visitor) -- 2.30.2