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 from functools
import cached_property
15 from cached_property
import cached_property
18 class ByteOrder(_enum
.Enum
):
23 return self
.name
.lower()
26 DATABASE
= _Database(_find_wiki_dir())
29 class Instruction(_SelectableInt
):
30 def __init__(self
, value
, byteorder
=ByteOrder
.LITTLE
):
31 if isinstance(value
, Instruction
):
33 elif isinstance(value
, bytes
):
34 value
= int.from_bytes(value
, byteorder
=str(byteorder
))
36 if not isinstance(value
, int) or (value
< 0) or (value
> ((1 << 32) - 1)):
37 raise ValueError(value
)
39 return super().__init
__(value
=value
, bits
=32)
42 return f
"{self.__class__.__name__}({self.value:08x})"
45 return f
".long 0x{self.value:08x}"
52 class PrefixedInstruction(_SelectableInt
):
53 def __init__(self
, prefix
, suffix
, byteorder
=ByteOrder
.LITTLE
):
54 insn
= _functools
.partial(Instruction
, byteorder
=byteorder
)
55 (prefix
, suffix
) = map(insn
, (prefix
, suffix
))
56 value
= ((prefix
.value
<< 32) | suffix
.value
)
57 return super().__init
__(value
=value
, bits
=64)
60 return f
"{self.__class__.__name__}({self.value:016x})"
63 return f
".llong 0x{self.value:016x}"
67 return Instruction(self
[0:32])
71 return Instruction(self
[32:64])
74 class SVP64Instruction(PrefixedInstruction
):
75 class PrefixError(ValueError):
78 class Prefix(_SVP64PrefixFields
, Instruction
):
79 class RM(_SVP64RMFields
):
82 return (self
.mode
& 0b11)
86 return self
.__class
__.RM(super().rm
)
88 class Suffix(Instruction
):
91 def __init__(self
, prefix
, suffix
, byteorder
=ByteOrder
.LITTLE
):
92 if SVP64Instruction
.Prefix(prefix
).pid
!= 0b11:
93 raise SVP64Instruction
.PrefixError(prefix
)
94 return super().__init
__(prefix
, suffix
, byteorder
)
97 return (super().__str
__() + " # sv")
101 return self
.__class
__.Prefix(super().prefix
)
105 return self
.__class
__.Suffix(super().suffix
)
108 def load(ifile
, byteorder
, **_
):
110 prefix
= ifile
.read(4)
115 raise IOError(prefix
)
116 prefix
= Instruction(prefix
, byteorder
)
117 if prefix
.major
!= 0x1:
118 return Instruction(prefix
, byteorder
)
120 suffix
= ifile
.read(4)
125 raise IOError(suffix
)
127 return SVP64Instruction(prefix
, suffix
, byteorder
)
128 except SVP64Instruction
.PrefixError
:
129 return PrefixedInstruction(prefix
, suffix
, byteorder
)
138 def dump(insns
, ofile
, **_
):
144 parser
= _argparse
.ArgumentParser()
145 parser
.add_argument("ifile", nargs
="?",
146 type=_argparse
.FileType("rb"), default
=_sys
.stdin
.buffer)
147 parser
.add_argument("ofile", nargs
="?",
148 type=_argparse
.FileType("w"), default
=_sys
.stdout
)
149 parser
.add_argument("-b", "--byteorder",
150 type=ByteOrder
, default
=ByteOrder
.LITTLE
)
152 args
= dict(vars(parser
.parse_args()))
153 ifile
= args
["ifile"]
154 ofile
= args
["ofile"]
155 byteorder
= args
["byteorder"]
157 insns
= load(ifile
, byteorder
)
158 for line
in dump(insns
, byteorder
):
159 print(line
, file=ofile
)
162 if __name__
== "__main__":