amaranth-lang / amaranth

A modern hardware definition language and toolchain based on Python
https://amaranth-lang.org/docs/amaranth/
BSD 2-Clause "Simplified" License
1.54k stars 170 forks source link

Constructing a `signed(0)`-shaped Const fails with `ValueError`. #807

Closed mwkmwkmwk closed 1 year ago

mwkmwkmwk commented 1 year ago

While a signed shape with width 0 is allowed by the model, it has unclear semantics, and attempting to actually use it results in errors:

>>> Const(0, signed(0))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mwk/amaranth/amaranth/hdl/ast.py", line 650, in __init__
    if self.signed and self.value >> (self.width - 1) & 1:
                       ~~~~~~~~~~~^^~~~~~~~~~~~~~~~~~
ValueError: negative shift count
whitequark commented 1 year ago

Denominating from discussion because this has a simple resolution:

Values are bit vectors (Sequence containers). These containers do not have elements at negative indices and give special meaning to them when they are used in __getitem__. Since there is no "bit -1" there is no sign bit and sign extension is impossible.

For the same reason, Part with a signed offset should not be allowed.