1 import argparse
as _argparse
3 import functools
as _functools
6 from openpower
.decoder
.power_enums
import find_wiki_dir
as _find_wiki_dir
7 from openpower
.decoder
.power_insn
import Database
as _Database
8 from openpower
.decoder
.selectable_int
import SelectableInt
as _SelectableInt
9 from openpower
.decoder
.isa
.caller
import SVP64PrefixFields
as _SVP64PrefixFields
10 from openpower
.decoder
.isa
.caller
import SVP64RMFields
as _SVP64RMFields
13 class ByteOrder(_enum
.Enum
):
18 return self
.name
.lower()
21 DATABASE
= _Database(_find_wiki_dir())
24 class Instruction(_SelectableInt
):
25 def __init__(self
, value
, byteorder
=ByteOrder
.LITTLE
, bits
=32):
26 if isinstance(value
, _SelectableInt
):
28 elif isinstance(value
, bytes
):
29 value
= int.from_bytes(value
, byteorder
=str(byteorder
))
31 if not isinstance(value
, int):
32 raise ValueError(value
)
33 if not isinstance(bits
, int) or (bits
not in {32, 64}):
34 raise ValueError(bits
)
36 return super().__init
__(value
=value
, bits
=bits
)
39 return f
"{self.__class__.__name__}({self.value:08x})"
41 def disassemble(self
):
42 if self
.dbrecord
is None:
43 yield f
".long 0x{self.value:08x}"
45 yield f
".long 0x{self.value:08x} # {self.dbrecord.name}"
54 return DATABASE
[int(self
)]
59 class PrefixedInstruction(Instruction
):
60 def __init__(self
, prefix
, suffix
, byteorder
=ByteOrder
.LITTLE
):
61 insn
= _functools
.partial(Instruction
, byteorder
=byteorder
)
62 (prefix
, suffix
) = map(insn
, (prefix
, suffix
))
63 value
= ((prefix
.value
<< 32) | suffix
.value
)
64 return super().__init
__(value
=value
, bits
=64)
67 return f
"{self.__class__.__name__}({self.value:016x})"
69 def disassemble(self
):
70 if self
.dbrecord
is None:
71 yield f
".llong 0x{self.value:08x}"
73 yield f
".llong 0x{self.value:08x} # {self.dbrecord.name}"
77 return Instruction(self
[0:32])
81 return Instruction(self
[32:64])
85 return self
.suffix
.major
89 return self
.suffix
.dbrecord
92 class SVP64Instruction(PrefixedInstruction
):
93 class PrefixError(ValueError):
96 class Prefix(_SVP64PrefixFields
, Instruction
):
97 class RM(_SVP64RMFields
):
100 return (self
.mode
& 0b11)
104 return self
.__class
__.RM(super().rm
)
106 class Suffix(Instruction
):
109 def __init__(self
, prefix
, suffix
, byteorder
=ByteOrder
.LITTLE
):
110 if SVP64Instruction
.Prefix(prefix
).pid
!= 0b11:
111 raise SVP64Instruction
.PrefixError(prefix
)
112 return super().__init
__(prefix
, suffix
, byteorder
)
114 def disassemble(self
):
115 if self
.dbrecord
is None:
116 yield f
".llong 0x{self.value:08x}"
118 yield f
".llong 0x{self.value:08x} # sv.{self.dbrecord.name}"
122 return self
.__class
__.Prefix(super().prefix
)
126 return self
.__class
__.Suffix(super().suffix
)
129 def load(ifile
, byteorder
, **_
):
131 prefix
= ifile
.read(4)
136 raise IOError(prefix
)
137 prefix
= Instruction(prefix
, byteorder
)
138 if prefix
.major
!= 0x1:
139 return Instruction(prefix
, byteorder
)
141 suffix
= ifile
.read(4)
146 raise IOError(suffix
)
148 return SVP64Instruction(prefix
, suffix
, byteorder
)
149 except SVP64Instruction
.PrefixError
:
150 return PrefixedInstruction(prefix
, suffix
, byteorder
)
159 def dump(insns
, ofile
, **_
):
161 yield from insn
.disassemble()
165 parser
= _argparse
.ArgumentParser()
166 parser
.add_argument("ifile", nargs
="?",
167 type=_argparse
.FileType("rb"), default
=_sys
.stdin
.buffer)
168 parser
.add_argument("ofile", nargs
="?",
169 type=_argparse
.FileType("w"), default
=_sys
.stdout
)
170 parser
.add_argument("-b", "--byteorder",
171 type=ByteOrder
, default
=ByteOrder
.LITTLE
)
173 args
= dict(vars(parser
.parse_args()))
174 ifile
= args
["ifile"]
175 ofile
= args
["ofile"]
176 byteorder
= args
["byteorder"]
178 insns
= load(ifile
, byteorder
)
179 for line
in dump(insns
, byteorder
):
180 print(line
, file=ofile
)
183 if __name__
== "__main__":