back.rtlil: fix expansion of Part() for partial dummy writes.
authorwhitequark <whitequark@whitequark.org>
Mon, 13 Apr 2020 15:56:39 +0000 (15:56 +0000)
committerwhitequark <whitequark@whitequark.org>
Mon, 13 Apr 2020 15:56:39 +0000 (15:56 +0000)
Before this commit, selecting a part that was fully out of bounds of
a value was correctly implemented as a write to a dummy wire, but
selecting a part that was only partially out of bounds resulted in
a crash.

Fixes #351.

nmigen/back/rtlil.py

index c074a97f5042fd8c9a04c393192d7efc7077d2ef..8433fe263af7edb5f2e69eeeca0302a553a4bfdb 100644 (file)
@@ -632,12 +632,14 @@ class _LHSValueCompiler(_ValueCompiler):
     def on_Part(self, value):
         offset = self.s.expand(value.offset)
         if isinstance(offset, ast.Const):
-            if offset.value == len(value.value):
-                dummy_wire = self.s.rtlil.wire(value.width)
-                return dummy_wire
-            return self(ast.Slice(value.value,
-                                  offset.value * value.stride,
-                                  offset.value * value.stride + value.width))
+            start = offset.value * value.stride
+            stop  = start + value.width
+            slice = self(ast.Slice(value.value, start, min(len(value.value), stop)))
+            if len(value.value) >= stop:
+                return slice
+            else:
+                dummy_wire = self.s.rtlil.wire(stop - len(value.value))
+                return "{{ {} {} }}".format(dummy_wire, slice)
         else:
             # Only so many possible parts. The amount of branches is exponential; if value.offset
             # is large (e.g. 32-bit wide), trying to naively legalize it is likely to exhaust