3247a26d1259d7806ceaf198388f13ad0130f9c3
5 from openpower
.decoder
.power_enums
import (
8 from openpower
.insndb
.core
import (
14 class Instruction(str):
15 def __new__(cls
, string
):
17 if string
.startswith("sv."):
18 string
= string
[len("sv."):]
20 self
= super().__new
__(cls
, string
)
29 class SVP64Instruction(Instruction
):
30 def __new__(cls
, string
):
31 self
= super().__new
__(cls
, string
)
33 raise ValueError("illegal SVP64 instruction")
37 class BaseVisitor(Visitor
):
38 def __init__(self
, **_
):
42 class ListVisitor(BaseVisitor
):
43 @contextlib.contextmanager
44 def record(self
, record
):
49 class InstructionVisitor(BaseVisitor
):
50 def __init__(self
, insn
, **_
):
52 return super().__init
__()
54 def concrete_record(self
, record
):
55 raise NotImplementedError
57 @contextlib.contextmanager
58 def record(self
, record
):
59 if record
.name
== self
.__insn
:
60 self
.concrete_record(record
=record
)
64 class SVP64InstructionVisitor(InstructionVisitor
):
68 class OpcodesVisitor(InstructionVisitor
):
69 def concrete_record(self
, record
):
70 for opcode
in record
.opcodes
:
74 class OperandsVisitor(InstructionVisitor
):
75 def concrete_record(self
, record
):
76 for operand
in record
.dynamic_operands
:
77 print(operand
.name
, ",".join(map(str, operand
.span
)))
78 for operand
in record
.static_operands
:
79 if operand
.name
not in ("PO", "XO"):
80 desc
= f
"{operand.name}={operand.value}"
81 print(desc
, ",".join(map(str, operand
.span
)))
84 class PCodeVisitor(InstructionVisitor
):
85 def concrete_record(self
, record
):
86 for line
in record
.pcode
:
90 class ExtrasVisitor(SVP64InstructionVisitor
):
91 def concrete_record(self
, record
):
92 for (key
, fields
) in record
.extras
.items():
94 for (field_key
, field_value
) in fields
.items():
95 print(f
" {field_key} {field_value}")
102 "list available instructions",
106 "print instruction opcodes",
110 "print instruction operands",
114 "print instruction pseudocode",
118 "print instruction extras (SVP64)",
122 main_parser
= argparse
.ArgumentParser()
123 main_parser
.add_argument("-l", "--log",
124 help="activate logging",
127 main_subparser
= main_parser
.add_subparsers(dest
="command", required
=True)
129 for (command
, (visitor
, help)) in commands
.items():
130 parser
= main_subparser
.add_parser(command
, help=help)
131 if issubclass(visitor
, InstructionVisitor
):
132 if issubclass(visitor
, SVP64InstructionVisitor
):
133 arg_cls
= SVP64Instruction
135 arg_cls
= Instruction
136 parser
.add_argument("insn", type=arg_cls
,
137 metavar
="INSN", help="instruction")
139 args
= vars(main_parser
.parse_args())
140 command
= args
.pop("command")
141 log
= args
.pop("log")
143 os
.environ
["SILENCELOG"] = "true"
144 visitor
= commands
[command
][0](**args
)
146 db
= Database(find_wiki_dir())
147 db
.visit(visitor
=visitor
)
150 if __name__
== "__main__":