sv_binutils: introduce Bitmap class
authorDmitry Selyutin <dmitry.selyutin@3mdeb.com>
Mon, 25 Apr 2022 09:13:36 +0000 (09:13 +0000)
committerDmitry Selyutin <dmitry.selyutin@3mdeb.com>
Mon, 25 Apr 2022 09:13:36 +0000 (09:13 +0000)
src/openpower/sv/sv_binutils.py

index 4b66e3b23dfb7dd041edbb8ef21e7472ee0ef98c..2c4b246f8d6fdcc81448498abdd2e313e9a66de2 100644 (file)
@@ -59,8 +59,8 @@ class CTypeMeta(type):
 
 
 class ArrayMeta(CTypeMeta):
-    def __new__(metacls, name, bases, attrs, type, size):
-        cls = super().__new__(metacls, name, bases, attrs)
+    def __new__(metacls, name, bases, attrs, type, size, **kwargs):
+        cls = super().__new__(metacls, name, bases, attrs, **kwargs)
         cls.__type = type
         cls.__ellipsis = (size is Ellipsis)
         cls.__size = 0 if cls.__ellipsis else size
@@ -70,17 +70,26 @@ class ArrayMeta(CTypeMeta):
     def __len__(cls):
         return cls.__size
 
-    @property
-    def c_type(cls):
-        return cls.__type
-
     def c_decl(cls):
         size = "" if cls.__ellipsis else f"{cls.__size}"
-        yield f"{cls.c_type.c_typedef}[{size}]"
+        yield f"{cls.__type.c_typedef}[{size}]"
 
     def c_var(cls, name, prefix="", suffix=""):
         size = "" if cls.__ellipsis else f"{cls.__size}"
-        yield f"{prefix}{cls.c_type.c_typedef} {name}[{size}]{suffix}"
+        yield f"{prefix}{cls.__type.c_typedef} {name}[{size}]{suffix}"
+
+
+class BitmapMeta(CTypeMeta):
+    def __new__(metacls, name, bases, attrs, typedef="uint64_t", bits=0, **kwargs):
+        cls = super().__new__(metacls, name, bases, attrs, typedef=typedef, **kwargs)
+        cls.__bits = bits
+        return cls
+
+    def __len__(cls):
+        return cls.__bits
+
+    def c_var(cls, name, prefix="", suffix=""):
+        yield f"{prefix}{cls.c_typedef} {name} : {cls.__bits}{suffix}"
 
 
 class CType(metaclass=CTypeMeta):
@@ -89,7 +98,7 @@ class CType(metaclass=CTypeMeta):
         yield from ()
 
 
-class Array(CType, tuple, metaclass=ArrayMeta, type=CType, size=0):
+class Array(CType, tuple, metaclass=ArrayMeta, type=CType, size=...):
     def c_value(self, prefix="", suffix=""):
         yield f"{prefix}{{"
         for (index, item) in enumerate(self):
@@ -97,6 +106,10 @@ class Array(CType, tuple, metaclass=ArrayMeta, type=CType, size=0):
         yield f"}}{suffix}"
 
 
+class Bitmap(metaclass=BitmapMeta):
+    pass
+
+
 class EnumMeta(_enum.EnumMeta, CTypeMeta):
     def __call__(metacls, name, entries, tag=None, **kwargs):
         if isinstance(entries, type) and issubclass(entries, _enum.Enum):