From 99c310a7f57b78331ffe3e1b70523cd8bf9faa12 Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Tue, 19 Apr 2022 21:27:33 +0000 Subject: [PATCH] sv_binutils: introduce Struct helper class --- src/openpower/sv/sv_binutils.py | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/openpower/sv/sv_binutils.py b/src/openpower/sv/sv_binutils.py index 462c41a8..f2ec62a1 100644 --- a/src/openpower/sv/sv_binutils.py +++ b/src/openpower/sv/sv_binutils.py @@ -109,6 +109,41 @@ class Constant(CType, _enum.Enum, metaclass=EnumMeta): Mode = Constant("Mode", _SVP64MODE) +class StructMeta(type): + def __new__(metacls, name, bases, attrs, tag=None): + if tag is None: + tag = f"svp64_{name.lower()}" + + cls = super().__new__(metacls, name, bases, attrs) + cls.__tag = tag + + return cls + + @property + def c_tag(cls): + return cls.__tag + + def c_decl(cls): + yield f"struct {cls.c_tag} {{" + for field in _dataclasses.fields(cls): + yield from indent(field.type.c_var(name=f"{field.name};")) + yield f"}};" + + def c_var(cls, name): + yield f"struct {cls.c_tag} {name}" + + +@_dataclasses.dataclass(eq=True, frozen=True) +class Struct(metaclass=StructMeta): + def c_value(self, prefix="", suffix=""): + yield f"{prefix}{{" + for field in _dataclasses.fields(self): + name = field.name + attr = getattr(self, name) + yield from indent(attr.c_value(prefix=f".{name} = ", suffix=",")) + yield f"}}{suffix}" + + class Opcode(CType): def __init__(self, value, mask, bits): self.__value = value -- 2.30.2