6 from openpower
.decoder
.power_enums
import (
9 from openpower
.insndb
.core
import (
19 class Instruction(str):
20 def __new__(cls
, string
):
22 if string
.startswith("sv."):
23 string
= string
[len("sv."):]
25 self
= super().__new
__(cls
, string
)
34 class SVP64Instruction(Instruction
):
35 def __new__(cls
, string
):
36 self
= super().__new
__(cls
, string
)
38 raise ValueError("illegal SVP64 instruction")
42 class ListVisitor(Visitor
):
43 @contextlib.contextmanager
44 def __call__(self
, node
):
45 if isinstance(node
, Record
):
50 class OpcodesVisitor(Visitor
):
51 @contextlib.contextmanager
52 def __call__(self
, node
):
53 if isinstance(node
, Record
):
54 for opcode
in node
.opcodes
:
59 class OperandsVisitor(Visitor
):
60 @contextlib.contextmanager
61 def __call__(self
, node
):
62 if isinstance(node
, Record
):
63 for operand
in node
.dynamic_operands
:
64 print(operand
.name
, ",".join(map(str, operand
.span
)))
65 for operand
in node
.static_operands
:
66 if operand
.name
not in ("PO", "XO"):
67 desc
= f
"{operand.name}={operand.value}"
68 print(desc
, ",".join(map(str, operand
.span
)))
72 class PCodeVisitor(Visitor
):
73 @contextlib.contextmanager
74 def __call__(self
, node
):
75 if isinstance(node
, Record
):
76 for line
in node
.pcode
:
81 class ExtrasVisitor(Visitor
):
82 @contextlib.contextmanager
83 def __call__(self
, node
):
84 if isinstance(node
, Extra
):
86 print(" sel", node
.sel
)
87 print(" reg", node
.reg
)
88 print(" seltype", node
.seltype
)
89 print(" idx", node
.idx
)
97 "list available instructions",
101 "print instruction opcodes",
105 "print instruction operands",
109 "print instruction pseudocode",
113 "print instruction extras (SVP64)",
117 main_parser
= argparse
.ArgumentParser()
118 main_parser
.add_argument("-l", "--log",
119 help="activate logging",
122 main_subparser
= main_parser
.add_subparsers(dest
="command", required
=True)
124 for (command
, (visitor
, helper
)) in commands
.items():
125 parser
= main_subparser
.add_parser(command
, help=helper
)
126 if command
not in ("list",):
127 if command
in ("extras",):
128 arg_cls
= SVP64Instruction
130 arg_cls
= Instruction
131 parser
.add_argument("insn", type=arg_cls
,
132 metavar
="INSN", help="instruction")
134 args
= vars(main_parser
.parse_args())
135 command
= args
.pop("command")
136 log
= args
.pop("log")
138 os
.environ
["SILENCELOG"] = "true"
139 visitor
= commands
[command
][0]()
141 db
= Database(find_wiki_dir())
142 records
= next(db
.walk(match
=lambda node
: isinstance(node
, Records
)))
143 if command
in ("list",):
146 insn
= args
.pop("insn")
148 return (isinstance(record
, Record
) and (record
.name
== insn
))
150 for node
in records
.walk(match
=match
):
151 visit(visitor
=visitor
, node
=node
)
154 if __name__
== "__main__":