hdl._ast: fix shift_right and as_signed edge cases.

This commit is contained in:
Wanda 2024-02-13 05:31:51 +01:00 committed by Catherine
parent 0056e982c5
commit e2fd819742
2 changed files with 26 additions and 4 deletions

View file

@ -483,7 +483,14 @@ class Value(metaclass=ABCMeta):
Returns
-------
:class:`Value`, :pc:`signed(len(self))`, :ref:`assignable <lang-assignable>`
Raises
------
ValueError
If :pc:`len(self) == 0`.
"""
if len(self) == 0:
raise ValueError("Cannot create a 0-width signed value")
return Operator("s", [self])
def __bool__(self):
@ -900,9 +907,9 @@ class Value(metaclass=ABCMeta):
Returns
-------
:class:`Value`, :pc:`unsigned(len(self) + amount)`
:class:`Value`, :pc:`unsigned(max(len(self) + amount, 0))`
If :pc:`self` is unsigned.
:class:`Value`, :pc:`signed(len(self) + amount)`
:class:`Value`, :pc:`signed(max(len(self) + amount, 1))`
If :pc:`self` is signed.
"""
if not isinstance(amount, int):
@ -974,6 +981,8 @@ class Value(metaclass=ABCMeta):
if amount < 0:
return self.shift_left(-amount)
if self.shape().signed:
if amount >= len(self):
amount = len(self) - 1
return self[amount:].as_signed()
else:
return self[amount:] # unsigned