import dataclasses as _dataclasses
import enum as _enum
import functools as _functools
+import itertools as _itertools
import os as _os
import operator as _operator
import pathlib as _pathlib
return super().__init__(value=value, mask=mask)
-class FieldsOpcode(Opcode):
- def __init__(self, fields):
- def field(opcode, field):
- (value, mask) = opcode
- (field, bits) = field
- shifts = map(lambda bit: (31 - bit), reversed(tuple(bits)))
- for (index, shift) in enumerate(shifts):
- bit = ((field & (1 << index)) != 0)
- value |= (bit << shift)
- mask |= (1 << shift)
- return (value, mask)
-
- (value, mask) = _functools.reduce(field, fields, (0, 0))
-
- return super().__init__(value=value, mask=mask)
-
-
@_dataclasses.dataclass(eq=True, frozen=True)
class PPCRecord:
class FlagsMeta(type):
def __iter__(self):
yield from range(self.start, (self.end + 1))
+ def __reversed__(self):
+ return tuple(reversed(tuple(self)))
+
@property
def start(self):
return self.__start
raise NotImplementedError
-@_dataclasses.dataclass(eq=True, frozen=True)
class DynamicOperand(Operand):
def disassemble(self, insn, record,
verbosity=Verbosity.NORMAL, indent=""):
yield str(int(value))
-@_dataclasses.dataclass(eq=True, frozen=True)
-class ImmediateOperand(DynamicOperand):
- pass
-
-
@_dataclasses.dataclass(eq=True, frozen=True)
class StaticOperand(Operand):
value: int
yield str(int(value))
-@_dataclasses.dataclass(eq=True, frozen=True)
+class ImmediateOperand(DynamicOperand):
+ pass
+
+
class DynamicOperandReg(DynamicOperand):
def spec(self, insn, record, merge):
vector = False
verbosity=verbosity, indent=indent)
-@_dataclasses.dataclass(eq=True, frozen=True)
class DynamicOperandFPR(DynamicOperandGPRFPR):
def disassemble(self, insn, record,
verbosity=Verbosity.NORMAL, indent=""):
verbosity=verbosity, indent=indent)
-@_dataclasses.dataclass(eq=True, frozen=True)
class DynamicOperandTargetAddr(DynamicOperandReg):
def disassemble(self, insn, record, field,
verbosity=Verbosity.NORMAL, indent=""):
_SelectableInt(value=0b00, bits=2))))
-@_dataclasses.dataclass(eq=True, frozen=True)
class DynamicOperandTargetAddrLI(DynamicOperandTargetAddr):
def span(self, record):
return record.fields["LI"]
verbosity=verbosity, indent=indent)
+class DynamicOperandDDX(DynamicOperand):
+ def span(self, record):
+ operands = map(DynamicOperand, ("d0", "d1", "d2"))
+ spans = map(lambda operand: operand.span(record=record), operands)
+ return sum(spans, tuple())
+
+ def disassemble(self, insn, record,
+ verbosity=Verbosity.NORMAL, indent=""):
+ span = self.span(record=record)
+ if isinstance(insn, SVP64Instruction):
+ span = tuple(map(lambda bit: (bit + 32), span))
+ value = insn[span]
+
+ if verbosity >= Verbosity.VERBOSE:
+ yield f"{indent}D"
+ mapping = {
+ "d0": "[0:9]",
+ "d1": "[10:15]",
+ "d2": "[16]",
+ }
+ for (subname, subspan) in mapping.items():
+ operand = DynamicOperand(name=subname)
+ span = operand.span(record=record)
+ if isinstance(insn, SVP64Instruction):
+ span = tuple(map(lambda bit: (bit + 32), span))
+ value = insn[span]
+ span = map(str, span)
+ yield f"{indent}{indent}{operand.name} = D{subspan}"
+ yield f"{indent}{indent}{indent}{int(value):0{value.bits}b}"
+ yield f"{indent}{indent}{indent}{', '.join(span)}"
+ else:
+ yield str(int(value))
+
+
class Operands(tuple):
def __new__(cls, insn, iterable):
- branches = {
+ custom = {
"b": {"target_addr": DynamicOperandTargetAddrLI},
"ba": {"target_addr": DynamicOperandTargetAddrLI},
"bl": {"target_addr": DynamicOperandTargetAddrLI},
"bca": {"target_addr": DynamicOperandTargetAddrBD},
"bcl": {"target_addr": DynamicOperandTargetAddrBD},
"bcla": {"target_addr": DynamicOperandTargetAddrBD},
+ "addpcis": {"D": DynamicOperandDDX},
+ "fishmv": {"D": DynamicOperandDDX},
+ "fmvis": {"D": DynamicOperandDDX},
}
operands = []
if immediate is not None:
operands.append(ImmediateOperand(name=immediate))
- if insn in branches and operand in branches[insn]:
- dynamic_cls = branches[insn][operand]
+ if insn in custom and operand in custom[insn]:
+ dynamic_cls = custom[insn][operand]
if operand in _RegType.__members__:
regtype = _RegType[operand]
@cached_property
def opcode(self):
- fields = []
+ value = ([0] * 32)
+ mask = ([0] * 32)
+
if self.section.opcode:
- fields += [(self.section.opcode.value, BitSel((0, 5)))]
- fields += [(self.ppc.opcode.value, self.section.bitsel)]
- else:
- fields += [(self.ppc.opcode.value, self.section.bitsel)]
+ for (src, dst) in enumerate(reversed(BitSel((0, 5)))):
+ value[dst] = ((self.section.opcode.value & (1 << src)) != 0)
+ mask[dst] = ((self.section.opcode.mask & (1 << src)) != 0)
+
+ for (src, dst) in enumerate(reversed(self.section.bitsel)):
+ value[dst] = ((self.ppc.opcode.value & (1 << src)) != 0)
+ mask[dst] = ((self.ppc.opcode.mask & (1 << src)) != 0)
for operand in self.operands.static:
- fields += [(operand.value, self.fields[operand.name])]
+ for (src, dst) in enumerate(reversed(operand.span(record=self))):
+ value[dst] = ((operand.value & (1 << src)) != 0)
+ mask[dst] = True
+
+ for operand in self.operands.dynamic:
+ for dst in operand.span(record=self):
+ value[dst] = False
+ mask[dst] = False
+
+ def onebit(bit):
+ return _SelectableInt(value=int(bit), bits=1)
+
+ value = _selectconcat(*map(onebit, value))
+ mask = _selectconcat(*map(onebit, mask))
+
+ value = int(value)
+ mask = int(mask)
- return FieldsOpcode(fields)
+ return Opcode(value=value, mask=mask)
@property
def function(self):
blob = " ".join(map(lambda byte: f"{byte:02x}", blob))
blob += " "
- record = self.record(db=db)
+ record = db[self]
if record is None:
yield f"{blob}.long 0x{integer:08x}"
return
blob_prefix = blob(int(self.prefix))
blob_suffix = blob(int(self.suffix))
- record = self.record(db=db)
+ record = db[self]
if record is None or record.svp64 is None:
yield f"{blob_prefix}.long 0x{int(self.prefix):08x}"
yield f"{blob_suffix}.long 0x{int(self.suffix):08x}"
yield f"{blob_prefix}sv.{record.name} {','.join(operands)}"
else:
yield f"{blob_prefix}{record.name}"
- yield f"{blob_suffix}"
+ if blob_suffix:
+ yield f"{blob_suffix}"
(mode, mode_desc) = self.mode(db=db)
def __getitem__(self, key):
if isinstance(key, (int, Instruction)):
key = int(key)
+ matches = []
for record in self:
opcode = record.opcode
if ((opcode.value & opcode.mask) ==
(key & opcode.mask)):
- return record
+ matches.append(record)
+ if matches:
+ return matches[-1]
return None
elif isinstance(key, Opcode):
for record in self: