6 from openpower
.decoder
.power_enums
import (
9 from openpower
.insndb
.core
import (
18 class Instruction(str):
19 def __new__(cls
, string
):
21 if string
.startswith("sv."):
22 string
= string
[len("sv."):]
24 self
= super().__new
__(cls
, string
)
33 class SVP64Instruction(Instruction
):
34 def __new__(cls
, string
):
35 self
= super().__new
__(cls
, string
)
37 raise ValueError("illegal SVP64 instruction")
41 class ListVisitor(Visitor
):
42 @contextlib.contextmanager
43 def __call__(self
, node
):
44 if isinstance(node
, Record
):
49 class OpcodesVisitor(Visitor
):
50 @contextlib.contextmanager
51 def __call__(self
, node
):
52 if isinstance(node
, Record
):
53 for opcode
in node
.opcodes
:
58 class OperandsVisitor(Visitor
):
59 @contextlib.contextmanager
60 def __call__(self
, node
):
61 if isinstance(node
, Record
):
62 for operand
in node
.dynamic_operands
:
63 print(operand
.name
, ",".join(map(str, operand
.span
)))
64 for operand
in node
.static_operands
:
65 if operand
.name
not in ("PO", "XO"):
66 desc
= f
"{operand.name}={operand.value}"
67 print(desc
, ",".join(map(str, operand
.span
)))
71 class PCodeVisitor(Visitor
):
72 @contextlib.contextmanager
73 def __call__(self
, node
):
74 if isinstance(node
, Record
):
75 for line
in node
.pcode
:
80 class ExtrasVisitor(Visitor
):
81 @contextlib.contextmanager
82 def __call__(self
, node
):
83 if isinstance(node
, Extra
):
85 print(" sel", node
.sel
)
86 print(" reg", node
.reg
)
87 print(" seltype", node
.seltype
)
88 print(" idx", node
.idx
)
96 "list available instructions",
100 "print instruction opcodes",
104 "print instruction operands",
108 "print instruction pseudocode",
112 "print instruction extras (SVP64)",
116 main_parser
= argparse
.ArgumentParser()
117 main_parser
.add_argument("-l", "--log",
118 help="activate logging",
121 main_subparser
= main_parser
.add_subparsers(dest
="command", required
=True)
123 for (command
, (visitor
, helper
)) in commands
.items():
124 parser
= main_subparser
.add_parser(command
, help=helper
)
125 if command
not in ("list",):
126 if command
in ("extras",):
127 arg_cls
= SVP64Instruction
129 arg_cls
= Instruction
130 parser
.add_argument("insn", type=arg_cls
,
131 metavar
="INSN", help="instruction")
133 args
= vars(main_parser
.parse_args())
134 command
= args
.pop("command")
135 log
= args
.pop("log")
137 os
.environ
["SILENCELOG"] = "true"
138 visitor
= commands
[command
][0]()
140 db
= Database(find_wiki_dir())
141 if command
in ("list",):
144 insn
= args
.pop("insn")
146 return (isinstance(record
, Record
) and (record
.name
== insn
))
148 for node
in db
.subnodes(match
=match
):
149 visit(visitor
=visitor
, node
=node
)
152 if __name__
== "__main__":