522d96501184bd9cb42b050253a5e39b4e638713
6 from openpower
.decoder
.power_enums
import (
9 from openpower
.insndb
.core
import (
17 class Instruction(str):
18 def __new__(cls
, string
):
20 if string
.startswith("sv."):
21 string
= string
[len("sv."):]
23 self
= super().__new
__(cls
, string
)
32 class SVP64Instruction(Instruction
):
33 def __new__(cls
, string
):
34 self
= super().__new
__(cls
, string
)
36 raise ValueError("illegal SVP64 instruction")
40 class ListHandler(Handler
):
41 @contextlib.contextmanager
42 def Record(self
, node
, depth
):
47 class InstructionMatcher(Matcher
):
48 def Record(self
, node
, depth
):
49 return (node
.name
== self
["insn"])
52 class SVP64InstructionMatcher(InstructionMatcher
):
56 class OpcodesHandler(Handler
):
57 @contextlib.contextmanager
58 def Record(self
, node
, depth
):
59 for opcode
in node
.opcodes
:
64 class OperandsHandler(Handler
):
65 @contextlib.contextmanager
66 def Record(self
, node
, depth
):
67 for operand
in node
.dynamic_operands
:
68 print(operand
.name
, ",".join(map(str, operand
.span
)))
69 for operand
in node
.static_operands
:
70 if operand
.name
not in ("PO", "XO"):
71 desc
= f
"{operand.name}={operand.value}"
72 print(desc
, ",".join(map(str, operand
.span
)))
76 class PCodeHandler(Handler
):
77 @contextlib.contextmanager
78 def Record(self
, node
, depth
):
79 for line
in node
.pcode
:
84 class ExtrasHandler(Handler
):
85 @contextlib.contextmanager
86 def Extra(self
, node
, depth
):
88 print(" sel", node
.sel
)
89 print(" reg", node
.reg
)
90 print(" seltype", node
.seltype
)
91 print(" idx", node
.idx
)
100 "list available instructions",
105 "print instruction opcodes",
110 "print instruction operands",
115 "print instruction pseudocode",
120 "print instruction extras (SVP64)",
124 main_parser
= argparse
.ArgumentParser()
125 main_parser
.add_argument("-l", "--log",
126 help="activate logging",
129 main_subparser
= main_parser
.add_subparsers(dest
="command", required
=True)
131 for (command
, (handler
, matcher
, help)) in commands
.items():
132 parser
= main_subparser
.add_parser(command
, help=help)
133 if issubclass(matcher
, InstructionMatcher
):
134 if issubclass(matcher
, SVP64InstructionMatcher
):
135 arg_cls
= SVP64Instruction
137 arg_cls
= Instruction
138 parser
.add_argument("insn", type=arg_cls
,
139 metavar
="INSN", help="instruction")
141 args
= vars(main_parser
.parse_args())
142 command
= args
.pop("command")
143 log
= args
.pop("log")
145 os
.environ
["SILENCELOG"] = "true"
146 handler
= commands
[command
][0](**args
)
147 matcher
= commands
[command
][1](**args
)
149 db
= Database(find_wiki_dir())
150 visit(handler
=handler
, matcher
=matcher
, node
=db
)
153 if __name__
== "__main__":