1 import collections
as _collections
3 import dataclasses
as _dataclasses
5 import functools
as _functools
7 import pathlib
as _pathlib
11 from functools
import cached_property
13 from cached_property
import cached_property
15 from openpower
.decoder
.power_enums
import (
16 Function
as _Function
,
23 CROutSel
as _CROutSel
,
25 LDSTMode
as _LDSTMode
,
34 SVExtraRegType
as _SVExtraRegType
,
35 SVExtraReg
as _SVExtraReg
,
37 from openpower
.decoder
.selectable_int
import (
38 SelectableInt
as _SelectableInt
,
39 selectconcat
as _selectconcat
,
41 from openpower
.decoder
.power_fields
import (
44 DecodeFields
as _DecodeFields
,
46 from openpower
.decoder
.pseudo
.pagereader
import ISA
as _ISA
49 def dataclass(cls
, record
, keymap
=None, typemap
=None):
53 typemap
= {field
.name
:field
.type for field
in _dataclasses
.fields(cls
)}
55 def transform(key_value
):
56 (key
, value
) = key_value
57 key
= keymap
.get(key
, key
)
58 hook
= typemap
.get(key
, lambda value
: value
)
59 if hook
is bool and value
in ("", "0"):
65 record
= dict(map(transform
, record
.items()))
66 for key
in frozenset(record
.keys()):
73 @_functools.total_ordering
74 @_dataclasses.dataclass(eq
=True, frozen
=True)
78 if self
.bit_length() <= 32:
79 return f
"0x{self:08x}"
81 return f
"0x{self:016x}"
85 if self
.bit_length() <= 32:
86 return f
"0x{self:08x}"
88 return f
"0x{self:016x}"
93 def __lt__(self
, other
):
94 if not isinstance(other
, Opcode
):
96 return ((self
.value
, self
.mask
) < (other
.value
, other
.mask
))
98 def __post_init__(self
):
99 (value
, mask
) = (self
.value
, self
.mask
)
101 if isinstance(value
, Opcode
):
103 raise ValueError(mask
)
104 (value
, mask
) = (value
.value
, value
.mask
)
105 elif isinstance(value
, str):
107 raise ValueError(mask
)
108 value
= int(value
, 0)
110 if not isinstance(value
, int):
111 raise ValueError(value
)
114 if not isinstance(mask
, int):
115 raise ValueError(mask
)
117 object.__setattr
__(self
, "value", self
.__class
__.Value(value
))
118 object.__setattr
__(self
, "mask", self
.__class
__.Mask(mask
))
121 class IntegerOpcode(Opcode
):
122 def __init__(self
, value
):
123 if isinstance(value
, str):
124 value
= int(value
, 0)
125 return super().__init
__(value
=value
, mask
=None)
128 class PatternOpcode(Opcode
):
129 def __init__(self
, value
):
130 (pattern
, value
, mask
) = (value
, 0, 0)
132 for symbol
in pattern
:
133 if symbol
not in {"0", "1", "-"}:
134 raise ValueError(pattern
)
135 value |
= (symbol
== "1")
136 mask |
= (symbol
!= "-")
142 return super().__init
__(value
=value
, mask
=mask
)
145 class FieldsOpcode(Opcode
):
146 def __init__(self
, fields
):
147 def field(opcode
, field
):
148 (value
, mask
) = opcode
149 (field
, bits
) = field
150 shifts
= map(lambda bit
: (31 - bit
), reversed(tuple(bits
)))
151 for (index
, shift
) in enumerate(shifts
):
152 bit
= ((field
& (1 << index
)) != 0)
153 value |
= (bit
<< shift
)
157 (value
, mask
) = _functools
.reduce(field
, fields
, (0, 0))
159 return super().__init
__(value
=value
, mask
=mask
)
162 @_dataclasses.dataclass(eq
=True, frozen
=True)
164 class FlagsMeta(type):
179 class Flags(frozenset, metaclass
=FlagsMeta
):
180 def __new__(cls
, flags
=frozenset()):
181 flags
= frozenset(flags
)
182 diff
= (flags
- frozenset(cls
))
184 raise ValueError(flags
)
185 return super().__new
__(cls
, flags
)
189 flags
: Flags
= Flags()
191 function
: _Function
= _Function
.NONE
192 intop
: _MicrOp
= _MicrOp
.OP_ILLEGAL
193 in1
: _In1Sel
= _In1Sel
.RA
194 in2
: _In2Sel
= _In2Sel
.NONE
195 in3
: _In3Sel
= _In3Sel
.NONE
196 out
: _OutSel
= _OutSel
.NONE
197 cr_in
: _CRInSel
= _CRInSel
.NONE
198 cr_out
: _CROutSel
= _CROutSel
.NONE
199 cry_in
: _CryIn
= _CryIn
.ZERO
200 ldst_len
: _LDSTLen
= _LDSTLen
.NONE
201 upd
: _LDSTMode
= _LDSTMode
.NONE
202 rc
: _RCOE
= _RCOE
.NONE
203 form
: _Form
= _Form
.NONE
205 unofficial
: bool = False
209 "internal op": "intop",
213 "ldst len": "ldst_len",
214 "CONDITIONS": "conditions",
218 def CSV(cls
, record
, opcode_cls
=Opcode
):
219 typemap
= {field
.name
:field
.type for field
in _dataclasses
.fields(cls
)}
220 typemap
["opcode"] = opcode_cls
223 for flag
in frozenset(PPCRecord
.Flags
):
224 if bool(record
.pop(flag
, "")):
226 record
["flags"] = PPCRecord
.Flags(flags
)
228 return dataclass(cls
, record
, keymap
=PPCRecord
.__KEYMAP
, typemap
=typemap
)
232 return frozenset(self
.comment
.split("=")[-1].split("/"))
235 @_dataclasses.dataclass(eq
=True, frozen
=True)
237 class ExtraMap(tuple):
239 @_dataclasses.dataclass(eq
=True, frozen
=True)
241 regtype
: _SVExtraRegType
= _SVExtraRegType
.NONE
242 reg
: _SVExtraReg
= _SVExtraReg
.NONE
245 return f
"{self.regtype.value}:{self.reg.name}"
247 def __new__(cls
, value
="0"):
248 if isinstance(value
, str):
249 def transform(value
):
250 (regtype
, reg
) = value
.split(":")
251 regtype
= _SVExtraRegType(regtype
)
252 reg
= _SVExtraReg(reg
)
253 return cls
.Entry(regtype
=regtype
, reg
=reg
)
258 value
= map(transform
, value
.split(";"))
260 return super().__new
__(cls
, value
)
263 return repr(list(self
))
265 def __new__(cls
, value
=tuple()):
269 return super().__new
__(cls
, map(cls
.Extra
, value
))
272 return repr({index
:self
[index
] for index
in range(0, 4)})
275 ptype
: _SVPtype
= _SVPtype
.NONE
276 etype
: _SVEtype
= _SVEtype
.NONE
277 in1
: _In1Sel
= _In1Sel
.NONE
278 in2
: _In2Sel
= _In2Sel
.NONE
279 in3
: _In3Sel
= _In3Sel
.NONE
280 out
: _OutSel
= _OutSel
.NONE
281 out2
: _OutSel
= _OutSel
.NONE
282 cr_in
: _CRInSel
= _CRInSel
.NONE
283 cr_out
: _CROutSel
= _CROutSel
.NONE
284 extra
: ExtraMap
= ExtraMap()
287 mode
: _SVMode
= _SVMode
.NORMAL
291 "CONDITIONS": "conditions",
300 def CSV(cls
, record
):
301 for key
in ("in1", "in2", "in3", "out", "out2", "CR in", "CR out"):
306 record
["extra"] = cls
.ExtraMap(record
.pop(f
"{index}") for index
in range(0, 4))
308 return dataclass(cls
, record
, keymap
=cls
.__KEYMAP
)
312 def __init__(self
, value
=(0, 32)):
313 if isinstance(value
, str):
314 (start
, end
) = map(int, value
.split(":"))
317 if start
< 0 or end
< 0 or start
>= end
:
318 raise ValueError(value
)
323 return super().__init
__()
326 return f
"[{self.__start}:{self.__end}]"
329 yield from range(self
.start
, (self
.end
+ 1))
340 @_dataclasses.dataclass(eq
=True, frozen
=True)
342 class Mode(_enum
.Enum
):
343 INTEGER
= _enum
.auto()
344 PATTERN
= _enum
.auto()
347 def _missing_(cls
, value
):
348 if isinstance(value
, str):
349 return cls
[value
.upper()]
350 return super()._missing
_(value
)
353 def __new__(cls
, value
=None):
354 if isinstance(value
, str):
355 if value
.upper() == "NONE":
358 value
= int(value
, 0)
362 return super().__new
__(cls
, value
)
368 return (bin(self
) if self
else "None")
377 def CSV(cls
, record
):
378 return dataclass(cls
, record
)
382 def __init__(self
, items
):
383 if isinstance(items
, dict):
384 items
= items
.items()
387 (name
, bitrange
) = item
388 return (name
, tuple(bitrange
.values()))
390 self
.__mapping
= dict(map(transform
, items
))
392 return super().__init
__()
395 return repr(self
.__mapping
)
398 yield from self
.__mapping
.items()
400 def __contains__(self
, key
):
401 return self
.__mapping
.__contains
__(key
)
403 def __getitem__(self
, key
):
404 return self
.__mapping
.get(key
, None)
407 @_dataclasses.dataclass(eq
=True, frozen
=True)
412 class Operands(tuple):
413 @_dataclasses.dataclass(eq
=True, frozen
=True)
414 class DynamicOperand(Operand
):
417 def disassemble(self
, value
, record
):
418 return str(int(value
[record
.fields
[self
.name
]]))
420 @_dataclasses.dataclass(eq
=True, frozen
=True)
421 class StaticOperand(Operand
):
425 @_dataclasses.dataclass(eq
=True, frozen
=True)
426 class DynamicOperandIFormLI(DynamicOperand
):
427 def disassemble(self
, value
, record
):
428 return hex(int(_selectconcat(
429 value
[record
.fields
["LI"]],
430 _SelectableInt(value
=0b00, bits
=2))))
432 class DynamicOperandBFormBD(DynamicOperand
):
433 def disassemble(self
, value
, record
):
434 return hex(int(_selectconcat(
435 value
[record
.fields
["BD"]],
436 _SelectableInt(value
=0b00, bits
=2))))
438 @_dataclasses.dataclass(eq
=True, frozen
=True)
439 class DynamicOperandGPR(DynamicOperand
):
440 def disassemble(self
, value
, record
):
441 return f
"r{super().disassemble(value=value, record=record)}"
443 @_dataclasses.dataclass(eq
=True, frozen
=True)
444 class DynamicOperandFPR(DynamicOperand
):
445 def disassemble(self
, value
, record
):
446 return f
"f{super().disassemble(value=value, record=record)}"
448 def __new__(cls
, insn
, iterable
):
450 "b": {"LI": cls
.DynamicOperandIFormLI
},
451 "ba": {"LI": cls
.DynamicOperandIFormLI
},
452 "bl": {"LI": cls
.DynamicOperandIFormLI
},
453 "bla": {"LI": cls
.DynamicOperandIFormLI
},
454 "bc": {"BD": cls
.DynamicOperandBFormBD
},
455 "bca": {"BD": cls
.DynamicOperandBFormBD
},
456 "bcl": {"BD": cls
.DynamicOperandBFormBD
},
457 "bcla": {"BD": cls
.DynamicOperandBFormBD
},
461 for operand
in iterable
:
462 dynamic_cls
= cls
.DynamicOperand
463 static_cls
= cls
.StaticOperand
466 (name
, value
) = operand
.split("=")
467 operand
= static_cls(name
=name
, value
=int(value
))
469 if insn
in branches
and operand
in branches
[insn
]:
470 dynamic_cls
= branches
[insn
][operand
]
472 if operand
in _RegType
.__members
__:
473 regtype
= _RegType
[operand
]
474 if regtype
is _RegType
.GPR
:
475 dynamic_cls
= cls
.DynamicOperandGPR
476 elif regtype
is _RegType
.FPR
:
477 dynamic_cls
= cls
.DynamicOperandFPR
479 operand
= dynamic_cls(name
=operand
)
481 operands
.append(operand
)
483 return super().__new
__(cls
, operands
)
486 @_functools.total_ordering
487 @_dataclasses.dataclass(eq
=True, frozen
=True)
494 svp64
: SVP64Record
= None
503 def __lt__(self
, other
):
504 if not isinstance(other
, Record
):
505 return NotImplemented
506 return (self
.opcode
< other
.opcode
)
509 return f
"{self.__class__.__name__}(name={self.name!r}, opcode={self.opcode})"
514 if self
.section
.opcode
:
515 fields
+= [(self
.section
.opcode
.value
, BitSel((0, 5)))]
516 fields
+= [(self
.ppc
.opcode
.value
, self
.section
.bitsel
)]
518 fields
+= [(self
.ppc
.opcode
.value
, self
.section
.bitsel
)]
520 for operand
in self
.static_operands
:
521 fields
+= [(operand
.value
, self
.fields
[operand
.name
])]
523 return FieldsOpcode(fields
)
527 return self
.ppc
.function
547 if self
.svp64
is None:
553 return self
.ppc
.cr_in
557 return self
.ppc
.cr_out
559 def sv_extra(self
, key
):
560 if key
not in frozenset({
561 "in1", "in2", "in3", "cr_in",
562 "out", "out2", "cr_out",
566 sel
= getattr(self
.svp64
, key
)
567 if sel
is _CRInSel
.BA_BB
:
568 return _SVExtra
.Idx_1_2
569 reg
= _SVExtraReg(sel
)
570 if reg
is _SVExtraReg
.NONE
:
574 _SVExtraRegType
.SRC
: {},
575 _SVExtraRegType
.DST
: {},
577 for index
in range(0, 4):
578 for entry
in self
.svp64
.extra
[index
]:
579 extra_map
[entry
.regtype
][entry
.reg
] = Record
.__EXTRA
[index
]
581 for regtype
in (_SVExtraRegType
.SRC
, _SVExtraRegType
.DST
):
582 extra
= extra_map
[regtype
].get(reg
, _SVExtra
.NONE
)
583 if extra
is not _SVExtra
.NONE
:
588 sv_in1
= property(_functools
.partial(sv_extra
, key
="in1"))
589 sv_in2
= property(_functools
.partial(sv_extra
, key
="in2"))
590 sv_in3
= property(_functools
.partial(sv_extra
, key
="in3"))
591 sv_out
= property(_functools
.partial(sv_extra
, key
="out"))
592 sv_out2
= property(_functools
.partial(sv_extra
, key
="out2"))
593 sv_cr_in
= property(_functools
.partial(sv_extra
, key
="cr_in"))
594 sv_cr_out
= property(_functools
.partial(sv_extra
, key
="cr_out"))
598 if self
.svp64
is None:
600 return self
.svp64
.ptype
604 if self
.svp64
is None:
606 return self
.svp64
.etype
609 def dynamic_operands(self
):
610 for operand
in self
.operands
:
611 if isinstance(operand
, Operands
.DynamicOperand
):
615 def static_operands(self
):
616 for operand
in self
.operands
:
617 if isinstance(operand
, Operands
.StaticOperand
):
621 class Instruction(_Mapping
):
623 def integer(cls
, value
=0, bits
=None, byteorder
="little"):
624 if isinstance(value
, (int, bytes
)) and not isinstance(bits
, int):
625 raise ValueError(bits
)
627 if isinstance(value
, bytes
):
628 if ((len(value
) * 8) != bits
):
629 raise ValueError(f
"bit length mismatch")
630 value
= int.from_bytes(value
, byteorder
=byteorder
)
632 if isinstance(value
, int):
633 value
= _SelectableInt(value
=value
, bits
=bits
)
634 elif isinstance(value
, Instruction
):
635 value
= value
.storage
637 if not isinstance(value
, _SelectableInt
):
638 raise ValueError(value
)
641 if len(value
) != bits
:
642 raise ValueError(value
)
644 value
= _SelectableInt(value
=value
, bits
=bits
)
646 return cls(storage
=value
)
649 return hash(int(self
))
651 def disassemble(self
, db
):
652 raise NotImplementedError
655 class WordInstruction(Instruction
):
656 _
: _Field
= range(0, 32)
657 po
: _Field
= range(0, 6)
660 def integer(cls
, value
, byteorder
="little"):
661 return super().integer(bits
=32, value
=value
, byteorder
=byteorder
)
663 def disassemble(self
, db
):
666 yield f
".long 0x{int(self):08x}"
669 for operand
in record
.dynamic_operands
:
670 operand
= operand
.disassemble(self
, record
)
671 operands
.append(operand
)
673 operands
= ",".join(operands
)
674 operands
= f
" {operands}"
678 yield f
"{record.name}{operands}"
680 class PrefixedInstruction(Instruction
):
681 class Prefix(WordInstruction
.remap(range(0, 32))):
684 class Suffix(WordInstruction
.remap(range(32, 64))):
687 _
: _Field
= range(64)
693 def integer(cls
, value
, byteorder
="little"):
694 return super().integer(bits
=64, value
=value
, byteorder
=byteorder
)
697 def pair(cls
, prefix
=0, suffix
=0, byteorder
="little"):
698 def transform(value
):
699 return WordInstruction
.integer(value
=value
,
700 byteorder
=byteorder
)[0:32]
702 (prefix
, suffix
) = map(transform
, (prefix
, suffix
))
703 value
= _selectconcat(prefix
, suffix
)
705 return super().integer(value
=value
)
708 class SVP64Instruction(PrefixedInstruction
):
709 """SVP64 instruction: https://libre-soc.org/openpower/sv/svp64/"""
710 class Prefix(PrefixedInstruction
.Prefix
):
712 _
: _Field
= range(24)
714 mask
: _Field
= range(1, 4)
715 elwidth
: _Field
= range(4, 6)
716 ewsrc
: _Field
= range(6, 8)
717 subvl
: _Field
= range(8, 10)
718 extra
: _Field
= range(10, 19)
719 mode
: _Field
= range(19, 24)
720 extra2
: _Field
[4] = (
726 smask
: _Field
= range(16, 19)
727 extra3
: _Field
[3] = (
734 rm
: RM
= ((6, 8) + tuple(range(10, 32)))
738 def disassemble(self
, db
):
739 record
= db
[self
.suffix
]
741 yield f
".llong 0x{int(self):016x}"
743 yield f
".llong 0x{int(self):016x} # sv.{record.name}"
746 def parse(stream
, factory
):
747 lines
= filter(lambda line
: not line
.strip().startswith("#"), stream
)
748 entries
= _csv
.DictReader(lines
)
749 entries
= filter(lambda entry
: "TODO" not in frozenset(entry
.values()), entries
)
750 return tuple(map(factory
, entries
))
754 def __init__(self
, root
):
755 db
= _collections
.defaultdict(set)
756 path
= (root
/ "insndb.csv")
757 with
open(path
, "r", encoding
="UTF-8") as stream
:
758 for section
in parse(stream
, Section
.CSV
):
759 path
= (root
/ section
.path
)
761 section
.Mode
.INTEGER
: IntegerOpcode
,
762 section
.Mode
.PATTERN
: PatternOpcode
,
764 factory
= _functools
.partial(PPCRecord
.CSV
, opcode_cls
=opcode_cls
)
765 with
open(path
, "r", encoding
="UTF-8") as stream
:
766 db
[section
].update(parse(stream
, factory
))
768 return super().__init
__()
770 def __getitem__(self
, key
):
771 for (section
, records
) in self
.__db
.items():
772 for record
in records
:
773 for name
in record
.names
:
775 ((record
.rc
is _RC
.RC
) and
776 key
.endswith(".") and
778 return (section
, record
)
783 def __init__(self
, root
):
785 pattern
= _re
.compile(r
"^(?:LDST)?RM-(1P|2P)-.*?\.csv$")
786 for (prefix
, _
, names
) in _os
.walk(root
):
787 prefix
= _pathlib
.Path(prefix
)
788 for name
in filter(lambda name
: pattern
.match(name
), names
):
789 path
= (prefix
/ _pathlib
.Path(name
))
790 with
open(path
, "r", encoding
="UTF-8") as stream
:
791 db
.update(parse(stream
, SVP64Record
.CSV
))
792 self
.__db
= {record
.name
:record
for record
in db
}
793 return super().__init
__()
795 def __getitem__(self
, key
):
797 record
= self
.__db
.get(name
, None)
798 if record
is not None:
803 class FieldsDatabase
:
808 for (form
, fields
) in df
.instrs
.items():
809 if form
in {"DQE", "TX"}:
813 db
[_Form
[form
]] = Fields(fields
)
815 return super().__init
__()
817 def __getitem__(self
, key
):
818 return self
.__db
.__getitem
__(key
)
821 class MarkdownDatabase
:
824 for (name
, desc
) in _ISA():
827 (dynamic
, *static
) = desc
.regs
828 operands
.extend(dynamic
)
829 operands
.extend(static
)
830 db
[name
] = Operands(insn
=name
, iterable
=operands
)
832 return super().__init
__()
835 yield from self
.__db
.items()
837 def __getitem__(self
, key
):
838 return self
.__db
.__getitem
__(key
)
842 def __init__(self
, root
):
843 root
= _pathlib
.Path(root
)
845 mdwndb
= MarkdownDatabase()
846 fieldsdb
= FieldsDatabase()
847 svp64db
= SVP64Database(root
)
848 ppcdb
= PPCDatabase(root
)
851 for (name
, operands
) in mdwndb
:
852 (section
, ppc
) = ppcdb
[name
]
855 svp64
= svp64db
[ppc
.names
]
856 fields
= fieldsdb
[ppc
.form
]
857 record
= Record(name
=name
,
858 section
=section
, ppc
=ppc
, svp64
=svp64
,
859 operands
=operands
, fields
=fields
)
862 self
.__db
= tuple(sorted(db
))
864 return super().__init
__()
867 return repr(self
.__db
)
872 @_functools.lru_cache(maxsize
=None)
873 def __contains__(self
, key
):
874 return self
.__getitem
__(key
) is not None
876 @_functools.lru_cache(maxsize
=None)
877 def __getitem__(self
, key
):
878 if isinstance(key
, (int, Instruction
)):
881 opcode
= record
.opcode
882 if ((opcode
.value
& opcode
.mask
) ==
883 (key
& opcode
.mask
)):
886 elif isinstance(key
, Opcode
):
888 if record
.opcode
== key
:
890 elif isinstance(key
, str):
892 if record
.name
== key
: