from collections import namedtuple
-from openpower.decoder.power_enums import find_wiki_file as _find_wiki_file
-from openpower.decoder.selectable_int import BitRange as _BitRange
+import operator
+import functools
+
+from openpower.decoder.power_enums import find_wiki_file
+from openpower.decoder.selectable_int import (
+ SelectableInt,
+ BitRange,
+ selectconcat,
+ selectltu,
+)
+
+
+class RemapError(ValueError):
+ pass
+
+
+class Descriptor:
+ def __init__(self, cls):
+ self.__cls = cls
+ return super().__init__()
+
+ def __get__(self, instance, owner):
+ if instance is None:
+ return self.__cls
+ return self.__cls(storage=instance.storage)
+
+ def __set__(self, instance, value):
+ if instance is None:
+ raise AttributeError("read-only attribute")
+ self.__cls(storage=instance.storage).assign(value)
+
+
+@functools.total_ordering
+class Reference:
+ def __init__(self, storage, *args, **kwargs):
+ if not isinstance(storage, SelectableInt):
+ raise ValueError(storage)
+
+ self.storage = storage
+
+ super().__init__()
+ self.__post_init__()
+
+ def __post_init__(self, *args, **kwargs):
+ _ = (args, kwargs)
+
+ def __binary_operator(self, op, other):
+ span = dict.fromkeys(self.__class__.span).keys()
+ lhs = selectconcat(*(self.storage[bit] for bit in span))
+
+ if isinstance(other, Reference):
+ span = dict.fromkeys(other.__class__.span).keys()
+ rhs = selectconcat(*(other.storage[bit] for bit in span))
+ elif isinstance(other, int):
+ bits = len(self.__class__)
+ if other.bit_length() > bits:
+ raise OverflowError(other)
+ rhs = SelectableInt(value=other, bits=bits)
+ elif isinstance(other, SelectableInt):
+ rhs = other
+ else:
+ raise ValueError(other)
+
+ return op(lhs, rhs)
+
+ def __lt__(self, other):
+ return self.__binary_operator(selectltu, other)
+
+ def __eq__(self, other):
+ return self.__binary_operator(operator.eq, other)
+
+ def __bool__(self):
+ return bool(int(self))
+
+ def __int__(self):
+ span = dict.fromkeys(self.__class__.span).keys()
+ return int(selectconcat(*(self.storage[bit] for bit in span)))
+
+ def __index__(self):
+ return int(self).__index__()
+
+ @property
+ def storage(self):
+ return self.__storage
+
+ @storage.setter
+ def storage(self, storage):
+ if not isinstance(storage, SelectableInt):
+ raise ValueError(storage)
+
+ self.__storage = storage
+
+ def assign(self, value, bits=None):
+ if bits is None:
+ bits = range(len(self.__class__))
+ elif isinstance(bits, int):
+ bits = (bits,)
+ elif isinstance(bits, slice):
+ assert bits.step is None or bits.step == 1
+ bits = range(bits.start, bits.stop)
+ bits = tuple(bits)
+
+ if isinstance(value, (int, self.__class__)):
+ value = int(value)
+ if value.bit_length() > len(bits):
+ raise OverflowError(value)
+ value = SelectableInt(value=value, bits=len(bits))
+ if not isinstance(value, SelectableInt):
+ raise ValueError(value)
+ if value.bits != len(bits):
+ raise OverflowError(value)
+
+ span = tuple(self.__class__.span)
+ mapping = dict(enumerate(span))
+ for (src, bit) in enumerate(bits):
+ if src >= value.bits:
+ raise OverflowError(src)
+ dst = mapping.get(bit)
+ if dst is None:
+ raise OverflowError(bit)
+ self.storage[dst] = value[src]
+
+
+class FieldMeta(type):
+ def __new__(metacls, clsname, bases, ns, items=()):
+ assert "__members__" not in ns
+
+ members = []
+ for item in items:
+ if not isinstance(item, int):
+ raise ValueError(item)
+ if item < 0:
+ raise ValueError(item)
+ members.append(item)
+
+ ns["__members__"] = tuple(members)
+
+ return super().__new__(metacls, clsname, bases, ns)
+
+ def __repr__(cls):
+ if not cls.__members__:
+ return cls.__name__
+ return f"{cls.__name__}{cls.__members__!r}"
+
+ def __iter__(cls):
+ yield from cls.__members__
+
+ def __len__(cls):
+ return len(cls.__members__)
+
+ def __getitem__(cls, selector):
+ if isinstance(selector, int):
+ selector = (selector,)
+
+ items = []
+ for idx in selector:
+ if not isinstance(idx, int):
+ raise ValueError(selector)
+ item = cls.__members__[idx]
+ items.append(item)
+
+ return cls.__class__(cls.__name__, (Field,), {}, items=items)
+
+ def remap(cls, scheme):
+ if isinstance(scheme, type) and issubclass(scheme, Mapping):
+ scheme = range(len(scheme))
+ scheme = cls.__class__(cls.__name__, (cls,), {}, items=scheme)
+
+ if len(cls) == 0:
+ return scheme
+ elif len(cls) > len(scheme):
+ llen = f"len(scheme)"
+ rlen = f"len({cls.__name__})"
+ raise RemapError(f"{llen} != {rlen}")
+
+ ns = {}
+ ns["__doc__"] = cls.__doc__
+ items = map(lambda item: scheme.__members__[item], cls)
+
+ return cls.__class__(cls.__name__, (cls,), ns, items=items)
+
+ @property
+ def span(cls):
+ return cls.__members__
+
+
+class Field(Reference, metaclass=FieldMeta):
+ def __repr__(self):
+ return f"[{len(self.__class__)}]0x{int(self):x}"
+
+ def __iter__(self):
+ for bit in self.__class__:
+ yield self.storage[bit]
+
+ def __getitem__(self, key):
+ if isinstance(key, int):
+ bit = self.storage[self.__class__.__members__[key]]
+ return SelectableInt(value=bit, bits=1)
+ if isinstance(key, slice):
+ assert key.step is None or key.step == 1
+ key = range(key.start, key.stop)
+
+ return selectconcat(*(self[bit] for bit in key))
+
+ def __setitem__(self, key, value):
+ return self.assign(value=value, bits=key)
+
+ @classmethod
+ def traverse(cls, path):
+ yield (path, cls.__members__)
+
+
+class MappingMeta(type):
+ def __new__(metacls, clsname, bases, ns):
+ members = {}
+
+ for cls in bases:
+ if isinstance(cls, metacls):
+ members.update(cls.__members__)
+
+ for (name, cls) in ns.get("__annotations__", {}).items():
+ if not (isinstance(cls, type) and
+ issubclass(cls, (Mapping, Field))):
+ raise ValueError(f"{clsname}.{name}: {cls!r}")
+
+ if name in ns:
+ try:
+ members[name] = cls.remap(ns[name])
+ except RemapError as error:
+ raise RemapError(f"{name}: {error}")
+ else:
+ if cls is Field:
+ raise ValueError(f"{clsname}.{name}: missing initializer")
+ members[name] = cls
+
+ ns["__members__"] = members
+ for (name, cls) in members.items():
+ ns[name] = Descriptor(cls)
+
+ return super().__new__(metacls, clsname, bases, ns)
+
+ def __repr__(cls):
+ return f"{cls.__name__}({cls.__members__!r})"
+
+ def __iter__(cls):
+ yield from cls.__members__.items()
+
+ def __len__(cls):
+ length = 0
+ for field in cls.__members__.values():
+ length = max(length, len(field))
+ return length
+
+ def __getitem__(cls, selector):
+ return cls.__members__["_"][selector]
+
+ def remap(cls, scheme):
+ ns = {}
+ annotations = {}
+
+ for (name, field) in cls:
+ annotations[name] = field.remap(scheme)
+ ns["__annotations__"] = annotations
+ ns["__doc__"] = cls.__doc__
+
+ return cls.__class__(cls.__name__, (cls,), ns)
+
+ @property
+ def span(cls):
+ for field in cls.__members__.values():
+ yield from field.span
+
+
+class Mapping(Reference, metaclass=MappingMeta):
+ def __init__(self, storage, **kwargs):
+ members = {}
+ for (name, cls) in self.__class__:
+ members[name] = cls(storage)
+
+ self.__members = members
+
+ return super().__init__(storage, **kwargs)
+
+ def __repr__(self):
+ items = tuple(f"{name}={field!r}" for (name, field) in self)
+ return f"{{{', '.join(items)}}}"
+
+ def __iter__(self):
+ yield from self.__members.items()
+
+ def __getitem__(self, key):
+ if isinstance(key, (int, slice, list, tuple, range)):
+ return self["_"].__getitem__(key)
+
+ return self.__members.__getitem__(key)
+
+ def __setitem__(self, key, value):
+ if isinstance(key, (int, slice, list, tuple, range)):
+ return self["_"].assign(value=value, bits=key)
+
+ return self.assign(value=value, bits=key)
+
+ def __getattr__(self, key):
+ raise AttributeError(key)
+
+ @classmethod
+ def traverse(cls, path):
+ for (name, member) in cls.__members__.items():
+ if name == "_":
+ yield from member.traverse(path=path)
+ elif path == "":
+ yield from member.traverse(path=name)
+ else:
+ yield from member.traverse(path=f"{path}.{name}")
def decode_instructions(form):
assert name_on_wiki is None
fname = "fields.txt"
name_on_wiki = "fields.text"
- self.fname = _find_wiki_file(name_on_wiki)
+ self.fname = find_wiki_file(name_on_wiki)
@property
def form_names(self):
#print ("decode", txt)
forms = {}
reading_data = False
- for l in txt:
+ for lineno, l in enumerate(txt):
l = l.strip()
if len(l) == 0:
continue
if l[0] == '#':
reading_data = False
else:
- forms[heading].append(l)
+ form = forms[heading]
+ form.append(l)
+ if len(form) <= 1:
+ continue
+ # check separators line up with header
+ for i, ch in enumerate(l):
+ if ch != '|':
+ continue
+ if i >= len(form[0]) or form[0][i] != '|':
+ col = len(txt[lineno]) - len(txt[lineno].lstrip())
+ col += i + 1
+ raise SyntaxError("form line field separator ('|') "
+ "with no corresponding separator in header",
+ (self.fname, lineno + 1, col, txt[lineno]))
if not reading_data:
assert l[0] == '#'
heading = l[1:].strip()