1 import collections
as _collections
3 import dataclasses
as _dataclasses
5 import functools
as _functools
7 import operator
as _operator
8 import pathlib
as _pathlib
12 from functools
import cached_property
14 from cached_property
import cached_property
16 from openpower
.decoder
.power_enums
import (
17 Function
as _Function
,
24 CROutSel
as _CROutSel
,
26 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
,
42 # TODO: these should be present in the decoder module.
43 from openpower
.decoder
.power_fields
import (
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
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
)
231 def identifier(self
):
236 return frozenset(self
.comment
.split("=")[-1].split("/"))
239 @_dataclasses.dataclass(eq
=True, frozen
=True)
241 class ExtraMap(tuple):
243 @_dataclasses.dataclass(eq
=True, frozen
=True)
245 regtype
: _SVExtraRegType
= _SVExtraRegType
.NONE
246 reg
: _SVExtraReg
= _SVExtraReg
.NONE
249 return f
"{self.regtype.value}:{self.reg.name}"
251 def __new__(cls
, value
="0"):
252 if isinstance(value
, str):
253 def transform(value
):
254 (regtype
, reg
) = value
.split(":")
255 regtype
= _SVExtraRegType(regtype
)
256 reg
= _SVExtraReg(reg
)
257 return cls
.Entry(regtype
=regtype
, reg
=reg
)
262 value
= map(transform
, value
.split(";"))
264 return super().__new
__(cls
, value
)
267 return repr(list(self
))
269 def __new__(cls
, value
=tuple()):
273 return super().__new
__(cls
, map(cls
.Extra
, value
))
276 return repr({index
:self
[index
] for index
in range(0, 4)})
279 ptype
: _SVPtype
= _SVPtype
.NONE
280 etype
: _SVEtype
= _SVEtype
.NONE
281 in1
: _In1Sel
= _In1Sel
.NONE
282 in2
: _In2Sel
= _In2Sel
.NONE
283 in3
: _In3Sel
= _In3Sel
.NONE
284 out
: _OutSel
= _OutSel
.NONE
285 out2
: _OutSel
= _OutSel
.NONE
286 cr_in
: _CRInSel
= _CRInSel
.NONE
287 cr_out
: _CROutSel
= _CROutSel
.NONE
288 extra
: ExtraMap
= ExtraMap()
291 mode
: _SVMode
= _SVMode
.NORMAL
294 "insn": "identifier",
295 "CONDITIONS": "conditions",
304 def CSV(cls
, record
):
305 for key
in ("in1", "in2", "in3", "out", "out2", "CR in", "CR out"):
310 record
["extra"] = cls
.ExtraMap(record
.pop(f
"{index}") for index
in range(0, 4))
312 return dataclass(cls
, record
, keymap
=cls
.__KEYMAP
)
316 def __init__(self
, value
=(0, 32)):
317 if isinstance(value
, str):
318 (start
, end
) = map(int, value
.split(":"))
321 if start
< 0 or end
< 0 or start
>= end
:
322 raise ValueError(value
)
327 return super().__init
__()
330 return f
"[{self.__start}:{self.__end}]"
333 yield from range(self
.start
, (self
.end
+ 1))
344 @_dataclasses.dataclass(eq
=True, frozen
=True)
346 class Mode(_enum
.Enum
):
347 INTEGER
= _enum
.auto()
348 PATTERN
= _enum
.auto()
351 def _missing_(cls
, value
):
352 if isinstance(value
, str):
353 return cls
[value
.upper()]
354 return super()._missing
_(value
)
357 def __new__(cls
, value
=None):
358 if isinstance(value
, str):
359 if value
.upper() == "NONE":
362 value
= int(value
, 0)
366 return super().__new
__(cls
, value
)
372 return (bin(self
) if self
else "None")
381 def CSV(cls
, record
):
382 return dataclass(cls
, record
)
386 def __init__(self
, items
):
387 if isinstance(items
, dict):
388 items
= items
.items()
391 (name
, bitrange
) = item
392 return (name
, tuple(bitrange
.values()))
394 self
.__mapping
= dict(map(transform
, items
))
396 return super().__init
__()
399 return repr(self
.__mapping
)
401 def __contains__(self
, key
):
402 return self
.__mapping
.__contains
__(key
)
404 def __getitem__(self
, key
):
405 return self
.__mapping
.__getitem
__(key
)
407 def get(self
, key
, default
):
408 return self
.__mapping
.get(key
, default
)
411 @_functools.total_ordering
412 @_dataclasses.dataclass(eq
=True, frozen
=True)
419 svp64
: SVP64Record
= None
428 def __lt__(self
, other
):
429 if not isinstance(other
, Record
):
430 return NotImplemented
431 return (self
.opcode
< other
.opcode
)
434 return f
"{self.__class__.__name__}(name={self.name!r}, opcode={self.opcode})"
439 if self
.section
.opcode
:
440 fields
+= [(self
.section
.opcode
.value
, BitSel((0, 5)))]
441 fields
+= [(self
.ppc
.opcode
.value
, self
.section
.bitsel
)]
443 fields
+= [(self
.ppc
.opcode
.value
, self
.section
.bitsel
)]
445 # Some instructions are special regarding Rc handling.
446 # They are marked with Rc.ONE, but don't have Rc field.
447 # At least addic., andi., andis. belong to this list.
448 if self
.rc
and "Rc" in self
.fields
:
449 fields
+= [(1, self
.fields
["Rc"])]
451 return FieldsOpcode(fields
)
455 return self
.ppc
.function
475 if self
.svp64
is None:
481 return self
.ppc
.cr_in
485 return self
.ppc
.cr_out
487 def sv_extra(self
, key
):
488 if key
not in frozenset({
489 "in1", "in2", "in3", "cr_in",
490 "out", "out2", "cr_out",
494 sel
= getattr(self
.svp64
, key
)
495 if sel
is _CRInSel
.BA_BB
:
496 return _SVExtra
.Idx_1_2
497 reg
= _SVExtraReg(sel
)
498 if reg
is _SVExtraReg
.NONE
:
502 _SVExtraRegType
.SRC
: {},
503 _SVExtraRegType
.DST
: {},
505 for index
in range(0, 4):
506 for entry
in self
.svp64
.extra
[index
]:
507 extra_map
[entry
.regtype
][entry
.reg
] = Record
.__EXTRA
[index
]
509 for regtype
in (_SVExtraRegType
.SRC
, _SVExtraRegType
.DST
):
510 extra
= extra_map
[regtype
].get(reg
, _SVExtra
.NONE
)
511 if extra
is not _SVExtra
.NONE
:
516 sv_in1
= property(_functools
.partial(sv_extra
, key
="in1"))
517 sv_in2
= property(_functools
.partial(sv_extra
, key
="in2"))
518 sv_in3
= property(_functools
.partial(sv_extra
, key
="in3"))
519 sv_out
= property(_functools
.partial(sv_extra
, key
="out"))
520 sv_out2
= property(_functools
.partial(sv_extra
, key
="out2"))
521 sv_cr_in
= property(_functools
.partial(sv_extra
, key
="cr_in"))
522 sv_cr_out
= property(_functools
.partial(sv_extra
, key
="cr_out"))
526 if self
.svp64
is None:
528 return self
.svp64
.ptype
532 if self
.svp64
is None:
534 return self
.svp64
.etype
537 class Instruction(_Mapping
):
539 def integer(cls
, value
=0, bits
=None, byteorder
="little"):
540 if isinstance(value
, (int, bytes
)) and not isinstance(bits
, int):
541 raise ValueError(bits
)
543 if isinstance(value
, bytes
):
544 if ((len(value
) * 8) != bits
):
545 raise ValueError(f
"bit length mismatch")
546 value
= int.from_bytes(value
, byteorder
=byteorder
)
548 if isinstance(value
, int):
549 value
= _SelectableInt(value
=value
, bits
=bits
)
550 elif isinstance(value
, Instruction
):
551 value
= value
.storage
553 if not isinstance(value
, _SelectableInt
):
554 raise ValueError(value
)
557 if len(value
) != bits
:
558 raise ValueError(value
)
560 value
= _SelectableInt(value
=value
, bits
=bits
)
562 return cls(storage
=value
)
565 return hash(int(self
))
567 def disassemble(self
, db
):
568 raise NotImplementedError
571 class WordInstruction(Instruction
):
572 _
: _Field
= range(0, 32)
573 po
: _Field
= range(0, 6)
576 def integer(cls
, value
, byteorder
="little"):
577 return super().integer(bits
=32, value
=value
, byteorder
=byteorder
)
579 def disassemble(self
, db
):
582 yield f
".long 0x{int(self):08x}"
584 yield f
".long 0x{int(self):08x} # {record.name}"
587 class PrefixedInstruction(Instruction
):
588 class Prefix(WordInstruction
.remap(range(0, 32))):
591 class Suffix(WordInstruction
.remap(range(32, 64))):
594 _
: _Field
= range(64)
600 def integer(cls
, value
, byteorder
="little"):
601 return super().integer(bits
=64, value
=value
, byteorder
=byteorder
)
604 def pair(cls
, prefix
=0, suffix
=0, byteorder
="little"):
605 def transform(value
):
606 return WordInstruction
.integer(value
=value
,
607 byteorder
=byteorder
)[0:32]
609 (prefix
, suffix
) = map(transform
, (prefix
, suffix
))
610 value
= _selectconcat(prefix
, suffix
)
612 return super().integer(value
=value
)
614 def disassemble(self
, db
):
615 record
= db
[self
.suffix
]
617 yield f
".long 0x{int(self.prefix):08x}"
618 yield f
".long 0x{int(self.suffix):08x}"
620 yield f
".llong 0x{int(self):08x} # {record.name}"
623 class SVP64Instruction(PrefixedInstruction
):
624 """SVP64 instruction: https://libre-soc.org/openpower/sv/svp64/"""
625 class Prefix(PrefixedInstruction
.Prefix
):
627 _
: _Field
= range(24)
629 mask
: _Field
= range(1, 4)
630 elwidth
: _Field
= range(4, 6)
631 ewsrc
: _Field
= range(6, 8)
632 subvl
: _Field
= range(8, 10)
633 extra
: _Field
= range(10, 19)
634 mode
: _Field
= range(19, 24)
635 extra2
: _Field
[4] = (
641 smask
: _Field
= range(16, 19)
642 extra3
: _Field
[3] = (
649 rm
: RM
= ((6, 8) + tuple(range(10, 32)))
653 def disassemble(self
, db
):
654 record
= db
[self
.suffix
]
656 yield f
".llong 0x{int(self):08x}"
658 yield f
".llong 0x{int(self):08x} # sv.{record.name}"
662 def __init__(self
, root
):
663 root
= _pathlib
.Path(root
)
665 def parse(stream
, factory
):
666 lines
= filter(lambda line
: not line
.strip().startswith("#"), stream
)
667 entries
= _csv
.DictReader(lines
)
668 entries
= filter(lambda entry
: "TODO" not in frozenset(entry
.values()), entries
)
669 return tuple(map(factory
, entries
))
671 def database_ppc(root
):
672 db
= _collections
.defaultdict(set)
673 path
= (root
/ "insndb.csv")
674 with
open(path
, "r", encoding
="UTF-8") as stream
:
675 for section
in parse(stream
, Section
.CSV
):
676 path
= (root
/ section
.path
)
678 section
.Mode
.INTEGER
: IntegerOpcode
,
679 section
.Mode
.PATTERN
: PatternOpcode
,
681 factory
= _functools
.partial(PPCRecord
.CSV
, opcode_cls
=opcode_cls
)
682 with
open(path
, "r", encoding
="UTF-8") as stream
:
683 db
[section
].update(parse(stream
, factory
))
684 for (section
, records
) in db
.items():
685 db
[section
] = {record
.identifier
:record
for record
in records
}
688 def database_svp64(root
):
690 pattern
= _re
.compile(r
"^(?:LDST)?RM-(1P|2P)-.*?\.csv$")
691 for (prefix
, _
, names
) in _os
.walk(root
):
692 prefix
= _pathlib
.Path(prefix
)
693 for name
in filter(lambda name
: pattern
.match(name
), names
):
694 path
= (prefix
/ _pathlib
.Path(name
))
695 with
open(path
, "r", encoding
="UTF-8") as stream
:
696 db
.update(parse(stream
, SVP64Record
.CSV
))
697 db
= {record
.identifier
:record
for record
in db
}
700 def database_forms(root
):
701 # This is hack. The whole code there should be moved here.
702 # The fields.text parser should take care of the validation.
703 from openpower
.decoder
.power_fields
import DecodeFields
as _DecodeFields
707 for (form
, fields
) in df
.instrs
.items():
708 if form
in {"DQE", "TX"}:
712 db
[_Form
[form
]] = Fields(fields
)
715 def database(ppcdb
, svp64db
, formsdb
):
717 for section
in ppcdb
:
718 for (identifier
, ppc
) in ppcdb
[section
].items():
719 fields
= formsdb
[ppc
.form
]
720 svp64
= svp64db
.get(identifier
)
721 if ppc
.rc
is _RC
.ONE
:
722 variants
= {name
:True for name
in ppc
.names
}
723 elif ppc
.rc
is _RC
.RC
:
724 variants
= {name
:False for name
in ppc
.names
}
725 variants
.update({f
"{name}.":True for name
in ppc
.names
})
727 variants
= {name
:False for name
in ppc
.names
}
728 for (name
, rc
) in variants
.items():
729 items
.add(Record(name
=name
, rc
=rc
,
730 section
=section
, ppc
=ppc
, fields
=fields
, svp64
=svp64
))
732 items
= tuple(sorted(items
, key
=_operator
.attrgetter("opcode")))
733 opcodes
= {item
.opcode
:item
for item
in items
}
734 names
= {item
.name
:item
for item
in sorted(items
, key
=_operator
.attrgetter("name"))}
736 return (items
, opcodes
, names
)
738 ppcdb
= database_ppc(root
)
739 svp64db
= database_svp64(root
)
740 formsdb
= database_forms(root
)
742 (items
, opcodes
, names
) = database(ppcdb
, svp64db
, formsdb
)
744 self
.__opcodes
= opcodes
747 return super().__init
__()
750 return repr(self
.__items
)
753 yield from self
.__items
755 @_functools.lru_cache(maxsize
=None)
756 def __contains__(self
, key
):
757 if isinstance(key
, int):
758 return self
.__opcodes
.__contains
__(key
)
759 elif isinstance(key
, int):
760 for (opcode
, insn
) in self
.__opcodes
.items():
761 if ((opcode
.value
& opcode
.mask
) ==
762 (key
& opcode
.mask
)):
765 elif isinstance(key
, str):
766 return self
.__names
.__contains
__(key
)
770 @_functools.lru_cache(maxsize
=None)
771 def __getitem__(self
, key
):
772 if isinstance(key
, Opcode
):
773 return self
.__opcodes
.__getitem
__(key
)
774 elif isinstance(key
, (int, Instruction
)):
776 for (opcode
, insn
) in self
.__opcodes
.items():
777 if ((opcode
.value
& opcode
.mask
) ==
778 (ikey
& opcode
.mask
)):
781 elif isinstance(key
, str):
782 return self
.__names
.__getitem
__(key
)