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
import re as _re
)
from openpower.decoder.power_fields import (
Field as _Field,
- Array as _Array,
Mapping as _Mapping,
DecodeFields as _DecodeFields,
)
from openpower.decoder.pseudo.pagereader import ISA as _ISA
+@_functools.total_ordering
+class Verbosity(_enum.Enum):
+ SHORT = _enum.auto()
+ NORMAL = _enum.auto()
+ VERBOSE = _enum.auto()
+
+ def __lt__(self, other):
+ if not isinstance(other, self.__class__):
+ return NotImplemented
+ return (self.value < other.value)
+
+
def dataclass(cls, record, keymap=None, typemap=None):
if keymap is None:
keymap = {}
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
def transform(item):
(name, bitrange) = item
- return (name, list(bitrange.values()))
+ return (name, tuple(bitrange.values()))
self.__mapping = dict(map(transform, items))
class Operand:
name: str
- def disassemble(self, insn, record, verbose=False):
+ def span(self, record):
+ return record.fields[self.name]
+
+ def disassemble(self, insn, record,
+ verbosity=Verbosity.NORMAL, indent=""):
raise NotImplementedError
-@_dataclasses.dataclass(eq=True, frozen=True)
class DynamicOperand(Operand):
- def disassemble(self, insn, record, verbose=False):
- span = record.fields[self.name]
+ 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 verbose:
- yield f"{int(value):0{value.bits}b}"
- yield repr(span)
+
+ if verbosity >= Verbosity.VERBOSE:
+ span = map(str, span)
+ yield f"{indent}{self.name}"
+ yield f"{indent}{indent}{int(value):0{value.bits}b}"
+ yield f"{indent}{indent}{', '.join(span)}"
else:
yield str(int(value))
@_dataclasses.dataclass(eq=True, frozen=True)
+class StaticOperand(Operand):
+ value: int
+
+ 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:
+ span = map(str, span)
+ yield f"{indent}{self.name}"
+ yield f"{indent}{indent}{int(value):0{value.bits}b}"
+ yield f"{indent}{indent}{', '.join(span)}"
+ else:
+ yield str(int(value))
+
+
+class ImmediateOperand(DynamicOperand):
+ pass
+
+
class DynamicOperandReg(DynamicOperand):
+ def spec(self, insn, record, merge):
+ vector = False
+ span = self.span(record=record)
+ if isinstance(insn, SVP64Instruction):
+ span = tuple(map(lambda bit: (bit + 32), span))
+ value = insn[span]
+ span = tuple(map(str, span))
+
+ if isinstance(insn, SVP64Instruction):
+ extra_idx = self.extra_idx(record=record)
+ if extra_idx is _SVExtra.NONE:
+ return (vector, value, span)
+
+ if record.etype is _SVEtype.EXTRA3:
+ spec = insn.prefix.rm.extra3[extra_idx]
+ elif record.etype is _SVEtype.EXTRA2:
+ spec = insn.prefix.rm.extra2[extra_idx]
+ else:
+ raise ValueError(record.etype)
+
+ if spec != 0:
+ vector = bool(spec[0])
+ spec_span = spec.__class__
+ if record.etype is _SVEtype.EXTRA3:
+ spec_span = tuple(map(str, spec_span[1, 2]))
+ spec = spec[1, 2]
+ elif record.etype is _SVEtype.EXTRA2:
+ spec_span = tuple(map(str, spec_span[1,]))
+ spec = _SelectableInt(value=spec[1].value, bits=2)
+ if vector:
+ spec <<= 1
+ spec_span = (spec_span + ("{0}",))
+ else:
+ spec_span = (("{0}",) + spec_span)
+ else:
+ raise ValueError(record.etype)
+
+ (value, span) = merge(vector, value, span, spec, spec_span)
+
+ return (vector, value, span)
+
@property
def extra_reg(self):
return _SVExtraReg(self.name)
"in1", "in2", "in3", "cr_in",
"out", "out2", "cr_out",
}):
- if self.extra_reg == record.svp64.extra_reg(key):
- return record.extra_idx(key)
+ extra_reg = record.svp64.extra_reg(key=key)
+ if extra_reg is self.extra_reg:
+ return record.extra_idx(key=key)
return _SVExtra.NONE
+ def disassemble(self, insn, record,
+ verbosity=Verbosity.NORMAL, prefix="", indent=""):
+ (vector, value, span) = self.spec(insn=insn, record=record)
-@_dataclasses.dataclass(eq=True, frozen=True)
-class ImmediateOperand(DynamicOperand):
- pass
+ if verbosity >= Verbosity.VERBOSE:
+ yield f"{indent}{self.name}"
+ yield f"{indent}{indent}{int(value):0{value.bits}b}"
+ yield f"{indent}{indent}{', '.join(span)}"
+ if isinstance(insn, SVP64Instruction):
+ extra_idx = self.extra_idx(record)
+ if record.etype is _SVEtype.NONE:
+ yield f"{indent}{indent}extra[none]"
+ else:
+ etype = repr(record.etype).lower()
+ yield f"{indent}{indent}{etype}{extra_idx!r}"
+ yield f"{indent}type"
+ yield f"{indent}{indent}{'vector' if vector else 'scalar'}"
+ else:
+ vector = "*" if vector else ""
+ yield f"{vector}{prefix}{int(value)}"
+
+
+class DynamicOperandGPRFPR(DynamicOperandReg):
+ def spec(self, insn, record):
+ def merge(vector, value, span, spec, spec_span):
+ bits = (len(span) + len(spec_span))
+ value = _SelectableInt(value=value.value, bits=bits)
+ spec = _SelectableInt(value=spec.value, bits=bits)
+ if vector:
+ value = ((value << 2) | spec)
+ span = (span + spec_span)
+ else:
+ value = ((spec << 5) | value)
+ span = (spec_span + span)
+ return (value, span)
-@_dataclasses.dataclass(eq=True, frozen=True)
-class StaticOperand(Operand):
- value: int
+ return super().spec(insn=insn, record=record, merge=merge)
- def disassemble(self, insn, record, verbose=False):
- span = record.fields[self.name]
- value = insn[span]
- if verbose:
- yield f"{int(value):0{value.bits}b}"
- yield repr(span)
- else:
- yield str(int(value))
+class DynamicOperandGPR(DynamicOperandGPRFPR):
+ def disassemble(self, insn, record,
+ verbosity=Verbosity.NORMAL, indent=""):
+ prefix = "" if (verbosity <= Verbosity.SHORT) else "r"
+ yield from super().disassemble(prefix=prefix,
+ insn=insn, record=record,
+ verbosity=verbosity, indent=indent)
-@_dataclasses.dataclass(eq=True, frozen=True)
-class DynamicOperandTargetAddrLI(DynamicOperandReg):
- @property
- def name(self):
- return "LI"
- @name.setter
- def name(self, _):
- pass
+class DynamicOperandFPR(DynamicOperandGPRFPR):
+ def disassemble(self, insn, record,
+ verbosity=Verbosity.NORMAL, indent=""):
+ prefix = "" if (verbosity <= Verbosity.SHORT) else "f"
+ yield from super().disassemble(prefix=prefix,
+ insn=insn, record=record,
+ verbosity=verbosity, indent=indent)
- def disassemble(self, insn, record, verbose=False):
- span = record.fields["LI"]
+
+class DynamicOperandTargetAddr(DynamicOperandReg):
+ def disassemble(self, insn, record, field,
+ 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 verbose:
- yield f"{int(value):0{value.bits}b}"
- yield repr(span)
- yield "target_addr = EXTS(LI || 0b00))"
+
+ if verbosity >= Verbosity.VERBOSE:
+ span = tuple(map(str, span))
+ yield f"{indent}{self.name}"
+ yield f"{indent}{indent}{int(value):0{value.bits}b}00"
+ yield f"{indent}{indent}{', '.join(span + ('{0}', '{0}'))}"
+ yield f"{indent}{indent}target_addr = EXTS({field} || 0b00))"
else:
yield hex(int(_selectconcat(value,
_SelectableInt(value=0b00, bits=2))))
-class DynamicOperandTargetAddrBD(DynamicOperand):
- @property
- def name(self):
- return "BD"
+class DynamicOperandTargetAddrLI(DynamicOperandTargetAddr):
+ def span(self, record):
+ return record.fields["LI"]
- @name.setter
- def name(self, _):
- pass
+ def disassemble(self, insn, record,
+ verbosity=Verbosity.NORMAL, indent=""):
+ return super().disassemble(field="LI",
+ insn=insn, record=record,
+ verbosity=verbosity, indent=indent)
- def disassemble(self, insn, record, verbose=False):
- span = record.fields["BD"]
- value = insn[span]
- if verbose:
- yield f"{int(value):0{value.bits}b}"
- yield repr(span)
- yield "target_addr = EXTS(BD || 0b00))"
- else:
- yield hex(int(_selectconcat(value,
- _SelectableInt(value=0b00, bits=2))))
+class DynamicOperandTargetAddrBD(DynamicOperandTargetAddr):
+ def span(self, record):
+ return record.fields["BD"]
-@_dataclasses.dataclass(eq=True, frozen=True)
-class DynamicOperandGPR(DynamicOperandReg):
- def disassemble(self, insn, record, verbose=False):
- span = record.fields[self.name]
- value = insn[span]
- if verbose:
- yield f"{int(value):0{value.bits}b}"
- yield repr(span)
- if isinstance(insn, SVP64Instruction):
- extra_idx = self.extra_idx(record)
- if record.etype is _SVEtype.NONE:
- yield f"extra[none]"
- else:
- etype = repr(record.etype).lower()
- yield f"{etype}{extra_idx!r}"
- else:
- yield f"r{str(int(value))}"
+ def disassemble(self, insn, record,
+ verbosity=Verbosity.NORMAL, indent=""):
+ return super().disassemble(field="BD",
+ insn=insn, record=record,
+ verbosity=verbosity, indent=indent)
-@_dataclasses.dataclass(eq=True, frozen=True)
-class DynamicOperandFPR(DynamicOperandReg):
- def disassemble(self, insn, record, verbose=False):
- span = record.fields[self.name]
+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 verbose:
- yield f"{int(value):0{value.bits}b}"
- yield repr(span)
- if isinstance(insn, SVP64Instruction):
- extra_idx = self.extra_idx(record)
- if record.etype is _SVEtype.NONE:
- yield f"extra[none]"
- else:
- etype = repr(record.etype).lower()
- yield f"{etype}{extra_idx!r}"
+
+ 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 f"f{str(int(value))}"
+ 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))
- return FieldsOpcode(fields)
+ value = int(value)
+ mask = int(mask)
+
+ return Opcode(value=value, mask=mask)
@property
def function(self):
def cr_out(self):
return self.ppc.cr_out
+ ptype = property(lambda self: self.svp64.ptype)
+ etype = property(lambda self: self.svp64.etype)
+
def extra_idx(self, key):
return self.svp64.extra_idx(key)
- ptype = property(lambda self: self.svp64.ptype)
- etype = property(lambda self: self.svp64.etype)
- extra_in1 = property(lambda self: self.svp64.extra_in1)
- extra_in2 = property(lambda self: self.svp64.extra_in2)
- extra_in3 = property(lambda self: self.svp64.extra_in3)
- extra_out = property(lambda self: self.svp64.extra_out)
- extra_out2 = property(lambda self: self.svp64.extra_out2)
- extra_cr_in = property(lambda self: self.svp64.extra_cr_in)
- extra_cr_out = property(lambda self: self.svp64.extra_cr_out)
+ extra_idx_in1 = property(lambda self: self.svp64.extra_idx_in1)
+ extra_idx_in2 = property(lambda self: self.svp64.extra_idx_in2)
+ extra_idx_in3 = property(lambda self: self.svp64.extra_idx_in3)
+ extra_idx_out = property(lambda self: self.svp64.extra_idx_out)
+ extra_idx_out2 = property(lambda self: self.svp64.extra_idx_out2)
+ extra_idx_cr_in = property(lambda self: self.svp64.extra_idx_cr_in)
+ extra_idx_cr_out = property(lambda self: self.svp64.extra_idx_cr_out)
class Instruction(_Mapping):
raise KeyError(self)
return record
- def disassemble(self, db, byteorder="little", verbose=False):
+ def spec(self, db, prefix):
+ record = self.record(db=db)
+
+ dynamic_operands = tuple(map(_operator.itemgetter(0),
+ self.dynamic_operands(db=db)))
+
+ static_operands = []
+ for (name, value) in self.static_operands(db=db):
+ static_operands.append(f"{name}={value}")
+
+ operands = ""
+ if dynamic_operands:
+ operands += f" {','.join(dynamic_operands)}"
+ if static_operands:
+ operands += f" ({' '.join(static_operands)})"
+
+ return f"{prefix}{record.name}{operands}"
+
+ def dynamic_operands(self, db, verbosity=Verbosity.NORMAL):
+ record = self.record(db=db)
+
+ imm = False
+ imm_name = ""
+ imm_value = ""
+ for operand in record.operands.dynamic:
+ name = operand.name
+ dis = operand.disassemble(insn=self, record=record,
+ verbosity=min(verbosity, Verbosity.NORMAL))
+ value = " ".join(dis)
+ if imm:
+ name = f"{imm_name}({name})"
+ value = f"{imm_value}({value})"
+ imm = False
+ if isinstance(operand, ImmediateOperand):
+ imm_name = name
+ imm_value = value
+ imm = True
+ if not imm:
+ yield (name, value)
+
+ def static_operands(self, db):
+ record = self.record(db=db)
+ for operand in record.operands.static:
+ yield (operand.name, operand.value)
+
+ def disassemble(self, db,
+ byteorder="little",
+ verbosity=Verbosity.NORMAL):
raise NotImplementedError
bits.append(bit)
return "".join(map(str, bits))
- def spec(self, db):
- record = self.record(db=db)
-
- immediate = ""
- dynamic_operands = []
- for operand in record.operands.dynamic:
- name = operand.name
- if immediate:
- name = f"{immediate}({name})"
- immediate = ""
- if isinstance(operand, ImmediateOperand):
- immediate = operand.name
- if not immediate:
- dynamic_operands.append(name)
-
- static_operands = []
- for operand in record.operands.static:
- static_operands.append(f"{operand.name}={operand.value}")
-
- operands = ""
- if dynamic_operands:
- operands += f" {','.join(dynamic_operands)}"
- if static_operands:
- operands += f" ({' '.join(static_operands)})"
-
- return f"{record.name}{operands}"
-
def opcode(self, db):
record = self.record(db=db)
return f"0x{record.opcode.value:08x}"
record = self.record(db=db)
return f"0x{record.opcode.mask:08x}"
- def disassemble(self, db, byteorder="little", verbose=False):
+ def disassemble(self, db,
+ byteorder="little",
+ verbosity=Verbosity.NORMAL):
integer = int(self)
- blob = integer.to_bytes(length=4, byteorder=byteorder)
- blob = " ".join(map(lambda byte: f"{byte:02x}", blob))
+ if verbosity <= Verbosity.SHORT:
+ blob = ""
+ else:
+ blob = integer.to_bytes(length=4, byteorder=byteorder)
+ 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}"
+ yield f"{blob}.long 0x{integer:08x}"
return
- operands = []
- for operand in record.operands.dynamic:
- operand = " ".join(operand.disassemble(insn=self,
- record=record, verbose=False))
- operands.append(operand)
+ operands = tuple(map(_operator.itemgetter(1),
+ self.dynamic_operands(db=db, verbosity=verbosity)))
if operands:
- operands = ",".join(operands)
- operands = f" {operands}"
+ yield f"{blob}{record.name} {','.join(operands)}"
else:
- operands = ""
+ yield f"{blob}{record.name}"
- yield f"{blob} {record.name}{operands}"
-
- if verbose:
+ if verbosity >= Verbosity.VERBOSE:
indent = (" " * 4)
binary = self.binary
- spec = self.spec(db=db)
+ spec = self.spec(db=db, prefix="")
opcode = self.opcode(db=db)
mask = self.mask(db=db)
yield f"{indent}spec"
yield f"{indent}mask"
yield f"{indent}{indent}{mask}"
for operand in record.operands:
- name = operand.name
- yield f"{indent}{name}"
- parts = operand.disassemble(insn=self,
- record=record, verbose=True)
- for part in parts:
- yield f"{indent}{indent}{part}"
+ yield from operand.disassemble(insn=self, record=record,
+ verbosity=verbosity, indent=indent)
yield ""
prrc0: prrc0
+class Extra(_Mapping):
+ _: _Field = range(0, 9)
+
+
+class Extra2(Extra):
+ idx0: _Field = range(0, 2)
+ idx1: _Field = range(2, 4)
+ idx2: _Field = range(4, 6)
+ idx3: _Field = range(6, 8)
+
+ def __getitem__(self, key):
+ return {
+ 0: self.idx0,
+ 1: self.idx1,
+ 2: self.idx2,
+ 3: self.idx3,
+ _SVExtra.Idx0: self.idx0,
+ _SVExtra.Idx1: self.idx1,
+ _SVExtra.Idx2: self.idx2,
+ _SVExtra.Idx3: self.idx3,
+ }[key]
+
+ def __setitem__(self, key, value):
+ self[key].assign(value)
+
+
+class Extra3(Extra):
+ idx0: _Field = range(0, 3)
+ idx1: _Field = range(3, 6)
+ idx2: _Field = range(6, 9)
+
+ def __getitem__(self, key):
+ return {
+ 0: self.idx0,
+ 1: self.idx1,
+ 2: self.idx2,
+ _SVExtra.Idx0: self.idx0,
+ _SVExtra.Idx1: self.idx1,
+ _SVExtra.Idx2: self.idx2,
+ }[key]
+
+ def __setitem__(self, key, value):
+ self[key].assign(value)
+
+
class RM(_Mapping):
class Mode(Mode):
normal: NormalMode
elwidth: _Field = range(4, 6)
ewsrc: _Field = range(6, 8)
subvl: _Field = range(8, 10)
- extra: _Field = range(10, 19)
mode: Mode.remap(range(19, 24))
- extra2: _Array[4] = (
- range(10, 12),
- range(12, 14),
- range(14, 16),
- range(16, 18),
- )
smask: _Field = range(16, 19)
- extra3: _Array[3] = (
- range(10, 13),
- range(13, 16),
- range(16, 19),
- )
+
+ extra: Extra.remap(range(10, 19))
+ extra2: Extra2.remap(range(10, 19))
+ extra3: Extra3.remap(range(10, 19))
class SVP64Instruction(PrefixedInstruction):
bits.append(bit)
return "".join(map(str, bits))
- def spec(self, db):
- return f"sv.{self.suffix.spec(db=db)}"
-
def opcode(self, db):
return self.suffix.opcode(db=db)
raise ValueError(self)
- def disassemble(self, db, byteorder="little", verbose=False):
- integer_prefix = int(self.prefix)
- blob_prefix = integer_prefix.to_bytes(length=4, byteorder=byteorder)
- blob_prefix = " ".join(map(lambda byte: f"{byte:02x}", blob_prefix))
-
- integer_suffix = int(self.suffix)
- blob_suffix = integer_suffix.to_bytes(length=4, byteorder=byteorder)
- blob_suffix = " ".join(map(lambda byte: f"{byte:02x}", blob_suffix))
+ def disassemble(self, db,
+ byteorder="little",
+ verbosity=Verbosity.NORMAL):
+ def blob(integer):
+ if verbosity <= Verbosity.SHORT:
+ return ""
+ else:
+ blob = integer.to_bytes(length=4, byteorder=byteorder)
+ blob = " ".join(map(lambda byte: f"{byte:02x}", blob))
+ return f"{blob} "
- record = self.record(db=db)
+ blob_prefix = blob(int(self.prefix))
+ blob_suffix = blob(int(self.suffix))
+ 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}.long 0x{int(self.prefix):08x}"
+ yield f"{blob_suffix}.long 0x{int(self.suffix):08x}"
return
- yield f"{blob_prefix} sv.{record.name}"
- yield f"{blob_suffix}"
+ operands = tuple(map(_operator.itemgetter(1),
+ self.dynamic_operands(db=db, verbosity=verbosity)))
+ if operands:
+ yield f"{blob_prefix}sv.{record.name} {','.join(operands)}"
+ else:
+ yield f"{blob_prefix}{record.name}"
+ if blob_suffix:
+ yield f"{blob_suffix}"
(mode, mode_desc) = self.mode(db=db)
- if verbose:
+ if verbosity >= Verbosity.VERBOSE:
indent = (" " * 4)
binary = self.binary
- spec = self.spec(db=db)
+ spec = self.spec(db=db, prefix="sv.")
opcode = self.opcode(db=db)
mask = self.mask(db=db)
yield f"{indent}spec"
yield f"{indent}mask"
yield f"{indent}{indent}{mask}"
for operand in record.operands:
- name = operand.name
- yield f"{indent}{name}"
- parts = operand.disassemble(insn=self,
- record=record, verbose=True)
- for part in parts:
- yield f"{indent}{indent}{part}"
+ yield from operand.disassemble(insn=self, record=record,
+ verbosity=verbosity, indent=indent)
yield f"{indent}mode"
yield f"{indent}{indent}{mode_desc}"
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: