9 from openpower
.decoder
.power_enums
import (
12 from openpower
.insndb
.core
import (
21 from openpower
.decoder
.power_enums
import (
35 class Instruction(str):
36 def __new__(cls
, string
):
38 if string
.startswith("sv."):
39 string
= string
[len("sv."):]
41 self
= super().__new
__(cls
, string
)
50 class SVP64Instruction(Instruction
):
51 def __new__(cls
, string
):
52 self
= super().__new
__(cls
, string
)
54 raise ValueError("illegal SVP64 instruction")
58 class ListVisitor(mdis
.visitor
.ContextVisitor
):
59 @mdis.dispatcher
.Hook(Record
)
60 @contextlib.contextmanager
61 def dispatch_record(self
, node
):
66 # No use other than checking issubclass and adding an argument.
67 class InstructionVisitor(mdis
.visitor
.ContextVisitor
):
70 class SVP64InstructionVisitor(InstructionVisitor
):
74 class OpcodesVisitor(InstructionVisitor
):
75 @mdis.dispatcher
.Hook(Record
)
76 @contextlib.contextmanager
77 def dispatch_record(self
, node
):
78 for opcode
in node
.opcodes
:
83 class OperandsVisitor(InstructionVisitor
):
86 return super().__init
__()
88 @mdis.dispatcher
.Hook(Record
)
89 @contextlib.contextmanager
90 def dispatch_record(self
, node
):
94 @mdis.dispatcher
.Hook(Operands
)
95 @contextlib.contextmanager
96 def dispatch_operands(self
, node
):
97 for (cls
, kwargs
) in node
:
98 operand
= cls(record
=self
.__record
, **kwargs
)
99 print(operand
.name
, ", ".join(map(str, operand
.span
)))
103 class PCodeVisitor(InstructionVisitor
):
104 @mdis.dispatcher
.Hook(PCode
)
105 @contextlib.contextmanager
106 def dispatch_record(self
, node
):
112 class SelectorsVisitor(InstructionVisitor
):
113 @mdis.dispatcher
.Hook(
114 In1Sel
, In2Sel
, In3Sel
, CRInSel
, CRIn2Sel
,
117 @contextlib.contextmanager
118 def dispatch_selector(self
, node
):
119 typename
= node
.__class
__.__name
__
120 typename
= typename
.replace("CR", "CR_")
121 typename
= typename
.replace("Sel", "")
122 typename
= typename
.lower()
123 print(typename
, node
)
127 class ETypeVisitor(SVP64InstructionVisitor
):
128 @mdis.dispatcher
.Hook(SVEType
)
129 @contextlib.contextmanager
130 def dispatch_ptype(self
, node
):
135 class PTypeVisitor(SVP64InstructionVisitor
):
136 @mdis.dispatcher
.Hook(SVPType
)
137 @contextlib.contextmanager
138 def dispatch_ptype(self
, node
):
143 class SectionVisitor(InstructionVisitor
):
144 @mdis.dispatcher
.Hook(Section
.Path
)
145 @contextlib.contextmanager
146 def dispatch_path(self
, node
):
150 @mdis.dispatcher
.Hook(Section
.BitSel
)
151 @contextlib.contextmanager
152 def dispatch_bitsel(self
, node
):
153 print("bitsel", node
)
156 @mdis.dispatcher
.Hook(Section
.Suffix
)
157 @contextlib.contextmanager
158 def dispatch_suffix(self
, node
):
159 print("suffix", node
)
162 @mdis.dispatcher
.Hook(Section
.Mode
)
163 @contextlib.contextmanager
164 def dispatch_mode(self
, node
):
168 @mdis.dispatcher
.Hook(Section
.Opcode
)
169 @contextlib.contextmanager
170 def dispatch_opcode(self
, node
):
171 print("opcode", int(node
))
174 @mdis.dispatcher
.Hook(Section
.Priority
)
175 @contextlib.contextmanager
176 def dispatch_priority(self
, node
):
177 print("priority", node
)
181 class ExtrasVisitor(SVP64InstructionVisitor
, SelectorsVisitor
):
182 @mdis.dispatcher
.Hook(SVP64Record
.ExtraMap
)
183 @contextlib.contextmanager
184 def dispatch_extramap(self
, node
):
188 @mdis.dispatcher
.Hook(SVP64Record
.ExtraMap
.Extra
)
189 @contextlib.contextmanager
190 def dispatch_extramap_extra(self
, node
):
194 @mdis.dispatcher
.Hook(SVP64Record
.ExtraMap
.Extra
.Entry
)
195 @contextlib.contextmanager
196 def dispatch_extramap_extra_entry(self
, node
):
203 print(idxmap
[self
.__index
], node
)
211 "list available instructions",
215 "print instruction opcodes",
219 "print instruction operands",
223 "print instruction pseudocode",
227 "print instruction selectors",
231 "print instruction etype",
235 "print instruction ptype",
239 "print instruction section",
243 "print instruction extras (SVP64)",
247 main_parser
= argparse
.ArgumentParser()
248 main_parser
.add_argument("-l", "--log",
249 help="activate logging",
252 main_subparser
= main_parser
.add_subparsers(dest
="command", required
=True)
254 for (command
, (visitor
, helper
)) in commands
.items():
255 parser
= main_subparser
.add_parser(command
, help=helper
)
256 if issubclass(visitor
, InstructionVisitor
):
257 if issubclass(visitor
, SVP64InstructionVisitor
):
258 arg_cls
= SVP64Instruction
260 arg_cls
= Instruction
261 parser
.add_argument("insn", type=arg_cls
,
262 metavar
="INSN", help="instruction")
264 args
= vars(main_parser
.parse_args())
265 command
= args
.pop("command")
266 log
= args
.pop("log")
268 os
.environ
["SILENCELOG"] = "true"
269 visitor
= commands
[command
][0]()
271 db
= Database(find_wiki_dir())
272 if not isinstance(visitor
, InstructionVisitor
):
275 root
= db
[args
.pop("insn")]
277 def traverse(root
, visitor
, walker
):
279 for (node
, *_
) in walker(root
):
280 traverse(root
=node
, visitor
=visitor
, walker
=walker
)
282 traverse(root
=root
, visitor
=visitor
, walker
=Walker())
285 if __name__
== "__main__":