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
):
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) or (value
< 0) or (value
> ((1 << 32) - 1)):
32 raise ValueError(value
)
34 return super().__init
__(value
=value
, bits
=32)
37 return f
"{self.__class__.__name__}({self.value:08x})"
39 def disassemble(self
):
40 if self
.dbrecord
is None:
41 yield f
".long 0x{self.value:08x}"
43 yield f
".long 0x{self.value:08x} # {self.dbrecord.name}"
52 return DATABASE
[int(self
)]
57 class PrefixedInstruction(_SelectableInt
):
58 def __init__(self
, prefix
, suffix
, byteorder
=ByteOrder
.LITTLE
):
59 insn
= _functools
.partial(Instruction
, byteorder
=byteorder
)
60 (prefix
, suffix
) = map(insn
, (prefix
, suffix
))
61 value
= ((prefix
.value
<< 32) | suffix
.value
)
62 return super().__init
__(value
=value
, bits
=64)
65 return f
"{self.__class__.__name__}({self.value:016x})"
67 def disassemble(self
):
68 if self
.dbrecord
is None:
69 yield f
".llong 0x{self.value:08x}"
71 yield f
".llong 0x{self.value:08x} # {self.dbrecord.name}"
75 return Instruction(self
[0:32])
79 return Instruction(self
[32:64])
83 return self
.suffix
.dbrecord
86 class SVP64Instruction(PrefixedInstruction
):
87 class PrefixError(ValueError):
90 class Prefix(_SVP64PrefixFields
, Instruction
):
91 class RM(_SVP64RMFields
):
94 return (self
.mode
& 0b11)
98 return self
.__class
__.RM(super().rm
)
100 class Suffix(Instruction
):
103 def __init__(self
, prefix
, suffix
, byteorder
=ByteOrder
.LITTLE
):
104 if SVP64Instruction
.Prefix(prefix
).pid
!= 0b11:
105 raise SVP64Instruction
.PrefixError(prefix
)
106 return super().__init
__(prefix
, suffix
, byteorder
)
108 def disassemble(self
):
109 if self
.dbrecord
is None:
110 yield f
".llong 0x{self.value:08x}"
112 yield f
".llong 0x{self.value:08x} # sv.{self.dbrecord.name}"
116 return self
.__class
__.Prefix(super().prefix
)
120 return self
.__class
__.Suffix(super().suffix
)
123 def load(ifile
, byteorder
, **_
):
125 prefix
= ifile
.read(4)
130 raise IOError(prefix
)
131 prefix
= Instruction(prefix
, byteorder
)
132 if prefix
.major
!= 0x1:
133 return Instruction(prefix
, byteorder
)
135 suffix
= ifile
.read(4)
140 raise IOError(suffix
)
142 return SVP64Instruction(prefix
, suffix
, byteorder
)
143 except SVP64Instruction
.PrefixError
:
144 return PrefixedInstruction(prefix
, suffix
, byteorder
)
153 def dump(insns
, ofile
, **_
):
155 yield from insn
.disassemble()
159 parser
= _argparse
.ArgumentParser()
160 parser
.add_argument("ifile", nargs
="?",
161 type=_argparse
.FileType("rb"), default
=_sys
.stdin
.buffer)
162 parser
.add_argument("ofile", nargs
="?",
163 type=_argparse
.FileType("w"), default
=_sys
.stdout
)
164 parser
.add_argument("-b", "--byteorder",
165 type=ByteOrder
, default
=ByteOrder
.LITTLE
)
167 args
= dict(vars(parser
.parse_args()))
168 ifile
= args
["ifile"]
169 ofile
= args
["ofile"]
170 byteorder
= args
["byteorder"]
172 insns
= load(ifile
, byteorder
)
173 for line
in dump(insns
, byteorder
):
174 print(line
, file=ofile
)
177 if __name__
== "__main__":