6 from openpower
.decoder
.power_enums
import (
9 from openpower
.insndb
.core
import (
15 class Instruction(str):
16 def __new__(cls
, string
):
18 if string
.startswith("sv."):
19 string
= string
[len("sv."):]
21 self
= super().__new
__(cls
, string
)
30 class SVP64Instruction(Instruction
):
31 def __new__(cls
, string
):
32 self
= super().__new
__(cls
, string
)
34 raise ValueError("illegal SVP64 instruction")
38 class BaseVisitor(Visitor
):
39 def __init__(self
, **arguments
):
40 self
.__arguments
= types
.MappingProxyType(arguments
)
41 self
.__current
_db
= None
42 self
.__current
_record
= None
43 self
.__current
_extra
= None
44 return super().__init
__()
48 return self
.__arguments
52 return self
.__current
_db
55 def current_record(self
):
56 return self
.__current
_record
59 def current_extra(self
):
60 return self
.__current
_extra
62 @contextlib.contextmanager
64 self
.__current
_db
= db
66 self
.__current
_db
= None
68 @contextlib.contextmanager
69 def record(self
, record
):
70 self
.__current
_record
= record
72 self
.__current
_record
= None
74 @contextlib.contextmanager
75 def extra(self
, extra
):
76 self
.__current
_extra
= extra
78 self
.__current
_extra
= None
81 class ListVisitor(BaseVisitor
):
82 @contextlib.contextmanager
83 def record(self
, record
):
88 class InstructionVisitor(BaseVisitor
):
92 class SVP64InstructionVisitor(InstructionVisitor
):
96 class OpcodesVisitor(InstructionVisitor
):
97 @contextlib.contextmanager
98 def record(self
, record
):
99 for opcode
in record
.opcodes
:
103 class OperandsVisitor(InstructionVisitor
):
104 @contextlib.contextmanager
105 def record(self
, record
):
106 with
super().record(record
=record
):
107 if self
.current_record
.name
== self
.arguments
["insn"]:
108 for operand
in record
.dynamic_operands
:
109 print(operand
.name
, ",".join(map(str, operand
.span
)))
110 for operand
in record
.static_operands
:
111 if operand
.name
not in ("PO", "XO"):
112 desc
= f
"{operand.name}={operand.value}"
113 print(desc
, ",".join(map(str, operand
.span
)))
118 class PCodeVisitor(InstructionVisitor
):
119 @contextlib.contextmanager
120 def record(self
, record
):
121 with
super().record(record
=record
):
122 if self
.current_record
.name
== self
.arguments
["insn"]:
123 for line
in record
.pcode
:
127 class ExtrasVisitor(SVP64InstructionVisitor
):
128 @contextlib.contextmanager
129 def extra(self
, extra
):
130 with
super().extra(extra
=extra
) as extra
:
131 if self
.current_record
.name
== self
.arguments
["insn"]:
133 print(" sel", extra
.sel
)
134 print(" reg", extra
.reg
)
135 print(" seltype", extra
.seltype
)
136 print(" idx", extra
.idx
)
146 "list available instructions",
150 "print instruction opcodes",
154 "print instruction operands",
158 "print instruction pseudocode",
162 "print instruction extras (SVP64)",
166 main_parser
= argparse
.ArgumentParser()
167 main_parser
.add_argument("-l", "--log",
168 help="activate logging",
171 main_subparser
= main_parser
.add_subparsers(dest
="command", required
=True)
173 for (command
, (visitor
, help)) in commands
.items():
174 parser
= main_subparser
.add_parser(command
, help=help)
175 if issubclass(visitor
, InstructionVisitor
):
176 if issubclass(visitor
, SVP64InstructionVisitor
):
177 arg_cls
= SVP64Instruction
179 arg_cls
= Instruction
180 parser
.add_argument("insn", type=arg_cls
,
181 metavar
="INSN", help="instruction")
183 args
= vars(main_parser
.parse_args())
184 command
= args
.pop("command")
185 log
= args
.pop("log")
187 os
.environ
["SILENCELOG"] = "true"
188 visitor
= commands
[command
][0](**args
)
190 db
= Database(find_wiki_dir())
191 db
.visit(visitor
=visitor
)
194 if __name__
== "__main__":