part of the nmigen Type (2) dsl.Module abstraction from Type (1) ast.*
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 2 Oct 2021 15:24:42 +0000 (16:24 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 2 Oct 2021 15:24:42 +0000 (16:24 +0100)
dsl.Module is passed in an AST Type which it may use to convert its
If, Elif and Switch statements to

nmigen/hdl/dsl.py

index 337e176338b73d20298d9aed6c251fb77c98c495..90c29766a3cc6ddf12afd5cf3d18719d5e77c9c3 100644 (file)
@@ -163,7 +163,7 @@ class Module(_ModuleBuilderRoot, Elaboratable):
         raise SyntaxError("Instead of inheriting from `Module`, inherit from `Elaboratable` "
                           "and return a `Module` from the `elaborate(self, platform)` method")
 
-    def __init__(self):
+    def __init__(self, _astType=None):
         _ModuleBuilderRoot.__init__(self, self, depth=0)
         self.submodules    = _ModuleBuilderSubmodules(self)
         self.domains       = _ModuleBuilderDomainSet(self)
@@ -178,6 +178,10 @@ class Module(_ModuleBuilderRoot, Elaboratable):
         self._domains      = {}
         self._generated    = {}
 
+        # to complete the Type 1 (ast.*) nmigen language construct abstraction
+        # from Type 2 (Module - this class) Module must be told what AST type
+        # it may cast m.If/Elif conditions and m.Switch
+
     def _check_context(self, construct, context):
         if self._ctrl_context != context:
             if self._ctrl_context is None:
@@ -209,7 +213,7 @@ class Module(_ModuleBuilderRoot, Elaboratable):
         return data
 
     def _check_signed_cond(self, cond):
-        cond = Value.cast(cond)
+        cond = self._astType.cast(cond)
         width, signed = cond.shape()
         if signed:
             warnings.warn("Signed values in If/Elif conditions usually result from inverting "
@@ -286,7 +290,7 @@ class Module(_ModuleBuilderRoot, Elaboratable):
     def Switch(self, test):
         self._check_context("Switch", context=None)
         switch_data = self._set_ctrl("Switch", {
-            "test":    Value.cast(test),
+            "test":    self._astType.cast(test),
             "cases":   OrderedDict(),
             "src_loc": tracer.get_src_loc(src_loc_at=1),
             "case_src_locs": {},
@@ -433,13 +437,6 @@ class Module(_ModuleBuilderRoot, Elaboratable):
             tests, cases = [], OrderedDict()
             for if_test, if_case in zip(if_tests + [None], if_bodies):
                 if if_test is not None:
-                    # check if the if_test is not considered boolean, and only
-                    # append a converted version if it is not. this defers the
-                    # Value.cast (which would lose type if done mandatorially).
-                    # ast._InternalSwitch does a (second) Value.cast anyway
-                    _if_test = Value.cast(if_test)
-                    if not _if_test.considered_bool():
-                        if_test = _if_test.bool()
                     tests.append(if_test)
 
                 if if_test is not None: