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 unit
: _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
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
)
230 def identifier(self
):
235 return frozenset(self
.comment
.split("=")[-1].split("/"))
238 @_dataclasses.dataclass(eq
=True, frozen
=True)
240 class ExtraMap(tuple):
242 @_dataclasses.dataclass(eq
=True, frozen
=True)
244 regtype
: _SVExtraRegType
= _SVExtraRegType
.NONE
245 reg
: _SVExtraReg
= _SVExtraReg
.NONE
248 return f
"{self.regtype.value}:{self.reg.name}"
250 def __new__(cls
, value
="0"):
251 if isinstance(value
, str):
252 def transform(value
):
253 (regtype
, reg
) = value
.split(":")
254 regtype
= _SVExtraRegType(regtype
)
255 reg
= _SVExtraReg(reg
)
256 return cls
.Entry(regtype
=regtype
, reg
=reg
)
261 value
= map(transform
, value
.split(";"))
263 return super().__new
__(cls
, value
)
266 return repr(list(self
))
268 def __new__(cls
, value
=tuple()):
272 return super().__new
__(cls
, map(cls
.Extra
, value
))
275 return repr({index
:self
[index
] for index
in range(0, 4)})
278 ptype
: _SVPtype
= _SVPtype
.NONE
279 etype
: _SVEtype
= _SVEtype
.NONE
280 in1
: _In1Sel
= _In1Sel
.NONE
281 in2
: _In2Sel
= _In2Sel
.NONE
282 in3
: _In3Sel
= _In3Sel
.NONE
283 out
: _OutSel
= _OutSel
.NONE
284 out2
: _OutSel
= _OutSel
.NONE
285 cr_in
: _CRInSel
= _CRInSel
.NONE
286 cr_out
: _CROutSel
= _CROutSel
.NONE
287 extra
: ExtraMap
= ExtraMap()
290 mode
: _SVMode
= _SVMode
.NORMAL
293 "insn": "identifier",
294 "CONDITIONS": "conditions",
303 def CSV(cls
, record
):
304 for key
in ("in1", "in2", "in3", "out", "out2", "CR in", "CR out"):
309 record
["extra"] = cls
.ExtraMap(record
.pop(f
"{index}") for index
in range(0, 4))
311 return dataclass(cls
, record
, keymap
=cls
.__KEYMAP
)
315 def __init__(self
, value
=(0, 32)):
316 if isinstance(value
, str):
317 (start
, end
) = map(int, value
.split(":"))
320 if start
< 0 or end
< 0 or start
>= end
:
321 raise ValueError(value
)
326 return super().__init
__()
329 return f
"[{self.__start}:{self.__end}]"
332 yield from range(self
.start
, (self
.end
+ 1))
343 @_dataclasses.dataclass(eq
=True, frozen
=True)
345 class Mode(_enum
.Enum
):
346 INTEGER
= _enum
.auto()
347 PATTERN
= _enum
.auto()
350 def _missing_(cls
, value
):
351 if isinstance(value
, str):
352 return cls
[value
.upper()]
353 return super()._missing
_(value
)
356 def __new__(cls
, value
=None):
357 if isinstance(value
, str):
358 if value
.upper() == "NONE":
361 value
= int(value
, 0)
365 return super().__new
__(cls
, value
)
371 return (bin(self
) if self
else "None")
380 def CSV(cls
, record
):
381 return dataclass(cls
, record
)
385 def __init__(self
, items
):
386 if isinstance(items
, dict):
387 items
= items
.items()
390 (name
, bitrange
) = item
391 return (name
, tuple(bitrange
.values()))
393 self
.__mapping
= dict(map(transform
, items
))
395 return super().__init
__()
398 return repr(self
.__mapping
)
400 def __contains__(self
, key
):
401 return self
.__mapping
.__contains
__(key
)
403 def __getitem__(self
, key
):
404 return self
.__mapping
.__getitem
__(key
)
406 def get(self
, key
, default
):
407 return self
.__mapping
.get(key
, default
)
410 @_functools.total_ordering
411 @_dataclasses.dataclass(eq
=True, frozen
=True)
418 svp64
: SVP64Record
= None
427 def __lt__(self
, other
):
428 if not isinstance(other
, Record
):
429 return NotImplemented
430 return (self
.opcode
< other
.opcode
)
433 return f
"{self.__class__.__name__}(name={self.name!r}, opcode={self.opcode})"
438 if self
.section
.opcode
:
439 fields
+= [(self
.section
.opcode
.value
, BitSel((0, 5)))]
440 fields
+= [(self
.ppc
.opcode
.value
, self
.section
.bitsel
)]
442 fields
+= [(self
.ppc
.opcode
.value
, self
.section
.bitsel
)]
444 # Some instructions are special regarding Rc handling.
445 # They are marked with Rc.ONE, but don't have Rc field.
446 # At least addic., andi., andis. belong to this list.
447 if self
.rc
and "Rc" in self
.fields
:
448 fields
+= [(1, self
.fields
["Rc"])]
450 return FieldsOpcode(fields
)
474 if self
.svp64
is None:
480 return self
.ppc
.cr_in
484 return self
.ppc
.cr_out
486 def sv_extra(self
, key
):
487 if key
not in frozenset({
488 "in1", "in2", "in3", "cr_in",
489 "out", "out2", "cr_out",
493 sel
= getattr(self
.svp64
, key
)
494 if sel
is _CRInSel
.BA_BB
:
495 return _SVExtra
.Idx_1_2
496 reg
= _SVExtraReg(sel
)
497 if reg
is _SVExtraReg
.NONE
:
501 _SVExtraRegType
.SRC
: {},
502 _SVExtraRegType
.DST
: {},
504 for index
in range(0, 4):
505 for entry
in self
.svp64
.extra
[index
]:
506 extra_map
[entry
.regtype
][entry
.reg
] = Record
.__EXTRA
[index
]
508 for regtype
in (_SVExtraRegType
.SRC
, _SVExtraRegType
.DST
):
509 extra
= extra_map
[regtype
].get(reg
, _SVExtra
.NONE
)
510 if extra
is not _SVExtra
.NONE
:
515 sv_in1
= property(_functools
.partial(sv_extra
, key
="in1"))
516 sv_in2
= property(_functools
.partial(sv_extra
, key
="in2"))
517 sv_in3
= property(_functools
.partial(sv_extra
, key
="in3"))
518 sv_out
= property(_functools
.partial(sv_extra
, key
="out"))
519 sv_out2
= property(_functools
.partial(sv_extra
, key
="out2"))
520 sv_cr_in
= property(_functools
.partial(sv_extra
, key
="cr_in"))
521 sv_cr_out
= property(_functools
.partial(sv_extra
, key
="cr_out"))
525 if self
.svp64
is None:
527 return self
.svp64
.ptype
531 if self
.svp64
is None:
533 return self
.svp64
.etype
536 class Instruction(_Mapping
):
538 def integer(cls
, value
=0, bits
=None, byteorder
="little"):
539 if isinstance(value
, (int, bytes
)) and not isinstance(bits
, int):
540 raise ValueError(bits
)
542 if isinstance(value
, bytes
):
543 if ((len(value
) * 8) != bits
):
544 raise ValueError(f
"bit length mismatch")
545 value
= int.from_bytes(value
, byteorder
=byteorder
)
547 if isinstance(value
, int):
548 value
= _SelectableInt(value
=value
, bits
=bits
)
549 elif isinstance(value
, Instruction
):
550 value
= value
.storage
552 if not isinstance(value
, _SelectableInt
):
553 raise ValueError(value
)
556 if len(value
) != bits
:
557 raise ValueError(value
)
559 value
= _SelectableInt(value
=value
, bits
=bits
)
561 return cls(storage
=value
)
564 return hash(int(self
))
566 def disassemble(self
, db
):
567 raise NotImplementedError
570 class WordInstruction(Instruction
):
571 _
: _Field
= range(0, 32)
572 PO
: _Field
= range(0, 6)
575 def integer(cls
, value
, byteorder
="little"):
576 return super().integer(bits
=32, value
=value
, byteorder
=byteorder
)
578 def disassemble(self
, db
):
581 yield f
".long 0x{int(self):08x}"
583 yield f
".long 0x{int(self):08x} # {record.name}"
586 class PrefixedInstruction(Instruction
):
587 class Prefix(WordInstruction
.remap(range(0, 32))):
590 class Suffix(WordInstruction
.remap(range(32, 64))):
593 _
: _Field
= range(64)
599 def integer(cls
, value
, byteorder
="little"):
600 return super().integer(bits
=64, value
=value
, byteorder
=byteorder
)
603 def pair(cls
, prefix
=0, suffix
=0, byteorder
="little"):
604 def transform(value
):
605 return WordInstruction
.integer(value
=value
,
606 byteorder
=byteorder
)[0:32]
608 (prefix
, suffix
) = map(transform
, (prefix
, suffix
))
609 value
= _selectconcat(prefix
, suffix
)
611 return super().integer(value
=value
)
613 def disassemble(self
, db
):
614 record
= db
[self
.suffix
]
616 yield f
".long 0x{int(self.prefix):08x}"
617 yield f
".long 0x{int(self.suffix):08x}"
619 yield f
".llong 0x{int(self):08x} # {record.name}"
622 class SVP64Instruction(PrefixedInstruction
):
623 """SVP64 instruction: https://libre-soc.org/openpower/sv/svp64/"""
624 class Prefix(PrefixedInstruction
.Prefix
):
626 _
: _Field
= range(24)
628 mask
: _Field
= range(1, 4)
629 elwidth
: _Field
= range(4, 6)
630 ewsrc
: _Field
= range(6, 8)
631 subvl
: _Field
= range(8, 10)
632 extra
: _Field
= range(10, 19)
633 mode
: _Field
= range(19, 24)
634 extra2
: _Field
[4] = (
640 smask
: _Field
= range(16, 19)
641 extra3
: _Field
[3] = (
648 rm
: RM
= ((6, 8) + tuple(range(10, 32)))
652 def disassemble(self
, db
):
653 record
= db
[self
.suffix
]
655 yield f
".llong 0x{int(self):08x}"
657 yield f
".llong 0x{int(self):08x} # sv.{record.name}"
661 def __init__(self
, root
):
662 root
= _pathlib
.Path(root
)
664 def parse(stream
, factory
):
665 lines
= filter(lambda line
: not line
.strip().startswith("#"), stream
)
666 entries
= _csv
.DictReader(lines
)
667 entries
= filter(lambda entry
: "TODO" not in frozenset(entry
.values()), entries
)
668 return tuple(map(factory
, entries
))
670 def database_ppc(root
):
671 db
= _collections
.defaultdict(set)
672 path
= (root
/ "insndb.csv")
673 with
open(path
, "r", encoding
="UTF-8") as stream
:
674 for section
in parse(stream
, Section
.CSV
):
675 path
= (root
/ section
.path
)
677 section
.Mode
.INTEGER
: IntegerOpcode
,
678 section
.Mode
.PATTERN
: PatternOpcode
,
680 factory
= _functools
.partial(PPCRecord
.CSV
, opcode_cls
=opcode_cls
)
681 with
open(path
, "r", encoding
="UTF-8") as stream
:
682 db
[section
].update(parse(stream
, factory
))
683 for (section
, records
) in db
.items():
684 db
[section
] = {record
.identifier
:record
for record
in records
}
687 def database_svp64(root
):
689 pattern
= _re
.compile(r
"^(?:LDST)?RM-(1P|2P)-.*?\.csv$")
690 for (prefix
, _
, names
) in _os
.walk(root
):
691 prefix
= _pathlib
.Path(prefix
)
692 for name
in filter(lambda name
: pattern
.match(name
), names
):
693 path
= (prefix
/ _pathlib
.Path(name
))
694 with
open(path
, "r", encoding
="UTF-8") as stream
:
695 db
.update(parse(stream
, SVP64Record
.CSV
))
696 db
= {record
.identifier
:record
for record
in db
}
699 def database_forms(root
):
700 # This is hack. The whole code there should be moved here.
701 # The fields.text parser should take care of the validation.
702 from openpower
.decoder
.power_fields
import DecodeFields
as _DecodeFields
706 for (form
, fields
) in df
.instrs
.items():
707 if form
in {"DQE", "TX"}:
711 db
[_Form
[form
]] = Fields(fields
)
714 def database(ppcdb
, svp64db
, formsdb
):
716 for section
in ppcdb
:
717 for (identifier
, ppc
) in ppcdb
[section
].items():
718 fields
= formsdb
[ppc
.form
]
719 svp64
= svp64db
.get(identifier
)
720 if ppc
.rc
is _RC
.ONE
:
721 variants
= {name
:True for name
in ppc
.names
}
722 elif ppc
.rc
is _RC
.RC
:
723 variants
= {name
:False for name
in ppc
.names
}
724 variants
.update({f
"{name}.":True for name
in ppc
.names
})
726 variants
= {name
:False for name
in ppc
.names
}
727 for (name
, rc
) in variants
.items():
728 items
.add(Record(name
=name
, rc
=rc
,
729 section
=section
, ppc
=ppc
, fields
=fields
, svp64
=svp64
))
731 items
= tuple(sorted(items
, key
=_operator
.attrgetter("opcode")))
732 opcodes
= {item
.opcode
:item
for item
in items
}
733 names
= {item
.name
:item
for item
in sorted(items
, key
=_operator
.attrgetter("name"))}
735 return (items
, opcodes
, names
)
737 ppcdb
= database_ppc(root
)
738 svp64db
= database_svp64(root
)
739 formsdb
= database_forms(root
)
741 (items
, opcodes
, names
) = database(ppcdb
, svp64db
, formsdb
)
743 self
.__opcodes
= opcodes
746 return super().__init
__()
749 return repr(self
.__items
)
752 yield from self
.__items
754 @_functools.lru_cache(maxsize
=None)
755 def __contains__(self
, key
):
756 if isinstance(key
, int):
757 return self
.__opcodes
.__contains
__(key
)
758 elif isinstance(key
, int):
759 for (opcode
, insn
) in self
.__opcodes
.items():
760 if ((opcode
.value
& opcode
.mask
) ==
761 (key
& opcode
.mask
)):
764 elif isinstance(key
, str):
765 return self
.__names
.__contains
__(key
)
769 @_functools.lru_cache(maxsize
=None)
770 def __getitem__(self
, key
):
771 if isinstance(key
, Opcode
):
772 return self
.__opcodes
.__getitem
__(key
)
773 elif isinstance(key
, (int, Instruction
)):
775 for (opcode
, insn
) in self
.__opcodes
.items():
776 if ((opcode
.value
& opcode
.mask
) ==
777 (ikey
& opcode
.mask
)):
780 elif isinstance(key
, str):
781 return self
.__names
.__getitem
__(key
)