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
):
572 _
: _Field
= range(0, 6)
574 _
: _Field
= range(0, 32)
578 def integer(cls
, value
, byteorder
="little"):
579 return super().integer(bits
=32, value
=value
, byteorder
=byteorder
)
581 def disassemble(self
, db
):
584 yield f
".long 0x{int(self):08x}"
586 yield f
".long 0x{int(self):08x} # {record.name}"
589 class PrefixedInstruction(Instruction
):
590 class Prefix(WordInstruction
.remap(range(0, 32))):
593 class Suffix(WordInstruction
.remap(range(32, 64))):
596 _
: _Field
= range(64)
599 po
: Suffix
.PO
= Suffix
.po
602 def integer(cls
, value
, byteorder
="little"):
603 return super().integer(bits
=64, value
=value
, byteorder
=byteorder
)
606 def pair(cls
, prefix
=0, suffix
=0, byteorder
="little"):
607 def transform(value
):
608 return WordInstruction
.integer(value
=value
,
609 byteorder
=byteorder
)[0:32]
611 (prefix
, suffix
) = map(transform
, (prefix
, suffix
))
612 value
= _selectconcat(prefix
, suffix
)
614 return super().integer(value
=value
)
616 def disassemble(self
, db
):
617 record
= db
[self
.suffix
]
619 yield f
".long 0x{int(self.prefix):08x}"
620 yield f
".long 0x{int(self.suffix):08x}"
622 yield f
".llong 0x{int(self):08x} # {record.name}"
625 class SVP64Instruction(PrefixedInstruction
):
626 class Prefix(PrefixedInstruction
.Prefix
):
627 """SVP64 Prefix: https://libre-soc.org/openpower/sv/svp64/"""
629 """SVP64 RM: https://libre-soc.org/openpower/sv/svp64/"""
630 _
: _Field
= range(24)
632 mask
: _Field
= range(1, 4)
633 elwidth
: _Field
= range(4, 6)
634 ewsrc
: _Field
= range(6, 8)
635 subvl
: _Field
= range(8, 10)
636 extra
: _Field
= range(10, 19)
637 mode
: _Field
= range(19, 24)
638 extra2
: _Field
[4] = (
644 smask
: _Field
= range(16, 19)
645 extra3
: _Field
[3] = (
651 # Backward compatibility
655 rm
: RM
= ((6, 8) + tuple(range(10, 32)))
657 # Backward compatibility
658 insn
: PrefixedInstruction
.Prefix
662 def disassemble(self
, db
):
663 record
= db
[self
.suffix
]
665 yield f
".llong 0x{int(self):08x}"
667 yield f
".llong 0x{int(self):08x} # sv.{record.name}"
671 def __init__(self
, root
):
672 root
= _pathlib
.Path(root
)
674 def parse(stream
, factory
):
675 lines
= filter(lambda line
: not line
.strip().startswith("#"), stream
)
676 entries
= _csv
.DictReader(lines
)
677 entries
= filter(lambda entry
: "TODO" not in frozenset(entry
.values()), entries
)
678 return tuple(map(factory
, entries
))
680 def database_ppc(root
):
681 db
= _collections
.defaultdict(set)
682 path
= (root
/ "insndb.csv")
683 with
open(path
, "r", encoding
="UTF-8") as stream
:
684 for section
in parse(stream
, Section
.CSV
):
685 path
= (root
/ section
.path
)
687 section
.Mode
.INTEGER
: IntegerOpcode
,
688 section
.Mode
.PATTERN
: PatternOpcode
,
690 factory
= _functools
.partial(PPCRecord
.CSV
, opcode_cls
=opcode_cls
)
691 with
open(path
, "r", encoding
="UTF-8") as stream
:
692 db
[section
].update(parse(stream
, factory
))
693 for (section
, records
) in db
.items():
694 db
[section
] = {record
.identifier
:record
for record
in records
}
697 def database_svp64(root
):
699 pattern
= _re
.compile(r
"^(?:LDST)?RM-(1P|2P)-.*?\.csv$")
700 for (prefix
, _
, names
) in _os
.walk(root
):
701 prefix
= _pathlib
.Path(prefix
)
702 for name
in filter(lambda name
: pattern
.match(name
), names
):
703 path
= (prefix
/ _pathlib
.Path(name
))
704 with
open(path
, "r", encoding
="UTF-8") as stream
:
705 db
.update(parse(stream
, SVP64Record
.CSV
))
706 db
= {record
.identifier
:record
for record
in db
}
709 def database_forms(root
):
710 # This is hack. The whole code there should be moved here.
711 # The fields.text parser should take care of the validation.
712 from openpower
.decoder
.power_fields
import DecodeFields
as _DecodeFields
716 for (form
, fields
) in df
.instrs
.items():
717 if form
in {"DQE", "TX"}:
721 db
[_Form
[form
]] = Fields(fields
)
724 def database(ppcdb
, svp64db
, formsdb
):
726 for section
in ppcdb
:
727 for (identifier
, ppc
) in ppcdb
[section
].items():
728 fields
= formsdb
[ppc
.form
]
729 svp64
= svp64db
.get(identifier
)
730 if ppc
.rc
is _RC
.ONE
:
731 variants
= {name
:True for name
in ppc
.names
}
732 elif ppc
.rc
is _RC
.RC
:
733 variants
= {name
:False for name
in ppc
.names
}
734 variants
.update({f
"{name}.":True for name
in ppc
.names
})
736 variants
= {name
:False for name
in ppc
.names
}
737 for (name
, rc
) in variants
.items():
738 items
.add(Record(name
=name
, rc
=rc
,
739 section
=section
, ppc
=ppc
, fields
=fields
, svp64
=svp64
))
741 items
= tuple(sorted(items
, key
=_operator
.attrgetter("opcode")))
742 opcodes
= {item
.opcode
:item
for item
in items
}
743 names
= {item
.name
:item
for item
in sorted(items
, key
=_operator
.attrgetter("name"))}
745 return (items
, opcodes
, names
)
747 ppcdb
= database_ppc(root
)
748 svp64db
= database_svp64(root
)
749 formsdb
= database_forms(root
)
751 (items
, opcodes
, names
) = database(ppcdb
, svp64db
, formsdb
)
753 self
.__opcodes
= opcodes
756 return super().__init
__()
759 return repr(self
.__items
)
762 yield from self
.__items
764 @_functools.lru_cache(maxsize
=None)
765 def __contains__(self
, key
):
766 if isinstance(key
, int):
767 return self
.__opcodes
.__contains
__(key
)
768 elif isinstance(key
, int):
769 for (opcode
, insn
) in self
.__opcodes
.items():
770 if ((opcode
.value
& opcode
.mask
) ==
771 (key
& opcode
.mask
)):
774 elif isinstance(key
, str):
775 return self
.__names
.__contains
__(key
)
779 @_functools.lru_cache(maxsize
=None)
780 def __getitem__(self
, key
):
781 if isinstance(key
, Opcode
):
782 return self
.__opcodes
.__getitem
__(key
)
783 elif isinstance(key
, (int, Instruction
)):
785 for (opcode
, insn
) in self
.__opcodes
.items():
786 if ((opcode
.value
& opcode
.mask
) ==
787 (ikey
& opcode
.mask
)):
790 elif isinstance(key
, str):
791 return self
.__names
.__getitem
__(key
)