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
,
33 SVExtraRegType
as _SVExtraRegType
,
34 SVExtraReg
as _SVExtraReg
,
36 from openpower
.decoder
.selectable_int
import (
37 SelectableInt
as _SelectableInt
,
38 selectconcat
as _selectconcat
,
40 from openpower
.decoder
.power_fields
import (
43 DecodeFields
as _DecodeFields
,
45 from openpower
.decoder
.pseudo
.pagereader
import ISA
as _ISA
48 def dataclass(cls
, record
, keymap
=None, typemap
=None):
52 typemap
= {field
.name
:field
.type for field
in _dataclasses
.fields(cls
)}
54 def transform(key_value
):
55 (key
, value
) = key_value
56 key
= keymap
.get(key
, key
)
57 hook
= typemap
.get(key
, lambda value
: value
)
58 if hook
is bool and value
in ("", "0"):
64 record
= dict(map(transform
, record
.items()))
65 for key
in frozenset(record
.keys()):
72 @_functools.total_ordering
73 @_dataclasses.dataclass(eq
=True, frozen
=True)
77 if self
.bit_length() <= 32:
78 return f
"0x{self:08x}"
80 return f
"0x{self:016x}"
84 if self
.bit_length() <= 32:
85 return f
"0x{self:08x}"
87 return f
"0x{self:016x}"
92 def __lt__(self
, other
):
93 if not isinstance(other
, Opcode
):
95 return ((self
.value
, self
.mask
) < (other
.value
, other
.mask
))
97 def __post_init__(self
):
98 (value
, mask
) = (self
.value
, self
.mask
)
100 if isinstance(value
, Opcode
):
102 raise ValueError(mask
)
103 (value
, mask
) = (value
.value
, value
.mask
)
104 elif isinstance(value
, str):
106 raise ValueError(mask
)
107 value
= int(value
, 0)
109 if not isinstance(value
, int):
110 raise ValueError(value
)
113 if not isinstance(mask
, int):
114 raise ValueError(mask
)
116 object.__setattr
__(self
, "value", self
.__class
__.Value(value
))
117 object.__setattr
__(self
, "mask", self
.__class
__.Mask(mask
))
120 class IntegerOpcode(Opcode
):
121 def __init__(self
, value
):
122 if isinstance(value
, str):
123 value
= int(value
, 0)
124 return super().__init
__(value
=value
, mask
=None)
127 class PatternOpcode(Opcode
):
128 def __init__(self
, value
):
129 (pattern
, value
, mask
) = (value
, 0, 0)
131 for symbol
in pattern
:
132 if symbol
not in {"0", "1", "-"}:
133 raise ValueError(pattern
)
134 value |
= (symbol
== "1")
135 mask |
= (symbol
!= "-")
141 return super().__init
__(value
=value
, mask
=mask
)
144 class FieldsOpcode(Opcode
):
145 def __init__(self
, fields
):
146 def field(opcode
, field
):
147 (value
, mask
) = opcode
148 (field
, bits
) = field
149 shifts
= map(lambda bit
: (31 - bit
), reversed(tuple(bits
)))
150 for (index
, shift
) in enumerate(shifts
):
151 bit
= ((field
& (1 << index
)) != 0)
152 value |
= (bit
<< shift
)
156 (value
, mask
) = _functools
.reduce(field
, fields
, (0, 0))
158 return super().__init
__(value
=value
, mask
=mask
)
161 @_dataclasses.dataclass(eq
=True, frozen
=True)
163 class FlagsMeta(type):
178 class Flags(frozenset, metaclass
=FlagsMeta
):
179 def __new__(cls
, flags
=frozenset()):
180 flags
= frozenset(flags
)
181 diff
= (flags
- frozenset(cls
))
183 raise ValueError(flags
)
184 return super().__new
__(cls
, flags
)
188 flags
: Flags
= Flags()
190 function
: _Function
= _Function
.NONE
191 intop
: _MicrOp
= _MicrOp
.OP_ILLEGAL
192 in1
: _In1Sel
= _In1Sel
.RA
193 in2
: _In2Sel
= _In2Sel
.NONE
194 in3
: _In3Sel
= _In3Sel
.NONE
195 out
: _OutSel
= _OutSel
.NONE
196 cr_in
: _CRInSel
= _CRInSel
.NONE
197 cr_out
: _CROutSel
= _CROutSel
.NONE
198 cry_in
: _CryIn
= _CryIn
.ZERO
199 ldst_len
: _LDSTLen
= _LDSTLen
.NONE
200 upd
: _LDSTMode
= _LDSTMode
.NONE
201 rc
: _RCOE
= _RCOE
.NONE
202 form
: _Form
= _Form
.NONE
204 unofficial
: bool = False
208 "internal op": "intop",
212 "ldst len": "ldst_len",
213 "CONDITIONS": "conditions",
217 def CSV(cls
, record
, opcode_cls
=Opcode
):
218 typemap
= {field
.name
:field
.type for field
in _dataclasses
.fields(cls
)}
219 typemap
["opcode"] = opcode_cls
222 for flag
in frozenset(PPCRecord
.Flags
):
223 if bool(record
.pop(flag
, "")):
225 record
["flags"] = PPCRecord
.Flags(flags
)
227 return dataclass(cls
, record
, keymap
=PPCRecord
.__KEYMAP
, typemap
=typemap
)
231 return frozenset(self
.comment
.split("=")[-1].split("/"))
234 @_dataclasses.dataclass(eq
=True, frozen
=True)
236 class ExtraMap(tuple):
238 @_dataclasses.dataclass(eq
=True, frozen
=True)
240 regtype
: _SVExtraRegType
= _SVExtraRegType
.NONE
241 reg
: _SVExtraReg
= _SVExtraReg
.NONE
244 return f
"{self.regtype.value}:{self.reg.name}"
246 def __new__(cls
, value
="0"):
247 if isinstance(value
, str):
248 def transform(value
):
249 (regtype
, reg
) = value
.split(":")
250 regtype
= _SVExtraRegType(regtype
)
251 reg
= _SVExtraReg(reg
)
252 return cls
.Entry(regtype
=regtype
, reg
=reg
)
257 value
= map(transform
, value
.split(";"))
259 return super().__new
__(cls
, value
)
262 return repr(list(self
))
264 def __new__(cls
, value
=tuple()):
268 return super().__new
__(cls
, map(cls
.Extra
, value
))
271 return repr({index
:self
[index
] for index
in range(0, 4)})
274 ptype
: _SVPtype
= _SVPtype
.NONE
275 etype
: _SVEtype
= _SVEtype
.NONE
276 in1
: _In1Sel
= _In1Sel
.NONE
277 in2
: _In2Sel
= _In2Sel
.NONE
278 in3
: _In3Sel
= _In3Sel
.NONE
279 out
: _OutSel
= _OutSel
.NONE
280 out2
: _OutSel
= _OutSel
.NONE
281 cr_in
: _CRInSel
= _CRInSel
.NONE
282 cr_out
: _CROutSel
= _CROutSel
.NONE
283 extra
: ExtraMap
= ExtraMap()
286 mode
: _SVMode
= _SVMode
.NORMAL
290 "CONDITIONS": "conditions",
299 def CSV(cls
, record
):
300 for key
in ("in1", "in2", "in3", "out", "out2", "CR in", "CR out"):
305 record
["extra"] = cls
.ExtraMap(record
.pop(f
"{index}") for index
in range(0, 4))
307 return dataclass(cls
, record
, keymap
=cls
.__KEYMAP
)
311 def __init__(self
, value
=(0, 32)):
312 if isinstance(value
, str):
313 (start
, end
) = map(int, value
.split(":"))
316 if start
< 0 or end
< 0 or start
>= end
:
317 raise ValueError(value
)
322 return super().__init
__()
325 return f
"[{self.__start}:{self.__end}]"
328 yield from range(self
.start
, (self
.end
+ 1))
339 @_dataclasses.dataclass(eq
=True, frozen
=True)
341 class Mode(_enum
.Enum
):
342 INTEGER
= _enum
.auto()
343 PATTERN
= _enum
.auto()
346 def _missing_(cls
, value
):
347 if isinstance(value
, str):
348 return cls
[value
.upper()]
349 return super()._missing
_(value
)
352 def __new__(cls
, value
=None):
353 if isinstance(value
, str):
354 if value
.upper() == "NONE":
357 value
= int(value
, 0)
361 return super().__new
__(cls
, value
)
367 return (bin(self
) if self
else "None")
376 def CSV(cls
, record
):
377 return dataclass(cls
, record
)
381 def __init__(self
, items
):
382 if isinstance(items
, dict):
383 items
= items
.items()
386 (name
, bitrange
) = item
387 return (name
, tuple(bitrange
.values()))
389 self
.__mapping
= dict(map(transform
, items
))
391 return super().__init
__()
394 return repr(self
.__mapping
)
397 yield from self
.__mapping
.items()
399 def __contains__(self
, key
):
400 return self
.__mapping
.__contains
__(key
)
402 def __getitem__(self
, key
):
403 return self
.__mapping
.get(key
, None)
406 @_dataclasses.dataclass(eq
=True, frozen
=True)
411 class Operands(tuple):
412 @_dataclasses.dataclass(eq
=True, frozen
=True)
413 class DynamicOperand(Operand
):
416 def disassemble(self
, value
, record
):
417 return str(int(value
[record
.fields
[self
.name
]]))
419 @_dataclasses.dataclass(eq
=True, frozen
=True)
420 class StaticOperand(Operand
):
424 def __new__(cls
, iterable
):
425 dynamic_cls
= cls
.DynamicOperand
426 static_cls
= cls
.StaticOperand
429 for operand
in iterable
:
431 (name
, value
) = operand
.split("=")
432 operand
= static_cls(name
=name
, value
=int(value
))
434 operand
= dynamic_cls(name
=operand
)
435 operands
.append(operand
)
437 return super().__new
__(cls
, operands
)
440 @_functools.total_ordering
441 @_dataclasses.dataclass(eq
=True, frozen
=True)
448 svp64
: SVP64Record
= None
457 def __lt__(self
, other
):
458 if not isinstance(other
, Record
):
459 return NotImplemented
460 return (self
.opcode
< other
.opcode
)
463 return f
"{self.__class__.__name__}(name={self.name!r}, opcode={self.opcode})"
468 if self
.section
.opcode
:
469 fields
+= [(self
.section
.opcode
.value
, BitSel((0, 5)))]
470 fields
+= [(self
.ppc
.opcode
.value
, self
.section
.bitsel
)]
472 fields
+= [(self
.ppc
.opcode
.value
, self
.section
.bitsel
)]
474 for operand
in self
.static_operands
:
475 fields
+= [(operand
.value
, self
.fields
[operand
.name
])]
477 return FieldsOpcode(fields
)
481 return self
.ppc
.function
501 if self
.svp64
is None:
507 return self
.ppc
.cr_in
511 return self
.ppc
.cr_out
513 def sv_extra(self
, key
):
514 if key
not in frozenset({
515 "in1", "in2", "in3", "cr_in",
516 "out", "out2", "cr_out",
520 sel
= getattr(self
.svp64
, key
)
521 if sel
is _CRInSel
.BA_BB
:
522 return _SVExtra
.Idx_1_2
523 reg
= _SVExtraReg(sel
)
524 if reg
is _SVExtraReg
.NONE
:
528 _SVExtraRegType
.SRC
: {},
529 _SVExtraRegType
.DST
: {},
531 for index
in range(0, 4):
532 for entry
in self
.svp64
.extra
[index
]:
533 extra_map
[entry
.regtype
][entry
.reg
] = Record
.__EXTRA
[index
]
535 for regtype
in (_SVExtraRegType
.SRC
, _SVExtraRegType
.DST
):
536 extra
= extra_map
[regtype
].get(reg
, _SVExtra
.NONE
)
537 if extra
is not _SVExtra
.NONE
:
542 sv_in1
= property(_functools
.partial(sv_extra
, key
="in1"))
543 sv_in2
= property(_functools
.partial(sv_extra
, key
="in2"))
544 sv_in3
= property(_functools
.partial(sv_extra
, key
="in3"))
545 sv_out
= property(_functools
.partial(sv_extra
, key
="out"))
546 sv_out2
= property(_functools
.partial(sv_extra
, key
="out2"))
547 sv_cr_in
= property(_functools
.partial(sv_extra
, key
="cr_in"))
548 sv_cr_out
= property(_functools
.partial(sv_extra
, key
="cr_out"))
552 if self
.svp64
is None:
554 return self
.svp64
.ptype
558 if self
.svp64
is None:
560 return self
.svp64
.etype
563 def dynamic_operands(self
):
564 for operand
in self
.operands
:
565 if isinstance(operand
, Operands
.DynamicOperand
):
569 def static_operands(self
):
570 for operand
in self
.operands
:
571 if isinstance(operand
, Operands
.StaticOperand
):
575 class Instruction(_Mapping
):
577 def integer(cls
, value
=0, bits
=None, byteorder
="little"):
578 if isinstance(value
, (int, bytes
)) and not isinstance(bits
, int):
579 raise ValueError(bits
)
581 if isinstance(value
, bytes
):
582 if ((len(value
) * 8) != bits
):
583 raise ValueError(f
"bit length mismatch")
584 value
= int.from_bytes(value
, byteorder
=byteorder
)
586 if isinstance(value
, int):
587 value
= _SelectableInt(value
=value
, bits
=bits
)
588 elif isinstance(value
, Instruction
):
589 value
= value
.storage
591 if not isinstance(value
, _SelectableInt
):
592 raise ValueError(value
)
595 if len(value
) != bits
:
596 raise ValueError(value
)
598 value
= _SelectableInt(value
=value
, bits
=bits
)
600 return cls(storage
=value
)
603 return hash(int(self
))
605 def disassemble(self
, db
):
606 raise NotImplementedError
609 class WordInstruction(Instruction
):
610 _
: _Field
= range(0, 32)
611 po
: _Field
= range(0, 6)
614 def integer(cls
, value
, byteorder
="little"):
615 return super().integer(bits
=32, value
=value
, byteorder
=byteorder
)
617 def disassemble(self
, db
):
620 yield f
".long 0x{int(self):08x}"
623 for operand
in record
.dynamic_operands
:
624 operand
= operand
.disassemble(self
, record
)
625 operands
.append(operand
)
627 operands
= ",".join(operands
)
628 operands
= f
" {operands}"
632 yield f
"{record.name}{operands}"
634 class PrefixedInstruction(Instruction
):
635 class Prefix(WordInstruction
.remap(range(0, 32))):
638 class Suffix(WordInstruction
.remap(range(32, 64))):
641 _
: _Field
= range(64)
647 def integer(cls
, value
, byteorder
="little"):
648 return super().integer(bits
=64, value
=value
, byteorder
=byteorder
)
651 def pair(cls
, prefix
=0, suffix
=0, byteorder
="little"):
652 def transform(value
):
653 return WordInstruction
.integer(value
=value
,
654 byteorder
=byteorder
)[0:32]
656 (prefix
, suffix
) = map(transform
, (prefix
, suffix
))
657 value
= _selectconcat(prefix
, suffix
)
659 return super().integer(value
=value
)
661 def disassemble(self
, db
):
662 record
= db
[self
.suffix
]
664 yield f
".llong 0x{int(self):016x}"
666 yield f
".llong 0x{int(self):016x} # {record.name}"
669 class SVP64Instruction(PrefixedInstruction
):
670 """SVP64 instruction: https://libre-soc.org/openpower/sv/svp64/"""
671 class Prefix(PrefixedInstruction
.Prefix
):
673 _
: _Field
= range(24)
675 mask
: _Field
= range(1, 4)
676 elwidth
: _Field
= range(4, 6)
677 ewsrc
: _Field
= range(6, 8)
678 subvl
: _Field
= range(8, 10)
679 extra
: _Field
= range(10, 19)
680 mode
: _Field
= range(19, 24)
681 extra2
: _Field
[4] = (
687 smask
: _Field
= range(16, 19)
688 extra3
: _Field
[3] = (
695 rm
: RM
= ((6, 8) + tuple(range(10, 32)))
699 def disassemble(self
, db
):
700 record
= db
[self
.suffix
]
702 yield f
".llong 0x{int(self):016x}"
704 yield f
".llong 0x{int(self):016x} # sv.{record.name}"
707 def parse(stream
, factory
):
708 lines
= filter(lambda line
: not line
.strip().startswith("#"), stream
)
709 entries
= _csv
.DictReader(lines
)
710 entries
= filter(lambda entry
: "TODO" not in frozenset(entry
.values()), entries
)
711 return tuple(map(factory
, entries
))
715 def __init__(self
, root
):
716 db
= _collections
.defaultdict(set)
717 path
= (root
/ "insndb.csv")
718 with
open(path
, "r", encoding
="UTF-8") as stream
:
719 for section
in parse(stream
, Section
.CSV
):
720 path
= (root
/ section
.path
)
722 section
.Mode
.INTEGER
: IntegerOpcode
,
723 section
.Mode
.PATTERN
: PatternOpcode
,
725 factory
= _functools
.partial(PPCRecord
.CSV
, opcode_cls
=opcode_cls
)
726 with
open(path
, "r", encoding
="UTF-8") as stream
:
727 db
[section
].update(parse(stream
, factory
))
729 return super().__init
__()
731 def __getitem__(self
, key
):
732 for (section
, records
) in self
.__db
.items():
733 for record
in records
:
734 for name
in record
.names
:
736 ((record
.rc
is _RC
.RC
) and
737 key
.endswith(".") and
739 return (section
, record
)
744 def __init__(self
, root
):
746 pattern
= _re
.compile(r
"^(?:LDST)?RM-(1P|2P)-.*?\.csv$")
747 for (prefix
, _
, names
) in _os
.walk(root
):
748 prefix
= _pathlib
.Path(prefix
)
749 for name
in filter(lambda name
: pattern
.match(name
), names
):
750 path
= (prefix
/ _pathlib
.Path(name
))
751 with
open(path
, "r", encoding
="UTF-8") as stream
:
752 db
.update(parse(stream
, SVP64Record
.CSV
))
753 self
.__db
= {record
.name
:record
for record
in db
}
754 return super().__init
__()
756 def __getitem__(self
, key
):
758 record
= self
.__db
.get(name
, None)
759 if record
is not None:
764 class FieldsDatabase
:
769 for (form
, fields
) in df
.instrs
.items():
770 if form
in {"DQE", "TX"}:
774 db
[_Form
[form
]] = Fields(fields
)
776 return super().__init
__()
778 def __getitem__(self
, key
):
779 return self
.__db
.__getitem
__(key
)
782 class MarkdownDatabase
:
785 for (name
, desc
) in _ISA():
788 (dynamic
, *static
) = desc
.regs
789 operands
.extend(dynamic
)
790 operands
.extend(static
)
791 db
[name
] = Operands(iterable
=operands
)
793 return super().__init
__()
796 yield from self
.__db
.items()
798 def __getitem__(self
, key
):
799 return self
.__db
.__getitem
__(key
)
803 def __init__(self
, root
):
804 root
= _pathlib
.Path(root
)
806 mdwndb
= MarkdownDatabase()
807 fieldsdb
= FieldsDatabase()
808 svp64db
= SVP64Database(root
)
809 ppcdb
= PPCDatabase(root
)
812 for (name
, operands
) in mdwndb
:
813 (section
, ppc
) = ppcdb
[name
]
816 svp64
= svp64db
[ppc
.names
]
817 fields
= fieldsdb
[ppc
.form
]
818 record
= Record(name
=name
,
819 section
=section
, ppc
=ppc
, svp64
=svp64
,
820 operands
=operands
, fields
=fields
)
823 self
.__db
= tuple(sorted(db
))
825 return super().__init
__()
828 return repr(self
.__db
)
833 @_functools.lru_cache(maxsize
=None)
834 def __contains__(self
, key
):
835 return self
.__getitem
__(key
) is not None
837 @_functools.lru_cache(maxsize
=None)
838 def __getitem__(self
, key
):
839 if isinstance(key
, (int, Instruction
)):
842 opcode
= record
.opcode
843 if ((opcode
.value
& opcode
.mask
) ==
844 (key
& opcode
.mask
)):
847 elif isinstance(key
, Opcode
):
849 if record
.opcode
== key
:
851 elif isinstance(key
, str):
853 if record
.name
== key
: