hdl.ast: simplify enum handling.
authorwhitequark <cz@m-labs.hk>
Fri, 11 Oct 2019 11:16:00 +0000 (11:16 +0000)
committerwhitequark <cz@m-labs.hk>
Fri, 11 Oct 2019 11:16:00 +0000 (11:16 +0000)
nmigen/hdl/ast.py
nmigen/test/test_hdl_ast.py

index 64e708268dc799a21378e504eaa3c38bc91cf255..db1a70bba811d67963b3f69f286d266724be6717 100644 (file)
@@ -40,11 +40,6 @@ def _enum_shape(enum_type):
     return (width, signed)
 
 
-def _enum_to_bits(enum_value):
-    width, signed = _enum_shape(type(enum_value))
-    return format(enum_value.value & ((1 << width) - 1), "b").rjust(width, "0")
-
-
 class Value(metaclass=ABCMeta):
     @staticmethod
     def cast(obj):
@@ -300,14 +295,14 @@ class Value(metaclass=ABCMeta):
                               .format(pattern, len(self)),
                               SyntaxWarning, stacklevel=3)
                 continue
-            if isinstance(pattern, int):
-                matches.append(self == pattern)
-            elif isinstance(pattern, (str, Enum)):
-                if isinstance(pattern, Enum):
-                    pattern = _enum_to_bits(pattern)
+            if isinstance(pattern, str):
                 mask    = int(pattern.replace("0", "1").replace("-", "0"), 2)
                 pattern = int(pattern.replace("-", "0"), 2)
                 matches.append((self & mask) == pattern)
+            elif isinstance(pattern, int):
+                matches.append(self == pattern)
+            elif isinstance(pattern, Enum):
+                matches.append(self == pattern.value)
             else:
                 assert False
         if not matches:
@@ -1304,12 +1299,12 @@ class Switch(Statement):
             # Map: 2 -> "0010"; "0010" -> "0010"
             new_keys = ()
             for key in keys:
-                if isinstance(key, (bool, int)):
-                    key = "{:0{}b}".format(key, len(self.test))
-                elif isinstance(key, str):
+                if isinstance(key, str):
                     pass
+                elif isinstance(key, int):
+                    key = format(key, "b").rjust(len(self.test), "0")
                 elif isinstance(key, Enum):
-                    key = _enum_to_bits(key)
+                    key = format(key.value, "b").rjust(len(self.test), "0")
                 else:
                     raise TypeError("Object '{!r}' cannot be used as a switch key"
                                     .format(key))
index 362e2a8bb48608365d7b461814136cf77c34ceb3..1a2bb17f299b5e16ae5cb0ef6732b09a2abdc5be 100644 (file)
@@ -346,7 +346,7 @@ class OperatorTestCase(FHDLTestCase):
     def test_matches_enum(self):
         s = Signal.enum(SignedEnum)
         self.assertRepr(s.matches(SignedEnum.FOO), """
-        (== (& (sig s) (const 2'd3)) (const 2'd3))
+        (== (sig s) (const 1'sd-1))
         """)
 
     def test_matches_width_wrong(self):