a5210f087c85f63ead87622ac161fec19554ffd2
6 from openpower
.decoder
.power_enums
import (
9 from openpower
.insndb
.core
import (
20 class Instruction(str):
21 def __new__(cls
, string
):
23 if string
.startswith("sv."):
24 string
= string
[len("sv."):]
26 self
= super().__new
__(cls
, string
)
35 class SVP64Instruction(Instruction
):
36 def __new__(cls
, string
):
37 self
= super().__new
__(cls
, string
)
39 raise ValueError("illegal SVP64 instruction")
43 class ListVisitor(Visitor
):
44 @visitormethod(Record
)
45 def Record(self
, node
):
50 class OpcodesVisitor(Visitor
):
51 @visitormethod(Record
)
52 def Record(self
, node
):
53 for opcode
in node
.opcodes
:
58 class OperandsVisitor(Visitor
):
59 @visitormethod(Record
)
60 def Record(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 @visitormethod(Record
)
73 def Record(self
, node
):
74 if isinstance(node
, Record
):
75 for line
in node
.pcode
:
80 class ExtrasVisitor(Visitor
):
81 @visitormethod(Record
)
82 def Record(self
, node
):
83 for (name
, extra
) in node
.extras
.items():
85 print(" sel", extra
["sel"])
86 print(" reg", extra
["reg"])
87 print(" seltype", extra
["seltype"])
88 print(" idx", extra
["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 records
= next(db
.walk(match
=lambda node
: isinstance(node
, Records
)))
142 if command
in ("list",):
145 insn
= args
.pop("insn")
147 return (isinstance(record
, Record
) and (record
.name
== insn
))
149 for node
in records
.walk(match
=match
):
150 visit(visitor
=visitor
, node
=node
)
153 if __name__
== "__main__":