hdl.ast: simplify {bit,word}_select with constant offset.

We don't have any other convenient shortcut for x[off*w:(off+1)*w],
but using word_select to extract a single static range would result
in severe bloat of emitted code through expansion to dead branches.
Recognize and simplify this pattern.
This commit is contained in:
whitequark 2019-10-26 00:09:53 +00:00
parent 61e6267daf
commit 51269ad4a0
2 changed files with 21 additions and 0 deletions

View file

@ -304,6 +304,9 @@ class Value(metaclass=ABCMeta):
Part, out
Selected part of the ``Value``
"""
offset = Value.cast(offset)
if type(offset) is Const and isinstance(width, int):
return self[offset.value:offset.value + width]
return Part(self, offset, width, stride=1, src_loc_at=1)
def word_select(self, offset, width):
@ -324,6 +327,9 @@ class Value(metaclass=ABCMeta):
Part, out
Selected part of the ``Value``
"""
offset = Value.cast(offset)
if type(offset) is Const and isinstance(width, int):
return self[offset.value * width:(offset.value + 1) * width]
return Part(self, offset, width, stride=width, src_loc_at=1)
def matches(self, *patterns):