5 from openpower
.decoder
.power_enums
import (
8 from openpower
.insndb
.core
import (
14 class Instruction(str):
15 def __new__(cls
, string
):
17 if string
.startswith("sv."):
18 string
= string
[len("sv."):]
20 self
= super().__new
__(cls
, string
)
29 class BaseVisitor(Visitor
):
30 def __init__(self
, **_
):
34 class ListVisitor(BaseVisitor
):
35 @contextlib.contextmanager
36 def record(self
, record
):
41 class InstructionVisitor(BaseVisitor
):
42 def __init__(self
, insn
, **_
):
44 return super().__init
__()
46 def concrete_record(self
, record
):
47 raise NotImplementedError
49 @contextlib.contextmanager
50 def record(self
, record
):
51 if record
.name
== self
.__insn
:
52 self
.concrete_record(record
=record
)
56 class OpcodesVisitor(InstructionVisitor
):
57 def concrete_record(self
, record
):
58 for opcode
in record
.opcodes
:
62 class OperandsVisitor(InstructionVisitor
):
63 def concrete_record(self
, record
):
64 for operand
in record
.dynamic_operands
:
66 for operand
in record
.static_operands
:
67 if operand
.name
not in ("PO", "XO"):
68 print(operand
.name
, operand
.value
, sep
="=")
71 class PCodeVisitor(InstructionVisitor
):
72 def concrete_record(self
, record
):
73 for line
in record
.pcode
:
81 "list available instructions",
85 "print instruction opcodes",
89 "print instruction operands",
93 "print instruction pseudocode",
97 main_parser
= argparse
.ArgumentParser()
98 main_parser
.add_argument("-l", "--log",
99 help="activate logging",
102 main_subparser
= main_parser
.add_subparsers(dest
="command", required
=True)
104 for (command
, (visitor
, help)) in commands
.items():
105 parser
= main_subparser
.add_parser(command
, help=help)
106 if issubclass(visitor
, InstructionVisitor
):
107 parser
.add_argument("insn", type=Instruction
,
108 metavar
="INSN", help="instruction")
110 args
= vars(main_parser
.parse_args())
111 command
= args
.pop("command")
112 log
= args
.pop("log")
114 os
.environ
["SILENCELOG"] = "true"
115 visitor
= commands
[command
][0](**args
)
117 db
= Database(find_wiki_dir())
118 db
.visit(visitor
=visitor
)
121 if __name__
== "__main__":