707bdaaf1f8c24c51b680048024cd97c95bfcab4
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 SVP64Instruction(Instruction
):
30 def __new__(cls
, string
):
31 self
= super().__new
__(cls
, string
)
33 raise ValueError("illegal SVP64 instruction")
37 class BaseVisitor(Visitor
):
38 def __init__(self
, **_
):
42 class ListVisitor(BaseVisitor
):
43 @contextlib.contextmanager
44 def record(self
, record
):
49 class InstructionVisitor(BaseVisitor
):
50 def __init__(self
, insn
, **_
):
52 return super().__init
__()
54 def concrete_record(self
, record
):
55 raise NotImplementedError
57 @contextlib.contextmanager
58 def record(self
, record
):
59 if record
.name
== self
.__insn
:
60 self
.concrete_record(record
=record
)
64 class SVP64InstructionVisitor(InstructionVisitor
):
68 class OpcodesVisitor(InstructionVisitor
):
69 def concrete_record(self
, record
):
70 for opcode
in record
.opcodes
:
74 class OperandsVisitor(InstructionVisitor
):
75 def concrete_record(self
, record
):
76 for operand
in record
.dynamic_operands
:
78 for operand
in record
.static_operands
:
79 if operand
.name
not in ("PO", "XO"):
80 print(operand
.name
, operand
.value
, sep
="=")
83 class PCodeVisitor(InstructionVisitor
):
84 def concrete_record(self
, record
):
85 for line
in record
.pcode
:
89 class ExtrasVisitor(SVP64InstructionVisitor
):
90 def concrete_record(self
, record
):
91 for (key
, fields
) in record
.extras
.items():
93 for (field_key
, field_value
) in fields
.items():
94 print(f
" {field_key} {field_value}")
101 "list available instructions",
105 "print instruction opcodes",
109 "print instruction operands",
113 "print instruction pseudocode",
117 "print instruction extras (SVP64)",
121 main_parser
= argparse
.ArgumentParser()
122 main_parser
.add_argument("-l", "--log",
123 help="activate logging",
126 main_subparser
= main_parser
.add_subparsers(dest
="command", required
=True)
128 for (command
, (visitor
, help)) in commands
.items():
129 parser
= main_subparser
.add_parser(command
, help=help)
130 if issubclass(visitor
, InstructionVisitor
):
131 if issubclass(visitor
, SVP64InstructionVisitor
):
132 arg_cls
= SVP64Instruction
134 arg_cls
= Instruction
135 parser
.add_argument("insn", type=arg_cls
,
136 metavar
="INSN", help="instruction")
138 args
= vars(main_parser
.parse_args())
139 command
= args
.pop("command")
140 log
= args
.pop("log")
142 os
.environ
["SILENCELOG"] = "true"
143 visitor
= commands
[command
][0](**args
)
145 db
= Database(find_wiki_dir())
146 db
.visit(visitor
=visitor
)
149 if __name__
== "__main__":