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 TreeVisitor(Visitor
):
45 return super().__init
__()
47 @contextlib.contextmanager
48 def __call__(self
, node
):
49 with
super().__call
__(node
) as node
:
50 print((" " * (self
.__depth
* 4)), repr(node
))
56 class ListVisitor(Visitor
):
57 @visitormethod(Record
)
58 def Record(self
, node
):
63 # No use other than checking issubclass and adding an argument.
64 class InstructionVisitor(Visitor
):
67 class SVP64InstructionVisitor(InstructionVisitor
):
71 class OpcodesVisitor(InstructionVisitor
):
72 @visitormethod(Record
)
73 def Record(self
, node
):
74 for opcode
in node
.opcodes
:
79 class OperandsVisitor(InstructionVisitor
):
80 @visitormethod(Record
)
81 def Record(self
, node
):
82 if isinstance(node
, Record
):
83 for operand
in node
.dynamic_operands
:
84 print(operand
.name
, ",".join(map(str, operand
.span
)))
85 for operand
in node
.static_operands
:
86 if operand
.name
not in ("PO", "XO"):
87 desc
= f
"{operand.name}={operand.value}"
88 print(desc
, ",".join(map(str, operand
.span
)))
92 class PCodeVisitor(InstructionVisitor
):
93 @visitormethod(Record
)
94 def Record(self
, node
):
95 if isinstance(node
, Record
):
96 for line
in node
.pcode
:
101 class ExtrasVisitor(SVP64InstructionVisitor
):
102 @visitormethod(Record
)
103 def Record(self
, node
):
104 for (name
, extra
) in node
.extras
.items():
106 print(" sel", extra
["sel"])
107 print(" reg", extra
["reg"])
108 print(" seltype", extra
["seltype"])
109 print(" idx", extra
["idx"])
121 "list available instructions",
125 "print instruction opcodes",
129 "print instruction operands",
133 "print instruction pseudocode",
137 "print instruction extras (SVP64)",
141 main_parser
= argparse
.ArgumentParser()
142 main_parser
.add_argument("-l", "--log",
143 help="activate logging",
146 main_subparser
= main_parser
.add_subparsers(dest
="command", required
=True)
148 for (command
, (visitor
, helper
)) in commands
.items():
149 parser
= main_subparser
.add_parser(command
, help=helper
)
150 if issubclass(visitor
, InstructionVisitor
):
151 if command
in ("extras",):
152 arg_cls
= SVP64Instruction
154 arg_cls
= Instruction
155 parser
.add_argument("insn", type=arg_cls
,
156 metavar
="INSN", help="instruction")
158 args
= vars(main_parser
.parse_args())
159 command
= args
.pop("command")
160 log
= args
.pop("log")
162 os
.environ
["SILENCELOG"] = "true"
163 visitor
= commands
[command
][0]()
165 db
= Database(find_wiki_dir())
166 records
= next(db
.walk(match
=lambda node
: isinstance(node
, Records
)))
167 if not isinstance(visitor
, InstructionVisitor
):
170 insn
= args
.pop("insn")
172 return (isinstance(record
, Record
) and (record
.name
== insn
))
174 for node
in records
.walk(match
=match
):
175 visit(visitor
=visitor
, node
=node
)
178 if __name__
== "__main__":