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 BaseVisitor(Visitor
):
30 def __init__(self
, **_
):
34 class ListVisitor(BaseVisitor
):
35 @contextlib.contextmanager
36 def record(self
, record
):
41 class InstructionVisitor(BaseVisitor
):
42 def __init__(self
, insn
, **_
):
44 return super().__init
__()
46 def concrete_record(self
, record
):
47 raise NotImplementedError
49 @contextlib.contextmanager
50 def record(self
, record
):
51 if record
.name
== self
.__insn
:
52 self
.concrete_record(record
=record
)
56 class OpcodesVisitor(InstructionVisitor
):
57 def concrete_record(self
, record
):
58 for opcode
in record
.opcodes
:
62 class OperandsVisitor(InstructionVisitor
):
63 def concrete_record(self
, record
):
64 for operand
in record
.dynamic_operands
:
66 for operand
in record
.static_operands
:
67 if operand
.name
not in ("PO", "XO"):
68 print(operand
.name
, operand
.value
, sep
="=")
71 class PCodeVisitor(InstructionVisitor
):
72 def concrete_record(self
, record
):
73 for line
in record
.pcode
:
77 class ExtrasVisitor(InstructionVisitor
):
78 def concrete_record(self
, record
):
79 for (key
, fields
) in record
.extras
.items():
81 for (field_key
, field_value
) in fields
.items():
82 print(f
" {field_key} {field_value}")
89 "list available instructions",
93 "print instruction opcodes",
97 "print instruction operands",
101 "print instruction pseudocode",
105 "print instruction extras (SVP64)",
109 main_parser
= argparse
.ArgumentParser()
110 main_parser
.add_argument("-l", "--log",
111 help="activate logging",
114 main_subparser
= main_parser
.add_subparsers(dest
="command", required
=True)
116 for (command
, (visitor
, help)) in commands
.items():
117 parser
= main_subparser
.add_parser(command
, help=help)
118 if issubclass(visitor
, InstructionVisitor
):
119 parser
.add_argument("insn", type=Instruction
,
120 metavar
="INSN", help="instruction")
122 args
= vars(main_parser
.parse_args())
123 command
= args
.pop("command")
124 log
= args
.pop("log")
126 os
.environ
["SILENCELOG"] = "true"
127 visitor
= commands
[command
][0](**args
)
129 db
= Database(find_wiki_dir())
130 db
.visit(visitor
=visitor
)
133 if __name__
== "__main__":